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