Rewrote item layouting - new code fixes bitmap/image position for
[wine/wine-kai.git] / dlls / comctl32 / header.c
blob67e7a40dad62fd0e413837b9cf48a3c5437d05ae
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 "wine/debug.h"
48 WINE_DEFAULT_DEBUG_CHANNEL(header);
50 typedef struct
52 INT cxy;
53 HBITMAP hbm;
54 LPWSTR pszText;
55 INT fmt;
56 LPARAM lParam;
57 INT iImage;
58 INT iOrder; /* see documentation of HD_ITEM */
60 BOOL bDown; /* is item pressed? (used for drawing) */
61 RECT rect; /* bounding rectangle of the item */
62 } HEADER_ITEM;
65 typedef struct
67 HWND hwndNotify; /* Owner window to send notifications to */
68 INT nNotifyFormat; /* format used for WM_NOTIFY messages */
69 UINT uNumItem; /* number of items (columns) */
70 INT nHeight; /* height of the header (pixels) */
71 HFONT hFont; /* handle to the current font */
72 HCURSOR hcurArrow; /* handle to the arrow cursor */
73 HCURSOR hcurDivider; /* handle to a cursor (used over dividers) <-|-> */
74 HCURSOR hcurDivopen; /* handle to a cursor (used over dividers) <-||-> */
75 BOOL bCaptured; /* Is the mouse captured? */
76 BOOL bPressed; /* Is a header item pressed (down)? */
77 BOOL bTracking; /* Is in tracking mode? */
78 BOOL bUnicode; /* Unicode flag */
79 INT iMoveItem; /* index of tracked item. (Tracking mode) */
80 INT xTrackOffset; /* distance between the right side of the tracked item and the cursor */
81 INT xOldTrack; /* track offset (see above) after the last WM_MOUSEMOVE */
82 INT nOldWidth; /* width of a sizing item after the last WM_MOUSEMOVE */
83 INT iHotItem; /* index of hot item (cursor is over this item) */
84 INT iMargin; /* width of the margin that surrounds a bitmap */
86 HIMAGELIST himl; /* handle to an image list (may be 0) */
87 HEADER_ITEM *items; /* pointer to array of HEADER_ITEM's */
88 BOOL bRectsValid; /* validity flag for bounding rectangles */
89 } HEADER_INFO;
92 #define VERT_BORDER 3
93 #define DIVIDER_WIDTH 10
95 #define HEADER_GetInfoPtr(hwnd) ((HEADER_INFO *)GetWindowLongPtrW(hwnd,0))
98 inline static LRESULT
99 HEADER_IndexToOrder (HWND hwnd, INT iItem)
101 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
102 HEADER_ITEM *lpItem = &infoPtr->items[iItem];
103 return lpItem->iOrder;
107 static INT
108 HEADER_OrderToIndex(HWND hwnd, WPARAM wParam)
110 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
111 INT iorder = (INT)wParam;
112 UINT i;
114 if ((iorder <0) || iorder >infoPtr->uNumItem)
115 return iorder;
116 for (i=0; i<infoPtr->uNumItem; i++)
117 if (HEADER_IndexToOrder(hwnd,i) == iorder)
118 return i;
119 return iorder;
122 static void
123 HEADER_SetItemBounds (HWND hwnd)
125 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
126 HEADER_ITEM *phdi;
127 RECT rect;
128 unsigned int i;
129 int x;
131 infoPtr->bRectsValid = TRUE;
133 if (infoPtr->uNumItem == 0)
134 return;
136 GetClientRect (hwnd, &rect);
138 x = rect.left;
139 for (i = 0; i < infoPtr->uNumItem; i++) {
140 phdi = &infoPtr->items[HEADER_OrderToIndex(hwnd,i)];
141 phdi->rect.top = rect.top;
142 phdi->rect.bottom = rect.bottom;
143 phdi->rect.left = x;
144 phdi->rect.right = phdi->rect.left + ((phdi->cxy>0)?phdi->cxy:0);
145 x = phdi->rect.right;
149 static LRESULT
150 HEADER_Size (HWND hwnd, WPARAM wParam)
152 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
154 infoPtr->bRectsValid = FALSE;
156 return 0;
160 static INT
161 HEADER_DrawItem (HWND hwnd, HDC hdc, INT iItem, BOOL bHotTrack)
163 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
164 HEADER_ITEM *phdi = &infoPtr->items[iItem];
165 RECT r;
166 INT oldBkMode, cxEdge = GetSystemMetrics(SM_CXEDGE);
168 TRACE("DrawItem(iItem %d bHotTrack %d unicode flag %d)\n", iItem, bHotTrack, infoPtr->bUnicode);
170 if (!infoPtr->bRectsValid)
171 HEADER_SetItemBounds(hwnd);
173 r = phdi->rect;
174 if (r.right - r.left == 0)
175 return phdi->rect.right;
177 if (GetWindowLongW (hwnd, GWL_STYLE) & HDS_BUTTONS) {
178 if (phdi->bDown) {
179 DrawEdge (hdc, &r, BDR_RAISEDOUTER,
180 BF_RECT | BF_FLAT | BF_MIDDLE | BF_ADJUST);
181 r.left += 2;
182 r.top += 2;
184 else
185 DrawEdge (hdc, &r, EDGE_RAISED,
186 BF_RECT | BF_SOFT | BF_MIDDLE | BF_ADJUST);
188 else
189 DrawEdge (hdc, &r, EDGE_ETCHED, BF_BOTTOM | BF_RIGHT | BF_ADJUST);
191 r.left -= cxEdge;
192 r.right += cxEdge;
194 if (phdi->fmt & HDF_OWNERDRAW) {
195 DRAWITEMSTRUCT dis;
196 dis.CtlType = ODT_HEADER;
197 dis.CtlID = GetWindowLongPtrW (hwnd, GWLP_ID);
198 dis.itemID = iItem;
199 dis.itemAction = ODA_DRAWENTIRE;
200 dis.itemState = phdi->bDown ? ODS_SELECTED : 0;
201 dis.hwndItem = hwnd;
202 dis.hDC = hdc;
203 dis.rcItem = r;
204 dis.itemData = phdi->lParam;
205 oldBkMode = SetBkMode(hdc, TRANSPARENT);
206 SendMessageW (infoPtr->hwndNotify, WM_DRAWITEM,
207 (WPARAM)dis.CtlID, (LPARAM)&dis);
208 if (oldBkMode != TRANSPARENT)
209 SetBkMode(hdc, oldBkMode);
211 else {
212 UINT rw, rh, /* width and height of r */
213 *x = NULL, *w = NULL; /* x and width of the pic (bmp or img) which is part of cnt */
214 /* cnt,txt,img,bmp */
215 UINT cx, tx, ix, bx,
216 cw, tw, iw, bw;
217 BITMAP bmp;
219 cw = tw = iw = bw = 0;
220 rw = r.right - r.left;
221 rh = r.bottom - r.top;
223 if (phdi->fmt & HDF_STRING) {
224 RECT textRect;
226 DrawTextW (hdc, phdi->pszText, -1,
227 &textRect, DT_LEFT|DT_VCENTER|DT_SINGLELINE|DT_CALCRECT);
228 cw = textRect.right - textRect.left + 2 * infoPtr->iMargin;
231 if ((phdi->fmt & HDF_IMAGE) && (infoPtr->himl)) {
232 iw = infoPtr->himl->cx + 2 * infoPtr->iMargin;
233 x = &ix;
234 w = &iw;
237 if ((phdi->fmt & HDF_BITMAP) && (phdi->hbm)) {
238 GetObjectW (phdi->hbm, sizeof(BITMAP), (LPVOID)&bmp);
239 bw = bmp.bmWidth + 2 * infoPtr->iMargin;
240 if (!iw) {
241 x = &bx;
242 w = &bw;
246 if (bw || iw)
247 cw += *w;
249 /* align cx using the unclipped cw */
250 if ((phdi->fmt & HDF_JUSTIFYMASK) == HDF_LEFT)
251 cx = r.left;
252 else if ((phdi->fmt & HDF_JUSTIFYMASK) == HDF_CENTER)
253 cx = r.left + rw / 2 - cw / 2;
254 else /* HDF_RIGHT */
255 cx = r.right - cw;
257 /* clip cx & cw */
258 if (cx < r.left)
259 cx = r.left;
260 if (cx + cw > r.right)
261 cw = r.right - cx;
263 tx = cx + infoPtr->iMargin;
264 /* since cw might have changed we have to recalculate tw */
265 tw = cw - infoPtr->iMargin * 2;
267 if (iw || bw) {
268 tw -= *w;
269 if (phdi->fmt & HDF_BITMAP_ON_RIGHT) {
270 /* put pic behind text */
271 *x = cx + tw + infoPtr->iMargin * 3;
272 } else {
273 *x = cx + infoPtr->iMargin;
274 /* move text behind pic */
275 tx += *w;
279 if (iw && bw) {
280 /* since we're done with the layout we can
281 now calculate the position of bmp which
282 has no influence on alignment and layout
283 because of img */
284 if ((phdi->fmt & HDF_JUSTIFYMASK) == HDF_RIGHT)
285 bx = cx - bw + infoPtr->iMargin;
286 else
287 bx = cx + cw + infoPtr->iMargin;
290 if (iw || bw) {
291 HDC hClipDC = GetDC(hwnd);
292 HRGN hClipRgn = CreateRectRgn(r.left, r.top, r.right, r.bottom);
293 SelectClipRgn(hClipDC, hClipRgn);
295 if (bw) {
296 HDC hdcBitmap = CreateCompatibleDC (hClipDC);
297 SelectObject (hdcBitmap, phdi->hbm);
298 BitBlt (hClipDC, bx, r.top + ((INT)rh - bmp.bmHeight) / 2,
299 bmp.bmWidth, bmp.bmHeight, hdcBitmap, 0, 0, SRCCOPY);
300 DeleteDC (hdcBitmap);
303 if (iw) {
304 ImageList_DrawEx (infoPtr->himl, phdi->iImage, hClipDC,
305 ix, r.top + ((INT)rh - infoPtr->himl->cy) / 2,
306 infoPtr->himl->cx, infoPtr->himl->cy, CLR_DEFAULT, CLR_DEFAULT, 0);
309 DeleteObject(hClipRgn);
310 DeleteDC(hClipDC);
313 if (((phdi->fmt & HDF_STRING)
314 || (!(phdi->fmt & (HDF_OWNERDRAW|HDF_STRING|HDF_BITMAP|
315 HDF_BITMAP_ON_RIGHT|HDF_IMAGE)))) /* no explicit format specified? */
316 && (phdi->pszText)) {
317 oldBkMode = SetBkMode(hdc, TRANSPARENT);
318 SetTextColor (hdc, (bHotTrack) ? COLOR_HIGHLIGHT : COLOR_BTNTEXT);
319 r.left = tx;
320 r.right = tx + tw;
321 DrawTextW (hdc, phdi->pszText, -1,
322 &r, DT_LEFT|DT_END_ELLIPSIS|DT_VCENTER|DT_SINGLELINE);
323 if (oldBkMode != TRANSPARENT)
324 SetBkMode(hdc, oldBkMode);
326 }/*Ownerdrawn*/
328 return phdi->rect.right;
332 static void
333 HEADER_Refresh (HWND hwnd, HDC hdc)
335 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
336 HFONT hFont, hOldFont;
337 RECT rect;
338 HBRUSH hbrBk;
339 UINT i;
340 INT x;
342 /* get rect for the bar, adjusted for the border */
343 GetClientRect (hwnd, &rect);
345 hFont = infoPtr->hFont ? infoPtr->hFont : GetStockObject (SYSTEM_FONT);
346 hOldFont = SelectObject (hdc, hFont);
348 /* draw Background */
349 hbrBk = GetSysColorBrush(COLOR_3DFACE);
350 FillRect(hdc, &rect, hbrBk);
352 x = rect.left;
353 for (i = 0; i < infoPtr->uNumItem; i++) {
354 x = HEADER_DrawItem (hwnd, hdc, HEADER_OrderToIndex(hwnd,i), FALSE);
357 if ((x <= rect.right) && (infoPtr->uNumItem > 0)) {
358 rect.left = x;
359 if (GetWindowLongW (hwnd, GWL_STYLE) & HDS_BUTTONS)
360 DrawEdge (hdc, &rect, EDGE_RAISED, BF_TOP|BF_LEFT|BF_BOTTOM|BF_SOFT);
361 else
362 DrawEdge (hdc, &rect, EDGE_ETCHED, BF_BOTTOM);
365 SelectObject (hdc, hOldFont);
369 static void
370 HEADER_RefreshItem (HWND hwnd, HDC hdc, INT iItem)
372 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
373 HFONT hFont, hOldFont;
375 hFont = infoPtr->hFont ? infoPtr->hFont : GetStockObject (SYSTEM_FONT);
376 hOldFont = SelectObject (hdc, hFont);
377 HEADER_DrawItem (hwnd, hdc, iItem, FALSE);
378 SelectObject (hdc, hOldFont);
382 static void
383 HEADER_InternalHitTest (HWND hwnd, LPPOINT lpPt, UINT *pFlags, INT *pItem)
385 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
386 RECT rect, rcTest;
387 UINT iCount;
388 INT width;
389 BOOL bNoWidth;
391 GetClientRect (hwnd, &rect);
393 *pFlags = 0;
394 bNoWidth = FALSE;
395 if (PtInRect (&rect, *lpPt))
397 if (infoPtr->uNumItem == 0) {
398 *pFlags |= HHT_NOWHERE;
399 *pItem = 1;
400 TRACE("NOWHERE\n");
401 return;
403 else {
404 /* somewhere inside */
405 for (iCount = 0; iCount < infoPtr->uNumItem; iCount++) {
406 rect = infoPtr->items[iCount].rect;
407 width = rect.right - rect.left;
408 if (width == 0) {
409 bNoWidth = TRUE;
410 continue;
412 if (PtInRect (&rect, *lpPt)) {
413 if (width <= 2 * DIVIDER_WIDTH) {
414 *pFlags |= HHT_ONHEADER;
415 *pItem = iCount;
416 TRACE("ON HEADER %d\n", iCount);
417 return;
419 if (iCount > 0) {
420 rcTest = rect;
421 rcTest.right = rcTest.left + DIVIDER_WIDTH;
422 if (PtInRect (&rcTest, *lpPt)) {
423 if (bNoWidth) {
424 *pFlags |= HHT_ONDIVOPEN;
425 *pItem = iCount - 1;
426 TRACE("ON DIVOPEN %d\n", *pItem);
427 return;
429 else {
430 *pFlags |= HHT_ONDIVIDER;
431 *pItem = iCount - 1;
432 TRACE("ON DIVIDER %d\n", *pItem);
433 return;
437 rcTest = rect;
438 rcTest.left = rcTest.right - DIVIDER_WIDTH;
439 if (PtInRect (&rcTest, *lpPt)) {
440 *pFlags |= HHT_ONDIVIDER;
441 *pItem = iCount;
442 TRACE("ON DIVIDER %d\n", *pItem);
443 return;
446 *pFlags |= HHT_ONHEADER;
447 *pItem = iCount;
448 TRACE("ON HEADER %d\n", iCount);
449 return;
453 /* check for last divider part (on nowhere) */
454 rect = infoPtr->items[infoPtr->uNumItem-1].rect;
455 rect.left = rect.right;
456 rect.right += DIVIDER_WIDTH;
457 if (PtInRect (&rect, *lpPt)) {
458 if (bNoWidth) {
459 *pFlags |= HHT_ONDIVOPEN;
460 *pItem = infoPtr->uNumItem - 1;
461 TRACE("ON DIVOPEN %d\n", *pItem);
462 return;
464 else {
465 *pFlags |= HHT_ONDIVIDER;
466 *pItem = infoPtr->uNumItem-1;
467 TRACE("ON DIVIDER %d\n", *pItem);
468 return;
472 *pFlags |= HHT_NOWHERE;
473 *pItem = 1;
474 TRACE("NOWHERE\n");
475 return;
478 else {
479 if (lpPt->x < rect.left) {
480 TRACE("TO LEFT\n");
481 *pFlags |= HHT_TOLEFT;
483 else if (lpPt->x > rect.right) {
484 TRACE("TO RIGHT\n");
485 *pFlags |= HHT_TORIGHT;
488 if (lpPt->y < rect.top) {
489 TRACE("ABOVE\n");
490 *pFlags |= HHT_ABOVE;
492 else if (lpPt->y > rect.bottom) {
493 TRACE("BELOW\n");
494 *pFlags |= HHT_BELOW;
498 *pItem = 1;
499 TRACE("flags=0x%X\n", *pFlags);
500 return;
504 static void
505 HEADER_DrawTrackLine (HWND hwnd, HDC hdc, INT x)
507 RECT rect;
508 HPEN hOldPen;
509 INT oldRop;
511 GetClientRect (hwnd, &rect);
513 hOldPen = SelectObject (hdc, GetStockObject (BLACK_PEN));
514 oldRop = SetROP2 (hdc, R2_XORPEN);
515 MoveToEx (hdc, x, rect.top, NULL);
516 LineTo (hdc, x, rect.bottom);
517 SetROP2 (hdc, oldRop);
518 SelectObject (hdc, hOldPen);
522 static BOOL
523 HEADER_SendSimpleNotify (HWND hwnd, UINT code)
525 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
526 NMHDR nmhdr;
528 nmhdr.hwndFrom = hwnd;
529 nmhdr.idFrom = GetWindowLongPtrW (hwnd, GWLP_ID);
530 nmhdr.code = code;
532 return (BOOL)SendMessageW (infoPtr->hwndNotify, WM_NOTIFY,
533 (WPARAM)nmhdr.idFrom, (LPARAM)&nmhdr);
536 static BOOL
537 HEADER_SendHeaderNotify (HWND hwnd, UINT code, INT iItem, INT mask)
539 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
540 NMHEADERA nmhdr;
541 HDITEMA nmitem;
543 nmhdr.hdr.hwndFrom = hwnd;
544 nmhdr.hdr.idFrom = GetWindowLongPtrW (hwnd, GWLP_ID);
545 nmhdr.hdr.code = code;
546 nmhdr.iItem = iItem;
547 nmhdr.iButton = 0;
548 nmhdr.pitem = &nmitem;
549 nmitem.mask = mask;
550 nmitem.cxy = infoPtr->items[iItem].cxy;
551 nmitem.hbm = infoPtr->items[iItem].hbm;
552 nmitem.pszText = NULL;
553 nmitem.cchTextMax = 0;
554 /* nmitem.pszText = infoPtr->items[iItem].pszText; */
555 /* nmitem.cchTextMax = infoPtr->items[iItem].cchTextMax; */
556 nmitem.fmt = infoPtr->items[iItem].fmt;
557 nmitem.lParam = infoPtr->items[iItem].lParam;
558 nmitem.iOrder = infoPtr->items[iItem].iOrder;
559 nmitem.iImage = infoPtr->items[iItem].iImage;
561 return (BOOL)SendMessageW (infoPtr->hwndNotify, WM_NOTIFY,
562 (WPARAM)nmhdr.hdr.idFrom, (LPARAM)&nmhdr);
566 static BOOL
567 HEADER_SendClickNotify (HWND hwnd, UINT code, INT iItem)
569 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
570 NMHEADERA nmhdr;
572 nmhdr.hdr.hwndFrom = hwnd;
573 nmhdr.hdr.idFrom = GetWindowLongPtrW (hwnd, GWLP_ID);
574 nmhdr.hdr.code = code;
575 nmhdr.iItem = iItem;
576 nmhdr.iButton = 0;
577 nmhdr.pitem = NULL;
579 return (BOOL)SendMessageA (infoPtr->hwndNotify, WM_NOTIFY,
580 (WPARAM)nmhdr.hdr.idFrom, (LPARAM)&nmhdr);
584 static LRESULT
585 HEADER_CreateDragImage (HWND hwnd, WPARAM wParam)
587 FIXME("empty stub!\n");
588 return 0;
592 static LRESULT
593 HEADER_DeleteItem (HWND hwnd, WPARAM wParam)
595 HEADER_INFO *infoPtr = HEADER_GetInfoPtr(hwnd);
596 INT iItem = (INT)wParam;
598 TRACE("[iItem=%d]\n", iItem);
600 if ((iItem < 0) || (iItem >= (INT)infoPtr->uNumItem))
601 return FALSE;
603 if (infoPtr->uNumItem == 1) {
604 TRACE("Simple delete!\n");
605 if (infoPtr->items[0].pszText)
606 Free (infoPtr->items[0].pszText);
607 Free (infoPtr->items);
608 infoPtr->items = 0;
609 infoPtr->uNumItem = 0;
611 else {
612 HEADER_ITEM *oldItems = infoPtr->items;
613 HEADER_ITEM *pItem;
614 INT i;
615 INT iOrder;
616 TRACE("Complex delete! [iItem=%d]\n", iItem);
618 if (infoPtr->items[iItem].pszText)
619 Free (infoPtr->items[iItem].pszText);
620 iOrder = infoPtr->items[iItem].iOrder;
622 infoPtr->uNumItem--;
623 infoPtr->items = Alloc (sizeof (HEADER_ITEM) * infoPtr->uNumItem);
624 /* pre delete copy */
625 if (iItem > 0) {
626 memcpy (&infoPtr->items[0], &oldItems[0],
627 iItem * sizeof(HEADER_ITEM));
630 /* post delete copy */
631 if (iItem < infoPtr->uNumItem) {
632 memcpy (&infoPtr->items[iItem], &oldItems[iItem+1],
633 (infoPtr->uNumItem - iItem) * sizeof(HEADER_ITEM));
636 /* Correct the orders */
637 for (i=infoPtr->uNumItem, pItem = infoPtr->items; i; i--, pItem++)
639 if (pItem->iOrder > iOrder)
640 pItem->iOrder--;
642 Free (oldItems);
645 HEADER_SetItemBounds (hwnd);
647 InvalidateRect(hwnd, NULL, FALSE);
649 return TRUE;
653 static LRESULT
654 HEADER_GetImageList (HWND hwnd)
656 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
658 return (LRESULT)infoPtr->himl;
662 static LRESULT
663 HEADER_GetItemA (HWND hwnd, WPARAM wParam, LPARAM lParam)
665 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
666 HDITEMA *phdi = (HDITEMA*)lParam;
667 INT nItem = (INT)wParam;
668 HEADER_ITEM *lpItem;
670 if (!phdi)
671 return FALSE;
673 TRACE("[nItem=%d]\n", nItem);
675 if (phdi->mask == 0)
676 return TRUE;
678 if ((nItem < 0) || (nItem >= (INT)infoPtr->uNumItem)) {
679 lpItem = NULL;
681 else {
682 lpItem = &infoPtr->items[nItem];
685 if (phdi->mask & HDI_BITMAP)
686 phdi->hbm = (lpItem != NULL) ? lpItem->hbm : 0;
688 if (phdi->mask & HDI_FORMAT)
689 phdi->fmt = (lpItem != NULL) ? lpItem->fmt : 0;
691 if (phdi->mask & HDI_WIDTH)
692 phdi->cxy = (lpItem != NULL) ? lpItem->cxy : 0;
694 if (phdi->mask & HDI_LPARAM)
695 phdi->lParam = (lpItem != NULL) ? lpItem->lParam : 0;
697 if (phdi->mask & HDI_TEXT) {
698 if (lpItem == NULL) {
699 *phdi->pszText = 0;
701 else if (lpItem->pszText != LPSTR_TEXTCALLBACKW) {
702 if (lpItem->pszText)
703 WideCharToMultiByte (CP_ACP, 0, lpItem->pszText, -1,
704 phdi->pszText, phdi->cchTextMax, NULL, NULL);
705 else
706 *phdi->pszText = 0;
708 else
709 phdi->pszText = LPSTR_TEXTCALLBACKA;
712 if (phdi->mask & HDI_IMAGE)
713 phdi->iImage = (lpItem != NULL) ? lpItem->iImage : 0;
715 if (phdi->mask & HDI_ORDER)
716 phdi->iOrder = (lpItem != NULL) ? lpItem->iOrder : 0;
718 return TRUE;
722 static LRESULT
723 HEADER_GetItemW (HWND hwnd, WPARAM wParam, LPARAM lParam)
725 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
726 HDITEMW *phdi = (HDITEMW*)lParam;
727 INT nItem = (INT)wParam;
728 HEADER_ITEM *lpItem;
730 if (!phdi)
731 return FALSE;
733 TRACE("[nItem=%d]\n", nItem);
735 if (phdi->mask == 0)
736 return TRUE;
738 if ((nItem < 0) || (nItem >= (INT)infoPtr->uNumItem)) {
739 lpItem = NULL;
741 else {
742 lpItem = &infoPtr->items[nItem];
745 if (phdi->mask & HDI_BITMAP)
746 phdi->hbm = (lpItem != NULL) ? lpItem->hbm : 0;
748 if (phdi->mask & HDI_FORMAT)
749 phdi->fmt = (lpItem != NULL) ? lpItem->fmt : 0;
751 if (phdi->mask & HDI_WIDTH)
752 phdi->cxy = (lpItem != NULL) ? lpItem->cxy : 0;
754 if (phdi->mask & HDI_LPARAM)
755 phdi->lParam = (lpItem != NULL) ? lpItem->lParam : 0;
757 if (phdi->mask & HDI_TEXT) {
758 if (lpItem == NULL) {
759 *phdi->pszText = 0;
761 else if (lpItem->pszText != LPSTR_TEXTCALLBACKW) {
762 if (lpItem->pszText)
763 lstrcpynW (phdi->pszText, lpItem->pszText, phdi->cchTextMax);
764 else
765 *phdi->pszText = 0;
767 else
768 phdi->pszText = LPSTR_TEXTCALLBACKW;
771 if (phdi->mask & HDI_IMAGE)
772 phdi->iImage = (lpItem != NULL) ? lpItem->iImage : 0;
774 if (phdi->mask & HDI_ORDER)
775 phdi->iOrder = (lpItem != NULL) ? lpItem->iOrder : 0;
777 return TRUE;
781 inline static LRESULT
782 HEADER_GetItemCount (HWND hwnd)
784 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
785 return infoPtr->uNumItem;
789 static LRESULT
790 HEADER_GetItemRect (HWND hwnd, WPARAM wParam, LPARAM lParam)
792 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
793 INT iItem = (INT)wParam;
794 LPRECT lpRect = (LPRECT)lParam;
796 if ((iItem < 0) || (iItem >= (INT)infoPtr->uNumItem))
797 return FALSE;
799 lpRect->left = infoPtr->items[iItem].rect.left;
800 lpRect->right = infoPtr->items[iItem].rect.right;
801 lpRect->top = infoPtr->items[iItem].rect.top;
802 lpRect->bottom = infoPtr->items[iItem].rect.bottom;
804 return TRUE;
808 static LRESULT
809 HEADER_GetOrderArray(HWND hwnd, WPARAM wParam, LPARAM lParam)
811 int i;
812 LPINT order = (LPINT) lParam;
813 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
815 if ((unsigned int)wParam <infoPtr->uNumItem)
816 return FALSE;
817 for (i=0; i<(int)wParam; i++)
818 *order++=HEADER_OrderToIndex(hwnd,i);
819 return TRUE;
822 static LRESULT
823 HEADER_SetOrderArray(HWND hwnd, WPARAM wParam, LPARAM lParam)
825 int i;
826 LPINT order = (LPINT) lParam;
827 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
828 HEADER_ITEM *lpItem;
830 if ((unsigned int)wParam <infoPtr->uNumItem)
831 return FALSE;
832 for (i=0; i<(int)wParam; i++)
834 lpItem = &infoPtr->items[*order++];
835 lpItem->iOrder=i;
837 infoPtr->bRectsValid=0;
838 InvalidateRect(hwnd, NULL, FALSE);
839 return TRUE;
842 inline static LRESULT
843 HEADER_GetUnicodeFormat (HWND hwnd)
845 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
846 return infoPtr->bUnicode;
850 static LRESULT
851 HEADER_HitTest (HWND hwnd, WPARAM wParam, LPARAM lParam)
853 LPHDHITTESTINFO phti = (LPHDHITTESTINFO)lParam;
855 HEADER_InternalHitTest (hwnd, &phti->pt, &phti->flags, &phti->iItem);
857 if (phti->flags == HHT_NOWHERE)
858 return -1;
859 else
860 return phti->iItem;
864 static LRESULT
865 HEADER_InsertItemA (HWND hwnd, WPARAM wParam, LPARAM lParam)
867 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
868 HDITEMA *phdi = (HDITEMA*)lParam;
869 INT nItem = (INT)wParam;
870 HEADER_ITEM *lpItem;
871 INT len, iOrder;
872 UINT i;
874 if ((phdi == NULL) || (nItem < 0))
875 return -1;
877 if (nItem > infoPtr->uNumItem)
878 nItem = infoPtr->uNumItem;
880 iOrder = (phdi->mask & HDI_ORDER) ? phdi->iOrder : nItem;
882 if (infoPtr->uNumItem == 0) {
883 infoPtr->items = Alloc (sizeof (HEADER_ITEM));
884 infoPtr->uNumItem++;
886 else {
887 HEADER_ITEM *oldItems = infoPtr->items;
889 infoPtr->uNumItem++;
890 infoPtr->items = Alloc (sizeof (HEADER_ITEM) * infoPtr->uNumItem);
891 if (nItem == 0) {
892 memcpy (&infoPtr->items[1], &oldItems[0],
893 (infoPtr->uNumItem-1) * sizeof(HEADER_ITEM));
895 else
897 /* pre insert copy */
898 if (nItem > 0) {
899 memcpy (&infoPtr->items[0], &oldItems[0],
900 nItem * sizeof(HEADER_ITEM));
903 /* post insert copy */
904 if (nItem < infoPtr->uNumItem - 1) {
905 memcpy (&infoPtr->items[nItem+1], &oldItems[nItem],
906 (infoPtr->uNumItem - nItem - 1) * sizeof(HEADER_ITEM));
910 Free (oldItems);
913 for (i=0; i < infoPtr->uNumItem; i++)
915 if (infoPtr->items[i].iOrder >= iOrder)
916 infoPtr->items[i].iOrder++;
919 lpItem = &infoPtr->items[nItem];
920 lpItem->bDown = FALSE;
922 if (phdi->mask & HDI_WIDTH)
923 lpItem->cxy = phdi->cxy;
925 if (phdi->mask & HDI_TEXT) {
926 if (!phdi->pszText) /* null pointer check */
927 phdi->pszText = "";
928 if (phdi->pszText != LPSTR_TEXTCALLBACKA) {
929 len = MultiByteToWideChar(CP_ACP, 0, phdi->pszText, -1, NULL, 0);
930 lpItem->pszText = Alloc( len*sizeof(WCHAR) );
931 MultiByteToWideChar(CP_ACP, 0, phdi->pszText, -1, lpItem->pszText, len);
933 else
934 lpItem->pszText = LPSTR_TEXTCALLBACKW;
937 if (phdi->mask & HDI_FORMAT)
938 lpItem->fmt = phdi->fmt;
940 if (lpItem->fmt == 0)
941 lpItem->fmt = HDF_LEFT;
943 if (!(lpItem->fmt & HDF_STRING) && (phdi->mask & HDI_TEXT))
945 lpItem->fmt |= HDF_STRING;
947 if (phdi->mask & HDI_BITMAP)
948 lpItem->hbm = phdi->hbm;
950 if (phdi->mask & HDI_LPARAM)
951 lpItem->lParam = phdi->lParam;
953 if (phdi->mask & HDI_IMAGE)
954 lpItem->iImage = phdi->iImage;
956 lpItem->iOrder = iOrder;
958 HEADER_SetItemBounds (hwnd);
960 InvalidateRect(hwnd, NULL, FALSE);
962 return nItem;
966 static LRESULT
967 HEADER_InsertItemW (HWND hwnd, WPARAM wParam, LPARAM lParam)
969 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
970 HDITEMW *phdi = (HDITEMW*)lParam;
971 INT nItem = (INT)wParam;
972 HEADER_ITEM *lpItem;
973 INT len, iOrder;
974 UINT i;
976 if ((phdi == NULL) || (nItem < 0))
977 return -1;
979 if (nItem > infoPtr->uNumItem)
980 nItem = infoPtr->uNumItem;
982 iOrder = (phdi->mask & HDI_ORDER) ? phdi->iOrder : nItem;
984 if (infoPtr->uNumItem == 0) {
985 infoPtr->items = Alloc (sizeof (HEADER_ITEM));
986 infoPtr->uNumItem++;
988 else {
989 HEADER_ITEM *oldItems = infoPtr->items;
991 infoPtr->uNumItem++;
992 infoPtr->items = Alloc (sizeof (HEADER_ITEM) * infoPtr->uNumItem);
993 if (nItem == 0) {
994 memcpy (&infoPtr->items[1], &oldItems[0],
995 (infoPtr->uNumItem-1) * sizeof(HEADER_ITEM));
997 else
999 /* pre insert copy */
1000 if (nItem > 0) {
1001 memcpy (&infoPtr->items[0], &oldItems[0],
1002 nItem * sizeof(HEADER_ITEM));
1005 /* post insert copy */
1006 if (nItem < infoPtr->uNumItem - 1) {
1007 memcpy (&infoPtr->items[nItem+1], &oldItems[nItem],
1008 (infoPtr->uNumItem - nItem - 1) * sizeof(HEADER_ITEM));
1012 Free (oldItems);
1015 for (i=0; i < infoPtr->uNumItem; i++)
1017 if (infoPtr->items[i].iOrder >= iOrder)
1018 infoPtr->items[i].iOrder++;
1021 lpItem = &infoPtr->items[nItem];
1022 lpItem->bDown = FALSE;
1024 if (phdi->mask & HDI_WIDTH)
1025 lpItem->cxy = phdi->cxy;
1027 if (phdi->mask & HDI_TEXT) {
1028 WCHAR wide_null_char = 0;
1029 if (!phdi->pszText) /* null pointer check */
1030 phdi->pszText = &wide_null_char;
1031 if (phdi->pszText != LPSTR_TEXTCALLBACKW) {
1032 len = strlenW (phdi->pszText);
1033 lpItem->pszText = Alloc ((len+1)*sizeof(WCHAR));
1034 strcpyW (lpItem->pszText, phdi->pszText);
1036 else
1037 lpItem->pszText = LPSTR_TEXTCALLBACKW;
1040 if (phdi->mask & HDI_FORMAT)
1041 lpItem->fmt = phdi->fmt;
1043 if (lpItem->fmt == 0)
1044 lpItem->fmt = HDF_LEFT;
1046 if (!(lpItem->fmt &HDF_STRING) && (phdi->mask & HDI_TEXT))
1048 lpItem->fmt |= HDF_STRING;
1050 if (phdi->mask & HDI_BITMAP)
1051 lpItem->hbm = phdi->hbm;
1053 if (phdi->mask & HDI_LPARAM)
1054 lpItem->lParam = phdi->lParam;
1056 if (phdi->mask & HDI_IMAGE)
1057 lpItem->iImage = phdi->iImage;
1059 lpItem->iOrder = iOrder;
1061 HEADER_SetItemBounds (hwnd);
1063 InvalidateRect(hwnd, NULL, FALSE);
1065 return nItem;
1069 static LRESULT
1070 HEADER_Layout (HWND hwnd, WPARAM wParam, LPARAM lParam)
1072 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
1073 LPHDLAYOUT lpLayout = (LPHDLAYOUT)lParam;
1075 lpLayout->pwpos->hwnd = hwnd;
1076 lpLayout->pwpos->hwndInsertAfter = 0;
1077 lpLayout->pwpos->x = lpLayout->prc->left;
1078 lpLayout->pwpos->y = lpLayout->prc->top;
1079 lpLayout->pwpos->cx = lpLayout->prc->right - lpLayout->prc->left;
1080 if (GetWindowLongW (hwnd, GWL_STYLE) & HDS_HIDDEN)
1081 lpLayout->pwpos->cy = 0;
1082 else {
1083 lpLayout->pwpos->cy = infoPtr->nHeight;
1084 lpLayout->prc->top += infoPtr->nHeight;
1086 lpLayout->pwpos->flags = SWP_NOZORDER;
1088 TRACE("Layout x=%d y=%d cx=%d cy=%d\n",
1089 lpLayout->pwpos->x, lpLayout->pwpos->y,
1090 lpLayout->pwpos->cx, lpLayout->pwpos->cy);
1092 infoPtr->bRectsValid = FALSE;
1094 return TRUE;
1098 static LRESULT
1099 HEADER_SetImageList (HWND hwnd, HIMAGELIST himl)
1101 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
1102 HIMAGELIST himlOld;
1104 TRACE("(himl 0x%x)\n", (int)himl);
1105 himlOld = infoPtr->himl;
1106 infoPtr->himl = himl;
1108 /* FIXME: Refresh needed??? */
1110 return (LRESULT)himlOld;
1114 static LRESULT
1115 HEADER_GetBitmapMargin(HWND hwnd)
1117 HEADER_INFO *infoPtr = HEADER_GetInfoPtr(hwnd);
1119 return infoPtr->iMargin;
1122 static LRESULT
1123 HEADER_SetBitmapMargin(HWND hwnd, WPARAM wParam)
1125 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
1126 INT oldMargin = infoPtr->iMargin;
1128 infoPtr->iMargin = (INT)wParam;
1130 return oldMargin;
1133 static LRESULT
1134 HEADER_SetItemA (HWND hwnd, WPARAM wParam, LPARAM lParam)
1136 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
1137 HDITEMA *phdi = (HDITEMA*)lParam;
1138 INT nItem = (INT)wParam;
1139 HEADER_ITEM *lpItem;
1141 if (phdi == NULL)
1142 return FALSE;
1143 if ((nItem < 0) || (nItem >= (INT)infoPtr->uNumItem))
1144 return FALSE;
1146 TRACE("[nItem=%d]\n", nItem);
1148 if (HEADER_SendHeaderNotify (hwnd, HDN_ITEMCHANGINGA, nItem, phdi->mask))
1149 return FALSE;
1151 lpItem = &infoPtr->items[nItem];
1152 if (phdi->mask & HDI_BITMAP)
1153 lpItem->hbm = phdi->hbm;
1155 if (phdi->mask & HDI_FORMAT)
1156 lpItem->fmt = phdi->fmt;
1158 if (phdi->mask & HDI_LPARAM)
1159 lpItem->lParam = phdi->lParam;
1161 if (phdi->mask & HDI_TEXT) {
1162 if (phdi->pszText != LPSTR_TEXTCALLBACKA) {
1163 if (lpItem->pszText) {
1164 Free (lpItem->pszText);
1165 lpItem->pszText = NULL;
1167 if (phdi->pszText) {
1168 INT len = MultiByteToWideChar (CP_ACP,0,phdi->pszText,-1,NULL,0);
1169 lpItem->pszText = Alloc( len*sizeof(WCHAR) );
1170 MultiByteToWideChar (CP_ACP,0,phdi->pszText,-1,lpItem->pszText,len);
1173 else
1174 lpItem->pszText = LPSTR_TEXTCALLBACKW;
1177 if (phdi->mask & HDI_WIDTH)
1178 lpItem->cxy = phdi->cxy;
1180 if (phdi->mask & HDI_IMAGE)
1181 lpItem->iImage = phdi->iImage;
1183 if (phdi->mask & HDI_ORDER)
1185 lpItem->iOrder = phdi->iOrder;
1188 HEADER_SendHeaderNotify (hwnd, HDN_ITEMCHANGEDA, nItem, phdi->mask);
1190 HEADER_SetItemBounds (hwnd);
1192 InvalidateRect(hwnd, NULL, FALSE);
1194 return TRUE;
1198 static LRESULT
1199 HEADER_SetItemW (HWND hwnd, WPARAM wParam, LPARAM lParam)
1201 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
1202 HDITEMW *phdi = (HDITEMW*)lParam;
1203 INT nItem = (INT)wParam;
1204 HEADER_ITEM *lpItem;
1206 if (phdi == NULL)
1207 return FALSE;
1208 if ((nItem < 0) || (nItem >= (INT)infoPtr->uNumItem))
1209 return FALSE;
1211 TRACE("[nItem=%d]\n", nItem);
1213 if (HEADER_SendHeaderNotify (hwnd, HDN_ITEMCHANGINGW, nItem, phdi->mask))
1214 return FALSE;
1216 lpItem = &infoPtr->items[nItem];
1217 if (phdi->mask & HDI_BITMAP)
1218 lpItem->hbm = phdi->hbm;
1220 if (phdi->mask & HDI_FORMAT)
1221 lpItem->fmt = phdi->fmt;
1223 if (phdi->mask & HDI_LPARAM)
1224 lpItem->lParam = phdi->lParam;
1226 if (phdi->mask & HDI_TEXT) {
1227 if (phdi->pszText != LPSTR_TEXTCALLBACKW) {
1228 if (lpItem->pszText) {
1229 Free (lpItem->pszText);
1230 lpItem->pszText = NULL;
1232 if (phdi->pszText) {
1233 INT len = strlenW (phdi->pszText);
1234 lpItem->pszText = Alloc ((len+1)*sizeof(WCHAR));
1235 strcpyW (lpItem->pszText, phdi->pszText);
1238 else
1239 lpItem->pszText = LPSTR_TEXTCALLBACKW;
1242 if (phdi->mask & HDI_WIDTH)
1243 lpItem->cxy = phdi->cxy;
1245 if (phdi->mask & HDI_IMAGE)
1246 lpItem->iImage = phdi->iImage;
1248 if (phdi->mask & HDI_ORDER)
1250 lpItem->iOrder = phdi->iOrder;
1253 HEADER_SendHeaderNotify(hwnd, HDN_ITEMCHANGEDW, nItem, phdi->mask);
1255 HEADER_SetItemBounds (hwnd);
1257 InvalidateRect(hwnd, NULL, FALSE);
1259 return TRUE;
1262 inline static LRESULT
1263 HEADER_SetUnicodeFormat (HWND hwnd, WPARAM wParam)
1265 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
1266 BOOL bTemp = infoPtr->bUnicode;
1268 infoPtr->bUnicode = (BOOL)wParam;
1270 return bTemp;
1274 static LRESULT
1275 HEADER_Create (HWND hwnd, WPARAM wParam, LPARAM lParam)
1277 HEADER_INFO *infoPtr;
1278 TEXTMETRICW tm;
1279 HFONT hOldFont;
1280 HDC hdc;
1282 infoPtr = (HEADER_INFO *)Alloc (sizeof(HEADER_INFO));
1283 SetWindowLongPtrW (hwnd, 0, (DWORD_PTR)infoPtr);
1285 infoPtr->hwndNotify = ((LPCREATESTRUCTA)lParam)->hwndParent;
1286 infoPtr->uNumItem = 0;
1287 infoPtr->hFont = 0;
1288 infoPtr->items = 0;
1289 infoPtr->bRectsValid = FALSE;
1290 infoPtr->hcurArrow = LoadCursorW (0, (LPWSTR)IDC_ARROW);
1291 infoPtr->hcurDivider = LoadCursorW (COMCTL32_hModule, MAKEINTRESOURCEW(IDC_DIVIDER));
1292 infoPtr->hcurDivopen = LoadCursorW (COMCTL32_hModule, MAKEINTRESOURCEW(IDC_DIVIDEROPEN));
1293 infoPtr->bPressed = FALSE;
1294 infoPtr->bTracking = FALSE;
1295 infoPtr->iMoveItem = 0;
1296 infoPtr->himl = 0;
1297 infoPtr->iHotItem = -1;
1298 infoPtr->bUnicode = IsWindowUnicode (hwnd);
1299 infoPtr->iMargin = 3*GetSystemMetrics(SM_CXEDGE);
1300 infoPtr->nNotifyFormat =
1301 SendMessageW (infoPtr->hwndNotify, WM_NOTIFYFORMAT, (WPARAM)hwnd, NF_QUERY);
1303 hdc = GetDC (0);
1304 hOldFont = SelectObject (hdc, GetStockObject (SYSTEM_FONT));
1305 GetTextMetricsW (hdc, &tm);
1306 infoPtr->nHeight = tm.tmHeight + VERT_BORDER;
1307 SelectObject (hdc, hOldFont);
1308 ReleaseDC (0, hdc);
1310 return 0;
1314 static LRESULT
1315 HEADER_Destroy (HWND hwnd, WPARAM wParam, LPARAM lParam)
1317 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
1318 HEADER_ITEM *lpItem;
1319 INT nItem;
1321 if (infoPtr->items) {
1322 lpItem = infoPtr->items;
1323 for (nItem = 0; nItem < infoPtr->uNumItem; nItem++, lpItem++) {
1324 if ((lpItem->pszText) && (lpItem->pszText != LPSTR_TEXTCALLBACKW))
1325 Free (lpItem->pszText);
1327 Free (infoPtr->items);
1330 if (infoPtr->himl)
1331 ImageList_Destroy (infoPtr->himl);
1333 SetWindowLongPtrW (hwnd, 0, 0);
1334 Free (infoPtr);
1335 return 0;
1339 static inline LRESULT
1340 HEADER_GetFont (HWND hwnd)
1342 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
1344 return (LRESULT)infoPtr->hFont;
1348 static LRESULT
1349 HEADER_LButtonDblClk (HWND hwnd, WPARAM wParam, LPARAM lParam)
1351 POINT pt;
1352 UINT flags;
1353 INT nItem;
1355 pt.x = (INT)LOWORD(lParam);
1356 pt.y = (INT)HIWORD(lParam);
1357 HEADER_InternalHitTest (hwnd, &pt, &flags, &nItem);
1359 if ((GetWindowLongW (hwnd, GWL_STYLE) & HDS_BUTTONS) && (flags == HHT_ONHEADER))
1360 HEADER_SendHeaderNotify (hwnd, HDN_ITEMDBLCLICKA, nItem,0);
1361 else if ((flags == HHT_ONDIVIDER) || (flags == HHT_ONDIVOPEN))
1362 HEADER_SendHeaderNotify (hwnd, HDN_DIVIDERDBLCLICKA, nItem,0);
1364 return 0;
1368 static LRESULT
1369 HEADER_LButtonDown (HWND hwnd, WPARAM wParam, LPARAM lParam)
1371 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
1372 DWORD dwStyle = GetWindowLongW (hwnd, GWL_STYLE);
1373 POINT pt;
1374 UINT flags;
1375 INT nItem;
1376 HDC hdc;
1378 pt.x = (INT)LOWORD(lParam);
1379 pt.y = (INT)HIWORD(lParam);
1380 HEADER_InternalHitTest (hwnd, &pt, &flags, &nItem);
1382 if ((dwStyle & HDS_BUTTONS) && (flags == HHT_ONHEADER)) {
1383 SetCapture (hwnd);
1384 infoPtr->bCaptured = TRUE;
1385 infoPtr->bPressed = TRUE;
1386 infoPtr->iMoveItem = nItem;
1388 infoPtr->items[nItem].bDown = TRUE;
1390 /* Send WM_CUSTOMDRAW */
1391 hdc = GetDC (hwnd);
1392 HEADER_RefreshItem (hwnd, hdc, nItem);
1393 ReleaseDC (hwnd, hdc);
1395 TRACE("Pressed item %d!\n", nItem);
1397 else if ((flags == HHT_ONDIVIDER) || (flags == HHT_ONDIVOPEN)) {
1398 if (!(HEADER_SendHeaderNotify (hwnd, HDN_BEGINTRACKA, nItem,0))) {
1399 SetCapture (hwnd);
1400 infoPtr->bCaptured = TRUE;
1401 infoPtr->bTracking = TRUE;
1402 infoPtr->iMoveItem = nItem;
1403 infoPtr->nOldWidth = infoPtr->items[nItem].cxy;
1404 infoPtr->xTrackOffset = infoPtr->items[nItem].rect.right - pt.x;
1406 if (!(dwStyle & HDS_FULLDRAG)) {
1407 infoPtr->xOldTrack = infoPtr->items[nItem].rect.right;
1408 hdc = GetDC (hwnd);
1409 HEADER_DrawTrackLine (hwnd, hdc, infoPtr->xOldTrack);
1410 ReleaseDC (hwnd, hdc);
1413 TRACE("Begin tracking item %d!\n", nItem);
1417 return 0;
1421 static LRESULT
1422 HEADER_LButtonUp (HWND hwnd, WPARAM wParam, LPARAM lParam)
1424 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
1426 *DWORD dwStyle = GetWindowLongW (hwnd, GWL_STYLE);
1428 POINT pt;
1429 UINT flags;
1430 INT nItem, nWidth;
1431 HDC hdc;
1433 pt.x = (INT)(SHORT)LOWORD(lParam);
1434 pt.y = (INT)(SHORT)HIWORD(lParam);
1435 HEADER_InternalHitTest (hwnd, &pt, &flags, &nItem);
1437 if (infoPtr->bPressed) {
1438 if ((nItem == infoPtr->iMoveItem) && (flags == HHT_ONHEADER)) {
1439 infoPtr->items[infoPtr->iMoveItem].bDown = FALSE;
1440 hdc = GetDC (hwnd);
1441 HEADER_RefreshItem (hwnd, hdc, infoPtr->iMoveItem);
1442 ReleaseDC (hwnd, hdc);
1444 HEADER_SendClickNotify (hwnd, HDN_ITEMCLICKA, infoPtr->iMoveItem);
1446 else if (flags == HHT_ONHEADER)
1448 HEADER_ITEM *lpItem;
1449 INT newindex = HEADER_IndexToOrder(hwnd,nItem);
1450 INT oldindex = HEADER_IndexToOrder(hwnd,infoPtr->iMoveItem);
1452 TRACE("Exchanging [index:order] [%d:%d] [%d:%d]\n",
1453 infoPtr->iMoveItem,oldindex,nItem,newindex);
1454 lpItem= &infoPtr->items[nItem];
1455 lpItem->iOrder=oldindex;
1457 lpItem= &infoPtr->items[infoPtr->iMoveItem];
1458 lpItem->iOrder = newindex;
1460 infoPtr->bRectsValid = FALSE;
1461 InvalidateRect(hwnd, NULL, FALSE);
1462 /* FIXME: Should some WM_NOTIFY be sent */
1465 TRACE("Released item %d!\n", infoPtr->iMoveItem);
1466 infoPtr->bPressed = FALSE;
1468 else if (infoPtr->bTracking) {
1469 TRACE("End tracking item %d!\n", infoPtr->iMoveItem);
1470 infoPtr->bTracking = FALSE;
1472 HEADER_SendHeaderNotify (hwnd, HDN_ENDTRACKA, infoPtr->iMoveItem,HDI_WIDTH);
1475 * we want to do this even for HDS_FULLDRAG because this is where
1476 * we send the HDN_ITEMCHANGING and HDN_ITEMCHANGED notifications
1478 * if (!(dwStyle & HDS_FULLDRAG)) {
1481 hdc = GetDC (hwnd);
1482 HEADER_DrawTrackLine (hwnd, hdc, infoPtr->xOldTrack);
1483 ReleaseDC (hwnd, hdc);
1484 if (HEADER_SendHeaderNotify(hwnd, HDN_ITEMCHANGINGA, infoPtr->iMoveItem, HDI_WIDTH))
1486 infoPtr->items[infoPtr->iMoveItem].cxy = infoPtr->nOldWidth;
1488 else {
1489 nWidth = pt.x - infoPtr->items[infoPtr->iMoveItem].rect.left + infoPtr->xTrackOffset;
1490 if (nWidth < 0)
1491 nWidth = 0;
1492 infoPtr->items[infoPtr->iMoveItem].cxy = nWidth;
1495 HEADER_SetItemBounds (hwnd);
1496 InvalidateRect(hwnd, NULL, TRUE);
1497 HEADER_SendHeaderNotify(hwnd, HDN_ITEMCHANGEDA, infoPtr->iMoveItem, HDI_WIDTH);
1503 if (infoPtr->bCaptured) {
1504 infoPtr->bCaptured = FALSE;
1505 ReleaseCapture ();
1506 HEADER_SendSimpleNotify (hwnd, NM_RELEASEDCAPTURE);
1509 return 0;
1513 static LRESULT
1514 HEADER_NotifyFormat (HWND hwnd, WPARAM wParam, LPARAM lParam)
1516 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
1518 switch (lParam)
1520 case NF_QUERY:
1521 return infoPtr->nNotifyFormat;
1523 case NF_REQUERY:
1524 infoPtr->nNotifyFormat =
1525 SendMessageW ((HWND)wParam, WM_NOTIFYFORMAT,
1526 (WPARAM)hwnd, (LPARAM)NF_QUERY);
1527 return infoPtr->nNotifyFormat;
1530 return 0;
1534 static LRESULT
1535 HEADER_MouseMove (HWND hwnd, WPARAM wParam, LPARAM lParam)
1537 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
1538 DWORD dwStyle = GetWindowLongW (hwnd, GWL_STYLE);
1539 POINT pt;
1540 UINT flags;
1541 INT nItem, nWidth;
1542 HDC hdc;
1544 pt.x = (INT)(SHORT)LOWORD(lParam);
1545 pt.y = (INT)(SHORT)HIWORD(lParam);
1546 HEADER_InternalHitTest (hwnd, &pt, &flags, &nItem);
1548 if ((dwStyle & HDS_BUTTONS) && (dwStyle & HDS_HOTTRACK)) {
1549 if (flags & (HHT_ONHEADER | HHT_ONDIVIDER | HHT_ONDIVOPEN))
1550 infoPtr->iHotItem = nItem;
1551 else
1552 infoPtr->iHotItem = -1;
1553 InvalidateRect(hwnd, NULL, FALSE);
1556 if (infoPtr->bCaptured) {
1557 if (infoPtr->bPressed) {
1558 if ((nItem == infoPtr->iMoveItem) && (flags == HHT_ONHEADER))
1559 infoPtr->items[infoPtr->iMoveItem].bDown = TRUE;
1560 else
1561 infoPtr->items[infoPtr->iMoveItem].bDown = FALSE;
1562 hdc = GetDC (hwnd);
1563 HEADER_RefreshItem (hwnd, hdc, infoPtr->iMoveItem);
1564 ReleaseDC (hwnd, hdc);
1566 TRACE("Moving pressed item %d!\n", infoPtr->iMoveItem);
1568 else if (infoPtr->bTracking) {
1569 if (dwStyle & HDS_FULLDRAG) {
1570 if (HEADER_SendHeaderNotify (hwnd, HDN_TRACKA, infoPtr->iMoveItem, HDI_WIDTH))
1572 nWidth = pt.x - infoPtr->items[infoPtr->iMoveItem].rect.left + infoPtr->xTrackOffset;
1573 if (nWidth < 0)
1574 nWidth = 0;
1575 infoPtr->items[infoPtr->iMoveItem].cxy = nWidth;
1576 HEADER_SendHeaderNotify(hwnd, HDN_ITEMCHANGEDA, infoPtr->iMoveItem, HDI_WIDTH);
1578 HEADER_SetItemBounds (hwnd);
1580 else {
1581 hdc = GetDC (hwnd);
1582 HEADER_DrawTrackLine (hwnd, hdc, infoPtr->xOldTrack);
1583 infoPtr->xOldTrack = pt.x + infoPtr->xTrackOffset;
1584 if (infoPtr->xOldTrack < infoPtr->items[infoPtr->iMoveItem].rect.left)
1585 infoPtr->xOldTrack = infoPtr->items[infoPtr->iMoveItem].rect.left;
1586 infoPtr->items[infoPtr->iMoveItem].cxy =
1587 infoPtr->xOldTrack - infoPtr->items[infoPtr->iMoveItem].rect.left;
1588 HEADER_DrawTrackLine (hwnd, hdc, infoPtr->xOldTrack);
1589 ReleaseDC (hwnd, hdc);
1590 HEADER_SendHeaderNotify (hwnd, HDN_TRACKA, infoPtr->iMoveItem, HDI_WIDTH);
1593 TRACE("Tracking item %d!\n", infoPtr->iMoveItem);
1597 if ((dwStyle & HDS_BUTTONS) && (dwStyle & HDS_HOTTRACK)) {
1598 FIXME("hot track support!\n");
1601 return 0;
1605 static LRESULT
1606 HEADER_Paint (HWND hwnd, WPARAM wParam)
1608 HDC hdc;
1609 PAINTSTRUCT ps;
1611 hdc = wParam==0 ? BeginPaint (hwnd, &ps) : (HDC)wParam;
1612 HEADER_Refresh (hwnd, hdc);
1613 if(!wParam)
1614 EndPaint (hwnd, &ps);
1615 return 0;
1619 static LRESULT
1620 HEADER_RButtonUp (HWND hwnd, WPARAM wParam, LPARAM lParam)
1622 BOOL bRet;
1623 POINT pt;
1625 pt.x = LOWORD(lParam);
1626 pt.y = HIWORD(lParam);
1628 /* Send a Notify message */
1629 bRet = HEADER_SendSimpleNotify (hwnd, NM_RCLICK);
1631 /* Change to screen coordinate for WM_CONTEXTMENU */
1632 ClientToScreen(hwnd, &pt);
1634 /* Send a WM_CONTEXTMENU message in response to the RBUTTONUP */
1635 SendMessageW( hwnd, WM_CONTEXTMENU, (WPARAM) hwnd, MAKELPARAM(pt.x, pt.y));
1637 return bRet;
1641 static LRESULT
1642 HEADER_SetCursor (HWND hwnd, WPARAM wParam, LPARAM lParam)
1644 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
1645 POINT pt;
1646 UINT flags;
1647 INT nItem;
1649 TRACE("code=0x%X id=0x%X\n", LOWORD(lParam), HIWORD(lParam));
1651 GetCursorPos (&pt);
1652 ScreenToClient (hwnd, &pt);
1654 HEADER_InternalHitTest (hwnd, &pt, &flags, &nItem);
1656 if (flags == HHT_ONDIVIDER)
1657 SetCursor (infoPtr->hcurDivider);
1658 else if (flags == HHT_ONDIVOPEN)
1659 SetCursor (infoPtr->hcurDivopen);
1660 else
1661 SetCursor (infoPtr->hcurArrow);
1663 return 0;
1667 static LRESULT
1668 HEADER_SetFont (HWND hwnd, WPARAM wParam, LPARAM lParam)
1670 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
1671 TEXTMETRICW tm;
1672 HFONT hFont, hOldFont;
1673 HDC hdc;
1675 infoPtr->hFont = (HFONT)wParam;
1677 hFont = infoPtr->hFont ? infoPtr->hFont : GetStockObject (SYSTEM_FONT);
1679 hdc = GetDC (0);
1680 hOldFont = SelectObject (hdc, hFont);
1681 GetTextMetricsW (hdc, &tm);
1682 infoPtr->nHeight = tm.tmHeight + VERT_BORDER;
1683 SelectObject (hdc, hOldFont);
1684 ReleaseDC (0, hdc);
1686 infoPtr->bRectsValid = FALSE;
1688 if (lParam) {
1689 InvalidateRect(hwnd, NULL, FALSE);
1692 return 0;
1696 static LRESULT WINAPI
1697 HEADER_WindowProc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
1699 TRACE("hwnd=%p msg=%x wparam=%x lParam=%lx\n", hwnd, msg, wParam, lParam);
1700 if (!HEADER_GetInfoPtr (hwnd) && (msg != WM_CREATE))
1701 return DefWindowProcW (hwnd, msg, wParam, lParam);
1702 switch (msg) {
1703 /* case HDM_CLEARFILTER: */
1705 case HDM_CREATEDRAGIMAGE:
1706 return HEADER_CreateDragImage (hwnd, wParam);
1708 case HDM_DELETEITEM:
1709 return HEADER_DeleteItem (hwnd, wParam);
1711 /* case HDM_EDITFILTER: */
1713 case HDM_GETBITMAPMARGIN:
1714 return HEADER_GetBitmapMargin(hwnd);
1716 case HDM_GETIMAGELIST:
1717 return HEADER_GetImageList (hwnd);
1719 case HDM_GETITEMA:
1720 return HEADER_GetItemA (hwnd, wParam, lParam);
1722 case HDM_GETITEMW:
1723 return HEADER_GetItemW (hwnd, wParam, lParam);
1725 case HDM_GETITEMCOUNT:
1726 return HEADER_GetItemCount (hwnd);
1728 case HDM_GETITEMRECT:
1729 return HEADER_GetItemRect (hwnd, wParam, lParam);
1731 case HDM_GETORDERARRAY:
1732 return HEADER_GetOrderArray(hwnd, wParam, lParam);
1734 case HDM_GETUNICODEFORMAT:
1735 return HEADER_GetUnicodeFormat (hwnd);
1737 case HDM_HITTEST:
1738 return HEADER_HitTest (hwnd, wParam, lParam);
1740 case HDM_INSERTITEMA:
1741 return HEADER_InsertItemA (hwnd, wParam, lParam);
1743 case HDM_INSERTITEMW:
1744 return HEADER_InsertItemW (hwnd, wParam, lParam);
1746 case HDM_LAYOUT:
1747 return HEADER_Layout (hwnd, wParam, lParam);
1749 case HDM_ORDERTOINDEX:
1750 return HEADER_OrderToIndex(hwnd, wParam);
1752 case HDM_SETBITMAPMARGIN:
1753 return HEADER_SetBitmapMargin(hwnd, wParam);
1755 /* case HDM_SETFILTERCHANGETIMEOUT: */
1757 /* case HDM_SETHOTDIVIDER: */
1759 case HDM_SETIMAGELIST:
1760 return HEADER_SetImageList (hwnd, (HIMAGELIST)lParam);
1762 case HDM_SETITEMA:
1763 return HEADER_SetItemA (hwnd, wParam, lParam);
1765 case HDM_SETITEMW:
1766 return HEADER_SetItemW (hwnd, wParam, lParam);
1768 case HDM_SETORDERARRAY:
1769 return HEADER_SetOrderArray(hwnd, wParam, lParam);
1771 case HDM_SETUNICODEFORMAT:
1772 return HEADER_SetUnicodeFormat (hwnd, wParam);
1774 case WM_CREATE:
1775 return HEADER_Create (hwnd, wParam, lParam);
1777 case WM_DESTROY:
1778 return HEADER_Destroy (hwnd, wParam, lParam);
1780 case WM_ERASEBKGND:
1781 return 1;
1783 case WM_GETDLGCODE:
1784 return DLGC_WANTTAB | DLGC_WANTARROWS;
1786 case WM_GETFONT:
1787 return HEADER_GetFont (hwnd);
1789 case WM_LBUTTONDBLCLK:
1790 return HEADER_LButtonDblClk (hwnd, wParam, lParam);
1792 case WM_LBUTTONDOWN:
1793 return HEADER_LButtonDown (hwnd, wParam, lParam);
1795 case WM_LBUTTONUP:
1796 return HEADER_LButtonUp (hwnd, wParam, lParam);
1798 case WM_MOUSEMOVE:
1799 return HEADER_MouseMove (hwnd, wParam, lParam);
1801 case WM_NOTIFYFORMAT:
1802 return HEADER_NotifyFormat (hwnd, wParam, lParam);
1804 case WM_SIZE:
1805 return HEADER_Size (hwnd, wParam);
1807 case WM_PAINT:
1808 return HEADER_Paint (hwnd, wParam);
1810 case WM_RBUTTONUP:
1811 return HEADER_RButtonUp (hwnd, wParam, lParam);
1813 case WM_SETCURSOR:
1814 return HEADER_SetCursor (hwnd, wParam, lParam);
1816 case WM_SETFONT:
1817 return HEADER_SetFont (hwnd, wParam, lParam);
1819 default:
1820 if ((msg >= WM_USER) && (msg < WM_APP))
1821 ERR("unknown msg %04x wp=%04x lp=%08lx\n",
1822 msg, wParam, lParam );
1823 return DefWindowProcA (hwnd, msg, wParam, lParam);
1828 VOID
1829 HEADER_Register (void)
1831 WNDCLASSW wndClass;
1833 ZeroMemory (&wndClass, sizeof(WNDCLASSW));
1834 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS;
1835 wndClass.lpfnWndProc = HEADER_WindowProc;
1836 wndClass.cbClsExtra = 0;
1837 wndClass.cbWndExtra = sizeof(HEADER_INFO *);
1838 wndClass.hCursor = LoadCursorW (0, (LPWSTR)IDC_ARROW);
1839 wndClass.lpszClassName = WC_HEADERW;
1841 RegisterClassW (&wndClass);
1845 VOID
1846 HEADER_Unregister (void)
1848 UnregisterClassW (WC_HEADERW, NULL);