maintainers: Update the Direct3D section.
[wine.git] / dlls / comctl32 / header.c
blobbe4c5616bb7692296900a1b29c8c5c0b8a9be863
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 "wingdi.h"
37 #include "winuser.h"
38 #include "winnls.h"
39 #include "commctrl.h"
40 #include "comctl32.h"
41 #include "vssym32.h"
42 #include "uxtheme.h"
43 #include "wine/debug.h"
44 #include "wine/heap.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[] = L"Header";
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 heap_free(lpItem->pszText);
145 lpItem->pszText = NULL;
147 if (phdi->pszText != LPSTR_TEXTCALLBACKW) /* covers != TEXTCALLBACKA too */
149 const WCHAR *pszText = phdi->pszText != NULL ? phdi->pszText : L"";
150 if (fUnicode)
151 Str_SetPtrW(&lpItem->pszText, pszText);
152 else
153 Str_SetPtrAtoW(&lpItem->pszText, (LPCSTR)pszText);
154 lpItem->callbackMask &= ~HDI_TEXT;
156 else
158 lpItem->pszText = NULL;
159 lpItem->callbackMask |= HDI_TEXT;
164 static inline LRESULT
165 HEADER_IndexToOrder (const HEADER_INFO *infoPtr, INT iItem)
167 HEADER_ITEM *lpItem = &infoPtr->items[iItem];
168 return lpItem->iOrder;
172 static INT
173 HEADER_OrderToIndex(const HEADER_INFO *infoPtr, INT iorder)
175 if ((iorder <0) || iorder >= infoPtr->uNumItem)
176 return iorder;
177 return infoPtr->order[iorder];
180 static void
181 HEADER_ChangeItemOrder(const HEADER_INFO *infoPtr, INT iItem, INT iNewOrder)
183 HEADER_ITEM *lpItem = &infoPtr->items[iItem];
184 INT i, nMin, nMax;
186 TRACE("%d: %d->%d\n", iItem, lpItem->iOrder, iNewOrder);
187 if (lpItem->iOrder < iNewOrder)
189 memmove(&infoPtr->order[lpItem->iOrder],
190 &infoPtr->order[lpItem->iOrder + 1],
191 (iNewOrder - lpItem->iOrder) * sizeof(INT));
193 if (iNewOrder < lpItem->iOrder)
195 memmove(&infoPtr->order[iNewOrder + 1],
196 &infoPtr->order[iNewOrder],
197 (lpItem->iOrder - iNewOrder) * sizeof(INT));
199 infoPtr->order[iNewOrder] = iItem;
200 nMin = min(lpItem->iOrder, iNewOrder);
201 nMax = max(lpItem->iOrder, iNewOrder);
202 for (i = nMin; i <= nMax; i++)
203 infoPtr->items[infoPtr->order[i]].iOrder = i;
206 /* Note: if iItem is the last item then this function returns infoPtr->uNumItem */
207 static INT
208 HEADER_NextItem(const HEADER_INFO *infoPtr, INT iItem)
210 return HEADER_OrderToIndex(infoPtr, HEADER_IndexToOrder(infoPtr, iItem)+1);
213 static INT
214 HEADER_PrevItem(const HEADER_INFO *infoPtr, INT iItem)
216 return HEADER_OrderToIndex(infoPtr, HEADER_IndexToOrder(infoPtr, iItem)-1);
219 /* TRUE when item is not resizable with dividers,
220 note that valid index should be supplied */
221 static inline BOOL
222 HEADER_IsItemFixed(const HEADER_INFO *infoPtr, INT iItem)
224 return (infoPtr->dwStyle & HDS_NOSIZING) || (infoPtr->items[iItem].fmt & HDF_FIXEDWIDTH);
227 static void
228 HEADER_SetItemBounds (HEADER_INFO *infoPtr)
230 HEADER_ITEM *phdi;
231 RECT rect;
232 unsigned int i;
233 int x;
235 infoPtr->bRectsValid = TRUE;
237 if (infoPtr->uNumItem == 0)
238 return;
240 GetClientRect (infoPtr->hwndSelf, &rect);
242 x = rect.left;
243 for (i = 0; i < infoPtr->uNumItem; i++) {
244 phdi = &infoPtr->items[HEADER_OrderToIndex(infoPtr,i)];
245 phdi->rect.top = rect.top;
246 phdi->rect.bottom = rect.bottom;
247 phdi->rect.left = x;
248 phdi->rect.right = phdi->rect.left + ((phdi->cxy>0)?phdi->cxy:0);
249 x = phdi->rect.right;
253 static LRESULT
254 HEADER_Size (HEADER_INFO *infoPtr)
256 HEADER_SetItemBounds(infoPtr);
257 return 0;
260 static void HEADER_GetHotDividerRect(const HEADER_INFO *infoPtr, RECT *r)
262 INT iDivider = infoPtr->iHotDivider;
263 if (infoPtr->uNumItem > 0)
265 HEADER_ITEM *lpItem;
267 if (iDivider < infoPtr->uNumItem)
269 lpItem = &infoPtr->items[iDivider];
270 r->left = lpItem->rect.left - HOT_DIVIDER_WIDTH/2;
271 r->right = lpItem->rect.left + HOT_DIVIDER_WIDTH/2;
273 else
275 lpItem = &infoPtr->items[HEADER_OrderToIndex(infoPtr, infoPtr->uNumItem-1)];
276 r->left = lpItem->rect.right - HOT_DIVIDER_WIDTH/2;
277 r->right = lpItem->rect.right + HOT_DIVIDER_WIDTH/2;
279 r->top = lpItem->rect.top;
280 r->bottom = lpItem->rect.bottom;
282 else
284 RECT clientRect;
285 GetClientRect(infoPtr->hwndSelf, &clientRect);
286 *r = clientRect;
287 r->right = r->left + HOT_DIVIDER_WIDTH/2;
291 static void
292 HEADER_FillItemFrame(HEADER_INFO *infoPtr, HDC hdc, RECT *r, const HEADER_ITEM *item, BOOL hottrack)
294 HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
296 if (theme) {
297 int state = (item->bDown) ? HIS_PRESSED : (hottrack ? HIS_HOT : HIS_NORMAL);
298 DrawThemeBackground (theme, hdc, HP_HEADERITEM, state, r, NULL);
299 GetThemeBackgroundContentRect (theme, hdc, HP_HEADERITEM, state, r, r);
301 else
303 HBRUSH hbr = CreateSolidBrush(GetBkColor(hdc));
304 FillRect(hdc, r, hbr);
305 DeleteObject(hbr);
309 static void
310 HEADER_DrawItemFrame(HEADER_INFO *infoPtr, HDC hdc, RECT *r, const HEADER_ITEM *item)
312 if (GetWindowTheme(infoPtr->hwndSelf)) return;
314 if (!(infoPtr->dwStyle & HDS_FLAT))
316 if (infoPtr->dwStyle & HDS_BUTTONS) {
317 if (item->bDown)
318 DrawEdge (hdc, r, BDR_RAISEDOUTER, BF_RECT | BF_FLAT | BF_ADJUST);
319 else
320 DrawEdge (hdc, r, EDGE_RAISED, BF_RECT | BF_SOFT | BF_ADJUST);
322 else
323 DrawEdge (hdc, r, EDGE_ETCHED, BF_BOTTOM | BF_RIGHT | BF_ADJUST);
327 /* Create a region for the sort arrow with its bounding rect's top-left
328 co-ord x,y and its height h. */
329 static HRGN create_sort_arrow( INT x, INT y, INT h, BOOL is_up )
331 char buffer[256];
332 RGNDATA *data = (RGNDATA *)buffer;
333 DWORD size = FIELD_OFFSET(RGNDATA, Buffer[h * sizeof(RECT)]);
334 INT i, yinc = 1;
335 HRGN rgn;
337 if (size > sizeof(buffer))
339 data = heap_alloc( size );
340 if (!data) return NULL;
342 data->rdh.dwSize = sizeof(data->rdh);
343 data->rdh.iType = RDH_RECTANGLES;
344 data->rdh.nCount = 0;
345 data->rdh.nRgnSize = h * sizeof(RECT);
347 if (!is_up)
349 y += h - 1;
350 yinc = -1;
353 x += h - 1; /* set x to the centre */
355 for (i = 0; i < h; i++, y += yinc)
357 RECT *rect = (RECT *)data->Buffer + data->rdh.nCount;
358 rect->left = x - i;
359 rect->top = y;
360 rect->right = x + i + 1;
361 rect->bottom = y + 1;
362 data->rdh.nCount++;
364 rgn = ExtCreateRegion( NULL, size, data );
365 if (data != (RGNDATA *)buffer) heap_free( data );
366 return rgn;
369 static INT
370 HEADER_DrawItem (HEADER_INFO *infoPtr, HDC hdc, INT iItem, BOOL bHotTrack, LRESULT lCDFlags)
372 HEADER_ITEM *phdi = &infoPtr->items[iItem];
373 RECT r;
374 INT oldBkMode;
375 HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
376 NMCUSTOMDRAW nmcd;
377 int state = 0;
379 TRACE("DrawItem(iItem %d bHotTrack %d unicode flag %d)\n", iItem, bHotTrack, (infoPtr->nNotifyFormat == NFR_UNICODE));
381 r = phdi->rect;
382 if (r.right - r.left == 0)
383 return phdi->rect.right;
385 if (theme)
386 state = (phdi->bDown) ? HIS_PRESSED : (bHotTrack ? HIS_HOT : HIS_NORMAL);
388 /* Set the colors before sending NM_CUSTOMDRAW so that it can change them */
389 SetTextColor(hdc, (bHotTrack && !theme) ? comctl32_color.clrHighlight : comctl32_color.clrBtnText);
390 SetBkColor(hdc, comctl32_color.clr3dFace);
392 if (lCDFlags & CDRF_NOTIFYITEMDRAW && !(phdi->fmt & HDF_OWNERDRAW))
394 LRESULT lCDItemFlags;
396 nmcd.dwDrawStage = CDDS_PREPAINT | CDDS_ITEM;
397 nmcd.hdc = hdc;
398 nmcd.dwItemSpec = iItem;
399 nmcd.rc = r;
400 nmcd.uItemState = phdi->bDown ? CDIS_SELECTED : 0;
401 nmcd.lItemlParam = phdi->lParam;
403 lCDItemFlags = HEADER_SendNotify(infoPtr, NM_CUSTOMDRAW, (NMHDR *)&nmcd);
404 if (lCDItemFlags & CDRF_SKIPDEFAULT)
405 return phdi->rect.right;
408 /* Fill background, owner could draw over it. */
409 HEADER_FillItemFrame(infoPtr, hdc, &r, phdi, bHotTrack);
411 if (phdi->fmt & HDF_OWNERDRAW)
413 DRAWITEMSTRUCT dis;
414 BOOL ret;
416 dis.CtlType = ODT_HEADER;
417 dis.CtlID = GetWindowLongPtrW (infoPtr->hwndSelf, GWLP_ID);
418 dis.itemID = iItem;
419 dis.itemAction = ODA_DRAWENTIRE;
420 dis.itemState = phdi->bDown ? ODS_SELECTED : 0;
421 dis.hwndItem = infoPtr->hwndSelf;
422 dis.hDC = hdc;
423 dis.rcItem = phdi->rect;
424 dis.itemData = phdi->lParam;
425 oldBkMode = SetBkMode(hdc, TRANSPARENT);
426 ret = SendMessageW (infoPtr->hwndNotify, WM_DRAWITEM, dis.CtlID, (LPARAM)&dis);
427 if (oldBkMode != TRANSPARENT)
428 SetBkMode(hdc, oldBkMode);
430 if (!ret)
431 HEADER_FillItemFrame(infoPtr, hdc, &r, phdi, bHotTrack);
433 /* Edges are always drawn if we don't have attached theme. */
434 HEADER_DrawItemFrame(infoPtr, hdc, &r, phdi);
435 /* If application processed WM_DRAWITEM we should skip label painting,
436 edges are drawn no matter what. */
437 if (ret) return phdi->rect.right;
439 else
440 HEADER_DrawItemFrame(infoPtr, hdc, &r, phdi);
442 if (phdi->bDown) {
443 r.left += 2;
444 r.top += 2;
447 /* Now text and image */
449 INT rw, rh; /* width and height of r */
450 INT *x = NULL; /* x and ... */
451 UINT *w = NULL; /* ... width of the pic (bmp or img) which is part of cnt */
452 /* cnt,txt,img,bmp */
453 INT cx, tx, ix, bx;
454 UINT cw, tw, iw, bw;
455 INT img_cx, img_cy;
456 INT sort_w, sort_x, sort_h;
457 BITMAP bmp;
459 HEADER_PrepareCallbackItems(infoPtr, iItem, HDI_TEXT|HDI_IMAGE);
460 cw = iw = bw = sort_w = sort_h = 0;
461 rw = r.right - r.left;
462 rh = r.bottom - r.top;
464 if (phdi->fmt & HDF_STRING) {
465 RECT textRect;
467 SetRectEmpty(&textRect);
469 if (theme) {
470 GetThemeTextExtent(theme, hdc, HP_HEADERITEM, state, phdi->pszText, -1,
471 DT_LEFT|DT_VCENTER|DT_SINGLELINE, NULL, &textRect);
472 } else {
473 DrawTextW (hdc, phdi->pszText, -1,
474 &textRect, DT_LEFT|DT_VCENTER|DT_SINGLELINE|DT_CALCRECT);
476 cw = textRect.right - textRect.left + 2 * infoPtr->iMargin;
479 if (phdi->fmt & (HDF_SORTUP | HDF_SORTDOWN)) {
480 sort_h = MulDiv( infoPtr->nHeight - VERT_BORDER, 4, 13 );
481 sort_w = 2 * sort_h - 1 + infoPtr->iMargin * 2;
482 cw += sort_w;
483 } else { /* sort arrows take precedent over images/bitmaps */
484 if ((phdi->fmt & HDF_IMAGE) && ImageList_GetIconSize( infoPtr->himl, &img_cx, &img_cy )) {
485 iw = img_cx + 2 * infoPtr->iMargin;
486 x = &ix;
487 w = &iw;
490 if ((phdi->fmt & HDF_BITMAP) && (phdi->hbm)) {
491 GetObjectW (phdi->hbm, sizeof(BITMAP), &bmp);
492 bw = bmp.bmWidth + 2 * infoPtr->iMargin;
493 if (!iw) {
494 x = &bx;
495 w = &bw;
498 if (bw || iw)
499 cw += *w;
502 /* align cx using the unclipped cw */
503 if ((phdi->fmt & HDF_JUSTIFYMASK) == HDF_LEFT)
504 cx = r.left;
505 else if ((phdi->fmt & HDF_JUSTIFYMASK) == HDF_CENTER)
506 cx = r.left + rw / 2 - cw / 2;
507 else /* HDF_RIGHT */
508 cx = r.right - cw;
510 /* clip cx & cw */
511 if (cx < r.left)
512 cx = r.left;
513 if (cx + cw > r.right)
514 cw = r.right - cx;
516 tx = cx + infoPtr->iMargin;
517 /* since cw might have changed we have to recalculate tw */
518 tw = cw - infoPtr->iMargin * 2;
520 tw -= sort_w;
521 sort_x = cx + tw + infoPtr->iMargin * 3;
523 if (iw || bw) {
524 tw -= *w;
525 if (phdi->fmt & HDF_BITMAP_ON_RIGHT) {
526 /* put pic behind text */
527 *x = cx + tw + infoPtr->iMargin * 3;
528 } else {
529 *x = cx + infoPtr->iMargin;
530 /* move text behind pic */
531 tx += *w;
535 if (iw && bw) {
536 /* since we're done with the layout we can
537 now calculate the position of bmp which
538 has no influence on alignment and layout
539 because of img */
540 if ((phdi->fmt & HDF_JUSTIFYMASK) == HDF_RIGHT)
541 bx = cx - bw + infoPtr->iMargin;
542 else
543 bx = cx + cw + infoPtr->iMargin;
546 if (sort_w || iw || bw) {
547 HDC hClipDC = GetDC(infoPtr->hwndSelf);
548 HRGN hClipRgn = CreateRectRgn(r.left, r.top, r.right, r.bottom);
549 SelectClipRgn(hClipDC, hClipRgn);
551 if (sort_w) {
552 HRGN arrow = create_sort_arrow( sort_x, r.top + (rh - sort_h) / 2,
553 sort_h, phdi->fmt & HDF_SORTUP );
554 if (arrow) {
555 FillRgn( hClipDC, arrow, GetSysColorBrush( COLOR_GRAYTEXT ) );
556 DeleteObject( arrow );
560 if (bw) {
561 HDC hdcBitmap = CreateCompatibleDC (hClipDC);
562 SelectObject (hdcBitmap, phdi->hbm);
563 BitBlt (hClipDC, bx, r.top + (rh - bmp.bmHeight) / 2,
564 bmp.bmWidth, bmp.bmHeight, hdcBitmap, 0, 0, SRCCOPY);
565 DeleteDC (hdcBitmap);
568 if (iw) {
569 ImageList_DrawEx (infoPtr->himl, phdi->iImage, hClipDC,
570 ix, r.top + (rh - img_cy) / 2,
571 img_cx, img_cy, CLR_DEFAULT, CLR_DEFAULT, 0);
574 DeleteObject(hClipRgn);
575 ReleaseDC(infoPtr->hwndSelf, hClipDC);
578 if (((phdi->fmt & HDF_STRING)
579 || (!(phdi->fmt & (HDF_OWNERDRAW|HDF_STRING|HDF_BITMAP|
580 HDF_BITMAP_ON_RIGHT|HDF_IMAGE)))) /* no explicit format specified? */
581 && (phdi->pszText)) {
582 oldBkMode = SetBkMode(hdc, TRANSPARENT);
583 r.left = tx;
584 r.right = tx + tw;
585 if (theme) {
586 DrawThemeText(theme, hdc, HP_HEADERITEM, state, phdi->pszText,
587 -1, DT_LEFT|DT_END_ELLIPSIS|DT_VCENTER|DT_SINGLELINE,
588 0, &r);
589 } else {
590 DrawTextW (hdc, phdi->pszText, -1,
591 &r, DT_LEFT|DT_END_ELLIPSIS|DT_VCENTER|DT_SINGLELINE);
593 if (oldBkMode != TRANSPARENT)
594 SetBkMode(hdc, oldBkMode);
596 HEADER_FreeCallbackItems(phdi);
599 return phdi->rect.right;
602 static void
603 HEADER_DrawHotDivider(const HEADER_INFO *infoPtr, HDC hdc)
605 HBRUSH brush;
606 RECT r;
608 HEADER_GetHotDividerRect(infoPtr, &r);
609 brush = CreateSolidBrush(comctl32_color.clrHighlight);
610 FillRect(hdc, &r, brush);
611 DeleteObject(brush);
614 static void
615 HEADER_Refresh (HEADER_INFO *infoPtr, HDC hdc)
617 HFONT hFont, hOldFont;
618 RECT rect, rcRest;
619 HBRUSH hbrBk;
620 UINT i;
621 INT x;
622 LRESULT lCDFlags;
623 HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
625 if (!infoPtr->bRectsValid)
626 HEADER_SetItemBounds(infoPtr);
628 /* get rect for the bar, adjusted for the border */
629 GetClientRect (infoPtr->hwndSelf, &rect);
630 lCDFlags = HEADER_SendCtrlCustomDraw(infoPtr, CDDS_PREPAINT, hdc, &rect);
632 if (infoPtr->bDragging)
633 ImageList_DragShowNolock(FALSE);
635 hFont = infoPtr->hFont ? infoPtr->hFont : GetStockObject (SYSTEM_FONT);
636 hOldFont = SelectObject (hdc, hFont);
638 /* draw Background */
639 if (infoPtr->uNumItem == 0 && theme == NULL) {
640 hbrBk = GetSysColorBrush(COLOR_3DFACE);
641 FillRect(hdc, &rect, hbrBk);
644 x = rect.left;
645 for (i = 0; x <= rect.right && i < infoPtr->uNumItem; i++) {
646 int idx = HEADER_OrderToIndex(infoPtr,i);
647 if (RectVisible(hdc, &infoPtr->items[idx].rect))
648 HEADER_DrawItem(infoPtr, hdc, idx, infoPtr->iHotItem == idx, lCDFlags);
649 x = infoPtr->items[idx].rect.right;
652 rcRest = rect;
653 rcRest.left = x;
654 if ((x <= rect.right) && RectVisible(hdc, &rcRest) && (infoPtr->uNumItem > 0)) {
655 if (theme != NULL) {
656 DrawThemeBackground(theme, hdc, HP_HEADERITEM, HIS_NORMAL, &rcRest, NULL);
658 else if (infoPtr->dwStyle & HDS_FLAT) {
659 hbrBk = GetSysColorBrush(COLOR_3DFACE);
660 FillRect(hdc, &rcRest, hbrBk);
662 else
664 if (infoPtr->dwStyle & HDS_BUTTONS)
665 DrawEdge (hdc, &rcRest, EDGE_RAISED, BF_TOP|BF_LEFT|BF_BOTTOM|BF_SOFT|BF_MIDDLE);
666 else
667 DrawEdge (hdc, &rcRest, EDGE_ETCHED, BF_BOTTOM|BF_MIDDLE);
671 if (infoPtr->iHotDivider != -1)
672 HEADER_DrawHotDivider(infoPtr, hdc);
674 if (infoPtr->bDragging)
675 ImageList_DragShowNolock(TRUE);
676 SelectObject (hdc, hOldFont);
678 if (lCDFlags & CDRF_NOTIFYPOSTPAINT)
679 HEADER_SendCtrlCustomDraw(infoPtr, CDDS_POSTPAINT, hdc, &rect);
683 static void
684 HEADER_RefreshItem (HEADER_INFO *infoPtr, INT iItem)
686 if (!infoPtr->bRectsValid)
687 HEADER_SetItemBounds(infoPtr);
689 InvalidateRect(infoPtr->hwndSelf, &infoPtr->items[iItem].rect, FALSE);
693 static void
694 HEADER_InternalHitTest (const HEADER_INFO *infoPtr, const POINT *lpPt, UINT *pFlags, INT *pItem)
696 RECT rect, rcTest;
697 UINT iCount;
698 INT width;
699 BOOL bNoWidth;
701 GetClientRect (infoPtr->hwndSelf, &rect);
703 *pFlags = 0;
704 bNoWidth = FALSE;
705 if (PtInRect (&rect, *lpPt))
707 if (infoPtr->uNumItem == 0) {
708 *pFlags |= HHT_NOWHERE;
709 *pItem = 1;
710 TRACE("NOWHERE\n");
711 return;
713 else {
714 /* somewhere inside */
715 for (iCount = 0; iCount < infoPtr->uNumItem; iCount++) {
716 rect = infoPtr->items[iCount].rect;
717 width = rect.right - rect.left;
718 if (width == 0) {
719 bNoWidth = TRUE;
720 continue;
722 if (PtInRect (&rect, *lpPt)) {
723 if (width <= 2 * DIVIDER_WIDTH) {
724 *pFlags |= HHT_ONHEADER;
725 *pItem = iCount;
726 TRACE("ON HEADER %d\n", iCount);
727 return;
729 if (HEADER_IndexToOrder(infoPtr, iCount) > 0) {
730 rcTest = rect;
731 rcTest.right = rcTest.left + DIVIDER_WIDTH;
732 if (PtInRect (&rcTest, *lpPt)) {
733 if (HEADER_IsItemFixed(infoPtr, HEADER_PrevItem(infoPtr, iCount)))
735 *pFlags |= HHT_ONHEADER;
736 *pItem = iCount;
737 TRACE("ON HEADER %d\n", *pItem);
738 return;
740 if (bNoWidth) {
741 *pFlags |= HHT_ONDIVOPEN;
742 *pItem = HEADER_PrevItem(infoPtr, iCount);
743 TRACE("ON DIVOPEN %d\n", *pItem);
744 return;
746 else {
747 *pFlags |= HHT_ONDIVIDER;
748 *pItem = HEADER_PrevItem(infoPtr, iCount);
749 TRACE("ON DIVIDER %d\n", *pItem);
750 return;
754 rcTest = rect;
755 rcTest.left = rcTest.right - DIVIDER_WIDTH;
756 if (!HEADER_IsItemFixed(infoPtr, iCount) && PtInRect (&rcTest, *lpPt))
758 *pFlags |= HHT_ONDIVIDER;
759 *pItem = iCount;
760 TRACE("ON DIVIDER %d\n", *pItem);
761 return;
764 *pFlags |= HHT_ONHEADER;
765 *pItem = iCount;
766 TRACE("ON HEADER %d\n", iCount);
767 return;
771 /* check for last divider part (on nowhere) */
772 if (!HEADER_IsItemFixed(infoPtr, infoPtr->uNumItem - 1))
774 rect = infoPtr->items[infoPtr->uNumItem-1].rect;
775 rect.left = rect.right;
776 rect.right += DIVIDER_WIDTH;
777 if (PtInRect (&rect, *lpPt)) {
778 if (bNoWidth) {
779 *pFlags |= HHT_ONDIVOPEN;
780 *pItem = infoPtr->uNumItem - 1;
781 TRACE("ON DIVOPEN %d\n", *pItem);
782 return;
784 else {
785 *pFlags |= HHT_ONDIVIDER;
786 *pItem = infoPtr->uNumItem - 1;
787 TRACE("ON DIVIDER %d\n", *pItem);
788 return;
793 *pFlags |= HHT_NOWHERE;
794 *pItem = 1;
795 TRACE("NOWHERE\n");
796 return;
799 else {
800 if (lpPt->x < rect.left) {
801 TRACE("TO LEFT\n");
802 *pFlags |= HHT_TOLEFT;
804 else if (lpPt->x > rect.right) {
805 TRACE("TO RIGHT\n");
806 *pFlags |= HHT_TORIGHT;
809 if (lpPt->y < rect.top) {
810 TRACE("ABOVE\n");
811 *pFlags |= HHT_ABOVE;
813 else if (lpPt->y > rect.bottom) {
814 TRACE("BELOW\n");
815 *pFlags |= HHT_BELOW;
819 *pItem = 1;
820 TRACE("flags=0x%X\n", *pFlags);
821 return;
825 static void
826 HEADER_DrawTrackLine (const HEADER_INFO *infoPtr, HDC hdc, INT x)
828 RECT rect;
830 GetClientRect (infoPtr->hwndSelf, &rect);
831 PatBlt( hdc, x, rect.top, 1, rect.bottom - rect.top, DSTINVERT );
834 /***
835 * DESCRIPTION:
836 * Convert a HDITEM into the correct format (ANSI/Unicode) to send it in a notify
838 * PARAMETER(S):
839 * [I] infoPtr : the header that wants to send the notify
840 * [O] dest : The buffer to store the HDITEM for notify. It may be set to a HDITEMA of HDITEMW
841 * [I] src : The source HDITEM. It may be a HDITEMA or HDITEMW
842 * [I] fSourceUnicode : is src a HDITEMW or HDITEMA
843 * [O] ppvScratch : a pointer to a scratch buffer that needs to be freed after
844 * the HDITEM is no longer in use or NULL if none was needed
846 * NOTE: We depend on HDITEMA and HDITEMW having the same structure
848 static void HEADER_CopyHDItemForNotify(const HEADER_INFO *infoPtr, HDITEMW *dest,
849 const HDITEMW *src, BOOL fSourceUnicode, LPVOID *ppvScratch)
851 *ppvScratch = NULL;
852 *dest = *src;
854 if (src->mask & HDI_TEXT && src->pszText != LPSTR_TEXTCALLBACKW) /* covers TEXTCALLBACKA as well */
856 if (fSourceUnicode && infoPtr->nNotifyFormat != NFR_UNICODE)
858 dest->pszText = NULL;
859 Str_SetPtrWtoA((LPSTR *)&dest->pszText, src->pszText);
860 *ppvScratch = dest->pszText;
863 if (!fSourceUnicode && infoPtr->nNotifyFormat == NFR_UNICODE)
865 dest->pszText = NULL;
866 Str_SetPtrAtoW(&dest->pszText, (LPSTR)src->pszText);
867 *ppvScratch = dest->pszText;
872 static UINT HEADER_NotifyCodeWtoA(UINT code)
874 /* we use the fact that all the unicode messages are in HDN_FIRST_UNICODE..HDN_LAST*/
875 if (code >= HDN_LAST && code <= HDN_FIRST_UNICODE)
876 return code + HDN_UNICODE_OFFSET;
877 else
878 return code;
881 static LRESULT
882 HEADER_SendNotify(const HEADER_INFO *infoPtr, UINT code, NMHDR *nmhdr)
884 nmhdr->hwndFrom = infoPtr->hwndSelf;
885 nmhdr->idFrom = GetWindowLongPtrW (infoPtr->hwndSelf, GWLP_ID);
886 nmhdr->code = code;
888 return SendMessageW(infoPtr->hwndNotify, WM_NOTIFY,
889 nmhdr->idFrom, (LPARAM)nmhdr);
892 static BOOL
893 HEADER_SendSimpleNotify (const HEADER_INFO *infoPtr, UINT code)
895 NMHDR nmhdr;
896 return (BOOL)HEADER_SendNotify(infoPtr, code, &nmhdr);
899 static LRESULT
900 HEADER_SendCtrlCustomDraw(const HEADER_INFO *infoPtr, DWORD dwDrawStage, HDC hdc, const RECT *rect)
902 NMCUSTOMDRAW nm;
903 nm.dwDrawStage = dwDrawStage;
904 nm.hdc = hdc;
905 nm.rc = *rect;
906 nm.dwItemSpec = 0;
907 nm.uItemState = 0;
908 nm.lItemlParam = 0;
910 return HEADER_SendNotify(infoPtr, NM_CUSTOMDRAW, (NMHDR *)&nm);
913 static BOOL
914 HEADER_SendNotifyWithHDItemT(const HEADER_INFO *infoPtr, UINT code, INT iItem, HDITEMW *lpItem)
916 NMHEADERW nmhdr;
918 if (infoPtr->nNotifyFormat != NFR_UNICODE)
919 code = HEADER_NotifyCodeWtoA(code);
920 nmhdr.iItem = iItem;
921 nmhdr.iButton = 0;
922 nmhdr.pitem = lpItem;
924 return (BOOL)HEADER_SendNotify(infoPtr, code, (NMHDR *)&nmhdr);
927 static BOOL
928 HEADER_SendNotifyWithIntFieldT(const HEADER_INFO *infoPtr, UINT code, INT iItem, INT mask, INT iValue)
930 HDITEMW nmitem;
932 /* copying only the iValue should be ok but to make the code more robust we copy everything */
933 nmitem.cxy = infoPtr->items[iItem].cxy;
934 nmitem.hbm = infoPtr->items[iItem].hbm;
935 nmitem.pszText = NULL;
936 nmitem.cchTextMax = 0;
937 nmitem.fmt = infoPtr->items[iItem].fmt;
938 nmitem.lParam = infoPtr->items[iItem].lParam;
939 nmitem.iOrder = infoPtr->items[iItem].iOrder;
940 nmitem.iImage = infoPtr->items[iItem].iImage;
942 nmitem.mask = mask;
943 switch (mask)
945 case HDI_WIDTH:
946 nmitem.cxy = iValue;
947 break;
948 case HDI_ORDER:
949 nmitem.iOrder = iValue;
950 break;
951 default:
952 ERR("invalid mask value 0x%x\n", iValue);
955 return HEADER_SendNotifyWithHDItemT(infoPtr, code, iItem, &nmitem);
959 * Prepare callback items
960 * depends on NMHDDISPINFOW having same structure as NMHDDISPINFOA
961 * (so we handle the two cases only doing a specific cast for pszText).
962 * Checks if any of the required fields is a callback. If this is the case sends a
963 * NMHDISPINFO notify to retrieve these items. The items are stored in the
964 * HEADER_ITEM pszText and iImage fields. They should be freed with
965 * HEADER_FreeCallbackItems.
967 * @param hwnd : hwnd header container handler
968 * @param iItem : the header item id
969 * @param reqMask : required fields. If any of them is callback this function will fetch it
971 * @return TRUE on success, else FALSE
973 static BOOL
974 HEADER_PrepareCallbackItems(const HEADER_INFO *infoPtr, INT iItem, INT reqMask)
976 HEADER_ITEM *lpItem = &infoPtr->items[iItem];
977 DWORD mask = reqMask & lpItem->callbackMask;
978 NMHDDISPINFOW dispInfo;
979 void *pvBuffer = NULL;
981 if (mask == 0)
982 return TRUE;
983 if (mask&HDI_TEXT && lpItem->pszText != NULL)
985 ERR("(): function called without a call to FreeCallbackItems\n");
986 heap_free(lpItem->pszText);
987 lpItem->pszText = NULL;
990 memset(&dispInfo, 0, sizeof(NMHDDISPINFOW));
991 dispInfo.hdr.hwndFrom = infoPtr->hwndSelf;
992 dispInfo.hdr.idFrom = GetWindowLongPtrW (infoPtr->hwndSelf, GWLP_ID);
993 if (infoPtr->nNotifyFormat == NFR_UNICODE)
995 dispInfo.hdr.code = HDN_GETDISPINFOW;
996 if (mask & HDI_TEXT)
997 pvBuffer = heap_alloc_zero(MAX_HEADER_TEXT_LEN * sizeof(WCHAR));
999 else
1001 dispInfo.hdr.code = HDN_GETDISPINFOA;
1002 if (mask & HDI_TEXT)
1003 pvBuffer = heap_alloc_zero(MAX_HEADER_TEXT_LEN * sizeof(CHAR));
1005 dispInfo.pszText = pvBuffer;
1006 dispInfo.cchTextMax = (pvBuffer!=NULL?MAX_HEADER_TEXT_LEN:0);
1007 dispInfo.iItem = iItem;
1008 dispInfo.mask = mask;
1009 dispInfo.lParam = lpItem->lParam;
1011 TRACE("Sending HDN_GETDISPINFO%c\n", infoPtr->nNotifyFormat == NFR_UNICODE?'W':'A');
1012 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, dispInfo.hdr.idFrom, (LPARAM)&dispInfo);
1014 TRACE("SendMessage returns(mask:0x%x,str:%s,lParam:%p)\n",
1015 dispInfo.mask,
1016 (infoPtr->nNotifyFormat == NFR_UNICODE ? debugstr_w(dispInfo.pszText) : (LPSTR) dispInfo.pszText),
1017 (void*) dispInfo.lParam);
1019 if (mask & HDI_IMAGE)
1020 lpItem->iImage = dispInfo.iImage;
1021 if (mask & HDI_TEXT)
1023 if (infoPtr->nNotifyFormat == NFR_UNICODE)
1025 lpItem->pszText = pvBuffer;
1027 /* the user might have used his own buffer */
1028 if (dispInfo.pszText != lpItem->pszText)
1029 Str_GetPtrW(dispInfo.pszText, lpItem->pszText, MAX_HEADER_TEXT_LEN);
1031 else
1033 Str_SetPtrAtoW(&lpItem->pszText, (LPSTR)dispInfo.pszText);
1034 heap_free(pvBuffer);
1038 if (dispInfo.mask & HDI_DI_SETITEM)
1040 /* make the items permanent */
1041 lpItem->callbackMask &= ~dispInfo.mask;
1044 return TRUE;
1047 /***
1048 * DESCRIPTION:
1049 * Free the items that might be allocated with HEADER_PrepareCallbackItems
1051 * PARAMETER(S):
1052 * [I] lpItem : the item to free the data
1055 static void
1056 HEADER_FreeCallbackItems(HEADER_ITEM *lpItem)
1058 if (lpItem->callbackMask&HDI_TEXT)
1060 heap_free(lpItem->pszText);
1061 lpItem->pszText = NULL;
1064 if (lpItem->callbackMask&HDI_IMAGE)
1065 lpItem->iImage = I_IMAGECALLBACK;
1068 static HIMAGELIST
1069 HEADER_CreateDragImage (HEADER_INFO *infoPtr, INT iItem)
1071 HEADER_ITEM *lpItem;
1072 HIMAGELIST himl;
1073 HBITMAP hMemory, hOldBitmap;
1074 LRESULT lCDFlags;
1075 RECT rc;
1076 HDC hMemoryDC;
1077 HDC hDeviceDC;
1078 int height, width;
1079 HFONT hFont;
1081 if (iItem >= infoPtr->uNumItem)
1082 return NULL;
1084 if (!infoPtr->bRectsValid)
1085 HEADER_SetItemBounds(infoPtr);
1087 lpItem = &infoPtr->items[iItem];
1088 width = lpItem->rect.right - lpItem->rect.left;
1089 height = lpItem->rect.bottom - lpItem->rect.top;
1091 hDeviceDC = GetDC(NULL);
1092 hMemoryDC = CreateCompatibleDC(hDeviceDC);
1093 hMemory = CreateCompatibleBitmap(hDeviceDC, width, height);
1094 ReleaseDC(NULL, hDeviceDC);
1095 hOldBitmap = SelectObject(hMemoryDC, hMemory);
1096 SetViewportOrgEx(hMemoryDC, -lpItem->rect.left, -lpItem->rect.top, NULL);
1097 hFont = infoPtr->hFont ? infoPtr->hFont : GetStockObject(SYSTEM_FONT);
1098 SelectObject(hMemoryDC, hFont);
1100 GetClientRect(infoPtr->hwndSelf, &rc);
1101 lCDFlags = HEADER_SendCtrlCustomDraw(infoPtr, CDDS_PREPAINT, hMemoryDC, &rc);
1102 HEADER_DrawItem(infoPtr, hMemoryDC, iItem, FALSE, lCDFlags);
1103 if (lCDFlags & CDRF_NOTIFYPOSTPAINT)
1104 HEADER_SendCtrlCustomDraw(infoPtr, CDDS_POSTPAINT, hMemoryDC, &rc);
1106 hMemory = SelectObject(hMemoryDC, hOldBitmap);
1107 DeleteDC(hMemoryDC);
1109 if (hMemory == NULL) /* if anything failed */
1110 return NULL;
1112 himl = ImageList_Create(width, height, ILC_COLORDDB, 1, 1);
1113 ImageList_Add(himl, hMemory, NULL);
1114 DeleteObject(hMemory);
1115 return himl;
1118 static LRESULT
1119 HEADER_SetHotDivider(HEADER_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
1121 INT iDivider;
1122 RECT r;
1124 if (wParam)
1126 POINT pt;
1127 UINT flags;
1128 pt.x = (INT)(SHORT)LOWORD(lParam);
1129 pt.y = 0;
1130 HEADER_InternalHitTest (infoPtr, &pt, &flags, &iDivider);
1132 if (flags & HHT_TOLEFT)
1133 iDivider = 0;
1134 else if (flags & HHT_NOWHERE || flags & HHT_TORIGHT)
1135 iDivider = infoPtr->uNumItem;
1136 else
1138 HEADER_ITEM *lpItem = &infoPtr->items[iDivider];
1139 if (pt.x > (lpItem->rect.left+lpItem->rect.right)/2)
1140 iDivider = HEADER_NextItem(infoPtr, iDivider);
1143 else
1144 iDivider = (INT)lParam;
1146 /* Note; wParam==FALSE, lParam==-1 is valid and is used to clear the hot divider */
1147 if (iDivider<-1 || iDivider>(int)infoPtr->uNumItem)
1148 return iDivider;
1150 if (iDivider != infoPtr->iHotDivider)
1152 if (infoPtr->iHotDivider != -1)
1154 HEADER_GetHotDividerRect(infoPtr, &r);
1155 InvalidateRect(infoPtr->hwndSelf, &r, FALSE);
1157 infoPtr->iHotDivider = iDivider;
1158 if (iDivider != -1)
1160 HEADER_GetHotDividerRect(infoPtr, &r);
1161 InvalidateRect(infoPtr->hwndSelf, &r, FALSE);
1164 return iDivider;
1167 static LRESULT
1168 HEADER_DeleteItem (HEADER_INFO *infoPtr, INT iItem)
1170 INT iOrder;
1171 UINT i;
1173 TRACE("[iItem=%d]\n", iItem);
1175 if ((iItem < 0) || (iItem >= (INT)infoPtr->uNumItem))
1176 return FALSE;
1178 for (i = 0; i < infoPtr->uNumItem; i++)
1179 TRACE("%d: order=%d, iOrder=%d, ->iOrder=%d\n", i, infoPtr->order[i], infoPtr->items[i].iOrder, infoPtr->items[infoPtr->order[i]].iOrder);
1181 iOrder = infoPtr->items[iItem].iOrder;
1182 heap_free(infoPtr->items[iItem].pszText);
1184 infoPtr->uNumItem--;
1185 memmove(&infoPtr->items[iItem], &infoPtr->items[iItem + 1],
1186 (infoPtr->uNumItem - iItem) * sizeof(HEADER_ITEM));
1187 memmove(&infoPtr->order[iOrder], &infoPtr->order[iOrder + 1],
1188 (infoPtr->uNumItem - iOrder) * sizeof(INT));
1189 infoPtr->items = heap_realloc(infoPtr->items, sizeof(HEADER_ITEM) * infoPtr->uNumItem);
1190 infoPtr->order = heap_realloc(infoPtr->order, sizeof(INT) * infoPtr->uNumItem);
1192 /* Correct the orders */
1193 for (i = 0; i < infoPtr->uNumItem; i++)
1195 if (infoPtr->order[i] > iItem)
1196 infoPtr->order[i]--;
1197 if (i >= iOrder)
1198 infoPtr->items[infoPtr->order[i]].iOrder = i;
1200 for (i = 0; i < infoPtr->uNumItem; i++)
1201 TRACE("%d: order=%d, iOrder=%d, ->iOrder=%d\n", i, infoPtr->order[i], infoPtr->items[i].iOrder, infoPtr->items[infoPtr->order[i]].iOrder);
1203 HEADER_SetItemBounds (infoPtr);
1204 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1206 return TRUE;
1210 static LRESULT
1211 HEADER_GetImageList (const HEADER_INFO *infoPtr)
1213 return (LRESULT)infoPtr->himl;
1217 static LRESULT
1218 HEADER_GetItemT (const HEADER_INFO *infoPtr, INT nItem, LPHDITEMW phdi, BOOL bUnicode)
1220 HEADER_ITEM *lpItem;
1221 UINT mask;
1223 if (!phdi)
1224 return FALSE;
1226 TRACE("[nItem=%d]\n", nItem);
1228 mask = phdi->mask;
1229 if (mask == 0)
1230 return TRUE;
1232 if ((nItem < 0) || (nItem >= (INT)infoPtr->uNumItem))
1233 return FALSE;
1235 if (mask & HDI_UNKNOWN_FIELDS)
1237 TRACE("mask %x contains unknown fields. Using only comctl32 4.0 fields\n", mask);
1238 mask &= HDI_COMCTL32_4_0_FIELDS;
1241 lpItem = &infoPtr->items[nItem];
1242 HEADER_PrepareCallbackItems(infoPtr, nItem, mask);
1244 if (mask & HDI_BITMAP)
1245 phdi->hbm = lpItem->hbm;
1247 if (mask & HDI_FORMAT)
1248 phdi->fmt = lpItem->fmt;
1250 if (mask & HDI_WIDTH)
1251 phdi->cxy = lpItem->cxy;
1253 if (mask & HDI_LPARAM)
1254 phdi->lParam = lpItem->lParam;
1256 if (mask & HDI_IMAGE)
1257 phdi->iImage = lpItem->iImage;
1259 if (mask & HDI_ORDER)
1260 phdi->iOrder = lpItem->iOrder;
1262 if (mask & HDI_TEXT)
1264 if (bUnicode)
1265 Str_GetPtrW (lpItem->pszText, phdi->pszText, phdi->cchTextMax);
1266 else
1267 Str_GetPtrWtoA (lpItem->pszText, (LPSTR)phdi->pszText, phdi->cchTextMax);
1270 HEADER_FreeCallbackItems(lpItem);
1271 return TRUE;
1275 static inline LRESULT
1276 HEADER_GetItemCount (const HEADER_INFO *infoPtr)
1278 return infoPtr->uNumItem;
1282 static LRESULT
1283 HEADER_GetItemRect (const HEADER_INFO *infoPtr, INT iItem, LPRECT lpRect)
1285 if ((iItem < 0) || (iItem >= (INT)infoPtr->uNumItem))
1286 return FALSE;
1288 lpRect->left = infoPtr->items[iItem].rect.left;
1289 lpRect->right = infoPtr->items[iItem].rect.right;
1290 lpRect->top = infoPtr->items[iItem].rect.top;
1291 lpRect->bottom = infoPtr->items[iItem].rect.bottom;
1293 return TRUE;
1297 static LRESULT
1298 HEADER_GetOrderArray(const HEADER_INFO *infoPtr, INT size, LPINT order)
1300 if ((UINT)size <infoPtr->uNumItem)
1301 return FALSE;
1303 memcpy(order, infoPtr->order, infoPtr->uNumItem * sizeof(INT));
1304 return TRUE;
1307 /* Returns index of first duplicate 'value' from [0,to) range,
1308 or -1 if there isn't any */
1309 static INT has_duplicate(const INT *array, INT to, INT value)
1311 INT i;
1312 for(i = 0; i < to; i++)
1313 if (array[i] == value) return i;
1314 return -1;
1317 /* returns next available value from [0,max] not to duplicate in [0,to) */
1318 static INT get_nextvalue(const INT *array, INT to, INT max)
1320 INT i;
1321 for(i = 0; i < max; i++)
1322 if (has_duplicate(array, to, i) == -1) return i;
1323 return 0;
1326 static LRESULT
1327 HEADER_SetOrderArray(HEADER_INFO *infoPtr, INT size, const INT *order)
1329 HEADER_ITEM *lpItem;
1330 INT i;
1332 if ((UINT)size != infoPtr->uNumItem)
1333 return FALSE;
1335 if (TRACE_ON(header))
1337 TRACE("count=%d, order array={", size);
1338 for (i = 0; i < size; i++)
1339 TRACE("%d%c", order[i], i != size-1 ? ',' : '}');
1340 TRACE("\n");
1343 for (i=0; i<size; i++)
1345 if (order[i] >= size || order[i] < 0)
1346 /* on invalid index get next available */
1347 /* FIXME: if i==0 array item is out of range behaviour is
1348 different, see tests */
1349 infoPtr->order[i] = get_nextvalue(infoPtr->order, i, size);
1350 else
1352 INT j, dup;
1354 infoPtr->order[i] = order[i];
1355 j = i;
1356 /* remove duplicates */
1357 while ((dup = has_duplicate(infoPtr->order, j, order[j])) != -1)
1359 INT next;
1361 next = get_nextvalue(infoPtr->order, j, size);
1362 infoPtr->order[dup] = next;
1363 j--;
1367 /* sync with item data */
1368 for (i=0; i<size; i++)
1370 lpItem = &infoPtr->items[infoPtr->order[i]];
1371 lpItem->iOrder = i;
1373 HEADER_SetItemBounds(infoPtr);
1374 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1375 return TRUE;
1378 static inline LRESULT
1379 HEADER_GetUnicodeFormat (const HEADER_INFO *infoPtr)
1381 return (infoPtr->nNotifyFormat == NFR_UNICODE);
1385 static LRESULT
1386 HEADER_HitTest (const HEADER_INFO *infoPtr, LPHDHITTESTINFO phti)
1388 UINT outside = HHT_NOWHERE | HHT_ABOVE | HHT_BELOW | HHT_TOLEFT | HHT_TORIGHT;
1390 HEADER_InternalHitTest (infoPtr, &phti->pt, &phti->flags, &phti->iItem);
1392 if (phti->flags & outside)
1393 return phti->iItem = -1;
1394 else
1395 return phti->iItem;
1399 static LRESULT
1400 HEADER_InsertItemT (HEADER_INFO *infoPtr, INT nItem, const HDITEMW *phdi, BOOL bUnicode)
1402 HEADER_ITEM *lpItem;
1403 INT iOrder;
1404 UINT i;
1405 UINT copyMask;
1407 if ((phdi == NULL) || (nItem < 0) || (phdi->mask == 0))
1408 return -1;
1410 if (nItem > infoPtr->uNumItem)
1411 nItem = infoPtr->uNumItem;
1413 iOrder = (phdi->mask & HDI_ORDER) ? phdi->iOrder : nItem;
1414 if (iOrder < 0)
1415 iOrder = 0;
1416 else if (infoPtr->uNumItem < iOrder)
1417 iOrder = infoPtr->uNumItem;
1419 infoPtr->uNumItem++;
1420 infoPtr->items = heap_realloc(infoPtr->items, sizeof(HEADER_ITEM) * infoPtr->uNumItem);
1421 infoPtr->order = heap_realloc(infoPtr->order, sizeof(INT) * infoPtr->uNumItem);
1423 /* make space for the new item */
1424 memmove(&infoPtr->items[nItem + 1], &infoPtr->items[nItem],
1425 (infoPtr->uNumItem - nItem - 1) * sizeof(HEADER_ITEM));
1426 memmove(&infoPtr->order[iOrder + 1], &infoPtr->order[iOrder],
1427 (infoPtr->uNumItem - iOrder - 1) * sizeof(INT));
1429 /* update the order array */
1430 infoPtr->order[iOrder] = nItem;
1431 for (i = 0; i < infoPtr->uNumItem; i++)
1433 if (i != iOrder && infoPtr->order[i] >= nItem)
1434 infoPtr->order[i]++;
1435 infoPtr->items[infoPtr->order[i]].iOrder = i;
1438 lpItem = &infoPtr->items[nItem];
1439 ZeroMemory(lpItem, sizeof(HEADER_ITEM));
1440 /* cxy, fmt and lParam are copied even if not in the HDITEM mask */
1441 copyMask = phdi->mask | HDI_WIDTH | HDI_FORMAT | HDI_LPARAM;
1442 HEADER_StoreHDItemInHeader(lpItem, copyMask, phdi, bUnicode);
1443 lpItem->iOrder = iOrder;
1445 /* set automatically some format bits */
1446 if (phdi->mask & HDI_TEXT)
1447 lpItem->fmt |= HDF_STRING;
1448 else
1449 lpItem->fmt &= ~HDF_STRING;
1451 if (lpItem->hbm != NULL)
1452 lpItem->fmt |= HDF_BITMAP;
1453 else
1454 lpItem->fmt &= ~HDF_BITMAP;
1456 if (phdi->mask & HDI_IMAGE)
1457 lpItem->fmt |= HDF_IMAGE;
1459 HEADER_SetItemBounds (infoPtr);
1460 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1462 return nItem;
1466 static LRESULT
1467 HEADER_Layout (HEADER_INFO *infoPtr, LPHDLAYOUT lpLayout)
1469 lpLayout->pwpos->hwnd = infoPtr->hwndSelf;
1470 lpLayout->pwpos->hwndInsertAfter = 0;
1471 lpLayout->pwpos->x = lpLayout->prc->left;
1472 lpLayout->pwpos->y = lpLayout->prc->top;
1473 lpLayout->pwpos->cx = lpLayout->prc->right - lpLayout->prc->left;
1474 if (infoPtr->dwStyle & HDS_HIDDEN)
1475 lpLayout->pwpos->cy = 0;
1476 else {
1477 lpLayout->pwpos->cy = infoPtr->nHeight;
1478 lpLayout->prc->top += infoPtr->nHeight;
1480 lpLayout->pwpos->flags = SWP_NOZORDER;
1482 TRACE("Layout x=%d y=%d cx=%d cy=%d\n",
1483 lpLayout->pwpos->x, lpLayout->pwpos->y,
1484 lpLayout->pwpos->cx, lpLayout->pwpos->cy);
1486 infoPtr->bRectsValid = FALSE;
1488 return TRUE;
1492 static LRESULT
1493 HEADER_SetImageList (HEADER_INFO *infoPtr, HIMAGELIST himl)
1495 HIMAGELIST himlOld;
1497 TRACE("(himl %p)\n", himl);
1498 himlOld = infoPtr->himl;
1499 infoPtr->himl = himl;
1501 /* FIXME: Refresh needed??? */
1503 return (LRESULT)himlOld;
1507 static LRESULT
1508 HEADER_GetBitmapMargin(const HEADER_INFO *infoPtr)
1510 return infoPtr->iMargin;
1513 static LRESULT
1514 HEADER_SetBitmapMargin(HEADER_INFO *infoPtr, INT iMargin)
1516 INT oldMargin = infoPtr->iMargin;
1518 infoPtr->iMargin = iMargin;
1520 return oldMargin;
1523 static LRESULT
1524 HEADER_SetItemT (HEADER_INFO *infoPtr, INT nItem, const HDITEMW *phdi, BOOL bUnicode)
1526 HEADER_ITEM *lpItem;
1527 HDITEMW hdNotify;
1528 void *pvScratch;
1530 if (phdi == NULL)
1531 return FALSE;
1532 if ((nItem < 0) || (nItem >= (INT)infoPtr->uNumItem))
1533 return FALSE;
1535 TRACE("[nItem=%d]\n", nItem);
1537 HEADER_CopyHDItemForNotify(infoPtr, &hdNotify, phdi, bUnicode, &pvScratch);
1538 if (HEADER_SendNotifyWithHDItemT(infoPtr, HDN_ITEMCHANGINGW, nItem, &hdNotify))
1540 heap_free(pvScratch);
1541 return FALSE;
1544 lpItem = &infoPtr->items[nItem];
1545 HEADER_StoreHDItemInHeader(lpItem, phdi->mask, phdi, bUnicode);
1547 if (phdi->mask & HDI_ORDER)
1548 if (phdi->iOrder >= 0 && phdi->iOrder < infoPtr->uNumItem)
1549 HEADER_ChangeItemOrder(infoPtr, nItem, phdi->iOrder);
1551 HEADER_SendNotifyWithHDItemT(infoPtr, HDN_ITEMCHANGEDW, nItem, &hdNotify);
1553 HEADER_SetItemBounds (infoPtr);
1555 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1557 heap_free(pvScratch);
1558 return TRUE;
1561 static inline LRESULT
1562 HEADER_SetUnicodeFormat (HEADER_INFO *infoPtr, WPARAM wParam)
1564 BOOL bTemp = (infoPtr->nNotifyFormat == NFR_UNICODE);
1566 infoPtr->nNotifyFormat = ((BOOL)wParam ? NFR_UNICODE : NFR_ANSI);
1568 return bTemp;
1572 static LRESULT
1573 HEADER_Create (HWND hwnd, const CREATESTRUCTW *lpcs)
1575 HEADER_INFO *infoPtr;
1576 TEXTMETRICW tm;
1577 HFONT hOldFont;
1578 HDC hdc;
1580 infoPtr = heap_alloc_zero (sizeof(*infoPtr));
1581 SetWindowLongPtrW (hwnd, 0, (DWORD_PTR)infoPtr);
1583 infoPtr->hwndSelf = hwnd;
1584 infoPtr->hwndNotify = lpcs->hwndParent;
1585 infoPtr->uNumItem = 0;
1586 infoPtr->hFont = 0;
1587 infoPtr->items = 0;
1588 infoPtr->order = 0;
1589 infoPtr->bRectsValid = FALSE;
1590 infoPtr->hcurArrow = LoadCursorW (0, (LPWSTR)IDC_ARROW);
1591 infoPtr->hcurDivider = LoadCursorW (COMCTL32_hModule, MAKEINTRESOURCEW(IDC_DIVIDER));
1592 infoPtr->hcurDivopen = LoadCursorW (COMCTL32_hModule, MAKEINTRESOURCEW(IDC_DIVIDEROPEN));
1593 infoPtr->bPressed = FALSE;
1594 infoPtr->bTracking = FALSE;
1595 infoPtr->dwStyle = lpcs->style;
1596 infoPtr->iMoveItem = 0;
1597 infoPtr->himl = 0;
1598 infoPtr->iHotItem = -1;
1599 infoPtr->iHotDivider = -1;
1600 infoPtr->iMargin = 3*GetSystemMetrics(SM_CXEDGE);
1601 infoPtr->nNotifyFormat =
1602 SendMessageW (infoPtr->hwndNotify, WM_NOTIFYFORMAT, (WPARAM)hwnd, NF_QUERY);
1603 infoPtr->filter_change_timeout = 1000;
1605 hdc = GetDC (0);
1606 hOldFont = SelectObject (hdc, GetStockObject (SYSTEM_FONT));
1607 GetTextMetricsW (hdc, &tm);
1608 infoPtr->nHeight = tm.tmHeight + VERT_BORDER;
1609 SelectObject (hdc, hOldFont);
1610 ReleaseDC (0, hdc);
1612 OpenThemeData(hwnd, themeClass);
1614 return 0;
1618 static LRESULT
1619 HEADER_Destroy (HEADER_INFO *infoPtr)
1621 HTHEME theme = GetWindowTheme(infoPtr->hwndSelf);
1622 CloseThemeData(theme);
1623 return 0;
1626 static LRESULT
1627 HEADER_NCDestroy (HEADER_INFO *infoPtr)
1629 HEADER_ITEM *lpItem;
1630 INT nItem;
1632 if (infoPtr->items) {
1633 lpItem = infoPtr->items;
1634 for (nItem = 0; nItem < infoPtr->uNumItem; nItem++, lpItem++)
1635 heap_free(lpItem->pszText);
1636 heap_free(infoPtr->items);
1639 heap_free(infoPtr->order);
1641 SetWindowLongPtrW (infoPtr->hwndSelf, 0, 0);
1642 heap_free(infoPtr);
1644 return 0;
1648 static inline LRESULT
1649 HEADER_GetFont (const HEADER_INFO *infoPtr)
1651 return (LRESULT)infoPtr->hFont;
1655 static BOOL
1656 HEADER_IsDragDistance(const HEADER_INFO *infoPtr, const POINT *pt)
1658 /* Windows allows for a mouse movement before starting the drag. We use the
1659 * SM_CXDOUBLECLICK/SM_CYDOUBLECLICK as that distance.
1661 return (abs(infoPtr->ptLButtonDown.x - pt->x)>GetSystemMetrics(SM_CXDOUBLECLK) ||
1662 abs(infoPtr->ptLButtonDown.y - pt->y)>GetSystemMetrics(SM_CYDOUBLECLK));
1665 static LRESULT
1666 HEADER_LButtonDblClk (const HEADER_INFO *infoPtr, INT x, INT y)
1668 POINT pt;
1669 UINT flags;
1670 INT nItem;
1672 pt.x = x;
1673 pt.y = y;
1674 HEADER_InternalHitTest (infoPtr, &pt, &flags, &nItem);
1676 if ((infoPtr->dwStyle & HDS_BUTTONS) && (flags == HHT_ONHEADER))
1677 HEADER_SendNotifyWithHDItemT(infoPtr, HDN_ITEMDBLCLICKW, nItem, NULL);
1678 else if ((flags == HHT_ONDIVIDER) || (flags == HHT_ONDIVOPEN))
1679 HEADER_SendNotifyWithHDItemT(infoPtr, HDN_DIVIDERDBLCLICKW, nItem, NULL);
1681 return 0;
1685 static LRESULT
1686 HEADER_LButtonDown (HEADER_INFO *infoPtr, INT x, INT y)
1688 POINT pt;
1689 UINT flags;
1690 INT nItem;
1691 HDC hdc;
1693 pt.x = x;
1694 pt.y = y;
1695 HEADER_InternalHitTest (infoPtr, &pt, &flags, &nItem);
1697 if ((infoPtr->dwStyle & HDS_BUTTONS) && (flags == HHT_ONHEADER)) {
1698 SetCapture (infoPtr->hwndSelf);
1699 infoPtr->bCaptured = TRUE;
1700 infoPtr->bPressed = TRUE;
1701 infoPtr->bDragging = FALSE;
1702 infoPtr->iMoveItem = nItem;
1703 infoPtr->ptLButtonDown = pt;
1705 infoPtr->items[nItem].bDown = TRUE;
1707 /* Send WM_CUSTOMDRAW */
1708 hdc = GetDC (infoPtr->hwndSelf);
1709 HEADER_RefreshItem (infoPtr, nItem);
1710 ReleaseDC (infoPtr->hwndSelf, hdc);
1712 TRACE("Pressed item %d.\n", nItem);
1714 else if ((flags == HHT_ONDIVIDER) || (flags == HHT_ONDIVOPEN)) {
1715 INT iCurrWidth = infoPtr->items[nItem].cxy;
1716 if (!HEADER_SendNotifyWithIntFieldT(infoPtr, HDN_BEGINTRACKW, nItem, HDI_WIDTH, iCurrWidth))
1718 SetCapture (infoPtr->hwndSelf);
1719 infoPtr->bCaptured = TRUE;
1720 infoPtr->bTracking = TRUE;
1721 infoPtr->iMoveItem = nItem;
1722 infoPtr->xTrackOffset = infoPtr->items[nItem].rect.right - pt.x;
1724 if (!(infoPtr->dwStyle & HDS_FULLDRAG)) {
1725 infoPtr->xOldTrack = infoPtr->items[nItem].rect.right;
1726 hdc = GetDC (infoPtr->hwndSelf);
1727 HEADER_DrawTrackLine (infoPtr, hdc, infoPtr->xOldTrack);
1728 ReleaseDC (infoPtr->hwndSelf, hdc);
1731 TRACE("Begin tracking item %d.\n", nItem);
1735 return 0;
1739 static LRESULT
1740 HEADER_LButtonUp (HEADER_INFO *infoPtr, INT x, INT y)
1742 POINT pt;
1743 UINT flags;
1744 INT nItem;
1745 HDC hdc;
1747 pt.x = x;
1748 pt.y = y;
1749 HEADER_InternalHitTest (infoPtr, &pt, &flags, &nItem);
1751 if (infoPtr->bPressed) {
1753 infoPtr->items[infoPtr->iMoveItem].bDown = FALSE;
1755 if (infoPtr->bDragging)
1757 HEADER_ITEM *lpItem = &infoPtr->items[infoPtr->iMoveItem];
1758 INT iNewOrder;
1760 ImageList_DragShowNolock(FALSE);
1761 ImageList_EndDrag();
1763 if (infoPtr->iHotDivider == -1)
1764 iNewOrder = -1;
1765 else if (infoPtr->iHotDivider == infoPtr->uNumItem)
1766 iNewOrder = infoPtr->uNumItem-1;
1767 else
1769 iNewOrder = HEADER_IndexToOrder(infoPtr, infoPtr->iHotDivider);
1770 if (iNewOrder > lpItem->iOrder)
1771 iNewOrder--;
1774 if (iNewOrder != -1 &&
1775 !HEADER_SendNotifyWithIntFieldT(infoPtr, HDN_ENDDRAG, infoPtr->iMoveItem, HDI_ORDER, iNewOrder))
1777 HEADER_ChangeItemOrder(infoPtr, infoPtr->iMoveItem, iNewOrder);
1778 infoPtr->bRectsValid = FALSE;
1779 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1781 else
1782 InvalidateRect(infoPtr->hwndSelf, &infoPtr->items[infoPtr->iMoveItem].rect, FALSE);
1784 infoPtr->bDragging = FALSE;
1785 HEADER_SetHotDivider(infoPtr, FALSE, -1);
1787 else
1789 hdc = GetDC (infoPtr->hwndSelf);
1790 HEADER_RefreshItem (infoPtr, infoPtr->iMoveItem);
1791 ReleaseDC (infoPtr->hwndSelf, hdc);
1793 if (!(infoPtr->dwStyle & HDS_DRAGDROP) || !HEADER_IsDragDistance(infoPtr, &pt))
1794 HEADER_SendNotifyWithHDItemT(infoPtr, HDN_ITEMCLICKW, infoPtr->iMoveItem, NULL);
1797 TRACE("Released item %d.\n", infoPtr->iMoveItem);
1798 infoPtr->bPressed = FALSE;
1800 else if (infoPtr->bTracking) {
1801 INT iNewWidth = pt.x - infoPtr->items[infoPtr->iMoveItem].rect.left + infoPtr->xTrackOffset;
1802 if (iNewWidth < 0)
1803 iNewWidth = 0;
1804 TRACE("End tracking item %d.\n", infoPtr->iMoveItem);
1805 infoPtr->bTracking = FALSE;
1807 HEADER_SendNotifyWithIntFieldT(infoPtr, HDN_ENDTRACKW, infoPtr->iMoveItem, HDI_WIDTH, iNewWidth);
1809 if (!(infoPtr->dwStyle & HDS_FULLDRAG)) {
1810 hdc = GetDC (infoPtr->hwndSelf);
1811 HEADER_DrawTrackLine (infoPtr, hdc, infoPtr->xOldTrack);
1812 ReleaseDC (infoPtr->hwndSelf, hdc);
1815 if (!HEADER_SendNotifyWithIntFieldT(infoPtr, HDN_ITEMCHANGINGW, infoPtr->iMoveItem, HDI_WIDTH, iNewWidth))
1817 infoPtr->items[infoPtr->iMoveItem].cxy = iNewWidth;
1818 HEADER_SendNotifyWithIntFieldT(infoPtr, HDN_ITEMCHANGEDW, infoPtr->iMoveItem, HDI_WIDTH, iNewWidth);
1821 HEADER_SetItemBounds (infoPtr);
1822 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1825 if (infoPtr->bCaptured) {
1826 infoPtr->bCaptured = FALSE;
1827 ReleaseCapture ();
1828 HEADER_SendSimpleNotify (infoPtr, NM_RELEASEDCAPTURE);
1831 return 0;
1835 static LRESULT
1836 HEADER_NotifyFormat (HEADER_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
1838 switch (lParam)
1840 case NF_QUERY:
1841 return infoPtr->nNotifyFormat;
1843 case NF_REQUERY:
1844 infoPtr->nNotifyFormat =
1845 SendMessageW ((HWND)wParam, WM_NOTIFYFORMAT,
1846 (WPARAM)infoPtr->hwndSelf, NF_QUERY);
1847 return infoPtr->nNotifyFormat;
1850 return 0;
1853 static LRESULT
1854 HEADER_MouseLeave (HEADER_INFO *infoPtr)
1856 /* Reset hot-tracked item when mouse leaves control. */
1857 INT oldHotItem = infoPtr->iHotItem;
1858 HDC hdc = GetDC (infoPtr->hwndSelf);
1860 infoPtr->iHotItem = -1;
1861 if (oldHotItem != -1) HEADER_RefreshItem (infoPtr, oldHotItem);
1862 ReleaseDC (infoPtr->hwndSelf, hdc);
1864 return 0;
1868 static LRESULT
1869 HEADER_MouseMove (HEADER_INFO *infoPtr, LPARAM lParam)
1871 POINT pt;
1872 UINT flags;
1873 INT nItem, nWidth;
1874 HDC hdc;
1875 /* With theming, hottracking is always enabled */
1876 BOOL hotTrackEnabled =
1877 ((infoPtr->dwStyle & HDS_BUTTONS) && (infoPtr->dwStyle & HDS_HOTTRACK))
1878 || (GetWindowTheme (infoPtr->hwndSelf) != NULL);
1879 INT oldHotItem = infoPtr->iHotItem;
1881 pt.x = (INT)(SHORT)LOWORD(lParam);
1882 pt.y = (INT)(SHORT)HIWORD(lParam);
1883 HEADER_InternalHitTest (infoPtr, &pt, &flags, &nItem);
1885 if (hotTrackEnabled) {
1886 if (flags & (HHT_ONHEADER | HHT_ONDIVIDER | HHT_ONDIVOPEN))
1887 infoPtr->iHotItem = nItem;
1888 else
1889 infoPtr->iHotItem = -1;
1892 if (infoPtr->bCaptured) {
1893 /* check if we should drag the header */
1894 if (infoPtr->bPressed && !infoPtr->bDragging && (infoPtr->dwStyle & HDS_DRAGDROP)
1895 && HEADER_IsDragDistance(infoPtr, &pt))
1897 if (!HEADER_SendNotifyWithHDItemT(infoPtr, HDN_BEGINDRAG, infoPtr->iMoveItem, NULL))
1899 HIMAGELIST hDragItem = HEADER_CreateDragImage(infoPtr, infoPtr->iMoveItem);
1900 if (hDragItem != NULL)
1902 HEADER_ITEM *lpItem = &infoPtr->items[infoPtr->iMoveItem];
1903 TRACE("Starting item drag\n");
1904 ImageList_BeginDrag(hDragItem, 0, pt.x - lpItem->rect.left, 0);
1905 ImageList_DragShowNolock(TRUE);
1906 ImageList_Destroy(hDragItem);
1907 infoPtr->bDragging = TRUE;
1912 if (infoPtr->bDragging)
1914 POINT drag;
1915 drag.x = pt.x;
1916 drag.y = 0;
1917 ClientToScreen(infoPtr->hwndSelf, &drag);
1918 ImageList_DragMove(drag.x, drag.y);
1919 HEADER_SetHotDivider(infoPtr, TRUE, lParam);
1922 if (infoPtr->bPressed && !infoPtr->bDragging) {
1923 BOOL oldState = infoPtr->items[infoPtr->iMoveItem].bDown;
1924 if ((nItem == infoPtr->iMoveItem) && (flags == HHT_ONHEADER))
1925 infoPtr->items[infoPtr->iMoveItem].bDown = TRUE;
1926 else
1927 infoPtr->items[infoPtr->iMoveItem].bDown = FALSE;
1928 if (oldState != infoPtr->items[infoPtr->iMoveItem].bDown) {
1929 hdc = GetDC (infoPtr->hwndSelf);
1930 HEADER_RefreshItem (infoPtr, infoPtr->iMoveItem);
1931 ReleaseDC (infoPtr->hwndSelf, hdc);
1934 TRACE("Moving pressed item %d.\n", infoPtr->iMoveItem);
1936 else if (infoPtr->bTracking) {
1937 if (infoPtr->dwStyle & HDS_FULLDRAG) {
1938 HEADER_ITEM *lpItem = &infoPtr->items[infoPtr->iMoveItem];
1939 nWidth = pt.x - lpItem->rect.left + infoPtr->xTrackOffset;
1940 if (!HEADER_SendNotifyWithIntFieldT(infoPtr, HDN_ITEMCHANGINGW, infoPtr->iMoveItem, HDI_WIDTH, nWidth))
1942 INT nOldWidth = lpItem->rect.right - lpItem->rect.left;
1943 RECT rcClient;
1944 RECT rcScroll;
1946 if (nWidth < 0) nWidth = 0;
1947 infoPtr->items[infoPtr->iMoveItem].cxy = nWidth;
1948 HEADER_SetItemBounds(infoPtr);
1950 GetClientRect(infoPtr->hwndSelf, &rcClient);
1951 rcScroll = rcClient;
1952 rcScroll.left = lpItem->rect.left + nOldWidth;
1953 ScrollWindowEx(infoPtr->hwndSelf, nWidth - nOldWidth, 0, &rcScroll, &rcClient, NULL, NULL, 0);
1954 InvalidateRect(infoPtr->hwndSelf, &lpItem->rect, FALSE);
1955 UpdateWindow(infoPtr->hwndSelf);
1957 HEADER_SendNotifyWithIntFieldT(infoPtr, HDN_ITEMCHANGEDW, infoPtr->iMoveItem, HDI_WIDTH, nWidth);
1960 else {
1961 INT iTrackWidth;
1962 hdc = GetDC (infoPtr->hwndSelf);
1963 HEADER_DrawTrackLine (infoPtr, hdc, infoPtr->xOldTrack);
1964 infoPtr->xOldTrack = pt.x + infoPtr->xTrackOffset;
1965 if (infoPtr->xOldTrack < infoPtr->items[infoPtr->iMoveItem].rect.left)
1966 infoPtr->xOldTrack = infoPtr->items[infoPtr->iMoveItem].rect.left;
1967 HEADER_DrawTrackLine (infoPtr, hdc, infoPtr->xOldTrack);
1968 ReleaseDC (infoPtr->hwndSelf, hdc);
1969 iTrackWidth = infoPtr->xOldTrack - infoPtr->items[infoPtr->iMoveItem].rect.left;
1970 /* FIXME: should stop tracking if HDN_TRACK returns TRUE */
1971 HEADER_SendNotifyWithIntFieldT(infoPtr, HDN_TRACKW, infoPtr->iMoveItem, HDI_WIDTH, iTrackWidth);
1974 TRACE("Tracking item %d.\n", infoPtr->iMoveItem);
1978 if (hotTrackEnabled) {
1979 TRACKMOUSEEVENT tme;
1980 if (oldHotItem != infoPtr->iHotItem && !infoPtr->bDragging) {
1981 hdc = GetDC (infoPtr->hwndSelf);
1982 if (oldHotItem != -1) HEADER_RefreshItem (infoPtr, oldHotItem);
1983 if (infoPtr->iHotItem != -1) HEADER_RefreshItem (infoPtr, infoPtr->iHotItem);
1984 ReleaseDC (infoPtr->hwndSelf, hdc);
1986 tme.cbSize = sizeof( tme );
1987 tme.dwFlags = TME_LEAVE;
1988 tme.hwndTrack = infoPtr->hwndSelf;
1989 TrackMouseEvent( &tme );
1992 return 0;
1996 static LRESULT
1997 HEADER_Paint (HEADER_INFO *infoPtr, HDC hdcParam)
1999 HDC hdc;
2000 PAINTSTRUCT ps;
2002 hdc = hdcParam==0 ? BeginPaint (infoPtr->hwndSelf, &ps) : hdcParam;
2003 HEADER_Refresh (infoPtr, hdc);
2004 if(!hdcParam)
2005 EndPaint (infoPtr->hwndSelf, &ps);
2006 return 0;
2010 static LRESULT
2011 HEADER_RButtonUp (HEADER_INFO *infoPtr, INT x, INT y)
2013 BOOL bRet;
2014 POINT pt;
2016 pt.x = x;
2017 pt.y = y;
2019 /* Send a Notify message */
2020 bRet = HEADER_SendSimpleNotify (infoPtr, NM_RCLICK);
2022 /* Change to screen coordinate for WM_CONTEXTMENU */
2023 ClientToScreen(infoPtr->hwndSelf, &pt);
2025 /* Send a WM_CONTEXTMENU message in response to the RBUTTONUP */
2026 SendMessageW( infoPtr->hwndSelf, WM_CONTEXTMENU, (WPARAM) infoPtr->hwndSelf, MAKELPARAM(pt.x, pt.y));
2028 return bRet;
2032 static LRESULT
2033 HEADER_SetCursor (HEADER_INFO *infoPtr, LPARAM lParam)
2035 POINT pt;
2036 UINT flags;
2037 INT nItem;
2039 TRACE("code=0x%X id=0x%X\n", LOWORD(lParam), HIWORD(lParam));
2041 GetCursorPos (&pt);
2042 ScreenToClient (infoPtr->hwndSelf, &pt);
2044 HEADER_InternalHitTest (infoPtr, &pt, &flags, &nItem);
2046 if (flags == HHT_ONDIVIDER)
2047 SetCursor (infoPtr->hcurDivider);
2048 else if (flags == HHT_ONDIVOPEN)
2049 SetCursor (infoPtr->hcurDivopen);
2050 else
2051 SetCursor (infoPtr->hcurArrow);
2053 return 0;
2057 static LRESULT
2058 HEADER_SetFont (HEADER_INFO *infoPtr, HFONT hFont, WORD Redraw)
2060 TEXTMETRICW tm;
2061 HFONT hOldFont;
2062 HDC hdc;
2064 infoPtr->hFont = hFont;
2066 hdc = GetDC (0);
2067 hOldFont = SelectObject (hdc, infoPtr->hFont ? infoPtr->hFont : GetStockObject (SYSTEM_FONT));
2068 GetTextMetricsW (hdc, &tm);
2069 infoPtr->nHeight = tm.tmHeight + VERT_BORDER;
2070 SelectObject (hdc, hOldFont);
2071 ReleaseDC (0, hdc);
2073 infoPtr->bRectsValid = FALSE;
2075 if (Redraw) {
2076 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2079 return 0;
2082 static LRESULT HEADER_SetRedraw(HEADER_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
2084 /* ignoring the InvalidateRect calls is handled by user32. But some apps expect
2085 * that we invalidate the header and this has to be done manually */
2086 LRESULT ret;
2088 ret = DefWindowProcW(infoPtr->hwndSelf, WM_SETREDRAW, wParam, lParam);
2089 if (wParam)
2090 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
2091 return ret;
2094 static INT HEADER_StyleChanged(HEADER_INFO *infoPtr, WPARAM wStyleType,
2095 const STYLESTRUCT *lpss)
2097 TRACE("styletype %Ix, styleOld %#lx, styleNew %#lx\n", wStyleType, lpss->styleOld, lpss->styleNew);
2099 if (wStyleType != GWL_STYLE) return 0;
2101 infoPtr->dwStyle = lpss->styleNew;
2103 return 0;
2106 /* Update the theme handle after a theme change */
2107 static LRESULT HEADER_ThemeChanged(const HEADER_INFO *infoPtr)
2109 HTHEME theme = GetWindowTheme(infoPtr->hwndSelf);
2110 CloseThemeData(theme);
2111 OpenThemeData(infoPtr->hwndSelf, themeClass);
2112 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
2113 return 0;
2116 static INT HEADER_SetFilterChangeTimeout(HEADER_INFO *infoPtr, INT timeout)
2118 INT old_timeout = infoPtr->filter_change_timeout;
2120 if (timeout != 0)
2121 infoPtr->filter_change_timeout = timeout;
2122 return old_timeout;
2125 static LRESULT WINAPI
2126 HEADER_WindowProc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
2128 HEADER_INFO *infoPtr = (HEADER_INFO *)GetWindowLongPtrW(hwnd, 0);
2130 TRACE("hwnd %p, msg %x, wparam %Ix, lParam %Ix\n", hwnd, msg, wParam, lParam);
2132 if (!infoPtr && (msg != WM_CREATE))
2133 return DefWindowProcW (hwnd, msg, wParam, lParam);
2134 switch (msg) {
2135 /* case HDM_CLEARFILTER: */
2137 case HDM_CREATEDRAGIMAGE:
2138 return (LRESULT)HEADER_CreateDragImage (infoPtr, (INT)wParam);
2140 case HDM_DELETEITEM:
2141 return HEADER_DeleteItem (infoPtr, (INT)wParam);
2143 /* case HDM_EDITFILTER: */
2145 case HDM_GETBITMAPMARGIN:
2146 return HEADER_GetBitmapMargin(infoPtr);
2148 case HDM_GETIMAGELIST:
2149 return HEADER_GetImageList (infoPtr);
2151 case HDM_GETITEMA:
2152 case HDM_GETITEMW:
2153 return HEADER_GetItemT (infoPtr, (INT)wParam, (LPHDITEMW)lParam, msg == HDM_GETITEMW);
2155 case HDM_GETITEMCOUNT:
2156 return HEADER_GetItemCount (infoPtr);
2158 case HDM_GETITEMRECT:
2159 return HEADER_GetItemRect (infoPtr, (INT)wParam, (LPRECT)lParam);
2161 case HDM_GETORDERARRAY:
2162 return HEADER_GetOrderArray(infoPtr, (INT)wParam, (LPINT)lParam);
2164 case HDM_GETUNICODEFORMAT:
2165 return HEADER_GetUnicodeFormat (infoPtr);
2167 case HDM_HITTEST:
2168 return HEADER_HitTest (infoPtr, (LPHDHITTESTINFO)lParam);
2170 case HDM_INSERTITEMA:
2171 case HDM_INSERTITEMW:
2172 return HEADER_InsertItemT (infoPtr, (INT)wParam, (LPHDITEMW)lParam, msg == HDM_INSERTITEMW);
2174 case HDM_LAYOUT:
2175 return HEADER_Layout (infoPtr, (LPHDLAYOUT)lParam);
2177 case HDM_ORDERTOINDEX:
2178 return HEADER_OrderToIndex(infoPtr, (INT)wParam);
2180 case HDM_SETBITMAPMARGIN:
2181 return HEADER_SetBitmapMargin(infoPtr, (INT)wParam);
2183 case HDM_SETFILTERCHANGETIMEOUT:
2184 return HEADER_SetFilterChangeTimeout(infoPtr, (INT)lParam);
2186 case HDM_SETHOTDIVIDER:
2187 return HEADER_SetHotDivider(infoPtr, wParam, lParam);
2189 case HDM_SETIMAGELIST:
2190 return HEADER_SetImageList (infoPtr, (HIMAGELIST)lParam);
2192 case HDM_SETITEMA:
2193 case HDM_SETITEMW:
2194 return HEADER_SetItemT (infoPtr, (INT)wParam, (LPHDITEMW)lParam, msg == HDM_SETITEMW);
2196 case HDM_SETORDERARRAY:
2197 return HEADER_SetOrderArray(infoPtr, (INT)wParam, (LPINT)lParam);
2199 case HDM_SETUNICODEFORMAT:
2200 return HEADER_SetUnicodeFormat (infoPtr, wParam);
2202 case WM_CREATE:
2203 return HEADER_Create (hwnd, (LPCREATESTRUCTW)lParam);
2205 case WM_DESTROY:
2206 return HEADER_Destroy (infoPtr);
2208 case WM_NCDESTROY:
2209 return HEADER_NCDestroy (infoPtr);
2211 case WM_ERASEBKGND:
2212 return 1;
2214 case WM_GETDLGCODE:
2215 return DLGC_WANTTAB | DLGC_WANTARROWS;
2217 case WM_GETFONT:
2218 return HEADER_GetFont (infoPtr);
2220 case WM_LBUTTONDBLCLK:
2221 return HEADER_LButtonDblClk (infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
2223 case WM_LBUTTONDOWN:
2224 return HEADER_LButtonDown (infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
2226 case WM_LBUTTONUP:
2227 return HEADER_LButtonUp (infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
2229 case WM_MOUSELEAVE:
2230 return HEADER_MouseLeave (infoPtr);
2232 case WM_MOUSEMOVE:
2233 return HEADER_MouseMove (infoPtr, lParam);
2235 case WM_NOTIFYFORMAT:
2236 return HEADER_NotifyFormat (infoPtr, wParam, lParam);
2238 case WM_SIZE:
2239 return HEADER_Size (infoPtr);
2241 case WM_THEMECHANGED:
2242 return HEADER_ThemeChanged (infoPtr);
2244 case WM_PRINTCLIENT:
2245 case WM_PAINT:
2246 return HEADER_Paint (infoPtr, (HDC)wParam);
2248 case WM_RBUTTONUP:
2249 return HEADER_RButtonUp (infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
2251 case WM_SETCURSOR:
2252 return HEADER_SetCursor (infoPtr, lParam);
2254 case WM_SETFONT:
2255 return HEADER_SetFont (infoPtr, (HFONT)wParam, (WORD)lParam);
2257 case WM_SETREDRAW:
2258 return HEADER_SetRedraw(infoPtr, wParam, lParam);
2260 case WM_STYLECHANGED:
2261 return HEADER_StyleChanged(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
2263 case WM_SYSCOLORCHANGE:
2264 COMCTL32_RefreshSysColors();
2265 return 0;
2267 default:
2268 if ((msg >= WM_USER) && (msg < WM_APP) && !COMCTL32_IsReflectedMessage(msg))
2269 ERR("unknown msg %04x, wp %#Ix, lp %#Ix\n", msg, wParam, lParam );
2270 return DefWindowProcW(hwnd, msg, wParam, lParam);
2275 VOID
2276 HEADER_Register (void)
2278 WNDCLASSW wndClass;
2280 ZeroMemory (&wndClass, sizeof(WNDCLASSW));
2281 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS;
2282 wndClass.lpfnWndProc = HEADER_WindowProc;
2283 wndClass.cbClsExtra = 0;
2284 wndClass.cbWndExtra = sizeof(HEADER_INFO *);
2285 wndClass.hCursor = LoadCursorW (0, (LPWSTR)IDC_ARROW);
2286 wndClass.lpszClassName = WC_HEADERW;
2288 RegisterClassW (&wndClass);
2292 VOID
2293 HEADER_Unregister (void)
2295 UnregisterClassW (WC_HEADERW, NULL);