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