cmd: Fix German translation.
[wine/hramrach.git] / dlls / comctl32 / header.c
blob1ee58cdb4fcba20b845211e3d01fe96c4546751f
1 /*
2 * Header control
4 * Copyright 1998 Eric Kohl
5 * Copyright 2000 Eric Kohl for CodeWeavers
6 * Copyright 2003 Maxime Bellenge
7 * Copyright 2006 Mikolaj Zalewski
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 * TODO:
24 * - Imagelist support (completed?)
25 * - Hottrack support (completed?)
26 * - Filters support (HDS_FILTER, HDI_FILTER, HDM_*FILTER*, HDN_*FILTER*)
27 * - New Windows Vista features
30 #include <stdarg.h>
31 #include <stdlib.h>
32 #include <string.h>
34 #include "windef.h"
35 #include "winbase.h"
36 #include "wine/unicode.h"
37 #include "wingdi.h"
38 #include "winuser.h"
39 #include "winnls.h"
40 #include "commctrl.h"
41 #include "comctl32.h"
42 #include "imagelist.h"
43 #include "tmschema.h"
44 #include "uxtheme.h"
45 #include "wine/debug.h"
47 WINE_DEFAULT_DEBUG_CHANNEL(header);
49 typedef struct
51 INT cxy;
52 HBITMAP hbm;
53 LPWSTR pszText;
54 INT fmt;
55 LPARAM lParam;
56 INT iImage;
57 INT iOrder; /* see documentation of HD_ITEM */
59 BOOL bDown; /* is item pressed? (used for drawing) */
60 RECT rect; /* bounding rectangle of the item */
61 DWORD callbackMask; /* HDI_* flags for items that are callback */
62 } HEADER_ITEM;
65 typedef struct
67 HWND hwndSelf; /* Control window */
68 HWND hwndNotify; /* Owner window to send notifications to */
69 INT nNotifyFormat; /* format used for WM_NOTIFY messages */
70 UINT uNumItem; /* number of items (columns) */
71 INT nHeight; /* height of the header (pixels) */
72 HFONT hFont; /* handle to the current font */
73 HCURSOR hcurArrow; /* handle to the arrow cursor */
74 HCURSOR hcurDivider; /* handle to a cursor (used over dividers) <-|-> */
75 HCURSOR hcurDivopen; /* handle to a cursor (used over dividers) <-||-> */
76 BOOL bCaptured; /* Is the mouse captured? */
77 BOOL bPressed; /* Is a header item pressed (down)? */
78 BOOL bDragging; /* Are we dragging an item? */
79 BOOL bTracking; /* Is in tracking mode? */
80 POINT ptLButtonDown; /* The point where the left button was pressed */
81 DWORD dwStyle; /* the cached window GWL_STYLE */
82 INT iMoveItem; /* index of tracked item. (Tracking mode) */
83 INT xTrackOffset; /* distance between the right side of the tracked item and the cursor */
84 INT xOldTrack; /* track offset (see above) after the last WM_MOUSEMOVE */
85 INT iHotItem; /* index of hot item (cursor is over this item) */
86 INT iHotDivider; /* index of the hot divider (used while dragging an item or by HDM_SETHOTDIVIDER) */
87 INT iMargin; /* width of the margin that surrounds a bitmap */
89 HIMAGELIST himl; /* handle to an image list (may be 0) */
90 HEADER_ITEM *items; /* pointer to array of HEADER_ITEM's */
91 INT *order; /* array of item IDs indexed by order */
92 BOOL bRectsValid; /* validity flag for bounding rectangles */
93 } HEADER_INFO;
96 #define VERT_BORDER 4
97 #define DIVIDER_WIDTH 10
98 #define HOT_DIVIDER_WIDTH 2
99 #define MAX_HEADER_TEXT_LEN 260
100 #define HDN_UNICODE_OFFSET 20
101 #define HDN_FIRST_UNICODE (HDN_FIRST-HDN_UNICODE_OFFSET)
103 #define HDI_SUPPORTED_FIELDS (HDI_WIDTH|HDI_TEXT|HDI_FORMAT|HDI_LPARAM|HDI_BITMAP|HDI_IMAGE|HDI_ORDER)
104 #define HDI_UNSUPPORTED_FIELDS (HDI_FILTER)
105 #define HDI_UNKNOWN_FIELDS (~(HDI_SUPPORTED_FIELDS|HDI_UNSUPPORTED_FIELDS|HDI_DI_SETITEM))
106 #define HDI_COMCTL32_4_0_FIELDS (HDI_WIDTH|HDI_TEXT|HDI_FORMAT|HDI_LPARAM|HDI_BITMAP)
109 static BOOL HEADER_PrepareCallbackItems(const HEADER_INFO *infoPtr, INT iItem, INT reqMask);
110 static void HEADER_FreeCallbackItems(HEADER_ITEM *lpItem);
111 static LRESULT HEADER_SendNotify(const HEADER_INFO *infoPtr, UINT code, NMHDR *hdr);
112 static LRESULT HEADER_SendCtrlCustomDraw(const HEADER_INFO *infoPtr, DWORD dwDrawStage, HDC hdc, const RECT *rect);
114 static const WCHAR themeClass[] = {'H','e','a','d','e','r',0};
116 static void HEADER_StoreHDItemInHeader(HEADER_ITEM *lpItem, UINT mask, const HDITEMW *phdi, BOOL fUnicode)
118 if (mask & HDI_UNSUPPORTED_FIELDS)
119 FIXME("unsupported header fields %x\n", (mask & HDI_UNSUPPORTED_FIELDS));
121 if (mask & HDI_BITMAP)
122 lpItem->hbm = phdi->hbm;
124 if (mask & HDI_FORMAT)
125 lpItem->fmt = phdi->fmt;
127 if (mask & HDI_LPARAM)
128 lpItem->lParam = phdi->lParam;
130 if (mask & HDI_WIDTH)
131 lpItem->cxy = phdi->cxy;
133 if (mask & HDI_IMAGE)
135 lpItem->iImage = phdi->iImage;
136 if (phdi->iImage == I_IMAGECALLBACK)
137 lpItem->callbackMask |= HDI_IMAGE;
138 else
139 lpItem->callbackMask &= ~HDI_IMAGE;
142 if (mask & HDI_TEXT)
144 Free(lpItem->pszText);
145 lpItem->pszText = NULL;
147 if (phdi->pszText != LPSTR_TEXTCALLBACKW) /* covers != TEXTCALLBACKA too */
149 static const WCHAR emptyString[] = {0};
151 LPCWSTR pszText = (phdi->pszText != NULL ? phdi->pszText : emptyString);
152 if (fUnicode)
153 Str_SetPtrW(&lpItem->pszText, pszText);
154 else
155 Str_SetPtrAtoW(&lpItem->pszText, (LPCSTR)pszText);
156 lpItem->callbackMask &= ~HDI_TEXT;
158 else
160 lpItem->pszText = NULL;
161 lpItem->callbackMask |= HDI_TEXT;
166 static inline LRESULT
167 HEADER_IndexToOrder (const HEADER_INFO *infoPtr, INT iItem)
169 HEADER_ITEM *lpItem = &infoPtr->items[iItem];
170 return lpItem->iOrder;
174 static INT
175 HEADER_OrderToIndex(const HEADER_INFO *infoPtr, INT iorder)
177 if ((iorder <0) || iorder >= infoPtr->uNumItem)
178 return iorder;
179 return infoPtr->order[iorder];
182 static void
183 HEADER_ChangeItemOrder(const HEADER_INFO *infoPtr, INT iItem, INT iNewOrder)
185 HEADER_ITEM *lpItem = &infoPtr->items[iItem];
186 INT i, nMin, nMax;
188 TRACE("%d: %d->%d\n", iItem, lpItem->iOrder, iNewOrder);
189 if (lpItem->iOrder < iNewOrder)
191 memmove(&infoPtr->order[lpItem->iOrder],
192 &infoPtr->order[lpItem->iOrder + 1],
193 (iNewOrder - lpItem->iOrder) * sizeof(INT));
195 if (iNewOrder < lpItem->iOrder)
197 memmove(&infoPtr->order[iNewOrder + 1],
198 &infoPtr->order[iNewOrder],
199 (lpItem->iOrder - iNewOrder) * sizeof(INT));
201 infoPtr->order[iNewOrder] = iItem;
202 nMin = min(lpItem->iOrder, iNewOrder);
203 nMax = max(lpItem->iOrder, iNewOrder);
204 for (i = nMin; i <= nMax; i++)
205 infoPtr->items[infoPtr->order[i]].iOrder = i;
208 /* Note: if iItem is the last item then this function returns infoPtr->uNumItem */
209 static INT
210 HEADER_NextItem(const HEADER_INFO *infoPtr, INT iItem)
212 return HEADER_OrderToIndex(infoPtr, HEADER_IndexToOrder(infoPtr, iItem)+1);
215 static INT
216 HEADER_PrevItem(const HEADER_INFO *infoPtr, INT iItem)
218 return HEADER_OrderToIndex(infoPtr, HEADER_IndexToOrder(infoPtr, iItem)-1);
221 static void
222 HEADER_SetItemBounds (HEADER_INFO *infoPtr)
224 HEADER_ITEM *phdi;
225 RECT rect;
226 unsigned int i;
227 int x;
229 infoPtr->bRectsValid = TRUE;
231 if (infoPtr->uNumItem == 0)
232 return;
234 GetClientRect (infoPtr->hwndSelf, &rect);
236 x = rect.left;
237 for (i = 0; i < infoPtr->uNumItem; i++) {
238 phdi = &infoPtr->items[HEADER_OrderToIndex(infoPtr,i)];
239 phdi->rect.top = rect.top;
240 phdi->rect.bottom = rect.bottom;
241 phdi->rect.left = x;
242 phdi->rect.right = phdi->rect.left + ((phdi->cxy>0)?phdi->cxy:0);
243 x = phdi->rect.right;
247 static LRESULT
248 HEADER_Size (HEADER_INFO *infoPtr)
250 HEADER_SetItemBounds(infoPtr);
251 return 0;
254 static void HEADER_GetHotDividerRect(const HEADER_INFO *infoPtr, RECT *r)
256 INT iDivider = infoPtr->iHotDivider;
257 if (infoPtr->uNumItem > 0)
259 HEADER_ITEM *lpItem;
261 if (iDivider < infoPtr->uNumItem)
263 lpItem = &infoPtr->items[iDivider];
264 r->left = lpItem->rect.left - HOT_DIVIDER_WIDTH/2;
265 r->right = lpItem->rect.left + HOT_DIVIDER_WIDTH/2;
267 else
269 lpItem = &infoPtr->items[HEADER_OrderToIndex(infoPtr, infoPtr->uNumItem-1)];
270 r->left = lpItem->rect.right - HOT_DIVIDER_WIDTH/2;
271 r->right = lpItem->rect.right + HOT_DIVIDER_WIDTH/2;
273 r->top = lpItem->rect.top;
274 r->bottom = lpItem->rect.bottom;
276 else
278 RECT clientRect;
279 GetClientRect(infoPtr->hwndSelf, &clientRect);
280 *r = clientRect;
281 r->right = r->left + HOT_DIVIDER_WIDTH/2;
286 static INT
287 HEADER_DrawItem (HEADER_INFO *infoPtr, HDC hdc, INT iItem, BOOL bHotTrack, LRESULT lCDFlags)
289 HEADER_ITEM *phdi = &infoPtr->items[iItem];
290 RECT r;
291 INT oldBkMode;
292 HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
293 NMCUSTOMDRAW nmcd;
295 TRACE("DrawItem(iItem %d bHotTrack %d unicode flag %d)\n", iItem, bHotTrack, (infoPtr->nNotifyFormat == NFR_UNICODE));
297 r = phdi->rect;
298 if (r.right - r.left == 0)
299 return phdi->rect.right;
301 /* Set the colors before sending NM_CUSTOMDRAW so that it can change them */
302 SetTextColor(hdc, (bHotTrack && !theme) ? COLOR_HIGHLIGHT : COLOR_BTNTEXT);
303 SetBkColor(hdc, comctl32_color.clr3dFace);
305 if (lCDFlags & CDRF_NOTIFYITEMDRAW && !(phdi->fmt & HDF_OWNERDRAW))
307 LRESULT lCDItemFlags;
309 nmcd.dwDrawStage = CDDS_PREPAINT | CDDS_ITEM;
310 nmcd.hdc = hdc;
311 nmcd.dwItemSpec = iItem;
312 nmcd.rc = r;
313 nmcd.uItemState = phdi->bDown ? CDIS_SELECTED : 0;
314 nmcd.lItemlParam = phdi->lParam;
316 lCDItemFlags = HEADER_SendNotify(infoPtr, NM_CUSTOMDRAW, (NMHDR *)&nmcd);
317 if (lCDItemFlags & CDRF_SKIPDEFAULT)
318 return phdi->rect.right;
321 if (theme != NULL) {
322 int state = (phdi->bDown) ? HIS_PRESSED :
323 (bHotTrack ? HIS_HOT : HIS_NORMAL);
324 DrawThemeBackground (theme, hdc, HP_HEADERITEM, state,
325 &r, NULL);
326 GetThemeBackgroundContentRect (theme, hdc, HP_HEADERITEM, state,
327 &r, &r);
329 else {
330 HBRUSH hbr;
332 if (!(infoPtr->dwStyle & HDS_FLAT))
334 if (infoPtr->dwStyle & HDS_BUTTONS) {
335 if (phdi->bDown) {
336 DrawEdge (hdc, &r, BDR_RAISEDOUTER,
337 BF_RECT | BF_FLAT | BF_MIDDLE | BF_ADJUST);
339 else
340 DrawEdge (hdc, &r, EDGE_RAISED,
341 BF_RECT | BF_SOFT | BF_MIDDLE | BF_ADJUST);
343 else
344 DrawEdge (hdc, &r, EDGE_ETCHED, BF_BOTTOM | BF_RIGHT | BF_ADJUST);
347 hbr = CreateSolidBrush(GetBkColor(hdc));
348 FillRect(hdc, &r, hbr);
349 DeleteObject(hbr);
351 if (phdi->bDown) {
352 r.left += 2;
353 r.top += 2;
356 if (phdi->fmt & HDF_OWNERDRAW) {
357 DRAWITEMSTRUCT dis;
359 dis.CtlType = ODT_HEADER;
360 dis.CtlID = GetWindowLongPtrW (infoPtr->hwndSelf, GWLP_ID);
361 dis.itemID = iItem;
362 dis.itemAction = ODA_DRAWENTIRE;
363 dis.itemState = phdi->bDown ? ODS_SELECTED : 0;
364 dis.hwndItem = infoPtr->hwndSelf;
365 dis.hDC = hdc;
366 dis.rcItem = phdi->rect;
367 dis.itemData = phdi->lParam;
368 oldBkMode = SetBkMode(hdc, TRANSPARENT);
369 SendMessageW (infoPtr->hwndNotify, WM_DRAWITEM,
370 (WPARAM)dis.CtlID, (LPARAM)&dis);
371 if (oldBkMode != TRANSPARENT)
372 SetBkMode(hdc, oldBkMode);
374 else {
375 UINT rw, rh, /* width and height of r */
376 *x = NULL, *w = NULL; /* x and width of the pic (bmp or img) which is part of cnt */
377 /* cnt,txt,img,bmp */
378 UINT cx, tx, ix, bx,
379 cw, tw, iw, bw;
380 BITMAP bmp;
382 HEADER_PrepareCallbackItems(infoPtr, iItem, HDI_TEXT|HDI_IMAGE);
383 cw = tw = iw = bw = 0;
384 rw = r.right - r.left;
385 rh = r.bottom - r.top;
387 if (phdi->fmt & HDF_STRING) {
388 RECT textRect;
390 SetRectEmpty(&textRect);
391 DrawTextW (hdc, phdi->pszText, -1,
392 &textRect, DT_LEFT|DT_VCENTER|DT_SINGLELINE|DT_CALCRECT);
393 cw = textRect.right - textRect.left + 2 * infoPtr->iMargin;
396 if ((phdi->fmt & HDF_IMAGE) && (infoPtr->himl)) {
397 iw = infoPtr->himl->cx + 2 * infoPtr->iMargin;
398 x = &ix;
399 w = &iw;
402 if ((phdi->fmt & HDF_BITMAP) && (phdi->hbm)) {
403 GetObjectW (phdi->hbm, sizeof(BITMAP), &bmp);
404 bw = bmp.bmWidth + 2 * infoPtr->iMargin;
405 if (!iw) {
406 x = &bx;
407 w = &bw;
411 if (bw || iw)
412 cw += *w;
414 /* align cx using the unclipped cw */
415 if ((phdi->fmt & HDF_JUSTIFYMASK) == HDF_LEFT)
416 cx = r.left;
417 else if ((phdi->fmt & HDF_JUSTIFYMASK) == HDF_CENTER)
418 cx = r.left + rw / 2 - cw / 2;
419 else /* HDF_RIGHT */
420 cx = r.right - cw;
422 /* clip cx & cw */
423 if (cx < r.left)
424 cx = r.left;
425 if (cx + cw > r.right)
426 cw = r.right - cx;
428 tx = cx + infoPtr->iMargin;
429 /* since cw might have changed we have to recalculate tw */
430 tw = cw - infoPtr->iMargin * 2;
432 if (iw || bw) {
433 tw -= *w;
434 if (phdi->fmt & HDF_BITMAP_ON_RIGHT) {
435 /* put pic behind text */
436 *x = cx + tw + infoPtr->iMargin * 3;
437 } else {
438 *x = cx + infoPtr->iMargin;
439 /* move text behind pic */
440 tx += *w;
444 if (iw && bw) {
445 /* since we're done with the layout we can
446 now calculate the position of bmp which
447 has no influence on alignment and layout
448 because of img */
449 if ((phdi->fmt & HDF_JUSTIFYMASK) == HDF_RIGHT)
450 bx = cx - bw + infoPtr->iMargin;
451 else
452 bx = cx + cw + infoPtr->iMargin;
455 if (iw || bw) {
456 HDC hClipDC = GetDC(infoPtr->hwndSelf);
457 HRGN hClipRgn = CreateRectRgn(r.left, r.top, r.right, r.bottom);
458 SelectClipRgn(hClipDC, hClipRgn);
460 if (bw) {
461 HDC hdcBitmap = CreateCompatibleDC (hClipDC);
462 SelectObject (hdcBitmap, phdi->hbm);
463 BitBlt (hClipDC, bx, r.top + ((INT)rh - bmp.bmHeight) / 2,
464 bmp.bmWidth, bmp.bmHeight, hdcBitmap, 0, 0, SRCCOPY);
465 DeleteDC (hdcBitmap);
468 if (iw) {
469 ImageList_DrawEx (infoPtr->himl, phdi->iImage, hClipDC,
470 ix, r.top + ((INT)rh - infoPtr->himl->cy) / 2,
471 infoPtr->himl->cx, infoPtr->himl->cy, CLR_DEFAULT, CLR_DEFAULT, 0);
474 DeleteObject(hClipRgn);
475 ReleaseDC(infoPtr->hwndSelf, hClipDC);
478 if (((phdi->fmt & HDF_STRING)
479 || (!(phdi->fmt & (HDF_OWNERDRAW|HDF_STRING|HDF_BITMAP|
480 HDF_BITMAP_ON_RIGHT|HDF_IMAGE)))) /* no explicit format specified? */
481 && (phdi->pszText)) {
482 oldBkMode = SetBkMode(hdc, TRANSPARENT);
483 r.left = tx;
484 r.right = tx + tw;
485 DrawTextW (hdc, phdi->pszText, -1,
486 &r, DT_LEFT|DT_END_ELLIPSIS|DT_VCENTER|DT_SINGLELINE);
487 if (oldBkMode != TRANSPARENT)
488 SetBkMode(hdc, oldBkMode);
490 HEADER_FreeCallbackItems(phdi);
491 }/*Ownerdrawn*/
493 return phdi->rect.right;
496 static void
497 HEADER_DrawHotDivider(const HEADER_INFO *infoPtr, HDC hdc)
499 HBRUSH brush;
500 RECT r;
502 HEADER_GetHotDividerRect(infoPtr, &r);
503 brush = CreateSolidBrush(comctl32_color.clrHighlight);
504 FillRect(hdc, &r, brush);
505 DeleteObject(brush);
508 static void
509 HEADER_Refresh (HEADER_INFO *infoPtr, HDC hdc)
511 HFONT hFont, hOldFont;
512 RECT rect, rcRest;
513 HBRUSH hbrBk;
514 UINT i;
515 INT x;
516 LRESULT lCDFlags;
517 HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
519 if (!infoPtr->bRectsValid)
520 HEADER_SetItemBounds(infoPtr);
522 /* get rect for the bar, adjusted for the border */
523 GetClientRect (infoPtr->hwndSelf, &rect);
524 lCDFlags = HEADER_SendCtrlCustomDraw(infoPtr, CDDS_PREPAINT, hdc, &rect);
526 if (infoPtr->bDragging)
527 ImageList_DragShowNolock(FALSE);
529 hFont = infoPtr->hFont ? infoPtr->hFont : GetStockObject (SYSTEM_FONT);
530 hOldFont = SelectObject (hdc, hFont);
532 /* draw Background */
533 if (infoPtr->uNumItem == 0 && theme == NULL) {
534 hbrBk = GetSysColorBrush(COLOR_3DFACE);
535 FillRect(hdc, &rect, hbrBk);
538 x = rect.left;
539 for (i = 0; x <= rect.right && i < infoPtr->uNumItem; i++) {
540 int idx = HEADER_OrderToIndex(infoPtr,i);
541 if (RectVisible(hdc, &infoPtr->items[idx].rect))
542 HEADER_DrawItem(infoPtr, hdc, idx, infoPtr->iHotItem == idx, lCDFlags);
543 x = infoPtr->items[idx].rect.right;
546 rcRest = rect;
547 rcRest.left = x;
548 if ((x <= rect.right) && RectVisible(hdc, &rcRest) && (infoPtr->uNumItem > 0)) {
549 if (theme != NULL) {
550 DrawThemeBackground(theme, hdc, HP_HEADERITEM, HIS_NORMAL, &rcRest, NULL);
552 else if (infoPtr->dwStyle & HDS_FLAT) {
553 hbrBk = GetSysColorBrush(COLOR_3DFACE);
554 FillRect(hdc, &rcRest, hbrBk);
556 else
558 if (infoPtr->dwStyle & HDS_BUTTONS)
559 DrawEdge (hdc, &rcRest, EDGE_RAISED, BF_TOP|BF_LEFT|BF_BOTTOM|BF_SOFT|BF_MIDDLE);
560 else
561 DrawEdge (hdc, &rcRest, EDGE_ETCHED, BF_BOTTOM|BF_MIDDLE);
565 if (infoPtr->iHotDivider != -1)
566 HEADER_DrawHotDivider(infoPtr, hdc);
568 if (infoPtr->bDragging)
569 ImageList_DragShowNolock(TRUE);
570 SelectObject (hdc, hOldFont);
572 if (lCDFlags & CDRF_NOTIFYPOSTPAINT)
573 HEADER_SendCtrlCustomDraw(infoPtr, CDDS_POSTPAINT, hdc, &rect);
577 static void
578 HEADER_RefreshItem (HEADER_INFO *infoPtr, INT iItem)
580 if (!infoPtr->bRectsValid)
581 HEADER_SetItemBounds(infoPtr);
583 InvalidateRect(infoPtr->hwndSelf, &infoPtr->items[iItem].rect, FALSE);
587 static void
588 HEADER_InternalHitTest (const HEADER_INFO *infoPtr, const POINT *lpPt, UINT *pFlags, INT *pItem)
590 RECT rect, rcTest;
591 UINT iCount;
592 INT width;
593 BOOL bNoWidth;
595 GetClientRect (infoPtr->hwndSelf, &rect);
597 *pFlags = 0;
598 bNoWidth = FALSE;
599 if (PtInRect (&rect, *lpPt))
601 if (infoPtr->uNumItem == 0) {
602 *pFlags |= HHT_NOWHERE;
603 *pItem = 1;
604 TRACE("NOWHERE\n");
605 return;
607 else {
608 /* somewhere inside */
609 for (iCount = 0; iCount < infoPtr->uNumItem; iCount++) {
610 rect = infoPtr->items[iCount].rect;
611 width = rect.right - rect.left;
612 if (width == 0) {
613 bNoWidth = TRUE;
614 continue;
616 if (PtInRect (&rect, *lpPt)) {
617 if (width <= 2 * DIVIDER_WIDTH) {
618 *pFlags |= HHT_ONHEADER;
619 *pItem = iCount;
620 TRACE("ON HEADER %d\n", iCount);
621 return;
623 if (HEADER_IndexToOrder(infoPtr, iCount) > 0) {
624 rcTest = rect;
625 rcTest.right = rcTest.left + DIVIDER_WIDTH;
626 if (PtInRect (&rcTest, *lpPt)) {
627 if (bNoWidth) {
628 *pFlags |= HHT_ONDIVOPEN;
629 *pItem = HEADER_PrevItem(infoPtr, iCount);
630 TRACE("ON DIVOPEN %d\n", *pItem);
631 return;
633 else {
634 *pFlags |= HHT_ONDIVIDER;
635 *pItem = HEADER_PrevItem(infoPtr, iCount);
636 TRACE("ON DIVIDER %d\n", *pItem);
637 return;
641 rcTest = rect;
642 rcTest.left = rcTest.right - DIVIDER_WIDTH;
643 if (PtInRect (&rcTest, *lpPt)) {
644 *pFlags |= HHT_ONDIVIDER;
645 *pItem = iCount;
646 TRACE("ON DIVIDER %d\n", *pItem);
647 return;
650 *pFlags |= HHT_ONHEADER;
651 *pItem = iCount;
652 TRACE("ON HEADER %d\n", iCount);
653 return;
657 /* check for last divider part (on nowhere) */
658 rect = infoPtr->items[infoPtr->uNumItem-1].rect;
659 rect.left = rect.right;
660 rect.right += DIVIDER_WIDTH;
661 if (PtInRect (&rect, *lpPt)) {
662 if (bNoWidth) {
663 *pFlags |= HHT_ONDIVOPEN;
664 *pItem = infoPtr->uNumItem - 1;
665 TRACE("ON DIVOPEN %d\n", *pItem);
666 return;
668 else {
669 *pFlags |= HHT_ONDIVIDER;
670 *pItem = infoPtr->uNumItem-1;
671 TRACE("ON DIVIDER %d\n", *pItem);
672 return;
676 *pFlags |= HHT_NOWHERE;
677 *pItem = 1;
678 TRACE("NOWHERE\n");
679 return;
682 else {
683 if (lpPt->x < rect.left) {
684 TRACE("TO LEFT\n");
685 *pFlags |= HHT_TOLEFT;
687 else if (lpPt->x > rect.right) {
688 TRACE("TO RIGHT\n");
689 *pFlags |= HHT_TORIGHT;
692 if (lpPt->y < rect.top) {
693 TRACE("ABOVE\n");
694 *pFlags |= HHT_ABOVE;
696 else if (lpPt->y > rect.bottom) {
697 TRACE("BELOW\n");
698 *pFlags |= HHT_BELOW;
702 *pItem = 1;
703 TRACE("flags=0x%X\n", *pFlags);
704 return;
708 static void
709 HEADER_DrawTrackLine (const HEADER_INFO *infoPtr, HDC hdc, INT x)
711 RECT rect;
712 HPEN hOldPen;
713 INT oldRop;
715 GetClientRect (infoPtr->hwndSelf, &rect);
717 hOldPen = SelectObject (hdc, GetStockObject (BLACK_PEN));
718 oldRop = SetROP2 (hdc, R2_XORPEN);
719 MoveToEx (hdc, x, rect.top, NULL);
720 LineTo (hdc, x, rect.bottom);
721 SetROP2 (hdc, oldRop);
722 SelectObject (hdc, hOldPen);
725 /***
726 * DESCRIPTION:
727 * Convert a HDITEM into the correct format (ANSI/Unicode) to send it in a notify
729 * PARAMETER(S):
730 * [I] infoPtr : the header that wants to send the notify
731 * [O] dest : The buffer to store the HDITEM for notify. It may be set to a HDITEMA of HDITEMW
732 * [I] src : The source HDITEM. It may be a HDITEMA or HDITEMW
733 * [I] fSourceUnicode : is src a HDITEMW or HDITEMA
734 * [O] ppvScratch : a pointer to a scratch buffer that needs to be freed after
735 * the HDITEM is no longer in use or NULL if none was needed
737 * NOTE: We depend on HDITEMA and HDITEMW having the same structure
739 static void HEADER_CopyHDItemForNotify(const HEADER_INFO *infoPtr, HDITEMW *dest,
740 const HDITEMW *src, BOOL fSourceUnicode, LPVOID *ppvScratch)
742 *ppvScratch = NULL;
743 *dest = *src;
745 if (src->mask & HDI_TEXT && src->pszText != LPSTR_TEXTCALLBACKW) /* covers TEXTCALLBACKA as well */
747 if (fSourceUnicode && infoPtr->nNotifyFormat != NFR_UNICODE)
749 dest->pszText = NULL;
750 Str_SetPtrWtoA((LPSTR *)&dest->pszText, src->pszText);
751 *ppvScratch = dest->pszText;
754 if (!fSourceUnicode && infoPtr->nNotifyFormat == NFR_UNICODE)
756 dest->pszText = NULL;
757 Str_SetPtrAtoW(&dest->pszText, (LPSTR)src->pszText);
758 *ppvScratch = dest->pszText;
763 static UINT HEADER_NotifyCodeWtoA(UINT code)
765 /* we use the fact that all the unicode messages are in HDN_FIRST_UNICODE..HDN_LAST*/
766 if (code >= HDN_LAST && code <= HDN_FIRST_UNICODE)
767 return code + HDN_UNICODE_OFFSET;
768 else
769 return code;
772 static LRESULT
773 HEADER_SendNotify(const HEADER_INFO *infoPtr, UINT code, NMHDR *nmhdr)
775 nmhdr->hwndFrom = infoPtr->hwndSelf;
776 nmhdr->idFrom = GetWindowLongPtrW (infoPtr->hwndSelf, GWLP_ID);
777 nmhdr->code = code;
779 return SendMessageW(infoPtr->hwndNotify, WM_NOTIFY,
780 nmhdr->idFrom, (LPARAM)nmhdr);
783 static BOOL
784 HEADER_SendSimpleNotify (const HEADER_INFO *infoPtr, UINT code)
786 NMHDR nmhdr;
787 return (BOOL)HEADER_SendNotify(infoPtr, code, &nmhdr);
790 static LRESULT
791 HEADER_SendCtrlCustomDraw(const HEADER_INFO *infoPtr, DWORD dwDrawStage, HDC hdc, const RECT *rect)
793 NMCUSTOMDRAW nm;
794 nm.dwDrawStage = dwDrawStage;
795 nm.hdc = hdc;
796 nm.rc = *rect;
797 nm.dwItemSpec = 0;
798 nm.uItemState = 0;
799 nm.lItemlParam = 0;
801 return HEADER_SendNotify(infoPtr, NM_CUSTOMDRAW, (NMHDR *)&nm);
804 static BOOL
805 HEADER_SendNotifyWithHDItemT(const HEADER_INFO *infoPtr, UINT code, INT iItem, HDITEMW *lpItem)
807 NMHEADERW nmhdr;
809 if (infoPtr->nNotifyFormat != NFR_UNICODE)
810 code = HEADER_NotifyCodeWtoA(code);
811 nmhdr.iItem = iItem;
812 nmhdr.iButton = 0;
813 nmhdr.pitem = lpItem;
815 return (BOOL)HEADER_SendNotify(infoPtr, code, (NMHDR *)&nmhdr);
818 static BOOL
819 HEADER_SendNotifyWithIntFieldT(const HEADER_INFO *infoPtr, UINT code, INT iItem, INT mask, INT iValue)
821 HDITEMW nmitem;
823 /* copying only the iValue should be ok but to make the code more robust we copy everything */
824 nmitem.cxy = infoPtr->items[iItem].cxy;
825 nmitem.hbm = infoPtr->items[iItem].hbm;
826 nmitem.pszText = NULL;
827 nmitem.cchTextMax = 0;
828 nmitem.fmt = infoPtr->items[iItem].fmt;
829 nmitem.lParam = infoPtr->items[iItem].lParam;
830 nmitem.iOrder = infoPtr->items[iItem].iOrder;
831 nmitem.iImage = infoPtr->items[iItem].iImage;
833 nmitem.mask = mask;
834 switch (mask)
836 case HDI_WIDTH:
837 nmitem.cxy = iValue;
838 break;
839 case HDI_ORDER:
840 nmitem.iOrder = iValue;
841 break;
842 default:
843 ERR("invalid mask value 0x%x\n", iValue);
846 return HEADER_SendNotifyWithHDItemT(infoPtr, code, iItem, &nmitem);
850 * Prepare callback items
851 * depends on NMHDDISPINFOW having same structure as NMHDDISPINFOA
852 * (so we handle the two cases only doing a specific cast for pszText).
853 * Checks if any of the required field are callback. If there are sends a
854 * NMHDISPINFO notify to retrieve these items. The items are stored in the
855 * HEADER_ITEM pszText and iImage fields. They should be freed with
856 * HEADER_FreeCallbackItems.
858 * @param hwnd : hwnd header container handler
859 * @param iItem : the header item id
860 * @param reqMask : required fields. If any of them is callback this function will fetch it
862 * @return TRUE on success, else FALSE
864 static BOOL
865 HEADER_PrepareCallbackItems(const HEADER_INFO *infoPtr, INT iItem, INT reqMask)
867 HEADER_ITEM *lpItem = &infoPtr->items[iItem];
868 DWORD mask = reqMask & lpItem->callbackMask;
869 NMHDDISPINFOW dispInfo;
870 void *pvBuffer = NULL;
872 if (mask == 0)
873 return TRUE;
874 if (mask&HDI_TEXT && lpItem->pszText != NULL)
876 ERR("(): function called without a call to FreeCallbackItems\n");
877 Free(lpItem->pszText);
878 lpItem->pszText = NULL;
881 memset(&dispInfo, 0, sizeof(NMHDDISPINFOW));
882 dispInfo.hdr.hwndFrom = infoPtr->hwndSelf;
883 dispInfo.hdr.idFrom = GetWindowLongPtrW (infoPtr->hwndSelf, GWLP_ID);
884 if (infoPtr->nNotifyFormat == NFR_UNICODE)
886 dispInfo.hdr.code = HDN_GETDISPINFOW;
887 if (mask & HDI_TEXT)
888 pvBuffer = Alloc(MAX_HEADER_TEXT_LEN * sizeof(WCHAR));
890 else
892 dispInfo.hdr.code = HDN_GETDISPINFOA;
893 if (mask & HDI_TEXT)
894 pvBuffer = Alloc(MAX_HEADER_TEXT_LEN * sizeof(CHAR));
896 dispInfo.pszText = pvBuffer;
897 dispInfo.cchTextMax = (pvBuffer!=NULL?MAX_HEADER_TEXT_LEN:0);
898 dispInfo.iItem = iItem;
899 dispInfo.mask = mask;
900 dispInfo.lParam = lpItem->lParam;
902 TRACE("Sending HDN_GETDISPINFO%c\n", infoPtr->nNotifyFormat == NFR_UNICODE?'W':'A');
903 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, dispInfo.hdr.idFrom, (LPARAM)&dispInfo);
905 TRACE("SendMessage returns(mask:0x%x,str:%s,lParam:%p)\n",
906 dispInfo.mask,
907 (infoPtr->nNotifyFormat == NFR_UNICODE ? debugstr_w(dispInfo.pszText) : (LPSTR) dispInfo.pszText),
908 (void*) dispInfo.lParam);
910 if (mask & HDI_IMAGE)
911 lpItem->iImage = dispInfo.iImage;
912 if (mask & HDI_TEXT)
914 if (infoPtr->nNotifyFormat == NFR_UNICODE)
916 lpItem->pszText = pvBuffer;
918 /* the user might have used his own buffer */
919 if (dispInfo.pszText != lpItem->pszText)
920 Str_GetPtrW(dispInfo.pszText, lpItem->pszText, MAX_HEADER_TEXT_LEN);
922 else
924 Str_SetPtrAtoW(&lpItem->pszText, (LPSTR)dispInfo.pszText);
925 Free(pvBuffer);
929 if (dispInfo.mask & HDI_DI_SETITEM)
931 /* make the items permanent */
932 lpItem->callbackMask &= ~dispInfo.mask;
935 return TRUE;
938 /***
939 * DESCRIPTION:
940 * Free the items that might be allocated with HEADER_PrepareCallbackItems
942 * PARAMETER(S):
943 * [I] lpItem : the item to free the data
946 static void
947 HEADER_FreeCallbackItems(HEADER_ITEM *lpItem)
949 if (lpItem->callbackMask&HDI_TEXT)
951 Free(lpItem->pszText);
952 lpItem->pszText = NULL;
955 if (lpItem->callbackMask&HDI_IMAGE)
956 lpItem->iImage = I_IMAGECALLBACK;
959 static LRESULT
960 HEADER_CreateDragImage (HEADER_INFO *infoPtr, INT iItem)
962 HEADER_ITEM *lpItem;
963 HIMAGELIST himl;
964 HBITMAP hMemory, hOldBitmap;
965 LRESULT lCDFlags;
966 RECT rc;
967 HDC hMemoryDC;
968 HDC hDeviceDC;
969 int height, width;
970 HFONT hFont;
972 if (iItem >= infoPtr->uNumItem)
973 return FALSE;
975 if (!infoPtr->bRectsValid)
976 HEADER_SetItemBounds(infoPtr);
978 lpItem = &infoPtr->items[iItem];
979 width = lpItem->rect.right - lpItem->rect.left;
980 height = lpItem->rect.bottom - lpItem->rect.top;
982 hDeviceDC = GetDC(NULL);
983 hMemoryDC = CreateCompatibleDC(hDeviceDC);
984 hMemory = CreateCompatibleBitmap(hDeviceDC, width, height);
985 ReleaseDC(NULL, hDeviceDC);
986 hOldBitmap = SelectObject(hMemoryDC, hMemory);
987 SetViewportOrgEx(hMemoryDC, -lpItem->rect.left, -lpItem->rect.top, NULL);
988 hFont = infoPtr->hFont ? infoPtr->hFont : GetStockObject(SYSTEM_FONT);
989 SelectObject(hMemoryDC, hFont);
991 GetClientRect(infoPtr->hwndSelf, &rc);
992 lCDFlags = HEADER_SendCtrlCustomDraw(infoPtr, CDDS_PREPAINT, hMemoryDC, &rc);
993 HEADER_DrawItem(infoPtr, hMemoryDC, iItem, FALSE, lCDFlags);
994 if (lCDFlags & CDRF_NOTIFYPOSTPAINT)
995 HEADER_SendCtrlCustomDraw(infoPtr, CDDS_POSTPAINT, hMemoryDC, &rc);
997 hMemory = SelectObject(hMemoryDC, hOldBitmap);
998 DeleteDC(hMemoryDC);
1000 if (hMemory == NULL) /* if anything failed */
1001 return FALSE;
1003 himl = ImageList_Create(width, height, ILC_COLORDDB, 1, 1);
1004 ImageList_Add(himl, hMemory, NULL);
1005 DeleteObject(hMemory);
1006 return (LRESULT)himl;
1009 static LRESULT
1010 HEADER_SetHotDivider(HEADER_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
1012 INT iDivider;
1013 RECT r;
1015 if (wParam)
1017 POINT pt;
1018 UINT flags;
1019 pt.x = (INT)(SHORT)LOWORD(lParam);
1020 pt.y = 0;
1021 HEADER_InternalHitTest (infoPtr, &pt, &flags, &iDivider);
1023 if (flags & HHT_TOLEFT)
1024 iDivider = 0;
1025 else if (flags & HHT_NOWHERE || flags & HHT_TORIGHT)
1026 iDivider = infoPtr->uNumItem;
1027 else
1029 HEADER_ITEM *lpItem = &infoPtr->items[iDivider];
1030 if (pt.x > (lpItem->rect.left+lpItem->rect.right)/2)
1031 iDivider = HEADER_NextItem(infoPtr, iDivider);
1034 else
1035 iDivider = (INT)lParam;
1037 /* Note; wParam==FALSE, lParam==-1 is valid and is used to clear the hot divider */
1038 if (iDivider<-1 || iDivider>(int)infoPtr->uNumItem)
1039 return iDivider;
1041 if (iDivider != infoPtr->iHotDivider)
1043 if (infoPtr->iHotDivider != -1)
1045 HEADER_GetHotDividerRect(infoPtr, &r);
1046 InvalidateRect(infoPtr->hwndSelf, &r, FALSE);
1048 infoPtr->iHotDivider = iDivider;
1049 if (iDivider != -1)
1051 HEADER_GetHotDividerRect(infoPtr, &r);
1052 InvalidateRect(infoPtr->hwndSelf, &r, FALSE);
1055 return iDivider;
1058 static LRESULT
1059 HEADER_DeleteItem (HEADER_INFO *infoPtr, INT iItem)
1061 INT iOrder;
1062 UINT i;
1064 TRACE("[iItem=%d]\n", iItem);
1066 if ((iItem < 0) || (iItem >= (INT)infoPtr->uNumItem))
1067 return FALSE;
1069 for (i = 0; i < infoPtr->uNumItem; i++)
1070 TRACE("%d: order=%d, iOrder=%d, ->iOrder=%d\n", i, infoPtr->order[i], infoPtr->items[i].iOrder, infoPtr->items[infoPtr->order[i]].iOrder);
1072 iOrder = infoPtr->items[iItem].iOrder;
1073 Free(infoPtr->items[iItem].pszText);
1075 infoPtr->uNumItem--;
1076 memmove(&infoPtr->items[iItem], &infoPtr->items[iItem + 1],
1077 (infoPtr->uNumItem - iItem) * sizeof(HEADER_ITEM));
1078 memmove(&infoPtr->order[iOrder], &infoPtr->order[iOrder + 1],
1079 (infoPtr->uNumItem - iOrder) * sizeof(INT));
1080 infoPtr->items = ReAlloc(infoPtr->items, sizeof(HEADER_ITEM) * infoPtr->uNumItem);
1081 infoPtr->order = ReAlloc(infoPtr->order, sizeof(INT) * infoPtr->uNumItem);
1083 /* Correct the orders */
1084 for (i = 0; i < infoPtr->uNumItem; i++)
1086 if (infoPtr->order[i] > iItem)
1087 infoPtr->order[i]--;
1088 if (i >= iOrder)
1089 infoPtr->items[infoPtr->order[i]].iOrder = i;
1091 for (i = 0; i < infoPtr->uNumItem; i++)
1092 TRACE("%d: order=%d, iOrder=%d, ->iOrder=%d\n", i, infoPtr->order[i], infoPtr->items[i].iOrder, infoPtr->items[infoPtr->order[i]].iOrder);
1094 HEADER_SetItemBounds (infoPtr);
1095 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1097 return TRUE;
1101 static LRESULT
1102 HEADER_GetImageList (const HEADER_INFO *infoPtr)
1104 return (LRESULT)infoPtr->himl;
1108 static LRESULT
1109 HEADER_GetItemT (const HEADER_INFO *infoPtr, INT nItem, LPHDITEMW phdi, BOOL bUnicode)
1111 HEADER_ITEM *lpItem;
1112 UINT mask;
1114 if (!phdi)
1115 return FALSE;
1117 TRACE("[nItem=%d]\n", nItem);
1119 mask = phdi->mask;
1120 if (mask == 0)
1121 return TRUE;
1123 if ((nItem < 0) || (nItem >= (INT)infoPtr->uNumItem))
1124 return FALSE;
1126 if (mask & HDI_UNKNOWN_FIELDS)
1128 TRACE("mask %x contains unknown fields. Using only comctl32 4.0 fields\n", mask);
1129 mask &= HDI_COMCTL32_4_0_FIELDS;
1132 lpItem = &infoPtr->items[nItem];
1133 HEADER_PrepareCallbackItems(infoPtr, nItem, mask);
1135 if (mask & HDI_BITMAP)
1136 phdi->hbm = lpItem->hbm;
1138 if (mask & HDI_FORMAT)
1139 phdi->fmt = lpItem->fmt;
1141 if (mask & HDI_WIDTH)
1142 phdi->cxy = lpItem->cxy;
1144 if (mask & HDI_LPARAM)
1145 phdi->lParam = lpItem->lParam;
1147 if (mask & HDI_IMAGE)
1148 phdi->iImage = lpItem->iImage;
1150 if (mask & HDI_ORDER)
1151 phdi->iOrder = lpItem->iOrder;
1153 if (mask & HDI_TEXT)
1155 if (bUnicode)
1156 Str_GetPtrW (lpItem->pszText, phdi->pszText, phdi->cchTextMax);
1157 else
1158 Str_GetPtrWtoA (lpItem->pszText, (LPSTR)phdi->pszText, phdi->cchTextMax);
1161 HEADER_FreeCallbackItems(lpItem);
1162 return TRUE;
1166 static inline LRESULT
1167 HEADER_GetItemCount (const HEADER_INFO *infoPtr)
1169 return infoPtr->uNumItem;
1173 static LRESULT
1174 HEADER_GetItemRect (const HEADER_INFO *infoPtr, INT iItem, LPRECT lpRect)
1176 if ((iItem < 0) || (iItem >= (INT)infoPtr->uNumItem))
1177 return FALSE;
1179 lpRect->left = infoPtr->items[iItem].rect.left;
1180 lpRect->right = infoPtr->items[iItem].rect.right;
1181 lpRect->top = infoPtr->items[iItem].rect.top;
1182 lpRect->bottom = infoPtr->items[iItem].rect.bottom;
1184 return TRUE;
1188 static LRESULT
1189 HEADER_GetOrderArray(const HEADER_INFO *infoPtr, INT size, LPINT order)
1191 if ((UINT)size <infoPtr->uNumItem)
1192 return FALSE;
1194 memcpy(order, infoPtr->order, infoPtr->uNumItem * sizeof(INT));
1195 return TRUE;
1198 static LRESULT
1199 HEADER_SetOrderArray(HEADER_INFO *infoPtr, INT size, const INT *order)
1201 INT i;
1202 HEADER_ITEM *lpItem;
1204 if ((UINT)size <infoPtr->uNumItem)
1205 return FALSE;
1206 memcpy(infoPtr->order, order, infoPtr->uNumItem * sizeof(INT));
1207 for (i=0; i<size; i++)
1209 lpItem = &infoPtr->items[*order++];
1210 lpItem->iOrder=i;
1212 HEADER_SetItemBounds(infoPtr);
1213 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1214 return TRUE;
1217 static inline LRESULT
1218 HEADER_GetUnicodeFormat (const HEADER_INFO *infoPtr)
1220 return (infoPtr->nNotifyFormat == NFR_UNICODE);
1224 static LRESULT
1225 HEADER_HitTest (const HEADER_INFO *infoPtr, LPHDHITTESTINFO phti)
1227 HEADER_InternalHitTest (infoPtr, &phti->pt, &phti->flags, &phti->iItem);
1229 if (phti->flags == HHT_NOWHERE)
1230 return -1;
1231 else
1232 return phti->iItem;
1236 static LRESULT
1237 HEADER_InsertItemT (HEADER_INFO *infoPtr, INT nItem, const HDITEMW *phdi, BOOL bUnicode)
1239 HEADER_ITEM *lpItem;
1240 INT iOrder;
1241 UINT i;
1242 UINT copyMask;
1244 if ((phdi == NULL) || (nItem < 0) || (phdi->mask == 0))
1245 return -1;
1247 if (nItem > infoPtr->uNumItem)
1248 nItem = infoPtr->uNumItem;
1250 iOrder = (phdi->mask & HDI_ORDER) ? phdi->iOrder : nItem;
1251 if (iOrder < 0)
1252 iOrder = 0;
1253 else if (infoPtr->uNumItem < iOrder)
1254 iOrder = infoPtr->uNumItem;
1256 infoPtr->uNumItem++;
1257 infoPtr->items = ReAlloc(infoPtr->items, sizeof(HEADER_ITEM) * infoPtr->uNumItem);
1258 infoPtr->order = ReAlloc(infoPtr->order, sizeof(INT) * infoPtr->uNumItem);
1260 /* make space for the new item */
1261 memmove(&infoPtr->items[nItem + 1], &infoPtr->items[nItem],
1262 (infoPtr->uNumItem - nItem - 1) * sizeof(HEADER_ITEM));
1263 memmove(&infoPtr->order[iOrder + 1], &infoPtr->order[iOrder],
1264 (infoPtr->uNumItem - iOrder - 1) * sizeof(INT));
1266 /* update the order array */
1267 infoPtr->order[iOrder] = nItem;
1268 for (i = 0; i < infoPtr->uNumItem; i++)
1270 if (i != iOrder && infoPtr->order[i] >= nItem)
1271 infoPtr->order[i]++;
1272 infoPtr->items[infoPtr->order[i]].iOrder = i;
1275 lpItem = &infoPtr->items[nItem];
1276 ZeroMemory(lpItem, sizeof(HEADER_ITEM));
1277 /* cxy, fmt and lParam are copied even if not in the HDITEM mask */
1278 copyMask = phdi->mask | HDI_WIDTH | HDI_FORMAT | HDI_LPARAM;
1279 HEADER_StoreHDItemInHeader(lpItem, copyMask, phdi, bUnicode);
1280 lpItem->iOrder = iOrder;
1282 /* set automatically some format bits */
1283 if (phdi->mask & HDI_TEXT)
1284 lpItem->fmt |= HDF_STRING;
1285 else
1286 lpItem->fmt &= ~HDF_STRING;
1288 if (lpItem->hbm != NULL)
1289 lpItem->fmt |= HDF_BITMAP;
1290 else
1291 lpItem->fmt &= ~HDF_BITMAP;
1293 if (phdi->mask & HDI_IMAGE)
1294 lpItem->fmt |= HDF_IMAGE;
1296 HEADER_SetItemBounds (infoPtr);
1297 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1299 return nItem;
1303 static LRESULT
1304 HEADER_Layout (HEADER_INFO *infoPtr, LPHDLAYOUT lpLayout)
1306 lpLayout->pwpos->hwnd = infoPtr->hwndSelf;
1307 lpLayout->pwpos->hwndInsertAfter = 0;
1308 lpLayout->pwpos->x = lpLayout->prc->left;
1309 lpLayout->pwpos->y = lpLayout->prc->top;
1310 lpLayout->pwpos->cx = lpLayout->prc->right - lpLayout->prc->left;
1311 if (infoPtr->dwStyle & HDS_HIDDEN)
1312 lpLayout->pwpos->cy = 0;
1313 else {
1314 lpLayout->pwpos->cy = infoPtr->nHeight;
1315 lpLayout->prc->top += infoPtr->nHeight;
1317 lpLayout->pwpos->flags = SWP_NOZORDER;
1319 TRACE("Layout x=%d y=%d cx=%d cy=%d\n",
1320 lpLayout->pwpos->x, lpLayout->pwpos->y,
1321 lpLayout->pwpos->cx, lpLayout->pwpos->cy);
1323 infoPtr->bRectsValid = FALSE;
1325 return TRUE;
1329 static LRESULT
1330 HEADER_SetImageList (HEADER_INFO *infoPtr, HIMAGELIST himl)
1332 HIMAGELIST himlOld;
1334 TRACE("(himl %p)\n", himl);
1335 himlOld = infoPtr->himl;
1336 infoPtr->himl = himl;
1338 /* FIXME: Refresh needed??? */
1340 return (LRESULT)himlOld;
1344 static LRESULT
1345 HEADER_GetBitmapMargin(const HEADER_INFO *infoPtr)
1347 return infoPtr->iMargin;
1350 static LRESULT
1351 HEADER_SetBitmapMargin(HEADER_INFO *infoPtr, INT iMargin)
1353 INT oldMargin = infoPtr->iMargin;
1355 infoPtr->iMargin = iMargin;
1357 return oldMargin;
1360 static LRESULT
1361 HEADER_SetItemT (HEADER_INFO *infoPtr, INT nItem, const HDITEMW *phdi, BOOL bUnicode)
1363 HEADER_ITEM *lpItem;
1364 HDITEMW hdNotify;
1365 void *pvScratch;
1367 if (phdi == NULL)
1368 return FALSE;
1369 if ((nItem < 0) || (nItem >= (INT)infoPtr->uNumItem))
1370 return FALSE;
1372 TRACE("[nItem=%d]\n", nItem);
1374 HEADER_CopyHDItemForNotify(infoPtr, &hdNotify, phdi, bUnicode, &pvScratch);
1375 if (HEADER_SendNotifyWithHDItemT(infoPtr, HDN_ITEMCHANGINGW, nItem, &hdNotify))
1377 Free(pvScratch);
1378 return FALSE;
1381 lpItem = &infoPtr->items[nItem];
1382 HEADER_StoreHDItemInHeader(lpItem, phdi->mask, phdi, bUnicode);
1384 if (phdi->mask & HDI_ORDER)
1385 if (phdi->iOrder >= 0 && phdi->iOrder < infoPtr->uNumItem)
1386 HEADER_ChangeItemOrder(infoPtr, nItem, phdi->iOrder);
1388 HEADER_SendNotifyWithHDItemT(infoPtr, HDN_ITEMCHANGEDW, nItem, &hdNotify);
1390 HEADER_SetItemBounds (infoPtr);
1392 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1394 Free(pvScratch);
1395 return TRUE;
1398 static inline LRESULT
1399 HEADER_SetUnicodeFormat (HEADER_INFO *infoPtr, WPARAM wParam)
1401 BOOL bTemp = (infoPtr->nNotifyFormat == NFR_UNICODE);
1403 infoPtr->nNotifyFormat = ((BOOL)wParam ? NFR_UNICODE : NFR_ANSI);
1405 return bTemp;
1409 static LRESULT
1410 HEADER_Create (HWND hwnd, const CREATESTRUCTW *lpcs)
1412 HEADER_INFO *infoPtr;
1413 TEXTMETRICW tm;
1414 HFONT hOldFont;
1415 HDC hdc;
1417 infoPtr = Alloc (sizeof(HEADER_INFO));
1418 SetWindowLongPtrW (hwnd, 0, (DWORD_PTR)infoPtr);
1420 infoPtr->hwndSelf = hwnd;
1421 infoPtr->hwndNotify = lpcs->hwndParent;
1422 infoPtr->uNumItem = 0;
1423 infoPtr->hFont = 0;
1424 infoPtr->items = 0;
1425 infoPtr->order = 0;
1426 infoPtr->bRectsValid = FALSE;
1427 infoPtr->hcurArrow = LoadCursorW (0, (LPWSTR)IDC_ARROW);
1428 infoPtr->hcurDivider = LoadCursorW (COMCTL32_hModule, MAKEINTRESOURCEW(IDC_DIVIDER));
1429 infoPtr->hcurDivopen = LoadCursorW (COMCTL32_hModule, MAKEINTRESOURCEW(IDC_DIVIDEROPEN));
1430 infoPtr->bPressed = FALSE;
1431 infoPtr->bTracking = FALSE;
1432 infoPtr->dwStyle = lpcs->style;
1433 infoPtr->iMoveItem = 0;
1434 infoPtr->himl = 0;
1435 infoPtr->iHotItem = -1;
1436 infoPtr->iHotDivider = -1;
1437 infoPtr->iMargin = 3*GetSystemMetrics(SM_CXEDGE);
1438 infoPtr->nNotifyFormat =
1439 SendMessageW (infoPtr->hwndNotify, WM_NOTIFYFORMAT, (WPARAM)hwnd, NF_QUERY);
1441 hdc = GetDC (0);
1442 hOldFont = SelectObject (hdc, GetStockObject (SYSTEM_FONT));
1443 GetTextMetricsW (hdc, &tm);
1444 infoPtr->nHeight = tm.tmHeight + VERT_BORDER;
1445 SelectObject (hdc, hOldFont);
1446 ReleaseDC (0, hdc);
1448 OpenThemeData(hwnd, themeClass);
1450 return 0;
1454 static LRESULT
1455 HEADER_Destroy (HEADER_INFO *infoPtr)
1457 HTHEME theme = GetWindowTheme(infoPtr->hwndSelf);
1458 CloseThemeData(theme);
1459 return 0;
1462 static LRESULT
1463 HEADER_NCDestroy (HEADER_INFO *infoPtr)
1465 HEADER_ITEM *lpItem;
1466 INT nItem;
1468 if (infoPtr->items) {
1469 lpItem = infoPtr->items;
1470 for (nItem = 0; nItem < infoPtr->uNumItem; nItem++, lpItem++) {
1471 Free(lpItem->pszText);
1473 Free (infoPtr->items);
1476 Free(infoPtr->order);
1478 if (infoPtr->himl)
1479 ImageList_Destroy (infoPtr->himl);
1481 SetWindowLongPtrW (infoPtr->hwndSelf, 0, 0);
1482 Free (infoPtr);
1484 return 0;
1488 static inline LRESULT
1489 HEADER_GetFont (const HEADER_INFO *infoPtr)
1491 return (LRESULT)infoPtr->hFont;
1495 static BOOL
1496 HEADER_IsDragDistance(const HEADER_INFO *infoPtr, const POINT *pt)
1498 /* Windows allows for a mouse movement before starting the drag. We use the
1499 * SM_CXDOUBLECLICK/SM_CYDOUBLECLICK as that distance.
1501 return (abs(infoPtr->ptLButtonDown.x - pt->x)>GetSystemMetrics(SM_CXDOUBLECLK) ||
1502 abs(infoPtr->ptLButtonDown.y - pt->y)>GetSystemMetrics(SM_CYDOUBLECLK));
1505 static LRESULT
1506 HEADER_LButtonDblClk (const HEADER_INFO *infoPtr, INT x, INT y)
1508 POINT pt;
1509 UINT flags;
1510 INT nItem;
1512 pt.x = x;
1513 pt.y = y;
1514 HEADER_InternalHitTest (infoPtr, &pt, &flags, &nItem);
1516 if ((infoPtr->dwStyle & HDS_BUTTONS) && (flags == HHT_ONHEADER))
1517 HEADER_SendNotifyWithHDItemT(infoPtr, HDN_ITEMDBLCLICKW, nItem, NULL);
1518 else if ((flags == HHT_ONDIVIDER) || (flags == HHT_ONDIVOPEN))
1519 HEADER_SendNotifyWithHDItemT(infoPtr, HDN_DIVIDERDBLCLICKW, nItem, NULL);
1521 return 0;
1525 static LRESULT
1526 HEADER_LButtonDown (HEADER_INFO *infoPtr, INT x, INT y)
1528 POINT pt;
1529 UINT flags;
1530 INT nItem;
1531 HDC hdc;
1533 pt.x = x;
1534 pt.y = y;
1535 HEADER_InternalHitTest (infoPtr, &pt, &flags, &nItem);
1537 if ((infoPtr->dwStyle & HDS_BUTTONS) && (flags == HHT_ONHEADER)) {
1538 SetCapture (infoPtr->hwndSelf);
1539 infoPtr->bCaptured = TRUE;
1540 infoPtr->bPressed = TRUE;
1541 infoPtr->bDragging = FALSE;
1542 infoPtr->iMoveItem = nItem;
1543 infoPtr->ptLButtonDown = pt;
1545 infoPtr->items[nItem].bDown = TRUE;
1547 /* Send WM_CUSTOMDRAW */
1548 hdc = GetDC (infoPtr->hwndSelf);
1549 HEADER_RefreshItem (infoPtr, nItem);
1550 ReleaseDC (infoPtr->hwndSelf, hdc);
1552 TRACE("Pressed item %d!\n", nItem);
1554 else if ((flags == HHT_ONDIVIDER) || (flags == HHT_ONDIVOPEN)) {
1555 INT iCurrWidth = infoPtr->items[nItem].cxy;
1556 if (!HEADER_SendNotifyWithIntFieldT(infoPtr, HDN_BEGINTRACKW, nItem, HDI_WIDTH, iCurrWidth))
1558 SetCapture (infoPtr->hwndSelf);
1559 infoPtr->bCaptured = TRUE;
1560 infoPtr->bTracking = TRUE;
1561 infoPtr->iMoveItem = nItem;
1562 infoPtr->xTrackOffset = infoPtr->items[nItem].rect.right - pt.x;
1564 if (!(infoPtr->dwStyle & HDS_FULLDRAG)) {
1565 infoPtr->xOldTrack = infoPtr->items[nItem].rect.right;
1566 hdc = GetDC (infoPtr->hwndSelf);
1567 HEADER_DrawTrackLine (infoPtr, hdc, infoPtr->xOldTrack);
1568 ReleaseDC (infoPtr->hwndSelf, hdc);
1571 TRACE("Begin tracking item %d!\n", nItem);
1575 return 0;
1579 static LRESULT
1580 HEADER_LButtonUp (HEADER_INFO *infoPtr, INT x, INT y)
1582 POINT pt;
1583 UINT flags;
1584 INT nItem;
1585 HDC hdc;
1587 pt.x = x;
1588 pt.y = y;
1589 HEADER_InternalHitTest (infoPtr, &pt, &flags, &nItem);
1591 if (infoPtr->bPressed) {
1593 infoPtr->items[infoPtr->iMoveItem].bDown = FALSE;
1595 if (infoPtr->bDragging)
1597 HEADER_ITEM *lpItem = &infoPtr->items[infoPtr->iMoveItem];
1598 INT iNewOrder;
1600 ImageList_DragShowNolock(FALSE);
1601 ImageList_EndDrag();
1603 if (infoPtr->iHotDivider == -1)
1604 iNewOrder = -1;
1605 else if (infoPtr->iHotDivider == infoPtr->uNumItem)
1606 iNewOrder = infoPtr->uNumItem-1;
1607 else
1609 iNewOrder = HEADER_IndexToOrder(infoPtr, infoPtr->iHotDivider);
1610 if (iNewOrder > lpItem->iOrder)
1611 iNewOrder--;
1614 if (iNewOrder != -1 &&
1615 !HEADER_SendNotifyWithIntFieldT(infoPtr, HDN_ENDDRAG, infoPtr->iMoveItem, HDI_ORDER, iNewOrder))
1617 HEADER_ChangeItemOrder(infoPtr, infoPtr->iMoveItem, iNewOrder);
1618 infoPtr->bRectsValid = FALSE;
1619 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1621 else
1622 InvalidateRect(infoPtr->hwndSelf, &infoPtr->items[infoPtr->iMoveItem].rect, FALSE);
1624 infoPtr->bDragging = FALSE;
1625 HEADER_SetHotDivider(infoPtr, FALSE, -1);
1627 else
1629 hdc = GetDC (infoPtr->hwndSelf);
1630 HEADER_RefreshItem (infoPtr, infoPtr->iMoveItem);
1631 ReleaseDC (infoPtr->hwndSelf, hdc);
1633 if (!(infoPtr->dwStyle & HDS_DRAGDROP) || !HEADER_IsDragDistance(infoPtr, &pt))
1634 HEADER_SendNotifyWithHDItemT(infoPtr, HDN_ITEMCLICKW, infoPtr->iMoveItem, NULL);
1637 TRACE("Released item %d!\n", infoPtr->iMoveItem);
1638 infoPtr->bPressed = FALSE;
1640 else if (infoPtr->bTracking) {
1641 INT iNewWidth = pt.x - infoPtr->items[infoPtr->iMoveItem].rect.left + infoPtr->xTrackOffset;
1642 if (iNewWidth < 0)
1643 iNewWidth = 0;
1644 TRACE("End tracking item %d!\n", infoPtr->iMoveItem);
1645 infoPtr->bTracking = FALSE;
1647 HEADER_SendNotifyWithIntFieldT(infoPtr, HDN_ENDTRACKW, infoPtr->iMoveItem, HDI_WIDTH, iNewWidth);
1649 if (!(infoPtr->dwStyle & HDS_FULLDRAG)) {
1650 hdc = GetDC (infoPtr->hwndSelf);
1651 HEADER_DrawTrackLine (infoPtr, hdc, infoPtr->xOldTrack);
1652 ReleaseDC (infoPtr->hwndSelf, hdc);
1655 if (!HEADER_SendNotifyWithIntFieldT(infoPtr, HDN_ITEMCHANGINGW, infoPtr->iMoveItem, HDI_WIDTH, iNewWidth))
1657 infoPtr->items[infoPtr->iMoveItem].cxy = iNewWidth;
1658 HEADER_SendNotifyWithIntFieldT(infoPtr, HDN_ITEMCHANGEDW, infoPtr->iMoveItem, HDI_WIDTH, iNewWidth);
1661 HEADER_SetItemBounds (infoPtr);
1662 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1665 if (infoPtr->bCaptured) {
1666 infoPtr->bCaptured = FALSE;
1667 ReleaseCapture ();
1668 HEADER_SendSimpleNotify (infoPtr, NM_RELEASEDCAPTURE);
1671 return 0;
1675 static LRESULT
1676 HEADER_NotifyFormat (HEADER_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
1678 switch (lParam)
1680 case NF_QUERY:
1681 return infoPtr->nNotifyFormat;
1683 case NF_REQUERY:
1684 infoPtr->nNotifyFormat =
1685 SendMessageW ((HWND)wParam, WM_NOTIFYFORMAT,
1686 (WPARAM)infoPtr->hwndSelf, (LPARAM)NF_QUERY);
1687 return infoPtr->nNotifyFormat;
1690 return 0;
1693 static LRESULT
1694 HEADER_MouseLeave (HEADER_INFO *infoPtr)
1696 /* Reset hot-tracked item when mouse leaves control. */
1697 INT oldHotItem = infoPtr->iHotItem;
1698 HDC hdc = GetDC (infoPtr->hwndSelf);
1700 infoPtr->iHotItem = -1;
1701 if (oldHotItem != -1) HEADER_RefreshItem (infoPtr, oldHotItem);
1702 ReleaseDC (infoPtr->hwndSelf, hdc);
1704 return 0;
1708 static LRESULT
1709 HEADER_MouseMove (HEADER_INFO *infoPtr, LPARAM lParam)
1711 POINT pt;
1712 UINT flags;
1713 INT nItem, nWidth;
1714 HDC hdc;
1715 /* With theming, hottracking is always enabled */
1716 BOOL hotTrackEnabled =
1717 ((infoPtr->dwStyle & HDS_BUTTONS) && (infoPtr->dwStyle & HDS_HOTTRACK))
1718 || (GetWindowTheme (infoPtr->hwndSelf) != NULL);
1719 INT oldHotItem = infoPtr->iHotItem;
1721 pt.x = (INT)(SHORT)LOWORD(lParam);
1722 pt.y = (INT)(SHORT)HIWORD(lParam);
1723 HEADER_InternalHitTest (infoPtr, &pt, &flags, &nItem);
1725 if (hotTrackEnabled) {
1726 if (flags & (HHT_ONHEADER | HHT_ONDIVIDER | HHT_ONDIVOPEN))
1727 infoPtr->iHotItem = nItem;
1728 else
1729 infoPtr->iHotItem = -1;
1732 if (infoPtr->bCaptured) {
1733 /* check if we should drag the header */
1734 if (infoPtr->bPressed && !infoPtr->bDragging && (infoPtr->dwStyle & HDS_DRAGDROP)
1735 && HEADER_IsDragDistance(infoPtr, &pt))
1737 if (!HEADER_SendNotifyWithHDItemT(infoPtr, HDN_BEGINDRAG, infoPtr->iMoveItem, NULL))
1739 HIMAGELIST hDragItem = (HIMAGELIST)HEADER_CreateDragImage(infoPtr, infoPtr->iMoveItem);
1740 if (hDragItem != NULL)
1742 HEADER_ITEM *lpItem = &infoPtr->items[infoPtr->iMoveItem];
1743 TRACE("Starting item drag\n");
1744 ImageList_BeginDrag(hDragItem, 0, pt.x - lpItem->rect.left, 0);
1745 ImageList_DragShowNolock(TRUE);
1746 ImageList_Destroy(hDragItem);
1747 infoPtr->bDragging = TRUE;
1752 if (infoPtr->bDragging)
1754 POINT drag;
1755 drag.x = pt.x;
1756 drag.y = 0;
1757 ClientToScreen(infoPtr->hwndSelf, &drag);
1758 ImageList_DragMove(drag.x, drag.y);
1759 HEADER_SetHotDivider(infoPtr, TRUE, lParam);
1762 if (infoPtr->bPressed && !infoPtr->bDragging) {
1763 BOOL oldState = infoPtr->items[infoPtr->iMoveItem].bDown;
1764 if ((nItem == infoPtr->iMoveItem) && (flags == HHT_ONHEADER))
1765 infoPtr->items[infoPtr->iMoveItem].bDown = TRUE;
1766 else
1767 infoPtr->items[infoPtr->iMoveItem].bDown = FALSE;
1768 if (oldState != infoPtr->items[infoPtr->iMoveItem].bDown) {
1769 hdc = GetDC (infoPtr->hwndSelf);
1770 HEADER_RefreshItem (infoPtr, infoPtr->iMoveItem);
1771 ReleaseDC (infoPtr->hwndSelf, hdc);
1774 TRACE("Moving pressed item %d!\n", infoPtr->iMoveItem);
1776 else if (infoPtr->bTracking) {
1777 if (infoPtr->dwStyle & HDS_FULLDRAG) {
1778 HEADER_ITEM *lpItem = &infoPtr->items[infoPtr->iMoveItem];
1779 nWidth = pt.x - lpItem->rect.left + infoPtr->xTrackOffset;
1780 if (!HEADER_SendNotifyWithIntFieldT(infoPtr, HDN_ITEMCHANGINGW, infoPtr->iMoveItem, HDI_WIDTH, nWidth))
1782 INT nOldWidth = lpItem->rect.right - lpItem->rect.left;
1783 RECT rcClient;
1784 RECT rcScroll;
1786 if (nWidth < 0) nWidth = 0;
1787 infoPtr->items[infoPtr->iMoveItem].cxy = nWidth;
1788 HEADER_SetItemBounds(infoPtr);
1790 GetClientRect(infoPtr->hwndSelf, &rcClient);
1791 rcScroll = rcClient;
1792 rcScroll.left = lpItem->rect.left + nOldWidth;
1793 ScrollWindowEx(infoPtr->hwndSelf, nWidth - nOldWidth, 0, &rcScroll, &rcClient, NULL, NULL, 0);
1794 InvalidateRect(infoPtr->hwndSelf, &lpItem->rect, FALSE);
1795 UpdateWindow(infoPtr->hwndSelf);
1797 HEADER_SendNotifyWithIntFieldT(infoPtr, HDN_ITEMCHANGEDW, infoPtr->iMoveItem, HDI_WIDTH, nWidth);
1800 else {
1801 INT iTrackWidth;
1802 hdc = GetDC (infoPtr->hwndSelf);
1803 HEADER_DrawTrackLine (infoPtr, hdc, infoPtr->xOldTrack);
1804 infoPtr->xOldTrack = pt.x + infoPtr->xTrackOffset;
1805 if (infoPtr->xOldTrack < infoPtr->items[infoPtr->iMoveItem].rect.left)
1806 infoPtr->xOldTrack = infoPtr->items[infoPtr->iMoveItem].rect.left;
1807 HEADER_DrawTrackLine (infoPtr, hdc, infoPtr->xOldTrack);
1808 ReleaseDC (infoPtr->hwndSelf, hdc);
1809 iTrackWidth = infoPtr->xOldTrack - infoPtr->items[infoPtr->iMoveItem].rect.left;
1810 /* FIXME: should stop tracking if HDN_TRACK returns TRUE */
1811 HEADER_SendNotifyWithIntFieldT(infoPtr, HDN_TRACKW, infoPtr->iMoveItem, HDI_WIDTH, iTrackWidth);
1814 TRACE("Tracking item %d!\n", infoPtr->iMoveItem);
1818 if (hotTrackEnabled) {
1819 TRACKMOUSEEVENT tme;
1820 if (oldHotItem != infoPtr->iHotItem && !infoPtr->bDragging) {
1821 hdc = GetDC (infoPtr->hwndSelf);
1822 if (oldHotItem != -1) HEADER_RefreshItem (infoPtr, oldHotItem);
1823 if (infoPtr->iHotItem != -1) HEADER_RefreshItem (infoPtr, infoPtr->iHotItem);
1824 ReleaseDC (infoPtr->hwndSelf, hdc);
1826 tme.cbSize = sizeof( tme );
1827 tme.dwFlags = TME_LEAVE;
1828 tme.hwndTrack = infoPtr->hwndSelf;
1829 TrackMouseEvent( &tme );
1832 return 0;
1836 static LRESULT
1837 HEADER_Paint (HEADER_INFO *infoPtr, HDC hdcParam)
1839 HDC hdc;
1840 PAINTSTRUCT ps;
1842 hdc = hdcParam==0 ? BeginPaint (infoPtr->hwndSelf, &ps) : hdcParam;
1843 HEADER_Refresh (infoPtr, hdc);
1844 if(!hdcParam)
1845 EndPaint (infoPtr->hwndSelf, &ps);
1846 return 0;
1850 static LRESULT
1851 HEADER_RButtonUp (HEADER_INFO *infoPtr, INT x, INT y)
1853 BOOL bRet;
1854 POINT pt;
1856 pt.x = x;
1857 pt.y = y;
1859 /* Send a Notify message */
1860 bRet = HEADER_SendSimpleNotify (infoPtr, NM_RCLICK);
1862 /* Change to screen coordinate for WM_CONTEXTMENU */
1863 ClientToScreen(infoPtr->hwndSelf, &pt);
1865 /* Send a WM_CONTEXTMENU message in response to the RBUTTONUP */
1866 SendMessageW( infoPtr->hwndSelf, WM_CONTEXTMENU, (WPARAM) infoPtr->hwndSelf, MAKELPARAM(pt.x, pt.y));
1868 return bRet;
1872 static LRESULT
1873 HEADER_SetCursor (HEADER_INFO *infoPtr, LPARAM lParam)
1875 POINT pt;
1876 UINT flags;
1877 INT nItem;
1879 TRACE("code=0x%X id=0x%X\n", LOWORD(lParam), HIWORD(lParam));
1881 GetCursorPos (&pt);
1882 ScreenToClient (infoPtr->hwndSelf, &pt);
1884 HEADER_InternalHitTest (infoPtr, &pt, &flags, &nItem);
1886 if (flags == HHT_ONDIVIDER)
1887 SetCursor (infoPtr->hcurDivider);
1888 else if (flags == HHT_ONDIVOPEN)
1889 SetCursor (infoPtr->hcurDivopen);
1890 else
1891 SetCursor (infoPtr->hcurArrow);
1893 return 0;
1897 static LRESULT
1898 HEADER_SetFont (HEADER_INFO *infoPtr, HFONT hFont, WORD Redraw)
1900 TEXTMETRICW tm;
1901 HFONT hOldFont;
1902 HDC hdc;
1904 infoPtr->hFont = hFont;
1906 hdc = GetDC (0);
1907 hOldFont = SelectObject (hdc, infoPtr->hFont ? infoPtr->hFont : GetStockObject (SYSTEM_FONT));
1908 GetTextMetricsW (hdc, &tm);
1909 infoPtr->nHeight = tm.tmHeight + VERT_BORDER;
1910 SelectObject (hdc, hOldFont);
1911 ReleaseDC (0, hdc);
1913 infoPtr->bRectsValid = FALSE;
1915 if (Redraw) {
1916 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1919 return 0;
1922 static LRESULT HEADER_SetRedraw(HEADER_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
1924 /* ignoring the InvalidateRect calls is handled by user32. But some apps expect
1925 * that we invalidate the header and this has to be done manually */
1926 LRESULT ret;
1928 ret = DefWindowProcW(infoPtr->hwndSelf, WM_SETREDRAW, wParam, lParam);
1929 if (wParam)
1930 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1931 return ret;
1934 static INT HEADER_StyleChanged(HEADER_INFO *infoPtr, WPARAM wStyleType,
1935 const STYLESTRUCT *lpss)
1937 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
1938 wStyleType, lpss->styleOld, lpss->styleNew);
1940 if (wStyleType != GWL_STYLE) return 0;
1942 infoPtr->dwStyle = lpss->styleNew;
1944 return 0;
1947 /* Update the theme handle after a theme change */
1948 static LRESULT HEADER_ThemeChanged(const HEADER_INFO *infoPtr)
1950 HTHEME theme = GetWindowTheme(infoPtr->hwndSelf);
1951 CloseThemeData(theme);
1952 OpenThemeData(infoPtr->hwndSelf, themeClass);
1953 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1954 return 0;
1958 static LRESULT WINAPI
1959 HEADER_WindowProc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
1961 HEADER_INFO *infoPtr = (HEADER_INFO *)GetWindowLongPtrW(hwnd, 0);
1963 TRACE("hwnd=%p msg=%x wparam=%lx lParam=%lx\n", hwnd, msg, wParam, lParam);
1964 if (!infoPtr && (msg != WM_CREATE))
1965 return DefWindowProcW (hwnd, msg, wParam, lParam);
1966 switch (msg) {
1967 /* case HDM_CLEARFILTER: */
1969 case HDM_CREATEDRAGIMAGE:
1970 return HEADER_CreateDragImage (infoPtr, (INT)wParam);
1972 case HDM_DELETEITEM:
1973 return HEADER_DeleteItem (infoPtr, (INT)wParam);
1975 /* case HDM_EDITFILTER: */
1977 case HDM_GETBITMAPMARGIN:
1978 return HEADER_GetBitmapMargin(infoPtr);
1980 case HDM_GETIMAGELIST:
1981 return HEADER_GetImageList (infoPtr);
1983 case HDM_GETITEMA:
1984 case HDM_GETITEMW:
1985 return HEADER_GetItemT (infoPtr, (INT)wParam, (LPHDITEMW)lParam, msg == HDM_GETITEMW);
1987 case HDM_GETITEMCOUNT:
1988 return HEADER_GetItemCount (infoPtr);
1990 case HDM_GETITEMRECT:
1991 return HEADER_GetItemRect (infoPtr, (INT)wParam, (LPRECT)lParam);
1993 case HDM_GETORDERARRAY:
1994 return HEADER_GetOrderArray(infoPtr, (INT)wParam, (LPINT)lParam);
1996 case HDM_GETUNICODEFORMAT:
1997 return HEADER_GetUnicodeFormat (infoPtr);
1999 case HDM_HITTEST:
2000 return HEADER_HitTest (infoPtr, (LPHDHITTESTINFO)lParam);
2002 case HDM_INSERTITEMA:
2003 case HDM_INSERTITEMW:
2004 return HEADER_InsertItemT (infoPtr, (INT)wParam, (LPHDITEMW)lParam, msg == HDM_INSERTITEMW);
2006 case HDM_LAYOUT:
2007 return HEADER_Layout (infoPtr, (LPHDLAYOUT)lParam);
2009 case HDM_ORDERTOINDEX:
2010 return HEADER_OrderToIndex(infoPtr, (INT)wParam);
2012 case HDM_SETBITMAPMARGIN:
2013 return HEADER_SetBitmapMargin(infoPtr, (INT)wParam);
2015 /* case HDM_SETFILTERCHANGETIMEOUT: */
2017 case HDM_SETHOTDIVIDER:
2018 return HEADER_SetHotDivider(infoPtr, wParam, lParam);
2020 case HDM_SETIMAGELIST:
2021 return HEADER_SetImageList (infoPtr, (HIMAGELIST)lParam);
2023 case HDM_SETITEMA:
2024 case HDM_SETITEMW:
2025 return HEADER_SetItemT (infoPtr, (INT)wParam, (LPHDITEMW)lParam, msg == HDM_SETITEMW);
2027 case HDM_SETORDERARRAY:
2028 return HEADER_SetOrderArray(infoPtr, (INT)wParam, (LPINT)lParam);
2030 case HDM_SETUNICODEFORMAT:
2031 return HEADER_SetUnicodeFormat (infoPtr, wParam);
2033 case WM_CREATE:
2034 return HEADER_Create (hwnd, (LPCREATESTRUCTW)lParam);
2036 case WM_DESTROY:
2037 return HEADER_Destroy (infoPtr);
2039 case WM_NCDESTROY:
2040 return HEADER_NCDestroy (infoPtr);
2042 case WM_ERASEBKGND:
2043 return 1;
2045 case WM_GETDLGCODE:
2046 return DLGC_WANTTAB | DLGC_WANTARROWS;
2048 case WM_GETFONT:
2049 return HEADER_GetFont (infoPtr);
2051 case WM_LBUTTONDBLCLK:
2052 return HEADER_LButtonDblClk (infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
2054 case WM_LBUTTONDOWN:
2055 return HEADER_LButtonDown (infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
2057 case WM_LBUTTONUP:
2058 return HEADER_LButtonUp (infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
2060 case WM_MOUSELEAVE:
2061 return HEADER_MouseLeave (infoPtr);
2063 case WM_MOUSEMOVE:
2064 return HEADER_MouseMove (infoPtr, lParam);
2066 case WM_NOTIFYFORMAT:
2067 return HEADER_NotifyFormat (infoPtr, wParam, lParam);
2069 case WM_SIZE:
2070 return HEADER_Size (infoPtr);
2072 case WM_THEMECHANGED:
2073 return HEADER_ThemeChanged (infoPtr);
2075 case WM_PRINTCLIENT:
2076 case WM_PAINT:
2077 return HEADER_Paint (infoPtr, (HDC)wParam);
2079 case WM_RBUTTONUP:
2080 return HEADER_RButtonUp (infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
2082 case WM_SETCURSOR:
2083 return HEADER_SetCursor (infoPtr, lParam);
2085 case WM_SETFONT:
2086 return HEADER_SetFont (infoPtr, (HFONT)wParam, (WORD)lParam);
2088 case WM_SETREDRAW:
2089 return HEADER_SetRedraw(infoPtr, wParam, lParam);
2091 case WM_STYLECHANGED:
2092 return HEADER_StyleChanged(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
2094 case WM_SYSCOLORCHANGE:
2095 COMCTL32_RefreshSysColors();
2096 return 0;
2098 default:
2099 if ((msg >= WM_USER) && (msg < WM_APP) && !COMCTL32_IsReflectedMessage(msg))
2100 ERR("unknown msg %04x wp=%04lx lp=%08lx\n",
2101 msg, wParam, lParam );
2102 return DefWindowProcW(hwnd, msg, wParam, lParam);
2107 VOID
2108 HEADER_Register (void)
2110 WNDCLASSW wndClass;
2112 ZeroMemory (&wndClass, sizeof(WNDCLASSW));
2113 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS;
2114 wndClass.lpfnWndProc = HEADER_WindowProc;
2115 wndClass.cbClsExtra = 0;
2116 wndClass.cbWndExtra = sizeof(HEADER_INFO *);
2117 wndClass.hCursor = LoadCursorW (0, (LPWSTR)IDC_ARROW);
2118 wndClass.lpszClassName = WC_HEADERW;
2120 RegisterClassW (&wndClass);
2124 VOID
2125 HEADER_Unregister (void)
2127 UnregisterClassW (WC_HEADERW, NULL);