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