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