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
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 * This code was audited for completeness against the documented features
27 * of Comctl32.dll version 6.0 on May. 20, 2005, by James Hawkins.
29 * Unless otherwise noted, we believe this code to be complete, as per
30 * the specification mentioned above.
31 * If you discover missing features, or bugs, please note them below.
35 * Default Message Processing
36 * -- EN_KILLFOCUS should be handled in WM_COMMAND
37 * -- WM_CREATE: create the icon and small icon image lists at this point only if
38 * the LVS_SHAREIMAGELISTS style is not specified.
39 * -- WM_WINDOWPOSCHANGED: arrange the list items if the current view is icon
40 * or small icon and the LVS_AUTOARRANGE style is specified.
45 * -- Hot item handling, mouse hovering
46 * -- Workareas support
51 * -- Expand large item in ICON mode when the cursor is flying over the icon or text.
52 * -- Support CustomDraw options for _WIN32_IE >= 0x560 (see NMLVCUSTOMDRAW docs).
53 * -- LVA_SNAPTOGRID not implemented
54 * -- LISTVIEW_ApproximateViewRect partially implemented
55 * -- LISTVIEW_[GS]etColumnOrderArray stubs
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)
83 * -- LVS_NOSCROLL (see Q137520)
84 * -- LVS_SORTASCENDING, LVS_SORTDESCENDING
86 * -- LVS_TYPESTYLEMASK
89 * -- LVS_EX_BORDERSELECT
91 * -- LVS_EX_HEADERDRAGDROP
94 * -- LVS_EX_MULTIWORKAREAS
96 * -- LVS_EX_SIMPLESELECT
97 * -- LVS_EX_TWOCLICKACTIVATE
98 * -- LVS_EX_UNDERLINECOLD
99 * -- LVS_EX_UNDERLINEHOT
102 * -- LVN_BEGINSCROLL, LVN_ENDSCROLL
105 * -- LVN_MARQUEEBEGIN
111 * -- LVM_CANCELEDITLABEL
112 * -- LVM_ENABLEGROUPVIEW
113 * -- LVM_GETBKIMAGE, LVM_SETBKIMAGE
114 * -- LVM_GETGROUPINFO, LVM_SETGROUPINFO
115 * -- LVM_GETGROUPMETRICS, LVM_SETGROUPMETRICS
116 * -- LVM_GETINSERTMARK, LVM_SETINSERTMARK
117 * -- LVM_GETINSERTMARKCOLOR, LVM_SETINSERTMARKCOLOR
118 * -- LVM_GETINSERTMARKRECT
119 * -- LVM_GETNUMBEROFWORKAREAS
120 * -- LVM_GETOUTLINECOLOR, LVM_SETOUTLINECOLOR
121 * -- LVM_GETSELECTEDCOLUMN, LVM_SETSELECTEDCOLUMN
122 * -- LVM_GETISEARCHSTRINGW, LVM_GETISEARCHSTRINGA
123 * -- LVM_GETTILEINFO, LVM_SETTILEINFO
124 * -- LVM_GETTILEVIEWINFO, LVM_SETTILEVIEWINFO
125 * -- LVM_GETUNICODEFORMAT, LVM_SETUNICODEFORMAT
126 * -- LVM_GETVIEW, LVM_SETVIEW
127 * -- LVM_GETWORKAREAS, LVM_SETWORKAREAS
128 * -- LVM_HASGROUP, LVM_INSERTGROUP, LVM_REMOVEGROUP, LVM_REMOVEALLGROUPS
129 * -- LVM_INSERTGROUPSORTED
130 * -- LVM_INSERTMARKHITTEST
131 * -- LVM_ISGROUPVIEWENABLED
132 * -- LVM_MAPIDTOINDEX, LVM_MAPINDEXTOID
134 * -- LVM_MOVEITEMTOGROUP
136 * -- LVM_SETTILEWIDTH
140 * -- ListView_GetCheckSate, ListView_SetCheckState
141 * -- ListView_GetHoverTime, ListView_SetHoverTime
142 * -- ListView_GetISearchString
143 * -- ListView_GetNumberOfWorkAreas
144 * -- ListView_GetUnicodeFormat, ListView_SetUnicodeFormat
145 * -- ListView_GetWorkAreas, ListView_SetWorkAreas
150 * Known differences in message stream from native control (not known if
151 * these differences cause problems):
152 * LVM_INSERTITEM issues LVM_SETITEMSTATE and LVM_SETITEM in certain cases.
153 * LVM_SETITEM does not always issue LVN_ITEMCHANGING/LVN_ITEMCHANGED.
154 * WM_CREATE does not issue WM_QUERYUISTATE and associated registry
155 * processing for "USEDOUBLECLICKTIME".
159 #include "wine/port.h"
174 #include "commctrl.h"
175 #include "comctl32.h"
178 #include "wine/debug.h"
179 #include "wine/unicode.h"
181 WINE_DEFAULT_DEBUG_CHANNEL(listview);
183 /* make sure you set this to 0 for production use! */
184 #define DEBUG_RANGES 1
186 typedef struct tagCOLUMN_INFO
188 RECT rcHeader; /* tracks the header's rectangle */
189 int fmt; /* same as LVCOLUMN.fmt */
192 typedef struct tagITEMHDR
196 } ITEMHDR, *LPITEMHDR;
198 typedef struct tagSUBITEM_INFO
204 typedef struct tagITEM_INFO
212 typedef struct tagRANGE
218 typedef struct tagRANGES
223 typedef struct tagITERATOR
232 typedef struct tagDELAYED_ITEM_EDIT
238 typedef struct tagLISTVIEW_INFO
245 HIMAGELIST himlNormal;
246 HIMAGELIST himlSmall;
247 HIMAGELIST himlState;
251 POINT ptClickPos; /* point where the user clicked */
252 BOOL bNoItemMetrics; /* flags if item metrics are not yet computed */
255 RANGES selectionRanges;
260 RECT rcList; /* This rectangle is really the window
261 * client rectangle possibly reduced by the
262 * horizontal scroll bar and/or header - see
263 * LISTVIEW_UpdateSize. This rectangle offset
264 * by the LISTVIEW_GetOrigin value is in
265 * client coordinates */
274 INT ntmHeight; /* Some cached metrics of the font used */
275 INT ntmMaxCharWidth; /* by the listview to draw items */
277 BOOL bRedraw; /* Turns on/off repaints & invalidations */
278 BOOL bAutoarrange; /* Autoarrange flag when NOT in LVS_AUTOARRANGE */
280 BOOL bDoChangeNotify; /* send change notification messages? */
283 DWORD dwStyle; /* the cached window GWL_STYLE */
284 DWORD dwLvExStyle; /* extended listview style */
285 INT nItemCount; /* the number of items in the list */
286 HDPA hdpaItems; /* array ITEM_INFO pointers */
287 HDPA hdpaPosX; /* maintains the (X, Y) coordinates of the */
288 HDPA hdpaPosY; /* items in LVS_ICON, and LVS_SMALLICON modes */
289 HDPA hdpaColumns; /* array of COLUMN_INFO pointers */
290 POINT currIconPos; /* this is the position next icon will be placed */
291 PFNLVCOMPARE pfnCompare;
296 INT nLButtonDownItem; /* tracks item to reset multiselection on WM_LBUTTONUP */
300 DWORD cditemmode; /* Keep the custom draw flags for an item/row */
302 DWORD lastKeyPressTimestamp;
304 INT nSearchParamLength;
305 WCHAR szSearchParam[ MAX_PATH ];
307 INT nMeasureItemHeight;
308 INT xTrackLine; /* The x coefficient of the track line or -1 if none */
309 DELAYED_ITEM_EDIT itemEdit; /* Pointer to this structure will be the timer ID */
311 DWORD iVersion; /* CCM_[G,S]ETVERSION */
317 /* How many we debug buffer to allocate */
318 #define DEBUG_BUFFERS 20
319 /* The size of a single debug bbuffer */
320 #define DEBUG_BUFFER_SIZE 256
322 /* Internal interface to LISTVIEW_HScroll and LISTVIEW_VScroll */
323 #define SB_INTERNAL -1
325 /* maximum size of a label */
326 #define DISP_TEXT_SIZE 512
328 /* padding for items in list and small icon display modes */
329 #define WIDTH_PADDING 12
331 /* padding for items in list, report and small icon display modes */
332 #define HEIGHT_PADDING 1
334 /* offset of items in report display mode */
335 #define REPORT_MARGINX 2
337 /* padding for icon in large icon display mode
338 * ICON_TOP_PADDING_NOTHITABLE - space between top of box and area
339 * that HITTEST will see.
340 * ICON_TOP_PADDING_HITABLE - spacing between above and icon.
341 * ICON_TOP_PADDING - sum of the two above.
342 * ICON_BOTTOM_PADDING - between bottom of icon and top of text
343 * LABEL_HOR_PADDING - between text and sides of box
344 * LABEL_VERT_PADDING - between bottom of text and end of box
346 * ICON_LR_PADDING - additional width above icon size.
347 * ICON_LR_HALF - half of the above value
349 #define ICON_TOP_PADDING_NOTHITABLE 2
350 #define ICON_TOP_PADDING_HITABLE 2
351 #define ICON_TOP_PADDING (ICON_TOP_PADDING_NOTHITABLE + ICON_TOP_PADDING_HITABLE)
352 #define ICON_BOTTOM_PADDING 4
353 #define LABEL_HOR_PADDING 5
354 #define LABEL_VERT_PADDING 7
355 #define ICON_LR_PADDING 16
356 #define ICON_LR_HALF (ICON_LR_PADDING/2)
358 /* default label width for items in list and small icon display modes */
359 #define DEFAULT_LABEL_WIDTH 40
361 /* default column width for items in list display mode */
362 #define DEFAULT_COLUMN_WIDTH 128
364 /* Size of "line" scroll for V & H scrolls */
365 #define LISTVIEW_SCROLL_ICON_LINE_SIZE 37
367 /* Padding between image and label */
368 #define IMAGE_PADDING 2
370 /* Padding behind the label */
371 #define TRAILING_LABEL_PADDING 12
372 #define TRAILING_HEADER_PADDING 11
374 /* Border for the icon caption */
375 #define CAPTION_BORDER 2
377 /* Standard DrawText flags */
378 #define LV_ML_DT_FLAGS (DT_TOP | DT_NOPREFIX | DT_EDITCONTROL | DT_CENTER | DT_WORDBREAK | DT_WORD_ELLIPSIS | DT_END_ELLIPSIS)
379 #define LV_FL_DT_FLAGS (DT_TOP | DT_NOPREFIX | DT_EDITCONTROL | DT_CENTER | DT_WORDBREAK | DT_NOCLIP)
380 #define LV_SL_DT_FLAGS (DT_VCENTER | DT_NOPREFIX | DT_EDITCONTROL | DT_SINGLELINE | DT_WORD_ELLIPSIS | DT_END_ELLIPSIS)
382 /* Image index from state */
383 #define STATEIMAGEINDEX(x) (((x) & LVIS_STATEIMAGEMASK) >> 12)
385 /* The time in milliseconds to reset the search in the list */
386 #define KEY_DELAY 450
388 /* Dump the LISTVIEW_INFO structure to the debug channel */
389 #define LISTVIEW_DUMP(iP) do { \
390 TRACE("hwndSelf=%p, clrBk=0x%06x, clrText=0x%06x, clrTextBk=0x%06x, ItemHeight=%d, ItemWidth=%d, Style=0x%08x\n", \
391 iP->hwndSelf, iP->clrBk, iP->clrText, iP->clrTextBk, \
392 iP->nItemHeight, iP->nItemWidth, iP->dwStyle); \
393 TRACE("hwndSelf=%p, himlNor=%p, himlSml=%p, himlState=%p, Focused=%d, Hot=%d, exStyle=0x%08x, Focus=%d\n", \
394 iP->hwndSelf, iP->himlNormal, iP->himlSmall, iP->himlState, \
395 iP->nFocusedItem, iP->nHotItem, iP->dwLvExStyle, iP->bFocus ); \
396 TRACE("hwndSelf=%p, ntmH=%d, icSz.cx=%d, icSz.cy=%d, icSp.cx=%d, icSp.cy=%d, notifyFmt=%d\n", \
397 iP->hwndSelf, iP->ntmHeight, iP->iconSize.cx, iP->iconSize.cy, \
398 iP->iconSpacing.cx, iP->iconSpacing.cy, iP->notifyFormat); \
399 TRACE("hwndSelf=%p, rcList=%s\n", iP->hwndSelf, wine_dbgstr_rect(&iP->rcList)); \
402 static const WCHAR themeClass[] = {'L','i','s','t','V','i','e','w',0};
405 * forward declarations
407 static BOOL LISTVIEW_GetItemT(const LISTVIEW_INFO *, LPLVITEMW, BOOL);
408 static void LISTVIEW_GetItemBox(const LISTVIEW_INFO *, INT, LPRECT);
409 static void LISTVIEW_GetItemOrigin(const LISTVIEW_INFO *, INT, LPPOINT);
410 static BOOL LISTVIEW_GetItemPosition(const LISTVIEW_INFO *, INT, LPPOINT);
411 static BOOL LISTVIEW_GetItemRect(const LISTVIEW_INFO *, INT, LPRECT);
412 static INT LISTVIEW_GetLabelWidth(const LISTVIEW_INFO *, INT);
413 static void LISTVIEW_GetOrigin(const LISTVIEW_INFO *, LPPOINT);
414 static BOOL LISTVIEW_GetViewRect(const LISTVIEW_INFO *, LPRECT);
415 static void LISTVIEW_UpdateSize(LISTVIEW_INFO *);
416 static LRESULT LISTVIEW_Command(const LISTVIEW_INFO *, WPARAM, LPARAM);
417 static INT LISTVIEW_GetStringWidthT(const LISTVIEW_INFO *, LPCWSTR, BOOL);
418 static BOOL LISTVIEW_KeySelection(LISTVIEW_INFO *, INT, BOOL);
419 static UINT LISTVIEW_GetItemState(const LISTVIEW_INFO *, INT, UINT);
420 static BOOL LISTVIEW_SetItemState(LISTVIEW_INFO *, INT, const LVITEMW *);
421 static LRESULT LISTVIEW_VScroll(LISTVIEW_INFO *, INT, INT, HWND);
422 static LRESULT LISTVIEW_HScroll(LISTVIEW_INFO *, INT, INT, HWND);
423 static BOOL LISTVIEW_EnsureVisible(LISTVIEW_INFO *, INT, BOOL);
424 static HWND CreateEditLabelT(LISTVIEW_INFO *, LPCWSTR, DWORD, INT, INT, INT, INT, BOOL);
425 static HIMAGELIST LISTVIEW_SetImageList(LISTVIEW_INFO *, INT, HIMAGELIST);
426 static INT LISTVIEW_HitTest(const LISTVIEW_INFO *, LPLVHITTESTINFO, BOOL, BOOL);
428 /******** Text handling functions *************************************/
430 /* A text pointer is either NULL, LPSTR_TEXTCALLBACK, or points to a
431 * text string. The string may be ANSI or Unicode, in which case
432 * the boolean isW tells us the type of the string.
434 * The name of the function tell what type of strings it expects:
435 * W: Unicode, T: ANSI/Unicode - function of isW
438 static inline BOOL is_textW(LPCWSTR text)
440 return text != NULL && text != LPSTR_TEXTCALLBACKW;
443 static inline BOOL is_textT(LPCWSTR text, BOOL isW)
445 /* we can ignore isW since LPSTR_TEXTCALLBACKW == LPSTR_TEXTCALLBACKA */
446 return is_textW(text);
449 static inline int textlenT(LPCWSTR text, BOOL isW)
451 return !is_textT(text, isW) ? 0 :
452 isW ? lstrlenW(text) : lstrlenA((LPCSTR)text);
455 static inline void textcpynT(LPWSTR dest, BOOL isDestW, LPCWSTR src, BOOL isSrcW, INT max)
458 if (isSrcW) lstrcpynW(dest, src, max);
459 else MultiByteToWideChar(CP_ACP, 0, (LPCSTR)src, -1, dest, max);
461 if (isSrcW) WideCharToMultiByte(CP_ACP, 0, src, -1, (LPSTR)dest, max, NULL, NULL);
462 else lstrcpynA((LPSTR)dest, (LPCSTR)src, max);
465 static inline LPWSTR textdupTtoW(LPCWSTR text, BOOL isW)
467 LPWSTR wstr = (LPWSTR)text;
469 if (!isW && is_textT(text, isW))
471 INT len = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)text, -1, NULL, 0);
472 wstr = Alloc(len * sizeof(WCHAR));
473 if (wstr) MultiByteToWideChar(CP_ACP, 0, (LPCSTR)text, -1, wstr, len);
475 TRACE(" wstr=%s\n", text == LPSTR_TEXTCALLBACKW ? "(callback)" : debugstr_w(wstr));
479 static inline void textfreeT(LPWSTR wstr, BOOL isW)
481 if (!isW && is_textT(wstr, isW)) Free (wstr);
485 * dest is a pointer to a Unicode string
486 * src is a pointer to a string (Unicode if isW, ANSI if !isW)
488 static BOOL textsetptrT(LPWSTR *dest, LPCWSTR src, BOOL isW)
492 if (src == LPSTR_TEXTCALLBACKW)
494 if (is_textW(*dest)) Free(*dest);
495 *dest = LPSTR_TEXTCALLBACKW;
499 LPWSTR pszText = textdupTtoW(src, isW);
500 if (*dest == LPSTR_TEXTCALLBACKW) *dest = NULL;
501 bResult = Str_SetPtrW(dest, pszText);
502 textfreeT(pszText, isW);
508 * compares a Unicode to a Unicode/ANSI text string
510 static inline int textcmpWT(LPCWSTR aw, LPCWSTR bt, BOOL isW)
512 if (!aw) return bt ? -1 : 0;
513 if (!bt) return aw ? 1 : 0;
514 if (aw == LPSTR_TEXTCALLBACKW)
515 return bt == LPSTR_TEXTCALLBACKW ? 0 : -1;
516 if (bt != LPSTR_TEXTCALLBACKW)
518 LPWSTR bw = textdupTtoW(bt, isW);
519 int r = bw ? lstrcmpW(aw, bw) : 1;
527 static inline int lstrncmpiW(LPCWSTR s1, LPCWSTR s2, int n)
531 n = min(min(n, lstrlenW(s1)), lstrlenW(s2));
532 res = CompareStringW(LOCALE_USER_DEFAULT, NORM_IGNORECASE, s1, n, s2, n);
533 return res ? res - sizeof(WCHAR) : res;
536 /******** Debugging functions *****************************************/
538 static inline LPCSTR debugtext_t(LPCWSTR text, BOOL isW)
540 if (text == LPSTR_TEXTCALLBACKW) return "(callback)";
541 return isW ? debugstr_w(text) : debugstr_a((LPCSTR)text);
544 static inline LPCSTR debugtext_tn(LPCWSTR text, BOOL isW, INT n)
546 if (text == LPSTR_TEXTCALLBACKW) return "(callback)";
547 n = min(textlenT(text, isW), n);
548 return isW ? debugstr_wn(text, n) : debugstr_an((LPCSTR)text, n);
551 static char* debug_getbuf(void)
553 static int index = 0;
554 static char buffers[DEBUG_BUFFERS][DEBUG_BUFFER_SIZE];
555 return buffers[index++ % DEBUG_BUFFERS];
558 static inline const char* debugrange(const RANGE *lprng)
560 if (!lprng) return "(null)";
561 return wine_dbg_sprintf("[%d, %d]", lprng->lower, lprng->upper);
564 static const char* debugscrollinfo(const SCROLLINFO *pScrollInfo)
566 char* buf = debug_getbuf(), *text = buf;
567 int len, size = DEBUG_BUFFER_SIZE;
569 if (pScrollInfo == NULL) return "(null)";
570 len = snprintf(buf, size, "{cbSize=%d, ", pScrollInfo->cbSize);
571 if (len == -1) goto end; buf += len; size -= len;
572 if (pScrollInfo->fMask & SIF_RANGE)
573 len = snprintf(buf, size, "nMin=%d, nMax=%d, ", pScrollInfo->nMin, pScrollInfo->nMax);
575 if (len == -1) goto end; buf += len; size -= len;
576 if (pScrollInfo->fMask & SIF_PAGE)
577 len = snprintf(buf, size, "nPage=%u, ", pScrollInfo->nPage);
579 if (len == -1) goto end; buf += len; size -= len;
580 if (pScrollInfo->fMask & SIF_POS)
581 len = snprintf(buf, size, "nPos=%d, ", pScrollInfo->nPos);
583 if (len == -1) goto end; buf += len; size -= len;
584 if (pScrollInfo->fMask & SIF_TRACKPOS)
585 len = snprintf(buf, size, "nTrackPos=%d, ", pScrollInfo->nTrackPos);
587 if (len == -1) goto end; buf += len; size -= len;
590 buf = text + strlen(text);
592 if (buf - text > 2) { buf[-2] = '}'; buf[-1] = 0; }
596 static const char* debugnmlistview(const NMLISTVIEW *plvnm)
598 if (!plvnm) return "(null)";
599 return wine_dbg_sprintf("iItem=%d, iSubItem=%d, uNewState=0x%x,"
600 " uOldState=0x%x, uChanged=0x%x, ptAction=%s, lParam=%ld",
601 plvnm->iItem, plvnm->iSubItem, plvnm->uNewState, plvnm->uOldState,
602 plvnm->uChanged, wine_dbgstr_point(&plvnm->ptAction), plvnm->lParam);
605 static const char* debuglvitem_t(const LVITEMW *lpLVItem, BOOL isW)
607 char* buf = debug_getbuf(), *text = buf;
608 int len, size = DEBUG_BUFFER_SIZE;
610 if (lpLVItem == NULL) return "(null)";
611 len = snprintf(buf, size, "{iItem=%d, iSubItem=%d, ", lpLVItem->iItem, lpLVItem->iSubItem);
612 if (len == -1) goto end; buf += len; size -= len;
613 if (lpLVItem->mask & LVIF_STATE)
614 len = snprintf(buf, size, "state=%x, stateMask=%x, ", lpLVItem->state, lpLVItem->stateMask);
616 if (len == -1) goto end; buf += len; size -= len;
617 if (lpLVItem->mask & LVIF_TEXT)
618 len = snprintf(buf, size, "pszText=%s, cchTextMax=%d, ", debugtext_tn(lpLVItem->pszText, isW, 80), lpLVItem->cchTextMax);
620 if (len == -1) goto end; buf += len; size -= len;
621 if (lpLVItem->mask & LVIF_IMAGE)
622 len = snprintf(buf, size, "iImage=%d, ", lpLVItem->iImage);
624 if (len == -1) goto end; buf += len; size -= len;
625 if (lpLVItem->mask & LVIF_PARAM)
626 len = snprintf(buf, size, "lParam=%lx, ", lpLVItem->lParam);
628 if (len == -1) goto end; buf += len; size -= len;
629 if (lpLVItem->mask & LVIF_INDENT)
630 len = snprintf(buf, size, "iIndent=%d, ", lpLVItem->iIndent);
632 if (len == -1) goto end; buf += len; size -= len;
635 buf = text + strlen(text);
637 if (buf - text > 2) { buf[-2] = '}'; buf[-1] = 0; }
641 static const char* debuglvcolumn_t(const LVCOLUMNW *lpColumn, BOOL isW)
643 char* buf = debug_getbuf(), *text = buf;
644 int len, size = DEBUG_BUFFER_SIZE;
646 if (lpColumn == NULL) return "(null)";
647 len = snprintf(buf, size, "{");
648 if (len == -1) goto end; buf += len; size -= len;
649 if (lpColumn->mask & LVCF_SUBITEM)
650 len = snprintf(buf, size, "iSubItem=%d, ", lpColumn->iSubItem);
652 if (len == -1) goto end; buf += len; size -= len;
653 if (lpColumn->mask & LVCF_FMT)
654 len = snprintf(buf, size, "fmt=%x, ", lpColumn->fmt);
656 if (len == -1) goto end; buf += len; size -= len;
657 if (lpColumn->mask & LVCF_WIDTH)
658 len = snprintf(buf, size, "cx=%d, ", lpColumn->cx);
660 if (len == -1) goto end; buf += len; size -= len;
661 if (lpColumn->mask & LVCF_TEXT)
662 len = snprintf(buf, size, "pszText=%s, cchTextMax=%d, ", debugtext_tn(lpColumn->pszText, isW, 80), lpColumn->cchTextMax);
664 if (len == -1) goto end; buf += len; size -= len;
665 if (lpColumn->mask & LVCF_IMAGE)
666 len = snprintf(buf, size, "iImage=%d, ", lpColumn->iImage);
668 if (len == -1) goto end; buf += len; size -= len;
669 if (lpColumn->mask & LVCF_ORDER)
670 len = snprintf(buf, size, "iOrder=%d, ", lpColumn->iOrder);
672 if (len == -1) goto end; buf += len; size -= len;
675 buf = text + strlen(text);
677 if (buf - text > 2) { buf[-2] = '}'; buf[-1] = 0; }
681 static const char* debuglvhittestinfo(const LVHITTESTINFO *lpht)
683 if (!lpht) return "(null)";
685 return wine_dbg_sprintf("{pt=%s, flags=0x%x, iItem=%d, iSubItem=%d}",
686 wine_dbgstr_point(&lpht->pt), lpht->flags, lpht->iItem, lpht->iSubItem);
689 /* Return the corresponding text for a given scroll value */
690 static inline LPCSTR debugscrollcode(int nScrollCode)
694 case SB_LINELEFT: return "SB_LINELEFT";
695 case SB_LINERIGHT: return "SB_LINERIGHT";
696 case SB_PAGELEFT: return "SB_PAGELEFT";
697 case SB_PAGERIGHT: return "SB_PAGERIGHT";
698 case SB_THUMBPOSITION: return "SB_THUMBPOSITION";
699 case SB_THUMBTRACK: return "SB_THUMBTRACK";
700 case SB_ENDSCROLL: return "SB_ENDSCROLL";
701 case SB_INTERNAL: return "SB_INTERNAL";
702 default: return "unknown";
707 /******** Notification functions ************************************/
709 static LRESULT notify_forward_header(const LISTVIEW_INFO *infoPtr, const NMHEADERW *lpnmh)
711 return SendMessageW(infoPtr->hwndNotify, WM_NOTIFY,
712 (WPARAM)lpnmh->hdr.idFrom, (LPARAM)lpnmh);
715 static LRESULT notify_hdr(const LISTVIEW_INFO *infoPtr, INT code, LPNMHDR pnmh)
719 TRACE("(code=%d)\n", code);
721 pnmh->hwndFrom = infoPtr->hwndSelf;
722 pnmh->idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
724 result = SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, pnmh->idFrom, (LPARAM)pnmh);
726 TRACE(" <= %ld\n", result);
731 static inline BOOL notify(const LISTVIEW_INFO *infoPtr, INT code)
734 HWND hwnd = infoPtr->hwndSelf;
735 notify_hdr(infoPtr, code, &nmh);
736 return IsWindow(hwnd);
739 static inline void notify_itemactivate(const LISTVIEW_INFO *infoPtr, const LVHITTESTINFO *htInfo)
750 item.mask = LVIF_PARAM|LVIF_STATE;
751 item.iItem = htInfo->iItem;
753 if (LISTVIEW_GetItemT(infoPtr, &item, TRUE)) {
754 nmia.lParam = item.lParam;
755 nmia.uOldState = item.state;
756 nmia.uNewState = item.state | LVIS_ACTIVATING;
757 nmia.uChanged = LVIF_STATE;
760 nmia.iItem = htInfo->iItem;
761 nmia.iSubItem = htInfo->iSubItem;
762 nmia.ptAction = htInfo->pt;
764 if (GetKeyState(VK_SHIFT) & 0x8000) nmia.uKeyFlags |= LVKF_SHIFT;
765 if (GetKeyState(VK_CONTROL) & 0x8000) nmia.uKeyFlags |= LVKF_CONTROL;
766 if (GetKeyState(VK_MENU) & 0x8000) nmia.uKeyFlags |= LVKF_ALT;
768 notify_hdr(infoPtr, LVN_ITEMACTIVATE, (LPNMHDR)&nmia);
771 static inline LRESULT notify_listview(const LISTVIEW_INFO *infoPtr, INT code, LPNMLISTVIEW plvnm)
773 TRACE("(code=%d, plvnm=%s)\n", code, debugnmlistview(plvnm));
774 return notify_hdr(infoPtr, code, (LPNMHDR)plvnm);
777 static BOOL notify_click(const LISTVIEW_INFO *infoPtr, INT code, const LVHITTESTINFO *lvht)
781 HWND hwnd = infoPtr->hwndSelf;
783 TRACE("code=%d, lvht=%s\n", code, debuglvhittestinfo(lvht));
784 ZeroMemory(&nmlv, sizeof(nmlv));
785 nmlv.iItem = lvht->iItem;
786 nmlv.iSubItem = lvht->iSubItem;
787 nmlv.ptAction = lvht->pt;
788 item.mask = LVIF_PARAM;
789 item.iItem = lvht->iItem;
791 if (LISTVIEW_GetItemT(infoPtr, &item, TRUE)) nmlv.lParam = item.lParam;
792 notify_listview(infoPtr, code, &nmlv);
793 return IsWindow(hwnd);
796 static BOOL notify_deleteitem(const LISTVIEW_INFO *infoPtr, INT nItem)
800 HWND hwnd = infoPtr->hwndSelf;
802 ZeroMemory(&nmlv, sizeof (NMLISTVIEW));
804 item.mask = LVIF_PARAM;
807 if (LISTVIEW_GetItemT(infoPtr, &item, TRUE)) nmlv.lParam = item.lParam;
808 notify_listview(infoPtr, LVN_DELETEITEM, &nmlv);
809 return IsWindow(hwnd);
812 static int get_ansi_notification(UINT unicodeNotificationCode)
814 switch (unicodeNotificationCode)
816 case LVN_BEGINLABELEDITW: return LVN_BEGINLABELEDITA;
817 case LVN_ENDLABELEDITW: return LVN_ENDLABELEDITA;
818 case LVN_GETDISPINFOW: return LVN_GETDISPINFOA;
819 case LVN_SETDISPINFOW: return LVN_SETDISPINFOA;
820 case LVN_ODFINDITEMW: return LVN_ODFINDITEMA;
821 case LVN_GETINFOTIPW: return LVN_GETINFOTIPA;
823 ERR("unknown notification %x\n", unicodeNotificationCode);
829 Send notification. depends on dispinfoW having same
830 structure as dispinfoA.
831 infoPtr : listview struct
832 notificationCode : *Unicode* notification code
833 pdi : dispinfo structure (can be unicode or ansi)
834 isW : TRUE if dispinfo is Unicode
836 static BOOL notify_dispinfoT(const LISTVIEW_INFO *infoPtr, UINT notificationCode, LPNMLVDISPINFOW pdi, BOOL isW)
838 BOOL bResult = FALSE;
839 BOOL convertToAnsi = FALSE, convertToUnicode = FALSE;
840 INT cchTempBufMax = 0, savCchTextMax = 0;
842 LPWSTR pszTempBuf = NULL, savPszText = NULL;
844 if ((pdi->item.mask & LVIF_TEXT) && is_textT(pdi->item.pszText, isW))
846 convertToAnsi = (isW && infoPtr->notifyFormat == NFR_ANSI);
847 convertToUnicode = (!isW && infoPtr->notifyFormat == NFR_UNICODE);
850 if (convertToAnsi || convertToUnicode)
852 if (notificationCode != LVN_GETDISPINFOW)
854 cchTempBufMax = convertToUnicode ?
855 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pdi->item.pszText, -1, NULL, 0):
856 WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, NULL, 0, NULL, NULL);
860 cchTempBufMax = pdi->item.cchTextMax;
861 *pdi->item.pszText = 0; /* make sure we don't process garbage */
864 pszTempBuf = Alloc( (convertToUnicode ? sizeof(WCHAR) : sizeof(CHAR)) * cchTempBufMax);
865 if (!pszTempBuf) return FALSE;
867 if (convertToUnicode)
868 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pdi->item.pszText, -1,
869 pszTempBuf, cchTempBufMax);
871 WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, (LPSTR) pszTempBuf,
872 cchTempBufMax, NULL, NULL);
874 savCchTextMax = pdi->item.cchTextMax;
875 savPszText = pdi->item.pszText;
876 pdi->item.pszText = pszTempBuf;
877 pdi->item.cchTextMax = cchTempBufMax;
880 if (infoPtr->notifyFormat == NFR_ANSI)
881 realNotifCode = get_ansi_notification(notificationCode);
883 realNotifCode = notificationCode;
884 TRACE(" pdi->item=%s\n", debuglvitem_t(&pdi->item, infoPtr->notifyFormat != NFR_ANSI));
885 bResult = notify_hdr(infoPtr, realNotifCode, &pdi->hdr);
887 if (convertToUnicode || convertToAnsi)
889 if (convertToUnicode) /* note : pointer can be changed by app ! */
890 WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, (LPSTR) savPszText,
891 savCchTextMax, NULL, NULL);
893 MultiByteToWideChar(CP_ACP, 0, (LPSTR) pdi->item.pszText, -1,
894 savPszText, savCchTextMax);
895 pdi->item.pszText = savPszText; /* restores our buffer */
896 pdi->item.cchTextMax = savCchTextMax;
902 static void customdraw_fill(NMLVCUSTOMDRAW *lpnmlvcd, const LISTVIEW_INFO *infoPtr, HDC hdc,
903 const RECT *rcBounds, const LVITEMW *lplvItem)
905 ZeroMemory(lpnmlvcd, sizeof(NMLVCUSTOMDRAW));
906 lpnmlvcd->nmcd.hdc = hdc;
907 lpnmlvcd->nmcd.rc = *rcBounds;
908 lpnmlvcd->clrTextBk = infoPtr->clrTextBk;
909 lpnmlvcd->clrText = infoPtr->clrText;
910 if (!lplvItem) return;
911 lpnmlvcd->nmcd.dwItemSpec = lplvItem->iItem + 1;
912 lpnmlvcd->iSubItem = lplvItem->iSubItem;
913 if (lplvItem->state & LVIS_SELECTED) lpnmlvcd->nmcd.uItemState |= CDIS_SELECTED;
914 if (lplvItem->state & LVIS_FOCUSED) lpnmlvcd->nmcd.uItemState |= CDIS_FOCUS;
915 if (lplvItem->iItem == infoPtr->nHotItem) lpnmlvcd->nmcd.uItemState |= CDIS_HOT;
916 lpnmlvcd->nmcd.lItemlParam = lplvItem->lParam;
919 static inline DWORD notify_customdraw (const LISTVIEW_INFO *infoPtr, DWORD dwDrawStage, NMLVCUSTOMDRAW *lpnmlvcd)
921 BOOL isForItem = (lpnmlvcd->nmcd.dwItemSpec != 0);
924 lpnmlvcd->nmcd.dwDrawStage = dwDrawStage;
925 if (isForItem) lpnmlvcd->nmcd.dwDrawStage |= CDDS_ITEM;
926 if (lpnmlvcd->iSubItem) lpnmlvcd->nmcd.dwDrawStage |= CDDS_SUBITEM;
927 if (isForItem) lpnmlvcd->nmcd.dwItemSpec--;
928 result = notify_hdr(infoPtr, NM_CUSTOMDRAW, &lpnmlvcd->nmcd.hdr);
929 if (isForItem) lpnmlvcd->nmcd.dwItemSpec++;
933 static void prepaint_setup (const LISTVIEW_INFO *infoPtr, HDC hdc, NMLVCUSTOMDRAW *lpnmlvcd, BOOL SubItem)
935 if (lpnmlvcd->clrTextBk == CLR_DEFAULT)
936 lpnmlvcd->clrTextBk = comctl32_color.clrWindow;
937 if (lpnmlvcd->clrText == CLR_DEFAULT)
938 lpnmlvcd->clrText = comctl32_color.clrWindowText;
940 /* apparently, for selected items, we have to override the returned values */
943 if (lpnmlvcd->nmcd.uItemState & CDIS_SELECTED)
947 lpnmlvcd->clrTextBk = comctl32_color.clrHighlight;
948 lpnmlvcd->clrText = comctl32_color.clrHighlightText;
950 else if (infoPtr->dwStyle & LVS_SHOWSELALWAYS)
952 lpnmlvcd->clrTextBk = comctl32_color.clr3dFace;
953 lpnmlvcd->clrText = comctl32_color.clrBtnText;
958 /* Set the text attributes */
959 if (lpnmlvcd->clrTextBk != CLR_NONE)
961 SetBkMode(hdc, OPAQUE);
962 SetBkColor(hdc,lpnmlvcd->clrTextBk);
965 SetBkMode(hdc, TRANSPARENT);
966 SetTextColor(hdc, lpnmlvcd->clrText);
969 static inline DWORD notify_postpaint (const LISTVIEW_INFO *infoPtr, NMLVCUSTOMDRAW *lpnmlvcd)
971 return notify_customdraw(infoPtr, CDDS_POSTPAINT, lpnmlvcd);
974 /******** Item iterator functions **********************************/
976 static RANGES ranges_create(int count);
977 static void ranges_destroy(RANGES ranges);
978 static BOOL ranges_add(RANGES ranges, RANGE range);
979 static BOOL ranges_del(RANGES ranges, RANGE range);
980 static void ranges_dump(RANGES ranges);
982 static inline BOOL ranges_additem(RANGES ranges, INT nItem)
984 RANGE range = { nItem, nItem + 1 };
986 return ranges_add(ranges, range);
989 static inline BOOL ranges_delitem(RANGES ranges, INT nItem)
991 RANGE range = { nItem, nItem + 1 };
993 return ranges_del(ranges, range);
997 * ITERATOR DOCUMENTATION
999 * The iterator functions allow for easy, and convenient iteration
1000 * over items of interest in the list. Typically, you create a
1001 * iterator, use it, and destroy it, as such:
1004 * iterator_xxxitems(&i, ...);
1005 * while (iterator_{prev,next}(&i)
1007 * //code which uses i.nItem
1009 * iterator_destroy(&i);
1011 * where xxx is either: framed, or visible.
1012 * Note that it is important that the code destroys the iterator
1013 * after it's done with it, as the creation of the iterator may
1014 * allocate memory, which thus needs to be freed.
1016 * You can iterate both forwards, and backwards through the list,
1017 * by using iterator_next or iterator_prev respectively.
1019 * Lower numbered items are draw on top of higher number items in
1020 * LVS_ICON, and LVS_SMALLICON (which are the only modes where
1021 * items may overlap). So, to test items, you should use
1023 * which lists the items top to bottom (in Z-order).
1024 * For drawing items, you should use
1026 * which lists the items bottom to top (in Z-order).
1027 * If you keep iterating over the items after the end-of-items
1028 * marker (-1) is returned, the iterator will start from the
1029 * beginning. Typically, you don't need to test for -1,
1030 * because iterator_{next,prev} will return TRUE if more items
1031 * are to be iterated over, or FALSE otherwise.
1033 * Note: the iterator is defined to be bidirectional. That is,
1034 * any number of prev followed by any number of next, or
1035 * five versa, should leave the iterator at the same item:
1036 * prev * n, next * n = next * n, prev * n
1038 * The iterator has a notion of an out-of-order, special item,
1039 * which sits at the start of the list. This is used in
1040 * LVS_ICON, and LVS_SMALLICON mode to handle the focused item,
1041 * which needs to be first, as it may overlap other items.
1043 * The code is a bit messy because we have:
1044 * - a special item to deal with
1045 * - simple range, or composite range
1047 * If you find bugs, or want to add features, please make sure you
1048 * always check/modify *both* iterator_prev, and iterator_next.
1052 * This function iterates through the items in increasing order,
1053 * but prefixed by the special item, then -1. That is:
1054 * special, 1, 2, 3, ..., n, -1.
1055 * Each item is listed only once.
1057 static inline BOOL iterator_next(ITERATOR* i)
1061 i->nItem = i->nSpecial;
1062 if (i->nItem != -1) return TRUE;
1064 if (i->nItem == i->nSpecial)
1066 if (i->ranges) i->index = 0;
1072 if (i->nItem == i->nSpecial) i->nItem++;
1073 if (i->nItem < i->range.upper) return TRUE;
1078 if (i->index < DPA_GetPtrCount(i->ranges->hdpa))
1079 i->range = *(RANGE*)DPA_GetPtr(i->ranges->hdpa, i->index++);
1082 else if (i->nItem >= i->range.upper) goto end;
1084 i->nItem = i->range.lower;
1085 if (i->nItem >= 0) goto testitem;
1092 * This function iterates through the items in decreasing order,
1093 * followed by the special item, then -1. That is:
1094 * n, n-1, ..., 3, 2, 1, special, -1.
1095 * Each item is listed only once.
1097 static inline BOOL iterator_prev(ITERATOR* i)
1104 if (i->ranges) i->index = DPA_GetPtrCount(i->ranges->hdpa);
1107 if (i->nItem == i->nSpecial)
1115 if (i->nItem == i->nSpecial) i->nItem--;
1116 if (i->nItem >= i->range.lower) return TRUE;
1122 i->range = *(RANGE*)DPA_GetPtr(i->ranges->hdpa, --i->index);
1125 else if (!start && i->nItem < i->range.lower) goto end;
1127 i->nItem = i->range.upper;
1128 if (i->nItem > 0) goto testitem;
1130 return (i->nItem = i->nSpecial) != -1;
1133 static RANGE iterator_range(const ITERATOR *i)
1137 if (!i->ranges) return i->range;
1139 if (DPA_GetPtrCount(i->ranges->hdpa) > 0)
1141 range.lower = (*(RANGE*)DPA_GetPtr(i->ranges->hdpa, 0)).lower;
1142 range.upper = (*(RANGE*)DPA_GetPtr(i->ranges->hdpa, DPA_GetPtrCount(i->ranges->hdpa) - 1)).upper;
1144 else range.lower = range.upper = 0;
1150 * Releases resources associated with this ierator.
1152 static inline void iterator_destroy(const ITERATOR *i)
1154 ranges_destroy(i->ranges);
1158 * Create an empty iterator.
1160 static inline BOOL iterator_empty(ITERATOR* i)
1162 ZeroMemory(i, sizeof(*i));
1163 i->nItem = i->nSpecial = i->range.lower = i->range.upper = -1;
1168 * Create an iterator over a range.
1170 static inline BOOL iterator_rangeitems(ITERATOR* i, RANGE range)
1178 * Create an iterator over a bunch of ranges.
1179 * Please note that the iterator will take ownership of the ranges,
1180 * and will free them upon destruction.
1182 static inline BOOL iterator_rangesitems(ITERATOR* i, RANGES ranges)
1190 * Creates an iterator over the items which intersect lprc.
1192 static BOOL iterator_frameditems(ITERATOR* i, const LISTVIEW_INFO* infoPtr, const RECT *lprc)
1194 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
1195 RECT frame = *lprc, rcItem, rcTemp;
1198 /* in case we fail, we want to return an empty iterator */
1199 if (!iterator_empty(i)) return FALSE;
1201 LISTVIEW_GetOrigin(infoPtr, &Origin);
1203 TRACE("(lprc=%s)\n", wine_dbgstr_rect(lprc));
1204 OffsetRect(&frame, -Origin.x, -Origin.y);
1206 if (uView == LVS_ICON || uView == LVS_SMALLICON)
1210 if (uView == LVS_ICON && infoPtr->nFocusedItem != -1)
1212 LISTVIEW_GetItemBox(infoPtr, infoPtr->nFocusedItem, &rcItem);
1213 if (IntersectRect(&rcTemp, &rcItem, lprc))
1214 i->nSpecial = infoPtr->nFocusedItem;
1216 if (!(iterator_rangesitems(i, ranges_create(50)))) return FALSE;
1217 /* to do better here, we need to have PosX, and PosY sorted */
1218 TRACE("building icon ranges:\n");
1219 for (nItem = 0; nItem < infoPtr->nItemCount; nItem++)
1221 rcItem.left = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosX, nItem);
1222 rcItem.top = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosY, nItem);
1223 rcItem.right = rcItem.left + infoPtr->nItemWidth;
1224 rcItem.bottom = rcItem.top + infoPtr->nItemHeight;
1225 if (IntersectRect(&rcTemp, &rcItem, &frame))
1226 ranges_additem(i->ranges, nItem);
1230 else if (uView == LVS_REPORT)
1234 if (frame.left >= infoPtr->nItemWidth) return TRUE;
1235 if (frame.top >= infoPtr->nItemHeight * infoPtr->nItemCount) return TRUE;
1237 range.lower = max(frame.top / infoPtr->nItemHeight, 0);
1238 range.upper = min((frame.bottom - 1) / infoPtr->nItemHeight, infoPtr->nItemCount - 1) + 1;
1239 if (range.upper <= range.lower) return TRUE;
1240 if (!iterator_rangeitems(i, range)) return FALSE;
1241 TRACE(" report=%s\n", debugrange(&i->range));
1245 INT nPerCol = max((infoPtr->rcList.bottom - infoPtr->rcList.top) / infoPtr->nItemHeight, 1);
1246 INT nFirstRow = max(frame.top / infoPtr->nItemHeight, 0);
1247 INT nLastRow = min((frame.bottom - 1) / infoPtr->nItemHeight, nPerCol - 1);
1248 INT nFirstCol = max(frame.left / infoPtr->nItemWidth, 0);
1249 INT nLastCol = min((frame.right - 1) / infoPtr->nItemWidth, (infoPtr->nItemCount + nPerCol - 1) / nPerCol);
1250 INT lower = nFirstCol * nPerCol + nFirstRow;
1254 TRACE("nPerCol=%d, nFirstRow=%d, nLastRow=%d, nFirstCol=%d, nLastCol=%d, lower=%d\n",
1255 nPerCol, nFirstRow, nLastRow, nFirstCol, nLastCol, lower);
1257 if (nLastCol < nFirstCol || nLastRow < nFirstRow) return TRUE;
1259 if (!(iterator_rangesitems(i, ranges_create(nLastCol - nFirstCol + 1)))) return FALSE;
1260 TRACE("building list ranges:\n");
1261 for (nCol = nFirstCol; nCol <= nLastCol; nCol++)
1263 item_range.lower = nCol * nPerCol + nFirstRow;
1264 if(item_range.lower >= infoPtr->nItemCount) break;
1265 item_range.upper = min(nCol * nPerCol + nLastRow + 1, infoPtr->nItemCount);
1266 TRACE(" list=%s\n", debugrange(&item_range));
1267 ranges_add(i->ranges, item_range);
1275 * Creates an iterator over the items which intersect the visible region of hdc.
1277 static BOOL iterator_visibleitems(ITERATOR *i, const LISTVIEW_INFO *infoPtr, HDC hdc)
1279 POINT Origin, Position;
1280 RECT rcItem, rcClip;
1283 rgntype = GetClipBox(hdc, &rcClip);
1284 if (rgntype == NULLREGION) return iterator_empty(i);
1285 if (!iterator_frameditems(i, infoPtr, &rcClip)) return FALSE;
1286 if (rgntype == SIMPLEREGION) return TRUE;
1288 /* first deal with the special item */
1289 if (i->nSpecial != -1)
1291 LISTVIEW_GetItemBox(infoPtr, i->nSpecial, &rcItem);
1292 if (!RectVisible(hdc, &rcItem)) i->nSpecial = -1;
1295 /* if we can't deal with the region, we'll just go with the simple range */
1296 LISTVIEW_GetOrigin(infoPtr, &Origin);
1297 TRACE("building visible range:\n");
1298 if (!i->ranges && i->range.lower < i->range.upper)
1300 if (!(i->ranges = ranges_create(50))) return TRUE;
1301 if (!ranges_add(i->ranges, i->range))
1303 ranges_destroy(i->ranges);
1309 /* now delete the invisible items from the list */
1310 while(iterator_next(i))
1312 LISTVIEW_GetItemOrigin(infoPtr, i->nItem, &Position);
1313 rcItem.left = Position.x + Origin.x;
1314 rcItem.top = Position.y + Origin.y;
1315 rcItem.right = rcItem.left + infoPtr->nItemWidth;
1316 rcItem.bottom = rcItem.top + infoPtr->nItemHeight;
1317 if (!RectVisible(hdc, &rcItem))
1318 ranges_delitem(i->ranges, i->nItem);
1320 /* the iterator should restart on the next iterator_next */
1326 /******** Misc helper functions ************************************/
1328 static inline LRESULT CallWindowProcT(WNDPROC proc, HWND hwnd, UINT uMsg,
1329 WPARAM wParam, LPARAM lParam, BOOL isW)
1331 if (isW) return CallWindowProcW(proc, hwnd, uMsg, wParam, lParam);
1332 else return CallWindowProcA(proc, hwnd, uMsg, wParam, lParam);
1335 static inline BOOL is_autoarrange(const LISTVIEW_INFO *infoPtr)
1337 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
1339 return ((infoPtr->dwStyle & LVS_AUTOARRANGE) || infoPtr->bAutoarrange) &&
1340 (uView == LVS_ICON || uView == LVS_SMALLICON);
1343 static void toggle_checkbox_state(LISTVIEW_INFO *infoPtr, INT nItem)
1345 DWORD state = STATEIMAGEINDEX(LISTVIEW_GetItemState(infoPtr, nItem, LVIS_STATEIMAGEMASK));
1346 if(state == 1 || state == 2)
1350 lvitem.state = INDEXTOSTATEIMAGEMASK(state);
1351 lvitem.stateMask = LVIS_STATEIMAGEMASK;
1352 LISTVIEW_SetItemState(infoPtr, nItem, &lvitem);
1356 /******** Internal API functions ************************************/
1358 static inline COLUMN_INFO * LISTVIEW_GetColumnInfo(const LISTVIEW_INFO *infoPtr, INT nSubItem)
1360 static COLUMN_INFO mainItem;
1362 if (nSubItem == 0 && DPA_GetPtrCount(infoPtr->hdpaColumns) == 0) return &mainItem;
1363 assert (nSubItem >= 0 && nSubItem < DPA_GetPtrCount(infoPtr->hdpaColumns));
1364 return DPA_GetPtr(infoPtr->hdpaColumns, nSubItem);
1367 static INT LISTVIEW_CreateHeader(LISTVIEW_INFO *infoPtr)
1369 DWORD dFlags = WS_CHILD | HDS_HORZ | HDS_FULLDRAG | HDS_DRAGDROP;
1372 if (infoPtr->hwndHeader) return 0;
1374 TRACE("Creating header for list %p\n", infoPtr->hwndSelf);
1376 /* setup creation flags */
1377 dFlags |= (LVS_NOSORTHEADER & infoPtr->dwStyle) ? 0 : HDS_BUTTONS;
1378 dFlags |= (LVS_NOCOLUMNHEADER & infoPtr->dwStyle) ? HDS_HIDDEN : 0;
1380 hInst = (HINSTANCE)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_HINSTANCE);
1383 infoPtr->hwndHeader = CreateWindowW(WC_HEADERW, NULL, dFlags,
1384 0, 0, 0, 0, infoPtr->hwndSelf, NULL, hInst, NULL);
1385 if (!infoPtr->hwndHeader) return -1;
1387 /* set header unicode format */
1388 SendMessageW(infoPtr->hwndHeader, HDM_SETUNICODEFORMAT, TRUE, 0);
1390 /* set header font */
1391 SendMessageW(infoPtr->hwndHeader, WM_SETFONT, (WPARAM)infoPtr->hFont, (LPARAM)TRUE);
1393 LISTVIEW_UpdateSize(infoPtr);
1398 static inline void LISTVIEW_GetHeaderRect(const LISTVIEW_INFO *infoPtr, INT nSubItem, LPRECT lprc)
1400 *lprc = LISTVIEW_GetColumnInfo(infoPtr, nSubItem)->rcHeader;
1403 static inline BOOL LISTVIEW_GetItemW(const LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem)
1405 return LISTVIEW_GetItemT(infoPtr, lpLVItem, TRUE);
1408 /* used to handle collapse main item column case */
1409 static inline BOOL LISTVIEW_DrawFocusRect(const LISTVIEW_INFO *infoPtr, HDC hdc)
1411 return (infoPtr->rcFocus.left < infoPtr->rcFocus.right) ?
1412 DrawFocusRect(hdc, &infoPtr->rcFocus) : FALSE;
1415 /* Listview invalidation functions: use _only_ these functions to invalidate */
1417 static inline BOOL is_redrawing(const LISTVIEW_INFO *infoPtr)
1419 return infoPtr->bRedraw;
1422 static inline void LISTVIEW_InvalidateRect(const LISTVIEW_INFO *infoPtr, const RECT* rect)
1424 if(!is_redrawing(infoPtr)) return;
1425 TRACE(" invalidating rect=%s\n", wine_dbgstr_rect(rect));
1426 InvalidateRect(infoPtr->hwndSelf, rect, TRUE);
1429 static inline void LISTVIEW_InvalidateItem(const LISTVIEW_INFO *infoPtr, INT nItem)
1433 if(!is_redrawing(infoPtr)) return;
1434 LISTVIEW_GetItemBox(infoPtr, nItem, &rcBox);
1435 LISTVIEW_InvalidateRect(infoPtr, &rcBox);
1438 static inline void LISTVIEW_InvalidateSubItem(const LISTVIEW_INFO *infoPtr, INT nItem, INT nSubItem)
1440 POINT Origin, Position;
1443 if(!is_redrawing(infoPtr)) return;
1444 assert ((infoPtr->dwStyle & LVS_TYPEMASK) == LVS_REPORT);
1445 LISTVIEW_GetOrigin(infoPtr, &Origin);
1446 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
1447 LISTVIEW_GetHeaderRect(infoPtr, nSubItem, &rcBox);
1449 rcBox.bottom = infoPtr->nItemHeight;
1450 OffsetRect(&rcBox, Origin.x + Position.x, Origin.y + Position.y);
1451 LISTVIEW_InvalidateRect(infoPtr, &rcBox);
1454 static inline void LISTVIEW_InvalidateList(const LISTVIEW_INFO *infoPtr)
1456 LISTVIEW_InvalidateRect(infoPtr, NULL);
1459 static inline void LISTVIEW_InvalidateColumn(const LISTVIEW_INFO *infoPtr, INT nColumn)
1463 if(!is_redrawing(infoPtr)) return;
1464 LISTVIEW_GetHeaderRect(infoPtr, nColumn, &rcCol);
1465 rcCol.top = infoPtr->rcList.top;
1466 rcCol.bottom = infoPtr->rcList.bottom;
1467 LISTVIEW_InvalidateRect(infoPtr, &rcCol);
1472 * Retrieves the number of items that can fit vertically in the client area.
1475 * [I] infoPtr : valid pointer to the listview structure
1478 * Number of items per row.
1480 static inline INT LISTVIEW_GetCountPerRow(const LISTVIEW_INFO *infoPtr)
1482 INT nListWidth = infoPtr->rcList.right - infoPtr->rcList.left;
1484 return max(nListWidth/infoPtr->nItemWidth, 1);
1489 * Retrieves the number of items that can fit horizontally in the client
1493 * [I] infoPtr : valid pointer to the listview structure
1496 * Number of items per column.
1498 static inline INT LISTVIEW_GetCountPerColumn(const LISTVIEW_INFO *infoPtr)
1500 INT nListHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
1502 return max(nListHeight / infoPtr->nItemHeight, 1);
1506 /*************************************************************************
1507 * LISTVIEW_ProcessLetterKeys
1509 * Processes keyboard messages generated by pressing the letter keys
1511 * What this does is perform a case insensitive search from the
1512 * current position with the following quirks:
1513 * - If two chars or more are pressed in quick succession we search
1514 * for the corresponding string (e.g. 'abc').
1515 * - If there is a delay we wipe away the current search string and
1516 * restart with just that char.
1517 * - If the user keeps pressing the same character, whether slowly or
1518 * fast, so that the search string is entirely composed of this
1519 * character ('aaaaa' for instance), then we search for first item
1520 * that starting with that character.
1521 * - If the user types the above character in quick succession, then
1522 * we must also search for the corresponding string ('aaaaa'), and
1523 * go to that string if there is a match.
1526 * [I] hwnd : handle to the window
1527 * [I] charCode : the character code, the actual character
1528 * [I] keyData : key data
1536 * - The current implementation has a list of characters it will
1537 * accept and it ignores everything else. In particular it will
1538 * ignore accentuated characters which seems to match what
1539 * Windows does. But I'm not sure it makes sense to follow
1541 * - We don't sound a beep when the search fails.
1545 * TREEVIEW_ProcessLetterKeys
1547 static INT LISTVIEW_ProcessLetterKeys(LISTVIEW_INFO *infoPtr, WPARAM charCode, LPARAM keyData)
1552 WCHAR buffer[MAX_PATH];
1553 DWORD lastKeyPressTimestamp = infoPtr->lastKeyPressTimestamp;
1555 /* simple parameter checking */
1556 if (!charCode || !keyData) return 0;
1558 /* only allow the valid WM_CHARs through */
1559 if (!isalnumW(charCode) &&
1560 charCode != '.' && charCode != '`' && charCode != '!' &&
1561 charCode != '@' && charCode != '#' && charCode != '$' &&
1562 charCode != '%' && charCode != '^' && charCode != '&' &&
1563 charCode != '*' && charCode != '(' && charCode != ')' &&
1564 charCode != '-' && charCode != '_' && charCode != '+' &&
1565 charCode != '=' && charCode != '\\'&& charCode != ']' &&
1566 charCode != '}' && charCode != '[' && charCode != '{' &&
1567 charCode != '/' && charCode != '?' && charCode != '>' &&
1568 charCode != '<' && charCode != ',' && charCode != '~')
1571 /* if there's one item or less, there is no where to go */
1572 if (infoPtr->nItemCount <= 1) return 0;
1574 /* update the search parameters */
1575 infoPtr->lastKeyPressTimestamp = GetTickCount();
1576 if (infoPtr->lastKeyPressTimestamp - lastKeyPressTimestamp < KEY_DELAY) {
1577 if (infoPtr->nSearchParamLength < MAX_PATH-1)
1578 infoPtr->szSearchParam[infoPtr->nSearchParamLength++]=charCode;
1579 if (infoPtr->charCode != charCode)
1580 infoPtr->charCode = charCode = 0;
1582 infoPtr->charCode=charCode;
1583 infoPtr->szSearchParam[0]=charCode;
1584 infoPtr->nSearchParamLength=1;
1585 /* Redundant with the 1 char string */
1589 /* and search from the current position */
1591 if (infoPtr->nFocusedItem >= 0) {
1592 endidx=infoPtr->nFocusedItem;
1594 /* if looking for single character match,
1595 * then we must always move forward
1597 if (infoPtr->nSearchParamLength == 1)
1600 endidx=infoPtr->nItemCount;
1604 /* Let application handle this for virtual listview */
1605 if (infoPtr->dwStyle & LVS_OWNERDATA)
1610 ZeroMemory(&lvfi, sizeof(lvfi));
1611 lvfi.flags = (LVFI_WRAP | LVFI_PARTIAL);
1612 infoPtr->szSearchParam[infoPtr->nSearchParamLength] = '\0';
1613 lvfi.psz = infoPtr->szSearchParam;
1617 nItem = notify_hdr(infoPtr, LVN_ODFINDITEMW, (LPNMHDR)&nmlv.hdr);
1620 LISTVIEW_KeySelection(infoPtr, nItem, FALSE);
1626 if (idx == infoPtr->nItemCount) {
1627 if (endidx == infoPtr->nItemCount || endidx == 0)
1633 item.mask = LVIF_TEXT;
1636 item.pszText = buffer;
1637 item.cchTextMax = MAX_PATH;
1638 if (!LISTVIEW_GetItemW(infoPtr, &item)) return 0;
1640 /* check for a match */
1641 if (lstrncmpiW(item.pszText,infoPtr->szSearchParam,infoPtr->nSearchParamLength) == 0) {
1644 } else if ( (charCode != 0) && (nItem == -1) && (nItem != infoPtr->nFocusedItem) &&
1645 (lstrncmpiW(item.pszText,infoPtr->szSearchParam,1) == 0) ) {
1646 /* This would work but we must keep looking for a longer match */
1650 } while (idx != endidx);
1653 LISTVIEW_KeySelection(infoPtr, nItem, FALSE);
1658 /*************************************************************************
1659 * LISTVIEW_UpdateHeaderSize [Internal]
1661 * Function to resize the header control
1664 * [I] hwnd : handle to a window
1665 * [I] nNewScrollPos : scroll pos to set
1670 static void LISTVIEW_UpdateHeaderSize(const LISTVIEW_INFO *infoPtr, INT nNewScrollPos)
1675 TRACE("nNewScrollPos=%d\n", nNewScrollPos);
1677 if (!infoPtr->hwndHeader) return;
1679 GetWindowRect(infoPtr->hwndHeader, &winRect);
1680 point[0].x = winRect.left;
1681 point[0].y = winRect.top;
1682 point[1].x = winRect.right;
1683 point[1].y = winRect.bottom;
1685 MapWindowPoints(HWND_DESKTOP, infoPtr->hwndSelf, point, 2);
1686 point[0].x = -nNewScrollPos;
1687 point[1].x += nNewScrollPos;
1689 SetWindowPos(infoPtr->hwndHeader,0,
1690 point[0].x,point[0].y,point[1].x,point[1].y,
1691 (infoPtr->dwStyle & LVS_NOCOLUMNHEADER) ? SWP_HIDEWINDOW : SWP_SHOWWINDOW |
1692 SWP_NOZORDER | SWP_NOACTIVATE);
1697 * Update the scrollbars. This functions should be called whenever
1698 * the content, size or view changes.
1701 * [I] infoPtr : valid pointer to the listview structure
1706 static void LISTVIEW_UpdateScroll(const LISTVIEW_INFO *infoPtr)
1708 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
1709 SCROLLINFO horzInfo, vertInfo;
1712 if ((infoPtr->dwStyle & LVS_NOSCROLL) || !is_redrawing(infoPtr)) return;
1714 ZeroMemory(&horzInfo, sizeof(SCROLLINFO));
1715 horzInfo.cbSize = sizeof(SCROLLINFO);
1716 horzInfo.nPage = infoPtr->rcList.right - infoPtr->rcList.left;
1718 /* for now, we'll set info.nMax to the _count_, and adjust it later */
1719 if (uView == LVS_LIST)
1721 INT nPerCol = LISTVIEW_GetCountPerColumn(infoPtr);
1722 horzInfo.nMax = (infoPtr->nItemCount + nPerCol - 1) / nPerCol;
1724 /* scroll by at least one column per page */
1725 if(horzInfo.nPage < infoPtr->nItemWidth)
1726 horzInfo.nPage = infoPtr->nItemWidth;
1728 horzInfo.nPage /= infoPtr->nItemWidth;
1730 else if (uView == LVS_REPORT)
1732 horzInfo.nMax = infoPtr->nItemWidth;
1734 else /* LVS_ICON, or LVS_SMALLICON */
1738 if (LISTVIEW_GetViewRect(infoPtr, &rcView)) horzInfo.nMax = rcView.right - rcView.left;
1741 horzInfo.fMask = SIF_RANGE | SIF_PAGE;
1742 horzInfo.nMax = max(horzInfo.nMax - 1, 0);
1743 dx = GetScrollPos(infoPtr->hwndSelf, SB_HORZ);
1744 dx -= SetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &horzInfo, TRUE);
1745 TRACE("horzInfo=%s\n", debugscrollinfo(&horzInfo));
1747 /* Setting the horizontal scroll can change the listview size
1748 * (and potentially everything else) so we need to recompute
1749 * everything again for the vertical scroll
1752 ZeroMemory(&vertInfo, sizeof(SCROLLINFO));
1753 vertInfo.cbSize = sizeof(SCROLLINFO);
1754 vertInfo.nPage = infoPtr->rcList.bottom - infoPtr->rcList.top;
1756 if (uView == LVS_REPORT)
1758 vertInfo.nMax = infoPtr->nItemCount;
1760 /* scroll by at least one page */
1761 if(vertInfo.nPage < infoPtr->nItemHeight)
1762 vertInfo.nPage = infoPtr->nItemHeight;
1764 if (infoPtr->nItemHeight > 0)
1765 vertInfo.nPage /= infoPtr->nItemHeight;
1767 else if (uView != LVS_LIST) /* LVS_ICON, or LVS_SMALLICON */
1771 if (LISTVIEW_GetViewRect(infoPtr, &rcView)) vertInfo.nMax = rcView.bottom - rcView.top;
1774 vertInfo.fMask = SIF_RANGE | SIF_PAGE;
1775 vertInfo.nMax = max(vertInfo.nMax - 1, 0);
1776 dy = GetScrollPos(infoPtr->hwndSelf, SB_VERT);
1777 dy -= SetScrollInfo(infoPtr->hwndSelf, SB_VERT, &vertInfo, TRUE);
1778 TRACE("vertInfo=%s\n", debugscrollinfo(&vertInfo));
1780 /* Change of the range may have changed the scroll pos. If so move the content */
1781 if (dx != 0 || dy != 0)
1784 listRect = infoPtr->rcList;
1785 ScrollWindowEx(infoPtr->hwndSelf, dx, dy, &listRect, &listRect, 0, 0,
1786 SW_ERASE | SW_INVALIDATE);
1789 /* Update the Header Control */
1790 if (uView == LVS_REPORT)
1792 horzInfo.fMask = SIF_POS;
1793 GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &horzInfo);
1794 LISTVIEW_UpdateHeaderSize(infoPtr, horzInfo.nPos);
1801 * Shows/hides the focus rectangle.
1804 * [I] infoPtr : valid pointer to the listview structure
1805 * [I] fShow : TRUE to show the focus, FALSE to hide it.
1810 static void LISTVIEW_ShowFocusRect(const LISTVIEW_INFO *infoPtr, BOOL fShow)
1812 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
1815 TRACE("fShow=%d, nItem=%d\n", fShow, infoPtr->nFocusedItem);
1817 if (infoPtr->nFocusedItem < 0) return;
1819 /* we need some gymnastics in ICON mode to handle large items */
1820 if ( (infoPtr->dwStyle & LVS_TYPEMASK) == LVS_ICON )
1824 LISTVIEW_GetItemBox(infoPtr, infoPtr->nFocusedItem, &rcBox);
1825 if ((rcBox.bottom - rcBox.top) > infoPtr->nItemHeight)
1827 LISTVIEW_InvalidateRect(infoPtr, &rcBox);
1832 if (!(hdc = GetDC(infoPtr->hwndSelf))) return;
1834 /* for some reason, owner draw should work only in report mode */
1835 if ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (uView == LVS_REPORT))
1840 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
1841 HFONT hOldFont = SelectObject(hdc, hFont);
1843 item.iItem = infoPtr->nFocusedItem;
1845 item.mask = LVIF_PARAM;
1846 if (!LISTVIEW_GetItemW(infoPtr, &item)) goto done;
1848 ZeroMemory(&dis, sizeof(dis));
1849 dis.CtlType = ODT_LISTVIEW;
1850 dis.CtlID = (UINT)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1851 dis.itemID = item.iItem;
1852 dis.itemAction = ODA_FOCUS;
1853 if (fShow) dis.itemState |= ODS_FOCUS;
1854 dis.hwndItem = infoPtr->hwndSelf;
1856 LISTVIEW_GetItemBox(infoPtr, dis.itemID, &dis.rcItem);
1857 dis.itemData = item.lParam;
1859 SendMessageW(infoPtr->hwndNotify, WM_DRAWITEM, dis.CtlID, (LPARAM)&dis);
1861 SelectObject(hdc, hOldFont);
1865 LISTVIEW_DrawFocusRect(infoPtr, hdc);
1868 ReleaseDC(infoPtr->hwndSelf, hdc);
1872 * Invalidates all visible selected items.
1874 static void LISTVIEW_InvalidateSelectedItems(const LISTVIEW_INFO *infoPtr)
1878 iterator_frameditems(&i, infoPtr, &infoPtr->rcList);
1879 while(iterator_next(&i))
1881 if (LISTVIEW_GetItemState(infoPtr, i.nItem, LVIS_SELECTED))
1882 LISTVIEW_InvalidateItem(infoPtr, i.nItem);
1884 iterator_destroy(&i);
1889 * DESCRIPTION: [INTERNAL]
1890 * Computes an item's (left,top) corner, relative to rcView.
1891 * That is, the position has NOT been made relative to the Origin.
1892 * This is deliberate, to avoid computing the Origin over, and
1893 * over again, when this function is called in a loop. Instead,
1894 * one can factor the computation of the Origin before the loop,
1895 * and offset the value returned by this function, on every iteration.
1898 * [I] infoPtr : valid pointer to the listview structure
1899 * [I] nItem : item number
1900 * [O] lpptOrig : item top, left corner
1905 static void LISTVIEW_GetItemOrigin(const LISTVIEW_INFO *infoPtr, INT nItem, LPPOINT lpptPosition)
1907 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
1909 assert(nItem >= 0 && nItem < infoPtr->nItemCount);
1911 if ((uView == LVS_SMALLICON) || (uView == LVS_ICON))
1913 lpptPosition->x = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosX, nItem);
1914 lpptPosition->y = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosY, nItem);
1916 else if (uView == LVS_LIST)
1918 INT nCountPerColumn = LISTVIEW_GetCountPerColumn(infoPtr);
1919 lpptPosition->x = nItem / nCountPerColumn * infoPtr->nItemWidth;
1920 lpptPosition->y = nItem % nCountPerColumn * infoPtr->nItemHeight;
1922 else /* LVS_REPORT */
1924 lpptPosition->x = 0;
1925 lpptPosition->y = nItem * infoPtr->nItemHeight;
1930 * DESCRIPTION: [INTERNAL]
1931 * Compute the rectangles of an item. This is to localize all
1932 * the computations in one place. If you are not interested in some
1933 * of these values, simply pass in a NULL -- the function is smart
1934 * enough to compute only what's necessary. The function computes
1935 * the standard rectangles (BOUNDS, ICON, LABEL) plus a non-standard
1936 * one, the BOX rectangle. This rectangle is very cheap to compute,
1937 * and is guaranteed to contain all the other rectangles. Computing
1938 * the ICON rect is also cheap, but all the others are potentially
1939 * expensive. This gives an easy and effective optimization when
1940 * searching (like point inclusion, or rectangle intersection):
1941 * first test against the BOX, and if TRUE, test against the desired
1943 * If the function does not have all the necessary information
1944 * to computed the requested rectangles, will crash with a
1945 * failed assertion. This is done so we catch all programming
1946 * errors, given that the function is called only from our code.
1948 * We have the following 'special' meanings for a few fields:
1949 * * If LVIS_FOCUSED is set, we assume the item has the focus
1950 * This is important in ICON mode, where it might get a larger
1951 * then usual rectangle
1953 * Please note that subitem support works only in REPORT mode.
1956 * [I] infoPtr : valid pointer to the listview structure
1957 * [I] lpLVItem : item to compute the measures for
1958 * [O] lprcBox : ptr to Box rectangle
1959 * Same as LVM_GETITEMRECT with LVIR_BOUNDS
1960 * [0] lprcSelectBox : ptr to select box rectangle
1961 * Same as LVM_GETITEMRECT with LVIR_SELECTEDBOUNDS
1962 * [O] lprcIcon : ptr to Icon rectangle
1963 * Same as LVM_GETITEMRECT with LVIR_ICON
1964 * [O] lprcStateIcon: ptr to State Icon rectangle
1965 * [O] lprcLabel : ptr to Label rectangle
1966 * Same as LVM_GETITEMRECT with LVIR_LABEL
1971 static void LISTVIEW_GetItemMetrics(const LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem,
1972 LPRECT lprcBox, LPRECT lprcSelectBox,
1973 LPRECT lprcIcon, LPRECT lprcStateIcon, LPRECT lprcLabel)
1975 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
1976 BOOL doSelectBox = FALSE, doIcon = FALSE, doLabel = FALSE, oversizedBox = FALSE;
1977 RECT Box, SelectBox, Icon, Label;
1978 COLUMN_INFO *lpColumnInfo = NULL;
1979 SIZE labelSize = { 0, 0 };
1981 TRACE("(lpLVItem=%s)\n", debuglvitem_t(lpLVItem, TRUE));
1983 /* Be smart and try to figure out the minimum we have to do */
1984 if (lpLVItem->iSubItem) assert(uView == LVS_REPORT);
1985 if (uView == LVS_ICON && (lprcBox || lprcLabel))
1987 assert((lpLVItem->mask & LVIF_STATE) && (lpLVItem->stateMask & LVIS_FOCUSED));
1988 if (lpLVItem->state & LVIS_FOCUSED) oversizedBox = doLabel = TRUE;
1990 if (lprcSelectBox) doSelectBox = TRUE;
1991 if (lprcLabel) doLabel = TRUE;
1992 if (doLabel || lprcIcon || lprcStateIcon) doIcon = TRUE;
1999 /************************************************************/
2000 /* compute the box rectangle (it should be cheap to do) */
2001 /************************************************************/
2002 if (lpLVItem->iSubItem || uView == LVS_REPORT)
2003 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, lpLVItem->iSubItem);
2005 if (lpLVItem->iSubItem)
2007 Box = lpColumnInfo->rcHeader;
2012 Box.right = infoPtr->nItemWidth;
2015 Box.bottom = infoPtr->nItemHeight;
2017 /******************************************************************/
2018 /* compute ICON bounding box (ala LVM_GETITEMRECT) and STATEICON */
2019 /******************************************************************/
2022 LONG state_width = 0;
2024 if (infoPtr->himlState && lpLVItem->iSubItem == 0)
2025 state_width = infoPtr->iconStateSize.cx;
2027 if (uView == LVS_ICON)
2029 Icon.left = Box.left + state_width;
2030 if (infoPtr->himlNormal)
2031 Icon.left += (infoPtr->nItemWidth - infoPtr->iconSize.cx - state_width) / 2;
2032 Icon.top = Box.top + ICON_TOP_PADDING;
2033 Icon.right = Icon.left;
2034 Icon.bottom = Icon.top;
2035 if (infoPtr->himlNormal)
2037 Icon.right += infoPtr->iconSize.cx;
2038 Icon.bottom += infoPtr->iconSize.cy;
2041 else /* LVS_SMALLICON, LVS_LIST or LVS_REPORT */
2043 Icon.left = Box.left + state_width;
2045 if (uView == LVS_REPORT)
2046 Icon.left += REPORT_MARGINX;
2049 Icon.right = Icon.left;
2050 if (infoPtr->himlSmall &&
2051 (!lpColumnInfo || lpLVItem->iSubItem == 0 || (lpColumnInfo->fmt & LVCFMT_IMAGE) ||
2052 ((infoPtr->dwLvExStyle & LVS_EX_SUBITEMIMAGES) && lpLVItem->iImage != I_IMAGECALLBACK)))
2053 Icon.right += infoPtr->iconSize.cx;
2054 Icon.bottom = Icon.top + infoPtr->iconSize.cy;
2056 if(lprcIcon) *lprcIcon = Icon;
2057 TRACE(" - icon=%s\n", wine_dbgstr_rect(&Icon));
2059 /* TODO: is this correct? */
2062 lprcStateIcon->left = Icon.left - state_width;
2063 lprcStateIcon->right = Icon.left;
2064 lprcStateIcon->top = Icon.top;
2065 lprcStateIcon->bottom = lprcStateIcon->top + infoPtr->iconSize.cy;
2066 TRACE(" - state icon=%s\n", wine_dbgstr_rect(lprcStateIcon));
2069 else Icon.right = 0;
2071 /************************************************************/
2072 /* compute LABEL bounding box (ala LVM_GETITEMRECT) */
2073 /************************************************************/
2076 /* calculate how far to the right can the label stretch */
2077 Label.right = Box.right;
2078 if (uView == LVS_REPORT)
2080 if (lpLVItem->iSubItem == 0) Label = lpColumnInfo->rcHeader;
2083 if (lpLVItem->iSubItem || ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && uView == LVS_REPORT))
2085 labelSize.cx = infoPtr->nItemWidth;
2086 labelSize.cy = infoPtr->nItemHeight;
2090 /* we need the text in non owner draw mode */
2091 assert(lpLVItem->mask & LVIF_TEXT);
2092 if (is_textT(lpLVItem->pszText, TRUE))
2094 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
2095 HDC hdc = GetDC(infoPtr->hwndSelf);
2096 HFONT hOldFont = SelectObject(hdc, hFont);
2100 /* compute rough rectangle where the label will go */
2101 SetRectEmpty(&rcText);
2102 rcText.right = infoPtr->nItemWidth - TRAILING_LABEL_PADDING;
2103 rcText.bottom = infoPtr->nItemHeight;
2104 if (uView == LVS_ICON)
2105 rcText.bottom -= ICON_TOP_PADDING + infoPtr->iconSize.cy + ICON_BOTTOM_PADDING;
2107 /* now figure out the flags */
2108 if (uView == LVS_ICON)
2109 uFormat = oversizedBox ? LV_FL_DT_FLAGS : LV_ML_DT_FLAGS;
2111 uFormat = LV_SL_DT_FLAGS;
2113 DrawTextW (hdc, lpLVItem->pszText, -1, &rcText, uFormat | DT_CALCRECT);
2115 labelSize.cx = min(rcText.right - rcText.left + TRAILING_LABEL_PADDING, infoPtr->nItemWidth);
2116 labelSize.cy = rcText.bottom - rcText.top;
2118 SelectObject(hdc, hOldFont);
2119 ReleaseDC(infoPtr->hwndSelf, hdc);
2123 if (uView == LVS_ICON)
2125 Label.left = Box.left + (infoPtr->nItemWidth - labelSize.cx) / 2;
2126 Label.top = Box.top + ICON_TOP_PADDING_HITABLE +
2127 infoPtr->iconSize.cy + ICON_BOTTOM_PADDING;
2128 Label.right = Label.left + labelSize.cx;
2129 Label.bottom = Label.top + infoPtr->nItemHeight;
2130 if (!oversizedBox && labelSize.cy > infoPtr->ntmHeight)
2132 labelSize.cy = min(Box.bottom - Label.top, labelSize.cy);
2133 labelSize.cy /= infoPtr->ntmHeight;
2134 labelSize.cy = max(labelSize.cy, 1);
2135 labelSize.cy *= infoPtr->ntmHeight;
2137 Label.bottom = Label.top + labelSize.cy + HEIGHT_PADDING;
2139 else if (uView == LVS_REPORT)
2141 Label.left = Icon.right;
2142 Label.top = Box.top;
2143 Label.right = lpColumnInfo->rcHeader.right;
2144 Label.bottom = Label.top + infoPtr->nItemHeight;
2146 else /* LVS_SMALLICON, LVS_LIST or LVS_REPORT */
2148 Label.left = Icon.right;
2149 Label.top = Box.top;
2150 Label.right = min(Label.left + labelSize.cx, Label.right);
2151 Label.bottom = Label.top + infoPtr->nItemHeight;
2154 if (lprcLabel) *lprcLabel = Label;
2155 TRACE(" - label=%s\n", wine_dbgstr_rect(&Label));
2158 /************************************************************/
2159 /* compute STATEICON bounding box */
2160 /************************************************************/
2163 if (uView == LVS_REPORT)
2165 SelectBox.left = Icon.right; /* FIXME: should be Icon.left */
2166 SelectBox.top = Box.top;
2167 SelectBox.bottom = Box.bottom;
2168 if (lpLVItem->iSubItem == 0)
2170 /* we need the indent in report mode */
2171 assert(lpLVItem->mask & LVIF_INDENT);
2172 SelectBox.left += infoPtr->iconSize.cx * lpLVItem->iIndent;
2174 SelectBox.right = min(SelectBox.left + labelSize.cx, Label.right);
2178 UnionRect(&SelectBox, &Icon, &Label);
2180 if (lprcSelectBox) *lprcSelectBox = SelectBox;
2181 TRACE(" - select box=%s\n", wine_dbgstr_rect(&SelectBox));
2184 /* Fix the Box if necessary */
2187 if (oversizedBox) UnionRect(lprcBox, &Box, &Label);
2188 else *lprcBox = Box;
2190 TRACE(" - box=%s\n", wine_dbgstr_rect(&Box));
2194 * DESCRIPTION: [INTERNAL]
2197 * [I] infoPtr : valid pointer to the listview structure
2198 * [I] nItem : item number
2199 * [O] lprcBox : ptr to Box rectangle
2204 static void LISTVIEW_GetItemBox(const LISTVIEW_INFO *infoPtr, INT nItem, LPRECT lprcBox)
2206 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
2207 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
2208 POINT Position, Origin;
2211 LISTVIEW_GetOrigin(infoPtr, &Origin);
2212 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
2214 /* Be smart and try to figure out the minimum we have to do */
2216 if (uView == LVS_ICON && infoPtr->bFocus && LISTVIEW_GetItemState(infoPtr, nItem, LVIS_FOCUSED))
2217 lvItem.mask |= LVIF_TEXT;
2218 lvItem.iItem = nItem;
2219 lvItem.iSubItem = 0;
2220 lvItem.pszText = szDispText;
2221 lvItem.cchTextMax = DISP_TEXT_SIZE;
2222 if (lvItem.mask) LISTVIEW_GetItemW(infoPtr, &lvItem);
2223 if (uView == LVS_ICON)
2225 lvItem.mask |= LVIF_STATE;
2226 lvItem.stateMask = LVIS_FOCUSED;
2227 lvItem.state = (lvItem.mask & LVIF_TEXT ? LVIS_FOCUSED : 0);
2229 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, lprcBox, 0, 0, 0, 0);
2231 OffsetRect(lprcBox, Position.x + Origin.x, Position.y + Origin.y);
2237 * Returns the current icon position, and advances it along the top.
2238 * The returned position is not offset by Origin.
2241 * [I] infoPtr : valid pointer to the listview structure
2242 * [O] lpPos : will get the current icon position
2247 static void LISTVIEW_NextIconPosTop(LISTVIEW_INFO *infoPtr, LPPOINT lpPos)
2249 INT nListWidth = infoPtr->rcList.right - infoPtr->rcList.left;
2251 *lpPos = infoPtr->currIconPos;
2253 infoPtr->currIconPos.x += infoPtr->nItemWidth;
2254 if (infoPtr->currIconPos.x + infoPtr->nItemWidth <= nListWidth) return;
2256 infoPtr->currIconPos.x = 0;
2257 infoPtr->currIconPos.y += infoPtr->nItemHeight;
2263 * Returns the current icon position, and advances it down the left edge.
2264 * The returned position is not offset by Origin.
2267 * [I] infoPtr : valid pointer to the listview structure
2268 * [O] lpPos : will get the current icon position
2273 static void LISTVIEW_NextIconPosLeft(LISTVIEW_INFO *infoPtr, LPPOINT lpPos)
2275 INT nListHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
2277 *lpPos = infoPtr->currIconPos;
2279 infoPtr->currIconPos.y += infoPtr->nItemHeight;
2280 if (infoPtr->currIconPos.y + infoPtr->nItemHeight <= nListHeight) return;
2282 infoPtr->currIconPos.x += infoPtr->nItemWidth;
2283 infoPtr->currIconPos.y = 0;
2289 * Moves an icon to the specified position.
2290 * It takes care of invalidating the item, etc.
2293 * [I] infoPtr : valid pointer to the listview structure
2294 * [I] nItem : the item to move
2295 * [I] lpPos : the new icon position
2296 * [I] isNew : flags the item as being new
2302 static BOOL LISTVIEW_MoveIconTo(const LISTVIEW_INFO *infoPtr, INT nItem, const POINT *lppt, BOOL isNew)
2308 old.x = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosX, nItem);
2309 old.y = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosY, nItem);
2311 if (lppt->x == old.x && lppt->y == old.y) return TRUE;
2312 LISTVIEW_InvalidateItem(infoPtr, nItem);
2315 /* Allocating a POINTER for every item is too resource intensive,
2316 * so we'll keep the (x,y) in different arrays */
2317 if (!DPA_SetPtr(infoPtr->hdpaPosX, nItem, (void *)(LONG_PTR)lppt->x)) return FALSE;
2318 if (!DPA_SetPtr(infoPtr->hdpaPosY, nItem, (void *)(LONG_PTR)lppt->y)) return FALSE;
2320 LISTVIEW_InvalidateItem(infoPtr, nItem);
2327 * Arranges listview items in icon display mode.
2330 * [I] infoPtr : valid pointer to the listview structure
2331 * [I] nAlignCode : alignment code
2337 static BOOL LISTVIEW_Arrange(LISTVIEW_INFO *infoPtr, INT nAlignCode)
2339 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
2340 void (*next_pos)(LISTVIEW_INFO *, LPPOINT);
2344 if (uView != LVS_ICON && uView != LVS_SMALLICON) return FALSE;
2346 TRACE("nAlignCode=%d\n", nAlignCode);
2348 if (nAlignCode == LVA_DEFAULT)
2350 if (infoPtr->dwStyle & LVS_ALIGNLEFT) nAlignCode = LVA_ALIGNLEFT;
2351 else nAlignCode = LVA_ALIGNTOP;
2356 case LVA_ALIGNLEFT: next_pos = LISTVIEW_NextIconPosLeft; break;
2357 case LVA_ALIGNTOP: next_pos = LISTVIEW_NextIconPosTop; break;
2358 case LVA_SNAPTOGRID: next_pos = LISTVIEW_NextIconPosTop; break; /* FIXME */
2359 default: return FALSE;
2362 infoPtr->bAutoarrange = TRUE;
2363 infoPtr->currIconPos.x = infoPtr->currIconPos.y = 0;
2364 for (i = 0; i < infoPtr->nItemCount; i++)
2366 next_pos(infoPtr, &pos);
2367 LISTVIEW_MoveIconTo(infoPtr, i, &pos, FALSE);
2375 * Retrieves the bounding rectangle of all the items, not offset by Origin.
2378 * [I] infoPtr : valid pointer to the listview structure
2379 * [O] lprcView : bounding rectangle
2385 static void LISTVIEW_GetAreaRect(const LISTVIEW_INFO *infoPtr, LPRECT lprcView)
2389 SetRectEmpty(lprcView);
2391 switch (infoPtr->dwStyle & LVS_TYPEMASK)
2395 for (i = 0; i < infoPtr->nItemCount; i++)
2397 x = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosX, i);
2398 y = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosY, i);
2399 lprcView->right = max(lprcView->right, x);
2400 lprcView->bottom = max(lprcView->bottom, y);
2402 if (infoPtr->nItemCount > 0)
2404 lprcView->right += infoPtr->nItemWidth;
2405 lprcView->bottom += infoPtr->nItemHeight;
2410 y = LISTVIEW_GetCountPerColumn(infoPtr);
2411 x = infoPtr->nItemCount / y;
2412 if (infoPtr->nItemCount % y) x++;
2413 lprcView->right = x * infoPtr->nItemWidth;
2414 lprcView->bottom = y * infoPtr->nItemHeight;
2418 lprcView->right = infoPtr->nItemWidth;
2419 lprcView->bottom = infoPtr->nItemCount * infoPtr->nItemHeight;
2426 * Retrieves the bounding rectangle of all the items.
2429 * [I] infoPtr : valid pointer to the listview structure
2430 * [O] lprcView : bounding rectangle
2436 static BOOL LISTVIEW_GetViewRect(const LISTVIEW_INFO *infoPtr, LPRECT lprcView)
2440 TRACE("(lprcView=%p)\n", lprcView);
2442 if (!lprcView) return FALSE;
2444 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
2445 LISTVIEW_GetAreaRect(infoPtr, lprcView);
2446 OffsetRect(lprcView, ptOrigin.x, ptOrigin.y);
2448 TRACE("lprcView=%s\n", wine_dbgstr_rect(lprcView));
2455 * Retrieves the subitem pointer associated with the subitem index.
2458 * [I] hdpaSubItems : DPA handle for a specific item
2459 * [I] nSubItem : index of subitem
2462 * SUCCESS : subitem pointer
2465 static SUBITEM_INFO* LISTVIEW_GetSubItemPtr(HDPA hdpaSubItems, INT nSubItem)
2467 SUBITEM_INFO *lpSubItem;
2470 /* we should binary search here if need be */
2471 for (i = 1; i < DPA_GetPtrCount(hdpaSubItems); i++)
2473 lpSubItem = DPA_GetPtr(hdpaSubItems, i);
2474 if (lpSubItem->iSubItem == nSubItem)
2484 * Calculates the desired item width.
2487 * [I] infoPtr : valid pointer to the listview structure
2490 * The desired item width.
2492 static INT LISTVIEW_CalculateItemWidth(const LISTVIEW_INFO *infoPtr)
2494 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
2497 TRACE("uView=%d\n", uView);
2499 if (uView == LVS_ICON)
2500 nItemWidth = infoPtr->iconSpacing.cx;
2501 else if (uView == LVS_REPORT)
2505 if (DPA_GetPtrCount(infoPtr->hdpaColumns) > 0)
2507 LISTVIEW_GetHeaderRect(infoPtr, DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, &rcHeader);
2508 nItemWidth = rcHeader.right;
2511 else /* LVS_SMALLICON, or LVS_LIST */
2515 for (i = 0; i < infoPtr->nItemCount; i++)
2516 nItemWidth = max(LISTVIEW_GetLabelWidth(infoPtr, i), nItemWidth);
2518 if (infoPtr->himlSmall) nItemWidth += infoPtr->iconSize.cx;
2519 if (infoPtr->himlState) nItemWidth += infoPtr->iconStateSize.cx;
2521 nItemWidth = max(DEFAULT_COLUMN_WIDTH, nItemWidth + WIDTH_PADDING);
2524 return max(nItemWidth, 1);
2529 * Calculates the desired item height.
2532 * [I] infoPtr : valid pointer to the listview structure
2535 * The desired item height.
2537 static INT LISTVIEW_CalculateItemHeight(const LISTVIEW_INFO *infoPtr)
2539 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
2542 TRACE("uView=%d\n", uView);
2544 if (uView == LVS_ICON)
2545 nItemHeight = infoPtr->iconSpacing.cy;
2548 nItemHeight = infoPtr->ntmHeight;
2549 if (uView == LVS_REPORT && infoPtr->dwLvExStyle & LVS_EX_GRIDLINES)
2551 if (infoPtr->himlState)
2552 nItemHeight = max(nItemHeight, infoPtr->iconStateSize.cy);
2553 if (infoPtr->himlSmall)
2554 nItemHeight = max(nItemHeight, infoPtr->iconSize.cy);
2555 if (infoPtr->himlState || infoPtr->himlSmall)
2556 nItemHeight += HEIGHT_PADDING;
2557 if (infoPtr->nMeasureItemHeight > 0)
2558 nItemHeight = infoPtr->nMeasureItemHeight;
2561 return max(nItemHeight, 1);
2566 * Updates the width, and height of an item.
2569 * [I] infoPtr : valid pointer to the listview structure
2574 static inline void LISTVIEW_UpdateItemSize(LISTVIEW_INFO *infoPtr)
2576 infoPtr->nItemWidth = LISTVIEW_CalculateItemWidth(infoPtr);
2577 infoPtr->nItemHeight = LISTVIEW_CalculateItemHeight(infoPtr);
2583 * Retrieves and saves important text metrics info for the current
2587 * [I] infoPtr : valid pointer to the listview structure
2590 static void LISTVIEW_SaveTextMetrics(LISTVIEW_INFO *infoPtr)
2592 HDC hdc = GetDC(infoPtr->hwndSelf);
2593 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
2594 HFONT hOldFont = SelectObject(hdc, hFont);
2598 if (GetTextMetricsW(hdc, &tm))
2600 infoPtr->ntmHeight = tm.tmHeight;
2601 infoPtr->ntmMaxCharWidth = tm.tmMaxCharWidth;
2604 if (GetTextExtentPoint32A(hdc, "...", 3, &sz))
2605 infoPtr->nEllipsisWidth = sz.cx;
2607 SelectObject(hdc, hOldFont);
2608 ReleaseDC(infoPtr->hwndSelf, hdc);
2610 TRACE("tmHeight=%d\n", infoPtr->ntmHeight);
2615 * A compare function for ranges
2618 * [I] range1 : pointer to range 1;
2619 * [I] range2 : pointer to range 2;
2623 * > 0 : if range 1 > range 2
2624 * < 0 : if range 2 > range 1
2625 * = 0 : if range intersects range 2
2627 static INT CALLBACK ranges_cmp(LPVOID range1, LPVOID range2, LPARAM flags)
2631 if (((RANGE*)range1)->upper <= ((RANGE*)range2)->lower)
2633 else if (((RANGE*)range2)->upper <= ((RANGE*)range1)->lower)
2638 TRACE("range1=%s, range2=%s, cmp=%d\n", debugrange(range1), debugrange(range2), cmp);
2644 #define ranges_check(ranges, desc) ranges_assert(ranges, desc, __FUNCTION__, __LINE__)
2646 #define ranges_check(ranges, desc) do { } while(0)
2649 static void ranges_assert(RANGES ranges, LPCSTR desc, const char *func, int line)
2654 TRACE("*** Checking %s:%d:%s ***\n", func, line, desc);
2656 assert (DPA_GetPtrCount(ranges->hdpa) >= 0);
2657 ranges_dump(ranges);
2658 if (DPA_GetPtrCount(ranges->hdpa) > 0)
2660 prev = DPA_GetPtr(ranges->hdpa, 0);
2661 assert (prev->lower >= 0 && prev->lower < prev->upper);
2662 for (i = 1; i < DPA_GetPtrCount(ranges->hdpa); i++)
2664 curr = DPA_GetPtr(ranges->hdpa, i);
2665 assert (prev->upper <= curr->lower);
2666 assert (curr->lower < curr->upper);
2670 TRACE("--- Done checking---\n");
2673 static RANGES ranges_create(int count)
2675 RANGES ranges = Alloc(sizeof(struct tagRANGES));
2676 if (!ranges) return NULL;
2677 ranges->hdpa = DPA_Create(count);
2678 if (ranges->hdpa) return ranges;
2683 static void ranges_clear(RANGES ranges)
2687 for(i = 0; i < DPA_GetPtrCount(ranges->hdpa); i++)
2688 Free(DPA_GetPtr(ranges->hdpa, i));
2689 DPA_DeleteAllPtrs(ranges->hdpa);
2693 static void ranges_destroy(RANGES ranges)
2695 if (!ranges) return;
2696 ranges_clear(ranges);
2697 DPA_Destroy(ranges->hdpa);
2701 static RANGES ranges_clone(RANGES ranges)
2706 if (!(clone = ranges_create(DPA_GetPtrCount(ranges->hdpa)))) goto fail;
2708 for (i = 0; i < DPA_GetPtrCount(ranges->hdpa); i++)
2710 RANGE *newrng = Alloc(sizeof(RANGE));
2711 if (!newrng) goto fail;
2712 *newrng = *((RANGE*)DPA_GetPtr(ranges->hdpa, i));
2713 DPA_SetPtr(clone->hdpa, i, newrng);
2718 TRACE ("clone failed\n");
2719 ranges_destroy(clone);
2723 static RANGES ranges_diff(RANGES ranges, RANGES sub)
2727 for (i = 0; i < DPA_GetPtrCount(sub->hdpa); i++)
2728 ranges_del(ranges, *((RANGE *)DPA_GetPtr(sub->hdpa, i)));
2733 static void ranges_dump(RANGES ranges)
2737 for (i = 0; i < DPA_GetPtrCount(ranges->hdpa); i++)
2738 TRACE(" %s\n", debugrange(DPA_GetPtr(ranges->hdpa, i)));
2741 static inline BOOL ranges_contain(RANGES ranges, INT nItem)
2743 RANGE srchrng = { nItem, nItem + 1 };
2745 TRACE("(nItem=%d)\n", nItem);
2746 ranges_check(ranges, "before contain");
2747 return DPA_Search(ranges->hdpa, &srchrng, 0, ranges_cmp, 0, DPAS_SORTED) != -1;
2750 static INT ranges_itemcount(RANGES ranges)
2754 for (i = 0; i < DPA_GetPtrCount(ranges->hdpa); i++)
2756 RANGE *sel = DPA_GetPtr(ranges->hdpa, i);
2757 count += sel->upper - sel->lower;
2763 static BOOL ranges_shift(RANGES ranges, INT nItem, INT delta, INT nUpper)
2765 RANGE srchrng = { nItem, nItem + 1 }, *chkrng;
2768 index = DPA_Search(ranges->hdpa, &srchrng, 0, ranges_cmp, 0, DPAS_SORTED | DPAS_INSERTAFTER);
2769 if (index == -1) return TRUE;
2771 for (; index < DPA_GetPtrCount(ranges->hdpa); index++)
2773 chkrng = DPA_GetPtr(ranges->hdpa, index);
2774 if (chkrng->lower >= nItem)
2775 chkrng->lower = max(min(chkrng->lower + delta, nUpper - 1), 0);
2776 if (chkrng->upper > nItem)
2777 chkrng->upper = max(min(chkrng->upper + delta, nUpper), 0);
2782 static BOOL ranges_add(RANGES ranges, RANGE range)
2787 TRACE("(%s)\n", debugrange(&range));
2788 ranges_check(ranges, "before add");
2790 /* try find overlapping regions first */
2791 srchrgn.lower = range.lower - 1;
2792 srchrgn.upper = range.upper + 1;
2793 index = DPA_Search(ranges->hdpa, &srchrgn, 0, ranges_cmp, 0, DPAS_SORTED);
2799 TRACE("Adding new range\n");
2801 /* create the brand new range to insert */
2802 newrgn = Alloc(sizeof(RANGE));
2803 if(!newrgn) goto fail;
2806 /* figure out where to insert it */
2807 index = DPA_Search(ranges->hdpa, newrgn, 0, ranges_cmp, 0, DPAS_SORTED | DPAS_INSERTAFTER);
2808 TRACE("index=%d\n", index);
2809 if (index == -1) index = 0;
2811 /* and get it over with */
2812 if (DPA_InsertPtr(ranges->hdpa, index, newrgn) == -1)
2820 RANGE *chkrgn, *mrgrgn;
2821 INT fromindex, mergeindex;
2823 chkrgn = DPA_GetPtr(ranges->hdpa, index);
2824 TRACE("Merge with %s @%d\n", debugrange(chkrgn), index);
2826 chkrgn->lower = min(range.lower, chkrgn->lower);
2827 chkrgn->upper = max(range.upper, chkrgn->upper);
2829 TRACE("New range %s @%d\n", debugrange(chkrgn), index);
2831 /* merge now common ranges */
2833 srchrgn.lower = chkrgn->lower - 1;
2834 srchrgn.upper = chkrgn->upper + 1;
2838 mergeindex = DPA_Search(ranges->hdpa, &srchrgn, fromindex, ranges_cmp, 0, 0);
2839 if (mergeindex == -1) break;
2840 if (mergeindex == index)
2842 fromindex = index + 1;
2846 TRACE("Merge with index %i\n", mergeindex);
2848 mrgrgn = DPA_GetPtr(ranges->hdpa, mergeindex);
2849 chkrgn->lower = min(chkrgn->lower, mrgrgn->lower);
2850 chkrgn->upper = max(chkrgn->upper, mrgrgn->upper);
2852 DPA_DeletePtr(ranges->hdpa, mergeindex);
2853 if (mergeindex < index) index --;
2857 ranges_check(ranges, "after add");
2861 ranges_check(ranges, "failed add");
2865 static BOOL ranges_del(RANGES ranges, RANGE range)
2870 TRACE("(%s)\n", debugrange(&range));
2871 ranges_check(ranges, "before del");
2873 /* we don't use DPAS_SORTED here, since we need *
2874 * to find the first overlapping range */
2875 index = DPA_Search(ranges->hdpa, &range, 0, ranges_cmp, 0, 0);
2878 chkrgn = DPA_GetPtr(ranges->hdpa, index);
2880 TRACE("Matches range %s @%d\n", debugrange(chkrgn), index);
2882 /* case 1: Same range */
2883 if ( (chkrgn->upper == range.upper) &&
2884 (chkrgn->lower == range.lower) )
2886 DPA_DeletePtr(ranges->hdpa, index);
2889 /* case 2: engulf */
2890 else if ( (chkrgn->upper <= range.upper) &&
2891 (chkrgn->lower >= range.lower) )
2893 DPA_DeletePtr(ranges->hdpa, index);
2895 /* case 3: overlap upper */
2896 else if ( (chkrgn->upper <= range.upper) &&
2897 (chkrgn->lower < range.lower) )
2899 chkrgn->upper = range.lower;
2901 /* case 4: overlap lower */
2902 else if ( (chkrgn->upper > range.upper) &&
2903 (chkrgn->lower >= range.lower) )
2905 chkrgn->lower = range.upper;
2908 /* case 5: fully internal */
2911 RANGE tmprgn = *chkrgn, *newrgn;
2913 if (!(newrgn = Alloc(sizeof(RANGE)))) goto fail;
2914 newrgn->lower = chkrgn->lower;
2915 newrgn->upper = range.lower;
2916 chkrgn->lower = range.upper;
2917 if (DPA_InsertPtr(ranges->hdpa, index, newrgn) == -1)
2926 index = DPA_Search(ranges->hdpa, &range, index, ranges_cmp, 0, 0);
2929 ranges_check(ranges, "after del");
2933 ranges_check(ranges, "failed del");
2939 * Removes all selection ranges
2942 * [I] infoPtr : valid pointer to the listview structure
2943 * [I] toSkip : item range to skip removing the selection
2949 static BOOL LISTVIEW_DeselectAllSkipItems(LISTVIEW_INFO *infoPtr, RANGES toSkip)
2958 lvItem.stateMask = LVIS_SELECTED;
2960 /* need to clone the DPA because callbacks can change it */
2961 if (!(clone = ranges_clone(infoPtr->selectionRanges))) return FALSE;
2962 iterator_rangesitems(&i, ranges_diff(clone, toSkip));
2963 while(iterator_next(&i))
2964 LISTVIEW_SetItemState(infoPtr, i.nItem, &lvItem);
2965 /* note that the iterator destructor will free the cloned range */
2966 iterator_destroy(&i);
2971 static inline BOOL LISTVIEW_DeselectAllSkipItem(LISTVIEW_INFO *infoPtr, INT nItem)
2975 if (!(toSkip = ranges_create(1))) return FALSE;
2976 if (nItem != -1) ranges_additem(toSkip, nItem);
2977 LISTVIEW_DeselectAllSkipItems(infoPtr, toSkip);
2978 ranges_destroy(toSkip);
2982 static inline BOOL LISTVIEW_DeselectAll(LISTVIEW_INFO *infoPtr)
2984 return LISTVIEW_DeselectAllSkipItem(infoPtr, -1);
2989 * Retrieves the number of items that are marked as selected.
2992 * [I] infoPtr : valid pointer to the listview structure
2995 * Number of items selected.
2997 static INT LISTVIEW_GetSelectedCount(const LISTVIEW_INFO *infoPtr)
2999 INT nSelectedCount = 0;
3001 if (infoPtr->uCallbackMask & LVIS_SELECTED)
3004 for (i = 0; i < infoPtr->nItemCount; i++)
3006 if (LISTVIEW_GetItemState(infoPtr, i, LVIS_SELECTED))
3011 nSelectedCount = ranges_itemcount(infoPtr->selectionRanges);
3013 TRACE("nSelectedCount=%d\n", nSelectedCount);
3014 return nSelectedCount;
3019 * Manages the item focus.
3022 * [I] infoPtr : valid pointer to the listview structure
3023 * [I] nItem : item index
3026 * TRUE : focused item changed
3027 * FALSE : focused item has NOT changed
3029 static inline BOOL LISTVIEW_SetItemFocus(LISTVIEW_INFO *infoPtr, INT nItem)
3031 INT oldFocus = infoPtr->nFocusedItem;
3034 if (nItem == infoPtr->nFocusedItem) return FALSE;
3036 lvItem.state = nItem == -1 ? 0 : LVIS_FOCUSED;
3037 lvItem.stateMask = LVIS_FOCUSED;
3038 LISTVIEW_SetItemState(infoPtr, nItem == -1 ? infoPtr->nFocusedItem : nItem, &lvItem);
3040 return oldFocus != infoPtr->nFocusedItem;
3043 /* Helper function for LISTVIEW_ShiftIndices *only* */
3044 static INT shift_item(const LISTVIEW_INFO *infoPtr, INT nShiftItem, INT nItem, INT direction)
3046 if (nShiftItem < nItem) return nShiftItem;
3048 if (nShiftItem > nItem) return nShiftItem + direction;
3050 if (direction > 0) return nShiftItem + direction;
3052 return min(nShiftItem, infoPtr->nItemCount - 1);
3057 * Updates the various indices after an item has been inserted or deleted.
3060 * [I] infoPtr : valid pointer to the listview structure
3061 * [I] nItem : item index
3062 * [I] direction : Direction of shift, +1 or -1.
3067 static void LISTVIEW_ShiftIndices(LISTVIEW_INFO *infoPtr, INT nItem, INT direction)
3072 /* temporarily disable change notification while shifting items */
3073 bOldChange = infoPtr->bDoChangeNotify;
3074 infoPtr->bDoChangeNotify = FALSE;
3076 TRACE("Shifting %iu, %i steps\n", nItem, direction);
3078 ranges_shift(infoPtr->selectionRanges, nItem, direction, infoPtr->nItemCount);
3080 assert(abs(direction) == 1);
3082 infoPtr->nSelectionMark = shift_item(infoPtr, infoPtr->nSelectionMark, nItem, direction);
3084 nNewFocus = shift_item(infoPtr, infoPtr->nFocusedItem, nItem, direction);
3085 if (nNewFocus != infoPtr->nFocusedItem)
3086 LISTVIEW_SetItemFocus(infoPtr, nNewFocus);
3088 /* But we are not supposed to modify nHotItem! */
3090 infoPtr->bDoChangeNotify = bOldChange;
3096 * Adds a block of selections.
3099 * [I] infoPtr : valid pointer to the listview structure
3100 * [I] nItem : item index
3103 * Whether the window is still valid.
3105 static BOOL LISTVIEW_AddGroupSelection(LISTVIEW_INFO *infoPtr, INT nItem)
3107 INT nFirst = min(infoPtr->nSelectionMark, nItem);
3108 INT nLast = max(infoPtr->nSelectionMark, nItem);
3109 HWND hwndSelf = infoPtr->hwndSelf;
3110 NMLVODSTATECHANGE nmlv;
3115 /* Temporarily disable change notification
3116 * If the control is LVS_OWNERDATA, we need to send
3117 * only one LVN_ODSTATECHANGED notification.
3118 * See MSDN documentation for LVN_ITEMCHANGED.
3120 bOldChange = infoPtr->bDoChangeNotify;
3121 if (infoPtr->dwStyle & LVS_OWNERDATA) infoPtr->bDoChangeNotify = FALSE;
3123 if (nFirst == -1) nFirst = nItem;
3125 item.state = LVIS_SELECTED;
3126 item.stateMask = LVIS_SELECTED;
3128 for (i = nFirst; i <= nLast; i++)
3129 LISTVIEW_SetItemState(infoPtr,i,&item);
3131 ZeroMemory(&nmlv, sizeof(nmlv));
3132 nmlv.iFrom = nFirst;
3135 nmlv.uOldState = item.state;
3137 notify_hdr(infoPtr, LVN_ODSTATECHANGED, (LPNMHDR)&nmlv);
3138 if (!IsWindow(hwndSelf))
3140 infoPtr->bDoChangeNotify = bOldChange;
3147 * Sets a single group selection.
3150 * [I] infoPtr : valid pointer to the listview structure
3151 * [I] nItem : item index
3156 static void LISTVIEW_SetGroupSelection(LISTVIEW_INFO *infoPtr, INT nItem)
3158 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
3164 if (!(selection = ranges_create(100))) return;
3166 item.state = LVIS_SELECTED;
3167 item.stateMask = LVIS_SELECTED;
3169 if ((uView == LVS_LIST) || (uView == LVS_REPORT))
3171 if (infoPtr->nSelectionMark == -1)
3173 infoPtr->nSelectionMark = nItem;
3174 ranges_additem(selection, nItem);
3180 sel.lower = min(infoPtr->nSelectionMark, nItem);
3181 sel.upper = max(infoPtr->nSelectionMark, nItem) + 1;
3182 ranges_add(selection, sel);
3187 RECT rcItem, rcSel, rcSelMark;
3190 rcItem.left = LVIR_BOUNDS;
3191 if (!LISTVIEW_GetItemRect(infoPtr, nItem, &rcItem)) return;
3192 rcSelMark.left = LVIR_BOUNDS;
3193 if (!LISTVIEW_GetItemRect(infoPtr, infoPtr->nSelectionMark, &rcSelMark)) return;
3194 UnionRect(&rcSel, &rcItem, &rcSelMark);
3195 iterator_frameditems(&i, infoPtr, &rcSel);
3196 while(iterator_next(&i))
3198 LISTVIEW_GetItemPosition(infoPtr, i.nItem, &ptItem);
3199 if (PtInRect(&rcSel, ptItem)) ranges_additem(selection, i.nItem);
3201 iterator_destroy(&i);
3204 /* disable per item notifications on LVS_OWNERDATA style
3205 FIXME: single LVN_ODSTATECHANGED should be used */
3206 bOldChange = infoPtr->bDoChangeNotify;
3207 if (infoPtr->dwStyle & LVS_OWNERDATA) infoPtr->bDoChangeNotify = FALSE;
3209 LISTVIEW_DeselectAllSkipItems(infoPtr, selection);
3212 iterator_rangesitems(&i, selection);
3213 while(iterator_next(&i))
3214 LISTVIEW_SetItemState(infoPtr, i.nItem, &item);
3215 /* this will also destroy the selection */
3216 iterator_destroy(&i);
3218 infoPtr->bDoChangeNotify = bOldChange;
3220 LISTVIEW_SetItemFocus(infoPtr, nItem);
3225 * Sets a single selection.
3228 * [I] infoPtr : valid pointer to the listview structure
3229 * [I] nItem : item index
3234 static void LISTVIEW_SetSelection(LISTVIEW_INFO *infoPtr, INT nItem)
3238 TRACE("nItem=%d\n", nItem);
3240 LISTVIEW_DeselectAllSkipItem(infoPtr, nItem);
3242 lvItem.state = LVIS_FOCUSED | LVIS_SELECTED;
3243 lvItem.stateMask = LVIS_FOCUSED | LVIS_SELECTED;
3244 LISTVIEW_SetItemState(infoPtr, nItem, &lvItem);
3246 infoPtr->nSelectionMark = nItem;
3251 * Set selection(s) with keyboard.
3254 * [I] infoPtr : valid pointer to the listview structure
3255 * [I] nItem : item index
3256 * [I] space : VK_SPACE code sent
3259 * SUCCESS : TRUE (needs to be repainted)
3260 * FAILURE : FALSE (nothing has changed)
3262 static BOOL LISTVIEW_KeySelection(LISTVIEW_INFO *infoPtr, INT nItem, BOOL space)
3264 /* FIXME: pass in the state */
3265 WORD wShift = HIWORD(GetKeyState(VK_SHIFT));
3266 WORD wCtrl = HIWORD(GetKeyState(VK_CONTROL));
3267 BOOL bResult = FALSE;
3269 TRACE("nItem=%d, wShift=%d, wCtrl=%d\n", nItem, wShift, wCtrl);
3270 if ((nItem >= 0) && (nItem < infoPtr->nItemCount))
3272 if (infoPtr->dwStyle & LVS_SINGLESEL)
3275 LISTVIEW_SetSelection(infoPtr, nItem);
3282 LISTVIEW_SetGroupSelection(infoPtr, nItem);
3287 lvItem.state = ~LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED);
3288 lvItem.stateMask = LVIS_SELECTED;
3291 LISTVIEW_SetItemState(infoPtr, nItem, &lvItem);
3292 if (lvItem.state & LVIS_SELECTED)
3293 infoPtr->nSelectionMark = nItem;
3295 bResult = LISTVIEW_SetItemFocus(infoPtr, nItem);
3300 LISTVIEW_SetSelection(infoPtr, nItem);
3303 LISTVIEW_EnsureVisible(infoPtr, nItem, FALSE);
3306 UpdateWindow(infoPtr->hwndSelf); /* update client area */
3310 static BOOL LISTVIEW_GetItemAtPt(const LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem, POINT pt)
3312 LVHITTESTINFO lvHitTestInfo;
3314 ZeroMemory(&lvHitTestInfo, sizeof(lvHitTestInfo));
3315 lvHitTestInfo.pt.x = pt.x;
3316 lvHitTestInfo.pt.y = pt.y;
3318 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
3320 lpLVItem->mask = LVIF_PARAM;
3321 lpLVItem->iItem = lvHitTestInfo.iItem;
3322 lpLVItem->iSubItem = 0;
3324 return LISTVIEW_GetItemT(infoPtr, lpLVItem, TRUE);
3327 static inline BOOL LISTVIEW_isHotTracking(const LISTVIEW_INFO *infoPtr)
3329 return ((infoPtr->dwLvExStyle & LVS_EX_TRACKSELECT) ||
3330 (infoPtr->dwLvExStyle & LVS_EX_ONECLICKACTIVATE) ||
3331 (infoPtr->dwLvExStyle & LVS_EX_TWOCLICKACTIVATE));
3336 * Called when the mouse is being actively tracked and has hovered for a specified
3340 * [I] infoPtr : valid pointer to the listview structure
3341 * [I] fwKeys : key indicator
3342 * [I] x,y : mouse position
3345 * 0 if the message was processed, non-zero if there was an error
3348 * LVS_EX_TRACKSELECT: An item is automatically selected when the cursor remains
3349 * over the item for a certain period of time.
3352 static LRESULT LISTVIEW_MouseHover(LISTVIEW_INFO *infoPtr, WORD fwKeys, INT x, INT y)
3354 if (LISTVIEW_isHotTracking(infoPtr))
3362 if (LISTVIEW_GetItemAtPt(infoPtr, &item, pt))
3363 LISTVIEW_SetSelection(infoPtr, item.iItem);
3371 * Called whenever WM_MOUSEMOVE is received.
3374 * [I] infoPtr : valid pointer to the listview structure
3375 * [I] fwKeys : key indicator
3376 * [I] x,y : mouse position
3379 * 0 if the message is processed, non-zero if there was an error
3381 static LRESULT LISTVIEW_MouseMove(LISTVIEW_INFO *infoPtr, WORD fwKeys, INT x, INT y)
3383 TRACKMOUSEEVENT trackinfo;
3385 if (!(fwKeys & MK_LBUTTON))
3386 infoPtr->bLButtonDown = FALSE;
3388 if (infoPtr->bLButtonDown)
3392 LVHITTESTINFO lvHitTestInfo;
3393 WORD wDragWidth = GetSystemMetrics(SM_CXDRAG);
3394 WORD wDragHeight= GetSystemMetrics(SM_CYDRAG);
3396 rect.left = infoPtr->ptClickPos.x - wDragWidth;
3397 rect.right = infoPtr->ptClickPos.x + wDragWidth;
3398 rect.top = infoPtr->ptClickPos.y - wDragHeight;
3399 rect.bottom = infoPtr->ptClickPos.y + wDragHeight;
3404 lvHitTestInfo.pt = tmp;
3405 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, TRUE);
3407 /* reset item marker */
3408 if (infoPtr->nLButtonDownItem != lvHitTestInfo.iItem)
3409 infoPtr->nLButtonDownItem = -1;
3411 if (!PtInRect(&rect, tmp))
3415 /* this path covers the following:
3416 1. WM_LBUTTONDOWN over selected item (sets focus on it)
3417 2. change focus with keys
3418 3. move mouse over item from step 1 selects it and moves focus on it */
3419 if (infoPtr->nLButtonDownItem != -1 &&
3420 !LISTVIEW_GetItemState(infoPtr, infoPtr->nLButtonDownItem, LVIS_SELECTED))
3424 lvItem.state = LVIS_FOCUSED | LVIS_SELECTED;
3425 lvItem.stateMask = LVIS_FOCUSED | LVIS_SELECTED;
3427 LISTVIEW_SetItemState(infoPtr, infoPtr->nLButtonDownItem, &lvItem);
3428 infoPtr->nLButtonDownItem = -1;
3431 lvHitTestInfo.pt = infoPtr->ptClickPos;
3432 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, TRUE);
3434 ZeroMemory(&nmlv, sizeof(nmlv));
3435 nmlv.iItem = lvHitTestInfo.iItem;
3436 nmlv.ptAction = infoPtr->ptClickPos;
3438 if (!infoPtr->bDragging)
3440 notify_listview(infoPtr, LVN_BEGINDRAG, &nmlv);
3441 infoPtr->bDragging = TRUE;
3448 /* see if we are supposed to be tracking mouse hovering */
3449 if (LISTVIEW_isHotTracking(infoPtr)) {
3450 /* fill in the trackinfo struct */
3451 trackinfo.cbSize = sizeof(TRACKMOUSEEVENT);
3452 trackinfo.dwFlags = TME_QUERY;
3453 trackinfo.hwndTrack = infoPtr->hwndSelf;
3454 trackinfo.dwHoverTime = infoPtr->dwHoverTime;
3456 /* see if we are already tracking this hwnd */
3457 _TrackMouseEvent(&trackinfo);
3459 if(!(trackinfo.dwFlags & TME_HOVER)) {
3460 trackinfo.dwFlags = TME_HOVER;
3462 /* call TRACKMOUSEEVENT so we receive WM_MOUSEHOVER messages */
3463 _TrackMouseEvent(&trackinfo);
3472 * Tests whether the item is assignable to a list with style lStyle
3474 static inline BOOL is_assignable_item(const LVITEMW *lpLVItem, LONG lStyle)
3476 if ( (lpLVItem->mask & LVIF_TEXT) &&
3477 (lpLVItem->pszText == LPSTR_TEXTCALLBACKW) &&
3478 (lStyle & (LVS_SORTASCENDING | LVS_SORTDESCENDING)) ) return FALSE;
3486 * Helper for LISTVIEW_SetItemT *only*: sets item attributes.
3489 * [I] infoPtr : valid pointer to the listview structure
3490 * [I] lpLVItem : valid pointer to new item attributes
3491 * [I] isNew : the item being set is being inserted
3492 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
3493 * [O] bChanged : will be set to TRUE if the item really changed
3499 static BOOL set_main_item(LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem, BOOL isNew, BOOL isW, BOOL *bChanged)
3501 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
3506 /* stateMask is ignored for LVM_INSERTITEM */
3507 UINT stateMask = isNew ? ~0 : lpLVItem->stateMask;
3511 assert(lpLVItem->iItem >= 0 && lpLVItem->iItem < infoPtr->nItemCount);
3513 if (lpLVItem->mask == 0) return TRUE;
3515 if (infoPtr->dwStyle & LVS_OWNERDATA)
3517 /* a virtual listview only stores selection and focus */
3518 if (lpLVItem->mask & ~LVIF_STATE)
3524 HDPA hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, lpLVItem->iItem);
3525 lpItem = DPA_GetPtr(hdpaSubItems, 0);
3529 /* we need to get the lParam and state of the item */
3530 item.iItem = lpLVItem->iItem;
3531 item.iSubItem = lpLVItem->iSubItem;
3532 item.mask = LVIF_STATE | LVIF_PARAM;
3533 item.stateMask = (infoPtr->dwStyle & LVS_OWNERDATA) ? LVIS_FOCUSED | LVIS_SELECTED : ~0;
3537 if (!isNew && !LISTVIEW_GetItemW(infoPtr, &item)) return FALSE;
3539 TRACE("oldState=%x, newState=%x\n", item.state, lpLVItem->state);
3540 /* determine what fields will change */
3541 if ((lpLVItem->mask & LVIF_STATE) && ((item.state ^ lpLVItem->state) & stateMask & ~infoPtr->uCallbackMask))
3542 uChanged |= LVIF_STATE;
3544 if ((lpLVItem->mask & LVIF_IMAGE) && (lpItem->hdr.iImage != lpLVItem->iImage))
3545 uChanged |= LVIF_IMAGE;
3547 if ((lpLVItem->mask & LVIF_PARAM) && (lpItem->lParam != lpLVItem->lParam))
3548 uChanged |= LVIF_PARAM;
3550 if ((lpLVItem->mask & LVIF_INDENT) && (lpItem->iIndent != lpLVItem->iIndent))
3551 uChanged |= LVIF_INDENT;
3553 if ((lpLVItem->mask & LVIF_TEXT) && textcmpWT(lpItem->hdr.pszText, lpLVItem->pszText, isW))
3554 uChanged |= LVIF_TEXT;
3556 TRACE("uChanged=0x%x\n", uChanged);
3557 if (!uChanged) return TRUE;
3560 ZeroMemory(&nmlv, sizeof(NMLISTVIEW));
3561 nmlv.iItem = lpLVItem->iItem;
3562 nmlv.uNewState = (item.state & ~stateMask) | (lpLVItem->state & stateMask);
3563 nmlv.uOldState = item.state;
3564 nmlv.uChanged = uChanged;
3565 nmlv.lParam = item.lParam;
3567 /* send LVN_ITEMCHANGING notification, if the item is not being inserted */
3568 /* and we are _NOT_ virtual (LVS_OWNERDATA), and change notifications */
3570 if(lpItem && !isNew && infoPtr->bDoChangeNotify)
3572 HWND hwndSelf = infoPtr->hwndSelf;
3574 if (notify_listview(infoPtr, LVN_ITEMCHANGING, &nmlv))
3576 if (!IsWindow(hwndSelf))
3580 /* copy information */
3581 if (lpLVItem->mask & LVIF_TEXT)
3582 textsetptrT(&lpItem->hdr.pszText, lpLVItem->pszText, isW);
3584 if (lpLVItem->mask & LVIF_IMAGE)
3585 lpItem->hdr.iImage = lpLVItem->iImage;
3587 if (lpLVItem->mask & LVIF_PARAM)
3588 lpItem->lParam = lpLVItem->lParam;
3590 if (lpLVItem->mask & LVIF_INDENT)
3591 lpItem->iIndent = lpLVItem->iIndent;
3593 if (uChanged & LVIF_STATE)
3595 if (lpItem && (stateMask & ~infoPtr->uCallbackMask))
3597 lpItem->state &= ~stateMask;
3598 lpItem->state |= (lpLVItem->state & stateMask);
3600 if (lpLVItem->state & stateMask & ~infoPtr->uCallbackMask & LVIS_SELECTED)
3602 if (infoPtr->dwStyle & LVS_SINGLESEL) LISTVIEW_DeselectAllSkipItem(infoPtr, lpLVItem->iItem);
3603 ranges_additem(infoPtr->selectionRanges, lpLVItem->iItem);
3605 else if (stateMask & LVIS_SELECTED)
3607 ranges_delitem(infoPtr->selectionRanges, lpLVItem->iItem);
3609 /* if we are asked to change focus, and we manage it, do it */
3610 if (stateMask & ~infoPtr->uCallbackMask & LVIS_FOCUSED)
3612 if (lpLVItem->state & LVIS_FOCUSED)
3614 if (infoPtr->nFocusedItem != -1)
3616 /* remove current focus */
3617 item.mask = LVIF_STATE;
3619 item.stateMask = LVIS_FOCUSED;
3621 /* recurse with redrawing an item */
3622 LISTVIEW_SetItemState(infoPtr, infoPtr->nFocusedItem, &item);
3625 infoPtr->nFocusedItem = lpLVItem->iItem;
3626 LISTVIEW_EnsureVisible(infoPtr, lpLVItem->iItem, uView == LVS_LIST);
3628 else if (infoPtr->nFocusedItem == lpLVItem->iItem)
3630 infoPtr->nFocusedItem = -1;
3635 /* if we're inserting the item, we're done */
3636 if (isNew) return TRUE;
3638 /* send LVN_ITEMCHANGED notification */
3639 if (lpLVItem->mask & LVIF_PARAM) nmlv.lParam = lpLVItem->lParam;
3640 if (infoPtr->bDoChangeNotify) notify_listview(infoPtr, LVN_ITEMCHANGED, &nmlv);
3647 * Helper for LISTVIEW_{Set,Insert}ItemT *only*: sets subitem attributes.
3650 * [I] infoPtr : valid pointer to the listview structure
3651 * [I] lpLVItem : valid pointer to new subitem attributes
3652 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
3653 * [O] bChanged : will be set to TRUE if the item really changed
3659 static BOOL set_sub_item(const LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem, BOOL isW, BOOL *bChanged)
3662 SUBITEM_INFO *lpSubItem;
3664 /* we do not support subitems for virtual listviews */
3665 if (infoPtr->dwStyle & LVS_OWNERDATA) return FALSE;
3667 /* set subitem only if column is present */
3668 if (lpLVItem->iSubItem >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
3670 /* First do some sanity checks */
3671 /* The LVIF_STATE flag is valid for subitems, but does not appear to be
3672 particularly useful. We currently do not actually do anything with
3673 the flag on subitems.
3675 if (lpLVItem->mask & ~(LVIF_TEXT | LVIF_IMAGE | LVIF_STATE)) return FALSE;
3676 if (!(lpLVItem->mask & (LVIF_TEXT | LVIF_IMAGE | LVIF_STATE))) return TRUE;
3678 /* get the subitem structure, and create it if not there */
3679 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, lpLVItem->iItem);
3680 assert (hdpaSubItems);
3682 lpSubItem = LISTVIEW_GetSubItemPtr(hdpaSubItems, lpLVItem->iSubItem);
3685 SUBITEM_INFO *tmpSubItem;
3688 lpSubItem = Alloc(sizeof(SUBITEM_INFO));
3689 if (!lpSubItem) return FALSE;
3690 /* we could binary search here, if need be...*/
3691 for (i = 1; i < DPA_GetPtrCount(hdpaSubItems); i++)
3693 tmpSubItem = DPA_GetPtr(hdpaSubItems, i);
3694 if (tmpSubItem->iSubItem > lpLVItem->iSubItem) break;
3696 if (DPA_InsertPtr(hdpaSubItems, i, lpSubItem) == -1)
3701 lpSubItem->iSubItem = lpLVItem->iSubItem;
3702 lpSubItem->hdr.iImage = I_IMAGECALLBACK;
3706 if (lpLVItem->mask & LVIF_IMAGE)
3707 if (lpSubItem->hdr.iImage != lpLVItem->iImage)
3709 lpSubItem->hdr.iImage = lpLVItem->iImage;
3713 if (lpLVItem->mask & LVIF_TEXT)
3714 if (lpSubItem->hdr.pszText != lpLVItem->pszText)
3716 textsetptrT(&lpSubItem->hdr.pszText, lpLVItem->pszText, isW);
3725 * Sets item attributes.
3728 * [I] infoPtr : valid pointer to the listview structure
3729 * [I] lpLVItem : new item attributes
3730 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
3736 static BOOL LISTVIEW_SetItemT(LISTVIEW_INFO *infoPtr, LVITEMW *lpLVItem, BOOL isW)
3738 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
3739 HWND hwndSelf = infoPtr->hwndSelf;
3740 LPWSTR pszText = NULL;
3741 BOOL bResult, bChanged = FALSE;
3743 TRACE("(lpLVItem=%s, isW=%d)\n", debuglvitem_t(lpLVItem, isW), isW);
3745 if (!lpLVItem || lpLVItem->iItem < 0 || lpLVItem->iItem >= infoPtr->nItemCount)
3748 /* For efficiency, we transform the lpLVItem->pszText to Unicode here */
3749 if ((lpLVItem->mask & LVIF_TEXT) && is_textW(lpLVItem->pszText))
3751 pszText = lpLVItem->pszText;
3752 lpLVItem->pszText = textdupTtoW(lpLVItem->pszText, isW);
3755 /* actually set the fields */
3756 if (!is_assignable_item(lpLVItem, infoPtr->dwStyle)) return FALSE;
3758 if (lpLVItem->iSubItem)
3759 bResult = set_sub_item(infoPtr, lpLVItem, TRUE, &bChanged);
3761 bResult = set_main_item(infoPtr, lpLVItem, FALSE, TRUE, &bChanged);
3762 if (!IsWindow(hwndSelf))
3765 /* redraw item, if necessary */
3766 if (bChanged && !infoPtr->bIsDrawing)
3768 /* this little optimization eliminates some nasty flicker */
3769 if ( uView == LVS_REPORT && !(infoPtr->dwStyle & LVS_OWNERDRAWFIXED) &&
3770 !(infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) &&
3771 lpLVItem->iSubItem > 0 && lpLVItem->iSubItem <= DPA_GetPtrCount(infoPtr->hdpaColumns) )
3772 LISTVIEW_InvalidateSubItem(infoPtr, lpLVItem->iItem, lpLVItem->iSubItem);
3774 LISTVIEW_InvalidateItem(infoPtr, lpLVItem->iItem);
3779 textfreeT(lpLVItem->pszText, isW);
3780 lpLVItem->pszText = pszText;
3788 * Retrieves the index of the item at coordinate (0, 0) of the client area.
3791 * [I] infoPtr : valid pointer to the listview structure
3796 static INT LISTVIEW_GetTopIndex(const LISTVIEW_INFO *infoPtr)
3798 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
3800 SCROLLINFO scrollInfo;
3802 scrollInfo.cbSize = sizeof(SCROLLINFO);
3803 scrollInfo.fMask = SIF_POS;
3805 if (uView == LVS_LIST)
3807 if (GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo))
3808 nItem = scrollInfo.nPos * LISTVIEW_GetCountPerColumn(infoPtr);
3810 else if (uView == LVS_REPORT)
3812 if (GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
3813 nItem = scrollInfo.nPos;
3817 if (GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
3818 nItem = LISTVIEW_GetCountPerRow(infoPtr) * (scrollInfo.nPos / infoPtr->nItemHeight);
3821 TRACE("nItem=%d\n", nItem);
3829 * Erases the background of the given rectangle
3832 * [I] infoPtr : valid pointer to the listview structure
3833 * [I] hdc : device context handle
3834 * [I] lprcBox : clipping rectangle
3840 static inline BOOL LISTVIEW_FillBkgnd(const LISTVIEW_INFO *infoPtr, HDC hdc, const RECT *lprcBox)
3842 if (!infoPtr->hBkBrush) return FALSE;
3844 TRACE("(hdc=%p, lprcBox=%s, hBkBrush=%p)\n", hdc, wine_dbgstr_rect(lprcBox), infoPtr->hBkBrush);
3846 return FillRect(hdc, lprcBox, infoPtr->hBkBrush);
3854 * [I] infoPtr : valid pointer to the listview structure
3855 * [I] hdc : device context handle
3856 * [I] nItem : item index
3857 * [I] nSubItem : subitem index
3858 * [I] pos : item position in client coordinates
3859 * [I] cdmode : custom draw mode
3865 static BOOL LISTVIEW_DrawItem(LISTVIEW_INFO *infoPtr, HDC hdc, INT nItem, INT nSubItem, POINT pos, DWORD cdmode)
3867 UINT uFormat, uView = infoPtr->dwStyle & LVS_TYPEMASK;
3868 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
3869 static WCHAR szCallback[] = { '(', 'c', 'a', 'l', 'l', 'b', 'a', 'c', 'k', ')', 0 };
3870 DWORD cdsubitemmode = CDRF_DODEFAULT;
3872 RECT rcSelect, rcBox, rcIcon, rcLabel, rcStateIcon;
3873 NMLVCUSTOMDRAW nmlvcd;
3878 TRACE("(hdc=%p, nItem=%d, nSubItem=%d, pos=%s)\n", hdc, nItem, nSubItem, wine_dbgstr_point(&pos));
3880 /* get information needed for drawing the item */
3881 lvItem.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_PARAM;
3882 if (nSubItem == 0) lvItem.mask |= LVIF_STATE;
3883 if (uView == LVS_REPORT) lvItem.mask |= LVIF_INDENT;
3884 lvItem.stateMask = LVIS_SELECTED | LVIS_FOCUSED | LVIS_STATEIMAGEMASK;
3885 lvItem.iItem = nItem;
3886 lvItem.iSubItem = nSubItem;
3889 lvItem.cchTextMax = DISP_TEXT_SIZE;
3890 lvItem.pszText = szDispText;
3891 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return FALSE;
3892 if (nSubItem > 0 && (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT))
3893 lvItem.state = LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED);
3894 if (lvItem.pszText == LPSTR_TEXTCALLBACKW) lvItem.pszText = szCallback;
3895 TRACE(" lvItem=%s\n", debuglvitem_t(&lvItem, TRUE));
3897 /* now check if we need to update the focus rectangle */
3898 lprcFocus = infoPtr->bFocus && (lvItem.state & LVIS_FOCUSED) ? &infoPtr->rcFocus : 0;
3900 if (!lprcFocus) lvItem.state &= ~LVIS_FOCUSED;
3901 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, &rcBox, &rcSelect, &rcIcon, &rcStateIcon, &rcLabel);
3902 OffsetRect(&rcBox, pos.x, pos.y);
3903 OffsetRect(&rcSelect, pos.x, pos.y);
3904 OffsetRect(&rcIcon, pos.x, pos.y);
3905 OffsetRect(&rcStateIcon, pos.x, pos.y);
3906 OffsetRect(&rcLabel, pos.x, pos.y);
3907 TRACE(" rcBox=%s, rcSelect=%s, rcIcon=%s. rcLabel=%s\n",
3908 wine_dbgstr_rect(&rcBox), wine_dbgstr_rect(&rcSelect),
3909 wine_dbgstr_rect(&rcIcon), wine_dbgstr_rect(&rcLabel));
3911 /* fill in the custom draw structure */
3912 customdraw_fill(&nmlvcd, infoPtr, hdc, &rcBox, &lvItem);
3914 hOldFont = GetCurrentObject(hdc, OBJ_FONT);
3915 if (nSubItem > 0) cdmode = infoPtr->cditemmode;
3916 if (cdmode & CDRF_SKIPDEFAULT) goto postpaint;
3917 if (cdmode & CDRF_NOTIFYITEMDRAW)
3918 cdsubitemmode = notify_customdraw(infoPtr, CDDS_PREPAINT, &nmlvcd);
3919 if (nSubItem == 0) infoPtr->cditemmode = cdsubitemmode;
3920 if (cdsubitemmode & CDRF_SKIPDEFAULT) goto postpaint;
3921 /* we have to send a CDDS_SUBITEM customdraw explicitly for subitem 0 */
3922 if (nSubItem == 0 && cdsubitemmode == CDRF_NOTIFYITEMDRAW)
3924 cdsubitemmode = notify_customdraw(infoPtr, CDDS_SUBITEM | CDDS_ITEMPREPAINT, &nmlvcd);
3925 if (cdsubitemmode & CDRF_SKIPDEFAULT) goto postpaint;
3927 if (nSubItem == 0 || (cdmode & CDRF_NOTIFYITEMDRAW))
3928 prepaint_setup(infoPtr, hdc, &nmlvcd, FALSE);
3929 else if ((infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) == FALSE)
3930 prepaint_setup(infoPtr, hdc, &nmlvcd, TRUE);
3932 /* in full row select, subitems, will just use main item's colors */
3933 if (nSubItem && uView == LVS_REPORT && (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT))
3934 nmlvcd.clrTextBk = CLR_NONE;
3937 if (infoPtr->himlState && STATEIMAGEINDEX(lvItem.state) && (nSubItem == 0))
3939 UINT uStateImage = STATEIMAGEINDEX(lvItem.state);
3942 TRACE("uStateImage=%d\n", uStateImage);
3943 ImageList_Draw(infoPtr->himlState, uStateImage - 1, hdc,
3944 rcStateIcon.left, rcStateIcon.top, ILD_NORMAL);
3949 himl = (uView == LVS_ICON ? infoPtr->himlNormal : infoPtr->himlSmall);
3950 if (himl && lvItem.iImage >= 0 && !IsRectEmpty(&rcIcon))
3952 TRACE("iImage=%d\n", lvItem.iImage);
3953 ImageList_DrawEx(himl, lvItem.iImage, hdc, rcIcon.left, rcIcon.top,
3954 rcIcon.right - rcIcon.left, rcIcon.bottom - rcIcon.top, infoPtr->clrBk, CLR_DEFAULT,
3955 (lvItem.state & LVIS_SELECTED) && (infoPtr->bFocus) ? ILD_SELECTED : ILD_NORMAL);
3958 /* Don't bother painting item being edited */
3959 if (infoPtr->hwndEdit && nItem == infoPtr->nEditLabelItem && nSubItem == 0) goto postpaint;
3961 /* FIXME: temporary hack */
3962 rcSelect.left = rcLabel.left;
3964 /* draw the selection background, if we're drawing the main item */
3967 /* in icon mode, the label rect is really what we want to draw the
3969 if (uView == LVS_ICON)
3972 if (uView == LVS_REPORT && (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT))
3973 rcSelect.right = rcBox.right;
3975 if (nmlvcd.clrTextBk != CLR_NONE)
3976 ExtTextOutW(hdc, rcSelect.left, rcSelect.top, ETO_OPAQUE, &rcSelect, 0, 0, 0);
3977 if(lprcFocus) *lprcFocus = rcSelect;
3980 /* figure out the text drawing flags */
3981 uFormat = (uView == LVS_ICON ? (lprcFocus ? LV_FL_DT_FLAGS : LV_ML_DT_FLAGS) : LV_SL_DT_FLAGS);
3982 if (uView == LVS_ICON)
3983 uFormat = (lprcFocus ? LV_FL_DT_FLAGS : LV_ML_DT_FLAGS);
3986 switch (LISTVIEW_GetColumnInfo(infoPtr, nSubItem)->fmt & LVCFMT_JUSTIFYMASK)
3988 case LVCFMT_RIGHT: uFormat |= DT_RIGHT; break;
3989 case LVCFMT_CENTER: uFormat |= DT_CENTER; break;
3990 default: uFormat |= DT_LEFT;
3993 if (!(uFormat & (DT_RIGHT | DT_CENTER)))
3995 if (himl && lvItem.iImage >= 0 && !IsRectEmpty(&rcIcon)) rcLabel.left += IMAGE_PADDING;
3996 else rcLabel.left += LABEL_HOR_PADDING;
3998 else if (uFormat & DT_RIGHT) rcLabel.right -= LABEL_HOR_PADDING;
4000 /* for GRIDLINES reduce the bottom so the text formats correctly */
4001 if (uView == LVS_REPORT && infoPtr->dwLvExStyle & LVS_EX_GRIDLINES)
4004 DrawTextW(hdc, lvItem.pszText, -1, &rcLabel, uFormat);
4007 if (cdsubitemmode & CDRF_NOTIFYPOSTPAINT)
4008 notify_postpaint(infoPtr, &nmlvcd);
4009 if (cdsubitemmode & CDRF_NEWFONT)
4010 SelectObject(hdc, hOldFont);
4016 * Draws listview items when in owner draw mode.
4019 * [I] infoPtr : valid pointer to the listview structure
4020 * [I] hdc : device context handle
4025 static void LISTVIEW_RefreshOwnerDraw(const LISTVIEW_INFO *infoPtr, ITERATOR *i, HDC hdc, DWORD cdmode)
4027 UINT uID = (UINT)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
4028 DWORD cditemmode = CDRF_DODEFAULT;
4029 NMLVCUSTOMDRAW nmlvcd;
4030 POINT Origin, Position;
4036 ZeroMemory(&dis, sizeof(dis));
4038 /* Get scroll info once before loop */
4039 LISTVIEW_GetOrigin(infoPtr, &Origin);
4041 /* iterate through the invalidated rows */
4042 while(iterator_next(i))
4044 item.iItem = i->nItem;
4046 item.mask = LVIF_PARAM | LVIF_STATE;
4047 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
4048 if (!LISTVIEW_GetItemW(infoPtr, &item)) continue;
4050 dis.CtlType = ODT_LISTVIEW;
4052 dis.itemID = item.iItem;
4053 dis.itemAction = ODA_DRAWENTIRE;
4055 if (item.state & LVIS_SELECTED) dis.itemState |= ODS_SELECTED;
4056 if (infoPtr->bFocus && (item.state & LVIS_FOCUSED)) dis.itemState |= ODS_FOCUS;
4057 dis.hwndItem = infoPtr->hwndSelf;
4059 LISTVIEW_GetItemOrigin(infoPtr, dis.itemID, &Position);
4060 dis.rcItem.left = Position.x + Origin.x;
4061 dis.rcItem.right = dis.rcItem.left + infoPtr->nItemWidth;
4062 dis.rcItem.top = Position.y + Origin.y;
4063 dis.rcItem.bottom = dis.rcItem.top + infoPtr->nItemHeight;
4064 dis.itemData = item.lParam;
4066 TRACE("item=%s, rcItem=%s\n", debuglvitem_t(&item, TRUE), wine_dbgstr_rect(&dis.rcItem));
4069 * Even if we do not send the CDRF_NOTIFYITEMDRAW we need to fill the nmlvcd
4070 * structure for the rest. of the paint cycle
4072 customdraw_fill(&nmlvcd, infoPtr, hdc, &dis.rcItem, &item);
4073 if (cdmode & CDRF_NOTIFYITEMDRAW)
4074 cditemmode = notify_customdraw(infoPtr, CDDS_PREPAINT, &nmlvcd);
4076 if (!(cditemmode & CDRF_SKIPDEFAULT))
4078 prepaint_setup (infoPtr, hdc, &nmlvcd, FALSE);
4079 SendMessageW(infoPtr->hwndNotify, WM_DRAWITEM, dis.CtlID, (LPARAM)&dis);
4082 if (cditemmode & CDRF_NOTIFYPOSTPAINT)
4083 notify_postpaint(infoPtr, &nmlvcd);
4089 * Draws listview items when in report display mode.
4092 * [I] infoPtr : valid pointer to the listview structure
4093 * [I] hdc : device context handle
4094 * [I] cdmode : custom draw mode
4099 static void LISTVIEW_RefreshReport(LISTVIEW_INFO *infoPtr, ITERATOR *i, HDC hdc, DWORD cdmode)
4102 RECT rcClip, rcItem;
4103 POINT Origin, Position;
4109 /* figure out what to draw */
4110 rgntype = GetClipBox(hdc, &rcClip);
4111 if (rgntype == NULLREGION) return;
4113 /* Get scroll info once before loop */
4114 LISTVIEW_GetOrigin(infoPtr, &Origin);
4116 /* narrow down the columns we need to paint */
4117 for(colRange.lower = 0; colRange.lower < DPA_GetPtrCount(infoPtr->hdpaColumns); colRange.lower++)
4119 LISTVIEW_GetHeaderRect(infoPtr, colRange.lower, &rcItem);
4120 if (rcItem.right + Origin.x >= rcClip.left) break;
4122 for(colRange.upper = DPA_GetPtrCount(infoPtr->hdpaColumns); colRange.upper > 0; colRange.upper--)
4124 LISTVIEW_GetHeaderRect(infoPtr, colRange.upper - 1, &rcItem);
4125 if (rcItem.left + Origin.x < rcClip.right) break;
4127 iterator_rangeitems(&j, colRange);
4129 /* in full row select, we _have_ to draw the main item */
4130 if (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT)
4133 /* iterate through the invalidated rows */
4134 while(iterator_next(i))
4136 /* iterate through the invalidated columns */
4137 while(iterator_next(&j))
4139 LISTVIEW_GetItemOrigin(infoPtr, i->nItem, &Position);
4140 Position.x += Origin.x;
4141 Position.y += Origin.y;
4143 if (rgntype == COMPLEXREGION && !((infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) && j.nItem == 0))
4145 LISTVIEW_GetHeaderRect(infoPtr, j.nItem, &rcItem);
4147 rcItem.bottom = infoPtr->nItemHeight;
4148 OffsetRect(&rcItem, Position.x, Position.y);
4149 if (!RectVisible(hdc, &rcItem)) continue;
4152 LISTVIEW_DrawItem(infoPtr, hdc, i->nItem, j.nItem, Position, cdmode);
4155 iterator_destroy(&j);
4160 * Draws the gridlines if necessary when in report display mode.
4163 * [I] infoPtr : valid pointer to the listview structure
4164 * [I] hdc : device context handle
4169 static void LISTVIEW_RefreshReportGrid(LISTVIEW_INFO *infoPtr, HDC hdc)
4174 RECT rcClip, rcItem = {0};
4182 /* figure out what to draw */
4183 rgntype = GetClipBox(hdc, &rcClip);
4184 if (rgntype == NULLREGION) return;
4186 /* Get scroll info once before loop */
4187 LISTVIEW_GetOrigin(infoPtr, &Origin);
4189 /* narrow down the columns we need to paint */
4190 for(colRange.lower = 0; colRange.lower < DPA_GetPtrCount(infoPtr->hdpaColumns); colRange.lower++)
4192 LISTVIEW_GetHeaderRect(infoPtr, colRange.lower, &rcItem);
4193 if (rcItem.right + Origin.x >= rcClip.left) break;
4195 for(colRange.upper = DPA_GetPtrCount(infoPtr->hdpaColumns); colRange.upper > 0; colRange.upper--)
4197 LISTVIEW_GetHeaderRect(infoPtr, colRange.upper - 1, &rcItem);
4198 if (rcItem.left + Origin.x < rcClip.right) break;
4200 /* is right most vertical line visible? */
4201 if (DPA_GetPtrCount(infoPtr->hdpaColumns) > 0)
4203 LISTVIEW_GetHeaderRect(infoPtr, DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, &rcItem);
4204 rmost = (rcItem.right + Origin.x < rcClip.right);
4207 if ((hPen = CreatePen( PS_SOLID, 1, comctl32_color.clr3dFace )))
4209 hOldPen = SelectObject ( hdc, hPen );
4211 /* draw the vertical lines for the columns */
4212 iterator_rangeitems(&j, colRange);
4213 while(iterator_next(&j))
4215 LISTVIEW_GetHeaderRect(infoPtr, j.nItem, &rcItem);
4216 if (rcItem.left == 0) continue; /* skip first column */
4217 rcItem.left += Origin.x;
4218 rcItem.right += Origin.x;
4219 rcItem.top = infoPtr->rcList.top;
4220 rcItem.bottom = infoPtr->rcList.bottom;
4221 TRACE("vert col=%d, rcItem=%s\n", j.nItem, wine_dbgstr_rect(&rcItem));
4222 MoveToEx (hdc, rcItem.left, rcItem.top, NULL);
4223 LineTo (hdc, rcItem.left, rcItem.bottom);
4225 iterator_destroy(&j);
4226 /* draw rightmost grid line if visible */
4229 MoveToEx (hdc, rcItem.right, rcItem.top, NULL);
4230 LineTo (hdc, rcItem.right, rcItem.bottom);
4233 /* draw the horizontial lines for the rows */
4234 itemheight = LISTVIEW_CalculateItemHeight(infoPtr);
4235 rcItem.left = infoPtr->rcList.left;
4236 rcItem.right = infoPtr->rcList.right;
4237 rcItem.bottom = rcItem.top = Origin.y - 1;
4238 MoveToEx(hdc, rcItem.left, rcItem.top, NULL);
4239 LineTo(hdc, rcItem.right, rcItem.top);
4240 for(y=itemheight-1+Origin.y; y<=infoPtr->rcList.bottom; y+=itemheight)
4242 rcItem.bottom = rcItem.top = y;
4243 TRACE("horz rcItem=%s\n", wine_dbgstr_rect(&rcItem));
4244 MoveToEx (hdc, rcItem.left, rcItem.top, NULL);
4245 LineTo (hdc, rcItem.right, rcItem.top);
4248 SelectObject( hdc, hOldPen );
4249 DeleteObject( hPen );
4255 * Draws listview items when in list display mode.
4258 * [I] infoPtr : valid pointer to the listview structure
4259 * [I] hdc : device context handle
4260 * [I] cdmode : custom draw mode
4265 static void LISTVIEW_RefreshList(LISTVIEW_INFO *infoPtr, ITERATOR *i, HDC hdc, DWORD cdmode)
4267 POINT Origin, Position;
4269 /* Get scroll info once before loop */
4270 LISTVIEW_GetOrigin(infoPtr, &Origin);
4272 while(iterator_prev(i))
4274 LISTVIEW_GetItemOrigin(infoPtr, i->nItem, &Position);
4275 Position.x += Origin.x;
4276 Position.y += Origin.y;
4278 LISTVIEW_DrawItem(infoPtr, hdc, i->nItem, 0, Position, cdmode);
4285 * Draws listview items.
4288 * [I] infoPtr : valid pointer to the listview structure
4289 * [I] hdc : device context handle
4290 * [I] prcErase : rect to be erased before refresh (may be NULL)
4295 static void LISTVIEW_Refresh(LISTVIEW_INFO *infoPtr, HDC hdc, const RECT *prcErase)
4297 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
4298 COLORREF oldTextColor = 0, oldBkColor = 0, oldClrTextBk, oldClrText;
4299 NMLVCUSTOMDRAW nmlvcd;
4306 HBITMAP hbmp = NULL;
4308 LISTVIEW_DUMP(infoPtr);
4310 if (infoPtr->dwLvExStyle & LVS_EX_DOUBLEBUFFER) {
4311 TRACE("double buffering\n");
4313 hdc = CreateCompatibleDC(hdcOrig);
4315 ERR("Failed to create DC for backbuffer\n");
4318 hbmp = CreateCompatibleBitmap(hdcOrig, infoPtr->rcList.right,
4319 infoPtr->rcList.bottom);
4321 ERR("Failed to create bitmap for backbuffer\n");
4326 SelectObject(hdc, hbmp);
4327 SelectObject(hdc, infoPtr->hFont);
4329 /* Save dc values we're gonna trash while drawing
4330 * FIXME: Should be done in LISTVIEW_DrawItem() */
4331 hOldFont = SelectObject(hdc, infoPtr->hFont);
4332 oldBkMode = GetBkMode(hdc);
4333 oldBkColor = GetBkColor(hdc);
4334 oldTextColor = GetTextColor(hdc);
4337 infoPtr->bIsDrawing = TRUE;
4340 LISTVIEW_FillBkgnd(infoPtr, hdc, prcErase);
4341 } else if (infoPtr->dwLvExStyle & LVS_EX_DOUBLEBUFFER) {
4342 /* If no erasing was done (usually because RedrawWindow was called
4343 * with RDW_INVALIDATE only) we need to copy the old contents into
4344 * the backbuffer before continuing. */
4345 BitBlt(hdc, infoPtr->rcList.left, infoPtr->rcList.top,
4346 infoPtr->rcList.right - infoPtr->rcList.left,
4347 infoPtr->rcList.bottom - infoPtr->rcList.top,
4348 hdcOrig, infoPtr->rcList.left, infoPtr->rcList.top, SRCCOPY);
4351 /* FIXME: Shouldn't need to do this */
4352 oldClrTextBk = infoPtr->clrTextBk;
4353 oldClrText = infoPtr->clrText;
4355 infoPtr->cditemmode = CDRF_DODEFAULT;
4357 GetClientRect(infoPtr->hwndSelf, &rcClient);
4358 customdraw_fill(&nmlvcd, infoPtr, hdc, &rcClient, 0);
4359 cdmode = notify_customdraw(infoPtr, CDDS_PREPAINT, &nmlvcd);
4360 if (cdmode & CDRF_SKIPDEFAULT) goto enddraw;
4361 prepaint_setup(infoPtr, hdc, &nmlvcd, FALSE);
4363 /* Use these colors to draw the items */
4364 infoPtr->clrTextBk = nmlvcd.clrTextBk;
4365 infoPtr->clrText = nmlvcd.clrText;
4367 /* nothing to draw */
4368 if(infoPtr->nItemCount == 0) goto enddraw;
4370 /* figure out what we need to draw */
4371 iterator_visibleitems(&i, infoPtr, hdc);
4373 /* send cache hint notification */
4374 if (infoPtr->dwStyle & LVS_OWNERDATA)
4376 RANGE range = iterator_range(&i);
4379 ZeroMemory(&nmlv, sizeof(NMLVCACHEHINT));
4380 nmlv.iFrom = range.lower;
4381 nmlv.iTo = range.upper - 1;
4382 notify_hdr(infoPtr, LVN_ODCACHEHINT, &nmlv.hdr);
4385 if ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (uView == LVS_REPORT))
4386 LISTVIEW_RefreshOwnerDraw(infoPtr, &i, hdc, cdmode);
4389 if (uView == LVS_REPORT)
4390 LISTVIEW_RefreshReport(infoPtr, &i, hdc, cdmode);
4391 else /* LVS_LIST, LVS_ICON or LVS_SMALLICON */
4392 LISTVIEW_RefreshList(infoPtr, &i, hdc, cdmode);
4394 /* if we have a focus rect, draw it */
4395 if (infoPtr->bFocus)
4396 LISTVIEW_DrawFocusRect(infoPtr, hdc);
4398 iterator_destroy(&i);
4401 /* For LVS_EX_GRIDLINES go and draw lines */
4402 /* This includes the case where there were *no* items */
4403 if ((uView == LVS_REPORT) && infoPtr->dwLvExStyle & LVS_EX_GRIDLINES)
4404 LISTVIEW_RefreshReportGrid(infoPtr, hdc);
4406 if (cdmode & CDRF_NOTIFYPOSTPAINT)
4407 notify_postpaint(infoPtr, &nmlvcd);
4409 infoPtr->clrTextBk = oldClrTextBk;
4410 infoPtr->clrText = oldClrText;
4413 BitBlt(hdcOrig, infoPtr->rcList.left, infoPtr->rcList.top,
4414 infoPtr->rcList.right - infoPtr->rcList.left,
4415 infoPtr->rcList.bottom - infoPtr->rcList.top,
4416 hdc, infoPtr->rcList.left, infoPtr->rcList.top, SRCCOPY);
4421 SelectObject(hdc, hOldFont);
4422 SetBkMode(hdc, oldBkMode);
4423 SetBkColor(hdc, oldBkColor);
4424 SetTextColor(hdc, oldTextColor);
4427 infoPtr->bIsDrawing = FALSE;
4433 * Calculates the approximate width and height of a given number of items.
4436 * [I] infoPtr : valid pointer to the listview structure
4437 * [I] nItemCount : number of items
4438 * [I] wWidth : width
4439 * [I] wHeight : height
4442 * Returns a DWORD. The width in the low word and the height in high word.
4444 static DWORD LISTVIEW_ApproximateViewRect(const LISTVIEW_INFO *infoPtr, INT nItemCount,
4445 WORD wWidth, WORD wHeight)
4447 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
4448 INT nItemCountPerColumn = 1;
4449 INT nColumnCount = 0;
4450 DWORD dwViewRect = 0;
4452 if (nItemCount == -1)
4453 nItemCount = infoPtr->nItemCount;
4455 if (uView == LVS_LIST)
4457 if (wHeight == 0xFFFF)
4459 /* use current height */
4460 wHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
4463 if (wHeight < infoPtr->nItemHeight)
4464 wHeight = infoPtr->nItemHeight;
4468 if (infoPtr->nItemHeight > 0)
4470 nItemCountPerColumn = wHeight / infoPtr->nItemHeight;
4471 if (nItemCountPerColumn == 0)
4472 nItemCountPerColumn = 1;
4474 if (nItemCount % nItemCountPerColumn != 0)
4475 nColumnCount = nItemCount / nItemCountPerColumn;
4477 nColumnCount = nItemCount / nItemCountPerColumn + 1;
4481 /* Microsoft padding magic */
4482 wHeight = nItemCountPerColumn * infoPtr->nItemHeight + 2;
4483 wWidth = nColumnCount * infoPtr->nItemWidth + 2;
4485 dwViewRect = MAKELONG(wWidth, wHeight);
4487 else if (uView == LVS_REPORT)
4491 if (infoPtr->nItemCount > 0)
4493 LISTVIEW_GetItemBox(infoPtr, 0, &rcBox);
4494 wWidth = rcBox.right - rcBox.left;
4495 wHeight = (rcBox.bottom - rcBox.top) * nItemCount;
4499 /* use current height and width */
4500 if (wHeight == 0xffff)
4501 wHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
4502 if (wWidth == 0xffff)
4503 wWidth = infoPtr->rcList.right - infoPtr->rcList.left;
4506 dwViewRect = MAKELONG(wWidth, wHeight);
4508 else if (uView == LVS_SMALLICON)
4509 FIXME("uView == LVS_SMALLICON: not implemented\n");
4510 else if (uView == LVS_ICON)
4511 FIXME("uView == LVS_ICON: not implemented\n");
4519 * Create a drag image list for the specified item.
4522 * [I] infoPtr : valid pointer to the listview structure
4523 * [I] iItem : index of item
4524 * [O] lppt : Upper-left corner of the image
4527 * Returns a handle to the image list if successful, NULL otherwise.
4529 static HIMAGELIST LISTVIEW_CreateDragImage(LISTVIEW_INFO *infoPtr, INT iItem, LPPOINT lppt)
4535 HBITMAP hbmp, hOldbmp;
4536 HIMAGELIST dragList = 0;
4537 TRACE("iItem=%d Count=%d\n", iItem, infoPtr->nItemCount);
4539 if (iItem < 0 || iItem >= infoPtr->nItemCount)
4542 rcItem.left = LVIR_BOUNDS;
4543 if (!LISTVIEW_GetItemRect(infoPtr, iItem, &rcItem))
4546 lppt->x = rcItem.left;
4547 lppt->y = rcItem.top;
4549 size.cx = rcItem.right - rcItem.left;
4550 size.cy = rcItem.bottom - rcItem.top;
4552 hdcOrig = GetDC(infoPtr->hwndSelf);
4553 hdc = CreateCompatibleDC(hdcOrig);
4554 hbmp = CreateCompatibleBitmap(hdcOrig, size.cx, size.cy);
4555 hOldbmp = SelectObject(hdc, hbmp);
4557 rcItem.left = rcItem.top = 0;
4558 rcItem.right = size.cx;
4559 rcItem.bottom = size.cy;
4560 FillRect(hdc, &rcItem, infoPtr->hBkBrush);
4563 if (LISTVIEW_DrawItem(infoPtr, hdc, iItem, 0, pos, infoPtr->cditemmode))
4565 dragList = ImageList_Create(size.cx, size.cy, ILC_COLOR, 10, 10);
4566 SelectObject(hdc, hOldbmp);
4567 ImageList_Add(dragList, hbmp, 0);
4570 SelectObject(hdc, hOldbmp);
4574 ReleaseDC(infoPtr->hwndSelf, hdcOrig);
4576 TRACE("ret=%p\n", dragList);
4584 * Removes all listview items and subitems.
4587 * [I] infoPtr : valid pointer to the listview structure
4593 static BOOL LISTVIEW_DeleteAllItems(LISTVIEW_INFO *infoPtr, BOOL destroy)
4596 HDPA hdpaSubItems = NULL;
4603 /* we do it directly, to avoid notifications */
4604 ranges_clear(infoPtr->selectionRanges);
4605 infoPtr->nSelectionMark = -1;
4606 infoPtr->nFocusedItem = -1;
4607 SetRectEmpty(&infoPtr->rcFocus);
4608 /* But we are supposed to leave nHotItem as is! */
4611 /* send LVN_DELETEALLITEMS notification */
4612 ZeroMemory(&nmlv, sizeof(NMLISTVIEW));
4614 bSuppress = notify_listview(infoPtr, LVN_DELETEALLITEMS, &nmlv);
4616 for (i = infoPtr->nItemCount - 1; i >= 0; i--)
4618 if (!(infoPtr->dwStyle & LVS_OWNERDATA))
4620 /* send LVN_DELETEITEM notification, if not suppressed
4621 and if it is not a virtual listview */
4622 if (!bSuppress) notify_deleteitem(infoPtr, i);
4623 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, i);
4624 for (j = 0; j < DPA_GetPtrCount(hdpaSubItems); j++)
4626 hdrItem = DPA_GetPtr(hdpaSubItems, j);
4627 if (is_textW(hdrItem->pszText)) Free(hdrItem->pszText);
4630 DPA_Destroy(hdpaSubItems);
4631 DPA_DeletePtr(infoPtr->hdpaItems, i);
4633 DPA_DeletePtr(infoPtr->hdpaPosX, i);
4634 DPA_DeletePtr(infoPtr->hdpaPosY, i);
4635 infoPtr->nItemCount --;
4640 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
4641 LISTVIEW_UpdateScroll(infoPtr);
4643 LISTVIEW_InvalidateList(infoPtr);
4650 * Scrolls, and updates the columns, when a column is changing width.
4653 * [I] infoPtr : valid pointer to the listview structure
4654 * [I] nColumn : column to scroll
4655 * [I] dx : amount of scroll, in pixels
4660 static void LISTVIEW_ScrollColumns(LISTVIEW_INFO *infoPtr, INT nColumn, INT dx)
4662 COLUMN_INFO *lpColumnInfo;
4667 if (nColumn < 0 || DPA_GetPtrCount(infoPtr->hdpaColumns) < 1) return;
4668 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, min(nColumn, DPA_GetPtrCount(infoPtr->hdpaColumns) - 1));
4669 rcCol = lpColumnInfo->rcHeader;
4670 if (nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns))
4671 rcCol.left = rcCol.right;
4673 /* adjust the other columns */
4674 for (nCol = nColumn; nCol < DPA_GetPtrCount(infoPtr->hdpaColumns); nCol++)
4676 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, nCol);
4677 lpColumnInfo->rcHeader.left += dx;
4678 lpColumnInfo->rcHeader.right += dx;
4681 /* do not update screen if not in report mode */
4682 if (!is_redrawing(infoPtr) || (infoPtr->dwStyle & LVS_TYPEMASK) != LVS_REPORT) return;
4684 /* Need to reset the item width when inserting a new column */
4685 infoPtr->nItemWidth += dx;
4687 LISTVIEW_UpdateScroll(infoPtr);
4688 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
4690 /* scroll to cover the deleted column, and invalidate for redraw */
4691 rcOld = infoPtr->rcList;
4692 rcOld.left = ptOrigin.x + rcCol.left + dx;
4693 ScrollWindowEx(infoPtr->hwndSelf, dx, 0, &rcOld, &rcOld, 0, 0, SW_ERASE | SW_INVALIDATE);
4698 * Removes a column from the listview control.
4701 * [I] infoPtr : valid pointer to the listview structure
4702 * [I] nColumn : column index
4708 static BOOL LISTVIEW_DeleteColumn(LISTVIEW_INFO *infoPtr, INT nColumn)
4712 TRACE("nColumn=%d\n", nColumn);
4714 if (nColumn < 0 || DPA_GetPtrCount(infoPtr->hdpaColumns) == 0
4715 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
4717 /* While the MSDN specifically says that column zero should not be deleted,
4718 what actually happens is that the column itself is deleted but no items or subitems
4722 LISTVIEW_GetHeaderRect(infoPtr, nColumn, &rcCol);
4724 if (!SendMessageW(infoPtr->hwndHeader, HDM_DELETEITEM, nColumn, 0))
4727 Free(DPA_GetPtr(infoPtr->hdpaColumns, nColumn));
4728 DPA_DeletePtr(infoPtr->hdpaColumns, nColumn);
4730 if (!(infoPtr->dwStyle & LVS_OWNERDATA) && nColumn)
4732 SUBITEM_INFO *lpSubItem, *lpDelItem;
4734 INT nItem, nSubItem, i;
4736 for (nItem = 0; nItem < infoPtr->nItemCount; nItem++)
4738 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, nItem);
4741 for (i = 1; i < DPA_GetPtrCount(hdpaSubItems); i++)
4743 lpSubItem = DPA_GetPtr(hdpaSubItems, i);
4744 if (lpSubItem->iSubItem == nColumn)
4747 lpDelItem = lpSubItem;
4749 else if (lpSubItem->iSubItem > nColumn)
4751 lpSubItem->iSubItem--;
4755 /* if we found our subitem, zapp it */
4759 if (is_textW(lpDelItem->hdr.pszText))
4760 Free(lpDelItem->hdr.pszText);
4765 /* free dpa memory */
4766 DPA_DeletePtr(hdpaSubItems, nSubItem);
4771 /* update the other column info */
4772 LISTVIEW_UpdateItemSize(infoPtr);
4773 if(DPA_GetPtrCount(infoPtr->hdpaColumns) == 0)
4774 LISTVIEW_InvalidateList(infoPtr);
4776 LISTVIEW_ScrollColumns(infoPtr, nColumn, -(rcCol.right - rcCol.left));
4783 * Invalidates the listview after an item's insertion or deletion.
4786 * [I] infoPtr : valid pointer to the listview structure
4787 * [I] nItem : item index
4788 * [I] dir : -1 if deleting, 1 if inserting
4793 static void LISTVIEW_ScrollOnInsert(LISTVIEW_INFO *infoPtr, INT nItem, INT dir)
4795 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
4796 INT nPerCol, nItemCol, nItemRow;
4800 /* if we don't refresh, what's the point of scrolling? */
4801 if (!is_redrawing(infoPtr)) return;
4803 assert (abs(dir) == 1);
4805 /* arrange icons if autoarrange is on */
4806 if (is_autoarrange(infoPtr))
4808 BOOL arrange = TRUE;
4809 if (dir < 0 && nItem >= infoPtr->nItemCount) arrange = FALSE;
4810 if (dir > 0 && nItem == infoPtr->nItemCount - 1) arrange = FALSE;
4811 if (arrange) LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
4814 /* scrollbars need updating */
4815 LISTVIEW_UpdateScroll(infoPtr);
4817 /* figure out the item's position */
4818 if (uView == LVS_REPORT)
4819 nPerCol = infoPtr->nItemCount + 1;
4820 else if (uView == LVS_LIST)
4821 nPerCol = LISTVIEW_GetCountPerColumn(infoPtr);
4822 else /* LVS_ICON, or LVS_SMALLICON */
4825 nItemCol = nItem / nPerCol;
4826 nItemRow = nItem % nPerCol;
4827 LISTVIEW_GetOrigin(infoPtr, &Origin);
4829 /* move the items below up a slot */
4830 rcScroll.left = nItemCol * infoPtr->nItemWidth;
4831 rcScroll.top = nItemRow * infoPtr->nItemHeight;
4832 rcScroll.right = rcScroll.left + infoPtr->nItemWidth;
4833 rcScroll.bottom = nPerCol * infoPtr->nItemHeight;
4834 OffsetRect(&rcScroll, Origin.x, Origin.y);
4835 TRACE("rcScroll=%s, dx=%d\n", wine_dbgstr_rect(&rcScroll), dir * infoPtr->nItemHeight);
4836 if (IntersectRect(&rcScroll, &rcScroll, &infoPtr->rcList))
4838 TRACE("Scrolling rcScroll=%s, rcList=%s\n", wine_dbgstr_rect(&rcScroll), wine_dbgstr_rect(&infoPtr->rcList));
4839 ScrollWindowEx(infoPtr->hwndSelf, 0, dir * infoPtr->nItemHeight,
4840 &rcScroll, &rcScroll, 0, 0, SW_ERASE | SW_INVALIDATE);
4843 /* report has only that column, so we're done */
4844 if (uView == LVS_REPORT) return;
4846 /* now for LISTs, we have to deal with the columns to the right */
4847 rcScroll.left = (nItemCol + 1) * infoPtr->nItemWidth;
4849 rcScroll.right = (infoPtr->nItemCount / nPerCol + 1) * infoPtr->nItemWidth;
4850 rcScroll.bottom = nPerCol * infoPtr->nItemHeight;
4851 OffsetRect(&rcScroll, Origin.x, Origin.y);
4852 if (IntersectRect(&rcScroll, &rcScroll, &infoPtr->rcList))
4853 ScrollWindowEx(infoPtr->hwndSelf, 0, dir * infoPtr->nItemHeight,
4854 &rcScroll, &rcScroll, 0, 0, SW_ERASE | SW_INVALIDATE);
4859 * Removes an item from the listview control.
4862 * [I] infoPtr : valid pointer to the listview structure
4863 * [I] nItem : item index
4869 static BOOL LISTVIEW_DeleteItem(LISTVIEW_INFO *infoPtr, INT nItem)
4872 const UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
4873 const BOOL is_icon = (uView == LVS_SMALLICON || uView == LVS_ICON);
4875 TRACE("(nItem=%d)\n", nItem);
4877 if (nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
4879 /* remove selection, and focus */
4881 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
4882 LISTVIEW_SetItemState(infoPtr, nItem, &item);
4884 /* send LVN_DELETEITEM notification. */
4885 if (!notify_deleteitem(infoPtr, nItem)) return FALSE;
4887 /* we need to do this here, because we'll be deleting stuff */
4889 LISTVIEW_InvalidateItem(infoPtr, nItem);
4891 if (!(infoPtr->dwStyle & LVS_OWNERDATA))
4897 hdpaSubItems = DPA_DeletePtr(infoPtr->hdpaItems, nItem);
4898 for (i = 0; i < DPA_GetPtrCount(hdpaSubItems); i++)
4900 hdrItem = DPA_GetPtr(hdpaSubItems, i);
4901 if (is_textW(hdrItem->pszText)) Free(hdrItem->pszText);
4904 DPA_Destroy(hdpaSubItems);
4909 DPA_DeletePtr(infoPtr->hdpaPosX, nItem);
4910 DPA_DeletePtr(infoPtr->hdpaPosY, nItem);
4913 infoPtr->nItemCount--;
4914 LISTVIEW_ShiftIndices(infoPtr, nItem, -1);
4916 /* now is the invalidation fun */
4918 LISTVIEW_ScrollOnInsert(infoPtr, nItem, -1);
4925 * Callback implementation for editlabel control
4928 * [I] infoPtr : valid pointer to the listview structure
4929 * [I] pszText : modified text
4930 * [I] isW : TRUE if psxText is Unicode, FALSE if it's ANSI
4936 static BOOL LISTVIEW_EndEditLabelT(LISTVIEW_INFO *infoPtr, LPWSTR pszText, BOOL isW)
4938 HWND hwndSelf = infoPtr->hwndSelf;
4939 NMLVDISPINFOW dispInfo;
4940 INT editedItem = infoPtr->nEditLabelItem;
4943 TRACE("(pszText=%s, isW=%d)\n", debugtext_t(pszText, isW), isW);
4945 infoPtr->nEditLabelItem = -1;
4947 ZeroMemory(&dispInfo, sizeof(dispInfo));
4948 dispInfo.item.mask = LVIF_PARAM | LVIF_STATE | LVIF_TEXT;
4949 dispInfo.item.iItem = editedItem;
4950 dispInfo.item.iSubItem = 0;
4951 dispInfo.item.stateMask = ~0;
4952 if (!LISTVIEW_GetItemW(infoPtr, &dispInfo.item)) return FALSE;
4955 bSame = (lstrcmpW(dispInfo.item.pszText, pszText) == 0);
4958 LPWSTR tmp = textdupTtoW(pszText, FALSE);
4959 bSame = (lstrcmpW(dispInfo.item.pszText, tmp) == 0);
4960 textfreeT(tmp, FALSE);
4962 if (bSame) return TRUE;
4964 /* add the text from the edit in */
4965 dispInfo.item.mask |= LVIF_TEXT;
4966 dispInfo.item.pszText = pszText;
4967 dispInfo.item.cchTextMax = textlenT(pszText, isW);
4969 /* Do we need to update the Item Text */
4970 if (!notify_dispinfoT(infoPtr, LVN_ENDLABELEDITW, &dispInfo, isW)) return FALSE;
4971 if (!IsWindow(hwndSelf))
4973 if (!pszText) return TRUE;
4975 if (!(infoPtr->dwStyle & LVS_OWNERDATA))
4977 HDPA hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, editedItem);
4978 ITEM_INFO* lpItem = DPA_GetPtr(hdpaSubItems, 0);
4979 if (lpItem && lpItem->hdr.pszText == LPSTR_TEXTCALLBACKW)
4981 LISTVIEW_InvalidateItem(infoPtr, editedItem);
4986 ZeroMemory(&dispInfo, sizeof(dispInfo));
4987 dispInfo.item.mask = LVIF_TEXT;
4988 dispInfo.item.iItem = editedItem;
4989 dispInfo.item.iSubItem = 0;
4990 dispInfo.item.pszText = pszText;
4991 dispInfo.item.cchTextMax = textlenT(pszText, isW);
4992 return LISTVIEW_SetItemT(infoPtr, &dispInfo.item, isW);
4997 * Begin in place editing of specified list view item
5000 * [I] infoPtr : valid pointer to the listview structure
5001 * [I] nItem : item index
5002 * [I] isW : TRUE if it's a Unicode req, FALSE if ASCII
5008 static HWND LISTVIEW_EditLabelT(LISTVIEW_INFO *infoPtr, INT nItem, BOOL isW)
5010 WCHAR szDispText[DISP_TEXT_SIZE] = { 0 };
5011 NMLVDISPINFOW dispInfo;
5013 HWND hwndSelf = infoPtr->hwndSelf;
5015 TRACE("(nItem=%d, isW=%d)\n", nItem, isW);
5017 if (~infoPtr->dwStyle & LVS_EDITLABELS) return 0;
5018 if (nItem < 0 || nItem >= infoPtr->nItemCount) return 0;
5020 infoPtr->nEditLabelItem = nItem;
5022 /* Is the EditBox still there, if so remove it */
5023 if(infoPtr->hwndEdit != 0)
5025 SetFocus(infoPtr->hwndSelf);
5026 infoPtr->hwndEdit = 0;
5029 LISTVIEW_SetSelection(infoPtr, nItem);
5030 LISTVIEW_SetItemFocus(infoPtr, nItem);
5031 LISTVIEW_InvalidateItem(infoPtr, nItem);
5033 rect.left = LVIR_LABEL;
5034 if (!LISTVIEW_GetItemRect(infoPtr, nItem, &rect)) return 0;
5036 ZeroMemory(&dispInfo, sizeof(dispInfo));
5037 dispInfo.item.mask = LVIF_PARAM | LVIF_STATE | LVIF_TEXT;
5038 dispInfo.item.iItem = nItem;
5039 dispInfo.item.iSubItem = 0;
5040 dispInfo.item.stateMask = ~0;
5041 dispInfo.item.pszText = szDispText;
5042 dispInfo.item.cchTextMax = DISP_TEXT_SIZE;
5043 if (!LISTVIEW_GetItemT(infoPtr, &dispInfo.item, isW)) return 0;
5045 infoPtr->hwndEdit = CreateEditLabelT(infoPtr, dispInfo.item.pszText, WS_VISIBLE,
5046 rect.left-2, rect.top-1, 0, rect.bottom - rect.top+2, isW);
5047 if (!infoPtr->hwndEdit) return 0;
5049 if (notify_dispinfoT(infoPtr, LVN_BEGINLABELEDITW, &dispInfo, isW))
5051 if (!IsWindow(hwndSelf))
5053 SendMessageW(infoPtr->hwndEdit, WM_CLOSE, 0, 0);
5054 infoPtr->hwndEdit = 0;
5058 ShowWindow(infoPtr->hwndEdit, SW_NORMAL);
5059 SetFocus(infoPtr->hwndEdit);
5060 SendMessageW(infoPtr->hwndEdit, EM_SETSEL, 0, -1);
5061 return infoPtr->hwndEdit;
5067 * Ensures the specified item is visible, scrolling into view if necessary.
5070 * [I] infoPtr : valid pointer to the listview structure
5071 * [I] nItem : item index
5072 * [I] bPartial : partially or entirely visible
5078 static BOOL LISTVIEW_EnsureVisible(LISTVIEW_INFO *infoPtr, INT nItem, BOOL bPartial)
5080 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
5081 INT nScrollPosHeight = 0;
5082 INT nScrollPosWidth = 0;
5083 INT nHorzAdjust = 0;
5084 INT nVertAdjust = 0;
5087 RECT rcItem, rcTemp;
5089 rcItem.left = LVIR_BOUNDS;
5090 if (!LISTVIEW_GetItemRect(infoPtr, nItem, &rcItem)) return FALSE;
5092 if (bPartial && IntersectRect(&rcTemp, &infoPtr->rcList, &rcItem)) return TRUE;
5094 if (rcItem.left < infoPtr->rcList.left || rcItem.right > infoPtr->rcList.right)
5096 /* scroll left/right, but in LVS_REPORT mode */
5097 if (uView == LVS_LIST)
5098 nScrollPosWidth = infoPtr->nItemWidth;
5099 else if ((uView == LVS_SMALLICON) || (uView == LVS_ICON))
5100 nScrollPosWidth = 1;
5102 if (rcItem.left < infoPtr->rcList.left)
5105 if (uView != LVS_REPORT) nHorzDiff = rcItem.left - infoPtr->rcList.left;
5110 if (uView != LVS_REPORT) nHorzDiff = rcItem.right - infoPtr->rcList.right;
5114 if (rcItem.top < infoPtr->rcList.top || rcItem.bottom > infoPtr->rcList.bottom)
5116 /* scroll up/down, but not in LVS_LIST mode */
5117 if (uView == LVS_REPORT)
5118 nScrollPosHeight = infoPtr->nItemHeight;
5119 else if ((uView == LVS_ICON) || (uView == LVS_SMALLICON))
5120 nScrollPosHeight = 1;
5122 if (rcItem.top < infoPtr->rcList.top)
5125 if (uView != LVS_LIST) nVertDiff = rcItem.top - infoPtr->rcList.top;
5130 if (uView != LVS_LIST) nVertDiff = rcItem.bottom - infoPtr->rcList.bottom;
5134 if (!nScrollPosWidth && !nScrollPosHeight) return TRUE;
5136 if (nScrollPosWidth)
5138 INT diff = nHorzDiff / nScrollPosWidth;
5139 if (nHorzDiff % nScrollPosWidth) diff += nHorzAdjust;
5140 LISTVIEW_HScroll(infoPtr, SB_INTERNAL, diff, 0);
5143 if (nScrollPosHeight)
5145 INT diff = nVertDiff / nScrollPosHeight;
5146 if (nVertDiff % nScrollPosHeight) diff += nVertAdjust;
5147 LISTVIEW_VScroll(infoPtr, SB_INTERNAL, diff, 0);
5155 * Searches for an item with specific characteristics.
5158 * [I] hwnd : window handle
5159 * [I] nStart : base item index
5160 * [I] lpFindInfo : item information to look for
5163 * SUCCESS : index of item
5166 static INT LISTVIEW_FindItemW(const LISTVIEW_INFO *infoPtr, INT nStart,
5167 const LVFINDINFOW *lpFindInfo)
5169 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
5170 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
5171 BOOL bWrap = FALSE, bNearest = FALSE;
5172 INT nItem = nStart + 1, nLast = infoPtr->nItemCount, nNearestItem = -1;
5173 ULONG xdist, ydist, dist, mindist = 0x7fffffff;
5174 POINT Position, Destination;
5177 /* Search in virtual listviews should be done by application, not by
5178 listview control, so we just send LVN_ODFINDITEMW and return the result */
5179 if (infoPtr->dwStyle & LVS_OWNERDATA)
5183 nmlv.iStart = nStart;
5184 nmlv.lvfi = *lpFindInfo;
5185 return notify_hdr(infoPtr, LVN_ODFINDITEMW, (LPNMHDR)&nmlv.hdr);
5188 if (!lpFindInfo || nItem < 0) return -1;
5191 if (lpFindInfo->flags & (LVFI_STRING | LVFI_PARTIAL))
5193 lvItem.mask |= LVIF_TEXT;
5194 lvItem.pszText = szDispText;
5195 lvItem.cchTextMax = DISP_TEXT_SIZE;
5198 if (lpFindInfo->flags & LVFI_WRAP)
5201 if ((lpFindInfo->flags & LVFI_NEARESTXY) &&
5202 (uView == LVS_ICON || uView ==LVS_SMALLICON))
5207 LISTVIEW_GetOrigin(infoPtr, &Origin);
5208 Destination.x = lpFindInfo->pt.x - Origin.x;
5209 Destination.y = lpFindInfo->pt.y - Origin.y;
5210 switch(lpFindInfo->vkDirection)
5212 case VK_DOWN: Destination.y += infoPtr->nItemHeight; break;
5213 case VK_UP: Destination.y -= infoPtr->nItemHeight; break;
5214 case VK_RIGHT: Destination.x += infoPtr->nItemWidth; break;
5215 case VK_LEFT: Destination.x -= infoPtr->nItemWidth; break;
5216 case VK_HOME: Destination.x = Destination.y = 0; break;
5217 case VK_NEXT: Destination.y += infoPtr->rcList.bottom - infoPtr->rcList.top; break;
5218 case VK_PRIOR: Destination.y -= infoPtr->rcList.bottom - infoPtr->rcList.top; break;
5220 LISTVIEW_GetAreaRect(infoPtr, &rcArea);
5221 Destination.x = rcArea.right;
5222 Destination.y = rcArea.bottom;
5224 default: ERR("Unknown vkDirection=%d\n", lpFindInfo->vkDirection);
5228 else Destination.x = Destination.y = 0;
5230 /* if LVFI_PARAM is specified, all other flags are ignored */
5231 if (lpFindInfo->flags & LVFI_PARAM)
5233 lvItem.mask |= LVIF_PARAM;
5235 lvItem.mask &= ~LVIF_TEXT;
5239 for (; nItem < nLast; nItem++)
5241 lvItem.iItem = nItem;
5242 lvItem.iSubItem = 0;
5243 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) continue;
5245 if (lvItem.mask & LVIF_PARAM)
5247 if (lpFindInfo->lParam == lvItem.lParam)
5253 if (lvItem.mask & LVIF_TEXT)
5255 if (lpFindInfo->flags & LVFI_PARTIAL)
5257 if (strstrW(lvItem.pszText, lpFindInfo->psz) == NULL) continue;
5261 if (lstrcmpW(lvItem.pszText, lpFindInfo->psz) != 0) continue;
5265 if (!bNearest) return nItem;
5267 /* This is very inefficient. To do a good job here,
5268 * we need a sorted array of (x,y) item positions */
5269 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
5271 /* compute the distance^2 to the destination */
5272 xdist = Destination.x - Position.x;
5273 ydist = Destination.y - Position.y;
5274 dist = xdist * xdist + ydist * ydist;
5276 /* remember the distance, and item if it's closer */
5280 nNearestItem = nItem;
5287 nLast = min(nStart + 1, infoPtr->nItemCount);
5292 return nNearestItem;
5297 * Searches for an item with specific characteristics.
5300 * [I] hwnd : window handle
5301 * [I] nStart : base item index
5302 * [I] lpFindInfo : item information to look for
5305 * SUCCESS : index of item
5308 static INT LISTVIEW_FindItemA(const LISTVIEW_INFO *infoPtr, INT nStart,
5309 const LVFINDINFOA *lpFindInfo)
5311 BOOL hasText = lpFindInfo->flags & (LVFI_STRING | LVFI_PARTIAL);
5316 memcpy(&fiw, lpFindInfo, sizeof(fiw));
5317 if (hasText) fiw.psz = strW = textdupTtoW((LPCWSTR)lpFindInfo->psz, FALSE);
5318 res = LISTVIEW_FindItemW(infoPtr, nStart, &fiw);
5319 textfreeT(strW, FALSE);
5325 * Retrieves the background image of the listview control.
5328 * [I] infoPtr : valid pointer to the listview structure
5329 * [O] lpBkImage : background image attributes
5335 /* static BOOL LISTVIEW_GetBkImage(const LISTVIEW_INFO *infoPtr, LPLVBKIMAGE lpBkImage) */
5337 /* FIXME (listview, "empty stub!\n"); */
5343 * Retrieves column attributes.
5346 * [I] infoPtr : valid pointer to the listview structure
5347 * [I] nColumn : column index
5348 * [IO] lpColumn : column information
5349 * [I] isW : if TRUE, then lpColumn is a LPLVCOLUMNW
5350 * otherwise it is in fact a LPLVCOLUMNA
5356 static BOOL LISTVIEW_GetColumnT(const LISTVIEW_INFO *infoPtr, INT nColumn, LPLVCOLUMNW lpColumn, BOOL isW)
5358 COLUMN_INFO *lpColumnInfo;
5361 if (!lpColumn || nColumn < 0 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
5362 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, nColumn);
5364 /* initialize memory */
5365 ZeroMemory(&hdi, sizeof(hdi));
5367 if (lpColumn->mask & LVCF_TEXT)
5369 hdi.mask |= HDI_TEXT;
5370 hdi.pszText = lpColumn->pszText;
5371 hdi.cchTextMax = lpColumn->cchTextMax;
5374 if (lpColumn->mask & LVCF_IMAGE)
5375 hdi.mask |= HDI_IMAGE;
5377 if (lpColumn->mask & LVCF_ORDER)
5378 hdi.mask |= HDI_ORDER;
5380 if (lpColumn->mask & LVCF_SUBITEM)
5381 hdi.mask |= HDI_LPARAM;
5383 if (!SendMessageW(infoPtr->hwndHeader, isW ? HDM_GETITEMW : HDM_GETITEMA, nColumn, (LPARAM)&hdi)) return FALSE;
5385 if (lpColumn->mask & LVCF_FMT)
5386 lpColumn->fmt = lpColumnInfo->fmt;
5388 if (lpColumn->mask & LVCF_WIDTH)
5389 lpColumn->cx = lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left;
5391 if (lpColumn->mask & LVCF_IMAGE)
5392 lpColumn->iImage = hdi.iImage;
5394 if (lpColumn->mask & LVCF_ORDER)
5395 lpColumn->iOrder = hdi.iOrder;
5397 if (lpColumn->mask & LVCF_SUBITEM)
5398 lpColumn->iSubItem = hdi.lParam;
5404 static BOOL LISTVIEW_GetColumnOrderArray(const LISTVIEW_INFO *infoPtr, INT iCount, LPINT lpiArray)
5406 TRACE("iCount=%d, lpiArray=%p\n", iCount, lpiArray);
5411 return SendMessageW(infoPtr->hwndHeader, HDM_GETORDERARRAY, iCount, (LPARAM)lpiArray);
5416 * Retrieves the column width.
5419 * [I] infoPtr : valid pointer to the listview structure
5420 * [I] int : column index
5423 * SUCCESS : column width
5426 static INT LISTVIEW_GetColumnWidth(const LISTVIEW_INFO *infoPtr, INT nColumn)
5428 INT nColumnWidth = 0;
5431 TRACE("nColumn=%d\n", nColumn);
5433 /* we have a 'column' in LIST and REPORT mode only */
5434 switch(infoPtr->dwStyle & LVS_TYPEMASK)
5437 nColumnWidth = infoPtr->nItemWidth;
5440 /* We are not using LISTVIEW_GetHeaderRect as this data is updated only after a HDM_ITEMCHANGED.
5441 * There is an application that subclasses the listview, calls LVM_GETCOLUMNWIDTH in the
5442 * HDM_ITEMCHANGED handler and goes into infinite recursion if it receives old data.
5444 * TODO: should we do the same in LVM_GETCOLUMN?
5446 hdItem.mask = HDI_WIDTH;
5447 if (!SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nColumn, (LPARAM)&hdItem))
5449 WARN("(%p): HDM_GETITEMW failed for item %d\n", infoPtr->hwndSelf, nColumn);
5452 nColumnWidth = hdItem.cxy;
5456 TRACE("nColumnWidth=%d\n", nColumnWidth);
5457 return nColumnWidth;
5462 * In list or report display mode, retrieves the number of items that can fit
5463 * vertically in the visible area. In icon or small icon display mode,
5464 * retrieves the total number of visible items.
5467 * [I] infoPtr : valid pointer to the listview structure
5470 * Number of fully visible items.
5472 static INT LISTVIEW_GetCountPerPage(const LISTVIEW_INFO *infoPtr)
5474 switch (infoPtr->dwStyle & LVS_TYPEMASK)
5478 return infoPtr->nItemCount;
5480 return LISTVIEW_GetCountPerColumn(infoPtr);
5482 return LISTVIEW_GetCountPerRow(infoPtr) * LISTVIEW_GetCountPerColumn(infoPtr);
5490 * Retrieves an image list handle.
5493 * [I] infoPtr : valid pointer to the listview structure
5494 * [I] nImageList : image list identifier
5497 * SUCCESS : image list handle
5500 static HIMAGELIST LISTVIEW_GetImageList(const LISTVIEW_INFO *infoPtr, INT nImageList)
5504 case LVSIL_NORMAL: return infoPtr->himlNormal;
5505 case LVSIL_SMALL: return infoPtr->himlSmall;
5506 case LVSIL_STATE: return infoPtr->himlState;
5511 /* LISTVIEW_GetISearchString */
5515 * Retrieves item attributes.
5518 * [I] hwnd : window handle
5519 * [IO] lpLVItem : item info
5520 * [I] isW : if TRUE, then lpLVItem is a LPLVITEMW,
5521 * if FALSE, then lpLVItem is a LPLVITEMA.
5524 * This is the internal 'GetItem' interface -- it tries to
5525 * be smart and avoid text copies, if possible, by modifying
5526 * lpLVItem->pszText to point to the text string. Please note
5527 * that this is not always possible (e.g. OWNERDATA), so on
5528 * entry you *must* supply valid values for pszText, and cchTextMax.
5529 * The only difference to the documented interface is that upon
5530 * return, you should use *only* the lpLVItem->pszText, rather than
5531 * the buffer pointer you provided on input. Most code already does
5532 * that, so it's not a problem.
5533 * For the two cases when the text must be copied (that is,
5534 * for LVM_GETITEM, and LVM_GETITEMTEXT), use LISTVIEW_GetItemExtT.
5540 static BOOL LISTVIEW_GetItemT(const LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem, BOOL isW)
5542 ITEMHDR callbackHdr = { LPSTR_TEXTCALLBACKW, I_IMAGECALLBACK };
5543 NMLVDISPINFOW dispInfo;
5549 TRACE("(lpLVItem=%s, isW=%d)\n", debuglvitem_t(lpLVItem, isW), isW);
5551 if (!lpLVItem || lpLVItem->iItem < 0 || lpLVItem->iItem >= infoPtr->nItemCount)
5554 if (lpLVItem->mask == 0) return TRUE;
5556 /* make a local copy */
5557 isubitem = lpLVItem->iSubItem;
5559 /* a quick optimization if all we're asked is the focus state
5560 * these queries are worth optimising since they are common,
5561 * and can be answered in constant time, without the heavy accesses */
5562 if ( (lpLVItem->mask == LVIF_STATE) && (lpLVItem->stateMask == LVIS_FOCUSED) &&
5563 !(infoPtr->uCallbackMask & LVIS_FOCUSED) )
5565 lpLVItem->state = 0;
5566 if (infoPtr->nFocusedItem == lpLVItem->iItem)
5567 lpLVItem->state |= LVIS_FOCUSED;
5571 ZeroMemory(&dispInfo, sizeof(dispInfo));
5573 /* if the app stores all the data, handle it separately */
5574 if (infoPtr->dwStyle & LVS_OWNERDATA)
5576 dispInfo.item.state = 0;
5578 /* apparently, we should not callback for lParam in LVS_OWNERDATA */
5579 if ((lpLVItem->mask & ~(LVIF_STATE | LVIF_PARAM)) ||
5580 ((lpLVItem->mask & LVIF_STATE) && (infoPtr->uCallbackMask & lpLVItem->stateMask)))
5582 UINT mask = lpLVItem->mask;
5584 /* NOTE: copy only fields which we _know_ are initialized, some apps
5585 * depend on the uninitialized fields being 0 */
5586 dispInfo.item.mask = lpLVItem->mask & ~LVIF_PARAM;
5587 dispInfo.item.iItem = lpLVItem->iItem;
5588 dispInfo.item.iSubItem = isubitem;
5589 if (lpLVItem->mask & LVIF_TEXT)
5591 if (lpLVItem->mask & LVIF_NORECOMPUTE)
5593 dispInfo.item.mask &= ~(LVIF_TEXT | LVIF_NORECOMPUTE);
5596 dispInfo.item.pszText = lpLVItem->pszText;
5597 dispInfo.item.cchTextMax = lpLVItem->cchTextMax;
5600 if (lpLVItem->mask & LVIF_STATE)
5601 dispInfo.item.stateMask = lpLVItem->stateMask & infoPtr->uCallbackMask;
5602 /* could be zeroed on LVIF_NORECOMPUTE case */
5603 if (dispInfo.item.mask != 0)
5605 notify_dispinfoT(infoPtr, LVN_GETDISPINFOW, &dispInfo, isW);
5606 dispInfo.item.stateMask = lpLVItem->stateMask;
5607 if (lpLVItem->mask & (LVIF_GROUPID|LVIF_COLUMNS))
5609 /* full size structure expected - _WIN32IE >= 0x560 */
5610 *lpLVItem = dispInfo.item;
5612 else if (lpLVItem->mask & LVIF_INDENT)
5614 /* indent member expected - _WIN32IE >= 0x300 */
5615 memcpy(lpLVItem, &dispInfo.item, offsetof( LVITEMW, iGroupId ));
5619 /* minimal structure expected */
5620 memcpy(lpLVItem, &dispInfo.item, offsetof( LVITEMW, iIndent ));
5622 lpLVItem->mask = mask;
5623 TRACE(" getdispinfo(1):lpLVItem=%s\n", debuglvitem_t(lpLVItem, isW));
5627 /* make sure lParam is zeroed out */
5628 if (lpLVItem->mask & LVIF_PARAM) lpLVItem->lParam = 0;
5630 /* callback marked pointer required here */
5631 if ((lpLVItem->mask & LVIF_TEXT) && (lpLVItem->mask & LVIF_NORECOMPUTE))
5632 lpLVItem->pszText = LPSTR_TEXTCALLBACKW;
5634 /* we store only a little state, so if we're not asked, we're done */
5635 if (!(lpLVItem->mask & LVIF_STATE) || isubitem) return TRUE;
5637 /* if focus is handled by us, report it */
5638 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_FOCUSED )
5640 lpLVItem->state &= ~LVIS_FOCUSED;
5641 if (infoPtr->nFocusedItem == lpLVItem->iItem)
5642 lpLVItem->state |= LVIS_FOCUSED;
5645 /* and do the same for selection, if we handle it */
5646 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_SELECTED )
5648 lpLVItem->state &= ~LVIS_SELECTED;
5649 if (ranges_contain(infoPtr->selectionRanges, lpLVItem->iItem))
5650 lpLVItem->state |= LVIS_SELECTED;
5656 /* find the item and subitem structures before we proceed */
5657 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, lpLVItem->iItem);
5658 lpItem = DPA_GetPtr(hdpaSubItems, 0);
5663 SUBITEM_INFO *lpSubItem = LISTVIEW_GetSubItemPtr(hdpaSubItems, isubitem);
5664 pItemHdr = lpSubItem ? &lpSubItem->hdr : &callbackHdr;
5667 WARN(" iSubItem invalid (%08x), ignored.\n", isubitem);
5672 pItemHdr = &lpItem->hdr;
5674 /* Do we need to query the state from the app? */
5675 if ((lpLVItem->mask & LVIF_STATE) && infoPtr->uCallbackMask && isubitem == 0)
5677 dispInfo.item.mask |= LVIF_STATE;
5678 dispInfo.item.stateMask = infoPtr->uCallbackMask;
5681 /* Do we need to enquire about the image? */
5682 if ((lpLVItem->mask & LVIF_IMAGE) && pItemHdr->iImage == I_IMAGECALLBACK &&
5683 (isubitem == 0 || (infoPtr->dwLvExStyle & LVS_EX_SUBITEMIMAGES)))
5685 dispInfo.item.mask |= LVIF_IMAGE;
5686 dispInfo.item.iImage = I_IMAGECALLBACK;
5689 /* Apps depend on calling back for text if it is NULL or LPSTR_TEXTCALLBACKW */
5690 if ((lpLVItem->mask & LVIF_TEXT) && !(lpLVItem->mask & LVIF_NORECOMPUTE) &&
5691 !is_textW(pItemHdr->pszText))
5693 dispInfo.item.mask |= LVIF_TEXT;
5694 dispInfo.item.pszText = lpLVItem->pszText;
5695 dispInfo.item.cchTextMax = lpLVItem->cchTextMax;
5696 if (dispInfo.item.pszText && dispInfo.item.cchTextMax > 0)
5697 *dispInfo.item.pszText = '\0';
5700 /* If we don't have all the requested info, query the application */
5701 if (dispInfo.item.mask != 0)
5703 dispInfo.item.iItem = lpLVItem->iItem;
5704 dispInfo.item.iSubItem = lpLVItem->iSubItem; /* yes: the original subitem */
5705 dispInfo.item.lParam = lpItem->lParam;
5706 notify_dispinfoT(infoPtr, LVN_GETDISPINFOW, &dispInfo, isW);
5707 TRACE(" getdispinfo(2):item=%s\n", debuglvitem_t(&dispInfo.item, isW));
5710 /* we should not store values for subitems */
5711 if (isubitem) dispInfo.item.mask &= ~LVIF_DI_SETITEM;
5713 /* Now, handle the iImage field */
5714 if (dispInfo.item.mask & LVIF_IMAGE)
5716 lpLVItem->iImage = dispInfo.item.iImage;
5717 if ((dispInfo.item.mask & LVIF_DI_SETITEM) && pItemHdr->iImage == I_IMAGECALLBACK)
5718 pItemHdr->iImage = dispInfo.item.iImage;
5720 else if (lpLVItem->mask & LVIF_IMAGE)
5722 if(isubitem == 0 || (infoPtr->dwLvExStyle & LVS_EX_SUBITEMIMAGES))
5723 lpLVItem->iImage = pItemHdr->iImage;
5725 lpLVItem->iImage = 0;
5728 /* The pszText field */
5729 if (dispInfo.item.mask & LVIF_TEXT)
5731 if ((dispInfo.item.mask & LVIF_DI_SETITEM) && pItemHdr->pszText)
5732 textsetptrT(&pItemHdr->pszText, dispInfo.item.pszText, isW);
5734 lpLVItem->pszText = dispInfo.item.pszText;
5736 else if (lpLVItem->mask & LVIF_TEXT)
5738 /* if LVN_GETDISPINFO's disabled with LVIF_NORECOMPUTE return callback placeholder */
5739 if (isW || !is_textW(pItemHdr->pszText)) lpLVItem->pszText = pItemHdr->pszText;
5740 else textcpynT(lpLVItem->pszText, isW, pItemHdr->pszText, TRUE, lpLVItem->cchTextMax);
5743 /* Next is the lParam field */
5744 if (dispInfo.item.mask & LVIF_PARAM)
5746 lpLVItem->lParam = dispInfo.item.lParam;
5747 if ((dispInfo.item.mask & LVIF_DI_SETITEM))
5748 lpItem->lParam = dispInfo.item.lParam;
5750 else if (lpLVItem->mask & LVIF_PARAM)
5751 lpLVItem->lParam = lpItem->lParam;
5753 /* if this is a subitem, we're done */
5754 if (isubitem) return TRUE;
5756 /* ... the state field (this one is different due to uCallbackmask) */
5757 if (lpLVItem->mask & LVIF_STATE)
5759 lpLVItem->state = lpItem->state & lpLVItem->stateMask;
5760 if (dispInfo.item.mask & LVIF_STATE)
5762 lpLVItem->state &= ~dispInfo.item.stateMask;
5763 lpLVItem->state |= (dispInfo.item.state & dispInfo.item.stateMask);
5765 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_FOCUSED )
5767 lpLVItem->state &= ~LVIS_FOCUSED;
5768 if (infoPtr->nFocusedItem == lpLVItem->iItem)
5769 lpLVItem->state |= LVIS_FOCUSED;
5771 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_SELECTED )
5773 lpLVItem->state &= ~LVIS_SELECTED;
5774 if (ranges_contain(infoPtr->selectionRanges, lpLVItem->iItem))
5775 lpLVItem->state |= LVIS_SELECTED;
5779 /* and last, but not least, the indent field */
5780 if (lpLVItem->mask & LVIF_INDENT)
5781 lpLVItem->iIndent = lpItem->iIndent;
5788 * Retrieves item attributes.
5791 * [I] hwnd : window handle
5792 * [IO] lpLVItem : item info
5793 * [I] isW : if TRUE, then lpLVItem is a LPLVITEMW,
5794 * if FALSE, then lpLVItem is a LPLVITEMA.
5797 * This is the external 'GetItem' interface -- it properly copies
5798 * the text in the provided buffer.
5804 static BOOL LISTVIEW_GetItemExtT(const LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem, BOOL isW)
5809 if (!lpLVItem || lpLVItem->iItem < 0 || lpLVItem->iItem >= infoPtr->nItemCount)
5812 pszText = lpLVItem->pszText;
5813 bResult = LISTVIEW_GetItemT(infoPtr, lpLVItem, isW);
5814 if (bResult && lpLVItem->pszText != pszText)
5816 if (lpLVItem->pszText != LPSTR_TEXTCALLBACKW)
5817 textcpynT(pszText, isW, lpLVItem->pszText, isW, lpLVItem->cchTextMax);
5819 pszText = LPSTR_TEXTCALLBACKW;
5821 lpLVItem->pszText = pszText;
5829 * Retrieves the position (upper-left) of the listview control item.
5830 * Note that for LVS_ICON style, the upper-left is that of the icon
5831 * and not the bounding box.
5834 * [I] infoPtr : valid pointer to the listview structure
5835 * [I] nItem : item index
5836 * [O] lpptPosition : coordinate information
5842 static BOOL LISTVIEW_GetItemPosition(const LISTVIEW_INFO *infoPtr, INT nItem, LPPOINT lpptPosition)
5844 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
5847 TRACE("(nItem=%d, lpptPosition=%p)\n", nItem, lpptPosition);
5849 if (!lpptPosition || nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
5851 LISTVIEW_GetOrigin(infoPtr, &Origin);
5852 LISTVIEW_GetItemOrigin(infoPtr, nItem, lpptPosition);
5854 if (uView == LVS_ICON)
5856 lpptPosition->x += (infoPtr->nItemWidth - infoPtr->iconSize.cx) / 2;
5857 lpptPosition->y += ICON_TOP_PADDING;
5859 lpptPosition->x += Origin.x;
5860 lpptPosition->y += Origin.y;
5862 TRACE (" lpptPosition=%s\n", wine_dbgstr_point(lpptPosition));
5869 * Retrieves the bounding rectangle for a listview control item.
5872 * [I] infoPtr : valid pointer to the listview structure
5873 * [I] nItem : item index
5874 * [IO] lprc : bounding rectangle coordinates
5875 * lprc->left specifies the portion of the item for which the bounding
5876 * rectangle will be retrieved.
5878 * LVIR_BOUNDS Returns the bounding rectangle of the entire item,
5879 * including the icon and label.
5882 * * Experiment shows that native control returns:
5883 * * width = min (48, length of text line)
5884 * * .left = position.x - (width - iconsize.cx)/2
5885 * * .right = .left + width
5886 * * height = #lines of text * ntmHeight + icon height + 8
5887 * * .top = position.y - 2
5888 * * .bottom = .top + height
5889 * * separation between items .y = itemSpacing.cy - height
5890 * * .x = itemSpacing.cx - width
5891 * LVIR_ICON Returns the bounding rectangle of the icon or small icon.
5894 * * Experiment shows that native control returns:
5895 * * width = iconSize.cx + 16
5896 * * .left = position.x - (width - iconsize.cx)/2
5897 * * .right = .left + width
5898 * * height = iconSize.cy + 4
5899 * * .top = position.y - 2
5900 * * .bottom = .top + height
5901 * * separation between items .y = itemSpacing.cy - height
5902 * * .x = itemSpacing.cx - width
5903 * LVIR_LABEL Returns the bounding rectangle of the item text.
5906 * * Experiment shows that native control returns:
5907 * * width = text length
5908 * * .left = position.x - width/2
5909 * * .right = .left + width
5910 * * height = ntmH * linecount + 2
5911 * * .top = position.y + iconSize.cy + 6
5912 * * .bottom = .top + height
5913 * * separation between items .y = itemSpacing.cy - height
5914 * * .x = itemSpacing.cx - width
5915 * LVIR_SELECTBOUNDS Returns the union of the LVIR_ICON and LVIR_LABEL
5916 * rectangles, but excludes columns in report view.
5923 * Note that the bounding rectangle of the label in the LVS_ICON view depends
5924 * upon whether the window has the focus currently and on whether the item
5925 * is the one with the focus. Ensure that the control's record of which
5926 * item has the focus agrees with the items' records.
5928 static BOOL LISTVIEW_GetItemRect(const LISTVIEW_INFO *infoPtr, INT nItem, LPRECT lprc)
5930 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
5931 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
5932 BOOL doLabel = TRUE, oversizedBox = FALSE;
5933 POINT Position, Origin;
5936 TRACE("(hwnd=%p, nItem=%d, lprc=%p)\n", infoPtr->hwndSelf, nItem, lprc);
5938 if (!lprc || nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
5940 LISTVIEW_GetOrigin(infoPtr, &Origin);
5941 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
5943 /* Be smart and try to figure out the minimum we have to do */
5944 if (lprc->left == LVIR_ICON) doLabel = FALSE;
5945 if (uView == LVS_REPORT && lprc->left == LVIR_BOUNDS) doLabel = FALSE;
5946 if (uView == LVS_ICON && lprc->left != LVIR_ICON &&
5947 infoPtr->bFocus && LISTVIEW_GetItemState(infoPtr, nItem, LVIS_FOCUSED))
5948 oversizedBox = TRUE;
5950 /* get what we need from the item before hand, so we make
5951 * only one request. This can speed up things, if data
5952 * is stored on the app side */
5954 if (uView == LVS_REPORT) lvItem.mask |= LVIF_INDENT;
5955 if (doLabel) lvItem.mask |= LVIF_TEXT;
5956 lvItem.iItem = nItem;
5957 lvItem.iSubItem = 0;
5958 lvItem.pszText = szDispText;
5959 lvItem.cchTextMax = DISP_TEXT_SIZE;
5960 if (lvItem.mask && !LISTVIEW_GetItemW(infoPtr, &lvItem)) return FALSE;
5961 /* we got the state already up, simulate it here, to avoid a reget */
5962 if (uView == LVS_ICON && (lprc->left != LVIR_ICON))
5964 lvItem.mask |= LVIF_STATE;
5965 lvItem.stateMask = LVIS_FOCUSED;
5966 lvItem.state = (oversizedBox ? LVIS_FOCUSED : 0);
5969 if (uView == LVS_REPORT && (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) && lprc->left == LVIR_SELECTBOUNDS)
5970 lprc->left = LVIR_BOUNDS;
5974 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, NULL, lprc, NULL, NULL);
5978 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, NULL, NULL, NULL, lprc);
5982 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, lprc, NULL, NULL, NULL, NULL);
5985 case LVIR_SELECTBOUNDS:
5986 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, lprc, NULL, NULL, NULL);
5990 WARN("Unknown value: %d\n", lprc->left);
5994 OffsetRect(lprc, Position.x + Origin.x, Position.y + Origin.y);
5996 TRACE(" rect=%s\n", wine_dbgstr_rect(lprc));
6003 * Retrieves the spacing between listview control items.
6006 * [I] infoPtr : valid pointer to the listview structure
6007 * [IO] lprc : rectangle to receive the output
6008 * on input, lprc->top = nSubItem
6009 * lprc->left = LVIR_ICON | LVIR_BOUNDS | LVIR_LABEL
6011 * NOTE: for subItem = 0, we should return the bounds of the _entire_ item,
6012 * not only those of the first column.
6013 * Fortunately, LISTVIEW_GetItemMetrics does the right thing.
6019 static BOOL LISTVIEW_GetSubItemRect(const LISTVIEW_INFO *infoPtr, INT nItem, LPRECT lprc)
6025 if (!lprc) return FALSE;
6027 nColumn = lprc->top;
6029 TRACE("(nItem=%d, nSubItem=%d)\n", nItem, lprc->top);
6030 /* On WinNT, a subitem of '0' calls LISTVIEW_GetItemRect */
6032 return LISTVIEW_GetItemRect(infoPtr, nItem, lprc);
6034 if ((infoPtr->dwStyle & LVS_TYPEMASK) != LVS_REPORT) return FALSE;
6036 /* special case for header items */
6039 if (lprc->left != LVIR_BOUNDS)
6041 FIXME("Only LVIR_BOUNDS is implemented for header, got %d\n", lprc->left);
6045 if (infoPtr->hwndHeader)
6046 return SendMessageW(infoPtr->hwndHeader, HDM_GETITEMRECT, lprc->top, (LPARAM)lprc);
6049 memset(lprc, 0, sizeof(RECT));
6054 if (!LISTVIEW_GetItemPosition(infoPtr, nItem, &Position)) return FALSE;
6056 if (nColumn < 0 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
6059 lvItem.iItem = nItem;
6060 lvItem.iSubItem = nColumn;
6062 if (lvItem.mask && !LISTVIEW_GetItemW(infoPtr, &lvItem)) return FALSE;
6066 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, NULL, lprc, NULL, NULL);
6071 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, lprc, NULL, NULL, NULL, NULL);
6075 ERR("Unknown bounds=%d\n", lprc->left);
6079 OffsetRect(lprc, Position.x, Position.y);
6086 * Retrieves the width of a label.
6089 * [I] infoPtr : valid pointer to the listview structure
6092 * SUCCESS : string width (in pixels)
6095 static INT LISTVIEW_GetLabelWidth(const LISTVIEW_INFO *infoPtr, INT nItem)
6097 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
6100 TRACE("(nItem=%d)\n", nItem);
6102 lvItem.mask = LVIF_TEXT;
6103 lvItem.iItem = nItem;
6104 lvItem.iSubItem = 0;
6105 lvItem.pszText = szDispText;
6106 lvItem.cchTextMax = DISP_TEXT_SIZE;
6107 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return 0;
6109 return LISTVIEW_GetStringWidthT(infoPtr, lvItem.pszText, TRUE);
6114 * Retrieves the spacing between listview control items.
6117 * [I] infoPtr : valid pointer to the listview structure
6118 * [I] bSmall : flag for small or large icon
6121 * Horizontal + vertical spacing
6123 static LONG LISTVIEW_GetItemSpacing(const LISTVIEW_INFO *infoPtr, BOOL bSmall)
6129 lResult = MAKELONG(infoPtr->iconSpacing.cx, infoPtr->iconSpacing.cy);
6133 if ((infoPtr->dwStyle & LVS_TYPEMASK) == LVS_ICON)
6134 lResult = MAKELONG(DEFAULT_COLUMN_WIDTH, GetSystemMetrics(SM_CXSMICON)+HEIGHT_PADDING);
6136 lResult = MAKELONG(infoPtr->nItemWidth, infoPtr->nItemHeight);
6143 * Retrieves the state of a listview control item.
6146 * [I] infoPtr : valid pointer to the listview structure
6147 * [I] nItem : item index
6148 * [I] uMask : state mask
6151 * State specified by the mask.
6153 static UINT LISTVIEW_GetItemState(const LISTVIEW_INFO *infoPtr, INT nItem, UINT uMask)
6157 if (nItem < 0 || nItem >= infoPtr->nItemCount) return 0;
6159 lvItem.iItem = nItem;
6160 lvItem.iSubItem = 0;
6161 lvItem.mask = LVIF_STATE;
6162 lvItem.stateMask = uMask;
6163 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return 0;
6165 return lvItem.state & uMask;
6170 * Retrieves the text of a listview control item or subitem.
6173 * [I] hwnd : window handle
6174 * [I] nItem : item index
6175 * [IO] lpLVItem : item information
6176 * [I] isW : TRUE if lpLVItem is Unicode
6179 * SUCCESS : string length
6182 static INT LISTVIEW_GetItemTextT(const LISTVIEW_INFO *infoPtr, INT nItem, LPLVITEMW lpLVItem, BOOL isW)
6184 if (!lpLVItem || nItem < 0 || nItem >= infoPtr->nItemCount) return 0;
6186 lpLVItem->mask = LVIF_TEXT;
6187 lpLVItem->iItem = nItem;
6188 if (!LISTVIEW_GetItemExtT(infoPtr, lpLVItem, isW)) return 0;
6190 return textlenT(lpLVItem->pszText, isW);
6195 * Searches for an item based on properties + relationships.
6198 * [I] infoPtr : valid pointer to the listview structure
6199 * [I] nItem : item index
6200 * [I] uFlags : relationship flag
6203 * SUCCESS : item index
6206 static INT LISTVIEW_GetNextItem(const LISTVIEW_INFO *infoPtr, INT nItem, UINT uFlags)
6208 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
6210 LVFINDINFOW lvFindInfo;
6211 INT nCountPerColumn;
6215 TRACE("nItem=%d, uFlags=%x, nItemCount=%d\n", nItem, uFlags, infoPtr->nItemCount);
6216 if (nItem < -1 || nItem >= infoPtr->nItemCount) return -1;
6218 ZeroMemory(&lvFindInfo, sizeof(lvFindInfo));
6220 if (uFlags & LVNI_CUT)
6223 if (uFlags & LVNI_DROPHILITED)
6224 uMask |= LVIS_DROPHILITED;
6226 if (uFlags & LVNI_FOCUSED)
6227 uMask |= LVIS_FOCUSED;
6229 if (uFlags & LVNI_SELECTED)
6230 uMask |= LVIS_SELECTED;
6232 /* if we're asked for the focused item, that's only one,
6233 * so it's worth optimizing */
6234 if (uFlags & LVNI_FOCUSED)
6236 if ((LISTVIEW_GetItemState(infoPtr, infoPtr->nFocusedItem, uMask) & uMask) != uMask) return -1;
6237 return (infoPtr->nFocusedItem == nItem) ? -1 : infoPtr->nFocusedItem;
6240 if (uFlags & LVNI_ABOVE)
6242 if ((uView == LVS_LIST) || (uView == LVS_REPORT))
6247 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
6253 /* Special case for autoarrange - move 'til the top of a list */
6254 if (is_autoarrange(infoPtr))
6256 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
6257 while (nItem - nCountPerRow >= 0)
6259 nItem -= nCountPerRow;
6260 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
6265 lvFindInfo.flags = LVFI_NEARESTXY;
6266 lvFindInfo.vkDirection = VK_UP;
6267 LISTVIEW_GetItemPosition(infoPtr, nItem, &lvFindInfo.pt);
6268 while ((nItem = LISTVIEW_FindItemW(infoPtr, nItem, &lvFindInfo)) != -1)
6270 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
6275 else if (uFlags & LVNI_BELOW)
6277 if ((uView == LVS_LIST) || (uView == LVS_REPORT))
6279 while (nItem < infoPtr->nItemCount)
6282 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
6288 /* Special case for autoarrange - move 'til the bottom of a list */
6289 if (is_autoarrange(infoPtr))
6291 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
6292 while (nItem + nCountPerRow < infoPtr->nItemCount )
6294 nItem += nCountPerRow;
6295 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
6300 lvFindInfo.flags = LVFI_NEARESTXY;
6301 lvFindInfo.vkDirection = VK_DOWN;
6302 LISTVIEW_GetItemPosition(infoPtr, nItem, &lvFindInfo.pt);
6303 while ((nItem = LISTVIEW_FindItemW(infoPtr, nItem, &lvFindInfo)) != -1)
6305 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
6310 else if (uFlags & LVNI_TOLEFT)
6312 if (uView == LVS_LIST)
6314 nCountPerColumn = LISTVIEW_GetCountPerColumn(infoPtr);
6315 while (nItem - nCountPerColumn >= 0)
6317 nItem -= nCountPerColumn;
6318 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
6322 else if ((uView == LVS_SMALLICON) || (uView == LVS_ICON))
6324 /* Special case for autoarrange - move 'til the beginning of a row */
6325 if (is_autoarrange(infoPtr))
6327 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
6328 while (nItem % nCountPerRow > 0)
6331 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
6336 lvFindInfo.flags = LVFI_NEARESTXY;
6337 lvFindInfo.vkDirection = VK_LEFT;
6338 LISTVIEW_GetItemPosition(infoPtr, nItem, &lvFindInfo.pt);
6339 while ((nItem = LISTVIEW_FindItemW(infoPtr, nItem, &lvFindInfo)) != -1)
6341 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
6346 else if (uFlags & LVNI_TORIGHT)
6348 if (uView == LVS_LIST)
6350 nCountPerColumn = LISTVIEW_GetCountPerColumn(infoPtr);
6351 while (nItem + nCountPerColumn < infoPtr->nItemCount)
6353 nItem += nCountPerColumn;
6354 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
6358 else if ((uView == LVS_SMALLICON) || (uView == LVS_ICON))
6360 /* Special case for autoarrange - move 'til the end of a row */
6361 if (is_autoarrange(infoPtr))
6363 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
6364 while (nItem % nCountPerRow < nCountPerRow - 1 )
6367 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
6372 lvFindInfo.flags = LVFI_NEARESTXY;
6373 lvFindInfo.vkDirection = VK_RIGHT;
6374 LISTVIEW_GetItemPosition(infoPtr, nItem, &lvFindInfo.pt);
6375 while ((nItem = LISTVIEW_FindItemW(infoPtr, nItem, &lvFindInfo)) != -1)
6377 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
6386 /* search by index */
6387 for (i = nItem; i < infoPtr->nItemCount; i++)
6389 if ((LISTVIEW_GetItemState(infoPtr, i, uMask) & uMask) == uMask)
6397 /* LISTVIEW_GetNumberOfWorkAreas */
6401 * Retrieves the origin coordinates when in icon or small icon display mode.
6404 * [I] infoPtr : valid pointer to the listview structure
6405 * [O] lpptOrigin : coordinate information
6410 static void LISTVIEW_GetOrigin(const LISTVIEW_INFO *infoPtr, LPPOINT lpptOrigin)
6412 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
6413 INT nHorzPos = 0, nVertPos = 0;
6414 SCROLLINFO scrollInfo;
6416 scrollInfo.cbSize = sizeof(SCROLLINFO);
6417 scrollInfo.fMask = SIF_POS;
6419 if (GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo))
6420 nHorzPos = scrollInfo.nPos;
6421 if (GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
6422 nVertPos = scrollInfo.nPos;
6424 TRACE("nHorzPos=%d, nVertPos=%d\n", nHorzPos, nVertPos);
6426 lpptOrigin->x = infoPtr->rcList.left;
6427 lpptOrigin->y = infoPtr->rcList.top;
6428 if (uView == LVS_LIST)
6429 nHorzPos *= infoPtr->nItemWidth;
6430 else if (uView == LVS_REPORT)
6431 nVertPos *= infoPtr->nItemHeight;
6433 lpptOrigin->x -= nHorzPos;
6434 lpptOrigin->y -= nVertPos;
6436 TRACE(" origin=%s\n", wine_dbgstr_point(lpptOrigin));
6441 * Retrieves the width of a string.
6444 * [I] hwnd : window handle
6445 * [I] lpszText : text string to process
6446 * [I] isW : TRUE if lpszText is Unicode, FALSE otherwise
6449 * SUCCESS : string width (in pixels)
6452 static INT LISTVIEW_GetStringWidthT(const LISTVIEW_INFO *infoPtr, LPCWSTR lpszText, BOOL isW)
6457 if (is_textT(lpszText, isW))
6459 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
6460 HDC hdc = GetDC(infoPtr->hwndSelf);
6461 HFONT hOldFont = SelectObject(hdc, hFont);
6464 GetTextExtentPointW(hdc, lpszText, lstrlenW(lpszText), &stringSize);
6466 GetTextExtentPointA(hdc, (LPCSTR)lpszText, lstrlenA((LPCSTR)lpszText), &stringSize);
6467 SelectObject(hdc, hOldFont);
6468 ReleaseDC(infoPtr->hwndSelf, hdc);
6470 return stringSize.cx;
6475 * Determines which listview item is located at the specified position.
6478 * [I] infoPtr : valid pointer to the listview structure
6479 * [IO] lpht : hit test information
6480 * [I] subitem : fill out iSubItem.
6481 * [I] select : return the index only if the hit selects the item
6484 * (mm 20001022): We must not allow iSubItem to be touched, for
6485 * an app might pass only a structure with space up to iItem!
6486 * (MS Office 97 does that for instance in the file open dialog)
6489 * SUCCESS : item index
6492 static INT LISTVIEW_HitTest(const LISTVIEW_INFO *infoPtr, LPLVHITTESTINFO lpht, BOOL subitem, BOOL select)
6494 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
6495 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
6496 RECT rcBox, rcBounds, rcState, rcIcon, rcLabel, rcSearch;
6497 POINT Origin, Position, opt;
6502 TRACE("(pt=%s, subitem=%d, select=%d)\n", wine_dbgstr_point(&lpht->pt), subitem, select);
6506 if (subitem) lpht->iSubItem = 0;
6508 if (infoPtr->rcList.left > lpht->pt.x)
6509 lpht->flags |= LVHT_TOLEFT;
6510 else if (infoPtr->rcList.right < lpht->pt.x)
6511 lpht->flags |= LVHT_TORIGHT;
6513 if (infoPtr->rcList.top > lpht->pt.y)
6514 lpht->flags |= LVHT_ABOVE;
6515 else if (infoPtr->rcList.bottom < lpht->pt.y)
6516 lpht->flags |= LVHT_BELOW;
6518 TRACE("lpht->flags=0x%x\n", lpht->flags);
6519 if (lpht->flags) return -1;
6521 lpht->flags |= LVHT_NOWHERE;
6523 LISTVIEW_GetOrigin(infoPtr, &Origin);
6525 /* first deal with the large items */
6526 rcSearch.left = lpht->pt.x;
6527 rcSearch.top = lpht->pt.y;
6528 rcSearch.right = rcSearch.left + 1;
6529 rcSearch.bottom = rcSearch.top + 1;
6531 iterator_frameditems(&i, infoPtr, &rcSearch);
6532 iterator_next(&i); /* go to first item in the sequence */
6534 iterator_destroy(&i);
6536 TRACE("lpht->iItem=%d\n", iItem);
6537 if (iItem == -1) return -1;
6539 lvItem.mask = LVIF_STATE | LVIF_TEXT;
6540 if (uView == LVS_REPORT) lvItem.mask |= LVIF_INDENT;
6541 lvItem.stateMask = LVIS_STATEIMAGEMASK;
6542 if (uView == LVS_ICON) lvItem.stateMask |= LVIS_FOCUSED;
6543 lvItem.iItem = iItem;
6544 lvItem.iSubItem = 0;
6545 lvItem.pszText = szDispText;
6546 lvItem.cchTextMax = DISP_TEXT_SIZE;
6547 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return -1;
6548 if (!infoPtr->bFocus) lvItem.state &= ~LVIS_FOCUSED;
6550 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, &rcBox, NULL, &rcIcon, &rcState, &rcLabel);
6551 LISTVIEW_GetItemOrigin(infoPtr, iItem, &Position);
6552 opt.x = lpht->pt.x - Position.x - Origin.x;
6553 opt.y = lpht->pt.y - Position.y - Origin.y;
6555 if (uView == LVS_REPORT)
6559 UnionRect(&rcBounds, &rcIcon, &rcLabel);
6560 UnionRect(&rcBounds, &rcBounds, &rcState);
6562 TRACE("rcBounds=%s\n", wine_dbgstr_rect(&rcBounds));
6563 if (!PtInRect(&rcBounds, opt)) return -1;
6565 if (PtInRect(&rcIcon, opt))
6566 lpht->flags |= LVHT_ONITEMICON;
6567 else if (PtInRect(&rcLabel, opt))
6568 lpht->flags |= LVHT_ONITEMLABEL;
6569 else if (infoPtr->himlState && STATEIMAGEINDEX(lvItem.state) && PtInRect(&rcState, opt))
6570 lpht->flags |= LVHT_ONITEMSTATEICON;
6571 if (lpht->flags & LVHT_ONITEM)
6572 lpht->flags &= ~LVHT_NOWHERE;
6574 TRACE("lpht->flags=0x%x\n", lpht->flags);
6575 if (uView == LVS_REPORT && subitem)
6579 rcBounds.right = rcBounds.left;
6580 for (j = 0; j < DPA_GetPtrCount(infoPtr->hdpaColumns); j++)
6582 rcBounds.left = rcBounds.right;
6583 rcBounds.right += LISTVIEW_GetColumnWidth(infoPtr, j);
6584 if (PtInRect(&rcBounds, opt))
6592 if (select && !(uView == LVS_REPORT &&
6593 ((infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) ||
6594 (infoPtr->dwStyle & LVS_OWNERDRAWFIXED))))
6596 if (uView == LVS_REPORT)
6598 UnionRect(&rcBounds, &rcIcon, &rcLabel);
6599 UnionRect(&rcBounds, &rcBounds, &rcState);
6601 if (!PtInRect(&rcBounds, opt)) iItem = -1;
6603 return lpht->iItem = iItem;
6608 * Inserts a new item in the listview control.
6611 * [I] infoPtr : valid pointer to the listview structure
6612 * [I] lpLVItem : item information
6613 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
6616 * SUCCESS : new item index
6619 static INT LISTVIEW_InsertItemT(LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem, BOOL isW)
6621 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
6626 BOOL is_sorted, has_changed;
6628 HWND hwndSelf = infoPtr->hwndSelf;
6630 TRACE("(lpLVItem=%s, isW=%d)\n", debuglvitem_t(lpLVItem, isW), isW);
6632 if (infoPtr->dwStyle & LVS_OWNERDATA) return infoPtr->nItemCount++;
6634 /* make sure it's an item, and not a subitem; cannot insert a subitem */
6635 if (!lpLVItem || lpLVItem->iSubItem) return -1;
6637 if (!is_assignable_item(lpLVItem, infoPtr->dwStyle)) return -1;
6639 if (!(lpItem = Alloc(sizeof(ITEM_INFO)))) return -1;
6641 /* insert item in listview control data structure */
6642 if ( !(hdpaSubItems = DPA_Create(8)) ) goto fail;
6643 if ( !DPA_SetPtr(hdpaSubItems, 0, lpItem) ) assert (FALSE);
6645 is_sorted = (infoPtr->dwStyle & (LVS_SORTASCENDING | LVS_SORTDESCENDING)) &&
6646 !(infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (LPSTR_TEXTCALLBACKW != lpLVItem->pszText);
6648 if (lpLVItem->iItem < 0 && !is_sorted) return -1;
6650 /* calculate new item index */
6657 while (i < infoPtr->nItemCount)
6659 hItem = DPA_GetPtr( infoPtr->hdpaItems, i);
6660 item_s = (ITEM_INFO*)DPA_GetPtr(hItem, 0);
6662 cmpv = textcmpWT(item_s->hdr.pszText, lpLVItem->pszText, TRUE);
6663 if (infoPtr->dwStyle & LVS_SORTDESCENDING) cmpv *= -1;
6665 if (cmpv >= 0) break;
6671 nItem = min(lpLVItem->iItem, infoPtr->nItemCount);
6673 TRACE(" inserting at %d, sorted=%d, count=%d, iItem=%d\n", nItem, is_sorted, infoPtr->nItemCount, lpLVItem->iItem);
6674 nItem = DPA_InsertPtr( infoPtr->hdpaItems, nItem, hdpaSubItems );
6675 if (nItem == -1) goto fail;
6676 infoPtr->nItemCount++;
6678 /* shift indices first so they don't get tangled */
6679 LISTVIEW_ShiftIndices(infoPtr, nItem, 1);
6681 /* set the item attributes */
6682 if (lpLVItem->mask & (LVIF_GROUPID|LVIF_COLUMNS))
6684 /* full size structure expected - _WIN32IE >= 0x560 */
6687 else if (lpLVItem->mask & LVIF_INDENT)
6689 /* indent member expected - _WIN32IE >= 0x300 */
6690 memcpy(&item, lpLVItem, offsetof( LVITEMW, iGroupId ));
6694 /* minimal structure expected */
6695 memcpy(&item, lpLVItem, offsetof( LVITEMW, iIndent ));
6698 if (infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES)
6700 item.mask |= LVIF_STATE;
6701 item.stateMask |= LVIS_STATEIMAGEMASK;
6702 item.state &= ~LVIS_STATEIMAGEMASK;
6703 item.state |= INDEXTOSTATEIMAGEMASK(1);
6705 if (!set_main_item(infoPtr, &item, TRUE, isW, &has_changed)) goto undo;
6707 /* make room for the position, if we are in the right mode */
6708 if ((uView == LVS_SMALLICON) || (uView == LVS_ICON))
6710 if (DPA_InsertPtr(infoPtr->hdpaPosX, nItem, 0) == -1)
6712 if (DPA_InsertPtr(infoPtr->hdpaPosY, nItem, 0) == -1)
6714 DPA_DeletePtr(infoPtr->hdpaPosX, nItem);
6719 /* send LVN_INSERTITEM notification */
6720 ZeroMemory(&nmlv, sizeof(NMLISTVIEW));
6722 nmlv.lParam = lpItem->lParam;
6723 notify_listview(infoPtr, LVN_INSERTITEM, &nmlv);
6724 if (!IsWindow(hwndSelf))
6727 /* align items (set position of each item) */
6728 if ((uView == LVS_SMALLICON || uView == LVS_ICON))
6732 if (infoPtr->dwStyle & LVS_ALIGNLEFT)
6733 LISTVIEW_NextIconPosLeft(infoPtr, &pt);
6735 LISTVIEW_NextIconPosTop(infoPtr, &pt);
6737 LISTVIEW_MoveIconTo(infoPtr, nItem, &pt, TRUE);
6740 /* now is the invalidation fun */
6741 LISTVIEW_ScrollOnInsert(infoPtr, nItem, 1);
6745 LISTVIEW_ShiftIndices(infoPtr, nItem, -1);
6746 DPA_DeletePtr(infoPtr->hdpaItems, nItem);
6747 infoPtr->nItemCount--;
6749 DPA_DeletePtr(hdpaSubItems, 0);
6750 DPA_Destroy (hdpaSubItems);
6757 * Redraws a range of items.
6760 * [I] infoPtr : valid pointer to the listview structure
6761 * [I] nFirst : first item
6762 * [I] nLast : last item
6768 static BOOL LISTVIEW_RedrawItems(const LISTVIEW_INFO *infoPtr, INT nFirst, INT nLast)
6772 if (nLast < nFirst || min(nFirst, nLast) < 0 ||
6773 max(nFirst, nLast) >= infoPtr->nItemCount)
6776 for (i = nFirst; i <= nLast; i++)
6777 LISTVIEW_InvalidateItem(infoPtr, i);
6784 * Scroll the content of a listview.
6787 * [I] infoPtr : valid pointer to the listview structure
6788 * [I] dx : horizontal scroll amount in pixels
6789 * [I] dy : vertical scroll amount in pixels
6796 * If the control is in report mode (LVS_REPORT) the control can
6797 * be scrolled only in line increments. "dy" will be rounded to the
6798 * nearest number of pixels that are a whole line. Ex: if line height
6799 * is 16 and an 8 is passed, the list will be scrolled by 16. If a 7
6800 * is passed, then the scroll will be 0. (per MSDN 7/2002)
6802 * For: (per experimentation with native control and CSpy ListView)
6803 * LVS_ICON dy=1 = 1 pixel (vertical only)
6805 * LVS_SMALLICON dy=1 = 1 pixel (vertical only)
6807 * LVS_LIST dx=1 = 1 column (horizontal only)
6808 * but will only scroll 1 column per message
6809 * no matter what the value.
6810 * dy must be 0 or FALSE returned.
6811 * LVS_REPORT dx=1 = 1 pixel
6815 static BOOL LISTVIEW_Scroll(LISTVIEW_INFO *infoPtr, INT dx, INT dy)
6817 switch(infoPtr->dwStyle & LVS_TYPEMASK) {
6819 dy += (dy < 0 ? -1 : 1) * infoPtr->nItemHeight/2;
6820 dy /= infoPtr->nItemHeight;
6823 if (dy != 0) return FALSE;
6830 if (dx != 0) LISTVIEW_HScroll(infoPtr, SB_INTERNAL, dx, 0);
6831 if (dy != 0) LISTVIEW_VScroll(infoPtr, SB_INTERNAL, dy, 0);
6838 * Sets the background color.
6841 * [I] infoPtr : valid pointer to the listview structure
6842 * [I] clrBk : background color
6848 static BOOL LISTVIEW_SetBkColor(LISTVIEW_INFO *infoPtr, COLORREF clrBk)
6850 TRACE("(clrBk=%x)\n", clrBk);
6852 if(infoPtr->clrBk != clrBk) {
6853 if (infoPtr->clrBk != CLR_NONE) DeleteObject(infoPtr->hBkBrush);
6854 infoPtr->clrBk = clrBk;
6855 if (clrBk == CLR_NONE)
6856 infoPtr->hBkBrush = (HBRUSH)GetClassLongPtrW(infoPtr->hwndSelf, GCLP_HBRBACKGROUND);
6858 infoPtr->hBkBrush = CreateSolidBrush(clrBk);
6859 LISTVIEW_InvalidateList(infoPtr);
6865 /* LISTVIEW_SetBkImage */
6867 /*** Helper for {Insert,Set}ColumnT *only* */
6868 static void column_fill_hditem(const LISTVIEW_INFO *infoPtr, HDITEMW *lphdi, INT nColumn,
6869 const LVCOLUMNW *lpColumn, BOOL isW)
6871 if (lpColumn->mask & LVCF_FMT)
6873 /* format member is valid */
6874 lphdi->mask |= HDI_FORMAT;
6876 /* set text alignment (leftmost column must be left-aligned) */
6877 if (nColumn == 0 || (lpColumn->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_LEFT)
6878 lphdi->fmt |= HDF_LEFT;
6879 else if ((lpColumn->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_RIGHT)
6880 lphdi->fmt |= HDF_RIGHT;
6881 else if ((lpColumn->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_CENTER)
6882 lphdi->fmt |= HDF_CENTER;
6884 if (lpColumn->fmt & LVCFMT_BITMAP_ON_RIGHT)
6885 lphdi->fmt |= HDF_BITMAP_ON_RIGHT;
6887 if (lpColumn->fmt & LVCFMT_COL_HAS_IMAGES)
6889 lphdi->fmt |= HDF_IMAGE;
6890 lphdi->iImage = I_IMAGECALLBACK;
6894 if (lpColumn->mask & LVCF_WIDTH)
6896 lphdi->mask |= HDI_WIDTH;
6897 if(lpColumn->cx == LVSCW_AUTOSIZE_USEHEADER)
6899 /* make it fill the remainder of the controls width */
6903 for(item_index = 0; item_index < (nColumn - 1); item_index++)
6905 LISTVIEW_GetHeaderRect(infoPtr, item_index, &rcHeader);
6906 lphdi->cxy += rcHeader.right - rcHeader.left;
6909 /* retrieve the layout of the header */
6910 GetClientRect(infoPtr->hwndSelf, &rcHeader);
6911 TRACE("start cxy=%d rcHeader=%s\n", lphdi->cxy, wine_dbgstr_rect(&rcHeader));
6913 lphdi->cxy = (rcHeader.right - rcHeader.left) - lphdi->cxy;
6916 lphdi->cxy = lpColumn->cx;
6919 if (lpColumn->mask & LVCF_TEXT)
6921 lphdi->mask |= HDI_TEXT | HDI_FORMAT;
6922 lphdi->fmt |= HDF_STRING;
6923 lphdi->pszText = lpColumn->pszText;
6924 lphdi->cchTextMax = textlenT(lpColumn->pszText, isW);
6927 if (lpColumn->mask & LVCF_IMAGE)
6929 lphdi->mask |= HDI_IMAGE;
6930 lphdi->iImage = lpColumn->iImage;
6933 if (lpColumn->mask & LVCF_ORDER)
6935 lphdi->mask |= HDI_ORDER;
6936 lphdi->iOrder = lpColumn->iOrder;
6943 * Inserts a new column.
6946 * [I] infoPtr : valid pointer to the listview structure
6947 * [I] nColumn : column index
6948 * [I] lpColumn : column information
6949 * [I] isW : TRUE if lpColumn is Unicode, FALSE otherwise
6952 * SUCCESS : new column index
6955 static INT LISTVIEW_InsertColumnT(LISTVIEW_INFO *infoPtr, INT nColumn,
6956 const LVCOLUMNW *lpColumn, BOOL isW)
6958 COLUMN_INFO *lpColumnInfo;
6961 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
6963 TRACE("(nColumn=%d, lpColumn=%s, isW=%d)\n", nColumn, debuglvcolumn_t(lpColumn, isW), isW);
6965 if (!lpColumn || nColumn < 0) return -1;
6966 nColumn = min(nColumn, DPA_GetPtrCount(infoPtr->hdpaColumns));
6968 ZeroMemory(&hdi, sizeof(HDITEMW));
6969 column_fill_hditem(infoPtr, &hdi, nColumn, lpColumn, isW);
6972 * A mask not including LVCF_WIDTH turns into a mask of width, width 10
6973 * (can be seen in SPY) otherwise column never gets added.
6975 if (!(lpColumn->mask & LVCF_WIDTH)) {
6976 hdi.mask |= HDI_WIDTH;
6981 * when the iSubItem is available Windows copies it to the header lParam. It seems
6982 * to happen only in LVM_INSERTCOLUMN - not in LVM_SETCOLUMN
6984 if (lpColumn->mask & LVCF_SUBITEM)
6986 hdi.mask |= HDI_LPARAM;
6987 hdi.lParam = lpColumn->iSubItem;
6990 /* create header if not present */
6991 LISTVIEW_CreateHeader(infoPtr);
6992 if (!(LVS_NOCOLUMNHEADER & infoPtr->dwStyle) &&
6993 (LVS_REPORT == uView) && (WS_VISIBLE & infoPtr->dwStyle))
6995 ShowWindow(infoPtr->hwndHeader, SW_SHOWNORMAL);
6998 /* insert item in header control */
6999 nNewColumn = SendMessageW(infoPtr->hwndHeader,
7000 isW ? HDM_INSERTITEMW : HDM_INSERTITEMA,
7001 (WPARAM)nColumn, (LPARAM)&hdi);
7002 if (nNewColumn == -1) return -1;
7003 if (nNewColumn != nColumn) ERR("nColumn=%d, nNewColumn=%d\n", nColumn, nNewColumn);
7005 /* create our own column info */
7006 if (!(lpColumnInfo = Alloc(sizeof(COLUMN_INFO)))) goto fail;
7007 if (DPA_InsertPtr(infoPtr->hdpaColumns, nNewColumn, lpColumnInfo) == -1) goto fail;
7009 if (lpColumn->mask & LVCF_FMT) lpColumnInfo->fmt = lpColumn->fmt;
7010 if (!SendMessageW(infoPtr->hwndHeader, HDM_GETITEMRECT, nNewColumn, (LPARAM)&lpColumnInfo->rcHeader))
7013 /* now we have to actually adjust the data */
7014 if (!(infoPtr->dwStyle & LVS_OWNERDATA) && infoPtr->nItemCount > 0)
7016 SUBITEM_INFO *lpSubItem;
7020 for (nItem = 0; nItem < infoPtr->nItemCount; nItem++)
7022 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, nItem);
7023 for (i = 1; i < DPA_GetPtrCount(hdpaSubItems); i++)
7025 lpSubItem = DPA_GetPtr(hdpaSubItems, i);
7026 if (lpSubItem->iSubItem >= nNewColumn)
7027 lpSubItem->iSubItem++;
7032 /* make space for the new column */
7033 LISTVIEW_ScrollColumns(infoPtr, nNewColumn + 1, lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left);
7034 LISTVIEW_UpdateItemSize(infoPtr);
7039 if (nNewColumn != -1) SendMessageW(infoPtr->hwndHeader, HDM_DELETEITEM, nNewColumn, 0);
7042 DPA_DeletePtr(infoPtr->hdpaColumns, nNewColumn);
7050 * Sets the attributes of a header item.
7053 * [I] infoPtr : valid pointer to the listview structure
7054 * [I] nColumn : column index
7055 * [I] lpColumn : column attributes
7056 * [I] isW: if TRUE, then lpColumn is a LPLVCOLUMNW, else it is a LPLVCOLUMNA
7062 static BOOL LISTVIEW_SetColumnT(const LISTVIEW_INFO *infoPtr, INT nColumn,
7063 const LVCOLUMNW *lpColumn, BOOL isW)
7065 HDITEMW hdi, hdiget;
7068 TRACE("(nColumn=%d, lpColumn=%s, isW=%d)\n", nColumn, debuglvcolumn_t(lpColumn, isW), isW);
7070 if (!lpColumn || nColumn < 0 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
7072 ZeroMemory(&hdi, sizeof(HDITEMW));
7073 if (lpColumn->mask & LVCF_FMT)
7075 hdi.mask |= HDI_FORMAT;
7076 hdiget.mask = HDI_FORMAT;
7077 if (Header_GetItemW(infoPtr->hwndHeader, nColumn, &hdiget))
7078 hdi.fmt = hdiget.fmt & HDF_STRING;
7080 column_fill_hditem(infoPtr, &hdi, nColumn, lpColumn, isW);
7082 /* set header item attributes */
7083 bResult = SendMessageW(infoPtr->hwndHeader, isW ? HDM_SETITEMW : HDM_SETITEMA, (WPARAM)nColumn, (LPARAM)&hdi);
7084 if (!bResult) return FALSE;
7086 if (lpColumn->mask & LVCF_FMT)
7088 COLUMN_INFO *lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, nColumn);
7089 int oldFmt = lpColumnInfo->fmt;
7091 lpColumnInfo->fmt = lpColumn->fmt;
7092 if ((oldFmt ^ lpColumn->fmt) & (LVCFMT_JUSTIFYMASK | LVCFMT_IMAGE))
7094 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
7095 if (uView == LVS_REPORT) LISTVIEW_InvalidateColumn(infoPtr, nColumn);
7104 * Sets the column order array
7107 * [I] infoPtr : valid pointer to the listview structure
7108 * [I] iCount : number of elements in column order array
7109 * [I] lpiArray : pointer to column order array
7115 static BOOL LISTVIEW_SetColumnOrderArray(const LISTVIEW_INFO *infoPtr, INT iCount, const INT *lpiArray)
7117 FIXME("iCount %d lpiArray %p\n", iCount, lpiArray);
7128 * Sets the width of a column
7131 * [I] infoPtr : valid pointer to the listview structure
7132 * [I] nColumn : column index
7133 * [I] cx : column width
7139 static BOOL LISTVIEW_SetColumnWidth(LISTVIEW_INFO *infoPtr, INT nColumn, INT cx)
7141 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
7142 WCHAR szDispText[DISP_TEXT_SIZE] = { 0 };
7146 TRACE("(nColumn=%d, cx=%d\n", nColumn, cx);
7148 /* set column width only if in report or list mode */
7149 if (uView != LVS_REPORT && uView != LVS_LIST) return FALSE;
7151 /* take care of invalid cx values */
7152 if(uView == LVS_REPORT && cx < -2) cx = LVSCW_AUTOSIZE;
7153 else if (uView == LVS_LIST && cx < 1) return FALSE;
7155 /* resize all columns if in LVS_LIST mode */
7156 if(uView == LVS_LIST)
7158 infoPtr->nItemWidth = cx;
7159 LISTVIEW_InvalidateList(infoPtr);
7163 if (nColumn < 0 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
7165 if (cx == LVSCW_AUTOSIZE || (cx == LVSCW_AUTOSIZE_USEHEADER && nColumn < DPA_GetPtrCount(infoPtr->hdpaColumns) -1))
7170 lvItem.mask = LVIF_TEXT;
7172 lvItem.iSubItem = nColumn;
7173 lvItem.pszText = szDispText;
7174 lvItem.cchTextMax = DISP_TEXT_SIZE;
7175 for (; lvItem.iItem < infoPtr->nItemCount; lvItem.iItem++)
7177 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) continue;
7178 nLabelWidth = LISTVIEW_GetStringWidthT(infoPtr, lvItem.pszText, TRUE);
7179 if (max_cx < nLabelWidth) max_cx = nLabelWidth;
7181 if (infoPtr->himlSmall && (nColumn == 0 || (LISTVIEW_GetColumnInfo(infoPtr, nColumn)->fmt & LVCFMT_IMAGE)))
7182 max_cx += infoPtr->iconSize.cx;
7183 max_cx += TRAILING_LABEL_PADDING;
7186 /* autosize based on listview items width */
7187 if(cx == LVSCW_AUTOSIZE)
7189 else if(cx == LVSCW_AUTOSIZE_USEHEADER)
7191 /* if iCol is the last column make it fill the remainder of the controls width */
7192 if(nColumn == DPA_GetPtrCount(infoPtr->hdpaColumns) - 1)
7197 LISTVIEW_GetOrigin(infoPtr, &Origin);
7198 LISTVIEW_GetHeaderRect(infoPtr, nColumn, &rcHeader);
7200 cx = infoPtr->rcList.right - Origin.x - rcHeader.left;
7204 /* Despite what the MS docs say, if this is not the last
7205 column, then MS resizes the column to the width of the
7206 largest text string in the column, including headers
7207 and items. This is different from LVSCW_AUTOSIZE in that
7208 LVSCW_AUTOSIZE ignores the header string length. */
7211 /* retrieve header text */
7212 hdi.mask = HDI_TEXT;
7213 hdi.cchTextMax = DISP_TEXT_SIZE;
7214 hdi.pszText = szDispText;
7215 if (Header_GetItemW(infoPtr->hwndHeader, nColumn, &hdi))
7217 HDC hdc = GetDC(infoPtr->hwndSelf);
7218 HFONT old_font = SelectObject(hdc, (HFONT)SendMessageW(infoPtr->hwndHeader, WM_GETFONT, 0, 0));
7221 if (GetTextExtentPoint32W(hdc, hdi.pszText, lstrlenW(hdi.pszText), &size))
7222 cx = size.cx + TRAILING_HEADER_PADDING;
7223 /* FIXME: Take into account the header image, if one is present */
7224 SelectObject(hdc, old_font);
7225 ReleaseDC(infoPtr->hwndSelf, hdc);
7227 cx = max (cx, max_cx);
7231 if (cx < 0) return FALSE;
7233 /* call header to update the column change */
7234 hdi.mask = HDI_WIDTH;
7236 TRACE("hdi.cxy=%d\n", hdi.cxy);
7237 return Header_SetItemW(infoPtr->hwndHeader, nColumn, &hdi);
7241 * Creates the checkbox imagelist. Helper for LISTVIEW_SetExtendedListViewStyle
7244 static HIMAGELIST LISTVIEW_CreateCheckBoxIL(const LISTVIEW_INFO *infoPtr)
7247 HBITMAP hbm_im, hbm_mask, hbm_orig;
7249 HBRUSH hbr_white = GetStockObject(WHITE_BRUSH);
7250 HBRUSH hbr_black = GetStockObject(BLACK_BRUSH);
7253 himl = ImageList_Create(GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON),
7254 ILC_COLOR | ILC_MASK, 2, 2);
7255 hdc_wnd = GetDC(infoPtr->hwndSelf);
7256 hdc = CreateCompatibleDC(hdc_wnd);
7257 hbm_im = CreateCompatibleBitmap(hdc_wnd, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON));
7258 hbm_mask = CreateBitmap(GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), 1, 1, NULL);
7259 ReleaseDC(infoPtr->hwndSelf, hdc_wnd);
7261 rc.left = rc.top = 0;
7262 rc.right = GetSystemMetrics(SM_CXSMICON);
7263 rc.bottom = GetSystemMetrics(SM_CYSMICON);
7265 hbm_orig = SelectObject(hdc, hbm_mask);
7266 FillRect(hdc, &rc, hbr_white);
7267 InflateRect(&rc, -2, -2);
7268 FillRect(hdc, &rc, hbr_black);
7270 SelectObject(hdc, hbm_im);
7271 DrawFrameControl(hdc, &rc, DFC_BUTTON, DFCS_BUTTONCHECK | DFCS_MONO);
7272 SelectObject(hdc, hbm_orig);
7273 ImageList_Add(himl, hbm_im, hbm_mask);
7275 SelectObject(hdc, hbm_im);
7276 DrawFrameControl(hdc, &rc, DFC_BUTTON, DFCS_BUTTONCHECK | DFCS_MONO | DFCS_CHECKED);
7277 SelectObject(hdc, hbm_orig);
7278 ImageList_Add(himl, hbm_im, hbm_mask);
7280 DeleteObject(hbm_mask);
7281 DeleteObject(hbm_im);
7289 * Sets the extended listview style.
7292 * [I] infoPtr : valid pointer to the listview structure
7294 * [I] dwStyle : style
7297 * SUCCESS : previous style
7300 static DWORD LISTVIEW_SetExtendedListViewStyle(LISTVIEW_INFO *infoPtr, DWORD dwMask, DWORD dwExStyle)
7302 DWORD dwOldExStyle = infoPtr->dwLvExStyle;
7306 infoPtr->dwLvExStyle = (dwOldExStyle & ~dwMask) | (dwExStyle & dwMask);
7308 infoPtr->dwLvExStyle = dwExStyle;
7310 if((infoPtr->dwLvExStyle ^ dwOldExStyle) & LVS_EX_CHECKBOXES)
7312 HIMAGELIST himl = 0;
7313 if(infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES)
7316 item.mask = LVIF_STATE;
7317 item.stateMask = LVIS_STATEIMAGEMASK;
7318 item.state = INDEXTOSTATEIMAGEMASK(1);
7319 LISTVIEW_SetItemState(infoPtr, -1, &item);
7321 himl = LISTVIEW_CreateCheckBoxIL(infoPtr);
7323 LISTVIEW_SetImageList(infoPtr, LVSIL_STATE, himl);
7326 if((infoPtr->dwLvExStyle ^ dwOldExStyle) & LVS_EX_HEADERDRAGDROP)
7330 /* if not already created */
7331 LISTVIEW_CreateHeader(infoPtr);
7333 dwStyle = GetWindowLongW(infoPtr->hwndHeader, GWL_STYLE);
7334 if (infoPtr->dwLvExStyle & LVS_EX_HEADERDRAGDROP)
7335 dwStyle |= HDS_DRAGDROP;
7337 dwStyle &= ~HDS_DRAGDROP;
7338 SetWindowLongW(infoPtr->hwndHeader, GWL_STYLE, dwStyle);
7341 /* GRIDLINES adds decoration at top so changes sizes */
7342 if((infoPtr->dwLvExStyle ^ dwOldExStyle) & LVS_EX_GRIDLINES)
7344 LISTVIEW_UpdateSize(infoPtr);
7348 LISTVIEW_InvalidateList(infoPtr);
7349 return dwOldExStyle;
7354 * Sets the new hot cursor used during hot tracking and hover selection.
7357 * [I] infoPtr : valid pointer to the listview structure
7358 * [I] hCursor : the new hot cursor handle
7361 * Returns the previous hot cursor
7363 static HCURSOR LISTVIEW_SetHotCursor(LISTVIEW_INFO *infoPtr, HCURSOR hCursor)
7365 HCURSOR oldCursor = infoPtr->hHotCursor;
7367 infoPtr->hHotCursor = hCursor;
7375 * Sets the hot item index.
7378 * [I] infoPtr : valid pointer to the listview structure
7379 * [I] iIndex : index
7382 * SUCCESS : previous hot item index
7383 * FAILURE : -1 (no hot item)
7385 static INT LISTVIEW_SetHotItem(LISTVIEW_INFO *infoPtr, INT iIndex)
7387 INT iOldIndex = infoPtr->nHotItem;
7389 infoPtr->nHotItem = iIndex;
7397 * Sets the amount of time the cursor must hover over an item before it is selected.
7400 * [I] infoPtr : valid pointer to the listview structure
7401 * [I] dwHoverTime : hover time, if -1 the hover time is set to the default
7404 * Returns the previous hover time
7406 static DWORD LISTVIEW_SetHoverTime(LISTVIEW_INFO *infoPtr, DWORD dwHoverTime)
7408 DWORD oldHoverTime = infoPtr->dwHoverTime;
7410 infoPtr->dwHoverTime = dwHoverTime;
7412 return oldHoverTime;
7417 * Sets spacing for icons of LVS_ICON style.
7420 * [I] infoPtr : valid pointer to the listview structure
7421 * [I] cx : horizontal spacing (-1 = system spacing, 0 = autosize)
7422 * [I] cy : vertical spacing (-1 = system spacing, 0 = autosize)
7425 * MAKELONG(oldcx, oldcy)
7427 static DWORD LISTVIEW_SetIconSpacing(LISTVIEW_INFO *infoPtr, INT cx, INT cy)
7429 DWORD oldspacing = MAKELONG(infoPtr->iconSpacing.cx, infoPtr->iconSpacing.cy);
7430 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
7432 TRACE("requested=(%d,%d)\n", cx, cy);
7434 /* this is supported only for LVS_ICON style */
7435 if (uView != LVS_ICON) return oldspacing;
7437 /* set to defaults, if instructed to */
7438 if (cx == -1) cx = GetSystemMetrics(SM_CXICONSPACING);
7439 if (cy == -1) cy = GetSystemMetrics(SM_CYICONSPACING);
7441 /* if 0 then compute width
7442 * FIXME: Should scan each item and determine max width of
7443 * icon or label, then make that the width */
7445 cx = infoPtr->iconSpacing.cx;
7447 /* if 0 then compute height */
7449 cy = infoPtr->iconSize.cy + 2 * infoPtr->ntmHeight +
7450 ICON_BOTTOM_PADDING + ICON_TOP_PADDING + LABEL_VERT_PADDING;
7453 infoPtr->iconSpacing.cx = cx;
7454 infoPtr->iconSpacing.cy = cy;
7456 TRACE("old=(%d,%d), new=(%d,%d), iconSize=(%d,%d), ntmH=%d\n",
7457 LOWORD(oldspacing), HIWORD(oldspacing), cx, cy,
7458 infoPtr->iconSize.cx, infoPtr->iconSize.cy,
7459 infoPtr->ntmHeight);
7461 /* these depend on the iconSpacing */
7462 LISTVIEW_UpdateItemSize(infoPtr);
7467 static inline void set_icon_size(SIZE *size, HIMAGELIST himl, BOOL small)
7471 if (himl && ImageList_GetIconSize(himl, &cx, &cy))
7478 size->cx = GetSystemMetrics(small ? SM_CXSMICON : SM_CXICON);
7479 size->cy = GetSystemMetrics(small ? SM_CYSMICON : SM_CYICON);
7488 * [I] infoPtr : valid pointer to the listview structure
7489 * [I] nType : image list type
7490 * [I] himl : image list handle
7493 * SUCCESS : old image list
7496 static HIMAGELIST LISTVIEW_SetImageList(LISTVIEW_INFO *infoPtr, INT nType, HIMAGELIST himl)
7498 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
7499 INT oldHeight = infoPtr->nItemHeight;
7500 HIMAGELIST himlOld = 0;
7502 TRACE("(nType=%d, himl=%p\n", nType, himl);
7507 himlOld = infoPtr->himlNormal;
7508 infoPtr->himlNormal = himl;
7509 if (uView == LVS_ICON) set_icon_size(&infoPtr->iconSize, himl, FALSE);
7510 LISTVIEW_SetIconSpacing(infoPtr, 0, 0);
7514 himlOld = infoPtr->himlSmall;
7515 infoPtr->himlSmall = himl;
7516 if (uView != LVS_ICON) set_icon_size(&infoPtr->iconSize, himl, TRUE);
7520 himlOld = infoPtr->himlState;
7521 infoPtr->himlState = himl;
7522 set_icon_size(&infoPtr->iconStateSize, himl, TRUE);
7523 ImageList_SetBkColor(infoPtr->himlState, CLR_NONE);
7527 ERR("Unknown icon type=%d\n", nType);
7531 infoPtr->nItemHeight = LISTVIEW_CalculateItemHeight(infoPtr);
7532 if (infoPtr->nItemHeight != oldHeight)
7533 LISTVIEW_UpdateScroll(infoPtr);
7540 * Preallocates memory (does *not* set the actual count of items !)
7543 * [I] infoPtr : valid pointer to the listview structure
7544 * [I] nItems : item count (projected number of items to allocate)
7545 * [I] dwFlags : update flags
7551 static BOOL LISTVIEW_SetItemCount(LISTVIEW_INFO *infoPtr, INT nItems, DWORD dwFlags)
7553 TRACE("(nItems=%d, dwFlags=%x)\n", nItems, dwFlags);
7555 if (infoPtr->dwStyle & LVS_OWNERDATA)
7557 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
7558 INT nOldCount = infoPtr->nItemCount;
7560 if (nItems < nOldCount)
7562 RANGE range = { nItems, nOldCount };
7563 ranges_del(infoPtr->selectionRanges, range);
7564 if (infoPtr->nFocusedItem >= nItems)
7566 LISTVIEW_SetItemFocus(infoPtr, -1);
7567 SetRectEmpty(&infoPtr->rcFocus);
7571 infoPtr->nItemCount = nItems;
7572 LISTVIEW_UpdateScroll(infoPtr);
7574 /* the flags are valid only in ownerdata report and list modes */
7575 if (uView == LVS_ICON || uView == LVS_SMALLICON) dwFlags = 0;
7577 if (!(dwFlags & LVSICF_NOSCROLL) && infoPtr->nFocusedItem != -1)
7578 LISTVIEW_EnsureVisible(infoPtr, infoPtr->nFocusedItem, FALSE);
7580 if (!(dwFlags & LVSICF_NOINVALIDATEALL))
7581 LISTVIEW_InvalidateList(infoPtr);
7588 LISTVIEW_GetOrigin(infoPtr, &Origin);
7589 nFrom = min(nOldCount, nItems);
7590 nTo = max(nOldCount, nItems);
7592 if (uView == LVS_REPORT)
7595 rcErase.top = nFrom * infoPtr->nItemHeight;
7596 rcErase.right = infoPtr->nItemWidth;
7597 rcErase.bottom = nTo * infoPtr->nItemHeight;
7598 OffsetRect(&rcErase, Origin.x, Origin.y);
7599 if (IntersectRect(&rcErase, &rcErase, &infoPtr->rcList))
7600 LISTVIEW_InvalidateRect(infoPtr, &rcErase);
7604 INT nPerCol = LISTVIEW_GetCountPerColumn(infoPtr);
7606 rcErase.left = (nFrom / nPerCol) * infoPtr->nItemWidth;
7607 rcErase.top = (nFrom % nPerCol) * infoPtr->nItemHeight;
7608 rcErase.right = rcErase.left + infoPtr->nItemWidth;
7609 rcErase.bottom = nPerCol * infoPtr->nItemHeight;
7610 OffsetRect(&rcErase, Origin.x, Origin.y);
7611 if (IntersectRect(&rcErase, &rcErase, &infoPtr->rcList))
7612 LISTVIEW_InvalidateRect(infoPtr, &rcErase);
7614 rcErase.left = (nFrom / nPerCol + 1) * infoPtr->nItemWidth;
7616 rcErase.right = (nTo / nPerCol + 1) * infoPtr->nItemWidth;
7617 rcErase.bottom = nPerCol * infoPtr->nItemHeight;
7618 OffsetRect(&rcErase, Origin.x, Origin.y);
7619 if (IntersectRect(&rcErase, &rcErase, &infoPtr->rcList))
7620 LISTVIEW_InvalidateRect(infoPtr, &rcErase);
7626 /* According to MSDN for non-LVS_OWNERDATA this is just
7627 * a performance issue. The control allocates its internal
7628 * data structures for the number of items specified. It
7629 * cuts down on the number of memory allocations. Therefore
7630 * we will just issue a WARN here
7632 WARN("for non-ownerdata performance option not implemented.\n");
7640 * Sets the position of an item.
7643 * [I] infoPtr : valid pointer to the listview structure
7644 * [I] nItem : item index
7645 * [I] pt : coordinate
7651 static BOOL LISTVIEW_SetItemPosition(LISTVIEW_INFO *infoPtr, INT nItem, POINT pt)
7653 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
7656 TRACE("(nItem=%d, &pt=%s\n", nItem, wine_dbgstr_point(&pt));
7658 if (nItem < 0 || nItem >= infoPtr->nItemCount ||
7659 !(uView == LVS_ICON || uView == LVS_SMALLICON)) return FALSE;
7661 LISTVIEW_GetOrigin(infoPtr, &Origin);
7663 /* This point value seems to be an undocumented feature.
7664 * The best guess is that it means either at the origin,
7665 * or at true beginning of the list. I will assume the origin. */
7666 if ((pt.x == -1) && (pt.y == -1))
7669 if (uView == LVS_ICON)
7671 pt.x -= (infoPtr->nItemWidth - infoPtr->iconSize.cx) / 2;
7672 pt.y -= ICON_TOP_PADDING;
7677 infoPtr->bAutoarrange = FALSE;
7679 return LISTVIEW_MoveIconTo(infoPtr, nItem, &pt, FALSE);
7684 * Sets the state of one or many items.
7687 * [I] infoPtr : valid pointer to the listview structure
7688 * [I] nItem : item index
7689 * [I] lpLVItem : item or subitem info
7695 static BOOL LISTVIEW_SetItemState(LISTVIEW_INFO *infoPtr, INT nItem, const LVITEMW *lpLVItem)
7697 BOOL bResult = TRUE;
7700 lvItem.iItem = nItem;
7701 lvItem.iSubItem = 0;
7702 lvItem.mask = LVIF_STATE;
7703 lvItem.state = lpLVItem->state;
7704 lvItem.stateMask = lpLVItem->stateMask;
7705 TRACE("lvItem=%s\n", debuglvitem_t(&lvItem, TRUE));
7709 /* select all isn't allowed in LVS_SINGLESEL */
7710 if ((lvItem.state & lvItem.stateMask & LVIS_SELECTED) && (infoPtr->dwStyle & LVS_SINGLESEL))
7713 /* apply to all items */
7714 for (lvItem.iItem = 0; lvItem.iItem < infoPtr->nItemCount; lvItem.iItem++)
7715 if (!LISTVIEW_SetItemT(infoPtr, &lvItem, TRUE)) bResult = FALSE;
7718 bResult = LISTVIEW_SetItemT(infoPtr, &lvItem, TRUE);
7721 * Update selection mark
7723 * Investigation on windows 2k showed that selection mark was updated
7724 * whenever a new selection was made, but if the selected item was
7725 * unselected it was not updated.
7727 * we are probably still not 100% accurate, but this at least sets the
7728 * proper selection mark when it is needed
7731 if (bResult && (lvItem.state & lvItem.stateMask & LVIS_SELECTED) &&
7732 (infoPtr->nSelectionMark == -1))
7735 for (i = 0; i < infoPtr->nItemCount; i++)
7737 if (infoPtr->uCallbackMask & LVIS_SELECTED)
7739 if (LISTVIEW_GetItemState(infoPtr, i, LVIS_SELECTED))
7741 infoPtr->nSelectionMark = i;
7745 else if (ranges_contain(infoPtr->selectionRanges, i))
7747 infoPtr->nSelectionMark = i;
7758 * Sets the text of an item or subitem.
7761 * [I] hwnd : window handle
7762 * [I] nItem : item index
7763 * [I] lpLVItem : item or subitem info
7764 * [I] isW : TRUE if input is Unicode
7770 static BOOL LISTVIEW_SetItemTextT(LISTVIEW_INFO *infoPtr, INT nItem, const LVITEMW *lpLVItem, BOOL isW)
7774 if (nItem < 0 && nItem >= infoPtr->nItemCount) return FALSE;
7776 lvItem.iItem = nItem;
7777 lvItem.iSubItem = lpLVItem->iSubItem;
7778 lvItem.mask = LVIF_TEXT;
7779 lvItem.pszText = lpLVItem->pszText;
7780 lvItem.cchTextMax = lpLVItem->cchTextMax;
7782 TRACE("(nItem=%d, lpLVItem=%s, isW=%d)\n", nItem, debuglvitem_t(&lvItem, isW), isW);
7784 return LISTVIEW_SetItemT(infoPtr, &lvItem, isW);
7789 * Set item index that marks the start of a multiple selection.
7792 * [I] infoPtr : valid pointer to the listview structure
7793 * [I] nIndex : index
7796 * Index number or -1 if there is no selection mark.
7798 static INT LISTVIEW_SetSelectionMark(LISTVIEW_INFO *infoPtr, INT nIndex)
7800 INT nOldIndex = infoPtr->nSelectionMark;
7802 TRACE("(nIndex=%d)\n", nIndex);
7804 infoPtr->nSelectionMark = nIndex;
7811 * Sets the text background color.
7814 * [I] infoPtr : valid pointer to the listview structure
7815 * [I] clrTextBk : text background color
7821 static BOOL LISTVIEW_SetTextBkColor(LISTVIEW_INFO *infoPtr, COLORREF clrTextBk)
7823 TRACE("(clrTextBk=%x)\n", clrTextBk);
7825 if (infoPtr->clrTextBk != clrTextBk)
7827 infoPtr->clrTextBk = clrTextBk;
7828 LISTVIEW_InvalidateList(infoPtr);
7836 * Sets the text foreground color.
7839 * [I] infoPtr : valid pointer to the listview structure
7840 * [I] clrText : text color
7846 static BOOL LISTVIEW_SetTextColor (LISTVIEW_INFO *infoPtr, COLORREF clrText)
7848 TRACE("(clrText=%x)\n", clrText);
7850 if (infoPtr->clrText != clrText)
7852 infoPtr->clrText = clrText;
7853 LISTVIEW_InvalidateList(infoPtr);
7861 * Sets new ToolTip window to ListView control.
7864 * [I] infoPtr : valid pointer to the listview structure
7865 * [I] hwndNewToolTip : handle to new ToolTip
7870 static HWND LISTVIEW_SetToolTips( LISTVIEW_INFO *infoPtr, HWND hwndNewToolTip)
7872 HWND hwndOldToolTip = infoPtr->hwndToolTip;
7873 infoPtr->hwndToolTip = hwndNewToolTip;
7874 return hwndOldToolTip;
7879 * sets the Unicode character format flag for the control
7881 * [I] infoPtr :valid pointer to the listview structure
7882 * [I] fUnicode :true to switch to UNICODE false to switch to ANSI
7885 * Old Unicode Format
7887 static BOOL LISTVIEW_SetUnicodeFormat( LISTVIEW_INFO *infoPtr, BOOL fUnicode)
7889 BOOL rc = infoPtr->notifyFormat;
7890 infoPtr->notifyFormat = (fUnicode)?NFR_UNICODE:NFR_ANSI;
7894 /* LISTVIEW_SetWorkAreas */
7898 * Callback internally used by LISTVIEW_SortItems() in response of LVM_SORTITEMS
7901 * [I] first : pointer to first ITEM_INFO to compare
7902 * [I] second : pointer to second ITEM_INFO to compare
7903 * [I] lParam : HWND of control
7906 * if first comes before second : negative
7907 * if first comes after second : positive
7908 * if first and second are equivalent : zero
7910 static INT WINAPI LISTVIEW_CallBackCompare(LPVOID first, LPVOID second, LPARAM lParam)
7912 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)lParam;
7913 ITEM_INFO* lv_first = DPA_GetPtr( first, 0 );
7914 ITEM_INFO* lv_second = DPA_GetPtr( second, 0 );
7916 /* Forward the call to the client defined callback */
7917 return (infoPtr->pfnCompare)( lv_first->lParam , lv_second->lParam, infoPtr->lParamSort );
7922 * Callback internally used by LISTVIEW_SortItems() in response of LVM_SORTITEMSEX
7925 * [I] first : pointer to first ITEM_INFO to compare
7926 * [I] second : pointer to second ITEM_INFO to compare
7927 * [I] lParam : HWND of control
7930 * if first comes before second : negative
7931 * if first comes after second : positive
7932 * if first and second are equivalent : zero
7934 static INT WINAPI LISTVIEW_CallBackCompareEx(LPVOID first, LPVOID second, LPARAM lParam)
7936 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)lParam;
7937 INT first_idx = DPA_GetPtrIndex( infoPtr->hdpaItems, first );
7938 INT second_idx = DPA_GetPtrIndex( infoPtr->hdpaItems, second );
7940 /* Forward the call to the client defined callback */
7941 return (infoPtr->pfnCompare)( first_idx, second_idx, infoPtr->lParamSort );
7946 * Sorts the listview items.
7949 * [I] infoPtr : valid pointer to the listview structure
7950 * [I] pfnCompare : application-defined value
7951 * [I] lParamSort : pointer to comparison callback
7952 * [I] IsEx : TRUE when LVM_SORTITEMSEX used
7958 static BOOL LISTVIEW_SortItems(LISTVIEW_INFO *infoPtr, PFNLVCOMPARE pfnCompare,
7959 LPARAM lParamSort, BOOL IsEx)
7961 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
7964 LPVOID selectionMarkItem = NULL;
7965 LPVOID focusedItem = NULL;
7968 TRACE("(pfnCompare=%p, lParamSort=%lx)\n", pfnCompare, lParamSort);
7970 if (infoPtr->dwStyle & LVS_OWNERDATA) return FALSE;
7972 if (!pfnCompare) return FALSE;
7973 if (!infoPtr->hdpaItems) return FALSE;
7975 /* if there are 0 or 1 items, there is no need to sort */
7976 if (infoPtr->nItemCount < 2) return TRUE;
7978 /* clear selection */
7979 ranges_clear(infoPtr->selectionRanges);
7981 /* save selection mark and focused item */
7982 if (infoPtr->nSelectionMark >= 0)
7983 selectionMarkItem = DPA_GetPtr(infoPtr->hdpaItems, infoPtr->nSelectionMark);
7984 if (infoPtr->nFocusedItem >= 0)
7985 focusedItem = DPA_GetPtr(infoPtr->hdpaItems, infoPtr->nFocusedItem);
7987 infoPtr->pfnCompare = pfnCompare;
7988 infoPtr->lParamSort = lParamSort;
7990 DPA_Sort(infoPtr->hdpaItems, LISTVIEW_CallBackCompareEx, (LPARAM)infoPtr);
7992 DPA_Sort(infoPtr->hdpaItems, LISTVIEW_CallBackCompare, (LPARAM)infoPtr);
7994 /* restore selection ranges */
7995 for (i=0; i < infoPtr->nItemCount; i++)
7997 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, i);
7998 lpItem = DPA_GetPtr(hdpaSubItems, 0);
8000 if (lpItem->state & LVIS_SELECTED)
8001 ranges_additem(infoPtr->selectionRanges, i);
8003 /* restore selection mark and focused item */
8004 infoPtr->nSelectionMark = DPA_GetPtrIndex(infoPtr->hdpaItems, selectionMarkItem);
8005 infoPtr->nFocusedItem = DPA_GetPtrIndex(infoPtr->hdpaItems, focusedItem);
8007 /* I believe nHotItem should be left alone, see LISTVIEW_ShiftIndices */
8009 /* refresh the display */
8010 if (uView != LVS_ICON && uView != LVS_SMALLICON)
8011 LISTVIEW_InvalidateList(infoPtr);
8018 * Update theme handle after a theme change.
8021 * [I] infoPtr : valid pointer to the listview structure
8025 * FAILURE : something else
8027 static LRESULT LISTVIEW_ThemeChanged(const LISTVIEW_INFO *infoPtr)
8029 HTHEME theme = GetWindowTheme(infoPtr->hwndSelf);
8030 CloseThemeData(theme);
8031 OpenThemeData(infoPtr->hwndSelf, themeClass);
8037 * Updates an items or rearranges the listview control.
8040 * [I] infoPtr : valid pointer to the listview structure
8041 * [I] nItem : item index
8047 static BOOL LISTVIEW_Update(LISTVIEW_INFO *infoPtr, INT nItem)
8049 TRACE("(nItem=%d)\n", nItem);
8051 if (nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
8053 /* rearrange with default alignment style */
8054 if (is_autoarrange(infoPtr))
8055 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
8057 LISTVIEW_InvalidateItem(infoPtr, nItem);
8064 * Draw the track line at the place defined in the infoPtr structure.
8065 * The line is drawn with a XOR pen so drawing the line for the second time
8066 * in the same place erases the line.
8069 * [I] infoPtr : valid pointer to the listview structure
8075 static BOOL LISTVIEW_DrawTrackLine(const LISTVIEW_INFO *infoPtr)
8081 if (infoPtr->xTrackLine == -1)
8084 if (!(hdc = GetDC(infoPtr->hwndSelf)))
8086 hOldPen = SelectObject(hdc, GetStockObject(BLACK_PEN));
8087 oldROP = SetROP2(hdc, R2_XORPEN);
8088 MoveToEx(hdc, infoPtr->xTrackLine, infoPtr->rcList.top, NULL);
8089 LineTo(hdc, infoPtr->xTrackLine, infoPtr->rcList.bottom);
8090 SetROP2(hdc, oldROP);
8091 SelectObject(hdc, hOldPen);
8092 ReleaseDC(infoPtr->hwndSelf, hdc);
8098 * Called when an edit control should be displayed. This function is called after
8099 * we are sure that there was a single click - not a double click (this is a TIMERPROC).
8102 * [I] hwnd : Handle to the listview
8103 * [I] uMsg : WM_TIMER (ignored)
8104 * [I] idEvent : The timer ID interpreted as a pointer to a DELAYED_EDIT_ITEM struct
8105 * [I] dwTimer : The elapsed time (ignored)
8110 static VOID CALLBACK LISTVIEW_DelayedEditItem(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
8112 DELAYED_ITEM_EDIT *editItem = (DELAYED_ITEM_EDIT *)idEvent;
8113 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongPtrW(hwnd, 0);
8115 KillTimer(hwnd, idEvent);
8116 editItem->fEnabled = FALSE;
8117 /* check if the item is still selected */
8118 if (infoPtr->bFocus && LISTVIEW_GetItemState(infoPtr, editItem->iItem, LVIS_SELECTED))
8119 LISTVIEW_EditLabelT(infoPtr, editItem->iItem, TRUE);
8124 * Creates the listview control - the WM_NCCREATE phase.
8127 * [I] hwnd : window handle
8128 * [I] lpcs : the create parameters
8134 static LRESULT LISTVIEW_NCCreate(HWND hwnd, const CREATESTRUCTW *lpcs)
8136 LISTVIEW_INFO *infoPtr;
8139 TRACE("(lpcs=%p)\n", lpcs);
8141 /* initialize info pointer */
8142 infoPtr = Alloc(sizeof(LISTVIEW_INFO));
8143 if (!infoPtr) return FALSE;
8145 SetWindowLongPtrW(hwnd, 0, (DWORD_PTR)infoPtr);
8147 infoPtr->hwndSelf = hwnd;
8148 infoPtr->dwStyle = lpcs->style; /* Note: may be changed in WM_CREATE */
8149 /* determine the type of structures to use */
8150 infoPtr->hwndNotify = lpcs->hwndParent;
8151 /* infoPtr->notifyFormat will be filled in WM_CREATE */
8153 /* initialize color information */
8154 infoPtr->clrBk = CLR_NONE;
8155 infoPtr->clrText = CLR_DEFAULT;
8156 infoPtr->clrTextBk = CLR_DEFAULT;
8157 LISTVIEW_SetBkColor(infoPtr, comctl32_color.clrWindow);
8159 /* set default values */
8160 infoPtr->nFocusedItem = -1;
8161 infoPtr->nSelectionMark = -1;
8162 infoPtr->nHotItem = -1;
8163 infoPtr->bRedraw = TRUE;
8164 infoPtr->bNoItemMetrics = TRUE;
8165 infoPtr->bDoChangeNotify = TRUE;
8166 infoPtr->iconSpacing.cx = GetSystemMetrics(SM_CXICONSPACING);
8167 infoPtr->iconSpacing.cy = GetSystemMetrics(SM_CYICONSPACING);
8168 infoPtr->nEditLabelItem = -1;
8169 infoPtr->nLButtonDownItem = -1;
8170 infoPtr->dwHoverTime = -1; /* default system hover time */
8171 infoPtr->nMeasureItemHeight = 0;
8172 infoPtr->xTrackLine = -1; /* no track line */
8173 infoPtr->itemEdit.fEnabled = FALSE;
8174 infoPtr->iVersion = COMCTL32_VERSION;
8176 /* get default font (icon title) */
8177 SystemParametersInfoW(SPI_GETICONTITLELOGFONT, 0, &logFont, 0);
8178 infoPtr->hDefaultFont = CreateFontIndirectW(&logFont);
8179 infoPtr->hFont = infoPtr->hDefaultFont;
8180 LISTVIEW_SaveTextMetrics(infoPtr);
8182 /* allocate memory for the data structure */
8183 if (!(infoPtr->selectionRanges = ranges_create(10))) goto fail;
8184 if (!(infoPtr->hdpaItems = DPA_Create(10))) goto fail;
8185 if (!(infoPtr->hdpaPosX = DPA_Create(10))) goto fail;
8186 if (!(infoPtr->hdpaPosY = DPA_Create(10))) goto fail;
8187 if (!(infoPtr->hdpaColumns = DPA_Create(10))) goto fail;
8191 DestroyWindow(infoPtr->hwndHeader);
8192 ranges_destroy(infoPtr->selectionRanges);
8193 DPA_Destroy(infoPtr->hdpaItems);
8194 DPA_Destroy(infoPtr->hdpaPosX);
8195 DPA_Destroy(infoPtr->hdpaPosY);
8196 DPA_Destroy(infoPtr->hdpaColumns);
8203 * Creates the listview control - the WM_CREATE phase. Most of the data is
8204 * already set up in LISTVIEW_NCCreate
8207 * [I] hwnd : window handle
8208 * [I] lpcs : the create parameters
8214 static LRESULT LISTVIEW_Create(HWND hwnd, const CREATESTRUCTW *lpcs)
8216 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongPtrW(hwnd, 0);
8217 UINT uView = lpcs->style & LVS_TYPEMASK;
8219 TRACE("(lpcs=%p)\n", lpcs);
8221 infoPtr->dwStyle = lpcs->style;
8222 infoPtr->notifyFormat = SendMessageW(infoPtr->hwndNotify, WM_NOTIFYFORMAT,
8223 (WPARAM)infoPtr->hwndSelf, (LPARAM)NF_QUERY);
8225 if ((uView == LVS_REPORT) && (lpcs->style & WS_VISIBLE))
8227 if (LISTVIEW_CreateHeader(infoPtr) < 0) return -1;
8230 infoPtr->hwndHeader = 0;
8232 /* init item size to avoid division by 0 */
8233 LISTVIEW_UpdateItemSize (infoPtr);
8235 if (uView == LVS_REPORT)
8237 if (!(LVS_NOCOLUMNHEADER & lpcs->style) && (WS_VISIBLE & lpcs->style))
8239 ShowWindow(infoPtr->hwndHeader, SW_SHOWNORMAL);
8241 LISTVIEW_UpdateScroll(infoPtr);
8244 OpenThemeData(hwnd, themeClass);
8246 /* initialize the icon sizes */
8247 set_icon_size(&infoPtr->iconSize, infoPtr->himlNormal, uView != LVS_ICON);
8248 set_icon_size(&infoPtr->iconStateSize, infoPtr->himlState, TRUE);
8254 * Destroys the listview control.
8257 * [I] infoPtr : valid pointer to the listview structure
8263 static LRESULT LISTVIEW_Destroy(const LISTVIEW_INFO *infoPtr)
8265 HTHEME theme = GetWindowTheme(infoPtr->hwndSelf);
8266 CloseThemeData(theme);
8272 * Enables the listview control.
8275 * [I] infoPtr : valid pointer to the listview structure
8276 * [I] bEnable : specifies whether to enable or disable the window
8282 static BOOL LISTVIEW_Enable(const LISTVIEW_INFO *infoPtr, BOOL bEnable)
8284 if (infoPtr->dwStyle & LVS_OWNERDRAWFIXED)
8285 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
8291 * Erases the background of the listview control.
8294 * [I] infoPtr : valid pointer to the listview structure
8295 * [I] hdc : device context handle
8301 static inline BOOL LISTVIEW_EraseBkgnd(const LISTVIEW_INFO *infoPtr, HDC hdc)
8305 TRACE("(hdc=%p)\n", hdc);
8307 if (!GetClipBox(hdc, &rc)) return FALSE;
8309 if (infoPtr->clrBk == CLR_NONE)
8310 return SendMessageW(infoPtr->hwndNotify, WM_ERASEBKGND, (WPARAM)hdc, 0);
8312 /* for double buffered controls we need to do this during refresh */
8313 if (infoPtr->dwLvExStyle & LVS_EX_DOUBLEBUFFER) return FALSE;
8315 return LISTVIEW_FillBkgnd(infoPtr, hdc, &rc);
8321 * Helper function for LISTVIEW_[HV]Scroll *only*.
8322 * Performs vertical/horizontal scrolling by a give amount.
8325 * [I] infoPtr : valid pointer to the listview structure
8326 * [I] dx : amount of horizontal scroll
8327 * [I] dy : amount of vertical scroll
8329 static void scroll_list(LISTVIEW_INFO *infoPtr, INT dx, INT dy)
8331 /* now we can scroll the list */
8332 ScrollWindowEx(infoPtr->hwndSelf, dx, dy, &infoPtr->rcList,
8333 &infoPtr->rcList, 0, 0, SW_ERASE | SW_INVALIDATE);
8334 /* if we have focus, adjust rect */
8335 OffsetRect(&infoPtr->rcFocus, dx, dy);
8336 UpdateWindow(infoPtr->hwndSelf);
8341 * Performs vertical scrolling.
8344 * [I] infoPtr : valid pointer to the listview structure
8345 * [I] nScrollCode : scroll code
8346 * [I] nScrollDiff : units to scroll in SB_INTERNAL mode, 0 otherwise
8347 * [I] hScrollWnd : scrollbar control window handle
8353 * SB_LINEUP/SB_LINEDOWN:
8354 * for LVS_ICON, LVS_SMALLICON is 37 by experiment
8355 * for LVS_REPORT is 1 line
8356 * for LVS_LIST cannot occur
8359 static LRESULT LISTVIEW_VScroll(LISTVIEW_INFO *infoPtr, INT nScrollCode,
8360 INT nScrollDiff, HWND hScrollWnd)
8362 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
8363 INT nOldScrollPos, nNewScrollPos;
8364 SCROLLINFO scrollInfo;
8367 TRACE("(nScrollCode=%d(%s), nScrollDiff=%d)\n", nScrollCode,
8368 debugscrollcode(nScrollCode), nScrollDiff);
8370 if (infoPtr->hwndEdit) SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
8372 scrollInfo.cbSize = sizeof(SCROLLINFO);
8373 scrollInfo.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_TRACKPOS;
8375 is_an_icon = ((uView == LVS_ICON) || (uView == LVS_SMALLICON));
8377 if (!GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo)) return 1;
8379 nOldScrollPos = scrollInfo.nPos;
8380 switch (nScrollCode)
8386 nScrollDiff = (is_an_icon) ? -LISTVIEW_SCROLL_ICON_LINE_SIZE : -1;
8390 nScrollDiff = (is_an_icon) ? LISTVIEW_SCROLL_ICON_LINE_SIZE : 1;
8394 nScrollDiff = -scrollInfo.nPage;
8398 nScrollDiff = scrollInfo.nPage;
8401 case SB_THUMBPOSITION:
8403 nScrollDiff = scrollInfo.nTrackPos - scrollInfo.nPos;
8410 /* quit right away if pos isn't changing */
8411 if (nScrollDiff == 0) return 0;
8413 /* calculate new position, and handle overflows */
8414 nNewScrollPos = scrollInfo.nPos + nScrollDiff;
8415 if (nScrollDiff > 0) {
8416 if (nNewScrollPos < nOldScrollPos ||
8417 nNewScrollPos > scrollInfo.nMax)
8418 nNewScrollPos = scrollInfo.nMax;
8420 if (nNewScrollPos > nOldScrollPos ||
8421 nNewScrollPos < scrollInfo.nMin)
8422 nNewScrollPos = scrollInfo.nMin;
8425 /* set the new position, and reread in case it changed */
8426 scrollInfo.fMask = SIF_POS;
8427 scrollInfo.nPos = nNewScrollPos;
8428 nNewScrollPos = SetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo, TRUE);
8430 /* carry on only if it really changed */
8431 if (nNewScrollPos == nOldScrollPos) return 0;
8433 /* now adjust to client coordinates */
8434 nScrollDiff = nOldScrollPos - nNewScrollPos;
8435 if (uView == LVS_REPORT) nScrollDiff *= infoPtr->nItemHeight;
8437 /* and scroll the window */
8438 scroll_list(infoPtr, 0, nScrollDiff);
8445 * Performs horizontal scrolling.
8448 * [I] infoPtr : valid pointer to the listview structure
8449 * [I] nScrollCode : scroll code
8450 * [I] nScrollDiff : units to scroll in SB_INTERNAL mode, 0 otherwise
8451 * [I] hScrollWnd : scrollbar control window handle
8457 * SB_LINELEFT/SB_LINERIGHT:
8458 * for LVS_ICON, LVS_SMALLICON 1 pixel
8459 * for LVS_REPORT is 1 pixel
8460 * for LVS_LIST is 1 column --> which is a 1 because the
8461 * scroll is based on columns not pixels
8464 static LRESULT LISTVIEW_HScroll(LISTVIEW_INFO *infoPtr, INT nScrollCode,
8465 INT nScrollDiff, HWND hScrollWnd)
8467 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
8468 INT nOldScrollPos, nNewScrollPos;
8469 SCROLLINFO scrollInfo;
8471 TRACE("(nScrollCode=%d(%s), nScrollDiff=%d)\n", nScrollCode,
8472 debugscrollcode(nScrollCode), nScrollDiff);
8474 if (infoPtr->hwndEdit) SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
8476 scrollInfo.cbSize = sizeof(SCROLLINFO);
8477 scrollInfo.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_TRACKPOS;
8479 if (!GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo)) return 1;
8481 nOldScrollPos = scrollInfo.nPos;
8483 switch (nScrollCode)
8497 nScrollDiff = -scrollInfo.nPage;
8501 nScrollDiff = scrollInfo.nPage;
8504 case SB_THUMBPOSITION:
8506 nScrollDiff = scrollInfo.nTrackPos - scrollInfo.nPos;
8513 /* quit right away if pos isn't changing */
8514 if (nScrollDiff == 0) return 0;
8516 /* calculate new position, and handle overflows */
8517 nNewScrollPos = scrollInfo.nPos + nScrollDiff;
8518 if (nScrollDiff > 0) {
8519 if (nNewScrollPos < nOldScrollPos ||
8520 nNewScrollPos > scrollInfo.nMax)
8521 nNewScrollPos = scrollInfo.nMax;
8523 if (nNewScrollPos > nOldScrollPos ||
8524 nNewScrollPos < scrollInfo.nMin)
8525 nNewScrollPos = scrollInfo.nMin;
8528 /* set the new position, and reread in case it changed */
8529 scrollInfo.fMask = SIF_POS;
8530 scrollInfo.nPos = nNewScrollPos;
8531 nNewScrollPos = SetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo, TRUE);
8533 /* carry on only if it really changed */
8534 if (nNewScrollPos == nOldScrollPos) return 0;
8536 if(uView == LVS_REPORT)
8537 LISTVIEW_UpdateHeaderSize(infoPtr, nNewScrollPos);
8539 /* now adjust to client coordinates */
8540 nScrollDiff = nOldScrollPos - nNewScrollPos;
8541 if (uView == LVS_LIST) nScrollDiff *= infoPtr->nItemWidth;
8543 /* and scroll the window */
8544 scroll_list(infoPtr, nScrollDiff, 0);
8549 static LRESULT LISTVIEW_MouseWheel(LISTVIEW_INFO *infoPtr, INT wheelDelta)
8551 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
8552 INT gcWheelDelta = 0;
8553 INT pulScrollLines = 3;
8554 SCROLLINFO scrollInfo;
8556 TRACE("(wheelDelta=%d)\n", wheelDelta);
8558 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
8559 gcWheelDelta -= wheelDelta;
8561 scrollInfo.cbSize = sizeof(SCROLLINFO);
8562 scrollInfo.fMask = SIF_POS;
8569 * listview should be scrolled by a multiple of 37 dependently on its dimension or its visible item number
8570 * should be fixed in the future.
8572 LISTVIEW_VScroll(infoPtr, SB_INTERNAL, (gcWheelDelta < 0) ?
8573 -LISTVIEW_SCROLL_ICON_LINE_SIZE : LISTVIEW_SCROLL_ICON_LINE_SIZE, 0);
8577 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
8579 int cLineScroll = min(LISTVIEW_GetCountPerColumn(infoPtr), pulScrollLines);
8580 cLineScroll *= (gcWheelDelta / WHEEL_DELTA);
8581 LISTVIEW_VScroll(infoPtr, SB_INTERNAL, cLineScroll, 0);
8586 LISTVIEW_HScroll(infoPtr, (gcWheelDelta < 0) ? SB_LINELEFT : SB_LINERIGHT, 0, 0);
8597 * [I] infoPtr : valid pointer to the listview structure
8598 * [I] nVirtualKey : virtual key
8599 * [I] lKeyData : key data
8604 static LRESULT LISTVIEW_KeyDown(LISTVIEW_INFO *infoPtr, INT nVirtualKey, LONG lKeyData)
8606 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
8607 HWND hwndSelf = infoPtr->hwndSelf;
8609 NMLVKEYDOWN nmKeyDown;
8611 TRACE("(nVirtualKey=%d, lKeyData=%d)\n", nVirtualKey, lKeyData);
8613 /* send LVN_KEYDOWN notification */
8614 nmKeyDown.wVKey = nVirtualKey;
8615 nmKeyDown.flags = 0;
8616 notify_hdr(infoPtr, LVN_KEYDOWN, &nmKeyDown.hdr);
8617 if (!IsWindow(hwndSelf))
8620 switch (nVirtualKey)
8623 nItem = infoPtr->nFocusedItem;
8624 if (infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES)
8625 toggle_checkbox_state(infoPtr, infoPtr->nFocusedItem);
8629 if ((infoPtr->nItemCount > 0) && (infoPtr->nFocusedItem != -1))
8631 if (!notify(infoPtr, NM_RETURN)) return 0;
8632 if (!notify(infoPtr, LVN_ITEMACTIVATE)) return 0;
8637 if (infoPtr->nItemCount > 0)
8642 if (infoPtr->nItemCount > 0)
8643 nItem = infoPtr->nItemCount - 1;
8647 nItem = LISTVIEW_GetNextItem(infoPtr, infoPtr->nFocusedItem, LVNI_TOLEFT);
8651 nItem = LISTVIEW_GetNextItem(infoPtr, infoPtr->nFocusedItem, LVNI_ABOVE);
8655 nItem = LISTVIEW_GetNextItem(infoPtr, infoPtr->nFocusedItem, LVNI_TORIGHT);
8659 nItem = LISTVIEW_GetNextItem(infoPtr, infoPtr->nFocusedItem, LVNI_BELOW);
8663 if (uView == LVS_REPORT)
8665 INT topidx = LISTVIEW_GetTopIndex(infoPtr);
8666 if (infoPtr->nFocusedItem == topidx)
8667 nItem = topidx - LISTVIEW_GetCountPerColumn(infoPtr) + 1;
8672 nItem = infoPtr->nFocusedItem - LISTVIEW_GetCountPerColumn(infoPtr)
8673 * LISTVIEW_GetCountPerRow(infoPtr);
8674 if(nItem < 0) nItem = 0;
8678 if (uView == LVS_REPORT)
8680 INT topidx = LISTVIEW_GetTopIndex(infoPtr);
8681 INT cnt = LISTVIEW_GetCountPerColumn(infoPtr);
8682 if (infoPtr->nFocusedItem == topidx + cnt - 1)
8683 nItem = infoPtr->nFocusedItem + cnt - 1;
8685 nItem = topidx + cnt - 1;
8688 nItem = infoPtr->nFocusedItem + LISTVIEW_GetCountPerColumn(infoPtr)
8689 * LISTVIEW_GetCountPerRow(infoPtr);
8690 if(nItem >= infoPtr->nItemCount) nItem = infoPtr->nItemCount - 1;
8694 if ((nItem != -1) && (nItem != infoPtr->nFocusedItem || nVirtualKey == VK_SPACE))
8695 LISTVIEW_KeySelection(infoPtr, nItem, nVirtualKey == VK_SPACE);
8705 * [I] infoPtr : valid pointer to the listview structure
8710 static LRESULT LISTVIEW_KillFocus(LISTVIEW_INFO *infoPtr)
8714 /* if we did not have the focus, there's nothing to do */
8715 if (!infoPtr->bFocus) return 0;
8717 /* send NM_KILLFOCUS notification */
8718 if (!notify(infoPtr, NM_KILLFOCUS)) return 0;
8720 /* if we have a focus rectangle, get rid of it */
8721 LISTVIEW_ShowFocusRect(infoPtr, FALSE);
8723 /* set window focus flag */
8724 infoPtr->bFocus = FALSE;
8726 /* invalidate the selected items before resetting focus flag */
8727 LISTVIEW_InvalidateSelectedItems(infoPtr);
8734 * Processes double click messages (left mouse button).
8737 * [I] infoPtr : valid pointer to the listview structure
8738 * [I] wKey : key flag
8739 * [I] x,y : mouse coordinate
8744 static LRESULT LISTVIEW_LButtonDblClk(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
8746 LVHITTESTINFO htInfo;
8748 TRACE("(key=%hu, X=%hu, Y=%hu)\n", wKey, x, y);
8750 /* Cancel the item edition if any */
8751 if (infoPtr->itemEdit.fEnabled)
8753 KillTimer(infoPtr->hwndSelf, (UINT_PTR)&infoPtr->itemEdit);
8754 infoPtr->itemEdit.fEnabled = FALSE;
8757 /* send NM_RELEASEDCAPTURE notification */
8758 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
8763 /* send NM_DBLCLK notification */
8764 LISTVIEW_HitTest(infoPtr, &htInfo, TRUE, FALSE);
8765 if (!notify_click(infoPtr, NM_DBLCLK, &htInfo)) return 0;
8767 /* To send the LVN_ITEMACTIVATE, it must be on an Item */
8768 if(htInfo.iItem != -1) notify_itemactivate(infoPtr,&htInfo);
8775 * Processes mouse down messages (left mouse button).
8778 * infoPtr [I ] valid pointer to the listview structure
8779 * wKey [I ] key flag
8780 * x,y [I ] mouse coordinate
8785 static LRESULT LISTVIEW_LButtonDown(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
8787 LVHITTESTINFO lvHitTestInfo;
8788 static BOOL bGroupSelect = TRUE;
8789 POINT pt = { x, y };
8792 TRACE("(key=%hu, X=%hu, Y=%hu)\n", wKey, x, y);
8794 /* send NM_RELEASEDCAPTURE notification */
8795 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
8797 /* set left button down flag and record the click position */
8798 infoPtr->bLButtonDown = TRUE;
8799 infoPtr->ptClickPos = pt;
8800 infoPtr->bDragging = FALSE;
8802 lvHitTestInfo.pt.x = x;
8803 lvHitTestInfo.pt.y = y;
8805 nItem = LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, TRUE);
8806 TRACE("at %s, nItem=%d\n", wine_dbgstr_point(&pt), nItem);
8807 if ((nItem >= 0) && (nItem < infoPtr->nItemCount))
8809 if ((infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES) && (lvHitTestInfo.flags & LVHT_ONITEMSTATEICON))
8811 toggle_checkbox_state(infoPtr, nItem);
8815 if (infoPtr->dwStyle & LVS_SINGLESEL)
8817 if (LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED))
8818 infoPtr->nEditLabelItem = nItem;
8820 LISTVIEW_SetSelection(infoPtr, nItem);
8824 if ((wKey & MK_CONTROL) && (wKey & MK_SHIFT))
8828 if (!LISTVIEW_AddGroupSelection(infoPtr, nItem)) return 0;
8829 LISTVIEW_SetItemFocus(infoPtr, nItem);
8830 infoPtr->nSelectionMark = nItem;
8836 item.state = LVIS_SELECTED | LVIS_FOCUSED;
8837 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
8839 LISTVIEW_SetItemState(infoPtr,nItem,&item);
8840 infoPtr->nSelectionMark = nItem;
8843 else if (wKey & MK_CONTROL)
8847 bGroupSelect = (LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED) == 0);
8849 item.state = (bGroupSelect ? LVIS_SELECTED : 0) | LVIS_FOCUSED;
8850 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
8851 LISTVIEW_SetItemState(infoPtr, nItem, &item);
8852 infoPtr->nSelectionMark = nItem;
8854 else if (wKey & MK_SHIFT)
8856 LISTVIEW_SetGroupSelection(infoPtr, nItem);
8860 if (LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED))
8862 infoPtr->nEditLabelItem = nItem;
8863 infoPtr->nLButtonDownItem = nItem;
8865 LISTVIEW_SetItemFocus(infoPtr, nItem);
8868 /* set selection (clears other pre-existing selections) */
8869 LISTVIEW_SetSelection(infoPtr, nItem);
8873 if (infoPtr->dwLvExStyle & LVS_EX_ONECLICKACTIVATE)
8874 if(lvHitTestInfo.iItem != -1) notify_itemactivate(infoPtr,&lvHitTestInfo);
8878 /* remove all selections */
8879 if (!(wKey & MK_CONTROL) && !(wKey & MK_SHIFT))
8880 LISTVIEW_DeselectAll(infoPtr);
8889 * Processes mouse up messages (left mouse button).
8892 * infoPtr [I ] valid pointer to the listview structure
8893 * wKey [I ] key flag
8894 * x,y [I ] mouse coordinate
8899 static LRESULT LISTVIEW_LButtonUp(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
8901 LVHITTESTINFO lvHitTestInfo;
8903 TRACE("(key=%hu, X=%hu, Y=%hu)\n", wKey, x, y);
8905 if (!infoPtr->bLButtonDown) return 0;
8907 lvHitTestInfo.pt.x = x;
8908 lvHitTestInfo.pt.y = y;
8910 /* send NM_CLICK notification */
8911 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
8912 if (!notify_click(infoPtr, NM_CLICK, &lvHitTestInfo)) return 0;
8914 /* set left button flag */
8915 infoPtr->bLButtonDown = FALSE;
8917 /* set a single selection, reset others */
8918 if(lvHitTestInfo.iItem == infoPtr->nLButtonDownItem && lvHitTestInfo.iItem != -1)
8919 LISTVIEW_SetSelection(infoPtr, infoPtr->nLButtonDownItem);
8920 infoPtr->nLButtonDownItem = -1;
8922 if (infoPtr->bDragging)
8924 infoPtr->bDragging = FALSE;
8928 /* if we clicked on a selected item, edit the label */
8929 if(lvHitTestInfo.iItem == infoPtr->nEditLabelItem && (lvHitTestInfo.flags & LVHT_ONITEMLABEL))
8931 /* we want to make sure the user doesn't want to do a double click. So we will
8932 * delay the edit. WM_LBUTTONDBLCLICK will cancel the timer
8934 infoPtr->itemEdit.fEnabled = TRUE;
8935 infoPtr->itemEdit.iItem = lvHitTestInfo.iItem;
8936 SetTimer(infoPtr->hwndSelf,
8937 (UINT_PTR)&infoPtr->itemEdit,
8938 GetDoubleClickTime(),
8939 LISTVIEW_DelayedEditItem);
8942 if (!infoPtr->bFocus)
8943 SetFocus(infoPtr->hwndSelf);
8950 * Destroys the listview control (called after WM_DESTROY).
8953 * [I] infoPtr : valid pointer to the listview structure
8958 static LRESULT LISTVIEW_NCDestroy(LISTVIEW_INFO *infoPtr)
8962 /* delete all items */
8963 LISTVIEW_DeleteAllItems(infoPtr, TRUE);
8965 /* destroy data structure */
8966 DPA_Destroy(infoPtr->hdpaItems);
8967 DPA_Destroy(infoPtr->hdpaPosX);
8968 DPA_Destroy(infoPtr->hdpaPosY);
8969 DPA_Destroy(infoPtr->hdpaColumns);
8970 ranges_destroy(infoPtr->selectionRanges);
8972 /* destroy image lists */
8973 if (!(infoPtr->dwStyle & LVS_SHAREIMAGELISTS))
8975 if (infoPtr->himlNormal)
8976 ImageList_Destroy(infoPtr->himlNormal);
8977 if (infoPtr->himlSmall)
8978 ImageList_Destroy(infoPtr->himlSmall);
8979 if (infoPtr->himlState)
8980 ImageList_Destroy(infoPtr->himlState);
8983 /* destroy font, bkgnd brush */
8985 if (infoPtr->hDefaultFont) DeleteObject(infoPtr->hDefaultFont);
8986 if (infoPtr->clrBk != CLR_NONE) DeleteObject(infoPtr->hBkBrush);
8988 SetWindowLongPtrW(infoPtr->hwndSelf, 0, 0);
8990 /* free listview info pointer*/
8998 * Handles notifications from header.
9001 * [I] infoPtr : valid pointer to the listview structure
9002 * [I] nCtrlId : control identifier
9003 * [I] lpnmh : notification information
9008 static LRESULT LISTVIEW_HeaderNotification(LISTVIEW_INFO *infoPtr, const NMHEADERW *lpnmh)
9010 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
9011 HWND hwndSelf = infoPtr->hwndSelf;
9013 TRACE("(lpnmh=%p)\n", lpnmh);
9015 if (!lpnmh || lpnmh->iItem < 0 || lpnmh->iItem >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return 0;
9017 switch (lpnmh->hdr.code)
9022 COLUMN_INFO *lpColumnInfo;
9026 if (!lpnmh->pitem || !(lpnmh->pitem->mask & HDI_WIDTH))
9029 /* remove the old line (if any) */
9030 LISTVIEW_DrawTrackLine(infoPtr);
9032 /* compute & draw the new line */
9033 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, lpnmh->iItem);
9034 x = lpColumnInfo->rcHeader.left + lpnmh->pitem->cxy;
9035 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
9036 infoPtr->xTrackLine = x + ptOrigin.x;
9037 LISTVIEW_DrawTrackLine(infoPtr);
9043 /* remove the track line (if any) */
9044 LISTVIEW_DrawTrackLine(infoPtr);
9045 infoPtr->xTrackLine = -1;
9049 FIXME("Changing column order not implemented\n");
9052 case HDN_ITEMCHANGINGW:
9053 case HDN_ITEMCHANGINGA:
9054 return notify_forward_header(infoPtr, lpnmh);
9056 case HDN_ITEMCHANGEDW:
9057 case HDN_ITEMCHANGEDA:
9059 COLUMN_INFO *lpColumnInfo;
9062 notify_forward_header(infoPtr, lpnmh);
9063 if (!IsWindow(hwndSelf))
9066 if (!lpnmh->pitem || !(lpnmh->pitem->mask & HDI_WIDTH))
9070 hdi.mask = HDI_WIDTH;
9071 if (!Header_GetItemW(infoPtr->hwndHeader, lpnmh->iItem, &hdi)) return 0;
9075 cxy = lpnmh->pitem->cxy;
9077 /* determine how much we change since the last know position */
9078 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, lpnmh->iItem);
9079 dx = cxy - (lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left);
9082 lpColumnInfo->rcHeader.right += dx;
9083 if (lpnmh->iItem + 1 < DPA_GetPtrCount(infoPtr->hdpaColumns))
9084 LISTVIEW_ScrollColumns(infoPtr, lpnmh->iItem + 1, dx);
9087 /* only needs to update the scrolls */
9088 infoPtr->nItemWidth += dx;
9089 LISTVIEW_UpdateScroll(infoPtr);
9091 LISTVIEW_UpdateItemSize(infoPtr);
9092 if (uView == LVS_REPORT && is_redrawing(infoPtr))
9095 RECT rcCol = lpColumnInfo->rcHeader;
9097 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
9098 OffsetRect(&rcCol, ptOrigin.x, 0);
9100 rcCol.top = infoPtr->rcList.top;
9101 rcCol.bottom = infoPtr->rcList.bottom;
9103 /* resizing left-aligned columns leaves most of the left side untouched */
9104 if ((lpColumnInfo->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_LEFT)
9106 INT nMaxDirty = infoPtr->nEllipsisWidth + infoPtr->ntmMaxCharWidth;
9109 rcCol.left = max (rcCol.left, rcCol.right - nMaxDirty);
9112 /* when shrinking the last column clear the now unused field */
9113 if (lpnmh->iItem == DPA_GetPtrCount(infoPtr->hdpaColumns) - 1)
9119 /* deal with right from rightmost column area */
9120 right.left = rcCol.right;
9121 right.top = rcCol.top;
9122 right.bottom = rcCol.bottom;
9123 right.right = infoPtr->rcList.right;
9125 LISTVIEW_InvalidateRect(infoPtr, &right);
9128 LISTVIEW_InvalidateRect(infoPtr, &rcCol);
9134 case HDN_ITEMCLICKW:
9135 case HDN_ITEMCLICKA:
9137 /* Handle sorting by Header Column */
9140 ZeroMemory(&nmlv, sizeof(NMLISTVIEW));
9142 nmlv.iSubItem = lpnmh->iItem;
9143 notify_listview(infoPtr, LVN_COLUMNCLICK, &nmlv);
9144 notify_forward_header(infoPtr, lpnmh);
9148 case HDN_DIVIDERDBLCLICKW:
9149 case HDN_DIVIDERDBLCLICKA:
9150 LISTVIEW_SetColumnWidth(infoPtr, lpnmh->iItem, LVSCW_AUTOSIZE);
9159 * Paint non-client area of control.
9162 * [I] infoPtr : valid pointer to the listview structureof the sender
9163 * [I] region : update region
9166 * TRUE - frame was painted
9167 * FALSE - call default window proc
9169 static BOOL LISTVIEW_NCPaint(const LISTVIEW_INFO *infoPtr, HRGN region)
9171 HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
9175 int cxEdge = GetSystemMetrics (SM_CXEDGE),
9176 cyEdge = GetSystemMetrics (SM_CYEDGE);
9178 if (!theme) return FALSE;
9180 GetWindowRect(infoPtr->hwndSelf, &r);
9182 cliprgn = CreateRectRgn (r.left + cxEdge, r.top + cyEdge,
9183 r.right - cxEdge, r.bottom - cyEdge);
9184 if (region != (HRGN)1)
9185 CombineRgn (cliprgn, cliprgn, region, RGN_AND);
9186 OffsetRect(&r, -r.left, -r.top);
9188 dc = GetDCEx(infoPtr->hwndSelf, region, DCX_WINDOW|DCX_INTERSECTRGN);
9189 OffsetRect(&r, -r.left, -r.top);
9191 if (IsThemeBackgroundPartiallyTransparent (theme, 0, 0))
9192 DrawThemeParentBackground(infoPtr->hwndSelf, dc, &r);
9193 DrawThemeBackground (theme, dc, 0, 0, &r, 0);
9194 ReleaseDC(infoPtr->hwndSelf, dc);
9196 /* Call default proc to get the scrollbars etc. painted */
9197 DefWindowProcW (infoPtr->hwndSelf, WM_NCPAINT, (WPARAM)cliprgn, 0);
9204 * Determines the type of structure to use.
9207 * [I] infoPtr : valid pointer to the listview structureof the sender
9208 * [I] hwndFrom : listview window handle
9209 * [I] nCommand : command specifying the nature of the WM_NOTIFYFORMAT
9214 static LRESULT LISTVIEW_NotifyFormat(LISTVIEW_INFO *infoPtr, HWND hwndFrom, INT nCommand)
9216 TRACE("(hwndFrom=%p, nCommand=%d)\n", hwndFrom, nCommand);
9218 if (nCommand == NF_REQUERY)
9219 infoPtr->notifyFormat = SendMessageW(hwndFrom, WM_NOTIFYFORMAT, (WPARAM)infoPtr->hwndSelf, NF_QUERY);
9221 return infoPtr->notifyFormat;
9226 * Paints/Repaints the listview control.
9229 * [I] infoPtr : valid pointer to the listview structure
9230 * [I] hdc : device context handle
9235 static LRESULT LISTVIEW_Paint(LISTVIEW_INFO *infoPtr, HDC hdc)
9237 TRACE("(hdc=%p)\n", hdc);
9239 if (infoPtr->bNoItemMetrics && infoPtr->nItemCount)
9241 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
9243 infoPtr->bNoItemMetrics = FALSE;
9244 LISTVIEW_UpdateItemSize(infoPtr);
9245 if (uView == LVS_ICON || uView == LVS_SMALLICON)
9246 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
9247 LISTVIEW_UpdateScroll(infoPtr);
9250 if (infoPtr->hwndHeader) UpdateWindow(infoPtr->hwndHeader);
9253 LISTVIEW_Refresh(infoPtr, hdc, NULL);
9258 hdc = BeginPaint(infoPtr->hwndSelf, &ps);
9260 LISTVIEW_Refresh(infoPtr, hdc, ps.fErase ? &ps.rcPaint : NULL);
9261 EndPaint(infoPtr->hwndSelf, &ps);
9270 * Paints/Repaints the listview control.
9273 * [I] infoPtr : valid pointer to the listview structure
9274 * [I] hdc : device context handle
9275 * [I] options : drawing options
9280 static LRESULT LISTVIEW_PrintClient(LISTVIEW_INFO *infoPtr, HDC hdc, DWORD options)
9282 FIXME("Partial Stub: (hdc=%p options=0x%08x)\n", hdc, options);
9284 if ((options & PRF_CHECKVISIBLE) && !IsWindowVisible(infoPtr->hwndSelf))
9287 if (options & PRF_ERASEBKGND)
9288 LISTVIEW_EraseBkgnd(infoPtr, hdc);
9290 if (options & PRF_CLIENT)
9291 LISTVIEW_Paint(infoPtr, hdc);
9299 * Processes double click messages (right mouse button).
9302 * [I] infoPtr : valid pointer to the listview structure
9303 * [I] wKey : key flag
9304 * [I] x,y : mouse coordinate
9309 static LRESULT LISTVIEW_RButtonDblClk(const LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
9311 LVHITTESTINFO lvHitTestInfo;
9313 TRACE("(key=%hu,X=%hu,Y=%hu)\n", wKey, x, y);
9315 /* send NM_RELEASEDCAPTURE notification */
9316 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
9318 /* send NM_RDBLCLK notification */
9319 lvHitTestInfo.pt.x = x;
9320 lvHitTestInfo.pt.y = y;
9321 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
9322 notify_click(infoPtr, NM_RDBLCLK, &lvHitTestInfo);
9329 * Processes mouse down messages (right mouse button).
9332 * [I] infoPtr : valid pointer to the listview structure
9333 * [I] wKey : key flag
9334 * [I] x,y : mouse coordinate
9339 static LRESULT LISTVIEW_RButtonDown(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
9341 LVHITTESTINFO lvHitTestInfo;
9344 TRACE("(key=%hu,X=%hu,Y=%hu)\n", wKey, x, y);
9346 /* send NM_RELEASEDCAPTURE notification */
9347 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
9349 /* make sure the listview control window has the focus */
9350 if (!infoPtr->bFocus) SetFocus(infoPtr->hwndSelf);
9352 /* set right button down flag */
9353 infoPtr->bRButtonDown = TRUE;
9355 /* determine the index of the selected item */
9356 lvHitTestInfo.pt.x = x;
9357 lvHitTestInfo.pt.y = y;
9358 nItem = LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, TRUE);
9360 if ((nItem >= 0) && (nItem < infoPtr->nItemCount))
9362 LISTVIEW_SetItemFocus(infoPtr, nItem);
9363 if (!((wKey & MK_SHIFT) || (wKey & MK_CONTROL)) &&
9364 !LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED))
9365 LISTVIEW_SetSelection(infoPtr, nItem);
9369 LISTVIEW_DeselectAll(infoPtr);
9377 * Processes mouse up messages (right mouse button).
9380 * [I] infoPtr : valid pointer to the listview structure
9381 * [I] wKey : key flag
9382 * [I] x,y : mouse coordinate
9387 static LRESULT LISTVIEW_RButtonUp(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
9389 LVHITTESTINFO lvHitTestInfo;
9392 TRACE("(key=%hu,X=%hu,Y=%hu)\n", wKey, x, y);
9394 if (!infoPtr->bRButtonDown) return 0;
9396 /* set button flag */
9397 infoPtr->bRButtonDown = FALSE;
9399 /* Send NM_RCLICK notification */
9400 lvHitTestInfo.pt.x = x;
9401 lvHitTestInfo.pt.y = y;
9402 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
9403 if (!notify_click(infoPtr, NM_RCLICK, &lvHitTestInfo)) return 0;
9405 /* Change to screen coordinate for WM_CONTEXTMENU */
9406 pt = lvHitTestInfo.pt;
9407 ClientToScreen(infoPtr->hwndSelf, &pt);
9409 /* Send a WM_CONTEXTMENU message in response to the RBUTTONUP */
9410 SendMessageW(infoPtr->hwndSelf, WM_CONTEXTMENU,
9411 (WPARAM)infoPtr->hwndSelf, MAKELPARAM(pt.x, pt.y));
9422 * [I] infoPtr : valid pointer to the listview structure
9423 * [I] hwnd : window handle of window containing the cursor
9424 * [I] nHittest : hit-test code
9425 * [I] wMouseMsg : ideintifier of the mouse message
9428 * TRUE if cursor is set
9431 static BOOL LISTVIEW_SetCursor(const LISTVIEW_INFO *infoPtr, HWND hwnd, UINT nHittest, UINT wMouseMsg)
9433 LVHITTESTINFO lvHitTestInfo;
9435 if(!(LISTVIEW_isHotTracking(infoPtr))) return FALSE;
9437 if(!infoPtr->hHotCursor) return FALSE;
9439 GetCursorPos(&lvHitTestInfo.pt);
9440 if (LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, FALSE, FALSE) < 0) return FALSE;
9442 SetCursor(infoPtr->hHotCursor);
9452 * [I] infoPtr : valid pointer to the listview structure
9453 * [I] hwndLoseFocus : handle of previously focused window
9458 static LRESULT LISTVIEW_SetFocus(LISTVIEW_INFO *infoPtr, HWND hwndLoseFocus)
9460 TRACE("(hwndLoseFocus=%p)\n", hwndLoseFocus);
9462 /* if we have the focus already, there's nothing to do */
9463 if (infoPtr->bFocus) return 0;
9465 /* send NM_SETFOCUS notification */
9466 if (!notify(infoPtr, NM_SETFOCUS)) return 0;
9468 /* set window focus flag */
9469 infoPtr->bFocus = TRUE;
9471 /* put the focus rect back on */
9472 LISTVIEW_ShowFocusRect(infoPtr, TRUE);
9474 /* redraw all visible selected items */
9475 LISTVIEW_InvalidateSelectedItems(infoPtr);
9485 * [I] infoPtr : valid pointer to the listview structure
9486 * [I] fRedraw : font handle
9487 * [I] fRedraw : redraw flag
9492 static LRESULT LISTVIEW_SetFont(LISTVIEW_INFO *infoPtr, HFONT hFont, WORD fRedraw)
9494 HFONT oldFont = infoPtr->hFont;
9496 TRACE("(hfont=%p,redraw=%hu)\n", hFont, fRedraw);
9498 infoPtr->hFont = hFont ? hFont : infoPtr->hDefaultFont;
9499 if (infoPtr->hFont == oldFont) return 0;
9501 LISTVIEW_SaveTextMetrics(infoPtr);
9503 if ((infoPtr->dwStyle & LVS_TYPEMASK) == LVS_REPORT)
9505 SendMessageW(infoPtr->hwndHeader, WM_SETFONT, (WPARAM)hFont, MAKELPARAM(fRedraw, 0));
9506 LISTVIEW_UpdateSize(infoPtr);
9507 LISTVIEW_UpdateScroll(infoPtr);
9510 if (fRedraw) LISTVIEW_InvalidateList(infoPtr);
9517 * Message handling for WM_SETREDRAW.
9518 * For the Listview, it invalidates the entire window (the doc specifies otherwise)
9521 * [I] infoPtr : valid pointer to the listview structure
9522 * [I] bRedraw: state of redraw flag
9525 * DefWinProc return value
9527 static LRESULT LISTVIEW_SetRedraw(LISTVIEW_INFO *infoPtr, BOOL bRedraw)
9529 TRACE("infoPtr->bRedraw=%d, bRedraw=%d\n", infoPtr->bRedraw, bRedraw);
9531 /* we cannot use straight equality here because _any_ non-zero value is TRUE */
9532 if ((infoPtr->bRedraw && bRedraw) || (!infoPtr->bRedraw && !bRedraw)) return 0;
9534 infoPtr->bRedraw = bRedraw;
9536 if(!bRedraw) return 0;
9538 if (is_autoarrange(infoPtr))
9539 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
9540 LISTVIEW_UpdateScroll(infoPtr);
9542 /* despite what the WM_SETREDRAW docs says, apps expect us
9543 * to invalidate the listview here... stupid! */
9544 LISTVIEW_InvalidateList(infoPtr);
9551 * Resizes the listview control. This function processes WM_SIZE
9552 * messages. At this time, the width and height are not used.
9555 * [I] infoPtr : valid pointer to the listview structure
9556 * [I] Width : new width
9557 * [I] Height : new height
9562 static LRESULT LISTVIEW_Size(LISTVIEW_INFO *infoPtr, int Width, int Height)
9564 RECT rcOld = infoPtr->rcList;
9566 TRACE("(width=%d, height=%d)\n", Width, Height);
9568 LISTVIEW_UpdateSize(infoPtr);
9569 if (EqualRect(&rcOld, &infoPtr->rcList)) return 0;
9571 /* do not bother with display related stuff if we're not redrawing */
9572 if (!is_redrawing(infoPtr)) return 0;
9574 if (is_autoarrange(infoPtr))
9575 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
9577 LISTVIEW_UpdateScroll(infoPtr);
9579 /* refresh all only for lists whose height changed significantly */
9580 if ((infoPtr->dwStyle & LVS_TYPEMASK) == LVS_LIST &&
9581 (rcOld.bottom - rcOld.top) / infoPtr->nItemHeight !=
9582 (infoPtr->rcList.bottom - infoPtr->rcList.top) / infoPtr->nItemHeight)
9583 LISTVIEW_InvalidateList(infoPtr);
9590 * Sets the size information.
9593 * [I] infoPtr : valid pointer to the listview structure
9598 static void LISTVIEW_UpdateSize(LISTVIEW_INFO *infoPtr)
9600 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
9602 TRACE("uView=%d, rcList(old)=%s\n", uView, wine_dbgstr_rect(&infoPtr->rcList));
9604 GetClientRect(infoPtr->hwndSelf, &infoPtr->rcList);
9606 if (uView == LVS_LIST)
9608 /* Apparently the "LIST" style is supposed to have the same
9609 * number of items in a column even if there is no scroll bar.
9610 * Since if a scroll bar already exists then the bottom is already
9611 * reduced, only reduce if the scroll bar does not currently exist.
9612 * The "2" is there to mimic the native control. I think it may be
9613 * related to either padding or edges. (GLA 7/2002)
9615 if (!(GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE) & WS_HSCROLL))
9616 infoPtr->rcList.bottom -= GetSystemMetrics(SM_CYHSCROLL);
9617 infoPtr->rcList.bottom = max (infoPtr->rcList.bottom - 2, 0);
9619 else if (uView == LVS_REPORT)
9624 hl.prc = &infoPtr->rcList;
9626 SendMessageW( infoPtr->hwndHeader, HDM_LAYOUT, 0, (LPARAM)&hl );
9627 TRACE(" wp.flags=0x%08x, wp=%d,%d (%dx%d)\n", wp.flags, wp.x, wp.y, wp.cx, wp.cy);
9628 SetWindowPos(wp.hwnd, wp.hwndInsertAfter, wp.x, wp.y, wp.cx, wp.cy,
9629 wp.flags | ((infoPtr->dwStyle & LVS_NOCOLUMNHEADER)
9630 ? SWP_HIDEWINDOW : SWP_SHOWWINDOW));
9631 TRACE(" after SWP wp=%d,%d (%dx%d)\n", wp.x, wp.y, wp.cx, wp.cy);
9633 infoPtr->rcList.top = max(wp.cy, 0);
9634 infoPtr->rcList.top += (infoPtr->dwLvExStyle & LVS_EX_GRIDLINES) ? 2 : 0;
9637 TRACE(" rcList=%s\n", wine_dbgstr_rect(&infoPtr->rcList));
9642 * Processes WM_STYLECHANGED messages.
9645 * [I] infoPtr : valid pointer to the listview structure
9646 * [I] wStyleType : window style type (normal or extended)
9647 * [I] lpss : window style information
9652 static INT LISTVIEW_StyleChanged(LISTVIEW_INFO *infoPtr, WPARAM wStyleType,
9653 const STYLESTRUCT *lpss)
9655 UINT uNewView = lpss->styleNew & LVS_TYPEMASK;
9656 UINT uOldView = lpss->styleOld & LVS_TYPEMASK;
9659 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
9660 wStyleType, lpss->styleOld, lpss->styleNew);
9662 if (wStyleType != GWL_STYLE) return 0;
9664 infoPtr->dwStyle = lpss->styleNew;
9666 if (((lpss->styleOld & WS_HSCROLL) != 0)&&
9667 ((lpss->styleNew & WS_HSCROLL) == 0))
9668 ShowScrollBar(infoPtr->hwndSelf, SB_HORZ, FALSE);
9670 if (((lpss->styleOld & WS_VSCROLL) != 0)&&
9671 ((lpss->styleNew & WS_VSCROLL) == 0))
9672 ShowScrollBar(infoPtr->hwndSelf, SB_VERT, FALSE);
9674 if (uNewView != uOldView)
9676 SIZE oldIconSize = infoPtr->iconSize;
9679 SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
9680 ShowWindow(infoPtr->hwndHeader, SW_HIDE);
9682 ShowScrollBar(infoPtr->hwndSelf, SB_BOTH, FALSE);
9683 SetRectEmpty(&infoPtr->rcFocus);
9685 himl = (uNewView == LVS_ICON ? infoPtr->himlNormal : infoPtr->himlSmall);
9686 set_icon_size(&infoPtr->iconSize, himl, uNewView != LVS_ICON);
9688 if (uNewView == LVS_ICON)
9690 if ((infoPtr->iconSize.cx != oldIconSize.cx) || (infoPtr->iconSize.cy != oldIconSize.cy))
9692 TRACE("icon old size=(%d,%d), new size=(%d,%d)\n",
9693 oldIconSize.cx, oldIconSize.cy, infoPtr->iconSize.cx, infoPtr->iconSize.cy);
9694 LISTVIEW_SetIconSpacing(infoPtr, 0, 0);
9697 else if (uNewView == LVS_REPORT)
9702 LISTVIEW_CreateHeader( infoPtr );
9704 hl.prc = &infoPtr->rcList;
9706 SendMessageW( infoPtr->hwndHeader, HDM_LAYOUT, 0, (LPARAM)&hl );
9707 SetWindowPos(infoPtr->hwndHeader, infoPtr->hwndSelf, wp.x, wp.y, wp.cx, wp.cy,
9708 wp.flags | ((infoPtr->dwStyle & LVS_NOCOLUMNHEADER)
9709 ? SWP_HIDEWINDOW : SWP_SHOWWINDOW));
9712 LISTVIEW_UpdateItemSize(infoPtr);
9715 if (uNewView == LVS_REPORT)
9717 if ((lpss->styleOld ^ lpss->styleNew) & LVS_NOCOLUMNHEADER)
9719 if (lpss->styleNew & LVS_NOCOLUMNHEADER)
9721 /* Turn off the header control */
9722 style = GetWindowLongW(infoPtr->hwndHeader, GWL_STYLE);
9723 TRACE("Hide header control, was 0x%08x\n", style);
9724 SetWindowLongW(infoPtr->hwndHeader, GWL_STYLE, style | HDS_HIDDEN);
9726 /* Turn on the header control */
9727 if ((style = GetWindowLongW(infoPtr->hwndHeader, GWL_STYLE)) & HDS_HIDDEN)
9729 TRACE("Show header control, was 0x%08x\n", style);
9730 SetWindowLongW(infoPtr->hwndHeader, GWL_STYLE, (style & ~HDS_HIDDEN) | WS_VISIBLE);
9736 if ( (uNewView == LVS_ICON || uNewView == LVS_SMALLICON) &&
9737 (uNewView != uOldView || ((lpss->styleNew ^ lpss->styleOld) & LVS_ALIGNMASK)) )
9738 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
9740 /* update the size of the client area */
9741 LISTVIEW_UpdateSize(infoPtr);
9743 /* add scrollbars if needed */
9744 LISTVIEW_UpdateScroll(infoPtr);
9746 /* invalidate client area + erase background */
9747 LISTVIEW_InvalidateList(infoPtr);
9754 * Processes WM_STYLECHANGING messages.
9757 * [I] infoPtr : valid pointer to the listview structure
9758 * [I] wStyleType : window style type (normal or extended)
9759 * [I0] lpss : window style information
9764 static INT LISTVIEW_StyleChanging(LISTVIEW_INFO *infoPtr, WPARAM wStyleType,
9767 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
9768 wStyleType, lpss->styleOld, lpss->styleNew);
9770 /* don't forward LVS_OWNERDATA only if not already set to */
9771 if ((lpss->styleNew ^ lpss->styleOld) & LVS_OWNERDATA)
9773 if (lpss->styleOld & LVS_OWNERDATA)
9774 lpss->styleNew |= LVS_OWNERDATA;
9776 lpss->styleNew &= ~LVS_OWNERDATA;
9784 * Processes WM_SHOWWINDOW messages.
9787 * [I] infoPtr : valid pointer to the listview structure
9788 * [I] bShown : window is being shown (FALSE when hidden)
9789 * [I] iStatus : window show status
9794 static LRESULT LISTVIEW_ShowWindow(LISTVIEW_INFO *infoPtr, BOOL bShown, INT iStatus)
9796 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
9798 /* header delayed creation */
9799 if ((uView == LVS_REPORT) && bShown)
9801 LISTVIEW_CreateHeader(infoPtr);
9803 if (!(LVS_NOCOLUMNHEADER & infoPtr->dwStyle))
9804 ShowWindow(infoPtr->hwndHeader, SW_SHOWNORMAL);
9812 * Processes CCM_GETVERSION messages.
9815 * [I] infoPtr : valid pointer to the listview structure
9820 static inline LRESULT LISTVIEW_GetVersion(LISTVIEW_INFO *infoPtr)
9822 return infoPtr->iVersion;
9827 * Processes CCM_SETVERSION messages.
9830 * [I] infoPtr : valid pointer to the listview structure
9831 * [I] iVersion : version to be set
9834 * -1 when requested version is greater than DLL version;
9835 * previous version otherwise
9837 static LRESULT LISTVIEW_SetVersion(LISTVIEW_INFO *infoPtr, DWORD iVersion)
9839 INT iOldVersion = infoPtr->iVersion;
9841 if (iVersion > COMCTL32_VERSION)
9844 infoPtr->iVersion = iVersion;
9846 TRACE("new version %d\n", iVersion);
9853 * Window procedure of the listview control.
9856 static LRESULT WINAPI
9857 LISTVIEW_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
9859 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongPtrW(hwnd, 0);
9861 TRACE("(uMsg=%x wParam=%lx lParam=%lx)\n", uMsg, wParam, lParam);
9863 if (!infoPtr && (uMsg != WM_NCCREATE))
9864 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
9868 case LVM_APPROXIMATEVIEWRECT:
9869 return LISTVIEW_ApproximateViewRect(infoPtr, (INT)wParam,
9870 LOWORD(lParam), HIWORD(lParam));
9872 return LISTVIEW_Arrange(infoPtr, (INT)wParam);
9874 /* case LVM_CANCELEDITLABEL: */
9876 case LVM_CREATEDRAGIMAGE:
9877 return (LRESULT)LISTVIEW_CreateDragImage(infoPtr, (INT)wParam, (LPPOINT)lParam);
9879 case LVM_DELETEALLITEMS:
9880 return LISTVIEW_DeleteAllItems(infoPtr, FALSE);
9882 case LVM_DELETECOLUMN:
9883 return LISTVIEW_DeleteColumn(infoPtr, (INT)wParam);
9885 case LVM_DELETEITEM:
9886 return LISTVIEW_DeleteItem(infoPtr, (INT)wParam);
9888 case LVM_EDITLABELW:
9889 return (LRESULT)LISTVIEW_EditLabelT(infoPtr, (INT)wParam, TRUE);
9891 case LVM_EDITLABELA:
9892 return (LRESULT)LISTVIEW_EditLabelT(infoPtr, (INT)wParam, FALSE);
9894 /* case LVM_ENABLEGROUPVIEW: */
9896 case LVM_ENSUREVISIBLE:
9897 return LISTVIEW_EnsureVisible(infoPtr, (INT)wParam, (BOOL)lParam);
9900 return LISTVIEW_FindItemW(infoPtr, (INT)wParam, (LPLVFINDINFOW)lParam);
9903 return LISTVIEW_FindItemA(infoPtr, (INT)wParam, (LPLVFINDINFOA)lParam);
9905 case LVM_GETBKCOLOR:
9906 return infoPtr->clrBk;
9908 /* case LVM_GETBKIMAGE: */
9910 case LVM_GETCALLBACKMASK:
9911 return infoPtr->uCallbackMask;
9913 case LVM_GETCOLUMNA:
9914 return LISTVIEW_GetColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam, FALSE);
9916 case LVM_GETCOLUMNW:
9917 return LISTVIEW_GetColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam, TRUE);
9919 case LVM_GETCOLUMNORDERARRAY:
9920 return LISTVIEW_GetColumnOrderArray(infoPtr, (INT)wParam, (LPINT)lParam);
9922 case LVM_GETCOLUMNWIDTH:
9923 return LISTVIEW_GetColumnWidth(infoPtr, (INT)wParam);
9925 case LVM_GETCOUNTPERPAGE:
9926 return LISTVIEW_GetCountPerPage(infoPtr);
9928 case LVM_GETEDITCONTROL:
9929 return (LRESULT)infoPtr->hwndEdit;
9931 case LVM_GETEXTENDEDLISTVIEWSTYLE:
9932 return infoPtr->dwLvExStyle;
9934 /* case LVM_GETGROUPINFO: */
9936 /* case LVM_GETGROUPMETRICS: */
9939 return (LRESULT)infoPtr->hwndHeader;
9941 case LVM_GETHOTCURSOR:
9942 return (LRESULT)infoPtr->hHotCursor;
9944 case LVM_GETHOTITEM:
9945 return infoPtr->nHotItem;
9947 case LVM_GETHOVERTIME:
9948 return infoPtr->dwHoverTime;
9950 case LVM_GETIMAGELIST:
9951 return (LRESULT)LISTVIEW_GetImageList(infoPtr, (INT)wParam);
9953 /* case LVM_GETINSERTMARK: */
9955 /* case LVM_GETINSERTMARKCOLOR: */
9957 /* case LVM_GETINSERTMARKRECT: */
9959 case LVM_GETISEARCHSTRINGA:
9960 case LVM_GETISEARCHSTRINGW:
9961 FIXME("LVM_GETISEARCHSTRING: unimplemented\n");
9965 return LISTVIEW_GetItemExtT(infoPtr, (LPLVITEMW)lParam, FALSE);
9968 return LISTVIEW_GetItemExtT(infoPtr, (LPLVITEMW)lParam, TRUE);
9970 case LVM_GETITEMCOUNT:
9971 return infoPtr->nItemCount;
9973 case LVM_GETITEMPOSITION:
9974 return LISTVIEW_GetItemPosition(infoPtr, (INT)wParam, (LPPOINT)lParam);
9976 case LVM_GETITEMRECT:
9977 return LISTVIEW_GetItemRect(infoPtr, (INT)wParam, (LPRECT)lParam);
9979 case LVM_GETITEMSPACING:
9980 return LISTVIEW_GetItemSpacing(infoPtr, (BOOL)wParam);
9982 case LVM_GETITEMSTATE:
9983 return LISTVIEW_GetItemState(infoPtr, (INT)wParam, (UINT)lParam);
9985 case LVM_GETITEMTEXTA:
9986 return LISTVIEW_GetItemTextT(infoPtr, (INT)wParam, (LPLVITEMW)lParam, FALSE);
9988 case LVM_GETITEMTEXTW:
9989 return LISTVIEW_GetItemTextT(infoPtr, (INT)wParam, (LPLVITEMW)lParam, TRUE);
9991 case LVM_GETNEXTITEM:
9992 return LISTVIEW_GetNextItem(infoPtr, (INT)wParam, LOWORD(lParam));
9994 case LVM_GETNUMBEROFWORKAREAS:
9995 FIXME("LVM_GETNUMBEROFWORKAREAS: unimplemented\n");
9999 if (!lParam) return FALSE;
10000 if ((infoPtr->dwStyle & LVS_TYPEMASK) == LVS_REPORT ||
10001 (infoPtr->dwStyle & LVS_TYPEMASK) == LVS_LIST) return FALSE;
10002 LISTVIEW_GetOrigin(infoPtr, (LPPOINT)lParam);
10005 /* case LVM_GETOUTLINECOLOR: */
10007 /* case LVM_GETSELECTEDCOLUMN: */
10009 case LVM_GETSELECTEDCOUNT:
10010 return LISTVIEW_GetSelectedCount(infoPtr);
10012 case LVM_GETSELECTIONMARK:
10013 return infoPtr->nSelectionMark;
10015 case LVM_GETSTRINGWIDTHA:
10016 return LISTVIEW_GetStringWidthT(infoPtr, (LPCWSTR)lParam, FALSE);
10018 case LVM_GETSTRINGWIDTHW:
10019 return LISTVIEW_GetStringWidthT(infoPtr, (LPCWSTR)lParam, TRUE);
10021 case LVM_GETSUBITEMRECT:
10022 return LISTVIEW_GetSubItemRect(infoPtr, (UINT)wParam, (LPRECT)lParam);
10024 case LVM_GETTEXTBKCOLOR:
10025 return infoPtr->clrTextBk;
10027 case LVM_GETTEXTCOLOR:
10028 return infoPtr->clrText;
10030 /* case LVM_GETTILEINFO: */
10032 /* case LVM_GETTILEVIEWINFO: */
10034 case LVM_GETTOOLTIPS:
10035 if( !infoPtr->hwndToolTip )
10036 infoPtr->hwndToolTip = COMCTL32_CreateToolTip( hwnd );
10037 return (LRESULT)infoPtr->hwndToolTip;
10039 case LVM_GETTOPINDEX:
10040 return LISTVIEW_GetTopIndex(infoPtr);
10042 case LVM_GETUNICODEFORMAT:
10043 return (infoPtr->notifyFormat == NFR_UNICODE);
10045 /* case LVM_GETVIEW: */
10047 case LVM_GETVIEWRECT:
10048 return LISTVIEW_GetViewRect(infoPtr, (LPRECT)lParam);
10050 case LVM_GETWORKAREAS:
10051 FIXME("LVM_GETWORKAREAS: unimplemented\n");
10054 /* case LVM_HASGROUP: */
10057 return LISTVIEW_HitTest(infoPtr, (LPLVHITTESTINFO)lParam, FALSE, FALSE);
10059 case LVM_INSERTCOLUMNA:
10060 return LISTVIEW_InsertColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam, FALSE);
10062 case LVM_INSERTCOLUMNW:
10063 return LISTVIEW_InsertColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam, TRUE);
10065 /* case LVM_INSERTGROUP: */
10067 /* case LVM_INSERTGROUPSORTED: */
10069 case LVM_INSERTITEMA:
10070 return LISTVIEW_InsertItemT(infoPtr, (LPLVITEMW)lParam, FALSE);
10072 case LVM_INSERTITEMW:
10073 return LISTVIEW_InsertItemT(infoPtr, (LPLVITEMW)lParam, TRUE);
10075 /* case LVM_INSERTMARKHITTEST: */
10077 /* case LVM_ISGROUPVIEWENABLED: */
10079 /* case LVM_MAPIDTOINDEX: */
10081 /* case LVM_MAPINDEXTOID: */
10083 /* case LVM_MOVEGROUP: */
10085 /* case LVM_MOVEITEMTOGROUP: */
10087 case LVM_REDRAWITEMS:
10088 return LISTVIEW_RedrawItems(infoPtr, (INT)wParam, (INT)lParam);
10090 /* case LVM_REMOVEALLGROUPS: */
10092 /* case LVM_REMOVEGROUP: */
10095 return LISTVIEW_Scroll(infoPtr, (INT)wParam, (INT)lParam);
10097 case LVM_SETBKCOLOR:
10098 return LISTVIEW_SetBkColor(infoPtr, (COLORREF)lParam);
10100 /* case LVM_SETBKIMAGE: */
10102 case LVM_SETCALLBACKMASK:
10103 infoPtr->uCallbackMask = (UINT)wParam;
10106 case LVM_SETCOLUMNA:
10107 return LISTVIEW_SetColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam, FALSE);
10109 case LVM_SETCOLUMNW:
10110 return LISTVIEW_SetColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam, TRUE);
10112 case LVM_SETCOLUMNORDERARRAY:
10113 return LISTVIEW_SetColumnOrderArray(infoPtr, (INT)wParam, (LPINT)lParam);
10115 case LVM_SETCOLUMNWIDTH:
10116 return LISTVIEW_SetColumnWidth(infoPtr, (INT)wParam, (short)LOWORD(lParam));
10118 case LVM_SETEXTENDEDLISTVIEWSTYLE:
10119 return LISTVIEW_SetExtendedListViewStyle(infoPtr, (DWORD)wParam, (DWORD)lParam);
10121 /* case LVM_SETGROUPINFO: */
10123 /* case LVM_SETGROUPMETRICS: */
10125 case LVM_SETHOTCURSOR:
10126 return (LRESULT)LISTVIEW_SetHotCursor(infoPtr, (HCURSOR)lParam);
10128 case LVM_SETHOTITEM:
10129 return LISTVIEW_SetHotItem(infoPtr, (INT)wParam);
10131 case LVM_SETHOVERTIME:
10132 return LISTVIEW_SetHoverTime(infoPtr, (DWORD)wParam);
10134 case LVM_SETICONSPACING:
10135 return LISTVIEW_SetIconSpacing(infoPtr, (short)LOWORD(lParam), (short)HIWORD(lParam));
10137 case LVM_SETIMAGELIST:
10138 return (LRESULT)LISTVIEW_SetImageList(infoPtr, (INT)wParam, (HIMAGELIST)lParam);
10140 /* case LVM_SETINFOTIP: */
10142 /* case LVM_SETINSERTMARK: */
10144 /* case LVM_SETINSERTMARKCOLOR: */
10149 if (infoPtr->dwStyle & LVS_OWNERDATA) return FALSE;
10150 return LISTVIEW_SetItemT(infoPtr, (LPLVITEMW)lParam, (uMsg == LVM_SETITEMW));
10153 case LVM_SETITEMCOUNT:
10154 return LISTVIEW_SetItemCount(infoPtr, (INT)wParam, (DWORD)lParam);
10156 case LVM_SETITEMPOSITION:
10159 pt.x = (short)LOWORD(lParam);
10160 pt.y = (short)HIWORD(lParam);
10161 return LISTVIEW_SetItemPosition(infoPtr, (INT)wParam, pt);
10164 case LVM_SETITEMPOSITION32:
10165 if (lParam == 0) return FALSE;
10166 return LISTVIEW_SetItemPosition(infoPtr, (INT)wParam, *((POINT*)lParam));
10168 case LVM_SETITEMSTATE:
10169 return LISTVIEW_SetItemState(infoPtr, (INT)wParam, (LPLVITEMW)lParam);
10171 case LVM_SETITEMTEXTA:
10172 return LISTVIEW_SetItemTextT(infoPtr, (INT)wParam, (LPLVITEMW)lParam, FALSE);
10174 case LVM_SETITEMTEXTW:
10175 return LISTVIEW_SetItemTextT(infoPtr, (INT)wParam, (LPLVITEMW)lParam, TRUE);
10177 /* case LVM_SETOUTLINECOLOR: */
10179 /* case LVM_SETSELECTEDCOLUMN: */
10181 case LVM_SETSELECTIONMARK:
10182 return LISTVIEW_SetSelectionMark(infoPtr, (INT)lParam);
10184 case LVM_SETTEXTBKCOLOR:
10185 return LISTVIEW_SetTextBkColor(infoPtr, (COLORREF)lParam);
10187 case LVM_SETTEXTCOLOR:
10188 return LISTVIEW_SetTextColor(infoPtr, (COLORREF)lParam);
10190 /* case LVM_SETTILEINFO: */
10192 /* case LVM_SETTILEVIEWINFO: */
10194 /* case LVM_SETTILEWIDTH: */
10196 case LVM_SETTOOLTIPS:
10197 return (LRESULT)LISTVIEW_SetToolTips(infoPtr, (HWND)lParam);
10199 case LVM_SETUNICODEFORMAT:
10200 return LISTVIEW_SetUnicodeFormat(infoPtr, wParam);
10202 /* case LVM_SETVIEW: */
10204 /* case LVM_SETWORKAREAS: */
10206 /* case LVM_SORTGROUPS: */
10208 case LVM_SORTITEMS:
10209 return LISTVIEW_SortItems(infoPtr, (PFNLVCOMPARE)lParam, (LPARAM)wParam, FALSE);
10211 case LVM_SORTITEMSEX:
10212 return LISTVIEW_SortItems(infoPtr, (PFNLVCOMPARE)lParam, (LPARAM)wParam, TRUE);
10214 case LVM_SUBITEMHITTEST:
10215 return LISTVIEW_HitTest(infoPtr, (LPLVHITTESTINFO)lParam, TRUE, FALSE);
10218 return LISTVIEW_Update(infoPtr, (INT)wParam);
10220 case CCM_GETVERSION:
10221 return LISTVIEW_GetVersion(infoPtr);
10223 case CCM_SETVERSION:
10224 return LISTVIEW_SetVersion(infoPtr, wParam);
10227 return LISTVIEW_ProcessLetterKeys( infoPtr, wParam, lParam );
10230 return LISTVIEW_Command(infoPtr, wParam, lParam);
10233 return LISTVIEW_NCCreate(hwnd, (LPCREATESTRUCTW)lParam);
10236 return LISTVIEW_Create(hwnd, (LPCREATESTRUCTW)lParam);
10239 return LISTVIEW_Destroy(infoPtr);
10242 return LISTVIEW_Enable(infoPtr, (BOOL)wParam);
10244 case WM_ERASEBKGND:
10245 return LISTVIEW_EraseBkgnd(infoPtr, (HDC)wParam);
10247 case WM_GETDLGCODE:
10248 return DLGC_WANTCHARS | DLGC_WANTARROWS;
10251 return (LRESULT)infoPtr->hFont;
10254 return LISTVIEW_HScroll(infoPtr, (INT)LOWORD(wParam), 0, (HWND)lParam);
10257 return LISTVIEW_KeyDown(infoPtr, (INT)wParam, (LONG)lParam);
10260 return LISTVIEW_KillFocus(infoPtr);
10262 case WM_LBUTTONDBLCLK:
10263 return LISTVIEW_LButtonDblClk(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
10265 case WM_LBUTTONDOWN:
10266 return LISTVIEW_LButtonDown(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
10269 return LISTVIEW_LButtonUp(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
10272 return LISTVIEW_MouseMove (infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
10274 case WM_MOUSEHOVER:
10275 return LISTVIEW_MouseHover(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
10278 return LISTVIEW_NCDestroy(infoPtr);
10281 if (LISTVIEW_NCPaint(infoPtr, (HRGN)wParam))
10286 if (lParam && ((LPNMHDR)lParam)->hwndFrom == infoPtr->hwndHeader)
10287 return LISTVIEW_HeaderNotification(infoPtr, (LPNMHEADERW)lParam);
10290 case WM_NOTIFYFORMAT:
10291 return LISTVIEW_NotifyFormat(infoPtr, (HWND)wParam, (INT)lParam);
10293 case WM_PRINTCLIENT:
10294 return LISTVIEW_PrintClient(infoPtr, (HDC)wParam, (DWORD)lParam);
10297 return LISTVIEW_Paint(infoPtr, (HDC)wParam);
10299 case WM_RBUTTONDBLCLK:
10300 return LISTVIEW_RButtonDblClk(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
10302 case WM_RBUTTONDOWN:
10303 return LISTVIEW_RButtonDown(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
10306 return LISTVIEW_RButtonUp(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
10309 if(LISTVIEW_SetCursor(infoPtr, (HWND)wParam, LOWORD(lParam), HIWORD(lParam)))
10314 return LISTVIEW_SetFocus(infoPtr, (HWND)wParam);
10317 return LISTVIEW_SetFont(infoPtr, (HFONT)wParam, (WORD)lParam);
10320 return LISTVIEW_SetRedraw(infoPtr, (BOOL)wParam);
10322 case WM_SHOWWINDOW:
10323 LISTVIEW_ShowWindow(infoPtr, (BOOL)wParam, (INT)lParam);
10324 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
10327 return LISTVIEW_Size(infoPtr, (short)LOWORD(lParam), (short)HIWORD(lParam));
10329 case WM_STYLECHANGED:
10330 return LISTVIEW_StyleChanged(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
10332 case WM_STYLECHANGING:
10333 return LISTVIEW_StyleChanging(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
10335 case WM_SYSCOLORCHANGE:
10336 COMCTL32_RefreshSysColors();
10339 /* case WM_TIMER: */
10340 case WM_THEMECHANGED:
10341 return LISTVIEW_ThemeChanged(infoPtr);
10344 return LISTVIEW_VScroll(infoPtr, (INT)LOWORD(wParam), 0, (HWND)lParam);
10346 case WM_MOUSEWHEEL:
10347 if (wParam & (MK_SHIFT | MK_CONTROL))
10348 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
10349 return LISTVIEW_MouseWheel(infoPtr, (short int)HIWORD(wParam));
10351 case WM_WINDOWPOSCHANGED:
10352 if (!(((WINDOWPOS *)lParam)->flags & SWP_NOSIZE))
10354 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
10355 SetWindowPos(infoPtr->hwndSelf, 0, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOACTIVATE |
10356 SWP_NOZORDER | SWP_NOMOVE | SWP_NOSIZE);
10358 if ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (uView == LVS_REPORT))
10360 MEASUREITEMSTRUCT mis;
10361 mis.CtlType = ODT_LISTVIEW;
10362 mis.CtlID = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
10366 mis.itemHeight= infoPtr->nItemHeight;
10367 SendMessageW(infoPtr->hwndNotify, WM_MEASUREITEM, mis.CtlID, (LPARAM)&mis);
10368 if (infoPtr->nItemHeight != max(mis.itemHeight, 1))
10369 infoPtr->nMeasureItemHeight = infoPtr->nItemHeight = max(mis.itemHeight, 1);
10372 LISTVIEW_UpdateSize(infoPtr);
10373 LISTVIEW_UpdateScroll(infoPtr);
10375 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
10377 /* case WM_WININICHANGE: */
10380 if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
10381 ERR("unknown msg %04x wp=%08lx lp=%08lx\n", uMsg, wParam, lParam);
10384 /* call default window procedure */
10385 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
10392 * Registers the window class.
10400 void LISTVIEW_Register(void)
10402 WNDCLASSW wndClass;
10404 ZeroMemory(&wndClass, sizeof(WNDCLASSW));
10405 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS;
10406 wndClass.lpfnWndProc = LISTVIEW_WindowProc;
10407 wndClass.cbClsExtra = 0;
10408 wndClass.cbWndExtra = sizeof(LISTVIEW_INFO *);
10409 wndClass.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
10410 wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
10411 wndClass.lpszClassName = WC_LISTVIEWW;
10412 RegisterClassW(&wndClass);
10417 * Unregisters the window class.
10425 void LISTVIEW_Unregister(void)
10427 UnregisterClassW(WC_LISTVIEWW, NULL);
10432 * Handle any WM_COMMAND messages
10435 * [I] infoPtr : valid pointer to the listview structure
10436 * [I] wParam : the first message parameter
10437 * [I] lParam : the second message parameter
10442 static LRESULT LISTVIEW_Command(const LISTVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
10444 switch (HIWORD(wParam))
10449 * Adjust the edit window size
10451 WCHAR buffer[1024];
10452 HDC hdc = GetDC(infoPtr->hwndEdit);
10453 HFONT hFont, hOldFont = 0;
10457 if (!infoPtr->hwndEdit || !hdc) return 0;
10458 GetWindowTextW(infoPtr->hwndEdit, buffer, sizeof(buffer)/sizeof(buffer[0]));
10459 GetWindowRect(infoPtr->hwndEdit, &rect);
10461 /* Select font to get the right dimension of the string */
10462 hFont = (HFONT)SendMessageW(infoPtr->hwndEdit, WM_GETFONT, 0, 0);
10465 hOldFont = SelectObject(hdc, hFont);
10468 if (GetTextExtentPoint32W(hdc, buffer, lstrlenW(buffer), &sz))
10470 TEXTMETRICW textMetric;
10472 /* Add Extra spacing for the next character */
10473 GetTextMetricsW(hdc, &textMetric);
10474 sz.cx += (textMetric.tmMaxCharWidth * 2);
10482 rect.bottom - rect.top,
10483 SWP_DRAWFRAME|SWP_NOMOVE);
10486 SelectObject(hdc, hOldFont);
10488 ReleaseDC(infoPtr->hwndEdit, hdc);
10494 return SendMessageW (infoPtr->hwndNotify, WM_COMMAND, wParam, lParam);
10503 * Subclassed edit control windproc function
10506 * [I] hwnd : the edit window handle
10507 * [I] uMsg : the message that is to be processed
10508 * [I] wParam : first message parameter
10509 * [I] lParam : second message parameter
10510 * [I] isW : TRUE if input is Unicode
10515 static LRESULT EditLblWndProcT(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL isW)
10517 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongPtrW(GetParent(hwnd), 0);
10518 BOOL cancel = FALSE;
10520 TRACE("(hwnd=%p, uMsg=%x, wParam=%lx, lParam=%lx, isW=%d)\n",
10521 hwnd, uMsg, wParam, lParam, isW);
10525 case WM_GETDLGCODE:
10526 return DLGC_WANTARROWS | DLGC_WANTALLKEYS;
10533 WNDPROC editProc = infoPtr->EditWndProc;
10534 infoPtr->EditWndProc = 0;
10535 SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (DWORD_PTR)editProc);
10536 return CallWindowProcT(editProc, hwnd, uMsg, wParam, lParam, isW);
10540 if (VK_ESCAPE == (INT)wParam)
10545 else if (VK_RETURN == (INT)wParam)
10549 return CallWindowProcT(infoPtr->EditWndProc, hwnd, uMsg, wParam, lParam, isW);
10552 /* kill the edit */
10553 if (infoPtr->hwndEdit)
10555 LPWSTR buffer = NULL;
10557 infoPtr->hwndEdit = 0;
10560 DWORD len = isW ? GetWindowTextLengthW(hwnd) : GetWindowTextLengthA(hwnd);
10564 if ( (buffer = Alloc((len+1) * (isW ? sizeof(WCHAR) : sizeof(CHAR)))) )
10566 if (isW) GetWindowTextW(hwnd, buffer, len+1);
10567 else GetWindowTextA(hwnd, (CHAR*)buffer, len+1);
10571 LISTVIEW_EndEditLabelT(infoPtr, buffer, isW);
10576 SendMessageW(hwnd, WM_CLOSE, 0, 0);
10582 * Subclassed edit control Unicode windproc function
10585 * [I] hwnd : the edit window handle
10586 * [I] uMsg : the message that is to be processed
10587 * [I] wParam : first message parameter
10588 * [I] lParam : second message parameter
10592 static LRESULT CALLBACK EditLblWndProcW(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
10594 return EditLblWndProcT(hwnd, uMsg, wParam, lParam, TRUE);
10599 * Subclassed edit control ANSI windproc function
10602 * [I] hwnd : the edit window handle
10603 * [I] uMsg : the message that is to be processed
10604 * [I] wParam : first message parameter
10605 * [I] lParam : second message parameter
10609 static LRESULT CALLBACK EditLblWndProcA(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
10611 return EditLblWndProcT(hwnd, uMsg, wParam, lParam, FALSE);
10616 * Creates a subclassed edit control
10619 * [I] infoPtr : valid pointer to the listview structure
10620 * [I] text : initial text for the edit
10621 * [I] style : the window style
10622 * [I] isW : TRUE if input is Unicode
10626 static HWND CreateEditLabelT(LISTVIEW_INFO *infoPtr, LPCWSTR text, DWORD style,
10627 INT x, INT y, INT width, INT height, BOOL isW)
10629 WCHAR editName[5] = { 'E', 'd', 'i', 't', '\0' };
10634 TEXTMETRICW textMetric;
10635 HINSTANCE hinst = (HINSTANCE)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_HINSTANCE);
10637 TRACE("(text=%s, ..., isW=%d)\n", debugtext_t(text, isW), isW);
10639 style |= WS_CHILDWINDOW|WS_CLIPSIBLINGS|ES_LEFT|ES_AUTOHSCROLL|WS_BORDER;
10640 hdc = GetDC(infoPtr->hwndSelf);
10642 /* Select the font to get appropriate metric dimensions */
10643 if(infoPtr->hFont != 0)
10644 hOldFont = SelectObject(hdc, infoPtr->hFont);
10646 /*Get String Length in pixels */
10647 GetTextExtentPoint32W(hdc, text, lstrlenW(text), &sz);
10649 /*Add Extra spacing for the next character */
10650 GetTextMetricsW(hdc, &textMetric);
10651 sz.cx += (textMetric.tmMaxCharWidth * 2);
10653 if(infoPtr->hFont != 0)
10654 SelectObject(hdc, hOldFont);
10656 ReleaseDC(infoPtr->hwndSelf, hdc);
10658 hedit = CreateWindowW(editName, text, style, x, y, sz.cx, height, infoPtr->hwndSelf, 0, hinst, 0);
10660 hedit = CreateWindowA("Edit", (LPCSTR)text, style, x, y, sz.cx, height, infoPtr->hwndSelf, 0, hinst, 0);
10662 if (!hedit) return 0;
10664 infoPtr->EditWndProc = (WNDPROC)
10665 (isW ? SetWindowLongPtrW(hedit, GWLP_WNDPROC, (DWORD_PTR)EditLblWndProcW) :
10666 SetWindowLongPtrA(hedit, GWLP_WNDPROC, (DWORD_PTR)EditLblWndProcA) );
10668 SendMessageW(hedit, WM_SETFONT, (WPARAM)infoPtr->hFont, FALSE);