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