comctl32 header: Free the old string also when the new one is LPSTR_TEXTCALLBACK.
[wine/multimedia.git] / dlls / comctl32 / header.c
blobbed77cae553af483c4c44b05f5a9557444fdaa9a
1 /*
2 * Header control
4 * Copyright 1998 Eric Kohl
5 * Copyright 2000 Eric Kohl for CodeWeavers
6 * Copyright 2003 Maxime Bellenge
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.
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.
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
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
34 #include <stdarg.h>
35 #include <string.h>
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"
50 WINE_DEFAULT_DEBUG_CHANNEL(header);
52 typedef struct
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 */
62 BOOL bDown; /* is item pressed? (used for drawing) */
63 RECT rect; /* bounding rectangle of the item */
64 } HEADER_ITEM;
67 typedef struct
69 HWND hwndNotify; /* Owner window to send notifications to */
70 INT nNotifyFormat; /* format used for WM_NOTIFY messages */
71 UINT uNumItem; /* number of items (columns) */
72 INT nHeight; /* height of the header (pixels) */
73 HFONT hFont; /* handle to the current font */
74 HCURSOR hcurArrow; /* handle to the arrow cursor */
75 HCURSOR hcurDivider; /* handle to a cursor (used over dividers) <-|-> */
76 HCURSOR hcurDivopen; /* handle to a cursor (used over dividers) <-||-> */
77 BOOL bCaptured; /* Is the mouse captured? */
78 BOOL bPressed; /* Is a header item pressed (down)? */
79 BOOL bTracking; /* Is in tracking mode? */
80 INT iMoveItem; /* index of tracked item. (Tracking mode) */
81 INT xTrackOffset; /* distance between the right side of the tracked item and the cursor */
82 INT xOldTrack; /* track offset (see above) after the last WM_MOUSEMOVE */
83 INT nOldWidth; /* width of a sizing item after the last WM_MOUSEMOVE */
84 INT iHotItem; /* index of hot item (cursor is over this item) */
85 INT iMargin; /* width of the margin that surrounds a bitmap */
87 HIMAGELIST himl; /* handle to an image list (may be 0) */
88 HEADER_ITEM *items; /* pointer to array of HEADER_ITEM's */
89 INT *order; /* array of item IDs indexed by order */
90 BOOL bRectsValid; /* validity flag for bounding rectangles */
91 } HEADER_INFO;
94 #define VERT_BORDER 3
95 #define DIVIDER_WIDTH 10
96 #define HDN_UNICODE_OFFSET 20
97 #define HDN_FIRST_UNICODE (HDN_FIRST-HDN_UNICODE_OFFSET)
99 #define HEADER_GetInfoPtr(hwnd) ((HEADER_INFO *)GetWindowLongPtrW(hwnd,0))
101 static const WCHAR themeClass[] = {'H','e','a','d','e','r',0};
102 static WCHAR emptyString[] = {0};
104 static void HEADER_DisposeItem(HEADER_ITEM *lpItem)
106 if (lpItem->pszText && lpItem->pszText != emptyString &&
107 lpItem->pszText != LPSTR_TEXTCALLBACKW) /* covers LPSTR_TEXTCALLBACKA too */
109 Free(lpItem->pszText);
113 static void HEADER_StoreHDItemInHeader(HEADER_ITEM *lpItem, HDITEMW *phdi, BOOL fUnicode)
115 if (phdi->mask & HDI_BITMAP)
116 lpItem->hbm = phdi->hbm;
118 if (phdi->mask & HDI_FORMAT)
119 lpItem->fmt = phdi->fmt;
121 if (phdi->mask & HDI_LPARAM)
122 lpItem->lParam = phdi->lParam;
124 if (phdi->mask & HDI_WIDTH)
125 lpItem->cxy = phdi->cxy;
127 if (phdi->mask & HDI_IMAGE)
129 lpItem->iImage = phdi->iImage;
132 if (phdi->mask & HDI_TEXT)
134 if (lpItem->pszText)
136 if (lpItem->pszText != emptyString && lpItem->pszText != LPSTR_TEXTCALLBACKW)
137 Free(lpItem->pszText);
138 lpItem->pszText = NULL;
141 if (phdi->pszText != LPSTR_TEXTCALLBACKW) /* covers != TEXTCALLBACKA too */
143 LPWSTR pszText = (phdi->pszText != NULL ? phdi->pszText : emptyString);
144 if (fUnicode)
145 Str_SetPtrW(&lpItem->pszText, pszText);
146 else
147 Str_SetPtrAtoW(&lpItem->pszText, (LPSTR)pszText);
149 else
151 lpItem->pszText = phdi->pszText;
156 inline static LRESULT
157 HEADER_IndexToOrder (HWND hwnd, INT iItem)
159 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
160 HEADER_ITEM *lpItem = &infoPtr->items[iItem];
161 return lpItem->iOrder;
165 static INT
166 HEADER_OrderToIndex(HWND hwnd, WPARAM wParam)
168 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
169 INT iorder = (INT)wParam;
171 if ((iorder <0) || iorder >= infoPtr->uNumItem)
172 return iorder;
173 return infoPtr->order[iorder];
176 static void
177 HEADER_SetItemBounds (HWND hwnd)
179 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
180 HEADER_ITEM *phdi;
181 RECT rect;
182 unsigned int i;
183 int x;
185 infoPtr->bRectsValid = TRUE;
187 if (infoPtr->uNumItem == 0)
188 return;
190 GetClientRect (hwnd, &rect);
192 x = rect.left;
193 for (i = 0; i < infoPtr->uNumItem; i++) {
194 phdi = &infoPtr->items[HEADER_OrderToIndex(hwnd,i)];
195 phdi->rect.top = rect.top;
196 phdi->rect.bottom = rect.bottom;
197 phdi->rect.left = x;
198 phdi->rect.right = phdi->rect.left + ((phdi->cxy>0)?phdi->cxy:0);
199 x = phdi->rect.right;
203 static LRESULT
204 HEADER_Size (HWND hwnd, WPARAM wParam)
206 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
208 infoPtr->bRectsValid = FALSE;
210 return 0;
214 static INT
215 HEADER_DrawItem (HWND hwnd, HDC hdc, INT iItem, BOOL bHotTrack)
217 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
218 HEADER_ITEM *phdi = &infoPtr->items[iItem];
219 RECT r;
220 INT oldBkMode, cxEdge = GetSystemMetrics(SM_CXEDGE);
221 HTHEME theme = GetWindowTheme (hwnd);
222 NMCUSTOMDRAW nmcd;
224 TRACE("DrawItem(iItem %d bHotTrack %d unicode flag %d)\n", iItem, bHotTrack, (infoPtr->nNotifyFormat == NFR_UNICODE));
226 if (!infoPtr->bRectsValid)
227 HEADER_SetItemBounds(hwnd);
229 r = phdi->rect;
230 if (r.right - r.left == 0)
231 return phdi->rect.right;
233 if (theme != NULL) {
234 int state = (phdi->bDown) ? HIS_PRESSED :
235 (bHotTrack ? HIS_HOT : HIS_NORMAL);
236 DrawThemeBackground (theme, hdc, HP_HEADERITEM, state,
237 &r, NULL);
238 GetThemeBackgroundContentRect (theme, hdc, HP_HEADERITEM, state,
239 &r, &r);
241 else {
242 if (GetWindowLongW (hwnd, GWL_STYLE) & HDS_BUTTONS) {
243 if (phdi->bDown) {
244 DrawEdge (hdc, &r, BDR_RAISEDOUTER,
245 BF_RECT | BF_FLAT | BF_MIDDLE | BF_ADJUST);
247 else
248 DrawEdge (hdc, &r, EDGE_RAISED,
249 BF_RECT | BF_SOFT | BF_MIDDLE | BF_ADJUST);
251 else
252 DrawEdge (hdc, &r, EDGE_ETCHED, BF_BOTTOM | BF_RIGHT | BF_ADJUST);
254 if (phdi->bDown) {
255 r.left += 2;
256 r.top += 2;
259 r.left -= cxEdge;
260 r.right += cxEdge;
262 /* Set the colors before sending NM_CUSTOMDRAW so that it can change them */
263 SetTextColor (hdc, (bHotTrack && !theme) ? COLOR_HIGHLIGHT : COLOR_BTNTEXT);
264 SetBkColor(hdc, GetSysColor(COLOR_3DFACE));
266 nmcd.hdr.hwndFrom = hwnd;
267 nmcd.hdr.idFrom = GetWindowLongPtrW (hwnd, GWLP_ID);
268 nmcd.hdr.code = NM_CUSTOMDRAW;
269 nmcd.dwDrawStage = CDDS_PREPAINT | CDDS_ITEM | CDDS_ITEMPOSTERASE;
270 nmcd.hdc = hdc;
271 nmcd.dwItemSpec = iItem;
272 nmcd.rc = r;
273 nmcd.uItemState = phdi->bDown ? CDIS_SELECTED : 0;
274 nmcd.lItemlParam = phdi->lParam;
276 SendMessageW (infoPtr->hwndNotify, WM_NOTIFY, nmcd.hdr.idFrom, (LPARAM)&nmcd);
278 if (phdi->fmt & HDF_OWNERDRAW) {
279 DRAWITEMSTRUCT dis;
281 dis.CtlType = ODT_HEADER;
282 dis.CtlID = GetWindowLongPtrW (hwnd, GWLP_ID);
283 dis.itemID = iItem;
284 dis.itemAction = ODA_DRAWENTIRE;
285 dis.itemState = phdi->bDown ? ODS_SELECTED : 0;
286 dis.hwndItem = hwnd;
287 dis.hDC = hdc;
288 dis.rcItem = r;
289 dis.itemData = phdi->lParam;
290 oldBkMode = SetBkMode(hdc, TRANSPARENT);
291 SendMessageW (infoPtr->hwndNotify, WM_DRAWITEM,
292 (WPARAM)dis.CtlID, (LPARAM)&dis);
293 if (oldBkMode != TRANSPARENT)
294 SetBkMode(hdc, oldBkMode);
296 else {
297 UINT rw, rh, /* width and height of r */
298 *x = NULL, *w = NULL; /* x and width of the pic (bmp or img) which is part of cnt */
299 /* cnt,txt,img,bmp */
300 UINT cx, tx, ix, bx,
301 cw, tw, iw, bw;
302 BITMAP bmp;
304 cw = tw = iw = bw = 0;
305 rw = r.right - r.left;
306 rh = r.bottom - r.top;
308 if (theme == NULL) {
309 HBRUSH hbr = CreateSolidBrush(GetBkColor(hdc));
310 RECT rcBackground = r;
312 rcBackground.right -= cxEdge;
313 FillRect(hdc, &rcBackground, hbr);
314 DeleteObject(hbr);
316 if (phdi->fmt & HDF_STRING) {
317 RECT textRect;
319 DrawTextW (hdc, phdi->pszText, -1,
320 &textRect, DT_LEFT|DT_VCENTER|DT_SINGLELINE|DT_CALCRECT);
321 cw = textRect.right - textRect.left + 2 * infoPtr->iMargin;
324 if ((phdi->fmt & HDF_IMAGE) && (infoPtr->himl)) {
325 iw = infoPtr->himl->cx + 2 * infoPtr->iMargin;
326 x = &ix;
327 w = &iw;
330 if ((phdi->fmt & HDF_BITMAP) && (phdi->hbm)) {
331 GetObjectW (phdi->hbm, sizeof(BITMAP), (LPVOID)&bmp);
332 bw = bmp.bmWidth + 2 * infoPtr->iMargin;
333 if (!iw) {
334 x = &bx;
335 w = &bw;
339 if (bw || iw)
340 cw += *w;
342 /* align cx using the unclipped cw */
343 if ((phdi->fmt & HDF_JUSTIFYMASK) == HDF_LEFT)
344 cx = r.left;
345 else if ((phdi->fmt & HDF_JUSTIFYMASK) == HDF_CENTER)
346 cx = r.left + rw / 2 - cw / 2;
347 else /* HDF_RIGHT */
348 cx = r.right - cw;
350 /* clip cx & cw */
351 if (cx < r.left)
352 cx = r.left;
353 if (cx + cw > r.right)
354 cw = r.right - cx;
356 tx = cx + infoPtr->iMargin;
357 /* since cw might have changed we have to recalculate tw */
358 tw = cw - infoPtr->iMargin * 2;
360 if (iw || bw) {
361 tw -= *w;
362 if (phdi->fmt & HDF_BITMAP_ON_RIGHT) {
363 /* put pic behind text */
364 *x = cx + tw + infoPtr->iMargin * 3;
365 } else {
366 *x = cx + infoPtr->iMargin;
367 /* move text behind pic */
368 tx += *w;
372 if (iw && bw) {
373 /* since we're done with the layout we can
374 now calculate the position of bmp which
375 has no influence on alignment and layout
376 because of img */
377 if ((phdi->fmt & HDF_JUSTIFYMASK) == HDF_RIGHT)
378 bx = cx - bw + infoPtr->iMargin;
379 else
380 bx = cx + cw + infoPtr->iMargin;
383 if (iw || bw) {
384 HDC hClipDC = GetDC(hwnd);
385 HRGN hClipRgn = CreateRectRgn(r.left, r.top, r.right, r.bottom);
386 SelectClipRgn(hClipDC, hClipRgn);
388 if (bw) {
389 HDC hdcBitmap = CreateCompatibleDC (hClipDC);
390 SelectObject (hdcBitmap, phdi->hbm);
391 BitBlt (hClipDC, bx, r.top + ((INT)rh - bmp.bmHeight) / 2,
392 bmp.bmWidth, bmp.bmHeight, hdcBitmap, 0, 0, SRCCOPY);
393 DeleteDC (hdcBitmap);
396 if (iw) {
397 ImageList_DrawEx (infoPtr->himl, phdi->iImage, hClipDC,
398 ix, r.top + ((INT)rh - infoPtr->himl->cy) / 2,
399 infoPtr->himl->cx, infoPtr->himl->cy, CLR_DEFAULT, CLR_DEFAULT, 0);
402 DeleteObject(hClipRgn);
403 ReleaseDC(hwnd, hClipDC);
406 if (((phdi->fmt & HDF_STRING)
407 || (!(phdi->fmt & (HDF_OWNERDRAW|HDF_STRING|HDF_BITMAP|
408 HDF_BITMAP_ON_RIGHT|HDF_IMAGE)))) /* no explicit format specified? */
409 && (phdi->pszText)) {
410 oldBkMode = SetBkMode(hdc, TRANSPARENT);
411 r.left = tx;
412 r.right = tx + tw;
413 DrawTextW (hdc, phdi->pszText, -1,
414 &r, DT_LEFT|DT_END_ELLIPSIS|DT_VCENTER|DT_SINGLELINE);
415 if (oldBkMode != TRANSPARENT)
416 SetBkMode(hdc, oldBkMode);
418 }/*Ownerdrawn*/
420 return phdi->rect.right;
424 static void
425 HEADER_Refresh (HWND hwnd, HDC hdc)
427 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
428 HFONT hFont, hOldFont;
429 RECT rect;
430 HBRUSH hbrBk;
431 UINT i;
432 INT x;
433 HTHEME theme = GetWindowTheme (hwnd);
435 /* get rect for the bar, adjusted for the border */
436 GetClientRect (hwnd, &rect);
438 hFont = infoPtr->hFont ? infoPtr->hFont : GetStockObject (SYSTEM_FONT);
439 hOldFont = SelectObject (hdc, hFont);
441 /* draw Background */
442 if (theme == NULL) {
443 hbrBk = GetSysColorBrush(COLOR_3DFACE);
444 FillRect(hdc, &rect, hbrBk);
447 x = rect.left;
448 for (i = 0; x <= rect.right && i < infoPtr->uNumItem; i++) {
449 x = HEADER_DrawItem (hwnd, hdc, HEADER_OrderToIndex(hwnd,i),
450 infoPtr->iHotItem == i);
453 if ((x <= rect.right) && (infoPtr->uNumItem > 0)) {
454 rect.left = x;
455 if (theme != NULL) {
456 DrawThemeBackground (theme, hdc, HP_HEADERITEM, HIS_NORMAL, &rect,
457 NULL);
459 else {
460 if (GetWindowLongW (hwnd, GWL_STYLE) & HDS_BUTTONS)
461 DrawEdge (hdc, &rect, EDGE_RAISED, BF_TOP|BF_LEFT|BF_BOTTOM|BF_SOFT);
462 else
463 DrawEdge (hdc, &rect, EDGE_ETCHED, BF_BOTTOM);
467 SelectObject (hdc, hOldFont);
471 static void
472 HEADER_RefreshItem (HWND hwnd, HDC hdc, INT iItem)
474 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
475 HFONT hFont, hOldFont;
477 hFont = infoPtr->hFont ? infoPtr->hFont : GetStockObject (SYSTEM_FONT);
478 hOldFont = SelectObject (hdc, hFont);
479 HEADER_DrawItem (hwnd, hdc, iItem, infoPtr->iHotItem == iItem);
480 SelectObject (hdc, hOldFont);
484 static void
485 HEADER_InternalHitTest (HWND hwnd, LPPOINT lpPt, UINT *pFlags, INT *pItem)
487 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
488 RECT rect, rcTest;
489 UINT iCount;
490 INT width;
491 BOOL bNoWidth;
493 GetClientRect (hwnd, &rect);
495 *pFlags = 0;
496 bNoWidth = FALSE;
497 if (PtInRect (&rect, *lpPt))
499 if (infoPtr->uNumItem == 0) {
500 *pFlags |= HHT_NOWHERE;
501 *pItem = 1;
502 TRACE("NOWHERE\n");
503 return;
505 else {
506 /* somewhere inside */
507 for (iCount = 0; iCount < infoPtr->uNumItem; iCount++) {
508 rect = infoPtr->items[iCount].rect;
509 width = rect.right - rect.left;
510 if (width == 0) {
511 bNoWidth = TRUE;
512 continue;
514 if (PtInRect (&rect, *lpPt)) {
515 if (width <= 2 * DIVIDER_WIDTH) {
516 *pFlags |= HHT_ONHEADER;
517 *pItem = iCount;
518 TRACE("ON HEADER %d\n", iCount);
519 return;
521 if (iCount > 0) {
522 rcTest = rect;
523 rcTest.right = rcTest.left + DIVIDER_WIDTH;
524 if (PtInRect (&rcTest, *lpPt)) {
525 if (bNoWidth) {
526 *pFlags |= HHT_ONDIVOPEN;
527 *pItem = iCount - 1;
528 TRACE("ON DIVOPEN %d\n", *pItem);
529 return;
531 else {
532 *pFlags |= HHT_ONDIVIDER;
533 *pItem = iCount - 1;
534 TRACE("ON DIVIDER %d\n", *pItem);
535 return;
539 rcTest = rect;
540 rcTest.left = rcTest.right - DIVIDER_WIDTH;
541 if (PtInRect (&rcTest, *lpPt)) {
542 *pFlags |= HHT_ONDIVIDER;
543 *pItem = iCount;
544 TRACE("ON DIVIDER %d\n", *pItem);
545 return;
548 *pFlags |= HHT_ONHEADER;
549 *pItem = iCount;
550 TRACE("ON HEADER %d\n", iCount);
551 return;
555 /* check for last divider part (on nowhere) */
556 rect = infoPtr->items[infoPtr->uNumItem-1].rect;
557 rect.left = rect.right;
558 rect.right += DIVIDER_WIDTH;
559 if (PtInRect (&rect, *lpPt)) {
560 if (bNoWidth) {
561 *pFlags |= HHT_ONDIVOPEN;
562 *pItem = infoPtr->uNumItem - 1;
563 TRACE("ON DIVOPEN %d\n", *pItem);
564 return;
566 else {
567 *pFlags |= HHT_ONDIVIDER;
568 *pItem = infoPtr->uNumItem-1;
569 TRACE("ON DIVIDER %d\n", *pItem);
570 return;
574 *pFlags |= HHT_NOWHERE;
575 *pItem = 1;
576 TRACE("NOWHERE\n");
577 return;
580 else {
581 if (lpPt->x < rect.left) {
582 TRACE("TO LEFT\n");
583 *pFlags |= HHT_TOLEFT;
585 else if (lpPt->x > rect.right) {
586 TRACE("TO RIGHT\n");
587 *pFlags |= HHT_TORIGHT;
590 if (lpPt->y < rect.top) {
591 TRACE("ABOVE\n");
592 *pFlags |= HHT_ABOVE;
594 else if (lpPt->y > rect.bottom) {
595 TRACE("BELOW\n");
596 *pFlags |= HHT_BELOW;
600 *pItem = 1;
601 TRACE("flags=0x%X\n", *pFlags);
602 return;
606 static void
607 HEADER_DrawTrackLine (HWND hwnd, HDC hdc, INT x)
609 RECT rect;
610 HPEN hOldPen;
611 INT oldRop;
613 GetClientRect (hwnd, &rect);
615 hOldPen = SelectObject (hdc, GetStockObject (BLACK_PEN));
616 oldRop = SetROP2 (hdc, R2_XORPEN);
617 MoveToEx (hdc, x, rect.top, NULL);
618 LineTo (hdc, x, rect.bottom);
619 SetROP2 (hdc, oldRop);
620 SelectObject (hdc, hOldPen);
623 /***
624 * DESCRIPTION:
625 * Convert a HDITEM into the correct format (ANSI/Unicode) to send it in a notify
627 * PARAMETER(S):
628 * [I] infoPtr : the header that wants to send the notify
629 * [O] dest : The buffer to store the HDITEM for notify. It may be set to a HDITEMA of HDITEMW
630 * [I] src : The source HDITEM. It may be a HDITEMA or HDITEMW
631 * [I] fSourceUnicode : is src a HDITEMW or HDITEMA
632 * [O] ppvScratch : a pointer to a scratch buffer that needs to be freed after
633 * the HDITEM is no longer in use or NULL if none was needed
635 * NOTE: We depend on HDITEMA and HDITEMW having the same structure
637 static void HEADER_CopyHDItemForNotify(HEADER_INFO *infoPtr, HDITEMW *dest,
638 HDITEMW *src, BOOL fSourceUnicode, LPVOID *ppvScratch)
640 *ppvScratch = NULL;
641 *dest = *src;
643 if (src->mask & HDI_TEXT && src->pszText != LPSTR_TEXTCALLBACKW) /* covers TEXTCALLBACKA as well */
645 if (fSourceUnicode && infoPtr->nNotifyFormat != NFR_UNICODE)
647 dest->pszText = NULL;
648 Str_SetPtrWtoA((LPSTR *)&dest->pszText, src->pszText);
649 *ppvScratch = dest->pszText;
652 if (!fSourceUnicode && infoPtr->nNotifyFormat == NFR_UNICODE)
654 dest->pszText = NULL;
655 Str_SetPtrAtoW(&dest->pszText, (LPSTR)src->pszText);
656 *ppvScratch = dest->pszText;
661 static UINT HEADER_NotifyCodeWtoA(UINT code)
663 /* we use the fact that all the unicode messages are in HDN_FIRST_UNICODE..HDN_LAST*/
664 if (code >= HDN_LAST && code <= HDN_FIRST_UNICODE)
665 return code + HDN_UNICODE_OFFSET;
666 else
667 return code;
670 static BOOL
671 HEADER_SendSimpleNotify (HWND hwnd, UINT code)
673 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
674 NMHDR nmhdr;
676 nmhdr.hwndFrom = hwnd;
677 nmhdr.idFrom = GetWindowLongPtrW (hwnd, GWLP_ID);
678 nmhdr.code = code;
680 return (BOOL)SendMessageW (infoPtr->hwndNotify, WM_NOTIFY,
681 (WPARAM)nmhdr.idFrom, (LPARAM)&nmhdr);
684 static BOOL
685 HEADER_SendHeaderNotifyT (HWND hwnd, UINT code, INT iItem, INT mask, HDITEMW *lpItem)
687 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
688 NMHEADERW nmhdr;
689 HDITEMW nmitem;
691 if (lpItem == NULL)
693 /* lpItem == NULL means that we should take the actual data from the item */
694 if (mask & HDI_TEXT)
696 FIXME("(): invalid parameters - lpItem == NULL and (mask & HDI_TEXT)\n");
697 mask &= ~HDI_TEXT;
699 nmitem.mask = mask;
700 nmitem.cxy = infoPtr->items[iItem].cxy;
701 nmitem.hbm = infoPtr->items[iItem].hbm;
702 nmitem.pszText = NULL;
703 nmitem.cchTextMax = 0;
704 nmitem.fmt = infoPtr->items[iItem].fmt;
705 nmitem.lParam = infoPtr->items[iItem].lParam;
706 nmitem.iOrder = infoPtr->items[iItem].iOrder;
707 nmitem.iImage = infoPtr->items[iItem].iImage;
708 lpItem = &nmitem;
711 nmhdr.hdr.hwndFrom = hwnd;
712 nmhdr.hdr.idFrom = GetWindowLongPtrW (hwnd, GWLP_ID);
713 nmhdr.hdr.code = (infoPtr->nNotifyFormat == NFR_UNICODE ? code : HEADER_NotifyCodeWtoA(code));
714 nmhdr.iItem = iItem;
715 nmhdr.iButton = 0;
716 nmhdr.pitem = lpItem;
718 return (BOOL)SendMessageW (infoPtr->hwndNotify, WM_NOTIFY,
719 (WPARAM)nmhdr.hdr.idFrom, (LPARAM)&nmhdr);
723 * Send Disp Info notification.
724 * depends on NMHDDISPINFOW having same structure as NMHDDISPINFOA
725 * (so we handle the two cases only doing a specific cast for pszText).
727 * @param hwnd : hwnd header container handler
728 * @param mask : notification mask (usually HDI_TEXT or HDI_IMAGE)
729 * @param pDispInfo : NMHDDISPINFO structure (can be unicode or ansi)
730 * @param isW : TRUE if dispinfo is Unicode
732 static BOOL
733 HEADER_SendHeaderDispInfoNotify(HWND hwnd, INT iItem, INT mask, LPHDITEMW phdi, HEADER_ITEM* lpItem, BOOL isW)
735 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
736 BOOL ret;
737 BOOL convertToAnsi = FALSE;
738 BOOL convertToUnicode = FALSE;
739 BOOL isUnicodeNotify = FALSE;
740 NMHDDISPINFOW dispInfo;
742 if (mask & HDI_TEXT)
744 convertToAnsi = (isW && infoPtr->nNotifyFormat == NFR_ANSI);
745 convertToUnicode = (!isW && infoPtr->nNotifyFormat == NFR_UNICODE);
747 isUnicodeNotify = (isW && !convertToAnsi);
749 memset(&dispInfo, 0, sizeof(NMHDDISPINFOW));
750 dispInfo.hdr.hwndFrom = hwnd;
751 dispInfo.hdr.idFrom = GetWindowLongPtrW (hwnd, GWLP_ID);
752 if (isUnicodeNotify || convertToUnicode)
754 dispInfo.hdr.code = HDN_GETDISPINFOW;
756 else
758 dispInfo.hdr.code = HDN_GETDISPINFOA;
760 dispInfo.iItem = iItem;
761 dispInfo.mask = mask;
763 dispInfo.pszText = Alloc(sizeof(WCHAR) * 260);
764 dispInfo.cchTextMax = 260;
766 ret = (BOOL) SendMessageW(infoPtr->hwndNotify, WM_NOTIFY,
767 (WPARAM) dispInfo.hdr.idFrom,
768 (LPARAM) &dispInfo);
770 TRACE("SendMessage returns(mask:0x%x,str:%s,lParam:%p)\n",
771 dispInfo.mask,
772 (isUnicodeNotify ? debugstr_w(dispInfo.pszText) : (LPSTR) dispInfo.pszText),
773 (void*) dispInfo.lParam);
775 if (dispInfo.mask & HDI_DI_SETITEM)
777 if (dispInfo.mask & HDI_IMAGE)
779 lpItem->iImage = dispInfo.iImage;
781 if (dispInfo.mask & HDI_TEXT)
783 if (isUnicodeNotify || convertToUnicode)
784 Str_SetPtrW(&lpItem->pszText, (LPCWSTR)dispInfo.pszText);
785 else /*if (convertToAnsi || !isW)*/
786 Str_SetPtrAtoW(&lpItem->pszText, (LPCSTR)dispInfo.pszText);
789 FIXME("NMHDDISPINFO returns with flags HDI_DI_SETITEM\n");
792 if (NULL != phdi)
794 if ((phdi->mask & mask) & HDI_IMAGE)
796 phdi->iImage = dispInfo.iImage;
798 if ((phdi->mask & mask) & HDI_TEXT)
800 if (isUnicodeNotify)
801 Str_GetPtrW ((LPCWSTR)dispInfo.pszText, phdi->pszText, phdi->cchTextMax);
802 else if (convertToUnicode)
803 Str_GetPtrWtoA ((LPCWSTR)dispInfo.pszText, (LPSTR)phdi->pszText, phdi->cchTextMax);
804 else /*if (!isW) */
805 Str_GetPtrA ((LPCSTR)dispInfo.pszText, (LPSTR)phdi->pszText, phdi->cchTextMax);
808 return ret;
812 static BOOL
813 HEADER_SendClickNotify (HWND hwnd, UINT code, INT iItem)
815 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
816 NMHEADERA nmhdr;
818 nmhdr.hdr.hwndFrom = hwnd;
819 nmhdr.hdr.idFrom = GetWindowLongPtrW (hwnd, GWLP_ID);
820 nmhdr.hdr.code = code;
821 nmhdr.iItem = iItem;
822 nmhdr.iButton = 0;
823 nmhdr.pitem = NULL;
825 return (BOOL)SendMessageA (infoPtr->hwndNotify, WM_NOTIFY,
826 (WPARAM)nmhdr.hdr.idFrom, (LPARAM)&nmhdr);
830 static LRESULT
831 HEADER_CreateDragImage (HWND hwnd, WPARAM wParam)
833 FIXME("empty stub!\n");
834 return 0;
838 static LRESULT
839 HEADER_DeleteItem (HWND hwnd, WPARAM wParam)
841 HEADER_INFO *infoPtr = HEADER_GetInfoPtr(hwnd);
842 INT iItem = (INT)wParam;
844 TRACE("[iItem=%d]\n", iItem);
846 if ((iItem < 0) || (iItem >= (INT)infoPtr->uNumItem))
847 return FALSE;
849 if (infoPtr->uNumItem == 1) {
850 TRACE("Simple delete!\n");
851 HEADER_DisposeItem(&infoPtr->items[0]);
852 Free (infoPtr->items);
853 Free(infoPtr->order);
854 infoPtr->items = 0;
855 infoPtr->order = 0;
856 infoPtr->uNumItem = 0;
858 else {
859 HEADER_ITEM *oldItems = infoPtr->items;
860 INT i;
861 INT iOrder;
862 TRACE("Complex delete! [iItem=%d]\n", iItem);
864 for (i = 0; i < infoPtr->uNumItem; i++)
865 TRACE("%d: order=%d, iOrder=%d, ->iOrder=%d\n", i, infoPtr->order[i], infoPtr->items[i].iOrder, infoPtr->items[infoPtr->order[i]].iOrder);
866 HEADER_DisposeItem(&infoPtr->items[iItem]);
867 iOrder = infoPtr->items[iItem].iOrder;
869 infoPtr->uNumItem--;
870 infoPtr->items = Alloc (sizeof (HEADER_ITEM) * infoPtr->uNumItem);
871 /* pre delete copy */
872 if (iItem > 0) {
873 memcpy (&infoPtr->items[0], &oldItems[0],
874 iItem * sizeof(HEADER_ITEM));
877 /* post delete copy */
878 if (iItem < infoPtr->uNumItem) {
879 memcpy (&infoPtr->items[iItem], &oldItems[iItem+1],
880 (infoPtr->uNumItem - iItem) * sizeof(HEADER_ITEM));
883 /* Correct the orders */
884 if (iOrder < infoPtr->uNumItem)
886 memmove(&infoPtr->order[iOrder], &infoPtr->order[iOrder + 1],
887 (infoPtr->uNumItem - iOrder) * sizeof(INT));
888 for (i = 0; i < infoPtr->uNumItem; i++)
890 if (infoPtr->order[i] > iItem)
891 infoPtr->order[i]--;
892 if (i >= iOrder)
893 infoPtr->items[infoPtr->order[i]].iOrder = infoPtr->order[i];
897 for (i = 0; i < infoPtr->uNumItem; i++)
898 TRACE("%d: order=%d, iOrder=%d, ->iOrder=%d\n", i, infoPtr->order[i], infoPtr->items[i].iOrder, infoPtr->items[infoPtr->order[i]].iOrder);
899 Free (oldItems);
902 HEADER_SetItemBounds (hwnd);
904 InvalidateRect(hwnd, NULL, FALSE);
906 return TRUE;
910 static LRESULT
911 HEADER_GetImageList (HWND hwnd)
913 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
915 return (LRESULT)infoPtr->himl;
919 static LRESULT
920 HEADER_GetItemT (HWND hwnd, INT nItem, LPHDITEMW phdi, BOOL bUnicode)
922 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
923 HEADER_ITEM *lpItem;
925 if (!phdi)
926 return FALSE;
928 TRACE("[nItem=%d]\n", nItem);
930 if (phdi->mask == 0)
931 return TRUE;
932 if ((nItem < 0) || (nItem >= (INT)infoPtr->uNumItem))
933 return FALSE;
935 lpItem = &infoPtr->items[nItem];
937 if (phdi->mask & HDI_BITMAP)
938 phdi->hbm = lpItem->hbm;
940 if (phdi->mask & HDI_FORMAT)
941 phdi->fmt = lpItem->fmt;
943 if (phdi->mask & HDI_WIDTH)
944 phdi->cxy = lpItem->cxy;
946 if (phdi->mask & HDI_LPARAM)
947 phdi->lParam = lpItem->lParam;
949 if (phdi->mask & HDI_IMAGE)
951 phdi->iImage = lpItem->iImage;
952 if (lpItem->iImage == I_IMAGECALLBACK)
954 HEADER_SendHeaderDispInfoNotify(hwnd, nItem, HDI_IMAGE, phdi, lpItem, bUnicode);
958 if (phdi->mask & HDI_ORDER)
959 phdi->iOrder = lpItem->iOrder;
961 if (phdi->mask & HDI_TEXT)
963 if (lpItem->pszText == LPSTR_TEXTCALLBACKW) /* covers == TEXTCALLBACKA too */
965 HEADER_SendHeaderDispInfoNotify(hwnd, nItem, HDI_TEXT, phdi, lpItem, bUnicode);
967 else
969 if (bUnicode)
970 Str_GetPtrW (lpItem->pszText, phdi->pszText, phdi->cchTextMax);
971 else
972 Str_GetPtrWtoA (lpItem->pszText, (LPSTR)phdi->pszText, phdi->cchTextMax);
976 return TRUE;
980 inline static LRESULT
981 HEADER_GetItemCount (HWND hwnd)
983 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
984 return infoPtr->uNumItem;
988 static LRESULT
989 HEADER_GetItemRect (HWND hwnd, WPARAM wParam, LPARAM lParam)
991 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
992 INT iItem = (INT)wParam;
993 LPRECT lpRect = (LPRECT)lParam;
995 if ((iItem < 0) || (iItem >= (INT)infoPtr->uNumItem))
996 return FALSE;
998 lpRect->left = infoPtr->items[iItem].rect.left;
999 lpRect->right = infoPtr->items[iItem].rect.right;
1000 lpRect->top = infoPtr->items[iItem].rect.top;
1001 lpRect->bottom = infoPtr->items[iItem].rect.bottom;
1003 return TRUE;
1007 static LRESULT
1008 HEADER_GetOrderArray(HWND hwnd, WPARAM wParam, LPARAM lParam)
1010 LPINT order = (LPINT) lParam;
1011 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
1013 if ((unsigned int)wParam <infoPtr->uNumItem)
1014 return FALSE;
1016 memcpy(order, infoPtr->order, infoPtr->uNumItem * sizeof(INT));
1017 return TRUE;
1020 static LRESULT
1021 HEADER_SetOrderArray(HWND hwnd, WPARAM wParam, LPARAM lParam)
1023 int i;
1024 LPINT order = (LPINT) lParam;
1025 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
1026 HEADER_ITEM *lpItem;
1028 if ((unsigned int)wParam <infoPtr->uNumItem)
1029 return FALSE;
1030 memcpy(infoPtr->order, order, infoPtr->uNumItem * sizeof(INT));
1031 for (i=0; i<(int)wParam; i++)
1033 lpItem = &infoPtr->items[*order++];
1034 lpItem->iOrder=i;
1036 infoPtr->bRectsValid=0;
1037 InvalidateRect(hwnd, NULL, FALSE);
1038 return TRUE;
1041 inline static LRESULT
1042 HEADER_GetUnicodeFormat (HWND hwnd)
1044 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
1045 return (infoPtr->nNotifyFormat == NFR_UNICODE);
1049 static LRESULT
1050 HEADER_HitTest (HWND hwnd, WPARAM wParam, LPARAM lParam)
1052 LPHDHITTESTINFO phti = (LPHDHITTESTINFO)lParam;
1054 HEADER_InternalHitTest (hwnd, &phti->pt, &phti->flags, &phti->iItem);
1056 if (phti->flags == HHT_NOWHERE)
1057 return -1;
1058 else
1059 return phti->iItem;
1063 static LRESULT
1064 HEADER_InsertItemT (HWND hwnd, INT nItem, LPHDITEMW phdi, BOOL bUnicode)
1066 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
1067 HEADER_ITEM *lpItem;
1068 INT iOrder;
1069 UINT i;
1071 if ((phdi == NULL) || (nItem < 0))
1072 return -1;
1074 if (nItem > infoPtr->uNumItem)
1075 nItem = infoPtr->uNumItem;
1077 iOrder = (phdi->mask & HDI_ORDER) ? phdi->iOrder : nItem;
1078 if (iOrder < 0)
1079 iOrder = 0;
1080 else if (infoPtr->uNumItem < iOrder)
1081 iOrder = infoPtr->uNumItem;
1083 if (infoPtr->uNumItem == 0) {
1084 infoPtr->items = Alloc (sizeof (HEADER_ITEM));
1085 infoPtr->order = Alloc(sizeof(INT));
1086 infoPtr->uNumItem++;
1088 else {
1089 HEADER_ITEM *oldItems = infoPtr->items;
1090 INT *oldOrder = infoPtr->order;
1092 infoPtr->uNumItem++;
1093 infoPtr->items = Alloc (sizeof (HEADER_ITEM) * infoPtr->uNumItem);
1094 if (nItem == 0) {
1095 memcpy (&infoPtr->items[1], &oldItems[0],
1096 (infoPtr->uNumItem-1) * sizeof(HEADER_ITEM));
1098 else
1100 /* pre insert copy */
1101 if (nItem > 0) {
1102 memcpy (&infoPtr->items[0], &oldItems[0],
1103 nItem * sizeof(HEADER_ITEM));
1106 /* post insert copy */
1107 if (nItem < infoPtr->uNumItem - 1) {
1108 memcpy (&infoPtr->items[nItem+1], &oldItems[nItem],
1109 (infoPtr->uNumItem - nItem - 1) * sizeof(HEADER_ITEM));
1113 infoPtr->order = Alloc(sizeof(INT) * infoPtr->uNumItem);
1114 memcpy(infoPtr->order, oldOrder, iOrder * sizeof(INT));
1115 infoPtr->order[iOrder] = nItem;
1116 memcpy(&infoPtr->order[iOrder + 1], &oldOrder[iOrder],
1117 (infoPtr->uNumItem - iOrder - 1) * sizeof(INT));
1119 Free (oldItems);
1120 Free(oldOrder);
1123 for (i = 0; i < infoPtr->uNumItem; i++)
1125 if (i != iOrder && infoPtr->order[i] >= nItem)
1126 infoPtr->order[i]++;
1127 infoPtr->items[infoPtr->order[i]].iOrder = infoPtr->order[i];
1130 lpItem = &infoPtr->items[nItem];
1131 ZeroMemory(lpItem, sizeof(HEADER_ITEM));
1132 HEADER_StoreHDItemInHeader(lpItem, phdi, bUnicode);
1134 if (phdi->mask & HDI_TEXT)
1135 lpItem->fmt |= HDF_STRING;
1137 lpItem->iOrder = iOrder;
1139 HEADER_SetItemBounds (hwnd);
1141 InvalidateRect(hwnd, NULL, FALSE);
1143 return nItem;
1147 static LRESULT
1148 HEADER_Layout (HWND hwnd, WPARAM wParam, LPARAM lParam)
1150 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
1151 LPHDLAYOUT lpLayout = (LPHDLAYOUT)lParam;
1153 lpLayout->pwpos->hwnd = hwnd;
1154 lpLayout->pwpos->hwndInsertAfter = 0;
1155 lpLayout->pwpos->x = lpLayout->prc->left;
1156 lpLayout->pwpos->y = lpLayout->prc->top;
1157 lpLayout->pwpos->cx = lpLayout->prc->right - lpLayout->prc->left;
1158 if (GetWindowLongW (hwnd, GWL_STYLE) & HDS_HIDDEN)
1159 lpLayout->pwpos->cy = 0;
1160 else {
1161 lpLayout->pwpos->cy = infoPtr->nHeight;
1162 lpLayout->prc->top += infoPtr->nHeight;
1164 lpLayout->pwpos->flags = SWP_NOZORDER;
1166 TRACE("Layout x=%d y=%d cx=%d cy=%d\n",
1167 lpLayout->pwpos->x, lpLayout->pwpos->y,
1168 lpLayout->pwpos->cx, lpLayout->pwpos->cy);
1170 infoPtr->bRectsValid = FALSE;
1172 return TRUE;
1176 static LRESULT
1177 HEADER_SetImageList (HWND hwnd, HIMAGELIST himl)
1179 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
1180 HIMAGELIST himlOld;
1182 TRACE("(himl %p)\n", himl);
1183 himlOld = infoPtr->himl;
1184 infoPtr->himl = himl;
1186 /* FIXME: Refresh needed??? */
1188 return (LRESULT)himlOld;
1192 static LRESULT
1193 HEADER_GetBitmapMargin(HWND hwnd)
1195 HEADER_INFO *infoPtr = HEADER_GetInfoPtr(hwnd);
1197 return infoPtr->iMargin;
1200 static LRESULT
1201 HEADER_SetBitmapMargin(HWND hwnd, WPARAM wParam)
1203 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
1204 INT oldMargin = infoPtr->iMargin;
1206 infoPtr->iMargin = (INT)wParam;
1208 return oldMargin;
1211 static LRESULT
1212 HEADER_SetItemT (HWND hwnd, INT nItem, LPHDITEMW phdi, BOOL bUnicode)
1214 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
1215 HEADER_ITEM *lpItem;
1216 HDITEMW hdNotify;
1217 void *pvScratch;
1219 if (phdi == NULL)
1220 return FALSE;
1221 if ((nItem < 0) || (nItem >= (INT)infoPtr->uNumItem))
1222 return FALSE;
1224 TRACE("[nItem=%d]\n", nItem);
1226 HEADER_CopyHDItemForNotify(infoPtr, &hdNotify, phdi, bUnicode, &pvScratch);
1227 if (HEADER_SendHeaderNotifyT (hwnd, HDN_ITEMCHANGINGW, nItem, phdi->mask, &hdNotify))
1229 if (pvScratch) Free(pvScratch);
1230 return FALSE;
1233 lpItem = &infoPtr->items[nItem];
1234 HEADER_StoreHDItemInHeader(lpItem, phdi, bUnicode);
1236 if (phdi->mask & HDI_ORDER)
1238 INT i, nMin, nMax;
1240 if (lpItem->iOrder < phdi->iOrder)
1242 memmove(&infoPtr->order[lpItem->iOrder],
1243 &infoPtr->order[lpItem->iOrder + 1],
1244 (phdi->iOrder - lpItem->iOrder) * sizeof(INT));
1246 if (phdi->iOrder < lpItem->iOrder)
1248 memmove(&infoPtr->order[phdi->iOrder + 1],
1249 &infoPtr->order[phdi->iOrder],
1250 (lpItem->iOrder - phdi->iOrder) * sizeof(INT));
1252 infoPtr->order[phdi->iOrder] = nItem;
1253 nMin = min(lpItem->iOrder, phdi->iOrder);
1254 nMax = max(lpItem->iOrder, phdi->iOrder);
1255 for (i = nMin; i <= nMax; i++)
1257 infoPtr->items[infoPtr->order[i]].iOrder = infoPtr->order[i];
1261 HEADER_SendHeaderNotifyT (hwnd, HDN_ITEMCHANGEDW, nItem, phdi->mask, &hdNotify);
1263 HEADER_SetItemBounds (hwnd);
1265 InvalidateRect(hwnd, NULL, FALSE);
1267 if (pvScratch != NULL)
1268 Free(pvScratch);
1269 return TRUE;
1272 inline static LRESULT
1273 HEADER_SetUnicodeFormat (HWND hwnd, WPARAM wParam)
1275 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
1276 BOOL bTemp = (infoPtr->nNotifyFormat == NFR_UNICODE);
1278 infoPtr->nNotifyFormat = ((BOOL)wParam ? NFR_UNICODE : NFR_ANSI);
1280 return bTemp;
1284 static LRESULT
1285 HEADER_Create (HWND hwnd, WPARAM wParam, LPARAM lParam)
1287 HEADER_INFO *infoPtr;
1288 TEXTMETRICW tm;
1289 HFONT hOldFont;
1290 HDC hdc;
1292 infoPtr = (HEADER_INFO *)Alloc (sizeof(HEADER_INFO));
1293 SetWindowLongPtrW (hwnd, 0, (DWORD_PTR)infoPtr);
1295 infoPtr->hwndNotify = ((LPCREATESTRUCTA)lParam)->hwndParent;
1296 infoPtr->uNumItem = 0;
1297 infoPtr->hFont = 0;
1298 infoPtr->items = 0;
1299 infoPtr->order = 0;
1300 infoPtr->bRectsValid = FALSE;
1301 infoPtr->hcurArrow = LoadCursorW (0, (LPWSTR)IDC_ARROW);
1302 infoPtr->hcurDivider = LoadCursorW (COMCTL32_hModule, MAKEINTRESOURCEW(IDC_DIVIDER));
1303 infoPtr->hcurDivopen = LoadCursorW (COMCTL32_hModule, MAKEINTRESOURCEW(IDC_DIVIDEROPEN));
1304 infoPtr->bPressed = FALSE;
1305 infoPtr->bTracking = FALSE;
1306 infoPtr->iMoveItem = 0;
1307 infoPtr->himl = 0;
1308 infoPtr->iHotItem = -1;
1309 infoPtr->iMargin = 3*GetSystemMetrics(SM_CXEDGE);
1310 infoPtr->nNotifyFormat =
1311 SendMessageW (infoPtr->hwndNotify, WM_NOTIFYFORMAT, (WPARAM)hwnd, NF_QUERY);
1313 hdc = GetDC (0);
1314 hOldFont = SelectObject (hdc, GetStockObject (SYSTEM_FONT));
1315 GetTextMetricsW (hdc, &tm);
1316 infoPtr->nHeight = tm.tmHeight + VERT_BORDER;
1317 SelectObject (hdc, hOldFont);
1318 ReleaseDC (0, hdc);
1320 OpenThemeData(hwnd, themeClass);
1322 return 0;
1326 static LRESULT
1327 HEADER_Destroy (HWND hwnd, WPARAM wParam, LPARAM lParam)
1329 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
1330 HEADER_ITEM *lpItem;
1331 INT nItem;
1332 HTHEME theme;
1334 if (infoPtr->items) {
1335 lpItem = infoPtr->items;
1336 for (nItem = 0; nItem < infoPtr->uNumItem; nItem++, lpItem++) {
1337 HEADER_DisposeItem(lpItem);
1339 Free (infoPtr->items);
1342 if (infoPtr->order)
1343 Free(infoPtr->order);
1345 if (infoPtr->himl)
1346 ImageList_Destroy (infoPtr->himl);
1348 SetWindowLongPtrW (hwnd, 0, 0);
1349 Free (infoPtr);
1351 theme = GetWindowTheme(hwnd);
1352 CloseThemeData(theme);
1353 return 0;
1357 static inline LRESULT
1358 HEADER_GetFont (HWND hwnd)
1360 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
1362 return (LRESULT)infoPtr->hFont;
1366 static LRESULT
1367 HEADER_LButtonDblClk (HWND hwnd, WPARAM wParam, LPARAM lParam)
1369 POINT pt;
1370 UINT flags;
1371 INT nItem;
1373 pt.x = (INT)LOWORD(lParam);
1374 pt.y = (INT)HIWORD(lParam);
1375 HEADER_InternalHitTest (hwnd, &pt, &flags, &nItem);
1377 if ((GetWindowLongW (hwnd, GWL_STYLE) & HDS_BUTTONS) && (flags == HHT_ONHEADER))
1378 HEADER_SendHeaderNotifyT (hwnd, HDN_ITEMDBLCLICKW, nItem, 0, NULL);
1379 else if ((flags == HHT_ONDIVIDER) || (flags == HHT_ONDIVOPEN))
1380 HEADER_SendHeaderNotifyT (hwnd, HDN_DIVIDERDBLCLICKW, nItem, 0, NULL);
1382 return 0;
1386 static LRESULT
1387 HEADER_LButtonDown (HWND hwnd, WPARAM wParam, LPARAM lParam)
1389 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
1390 DWORD dwStyle = GetWindowLongW (hwnd, GWL_STYLE);
1391 POINT pt;
1392 UINT flags;
1393 INT nItem;
1394 HDC hdc;
1396 pt.x = (INT)LOWORD(lParam);
1397 pt.y = (INT)HIWORD(lParam);
1398 HEADER_InternalHitTest (hwnd, &pt, &flags, &nItem);
1400 if ((dwStyle & HDS_BUTTONS) && (flags == HHT_ONHEADER)) {
1401 SetCapture (hwnd);
1402 infoPtr->bCaptured = TRUE;
1403 infoPtr->bPressed = TRUE;
1404 infoPtr->iMoveItem = nItem;
1406 infoPtr->items[nItem].bDown = TRUE;
1408 /* Send WM_CUSTOMDRAW */
1409 hdc = GetDC (hwnd);
1410 HEADER_RefreshItem (hwnd, hdc, nItem);
1411 ReleaseDC (hwnd, hdc);
1413 TRACE("Pressed item %d!\n", nItem);
1415 else if ((flags == HHT_ONDIVIDER) || (flags == HHT_ONDIVOPEN)) {
1416 if (!(HEADER_SendHeaderNotifyT (hwnd, HDN_BEGINTRACKW, nItem, 0, NULL))) {
1417 SetCapture (hwnd);
1418 infoPtr->bCaptured = TRUE;
1419 infoPtr->bTracking = TRUE;
1420 infoPtr->iMoveItem = nItem;
1421 infoPtr->nOldWidth = infoPtr->items[nItem].cxy;
1422 infoPtr->xTrackOffset = infoPtr->items[nItem].rect.right - pt.x;
1424 if (!(dwStyle & HDS_FULLDRAG)) {
1425 infoPtr->xOldTrack = infoPtr->items[nItem].rect.right;
1426 hdc = GetDC (hwnd);
1427 HEADER_DrawTrackLine (hwnd, hdc, infoPtr->xOldTrack);
1428 ReleaseDC (hwnd, hdc);
1431 TRACE("Begin tracking item %d!\n", nItem);
1435 return 0;
1439 static LRESULT
1440 HEADER_LButtonUp (HWND hwnd, WPARAM wParam, LPARAM lParam)
1442 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
1443 DWORD dwStyle = GetWindowLongW (hwnd, GWL_STYLE);
1444 POINT pt;
1445 UINT flags;
1446 INT nItem, nWidth;
1447 HDC hdc;
1449 pt.x = (INT)(SHORT)LOWORD(lParam);
1450 pt.y = (INT)(SHORT)HIWORD(lParam);
1451 HEADER_InternalHitTest (hwnd, &pt, &flags, &nItem);
1453 if (infoPtr->bPressed) {
1454 if ((nItem == infoPtr->iMoveItem) && (flags == HHT_ONHEADER)) {
1455 infoPtr->items[infoPtr->iMoveItem].bDown = FALSE;
1456 hdc = GetDC (hwnd);
1457 HEADER_RefreshItem (hwnd, hdc, infoPtr->iMoveItem);
1458 ReleaseDC (hwnd, hdc);
1460 HEADER_SendClickNotify (hwnd, HDN_ITEMCLICKA, infoPtr->iMoveItem);
1462 else if (flags == HHT_ONHEADER)
1464 HEADER_ITEM *lpItem;
1465 INT newindex = HEADER_IndexToOrder(hwnd,nItem);
1466 INT oldindex = HEADER_IndexToOrder(hwnd,infoPtr->iMoveItem);
1468 TRACE("Exchanging [index:order] [%d:%d] [%d:%d]\n",
1469 infoPtr->iMoveItem,oldindex,nItem,newindex);
1470 lpItem= &infoPtr->items[nItem];
1471 lpItem->iOrder=oldindex;
1473 lpItem= &infoPtr->items[infoPtr->iMoveItem];
1474 lpItem->iOrder = newindex;
1476 infoPtr->order[oldindex] = nItem;
1477 infoPtr->order[newindex] = infoPtr->iMoveItem;
1479 infoPtr->bRectsValid = FALSE;
1480 InvalidateRect(hwnd, NULL, FALSE);
1481 /* FIXME: Should some WM_NOTIFY be sent */
1484 TRACE("Released item %d!\n", infoPtr->iMoveItem);
1485 infoPtr->bPressed = FALSE;
1487 else if (infoPtr->bTracking) {
1488 TRACE("End tracking item %d!\n", infoPtr->iMoveItem);
1489 infoPtr->bTracking = FALSE;
1491 HEADER_SendHeaderNotifyT (hwnd, HDN_ENDTRACKW, infoPtr->iMoveItem, HDI_WIDTH, NULL);
1493 if (!(dwStyle & HDS_FULLDRAG)) {
1494 hdc = GetDC (hwnd);
1495 HEADER_DrawTrackLine (hwnd, hdc, infoPtr->xOldTrack);
1496 ReleaseDC (hwnd, hdc);
1499 if (HEADER_SendHeaderNotifyT(hwnd, HDN_ITEMCHANGINGW, infoPtr->iMoveItem, HDI_WIDTH, NULL))
1501 infoPtr->items[infoPtr->iMoveItem].cxy = infoPtr->nOldWidth;
1503 else {
1504 nWidth = pt.x - infoPtr->items[infoPtr->iMoveItem].rect.left + infoPtr->xTrackOffset;
1505 if (nWidth < 0)
1506 nWidth = 0;
1507 infoPtr->items[infoPtr->iMoveItem].cxy = nWidth;
1510 HEADER_SetItemBounds (hwnd);
1511 InvalidateRect(hwnd, NULL, TRUE);
1512 HEADER_SendHeaderNotifyT(hwnd, HDN_ITEMCHANGEDW, infoPtr->iMoveItem, HDI_WIDTH, NULL);
1515 if (infoPtr->bCaptured) {
1516 infoPtr->bCaptured = FALSE;
1517 ReleaseCapture ();
1518 HEADER_SendSimpleNotify (hwnd, NM_RELEASEDCAPTURE);
1521 return 0;
1525 static LRESULT
1526 HEADER_NotifyFormat (HWND hwnd, WPARAM wParam, LPARAM lParam)
1528 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
1530 switch (lParam)
1532 case NF_QUERY:
1533 return infoPtr->nNotifyFormat;
1535 case NF_REQUERY:
1536 infoPtr->nNotifyFormat =
1537 SendMessageW ((HWND)wParam, WM_NOTIFYFORMAT,
1538 (WPARAM)hwnd, (LPARAM)NF_QUERY);
1539 return infoPtr->nNotifyFormat;
1542 return 0;
1546 static LRESULT
1547 HEADER_MouseLeave (HWND hwnd, WPARAM wParam, LPARAM lParam)
1549 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
1550 /* Reset hot-tracked item when mouse leaves control. */
1551 INT oldHotItem = infoPtr->iHotItem;
1552 HDC hdc = GetDC (hwnd);
1554 infoPtr->iHotItem = -1;
1555 if (oldHotItem != -1) HEADER_RefreshItem (hwnd, hdc, oldHotItem);
1556 ReleaseDC (hwnd, hdc);
1558 return 0;
1562 static LRESULT
1563 HEADER_MouseMove (HWND hwnd, WPARAM wParam, LPARAM lParam)
1565 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
1566 DWORD dwStyle = GetWindowLongW (hwnd, GWL_STYLE);
1567 POINT pt;
1568 UINT flags;
1569 INT nItem, nWidth;
1570 HDC hdc;
1571 /* With theming, hottracking is always enabled */
1572 BOOL hotTrackEnabled =
1573 ((dwStyle & HDS_BUTTONS) && (dwStyle & HDS_HOTTRACK))
1574 || (GetWindowTheme (hwnd) != NULL);
1575 INT oldHotItem = infoPtr->iHotItem;
1577 pt.x = (INT)(SHORT)LOWORD(lParam);
1578 pt.y = (INT)(SHORT)HIWORD(lParam);
1579 HEADER_InternalHitTest (hwnd, &pt, &flags, &nItem);
1581 if (hotTrackEnabled) {
1582 if (flags & (HHT_ONHEADER | HHT_ONDIVIDER | HHT_ONDIVOPEN))
1583 infoPtr->iHotItem = nItem;
1584 else
1585 infoPtr->iHotItem = -1;
1588 if (infoPtr->bCaptured) {
1589 if (infoPtr->bPressed) {
1590 BOOL oldState = infoPtr->items[infoPtr->iMoveItem].bDown;
1591 if ((nItem == infoPtr->iMoveItem) && (flags == HHT_ONHEADER))
1592 infoPtr->items[infoPtr->iMoveItem].bDown = TRUE;
1593 else
1594 infoPtr->items[infoPtr->iMoveItem].bDown = FALSE;
1595 if (oldState != infoPtr->items[infoPtr->iMoveItem].bDown) {
1596 hdc = GetDC (hwnd);
1597 HEADER_RefreshItem (hwnd, hdc, infoPtr->iMoveItem);
1598 ReleaseDC (hwnd, hdc);
1601 TRACE("Moving pressed item %d!\n", infoPtr->iMoveItem);
1603 else if (infoPtr->bTracking) {
1604 if (dwStyle & HDS_FULLDRAG) {
1605 if (!HEADER_SendHeaderNotifyT (hwnd, HDN_ITEMCHANGINGW, infoPtr->iMoveItem, HDI_WIDTH, NULL))
1607 nWidth = pt.x - infoPtr->items[infoPtr->iMoveItem].rect.left + infoPtr->xTrackOffset;
1608 if (nWidth < 0)
1609 nWidth = 0;
1610 infoPtr->items[infoPtr->iMoveItem].cxy = nWidth;
1611 HEADER_SendHeaderNotifyT(hwnd, HDN_ITEMCHANGEDW, infoPtr->iMoveItem, HDI_WIDTH, NULL);
1613 HEADER_SetItemBounds (hwnd);
1614 InvalidateRect(hwnd, NULL, FALSE);
1616 else {
1617 hdc = GetDC (hwnd);
1618 HEADER_DrawTrackLine (hwnd, hdc, infoPtr->xOldTrack);
1619 infoPtr->xOldTrack = pt.x + infoPtr->xTrackOffset;
1620 if (infoPtr->xOldTrack < infoPtr->items[infoPtr->iMoveItem].rect.left)
1621 infoPtr->xOldTrack = infoPtr->items[infoPtr->iMoveItem].rect.left;
1622 infoPtr->items[infoPtr->iMoveItem].cxy =
1623 infoPtr->xOldTrack - infoPtr->items[infoPtr->iMoveItem].rect.left;
1624 HEADER_DrawTrackLine (hwnd, hdc, infoPtr->xOldTrack);
1625 ReleaseDC (hwnd, hdc);
1626 HEADER_SendHeaderNotifyT (hwnd, HDN_TRACKW, infoPtr->iMoveItem, HDI_WIDTH, NULL);
1629 TRACE("Tracking item %d!\n", infoPtr->iMoveItem);
1633 if (hotTrackEnabled) {
1634 TRACKMOUSEEVENT tme;
1635 if (oldHotItem != infoPtr->iHotItem) {
1636 hdc = GetDC (hwnd);
1637 if (oldHotItem != -1) HEADER_RefreshItem (hwnd, hdc, oldHotItem);
1638 if (infoPtr->iHotItem != -1) HEADER_RefreshItem (hwnd, hdc, infoPtr->iHotItem);
1639 ReleaseDC (hwnd, hdc);
1641 tme.cbSize = sizeof( tme );
1642 tme.dwFlags = TME_LEAVE;
1643 tme.hwndTrack = hwnd;
1644 TrackMouseEvent( &tme );
1647 return 0;
1651 static LRESULT
1652 HEADER_Paint (HWND hwnd, WPARAM wParam)
1654 HDC hdc;
1655 PAINTSTRUCT ps;
1657 hdc = wParam==0 ? BeginPaint (hwnd, &ps) : (HDC)wParam;
1658 HEADER_Refresh (hwnd, hdc);
1659 if(!wParam)
1660 EndPaint (hwnd, &ps);
1661 return 0;
1665 static LRESULT
1666 HEADER_RButtonUp (HWND hwnd, WPARAM wParam, LPARAM lParam)
1668 BOOL bRet;
1669 POINT pt;
1671 pt.x = LOWORD(lParam);
1672 pt.y = HIWORD(lParam);
1674 /* Send a Notify message */
1675 bRet = HEADER_SendSimpleNotify (hwnd, NM_RCLICK);
1677 /* Change to screen coordinate for WM_CONTEXTMENU */
1678 ClientToScreen(hwnd, &pt);
1680 /* Send a WM_CONTEXTMENU message in response to the RBUTTONUP */
1681 SendMessageW( hwnd, WM_CONTEXTMENU, (WPARAM) hwnd, MAKELPARAM(pt.x, pt.y));
1683 return bRet;
1687 static LRESULT
1688 HEADER_SetCursor (HWND hwnd, WPARAM wParam, LPARAM lParam)
1690 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
1691 POINT pt;
1692 UINT flags;
1693 INT nItem;
1695 TRACE("code=0x%X id=0x%X\n", LOWORD(lParam), HIWORD(lParam));
1697 GetCursorPos (&pt);
1698 ScreenToClient (hwnd, &pt);
1700 HEADER_InternalHitTest (hwnd, &pt, &flags, &nItem);
1702 if (flags == HHT_ONDIVIDER)
1703 SetCursor (infoPtr->hcurDivider);
1704 else if (flags == HHT_ONDIVOPEN)
1705 SetCursor (infoPtr->hcurDivopen);
1706 else
1707 SetCursor (infoPtr->hcurArrow);
1709 return 0;
1713 static LRESULT
1714 HEADER_SetFont (HWND hwnd, WPARAM wParam, LPARAM lParam)
1716 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
1717 TEXTMETRICW tm;
1718 HFONT hFont, hOldFont;
1719 HDC hdc;
1721 infoPtr->hFont = (HFONT)wParam;
1723 hFont = infoPtr->hFont ? infoPtr->hFont : GetStockObject (SYSTEM_FONT);
1725 hdc = GetDC (0);
1726 hOldFont = SelectObject (hdc, hFont);
1727 GetTextMetricsW (hdc, &tm);
1728 infoPtr->nHeight = tm.tmHeight + VERT_BORDER;
1729 SelectObject (hdc, hOldFont);
1730 ReleaseDC (0, hdc);
1732 infoPtr->bRectsValid = FALSE;
1734 if (lParam) {
1735 InvalidateRect(hwnd, NULL, FALSE);
1738 return 0;
1741 /* Update the theme handle after a theme change */
1742 static LRESULT HEADER_ThemeChanged(HWND hwnd)
1744 HTHEME theme = GetWindowTheme(hwnd);
1745 CloseThemeData(theme);
1746 OpenThemeData(hwnd, themeClass);
1747 InvalidateRect(hwnd, NULL, FALSE);
1748 return 0;
1752 static LRESULT WINAPI
1753 HEADER_WindowProc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
1755 TRACE("hwnd=%p msg=%x wparam=%x lParam=%lx\n", hwnd, msg, wParam, lParam);
1756 if (!HEADER_GetInfoPtr (hwnd) && (msg != WM_CREATE))
1757 return DefWindowProcW (hwnd, msg, wParam, lParam);
1758 switch (msg) {
1759 /* case HDM_CLEARFILTER: */
1761 case HDM_CREATEDRAGIMAGE:
1762 return HEADER_CreateDragImage (hwnd, wParam);
1764 case HDM_DELETEITEM:
1765 return HEADER_DeleteItem (hwnd, wParam);
1767 /* case HDM_EDITFILTER: */
1769 case HDM_GETBITMAPMARGIN:
1770 return HEADER_GetBitmapMargin(hwnd);
1772 case HDM_GETIMAGELIST:
1773 return HEADER_GetImageList (hwnd);
1775 case HDM_GETITEMA:
1776 case HDM_GETITEMW:
1777 return HEADER_GetItemT (hwnd, (INT)wParam, (LPHDITEMW)lParam, msg == HDM_GETITEMW);
1779 case HDM_GETITEMCOUNT:
1780 return HEADER_GetItemCount (hwnd);
1782 case HDM_GETITEMRECT:
1783 return HEADER_GetItemRect (hwnd, wParam, lParam);
1785 case HDM_GETORDERARRAY:
1786 return HEADER_GetOrderArray(hwnd, wParam, lParam);
1788 case HDM_GETUNICODEFORMAT:
1789 return HEADER_GetUnicodeFormat (hwnd);
1791 case HDM_HITTEST:
1792 return HEADER_HitTest (hwnd, wParam, lParam);
1794 case HDM_INSERTITEMA:
1795 case HDM_INSERTITEMW:
1796 return HEADER_InsertItemT (hwnd, (INT)wParam, (LPHDITEMW)lParam, msg == HDM_INSERTITEMW);
1798 case HDM_LAYOUT:
1799 return HEADER_Layout (hwnd, wParam, lParam);
1801 case HDM_ORDERTOINDEX:
1802 return HEADER_OrderToIndex(hwnd, wParam);
1804 case HDM_SETBITMAPMARGIN:
1805 return HEADER_SetBitmapMargin(hwnd, wParam);
1807 /* case HDM_SETFILTERCHANGETIMEOUT: */
1809 /* case HDM_SETHOTDIVIDER: */
1811 case HDM_SETIMAGELIST:
1812 return HEADER_SetImageList (hwnd, (HIMAGELIST)lParam);
1814 case HDM_SETITEMA:
1815 case HDM_SETITEMW:
1816 return HEADER_SetItemT (hwnd, (INT)wParam, (LPHDITEMW)lParam, msg == HDM_SETITEMW);
1818 case HDM_SETORDERARRAY:
1819 return HEADER_SetOrderArray(hwnd, wParam, lParam);
1821 case HDM_SETUNICODEFORMAT:
1822 return HEADER_SetUnicodeFormat (hwnd, wParam);
1824 case WM_CREATE:
1825 return HEADER_Create (hwnd, wParam, lParam);
1827 case WM_DESTROY:
1828 return HEADER_Destroy (hwnd, wParam, lParam);
1830 case WM_ERASEBKGND:
1831 return 1;
1833 case WM_GETDLGCODE:
1834 return DLGC_WANTTAB | DLGC_WANTARROWS;
1836 case WM_GETFONT:
1837 return HEADER_GetFont (hwnd);
1839 case WM_LBUTTONDBLCLK:
1840 return HEADER_LButtonDblClk (hwnd, wParam, lParam);
1842 case WM_LBUTTONDOWN:
1843 return HEADER_LButtonDown (hwnd, wParam, lParam);
1845 case WM_LBUTTONUP:
1846 return HEADER_LButtonUp (hwnd, wParam, lParam);
1848 case WM_MOUSELEAVE:
1849 return HEADER_MouseLeave (hwnd, wParam, lParam);
1851 case WM_MOUSEMOVE:
1852 return HEADER_MouseMove (hwnd, wParam, lParam);
1854 case WM_NOTIFYFORMAT:
1855 return HEADER_NotifyFormat (hwnd, wParam, lParam);
1857 case WM_SIZE:
1858 return HEADER_Size (hwnd, wParam);
1860 case WM_THEMECHANGED:
1861 return HEADER_ThemeChanged (hwnd);
1863 case WM_PRINTCLIENT:
1864 case WM_PAINT:
1865 return HEADER_Paint (hwnd, wParam);
1867 case WM_RBUTTONUP:
1868 return HEADER_RButtonUp (hwnd, wParam, lParam);
1870 case WM_SETCURSOR:
1871 return HEADER_SetCursor (hwnd, wParam, lParam);
1873 case WM_SETFONT:
1874 return HEADER_SetFont (hwnd, wParam, lParam);
1876 default:
1877 if ((msg >= WM_USER) && (msg < WM_APP))
1878 ERR("unknown msg %04x wp=%04x lp=%08lx\n",
1879 msg, wParam, lParam );
1880 return DefWindowProcA (hwnd, msg, wParam, lParam);
1885 VOID
1886 HEADER_Register (void)
1888 WNDCLASSW wndClass;
1890 ZeroMemory (&wndClass, sizeof(WNDCLASSW));
1891 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS;
1892 wndClass.lpfnWndProc = HEADER_WindowProc;
1893 wndClass.cbClsExtra = 0;
1894 wndClass.cbWndExtra = sizeof(HEADER_INFO *);
1895 wndClass.hCursor = LoadCursorW (0, (LPWSTR)IDC_ARROW);
1896 wndClass.lpszClassName = WC_HEADERW;
1898 RegisterClassW (&wndClass);
1902 VOID
1903 HEADER_Unregister (void)
1905 UnregisterClassW (WC_HEADERW, NULL);