4 * Copyright 1998, 1999 Eric Kohl
5 * Copyright 1999 Luc Tourangeau
6 * Copyright 2000 Jason Mawdsley
7 * Copyright 2001 CodeWeavers Inc.
8 * Copyright 2002 Dimitrie O. Paun
9 * Copyright 2009-2013 Nikolay Sivov
10 * Copyright 2009 Owen Rudge for CodeWeavers
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; either
15 * version 2.1 of the License, or (at your option) any later version.
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
28 * This code was audited for completeness against the documented features
29 * of Comctl32.dll version 6.0 on May. 20, 2005, by James Hawkins.
31 * Unless otherwise noted, we believe this code to be complete, as per
32 * the specification mentioned above.
33 * If you discover missing features, or bugs, please note them below.
37 * Default Message Processing
38 * -- WM_CREATE: create the icon and small icon image lists at this point only if
39 * the LVS_SHAREIMAGELISTS style is not specified.
40 * -- WM_WINDOWPOSCHANGED: arrange the list items if the current view is icon
41 * or small icon and the LVS_AUTOARRANGE style is specified.
46 * -- Hot item handling, mouse hovering
47 * -- Workareas support
52 * -- Expand large item in ICON mode when the cursor is flying over the icon or text.
53 * -- Support CustomDraw options for _WIN32_IE >= 0x560 (see NMLVCUSTOMDRAW docs).
54 * -- LVA_SNAPTOGRID not implemented
55 * -- LISTVIEW_ApproximateViewRect partially implemented
56 * -- LISTVIEW_SetColumnWidth ignores header images & bitmap
57 * -- LISTVIEW_SetIconSpacing is incomplete
58 * -- LISTVIEW_StyleChanged doesn't handle some changes too well
61 * -- LISTVIEW_GetNextItem needs to be rewritten. It is currently
62 * linear in the number of items in the list, and this is
63 * unacceptable for large lists.
64 * -- if list is sorted by item text LISTVIEW_InsertItemT could use
65 * binary search to calculate item index (e.g. DPA_Search()).
66 * This requires sorted state to be reliably tracked in item modifiers.
67 * -- we should keep an ordered array of coordinates in iconic mode
68 * this would allow to frame items (iterator_frameditems),
69 * and find nearest item (LVFI_NEARESTXY) a lot more efficiently
76 * -- LVIS_ACTIVATING (not currently supported by comctl32.dll version 6.0)
82 * -- LVS_NOSCROLL (see Q137520)
86 * -- LVS_EX_BORDERSELECT
90 * -- LVS_EX_MULTIWORKAREAS
92 * -- LVS_EX_SIMPLESELECT
93 * -- LVS_EX_TWOCLICKACTIVATE
94 * -- LVS_EX_UNDERLINECOLD
95 * -- LVS_EX_UNDERLINEHOT
98 * -- LVN_BEGINSCROLL, LVN_ENDSCROLL
105 * -- LVM_ENABLEGROUPVIEW
106 * -- LVM_GETBKIMAGE, LVM_SETBKIMAGE
107 * -- LVM_GETGROUPINFO, LVM_SETGROUPINFO
108 * -- LVM_GETGROUPMETRICS, LVM_SETGROUPMETRICS
109 * -- LVM_GETINSERTMARK, LVM_SETINSERTMARK
110 * -- LVM_GETINSERTMARKCOLOR, LVM_SETINSERTMARKCOLOR
111 * -- LVM_GETINSERTMARKRECT
112 * -- LVM_GETNUMBEROFWORKAREAS
113 * -- LVM_GETOUTLINECOLOR, LVM_SETOUTLINECOLOR
114 * -- LVM_GETSELECTEDCOLUMN, LVM_SETSELECTEDCOLUMN
115 * -- LVM_GETISEARCHSTRINGW, LVM_GETISEARCHSTRINGA
116 * -- LVM_GETTILEINFO, LVM_SETTILEINFO
117 * -- LVM_GETTILEVIEWINFO, LVM_SETTILEVIEWINFO
118 * -- LVM_GETWORKAREAS, LVM_SETWORKAREAS
119 * -- LVM_HASGROUP, LVM_INSERTGROUP, LVM_REMOVEGROUP, LVM_REMOVEALLGROUPS
120 * -- LVM_INSERTGROUPSORTED
121 * -- LVM_INSERTMARKHITTEST
122 * -- LVM_ISGROUPVIEWENABLED
124 * -- LVM_MOVEITEMTOGROUP
126 * -- LVM_SETTILEWIDTH
130 * -- ListView_GetHoverTime, ListView_SetHoverTime
131 * -- ListView_GetISearchString
132 * -- ListView_GetNumberOfWorkAreas
133 * -- ListView_GetWorkAreas, ListView_SetWorkAreas
138 * Known differences in message stream from native control (not known if
139 * these differences cause problems):
140 * LVM_INSERTITEM issues LVM_SETITEMSTATE and LVM_SETITEM in certain cases.
141 * LVM_SETITEM does not always issue LVN_ITEMCHANGING/LVN_ITEMCHANGED.
142 * WM_CREATE does not issue WM_QUERYUISTATE and associated registry
143 * processing for "USEDOUBLECLICKTIME".
147 #include "wine/port.h"
162 #include "commctrl.h"
163 #include "comctl32.h"
166 #include "wine/debug.h"
167 #include "wine/unicode.h"
169 WINE_DEFAULT_DEBUG_CHANNEL(listview);
171 typedef struct tagCOLUMN_INFO
173 RECT rcHeader; /* tracks the header's rectangle */
174 INT fmt; /* same as LVCOLUMN.fmt */
178 typedef struct tagITEMHDR
182 } ITEMHDR, *LPITEMHDR;
184 typedef struct tagSUBITEM_INFO
190 typedef struct tagITEM_ID ITEM_ID;
192 typedef struct tagITEM_INFO
203 UINT id; /* item id */
204 HDPA item; /* link to item data */
207 typedef struct tagRANGE
213 typedef struct tagRANGES
218 typedef struct tagITERATOR
227 typedef struct tagDELAYED_ITEM_EDIT
233 typedef struct tagLISTVIEW_INFO
237 RECT rcList; /* This rectangle is really the window
238 * client rectangle possibly reduced by the
239 * horizontal scroll bar and/or header - see
240 * LISTVIEW_UpdateSize. This rectangle offset
241 * by the LISTVIEW_GetOrigin value is in
242 * client coordinates */
244 /* notification window */
247 BOOL bDoChangeNotify; /* send change notification messages? */
254 INT nItemCount; /* the number of items in the list */
255 HDPA hdpaItems; /* array ITEM_INFO pointers */
256 HDPA hdpaItemIds; /* array of ITEM_ID pointers */
257 HDPA hdpaPosX; /* maintains the (X, Y) coordinates of the */
258 HDPA hdpaPosY; /* items in LVS_ICON, and LVS_SMALLICON modes */
259 RANGES selectionRanges;
260 INT nSelectionMark; /* item to start next multiselection from */
262 BOOL bAutoarrange; /* Autoarrange flag when NOT in LVS_AUTOARRANGE */
265 HDPA hdpaColumns; /* array of COLUMN_INFO pointers */
266 BOOL colRectsDirty; /* trigger column rectangles requery from header */
269 BOOL bNoItemMetrics; /* flags if item metrics are not yet computed */
274 PFNLVCOMPARE pfnCompare; /* sorting callback pointer */
278 DWORD dwStyle; /* the cached window GWL_STYLE */
279 DWORD dwLvExStyle; /* extended listview style */
280 DWORD uView; /* current view available through LVM_[G,S]ETVIEW */
286 DELAYED_ITEM_EDIT itemEdit; /* Pointer to this structure will be the timer ID */
289 HIMAGELIST himlNormal;
290 HIMAGELIST himlSmall;
291 HIMAGELIST himlState;
295 POINT currIconPos; /* this is the position next icon will be placed */
299 INT xTrackLine; /* The x coefficient of the track line or -1 if none */
301 /* marquee selection */
302 BOOL bMarqueeSelect; /* marquee selection/highlight underway */
304 RECT marqueeRect; /* absolute coordinates of marquee selection */
305 RECT marqueeDrawRect; /* relative coordinates for drawing marquee */
306 POINT marqueeOrigin; /* absolute coordinates of marquee click origin */
309 BOOL bFocus; /* control has focus */
311 RECT rcFocus; /* focus bounds */
322 INT ntmHeight; /* Some cached metrics of the font used */
323 INT ntmMaxCharWidth; /* by the listview to draw items */
326 /* mouse operation */
330 POINT ptClickPos; /* point where the user clicked */
331 INT nLButtonDownItem; /* tracks item to reset multiselection on WM_LBUTTONUP */
335 /* keyboard operation */
336 DWORD lastKeyPressTimestamp;
338 INT nSearchParamLength;
339 WCHAR szSearchParam[ MAX_PATH ];
342 DWORD cditemmode; /* Keep the custom draw flags for an item/row */
343 BOOL bIsDrawing; /* Drawing in progress */
344 INT nMeasureItemHeight; /* WM_MEASUREITEM result */
345 BOOL bRedraw; /* WM_SETREDRAW switch */
348 DWORD iVersion; /* CCM_[G,S]ETVERSION */
354 /* How many we debug buffer to allocate */
355 #define DEBUG_BUFFERS 20
356 /* The size of a single debug buffer */
357 #define DEBUG_BUFFER_SIZE 256
359 /* Internal interface to LISTVIEW_HScroll and LISTVIEW_VScroll */
360 #define SB_INTERNAL -1
362 /* maximum size of a label */
363 #define DISP_TEXT_SIZE 260
365 /* padding for items in list and small icon display modes */
366 #define WIDTH_PADDING 12
368 /* padding for items in list, report and small icon display modes */
369 #define HEIGHT_PADDING 1
371 /* offset of items in report display mode */
372 #define REPORT_MARGINX 2
374 /* padding for icon in large icon display mode
375 * ICON_TOP_PADDING_NOTHITABLE - space between top of box and area
376 * that HITTEST will see.
377 * ICON_TOP_PADDING_HITABLE - spacing between above and icon.
378 * ICON_TOP_PADDING - sum of the two above.
379 * ICON_BOTTOM_PADDING - between bottom of icon and top of text
380 * LABEL_HOR_PADDING - between text and sides of box
381 * LABEL_VERT_PADDING - between bottom of text and end of box
383 * ICON_LR_PADDING - additional width above icon size.
384 * ICON_LR_HALF - half of the above value
386 #define ICON_TOP_PADDING_NOTHITABLE 2
387 #define ICON_TOP_PADDING_HITABLE 2
388 #define ICON_TOP_PADDING (ICON_TOP_PADDING_NOTHITABLE + ICON_TOP_PADDING_HITABLE)
389 #define ICON_BOTTOM_PADDING 4
390 #define LABEL_HOR_PADDING 5
391 #define LABEL_VERT_PADDING 7
392 #define ICON_LR_PADDING 16
393 #define ICON_LR_HALF (ICON_LR_PADDING/2)
395 /* default label width for items in list and small icon display modes */
396 #define DEFAULT_LABEL_WIDTH 40
397 /* maximum select rectangle width for empty text item in LV_VIEW_DETAILS */
398 #define MAX_EMPTYTEXT_SELECT_WIDTH 80
400 /* default column width for items in list display mode */
401 #define DEFAULT_COLUMN_WIDTH 128
403 /* Size of "line" scroll for V & H scrolls */
404 #define LISTVIEW_SCROLL_ICON_LINE_SIZE 37
406 /* Padding between image and label */
407 #define IMAGE_PADDING 2
409 /* Padding behind the label */
410 #define TRAILING_LABEL_PADDING 12
411 #define TRAILING_HEADER_PADDING 11
413 /* Border for the icon caption */
414 #define CAPTION_BORDER 2
416 /* Standard DrawText flags */
417 #define LV_ML_DT_FLAGS (DT_TOP | DT_NOPREFIX | DT_EDITCONTROL | DT_CENTER | DT_WORDBREAK | DT_WORD_ELLIPSIS | DT_END_ELLIPSIS)
418 #define LV_FL_DT_FLAGS (DT_TOP | DT_NOPREFIX | DT_EDITCONTROL | DT_CENTER | DT_WORDBREAK | DT_NOCLIP)
419 #define LV_SL_DT_FLAGS (DT_VCENTER | DT_NOPREFIX | DT_EDITCONTROL | DT_SINGLELINE | DT_WORD_ELLIPSIS | DT_END_ELLIPSIS)
421 /* Image index from state */
422 #define STATEIMAGEINDEX(x) (((x) & LVIS_STATEIMAGEMASK) >> 12)
424 /* The time in milliseconds to reset the search in the list */
425 #define KEY_DELAY 450
427 /* Dump the LISTVIEW_INFO structure to the debug channel */
428 #define LISTVIEW_DUMP(iP) do { \
429 TRACE("hwndSelf=%p, clrBk=0x%06x, clrText=0x%06x, clrTextBk=0x%06x, ItemHeight=%d, ItemWidth=%d, Style=0x%08x\n", \
430 iP->hwndSelf, iP->clrBk, iP->clrText, iP->clrTextBk, \
431 iP->nItemHeight, iP->nItemWidth, iP->dwStyle); \
432 TRACE("hwndSelf=%p, himlNor=%p, himlSml=%p, himlState=%p, Focused=%d, Hot=%d, exStyle=0x%08x, Focus=%d\n", \
433 iP->hwndSelf, iP->himlNormal, iP->himlSmall, iP->himlState, \
434 iP->nFocusedItem, iP->nHotItem, iP->dwLvExStyle, iP->bFocus ); \
435 TRACE("hwndSelf=%p, ntmH=%d, icSz.cx=%d, icSz.cy=%d, icSp.cx=%d, icSp.cy=%d, notifyFmt=%d\n", \
436 iP->hwndSelf, iP->ntmHeight, iP->iconSize.cx, iP->iconSize.cy, \
437 iP->iconSpacing.cx, iP->iconSpacing.cy, iP->notifyFormat); \
438 TRACE("hwndSelf=%p, rcList=%s\n", iP->hwndSelf, wine_dbgstr_rect(&iP->rcList)); \
441 static const WCHAR themeClass[] = {'L','i','s','t','V','i','e','w',0};
444 * forward declarations
446 static BOOL LISTVIEW_GetItemT(const LISTVIEW_INFO *, LPLVITEMW, BOOL);
447 static void LISTVIEW_GetItemBox(const LISTVIEW_INFO *, INT, LPRECT);
448 static void LISTVIEW_GetItemOrigin(const LISTVIEW_INFO *, INT, LPPOINT);
449 static BOOL LISTVIEW_GetItemPosition(const LISTVIEW_INFO *, INT, LPPOINT);
450 static BOOL LISTVIEW_GetItemRect(const LISTVIEW_INFO *, INT, LPRECT);
451 static void LISTVIEW_GetOrigin(const LISTVIEW_INFO *, LPPOINT);
452 static BOOL LISTVIEW_GetViewRect(const LISTVIEW_INFO *, LPRECT);
453 static void LISTVIEW_UpdateSize(LISTVIEW_INFO *);
454 static LRESULT LISTVIEW_Command(LISTVIEW_INFO *, WPARAM, LPARAM);
455 static INT LISTVIEW_GetStringWidthT(const LISTVIEW_INFO *, LPCWSTR, BOOL);
456 static BOOL LISTVIEW_KeySelection(LISTVIEW_INFO *, INT, BOOL);
457 static UINT LISTVIEW_GetItemState(const LISTVIEW_INFO *, INT, UINT);
458 static BOOL LISTVIEW_SetItemState(LISTVIEW_INFO *, INT, const LVITEMW *);
459 static LRESULT LISTVIEW_VScroll(LISTVIEW_INFO *, INT, INT);
460 static LRESULT LISTVIEW_HScroll(LISTVIEW_INFO *, INT, INT);
461 static BOOL LISTVIEW_EnsureVisible(LISTVIEW_INFO *, INT, BOOL);
462 static HIMAGELIST LISTVIEW_SetImageList(LISTVIEW_INFO *, INT, HIMAGELIST);
463 static INT LISTVIEW_HitTest(const LISTVIEW_INFO *, LPLVHITTESTINFO, BOOL, BOOL);
464 static BOOL LISTVIEW_EndEditLabelT(LISTVIEW_INFO *, BOOL, BOOL);
465 static BOOL LISTVIEW_Scroll(LISTVIEW_INFO *, INT, INT);
467 /******** Text handling functions *************************************/
469 /* A text pointer is either NULL, LPSTR_TEXTCALLBACK, or points to a
470 * text string. The string may be ANSI or Unicode, in which case
471 * the boolean isW tells us the type of the string.
473 * The name of the function tell what type of strings it expects:
474 * W: Unicode, T: ANSI/Unicode - function of isW
477 static inline BOOL is_text(LPCWSTR text)
479 return text != NULL && text != LPSTR_TEXTCALLBACKW;
482 static inline int textlenT(LPCWSTR text, BOOL isW)
484 return !is_text(text) ? 0 :
485 isW ? lstrlenW(text) : lstrlenA((LPCSTR)text);
488 static inline void textcpynT(LPWSTR dest, BOOL isDestW, LPCWSTR src, BOOL isSrcW, INT max)
491 if (isSrcW) lstrcpynW(dest, src, max);
492 else MultiByteToWideChar(CP_ACP, 0, (LPCSTR)src, -1, dest, max);
494 if (isSrcW) WideCharToMultiByte(CP_ACP, 0, src, -1, (LPSTR)dest, max, NULL, NULL);
495 else lstrcpynA((LPSTR)dest, (LPCSTR)src, max);
498 static inline LPWSTR textdupTtoW(LPCWSTR text, BOOL isW)
500 LPWSTR wstr = (LPWSTR)text;
502 if (!isW && is_text(text))
504 INT len = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)text, -1, NULL, 0);
505 wstr = Alloc(len * sizeof(WCHAR));
506 if (wstr) MultiByteToWideChar(CP_ACP, 0, (LPCSTR)text, -1, wstr, len);
508 TRACE(" wstr=%s\n", text == LPSTR_TEXTCALLBACKW ? "(callback)" : debugstr_w(wstr));
512 static inline void textfreeT(LPWSTR wstr, BOOL isW)
514 if (!isW && is_text(wstr)) Free (wstr);
518 * dest is a pointer to a Unicode string
519 * src is a pointer to a string (Unicode if isW, ANSI if !isW)
521 static BOOL textsetptrT(LPWSTR *dest, LPCWSTR src, BOOL isW)
525 if (src == LPSTR_TEXTCALLBACKW)
527 if (is_text(*dest)) Free(*dest);
528 *dest = LPSTR_TEXTCALLBACKW;
532 LPWSTR pszText = textdupTtoW(src, isW);
533 if (*dest == LPSTR_TEXTCALLBACKW) *dest = NULL;
534 bResult = Str_SetPtrW(dest, pszText);
535 textfreeT(pszText, isW);
541 * compares a Unicode to a Unicode/ANSI text string
543 static inline int textcmpWT(LPCWSTR aw, LPCWSTR bt, BOOL isW)
545 if (!aw) return bt ? -1 : 0;
547 if (aw == LPSTR_TEXTCALLBACKW)
548 return bt == LPSTR_TEXTCALLBACKW ? 1 : -1;
549 if (bt != LPSTR_TEXTCALLBACKW)
551 LPWSTR bw = textdupTtoW(bt, isW);
552 int r = bw ? lstrcmpW(aw, bw) : 1;
560 static inline int lstrncmpiW(LPCWSTR s1, LPCWSTR s2, int n)
562 n = min(min(n, lstrlenW(s1)), lstrlenW(s2));
563 return CompareStringW(LOCALE_USER_DEFAULT, NORM_IGNORECASE, s1, n, s2, n) - CSTR_EQUAL;
566 /******** Debugging functions *****************************************/
568 static inline LPCSTR debugtext_t(LPCWSTR text, BOOL isW)
570 if (text == LPSTR_TEXTCALLBACKW) return "(callback)";
571 return isW ? debugstr_w(text) : debugstr_a((LPCSTR)text);
574 static inline LPCSTR debugtext_tn(LPCWSTR text, BOOL isW, INT n)
576 if (text == LPSTR_TEXTCALLBACKW) return "(callback)";
577 n = min(textlenT(text, isW), n);
578 return isW ? debugstr_wn(text, n) : debugstr_an((LPCSTR)text, n);
581 static char* debug_getbuf(void)
583 static int index = 0;
584 static char buffers[DEBUG_BUFFERS][DEBUG_BUFFER_SIZE];
585 return buffers[index++ % DEBUG_BUFFERS];
588 static inline const char* debugrange(const RANGE *lprng)
590 if (!lprng) return "(null)";
591 return wine_dbg_sprintf("[%d, %d]", lprng->lower, lprng->upper);
594 static const char* debugscrollinfo(const SCROLLINFO *pScrollInfo)
596 char* buf = debug_getbuf(), *text = buf;
597 int len, size = DEBUG_BUFFER_SIZE;
599 if (pScrollInfo == NULL) return "(null)";
600 len = snprintf(buf, size, "{cbSize=%d, ", pScrollInfo->cbSize);
601 if (len == -1) goto end; buf += len; size -= len;
602 if (pScrollInfo->fMask & SIF_RANGE)
603 len = snprintf(buf, size, "nMin=%d, nMax=%d, ", pScrollInfo->nMin, pScrollInfo->nMax);
605 if (len == -1) goto end; buf += len; size -= len;
606 if (pScrollInfo->fMask & SIF_PAGE)
607 len = snprintf(buf, size, "nPage=%u, ", pScrollInfo->nPage);
609 if (len == -1) goto end; buf += len; size -= len;
610 if (pScrollInfo->fMask & SIF_POS)
611 len = snprintf(buf, size, "nPos=%d, ", pScrollInfo->nPos);
613 if (len == -1) goto end; buf += len; size -= len;
614 if (pScrollInfo->fMask & SIF_TRACKPOS)
615 len = snprintf(buf, size, "nTrackPos=%d, ", pScrollInfo->nTrackPos);
617 if (len == -1) goto end; buf += len;
620 buf = text + strlen(text);
622 if (buf - text > 2) { buf[-2] = '}'; buf[-1] = 0; }
626 static const char* debugnmlistview(const NMLISTVIEW *plvnm)
628 if (!plvnm) return "(null)";
629 return wine_dbg_sprintf("iItem=%d, iSubItem=%d, uNewState=0x%x,"
630 " uOldState=0x%x, uChanged=0x%x, ptAction=%s, lParam=%ld",
631 plvnm->iItem, plvnm->iSubItem, plvnm->uNewState, plvnm->uOldState,
632 plvnm->uChanged, wine_dbgstr_point(&plvnm->ptAction), plvnm->lParam);
635 static const char* debuglvitem_t(const LVITEMW *lpLVItem, BOOL isW)
637 char* buf = debug_getbuf(), *text = buf;
638 int len, size = DEBUG_BUFFER_SIZE;
640 if (lpLVItem == NULL) return "(null)";
641 len = snprintf(buf, size, "{iItem=%d, iSubItem=%d, ", lpLVItem->iItem, lpLVItem->iSubItem);
642 if (len == -1) goto end; buf += len; size -= len;
643 if (lpLVItem->mask & LVIF_STATE)
644 len = snprintf(buf, size, "state=%x, stateMask=%x, ", lpLVItem->state, lpLVItem->stateMask);
646 if (len == -1) goto end; buf += len; size -= len;
647 if (lpLVItem->mask & LVIF_TEXT)
648 len = snprintf(buf, size, "pszText=%s, cchTextMax=%d, ", debugtext_tn(lpLVItem->pszText, isW, 80), lpLVItem->cchTextMax);
650 if (len == -1) goto end; buf += len; size -= len;
651 if (lpLVItem->mask & LVIF_IMAGE)
652 len = snprintf(buf, size, "iImage=%d, ", lpLVItem->iImage);
654 if (len == -1) goto end; buf += len; size -= len;
655 if (lpLVItem->mask & LVIF_PARAM)
656 len = snprintf(buf, size, "lParam=%lx, ", lpLVItem->lParam);
658 if (len == -1) goto end; buf += len; size -= len;
659 if (lpLVItem->mask & LVIF_INDENT)
660 len = snprintf(buf, size, "iIndent=%d, ", lpLVItem->iIndent);
662 if (len == -1) goto end; buf += len;
665 buf = text + strlen(text);
667 if (buf - text > 2) { buf[-2] = '}'; buf[-1] = 0; }
671 static const char* debuglvcolumn_t(const LVCOLUMNW *lpColumn, BOOL isW)
673 char* buf = debug_getbuf(), *text = buf;
674 int len, size = DEBUG_BUFFER_SIZE;
676 if (lpColumn == NULL) return "(null)";
677 len = snprintf(buf, size, "{");
678 if (len == -1) goto end; buf += len; size -= len;
679 if (lpColumn->mask & LVCF_SUBITEM)
680 len = snprintf(buf, size, "iSubItem=%d, ", lpColumn->iSubItem);
682 if (len == -1) goto end; buf += len; size -= len;
683 if (lpColumn->mask & LVCF_FMT)
684 len = snprintf(buf, size, "fmt=%x, ", lpColumn->fmt);
686 if (len == -1) goto end; buf += len; size -= len;
687 if (lpColumn->mask & LVCF_WIDTH)
688 len = snprintf(buf, size, "cx=%d, ", lpColumn->cx);
690 if (len == -1) goto end; buf += len; size -= len;
691 if (lpColumn->mask & LVCF_TEXT)
692 len = snprintf(buf, size, "pszText=%s, cchTextMax=%d, ", debugtext_tn(lpColumn->pszText, isW, 80), lpColumn->cchTextMax);
694 if (len == -1) goto end; buf += len; size -= len;
695 if (lpColumn->mask & LVCF_IMAGE)
696 len = snprintf(buf, size, "iImage=%d, ", lpColumn->iImage);
698 if (len == -1) goto end; buf += len; size -= len;
699 if (lpColumn->mask & LVCF_ORDER)
700 len = snprintf(buf, size, "iOrder=%d, ", lpColumn->iOrder);
702 if (len == -1) goto end; buf += len;
705 buf = text + strlen(text);
707 if (buf - text > 2) { buf[-2] = '}'; buf[-1] = 0; }
711 static const char* debuglvhittestinfo(const LVHITTESTINFO *lpht)
713 if (!lpht) return "(null)";
715 return wine_dbg_sprintf("{pt=%s, flags=0x%x, iItem=%d, iSubItem=%d}",
716 wine_dbgstr_point(&lpht->pt), lpht->flags, lpht->iItem, lpht->iSubItem);
719 /* Return the corresponding text for a given scroll value */
720 static inline LPCSTR debugscrollcode(int nScrollCode)
724 case SB_LINELEFT: return "SB_LINELEFT";
725 case SB_LINERIGHT: return "SB_LINERIGHT";
726 case SB_PAGELEFT: return "SB_PAGELEFT";
727 case SB_PAGERIGHT: return "SB_PAGERIGHT";
728 case SB_THUMBPOSITION: return "SB_THUMBPOSITION";
729 case SB_THUMBTRACK: return "SB_THUMBTRACK";
730 case SB_ENDSCROLL: return "SB_ENDSCROLL";
731 case SB_INTERNAL: return "SB_INTERNAL";
732 default: return "unknown";
737 /******** Notification functions ************************************/
739 static int get_ansi_notification(UINT unicodeNotificationCode)
741 switch (unicodeNotificationCode)
743 case LVN_BEGINLABELEDITA:
744 case LVN_BEGINLABELEDITW: return LVN_BEGINLABELEDITA;
745 case LVN_ENDLABELEDITA:
746 case LVN_ENDLABELEDITW: return LVN_ENDLABELEDITA;
747 case LVN_GETDISPINFOA:
748 case LVN_GETDISPINFOW: return LVN_GETDISPINFOA;
749 case LVN_SETDISPINFOA:
750 case LVN_SETDISPINFOW: return LVN_SETDISPINFOA;
751 case LVN_ODFINDITEMA:
752 case LVN_ODFINDITEMW: return LVN_ODFINDITEMA;
753 case LVN_GETINFOTIPA:
754 case LVN_GETINFOTIPW: return LVN_GETINFOTIPA;
755 /* header forwards */
757 case HDN_TRACKW: return HDN_TRACKA;
759 case HDN_ENDTRACKW: return HDN_ENDTRACKA;
760 case HDN_BEGINDRAG: return HDN_BEGINDRAG;
761 case HDN_ENDDRAG: return HDN_ENDDRAG;
762 case HDN_ITEMCHANGINGA:
763 case HDN_ITEMCHANGINGW: return HDN_ITEMCHANGINGA;
764 case HDN_ITEMCHANGEDA:
765 case HDN_ITEMCHANGEDW: return HDN_ITEMCHANGEDA;
767 case HDN_ITEMCLICKW: return HDN_ITEMCLICKA;
768 case HDN_DIVIDERDBLCLICKA:
769 case HDN_DIVIDERDBLCLICKW: return HDN_DIVIDERDBLCLICKA;
772 FIXME("unknown notification %x\n", unicodeNotificationCode);
773 return unicodeNotificationCode;
776 /* forwards header notifications to listview parent */
777 static LRESULT notify_forward_header(const LISTVIEW_INFO *infoPtr, NMHEADERW *lpnmhW)
779 LPCWSTR text = NULL, filter = NULL;
781 NMHEADERA *lpnmh = (NMHEADERA*) lpnmhW;
783 /* on unicode format exit earlier */
784 if (infoPtr->notifyFormat == NFR_UNICODE)
785 return SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, lpnmh->hdr.idFrom,
788 /* header always supplies unicode notifications,
789 all we have to do is to convert strings to ANSI */
792 /* convert item text */
793 if (lpnmh->pitem->mask & HDI_TEXT)
795 text = (LPCWSTR)lpnmh->pitem->pszText;
796 Str_SetPtrWtoA(&lpnmh->pitem->pszText, text);
798 /* convert filter text */
799 if ((lpnmh->pitem->mask & HDI_FILTER) && (lpnmh->pitem->type == HDFT_ISSTRING) &&
800 lpnmh->pitem->pvFilter)
802 filter = (LPCWSTR)((HD_TEXTFILTERA*)lpnmh->pitem->pvFilter)->pszText;
803 Str_SetPtrWtoA(&((HD_TEXTFILTERA*)lpnmh->pitem->pvFilter)->pszText, filter);
806 lpnmh->hdr.code = get_ansi_notification(lpnmh->hdr.code);
808 ret = SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, lpnmh->hdr.idFrom,
814 Free(lpnmh->pitem->pszText);
815 lpnmh->pitem->pszText = (LPSTR)text;
819 Free(((HD_TEXTFILTERA*)lpnmh->pitem->pvFilter)->pszText);
820 ((HD_TEXTFILTERA*)lpnmh->pitem->pvFilter)->pszText = (LPSTR)filter;
826 static LRESULT notify_hdr(const LISTVIEW_INFO *infoPtr, INT code, LPNMHDR pnmh)
830 TRACE("(code=%d)\n", code);
832 pnmh->hwndFrom = infoPtr->hwndSelf;
833 pnmh->idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
835 result = SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, pnmh->idFrom, (LPARAM)pnmh);
837 TRACE(" <= %ld\n", result);
842 static inline BOOL notify(const LISTVIEW_INFO *infoPtr, INT code)
845 HWND hwnd = infoPtr->hwndSelf;
846 notify_hdr(infoPtr, code, &nmh);
847 return IsWindow(hwnd);
850 static inline void notify_itemactivate(const LISTVIEW_INFO *infoPtr, const LVHITTESTINFO *htInfo)
861 item.mask = LVIF_PARAM|LVIF_STATE;
862 item.iItem = htInfo->iItem;
864 item.stateMask = (UINT)-1;
865 if (LISTVIEW_GetItemT(infoPtr, &item, TRUE)) {
866 nmia.lParam = item.lParam;
867 nmia.uOldState = item.state;
868 nmia.uNewState = item.state | LVIS_ACTIVATING;
869 nmia.uChanged = LVIF_STATE;
872 nmia.iItem = htInfo->iItem;
873 nmia.iSubItem = htInfo->iSubItem;
874 nmia.ptAction = htInfo->pt;
876 if (GetKeyState(VK_SHIFT) & 0x8000) nmia.uKeyFlags |= LVKF_SHIFT;
877 if (GetKeyState(VK_CONTROL) & 0x8000) nmia.uKeyFlags |= LVKF_CONTROL;
878 if (GetKeyState(VK_MENU) & 0x8000) nmia.uKeyFlags |= LVKF_ALT;
880 notify_hdr(infoPtr, LVN_ITEMACTIVATE, (LPNMHDR)&nmia);
883 static inline LRESULT notify_listview(const LISTVIEW_INFO *infoPtr, INT code, LPNMLISTVIEW plvnm)
885 TRACE("(code=%d, plvnm=%s)\n", code, debugnmlistview(plvnm));
886 return notify_hdr(infoPtr, code, (LPNMHDR)plvnm);
889 static BOOL notify_click(const LISTVIEW_INFO *infoPtr, INT code, const LVHITTESTINFO *lvht)
893 HWND hwnd = infoPtr->hwndSelf;
895 TRACE("code=%d, lvht=%s\n", code, debuglvhittestinfo(lvht));
896 ZeroMemory(&nmia, sizeof(nmia));
897 nmia.iItem = lvht->iItem;
898 nmia.iSubItem = lvht->iSubItem;
899 nmia.ptAction = lvht->pt;
900 item.mask = LVIF_PARAM;
901 item.iItem = lvht->iItem;
903 if (LISTVIEW_GetItemT(infoPtr, &item, TRUE)) nmia.lParam = item.lParam;
904 notify_hdr(infoPtr, code, (LPNMHDR)&nmia);
905 return IsWindow(hwnd);
908 static BOOL notify_deleteitem(const LISTVIEW_INFO *infoPtr, INT nItem)
912 HWND hwnd = infoPtr->hwndSelf;
914 ZeroMemory(&nmlv, sizeof (NMLISTVIEW));
916 item.mask = LVIF_PARAM;
919 if (LISTVIEW_GetItemT(infoPtr, &item, TRUE)) nmlv.lParam = item.lParam;
920 notify_listview(infoPtr, LVN_DELETEITEM, &nmlv);
921 return IsWindow(hwnd);
925 Send notification. depends on dispinfoW having same
926 structure as dispinfoA.
927 infoPtr : listview struct
928 code : *Unicode* notification code
929 pdi : dispinfo structure (can be unicode or ansi)
930 isW : TRUE if dispinfo is Unicode
932 static BOOL notify_dispinfoT(const LISTVIEW_INFO *infoPtr, UINT code, LPNMLVDISPINFOW pdi, BOOL isW)
934 INT length = 0, ret_length;
935 LPWSTR buffer = NULL, ret_text;
936 BOOL return_ansi = FALSE;
937 BOOL return_unicode = FALSE;
940 if ((pdi->item.mask & LVIF_TEXT) && is_text(pdi->item.pszText))
942 return_unicode = ( isW && infoPtr->notifyFormat == NFR_ANSI);
943 return_ansi = (!isW && infoPtr->notifyFormat == NFR_UNICODE);
946 ret_length = pdi->item.cchTextMax;
947 ret_text = pdi->item.pszText;
949 if (return_unicode || return_ansi)
951 if (code != LVN_GETDISPINFOW)
953 length = return_ansi ?
954 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pdi->item.pszText, -1, NULL, 0):
955 WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, NULL, 0, NULL, NULL);
959 length = pdi->item.cchTextMax;
960 *pdi->item.pszText = 0; /* make sure we don't process garbage */
963 buffer = Alloc( (return_ansi ? sizeof(WCHAR) : sizeof(CHAR)) * length);
964 if (!buffer) return FALSE;
967 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pdi->item.pszText, -1,
970 WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, (LPSTR) buffer,
973 pdi->item.pszText = buffer;
974 pdi->item.cchTextMax = length;
977 if (infoPtr->notifyFormat == NFR_ANSI)
978 code = get_ansi_notification(code);
980 TRACE(" pdi->item=%s\n", debuglvitem_t(&pdi->item, infoPtr->notifyFormat != NFR_ANSI));
981 ret = notify_hdr(infoPtr, code, &pdi->hdr);
982 TRACE(" resulting code=%d\n", pdi->hdr.code);
984 if (return_ansi || return_unicode)
986 if (return_ansi && (pdi->hdr.code == LVN_GETDISPINFOA))
988 strcpy((char*)ret_text, (char*)pdi->item.pszText);
990 else if (return_unicode && (pdi->hdr.code == LVN_GETDISPINFOW))
992 strcpyW(ret_text, pdi->item.pszText);
994 else if (return_ansi) /* note : pointer can be changed by app ! */
996 WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, (LPSTR) ret_text,
997 ret_length, NULL, NULL);
1000 MultiByteToWideChar(CP_ACP, 0, (LPSTR) pdi->item.pszText, -1,
1001 ret_text, ret_length);
1003 pdi->item.pszText = ret_text; /* restores our buffer */
1004 pdi->item.cchTextMax = ret_length;
1010 /* if dipsinfo holder changed notification code then convert */
1011 if (!isW && (pdi->hdr.code == LVN_GETDISPINFOW) && (pdi->item.mask & LVIF_TEXT))
1013 length = WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, NULL, 0, NULL, NULL);
1015 buffer = Alloc(length * sizeof(CHAR));
1016 if (!buffer) return FALSE;
1018 WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, (LPSTR) buffer,
1019 ret_length, NULL, NULL);
1021 strcpy((LPSTR)pdi->item.pszText, (LPSTR)buffer);
1028 static void customdraw_fill(NMLVCUSTOMDRAW *lpnmlvcd, const LISTVIEW_INFO *infoPtr, HDC hdc,
1029 const RECT *rcBounds, const LVITEMW *lplvItem)
1031 ZeroMemory(lpnmlvcd, sizeof(NMLVCUSTOMDRAW));
1032 lpnmlvcd->nmcd.hdc = hdc;
1033 lpnmlvcd->nmcd.rc = *rcBounds;
1034 lpnmlvcd->clrTextBk = infoPtr->clrTextBk;
1035 lpnmlvcd->clrText = infoPtr->clrText;
1036 if (!lplvItem) return;
1037 lpnmlvcd->nmcd.dwItemSpec = lplvItem->iItem + 1;
1038 lpnmlvcd->iSubItem = lplvItem->iSubItem;
1039 if (lplvItem->state & LVIS_SELECTED) lpnmlvcd->nmcd.uItemState |= CDIS_SELECTED;
1040 if (lplvItem->state & LVIS_FOCUSED) lpnmlvcd->nmcd.uItemState |= CDIS_FOCUS;
1041 if (lplvItem->iItem == infoPtr->nHotItem) lpnmlvcd->nmcd.uItemState |= CDIS_HOT;
1042 lpnmlvcd->nmcd.lItemlParam = lplvItem->lParam;
1045 static inline DWORD notify_customdraw (const LISTVIEW_INFO *infoPtr, DWORD dwDrawStage, NMLVCUSTOMDRAW *lpnmlvcd)
1047 BOOL isForItem = (lpnmlvcd->nmcd.dwItemSpec != 0);
1050 lpnmlvcd->nmcd.dwDrawStage = dwDrawStage;
1051 if (isForItem) lpnmlvcd->nmcd.dwDrawStage |= CDDS_ITEM;
1052 if (lpnmlvcd->iSubItem) lpnmlvcd->nmcd.dwDrawStage |= CDDS_SUBITEM;
1053 if (isForItem) lpnmlvcd->nmcd.dwItemSpec--;
1054 result = notify_hdr(infoPtr, NM_CUSTOMDRAW, &lpnmlvcd->nmcd.hdr);
1055 if (isForItem) lpnmlvcd->nmcd.dwItemSpec++;
1059 static void prepaint_setup (const LISTVIEW_INFO *infoPtr, HDC hdc, NMLVCUSTOMDRAW *lpnmlvcd, BOOL SubItem)
1061 if (lpnmlvcd->clrTextBk == CLR_DEFAULT)
1062 lpnmlvcd->clrTextBk = comctl32_color.clrWindow;
1063 if (lpnmlvcd->clrText == CLR_DEFAULT)
1064 lpnmlvcd->clrText = comctl32_color.clrWindowText;
1066 /* apparently, for selected items, we have to override the returned values */
1069 if (lpnmlvcd->nmcd.uItemState & CDIS_SELECTED)
1071 if (infoPtr->bFocus)
1073 lpnmlvcd->clrTextBk = comctl32_color.clrHighlight;
1074 lpnmlvcd->clrText = comctl32_color.clrHighlightText;
1076 else if (infoPtr->dwStyle & LVS_SHOWSELALWAYS)
1078 lpnmlvcd->clrTextBk = comctl32_color.clr3dFace;
1079 lpnmlvcd->clrText = comctl32_color.clrBtnText;
1084 /* Set the text attributes */
1085 if (lpnmlvcd->clrTextBk != CLR_NONE)
1087 SetBkMode(hdc, OPAQUE);
1088 SetBkColor(hdc,lpnmlvcd->clrTextBk);
1091 SetBkMode(hdc, TRANSPARENT);
1092 SetTextColor(hdc, lpnmlvcd->clrText);
1095 static inline DWORD notify_postpaint (const LISTVIEW_INFO *infoPtr, NMLVCUSTOMDRAW *lpnmlvcd)
1097 return notify_customdraw(infoPtr, CDDS_POSTPAINT, lpnmlvcd);
1100 /* returns TRUE when repaint needed, FALSE otherwise */
1101 static BOOL notify_measureitem(LISTVIEW_INFO *infoPtr)
1103 MEASUREITEMSTRUCT mis;
1104 mis.CtlType = ODT_LISTVIEW;
1105 mis.CtlID = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1109 mis.itemHeight= infoPtr->nItemHeight;
1110 SendMessageW(infoPtr->hwndNotify, WM_MEASUREITEM, mis.CtlID, (LPARAM)&mis);
1111 if (infoPtr->nItemHeight != max(mis.itemHeight, 1))
1113 infoPtr->nMeasureItemHeight = infoPtr->nItemHeight = max(mis.itemHeight, 1);
1119 /******** Item iterator functions **********************************/
1121 static RANGES ranges_create(int count);
1122 static void ranges_destroy(RANGES ranges);
1123 static BOOL ranges_add(RANGES ranges, RANGE range);
1124 static BOOL ranges_del(RANGES ranges, RANGE range);
1125 static void ranges_dump(RANGES ranges);
1127 static inline BOOL ranges_additem(RANGES ranges, INT nItem)
1129 RANGE range = { nItem, nItem + 1 };
1131 return ranges_add(ranges, range);
1134 static inline BOOL ranges_delitem(RANGES ranges, INT nItem)
1136 RANGE range = { nItem, nItem + 1 };
1138 return ranges_del(ranges, range);
1142 * ITERATOR DOCUMENTATION
1144 * The iterator functions allow for easy, and convenient iteration
1145 * over items of interest in the list. Typically, you create a
1146 * iterator, use it, and destroy it, as such:
1149 * iterator_xxxitems(&i, ...);
1150 * while (iterator_{prev,next}(&i)
1152 * //code which uses i.nItem
1154 * iterator_destroy(&i);
1156 * where xxx is either: framed, or visible.
1157 * Note that it is important that the code destroys the iterator
1158 * after it's done with it, as the creation of the iterator may
1159 * allocate memory, which thus needs to be freed.
1161 * You can iterate both forwards, and backwards through the list,
1162 * by using iterator_next or iterator_prev respectively.
1164 * Lower numbered items are draw on top of higher number items in
1165 * LVS_ICON, and LVS_SMALLICON (which are the only modes where
1166 * items may overlap). So, to test items, you should use
1168 * which lists the items top to bottom (in Z-order).
1169 * For drawing items, you should use
1171 * which lists the items bottom to top (in Z-order).
1172 * If you keep iterating over the items after the end-of-items
1173 * marker (-1) is returned, the iterator will start from the
1174 * beginning. Typically, you don't need to test for -1,
1175 * because iterator_{next,prev} will return TRUE if more items
1176 * are to be iterated over, or FALSE otherwise.
1178 * Note: the iterator is defined to be bidirectional. That is,
1179 * any number of prev followed by any number of next, or
1180 * five versa, should leave the iterator at the same item:
1181 * prev * n, next * n = next * n, prev * n
1183 * The iterator has a notion of an out-of-order, special item,
1184 * which sits at the start of the list. This is used in
1185 * LVS_ICON, and LVS_SMALLICON mode to handle the focused item,
1186 * which needs to be first, as it may overlap other items.
1188 * The code is a bit messy because we have:
1189 * - a special item to deal with
1190 * - simple range, or composite range
1192 * If you find bugs, or want to add features, please make sure you
1193 * always check/modify *both* iterator_prev, and iterator_next.
1197 * This function iterates through the items in increasing order,
1198 * but prefixed by the special item, then -1. That is:
1199 * special, 1, 2, 3, ..., n, -1.
1200 * Each item is listed only once.
1202 static inline BOOL iterator_next(ITERATOR* i)
1206 i->nItem = i->nSpecial;
1207 if (i->nItem != -1) return TRUE;
1209 if (i->nItem == i->nSpecial)
1211 if (i->ranges) i->index = 0;
1217 if (i->nItem == i->nSpecial) i->nItem++;
1218 if (i->nItem < i->range.upper) return TRUE;
1223 if (i->index < DPA_GetPtrCount(i->ranges->hdpa))
1224 i->range = *(RANGE*)DPA_GetPtr(i->ranges->hdpa, i->index++);
1227 else if (i->nItem >= i->range.upper) goto end;
1229 i->nItem = i->range.lower;
1230 if (i->nItem >= 0) goto testitem;
1237 * This function iterates through the items in decreasing order,
1238 * followed by the special item, then -1. That is:
1239 * n, n-1, ..., 3, 2, 1, special, -1.
1240 * Each item is listed only once.
1242 static inline BOOL iterator_prev(ITERATOR* i)
1249 if (i->ranges) i->index = DPA_GetPtrCount(i->ranges->hdpa);
1252 if (i->nItem == i->nSpecial)
1260 if (i->nItem == i->nSpecial) i->nItem--;
1261 if (i->nItem >= i->range.lower) return TRUE;
1267 i->range = *(RANGE*)DPA_GetPtr(i->ranges->hdpa, --i->index);
1270 else if (!start && i->nItem < i->range.lower) goto end;
1272 i->nItem = i->range.upper;
1273 if (i->nItem > 0) goto testitem;
1275 return (i->nItem = i->nSpecial) != -1;
1278 static RANGE iterator_range(const ITERATOR *i)
1282 if (!i->ranges) return i->range;
1284 if (DPA_GetPtrCount(i->ranges->hdpa) > 0)
1286 range.lower = (*(RANGE*)DPA_GetPtr(i->ranges->hdpa, 0)).lower;
1287 range.upper = (*(RANGE*)DPA_GetPtr(i->ranges->hdpa, DPA_GetPtrCount(i->ranges->hdpa) - 1)).upper;
1289 else range.lower = range.upper = 0;
1295 * Releases resources associated with this iterator.
1297 static inline void iterator_destroy(const ITERATOR *i)
1299 ranges_destroy(i->ranges);
1303 * Create an empty iterator.
1305 static inline BOOL iterator_empty(ITERATOR* i)
1307 ZeroMemory(i, sizeof(*i));
1308 i->nItem = i->nSpecial = i->range.lower = i->range.upper = -1;
1313 * Create an iterator over a range.
1315 static inline BOOL iterator_rangeitems(ITERATOR* i, RANGE range)
1323 * Create an iterator over a bunch of ranges.
1324 * Please note that the iterator will take ownership of the ranges,
1325 * and will free them upon destruction.
1327 static inline BOOL iterator_rangesitems(ITERATOR* i, RANGES ranges)
1335 * Creates an iterator over the items which intersect frame.
1336 * Uses absolute coordinates rather than compensating for the current offset.
1338 static BOOL iterator_frameditems_absolute(ITERATOR* i, const LISTVIEW_INFO* infoPtr, const RECT *frame)
1340 RECT rcItem, rcTemp;
1342 /* in case we fail, we want to return an empty iterator */
1343 if (!iterator_empty(i)) return FALSE;
1345 TRACE("(frame=%s)\n", wine_dbgstr_rect(frame));
1347 if (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON)
1351 if (infoPtr->uView == LV_VIEW_ICON && infoPtr->nFocusedItem != -1)
1353 LISTVIEW_GetItemBox(infoPtr, infoPtr->nFocusedItem, &rcItem);
1354 if (IntersectRect(&rcTemp, &rcItem, frame))
1355 i->nSpecial = infoPtr->nFocusedItem;
1357 if (!(iterator_rangesitems(i, ranges_create(50)))) return FALSE;
1358 /* to do better here, we need to have PosX, and PosY sorted */
1359 TRACE("building icon ranges:\n");
1360 for (nItem = 0; nItem < infoPtr->nItemCount; nItem++)
1362 rcItem.left = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosX, nItem);
1363 rcItem.top = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosY, nItem);
1364 rcItem.right = rcItem.left + infoPtr->nItemWidth;
1365 rcItem.bottom = rcItem.top + infoPtr->nItemHeight;
1366 if (IntersectRect(&rcTemp, &rcItem, frame))
1367 ranges_additem(i->ranges, nItem);
1371 else if (infoPtr->uView == LV_VIEW_DETAILS)
1375 if (frame->left >= infoPtr->nItemWidth) return TRUE;
1376 if (frame->top >= infoPtr->nItemHeight * infoPtr->nItemCount) return TRUE;
1378 range.lower = max(frame->top / infoPtr->nItemHeight, 0);
1379 range.upper = min((frame->bottom - 1) / infoPtr->nItemHeight, infoPtr->nItemCount - 1) + 1;
1380 if (range.upper <= range.lower) return TRUE;
1381 if (!iterator_rangeitems(i, range)) return FALSE;
1382 TRACE(" report=%s\n", debugrange(&i->range));
1386 INT nPerCol = max((infoPtr->rcList.bottom - infoPtr->rcList.top) / infoPtr->nItemHeight, 1);
1387 INT nFirstRow = max(frame->top / infoPtr->nItemHeight, 0);
1388 INT nLastRow = min((frame->bottom - 1) / infoPtr->nItemHeight, nPerCol - 1);
1395 if (infoPtr->nItemWidth)
1397 nFirstCol = max(frame->left / infoPtr->nItemWidth, 0);
1398 nLastCol = min((frame->right - 1) / infoPtr->nItemWidth, (infoPtr->nItemCount + nPerCol - 1) / nPerCol);
1402 nFirstCol = max(frame->left, 0);
1403 nLastCol = min(frame->right - 1, (infoPtr->nItemCount + nPerCol - 1) / nPerCol);
1406 lower = nFirstCol * nPerCol + nFirstRow;
1408 TRACE("nPerCol=%d, nFirstRow=%d, nLastRow=%d, nFirstCol=%d, nLastCol=%d, lower=%d\n",
1409 nPerCol, nFirstRow, nLastRow, nFirstCol, nLastCol, lower);
1411 if (nLastCol < nFirstCol || nLastRow < nFirstRow) return TRUE;
1413 if (!(iterator_rangesitems(i, ranges_create(nLastCol - nFirstCol + 1)))) return FALSE;
1414 TRACE("building list ranges:\n");
1415 for (nCol = nFirstCol; nCol <= nLastCol; nCol++)
1417 item_range.lower = nCol * nPerCol + nFirstRow;
1418 if(item_range.lower >= infoPtr->nItemCount) break;
1419 item_range.upper = min(nCol * nPerCol + nLastRow + 1, infoPtr->nItemCount);
1420 TRACE(" list=%s\n", debugrange(&item_range));
1421 ranges_add(i->ranges, item_range);
1429 * Creates an iterator over the items which intersect lprc.
1431 static BOOL iterator_frameditems(ITERATOR* i, const LISTVIEW_INFO* infoPtr, const RECT *lprc)
1436 TRACE("(lprc=%s)\n", wine_dbgstr_rect(lprc));
1438 LISTVIEW_GetOrigin(infoPtr, &Origin);
1439 OffsetRect(&frame, -Origin.x, -Origin.y);
1441 return iterator_frameditems_absolute(i, infoPtr, &frame);
1445 * Creates an iterator over the items which intersect the visible region of hdc.
1447 static BOOL iterator_visibleitems(ITERATOR *i, const LISTVIEW_INFO *infoPtr, HDC hdc)
1449 POINT Origin, Position;
1450 RECT rcItem, rcClip;
1453 rgntype = GetClipBox(hdc, &rcClip);
1454 if (rgntype == NULLREGION) return iterator_empty(i);
1455 if (!iterator_frameditems(i, infoPtr, &rcClip)) return FALSE;
1456 if (rgntype == SIMPLEREGION) return TRUE;
1458 /* first deal with the special item */
1459 if (i->nSpecial != -1)
1461 LISTVIEW_GetItemBox(infoPtr, i->nSpecial, &rcItem);
1462 if (!RectVisible(hdc, &rcItem)) i->nSpecial = -1;
1465 /* if we can't deal with the region, we'll just go with the simple range */
1466 LISTVIEW_GetOrigin(infoPtr, &Origin);
1467 TRACE("building visible range:\n");
1468 if (!i->ranges && i->range.lower < i->range.upper)
1470 if (!(i->ranges = ranges_create(50))) return TRUE;
1471 if (!ranges_add(i->ranges, i->range))
1473 ranges_destroy(i->ranges);
1479 /* now delete the invisible items from the list */
1480 while(iterator_next(i))
1482 LISTVIEW_GetItemOrigin(infoPtr, i->nItem, &Position);
1483 rcItem.left = (infoPtr->uView == LV_VIEW_DETAILS) ? Origin.x : Position.x + Origin.x;
1484 rcItem.top = Position.y + Origin.y;
1485 rcItem.right = rcItem.left + infoPtr->nItemWidth;
1486 rcItem.bottom = rcItem.top + infoPtr->nItemHeight;
1487 if (!RectVisible(hdc, &rcItem))
1488 ranges_delitem(i->ranges, i->nItem);
1490 /* the iterator should restart on the next iterator_next */
1496 /* Remove common elements from two iterators */
1497 /* Passed iterators have to point on the first elements */
1498 static BOOL iterator_remove_common_items(ITERATOR *iter1, ITERATOR *iter2)
1500 if(!iter1->ranges || !iter2->ranges) {
1503 if(iter1->ranges || iter2->ranges ||
1504 (iter1->range.lower<iter2->range.lower && iter1->range.upper>iter2->range.upper) ||
1505 (iter1->range.lower>iter2->range.lower && iter1->range.upper<iter2->range.upper)) {
1506 ERR("result is not a one range iterator\n");
1510 if(iter1->range.lower==-1 || iter2->range.lower==-1)
1513 lower = iter1->range.lower;
1514 upper = iter1->range.upper;
1516 if(lower < iter2->range.lower)
1517 iter1->range.upper = iter2->range.lower;
1518 else if(upper > iter2->range.upper)
1519 iter1->range.lower = iter2->range.upper;
1521 iter1->range.lower = iter1->range.upper = -1;
1523 if(iter2->range.lower < lower)
1524 iter2->range.upper = lower;
1525 else if(iter2->range.upper > upper)
1526 iter2->range.lower = upper;
1528 iter2->range.lower = iter2->range.upper = -1;
1533 iterator_next(iter1);
1534 iterator_next(iter2);
1537 if(iter1->nItem==-1 || iter2->nItem==-1)
1540 if(iter1->nItem == iter2->nItem) {
1541 int delete = iter1->nItem;
1543 iterator_prev(iter1);
1544 iterator_prev(iter2);
1545 ranges_delitem(iter1->ranges, delete);
1546 ranges_delitem(iter2->ranges, delete);
1547 iterator_next(iter1);
1548 iterator_next(iter2);
1549 } else if(iter1->nItem > iter2->nItem)
1550 iterator_next(iter2);
1552 iterator_next(iter1);
1555 iter1->nItem = iter1->range.lower = iter1->range.upper = -1;
1556 iter2->nItem = iter2->range.lower = iter2->range.upper = -1;
1560 /******** Misc helper functions ************************************/
1562 static inline LRESULT CallWindowProcT(WNDPROC proc, HWND hwnd, UINT uMsg,
1563 WPARAM wParam, LPARAM lParam, BOOL isW)
1565 if (isW) return CallWindowProcW(proc, hwnd, uMsg, wParam, lParam);
1566 else return CallWindowProcA(proc, hwnd, uMsg, wParam, lParam);
1569 static inline BOOL is_autoarrange(const LISTVIEW_INFO *infoPtr)
1571 return ((infoPtr->dwStyle & LVS_AUTOARRANGE) || infoPtr->bAutoarrange) &&
1572 (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON);
1575 static void toggle_checkbox_state(LISTVIEW_INFO *infoPtr, INT nItem)
1577 DWORD state = STATEIMAGEINDEX(LISTVIEW_GetItemState(infoPtr, nItem, LVIS_STATEIMAGEMASK));
1578 if(state == 1 || state == 2)
1582 lvitem.state = INDEXTOSTATEIMAGEMASK(state);
1583 lvitem.stateMask = LVIS_STATEIMAGEMASK;
1584 LISTVIEW_SetItemState(infoPtr, nItem, &lvitem);
1588 /* this should be called after window style got updated,
1589 it used to reset view state to match current window style */
1590 static inline void map_style_view(LISTVIEW_INFO *infoPtr)
1592 switch (infoPtr->dwStyle & LVS_TYPEMASK)
1595 infoPtr->uView = LV_VIEW_ICON;
1598 infoPtr->uView = LV_VIEW_DETAILS;
1601 infoPtr->uView = LV_VIEW_SMALLICON;
1604 infoPtr->uView = LV_VIEW_LIST;
1608 /* computes next item id value */
1609 static DWORD get_next_itemid(const LISTVIEW_INFO *infoPtr)
1611 INT count = DPA_GetPtrCount(infoPtr->hdpaItemIds);
1615 ITEM_ID *lpID = DPA_GetPtr(infoPtr->hdpaItemIds, count - 1);
1616 return lpID->id + 1;
1621 /******** Internal API functions ************************************/
1623 static inline COLUMN_INFO * LISTVIEW_GetColumnInfo(const LISTVIEW_INFO *infoPtr, INT nSubItem)
1625 static COLUMN_INFO mainItem;
1627 if (nSubItem == 0 && DPA_GetPtrCount(infoPtr->hdpaColumns) == 0) return &mainItem;
1628 assert (nSubItem >= 0 && nSubItem < DPA_GetPtrCount(infoPtr->hdpaColumns));
1630 /* update cached column rectangles */
1631 if (infoPtr->colRectsDirty)
1634 LISTVIEW_INFO *Ptr = (LISTVIEW_INFO*)infoPtr;
1637 for (i = 0; i < DPA_GetPtrCount(infoPtr->hdpaColumns); i++) {
1638 info = DPA_GetPtr(infoPtr->hdpaColumns, i);
1639 SendMessageW(infoPtr->hwndHeader, HDM_GETITEMRECT, i, (LPARAM)&info->rcHeader);
1641 Ptr->colRectsDirty = FALSE;
1644 return DPA_GetPtr(infoPtr->hdpaColumns, nSubItem);
1647 static INT LISTVIEW_CreateHeader(LISTVIEW_INFO *infoPtr)
1649 DWORD dFlags = WS_CHILD | HDS_HORZ | HDS_FULLDRAG | HDS_DRAGDROP;
1652 if (infoPtr->hwndHeader) return 0;
1654 TRACE("Creating header for list %p\n", infoPtr->hwndSelf);
1656 /* setup creation flags */
1657 dFlags |= (LVS_NOSORTHEADER & infoPtr->dwStyle) ? 0 : HDS_BUTTONS;
1658 dFlags |= (LVS_NOCOLUMNHEADER & infoPtr->dwStyle) ? HDS_HIDDEN : 0;
1660 hInst = (HINSTANCE)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_HINSTANCE);
1663 infoPtr->hwndHeader = CreateWindowW(WC_HEADERW, NULL, dFlags,
1664 0, 0, 0, 0, infoPtr->hwndSelf, NULL, hInst, NULL);
1665 if (!infoPtr->hwndHeader) return -1;
1667 /* set header unicode format */
1668 SendMessageW(infoPtr->hwndHeader, HDM_SETUNICODEFORMAT, TRUE, 0);
1670 /* set header font */
1671 SendMessageW(infoPtr->hwndHeader, WM_SETFONT, (WPARAM)infoPtr->hFont, TRUE);
1673 /* set header image list */
1674 if (infoPtr->himlSmall)
1675 SendMessageW(infoPtr->hwndHeader, HDM_SETIMAGELIST, 0, (LPARAM)infoPtr->himlSmall);
1677 LISTVIEW_UpdateSize(infoPtr);
1682 static inline void LISTVIEW_GetHeaderRect(const LISTVIEW_INFO *infoPtr, INT nSubItem, LPRECT lprc)
1684 *lprc = LISTVIEW_GetColumnInfo(infoPtr, nSubItem)->rcHeader;
1687 static inline BOOL LISTVIEW_IsHeaderEnabled(const LISTVIEW_INFO *infoPtr)
1689 return (infoPtr->uView == LV_VIEW_DETAILS ||
1690 infoPtr->dwLvExStyle & LVS_EX_HEADERINALLVIEWS) &&
1691 !(infoPtr->dwStyle & LVS_NOCOLUMNHEADER);
1694 static inline BOOL LISTVIEW_GetItemW(const LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem)
1696 return LISTVIEW_GetItemT(infoPtr, lpLVItem, TRUE);
1699 /* used to handle collapse main item column case */
1700 static inline BOOL LISTVIEW_DrawFocusRect(const LISTVIEW_INFO *infoPtr, HDC hdc)
1702 return (infoPtr->rcFocus.left < infoPtr->rcFocus.right) ?
1703 DrawFocusRect(hdc, &infoPtr->rcFocus) : FALSE;
1706 /* Listview invalidation functions: use _only_ these functions to invalidate */
1708 static inline BOOL is_redrawing(const LISTVIEW_INFO *infoPtr)
1710 return infoPtr->bRedraw;
1713 static inline void LISTVIEW_InvalidateRect(const LISTVIEW_INFO *infoPtr, const RECT* rect)
1715 if(!is_redrawing(infoPtr)) return;
1716 TRACE(" invalidating rect=%s\n", wine_dbgstr_rect(rect));
1717 InvalidateRect(infoPtr->hwndSelf, rect, TRUE);
1720 static inline void LISTVIEW_InvalidateItem(const LISTVIEW_INFO *infoPtr, INT nItem)
1724 if(!is_redrawing(infoPtr)) return;
1725 LISTVIEW_GetItemBox(infoPtr, nItem, &rcBox);
1726 LISTVIEW_InvalidateRect(infoPtr, &rcBox);
1729 static inline void LISTVIEW_InvalidateSubItem(const LISTVIEW_INFO *infoPtr, INT nItem, INT nSubItem)
1731 POINT Origin, Position;
1734 if(!is_redrawing(infoPtr)) return;
1735 assert (infoPtr->uView == LV_VIEW_DETAILS);
1736 LISTVIEW_GetOrigin(infoPtr, &Origin);
1737 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
1738 LISTVIEW_GetHeaderRect(infoPtr, nSubItem, &rcBox);
1740 rcBox.bottom = infoPtr->nItemHeight;
1741 OffsetRect(&rcBox, Origin.x + Position.x, Origin.y + Position.y);
1742 LISTVIEW_InvalidateRect(infoPtr, &rcBox);
1745 static inline void LISTVIEW_InvalidateList(const LISTVIEW_INFO *infoPtr)
1747 LISTVIEW_InvalidateRect(infoPtr, NULL);
1750 static inline void LISTVIEW_InvalidateColumn(const LISTVIEW_INFO *infoPtr, INT nColumn)
1754 if(!is_redrawing(infoPtr)) return;
1755 LISTVIEW_GetHeaderRect(infoPtr, nColumn, &rcCol);
1756 rcCol.top = infoPtr->rcList.top;
1757 rcCol.bottom = infoPtr->rcList.bottom;
1758 LISTVIEW_InvalidateRect(infoPtr, &rcCol);
1763 * Retrieves the number of items that can fit vertically in the client area.
1766 * [I] infoPtr : valid pointer to the listview structure
1769 * Number of items per row.
1771 static inline INT LISTVIEW_GetCountPerRow(const LISTVIEW_INFO *infoPtr)
1773 INT nListWidth = infoPtr->rcList.right - infoPtr->rcList.left;
1775 return max(nListWidth/(infoPtr->nItemWidth ? infoPtr->nItemWidth : 1), 1);
1780 * Retrieves the number of items that can fit horizontally in the client
1784 * [I] infoPtr : valid pointer to the listview structure
1787 * Number of items per column.
1789 static inline INT LISTVIEW_GetCountPerColumn(const LISTVIEW_INFO *infoPtr)
1791 INT nListHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
1793 return max(nListHeight / infoPtr->nItemHeight, 1);
1797 /*************************************************************************
1798 * LISTVIEW_ProcessLetterKeys
1800 * Processes keyboard messages generated by pressing the letter keys
1802 * What this does is perform a case insensitive search from the
1803 * current position with the following quirks:
1804 * - If two chars or more are pressed in quick succession we search
1805 * for the corresponding string (e.g. 'abc').
1806 * - If there is a delay we wipe away the current search string and
1807 * restart with just that char.
1808 * - If the user keeps pressing the same character, whether slowly or
1809 * fast, so that the search string is entirely composed of this
1810 * character ('aaaaa' for instance), then we search for first item
1811 * that starting with that character.
1812 * - If the user types the above character in quick succession, then
1813 * we must also search for the corresponding string ('aaaaa'), and
1814 * go to that string if there is a match.
1817 * [I] hwnd : handle to the window
1818 * [I] charCode : the character code, the actual character
1819 * [I] keyData : key data
1827 * - The current implementation has a list of characters it will
1828 * accept and it ignores everything else. In particular it will
1829 * ignore accentuated characters which seems to match what
1830 * Windows does. But I'm not sure it makes sense to follow
1832 * - We don't sound a beep when the search fails.
1836 * TREEVIEW_ProcessLetterKeys
1838 static INT LISTVIEW_ProcessLetterKeys(LISTVIEW_INFO *infoPtr, WPARAM charCode, LPARAM keyData)
1840 WCHAR buffer[MAX_PATH];
1841 INT endidx, startidx;
1847 /* simple parameter checking */
1848 if (!charCode || !keyData || infoPtr->nItemCount == 0) return 0;
1850 /* only allow the valid WM_CHARs through */
1851 if (!isalnumW(charCode) &&
1852 charCode != '.' && charCode != '`' && charCode != '!' &&
1853 charCode != '@' && charCode != '#' && charCode != '$' &&
1854 charCode != '%' && charCode != '^' && charCode != '&' &&
1855 charCode != '*' && charCode != '(' && charCode != ')' &&
1856 charCode != '-' && charCode != '_' && charCode != '+' &&
1857 charCode != '=' && charCode != '\\'&& charCode != ']' &&
1858 charCode != '}' && charCode != '[' && charCode != '{' &&
1859 charCode != '/' && charCode != '?' && charCode != '>' &&
1860 charCode != '<' && charCode != ',' && charCode != '~')
1863 /* update the search parameters */
1864 prevTime = infoPtr->lastKeyPressTimestamp;
1865 infoPtr->lastKeyPressTimestamp = GetTickCount();
1866 diff = infoPtr->lastKeyPressTimestamp - prevTime;
1868 if (diff >= 0 && diff < KEY_DELAY)
1870 if (infoPtr->nSearchParamLength < MAX_PATH - 1)
1871 infoPtr->szSearchParam[infoPtr->nSearchParamLength++] = charCode;
1873 if (infoPtr->charCode != charCode)
1874 infoPtr->charCode = charCode = 0;
1878 infoPtr->charCode = charCode;
1879 infoPtr->szSearchParam[0] = charCode;
1880 infoPtr->nSearchParamLength = 1;
1883 /* and search from the current position */
1885 endidx = infoPtr->nItemCount;
1887 /* should start from next after focused item, so next item that matches
1888 will be selected, if there isn't any and focused matches it will be selected
1889 on second search stage from beginning of the list */
1890 if (infoPtr->nFocusedItem >= 0 && infoPtr->nItemCount > 1)
1891 startidx = infoPtr->nFocusedItem + 1;
1895 /* let application handle this for virtual listview */
1896 if (infoPtr->dwStyle & LVS_OWNERDATA)
1900 memset(&nmlv.lvfi, 0, sizeof(nmlv.lvfi));
1901 nmlv.lvfi.flags = (LVFI_WRAP | LVFI_PARTIAL);
1902 nmlv.lvfi.psz = infoPtr->szSearchParam;
1903 nmlv.iStart = startidx;
1905 infoPtr->szSearchParam[infoPtr->nSearchParamLength] = 0;
1907 nItem = notify_hdr(infoPtr, LVN_ODFINDITEMW, (LPNMHDR)&nmlv.hdr);
1913 /* first search in [startidx, endidx), on failure continue in [0, startidx) */
1916 /* start from first item if not found with >= startidx */
1917 if (i == infoPtr->nItemCount && startidx > 0)
1923 for (i = startidx; i < endidx; i++)
1926 item.mask = LVIF_TEXT;
1929 item.pszText = buffer;
1930 item.cchTextMax = MAX_PATH;
1931 if (!LISTVIEW_GetItemW(infoPtr, &item)) return 0;
1933 if (lstrncmpiW(item.pszText, infoPtr->szSearchParam, infoPtr->nSearchParamLength) == 0)
1938 else if (nItem == -1 && lstrncmpiW(item.pszText, infoPtr->szSearchParam, 1) == 0)
1940 /* this would work but we must keep looking for a longer match */
1945 if ( nItem != -1 || /* found something */
1946 endidx != infoPtr->nItemCount || /* second search done */
1947 (startidx == 0 && endidx == infoPtr->nItemCount) /* full range for first search */ )
1953 LISTVIEW_KeySelection(infoPtr, nItem, FALSE);
1958 /*************************************************************************
1959 * LISTVIEW_UpdateHeaderSize [Internal]
1961 * Function to resize the header control
1964 * [I] hwnd : handle to a window
1965 * [I] nNewScrollPos : scroll pos to set
1970 static void LISTVIEW_UpdateHeaderSize(const LISTVIEW_INFO *infoPtr, INT nNewScrollPos)
1975 TRACE("nNewScrollPos=%d\n", nNewScrollPos);
1977 if (!infoPtr->hwndHeader) return;
1979 GetWindowRect(infoPtr->hwndHeader, &winRect);
1980 point[0].x = winRect.left;
1981 point[0].y = winRect.top;
1982 point[1].x = winRect.right;
1983 point[1].y = winRect.bottom;
1985 MapWindowPoints(HWND_DESKTOP, infoPtr->hwndSelf, point, 2);
1986 point[0].x = -nNewScrollPos;
1987 point[1].x += nNewScrollPos;
1989 SetWindowPos(infoPtr->hwndHeader,0,
1990 point[0].x,point[0].y,point[1].x,point[1].y,
1991 (infoPtr->dwStyle & LVS_NOCOLUMNHEADER) ? SWP_HIDEWINDOW : SWP_SHOWWINDOW |
1992 SWP_NOZORDER | SWP_NOACTIVATE);
1997 * Update the scrollbars. This functions should be called whenever
1998 * the content, size or view changes.
2001 * [I] infoPtr : valid pointer to the listview structure
2006 static void LISTVIEW_UpdateScroll(const LISTVIEW_INFO *infoPtr)
2008 SCROLLINFO horzInfo, vertInfo;
2011 if ((infoPtr->dwStyle & LVS_NOSCROLL) || !is_redrawing(infoPtr)) return;
2013 ZeroMemory(&horzInfo, sizeof(SCROLLINFO));
2014 horzInfo.cbSize = sizeof(SCROLLINFO);
2015 horzInfo.nPage = infoPtr->rcList.right - infoPtr->rcList.left;
2017 /* for now, we'll set info.nMax to the _count_, and adjust it later */
2018 if (infoPtr->uView == LV_VIEW_LIST)
2020 INT nPerCol = LISTVIEW_GetCountPerColumn(infoPtr);
2021 horzInfo.nMax = (infoPtr->nItemCount + nPerCol - 1) / nPerCol;
2023 /* scroll by at least one column per page */
2024 if(horzInfo.nPage < infoPtr->nItemWidth)
2025 horzInfo.nPage = infoPtr->nItemWidth;
2027 if (infoPtr->nItemWidth)
2028 horzInfo.nPage /= infoPtr->nItemWidth;
2030 else if (infoPtr->uView == LV_VIEW_DETAILS)
2032 horzInfo.nMax = infoPtr->nItemWidth;
2034 else /* LV_VIEW_ICON, or LV_VIEW_SMALLICON */
2038 if (LISTVIEW_GetViewRect(infoPtr, &rcView)) horzInfo.nMax = rcView.right - rcView.left;
2041 if (LISTVIEW_IsHeaderEnabled(infoPtr))
2043 if (DPA_GetPtrCount(infoPtr->hdpaColumns))
2048 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX,
2049 DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, 0);
2051 LISTVIEW_GetHeaderRect(infoPtr, index, &rcHeader);
2052 horzInfo.nMax = rcHeader.right;
2053 TRACE("horzInfo.nMax=%d\n", horzInfo.nMax);
2057 horzInfo.fMask = SIF_RANGE | SIF_PAGE;
2058 horzInfo.nMax = max(horzInfo.nMax - 1, 0);
2059 dx = GetScrollPos(infoPtr->hwndSelf, SB_HORZ);
2060 dx -= SetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &horzInfo, TRUE);
2061 TRACE("horzInfo=%s\n", debugscrollinfo(&horzInfo));
2063 /* Setting the horizontal scroll can change the listview size
2064 * (and potentially everything else) so we need to recompute
2065 * everything again for the vertical scroll
2068 ZeroMemory(&vertInfo, sizeof(SCROLLINFO));
2069 vertInfo.cbSize = sizeof(SCROLLINFO);
2070 vertInfo.nPage = infoPtr->rcList.bottom - infoPtr->rcList.top;
2072 if (infoPtr->uView == LV_VIEW_DETAILS)
2074 vertInfo.nMax = infoPtr->nItemCount;
2076 /* scroll by at least one page */
2077 if(vertInfo.nPage < infoPtr->nItemHeight)
2078 vertInfo.nPage = infoPtr->nItemHeight;
2080 if (infoPtr->nItemHeight > 0)
2081 vertInfo.nPage /= infoPtr->nItemHeight;
2083 else if (infoPtr->uView != LV_VIEW_LIST) /* LV_VIEW_ICON, or LV_VIEW_SMALLICON */
2087 if (LISTVIEW_GetViewRect(infoPtr, &rcView)) vertInfo.nMax = rcView.bottom - rcView.top;
2090 vertInfo.fMask = SIF_RANGE | SIF_PAGE;
2091 vertInfo.nMax = max(vertInfo.nMax - 1, 0);
2092 dy = GetScrollPos(infoPtr->hwndSelf, SB_VERT);
2093 dy -= SetScrollInfo(infoPtr->hwndSelf, SB_VERT, &vertInfo, TRUE);
2094 TRACE("vertInfo=%s\n", debugscrollinfo(&vertInfo));
2096 /* Change of the range may have changed the scroll pos. If so move the content */
2097 if (dx != 0 || dy != 0)
2100 listRect = infoPtr->rcList;
2101 ScrollWindowEx(infoPtr->hwndSelf, dx, dy, &listRect, &listRect, 0, 0,
2102 SW_ERASE | SW_INVALIDATE);
2105 /* Update the Header Control */
2106 if (infoPtr->hwndHeader)
2108 horzInfo.fMask = SIF_POS;
2109 GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &horzInfo);
2110 LISTVIEW_UpdateHeaderSize(infoPtr, horzInfo.nPos);
2117 * Shows/hides the focus rectangle.
2120 * [I] infoPtr : valid pointer to the listview structure
2121 * [I] fShow : TRUE to show the focus, FALSE to hide it.
2126 static void LISTVIEW_ShowFocusRect(const LISTVIEW_INFO *infoPtr, BOOL fShow)
2130 TRACE("fShow=%d, nItem=%d\n", fShow, infoPtr->nFocusedItem);
2132 if (infoPtr->nFocusedItem < 0) return;
2134 /* we need some gymnastics in ICON mode to handle large items */
2135 if (infoPtr->uView == LV_VIEW_ICON)
2139 LISTVIEW_GetItemBox(infoPtr, infoPtr->nFocusedItem, &rcBox);
2140 if ((rcBox.bottom - rcBox.top) > infoPtr->nItemHeight)
2142 LISTVIEW_InvalidateRect(infoPtr, &rcBox);
2147 if (!(hdc = GetDC(infoPtr->hwndSelf))) return;
2149 /* for some reason, owner draw should work only in report mode */
2150 if ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (infoPtr->uView == LV_VIEW_DETAILS))
2155 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
2156 HFONT hOldFont = SelectObject(hdc, hFont);
2158 item.iItem = infoPtr->nFocusedItem;
2160 item.mask = LVIF_PARAM;
2161 if (!LISTVIEW_GetItemW(infoPtr, &item)) goto done;
2163 ZeroMemory(&dis, sizeof(dis));
2164 dis.CtlType = ODT_LISTVIEW;
2165 dis.CtlID = (UINT)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
2166 dis.itemID = item.iItem;
2167 dis.itemAction = ODA_FOCUS;
2168 if (fShow) dis.itemState |= ODS_FOCUS;
2169 dis.hwndItem = infoPtr->hwndSelf;
2171 LISTVIEW_GetItemBox(infoPtr, dis.itemID, &dis.rcItem);
2172 dis.itemData = item.lParam;
2174 SendMessageW(infoPtr->hwndNotify, WM_DRAWITEM, dis.CtlID, (LPARAM)&dis);
2176 SelectObject(hdc, hOldFont);
2180 LISTVIEW_DrawFocusRect(infoPtr, hdc);
2183 ReleaseDC(infoPtr->hwndSelf, hdc);
2187 * Invalidates all visible selected items.
2189 static void LISTVIEW_InvalidateSelectedItems(const LISTVIEW_INFO *infoPtr)
2193 iterator_frameditems(&i, infoPtr, &infoPtr->rcList);
2194 while(iterator_next(&i))
2196 if (LISTVIEW_GetItemState(infoPtr, i.nItem, LVIS_SELECTED))
2197 LISTVIEW_InvalidateItem(infoPtr, i.nItem);
2199 iterator_destroy(&i);
2204 * DESCRIPTION: [INTERNAL]
2205 * Computes an item's (left,top) corner, relative to rcView.
2206 * That is, the position has NOT been made relative to the Origin.
2207 * This is deliberate, to avoid computing the Origin over, and
2208 * over again, when this function is called in a loop. Instead,
2209 * one can factor the computation of the Origin before the loop,
2210 * and offset the value returned by this function, on every iteration.
2213 * [I] infoPtr : valid pointer to the listview structure
2214 * [I] nItem : item number
2215 * [O] lpptOrig : item top, left corner
2220 static void LISTVIEW_GetItemOrigin(const LISTVIEW_INFO *infoPtr, INT nItem, LPPOINT lpptPosition)
2222 assert(nItem >= 0 && nItem < infoPtr->nItemCount);
2224 if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
2226 lpptPosition->x = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosX, nItem);
2227 lpptPosition->y = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosY, nItem);
2229 else if (infoPtr->uView == LV_VIEW_LIST)
2231 INT nCountPerColumn = LISTVIEW_GetCountPerColumn(infoPtr);
2232 lpptPosition->x = nItem / nCountPerColumn * infoPtr->nItemWidth;
2233 lpptPosition->y = nItem % nCountPerColumn * infoPtr->nItemHeight;
2235 else /* LV_VIEW_DETAILS */
2237 lpptPosition->x = REPORT_MARGINX;
2238 /* item is always at zero indexed column */
2239 if (DPA_GetPtrCount(infoPtr->hdpaColumns) > 0)
2240 lpptPosition->x += LISTVIEW_GetColumnInfo(infoPtr, 0)->rcHeader.left;
2241 lpptPosition->y = nItem * infoPtr->nItemHeight;
2246 * DESCRIPTION: [INTERNAL]
2247 * Compute the rectangles of an item. This is to localize all
2248 * the computations in one place. If you are not interested in some
2249 * of these values, simply pass in a NULL -- the function is smart
2250 * enough to compute only what's necessary. The function computes
2251 * the standard rectangles (BOUNDS, ICON, LABEL) plus a non-standard
2252 * one, the BOX rectangle. This rectangle is very cheap to compute,
2253 * and is guaranteed to contain all the other rectangles. Computing
2254 * the ICON rect is also cheap, but all the others are potentially
2255 * expensive. This gives an easy and effective optimization when
2256 * searching (like point inclusion, or rectangle intersection):
2257 * first test against the BOX, and if TRUE, test against the desired
2259 * If the function does not have all the necessary information
2260 * to computed the requested rectangles, will crash with a
2261 * failed assertion. This is done so we catch all programming
2262 * errors, given that the function is called only from our code.
2264 * We have the following 'special' meanings for a few fields:
2265 * * If LVIS_FOCUSED is set, we assume the item has the focus
2266 * This is important in ICON mode, where it might get a larger
2267 * then usual rectangle
2269 * Please note that subitem support works only in REPORT mode.
2272 * [I] infoPtr : valid pointer to the listview structure
2273 * [I] lpLVItem : item to compute the measures for
2274 * [O] lprcBox : ptr to Box rectangle
2275 * Same as LVM_GETITEMRECT with LVIR_BOUNDS
2276 * [0] lprcSelectBox : ptr to select box rectangle
2277 * Same as LVM_GETITEMRECT with LVIR_SELECTEDBOUNDS
2278 * [O] lprcIcon : ptr to Icon rectangle
2279 * Same as LVM_GETITEMRECT with LVIR_ICON
2280 * [O] lprcStateIcon: ptr to State Icon rectangle
2281 * [O] lprcLabel : ptr to Label rectangle
2282 * Same as LVM_GETITEMRECT with LVIR_LABEL
2287 static void LISTVIEW_GetItemMetrics(const LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem,
2288 LPRECT lprcBox, LPRECT lprcSelectBox,
2289 LPRECT lprcIcon, LPRECT lprcStateIcon, LPRECT lprcLabel)
2291 BOOL doSelectBox = FALSE, doIcon = FALSE, doLabel = FALSE, oversizedBox = FALSE;
2292 RECT Box, SelectBox, Icon, Label;
2293 COLUMN_INFO *lpColumnInfo = NULL;
2294 SIZE labelSize = { 0, 0 };
2296 TRACE("(lpLVItem=%s)\n", debuglvitem_t(lpLVItem, TRUE));
2298 /* Be smart and try to figure out the minimum we have to do */
2299 if (lpLVItem->iSubItem) assert(infoPtr->uView == LV_VIEW_DETAILS);
2300 if (infoPtr->uView == LV_VIEW_ICON && (lprcBox || lprcLabel))
2302 assert((lpLVItem->mask & LVIF_STATE) && (lpLVItem->stateMask & LVIS_FOCUSED));
2303 if (lpLVItem->state & LVIS_FOCUSED) oversizedBox = doLabel = TRUE;
2305 if (lprcSelectBox) doSelectBox = TRUE;
2306 if (lprcLabel) doLabel = TRUE;
2307 if (doLabel || lprcIcon || lprcStateIcon) doIcon = TRUE;
2314 /************************************************************/
2315 /* compute the box rectangle (it should be cheap to do) */
2316 /************************************************************/
2317 if (lpLVItem->iSubItem || infoPtr->uView == LV_VIEW_DETAILS)
2318 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, lpLVItem->iSubItem);
2320 if (lpLVItem->iSubItem)
2322 Box = lpColumnInfo->rcHeader;
2327 Box.right = infoPtr->nItemWidth;
2330 Box.bottom = infoPtr->nItemHeight;
2332 /******************************************************************/
2333 /* compute ICON bounding box (ala LVM_GETITEMRECT) and STATEICON */
2334 /******************************************************************/
2337 LONG state_width = 0;
2339 if (infoPtr->himlState && lpLVItem->iSubItem == 0)
2340 state_width = infoPtr->iconStateSize.cx;
2342 if (infoPtr->uView == LV_VIEW_ICON)
2344 Icon.left = Box.left + state_width;
2345 if (infoPtr->himlNormal)
2346 Icon.left += (infoPtr->nItemWidth - infoPtr->iconSize.cx - state_width) / 2;
2347 Icon.top = Box.top + ICON_TOP_PADDING;
2348 Icon.right = Icon.left;
2349 Icon.bottom = Icon.top;
2350 if (infoPtr->himlNormal)
2352 Icon.right += infoPtr->iconSize.cx;
2353 Icon.bottom += infoPtr->iconSize.cy;
2356 else /* LV_VIEW_SMALLICON, LV_VIEW_LIST or LV_VIEW_DETAILS */
2358 Icon.left = Box.left + state_width;
2360 if (infoPtr->uView == LV_VIEW_DETAILS && lpLVItem->iSubItem == 0)
2362 /* we need the indent in report mode */
2363 assert(lpLVItem->mask & LVIF_INDENT);
2364 Icon.left += infoPtr->iconSize.cx * lpLVItem->iIndent + REPORT_MARGINX;
2368 Icon.right = Icon.left;
2369 if (infoPtr->himlSmall &&
2370 (!lpColumnInfo || lpLVItem->iSubItem == 0 ||
2371 ((infoPtr->dwLvExStyle & LVS_EX_SUBITEMIMAGES) && lpLVItem->iImage != I_IMAGECALLBACK)))
2372 Icon.right += infoPtr->iconSize.cx;
2373 Icon.bottom = Icon.top + infoPtr->iconSize.cy;
2375 if(lprcIcon) *lprcIcon = Icon;
2376 TRACE(" - icon=%s\n", wine_dbgstr_rect(&Icon));
2378 /* TODO: is this correct? */
2381 lprcStateIcon->left = Icon.left - state_width;
2382 lprcStateIcon->right = Icon.left;
2383 lprcStateIcon->top = Icon.top;
2384 lprcStateIcon->bottom = lprcStateIcon->top + infoPtr->iconSize.cy;
2385 TRACE(" - state icon=%s\n", wine_dbgstr_rect(lprcStateIcon));
2388 else Icon.right = 0;
2390 /************************************************************/
2391 /* compute LABEL bounding box (ala LVM_GETITEMRECT) */
2392 /************************************************************/
2395 /* calculate how far to the right can the label stretch */
2396 Label.right = Box.right;
2397 if (infoPtr->uView == LV_VIEW_DETAILS)
2399 if (lpLVItem->iSubItem == 0)
2401 /* we need a zero based rect here */
2402 Label = lpColumnInfo->rcHeader;
2403 OffsetRect(&Label, -Label.left, 0);
2407 if (lpLVItem->iSubItem || ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && infoPtr->uView == LV_VIEW_DETAILS))
2409 labelSize.cx = infoPtr->nItemWidth;
2410 labelSize.cy = infoPtr->nItemHeight;
2414 /* we need the text in non owner draw mode */
2415 assert(lpLVItem->mask & LVIF_TEXT);
2416 if (is_text(lpLVItem->pszText))
2418 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
2419 HDC hdc = GetDC(infoPtr->hwndSelf);
2420 HFONT hOldFont = SelectObject(hdc, hFont);
2424 /* compute rough rectangle where the label will go */
2425 SetRectEmpty(&rcText);
2426 rcText.right = infoPtr->nItemWidth - TRAILING_LABEL_PADDING;
2427 rcText.bottom = infoPtr->nItemHeight;
2428 if (infoPtr->uView == LV_VIEW_ICON)
2429 rcText.bottom -= ICON_TOP_PADDING + infoPtr->iconSize.cy + ICON_BOTTOM_PADDING;
2431 /* now figure out the flags */
2432 if (infoPtr->uView == LV_VIEW_ICON)
2433 uFormat = oversizedBox ? LV_FL_DT_FLAGS : LV_ML_DT_FLAGS;
2435 uFormat = LV_SL_DT_FLAGS;
2437 DrawTextW (hdc, lpLVItem->pszText, -1, &rcText, uFormat | DT_CALCRECT);
2439 if (rcText.right != rcText.left)
2440 labelSize.cx = min(rcText.right - rcText.left + TRAILING_LABEL_PADDING, infoPtr->nItemWidth);
2442 labelSize.cy = rcText.bottom - rcText.top;
2444 SelectObject(hdc, hOldFont);
2445 ReleaseDC(infoPtr->hwndSelf, hdc);
2449 if (infoPtr->uView == LV_VIEW_ICON)
2451 Label.left = Box.left + (infoPtr->nItemWidth - labelSize.cx) / 2;
2452 Label.top = Box.top + ICON_TOP_PADDING_HITABLE +
2453 infoPtr->iconSize.cy + ICON_BOTTOM_PADDING;
2454 Label.right = Label.left + labelSize.cx;
2455 Label.bottom = Label.top + infoPtr->nItemHeight;
2456 if (!oversizedBox && labelSize.cy > infoPtr->ntmHeight)
2458 labelSize.cy = min(Box.bottom - Label.top, labelSize.cy);
2459 labelSize.cy /= infoPtr->ntmHeight;
2460 labelSize.cy = max(labelSize.cy, 1);
2461 labelSize.cy *= infoPtr->ntmHeight;
2463 Label.bottom = Label.top + labelSize.cy + HEIGHT_PADDING;
2465 else if (infoPtr->uView == LV_VIEW_DETAILS)
2467 Label.left = Icon.right;
2468 Label.top = Box.top;
2469 Label.right = lpLVItem->iSubItem ? lpColumnInfo->rcHeader.right :
2470 lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left;
2471 Label.bottom = Label.top + infoPtr->nItemHeight;
2473 else /* LV_VIEW_SMALLICON or LV_VIEW_LIST */
2475 Label.left = Icon.right;
2476 Label.top = Box.top;
2477 Label.right = min(Label.left + labelSize.cx, Label.right);
2478 Label.bottom = Label.top + infoPtr->nItemHeight;
2481 if (lprcLabel) *lprcLabel = Label;
2482 TRACE(" - label=%s\n", wine_dbgstr_rect(&Label));
2485 /************************************************************/
2486 /* compute SELECT bounding box */
2487 /************************************************************/
2490 if (infoPtr->uView == LV_VIEW_DETAILS)
2492 SelectBox.left = Icon.left;
2493 SelectBox.top = Box.top;
2494 SelectBox.bottom = Box.bottom;
2497 SelectBox.right = min(Label.left + labelSize.cx, Label.right);
2499 SelectBox.right = min(Label.left + MAX_EMPTYTEXT_SELECT_WIDTH, Label.right);
2503 UnionRect(&SelectBox, &Icon, &Label);
2505 if (lprcSelectBox) *lprcSelectBox = SelectBox;
2506 TRACE(" - select box=%s\n", wine_dbgstr_rect(&SelectBox));
2509 /* Fix the Box if necessary */
2512 if (oversizedBox) UnionRect(lprcBox, &Box, &Label);
2513 else *lprcBox = Box;
2515 TRACE(" - box=%s\n", wine_dbgstr_rect(&Box));
2519 * DESCRIPTION: [INTERNAL]
2522 * [I] infoPtr : valid pointer to the listview structure
2523 * [I] nItem : item number
2524 * [O] lprcBox : ptr to Box rectangle
2529 static void LISTVIEW_GetItemBox(const LISTVIEW_INFO *infoPtr, INT nItem, LPRECT lprcBox)
2531 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
2532 POINT Position, Origin;
2535 LISTVIEW_GetOrigin(infoPtr, &Origin);
2536 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
2538 /* Be smart and try to figure out the minimum we have to do */
2540 if (infoPtr->uView == LV_VIEW_ICON && infoPtr->bFocus && LISTVIEW_GetItemState(infoPtr, nItem, LVIS_FOCUSED))
2541 lvItem.mask |= LVIF_TEXT;
2542 lvItem.iItem = nItem;
2543 lvItem.iSubItem = 0;
2544 lvItem.pszText = szDispText;
2545 lvItem.cchTextMax = DISP_TEXT_SIZE;
2546 if (lvItem.mask) LISTVIEW_GetItemW(infoPtr, &lvItem);
2547 if (infoPtr->uView == LV_VIEW_ICON)
2549 lvItem.mask |= LVIF_STATE;
2550 lvItem.stateMask = LVIS_FOCUSED;
2551 lvItem.state = (lvItem.mask & LVIF_TEXT ? LVIS_FOCUSED : 0);
2553 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, lprcBox, 0, 0, 0, 0);
2555 if (infoPtr->uView == LV_VIEW_DETAILS && infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT &&
2556 SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, 0, 0))
2558 OffsetRect(lprcBox, Origin.x, Position.y + Origin.y);
2561 OffsetRect(lprcBox, Position.x + Origin.x, Position.y + Origin.y);
2564 /* LISTVIEW_MapIdToIndex helper */
2565 static INT CALLBACK MapIdSearchCompare(LPVOID p1, LPVOID p2, LPARAM lParam)
2567 ITEM_ID *id1 = (ITEM_ID*)p1;
2568 ITEM_ID *id2 = (ITEM_ID*)p2;
2570 if (id1->id == id2->id) return 0;
2572 return (id1->id < id2->id) ? -1 : 1;
2577 * Returns the item index for id specified.
2580 * [I] infoPtr : valid pointer to the listview structure
2581 * [I] iID : item id to get index for
2584 * Item index, or -1 on failure.
2586 static INT LISTVIEW_MapIdToIndex(const LISTVIEW_INFO *infoPtr, UINT iID)
2591 TRACE("iID=%d\n", iID);
2593 if (infoPtr->dwStyle & LVS_OWNERDATA) return -1;
2594 if (infoPtr->nItemCount == 0) return -1;
2597 index = DPA_Search(infoPtr->hdpaItemIds, &ID, -1, MapIdSearchCompare, 0, DPAS_SORTED);
2601 ITEM_ID *lpID = DPA_GetPtr(infoPtr->hdpaItemIds, index);
2602 return DPA_GetPtrIndex(infoPtr->hdpaItems, lpID->item);
2610 * Returns the item id for index given.
2613 * [I] infoPtr : valid pointer to the listview structure
2614 * [I] iItem : item index to get id for
2619 static DWORD LISTVIEW_MapIndexToId(const LISTVIEW_INFO *infoPtr, INT iItem)
2624 TRACE("iItem=%d\n", iItem);
2626 if (infoPtr->dwStyle & LVS_OWNERDATA) return -1;
2627 if (iItem < 0 || iItem >= infoPtr->nItemCount) return -1;
2629 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, iItem);
2630 lpItem = DPA_GetPtr(hdpaSubItems, 0);
2632 return lpItem->id->id;
2637 * Returns the current icon position, and advances it along the top.
2638 * The returned position is not offset by Origin.
2641 * [I] infoPtr : valid pointer to the listview structure
2642 * [O] lpPos : will get the current icon position
2647 static void LISTVIEW_NextIconPosTop(LISTVIEW_INFO *infoPtr, LPPOINT lpPos)
2649 INT nListWidth = infoPtr->rcList.right - infoPtr->rcList.left;
2651 *lpPos = infoPtr->currIconPos;
2653 infoPtr->currIconPos.x += infoPtr->nItemWidth;
2654 if (infoPtr->currIconPos.x + infoPtr->nItemWidth <= nListWidth) return;
2656 infoPtr->currIconPos.x = 0;
2657 infoPtr->currIconPos.y += infoPtr->nItemHeight;
2663 * Returns the current icon position, and advances it down the left edge.
2664 * The returned position is not offset by Origin.
2667 * [I] infoPtr : valid pointer to the listview structure
2668 * [O] lpPos : will get the current icon position
2673 static void LISTVIEW_NextIconPosLeft(LISTVIEW_INFO *infoPtr, LPPOINT lpPos)
2675 INT nListHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
2677 *lpPos = infoPtr->currIconPos;
2679 infoPtr->currIconPos.y += infoPtr->nItemHeight;
2680 if (infoPtr->currIconPos.y + infoPtr->nItemHeight <= nListHeight) return;
2682 infoPtr->currIconPos.x += infoPtr->nItemWidth;
2683 infoPtr->currIconPos.y = 0;
2689 * Moves an icon to the specified position.
2690 * It takes care of invalidating the item, etc.
2693 * [I] infoPtr : valid pointer to the listview structure
2694 * [I] nItem : the item to move
2695 * [I] lpPos : the new icon position
2696 * [I] isNew : flags the item as being new
2702 static BOOL LISTVIEW_MoveIconTo(const LISTVIEW_INFO *infoPtr, INT nItem, const POINT *lppt, BOOL isNew)
2708 old.x = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosX, nItem);
2709 old.y = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosY, nItem);
2711 if (lppt->x == old.x && lppt->y == old.y) return TRUE;
2712 LISTVIEW_InvalidateItem(infoPtr, nItem);
2715 /* Allocating a POINTER for every item is too resource intensive,
2716 * so we'll keep the (x,y) in different arrays */
2717 if (!DPA_SetPtr(infoPtr->hdpaPosX, nItem, (void *)(LONG_PTR)lppt->x)) return FALSE;
2718 if (!DPA_SetPtr(infoPtr->hdpaPosY, nItem, (void *)(LONG_PTR)lppt->y)) return FALSE;
2720 LISTVIEW_InvalidateItem(infoPtr, nItem);
2727 * Arranges listview items in icon display mode.
2730 * [I] infoPtr : valid pointer to the listview structure
2731 * [I] nAlignCode : alignment code
2737 static BOOL LISTVIEW_Arrange(LISTVIEW_INFO *infoPtr, INT nAlignCode)
2739 void (*next_pos)(LISTVIEW_INFO *, LPPOINT);
2743 if (infoPtr->uView != LV_VIEW_ICON && infoPtr->uView != LV_VIEW_SMALLICON) return FALSE;
2745 TRACE("nAlignCode=%d\n", nAlignCode);
2747 if (nAlignCode == LVA_DEFAULT)
2749 if (infoPtr->dwStyle & LVS_ALIGNLEFT) nAlignCode = LVA_ALIGNLEFT;
2750 else nAlignCode = LVA_ALIGNTOP;
2755 case LVA_ALIGNLEFT: next_pos = LISTVIEW_NextIconPosLeft; break;
2756 case LVA_ALIGNTOP: next_pos = LISTVIEW_NextIconPosTop; break;
2757 case LVA_SNAPTOGRID: next_pos = LISTVIEW_NextIconPosTop; break; /* FIXME */
2758 default: return FALSE;
2761 infoPtr->bAutoarrange = TRUE;
2762 infoPtr->currIconPos.x = infoPtr->currIconPos.y = 0;
2763 for (i = 0; i < infoPtr->nItemCount; i++)
2765 next_pos(infoPtr, &pos);
2766 LISTVIEW_MoveIconTo(infoPtr, i, &pos, FALSE);
2774 * Retrieves the bounding rectangle of all the items, not offset by Origin.
2775 * For LVS_REPORT always returns empty rectangle.
2778 * [I] infoPtr : valid pointer to the listview structure
2779 * [O] lprcView : bounding rectangle
2785 static void LISTVIEW_GetAreaRect(const LISTVIEW_INFO *infoPtr, LPRECT lprcView)
2789 SetRectEmpty(lprcView);
2791 switch (infoPtr->uView)
2794 case LV_VIEW_SMALLICON:
2795 for (i = 0; i < infoPtr->nItemCount; i++)
2797 x = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosX, i);
2798 y = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosY, i);
2799 lprcView->right = max(lprcView->right, x);
2800 lprcView->bottom = max(lprcView->bottom, y);
2802 if (infoPtr->nItemCount > 0)
2804 lprcView->right += infoPtr->nItemWidth;
2805 lprcView->bottom += infoPtr->nItemHeight;
2810 y = LISTVIEW_GetCountPerColumn(infoPtr);
2811 x = infoPtr->nItemCount / y;
2812 if (infoPtr->nItemCount % y) x++;
2813 lprcView->right = x * infoPtr->nItemWidth;
2814 lprcView->bottom = y * infoPtr->nItemHeight;
2821 * Retrieves the bounding rectangle of all the items.
2824 * [I] infoPtr : valid pointer to the listview structure
2825 * [O] lprcView : bounding rectangle
2831 static BOOL LISTVIEW_GetViewRect(const LISTVIEW_INFO *infoPtr, LPRECT lprcView)
2835 TRACE("(lprcView=%p)\n", lprcView);
2837 if (!lprcView) return FALSE;
2839 LISTVIEW_GetAreaRect(infoPtr, lprcView);
2841 if (infoPtr->uView != LV_VIEW_DETAILS)
2843 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
2844 OffsetRect(lprcView, ptOrigin.x, ptOrigin.y);
2847 TRACE("lprcView=%s\n", wine_dbgstr_rect(lprcView));
2854 * Retrieves the subitem pointer associated with the subitem index.
2857 * [I] hdpaSubItems : DPA handle for a specific item
2858 * [I] nSubItem : index of subitem
2861 * SUCCESS : subitem pointer
2864 static SUBITEM_INFO* LISTVIEW_GetSubItemPtr(HDPA hdpaSubItems, INT nSubItem)
2866 SUBITEM_INFO *lpSubItem;
2869 /* we should binary search here if need be */
2870 for (i = 1; i < DPA_GetPtrCount(hdpaSubItems); i++)
2872 lpSubItem = DPA_GetPtr(hdpaSubItems, i);
2873 if (lpSubItem->iSubItem == nSubItem)
2883 * Calculates the desired item width.
2886 * [I] infoPtr : valid pointer to the listview structure
2889 * The desired item width.
2891 static INT LISTVIEW_CalculateItemWidth(const LISTVIEW_INFO *infoPtr)
2895 TRACE("uView=%d\n", infoPtr->uView);
2897 if (infoPtr->uView == LV_VIEW_ICON)
2898 nItemWidth = infoPtr->iconSpacing.cx;
2899 else if (infoPtr->uView == LV_VIEW_DETAILS)
2901 if (DPA_GetPtrCount(infoPtr->hdpaColumns) > 0)
2906 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX,
2907 DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, 0);
2909 LISTVIEW_GetHeaderRect(infoPtr, index, &rcHeader);
2910 nItemWidth = rcHeader.right;
2913 else /* LV_VIEW_SMALLICON, or LV_VIEW_LIST */
2915 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
2919 lvItem.mask = LVIF_TEXT;
2920 lvItem.iSubItem = 0;
2922 for (i = 0; i < infoPtr->nItemCount; i++)
2925 lvItem.pszText = szDispText;
2926 lvItem.cchTextMax = DISP_TEXT_SIZE;
2927 if (LISTVIEW_GetItemW(infoPtr, &lvItem))
2928 nItemWidth = max(LISTVIEW_GetStringWidthT(infoPtr, lvItem.pszText, TRUE),
2932 if (infoPtr->himlSmall) nItemWidth += infoPtr->iconSize.cx;
2933 if (infoPtr->himlState) nItemWidth += infoPtr->iconStateSize.cx;
2935 nItemWidth = max(DEFAULT_COLUMN_WIDTH, nItemWidth + WIDTH_PADDING);
2943 * Calculates the desired item height.
2946 * [I] infoPtr : valid pointer to the listview structure
2949 * The desired item height.
2951 static INT LISTVIEW_CalculateItemHeight(const LISTVIEW_INFO *infoPtr)
2955 TRACE("uView=%d\n", infoPtr->uView);
2957 if (infoPtr->uView == LV_VIEW_ICON)
2958 nItemHeight = infoPtr->iconSpacing.cy;
2961 nItemHeight = infoPtr->ntmHeight;
2962 if (infoPtr->himlState)
2963 nItemHeight = max(nItemHeight, infoPtr->iconStateSize.cy);
2964 if (infoPtr->himlSmall)
2965 nItemHeight = max(nItemHeight, infoPtr->iconSize.cy);
2966 nItemHeight += HEIGHT_PADDING;
2967 if (infoPtr->nMeasureItemHeight > 0)
2968 nItemHeight = infoPtr->nMeasureItemHeight;
2971 return max(nItemHeight, 1);
2976 * Updates the width, and height of an item.
2979 * [I] infoPtr : valid pointer to the listview structure
2984 static inline void LISTVIEW_UpdateItemSize(LISTVIEW_INFO *infoPtr)
2986 infoPtr->nItemWidth = LISTVIEW_CalculateItemWidth(infoPtr);
2987 infoPtr->nItemHeight = LISTVIEW_CalculateItemHeight(infoPtr);
2993 * Retrieves and saves important text metrics info for the current
2997 * [I] infoPtr : valid pointer to the listview structure
3000 static void LISTVIEW_SaveTextMetrics(LISTVIEW_INFO *infoPtr)
3002 HDC hdc = GetDC(infoPtr->hwndSelf);
3003 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
3004 HFONT hOldFont = SelectObject(hdc, hFont);
3008 if (GetTextMetricsW(hdc, &tm))
3010 infoPtr->ntmHeight = tm.tmHeight;
3011 infoPtr->ntmMaxCharWidth = tm.tmMaxCharWidth;
3014 if (GetTextExtentPoint32A(hdc, "...", 3, &sz))
3015 infoPtr->nEllipsisWidth = sz.cx;
3017 SelectObject(hdc, hOldFont);
3018 ReleaseDC(infoPtr->hwndSelf, hdc);
3020 TRACE("tmHeight=%d\n", infoPtr->ntmHeight);
3025 * A compare function for ranges
3028 * [I] range1 : pointer to range 1;
3029 * [I] range2 : pointer to range 2;
3033 * > 0 : if range 1 > range 2
3034 * < 0 : if range 2 > range 1
3035 * = 0 : if range intersects range 2
3037 static INT CALLBACK ranges_cmp(LPVOID range1, LPVOID range2, LPARAM flags)
3041 if (((RANGE*)range1)->upper <= ((RANGE*)range2)->lower)
3043 else if (((RANGE*)range2)->upper <= ((RANGE*)range1)->lower)
3048 TRACE("range1=%s, range2=%s, cmp=%d\n", debugrange(range1), debugrange(range2), cmp);
3053 #define ranges_check(ranges, desc) if (TRACE_ON(listview)) ranges_assert(ranges, desc, __FILE__, __LINE__)
3055 static void ranges_assert(RANGES ranges, LPCSTR desc, const char *file, int line)
3060 TRACE("*** Checking %s:%d:%s ***\n", file, line, desc);
3062 assert (DPA_GetPtrCount(ranges->hdpa) >= 0);
3063 ranges_dump(ranges);
3064 if (DPA_GetPtrCount(ranges->hdpa) > 0)
3066 prev = DPA_GetPtr(ranges->hdpa, 0);
3067 assert (prev->lower >= 0 && prev->lower < prev->upper);
3068 for (i = 1; i < DPA_GetPtrCount(ranges->hdpa); i++)
3070 curr = DPA_GetPtr(ranges->hdpa, i);
3071 assert (prev->upper <= curr->lower);
3072 assert (curr->lower < curr->upper);
3076 TRACE("--- Done checking---\n");
3079 static RANGES ranges_create(int count)
3081 RANGES ranges = Alloc(sizeof(struct tagRANGES));
3082 if (!ranges) return NULL;
3083 ranges->hdpa = DPA_Create(count);
3084 if (ranges->hdpa) return ranges;
3089 static void ranges_clear(RANGES ranges)
3093 for(i = 0; i < DPA_GetPtrCount(ranges->hdpa); i++)
3094 Free(DPA_GetPtr(ranges->hdpa, i));
3095 DPA_DeleteAllPtrs(ranges->hdpa);
3099 static void ranges_destroy(RANGES ranges)
3101 if (!ranges) return;
3102 ranges_clear(ranges);
3103 DPA_Destroy(ranges->hdpa);
3107 static RANGES ranges_clone(RANGES ranges)
3112 if (!(clone = ranges_create(DPA_GetPtrCount(ranges->hdpa)))) goto fail;
3114 for (i = 0; i < DPA_GetPtrCount(ranges->hdpa); i++)
3116 RANGE *newrng = Alloc(sizeof(RANGE));
3117 if (!newrng) goto fail;
3118 *newrng = *((RANGE*)DPA_GetPtr(ranges->hdpa, i));
3119 DPA_SetPtr(clone->hdpa, i, newrng);
3124 TRACE ("clone failed\n");
3125 ranges_destroy(clone);
3129 static RANGES ranges_diff(RANGES ranges, RANGES sub)
3133 for (i = 0; i < DPA_GetPtrCount(sub->hdpa); i++)
3134 ranges_del(ranges, *((RANGE *)DPA_GetPtr(sub->hdpa, i)));
3139 static void ranges_dump(RANGES ranges)
3143 for (i = 0; i < DPA_GetPtrCount(ranges->hdpa); i++)
3144 TRACE(" %s\n", debugrange(DPA_GetPtr(ranges->hdpa, i)));
3147 static inline BOOL ranges_contain(RANGES ranges, INT nItem)
3149 RANGE srchrng = { nItem, nItem + 1 };
3151 TRACE("(nItem=%d)\n", nItem);
3152 ranges_check(ranges, "before contain");
3153 return DPA_Search(ranges->hdpa, &srchrng, 0, ranges_cmp, 0, DPAS_SORTED) != -1;
3156 static INT ranges_itemcount(RANGES ranges)
3160 for (i = 0; i < DPA_GetPtrCount(ranges->hdpa); i++)
3162 RANGE *sel = DPA_GetPtr(ranges->hdpa, i);
3163 count += sel->upper - sel->lower;
3169 static BOOL ranges_shift(RANGES ranges, INT nItem, INT delta, INT nUpper)
3171 RANGE srchrng = { nItem, nItem + 1 }, *chkrng;
3174 index = DPA_Search(ranges->hdpa, &srchrng, 0, ranges_cmp, 0, DPAS_SORTED | DPAS_INSERTAFTER);
3175 if (index == -1) return TRUE;
3177 for (; index < DPA_GetPtrCount(ranges->hdpa); index++)
3179 chkrng = DPA_GetPtr(ranges->hdpa, index);
3180 if (chkrng->lower >= nItem)
3181 chkrng->lower = max(min(chkrng->lower + delta, nUpper - 1), 0);
3182 if (chkrng->upper > nItem)
3183 chkrng->upper = max(min(chkrng->upper + delta, nUpper), 0);
3188 static BOOL ranges_add(RANGES ranges, RANGE range)
3193 TRACE("(%s)\n", debugrange(&range));
3194 ranges_check(ranges, "before add");
3196 /* try find overlapping regions first */
3197 srchrgn.lower = range.lower - 1;
3198 srchrgn.upper = range.upper + 1;
3199 index = DPA_Search(ranges->hdpa, &srchrgn, 0, ranges_cmp, 0, DPAS_SORTED);
3205 TRACE("Adding new range\n");
3207 /* create the brand new range to insert */
3208 newrgn = Alloc(sizeof(RANGE));
3209 if(!newrgn) goto fail;
3212 /* figure out where to insert it */
3213 index = DPA_Search(ranges->hdpa, newrgn, 0, ranges_cmp, 0, DPAS_SORTED | DPAS_INSERTAFTER);
3214 TRACE("index=%d\n", index);
3215 if (index == -1) index = 0;
3217 /* and get it over with */
3218 if (DPA_InsertPtr(ranges->hdpa, index, newrgn) == -1)
3226 RANGE *chkrgn, *mrgrgn;
3227 INT fromindex, mergeindex;
3229 chkrgn = DPA_GetPtr(ranges->hdpa, index);
3230 TRACE("Merge with %s @%d\n", debugrange(chkrgn), index);
3232 chkrgn->lower = min(range.lower, chkrgn->lower);
3233 chkrgn->upper = max(range.upper, chkrgn->upper);
3235 TRACE("New range %s @%d\n", debugrange(chkrgn), index);
3237 /* merge now common ranges */
3239 srchrgn.lower = chkrgn->lower - 1;
3240 srchrgn.upper = chkrgn->upper + 1;
3244 mergeindex = DPA_Search(ranges->hdpa, &srchrgn, fromindex, ranges_cmp, 0, 0);
3245 if (mergeindex == -1) break;
3246 if (mergeindex == index)
3248 fromindex = index + 1;
3252 TRACE("Merge with index %i\n", mergeindex);
3254 mrgrgn = DPA_GetPtr(ranges->hdpa, mergeindex);
3255 chkrgn->lower = min(chkrgn->lower, mrgrgn->lower);
3256 chkrgn->upper = max(chkrgn->upper, mrgrgn->upper);
3258 DPA_DeletePtr(ranges->hdpa, mergeindex);
3259 if (mergeindex < index) index --;
3263 ranges_check(ranges, "after add");
3267 ranges_check(ranges, "failed add");
3271 static BOOL ranges_del(RANGES ranges, RANGE range)
3276 TRACE("(%s)\n", debugrange(&range));
3277 ranges_check(ranges, "before del");
3279 /* we don't use DPAS_SORTED here, since we need *
3280 * to find the first overlapping range */
3281 index = DPA_Search(ranges->hdpa, &range, 0, ranges_cmp, 0, 0);
3284 chkrgn = DPA_GetPtr(ranges->hdpa, index);
3286 TRACE("Matches range %s @%d\n", debugrange(chkrgn), index);
3288 /* case 1: Same range */
3289 if ( (chkrgn->upper == range.upper) &&
3290 (chkrgn->lower == range.lower) )
3292 DPA_DeletePtr(ranges->hdpa, index);
3296 /* case 2: engulf */
3297 else if ( (chkrgn->upper <= range.upper) &&
3298 (chkrgn->lower >= range.lower) )
3300 DPA_DeletePtr(ranges->hdpa, index);
3303 /* case 3: overlap upper */
3304 else if ( (chkrgn->upper <= range.upper) &&
3305 (chkrgn->lower < range.lower) )
3307 chkrgn->upper = range.lower;
3309 /* case 4: overlap lower */
3310 else if ( (chkrgn->upper > range.upper) &&
3311 (chkrgn->lower >= range.lower) )
3313 chkrgn->lower = range.upper;
3316 /* case 5: fully internal */
3321 if (!(newrgn = Alloc(sizeof(RANGE)))) goto fail;
3322 newrgn->lower = chkrgn->lower;
3323 newrgn->upper = range.lower;
3324 chkrgn->lower = range.upper;
3325 if (DPA_InsertPtr(ranges->hdpa, index, newrgn) == -1)
3333 index = DPA_Search(ranges->hdpa, &range, index, ranges_cmp, 0, 0);
3336 ranges_check(ranges, "after del");
3340 ranges_check(ranges, "failed del");
3346 * Removes all selection ranges
3349 * [I] infoPtr : valid pointer to the listview structure
3350 * [I] toSkip : item range to skip removing the selection
3356 static BOOL LISTVIEW_DeselectAllSkipItems(LISTVIEW_INFO *infoPtr, RANGES toSkip)
3365 lvItem.stateMask = LVIS_SELECTED;
3367 /* need to clone the DPA because callbacks can change it */
3368 if (!(clone = ranges_clone(infoPtr->selectionRanges))) return FALSE;
3369 iterator_rangesitems(&i, ranges_diff(clone, toSkip));
3370 while(iterator_next(&i))
3371 LISTVIEW_SetItemState(infoPtr, i.nItem, &lvItem);
3372 /* note that the iterator destructor will free the cloned range */
3373 iterator_destroy(&i);
3378 static inline BOOL LISTVIEW_DeselectAllSkipItem(LISTVIEW_INFO *infoPtr, INT nItem)
3382 if (!(toSkip = ranges_create(1))) return FALSE;
3383 if (nItem != -1) ranges_additem(toSkip, nItem);
3384 LISTVIEW_DeselectAllSkipItems(infoPtr, toSkip);
3385 ranges_destroy(toSkip);
3389 static inline BOOL LISTVIEW_DeselectAll(LISTVIEW_INFO *infoPtr)
3391 return LISTVIEW_DeselectAllSkipItem(infoPtr, -1);
3396 * Retrieves the number of items that are marked as selected.
3399 * [I] infoPtr : valid pointer to the listview structure
3402 * Number of items selected.
3404 static INT LISTVIEW_GetSelectedCount(const LISTVIEW_INFO *infoPtr)
3406 INT nSelectedCount = 0;
3408 if (infoPtr->uCallbackMask & LVIS_SELECTED)
3411 for (i = 0; i < infoPtr->nItemCount; i++)
3413 if (LISTVIEW_GetItemState(infoPtr, i, LVIS_SELECTED))
3418 nSelectedCount = ranges_itemcount(infoPtr->selectionRanges);
3420 TRACE("nSelectedCount=%d\n", nSelectedCount);
3421 return nSelectedCount;
3426 * Manages the item focus.
3429 * [I] infoPtr : valid pointer to the listview structure
3430 * [I] nItem : item index
3433 * TRUE : focused item changed
3434 * FALSE : focused item has NOT changed
3436 static inline BOOL LISTVIEW_SetItemFocus(LISTVIEW_INFO *infoPtr, INT nItem)
3438 INT oldFocus = infoPtr->nFocusedItem;
3441 if (nItem == infoPtr->nFocusedItem) return FALSE;
3443 lvItem.state = nItem == -1 ? 0 : LVIS_FOCUSED;
3444 lvItem.stateMask = LVIS_FOCUSED;
3445 LISTVIEW_SetItemState(infoPtr, nItem == -1 ? infoPtr->nFocusedItem : nItem, &lvItem);
3447 return oldFocus != infoPtr->nFocusedItem;
3450 static INT shift_item(const LISTVIEW_INFO *infoPtr, INT nShiftItem, INT nItem, INT direction)
3452 if (nShiftItem < nItem) return nShiftItem;
3454 if (nShiftItem > nItem) return nShiftItem + direction;
3456 if (direction > 0) return nShiftItem + direction;
3458 return min(nShiftItem, infoPtr->nItemCount - 1);
3461 /* This function updates focus index.
3464 focus : current focus index
3465 item : index of item to be added/removed
3466 direction : add/remove flag
3468 static void LISTVIEW_ShiftFocus(LISTVIEW_INFO *infoPtr, INT focus, INT item, INT direction)
3470 BOOL old_change = infoPtr->bDoChangeNotify;
3472 infoPtr->bDoChangeNotify = FALSE;
3473 focus = shift_item(infoPtr, focus, item, direction);
3474 if (focus != infoPtr->nFocusedItem)
3475 LISTVIEW_SetItemFocus(infoPtr, focus);
3476 infoPtr->bDoChangeNotify = old_change;
3481 * Updates the various indices after an item has been inserted or deleted.
3484 * [I] infoPtr : valid pointer to the listview structure
3485 * [I] nItem : item index
3486 * [I] direction : Direction of shift, +1 or -1.
3491 static void LISTVIEW_ShiftIndices(LISTVIEW_INFO *infoPtr, INT nItem, INT direction)
3495 /* temporarily disable change notification while shifting items */
3496 bOldChange = infoPtr->bDoChangeNotify;
3497 infoPtr->bDoChangeNotify = FALSE;
3499 TRACE("Shifting %i, %i steps\n", nItem, direction);
3501 ranges_shift(infoPtr->selectionRanges, nItem, direction, infoPtr->nItemCount);
3503 assert(abs(direction) == 1);
3505 infoPtr->nSelectionMark = shift_item(infoPtr, infoPtr->nSelectionMark, nItem, direction);
3507 /* But we are not supposed to modify nHotItem! */
3509 infoPtr->bDoChangeNotify = bOldChange;
3515 * Adds a block of selections.
3518 * [I] infoPtr : valid pointer to the listview structure
3519 * [I] nItem : item index
3522 * Whether the window is still valid.
3524 static BOOL LISTVIEW_AddGroupSelection(LISTVIEW_INFO *infoPtr, INT nItem)
3526 INT nFirst = min(infoPtr->nSelectionMark, nItem);
3527 INT nLast = max(infoPtr->nSelectionMark, nItem);
3528 HWND hwndSelf = infoPtr->hwndSelf;
3529 NMLVODSTATECHANGE nmlv;
3534 /* Temporarily disable change notification
3535 * If the control is LVS_OWNERDATA, we need to send
3536 * only one LVN_ODSTATECHANGED notification.
3537 * See MSDN documentation for LVN_ITEMCHANGED.
3539 bOldChange = infoPtr->bDoChangeNotify;
3540 if (infoPtr->dwStyle & LVS_OWNERDATA) infoPtr->bDoChangeNotify = FALSE;
3542 if (nFirst == -1) nFirst = nItem;
3544 item.state = LVIS_SELECTED;
3545 item.stateMask = LVIS_SELECTED;
3547 for (i = nFirst; i <= nLast; i++)
3548 LISTVIEW_SetItemState(infoPtr,i,&item);
3550 ZeroMemory(&nmlv, sizeof(nmlv));
3551 nmlv.iFrom = nFirst;
3554 nmlv.uOldState = item.state;
3556 notify_hdr(infoPtr, LVN_ODSTATECHANGED, (LPNMHDR)&nmlv);
3557 if (!IsWindow(hwndSelf))
3559 infoPtr->bDoChangeNotify = bOldChange;
3566 * Sets a single group selection.
3569 * [I] infoPtr : valid pointer to the listview structure
3570 * [I] nItem : item index
3575 static void LISTVIEW_SetGroupSelection(LISTVIEW_INFO *infoPtr, INT nItem)
3582 if (!(selection = ranges_create(100))) return;
3584 item.state = LVIS_SELECTED;
3585 item.stateMask = LVIS_SELECTED;
3587 if ((infoPtr->uView == LV_VIEW_LIST) || (infoPtr->uView == LV_VIEW_DETAILS))
3589 if (infoPtr->nSelectionMark == -1)
3591 infoPtr->nSelectionMark = nItem;
3592 ranges_additem(selection, nItem);
3598 sel.lower = min(infoPtr->nSelectionMark, nItem);
3599 sel.upper = max(infoPtr->nSelectionMark, nItem) + 1;
3600 ranges_add(selection, sel);
3605 RECT rcItem, rcSel, rcSelMark;
3608 rcItem.left = LVIR_BOUNDS;
3609 if (!LISTVIEW_GetItemRect(infoPtr, nItem, &rcItem)) {
3610 ranges_destroy (selection);
3613 rcSelMark.left = LVIR_BOUNDS;
3614 if (!LISTVIEW_GetItemRect(infoPtr, infoPtr->nSelectionMark, &rcSelMark)) {
3615 ranges_destroy (selection);
3618 UnionRect(&rcSel, &rcItem, &rcSelMark);
3619 iterator_frameditems(&i, infoPtr, &rcSel);
3620 while(iterator_next(&i))
3622 LISTVIEW_GetItemPosition(infoPtr, i.nItem, &ptItem);
3623 if (PtInRect(&rcSel, ptItem)) ranges_additem(selection, i.nItem);
3625 iterator_destroy(&i);
3628 /* disable per item notifications on LVS_OWNERDATA style
3629 FIXME: single LVN_ODSTATECHANGED should be used */
3630 bOldChange = infoPtr->bDoChangeNotify;
3631 if (infoPtr->dwStyle & LVS_OWNERDATA) infoPtr->bDoChangeNotify = FALSE;
3633 LISTVIEW_DeselectAllSkipItems(infoPtr, selection);
3636 iterator_rangesitems(&i, selection);
3637 while(iterator_next(&i))
3638 LISTVIEW_SetItemState(infoPtr, i.nItem, &item);
3639 /* this will also destroy the selection */
3640 iterator_destroy(&i);
3642 infoPtr->bDoChangeNotify = bOldChange;
3644 LISTVIEW_SetItemFocus(infoPtr, nItem);
3649 * Sets a single selection.
3652 * [I] infoPtr : valid pointer to the listview structure
3653 * [I] nItem : item index
3658 static void LISTVIEW_SetSelection(LISTVIEW_INFO *infoPtr, INT nItem)
3662 TRACE("nItem=%d\n", nItem);
3664 LISTVIEW_DeselectAllSkipItem(infoPtr, nItem);
3666 lvItem.state = LVIS_FOCUSED | LVIS_SELECTED;
3667 lvItem.stateMask = LVIS_FOCUSED | LVIS_SELECTED;
3668 LISTVIEW_SetItemState(infoPtr, nItem, &lvItem);
3670 infoPtr->nSelectionMark = nItem;
3675 * Set selection(s) with keyboard.
3678 * [I] infoPtr : valid pointer to the listview structure
3679 * [I] nItem : item index
3680 * [I] space : VK_SPACE code sent
3683 * SUCCESS : TRUE (needs to be repainted)
3684 * FAILURE : FALSE (nothing has changed)
3686 static BOOL LISTVIEW_KeySelection(LISTVIEW_INFO *infoPtr, INT nItem, BOOL space)
3688 /* FIXME: pass in the state */
3689 WORD wShift = HIWORD(GetKeyState(VK_SHIFT));
3690 WORD wCtrl = HIWORD(GetKeyState(VK_CONTROL));
3691 BOOL bResult = FALSE;
3693 TRACE("nItem=%d, wShift=%d, wCtrl=%d\n", nItem, wShift, wCtrl);
3694 if ((nItem >= 0) && (nItem < infoPtr->nItemCount))
3698 if (infoPtr->dwStyle & LVS_SINGLESEL || (wShift == 0 && wCtrl == 0))
3699 LISTVIEW_SetSelection(infoPtr, nItem);
3703 LISTVIEW_SetGroupSelection(infoPtr, nItem);
3707 lvItem.state = ~LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED);
3708 lvItem.stateMask = LVIS_SELECTED;
3711 LISTVIEW_SetItemState(infoPtr, nItem, &lvItem);
3712 if (lvItem.state & LVIS_SELECTED)
3713 infoPtr->nSelectionMark = nItem;
3715 bResult = LISTVIEW_SetItemFocus(infoPtr, nItem);
3718 LISTVIEW_EnsureVisible(infoPtr, nItem, FALSE);
3721 UpdateWindow(infoPtr->hwndSelf); /* update client area */
3725 static BOOL LISTVIEW_GetItemAtPt(const LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem, POINT pt)
3727 LVHITTESTINFO lvHitTestInfo;
3729 ZeroMemory(&lvHitTestInfo, sizeof(lvHitTestInfo));
3730 lvHitTestInfo.pt.x = pt.x;
3731 lvHitTestInfo.pt.y = pt.y;
3733 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
3735 lpLVItem->mask = LVIF_PARAM;
3736 lpLVItem->iItem = lvHitTestInfo.iItem;
3737 lpLVItem->iSubItem = 0;
3739 return LISTVIEW_GetItemT(infoPtr, lpLVItem, TRUE);
3742 static inline BOOL LISTVIEW_IsHotTracking(const LISTVIEW_INFO *infoPtr)
3744 return ((infoPtr->dwLvExStyle & LVS_EX_TRACKSELECT) ||
3745 (infoPtr->dwLvExStyle & LVS_EX_ONECLICKACTIVATE) ||
3746 (infoPtr->dwLvExStyle & LVS_EX_TWOCLICKACTIVATE));
3751 * Called when the mouse is being actively tracked and has hovered for a specified
3755 * [I] infoPtr : valid pointer to the listview structure
3756 * [I] fwKeys : key indicator
3757 * [I] x,y : mouse position
3760 * 0 if the message was processed, non-zero if there was an error
3763 * LVS_EX_TRACKSELECT: An item is automatically selected when the cursor remains
3764 * over the item for a certain period of time.
3767 static LRESULT LISTVIEW_MouseHover(LISTVIEW_INFO *infoPtr, INT x, INT y)
3771 if (notify_hdr(infoPtr, NM_HOVER, &hdr)) return 0;
3773 if (LISTVIEW_IsHotTracking(infoPtr))
3781 if (LISTVIEW_GetItemAtPt(infoPtr, &item, pt))
3782 LISTVIEW_SetSelection(infoPtr, item.iItem);
3784 SetFocus(infoPtr->hwndSelf);
3790 #define SCROLL_LEFT 0x1
3791 #define SCROLL_RIGHT 0x2
3792 #define SCROLL_UP 0x4
3793 #define SCROLL_DOWN 0x8
3797 * Utility routine to draw and highlight items within a marquee selection rectangle.
3800 * [I] infoPtr : valid pointer to the listview structure
3801 * [I] coords_orig : original co-ordinates of the cursor
3802 * [I] coords_offs : offsetted coordinates of the cursor
3803 * [I] offset : offset amount
3804 * [I] scroll : Bitmask of which directions we should scroll, if at all
3809 static void LISTVIEW_MarqueeHighlight(LISTVIEW_INFO *infoPtr, const POINT *coords_orig,
3810 const POINT *coords_offs, const POINT *offset,
3813 BOOL controlDown = FALSE;
3815 ITERATOR old_elems, new_elems;
3818 if (coords_offs->x > infoPtr->marqueeOrigin.x)
3820 rect.left = infoPtr->marqueeOrigin.x;
3821 rect.right = coords_offs->x;
3825 rect.left = coords_offs->x;
3826 rect.right = infoPtr->marqueeOrigin.x;
3829 if (coords_offs->y > infoPtr->marqueeOrigin.y)
3831 rect.top = infoPtr->marqueeOrigin.y;
3832 rect.bottom = coords_offs->y;
3836 rect.top = coords_offs->y;
3837 rect.bottom = infoPtr->marqueeOrigin.y;
3840 /* Cancel out the old marquee rectangle and draw the new one */
3841 LISTVIEW_InvalidateRect(infoPtr, &infoPtr->marqueeDrawRect);
3843 /* Scroll by the appropriate distance if applicable - speed up scrolling as
3844 the cursor is further away */
3846 if ((scroll & SCROLL_LEFT) && (coords_orig->x <= 0))
3847 LISTVIEW_Scroll(infoPtr, coords_orig->x, 0);
3849 if ((scroll & SCROLL_RIGHT) && (coords_orig->x >= infoPtr->rcList.right))
3850 LISTVIEW_Scroll(infoPtr, (coords_orig->x - infoPtr->rcList.right), 0);
3852 if ((scroll & SCROLL_UP) && (coords_orig->y <= 0))
3853 LISTVIEW_Scroll(infoPtr, 0, coords_orig->y);
3855 if ((scroll & SCROLL_DOWN) && (coords_orig->y >= infoPtr->rcList.bottom))
3856 LISTVIEW_Scroll(infoPtr, 0, (coords_orig->y - infoPtr->rcList.bottom));
3858 iterator_frameditems_absolute(&old_elems, infoPtr, &infoPtr->marqueeRect);
3860 CopyRect(&infoPtr->marqueeRect, &rect);
3862 CopyRect(&infoPtr->marqueeDrawRect, &rect);
3863 OffsetRect(&infoPtr->marqueeDrawRect, offset->x, offset->y);
3865 iterator_frameditems_absolute(&new_elems, infoPtr, &infoPtr->marqueeRect);
3866 iterator_remove_common_items(&old_elems, &new_elems);
3868 /* Iterate over no longer selected items */
3869 while (iterator_next(&old_elems))
3871 if (old_elems.nItem > -1)
3873 if (LISTVIEW_GetItemState(infoPtr, old_elems.nItem, LVIS_SELECTED) == LVIS_SELECTED)
3876 item.state = LVIS_SELECTED;
3878 item.stateMask = LVIS_SELECTED;
3880 LISTVIEW_SetItemState(infoPtr, old_elems.nItem, &item);
3883 iterator_destroy(&old_elems);
3886 /* Iterate over newly selected items */
3887 if (GetKeyState(VK_CONTROL) & 0x8000)
3890 while (iterator_next(&new_elems))
3892 if (new_elems.nItem > -1)
3894 /* If CTRL is pressed, invert. If not, always select the item. */
3895 if ((controlDown) && (LISTVIEW_GetItemState(infoPtr, new_elems.nItem, LVIS_SELECTED)))
3898 item.state = LVIS_SELECTED;
3900 item.stateMask = LVIS_SELECTED;
3902 LISTVIEW_SetItemState(infoPtr, new_elems.nItem, &item);
3905 iterator_destroy(&new_elems);
3907 LISTVIEW_InvalidateRect(infoPtr, &infoPtr->marqueeDrawRect);
3912 * Called when we are in a marquee selection that involves scrolling the listview (ie,
3913 * the cursor is outside the bounds of the client area). This is a TIMERPROC.
3916 * [I] hwnd : Handle to the listview
3917 * [I] uMsg : WM_TIMER (ignored)
3918 * [I] idEvent : The timer ID interpreted as a pointer to a LISTVIEW_INFO struct
3919 * [I] dwTimer : The elapsed time (ignored)
3924 static VOID CALLBACK LISTVIEW_ScrollTimer(HWND hWnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
3926 LISTVIEW_INFO *infoPtr;
3927 SCROLLINFO scrollInfo;
3933 infoPtr = (LISTVIEW_INFO *) idEvent;
3938 /* Get the current cursor position and convert to client coordinates */
3939 GetCursorPos(&coords_orig);
3940 ScreenToClient(hWnd, &coords_orig);
3942 /* Ensure coordinates are within client bounds */
3943 coords_offs.x = max(min(coords_orig.x, infoPtr->rcList.right), 0);
3944 coords_offs.y = max(min(coords_orig.y, infoPtr->rcList.bottom), 0);
3947 LISTVIEW_GetOrigin(infoPtr, &offset);
3949 /* Offset coordinates by the appropriate amount */
3950 coords_offs.x -= offset.x;
3951 coords_offs.y -= offset.y;
3953 scrollInfo.cbSize = sizeof(SCROLLINFO);
3954 scrollInfo.fMask = SIF_ALL;
3956 /* Work out in which directions we can scroll */
3957 if (GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
3959 if (scrollInfo.nPos != scrollInfo.nMin)
3960 scroll |= SCROLL_UP;
3962 if (((scrollInfo.nPage + scrollInfo.nPos) - 1) != scrollInfo.nMax)
3963 scroll |= SCROLL_DOWN;
3966 if (GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo))
3968 if (scrollInfo.nPos != scrollInfo.nMin)
3969 scroll |= SCROLL_LEFT;
3971 if (((scrollInfo.nPage + scrollInfo.nPos) - 1) != scrollInfo.nMax)
3972 scroll |= SCROLL_RIGHT;
3975 if (((coords_orig.x <= 0) && (scroll & SCROLL_LEFT)) ||
3976 ((coords_orig.y <= 0) && (scroll & SCROLL_UP)) ||
3977 ((coords_orig.x >= infoPtr->rcList.right) && (scroll & SCROLL_RIGHT)) ||
3978 ((coords_orig.y >= infoPtr->rcList.bottom) && (scroll & SCROLL_DOWN)))
3980 LISTVIEW_MarqueeHighlight(infoPtr, &coords_orig, &coords_offs, &offset, scroll);
3986 * Called whenever WM_MOUSEMOVE is received.
3989 * [I] infoPtr : valid pointer to the listview structure
3990 * [I] fwKeys : key indicator
3991 * [I] x,y : mouse position
3994 * 0 if the message is processed, non-zero if there was an error
3996 static LRESULT LISTVIEW_MouseMove(LISTVIEW_INFO *infoPtr, WORD fwKeys, INT x, INT y)
3998 if (!(fwKeys & MK_LBUTTON))
3999 infoPtr->bLButtonDown = FALSE;
4001 if (infoPtr->bLButtonDown)
4005 LVHITTESTINFO lvHitTestInfo;
4006 WORD wDragWidth = GetSystemMetrics(SM_CXDRAG);
4007 WORD wDragHeight= GetSystemMetrics(SM_CYDRAG);
4009 if (infoPtr->bMarqueeSelect)
4019 LISTVIEW_GetOrigin(infoPtr, &offset);
4021 /* Ensure coordinates are within client bounds */
4022 coords_offs.x = max(min(x, infoPtr->rcList.right), 0);
4023 coords_offs.y = max(min(y, infoPtr->rcList.bottom), 0);
4025 /* Offset coordinates by the appropriate amount */
4026 coords_offs.x -= offset.x;
4027 coords_offs.y -= offset.y;
4029 /* Enable the timer if we're going outside our bounds, in case the user doesn't
4030 move the mouse again */
4032 if ((x <= 0) || (y <= 0) || (x >= infoPtr->rcList.right) ||
4033 (y >= infoPtr->rcList.bottom))
4035 if (!infoPtr->bScrolling)
4037 infoPtr->bScrolling = TRUE;
4038 SetTimer(infoPtr->hwndSelf, (UINT_PTR) infoPtr, 1, LISTVIEW_ScrollTimer);
4043 infoPtr->bScrolling = FALSE;
4044 KillTimer(infoPtr->hwndSelf, (UINT_PTR) infoPtr);
4047 LISTVIEW_MarqueeHighlight(infoPtr, &coords_orig, &coords_offs, &offset, 0);
4051 rect.left = infoPtr->ptClickPos.x - wDragWidth;
4052 rect.right = infoPtr->ptClickPos.x + wDragWidth;
4053 rect.top = infoPtr->ptClickPos.y - wDragHeight;
4054 rect.bottom = infoPtr->ptClickPos.y + wDragHeight;
4059 lvHitTestInfo.pt = tmp;
4060 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, TRUE);
4062 /* reset item marker */
4063 if (infoPtr->nLButtonDownItem != lvHitTestInfo.iItem)
4064 infoPtr->nLButtonDownItem = -1;
4066 if (!PtInRect(&rect, tmp))
4068 /* this path covers the following:
4069 1. WM_LBUTTONDOWN over selected item (sets focus on it)
4070 2. change focus with keys
4071 3. move mouse over item from step 1 selects it and moves focus on it */
4072 if (infoPtr->nLButtonDownItem != -1 &&
4073 !LISTVIEW_GetItemState(infoPtr, infoPtr->nLButtonDownItem, LVIS_SELECTED))
4077 lvItem.state = LVIS_FOCUSED | LVIS_SELECTED;
4078 lvItem.stateMask = LVIS_FOCUSED | LVIS_SELECTED;
4080 LISTVIEW_SetItemState(infoPtr, infoPtr->nLButtonDownItem, &lvItem);
4081 infoPtr->nLButtonDownItem = -1;
4084 if (!infoPtr->bDragging)
4086 lvHitTestInfo.pt = infoPtr->ptClickPos;
4087 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, TRUE);
4089 /* If the click is outside the range of an item, begin a
4090 highlight. If not, begin an item drag. */
4091 if (lvHitTestInfo.iItem == -1)
4095 /* If we're allowing multiple selections, send notification.
4096 If return value is non-zero, cancel. */
4097 if (!(infoPtr->dwStyle & LVS_SINGLESEL) && (notify_hdr(infoPtr, LVN_MARQUEEBEGIN, &hdr) == 0))
4099 /* Store the absolute coordinates of the click */
4101 LISTVIEW_GetOrigin(infoPtr, &offset);
4103 infoPtr->marqueeOrigin.x = infoPtr->ptClickPos.x - offset.x;
4104 infoPtr->marqueeOrigin.y = infoPtr->ptClickPos.y - offset.y;
4106 /* Begin selection and capture mouse */
4107 infoPtr->bMarqueeSelect = TRUE;
4108 SetCapture(infoPtr->hwndSelf);
4115 ZeroMemory(&nmlv, sizeof(nmlv));
4116 nmlv.iItem = lvHitTestInfo.iItem;
4117 nmlv.ptAction = infoPtr->ptClickPos;
4119 notify_listview(infoPtr, LVN_BEGINDRAG, &nmlv);
4120 infoPtr->bDragging = TRUE;
4128 /* see if we are supposed to be tracking mouse hovering */
4129 if (LISTVIEW_IsHotTracking(infoPtr)) {
4130 TRACKMOUSEEVENT trackinfo;
4132 trackinfo.cbSize = sizeof(TRACKMOUSEEVENT);
4133 trackinfo.dwFlags = TME_QUERY;
4135 /* see if we are already tracking this hwnd */
4136 _TrackMouseEvent(&trackinfo);
4138 if(!(trackinfo.dwFlags & TME_HOVER) || trackinfo.hwndTrack != infoPtr->hwndSelf) {
4139 trackinfo.dwFlags = TME_HOVER;
4140 trackinfo.dwHoverTime = infoPtr->dwHoverTime;
4141 trackinfo.hwndTrack = infoPtr->hwndSelf;
4143 /* call TRACKMOUSEEVENT so we receive WM_MOUSEHOVER messages */
4144 _TrackMouseEvent(&trackinfo);
4153 * Tests whether the item is assignable to a list with style lStyle
4155 static inline BOOL is_assignable_item(const LVITEMW *lpLVItem, LONG lStyle)
4157 if ( (lpLVItem->mask & LVIF_TEXT) &&
4158 (lpLVItem->pszText == LPSTR_TEXTCALLBACKW) &&
4159 (lStyle & (LVS_SORTASCENDING | LVS_SORTDESCENDING)) ) return FALSE;
4167 * Helper for LISTVIEW_SetItemT *only*: sets item attributes.
4170 * [I] infoPtr : valid pointer to the listview structure
4171 * [I] lpLVItem : valid pointer to new item attributes
4172 * [I] isNew : the item being set is being inserted
4173 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
4174 * [O] bChanged : will be set to TRUE if the item really changed
4180 static BOOL set_main_item(LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem, BOOL isNew, BOOL isW, BOOL *bChanged)
4186 /* stateMask is ignored for LVM_INSERTITEM */
4187 UINT stateMask = isNew ? ~0 : lpLVItem->stateMask;
4191 assert(lpLVItem->iItem >= 0 && lpLVItem->iItem < infoPtr->nItemCount);
4193 if (lpLVItem->mask == 0) return TRUE;
4195 if (infoPtr->dwStyle & LVS_OWNERDATA)
4197 /* a virtual listview only stores selection and focus */
4198 if (lpLVItem->mask & ~LVIF_STATE)
4204 HDPA hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, lpLVItem->iItem);
4205 lpItem = DPA_GetPtr(hdpaSubItems, 0);
4209 /* we need to get the lParam and state of the item */
4210 item.iItem = lpLVItem->iItem;
4211 item.iSubItem = lpLVItem->iSubItem;
4212 item.mask = LVIF_STATE | LVIF_PARAM;
4213 item.stateMask = (infoPtr->dwStyle & LVS_OWNERDATA) ? LVIS_FOCUSED | LVIS_SELECTED : ~0;
4217 if (!isNew && !LISTVIEW_GetItemW(infoPtr, &item)) return FALSE;
4219 TRACE("oldState=%x, newState=%x\n", item.state, lpLVItem->state);
4220 /* determine what fields will change */
4221 if ((lpLVItem->mask & LVIF_STATE) && ((item.state ^ lpLVItem->state) & stateMask & ~infoPtr->uCallbackMask))
4222 uChanged |= LVIF_STATE;
4224 if ((lpLVItem->mask & LVIF_IMAGE) && (lpItem->hdr.iImage != lpLVItem->iImage))
4225 uChanged |= LVIF_IMAGE;
4227 if ((lpLVItem->mask & LVIF_PARAM) && (lpItem->lParam != lpLVItem->lParam))
4228 uChanged |= LVIF_PARAM;
4230 if ((lpLVItem->mask & LVIF_INDENT) && (lpItem->iIndent != lpLVItem->iIndent))
4231 uChanged |= LVIF_INDENT;
4233 if ((lpLVItem->mask & LVIF_TEXT) && textcmpWT(lpItem->hdr.pszText, lpLVItem->pszText, isW))
4234 uChanged |= LVIF_TEXT;
4236 TRACE("change mask=0x%x\n", uChanged);
4238 memset(&nmlv, 0, sizeof(NMLISTVIEW));
4239 nmlv.iItem = lpLVItem->iItem;
4240 nmlv.uNewState = (item.state & ~stateMask) | (lpLVItem->state & stateMask);
4241 nmlv.uOldState = item.state;
4242 nmlv.uChanged = uChanged ? uChanged : lpLVItem->mask;
4243 nmlv.lParam = item.lParam;
4245 /* Send LVN_ITEMCHANGING notification, if the item is not being inserted
4246 and we are _NOT_ virtual (LVS_OWNERDATA), and change notifications
4247 are enabled. Even nothing really changed we still need to send this,
4248 in this case uChanged mask is just set to passed item mask. */
4249 if(lpItem && !isNew && infoPtr->bDoChangeNotify)
4251 HWND hwndSelf = infoPtr->hwndSelf;
4253 if (notify_listview(infoPtr, LVN_ITEMCHANGING, &nmlv))
4255 if (!IsWindow(hwndSelf))
4259 if (!uChanged) return TRUE;
4262 /* copy information */
4263 if (lpLVItem->mask & LVIF_TEXT)
4264 textsetptrT(&lpItem->hdr.pszText, lpLVItem->pszText, isW);
4266 if (lpLVItem->mask & LVIF_IMAGE)
4267 lpItem->hdr.iImage = lpLVItem->iImage;
4269 if (lpLVItem->mask & LVIF_PARAM)
4270 lpItem->lParam = lpLVItem->lParam;
4272 if (lpLVItem->mask & LVIF_INDENT)
4273 lpItem->iIndent = lpLVItem->iIndent;
4275 if (uChanged & LVIF_STATE)
4277 if (lpItem && (stateMask & ~infoPtr->uCallbackMask))
4279 lpItem->state &= ~stateMask;
4280 lpItem->state |= (lpLVItem->state & stateMask);
4282 if (lpLVItem->state & stateMask & ~infoPtr->uCallbackMask & LVIS_SELECTED)
4284 if (infoPtr->dwStyle & LVS_SINGLESEL) LISTVIEW_DeselectAllSkipItem(infoPtr, lpLVItem->iItem);
4285 ranges_additem(infoPtr->selectionRanges, lpLVItem->iItem);
4287 else if (stateMask & LVIS_SELECTED)
4289 ranges_delitem(infoPtr->selectionRanges, lpLVItem->iItem);
4291 /* if we are asked to change focus, and we manage it, do it */
4292 if (stateMask & ~infoPtr->uCallbackMask & LVIS_FOCUSED)
4294 if (lpLVItem->state & LVIS_FOCUSED)
4296 /* update selection mark */
4297 if (infoPtr->nFocusedItem == -1 && infoPtr->nSelectionMark == -1)
4298 infoPtr->nSelectionMark = lpLVItem->iItem;
4300 if (infoPtr->nFocusedItem != -1)
4302 /* remove current focus */
4303 item.mask = LVIF_STATE;
4305 item.stateMask = LVIS_FOCUSED;
4307 /* recurse with redrawing an item */
4308 LISTVIEW_SetItemState(infoPtr, infoPtr->nFocusedItem, &item);
4311 infoPtr->nFocusedItem = lpLVItem->iItem;
4312 LISTVIEW_EnsureVisible(infoPtr, lpLVItem->iItem, infoPtr->uView == LV_VIEW_LIST);
4314 else if (infoPtr->nFocusedItem == lpLVItem->iItem)
4316 infoPtr->nFocusedItem = -1;
4321 /* if we're inserting the item, we're done */
4322 if (isNew) return TRUE;
4324 /* send LVN_ITEMCHANGED notification */
4325 if (lpLVItem->mask & LVIF_PARAM) nmlv.lParam = lpLVItem->lParam;
4326 if (infoPtr->bDoChangeNotify) notify_listview(infoPtr, LVN_ITEMCHANGED, &nmlv);
4333 * Helper for LISTVIEW_{Set,Insert}ItemT *only*: sets subitem attributes.
4336 * [I] infoPtr : valid pointer to the listview structure
4337 * [I] lpLVItem : valid pointer to new subitem attributes
4338 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
4339 * [O] bChanged : will be set to TRUE if the item really changed
4345 static BOOL set_sub_item(const LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem, BOOL isW, BOOL *bChanged)
4348 SUBITEM_INFO *lpSubItem;
4350 /* we do not support subitems for virtual listviews */
4351 if (infoPtr->dwStyle & LVS_OWNERDATA) return FALSE;
4353 /* set subitem only if column is present */
4354 if (lpLVItem->iSubItem >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
4356 /* First do some sanity checks */
4357 /* The LVIF_STATE flag is valid for subitems, but does not appear to be
4358 particularly useful. We currently do not actually do anything with
4359 the flag on subitems.
4361 if (lpLVItem->mask & ~(LVIF_TEXT | LVIF_IMAGE | LVIF_STATE | LVIF_DI_SETITEM)) return FALSE;
4362 if (!(lpLVItem->mask & (LVIF_TEXT | LVIF_IMAGE | LVIF_STATE))) return TRUE;
4364 /* get the subitem structure, and create it if not there */
4365 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, lpLVItem->iItem);
4366 assert (hdpaSubItems);
4368 lpSubItem = LISTVIEW_GetSubItemPtr(hdpaSubItems, lpLVItem->iSubItem);
4371 SUBITEM_INFO *tmpSubItem;
4374 lpSubItem = Alloc(sizeof(SUBITEM_INFO));
4375 if (!lpSubItem) return FALSE;
4376 /* we could binary search here, if need be...*/
4377 for (i = 1; i < DPA_GetPtrCount(hdpaSubItems); i++)
4379 tmpSubItem = DPA_GetPtr(hdpaSubItems, i);
4380 if (tmpSubItem->iSubItem > lpLVItem->iSubItem) break;
4382 if (DPA_InsertPtr(hdpaSubItems, i, lpSubItem) == -1)
4387 lpSubItem->iSubItem = lpLVItem->iSubItem;
4388 lpSubItem->hdr.iImage = I_IMAGECALLBACK;
4392 if ((lpLVItem->mask & LVIF_IMAGE) && (lpSubItem->hdr.iImage != lpLVItem->iImage))
4394 lpSubItem->hdr.iImage = lpLVItem->iImage;
4398 if ((lpLVItem->mask & LVIF_TEXT) && textcmpWT(lpSubItem->hdr.pszText, lpLVItem->pszText, isW))
4400 textsetptrT(&lpSubItem->hdr.pszText, lpLVItem->pszText, isW);
4409 * Sets item attributes.
4412 * [I] infoPtr : valid pointer to the listview structure
4413 * [I] lpLVItem : new item attributes
4414 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
4420 static BOOL LISTVIEW_SetItemT(LISTVIEW_INFO *infoPtr, LVITEMW *lpLVItem, BOOL isW)
4422 HWND hwndSelf = infoPtr->hwndSelf;
4423 LPWSTR pszText = NULL;
4424 BOOL bResult, bChanged = FALSE;
4427 TRACE("(lpLVItem=%s, isW=%d)\n", debuglvitem_t(lpLVItem, isW), isW);
4429 if (!lpLVItem || lpLVItem->iItem < 0 || lpLVItem->iItem >= infoPtr->nItemCount)
4432 /* Store old item area */
4433 LISTVIEW_GetItemBox(infoPtr, lpLVItem->iItem, &oldItemArea);
4435 /* For efficiency, we transform the lpLVItem->pszText to Unicode here */
4436 if ((lpLVItem->mask & LVIF_TEXT) && is_text(lpLVItem->pszText))
4438 pszText = lpLVItem->pszText;
4439 lpLVItem->pszText = textdupTtoW(lpLVItem->pszText, isW);
4442 /* actually set the fields */
4443 if (!is_assignable_item(lpLVItem, infoPtr->dwStyle)) return FALSE;
4445 if (lpLVItem->iSubItem)
4446 bResult = set_sub_item(infoPtr, lpLVItem, TRUE, &bChanged);
4448 bResult = set_main_item(infoPtr, lpLVItem, FALSE, TRUE, &bChanged);
4449 if (!IsWindow(hwndSelf))
4452 /* redraw item, if necessary */
4453 if (bChanged && !infoPtr->bIsDrawing)
4455 /* this little optimization eliminates some nasty flicker */
4456 if ( infoPtr->uView == LV_VIEW_DETAILS && !(infoPtr->dwStyle & LVS_OWNERDRAWFIXED) &&
4457 !(infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) &&
4458 lpLVItem->iSubItem > 0 && lpLVItem->iSubItem <= DPA_GetPtrCount(infoPtr->hdpaColumns) )
4459 LISTVIEW_InvalidateSubItem(infoPtr, lpLVItem->iItem, lpLVItem->iSubItem);
4462 LISTVIEW_InvalidateRect(infoPtr, &oldItemArea);
4463 LISTVIEW_InvalidateItem(infoPtr, lpLVItem->iItem);
4469 textfreeT(lpLVItem->pszText, isW);
4470 lpLVItem->pszText = pszText;
4478 * Retrieves the index of the item at coordinate (0, 0) of the client area.
4481 * [I] infoPtr : valid pointer to the listview structure
4486 static INT LISTVIEW_GetTopIndex(const LISTVIEW_INFO *infoPtr)
4489 SCROLLINFO scrollInfo;
4491 scrollInfo.cbSize = sizeof(SCROLLINFO);
4492 scrollInfo.fMask = SIF_POS;
4494 if (infoPtr->uView == LV_VIEW_LIST)
4496 if (GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo))
4497 nItem = scrollInfo.nPos * LISTVIEW_GetCountPerColumn(infoPtr);
4499 else if (infoPtr->uView == LV_VIEW_DETAILS)
4501 if (GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
4502 nItem = scrollInfo.nPos;
4506 if (GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
4507 nItem = LISTVIEW_GetCountPerRow(infoPtr) * (scrollInfo.nPos / infoPtr->nItemHeight);
4510 TRACE("nItem=%d\n", nItem);
4518 * Erases the background of the given rectangle
4521 * [I] infoPtr : valid pointer to the listview structure
4522 * [I] hdc : device context handle
4523 * [I] lprcBox : clipping rectangle
4529 static inline BOOL LISTVIEW_FillBkgnd(const LISTVIEW_INFO *infoPtr, HDC hdc, const RECT *lprcBox)
4531 if (!infoPtr->hBkBrush) return FALSE;
4533 TRACE("(hdc=%p, lprcBox=%s, hBkBrush=%p)\n", hdc, wine_dbgstr_rect(lprcBox), infoPtr->hBkBrush);
4535 return FillRect(hdc, lprcBox, infoPtr->hBkBrush);
4543 * [I] infoPtr : valid pointer to the listview structure
4544 * [I] hdc : device context handle
4545 * [I] nItem : item index
4546 * [I] nSubItem : subitem index
4547 * [I] pos : item position in client coordinates
4548 * [I] cdmode : custom draw mode
4554 static BOOL LISTVIEW_DrawItem(LISTVIEW_INFO *infoPtr, HDC hdc, INT nItem, INT nSubItem, POINT pos, DWORD cdmode)
4557 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
4558 static WCHAR szCallback[] = { '(', 'c', 'a', 'l', 'l', 'b', 'a', 'c', 'k', ')', 0 };
4559 DWORD cdsubitemmode = CDRF_DODEFAULT;
4561 RECT rcSelect, rcBox, rcIcon, rcLabel, rcStateIcon;
4562 NMLVCUSTOMDRAW nmlvcd;
4567 TRACE("(hdc=%p, nItem=%d, nSubItem=%d, pos=%s)\n", hdc, nItem, nSubItem, wine_dbgstr_point(&pos));
4569 /* get information needed for drawing the item */
4570 lvItem.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_PARAM;
4571 if (nSubItem == 0) lvItem.mask |= LVIF_STATE;
4572 if (infoPtr->uView == LV_VIEW_DETAILS) lvItem.mask |= LVIF_INDENT;
4573 lvItem.stateMask = LVIS_SELECTED | LVIS_FOCUSED | LVIS_STATEIMAGEMASK | LVIS_CUT;
4574 lvItem.iItem = nItem;
4575 lvItem.iSubItem = nSubItem;
4578 lvItem.cchTextMax = DISP_TEXT_SIZE;
4579 lvItem.pszText = szDispText;
4580 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return FALSE;
4581 if (nSubItem > 0 && (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT))
4582 lvItem.state = LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED);
4583 if (lvItem.pszText == LPSTR_TEXTCALLBACKW) lvItem.pszText = szCallback;
4584 TRACE(" lvItem=%s\n", debuglvitem_t(&lvItem, TRUE));
4586 /* now check if we need to update the focus rectangle */
4587 lprcFocus = infoPtr->bFocus && (lvItem.state & LVIS_FOCUSED) ? &infoPtr->rcFocus : 0;
4589 if (!lprcFocus) lvItem.state &= ~LVIS_FOCUSED;
4590 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, &rcBox, &rcSelect, &rcIcon, &rcStateIcon, &rcLabel);
4591 OffsetRect(&rcBox, pos.x, pos.y);
4592 OffsetRect(&rcSelect, pos.x, pos.y);
4593 OffsetRect(&rcIcon, pos.x, pos.y);
4594 OffsetRect(&rcStateIcon, pos.x, pos.y);
4595 OffsetRect(&rcLabel, pos.x, pos.y);
4596 TRACE(" rcBox=%s, rcSelect=%s, rcIcon=%s. rcLabel=%s\n",
4597 wine_dbgstr_rect(&rcBox), wine_dbgstr_rect(&rcSelect),
4598 wine_dbgstr_rect(&rcIcon), wine_dbgstr_rect(&rcLabel));
4600 /* fill in the custom draw structure */
4601 customdraw_fill(&nmlvcd, infoPtr, hdc, &rcBox, &lvItem);
4603 hOldFont = GetCurrentObject(hdc, OBJ_FONT);
4604 if (nSubItem > 0) cdmode = infoPtr->cditemmode;
4605 if (cdmode & CDRF_SKIPDEFAULT) goto postpaint;
4606 if (cdmode & CDRF_NOTIFYITEMDRAW)
4607 cdsubitemmode = notify_customdraw(infoPtr, CDDS_PREPAINT, &nmlvcd);
4608 if (nSubItem == 0) infoPtr->cditemmode = cdsubitemmode;
4609 if (cdsubitemmode & CDRF_SKIPDEFAULT) goto postpaint;
4610 /* we have to send a CDDS_SUBITEM customdraw explicitly for subitem 0 */
4611 if (nSubItem == 0 && cdsubitemmode == CDRF_NOTIFYITEMDRAW)
4613 cdsubitemmode = notify_customdraw(infoPtr, CDDS_SUBITEM | CDDS_ITEMPREPAINT, &nmlvcd);
4614 if (cdsubitemmode & CDRF_SKIPDEFAULT) goto postpaint;
4616 if (nSubItem == 0 || (cdmode & CDRF_NOTIFYITEMDRAW))
4617 prepaint_setup(infoPtr, hdc, &nmlvcd, FALSE);
4618 else if ((infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) == FALSE)
4619 prepaint_setup(infoPtr, hdc, &nmlvcd, TRUE);
4621 /* in full row select, subitems, will just use main item's colors */
4622 if (nSubItem && infoPtr->uView == LV_VIEW_DETAILS && (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT))
4623 nmlvcd.clrTextBk = CLR_NONE;
4625 /* FIXME: temporary hack */
4626 rcSelect.left = rcLabel.left;
4628 /* draw the selection background, if we're drawing the main item */
4631 /* in icon mode, the label rect is really what we want to draw the
4633 if (infoPtr->uView == LV_VIEW_ICON)
4636 if (infoPtr->uView == LV_VIEW_DETAILS && (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT))
4638 /* we have to update left focus bound too if item isn't in leftmost column
4639 and reduce right box bound */
4640 if (DPA_GetPtrCount(infoPtr->hdpaColumns) > 0)
4644 if ((leftmost = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, 0, 0)))
4646 INT Originx = pos.x - LISTVIEW_GetColumnInfo(infoPtr, 0)->rcHeader.left;
4647 INT index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX,
4648 DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, 0);
4650 rcBox.right = LISTVIEW_GetColumnInfo(infoPtr, index)->rcHeader.right + Originx;
4651 rcSelect.left = LISTVIEW_GetColumnInfo(infoPtr, leftmost)->rcHeader.left + Originx;
4655 rcSelect.right = rcBox.right;
4658 if (nmlvcd.clrTextBk != CLR_NONE)
4659 ExtTextOutW(hdc, rcSelect.left, rcSelect.top, ETO_OPAQUE, &rcSelect, NULL, 0, NULL);
4660 /* store new focus rectangle */
4661 if (infoPtr->nFocusedItem == nItem) infoPtr->rcFocus = rcSelect;
4665 if (infoPtr->himlState && STATEIMAGEINDEX(lvItem.state) && (nSubItem == 0))
4667 UINT uStateImage = STATEIMAGEINDEX(lvItem.state);
4670 TRACE("uStateImage=%d\n", uStateImage);
4671 ImageList_Draw(infoPtr->himlState, uStateImage - 1, hdc,
4672 rcStateIcon.left, rcStateIcon.top, ILD_NORMAL);
4677 himl = (infoPtr->uView == LV_VIEW_ICON ? infoPtr->himlNormal : infoPtr->himlSmall);
4678 if (himl && lvItem.iImage >= 0 && !IsRectEmpty(&rcIcon))
4682 TRACE("iImage=%d\n", lvItem.iImage);
4684 if (lvItem.state & (LVIS_SELECTED | LVIS_CUT) && infoPtr->bFocus)
4685 style = ILD_SELECTED;
4689 ImageList_DrawEx(himl, lvItem.iImage, hdc, rcIcon.left, rcIcon.top,
4690 rcIcon.right - rcIcon.left, rcIcon.bottom - rcIcon.top, infoPtr->clrBk,
4691 lvItem.state & LVIS_CUT ? RGB(255, 255, 255) : CLR_DEFAULT,
4695 /* Don't bother painting item being edited */
4696 if (infoPtr->hwndEdit && nItem == infoPtr->nEditLabelItem && nSubItem == 0) goto postpaint;
4698 /* figure out the text drawing flags */
4699 uFormat = (infoPtr->uView == LV_VIEW_ICON ? (lprcFocus ? LV_FL_DT_FLAGS : LV_ML_DT_FLAGS) : LV_SL_DT_FLAGS);
4700 if (infoPtr->uView == LV_VIEW_ICON)
4701 uFormat = (lprcFocus ? LV_FL_DT_FLAGS : LV_ML_DT_FLAGS);
4704 switch (LISTVIEW_GetColumnInfo(infoPtr, nSubItem)->fmt & LVCFMT_JUSTIFYMASK)
4706 case LVCFMT_RIGHT: uFormat |= DT_RIGHT; break;
4707 case LVCFMT_CENTER: uFormat |= DT_CENTER; break;
4708 default: uFormat |= DT_LEFT;
4711 if (!(uFormat & (DT_RIGHT | DT_CENTER)))
4713 if (himl && lvItem.iImage >= 0 && !IsRectEmpty(&rcIcon)) rcLabel.left += IMAGE_PADDING;
4714 else rcLabel.left += LABEL_HOR_PADDING;
4716 else if (uFormat & DT_RIGHT) rcLabel.right -= LABEL_HOR_PADDING;
4718 /* for GRIDLINES reduce the bottom so the text formats correctly */
4719 if (infoPtr->uView == LV_VIEW_DETAILS && infoPtr->dwLvExStyle & LVS_EX_GRIDLINES)
4722 DrawTextW(hdc, lvItem.pszText, -1, &rcLabel, uFormat);
4725 if (cdsubitemmode & CDRF_NOTIFYPOSTPAINT)
4726 notify_postpaint(infoPtr, &nmlvcd);
4727 if (cdsubitemmode & CDRF_NEWFONT)
4728 SelectObject(hdc, hOldFont);
4734 * Draws listview items when in owner draw mode.
4737 * [I] infoPtr : valid pointer to the listview structure
4738 * [I] hdc : device context handle
4743 static void LISTVIEW_RefreshOwnerDraw(const LISTVIEW_INFO *infoPtr, ITERATOR *i, HDC hdc, DWORD cdmode)
4745 UINT uID = (UINT)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
4746 DWORD cditemmode = CDRF_DODEFAULT;
4747 NMLVCUSTOMDRAW nmlvcd;
4748 POINT Origin, Position;
4754 ZeroMemory(&dis, sizeof(dis));
4756 /* Get scroll info once before loop */
4757 LISTVIEW_GetOrigin(infoPtr, &Origin);
4759 /* iterate through the invalidated rows */
4760 while(iterator_next(i))
4762 item.iItem = i->nItem;
4764 item.mask = LVIF_PARAM | LVIF_STATE;
4765 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
4766 if (!LISTVIEW_GetItemW(infoPtr, &item)) continue;
4768 dis.CtlType = ODT_LISTVIEW;
4770 dis.itemID = item.iItem;
4771 dis.itemAction = ODA_DRAWENTIRE;
4773 if (item.state & LVIS_SELECTED) dis.itemState |= ODS_SELECTED;
4774 if (infoPtr->bFocus && (item.state & LVIS_FOCUSED)) dis.itemState |= ODS_FOCUS;
4775 dis.hwndItem = infoPtr->hwndSelf;
4777 LISTVIEW_GetItemOrigin(infoPtr, dis.itemID, &Position);
4778 dis.rcItem.left = Position.x + Origin.x;
4779 dis.rcItem.right = dis.rcItem.left + infoPtr->nItemWidth;
4780 dis.rcItem.top = Position.y + Origin.y;
4781 dis.rcItem.bottom = dis.rcItem.top + infoPtr->nItemHeight;
4782 dis.itemData = item.lParam;
4784 TRACE("item=%s, rcItem=%s\n", debuglvitem_t(&item, TRUE), wine_dbgstr_rect(&dis.rcItem));
4787 * Even if we do not send the CDRF_NOTIFYITEMDRAW we need to fill the nmlvcd
4788 * structure for the rest. of the paint cycle
4790 customdraw_fill(&nmlvcd, infoPtr, hdc, &dis.rcItem, &item);
4791 if (cdmode & CDRF_NOTIFYITEMDRAW)
4792 cditemmode = notify_customdraw(infoPtr, CDDS_PREPAINT, &nmlvcd);
4794 if (!(cditemmode & CDRF_SKIPDEFAULT))
4796 prepaint_setup (infoPtr, hdc, &nmlvcd, FALSE);
4797 SendMessageW(infoPtr->hwndNotify, WM_DRAWITEM, dis.CtlID, (LPARAM)&dis);
4800 if (cditemmode & CDRF_NOTIFYPOSTPAINT)
4801 notify_postpaint(infoPtr, &nmlvcd);
4807 * Draws listview items when in report display mode.
4810 * [I] infoPtr : valid pointer to the listview structure
4811 * [I] hdc : device context handle
4812 * [I] cdmode : custom draw mode
4817 static void LISTVIEW_RefreshReport(LISTVIEW_INFO *infoPtr, ITERATOR *i, HDC hdc, DWORD cdmode)
4820 RECT rcClip, rcItem;
4821 POINT Origin, Position;
4828 /* figure out what to draw */
4829 rgntype = GetClipBox(hdc, &rcClip);
4830 if (rgntype == NULLREGION) return;
4832 /* Get scroll info once before loop */
4833 LISTVIEW_GetOrigin(infoPtr, &Origin);
4835 colRanges = ranges_create(DPA_GetPtrCount(infoPtr->hdpaColumns));
4837 /* narrow down the columns we need to paint */
4838 for(col = 0; col < DPA_GetPtrCount(infoPtr->hdpaColumns); col++)
4840 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, col, 0);
4842 LISTVIEW_GetHeaderRect(infoPtr, index, &rcItem);
4843 if ((rcItem.right + Origin.x >= rcClip.left) && (rcItem.left + Origin.x < rcClip.right))
4844 ranges_additem(colRanges, index);
4846 iterator_rangesitems(&j, colRanges);
4848 /* in full row select, we _have_ to draw the main item */
4849 if (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT)
4852 /* iterate through the invalidated rows */
4853 while(iterator_next(i))
4855 LISTVIEW_GetItemOrigin(infoPtr, i->nItem, &Position);
4856 Position.y += Origin.y;
4858 /* iterate through the invalidated columns */
4859 while(iterator_next(&j))
4861 LISTVIEW_GetHeaderRect(infoPtr, j.nItem, &rcItem);
4862 Position.x = (j.nItem == 0) ? rcItem.left + Origin.x : Origin.x;
4864 if (rgntype == COMPLEXREGION && !((infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) && j.nItem == 0))
4867 rcItem.bottom = infoPtr->nItemHeight;
4868 OffsetRect(&rcItem, Origin.x, Position.y);
4869 if (!RectVisible(hdc, &rcItem)) continue;
4872 LISTVIEW_DrawItem(infoPtr, hdc, i->nItem, j.nItem, Position, cdmode);
4875 iterator_destroy(&j);
4880 * Draws the gridlines if necessary when in report display mode.
4883 * [I] infoPtr : valid pointer to the listview structure
4884 * [I] hdc : device context handle
4889 static void LISTVIEW_RefreshReportGrid(LISTVIEW_INFO *infoPtr, HDC hdc)
4895 RECT rcClip, rcItem = {0};
4903 /* figure out what to draw */
4904 rgntype = GetClipBox(hdc, &rcClip);
4905 if (rgntype == NULLREGION) return;
4907 /* Get scroll info once before loop */
4908 LISTVIEW_GetOrigin(infoPtr, &Origin);
4910 colRanges = ranges_create(DPA_GetPtrCount(infoPtr->hdpaColumns));
4912 /* narrow down the columns we need to paint */
4913 for(col = 0; col < DPA_GetPtrCount(infoPtr->hdpaColumns); col++)
4915 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, col, 0);
4917 LISTVIEW_GetHeaderRect(infoPtr, index, &rcItem);
4918 if ((rcItem.right + Origin.x >= rcClip.left) && (rcItem.left + Origin.x < rcClip.right))
4919 ranges_additem(colRanges, index);
4922 /* is right most vertical line visible? */
4923 if (DPA_GetPtrCount(infoPtr->hdpaColumns) > 0)
4925 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, 0);
4926 LISTVIEW_GetHeaderRect(infoPtr, index, &rcItem);
4927 rmost = (rcItem.right + Origin.x < rcClip.right);
4930 if ((hPen = CreatePen( PS_SOLID, 1, comctl32_color.clr3dFace )))
4932 hOldPen = SelectObject ( hdc, hPen );
4934 /* draw the vertical lines for the columns */
4935 iterator_rangesitems(&j, colRanges);
4936 while(iterator_next(&j))
4938 LISTVIEW_GetHeaderRect(infoPtr, j.nItem, &rcItem);
4939 if (rcItem.left == 0) continue; /* skip leftmost column */
4940 rcItem.left += Origin.x;
4941 rcItem.right += Origin.x;
4942 rcItem.top = infoPtr->rcList.top;
4943 rcItem.bottom = infoPtr->rcList.bottom;
4944 TRACE("vert col=%d, rcItem=%s\n", j.nItem, wine_dbgstr_rect(&rcItem));
4945 MoveToEx (hdc, rcItem.left, rcItem.top, NULL);
4946 LineTo (hdc, rcItem.left, rcItem.bottom);
4948 iterator_destroy(&j);
4949 /* draw rightmost grid line if visible */
4952 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX,
4953 DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, 0);
4954 LISTVIEW_GetHeaderRect(infoPtr, index, &rcItem);
4956 rcItem.right += Origin.x;
4958 MoveToEx (hdc, rcItem.right, infoPtr->rcList.top, NULL);
4959 LineTo (hdc, rcItem.right, infoPtr->rcList.bottom);
4962 /* draw the horizontal lines for the rows */
4963 itemheight = LISTVIEW_CalculateItemHeight(infoPtr);
4964 rcItem.left = infoPtr->rcList.left;
4965 rcItem.right = infoPtr->rcList.right;
4966 for(y = Origin.y > 1 ? Origin.y - 1 : itemheight - 1 + Origin.y % itemheight; y<=infoPtr->rcList.bottom; y+=itemheight)
4968 rcItem.bottom = rcItem.top = y;
4969 TRACE("horz rcItem=%s\n", wine_dbgstr_rect(&rcItem));
4970 MoveToEx (hdc, rcItem.left, rcItem.top, NULL);
4971 LineTo (hdc, rcItem.right, rcItem.top);
4974 SelectObject( hdc, hOldPen );
4975 DeleteObject( hPen );
4978 ranges_destroy(colRanges);
4983 * Draws listview items when in list display mode.
4986 * [I] infoPtr : valid pointer to the listview structure
4987 * [I] hdc : device context handle
4988 * [I] cdmode : custom draw mode
4993 static void LISTVIEW_RefreshList(LISTVIEW_INFO *infoPtr, ITERATOR *i, HDC hdc, DWORD cdmode)
4995 POINT Origin, Position;
4997 /* Get scroll info once before loop */
4998 LISTVIEW_GetOrigin(infoPtr, &Origin);
5000 while(iterator_prev(i))
5002 LISTVIEW_GetItemOrigin(infoPtr, i->nItem, &Position);
5003 Position.x += Origin.x;
5004 Position.y += Origin.y;
5006 LISTVIEW_DrawItem(infoPtr, hdc, i->nItem, 0, Position, cdmode);
5013 * Draws listview items.
5016 * [I] infoPtr : valid pointer to the listview structure
5017 * [I] hdc : device context handle
5018 * [I] prcErase : rect to be erased before refresh (may be NULL)
5023 static void LISTVIEW_Refresh(LISTVIEW_INFO *infoPtr, HDC hdc, const RECT *prcErase)
5025 COLORREF oldTextColor = 0, oldBkColor = 0, oldClrTextBk, oldClrText;
5026 NMLVCUSTOMDRAW nmlvcd;
5033 HBITMAP hbmp = NULL;
5036 LISTVIEW_DUMP(infoPtr);
5038 if (infoPtr->dwLvExStyle & LVS_EX_DOUBLEBUFFER) {
5039 TRACE("double buffering\n");
5041 hdc = CreateCompatibleDC(hdcOrig);
5043 ERR("Failed to create DC for backbuffer\n");
5046 hbmp = CreateCompatibleBitmap(hdcOrig, infoPtr->rcList.right,
5047 infoPtr->rcList.bottom);
5049 ERR("Failed to create bitmap for backbuffer\n");
5054 SelectObject(hdc, hbmp);
5055 SelectObject(hdc, infoPtr->hFont);
5057 if(GetClipBox(hdcOrig, &rcClient))
5058 IntersectClipRect(hdc, rcClient.left, rcClient.top, rcClient.right, rcClient.bottom);
5060 /* Save dc values we're gonna trash while drawing
5061 * FIXME: Should be done in LISTVIEW_DrawItem() */
5062 hOldFont = SelectObject(hdc, infoPtr->hFont);
5063 oldBkMode = GetBkMode(hdc);
5064 oldBkColor = GetBkColor(hdc);
5065 oldTextColor = GetTextColor(hdc);
5068 infoPtr->bIsDrawing = TRUE;
5071 LISTVIEW_FillBkgnd(infoPtr, hdc, prcErase);
5072 } else if (infoPtr->dwLvExStyle & LVS_EX_DOUBLEBUFFER) {
5073 /* If no erasing was done (usually because RedrawWindow was called
5074 * with RDW_INVALIDATE only) we need to copy the old contents into
5075 * the backbuffer before continuing. */
5076 BitBlt(hdc, infoPtr->rcList.left, infoPtr->rcList.top,
5077 infoPtr->rcList.right - infoPtr->rcList.left,
5078 infoPtr->rcList.bottom - infoPtr->rcList.top,
5079 hdcOrig, infoPtr->rcList.left, infoPtr->rcList.top, SRCCOPY);
5082 /* FIXME: Shouldn't need to do this */
5083 oldClrTextBk = infoPtr->clrTextBk;
5084 oldClrText = infoPtr->clrText;
5086 infoPtr->cditemmode = CDRF_DODEFAULT;
5088 GetClientRect(infoPtr->hwndSelf, &rcClient);
5089 customdraw_fill(&nmlvcd, infoPtr, hdc, &rcClient, 0);
5090 cdmode = notify_customdraw(infoPtr, CDDS_PREPAINT, &nmlvcd);
5091 if (cdmode & CDRF_SKIPDEFAULT) goto enddraw;
5092 prepaint_setup(infoPtr, hdc, &nmlvcd, FALSE);
5094 /* Use these colors to draw the items */
5095 infoPtr->clrTextBk = nmlvcd.clrTextBk;
5096 infoPtr->clrText = nmlvcd.clrText;
5098 /* nothing to draw */
5099 if(infoPtr->nItemCount == 0) goto enddraw;
5101 /* figure out what we need to draw */
5102 iterator_visibleitems(&i, infoPtr, hdc);
5103 range = iterator_range(&i);
5105 /* send cache hint notification */
5106 if (infoPtr->dwStyle & LVS_OWNERDATA)
5110 ZeroMemory(&nmlv, sizeof(NMLVCACHEHINT));
5111 nmlv.iFrom = range.lower;
5112 nmlv.iTo = range.upper - 1;
5113 notify_hdr(infoPtr, LVN_ODCACHEHINT, &nmlv.hdr);
5116 if ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (infoPtr->uView == LV_VIEW_DETAILS))
5117 LISTVIEW_RefreshOwnerDraw(infoPtr, &i, hdc, cdmode);
5120 if (infoPtr->uView == LV_VIEW_DETAILS)
5121 LISTVIEW_RefreshReport(infoPtr, &i, hdc, cdmode);
5122 else /* LV_VIEW_LIST, LV_VIEW_ICON or LV_VIEW_SMALLICON */
5123 LISTVIEW_RefreshList(infoPtr, &i, hdc, cdmode);
5125 /* if we have a focus rect and it's visible, draw it */
5126 if (infoPtr->bFocus && range.lower <= infoPtr->nFocusedItem &&
5127 (range.upper - 1) >= infoPtr->nFocusedItem)
5128 LISTVIEW_DrawFocusRect(infoPtr, hdc);
5130 iterator_destroy(&i);
5133 /* For LVS_EX_GRIDLINES go and draw lines */
5134 /* This includes the case where there were *no* items */
5135 if ((infoPtr->uView == LV_VIEW_DETAILS) && infoPtr->dwLvExStyle & LVS_EX_GRIDLINES)
5136 LISTVIEW_RefreshReportGrid(infoPtr, hdc);
5138 /* Draw marquee rectangle if appropriate */
5139 if (infoPtr->bMarqueeSelect)
5140 DrawFocusRect(hdc, &infoPtr->marqueeDrawRect);
5142 if (cdmode & CDRF_NOTIFYPOSTPAINT)
5143 notify_postpaint(infoPtr, &nmlvcd);
5145 infoPtr->clrTextBk = oldClrTextBk;
5146 infoPtr->clrText = oldClrText;
5149 BitBlt(hdcOrig, infoPtr->rcList.left, infoPtr->rcList.top,
5150 infoPtr->rcList.right - infoPtr->rcList.left,
5151 infoPtr->rcList.bottom - infoPtr->rcList.top,
5152 hdc, infoPtr->rcList.left, infoPtr->rcList.top, SRCCOPY);
5157 SelectObject(hdc, hOldFont);
5158 SetBkMode(hdc, oldBkMode);
5159 SetBkColor(hdc, oldBkColor);
5160 SetTextColor(hdc, oldTextColor);
5163 infoPtr->bIsDrawing = FALSE;
5169 * Calculates the approximate width and height of a given number of items.
5172 * [I] infoPtr : valid pointer to the listview structure
5173 * [I] nItemCount : number of items
5174 * [I] wWidth : width
5175 * [I] wHeight : height
5178 * Returns a DWORD. The width in the low word and the height in high word.
5180 static DWORD LISTVIEW_ApproximateViewRect(const LISTVIEW_INFO *infoPtr, INT nItemCount,
5181 WORD wWidth, WORD wHeight)
5183 DWORD dwViewRect = 0;
5185 if (nItemCount == -1)
5186 nItemCount = infoPtr->nItemCount;
5188 if (infoPtr->uView == LV_VIEW_LIST)
5190 INT nItemCountPerColumn = 1;
5191 INT nColumnCount = 0;
5193 if (wHeight == 0xFFFF)
5195 /* use current height */
5196 wHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
5199 if (wHeight < infoPtr->nItemHeight)
5200 wHeight = infoPtr->nItemHeight;
5204 if (infoPtr->nItemHeight > 0)
5206 nItemCountPerColumn = wHeight / infoPtr->nItemHeight;
5207 if (nItemCountPerColumn == 0)
5208 nItemCountPerColumn = 1;
5210 if (nItemCount % nItemCountPerColumn != 0)
5211 nColumnCount = nItemCount / nItemCountPerColumn;
5213 nColumnCount = nItemCount / nItemCountPerColumn + 1;
5217 /* Microsoft padding magic */
5218 wHeight = nItemCountPerColumn * infoPtr->nItemHeight + 2;
5219 wWidth = nColumnCount * infoPtr->nItemWidth + 2;
5221 dwViewRect = MAKELONG(wWidth, wHeight);
5223 else if (infoPtr->uView == LV_VIEW_DETAILS)
5227 if (infoPtr->nItemCount > 0)
5229 LISTVIEW_GetItemBox(infoPtr, 0, &rcBox);
5230 wWidth = rcBox.right - rcBox.left;
5231 wHeight = (rcBox.bottom - rcBox.top) * nItemCount;
5235 /* use current height and width */
5236 if (wHeight == 0xffff)
5237 wHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
5238 if (wWidth == 0xffff)
5239 wWidth = infoPtr->rcList.right - infoPtr->rcList.left;
5242 dwViewRect = MAKELONG(wWidth, wHeight);
5244 else if (infoPtr->uView == LV_VIEW_ICON)
5250 nItemWidth = infoPtr->iconSpacing.cx;
5251 nItemHeight = infoPtr->iconSpacing.cy;
5253 if (wWidth == 0xffff)
5254 wWidth = infoPtr->rcList.right - infoPtr->rcList.left;
5256 if (wWidth < nItemWidth)
5257 wWidth = nItemWidth;
5259 cols = wWidth / nItemWidth;
5260 if (cols > nItemCount)
5267 rows = nItemCount / cols;
5268 if (nItemCount % cols)
5274 wHeight = (nItemHeight * rows)+2;
5275 wWidth = (nItemWidth * cols)+2;
5277 dwViewRect = MAKELONG(wWidth, wHeight);
5279 else if (infoPtr->uView == LV_VIEW_SMALLICON)
5280 FIXME("uView == LV_VIEW_SMALLICON: not implemented\n");
5287 * Cancel edit label with saving item text.
5290 * [I] infoPtr : valid pointer to the listview structure
5293 * Always returns TRUE.
5295 static LRESULT LISTVIEW_CancelEditLabel(LISTVIEW_INFO *infoPtr)
5297 if (infoPtr->hwndEdit)
5299 /* handle value will be lost after LISTVIEW_EndEditLabelT */
5300 HWND edit = infoPtr->hwndEdit;
5302 LISTVIEW_EndEditLabelT(infoPtr, TRUE, IsWindowUnicode(infoPtr->hwndEdit));
5303 SendMessageW(edit, WM_CLOSE, 0, 0);
5311 * Create a drag image list for the specified item.
5314 * [I] infoPtr : valid pointer to the listview structure
5315 * [I] iItem : index of item
5316 * [O] lppt : Upper-left corner of the image
5319 * Returns a handle to the image list if successful, NULL otherwise.
5321 static HIMAGELIST LISTVIEW_CreateDragImage(LISTVIEW_INFO *infoPtr, INT iItem, LPPOINT lppt)
5327 HBITMAP hbmp, hOldbmp;
5328 HIMAGELIST dragList = 0;
5329 TRACE("iItem=%d Count=%d\n", iItem, infoPtr->nItemCount);
5331 if (iItem < 0 || iItem >= infoPtr->nItemCount || !lppt)
5334 rcItem.left = LVIR_BOUNDS;
5335 if (!LISTVIEW_GetItemRect(infoPtr, iItem, &rcItem))
5338 lppt->x = rcItem.left;
5339 lppt->y = rcItem.top;
5341 size.cx = rcItem.right - rcItem.left;
5342 size.cy = rcItem.bottom - rcItem.top;
5344 hdcOrig = GetDC(infoPtr->hwndSelf);
5345 hdc = CreateCompatibleDC(hdcOrig);
5346 hbmp = CreateCompatibleBitmap(hdcOrig, size.cx, size.cy);
5347 hOldbmp = SelectObject(hdc, hbmp);
5349 rcItem.left = rcItem.top = 0;
5350 rcItem.right = size.cx;
5351 rcItem.bottom = size.cy;
5352 FillRect(hdc, &rcItem, infoPtr->hBkBrush);
5355 if (LISTVIEW_DrawItem(infoPtr, hdc, iItem, 0, pos, infoPtr->cditemmode))
5357 dragList = ImageList_Create(size.cx, size.cy, ILC_COLOR, 10, 10);
5358 SelectObject(hdc, hOldbmp);
5359 ImageList_Add(dragList, hbmp, 0);
5362 SelectObject(hdc, hOldbmp);
5366 ReleaseDC(infoPtr->hwndSelf, hdcOrig);
5368 TRACE("ret=%p\n", dragList);
5376 * Removes all listview items and subitems.
5379 * [I] infoPtr : valid pointer to the listview structure
5385 static BOOL LISTVIEW_DeleteAllItems(LISTVIEW_INFO *infoPtr, BOOL destroy)
5388 HDPA hdpaSubItems = NULL;
5397 /* we do it directly, to avoid notifications */
5398 ranges_clear(infoPtr->selectionRanges);
5399 infoPtr->nSelectionMark = -1;
5400 infoPtr->nFocusedItem = -1;
5401 SetRectEmpty(&infoPtr->rcFocus);
5402 /* But we are supposed to leave nHotItem as is! */
5405 /* send LVN_DELETEALLITEMS notification */
5406 ZeroMemory(&nmlv, sizeof(NMLISTVIEW));
5408 bSuppress = notify_listview(infoPtr, LVN_DELETEALLITEMS, &nmlv);
5410 for (i = infoPtr->nItemCount - 1; i >= 0; i--)
5412 if (!(infoPtr->dwStyle & LVS_OWNERDATA))
5414 /* send LVN_DELETEITEM notification, if not suppressed
5415 and if it is not a virtual listview */
5416 if (!bSuppress) notify_deleteitem(infoPtr, i);
5417 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, i);
5418 lpItem = DPA_GetPtr(hdpaSubItems, 0);
5419 /* free id struct */
5420 j = DPA_GetPtrIndex(infoPtr->hdpaItemIds, lpItem->id);
5421 lpID = DPA_GetPtr(infoPtr->hdpaItemIds, j);
5422 DPA_DeletePtr(infoPtr->hdpaItemIds, j);
5424 /* both item and subitem start with ITEMHDR header */
5425 for (j = 0; j < DPA_GetPtrCount(hdpaSubItems); j++)
5427 hdrItem = DPA_GetPtr(hdpaSubItems, j);
5428 if (is_text(hdrItem->pszText)) Free(hdrItem->pszText);
5431 DPA_Destroy(hdpaSubItems);
5432 DPA_DeletePtr(infoPtr->hdpaItems, i);
5434 DPA_DeletePtr(infoPtr->hdpaPosX, i);
5435 DPA_DeletePtr(infoPtr->hdpaPosY, i);
5436 infoPtr->nItemCount --;
5441 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
5442 LISTVIEW_UpdateScroll(infoPtr);
5444 LISTVIEW_InvalidateList(infoPtr);
5451 * Scrolls, and updates the columns, when a column is changing width.
5454 * [I] infoPtr : valid pointer to the listview structure
5455 * [I] nColumn : column to scroll
5456 * [I] dx : amount of scroll, in pixels
5461 static void LISTVIEW_ScrollColumns(LISTVIEW_INFO *infoPtr, INT nColumn, INT dx)
5463 COLUMN_INFO *lpColumnInfo;
5469 if (nColumn < 0 || DPA_GetPtrCount(infoPtr->hdpaColumns) < 1) return;
5470 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, min(nColumn, DPA_GetPtrCount(infoPtr->hdpaColumns) - 1));
5471 rcCol = lpColumnInfo->rcHeader;
5472 if (nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns))
5473 rcCol.left = rcCol.right;
5475 /* adjust the other columns */
5476 hdi.mask = HDI_ORDER;
5477 if (SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nColumn, (LPARAM)&hdi))
5479 INT nOrder = hdi.iOrder;
5480 for (nCol = 0; nCol < DPA_GetPtrCount(infoPtr->hdpaColumns); nCol++)
5482 hdi.mask = HDI_ORDER;
5483 SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nCol, (LPARAM)&hdi);
5484 if (hdi.iOrder >= nOrder) {
5485 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, nCol);
5486 lpColumnInfo->rcHeader.left += dx;
5487 lpColumnInfo->rcHeader.right += dx;
5492 /* do not update screen if not in report mode */
5493 if (!is_redrawing(infoPtr) || infoPtr->uView != LV_VIEW_DETAILS) return;
5495 /* Need to reset the item width when inserting a new column */
5496 infoPtr->nItemWidth += dx;
5498 LISTVIEW_UpdateScroll(infoPtr);
5499 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
5501 /* scroll to cover the deleted column, and invalidate for redraw */
5502 rcOld = infoPtr->rcList;
5503 rcOld.left = ptOrigin.x + rcCol.left + dx;
5504 ScrollWindowEx(infoPtr->hwndSelf, dx, 0, &rcOld, &rcOld, 0, 0, SW_ERASE | SW_INVALIDATE);
5509 * Removes a column from the listview control.
5512 * [I] infoPtr : valid pointer to the listview structure
5513 * [I] nColumn : column index
5519 static BOOL LISTVIEW_DeleteColumn(LISTVIEW_INFO *infoPtr, INT nColumn)
5523 TRACE("nColumn=%d\n", nColumn);
5525 if (nColumn < 0 || DPA_GetPtrCount(infoPtr->hdpaColumns) == 0
5526 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
5528 /* While the MSDN specifically says that column zero should not be deleted,
5529 what actually happens is that the column itself is deleted but no items or subitems
5533 LISTVIEW_GetHeaderRect(infoPtr, nColumn, &rcCol);
5535 if (!SendMessageW(infoPtr->hwndHeader, HDM_DELETEITEM, nColumn, 0))
5538 Free(DPA_GetPtr(infoPtr->hdpaColumns, nColumn));
5539 DPA_DeletePtr(infoPtr->hdpaColumns, nColumn);
5541 if (!(infoPtr->dwStyle & LVS_OWNERDATA) && nColumn)
5543 SUBITEM_INFO *lpSubItem, *lpDelItem;
5545 INT nItem, nSubItem, i;
5547 for (nItem = 0; nItem < infoPtr->nItemCount; nItem++)
5549 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, nItem);
5552 for (i = 1; i < DPA_GetPtrCount(hdpaSubItems); i++)
5554 lpSubItem = DPA_GetPtr(hdpaSubItems, i);
5555 if (lpSubItem->iSubItem == nColumn)
5558 lpDelItem = lpSubItem;
5560 else if (lpSubItem->iSubItem > nColumn)
5562 lpSubItem->iSubItem--;
5566 /* if we found our subitem, zap it */
5570 if (is_text(lpDelItem->hdr.pszText))
5571 Free(lpDelItem->hdr.pszText);
5576 /* free dpa memory */
5577 DPA_DeletePtr(hdpaSubItems, nSubItem);
5582 /* update the other column info */
5583 LISTVIEW_UpdateItemSize(infoPtr);
5584 if(DPA_GetPtrCount(infoPtr->hdpaColumns) == 0)
5585 LISTVIEW_InvalidateList(infoPtr);
5587 LISTVIEW_ScrollColumns(infoPtr, nColumn, -(rcCol.right - rcCol.left));
5594 * Invalidates the listview after an item's insertion or deletion.
5597 * [I] infoPtr : valid pointer to the listview structure
5598 * [I] nItem : item index
5599 * [I] dir : -1 if deleting, 1 if inserting
5604 static void LISTVIEW_ScrollOnInsert(LISTVIEW_INFO *infoPtr, INT nItem, INT dir)
5606 INT nPerCol, nItemCol, nItemRow;
5610 /* if we don't refresh, what's the point of scrolling? */
5611 if (!is_redrawing(infoPtr)) return;
5613 assert (abs(dir) == 1);
5615 /* arrange icons if autoarrange is on */
5616 if (is_autoarrange(infoPtr))
5618 BOOL arrange = TRUE;
5619 if (dir < 0 && nItem >= infoPtr->nItemCount) arrange = FALSE;
5620 if (dir > 0 && nItem == infoPtr->nItemCount - 1) arrange = FALSE;
5621 if (arrange) LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
5624 /* scrollbars need updating */
5625 LISTVIEW_UpdateScroll(infoPtr);
5627 /* figure out the item's position */
5628 if (infoPtr->uView == LV_VIEW_DETAILS)
5629 nPerCol = infoPtr->nItemCount + 1;
5630 else if (infoPtr->uView == LV_VIEW_LIST)
5631 nPerCol = LISTVIEW_GetCountPerColumn(infoPtr);
5632 else /* LV_VIEW_ICON, or LV_VIEW_SMALLICON */
5635 nItemCol = nItem / nPerCol;
5636 nItemRow = nItem % nPerCol;
5637 LISTVIEW_GetOrigin(infoPtr, &Origin);
5639 /* move the items below up a slot */
5640 rcScroll.left = nItemCol * infoPtr->nItemWidth;
5641 rcScroll.top = nItemRow * infoPtr->nItemHeight;
5642 rcScroll.right = rcScroll.left + infoPtr->nItemWidth;
5643 rcScroll.bottom = nPerCol * infoPtr->nItemHeight;
5644 OffsetRect(&rcScroll, Origin.x, Origin.y);
5645 TRACE("rcScroll=%s, dx=%d\n", wine_dbgstr_rect(&rcScroll), dir * infoPtr->nItemHeight);
5646 if (IntersectRect(&rcScroll, &rcScroll, &infoPtr->rcList))
5648 TRACE("Invalidating rcScroll=%s, rcList=%s\n", wine_dbgstr_rect(&rcScroll), wine_dbgstr_rect(&infoPtr->rcList));
5649 InvalidateRect(infoPtr->hwndSelf, &rcScroll, TRUE);
5652 /* report has only that column, so we're done */
5653 if (infoPtr->uView == LV_VIEW_DETAILS) return;
5655 /* now for LISTs, we have to deal with the columns to the right */
5656 rcScroll.left = (nItemCol + 1) * infoPtr->nItemWidth;
5658 rcScroll.right = (infoPtr->nItemCount / nPerCol + 1) * infoPtr->nItemWidth;
5659 rcScroll.bottom = nPerCol * infoPtr->nItemHeight;
5660 OffsetRect(&rcScroll, Origin.x, Origin.y);
5661 if (IntersectRect(&rcScroll, &rcScroll, &infoPtr->rcList))
5662 InvalidateRect(infoPtr->hwndSelf, &rcScroll, TRUE);
5667 * Removes an item from the listview control.
5670 * [I] infoPtr : valid pointer to the listview structure
5671 * [I] nItem : item index
5677 static BOOL LISTVIEW_DeleteItem(LISTVIEW_INFO *infoPtr, INT nItem)
5680 const BOOL is_icon = (infoPtr->uView == LV_VIEW_SMALLICON || infoPtr->uView == LV_VIEW_ICON);
5681 INT focus = infoPtr->nFocusedItem;
5683 TRACE("(nItem=%d)\n", nItem);
5685 if (nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
5687 /* remove selection, and focus */
5689 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
5690 LISTVIEW_SetItemState(infoPtr, nItem, &item);
5692 /* send LVN_DELETEITEM notification. */
5693 if (!notify_deleteitem(infoPtr, nItem)) return FALSE;
5695 /* we need to do this here, because we'll be deleting stuff */
5697 LISTVIEW_InvalidateItem(infoPtr, nItem);
5699 if (!(infoPtr->dwStyle & LVS_OWNERDATA))
5707 hdpaSubItems = DPA_DeletePtr(infoPtr->hdpaItems, nItem);
5708 lpItem = DPA_GetPtr(hdpaSubItems, 0);
5710 /* free id struct */
5711 i = DPA_GetPtrIndex(infoPtr->hdpaItemIds, lpItem->id);
5712 lpID = DPA_GetPtr(infoPtr->hdpaItemIds, i);
5713 DPA_DeletePtr(infoPtr->hdpaItemIds, i);
5715 for (i = 0; i < DPA_GetPtrCount(hdpaSubItems); i++)
5717 hdrItem = DPA_GetPtr(hdpaSubItems, i);
5718 if (is_text(hdrItem->pszText)) Free(hdrItem->pszText);
5721 DPA_Destroy(hdpaSubItems);
5726 DPA_DeletePtr(infoPtr->hdpaPosX, nItem);
5727 DPA_DeletePtr(infoPtr->hdpaPosY, nItem);
5730 infoPtr->nItemCount--;
5731 LISTVIEW_ShiftIndices(infoPtr, nItem, -1);
5732 LISTVIEW_ShiftFocus(infoPtr, focus, nItem, -1);
5734 /* now is the invalidation fun */
5736 LISTVIEW_ScrollOnInsert(infoPtr, nItem, -1);
5743 * Callback implementation for editlabel control
5746 * [I] infoPtr : valid pointer to the listview structure
5747 * [I] storeText : store edit box text as item text
5748 * [I] isW : TRUE if psxText is Unicode, FALSE if it's ANSI
5754 static BOOL LISTVIEW_EndEditLabelT(LISTVIEW_INFO *infoPtr, BOOL storeText, BOOL isW)
5756 HWND hwndSelf = infoPtr->hwndSelf;
5757 WCHAR szDispText[DISP_TEXT_SIZE] = { 0 };
5758 NMLVDISPINFOW dispInfo;
5759 INT editedItem = infoPtr->nEditLabelItem;
5761 WCHAR *pszText = NULL;
5766 DWORD len = isW ? GetWindowTextLengthW(infoPtr->hwndEdit) : GetWindowTextLengthA(infoPtr->hwndEdit);
5770 if ((pszText = Alloc((len+1) * (isW ? sizeof(WCHAR) : sizeof(CHAR)))))
5772 if (isW) GetWindowTextW(infoPtr->hwndEdit, pszText, len+1);
5773 else GetWindowTextA(infoPtr->hwndEdit, (CHAR*)pszText, len+1);
5778 TRACE("(pszText=%s, isW=%d)\n", debugtext_t(pszText, isW), isW);
5780 ZeroMemory(&dispInfo, sizeof(dispInfo));
5781 dispInfo.item.mask = LVIF_PARAM | LVIF_STATE | LVIF_TEXT;
5782 dispInfo.item.iItem = editedItem;
5783 dispInfo.item.iSubItem = 0;
5784 dispInfo.item.stateMask = ~0;
5785 dispInfo.item.pszText = szDispText;
5786 dispInfo.item.cchTextMax = DISP_TEXT_SIZE;
5787 if (!LISTVIEW_GetItemT(infoPtr, &dispInfo.item, isW))
5794 same = (lstrcmpW(dispInfo.item.pszText, pszText) == 0);
5797 LPWSTR tmp = textdupTtoW(pszText, FALSE);
5798 same = (lstrcmpW(dispInfo.item.pszText, tmp) == 0);
5799 textfreeT(tmp, FALSE);
5802 /* add the text from the edit in */
5803 dispInfo.item.mask |= LVIF_TEXT;
5804 dispInfo.item.pszText = same ? NULL : pszText;
5805 dispInfo.item.cchTextMax = textlenT(dispInfo.item.pszText, isW);
5807 /* Do we need to update the Item Text */
5808 res = notify_dispinfoT(infoPtr, LVN_ENDLABELEDITW, &dispInfo, isW);
5810 infoPtr->nEditLabelItem = -1;
5811 infoPtr->hwndEdit = 0;
5813 if (!res) goto cleanup;
5815 if (!IsWindow(hwndSelf))
5820 if (!pszText) return TRUE;
5827 if (!(infoPtr->dwStyle & LVS_OWNERDATA))
5829 HDPA hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, editedItem);
5830 ITEM_INFO* lpItem = DPA_GetPtr(hdpaSubItems, 0);
5831 if (lpItem && lpItem->hdr.pszText == LPSTR_TEXTCALLBACKW)
5833 LISTVIEW_InvalidateItem(infoPtr, editedItem);
5839 ZeroMemory(&dispInfo, sizeof(dispInfo));
5840 dispInfo.item.mask = LVIF_TEXT;
5841 dispInfo.item.iItem = editedItem;
5842 dispInfo.item.iSubItem = 0;
5843 dispInfo.item.pszText = pszText;
5844 dispInfo.item.cchTextMax = textlenT(pszText, isW);
5845 res = LISTVIEW_SetItemT(infoPtr, &dispInfo.item, isW);
5855 * Subclassed edit control windproc function
5858 * [I] hwnd : the edit window handle
5859 * [I] uMsg : the message that is to be processed
5860 * [I] wParam : first message parameter
5861 * [I] lParam : second message parameter
5862 * [I] isW : TRUE if input is Unicode
5867 static LRESULT EditLblWndProcT(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL isW)
5869 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongPtrW(GetParent(hwnd), 0);
5872 TRACE("(hwnd=%p, uMsg=%x, wParam=%lx, lParam=%lx, isW=%d)\n",
5873 hwnd, uMsg, wParam, lParam, isW);
5878 return DLGC_WANTARROWS | DLGC_WANTALLKEYS;
5882 WNDPROC editProc = infoPtr->EditWndProc;
5883 infoPtr->EditWndProc = 0;
5884 SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (DWORD_PTR)editProc);
5885 return CallWindowProcT(editProc, hwnd, uMsg, wParam, lParam, isW);
5889 if (VK_ESCAPE == (INT)wParam)
5894 else if (VK_RETURN == (INT)wParam)
5898 return CallWindowProcT(infoPtr->EditWndProc, hwnd, uMsg, wParam, lParam, isW);
5902 if (infoPtr->hwndEdit)
5903 LISTVIEW_EndEditLabelT(infoPtr, save, isW);
5905 SendMessageW(hwnd, WM_CLOSE, 0, 0);
5911 * Subclassed edit control Unicode windproc function
5914 * [I] hwnd : the edit window handle
5915 * [I] uMsg : the message that is to be processed
5916 * [I] wParam : first message parameter
5917 * [I] lParam : second message parameter
5921 static LRESULT CALLBACK EditLblWndProcW(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
5923 return EditLblWndProcT(hwnd, uMsg, wParam, lParam, TRUE);
5928 * Subclassed edit control ANSI windproc function
5931 * [I] hwnd : the edit window handle
5932 * [I] uMsg : the message that is to be processed
5933 * [I] wParam : first message parameter
5934 * [I] lParam : second message parameter
5938 static LRESULT CALLBACK EditLblWndProcA(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
5940 return EditLblWndProcT(hwnd, uMsg, wParam, lParam, FALSE);
5945 * Creates a subclassed edit control
5948 * [I] infoPtr : valid pointer to the listview structure
5949 * [I] text : initial text for the edit
5950 * [I] style : the window style
5951 * [I] isW : TRUE if input is Unicode
5955 static HWND CreateEditLabelT(LISTVIEW_INFO *infoPtr, LPCWSTR text, BOOL isW)
5957 static const DWORD style = WS_CHILDWINDOW|WS_CLIPSIBLINGS|ES_LEFT|ES_AUTOHSCROLL|WS_BORDER|WS_VISIBLE;
5958 HINSTANCE hinst = (HINSTANCE)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_HINSTANCE);
5961 TRACE("(%p, text=%s, isW=%d)\n", infoPtr, debugtext_t(text, isW), isW);
5963 /* window will be resized and positioned after LVN_BEGINLABELEDIT */
5965 hedit = CreateWindowW(WC_EDITW, text, style, 0, 0, 0, 0, infoPtr->hwndSelf, 0, hinst, 0);
5967 hedit = CreateWindowA(WC_EDITA, (LPCSTR)text, style, 0, 0, 0, 0, infoPtr->hwndSelf, 0, hinst, 0);
5969 if (!hedit) return 0;
5971 infoPtr->EditWndProc = (WNDPROC)
5972 (isW ? SetWindowLongPtrW(hedit, GWLP_WNDPROC, (DWORD_PTR)EditLblWndProcW) :
5973 SetWindowLongPtrA(hedit, GWLP_WNDPROC, (DWORD_PTR)EditLblWndProcA) );
5975 SendMessageW(hedit, WM_SETFONT, (WPARAM)infoPtr->hFont, FALSE);
5976 SendMessageW(hedit, EM_SETLIMITTEXT, DISP_TEXT_SIZE-1, 0);
5983 * Begin in place editing of specified list view item
5986 * [I] infoPtr : valid pointer to the listview structure
5987 * [I] nItem : item index
5988 * [I] isW : TRUE if it's a Unicode req, FALSE if ASCII
5994 static HWND LISTVIEW_EditLabelT(LISTVIEW_INFO *infoPtr, INT nItem, BOOL isW)
5996 WCHAR disptextW[DISP_TEXT_SIZE] = { 0 };
5997 HWND hwndSelf = infoPtr->hwndSelf;
5998 NMLVDISPINFOW dispInfo;
5999 HFONT hOldFont = NULL;
6005 TRACE("(nItem=%d, isW=%d)\n", nItem, isW);
6007 if (~infoPtr->dwStyle & LVS_EDITLABELS) return 0;
6009 /* remove existing edit box */
6010 if (infoPtr->hwndEdit)
6012 SetFocus(infoPtr->hwndSelf);
6013 infoPtr->hwndEdit = 0;
6016 if (nItem < 0 || nItem >= infoPtr->nItemCount) return 0;
6018 infoPtr->nEditLabelItem = nItem;
6020 LISTVIEW_SetSelection(infoPtr, nItem);
6021 LISTVIEW_SetItemFocus(infoPtr, nItem);
6022 LISTVIEW_InvalidateItem(infoPtr, nItem);
6024 rect.left = LVIR_LABEL;
6025 if (!LISTVIEW_GetItemRect(infoPtr, nItem, &rect)) return 0;
6027 ZeroMemory(&dispInfo, sizeof(dispInfo));
6028 dispInfo.item.mask = LVIF_PARAM | LVIF_STATE | LVIF_TEXT;
6029 dispInfo.item.iItem = nItem;
6030 dispInfo.item.iSubItem = 0;
6031 dispInfo.item.stateMask = ~0;
6032 dispInfo.item.pszText = disptextW;
6033 dispInfo.item.cchTextMax = DISP_TEXT_SIZE;
6034 if (!LISTVIEW_GetItemT(infoPtr, &dispInfo.item, isW)) return 0;
6036 infoPtr->hwndEdit = CreateEditLabelT(infoPtr, dispInfo.item.pszText, isW);
6037 if (!infoPtr->hwndEdit) return 0;
6039 if (notify_dispinfoT(infoPtr, LVN_BEGINLABELEDITW, &dispInfo, isW))
6041 if (!IsWindow(hwndSelf))
6043 SendMessageW(infoPtr->hwndEdit, WM_CLOSE, 0, 0);
6044 infoPtr->hwndEdit = 0;
6048 TRACE("disp text=%s\n", debugtext_t(dispInfo.item.pszText, isW));
6050 /* position and display edit box */
6051 hdc = GetDC(infoPtr->hwndSelf);
6053 /* select the font to get appropriate metric dimensions */
6055 hOldFont = SelectObject(hdc, infoPtr->hFont);
6057 /* use real edit box content, it could be altered during LVN_BEGINLABELEDIT notification */
6058 GetWindowTextW(infoPtr->hwndEdit, disptextW, DISP_TEXT_SIZE);
6059 TRACE("edit box text=%s\n", debugstr_w(disptextW));
6061 /* get string length in pixels */
6062 GetTextExtentPoint32W(hdc, disptextW, lstrlenW(disptextW), &sz);
6064 /* add extra spacing for the next character */
6065 GetTextMetricsW(hdc, &tm);
6066 sz.cx += tm.tmMaxCharWidth * 2;
6069 SelectObject(hdc, hOldFont);
6071 ReleaseDC(infoPtr->hwndSelf, hdc);
6073 sz.cy = rect.bottom - rect.top + 2;
6076 TRACE("moving edit=(%d,%d)-(%d,%d)\n", rect.left, rect.top, sz.cx, sz.cy);
6077 MoveWindow(infoPtr->hwndEdit, rect.left, rect.top, sz.cx, sz.cy, FALSE);
6078 ShowWindow(infoPtr->hwndEdit, SW_NORMAL);
6079 SetFocus(infoPtr->hwndEdit);
6080 SendMessageW(infoPtr->hwndEdit, EM_SETSEL, 0, -1);
6081 return infoPtr->hwndEdit;
6087 * Ensures the specified item is visible, scrolling into view if necessary.
6090 * [I] infoPtr : valid pointer to the listview structure
6091 * [I] nItem : item index
6092 * [I] bPartial : partially or entirely visible
6098 static BOOL LISTVIEW_EnsureVisible(LISTVIEW_INFO *infoPtr, INT nItem, BOOL bPartial)
6100 INT nScrollPosHeight = 0;
6101 INT nScrollPosWidth = 0;
6102 INT nHorzAdjust = 0;
6103 INT nVertAdjust = 0;
6106 RECT rcItem, rcTemp;
6108 rcItem.left = LVIR_BOUNDS;
6109 if (!LISTVIEW_GetItemRect(infoPtr, nItem, &rcItem)) return FALSE;
6111 if (bPartial && IntersectRect(&rcTemp, &infoPtr->rcList, &rcItem)) return TRUE;
6113 if (rcItem.left < infoPtr->rcList.left || rcItem.right > infoPtr->rcList.right)
6115 /* scroll left/right, but in LV_VIEW_DETAILS mode */
6116 if (infoPtr->uView == LV_VIEW_LIST)
6117 nScrollPosWidth = infoPtr->nItemWidth;
6118 else if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
6119 nScrollPosWidth = 1;
6121 if (rcItem.left < infoPtr->rcList.left)
6124 if (infoPtr->uView != LV_VIEW_DETAILS) nHorzDiff = rcItem.left - infoPtr->rcList.left;
6129 if (infoPtr->uView != LV_VIEW_DETAILS) nHorzDiff = rcItem.right - infoPtr->rcList.right;
6133 if (rcItem.top < infoPtr->rcList.top || rcItem.bottom > infoPtr->rcList.bottom)
6135 /* scroll up/down, but not in LVS_LIST mode */
6136 if (infoPtr->uView == LV_VIEW_DETAILS)
6137 nScrollPosHeight = infoPtr->nItemHeight;
6138 else if ((infoPtr->uView == LV_VIEW_ICON) || (infoPtr->uView == LV_VIEW_SMALLICON))
6139 nScrollPosHeight = 1;
6141 if (rcItem.top < infoPtr->rcList.top)
6144 if (infoPtr->uView != LV_VIEW_LIST) nVertDiff = rcItem.top - infoPtr->rcList.top;
6149 if (infoPtr->uView != LV_VIEW_LIST) nVertDiff = rcItem.bottom - infoPtr->rcList.bottom;
6153 if (!nScrollPosWidth && !nScrollPosHeight) return TRUE;
6155 if (nScrollPosWidth)
6157 INT diff = nHorzDiff / nScrollPosWidth;
6158 if (nHorzDiff % nScrollPosWidth) diff += nHorzAdjust;
6159 LISTVIEW_HScroll(infoPtr, SB_INTERNAL, diff);
6162 if (nScrollPosHeight)
6164 INT diff = nVertDiff / nScrollPosHeight;
6165 if (nVertDiff % nScrollPosHeight) diff += nVertAdjust;
6166 LISTVIEW_VScroll(infoPtr, SB_INTERNAL, diff);
6174 * Searches for an item with specific characteristics.
6177 * [I] hwnd : window handle
6178 * [I] nStart : base item index
6179 * [I] lpFindInfo : item information to look for
6182 * SUCCESS : index of item
6185 static INT LISTVIEW_FindItemW(const LISTVIEW_INFO *infoPtr, INT nStart,
6186 const LVFINDINFOW *lpFindInfo)
6188 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
6189 BOOL bWrap = FALSE, bNearest = FALSE;
6190 INT nItem = nStart + 1, nLast = infoPtr->nItemCount, nNearestItem = -1;
6191 ULONG xdist, ydist, dist, mindist = 0x7fffffff;
6192 POINT Position, Destination;
6195 /* Search in virtual listviews should be done by application, not by
6196 listview control, so we just send LVN_ODFINDITEMW and return the result */
6197 if (infoPtr->dwStyle & LVS_OWNERDATA)
6201 nmlv.iStart = nStart;
6202 nmlv.lvfi = *lpFindInfo;
6203 return notify_hdr(infoPtr, LVN_ODFINDITEMW, (LPNMHDR)&nmlv.hdr);
6206 if (!lpFindInfo || nItem < 0) return -1;
6209 if (lpFindInfo->flags & (LVFI_STRING | LVFI_PARTIAL) ||
6210 lpFindInfo->flags & LVFI_SUBSTRING)
6212 lvItem.mask |= LVIF_TEXT;
6213 lvItem.pszText = szDispText;
6214 lvItem.cchTextMax = DISP_TEXT_SIZE;
6217 if (lpFindInfo->flags & LVFI_WRAP)
6220 if ((lpFindInfo->flags & LVFI_NEARESTXY) &&
6221 (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON))
6226 LISTVIEW_GetOrigin(infoPtr, &Origin);
6227 Destination.x = lpFindInfo->pt.x - Origin.x;
6228 Destination.y = lpFindInfo->pt.y - Origin.y;
6229 switch(lpFindInfo->vkDirection)
6231 case VK_DOWN: Destination.y += infoPtr->nItemHeight; break;
6232 case VK_UP: Destination.y -= infoPtr->nItemHeight; break;
6233 case VK_RIGHT: Destination.x += infoPtr->nItemWidth; break;
6234 case VK_LEFT: Destination.x -= infoPtr->nItemWidth; break;
6235 case VK_HOME: Destination.x = Destination.y = 0; break;
6236 case VK_NEXT: Destination.y += infoPtr->rcList.bottom - infoPtr->rcList.top; break;
6237 case VK_PRIOR: Destination.y -= infoPtr->rcList.bottom - infoPtr->rcList.top; break;
6239 LISTVIEW_GetAreaRect(infoPtr, &rcArea);
6240 Destination.x = rcArea.right;
6241 Destination.y = rcArea.bottom;
6243 default: ERR("Unknown vkDirection=%d\n", lpFindInfo->vkDirection);
6247 else Destination.x = Destination.y = 0;
6249 /* if LVFI_PARAM is specified, all other flags are ignored */
6250 if (lpFindInfo->flags & LVFI_PARAM)
6252 lvItem.mask |= LVIF_PARAM;
6254 lvItem.mask &= ~LVIF_TEXT;
6258 for (; nItem < nLast; nItem++)
6260 lvItem.iItem = nItem;
6261 lvItem.iSubItem = 0;
6262 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) continue;
6264 if (lvItem.mask & LVIF_PARAM)
6266 if (lpFindInfo->lParam == lvItem.lParam)
6272 if (lvItem.mask & LVIF_TEXT)
6274 if (lpFindInfo->flags & (LVFI_PARTIAL | LVFI_SUBSTRING))
6276 WCHAR *p = strstrW(lvItem.pszText, lpFindInfo->psz);
6277 if (!p || p != lvItem.pszText) continue;
6281 if (lstrcmpW(lvItem.pszText, lpFindInfo->psz) != 0) continue;
6285 if (!bNearest) return nItem;
6287 /* This is very inefficient. To do a good job here,
6288 * we need a sorted array of (x,y) item positions */
6289 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
6291 /* compute the distance^2 to the destination */
6292 xdist = Destination.x - Position.x;
6293 ydist = Destination.y - Position.y;
6294 dist = xdist * xdist + ydist * ydist;
6296 /* remember the distance, and item if it's closer */
6300 nNearestItem = nItem;
6307 nLast = min(nStart + 1, infoPtr->nItemCount);
6312 return nNearestItem;
6317 * Searches for an item with specific characteristics.
6320 * [I] hwnd : window handle
6321 * [I] nStart : base item index
6322 * [I] lpFindInfo : item information to look for
6325 * SUCCESS : index of item
6328 static INT LISTVIEW_FindItemA(const LISTVIEW_INFO *infoPtr, INT nStart,
6329 const LVFINDINFOA *lpFindInfo)
6331 BOOL hasText = lpFindInfo->flags & (LVFI_STRING | LVFI_PARTIAL) ||
6332 lpFindInfo->flags & LVFI_SUBSTRING;
6337 memcpy(&fiw, lpFindInfo, sizeof(fiw));
6338 if (hasText) fiw.psz = strW = textdupTtoW((LPCWSTR)lpFindInfo->psz, FALSE);
6339 res = LISTVIEW_FindItemW(infoPtr, nStart, &fiw);
6340 textfreeT(strW, FALSE);
6346 * Retrieves column attributes.
6349 * [I] infoPtr : valid pointer to the listview structure
6350 * [I] nColumn : column index
6351 * [IO] lpColumn : column information
6352 * [I] isW : if TRUE, then lpColumn is a LPLVCOLUMNW
6353 * otherwise it is in fact a LPLVCOLUMNA
6359 static BOOL LISTVIEW_GetColumnT(const LISTVIEW_INFO *infoPtr, INT nColumn, LPLVCOLUMNW lpColumn, BOOL isW)
6361 COLUMN_INFO *lpColumnInfo;
6364 if (!lpColumn || nColumn < 0 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
6365 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, nColumn);
6367 /* initialize memory */
6368 ZeroMemory(&hdi, sizeof(hdi));
6370 if (lpColumn->mask & LVCF_TEXT)
6372 hdi.mask |= HDI_TEXT;
6373 hdi.pszText = lpColumn->pszText;
6374 hdi.cchTextMax = lpColumn->cchTextMax;
6377 if (lpColumn->mask & LVCF_IMAGE)
6378 hdi.mask |= HDI_IMAGE;
6380 if (lpColumn->mask & LVCF_ORDER)
6381 hdi.mask |= HDI_ORDER;
6383 if (lpColumn->mask & LVCF_SUBITEM)
6384 hdi.mask |= HDI_LPARAM;
6386 if (!SendMessageW(infoPtr->hwndHeader, isW ? HDM_GETITEMW : HDM_GETITEMA, nColumn, (LPARAM)&hdi)) return FALSE;
6388 if (lpColumn->mask & LVCF_FMT)
6389 lpColumn->fmt = lpColumnInfo->fmt;
6391 if (lpColumn->mask & LVCF_WIDTH)
6392 lpColumn->cx = lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left;
6394 if (lpColumn->mask & LVCF_IMAGE)
6395 lpColumn->iImage = hdi.iImage;
6397 if (lpColumn->mask & LVCF_ORDER)
6398 lpColumn->iOrder = hdi.iOrder;
6400 if (lpColumn->mask & LVCF_SUBITEM)
6401 lpColumn->iSubItem = hdi.lParam;
6403 if (lpColumn->mask & LVCF_MINWIDTH)
6404 lpColumn->cxMin = lpColumnInfo->cxMin;
6410 static BOOL LISTVIEW_GetColumnOrderArray(const LISTVIEW_INFO *infoPtr, INT iCount, LPINT lpiArray)
6412 TRACE("iCount=%d, lpiArray=%p\n", iCount, lpiArray);
6417 return SendMessageW(infoPtr->hwndHeader, HDM_GETORDERARRAY, iCount, (LPARAM)lpiArray);
6422 * Retrieves the column width.
6425 * [I] infoPtr : valid pointer to the listview structure
6426 * [I] int : column index
6429 * SUCCESS : column width
6432 static INT LISTVIEW_GetColumnWidth(const LISTVIEW_INFO *infoPtr, INT nColumn)
6434 INT nColumnWidth = 0;
6437 TRACE("nColumn=%d\n", nColumn);
6439 /* we have a 'column' in LIST and REPORT mode only */
6440 switch(infoPtr->uView)
6443 nColumnWidth = infoPtr->nItemWidth;
6445 case LV_VIEW_DETAILS:
6446 /* We are not using LISTVIEW_GetHeaderRect as this data is updated only after a HDN_ITEMCHANGED.
6447 * There is an application that subclasses the listview, calls LVM_GETCOLUMNWIDTH in the
6448 * HDN_ITEMCHANGED handler and goes into infinite recursion if it receives old data.
6450 * TODO: should we do the same in LVM_GETCOLUMN?
6452 hdItem.mask = HDI_WIDTH;
6453 if (!SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nColumn, (LPARAM)&hdItem))
6455 WARN("(%p): HDM_GETITEMW failed for item %d\n", infoPtr->hwndSelf, nColumn);
6458 nColumnWidth = hdItem.cxy;
6462 TRACE("nColumnWidth=%d\n", nColumnWidth);
6463 return nColumnWidth;
6468 * In list or report display mode, retrieves the number of items that can fit
6469 * vertically in the visible area. In icon or small icon display mode,
6470 * retrieves the total number of visible items.
6473 * [I] infoPtr : valid pointer to the listview structure
6476 * Number of fully visible items.
6478 static INT LISTVIEW_GetCountPerPage(const LISTVIEW_INFO *infoPtr)
6480 switch (infoPtr->uView)
6483 case LV_VIEW_SMALLICON:
6484 return infoPtr->nItemCount;
6485 case LV_VIEW_DETAILS:
6486 return LISTVIEW_GetCountPerColumn(infoPtr);
6488 return LISTVIEW_GetCountPerRow(infoPtr) * LISTVIEW_GetCountPerColumn(infoPtr);
6496 * Retrieves an image list handle.
6499 * [I] infoPtr : valid pointer to the listview structure
6500 * [I] nImageList : image list identifier
6503 * SUCCESS : image list handle
6506 static HIMAGELIST LISTVIEW_GetImageList(const LISTVIEW_INFO *infoPtr, INT nImageList)
6510 case LVSIL_NORMAL: return infoPtr->himlNormal;
6511 case LVSIL_SMALL: return infoPtr->himlSmall;
6512 case LVSIL_STATE: return infoPtr->himlState;
6513 case LVSIL_GROUPHEADER:
6514 FIXME("LVSIL_GROUPHEADER not supported\n");
6517 WARN("got unknown imagelist index - %d\n", nImageList);
6522 /* LISTVIEW_GetISearchString */
6526 * Retrieves item attributes.
6529 * [I] hwnd : window handle
6530 * [IO] lpLVItem : item info
6531 * [I] isW : if TRUE, then lpLVItem is a LPLVITEMW,
6532 * if FALSE, then lpLVItem is a LPLVITEMA.
6535 * This is the internal 'GetItem' interface -- it tries to
6536 * be smart and avoid text copies, if possible, by modifying
6537 * lpLVItem->pszText to point to the text string. Please note
6538 * that this is not always possible (e.g. OWNERDATA), so on
6539 * entry you *must* supply valid values for pszText, and cchTextMax.
6540 * The only difference to the documented interface is that upon
6541 * return, you should use *only* the lpLVItem->pszText, rather than
6542 * the buffer pointer you provided on input. Most code already does
6543 * that, so it's not a problem.
6544 * For the two cases when the text must be copied (that is,
6545 * for LVM_GETITEM, and LVM_GETITEMTEXT), use LISTVIEW_GetItemExtT.
6551 static BOOL LISTVIEW_GetItemT(const LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem, BOOL isW)
6553 ITEMHDR callbackHdr = { LPSTR_TEXTCALLBACKW, I_IMAGECALLBACK };
6554 NMLVDISPINFOW dispInfo;
6560 TRACE("(item=%s, isW=%d)\n", debuglvitem_t(lpLVItem, isW), isW);
6562 if (!lpLVItem || lpLVItem->iItem < 0 || lpLVItem->iItem >= infoPtr->nItemCount)
6565 if (lpLVItem->mask == 0) return TRUE;
6566 TRACE("mask=%x\n", lpLVItem->mask);
6568 /* make a local copy */
6569 isubitem = lpLVItem->iSubItem;
6571 /* a quick optimization if all we're asked is the focus state
6572 * these queries are worth optimising since they are common,
6573 * and can be answered in constant time, without the heavy accesses */
6574 if ( (lpLVItem->mask == LVIF_STATE) && (lpLVItem->stateMask == LVIS_FOCUSED) &&
6575 !(infoPtr->uCallbackMask & LVIS_FOCUSED) )
6577 lpLVItem->state = 0;
6578 if (infoPtr->nFocusedItem == lpLVItem->iItem)
6579 lpLVItem->state |= LVIS_FOCUSED;
6583 ZeroMemory(&dispInfo, sizeof(dispInfo));
6585 /* if the app stores all the data, handle it separately */
6586 if (infoPtr->dwStyle & LVS_OWNERDATA)
6588 dispInfo.item.state = 0;
6590 /* apparently, we should not callback for lParam in LVS_OWNERDATA */
6591 if ((lpLVItem->mask & ~(LVIF_STATE | LVIF_PARAM)) ||
6592 ((lpLVItem->mask & LVIF_STATE) && (infoPtr->uCallbackMask & lpLVItem->stateMask)))
6594 UINT mask = lpLVItem->mask;
6596 /* NOTE: copy only fields which we _know_ are initialized, some apps
6597 * depend on the uninitialized fields being 0 */
6598 dispInfo.item.mask = lpLVItem->mask & ~LVIF_PARAM;
6599 dispInfo.item.iItem = lpLVItem->iItem;
6600 dispInfo.item.iSubItem = isubitem;
6601 if (lpLVItem->mask & LVIF_TEXT)
6603 if (lpLVItem->mask & LVIF_NORECOMPUTE)
6605 dispInfo.item.mask &= ~(LVIF_TEXT | LVIF_NORECOMPUTE);
6608 dispInfo.item.pszText = lpLVItem->pszText;
6609 dispInfo.item.cchTextMax = lpLVItem->cchTextMax;
6612 if (lpLVItem->mask & LVIF_STATE)
6613 dispInfo.item.stateMask = lpLVItem->stateMask & infoPtr->uCallbackMask;
6614 /* could be zeroed on LVIF_NORECOMPUTE case */
6615 if (dispInfo.item.mask)
6617 notify_dispinfoT(infoPtr, LVN_GETDISPINFOW, &dispInfo, isW);
6618 dispInfo.item.stateMask = lpLVItem->stateMask;
6619 if (lpLVItem->mask & (LVIF_GROUPID|LVIF_COLUMNS))
6621 /* full size structure expected - _WIN32IE >= 0x560 */
6622 *lpLVItem = dispInfo.item;
6624 else if (lpLVItem->mask & LVIF_INDENT)
6626 /* indent member expected - _WIN32IE >= 0x300 */
6627 memcpy(lpLVItem, &dispInfo.item, offsetof( LVITEMW, iGroupId ));
6631 /* minimal structure expected */
6632 memcpy(lpLVItem, &dispInfo.item, offsetof( LVITEMW, iIndent ));
6634 lpLVItem->mask = mask;
6635 TRACE(" getdispinfo(1):lpLVItem=%s\n", debuglvitem_t(lpLVItem, isW));
6639 /* make sure lParam is zeroed out */
6640 if (lpLVItem->mask & LVIF_PARAM) lpLVItem->lParam = 0;
6642 /* callback marked pointer required here */
6643 if ((lpLVItem->mask & LVIF_TEXT) && (lpLVItem->mask & LVIF_NORECOMPUTE))
6644 lpLVItem->pszText = LPSTR_TEXTCALLBACKW;
6646 /* we store only a little state, so if we're not asked, we're done */
6647 if (!(lpLVItem->mask & LVIF_STATE) || isubitem) return TRUE;
6649 /* if focus is handled by us, report it */
6650 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_FOCUSED )
6652 lpLVItem->state &= ~LVIS_FOCUSED;
6653 if (infoPtr->nFocusedItem == lpLVItem->iItem)
6654 lpLVItem->state |= LVIS_FOCUSED;
6657 /* and do the same for selection, if we handle it */
6658 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_SELECTED )
6660 lpLVItem->state &= ~LVIS_SELECTED;
6661 if (ranges_contain(infoPtr->selectionRanges, lpLVItem->iItem))
6662 lpLVItem->state |= LVIS_SELECTED;
6668 /* find the item and subitem structures before we proceed */
6669 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, lpLVItem->iItem);
6670 lpItem = DPA_GetPtr(hdpaSubItems, 0);
6675 SUBITEM_INFO *lpSubItem = LISTVIEW_GetSubItemPtr(hdpaSubItems, isubitem);
6676 pItemHdr = lpSubItem ? &lpSubItem->hdr : &callbackHdr;
6679 WARN(" iSubItem invalid (%08x), ignored.\n", isubitem);
6684 pItemHdr = &lpItem->hdr;
6686 /* Do we need to query the state from the app? */
6687 if ((lpLVItem->mask & LVIF_STATE) && infoPtr->uCallbackMask && isubitem == 0)
6689 dispInfo.item.mask |= LVIF_STATE;
6690 dispInfo.item.stateMask = infoPtr->uCallbackMask;
6693 /* Do we need to enquire about the image? */
6694 if ((lpLVItem->mask & LVIF_IMAGE) && pItemHdr->iImage == I_IMAGECALLBACK &&
6695 (isubitem == 0 || (infoPtr->dwLvExStyle & LVS_EX_SUBITEMIMAGES)))
6697 dispInfo.item.mask |= LVIF_IMAGE;
6698 dispInfo.item.iImage = I_IMAGECALLBACK;
6701 /* Only items support indentation */
6702 if ((lpLVItem->mask & LVIF_INDENT) && lpItem->iIndent == I_INDENTCALLBACK &&
6705 dispInfo.item.mask |= LVIF_INDENT;
6706 dispInfo.item.iIndent = I_INDENTCALLBACK;
6709 /* Apps depend on calling back for text if it is NULL or LPSTR_TEXTCALLBACKW */
6710 if ((lpLVItem->mask & LVIF_TEXT) && !(lpLVItem->mask & LVIF_NORECOMPUTE) &&
6711 !is_text(pItemHdr->pszText))
6713 dispInfo.item.mask |= LVIF_TEXT;
6714 dispInfo.item.pszText = lpLVItem->pszText;
6715 dispInfo.item.cchTextMax = lpLVItem->cchTextMax;
6716 if (dispInfo.item.pszText && dispInfo.item.cchTextMax > 0)
6717 *dispInfo.item.pszText = '\0';
6720 /* If we don't have all the requested info, query the application */
6721 if (dispInfo.item.mask)
6723 dispInfo.item.iItem = lpLVItem->iItem;
6724 dispInfo.item.iSubItem = lpLVItem->iSubItem; /* yes: the original subitem */
6725 dispInfo.item.lParam = lpItem->lParam;
6726 notify_dispinfoT(infoPtr, LVN_GETDISPINFOW, &dispInfo, isW);
6727 TRACE(" getdispinfo(2):item=%s\n", debuglvitem_t(&dispInfo.item, isW));
6730 /* we should not store values for subitems */
6731 if (isubitem) dispInfo.item.mask &= ~LVIF_DI_SETITEM;
6733 /* Now, handle the iImage field */
6734 if (dispInfo.item.mask & LVIF_IMAGE)
6736 lpLVItem->iImage = dispInfo.item.iImage;
6737 if ((dispInfo.item.mask & LVIF_DI_SETITEM) && pItemHdr->iImage == I_IMAGECALLBACK)
6738 pItemHdr->iImage = dispInfo.item.iImage;
6740 else if (lpLVItem->mask & LVIF_IMAGE)
6742 if(isubitem == 0 || (infoPtr->dwLvExStyle & LVS_EX_SUBITEMIMAGES))
6743 lpLVItem->iImage = pItemHdr->iImage;
6745 lpLVItem->iImage = 0;
6748 /* The pszText field */
6749 if (dispInfo.item.mask & LVIF_TEXT)
6751 if ((dispInfo.item.mask & LVIF_DI_SETITEM) && pItemHdr->pszText)
6752 textsetptrT(&pItemHdr->pszText, dispInfo.item.pszText, isW);
6754 lpLVItem->pszText = dispInfo.item.pszText;
6756 else if (lpLVItem->mask & LVIF_TEXT)
6758 /* if LVN_GETDISPINFO's disabled with LVIF_NORECOMPUTE return callback placeholder */
6759 if (isW || !is_text(pItemHdr->pszText)) lpLVItem->pszText = pItemHdr->pszText;
6760 else textcpynT(lpLVItem->pszText, isW, pItemHdr->pszText, TRUE, lpLVItem->cchTextMax);
6763 /* Next is the lParam field */
6764 if (dispInfo.item.mask & LVIF_PARAM)
6766 lpLVItem->lParam = dispInfo.item.lParam;
6767 if ((dispInfo.item.mask & LVIF_DI_SETITEM))
6768 lpItem->lParam = dispInfo.item.lParam;
6770 else if (lpLVItem->mask & LVIF_PARAM)
6771 lpLVItem->lParam = lpItem->lParam;
6773 /* if this is a subitem, we're done */
6774 if (isubitem) return TRUE;
6776 /* ... the state field (this one is different due to uCallbackmask) */
6777 if (lpLVItem->mask & LVIF_STATE)
6779 lpLVItem->state = lpItem->state & lpLVItem->stateMask;
6780 if (dispInfo.item.mask & LVIF_STATE)
6782 lpLVItem->state &= ~dispInfo.item.stateMask;
6783 lpLVItem->state |= (dispInfo.item.state & dispInfo.item.stateMask);
6785 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_FOCUSED )
6787 lpLVItem->state &= ~LVIS_FOCUSED;
6788 if (infoPtr->nFocusedItem == lpLVItem->iItem)
6789 lpLVItem->state |= LVIS_FOCUSED;
6791 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_SELECTED )
6793 lpLVItem->state &= ~LVIS_SELECTED;
6794 if (ranges_contain(infoPtr->selectionRanges, lpLVItem->iItem))
6795 lpLVItem->state |= LVIS_SELECTED;
6799 /* and last, but not least, the indent field */
6800 if (dispInfo.item.mask & LVIF_INDENT)
6802 lpLVItem->iIndent = dispInfo.item.iIndent;
6803 if ((dispInfo.item.mask & LVIF_DI_SETITEM) && lpItem->iIndent == I_INDENTCALLBACK)
6804 lpItem->iIndent = dispInfo.item.iIndent;
6806 else if (lpLVItem->mask & LVIF_INDENT)
6808 lpLVItem->iIndent = lpItem->iIndent;
6816 * Retrieves item attributes.
6819 * [I] hwnd : window handle
6820 * [IO] lpLVItem : item info
6821 * [I] isW : if TRUE, then lpLVItem is a LPLVITEMW,
6822 * if FALSE, then lpLVItem is a LPLVITEMA.
6825 * This is the external 'GetItem' interface -- it properly copies
6826 * the text in the provided buffer.
6832 static BOOL LISTVIEW_GetItemExtT(const LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem, BOOL isW)
6837 if (!lpLVItem || lpLVItem->iItem < 0 || lpLVItem->iItem >= infoPtr->nItemCount)
6840 pszText = lpLVItem->pszText;
6841 bResult = LISTVIEW_GetItemT(infoPtr, lpLVItem, isW);
6842 if (bResult && (lpLVItem->mask & LVIF_TEXT) && lpLVItem->pszText != pszText)
6844 if (lpLVItem->pszText != LPSTR_TEXTCALLBACKW)
6845 textcpynT(pszText, isW, lpLVItem->pszText, isW, lpLVItem->cchTextMax);
6847 pszText = LPSTR_TEXTCALLBACKW;
6849 lpLVItem->pszText = pszText;
6857 * Retrieves the position (upper-left) of the listview control item.
6858 * Note that for LVS_ICON style, the upper-left is that of the icon
6859 * and not the bounding box.
6862 * [I] infoPtr : valid pointer to the listview structure
6863 * [I] nItem : item index
6864 * [O] lpptPosition : coordinate information
6870 static BOOL LISTVIEW_GetItemPosition(const LISTVIEW_INFO *infoPtr, INT nItem, LPPOINT lpptPosition)
6874 TRACE("(nItem=%d, lpptPosition=%p)\n", nItem, lpptPosition);
6876 if (!lpptPosition || nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
6878 LISTVIEW_GetOrigin(infoPtr, &Origin);
6879 LISTVIEW_GetItemOrigin(infoPtr, nItem, lpptPosition);
6881 if (infoPtr->uView == LV_VIEW_ICON)
6883 lpptPosition->x += (infoPtr->nItemWidth - infoPtr->iconSize.cx) / 2;
6884 lpptPosition->y += ICON_TOP_PADDING;
6886 lpptPosition->x += Origin.x;
6887 lpptPosition->y += Origin.y;
6889 TRACE (" lpptPosition=%s\n", wine_dbgstr_point(lpptPosition));
6896 * Retrieves the bounding rectangle for a listview control item.
6899 * [I] infoPtr : valid pointer to the listview structure
6900 * [I] nItem : item index
6901 * [IO] lprc : bounding rectangle coordinates
6902 * lprc->left specifies the portion of the item for which the bounding
6903 * rectangle will be retrieved.
6905 * LVIR_BOUNDS Returns the bounding rectangle of the entire item,
6906 * including the icon and label.
6909 * * Experiment shows that native control returns:
6910 * * width = min (48, length of text line)
6911 * * .left = position.x - (width - iconsize.cx)/2
6912 * * .right = .left + width
6913 * * height = #lines of text * ntmHeight + icon height + 8
6914 * * .top = position.y - 2
6915 * * .bottom = .top + height
6916 * * separation between items .y = itemSpacing.cy - height
6917 * * .x = itemSpacing.cx - width
6918 * LVIR_ICON Returns the bounding rectangle of the icon or small icon.
6921 * * Experiment shows that native control returns:
6922 * * width = iconSize.cx + 16
6923 * * .left = position.x - (width - iconsize.cx)/2
6924 * * .right = .left + width
6925 * * height = iconSize.cy + 4
6926 * * .top = position.y - 2
6927 * * .bottom = .top + height
6928 * * separation between items .y = itemSpacing.cy - height
6929 * * .x = itemSpacing.cx - width
6930 * LVIR_LABEL Returns the bounding rectangle of the item text.
6933 * * Experiment shows that native control returns:
6934 * * width = text length
6935 * * .left = position.x - width/2
6936 * * .right = .left + width
6937 * * height = ntmH * linecount + 2
6938 * * .top = position.y + iconSize.cy + 6
6939 * * .bottom = .top + height
6940 * * separation between items .y = itemSpacing.cy - height
6941 * * .x = itemSpacing.cx - width
6942 * LVIR_SELECTBOUNDS Returns the union of the LVIR_ICON and LVIR_LABEL
6943 * rectangles, but excludes columns in report view.
6950 * Note that the bounding rectangle of the label in the LVS_ICON view depends
6951 * upon whether the window has the focus currently and on whether the item
6952 * is the one with the focus. Ensure that the control's record of which
6953 * item has the focus agrees with the items' records.
6955 static BOOL LISTVIEW_GetItemRect(const LISTVIEW_INFO *infoPtr, INT nItem, LPRECT lprc)
6957 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
6958 BOOL doLabel = TRUE, oversizedBox = FALSE;
6959 POINT Position, Origin;
6963 TRACE("(hwnd=%p, nItem=%d, lprc=%p)\n", infoPtr->hwndSelf, nItem, lprc);
6965 if (!lprc || nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
6967 LISTVIEW_GetOrigin(infoPtr, &Origin);
6968 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
6970 /* Be smart and try to figure out the minimum we have to do */
6971 if (lprc->left == LVIR_ICON) doLabel = FALSE;
6972 if (infoPtr->uView == LV_VIEW_DETAILS && lprc->left == LVIR_BOUNDS) doLabel = FALSE;
6973 if (infoPtr->uView == LV_VIEW_ICON && lprc->left != LVIR_ICON &&
6974 infoPtr->bFocus && LISTVIEW_GetItemState(infoPtr, nItem, LVIS_FOCUSED))
6975 oversizedBox = TRUE;
6977 /* get what we need from the item before hand, so we make
6978 * only one request. This can speed up things, if data
6979 * is stored on the app side */
6981 if (infoPtr->uView == LV_VIEW_DETAILS) lvItem.mask |= LVIF_INDENT;
6982 if (doLabel) lvItem.mask |= LVIF_TEXT;
6983 lvItem.iItem = nItem;
6984 lvItem.iSubItem = 0;
6985 lvItem.pszText = szDispText;
6986 lvItem.cchTextMax = DISP_TEXT_SIZE;
6987 if (lvItem.mask && !LISTVIEW_GetItemW(infoPtr, &lvItem)) return FALSE;
6988 /* we got the state already up, simulate it here, to avoid a reget */
6989 if (infoPtr->uView == LV_VIEW_ICON && (lprc->left != LVIR_ICON))
6991 lvItem.mask |= LVIF_STATE;
6992 lvItem.stateMask = LVIS_FOCUSED;
6993 lvItem.state = (oversizedBox ? LVIS_FOCUSED : 0);
6996 if (infoPtr->uView == LV_VIEW_DETAILS && (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) && lprc->left == LVIR_SELECTBOUNDS)
6997 lprc->left = LVIR_BOUNDS;
7003 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, NULL, lprc, NULL, NULL);
7007 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, NULL, NULL, NULL, lprc);
7011 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, lprc, NULL, NULL, NULL, NULL);
7014 case LVIR_SELECTBOUNDS:
7015 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, lprc, NULL, NULL, NULL);
7019 WARN("Unknown value: %d\n", lprc->left);
7023 if (infoPtr->uView == LV_VIEW_DETAILS)
7025 if (mode != LVIR_BOUNDS)
7026 OffsetRect(lprc, Origin.x + LISTVIEW_GetColumnInfo(infoPtr, 0)->rcHeader.left,
7027 Position.y + Origin.y);
7029 OffsetRect(lprc, Origin.x, Position.y + Origin.y);
7032 OffsetRect(lprc, Position.x + Origin.x, Position.y + Origin.y);
7034 TRACE(" rect=%s\n", wine_dbgstr_rect(lprc));
7041 * Retrieves the spacing between listview control items.
7044 * [I] infoPtr : valid pointer to the listview structure
7045 * [IO] lprc : rectangle to receive the output
7046 * on input, lprc->top = nSubItem
7047 * lprc->left = LVIR_ICON | LVIR_BOUNDS | LVIR_LABEL
7049 * NOTE: for subItem = 0, we should return the bounds of the _entire_ item,
7050 * not only those of the first column.
7056 static BOOL LISTVIEW_GetSubItemRect(const LISTVIEW_INFO *infoPtr, INT item, LPRECT lprc)
7058 RECT rect = { 0, 0, 0, 0 };
7062 if (!lprc) return FALSE;
7064 TRACE("(item=%d, subitem=%d, type=%d)\n", item, lprc->top, lprc->left);
7065 /* Subitem of '0' means item itself, and this works for all control view modes */
7067 return LISTVIEW_GetItemRect(infoPtr, item, lprc);
7069 if (infoPtr->uView != LV_VIEW_DETAILS) return FALSE;
7071 LISTVIEW_GetOrigin(infoPtr, &origin);
7072 /* this works for any item index, no matter if it exists or not */
7073 y = item * infoPtr->nItemHeight + origin.y;
7075 if (infoPtr->hwndHeader && SendMessageW(infoPtr->hwndHeader, HDM_GETITEMRECT, lprc->top, (LPARAM)&rect))
7078 rect.bottom = infoPtr->nItemHeight;
7082 /* Native implementation is broken for this case and garbage is left for left and right fields,
7083 we zero them to get predictable output */
7084 lprc->left = lprc->right = lprc->top = 0;
7085 lprc->bottom = infoPtr->nItemHeight;
7086 OffsetRect(lprc, origin.x, y);
7087 TRACE("return rect %s\n", wine_dbgstr_rect(lprc));
7095 /* it doesn't matter if main item actually has an icon, if imagelist is set icon width is returned */
7096 if (infoPtr->himlSmall)
7097 rect.right = rect.left + infoPtr->iconSize.cx;
7099 rect.right = rect.left;
7101 rect.bottom = rect.top + infoPtr->iconSize.cy;
7109 ERR("Unknown bounds=%d\n", lprc->left);
7113 OffsetRect(&rect, origin.x, y);
7115 TRACE("return rect %s\n", wine_dbgstr_rect(lprc));
7122 * Retrieves the spacing between listview control items.
7125 * [I] infoPtr : valid pointer to the listview structure
7126 * [I] bSmall : flag for small or large icon
7129 * Horizontal + vertical spacing
7131 static LONG LISTVIEW_GetItemSpacing(const LISTVIEW_INFO *infoPtr, BOOL bSmall)
7137 lResult = MAKELONG(infoPtr->iconSpacing.cx, infoPtr->iconSpacing.cy);
7141 if (infoPtr->uView == LV_VIEW_ICON)
7142 lResult = MAKELONG(DEFAULT_COLUMN_WIDTH, GetSystemMetrics(SM_CXSMICON)+HEIGHT_PADDING);
7144 lResult = MAKELONG(infoPtr->nItemWidth, infoPtr->nItemHeight);
7151 * Retrieves the state of a listview control item.
7154 * [I] infoPtr : valid pointer to the listview structure
7155 * [I] nItem : item index
7156 * [I] uMask : state mask
7159 * State specified by the mask.
7161 static UINT LISTVIEW_GetItemState(const LISTVIEW_INFO *infoPtr, INT nItem, UINT uMask)
7165 if (nItem < 0 || nItem >= infoPtr->nItemCount) return 0;
7167 lvItem.iItem = nItem;
7168 lvItem.iSubItem = 0;
7169 lvItem.mask = LVIF_STATE;
7170 lvItem.stateMask = uMask;
7171 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return 0;
7173 return lvItem.state & uMask;
7178 * Retrieves the text of a listview control item or subitem.
7181 * [I] hwnd : window handle
7182 * [I] nItem : item index
7183 * [IO] lpLVItem : item information
7184 * [I] isW : TRUE if lpLVItem is Unicode
7187 * SUCCESS : string length
7190 static INT LISTVIEW_GetItemTextT(const LISTVIEW_INFO *infoPtr, INT nItem, LPLVITEMW lpLVItem, BOOL isW)
7192 if (!lpLVItem || nItem < 0 || nItem >= infoPtr->nItemCount) return 0;
7194 lpLVItem->mask = LVIF_TEXT;
7195 lpLVItem->iItem = nItem;
7196 if (!LISTVIEW_GetItemExtT(infoPtr, lpLVItem, isW)) return 0;
7198 return textlenT(lpLVItem->pszText, isW);
7203 * Searches for an item based on properties + relationships.
7206 * [I] infoPtr : valid pointer to the listview structure
7207 * [I] nItem : item index
7208 * [I] uFlags : relationship flag
7211 * SUCCESS : item index
7214 static INT LISTVIEW_GetNextItem(const LISTVIEW_INFO *infoPtr, INT nItem, UINT uFlags)
7217 LVFINDINFOW lvFindInfo;
7218 INT nCountPerColumn;
7222 TRACE("nItem=%d, uFlags=%x, nItemCount=%d\n", nItem, uFlags, infoPtr->nItemCount);
7223 if (nItem < -1 || nItem >= infoPtr->nItemCount) return -1;
7225 ZeroMemory(&lvFindInfo, sizeof(lvFindInfo));
7227 if (uFlags & LVNI_CUT)
7230 if (uFlags & LVNI_DROPHILITED)
7231 uMask |= LVIS_DROPHILITED;
7233 if (uFlags & LVNI_FOCUSED)
7234 uMask |= LVIS_FOCUSED;
7236 if (uFlags & LVNI_SELECTED)
7237 uMask |= LVIS_SELECTED;
7239 /* if we're asked for the focused item, that's only one,
7240 * so it's worth optimizing */
7241 if (uFlags & LVNI_FOCUSED)
7243 if ((LISTVIEW_GetItemState(infoPtr, infoPtr->nFocusedItem, uMask) & uMask) != uMask) return -1;
7244 return (infoPtr->nFocusedItem == nItem) ? -1 : infoPtr->nFocusedItem;
7247 if (uFlags & LVNI_ABOVE)
7249 if ((infoPtr->uView == LV_VIEW_LIST) || (infoPtr->uView == LV_VIEW_DETAILS))
7254 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7260 /* Special case for autoarrange - move 'til the top of a list */
7261 if (is_autoarrange(infoPtr))
7263 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
7264 while (nItem - nCountPerRow >= 0)
7266 nItem -= nCountPerRow;
7267 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7272 lvFindInfo.flags = LVFI_NEARESTXY;
7273 lvFindInfo.vkDirection = VK_UP;
7274 LISTVIEW_GetItemPosition(infoPtr, nItem, &lvFindInfo.pt);
7275 while ((nItem = LISTVIEW_FindItemW(infoPtr, nItem, &lvFindInfo)) != -1)
7277 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7282 else if (uFlags & LVNI_BELOW)
7284 if ((infoPtr->uView == LV_VIEW_LIST) || (infoPtr->uView == LV_VIEW_DETAILS))
7286 while (nItem < infoPtr->nItemCount)
7289 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7295 /* Special case for autoarrange - move 'til the bottom of a list */
7296 if (is_autoarrange(infoPtr))
7298 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
7299 while (nItem + nCountPerRow < infoPtr->nItemCount )
7301 nItem += nCountPerRow;
7302 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7307 lvFindInfo.flags = LVFI_NEARESTXY;
7308 lvFindInfo.vkDirection = VK_DOWN;
7309 LISTVIEW_GetItemPosition(infoPtr, nItem, &lvFindInfo.pt);
7310 while ((nItem = LISTVIEW_FindItemW(infoPtr, nItem, &lvFindInfo)) != -1)
7312 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7317 else if (uFlags & LVNI_TOLEFT)
7319 if (infoPtr->uView == LV_VIEW_LIST)
7321 nCountPerColumn = LISTVIEW_GetCountPerColumn(infoPtr);
7322 while (nItem - nCountPerColumn >= 0)
7324 nItem -= nCountPerColumn;
7325 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7329 else if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
7331 /* Special case for autoarrange - move 'til the beginning of a row */
7332 if (is_autoarrange(infoPtr))
7334 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
7335 while (nItem % nCountPerRow > 0)
7338 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7343 lvFindInfo.flags = LVFI_NEARESTXY;
7344 lvFindInfo.vkDirection = VK_LEFT;
7345 LISTVIEW_GetItemPosition(infoPtr, nItem, &lvFindInfo.pt);
7346 while ((nItem = LISTVIEW_FindItemW(infoPtr, nItem, &lvFindInfo)) != -1)
7348 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7353 else if (uFlags & LVNI_TORIGHT)
7355 if (infoPtr->uView == LV_VIEW_LIST)
7357 nCountPerColumn = LISTVIEW_GetCountPerColumn(infoPtr);
7358 while (nItem + nCountPerColumn < infoPtr->nItemCount)
7360 nItem += nCountPerColumn;
7361 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7365 else if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
7367 /* Special case for autoarrange - move 'til the end of a row */
7368 if (is_autoarrange(infoPtr))
7370 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
7371 while (nItem % nCountPerRow < nCountPerRow - 1 )
7374 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7379 lvFindInfo.flags = LVFI_NEARESTXY;
7380 lvFindInfo.vkDirection = VK_RIGHT;
7381 LISTVIEW_GetItemPosition(infoPtr, nItem, &lvFindInfo.pt);
7382 while ((nItem = LISTVIEW_FindItemW(infoPtr, nItem, &lvFindInfo)) != -1)
7384 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7393 /* search by index */
7394 for (i = nItem; i < infoPtr->nItemCount; i++)
7396 if ((LISTVIEW_GetItemState(infoPtr, i, uMask) & uMask) == uMask)
7404 /* LISTVIEW_GetNumberOfWorkAreas */
7408 * Retrieves the origin coordinates when in icon or small icon display mode.
7411 * [I] infoPtr : valid pointer to the listview structure
7412 * [O] lpptOrigin : coordinate information
7417 static void LISTVIEW_GetOrigin(const LISTVIEW_INFO *infoPtr, LPPOINT lpptOrigin)
7419 INT nHorzPos = 0, nVertPos = 0;
7420 SCROLLINFO scrollInfo;
7422 scrollInfo.cbSize = sizeof(SCROLLINFO);
7423 scrollInfo.fMask = SIF_POS;
7425 if (GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo))
7426 nHorzPos = scrollInfo.nPos;
7427 if (GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
7428 nVertPos = scrollInfo.nPos;
7430 TRACE("nHorzPos=%d, nVertPos=%d\n", nHorzPos, nVertPos);
7432 lpptOrigin->x = infoPtr->rcList.left;
7433 lpptOrigin->y = infoPtr->rcList.top;
7434 if (infoPtr->uView == LV_VIEW_LIST)
7435 nHorzPos *= infoPtr->nItemWidth;
7436 else if (infoPtr->uView == LV_VIEW_DETAILS)
7437 nVertPos *= infoPtr->nItemHeight;
7439 lpptOrigin->x -= nHorzPos;
7440 lpptOrigin->y -= nVertPos;
7442 TRACE(" origin=%s\n", wine_dbgstr_point(lpptOrigin));
7447 * Retrieves the width of a string.
7450 * [I] hwnd : window handle
7451 * [I] lpszText : text string to process
7452 * [I] isW : TRUE if lpszText is Unicode, FALSE otherwise
7455 * SUCCESS : string width (in pixels)
7458 static INT LISTVIEW_GetStringWidthT(const LISTVIEW_INFO *infoPtr, LPCWSTR lpszText, BOOL isW)
7463 if (is_text(lpszText))
7465 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
7466 HDC hdc = GetDC(infoPtr->hwndSelf);
7467 HFONT hOldFont = SelectObject(hdc, hFont);
7470 GetTextExtentPointW(hdc, lpszText, lstrlenW(lpszText), &stringSize);
7472 GetTextExtentPointA(hdc, (LPCSTR)lpszText, lstrlenA((LPCSTR)lpszText), &stringSize);
7473 SelectObject(hdc, hOldFont);
7474 ReleaseDC(infoPtr->hwndSelf, hdc);
7476 return stringSize.cx;
7481 * Determines which listview item is located at the specified position.
7484 * [I] infoPtr : valid pointer to the listview structure
7485 * [IO] lpht : hit test information
7486 * [I] subitem : fill out iSubItem.
7487 * [I] select : return the index only if the hit selects the item
7490 * (mm 20001022): We must not allow iSubItem to be touched, for
7491 * an app might pass only a structure with space up to iItem!
7492 * (MS Office 97 does that for instance in the file open dialog)
7495 * SUCCESS : item index
7498 static INT LISTVIEW_HitTest(const LISTVIEW_INFO *infoPtr, LPLVHITTESTINFO lpht, BOOL subitem, BOOL select)
7500 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
7501 RECT rcBox, rcBounds, rcState, rcIcon, rcLabel, rcSearch;
7502 POINT Origin, Position, opt;
7507 TRACE("(pt=%s, subitem=%d, select=%d)\n", wine_dbgstr_point(&lpht->pt), subitem, select);
7511 if (subitem) lpht->iSubItem = 0;
7513 LISTVIEW_GetOrigin(infoPtr, &Origin);
7515 /* set whole list relation flags */
7516 if (subitem && infoPtr->uView == LV_VIEW_DETAILS)
7518 /* LVM_SUBITEMHITTEST checks left bound of possible client area */
7519 if (infoPtr->rcList.left > lpht->pt.x && Origin.x < lpht->pt.x)
7520 lpht->flags |= LVHT_TOLEFT;
7522 if (lpht->pt.y < infoPtr->rcList.top && lpht->pt.y >= 0)
7523 opt.y = lpht->pt.y + infoPtr->rcList.top;
7527 if (infoPtr->rcList.bottom < opt.y)
7528 lpht->flags |= LVHT_BELOW;
7532 if (infoPtr->rcList.left > lpht->pt.x)
7533 lpht->flags |= LVHT_TOLEFT;
7534 else if (infoPtr->rcList.right < lpht->pt.x)
7535 lpht->flags |= LVHT_TORIGHT;
7537 if (infoPtr->rcList.top > lpht->pt.y)
7538 lpht->flags |= LVHT_ABOVE;
7539 else if (infoPtr->rcList.bottom < lpht->pt.y)
7540 lpht->flags |= LVHT_BELOW;
7543 /* even if item is invalid try to find subitem */
7544 if (infoPtr->uView == LV_VIEW_DETAILS && subitem)
7549 opt.x = lpht->pt.x - Origin.x;
7551 lpht->iSubItem = -1;
7552 for (j = 0; j < DPA_GetPtrCount(infoPtr->hdpaColumns); j++)
7554 pRect = &LISTVIEW_GetColumnInfo(infoPtr, j)->rcHeader;
7556 if ((opt.x >= pRect->left) && (opt.x < pRect->right))
7562 TRACE("lpht->iSubItem=%d\n", lpht->iSubItem);
7564 /* if we're outside horizontal columns bounds there's nothing to test further */
7565 if (lpht->iSubItem == -1)
7568 lpht->flags = LVHT_NOWHERE;
7573 TRACE("lpht->flags=0x%x\n", lpht->flags);
7574 if (lpht->flags) return -1;
7576 lpht->flags |= LVHT_NOWHERE;
7578 /* first deal with the large items */
7579 rcSearch.left = lpht->pt.x;
7580 rcSearch.top = lpht->pt.y;
7581 rcSearch.right = rcSearch.left + 1;
7582 rcSearch.bottom = rcSearch.top + 1;
7584 iterator_frameditems(&i, infoPtr, &rcSearch);
7585 iterator_next(&i); /* go to first item in the sequence */
7587 iterator_destroy(&i);
7589 TRACE("lpht->iItem=%d\n", iItem);
7590 if (iItem == -1) return -1;
7592 lvItem.mask = LVIF_STATE | LVIF_TEXT;
7593 if (infoPtr->uView == LV_VIEW_DETAILS) lvItem.mask |= LVIF_INDENT;
7594 lvItem.stateMask = LVIS_STATEIMAGEMASK;
7595 if (infoPtr->uView == LV_VIEW_ICON) lvItem.stateMask |= LVIS_FOCUSED;
7596 lvItem.iItem = iItem;
7597 lvItem.iSubItem = subitem ? lpht->iSubItem : 0;
7598 lvItem.pszText = szDispText;
7599 lvItem.cchTextMax = DISP_TEXT_SIZE;
7600 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return -1;
7601 if (!infoPtr->bFocus) lvItem.state &= ~LVIS_FOCUSED;
7603 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, &rcBox, NULL, &rcIcon, &rcState, &rcLabel);
7604 LISTVIEW_GetItemOrigin(infoPtr, iItem, &Position);
7605 opt.x = lpht->pt.x - Position.x - Origin.x;
7607 if (lpht->pt.y < infoPtr->rcList.top && lpht->pt.y >= 0)
7608 opt.y = lpht->pt.y - Position.y - Origin.y + infoPtr->rcList.top;
7610 opt.y = lpht->pt.y - Position.y - Origin.y;
7612 if (infoPtr->uView == LV_VIEW_DETAILS)
7615 if (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT)
7616 opt.x = lpht->pt.x - Origin.x;
7620 UnionRect(&rcBounds, &rcIcon, &rcLabel);
7621 UnionRect(&rcBounds, &rcBounds, &rcState);
7623 TRACE("rcBounds=%s\n", wine_dbgstr_rect(&rcBounds));
7624 if (!PtInRect(&rcBounds, opt)) return -1;
7626 if (PtInRect(&rcIcon, opt))
7627 lpht->flags |= LVHT_ONITEMICON;
7628 else if (PtInRect(&rcLabel, opt))
7629 lpht->flags |= LVHT_ONITEMLABEL;
7630 else if (infoPtr->himlState && PtInRect(&rcState, opt))
7631 lpht->flags |= LVHT_ONITEMSTATEICON;
7632 /* special case for LVS_EX_FULLROWSELECT */
7633 if (infoPtr->uView == LV_VIEW_DETAILS && infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT &&
7634 !(lpht->flags & LVHT_ONITEM))
7636 lpht->flags = LVHT_ONITEM | LVHT_ABOVE;
7638 if (lpht->flags & LVHT_ONITEM)
7639 lpht->flags &= ~LVHT_NOWHERE;
7640 TRACE("lpht->flags=0x%x\n", lpht->flags);
7642 if (select && !(infoPtr->uView == LV_VIEW_DETAILS &&
7643 ((infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) ||
7644 (infoPtr->dwStyle & LVS_OWNERDRAWFIXED))))
7646 if (infoPtr->uView == LV_VIEW_DETAILS)
7648 /* get main item bounds */
7649 lvItem.iSubItem = 0;
7650 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, &rcBox, NULL, &rcIcon, &rcState, &rcLabel);
7651 UnionRect(&rcBounds, &rcIcon, &rcLabel);
7652 UnionRect(&rcBounds, &rcBounds, &rcState);
7654 if (!PtInRect(&rcBounds, opt)) iItem = -1;
7656 return lpht->iItem = iItem;
7661 * Inserts a new item in the listview control.
7664 * [I] infoPtr : valid pointer to the listview structure
7665 * [I] lpLVItem : item information
7666 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
7669 * SUCCESS : new item index
7672 static INT LISTVIEW_InsertItemT(LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem, BOOL isW)
7679 BOOL is_sorted, has_changed;
7681 HWND hwndSelf = infoPtr->hwndSelf;
7683 TRACE("(lpLVItem=%s, isW=%d)\n", debuglvitem_t(lpLVItem, isW), isW);
7685 if (infoPtr->dwStyle & LVS_OWNERDATA) return infoPtr->nItemCount++;
7687 /* make sure it's an item, and not a subitem; cannot insert a subitem */
7688 if (!lpLVItem || lpLVItem->iSubItem) return -1;
7690 if (!is_assignable_item(lpLVItem, infoPtr->dwStyle)) return -1;
7692 if (!(lpItem = Alloc(sizeof(ITEM_INFO)))) return -1;
7694 /* insert item in listview control data structure */
7695 if ( !(hdpaSubItems = DPA_Create(8)) ) goto fail;
7696 if ( !DPA_SetPtr(hdpaSubItems, 0, lpItem) ) assert (FALSE);
7698 /* link with id struct */
7699 if (!(lpID = Alloc(sizeof(ITEM_ID)))) goto fail;
7701 lpID->item = hdpaSubItems;
7702 lpID->id = get_next_itemid(infoPtr);
7703 if ( DPA_InsertPtr(infoPtr->hdpaItemIds, infoPtr->nItemCount, lpID) == -1) goto fail;
7705 is_sorted = (infoPtr->dwStyle & (LVS_SORTASCENDING | LVS_SORTDESCENDING)) &&
7706 !(infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (LPSTR_TEXTCALLBACKW != lpLVItem->pszText);
7708 if (lpLVItem->iItem < 0 && !is_sorted) return -1;
7710 /* calculate new item index */
7717 while (i < infoPtr->nItemCount)
7719 hItem = DPA_GetPtr( infoPtr->hdpaItems, i);
7720 item_s = DPA_GetPtr(hItem, 0);
7722 cmpv = textcmpWT(item_s->hdr.pszText, lpLVItem->pszText, isW);
7723 if (infoPtr->dwStyle & LVS_SORTDESCENDING) cmpv *= -1;
7725 if (cmpv >= 0) break;
7731 nItem = min(lpLVItem->iItem, infoPtr->nItemCount);
7733 TRACE(" inserting at %d, sorted=%d, count=%d, iItem=%d\n", nItem, is_sorted, infoPtr->nItemCount, lpLVItem->iItem);
7734 nItem = DPA_InsertPtr( infoPtr->hdpaItems, nItem, hdpaSubItems );
7735 if (nItem == -1) goto fail;
7736 infoPtr->nItemCount++;
7738 /* shift indices first so they don't get tangled */
7739 LISTVIEW_ShiftIndices(infoPtr, nItem, 1);
7740 LISTVIEW_ShiftFocus(infoPtr, infoPtr->nFocusedItem, nItem, 1);
7742 /* set the item attributes */
7743 if (lpLVItem->mask & (LVIF_GROUPID|LVIF_COLUMNS))
7745 /* full size structure expected - _WIN32IE >= 0x560 */
7748 else if (lpLVItem->mask & LVIF_INDENT)
7750 /* indent member expected - _WIN32IE >= 0x300 */
7751 memcpy(&item, lpLVItem, offsetof( LVITEMW, iGroupId ));
7755 /* minimal structure expected */
7756 memcpy(&item, lpLVItem, offsetof( LVITEMW, iIndent ));
7759 if (infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES)
7761 item.mask |= LVIF_STATE;
7762 item.stateMask |= LVIS_STATEIMAGEMASK;
7763 item.state &= ~LVIS_STATEIMAGEMASK;
7764 item.state |= INDEXTOSTATEIMAGEMASK(1);
7766 if (!set_main_item(infoPtr, &item, TRUE, isW, &has_changed)) goto undo;
7768 /* make room for the position, if we are in the right mode */
7769 if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
7771 if (DPA_InsertPtr(infoPtr->hdpaPosX, nItem, 0) == -1)
7773 if (DPA_InsertPtr(infoPtr->hdpaPosY, nItem, 0) == -1)
7775 DPA_DeletePtr(infoPtr->hdpaPosX, nItem);
7780 /* send LVN_INSERTITEM notification */
7781 ZeroMemory(&nmlv, sizeof(NMLISTVIEW));
7783 nmlv.lParam = lpItem->lParam;
7784 notify_listview(infoPtr, LVN_INSERTITEM, &nmlv);
7785 if (!IsWindow(hwndSelf))
7788 /* align items (set position of each item) */
7789 if (infoPtr->uView == LV_VIEW_SMALLICON || infoPtr->uView == LV_VIEW_ICON)
7793 if (infoPtr->dwStyle & LVS_ALIGNLEFT)
7794 LISTVIEW_NextIconPosLeft(infoPtr, &pt);
7796 LISTVIEW_NextIconPosTop(infoPtr, &pt);
7798 LISTVIEW_MoveIconTo(infoPtr, nItem, &pt, TRUE);
7801 /* now is the invalidation fun */
7802 LISTVIEW_ScrollOnInsert(infoPtr, nItem, 1);
7806 LISTVIEW_ShiftIndices(infoPtr, nItem, -1);
7807 LISTVIEW_ShiftFocus(infoPtr, infoPtr->nFocusedItem, nItem, -1);
7808 DPA_DeletePtr(infoPtr->hdpaItems, nItem);
7809 infoPtr->nItemCount--;
7811 DPA_DeletePtr(hdpaSubItems, 0);
7812 DPA_Destroy (hdpaSubItems);
7819 * Checks item visibility.
7822 * [I] infoPtr : valid pointer to the listview structure
7823 * [I] nFirst : item index to check for
7826 * Item visible : TRUE
7827 * Item invisible or failure : FALSE
7829 static BOOL LISTVIEW_IsItemVisible(const LISTVIEW_INFO *infoPtr, INT nItem)
7831 POINT Origin, Position;
7836 TRACE("nItem=%d\n", nItem);
7838 if (nItem < 0 || nItem >= DPA_GetPtrCount(infoPtr->hdpaItems)) return FALSE;
7840 LISTVIEW_GetOrigin(infoPtr, &Origin);
7841 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
7842 rcItem.left = Position.x + Origin.x;
7843 rcItem.top = Position.y + Origin.y;
7844 rcItem.right = rcItem.left + infoPtr->nItemWidth;
7845 rcItem.bottom = rcItem.top + infoPtr->nItemHeight;
7847 hdc = GetDC(infoPtr->hwndSelf);
7848 if (!hdc) return FALSE;
7849 ret = RectVisible(hdc, &rcItem);
7850 ReleaseDC(infoPtr->hwndSelf, hdc);
7857 * Redraws a range of items.
7860 * [I] infoPtr : valid pointer to the listview structure
7861 * [I] nFirst : first item
7862 * [I] nLast : last item
7868 static BOOL LISTVIEW_RedrawItems(const LISTVIEW_INFO *infoPtr, INT nFirst, INT nLast)
7872 if (nLast < nFirst || min(nFirst, nLast) < 0 ||
7873 max(nFirst, nLast) >= infoPtr->nItemCount)
7876 for (i = nFirst; i <= nLast; i++)
7877 LISTVIEW_InvalidateItem(infoPtr, i);
7884 * Scroll the content of a listview.
7887 * [I] infoPtr : valid pointer to the listview structure
7888 * [I] dx : horizontal scroll amount in pixels
7889 * [I] dy : vertical scroll amount in pixels
7896 * If the control is in report view (LV_VIEW_DETAILS) the control can
7897 * be scrolled only in line increments. "dy" will be rounded to the
7898 * nearest number of pixels that are a whole line. Ex: if line height
7899 * is 16 and an 8 is passed, the list will be scrolled by 16. If a 7
7900 * is passed, then the scroll will be 0. (per MSDN 7/2002)
7902 * For: (per experimentation with native control and CSpy ListView)
7903 * LV_VIEW_ICON scrolling in any direction is allowed
7904 * LV_VIEW_SMALLICON scrolling in any direction is allowed
7905 * LV_VIEW_LIST dx=1 = 1 column (horizontal only)
7906 * but will only scroll 1 column per message
7907 * no matter what the value.
7908 * dy must be 0 or FALSE returned.
7909 * LV_VIEW_DETAILS dx=1 = 1 pixel
7913 static BOOL LISTVIEW_Scroll(LISTVIEW_INFO *infoPtr, INT dx, INT dy)
7915 switch(infoPtr->uView) {
7916 case LV_VIEW_DETAILS:
7917 dy += (dy < 0 ? -1 : 1) * infoPtr->nItemHeight/2;
7918 dy /= infoPtr->nItemHeight;
7921 if (dy != 0) return FALSE;
7927 if (dx != 0) LISTVIEW_HScroll(infoPtr, SB_INTERNAL, dx);
7928 if (dy != 0) LISTVIEW_VScroll(infoPtr, SB_INTERNAL, dy);
7935 * Sets the background color.
7938 * [I] infoPtr : valid pointer to the listview structure
7939 * [I] color : background color
7945 static BOOL LISTVIEW_SetBkColor(LISTVIEW_INFO *infoPtr, COLORREF color)
7947 TRACE("(color=%x)\n", color);
7949 if(infoPtr->clrBk != color) {
7950 if (infoPtr->clrBk != CLR_NONE) DeleteObject(infoPtr->hBkBrush);
7951 infoPtr->clrBk = color;
7952 if (color == CLR_NONE)
7953 infoPtr->hBkBrush = (HBRUSH)GetClassLongPtrW(infoPtr->hwndSelf, GCLP_HBRBACKGROUND);
7956 infoPtr->hBkBrush = CreateSolidBrush(color);
7957 infoPtr->dwLvExStyle &= ~LVS_EX_TRANSPARENTBKGND;
7964 /* LISTVIEW_SetBkImage */
7966 /*** Helper for {Insert,Set}ColumnT *only* */
7967 static void column_fill_hditem(const LISTVIEW_INFO *infoPtr, HDITEMW *lphdi, INT nColumn,
7968 const LVCOLUMNW *lpColumn, BOOL isW)
7970 if (lpColumn->mask & LVCF_FMT)
7972 /* format member is valid */
7973 lphdi->mask |= HDI_FORMAT;
7975 /* set text alignment (leftmost column must be left-aligned) */
7976 if (nColumn == 0 || (lpColumn->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_LEFT)
7977 lphdi->fmt |= HDF_LEFT;
7978 else if ((lpColumn->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_RIGHT)
7979 lphdi->fmt |= HDF_RIGHT;
7980 else if ((lpColumn->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_CENTER)
7981 lphdi->fmt |= HDF_CENTER;
7983 if (lpColumn->fmt & LVCFMT_BITMAP_ON_RIGHT)
7984 lphdi->fmt |= HDF_BITMAP_ON_RIGHT;
7986 if (lpColumn->fmt & LVCFMT_COL_HAS_IMAGES)
7988 lphdi->fmt |= HDF_IMAGE;
7989 lphdi->iImage = I_IMAGECALLBACK;
7992 if (lpColumn->fmt & LVCFMT_FIXED_WIDTH)
7993 lphdi->fmt |= HDF_FIXEDWIDTH;
7996 if (lpColumn->mask & LVCF_WIDTH)
7998 lphdi->mask |= HDI_WIDTH;
7999 if(lpColumn->cx == LVSCW_AUTOSIZE_USEHEADER)
8001 /* make it fill the remainder of the controls width */
8005 for(item_index = 0; item_index < (nColumn - 1); item_index++)
8007 LISTVIEW_GetHeaderRect(infoPtr, item_index, &rcHeader);
8008 lphdi->cxy += rcHeader.right - rcHeader.left;
8011 /* retrieve the layout of the header */
8012 GetClientRect(infoPtr->hwndSelf, &rcHeader);
8013 TRACE("start cxy=%d rcHeader=%s\n", lphdi->cxy, wine_dbgstr_rect(&rcHeader));
8015 lphdi->cxy = (rcHeader.right - rcHeader.left) - lphdi->cxy;
8018 lphdi->cxy = lpColumn->cx;
8021 if (lpColumn->mask & LVCF_TEXT)
8023 lphdi->mask |= HDI_TEXT | HDI_FORMAT;
8024 lphdi->fmt |= HDF_STRING;
8025 lphdi->pszText = lpColumn->pszText;
8026 lphdi->cchTextMax = textlenT(lpColumn->pszText, isW);
8029 if (lpColumn->mask & LVCF_IMAGE)
8031 lphdi->mask |= HDI_IMAGE;
8032 lphdi->iImage = lpColumn->iImage;
8035 if (lpColumn->mask & LVCF_ORDER)
8037 lphdi->mask |= HDI_ORDER;
8038 lphdi->iOrder = lpColumn->iOrder;
8045 * Inserts a new column.
8048 * [I] infoPtr : valid pointer to the listview structure
8049 * [I] nColumn : column index
8050 * [I] lpColumn : column information
8051 * [I] isW : TRUE if lpColumn is Unicode, FALSE otherwise
8054 * SUCCESS : new column index
8057 static INT LISTVIEW_InsertColumnT(LISTVIEW_INFO *infoPtr, INT nColumn,
8058 const LVCOLUMNW *lpColumn, BOOL isW)
8060 COLUMN_INFO *lpColumnInfo;
8064 TRACE("(nColumn=%d, lpColumn=%s, isW=%d)\n", nColumn, debuglvcolumn_t(lpColumn, isW), isW);
8066 if (!lpColumn || nColumn < 0) return -1;
8067 nColumn = min(nColumn, DPA_GetPtrCount(infoPtr->hdpaColumns));
8069 ZeroMemory(&hdi, sizeof(HDITEMW));
8070 column_fill_hditem(infoPtr, &hdi, nColumn, lpColumn, isW);
8073 * A mask not including LVCF_WIDTH turns into a mask of width, width 10
8074 * (can be seen in SPY) otherwise column never gets added.
8076 if (!(lpColumn->mask & LVCF_WIDTH)) {
8077 hdi.mask |= HDI_WIDTH;
8082 * when the iSubItem is available Windows copies it to the header lParam. It seems
8083 * to happen only in LVM_INSERTCOLUMN - not in LVM_SETCOLUMN
8085 if (lpColumn->mask & LVCF_SUBITEM)
8087 hdi.mask |= HDI_LPARAM;
8088 hdi.lParam = lpColumn->iSubItem;
8091 /* create header if not present */
8092 LISTVIEW_CreateHeader(infoPtr);
8093 if (!(LVS_NOCOLUMNHEADER & infoPtr->dwStyle) &&
8094 (infoPtr->uView == LV_VIEW_DETAILS) && (WS_VISIBLE & infoPtr->dwStyle))
8096 ShowWindow(infoPtr->hwndHeader, SW_SHOWNORMAL);
8099 /* insert item in header control */
8100 nNewColumn = SendMessageW(infoPtr->hwndHeader,
8101 isW ? HDM_INSERTITEMW : HDM_INSERTITEMA,
8102 nColumn, (LPARAM)&hdi);
8103 if (nNewColumn == -1) return -1;
8104 if (nNewColumn != nColumn) ERR("nColumn=%d, nNewColumn=%d\n", nColumn, nNewColumn);
8106 /* create our own column info */
8107 if (!(lpColumnInfo = Alloc(sizeof(COLUMN_INFO)))) goto fail;
8108 if (DPA_InsertPtr(infoPtr->hdpaColumns, nNewColumn, lpColumnInfo) == -1) goto fail;
8110 if (lpColumn->mask & LVCF_FMT) lpColumnInfo->fmt = lpColumn->fmt;
8111 if (lpColumn->mask & LVCF_MINWIDTH) lpColumnInfo->cxMin = lpColumn->cxMin;
8112 if (!SendMessageW(infoPtr->hwndHeader, HDM_GETITEMRECT, nNewColumn, (LPARAM)&lpColumnInfo->rcHeader))
8115 /* now we have to actually adjust the data */
8116 if (!(infoPtr->dwStyle & LVS_OWNERDATA) && infoPtr->nItemCount > 0)
8118 SUBITEM_INFO *lpSubItem;
8124 item.iSubItem = nNewColumn;
8125 item.mask = LVIF_TEXT | LVIF_IMAGE;
8126 item.iImage = I_IMAGECALLBACK;
8127 item.pszText = LPSTR_TEXTCALLBACKW;
8129 for (nItem = 0; nItem < infoPtr->nItemCount; nItem++)
8131 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, nItem);
8132 for (i = 1; i < DPA_GetPtrCount(hdpaSubItems); i++)
8134 lpSubItem = DPA_GetPtr(hdpaSubItems, i);
8135 if (lpSubItem->iSubItem >= nNewColumn)
8136 lpSubItem->iSubItem++;
8139 /* add new subitem for each item */
8141 set_sub_item(infoPtr, &item, isW, &changed);
8145 /* make space for the new column */
8146 LISTVIEW_ScrollColumns(infoPtr, nNewColumn + 1, lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left);
8147 LISTVIEW_UpdateItemSize(infoPtr);
8152 if (nNewColumn != -1) SendMessageW(infoPtr->hwndHeader, HDM_DELETEITEM, nNewColumn, 0);
8155 DPA_DeletePtr(infoPtr->hdpaColumns, nNewColumn);
8163 * Sets the attributes of a header item.
8166 * [I] infoPtr : valid pointer to the listview structure
8167 * [I] nColumn : column index
8168 * [I] lpColumn : column attributes
8169 * [I] isW: if TRUE, then lpColumn is a LPLVCOLUMNW, else it is a LPLVCOLUMNA
8175 static BOOL LISTVIEW_SetColumnT(const LISTVIEW_INFO *infoPtr, INT nColumn,
8176 const LVCOLUMNW *lpColumn, BOOL isW)
8178 HDITEMW hdi, hdiget;
8181 TRACE("(nColumn=%d, lpColumn=%s, isW=%d)\n", nColumn, debuglvcolumn_t(lpColumn, isW), isW);
8183 if (!lpColumn || nColumn < 0 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
8185 ZeroMemory(&hdi, sizeof(HDITEMW));
8186 if (lpColumn->mask & LVCF_FMT)
8188 hdi.mask |= HDI_FORMAT;
8189 hdiget.mask = HDI_FORMAT;
8190 if (SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nColumn, (LPARAM)&hdiget))
8191 hdi.fmt = hdiget.fmt & HDF_STRING;
8193 column_fill_hditem(infoPtr, &hdi, nColumn, lpColumn, isW);
8195 /* set header item attributes */
8196 bResult = SendMessageW(infoPtr->hwndHeader, isW ? HDM_SETITEMW : HDM_SETITEMA, nColumn, (LPARAM)&hdi);
8197 if (!bResult) return FALSE;
8199 if (lpColumn->mask & LVCF_FMT)
8201 COLUMN_INFO *lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, nColumn);
8202 INT oldFmt = lpColumnInfo->fmt;
8204 lpColumnInfo->fmt = lpColumn->fmt;
8205 if ((oldFmt ^ lpColumn->fmt) & (LVCFMT_JUSTIFYMASK | LVCFMT_IMAGE))
8207 if (infoPtr->uView == LV_VIEW_DETAILS) LISTVIEW_InvalidateColumn(infoPtr, nColumn);
8211 if (lpColumn->mask & LVCF_MINWIDTH)
8212 LISTVIEW_GetColumnInfo(infoPtr, nColumn)->cxMin = lpColumn->cxMin;
8219 * Sets the column order array
8222 * [I] infoPtr : valid pointer to the listview structure
8223 * [I] iCount : number of elements in column order array
8224 * [I] lpiArray : pointer to column order array
8230 static BOOL LISTVIEW_SetColumnOrderArray(LISTVIEW_INFO *infoPtr, INT iCount, const INT *lpiArray)
8232 TRACE("iCount %d lpiArray %p\n", iCount, lpiArray);
8234 if (!lpiArray || !IsWindow(infoPtr->hwndHeader)) return FALSE;
8236 infoPtr->colRectsDirty = TRUE;
8238 return SendMessageW(infoPtr->hwndHeader, HDM_SETORDERARRAY, iCount, (LPARAM)lpiArray);
8243 * Sets the width of a column
8246 * [I] infoPtr : valid pointer to the listview structure
8247 * [I] nColumn : column index
8248 * [I] cx : column width
8254 static BOOL LISTVIEW_SetColumnWidth(LISTVIEW_INFO *infoPtr, INT nColumn, INT cx)
8256 WCHAR szDispText[DISP_TEXT_SIZE] = { 0 };
8260 TRACE("(nColumn=%d, cx=%d\n", nColumn, cx);
8262 /* set column width only if in report or list mode */
8263 if (infoPtr->uView != LV_VIEW_DETAILS && infoPtr->uView != LV_VIEW_LIST) return FALSE;
8265 /* take care of invalid cx values */
8266 if(infoPtr->uView == LV_VIEW_DETAILS && cx < -2) cx = LVSCW_AUTOSIZE;
8267 else if (infoPtr->uView == LV_VIEW_LIST && cx < 1) return FALSE;
8269 /* resize all columns if in LV_VIEW_LIST mode */
8270 if(infoPtr->uView == LV_VIEW_LIST)
8272 infoPtr->nItemWidth = cx;
8273 LISTVIEW_InvalidateList(infoPtr);
8277 if (nColumn < 0 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
8279 if (cx == LVSCW_AUTOSIZE || (cx == LVSCW_AUTOSIZE_USEHEADER && nColumn < DPA_GetPtrCount(infoPtr->hdpaColumns) -1))
8284 lvItem.mask = LVIF_TEXT;
8286 lvItem.iSubItem = nColumn;
8287 lvItem.pszText = szDispText;
8288 lvItem.cchTextMax = DISP_TEXT_SIZE;
8289 for (; lvItem.iItem < infoPtr->nItemCount; lvItem.iItem++)
8291 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) continue;
8292 nLabelWidth = LISTVIEW_GetStringWidthT(infoPtr, lvItem.pszText, TRUE);
8293 if (max_cx < nLabelWidth) max_cx = nLabelWidth;
8295 if (infoPtr->himlSmall && (nColumn == 0 || (LISTVIEW_GetColumnInfo(infoPtr, nColumn)->fmt & LVCFMT_IMAGE)))
8296 max_cx += infoPtr->iconSize.cx;
8297 max_cx += TRAILING_LABEL_PADDING;
8300 /* autosize based on listview items width */
8301 if(cx == LVSCW_AUTOSIZE)
8303 else if(cx == LVSCW_AUTOSIZE_USEHEADER)
8305 /* if iCol is the last column make it fill the remainder of the controls width */
8306 if(nColumn == DPA_GetPtrCount(infoPtr->hdpaColumns) - 1)
8311 LISTVIEW_GetOrigin(infoPtr, &Origin);
8312 LISTVIEW_GetHeaderRect(infoPtr, nColumn, &rcHeader);
8314 cx = infoPtr->rcList.right - Origin.x - rcHeader.left;
8318 /* Despite what the MS docs say, if this is not the last
8319 column, then MS resizes the column to the width of the
8320 largest text string in the column, including headers
8321 and items. This is different from LVSCW_AUTOSIZE in that
8322 LVSCW_AUTOSIZE ignores the header string length. */
8325 /* retrieve header text */
8326 hdi.mask = HDI_TEXT;
8327 hdi.cchTextMax = DISP_TEXT_SIZE;
8328 hdi.pszText = szDispText;
8329 if (SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nColumn, (LPARAM)&hdi))
8331 HDC hdc = GetDC(infoPtr->hwndSelf);
8332 HFONT old_font = SelectObject(hdc, (HFONT)SendMessageW(infoPtr->hwndHeader, WM_GETFONT, 0, 0));
8335 if (GetTextExtentPoint32W(hdc, hdi.pszText, lstrlenW(hdi.pszText), &size))
8336 cx = size.cx + TRAILING_HEADER_PADDING;
8337 /* FIXME: Take into account the header image, if one is present */
8338 SelectObject(hdc, old_font);
8339 ReleaseDC(infoPtr->hwndSelf, hdc);
8341 cx = max (cx, max_cx);
8345 if (cx < 0) return FALSE;
8347 /* call header to update the column change */
8348 hdi.mask = HDI_WIDTH;
8349 hdi.cxy = max(cx, LISTVIEW_GetColumnInfo(infoPtr, nColumn)->cxMin);
8350 TRACE("hdi.cxy=%d\n", hdi.cxy);
8351 return SendMessageW(infoPtr->hwndHeader, HDM_SETITEMW, nColumn, (LPARAM)&hdi);
8355 * Creates the checkbox imagelist. Helper for LISTVIEW_SetExtendedListViewStyle
8358 static HIMAGELIST LISTVIEW_CreateCheckBoxIL(const LISTVIEW_INFO *infoPtr)
8361 HBITMAP hbm_im, hbm_mask, hbm_orig;
8363 HBRUSH hbr_white = GetStockObject(WHITE_BRUSH);
8364 HBRUSH hbr_black = GetStockObject(BLACK_BRUSH);
8367 himl = ImageList_Create(GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON),
8368 ILC_COLOR | ILC_MASK, 2, 2);
8369 hdc_wnd = GetDC(infoPtr->hwndSelf);
8370 hdc = CreateCompatibleDC(hdc_wnd);
8371 hbm_im = CreateCompatibleBitmap(hdc_wnd, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON));
8372 hbm_mask = CreateBitmap(GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), 1, 1, NULL);
8373 ReleaseDC(infoPtr->hwndSelf, hdc_wnd);
8375 rc.left = rc.top = 0;
8376 rc.right = GetSystemMetrics(SM_CXSMICON);
8377 rc.bottom = GetSystemMetrics(SM_CYSMICON);
8379 hbm_orig = SelectObject(hdc, hbm_mask);
8380 FillRect(hdc, &rc, hbr_white);
8381 InflateRect(&rc, -2, -2);
8382 FillRect(hdc, &rc, hbr_black);
8384 SelectObject(hdc, hbm_im);
8385 DrawFrameControl(hdc, &rc, DFC_BUTTON, DFCS_BUTTONCHECK | DFCS_MONO);
8386 SelectObject(hdc, hbm_orig);
8387 ImageList_Add(himl, hbm_im, hbm_mask);
8389 SelectObject(hdc, hbm_im);
8390 DrawFrameControl(hdc, &rc, DFC_BUTTON, DFCS_BUTTONCHECK | DFCS_MONO | DFCS_CHECKED);
8391 SelectObject(hdc, hbm_orig);
8392 ImageList_Add(himl, hbm_im, hbm_mask);
8394 DeleteObject(hbm_mask);
8395 DeleteObject(hbm_im);
8403 * Sets the extended listview style.
8406 * [I] infoPtr : valid pointer to the listview structure
8408 * [I] dwStyle : style
8411 * SUCCESS : previous style
8414 static DWORD LISTVIEW_SetExtendedListViewStyle(LISTVIEW_INFO *infoPtr, DWORD mask, DWORD ex_style)
8416 DWORD old_ex_style = infoPtr->dwLvExStyle;
8418 TRACE("mask=0x%08x, ex_style=0x%08x\n", mask, ex_style);
8422 infoPtr->dwLvExStyle = (old_ex_style & ~mask) | (ex_style & mask);
8424 infoPtr->dwLvExStyle = ex_style;
8426 if((infoPtr->dwLvExStyle ^ old_ex_style) & LVS_EX_CHECKBOXES)
8428 HIMAGELIST himl = 0;
8429 if(infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES)
8432 item.mask = LVIF_STATE;
8433 item.stateMask = LVIS_STATEIMAGEMASK;
8434 item.state = INDEXTOSTATEIMAGEMASK(1);
8435 LISTVIEW_SetItemState(infoPtr, -1, &item);
8437 himl = LISTVIEW_CreateCheckBoxIL(infoPtr);
8438 if(!(infoPtr->dwStyle & LVS_SHAREIMAGELISTS))
8439 ImageList_Destroy(infoPtr->himlState);
8441 himl = LISTVIEW_SetImageList(infoPtr, LVSIL_STATE, himl);
8442 /* checkbox list replaces previous custom list or... */
8443 if(((infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES) &&
8444 !(infoPtr->dwStyle & LVS_SHAREIMAGELISTS)) ||
8445 /* ...previous was checkbox list */
8446 (old_ex_style & LVS_EX_CHECKBOXES))
8447 ImageList_Destroy(himl);
8450 if((infoPtr->dwLvExStyle ^ old_ex_style) & LVS_EX_HEADERDRAGDROP)
8454 /* if not already created */
8455 LISTVIEW_CreateHeader(infoPtr);
8457 style = GetWindowLongW(infoPtr->hwndHeader, GWL_STYLE);
8458 if (infoPtr->dwLvExStyle & LVS_EX_HEADERDRAGDROP)
8459 style |= HDS_DRAGDROP;
8461 style &= ~HDS_DRAGDROP;
8462 SetWindowLongW(infoPtr->hwndHeader, GWL_STYLE, style);
8465 /* GRIDLINES adds decoration at top so changes sizes */
8466 if((infoPtr->dwLvExStyle ^ old_ex_style) & LVS_EX_GRIDLINES)
8468 LISTVIEW_CreateHeader(infoPtr);
8469 LISTVIEW_UpdateSize(infoPtr);
8472 if((infoPtr->dwLvExStyle ^ old_ex_style) & LVS_EX_FULLROWSELECT)
8474 LISTVIEW_CreateHeader(infoPtr);
8477 if((infoPtr->dwLvExStyle ^ old_ex_style) & LVS_EX_TRANSPARENTBKGND)
8479 if (infoPtr->dwLvExStyle & LVS_EX_TRANSPARENTBKGND)
8480 LISTVIEW_SetBkColor(infoPtr, CLR_NONE);
8483 if((infoPtr->dwLvExStyle ^ old_ex_style) & LVS_EX_HEADERINALLVIEWS)
8485 if (infoPtr->dwLvExStyle & LVS_EX_HEADERINALLVIEWS)
8486 LISTVIEW_CreateHeader(infoPtr);
8488 ShowWindow(infoPtr->hwndHeader, SW_HIDE);
8489 LISTVIEW_UpdateSize(infoPtr);
8490 LISTVIEW_UpdateScroll(infoPtr);
8493 LISTVIEW_InvalidateList(infoPtr);
8494 return old_ex_style;
8499 * Sets the new hot cursor used during hot tracking and hover selection.
8502 * [I] infoPtr : valid pointer to the listview structure
8503 * [I] hCursor : the new hot cursor handle
8506 * Returns the previous hot cursor
8508 static HCURSOR LISTVIEW_SetHotCursor(LISTVIEW_INFO *infoPtr, HCURSOR hCursor)
8510 HCURSOR oldCursor = infoPtr->hHotCursor;
8512 infoPtr->hHotCursor = hCursor;
8520 * Sets the hot item index.
8523 * [I] infoPtr : valid pointer to the listview structure
8524 * [I] iIndex : index
8527 * SUCCESS : previous hot item index
8528 * FAILURE : -1 (no hot item)
8530 static INT LISTVIEW_SetHotItem(LISTVIEW_INFO *infoPtr, INT iIndex)
8532 INT iOldIndex = infoPtr->nHotItem;
8534 infoPtr->nHotItem = iIndex;
8542 * Sets the amount of time the cursor must hover over an item before it is selected.
8545 * [I] infoPtr : valid pointer to the listview structure
8546 * [I] dwHoverTime : hover time, if -1 the hover time is set to the default
8549 * Returns the previous hover time
8551 static DWORD LISTVIEW_SetHoverTime(LISTVIEW_INFO *infoPtr, DWORD dwHoverTime)
8553 DWORD oldHoverTime = infoPtr->dwHoverTime;
8555 infoPtr->dwHoverTime = dwHoverTime;
8557 return oldHoverTime;
8562 * Sets spacing for icons of LVS_ICON style.
8565 * [I] infoPtr : valid pointer to the listview structure
8566 * [I] cx : horizontal spacing (-1 = system spacing, 0 = autosize)
8567 * [I] cy : vertical spacing (-1 = system spacing, 0 = autosize)
8570 * MAKELONG(oldcx, oldcy)
8572 static DWORD LISTVIEW_SetIconSpacing(LISTVIEW_INFO *infoPtr, INT cx, INT cy)
8574 DWORD oldspacing = MAKELONG(infoPtr->iconSpacing.cx, infoPtr->iconSpacing.cy);
8576 TRACE("requested=(%d,%d)\n", cx, cy);
8578 /* this is supported only for LVS_ICON style */
8579 if (infoPtr->uView != LV_VIEW_ICON) return oldspacing;
8581 /* set to defaults, if instructed to */
8582 if (cx == -1) cx = GetSystemMetrics(SM_CXICONSPACING);
8583 if (cy == -1) cy = GetSystemMetrics(SM_CYICONSPACING);
8585 /* if 0 then compute width
8586 * FIXME: computed cx and cy is not matching native behaviour */
8588 cx = GetSystemMetrics(SM_CXICONSPACING);
8589 if (infoPtr->iconSize.cx + ICON_LR_PADDING > cx)
8590 cx = infoPtr->iconSize.cx + ICON_LR_PADDING;
8593 /* if 0 then compute height */
8595 cy = infoPtr->iconSize.cy + 2 * infoPtr->ntmHeight +
8596 ICON_BOTTOM_PADDING + ICON_TOP_PADDING + LABEL_VERT_PADDING;
8599 infoPtr->iconSpacing.cx = cx;
8600 infoPtr->iconSpacing.cy = cy;
8602 TRACE("old=(%d,%d), new=(%d,%d), iconSize=(%d,%d), ntmH=%d\n",
8603 LOWORD(oldspacing), HIWORD(oldspacing), cx, cy,
8604 infoPtr->iconSize.cx, infoPtr->iconSize.cy,
8605 infoPtr->ntmHeight);
8607 /* these depend on the iconSpacing */
8608 LISTVIEW_UpdateItemSize(infoPtr);
8613 static inline void set_icon_size(SIZE *size, HIMAGELIST himl, BOOL small)
8617 if (himl && ImageList_GetIconSize(himl, &cx, &cy))
8624 size->cx = GetSystemMetrics(small ? SM_CXSMICON : SM_CXICON);
8625 size->cy = GetSystemMetrics(small ? SM_CYSMICON : SM_CYICON);
8634 * [I] infoPtr : valid pointer to the listview structure
8635 * [I] nType : image list type
8636 * [I] himl : image list handle
8639 * SUCCESS : old image list
8642 static HIMAGELIST LISTVIEW_SetImageList(LISTVIEW_INFO *infoPtr, INT nType, HIMAGELIST himl)
8644 INT oldHeight = infoPtr->nItemHeight;
8645 HIMAGELIST himlOld = 0;
8647 TRACE("(nType=%d, himl=%p\n", nType, himl);
8652 himlOld = infoPtr->himlNormal;
8653 infoPtr->himlNormal = himl;
8654 if (infoPtr->uView == LV_VIEW_ICON) set_icon_size(&infoPtr->iconSize, himl, FALSE);
8655 LISTVIEW_SetIconSpacing(infoPtr, 0, 0);
8659 himlOld = infoPtr->himlSmall;
8660 infoPtr->himlSmall = himl;
8661 if (infoPtr->uView != LV_VIEW_ICON) set_icon_size(&infoPtr->iconSize, himl, TRUE);
8662 if (infoPtr->hwndHeader)
8663 SendMessageW(infoPtr->hwndHeader, HDM_SETIMAGELIST, 0, (LPARAM)himl);
8667 himlOld = infoPtr->himlState;
8668 infoPtr->himlState = himl;
8669 set_icon_size(&infoPtr->iconStateSize, himl, TRUE);
8670 ImageList_SetBkColor(infoPtr->himlState, CLR_NONE);
8674 ERR("Unknown icon type=%d\n", nType);
8678 infoPtr->nItemHeight = LISTVIEW_CalculateItemHeight(infoPtr);
8679 if (infoPtr->nItemHeight != oldHeight)
8680 LISTVIEW_UpdateScroll(infoPtr);
8687 * Preallocates memory (does *not* set the actual count of items !)
8690 * [I] infoPtr : valid pointer to the listview structure
8691 * [I] nItems : item count (projected number of items to allocate)
8692 * [I] dwFlags : update flags
8698 static BOOL LISTVIEW_SetItemCount(LISTVIEW_INFO *infoPtr, INT nItems, DWORD dwFlags)
8700 TRACE("(nItems=%d, dwFlags=%x)\n", nItems, dwFlags);
8702 if (infoPtr->dwStyle & LVS_OWNERDATA)
8704 INT nOldCount = infoPtr->nItemCount;
8706 if (nItems < nOldCount)
8708 RANGE range = { nItems, nOldCount };
8709 ranges_del(infoPtr->selectionRanges, range);
8710 if (infoPtr->nFocusedItem >= nItems)
8712 LISTVIEW_SetItemFocus(infoPtr, -1);
8713 SetRectEmpty(&infoPtr->rcFocus);
8717 infoPtr->nItemCount = nItems;
8718 LISTVIEW_UpdateScroll(infoPtr);
8720 /* the flags are valid only in ownerdata report and list modes */
8721 if (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON) dwFlags = 0;
8723 if (!(dwFlags & LVSICF_NOSCROLL) && infoPtr->nFocusedItem != -1)
8724 LISTVIEW_EnsureVisible(infoPtr, infoPtr->nFocusedItem, FALSE);
8726 if (!(dwFlags & LVSICF_NOINVALIDATEALL))
8727 LISTVIEW_InvalidateList(infoPtr);
8734 LISTVIEW_GetOrigin(infoPtr, &Origin);
8735 nFrom = min(nOldCount, nItems);
8736 nTo = max(nOldCount, nItems);
8738 if (infoPtr->uView == LV_VIEW_DETAILS)
8741 rcErase.top = nFrom * infoPtr->nItemHeight;
8742 rcErase.right = infoPtr->nItemWidth;
8743 rcErase.bottom = nTo * infoPtr->nItemHeight;
8744 OffsetRect(&rcErase, Origin.x, Origin.y);
8745 if (IntersectRect(&rcErase, &rcErase, &infoPtr->rcList))
8746 LISTVIEW_InvalidateRect(infoPtr, &rcErase);
8748 else /* LV_VIEW_LIST */
8750 INT nPerCol = LISTVIEW_GetCountPerColumn(infoPtr);
8752 rcErase.left = (nFrom / nPerCol) * infoPtr->nItemWidth;
8753 rcErase.top = (nFrom % nPerCol) * infoPtr->nItemHeight;
8754 rcErase.right = rcErase.left + infoPtr->nItemWidth;
8755 rcErase.bottom = nPerCol * infoPtr->nItemHeight;
8756 OffsetRect(&rcErase, Origin.x, Origin.y);
8757 if (IntersectRect(&rcErase, &rcErase, &infoPtr->rcList))
8758 LISTVIEW_InvalidateRect(infoPtr, &rcErase);
8760 rcErase.left = (nFrom / nPerCol + 1) * infoPtr->nItemWidth;
8762 rcErase.right = (nTo / nPerCol + 1) * infoPtr->nItemWidth;
8763 rcErase.bottom = nPerCol * infoPtr->nItemHeight;
8764 OffsetRect(&rcErase, Origin.x, Origin.y);
8765 if (IntersectRect(&rcErase, &rcErase, &infoPtr->rcList))
8766 LISTVIEW_InvalidateRect(infoPtr, &rcErase);
8772 /* According to MSDN for non-LVS_OWNERDATA this is just
8773 * a performance issue. The control allocates its internal
8774 * data structures for the number of items specified. It
8775 * cuts down on the number of memory allocations. Therefore
8776 * we will just issue a WARN here
8778 WARN("for non-ownerdata performance option not implemented.\n");
8786 * Sets the position of an item.
8789 * [I] infoPtr : valid pointer to the listview structure
8790 * [I] nItem : item index
8791 * [I] pt : coordinate
8797 static BOOL LISTVIEW_SetItemPosition(LISTVIEW_INFO *infoPtr, INT nItem, const POINT *pt)
8801 TRACE("(nItem=%d, pt=%s\n", nItem, wine_dbgstr_point(pt));
8803 if (!pt || nItem < 0 || nItem >= infoPtr->nItemCount ||
8804 !(infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON)) return FALSE;
8807 LISTVIEW_GetOrigin(infoPtr, &Origin);
8809 /* This point value seems to be an undocumented feature.
8810 * The best guess is that it means either at the origin,
8811 * or at true beginning of the list. I will assume the origin. */
8812 if ((Pt.x == -1) && (Pt.y == -1))
8815 if (infoPtr->uView == LV_VIEW_ICON)
8817 Pt.x -= (infoPtr->nItemWidth - infoPtr->iconSize.cx) / 2;
8818 Pt.y -= ICON_TOP_PADDING;
8823 infoPtr->bAutoarrange = FALSE;
8825 return LISTVIEW_MoveIconTo(infoPtr, nItem, &Pt, FALSE);
8830 * Sets the state of one or many items.
8833 * [I] infoPtr : valid pointer to the listview structure
8834 * [I] nItem : item index
8835 * [I] item : item or subitem info
8841 static BOOL LISTVIEW_SetItemState(LISTVIEW_INFO *infoPtr, INT nItem, const LVITEMW *item)
8846 if (!item) return FALSE;
8848 lvItem.iItem = nItem;
8849 lvItem.iSubItem = 0;
8850 lvItem.mask = LVIF_STATE;
8851 lvItem.state = item->state;
8852 lvItem.stateMask = item->stateMask;
8853 TRACE("item=%s\n", debuglvitem_t(&lvItem, TRUE));
8860 /* special case optimization for recurring attemp to deselect all */
8861 if (lvItem.state == 0 && lvItem.stateMask == LVIS_SELECTED && !LISTVIEW_GetSelectedCount(infoPtr))
8864 /* select all isn't allowed in LVS_SINGLESEL */
8865 if ((lvItem.state & lvItem.stateMask & LVIS_SELECTED) && (infoPtr->dwStyle & LVS_SINGLESEL))
8868 /* focus all isn't allowed */
8869 if (lvItem.state & lvItem.stateMask & LVIS_FOCUSED) return FALSE;
8871 notify = infoPtr->bDoChangeNotify;
8872 if (infoPtr->dwStyle & LVS_OWNERDATA)
8874 infoPtr->bDoChangeNotify = FALSE;
8875 if (!(lvItem.state & LVIS_SELECTED) && LISTVIEW_GetSelectedCount(infoPtr))
8876 oldstate |= LVIS_SELECTED;
8877 if (infoPtr->nFocusedItem != -1) oldstate |= LVIS_FOCUSED;
8880 /* apply to all items */
8881 for (lvItem.iItem = 0; lvItem.iItem < infoPtr->nItemCount; lvItem.iItem++)
8882 if (!LISTVIEW_SetItemT(infoPtr, &lvItem, TRUE)) ret = FALSE;
8884 if (infoPtr->dwStyle & LVS_OWNERDATA)
8888 infoPtr->bDoChangeNotify = notify;
8892 nmlv.uNewState = lvItem.state & lvItem.stateMask;
8893 nmlv.uOldState = oldstate & lvItem.stateMask;
8894 nmlv.uChanged = LVIF_STATE;
8895 nmlv.ptAction.x = nmlv.ptAction.y = 0;
8898 notify_listview(infoPtr, LVN_ITEMCHANGED, &nmlv);
8902 ret = LISTVIEW_SetItemT(infoPtr, &lvItem, TRUE);
8909 * Sets the text of an item or subitem.
8912 * [I] hwnd : window handle
8913 * [I] nItem : item index
8914 * [I] lpLVItem : item or subitem info
8915 * [I] isW : TRUE if input is Unicode
8921 static BOOL LISTVIEW_SetItemTextT(LISTVIEW_INFO *infoPtr, INT nItem, const LVITEMW *lpLVItem, BOOL isW)
8925 if (!lpLVItem || nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
8926 if (infoPtr->dwStyle & LVS_OWNERDATA) return FALSE;
8928 lvItem.iItem = nItem;
8929 lvItem.iSubItem = lpLVItem->iSubItem;
8930 lvItem.mask = LVIF_TEXT;
8931 lvItem.pszText = lpLVItem->pszText;
8932 lvItem.cchTextMax = lpLVItem->cchTextMax;
8934 TRACE("(nItem=%d, lpLVItem=%s, isW=%d)\n", nItem, debuglvitem_t(&lvItem, isW), isW);
8936 return LISTVIEW_SetItemT(infoPtr, &lvItem, isW);
8941 * Set item index that marks the start of a multiple selection.
8944 * [I] infoPtr : valid pointer to the listview structure
8945 * [I] nIndex : index
8948 * Index number or -1 if there is no selection mark.
8950 static INT LISTVIEW_SetSelectionMark(LISTVIEW_INFO *infoPtr, INT nIndex)
8952 INT nOldIndex = infoPtr->nSelectionMark;
8954 TRACE("(nIndex=%d)\n", nIndex);
8956 infoPtr->nSelectionMark = nIndex;
8963 * Sets the text background color.
8966 * [I] infoPtr : valid pointer to the listview structure
8967 * [I] color : text background color
8973 static BOOL LISTVIEW_SetTextBkColor(LISTVIEW_INFO *infoPtr, COLORREF color)
8975 TRACE("(color=%x)\n", color);
8977 infoPtr->clrTextBk = color;
8983 * Sets the text foreground color.
8986 * [I] infoPtr : valid pointer to the listview structure
8987 * [I] color : text color
8993 static BOOL LISTVIEW_SetTextColor (LISTVIEW_INFO *infoPtr, COLORREF color)
8995 TRACE("(color=%x)\n", color);
8997 infoPtr->clrText = color;
9003 * Sets new ToolTip window to ListView control.
9006 * [I] infoPtr : valid pointer to the listview structure
9007 * [I] hwndNewToolTip : handle to new ToolTip
9012 static HWND LISTVIEW_SetToolTips( LISTVIEW_INFO *infoPtr, HWND hwndNewToolTip)
9014 HWND hwndOldToolTip = infoPtr->hwndToolTip;
9015 infoPtr->hwndToolTip = hwndNewToolTip;
9016 return hwndOldToolTip;
9021 * sets the Unicode character format flag for the control
9023 * [I] infoPtr :valid pointer to the listview structure
9024 * [I] fUnicode :true to switch to UNICODE false to switch to ANSI
9027 * Old Unicode Format
9029 static BOOL LISTVIEW_SetUnicodeFormat( LISTVIEW_INFO *infoPtr, BOOL unicode)
9031 SHORT rc = infoPtr->notifyFormat;
9032 infoPtr->notifyFormat = (unicode) ? NFR_UNICODE : NFR_ANSI;
9033 return rc == NFR_UNICODE;
9038 * sets the control view mode
9040 * [I] infoPtr :valid pointer to the listview structure
9041 * [I] nView :new view mode value
9047 static INT LISTVIEW_SetView(LISTVIEW_INFO *infoPtr, DWORD nView)
9049 SIZE oldIconSize = infoPtr->iconSize;
9052 if (infoPtr->uView == nView) return 1;
9054 if ((INT)nView < 0 || nView > LV_VIEW_MAX) return -1;
9055 if (nView == LV_VIEW_TILE)
9057 FIXME("View LV_VIEW_TILE unimplemented\n");
9061 infoPtr->uView = nView;
9063 SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
9064 ShowWindow(infoPtr->hwndHeader, SW_HIDE);
9066 ShowScrollBar(infoPtr->hwndSelf, SB_BOTH, FALSE);
9067 SetRectEmpty(&infoPtr->rcFocus);
9069 himl = (nView == LV_VIEW_ICON ? infoPtr->himlNormal : infoPtr->himlSmall);
9070 set_icon_size(&infoPtr->iconSize, himl, nView != LV_VIEW_ICON);
9075 if ((infoPtr->iconSize.cx != oldIconSize.cx) || (infoPtr->iconSize.cy != oldIconSize.cy))
9077 TRACE("icon old size=(%d,%d), new size=(%d,%d)\n",
9078 oldIconSize.cx, oldIconSize.cy, infoPtr->iconSize.cx, infoPtr->iconSize.cy);
9079 LISTVIEW_SetIconSpacing(infoPtr, 0, 0);
9081 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
9083 case LV_VIEW_SMALLICON:
9084 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
9086 case LV_VIEW_DETAILS:
9091 LISTVIEW_CreateHeader( infoPtr );
9093 hl.prc = &infoPtr->rcList;
9095 SendMessageW(infoPtr->hwndHeader, HDM_LAYOUT, 0, (LPARAM)&hl);
9096 SetWindowPos(infoPtr->hwndHeader, infoPtr->hwndSelf, wp.x, wp.y, wp.cx, wp.cy,
9097 wp.flags | ((infoPtr->dwStyle & LVS_NOCOLUMNHEADER) ? SWP_HIDEWINDOW : SWP_SHOWWINDOW));
9104 LISTVIEW_UpdateItemSize(infoPtr);
9105 LISTVIEW_UpdateSize(infoPtr);
9106 LISTVIEW_UpdateScroll(infoPtr);
9107 LISTVIEW_InvalidateList(infoPtr);
9109 TRACE("nView=%d\n", nView);
9114 /* LISTVIEW_SetWorkAreas */
9118 * Callback internally used by LISTVIEW_SortItems() in response of LVM_SORTITEMS
9121 * [I] first : pointer to first ITEM_INFO to compare
9122 * [I] second : pointer to second ITEM_INFO to compare
9123 * [I] lParam : HWND of control
9126 * if first comes before second : negative
9127 * if first comes after second : positive
9128 * if first and second are equivalent : zero
9130 static INT WINAPI LISTVIEW_CallBackCompare(LPVOID first, LPVOID second, LPARAM lParam)
9132 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)lParam;
9133 ITEM_INFO* lv_first = DPA_GetPtr( first, 0 );
9134 ITEM_INFO* lv_second = DPA_GetPtr( second, 0 );
9136 /* Forward the call to the client defined callback */
9137 return (infoPtr->pfnCompare)( lv_first->lParam , lv_second->lParam, infoPtr->lParamSort );
9142 * Callback internally used by LISTVIEW_SortItems() in response of LVM_SORTITEMSEX
9145 * [I] first : pointer to first ITEM_INFO to compare
9146 * [I] second : pointer to second ITEM_INFO to compare
9147 * [I] lParam : HWND of control
9150 * if first comes before second : negative
9151 * if first comes after second : positive
9152 * if first and second are equivalent : zero
9154 static INT WINAPI LISTVIEW_CallBackCompareEx(LPVOID first, LPVOID second, LPARAM lParam)
9156 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)lParam;
9157 INT first_idx = DPA_GetPtrIndex( infoPtr->hdpaItems, first );
9158 INT second_idx = DPA_GetPtrIndex( infoPtr->hdpaItems, second );
9160 /* Forward the call to the client defined callback */
9161 return (infoPtr->pfnCompare)( first_idx, second_idx, infoPtr->lParamSort );
9166 * Sorts the listview items.
9169 * [I] infoPtr : valid pointer to the listview structure
9170 * [I] pfnCompare : application-defined value
9171 * [I] lParamSort : pointer to comparison callback
9172 * [I] IsEx : TRUE when LVM_SORTITEMSEX used
9178 static BOOL LISTVIEW_SortItems(LISTVIEW_INFO *infoPtr, PFNLVCOMPARE pfnCompare,
9179 LPARAM lParamSort, BOOL IsEx)
9183 LPVOID selectionMarkItem = NULL;
9184 LPVOID focusedItem = NULL;
9187 TRACE("(pfnCompare=%p, lParamSort=%lx)\n", pfnCompare, lParamSort);
9189 if (infoPtr->dwStyle & LVS_OWNERDATA) return FALSE;
9191 if (!pfnCompare) return FALSE;
9192 if (!infoPtr->hdpaItems) return FALSE;
9194 /* if there are 0 or 1 items, there is no need to sort */
9195 if (infoPtr->nItemCount < 2) return TRUE;
9197 /* clear selection */
9198 ranges_clear(infoPtr->selectionRanges);
9200 /* save selection mark and focused item */
9201 if (infoPtr->nSelectionMark >= 0)
9202 selectionMarkItem = DPA_GetPtr(infoPtr->hdpaItems, infoPtr->nSelectionMark);
9203 if (infoPtr->nFocusedItem >= 0)
9204 focusedItem = DPA_GetPtr(infoPtr->hdpaItems, infoPtr->nFocusedItem);
9206 infoPtr->pfnCompare = pfnCompare;
9207 infoPtr->lParamSort = lParamSort;
9209 DPA_Sort(infoPtr->hdpaItems, LISTVIEW_CallBackCompareEx, (LPARAM)infoPtr);
9211 DPA_Sort(infoPtr->hdpaItems, LISTVIEW_CallBackCompare, (LPARAM)infoPtr);
9213 /* restore selection ranges */
9214 for (i=0; i < infoPtr->nItemCount; i++)
9216 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, i);
9217 lpItem = DPA_GetPtr(hdpaSubItems, 0);
9219 if (lpItem->state & LVIS_SELECTED)
9220 ranges_additem(infoPtr->selectionRanges, i);
9222 /* restore selection mark and focused item */
9223 infoPtr->nSelectionMark = DPA_GetPtrIndex(infoPtr->hdpaItems, selectionMarkItem);
9224 infoPtr->nFocusedItem = DPA_GetPtrIndex(infoPtr->hdpaItems, focusedItem);
9226 /* I believe nHotItem should be left alone, see LISTVIEW_ShiftIndices */
9228 /* refresh the display */
9229 LISTVIEW_InvalidateList(infoPtr);
9235 * Update theme handle after a theme change.
9238 * [I] infoPtr : valid pointer to the listview structure
9242 * FAILURE : something else
9244 static LRESULT LISTVIEW_ThemeChanged(const LISTVIEW_INFO *infoPtr)
9246 HTHEME theme = GetWindowTheme(infoPtr->hwndSelf);
9247 CloseThemeData(theme);
9248 OpenThemeData(infoPtr->hwndSelf, themeClass);
9254 * Updates an items or rearranges the listview control.
9257 * [I] infoPtr : valid pointer to the listview structure
9258 * [I] nItem : item index
9264 static BOOL LISTVIEW_Update(LISTVIEW_INFO *infoPtr, INT nItem)
9266 TRACE("(nItem=%d)\n", nItem);
9268 if (nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
9270 /* rearrange with default alignment style */
9271 if (is_autoarrange(infoPtr))
9272 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
9274 LISTVIEW_InvalidateItem(infoPtr, nItem);
9281 * Draw the track line at the place defined in the infoPtr structure.
9282 * The line is drawn with a XOR pen so drawing the line for the second time
9283 * in the same place erases the line.
9286 * [I] infoPtr : valid pointer to the listview structure
9292 static BOOL LISTVIEW_DrawTrackLine(const LISTVIEW_INFO *infoPtr)
9296 if (infoPtr->xTrackLine == -1)
9299 if (!(hdc = GetDC(infoPtr->hwndSelf)))
9301 PatBlt( hdc, infoPtr->xTrackLine, infoPtr->rcList.top,
9302 1, infoPtr->rcList.bottom - infoPtr->rcList.top, DSTINVERT );
9303 ReleaseDC(infoPtr->hwndSelf, hdc);
9309 * Called when an edit control should be displayed. This function is called after
9310 * we are sure that there was a single click - not a double click (this is a TIMERPROC).
9313 * [I] hwnd : Handle to the listview
9314 * [I] uMsg : WM_TIMER (ignored)
9315 * [I] idEvent : The timer ID interpreted as a pointer to a DELAYED_EDIT_ITEM struct
9316 * [I] dwTimer : The elapsed time (ignored)
9321 static VOID CALLBACK LISTVIEW_DelayedEditItem(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
9323 DELAYED_ITEM_EDIT *editItem = (DELAYED_ITEM_EDIT *)idEvent;
9324 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongPtrW(hwnd, 0);
9326 KillTimer(hwnd, idEvent);
9327 editItem->fEnabled = FALSE;
9328 /* check if the item is still selected */
9329 if (infoPtr->bFocus && LISTVIEW_GetItemState(infoPtr, editItem->iItem, LVIS_SELECTED))
9330 LISTVIEW_EditLabelT(infoPtr, editItem->iItem, TRUE);
9335 * Creates the listview control - the WM_NCCREATE phase.
9338 * [I] hwnd : window handle
9339 * [I] lpcs : the create parameters
9345 static LRESULT LISTVIEW_NCCreate(HWND hwnd, const CREATESTRUCTW *lpcs)
9347 LISTVIEW_INFO *infoPtr;
9350 TRACE("(lpcs=%p)\n", lpcs);
9352 /* initialize info pointer */
9353 infoPtr = Alloc(sizeof(LISTVIEW_INFO));
9354 if (!infoPtr) return FALSE;
9356 SetWindowLongPtrW(hwnd, 0, (DWORD_PTR)infoPtr);
9358 infoPtr->hwndSelf = hwnd;
9359 infoPtr->dwStyle = lpcs->style; /* Note: may be changed in WM_CREATE */
9360 map_style_view(infoPtr);
9361 /* determine the type of structures to use */
9362 infoPtr->hwndNotify = lpcs->hwndParent;
9363 /* infoPtr->notifyFormat will be filled in WM_CREATE */
9365 /* initialize color information */
9366 infoPtr->clrBk = CLR_NONE;
9367 infoPtr->clrText = CLR_DEFAULT;
9368 infoPtr->clrTextBk = CLR_DEFAULT;
9369 LISTVIEW_SetBkColor(infoPtr, comctl32_color.clrWindow);
9371 /* set default values */
9372 infoPtr->nFocusedItem = -1;
9373 infoPtr->nSelectionMark = -1;
9374 infoPtr->nHotItem = -1;
9375 infoPtr->bRedraw = TRUE;
9376 infoPtr->bNoItemMetrics = TRUE;
9377 infoPtr->bDoChangeNotify = TRUE;
9378 infoPtr->iconSpacing.cx = GetSystemMetrics(SM_CXICONSPACING);
9379 infoPtr->iconSpacing.cy = GetSystemMetrics(SM_CYICONSPACING);
9380 infoPtr->nEditLabelItem = -1;
9381 infoPtr->nLButtonDownItem = -1;
9382 infoPtr->dwHoverTime = HOVER_DEFAULT; /* default system hover time */
9383 infoPtr->nMeasureItemHeight = 0;
9384 infoPtr->xTrackLine = -1; /* no track line */
9385 infoPtr->itemEdit.fEnabled = FALSE;
9386 infoPtr->iVersion = COMCTL32_VERSION;
9387 infoPtr->colRectsDirty = FALSE;
9389 /* get default font (icon title) */
9390 SystemParametersInfoW(SPI_GETICONTITLELOGFONT, 0, &logFont, 0);
9391 infoPtr->hDefaultFont = CreateFontIndirectW(&logFont);
9392 infoPtr->hFont = infoPtr->hDefaultFont;
9393 LISTVIEW_SaveTextMetrics(infoPtr);
9395 /* allocate memory for the data structure */
9396 if (!(infoPtr->selectionRanges = ranges_create(10))) goto fail;
9397 if (!(infoPtr->hdpaItems = DPA_Create(10))) goto fail;
9398 if (!(infoPtr->hdpaItemIds = DPA_Create(10))) goto fail;
9399 if (!(infoPtr->hdpaPosX = DPA_Create(10))) goto fail;
9400 if (!(infoPtr->hdpaPosY = DPA_Create(10))) goto fail;
9401 if (!(infoPtr->hdpaColumns = DPA_Create(10))) goto fail;
9405 DestroyWindow(infoPtr->hwndHeader);
9406 ranges_destroy(infoPtr->selectionRanges);
9407 DPA_Destroy(infoPtr->hdpaItems);
9408 DPA_Destroy(infoPtr->hdpaItemIds);
9409 DPA_Destroy(infoPtr->hdpaPosX);
9410 DPA_Destroy(infoPtr->hdpaPosY);
9411 DPA_Destroy(infoPtr->hdpaColumns);
9418 * Creates the listview control - the WM_CREATE phase. Most of the data is
9419 * already set up in LISTVIEW_NCCreate
9422 * [I] hwnd : window handle
9423 * [I] lpcs : the create parameters
9429 static LRESULT LISTVIEW_Create(HWND hwnd, const CREATESTRUCTW *lpcs)
9431 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongPtrW(hwnd, 0);
9433 TRACE("(lpcs=%p, style=0x%08x)\n", lpcs, lpcs->style);
9435 infoPtr->dwStyle = lpcs->style;
9436 map_style_view(infoPtr);
9438 infoPtr->notifyFormat = SendMessageW(infoPtr->hwndNotify, WM_NOTIFYFORMAT,
9439 (WPARAM)infoPtr->hwndSelf, NF_QUERY);
9440 /* on error defaulting to ANSI notifications */
9441 if (infoPtr->notifyFormat == 0) infoPtr->notifyFormat = NFR_ANSI;
9442 TRACE("notify format=%d\n", infoPtr->notifyFormat);
9444 if ((infoPtr->uView == LV_VIEW_DETAILS) && (lpcs->style & WS_VISIBLE))
9446 if (LISTVIEW_CreateHeader(infoPtr) < 0) return -1;
9449 infoPtr->hwndHeader = 0;
9451 /* init item size to avoid division by 0 */
9452 LISTVIEW_UpdateItemSize (infoPtr);
9453 LISTVIEW_UpdateSize (infoPtr);
9455 if (infoPtr->uView == LV_VIEW_DETAILS)
9457 if (!(LVS_NOCOLUMNHEADER & lpcs->style) && (WS_VISIBLE & lpcs->style))
9459 ShowWindow(infoPtr->hwndHeader, SW_SHOWNORMAL);
9461 LISTVIEW_UpdateScroll(infoPtr);
9462 /* send WM_MEASUREITEM notification */
9463 if (infoPtr->dwStyle & LVS_OWNERDRAWFIXED) notify_measureitem(infoPtr);
9466 OpenThemeData(hwnd, themeClass);
9468 /* initialize the icon sizes */
9469 set_icon_size(&infoPtr->iconSize, infoPtr->himlNormal, infoPtr->uView != LV_VIEW_ICON);
9470 set_icon_size(&infoPtr->iconStateSize, infoPtr->himlState, TRUE);
9476 * Destroys the listview control.
9479 * [I] infoPtr : valid pointer to the listview structure
9485 static LRESULT LISTVIEW_Destroy(LISTVIEW_INFO *infoPtr)
9487 HTHEME theme = GetWindowTheme(infoPtr->hwndSelf);
9488 CloseThemeData(theme);
9490 /* delete all items */
9491 LISTVIEW_DeleteAllItems(infoPtr, TRUE);
9498 * Enables the listview control.
9501 * [I] infoPtr : valid pointer to the listview structure
9502 * [I] bEnable : specifies whether to enable or disable the window
9508 static BOOL LISTVIEW_Enable(const LISTVIEW_INFO *infoPtr)
9510 if (infoPtr->dwStyle & LVS_OWNERDRAWFIXED)
9511 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
9517 * Erases the background of the listview control.
9520 * [I] infoPtr : valid pointer to the listview structure
9521 * [I] hdc : device context handle
9527 static inline BOOL LISTVIEW_EraseBkgnd(const LISTVIEW_INFO *infoPtr, HDC hdc)
9531 TRACE("(hdc=%p)\n", hdc);
9533 if (!GetClipBox(hdc, &rc)) return FALSE;
9535 if (infoPtr->clrBk == CLR_NONE)
9537 if (infoPtr->dwLvExStyle & LVS_EX_TRANSPARENTBKGND)
9538 return SendMessageW(infoPtr->hwndNotify, WM_PRINTCLIENT,
9539 (WPARAM)hdc, PRF_ERASEBKGND);
9541 return SendMessageW(infoPtr->hwndNotify, WM_ERASEBKGND, (WPARAM)hdc, 0);
9544 /* for double buffered controls we need to do this during refresh */
9545 if (infoPtr->dwLvExStyle & LVS_EX_DOUBLEBUFFER) return FALSE;
9547 return LISTVIEW_FillBkgnd(infoPtr, hdc, &rc);
9553 * Helper function for LISTVIEW_[HV]Scroll *only*.
9554 * Performs vertical/horizontal scrolling by a give amount.
9557 * [I] infoPtr : valid pointer to the listview structure
9558 * [I] dx : amount of horizontal scroll
9559 * [I] dy : amount of vertical scroll
9561 static void scroll_list(LISTVIEW_INFO *infoPtr, INT dx, INT dy)
9563 /* now we can scroll the list */
9564 ScrollWindowEx(infoPtr->hwndSelf, dx, dy, &infoPtr->rcList,
9565 &infoPtr->rcList, 0, 0, SW_ERASE | SW_INVALIDATE);
9566 /* if we have focus, adjust rect */
9567 OffsetRect(&infoPtr->rcFocus, dx, dy);
9568 UpdateWindow(infoPtr->hwndSelf);
9573 * Performs vertical scrolling.
9576 * [I] infoPtr : valid pointer to the listview structure
9577 * [I] nScrollCode : scroll code
9578 * [I] nScrollDiff : units to scroll in SB_INTERNAL mode, 0 otherwise
9579 * [I] hScrollWnd : scrollbar control window handle
9585 * SB_LINEUP/SB_LINEDOWN:
9586 * for LVS_ICON, LVS_SMALLICON is 37 by experiment
9587 * for LVS_REPORT is 1 line
9588 * for LVS_LIST cannot occur
9591 static LRESULT LISTVIEW_VScroll(LISTVIEW_INFO *infoPtr, INT nScrollCode,
9594 INT nOldScrollPos, nNewScrollPos;
9595 SCROLLINFO scrollInfo;
9598 TRACE("(nScrollCode=%d(%s), nScrollDiff=%d)\n", nScrollCode,
9599 debugscrollcode(nScrollCode), nScrollDiff);
9601 if (infoPtr->hwndEdit) SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
9603 scrollInfo.cbSize = sizeof(SCROLLINFO);
9604 scrollInfo.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_TRACKPOS;
9606 is_an_icon = ((infoPtr->uView == LV_VIEW_ICON) || (infoPtr->uView == LV_VIEW_SMALLICON));
9608 if (!GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo)) return 1;
9610 nOldScrollPos = scrollInfo.nPos;
9611 switch (nScrollCode)
9617 nScrollDiff = (is_an_icon) ? -LISTVIEW_SCROLL_ICON_LINE_SIZE : -1;
9621 nScrollDiff = (is_an_icon) ? LISTVIEW_SCROLL_ICON_LINE_SIZE : 1;
9625 nScrollDiff = -scrollInfo.nPage;
9629 nScrollDiff = scrollInfo.nPage;
9632 case SB_THUMBPOSITION:
9634 nScrollDiff = scrollInfo.nTrackPos - scrollInfo.nPos;
9641 /* quit right away if pos isn't changing */
9642 if (nScrollDiff == 0) return 0;
9644 /* calculate new position, and handle overflows */
9645 nNewScrollPos = scrollInfo.nPos + nScrollDiff;
9646 if (nScrollDiff > 0) {
9647 if (nNewScrollPos < nOldScrollPos ||
9648 nNewScrollPos > scrollInfo.nMax)
9649 nNewScrollPos = scrollInfo.nMax;
9651 if (nNewScrollPos > nOldScrollPos ||
9652 nNewScrollPos < scrollInfo.nMin)
9653 nNewScrollPos = scrollInfo.nMin;
9656 /* set the new position, and reread in case it changed */
9657 scrollInfo.fMask = SIF_POS;
9658 scrollInfo.nPos = nNewScrollPos;
9659 nNewScrollPos = SetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo, TRUE);
9661 /* carry on only if it really changed */
9662 if (nNewScrollPos == nOldScrollPos) return 0;
9664 /* now adjust to client coordinates */
9665 nScrollDiff = nOldScrollPos - nNewScrollPos;
9666 if (infoPtr->uView == LV_VIEW_DETAILS) nScrollDiff *= infoPtr->nItemHeight;
9668 /* and scroll the window */
9669 scroll_list(infoPtr, 0, nScrollDiff);
9676 * Performs horizontal scrolling.
9679 * [I] infoPtr : valid pointer to the listview structure
9680 * [I] nScrollCode : scroll code
9681 * [I] nScrollDiff : units to scroll in SB_INTERNAL mode, 0 otherwise
9682 * [I] hScrollWnd : scrollbar control window handle
9688 * SB_LINELEFT/SB_LINERIGHT:
9689 * for LVS_ICON, LVS_SMALLICON 1 pixel
9690 * for LVS_REPORT is 1 pixel
9691 * for LVS_LIST is 1 column --> which is a 1 because the
9692 * scroll is based on columns not pixels
9695 static LRESULT LISTVIEW_HScroll(LISTVIEW_INFO *infoPtr, INT nScrollCode,
9698 INT nOldScrollPos, nNewScrollPos;
9699 SCROLLINFO scrollInfo;
9702 TRACE("(nScrollCode=%d(%s), nScrollDiff=%d)\n", nScrollCode,
9703 debugscrollcode(nScrollCode), nScrollDiff);
9705 if (infoPtr->hwndEdit) SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
9707 scrollInfo.cbSize = sizeof(SCROLLINFO);
9708 scrollInfo.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_TRACKPOS;
9710 is_an_icon = ((infoPtr->uView == LV_VIEW_ICON) || (infoPtr->uView == LV_VIEW_SMALLICON));
9712 if (!GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo)) return 1;
9714 nOldScrollPos = scrollInfo.nPos;
9716 switch (nScrollCode)
9722 nScrollDiff = (is_an_icon) ? -LISTVIEW_SCROLL_ICON_LINE_SIZE : -1;
9726 nScrollDiff = (is_an_icon) ? LISTVIEW_SCROLL_ICON_LINE_SIZE : 1;
9730 nScrollDiff = -scrollInfo.nPage;
9734 nScrollDiff = scrollInfo.nPage;
9737 case SB_THUMBPOSITION:
9739 nScrollDiff = scrollInfo.nTrackPos - scrollInfo.nPos;
9746 /* quit right away if pos isn't changing */
9747 if (nScrollDiff == 0) return 0;
9749 /* calculate new position, and handle overflows */
9750 nNewScrollPos = scrollInfo.nPos + nScrollDiff;
9751 if (nScrollDiff > 0) {
9752 if (nNewScrollPos < nOldScrollPos ||
9753 nNewScrollPos > scrollInfo.nMax)
9754 nNewScrollPos = scrollInfo.nMax;
9756 if (nNewScrollPos > nOldScrollPos ||
9757 nNewScrollPos < scrollInfo.nMin)
9758 nNewScrollPos = scrollInfo.nMin;
9761 /* set the new position, and reread in case it changed */
9762 scrollInfo.fMask = SIF_POS;
9763 scrollInfo.nPos = nNewScrollPos;
9764 nNewScrollPos = SetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo, TRUE);
9766 /* carry on only if it really changed */
9767 if (nNewScrollPos == nOldScrollPos) return 0;
9769 if (infoPtr->hwndHeader) LISTVIEW_UpdateHeaderSize(infoPtr, nNewScrollPos);
9771 /* now adjust to client coordinates */
9772 nScrollDiff = nOldScrollPos - nNewScrollPos;
9773 if (infoPtr->uView == LV_VIEW_LIST) nScrollDiff *= infoPtr->nItemWidth;
9775 /* and scroll the window */
9776 scroll_list(infoPtr, nScrollDiff, 0);
9781 static LRESULT LISTVIEW_MouseWheel(LISTVIEW_INFO *infoPtr, INT wheelDelta)
9783 INT gcWheelDelta = 0;
9784 INT pulScrollLines = 3;
9786 TRACE("(wheelDelta=%d)\n", wheelDelta);
9788 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
9789 gcWheelDelta -= wheelDelta;
9791 switch(infoPtr->uView)
9794 case LV_VIEW_SMALLICON:
9796 * listview should be scrolled by a multiple of 37 dependently on its dimension or its visible item number
9797 * should be fixed in the future.
9799 LISTVIEW_VScroll(infoPtr, SB_INTERNAL, (gcWheelDelta < 0) ?
9800 -LISTVIEW_SCROLL_ICON_LINE_SIZE : LISTVIEW_SCROLL_ICON_LINE_SIZE);
9803 case LV_VIEW_DETAILS:
9804 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
9806 int cLineScroll = min(LISTVIEW_GetCountPerColumn(infoPtr), pulScrollLines);
9807 cLineScroll *= (gcWheelDelta / WHEEL_DELTA);
9808 LISTVIEW_VScroll(infoPtr, SB_INTERNAL, cLineScroll);
9813 LISTVIEW_HScroll(infoPtr, (gcWheelDelta < 0) ? SB_LINELEFT : SB_LINERIGHT, 0);
9824 * [I] infoPtr : valid pointer to the listview structure
9825 * [I] nVirtualKey : virtual key
9826 * [I] lKeyData : key data
9831 static LRESULT LISTVIEW_KeyDown(LISTVIEW_INFO *infoPtr, INT nVirtualKey, LONG lKeyData)
9833 HWND hwndSelf = infoPtr->hwndSelf;
9835 NMLVKEYDOWN nmKeyDown;
9837 TRACE("(nVirtualKey=%d, lKeyData=%d)\n", nVirtualKey, lKeyData);
9839 /* send LVN_KEYDOWN notification */
9840 nmKeyDown.wVKey = nVirtualKey;
9841 nmKeyDown.flags = 0;
9842 notify_hdr(infoPtr, LVN_KEYDOWN, &nmKeyDown.hdr);
9843 if (!IsWindow(hwndSelf))
9846 switch (nVirtualKey)
9849 nItem = infoPtr->nFocusedItem;
9850 if (infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES)
9851 toggle_checkbox_state(infoPtr, infoPtr->nFocusedItem);
9855 if ((infoPtr->nItemCount > 0) && (infoPtr->nFocusedItem != -1))
9857 if (!notify(infoPtr, NM_RETURN)) return 0;
9858 if (!notify(infoPtr, LVN_ITEMACTIVATE)) return 0;
9863 if (infoPtr->nItemCount > 0)
9868 if (infoPtr->nItemCount > 0)
9869 nItem = infoPtr->nItemCount - 1;
9873 nItem = LISTVIEW_GetNextItem(infoPtr, infoPtr->nFocusedItem, LVNI_TOLEFT);
9877 nItem = LISTVIEW_GetNextItem(infoPtr, infoPtr->nFocusedItem, LVNI_ABOVE);
9881 nItem = LISTVIEW_GetNextItem(infoPtr, infoPtr->nFocusedItem, LVNI_TORIGHT);
9885 nItem = LISTVIEW_GetNextItem(infoPtr, infoPtr->nFocusedItem, LVNI_BELOW);
9889 if (infoPtr->uView == LV_VIEW_DETAILS)
9891 INT topidx = LISTVIEW_GetTopIndex(infoPtr);
9892 if (infoPtr->nFocusedItem == topidx)
9893 nItem = topidx - LISTVIEW_GetCountPerColumn(infoPtr) + 1;
9898 nItem = infoPtr->nFocusedItem - LISTVIEW_GetCountPerColumn(infoPtr)
9899 * LISTVIEW_GetCountPerRow(infoPtr);
9900 if(nItem < 0) nItem = 0;
9904 if (infoPtr->uView == LV_VIEW_DETAILS)
9906 INT topidx = LISTVIEW_GetTopIndex(infoPtr);
9907 INT cnt = LISTVIEW_GetCountPerColumn(infoPtr);
9908 if (infoPtr->nFocusedItem == topidx + cnt - 1)
9909 nItem = infoPtr->nFocusedItem + cnt - 1;
9911 nItem = topidx + cnt - 1;
9914 nItem = infoPtr->nFocusedItem + LISTVIEW_GetCountPerColumn(infoPtr)
9915 * LISTVIEW_GetCountPerRow(infoPtr);
9916 if(nItem >= infoPtr->nItemCount) nItem = infoPtr->nItemCount - 1;
9920 if ((nItem != -1) && (nItem != infoPtr->nFocusedItem || nVirtualKey == VK_SPACE))
9921 LISTVIEW_KeySelection(infoPtr, nItem, nVirtualKey == VK_SPACE);
9931 * [I] infoPtr : valid pointer to the listview structure
9936 static LRESULT LISTVIEW_KillFocus(LISTVIEW_INFO *infoPtr)
9940 /* if we did not have the focus, there's nothing to do */
9941 if (!infoPtr->bFocus) return 0;
9943 /* send NM_KILLFOCUS notification */
9944 if (!notify(infoPtr, NM_KILLFOCUS)) return 0;
9946 /* if we have a focus rectangle, get rid of it */
9947 LISTVIEW_ShowFocusRect(infoPtr, FALSE);
9949 /* if have a marquee selection, stop it */
9950 if (infoPtr->bMarqueeSelect)
9952 /* Remove the marquee rectangle and release our mouse capture */
9953 LISTVIEW_InvalidateRect(infoPtr, &infoPtr->marqueeRect);
9956 SetRect(&infoPtr->marqueeRect, 0, 0, 0, 0);
9958 infoPtr->bMarqueeSelect = FALSE;
9959 infoPtr->bScrolling = FALSE;
9960 KillTimer(infoPtr->hwndSelf, (UINT_PTR) infoPtr);
9963 /* set window focus flag */
9964 infoPtr->bFocus = FALSE;
9966 /* invalidate the selected items before resetting focus flag */
9967 LISTVIEW_InvalidateSelectedItems(infoPtr);
9974 * Processes double click messages (left mouse button).
9977 * [I] infoPtr : valid pointer to the listview structure
9978 * [I] wKey : key flag
9979 * [I] x,y : mouse coordinate
9984 static LRESULT LISTVIEW_LButtonDblClk(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
9986 LVHITTESTINFO htInfo;
9988 TRACE("(key=%hu, X=%u, Y=%u)\n", wKey, x, y);
9990 /* Cancel the item edition if any */
9991 if (infoPtr->itemEdit.fEnabled)
9993 KillTimer(infoPtr->hwndSelf, (UINT_PTR)&infoPtr->itemEdit);
9994 infoPtr->itemEdit.fEnabled = FALSE;
9997 /* send NM_RELEASEDCAPTURE notification */
9998 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
10003 /* send NM_DBLCLK notification */
10004 LISTVIEW_HitTest(infoPtr, &htInfo, TRUE, FALSE);
10005 if (!notify_click(infoPtr, NM_DBLCLK, &htInfo)) return 0;
10007 /* To send the LVN_ITEMACTIVATE, it must be on an Item */
10008 if(htInfo.iItem != -1) notify_itemactivate(infoPtr,&htInfo);
10015 * Processes mouse down messages (left mouse button).
10018 * infoPtr [I ] valid pointer to the listview structure
10019 * wKey [I ] key flag
10020 * x,y [I ] mouse coordinate
10025 static LRESULT LISTVIEW_LButtonDown(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
10027 LVHITTESTINFO lvHitTestInfo;
10028 static BOOL bGroupSelect = TRUE;
10029 POINT pt = { x, y };
10032 TRACE("(key=%hu, X=%u, Y=%u)\n", wKey, x, y);
10034 /* send NM_RELEASEDCAPTURE notification */
10035 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
10037 /* set left button down flag and record the click position */
10038 infoPtr->bLButtonDown = TRUE;
10039 infoPtr->ptClickPos = pt;
10040 infoPtr->bDragging = FALSE;
10041 infoPtr->bMarqueeSelect = FALSE;
10042 infoPtr->bScrolling = FALSE;
10044 lvHitTestInfo.pt.x = x;
10045 lvHitTestInfo.pt.y = y;
10047 nItem = LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, TRUE);
10048 TRACE("at %s, nItem=%d\n", wine_dbgstr_point(&pt), nItem);
10049 if ((nItem >= 0) && (nItem < infoPtr->nItemCount))
10051 if ((infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES) && (lvHitTestInfo.flags & LVHT_ONITEMSTATEICON))
10053 toggle_checkbox_state(infoPtr, nItem);
10057 if (infoPtr->dwStyle & LVS_SINGLESEL)
10059 if (LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED))
10060 infoPtr->nEditLabelItem = nItem;
10062 LISTVIEW_SetSelection(infoPtr, nItem);
10066 if ((wKey & MK_CONTROL) && (wKey & MK_SHIFT))
10070 if (!LISTVIEW_AddGroupSelection(infoPtr, nItem)) return 0;
10071 LISTVIEW_SetItemFocus(infoPtr, nItem);
10072 infoPtr->nSelectionMark = nItem;
10078 item.state = LVIS_SELECTED | LVIS_FOCUSED;
10079 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
10081 LISTVIEW_SetItemState(infoPtr,nItem,&item);
10082 infoPtr->nSelectionMark = nItem;
10085 else if (wKey & MK_CONTROL)
10089 bGroupSelect = (LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED) == 0);
10091 item.state = (bGroupSelect ? LVIS_SELECTED : 0) | LVIS_FOCUSED;
10092 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
10093 LISTVIEW_SetItemState(infoPtr, nItem, &item);
10094 infoPtr->nSelectionMark = nItem;
10096 else if (wKey & MK_SHIFT)
10098 LISTVIEW_SetGroupSelection(infoPtr, nItem);
10102 if (LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED))
10104 infoPtr->nEditLabelItem = nItem;
10105 infoPtr->nLButtonDownItem = nItem;
10107 LISTVIEW_SetItemFocus(infoPtr, nItem);
10110 /* set selection (clears other pre-existing selections) */
10111 LISTVIEW_SetSelection(infoPtr, nItem);
10115 if (infoPtr->dwLvExStyle & LVS_EX_ONECLICKACTIVATE)
10116 if(lvHitTestInfo.iItem != -1) notify_itemactivate(infoPtr,&lvHitTestInfo);
10120 if (!infoPtr->bFocus)
10121 SetFocus(infoPtr->hwndSelf);
10123 /* remove all selections */
10124 if (!(wKey & MK_CONTROL) && !(wKey & MK_SHIFT))
10125 LISTVIEW_DeselectAll(infoPtr);
10134 * Processes mouse up messages (left mouse button).
10137 * infoPtr [I ] valid pointer to the listview structure
10138 * wKey [I ] key flag
10139 * x,y [I ] mouse coordinate
10144 static LRESULT LISTVIEW_LButtonUp(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
10146 LVHITTESTINFO lvHitTestInfo;
10148 TRACE("(key=%hu, X=%u, Y=%u)\n", wKey, x, y);
10150 if (!infoPtr->bLButtonDown) return 0;
10152 lvHitTestInfo.pt.x = x;
10153 lvHitTestInfo.pt.y = y;
10155 /* send NM_CLICK notification */
10156 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
10157 if (!notify_click(infoPtr, NM_CLICK, &lvHitTestInfo)) return 0;
10159 /* set left button flag */
10160 infoPtr->bLButtonDown = FALSE;
10162 /* set a single selection, reset others */
10163 if(lvHitTestInfo.iItem == infoPtr->nLButtonDownItem && lvHitTestInfo.iItem != -1)
10164 LISTVIEW_SetSelection(infoPtr, infoPtr->nLButtonDownItem);
10165 infoPtr->nLButtonDownItem = -1;
10167 if (infoPtr->bDragging || infoPtr->bMarqueeSelect)
10169 /* Remove the marquee rectangle and release our mouse capture */
10170 if (infoPtr->bMarqueeSelect)
10172 LISTVIEW_InvalidateRect(infoPtr, &infoPtr->marqueeDrawRect);
10176 SetRect(&infoPtr->marqueeRect, 0, 0, 0, 0);
10177 SetRect(&infoPtr->marqueeDrawRect, 0, 0, 0, 0);
10179 infoPtr->bDragging = FALSE;
10180 infoPtr->bMarqueeSelect = FALSE;
10181 infoPtr->bScrolling = FALSE;
10183 KillTimer(infoPtr->hwndSelf, (UINT_PTR) infoPtr);
10187 /* if we clicked on a selected item, edit the label */
10188 if(lvHitTestInfo.iItem == infoPtr->nEditLabelItem && (lvHitTestInfo.flags & LVHT_ONITEMLABEL))
10190 /* we want to make sure the user doesn't want to do a double click. So we will
10191 * delay the edit. WM_LBUTTONDBLCLICK will cancel the timer
10193 infoPtr->itemEdit.fEnabled = TRUE;
10194 infoPtr->itemEdit.iItem = lvHitTestInfo.iItem;
10195 SetTimer(infoPtr->hwndSelf,
10196 (UINT_PTR)&infoPtr->itemEdit,
10197 GetDoubleClickTime(),
10198 LISTVIEW_DelayedEditItem);
10201 if (!infoPtr->bFocus)
10202 SetFocus(infoPtr->hwndSelf);
10209 * Destroys the listview control (called after WM_DESTROY).
10212 * [I] infoPtr : valid pointer to the listview structure
10217 static LRESULT LISTVIEW_NCDestroy(LISTVIEW_INFO *infoPtr)
10223 /* destroy data structure */
10224 DPA_Destroy(infoPtr->hdpaItems);
10225 DPA_Destroy(infoPtr->hdpaItemIds);
10226 DPA_Destroy(infoPtr->hdpaPosX);
10227 DPA_Destroy(infoPtr->hdpaPosY);
10229 for (i = 0; i < DPA_GetPtrCount(infoPtr->hdpaColumns); i++)
10230 Free(DPA_GetPtr(infoPtr->hdpaColumns, i));
10231 DPA_Destroy(infoPtr->hdpaColumns);
10232 ranges_destroy(infoPtr->selectionRanges);
10234 /* destroy image lists */
10235 if (!(infoPtr->dwStyle & LVS_SHAREIMAGELISTS))
10237 ImageList_Destroy(infoPtr->himlNormal);
10238 ImageList_Destroy(infoPtr->himlSmall);
10239 ImageList_Destroy(infoPtr->himlState);
10242 /* destroy font, bkgnd brush */
10243 infoPtr->hFont = 0;
10244 if (infoPtr->hDefaultFont) DeleteObject(infoPtr->hDefaultFont);
10245 if (infoPtr->clrBk != CLR_NONE) DeleteObject(infoPtr->hBkBrush);
10247 SetWindowLongPtrW(infoPtr->hwndSelf, 0, 0);
10249 /* free listview info pointer*/
10257 * Handles notifications.
10260 * [I] infoPtr : valid pointer to the listview structure
10261 * [I] lpnmhdr : notification information
10266 static LRESULT LISTVIEW_Notify(LISTVIEW_INFO *infoPtr, NMHDR *lpnmhdr)
10270 TRACE("(lpnmhdr=%p)\n", lpnmhdr);
10272 if (!lpnmhdr || lpnmhdr->hwndFrom != infoPtr->hwndHeader) return 0;
10274 /* remember: HDN_LAST < HDN_FIRST */
10275 if (lpnmhdr->code > HDN_FIRST || lpnmhdr->code < HDN_LAST) return 0;
10276 lpnmh = (NMHEADERW *)lpnmhdr;
10278 if (lpnmh->iItem < 0 || lpnmh->iItem >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return 0;
10280 switch (lpnmhdr->code)
10285 COLUMN_INFO *lpColumnInfo;
10289 if (!lpnmh->pitem || !(lpnmh->pitem->mask & HDI_WIDTH))
10292 /* remove the old line (if any) */
10293 LISTVIEW_DrawTrackLine(infoPtr);
10295 /* compute & draw the new line */
10296 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, lpnmh->iItem);
10297 x = lpColumnInfo->rcHeader.left + lpnmh->pitem->cxy;
10298 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
10299 infoPtr->xTrackLine = x + ptOrigin.x;
10300 LISTVIEW_DrawTrackLine(infoPtr);
10301 return notify_forward_header(infoPtr, lpnmh);
10304 case HDN_ENDTRACKA:
10305 case HDN_ENDTRACKW:
10306 /* remove the track line (if any) */
10307 LISTVIEW_DrawTrackLine(infoPtr);
10308 infoPtr->xTrackLine = -1;
10309 return notify_forward_header(infoPtr, lpnmh);
10311 case HDN_BEGINDRAG:
10312 if ((infoPtr->dwLvExStyle & LVS_EX_HEADERDRAGDROP) == 0) return 1;
10313 return notify_forward_header(infoPtr, lpnmh);
10316 infoPtr->colRectsDirty = TRUE;
10317 LISTVIEW_InvalidateList(infoPtr);
10318 return notify_forward_header(infoPtr, lpnmh);
10320 case HDN_ITEMCHANGEDW:
10321 case HDN_ITEMCHANGEDA:
10323 COLUMN_INFO *lpColumnInfo;
10327 if (!lpnmh->pitem || !(lpnmh->pitem->mask & HDI_WIDTH))
10329 hdi.mask = HDI_WIDTH;
10330 if (!SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, lpnmh->iItem, (LPARAM)&hdi)) return 0;
10334 cxy = lpnmh->pitem->cxy;
10336 /* determine how much we change since the last know position */
10337 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, lpnmh->iItem);
10338 dx = cxy - (lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left);
10341 lpColumnInfo->rcHeader.right += dx;
10343 hdi.mask = HDI_ORDER;
10344 SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, lpnmh->iItem, (LPARAM)&hdi);
10346 /* not the rightmost one */
10347 if (hdi.iOrder + 1 < DPA_GetPtrCount(infoPtr->hdpaColumns))
10349 INT nIndex = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX,
10350 hdi.iOrder + 1, 0);
10351 LISTVIEW_ScrollColumns(infoPtr, nIndex, dx);
10355 /* only needs to update the scrolls */
10356 infoPtr->nItemWidth += dx;
10357 LISTVIEW_UpdateScroll(infoPtr);
10359 LISTVIEW_UpdateItemSize(infoPtr);
10360 if (infoPtr->uView == LV_VIEW_DETAILS && is_redrawing(infoPtr))
10363 RECT rcCol = lpColumnInfo->rcHeader;
10365 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
10366 OffsetRect(&rcCol, ptOrigin.x, 0);
10368 rcCol.top = infoPtr->rcList.top;
10369 rcCol.bottom = infoPtr->rcList.bottom;
10371 /* resizing left-aligned columns leaves most of the left side untouched */
10372 if ((lpColumnInfo->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_LEFT)
10374 INT nMaxDirty = infoPtr->nEllipsisWidth + infoPtr->ntmMaxCharWidth;
10377 rcCol.left = max (rcCol.left, rcCol.right - nMaxDirty);
10380 /* when shrinking the last column clear the now unused field */
10381 if (hdi.iOrder == DPA_GetPtrCount(infoPtr->hdpaColumns) - 1)
10387 /* deal with right from rightmost column area */
10388 right.left = rcCol.right;
10389 right.top = rcCol.top;
10390 right.bottom = rcCol.bottom;
10391 right.right = infoPtr->rcList.right;
10393 LISTVIEW_InvalidateRect(infoPtr, &right);
10396 LISTVIEW_InvalidateRect(infoPtr, &rcCol);
10402 case HDN_ITEMCLICKW:
10403 case HDN_ITEMCLICKA:
10405 /* Handle sorting by Header Column */
10408 ZeroMemory(&nmlv, sizeof(NMLISTVIEW));
10410 nmlv.iSubItem = lpnmh->iItem;
10411 notify_listview(infoPtr, LVN_COLUMNCLICK, &nmlv);
10412 return notify_forward_header(infoPtr, lpnmh);
10415 case HDN_DIVIDERDBLCLICKW:
10416 case HDN_DIVIDERDBLCLICKA:
10417 /* FIXME: for LVS_EX_HEADERINALLVIEWS and not LV_VIEW_DETAILS
10418 we should use LVSCW_AUTOSIZE_USEHEADER, helper rework or
10419 split needed for that */
10420 LISTVIEW_SetColumnWidth(infoPtr, lpnmh->iItem, LVSCW_AUTOSIZE);
10421 return notify_forward_header(infoPtr, lpnmh);
10428 * Paint non-client area of control.
10431 * [I] infoPtr : valid pointer to the listview structureof the sender
10432 * [I] region : update region
10435 * TRUE - frame was painted
10436 * FALSE - call default window proc
10438 static BOOL LISTVIEW_NCPaint(const LISTVIEW_INFO *infoPtr, HRGN region)
10440 HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
10444 int cxEdge = GetSystemMetrics (SM_CXEDGE),
10445 cyEdge = GetSystemMetrics (SM_CYEDGE);
10448 return DefWindowProcW (infoPtr->hwndSelf, WM_NCPAINT, (WPARAM)region, 0);
10450 GetWindowRect(infoPtr->hwndSelf, &r);
10452 cliprgn = CreateRectRgn (r.left + cxEdge, r.top + cyEdge,
10453 r.right - cxEdge, r.bottom - cyEdge);
10454 if (region != (HRGN)1)
10455 CombineRgn (cliprgn, cliprgn, region, RGN_AND);
10456 OffsetRect(&r, -r.left, -r.top);
10458 dc = GetDCEx(infoPtr->hwndSelf, region, DCX_WINDOW|DCX_INTERSECTRGN);
10459 OffsetRect(&r, -r.left, -r.top);
10461 if (IsThemeBackgroundPartiallyTransparent (theme, 0, 0))
10462 DrawThemeParentBackground(infoPtr->hwndSelf, dc, &r);
10463 DrawThemeBackground (theme, dc, 0, 0, &r, 0);
10464 ReleaseDC(infoPtr->hwndSelf, dc);
10466 /* Call default proc to get the scrollbars etc. painted */
10467 DefWindowProcW (infoPtr->hwndSelf, WM_NCPAINT, (WPARAM)cliprgn, 0);
10474 * Determines the type of structure to use.
10477 * [I] infoPtr : valid pointer to the listview structureof the sender
10478 * [I] hwndFrom : listview window handle
10479 * [I] nCommand : command specifying the nature of the WM_NOTIFYFORMAT
10484 static LRESULT LISTVIEW_NotifyFormat(LISTVIEW_INFO *infoPtr, HWND hwndFrom, INT nCommand)
10486 TRACE("(hwndFrom=%p, nCommand=%d)\n", hwndFrom, nCommand);
10488 if (nCommand == NF_REQUERY)
10489 infoPtr->notifyFormat = SendMessageW(infoPtr->hwndNotify, WM_NOTIFYFORMAT, (WPARAM)infoPtr->hwndSelf, NF_QUERY);
10491 return infoPtr->notifyFormat;
10496 * Paints/Repaints the listview control. Internal use.
10499 * [I] infoPtr : valid pointer to the listview structure
10500 * [I] hdc : device context handle
10505 static LRESULT LISTVIEW_Paint(LISTVIEW_INFO *infoPtr, HDC hdc)
10507 TRACE("(hdc=%p)\n", hdc);
10509 if (infoPtr->bNoItemMetrics && infoPtr->nItemCount)
10511 infoPtr->bNoItemMetrics = FALSE;
10512 LISTVIEW_UpdateItemSize(infoPtr);
10513 if (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON)
10514 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
10515 LISTVIEW_UpdateScroll(infoPtr);
10518 if (infoPtr->hwndHeader) UpdateWindow(infoPtr->hwndHeader);
10521 LISTVIEW_Refresh(infoPtr, hdc, NULL);
10526 hdc = BeginPaint(infoPtr->hwndSelf, &ps);
10527 if (!hdc) return 1;
10528 LISTVIEW_Refresh(infoPtr, hdc, ps.fErase ? &ps.rcPaint : NULL);
10529 EndPaint(infoPtr->hwndSelf, &ps);
10537 * Paints/Repaints the listview control, WM_PAINT handler.
10540 * [I] infoPtr : valid pointer to the listview structure
10541 * [I] hdc : device context handle
10546 static inline LRESULT LISTVIEW_WMPaint(LISTVIEW_INFO *infoPtr, HDC hdc)
10548 TRACE("(hdc=%p)\n", hdc);
10550 if (!is_redrawing(infoPtr))
10551 return DefWindowProcW (infoPtr->hwndSelf, WM_PAINT, (WPARAM)hdc, 0);
10553 return LISTVIEW_Paint(infoPtr, hdc);
10558 * Paints/Repaints the listview control.
10561 * [I] infoPtr : valid pointer to the listview structure
10562 * [I] hdc : device context handle
10563 * [I] options : drawing options
10568 static LRESULT LISTVIEW_PrintClient(LISTVIEW_INFO *infoPtr, HDC hdc, DWORD options)
10570 FIXME("Partial Stub: (hdc=%p options=0x%08x)\n", hdc, options);
10572 if ((options & PRF_CHECKVISIBLE) && !IsWindowVisible(infoPtr->hwndSelf))
10575 if (options & PRF_ERASEBKGND)
10576 LISTVIEW_EraseBkgnd(infoPtr, hdc);
10578 if (options & PRF_CLIENT)
10579 LISTVIEW_Paint(infoPtr, hdc);
10587 * Processes double click messages (right mouse button).
10590 * [I] infoPtr : valid pointer to the listview structure
10591 * [I] wKey : key flag
10592 * [I] x,y : mouse coordinate
10597 static LRESULT LISTVIEW_RButtonDblClk(const LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
10599 LVHITTESTINFO lvHitTestInfo;
10601 TRACE("(key=%hu,X=%u,Y=%u)\n", wKey, x, y);
10603 /* send NM_RELEASEDCAPTURE notification */
10604 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
10606 /* send NM_RDBLCLK notification */
10607 lvHitTestInfo.pt.x = x;
10608 lvHitTestInfo.pt.y = y;
10609 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
10610 notify_click(infoPtr, NM_RDBLCLK, &lvHitTestInfo);
10617 * Processes mouse down messages (right mouse button).
10620 * [I] infoPtr : valid pointer to the listview structure
10621 * [I] wKey : key flag
10622 * [I] x,y : mouse coordinate
10627 static LRESULT LISTVIEW_RButtonDown(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
10629 LVHITTESTINFO lvHitTestInfo;
10632 TRACE("(key=%hu,X=%u,Y=%u)\n", wKey, x, y);
10634 /* send NM_RELEASEDCAPTURE notification */
10635 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
10637 /* make sure the listview control window has the focus */
10638 if (!infoPtr->bFocus) SetFocus(infoPtr->hwndSelf);
10640 /* set right button down flag */
10641 infoPtr->bRButtonDown = TRUE;
10643 /* determine the index of the selected item */
10644 lvHitTestInfo.pt.x = x;
10645 lvHitTestInfo.pt.y = y;
10646 nItem = LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, TRUE);
10648 if ((nItem >= 0) && (nItem < infoPtr->nItemCount))
10650 LISTVIEW_SetItemFocus(infoPtr, nItem);
10651 if (!((wKey & MK_SHIFT) || (wKey & MK_CONTROL)) &&
10652 !LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED))
10653 LISTVIEW_SetSelection(infoPtr, nItem);
10657 LISTVIEW_DeselectAll(infoPtr);
10665 * Processes mouse up messages (right mouse button).
10668 * [I] infoPtr : valid pointer to the listview structure
10669 * [I] wKey : key flag
10670 * [I] x,y : mouse coordinate
10675 static LRESULT LISTVIEW_RButtonUp(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
10677 LVHITTESTINFO lvHitTestInfo;
10680 TRACE("(key=%hu,X=%u,Y=%u)\n", wKey, x, y);
10682 if (!infoPtr->bRButtonDown) return 0;
10684 /* set button flag */
10685 infoPtr->bRButtonDown = FALSE;
10687 /* Send NM_RCLICK notification */
10688 lvHitTestInfo.pt.x = x;
10689 lvHitTestInfo.pt.y = y;
10690 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
10691 if (!notify_click(infoPtr, NM_RCLICK, &lvHitTestInfo)) return 0;
10693 /* Change to screen coordinate for WM_CONTEXTMENU */
10694 pt = lvHitTestInfo.pt;
10695 ClientToScreen(infoPtr->hwndSelf, &pt);
10697 /* Send a WM_CONTEXTMENU message in response to the RBUTTONUP */
10698 SendMessageW(infoPtr->hwndSelf, WM_CONTEXTMENU,
10699 (WPARAM)infoPtr->hwndSelf, MAKELPARAM(pt.x, pt.y));
10710 * [I] infoPtr : valid pointer to the listview structure
10711 * [I] hwnd : window handle of window containing the cursor
10712 * [I] nHittest : hit-test code
10713 * [I] wMouseMsg : ideintifier of the mouse message
10716 * TRUE if cursor is set
10719 static BOOL LISTVIEW_SetCursor(const LISTVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
10721 LVHITTESTINFO lvHitTestInfo;
10723 if (!LISTVIEW_IsHotTracking(infoPtr)) goto forward;
10725 if (!infoPtr->hHotCursor) goto forward;
10727 GetCursorPos(&lvHitTestInfo.pt);
10728 if (LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, FALSE, FALSE) < 0) goto forward;
10730 SetCursor(infoPtr->hHotCursor);
10736 return DefWindowProcW(infoPtr->hwndSelf, WM_SETCURSOR, wParam, lParam);
10744 * [I] infoPtr : valid pointer to the listview structure
10745 * [I] hwndLoseFocus : handle of previously focused window
10750 static LRESULT LISTVIEW_SetFocus(LISTVIEW_INFO *infoPtr, HWND hwndLoseFocus)
10752 TRACE("(hwndLoseFocus=%p)\n", hwndLoseFocus);
10754 /* if we have the focus already, there's nothing to do */
10755 if (infoPtr->bFocus) return 0;
10757 /* send NM_SETFOCUS notification */
10758 if (!notify(infoPtr, NM_SETFOCUS)) return 0;
10760 /* set window focus flag */
10761 infoPtr->bFocus = TRUE;
10763 /* put the focus rect back on */
10764 LISTVIEW_ShowFocusRect(infoPtr, TRUE);
10766 /* redraw all visible selected items */
10767 LISTVIEW_InvalidateSelectedItems(infoPtr);
10777 * [I] infoPtr : valid pointer to the listview structure
10778 * [I] fRedraw : font handle
10779 * [I] fRedraw : redraw flag
10784 static LRESULT LISTVIEW_SetFont(LISTVIEW_INFO *infoPtr, HFONT hFont, WORD fRedraw)
10786 HFONT oldFont = infoPtr->hFont;
10787 INT oldHeight = infoPtr->nItemHeight;
10789 TRACE("(hfont=%p,redraw=%hu)\n", hFont, fRedraw);
10791 infoPtr->hFont = hFont ? hFont : infoPtr->hDefaultFont;
10792 if (infoPtr->hFont == oldFont) return 0;
10794 LISTVIEW_SaveTextMetrics(infoPtr);
10796 infoPtr->nItemHeight = LISTVIEW_CalculateItemHeight(infoPtr);
10798 if (infoPtr->uView == LV_VIEW_DETAILS)
10800 SendMessageW(infoPtr->hwndHeader, WM_SETFONT, (WPARAM)hFont, MAKELPARAM(fRedraw, 0));
10801 LISTVIEW_UpdateSize(infoPtr);
10802 LISTVIEW_UpdateScroll(infoPtr);
10804 else if (infoPtr->nItemHeight != oldHeight)
10805 LISTVIEW_UpdateScroll(infoPtr);
10807 if (fRedraw) LISTVIEW_InvalidateList(infoPtr);
10814 * Message handling for WM_SETREDRAW.
10815 * For the Listview, it invalidates the entire window (the doc specifies otherwise)
10818 * [I] infoPtr : valid pointer to the listview structure
10819 * [I] bRedraw: state of redraw flag
10822 * DefWinProc return value
10824 static LRESULT LISTVIEW_SetRedraw(LISTVIEW_INFO *infoPtr, BOOL bRedraw)
10826 TRACE("infoPtr->bRedraw=%d, bRedraw=%d\n", infoPtr->bRedraw, bRedraw);
10828 /* we cannot use straight equality here because _any_ non-zero value is TRUE */
10829 if ((infoPtr->bRedraw && bRedraw) || (!infoPtr->bRedraw && !bRedraw)) return 0;
10831 infoPtr->bRedraw = bRedraw;
10833 if(!bRedraw) return 0;
10835 if (is_autoarrange(infoPtr))
10836 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
10837 LISTVIEW_UpdateScroll(infoPtr);
10839 /* despite what the WM_SETREDRAW docs says, apps expect us
10840 * to invalidate the listview here... stupid! */
10841 LISTVIEW_InvalidateList(infoPtr);
10848 * Resizes the listview control. This function processes WM_SIZE
10849 * messages. At this time, the width and height are not used.
10852 * [I] infoPtr : valid pointer to the listview structure
10853 * [I] Width : new width
10854 * [I] Height : new height
10859 static LRESULT LISTVIEW_Size(LISTVIEW_INFO *infoPtr, int Width, int Height)
10861 RECT rcOld = infoPtr->rcList;
10863 TRACE("(width=%d, height=%d)\n", Width, Height);
10865 LISTVIEW_UpdateSize(infoPtr);
10866 if (EqualRect(&rcOld, &infoPtr->rcList)) return 0;
10868 /* do not bother with display related stuff if we're not redrawing */
10869 if (!is_redrawing(infoPtr)) return 0;
10871 if (is_autoarrange(infoPtr))
10872 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
10874 LISTVIEW_UpdateScroll(infoPtr);
10876 /* refresh all only for lists whose height changed significantly */
10877 if ((infoPtr->uView == LV_VIEW_LIST) &&
10878 (rcOld.bottom - rcOld.top) / infoPtr->nItemHeight !=
10879 (infoPtr->rcList.bottom - infoPtr->rcList.top) / infoPtr->nItemHeight)
10880 LISTVIEW_InvalidateList(infoPtr);
10887 * Sets the size information.
10890 * [I] infoPtr : valid pointer to the listview structure
10895 static void LISTVIEW_UpdateSize(LISTVIEW_INFO *infoPtr)
10897 TRACE("uView=%d, rcList(old)=%s\n", infoPtr->uView, wine_dbgstr_rect(&infoPtr->rcList));
10899 GetClientRect(infoPtr->hwndSelf, &infoPtr->rcList);
10901 if (infoPtr->uView == LV_VIEW_LIST)
10903 /* Apparently the "LIST" style is supposed to have the same
10904 * number of items in a column even if there is no scroll bar.
10905 * Since if a scroll bar already exists then the bottom is already
10906 * reduced, only reduce if the scroll bar does not currently exist.
10907 * The "2" is there to mimic the native control. I think it may be
10908 * related to either padding or edges. (GLA 7/2002)
10910 if (!(GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE) & WS_HSCROLL))
10911 infoPtr->rcList.bottom -= GetSystemMetrics(SM_CYHSCROLL);
10912 infoPtr->rcList.bottom = max (infoPtr->rcList.bottom - 2, 0);
10915 /* if control created invisible header isn't created */
10916 if (infoPtr->hwndHeader)
10921 hl.prc = &infoPtr->rcList;
10923 SendMessageW( infoPtr->hwndHeader, HDM_LAYOUT, 0, (LPARAM)&hl );
10924 TRACE(" wp.flags=0x%08x, wp=%d,%d (%dx%d)\n", wp.flags, wp.x, wp.y, wp.cx, wp.cy);
10926 if (LISTVIEW_IsHeaderEnabled(infoPtr))
10927 wp.flags |= SWP_SHOWWINDOW;
10930 wp.flags |= SWP_HIDEWINDOW;
10934 SetWindowPos(wp.hwnd, wp.hwndInsertAfter, wp.x, wp.y, wp.cx, wp.cy, wp.flags);
10935 TRACE(" after SWP wp=%d,%d (%dx%d)\n", wp.x, wp.y, wp.cx, wp.cy);
10937 infoPtr->rcList.top = max(wp.cy, 0);
10939 /* extra padding for grid */
10940 if (infoPtr->uView == LV_VIEW_DETAILS && infoPtr->dwLvExStyle & LVS_EX_GRIDLINES)
10941 infoPtr->rcList.top += 2;
10943 TRACE(" rcList=%s\n", wine_dbgstr_rect(&infoPtr->rcList));
10948 * Processes WM_STYLECHANGED messages.
10951 * [I] infoPtr : valid pointer to the listview structure
10952 * [I] wStyleType : window style type (normal or extended)
10953 * [I] lpss : window style information
10958 static INT LISTVIEW_StyleChanged(LISTVIEW_INFO *infoPtr, WPARAM wStyleType,
10959 const STYLESTRUCT *lpss)
10961 UINT uNewView = lpss->styleNew & LVS_TYPEMASK;
10962 UINT uOldView = lpss->styleOld & LVS_TYPEMASK;
10965 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
10966 wStyleType, lpss->styleOld, lpss->styleNew);
10968 if (wStyleType != GWL_STYLE) return 0;
10970 infoPtr->dwStyle = lpss->styleNew;
10971 map_style_view(infoPtr);
10973 if (((lpss->styleOld & WS_HSCROLL) != 0)&&
10974 ((lpss->styleNew & WS_HSCROLL) == 0))
10975 ShowScrollBar(infoPtr->hwndSelf, SB_HORZ, FALSE);
10977 if (((lpss->styleOld & WS_VSCROLL) != 0)&&
10978 ((lpss->styleNew & WS_VSCROLL) == 0))
10979 ShowScrollBar(infoPtr->hwndSelf, SB_VERT, FALSE);
10981 if (uNewView != uOldView)
10983 SIZE oldIconSize = infoPtr->iconSize;
10986 SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
10987 ShowWindow(infoPtr->hwndHeader, SW_HIDE);
10989 ShowScrollBar(infoPtr->hwndSelf, SB_BOTH, FALSE);
10990 SetRectEmpty(&infoPtr->rcFocus);
10992 himl = (uNewView == LVS_ICON ? infoPtr->himlNormal : infoPtr->himlSmall);
10993 set_icon_size(&infoPtr->iconSize, himl, uNewView != LVS_ICON);
10995 if (uNewView == LVS_ICON)
10997 if ((infoPtr->iconSize.cx != oldIconSize.cx) || (infoPtr->iconSize.cy != oldIconSize.cy))
10999 TRACE("icon old size=(%d,%d), new size=(%d,%d)\n",
11000 oldIconSize.cx, oldIconSize.cy, infoPtr->iconSize.cx, infoPtr->iconSize.cy);
11001 LISTVIEW_SetIconSpacing(infoPtr, 0, 0);
11004 else if (uNewView == LVS_REPORT)
11009 LISTVIEW_CreateHeader( infoPtr );
11011 hl.prc = &infoPtr->rcList;
11013 SendMessageW( infoPtr->hwndHeader, HDM_LAYOUT, 0, (LPARAM)&hl );
11014 SetWindowPos(infoPtr->hwndHeader, infoPtr->hwndSelf, wp.x, wp.y, wp.cx, wp.cy,
11015 wp.flags | ((infoPtr->dwStyle & LVS_NOCOLUMNHEADER)
11016 ? SWP_HIDEWINDOW : SWP_SHOWWINDOW));
11019 LISTVIEW_UpdateItemSize(infoPtr);
11022 if (uNewView == LVS_REPORT || infoPtr->dwLvExStyle & LVS_EX_HEADERINALLVIEWS)
11024 if ((lpss->styleOld ^ lpss->styleNew) & LVS_NOCOLUMNHEADER)
11026 if (lpss->styleNew & LVS_NOCOLUMNHEADER)
11028 /* Turn off the header control */
11029 style = GetWindowLongW(infoPtr->hwndHeader, GWL_STYLE);
11030 TRACE("Hide header control, was 0x%08x\n", style);
11031 SetWindowLongW(infoPtr->hwndHeader, GWL_STYLE, style | HDS_HIDDEN);
11033 /* Turn on the header control */
11034 if ((style = GetWindowLongW(infoPtr->hwndHeader, GWL_STYLE)) & HDS_HIDDEN)
11036 TRACE("Show header control, was 0x%08x\n", style);
11037 SetWindowLongW(infoPtr->hwndHeader, GWL_STYLE, (style & ~HDS_HIDDEN) | WS_VISIBLE);
11043 if ( (uNewView == LVS_ICON || uNewView == LVS_SMALLICON) &&
11044 (uNewView != uOldView || ((lpss->styleNew ^ lpss->styleOld) & LVS_ALIGNMASK)) )
11045 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
11047 /* update the size of the client area */
11048 LISTVIEW_UpdateSize(infoPtr);
11050 /* add scrollbars if needed */
11051 LISTVIEW_UpdateScroll(infoPtr);
11053 /* invalidate client area + erase background */
11054 LISTVIEW_InvalidateList(infoPtr);
11061 * Processes WM_STYLECHANGING messages.
11064 * [I] wStyleType : window style type (normal or extended)
11065 * [I0] lpss : window style information
11070 static INT LISTVIEW_StyleChanging(WPARAM wStyleType,
11073 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
11074 wStyleType, lpss->styleOld, lpss->styleNew);
11076 /* don't forward LVS_OWNERDATA only if not already set to */
11077 if ((lpss->styleNew ^ lpss->styleOld) & LVS_OWNERDATA)
11079 if (lpss->styleOld & LVS_OWNERDATA)
11080 lpss->styleNew |= LVS_OWNERDATA;
11082 lpss->styleNew &= ~LVS_OWNERDATA;
11090 * Processes WM_SHOWWINDOW messages.
11093 * [I] infoPtr : valid pointer to the listview structure
11094 * [I] bShown : window is being shown (FALSE when hidden)
11095 * [I] iStatus : window show status
11100 static LRESULT LISTVIEW_ShowWindow(LISTVIEW_INFO *infoPtr, WPARAM bShown, LPARAM iStatus)
11102 /* header delayed creation */
11103 if ((infoPtr->uView == LV_VIEW_DETAILS) && bShown)
11105 LISTVIEW_CreateHeader(infoPtr);
11107 if (!(LVS_NOCOLUMNHEADER & infoPtr->dwStyle))
11108 ShowWindow(infoPtr->hwndHeader, SW_SHOWNORMAL);
11111 return DefWindowProcW(infoPtr->hwndSelf, WM_SHOWWINDOW, bShown, iStatus);
11116 * Processes CCM_GETVERSION messages.
11119 * [I] infoPtr : valid pointer to the listview structure
11124 static inline LRESULT LISTVIEW_GetVersion(const LISTVIEW_INFO *infoPtr)
11126 return infoPtr->iVersion;
11131 * Processes CCM_SETVERSION messages.
11134 * [I] infoPtr : valid pointer to the listview structure
11135 * [I] iVersion : version to be set
11138 * -1 when requested version is greater than DLL version;
11139 * previous version otherwise
11141 static LRESULT LISTVIEW_SetVersion(LISTVIEW_INFO *infoPtr, DWORD iVersion)
11143 INT iOldVersion = infoPtr->iVersion;
11145 if (iVersion > COMCTL32_VERSION)
11148 infoPtr->iVersion = iVersion;
11150 TRACE("new version %d\n", iVersion);
11152 return iOldVersion;
11157 * Window procedure of the listview control.
11160 static LRESULT WINAPI
11161 LISTVIEW_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
11163 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongPtrW(hwnd, 0);
11165 TRACE("(hwnd=%p uMsg=%x wParam=%lx lParam=%lx)\n", hwnd, uMsg, wParam, lParam);
11167 if (!infoPtr && (uMsg != WM_NCCREATE))
11168 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
11172 case LVM_APPROXIMATEVIEWRECT:
11173 return LISTVIEW_ApproximateViewRect(infoPtr, (INT)wParam,
11174 LOWORD(lParam), HIWORD(lParam));
11176 return LISTVIEW_Arrange(infoPtr, (INT)wParam);
11178 case LVM_CANCELEDITLABEL:
11179 return LISTVIEW_CancelEditLabel(infoPtr);
11181 case LVM_CREATEDRAGIMAGE:
11182 return (LRESULT)LISTVIEW_CreateDragImage(infoPtr, (INT)wParam, (LPPOINT)lParam);
11184 case LVM_DELETEALLITEMS:
11185 return LISTVIEW_DeleteAllItems(infoPtr, FALSE);
11187 case LVM_DELETECOLUMN:
11188 return LISTVIEW_DeleteColumn(infoPtr, (INT)wParam);
11190 case LVM_DELETEITEM:
11191 return LISTVIEW_DeleteItem(infoPtr, (INT)wParam);
11193 case LVM_EDITLABELA:
11194 case LVM_EDITLABELW:
11195 return (LRESULT)LISTVIEW_EditLabelT(infoPtr, (INT)wParam,
11196 uMsg == LVM_EDITLABELW);
11197 /* case LVM_ENABLEGROUPVIEW: */
11199 case LVM_ENSUREVISIBLE:
11200 return LISTVIEW_EnsureVisible(infoPtr, (INT)wParam, (BOOL)lParam);
11202 case LVM_FINDITEMW:
11203 return LISTVIEW_FindItemW(infoPtr, (INT)wParam, (LPLVFINDINFOW)lParam);
11205 case LVM_FINDITEMA:
11206 return LISTVIEW_FindItemA(infoPtr, (INT)wParam, (LPLVFINDINFOA)lParam);
11208 case LVM_GETBKCOLOR:
11209 return infoPtr->clrBk;
11211 /* case LVM_GETBKIMAGE: */
11213 case LVM_GETCALLBACKMASK:
11214 return infoPtr->uCallbackMask;
11216 case LVM_GETCOLUMNA:
11217 case LVM_GETCOLUMNW:
11218 return LISTVIEW_GetColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam,
11219 uMsg == LVM_GETCOLUMNW);
11221 case LVM_GETCOLUMNORDERARRAY:
11222 return LISTVIEW_GetColumnOrderArray(infoPtr, (INT)wParam, (LPINT)lParam);
11224 case LVM_GETCOLUMNWIDTH:
11225 return LISTVIEW_GetColumnWidth(infoPtr, (INT)wParam);
11227 case LVM_GETCOUNTPERPAGE:
11228 return LISTVIEW_GetCountPerPage(infoPtr);
11230 case LVM_GETEDITCONTROL:
11231 return (LRESULT)infoPtr->hwndEdit;
11233 case LVM_GETEXTENDEDLISTVIEWSTYLE:
11234 return infoPtr->dwLvExStyle;
11236 /* case LVM_GETGROUPINFO: */
11238 /* case LVM_GETGROUPMETRICS: */
11240 case LVM_GETHEADER:
11241 return (LRESULT)infoPtr->hwndHeader;
11243 case LVM_GETHOTCURSOR:
11244 return (LRESULT)infoPtr->hHotCursor;
11246 case LVM_GETHOTITEM:
11247 return infoPtr->nHotItem;
11249 case LVM_GETHOVERTIME:
11250 return infoPtr->dwHoverTime;
11252 case LVM_GETIMAGELIST:
11253 return (LRESULT)LISTVIEW_GetImageList(infoPtr, (INT)wParam);
11255 /* case LVM_GETINSERTMARK: */
11257 /* case LVM_GETINSERTMARKCOLOR: */
11259 /* case LVM_GETINSERTMARKRECT: */
11261 case LVM_GETISEARCHSTRINGA:
11262 case LVM_GETISEARCHSTRINGW:
11263 FIXME("LVM_GETISEARCHSTRING: unimplemented\n");
11268 return LISTVIEW_GetItemExtT(infoPtr, (LPLVITEMW)lParam, uMsg == LVM_GETITEMW);
11270 case LVM_GETITEMCOUNT:
11271 return infoPtr->nItemCount;
11273 case LVM_GETITEMPOSITION:
11274 return LISTVIEW_GetItemPosition(infoPtr, (INT)wParam, (LPPOINT)lParam);
11276 case LVM_GETITEMRECT:
11277 return LISTVIEW_GetItemRect(infoPtr, (INT)wParam, (LPRECT)lParam);
11279 case LVM_GETITEMSPACING:
11280 return LISTVIEW_GetItemSpacing(infoPtr, (BOOL)wParam);
11282 case LVM_GETITEMSTATE:
11283 return LISTVIEW_GetItemState(infoPtr, (INT)wParam, (UINT)lParam);
11285 case LVM_GETITEMTEXTA:
11286 case LVM_GETITEMTEXTW:
11287 return LISTVIEW_GetItemTextT(infoPtr, (INT)wParam, (LPLVITEMW)lParam,
11288 uMsg == LVM_GETITEMTEXTW);
11290 case LVM_GETNEXTITEM:
11291 return LISTVIEW_GetNextItem(infoPtr, (INT)wParam, LOWORD(lParam));
11293 case LVM_GETNUMBEROFWORKAREAS:
11294 FIXME("LVM_GETNUMBEROFWORKAREAS: unimplemented\n");
11297 case LVM_GETORIGIN:
11298 if (!lParam) return FALSE;
11299 if (infoPtr->uView == LV_VIEW_DETAILS ||
11300 infoPtr->uView == LV_VIEW_LIST) return FALSE;
11301 LISTVIEW_GetOrigin(infoPtr, (LPPOINT)lParam);
11304 /* case LVM_GETOUTLINECOLOR: */
11306 /* case LVM_GETSELECTEDCOLUMN: */
11308 case LVM_GETSELECTEDCOUNT:
11309 return LISTVIEW_GetSelectedCount(infoPtr);
11311 case LVM_GETSELECTIONMARK:
11312 return infoPtr->nSelectionMark;
11314 case LVM_GETSTRINGWIDTHA:
11315 case LVM_GETSTRINGWIDTHW:
11316 return LISTVIEW_GetStringWidthT(infoPtr, (LPCWSTR)lParam,
11317 uMsg == LVM_GETSTRINGWIDTHW);
11319 case LVM_GETSUBITEMRECT:
11320 return LISTVIEW_GetSubItemRect(infoPtr, (UINT)wParam, (LPRECT)lParam);
11322 case LVM_GETTEXTBKCOLOR:
11323 return infoPtr->clrTextBk;
11325 case LVM_GETTEXTCOLOR:
11326 return infoPtr->clrText;
11328 /* case LVM_GETTILEINFO: */
11330 /* case LVM_GETTILEVIEWINFO: */
11332 case LVM_GETTOOLTIPS:
11333 if( !infoPtr->hwndToolTip )
11334 infoPtr->hwndToolTip = COMCTL32_CreateToolTip( hwnd );
11335 return (LRESULT)infoPtr->hwndToolTip;
11337 case LVM_GETTOPINDEX:
11338 return LISTVIEW_GetTopIndex(infoPtr);
11340 case LVM_GETUNICODEFORMAT:
11341 return (infoPtr->notifyFormat == NFR_UNICODE);
11344 return infoPtr->uView;
11346 case LVM_GETVIEWRECT:
11347 return LISTVIEW_GetViewRect(infoPtr, (LPRECT)lParam);
11349 case LVM_GETWORKAREAS:
11350 FIXME("LVM_GETWORKAREAS: unimplemented\n");
11353 /* case LVM_HASGROUP: */
11356 return LISTVIEW_HitTest(infoPtr, (LPLVHITTESTINFO)lParam, FALSE, TRUE);
11358 case LVM_INSERTCOLUMNA:
11359 case LVM_INSERTCOLUMNW:
11360 return LISTVIEW_InsertColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam,
11361 uMsg == LVM_INSERTCOLUMNW);
11363 /* case LVM_INSERTGROUP: */
11365 /* case LVM_INSERTGROUPSORTED: */
11367 case LVM_INSERTITEMA:
11368 case LVM_INSERTITEMW:
11369 return LISTVIEW_InsertItemT(infoPtr, (LPLVITEMW)lParam, uMsg == LVM_INSERTITEMW);
11371 /* case LVM_INSERTMARKHITTEST: */
11373 /* case LVM_ISGROUPVIEWENABLED: */
11375 case LVM_ISITEMVISIBLE:
11376 return LISTVIEW_IsItemVisible(infoPtr, (INT)wParam);
11378 case LVM_MAPIDTOINDEX:
11379 return LISTVIEW_MapIdToIndex(infoPtr, (UINT)wParam);
11381 case LVM_MAPINDEXTOID:
11382 return LISTVIEW_MapIndexToId(infoPtr, (INT)wParam);
11384 /* case LVM_MOVEGROUP: */
11386 /* case LVM_MOVEITEMTOGROUP: */
11388 case LVM_REDRAWITEMS:
11389 return LISTVIEW_RedrawItems(infoPtr, (INT)wParam, (INT)lParam);
11391 /* case LVM_REMOVEALLGROUPS: */
11393 /* case LVM_REMOVEGROUP: */
11396 return LISTVIEW_Scroll(infoPtr, (INT)wParam, (INT)lParam);
11398 case LVM_SETBKCOLOR:
11399 return LISTVIEW_SetBkColor(infoPtr, (COLORREF)lParam);
11401 /* case LVM_SETBKIMAGE: */
11403 case LVM_SETCALLBACKMASK:
11404 infoPtr->uCallbackMask = (UINT)wParam;
11407 case LVM_SETCOLUMNA:
11408 case LVM_SETCOLUMNW:
11409 return LISTVIEW_SetColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam,
11410 uMsg == LVM_SETCOLUMNW);
11412 case LVM_SETCOLUMNORDERARRAY:
11413 return LISTVIEW_SetColumnOrderArray(infoPtr, (INT)wParam, (LPINT)lParam);
11415 case LVM_SETCOLUMNWIDTH:
11416 return LISTVIEW_SetColumnWidth(infoPtr, (INT)wParam, (short)LOWORD(lParam));
11418 case LVM_SETEXTENDEDLISTVIEWSTYLE:
11419 return LISTVIEW_SetExtendedListViewStyle(infoPtr, (DWORD)wParam, (DWORD)lParam);
11421 /* case LVM_SETGROUPINFO: */
11423 /* case LVM_SETGROUPMETRICS: */
11425 case LVM_SETHOTCURSOR:
11426 return (LRESULT)LISTVIEW_SetHotCursor(infoPtr, (HCURSOR)lParam);
11428 case LVM_SETHOTITEM:
11429 return LISTVIEW_SetHotItem(infoPtr, (INT)wParam);
11431 case LVM_SETHOVERTIME:
11432 return LISTVIEW_SetHoverTime(infoPtr, (DWORD)lParam);
11434 case LVM_SETICONSPACING:
11435 return LISTVIEW_SetIconSpacing(infoPtr, (short)LOWORD(lParam), (short)HIWORD(lParam));
11437 case LVM_SETIMAGELIST:
11438 return (LRESULT)LISTVIEW_SetImageList(infoPtr, (INT)wParam, (HIMAGELIST)lParam);
11440 /* case LVM_SETINFOTIP: */
11442 /* case LVM_SETINSERTMARK: */
11444 /* case LVM_SETINSERTMARKCOLOR: */
11449 if (infoPtr->dwStyle & LVS_OWNERDATA) return FALSE;
11450 return LISTVIEW_SetItemT(infoPtr, (LPLVITEMW)lParam, (uMsg == LVM_SETITEMW));
11453 case LVM_SETITEMCOUNT:
11454 return LISTVIEW_SetItemCount(infoPtr, (INT)wParam, (DWORD)lParam);
11456 case LVM_SETITEMPOSITION:
11459 pt.x = (short)LOWORD(lParam);
11460 pt.y = (short)HIWORD(lParam);
11461 return LISTVIEW_SetItemPosition(infoPtr, (INT)wParam, &pt);
11464 case LVM_SETITEMPOSITION32:
11465 return LISTVIEW_SetItemPosition(infoPtr, (INT)wParam, (POINT*)lParam);
11467 case LVM_SETITEMSTATE:
11468 return LISTVIEW_SetItemState(infoPtr, (INT)wParam, (LPLVITEMW)lParam);
11470 case LVM_SETITEMTEXTA:
11471 case LVM_SETITEMTEXTW:
11472 return LISTVIEW_SetItemTextT(infoPtr, (INT)wParam, (LPLVITEMW)lParam,
11473 uMsg == LVM_SETITEMTEXTW);
11475 /* case LVM_SETOUTLINECOLOR: */
11477 /* case LVM_SETSELECTEDCOLUMN: */
11479 case LVM_SETSELECTIONMARK:
11480 return LISTVIEW_SetSelectionMark(infoPtr, (INT)lParam);
11482 case LVM_SETTEXTBKCOLOR:
11483 return LISTVIEW_SetTextBkColor(infoPtr, (COLORREF)lParam);
11485 case LVM_SETTEXTCOLOR:
11486 return LISTVIEW_SetTextColor(infoPtr, (COLORREF)lParam);
11488 /* case LVM_SETTILEINFO: */
11490 /* case LVM_SETTILEVIEWINFO: */
11492 /* case LVM_SETTILEWIDTH: */
11494 case LVM_SETTOOLTIPS:
11495 return (LRESULT)LISTVIEW_SetToolTips(infoPtr, (HWND)lParam);
11497 case LVM_SETUNICODEFORMAT:
11498 return LISTVIEW_SetUnicodeFormat(infoPtr, wParam);
11501 return LISTVIEW_SetView(infoPtr, wParam);
11503 /* case LVM_SETWORKAREAS: */
11505 /* case LVM_SORTGROUPS: */
11507 case LVM_SORTITEMS:
11508 case LVM_SORTITEMSEX:
11509 return LISTVIEW_SortItems(infoPtr, (PFNLVCOMPARE)lParam, wParam,
11510 uMsg == LVM_SORTITEMSEX);
11511 case LVM_SUBITEMHITTEST:
11512 return LISTVIEW_HitTest(infoPtr, (LPLVHITTESTINFO)lParam, TRUE, FALSE);
11515 return LISTVIEW_Update(infoPtr, (INT)wParam);
11517 case CCM_GETVERSION:
11518 return LISTVIEW_GetVersion(infoPtr);
11520 case CCM_SETVERSION:
11521 return LISTVIEW_SetVersion(infoPtr, wParam);
11524 return LISTVIEW_ProcessLetterKeys( infoPtr, wParam, lParam );
11527 return LISTVIEW_Command(infoPtr, wParam, lParam);
11530 return LISTVIEW_NCCreate(hwnd, (LPCREATESTRUCTW)lParam);
11533 return LISTVIEW_Create(hwnd, (LPCREATESTRUCTW)lParam);
11536 return LISTVIEW_Destroy(infoPtr);
11539 return LISTVIEW_Enable(infoPtr);
11541 case WM_ERASEBKGND:
11542 return LISTVIEW_EraseBkgnd(infoPtr, (HDC)wParam);
11544 case WM_GETDLGCODE:
11545 return DLGC_WANTCHARS | DLGC_WANTARROWS;
11548 return (LRESULT)infoPtr->hFont;
11551 return LISTVIEW_HScroll(infoPtr, (INT)LOWORD(wParam), 0);
11554 return LISTVIEW_KeyDown(infoPtr, (INT)wParam, (LONG)lParam);
11557 return LISTVIEW_KillFocus(infoPtr);
11559 case WM_LBUTTONDBLCLK:
11560 return LISTVIEW_LButtonDblClk(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11562 case WM_LBUTTONDOWN:
11563 return LISTVIEW_LButtonDown(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11566 return LISTVIEW_LButtonUp(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11569 return LISTVIEW_MouseMove (infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11571 case WM_MOUSEHOVER:
11572 return LISTVIEW_MouseHover(infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11575 return LISTVIEW_NCDestroy(infoPtr);
11578 return LISTVIEW_NCPaint(infoPtr, (HRGN)wParam);
11581 return LISTVIEW_Notify(infoPtr, (LPNMHDR)lParam);
11583 case WM_NOTIFYFORMAT:
11584 return LISTVIEW_NotifyFormat(infoPtr, (HWND)wParam, (INT)lParam);
11586 case WM_PRINTCLIENT:
11587 return LISTVIEW_PrintClient(infoPtr, (HDC)wParam, (DWORD)lParam);
11590 return LISTVIEW_WMPaint(infoPtr, (HDC)wParam);
11592 case WM_RBUTTONDBLCLK:
11593 return LISTVIEW_RButtonDblClk(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11595 case WM_RBUTTONDOWN:
11596 return LISTVIEW_RButtonDown(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11599 return LISTVIEW_RButtonUp(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11602 return LISTVIEW_SetCursor(infoPtr, wParam, lParam);
11605 return LISTVIEW_SetFocus(infoPtr, (HWND)wParam);
11608 return LISTVIEW_SetFont(infoPtr, (HFONT)wParam, (WORD)lParam);
11611 return LISTVIEW_SetRedraw(infoPtr, (BOOL)wParam);
11613 case WM_SHOWWINDOW:
11614 return LISTVIEW_ShowWindow(infoPtr, wParam, lParam);
11616 case WM_STYLECHANGED:
11617 return LISTVIEW_StyleChanged(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
11619 case WM_STYLECHANGING:
11620 return LISTVIEW_StyleChanging(wParam, (LPSTYLESTRUCT)lParam);
11622 case WM_SYSCOLORCHANGE:
11623 COMCTL32_RefreshSysColors();
11626 /* case WM_TIMER: */
11627 case WM_THEMECHANGED:
11628 return LISTVIEW_ThemeChanged(infoPtr);
11631 return LISTVIEW_VScroll(infoPtr, (INT)LOWORD(wParam), 0);
11633 case WM_MOUSEWHEEL:
11634 if (wParam & (MK_SHIFT | MK_CONTROL))
11635 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
11636 return LISTVIEW_MouseWheel(infoPtr, (short int)HIWORD(wParam));
11638 case WM_WINDOWPOSCHANGED:
11639 if (!(((WINDOWPOS *)lParam)->flags & SWP_NOSIZE))
11641 SetWindowPos(infoPtr->hwndSelf, 0, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOACTIVATE |
11642 SWP_NOZORDER | SWP_NOMOVE | SWP_NOSIZE);
11644 if ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (infoPtr->uView == LV_VIEW_DETAILS))
11646 if (notify_measureitem(infoPtr)) LISTVIEW_InvalidateList(infoPtr);
11648 LISTVIEW_Size(infoPtr, ((WINDOWPOS *)lParam)->cx, ((WINDOWPOS *)lParam)->cy);
11650 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
11652 /* case WM_WININICHANGE: */
11655 if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
11656 ERR("unknown msg %04x wp=%08lx lp=%08lx\n", uMsg, wParam, lParam);
11658 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
11665 * Registers the window class.
11673 void LISTVIEW_Register(void)
11675 WNDCLASSW wndClass;
11677 ZeroMemory(&wndClass, sizeof(WNDCLASSW));
11678 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS;
11679 wndClass.lpfnWndProc = LISTVIEW_WindowProc;
11680 wndClass.cbClsExtra = 0;
11681 wndClass.cbWndExtra = sizeof(LISTVIEW_INFO *);
11682 wndClass.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
11683 wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
11684 wndClass.lpszClassName = WC_LISTVIEWW;
11685 RegisterClassW(&wndClass);
11690 * Unregisters the window class.
11698 void LISTVIEW_Unregister(void)
11700 UnregisterClassW(WC_LISTVIEWW, NULL);
11705 * Handle any WM_COMMAND messages
11708 * [I] infoPtr : valid pointer to the listview structure
11709 * [I] wParam : the first message parameter
11710 * [I] lParam : the second message parameter
11715 static LRESULT LISTVIEW_Command(LISTVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
11718 TRACE("(%p %x %x %lx)\n", infoPtr, HIWORD(wParam), LOWORD(wParam), lParam);
11720 if (!infoPtr->hwndEdit) return 0;
11722 switch (HIWORD(wParam))
11727 * Adjust the edit window size
11729 WCHAR buffer[1024];
11730 HDC hdc = GetDC(infoPtr->hwndEdit);
11731 HFONT hFont, hOldFont = 0;
11735 if (!infoPtr->hwndEdit || !hdc) return 0;
11736 GetWindowTextW(infoPtr->hwndEdit, buffer, sizeof(buffer)/sizeof(buffer[0]));
11737 GetWindowRect(infoPtr->hwndEdit, &rect);
11739 /* Select font to get the right dimension of the string */
11740 hFont = (HFONT)SendMessageW(infoPtr->hwndEdit, WM_GETFONT, 0, 0);
11743 hOldFont = SelectObject(hdc, hFont);
11746 if (GetTextExtentPoint32W(hdc, buffer, lstrlenW(buffer), &sz))
11748 TEXTMETRICW textMetric;
11750 /* Add Extra spacing for the next character */
11751 GetTextMetricsW(hdc, &textMetric);
11752 sz.cx += (textMetric.tmMaxCharWidth * 2);
11754 SetWindowPos(infoPtr->hwndEdit, NULL, 0, 0, sz.cx,
11755 rect.bottom - rect.top, SWP_DRAWFRAME | SWP_NOMOVE | SWP_NOZORDER);
11758 SelectObject(hdc, hOldFont);
11760 ReleaseDC(infoPtr->hwndEdit, hdc);
11766 LISTVIEW_CancelEditLabel(infoPtr);
11771 return SendMessageW (infoPtr->hwndNotify, WM_COMMAND, wParam, lParam);