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