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