comctl32: Remove superfluous pointer casts.
[wine] / dlls / comctl32 / header.c
1 /*
2  *  Header control
3  *
4  *  Copyright 1998 Eric Kohl
5  *  Copyright 2000 Eric Kohl for CodeWeavers
6  *  Copyright 2003 Maxime Bellenge
7  *  Copyright 2006 Mikolaj Zalewski
8  *
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.
13  *
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.
18  *
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
22  *
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
28  */
29
30 #include <stdarg.h>
31 #include <stdlib.h>
32 #include <string.h>
33
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"
46
47 WINE_DEFAULT_DEBUG_CHANNEL(header);
48
49 typedef struct
50 {
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 */
58
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;
63
64
65 typedef struct
66 {
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     INT       iMoveItem;        /* index of tracked item. (Tracking mode) */
81     INT       xTrackOffset;     /* distance between the right side of the tracked item and the cursor */
82     INT       xOldTrack;        /* track offset (see above) after the last WM_MOUSEMOVE */
83     INT       iHotItem;         /* index of hot item (cursor is over this item) */
84     INT       iHotDivider;      /* index of the hot divider (used while dragging an item or by HDM_SETHOTDIVIDER) */
85     INT       iMargin;          /* width of the margin that surrounds a bitmap */
86
87     HIMAGELIST  himl;           /* handle to an image list (may be 0) */
88     HEADER_ITEM *items;         /* pointer to array of HEADER_ITEM's */
89     INT         *order;         /* array of item IDs indexed by order */
90     BOOL        bRectsValid;    /* validity flag for bounding rectangles */
91 } HEADER_INFO;
92
93
94 #define VERT_BORDER     4
95 #define DIVIDER_WIDTH  10
96 #define HOT_DIVIDER_WIDTH 2
97 #define MAX_HEADER_TEXT_LEN 260
98 #define HDN_UNICODE_OFFSET 20
99 #define HDN_FIRST_UNICODE (HDN_FIRST-HDN_UNICODE_OFFSET)
100
101 #define HDI_SUPPORTED_FIELDS (HDI_WIDTH|HDI_TEXT|HDI_FORMAT|HDI_LPARAM|HDI_BITMAP|HDI_IMAGE|HDI_ORDER)
102 #define HDI_UNSUPPORTED_FIELDS (HDI_FILTER)
103 #define HDI_UNKNOWN_FIELDS (~(HDI_SUPPORTED_FIELDS|HDI_UNSUPPORTED_FIELDS|HDI_DI_SETITEM))
104 #define HDI_COMCTL32_4_0_FIELDS (HDI_WIDTH|HDI_TEXT|HDI_FORMAT|HDI_LPARAM|HDI_BITMAP)
105
106 #define HEADER_GetInfoPtr(hwnd) ((HEADER_INFO *)GetWindowLongPtrW(hwnd,0))
107
108 static BOOL HEADER_PrepareCallbackItems(HWND hwnd, INT iItem, INT reqMask);
109 static void HEADER_FreeCallbackItems(HEADER_ITEM *lpItem);
110 static LRESULT HEADER_SendNotify(HWND hwnd, UINT code, NMHDR *hdr);
111 static LRESULT HEADER_SendCtrlCustomDraw(HWND hwnd, DWORD dwDrawStage, HDC hdc, const RECT *rect);
112
113 static const WCHAR themeClass[] = {'H','e','a','d','e','r',0};
114
115 static void HEADER_StoreHDItemInHeader(HEADER_ITEM *lpItem, UINT mask, const HDITEMW *phdi, BOOL fUnicode)
116 {
117     if (mask & HDI_UNSUPPORTED_FIELDS)
118         FIXME("unsupported header fields %x\n", (mask & HDI_UNSUPPORTED_FIELDS));
119     
120     if (mask & HDI_BITMAP)
121         lpItem->hbm = phdi->hbm;
122
123     if (mask & HDI_FORMAT)
124         lpItem->fmt = phdi->fmt;
125
126     if (mask & HDI_LPARAM)
127         lpItem->lParam = phdi->lParam;
128
129     if (mask & HDI_WIDTH)
130         lpItem->cxy = phdi->cxy;
131
132     if (mask & HDI_IMAGE) 
133     {
134         lpItem->iImage = phdi->iImage;
135         if (phdi->iImage == I_IMAGECALLBACK)
136             lpItem->callbackMask |= HDI_IMAGE;
137         else
138             lpItem->callbackMask &= ~HDI_IMAGE;
139     }
140
141     if (mask & HDI_TEXT)
142     {
143         Free(lpItem->pszText);
144         lpItem->pszText = NULL;
145
146         if (phdi->pszText != LPSTR_TEXTCALLBACKW) /* covers != TEXTCALLBACKA too */
147         {
148             static const WCHAR emptyString[] = {0};
149
150             LPCWSTR pszText = (phdi->pszText != NULL ? phdi->pszText : emptyString);
151             if (fUnicode)
152                 Str_SetPtrW(&lpItem->pszText, pszText);
153             else
154                 Str_SetPtrAtoW(&lpItem->pszText, (LPCSTR)pszText);
155             lpItem->callbackMask &= ~HDI_TEXT;
156         }
157         else
158         {
159             lpItem->pszText = NULL;
160             lpItem->callbackMask |= HDI_TEXT;
161         }  
162     }
163 }
164
165 static inline LRESULT
166 HEADER_IndexToOrder (HWND hwnd, INT iItem)
167 {
168     HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
169     HEADER_ITEM *lpItem = &infoPtr->items[iItem];
170     return lpItem->iOrder;
171 }
172
173
174 static INT
175 HEADER_OrderToIndex(HWND hwnd, WPARAM wParam)
176 {
177     HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
178     INT iorder = (INT)wParam;
179
180     if ((iorder <0) || iorder >= infoPtr->uNumItem)
181       return iorder;
182     return infoPtr->order[iorder];
183 }
184
185 static void
186 HEADER_ChangeItemOrder(const HEADER_INFO *infoPtr, INT iItem, INT iNewOrder)
187 {
188     HEADER_ITEM *lpItem = &infoPtr->items[iItem];
189     INT i, nMin, nMax;
190
191     TRACE("%d: %d->%d\n", iItem, lpItem->iOrder, iNewOrder);
192     if (lpItem->iOrder < iNewOrder)
193     {
194         memmove(&infoPtr->order[lpItem->iOrder],
195                &infoPtr->order[lpItem->iOrder + 1],
196                (iNewOrder - lpItem->iOrder) * sizeof(INT));
197     }
198     if (iNewOrder < lpItem->iOrder)
199     {
200         memmove(&infoPtr->order[iNewOrder + 1],
201                 &infoPtr->order[iNewOrder],
202                 (lpItem->iOrder - iNewOrder) * sizeof(INT));
203     }
204     infoPtr->order[iNewOrder] = iItem;
205     nMin = min(lpItem->iOrder, iNewOrder);
206     nMax = max(lpItem->iOrder, iNewOrder);
207     for (i = nMin; i <= nMax; i++)
208         infoPtr->items[infoPtr->order[i]].iOrder = i;
209 }
210
211 /* Note: if iItem is the last item then this function returns infoPtr->uNumItem */
212 static INT
213 HEADER_NextItem(HWND hwnd, INT iItem)
214 {
215     return HEADER_OrderToIndex(hwnd, HEADER_IndexToOrder(hwnd, iItem)+1);
216 }
217
218 static INT
219 HEADER_PrevItem(HWND hwnd, INT iItem)
220 {
221     return HEADER_OrderToIndex(hwnd, HEADER_IndexToOrder(hwnd, iItem)-1);
222 }
223
224 static void
225 HEADER_SetItemBounds (HWND hwnd)
226 {
227     HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
228     HEADER_ITEM *phdi;
229     RECT rect;
230     unsigned int i;
231     int x;
232
233     infoPtr->bRectsValid = TRUE;
234
235     if (infoPtr->uNumItem == 0)
236         return;
237
238     GetClientRect (hwnd, &rect);
239
240     x = rect.left;
241     for (i = 0; i < infoPtr->uNumItem; i++) {
242         phdi = &infoPtr->items[HEADER_OrderToIndex(hwnd,i)];
243         phdi->rect.top = rect.top;
244         phdi->rect.bottom = rect.bottom;
245         phdi->rect.left = x;
246         phdi->rect.right = phdi->rect.left + ((phdi->cxy>0)?phdi->cxy:0);
247         x = phdi->rect.right;
248     }
249 }
250
251 static LRESULT
252 HEADER_Size (HWND hwnd)
253 {
254     HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
255
256     infoPtr->bRectsValid = FALSE;
257
258     return 0;
259 }
260
261 static void HEADER_GetHotDividerRect(HWND hwnd, const HEADER_INFO *infoPtr, RECT *r)
262 {
263     INT iDivider = infoPtr->iHotDivider;
264     if (infoPtr->uNumItem > 0)
265     {
266         HEADER_ITEM *lpItem;
267         
268         if (iDivider < infoPtr->uNumItem)
269         {
270             lpItem = &infoPtr->items[iDivider];
271             r->left  = lpItem->rect.left - HOT_DIVIDER_WIDTH/2;
272             r->right = lpItem->rect.left + HOT_DIVIDER_WIDTH/2;
273         }
274         else
275         {
276             lpItem = &infoPtr->items[HEADER_OrderToIndex(hwnd, infoPtr->uNumItem-1)];
277             r->left  = lpItem->rect.right - HOT_DIVIDER_WIDTH/2;
278             r->right = lpItem->rect.right + HOT_DIVIDER_WIDTH/2;
279         }
280         r->top    = lpItem->rect.top;
281         r->bottom = lpItem->rect.bottom;
282     }
283     else
284     {
285         RECT clientRect;
286         GetClientRect(hwnd, &clientRect);
287         *r = clientRect;
288         r->right = r->left + HOT_DIVIDER_WIDTH/2;
289     }
290 }
291
292
293 static INT
294 HEADER_DrawItem (HWND hwnd, HDC hdc, INT iItem, BOOL bHotTrack, LRESULT lCDFlags)
295 {
296     HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
297     HEADER_ITEM *phdi = &infoPtr->items[iItem];
298     RECT r;
299     INT  oldBkMode;
300     HTHEME theme = GetWindowTheme (hwnd);
301     NMCUSTOMDRAW nmcd;
302
303     TRACE("DrawItem(iItem %d bHotTrack %d unicode flag %d)\n", iItem, bHotTrack, (infoPtr->nNotifyFormat == NFR_UNICODE));
304
305     r = phdi->rect;
306     if (r.right - r.left == 0)
307         return phdi->rect.right;
308
309     /* Set the colors before sending NM_CUSTOMDRAW so that it can change them */
310     SetTextColor(hdc, (bHotTrack && !theme) ? COLOR_HIGHLIGHT : COLOR_BTNTEXT);
311     SetBkColor(hdc, GetSysColor(COLOR_3DFACE));
312
313     if (lCDFlags & CDRF_NOTIFYITEMDRAW && !(phdi->fmt & HDF_OWNERDRAW))
314     {
315         LRESULT lCDItemFlags;
316
317         nmcd.dwDrawStage  = CDDS_PREPAINT | CDDS_ITEM;
318         nmcd.hdc          = hdc;
319         nmcd.dwItemSpec   = iItem;
320         nmcd.rc           = r;
321         nmcd.uItemState   = phdi->bDown ? CDIS_SELECTED : 0;
322         nmcd.lItemlParam  = phdi->lParam;
323
324         lCDItemFlags = HEADER_SendNotify(hwnd, NM_CUSTOMDRAW, (NMHDR *)&nmcd);
325         if (lCDItemFlags & CDRF_SKIPDEFAULT)
326             return phdi->rect.right;
327     }
328
329     if (theme != NULL) {
330         int state = (phdi->bDown) ? HIS_PRESSED :
331             (bHotTrack ? HIS_HOT : HIS_NORMAL);
332         DrawThemeBackground (theme, hdc, HP_HEADERITEM, state,
333             &r, NULL);
334         GetThemeBackgroundContentRect (theme, hdc, HP_HEADERITEM, state,
335             &r, &r);
336     }
337     else {
338         HBRUSH hbr;
339     
340         if (GetWindowLongW (hwnd, GWL_STYLE) & HDS_BUTTONS) {
341             if (phdi->bDown) {
342                 DrawEdge (hdc, &r, BDR_RAISEDOUTER,
343                             BF_RECT | BF_FLAT | BF_MIDDLE | BF_ADJUST);
344             }
345             else
346                 DrawEdge (hdc, &r, EDGE_RAISED,
347                             BF_RECT | BF_SOFT | BF_MIDDLE | BF_ADJUST);
348         }
349         else
350             DrawEdge (hdc, &r, EDGE_ETCHED, BF_BOTTOM | BF_RIGHT | BF_ADJUST);
351
352         hbr = CreateSolidBrush(GetBkColor(hdc));
353         FillRect(hdc, &r, hbr);
354         DeleteObject(hbr);
355     }
356     if (phdi->bDown) {
357         r.left += 2;
358         r.top  += 2;
359     }
360
361     if (phdi->fmt & HDF_OWNERDRAW) {
362         DRAWITEMSTRUCT dis;
363
364         dis.CtlType    = ODT_HEADER;
365         dis.CtlID      = GetWindowLongPtrW (hwnd, GWLP_ID);
366         dis.itemID     = iItem;
367         dis.itemAction = ODA_DRAWENTIRE;
368         dis.itemState  = phdi->bDown ? ODS_SELECTED : 0;
369         dis.hwndItem   = hwnd;
370         dis.hDC        = hdc;
371         dis.rcItem     = phdi->rect;
372         dis.itemData   = phdi->lParam;
373         oldBkMode = SetBkMode(hdc, TRANSPARENT);
374         SendMessageW (infoPtr->hwndNotify, WM_DRAWITEM,
375                         (WPARAM)dis.CtlID, (LPARAM)&dis);
376         if (oldBkMode != TRANSPARENT)
377             SetBkMode(hdc, oldBkMode);
378     }
379     else {
380         UINT rw, rh, /* width and height of r */
381              *x = NULL, *w = NULL; /* x and width of the pic (bmp or img) which is part of cnt */
382           /* cnt,txt,img,bmp */
383         UINT cx, tx, ix, bx,
384              cw, tw, iw, bw;
385         BITMAP bmp;
386
387         HEADER_PrepareCallbackItems(hwnd, iItem, HDI_TEXT|HDI_IMAGE);
388         cw = tw = iw = bw = 0;
389         rw = r.right - r.left;
390         rh = r.bottom - r.top;
391
392         if (phdi->fmt & HDF_STRING) {
393             RECT textRect;
394
395             SetRectEmpty(&textRect);
396             DrawTextW (hdc, phdi->pszText, -1,
397                        &textRect, DT_LEFT|DT_VCENTER|DT_SINGLELINE|DT_CALCRECT);
398             cw = textRect.right - textRect.left + 2 * infoPtr->iMargin;
399         }
400
401         if ((phdi->fmt & HDF_IMAGE) && (infoPtr->himl)) {
402             iw = infoPtr->himl->cx + 2 * infoPtr->iMargin;
403             x = &ix;
404             w = &iw;
405         }
406
407         if ((phdi->fmt & HDF_BITMAP) && (phdi->hbm)) {
408             GetObjectW (phdi->hbm, sizeof(BITMAP), &bmp);
409             bw = bmp.bmWidth + 2 * infoPtr->iMargin;
410             if (!iw) {
411                 x = &bx;
412                 w = &bw;
413             }
414         }
415
416         if (bw || iw)
417             cw += *w; 
418
419         /* align cx using the unclipped cw */
420         if ((phdi->fmt & HDF_JUSTIFYMASK) == HDF_LEFT)
421             cx = r.left;
422         else if ((phdi->fmt & HDF_JUSTIFYMASK) == HDF_CENTER)
423             cx = r.left + rw / 2 - cw / 2;
424         else /* HDF_RIGHT */
425             cx = r.right - cw;
426         
427         /* clip cx & cw */
428         if (cx < r.left)
429             cx = r.left;
430         if (cx + cw > r.right)
431             cw = r.right - cx;
432         
433         tx = cx + infoPtr->iMargin;
434         /* since cw might have changed we have to recalculate tw */
435         tw = cw - infoPtr->iMargin * 2;
436                         
437         if (iw || bw) {
438             tw -= *w;
439             if (phdi->fmt & HDF_BITMAP_ON_RIGHT) {
440                 /* put pic behind text */
441                 *x = cx + tw + infoPtr->iMargin * 3;
442             } else {
443                 *x = cx + infoPtr->iMargin;
444                 /* move text behind pic */
445                 tx += *w;
446             }
447         }
448
449         if (iw && bw) {
450             /* since we're done with the layout we can
451                now calculate the position of bmp which
452                has no influence on alignment and layout
453                because of img */
454             if ((phdi->fmt & HDF_JUSTIFYMASK) == HDF_RIGHT)
455                 bx = cx - bw + infoPtr->iMargin;
456             else
457                 bx = cx + cw + infoPtr->iMargin;
458         }
459
460         if (iw || bw) {
461             HDC hClipDC = GetDC(hwnd);
462             HRGN hClipRgn = CreateRectRgn(r.left, r.top, r.right, r.bottom);
463             SelectClipRgn(hClipDC, hClipRgn);
464             
465             if (bw) {
466                 HDC hdcBitmap = CreateCompatibleDC (hClipDC);
467                 SelectObject (hdcBitmap, phdi->hbm);
468                 BitBlt (hClipDC, bx, r.top + ((INT)rh - bmp.bmHeight) / 2, 
469                         bmp.bmWidth, bmp.bmHeight, hdcBitmap, 0, 0, SRCCOPY);
470                 DeleteDC (hdcBitmap);
471             }
472
473             if (iw) {
474                 ImageList_DrawEx (infoPtr->himl, phdi->iImage, hClipDC, 
475                                   ix, r.top + ((INT)rh - infoPtr->himl->cy) / 2,
476                                   infoPtr->himl->cx, infoPtr->himl->cy, CLR_DEFAULT, CLR_DEFAULT, 0);
477             }
478
479             DeleteObject(hClipRgn);
480             ReleaseDC(hwnd, hClipDC);
481         }
482         
483         if (((phdi->fmt & HDF_STRING)
484                 || (!(phdi->fmt & (HDF_OWNERDRAW|HDF_STRING|HDF_BITMAP|
485                                    HDF_BITMAP_ON_RIGHT|HDF_IMAGE)))) /* no explicit format specified? */
486             && (phdi->pszText)) {
487             oldBkMode = SetBkMode(hdc, TRANSPARENT);
488             r.left  = tx;
489             r.right = tx + tw;
490             DrawTextW (hdc, phdi->pszText, -1,
491                        &r, DT_LEFT|DT_END_ELLIPSIS|DT_VCENTER|DT_SINGLELINE);
492             if (oldBkMode != TRANSPARENT)
493                 SetBkMode(hdc, oldBkMode);
494         }
495         HEADER_FreeCallbackItems(phdi);
496     }/*Ownerdrawn*/
497
498     return phdi->rect.right;
499 }
500
501 static void
502 HEADER_DrawHotDivider(HWND hwnd, HDC hdc)
503 {
504     HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
505     HBRUSH brush;
506     RECT r;
507     
508     HEADER_GetHotDividerRect(hwnd, infoPtr, &r);
509     brush = CreateSolidBrush(GetSysColor(COLOR_HIGHLIGHT));
510     FillRect(hdc, &r, brush);
511     DeleteObject(brush);
512 }
513
514 static void
515 HEADER_Refresh (HWND hwnd, HDC hdc)
516 {
517     HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
518     HFONT hFont, hOldFont;
519     RECT rect, rcRest;
520     HBRUSH hbrBk;
521     UINT i;
522     INT x;
523     LRESULT lCDFlags;
524     HTHEME theme = GetWindowTheme (hwnd);
525
526     if (!infoPtr->bRectsValid)
527         HEADER_SetItemBounds(hwnd);
528
529     /* get rect for the bar, adjusted for the border */
530     GetClientRect (hwnd, &rect);
531     lCDFlags = HEADER_SendCtrlCustomDraw(hwnd, CDDS_PREPAINT, hdc, &rect);
532     
533     if (infoPtr->bDragging)
534         ImageList_DragShowNolock(FALSE);
535
536     hFont = infoPtr->hFont ? infoPtr->hFont : GetStockObject (SYSTEM_FONT);
537     hOldFont = SelectObject (hdc, hFont);
538
539     /* draw Background */
540     if (infoPtr->uNumItem == 0 && theme == NULL) {
541         hbrBk = GetSysColorBrush(COLOR_3DFACE);
542         FillRect(hdc, &rect, hbrBk);
543     }
544
545     x = rect.left;
546     for (i = 0; x <= rect.right && i < infoPtr->uNumItem; i++) {
547         int idx = HEADER_OrderToIndex(hwnd,i);
548         if (RectVisible(hdc, &infoPtr->items[idx].rect))
549             HEADER_DrawItem(hwnd, hdc, idx, infoPtr->iHotItem == idx, lCDFlags);
550         x = infoPtr->items[idx].rect.right;
551     }
552
553     rcRest = rect;
554     rcRest.left = x;
555     if ((x <= rect.right) && RectVisible(hdc, &rcRest) && (infoPtr->uNumItem > 0)) {
556         if (theme != NULL) {
557             DrawThemeBackground(theme, hdc, HP_HEADERITEM, HIS_NORMAL, &rcRest, NULL);
558         }
559         else {
560             if (GetWindowLongW (hwnd, GWL_STYLE) & HDS_BUTTONS)
561                 DrawEdge (hdc, &rcRest, EDGE_RAISED, BF_TOP|BF_LEFT|BF_BOTTOM|BF_SOFT|BF_MIDDLE);
562             else
563                 DrawEdge (hdc, &rcRest, EDGE_ETCHED, BF_BOTTOM|BF_MIDDLE);
564         }
565     }
566     
567     if (infoPtr->iHotDivider != -1)
568         HEADER_DrawHotDivider(hwnd, hdc);
569
570     if (infoPtr->bDragging)
571         ImageList_DragShowNolock(TRUE);
572     SelectObject (hdc, hOldFont);
573     
574     if (lCDFlags & CDRF_NOTIFYPOSTPAINT)
575         HEADER_SendCtrlCustomDraw(hwnd, CDDS_POSTPAINT, hdc, &rect);
576 }
577
578
579 static void
580 HEADER_RefreshItem (HWND hwnd, INT iItem)
581 {
582     HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
583
584     if (!infoPtr->bRectsValid)
585         HEADER_SetItemBounds(hwnd);
586
587     InvalidateRect(hwnd, &infoPtr->items[iItem].rect, FALSE);
588 }
589
590
591 static void
592 HEADER_InternalHitTest (HWND hwnd, const POINT *lpPt, UINT *pFlags, INT *pItem)
593 {
594     HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
595     RECT rect, rcTest;
596     UINT iCount;
597     INT width;
598     BOOL bNoWidth;
599
600     GetClientRect (hwnd, &rect);
601
602     *pFlags = 0;
603     bNoWidth = FALSE;
604     if (PtInRect (&rect, *lpPt))
605     {
606         if (infoPtr->uNumItem == 0) {
607             *pFlags |= HHT_NOWHERE;
608             *pItem = 1;
609             TRACE("NOWHERE\n");
610             return;
611         }
612         else {
613             /* somewhere inside */
614             for (iCount = 0; iCount < infoPtr->uNumItem; iCount++) {
615                 rect = infoPtr->items[iCount].rect;
616                 width = rect.right - rect.left;
617                 if (width == 0) {
618                     bNoWidth = TRUE;
619                     continue;
620                 }
621                 if (PtInRect (&rect, *lpPt)) {
622                     if (width <= 2 * DIVIDER_WIDTH) {
623                         *pFlags |= HHT_ONHEADER;
624                         *pItem = iCount;
625                         TRACE("ON HEADER %d\n", iCount);
626                         return;
627                     }
628                     if (HEADER_IndexToOrder(hwnd, iCount) > 0) {
629                         rcTest = rect;
630                         rcTest.right = rcTest.left + DIVIDER_WIDTH;
631                         if (PtInRect (&rcTest, *lpPt)) {
632                             if (bNoWidth) {
633                                 *pFlags |= HHT_ONDIVOPEN;
634                                 *pItem = HEADER_PrevItem(hwnd, iCount);
635                                 TRACE("ON DIVOPEN %d\n", *pItem);
636                                 return;
637                             }
638                             else {
639                                 *pFlags |= HHT_ONDIVIDER;
640                                 *pItem = HEADER_PrevItem(hwnd, iCount);
641                                 TRACE("ON DIVIDER %d\n", *pItem);
642                                 return;
643                             }
644                         }
645                     }
646                     rcTest = rect;
647                     rcTest.left = rcTest.right - DIVIDER_WIDTH;
648                     if (PtInRect (&rcTest, *lpPt)) {
649                         *pFlags |= HHT_ONDIVIDER;
650                         *pItem = iCount;
651                         TRACE("ON DIVIDER %d\n", *pItem);
652                         return;
653                     }
654
655                     *pFlags |= HHT_ONHEADER;
656                     *pItem = iCount;
657                     TRACE("ON HEADER %d\n", iCount);
658                     return;
659                 }
660             }
661
662             /* check for last divider part (on nowhere) */
663             rect = infoPtr->items[infoPtr->uNumItem-1].rect;
664             rect.left = rect.right;
665             rect.right += DIVIDER_WIDTH;
666             if (PtInRect (&rect, *lpPt)) {
667                 if (bNoWidth) {
668                     *pFlags |= HHT_ONDIVOPEN;
669                     *pItem = infoPtr->uNumItem - 1;
670                     TRACE("ON DIVOPEN %d\n", *pItem);
671                     return;
672                 }
673                 else {
674                     *pFlags |= HHT_ONDIVIDER;
675                     *pItem = infoPtr->uNumItem-1;
676                     TRACE("ON DIVIDER %d\n", *pItem);
677                     return;
678                 }
679             }
680
681             *pFlags |= HHT_NOWHERE;
682             *pItem = 1;
683             TRACE("NOWHERE\n");
684             return;
685         }
686     }
687     else {
688         if (lpPt->x < rect.left) {
689            TRACE("TO LEFT\n");
690            *pFlags |= HHT_TOLEFT;
691         }
692         else if (lpPt->x > rect.right) {
693             TRACE("TO RIGHT\n");
694             *pFlags |= HHT_TORIGHT;
695         }
696
697         if (lpPt->y < rect.top) {
698             TRACE("ABOVE\n");
699             *pFlags |= HHT_ABOVE;
700         }
701         else if (lpPt->y > rect.bottom) {
702             TRACE("BELOW\n");
703             *pFlags |= HHT_BELOW;
704         }
705     }
706
707     *pItem = 1;
708     TRACE("flags=0x%X\n", *pFlags);
709     return;
710 }
711
712
713 static void
714 HEADER_DrawTrackLine (HWND hwnd, HDC hdc, INT x)
715 {
716     RECT rect;
717     HPEN hOldPen;
718     INT  oldRop;
719
720     GetClientRect (hwnd, &rect);
721
722     hOldPen = SelectObject (hdc, GetStockObject (BLACK_PEN));
723     oldRop = SetROP2 (hdc, R2_XORPEN);
724     MoveToEx (hdc, x, rect.top, NULL);
725     LineTo (hdc, x, rect.bottom);
726     SetROP2 (hdc, oldRop);
727     SelectObject (hdc, hOldPen);
728 }
729
730 /***
731  * DESCRIPTION:
732  * Convert a HDITEM into the correct format (ANSI/Unicode) to send it in a notify
733  *
734  * PARAMETER(S):
735  * [I] infoPtr : the header that wants to send the notify
736  * [O] dest : The buffer to store the HDITEM for notify. It may be set to a HDITEMA of HDITEMW
737  * [I] src  : The source HDITEM. It may be a HDITEMA or HDITEMW
738  * [I] fSourceUnicode : is src a HDITEMW or HDITEMA
739  * [O] ppvScratch : a pointer to a scratch buffer that needs to be freed after
740  *                  the HDITEM is no longer in use or NULL if none was needed
741  * 
742  * NOTE: We depend on HDITEMA and HDITEMW having the same structure
743  */
744 static void HEADER_CopyHDItemForNotify(const HEADER_INFO *infoPtr, HDITEMW *dest,
745     const HDITEMW *src, BOOL fSourceUnicode, LPVOID *ppvScratch)
746 {
747     *ppvScratch = NULL;
748     *dest = *src;
749     
750     if (src->mask & HDI_TEXT && src->pszText != LPSTR_TEXTCALLBACKW) /* covers TEXTCALLBACKA as well */
751     {
752         if (fSourceUnicode && infoPtr->nNotifyFormat != NFR_UNICODE)
753         {
754             dest->pszText = NULL;
755             Str_SetPtrWtoA((LPSTR *)&dest->pszText, src->pszText);
756             *ppvScratch = dest->pszText;
757         }
758         
759         if (!fSourceUnicode && infoPtr->nNotifyFormat == NFR_UNICODE)
760         {
761             dest->pszText = NULL;
762             Str_SetPtrAtoW(&dest->pszText, (LPSTR)src->pszText);
763             *ppvScratch = dest->pszText;
764         }
765     }
766 }
767
768 static UINT HEADER_NotifyCodeWtoA(UINT code)
769 {
770     /* we use the fact that all the unicode messages are in HDN_FIRST_UNICODE..HDN_LAST*/
771     if (code >= HDN_LAST && code <= HDN_FIRST_UNICODE)
772         return code + HDN_UNICODE_OFFSET;
773     else
774         return code;
775 }
776
777 static LRESULT
778 HEADER_SendNotify(HWND hwnd, UINT code, NMHDR *nmhdr)
779 {
780     HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
781
782     nmhdr->hwndFrom = hwnd;
783     nmhdr->idFrom   = GetWindowLongPtrW (hwnd, GWLP_ID);
784     nmhdr->code     = code;
785
786     return SendMessageW(infoPtr->hwndNotify, WM_NOTIFY,
787                                    nmhdr->idFrom, (LPARAM)nmhdr);
788 }
789
790 static BOOL
791 HEADER_SendSimpleNotify (HWND hwnd, UINT code)
792 {
793     NMHDR nmhdr;
794     return (BOOL)HEADER_SendNotify(hwnd, code, &nmhdr);
795 }
796
797 static LRESULT
798 HEADER_SendCtrlCustomDraw(HWND hwnd, DWORD dwDrawStage, HDC hdc, const RECT *rect)
799 {
800     NMCUSTOMDRAW nm;
801     nm.dwDrawStage = dwDrawStage;
802     nm.hdc = hdc;
803     nm.rc = *rect;
804     nm.dwItemSpec = 0;
805     nm.uItemState = 0;
806     nm.lItemlParam = 0;
807
808     return HEADER_SendNotify(hwnd, NM_CUSTOMDRAW, (NMHDR *)&nm);
809 }
810
811 static BOOL
812 HEADER_SendNotifyWithHDItemT(HWND hwnd, UINT code, INT iItem, HDITEMW *lpItem)
813 {
814     HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
815     NMHEADERW nmhdr;
816     
817     if (infoPtr->nNotifyFormat != NFR_UNICODE)
818         code = HEADER_NotifyCodeWtoA(code);
819     nmhdr.iItem = iItem;
820     nmhdr.iButton = 0;
821     nmhdr.pitem = lpItem;
822
823     return (BOOL)HEADER_SendNotify(hwnd, code, (NMHDR *)&nmhdr);
824 }
825
826 static BOOL
827 HEADER_SendNotifyWithIntFieldT(HWND hwnd, UINT code, INT iItem, INT mask, INT iValue)
828 {
829     HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
830     HDITEMW nmitem;
831
832     /* copying only the iValue should be ok but to make the code more robust we copy everything */
833     nmitem.cxy = infoPtr->items[iItem].cxy;
834     nmitem.hbm = infoPtr->items[iItem].hbm;
835     nmitem.pszText = NULL;
836     nmitem.cchTextMax = 0;
837     nmitem.fmt = infoPtr->items[iItem].fmt;
838     nmitem.lParam = infoPtr->items[iItem].lParam;
839     nmitem.iOrder = infoPtr->items[iItem].iOrder;
840     nmitem.iImage = infoPtr->items[iItem].iImage;
841
842     nmitem.mask = mask;
843     switch (mask)
844     {
845         case HDI_WIDTH:
846             nmitem.cxy = iValue;
847             break;
848         case HDI_ORDER:
849             nmitem.iOrder = iValue;
850             break;
851         default:
852             ERR("invalid mask value 0x%x\n", iValue);
853     }
854
855     return HEADER_SendNotifyWithHDItemT(hwnd, code, iItem, &nmitem);
856 }
857
858 /**
859  * Prepare callback items
860  *   depends on NMHDDISPINFOW having same structure as NMHDDISPINFOA 
861  *   (so we handle the two cases only doing a specific cast for pszText).
862  * Checks if any of the required field are callback. If there are sends a 
863  * NMHDISPINFO notify to retrieve these items. The items are stored in the
864  * HEADER_ITEM pszText and iImage fields. They should be freed with
865  * HEADER_FreeCallbackItems.
866  *
867  * @param hwnd : hwnd header container handler
868  * @param iItem : the header item id
869  * @param reqMask : required fields. If any of them is callback this function will fetch it
870  *
871  * @return TRUE on success, else FALSE
872  */
873 static BOOL
874 HEADER_PrepareCallbackItems(HWND hwnd, INT iItem, INT reqMask)
875 {
876     HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
877     HEADER_ITEM *lpItem = &infoPtr->items[iItem];
878     DWORD mask = reqMask & lpItem->callbackMask;
879     NMHDDISPINFOW dispInfo;
880     void *pvBuffer = NULL;
881
882     if (mask == 0)
883         return TRUE;
884     if (mask&HDI_TEXT && lpItem->pszText != NULL)
885     {
886         ERR("(): function called without a call to FreeCallbackItems\n");
887         Free(lpItem->pszText);
888         lpItem->pszText = NULL;
889     }
890     
891     memset(&dispInfo, 0, sizeof(NMHDDISPINFOW));
892     dispInfo.hdr.hwndFrom = hwnd;
893     dispInfo.hdr.idFrom   = GetWindowLongPtrW (hwnd, GWLP_ID);
894     if (infoPtr->nNotifyFormat == NFR_UNICODE)
895     {
896         dispInfo.hdr.code = HDN_GETDISPINFOW;
897         if (mask & HDI_TEXT)
898             pvBuffer = Alloc(MAX_HEADER_TEXT_LEN * sizeof(WCHAR));
899     }
900     else
901     {
902         dispInfo.hdr.code = HDN_GETDISPINFOA;
903         if (mask & HDI_TEXT)
904             pvBuffer = Alloc(MAX_HEADER_TEXT_LEN * sizeof(CHAR));
905     }
906     dispInfo.pszText      = pvBuffer;
907     dispInfo.cchTextMax   = (pvBuffer!=NULL?MAX_HEADER_TEXT_LEN:0);
908     dispInfo.iItem        = iItem;
909     dispInfo.mask         = mask;
910     dispInfo.lParam       = lpItem->lParam;
911     
912     TRACE("Sending HDN_GETDISPINFO%c\n", infoPtr->nNotifyFormat == NFR_UNICODE?'W':'A');
913     SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, dispInfo.hdr.idFrom, (LPARAM)&dispInfo);
914
915     TRACE("SendMessage returns(mask:0x%x,str:%s,lParam:%p)\n", 
916           dispInfo.mask,
917           (infoPtr->nNotifyFormat == NFR_UNICODE ? debugstr_w(dispInfo.pszText) : (LPSTR) dispInfo.pszText),
918           (void*) dispInfo.lParam);
919           
920     if (mask & HDI_IMAGE)
921         lpItem->iImage = dispInfo.iImage;
922     if (mask & HDI_TEXT)
923     {
924         if (infoPtr->nNotifyFormat == NFR_UNICODE)
925         {
926             lpItem->pszText = pvBuffer;
927
928             /* the user might have used his own buffer */
929             if (dispInfo.pszText != lpItem->pszText)
930                 Str_GetPtrW(dispInfo.pszText, lpItem->pszText, MAX_HEADER_TEXT_LEN);
931         }
932         else
933         {
934             Str_SetPtrAtoW(&lpItem->pszText, (LPSTR)dispInfo.pszText);
935             Free(pvBuffer);
936         }
937     }
938         
939     if (dispInfo.mask & HDI_DI_SETITEM) 
940     {
941         /* make the items permanent */
942         lpItem->callbackMask &= ~dispInfo.mask;
943     }
944     
945     return TRUE;
946 }
947
948 /***
949  * DESCRIPTION:
950  * Free the items that might be allocated with HEADER_PrepareCallbackItems
951  *
952  * PARAMETER(S):
953  * [I] lpItem : the item to free the data
954  *
955  */
956 static void
957 HEADER_FreeCallbackItems(HEADER_ITEM *lpItem)
958 {
959     if (lpItem->callbackMask&HDI_TEXT)
960     {
961         Free(lpItem->pszText);
962         lpItem->pszText = NULL;
963     }
964
965     if (lpItem->callbackMask&HDI_IMAGE)
966         lpItem->iImage = I_IMAGECALLBACK;
967 }
968
969 static LRESULT
970 HEADER_CreateDragImage (HWND hwnd, WPARAM wParam)
971 {
972     HEADER_INFO *infoPtr = HEADER_GetInfoPtr(hwnd);
973     HEADER_ITEM *lpItem;
974     HIMAGELIST himl;
975     HBITMAP hMemory, hOldBitmap;
976     LRESULT lCDFlags;
977     RECT rc;
978     HDC hMemoryDC;
979     HDC hDeviceDC;
980     int height, width;
981     HFONT hFont;
982     
983     if (wParam >= infoPtr->uNumItem)
984         return FALSE;
985
986     if (!infoPtr->bRectsValid)
987         HEADER_SetItemBounds(hwnd);
988
989     lpItem = &infoPtr->items[wParam];
990     width = lpItem->rect.right - lpItem->rect.left;
991     height = lpItem->rect.bottom - lpItem->rect.top;
992     
993     hDeviceDC = GetDC(NULL);
994     hMemoryDC = CreateCompatibleDC(hDeviceDC);
995     hMemory = CreateCompatibleBitmap(hDeviceDC, width, height);
996     ReleaseDC(NULL, hDeviceDC);
997     hOldBitmap = SelectObject(hMemoryDC, hMemory);
998     SetViewportOrgEx(hMemoryDC, -lpItem->rect.left, -lpItem->rect.top, NULL);
999     hFont = infoPtr->hFont ? infoPtr->hFont : GetStockObject(SYSTEM_FONT);
1000     SelectObject(hMemoryDC, hFont);
1001
1002     GetClientRect(hwnd, &rc);
1003     lCDFlags = HEADER_SendCtrlCustomDraw(hwnd, CDDS_PREPAINT, hMemoryDC, &rc);
1004     HEADER_DrawItem(hwnd, hMemoryDC, wParam, FALSE, lCDFlags);
1005     if (lCDFlags & CDRF_NOTIFYPOSTPAINT)
1006         HEADER_SendCtrlCustomDraw(hwnd, CDDS_POSTPAINT, hMemoryDC, &rc);
1007     
1008     hMemory = SelectObject(hMemoryDC, hOldBitmap);
1009     DeleteDC(hMemoryDC);
1010     
1011     if (hMemory == NULL)    /* if anything failed */
1012         return FALSE;
1013     
1014     himl = ImageList_Create(width, height, ILC_COLORDDB, 1, 1);
1015     ImageList_Add(himl, hMemory, NULL);
1016     DeleteObject(hMemory);
1017     return (LRESULT)himl;
1018 }
1019
1020 static LRESULT
1021 HEADER_SetHotDivider(HWND hwnd, WPARAM wParam, LPARAM lParam)
1022 {
1023     HEADER_INFO *infoPtr = HEADER_GetInfoPtr(hwnd);
1024     INT iDivider;
1025     RECT r;
1026     
1027     if (wParam)
1028     {
1029         POINT pt;
1030         UINT flags;
1031         pt.x = (INT)(SHORT)LOWORD(lParam);
1032         pt.y = 0;
1033         HEADER_InternalHitTest (hwnd, &pt, &flags, &iDivider);
1034         
1035         if (flags & HHT_TOLEFT)
1036             iDivider = 0;
1037         else if (flags & HHT_NOWHERE || flags & HHT_TORIGHT)
1038             iDivider = infoPtr->uNumItem;
1039         else
1040         {
1041             HEADER_ITEM *lpItem = &infoPtr->items[iDivider];
1042             if (pt.x > (lpItem->rect.left+lpItem->rect.right)/2)
1043                 iDivider = HEADER_NextItem(hwnd, iDivider);
1044         }
1045     }
1046     else
1047         iDivider = (INT)lParam;
1048         
1049     /* Note; wParam==FALSE, lParam==-1 is valid and is used to clear the hot divider */
1050     if (iDivider<-1 || iDivider>(int)infoPtr->uNumItem)
1051         return iDivider;
1052
1053     if (iDivider != infoPtr->iHotDivider)
1054     {
1055         if (infoPtr->iHotDivider != -1)
1056         {
1057             HEADER_GetHotDividerRect(hwnd, infoPtr, &r);
1058             InvalidateRect(hwnd, &r, FALSE);
1059         }
1060         infoPtr->iHotDivider = iDivider;
1061         if (iDivider != -1)
1062         {
1063             HEADER_GetHotDividerRect(hwnd, infoPtr, &r);
1064             InvalidateRect(hwnd, &r, FALSE);
1065         }
1066     }
1067     return iDivider;
1068 }
1069
1070 static LRESULT
1071 HEADER_DeleteItem (HWND hwnd, WPARAM wParam)
1072 {
1073     HEADER_INFO *infoPtr = HEADER_GetInfoPtr(hwnd);
1074     INT iItem = (INT)wParam;
1075     INT iOrder;
1076     UINT i;
1077
1078     TRACE("[iItem=%d]\n", iItem);
1079
1080     if ((iItem < 0) || (iItem >= (INT)infoPtr->uNumItem))
1081         return FALSE;
1082
1083     for (i = 0; i < infoPtr->uNumItem; i++)
1084        TRACE("%d: order=%d, iOrder=%d, ->iOrder=%d\n", i, infoPtr->order[i], infoPtr->items[i].iOrder, infoPtr->items[infoPtr->order[i]].iOrder);
1085
1086     iOrder = infoPtr->items[iItem].iOrder;
1087     Free(infoPtr->items[iItem].pszText);
1088
1089     infoPtr->uNumItem--;
1090     memmove(&infoPtr->items[iItem], &infoPtr->items[iItem + 1],
1091             (infoPtr->uNumItem - iItem) * sizeof(HEADER_ITEM));
1092     memmove(&infoPtr->order[iOrder], &infoPtr->order[iOrder + 1],
1093             (infoPtr->uNumItem - iOrder) * sizeof(INT));
1094     infoPtr->items = ReAlloc(infoPtr->items, sizeof(HEADER_ITEM) * infoPtr->uNumItem);
1095     infoPtr->order = ReAlloc(infoPtr->order, sizeof(INT) * infoPtr->uNumItem);
1096         
1097     /* Correct the orders */
1098     for (i = 0; i < infoPtr->uNumItem; i++)
1099     {
1100         if (infoPtr->order[i] > iItem)
1101             infoPtr->order[i]--;
1102         if (i >= iOrder)
1103             infoPtr->items[infoPtr->order[i]].iOrder = i;
1104     }
1105     for (i = 0; i < infoPtr->uNumItem; i++)
1106        TRACE("%d: order=%d, iOrder=%d, ->iOrder=%d\n", i, infoPtr->order[i], infoPtr->items[i].iOrder, infoPtr->items[infoPtr->order[i]].iOrder);
1107
1108     HEADER_SetItemBounds (hwnd);
1109     InvalidateRect(hwnd, NULL, FALSE);
1110
1111     return TRUE;
1112 }
1113
1114
1115 static LRESULT
1116 HEADER_GetImageList (HWND hwnd)
1117 {
1118     HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
1119
1120     return (LRESULT)infoPtr->himl;
1121 }
1122
1123
1124 static LRESULT
1125 HEADER_GetItemT (HWND hwnd, INT nItem, LPHDITEMW phdi, BOOL bUnicode)
1126 {
1127     HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
1128     HEADER_ITEM *lpItem;
1129     UINT mask;
1130
1131     if (!phdi)
1132         return FALSE;
1133
1134     TRACE("[nItem=%d]\n", nItem);
1135
1136     mask = phdi->mask;
1137     if (mask == 0)
1138         return TRUE;
1139
1140     if ((nItem < 0) || (nItem >= (INT)infoPtr->uNumItem))
1141         return FALSE;
1142
1143     if (mask & HDI_UNKNOWN_FIELDS)
1144     {
1145         TRACE("mask %x contains unknown fields. Using only comctl32 4.0 fields\n", mask);
1146         mask &= HDI_COMCTL32_4_0_FIELDS;
1147     }
1148     
1149     lpItem = &infoPtr->items[nItem];
1150     HEADER_PrepareCallbackItems(hwnd, nItem, mask);
1151
1152     if (mask & HDI_BITMAP)
1153         phdi->hbm = lpItem->hbm;
1154
1155     if (mask & HDI_FORMAT)
1156         phdi->fmt = lpItem->fmt;
1157
1158     if (mask & HDI_WIDTH)
1159         phdi->cxy = lpItem->cxy;
1160
1161     if (mask & HDI_LPARAM)
1162         phdi->lParam = lpItem->lParam;
1163
1164     if (mask & HDI_IMAGE) 
1165         phdi->iImage = lpItem->iImage;
1166
1167     if (mask & HDI_ORDER)
1168         phdi->iOrder = lpItem->iOrder;
1169
1170     if (mask & HDI_TEXT)
1171     {
1172         if (bUnicode)
1173             Str_GetPtrW (lpItem->pszText, phdi->pszText, phdi->cchTextMax);
1174         else
1175             Str_GetPtrWtoA (lpItem->pszText, (LPSTR)phdi->pszText, phdi->cchTextMax);
1176     }
1177
1178     HEADER_FreeCallbackItems(lpItem);
1179     return TRUE;
1180 }
1181
1182
1183 static inline LRESULT
1184 HEADER_GetItemCount (HWND hwnd)
1185 {
1186     HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
1187     return infoPtr->uNumItem;
1188 }
1189
1190
1191 static LRESULT
1192 HEADER_GetItemRect (HWND hwnd, WPARAM wParam, LPARAM lParam)
1193 {
1194     HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
1195     INT iItem = (INT)wParam;
1196     LPRECT lpRect = (LPRECT)lParam;
1197
1198     if ((iItem < 0) || (iItem >= (INT)infoPtr->uNumItem))
1199         return FALSE;
1200
1201     lpRect->left   = infoPtr->items[iItem].rect.left;
1202     lpRect->right  = infoPtr->items[iItem].rect.right;
1203     lpRect->top    = infoPtr->items[iItem].rect.top;
1204     lpRect->bottom = infoPtr->items[iItem].rect.bottom;
1205
1206     return TRUE;
1207 }
1208
1209
1210 static LRESULT
1211 HEADER_GetOrderArray(HWND hwnd, WPARAM wParam, LPARAM lParam)
1212 {
1213     LPINT order = (LPINT) lParam;
1214     HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
1215
1216     if ((unsigned int)wParam <infoPtr->uNumItem)
1217       return FALSE;
1218
1219     memcpy(order, infoPtr->order, infoPtr->uNumItem * sizeof(INT));
1220     return TRUE;
1221 }
1222
1223 static LRESULT
1224 HEADER_SetOrderArray(HWND hwnd, WPARAM wParam, LPARAM lParam)
1225 {
1226     int i;
1227     LPINT order = (LPINT) lParam;
1228     HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
1229     HEADER_ITEM *lpItem;
1230
1231     if ((unsigned int)wParam <infoPtr->uNumItem)
1232       return FALSE;
1233     memcpy(infoPtr->order, order, infoPtr->uNumItem * sizeof(INT));
1234     for (i=0; i<(int)wParam; i++)
1235       {
1236         lpItem = &infoPtr->items[*order++];
1237         lpItem->iOrder=i;
1238       }
1239     infoPtr->bRectsValid=0;
1240     InvalidateRect(hwnd, NULL, FALSE);
1241     return TRUE;
1242 }
1243
1244 static inline LRESULT
1245 HEADER_GetUnicodeFormat (HWND hwnd)
1246 {
1247     HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
1248     return (infoPtr->nNotifyFormat == NFR_UNICODE);
1249 }
1250
1251
1252 static LRESULT
1253 HEADER_HitTest (HWND hwnd, LPARAM lParam)
1254 {
1255     LPHDHITTESTINFO phti = (LPHDHITTESTINFO)lParam;
1256
1257     HEADER_InternalHitTest (hwnd, &phti->pt, &phti->flags, &phti->iItem);
1258
1259     if (phti->flags == HHT_NOWHERE)
1260         return -1;
1261     else
1262         return phti->iItem;
1263 }
1264
1265
1266 static LRESULT
1267 HEADER_InsertItemT (HWND hwnd, INT nItem, const HDITEMW *phdi, BOOL bUnicode)
1268 {
1269     HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
1270     HEADER_ITEM *lpItem;
1271     INT       iOrder;
1272     UINT      i;
1273     UINT      copyMask;
1274
1275     if ((phdi == NULL) || (nItem < 0) || (phdi->mask == 0))
1276         return -1;
1277
1278     if (nItem > infoPtr->uNumItem)
1279         nItem = infoPtr->uNumItem;
1280
1281     iOrder = (phdi->mask & HDI_ORDER) ? phdi->iOrder : nItem;
1282     if (iOrder < 0)
1283         iOrder = 0;
1284     else if (infoPtr->uNumItem < iOrder)
1285         iOrder = infoPtr->uNumItem;
1286
1287     infoPtr->uNumItem++;
1288     infoPtr->items = ReAlloc(infoPtr->items, sizeof(HEADER_ITEM) * infoPtr->uNumItem);
1289     infoPtr->order = ReAlloc(infoPtr->order, sizeof(INT) * infoPtr->uNumItem);
1290     
1291     /* make space for the new item */
1292     memmove(&infoPtr->items[nItem + 1], &infoPtr->items[nItem],
1293             (infoPtr->uNumItem - nItem - 1) * sizeof(HEADER_ITEM));
1294     memmove(&infoPtr->order[iOrder + 1], &infoPtr->order[iOrder],
1295            (infoPtr->uNumItem - iOrder - 1) * sizeof(INT));
1296
1297     /* update the order array */
1298     infoPtr->order[iOrder] = nItem;
1299     for (i = 0; i < infoPtr->uNumItem; i++)
1300     {
1301         if (i != iOrder && infoPtr->order[i] >= nItem)
1302             infoPtr->order[i]++;
1303         infoPtr->items[infoPtr->order[i]].iOrder = i;
1304     }
1305
1306     lpItem = &infoPtr->items[nItem];
1307     ZeroMemory(lpItem, sizeof(HEADER_ITEM));
1308     /* cxy, fmt and lParam are copied even if not in the HDITEM mask */
1309     copyMask = phdi->mask | HDI_WIDTH | HDI_FORMAT | HDI_LPARAM;
1310     HEADER_StoreHDItemInHeader(lpItem, copyMask, phdi, bUnicode);
1311     lpItem->iOrder = iOrder;
1312
1313     /* set automatically some format bits */
1314     if (phdi->mask & HDI_TEXT)
1315         lpItem->fmt |= HDF_STRING;
1316     else
1317         lpItem->fmt &= ~HDF_STRING;
1318
1319     if (lpItem->hbm != NULL)
1320         lpItem->fmt |= HDF_BITMAP;
1321     else
1322         lpItem->fmt &= ~HDF_BITMAP;
1323
1324     if (phdi->mask & HDI_IMAGE)
1325         lpItem->fmt |= HDF_IMAGE;
1326
1327     HEADER_SetItemBounds (hwnd);
1328     InvalidateRect(hwnd, NULL, FALSE);
1329
1330     return nItem;
1331 }
1332
1333
1334 static LRESULT
1335 HEADER_Layout (HWND hwnd, LPARAM lParam)
1336 {
1337     HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
1338     LPHDLAYOUT lpLayout = (LPHDLAYOUT)lParam;
1339
1340     lpLayout->pwpos->hwnd = hwnd;
1341     lpLayout->pwpos->hwndInsertAfter = 0;
1342     lpLayout->pwpos->x = lpLayout->prc->left;
1343     lpLayout->pwpos->y = lpLayout->prc->top;
1344     lpLayout->pwpos->cx = lpLayout->prc->right - lpLayout->prc->left;
1345     if (GetWindowLongW (hwnd, GWL_STYLE) & HDS_HIDDEN)
1346         lpLayout->pwpos->cy = 0;
1347     else {
1348         lpLayout->pwpos->cy = infoPtr->nHeight;
1349         lpLayout->prc->top += infoPtr->nHeight;
1350     }
1351     lpLayout->pwpos->flags = SWP_NOZORDER;
1352
1353     TRACE("Layout x=%d y=%d cx=%d cy=%d\n",
1354            lpLayout->pwpos->x, lpLayout->pwpos->y,
1355            lpLayout->pwpos->cx, lpLayout->pwpos->cy);
1356
1357     infoPtr->bRectsValid = FALSE;
1358
1359     return TRUE;
1360 }
1361
1362
1363 static LRESULT
1364 HEADER_SetImageList (HWND hwnd, HIMAGELIST himl)
1365 {
1366     HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
1367     HIMAGELIST himlOld;
1368
1369     TRACE("(himl %p)\n", himl);
1370     himlOld = infoPtr->himl;
1371     infoPtr->himl = himl;
1372
1373     /* FIXME: Refresh needed??? */
1374
1375     return (LRESULT)himlOld;
1376 }
1377
1378
1379 static LRESULT
1380 HEADER_GetBitmapMargin(HWND hwnd)
1381 {
1382     HEADER_INFO *infoPtr = HEADER_GetInfoPtr(hwnd);
1383     
1384     return infoPtr->iMargin;
1385 }
1386
1387 static LRESULT
1388 HEADER_SetBitmapMargin(HWND hwnd, WPARAM wParam)
1389 {
1390     HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
1391     INT oldMargin = infoPtr->iMargin;
1392
1393     infoPtr->iMargin = (INT)wParam;
1394
1395     return oldMargin;
1396 }
1397
1398 static LRESULT
1399 HEADER_SetItemT (HWND hwnd, INT nItem, const HDITEMW *phdi, BOOL bUnicode)
1400 {
1401     HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
1402     HEADER_ITEM *lpItem;
1403     HDITEMW hdNotify;
1404     void *pvScratch;
1405
1406     if (phdi == NULL)
1407         return FALSE;
1408     if ((nItem < 0) || (nItem >= (INT)infoPtr->uNumItem))
1409         return FALSE;
1410
1411     TRACE("[nItem=%d]\n", nItem);
1412
1413     HEADER_CopyHDItemForNotify(infoPtr, &hdNotify, phdi, bUnicode, &pvScratch);
1414     if (HEADER_SendNotifyWithHDItemT(hwnd, HDN_ITEMCHANGINGW, nItem, &hdNotify))
1415     {
1416         Free(pvScratch);
1417         return FALSE;
1418     }
1419
1420     lpItem = &infoPtr->items[nItem];
1421     HEADER_StoreHDItemInHeader(lpItem, phdi->mask, phdi, bUnicode);
1422
1423     if (phdi->mask & HDI_ORDER)
1424         if (phdi->iOrder >= 0 && phdi->iOrder < infoPtr->uNumItem)
1425             HEADER_ChangeItemOrder(infoPtr, nItem, phdi->iOrder);
1426
1427     HEADER_SendNotifyWithHDItemT(hwnd, HDN_ITEMCHANGEDW, nItem, &hdNotify);
1428
1429     HEADER_SetItemBounds (hwnd);
1430
1431     InvalidateRect(hwnd, NULL, FALSE);
1432
1433     Free(pvScratch);
1434     return TRUE;
1435 }
1436
1437 static inline LRESULT
1438 HEADER_SetUnicodeFormat (HWND hwnd, WPARAM wParam)
1439 {
1440     HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
1441     BOOL bTemp = (infoPtr->nNotifyFormat == NFR_UNICODE);
1442
1443     infoPtr->nNotifyFormat = ((BOOL)wParam ? NFR_UNICODE : NFR_ANSI);
1444
1445     return bTemp;
1446 }
1447
1448
1449 static LRESULT
1450 HEADER_Create (HWND hwnd, LPARAM lParam)
1451 {
1452     HEADER_INFO *infoPtr;
1453     TEXTMETRICW tm;
1454     HFONT hOldFont;
1455     HDC   hdc;
1456
1457     infoPtr = Alloc (sizeof(HEADER_INFO));
1458     SetWindowLongPtrW (hwnd, 0, (DWORD_PTR)infoPtr);
1459
1460     infoPtr->hwndNotify = ((LPCREATESTRUCTA)lParam)->hwndParent;
1461     infoPtr->uNumItem = 0;
1462     infoPtr->hFont = 0;
1463     infoPtr->items = 0;
1464     infoPtr->order = 0;
1465     infoPtr->bRectsValid = FALSE;
1466     infoPtr->hcurArrow = LoadCursorW (0, (LPWSTR)IDC_ARROW);
1467     infoPtr->hcurDivider = LoadCursorW (COMCTL32_hModule, MAKEINTRESOURCEW(IDC_DIVIDER));
1468     infoPtr->hcurDivopen = LoadCursorW (COMCTL32_hModule, MAKEINTRESOURCEW(IDC_DIVIDEROPEN));
1469     infoPtr->bPressed  = FALSE;
1470     infoPtr->bTracking = FALSE;
1471     infoPtr->iMoveItem = 0;
1472     infoPtr->himl = 0;
1473     infoPtr->iHotItem = -1;
1474     infoPtr->iHotDivider = -1;
1475     infoPtr->iMargin = 3*GetSystemMetrics(SM_CXEDGE);
1476     infoPtr->nNotifyFormat =
1477         SendMessageW (infoPtr->hwndNotify, WM_NOTIFYFORMAT, (WPARAM)hwnd, NF_QUERY);
1478
1479     hdc = GetDC (0);
1480     hOldFont = SelectObject (hdc, GetStockObject (SYSTEM_FONT));
1481     GetTextMetricsW (hdc, &tm);
1482     infoPtr->nHeight = tm.tmHeight + VERT_BORDER;
1483     SelectObject (hdc, hOldFont);
1484     ReleaseDC (0, hdc);
1485
1486     OpenThemeData(hwnd, themeClass);
1487
1488     return 0;
1489 }
1490
1491
1492 static LRESULT
1493 HEADER_Destroy (HWND hwnd)
1494 {
1495     HTHEME theme = GetWindowTheme(hwnd);
1496     CloseThemeData(theme);
1497     return 0;
1498 }
1499
1500 static LRESULT
1501 HEADER_NCDestroy (HWND hwnd)
1502 {
1503     HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
1504     HEADER_ITEM *lpItem;
1505     INT nItem;
1506
1507     if (infoPtr->items) {
1508         lpItem = infoPtr->items;
1509         for (nItem = 0; nItem < infoPtr->uNumItem; nItem++, lpItem++) {
1510             Free(lpItem->pszText);
1511         }
1512         Free (infoPtr->items);
1513     }
1514
1515     Free(infoPtr->order);
1516
1517     if (infoPtr->himl)
1518       ImageList_Destroy (infoPtr->himl);
1519
1520     SetWindowLongPtrW (hwnd, 0, 0);
1521     Free (infoPtr);
1522
1523     return 0;
1524 }
1525
1526
1527 static inline LRESULT
1528 HEADER_GetFont (HWND hwnd)
1529 {
1530     HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
1531
1532     return (LRESULT)infoPtr->hFont;
1533 }
1534
1535
1536 static BOOL
1537 HEADER_IsDragDistance(const HEADER_INFO *infoPtr, const POINT *pt)
1538 {
1539     /* Windows allows for a mouse movement before starting the drag. We use the
1540      * SM_CXDOUBLECLICK/SM_CYDOUBLECLICK as that distance.
1541      */
1542     return (abs(infoPtr->ptLButtonDown.x - pt->x)>GetSystemMetrics(SM_CXDOUBLECLK) ||
1543             abs(infoPtr->ptLButtonDown.y - pt->y)>GetSystemMetrics(SM_CYDOUBLECLK));
1544 }
1545
1546 static LRESULT
1547 HEADER_LButtonDblClk (HWND hwnd, LPARAM lParam)
1548 {
1549     POINT pt;
1550     UINT  flags;
1551     INT   nItem;
1552
1553     pt.x = (short)LOWORD(lParam);
1554     pt.y = (short)HIWORD(lParam);
1555     HEADER_InternalHitTest (hwnd, &pt, &flags, &nItem);
1556
1557     if ((GetWindowLongW (hwnd, GWL_STYLE) & HDS_BUTTONS) && (flags == HHT_ONHEADER))
1558         HEADER_SendNotifyWithHDItemT(hwnd, HDN_ITEMDBLCLICKW, nItem, NULL);
1559     else if ((flags == HHT_ONDIVIDER) || (flags == HHT_ONDIVOPEN))
1560         HEADER_SendNotifyWithHDItemT(hwnd, HDN_DIVIDERDBLCLICKW, nItem, NULL);
1561
1562     return 0;
1563 }
1564
1565
1566 static LRESULT
1567 HEADER_LButtonDown (HWND hwnd, LPARAM lParam)
1568 {
1569     HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
1570     DWORD dwStyle = GetWindowLongW (hwnd, GWL_STYLE);
1571     POINT pt;
1572     UINT  flags;
1573     INT   nItem;
1574     HDC   hdc;
1575
1576     pt.x = (short)LOWORD(lParam);
1577     pt.y = (short)HIWORD(lParam);
1578     HEADER_InternalHitTest (hwnd, &pt, &flags, &nItem);
1579
1580     if ((dwStyle & HDS_BUTTONS) && (flags == HHT_ONHEADER)) {
1581         SetCapture (hwnd);
1582         infoPtr->bCaptured = TRUE;
1583         infoPtr->bPressed  = TRUE;
1584         infoPtr->bDragging = FALSE;
1585         infoPtr->iMoveItem = nItem;
1586         infoPtr->ptLButtonDown = pt;
1587
1588         infoPtr->items[nItem].bDown = TRUE;
1589
1590         /* Send WM_CUSTOMDRAW */
1591         hdc = GetDC (hwnd);
1592         HEADER_RefreshItem (hwnd, nItem);
1593         ReleaseDC (hwnd, hdc);
1594
1595         TRACE("Pressed item %d!\n", nItem);
1596     }
1597     else if ((flags == HHT_ONDIVIDER) || (flags == HHT_ONDIVOPEN)) {
1598         INT iCurrWidth = infoPtr->items[nItem].cxy;
1599         if (!HEADER_SendNotifyWithIntFieldT(hwnd, HDN_BEGINTRACKW, nItem, HDI_WIDTH, iCurrWidth))
1600         {
1601             SetCapture (hwnd);
1602             infoPtr->bCaptured = TRUE;
1603             infoPtr->bTracking = TRUE;
1604             infoPtr->iMoveItem = nItem;
1605             infoPtr->xTrackOffset = infoPtr->items[nItem].rect.right - pt.x;
1606
1607             if (!(dwStyle & HDS_FULLDRAG)) {
1608                 infoPtr->xOldTrack = infoPtr->items[nItem].rect.right;
1609                 hdc = GetDC (hwnd);
1610                 HEADER_DrawTrackLine (hwnd, hdc, infoPtr->xOldTrack);
1611                 ReleaseDC (hwnd, hdc);
1612             }
1613
1614             TRACE("Begin tracking item %d!\n", nItem);
1615         }
1616     }
1617
1618     return 0;
1619 }
1620
1621
1622 static LRESULT
1623 HEADER_LButtonUp (HWND hwnd, LPARAM lParam)
1624 {
1625     HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
1626     DWORD dwStyle = GetWindowLongW (hwnd, GWL_STYLE);
1627     POINT pt;
1628     UINT  flags;
1629     INT   nItem;
1630     HDC   hdc;
1631
1632     pt.x = (INT)(SHORT)LOWORD(lParam);
1633     pt.y = (INT)(SHORT)HIWORD(lParam);
1634     HEADER_InternalHitTest (hwnd, &pt, &flags, &nItem);
1635
1636     if (infoPtr->bPressed) {
1637         if (infoPtr->bDragging)
1638         {
1639             HEADER_ITEM *lpItem = &infoPtr->items[infoPtr->iMoveItem];
1640             INT iNewOrder;
1641             
1642             ImageList_DragShowNolock(FALSE);
1643             ImageList_EndDrag();
1644             lpItem->bDown=FALSE;
1645             
1646             if (infoPtr->iHotDivider == -1)
1647                 iNewOrder = -1;
1648             else if (infoPtr->iHotDivider == infoPtr->uNumItem)
1649                 iNewOrder = infoPtr->uNumItem-1;
1650             else
1651             {
1652                 iNewOrder = HEADER_IndexToOrder(hwnd, infoPtr->iHotDivider);
1653                 if (iNewOrder > lpItem->iOrder)
1654                     iNewOrder--;
1655             }
1656
1657             if (iNewOrder != -1 &&
1658                 !HEADER_SendNotifyWithIntFieldT(hwnd, HDN_ENDDRAG, infoPtr->iMoveItem, HDI_ORDER, iNewOrder))
1659             {
1660                 HEADER_ChangeItemOrder(infoPtr, infoPtr->iMoveItem, iNewOrder);
1661                 infoPtr->bRectsValid = FALSE;
1662                 InvalidateRect(hwnd, NULL, FALSE);
1663             }
1664             else
1665                 InvalidateRect(hwnd, &infoPtr->items[infoPtr->iMoveItem].rect, FALSE);
1666                 
1667             HEADER_SetHotDivider(hwnd, FALSE, -1);
1668         }
1669         else if (!(dwStyle&HDS_DRAGDROP) || !HEADER_IsDragDistance(infoPtr, &pt))
1670         {
1671             infoPtr->items[infoPtr->iMoveItem].bDown = FALSE;
1672             hdc = GetDC (hwnd);
1673             HEADER_RefreshItem (hwnd, infoPtr->iMoveItem);
1674             ReleaseDC (hwnd, hdc);
1675
1676             HEADER_SendNotifyWithHDItemT(hwnd, HDN_ITEMCLICKW, infoPtr->iMoveItem, NULL);
1677         }
1678
1679         TRACE("Released item %d!\n", infoPtr->iMoveItem);
1680         infoPtr->bPressed = FALSE;
1681     }
1682     else if (infoPtr->bTracking) {
1683         INT iNewWidth = pt.x - infoPtr->items[infoPtr->iMoveItem].rect.left + infoPtr->xTrackOffset;
1684         if (iNewWidth < 0)
1685             iNewWidth = 0;
1686         TRACE("End tracking item %d!\n", infoPtr->iMoveItem);
1687         infoPtr->bTracking = FALSE;
1688
1689         HEADER_SendNotifyWithIntFieldT(hwnd, HDN_ENDTRACKW, infoPtr->iMoveItem, HDI_WIDTH, iNewWidth);
1690
1691         if (!(dwStyle & HDS_FULLDRAG)) {
1692             hdc = GetDC (hwnd);
1693             HEADER_DrawTrackLine (hwnd, hdc, infoPtr->xOldTrack);
1694             ReleaseDC (hwnd, hdc);
1695         }
1696           
1697         if (!HEADER_SendNotifyWithIntFieldT(hwnd, HDN_ITEMCHANGINGW, infoPtr->iMoveItem, HDI_WIDTH, iNewWidth))
1698         {
1699             infoPtr->items[infoPtr->iMoveItem].cxy = iNewWidth;
1700             HEADER_SendNotifyWithIntFieldT(hwnd, HDN_ITEMCHANGEDW, infoPtr->iMoveItem, HDI_WIDTH, iNewWidth);
1701         }
1702
1703         HEADER_SetItemBounds (hwnd);
1704         InvalidateRect(hwnd, NULL, TRUE);
1705     }
1706
1707     if (infoPtr->bCaptured) {
1708         infoPtr->bCaptured = FALSE;
1709         ReleaseCapture ();
1710         HEADER_SendSimpleNotify (hwnd, NM_RELEASEDCAPTURE);
1711     }
1712
1713     return 0;
1714 }
1715
1716
1717 static LRESULT
1718 HEADER_NotifyFormat (HWND hwnd, WPARAM wParam, LPARAM lParam)
1719 {
1720     HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
1721
1722     switch (lParam)
1723     {
1724         case NF_QUERY:
1725             return infoPtr->nNotifyFormat;
1726
1727         case NF_REQUERY:
1728             infoPtr->nNotifyFormat =
1729                 SendMessageW ((HWND)wParam, WM_NOTIFYFORMAT,
1730                               (WPARAM)hwnd, (LPARAM)NF_QUERY);
1731             return infoPtr->nNotifyFormat;
1732     }
1733
1734     return 0;
1735 }
1736
1737 static LRESULT
1738 HEADER_MouseLeave (HWND hwnd)
1739 {
1740     HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
1741     /* Reset hot-tracked item when mouse leaves control. */
1742     INT oldHotItem = infoPtr->iHotItem;
1743     HDC hdc = GetDC (hwnd);
1744
1745     infoPtr->iHotItem = -1;
1746     if (oldHotItem != -1) HEADER_RefreshItem (hwnd, oldHotItem);
1747     ReleaseDC (hwnd, hdc);
1748
1749     return 0;
1750 }
1751
1752
1753 static LRESULT
1754 HEADER_MouseMove (HWND hwnd, LPARAM lParam)
1755 {
1756     HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
1757     DWORD dwStyle = GetWindowLongW (hwnd, GWL_STYLE);
1758     POINT pt;
1759     UINT  flags;
1760     INT   nItem, nWidth;
1761     HDC   hdc;
1762     /* With theming, hottracking is always enabled */
1763     BOOL  hotTrackEnabled =
1764         ((dwStyle & HDS_BUTTONS) && (dwStyle & HDS_HOTTRACK))
1765         || (GetWindowTheme (hwnd) != NULL);
1766     INT oldHotItem = infoPtr->iHotItem;
1767
1768     pt.x = (INT)(SHORT)LOWORD(lParam);
1769     pt.y = (INT)(SHORT)HIWORD(lParam);
1770     HEADER_InternalHitTest (hwnd, &pt, &flags, &nItem);
1771
1772     if (hotTrackEnabled) {
1773         if (flags & (HHT_ONHEADER | HHT_ONDIVIDER | HHT_ONDIVOPEN))
1774             infoPtr->iHotItem = nItem;
1775         else
1776             infoPtr->iHotItem = -1;
1777     }
1778
1779     if (infoPtr->bCaptured) {
1780         /* check if we should drag the header */
1781         if (infoPtr->bPressed && !infoPtr->bDragging && dwStyle&HDS_DRAGDROP
1782             && HEADER_IsDragDistance(infoPtr, &pt))
1783         {
1784             if (!HEADER_SendNotifyWithHDItemT(hwnd, HDN_BEGINDRAG, infoPtr->iMoveItem, NULL))
1785             {
1786                 HIMAGELIST hDragItem = (HIMAGELIST)HEADER_CreateDragImage(hwnd, infoPtr->iMoveItem);
1787                 if (hDragItem != NULL)
1788                 {
1789                     HEADER_ITEM *lpItem = &infoPtr->items[infoPtr->iMoveItem];
1790                     TRACE("Starting item drag\n");
1791                     ImageList_BeginDrag(hDragItem, 0, pt.x - lpItem->rect.left, 0);
1792                     ImageList_DragShowNolock(TRUE);
1793                     ImageList_Destroy(hDragItem);
1794                     infoPtr->bDragging = TRUE;
1795                 }
1796             }
1797         }
1798         
1799         if (infoPtr->bDragging)
1800         {
1801             POINT drag;
1802             drag.x = pt.x;
1803             drag.y = 0;
1804             ClientToScreen(hwnd, &drag);
1805             ImageList_DragMove(drag.x, drag.y);
1806             HEADER_SetHotDivider(hwnd, TRUE, lParam);
1807         }
1808         
1809         if (infoPtr->bPressed && !infoPtr->bDragging) {
1810             BOOL oldState = infoPtr->items[infoPtr->iMoveItem].bDown;
1811             if ((nItem == infoPtr->iMoveItem) && (flags == HHT_ONHEADER))
1812                 infoPtr->items[infoPtr->iMoveItem].bDown = TRUE;
1813             else
1814                 infoPtr->items[infoPtr->iMoveItem].bDown = FALSE;
1815             if (oldState != infoPtr->items[infoPtr->iMoveItem].bDown) {
1816                 hdc = GetDC (hwnd);
1817                 HEADER_RefreshItem (hwnd, infoPtr->iMoveItem);
1818                 ReleaseDC (hwnd, hdc);
1819             }
1820
1821             TRACE("Moving pressed item %d!\n", infoPtr->iMoveItem);
1822         }
1823         else if (infoPtr->bTracking) {
1824             if (dwStyle & HDS_FULLDRAG) {
1825                 HEADER_ITEM *lpItem = &infoPtr->items[infoPtr->iMoveItem];
1826                 nWidth = pt.x - lpItem->rect.left + infoPtr->xTrackOffset;
1827                 if (!HEADER_SendNotifyWithIntFieldT(hwnd, HDN_ITEMCHANGINGW, infoPtr->iMoveItem, HDI_WIDTH, nWidth))
1828                 {
1829                     INT nOldWidth = lpItem->rect.right - lpItem->rect.left;
1830                     RECT rcClient;
1831                     RECT rcScroll;
1832                     
1833                     if (nWidth < 0) nWidth = 0;
1834                     infoPtr->items[infoPtr->iMoveItem].cxy = nWidth;
1835                     HEADER_SetItemBounds(hwnd);
1836                     
1837                     GetClientRect(hwnd, &rcClient);
1838                     rcScroll = rcClient;
1839                     rcScroll.left = lpItem->rect.left + nOldWidth;
1840                     ScrollWindowEx(hwnd, nWidth - nOldWidth, 0, &rcScroll, &rcClient, NULL, NULL, 0);
1841                     InvalidateRect(hwnd, &lpItem->rect, FALSE);
1842                     UpdateWindow(hwnd);
1843                     
1844                     HEADER_SendNotifyWithIntFieldT(hwnd, HDN_ITEMCHANGEDW, infoPtr->iMoveItem, HDI_WIDTH, nWidth);
1845                 }
1846             }
1847             else {
1848                 INT iTrackWidth;
1849                 hdc = GetDC (hwnd);
1850                 HEADER_DrawTrackLine (hwnd, hdc, infoPtr->xOldTrack);
1851                 infoPtr->xOldTrack = pt.x + infoPtr->xTrackOffset;
1852                 if (infoPtr->xOldTrack < infoPtr->items[infoPtr->iMoveItem].rect.left)
1853                     infoPtr->xOldTrack = infoPtr->items[infoPtr->iMoveItem].rect.left;
1854                 HEADER_DrawTrackLine (hwnd, hdc, infoPtr->xOldTrack);
1855                 ReleaseDC (hwnd, hdc);
1856                 iTrackWidth = infoPtr->xOldTrack - infoPtr->items[infoPtr->iMoveItem].rect.left;
1857                 /* FIXME: should stop tracking if HDN_TRACK returns TRUE */
1858                 HEADER_SendNotifyWithIntFieldT(hwnd, HDN_TRACKW, infoPtr->iMoveItem, HDI_WIDTH, iTrackWidth);
1859             }
1860
1861             TRACE("Tracking item %d!\n", infoPtr->iMoveItem);
1862         }
1863     }
1864
1865     if (hotTrackEnabled) {
1866         TRACKMOUSEEVENT tme;
1867         if (oldHotItem != infoPtr->iHotItem && !infoPtr->bDragging) {
1868             hdc = GetDC (hwnd);
1869             if (oldHotItem != -1) HEADER_RefreshItem (hwnd, oldHotItem);
1870             if (infoPtr->iHotItem != -1) HEADER_RefreshItem (hwnd, infoPtr->iHotItem);
1871             ReleaseDC (hwnd, hdc);
1872         }
1873         tme.cbSize = sizeof( tme );
1874         tme.dwFlags = TME_LEAVE;
1875         tme.hwndTrack = hwnd;
1876         TrackMouseEvent( &tme );
1877     }
1878
1879     return 0;
1880 }
1881
1882
1883 static LRESULT
1884 HEADER_Paint (HWND hwnd, WPARAM wParam)
1885 {
1886     HDC hdc;
1887     PAINTSTRUCT ps;
1888
1889     hdc = wParam==0 ? BeginPaint (hwnd, &ps) : (HDC)wParam;
1890     HEADER_Refresh (hwnd, hdc);
1891     if(!wParam)
1892         EndPaint (hwnd, &ps);
1893     return 0;
1894 }
1895
1896
1897 static LRESULT
1898 HEADER_RButtonUp (HWND hwnd, LPARAM lParam)
1899 {
1900     BOOL bRet;
1901     POINT pt;
1902
1903     pt.x = (short)LOWORD(lParam);
1904     pt.y = (short)HIWORD(lParam);
1905
1906     /* Send a Notify message */
1907     bRet = HEADER_SendSimpleNotify (hwnd, NM_RCLICK);
1908
1909     /* Change to screen coordinate for WM_CONTEXTMENU */
1910     ClientToScreen(hwnd, &pt);
1911
1912     /* Send a WM_CONTEXTMENU message in response to the RBUTTONUP */
1913     SendMessageW( hwnd, WM_CONTEXTMENU, (WPARAM) hwnd, MAKELPARAM(pt.x, pt.y));
1914
1915     return bRet;
1916 }
1917
1918
1919 static LRESULT
1920 HEADER_SetCursor (HWND hwnd, LPARAM lParam)
1921 {
1922     HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
1923     POINT pt;
1924     UINT  flags;
1925     INT   nItem;
1926
1927     TRACE("code=0x%X  id=0x%X\n", LOWORD(lParam), HIWORD(lParam));
1928
1929     GetCursorPos (&pt);
1930     ScreenToClient (hwnd, &pt);
1931
1932     HEADER_InternalHitTest (hwnd, &pt, &flags, &nItem);
1933
1934     if (flags == HHT_ONDIVIDER)
1935         SetCursor (infoPtr->hcurDivider);
1936     else if (flags == HHT_ONDIVOPEN)
1937         SetCursor (infoPtr->hcurDivopen);
1938     else
1939         SetCursor (infoPtr->hcurArrow);
1940
1941     return 0;
1942 }
1943
1944
1945 static LRESULT
1946 HEADER_SetFont (HWND hwnd, WPARAM wParam, LPARAM lParam)
1947 {
1948     HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
1949     TEXTMETRICW tm;
1950     HFONT hFont, hOldFont;
1951     HDC hdc;
1952
1953     infoPtr->hFont = (HFONT)wParam;
1954
1955     hFont = infoPtr->hFont ? infoPtr->hFont : GetStockObject (SYSTEM_FONT);
1956
1957     hdc = GetDC (0);
1958     hOldFont = SelectObject (hdc, hFont);
1959     GetTextMetricsW (hdc, &tm);
1960     infoPtr->nHeight = tm.tmHeight + VERT_BORDER;
1961     SelectObject (hdc, hOldFont);
1962     ReleaseDC (0, hdc);
1963
1964     infoPtr->bRectsValid = FALSE;
1965
1966     if (lParam) {
1967         InvalidateRect(hwnd, NULL, FALSE);
1968     }
1969
1970     return 0;
1971 }
1972
1973 static LRESULT HEADER_SetRedraw(HWND hwnd, WPARAM wParam, LPARAM lParam)
1974 {
1975     /* ignoring the InvalidateRect calls is handled by user32. But some apps expect
1976      * that we invalidate the header and this has to be done manually  */
1977     LRESULT ret;
1978
1979     ret = DefWindowProcW(hwnd, WM_SETREDRAW, wParam, lParam);
1980     if (wParam)
1981         InvalidateRect(hwnd, NULL, TRUE);
1982     return ret;
1983 }
1984
1985 /* Update the theme handle after a theme change */
1986 static LRESULT HEADER_ThemeChanged(HWND hwnd)
1987 {
1988     HTHEME theme = GetWindowTheme(hwnd);
1989     CloseThemeData(theme);
1990     OpenThemeData(hwnd, themeClass);
1991     InvalidateRect(hwnd, NULL, FALSE);
1992     return 0;
1993 }
1994
1995
1996 static LRESULT WINAPI
1997 HEADER_WindowProc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
1998 {
1999     TRACE("hwnd=%p msg=%x wparam=%lx lParam=%lx\n", hwnd, msg, wParam, lParam);
2000     if (!HEADER_GetInfoPtr (hwnd) && (msg != WM_CREATE))
2001         return DefWindowProcW (hwnd, msg, wParam, lParam);
2002     switch (msg) {
2003 /*      case HDM_CLEARFILTER: */
2004
2005         case HDM_CREATEDRAGIMAGE:
2006             return HEADER_CreateDragImage (hwnd, wParam);
2007
2008         case HDM_DELETEITEM:
2009             return HEADER_DeleteItem (hwnd, wParam);
2010
2011 /*      case HDM_EDITFILTER: */
2012
2013         case HDM_GETBITMAPMARGIN:
2014             return HEADER_GetBitmapMargin(hwnd);
2015
2016         case HDM_GETIMAGELIST:
2017             return HEADER_GetImageList (hwnd);
2018
2019         case HDM_GETITEMA:
2020         case HDM_GETITEMW:
2021             return HEADER_GetItemT (hwnd, (INT)wParam, (LPHDITEMW)lParam, msg == HDM_GETITEMW);
2022
2023         case HDM_GETITEMCOUNT:
2024             return HEADER_GetItemCount (hwnd);
2025
2026         case HDM_GETITEMRECT:
2027             return HEADER_GetItemRect (hwnd, wParam, lParam);
2028
2029         case HDM_GETORDERARRAY:
2030             return HEADER_GetOrderArray(hwnd, wParam, lParam);
2031
2032         case HDM_GETUNICODEFORMAT:
2033             return HEADER_GetUnicodeFormat (hwnd);
2034
2035         case HDM_HITTEST:
2036             return HEADER_HitTest (hwnd, lParam);
2037
2038         case HDM_INSERTITEMA:
2039         case HDM_INSERTITEMW:
2040             return HEADER_InsertItemT (hwnd, (INT)wParam, (LPHDITEMW)lParam, msg == HDM_INSERTITEMW);
2041
2042         case HDM_LAYOUT:
2043             return HEADER_Layout (hwnd, lParam);
2044
2045         case HDM_ORDERTOINDEX:
2046             return HEADER_OrderToIndex(hwnd, wParam);
2047
2048         case HDM_SETBITMAPMARGIN:
2049             return HEADER_SetBitmapMargin(hwnd, wParam);
2050
2051 /*      case HDM_SETFILTERCHANGETIMEOUT: */
2052
2053         case HDM_SETHOTDIVIDER:
2054             return HEADER_SetHotDivider(hwnd, wParam, lParam);
2055
2056         case HDM_SETIMAGELIST:
2057             return HEADER_SetImageList (hwnd, (HIMAGELIST)lParam);
2058
2059         case HDM_SETITEMA:
2060         case HDM_SETITEMW:
2061             return HEADER_SetItemT (hwnd, (INT)wParam, (LPHDITEMW)lParam, msg == HDM_SETITEMW);
2062
2063         case HDM_SETORDERARRAY:
2064             return HEADER_SetOrderArray(hwnd, wParam, lParam);
2065
2066         case HDM_SETUNICODEFORMAT:
2067             return HEADER_SetUnicodeFormat (hwnd, wParam);
2068
2069         case WM_CREATE:
2070             return HEADER_Create (hwnd, lParam);
2071
2072         case WM_DESTROY:
2073             return HEADER_Destroy (hwnd);
2074
2075         case WM_NCDESTROY:
2076             return HEADER_NCDestroy (hwnd);
2077
2078         case WM_ERASEBKGND:
2079             return 1;
2080
2081         case WM_GETDLGCODE:
2082             return DLGC_WANTTAB | DLGC_WANTARROWS;
2083
2084         case WM_GETFONT:
2085             return HEADER_GetFont (hwnd);
2086
2087         case WM_LBUTTONDBLCLK:
2088             return HEADER_LButtonDblClk (hwnd, lParam);
2089
2090         case WM_LBUTTONDOWN:
2091             return HEADER_LButtonDown (hwnd, lParam);
2092
2093         case WM_LBUTTONUP:
2094             return HEADER_LButtonUp (hwnd, lParam);
2095
2096         case WM_MOUSELEAVE:
2097             return HEADER_MouseLeave (hwnd);
2098
2099         case WM_MOUSEMOVE:
2100             return HEADER_MouseMove (hwnd, lParam);
2101
2102         case WM_NOTIFYFORMAT:
2103             return HEADER_NotifyFormat (hwnd, wParam, lParam);
2104
2105         case WM_SIZE:
2106             return HEADER_Size (hwnd);
2107
2108         case WM_THEMECHANGED:
2109             return HEADER_ThemeChanged (hwnd);
2110
2111         case WM_PRINTCLIENT:
2112         case WM_PAINT:
2113             return HEADER_Paint (hwnd, wParam);
2114
2115         case WM_RBUTTONUP:
2116             return HEADER_RButtonUp (hwnd, lParam);
2117
2118         case WM_SETCURSOR:
2119             return HEADER_SetCursor (hwnd, lParam);
2120
2121         case WM_SETFONT:
2122             return HEADER_SetFont (hwnd, wParam, lParam);
2123
2124         case WM_SETREDRAW:
2125             return HEADER_SetRedraw(hwnd, wParam, lParam);
2126
2127         default:
2128             if ((msg >= WM_USER) && (msg < WM_APP) && !COMCTL32_IsReflectedMessage(msg))
2129                 ERR("unknown msg %04x wp=%04lx lp=%08lx\n",
2130                      msg, wParam, lParam );
2131             return DefWindowProcW(hwnd, msg, wParam, lParam);
2132     }
2133 }
2134
2135
2136 VOID
2137 HEADER_Register (void)
2138 {
2139     WNDCLASSW wndClass;
2140
2141     ZeroMemory (&wndClass, sizeof(WNDCLASSW));
2142     wndClass.style         = CS_GLOBALCLASS | CS_DBLCLKS;
2143     wndClass.lpfnWndProc   = HEADER_WindowProc;
2144     wndClass.cbClsExtra    = 0;
2145     wndClass.cbWndExtra    = sizeof(HEADER_INFO *);
2146     wndClass.hCursor       = LoadCursorW (0, (LPWSTR)IDC_ARROW);
2147     wndClass.lpszClassName = WC_HEADERW;
2148
2149     RegisterClassW (&wndClass);
2150 }
2151
2152
2153 VOID
2154 HEADER_Unregister (void)
2155 {
2156     UnregisterClassW (WC_HEADERW, NULL);
2157 }