1 /*
2 * Header control
3 *
4 * Copyright 1998 Eric Kohl
5 * Copyright 2000 Eric Kohl for CodeWeavers
6 * Copyright 2003 Maxime Bellenge
7 * Copyright 2006 Mikolaj Zalewski
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 *
23 * TODO:
24 * - Imagelist support (completed?)
25 * - Hottrack support (completed?)
26 * - Filters support (HDS_FILTER, HDI_FILTER, HDM_*FILTER*, HDN_*FILTER*)
27 * - New Windows Vista features
28 */
29
30 #include <stdarg.h>
31 #include <stdlib.h>
32 #include <string.h>
33
34 #include "windef.h"
35 #include "winbase.h"
36 #include "wine/unicode.h"
37 #include "wingdi.h"
38 #include "winuser.h"
39 #include "winnls.h"
40 #include "commctrl.h"
41 #include "comctl32.h"
42 #include "imagelist.h"
43 #include "tmschema.h"
44 #include "uxtheme.h"
45 #include "wine/debug.h"
46
47 WINE_DEFAULT_DEBUG_CHANNEL(header);
48
49 typedef struct
50 {
51 INT cxy;
52 HBITMAP hbm;
53 LPWSTR pszText;
54 INT fmt;
55 LPARAM lParam;
56 INT iImage;
57 INT iOrder; /* see documentation of HD_ITEM */
58
59 BOOL bDown; /* is item pressed? (used for drawing) */
60 RECT rect; /* bounding rectangle of the item */
61 DWORD callbackMask; /* HDI_* flags for items that are callback */
62 } HEADER_ITEM;
63
64
65 typedef struct
66 {
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 bDragging; /* Are we dragging an item? */
78 BOOL bTracking; /* Is in tracking mode? */
79 POINT ptLButtonDown; /* The point where the left button was pressed */
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 iHotItem; /* index of hot item (cursor is over this item) */
84 INT iHotDivider; /* index of the hot divider (used while dragging an item or by HDM_SETHOTDIVIDER) */
85 INT iMargin; /* width of the margin that surrounds a bitmap */
86
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;
92
93
94 #define VERT_BORDER 4
95 #define DIVIDER_WIDTH 10
96 #define HOT_DIVIDER_WIDTH 2
97 #define MAX_HEADER_TEXT_LEN 260
98 #define HDN_UNICODE_OFFSET 20
99 #define HDN_FIRST_UNICODE (HDN_FIRST-HDN_UNICODE_OFFSET)
100
101 #define HDI_SUPPORTED_FIELDS (HDI_WIDTH|HDI_TEXT|HDI_FORMAT|HDI_LPARAM|HDI_BITMAP|HDI_IMAGE|HDI_ORDER)
102 #define HDI_UNSUPPORTED_FIELDS (HDI_FILTER)
103 #define HDI_UNKNOWN_FIELDS (~(HDI_SUPPORTED_FIELDS|HDI_UNSUPPORTED_FIELDS|HDI_DI_SETITEM))
104 #define HDI_COMCTL32_4_0_FIELDS (HDI_WIDTH|HDI_TEXT|HDI_FORMAT|HDI_LPARAM|HDI_BITMAP)
105
106 #define HEADER_GetInfoPtr(hwnd) ((HEADER_INFO *)GetWindowLongPtrW(hwnd,0))
107
108 static BOOL HEADER_PrepareCallbackItems(HWND hwnd, INT iItem, INT reqMask);
109 static void HEADER_FreeCallbackItems(HEADER_ITEM *lpItem);
110 static LRESULT HEADER_SendNotify(HWND hwnd, UINT code, NMHDR *hdr);
111 static LRESULT HEADER_SendCtrlCustomDraw(HWND hwnd, DWORD dwDrawStage, HDC hdc, const RECT *rect);
112
113 static const WCHAR themeClass[] = {'H','e','a','d','e','r',0};
114
115 static void HEADER_StoreHDItemInHeader(HEADER_ITEM *lpItem, UINT mask, const HDITEMW *phdi, BOOL fUnicode)
116 {
117 if (mask & HDI_UNSUPPORTED_FIELDS)
118 FIXME("unsupported header fields %x\n", (mask & HDI_UNSUPPORTED_FIELDS));
119
120 if (mask & HDI_BITMAP)
121 lpItem->hbm = phdi->hbm;
122
123 if (mask & HDI_FORMAT)
124 lpItem->fmt = phdi->fmt;
125
126 if (mask & HDI_LPARAM)
127 lpItem->lParam = phdi->lParam;
128
129 if (mask & HDI_WIDTH)
130 lpItem->cxy = phdi->cxy;
131
132 if (mask & HDI_IMAGE)
133 {
134 lpItem->iImage = phdi->iImage;
135 if (phdi->iImage == I_IMAGECALLBACK)
136 lpItem->callbackMask |= HDI_IMAGE;
137 else
138 lpItem->callbackMask &= ~HDI_IMAGE;
139 }
140
141 if (mask & HDI_TEXT)
142 {
143 Free(lpItem->pszText);
144 lpItem->pszText = NULL;
145
146 if (phdi->pszText != LPSTR_TEXTCALLBACKW) /* covers != TEXTCALLBACKA too */
147 {
148 static const WCHAR emptyString[] = {0};
149
150 LPCWSTR pszText = (phdi->pszText != NULL ? phdi->pszText : emptyString);
151 if (fUnicode)
152 Str_SetPtrW(&lpItem->pszText, pszText);
153 else
154 Str_SetPtrAtoW(&lpItem->pszText, (LPCSTR)pszText);
155 lpItem->callbackMask &= ~HDI_TEXT;
156 }
157 else
158 {
159 lpItem->pszText = NULL;
160 lpItem->callbackMask |= HDI_TEXT;
161 }
162 }
163 }
164
165 static inline LRESULT
166 HEADER_IndexToOrder (HWND hwnd, INT iItem)
167 {
168 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
169 HEADER_ITEM *lpItem = &infoPtr->items[iItem];
170 return lpItem->iOrder;
171 }
172
173
174 static INT
175 HEADER_OrderToIndex(HWND hwnd, WPARAM wParam)
176 {
177 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
178 INT iorder = (INT)wParam;
179
180 if ((iorder <0) || iorder >= infoPtr->uNumItem)
181 return iorder;
182 return infoPtr->order[iorder];
183 }
184
185 static void
186 HEADER_ChangeItemOrder(const HEADER_INFO *infoPtr, INT iItem, INT iNewOrder)
187 {
188 HEADER_ITEM *lpItem = &infoPtr->items[iItem];
189 INT i, nMin, nMax;
190
191 TRACE("%d: %d->%d\n", iItem, lpItem->iOrder, iNewOrder);
192 if (lpItem->iOrder < iNewOrder)
193 {
194 memmove(&infoPtr->order[lpItem->iOrder],
195 &infoPtr->order[lpItem->iOrder + 1],
196 (iNewOrder - lpItem->iOrder) * sizeof(INT));
197 }
198 if (iNewOrder < lpItem->iOrder)
199 {
200 memmove(&infoPtr->order[iNewOrder + 1],
201 &infoPtr->order[iNewOrder],
202 (lpItem->iOrder - iNewOrder) * sizeof(INT));
203 }
204 infoPtr->order[iNewOrder] = iItem;
205 nMin = min(lpItem->iOrder, iNewOrder);
206 nMax = max(lpItem->iOrder, iNewOrder);
207 for (i = nMin; i <= nMax; i++)
208 infoPtr->items[infoPtr->order[i]].iOrder = i;
209 }
210
211 /* Note: if iItem is the last item then this function returns infoPtr->uNumItem */
212 static INT
213 HEADER_NextItem(HWND hwnd, INT iItem)
214 {
215 return HEADER_OrderToIndex(hwnd, HEADER_IndexToOrder(hwnd, iItem)+1);
216 }
217
218 static INT
219 HEADER_PrevItem(HWND hwnd, INT iItem)
220 {
221 return HEADER_OrderToIndex(hwnd, HEADER_IndexToOrder(hwnd, iItem)-1);
222 }
223
224 static void
225 HEADER_SetItemBounds (HWND hwnd)
226 {
227 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
228 HEADER_ITEM *phdi;
229 RECT rect;
230 unsigned int i;
231 int x;
232
233 infoPtr->bRectsValid = TRUE;
234
235 if (infoPtr->uNumItem == 0)
236 return;
237
238 GetClientRect (hwnd, &rect);
239
240 x = rect.left;
241 for (i = 0; i < infoPtr->uNumItem; i++) {
242 phdi = &infoPtr->items[HEADER_OrderToIndex(hwnd,i)];
243 phdi->rect.top = rect.top;
244 phdi->rect.bottom = rect.bottom;
245 phdi->rect.left = x;
246 phdi->rect.right = phdi->rect.left + ((phdi->cxy>0)?phdi->cxy:0);
247 x = phdi->rect.right;
248 }
249 }
250
251 static LRESULT
252 HEADER_Size (HWND hwnd, WPARAM wParam)
253 {
254 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
255
256 infoPtr->bRectsValid = FALSE;
257
258 return 0;
259 }
260
261 static void HEADER_GetHotDividerRect(HWND hwnd, const HEADER_INFO *infoPtr, RECT *r)
262 {
263 INT iDivider = infoPtr->iHotDivider;
264 if (infoPtr->uNumItem > 0)
265 {
266 HEADER_ITEM *lpItem;
267
268 if (iDivider < infoPtr->uNumItem)
269 {
270 lpItem = &infoPtr->items[iDivider];
271 r->left = lpItem->rect.left - HOT_DIVIDER_WIDTH/2;
272 r->right = lpItem->rect.left + HOT_DIVIDER_WIDTH/2;
273 }
274 else
275 {
276 lpItem = &infoPtr->items[HEADER_OrderToIndex(hwnd, infoPtr->uNumItem-1)];
277 r->left = lpItem->rect.right - HOT_DIVIDER_WIDTH/2;
278 r->right = lpItem->rect.right + HOT_DIVIDER_WIDTH/2;
279 }
280 r->top = lpItem->rect.top;
281 r->bottom = lpItem->rect.bottom;
282 }
283 else
284 {
285 RECT clientRect;
286 GetClientRect(hwnd, &clientRect);
287 *r = clientRect;
288 r->right = r->left + HOT_DIVIDER_WIDTH/2;
289 }
290 }
291
292
293 static INT
294 HEADER_DrawItem (HWND hwnd, HDC hdc, INT iItem, BOOL bHotTrack, LRESULT lCDFlags)
295 {
296 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
297 HEADER_ITEM *phdi = &infoPtr->items[iItem];
298 RECT r;
299 INT oldBkMode;
300 HTHEME theme = GetWindowTheme (hwnd);
301 NMCUSTOMDRAW nmcd;
302
303 TRACE("DrawItem(iItem %d bHotTrack %d unicode flag %d)\n", iItem, bHotTrack, (infoPtr->nNotifyFormat == NFR_UNICODE));
304
305 r = phdi->rect;
306 if (r.right - r.left == 0)
307 return phdi->rect.right;
308
309 /* Set the colors before sending NM_CUSTOMDRAW so that it can change them */
310 SetTextColor(hdc, (bHotTrack && !theme) ? COLOR_HIGHLIGHT : COLOR_BTNTEXT);
311 SetBkColor(hdc, GetSysColor(COLOR_3DFACE));
312
313 if (lCDFlags & CDRF_NOTIFYITEMDRAW && !(phdi->fmt & HDF_OWNERDRAW))
314 {
315 LRESULT lCDItemFlags;
316
317 nmcd.dwDrawStage = CDDS_PREPAINT | CDDS_ITEM;
318 nmcd.hdc = hdc;
319 nmcd.dwItemSpec = iItem;
320 nmcd.rc = r;
321 nmcd.uItemState = phdi->bDown ? CDIS_SELECTED : 0;
322 nmcd.lItemlParam = phdi->lParam;
323
324 lCDItemFlags = HEADER_SendNotify(hwnd, NM_CUSTOMDRAW, (NMHDR *)&nmcd);
325 if (lCDItemFlags & CDRF_SKIPDEFAULT)
326 return phdi->rect.right;
327 }
328
329 if (theme != NULL) {
330 int state = (phdi->bDown) ? HIS_PRESSED :
331 (bHotTrack ? HIS_HOT : HIS_NORMAL);
332 DrawThemeBackground (theme, hdc, HP_HEADERITEM, state,
333 &r, NULL);
334 GetThemeBackgroundContentRect (theme, hdc, HP_HEADERITEM, state,
335 &r, &r);
336 }
337 else {
338 HBRUSH hbr;
339
340 if (GetWindowLongW (hwnd, GWL_STYLE) & HDS_BUTTONS) {
341 if (phdi->bDown) {
342 DrawEdge (hdc, &r, BDR_RAISEDOUTER,
343 BF_RECT | BF_FLAT | BF_MIDDLE | BF_ADJUST);
344 }
345 else
346 DrawEdge (hdc, &r, EDGE_RAISED,
347 BF_RECT | BF_SOFT | BF_MIDDLE | BF_ADJUST);
348 }
349 else
350 DrawEdge (hdc, &r, EDGE_ETCHED, BF_BOTTOM | BF_RIGHT | BF_ADJUST);
351
352 hbr = CreateSolidBrush(GetBkColor(hdc));
353 FillRect(hdc, &r, hbr);
354 DeleteObject(hbr);
355 }
356 if (phdi->bDown) {
357 r.left += 2;
358 r.top += 2;
359 }
360
361 if (phdi->fmt & HDF_OWNERDRAW) {
362 DRAWITEMSTRUCT dis;
363
364 dis.CtlType = ODT_HEADER;
365 dis.CtlID = GetWindowLongPtrW (hwnd, GWLP_ID);
366 dis.itemID = iItem;
367 dis.itemAction = ODA_DRAWENTIRE;
368 dis.itemState = phdi->bDown ? ODS_SELECTED : 0;
369 dis.hwndItem = hwnd;
370 dis.hDC = hdc;
371 dis.rcItem = phdi->rect;
372 dis.itemData = phdi->lParam;
373 oldBkMode = SetBkMode(hdc, TRANSPARENT);
374 SendMessageW (infoPtr->hwndNotify, WM_DRAWITEM,
375 (WPARAM)dis.CtlID, (LPARAM)&dis);
376 if (oldBkMode != TRANSPARENT)
377 SetBkMode(hdc, oldBkMode);
378 }
379 else {
380 UINT rw, rh, /* width and height of r */
381 *x = NULL, *w = NULL; /* x and width of the pic (bmp or img) which is part of cnt */
382 /* cnt,txt,img,bmp */
383 UINT cx, tx, ix, bx,
384 cw, tw, iw, bw;
385 BITMAP bmp;
386
387 HEADER_PrepareCallbackItems(hwnd, iItem, HDI_TEXT|HDI_IMAGE);
388 cw = tw = iw = bw = 0;
389 rw = r.right - r.left;
390 rh = r.bottom - r.top;
391
392 if (phdi->fmt & HDF_STRING) {
393 RECT textRect;
394
395 SetRectEmpty(&textRect);
396 DrawTextW (hdc, phdi->pszText, -1,
397 &textRect, DT_LEFT|DT_VCENTER|DT_SINGLELINE|DT_CALCRECT);
398 cw = textRect.right - textRect.left + 2 * infoPtr->iMargin;
399 }
400
401 if ((phdi->fmt & HDF_IMAGE) && (infoPtr->himl)) {
402 iw = infoPtr->himl->cx + 2 * infoPtr->iMargin;
403 x = &ix;
404 w = &iw;
405 }
406
407 if ((phdi->fmt & HDF_BITMAP) && (phdi->hbm)) {
408 GetObjectW (phdi->hbm, sizeof(BITMAP), (LPVOID)&bmp);
409 bw = bmp.bmWidth + 2 * infoPtr->iMargin;
410 if (!iw) {
411 x = &bx;
412 w = &bw;
413 }
414 }
415
416 if (bw || iw)
417 cw += *w;
418
419 /* align cx using the unclipped cw */
420 if ((phdi->fmt & HDF_JUSTIFYMASK) == HDF_LEFT)
421 cx = r.left;
422 else if ((phdi->fmt & HDF_JUSTIFYMASK) == HDF_CENTER)
423 cx = r.left + rw / 2 - cw / 2;
424 else /* HDF_RIGHT */
425 cx = r.right - cw;
426
427 /* clip cx & cw */
428 if (cx < r.left)
429 cx = r.left;
430 if (cx + cw > r.right)
431 cw = r.right - cx;
432
433 tx = cx + infoPtr->iMargin;
434 /* since cw might have changed we have to recalculate tw */
435 tw = cw - infoPtr->iMargin * 2;
436
437 if (iw || bw) {
438 tw -= *w;
439 if (phdi->fmt & HDF_BITMAP_ON_RIGHT) {
440 /* put pic behind text */
441 *x = cx + tw + infoPtr->iMargin * 3;
442 } else {
443 *x = cx + infoPtr->iMargin;
444 /* move text behind pic */
445 tx += *w;
446 }
447 }
448
449 if (iw && bw) {
450 /* since we're done with the layout we can
451 now calculate the position of bmp which
452 has no influence on alignment and layout
453 because of img */
454 if ((phdi->fmt & HDF_JUSTIFYMASK) == HDF_RIGHT)
455 bx = cx - bw + infoPtr->iMargin;
456 else
457 bx = cx + cw + infoPtr->iMargin;
458 }
459
460 if (iw || bw) {
461 HDC hClipDC = GetDC(hwnd);
462 HRGN hClipRgn = CreateRectRgn(r.left, r.top, r.right, r.bottom);
463 SelectClipRgn(hClipDC, hClipRgn);
464
465 if (bw) {
466 HDC hdcBitmap = CreateCompatibleDC (hClipDC);
467 SelectObject (hdcBitmap, phdi->hbm);
468 BitBlt (hClipDC, bx, r.top + ((INT)rh - bmp.bmHeight) / 2,
469 bmp.bmWidth, bmp.bmHeight, hdcBitmap, 0, 0, SRCCOPY);
470 DeleteDC (hdcBitmap);
471 }
472
473 if (iw) {
474 ImageList_DrawEx (infoPtr->himl, phdi->iImage, hClipDC,
475 ix, r.top + ((INT)rh - infoPtr->himl->cy) / 2,
476 infoPtr->himl->cx, infoPtr->himl->cy, CLR_DEFAULT, CLR_DEFAULT, 0);
477 }
478
479 DeleteObject(hClipRgn);
480 ReleaseDC(hwnd, hClipDC);
481 }
482
483 if (((phdi->fmt & HDF_STRING)
484 || (!(phdi->fmt & (HDF_OWNERDRAW|HDF_STRING|HDF_BITMAP|
485 HDF_BITMAP_ON_RIGHT|HDF_IMAGE)))) /* no explicit format specified? */
486 && (phdi->pszText)) {
487 oldBkMode = SetBkMode(hdc, TRANSPARENT);
488 r.left = tx;
489 r.right = tx + tw;
490 DrawTextW (hdc, phdi->pszText, -1,
491 &r, DT_LEFT|DT_END_ELLIPSIS|DT_VCENTER|DT_SINGLELINE);
492 if (oldBkMode != TRANSPARENT)
493 SetBkMode(hdc, oldBkMode);
494 }
495 HEADER_FreeCallbackItems(phdi);
496 }/*Ownerdrawn*/
497
498 return phdi->rect.right;
499 }
500
501 static void
502 HEADER_DrawHotDivider(HWND hwnd, HDC hdc)
503 {
504 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
505 HBRUSH brush;
506 RECT r;
507
508 HEADER_GetHotDividerRect(hwnd, infoPtr, &r);
509 brush = CreateSolidBrush(GetSysColor(COLOR_HIGHLIGHT));
510 FillRect(hdc, &r, brush);
511 DeleteObject(brush);
512 }
513
514 static void
515 HEADER_Refresh (HWND hwnd, HDC hdc)
516 {
517 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
518 HFONT hFont, hOldFont;
519 RECT rect, rcRest;
520 HBRUSH hbrBk;
521 UINT i;
522 INT x;
523 LRESULT lCDFlags;
524 HTHEME theme = GetWindowTheme (hwnd);
525
526 if (!infoPtr->bRectsValid)
527 HEADER_SetItemBounds(hwnd);
528
529 /* get rect for the bar, adjusted for the border */
530 GetClientRect (hwnd, &rect);
531 lCDFlags = HEADER_SendCtrlCustomDraw(hwnd, CDDS_PREPAINT, hdc, &rect);
532
533 if (infoPtr->bDragging)
534 ImageList_DragShowNolock(FALSE);
535
536 hFont = infoPtr->hFont ? infoPtr->hFont : GetStockObject (SYSTEM_FONT);
537 hOldFont = SelectObject (hdc, hFont);
538
539 /* draw Background */
540 if (infoPtr->uNumItem == 0 && theme == NULL) {
541 hbrBk = GetSysColorBrush(COLOR_3DFACE);
542 FillRect(hdc, &rect, hbrBk);
543 }
544
545 x = rect.left;
546 for (i = 0; x <= rect.right && i < infoPtr->uNumItem; i++) {
547 int idx = HEADER_OrderToIndex(hwnd,i);
548 if (RectVisible(hdc, &infoPtr->items[idx].rect))
549 HEADER_DrawItem(hwnd, hdc, idx, infoPtr->iHotItem == idx, lCDFlags);
550 x = infoPtr->items[idx].rect.right;
551 }
552
553 rcRest = rect;
554 rcRest.left = x;
555 if ((x <= rect.right) && RectVisible(hdc, &rcRest) && (infoPtr->uNumItem > 0)) {
556 if (theme != NULL) {
557 DrawThemeBackground(theme, hdc, HP_HEADERITEM, HIS_NORMAL, &rcRest, NULL);
558 }
559 else {
560 if (GetWindowLongW (hwnd, GWL_STYLE) & HDS_BUTTONS)
561 DrawEdge (hdc, &rcRest, EDGE_RAISED, BF_TOP|BF_LEFT|BF_BOTTOM|BF_SOFT|BF_MIDDLE);
562 else
563 DrawEdge (hdc, &rcRest, EDGE_ETCHED, BF_BOTTOM|BF_MIDDLE);
564 }
565 }
566
567 if (infoPtr->iHotDivider != -1)
568 HEADER_DrawHotDivider(hwnd, hdc);
569
570 if (infoPtr->bDragging)
571 ImageList_DragShowNolock(TRUE);
572 SelectObject (hdc, hOldFont);
573
574 if (lCDFlags & CDRF_NOTIFYPOSTPAINT)
575 HEADER_SendCtrlCustomDraw(hwnd, CDDS_POSTPAINT, hdc, &rect);
576 }
577
578
579 static void
580 HEADER_RefreshItem (HWND hwnd, HDC hdc, INT iItem)
581 {
582 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
583
584 if (!infoPtr->bRectsValid)
585 HEADER_SetItemBounds(hwnd);
586
587 InvalidateRect(hwnd, &infoPtr->items[iItem].rect, FALSE);
588 }
589
590
591 static void
592 HEADER_InternalHitTest (HWND hwnd, const POINT *lpPt, UINT *pFlags, INT *pItem)
593 {
594 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
595 RECT rect, rcTest;
596 UINT iCount;
597 INT width;
598 BOOL bNoWidth;
599
600 GetClientRect (hwnd, &rect);
601
602 *pFlags = 0;
603 bNoWidth = FALSE;
604 if (PtInRect (&rect, *lpPt))
605 {
606 if (infoPtr->uNumItem == 0) {
607 *pFlags |= HHT_NOWHERE;
608 *pItem = 1;
609 TRACE("NOWHERE\n");
610 return;
611 }
612 else {
613 /* somewhere inside */
614 for (iCount = 0; iCount < infoPtr->uNumItem; iCount++) {
615 rect = infoPtr->items[iCount].rect;
616 width = rect.right - rect.left;
617 if (width == 0) {
618 bNoWidth = TRUE;
619 continue;
620 }
621 if (PtInRect (&rect, *lpPt)) {
622 if (width <= 2 * DIVIDER_WIDTH) {
623 *pFlags |= HHT_ONHEADER;
624 *pItem = iCount;
625 TRACE("ON HEADER %d\n", iCount);
626 return;
627 }
628 if (HEADER_IndexToOrder(hwnd, iCount) > 0) {
629 rcTest = rect;
630 rcTest.right = rcTest.left + DIVIDER_WIDTH;
631 if (PtInRect (&rcTest, *lpPt)) {
632 if (bNoWidth) {
633 *pFlags |= HHT_ONDIVOPEN;
634 *pItem = HEADER_PrevItem(hwnd, iCount);
635 TRACE("ON DIVOPEN %d\n", *pItem);
636 return;
637 }
638 else {
639 *pFlags |= HHT_ONDIVIDER;
640 *pItem = HEADER_PrevItem(hwnd, iCount);
641 TRACE("ON DIVIDER %d\n", *pItem);
642 return;
643 }
644 }
645 }
646 rcTest = rect;
647 rcTest.left = rcTest.right - DIVIDER_WIDTH;
648 if (PtInRect (&rcTest, *lpPt)) {
649 *pFlags |= HHT_ONDIVIDER;
650 *pItem = iCount;
651 TRACE("ON DIVIDER %d\n", *pItem);
652 return;
653 }
654
655 *pFlags |= HHT_ONHEADER;
656 *pItem = iCount;
657 TRACE("ON HEADER %d\n", iCount);
658 return;
659 }
660 }
661
662 /* check for last divider part (on nowhere) */
663 rect = infoPtr->items[infoPtr->uNumItem-1].rect;
664 rect.left = rect.right;
665 rect.right += DIVIDER_WIDTH;
666 if (PtInRect (&rect, *lpPt)) {
667 if (bNoWidth) {
668 *pFlags |= HHT_ONDIVOPEN;
669 *pItem = infoPtr->uNumItem - 1;
670 TRACE("ON DIVOPEN %d\n", *pItem);
671 return;
672 }
673 else {
674 *pFlags |= HHT_ONDIVIDER;
675 *pItem = infoPtr->uNumItem-1;
676 TRACE("ON DIVIDER %d\n", *pItem);
677 return;
678 }
679 }
680
681 *pFlags |= HHT_NOWHERE;
682 *pItem = 1;
683 TRACE("NOWHERE\n");
684 return;
685 }
686 }
687 else {
688 if (lpPt->x < rect.left) {
689 TRACE("TO LEFT\n");
690 *pFlags |= HHT_TOLEFT;
691 }
692 else if (lpPt->x > rect.right) {
693 TRACE("TO RIGHT\n");
694 *pFlags |= HHT_TORIGHT;
695 }
696
697 if (lpPt->y < rect.top) {
698 TRACE("ABOVE\n");
699 *pFlags |= HHT_ABOVE;
700 }
701 else if (lpPt->y > rect.bottom) {
702 TRACE("BELOW\n");
703 *pFlags |= HHT_BELOW;
704 }
705 }
706
707 *pItem = 1;
708 TRACE("flags=0x%X\n", *pFlags);
709 return;
710 }
711
712
713 static void
714 HEADER_DrawTrackLine (HWND hwnd, HDC hdc, INT x)
715 {
716 RECT rect;
717 HPEN hOldPen;
718 INT oldRop;
719
720 GetClientRect (hwnd, &rect);
721
722 hOldPen = SelectObject (hdc, GetStockObject (BLACK_PEN));
723 oldRop = SetROP2 (hdc, R2_XORPEN);
724 MoveToEx (hdc, x, rect.top, NULL);
725 LineTo (hdc, x, rect.bottom);
726 SetROP2 (hdc, oldRop);
727 SelectObject (hdc, hOldPen);
728 }
729
730 /***
731 * DESCRIPTION:
732 * Convert a HDITEM into the correct format (ANSI/Unicode) to send it in a notify
733 *
734 * PARAMETER(S):
735 * [I] infoPtr : the header that wants to send the notify
736 * [O] dest : The buffer to store the HDITEM for notify. It may be set to a HDITEMA of HDITEMW
737 * [I] src : The source HDITEM. It may be a HDITEMA or HDITEMW
738 * [I] fSourceUnicode : is src a HDITEMW or HDITEMA
739 * [O] ppvScratch : a pointer to a scratch buffer that needs to be freed after
740 * the HDITEM is no longer in use or NULL if none was needed
741 *
742 * NOTE: We depend on HDITEMA and HDITEMW having the same structure
743 */
744 static void HEADER_CopyHDItemForNotify(const HEADER_INFO *infoPtr, HDITEMW *dest,
745 const HDITEMW *src, BOOL fSourceUnicode, LPVOID *ppvScratch)
746 {
747 *ppvScratch = NULL;
748 *dest = *src;
749
750 if (src->mask & HDI_TEXT && src->pszText != LPSTR_TEXTCALLBACKW) /* covers TEXTCALLBACKA as well */
751 {
752 if (fSourceUnicode && infoPtr->nNotifyFormat != NFR_UNICODE)
753 {
754 dest->pszText = NULL;
755 Str_SetPtrWtoA((LPSTR *)&dest->pszText, src->pszText);
756 *ppvScratch = dest->pszText;
757 }
758
759 if (!fSourceUnicode && infoPtr->nNotifyFormat == NFR_UNICODE)
760 {
761 dest->pszText = NULL;
762 Str_SetPtrAtoW(&dest->pszText, (LPSTR)src->pszText);
763 *ppvScratch = dest->pszText;
764 }
765 }
766 }
767
768 static UINT HEADER_NotifyCodeWtoA(UINT code)
769 {
770 /* we use the fact that all the unicode messages are in HDN_FIRST_UNICODE..HDN_LAST*/
771 if (code >= HDN_LAST && code <= HDN_FIRST_UNICODE)
772 return code + HDN_UNICODE_OFFSET;
773 else
774 return code;
775 }
776
777 static LRESULT
778 HEADER_SendNotify(HWND hwnd, UINT code, NMHDR *nmhdr)
779 {
780 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
781
782 nmhdr->hwndFrom = hwnd;
783 nmhdr->idFrom = GetWindowLongPtrW (hwnd, GWLP_ID);
784 nmhdr->code = code;
785
786 return SendMessageW(infoPtr->hwndNotify, WM_NOTIFY,
787 nmhdr->idFrom, (LPARAM)nmhdr);
788 }
789
790 static BOOL
791 HEADER_SendSimpleNotify (HWND hwnd, UINT code)
792 {
793 NMHDR nmhdr;
794 return (BOOL)HEADER_SendNotify(hwnd, code, &nmhdr);
795 }
796
797 static LRESULT
798 HEADER_SendCtrlCustomDraw(HWND hwnd, DWORD dwDrawStage, HDC hdc, const RECT *rect)
799 {
800 NMCUSTOMDRAW nm;
801 nm.dwDrawStage = dwDrawStage;
802 nm.hdc = hdc;
803 nm.rc = *rect;
804 nm.dwItemSpec = 0;
805 nm.uItemState = 0;
806 nm.lItemlParam = 0;
807
808 return HEADER_SendNotify(hwnd, NM_CUSTOMDRAW, (NMHDR *)&nm);
809 }
810
811 static BOOL
812 HEADER_SendNotifyWithHDItemT(HWND hwnd, UINT code, INT iItem, HDITEMW *lpItem)
813 {
814 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
815 NMHEADERW nmhdr;
816
817 if (infoPtr->nNotifyFormat != NFR_UNICODE)
818 code = HEADER_NotifyCodeWtoA(code);
819 nmhdr.iItem = iItem;
820 nmhdr.iButton = 0;
821 nmhdr.pitem = lpItem;
822
823 return (BOOL)HEADER_SendNotify(hwnd, code, (NMHDR *)&nmhdr);
824 }
825
826 static BOOL
827 HEADER_SendNotifyWithIntFieldT(HWND hwnd, UINT code, INT iItem, INT mask, INT iValue)
828 {
829 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
830 HDITEMW nmitem;
831
832 /* copying only the iValue should be ok but to make the code more robust we copy everything */
833 nmitem.cxy = infoPtr->items[iItem].cxy;
834 nmitem.hbm = infoPtr->items[iItem].hbm;
835 nmitem.pszText = NULL;
836 nmitem.cchTextMax = 0;
837 nmitem.fmt = infoPtr->items[iItem].fmt;
838 nmitem.lParam = infoPtr->items[iItem].lParam;
839 nmitem.iOrder = infoPtr->items[iItem].iOrder;
840 nmitem.iImage = infoPtr->items[iItem].iImage;
841
842 nmitem.mask = mask;
843 switch (mask)
844 {
845 case HDI_WIDTH:
846 nmitem.cxy = iValue;
847 break;
848 case HDI_ORDER:
849 nmitem.iOrder = iValue;
850 break;
851 default:
852 ERR("invalid mask value 0x%x\n", iValue);
853 }
854
855 return HEADER_SendNotifyWithHDItemT(hwnd, code, iItem, &nmitem);
856 }
857
858 /**
859 * Prepare callback items
860 * depends on NMHDDISPINFOW having same structure as NMHDDISPINFOA
861 * (so we handle the two cases only doing a specific cast for pszText).
862 * Checks if any of the required field are callback. If there are sends a
863 * NMHDISPINFO notify to retrieve these items. The items are stored in the
864 * HEADER_ITEM pszText and iImage fields. They should be freed with
865 * HEADER_FreeCallbackItems.
866 *
867 * @param hwnd : hwnd header container handler
868 * @param iItem : the header item id
869 * @param reqMask : required fields. If any of them is callback this function will fetch it
870 *
871 * @return TRUE on success, else FALSE
872 */
873 static BOOL
874 HEADER_PrepareCallbackItems(HWND hwnd, INT iItem, INT reqMask)
875 {
876 HEADER_INFO *infoPtr = HEADER_GetInfoPtr (hwnd);
877 HEADER_ITEM *lpItem = &infoPtr->items[iItem];
878 DWORD mask = reqMask & lpItem->callbackMask;
879 NMHDDISPINFOW dispInfo;
880 void *pvBuffer = NULL;
881
882 if (mask == 0)
883 return TRUE;
884 if (mask&HDI_TEXT && lpItem->pszText != NULL)
885 {
886 ERR("(): function called without a call to FreeCallbackItems\n");
887 Free(lpItem->pszText);
888 lpItem->pszText = NULL;
889 }
890
891 memset(&dispInfo, 0, sizeof(NMHDDISPINFOW));
892 dispInfo.hdr.hwndFrom = hwnd;
893 dispInfo.hdr.idFrom = GetWindowLongPtrW (hwnd, GWLP_ID);
894 if (infoPtr->nNotifyFormat == NFR_UNICODE)
895 {
896 dispInfo.hdr.code = HDN_GETDISPINFOW;
897 if (mask & HDI_TEXT)
898 pvBuffer = Alloc(MAX_HEADER_TEXT_LEN * sizeof(WCHAR));
899 }
900 else
901 {
902 dispInfo.hdr.code = HDN_GETDISPINFOA;
903 if (mask & HDI_TEXT)
904 pvBuffer = Alloc(MAX_HEADER_TEXT_LEN * sizeof(CHAR));
905 }
906 dispInfo.pszText = (LPWSTR)pvBuffer;
907 dispInfo.cchTextMax = (pvBuffer!=NULL?MAX_HEADER_TEXT_LEN:0);
908 dispInfo.iItem = iItem;
909 dispInfo.mask = mask;
910 dispInfo.lParam = lpItem->lParam;
911
912 TRACE("Sending HDN_GETDISPINFO%c\n", infoPtr->nNotifyFormat == NFR_UNICODE?'W':'A');
913 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, dispInfo.hdr.idFrom, (LPARAM)&dispInfo);
914
915 TRACE("SendMessage returns(mask:0x%x,str:%s,lParam:%p)\n",
916 dispInfo.mask,
917 (infoPtr->nNotifyFormat == NFR_UNICODE ? debugstr_w(dispInfo.pszText) : (LPSTR) dispInfo.pszText),
918 (void*) dispInfo.lParam);
919
920 if (mask & HDI_IMAGE)
921 lpItem->iImage = dispInfo.iImage;
922 if (mask & HDI_TEXT)
923 {
924 if (infoPtr->nNotifyFormat == NFR_UNICODE)
925 {
926 lpItem->pszText = (LPWSTR)pvBuffer;
927
928 /* the user might have used his own buffer */
929 if (dispInfo.pszText != lpItem->pszText)
930 Str_GetPtrW(dispInfo.pszText, lpItem->pszText, MAX_HEADER_TEXT_LEN);
931 }
932 else
933 {
934