advapi32: Make rpcrt4 a delayed import to work around circular dependencies with...
[wine/testsucceed.git] / dlls / comctl32 / header.c
blobd916c7297d505acbf077e163a20f9e25d25bd831
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 hwndSelf; /* Control window */
68 HWND hwndNotify; /* Owner window to send notifications to */
69 INT nNotifyFormat; /* format used for WM_NOTIFY messages */
70 UINT uNumItem; /* number of items (columns) */
71 INT nHeight; /* height of the header (pixels) */
72 HFONT hFont; /* handle to the current font */
73 HCURSOR hcurArrow; /* handle to the arrow cursor */
74 HCURSOR hcurDivider; /* handle to a cursor (used over dividers) <-|-> */
75 HCURSOR hcurDivopen; /* handle to a cursor (used over dividers) <-||-> */
76 BOOL bCaptured; /* Is the mouse captured? */
77 BOOL bPressed; /* Is a header item pressed (down)? */
78 BOOL bDragging; /* Are we dragging an item? */
79 BOOL bTracking; /* Is in tracking mode? */
80 POINT ptLButtonDown; /* The point where the left button was pressed */
81 DWORD dwStyle; /* the cached window GWL_STYLE */
82 INT iMoveItem; /* index of tracked item. (Tracking mode) */
83 INT xTrackOffset; /* distance between the right side of the tracked item and the cursor */
84 INT xOldTrack; /* track offset (see above) after the last WM_MOUSEMOVE */
85 INT iHotItem; /* index of hot item (cursor is over this item) */
86 INT iHotDivider; /* index of the hot divider (used while dragging an item or by HDM_SETHOTDIVIDER) */
87 INT iMargin; /* width of the margin that surrounds a bitmap */
89 HIMAGELIST himl; /* handle to an image list (may be 0) */
90 HEADER_ITEM *items; /* pointer to array of HEADER_ITEM's */
91 INT *order; /* array of item IDs indexed by order */
92 BOOL bRectsValid; /* validity flag for bounding rectangles */
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;
294 static INT
295 HEADER_DrawItem (HEADER_INFO *infoPtr, HDC hdc, INT iItem, BOOL bHotTrack, LRESULT lCDFlags)
297 HEADER_ITEM *phdi = &infoPtr->items[iItem];
298 RECT r;
299 INT oldBkMode;
300 HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
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, comctl32_color.clr3dFace);
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(infoPtr, 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 (!(infoPtr->dwStyle & HDS_FLAT))
342 if (infoPtr->dwStyle & HDS_BUTTONS) {
343 if (phdi->bDown) {
344 DrawEdge (hdc, &r, BDR_RAISEDOUTER,
345 BF_RECT | BF_FLAT | BF_MIDDLE | BF_ADJUST);
347 else
348 DrawEdge (hdc, &r, EDGE_RAISED,
349 BF_RECT | BF_SOFT | BF_MIDDLE | BF_ADJUST);
351 else
352 DrawEdge (hdc, &r, EDGE_ETCHED, BF_BOTTOM | BF_RIGHT | BF_ADJUST);
355 hbr = CreateSolidBrush(GetBkColor(hdc));
356 FillRect(hdc, &r, hbr);
357 DeleteObject(hbr);
359 if (phdi->bDown) {
360 r.left += 2;
361 r.top += 2;
364 if (phdi->fmt & HDF_OWNERDRAW) {
365 DRAWITEMSTRUCT dis;
367 dis.CtlType = ODT_HEADER;
368 dis.CtlID = GetWindowLongPtrW (infoPtr->hwndSelf, GWLP_ID);
369 dis.itemID = iItem;
370 dis.itemAction = ODA_DRAWENTIRE;
371 dis.itemState = phdi->bDown ? ODS_SELECTED : 0;
372 dis.hwndItem = infoPtr->hwndSelf;
373 dis.hDC = hdc;
374 dis.rcItem = phdi->rect;
375 dis.itemData = phdi->lParam;
376 oldBkMode = SetBkMode(hdc, TRANSPARENT);
377 SendMessageW (infoPtr->hwndNotify, WM_DRAWITEM, dis.CtlID, (LPARAM)&dis);
378 if (oldBkMode != TRANSPARENT)
379 SetBkMode(hdc, oldBkMode);
381 else {
382 UINT rw, rh, /* width and height of r */
383 *x = NULL, *w = NULL; /* x and width of the pic (bmp or img) which is part of cnt */
384 /* cnt,txt,img,bmp */
385 UINT cx, tx, ix, bx,
386 cw, tw, iw, bw;
387 BITMAP bmp;
389 HEADER_PrepareCallbackItems(infoPtr, iItem, HDI_TEXT|HDI_IMAGE);
390 cw = tw = iw = bw = 0;
391 rw = r.right - r.left;
392 rh = r.bottom - r.top;
394 if (phdi->fmt & HDF_STRING) {
395 RECT textRect;
397 SetRectEmpty(&textRect);
398 DrawTextW (hdc, phdi->pszText, -1,
399 &textRect, DT_LEFT|DT_VCENTER|DT_SINGLELINE|DT_CALCRECT);
400 cw = textRect.right - textRect.left + 2 * infoPtr->iMargin;
403 if ((phdi->fmt & HDF_IMAGE) && (infoPtr->himl)) {
404 iw = infoPtr->himl->cx + 2 * infoPtr->iMargin;
405 x = &ix;
406 w = &iw;
409 if ((phdi->fmt & HDF_BITMAP) && (phdi->hbm)) {
410 GetObjectW (phdi->hbm, sizeof(BITMAP), &bmp);
411 bw = bmp.bmWidth + 2 * infoPtr->iMargin;
412 if (!iw) {
413 x = &bx;
414 w = &bw;
418 if (bw || iw)
419 cw += *w;
421 /* align cx using the unclipped cw */
422 if ((phdi->fmt & HDF_JUSTIFYMASK) == HDF_LEFT)
423 cx = r.left;
424 else if ((phdi->fmt & HDF_JUSTIFYMASK) == HDF_CENTER)
425 cx = r.left + rw / 2 - cw / 2;
426 else /* HDF_RIGHT */
427 cx = r.right - cw;
429 /* clip cx & cw */
430 if (cx < r.left)
431 cx = r.left;
432 if (cx + cw > r.right)
433 cw = r.right - cx;
435 tx = cx + infoPtr->iMargin;
436 /* since cw might have changed we have to recalculate tw */
437 tw = cw - infoPtr->iMargin * 2;
439 if (iw || bw) {
440 tw -= *w;
441 if (phdi->fmt & HDF_BITMAP_ON_RIGHT) {
442 /* put pic behind text */
443 *x = cx + tw + infoPtr->iMargin * 3;
444 } else {
445 *x = cx + infoPtr->iMargin;
446 /* move text behind pic */
447 tx += *w;
451 if (iw && bw) {
452 /* since we're done with the layout we can
453 now calculate the position of bmp which
454 has no influence on alignment and layout
455 because of img */
456 if ((phdi->fmt & HDF_JUSTIFYMASK) == HDF_RIGHT)
457 bx = cx - bw + infoPtr->iMargin;
458 else
459 bx = cx + cw + infoPtr->iMargin;
462 if (iw || bw) {
463 HDC hClipDC = GetDC(infoPtr->hwndSelf);
464 HRGN hClipRgn = CreateRectRgn(r.left, r.top, r.right, r.bottom);
465 SelectClipRgn(hClipDC, hClipRgn);
467 if (bw) {
468 HDC hdcBitmap = CreateCompatibleDC (hClipDC);
469 SelectObject (hdcBitmap, phdi->hbm);
470 BitBlt (hClipDC, bx, r.top + ((INT)rh - bmp.bmHeight) / 2,
471 bmp.bmWidth, bmp.bmHeight, hdcBitmap, 0, 0, SRCCOPY);
472 DeleteDC (hdcBitmap);
475 if (iw) {
476 ImageList_DrawEx (infoPtr->himl, phdi->iImage, hClipDC,
477 ix, r.top + ((INT)rh - infoPtr->himl->cy) / 2,
478 infoPtr->himl->cx, infoPtr->himl->cy, CLR_DEFAULT, CLR_DEFAULT, 0);
481 DeleteObject(hClipRgn);
482 ReleaseDC(infoPtr->hwndSelf, hClipDC);
485 if (((phdi->fmt & HDF_STRING)
486 || (!(phdi->fmt & (HDF_OWNERDRAW|HDF_STRING|HDF_BITMAP|
487 HDF_BITMAP_ON_RIGHT|HDF_IMAGE)))) /* no explicit format specified? */
488 && (phdi->pszText)) {
489 oldBkMode = SetBkMode(hdc, TRANSPARENT);
490 r.left = tx;
491 r.right = tx + tw;
492 DrawTextW (hdc, phdi->pszText, -1,
493 &r, DT_LEFT|DT_END_ELLIPSIS|DT_VCENTER|DT_SINGLELINE);
494 if (oldBkMode != TRANSPARENT)
495 SetBkMode(hdc, oldBkMode);
497 HEADER_FreeCallbackItems(phdi);
498 }/*Ownerdrawn*/
500 return phdi->rect.right;
503 static void
504 HEADER_DrawHotDivider(const HEADER_INFO *infoPtr, HDC hdc)
506 HBRUSH brush;
507 RECT r;
509 HEADER_GetHotDividerRect(infoPtr, &r);
510 brush = CreateSolidBrush(comctl32_color.clrHighlight);
511 FillRect(hdc, &r, brush);
512 DeleteObject(brush);
515 static void
516 HEADER_Refresh (HEADER_INFO *infoPtr, HDC hdc)
518 HFONT hFont, hOldFont;
519 RECT rect, rcRest;
520 HBRUSH hbrBk;
521 UINT i;
522 INT x;
523 LRESULT lCDFlags;
524 HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
526 if (!infoPtr->bRectsValid)
527 HEADER_SetItemBounds(infoPtr);
529 /* get rect for the bar, adjusted for the border */
530 GetClientRect (infoPtr->hwndSelf, &rect);
531 lCDFlags = HEADER_SendCtrlCustomDraw(infoPtr, 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(infoPtr,i);
548 if (RectVisible(hdc, &infoPtr->items[idx].rect))
549 HEADER_DrawItem(infoPtr, 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 if (infoPtr->dwStyle & HDS_FLAT) {
560 hbrBk = GetSysColorBrush(COLOR_3DFACE);
561 FillRect(hdc, &rcRest, hbrBk);
563 else
565 if (infoPtr->dwStyle & HDS_BUTTONS)
566 DrawEdge (hdc, &rcRest, EDGE_RAISED, BF_TOP|BF_LEFT|BF_BOTTOM|BF_SOFT|BF_MIDDLE);
567 else
568 DrawEdge (hdc, &rcRest, EDGE_ETCHED, BF_BOTTOM|BF_MIDDLE);
572 if (infoPtr->iHotDivider != -1)
573 HEADER_DrawHotDivider(infoPtr, hdc);
575 if (infoPtr->bDragging)
576 ImageList_DragShowNolock(TRUE);
577 SelectObject (hdc, hOldFont);
579 if (lCDFlags & CDRF_NOTIFYPOSTPAINT)
580 HEADER_SendCtrlCustomDraw(infoPtr, CDDS_POSTPAINT, hdc, &rect);
584 static void
585 HEADER_RefreshItem (HEADER_INFO *infoPtr, INT iItem)
587 if (!infoPtr->bRectsValid)
588 HEADER_SetItemBounds(infoPtr);
590 InvalidateRect(infoPtr->hwndSelf, &infoPtr->items[iItem].rect, FALSE);
594 static void
595 HEADER_InternalHitTest (const HEADER_INFO *infoPtr, const POINT *lpPt, UINT *pFlags, INT *pItem)
597 RECT rect, rcTest;
598 UINT iCount;
599 INT width;
600 BOOL bNoWidth;
602 GetClientRect (infoPtr->hwndSelf, &rect);
604 *pFlags = 0;
605 bNoWidth = FALSE;
606 if (PtInRect (&rect, *lpPt))
608 if (infoPtr->uNumItem == 0) {
609 *pFlags |= HHT_NOWHERE;
610 *pItem = 1;
611 TRACE("NOWHERE\n");
612 return;
614 else {
615 /* somewhere inside */
616 for (iCount = 0; iCount < infoPtr->uNumItem; iCount++) {
617 rect = infoPtr->items[iCount].rect;
618 width = rect.right - rect.left;
619 if (width == 0) {
620 bNoWidth = TRUE;
621 continue;
623 if (PtInRect (&rect, *lpPt)) {
624 if (width <= 2 * DIVIDER_WIDTH) {
625 *pFlags |= HHT_ONHEADER;
626 *pItem = iCount;
627 TRACE("ON HEADER %d\n", iCount);
628 return;
630 if (HEADER_IndexToOrder(infoPtr, iCount) > 0) {
631 rcTest = rect;
632 rcTest.right = rcTest.left + DIVIDER_WIDTH;
633 if (PtInRect (&rcTest, *lpPt)) {
634 if (HEADER_IsItemFixed(infoPtr, HEADER_PrevItem(infoPtr, iCount)))
636 *pFlags |= HHT_ONHEADER;
637 *pItem = iCount;
638 TRACE("ON HEADER %d\n", *pItem);
639 return;
641 if (bNoWidth) {
642 *pFlags |= HHT_ONDIVOPEN;
643 *pItem = HEADER_PrevItem(infoPtr, iCount);
644 TRACE("ON DIVOPEN %d\n", *pItem);
645 return;
647 else {
648 *pFlags |= HHT_ONDIVIDER;
649 *pItem = HEADER_PrevItem(infoPtr, iCount);
650 TRACE("ON DIVIDER %d\n", *pItem);
651 return;
655 rcTest = rect;
656 rcTest.left = rcTest.right - DIVIDER_WIDTH;
657 if (!HEADER_IsItemFixed(infoPtr, iCount) && PtInRect (&rcTest, *lpPt))
659 *pFlags |= HHT_ONDIVIDER;
660 *pItem = iCount;
661 TRACE("ON DIVIDER %d\n", *pItem);
662 return;
665 *pFlags |= HHT_ONHEADER;
666 *pItem = iCount;
667 TRACE("ON HEADER %d\n", iCount);
668 return;
672 /* check for last divider part (on nowhere) */
673 if (!HEADER_IsItemFixed(infoPtr, infoPtr->uNumItem - 1))
675 rect = infoPtr->items[infoPtr->uNumItem-1].rect;
676 rect.left = rect.right;
677 rect.right += DIVIDER_WIDTH;
678 if (PtInRect (&rect, *lpPt)) {
679 if (bNoWidth) {
680 *pFlags |= HHT_ONDIVOPEN;
681 *pItem = infoPtr->uNumItem - 1;
682 TRACE("ON DIVOPEN %d\n", *pItem);
683 return;
685 else {
686 *pFlags |= HHT_ONDIVIDER;
687 *pItem = infoPtr->uNumItem - 1;
688 TRACE("ON DIVIDER %d\n", *pItem);
689 return;
694 *pFlags |= HHT_NOWHERE;
695 *pItem = 1;
696 TRACE("NOWHERE\n");
697 return;
700 else {
701 if (lpPt->x < rect.left) {
702 TRACE("TO LEFT\n");
703 *pFlags |= HHT_TOLEFT;
705 else if (lpPt->x > rect.right) {
706 TRACE("TO RIGHT\n");
707 *pFlags |= HHT_TORIGHT;
710 if (lpPt->y < rect.top) {
711 TRACE("ABOVE\n");
712 *pFlags |= HHT_ABOVE;
714 else if (lpPt->y > rect.bottom) {
715 TRACE("BELOW\n");
716 *pFlags |= HHT_BELOW;
720 *pItem = 1;
721 TRACE("flags=0x%X\n", *pFlags);
722 return;
726 static void
727 HEADER_DrawTrackLine (const HEADER_INFO *infoPtr, HDC hdc, INT x)
729 RECT rect;
730 HPEN hOldPen;
731 INT oldRop;
733 GetClientRect (infoPtr->hwndSelf, &rect);
735 hOldPen = SelectObject (hdc, GetStockObject (BLACK_PEN));
736 oldRop = SetROP2 (hdc, R2_XORPEN);
737 MoveToEx (hdc, x, rect.top, NULL);
738 LineTo (hdc, x, rect.bottom);
739 SetROP2 (hdc, oldRop);
740 SelectObject (hdc, hOldPen);
743 /***
744 * DESCRIPTION:
745 * Convert a HDITEM into the correct format (ANSI/Unicode) to send it in a notify
747 * PARAMETER(S):
748 * [I] infoPtr : the header that wants to send the notify
749 * [O] dest : The buffer to store the HDITEM for notify. It may be set to a HDITEMA of HDITEMW
750 * [I] src : The source HDITEM. It may be a HDITEMA or HDITEMW
751 * [I] fSourceUnicode : is src a HDITEMW or HDITEMA
752 * [O] ppvScratch : a pointer to a scratch buffer that needs to be freed after
753 * the HDITEM is no longer in use or NULL if none was needed
755 * NOTE: We depend on HDITEMA and HDITEMW having the same structure
757 static void HEADER_CopyHDItemForNotify(const HEADER_INFO *infoPtr, HDITEMW *dest,
758 const HDITEMW *src, BOOL fSourceUnicode, LPVOID *ppvScratch)
760 *ppvScratch = NULL;
761 *dest = *src;
763 if (src->mask & HDI_TEXT && src->pszText != LPSTR_TEXTCALLBACKW) /* covers TEXTCALLBACKA as well */
765 if (fSourceUnicode && infoPtr->nNotifyFormat != NFR_UNICODE)
767 dest->pszText = NULL;
768 Str_SetPtrWtoA((LPSTR *)&dest->pszText, src->pszText);
769 *ppvScratch = dest->pszText;
772 if (!fSourceUnicode && infoPtr->nNotifyFormat == NFR_UNICODE)
774 dest->pszText = NULL;
775 Str_SetPtrAtoW(&dest->pszText, (LPSTR)src->pszText);
776 *ppvScratch = dest->pszText;
781 static UINT HEADER_NotifyCodeWtoA(UINT code)
783 /* we use the fact that all the unicode messages are in HDN_FIRST_UNICODE..HDN_LAST*/
784 if (code >= HDN_LAST && code <= HDN_FIRST_UNICODE)
785 return code + HDN_UNICODE_OFFSET;
786 else
787 return code;
790 static LRESULT
791 HEADER_SendNotify(const HEADER_INFO *infoPtr, UINT code, NMHDR *nmhdr)
793 nmhdr->hwndFrom = infoPtr->hwndSelf;
794 nmhdr->idFrom = GetWindowLongPtrW (infoPtr->hwndSelf, GWLP_ID);
795 nmhdr->code = code;
797 return SendMessageW(infoPtr->hwndNotify, WM_NOTIFY,
798 nmhdr->idFrom, (LPARAM)nmhdr);
801 static BOOL
802 HEADER_SendSimpleNotify (const HEADER_INFO *infoPtr, UINT code)
804 NMHDR nmhdr;
805 return (BOOL)HEADER_SendNotify(infoPtr, code, &nmhdr);
808 static LRESULT
809 HEADER_SendCtrlCustomDraw(const HEADER_INFO *infoPtr, DWORD dwDrawStage, HDC hdc, const RECT *rect)
811 NMCUSTOMDRAW nm;
812 nm.dwDrawStage = dwDrawStage;
813 nm.hdc = hdc;
814 nm.rc = *rect;
815 nm.dwItemSpec = 0;
816 nm.uItemState = 0;
817 nm.lItemlParam = 0;
819 return HEADER_SendNotify(infoPtr, NM_CUSTOMDRAW, (NMHDR *)&nm);
822 static BOOL
823 HEADER_SendNotifyWithHDItemT(const HEADER_INFO *infoPtr, UINT code, INT iItem, HDITEMW *lpItem)
825 NMHEADERW nmhdr;
827 if (infoPtr->nNotifyFormat != NFR_UNICODE)
828 code = HEADER_NotifyCodeWtoA(code);
829 nmhdr.iItem = iItem;
830 nmhdr.iButton = 0;
831 nmhdr.pitem = lpItem;
833 return (BOOL)HEADER_SendNotify(infoPtr, code, (NMHDR *)&nmhdr);
836 static BOOL
837 HEADER_SendNotifyWithIntFieldT(const HEADER_INFO *infoPtr, UINT code, INT iItem, INT mask, INT iValue)
839 HDITEMW nmitem;
841 /* copying only the iValue should be ok but to make the code more robust we copy everything */
842 nmitem.cxy = infoPtr->items[iItem].cxy;
843 nmitem.hbm = infoPtr->items[iItem].hbm;
844 nmitem.pszText = NULL;
845 nmitem.cchTextMax = 0;
846 nmitem.fmt = infoPtr->items[iItem].fmt;
847 nmitem.lParam = infoPtr->items[iItem].lParam;
848 nmitem.iOrder = infoPtr->items[iItem].iOrder;
849 nmitem.iImage = infoPtr->items[iItem].iImage;
851 nmitem.mask = mask;
852 switch (mask)
854 case HDI_WIDTH:
855 nmitem.cxy = iValue;
856 break;
857 case HDI_ORDER:
858 nmitem.iOrder = iValue;
859 break;
860 default:
861 ERR("invalid mask value 0x%x\n", iValue);
864 return HEADER_SendNotifyWithHDItemT(infoPtr, code, iItem, &nmitem);
868 * Prepare callback items
869 * depends on NMHDDISPINFOW having same structure as NMHDDISPINFOA
870 * (so we handle the two cases only doing a specific cast for pszText).
871 * Checks if any of the required field are callback. If there are sends a
872 * NMHDISPINFO notify to retrieve these items. The items are stored in the
873 * HEADER_ITEM pszText and iImage fields. They should be freed with
874 * HEADER_FreeCallbackItems.
876 * @param hwnd : hwnd header container handler
877 * @param iItem : the header item id
878 * @param reqMask : required fields. If any of them is callback this function will fetch it
880 * @return TRUE on success, else FALSE
882 static BOOL
883 HEADER_PrepareCallbackItems(const HEADER_INFO *infoPtr, INT iItem, INT reqMask)
885 HEADER_ITEM *lpItem = &infoPtr->items[iItem];
886 DWORD mask = reqMask & lpItem->callbackMask;
887 NMHDDISPINFOW dispInfo;
888 void *pvBuffer = NULL;
890 if (mask == 0)
891 return TRUE;
892 if (mask&HDI_TEXT && lpItem->pszText != NULL)
894 ERR("(): function called without a call to FreeCallbackItems\n");
895 Free(lpItem->pszText);
896 lpItem->pszText = NULL;
899 memset(&dispInfo, 0, sizeof(NMHDDISPINFOW));
900 dispInfo.hdr.hwndFrom = infoPtr->hwndSelf;
901 dispInfo.hdr.idFrom = GetWindowLongPtrW (infoPtr->hwndSelf, GWLP_ID);
902 if (infoPtr->nNotifyFormat == NFR_UNICODE)
904 dispInfo.hdr.code = HDN_GETDISPINFOW;
905 if (mask & HDI_TEXT)
906 pvBuffer = Alloc(MAX_HEADER_TEXT_LEN * sizeof(WCHAR));
908 else
910 dispInfo.hdr.code = HDN_GETDISPINFOA;
911 if (mask & HDI_TEXT)
912 pvBuffer = Alloc(MAX_HEADER_TEXT_LEN * sizeof(CHAR));
914 dispInfo.pszText = pvBuffer;
915 dispInfo.cchTextMax = (pvBuffer!=NULL?MAX_HEADER_TEXT_LEN:0);
916 dispInfo.iItem = iItem;
917 dispInfo.mask = mask;
918 dispInfo.lParam = lpItem->lParam;
920 TRACE("Sending HDN_GETDISPINFO%c\n", infoPtr->nNotifyFormat == NFR_UNICODE?'W':'A');
921 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, dispInfo.hdr.idFrom, (LPARAM)&dispInfo);
923 TRACE("SendMessage returns(mask:0x%x,str:%s,lParam:%p)\n",
924 dispInfo.mask,
925 (infoPtr->nNotifyFormat == NFR_UNICODE ? debugstr_w(dispInfo.pszText) : (LPSTR) dispInfo.pszText),
926 (void*) dispInfo.lParam);
928 if (mask & HDI_IMAGE)
929 lpItem->iImage = dispInfo.iImage;
930 if (mask & HDI_TEXT)
932 if (infoPtr->nNotifyFormat == NFR_UNICODE)
934 lpItem->pszText = pvBuffer;
936 /* the user might have used his own buffer */
937 if (dispInfo.pszText != lpItem->pszText)
938 Str_GetPtrW(dispInfo.pszText, lpItem->pszText, MAX_HEADER_TEXT_LEN);
940 else
942 Str_SetPtrAtoW(&lpItem->pszText, (LPSTR)dispInfo.pszText);
943 Free(pvBuffer);
947 if (dispInfo.mask & HDI_DI_SETITEM)
949 /* make the items permanent */
950 lpItem->callbackMask &= ~dispInfo.mask;
953 return TRUE;
956 /***
957 * DESCRIPTION:
958 * Free the items that might be allocated with HEADER_PrepareCallbackItems
960 * PARAMETER(S):
961 * [I] lpItem : the item to free the data
964 static void
965 HEADER_FreeCallbackItems(HEADER_ITEM *lpItem)
967 if (lpItem->callbackMask&HDI_TEXT)
969 Free(lpItem->pszText);
970 lpItem->pszText = NULL;
973 if (lpItem->callbackMask&HDI_IMAGE)
974 lpItem->iImage = I_IMAGECALLBACK;
977 static LRESULT
978 HEADER_CreateDragImage (HEADER_INFO *infoPtr, INT iItem)
980 HEADER_ITEM *lpItem;
981 HIMAGELIST himl;
982 HBITMAP hMemory, hOldBitmap;
983 LRESULT lCDFlags;
984 RECT rc;
985 HDC hMemoryDC;
986 HDC hDeviceDC;
987 int height, width;
988 HFONT hFont;
990 if (iItem >= infoPtr->uNumItem)
991 return FALSE;
993 if (!infoPtr->bRectsValid)
994 HEADER_SetItemBounds(infoPtr);
996 lpItem = &infoPtr->items[iItem];
997 width = lpItem->rect.right - lpItem->rect.left;
998 height = lpItem->rect.bottom - lpItem->rect.top;
1000 hDeviceDC = GetDC(NULL);
1001 hMemoryDC = CreateCompatibleDC(hDeviceDC);
1002 hMemory = CreateCompatibleBitmap(hDeviceDC, width, height);
1003 ReleaseDC(NULL, hDeviceDC);
1004 hOldBitmap = SelectObject(hMemoryDC, hMemory);
1005 SetViewportOrgEx(hMemoryDC, -lpItem->rect.left, -lpItem->rect.top, NULL);
1006 hFont = infoPtr->hFont ? infoPtr->hFont : GetStockObject(SYSTEM_FONT);
1007 SelectObject(hMemoryDC, hFont);
1009 GetClientRect(infoPtr->hwndSelf, &rc);
1010 lCDFlags = HEADER_SendCtrlCustomDraw(infoPtr, CDDS_PREPAINT, hMemoryDC, &rc);
1011 HEADER_DrawItem(infoPtr, hMemoryDC, iItem, FALSE, lCDFlags);
1012 if (lCDFlags & CDRF_NOTIFYPOSTPAINT)
1013 HEADER_SendCtrlCustomDraw(infoPtr, CDDS_POSTPAINT, hMemoryDC, &rc);
1015 hMemory = SelectObject(hMemoryDC, hOldBitmap);
1016 DeleteDC(hMemoryDC);
1018 if (hMemory == NULL) /* if anything failed */
1019 return FALSE;
1021 himl = ImageList_Create(width, height, ILC_COLORDDB, 1, 1);
1022 ImageList_Add(himl, hMemory, NULL);
1023 DeleteObject(hMemory);
1024 return (LRESULT)himl;
1027 static LRESULT
1028 HEADER_SetHotDivider(HEADER_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
1030 INT iDivider;
1031 RECT r;
1033 if (wParam)
1035 POINT pt;
1036 UINT flags;
1037 pt.x = (INT)(SHORT)LOWORD(lParam);
1038 pt.y = 0;
1039 HEADER_InternalHitTest (infoPtr, &pt, &flags, &iDivider);
1041 if (flags & HHT_TOLEFT)
1042 iDivider = 0;
1043 else if (flags & HHT_NOWHERE || flags & HHT_TORIGHT)
1044 iDivider = infoPtr->uNumItem;
1045 else
1047 HEADER_ITEM *lpItem = &infoPtr->items[iDivider];
1048 if (pt.x > (lpItem->rect.left+lpItem->rect.right)/2)
1049 iDivider = HEADER_NextItem(infoPtr, iDivider);
1052 else
1053 iDivider = (INT)lParam;
1055 /* Note; wParam==FALSE, lParam==-1 is valid and is used to clear the hot divider */
1056 if (iDivider<-1 || iDivider>(int)infoPtr->uNumItem)
1057 return iDivider;
1059 if (iDivider != infoPtr->iHotDivider)
1061 if (infoPtr->iHotDivider != -1)
1063 HEADER_GetHotDividerRect(infoPtr, &r);
1064 InvalidateRect(infoPtr->hwndSelf, &r, FALSE);
1066 infoPtr->iHotDivider = iDivider;
1067 if (iDivider != -1)
1069 HEADER_GetHotDividerRect(infoPtr, &r);
1070 InvalidateRect(infoPtr->hwndSelf, &r, FALSE);
1073 return iDivider;
1076 static LRESULT
1077 HEADER_DeleteItem (HEADER_INFO *infoPtr, INT iItem)
1079 INT iOrder;
1080 UINT i;
1082 TRACE("[iItem=%d]\n", iItem);
1084 if ((iItem < 0) || (iItem >= (INT)infoPtr->uNumItem))
1085 return FALSE;
1087 for (i = 0; i < infoPtr->uNumItem; i++)
1088 TRACE("%d: order=%d, iOrder=%d, ->iOrder=%d\n", i, infoPtr->order[i], infoPtr->items[i].iOrder, infoPtr->items[infoPtr->order[i]].iOrder);
1090 iOrder = infoPtr->items[iItem].iOrder;
1091 Free(infoPtr->items[iItem].pszText);
1093 infoPtr->uNumItem--;
1094 memmove(&infoPtr->items[iItem], &infoPtr->items[iItem + 1],
1095 (infoPtr->uNumItem - iItem) * sizeof(HEADER_ITEM));
1096 memmove(&infoPtr->order[iOrder], &infoPtr->order[iOrder + 1],
1097 (infoPtr->uNumItem - iOrder) * sizeof(INT));
1098 infoPtr->items = ReAlloc(infoPtr->items, sizeof(HEADER_ITEM) * infoPtr->uNumItem);
1099 infoPtr->order = ReAlloc(infoPtr->order, sizeof(INT) * infoPtr->uNumItem);
1101 /* Correct the orders */
1102 for (i = 0; i < infoPtr->uNumItem; i++)
1104 if (infoPtr->order[i] > iItem)
1105 infoPtr->order[i]--;
1106 if (i >= iOrder)
1107 infoPtr->items[infoPtr->order[i]].iOrder = i;
1109 for (i = 0; i < infoPtr->uNumItem; i++)
1110 TRACE("%d: order=%d, iOrder=%d, ->iOrder=%d\n", i, infoPtr->order[i], infoPtr->items[i].iOrder, infoPtr->items[infoPtr->order[i]].iOrder);
1112 HEADER_SetItemBounds (infoPtr);
1113 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1115 return TRUE;
1119 static LRESULT
1120 HEADER_GetImageList (const HEADER_INFO *infoPtr)
1122 return (LRESULT)infoPtr->himl;
1126 static LRESULT
1127 HEADER_GetItemT (const HEADER_INFO *infoPtr, INT nItem, LPHDITEMW phdi, BOOL bUnicode)
1129 HEADER_ITEM *lpItem;
1130 UINT mask;
1132 if (!phdi)
1133 return FALSE;
1135 TRACE("[nItem=%d]\n", nItem);
1137 mask = phdi->mask;
1138 if (mask == 0)
1139 return TRUE;
1141 if ((nItem < 0) || (nItem >= (INT)infoPtr->uNumItem))
1142 return FALSE;
1144 if (mask & HDI_UNKNOWN_FIELDS)
1146 TRACE("mask %x contains unknown fields. Using only comctl32 4.0 fields\n", mask);
1147 mask &= HDI_COMCTL32_4_0_FIELDS;
1150 lpItem = &infoPtr->items[nItem];
1151 HEADER_PrepareCallbackItems(infoPtr, nItem, mask);
1153 if (mask & HDI_BITMAP)
1154 phdi->hbm = lpItem->hbm;
1156 if (mask & HDI_FORMAT)
1157 phdi->fmt = lpItem->fmt;
1159 if (mask & HDI_WIDTH)
1160 phdi->cxy = lpItem->cxy;
1162 if (mask & HDI_LPARAM)
1163 phdi->lParam = lpItem->lParam;
1165 if (mask & HDI_IMAGE)
1166 phdi->iImage = lpItem->iImage;
1168 if (mask & HDI_ORDER)
1169 phdi->iOrder = lpItem->iOrder;
1171 if (mask & HDI_TEXT)
1173 if (bUnicode)
1174 Str_GetPtrW (lpItem->pszText, phdi->pszText, phdi->cchTextMax);
1175 else
1176 Str_GetPtrWtoA (lpItem->pszText, (LPSTR)phdi->pszText, phdi->cchTextMax);
1179 HEADER_FreeCallbackItems(lpItem);
1180 return TRUE;
1184 static inline LRESULT
1185 HEADER_GetItemCount (const HEADER_INFO *infoPtr)
1187 return infoPtr->uNumItem;
1191 static LRESULT
1192 HEADER_GetItemRect (const HEADER_INFO *infoPtr, INT iItem, LPRECT lpRect)
1194 if ((iItem < 0) || (iItem >= (INT)infoPtr->uNumItem))
1195 return FALSE;
1197 lpRect->left = infoPtr->items[iItem].rect.left;
1198 lpRect->right = infoPtr->items[iItem].rect.right;
1199 lpRect->top = infoPtr->items[iItem].rect.top;
1200 lpRect->bottom = infoPtr->items[iItem].rect.bottom;
1202 return TRUE;
1206 static LRESULT
1207 HEADER_GetOrderArray(const HEADER_INFO *infoPtr, INT size, LPINT order)
1209 if ((UINT)size <infoPtr->uNumItem)
1210 return FALSE;
1212 memcpy(order, infoPtr->order, infoPtr->uNumItem * sizeof(INT));
1213 return TRUE;
1216 /* Returns index of first duplicate 'value' from [0,to) range,
1217 or -1 if there isn't any */
1218 static INT has_duplicate(INT *array, INT to, INT value)
1220 INT i;
1221 for(i = 0; i < to; i++)
1222 if (array[i] == value) return i;
1223 return -1;
1226 /* returns next available value from [0,max] not to duplicate in [0,to) */
1227 static INT get_nextvalue(INT *array, INT to, INT max)
1229 INT i;
1230 for(i = 0; i < max; i++)
1231 if (has_duplicate(array, to, i) == -1) return i;
1232 return 0;
1235 static LRESULT
1236 HEADER_SetOrderArray(HEADER_INFO *infoPtr, INT size, const INT *order)
1238 HEADER_ITEM *lpItem;
1239 INT i;
1241 if ((UINT)size != infoPtr->uNumItem)
1242 return FALSE;
1244 for (i=0; i<size; i++)
1246 if (order[i] >= size || order[i] < 0)
1247 /* on invalid index get next available */
1248 /* FIXME: if i==0 array item is out of range behaviour is
1249 different, see tests */
1250 infoPtr->order[i] = get_nextvalue(infoPtr->order, i, size);
1251 else
1253 INT j, dup;
1255 infoPtr->order[i] = order[i];
1256 j = i;
1257 /* remove duplicates */
1258 while ((dup = has_duplicate(infoPtr->order, j, order[j])) != -1)
1260 INT next;
1262 next = get_nextvalue(infoPtr->order, j, size);
1263 infoPtr->order[dup] = next;
1264 j--;
1268 /* sync with item data */
1269 for (i=0; i<size; i++)
1271 lpItem = &infoPtr->items[infoPtr->order[i]];
1272 lpItem->iOrder = i;
1274 HEADER_SetItemBounds(infoPtr);
1275 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1276 return TRUE;
1279 static inline LRESULT
1280 HEADER_GetUnicodeFormat (const HEADER_INFO *infoPtr)
1282 return (infoPtr->nNotifyFormat == NFR_UNICODE);
1286 static LRESULT
1287 HEADER_HitTest (const HEADER_INFO *infoPtr, LPHDHITTESTINFO phti)
1289 UINT outside = HHT_NOWHERE | HHT_ABOVE | HHT_BELOW | HHT_TOLEFT | HHT_TORIGHT;
1291 HEADER_InternalHitTest (infoPtr, &phti->pt, &phti->flags, &phti->iItem);
1293 if (phti->flags & outside)
1294 return phti->iItem = -1;
1295 else
1296 return phti->iItem;
1300 static LRESULT
1301 HEADER_InsertItemT (HEADER_INFO *infoPtr, INT nItem, const HDITEMW *phdi, BOOL bUnicode)
1303 HEADER_ITEM *lpItem;
1304 INT iOrder;
1305 UINT i;
1306 UINT copyMask;
1308 if ((phdi == NULL) || (nItem < 0) || (phdi->mask == 0))
1309 return -1;
1311 if (nItem > infoPtr->uNumItem)
1312 nItem = infoPtr->uNumItem;
1314 iOrder = (phdi->mask & HDI_ORDER) ? phdi->iOrder : nItem;
1315 if (iOrder < 0)
1316 iOrder = 0;
1317 else if (infoPtr->uNumItem < iOrder)
1318 iOrder = infoPtr->uNumItem;
1320 infoPtr->uNumItem++;
1321 infoPtr->items = ReAlloc(infoPtr->items, sizeof(HEADER_ITEM) * infoPtr->uNumItem);
1322 infoPtr->order = ReAlloc(infoPtr->order, sizeof(INT) * infoPtr->uNumItem);
1324 /* make space for the new item */
1325 memmove(&infoPtr->items[nItem + 1], &infoPtr->items[nItem],
1326 (infoPtr->uNumItem - nItem - 1) * sizeof(HEADER_ITEM));
1327 memmove(&infoPtr->order[iOrder + 1], &infoPtr->order[iOrder],
1328 (infoPtr->uNumItem - iOrder - 1) * sizeof(INT));
1330 /* update the order array */
1331 infoPtr->order[iOrder] = nItem;
1332 for (i = 0; i < infoPtr->uNumItem; i++)
1334 if (i != iOrder && infoPtr->order[i] >= nItem)
1335 infoPtr->order[i]++;
1336 infoPtr->items[infoPtr->order[i]].iOrder = i;
1339 lpItem = &infoPtr->items[nItem];
1340 ZeroMemory(lpItem, sizeof(HEADER_ITEM));
1341 /* cxy, fmt and lParam are copied even if not in the HDITEM mask */
1342 copyMask = phdi->mask | HDI_WIDTH | HDI_FORMAT | HDI_LPARAM;
1343 HEADER_StoreHDItemInHeader(lpItem, copyMask, phdi, bUnicode);
1344 lpItem->iOrder = iOrder;
1346 /* set automatically some format bits */
1347 if (phdi->mask & HDI_TEXT)
1348 lpItem->fmt |= HDF_STRING;
1349 else
1350 lpItem->fmt &= ~HDF_STRING;
1352 if (lpItem->hbm != NULL)
1353 lpItem->fmt |= HDF_BITMAP;
1354 else
1355 lpItem->fmt &= ~HDF_BITMAP;
1357 if (phdi->mask & HDI_IMAGE)
1358 lpItem->fmt |= HDF_IMAGE;
1360 HEADER_SetItemBounds (infoPtr);
1361 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1363 return nItem;
1367 static LRESULT
1368 HEADER_Layout (HEADER_INFO *infoPtr, LPHDLAYOUT lpLayout)
1370 lpLayout->pwpos->hwnd = infoPtr->hwndSelf;
1371 lpLayout->pwpos->hwndInsertAfter = 0;
1372 lpLayout->pwpos->x = lpLayout->prc->left;
1373 lpLayout->pwpos->y = lpLayout->prc->top;
1374 lpLayout->pwpos->cx = lpLayout->prc->right - lpLayout->prc->left;
1375 if (infoPtr->dwStyle & HDS_HIDDEN)
1376 lpLayout->pwpos->cy = 0;
1377 else {
1378 lpLayout->pwpos->cy = infoPtr->nHeight;
1379 lpLayout->prc->top += infoPtr->nHeight;
1381 lpLayout->pwpos->flags = SWP_NOZORDER;
1383 TRACE("Layout x=%d y=%d cx=%d cy=%d\n",
1384 lpLayout->pwpos->x, lpLayout->pwpos->y,
1385 lpLayout->pwpos->cx, lpLayout->pwpos->cy);
1387 infoPtr->bRectsValid = FALSE;
1389 return TRUE;
1393 static LRESULT
1394 HEADER_SetImageList (HEADER_INFO *infoPtr, HIMAGELIST himl)
1396 HIMAGELIST himlOld;
1398 TRACE("(himl %p)\n", himl);
1399 himlOld = infoPtr->himl;
1400 infoPtr->himl = himl;
1402 /* FIXME: Refresh needed??? */
1404 return (LRESULT)himlOld;
1408 static LRESULT
1409 HEADER_GetBitmapMargin(const HEADER_INFO *infoPtr)
1411 return infoPtr->iMargin;
1414 static LRESULT
1415 HEADER_SetBitmapMargin(HEADER_INFO *infoPtr, INT iMargin)
1417 INT oldMargin = infoPtr->iMargin;
1419 infoPtr->iMargin = iMargin;
1421 return oldMargin;
1424 static LRESULT
1425 HEADER_SetItemT (HEADER_INFO *infoPtr, INT nItem, const HDITEMW *phdi, BOOL bUnicode)
1427 HEADER_ITEM *lpItem;
1428 HDITEMW hdNotify;
1429 void *pvScratch;
1431 if (phdi == NULL)
1432 return FALSE;
1433 if ((nItem < 0) || (nItem >= (INT)infoPtr->uNumItem))
1434 return FALSE;
1436 TRACE("[nItem=%d]\n", nItem);
1438 HEADER_CopyHDItemForNotify(infoPtr, &hdNotify, phdi, bUnicode, &pvScratch);
1439 if (HEADER_SendNotifyWithHDItemT(infoPtr, HDN_ITEMCHANGINGW, nItem, &hdNotify))
1441 Free(pvScratch);
1442 return FALSE;
1445 lpItem = &infoPtr->items[nItem];
1446 HEADER_StoreHDItemInHeader(lpItem, phdi->mask, phdi, bUnicode);
1448 if (phdi->mask & HDI_ORDER)
1449 if (phdi->iOrder >= 0 && phdi->iOrder < infoPtr->uNumItem)
1450 HEADER_ChangeItemOrder(infoPtr, nItem, phdi->iOrder);
1452 HEADER_SendNotifyWithHDItemT(infoPtr, HDN_ITEMCHANGEDW, nItem, &hdNotify);
1454 HEADER_SetItemBounds (infoPtr);
1456 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1458 Free(pvScratch);
1459 return TRUE;
1462 static inline LRESULT
1463 HEADER_SetUnicodeFormat (HEADER_INFO *infoPtr, WPARAM wParam)
1465 BOOL bTemp = (infoPtr->nNotifyFormat == NFR_UNICODE);
1467 infoPtr->nNotifyFormat = ((BOOL)wParam ? NFR_UNICODE : NFR_ANSI);
1469 return bTemp;
1473 static LRESULT
1474 HEADER_Create (HWND hwnd, const CREATESTRUCTW *lpcs)
1476 HEADER_INFO *infoPtr;
1477 TEXTMETRICW tm;
1478 HFONT hOldFont;
1479 HDC hdc;
1481 infoPtr = Alloc (sizeof(HEADER_INFO));
1482 SetWindowLongPtrW (hwnd, 0, (DWORD_PTR)infoPtr);
1484 infoPtr->hwndSelf = hwnd;
1485 infoPtr->hwndNotify = lpcs->hwndParent;
1486 infoPtr->uNumItem = 0;
1487 infoPtr->hFont = 0;
1488 infoPtr->items = 0;
1489 infoPtr->order = 0;
1490 infoPtr->bRectsValid = FALSE;
1491 infoPtr->hcurArrow = LoadCursorW (0, (LPWSTR)IDC_ARROW);
1492 infoPtr->hcurDivider = LoadCursorW (COMCTL32_hModule, MAKEINTRESOURCEW(IDC_DIVIDER));
1493 infoPtr->hcurDivopen = LoadCursorW (COMCTL32_hModule, MAKEINTRESOURCEW(IDC_DIVIDEROPEN));
1494 infoPtr->bPressed = FALSE;
1495 infoPtr->bTracking = FALSE;
1496 infoPtr->dwStyle = lpcs->style;
1497 infoPtr->iMoveItem = 0;
1498 infoPtr->himl = 0;
1499 infoPtr->iHotItem = -1;
1500 infoPtr->iHotDivider = -1;
1501 infoPtr->iMargin = 3*GetSystemMetrics(SM_CXEDGE);
1502 infoPtr->nNotifyFormat =
1503 SendMessageW (infoPtr->hwndNotify, WM_NOTIFYFORMAT, (WPARAM)hwnd, NF_QUERY);
1505 hdc = GetDC (0);
1506 hOldFont = SelectObject (hdc, GetStockObject (SYSTEM_FONT));
1507 GetTextMetricsW (hdc, &tm);
1508 infoPtr->nHeight = tm.tmHeight + VERT_BORDER;
1509 SelectObject (hdc, hOldFont);
1510 ReleaseDC (0, hdc);
1512 OpenThemeData(hwnd, themeClass);
1514 return 0;
1518 static LRESULT
1519 HEADER_Destroy (HEADER_INFO *infoPtr)
1521 HTHEME theme = GetWindowTheme(infoPtr->hwndSelf);
1522 CloseThemeData(theme);
1523 return 0;
1526 static LRESULT
1527 HEADER_NCDestroy (HEADER_INFO *infoPtr)
1529 HEADER_ITEM *lpItem;
1530 INT nItem;
1532 if (infoPtr->items) {
1533 lpItem = infoPtr->items;
1534 for (nItem = 0; nItem < infoPtr->uNumItem; nItem++, lpItem++) {
1535 Free(lpItem->pszText);
1537 Free (infoPtr->items);
1540 Free(infoPtr->order);
1542 if (infoPtr->himl)
1543 ImageList_Destroy (infoPtr->himl);
1545 SetWindowLongPtrW (infoPtr->hwndSelf, 0, 0);
1546 Free (infoPtr);
1548 return 0;
1552 static inline LRESULT
1553 HEADER_GetFont (const HEADER_INFO *infoPtr)
1555 return (LRESULT)infoPtr->hFont;
1559 static BOOL
1560 HEADER_IsDragDistance(const HEADER_INFO *infoPtr, const POINT *pt)
1562 /* Windows allows for a mouse movement before starting the drag. We use the
1563 * SM_CXDOUBLECLICK/SM_CYDOUBLECLICK as that distance.
1565 return (abs(infoPtr->ptLButtonDown.x - pt->x)>GetSystemMetrics(SM_CXDOUBLECLK) ||
1566 abs(infoPtr->ptLButtonDown.y - pt->y)>GetSystemMetrics(SM_CYDOUBLECLK));
1569 static LRESULT
1570 HEADER_LButtonDblClk (const HEADER_INFO *infoPtr, INT x, INT y)
1572 POINT pt;
1573 UINT flags;
1574 INT nItem;
1576 pt.x = x;
1577 pt.y = y;
1578 HEADER_InternalHitTest (infoPtr, &pt, &flags, &nItem);
1580 if ((infoPtr->dwStyle & HDS_BUTTONS) && (flags == HHT_ONHEADER))
1581 HEADER_SendNotifyWithHDItemT(infoPtr, HDN_ITEMDBLCLICKW, nItem, NULL);
1582 else if ((flags == HHT_ONDIVIDER) || (flags == HHT_ONDIVOPEN))
1583 HEADER_SendNotifyWithHDItemT(infoPtr, HDN_DIVIDERDBLCLICKW, nItem, NULL);
1585 return 0;
1589 static LRESULT
1590 HEADER_LButtonDown (HEADER_INFO *infoPtr, INT x, INT y)
1592 POINT pt;
1593 UINT flags;
1594 INT nItem;
1595 HDC hdc;
1597 pt.x = x;
1598 pt.y = y;
1599 HEADER_InternalHitTest (infoPtr, &pt, &flags, &nItem);
1601 if ((infoPtr->dwStyle & HDS_BUTTONS) && (flags == HHT_ONHEADER)) {
1602 SetCapture (infoPtr->hwndSelf);
1603 infoPtr->bCaptured = TRUE;
1604 infoPtr->bPressed = TRUE;
1605 infoPtr->bDragging = FALSE;
1606 infoPtr->iMoveItem = nItem;
1607 infoPtr->ptLButtonDown = pt;
1609 infoPtr->items[nItem].bDown = TRUE;
1611 /* Send WM_CUSTOMDRAW */
1612 hdc = GetDC (infoPtr->hwndSelf);
1613 HEADER_RefreshItem (infoPtr, nItem);
1614 ReleaseDC (infoPtr->hwndSelf, hdc);
1616 TRACE("Pressed item %d!\n", nItem);
1618 else if ((flags == HHT_ONDIVIDER) || (flags == HHT_ONDIVOPEN)) {
1619 INT iCurrWidth = infoPtr->items[nItem].cxy;
1620 if (!HEADER_SendNotifyWithIntFieldT(infoPtr, HDN_BEGINTRACKW, nItem, HDI_WIDTH, iCurrWidth))
1622 SetCapture (infoPtr->hwndSelf);
1623 infoPtr->bCaptured = TRUE;
1624 infoPtr->bTracking = TRUE;
1625 infoPtr->iMoveItem = nItem;
1626 infoPtr->xTrackOffset = infoPtr->items[nItem].rect.right - pt.x;
1628 if (!(infoPtr->dwStyle & HDS_FULLDRAG)) {
1629 infoPtr->xOldTrack = infoPtr->items[nItem].rect.right;
1630 hdc = GetDC (infoPtr->hwndSelf);
1631 HEADER_DrawTrackLine (infoPtr, hdc, infoPtr->xOldTrack);
1632 ReleaseDC (infoPtr->hwndSelf, hdc);
1635 TRACE("Begin tracking item %d!\n", nItem);
1639 return 0;
1643 static LRESULT
1644 HEADER_LButtonUp (HEADER_INFO *infoPtr, INT x, INT y)
1646 POINT pt;
1647 UINT flags;
1648 INT nItem;
1649 HDC hdc;
1651 pt.x = x;
1652 pt.y = y;
1653 HEADER_InternalHitTest (infoPtr, &pt, &flags, &nItem);
1655 if (infoPtr->bPressed) {
1657 infoPtr->items[infoPtr->iMoveItem].bDown = FALSE;
1659 if (infoPtr->bDragging)
1661 HEADER_ITEM *lpItem = &infoPtr->items[infoPtr->iMoveItem];
1662 INT iNewOrder;
1664 ImageList_DragShowNolock(FALSE);
1665 ImageList_EndDrag();
1667 if (infoPtr->iHotDivider == -1)
1668 iNewOrder = -1;
1669 else if (infoPtr->iHotDivider == infoPtr->uNumItem)
1670 iNewOrder = infoPtr->uNumItem-1;
1671 else
1673 iNewOrder = HEADER_IndexToOrder(infoPtr, infoPtr->iHotDivider);
1674 if (iNewOrder > lpItem->iOrder)
1675 iNewOrder--;
1678 if (iNewOrder != -1 &&
1679 !HEADER_SendNotifyWithIntFieldT(infoPtr, HDN_ENDDRAG, infoPtr->iMoveItem, HDI_ORDER, iNewOrder))
1681 HEADER_ChangeItemOrder(infoPtr, infoPtr->iMoveItem, iNewOrder);
1682 infoPtr->bRectsValid = FALSE;
1683 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1685 else
1686 InvalidateRect(infoPtr->hwndSelf, &infoPtr->items[infoPtr->iMoveItem].rect, FALSE);
1688 infoPtr->bDragging = FALSE;
1689 HEADER_SetHotDivider(infoPtr, FALSE, -1);
1691 else
1693 hdc = GetDC (infoPtr->hwndSelf);
1694 HEADER_RefreshItem (infoPtr, infoPtr->iMoveItem);
1695 ReleaseDC (infoPtr->hwndSelf, hdc);
1697 if (!(infoPtr->dwStyle & HDS_DRAGDROP) || !HEADER_IsDragDistance(infoPtr, &pt))
1698 HEADER_SendNotifyWithHDItemT(infoPtr, HDN_ITEMCLICKW, infoPtr->iMoveItem, NULL);
1701 TRACE("Released item %d!\n", infoPtr->iMoveItem);
1702 infoPtr->bPressed = FALSE;
1704 else if (infoPtr->bTracking) {
1705 INT iNewWidth = pt.x - infoPtr->items[infoPtr->iMoveItem].rect.left + infoPtr->xTrackOffset;
1706 if (iNewWidth < 0)
1707 iNewWidth = 0;
1708 TRACE("End tracking item %d!\n", infoPtr->iMoveItem);
1709 infoPtr->bTracking = FALSE;
1711 HEADER_SendNotifyWithIntFieldT(infoPtr, HDN_ENDTRACKW, infoPtr->iMoveItem, HDI_WIDTH, iNewWidth);
1713 if (!(infoPtr->dwStyle & HDS_FULLDRAG)) {
1714 hdc = GetDC (infoPtr->hwndSelf);
1715 HEADER_DrawTrackLine (infoPtr, hdc, infoPtr->xOldTrack);
1716 ReleaseDC (infoPtr->hwndSelf, hdc);
1719 if (!HEADER_SendNotifyWithIntFieldT(infoPtr, HDN_ITEMCHANGINGW, infoPtr->iMoveItem, HDI_WIDTH, iNewWidth))
1721 infoPtr->items[infoPtr->iMoveItem].cxy = iNewWidth;
1722 HEADER_SendNotifyWithIntFieldT(infoPtr, HDN_ITEMCHANGEDW, infoPtr->iMoveItem, HDI_WIDTH, iNewWidth);
1725 HEADER_SetItemBounds (infoPtr);
1726 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1729 if (infoPtr->bCaptured) {
1730 infoPtr->bCaptured = FALSE;
1731 ReleaseCapture ();
1732 HEADER_SendSimpleNotify (infoPtr, NM_RELEASEDCAPTURE);
1735 return 0;
1739 static LRESULT
1740 HEADER_NotifyFormat (HEADER_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
1742 switch (lParam)
1744 case NF_QUERY:
1745 return infoPtr->nNotifyFormat;
1747 case NF_REQUERY:
1748 infoPtr->nNotifyFormat =
1749 SendMessageW ((HWND)wParam, WM_NOTIFYFORMAT,
1750 (WPARAM)infoPtr->hwndSelf, NF_QUERY);
1751 return infoPtr->nNotifyFormat;
1754 return 0;
1757 static LRESULT
1758 HEADER_MouseLeave (HEADER_INFO *infoPtr)
1760 /* Reset hot-tracked item when mouse leaves control. */
1761 INT oldHotItem = infoPtr->iHotItem;
1762 HDC hdc = GetDC (infoPtr->hwndSelf);
1764 infoPtr->iHotItem = -1;
1765 if (oldHotItem != -1) HEADER_RefreshItem (infoPtr, oldHotItem);
1766 ReleaseDC (infoPtr->hwndSelf, hdc);
1768 return 0;
1772 static LRESULT
1773 HEADER_MouseMove (HEADER_INFO *infoPtr, LPARAM lParam)
1775 POINT pt;
1776 UINT flags;
1777 INT nItem, nWidth;
1778 HDC hdc;
1779 /* With theming, hottracking is always enabled */
1780 BOOL hotTrackEnabled =
1781 ((infoPtr->dwStyle & HDS_BUTTONS) && (infoPtr->dwStyle & HDS_HOTTRACK))
1782 || (GetWindowTheme (infoPtr->hwndSelf) != NULL);
1783 INT oldHotItem = infoPtr->iHotItem;
1785 pt.x = (INT)(SHORT)LOWORD(lParam);
1786 pt.y = (INT)(SHORT)HIWORD(lParam);
1787 HEADER_InternalHitTest (infoPtr, &pt, &flags, &nItem);
1789 if (hotTrackEnabled) {
1790 if (flags & (HHT_ONHEADER | HHT_ONDIVIDER | HHT_ONDIVOPEN))
1791 infoPtr->iHotItem = nItem;
1792 else
1793 infoPtr->iHotItem = -1;
1796 if (infoPtr->bCaptured) {
1797 /* check if we should drag the header */
1798 if (infoPtr->bPressed && !infoPtr->bDragging && (infoPtr->dwStyle & HDS_DRAGDROP)
1799 && HEADER_IsDragDistance(infoPtr, &pt))
1801 if (!HEADER_SendNotifyWithHDItemT(infoPtr, HDN_BEGINDRAG, infoPtr->iMoveItem, NULL))
1803 HIMAGELIST hDragItem = (HIMAGELIST)HEADER_CreateDragImage(infoPtr, infoPtr->iMoveItem);
1804 if (hDragItem != NULL)
1806 HEADER_ITEM *lpItem = &infoPtr->items[infoPtr->iMoveItem];
1807 TRACE("Starting item drag\n");
1808 ImageList_BeginDrag(hDragItem, 0, pt.x - lpItem->rect.left, 0);
1809 ImageList_DragShowNolock(TRUE);
1810 ImageList_Destroy(hDragItem);
1811 infoPtr->bDragging = TRUE;
1816 if (infoPtr->bDragging)
1818 POINT drag;
1819 drag.x = pt.x;
1820 drag.y = 0;
1821 ClientToScreen(infoPtr->hwndSelf, &drag);
1822 ImageList_DragMove(drag.x, drag.y);
1823 HEADER_SetHotDivider(infoPtr, TRUE, lParam);
1826 if (infoPtr->bPressed && !infoPtr->bDragging) {
1827 BOOL oldState = infoPtr->items[infoPtr->iMoveItem].bDown;
1828 if ((nItem == infoPtr->iMoveItem) && (flags == HHT_ONHEADER))
1829 infoPtr->items[infoPtr->iMoveItem].bDown = TRUE;
1830 else
1831 infoPtr->items[infoPtr->iMoveItem].bDown = FALSE;
1832 if (oldState != infoPtr->items[infoPtr->iMoveItem].bDown) {
1833 hdc = GetDC (infoPtr->hwndSelf);
1834 HEADER_RefreshItem (infoPtr, infoPtr->iMoveItem);
1835 ReleaseDC (infoPtr->hwndSelf, hdc);
1838 TRACE("Moving pressed item %d!\n", infoPtr->iMoveItem);
1840 else if (infoPtr->bTracking) {
1841 if (infoPtr->dwStyle & HDS_FULLDRAG) {
1842 HEADER_ITEM *lpItem = &infoPtr->items[infoPtr->iMoveItem];
1843 nWidth = pt.x - lpItem->rect.left + infoPtr->xTrackOffset;
1844 if (!HEADER_SendNotifyWithIntFieldT(infoPtr, HDN_ITEMCHANGINGW, infoPtr->iMoveItem, HDI_WIDTH, nWidth))
1846 INT nOldWidth = lpItem->rect.right - lpItem->rect.left;
1847 RECT rcClient;
1848 RECT rcScroll;
1850 if (nWidth < 0) nWidth = 0;
1851 infoPtr->items[infoPtr->iMoveItem].cxy = nWidth;
1852 HEADER_SetItemBounds(infoPtr);
1854 GetClientRect(infoPtr->hwndSelf, &rcClient);
1855 rcScroll = rcClient;
1856 rcScroll.left = lpItem->rect.left + nOldWidth;
1857 ScrollWindowEx(infoPtr->hwndSelf, nWidth - nOldWidth, 0, &rcScroll, &rcClient, NULL, NULL, 0);
1858 InvalidateRect(infoPtr->hwndSelf, &lpItem->rect, FALSE);
1859 UpdateWindow(infoPtr->hwndSelf);
1861 HEADER_SendNotifyWithIntFieldT(infoPtr, HDN_ITEMCHANGEDW, infoPtr->iMoveItem, HDI_WIDTH, nWidth);
1864 else {
1865 INT iTrackWidth;
1866 hdc = GetDC (infoPtr->hwndSelf);
1867 HEADER_DrawTrackLine (infoPtr, hdc, infoPtr->xOldTrack);
1868 infoPtr->xOldTrack = pt.x + infoPtr->xTrackOffset;
1869 if (infoPtr->xOldTrack < infoPtr->items[infoPtr->iMoveItem].rect.left)
1870 infoPtr->xOldTrack = infoPtr->items[infoPtr->iMoveItem].rect.left;
1871 HEADER_DrawTrackLine (infoPtr, hdc, infoPtr->xOldTrack);
1872 ReleaseDC (infoPtr->hwndSelf, hdc);
1873 iTrackWidth = infoPtr->xOldTrack - infoPtr->items[infoPtr->iMoveItem].rect.left;
1874 /* FIXME: should stop tracking if HDN_TRACK returns TRUE */
1875 HEADER_SendNotifyWithIntFieldT(infoPtr, HDN_TRACKW, infoPtr->iMoveItem, HDI_WIDTH, iTrackWidth);
1878 TRACE("Tracking item %d!\n", infoPtr->iMoveItem);
1882 if (hotTrackEnabled) {
1883 TRACKMOUSEEVENT tme;
1884 if (oldHotItem != infoPtr->iHotItem && !infoPtr->bDragging) {
1885 hdc = GetDC (infoPtr->hwndSelf);
1886 if (oldHotItem != -1) HEADER_RefreshItem (infoPtr, oldHotItem);
1887 if (infoPtr->iHotItem != -1) HEADER_RefreshItem (infoPtr, infoPtr->iHotItem);
1888 ReleaseDC (infoPtr->hwndSelf, hdc);
1890 tme.cbSize = sizeof( tme );
1891 tme.dwFlags = TME_LEAVE;
1892 tme.hwndTrack = infoPtr->hwndSelf;
1893 TrackMouseEvent( &tme );
1896 return 0;
1900 static LRESULT
1901 HEADER_Paint (HEADER_INFO *infoPtr, HDC hdcParam)
1903 HDC hdc;
1904 PAINTSTRUCT ps;
1906 hdc = hdcParam==0 ? BeginPaint (infoPtr->hwndSelf, &ps) : hdcParam;
1907 HEADER_Refresh (infoPtr, hdc);
1908 if(!hdcParam)
1909 EndPaint (infoPtr->hwndSelf, &ps);
1910 return 0;
1914 static LRESULT
1915 HEADER_RButtonUp (HEADER_INFO *infoPtr, INT x, INT y)
1917 BOOL bRet;
1918 POINT pt;
1920 pt.x = x;
1921 pt.y = y;
1923 /* Send a Notify message */
1924 bRet = HEADER_SendSimpleNotify (infoPtr, NM_RCLICK);
1926 /* Change to screen coordinate for WM_CONTEXTMENU */
1927 ClientToScreen(infoPtr->hwndSelf, &pt);
1929 /* Send a WM_CONTEXTMENU message in response to the RBUTTONUP */
1930 SendMessageW( infoPtr->hwndSelf, WM_CONTEXTMENU, (WPARAM) infoPtr->hwndSelf, MAKELPARAM(pt.x, pt.y));
1932 return bRet;
1936 static LRESULT
1937 HEADER_SetCursor (HEADER_INFO *infoPtr, LPARAM lParam)
1939 POINT pt;
1940 UINT flags;
1941 INT nItem;
1943 TRACE("code=0x%X id=0x%X\n", LOWORD(lParam), HIWORD(lParam));
1945 GetCursorPos (&pt);
1946 ScreenToClient (infoPtr->hwndSelf, &pt);
1948 HEADER_InternalHitTest (infoPtr, &pt, &flags, &nItem);
1950 if (flags == HHT_ONDIVIDER)
1951 SetCursor (infoPtr->hcurDivider);
1952 else if (flags == HHT_ONDIVOPEN)
1953 SetCursor (infoPtr->hcurDivopen);
1954 else
1955 SetCursor (infoPtr->hcurArrow);
1957 return 0;
1961 static LRESULT
1962 HEADER_SetFont (HEADER_INFO *infoPtr, HFONT hFont, WORD Redraw)
1964 TEXTMETRICW tm;
1965 HFONT hOldFont;
1966 HDC hdc;
1968 infoPtr->hFont = hFont;
1970 hdc = GetDC (0);
1971 hOldFont = SelectObject (hdc, infoPtr->hFont ? infoPtr->hFont : GetStockObject (SYSTEM_FONT));
1972 GetTextMetricsW (hdc, &tm);
1973 infoPtr->nHeight = tm.tmHeight + VERT_BORDER;
1974 SelectObject (hdc, hOldFont);
1975 ReleaseDC (0, hdc);
1977 infoPtr->bRectsValid = FALSE;
1979 if (Redraw) {
1980 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1983 return 0;
1986 static LRESULT HEADER_SetRedraw(HEADER_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
1988 /* ignoring the InvalidateRect calls is handled by user32. But some apps expect
1989 * that we invalidate the header and this has to be done manually */
1990 LRESULT ret;
1992 ret = DefWindowProcW(infoPtr->hwndSelf, WM_SETREDRAW, wParam, lParam);
1993 if (wParam)
1994 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1995 return ret;
1998 static INT HEADER_StyleChanged(HEADER_INFO *infoPtr, WPARAM wStyleType,
1999 const STYLESTRUCT *lpss)
2001 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
2002 wStyleType, lpss->styleOld, lpss->styleNew);
2004 if (wStyleType != GWL_STYLE) return 0;
2006 infoPtr->dwStyle = lpss->styleNew;
2008 return 0;
2011 /* Update the theme handle after a theme change */
2012 static LRESULT HEADER_ThemeChanged(const HEADER_INFO *infoPtr)
2014 HTHEME theme = GetWindowTheme(infoPtr->hwndSelf);
2015 CloseThemeData(theme);
2016 OpenThemeData(infoPtr->hwndSelf, themeClass);
2017 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2018 return 0;
2022 static LRESULT WINAPI
2023 HEADER_WindowProc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
2025 HEADER_INFO *infoPtr = (HEADER_INFO *)GetWindowLongPtrW(hwnd, 0);
2027 TRACE("hwnd=%p msg=%x wparam=%lx lParam=%lx\n", hwnd, msg, wParam, lParam);
2028 if (!infoPtr && (msg != WM_CREATE))
2029 return DefWindowProcW (hwnd, msg, wParam, lParam);
2030 switch (msg) {
2031 /* case HDM_CLEARFILTER: */
2033 case HDM_CREATEDRAGIMAGE:
2034 return HEADER_CreateDragImage (infoPtr, (INT)wParam);
2036 case HDM_DELETEITEM:
2037 return HEADER_DeleteItem (infoPtr, (INT)wParam);
2039 /* case HDM_EDITFILTER: */
2041 case HDM_GETBITMAPMARGIN:
2042 return HEADER_GetBitmapMargin(infoPtr);
2044 case HDM_GETIMAGELIST:
2045 return HEADER_GetImageList (infoPtr);
2047 case HDM_GETITEMA:
2048 case HDM_GETITEMW:
2049 return HEADER_GetItemT (infoPtr, (INT)wParam, (LPHDITEMW)lParam, msg == HDM_GETITEMW);
2051 case HDM_GETITEMCOUNT:
2052 return HEADER_GetItemCount (infoPtr);
2054 case HDM_GETITEMRECT:
2055 return HEADER_GetItemRect (infoPtr, (INT)wParam, (LPRECT)lParam);
2057 case HDM_GETORDERARRAY:
2058 return HEADER_GetOrderArray(infoPtr, (INT)wParam, (LPINT)lParam);
2060 case HDM_GETUNICODEFORMAT:
2061 return HEADER_GetUnicodeFormat (infoPtr);
2063 case HDM_HITTEST:
2064 return HEADER_HitTest (infoPtr, (LPHDHITTESTINFO)lParam);
2066 case HDM_INSERTITEMA:
2067 case HDM_INSERTITEMW:
2068 return HEADER_InsertItemT (infoPtr, (INT)wParam, (LPHDITEMW)lParam, msg == HDM_INSERTITEMW);
2070 case HDM_LAYOUT:
2071 return HEADER_Layout (infoPtr, (LPHDLAYOUT)lParam);
2073 case HDM_ORDERTOINDEX:
2074 return HEADER_OrderToIndex(infoPtr, (INT)wParam);
2076 case HDM_SETBITMAPMARGIN:
2077 return HEADER_SetBitmapMargin(infoPtr, (INT)wParam);
2079 /* case HDM_SETFILTERCHANGETIMEOUT: */
2081 case HDM_SETHOTDIVIDER:
2082 return HEADER_SetHotDivider(infoPtr, wParam, lParam);
2084 case HDM_SETIMAGELIST:
2085 return HEADER_SetImageList (infoPtr, (HIMAGELIST)lParam);
2087 case HDM_SETITEMA:
2088 case HDM_SETITEMW:
2089 return HEADER_SetItemT (infoPtr, (INT)wParam, (LPHDITEMW)lParam, msg == HDM_SETITEMW);
2091 case HDM_SETORDERARRAY:
2092 return HEADER_SetOrderArray(infoPtr, (INT)wParam, (LPINT)lParam);
2094 case HDM_SETUNICODEFORMAT:
2095 return HEADER_SetUnicodeFormat (infoPtr, wParam);
2097 case WM_CREATE:
2098 return HEADER_Create (hwnd, (LPCREATESTRUCTW)lParam);
2100 case WM_DESTROY:
2101 return HEADER_Destroy (infoPtr);
2103 case WM_NCDESTROY:
2104 return HEADER_NCDestroy (infoPtr);
2106 case WM_ERASEBKGND:
2107 return 1;
2109 case WM_GETDLGCODE:
2110 return DLGC_WANTTAB | DLGC_WANTARROWS;
2112 case WM_GETFONT:
2113 return HEADER_GetFont (infoPtr);
2115 case WM_LBUTTONDBLCLK:
2116 return HEADER_LButtonDblClk (infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
2118 case WM_LBUTTONDOWN:
2119 return HEADER_LButtonDown (infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
2121 case WM_LBUTTONUP:
2122 return HEADER_LButtonUp (infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
2124 case WM_MOUSELEAVE:
2125 return HEADER_MouseLeave (infoPtr);
2127 case WM_MOUSEMOVE:
2128 return HEADER_MouseMove (infoPtr, lParam);
2130 case WM_NOTIFYFORMAT:
2131 return HEADER_NotifyFormat (infoPtr, wParam, lParam);
2133 case WM_SIZE:
2134 return HEADER_Size (infoPtr);
2136 case WM_THEMECHANGED:
2137 return HEADER_ThemeChanged (infoPtr);
2139 case WM_PRINTCLIENT:
2140 case WM_PAINT:
2141 return HEADER_Paint (infoPtr, (HDC)wParam);
2143 case WM_RBUTTONUP:
2144 return HEADER_RButtonUp (infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
2146 case WM_SETCURSOR:
2147 return HEADER_SetCursor (infoPtr, lParam);
2149 case WM_SETFONT:
2150 return HEADER_SetFont (infoPtr, (HFONT)wParam, (WORD)lParam);
2152 case WM_SETREDRAW:
2153 return HEADER_SetRedraw(infoPtr, wParam, lParam);
2155 case WM_STYLECHANGED:
2156 return HEADER_StyleChanged(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
2158 case WM_SYSCOLORCHANGE:
2159 COMCTL32_RefreshSysColors();
2160 return 0;
2162 default:
2163 if ((msg >= WM_USER) && (msg < WM_APP) && !COMCTL32_IsReflectedMessage(msg))
2164 ERR("unknown msg %04x wp=%04lx lp=%08lx\n",
2165 msg, wParam, lParam );
2166 return DefWindowProcW(hwnd, msg, wParam, lParam);
2171 VOID
2172 HEADER_Register (void)
2174 WNDCLASSW wndClass;
2176 ZeroMemory (&wndClass, sizeof(WNDCLASSW));
2177 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS;
2178 wndClass.lpfnWndProc = HEADER_WindowProc;
2179 wndClass.cbClsExtra = 0;
2180 wndClass.cbWndExtra = sizeof(HEADER_INFO *);
2181 wndClass.hCursor = LoadCursorW (0, (LPWSTR)IDC_ARROW);
2182 wndClass.lpszClassName = WC_HEADERW;
2184 RegisterClassW (&wndClass);
2188 VOID
2189 HEADER_Unregister (void)
2191 UnregisterClassW (WC_HEADERW, NULL);