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