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