winedbg: Add partial ARM disassembler.
[wine/multimedia.git] / dlls / comctl32 / header.c
blob0a4042362ab5073ec446cd1e58dca94cf2266ec4
1 /*
2 * Header control
4 * Copyright 1998 Eric Kohl
5 * Copyright 2000 Eric Kohl for CodeWeavers
6 * Copyright 2003 Maxime Bellenge
7 * Copyright 2006 Mikolaj Zalewski
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 * TODO:
24 * - Imagelist support (completed?)
25 * - Hottrack support (completed?)
26 * - Filters support (HDS_FILTER, HDI_FILTER, HDM_*FILTER*, HDN_*FILTER*)
27 * - New Windows Vista features
30 #include <stdarg.h>
31 #include <stdlib.h>
32 #include <string.h>
34 #include "windef.h"
35 #include "winbase.h"
36 #include "wine/unicode.h"
37 #include "wingdi.h"
38 #include "winuser.h"
39 #include "winnls.h"
40 #include "commctrl.h"
41 #include "comctl32.h"
42 #include "vssym32.h"
43 #include "uxtheme.h"
44 #include "wine/debug.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(header);
48 typedef struct
50 INT cxy;
51 HBITMAP hbm;
52 LPWSTR pszText;
53 INT fmt;
54 LPARAM lParam;
55 INT iImage;
56 INT iOrder; /* see documentation of HD_ITEM */
58 BOOL bDown; /* is item pressed? (used for drawing) */
59 RECT rect; /* bounding rectangle of the item */
60 DWORD callbackMask; /* HDI_* flags for items that are callback */
61 } HEADER_ITEM;
64 typedef struct
66 HWND hwndSelf; /* Control window */
67 HWND hwndNotify; /* Owner window to send notifications to */
68 INT nNotifyFormat; /* format used for WM_NOTIFY messages */
69 UINT uNumItem; /* number of items (columns) */
70 INT nHeight; /* height of the header (pixels) */
71 HFONT hFont; /* handle to the current font */
72 HCURSOR hcurArrow; /* handle to the arrow cursor */
73 HCURSOR hcurDivider; /* handle to a cursor (used over dividers) <-|-> */
74 HCURSOR hcurDivopen; /* handle to a cursor (used over dividers) <-||-> */
75 BOOL bCaptured; /* Is the mouse captured? */
76 BOOL bPressed; /* Is a header item pressed (down)? */
77 BOOL bDragging; /* Are we dragging an item? */
78 BOOL bTracking; /* Is in tracking mode? */
79 POINT ptLButtonDown; /* The point where the left button was pressed */
80 DWORD dwStyle; /* the cached window GWL_STYLE */
81 INT iMoveItem; /* index of tracked item. (Tracking mode) */
82 INT xTrackOffset; /* distance between the right side of the tracked item and the cursor */
83 INT xOldTrack; /* track offset (see above) after the last WM_MOUSEMOVE */
84 INT iHotItem; /* index of hot item (cursor is over this item) */
85 INT iHotDivider; /* index of the hot divider (used while dragging an item or by HDM_SETHOTDIVIDER) */
86 INT iMargin; /* width of the margin that surrounds a bitmap */
88 HIMAGELIST himl; /* handle to an image list (may be 0) */
89 HEADER_ITEM *items; /* pointer to array of HEADER_ITEM's */
90 INT *order; /* array of item IDs indexed by order */
91 BOOL bRectsValid; /* validity flag for bounding rectangles */
92 } HEADER_INFO;
95 #define VERT_BORDER 4
96 #define DIVIDER_WIDTH 10
97 #define HOT_DIVIDER_WIDTH 2
98 #define MAX_HEADER_TEXT_LEN 260
99 #define HDN_UNICODE_OFFSET 20
100 #define HDN_FIRST_UNICODE (HDN_FIRST-HDN_UNICODE_OFFSET)
102 #define HDI_SUPPORTED_FIELDS (HDI_WIDTH|HDI_TEXT|HDI_FORMAT|HDI_LPARAM|HDI_BITMAP|HDI_IMAGE|HDI_ORDER)
103 #define HDI_UNSUPPORTED_FIELDS (HDI_FILTER)
104 #define HDI_UNKNOWN_FIELDS (~(HDI_SUPPORTED_FIELDS|HDI_UNSUPPORTED_FIELDS|HDI_DI_SETITEM))
105 #define HDI_COMCTL32_4_0_FIELDS (HDI_WIDTH|HDI_TEXT|HDI_FORMAT|HDI_LPARAM|HDI_BITMAP)
108 static BOOL HEADER_PrepareCallbackItems(const HEADER_INFO *infoPtr, INT iItem, INT reqMask);
109 static void HEADER_FreeCallbackItems(HEADER_ITEM *lpItem);
110 static LRESULT HEADER_SendNotify(const HEADER_INFO *infoPtr, UINT code, NMHDR *hdr);
111 static LRESULT HEADER_SendCtrlCustomDraw(const HEADER_INFO *infoPtr, 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 (const HEADER_INFO *infoPtr, INT iItem)
168 HEADER_ITEM *lpItem = &infoPtr->items[iItem];
169 return lpItem->iOrder;
173 static INT
174 HEADER_OrderToIndex(const HEADER_INFO *infoPtr, INT iorder)
176 if ((iorder <0) || iorder >= infoPtr->uNumItem)
177 return iorder;
178 return infoPtr->order[iorder];
181 static void
182 HEADER_ChangeItemOrder(const HEADER_INFO *infoPtr, INT iItem, INT iNewOrder)
184 HEADER_ITEM *lpItem = &infoPtr->items[iItem];
185 INT i, nMin, nMax;
187 TRACE("%d: %d->%d\n", iItem, lpItem->iOrder, iNewOrder);
188 if (lpItem->iOrder < iNewOrder)
190 memmove(&infoPtr->order[lpItem->iOrder],
191 &infoPtr->order[lpItem->iOrder + 1],
192 (iNewOrder - lpItem->iOrder) * sizeof(INT));
194 if (iNewOrder < lpItem->iOrder)
196 memmove(&infoPtr->order[iNewOrder + 1],
197 &infoPtr->order[iNewOrder],
198 (lpItem->iOrder - iNewOrder) * sizeof(INT));
200 infoPtr->order[iNewOrder] = iItem;
201 nMin = min(lpItem->iOrder, iNewOrder);
202 nMax = max(lpItem->iOrder, iNewOrder);
203 for (i = nMin; i <= nMax; i++)
204 infoPtr->items[infoPtr->order[i]].iOrder = i;
207 /* Note: if iItem is the last item then this function returns infoPtr->uNumItem */
208 static INT
209 HEADER_NextItem(const HEADER_INFO *infoPtr, INT iItem)
211 return HEADER_OrderToIndex(infoPtr, HEADER_IndexToOrder(infoPtr, iItem)+1);
214 static INT
215 HEADER_PrevItem(const HEADER_INFO *infoPtr, INT iItem)
217 return HEADER_OrderToIndex(infoPtr, HEADER_IndexToOrder(infoPtr, iItem)-1);
220 /* TRUE when item is not resizable with dividers,
221 note that valid index should be supplied */
222 static inline BOOL
223 HEADER_IsItemFixed(const HEADER_INFO *infoPtr, INT iItem)
225 return (infoPtr->dwStyle & HDS_NOSIZING) || (infoPtr->items[iItem].fmt & HDF_FIXEDWIDTH);
228 static void
229 HEADER_SetItemBounds (HEADER_INFO *infoPtr)
231 HEADER_ITEM *phdi;
232 RECT rect;
233 unsigned int i;
234 int x;
236 infoPtr->bRectsValid = TRUE;
238 if (infoPtr->uNumItem == 0)
239 return;
241 GetClientRect (infoPtr->hwndSelf, &rect);
243 x = rect.left;
244 for (i = 0; i < infoPtr->uNumItem; i++) {
245 phdi = &infoPtr->items[HEADER_OrderToIndex(infoPtr,i)];
246 phdi->rect.top = rect.top;
247 phdi->rect.bottom = rect.bottom;
248 phdi->rect.left = x;
249 phdi->rect.right = phdi->rect.left + ((phdi->cxy>0)?phdi->cxy:0);
250 x = phdi->rect.right;
254 static LRESULT
255 HEADER_Size (HEADER_INFO *infoPtr)
257 HEADER_SetItemBounds(infoPtr);
258 return 0;
261 static void HEADER_GetHotDividerRect(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(infoPtr, 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(infoPtr->hwndSelf, &clientRect);
287 *r = clientRect;
288 r->right = r->left + HOT_DIVIDER_WIDTH/2;
293 static INT
294 HEADER_DrawItem (HEADER_INFO *infoPtr, HDC hdc, INT iItem, BOOL bHotTrack, LRESULT lCDFlags)
296 HEADER_ITEM *phdi = &infoPtr->items[iItem];
297 RECT r;
298 INT oldBkMode;
299 HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
300 NMCUSTOMDRAW nmcd;
302 TRACE("DrawItem(iItem %d bHotTrack %d unicode flag %d)\n", iItem, bHotTrack, (infoPtr->nNotifyFormat == NFR_UNICODE));
304 r = phdi->rect;
305 if (r.right - r.left == 0)
306 return phdi->rect.right;
308 /* Set the colors before sending NM_CUSTOMDRAW so that it can change them */
309 SetTextColor(hdc, (bHotTrack && !theme) ? COLOR_HIGHLIGHT : COLOR_BTNTEXT);
310 SetBkColor(hdc, comctl32_color.clr3dFace);
312 if (lCDFlags & CDRF_NOTIFYITEMDRAW && !(phdi->fmt & HDF_OWNERDRAW))
314 LRESULT lCDItemFlags;
316 nmcd.dwDrawStage = CDDS_PREPAINT | CDDS_ITEM;
317 nmcd.hdc = hdc;
318 nmcd.dwItemSpec = iItem;
319 nmcd.rc = r;
320 nmcd.uItemState = phdi->bDown ? CDIS_SELECTED : 0;
321 nmcd.lItemlParam = phdi->lParam;
323 lCDItemFlags = HEADER_SendNotify(infoPtr, NM_CUSTOMDRAW, (NMHDR *)&nmcd);
324 if (lCDItemFlags & CDRF_SKIPDEFAULT)
325 return phdi->rect.right;
328 if (theme != NULL) {
329 int state = (phdi->bDown) ? HIS_PRESSED :
330 (bHotTrack ? HIS_HOT : HIS_NORMAL);
331 DrawThemeBackground (theme, hdc, HP_HEADERITEM, state,
332 &r, NULL);
333 GetThemeBackgroundContentRect (theme, hdc, HP_HEADERITEM, state,
334 &r, &r);
336 else {
337 HBRUSH hbr;
339 if (!(infoPtr->dwStyle & HDS_FLAT))
341 if (infoPtr->dwStyle & HDS_BUTTONS) {
342 if (phdi->bDown) {
343 DrawEdge (hdc, &r, BDR_RAISEDOUTER,
344 BF_RECT | BF_FLAT | BF_MIDDLE | BF_ADJUST);
346 else
347 DrawEdge (hdc, &r, EDGE_RAISED,
348 BF_RECT | BF_SOFT | BF_MIDDLE | BF_ADJUST);
350 else
351 DrawEdge (hdc, &r, EDGE_ETCHED, BF_BOTTOM | BF_RIGHT | BF_ADJUST);
354 hbr = CreateSolidBrush(GetBkColor(hdc));
355 FillRect(hdc, &r, hbr);
356 DeleteObject(hbr);
358 if (phdi->bDown) {
359 r.left += 2;
360 r.top += 2;
363 if (phdi->fmt & HDF_OWNERDRAW) {
364 DRAWITEMSTRUCT dis;
366 dis.CtlType = ODT_HEADER;
367 dis.CtlID = GetWindowLongPtrW (infoPtr->hwndSelf, GWLP_ID);
368 dis.itemID = iItem;
369 dis.itemAction = ODA_DRAWENTIRE;
370 dis.itemState = phdi->bDown ? ODS_SELECTED : 0;
371 dis.hwndItem = infoPtr->hwndSelf;
372 dis.hDC = hdc;
373 dis.rcItem = phdi->rect;
374 dis.itemData = phdi->lParam;
375 oldBkMode = SetBkMode(hdc, TRANSPARENT);
376 SendMessageW (infoPtr->hwndNotify, WM_DRAWITEM, dis.CtlID, (LPARAM)&dis);
377 if (oldBkMode != TRANSPARENT)
378 SetBkMode(hdc, oldBkMode);
380 else {
381 UINT rw, rh, /* width and height of r */
382 *x = NULL, *w = NULL; /* x and width of the pic (bmp or img) which is part of cnt */
383 /* cnt,txt,img,bmp */
384 UINT cx, tx, ix, bx,
385 cw, tw, iw, bw;
386 INT img_cx, img_cy;
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) && ImageList_GetIconSize( infoPtr->himl, &img_cx, &img_cy )) {
404 iw = img_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 - img_cy) / 2,
478 img_cx, img_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(const 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(const 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 if (TRACE_ON(header))
1246 TRACE("count=%d, order array={", size);
1247 for (i = 0; i < size; i++)
1248 TRACE("%d%c", order[i], i != size-1 ? ',' : '}');
1249 TRACE("\n");
1252 for (i=0; i<size; i++)
1254 if (order[i] >= size || order[i] < 0)
1255 /* on invalid index get next available */
1256 /* FIXME: if i==0 array item is out of range behaviour is
1257 different, see tests */
1258 infoPtr->order[i] = get_nextvalue(infoPtr->order, i, size);
1259 else
1261 INT j, dup;
1263 infoPtr->order[i] = order[i];
1264 j = i;
1265 /* remove duplicates */
1266 while ((dup = has_duplicate(infoPtr->order, j, order[j])) != -1)
1268 INT next;
1270 next = get_nextvalue(infoPtr->order, j, size);
1271 infoPtr->order[dup] = next;
1272 j--;
1276 /* sync with item data */
1277 for (i=0; i<size; i++)
1279 lpItem = &infoPtr->items[infoPtr->order[i]];
1280 lpItem->iOrder = i;
1282 HEADER_SetItemBounds(infoPtr);
1283 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1284 return TRUE;
1287 static inline LRESULT
1288 HEADER_GetUnicodeFormat (const HEADER_INFO *infoPtr)
1290 return (infoPtr->nNotifyFormat == NFR_UNICODE);
1294 static LRESULT
1295 HEADER_HitTest (const HEADER_INFO *infoPtr, LPHDHITTESTINFO phti)
1297 UINT outside = HHT_NOWHERE | HHT_ABOVE | HHT_BELOW | HHT_TOLEFT | HHT_TORIGHT;
1299 HEADER_InternalHitTest (infoPtr, &phti->pt, &phti->flags, &phti->iItem);
1301 if (phti->flags & outside)
1302 return phti->iItem = -1;
1303 else
1304 return phti->iItem;
1308 static LRESULT
1309 HEADER_InsertItemT (HEADER_INFO *infoPtr, INT nItem, const HDITEMW *phdi, BOOL bUnicode)
1311 HEADER_ITEM *lpItem;
1312 INT iOrder;
1313 UINT i;
1314 UINT copyMask;
1316 if ((phdi == NULL) || (nItem < 0) || (phdi->mask == 0))
1317 return -1;
1319 if (nItem > infoPtr->uNumItem)
1320 nItem = infoPtr->uNumItem;
1322 iOrder = (phdi->mask & HDI_ORDER) ? phdi->iOrder : nItem;
1323 if (iOrder < 0)
1324 iOrder = 0;
1325 else if (infoPtr->uNumItem < iOrder)
1326 iOrder = infoPtr->uNumItem;
1328 infoPtr->uNumItem++;
1329 infoPtr->items = ReAlloc(infoPtr->items, sizeof(HEADER_ITEM) * infoPtr->uNumItem);
1330 infoPtr->order = ReAlloc(infoPtr->order, sizeof(INT) * infoPtr->uNumItem);
1332 /* make space for the new item */
1333 memmove(&infoPtr->items[nItem + 1], &infoPtr->items[nItem],
1334 (infoPtr->uNumItem - nItem - 1) * sizeof(HEADER_ITEM));
1335 memmove(&infoPtr->order[iOrder + 1], &infoPtr->order[iOrder],
1336 (infoPtr->uNumItem - iOrder - 1) * sizeof(INT));
1338 /* update the order array */
1339 infoPtr->order[iOrder] = nItem;
1340 for (i = 0; i < infoPtr->uNumItem; i++)
1342 if (i != iOrder && infoPtr->order[i] >= nItem)
1343 infoPtr->order[i]++;
1344 infoPtr->items[infoPtr->order[i]].iOrder = i;
1347 lpItem = &infoPtr->items[nItem];
1348 ZeroMemory(lpItem, sizeof(HEADER_ITEM));
1349 /* cxy, fmt and lParam are copied even if not in the HDITEM mask */
1350 copyMask = phdi->mask | HDI_WIDTH | HDI_FORMAT | HDI_LPARAM;
1351 HEADER_StoreHDItemInHeader(lpItem, copyMask, phdi, bUnicode);
1352 lpItem->iOrder = iOrder;
1354 /* set automatically some format bits */
1355 if (phdi->mask & HDI_TEXT)
1356 lpItem->fmt |= HDF_STRING;
1357 else
1358 lpItem->fmt &= ~HDF_STRING;
1360 if (lpItem->hbm != NULL)
1361 lpItem->fmt |= HDF_BITMAP;
1362 else
1363 lpItem->fmt &= ~HDF_BITMAP;
1365 if (phdi->mask & HDI_IMAGE)
1366 lpItem->fmt |= HDF_IMAGE;
1368 HEADER_SetItemBounds (infoPtr);
1369 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1371 return nItem;
1375 static LRESULT
1376 HEADER_Layout (HEADER_INFO *infoPtr, LPHDLAYOUT lpLayout)
1378 lpLayout->pwpos->hwnd = infoPtr->hwndSelf;
1379 lpLayout->pwpos->hwndInsertAfter = 0;
1380 lpLayout->pwpos->x = lpLayout->prc->left;
1381 lpLayout->pwpos->y = lpLayout->prc->top;
1382 lpLayout->pwpos->cx = lpLayout->prc->right - lpLayout->prc->left;
1383 if (infoPtr->dwStyle & HDS_HIDDEN)
1384 lpLayout->pwpos->cy = 0;
1385 else {
1386 lpLayout->pwpos->cy = infoPtr->nHeight;
1387 lpLayout->prc->top += infoPtr->nHeight;
1389 lpLayout->pwpos->flags = SWP_NOZORDER;
1391 TRACE("Layout x=%d y=%d cx=%d cy=%d\n",
1392 lpLayout->pwpos->x, lpLayout->pwpos->y,
1393 lpLayout->pwpos->cx, lpLayout->pwpos->cy);
1395 infoPtr->bRectsValid = FALSE;
1397 return TRUE;
1401 static LRESULT
1402 HEADER_SetImageList (HEADER_INFO *infoPtr, HIMAGELIST himl)
1404 HIMAGELIST himlOld;
1406 TRACE("(himl %p)\n", himl);
1407 himlOld = infoPtr->himl;
1408 infoPtr->himl = himl;
1410 /* FIXME: Refresh needed??? */
1412 return (LRESULT)himlOld;
1416 static LRESULT
1417 HEADER_GetBitmapMargin(const HEADER_INFO *infoPtr)
1419 return infoPtr->iMargin;
1422 static LRESULT
1423 HEADER_SetBitmapMargin(HEADER_INFO *infoPtr, INT iMargin)
1425 INT oldMargin = infoPtr->iMargin;
1427 infoPtr->iMargin = iMargin;
1429 return oldMargin;
1432 static LRESULT
1433 HEADER_SetItemT (HEADER_INFO *infoPtr, INT nItem, const HDITEMW *phdi, BOOL bUnicode)
1435 HEADER_ITEM *lpItem;
1436 HDITEMW hdNotify;
1437 void *pvScratch;
1439 if (phdi == NULL)
1440 return FALSE;
1441 if ((nItem < 0) || (nItem >= (INT)infoPtr->uNumItem))
1442 return FALSE;
1444 TRACE("[nItem=%d]\n", nItem);
1446 HEADER_CopyHDItemForNotify(infoPtr, &hdNotify, phdi, bUnicode, &pvScratch);
1447 if (HEADER_SendNotifyWithHDItemT(infoPtr, HDN_ITEMCHANGINGW, nItem, &hdNotify))
1449 Free(pvScratch);
1450 return FALSE;
1453 lpItem = &infoPtr->items[nItem];
1454 HEADER_StoreHDItemInHeader(lpItem, phdi->mask, phdi, bUnicode);
1456 if (phdi->mask & HDI_ORDER)
1457 if (phdi->iOrder >= 0 && phdi->iOrder < infoPtr->uNumItem)
1458 HEADER_ChangeItemOrder(infoPtr, nItem, phdi->iOrder);
1460 HEADER_SendNotifyWithHDItemT(infoPtr, HDN_ITEMCHANGEDW, nItem, &hdNotify);
1462 HEADER_SetItemBounds (infoPtr);
1464 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1466 Free(pvScratch);
1467 return TRUE;
1470 static inline LRESULT
1471 HEADER_SetUnicodeFormat (HEADER_INFO *infoPtr, WPARAM wParam)
1473 BOOL bTemp = (infoPtr->nNotifyFormat == NFR_UNICODE);
1475 infoPtr->nNotifyFormat = ((BOOL)wParam ? NFR_UNICODE : NFR_ANSI);
1477 return bTemp;
1481 static LRESULT
1482 HEADER_Create (HWND hwnd, const CREATESTRUCTW *lpcs)
1484 HEADER_INFO *infoPtr;
1485 TEXTMETRICW tm;
1486 HFONT hOldFont;
1487 HDC hdc;
1489 infoPtr = Alloc (sizeof(HEADER_INFO));
1490 SetWindowLongPtrW (hwnd, 0, (DWORD_PTR)infoPtr);
1492 infoPtr->hwndSelf = hwnd;
1493 infoPtr->hwndNotify = lpcs->hwndParent;
1494 infoPtr->uNumItem = 0;
1495 infoPtr->hFont = 0;
1496 infoPtr->items = 0;
1497 infoPtr->order = 0;
1498 infoPtr->bRectsValid = FALSE;
1499 infoPtr->hcurArrow = LoadCursorW (0, (LPWSTR)IDC_ARROW);
1500 infoPtr->hcurDivider = LoadCursorW (COMCTL32_hModule, MAKEINTRESOURCEW(IDC_DIVIDER));
1501 infoPtr->hcurDivopen = LoadCursorW (COMCTL32_hModule, MAKEINTRESOURCEW(IDC_DIVIDEROPEN));
1502 infoPtr->bPressed = FALSE;
1503 infoPtr->bTracking = FALSE;
1504 infoPtr->dwStyle = lpcs->style;
1505 infoPtr->iMoveItem = 0;
1506 infoPtr->himl = 0;
1507 infoPtr->iHotItem = -1;
1508 infoPtr->iHotDivider = -1;
1509 infoPtr->iMargin = 3*GetSystemMetrics(SM_CXEDGE);
1510 infoPtr->nNotifyFormat =
1511 SendMessageW (infoPtr->hwndNotify, WM_NOTIFYFORMAT, (WPARAM)hwnd, NF_QUERY);
1513 hdc = GetDC (0);
1514 hOldFont = SelectObject (hdc, GetStockObject (SYSTEM_FONT));
1515 GetTextMetricsW (hdc, &tm);
1516 infoPtr->nHeight = tm.tmHeight + VERT_BORDER;
1517 SelectObject (hdc, hOldFont);
1518 ReleaseDC (0, hdc);
1520 OpenThemeData(hwnd, themeClass);
1522 return 0;
1526 static LRESULT
1527 HEADER_Destroy (HEADER_INFO *infoPtr)
1529 HTHEME theme = GetWindowTheme(infoPtr->hwndSelf);
1530 CloseThemeData(theme);
1531 return 0;
1534 static LRESULT
1535 HEADER_NCDestroy (HEADER_INFO *infoPtr)
1537 HEADER_ITEM *lpItem;
1538 INT nItem;
1540 if (infoPtr->items) {
1541 lpItem = infoPtr->items;
1542 for (nItem = 0; nItem < infoPtr->uNumItem; nItem++, lpItem++) {
1543 Free(lpItem->pszText);
1545 Free (infoPtr->items);
1548 Free(infoPtr->order);
1550 SetWindowLongPtrW (infoPtr->hwndSelf, 0, 0);
1551 Free (infoPtr);
1553 return 0;
1557 static inline LRESULT
1558 HEADER_GetFont (const HEADER_INFO *infoPtr)
1560 return (LRESULT)infoPtr->hFont;
1564 static BOOL
1565 HEADER_IsDragDistance(const HEADER_INFO *infoPtr, const POINT *pt)
1567 /* Windows allows for a mouse movement before starting the drag. We use the
1568 * SM_CXDOUBLECLICK/SM_CYDOUBLECLICK as that distance.
1570 return (abs(infoPtr->ptLButtonDown.x - pt->x)>GetSystemMetrics(SM_CXDOUBLECLK) ||
1571 abs(infoPtr->ptLButtonDown.y - pt->y)>GetSystemMetrics(SM_CYDOUBLECLK));
1574 static LRESULT
1575 HEADER_LButtonDblClk (const HEADER_INFO *infoPtr, INT x, INT y)
1577 POINT pt;
1578 UINT flags;
1579 INT nItem;
1581 pt.x = x;
1582 pt.y = y;
1583 HEADER_InternalHitTest (infoPtr, &pt, &flags, &nItem);
1585 if ((infoPtr->dwStyle & HDS_BUTTONS) && (flags == HHT_ONHEADER))
1586 HEADER_SendNotifyWithHDItemT(infoPtr, HDN_ITEMDBLCLICKW, nItem, NULL);
1587 else if ((flags == HHT_ONDIVIDER) || (flags == HHT_ONDIVOPEN))
1588 HEADER_SendNotifyWithHDItemT(infoPtr, HDN_DIVIDERDBLCLICKW, nItem, NULL);
1590 return 0;
1594 static LRESULT
1595 HEADER_LButtonDown (HEADER_INFO *infoPtr, INT x, INT y)
1597 POINT pt;
1598 UINT flags;
1599 INT nItem;
1600 HDC hdc;
1602 pt.x = x;
1603 pt.y = y;
1604 HEADER_InternalHitTest (infoPtr, &pt, &flags, &nItem);
1606 if ((infoPtr->dwStyle & HDS_BUTTONS) && (flags == HHT_ONHEADER)) {
1607 SetCapture (infoPtr->hwndSelf);
1608 infoPtr->bCaptured = TRUE;
1609 infoPtr->bPressed = TRUE;
1610 infoPtr->bDragging = FALSE;
1611 infoPtr->iMoveItem = nItem;
1612 infoPtr->ptLButtonDown = pt;
1614 infoPtr->items[nItem].bDown = TRUE;
1616 /* Send WM_CUSTOMDRAW */
1617 hdc = GetDC (infoPtr->hwndSelf);
1618 HEADER_RefreshItem (infoPtr, nItem);
1619 ReleaseDC (infoPtr->hwndSelf, hdc);
1621 TRACE("Pressed item %d!\n", nItem);
1623 else if ((flags == HHT_ONDIVIDER) || (flags == HHT_ONDIVOPEN)) {
1624 INT iCurrWidth = infoPtr->items[nItem].cxy;
1625 if (!HEADER_SendNotifyWithIntFieldT(infoPtr, HDN_BEGINTRACKW, nItem, HDI_WIDTH, iCurrWidth))
1627 SetCapture (infoPtr->hwndSelf);
1628 infoPtr->bCaptured = TRUE;
1629 infoPtr->bTracking = TRUE;
1630 infoPtr->iMoveItem = nItem;
1631 infoPtr->xTrackOffset = infoPtr->items[nItem].rect.right - pt.x;
1633 if (!(infoPtr->dwStyle & HDS_FULLDRAG)) {
1634 infoPtr->xOldTrack = infoPtr->items[nItem].rect.right;
1635 hdc = GetDC (infoPtr->hwndSelf);
1636 HEADER_DrawTrackLine (infoPtr, hdc, infoPtr->xOldTrack);
1637 ReleaseDC (infoPtr->hwndSelf, hdc);
1640 TRACE("Begin tracking item %d!\n", nItem);
1644 return 0;
1648 static LRESULT
1649 HEADER_LButtonUp (HEADER_INFO *infoPtr, INT x, INT y)
1651 POINT pt;
1652 UINT flags;
1653 INT nItem;
1654 HDC hdc;
1656 pt.x = x;
1657 pt.y = y;
1658 HEADER_InternalHitTest (infoPtr, &pt, &flags, &nItem);
1660 if (infoPtr->bPressed) {
1662 infoPtr->items[infoPtr->iMoveItem].bDown = FALSE;
1664 if (infoPtr->bDragging)
1666 HEADER_ITEM *lpItem = &infoPtr->items[infoPtr->iMoveItem];
1667 INT iNewOrder;
1669 ImageList_DragShowNolock(FALSE);
1670 ImageList_EndDrag();
1672 if (infoPtr->iHotDivider == -1)
1673 iNewOrder = -1;
1674 else if (infoPtr->iHotDivider == infoPtr->uNumItem)
1675 iNewOrder = infoPtr->uNumItem-1;
1676 else
1678 iNewOrder = HEADER_IndexToOrder(infoPtr, infoPtr->iHotDivider);
1679 if (iNewOrder > lpItem->iOrder)
1680 iNewOrder--;
1683 if (iNewOrder != -1 &&
1684 !HEADER_SendNotifyWithIntFieldT(infoPtr, HDN_ENDDRAG, infoPtr->iMoveItem, HDI_ORDER, iNewOrder))
1686 HEADER_ChangeItemOrder(infoPtr, infoPtr->iMoveItem, iNewOrder);
1687 infoPtr->bRectsValid = FALSE;
1688 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1690 else
1691 InvalidateRect(infoPtr->hwndSelf, &infoPtr->items[infoPtr->iMoveItem].rect, FALSE);
1693 infoPtr->bDragging = FALSE;
1694 HEADER_SetHotDivider(infoPtr, FALSE, -1);
1696 else
1698 hdc = GetDC (infoPtr->hwndSelf);
1699 HEADER_RefreshItem (infoPtr, infoPtr->iMoveItem);
1700 ReleaseDC (infoPtr->hwndSelf, hdc);
1702 if (!(infoPtr->dwStyle & HDS_DRAGDROP) || !HEADER_IsDragDistance(infoPtr, &pt))
1703 HEADER_SendNotifyWithHDItemT(infoPtr, HDN_ITEMCLICKW, infoPtr->iMoveItem, NULL);
1706 TRACE("Released item %d!\n", infoPtr->iMoveItem);
1707 infoPtr->bPressed = FALSE;
1709 else if (infoPtr->bTracking) {
1710 INT iNewWidth = pt.x - infoPtr->items[infoPtr->iMoveItem].rect.left + infoPtr->xTrackOffset;
1711 if (iNewWidth < 0)
1712 iNewWidth = 0;
1713 TRACE("End tracking item %d!\n", infoPtr->iMoveItem);
1714 infoPtr->bTracking = FALSE;
1716 HEADER_SendNotifyWithIntFieldT(infoPtr, HDN_ENDTRACKW, infoPtr->iMoveItem, HDI_WIDTH, iNewWidth);
1718 if (!(infoPtr->dwStyle & HDS_FULLDRAG)) {
1719 hdc = GetDC (infoPtr->hwndSelf);
1720 HEADER_DrawTrackLine (infoPtr, hdc, infoPtr->xOldTrack);
1721 ReleaseDC (infoPtr->hwndSelf, hdc);
1724 if (!HEADER_SendNotifyWithIntFieldT(infoPtr, HDN_ITEMCHANGINGW, infoPtr->iMoveItem, HDI_WIDTH, iNewWidth))
1726 infoPtr->items[infoPtr->iMoveItem].cxy = iNewWidth;
1727 HEADER_SendNotifyWithIntFieldT(infoPtr, HDN_ITEMCHANGEDW, infoPtr->iMoveItem, HDI_WIDTH, iNewWidth);
1730 HEADER_SetItemBounds (infoPtr);
1731 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1734 if (infoPtr->bCaptured) {
1735 infoPtr->bCaptured = FALSE;
1736 ReleaseCapture ();
1737 HEADER_SendSimpleNotify (infoPtr, NM_RELEASEDCAPTURE);
1740 return 0;
1744 static LRESULT
1745 HEADER_NotifyFormat (HEADER_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
1747 switch (lParam)
1749 case NF_QUERY:
1750 return infoPtr->nNotifyFormat;
1752 case NF_REQUERY:
1753 infoPtr->nNotifyFormat =
1754 SendMessageW ((HWND)wParam, WM_NOTIFYFORMAT,
1755 (WPARAM)infoPtr->hwndSelf, NF_QUERY);
1756 return infoPtr->nNotifyFormat;
1759 return 0;
1762 static LRESULT
1763 HEADER_MouseLeave (HEADER_INFO *infoPtr)
1765 /* Reset hot-tracked item when mouse leaves control. */
1766 INT oldHotItem = infoPtr->iHotItem;
1767 HDC hdc = GetDC (infoPtr->hwndSelf);
1769 infoPtr->iHotItem = -1;
1770 if (oldHotItem != -1) HEADER_RefreshItem (infoPtr, oldHotItem);
1771 ReleaseDC (infoPtr->hwndSelf, hdc);
1773 return 0;
1777 static LRESULT
1778 HEADER_MouseMove (HEADER_INFO *infoPtr, LPARAM lParam)
1780 POINT pt;
1781 UINT flags;
1782 INT nItem, nWidth;
1783 HDC hdc;
1784 /* With theming, hottracking is always enabled */
1785 BOOL hotTrackEnabled =
1786 ((infoPtr->dwStyle & HDS_BUTTONS) && (infoPtr->dwStyle & HDS_HOTTRACK))
1787 || (GetWindowTheme (infoPtr->hwndSelf) != NULL);
1788 INT oldHotItem = infoPtr->iHotItem;
1790 pt.x = (INT)(SHORT)LOWORD(lParam);
1791 pt.y = (INT)(SHORT)HIWORD(lParam);
1792 HEADER_InternalHitTest (infoPtr, &pt, &flags, &nItem);
1794 if (hotTrackEnabled) {
1795 if (flags & (HHT_ONHEADER | HHT_ONDIVIDER | HHT_ONDIVOPEN))
1796 infoPtr->iHotItem = nItem;
1797 else
1798 infoPtr->iHotItem = -1;
1801 if (infoPtr->bCaptured) {
1802 /* check if we should drag the header */
1803 if (infoPtr->bPressed && !infoPtr->bDragging && (infoPtr->dwStyle & HDS_DRAGDROP)
1804 && HEADER_IsDragDistance(infoPtr, &pt))
1806 if (!HEADER_SendNotifyWithHDItemT(infoPtr, HDN_BEGINDRAG, infoPtr->iMoveItem, NULL))
1808 HIMAGELIST hDragItem = (HIMAGELIST)HEADER_CreateDragImage(infoPtr, infoPtr->iMoveItem);
1809 if (hDragItem != NULL)
1811 HEADER_ITEM *lpItem = &infoPtr->items[infoPtr->iMoveItem];
1812 TRACE("Starting item drag\n");
1813 ImageList_BeginDrag(hDragItem, 0, pt.x - lpItem->rect.left, 0);
1814 ImageList_DragShowNolock(TRUE);
1815 ImageList_Destroy(hDragItem);
1816 infoPtr->bDragging = TRUE;
1821 if (infoPtr->bDragging)
1823 POINT drag;
1824 drag.x = pt.x;
1825 drag.y = 0;
1826 ClientToScreen(infoPtr->hwndSelf, &drag);
1827 ImageList_DragMove(drag.x, drag.y);
1828 HEADER_SetHotDivider(infoPtr, TRUE, lParam);
1831 if (infoPtr->bPressed && !infoPtr->bDragging) {
1832 BOOL oldState = infoPtr->items[infoPtr->iMoveItem].bDown;
1833 if ((nItem == infoPtr->iMoveItem) && (flags == HHT_ONHEADER))
1834 infoPtr->items[infoPtr->iMoveItem].bDown = TRUE;
1835 else
1836 infoPtr->items[infoPtr->iMoveItem].bDown = FALSE;
1837 if (oldState != infoPtr->items[infoPtr->iMoveItem].bDown) {
1838 hdc = GetDC (infoPtr->hwndSelf);
1839 HEADER_RefreshItem (infoPtr, infoPtr->iMoveItem);
1840 ReleaseDC (infoPtr->hwndSelf, hdc);
1843 TRACE("Moving pressed item %d!\n", infoPtr->iMoveItem);
1845 else if (infoPtr->bTracking) {
1846 if (infoPtr->dwStyle & HDS_FULLDRAG) {
1847 HEADER_ITEM *lpItem = &infoPtr->items[infoPtr->iMoveItem];
1848 nWidth = pt.x - lpItem->rect.left + infoPtr->xTrackOffset;
1849 if (!HEADER_SendNotifyWithIntFieldT(infoPtr, HDN_ITEMCHANGINGW, infoPtr->iMoveItem, HDI_WIDTH, nWidth))
1851 INT nOldWidth = lpItem->rect.right - lpItem->rect.left;
1852 RECT rcClient;
1853 RECT rcScroll;
1855 if (nWidth < 0) nWidth = 0;
1856 infoPtr->items[infoPtr->iMoveItem].cxy = nWidth;
1857 HEADER_SetItemBounds(infoPtr);
1859 GetClientRect(infoPtr->hwndSelf, &rcClient);
1860 rcScroll = rcClient;
1861 rcScroll.left = lpItem->rect.left + nOldWidth;
1862 ScrollWindowEx(infoPtr->hwndSelf, nWidth - nOldWidth, 0, &rcScroll, &rcClient, NULL, NULL, 0);
1863 InvalidateRect(infoPtr->hwndSelf, &lpItem->rect, FALSE);
1864 UpdateWindow(infoPtr->hwndSelf);
1866 HEADER_SendNotifyWithIntFieldT(infoPtr, HDN_ITEMCHANGEDW, infoPtr->iMoveItem, HDI_WIDTH, nWidth);
1869 else {
1870 INT iTrackWidth;
1871 hdc = GetDC (infoPtr->hwndSelf);
1872 HEADER_DrawTrackLine (infoPtr, hdc, infoPtr->xOldTrack);
1873 infoPtr->xOldTrack = pt.x + infoPtr->xTrackOffset;
1874 if (infoPtr->xOldTrack < infoPtr->items[infoPtr->iMoveItem].rect.left)
1875 infoPtr->xOldTrack = infoPtr->items[infoPtr->iMoveItem].rect.left;
1876 HEADER_DrawTrackLine (infoPtr, hdc, infoPtr->xOldTrack);
1877 ReleaseDC (infoPtr->hwndSelf, hdc);
1878 iTrackWidth = infoPtr->xOldTrack - infoPtr->items[infoPtr->iMoveItem].rect.left;
1879 /* FIXME: should stop tracking if HDN_TRACK returns TRUE */
1880 HEADER_SendNotifyWithIntFieldT(infoPtr, HDN_TRACKW, infoPtr->iMoveItem, HDI_WIDTH, iTrackWidth);
1883 TRACE("Tracking item %d!\n", infoPtr->iMoveItem);
1887 if (hotTrackEnabled) {
1888 TRACKMOUSEEVENT tme;
1889 if (oldHotItem != infoPtr->iHotItem && !infoPtr->bDragging) {
1890 hdc = GetDC (infoPtr->hwndSelf);
1891 if (oldHotItem != -1) HEADER_RefreshItem (infoPtr, oldHotItem);
1892 if (infoPtr->iHotItem != -1) HEADER_RefreshItem (infoPtr, infoPtr->iHotItem);
1893 ReleaseDC (infoPtr->hwndSelf, hdc);
1895 tme.cbSize = sizeof( tme );
1896 tme.dwFlags = TME_LEAVE;
1897 tme.hwndTrack = infoPtr->hwndSelf;
1898 TrackMouseEvent( &tme );
1901 return 0;
1905 static LRESULT
1906 HEADER_Paint (HEADER_INFO *infoPtr, HDC hdcParam)
1908 HDC hdc;
1909 PAINTSTRUCT ps;
1911 hdc = hdcParam==0 ? BeginPaint (infoPtr->hwndSelf, &ps) : hdcParam;
1912 HEADER_Refresh (infoPtr, hdc);
1913 if(!hdcParam)
1914 EndPaint (infoPtr->hwndSelf, &ps);
1915 return 0;
1919 static LRESULT
1920 HEADER_RButtonUp (HEADER_INFO *infoPtr, INT x, INT y)
1922 BOOL bRet;
1923 POINT pt;
1925 pt.x = x;
1926 pt.y = y;
1928 /* Send a Notify message */
1929 bRet = HEADER_SendSimpleNotify (infoPtr, NM_RCLICK);
1931 /* Change to screen coordinate for WM_CONTEXTMENU */
1932 ClientToScreen(infoPtr->hwndSelf, &pt);
1934 /* Send a WM_CONTEXTMENU message in response to the RBUTTONUP */
1935 SendMessageW( infoPtr->hwndSelf, WM_CONTEXTMENU, (WPARAM) infoPtr->hwndSelf, MAKELPARAM(pt.x, pt.y));
1937 return bRet;
1941 static LRESULT
1942 HEADER_SetCursor (HEADER_INFO *infoPtr, LPARAM lParam)
1944 POINT pt;
1945 UINT flags;
1946 INT nItem;
1948 TRACE("code=0x%X id=0x%X\n", LOWORD(lParam), HIWORD(lParam));
1950 GetCursorPos (&pt);
1951 ScreenToClient (infoPtr->hwndSelf, &pt);
1953 HEADER_InternalHitTest (infoPtr, &pt, &flags, &nItem);
1955 if (flags == HHT_ONDIVIDER)
1956 SetCursor (infoPtr->hcurDivider);
1957 else if (flags == HHT_ONDIVOPEN)
1958 SetCursor (infoPtr->hcurDivopen);
1959 else
1960 SetCursor (infoPtr->hcurArrow);
1962 return 0;
1966 static LRESULT
1967 HEADER_SetFont (HEADER_INFO *infoPtr, HFONT hFont, WORD Redraw)
1969 TEXTMETRICW tm;
1970 HFONT hOldFont;
1971 HDC hdc;
1973 infoPtr->hFont = hFont;
1975 hdc = GetDC (0);
1976 hOldFont = SelectObject (hdc, infoPtr->hFont ? infoPtr->hFont : GetStockObject (SYSTEM_FONT));
1977 GetTextMetricsW (hdc, &tm);
1978 infoPtr->nHeight = tm.tmHeight + VERT_BORDER;
1979 SelectObject (hdc, hOldFont);
1980 ReleaseDC (0, hdc);
1982 infoPtr->bRectsValid = FALSE;
1984 if (Redraw) {
1985 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1988 return 0;
1991 static LRESULT HEADER_SetRedraw(HEADER_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
1993 /* ignoring the InvalidateRect calls is handled by user32. But some apps expect
1994 * that we invalidate the header and this has to be done manually */
1995 LRESULT ret;
1997 ret = DefWindowProcW(infoPtr->hwndSelf, WM_SETREDRAW, wParam, lParam);
1998 if (wParam)
1999 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
2000 return ret;
2003 static INT HEADER_StyleChanged(HEADER_INFO *infoPtr, WPARAM wStyleType,
2004 const STYLESTRUCT *lpss)
2006 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
2007 wStyleType, lpss->styleOld, lpss->styleNew);
2009 if (wStyleType != GWL_STYLE) return 0;
2011 infoPtr->dwStyle = lpss->styleNew;
2013 return 0;
2016 /* Update the theme handle after a theme change */
2017 static LRESULT HEADER_ThemeChanged(const HEADER_INFO *infoPtr)
2019 HTHEME theme = GetWindowTheme(infoPtr->hwndSelf);
2020 CloseThemeData(theme);
2021 OpenThemeData(infoPtr->hwndSelf, themeClass);
2022 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2023 return 0;
2027 static LRESULT WINAPI
2028 HEADER_WindowProc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
2030 HEADER_INFO *infoPtr = (HEADER_INFO *)GetWindowLongPtrW(hwnd, 0);
2032 TRACE("hwnd=%p msg=%x wparam=%lx lParam=%lx\n", hwnd, msg, wParam, lParam);
2033 if (!infoPtr && (msg != WM_CREATE))
2034 return DefWindowProcW (hwnd, msg, wParam, lParam);
2035 switch (msg) {
2036 /* case HDM_CLEARFILTER: */
2038 case HDM_CREATEDRAGIMAGE:
2039 return HEADER_CreateDragImage (infoPtr, (INT)wParam);
2041 case HDM_DELETEITEM:
2042 return HEADER_DeleteItem (infoPtr, (INT)wParam);
2044 /* case HDM_EDITFILTER: */
2046 case HDM_GETBITMAPMARGIN:
2047 return HEADER_GetBitmapMargin(infoPtr);
2049 case HDM_GETIMAGELIST:
2050 return HEADER_GetImageList (infoPtr);
2052 case HDM_GETITEMA:
2053 case HDM_GETITEMW:
2054 return HEADER_GetItemT (infoPtr, (INT)wParam, (LPHDITEMW)lParam, msg == HDM_GETITEMW);
2056 case HDM_GETITEMCOUNT:
2057 return HEADER_GetItemCount (infoPtr);
2059 case HDM_GETITEMRECT:
2060 return HEADER_GetItemRect (infoPtr, (INT)wParam, (LPRECT)lParam);
2062 case HDM_GETORDERARRAY:
2063 return HEADER_GetOrderArray(infoPtr, (INT)wParam, (LPINT)lParam);
2065 case HDM_GETUNICODEFORMAT:
2066 return HEADER_GetUnicodeFormat (infoPtr);
2068 case HDM_HITTEST:
2069 return HEADER_HitTest (infoPtr, (LPHDHITTESTINFO)lParam);
2071 case HDM_INSERTITEMA:
2072 case HDM_INSERTITEMW:
2073 return HEADER_InsertItemT (infoPtr, (INT)wParam, (LPHDITEMW)lParam, msg == HDM_INSERTITEMW);
2075 case HDM_LAYOUT:
2076 return HEADER_Layout (infoPtr, (LPHDLAYOUT)lParam);
2078 case HDM_ORDERTOINDEX:
2079 return HEADER_OrderToIndex(infoPtr, (INT)wParam);
2081 case HDM_SETBITMAPMARGIN:
2082 return HEADER_SetBitmapMargin(infoPtr, (INT)wParam);
2084 /* case HDM_SETFILTERCHANGETIMEOUT: */
2086 case HDM_SETHOTDIVIDER:
2087 return HEADER_SetHotDivider(infoPtr, wParam, lParam);
2089 case HDM_SETIMAGELIST:
2090 return HEADER_SetImageList (infoPtr, (HIMAGELIST)lParam);
2092 case HDM_SETITEMA:
2093 case HDM_SETITEMW:
2094 return HEADER_SetItemT (infoPtr, (INT)wParam, (LPHDITEMW)lParam, msg == HDM_SETITEMW);
2096 case HDM_SETORDERARRAY:
2097 return HEADER_SetOrderArray(infoPtr, (INT)wParam, (LPINT)lParam);
2099 case HDM_SETUNICODEFORMAT:
2100 return HEADER_SetUnicodeFormat (infoPtr, wParam);
2102 case WM_CREATE:
2103 return HEADER_Create (hwnd, (LPCREATESTRUCTW)lParam);
2105 case WM_DESTROY:
2106 return HEADER_Destroy (infoPtr);
2108 case WM_NCDESTROY:
2109 return HEADER_NCDestroy (infoPtr);
2111 case WM_ERASEBKGND:
2112 return 1;
2114 case WM_GETDLGCODE:
2115 return DLGC_WANTTAB | DLGC_WANTARROWS;
2117 case WM_GETFONT:
2118 return HEADER_GetFont (infoPtr);
2120 case WM_LBUTTONDBLCLK:
2121 return HEADER_LButtonDblClk (infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
2123 case WM_LBUTTONDOWN:
2124 return HEADER_LButtonDown (infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
2126 case WM_LBUTTONUP:
2127 return HEADER_LButtonUp (infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
2129 case WM_MOUSELEAVE:
2130 return HEADER_MouseLeave (infoPtr);
2132 case WM_MOUSEMOVE:
2133 return HEADER_MouseMove (infoPtr, lParam);
2135 case WM_NOTIFYFORMAT:
2136 return HEADER_NotifyFormat (infoPtr, wParam, lParam);
2138 case WM_SIZE:
2139 return HEADER_Size (infoPtr);
2141 case WM_THEMECHANGED:
2142 return HEADER_ThemeChanged (infoPtr);
2144 case WM_PRINTCLIENT:
2145 case WM_PAINT:
2146 return HEADER_Paint (infoPtr, (HDC)wParam);
2148 case WM_RBUTTONUP:
2149 return HEADER_RButtonUp (infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
2151 case WM_SETCURSOR:
2152 return HEADER_SetCursor (infoPtr, lParam);
2154 case WM_SETFONT:
2155 return HEADER_SetFont (infoPtr, (HFONT)wParam, (WORD)lParam);
2157 case WM_SETREDRAW:
2158 return HEADER_SetRedraw(infoPtr, wParam, lParam);
2160 case WM_STYLECHANGED:
2161 return HEADER_StyleChanged(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
2163 case WM_SYSCOLORCHANGE:
2164 COMCTL32_RefreshSysColors();
2165 return 0;
2167 default:
2168 if ((msg >= WM_USER) && (msg < WM_APP) && !COMCTL32_IsReflectedMessage(msg))
2169 ERR("unknown msg %04x wp=%04lx lp=%08lx\n",
2170 msg, wParam, lParam );
2171 return DefWindowProcW(hwnd, msg, wParam, lParam);
2176 VOID
2177 HEADER_Register (void)
2179 WNDCLASSW wndClass;
2181 ZeroMemory (&wndClass, sizeof(WNDCLASSW));
2182 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS;
2183 wndClass.lpfnWndProc = HEADER_WindowProc;
2184 wndClass.cbClsExtra = 0;
2185 wndClass.cbWndExtra = sizeof(HEADER_INFO *);
2186 wndClass.hCursor = LoadCursorW (0, (LPWSTR)IDC_ARROW);
2187 wndClass.lpszClassName = WC_HEADERW;
2189 RegisterClassW (&wndClass);
2193 VOID
2194 HEADER_Unregister (void)
2196 UnregisterClassW (WC_HEADERW, NULL);