mountmgr: Name DosDevices constants consistently.
[wine.git] / dlls / comctl32 / header.c
blob705c6c4c637ecaabfd0df69ae0a77a9bc3970230
1 /*
2 * Header control
4 * Copyright 1998 Eric Kohl
5 * Copyright 2000 Eric Kohl for CodeWeavers
6 * Copyright 2003 Maxime Bellenge
7 * Copyright 2006 Mikolaj Zalewski
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 * TODO:
24 * - Imagelist support (completed?)
25 * - Hottrack support (completed?)
26 * - Filters support (HDS_FILTER, HDI_FILTER, HDM_*FILTER*, HDN_*FILTER*)
27 * - New Windows Vista features
30 #include <stdarg.h>
31 #include <stdlib.h>
32 #include <string.h>
34 #include "windef.h"
35 #include "winbase.h"
36 #include "wine/unicode.h"
37 #include "wingdi.h"
38 #include "winuser.h"
39 #include "winnls.h"
40 #include "commctrl.h"
41 #include "comctl32.h"
42 #include "vssym32.h"
43 #include "uxtheme.h"
44 #include "wine/debug.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(header);
48 typedef struct
50 INT cxy;
51 HBITMAP hbm;
52 LPWSTR pszText;
53 INT fmt;
54 LPARAM lParam;
55 INT iImage;
56 INT iOrder; /* see documentation of HD_ITEM */
58 BOOL bDown; /* is item pressed? (used for drawing) */
59 RECT rect; /* bounding rectangle of the item */
60 DWORD callbackMask; /* HDI_* flags for items that are callback */
61 } HEADER_ITEM;
64 typedef struct
66 HWND hwndSelf; /* Control window */
67 HWND hwndNotify; /* Owner window to send notifications to */
68 INT nNotifyFormat; /* format used for WM_NOTIFY messages */
69 UINT uNumItem; /* number of items (columns) */
70 INT nHeight; /* height of the header (pixels) */
71 HFONT hFont; /* handle to the current font */
72 HCURSOR hcurArrow; /* handle to the arrow cursor */
73 HCURSOR hcurDivider; /* handle to a cursor (used over dividers) <-|-> */
74 HCURSOR hcurDivopen; /* handle to a cursor (used over dividers) <-||-> */
75 BOOL bCaptured; /* Is the mouse captured? */
76 BOOL bPressed; /* Is a header item pressed (down)? */
77 BOOL bDragging; /* Are we dragging an item? */
78 BOOL bTracking; /* Is in tracking mode? */
79 POINT ptLButtonDown; /* The point where the left button was pressed */
80 DWORD dwStyle; /* the cached window GWL_STYLE */
81 INT iMoveItem; /* index of tracked item. (Tracking mode) */
82 INT xTrackOffset; /* distance between the right side of the tracked item and the cursor */
83 INT xOldTrack; /* track offset (see above) after the last WM_MOUSEMOVE */
84 INT iHotItem; /* index of hot item (cursor is over this item) */
85 INT iHotDivider; /* index of the hot divider (used while dragging an item or by HDM_SETHOTDIVIDER) */
86 INT iMargin; /* width of the margin that surrounds a bitmap */
87 INT filter_change_timeout; /* change timeout set with HDM_SETFILTERCHANGETIMEOUT */
89 HIMAGELIST himl; /* handle to an image list (may be 0) */
90 HEADER_ITEM *items; /* pointer to array of HEADER_ITEM's */
91 INT *order; /* array of item IDs indexed by order */
92 BOOL bRectsValid; /* validity flag for bounding rectangles */
93 } HEADER_INFO;
96 #define VERT_BORDER 4
97 #define DIVIDER_WIDTH 10
98 #define HOT_DIVIDER_WIDTH 2
99 #define MAX_HEADER_TEXT_LEN 260
100 #define HDN_UNICODE_OFFSET 20
101 #define HDN_FIRST_UNICODE (HDN_FIRST-HDN_UNICODE_OFFSET)
103 #define HDI_SUPPORTED_FIELDS (HDI_WIDTH|HDI_TEXT|HDI_FORMAT|HDI_LPARAM|HDI_BITMAP|HDI_IMAGE|HDI_ORDER)
104 #define HDI_UNSUPPORTED_FIELDS (HDI_FILTER)
105 #define HDI_UNKNOWN_FIELDS (~(HDI_SUPPORTED_FIELDS|HDI_UNSUPPORTED_FIELDS|HDI_DI_SETITEM))
106 #define HDI_COMCTL32_4_0_FIELDS (HDI_WIDTH|HDI_TEXT|HDI_FORMAT|HDI_LPARAM|HDI_BITMAP)
109 static BOOL HEADER_PrepareCallbackItems(const HEADER_INFO *infoPtr, INT iItem, INT reqMask);
110 static void HEADER_FreeCallbackItems(HEADER_ITEM *lpItem);
111 static LRESULT HEADER_SendNotify(const HEADER_INFO *infoPtr, UINT code, NMHDR *hdr);
112 static LRESULT HEADER_SendCtrlCustomDraw(const HEADER_INFO *infoPtr, DWORD dwDrawStage, HDC hdc, const RECT *rect);
114 static const WCHAR themeClass[] = {'H','e','a','d','e','r',0};
116 static void HEADER_StoreHDItemInHeader(HEADER_ITEM *lpItem, UINT mask, const HDITEMW *phdi, BOOL fUnicode)
118 if (mask & HDI_UNSUPPORTED_FIELDS)
119 FIXME("unsupported header fields %x\n", (mask & HDI_UNSUPPORTED_FIELDS));
121 if (mask & HDI_BITMAP)
122 lpItem->hbm = phdi->hbm;
124 if (mask & HDI_FORMAT)
125 lpItem->fmt = phdi->fmt;
127 if (mask & HDI_LPARAM)
128 lpItem->lParam = phdi->lParam;
130 if (mask & HDI_WIDTH)
131 lpItem->cxy = phdi->cxy;
133 if (mask & HDI_IMAGE)
135 lpItem->iImage = phdi->iImage;
136 if (phdi->iImage == I_IMAGECALLBACK)
137 lpItem->callbackMask |= HDI_IMAGE;
138 else
139 lpItem->callbackMask &= ~HDI_IMAGE;
142 if (mask & HDI_TEXT)
144 Free(lpItem->pszText);
145 lpItem->pszText = NULL;
147 if (phdi->pszText != LPSTR_TEXTCALLBACKW) /* covers != TEXTCALLBACKA too */
149 static const WCHAR emptyString[] = {0};
151 LPCWSTR pszText = (phdi->pszText != NULL ? phdi->pszText : emptyString);
152 if (fUnicode)
153 Str_SetPtrW(&lpItem->pszText, pszText);
154 else
155 Str_SetPtrAtoW(&lpItem->pszText, (LPCSTR)pszText);
156 lpItem->callbackMask &= ~HDI_TEXT;
158 else
160 lpItem->pszText = NULL;
161 lpItem->callbackMask |= HDI_TEXT;
166 static inline LRESULT
167 HEADER_IndexToOrder (const HEADER_INFO *infoPtr, INT iItem)
169 HEADER_ITEM *lpItem = &infoPtr->items[iItem];
170 return lpItem->iOrder;
174 static INT
175 HEADER_OrderToIndex(const HEADER_INFO *infoPtr, INT iorder)
177 if ((iorder <0) || iorder >= infoPtr->uNumItem)
178 return iorder;
179 return infoPtr->order[iorder];
182 static void
183 HEADER_ChangeItemOrder(const HEADER_INFO *infoPtr, INT iItem, INT iNewOrder)
185 HEADER_ITEM *lpItem = &infoPtr->items[iItem];
186 INT i, nMin, nMax;
188 TRACE("%d: %d->%d\n", iItem, lpItem->iOrder, iNewOrder);
189 if (lpItem->iOrder < iNewOrder)
191 memmove(&infoPtr->order[lpItem->iOrder],
192 &infoPtr->order[lpItem->iOrder + 1],
193 (iNewOrder - lpItem->iOrder) * sizeof(INT));
195 if (iNewOrder < lpItem->iOrder)
197 memmove(&infoPtr->order[iNewOrder + 1],
198 &infoPtr->order[iNewOrder],
199 (lpItem->iOrder - iNewOrder) * sizeof(INT));
201 infoPtr->order[iNewOrder] = iItem;
202 nMin = min(lpItem->iOrder, iNewOrder);
203 nMax = max(lpItem->iOrder, iNewOrder);
204 for (i = nMin; i <= nMax; i++)
205 infoPtr->items[infoPtr->order[i]].iOrder = i;
208 /* Note: if iItem is the last item then this function returns infoPtr->uNumItem */
209 static INT
210 HEADER_NextItem(const HEADER_INFO *infoPtr, INT iItem)
212 return HEADER_OrderToIndex(infoPtr, HEADER_IndexToOrder(infoPtr, iItem)+1);
215 static INT
216 HEADER_PrevItem(const HEADER_INFO *infoPtr, INT iItem)
218 return HEADER_OrderToIndex(infoPtr, HEADER_IndexToOrder(infoPtr, iItem)-1);
221 /* TRUE when item is not resizable with dividers,
222 note that valid index should be supplied */
223 static inline BOOL
224 HEADER_IsItemFixed(const HEADER_INFO *infoPtr, INT iItem)
226 return (infoPtr->dwStyle & HDS_NOSIZING) || (infoPtr->items[iItem].fmt & HDF_FIXEDWIDTH);
229 static void
230 HEADER_SetItemBounds (HEADER_INFO *infoPtr)
232 HEADER_ITEM *phdi;
233 RECT rect;
234 unsigned int i;
235 int x;
237 infoPtr->bRectsValid = TRUE;
239 if (infoPtr->uNumItem == 0)
240 return;
242 GetClientRect (infoPtr->hwndSelf, &rect);
244 x = rect.left;
245 for (i = 0; i < infoPtr->uNumItem; i++) {
246 phdi = &infoPtr->items[HEADER_OrderToIndex(infoPtr,i)];
247 phdi->rect.top = rect.top;
248 phdi->rect.bottom = rect.bottom;
249 phdi->rect.left = x;
250 phdi->rect.right = phdi->rect.left + ((phdi->cxy>0)?phdi->cxy:0);
251 x = phdi->rect.right;
255 static LRESULT
256 HEADER_Size (HEADER_INFO *infoPtr)
258 HEADER_SetItemBounds(infoPtr);
259 return 0;
262 static void HEADER_GetHotDividerRect(const HEADER_INFO *infoPtr, RECT *r)
264 INT iDivider = infoPtr->iHotDivider;
265 if (infoPtr->uNumItem > 0)
267 HEADER_ITEM *lpItem;
269 if (iDivider < infoPtr->uNumItem)
271 lpItem = &infoPtr->items[iDivider];
272 r->left = lpItem->rect.left - HOT_DIVIDER_WIDTH/2;
273 r->right = lpItem->rect.left + HOT_DIVIDER_WIDTH/2;
275 else
277 lpItem = &infoPtr->items[HEADER_OrderToIndex(infoPtr, infoPtr->uNumItem-1)];
278 r->left = lpItem->rect.right - HOT_DIVIDER_WIDTH/2;
279 r->right = lpItem->rect.right + HOT_DIVIDER_WIDTH/2;
281 r->top = lpItem->rect.top;
282 r->bottom = lpItem->rect.bottom;
284 else
286 RECT clientRect;
287 GetClientRect(infoPtr->hwndSelf, &clientRect);
288 *r = clientRect;
289 r->right = r->left + HOT_DIVIDER_WIDTH/2;
293 static void
294 HEADER_FillItemFrame(HEADER_INFO *infoPtr, HDC hdc, RECT *r, const HEADER_ITEM *item, BOOL hottrack)
296 HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
298 if (theme) {
299 int state = (item->bDown) ? HIS_PRESSED : (hottrack ? HIS_HOT : HIS_NORMAL);
300 DrawThemeBackground (theme, hdc, HP_HEADERITEM, state, r, NULL);
301 GetThemeBackgroundContentRect (theme, hdc, HP_HEADERITEM, state, r, r);
303 else
305 HBRUSH hbr = CreateSolidBrush(GetBkColor(hdc));
306 FillRect(hdc, r, hbr);
307 DeleteObject(hbr);
311 static void
312 HEADER_DrawItemFrame(HEADER_INFO *infoPtr, HDC hdc, RECT *r, const HEADER_ITEM *item)
314 if (GetWindowTheme(infoPtr->hwndSelf)) return;
316 if (!(infoPtr->dwStyle & HDS_FLAT))
318 if (infoPtr->dwStyle & HDS_BUTTONS) {
319 if (item->bDown)
320 DrawEdge (hdc, r, BDR_RAISEDOUTER, BF_RECT | BF_FLAT | BF_ADJUST);
321 else
322 DrawEdge (hdc, r, EDGE_RAISED, BF_RECT | BF_SOFT | BF_ADJUST);
324 else
325 DrawEdge (hdc, r, EDGE_ETCHED, BF_BOTTOM | BF_RIGHT | BF_ADJUST);
329 /* Create a region for the sort arrow with its bounding rect's top-left
330 co-ord x,y and its height h. */
331 static HRGN create_sort_arrow( INT x, INT y, INT h, BOOL is_up )
333 char buffer[256];
334 RGNDATA *data = (RGNDATA *)buffer;
335 DWORD size = FIELD_OFFSET(RGNDATA, Buffer[h * sizeof(RECT)]);
336 INT i, yinc = 1;
337 HRGN rgn;
339 if (size > sizeof(buffer))
341 data = HeapAlloc( GetProcessHeap(), 0, size );
342 if (!data) return NULL;
344 data->rdh.dwSize = sizeof(data->rdh);
345 data->rdh.iType = RDH_RECTANGLES;
346 data->rdh.nCount = 0;
347 data->rdh.nRgnSize = h * sizeof(RECT);
349 if (!is_up)
351 y += h - 1;
352 yinc = -1;
355 x += h - 1; /* set x to the centre */
357 for (i = 0; i < h; i++, y += yinc)
359 RECT *rect = (RECT *)data->Buffer + data->rdh.nCount;
360 rect->left = x - i;
361 rect->top = y;
362 rect->right = x + i + 1;
363 rect->bottom = y + 1;
364 data->rdh.nCount++;
366 rgn = ExtCreateRegion( NULL, size, data );
367 if (data != (RGNDATA *)buffer) HeapFree( GetProcessHeap(), 0, data );
368 return rgn;
371 static INT
372 HEADER_DrawItem (HEADER_INFO *infoPtr, HDC hdc, INT iItem, BOOL bHotTrack, LRESULT lCDFlags)
374 HEADER_ITEM *phdi = &infoPtr->items[iItem];
375 RECT r;
376 INT oldBkMode;
377 HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
378 NMCUSTOMDRAW nmcd;
379 int state = 0;
381 TRACE("DrawItem(iItem %d bHotTrack %d unicode flag %d)\n", iItem, bHotTrack, (infoPtr->nNotifyFormat == NFR_UNICODE));
383 r = phdi->rect;
384 if (r.right - r.left == 0)
385 return phdi->rect.right;
387 if (theme)
388 state = (phdi->bDown) ? HIS_PRESSED : (bHotTrack ? HIS_HOT : HIS_NORMAL);
390 /* Set the colors before sending NM_CUSTOMDRAW so that it can change them */
391 SetTextColor(hdc, (bHotTrack && !theme) ? COLOR_HIGHLIGHT : COLOR_BTNTEXT);
392 SetBkColor(hdc, comctl32_color.clr3dFace);
394 if (lCDFlags & CDRF_NOTIFYITEMDRAW && !(phdi->fmt & HDF_OWNERDRAW))
396 LRESULT lCDItemFlags;
398 nmcd.dwDrawStage = CDDS_PREPAINT | CDDS_ITEM;
399 nmcd.hdc = hdc;
400 nmcd.dwItemSpec = iItem;
401 nmcd.rc = r;
402 nmcd.uItemState = phdi->bDown ? CDIS_SELECTED : 0;
403 nmcd.lItemlParam = phdi->lParam;
405 lCDItemFlags = HEADER_SendNotify(infoPtr, NM_CUSTOMDRAW, (NMHDR *)&nmcd);
406 if (lCDItemFlags & CDRF_SKIPDEFAULT)
407 return phdi->rect.right;
410 /* Fill background, owner could draw over it. */
411 HEADER_FillItemFrame(infoPtr, hdc, &r, phdi, bHotTrack);
413 if (phdi->fmt & HDF_OWNERDRAW)
415 DRAWITEMSTRUCT dis;
416 BOOL ret;
418 dis.CtlType = ODT_HEADER;
419 dis.CtlID = GetWindowLongPtrW (infoPtr->hwndSelf, GWLP_ID);
420 dis.itemID = iItem;
421 dis.itemAction = ODA_DRAWENTIRE;
422 dis.itemState = phdi->bDown ? ODS_SELECTED : 0;
423 dis.hwndItem = infoPtr->hwndSelf;
424 dis.hDC = hdc;
425 dis.rcItem = phdi->rect;
426 dis.itemData = phdi->lParam;
427 oldBkMode = SetBkMode(hdc, TRANSPARENT);
428 ret = SendMessageW (infoPtr->hwndNotify, WM_DRAWITEM, dis.CtlID, (LPARAM)&dis);
429 if (oldBkMode != TRANSPARENT)
430 SetBkMode(hdc, oldBkMode);
432 if (!ret)
433 HEADER_FillItemFrame(infoPtr, hdc, &r, phdi, bHotTrack);
435 /* Edges are always drawn if we don't have attached theme. */
436 HEADER_DrawItemFrame(infoPtr, hdc, &r, phdi);
437 /* If application processed WM_DRAWITEM we should skip label painting,
438 edges are drawn no matter what. */
439 if (ret) return phdi->rect.right;
441 else
442 HEADER_DrawItemFrame(infoPtr, hdc, &r, phdi);
444 if (phdi->bDown) {
445 r.left += 2;
446 r.top += 2;
449 /* Now text and image */
451 INT rw, rh; /* width and height of r */
452 INT *x = NULL; /* x and ... */
453 UINT *w = NULL; /* ... width of the pic (bmp or img) which is part of cnt */
454 /* cnt,txt,img,bmp */
455 INT cx, tx, ix, bx;
456 UINT cw, tw, iw, bw;
457 INT img_cx, img_cy;
458 INT sort_w, sort_x, sort_h;
459 BITMAP bmp;
461 HEADER_PrepareCallbackItems(infoPtr, iItem, HDI_TEXT|HDI_IMAGE);
462 cw = iw = bw = sort_w = sort_h = 0;
463 rw = r.right - r.left;
464 rh = r.bottom - r.top;
466 if (phdi->fmt & HDF_STRING) {
467 RECT textRect;
469 SetRectEmpty(&textRect);
471 if (theme) {
472 GetThemeTextExtent(theme, hdc, HP_HEADERITEM, state, phdi->pszText, -1,
473 DT_LEFT|DT_VCENTER|DT_SINGLELINE, NULL, &textRect);
474 } else {
475 DrawTextW (hdc, phdi->pszText, -1,
476 &textRect, DT_LEFT|DT_VCENTER|DT_SINGLELINE|DT_CALCRECT);
478 cw = textRect.right - textRect.left + 2 * infoPtr->iMargin;
481 if (phdi->fmt & (HDF_SORTUP | HDF_SORTDOWN)) {
482 sort_h = MulDiv( infoPtr->nHeight - VERT_BORDER, 4, 13 );
483 sort_w = 2 * sort_h - 1 + infoPtr->iMargin * 2;
484 cw += sort_w;
485 } else { /* sort arrows take precedent over images/bitmaps */
486 if ((phdi->fmt & HDF_IMAGE) && ImageList_GetIconSize( infoPtr->himl, &img_cx, &img_cy )) {
487 iw = img_cx + 2 * infoPtr->iMargin;
488 x = &ix;
489 w = &iw;
492 if ((phdi->fmt & HDF_BITMAP) && (phdi->hbm)) {
493 GetObjectW (phdi->hbm, sizeof(BITMAP), &bmp);
494 bw = bmp.bmWidth + 2 * infoPtr->iMargin;
495 if (!iw) {
496 x = &bx;
497 w = &bw;
500 if (bw || iw)
501 cw += *w;
504 /* align cx using the unclipped cw */
505 if ((phdi->fmt & HDF_JUSTIFYMASK) == HDF_LEFT)
506 cx = r.left;
507 else if ((phdi->fmt & HDF_JUSTIFYMASK) == HDF_CENTER)
508 cx = r.left + rw / 2 - cw / 2;
509 else /* HDF_RIGHT */
510 cx = r.right - cw;
512 /* clip cx & cw */
513 if (cx < r.left)
514 cx = r.left;
515 if (cx + cw > r.right)
516 cw = r.right - cx;
518 tx = cx + infoPtr->iMargin;
519 /* since cw might have changed we have to recalculate tw */
520 tw = cw - infoPtr->iMargin * 2;
522 tw -= sort_w;
523 sort_x = cx + tw + infoPtr->iMargin * 3;
525 if (iw || bw) {
526 tw -= *w;
527 if (phdi->fmt & HDF_BITMAP_ON_RIGHT) {
528 /* put pic behind text */
529 *x = cx + tw + infoPtr->iMargin * 3;
530 } else {
531 *x = cx + infoPtr->iMargin;
532 /* move text behind pic */
533 tx += *w;
537 if (iw && bw) {
538 /* since we're done with the layout we can
539 now calculate the position of bmp which
540 has no influence on alignment and layout
541 because of img */
542 if ((phdi->fmt & HDF_JUSTIFYMASK) == HDF_RIGHT)
543 bx = cx - bw + infoPtr->iMargin;
544 else
545 bx = cx + cw + infoPtr->iMargin;
548 if (sort_w || iw || bw) {
549 HDC hClipDC = GetDC(infoPtr->hwndSelf);
550 HRGN hClipRgn = CreateRectRgn(r.left, r.top, r.right, r.bottom);
551 SelectClipRgn(hClipDC, hClipRgn);
553 if (sort_w) {
554 HRGN arrow = create_sort_arrow( sort_x, r.top + (rh - sort_h) / 2,
555 sort_h, phdi->fmt & HDF_SORTUP );
556 if (arrow) {
557 FillRgn( hClipDC, arrow, GetSysColorBrush( COLOR_GRAYTEXT ) );
558 DeleteObject( arrow );
562 if (bw) {
563 HDC hdcBitmap = CreateCompatibleDC (hClipDC);
564 SelectObject (hdcBitmap, phdi->hbm);
565 BitBlt (hClipDC, bx, r.top + (rh - bmp.bmHeight) / 2,
566 bmp.bmWidth, bmp.bmHeight, hdcBitmap, 0, 0, SRCCOPY);
567 DeleteDC (hdcBitmap);
570 if (iw) {
571 ImageList_DrawEx (infoPtr->himl, phdi->iImage, hClipDC,
572 ix, r.top + (rh - img_cy) / 2,
573 img_cx, img_cy, CLR_DEFAULT, CLR_DEFAULT, 0);
576 DeleteObject(hClipRgn);
577 ReleaseDC(infoPtr->hwndSelf, hClipDC);
580 if (((phdi->fmt & HDF_STRING)
581 || (!(phdi->fmt & (HDF_OWNERDRAW|HDF_STRING|HDF_BITMAP|
582 HDF_BITMAP_ON_RIGHT|HDF_IMAGE)))) /* no explicit format specified? */
583 && (phdi->pszText)) {
584 oldBkMode = SetBkMode(hdc, TRANSPARENT);
585 r.left = tx;
586 r.right = tx + tw;
587 if (theme) {
588 DrawThemeText(theme, hdc, HP_HEADERITEM, state, phdi->pszText,
589 -1, DT_LEFT|DT_END_ELLIPSIS|DT_VCENTER|DT_SINGLELINE,
590 0, &r);
591 } else {
592 DrawTextW (hdc, phdi->pszText, -1,
593 &r, DT_LEFT|DT_END_ELLIPSIS|DT_VCENTER|DT_SINGLELINE);
595 if (oldBkMode != TRANSPARENT)
596 SetBkMode(hdc, oldBkMode);
598 HEADER_FreeCallbackItems(phdi);
601 return phdi->rect.right;
604 static void
605 HEADER_DrawHotDivider(const HEADER_INFO *infoPtr, HDC hdc)
607 HBRUSH brush;
608 RECT r;
610 HEADER_GetHotDividerRect(infoPtr, &r);
611 brush = CreateSolidBrush(comctl32_color.clrHighlight);
612 FillRect(hdc, &r, brush);
613 DeleteObject(brush);
616 static void
617 HEADER_Refresh (HEADER_INFO *infoPtr, HDC hdc)
619 HFONT hFont, hOldFont;
620 RECT rect, rcRest;
621 HBRUSH hbrBk;
622 UINT i;
623 INT x;
624 LRESULT lCDFlags;
625 HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
627 if (!infoPtr->bRectsValid)
628 HEADER_SetItemBounds(infoPtr);
630 /* get rect for the bar, adjusted for the border */
631 GetClientRect (infoPtr->hwndSelf, &rect);
632 lCDFlags = HEADER_SendCtrlCustomDraw(infoPtr, CDDS_PREPAINT, hdc, &rect);
634 if (infoPtr->bDragging)
635 ImageList_DragShowNolock(FALSE);
637 hFont = infoPtr->hFont ? infoPtr->hFont : GetStockObject (SYSTEM_FONT);
638 hOldFont = SelectObject (hdc, hFont);
640 /* draw Background */
641 if (infoPtr->uNumItem == 0 && theme == NULL) {
642 hbrBk = GetSysColorBrush(COLOR_3DFACE);
643 FillRect(hdc, &rect, hbrBk);
646 x = rect.left;
647 for (i = 0; x <= rect.right && i < infoPtr->uNumItem; i++) {
648 int idx = HEADER_OrderToIndex(infoPtr,i);
649 if (RectVisible(hdc, &infoPtr->items[idx].rect))
650 HEADER_DrawItem(infoPtr, hdc, idx, infoPtr->iHotItem == idx, lCDFlags);
651 x = infoPtr->items[idx].rect.right;
654 rcRest = rect;
655 rcRest.left = x;
656 if ((x <= rect.right) && RectVisible(hdc, &rcRest) && (infoPtr->uNumItem > 0)) {
657 if (theme != NULL) {
658 DrawThemeBackground(theme, hdc, HP_HEADERITEM, HIS_NORMAL, &rcRest, NULL);
660 else if (infoPtr->dwStyle & HDS_FLAT) {
661 hbrBk = GetSysColorBrush(COLOR_3DFACE);
662 FillRect(hdc, &rcRest, hbrBk);
664 else
666 if (infoPtr->dwStyle & HDS_BUTTONS)
667 DrawEdge (hdc, &rcRest, EDGE_RAISED, BF_TOP|BF_LEFT|BF_BOTTOM|BF_SOFT|BF_MIDDLE);
668 else
669 DrawEdge (hdc, &rcRest, EDGE_ETCHED, BF_BOTTOM|BF_MIDDLE);
673 if (infoPtr->iHotDivider != -1)
674 HEADER_DrawHotDivider(infoPtr, hdc);
676 if (infoPtr->bDragging)
677 ImageList_DragShowNolock(TRUE);
678 SelectObject (hdc, hOldFont);
680 if (lCDFlags & CDRF_NOTIFYPOSTPAINT)
681 HEADER_SendCtrlCustomDraw(infoPtr, CDDS_POSTPAINT, hdc, &rect);
685 static void
686 HEADER_RefreshItem (HEADER_INFO *infoPtr, INT iItem)
688 if (!infoPtr->bRectsValid)
689 HEADER_SetItemBounds(infoPtr);
691 InvalidateRect(infoPtr->hwndSelf, &infoPtr->items[iItem].rect, FALSE);
695 static void
696 HEADER_InternalHitTest (const HEADER_INFO *infoPtr, const POINT *lpPt, UINT *pFlags, INT *pItem)
698 RECT rect, rcTest;
699 UINT iCount;
700 INT width;
701 BOOL bNoWidth;
703 GetClientRect (infoPtr->hwndSelf, &rect);
705 *pFlags = 0;
706 bNoWidth = FALSE;
707 if (PtInRect (&rect, *lpPt))
709 if (infoPtr->uNumItem == 0) {
710 *pFlags |= HHT_NOWHERE;
711 *pItem = 1;
712 TRACE("NOWHERE\n");
713 return;
715 else {
716 /* somewhere inside */
717 for (iCount = 0; iCount < infoPtr->uNumItem; iCount++) {
718 rect = infoPtr->items[iCount].rect;
719 width = rect.right - rect.left;
720 if (width == 0) {
721 bNoWidth = TRUE;
722 continue;
724 if (PtInRect (&rect, *lpPt)) {
725 if (width <= 2 * DIVIDER_WIDTH) {
726 *pFlags |= HHT_ONHEADER;
727 *pItem = iCount;
728 TRACE("ON HEADER %d\n", iCount);
729 return;
731 if (HEADER_IndexToOrder(infoPtr, iCount) > 0) {
732 rcTest = rect;
733 rcTest.right = rcTest.left + DIVIDER_WIDTH;
734 if (PtInRect (&rcTest, *lpPt)) {
735 if (HEADER_IsItemFixed(infoPtr, HEADER_PrevItem(infoPtr, iCount)))
737 *pFlags |= HHT_ONHEADER;
738 *pItem = iCount;
739 TRACE("ON HEADER %d\n", *pItem);
740 return;
742 if (bNoWidth) {
743 *pFlags |= HHT_ONDIVOPEN;
744 *pItem = HEADER_PrevItem(infoPtr, iCount);
745 TRACE("ON DIVOPEN %d\n", *pItem);
746 return;
748 else {
749 *pFlags |= HHT_ONDIVIDER;
750 *pItem = HEADER_PrevItem(infoPtr, iCount);
751 TRACE("ON DIVIDER %d\n", *pItem);
752 return;
756 rcTest = rect;
757 rcTest.left = rcTest.right - DIVIDER_WIDTH;
758 if (!HEADER_IsItemFixed(infoPtr, iCount) && PtInRect (&rcTest, *lpPt))
760 *pFlags |= HHT_ONDIVIDER;
761 *pItem = iCount;
762 TRACE("ON DIVIDER %d\n", *pItem);
763 return;
766 *pFlags |= HHT_ONHEADER;
767 *pItem = iCount;
768 TRACE("ON HEADER %d\n", iCount);
769 return;
773 /* check for last divider part (on nowhere) */
774 if (!HEADER_IsItemFixed(infoPtr, infoPtr->uNumItem - 1))
776 rect = infoPtr->items[infoPtr->uNumItem-1].rect;
777 rect.left = rect.right;
778 rect.right += DIVIDER_WIDTH;
779 if (PtInRect (&rect, *lpPt)) {
780 if (bNoWidth) {
781 *pFlags |= HHT_ONDIVOPEN;
782 *pItem = infoPtr->uNumItem - 1;
783 TRACE("ON DIVOPEN %d\n", *pItem);
784 return;
786 else {
787 *pFlags |= HHT_ONDIVIDER;
788 *pItem = infoPtr->uNumItem - 1;
789 TRACE("ON DIVIDER %d\n", *pItem);
790 return;
795 *pFlags |= HHT_NOWHERE;
796 *pItem = 1;
797 TRACE("NOWHERE\n");
798 return;
801 else {
802 if (lpPt->x < rect.left) {
803 TRACE("TO LEFT\n");
804 *pFlags |= HHT_TOLEFT;
806 else if (lpPt->x > rect.right) {
807 TRACE("TO RIGHT\n");
808 *pFlags |= HHT_TORIGHT;
811 if (lpPt->y < rect.top) {
812 TRACE("ABOVE\n");
813 *pFlags |= HHT_ABOVE;
815 else if (lpPt->y > rect.bottom) {
816 TRACE("BELOW\n");
817 *pFlags |= HHT_BELOW;
821 *pItem = 1;
822 TRACE("flags=0x%X\n", *pFlags);
823 return;
827 static void
828 HEADER_DrawTrackLine (const HEADER_INFO *infoPtr, HDC hdc, INT x)
830 RECT rect;
832 GetClientRect (infoPtr->hwndSelf, &rect);
833 PatBlt( hdc, x, rect.top, 1, rect.bottom - rect.top, DSTINVERT );
836 /***
837 * DESCRIPTION:
838 * Convert a HDITEM into the correct format (ANSI/Unicode) to send it in a notify
840 * PARAMETER(S):
841 * [I] infoPtr : the header that wants to send the notify
842 * [O] dest : The buffer to store the HDITEM for notify. It may be set to a HDITEMA of HDITEMW
843 * [I] src : The source HDITEM. It may be a HDITEMA or HDITEMW
844 * [I] fSourceUnicode : is src a HDITEMW or HDITEMA
845 * [O] ppvScratch : a pointer to a scratch buffer that needs to be freed after
846 * the HDITEM is no longer in use or NULL if none was needed
848 * NOTE: We depend on HDITEMA and HDITEMW having the same structure
850 static void HEADER_CopyHDItemForNotify(const HEADER_INFO *infoPtr, HDITEMW *dest,
851 const HDITEMW *src, BOOL fSourceUnicode, LPVOID *ppvScratch)
853 *ppvScratch = NULL;
854 *dest = *src;
856 if (src->mask & HDI_TEXT && src->pszText != LPSTR_TEXTCALLBACKW) /* covers TEXTCALLBACKA as well */
858 if (fSourceUnicode && infoPtr->nNotifyFormat != NFR_UNICODE)
860 dest->pszText = NULL;
861 Str_SetPtrWtoA((LPSTR *)&dest->pszText, src->pszText);
862 *ppvScratch = dest->pszText;
865 if (!fSourceUnicode && infoPtr->nNotifyFormat == NFR_UNICODE)
867 dest->pszText = NULL;
868 Str_SetPtrAtoW(&dest->pszText, (LPSTR)src->pszText);
869 *ppvScratch = dest->pszText;
874 static UINT HEADER_NotifyCodeWtoA(UINT code)
876 /* we use the fact that all the unicode messages are in HDN_FIRST_UNICODE..HDN_LAST*/
877 if (code >= HDN_LAST && code <= HDN_FIRST_UNICODE)
878 return code + HDN_UNICODE_OFFSET;
879 else
880 return code;
883 static LRESULT
884 HEADER_SendNotify(const HEADER_INFO *infoPtr, UINT code, NMHDR *nmhdr)
886 nmhdr->hwndFrom = infoPtr->hwndSelf;
887 nmhdr->idFrom = GetWindowLongPtrW (infoPtr->hwndSelf, GWLP_ID);
888 nmhdr->code = code;
890 return SendMessageW(infoPtr->hwndNotify, WM_NOTIFY,
891 nmhdr->idFrom, (LPARAM)nmhdr);
894 static BOOL
895 HEADER_SendSimpleNotify (const HEADER_INFO *infoPtr, UINT code)
897 NMHDR nmhdr;
898 return (BOOL)HEADER_SendNotify(infoPtr, code, &nmhdr);
901 static LRESULT
902 HEADER_SendCtrlCustomDraw(const HEADER_INFO *infoPtr, DWORD dwDrawStage, HDC hdc, const RECT *rect)
904 NMCUSTOMDRAW nm;
905 nm.dwDrawStage = dwDrawStage;
906 nm.hdc = hdc;
907 nm.rc = *rect;
908 nm.dwItemSpec = 0;
909 nm.uItemState = 0;
910 nm.lItemlParam = 0;
912 return HEADER_SendNotify(infoPtr, NM_CUSTOMDRAW, (NMHDR *)&nm);
915 static BOOL
916 HEADER_SendNotifyWithHDItemT(const HEADER_INFO *infoPtr, UINT code, INT iItem, HDITEMW *lpItem)
918 NMHEADERW nmhdr;
920 if (infoPtr->nNotifyFormat != NFR_UNICODE)
921 code = HEADER_NotifyCodeWtoA(code);
922 nmhdr.iItem = iItem;
923 nmhdr.iButton = 0;
924 nmhdr.pitem = lpItem;
926 return (BOOL)HEADER_SendNotify(infoPtr, code, (NMHDR *)&nmhdr);
929 static BOOL
930 HEADER_SendNotifyWithIntFieldT(const HEADER_INFO *infoPtr, UINT code, INT iItem, INT mask, INT iValue)
932 HDITEMW nmitem;
934 /* copying only the iValue should be ok but to make the code more robust we copy everything */
935 nmitem.cxy = infoPtr->items[iItem].cxy;
936 nmitem.hbm = infoPtr->items[iItem].hbm;
937 nmitem.pszText = NULL;
938 nmitem.cchTextMax = 0;
939 nmitem.fmt = infoPtr->items[iItem].fmt;
940 nmitem.lParam = infoPtr->items[iItem].lParam;
941 nmitem.iOrder = infoPtr->items[iItem].iOrder;
942 nmitem.iImage = infoPtr->items[iItem].iImage;
944 nmitem.mask = mask;
945 switch (mask)
947 case HDI_WIDTH:
948 nmitem.cxy = iValue;
949 break;
950 case HDI_ORDER:
951 nmitem.iOrder = iValue;
952 break;
953 default:
954 ERR("invalid mask value 0x%x\n", iValue);
957 return HEADER_SendNotifyWithHDItemT(infoPtr, code, iItem, &nmitem);
961 * Prepare callback items
962 * depends on NMHDDISPINFOW having same structure as NMHDDISPINFOA
963 * (so we handle the two cases only doing a specific cast for pszText).
964 * Checks if any of the required fields is a callback. If this is the case sends a
965 * NMHDISPINFO notify to retrieve these items. The items are stored in the
966 * HEADER_ITEM pszText and iImage fields. They should be freed with
967 * HEADER_FreeCallbackItems.
969 * @param hwnd : hwnd header container handler
970 * @param iItem : the header item id
971 * @param reqMask : required fields. If any of them is callback this function will fetch it
973 * @return TRUE on success, else FALSE
975 static BOOL
976 HEADER_PrepareCallbackItems(const HEADER_INFO *infoPtr, INT iItem, INT reqMask)
978 HEADER_ITEM *lpItem = &infoPtr->items[iItem];
979 DWORD mask = reqMask & lpItem->callbackMask;
980 NMHDDISPINFOW dispInfo;
981 void *pvBuffer = NULL;
983 if (mask == 0)
984 return TRUE;
985 if (mask&HDI_TEXT && lpItem->pszText != NULL)
987 ERR("(): function called without a call to FreeCallbackItems\n");
988 Free(lpItem->pszText);
989 lpItem->pszText = NULL;
992 memset(&dispInfo, 0, sizeof(NMHDDISPINFOW));
993 dispInfo.hdr.hwndFrom = infoPtr->hwndSelf;
994 dispInfo.hdr.idFrom = GetWindowLongPtrW (infoPtr->hwndSelf, GWLP_ID);
995 if (infoPtr->nNotifyFormat == NFR_UNICODE)
997 dispInfo.hdr.code = HDN_GETDISPINFOW;
998 if (mask & HDI_TEXT)
999 pvBuffer = Alloc(MAX_HEADER_TEXT_LEN * sizeof(WCHAR));
1001 else
1003 dispInfo.hdr.code = HDN_GETDISPINFOA;
1004 if (mask & HDI_TEXT)
1005 pvBuffer = Alloc(MAX_HEADER_TEXT_LEN * sizeof(CHAR));
1007 dispInfo.pszText = pvBuffer;
1008 dispInfo.cchTextMax = (pvBuffer!=NULL?MAX_HEADER_TEXT_LEN:0);
1009 dispInfo.iItem = iItem;
1010 dispInfo.mask = mask;
1011 dispInfo.lParam = lpItem->lParam;
1013 TRACE("Sending HDN_GETDISPINFO%c\n", infoPtr->nNotifyFormat == NFR_UNICODE?'W':'A');
1014 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, dispInfo.hdr.idFrom, (LPARAM)&dispInfo);
1016 TRACE("SendMessage returns(mask:0x%x,str:%s,lParam:%p)\n",
1017 dispInfo.mask,
1018 (infoPtr->nNotifyFormat == NFR_UNICODE ? debugstr_w(dispInfo.pszText) : (LPSTR) dispInfo.pszText),
1019 (void*) dispInfo.lParam);
1021 if (mask & HDI_IMAGE)
1022 lpItem->iImage = dispInfo.iImage;
1023 if (mask & HDI_TEXT)
1025 if (infoPtr->nNotifyFormat == NFR_UNICODE)
1027 lpItem->pszText = pvBuffer;
1029 /* the user might have used his own buffer */
1030 if (dispInfo.pszText != lpItem->pszText)
1031 Str_GetPtrW(dispInfo.pszText, lpItem->pszText, MAX_HEADER_TEXT_LEN);
1033 else
1035 Str_SetPtrAtoW(&lpItem->pszText, (LPSTR)dispInfo.pszText);
1036 Free(pvBuffer);
1040 if (dispInfo.mask & HDI_DI_SETITEM)
1042 /* make the items permanent */
1043 lpItem->callbackMask &= ~dispInfo.mask;
1046 return TRUE;
1049 /***
1050 * DESCRIPTION:
1051 * Free the items that might be allocated with HEADER_PrepareCallbackItems
1053 * PARAMETER(S):
1054 * [I] lpItem : the item to free the data
1057 static void
1058 HEADER_FreeCallbackItems(HEADER_ITEM *lpItem)
1060 if (lpItem->callbackMask&HDI_TEXT)
1062 Free(lpItem->pszText);
1063 lpItem->pszText = NULL;
1066 if (lpItem->callbackMask&HDI_IMAGE)
1067 lpItem->iImage = I_IMAGECALLBACK;
1070 static HIMAGELIST
1071 HEADER_CreateDragImage (HEADER_INFO *infoPtr, INT iItem)
1073 HEADER_ITEM *lpItem;
1074 HIMAGELIST himl;
1075 HBITMAP hMemory, hOldBitmap;
1076 LRESULT lCDFlags;
1077 RECT rc;
1078 HDC hMemoryDC;
1079 HDC hDeviceDC;
1080 int height, width;
1081 HFONT hFont;
1083 if (iItem >= infoPtr->uNumItem)
1084 return NULL;
1086 if (!infoPtr->bRectsValid)
1087 HEADER_SetItemBounds(infoPtr);
1089 lpItem = &infoPtr->items[iItem];
1090 width = lpItem->rect.right - lpItem->rect.left;
1091 height = lpItem->rect.bottom - lpItem->rect.top;
1093 hDeviceDC = GetDC(NULL);
1094 hMemoryDC = CreateCompatibleDC(hDeviceDC);
1095 hMemory = CreateCompatibleBitmap(hDeviceDC, width, height);
1096 ReleaseDC(NULL, hDeviceDC);
1097 hOldBitmap = SelectObject(hMemoryDC, hMemory);
1098 SetViewportOrgEx(hMemoryDC, -lpItem->rect.left, -lpItem->rect.top, NULL);
1099 hFont = infoPtr->hFont ? infoPtr->hFont : GetStockObject(SYSTEM_FONT);
1100 SelectObject(hMemoryDC, hFont);
1102 GetClientRect(infoPtr->hwndSelf, &rc);
1103 lCDFlags = HEADER_SendCtrlCustomDraw(infoPtr, CDDS_PREPAINT, hMemoryDC, &rc);
1104 HEADER_DrawItem(infoPtr, hMemoryDC, iItem, FALSE, lCDFlags);
1105 if (lCDFlags & CDRF_NOTIFYPOSTPAINT)
1106 HEADER_SendCtrlCustomDraw(infoPtr, CDDS_POSTPAINT, hMemoryDC, &rc);
1108 hMemory = SelectObject(hMemoryDC, hOldBitmap);
1109 DeleteDC(hMemoryDC);
1111 if (hMemory == NULL) /* if anything failed */
1112 return NULL;
1114 himl = ImageList_Create(width, height, ILC_COLORDDB, 1, 1);
1115 ImageList_Add(himl, hMemory, NULL);
1116 DeleteObject(hMemory);
1117 return himl;
1120 static LRESULT
1121 HEADER_SetHotDivider(HEADER_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
1123 INT iDivider;
1124 RECT r;
1126 if (wParam)
1128 POINT pt;
1129 UINT flags;
1130 pt.x = (INT)(SHORT)LOWORD(lParam);
1131 pt.y = 0;
1132 HEADER_InternalHitTest (infoPtr, &pt, &flags, &iDivider);
1134 if (flags & HHT_TOLEFT)
1135 iDivider = 0;
1136 else if (flags & HHT_NOWHERE || flags & HHT_TORIGHT)
1137 iDivider = infoPtr->uNumItem;
1138 else
1140 HEADER_ITEM *lpItem = &infoPtr->items[iDivider];
1141 if (pt.x > (lpItem->rect.left+lpItem->rect.right)/2)
1142 iDivider = HEADER_NextItem(infoPtr, iDivider);
1145 else
1146 iDivider = (INT)lParam;
1148 /* Note; wParam==FALSE, lParam==-1 is valid and is used to clear the hot divider */
1149 if (iDivider<-1 || iDivider>(int)infoPtr->uNumItem)
1150 return iDivider;
1152 if (iDivider != infoPtr->iHotDivider)
1154 if (infoPtr->iHotDivider != -1)
1156 HEADER_GetHotDividerRect(infoPtr, &r);
1157 InvalidateRect(infoPtr->hwndSelf, &r, FALSE);
1159 infoPtr->iHotDivider = iDivider;
1160 if (iDivider != -1)
1162 HEADER_GetHotDividerRect(infoPtr, &r);
1163 InvalidateRect(infoPtr->hwndSelf, &r, FALSE);
1166 return iDivider;
1169 static LRESULT
1170 HEADER_DeleteItem (HEADER_INFO *infoPtr, INT iItem)
1172 INT iOrder;
1173 UINT i;
1175 TRACE("[iItem=%d]\n", iItem);
1177 if ((iItem < 0) || (iItem >= (INT)infoPtr->uNumItem))
1178 return FALSE;
1180 for (i = 0; i < infoPtr->uNumItem; i++)
1181 TRACE("%d: order=%d, iOrder=%d, ->iOrder=%d\n", i, infoPtr->order[i], infoPtr->items[i].iOrder, infoPtr->items[infoPtr->order[i]].iOrder);
1183 iOrder = infoPtr->items[iItem].iOrder;
1184 Free(infoPtr->items[iItem].pszText);
1186 infoPtr->uNumItem--;
1187 memmove(&infoPtr->items[iItem], &infoPtr->items[iItem + 1],
1188 (infoPtr->uNumItem - iItem) * sizeof(HEADER_ITEM));
1189 memmove(&infoPtr->order[iOrder], &infoPtr->order[iOrder + 1],
1190 (infoPtr->uNumItem - iOrder) * sizeof(INT));
1191 infoPtr->items = ReAlloc(infoPtr->items, sizeof(HEADER_ITEM) * infoPtr->uNumItem);
1192 infoPtr->order = ReAlloc(infoPtr->order, sizeof(INT) * infoPtr->uNumItem);
1194 /* Correct the orders */
1195 for (i = 0; i < infoPtr->uNumItem; i++)
1197 if (infoPtr->order[i] > iItem)
1198 infoPtr->order[i]--;
1199 if (i >= iOrder)
1200 infoPtr->items[infoPtr->order[i]].iOrder = i;
1202 for (i = 0; i < infoPtr->uNumItem; i++)
1203 TRACE("%d: order=%d, iOrder=%d, ->iOrder=%d\n", i, infoPtr->order[i], infoPtr->items[i].iOrder, infoPtr->items[infoPtr->order[i]].iOrder);
1205 HEADER_SetItemBounds (infoPtr);
1206 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1208 return TRUE;
1212 static LRESULT
1213 HEADER_GetImageList (const HEADER_INFO *infoPtr)
1215 return (LRESULT)infoPtr->himl;
1219 static LRESULT
1220 HEADER_GetItemT (const HEADER_INFO *infoPtr, INT nItem, LPHDITEMW phdi, BOOL bUnicode)
1222 HEADER_ITEM *lpItem;
1223 UINT mask;
1225 if (!phdi)
1226 return FALSE;
1228 TRACE("[nItem=%d]\n", nItem);
1230 mask = phdi->mask;
1231 if (mask == 0)
1232 return TRUE;
1234 if ((nItem < 0) || (nItem >= (INT)infoPtr->uNumItem))
1235 return FALSE;
1237 if (mask & HDI_UNKNOWN_FIELDS)
1239 TRACE("mask %x contains unknown fields. Using only comctl32 4.0 fields\n", mask);
1240 mask &= HDI_COMCTL32_4_0_FIELDS;
1243 lpItem = &infoPtr->items[nItem];
1244 HEADER_PrepareCallbackItems(infoPtr, nItem, mask);
1246 if (mask & HDI_BITMAP)
1247 phdi->hbm = lpItem->hbm;
1249 if (mask & HDI_FORMAT)
1250 phdi->fmt = lpItem->fmt;
1252 if (mask & HDI_WIDTH)
1253 phdi->cxy = lpItem->cxy;
1255 if (mask & HDI_LPARAM)
1256 phdi->lParam = lpItem->lParam;
1258 if (mask & HDI_IMAGE)
1259 phdi->iImage = lpItem->iImage;
1261 if (mask & HDI_ORDER)
1262 phdi->iOrder = lpItem->iOrder;
1264 if (mask & HDI_TEXT)
1266 if (bUnicode)
1267 Str_GetPtrW (lpItem->pszText, phdi->pszText, phdi->cchTextMax);
1268 else
1269 Str_GetPtrWtoA (lpItem->pszText, (LPSTR)phdi->pszText, phdi->cchTextMax);
1272 HEADER_FreeCallbackItems(lpItem);
1273 return TRUE;
1277 static inline LRESULT
1278 HEADER_GetItemCount (const HEADER_INFO *infoPtr)
1280 return infoPtr->uNumItem;
1284 static LRESULT
1285 HEADER_GetItemRect (const HEADER_INFO *infoPtr, INT iItem, LPRECT lpRect)
1287 if ((iItem < 0) || (iItem >= (INT)infoPtr->uNumItem))
1288 return FALSE;
1290 lpRect->left = infoPtr->items[iItem].rect.left;
1291 lpRect->right = infoPtr->items[iItem].rect.right;
1292 lpRect->top = infoPtr->items[iItem].rect.top;
1293 lpRect->bottom = infoPtr->items[iItem].rect.bottom;
1295 return TRUE;
1299 static LRESULT
1300 HEADER_GetOrderArray(const HEADER_INFO *infoPtr, INT size, LPINT order)
1302 if ((UINT)size <infoPtr->uNumItem)
1303 return FALSE;
1305 memcpy(order, infoPtr->order, infoPtr->uNumItem * sizeof(INT));
1306 return TRUE;
1309 /* Returns index of first duplicate 'value' from [0,to) range,
1310 or -1 if there isn't any */
1311 static INT has_duplicate(const INT *array, INT to, INT value)
1313 INT i;
1314 for(i = 0; i < to; i++)
1315 if (array[i] == value) return i;
1316 return -1;
1319 /* returns next available value from [0,max] not to duplicate in [0,to) */
1320 static INT get_nextvalue(const INT *array, INT to, INT max)
1322 INT i;
1323 for(i = 0; i < max; i++)
1324 if (has_duplicate(array, to, i) == -1) return i;
1325 return 0;
1328 static LRESULT
1329 HEADER_SetOrderArray(HEADER_INFO *infoPtr, INT size, const INT *order)
1331 HEADER_ITEM *lpItem;
1332 INT i;
1334 if ((UINT)size != infoPtr->uNumItem)
1335 return FALSE;
1337 if (TRACE_ON(header))
1339 TRACE("count=%d, order array={", size);
1340 for (i = 0; i < size; i++)
1341 TRACE("%d%c", order[i], i != size-1 ? ',' : '}');
1342 TRACE("\n");
1345 for (i=0; i<size; i++)
1347 if (order[i] >= size || order[i] < 0)
1348 /* on invalid index get next available */
1349 /* FIXME: if i==0 array item is out of range behaviour is
1350 different, see tests */
1351 infoPtr->order[i] = get_nextvalue(infoPtr->order, i, size);
1352 else
1354 INT j, dup;
1356 infoPtr->order[i] = order[i];
1357 j = i;
1358 /* remove duplicates */
1359 while ((dup = has_duplicate(infoPtr->order, j, order[j])) != -1)
1361 INT next;
1363 next = get_nextvalue(infoPtr->order, j, size);
1364 infoPtr->order[dup] = next;
1365 j--;
1369 /* sync with item data */
1370 for (i=0; i<size; i++)
1372 lpItem = &infoPtr->items[infoPtr->order[i]];
1373 lpItem->iOrder = i;
1375 HEADER_SetItemBounds(infoPtr);
1376 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1377 return TRUE;
1380 static inline LRESULT
1381 HEADER_GetUnicodeFormat (const HEADER_INFO *infoPtr)
1383 return (infoPtr->nNotifyFormat == NFR_UNICODE);
1387 static LRESULT
1388 HEADER_HitTest (const HEADER_INFO *infoPtr, LPHDHITTESTINFO phti)
1390 UINT outside = HHT_NOWHERE | HHT_ABOVE | HHT_BELOW | HHT_TOLEFT | HHT_TORIGHT;
1392 HEADER_InternalHitTest (infoPtr, &phti->pt, &phti->flags, &phti->iItem);
1394 if (phti->flags & outside)
1395 return phti->iItem = -1;
1396 else
1397 return phti->iItem;
1401 static LRESULT
1402 HEADER_InsertItemT (HEADER_INFO *infoPtr, INT nItem, const HDITEMW *phdi, BOOL bUnicode)
1404 HEADER_ITEM *lpItem;
1405 INT iOrder;
1406 UINT i;
1407 UINT copyMask;
1409 if ((phdi == NULL) || (nItem < 0) || (phdi->mask == 0))
1410 return -1;
1412 if (nItem > infoPtr->uNumItem)
1413 nItem = infoPtr->uNumItem;
1415 iOrder = (phdi->mask & HDI_ORDER) ? phdi->iOrder : nItem;
1416 if (iOrder < 0)
1417 iOrder = 0;
1418 else if (infoPtr->uNumItem < iOrder)
1419 iOrder = infoPtr->uNumItem;
1421 infoPtr->uNumItem++;
1422 infoPtr->items = ReAlloc(infoPtr->items, sizeof(HEADER_ITEM) * infoPtr->uNumItem);
1423 infoPtr->order = ReAlloc(infoPtr->order, sizeof(INT) * infoPtr->uNumItem);
1425 /* make space for the new item */
1426 memmove(&infoPtr->items[nItem + 1], &infoPtr->items[nItem],
1427 (infoPtr->uNumItem - nItem - 1) * sizeof(HEADER_ITEM));
1428 memmove(&infoPtr->order[iOrder + 1], &infoPtr->order[iOrder],
1429 (infoPtr->uNumItem - iOrder - 1) * sizeof(INT));
1431 /* update the order array */
1432 infoPtr->order[iOrder] = nItem;
1433 for (i = 0; i < infoPtr->uNumItem; i++)
1435 if (i != iOrder && infoPtr->order[i] >= nItem)
1436 infoPtr->order[i]++;
1437 infoPtr->items[infoPtr->order[i]].iOrder = i;
1440 lpItem = &infoPtr->items[nItem];
1441 ZeroMemory(lpItem, sizeof(HEADER_ITEM));
1442 /* cxy, fmt and lParam are copied even if not in the HDITEM mask */
1443 copyMask = phdi->mask | HDI_WIDTH | HDI_FORMAT | HDI_LPARAM;
1444 HEADER_StoreHDItemInHeader(lpItem, copyMask, phdi, bUnicode);
1445 lpItem->iOrder = iOrder;
1447 /* set automatically some format bits */
1448 if (phdi->mask & HDI_TEXT)
1449 lpItem->fmt |= HDF_STRING;
1450 else
1451 lpItem->fmt &= ~HDF_STRING;
1453 if (lpItem->hbm != NULL)
1454 lpItem->fmt |= HDF_BITMAP;
1455 else
1456 lpItem->fmt &= ~HDF_BITMAP;
1458 if (phdi->mask & HDI_IMAGE)
1459 lpItem->fmt |= HDF_IMAGE;
1461 HEADER_SetItemBounds (infoPtr);
1462 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1464 return nItem;
1468 static LRESULT
1469 HEADER_Layout (HEADER_INFO *infoPtr, LPHDLAYOUT lpLayout)
1471 lpLayout->pwpos->hwnd = infoPtr->hwndSelf;
1472 lpLayout->pwpos->hwndInsertAfter = 0;
1473 lpLayout->pwpos->x = lpLayout->prc->left;
1474 lpLayout->pwpos->y = lpLayout->prc->top;
1475 lpLayout->pwpos->cx = lpLayout->prc->right - lpLayout->prc->left;
1476 if (infoPtr->dwStyle & HDS_HIDDEN)
1477 lpLayout->pwpos->cy = 0;
1478 else {
1479 lpLayout->pwpos->cy = infoPtr->nHeight;
1480 lpLayout->prc->top += infoPtr->nHeight;
1482 lpLayout->pwpos->flags = SWP_NOZORDER;
1484 TRACE("Layout x=%d y=%d cx=%d cy=%d\n",
1485 lpLayout->pwpos->x, lpLayout->pwpos->y,
1486 lpLayout->pwpos->cx, lpLayout->pwpos->cy);
1488 infoPtr->bRectsValid = FALSE;
1490 return TRUE;
1494 static LRESULT
1495 HEADER_SetImageList (HEADER_INFO *infoPtr, HIMAGELIST himl)
1497 HIMAGELIST himlOld;
1499 TRACE("(himl %p)\n", himl);
1500 himlOld = infoPtr->himl;
1501 infoPtr->himl = himl;
1503 /* FIXME: Refresh needed??? */
1505 return (LRESULT)himlOld;
1509 static LRESULT
1510 HEADER_GetBitmapMargin(const HEADER_INFO *infoPtr)
1512 return infoPtr->iMargin;
1515 static LRESULT
1516 HEADER_SetBitmapMargin(HEADER_INFO *infoPtr, INT iMargin)
1518 INT oldMargin = infoPtr->iMargin;
1520 infoPtr->iMargin = iMargin;
1522 return oldMargin;
1525 static LRESULT
1526 HEADER_SetItemT (HEADER_INFO *infoPtr, INT nItem, const HDITEMW *phdi, BOOL bUnicode)
1528 HEADER_ITEM *lpItem;
1529 HDITEMW hdNotify;
1530 void *pvScratch;
1532 if (phdi == NULL)
1533 return FALSE;
1534 if ((nItem < 0) || (nItem >= (INT)infoPtr->uNumItem))
1535 return FALSE;
1537 TRACE("[nItem=%d]\n", nItem);
1539 HEADER_CopyHDItemForNotify(infoPtr, &hdNotify, phdi, bUnicode, &pvScratch);
1540 if (HEADER_SendNotifyWithHDItemT(infoPtr, HDN_ITEMCHANGINGW, nItem, &hdNotify))
1542 Free(pvScratch);
1543 return FALSE;
1546 lpItem = &infoPtr->items[nItem];
1547 HEADER_StoreHDItemInHeader(lpItem, phdi->mask, phdi, bUnicode);
1549 if (phdi->mask & HDI_ORDER)
1550 if (phdi->iOrder >= 0 && phdi->iOrder < infoPtr->uNumItem)
1551 HEADER_ChangeItemOrder(infoPtr, nItem, phdi->iOrder);
1553 HEADER_SendNotifyWithHDItemT(infoPtr, HDN_ITEMCHANGEDW, nItem, &hdNotify);
1555 HEADER_SetItemBounds (infoPtr);
1557 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1559 Free(pvScratch);
1560 return TRUE;
1563 static inline LRESULT
1564 HEADER_SetUnicodeFormat (HEADER_INFO *infoPtr, WPARAM wParam)
1566 BOOL bTemp = (infoPtr->nNotifyFormat == NFR_UNICODE);
1568 infoPtr->nNotifyFormat = ((BOOL)wParam ? NFR_UNICODE : NFR_ANSI);
1570 return bTemp;
1574 static LRESULT
1575 HEADER_Create (HWND hwnd, const CREATESTRUCTW *lpcs)
1577 HEADER_INFO *infoPtr;
1578 TEXTMETRICW tm;
1579 HFONT hOldFont;
1580 HDC hdc;
1582 infoPtr = Alloc (sizeof(HEADER_INFO));
1583 SetWindowLongPtrW (hwnd, 0, (DWORD_PTR)infoPtr);
1585 infoPtr->hwndSelf = hwnd;
1586 infoPtr->hwndNotify = lpcs->hwndParent;
1587 infoPtr->uNumItem = 0;
1588 infoPtr->hFont = 0;
1589 infoPtr->items = 0;
1590 infoPtr->order = 0;
1591 infoPtr->bRectsValid = FALSE;
1592 infoPtr->hcurArrow = LoadCursorW (0, (LPWSTR)IDC_ARROW);
1593 infoPtr->hcurDivider = LoadCursorW (COMCTL32_hModule, MAKEINTRESOURCEW(IDC_DIVIDER));
1594 infoPtr->hcurDivopen = LoadCursorW (COMCTL32_hModule, MAKEINTRESOURCEW(IDC_DIVIDEROPEN));
1595 infoPtr->bPressed = FALSE;
1596 infoPtr->bTracking = FALSE;
1597 infoPtr->dwStyle = lpcs->style;
1598 infoPtr->iMoveItem = 0;
1599 infoPtr->himl = 0;
1600 infoPtr->iHotItem = -1;
1601 infoPtr->iHotDivider = -1;
1602 infoPtr->iMargin = 3*GetSystemMetrics(SM_CXEDGE);
1603 infoPtr->nNotifyFormat =
1604 SendMessageW (infoPtr->hwndNotify, WM_NOTIFYFORMAT, (WPARAM)hwnd, NF_QUERY);
1605 infoPtr->filter_change_timeout = 1000;
1607 hdc = GetDC (0);
1608 hOldFont = SelectObject (hdc, GetStockObject (SYSTEM_FONT));
1609 GetTextMetricsW (hdc, &tm);
1610 infoPtr->nHeight = tm.tmHeight + VERT_BORDER;
1611 SelectObject (hdc, hOldFont);
1612 ReleaseDC (0, hdc);
1614 OpenThemeData(hwnd, themeClass);
1616 return 0;
1620 static LRESULT
1621 HEADER_Destroy (HEADER_INFO *infoPtr)
1623 HTHEME theme = GetWindowTheme(infoPtr->hwndSelf);
1624 CloseThemeData(theme);
1625 return 0;
1628 static LRESULT
1629 HEADER_NCDestroy (HEADER_INFO *infoPtr)
1631 HEADER_ITEM *lpItem;
1632 INT nItem;
1634 if (infoPtr->items) {
1635 lpItem = infoPtr->items;
1636 for (nItem = 0; nItem < infoPtr->uNumItem; nItem++, lpItem++) {
1637 Free(lpItem->pszText);
1639 Free (infoPtr->items);
1642 Free(infoPtr->order);
1644 SetWindowLongPtrW (infoPtr->hwndSelf, 0, 0);
1645 Free (infoPtr);
1647 return 0;
1651 static inline LRESULT
1652 HEADER_GetFont (const HEADER_INFO *infoPtr)
1654 return (LRESULT)infoPtr->hFont;
1658 static BOOL
1659 HEADER_IsDragDistance(const HEADER_INFO *infoPtr, const POINT *pt)
1661 /* Windows allows for a mouse movement before starting the drag. We use the
1662 * SM_CXDOUBLECLICK/SM_CYDOUBLECLICK as that distance.
1664 return (abs(infoPtr->ptLButtonDown.x - pt->x)>GetSystemMetrics(SM_CXDOUBLECLK) ||
1665 abs(infoPtr->ptLButtonDown.y - pt->y)>GetSystemMetrics(SM_CYDOUBLECLK));
1668 static LRESULT
1669 HEADER_LButtonDblClk (const HEADER_INFO *infoPtr, INT x, INT y)
1671 POINT pt;
1672 UINT flags;
1673 INT nItem;
1675 pt.x = x;
1676 pt.y = y;
1677 HEADER_InternalHitTest (infoPtr, &pt, &flags, &nItem);
1679 if ((infoPtr->dwStyle & HDS_BUTTONS) && (flags == HHT_ONHEADER))
1680 HEADER_SendNotifyWithHDItemT(infoPtr, HDN_ITEMDBLCLICKW, nItem, NULL);
1681 else if ((flags == HHT_ONDIVIDER) || (flags == HHT_ONDIVOPEN))
1682 HEADER_SendNotifyWithHDItemT(infoPtr, HDN_DIVIDERDBLCLICKW, nItem, NULL);
1684 return 0;
1688 static LRESULT
1689 HEADER_LButtonDown (HEADER_INFO *infoPtr, INT x, INT y)
1691 POINT pt;
1692 UINT flags;
1693 INT nItem;
1694 HDC hdc;
1696 pt.x = x;
1697 pt.y = y;
1698 HEADER_InternalHitTest (infoPtr, &pt, &flags, &nItem);
1700 if ((infoPtr->dwStyle & HDS_BUTTONS) && (flags == HHT_ONHEADER)) {
1701 SetCapture (infoPtr->hwndSelf);
1702 infoPtr->bCaptured = TRUE;
1703 infoPtr->bPressed = TRUE;
1704 infoPtr->bDragging = FALSE;
1705 infoPtr->iMoveItem = nItem;
1706 infoPtr->ptLButtonDown = pt;
1708 infoPtr->items[nItem].bDown = TRUE;
1710 /* Send WM_CUSTOMDRAW */
1711 hdc = GetDC (infoPtr->hwndSelf);
1712 HEADER_RefreshItem (infoPtr, nItem);
1713 ReleaseDC (infoPtr->hwndSelf, hdc);
1715 TRACE("Pressed item %d!\n", nItem);
1717 else if ((flags == HHT_ONDIVIDER) || (flags == HHT_ONDIVOPEN)) {
1718 INT iCurrWidth = infoPtr->items[nItem].cxy;
1719 if (!HEADER_SendNotifyWithIntFieldT(infoPtr, HDN_BEGINTRACKW, nItem, HDI_WIDTH, iCurrWidth))
1721 SetCapture (infoPtr->hwndSelf);
1722 infoPtr->bCaptured = TRUE;
1723 infoPtr->bTracking = TRUE;
1724 infoPtr->iMoveItem = nItem;
1725 infoPtr->xTrackOffset = infoPtr->items[nItem].rect.right - pt.x;
1727 if (!(infoPtr->dwStyle & HDS_FULLDRAG)) {
1728 infoPtr->xOldTrack = infoPtr->items[nItem].rect.right;
1729 hdc = GetDC (infoPtr->hwndSelf);
1730 HEADER_DrawTrackLine (infoPtr, hdc, infoPtr->xOldTrack);
1731 ReleaseDC (infoPtr->hwndSelf, hdc);
1734 TRACE("Begin tracking item %d!\n", nItem);
1738 return 0;
1742 static LRESULT
1743 HEADER_LButtonUp (HEADER_INFO *infoPtr, INT x, INT y)
1745 POINT pt;
1746 UINT flags;
1747 INT nItem;
1748 HDC hdc;
1750 pt.x = x;
1751 pt.y = y;
1752 HEADER_InternalHitTest (infoPtr, &pt, &flags, &nItem);
1754 if (infoPtr->bPressed) {
1756 infoPtr->items[infoPtr->iMoveItem].bDown = FALSE;
1758 if (infoPtr->bDragging)
1760 HEADER_ITEM *lpItem = &infoPtr->items[infoPtr->iMoveItem];
1761 INT iNewOrder;
1763 ImageList_DragShowNolock(FALSE);
1764 ImageList_EndDrag();
1766 if (infoPtr->iHotDivider == -1)
1767 iNewOrder = -1;
1768 else if (infoPtr->iHotDivider == infoPtr->uNumItem)
1769 iNewOrder = infoPtr->uNumItem-1;
1770 else
1772 iNewOrder = HEADER_IndexToOrder(infoPtr, infoPtr->iHotDivider);
1773 if (iNewOrder > lpItem->iOrder)
1774 iNewOrder--;
1777 if (iNewOrder != -1 &&
1778 !HEADER_SendNotifyWithIntFieldT(infoPtr, HDN_ENDDRAG, infoPtr->iMoveItem, HDI_ORDER, iNewOrder))
1780 HEADER_ChangeItemOrder(infoPtr, infoPtr->iMoveItem, iNewOrder);
1781 infoPtr->bRectsValid = FALSE;
1782 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1784 else
1785 InvalidateRect(infoPtr->hwndSelf, &infoPtr->items[infoPtr->iMoveItem].rect, FALSE);
1787 infoPtr->bDragging = FALSE;
1788 HEADER_SetHotDivider(infoPtr, FALSE, -1);
1790 else
1792 hdc = GetDC (infoPtr->hwndSelf);
1793 HEADER_RefreshItem (infoPtr, infoPtr->iMoveItem);
1794 ReleaseDC (infoPtr->hwndSelf, hdc);
1796 if (!(infoPtr->dwStyle & HDS_DRAGDROP) || !HEADER_IsDragDistance(infoPtr, &pt))
1797 HEADER_SendNotifyWithHDItemT(infoPtr, HDN_ITEMCLICKW, infoPtr->iMoveItem, NULL);
1800 TRACE("Released item %d!\n", infoPtr->iMoveItem);
1801 infoPtr->bPressed = FALSE;
1803 else if (infoPtr->bTracking) {
1804 INT iNewWidth = pt.x - infoPtr->items[infoPtr->iMoveItem].rect.left + infoPtr->xTrackOffset;
1805 if (iNewWidth < 0)
1806 iNewWidth = 0;
1807 TRACE("End tracking item %d!\n", infoPtr->iMoveItem);
1808 infoPtr->bTracking = FALSE;
1810 HEADER_SendNotifyWithIntFieldT(infoPtr, HDN_ENDTRACKW, infoPtr->iMoveItem, HDI_WIDTH, iNewWidth);
1812 if (!(infoPtr->dwStyle & HDS_FULLDRAG)) {
1813 hdc = GetDC (infoPtr->hwndSelf);
1814 HEADER_DrawTrackLine (infoPtr, hdc, infoPtr->xOldTrack);
1815 ReleaseDC (infoPtr->hwndSelf, hdc);
1818 if (!HEADER_SendNotifyWithIntFieldT(infoPtr, HDN_ITEMCHANGINGW, infoPtr->iMoveItem, HDI_WIDTH, iNewWidth))
1820 infoPtr->items[infoPtr->iMoveItem].cxy = iNewWidth;
1821 HEADER_SendNotifyWithIntFieldT(infoPtr, HDN_ITEMCHANGEDW, infoPtr->iMoveItem, HDI_WIDTH, iNewWidth);
1824 HEADER_SetItemBounds (infoPtr);
1825 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1828 if (infoPtr->bCaptured) {
1829 infoPtr->bCaptured = FALSE;
1830 ReleaseCapture ();
1831 HEADER_SendSimpleNotify (infoPtr, NM_RELEASEDCAPTURE);
1834 return 0;
1838 static LRESULT
1839 HEADER_NotifyFormat (HEADER_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
1841 switch (lParam)
1843 case NF_QUERY:
1844 return infoPtr->nNotifyFormat;
1846 case NF_REQUERY:
1847 infoPtr->nNotifyFormat =
1848 SendMessageW ((HWND)wParam, WM_NOTIFYFORMAT,
1849 (WPARAM)infoPtr->hwndSelf, NF_QUERY);
1850 return infoPtr->nNotifyFormat;
1853 return 0;
1856 static LRESULT
1857 HEADER_MouseLeave (HEADER_INFO *infoPtr)
1859 /* Reset hot-tracked item when mouse leaves control. */
1860 INT oldHotItem = infoPtr->iHotItem;
1861 HDC hdc = GetDC (infoPtr->hwndSelf);
1863 infoPtr->iHotItem = -1;
1864 if (oldHotItem != -1) HEADER_RefreshItem (infoPtr, oldHotItem);
1865 ReleaseDC (infoPtr->hwndSelf, hdc);
1867 return 0;
1871 static LRESULT
1872 HEADER_MouseMove (HEADER_INFO *infoPtr, LPARAM lParam)
1874 POINT pt;
1875 UINT flags;
1876 INT nItem, nWidth;
1877 HDC hdc;
1878 /* With theming, hottracking is always enabled */
1879 BOOL hotTrackEnabled =
1880 ((infoPtr->dwStyle & HDS_BUTTONS) && (infoPtr->dwStyle & HDS_HOTTRACK))
1881 || (GetWindowTheme (infoPtr->hwndSelf) != NULL);
1882 INT oldHotItem = infoPtr->iHotItem;
1884 pt.x = (INT)(SHORT)LOWORD(lParam);
1885 pt.y = (INT)(SHORT)HIWORD(lParam);
1886 HEADER_InternalHitTest (infoPtr, &pt, &flags, &nItem);
1888 if (hotTrackEnabled) {
1889 if (flags & (HHT_ONHEADER | HHT_ONDIVIDER | HHT_ONDIVOPEN))
1890 infoPtr->iHotItem = nItem;
1891 else
1892 infoPtr->iHotItem = -1;
1895 if (infoPtr->bCaptured) {
1896 /* check if we should drag the header */
1897 if (infoPtr->bPressed && !infoPtr->bDragging && (infoPtr->dwStyle & HDS_DRAGDROP)
1898 && HEADER_IsDragDistance(infoPtr, &pt))
1900 if (!HEADER_SendNotifyWithHDItemT(infoPtr, HDN_BEGINDRAG, infoPtr->iMoveItem, NULL))
1902 HIMAGELIST hDragItem = HEADER_CreateDragImage(infoPtr, infoPtr->iMoveItem);
1903 if (hDragItem != NULL)
1905 HEADER_ITEM *lpItem = &infoPtr->items[infoPtr->iMoveItem];
1906 TRACE("Starting item drag\n");
1907 ImageList_BeginDrag(hDragItem, 0, pt.x - lpItem->rect.left, 0);
1908 ImageList_DragShowNolock(TRUE);
1909 ImageList_Destroy(hDragItem);
1910 infoPtr->bDragging = TRUE;
1915 if (infoPtr->bDragging)
1917 POINT drag;
1918 drag.x = pt.x;
1919 drag.y = 0;
1920 ClientToScreen(infoPtr->hwndSelf, &drag);
1921 ImageList_DragMove(drag.x, drag.y);
1922 HEADER_SetHotDivider(infoPtr, TRUE, lParam);
1925 if (infoPtr->bPressed && !infoPtr->bDragging) {
1926 BOOL oldState = infoPtr->items[infoPtr->iMoveItem].bDown;
1927 if ((nItem == infoPtr->iMoveItem) && (flags == HHT_ONHEADER))
1928 infoPtr->items[infoPtr->iMoveItem].bDown = TRUE;
1929 else
1930 infoPtr->items[infoPtr->iMoveItem].bDown = FALSE;
1931 if (oldState != infoPtr->items[infoPtr->iMoveItem].bDown) {
1932 hdc = GetDC (infoPtr->hwndSelf);
1933 HEADER_RefreshItem (infoPtr, infoPtr->iMoveItem);
1934 ReleaseDC (infoPtr->hwndSelf, hdc);
1937 TRACE("Moving pressed item %d!\n", infoPtr->iMoveItem);
1939 else if (infoPtr->bTracking) {
1940 if (infoPtr->dwStyle & HDS_FULLDRAG) {
1941 HEADER_ITEM *lpItem = &infoPtr->items[infoPtr->iMoveItem];
1942 nWidth = pt.x - lpItem->rect.left + infoPtr->xTrackOffset;
1943 if (!HEADER_SendNotifyWithIntFieldT(infoPtr, HDN_ITEMCHANGINGW, infoPtr->iMoveItem, HDI_WIDTH, nWidth))
1945 INT nOldWidth = lpItem->rect.right - lpItem->rect.left;
1946 RECT rcClient;
1947 RECT rcScroll;
1949 if (nWidth < 0) nWidth = 0;
1950 infoPtr->items[infoPtr->iMoveItem].cxy = nWidth;
1951 HEADER_SetItemBounds(infoPtr);
1953 GetClientRect(infoPtr->hwndSelf, &rcClient);
1954 rcScroll = rcClient;
1955 rcScroll.left = lpItem->rect.left + nOldWidth;
1956 ScrollWindowEx(infoPtr->hwndSelf, nWidth - nOldWidth, 0, &rcScroll, &rcClient, NULL, NULL, 0);
1957 InvalidateRect(infoPtr->hwndSelf, &lpItem->rect, FALSE);
1958 UpdateWindow(infoPtr->hwndSelf);
1960 HEADER_SendNotifyWithIntFieldT(infoPtr, HDN_ITEMCHANGEDW, infoPtr->iMoveItem, HDI_WIDTH, nWidth);
1963 else {
1964 INT iTrackWidth;
1965 hdc = GetDC (infoPtr->hwndSelf);
1966 HEADER_DrawTrackLine (infoPtr, hdc, infoPtr->xOldTrack);
1967 infoPtr->xOldTrack = pt.x + infoPtr->xTrackOffset;
1968 if (infoPtr->xOldTrack < infoPtr->items[infoPtr->iMoveItem].rect.left)
1969 infoPtr->xOldTrack = infoPtr->items[infoPtr->iMoveItem].rect.left;
1970 HEADER_DrawTrackLine (infoPtr, hdc, infoPtr->xOldTrack);
1971 ReleaseDC (infoPtr->hwndSelf, hdc);
1972 iTrackWidth = infoPtr->xOldTrack - infoPtr->items[infoPtr->iMoveItem].rect.left;
1973 /* FIXME: should stop tracking if HDN_TRACK returns TRUE */
1974 HEADER_SendNotifyWithIntFieldT(infoPtr, HDN_TRACKW, infoPtr->iMoveItem, HDI_WIDTH, iTrackWidth);
1977 TRACE("Tracking item %d!\n", infoPtr->iMoveItem);
1981 if (hotTrackEnabled) {
1982 TRACKMOUSEEVENT tme;
1983 if (oldHotItem != infoPtr->iHotItem && !infoPtr->bDragging) {
1984 hdc = GetDC (infoPtr->hwndSelf);
1985 if (oldHotItem != -1) HEADER_RefreshItem (infoPtr, oldHotItem);
1986 if (infoPtr->iHotItem != -1) HEADER_RefreshItem (infoPtr, infoPtr->iHotItem);
1987 ReleaseDC (infoPtr->hwndSelf, hdc);
1989 tme.cbSize = sizeof( tme );
1990 tme.dwFlags = TME_LEAVE;
1991 tme.hwndTrack = infoPtr->hwndSelf;
1992 TrackMouseEvent( &tme );
1995 return 0;
1999 static LRESULT
2000 HEADER_Paint (HEADER_INFO *infoPtr, HDC hdcParam)
2002 HDC hdc;
2003 PAINTSTRUCT ps;
2005 hdc = hdcParam==0 ? BeginPaint (infoPtr->hwndSelf, &ps) : hdcParam;
2006 HEADER_Refresh (infoPtr, hdc);
2007 if(!hdcParam)
2008 EndPaint (infoPtr->hwndSelf, &ps);
2009 return 0;
2013 static LRESULT
2014 HEADER_RButtonUp (HEADER_INFO *infoPtr, INT x, INT y)
2016 BOOL bRet;
2017 POINT pt;
2019 pt.x = x;
2020 pt.y = y;
2022 /* Send a Notify message */
2023 bRet = HEADER_SendSimpleNotify (infoPtr, NM_RCLICK);
2025 /* Change to screen coordinate for WM_CONTEXTMENU */
2026 ClientToScreen(infoPtr->hwndSelf, &pt);
2028 /* Send a WM_CONTEXTMENU message in response to the RBUTTONUP */
2029 SendMessageW( infoPtr->hwndSelf, WM_CONTEXTMENU, (WPARAM) infoPtr->hwndSelf, MAKELPARAM(pt.x, pt.y));
2031 return bRet;
2035 static LRESULT
2036 HEADER_SetCursor (HEADER_INFO *infoPtr, LPARAM lParam)
2038 POINT pt;
2039 UINT flags;
2040 INT nItem;
2042 TRACE("code=0x%X id=0x%X\n", LOWORD(lParam), HIWORD(lParam));
2044 GetCursorPos (&pt);
2045 ScreenToClient (infoPtr->hwndSelf, &pt);
2047 HEADER_InternalHitTest (infoPtr, &pt, &flags, &nItem);
2049 if (flags == HHT_ONDIVIDER)
2050 SetCursor (infoPtr->hcurDivider);
2051 else if (flags == HHT_ONDIVOPEN)
2052 SetCursor (infoPtr->hcurDivopen);
2053 else
2054 SetCursor (infoPtr->hcurArrow);
2056 return 0;
2060 static LRESULT
2061 HEADER_SetFont (HEADER_INFO *infoPtr, HFONT hFont, WORD Redraw)
2063 TEXTMETRICW tm;
2064 HFONT hOldFont;
2065 HDC hdc;
2067 infoPtr->hFont = hFont;
2069 hdc = GetDC (0);
2070 hOldFont = SelectObject (hdc, infoPtr->hFont ? infoPtr->hFont : GetStockObject (SYSTEM_FONT));
2071 GetTextMetricsW (hdc, &tm);
2072 infoPtr->nHeight = tm.tmHeight + VERT_BORDER;
2073 SelectObject (hdc, hOldFont);
2074 ReleaseDC (0, hdc);
2076 infoPtr->bRectsValid = FALSE;
2078 if (Redraw) {
2079 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2082 return 0;
2085 static LRESULT HEADER_SetRedraw(HEADER_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
2087 /* ignoring the InvalidateRect calls is handled by user32. But some apps expect
2088 * that we invalidate the header and this has to be done manually */
2089 LRESULT ret;
2091 ret = DefWindowProcW(infoPtr->hwndSelf, WM_SETREDRAW, wParam, lParam);
2092 if (wParam)
2093 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
2094 return ret;
2097 static INT HEADER_StyleChanged(HEADER_INFO *infoPtr, WPARAM wStyleType,
2098 const STYLESTRUCT *lpss)
2100 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
2101 wStyleType, lpss->styleOld, lpss->styleNew);
2103 if (wStyleType != GWL_STYLE) return 0;
2105 infoPtr->dwStyle = lpss->styleNew;
2107 return 0;
2110 /* Update the theme handle after a theme change */
2111 static LRESULT HEADER_ThemeChanged(const HEADER_INFO *infoPtr)
2113 HTHEME theme = GetWindowTheme(infoPtr->hwndSelf);
2114 CloseThemeData(theme);
2115 OpenThemeData(infoPtr->hwndSelf, themeClass);
2116 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2117 return 0;
2120 static INT HEADER_SetFilterChangeTimeout(HEADER_INFO *infoPtr, INT timeout)
2122 INT old_timeout = infoPtr->filter_change_timeout;
2124 if (timeout != 0)
2125 infoPtr->filter_change_timeout = timeout;
2126 return old_timeout;
2129 static LRESULT WINAPI
2130 HEADER_WindowProc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
2132 HEADER_INFO *infoPtr = (HEADER_INFO *)GetWindowLongPtrW(hwnd, 0);
2134 TRACE("hwnd=%p msg=%x wparam=%lx lParam=%lx\n", hwnd, msg, wParam, lParam);
2135 if (!infoPtr && (msg != WM_CREATE))
2136 return DefWindowProcW (hwnd, msg, wParam, lParam);
2137 switch (msg) {
2138 /* case HDM_CLEARFILTER: */
2140 case HDM_CREATEDRAGIMAGE:
2141 return (LRESULT)HEADER_CreateDragImage (infoPtr, (INT)wParam);
2143 case HDM_DELETEITEM:
2144 return HEADER_DeleteItem (infoPtr, (INT)wParam);
2146 /* case HDM_EDITFILTER: */
2148 case HDM_GETBITMAPMARGIN:
2149 return HEADER_GetBitmapMargin(infoPtr);
2151 case HDM_GETIMAGELIST:
2152 return HEADER_GetImageList (infoPtr);
2154 case HDM_GETITEMA:
2155 case HDM_GETITEMW:
2156 return HEADER_GetItemT (infoPtr, (INT)wParam, (LPHDITEMW)lParam, msg == HDM_GETITEMW);
2158 case HDM_GETITEMCOUNT:
2159 return HEADER_GetItemCount (infoPtr);
2161 case HDM_GETITEMRECT:
2162 return HEADER_GetItemRect (infoPtr, (INT)wParam, (LPRECT)lParam);
2164 case HDM_GETORDERARRAY:
2165 return HEADER_GetOrderArray(infoPtr, (INT)wParam, (LPINT)lParam);
2167 case HDM_GETUNICODEFORMAT:
2168 return HEADER_GetUnicodeFormat (infoPtr);
2170 case HDM_HITTEST:
2171 return HEADER_HitTest (infoPtr, (LPHDHITTESTINFO)lParam);
2173 case HDM_INSERTITEMA:
2174 case HDM_INSERTITEMW:
2175 return HEADER_InsertItemT (infoPtr, (INT)wParam, (LPHDITEMW)lParam, msg == HDM_INSERTITEMW);
2177 case HDM_LAYOUT:
2178 return HEADER_Layout (infoPtr, (LPHDLAYOUT)lParam);
2180 case HDM_ORDERTOINDEX:
2181 return HEADER_OrderToIndex(infoPtr, (INT)wParam);
2183 case HDM_SETBITMAPMARGIN:
2184 return HEADER_SetBitmapMargin(infoPtr, (INT)wParam);
2186 case HDM_SETFILTERCHANGETIMEOUT:
2187 return HEADER_SetFilterChangeTimeout(infoPtr, (INT)lParam);
2189 case HDM_SETHOTDIVIDER:
2190 return HEADER_SetHotDivider(infoPtr, wParam, lParam);
2192 case HDM_SETIMAGELIST:
2193 return HEADER_SetImageList (infoPtr, (HIMAGELIST)lParam);
2195 case HDM_SETITEMA:
2196 case HDM_SETITEMW:
2197 return HEADER_SetItemT (infoPtr, (INT)wParam, (LPHDITEMW)lParam, msg == HDM_SETITEMW);
2199 case HDM_SETORDERARRAY:
2200 return HEADER_SetOrderArray(infoPtr, (INT)wParam, (LPINT)lParam);
2202 case HDM_SETUNICODEFORMAT:
2203 return HEADER_SetUnicodeFormat (infoPtr, wParam);
2205 case WM_CREATE:
2206 return HEADER_Create (hwnd, (LPCREATESTRUCTW)lParam);
2208 case WM_DESTROY:
2209 return HEADER_Destroy (infoPtr);
2211 case WM_NCDESTROY:
2212 return HEADER_NCDestroy (infoPtr);
2214 case WM_ERASEBKGND:
2215 return 1;
2217 case WM_GETDLGCODE:
2218 return DLGC_WANTTAB | DLGC_WANTARROWS;
2220 case WM_GETFONT:
2221 return HEADER_GetFont (infoPtr);
2223 case WM_LBUTTONDBLCLK:
2224 return HEADER_LButtonDblClk (infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
2226 case WM_LBUTTONDOWN:
2227 return HEADER_LButtonDown (infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
2229 case WM_LBUTTONUP:
2230 return HEADER_LButtonUp (infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
2232 case WM_MOUSELEAVE:
2233 return HEADER_MouseLeave (infoPtr);
2235 case WM_MOUSEMOVE:
2236 return HEADER_MouseMove (infoPtr, lParam);
2238 case WM_NOTIFYFORMAT:
2239 return HEADER_NotifyFormat (infoPtr, wParam, lParam);
2241 case WM_SIZE:
2242 return HEADER_Size (infoPtr);
2244 case WM_THEMECHANGED:
2245 return HEADER_ThemeChanged (infoPtr);
2247 case WM_PRINTCLIENT:
2248 case WM_PAINT:
2249 return HEADER_Paint (infoPtr, (HDC)wParam);
2251 case WM_RBUTTONUP:
2252 return HEADER_RButtonUp (infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
2254 case WM_SETCURSOR:
2255 return HEADER_SetCursor (infoPtr, lParam);
2257 case WM_SETFONT:
2258 return HEADER_SetFont (infoPtr, (HFONT)wParam, (WORD)lParam);
2260 case WM_SETREDRAW:
2261 return HEADER_SetRedraw(infoPtr, wParam, lParam);
2263 case WM_STYLECHANGED:
2264 return HEADER_StyleChanged(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
2266 case WM_SYSCOLORCHANGE:
2267 COMCTL32_RefreshSysColors();
2268 return 0;
2270 default:
2271 if ((msg >= WM_USER) && (msg < WM_APP) && !COMCTL32_IsReflectedMessage(msg))
2272 ERR("unknown msg %04x wp=%04lx lp=%08lx\n",
2273 msg, wParam, lParam );
2274 return DefWindowProcW(hwnd, msg, wParam, lParam);
2279 VOID
2280 HEADER_Register (void)
2282 WNDCLASSW wndClass;
2284 ZeroMemory (&wndClass, sizeof(WNDCLASSW));
2285 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS;
2286 wndClass.lpfnWndProc = HEADER_WindowProc;
2287 wndClass.cbClsExtra = 0;
2288 wndClass.cbWndExtra = sizeof(HEADER_INFO *);
2289 wndClass.hCursor = LoadCursorW (0, (LPWSTR)IDC_ARROW);
2290 wndClass.lpszClassName = WC_HEADERW;
2292 RegisterClassW (&wndClass);
2296 VOID
2297 HEADER_Unregister (void)
2299 UnregisterClassW (WC_HEADERW, NULL);