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