4 * Copyright 1998, 1999 Eric Kohl
5 * Copyright 1999 Luc Tourangeau
6 * Copyright 2000 Jason Mawdsley
7 * Copyright 2001 CodeWeavers Inc.
8 * Copyright 2002 Dimitrie O. Paun
9 * Copyright 2009-2013 Nikolay Sivov
10 * Copyright 2009 Owen Rudge for CodeWeavers
11 * Copyright 2012-2013 Daniel Jelinski
13 * This library is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU Lesser General Public
15 * License as published by the Free Software Foundation; either
16 * version 2.1 of the License, or (at your option) any later version.
18 * This library is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * Lesser General Public License for more details.
23 * You should have received a copy of the GNU Lesser General Public
24 * License along with this library; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
29 * This code was audited for completeness against the documented features
30 * of Comctl32.dll version 6.0 on May. 20, 2005, by James Hawkins.
32 * Unless otherwise noted, we believe this code to be complete, as per
33 * the specification mentioned above.
34 * If you discover missing features, or bugs, please note them below.
38 * Default Message Processing
39 * -- WM_CREATE: create the icon and small icon image lists at this point only if
40 * the LVS_SHAREIMAGELISTS style is not specified.
41 * -- WM_WINDOWPOSCHANGED: arrange the list items if the current view is icon
42 * or small icon and the LVS_AUTOARRANGE style is specified.
47 * -- Hot item handling, mouse hovering
48 * -- Workareas support
53 * -- Expand large item in ICON mode when the cursor is flying over the icon or text.
54 * -- Support CustomDraw options for _WIN32_IE >= 0x560 (see NMLVCUSTOMDRAW docs).
55 * -- LVA_SNAPTOGRID not implemented
56 * -- LISTVIEW_ApproximateViewRect partially implemented
57 * -- LISTVIEW_SetColumnWidth ignores header images & bitmap
58 * -- LISTVIEW_StyleChanged doesn't handle some changes too well
61 * -- LISTVIEW_GetNextItem needs to be rewritten. It is currently
62 * linear in the number of items in the list, and this is
63 * unacceptable for large lists.
64 * -- if list is sorted by item text LISTVIEW_InsertItemT could use
65 * binary search to calculate item index (e.g. DPA_Search()).
66 * This requires sorted state to be reliably tracked in item modifiers.
67 * -- we should keep an ordered array of coordinates in iconic mode
68 * this would allow to frame items (iterator_frameditems),
69 * and find nearest item (LVFI_NEARESTXY) a lot more efficiently
76 * -- LVIS_ACTIVATING (not currently supported by comctl32.dll version 6.0)
82 * -- LVS_NOSCROLL (see Q137520)
86 * -- LVS_EX_BORDERSELECT
90 * -- LVS_EX_MULTIWORKAREAS
92 * -- LVS_EX_SIMPLESELECT
93 * -- LVS_EX_TWOCLICKACTIVATE
94 * -- LVS_EX_UNDERLINECOLD
95 * -- LVS_EX_UNDERLINEHOT
98 * -- LVN_BEGINSCROLL, LVN_ENDSCROLL
104 * -- LVM_ENABLEGROUPVIEW
105 * -- LVM_GETBKIMAGE, LVM_SETBKIMAGE
106 * -- LVM_GETGROUPINFO, LVM_SETGROUPINFO
107 * -- LVM_GETGROUPMETRICS, LVM_SETGROUPMETRICS
108 * -- LVM_GETINSERTMARK, LVM_SETINSERTMARK
109 * -- LVM_GETINSERTMARKCOLOR, LVM_SETINSERTMARKCOLOR
110 * -- LVM_GETINSERTMARKRECT
111 * -- LVM_GETNUMBEROFWORKAREAS
112 * -- LVM_GETOUTLINECOLOR, LVM_SETOUTLINECOLOR
113 * -- LVM_GETSELECTEDCOLUMN, LVM_SETSELECTEDCOLUMN
114 * -- LVM_GETISEARCHSTRINGW, LVM_GETISEARCHSTRINGA
115 * -- LVM_GETTILEINFO, LVM_SETTILEINFO
116 * -- LVM_GETTILEVIEWINFO, LVM_SETTILEVIEWINFO
117 * -- LVM_GETWORKAREAS, LVM_SETWORKAREAS
118 * -- LVM_HASGROUP, LVM_INSERTGROUP, LVM_REMOVEGROUP, LVM_REMOVEALLGROUPS
119 * -- LVM_INSERTGROUPSORTED
120 * -- LVM_INSERTMARKHITTEST
121 * -- LVM_ISGROUPVIEWENABLED
123 * -- LVM_MOVEITEMTOGROUP
125 * -- LVM_SETTILEWIDTH
129 * -- ListView_GetHoverTime, ListView_SetHoverTime
130 * -- ListView_GetISearchString
131 * -- ListView_GetNumberOfWorkAreas
132 * -- ListView_GetWorkAreas, ListView_SetWorkAreas
137 * Known differences in message stream from native control (not known if
138 * these differences cause problems):
139 * LVM_INSERTITEM issues LVM_SETITEMSTATE and LVM_SETITEM in certain cases.
140 * LVM_SETITEM does not always issue LVN_ITEMCHANGING/LVN_ITEMCHANGED.
141 * WM_CREATE does not issue WM_QUERYUISTATE and associated registry
142 * processing for "USEDOUBLECLICKTIME".
146 #include "wine/port.h"
161 #include "commctrl.h"
162 #include "comctl32.h"
165 #include "wine/debug.h"
166 #include "wine/unicode.h"
168 WINE_DEFAULT_DEBUG_CHANNEL(listview);
170 typedef struct tagCOLUMN_INFO
172 RECT rcHeader; /* tracks the header's rectangle */
173 INT fmt; /* same as LVCOLUMN.fmt */
177 typedef struct tagITEMHDR
181 } ITEMHDR, *LPITEMHDR;
183 typedef struct tagSUBITEM_INFO
189 typedef struct tagITEM_ID ITEM_ID;
191 typedef struct tagITEM_INFO
202 UINT id; /* item id */
203 HDPA item; /* link to item data */
206 typedef struct tagRANGE
212 typedef struct tagRANGES
217 typedef struct tagITERATOR
226 typedef struct tagDELAYED_ITEM_EDIT
232 typedef struct tagLISTVIEW_INFO
236 RECT rcList; /* This rectangle is really the window
237 * client rectangle possibly reduced by the
238 * horizontal scroll bar and/or header - see
239 * LISTVIEW_UpdateSize. This rectangle offset
240 * by the LISTVIEW_GetOrigin value is in
241 * client coordinates */
243 /* notification window */
246 BOOL bDoChangeNotify; /* send change notification messages? */
253 INT nItemCount; /* the number of items in the list */
254 HDPA hdpaItems; /* array ITEM_INFO pointers */
255 HDPA hdpaItemIds; /* array of ITEM_ID pointers */
256 HDPA hdpaPosX; /* maintains the (X, Y) coordinates of the */
257 HDPA hdpaPosY; /* items in LVS_ICON, and LVS_SMALLICON modes */
258 RANGES selectionRanges;
259 INT nSelectionMark; /* item to start next multiselection from */
261 BOOL bAutoarrange; /* Autoarrange flag when NOT in LVS_AUTOARRANGE */
264 HDPA hdpaColumns; /* array of COLUMN_INFO pointers */
265 BOOL colRectsDirty; /* trigger column rectangles requery from header */
268 BOOL bNoItemMetrics; /* flags if item metrics are not yet computed */
273 PFNLVCOMPARE pfnCompare; /* sorting callback pointer */
277 DWORD dwStyle; /* the cached window GWL_STYLE */
278 DWORD dwLvExStyle; /* extended listview style */
279 DWORD uView; /* current view available through LVM_[G,S]ETVIEW */
285 DELAYED_ITEM_EDIT itemEdit; /* Pointer to this structure will be the timer ID */
288 HIMAGELIST himlNormal;
289 HIMAGELIST himlSmall;
290 HIMAGELIST himlState;
295 POINT currIconPos; /* this is the position next icon will be placed */
299 INT xTrackLine; /* The x coefficient of the track line or -1 if none */
301 /* marquee selection */
302 BOOL bMarqueeSelect; /* marquee selection/highlight underway */
304 RECT marqueeRect; /* absolute coordinates of marquee selection */
305 RECT marqueeDrawRect; /* relative coordinates for drawing marquee */
306 POINT marqueeOrigin; /* absolute coordinates of marquee click origin */
309 BOOL bFocus; /* control has focus */
311 RECT rcFocus; /* focus bounds */
322 INT ntmHeight; /* Some cached metrics of the font used */
323 INT ntmMaxCharWidth; /* by the listview to draw items */
326 /* mouse operation */
329 POINT ptClickPos; /* point where the user clicked */
330 INT nLButtonDownItem; /* tracks item to reset multiselection on WM_LBUTTONUP */
335 /* keyboard operation */
336 DWORD lastKeyPressTimestamp;
338 INT nSearchParamLength;
339 WCHAR szSearchParam[ MAX_PATH ];
342 DWORD cditemmode; /* Keep the custom draw flags for an item/row */
343 BOOL bIsDrawing; /* Drawing in progress */
344 INT nMeasureItemHeight; /* WM_MEASUREITEM result */
345 BOOL bRedraw; /* WM_SETREDRAW switch */
348 DWORD iVersion; /* CCM_[G,S]ETVERSION */
354 /* How many we debug buffer to allocate */
355 #define DEBUG_BUFFERS 20
356 /* The size of a single debug buffer */
357 #define DEBUG_BUFFER_SIZE 256
359 /* Internal interface to LISTVIEW_HScroll and LISTVIEW_VScroll */
360 #define SB_INTERNAL -1
362 /* maximum size of a label */
363 #define DISP_TEXT_SIZE 260
365 /* padding for items in list and small icon display modes */
366 #define WIDTH_PADDING 12
368 /* padding for items in list, report and small icon display modes */
369 #define HEIGHT_PADDING 1
371 /* offset of items in report display mode */
372 #define REPORT_MARGINX 2
374 /* padding for icon in large icon display mode
375 * ICON_TOP_PADDING_NOTHITABLE - space between top of box and area
376 * that HITTEST will see.
377 * ICON_TOP_PADDING_HITABLE - spacing between above and icon.
378 * ICON_TOP_PADDING - sum of the two above.
379 * ICON_BOTTOM_PADDING - between bottom of icon and top of text
380 * LABEL_HOR_PADDING - between text and sides of box
381 * LABEL_VERT_PADDING - between bottom of text and end of box
383 * ICON_LR_PADDING - additional width above icon size.
384 * ICON_LR_HALF - half of the above value
386 #define ICON_TOP_PADDING_NOTHITABLE 2
387 #define ICON_TOP_PADDING_HITABLE 2
388 #define ICON_TOP_PADDING (ICON_TOP_PADDING_NOTHITABLE + ICON_TOP_PADDING_HITABLE)
389 #define ICON_BOTTOM_PADDING 4
390 #define LABEL_HOR_PADDING 5
391 #define LABEL_VERT_PADDING 7
392 #define ICON_LR_PADDING 16
393 #define ICON_LR_HALF (ICON_LR_PADDING/2)
395 /* default label width for items in list and small icon display modes */
396 #define DEFAULT_LABEL_WIDTH 40
397 /* maximum select rectangle width for empty text item in LV_VIEW_DETAILS */
398 #define MAX_EMPTYTEXT_SELECT_WIDTH 80
400 /* default column width for items in list display mode */
401 #define DEFAULT_COLUMN_WIDTH 128
403 /* Size of "line" scroll for V & H scrolls */
404 #define LISTVIEW_SCROLL_ICON_LINE_SIZE 37
406 /* Padding between image and label */
407 #define IMAGE_PADDING 2
409 /* Padding behind the label */
410 #define TRAILING_LABEL_PADDING 12
411 #define TRAILING_HEADER_PADDING 11
413 /* Border for the icon caption */
414 #define CAPTION_BORDER 2
416 /* Standard DrawText flags */
417 #define LV_ML_DT_FLAGS (DT_TOP | DT_NOPREFIX | DT_EDITCONTROL | DT_CENTER | DT_WORDBREAK | DT_WORD_ELLIPSIS | DT_END_ELLIPSIS)
418 #define LV_FL_DT_FLAGS (DT_TOP | DT_NOPREFIX | DT_EDITCONTROL | DT_CENTER | DT_WORDBREAK | DT_NOCLIP)
419 #define LV_SL_DT_FLAGS (DT_VCENTER | DT_NOPREFIX | DT_EDITCONTROL | DT_SINGLELINE | DT_WORD_ELLIPSIS | DT_END_ELLIPSIS)
421 /* Image index from state */
422 #define STATEIMAGEINDEX(x) (((x) & LVIS_STATEIMAGEMASK) >> 12)
424 /* The time in milliseconds to reset the search in the list */
425 #define KEY_DELAY 450
427 /* Dump the LISTVIEW_INFO structure to the debug channel */
428 #define LISTVIEW_DUMP(iP) do { \
429 TRACE("hwndSelf=%p, clrBk=0x%06x, clrText=0x%06x, clrTextBk=0x%06x, ItemHeight=%d, ItemWidth=%d, Style=0x%08x\n", \
430 iP->hwndSelf, iP->clrBk, iP->clrText, iP->clrTextBk, \
431 iP->nItemHeight, iP->nItemWidth, iP->dwStyle); \
432 TRACE("hwndSelf=%p, himlNor=%p, himlSml=%p, himlState=%p, Focused=%d, Hot=%d, exStyle=0x%08x, Focus=%d\n", \
433 iP->hwndSelf, iP->himlNormal, iP->himlSmall, iP->himlState, \
434 iP->nFocusedItem, iP->nHotItem, iP->dwLvExStyle, iP->bFocus ); \
435 TRACE("hwndSelf=%p, ntmH=%d, icSz.cx=%d, icSz.cy=%d, icSp.cx=%d, icSp.cy=%d, notifyFmt=%d\n", \
436 iP->hwndSelf, iP->ntmHeight, iP->iconSize.cx, iP->iconSize.cy, \
437 iP->iconSpacing.cx, iP->iconSpacing.cy, iP->notifyFormat); \
438 TRACE("hwndSelf=%p, rcList=%s\n", iP->hwndSelf, wine_dbgstr_rect(&iP->rcList)); \
441 static const WCHAR themeClass[] = {'L','i','s','t','V','i','e','w',0};
444 * forward declarations
446 static BOOL LISTVIEW_GetItemT(const LISTVIEW_INFO *, LPLVITEMW, BOOL);
447 static void LISTVIEW_GetItemBox(const LISTVIEW_INFO *, INT, LPRECT);
448 static void LISTVIEW_GetItemOrigin(const LISTVIEW_INFO *, INT, LPPOINT);
449 static BOOL LISTVIEW_GetItemPosition(const LISTVIEW_INFO *, INT, LPPOINT);
450 static BOOL LISTVIEW_GetItemRect(const LISTVIEW_INFO *, INT, LPRECT);
451 static void LISTVIEW_GetOrigin(const LISTVIEW_INFO *, LPPOINT);
452 static BOOL LISTVIEW_GetViewRect(const LISTVIEW_INFO *, LPRECT);
453 static void LISTVIEW_UpdateSize(LISTVIEW_INFO *);
454 static LRESULT LISTVIEW_Command(LISTVIEW_INFO *, WPARAM, LPARAM);
455 static INT LISTVIEW_GetStringWidthT(const LISTVIEW_INFO *, LPCWSTR, BOOL);
456 static BOOL LISTVIEW_KeySelection(LISTVIEW_INFO *, INT, BOOL);
457 static UINT LISTVIEW_GetItemState(const LISTVIEW_INFO *, INT, UINT);
458 static BOOL LISTVIEW_SetItemState(LISTVIEW_INFO *, INT, const LVITEMW *);
459 static LRESULT LISTVIEW_VScroll(LISTVIEW_INFO *, INT, INT);
460 static LRESULT LISTVIEW_HScroll(LISTVIEW_INFO *, INT, INT);
461 static BOOL LISTVIEW_EnsureVisible(LISTVIEW_INFO *, INT, BOOL);
462 static HIMAGELIST LISTVIEW_SetImageList(LISTVIEW_INFO *, INT, HIMAGELIST);
463 static INT LISTVIEW_HitTest(const LISTVIEW_INFO *, LPLVHITTESTINFO, BOOL, BOOL);
464 static BOOL LISTVIEW_EndEditLabelT(LISTVIEW_INFO *, BOOL, BOOL);
465 static BOOL LISTVIEW_Scroll(LISTVIEW_INFO *, INT, INT);
467 /******** Text handling functions *************************************/
469 /* A text pointer is either NULL, LPSTR_TEXTCALLBACK, or points to a
470 * text string. The string may be ANSI or Unicode, in which case
471 * the boolean isW tells us the type of the string.
473 * The name of the function tell what type of strings it expects:
474 * W: Unicode, T: ANSI/Unicode - function of isW
477 static inline BOOL is_text(LPCWSTR text)
479 return text != NULL && text != LPSTR_TEXTCALLBACKW;
482 static inline int textlenT(LPCWSTR text, BOOL isW)
484 return !is_text(text) ? 0 :
485 isW ? lstrlenW(text) : lstrlenA((LPCSTR)text);
488 static inline void textcpynT(LPWSTR dest, BOOL isDestW, LPCWSTR src, BOOL isSrcW, INT max)
491 if (isSrcW) lstrcpynW(dest, src, max);
492 else MultiByteToWideChar(CP_ACP, 0, (LPCSTR)src, -1, dest, max);
494 if (isSrcW) WideCharToMultiByte(CP_ACP, 0, src, -1, (LPSTR)dest, max, NULL, NULL);
495 else lstrcpynA((LPSTR)dest, (LPCSTR)src, max);
498 static inline LPWSTR textdupTtoW(LPCWSTR text, BOOL isW)
500 LPWSTR wstr = (LPWSTR)text;
502 if (!isW && is_text(text))
504 INT len = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)text, -1, NULL, 0);
505 wstr = Alloc(len * sizeof(WCHAR));
506 if (wstr) MultiByteToWideChar(CP_ACP, 0, (LPCSTR)text, -1, wstr, len);
508 TRACE(" wstr=%s\n", text == LPSTR_TEXTCALLBACKW ? "(callback)" : debugstr_w(wstr));
512 static inline void textfreeT(LPWSTR wstr, BOOL isW)
514 if (!isW && is_text(wstr)) Free (wstr);
518 * dest is a pointer to a Unicode string
519 * src is a pointer to a string (Unicode if isW, ANSI if !isW)
521 static BOOL textsetptrT(LPWSTR *dest, LPCWSTR src, BOOL isW)
525 if (src == LPSTR_TEXTCALLBACKW)
527 if (is_text(*dest)) Free(*dest);
528 *dest = LPSTR_TEXTCALLBACKW;
532 LPWSTR pszText = textdupTtoW(src, isW);
533 if (*dest == LPSTR_TEXTCALLBACKW) *dest = NULL;
534 bResult = Str_SetPtrW(dest, pszText);
535 textfreeT(pszText, isW);
541 * compares a Unicode to a Unicode/ANSI text string
543 static inline int textcmpWT(LPCWSTR aw, LPCWSTR bt, BOOL isW)
545 if (!aw) return bt ? -1 : 0;
547 if (aw == LPSTR_TEXTCALLBACKW)
548 return bt == LPSTR_TEXTCALLBACKW ? 1 : -1;
549 if (bt != LPSTR_TEXTCALLBACKW)
551 LPWSTR bw = textdupTtoW(bt, isW);
552 int r = bw ? lstrcmpW(aw, bw) : 1;
560 static inline int lstrncmpiW(LPCWSTR s1, LPCWSTR s2, int n)
562 n = min(min(n, lstrlenW(s1)), lstrlenW(s2));
563 return CompareStringW(LOCALE_USER_DEFAULT, NORM_IGNORECASE, s1, n, s2, n) - CSTR_EQUAL;
566 /******** Debugging functions *****************************************/
568 static inline LPCSTR debugtext_t(LPCWSTR text, BOOL isW)
570 if (text == LPSTR_TEXTCALLBACKW) return "(callback)";
571 return isW ? debugstr_w(text) : debugstr_a((LPCSTR)text);
574 static inline LPCSTR debugtext_tn(LPCWSTR text, BOOL isW, INT n)
576 if (text == LPSTR_TEXTCALLBACKW) return "(callback)";
577 n = min(textlenT(text, isW), n);
578 return isW ? debugstr_wn(text, n) : debugstr_an((LPCSTR)text, n);
581 static char* debug_getbuf(void)
583 static int index = 0;
584 static char buffers[DEBUG_BUFFERS][DEBUG_BUFFER_SIZE];
585 return buffers[index++ % DEBUG_BUFFERS];
588 static inline const char* debugrange(const RANGE *lprng)
590 if (!lprng) return "(null)";
591 return wine_dbg_sprintf("[%d, %d]", lprng->lower, lprng->upper);
594 static const char* debugscrollinfo(const SCROLLINFO *pScrollInfo)
596 char* buf = debug_getbuf(), *text = buf;
597 int len, size = DEBUG_BUFFER_SIZE;
599 if (pScrollInfo == NULL) return "(null)";
600 len = snprintf(buf, size, "{cbSize=%d, ", pScrollInfo->cbSize);
601 if (len == -1) goto end; buf += len; size -= len;
602 if (pScrollInfo->fMask & SIF_RANGE)
603 len = snprintf(buf, size, "nMin=%d, nMax=%d, ", pScrollInfo->nMin, pScrollInfo->nMax);
605 if (len == -1) goto end; buf += len; size -= len;
606 if (pScrollInfo->fMask & SIF_PAGE)
607 len = snprintf(buf, size, "nPage=%u, ", pScrollInfo->nPage);
609 if (len == -1) goto end; buf += len; size -= len;
610 if (pScrollInfo->fMask & SIF_POS)
611 len = snprintf(buf, size, "nPos=%d, ", pScrollInfo->nPos);
613 if (len == -1) goto end; buf += len; size -= len;
614 if (pScrollInfo->fMask & SIF_TRACKPOS)
615 len = snprintf(buf, size, "nTrackPos=%d, ", pScrollInfo->nTrackPos);
617 if (len == -1) goto end; buf += len;
620 buf = text + strlen(text);
622 if (buf - text > 2) { buf[-2] = '}'; buf[-1] = 0; }
626 static const char* debugnmlistview(const NMLISTVIEW *plvnm)
628 if (!plvnm) return "(null)";
629 return wine_dbg_sprintf("iItem=%d, iSubItem=%d, uNewState=0x%x,"
630 " uOldState=0x%x, uChanged=0x%x, ptAction=%s, lParam=%ld",
631 plvnm->iItem, plvnm->iSubItem, plvnm->uNewState, plvnm->uOldState,
632 plvnm->uChanged, wine_dbgstr_point(&plvnm->ptAction), plvnm->lParam);
635 static const char* debuglvitem_t(const LVITEMW *lpLVItem, BOOL isW)
637 char* buf = debug_getbuf(), *text = buf;
638 int len, size = DEBUG_BUFFER_SIZE;
640 if (lpLVItem == NULL) return "(null)";
641 len = snprintf(buf, size, "{iItem=%d, iSubItem=%d, ", lpLVItem->iItem, lpLVItem->iSubItem);
642 if (len == -1) goto end; buf += len; size -= len;
643 if (lpLVItem->mask & LVIF_STATE)
644 len = snprintf(buf, size, "state=%x, stateMask=%x, ", lpLVItem->state, lpLVItem->stateMask);
646 if (len == -1) goto end; buf += len; size -= len;
647 if (lpLVItem->mask & LVIF_TEXT)
648 len = snprintf(buf, size, "pszText=%s, cchTextMax=%d, ", debugtext_tn(lpLVItem->pszText, isW, 80), lpLVItem->cchTextMax);
650 if (len == -1) goto end; buf += len; size -= len;
651 if (lpLVItem->mask & LVIF_IMAGE)
652 len = snprintf(buf, size, "iImage=%d, ", lpLVItem->iImage);
654 if (len == -1) goto end; buf += len; size -= len;
655 if (lpLVItem->mask & LVIF_PARAM)
656 len = snprintf(buf, size, "lParam=%lx, ", lpLVItem->lParam);
658 if (len == -1) goto end; buf += len; size -= len;
659 if (lpLVItem->mask & LVIF_INDENT)
660 len = snprintf(buf, size, "iIndent=%d, ", lpLVItem->iIndent);
662 if (len == -1) goto end; buf += len;
665 buf = text + strlen(text);
667 if (buf - text > 2) { buf[-2] = '}'; buf[-1] = 0; }
671 static const char* debuglvcolumn_t(const LVCOLUMNW *lpColumn, BOOL isW)
673 char* buf = debug_getbuf(), *text = buf;
674 int len, size = DEBUG_BUFFER_SIZE;
676 if (lpColumn == NULL) return "(null)";
677 len = snprintf(buf, size, "{");
678 if (len == -1) goto end; buf += len; size -= len;
679 if (lpColumn->mask & LVCF_SUBITEM)
680 len = snprintf(buf, size, "iSubItem=%d, ", lpColumn->iSubItem);
682 if (len == -1) goto end; buf += len; size -= len;
683 if (lpColumn->mask & LVCF_FMT)
684 len = snprintf(buf, size, "fmt=%x, ", lpColumn->fmt);
686 if (len == -1) goto end; buf += len; size -= len;
687 if (lpColumn->mask & LVCF_WIDTH)
688 len = snprintf(buf, size, "cx=%d, ", lpColumn->cx);
690 if (len == -1) goto end; buf += len; size -= len;
691 if (lpColumn->mask & LVCF_TEXT)
692 len = snprintf(buf, size, "pszText=%s, cchTextMax=%d, ", debugtext_tn(lpColumn->pszText, isW, 80), lpColumn->cchTextMax);
694 if (len == -1) goto end; buf += len; size -= len;
695 if (lpColumn->mask & LVCF_IMAGE)
696 len = snprintf(buf, size, "iImage=%d, ", lpColumn->iImage);
698 if (len == -1) goto end; buf += len; size -= len;
699 if (lpColumn->mask & LVCF_ORDER)
700 len = snprintf(buf, size, "iOrder=%d, ", lpColumn->iOrder);
702 if (len == -1) goto end; buf += len;
705 buf = text + strlen(text);
707 if (buf - text > 2) { buf[-2] = '}'; buf[-1] = 0; }
711 static const char* debuglvhittestinfo(const LVHITTESTINFO *lpht)
713 if (!lpht) return "(null)";
715 return wine_dbg_sprintf("{pt=%s, flags=0x%x, iItem=%d, iSubItem=%d}",
716 wine_dbgstr_point(&lpht->pt), lpht->flags, lpht->iItem, lpht->iSubItem);
719 /* Return the corresponding text for a given scroll value */
720 static inline LPCSTR debugscrollcode(int nScrollCode)
724 case SB_LINELEFT: return "SB_LINELEFT";
725 case SB_LINERIGHT: return "SB_LINERIGHT";
726 case SB_PAGELEFT: return "SB_PAGELEFT";
727 case SB_PAGERIGHT: return "SB_PAGERIGHT";
728 case SB_THUMBPOSITION: return "SB_THUMBPOSITION";
729 case SB_THUMBTRACK: return "SB_THUMBTRACK";
730 case SB_ENDSCROLL: return "SB_ENDSCROLL";
731 case SB_INTERNAL: return "SB_INTERNAL";
732 default: return "unknown";
737 /******** Notification functions ************************************/
739 static int get_ansi_notification(UINT unicodeNotificationCode)
741 switch (unicodeNotificationCode)
743 case LVN_BEGINLABELEDITA:
744 case LVN_BEGINLABELEDITW: return LVN_BEGINLABELEDITA;
745 case LVN_ENDLABELEDITA:
746 case LVN_ENDLABELEDITW: return LVN_ENDLABELEDITA;
747 case LVN_GETDISPINFOA:
748 case LVN_GETDISPINFOW: return LVN_GETDISPINFOA;
749 case LVN_SETDISPINFOA:
750 case LVN_SETDISPINFOW: return LVN_SETDISPINFOA;
751 case LVN_ODFINDITEMA:
752 case LVN_ODFINDITEMW: return LVN_ODFINDITEMA;
753 case LVN_GETINFOTIPA:
754 case LVN_GETINFOTIPW: return LVN_GETINFOTIPA;
755 /* header forwards */
757 case HDN_TRACKW: return HDN_TRACKA;
759 case HDN_ENDTRACKW: return HDN_ENDTRACKA;
760 case HDN_BEGINDRAG: return HDN_BEGINDRAG;
761 case HDN_ENDDRAG: return HDN_ENDDRAG;
762 case HDN_ITEMCHANGINGA:
763 case HDN_ITEMCHANGINGW: return HDN_ITEMCHANGINGA;
764 case HDN_ITEMCHANGEDA:
765 case HDN_ITEMCHANGEDW: return HDN_ITEMCHANGEDA;
767 case HDN_ITEMCLICKW: return HDN_ITEMCLICKA;
768 case HDN_DIVIDERDBLCLICKA:
769 case HDN_DIVIDERDBLCLICKW: return HDN_DIVIDERDBLCLICKA;
772 FIXME("unknown notification %x\n", unicodeNotificationCode);
773 return unicodeNotificationCode;
776 /* forwards header notifications to listview parent */
777 static LRESULT notify_forward_header(const LISTVIEW_INFO *infoPtr, NMHEADERW *lpnmhW)
779 LPCWSTR text = NULL, filter = NULL;
781 NMHEADERA *lpnmh = (NMHEADERA*) lpnmhW;
783 /* on unicode format exit earlier */
784 if (infoPtr->notifyFormat == NFR_UNICODE)
785 return SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, lpnmh->hdr.idFrom,
788 /* header always supplies unicode notifications,
789 all we have to do is to convert strings to ANSI */
792 /* convert item text */
793 if (lpnmh->pitem->mask & HDI_TEXT)
795 text = (LPCWSTR)lpnmh->pitem->pszText;
796 Str_SetPtrWtoA(&lpnmh->pitem->pszText, text);
798 /* convert filter text */
799 if ((lpnmh->pitem->mask & HDI_FILTER) && (lpnmh->pitem->type == HDFT_ISSTRING) &&
800 lpnmh->pitem->pvFilter)
802 filter = (LPCWSTR)((HD_TEXTFILTERA*)lpnmh->pitem->pvFilter)->pszText;
803 Str_SetPtrWtoA(&((HD_TEXTFILTERA*)lpnmh->pitem->pvFilter)->pszText, filter);
806 lpnmh->hdr.code = get_ansi_notification(lpnmh->hdr.code);
808 ret = SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, lpnmh->hdr.idFrom,
814 Free(lpnmh->pitem->pszText);
815 lpnmh->pitem->pszText = (LPSTR)text;
819 Free(((HD_TEXTFILTERA*)lpnmh->pitem->pvFilter)->pszText);
820 ((HD_TEXTFILTERA*)lpnmh->pitem->pvFilter)->pszText = (LPSTR)filter;
826 static LRESULT notify_hdr(const LISTVIEW_INFO *infoPtr, INT code, LPNMHDR pnmh)
830 TRACE("(code=%d)\n", code);
832 pnmh->hwndFrom = infoPtr->hwndSelf;
833 pnmh->idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
835 result = SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, pnmh->idFrom, (LPARAM)pnmh);
837 TRACE(" <= %ld\n", result);
842 static inline BOOL notify(const LISTVIEW_INFO *infoPtr, INT code)
845 HWND hwnd = infoPtr->hwndSelf;
846 notify_hdr(infoPtr, code, &nmh);
847 return IsWindow(hwnd);
850 static inline void notify_itemactivate(const LISTVIEW_INFO *infoPtr, const LVHITTESTINFO *htInfo)
861 item.mask = LVIF_PARAM|LVIF_STATE;
862 item.iItem = htInfo->iItem;
864 item.stateMask = (UINT)-1;
865 if (LISTVIEW_GetItemT(infoPtr, &item, TRUE)) {
866 nmia.lParam = item.lParam;
867 nmia.uOldState = item.state;
868 nmia.uNewState = item.state | LVIS_ACTIVATING;
869 nmia.uChanged = LVIF_STATE;
872 nmia.iItem = htInfo->iItem;
873 nmia.iSubItem = htInfo->iSubItem;
874 nmia.ptAction = htInfo->pt;
876 if (GetKeyState(VK_SHIFT) & 0x8000) nmia.uKeyFlags |= LVKF_SHIFT;
877 if (GetKeyState(VK_CONTROL) & 0x8000) nmia.uKeyFlags |= LVKF_CONTROL;
878 if (GetKeyState(VK_MENU) & 0x8000) nmia.uKeyFlags |= LVKF_ALT;
880 notify_hdr(infoPtr, LVN_ITEMACTIVATE, (LPNMHDR)&nmia);
883 static inline LRESULT notify_listview(const LISTVIEW_INFO *infoPtr, INT code, LPNMLISTVIEW plvnm)
885 TRACE("(code=%d, plvnm=%s)\n", code, debugnmlistview(plvnm));
886 return notify_hdr(infoPtr, code, (LPNMHDR)plvnm);
889 /* Handles NM_DBLCLK, NM_CLICK, NM_RDBLCLK, NM_RCLICK. Only NM_RCLICK return value is used. */
890 static BOOL notify_click(const LISTVIEW_INFO *infoPtr, INT code, const LVHITTESTINFO *lvht)
894 HWND hwnd = infoPtr->hwndSelf;
897 TRACE("code=%d, lvht=%s\n", code, debuglvhittestinfo(lvht));
898 ZeroMemory(&nmia, sizeof(nmia));
899 nmia.iItem = lvht->iItem;
900 nmia.iSubItem = lvht->iSubItem;
901 nmia.ptAction = lvht->pt;
902 item.mask = LVIF_PARAM;
903 item.iItem = lvht->iItem;
905 if (LISTVIEW_GetItemT(infoPtr, &item, TRUE)) nmia.lParam = item.lParam;
906 ret = notify_hdr(infoPtr, code, (NMHDR*)&nmia);
907 return IsWindow(hwnd) && (code == NM_RCLICK ? !ret : TRUE);
910 static BOOL notify_deleteitem(const LISTVIEW_INFO *infoPtr, INT nItem)
914 HWND hwnd = infoPtr->hwndSelf;
916 ZeroMemory(&nmlv, sizeof (NMLISTVIEW));
918 item.mask = LVIF_PARAM;
921 if (LISTVIEW_GetItemT(infoPtr, &item, TRUE)) nmlv.lParam = item.lParam;
922 notify_listview(infoPtr, LVN_DELETEITEM, &nmlv);
923 return IsWindow(hwnd);
927 Send notification. depends on dispinfoW having same
928 structure as dispinfoA.
929 infoPtr : listview struct
930 code : *Unicode* notification code
931 pdi : dispinfo structure (can be unicode or ansi)
932 isW : TRUE if dispinfo is Unicode
934 static BOOL notify_dispinfoT(const LISTVIEW_INFO *infoPtr, UINT code, LPNMLVDISPINFOW pdi, BOOL isW)
936 INT length = 0, ret_length;
937 LPWSTR buffer = NULL, ret_text;
938 BOOL return_ansi = FALSE;
939 BOOL return_unicode = FALSE;
942 if ((pdi->item.mask & LVIF_TEXT) && is_text(pdi->item.pszText))
944 return_unicode = ( isW && infoPtr->notifyFormat == NFR_ANSI);
945 return_ansi = (!isW && infoPtr->notifyFormat == NFR_UNICODE);
948 ret_length = pdi->item.cchTextMax;
949 ret_text = pdi->item.pszText;
951 if (return_unicode || return_ansi)
953 if (code != LVN_GETDISPINFOW)
955 length = return_ansi ?
956 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pdi->item.pszText, -1, NULL, 0):
957 WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, NULL, 0, NULL, NULL);
961 length = pdi->item.cchTextMax;
962 *pdi->item.pszText = 0; /* make sure we don't process garbage */
965 buffer = Alloc( (return_ansi ? sizeof(WCHAR) : sizeof(CHAR)) * length);
966 if (!buffer) return FALSE;
969 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pdi->item.pszText, -1,
972 WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, (LPSTR) buffer,
975 pdi->item.pszText = buffer;
976 pdi->item.cchTextMax = length;
979 if (infoPtr->notifyFormat == NFR_ANSI)
980 code = get_ansi_notification(code);
982 TRACE(" pdi->item=%s\n", debuglvitem_t(&pdi->item, infoPtr->notifyFormat != NFR_ANSI));
983 ret = notify_hdr(infoPtr, code, &pdi->hdr);
984 TRACE(" resulting code=%d\n", pdi->hdr.code);
986 if (return_ansi || return_unicode)
988 if (return_ansi && (pdi->hdr.code == LVN_GETDISPINFOA))
990 strcpy((char*)ret_text, (char*)pdi->item.pszText);
992 else if (return_unicode && (pdi->hdr.code == LVN_GETDISPINFOW))
994 strcpyW(ret_text, pdi->item.pszText);
996 else if (return_ansi) /* note : pointer can be changed by app ! */
998 WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, (LPSTR) ret_text,
999 ret_length, NULL, NULL);
1002 MultiByteToWideChar(CP_ACP, 0, (LPSTR) pdi->item.pszText, -1,
1003 ret_text, ret_length);
1005 pdi->item.pszText = ret_text; /* restores our buffer */
1006 pdi->item.cchTextMax = ret_length;
1012 /* if dipsinfo holder changed notification code then convert */
1013 if (!isW && (pdi->hdr.code == LVN_GETDISPINFOW) && (pdi->item.mask & LVIF_TEXT))
1015 length = WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, NULL, 0, NULL, NULL);
1017 buffer = Alloc(length * sizeof(CHAR));
1018 if (!buffer) return FALSE;
1020 WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, (LPSTR) buffer,
1021 ret_length, NULL, NULL);
1023 strcpy((LPSTR)pdi->item.pszText, (LPSTR)buffer);
1030 static void customdraw_fill(NMLVCUSTOMDRAW *lpnmlvcd, const LISTVIEW_INFO *infoPtr, HDC hdc,
1031 const RECT *rcBounds, const LVITEMW *lplvItem)
1033 ZeroMemory(lpnmlvcd, sizeof(NMLVCUSTOMDRAW));
1034 lpnmlvcd->nmcd.hdc = hdc;
1035 lpnmlvcd->nmcd.rc = *rcBounds;
1036 lpnmlvcd->clrTextBk = infoPtr->clrTextBk;
1037 lpnmlvcd->clrText = infoPtr->clrText;
1038 if (!lplvItem) return;
1039 lpnmlvcd->nmcd.dwItemSpec = lplvItem->iItem + 1;
1040 lpnmlvcd->iSubItem = lplvItem->iSubItem;
1041 if (lplvItem->state & LVIS_SELECTED) lpnmlvcd->nmcd.uItemState |= CDIS_SELECTED;
1042 if (lplvItem->state & LVIS_FOCUSED) lpnmlvcd->nmcd.uItemState |= CDIS_FOCUS;
1043 if (lplvItem->iItem == infoPtr->nHotItem) lpnmlvcd->nmcd.uItemState |= CDIS_HOT;
1044 lpnmlvcd->nmcd.lItemlParam = lplvItem->lParam;
1047 static inline DWORD notify_customdraw (const LISTVIEW_INFO *infoPtr, DWORD dwDrawStage, NMLVCUSTOMDRAW *lpnmlvcd)
1049 BOOL isForItem = (lpnmlvcd->nmcd.dwItemSpec != 0);
1052 lpnmlvcd->nmcd.dwDrawStage = dwDrawStage;
1053 if (isForItem) lpnmlvcd->nmcd.dwDrawStage |= CDDS_ITEM;
1054 if (lpnmlvcd->iSubItem) lpnmlvcd->nmcd.dwDrawStage |= CDDS_SUBITEM;
1055 if (isForItem) lpnmlvcd->nmcd.dwItemSpec--;
1056 result = notify_hdr(infoPtr, NM_CUSTOMDRAW, &lpnmlvcd->nmcd.hdr);
1057 if (isForItem) lpnmlvcd->nmcd.dwItemSpec++;
1061 static void prepaint_setup (const LISTVIEW_INFO *infoPtr, HDC hdc, NMLVCUSTOMDRAW *lpnmlvcd, BOOL SubItem)
1063 if (lpnmlvcd->clrTextBk == CLR_DEFAULT)
1064 lpnmlvcd->clrTextBk = comctl32_color.clrWindow;
1065 if (lpnmlvcd->clrText == CLR_DEFAULT)
1066 lpnmlvcd->clrText = comctl32_color.clrWindowText;
1068 /* apparently, for selected items, we have to override the returned values */
1071 if (lpnmlvcd->nmcd.uItemState & CDIS_SELECTED)
1073 if (infoPtr->bFocus)
1075 lpnmlvcd->clrTextBk = comctl32_color.clrHighlight;
1076 lpnmlvcd->clrText = comctl32_color.clrHighlightText;
1078 else if (infoPtr->dwStyle & LVS_SHOWSELALWAYS)
1080 lpnmlvcd->clrTextBk = comctl32_color.clr3dFace;
1081 lpnmlvcd->clrText = comctl32_color.clrBtnText;
1086 /* Set the text attributes */
1087 if (lpnmlvcd->clrTextBk != CLR_NONE)
1089 SetBkMode(hdc, OPAQUE);
1090 SetBkColor(hdc,lpnmlvcd->clrTextBk);
1093 SetBkMode(hdc, TRANSPARENT);
1094 SetTextColor(hdc, lpnmlvcd->clrText);
1097 static inline DWORD notify_postpaint (const LISTVIEW_INFO *infoPtr, NMLVCUSTOMDRAW *lpnmlvcd)
1099 return notify_customdraw(infoPtr, CDDS_POSTPAINT, lpnmlvcd);
1102 /* returns TRUE when repaint needed, FALSE otherwise */
1103 static BOOL notify_measureitem(LISTVIEW_INFO *infoPtr)
1105 MEASUREITEMSTRUCT mis;
1106 mis.CtlType = ODT_LISTVIEW;
1107 mis.CtlID = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1111 mis.itemHeight= infoPtr->nItemHeight;
1112 SendMessageW(infoPtr->hwndNotify, WM_MEASUREITEM, mis.CtlID, (LPARAM)&mis);
1113 if (infoPtr->nItemHeight != max(mis.itemHeight, 1))
1115 infoPtr->nMeasureItemHeight = infoPtr->nItemHeight = max(mis.itemHeight, 1);
1121 /******** Item iterator functions **********************************/
1123 static RANGES ranges_create(int count);
1124 static void ranges_destroy(RANGES ranges);
1125 static BOOL ranges_add(RANGES ranges, RANGE range);
1126 static BOOL ranges_del(RANGES ranges, RANGE range);
1127 static void ranges_dump(RANGES ranges);
1129 static inline BOOL ranges_additem(RANGES ranges, INT nItem)
1131 RANGE range = { nItem, nItem + 1 };
1133 return ranges_add(ranges, range);
1136 static inline BOOL ranges_delitem(RANGES ranges, INT nItem)
1138 RANGE range = { nItem, nItem + 1 };
1140 return ranges_del(ranges, range);
1144 * ITERATOR DOCUMENTATION
1146 * The iterator functions allow for easy, and convenient iteration
1147 * over items of interest in the list. Typically, you create a
1148 * iterator, use it, and destroy it, as such:
1151 * iterator_xxxitems(&i, ...);
1152 * while (iterator_{prev,next}(&i)
1154 * //code which uses i.nItem
1156 * iterator_destroy(&i);
1158 * where xxx is either: framed, or visible.
1159 * Note that it is important that the code destroys the iterator
1160 * after it's done with it, as the creation of the iterator may
1161 * allocate memory, which thus needs to be freed.
1163 * You can iterate both forwards, and backwards through the list,
1164 * by using iterator_next or iterator_prev respectively.
1166 * Lower numbered items are draw on top of higher number items in
1167 * LVS_ICON, and LVS_SMALLICON (which are the only modes where
1168 * items may overlap). So, to test items, you should use
1170 * which lists the items top to bottom (in Z-order).
1171 * For drawing items, you should use
1173 * which lists the items bottom to top (in Z-order).
1174 * If you keep iterating over the items after the end-of-items
1175 * marker (-1) is returned, the iterator will start from the
1176 * beginning. Typically, you don't need to test for -1,
1177 * because iterator_{next,prev} will return TRUE if more items
1178 * are to be iterated over, or FALSE otherwise.
1180 * Note: the iterator is defined to be bidirectional. That is,
1181 * any number of prev followed by any number of next, or
1182 * five versa, should leave the iterator at the same item:
1183 * prev * n, next * n = next * n, prev * n
1185 * The iterator has a notion of an out-of-order, special item,
1186 * which sits at the start of the list. This is used in
1187 * LVS_ICON, and LVS_SMALLICON mode to handle the focused item,
1188 * which needs to be first, as it may overlap other items.
1190 * The code is a bit messy because we have:
1191 * - a special item to deal with
1192 * - simple range, or composite range
1194 * If you find bugs, or want to add features, please make sure you
1195 * always check/modify *both* iterator_prev, and iterator_next.
1199 * This function iterates through the items in increasing order,
1200 * but prefixed by the special item, then -1. That is:
1201 * special, 1, 2, 3, ..., n, -1.
1202 * Each item is listed only once.
1204 static inline BOOL iterator_next(ITERATOR* i)
1208 i->nItem = i->nSpecial;
1209 if (i->nItem != -1) return TRUE;
1211 if (i->nItem == i->nSpecial)
1213 if (i->ranges) i->index = 0;
1219 if (i->nItem == i->nSpecial) i->nItem++;
1220 if (i->nItem < i->range.upper) return TRUE;
1225 if (i->index < DPA_GetPtrCount(i->ranges->hdpa))
1226 i->range = *(RANGE*)DPA_GetPtr(i->ranges->hdpa, i->index++);
1229 else if (i->nItem >= i->range.upper) goto end;
1231 i->nItem = i->range.lower;
1232 if (i->nItem >= 0) goto testitem;
1239 * This function iterates through the items in decreasing order,
1240 * followed by the special item, then -1. That is:
1241 * n, n-1, ..., 3, 2, 1, special, -1.
1242 * Each item is listed only once.
1244 static inline BOOL iterator_prev(ITERATOR* i)
1251 if (i->ranges) i->index = DPA_GetPtrCount(i->ranges->hdpa);
1254 if (i->nItem == i->nSpecial)
1262 if (i->nItem == i->nSpecial) i->nItem--;
1263 if (i->nItem >= i->range.lower) return TRUE;
1269 i->range = *(RANGE*)DPA_GetPtr(i->ranges->hdpa, --i->index);
1272 else if (!start && i->nItem < i->range.lower) goto end;
1274 i->nItem = i->range.upper;
1275 if (i->nItem > 0) goto testitem;
1277 return (i->nItem = i->nSpecial) != -1;
1280 static RANGE iterator_range(const ITERATOR *i)
1284 if (!i->ranges) return i->range;
1286 if (DPA_GetPtrCount(i->ranges->hdpa) > 0)
1288 range.lower = (*(RANGE*)DPA_GetPtr(i->ranges->hdpa, 0)).lower;
1289 range.upper = (*(RANGE*)DPA_GetPtr(i->ranges->hdpa, DPA_GetPtrCount(i->ranges->hdpa) - 1)).upper;
1291 else range.lower = range.upper = 0;
1297 * Releases resources associated with this iterator.
1299 static inline void iterator_destroy(const ITERATOR *i)
1301 ranges_destroy(i->ranges);
1305 * Create an empty iterator.
1307 static inline BOOL iterator_empty(ITERATOR* i)
1309 ZeroMemory(i, sizeof(*i));
1310 i->nItem = i->nSpecial = i->range.lower = i->range.upper = -1;
1315 * Create an iterator over a range.
1317 static inline BOOL iterator_rangeitems(ITERATOR* i, RANGE range)
1325 * Create an iterator over a bunch of ranges.
1326 * Please note that the iterator will take ownership of the ranges,
1327 * and will free them upon destruction.
1329 static inline BOOL iterator_rangesitems(ITERATOR* i, RANGES ranges)
1337 * Creates an iterator over the items which intersect frame.
1338 * Uses absolute coordinates rather than compensating for the current offset.
1340 static BOOL iterator_frameditems_absolute(ITERATOR* i, const LISTVIEW_INFO* infoPtr, const RECT *frame)
1342 RECT rcItem, rcTemp;
1344 /* in case we fail, we want to return an empty iterator */
1345 if (!iterator_empty(i)) return FALSE;
1347 TRACE("(frame=%s)\n", wine_dbgstr_rect(frame));
1349 if (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON)
1353 if (infoPtr->uView == LV_VIEW_ICON && infoPtr->nFocusedItem != -1)
1355 LISTVIEW_GetItemBox(infoPtr, infoPtr->nFocusedItem, &rcItem);
1356 if (IntersectRect(&rcTemp, &rcItem, frame))
1357 i->nSpecial = infoPtr->nFocusedItem;
1359 if (!(iterator_rangesitems(i, ranges_create(50)))) return FALSE;
1360 /* to do better here, we need to have PosX, and PosY sorted */
1361 TRACE("building icon ranges:\n");
1362 for (nItem = 0; nItem < infoPtr->nItemCount; nItem++)
1364 rcItem.left = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosX, nItem);
1365 rcItem.top = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosY, nItem);
1366 rcItem.right = rcItem.left + infoPtr->nItemWidth;
1367 rcItem.bottom = rcItem.top + infoPtr->nItemHeight;
1368 if (IntersectRect(&rcTemp, &rcItem, frame))
1369 ranges_additem(i->ranges, nItem);
1373 else if (infoPtr->uView == LV_VIEW_DETAILS)
1377 if (frame->left >= infoPtr->nItemWidth) return TRUE;
1378 if (frame->top >= infoPtr->nItemHeight * infoPtr->nItemCount) return TRUE;
1380 range.lower = max(frame->top / infoPtr->nItemHeight, 0);
1381 range.upper = min((frame->bottom - 1) / infoPtr->nItemHeight, infoPtr->nItemCount - 1) + 1;
1382 if (range.upper <= range.lower) return TRUE;
1383 if (!iterator_rangeitems(i, range)) return FALSE;
1384 TRACE(" report=%s\n", debugrange(&i->range));
1388 INT nPerCol = max((infoPtr->rcList.bottom - infoPtr->rcList.top) / infoPtr->nItemHeight, 1);
1389 INT nFirstRow = max(frame->top / infoPtr->nItemHeight, 0);
1390 INT nLastRow = min((frame->bottom - 1) / infoPtr->nItemHeight, nPerCol - 1);
1397 if (infoPtr->nItemWidth)
1399 nFirstCol = max(frame->left / infoPtr->nItemWidth, 0);
1400 nLastCol = min((frame->right - 1) / infoPtr->nItemWidth, (infoPtr->nItemCount + nPerCol - 1) / nPerCol);
1404 nFirstCol = max(frame->left, 0);
1405 nLastCol = min(frame->right - 1, (infoPtr->nItemCount + nPerCol - 1) / nPerCol);
1408 lower = nFirstCol * nPerCol + nFirstRow;
1410 TRACE("nPerCol=%d, nFirstRow=%d, nLastRow=%d, nFirstCol=%d, nLastCol=%d, lower=%d\n",
1411 nPerCol, nFirstRow, nLastRow, nFirstCol, nLastCol, lower);
1413 if (nLastCol < nFirstCol || nLastRow < nFirstRow) return TRUE;
1415 if (!(iterator_rangesitems(i, ranges_create(nLastCol - nFirstCol + 1)))) return FALSE;
1416 TRACE("building list ranges:\n");
1417 for (nCol = nFirstCol; nCol <= nLastCol; nCol++)
1419 item_range.lower = nCol * nPerCol + nFirstRow;
1420 if(item_range.lower >= infoPtr->nItemCount) break;
1421 item_range.upper = min(nCol * nPerCol + nLastRow + 1, infoPtr->nItemCount);
1422 TRACE(" list=%s\n", debugrange(&item_range));
1423 ranges_add(i->ranges, item_range);
1431 * Creates an iterator over the items which intersect lprc.
1433 static BOOL iterator_frameditems(ITERATOR* i, const LISTVIEW_INFO* infoPtr, const RECT *lprc)
1438 TRACE("(lprc=%s)\n", wine_dbgstr_rect(lprc));
1440 LISTVIEW_GetOrigin(infoPtr, &Origin);
1441 OffsetRect(&frame, -Origin.x, -Origin.y);
1443 return iterator_frameditems_absolute(i, infoPtr, &frame);
1447 * Creates an iterator over the items which intersect the visible region of hdc.
1449 static BOOL iterator_visibleitems(ITERATOR *i, const LISTVIEW_INFO *infoPtr, HDC hdc)
1451 POINT Origin, Position;
1452 RECT rcItem, rcClip;
1455 rgntype = GetClipBox(hdc, &rcClip);
1456 if (rgntype == NULLREGION) return iterator_empty(i);
1457 if (!iterator_frameditems(i, infoPtr, &rcClip)) return FALSE;
1458 if (rgntype == SIMPLEREGION) return TRUE;
1460 /* first deal with the special item */
1461 if (i->nSpecial != -1)
1463 LISTVIEW_GetItemBox(infoPtr, i->nSpecial, &rcItem);
1464 if (!RectVisible(hdc, &rcItem)) i->nSpecial = -1;
1467 /* if we can't deal with the region, we'll just go with the simple range */
1468 LISTVIEW_GetOrigin(infoPtr, &Origin);
1469 TRACE("building visible range:\n");
1470 if (!i->ranges && i->range.lower < i->range.upper)
1472 if (!(i->ranges = ranges_create(50))) return TRUE;
1473 if (!ranges_add(i->ranges, i->range))
1475 ranges_destroy(i->ranges);
1481 /* now delete the invisible items from the list */
1482 while(iterator_next(i))
1484 LISTVIEW_GetItemOrigin(infoPtr, i->nItem, &Position);
1485 rcItem.left = (infoPtr->uView == LV_VIEW_DETAILS) ? Origin.x : Position.x + Origin.x;
1486 rcItem.top = Position.y + Origin.y;
1487 rcItem.right = rcItem.left + infoPtr->nItemWidth;
1488 rcItem.bottom = rcItem.top + infoPtr->nItemHeight;
1489 if (!RectVisible(hdc, &rcItem))
1490 ranges_delitem(i->ranges, i->nItem);
1492 /* the iterator should restart on the next iterator_next */
1498 /* Remove common elements from two iterators */
1499 /* Passed iterators have to point on the first elements */
1500 static BOOL iterator_remove_common_items(ITERATOR *iter1, ITERATOR *iter2)
1502 if(!iter1->ranges || !iter2->ranges) {
1505 if(iter1->ranges || iter2->ranges ||
1506 (iter1->range.lower<iter2->range.lower && iter1->range.upper>iter2->range.upper) ||
1507 (iter1->range.lower>iter2->range.lower && iter1->range.upper<iter2->range.upper)) {
1508 ERR("result is not a one range iterator\n");
1512 if(iter1->range.lower==-1 || iter2->range.lower==-1)
1515 lower = iter1->range.lower;
1516 upper = iter1->range.upper;
1518 if(lower < iter2->range.lower)
1519 iter1->range.upper = iter2->range.lower;
1520 else if(upper > iter2->range.upper)
1521 iter1->range.lower = iter2->range.upper;
1523 iter1->range.lower = iter1->range.upper = -1;
1525 if(iter2->range.lower < lower)
1526 iter2->range.upper = lower;
1527 else if(iter2->range.upper > upper)
1528 iter2->range.lower = upper;
1530 iter2->range.lower = iter2->range.upper = -1;
1535 iterator_next(iter1);
1536 iterator_next(iter2);
1539 if(iter1->nItem==-1 || iter2->nItem==-1)
1542 if(iter1->nItem == iter2->nItem) {
1543 int delete = iter1->nItem;
1545 iterator_prev(iter1);
1546 iterator_prev(iter2);
1547 ranges_delitem(iter1->ranges, delete);
1548 ranges_delitem(iter2->ranges, delete);
1549 iterator_next(iter1);
1550 iterator_next(iter2);
1551 } else if(iter1->nItem > iter2->nItem)
1552 iterator_next(iter2);
1554 iterator_next(iter1);
1557 iter1->nItem = iter1->range.lower = iter1->range.upper = -1;
1558 iter2->nItem = iter2->range.lower = iter2->range.upper = -1;
1562 /******** Misc helper functions ************************************/
1564 static inline LRESULT CallWindowProcT(WNDPROC proc, HWND hwnd, UINT uMsg,
1565 WPARAM wParam, LPARAM lParam, BOOL isW)
1567 if (isW) return CallWindowProcW(proc, hwnd, uMsg, wParam, lParam);
1568 else return CallWindowProcA(proc, hwnd, uMsg, wParam, lParam);
1571 static inline BOOL is_autoarrange(const LISTVIEW_INFO *infoPtr)
1573 return ((infoPtr->dwStyle & LVS_AUTOARRANGE) || infoPtr->bAutoarrange) &&
1574 (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON);
1577 static void toggle_checkbox_state(LISTVIEW_INFO *infoPtr, INT nItem)
1579 DWORD state = STATEIMAGEINDEX(LISTVIEW_GetItemState(infoPtr, nItem, LVIS_STATEIMAGEMASK));
1580 if(state == 1 || state == 2)
1584 lvitem.state = INDEXTOSTATEIMAGEMASK(state);
1585 lvitem.stateMask = LVIS_STATEIMAGEMASK;
1586 LISTVIEW_SetItemState(infoPtr, nItem, &lvitem);
1590 /* this should be called after window style got updated,
1591 it used to reset view state to match current window style */
1592 static inline void map_style_view(LISTVIEW_INFO *infoPtr)
1594 switch (infoPtr->dwStyle & LVS_TYPEMASK)
1597 infoPtr->uView = LV_VIEW_ICON;
1600 infoPtr->uView = LV_VIEW_DETAILS;
1603 infoPtr->uView = LV_VIEW_SMALLICON;
1606 infoPtr->uView = LV_VIEW_LIST;
1610 /* computes next item id value */
1611 static DWORD get_next_itemid(const LISTVIEW_INFO *infoPtr)
1613 INT count = DPA_GetPtrCount(infoPtr->hdpaItemIds);
1617 ITEM_ID *lpID = DPA_GetPtr(infoPtr->hdpaItemIds, count - 1);
1618 return lpID->id + 1;
1623 /******** Internal API functions ************************************/
1625 static inline COLUMN_INFO * LISTVIEW_GetColumnInfo(const LISTVIEW_INFO *infoPtr, INT nSubItem)
1627 static COLUMN_INFO mainItem;
1629 if (nSubItem == 0 && DPA_GetPtrCount(infoPtr->hdpaColumns) == 0) return &mainItem;
1630 assert (nSubItem >= 0 && nSubItem < DPA_GetPtrCount(infoPtr->hdpaColumns));
1632 /* update cached column rectangles */
1633 if (infoPtr->colRectsDirty)
1636 LISTVIEW_INFO *Ptr = (LISTVIEW_INFO*)infoPtr;
1639 for (i = 0; i < DPA_GetPtrCount(infoPtr->hdpaColumns); i++) {
1640 info = DPA_GetPtr(infoPtr->hdpaColumns, i);
1641 SendMessageW(infoPtr->hwndHeader, HDM_GETITEMRECT, i, (LPARAM)&info->rcHeader);
1643 Ptr->colRectsDirty = FALSE;
1646 return DPA_GetPtr(infoPtr->hdpaColumns, nSubItem);
1649 static INT LISTVIEW_CreateHeader(LISTVIEW_INFO *infoPtr)
1651 DWORD dFlags = WS_CHILD | HDS_HORZ | HDS_FULLDRAG | HDS_DRAGDROP;
1654 if (infoPtr->hwndHeader) return 0;
1656 TRACE("Creating header for list %p\n", infoPtr->hwndSelf);
1658 /* setup creation flags */
1659 dFlags |= (LVS_NOSORTHEADER & infoPtr->dwStyle) ? 0 : HDS_BUTTONS;
1660 dFlags |= (LVS_NOCOLUMNHEADER & infoPtr->dwStyle) ? HDS_HIDDEN : 0;
1662 hInst = (HINSTANCE)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_HINSTANCE);
1665 infoPtr->hwndHeader = CreateWindowW(WC_HEADERW, NULL, dFlags,
1666 0, 0, 0, 0, infoPtr->hwndSelf, NULL, hInst, NULL);
1667 if (!infoPtr->hwndHeader) return -1;
1669 /* set header unicode format */
1670 SendMessageW(infoPtr->hwndHeader, HDM_SETUNICODEFORMAT, TRUE, 0);
1672 /* set header font */
1673 SendMessageW(infoPtr->hwndHeader, WM_SETFONT, (WPARAM)infoPtr->hFont, TRUE);
1675 /* set header image list */
1676 if (infoPtr->himlSmall)
1677 SendMessageW(infoPtr->hwndHeader, HDM_SETIMAGELIST, 0, (LPARAM)infoPtr->himlSmall);
1679 LISTVIEW_UpdateSize(infoPtr);
1684 static inline void LISTVIEW_GetHeaderRect(const LISTVIEW_INFO *infoPtr, INT nSubItem, LPRECT lprc)
1686 *lprc = LISTVIEW_GetColumnInfo(infoPtr, nSubItem)->rcHeader;
1689 static inline BOOL LISTVIEW_IsHeaderEnabled(const LISTVIEW_INFO *infoPtr)
1691 return (infoPtr->uView == LV_VIEW_DETAILS ||
1692 infoPtr->dwLvExStyle & LVS_EX_HEADERINALLVIEWS) &&
1693 !(infoPtr->dwStyle & LVS_NOCOLUMNHEADER);
1696 static inline BOOL LISTVIEW_GetItemW(const LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem)
1698 return LISTVIEW_GetItemT(infoPtr, lpLVItem, TRUE);
1701 /* used to handle collapse main item column case */
1702 static inline BOOL LISTVIEW_DrawFocusRect(const LISTVIEW_INFO *infoPtr, HDC hdc)
1704 return (infoPtr->rcFocus.left < infoPtr->rcFocus.right) ?
1705 DrawFocusRect(hdc, &infoPtr->rcFocus) : FALSE;
1708 /* Listview invalidation functions: use _only_ these functions to invalidate */
1710 static inline BOOL is_redrawing(const LISTVIEW_INFO *infoPtr)
1712 return infoPtr->bRedraw;
1715 static inline void LISTVIEW_InvalidateRect(const LISTVIEW_INFO *infoPtr, const RECT* rect)
1717 if(!is_redrawing(infoPtr)) return;
1718 TRACE(" invalidating rect=%s\n", wine_dbgstr_rect(rect));
1719 InvalidateRect(infoPtr->hwndSelf, rect, TRUE);
1722 static inline void LISTVIEW_InvalidateItem(const LISTVIEW_INFO *infoPtr, INT nItem)
1726 if(!is_redrawing(infoPtr)) return;
1727 LISTVIEW_GetItemBox(infoPtr, nItem, &rcBox);
1728 LISTVIEW_InvalidateRect(infoPtr, &rcBox);
1731 static inline void LISTVIEW_InvalidateSubItem(const LISTVIEW_INFO *infoPtr, INT nItem, INT nSubItem)
1733 POINT Origin, Position;
1736 if(!is_redrawing(infoPtr)) return;
1737 assert (infoPtr->uView == LV_VIEW_DETAILS);
1738 LISTVIEW_GetOrigin(infoPtr, &Origin);
1739 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
1740 LISTVIEW_GetHeaderRect(infoPtr, nSubItem, &rcBox);
1742 rcBox.bottom = infoPtr->nItemHeight;
1743 OffsetRect(&rcBox, Origin.x + Position.x, Origin.y + Position.y);
1744 LISTVIEW_InvalidateRect(infoPtr, &rcBox);
1747 static inline void LISTVIEW_InvalidateList(const LISTVIEW_INFO *infoPtr)
1749 LISTVIEW_InvalidateRect(infoPtr, NULL);
1752 static inline void LISTVIEW_InvalidateColumn(const LISTVIEW_INFO *infoPtr, INT nColumn)
1756 if(!is_redrawing(infoPtr)) return;
1757 LISTVIEW_GetHeaderRect(infoPtr, nColumn, &rcCol);
1758 rcCol.top = infoPtr->rcList.top;
1759 rcCol.bottom = infoPtr->rcList.bottom;
1760 LISTVIEW_InvalidateRect(infoPtr, &rcCol);
1765 * Retrieves the number of items that can fit vertically in the client area.
1768 * [I] infoPtr : valid pointer to the listview structure
1771 * Number of items per row.
1773 static inline INT LISTVIEW_GetCountPerRow(const LISTVIEW_INFO *infoPtr)
1775 INT nListWidth = infoPtr->rcList.right - infoPtr->rcList.left;
1777 return max(nListWidth/(infoPtr->nItemWidth ? infoPtr->nItemWidth : 1), 1);
1782 * Retrieves the number of items that can fit horizontally in the client
1786 * [I] infoPtr : valid pointer to the listview structure
1789 * Number of items per column.
1791 static inline INT LISTVIEW_GetCountPerColumn(const LISTVIEW_INFO *infoPtr)
1793 INT nListHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
1795 return max(nListHeight / infoPtr->nItemHeight, 1);
1799 /*************************************************************************
1800 * LISTVIEW_ProcessLetterKeys
1802 * Processes keyboard messages generated by pressing the letter keys
1804 * What this does is perform a case insensitive search from the
1805 * current position with the following quirks:
1806 * - If two chars or more are pressed in quick succession we search
1807 * for the corresponding string (e.g. 'abc').
1808 * - If there is a delay we wipe away the current search string and
1809 * restart with just that char.
1810 * - If the user keeps pressing the same character, whether slowly or
1811 * fast, so that the search string is entirely composed of this
1812 * character ('aaaaa' for instance), then we search for first item
1813 * that starting with that character.
1814 * - If the user types the above character in quick succession, then
1815 * we must also search for the corresponding string ('aaaaa'), and
1816 * go to that string if there is a match.
1819 * [I] hwnd : handle to the window
1820 * [I] charCode : the character code, the actual character
1821 * [I] keyData : key data
1829 * - The current implementation has a list of characters it will
1830 * accept and it ignores everything else. In particular it will
1831 * ignore accentuated characters which seems to match what
1832 * Windows does. But I'm not sure it makes sense to follow
1834 * - We don't sound a beep when the search fails.
1838 * TREEVIEW_ProcessLetterKeys
1840 static INT LISTVIEW_ProcessLetterKeys(LISTVIEW_INFO *infoPtr, WPARAM charCode, LPARAM keyData)
1842 WCHAR buffer[MAX_PATH];
1849 /* simple parameter checking */
1850 if (!charCode || !keyData || infoPtr->nItemCount == 0) return 0;
1852 /* only allow the valid WM_CHARs through */
1853 if (!isalnumW(charCode) &&
1854 charCode != '.' && charCode != '`' && charCode != '!' &&
1855 charCode != '@' && charCode != '#' && charCode != '$' &&
1856 charCode != '%' && charCode != '^' && charCode != '&' &&
1857 charCode != '*' && charCode != '(' && charCode != ')' &&
1858 charCode != '-' && charCode != '_' && charCode != '+' &&
1859 charCode != '=' && charCode != '\\'&& charCode != ']' &&
1860 charCode != '}' && charCode != '[' && charCode != '{' &&
1861 charCode != '/' && charCode != '?' && charCode != '>' &&
1862 charCode != '<' && charCode != ',' && charCode != '~')
1865 /* update the search parameters */
1866 prevTime = infoPtr->lastKeyPressTimestamp;
1867 infoPtr->lastKeyPressTimestamp = GetTickCount();
1868 diff = infoPtr->lastKeyPressTimestamp - prevTime;
1870 if (diff >= 0 && diff < KEY_DELAY)
1872 if (infoPtr->nSearchParamLength < MAX_PATH - 1)
1873 infoPtr->szSearchParam[infoPtr->nSearchParamLength++] = charCode;
1875 if (infoPtr->charCode != charCode)
1876 infoPtr->charCode = charCode = 0;
1880 infoPtr->charCode = charCode;
1881 infoPtr->szSearchParam[0] = charCode;
1882 infoPtr->nSearchParamLength = 1;
1885 /* should start from next after focused item, so next item that matches
1886 will be selected, if there isn't any and focused matches it will be selected
1887 on second search stage from beginning of the list */
1888 if (infoPtr->nFocusedItem >= 0 && infoPtr->nItemCount > 1)
1890 /* with some accumulated search data available start with current focus, otherwise
1891 it's excluded from search */
1892 startidx = infoPtr->nSearchParamLength > 1 ? infoPtr->nFocusedItem : infoPtr->nFocusedItem + 1;
1893 if (startidx == infoPtr->nItemCount) startidx = 0;
1898 /* let application handle this for virtual listview */
1899 if (infoPtr->dwStyle & LVS_OWNERDATA)
1903 memset(&nmlv.lvfi, 0, sizeof(nmlv.lvfi));
1904 nmlv.lvfi.flags = (LVFI_WRAP | LVFI_PARTIAL);
1905 nmlv.lvfi.psz = infoPtr->szSearchParam;
1906 nmlv.iStart = startidx;
1908 infoPtr->szSearchParam[infoPtr->nSearchParamLength] = 0;
1910 nItem = notify_hdr(infoPtr, LVN_ODFINDITEMW, (LPNMHDR)&nmlv.hdr);
1914 int i = startidx, endidx;
1916 /* and search from the current position */
1918 endidx = infoPtr->nItemCount;
1920 /* first search in [startidx, endidx), on failure continue in [0, startidx) */
1923 /* start from first item if not found with >= startidx */
1924 if (i == infoPtr->nItemCount && startidx > 0)
1930 for (i = startidx; i < endidx; i++)
1933 item.mask = LVIF_TEXT;
1936 item.pszText = buffer;
1937 item.cchTextMax = MAX_PATH;
1938 if (!LISTVIEW_GetItemW(infoPtr, &item)) return 0;
1940 if (!lstrncmpiW(item.pszText, infoPtr->szSearchParam, infoPtr->nSearchParamLength))
1945 /* this is used to find first char match when search string is not available yet,
1946 otherwise every WM_CHAR will search to next item by first char, ignoring that we're
1947 already waiting for user to complete a string */
1948 else if (nItem == -1 && infoPtr->nSearchParamLength == 1 && !lstrncmpiW(item.pszText, infoPtr->szSearchParam, 1))
1950 /* this would work but we must keep looking for a longer match */
1955 if ( nItem != -1 || /* found something */
1956 endidx != infoPtr->nItemCount || /* second search done */
1957 (startidx == 0 && endidx == infoPtr->nItemCount) /* full range for first search */ )
1963 LISTVIEW_KeySelection(infoPtr, nItem, FALSE);
1968 /*************************************************************************
1969 * LISTVIEW_UpdateHeaderSize [Internal]
1971 * Function to resize the header control
1974 * [I] hwnd : handle to a window
1975 * [I] nNewScrollPos : scroll pos to set
1980 static void LISTVIEW_UpdateHeaderSize(const LISTVIEW_INFO *infoPtr, INT nNewScrollPos)
1985 TRACE("nNewScrollPos=%d\n", nNewScrollPos);
1987 if (!infoPtr->hwndHeader) return;
1989 GetWindowRect(infoPtr->hwndHeader, &winRect);
1990 point[0].x = winRect.left;
1991 point[0].y = winRect.top;
1992 point[1].x = winRect.right;
1993 point[1].y = winRect.bottom;
1995 MapWindowPoints(HWND_DESKTOP, infoPtr->hwndSelf, point, 2);
1996 point[0].x = -nNewScrollPos;
1997 point[1].x += nNewScrollPos;
1999 SetWindowPos(infoPtr->hwndHeader,0,
2000 point[0].x,point[0].y,point[1].x,point[1].y,
2001 (infoPtr->dwStyle & LVS_NOCOLUMNHEADER) ? SWP_HIDEWINDOW : SWP_SHOWWINDOW |
2002 SWP_NOZORDER | SWP_NOACTIVATE);
2007 * Update the scrollbars. This functions should be called whenever
2008 * the content, size or view changes.
2011 * [I] infoPtr : valid pointer to the listview structure
2016 static void LISTVIEW_UpdateScroll(const LISTVIEW_INFO *infoPtr)
2018 SCROLLINFO horzInfo, vertInfo;
2021 if ((infoPtr->dwStyle & LVS_NOSCROLL) || !is_redrawing(infoPtr)) return;
2023 ZeroMemory(&horzInfo, sizeof(SCROLLINFO));
2024 horzInfo.cbSize = sizeof(SCROLLINFO);
2025 horzInfo.nPage = infoPtr->rcList.right - infoPtr->rcList.left;
2027 /* for now, we'll set info.nMax to the _count_, and adjust it later */
2028 if (infoPtr->uView == LV_VIEW_LIST)
2030 INT nPerCol = LISTVIEW_GetCountPerColumn(infoPtr);
2031 horzInfo.nMax = (infoPtr->nItemCount + nPerCol - 1) / nPerCol;
2033 /* scroll by at least one column per page */
2034 if(horzInfo.nPage < infoPtr->nItemWidth)
2035 horzInfo.nPage = infoPtr->nItemWidth;
2037 if (infoPtr->nItemWidth)
2038 horzInfo.nPage /= infoPtr->nItemWidth;
2040 else if (infoPtr->uView == LV_VIEW_DETAILS)
2042 horzInfo.nMax = infoPtr->nItemWidth;
2044 else /* LV_VIEW_ICON, or LV_VIEW_SMALLICON */
2048 if (LISTVIEW_GetViewRect(infoPtr, &rcView)) horzInfo.nMax = rcView.right - rcView.left;
2051 if (LISTVIEW_IsHeaderEnabled(infoPtr))
2053 if (DPA_GetPtrCount(infoPtr->hdpaColumns))
2058 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX,
2059 DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, 0);
2061 LISTVIEW_GetHeaderRect(infoPtr, index, &rcHeader);
2062 horzInfo.nMax = rcHeader.right;
2063 TRACE("horzInfo.nMax=%d\n", horzInfo.nMax);
2067 horzInfo.fMask = SIF_RANGE | SIF_PAGE;
2068 horzInfo.nMax = max(horzInfo.nMax - 1, 0);
2069 dx = GetScrollPos(infoPtr->hwndSelf, SB_HORZ);
2070 dx -= SetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &horzInfo, TRUE);
2071 TRACE("horzInfo=%s\n", debugscrollinfo(&horzInfo));
2073 /* Setting the horizontal scroll can change the listview size
2074 * (and potentially everything else) so we need to recompute
2075 * everything again for the vertical scroll
2078 ZeroMemory(&vertInfo, sizeof(SCROLLINFO));
2079 vertInfo.cbSize = sizeof(SCROLLINFO);
2080 vertInfo.nPage = infoPtr->rcList.bottom - infoPtr->rcList.top;
2082 if (infoPtr->uView == LV_VIEW_DETAILS)
2084 vertInfo.nMax = infoPtr->nItemCount;
2086 /* scroll by at least one page */
2087 if(vertInfo.nPage < infoPtr->nItemHeight)
2088 vertInfo.nPage = infoPtr->nItemHeight;
2090 if (infoPtr->nItemHeight > 0)
2091 vertInfo.nPage /= infoPtr->nItemHeight;
2093 else if (infoPtr->uView != LV_VIEW_LIST) /* LV_VIEW_ICON, or LV_VIEW_SMALLICON */
2097 if (LISTVIEW_GetViewRect(infoPtr, &rcView)) vertInfo.nMax = rcView.bottom - rcView.top;
2100 vertInfo.fMask = SIF_RANGE | SIF_PAGE;
2101 vertInfo.nMax = max(vertInfo.nMax - 1, 0);
2102 dy = GetScrollPos(infoPtr->hwndSelf, SB_VERT);
2103 dy -= SetScrollInfo(infoPtr->hwndSelf, SB_VERT, &vertInfo, TRUE);
2104 TRACE("vertInfo=%s\n", debugscrollinfo(&vertInfo));
2106 /* Change of the range may have changed the scroll pos. If so move the content */
2107 if (dx != 0 || dy != 0)
2110 listRect = infoPtr->rcList;
2111 ScrollWindowEx(infoPtr->hwndSelf, dx, dy, &listRect, &listRect, 0, 0,
2112 SW_ERASE | SW_INVALIDATE);
2115 /* Update the Header Control */
2116 if (infoPtr->hwndHeader)
2118 horzInfo.fMask = SIF_POS;
2119 GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &horzInfo);
2120 LISTVIEW_UpdateHeaderSize(infoPtr, horzInfo.nPos);
2127 * Shows/hides the focus rectangle.
2130 * [I] infoPtr : valid pointer to the listview structure
2131 * [I] fShow : TRUE to show the focus, FALSE to hide it.
2136 static void LISTVIEW_ShowFocusRect(const LISTVIEW_INFO *infoPtr, BOOL fShow)
2140 TRACE("fShow=%d, nItem=%d\n", fShow, infoPtr->nFocusedItem);
2142 if (infoPtr->nFocusedItem < 0) return;
2144 /* we need some gymnastics in ICON mode to handle large items */
2145 if (infoPtr->uView == LV_VIEW_ICON)
2149 LISTVIEW_GetItemBox(infoPtr, infoPtr->nFocusedItem, &rcBox);
2150 if ((rcBox.bottom - rcBox.top) > infoPtr->nItemHeight)
2152 LISTVIEW_InvalidateRect(infoPtr, &rcBox);
2157 if (!(hdc = GetDC(infoPtr->hwndSelf))) return;
2159 /* for some reason, owner draw should work only in report mode */
2160 if ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (infoPtr->uView == LV_VIEW_DETAILS))
2165 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
2166 HFONT hOldFont = SelectObject(hdc, hFont);
2168 item.iItem = infoPtr->nFocusedItem;
2170 item.mask = LVIF_PARAM;
2171 if (!LISTVIEW_GetItemW(infoPtr, &item)) goto done;
2173 ZeroMemory(&dis, sizeof(dis));
2174 dis.CtlType = ODT_LISTVIEW;
2175 dis.CtlID = (UINT)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
2176 dis.itemID = item.iItem;
2177 dis.itemAction = ODA_FOCUS;
2178 if (fShow) dis.itemState |= ODS_FOCUS;
2179 dis.hwndItem = infoPtr->hwndSelf;
2181 LISTVIEW_GetItemBox(infoPtr, dis.itemID, &dis.rcItem);
2182 dis.itemData = item.lParam;
2184 SendMessageW(infoPtr->hwndNotify, WM_DRAWITEM, dis.CtlID, (LPARAM)&dis);
2186 SelectObject(hdc, hOldFont);
2190 LISTVIEW_DrawFocusRect(infoPtr, hdc);
2193 ReleaseDC(infoPtr->hwndSelf, hdc);
2197 * Invalidates all visible selected items.
2199 static void LISTVIEW_InvalidateSelectedItems(const LISTVIEW_INFO *infoPtr)
2203 iterator_frameditems(&i, infoPtr, &infoPtr->rcList);
2204 while(iterator_next(&i))
2206 if (LISTVIEW_GetItemState(infoPtr, i.nItem, LVIS_SELECTED))
2207 LISTVIEW_InvalidateItem(infoPtr, i.nItem);
2209 iterator_destroy(&i);
2214 * DESCRIPTION: [INTERNAL]
2215 * Computes an item's (left,top) corner, relative to rcView.
2216 * That is, the position has NOT been made relative to the Origin.
2217 * This is deliberate, to avoid computing the Origin over, and
2218 * over again, when this function is called in a loop. Instead,
2219 * one can factor the computation of the Origin before the loop,
2220 * and offset the value returned by this function, on every iteration.
2223 * [I] infoPtr : valid pointer to the listview structure
2224 * [I] nItem : item number
2225 * [O] lpptOrig : item top, left corner
2230 static void LISTVIEW_GetItemOrigin(const LISTVIEW_INFO *infoPtr, INT nItem, LPPOINT lpptPosition)
2232 assert(nItem >= 0 && nItem < infoPtr->nItemCount);
2234 if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
2236 lpptPosition->x = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosX, nItem);
2237 lpptPosition->y = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosY, nItem);
2239 else if (infoPtr->uView == LV_VIEW_LIST)
2241 INT nCountPerColumn = LISTVIEW_GetCountPerColumn(infoPtr);
2242 lpptPosition->x = nItem / nCountPerColumn * infoPtr->nItemWidth;
2243 lpptPosition->y = nItem % nCountPerColumn * infoPtr->nItemHeight;
2245 else /* LV_VIEW_DETAILS */
2247 lpptPosition->x = REPORT_MARGINX;
2248 /* item is always at zero indexed column */
2249 if (DPA_GetPtrCount(infoPtr->hdpaColumns) > 0)
2250 lpptPosition->x += LISTVIEW_GetColumnInfo(infoPtr, 0)->rcHeader.left;
2251 lpptPosition->y = nItem * infoPtr->nItemHeight;
2256 * DESCRIPTION: [INTERNAL]
2257 * Compute the rectangles of an item. This is to localize all
2258 * the computations in one place. If you are not interested in some
2259 * of these values, simply pass in a NULL -- the function is smart
2260 * enough to compute only what's necessary. The function computes
2261 * the standard rectangles (BOUNDS, ICON, LABEL) plus a non-standard
2262 * one, the BOX rectangle. This rectangle is very cheap to compute,
2263 * and is guaranteed to contain all the other rectangles. Computing
2264 * the ICON rect is also cheap, but all the others are potentially
2265 * expensive. This gives an easy and effective optimization when
2266 * searching (like point inclusion, or rectangle intersection):
2267 * first test against the BOX, and if TRUE, test against the desired
2269 * If the function does not have all the necessary information
2270 * to computed the requested rectangles, will crash with a
2271 * failed assertion. This is done so we catch all programming
2272 * errors, given that the function is called only from our code.
2274 * We have the following 'special' meanings for a few fields:
2275 * * If LVIS_FOCUSED is set, we assume the item has the focus
2276 * This is important in ICON mode, where it might get a larger
2277 * then usual rectangle
2279 * Please note that subitem support works only in REPORT mode.
2282 * [I] infoPtr : valid pointer to the listview structure
2283 * [I] lpLVItem : item to compute the measures for
2284 * [O] lprcBox : ptr to Box rectangle
2285 * Same as LVM_GETITEMRECT with LVIR_BOUNDS
2286 * [0] lprcSelectBox : ptr to select box rectangle
2287 * Same as LVM_GETITEMRECT with LVIR_SELECTEDBOUNDS
2288 * [O] lprcIcon : ptr to Icon rectangle
2289 * Same as LVM_GETITEMRECT with LVIR_ICON
2290 * [O] lprcStateIcon: ptr to State Icon rectangle
2291 * [O] lprcLabel : ptr to Label rectangle
2292 * Same as LVM_GETITEMRECT with LVIR_LABEL
2297 static void LISTVIEW_GetItemMetrics(const LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem,
2298 LPRECT lprcBox, LPRECT lprcSelectBox,
2299 LPRECT lprcIcon, LPRECT lprcStateIcon, LPRECT lprcLabel)
2301 BOOL doSelectBox = FALSE, doIcon = FALSE, doLabel = FALSE, oversizedBox = FALSE;
2302 RECT Box, SelectBox, Icon, Label;
2303 COLUMN_INFO *lpColumnInfo = NULL;
2304 SIZE labelSize = { 0, 0 };
2306 TRACE("(lpLVItem=%s)\n", debuglvitem_t(lpLVItem, TRUE));
2308 /* Be smart and try to figure out the minimum we have to do */
2309 if (lpLVItem->iSubItem) assert(infoPtr->uView == LV_VIEW_DETAILS);
2310 if (infoPtr->uView == LV_VIEW_ICON && (lprcBox || lprcLabel))
2312 assert((lpLVItem->mask & LVIF_STATE) && (lpLVItem->stateMask & LVIS_FOCUSED));
2313 if (lpLVItem->state & LVIS_FOCUSED) oversizedBox = doLabel = TRUE;
2315 if (lprcSelectBox) doSelectBox = TRUE;
2316 if (lprcLabel) doLabel = TRUE;
2317 if (doLabel || lprcIcon || lprcStateIcon) doIcon = TRUE;
2324 /************************************************************/
2325 /* compute the box rectangle (it should be cheap to do) */
2326 /************************************************************/
2327 if (lpLVItem->iSubItem || infoPtr->uView == LV_VIEW_DETAILS)
2328 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, lpLVItem->iSubItem);
2330 if (lpLVItem->iSubItem)
2332 Box = lpColumnInfo->rcHeader;
2337 Box.right = infoPtr->nItemWidth;
2340 Box.bottom = infoPtr->nItemHeight;
2342 /******************************************************************/
2343 /* compute ICON bounding box (ala LVM_GETITEMRECT) and STATEICON */
2344 /******************************************************************/
2347 LONG state_width = 0;
2349 if (infoPtr->himlState && lpLVItem->iSubItem == 0)
2350 state_width = infoPtr->iconStateSize.cx;
2352 if (infoPtr->uView == LV_VIEW_ICON)
2354 Icon.left = Box.left + state_width;
2355 if (infoPtr->himlNormal)
2356 Icon.left += (infoPtr->nItemWidth - infoPtr->iconSize.cx - state_width) / 2;
2357 Icon.top = Box.top + ICON_TOP_PADDING;
2358 Icon.right = Icon.left;
2359 Icon.bottom = Icon.top;
2360 if (infoPtr->himlNormal)
2362 Icon.right += infoPtr->iconSize.cx;
2363 Icon.bottom += infoPtr->iconSize.cy;
2366 else /* LV_VIEW_SMALLICON, LV_VIEW_LIST or LV_VIEW_DETAILS */
2368 Icon.left = Box.left + state_width;
2370 if (infoPtr->uView == LV_VIEW_DETAILS && lpLVItem->iSubItem == 0)
2372 /* we need the indent in report mode */
2373 assert(lpLVItem->mask & LVIF_INDENT);
2374 Icon.left += infoPtr->iconSize.cx * lpLVItem->iIndent + REPORT_MARGINX;
2378 Icon.right = Icon.left;
2379 if (infoPtr->himlSmall &&
2380 (!lpColumnInfo || lpLVItem->iSubItem == 0 ||
2381 ((infoPtr->dwLvExStyle & LVS_EX_SUBITEMIMAGES) && lpLVItem->iImage != I_IMAGECALLBACK)))
2382 Icon.right += infoPtr->iconSize.cx;
2383 Icon.bottom = Icon.top + infoPtr->iconSize.cy;
2385 if(lprcIcon) *lprcIcon = Icon;
2386 TRACE(" - icon=%s\n", wine_dbgstr_rect(&Icon));
2388 /* TODO: is this correct? */
2391 lprcStateIcon->left = Icon.left - state_width;
2392 lprcStateIcon->right = Icon.left;
2393 lprcStateIcon->top = Icon.top;
2394 lprcStateIcon->bottom = lprcStateIcon->top + infoPtr->iconSize.cy;
2395 TRACE(" - state icon=%s\n", wine_dbgstr_rect(lprcStateIcon));
2398 else Icon.right = 0;
2400 /************************************************************/
2401 /* compute LABEL bounding box (ala LVM_GETITEMRECT) */
2402 /************************************************************/
2405 /* calculate how far to the right can the label stretch */
2406 Label.right = Box.right;
2407 if (infoPtr->uView == LV_VIEW_DETAILS)
2409 if (lpLVItem->iSubItem == 0)
2411 /* we need a zero based rect here */
2412 Label = lpColumnInfo->rcHeader;
2413 OffsetRect(&Label, -Label.left, 0);
2417 if (lpLVItem->iSubItem || ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && infoPtr->uView == LV_VIEW_DETAILS))
2419 labelSize.cx = infoPtr->nItemWidth;
2420 labelSize.cy = infoPtr->nItemHeight;
2424 /* we need the text in non owner draw mode */
2425 assert(lpLVItem->mask & LVIF_TEXT);
2426 if (is_text(lpLVItem->pszText))
2428 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
2429 HDC hdc = GetDC(infoPtr->hwndSelf);
2430 HFONT hOldFont = SelectObject(hdc, hFont);
2434 /* compute rough rectangle where the label will go */
2435 SetRectEmpty(&rcText);
2436 rcText.right = infoPtr->nItemWidth - TRAILING_LABEL_PADDING;
2437 rcText.bottom = infoPtr->nItemHeight;
2438 if (infoPtr->uView == LV_VIEW_ICON)
2439 rcText.bottom -= ICON_TOP_PADDING + infoPtr->iconSize.cy + ICON_BOTTOM_PADDING;
2441 /* now figure out the flags */
2442 if (infoPtr->uView == LV_VIEW_ICON)
2443 uFormat = oversizedBox ? LV_FL_DT_FLAGS : LV_ML_DT_FLAGS;
2445 uFormat = LV_SL_DT_FLAGS;
2447 DrawTextW (hdc, lpLVItem->pszText, -1, &rcText, uFormat | DT_CALCRECT);
2449 if (rcText.right != rcText.left)
2450 labelSize.cx = min(rcText.right - rcText.left + TRAILING_LABEL_PADDING, infoPtr->nItemWidth);
2452 labelSize.cy = rcText.bottom - rcText.top;
2454 SelectObject(hdc, hOldFont);
2455 ReleaseDC(infoPtr->hwndSelf, hdc);
2459 if (infoPtr->uView == LV_VIEW_ICON)
2461 Label.left = Box.left + (infoPtr->nItemWidth - labelSize.cx) / 2;
2462 Label.top = Box.top + ICON_TOP_PADDING_HITABLE +
2463 infoPtr->iconSize.cy + ICON_BOTTOM_PADDING;
2464 Label.right = Label.left + labelSize.cx;
2465 Label.bottom = Label.top + infoPtr->nItemHeight;
2466 if (!oversizedBox && labelSize.cy > infoPtr->ntmHeight)
2468 labelSize.cy = min(Box.bottom - Label.top, labelSize.cy);
2469 labelSize.cy /= infoPtr->ntmHeight;
2470 labelSize.cy = max(labelSize.cy, 1);
2471 labelSize.cy *= infoPtr->ntmHeight;
2473 Label.bottom = Label.top + labelSize.cy + HEIGHT_PADDING;
2475 else if (infoPtr->uView == LV_VIEW_DETAILS)
2477 Label.left = Icon.right;
2478 Label.top = Box.top;
2479 Label.right = lpLVItem->iSubItem ? lpColumnInfo->rcHeader.right :
2480 lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left;
2481 Label.bottom = Label.top + infoPtr->nItemHeight;
2483 else /* LV_VIEW_SMALLICON or LV_VIEW_LIST */
2485 Label.left = Icon.right;
2486 Label.top = Box.top;
2487 Label.right = min(Label.left + labelSize.cx, Label.right);
2488 Label.bottom = Label.top + infoPtr->nItemHeight;
2491 if (lprcLabel) *lprcLabel = Label;
2492 TRACE(" - label=%s\n", wine_dbgstr_rect(&Label));
2495 /************************************************************/
2496 /* compute SELECT bounding box */
2497 /************************************************************/
2500 if (infoPtr->uView == LV_VIEW_DETAILS)
2502 SelectBox.left = Icon.left;
2503 SelectBox.top = Box.top;
2504 SelectBox.bottom = Box.bottom;
2507 SelectBox.right = min(Label.left + labelSize.cx, Label.right);
2509 SelectBox.right = min(Label.left + MAX_EMPTYTEXT_SELECT_WIDTH, Label.right);
2513 UnionRect(&SelectBox, &Icon, &Label);
2515 if (lprcSelectBox) *lprcSelectBox = SelectBox;
2516 TRACE(" - select box=%s\n", wine_dbgstr_rect(&SelectBox));
2519 /* Fix the Box if necessary */
2522 if (oversizedBox) UnionRect(lprcBox, &Box, &Label);
2523 else *lprcBox = Box;
2525 TRACE(" - box=%s\n", wine_dbgstr_rect(&Box));
2529 * DESCRIPTION: [INTERNAL]
2532 * [I] infoPtr : valid pointer to the listview structure
2533 * [I] nItem : item number
2534 * [O] lprcBox : ptr to Box rectangle
2539 static void LISTVIEW_GetItemBox(const LISTVIEW_INFO *infoPtr, INT nItem, LPRECT lprcBox)
2541 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
2542 POINT Position, Origin;
2545 LISTVIEW_GetOrigin(infoPtr, &Origin);
2546 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
2548 /* Be smart and try to figure out the minimum we have to do */
2550 if (infoPtr->uView == LV_VIEW_ICON && infoPtr->bFocus && LISTVIEW_GetItemState(infoPtr, nItem, LVIS_FOCUSED))
2551 lvItem.mask |= LVIF_TEXT;
2552 lvItem.iItem = nItem;
2553 lvItem.iSubItem = 0;
2554 lvItem.pszText = szDispText;
2555 lvItem.cchTextMax = DISP_TEXT_SIZE;
2556 if (lvItem.mask) LISTVIEW_GetItemW(infoPtr, &lvItem);
2557 if (infoPtr->uView == LV_VIEW_ICON)
2559 lvItem.mask |= LVIF_STATE;
2560 lvItem.stateMask = LVIS_FOCUSED;
2561 lvItem.state = (lvItem.mask & LVIF_TEXT ? LVIS_FOCUSED : 0);
2563 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, lprcBox, 0, 0, 0, 0);
2565 if (infoPtr->uView == LV_VIEW_DETAILS && infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT &&
2566 SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, 0, 0))
2568 OffsetRect(lprcBox, Origin.x, Position.y + Origin.y);
2571 OffsetRect(lprcBox, Position.x + Origin.x, Position.y + Origin.y);
2574 /* LISTVIEW_MapIdToIndex helper */
2575 static INT CALLBACK MapIdSearchCompare(LPVOID p1, LPVOID p2, LPARAM lParam)
2577 ITEM_ID *id1 = (ITEM_ID*)p1;
2578 ITEM_ID *id2 = (ITEM_ID*)p2;
2580 if (id1->id == id2->id) return 0;
2582 return (id1->id < id2->id) ? -1 : 1;
2587 * Returns the item index for id specified.
2590 * [I] infoPtr : valid pointer to the listview structure
2591 * [I] iID : item id to get index for
2594 * Item index, or -1 on failure.
2596 static INT LISTVIEW_MapIdToIndex(const LISTVIEW_INFO *infoPtr, UINT iID)
2601 TRACE("iID=%d\n", iID);
2603 if (infoPtr->dwStyle & LVS_OWNERDATA) return -1;
2604 if (infoPtr->nItemCount == 0) return -1;
2607 index = DPA_Search(infoPtr->hdpaItemIds, &ID, -1, MapIdSearchCompare, 0, DPAS_SORTED);
2611 ITEM_ID *lpID = DPA_GetPtr(infoPtr->hdpaItemIds, index);
2612 return DPA_GetPtrIndex(infoPtr->hdpaItems, lpID->item);
2620 * Returns the item id for index given.
2623 * [I] infoPtr : valid pointer to the listview structure
2624 * [I] iItem : item index to get id for
2629 static DWORD LISTVIEW_MapIndexToId(const LISTVIEW_INFO *infoPtr, INT iItem)
2634 TRACE("iItem=%d\n", iItem);
2636 if (infoPtr->dwStyle & LVS_OWNERDATA) return -1;
2637 if (iItem < 0 || iItem >= infoPtr->nItemCount) return -1;
2639 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, iItem);
2640 lpItem = DPA_GetPtr(hdpaSubItems, 0);
2642 return lpItem->id->id;
2647 * Returns the current icon position, and advances it along the top.
2648 * The returned position is not offset by Origin.
2651 * [I] infoPtr : valid pointer to the listview structure
2652 * [O] lpPos : will get the current icon position
2657 static void LISTVIEW_NextIconPosTop(LISTVIEW_INFO *infoPtr, LPPOINT lpPos)
2659 INT nListWidth = infoPtr->rcList.right - infoPtr->rcList.left;
2661 *lpPos = infoPtr->currIconPos;
2663 infoPtr->currIconPos.x += infoPtr->nItemWidth;
2664 if (infoPtr->currIconPos.x + infoPtr->nItemWidth <= nListWidth) return;
2666 infoPtr->currIconPos.x = 0;
2667 infoPtr->currIconPos.y += infoPtr->nItemHeight;
2673 * Returns the current icon position, and advances it down the left edge.
2674 * The returned position is not offset by Origin.
2677 * [I] infoPtr : valid pointer to the listview structure
2678 * [O] lpPos : will get the current icon position
2683 static void LISTVIEW_NextIconPosLeft(LISTVIEW_INFO *infoPtr, LPPOINT lpPos)
2685 INT nListHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
2687 *lpPos = infoPtr->currIconPos;
2689 infoPtr->currIconPos.y += infoPtr->nItemHeight;
2690 if (infoPtr->currIconPos.y + infoPtr->nItemHeight <= nListHeight) return;
2692 infoPtr->currIconPos.x += infoPtr->nItemWidth;
2693 infoPtr->currIconPos.y = 0;
2699 * Moves an icon to the specified position.
2700 * It takes care of invalidating the item, etc.
2703 * [I] infoPtr : valid pointer to the listview structure
2704 * [I] nItem : the item to move
2705 * [I] lpPos : the new icon position
2706 * [I] isNew : flags the item as being new
2712 static BOOL LISTVIEW_MoveIconTo(const LISTVIEW_INFO *infoPtr, INT nItem, const POINT *lppt, BOOL isNew)
2718 old.x = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosX, nItem);
2719 old.y = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosY, nItem);
2721 if (lppt->x == old.x && lppt->y == old.y) return TRUE;
2722 LISTVIEW_InvalidateItem(infoPtr, nItem);
2725 /* Allocating a POINTER for every item is too resource intensive,
2726 * so we'll keep the (x,y) in different arrays */
2727 if (!DPA_SetPtr(infoPtr->hdpaPosX, nItem, (void *)(LONG_PTR)lppt->x)) return FALSE;
2728 if (!DPA_SetPtr(infoPtr->hdpaPosY, nItem, (void *)(LONG_PTR)lppt->y)) return FALSE;
2730 LISTVIEW_InvalidateItem(infoPtr, nItem);
2737 * Arranges listview items in icon display mode.
2740 * [I] infoPtr : valid pointer to the listview structure
2741 * [I] nAlignCode : alignment code
2747 static BOOL LISTVIEW_Arrange(LISTVIEW_INFO *infoPtr, INT nAlignCode)
2749 void (*next_pos)(LISTVIEW_INFO *, LPPOINT);
2753 if (infoPtr->uView != LV_VIEW_ICON && infoPtr->uView != LV_VIEW_SMALLICON) return FALSE;
2755 TRACE("nAlignCode=%d\n", nAlignCode);
2757 if (nAlignCode == LVA_DEFAULT)
2759 if (infoPtr->dwStyle & LVS_ALIGNLEFT) nAlignCode = LVA_ALIGNLEFT;
2760 else nAlignCode = LVA_ALIGNTOP;
2765 case LVA_ALIGNLEFT: next_pos = LISTVIEW_NextIconPosLeft; break;
2766 case LVA_ALIGNTOP: next_pos = LISTVIEW_NextIconPosTop; break;
2767 case LVA_SNAPTOGRID: next_pos = LISTVIEW_NextIconPosTop; break; /* FIXME */
2768 default: return FALSE;
2771 infoPtr->bAutoarrange = TRUE;
2772 infoPtr->currIconPos.x = infoPtr->currIconPos.y = 0;
2773 for (i = 0; i < infoPtr->nItemCount; i++)
2775 next_pos(infoPtr, &pos);
2776 LISTVIEW_MoveIconTo(infoPtr, i, &pos, FALSE);
2784 * Retrieves the bounding rectangle of all the items, not offset by Origin.
2785 * For LVS_REPORT always returns empty rectangle.
2788 * [I] infoPtr : valid pointer to the listview structure
2789 * [O] lprcView : bounding rectangle
2795 static void LISTVIEW_GetAreaRect(const LISTVIEW_INFO *infoPtr, LPRECT lprcView)
2799 SetRectEmpty(lprcView);
2801 switch (infoPtr->uView)
2804 case LV_VIEW_SMALLICON:
2805 for (i = 0; i < infoPtr->nItemCount; i++)
2807 x = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosX, i);
2808 y = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosY, i);
2809 lprcView->right = max(lprcView->right, x);
2810 lprcView->bottom = max(lprcView->bottom, y);
2812 if (infoPtr->nItemCount > 0)
2814 lprcView->right += infoPtr->nItemWidth;
2815 lprcView->bottom += infoPtr->nItemHeight;
2820 y = LISTVIEW_GetCountPerColumn(infoPtr);
2821 x = infoPtr->nItemCount / y;
2822 if (infoPtr->nItemCount % y) x++;
2823 lprcView->right = x * infoPtr->nItemWidth;
2824 lprcView->bottom = y * infoPtr->nItemHeight;
2831 * Retrieves the bounding rectangle of all the items.
2834 * [I] infoPtr : valid pointer to the listview structure
2835 * [O] lprcView : bounding rectangle
2841 static BOOL LISTVIEW_GetViewRect(const LISTVIEW_INFO *infoPtr, LPRECT lprcView)
2845 TRACE("(lprcView=%p)\n", lprcView);
2847 if (!lprcView) return FALSE;
2849 LISTVIEW_GetAreaRect(infoPtr, lprcView);
2851 if (infoPtr->uView != LV_VIEW_DETAILS)
2853 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
2854 OffsetRect(lprcView, ptOrigin.x, ptOrigin.y);
2857 TRACE("lprcView=%s\n", wine_dbgstr_rect(lprcView));
2864 * Retrieves the subitem pointer associated with the subitem index.
2867 * [I] hdpaSubItems : DPA handle for a specific item
2868 * [I] nSubItem : index of subitem
2871 * SUCCESS : subitem pointer
2874 static SUBITEM_INFO* LISTVIEW_GetSubItemPtr(HDPA hdpaSubItems, INT nSubItem)
2876 SUBITEM_INFO *lpSubItem;
2879 /* we should binary search here if need be */
2880 for (i = 1; i < DPA_GetPtrCount(hdpaSubItems); i++)
2882 lpSubItem = DPA_GetPtr(hdpaSubItems, i);
2883 if (lpSubItem->iSubItem == nSubItem)
2893 * Calculates the desired item width.
2896 * [I] infoPtr : valid pointer to the listview structure
2899 * The desired item width.
2901 static INT LISTVIEW_CalculateItemWidth(const LISTVIEW_INFO *infoPtr)
2905 TRACE("uView=%d\n", infoPtr->uView);
2907 if (infoPtr->uView == LV_VIEW_ICON)
2908 nItemWidth = infoPtr->iconSpacing.cx;
2909 else if (infoPtr->uView == LV_VIEW_DETAILS)
2911 if (DPA_GetPtrCount(infoPtr->hdpaColumns) > 0)
2916 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX,
2917 DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, 0);
2919 LISTVIEW_GetHeaderRect(infoPtr, index, &rcHeader);
2920 nItemWidth = rcHeader.right;
2923 else /* LV_VIEW_SMALLICON, or LV_VIEW_LIST */
2925 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
2929 lvItem.mask = LVIF_TEXT;
2930 lvItem.iSubItem = 0;
2932 for (i = 0; i < infoPtr->nItemCount; i++)
2935 lvItem.pszText = szDispText;
2936 lvItem.cchTextMax = DISP_TEXT_SIZE;
2937 if (LISTVIEW_GetItemW(infoPtr, &lvItem))
2938 nItemWidth = max(LISTVIEW_GetStringWidthT(infoPtr, lvItem.pszText, TRUE),
2942 if (infoPtr->himlSmall) nItemWidth += infoPtr->iconSize.cx;
2943 if (infoPtr->himlState) nItemWidth += infoPtr->iconStateSize.cx;
2945 nItemWidth = max(DEFAULT_COLUMN_WIDTH, nItemWidth + WIDTH_PADDING);
2953 * Calculates the desired item height.
2956 * [I] infoPtr : valid pointer to the listview structure
2959 * The desired item height.
2961 static INT LISTVIEW_CalculateItemHeight(const LISTVIEW_INFO *infoPtr)
2965 TRACE("uView=%d\n", infoPtr->uView);
2967 if (infoPtr->uView == LV_VIEW_ICON)
2968 nItemHeight = infoPtr->iconSpacing.cy;
2971 nItemHeight = infoPtr->ntmHeight;
2972 if (infoPtr->himlState)
2973 nItemHeight = max(nItemHeight, infoPtr->iconStateSize.cy);
2974 if (infoPtr->himlSmall)
2975 nItemHeight = max(nItemHeight, infoPtr->iconSize.cy);
2976 nItemHeight += HEIGHT_PADDING;
2977 if (infoPtr->nMeasureItemHeight > 0)
2978 nItemHeight = infoPtr->nMeasureItemHeight;
2981 return max(nItemHeight, 1);
2986 * Updates the width, and height of an item.
2989 * [I] infoPtr : valid pointer to the listview structure
2994 static inline void LISTVIEW_UpdateItemSize(LISTVIEW_INFO *infoPtr)
2996 infoPtr->nItemWidth = LISTVIEW_CalculateItemWidth(infoPtr);
2997 infoPtr->nItemHeight = LISTVIEW_CalculateItemHeight(infoPtr);
3003 * Retrieves and saves important text metrics info for the current
3007 * [I] infoPtr : valid pointer to the listview structure
3010 static void LISTVIEW_SaveTextMetrics(LISTVIEW_INFO *infoPtr)
3012 HDC hdc = GetDC(infoPtr->hwndSelf);
3013 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
3014 HFONT hOldFont = SelectObject(hdc, hFont);
3018 if (GetTextMetricsW(hdc, &tm))
3020 infoPtr->ntmHeight = tm.tmHeight;
3021 infoPtr->ntmMaxCharWidth = tm.tmMaxCharWidth;
3024 if (GetTextExtentPoint32A(hdc, "...", 3, &sz))
3025 infoPtr->nEllipsisWidth = sz.cx;
3027 SelectObject(hdc, hOldFont);
3028 ReleaseDC(infoPtr->hwndSelf, hdc);
3030 TRACE("tmHeight=%d\n", infoPtr->ntmHeight);
3035 * A compare function for ranges
3038 * [I] range1 : pointer to range 1;
3039 * [I] range2 : pointer to range 2;
3043 * > 0 : if range 1 > range 2
3044 * < 0 : if range 2 > range 1
3045 * = 0 : if range intersects range 2
3047 static INT CALLBACK ranges_cmp(LPVOID range1, LPVOID range2, LPARAM flags)
3051 if (((RANGE*)range1)->upper <= ((RANGE*)range2)->lower)
3053 else if (((RANGE*)range2)->upper <= ((RANGE*)range1)->lower)
3058 TRACE("range1=%s, range2=%s, cmp=%d\n", debugrange(range1), debugrange(range2), cmp);
3063 #define ranges_check(ranges, desc) if (TRACE_ON(listview)) ranges_assert(ranges, desc, __FILE__, __LINE__)
3065 static void ranges_assert(RANGES ranges, LPCSTR desc, const char *file, int line)
3070 TRACE("*** Checking %s:%d:%s ***\n", file, line, desc);
3072 assert (DPA_GetPtrCount(ranges->hdpa) >= 0);
3073 ranges_dump(ranges);
3074 if (DPA_GetPtrCount(ranges->hdpa) > 0)
3076 prev = DPA_GetPtr(ranges->hdpa, 0);
3077 assert (prev->lower >= 0 && prev->lower < prev->upper);
3078 for (i = 1; i < DPA_GetPtrCount(ranges->hdpa); i++)
3080 curr = DPA_GetPtr(ranges->hdpa, i);
3081 assert (prev->upper <= curr->lower);
3082 assert (curr->lower < curr->upper);
3086 TRACE("--- Done checking---\n");
3089 static RANGES ranges_create(int count)
3091 RANGES ranges = Alloc(sizeof(struct tagRANGES));
3092 if (!ranges) return NULL;
3093 ranges->hdpa = DPA_Create(count);
3094 if (ranges->hdpa) return ranges;
3099 static void ranges_clear(RANGES ranges)
3103 for(i = 0; i < DPA_GetPtrCount(ranges->hdpa); i++)
3104 Free(DPA_GetPtr(ranges->hdpa, i));
3105 DPA_DeleteAllPtrs(ranges->hdpa);
3109 static void ranges_destroy(RANGES ranges)
3111 if (!ranges) return;
3112 ranges_clear(ranges);
3113 DPA_Destroy(ranges->hdpa);
3117 static RANGES ranges_clone(RANGES ranges)
3122 if (!(clone = ranges_create(DPA_GetPtrCount(ranges->hdpa)))) goto fail;
3124 for (i = 0; i < DPA_GetPtrCount(ranges->hdpa); i++)
3126 RANGE *newrng = Alloc(sizeof(RANGE));
3127 if (!newrng) goto fail;
3128 *newrng = *((RANGE*)DPA_GetPtr(ranges->hdpa, i));
3129 DPA_SetPtr(clone->hdpa, i, newrng);
3134 TRACE ("clone failed\n");
3135 ranges_destroy(clone);
3139 static RANGES ranges_diff(RANGES ranges, RANGES sub)
3143 for (i = 0; i < DPA_GetPtrCount(sub->hdpa); i++)
3144 ranges_del(ranges, *((RANGE *)DPA_GetPtr(sub->hdpa, i)));
3149 static void ranges_dump(RANGES ranges)
3153 for (i = 0; i < DPA_GetPtrCount(ranges->hdpa); i++)
3154 TRACE(" %s\n", debugrange(DPA_GetPtr(ranges->hdpa, i)));
3157 static inline BOOL ranges_contain(RANGES ranges, INT nItem)
3159 RANGE srchrng = { nItem, nItem + 1 };
3161 TRACE("(nItem=%d)\n", nItem);
3162 ranges_check(ranges, "before contain");
3163 return DPA_Search(ranges->hdpa, &srchrng, 0, ranges_cmp, 0, DPAS_SORTED) != -1;
3166 static INT ranges_itemcount(RANGES ranges)
3170 for (i = 0; i < DPA_GetPtrCount(ranges->hdpa); i++)
3172 RANGE *sel = DPA_GetPtr(ranges->hdpa, i);
3173 count += sel->upper - sel->lower;
3179 static BOOL ranges_shift(RANGES ranges, INT nItem, INT delta, INT nUpper)
3181 RANGE srchrng = { nItem, nItem + 1 }, *chkrng;
3184 index = DPA_Search(ranges->hdpa, &srchrng, 0, ranges_cmp, 0, DPAS_SORTED | DPAS_INSERTAFTER);
3185 if (index == -1) return TRUE;
3187 for (; index < DPA_GetPtrCount(ranges->hdpa); index++)
3189 chkrng = DPA_GetPtr(ranges->hdpa, index);
3190 if (chkrng->lower >= nItem)
3191 chkrng->lower = max(min(chkrng->lower + delta, nUpper - 1), 0);
3192 if (chkrng->upper > nItem)
3193 chkrng->upper = max(min(chkrng->upper + delta, nUpper), 0);
3198 static BOOL ranges_add(RANGES ranges, RANGE range)
3203 TRACE("(%s)\n", debugrange(&range));
3204 ranges_check(ranges, "before add");
3206 /* try find overlapping regions first */
3207 srchrgn.lower = range.lower - 1;
3208 srchrgn.upper = range.upper + 1;
3209 index = DPA_Search(ranges->hdpa, &srchrgn, 0, ranges_cmp, 0, DPAS_SORTED);
3215 TRACE("Adding new range\n");
3217 /* create the brand new range to insert */
3218 newrgn = Alloc(sizeof(RANGE));
3219 if(!newrgn) goto fail;
3222 /* figure out where to insert it */
3223 index = DPA_Search(ranges->hdpa, newrgn, 0, ranges_cmp, 0, DPAS_SORTED | DPAS_INSERTAFTER);
3224 TRACE("index=%d\n", index);
3225 if (index == -1) index = 0;
3227 /* and get it over with */
3228 if (DPA_InsertPtr(ranges->hdpa, index, newrgn) == -1)
3236 RANGE *chkrgn, *mrgrgn;
3237 INT fromindex, mergeindex;
3239 chkrgn = DPA_GetPtr(ranges->hdpa, index);
3240 TRACE("Merge with %s @%d\n", debugrange(chkrgn), index);
3242 chkrgn->lower = min(range.lower, chkrgn->lower);
3243 chkrgn->upper = max(range.upper, chkrgn->upper);
3245 TRACE("New range %s @%d\n", debugrange(chkrgn), index);
3247 /* merge now common ranges */
3249 srchrgn.lower = chkrgn->lower - 1;
3250 srchrgn.upper = chkrgn->upper + 1;
3254 mergeindex = DPA_Search(ranges->hdpa, &srchrgn, fromindex, ranges_cmp, 0, 0);
3255 if (mergeindex == -1) break;
3256 if (mergeindex == index)
3258 fromindex = index + 1;
3262 TRACE("Merge with index %i\n", mergeindex);
3264 mrgrgn = DPA_GetPtr(ranges->hdpa, mergeindex);
3265 chkrgn->lower = min(chkrgn->lower, mrgrgn->lower);
3266 chkrgn->upper = max(chkrgn->upper, mrgrgn->upper);
3268 DPA_DeletePtr(ranges->hdpa, mergeindex);
3269 if (mergeindex < index) index --;
3273 ranges_check(ranges, "after add");
3277 ranges_check(ranges, "failed add");
3281 static BOOL ranges_del(RANGES ranges, RANGE range)
3286 TRACE("(%s)\n", debugrange(&range));
3287 ranges_check(ranges, "before del");
3289 /* we don't use DPAS_SORTED here, since we need *
3290 * to find the first overlapping range */
3291 index = DPA_Search(ranges->hdpa, &range, 0, ranges_cmp, 0, 0);
3294 chkrgn = DPA_GetPtr(ranges->hdpa, index);
3296 TRACE("Matches range %s @%d\n", debugrange(chkrgn), index);
3298 /* case 1: Same range */
3299 if ( (chkrgn->upper == range.upper) &&
3300 (chkrgn->lower == range.lower) )
3302 DPA_DeletePtr(ranges->hdpa, index);
3306 /* case 2: engulf */
3307 else if ( (chkrgn->upper <= range.upper) &&
3308 (chkrgn->lower >= range.lower) )
3310 DPA_DeletePtr(ranges->hdpa, index);
3313 /* case 3: overlap upper */
3314 else if ( (chkrgn->upper <= range.upper) &&
3315 (chkrgn->lower < range.lower) )
3317 chkrgn->upper = range.lower;
3319 /* case 4: overlap lower */
3320 else if ( (chkrgn->upper > range.upper) &&
3321 (chkrgn->lower >= range.lower) )
3323 chkrgn->lower = range.upper;
3326 /* case 5: fully internal */
3331 if (!(newrgn = Alloc(sizeof(RANGE)))) goto fail;
3332 newrgn->lower = chkrgn->lower;
3333 newrgn->upper = range.lower;
3334 chkrgn->lower = range.upper;
3335 if (DPA_InsertPtr(ranges->hdpa, index, newrgn) == -1)
3343 index = DPA_Search(ranges->hdpa, &range, index, ranges_cmp, 0, 0);
3346 ranges_check(ranges, "after del");
3350 ranges_check(ranges, "failed del");
3356 * Removes all selection ranges
3359 * [I] infoPtr : valid pointer to the listview structure
3360 * [I] toSkip : item range to skip removing the selection
3366 static BOOL LISTVIEW_DeselectAllSkipItems(LISTVIEW_INFO *infoPtr, RANGES toSkip)
3375 lvItem.stateMask = LVIS_SELECTED;
3377 /* need to clone the DPA because callbacks can change it */
3378 if (!(clone = ranges_clone(infoPtr->selectionRanges))) return FALSE;
3379 iterator_rangesitems(&i, ranges_diff(clone, toSkip));
3380 while(iterator_next(&i))
3381 LISTVIEW_SetItemState(infoPtr, i.nItem, &lvItem);
3382 /* note that the iterator destructor will free the cloned range */
3383 iterator_destroy(&i);
3388 static inline BOOL LISTVIEW_DeselectAllSkipItem(LISTVIEW_INFO *infoPtr, INT nItem)
3392 if (!(toSkip = ranges_create(1))) return FALSE;
3393 if (nItem != -1) ranges_additem(toSkip, nItem);
3394 LISTVIEW_DeselectAllSkipItems(infoPtr, toSkip);
3395 ranges_destroy(toSkip);
3399 static inline BOOL LISTVIEW_DeselectAll(LISTVIEW_INFO *infoPtr)
3401 return LISTVIEW_DeselectAllSkipItem(infoPtr, -1);
3406 * Retrieves the number of items that are marked as selected.
3409 * [I] infoPtr : valid pointer to the listview structure
3412 * Number of items selected.
3414 static INT LISTVIEW_GetSelectedCount(const LISTVIEW_INFO *infoPtr)
3416 INT nSelectedCount = 0;
3418 if (infoPtr->uCallbackMask & LVIS_SELECTED)
3421 for (i = 0; i < infoPtr->nItemCount; i++)
3423 if (LISTVIEW_GetItemState(infoPtr, i, LVIS_SELECTED))
3428 nSelectedCount = ranges_itemcount(infoPtr->selectionRanges);
3430 TRACE("nSelectedCount=%d\n", nSelectedCount);
3431 return nSelectedCount;
3436 * Manages the item focus.
3439 * [I] infoPtr : valid pointer to the listview structure
3440 * [I] nItem : item index
3443 * TRUE : focused item changed
3444 * FALSE : focused item has NOT changed
3446 static inline BOOL LISTVIEW_SetItemFocus(LISTVIEW_INFO *infoPtr, INT nItem)
3448 INT oldFocus = infoPtr->nFocusedItem;
3451 if (nItem == infoPtr->nFocusedItem) return FALSE;
3453 lvItem.state = nItem == -1 ? 0 : LVIS_FOCUSED;
3454 lvItem.stateMask = LVIS_FOCUSED;
3455 LISTVIEW_SetItemState(infoPtr, nItem == -1 ? infoPtr->nFocusedItem : nItem, &lvItem);
3457 return oldFocus != infoPtr->nFocusedItem;
3460 static INT shift_item(const LISTVIEW_INFO *infoPtr, INT nShiftItem, INT nItem, INT direction)
3462 if (nShiftItem < nItem) return nShiftItem;
3464 if (nShiftItem > nItem) return nShiftItem + direction;
3466 if (direction > 0) return nShiftItem + direction;
3468 return min(nShiftItem, infoPtr->nItemCount - 1);
3471 /* This function updates focus index.
3474 focus : current focus index
3475 item : index of item to be added/removed
3476 direction : add/remove flag
3478 static void LISTVIEW_ShiftFocus(LISTVIEW_INFO *infoPtr, INT focus, INT item, INT direction)
3480 BOOL old_change = infoPtr->bDoChangeNotify;
3482 infoPtr->bDoChangeNotify = FALSE;
3483 focus = shift_item(infoPtr, focus, item, direction);
3484 if (focus != infoPtr->nFocusedItem)
3485 LISTVIEW_SetItemFocus(infoPtr, focus);
3486 infoPtr->bDoChangeNotify = old_change;
3491 * Updates the various indices after an item has been inserted or deleted.
3494 * [I] infoPtr : valid pointer to the listview structure
3495 * [I] nItem : item index
3496 * [I] direction : Direction of shift, +1 or -1.
3501 static void LISTVIEW_ShiftIndices(LISTVIEW_INFO *infoPtr, INT nItem, INT direction)
3503 TRACE("Shifting %i, %i steps\n", nItem, direction);
3505 ranges_shift(infoPtr->selectionRanges, nItem, direction, infoPtr->nItemCount);
3506 assert(abs(direction) == 1);
3507 infoPtr->nSelectionMark = shift_item(infoPtr, infoPtr->nSelectionMark, nItem, direction);
3509 /* But we are not supposed to modify nHotItem! */
3514 * Adds a block of selections.
3517 * [I] infoPtr : valid pointer to the listview structure
3518 * [I] nItem : item index
3521 * Whether the window is still valid.
3523 static BOOL LISTVIEW_AddGroupSelection(LISTVIEW_INFO *infoPtr, INT nItem)
3525 INT nFirst = min(infoPtr->nSelectionMark, nItem);
3526 INT nLast = max(infoPtr->nSelectionMark, nItem);
3527 HWND hwndSelf = infoPtr->hwndSelf;
3528 NMLVODSTATECHANGE nmlv;
3533 /* Temporarily disable change notification
3534 * If the control is LVS_OWNERDATA, we need to send
3535 * only one LVN_ODSTATECHANGED notification.
3536 * See MSDN documentation for LVN_ITEMCHANGED.
3538 bOldChange = infoPtr->bDoChangeNotify;
3539 if (infoPtr->dwStyle & LVS_OWNERDATA) infoPtr->bDoChangeNotify = FALSE;
3541 if (nFirst == -1) nFirst = nItem;
3543 item.state = LVIS_SELECTED;
3544 item.stateMask = LVIS_SELECTED;
3546 for (i = nFirst; i <= nLast; i++)
3547 LISTVIEW_SetItemState(infoPtr,i,&item);
3549 ZeroMemory(&nmlv, sizeof(nmlv));
3550 nmlv.iFrom = nFirst;
3553 nmlv.uOldState = item.state;
3555 notify_hdr(infoPtr, LVN_ODSTATECHANGED, (LPNMHDR)&nmlv);
3556 if (!IsWindow(hwndSelf))
3558 infoPtr->bDoChangeNotify = bOldChange;
3565 * Sets a single group selection.
3568 * [I] infoPtr : valid pointer to the listview structure
3569 * [I] nItem : item index
3574 static void LISTVIEW_SetGroupSelection(LISTVIEW_INFO *infoPtr, INT nItem)
3581 if (!(selection = ranges_create(100))) return;
3583 item.state = LVIS_SELECTED;
3584 item.stateMask = LVIS_SELECTED;
3586 if ((infoPtr->uView == LV_VIEW_LIST) || (infoPtr->uView == LV_VIEW_DETAILS))
3588 if (infoPtr->nSelectionMark == -1)
3590 infoPtr->nSelectionMark = nItem;
3591 ranges_additem(selection, nItem);
3597 sel.lower = min(infoPtr->nSelectionMark, nItem);
3598 sel.upper = max(infoPtr->nSelectionMark, nItem) + 1;
3599 ranges_add(selection, sel);
3604 RECT rcItem, rcSel, rcSelMark;
3607 rcItem.left = LVIR_BOUNDS;
3608 if (!LISTVIEW_GetItemRect(infoPtr, nItem, &rcItem)) {
3609 ranges_destroy (selection);
3612 rcSelMark.left = LVIR_BOUNDS;
3613 if (!LISTVIEW_GetItemRect(infoPtr, infoPtr->nSelectionMark, &rcSelMark)) {
3614 ranges_destroy (selection);
3617 UnionRect(&rcSel, &rcItem, &rcSelMark);
3618 iterator_frameditems(&i, infoPtr, &rcSel);
3619 while(iterator_next(&i))
3621 LISTVIEW_GetItemPosition(infoPtr, i.nItem, &ptItem);
3622 if (PtInRect(&rcSel, ptItem)) ranges_additem(selection, i.nItem);
3624 iterator_destroy(&i);
3627 /* disable per item notifications on LVS_OWNERDATA style
3628 FIXME: single LVN_ODSTATECHANGED should be used */
3629 bOldChange = infoPtr->bDoChangeNotify;
3630 if (infoPtr->dwStyle & LVS_OWNERDATA) infoPtr->bDoChangeNotify = FALSE;
3632 LISTVIEW_DeselectAllSkipItems(infoPtr, selection);
3635 iterator_rangesitems(&i, selection);
3636 while(iterator_next(&i))
3637 LISTVIEW_SetItemState(infoPtr, i.nItem, &item);
3638 /* this will also destroy the selection */
3639 iterator_destroy(&i);
3641 infoPtr->bDoChangeNotify = bOldChange;
3643 LISTVIEW_SetItemFocus(infoPtr, nItem);
3648 * Sets a single selection.
3651 * [I] infoPtr : valid pointer to the listview structure
3652 * [I] nItem : item index
3657 static void LISTVIEW_SetSelection(LISTVIEW_INFO *infoPtr, INT nItem)
3661 TRACE("nItem=%d\n", nItem);
3663 LISTVIEW_DeselectAllSkipItem(infoPtr, nItem);
3665 lvItem.state = LVIS_FOCUSED | LVIS_SELECTED;
3666 lvItem.stateMask = LVIS_FOCUSED | LVIS_SELECTED;
3667 LISTVIEW_SetItemState(infoPtr, nItem, &lvItem);
3669 infoPtr->nSelectionMark = nItem;
3674 * Set selection(s) with keyboard.
3677 * [I] infoPtr : valid pointer to the listview structure
3678 * [I] nItem : item index
3679 * [I] space : VK_SPACE code sent
3682 * SUCCESS : TRUE (needs to be repainted)
3683 * FAILURE : FALSE (nothing has changed)
3685 static BOOL LISTVIEW_KeySelection(LISTVIEW_INFO *infoPtr, INT nItem, BOOL space)
3687 /* FIXME: pass in the state */
3688 WORD wShift = HIWORD(GetKeyState(VK_SHIFT));
3689 WORD wCtrl = HIWORD(GetKeyState(VK_CONTROL));
3690 BOOL bResult = FALSE;
3692 TRACE("nItem=%d, wShift=%d, wCtrl=%d\n", nItem, wShift, wCtrl);
3693 if ((nItem >= 0) && (nItem < infoPtr->nItemCount))
3697 if (infoPtr->dwStyle & LVS_SINGLESEL || (wShift == 0 && wCtrl == 0))
3698 LISTVIEW_SetSelection(infoPtr, nItem);
3702 LISTVIEW_SetGroupSelection(infoPtr, nItem);
3706 lvItem.state = ~LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED);
3707 lvItem.stateMask = LVIS_SELECTED;
3710 LISTVIEW_SetItemState(infoPtr, nItem, &lvItem);
3711 if (lvItem.state & LVIS_SELECTED)
3712 infoPtr->nSelectionMark = nItem;
3714 bResult = LISTVIEW_SetItemFocus(infoPtr, nItem);
3717 LISTVIEW_EnsureVisible(infoPtr, nItem, FALSE);
3720 UpdateWindow(infoPtr->hwndSelf); /* update client area */
3724 static BOOL LISTVIEW_GetItemAtPt(const LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem, POINT pt)
3726 LVHITTESTINFO lvHitTestInfo;
3728 ZeroMemory(&lvHitTestInfo, sizeof(lvHitTestInfo));
3729 lvHitTestInfo.pt.x = pt.x;
3730 lvHitTestInfo.pt.y = pt.y;
3732 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
3734 lpLVItem->mask = LVIF_PARAM;
3735 lpLVItem->iItem = lvHitTestInfo.iItem;
3736 lpLVItem->iSubItem = 0;
3738 return LISTVIEW_GetItemT(infoPtr, lpLVItem, TRUE);
3741 static inline BOOL LISTVIEW_IsHotTracking(const LISTVIEW_INFO *infoPtr)
3743 return ((infoPtr->dwLvExStyle & LVS_EX_TRACKSELECT) ||
3744 (infoPtr->dwLvExStyle & LVS_EX_ONECLICKACTIVATE) ||
3745 (infoPtr->dwLvExStyle & LVS_EX_TWOCLICKACTIVATE));
3750 * Called when the mouse is being actively tracked and has hovered for a specified
3754 * [I] infoPtr : valid pointer to the listview structure
3755 * [I] fwKeys : key indicator
3756 * [I] x,y : mouse position
3759 * 0 if the message was processed, non-zero if there was an error
3762 * LVS_EX_TRACKSELECT: An item is automatically selected when the cursor remains
3763 * over the item for a certain period of time.
3766 static LRESULT LISTVIEW_MouseHover(LISTVIEW_INFO *infoPtr, INT x, INT y)
3770 if (notify_hdr(infoPtr, NM_HOVER, &hdr)) return 0;
3772 if (LISTVIEW_IsHotTracking(infoPtr))
3780 if (LISTVIEW_GetItemAtPt(infoPtr, &item, pt))
3781 LISTVIEW_SetSelection(infoPtr, item.iItem);
3783 SetFocus(infoPtr->hwndSelf);
3789 #define SCROLL_LEFT 0x1
3790 #define SCROLL_RIGHT 0x2
3791 #define SCROLL_UP 0x4
3792 #define SCROLL_DOWN 0x8
3796 * Utility routine to draw and highlight items within a marquee selection rectangle.
3799 * [I] infoPtr : valid pointer to the listview structure
3800 * [I] coords_orig : original co-ordinates of the cursor
3801 * [I] coords_offs : offsetted coordinates of the cursor
3802 * [I] offset : offset amount
3803 * [I] scroll : Bitmask of which directions we should scroll, if at all
3808 static void LISTVIEW_MarqueeHighlight(LISTVIEW_INFO *infoPtr, const POINT *coords_orig,
3809 const POINT *coords_offs, const POINT *offset,
3812 BOOL controlDown = FALSE;
3814 ITERATOR old_elems, new_elems;
3817 if (coords_offs->x > infoPtr->marqueeOrigin.x)
3819 rect.left = infoPtr->marqueeOrigin.x;
3820 rect.right = coords_offs->x;
3824 rect.left = coords_offs->x;
3825 rect.right = infoPtr->marqueeOrigin.x;
3828 if (coords_offs->y > infoPtr->marqueeOrigin.y)
3830 rect.top = infoPtr->marqueeOrigin.y;
3831 rect.bottom = coords_offs->y;
3835 rect.top = coords_offs->y;
3836 rect.bottom = infoPtr->marqueeOrigin.y;
3839 /* Cancel out the old marquee rectangle and draw the new one */
3840 LISTVIEW_InvalidateRect(infoPtr, &infoPtr->marqueeDrawRect);
3842 /* Scroll by the appropriate distance if applicable - speed up scrolling as
3843 the cursor is further away */
3845 if ((scroll & SCROLL_LEFT) && (coords_orig->x <= 0))
3846 LISTVIEW_Scroll(infoPtr, coords_orig->x, 0);
3848 if ((scroll & SCROLL_RIGHT) && (coords_orig->x >= infoPtr->rcList.right))
3849 LISTVIEW_Scroll(infoPtr, (coords_orig->x - infoPtr->rcList.right), 0);
3851 if ((scroll & SCROLL_UP) && (coords_orig->y <= 0))
3852 LISTVIEW_Scroll(infoPtr, 0, coords_orig->y);
3854 if ((scroll & SCROLL_DOWN) && (coords_orig->y >= infoPtr->rcList.bottom))
3855 LISTVIEW_Scroll(infoPtr, 0, (coords_orig->y - infoPtr->rcList.bottom));
3857 iterator_frameditems_absolute(&old_elems, infoPtr, &infoPtr->marqueeRect);
3859 CopyRect(&infoPtr->marqueeRect, &rect);
3861 CopyRect(&infoPtr->marqueeDrawRect, &rect);
3862 OffsetRect(&infoPtr->marqueeDrawRect, offset->x, offset->y);
3864 iterator_frameditems_absolute(&new_elems, infoPtr, &infoPtr->marqueeRect);
3865 iterator_remove_common_items(&old_elems, &new_elems);
3867 /* Iterate over no longer selected items */
3868 while (iterator_next(&old_elems))
3870 if (old_elems.nItem > -1)
3872 if (LISTVIEW_GetItemState(infoPtr, old_elems.nItem, LVIS_SELECTED) == LVIS_SELECTED)
3875 item.state = LVIS_SELECTED;
3877 item.stateMask = LVIS_SELECTED;
3879 LISTVIEW_SetItemState(infoPtr, old_elems.nItem, &item);
3882 iterator_destroy(&old_elems);
3885 /* Iterate over newly selected items */
3886 if (GetKeyState(VK_CONTROL) & 0x8000)
3889 while (iterator_next(&new_elems))
3891 if (new_elems.nItem > -1)
3893 /* If CTRL is pressed, invert. If not, always select the item. */
3894 if ((controlDown) && (LISTVIEW_GetItemState(infoPtr, new_elems.nItem, LVIS_SELECTED)))
3897 item.state = LVIS_SELECTED;
3899 item.stateMask = LVIS_SELECTED;
3901 LISTVIEW_SetItemState(infoPtr, new_elems.nItem, &item);
3904 iterator_destroy(&new_elems);
3906 LISTVIEW_InvalidateRect(infoPtr, &infoPtr->marqueeDrawRect);
3911 * Called when we are in a marquee selection that involves scrolling the listview (ie,
3912 * the cursor is outside the bounds of the client area). This is a TIMERPROC.
3915 * [I] hwnd : Handle to the listview
3916 * [I] uMsg : WM_TIMER (ignored)
3917 * [I] idEvent : The timer ID interpreted as a pointer to a LISTVIEW_INFO struct
3918 * [I] dwTimer : The elapsed time (ignored)
3923 static VOID CALLBACK LISTVIEW_ScrollTimer(HWND hWnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
3925 LISTVIEW_INFO *infoPtr;
3926 SCROLLINFO scrollInfo;
3932 infoPtr = (LISTVIEW_INFO *) idEvent;
3937 /* Get the current cursor position and convert to client coordinates */
3938 GetCursorPos(&coords_orig);
3939 ScreenToClient(hWnd, &coords_orig);
3941 /* Ensure coordinates are within client bounds */
3942 coords_offs.x = max(min(coords_orig.x, infoPtr->rcList.right), 0);
3943 coords_offs.y = max(min(coords_orig.y, infoPtr->rcList.bottom), 0);
3946 LISTVIEW_GetOrigin(infoPtr, &offset);
3948 /* Offset coordinates by the appropriate amount */
3949 coords_offs.x -= offset.x;
3950 coords_offs.y -= offset.y;
3952 scrollInfo.cbSize = sizeof(SCROLLINFO);
3953 scrollInfo.fMask = SIF_ALL;
3955 /* Work out in which directions we can scroll */
3956 if (GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
3958 if (scrollInfo.nPos != scrollInfo.nMin)
3959 scroll |= SCROLL_UP;
3961 if (((scrollInfo.nPage + scrollInfo.nPos) - 1) != scrollInfo.nMax)
3962 scroll |= SCROLL_DOWN;
3965 if (GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo))
3967 if (scrollInfo.nPos != scrollInfo.nMin)
3968 scroll |= SCROLL_LEFT;
3970 if (((scrollInfo.nPage + scrollInfo.nPos) - 1) != scrollInfo.nMax)
3971 scroll |= SCROLL_RIGHT;
3974 if (((coords_orig.x <= 0) && (scroll & SCROLL_LEFT)) ||
3975 ((coords_orig.y <= 0) && (scroll & SCROLL_UP)) ||
3976 ((coords_orig.x >= infoPtr->rcList.right) && (scroll & SCROLL_RIGHT)) ||
3977 ((coords_orig.y >= infoPtr->rcList.bottom) && (scroll & SCROLL_DOWN)))
3979 LISTVIEW_MarqueeHighlight(infoPtr, &coords_orig, &coords_offs, &offset, scroll);
3985 * Called whenever WM_MOUSEMOVE is received.
3988 * [I] infoPtr : valid pointer to the listview structure
3989 * [I] fwKeys : key indicator
3990 * [I] x,y : mouse position
3993 * 0 if the message is processed, non-zero if there was an error
3995 static LRESULT LISTVIEW_MouseMove(LISTVIEW_INFO *infoPtr, WORD fwKeys, INT x, INT y)
4001 if (!(fwKeys & MK_LBUTTON))
4002 infoPtr->bLButtonDown = FALSE;
4004 if (infoPtr->bLButtonDown)
4006 rect.left = rect.right = infoPtr->ptClickPos.x;
4007 rect.top = rect.bottom = infoPtr->ptClickPos.y;
4009 InflateRect(&rect, GetSystemMetrics(SM_CXDRAG), GetSystemMetrics(SM_CYDRAG));
4012 if (infoPtr->bLButtonDown)
4014 if (infoPtr->bMarqueeSelect)
4024 LISTVIEW_GetOrigin(infoPtr, &offset);
4026 /* Ensure coordinates are within client bounds */
4027 coords_offs.x = max(min(x, infoPtr->rcList.right), 0);
4028 coords_offs.y = max(min(y, infoPtr->rcList.bottom), 0);
4030 /* Offset coordinates by the appropriate amount */
4031 coords_offs.x -= offset.x;
4032 coords_offs.y -= offset.y;
4034 /* Enable the timer if we're going outside our bounds, in case the user doesn't
4035 move the mouse again */
4037 if ((x <= 0) || (y <= 0) || (x >= infoPtr->rcList.right) ||
4038 (y >= infoPtr->rcList.bottom))
4040 if (!infoPtr->bScrolling)
4042 infoPtr->bScrolling = TRUE;
4043 SetTimer(infoPtr->hwndSelf, (UINT_PTR) infoPtr, 1, LISTVIEW_ScrollTimer);
4048 infoPtr->bScrolling = FALSE;
4049 KillTimer(infoPtr->hwndSelf, (UINT_PTR) infoPtr);
4052 LISTVIEW_MarqueeHighlight(infoPtr, &coords_orig, &coords_offs, &offset, 0);
4060 LISTVIEW_HitTest(infoPtr, &ht, TRUE, TRUE);
4062 /* reset item marker */
4063 if (infoPtr->nLButtonDownItem != ht.iItem)
4064 infoPtr->nLButtonDownItem = -1;
4066 if (!PtInRect(&rect, pt))
4068 /* this path covers the following:
4069 1. WM_LBUTTONDOWN over selected item (sets focus on it)
4070 2. change focus with keys
4071 3. move mouse over item from step 1 selects it and moves focus on it */
4072 if (infoPtr->nLButtonDownItem != -1 &&
4073 !LISTVIEW_GetItemState(infoPtr, infoPtr->nLButtonDownItem, LVIS_SELECTED))
4077 lvItem.state = LVIS_FOCUSED | LVIS_SELECTED;
4078 lvItem.stateMask = LVIS_FOCUSED | LVIS_SELECTED;
4080 LISTVIEW_SetItemState(infoPtr, infoPtr->nLButtonDownItem, &lvItem);
4081 infoPtr->nLButtonDownItem = -1;
4084 if (!infoPtr->bDragging)
4086 ht.pt = infoPtr->ptClickPos;
4087 LISTVIEW_HitTest(infoPtr, &ht, TRUE, TRUE);
4089 /* If the click is outside the range of an item, begin a
4090 highlight. If not, begin an item drag. */
4095 /* If we're allowing multiple selections, send notification.
4096 If return value is non-zero, cancel. */
4097 if (!(infoPtr->dwStyle & LVS_SINGLESEL) && (notify_hdr(infoPtr, LVN_MARQUEEBEGIN, &hdr) == 0))
4099 /* Store the absolute coordinates of the click */
4101 LISTVIEW_GetOrigin(infoPtr, &offset);
4103 infoPtr->marqueeOrigin.x = infoPtr->ptClickPos.x - offset.x;
4104 infoPtr->marqueeOrigin.y = infoPtr->ptClickPos.y - offset.y;
4106 /* Begin selection and capture mouse */
4107 infoPtr->bMarqueeSelect = TRUE;
4108 SetCapture(infoPtr->hwndSelf);
4115 ZeroMemory(&nmlv, sizeof(nmlv));
4116 nmlv.iItem = ht.iItem;
4117 nmlv.ptAction = infoPtr->ptClickPos;
4119 notify_listview(infoPtr, LVN_BEGINDRAG, &nmlv);
4120 infoPtr->bDragging = TRUE;
4128 /* see if we are supposed to be tracking mouse hovering */
4129 if (LISTVIEW_IsHotTracking(infoPtr)) {
4130 TRACKMOUSEEVENT trackinfo;
4132 trackinfo.cbSize = sizeof(TRACKMOUSEEVENT);
4133 trackinfo.dwFlags = TME_QUERY;
4135 /* see if we are already tracking this hwnd */
4136 _TrackMouseEvent(&trackinfo);
4138 if(!(trackinfo.dwFlags & TME_HOVER) || trackinfo.hwndTrack != infoPtr->hwndSelf) {
4139 trackinfo.dwFlags = TME_HOVER;
4140 trackinfo.dwHoverTime = infoPtr->dwHoverTime;
4141 trackinfo.hwndTrack = infoPtr->hwndSelf;
4143 /* call TRACKMOUSEEVENT so we receive WM_MOUSEHOVER messages */
4144 _TrackMouseEvent(&trackinfo);
4153 * Tests whether the item is assignable to a list with style lStyle
4155 static inline BOOL is_assignable_item(const LVITEMW *lpLVItem, LONG lStyle)
4157 if ( (lpLVItem->mask & LVIF_TEXT) &&
4158 (lpLVItem->pszText == LPSTR_TEXTCALLBACKW) &&
4159 (lStyle & (LVS_SORTASCENDING | LVS_SORTDESCENDING)) ) return FALSE;
4167 * Helper for LISTVIEW_SetItemT and LISTVIEW_InsertItemT: sets item attributes.
4170 * [I] infoPtr : valid pointer to the listview structure
4171 * [I] lpLVItem : valid pointer to new item attributes
4172 * [I] isNew : the item being set is being inserted
4173 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
4174 * [O] bChanged : will be set to TRUE if the item really changed
4180 static BOOL set_main_item(LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem, BOOL isNew, BOOL isW, BOOL *bChanged)
4186 /* stateMask is ignored for LVM_INSERTITEM */
4187 UINT stateMask = isNew ? ~0 : lpLVItem->stateMask;
4191 assert(lpLVItem->iItem >= 0 && lpLVItem->iItem < infoPtr->nItemCount);
4193 if (lpLVItem->mask == 0) return TRUE;
4195 if (infoPtr->dwStyle & LVS_OWNERDATA)
4197 /* a virtual listview only stores selection and focus */
4198 if (lpLVItem->mask & ~LVIF_STATE)
4204 HDPA hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, lpLVItem->iItem);
4205 lpItem = DPA_GetPtr(hdpaSubItems, 0);
4209 /* we need to get the lParam and state of the item */
4210 item.iItem = lpLVItem->iItem;
4211 item.iSubItem = lpLVItem->iSubItem;
4212 item.mask = LVIF_STATE | LVIF_PARAM;
4213 item.stateMask = (infoPtr->dwStyle & LVS_OWNERDATA) ? LVIS_FOCUSED | LVIS_SELECTED : ~0;
4217 if (!isNew && !LISTVIEW_GetItemW(infoPtr, &item)) return FALSE;
4219 TRACE("oldState=%x, newState=%x\n", item.state, lpLVItem->state);
4220 /* determine what fields will change */
4221 if ((lpLVItem->mask & LVIF_STATE) && ((item.state ^ lpLVItem->state) & stateMask & ~infoPtr->uCallbackMask))
4222 uChanged |= LVIF_STATE;
4224 if ((lpLVItem->mask & LVIF_IMAGE) && (lpItem->hdr.iImage != lpLVItem->iImage))
4225 uChanged |= LVIF_IMAGE;
4227 if ((lpLVItem->mask & LVIF_PARAM) && (lpItem->lParam != lpLVItem->lParam))
4228 uChanged |= LVIF_PARAM;
4230 if ((lpLVItem->mask & LVIF_INDENT) && (lpItem->iIndent != lpLVItem->iIndent))
4231 uChanged |= LVIF_INDENT;
4233 if ((lpLVItem->mask & LVIF_TEXT) && textcmpWT(lpItem->hdr.pszText, lpLVItem->pszText, isW))
4234 uChanged |= LVIF_TEXT;
4236 TRACE("change mask=0x%x\n", uChanged);
4238 memset(&nmlv, 0, sizeof(NMLISTVIEW));
4239 nmlv.iItem = lpLVItem->iItem;
4240 nmlv.uNewState = (item.state & ~stateMask) | (lpLVItem->state & stateMask);
4241 nmlv.uOldState = item.state;
4242 nmlv.uChanged = uChanged ? uChanged : lpLVItem->mask;
4243 nmlv.lParam = item.lParam;
4245 /* Send LVN_ITEMCHANGING notification, if the item is not being inserted
4246 and we are _NOT_ virtual (LVS_OWNERDATA), and change notifications
4247 are enabled. Even nothing really changed we still need to send this,
4248 in this case uChanged mask is just set to passed item mask. */
4249 if(lpItem && !isNew && infoPtr->bDoChangeNotify)
4251 HWND hwndSelf = infoPtr->hwndSelf;
4253 if (notify_listview(infoPtr, LVN_ITEMCHANGING, &nmlv))
4255 if (!IsWindow(hwndSelf))
4259 /* When item is inserted we need to shift existing focus index if new item has lower index. */
4260 if (isNew && (stateMask & ~infoPtr->uCallbackMask & LVIS_FOCUSED) &&
4261 /* this means we won't hit a focus change path later */
4262 ((uChanged & LVIF_STATE) == 0 || (!(lpLVItem->state & LVIS_FOCUSED) && (infoPtr->nFocusedItem != lpLVItem->iItem))))
4264 if (infoPtr->nFocusedItem != -1 && (lpLVItem->iItem <= infoPtr->nFocusedItem))
4265 infoPtr->nFocusedItem++;
4268 if (!uChanged) return TRUE;
4271 /* copy information */
4272 if (lpLVItem->mask & LVIF_TEXT)
4273 textsetptrT(&lpItem->hdr.pszText, lpLVItem->pszText, isW);
4275 if (lpLVItem->mask & LVIF_IMAGE)
4276 lpItem->hdr.iImage = lpLVItem->iImage;
4278 if (lpLVItem->mask & LVIF_PARAM)
4279 lpItem->lParam = lpLVItem->lParam;
4281 if (lpLVItem->mask & LVIF_INDENT)
4282 lpItem->iIndent = lpLVItem->iIndent;
4284 if (uChanged & LVIF_STATE)
4286 if (lpItem && (stateMask & ~infoPtr->uCallbackMask))
4288 lpItem->state &= ~stateMask;
4289 lpItem->state |= (lpLVItem->state & stateMask);
4291 if (lpLVItem->state & stateMask & ~infoPtr->uCallbackMask & LVIS_SELECTED)
4293 if (infoPtr->dwStyle & LVS_SINGLESEL) LISTVIEW_DeselectAllSkipItem(infoPtr, lpLVItem->iItem);
4294 ranges_additem(infoPtr->selectionRanges, lpLVItem->iItem);
4296 else if (stateMask & LVIS_SELECTED)
4298 ranges_delitem(infoPtr->selectionRanges, lpLVItem->iItem);
4300 /* If we are asked to change focus, and we manage it, do it.
4301 It's important to have all new item data stored at this point,
4302 because changing existing focus could result in a redrawing operation,
4303 which in turn could ask for disp data, application should see all data
4304 for inserted item when processing LVN_GETDISPINFO.
4306 The way this works application will see nested item change notifications -
4307 changed item notifications interrupted by ones from item losing focus. */
4308 if (stateMask & ~infoPtr->uCallbackMask & LVIS_FOCUSED)
4310 if (lpLVItem->state & LVIS_FOCUSED)
4312 /* update selection mark */
4313 if (infoPtr->nFocusedItem == -1 && infoPtr->nSelectionMark == -1)
4314 infoPtr->nSelectionMark = lpLVItem->iItem;
4316 if (infoPtr->nFocusedItem != -1)
4318 /* remove current focus */
4319 item.mask = LVIF_STATE;
4321 item.stateMask = LVIS_FOCUSED;
4323 /* recurse with redrawing an item */
4324 LISTVIEW_SetItemState(infoPtr, infoPtr->nFocusedItem, &item);
4327 infoPtr->nFocusedItem = lpLVItem->iItem;
4328 LISTVIEW_EnsureVisible(infoPtr, lpLVItem->iItem, infoPtr->uView == LV_VIEW_LIST);
4330 else if (infoPtr->nFocusedItem == lpLVItem->iItem)
4332 infoPtr->nFocusedItem = -1;
4337 /* if we're inserting the item, we're done */
4338 if (isNew) return TRUE;
4340 /* send LVN_ITEMCHANGED notification */
4341 if (lpLVItem->mask & LVIF_PARAM) nmlv.lParam = lpLVItem->lParam;
4342 if (infoPtr->bDoChangeNotify) notify_listview(infoPtr, LVN_ITEMCHANGED, &nmlv);
4349 * Helper for LISTVIEW_{Set,Insert}ItemT *only*: sets subitem attributes.
4352 * [I] infoPtr : valid pointer to the listview structure
4353 * [I] lpLVItem : valid pointer to new subitem attributes
4354 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
4355 * [O] bChanged : will be set to TRUE if the item really changed
4361 static BOOL set_sub_item(const LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem, BOOL isW, BOOL *bChanged)
4364 SUBITEM_INFO *lpSubItem;
4366 /* we do not support subitems for virtual listviews */
4367 if (infoPtr->dwStyle & LVS_OWNERDATA) return FALSE;
4369 /* set subitem only if column is present */
4370 if (lpLVItem->iSubItem >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
4372 /* First do some sanity checks */
4373 /* The LVIF_STATE flag is valid for subitems, but does not appear to be
4374 particularly useful. We currently do not actually do anything with
4375 the flag on subitems.
4377 if (lpLVItem->mask & ~(LVIF_TEXT | LVIF_IMAGE | LVIF_STATE | LVIF_DI_SETITEM)) return FALSE;
4378 if (!(lpLVItem->mask & (LVIF_TEXT | LVIF_IMAGE | LVIF_STATE))) return TRUE;
4380 /* get the subitem structure, and create it if not there */
4381 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, lpLVItem->iItem);
4382 assert (hdpaSubItems);
4384 lpSubItem = LISTVIEW_GetSubItemPtr(hdpaSubItems, lpLVItem->iSubItem);
4387 SUBITEM_INFO *tmpSubItem;
4390 lpSubItem = Alloc(sizeof(SUBITEM_INFO));
4391 if (!lpSubItem) return FALSE;
4392 /* we could binary search here, if need be...*/
4393 for (i = 1; i < DPA_GetPtrCount(hdpaSubItems); i++)
4395 tmpSubItem = DPA_GetPtr(hdpaSubItems, i);
4396 if (tmpSubItem->iSubItem > lpLVItem->iSubItem) break;
4398 if (DPA_InsertPtr(hdpaSubItems, i, lpSubItem) == -1)
4403 lpSubItem->iSubItem = lpLVItem->iSubItem;
4404 lpSubItem->hdr.iImage = I_IMAGECALLBACK;
4408 if ((lpLVItem->mask & LVIF_IMAGE) && (lpSubItem->hdr.iImage != lpLVItem->iImage))
4410 lpSubItem->hdr.iImage = lpLVItem->iImage;
4414 if ((lpLVItem->mask & LVIF_TEXT) && textcmpWT(lpSubItem->hdr.pszText, lpLVItem->pszText, isW))
4416 textsetptrT(&lpSubItem->hdr.pszText, lpLVItem->pszText, isW);
4425 * Sets item attributes.
4428 * [I] infoPtr : valid pointer to the listview structure
4429 * [I] lpLVItem : new item attributes
4430 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
4436 static BOOL LISTVIEW_SetItemT(LISTVIEW_INFO *infoPtr, LVITEMW *lpLVItem, BOOL isW)
4438 HWND hwndSelf = infoPtr->hwndSelf;
4439 LPWSTR pszText = NULL;
4440 BOOL bResult, bChanged = FALSE;
4443 TRACE("(lpLVItem=%s, isW=%d)\n", debuglvitem_t(lpLVItem, isW), isW);
4445 if (!lpLVItem || lpLVItem->iItem < 0 || lpLVItem->iItem >= infoPtr->nItemCount)
4448 /* Store old item area */
4449 LISTVIEW_GetItemBox(infoPtr, lpLVItem->iItem, &oldItemArea);
4451 /* For efficiency, we transform the lpLVItem->pszText to Unicode here */
4452 if ((lpLVItem->mask & LVIF_TEXT) && is_text(lpLVItem->pszText))
4454 pszText = lpLVItem->pszText;
4455 lpLVItem->pszText = textdupTtoW(lpLVItem->pszText, isW);
4458 /* actually set the fields */
4459 if (!is_assignable_item(lpLVItem, infoPtr->dwStyle)) return FALSE;
4461 if (lpLVItem->iSubItem)
4462 bResult = set_sub_item(infoPtr, lpLVItem, TRUE, &bChanged);
4464 bResult = set_main_item(infoPtr, lpLVItem, FALSE, TRUE, &bChanged);
4465 if (!IsWindow(hwndSelf))
4468 /* redraw item, if necessary */
4469 if (bChanged && !infoPtr->bIsDrawing)
4471 /* this little optimization eliminates some nasty flicker */
4472 if ( infoPtr->uView == LV_VIEW_DETAILS && !(infoPtr->dwStyle & LVS_OWNERDRAWFIXED) &&
4473 !(infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) &&
4474 lpLVItem->iSubItem > 0 && lpLVItem->iSubItem <= DPA_GetPtrCount(infoPtr->hdpaColumns) )
4475 LISTVIEW_InvalidateSubItem(infoPtr, lpLVItem->iItem, lpLVItem->iSubItem);
4478 LISTVIEW_InvalidateRect(infoPtr, &oldItemArea);
4479 LISTVIEW_InvalidateItem(infoPtr, lpLVItem->iItem);
4485 textfreeT(lpLVItem->pszText, isW);
4486 lpLVItem->pszText = pszText;
4494 * Retrieves the index of the item at coordinate (0, 0) of the client area.
4497 * [I] infoPtr : valid pointer to the listview structure
4502 static INT LISTVIEW_GetTopIndex(const LISTVIEW_INFO *infoPtr)
4505 SCROLLINFO scrollInfo;
4507 scrollInfo.cbSize = sizeof(SCROLLINFO);
4508 scrollInfo.fMask = SIF_POS;
4510 if (infoPtr->uView == LV_VIEW_LIST)
4512 if (GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo))
4513 nItem = scrollInfo.nPos * LISTVIEW_GetCountPerColumn(infoPtr);
4515 else if (infoPtr->uView == LV_VIEW_DETAILS)
4517 if (GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
4518 nItem = scrollInfo.nPos;
4522 if (GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
4523 nItem = LISTVIEW_GetCountPerRow(infoPtr) * (scrollInfo.nPos / infoPtr->nItemHeight);
4526 TRACE("nItem=%d\n", nItem);
4534 * Erases the background of the given rectangle
4537 * [I] infoPtr : valid pointer to the listview structure
4538 * [I] hdc : device context handle
4539 * [I] lprcBox : clipping rectangle
4545 static inline BOOL LISTVIEW_FillBkgnd(const LISTVIEW_INFO *infoPtr, HDC hdc, const RECT *lprcBox)
4547 if (!infoPtr->hBkBrush) return FALSE;
4549 TRACE("(hdc=%p, lprcBox=%s, hBkBrush=%p)\n", hdc, wine_dbgstr_rect(lprcBox), infoPtr->hBkBrush);
4551 return FillRect(hdc, lprcBox, infoPtr->hBkBrush);
4559 * [I] infoPtr : valid pointer to the listview structure
4560 * [I] hdc : device context handle
4561 * [I] nItem : item index
4562 * [I] nSubItem : subitem index
4563 * [I] pos : item position in client coordinates
4564 * [I] cdmode : custom draw mode
4570 static BOOL LISTVIEW_DrawItem(LISTVIEW_INFO *infoPtr, HDC hdc, INT nItem, INT nSubItem, POINT pos, DWORD cdmode)
4573 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
4574 static WCHAR szCallback[] = { '(', 'c', 'a', 'l', 'l', 'b', 'a', 'c', 'k', ')', 0 };
4575 DWORD cdsubitemmode = CDRF_DODEFAULT;
4577 RECT rcSelect, rcBox, rcIcon, rcLabel, rcStateIcon;
4578 NMLVCUSTOMDRAW nmlvcd;
4583 TRACE("(hdc=%p, nItem=%d, nSubItem=%d, pos=%s)\n", hdc, nItem, nSubItem, wine_dbgstr_point(&pos));
4585 /* get information needed for drawing the item */
4586 lvItem.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_PARAM;
4587 if (nSubItem == 0) lvItem.mask |= LVIF_STATE;
4588 if (infoPtr->uView == LV_VIEW_DETAILS) lvItem.mask |= LVIF_INDENT;
4589 lvItem.stateMask = LVIS_SELECTED | LVIS_FOCUSED | LVIS_STATEIMAGEMASK | LVIS_CUT;
4590 lvItem.iItem = nItem;
4591 lvItem.iSubItem = nSubItem;
4594 lvItem.cchTextMax = DISP_TEXT_SIZE;
4595 lvItem.pszText = szDispText;
4596 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return FALSE;
4597 if (nSubItem > 0 && (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT))
4598 lvItem.state = LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED);
4599 if (lvItem.pszText == LPSTR_TEXTCALLBACKW) lvItem.pszText = szCallback;
4600 TRACE(" lvItem=%s\n", debuglvitem_t(&lvItem, TRUE));
4602 /* now check if we need to update the focus rectangle */
4603 lprcFocus = infoPtr->bFocus && (lvItem.state & LVIS_FOCUSED) ? &infoPtr->rcFocus : 0;
4605 if (!lprcFocus) lvItem.state &= ~LVIS_FOCUSED;
4606 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, &rcBox, &rcSelect, &rcIcon, &rcStateIcon, &rcLabel);
4607 OffsetRect(&rcBox, pos.x, pos.y);
4608 OffsetRect(&rcSelect, pos.x, pos.y);
4609 OffsetRect(&rcIcon, pos.x, pos.y);
4610 OffsetRect(&rcStateIcon, pos.x, pos.y);
4611 OffsetRect(&rcLabel, pos.x, pos.y);
4612 TRACE(" rcBox=%s, rcSelect=%s, rcIcon=%s. rcLabel=%s\n",
4613 wine_dbgstr_rect(&rcBox), wine_dbgstr_rect(&rcSelect),
4614 wine_dbgstr_rect(&rcIcon), wine_dbgstr_rect(&rcLabel));
4616 /* fill in the custom draw structure */
4617 customdraw_fill(&nmlvcd, infoPtr, hdc, &rcBox, &lvItem);
4619 hOldFont = GetCurrentObject(hdc, OBJ_FONT);
4620 if (nSubItem > 0) cdmode = infoPtr->cditemmode;
4621 if (cdmode & CDRF_SKIPDEFAULT) goto postpaint;
4622 if (cdmode & CDRF_NOTIFYITEMDRAW)
4623 cdsubitemmode = notify_customdraw(infoPtr, CDDS_PREPAINT, &nmlvcd);
4624 if (nSubItem == 0) infoPtr->cditemmode = cdsubitemmode;
4625 if (cdsubitemmode & CDRF_SKIPDEFAULT) goto postpaint;
4626 /* we have to send a CDDS_SUBITEM customdraw explicitly for subitem 0 */
4627 if (nSubItem == 0 && cdsubitemmode == CDRF_NOTIFYITEMDRAW)
4629 cdsubitemmode = notify_customdraw(infoPtr, CDDS_SUBITEM | CDDS_ITEMPREPAINT, &nmlvcd);
4630 if (cdsubitemmode & CDRF_SKIPDEFAULT) goto postpaint;
4632 if (nSubItem == 0 || (cdmode & CDRF_NOTIFYITEMDRAW))
4633 prepaint_setup(infoPtr, hdc, &nmlvcd, FALSE);
4634 else if ((infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) == FALSE)
4635 prepaint_setup(infoPtr, hdc, &nmlvcd, TRUE);
4637 /* in full row select, subitems, will just use main item's colors */
4638 if (nSubItem && infoPtr->uView == LV_VIEW_DETAILS && (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT))
4639 nmlvcd.clrTextBk = CLR_NONE;
4641 /* FIXME: temporary hack */
4642 rcSelect.left = rcLabel.left;
4644 /* draw the selection background, if we're drawing the main item */
4647 /* in icon mode, the label rect is really what we want to draw the
4649 if (infoPtr->uView == LV_VIEW_ICON)
4652 if (infoPtr->uView == LV_VIEW_DETAILS && (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT))
4654 /* we have to update left focus bound too if item isn't in leftmost column
4655 and reduce right box bound */
4656 if (DPA_GetPtrCount(infoPtr->hdpaColumns) > 0)
4660 if ((leftmost = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, 0, 0)))
4662 INT Originx = pos.x - LISTVIEW_GetColumnInfo(infoPtr, 0)->rcHeader.left;
4663 INT index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX,
4664 DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, 0);
4666 rcBox.right = LISTVIEW_GetColumnInfo(infoPtr, index)->rcHeader.right + Originx;
4667 rcSelect.left = LISTVIEW_GetColumnInfo(infoPtr, leftmost)->rcHeader.left + Originx;
4671 rcSelect.right = rcBox.right;
4674 if (nmlvcd.clrTextBk != CLR_NONE)
4675 ExtTextOutW(hdc, rcSelect.left, rcSelect.top, ETO_OPAQUE, &rcSelect, NULL, 0, NULL);
4676 /* store new focus rectangle */
4677 if (infoPtr->nFocusedItem == nItem) infoPtr->rcFocus = rcSelect;
4681 if (infoPtr->himlState && STATEIMAGEINDEX(lvItem.state) && (nSubItem == 0))
4683 UINT uStateImage = STATEIMAGEINDEX(lvItem.state);
4686 TRACE("uStateImage=%d\n", uStateImage);
4687 ImageList_Draw(infoPtr->himlState, uStateImage - 1, hdc,
4688 rcStateIcon.left, rcStateIcon.top, ILD_NORMAL);
4693 himl = (infoPtr->uView == LV_VIEW_ICON ? infoPtr->himlNormal : infoPtr->himlSmall);
4694 if (himl && lvItem.iImage >= 0 && !IsRectEmpty(&rcIcon))
4698 TRACE("iImage=%d\n", lvItem.iImage);
4700 if (lvItem.state & (LVIS_SELECTED | LVIS_CUT) && infoPtr->bFocus)
4701 style = ILD_SELECTED;
4705 ImageList_DrawEx(himl, lvItem.iImage, hdc, rcIcon.left, rcIcon.top,
4706 rcIcon.right - rcIcon.left, rcIcon.bottom - rcIcon.top, infoPtr->clrBk,
4707 lvItem.state & LVIS_CUT ? RGB(255, 255, 255) : CLR_DEFAULT,
4711 /* Don't bother painting item being edited */
4712 if (infoPtr->hwndEdit && nItem == infoPtr->nEditLabelItem && nSubItem == 0) goto postpaint;
4714 /* figure out the text drawing flags */
4715 uFormat = (infoPtr->uView == LV_VIEW_ICON ? (lprcFocus ? LV_FL_DT_FLAGS : LV_ML_DT_FLAGS) : LV_SL_DT_FLAGS);
4716 if (infoPtr->uView == LV_VIEW_ICON)
4717 uFormat = (lprcFocus ? LV_FL_DT_FLAGS : LV_ML_DT_FLAGS);
4720 switch (LISTVIEW_GetColumnInfo(infoPtr, nSubItem)->fmt & LVCFMT_JUSTIFYMASK)
4722 case LVCFMT_RIGHT: uFormat |= DT_RIGHT; break;
4723 case LVCFMT_CENTER: uFormat |= DT_CENTER; break;
4724 default: uFormat |= DT_LEFT;
4727 if (!(uFormat & (DT_RIGHT | DT_CENTER)))
4729 if (himl && lvItem.iImage >= 0 && !IsRectEmpty(&rcIcon)) rcLabel.left += IMAGE_PADDING;
4730 else rcLabel.left += LABEL_HOR_PADDING;
4732 else if (uFormat & DT_RIGHT) rcLabel.right -= LABEL_HOR_PADDING;
4734 /* for GRIDLINES reduce the bottom so the text formats correctly */
4735 if (infoPtr->uView == LV_VIEW_DETAILS && infoPtr->dwLvExStyle & LVS_EX_GRIDLINES)
4738 DrawTextW(hdc, lvItem.pszText, -1, &rcLabel, uFormat);
4741 if (cdsubitemmode & CDRF_NOTIFYPOSTPAINT)
4742 notify_postpaint(infoPtr, &nmlvcd);
4743 if (cdsubitemmode & CDRF_NEWFONT)
4744 SelectObject(hdc, hOldFont);
4750 * Draws listview items when in owner draw mode.
4753 * [I] infoPtr : valid pointer to the listview structure
4754 * [I] hdc : device context handle
4759 static void LISTVIEW_RefreshOwnerDraw(const LISTVIEW_INFO *infoPtr, ITERATOR *i, HDC hdc, DWORD cdmode)
4761 UINT uID = (UINT)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
4762 DWORD cditemmode = CDRF_DODEFAULT;
4763 NMLVCUSTOMDRAW nmlvcd;
4764 POINT Origin, Position;
4770 ZeroMemory(&dis, sizeof(dis));
4772 /* Get scroll info once before loop */
4773 LISTVIEW_GetOrigin(infoPtr, &Origin);
4775 /* iterate through the invalidated rows */
4776 while(iterator_next(i))
4778 item.iItem = i->nItem;
4780 item.mask = LVIF_PARAM | LVIF_STATE;
4781 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
4782 if (!LISTVIEW_GetItemW(infoPtr, &item)) continue;
4784 dis.CtlType = ODT_LISTVIEW;
4786 dis.itemID = item.iItem;
4787 dis.itemAction = ODA_DRAWENTIRE;
4789 if (item.state & LVIS_SELECTED) dis.itemState |= ODS_SELECTED;
4790 if (infoPtr->bFocus && (item.state & LVIS_FOCUSED)) dis.itemState |= ODS_FOCUS;
4791 dis.hwndItem = infoPtr->hwndSelf;
4793 LISTVIEW_GetItemOrigin(infoPtr, dis.itemID, &Position);
4794 dis.rcItem.left = Position.x + Origin.x;
4795 dis.rcItem.right = dis.rcItem.left + infoPtr->nItemWidth;
4796 dis.rcItem.top = Position.y + Origin.y;
4797 dis.rcItem.bottom = dis.rcItem.top + infoPtr->nItemHeight;
4798 dis.itemData = item.lParam;
4800 TRACE("item=%s, rcItem=%s\n", debuglvitem_t(&item, TRUE), wine_dbgstr_rect(&dis.rcItem));
4803 * Even if we do not send the CDRF_NOTIFYITEMDRAW we need to fill the nmlvcd
4804 * structure for the rest. of the paint cycle
4806 customdraw_fill(&nmlvcd, infoPtr, hdc, &dis.rcItem, &item);
4807 if (cdmode & CDRF_NOTIFYITEMDRAW)
4808 cditemmode = notify_customdraw(infoPtr, CDDS_PREPAINT, &nmlvcd);
4810 if (!(cditemmode & CDRF_SKIPDEFAULT))
4812 prepaint_setup (infoPtr, hdc, &nmlvcd, FALSE);
4813 SendMessageW(infoPtr->hwndNotify, WM_DRAWITEM, dis.CtlID, (LPARAM)&dis);
4816 if (cditemmode & CDRF_NOTIFYPOSTPAINT)
4817 notify_postpaint(infoPtr, &nmlvcd);
4823 * Draws listview items when in report display mode.
4826 * [I] infoPtr : valid pointer to the listview structure
4827 * [I] hdc : device context handle
4828 * [I] cdmode : custom draw mode
4833 static void LISTVIEW_RefreshReport(LISTVIEW_INFO *infoPtr, ITERATOR *i, HDC hdc, DWORD cdmode)
4836 RECT rcClip, rcItem;
4837 POINT Origin, Position;
4844 /* figure out what to draw */
4845 rgntype = GetClipBox(hdc, &rcClip);
4846 if (rgntype == NULLREGION) return;
4848 /* Get scroll info once before loop */
4849 LISTVIEW_GetOrigin(infoPtr, &Origin);
4851 colRanges = ranges_create(DPA_GetPtrCount(infoPtr->hdpaColumns));
4853 /* narrow down the columns we need to paint */
4854 for(col = 0; col < DPA_GetPtrCount(infoPtr->hdpaColumns); col++)
4856 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, col, 0);
4858 LISTVIEW_GetHeaderRect(infoPtr, index, &rcItem);
4859 if ((rcItem.right + Origin.x >= rcClip.left) && (rcItem.left + Origin.x < rcClip.right))
4860 ranges_additem(colRanges, index);
4862 iterator_rangesitems(&j, colRanges);
4864 /* in full row select, we _have_ to draw the main item */
4865 if (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT)
4868 /* iterate through the invalidated rows */
4869 while(iterator_next(i))
4871 LISTVIEW_GetItemOrigin(infoPtr, i->nItem, &Position);
4872 Position.y += Origin.y;
4874 /* iterate through the invalidated columns */
4875 while(iterator_next(&j))
4877 LISTVIEW_GetHeaderRect(infoPtr, j.nItem, &rcItem);
4878 Position.x = (j.nItem == 0) ? rcItem.left + Origin.x : Origin.x;
4880 if (rgntype == COMPLEXREGION && !((infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) && j.nItem == 0))
4883 rcItem.bottom = infoPtr->nItemHeight;
4884 OffsetRect(&rcItem, Origin.x, Position.y);
4885 if (!RectVisible(hdc, &rcItem)) continue;
4888 LISTVIEW_DrawItem(infoPtr, hdc, i->nItem, j.nItem, Position, cdmode);
4891 iterator_destroy(&j);
4896 * Draws the gridlines if necessary when in report display mode.
4899 * [I] infoPtr : valid pointer to the listview structure
4900 * [I] hdc : device context handle
4905 static void LISTVIEW_RefreshReportGrid(LISTVIEW_INFO *infoPtr, HDC hdc)
4911 RECT rcClip, rcItem = {0};
4919 /* figure out what to draw */
4920 rgntype = GetClipBox(hdc, &rcClip);
4921 if (rgntype == NULLREGION) return;
4923 /* Get scroll info once before loop */
4924 LISTVIEW_GetOrigin(infoPtr, &Origin);
4926 colRanges = ranges_create(DPA_GetPtrCount(infoPtr->hdpaColumns));
4928 /* narrow down the columns we need to paint */
4929 for(col = 0; col < DPA_GetPtrCount(infoPtr->hdpaColumns); col++)
4931 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, col, 0);
4933 LISTVIEW_GetHeaderRect(infoPtr, index, &rcItem);
4934 if ((rcItem.right + Origin.x >= rcClip.left) && (rcItem.left + Origin.x < rcClip.right))
4935 ranges_additem(colRanges, index);
4938 /* is right most vertical line visible? */
4939 if (DPA_GetPtrCount(infoPtr->hdpaColumns) > 0)
4941 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, 0);
4942 LISTVIEW_GetHeaderRect(infoPtr, index, &rcItem);
4943 rmost = (rcItem.right + Origin.x < rcClip.right);
4946 if ((hPen = CreatePen( PS_SOLID, 1, comctl32_color.clr3dFace )))
4948 hOldPen = SelectObject ( hdc, hPen );
4950 /* draw the vertical lines for the columns */
4951 iterator_rangesitems(&j, colRanges);
4952 while(iterator_next(&j))
4954 LISTVIEW_GetHeaderRect(infoPtr, j.nItem, &rcItem);
4955 if (rcItem.left == 0) continue; /* skip leftmost column */
4956 rcItem.left += Origin.x;
4957 rcItem.right += Origin.x;
4958 rcItem.top = infoPtr->rcList.top;
4959 rcItem.bottom = infoPtr->rcList.bottom;
4960 TRACE("vert col=%d, rcItem=%s\n", j.nItem, wine_dbgstr_rect(&rcItem));
4961 MoveToEx (hdc, rcItem.left, rcItem.top, NULL);
4962 LineTo (hdc, rcItem.left, rcItem.bottom);
4964 iterator_destroy(&j);
4965 /* draw rightmost grid line if visible */
4968 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX,
4969 DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, 0);
4970 LISTVIEW_GetHeaderRect(infoPtr, index, &rcItem);
4972 rcItem.right += Origin.x;
4974 MoveToEx (hdc, rcItem.right, infoPtr->rcList.top, NULL);
4975 LineTo (hdc, rcItem.right, infoPtr->rcList.bottom);
4978 /* draw the horizontal lines for the rows */
4979 itemheight = LISTVIEW_CalculateItemHeight(infoPtr);
4980 rcItem.left = infoPtr->rcList.left;
4981 rcItem.right = infoPtr->rcList.right;
4982 for(y = Origin.y > 1 ? Origin.y - 1 : itemheight - 1 + Origin.y % itemheight; y<=infoPtr->rcList.bottom; y+=itemheight)
4984 rcItem.bottom = rcItem.top = y;
4985 TRACE("horz rcItem=%s\n", wine_dbgstr_rect(&rcItem));
4986 MoveToEx (hdc, rcItem.left, rcItem.top, NULL);
4987 LineTo (hdc, rcItem.right, rcItem.top);
4990 SelectObject( hdc, hOldPen );
4991 DeleteObject( hPen );
4994 ranges_destroy(colRanges);
4999 * Draws listview items when in list display mode.
5002 * [I] infoPtr : valid pointer to the listview structure
5003 * [I] hdc : device context handle
5004 * [I] cdmode : custom draw mode
5009 static void LISTVIEW_RefreshList(LISTVIEW_INFO *infoPtr, ITERATOR *i, HDC hdc, DWORD cdmode)
5011 POINT Origin, Position;
5013 /* Get scroll info once before loop */
5014 LISTVIEW_GetOrigin(infoPtr, &Origin);
5016 while(iterator_prev(i))
5018 LISTVIEW_GetItemOrigin(infoPtr, i->nItem, &Position);
5019 Position.x += Origin.x;
5020 Position.y += Origin.y;
5022 LISTVIEW_DrawItem(infoPtr, hdc, i->nItem, 0, Position, cdmode);
5029 * Draws listview items.
5032 * [I] infoPtr : valid pointer to the listview structure
5033 * [I] hdc : device context handle
5034 * [I] prcErase : rect to be erased before refresh (may be NULL)
5039 static void LISTVIEW_Refresh(LISTVIEW_INFO *infoPtr, HDC hdc, const RECT *prcErase)
5041 COLORREF oldTextColor = 0, oldBkColor = 0, oldClrTextBk, oldClrText;
5042 NMLVCUSTOMDRAW nmlvcd;
5049 HBITMAP hbmp = NULL;
5052 LISTVIEW_DUMP(infoPtr);
5054 if (infoPtr->dwLvExStyle & LVS_EX_DOUBLEBUFFER) {
5055 TRACE("double buffering\n");
5057 hdc = CreateCompatibleDC(hdcOrig);
5059 ERR("Failed to create DC for backbuffer\n");
5062 hbmp = CreateCompatibleBitmap(hdcOrig, infoPtr->rcList.right,
5063 infoPtr->rcList.bottom);
5065 ERR("Failed to create bitmap for backbuffer\n");
5070 SelectObject(hdc, hbmp);
5071 SelectObject(hdc, infoPtr->hFont);
5073 if(GetClipBox(hdcOrig, &rcClient))
5074 IntersectClipRect(hdc, rcClient.left, rcClient.top, rcClient.right, rcClient.bottom);
5076 /* Save dc values we're gonna trash while drawing
5077 * FIXME: Should be done in LISTVIEW_DrawItem() */
5078 hOldFont = SelectObject(hdc, infoPtr->hFont);
5079 oldBkMode = GetBkMode(hdc);
5080 oldBkColor = GetBkColor(hdc);
5081 oldTextColor = GetTextColor(hdc);
5084 infoPtr->bIsDrawing = TRUE;
5087 LISTVIEW_FillBkgnd(infoPtr, hdc, prcErase);
5088 } else if (infoPtr->dwLvExStyle & LVS_EX_DOUBLEBUFFER) {
5089 /* If no erasing was done (usually because RedrawWindow was called
5090 * with RDW_INVALIDATE only) we need to copy the old contents into
5091 * the backbuffer before continuing. */
5092 BitBlt(hdc, infoPtr->rcList.left, infoPtr->rcList.top,
5093 infoPtr->rcList.right - infoPtr->rcList.left,
5094 infoPtr->rcList.bottom - infoPtr->rcList.top,
5095 hdcOrig, infoPtr->rcList.left, infoPtr->rcList.top, SRCCOPY);
5098 /* FIXME: Shouldn't need to do this */
5099 oldClrTextBk = infoPtr->clrTextBk;
5100 oldClrText = infoPtr->clrText;
5102 infoPtr->cditemmode = CDRF_DODEFAULT;
5104 GetClientRect(infoPtr->hwndSelf, &rcClient);
5105 customdraw_fill(&nmlvcd, infoPtr, hdc, &rcClient, 0);
5106 cdmode = notify_customdraw(infoPtr, CDDS_PREPAINT, &nmlvcd);
5107 if (cdmode & CDRF_SKIPDEFAULT) goto enddraw;
5108 prepaint_setup(infoPtr, hdc, &nmlvcd, FALSE);
5110 /* Use these colors to draw the items */
5111 infoPtr->clrTextBk = nmlvcd.clrTextBk;
5112 infoPtr->clrText = nmlvcd.clrText;
5114 /* nothing to draw */
5115 if(infoPtr->nItemCount == 0) goto enddraw;
5117 /* figure out what we need to draw */
5118 iterator_visibleitems(&i, infoPtr, hdc);
5119 range = iterator_range(&i);
5121 /* send cache hint notification */
5122 if (infoPtr->dwStyle & LVS_OWNERDATA)
5126 ZeroMemory(&nmlv, sizeof(NMLVCACHEHINT));
5127 nmlv.iFrom = range.lower;
5128 nmlv.iTo = range.upper - 1;
5129 notify_hdr(infoPtr, LVN_ODCACHEHINT, &nmlv.hdr);
5132 if ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (infoPtr->uView == LV_VIEW_DETAILS))
5133 LISTVIEW_RefreshOwnerDraw(infoPtr, &i, hdc, cdmode);
5136 if (infoPtr->uView == LV_VIEW_DETAILS)
5137 LISTVIEW_RefreshReport(infoPtr, &i, hdc, cdmode);
5138 else /* LV_VIEW_LIST, LV_VIEW_ICON or LV_VIEW_SMALLICON */
5139 LISTVIEW_RefreshList(infoPtr, &i, hdc, cdmode);
5141 /* if we have a focus rect and it's visible, draw it */
5142 if (infoPtr->bFocus && range.lower <= infoPtr->nFocusedItem &&
5143 (range.upper - 1) >= infoPtr->nFocusedItem)
5144 LISTVIEW_DrawFocusRect(infoPtr, hdc);
5146 iterator_destroy(&i);
5149 /* For LVS_EX_GRIDLINES go and draw lines */
5150 /* This includes the case where there were *no* items */
5151 if ((infoPtr->uView == LV_VIEW_DETAILS) && infoPtr->dwLvExStyle & LVS_EX_GRIDLINES)
5152 LISTVIEW_RefreshReportGrid(infoPtr, hdc);
5154 /* Draw marquee rectangle if appropriate */
5155 if (infoPtr->bMarqueeSelect)
5156 DrawFocusRect(hdc, &infoPtr->marqueeDrawRect);
5158 if (cdmode & CDRF_NOTIFYPOSTPAINT)
5159 notify_postpaint(infoPtr, &nmlvcd);
5161 infoPtr->clrTextBk = oldClrTextBk;
5162 infoPtr->clrText = oldClrText;
5165 BitBlt(hdcOrig, infoPtr->rcList.left, infoPtr->rcList.top,
5166 infoPtr->rcList.right - infoPtr->rcList.left,
5167 infoPtr->rcList.bottom - infoPtr->rcList.top,
5168 hdc, infoPtr->rcList.left, infoPtr->rcList.top, SRCCOPY);
5173 SelectObject(hdc, hOldFont);
5174 SetBkMode(hdc, oldBkMode);
5175 SetBkColor(hdc, oldBkColor);
5176 SetTextColor(hdc, oldTextColor);
5179 infoPtr->bIsDrawing = FALSE;
5185 * Calculates the approximate width and height of a given number of items.
5188 * [I] infoPtr : valid pointer to the listview structure
5189 * [I] nItemCount : number of items
5190 * [I] wWidth : width
5191 * [I] wHeight : height
5194 * Returns a DWORD. The width in the low word and the height in high word.
5196 static DWORD LISTVIEW_ApproximateViewRect(const LISTVIEW_INFO *infoPtr, INT nItemCount,
5197 WORD wWidth, WORD wHeight)
5199 DWORD dwViewRect = 0;
5201 if (nItemCount == -1)
5202 nItemCount = infoPtr->nItemCount;
5204 if (infoPtr->uView == LV_VIEW_LIST)
5206 INT nItemCountPerColumn = 1;
5207 INT nColumnCount = 0;
5209 if (wHeight == 0xFFFF)
5211 /* use current height */
5212 wHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
5215 if (wHeight < infoPtr->nItemHeight)
5216 wHeight = infoPtr->nItemHeight;
5220 if (infoPtr->nItemHeight > 0)
5222 nItemCountPerColumn = wHeight / infoPtr->nItemHeight;
5223 if (nItemCountPerColumn == 0)
5224 nItemCountPerColumn = 1;
5226 if (nItemCount % nItemCountPerColumn != 0)
5227 nColumnCount = nItemCount / nItemCountPerColumn;
5229 nColumnCount = nItemCount / nItemCountPerColumn + 1;
5233 /* Microsoft padding magic */
5234 wHeight = nItemCountPerColumn * infoPtr->nItemHeight + 2;
5235 wWidth = nColumnCount * infoPtr->nItemWidth + 2;
5237 dwViewRect = MAKELONG(wWidth, wHeight);
5239 else if (infoPtr->uView == LV_VIEW_DETAILS)
5243 if (infoPtr->nItemCount > 0)
5245 LISTVIEW_GetItemBox(infoPtr, 0, &rcBox);
5246 wWidth = rcBox.right - rcBox.left;
5247 wHeight = (rcBox.bottom - rcBox.top) * nItemCount;
5251 /* use current height and width */
5252 if (wHeight == 0xffff)
5253 wHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
5254 if (wWidth == 0xffff)
5255 wWidth = infoPtr->rcList.right - infoPtr->rcList.left;
5258 dwViewRect = MAKELONG(wWidth, wHeight);
5260 else if (infoPtr->uView == LV_VIEW_ICON)
5266 nItemWidth = infoPtr->iconSpacing.cx;
5267 nItemHeight = infoPtr->iconSpacing.cy;
5269 if (wWidth == 0xffff)
5270 wWidth = infoPtr->rcList.right - infoPtr->rcList.left;
5272 if (wWidth < nItemWidth)
5273 wWidth = nItemWidth;
5275 cols = wWidth / nItemWidth;
5276 if (cols > nItemCount)
5283 rows = nItemCount / cols;
5284 if (nItemCount % cols)
5290 wHeight = (nItemHeight * rows)+2;
5291 wWidth = (nItemWidth * cols)+2;
5293 dwViewRect = MAKELONG(wWidth, wHeight);
5295 else if (infoPtr->uView == LV_VIEW_SMALLICON)
5296 FIXME("uView == LV_VIEW_SMALLICON: not implemented\n");
5303 * Cancel edit label with saving item text.
5306 * [I] infoPtr : valid pointer to the listview structure
5309 * Always returns TRUE.
5311 static LRESULT LISTVIEW_CancelEditLabel(LISTVIEW_INFO *infoPtr)
5313 if (infoPtr->hwndEdit)
5315 /* handle value will be lost after LISTVIEW_EndEditLabelT */
5316 HWND edit = infoPtr->hwndEdit;
5318 LISTVIEW_EndEditLabelT(infoPtr, TRUE, IsWindowUnicode(infoPtr->hwndEdit));
5319 SendMessageW(edit, WM_CLOSE, 0, 0);
5327 * Create a drag image list for the specified item.
5330 * [I] infoPtr : valid pointer to the listview structure
5331 * [I] iItem : index of item
5332 * [O] lppt : Upper-left corner of the image
5335 * Returns a handle to the image list if successful, NULL otherwise.
5337 static HIMAGELIST LISTVIEW_CreateDragImage(LISTVIEW_INFO *infoPtr, INT iItem, LPPOINT lppt)
5343 HBITMAP hbmp, hOldbmp;
5344 HIMAGELIST dragList = 0;
5345 TRACE("iItem=%d Count=%d\n", iItem, infoPtr->nItemCount);
5347 if (iItem < 0 || iItem >= infoPtr->nItemCount || !lppt)
5350 rcItem.left = LVIR_BOUNDS;
5351 if (!LISTVIEW_GetItemRect(infoPtr, iItem, &rcItem))
5354 lppt->x = rcItem.left;
5355 lppt->y = rcItem.top;
5357 size.cx = rcItem.right - rcItem.left;
5358 size.cy = rcItem.bottom - rcItem.top;
5360 hdcOrig = GetDC(infoPtr->hwndSelf);
5361 hdc = CreateCompatibleDC(hdcOrig);
5362 hbmp = CreateCompatibleBitmap(hdcOrig, size.cx, size.cy);
5363 hOldbmp = SelectObject(hdc, hbmp);
5365 rcItem.left = rcItem.top = 0;
5366 rcItem.right = size.cx;
5367 rcItem.bottom = size.cy;
5368 FillRect(hdc, &rcItem, infoPtr->hBkBrush);
5371 if (LISTVIEW_DrawItem(infoPtr, hdc, iItem, 0, pos, infoPtr->cditemmode))
5373 dragList = ImageList_Create(size.cx, size.cy, ILC_COLOR, 10, 10);
5374 SelectObject(hdc, hOldbmp);
5375 ImageList_Add(dragList, hbmp, 0);
5378 SelectObject(hdc, hOldbmp);
5382 ReleaseDC(infoPtr->hwndSelf, hdcOrig);
5384 TRACE("ret=%p\n", dragList);
5392 * Removes all listview items and subitems.
5395 * [I] infoPtr : valid pointer to the listview structure
5401 static BOOL LISTVIEW_DeleteAllItems(LISTVIEW_INFO *infoPtr, BOOL destroy)
5404 HDPA hdpaSubItems = NULL;
5413 /* we do it directly, to avoid notifications */
5414 ranges_clear(infoPtr->selectionRanges);
5415 infoPtr->nSelectionMark = -1;
5416 infoPtr->nFocusedItem = -1;
5417 SetRectEmpty(&infoPtr->rcFocus);
5418 /* But we are supposed to leave nHotItem as is! */
5421 /* send LVN_DELETEALLITEMS notification */
5422 ZeroMemory(&nmlv, sizeof(NMLISTVIEW));
5424 bSuppress = notify_listview(infoPtr, LVN_DELETEALLITEMS, &nmlv);
5426 for (i = infoPtr->nItemCount - 1; i >= 0; i--)
5428 if (!(infoPtr->dwStyle & LVS_OWNERDATA))
5430 /* send LVN_DELETEITEM notification, if not suppressed
5431 and if it is not a virtual listview */
5432 if (!bSuppress) notify_deleteitem(infoPtr, i);
5433 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, i);
5434 lpItem = DPA_GetPtr(hdpaSubItems, 0);
5435 /* free id struct */
5436 j = DPA_GetPtrIndex(infoPtr->hdpaItemIds, lpItem->id);
5437 lpID = DPA_GetPtr(infoPtr->hdpaItemIds, j);
5438 DPA_DeletePtr(infoPtr->hdpaItemIds, j);
5440 /* both item and subitem start with ITEMHDR header */
5441 for (j = 0; j < DPA_GetPtrCount(hdpaSubItems); j++)
5443 hdrItem = DPA_GetPtr(hdpaSubItems, j);
5444 if (is_text(hdrItem->pszText)) Free(hdrItem->pszText);
5447 DPA_Destroy(hdpaSubItems);
5448 DPA_DeletePtr(infoPtr->hdpaItems, i);
5450 DPA_DeletePtr(infoPtr->hdpaPosX, i);
5451 DPA_DeletePtr(infoPtr->hdpaPosY, i);
5452 infoPtr->nItemCount --;
5457 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
5458 LISTVIEW_UpdateScroll(infoPtr);
5460 LISTVIEW_InvalidateList(infoPtr);
5467 * Scrolls, and updates the columns, when a column is changing width.
5470 * [I] infoPtr : valid pointer to the listview structure
5471 * [I] nColumn : column to scroll
5472 * [I] dx : amount of scroll, in pixels
5477 static void LISTVIEW_ScrollColumns(LISTVIEW_INFO *infoPtr, INT nColumn, INT dx)
5479 COLUMN_INFO *lpColumnInfo;
5485 if (nColumn < 0 || DPA_GetPtrCount(infoPtr->hdpaColumns) < 1) return;
5486 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, min(nColumn, DPA_GetPtrCount(infoPtr->hdpaColumns) - 1));
5487 rcCol = lpColumnInfo->rcHeader;
5488 if (nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns))
5489 rcCol.left = rcCol.right;
5491 /* adjust the other columns */
5492 hdi.mask = HDI_ORDER;
5493 if (SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nColumn, (LPARAM)&hdi))
5495 INT nOrder = hdi.iOrder;
5496 for (nCol = 0; nCol < DPA_GetPtrCount(infoPtr->hdpaColumns); nCol++)
5498 hdi.mask = HDI_ORDER;
5499 SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nCol, (LPARAM)&hdi);
5500 if (hdi.iOrder >= nOrder) {
5501 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, nCol);
5502 lpColumnInfo->rcHeader.left += dx;
5503 lpColumnInfo->rcHeader.right += dx;
5508 /* do not update screen if not in report mode */
5509 if (!is_redrawing(infoPtr) || infoPtr->uView != LV_VIEW_DETAILS) return;
5511 /* Need to reset the item width when inserting a new column */
5512 infoPtr->nItemWidth += dx;
5514 LISTVIEW_UpdateScroll(infoPtr);
5515 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
5517 /* scroll to cover the deleted column, and invalidate for redraw */
5518 rcOld = infoPtr->rcList;
5519 rcOld.left = ptOrigin.x + rcCol.left + dx;
5520 ScrollWindowEx(infoPtr->hwndSelf, dx, 0, &rcOld, &rcOld, 0, 0, SW_ERASE | SW_INVALIDATE);
5525 * Removes a column from the listview control.
5528 * [I] infoPtr : valid pointer to the listview structure
5529 * [I] nColumn : column index
5535 static BOOL LISTVIEW_DeleteColumn(LISTVIEW_INFO *infoPtr, INT nColumn)
5539 TRACE("nColumn=%d\n", nColumn);
5541 if (nColumn < 0 || DPA_GetPtrCount(infoPtr->hdpaColumns) == 0
5542 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
5544 /* While the MSDN specifically says that column zero should not be deleted,
5545 what actually happens is that the column itself is deleted but no items or subitems
5549 LISTVIEW_GetHeaderRect(infoPtr, nColumn, &rcCol);
5551 if (!SendMessageW(infoPtr->hwndHeader, HDM_DELETEITEM, nColumn, 0))
5554 Free(DPA_GetPtr(infoPtr->hdpaColumns, nColumn));
5555 DPA_DeletePtr(infoPtr->hdpaColumns, nColumn);
5557 if (!(infoPtr->dwStyle & LVS_OWNERDATA) && nColumn)
5559 SUBITEM_INFO *lpSubItem, *lpDelItem;
5561 INT nItem, nSubItem, i;
5563 for (nItem = 0; nItem < infoPtr->nItemCount; nItem++)
5565 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, nItem);
5568 for (i = 1; i < DPA_GetPtrCount(hdpaSubItems); i++)
5570 lpSubItem = DPA_GetPtr(hdpaSubItems, i);
5571 if (lpSubItem->iSubItem == nColumn)
5574 lpDelItem = lpSubItem;
5576 else if (lpSubItem->iSubItem > nColumn)
5578 lpSubItem->iSubItem--;
5582 /* if we found our subitem, zap it */
5586 if (is_text(lpDelItem->hdr.pszText))
5587 Free(lpDelItem->hdr.pszText);
5592 /* free dpa memory */
5593 DPA_DeletePtr(hdpaSubItems, nSubItem);
5598 /* update the other column info */
5599 LISTVIEW_UpdateItemSize(infoPtr);
5600 if(DPA_GetPtrCount(infoPtr->hdpaColumns) == 0)
5601 LISTVIEW_InvalidateList(infoPtr);
5603 LISTVIEW_ScrollColumns(infoPtr, nColumn, -(rcCol.right - rcCol.left));
5610 * Invalidates the listview after an item's insertion or deletion.
5613 * [I] infoPtr : valid pointer to the listview structure
5614 * [I] nItem : item index
5615 * [I] dir : -1 if deleting, 1 if inserting
5620 static void LISTVIEW_ScrollOnInsert(LISTVIEW_INFO *infoPtr, INT nItem, INT dir)
5622 INT nPerCol, nItemCol, nItemRow;
5626 /* if we don't refresh, what's the point of scrolling? */
5627 if (!is_redrawing(infoPtr)) return;
5629 assert (abs(dir) == 1);
5631 /* arrange icons if autoarrange is on */
5632 if (is_autoarrange(infoPtr))
5634 BOOL arrange = TRUE;
5635 if (dir < 0 && nItem >= infoPtr->nItemCount) arrange = FALSE;
5636 if (dir > 0 && nItem == infoPtr->nItemCount - 1) arrange = FALSE;
5637 if (arrange) LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
5640 /* scrollbars need updating */
5641 LISTVIEW_UpdateScroll(infoPtr);
5643 /* figure out the item's position */
5644 if (infoPtr->uView == LV_VIEW_DETAILS)
5645 nPerCol = infoPtr->nItemCount + 1;
5646 else if (infoPtr->uView == LV_VIEW_LIST)
5647 nPerCol = LISTVIEW_GetCountPerColumn(infoPtr);
5648 else /* LV_VIEW_ICON, or LV_VIEW_SMALLICON */
5651 nItemCol = nItem / nPerCol;
5652 nItemRow = nItem % nPerCol;
5653 LISTVIEW_GetOrigin(infoPtr, &Origin);
5655 /* move the items below up a slot */
5656 rcScroll.left = nItemCol * infoPtr->nItemWidth;
5657 rcScroll.top = nItemRow * infoPtr->nItemHeight;
5658 rcScroll.right = rcScroll.left + infoPtr->nItemWidth;
5659 rcScroll.bottom = nPerCol * infoPtr->nItemHeight;
5660 OffsetRect(&rcScroll, Origin.x, Origin.y);
5661 TRACE("rcScroll=%s, dx=%d\n", wine_dbgstr_rect(&rcScroll), dir * infoPtr->nItemHeight);
5662 if (IntersectRect(&rcScroll, &rcScroll, &infoPtr->rcList))
5664 TRACE("Invalidating rcScroll=%s, rcList=%s\n", wine_dbgstr_rect(&rcScroll), wine_dbgstr_rect(&infoPtr->rcList));
5665 InvalidateRect(infoPtr->hwndSelf, &rcScroll, TRUE);
5668 /* report has only that column, so we're done */
5669 if (infoPtr->uView == LV_VIEW_DETAILS) return;
5671 /* now for LISTs, we have to deal with the columns to the right */
5672 rcScroll.left = (nItemCol + 1) * infoPtr->nItemWidth;
5674 rcScroll.right = (infoPtr->nItemCount / nPerCol + 1) * infoPtr->nItemWidth;
5675 rcScroll.bottom = nPerCol * infoPtr->nItemHeight;
5676 OffsetRect(&rcScroll, Origin.x, Origin.y);
5677 if (IntersectRect(&rcScroll, &rcScroll, &infoPtr->rcList))
5678 InvalidateRect(infoPtr->hwndSelf, &rcScroll, TRUE);
5683 * Removes an item from the listview control.
5686 * [I] infoPtr : valid pointer to the listview structure
5687 * [I] nItem : item index
5693 static BOOL LISTVIEW_DeleteItem(LISTVIEW_INFO *infoPtr, INT nItem)
5696 const BOOL is_icon = (infoPtr->uView == LV_VIEW_SMALLICON || infoPtr->uView == LV_VIEW_ICON);
5697 INT focus = infoPtr->nFocusedItem;
5699 TRACE("(nItem=%d)\n", nItem);
5701 if (nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
5703 /* remove selection, and focus */
5705 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
5706 LISTVIEW_SetItemState(infoPtr, nItem, &item);
5708 /* send LVN_DELETEITEM notification. */
5709 if (!notify_deleteitem(infoPtr, nItem)) return FALSE;
5711 /* we need to do this here, because we'll be deleting stuff */
5713 LISTVIEW_InvalidateItem(infoPtr, nItem);
5715 if (!(infoPtr->dwStyle & LVS_OWNERDATA))
5723 hdpaSubItems = DPA_DeletePtr(infoPtr->hdpaItems, nItem);
5724 lpItem = DPA_GetPtr(hdpaSubItems, 0);
5726 /* free id struct */
5727 i = DPA_GetPtrIndex(infoPtr->hdpaItemIds, lpItem->id);
5728 lpID = DPA_GetPtr(infoPtr->hdpaItemIds, i);
5729 DPA_DeletePtr(infoPtr->hdpaItemIds, i);
5731 for (i = 0; i < DPA_GetPtrCount(hdpaSubItems); i++)
5733 hdrItem = DPA_GetPtr(hdpaSubItems, i);
5734 if (is_text(hdrItem->pszText)) Free(hdrItem->pszText);
5737 DPA_Destroy(hdpaSubItems);
5742 DPA_DeletePtr(infoPtr->hdpaPosX, nItem);
5743 DPA_DeletePtr(infoPtr->hdpaPosY, nItem);
5746 infoPtr->nItemCount--;
5747 LISTVIEW_ShiftIndices(infoPtr, nItem, -1);
5748 LISTVIEW_ShiftFocus(infoPtr, focus, nItem, -1);
5750 /* now is the invalidation fun */
5752 LISTVIEW_ScrollOnInsert(infoPtr, nItem, -1);
5759 * Callback implementation for editlabel control
5762 * [I] infoPtr : valid pointer to the listview structure
5763 * [I] storeText : store edit box text as item text
5764 * [I] isW : TRUE if psxText is Unicode, FALSE if it's ANSI
5770 static BOOL LISTVIEW_EndEditLabelT(LISTVIEW_INFO *infoPtr, BOOL storeText, BOOL isW)
5772 HWND hwndSelf = infoPtr->hwndSelf;
5773 WCHAR szDispText[DISP_TEXT_SIZE] = { 0 };
5774 NMLVDISPINFOW dispInfo;
5775 INT editedItem = infoPtr->nEditLabelItem;
5777 WCHAR *pszText = NULL;
5782 DWORD len = isW ? GetWindowTextLengthW(infoPtr->hwndEdit) : GetWindowTextLengthA(infoPtr->hwndEdit);
5786 if ((pszText = Alloc((len+1) * (isW ? sizeof(WCHAR) : sizeof(CHAR)))))
5788 if (isW) GetWindowTextW(infoPtr->hwndEdit, pszText, len+1);
5789 else GetWindowTextA(infoPtr->hwndEdit, (CHAR*)pszText, len+1);
5794 TRACE("(pszText=%s, isW=%d)\n", debugtext_t(pszText, isW), isW);
5796 ZeroMemory(&dispInfo, sizeof(dispInfo));
5797 dispInfo.item.mask = LVIF_PARAM | LVIF_STATE | LVIF_TEXT;
5798 dispInfo.item.iItem = editedItem;
5799 dispInfo.item.iSubItem = 0;
5800 dispInfo.item.stateMask = ~0;
5801 dispInfo.item.pszText = szDispText;
5802 dispInfo.item.cchTextMax = DISP_TEXT_SIZE;
5803 if (!LISTVIEW_GetItemT(infoPtr, &dispInfo.item, isW))
5810 same = (lstrcmpW(dispInfo.item.pszText, pszText) == 0);
5813 LPWSTR tmp = textdupTtoW(pszText, FALSE);
5814 same = (lstrcmpW(dispInfo.item.pszText, tmp) == 0);
5815 textfreeT(tmp, FALSE);
5818 /* add the text from the edit in */
5819 dispInfo.item.mask |= LVIF_TEXT;
5820 dispInfo.item.pszText = same ? NULL : pszText;
5821 dispInfo.item.cchTextMax = textlenT(dispInfo.item.pszText, isW);
5823 /* Do we need to update the Item Text */
5824 res = notify_dispinfoT(infoPtr, LVN_ENDLABELEDITW, &dispInfo, isW);
5826 infoPtr->nEditLabelItem = -1;
5827 infoPtr->hwndEdit = 0;
5829 if (!res) goto cleanup;
5831 if (!IsWindow(hwndSelf))
5836 if (!pszText) return TRUE;
5843 if (!(infoPtr->dwStyle & LVS_OWNERDATA))
5845 HDPA hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, editedItem);
5846 ITEM_INFO* lpItem = DPA_GetPtr(hdpaSubItems, 0);
5847 if (lpItem && lpItem->hdr.pszText == LPSTR_TEXTCALLBACKW)
5849 LISTVIEW_InvalidateItem(infoPtr, editedItem);
5855 ZeroMemory(&dispInfo, sizeof(dispInfo));
5856 dispInfo.item.mask = LVIF_TEXT;
5857 dispInfo.item.iItem = editedItem;
5858 dispInfo.item.iSubItem = 0;
5859 dispInfo.item.pszText = pszText;
5860 dispInfo.item.cchTextMax = textlenT(pszText, isW);
5861 res = LISTVIEW_SetItemT(infoPtr, &dispInfo.item, isW);
5871 * Subclassed edit control windproc function
5874 * [I] hwnd : the edit window handle
5875 * [I] uMsg : the message that is to be processed
5876 * [I] wParam : first message parameter
5877 * [I] lParam : second message parameter
5878 * [I] isW : TRUE if input is Unicode
5883 static LRESULT EditLblWndProcT(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL isW)
5885 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongPtrW(GetParent(hwnd), 0);
5888 TRACE("(hwnd=%p, uMsg=%x, wParam=%lx, lParam=%lx, isW=%d)\n",
5889 hwnd, uMsg, wParam, lParam, isW);
5894 return DLGC_WANTARROWS | DLGC_WANTALLKEYS;
5898 WNDPROC editProc = infoPtr->EditWndProc;
5899 infoPtr->EditWndProc = 0;
5900 SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (DWORD_PTR)editProc);
5901 return CallWindowProcT(editProc, hwnd, uMsg, wParam, lParam, isW);
5905 if (VK_ESCAPE == (INT)wParam)
5910 else if (VK_RETURN == (INT)wParam)
5914 return CallWindowProcT(infoPtr->EditWndProc, hwnd, uMsg, wParam, lParam, isW);
5918 if (infoPtr->hwndEdit)
5919 LISTVIEW_EndEditLabelT(infoPtr, save, isW);
5921 SendMessageW(hwnd, WM_CLOSE, 0, 0);
5927 * Subclassed edit control Unicode windproc function
5930 * [I] hwnd : the edit window handle
5931 * [I] uMsg : the message that is to be processed
5932 * [I] wParam : first message parameter
5933 * [I] lParam : second message parameter
5937 static LRESULT CALLBACK EditLblWndProcW(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
5939 return EditLblWndProcT(hwnd, uMsg, wParam, lParam, TRUE);
5944 * Subclassed edit control ANSI windproc function
5947 * [I] hwnd : the edit window handle
5948 * [I] uMsg : the message that is to be processed
5949 * [I] wParam : first message parameter
5950 * [I] lParam : second message parameter
5954 static LRESULT CALLBACK EditLblWndProcA(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
5956 return EditLblWndProcT(hwnd, uMsg, wParam, lParam, FALSE);
5961 * Creates a subclassed edit control
5964 * [I] infoPtr : valid pointer to the listview structure
5965 * [I] text : initial text for the edit
5966 * [I] style : the window style
5967 * [I] isW : TRUE if input is Unicode
5971 static HWND CreateEditLabelT(LISTVIEW_INFO *infoPtr, LPCWSTR text, BOOL isW)
5973 static const DWORD style = WS_CHILDWINDOW|WS_CLIPSIBLINGS|ES_LEFT|ES_AUTOHSCROLL|WS_BORDER|WS_VISIBLE;
5974 HINSTANCE hinst = (HINSTANCE)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_HINSTANCE);
5977 TRACE("(%p, text=%s, isW=%d)\n", infoPtr, debugtext_t(text, isW), isW);
5979 /* window will be resized and positioned after LVN_BEGINLABELEDIT */
5981 hedit = CreateWindowW(WC_EDITW, text, style, 0, 0, 0, 0, infoPtr->hwndSelf, 0, hinst, 0);
5983 hedit = CreateWindowA(WC_EDITA, (LPCSTR)text, style, 0, 0, 0, 0, infoPtr->hwndSelf, 0, hinst, 0);
5985 if (!hedit) return 0;
5987 infoPtr->EditWndProc = (WNDPROC)
5988 (isW ? SetWindowLongPtrW(hedit, GWLP_WNDPROC, (DWORD_PTR)EditLblWndProcW) :
5989 SetWindowLongPtrA(hedit, GWLP_WNDPROC, (DWORD_PTR)EditLblWndProcA) );
5991 SendMessageW(hedit, WM_SETFONT, (WPARAM)infoPtr->hFont, FALSE);
5992 SendMessageW(hedit, EM_SETLIMITTEXT, DISP_TEXT_SIZE-1, 0);
5999 * Begin in place editing of specified list view item
6002 * [I] infoPtr : valid pointer to the listview structure
6003 * [I] nItem : item index
6004 * [I] isW : TRUE if it's a Unicode req, FALSE if ASCII
6010 static HWND LISTVIEW_EditLabelT(LISTVIEW_INFO *infoPtr, INT nItem, BOOL isW)
6012 WCHAR disptextW[DISP_TEXT_SIZE] = { 0 };
6013 HWND hwndSelf = infoPtr->hwndSelf;
6014 NMLVDISPINFOW dispInfo;
6015 HFONT hOldFont = NULL;
6021 TRACE("(nItem=%d, isW=%d)\n", nItem, isW);
6023 if (~infoPtr->dwStyle & LVS_EDITLABELS) return 0;
6025 /* remove existing edit box */
6026 if (infoPtr->hwndEdit)
6028 SetFocus(infoPtr->hwndSelf);
6029 infoPtr->hwndEdit = 0;
6032 if (nItem < 0 || nItem >= infoPtr->nItemCount) return 0;
6034 infoPtr->nEditLabelItem = nItem;
6036 LISTVIEW_SetSelection(infoPtr, nItem);
6037 LISTVIEW_SetItemFocus(infoPtr, nItem);
6038 LISTVIEW_InvalidateItem(infoPtr, nItem);
6040 rect.left = LVIR_LABEL;
6041 if (!LISTVIEW_GetItemRect(infoPtr, nItem, &rect)) return 0;
6043 ZeroMemory(&dispInfo, sizeof(dispInfo));
6044 dispInfo.item.mask = LVIF_PARAM | LVIF_STATE | LVIF_TEXT;
6045 dispInfo.item.iItem = nItem;
6046 dispInfo.item.iSubItem = 0;
6047 dispInfo.item.stateMask = ~0;
6048 dispInfo.item.pszText = disptextW;
6049 dispInfo.item.cchTextMax = DISP_TEXT_SIZE;
6050 if (!LISTVIEW_GetItemT(infoPtr, &dispInfo.item, isW)) return 0;
6052 infoPtr->hwndEdit = CreateEditLabelT(infoPtr, dispInfo.item.pszText, isW);
6053 if (!infoPtr->hwndEdit) return 0;
6055 if (notify_dispinfoT(infoPtr, LVN_BEGINLABELEDITW, &dispInfo, isW))
6057 if (!IsWindow(hwndSelf))
6059 SendMessageW(infoPtr->hwndEdit, WM_CLOSE, 0, 0);
6060 infoPtr->hwndEdit = 0;
6064 TRACE("disp text=%s\n", debugtext_t(dispInfo.item.pszText, isW));
6066 /* position and display edit box */
6067 hdc = GetDC(infoPtr->hwndSelf);
6069 /* select the font to get appropriate metric dimensions */
6071 hOldFont = SelectObject(hdc, infoPtr->hFont);
6073 /* use real edit box content, it could be altered during LVN_BEGINLABELEDIT notification */
6074 GetWindowTextW(infoPtr->hwndEdit, disptextW, DISP_TEXT_SIZE);
6075 TRACE("edit box text=%s\n", debugstr_w(disptextW));
6077 /* get string length in pixels */
6078 GetTextExtentPoint32W(hdc, disptextW, lstrlenW(disptextW), &sz);
6080 /* add extra spacing for the next character */
6081 GetTextMetricsW(hdc, &tm);
6082 sz.cx += tm.tmMaxCharWidth * 2;
6085 SelectObject(hdc, hOldFont);
6087 ReleaseDC(infoPtr->hwndSelf, hdc);
6089 sz.cy = rect.bottom - rect.top + 2;
6092 TRACE("moving edit=(%d,%d)-(%d,%d)\n", rect.left, rect.top, sz.cx, sz.cy);
6093 MoveWindow(infoPtr->hwndEdit, rect.left, rect.top, sz.cx, sz.cy, FALSE);
6094 ShowWindow(infoPtr->hwndEdit, SW_NORMAL);
6095 SetFocus(infoPtr->hwndEdit);
6096 SendMessageW(infoPtr->hwndEdit, EM_SETSEL, 0, -1);
6097 return infoPtr->hwndEdit;
6103 * Ensures the specified item is visible, scrolling into view if necessary.
6106 * [I] infoPtr : valid pointer to the listview structure
6107 * [I] nItem : item index
6108 * [I] bPartial : partially or entirely visible
6114 static BOOL LISTVIEW_EnsureVisible(LISTVIEW_INFO *infoPtr, INT nItem, BOOL bPartial)
6116 INT nScrollPosHeight = 0;
6117 INT nScrollPosWidth = 0;
6118 INT nHorzAdjust = 0;
6119 INT nVertAdjust = 0;
6122 RECT rcItem, rcTemp;
6124 rcItem.left = LVIR_BOUNDS;
6125 if (!LISTVIEW_GetItemRect(infoPtr, nItem, &rcItem)) return FALSE;
6127 if (bPartial && IntersectRect(&rcTemp, &infoPtr->rcList, &rcItem)) return TRUE;
6129 if (rcItem.left < infoPtr->rcList.left || rcItem.right > infoPtr->rcList.right)
6131 /* scroll left/right, but in LV_VIEW_DETAILS mode */
6132 if (infoPtr->uView == LV_VIEW_LIST)
6133 nScrollPosWidth = infoPtr->nItemWidth;
6134 else if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
6135 nScrollPosWidth = 1;
6137 if (rcItem.left < infoPtr->rcList.left)
6140 if (infoPtr->uView != LV_VIEW_DETAILS) nHorzDiff = rcItem.left - infoPtr->rcList.left;
6145 if (infoPtr->uView != LV_VIEW_DETAILS) nHorzDiff = rcItem.right - infoPtr->rcList.right;
6149 if (rcItem.top < infoPtr->rcList.top || rcItem.bottom > infoPtr->rcList.bottom)
6151 /* scroll up/down, but not in LVS_LIST mode */
6152 if (infoPtr->uView == LV_VIEW_DETAILS)
6153 nScrollPosHeight = infoPtr->nItemHeight;
6154 else if ((infoPtr->uView == LV_VIEW_ICON) || (infoPtr->uView == LV_VIEW_SMALLICON))
6155 nScrollPosHeight = 1;
6157 if (rcItem.top < infoPtr->rcList.top)
6160 if (infoPtr->uView != LV_VIEW_LIST) nVertDiff = rcItem.top - infoPtr->rcList.top;
6165 if (infoPtr->uView != LV_VIEW_LIST) nVertDiff = rcItem.bottom - infoPtr->rcList.bottom;
6169 if (!nScrollPosWidth && !nScrollPosHeight) return TRUE;
6171 if (nScrollPosWidth)
6173 INT diff = nHorzDiff / nScrollPosWidth;
6174 if (nHorzDiff % nScrollPosWidth) diff += nHorzAdjust;
6175 LISTVIEW_HScroll(infoPtr, SB_INTERNAL, diff);
6178 if (nScrollPosHeight)
6180 INT diff = nVertDiff / nScrollPosHeight;
6181 if (nVertDiff % nScrollPosHeight) diff += nVertAdjust;
6182 LISTVIEW_VScroll(infoPtr, SB_INTERNAL, diff);
6190 * Searches for an item with specific characteristics.
6193 * [I] hwnd : window handle
6194 * [I] nStart : base item index
6195 * [I] lpFindInfo : item information to look for
6198 * SUCCESS : index of item
6201 static INT LISTVIEW_FindItemW(const LISTVIEW_INFO *infoPtr, INT nStart,
6202 const LVFINDINFOW *lpFindInfo)
6204 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
6205 BOOL bWrap = FALSE, bNearest = FALSE;
6206 INT nItem = nStart + 1, nLast = infoPtr->nItemCount, nNearestItem = -1;
6207 ULONG xdist, ydist, dist, mindist = 0x7fffffff;
6208 POINT Position, Destination;
6211 /* Search in virtual listviews should be done by application, not by
6212 listview control, so we just send LVN_ODFINDITEMW and return the result */
6213 if (infoPtr->dwStyle & LVS_OWNERDATA)
6217 nmlv.iStart = nStart;
6218 nmlv.lvfi = *lpFindInfo;
6219 return notify_hdr(infoPtr, LVN_ODFINDITEMW, (LPNMHDR)&nmlv.hdr);
6222 if (!lpFindInfo || nItem < 0) return -1;
6225 if (lpFindInfo->flags & (LVFI_STRING | LVFI_PARTIAL) ||
6226 lpFindInfo->flags & LVFI_SUBSTRING)
6228 lvItem.mask |= LVIF_TEXT;
6229 lvItem.pszText = szDispText;
6230 lvItem.cchTextMax = DISP_TEXT_SIZE;
6233 if (lpFindInfo->flags & LVFI_WRAP)
6236 if ((lpFindInfo->flags & LVFI_NEARESTXY) &&
6237 (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON))
6242 LISTVIEW_GetOrigin(infoPtr, &Origin);
6243 Destination.x = lpFindInfo->pt.x - Origin.x;
6244 Destination.y = lpFindInfo->pt.y - Origin.y;
6245 switch(lpFindInfo->vkDirection)
6247 case VK_DOWN: Destination.y += infoPtr->nItemHeight; break;
6248 case VK_UP: Destination.y -= infoPtr->nItemHeight; break;
6249 case VK_RIGHT: Destination.x += infoPtr->nItemWidth; break;
6250 case VK_LEFT: Destination.x -= infoPtr->nItemWidth; break;
6251 case VK_HOME: Destination.x = Destination.y = 0; break;
6252 case VK_NEXT: Destination.y += infoPtr->rcList.bottom - infoPtr->rcList.top; break;
6253 case VK_PRIOR: Destination.y -= infoPtr->rcList.bottom - infoPtr->rcList.top; break;
6255 LISTVIEW_GetAreaRect(infoPtr, &rcArea);
6256 Destination.x = rcArea.right;
6257 Destination.y = rcArea.bottom;
6259 default: ERR("Unknown vkDirection=%d\n", lpFindInfo->vkDirection);
6263 else Destination.x = Destination.y = 0;
6265 /* if LVFI_PARAM is specified, all other flags are ignored */
6266 if (lpFindInfo->flags & LVFI_PARAM)
6268 lvItem.mask |= LVIF_PARAM;
6270 lvItem.mask &= ~LVIF_TEXT;
6274 for (; nItem < nLast; nItem++)
6276 lvItem.iItem = nItem;
6277 lvItem.iSubItem = 0;
6278 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) continue;
6280 if (lvItem.mask & LVIF_PARAM)
6282 if (lpFindInfo->lParam == lvItem.lParam)
6288 if (lvItem.mask & LVIF_TEXT)
6290 if (lpFindInfo->flags & (LVFI_PARTIAL | LVFI_SUBSTRING))
6292 WCHAR *p = strstrW(lvItem.pszText, lpFindInfo->psz);
6293 if (!p || p != lvItem.pszText) continue;
6297 if (lstrcmpW(lvItem.pszText, lpFindInfo->psz) != 0) continue;
6301 if (!bNearest) return nItem;
6303 /* This is very inefficient. To do a good job here,
6304 * we need a sorted array of (x,y) item positions */
6305 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
6307 /* compute the distance^2 to the destination */
6308 xdist = Destination.x - Position.x;
6309 ydist = Destination.y - Position.y;
6310 dist = xdist * xdist + ydist * ydist;
6312 /* remember the distance, and item if it's closer */
6316 nNearestItem = nItem;
6323 nLast = min(nStart + 1, infoPtr->nItemCount);
6328 return nNearestItem;
6333 * Searches for an item with specific characteristics.
6336 * [I] hwnd : window handle
6337 * [I] nStart : base item index
6338 * [I] lpFindInfo : item information to look for
6341 * SUCCESS : index of item
6344 static INT LISTVIEW_FindItemA(const LISTVIEW_INFO *infoPtr, INT nStart,
6345 const LVFINDINFOA *lpFindInfo)
6347 BOOL hasText = lpFindInfo->flags & (LVFI_STRING | LVFI_PARTIAL) ||
6348 lpFindInfo->flags & LVFI_SUBSTRING;
6353 memcpy(&fiw, lpFindInfo, sizeof(fiw));
6354 if (hasText) fiw.psz = strW = textdupTtoW((LPCWSTR)lpFindInfo->psz, FALSE);
6355 res = LISTVIEW_FindItemW(infoPtr, nStart, &fiw);
6356 textfreeT(strW, FALSE);
6362 * Retrieves column attributes.
6365 * [I] infoPtr : valid pointer to the listview structure
6366 * [I] nColumn : column index
6367 * [IO] lpColumn : column information
6368 * [I] isW : if TRUE, then lpColumn is a LPLVCOLUMNW
6369 * otherwise it is in fact a LPLVCOLUMNA
6375 static BOOL LISTVIEW_GetColumnT(const LISTVIEW_INFO *infoPtr, INT nColumn, LPLVCOLUMNW lpColumn, BOOL isW)
6377 COLUMN_INFO *lpColumnInfo;
6380 if (!lpColumn || nColumn < 0 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
6381 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, nColumn);
6383 /* initialize memory */
6384 ZeroMemory(&hdi, sizeof(hdi));
6386 if (lpColumn->mask & LVCF_TEXT)
6388 hdi.mask |= HDI_TEXT;
6389 hdi.pszText = lpColumn->pszText;
6390 hdi.cchTextMax = lpColumn->cchTextMax;
6393 if (lpColumn->mask & LVCF_IMAGE)
6394 hdi.mask |= HDI_IMAGE;
6396 if (lpColumn->mask & LVCF_ORDER)
6397 hdi.mask |= HDI_ORDER;
6399 if (lpColumn->mask & LVCF_SUBITEM)
6400 hdi.mask |= HDI_LPARAM;
6402 if (!SendMessageW(infoPtr->hwndHeader, isW ? HDM_GETITEMW : HDM_GETITEMA, nColumn, (LPARAM)&hdi)) return FALSE;
6404 if (lpColumn->mask & LVCF_FMT)
6405 lpColumn->fmt = lpColumnInfo->fmt;
6407 if (lpColumn->mask & LVCF_WIDTH)
6408 lpColumn->cx = lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left;
6410 if (lpColumn->mask & LVCF_IMAGE)
6411 lpColumn->iImage = hdi.iImage;
6413 if (lpColumn->mask & LVCF_ORDER)
6414 lpColumn->iOrder = hdi.iOrder;
6416 if (lpColumn->mask & LVCF_SUBITEM)
6417 lpColumn->iSubItem = hdi.lParam;
6419 if (lpColumn->mask & LVCF_MINWIDTH)
6420 lpColumn->cxMin = lpColumnInfo->cxMin;
6426 static BOOL LISTVIEW_GetColumnOrderArray(const LISTVIEW_INFO *infoPtr, INT iCount, LPINT lpiArray)
6428 TRACE("iCount=%d, lpiArray=%p\n", iCount, lpiArray);
6433 return SendMessageW(infoPtr->hwndHeader, HDM_GETORDERARRAY, iCount, (LPARAM)lpiArray);
6438 * Retrieves the column width.
6441 * [I] infoPtr : valid pointer to the listview structure
6442 * [I] int : column index
6445 * SUCCESS : column width
6448 static INT LISTVIEW_GetColumnWidth(const LISTVIEW_INFO *infoPtr, INT nColumn)
6450 INT nColumnWidth = 0;
6453 TRACE("nColumn=%d\n", nColumn);
6455 /* we have a 'column' in LIST and REPORT mode only */
6456 switch(infoPtr->uView)
6459 nColumnWidth = infoPtr->nItemWidth;
6461 case LV_VIEW_DETAILS:
6462 /* We are not using LISTVIEW_GetHeaderRect as this data is updated only after a HDN_ITEMCHANGED.
6463 * There is an application that subclasses the listview, calls LVM_GETCOLUMNWIDTH in the
6464 * HDN_ITEMCHANGED handler and goes into infinite recursion if it receives old data.
6466 * TODO: should we do the same in LVM_GETCOLUMN?
6468 hdItem.mask = HDI_WIDTH;
6469 if (!SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nColumn, (LPARAM)&hdItem))
6471 WARN("(%p): HDM_GETITEMW failed for item %d\n", infoPtr->hwndSelf, nColumn);
6474 nColumnWidth = hdItem.cxy;
6478 TRACE("nColumnWidth=%d\n", nColumnWidth);
6479 return nColumnWidth;
6484 * In list or report display mode, retrieves the number of items that can fit
6485 * vertically in the visible area. In icon or small icon display mode,
6486 * retrieves the total number of visible items.
6489 * [I] infoPtr : valid pointer to the listview structure
6492 * Number of fully visible items.
6494 static INT LISTVIEW_GetCountPerPage(const LISTVIEW_INFO *infoPtr)
6496 switch (infoPtr->uView)
6499 case LV_VIEW_SMALLICON:
6500 return infoPtr->nItemCount;
6501 case LV_VIEW_DETAILS:
6502 return LISTVIEW_GetCountPerColumn(infoPtr);
6504 return LISTVIEW_GetCountPerRow(infoPtr) * LISTVIEW_GetCountPerColumn(infoPtr);
6512 * Retrieves an image list handle.
6515 * [I] infoPtr : valid pointer to the listview structure
6516 * [I] nImageList : image list identifier
6519 * SUCCESS : image list handle
6522 static HIMAGELIST LISTVIEW_GetImageList(const LISTVIEW_INFO *infoPtr, INT nImageList)
6526 case LVSIL_NORMAL: return infoPtr->himlNormal;
6527 case LVSIL_SMALL: return infoPtr->himlSmall;
6528 case LVSIL_STATE: return infoPtr->himlState;
6529 case LVSIL_GROUPHEADER:
6530 FIXME("LVSIL_GROUPHEADER not supported\n");
6533 WARN("got unknown imagelist index - %d\n", nImageList);
6538 /* LISTVIEW_GetISearchString */
6542 * Retrieves item attributes.
6545 * [I] hwnd : window handle
6546 * [IO] lpLVItem : item info
6547 * [I] isW : if TRUE, then lpLVItem is a LPLVITEMW,
6548 * if FALSE, then lpLVItem is a LPLVITEMA.
6551 * This is the internal 'GetItem' interface -- it tries to
6552 * be smart and avoid text copies, if possible, by modifying
6553 * lpLVItem->pszText to point to the text string. Please note
6554 * that this is not always possible (e.g. OWNERDATA), so on
6555 * entry you *must* supply valid values for pszText, and cchTextMax.
6556 * The only difference to the documented interface is that upon
6557 * return, you should use *only* the lpLVItem->pszText, rather than
6558 * the buffer pointer you provided on input. Most code already does
6559 * that, so it's not a problem.
6560 * For the two cases when the text must be copied (that is,
6561 * for LVM_GETITEM, and LVM_GETITEMTEXT), use LISTVIEW_GetItemExtT.
6567 static BOOL LISTVIEW_GetItemT(const LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem, BOOL isW)
6569 ITEMHDR callbackHdr = { LPSTR_TEXTCALLBACKW, I_IMAGECALLBACK };
6570 NMLVDISPINFOW dispInfo;
6576 TRACE("(item=%s, isW=%d)\n", debuglvitem_t(lpLVItem, isW), isW);
6578 if (!lpLVItem || lpLVItem->iItem < 0 || lpLVItem->iItem >= infoPtr->nItemCount)
6581 if (lpLVItem->mask == 0) return TRUE;
6582 TRACE("mask=%x\n", lpLVItem->mask);
6584 /* make a local copy */
6585 isubitem = lpLVItem->iSubItem;
6587 /* a quick optimization if all we're asked is the focus state
6588 * these queries are worth optimising since they are common,
6589 * and can be answered in constant time, without the heavy accesses */
6590 if ( (lpLVItem->mask == LVIF_STATE) && (lpLVItem->stateMask == LVIS_FOCUSED) &&
6591 !(infoPtr->uCallbackMask & LVIS_FOCUSED) )
6593 lpLVItem->state = 0;
6594 if (infoPtr->nFocusedItem == lpLVItem->iItem)
6595 lpLVItem->state |= LVIS_FOCUSED;
6599 ZeroMemory(&dispInfo, sizeof(dispInfo));
6601 /* if the app stores all the data, handle it separately */
6602 if (infoPtr->dwStyle & LVS_OWNERDATA)
6604 dispInfo.item.state = 0;
6606 /* apparently, we should not callback for lParam in LVS_OWNERDATA */
6607 if ((lpLVItem->mask & ~(LVIF_STATE | LVIF_PARAM)) ||
6608 ((lpLVItem->mask & LVIF_STATE) && (infoPtr->uCallbackMask & lpLVItem->stateMask)))
6610 UINT mask = lpLVItem->mask;
6612 /* NOTE: copy only fields which we _know_ are initialized, some apps
6613 * depend on the uninitialized fields being 0 */
6614 dispInfo.item.mask = lpLVItem->mask & ~LVIF_PARAM;
6615 dispInfo.item.iItem = lpLVItem->iItem;
6616 dispInfo.item.iSubItem = isubitem;
6617 if (lpLVItem->mask & LVIF_TEXT)
6619 if (lpLVItem->mask & LVIF_NORECOMPUTE)
6621 dispInfo.item.mask &= ~(LVIF_TEXT | LVIF_NORECOMPUTE);
6624 dispInfo.item.pszText = lpLVItem->pszText;
6625 dispInfo.item.cchTextMax = lpLVItem->cchTextMax;
6628 if (lpLVItem->mask & LVIF_STATE)
6629 dispInfo.item.stateMask = lpLVItem->stateMask & infoPtr->uCallbackMask;
6630 /* could be zeroed on LVIF_NORECOMPUTE case */
6631 if (dispInfo.item.mask)
6633 notify_dispinfoT(infoPtr, LVN_GETDISPINFOW, &dispInfo, isW);
6634 dispInfo.item.stateMask = lpLVItem->stateMask;
6635 if (lpLVItem->mask & (LVIF_GROUPID|LVIF_COLUMNS))
6637 /* full size structure expected - _WIN32IE >= 0x560 */
6638 *lpLVItem = dispInfo.item;
6640 else if (lpLVItem->mask & LVIF_INDENT)
6642 /* indent member expected - _WIN32IE >= 0x300 */
6643 memcpy(lpLVItem, &dispInfo.item, offsetof( LVITEMW, iGroupId ));
6647 /* minimal structure expected */
6648 memcpy(lpLVItem, &dispInfo.item, offsetof( LVITEMW, iIndent ));
6650 lpLVItem->mask = mask;
6651 TRACE(" getdispinfo(1):lpLVItem=%s\n", debuglvitem_t(lpLVItem, isW));
6655 /* make sure lParam is zeroed out */
6656 if (lpLVItem->mask & LVIF_PARAM) lpLVItem->lParam = 0;
6658 /* callback marked pointer required here */
6659 if ((lpLVItem->mask & LVIF_TEXT) && (lpLVItem->mask & LVIF_NORECOMPUTE))
6660 lpLVItem->pszText = LPSTR_TEXTCALLBACKW;
6662 /* we store only a little state, so if we're not asked, we're done */
6663 if (!(lpLVItem->mask & LVIF_STATE) || isubitem) return TRUE;
6665 /* if focus is handled by us, report it */
6666 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_FOCUSED )
6668 lpLVItem->state &= ~LVIS_FOCUSED;
6669 if (infoPtr->nFocusedItem == lpLVItem->iItem)
6670 lpLVItem->state |= LVIS_FOCUSED;
6673 /* and do the same for selection, if we handle it */
6674 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_SELECTED )
6676 lpLVItem->state &= ~LVIS_SELECTED;
6677 if (ranges_contain(infoPtr->selectionRanges, lpLVItem->iItem))
6678 lpLVItem->state |= LVIS_SELECTED;
6684 /* find the item and subitem structures before we proceed */
6685 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, lpLVItem->iItem);
6686 lpItem = DPA_GetPtr(hdpaSubItems, 0);
6691 SUBITEM_INFO *lpSubItem = LISTVIEW_GetSubItemPtr(hdpaSubItems, isubitem);
6692 pItemHdr = lpSubItem ? &lpSubItem->hdr : &callbackHdr;
6695 WARN(" iSubItem invalid (%08x), ignored.\n", isubitem);
6700 pItemHdr = &lpItem->hdr;
6702 /* Do we need to query the state from the app? */
6703 if ((lpLVItem->mask & LVIF_STATE) && infoPtr->uCallbackMask && isubitem == 0)
6705 dispInfo.item.mask |= LVIF_STATE;
6706 dispInfo.item.stateMask = infoPtr->uCallbackMask;
6709 /* Do we need to enquire about the image? */
6710 if ((lpLVItem->mask & LVIF_IMAGE) && pItemHdr->iImage == I_IMAGECALLBACK &&
6711 (isubitem == 0 || (infoPtr->dwLvExStyle & LVS_EX_SUBITEMIMAGES)))
6713 dispInfo.item.mask |= LVIF_IMAGE;
6714 dispInfo.item.iImage = I_IMAGECALLBACK;
6717 /* Only items support indentation */
6718 if ((lpLVItem->mask & LVIF_INDENT) && lpItem->iIndent == I_INDENTCALLBACK &&
6721 dispInfo.item.mask |= LVIF_INDENT;
6722 dispInfo.item.iIndent = I_INDENTCALLBACK;
6725 /* Apps depend on calling back for text if it is NULL or LPSTR_TEXTCALLBACKW */
6726 if ((lpLVItem->mask & LVIF_TEXT) && !(lpLVItem->mask & LVIF_NORECOMPUTE) &&
6727 !is_text(pItemHdr->pszText))
6729 dispInfo.item.mask |= LVIF_TEXT;
6730 dispInfo.item.pszText = lpLVItem->pszText;
6731 dispInfo.item.cchTextMax = lpLVItem->cchTextMax;
6732 if (dispInfo.item.pszText && dispInfo.item.cchTextMax > 0)
6733 *dispInfo.item.pszText = '\0';
6736 /* If we don't have all the requested info, query the application */
6737 if (dispInfo.item.mask)
6739 dispInfo.item.iItem = lpLVItem->iItem;
6740 dispInfo.item.iSubItem = lpLVItem->iSubItem; /* yes: the original subitem */
6741 dispInfo.item.lParam = lpItem->lParam;
6742 notify_dispinfoT(infoPtr, LVN_GETDISPINFOW, &dispInfo, isW);
6743 TRACE(" getdispinfo(2):item=%s\n", debuglvitem_t(&dispInfo.item, isW));
6746 /* we should not store values for subitems */
6747 if (isubitem) dispInfo.item.mask &= ~LVIF_DI_SETITEM;
6749 /* Now, handle the iImage field */
6750 if (dispInfo.item.mask & LVIF_IMAGE)
6752 lpLVItem->iImage = dispInfo.item.iImage;
6753 if ((dispInfo.item.mask & LVIF_DI_SETITEM) && pItemHdr->iImage == I_IMAGECALLBACK)
6754 pItemHdr->iImage = dispInfo.item.iImage;
6756 else if (lpLVItem->mask & LVIF_IMAGE)
6758 if(isubitem == 0 || (infoPtr->dwLvExStyle & LVS_EX_SUBITEMIMAGES))
6759 lpLVItem->iImage = pItemHdr->iImage;
6761 lpLVItem->iImage = 0;
6764 /* The pszText field */
6765 if (dispInfo.item.mask & LVIF_TEXT)
6767 if ((dispInfo.item.mask & LVIF_DI_SETITEM) && pItemHdr->pszText)
6768 textsetptrT(&pItemHdr->pszText, dispInfo.item.pszText, isW);
6770 lpLVItem->pszText = dispInfo.item.pszText;
6772 else if (lpLVItem->mask & LVIF_TEXT)
6774 /* if LVN_GETDISPINFO's disabled with LVIF_NORECOMPUTE return callback placeholder */
6775 if (isW || !is_text(pItemHdr->pszText)) lpLVItem->pszText = pItemHdr->pszText;
6776 else textcpynT(lpLVItem->pszText, isW, pItemHdr->pszText, TRUE, lpLVItem->cchTextMax);
6779 /* Next is the lParam field */
6780 if (dispInfo.item.mask & LVIF_PARAM)
6782 lpLVItem->lParam = dispInfo.item.lParam;
6783 if ((dispInfo.item.mask & LVIF_DI_SETITEM))
6784 lpItem->lParam = dispInfo.item.lParam;
6786 else if (lpLVItem->mask & LVIF_PARAM)
6787 lpLVItem->lParam = lpItem->lParam;
6789 /* if this is a subitem, we're done */
6790 if (isubitem) return TRUE;
6792 /* ... the state field (this one is different due to uCallbackmask) */
6793 if (lpLVItem->mask & LVIF_STATE)
6795 lpLVItem->state = lpItem->state & lpLVItem->stateMask;
6796 if (dispInfo.item.mask & LVIF_STATE)
6798 lpLVItem->state &= ~dispInfo.item.stateMask;
6799 lpLVItem->state |= (dispInfo.item.state & dispInfo.item.stateMask);
6801 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_FOCUSED )
6803 lpLVItem->state &= ~LVIS_FOCUSED;
6804 if (infoPtr->nFocusedItem == lpLVItem->iItem)
6805 lpLVItem->state |= LVIS_FOCUSED;
6807 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_SELECTED )
6809 lpLVItem->state &= ~LVIS_SELECTED;
6810 if (ranges_contain(infoPtr->selectionRanges, lpLVItem->iItem))
6811 lpLVItem->state |= LVIS_SELECTED;
6815 /* and last, but not least, the indent field */
6816 if (dispInfo.item.mask & LVIF_INDENT)
6818 lpLVItem->iIndent = dispInfo.item.iIndent;
6819 if ((dispInfo.item.mask & LVIF_DI_SETITEM) && lpItem->iIndent == I_INDENTCALLBACK)
6820 lpItem->iIndent = dispInfo.item.iIndent;
6822 else if (lpLVItem->mask & LVIF_INDENT)
6824 lpLVItem->iIndent = lpItem->iIndent;
6832 * Retrieves item attributes.
6835 * [I] hwnd : window handle
6836 * [IO] lpLVItem : item info
6837 * [I] isW : if TRUE, then lpLVItem is a LPLVITEMW,
6838 * if FALSE, then lpLVItem is a LPLVITEMA.
6841 * This is the external 'GetItem' interface -- it properly copies
6842 * the text in the provided buffer.
6848 static BOOL LISTVIEW_GetItemExtT(const LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem, BOOL isW)
6853 if (!lpLVItem || lpLVItem->iItem < 0 || lpLVItem->iItem >= infoPtr->nItemCount)
6856 pszText = lpLVItem->pszText;
6857 bResult = LISTVIEW_GetItemT(infoPtr, lpLVItem, isW);
6858 if (bResult && (lpLVItem->mask & LVIF_TEXT) && lpLVItem->pszText != pszText)
6860 if (lpLVItem->pszText != LPSTR_TEXTCALLBACKW)
6861 textcpynT(pszText, isW, lpLVItem->pszText, isW, lpLVItem->cchTextMax);
6863 pszText = LPSTR_TEXTCALLBACKW;
6865 lpLVItem->pszText = pszText;
6873 * Retrieves the position (upper-left) of the listview control item.
6874 * Note that for LVS_ICON style, the upper-left is that of the icon
6875 * and not the bounding box.
6878 * [I] infoPtr : valid pointer to the listview structure
6879 * [I] nItem : item index
6880 * [O] lpptPosition : coordinate information
6886 static BOOL LISTVIEW_GetItemPosition(const LISTVIEW_INFO *infoPtr, INT nItem, LPPOINT lpptPosition)
6890 TRACE("(nItem=%d, lpptPosition=%p)\n", nItem, lpptPosition);
6892 if (!lpptPosition || nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
6894 LISTVIEW_GetOrigin(infoPtr, &Origin);
6895 LISTVIEW_GetItemOrigin(infoPtr, nItem, lpptPosition);
6897 if (infoPtr->uView == LV_VIEW_ICON)
6899 lpptPosition->x += (infoPtr->nItemWidth - infoPtr->iconSize.cx) / 2;
6900 lpptPosition->y += ICON_TOP_PADDING;
6902 lpptPosition->x += Origin.x;
6903 lpptPosition->y += Origin.y;
6905 TRACE (" lpptPosition=%s\n", wine_dbgstr_point(lpptPosition));
6912 * Retrieves the bounding rectangle for a listview control item.
6915 * [I] infoPtr : valid pointer to the listview structure
6916 * [I] nItem : item index
6917 * [IO] lprc : bounding rectangle coordinates
6918 * lprc->left specifies the portion of the item for which the bounding
6919 * rectangle will be retrieved.
6921 * LVIR_BOUNDS Returns the bounding rectangle of the entire item,
6922 * including the icon and label.
6925 * * Experiment shows that native control returns:
6926 * * width = min (48, length of text line)
6927 * * .left = position.x - (width - iconsize.cx)/2
6928 * * .right = .left + width
6929 * * height = #lines of text * ntmHeight + icon height + 8
6930 * * .top = position.y - 2
6931 * * .bottom = .top + height
6932 * * separation between items .y = itemSpacing.cy - height
6933 * * .x = itemSpacing.cx - width
6934 * LVIR_ICON Returns the bounding rectangle of the icon or small icon.
6937 * * Experiment shows that native control returns:
6938 * * width = iconSize.cx + 16
6939 * * .left = position.x - (width - iconsize.cx)/2
6940 * * .right = .left + width
6941 * * height = iconSize.cy + 4
6942 * * .top = position.y - 2
6943 * * .bottom = .top + height
6944 * * separation between items .y = itemSpacing.cy - height
6945 * * .x = itemSpacing.cx - width
6946 * LVIR_LABEL Returns the bounding rectangle of the item text.
6949 * * Experiment shows that native control returns:
6950 * * width = text length
6951 * * .left = position.x - width/2
6952 * * .right = .left + width
6953 * * height = ntmH * linecount + 2
6954 * * .top = position.y + iconSize.cy + 6
6955 * * .bottom = .top + height
6956 * * separation between items .y = itemSpacing.cy - height
6957 * * .x = itemSpacing.cx - width
6958 * LVIR_SELECTBOUNDS Returns the union of the LVIR_ICON and LVIR_LABEL
6959 * rectangles, but excludes columns in report view.
6966 * Note that the bounding rectangle of the label in the LVS_ICON view depends
6967 * upon whether the window has the focus currently and on whether the item
6968 * is the one with the focus. Ensure that the control's record of which
6969 * item has the focus agrees with the items' records.
6971 static BOOL LISTVIEW_GetItemRect(const LISTVIEW_INFO *infoPtr, INT nItem, LPRECT lprc)
6973 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
6974 BOOL doLabel = TRUE, oversizedBox = FALSE;
6975 POINT Position, Origin;
6979 TRACE("(hwnd=%p, nItem=%d, lprc=%p)\n", infoPtr->hwndSelf, nItem, lprc);
6981 if (!lprc || nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
6983 LISTVIEW_GetOrigin(infoPtr, &Origin);
6984 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
6986 /* Be smart and try to figure out the minimum we have to do */
6987 if (lprc->left == LVIR_ICON) doLabel = FALSE;
6988 if (infoPtr->uView == LV_VIEW_DETAILS && lprc->left == LVIR_BOUNDS) doLabel = FALSE;
6989 if (infoPtr->uView == LV_VIEW_ICON && lprc->left != LVIR_ICON &&
6990 infoPtr->bFocus && LISTVIEW_GetItemState(infoPtr, nItem, LVIS_FOCUSED))
6991 oversizedBox = TRUE;
6993 /* get what we need from the item before hand, so we make
6994 * only one request. This can speed up things, if data
6995 * is stored on the app side */
6997 if (infoPtr->uView == LV_VIEW_DETAILS) lvItem.mask |= LVIF_INDENT;
6998 if (doLabel) lvItem.mask |= LVIF_TEXT;
6999 lvItem.iItem = nItem;
7000 lvItem.iSubItem = 0;
7001 lvItem.pszText = szDispText;
7002 lvItem.cchTextMax = DISP_TEXT_SIZE;
7003 if (lvItem.mask && !LISTVIEW_GetItemW(infoPtr, &lvItem)) return FALSE;
7004 /* we got the state already up, simulate it here, to avoid a reget */
7005 if (infoPtr->uView == LV_VIEW_ICON && (lprc->left != LVIR_ICON))
7007 lvItem.mask |= LVIF_STATE;
7008 lvItem.stateMask = LVIS_FOCUSED;
7009 lvItem.state = (oversizedBox ? LVIS_FOCUSED : 0);
7012 if (infoPtr->uView == LV_VIEW_DETAILS && (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) && lprc->left == LVIR_SELECTBOUNDS)
7013 lprc->left = LVIR_BOUNDS;
7019 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, NULL, lprc, NULL, NULL);
7023 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, NULL, NULL, NULL, lprc);
7027 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, lprc, NULL, NULL, NULL, NULL);
7030 case LVIR_SELECTBOUNDS:
7031 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, lprc, NULL, NULL, NULL);
7035 WARN("Unknown value: %d\n", lprc->left);
7039 if (infoPtr->uView == LV_VIEW_DETAILS)
7041 if (mode != LVIR_BOUNDS)
7042 OffsetRect(lprc, Origin.x + LISTVIEW_GetColumnInfo(infoPtr, 0)->rcHeader.left,
7043 Position.y + Origin.y);
7045 OffsetRect(lprc, Origin.x, Position.y + Origin.y);
7048 OffsetRect(lprc, Position.x + Origin.x, Position.y + Origin.y);
7050 TRACE(" rect=%s\n", wine_dbgstr_rect(lprc));
7057 * Retrieves the spacing between listview control items.
7060 * [I] infoPtr : valid pointer to the listview structure
7061 * [IO] lprc : rectangle to receive the output
7062 * on input, lprc->top = nSubItem
7063 * lprc->left = LVIR_ICON | LVIR_BOUNDS | LVIR_LABEL
7065 * NOTE: for subItem = 0, we should return the bounds of the _entire_ item,
7066 * not only those of the first column.
7072 static BOOL LISTVIEW_GetSubItemRect(const LISTVIEW_INFO *infoPtr, INT item, LPRECT lprc)
7074 RECT rect = { 0, 0, 0, 0 };
7078 if (!lprc) return FALSE;
7080 TRACE("(item=%d, subitem=%d, type=%d)\n", item, lprc->top, lprc->left);
7081 /* Subitem of '0' means item itself, and this works for all control view modes */
7083 return LISTVIEW_GetItemRect(infoPtr, item, lprc);
7085 if (infoPtr->uView != LV_VIEW_DETAILS) return FALSE;
7087 LISTVIEW_GetOrigin(infoPtr, &origin);
7088 /* this works for any item index, no matter if it exists or not */
7089 y = item * infoPtr->nItemHeight + origin.y;
7091 if (infoPtr->hwndHeader && SendMessageW(infoPtr->hwndHeader, HDM_GETITEMRECT, lprc->top, (LPARAM)&rect))
7094 rect.bottom = infoPtr->nItemHeight;
7098 /* Native implementation is broken for this case and garbage is left for left and right fields,
7099 we zero them to get predictable output */
7100 lprc->left = lprc->right = lprc->top = 0;
7101 lprc->bottom = infoPtr->nItemHeight;
7102 OffsetRect(lprc, origin.x, y);
7103 TRACE("return rect %s\n", wine_dbgstr_rect(lprc));
7111 /* it doesn't matter if main item actually has an icon, if imagelist is set icon width is returned */
7112 if (infoPtr->himlSmall)
7113 rect.right = rect.left + infoPtr->iconSize.cx;
7115 rect.right = rect.left;
7117 rect.bottom = rect.top + infoPtr->iconSize.cy;
7125 ERR("Unknown bounds=%d\n", lprc->left);
7129 OffsetRect(&rect, origin.x, y);
7131 TRACE("return rect %s\n", wine_dbgstr_rect(lprc));
7138 * Retrieves the spacing between listview control items.
7141 * [I] infoPtr : valid pointer to the listview structure
7142 * [I] bSmall : flag for small or large icon
7145 * Horizontal + vertical spacing
7147 static LONG LISTVIEW_GetItemSpacing(const LISTVIEW_INFO *infoPtr, BOOL bSmall)
7153 lResult = MAKELONG(infoPtr->iconSpacing.cx, infoPtr->iconSpacing.cy);
7157 if (infoPtr->uView == LV_VIEW_ICON)
7158 lResult = MAKELONG(DEFAULT_COLUMN_WIDTH, GetSystemMetrics(SM_CXSMICON)+HEIGHT_PADDING);
7160 lResult = MAKELONG(infoPtr->nItemWidth, infoPtr->nItemHeight);
7167 * Retrieves the state of a listview control item.
7170 * [I] infoPtr : valid pointer to the listview structure
7171 * [I] nItem : item index
7172 * [I] uMask : state mask
7175 * State specified by the mask.
7177 static UINT LISTVIEW_GetItemState(const LISTVIEW_INFO *infoPtr, INT nItem, UINT uMask)
7181 if (nItem < 0 || nItem >= infoPtr->nItemCount) return 0;
7183 lvItem.iItem = nItem;
7184 lvItem.iSubItem = 0;
7185 lvItem.mask = LVIF_STATE;
7186 lvItem.stateMask = uMask;
7187 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return 0;
7189 return lvItem.state & uMask;
7194 * Retrieves the text of a listview control item or subitem.
7197 * [I] hwnd : window handle
7198 * [I] nItem : item index
7199 * [IO] lpLVItem : item information
7200 * [I] isW : TRUE if lpLVItem is Unicode
7203 * SUCCESS : string length
7206 static INT LISTVIEW_GetItemTextT(const LISTVIEW_INFO *infoPtr, INT nItem, LPLVITEMW lpLVItem, BOOL isW)
7208 if (!lpLVItem || nItem < 0 || nItem >= infoPtr->nItemCount) return 0;
7210 lpLVItem->mask = LVIF_TEXT;
7211 lpLVItem->iItem = nItem;
7212 if (!LISTVIEW_GetItemExtT(infoPtr, lpLVItem, isW)) return 0;
7214 return textlenT(lpLVItem->pszText, isW);
7219 * Searches for an item based on properties + relationships.
7222 * [I] infoPtr : valid pointer to the listview structure
7223 * [I] nItem : item index
7224 * [I] uFlags : relationship flag
7227 * SUCCESS : item index
7230 static INT LISTVIEW_GetNextItem(const LISTVIEW_INFO *infoPtr, INT nItem, UINT uFlags)
7233 LVFINDINFOW lvFindInfo;
7234 INT nCountPerColumn;
7238 TRACE("nItem=%d, uFlags=%x, nItemCount=%d\n", nItem, uFlags, infoPtr->nItemCount);
7239 if (nItem < -1 || nItem >= infoPtr->nItemCount) return -1;
7241 ZeroMemory(&lvFindInfo, sizeof(lvFindInfo));
7243 if (uFlags & LVNI_CUT)
7246 if (uFlags & LVNI_DROPHILITED)
7247 uMask |= LVIS_DROPHILITED;
7249 if (uFlags & LVNI_FOCUSED)
7250 uMask |= LVIS_FOCUSED;
7252 if (uFlags & LVNI_SELECTED)
7253 uMask |= LVIS_SELECTED;
7255 /* if we're asked for the focused item, that's only one,
7256 * so it's worth optimizing */
7257 if (uFlags & LVNI_FOCUSED)
7259 if ((LISTVIEW_GetItemState(infoPtr, infoPtr->nFocusedItem, uMask) & uMask) != uMask) return -1;
7260 return (infoPtr->nFocusedItem == nItem) ? -1 : infoPtr->nFocusedItem;
7263 if (uFlags & LVNI_ABOVE)
7265 if ((infoPtr->uView == LV_VIEW_LIST) || (infoPtr->uView == LV_VIEW_DETAILS))
7270 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7276 /* Special case for autoarrange - move 'til the top of a list */
7277 if (is_autoarrange(infoPtr))
7279 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
7280 while (nItem - nCountPerRow >= 0)
7282 nItem -= nCountPerRow;
7283 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7288 lvFindInfo.flags = LVFI_NEARESTXY;
7289 lvFindInfo.vkDirection = VK_UP;
7290 LISTVIEW_GetItemPosition(infoPtr, nItem, &lvFindInfo.pt);
7291 while ((nItem = LISTVIEW_FindItemW(infoPtr, nItem, &lvFindInfo)) != -1)
7293 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7298 else if (uFlags & LVNI_BELOW)
7300 if ((infoPtr->uView == LV_VIEW_LIST) || (infoPtr->uView == LV_VIEW_DETAILS))
7302 while (nItem < infoPtr->nItemCount)
7305 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7311 /* Special case for autoarrange - move 'til the bottom of a list */
7312 if (is_autoarrange(infoPtr))
7314 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
7315 while (nItem + nCountPerRow < infoPtr->nItemCount )
7317 nItem += nCountPerRow;
7318 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7323 lvFindInfo.flags = LVFI_NEARESTXY;
7324 lvFindInfo.vkDirection = VK_DOWN;
7325 LISTVIEW_GetItemPosition(infoPtr, nItem, &lvFindInfo.pt);
7326 while ((nItem = LISTVIEW_FindItemW(infoPtr, nItem, &lvFindInfo)) != -1)
7328 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7333 else if (uFlags & LVNI_TOLEFT)
7335 if (infoPtr->uView == LV_VIEW_LIST)
7337 nCountPerColumn = LISTVIEW_GetCountPerColumn(infoPtr);
7338 while (nItem - nCountPerColumn >= 0)
7340 nItem -= nCountPerColumn;
7341 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7345 else if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
7347 /* Special case for autoarrange - move 'til the beginning of a row */
7348 if (is_autoarrange(infoPtr))
7350 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
7351 while (nItem % nCountPerRow > 0)
7354 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7359 lvFindInfo.flags = LVFI_NEARESTXY;
7360 lvFindInfo.vkDirection = VK_LEFT;
7361 LISTVIEW_GetItemPosition(infoPtr, nItem, &lvFindInfo.pt);
7362 while ((nItem = LISTVIEW_FindItemW(infoPtr, nItem, &lvFindInfo)) != -1)
7364 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7369 else if (uFlags & LVNI_TORIGHT)
7371 if (infoPtr->uView == LV_VIEW_LIST)
7373 nCountPerColumn = LISTVIEW_GetCountPerColumn(infoPtr);
7374 while (nItem + nCountPerColumn < infoPtr->nItemCount)
7376 nItem += nCountPerColumn;
7377 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7381 else if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
7383 /* Special case for autoarrange - move 'til the end of a row */
7384 if (is_autoarrange(infoPtr))
7386 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
7387 while (nItem % nCountPerRow < nCountPerRow - 1 )
7390 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7395 lvFindInfo.flags = LVFI_NEARESTXY;
7396 lvFindInfo.vkDirection = VK_RIGHT;
7397 LISTVIEW_GetItemPosition(infoPtr, nItem, &lvFindInfo.pt);
7398 while ((nItem = LISTVIEW_FindItemW(infoPtr, nItem, &lvFindInfo)) != -1)
7400 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7409 /* search by index */
7410 for (i = nItem; i < infoPtr->nItemCount; i++)
7412 if ((LISTVIEW_GetItemState(infoPtr, i, uMask) & uMask) == uMask)
7420 /* LISTVIEW_GetNumberOfWorkAreas */
7424 * Retrieves the origin coordinates when in icon or small icon display mode.
7427 * [I] infoPtr : valid pointer to the listview structure
7428 * [O] lpptOrigin : coordinate information
7433 static void LISTVIEW_GetOrigin(const LISTVIEW_INFO *infoPtr, LPPOINT lpptOrigin)
7435 INT nHorzPos = 0, nVertPos = 0;
7436 SCROLLINFO scrollInfo;
7438 scrollInfo.cbSize = sizeof(SCROLLINFO);
7439 scrollInfo.fMask = SIF_POS;
7441 if (GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo))
7442 nHorzPos = scrollInfo.nPos;
7443 if (GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
7444 nVertPos = scrollInfo.nPos;
7446 TRACE("nHorzPos=%d, nVertPos=%d\n", nHorzPos, nVertPos);
7448 lpptOrigin->x = infoPtr->rcList.left;
7449 lpptOrigin->y = infoPtr->rcList.top;
7450 if (infoPtr->uView == LV_VIEW_LIST)
7451 nHorzPos *= infoPtr->nItemWidth;
7452 else if (infoPtr->uView == LV_VIEW_DETAILS)
7453 nVertPos *= infoPtr->nItemHeight;
7455 lpptOrigin->x -= nHorzPos;
7456 lpptOrigin->y -= nVertPos;
7458 TRACE(" origin=%s\n", wine_dbgstr_point(lpptOrigin));
7463 * Retrieves the width of a string.
7466 * [I] hwnd : window handle
7467 * [I] lpszText : text string to process
7468 * [I] isW : TRUE if lpszText is Unicode, FALSE otherwise
7471 * SUCCESS : string width (in pixels)
7474 static INT LISTVIEW_GetStringWidthT(const LISTVIEW_INFO *infoPtr, LPCWSTR lpszText, BOOL isW)
7479 if (is_text(lpszText))
7481 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
7482 HDC hdc = GetDC(infoPtr->hwndSelf);
7483 HFONT hOldFont = SelectObject(hdc, hFont);
7486 GetTextExtentPointW(hdc, lpszText, lstrlenW(lpszText), &stringSize);
7488 GetTextExtentPointA(hdc, (LPCSTR)lpszText, lstrlenA((LPCSTR)lpszText), &stringSize);
7489 SelectObject(hdc, hOldFont);
7490 ReleaseDC(infoPtr->hwndSelf, hdc);
7492 return stringSize.cx;
7497 * Determines which listview item is located at the specified position.
7500 * [I] infoPtr : valid pointer to the listview structure
7501 * [IO] lpht : hit test information
7502 * [I] subitem : fill out iSubItem.
7503 * [I] select : return the index only if the hit selects the item
7506 * (mm 20001022): We must not allow iSubItem to be touched, for
7507 * an app might pass only a structure with space up to iItem!
7508 * (MS Office 97 does that for instance in the file open dialog)
7511 * SUCCESS : item index
7514 static INT LISTVIEW_HitTest(const LISTVIEW_INFO *infoPtr, LPLVHITTESTINFO lpht, BOOL subitem, BOOL select)
7516 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
7517 RECT rcBox, rcBounds, rcState, rcIcon, rcLabel, rcSearch;
7518 POINT Origin, Position, opt;
7523 TRACE("(pt=%s, subitem=%d, select=%d)\n", wine_dbgstr_point(&lpht->pt), subitem, select);
7527 if (subitem) lpht->iSubItem = 0;
7529 LISTVIEW_GetOrigin(infoPtr, &Origin);
7531 /* set whole list relation flags */
7532 if (subitem && infoPtr->uView == LV_VIEW_DETAILS)
7534 /* LVM_SUBITEMHITTEST checks left bound of possible client area */
7535 if (infoPtr->rcList.left > lpht->pt.x && Origin.x < lpht->pt.x)
7536 lpht->flags |= LVHT_TOLEFT;
7538 if (lpht->pt.y < infoPtr->rcList.top && lpht->pt.y >= 0)
7539 opt.y = lpht->pt.y + infoPtr->rcList.top;
7543 if (infoPtr->rcList.bottom < opt.y)
7544 lpht->flags |= LVHT_BELOW;
7548 if (infoPtr->rcList.left > lpht->pt.x)
7549 lpht->flags |= LVHT_TOLEFT;
7550 else if (infoPtr->rcList.right < lpht->pt.x)
7551 lpht->flags |= LVHT_TORIGHT;
7553 if (infoPtr->rcList.top > lpht->pt.y)
7554 lpht->flags |= LVHT_ABOVE;
7555 else if (infoPtr->rcList.bottom < lpht->pt.y)
7556 lpht->flags |= LVHT_BELOW;
7559 /* even if item is invalid try to find subitem */
7560 if (infoPtr->uView == LV_VIEW_DETAILS && subitem)
7565 opt.x = lpht->pt.x - Origin.x;
7567 lpht->iSubItem = -1;
7568 for (j = 0; j < DPA_GetPtrCount(infoPtr->hdpaColumns); j++)
7570 pRect = &LISTVIEW_GetColumnInfo(infoPtr, j)->rcHeader;
7572 if ((opt.x >= pRect->left) && (opt.x < pRect->right))
7578 TRACE("lpht->iSubItem=%d\n", lpht->iSubItem);
7580 /* if we're outside horizontal columns bounds there's nothing to test further */
7581 if (lpht->iSubItem == -1)
7584 lpht->flags = LVHT_NOWHERE;
7589 TRACE("lpht->flags=0x%x\n", lpht->flags);
7590 if (lpht->flags) return -1;
7592 lpht->flags |= LVHT_NOWHERE;
7594 /* first deal with the large items */
7595 rcSearch.left = lpht->pt.x;
7596 rcSearch.top = lpht->pt.y;
7597 rcSearch.right = rcSearch.left + 1;
7598 rcSearch.bottom = rcSearch.top + 1;
7600 iterator_frameditems(&i, infoPtr, &rcSearch);
7601 iterator_next(&i); /* go to first item in the sequence */
7603 iterator_destroy(&i);
7605 TRACE("lpht->iItem=%d\n", iItem);
7606 if (iItem == -1) return -1;
7608 lvItem.mask = LVIF_STATE | LVIF_TEXT;
7609 if (infoPtr->uView == LV_VIEW_DETAILS) lvItem.mask |= LVIF_INDENT;
7610 lvItem.stateMask = LVIS_STATEIMAGEMASK;
7611 if (infoPtr->uView == LV_VIEW_ICON) lvItem.stateMask |= LVIS_FOCUSED;
7612 lvItem.iItem = iItem;
7613 lvItem.iSubItem = subitem ? lpht->iSubItem : 0;
7614 lvItem.pszText = szDispText;
7615 lvItem.cchTextMax = DISP_TEXT_SIZE;
7616 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return -1;
7617 if (!infoPtr->bFocus) lvItem.state &= ~LVIS_FOCUSED;
7619 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, &rcBox, NULL, &rcIcon, &rcState, &rcLabel);
7620 LISTVIEW_GetItemOrigin(infoPtr, iItem, &Position);
7621 opt.x = lpht->pt.x - Position.x - Origin.x;
7623 if (lpht->pt.y < infoPtr->rcList.top && lpht->pt.y >= 0)
7624 opt.y = lpht->pt.y - Position.y - Origin.y + infoPtr->rcList.top;
7626 opt.y = lpht->pt.y - Position.y - Origin.y;
7628 if (infoPtr->uView == LV_VIEW_DETAILS)
7631 if (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT)
7632 opt.x = lpht->pt.x - Origin.x;
7636 UnionRect(&rcBounds, &rcIcon, &rcLabel);
7637 UnionRect(&rcBounds, &rcBounds, &rcState);
7639 TRACE("rcBounds=%s\n", wine_dbgstr_rect(&rcBounds));
7640 if (!PtInRect(&rcBounds, opt)) return -1;
7642 if (PtInRect(&rcIcon, opt))
7643 lpht->flags |= LVHT_ONITEMICON;
7644 else if (PtInRect(&rcLabel, opt))
7645 lpht->flags |= LVHT_ONITEMLABEL;
7646 else if (infoPtr->himlState && PtInRect(&rcState, opt))
7647 lpht->flags |= LVHT_ONITEMSTATEICON;
7648 /* special case for LVS_EX_FULLROWSELECT */
7649 if (infoPtr->uView == LV_VIEW_DETAILS && infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT &&
7650 !(lpht->flags & LVHT_ONITEM))
7652 lpht->flags = LVHT_ONITEM | LVHT_ABOVE;
7654 if (lpht->flags & LVHT_ONITEM)
7655 lpht->flags &= ~LVHT_NOWHERE;
7656 TRACE("lpht->flags=0x%x\n", lpht->flags);
7658 if (select && !(infoPtr->uView == LV_VIEW_DETAILS &&
7659 ((infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) ||
7660 (infoPtr->dwStyle & LVS_OWNERDRAWFIXED))))
7662 if (infoPtr->uView == LV_VIEW_DETAILS)
7664 /* get main item bounds */
7665 lvItem.iSubItem = 0;
7666 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, &rcBox, NULL, &rcIcon, &rcState, &rcLabel);
7667 UnionRect(&rcBounds, &rcIcon, &rcLabel);
7668 UnionRect(&rcBounds, &rcBounds, &rcState);
7670 if (!PtInRect(&rcBounds, opt)) iItem = -1;
7672 return lpht->iItem = iItem;
7677 * Inserts a new item in the listview control.
7680 * [I] infoPtr : valid pointer to the listview structure
7681 * [I] lpLVItem : item information
7682 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
7685 * SUCCESS : new item index
7688 static INT LISTVIEW_InsertItemT(LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem, BOOL isW)
7695 BOOL is_sorted, has_changed;
7697 HWND hwndSelf = infoPtr->hwndSelf;
7699 TRACE("(item=%s, isW=%d)\n", debuglvitem_t(lpLVItem, isW), isW);
7701 if (infoPtr->dwStyle & LVS_OWNERDATA) return infoPtr->nItemCount++;
7703 /* make sure it's an item, and not a subitem; cannot insert a subitem */
7704 if (!lpLVItem || lpLVItem->iSubItem) return -1;
7706 if (!is_assignable_item(lpLVItem, infoPtr->dwStyle)) return -1;
7708 if (!(lpItem = Alloc(sizeof(ITEM_INFO)))) return -1;
7710 /* insert item in listview control data structure */
7711 if ( !(hdpaSubItems = DPA_Create(8)) ) goto fail;
7712 if ( !DPA_SetPtr(hdpaSubItems, 0, lpItem) ) assert (FALSE);
7714 /* link with id struct */
7715 if (!(lpID = Alloc(sizeof(ITEM_ID)))) goto fail;
7717 lpID->item = hdpaSubItems;
7718 lpID->id = get_next_itemid(infoPtr);
7719 if ( DPA_InsertPtr(infoPtr->hdpaItemIds, infoPtr->nItemCount, lpID) == -1) goto fail;
7721 is_sorted = (infoPtr->dwStyle & (LVS_SORTASCENDING | LVS_SORTDESCENDING)) &&
7722 !(infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (LPSTR_TEXTCALLBACKW != lpLVItem->pszText);
7724 if (lpLVItem->iItem < 0 && !is_sorted) return -1;
7726 /* calculate new item index */
7733 while (i < infoPtr->nItemCount)
7735 hItem = DPA_GetPtr( infoPtr->hdpaItems, i);
7736 item_s = DPA_GetPtr(hItem, 0);
7738 cmpv = textcmpWT(item_s->hdr.pszText, lpLVItem->pszText, isW);
7739 if (infoPtr->dwStyle & LVS_SORTDESCENDING) cmpv *= -1;
7741 if (cmpv >= 0) break;
7747 nItem = min(lpLVItem->iItem, infoPtr->nItemCount);
7749 TRACE("inserting at %d, sorted=%d, count=%d, iItem=%d\n", nItem, is_sorted, infoPtr->nItemCount, lpLVItem->iItem);
7750 nItem = DPA_InsertPtr( infoPtr->hdpaItems, nItem, hdpaSubItems );
7751 if (nItem == -1) goto fail;
7752 infoPtr->nItemCount++;
7754 /* shift indices first so they don't get tangled */
7755 LISTVIEW_ShiftIndices(infoPtr, nItem, 1);
7757 /* set the item attributes */
7758 if (lpLVItem->mask & (LVIF_GROUPID|LVIF_COLUMNS))
7760 /* full size structure expected - _WIN32IE >= 0x560 */
7763 else if (lpLVItem->mask & LVIF_INDENT)
7765 /* indent member expected - _WIN32IE >= 0x300 */
7766 memcpy(&item, lpLVItem, offsetof( LVITEMW, iGroupId ));
7770 /* minimal structure expected */
7771 memcpy(&item, lpLVItem, offsetof( LVITEMW, iIndent ));
7774 if (infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES)
7776 item.mask |= LVIF_STATE;
7777 item.stateMask |= LVIS_STATEIMAGEMASK;
7778 item.state &= ~LVIS_STATEIMAGEMASK;
7779 item.state |= INDEXTOSTATEIMAGEMASK(1);
7782 if (!set_main_item(infoPtr, &item, TRUE, isW, &has_changed)) goto undo;
7784 /* make room for the position, if we are in the right mode */
7785 if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
7787 if (DPA_InsertPtr(infoPtr->hdpaPosX, nItem, 0) == -1)
7789 if (DPA_InsertPtr(infoPtr->hdpaPosY, nItem, 0) == -1)
7791 DPA_DeletePtr(infoPtr->hdpaPosX, nItem);
7796 /* send LVN_INSERTITEM notification */
7797 memset(&nmlv, 0, sizeof(NMLISTVIEW));
7799 nmlv.lParam = lpItem->lParam;
7800 notify_listview(infoPtr, LVN_INSERTITEM, &nmlv);
7801 if (!IsWindow(hwndSelf))
7804 /* align items (set position of each item) */
7805 if (infoPtr->uView == LV_VIEW_SMALLICON || infoPtr->uView == LV_VIEW_ICON)
7809 if (infoPtr->dwStyle & LVS_ALIGNLEFT)
7810 LISTVIEW_NextIconPosLeft(infoPtr, &pt);
7812 LISTVIEW_NextIconPosTop(infoPtr, &pt);
7814 LISTVIEW_MoveIconTo(infoPtr, nItem, &pt, TRUE);
7817 /* now is the invalidation fun */
7818 LISTVIEW_ScrollOnInsert(infoPtr, nItem, 1);
7822 LISTVIEW_ShiftIndices(infoPtr, nItem, -1);
7823 LISTVIEW_ShiftFocus(infoPtr, infoPtr->nFocusedItem, nItem, -1);
7824 DPA_DeletePtr(infoPtr->hdpaItems, nItem);
7825 infoPtr->nItemCount--;
7827 DPA_DeletePtr(hdpaSubItems, 0);
7828 DPA_Destroy (hdpaSubItems);
7835 * Checks item visibility.
7838 * [I] infoPtr : valid pointer to the listview structure
7839 * [I] nFirst : item index to check for
7842 * Item visible : TRUE
7843 * Item invisible or failure : FALSE
7845 static BOOL LISTVIEW_IsItemVisible(const LISTVIEW_INFO *infoPtr, INT nItem)
7847 POINT Origin, Position;
7852 TRACE("nItem=%d\n", nItem);
7854 if (nItem < 0 || nItem >= DPA_GetPtrCount(infoPtr->hdpaItems)) return FALSE;
7856 LISTVIEW_GetOrigin(infoPtr, &Origin);
7857 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
7858 rcItem.left = Position.x + Origin.x;
7859 rcItem.top = Position.y + Origin.y;
7860 rcItem.right = rcItem.left + infoPtr->nItemWidth;
7861 rcItem.bottom = rcItem.top + infoPtr->nItemHeight;
7863 hdc = GetDC(infoPtr->hwndSelf);
7864 if (!hdc) return FALSE;
7865 ret = RectVisible(hdc, &rcItem);
7866 ReleaseDC(infoPtr->hwndSelf, hdc);
7873 * Redraws a range of items.
7876 * [I] infoPtr : valid pointer to the listview structure
7877 * [I] nFirst : first item
7878 * [I] nLast : last item
7884 static BOOL LISTVIEW_RedrawItems(const LISTVIEW_INFO *infoPtr, INT nFirst, INT nLast)
7888 if (nLast < nFirst || min(nFirst, nLast) < 0 ||
7889 max(nFirst, nLast) >= infoPtr->nItemCount)
7892 for (i = nFirst; i <= nLast; i++)
7893 LISTVIEW_InvalidateItem(infoPtr, i);
7900 * Scroll the content of a listview.
7903 * [I] infoPtr : valid pointer to the listview structure
7904 * [I] dx : horizontal scroll amount in pixels
7905 * [I] dy : vertical scroll amount in pixels
7912 * If the control is in report view (LV_VIEW_DETAILS) the control can
7913 * be scrolled only in line increments. "dy" will be rounded to the
7914 * nearest number of pixels that are a whole line. Ex: if line height
7915 * is 16 and an 8 is passed, the list will be scrolled by 16. If a 7
7916 * is passed, then the scroll will be 0. (per MSDN 7/2002)
7918 * For: (per experimentation with native control and CSpy ListView)
7919 * LV_VIEW_ICON scrolling in any direction is allowed
7920 * LV_VIEW_SMALLICON scrolling in any direction is allowed
7921 * LV_VIEW_LIST dx=1 = 1 column (horizontal only)
7922 * but will only scroll 1 column per message
7923 * no matter what the value.
7924 * dy must be 0 or FALSE returned.
7925 * LV_VIEW_DETAILS dx=1 = 1 pixel
7929 static BOOL LISTVIEW_Scroll(LISTVIEW_INFO *infoPtr, INT dx, INT dy)
7931 switch(infoPtr->uView) {
7932 case LV_VIEW_DETAILS:
7933 dy += (dy < 0 ? -1 : 1) * infoPtr->nItemHeight/2;
7934 dy /= infoPtr->nItemHeight;
7937 if (dy != 0) return FALSE;
7943 if (dx != 0) LISTVIEW_HScroll(infoPtr, SB_INTERNAL, dx);
7944 if (dy != 0) LISTVIEW_VScroll(infoPtr, SB_INTERNAL, dy);
7951 * Sets the background color.
7954 * [I] infoPtr : valid pointer to the listview structure
7955 * [I] color : background color
7961 static BOOL LISTVIEW_SetBkColor(LISTVIEW_INFO *infoPtr, COLORREF color)
7963 TRACE("(color=%x)\n", color);
7965 if(infoPtr->clrBk != color) {
7966 if (infoPtr->clrBk != CLR_NONE) DeleteObject(infoPtr->hBkBrush);
7967 infoPtr->clrBk = color;
7968 if (color == CLR_NONE)
7969 infoPtr->hBkBrush = (HBRUSH)GetClassLongPtrW(infoPtr->hwndSelf, GCLP_HBRBACKGROUND);
7972 infoPtr->hBkBrush = CreateSolidBrush(color);
7973 infoPtr->dwLvExStyle &= ~LVS_EX_TRANSPARENTBKGND;
7980 /* LISTVIEW_SetBkImage */
7982 /*** Helper for {Insert,Set}ColumnT *only* */
7983 static void column_fill_hditem(const LISTVIEW_INFO *infoPtr, HDITEMW *lphdi, INT nColumn,
7984 const LVCOLUMNW *lpColumn, BOOL isW)
7986 if (lpColumn->mask & LVCF_FMT)
7988 /* format member is valid */
7989 lphdi->mask |= HDI_FORMAT;
7991 /* set text alignment (leftmost column must be left-aligned) */
7992 if (nColumn == 0 || (lpColumn->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_LEFT)
7993 lphdi->fmt |= HDF_LEFT;
7994 else if ((lpColumn->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_RIGHT)
7995 lphdi->fmt |= HDF_RIGHT;
7996 else if ((lpColumn->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_CENTER)
7997 lphdi->fmt |= HDF_CENTER;
7999 if (lpColumn->fmt & LVCFMT_BITMAP_ON_RIGHT)
8000 lphdi->fmt |= HDF_BITMAP_ON_RIGHT;
8002 if (lpColumn->fmt & LVCFMT_COL_HAS_IMAGES)
8004 lphdi->fmt |= HDF_IMAGE;
8005 lphdi->iImage = I_IMAGECALLBACK;
8008 if (lpColumn->fmt & LVCFMT_FIXED_WIDTH)
8009 lphdi->fmt |= HDF_FIXEDWIDTH;
8012 if (lpColumn->mask & LVCF_WIDTH)
8014 lphdi->mask |= HDI_WIDTH;
8015 if(lpColumn->cx == LVSCW_AUTOSIZE_USEHEADER)
8017 /* make it fill the remainder of the controls width */
8021 for(item_index = 0; item_index < (nColumn - 1); item_index++)
8023 LISTVIEW_GetHeaderRect(infoPtr, item_index, &rcHeader);
8024 lphdi->cxy += rcHeader.right - rcHeader.left;
8027 /* retrieve the layout of the header */
8028 GetClientRect(infoPtr->hwndSelf, &rcHeader);
8029 TRACE("start cxy=%d rcHeader=%s\n", lphdi->cxy, wine_dbgstr_rect(&rcHeader));
8031 lphdi->cxy = (rcHeader.right - rcHeader.left) - lphdi->cxy;
8034 lphdi->cxy = lpColumn->cx;
8037 if (lpColumn->mask & LVCF_TEXT)
8039 lphdi->mask |= HDI_TEXT | HDI_FORMAT;
8040 lphdi->fmt |= HDF_STRING;
8041 lphdi->pszText = lpColumn->pszText;
8042 lphdi->cchTextMax = textlenT(lpColumn->pszText, isW);
8045 if (lpColumn->mask & LVCF_IMAGE)
8047 lphdi->mask |= HDI_IMAGE;
8048 lphdi->iImage = lpColumn->iImage;
8051 if (lpColumn->mask & LVCF_ORDER)
8053 lphdi->mask |= HDI_ORDER;
8054 lphdi->iOrder = lpColumn->iOrder;
8061 * Inserts a new column.
8064 * [I] infoPtr : valid pointer to the listview structure
8065 * [I] nColumn : column index
8066 * [I] lpColumn : column information
8067 * [I] isW : TRUE if lpColumn is Unicode, FALSE otherwise
8070 * SUCCESS : new column index
8073 static INT LISTVIEW_InsertColumnT(LISTVIEW_INFO *infoPtr, INT nColumn,
8074 const LVCOLUMNW *lpColumn, BOOL isW)
8076 COLUMN_INFO *lpColumnInfo;
8080 TRACE("(nColumn=%d, lpColumn=%s, isW=%d)\n", nColumn, debuglvcolumn_t(lpColumn, isW), isW);
8082 if (!lpColumn || nColumn < 0) return -1;
8083 nColumn = min(nColumn, DPA_GetPtrCount(infoPtr->hdpaColumns));
8085 ZeroMemory(&hdi, sizeof(HDITEMW));
8086 column_fill_hditem(infoPtr, &hdi, nColumn, lpColumn, isW);
8089 * A mask not including LVCF_WIDTH turns into a mask of width, width 10
8090 * (can be seen in SPY) otherwise column never gets added.
8092 if (!(lpColumn->mask & LVCF_WIDTH)) {
8093 hdi.mask |= HDI_WIDTH;
8098 * when the iSubItem is available Windows copies it to the header lParam. It seems
8099 * to happen only in LVM_INSERTCOLUMN - not in LVM_SETCOLUMN
8101 if (lpColumn->mask & LVCF_SUBITEM)
8103 hdi.mask |= HDI_LPARAM;
8104 hdi.lParam = lpColumn->iSubItem;
8107 /* create header if not present */
8108 LISTVIEW_CreateHeader(infoPtr);
8109 if (!(LVS_NOCOLUMNHEADER & infoPtr->dwStyle) &&
8110 (infoPtr->uView == LV_VIEW_DETAILS) && (WS_VISIBLE & infoPtr->dwStyle))
8112 ShowWindow(infoPtr->hwndHeader, SW_SHOWNORMAL);
8115 /* insert item in header control */
8116 nNewColumn = SendMessageW(infoPtr->hwndHeader,
8117 isW ? HDM_INSERTITEMW : HDM_INSERTITEMA,
8118 nColumn, (LPARAM)&hdi);
8119 if (nNewColumn == -1) return -1;
8120 if (nNewColumn != nColumn) ERR("nColumn=%d, nNewColumn=%d\n", nColumn, nNewColumn);
8122 /* create our own column info */
8123 if (!(lpColumnInfo = Alloc(sizeof(COLUMN_INFO)))) goto fail;
8124 if (DPA_InsertPtr(infoPtr->hdpaColumns, nNewColumn, lpColumnInfo) == -1) goto fail;
8126 if (lpColumn->mask & LVCF_FMT) lpColumnInfo->fmt = lpColumn->fmt;
8127 if (lpColumn->mask & LVCF_MINWIDTH) lpColumnInfo->cxMin = lpColumn->cxMin;
8128 if (!SendMessageW(infoPtr->hwndHeader, HDM_GETITEMRECT, nNewColumn, (LPARAM)&lpColumnInfo->rcHeader))
8131 /* now we have to actually adjust the data */
8132 if (!(infoPtr->dwStyle & LVS_OWNERDATA) && infoPtr->nItemCount > 0)
8134 SUBITEM_INFO *lpSubItem;
8140 item.iSubItem = nNewColumn;
8141 item.mask = LVIF_TEXT | LVIF_IMAGE;
8142 item.iImage = I_IMAGECALLBACK;
8143 item.pszText = LPSTR_TEXTCALLBACKW;
8145 for (nItem = 0; nItem < infoPtr->nItemCount; nItem++)
8147 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, nItem);
8148 for (i = 1; i < DPA_GetPtrCount(hdpaSubItems); i++)
8150 lpSubItem = DPA_GetPtr(hdpaSubItems, i);
8151 if (lpSubItem->iSubItem >= nNewColumn)
8152 lpSubItem->iSubItem++;
8155 /* add new subitem for each item */
8157 set_sub_item(infoPtr, &item, isW, &changed);
8161 /* make space for the new column */
8162 LISTVIEW_ScrollColumns(infoPtr, nNewColumn + 1, lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left);
8163 LISTVIEW_UpdateItemSize(infoPtr);
8168 if (nNewColumn != -1) SendMessageW(infoPtr->hwndHeader, HDM_DELETEITEM, nNewColumn, 0);
8171 DPA_DeletePtr(infoPtr->hdpaColumns, nNewColumn);
8179 * Sets the attributes of a header item.
8182 * [I] infoPtr : valid pointer to the listview structure
8183 * [I] nColumn : column index
8184 * [I] lpColumn : column attributes
8185 * [I] isW: if TRUE, then lpColumn is a LPLVCOLUMNW, else it is a LPLVCOLUMNA
8191 static BOOL LISTVIEW_SetColumnT(const LISTVIEW_INFO *infoPtr, INT nColumn,
8192 const LVCOLUMNW *lpColumn, BOOL isW)
8194 HDITEMW hdi, hdiget;
8197 TRACE("(nColumn=%d, lpColumn=%s, isW=%d)\n", nColumn, debuglvcolumn_t(lpColumn, isW), isW);
8199 if (!lpColumn || nColumn < 0 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
8201 ZeroMemory(&hdi, sizeof(HDITEMW));
8202 if (lpColumn->mask & LVCF_FMT)
8204 hdi.mask |= HDI_FORMAT;
8205 hdiget.mask = HDI_FORMAT;
8206 if (SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nColumn, (LPARAM)&hdiget))
8207 hdi.fmt = hdiget.fmt & HDF_STRING;
8209 column_fill_hditem(infoPtr, &hdi, nColumn, lpColumn, isW);
8211 /* set header item attributes */
8212 bResult = SendMessageW(infoPtr->hwndHeader, isW ? HDM_SETITEMW : HDM_SETITEMA, nColumn, (LPARAM)&hdi);
8213 if (!bResult) return FALSE;
8215 if (lpColumn->mask & LVCF_FMT)
8217 COLUMN_INFO *lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, nColumn);
8218 INT oldFmt = lpColumnInfo->fmt;
8220 lpColumnInfo->fmt = lpColumn->fmt;
8221 if ((oldFmt ^ lpColumn->fmt) & (LVCFMT_JUSTIFYMASK | LVCFMT_IMAGE))
8223 if (infoPtr->uView == LV_VIEW_DETAILS) LISTVIEW_InvalidateColumn(infoPtr, nColumn);
8227 if (lpColumn->mask & LVCF_MINWIDTH)
8228 LISTVIEW_GetColumnInfo(infoPtr, nColumn)->cxMin = lpColumn->cxMin;
8235 * Sets the column order array
8238 * [I] infoPtr : valid pointer to the listview structure
8239 * [I] iCount : number of elements in column order array
8240 * [I] lpiArray : pointer to column order array
8246 static BOOL LISTVIEW_SetColumnOrderArray(LISTVIEW_INFO *infoPtr, INT iCount, const INT *lpiArray)
8248 TRACE("iCount %d lpiArray %p\n", iCount, lpiArray);
8250 if (!lpiArray || !IsWindow(infoPtr->hwndHeader)) return FALSE;
8252 infoPtr->colRectsDirty = TRUE;
8254 return SendMessageW(infoPtr->hwndHeader, HDM_SETORDERARRAY, iCount, (LPARAM)lpiArray);
8259 * Sets the width of a column
8262 * [I] infoPtr : valid pointer to the listview structure
8263 * [I] nColumn : column index
8264 * [I] cx : column width
8270 static BOOL LISTVIEW_SetColumnWidth(LISTVIEW_INFO *infoPtr, INT nColumn, INT cx)
8272 WCHAR szDispText[DISP_TEXT_SIZE] = { 0 };
8276 TRACE("(nColumn=%d, cx=%d\n", nColumn, cx);
8278 /* set column width only if in report or list mode */
8279 if (infoPtr->uView != LV_VIEW_DETAILS && infoPtr->uView != LV_VIEW_LIST) return FALSE;
8281 /* take care of invalid cx values */
8282 if(infoPtr->uView == LV_VIEW_DETAILS && cx < -2) cx = LVSCW_AUTOSIZE;
8283 else if (infoPtr->uView == LV_VIEW_LIST && cx < 1) return FALSE;
8285 /* resize all columns if in LV_VIEW_LIST mode */
8286 if(infoPtr->uView == LV_VIEW_LIST)
8288 infoPtr->nItemWidth = cx;
8289 LISTVIEW_InvalidateList(infoPtr);
8293 if (nColumn < 0 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
8295 if (cx == LVSCW_AUTOSIZE || (cx == LVSCW_AUTOSIZE_USEHEADER && nColumn < DPA_GetPtrCount(infoPtr->hdpaColumns) -1))
8300 lvItem.mask = LVIF_TEXT;
8302 lvItem.iSubItem = nColumn;
8303 lvItem.pszText = szDispText;
8304 lvItem.cchTextMax = DISP_TEXT_SIZE;
8305 for (; lvItem.iItem < infoPtr->nItemCount; lvItem.iItem++)
8307 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) continue;
8308 nLabelWidth = LISTVIEW_GetStringWidthT(infoPtr, lvItem.pszText, TRUE);
8309 if (max_cx < nLabelWidth) max_cx = nLabelWidth;
8311 if (infoPtr->himlSmall && (nColumn == 0 || (LISTVIEW_GetColumnInfo(infoPtr, nColumn)->fmt & LVCFMT_IMAGE)))
8312 max_cx += infoPtr->iconSize.cx;
8313 max_cx += TRAILING_LABEL_PADDING;
8316 /* autosize based on listview items width */
8317 if(cx == LVSCW_AUTOSIZE)
8319 else if(cx == LVSCW_AUTOSIZE_USEHEADER)
8321 /* if iCol is the last column make it fill the remainder of the controls width */
8322 if(nColumn == DPA_GetPtrCount(infoPtr->hdpaColumns) - 1)
8327 LISTVIEW_GetOrigin(infoPtr, &Origin);
8328 LISTVIEW_GetHeaderRect(infoPtr, nColumn, &rcHeader);
8330 cx = infoPtr->rcList.right - Origin.x - rcHeader.left;
8334 /* Despite what the MS docs say, if this is not the last
8335 column, then MS resizes the column to the width of the
8336 largest text string in the column, including headers
8337 and items. This is different from LVSCW_AUTOSIZE in that
8338 LVSCW_AUTOSIZE ignores the header string length. */
8341 /* retrieve header text */
8342 hdi.mask = HDI_TEXT;
8343 hdi.cchTextMax = DISP_TEXT_SIZE;
8344 hdi.pszText = szDispText;
8345 if (SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nColumn, (LPARAM)&hdi))
8347 HDC hdc = GetDC(infoPtr->hwndSelf);
8348 HFONT old_font = SelectObject(hdc, (HFONT)SendMessageW(infoPtr->hwndHeader, WM_GETFONT, 0, 0));
8351 if (GetTextExtentPoint32W(hdc, hdi.pszText, lstrlenW(hdi.pszText), &size))
8352 cx = size.cx + TRAILING_HEADER_PADDING;
8353 /* FIXME: Take into account the header image, if one is present */
8354 SelectObject(hdc, old_font);
8355 ReleaseDC(infoPtr->hwndSelf, hdc);
8357 cx = max (cx, max_cx);
8361 if (cx < 0) return FALSE;
8363 /* call header to update the column change */
8364 hdi.mask = HDI_WIDTH;
8365 hdi.cxy = max(cx, LISTVIEW_GetColumnInfo(infoPtr, nColumn)->cxMin);
8366 TRACE("hdi.cxy=%d\n", hdi.cxy);
8367 return SendMessageW(infoPtr->hwndHeader, HDM_SETITEMW, nColumn, (LPARAM)&hdi);
8371 * Creates the checkbox imagelist. Helper for LISTVIEW_SetExtendedListViewStyle
8374 static HIMAGELIST LISTVIEW_CreateCheckBoxIL(const LISTVIEW_INFO *infoPtr)
8377 HBITMAP hbm_im, hbm_mask, hbm_orig;
8379 HBRUSH hbr_white = GetStockObject(WHITE_BRUSH);
8380 HBRUSH hbr_black = GetStockObject(BLACK_BRUSH);
8383 himl = ImageList_Create(GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON),
8384 ILC_COLOR | ILC_MASK, 2, 2);
8385 hdc_wnd = GetDC(infoPtr->hwndSelf);
8386 hdc = CreateCompatibleDC(hdc_wnd);
8387 hbm_im = CreateCompatibleBitmap(hdc_wnd, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON));
8388 hbm_mask = CreateBitmap(GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), 1, 1, NULL);
8389 ReleaseDC(infoPtr->hwndSelf, hdc_wnd);
8391 rc.left = rc.top = 0;
8392 rc.right = GetSystemMetrics(SM_CXSMICON);
8393 rc.bottom = GetSystemMetrics(SM_CYSMICON);
8395 hbm_orig = SelectObject(hdc, hbm_mask);
8396 FillRect(hdc, &rc, hbr_white);
8397 InflateRect(&rc, -2, -2);
8398 FillRect(hdc, &rc, hbr_black);
8400 SelectObject(hdc, hbm_im);
8401 DrawFrameControl(hdc, &rc, DFC_BUTTON, DFCS_BUTTONCHECK | DFCS_MONO);
8402 SelectObject(hdc, hbm_orig);
8403 ImageList_Add(himl, hbm_im, hbm_mask);
8405 SelectObject(hdc, hbm_im);
8406 DrawFrameControl(hdc, &rc, DFC_BUTTON, DFCS_BUTTONCHECK | DFCS_MONO | DFCS_CHECKED);
8407 SelectObject(hdc, hbm_orig);
8408 ImageList_Add(himl, hbm_im, hbm_mask);
8410 DeleteObject(hbm_mask);
8411 DeleteObject(hbm_im);
8419 * Sets the extended listview style.
8422 * [I] infoPtr : valid pointer to the listview structure
8424 * [I] dwStyle : style
8427 * SUCCESS : previous style
8430 static DWORD LISTVIEW_SetExtendedListViewStyle(LISTVIEW_INFO *infoPtr, DWORD mask, DWORD ex_style)
8432 DWORD old_ex_style = infoPtr->dwLvExStyle;
8434 TRACE("mask=0x%08x, ex_style=0x%08x\n", mask, ex_style);
8438 infoPtr->dwLvExStyle = (old_ex_style & ~mask) | (ex_style & mask);
8440 infoPtr->dwLvExStyle = ex_style;
8442 if((infoPtr->dwLvExStyle ^ old_ex_style) & LVS_EX_CHECKBOXES)
8444 HIMAGELIST himl = 0;
8445 if(infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES)
8448 item.mask = LVIF_STATE;
8449 item.stateMask = LVIS_STATEIMAGEMASK;
8450 item.state = INDEXTOSTATEIMAGEMASK(1);
8451 LISTVIEW_SetItemState(infoPtr, -1, &item);
8453 himl = LISTVIEW_CreateCheckBoxIL(infoPtr);
8454 if(!(infoPtr->dwStyle & LVS_SHAREIMAGELISTS))
8455 ImageList_Destroy(infoPtr->himlState);
8457 himl = LISTVIEW_SetImageList(infoPtr, LVSIL_STATE, himl);
8458 /* checkbox list replaces previous custom list or... */
8459 if(((infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES) &&
8460 !(infoPtr->dwStyle & LVS_SHAREIMAGELISTS)) ||
8461 /* ...previous was checkbox list */
8462 (old_ex_style & LVS_EX_CHECKBOXES))
8463 ImageList_Destroy(himl);
8466 if((infoPtr->dwLvExStyle ^ old_ex_style) & LVS_EX_HEADERDRAGDROP)
8470 /* if not already created */
8471 LISTVIEW_CreateHeader(infoPtr);
8473 style = GetWindowLongW(infoPtr->hwndHeader, GWL_STYLE);
8474 if (infoPtr->dwLvExStyle & LVS_EX_HEADERDRAGDROP)
8475 style |= HDS_DRAGDROP;
8477 style &= ~HDS_DRAGDROP;
8478 SetWindowLongW(infoPtr->hwndHeader, GWL_STYLE, style);
8481 /* GRIDLINES adds decoration at top so changes sizes */
8482 if((infoPtr->dwLvExStyle ^ old_ex_style) & LVS_EX_GRIDLINES)
8484 LISTVIEW_CreateHeader(infoPtr);
8485 LISTVIEW_UpdateSize(infoPtr);
8488 if((infoPtr->dwLvExStyle ^ old_ex_style) & LVS_EX_FULLROWSELECT)
8490 LISTVIEW_CreateHeader(infoPtr);
8493 if((infoPtr->dwLvExStyle ^ old_ex_style) & LVS_EX_TRANSPARENTBKGND)
8495 if (infoPtr->dwLvExStyle & LVS_EX_TRANSPARENTBKGND)
8496 LISTVIEW_SetBkColor(infoPtr, CLR_NONE);
8499 if((infoPtr->dwLvExStyle ^ old_ex_style) & LVS_EX_HEADERINALLVIEWS)
8501 if (infoPtr->dwLvExStyle & LVS_EX_HEADERINALLVIEWS)
8502 LISTVIEW_CreateHeader(infoPtr);
8504 ShowWindow(infoPtr->hwndHeader, SW_HIDE);
8505 LISTVIEW_UpdateSize(infoPtr);
8506 LISTVIEW_UpdateScroll(infoPtr);
8509 LISTVIEW_InvalidateList(infoPtr);
8510 return old_ex_style;
8515 * Sets the new hot cursor used during hot tracking and hover selection.
8518 * [I] infoPtr : valid pointer to the listview structure
8519 * [I] hCursor : the new hot cursor handle
8522 * Returns the previous hot cursor
8524 static HCURSOR LISTVIEW_SetHotCursor(LISTVIEW_INFO *infoPtr, HCURSOR hCursor)
8526 HCURSOR oldCursor = infoPtr->hHotCursor;
8528 infoPtr->hHotCursor = hCursor;
8536 * Sets the hot item index.
8539 * [I] infoPtr : valid pointer to the listview structure
8540 * [I] iIndex : index
8543 * SUCCESS : previous hot item index
8544 * FAILURE : -1 (no hot item)
8546 static INT LISTVIEW_SetHotItem(LISTVIEW_INFO *infoPtr, INT iIndex)
8548 INT iOldIndex = infoPtr->nHotItem;
8550 infoPtr->nHotItem = iIndex;
8558 * Sets the amount of time the cursor must hover over an item before it is selected.
8561 * [I] infoPtr : valid pointer to the listview structure
8562 * [I] dwHoverTime : hover time, if -1 the hover time is set to the default
8565 * Returns the previous hover time
8567 static DWORD LISTVIEW_SetHoverTime(LISTVIEW_INFO *infoPtr, DWORD dwHoverTime)
8569 DWORD oldHoverTime = infoPtr->dwHoverTime;
8571 infoPtr->dwHoverTime = dwHoverTime;
8573 return oldHoverTime;
8578 * Sets spacing for icons of LVS_ICON style.
8581 * [I] infoPtr : valid pointer to the listview structure
8582 * [I] cx : horizontal spacing (-1 = system spacing, 0 = autosize)
8583 * [I] cy : vertical spacing (-1 = system spacing, 0 = autosize)
8586 * MAKELONG(oldcx, oldcy)
8588 static DWORD LISTVIEW_SetIconSpacing(LISTVIEW_INFO *infoPtr, INT cx, INT cy)
8590 INT iconWidth = 0, iconHeight = 0;
8591 DWORD oldspacing = MAKELONG(infoPtr->iconSpacing.cx, infoPtr->iconSpacing.cy);
8593 TRACE("requested=(%d,%d)\n", cx, cy);
8595 /* set to defaults, if instructed to */
8596 if (cx == -1 && cy == -1)
8598 infoPtr->autoSpacing = TRUE;
8599 if (infoPtr->himlNormal)
8600 ImageList_GetIconSize(infoPtr->himlNormal, &iconWidth, &iconHeight);
8601 cx = GetSystemMetrics(SM_CXICONSPACING) - GetSystemMetrics(SM_CXICON) + iconWidth;
8602 cy = GetSystemMetrics(SM_CYICONSPACING) - GetSystemMetrics(SM_CYICON) + iconHeight;
8605 infoPtr->autoSpacing = FALSE;
8607 /* if 0 then keep width */
8609 infoPtr->iconSpacing.cx = cx;
8611 /* if 0 then keep height */
8613 infoPtr->iconSpacing.cy = cy;
8615 TRACE("old=(%d,%d), new=(%d,%d), iconSize=(%d,%d), ntmH=%d\n",
8616 LOWORD(oldspacing), HIWORD(oldspacing), infoPtr->iconSpacing.cx, infoPtr->iconSpacing.cy,
8617 infoPtr->iconSize.cx, infoPtr->iconSize.cy,
8618 infoPtr->ntmHeight);
8620 /* these depend on the iconSpacing */
8621 LISTVIEW_UpdateItemSize(infoPtr);
8626 static inline void set_icon_size(SIZE *size, HIMAGELIST himl, BOOL small)
8630 if (himl && ImageList_GetIconSize(himl, &cx, &cy))
8637 size->cx = GetSystemMetrics(small ? SM_CXSMICON : SM_CXICON);
8638 size->cy = GetSystemMetrics(small ? SM_CYSMICON : SM_CYICON);
8647 * [I] infoPtr : valid pointer to the listview structure
8648 * [I] nType : image list type
8649 * [I] himl : image list handle
8652 * SUCCESS : old image list
8655 static HIMAGELIST LISTVIEW_SetImageList(LISTVIEW_INFO *infoPtr, INT nType, HIMAGELIST himl)
8657 INT oldHeight = infoPtr->nItemHeight;
8658 HIMAGELIST himlOld = 0;
8660 TRACE("(nType=%d, himl=%p\n", nType, himl);
8665 himlOld = infoPtr->himlNormal;
8666 infoPtr->himlNormal = himl;
8667 if (infoPtr->uView == LV_VIEW_ICON) set_icon_size(&infoPtr->iconSize, himl, FALSE);
8668 if (infoPtr->autoSpacing)
8669 LISTVIEW_SetIconSpacing(infoPtr, -1, -1);
8673 himlOld = infoPtr->himlSmall;
8674 infoPtr->himlSmall = himl;
8675 if (infoPtr->uView != LV_VIEW_ICON) set_icon_size(&infoPtr->iconSize, himl, TRUE);
8676 if (infoPtr->hwndHeader)
8677 SendMessageW(infoPtr->hwndHeader, HDM_SETIMAGELIST, 0, (LPARAM)himl);
8681 himlOld = infoPtr->himlState;
8682 infoPtr->himlState = himl;
8683 set_icon_size(&infoPtr->iconStateSize, himl, TRUE);
8684 ImageList_SetBkColor(infoPtr->himlState, CLR_NONE);
8688 ERR("Unknown icon type=%d\n", nType);
8692 infoPtr->nItemHeight = LISTVIEW_CalculateItemHeight(infoPtr);
8693 if (infoPtr->nItemHeight != oldHeight)
8694 LISTVIEW_UpdateScroll(infoPtr);
8701 * Preallocates memory (does *not* set the actual count of items !)
8704 * [I] infoPtr : valid pointer to the listview structure
8705 * [I] nItems : item count (projected number of items to allocate)
8706 * [I] dwFlags : update flags
8712 static BOOL LISTVIEW_SetItemCount(LISTVIEW_INFO *infoPtr, INT nItems, DWORD dwFlags)
8714 TRACE("(nItems=%d, dwFlags=%x)\n", nItems, dwFlags);
8716 if (infoPtr->dwStyle & LVS_OWNERDATA)
8718 INT nOldCount = infoPtr->nItemCount;
8720 if (nItems < nOldCount)
8722 RANGE range = { nItems, nOldCount };
8723 ranges_del(infoPtr->selectionRanges, range);
8724 if (infoPtr->nFocusedItem >= nItems)
8726 LISTVIEW_SetItemFocus(infoPtr, -1);
8727 SetRectEmpty(&infoPtr->rcFocus);
8731 infoPtr->nItemCount = nItems;
8732 LISTVIEW_UpdateScroll(infoPtr);
8734 /* the flags are valid only in ownerdata report and list modes */
8735 if (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON) dwFlags = 0;
8737 if (!(dwFlags & LVSICF_NOSCROLL) && infoPtr->nFocusedItem != -1)
8738 LISTVIEW_EnsureVisible(infoPtr, infoPtr->nFocusedItem, FALSE);
8740 if (!(dwFlags & LVSICF_NOINVALIDATEALL))
8741 LISTVIEW_InvalidateList(infoPtr);
8748 LISTVIEW_GetOrigin(infoPtr, &Origin);
8749 nFrom = min(nOldCount, nItems);
8750 nTo = max(nOldCount, nItems);
8752 if (infoPtr->uView == LV_VIEW_DETAILS)
8755 rcErase.top = nFrom * infoPtr->nItemHeight;
8756 rcErase.right = infoPtr->nItemWidth;
8757 rcErase.bottom = nTo * infoPtr->nItemHeight;
8758 OffsetRect(&rcErase, Origin.x, Origin.y);
8759 if (IntersectRect(&rcErase, &rcErase, &infoPtr->rcList))
8760 LISTVIEW_InvalidateRect(infoPtr, &rcErase);
8762 else /* LV_VIEW_LIST */
8764 INT nPerCol = LISTVIEW_GetCountPerColumn(infoPtr);
8766 rcErase.left = (nFrom / nPerCol) * infoPtr->nItemWidth;
8767 rcErase.top = (nFrom % nPerCol) * infoPtr->nItemHeight;
8768 rcErase.right = rcErase.left + infoPtr->nItemWidth;
8769 rcErase.bottom = nPerCol * infoPtr->nItemHeight;
8770 OffsetRect(&rcErase, Origin.x, Origin.y);
8771 if (IntersectRect(&rcErase, &rcErase, &infoPtr->rcList))
8772 LISTVIEW_InvalidateRect(infoPtr, &rcErase);
8774 rcErase.left = (nFrom / nPerCol + 1) * infoPtr->nItemWidth;
8776 rcErase.right = (nTo / nPerCol + 1) * infoPtr->nItemWidth;
8777 rcErase.bottom = nPerCol * infoPtr->nItemHeight;
8778 OffsetRect(&rcErase, Origin.x, Origin.y);
8779 if (IntersectRect(&rcErase, &rcErase, &infoPtr->rcList))
8780 LISTVIEW_InvalidateRect(infoPtr, &rcErase);
8786 /* According to MSDN for non-LVS_OWNERDATA this is just
8787 * a performance issue. The control allocates its internal
8788 * data structures for the number of items specified. It
8789 * cuts down on the number of memory allocations. Therefore
8790 * we will just issue a WARN here
8792 WARN("for non-ownerdata performance option not implemented.\n");
8800 * Sets the position of an item.
8803 * [I] infoPtr : valid pointer to the listview structure
8804 * [I] nItem : item index
8805 * [I] pt : coordinate
8811 static BOOL LISTVIEW_SetItemPosition(LISTVIEW_INFO *infoPtr, INT nItem, const POINT *pt)
8815 TRACE("(nItem=%d, pt=%s\n", nItem, wine_dbgstr_point(pt));
8817 if (!pt || nItem < 0 || nItem >= infoPtr->nItemCount ||
8818 !(infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON)) return FALSE;
8821 LISTVIEW_GetOrigin(infoPtr, &Origin);
8823 /* This point value seems to be an undocumented feature.
8824 * The best guess is that it means either at the origin,
8825 * or at true beginning of the list. I will assume the origin. */
8826 if ((Pt.x == -1) && (Pt.y == -1))
8829 if (infoPtr->uView == LV_VIEW_ICON)
8831 Pt.x -= (infoPtr->nItemWidth - infoPtr->iconSize.cx) / 2;
8832 Pt.y -= ICON_TOP_PADDING;
8837 infoPtr->bAutoarrange = FALSE;
8839 return LISTVIEW_MoveIconTo(infoPtr, nItem, &Pt, FALSE);
8844 * Sets the state of one or many items.
8847 * [I] infoPtr : valid pointer to the listview structure
8848 * [I] nItem : item index
8849 * [I] item : item or subitem info
8855 static BOOL LISTVIEW_SetItemState(LISTVIEW_INFO *infoPtr, INT nItem, const LVITEMW *item)
8860 if (!item) return FALSE;
8862 lvItem.iItem = nItem;
8863 lvItem.iSubItem = 0;
8864 lvItem.mask = LVIF_STATE;
8865 lvItem.state = item->state;
8866 lvItem.stateMask = item->stateMask;
8867 TRACE("item=%s\n", debuglvitem_t(&lvItem, TRUE));
8874 /* special case optimization for recurring attempt to deselect all */
8875 if (lvItem.state == 0 && lvItem.stateMask == LVIS_SELECTED && !LISTVIEW_GetSelectedCount(infoPtr))
8878 /* select all isn't allowed in LVS_SINGLESEL */
8879 if ((lvItem.state & lvItem.stateMask & LVIS_SELECTED) && (infoPtr->dwStyle & LVS_SINGLESEL))
8882 /* focus all isn't allowed */
8883 if (lvItem.state & lvItem.stateMask & LVIS_FOCUSED) return FALSE;
8885 notify = infoPtr->bDoChangeNotify;
8886 if (infoPtr->dwStyle & LVS_OWNERDATA)
8888 infoPtr->bDoChangeNotify = FALSE;
8889 if (!(lvItem.state & LVIS_SELECTED) && LISTVIEW_GetSelectedCount(infoPtr))
8890 oldstate |= LVIS_SELECTED;
8891 if (infoPtr->nFocusedItem != -1) oldstate |= LVIS_FOCUSED;
8894 /* apply to all items */
8895 for (lvItem.iItem = 0; lvItem.iItem < infoPtr->nItemCount; lvItem.iItem++)
8896 if (!LISTVIEW_SetItemT(infoPtr, &lvItem, TRUE)) ret = FALSE;
8898 if (infoPtr->dwStyle & LVS_OWNERDATA)
8902 infoPtr->bDoChangeNotify = notify;
8906 nmlv.uNewState = lvItem.state & lvItem.stateMask;
8907 nmlv.uOldState = oldstate & lvItem.stateMask;
8908 nmlv.uChanged = LVIF_STATE;
8909 nmlv.ptAction.x = nmlv.ptAction.y = 0;
8912 notify_listview(infoPtr, LVN_ITEMCHANGED, &nmlv);
8916 ret = LISTVIEW_SetItemT(infoPtr, &lvItem, TRUE);
8923 * Sets the text of an item or subitem.
8926 * [I] hwnd : window handle
8927 * [I] nItem : item index
8928 * [I] lpLVItem : item or subitem info
8929 * [I] isW : TRUE if input is Unicode
8935 static BOOL LISTVIEW_SetItemTextT(LISTVIEW_INFO *infoPtr, INT nItem, const LVITEMW *lpLVItem, BOOL isW)
8939 if (!lpLVItem || nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
8940 if (infoPtr->dwStyle & LVS_OWNERDATA) return FALSE;
8942 lvItem.iItem = nItem;
8943 lvItem.iSubItem = lpLVItem->iSubItem;
8944 lvItem.mask = LVIF_TEXT;
8945 lvItem.pszText = lpLVItem->pszText;
8946 lvItem.cchTextMax = lpLVItem->cchTextMax;
8948 TRACE("(nItem=%d, lpLVItem=%s, isW=%d)\n", nItem, debuglvitem_t(&lvItem, isW), isW);
8950 return LISTVIEW_SetItemT(infoPtr, &lvItem, isW);
8955 * Set item index that marks the start of a multiple selection.
8958 * [I] infoPtr : valid pointer to the listview structure
8959 * [I] nIndex : index
8962 * Index number or -1 if there is no selection mark.
8964 static INT LISTVIEW_SetSelectionMark(LISTVIEW_INFO *infoPtr, INT nIndex)
8966 INT nOldIndex = infoPtr->nSelectionMark;
8968 TRACE("(nIndex=%d)\n", nIndex);
8970 infoPtr->nSelectionMark = nIndex;
8977 * Sets the text background color.
8980 * [I] infoPtr : valid pointer to the listview structure
8981 * [I] color : text background color
8987 static BOOL LISTVIEW_SetTextBkColor(LISTVIEW_INFO *infoPtr, COLORREF color)
8989 TRACE("(color=%x)\n", color);
8991 infoPtr->clrTextBk = color;
8997 * Sets the text foreground color.
9000 * [I] infoPtr : valid pointer to the listview structure
9001 * [I] color : text color
9007 static BOOL LISTVIEW_SetTextColor (LISTVIEW_INFO *infoPtr, COLORREF color)
9009 TRACE("(color=%x)\n", color);
9011 infoPtr->clrText = color;
9017 * Sets new ToolTip window to ListView control.
9020 * [I] infoPtr : valid pointer to the listview structure
9021 * [I] hwndNewToolTip : handle to new ToolTip
9026 static HWND LISTVIEW_SetToolTips( LISTVIEW_INFO *infoPtr, HWND hwndNewToolTip)
9028 HWND hwndOldToolTip = infoPtr->hwndToolTip;
9029 infoPtr->hwndToolTip = hwndNewToolTip;
9030 return hwndOldToolTip;
9035 * sets the Unicode character format flag for the control
9037 * [I] infoPtr :valid pointer to the listview structure
9038 * [I] fUnicode :true to switch to UNICODE false to switch to ANSI
9041 * Old Unicode Format
9043 static BOOL LISTVIEW_SetUnicodeFormat( LISTVIEW_INFO *infoPtr, BOOL unicode)
9045 SHORT rc = infoPtr->notifyFormat;
9046 infoPtr->notifyFormat = (unicode) ? NFR_UNICODE : NFR_ANSI;
9047 return rc == NFR_UNICODE;
9052 * sets the control view mode
9054 * [I] infoPtr :valid pointer to the listview structure
9055 * [I] nView :new view mode value
9061 static INT LISTVIEW_SetView(LISTVIEW_INFO *infoPtr, DWORD nView)
9065 if (infoPtr->uView == nView) return 1;
9067 if ((INT)nView < 0 || nView > LV_VIEW_MAX) return -1;
9068 if (nView == LV_VIEW_TILE)
9070 FIXME("View LV_VIEW_TILE unimplemented\n");
9074 infoPtr->uView = nView;
9076 SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
9077 ShowWindow(infoPtr->hwndHeader, SW_HIDE);
9079 ShowScrollBar(infoPtr->hwndSelf, SB_BOTH, FALSE);
9080 SetRectEmpty(&infoPtr->rcFocus);
9082 himl = (nView == LV_VIEW_ICON ? infoPtr->himlNormal : infoPtr->himlSmall);
9083 set_icon_size(&infoPtr->iconSize, himl, nView != LV_VIEW_ICON);
9088 case LV_VIEW_SMALLICON:
9089 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
9091 case LV_VIEW_DETAILS:
9096 LISTVIEW_CreateHeader( infoPtr );
9098 hl.prc = &infoPtr->rcList;
9100 SendMessageW(infoPtr->hwndHeader, HDM_LAYOUT, 0, (LPARAM)&hl);
9101 SetWindowPos(infoPtr->hwndHeader, infoPtr->hwndSelf, wp.x, wp.y, wp.cx, wp.cy,
9102 wp.flags | ((infoPtr->dwStyle & LVS_NOCOLUMNHEADER) ? SWP_HIDEWINDOW : SWP_SHOWWINDOW));
9109 LISTVIEW_UpdateItemSize(infoPtr);
9110 LISTVIEW_UpdateSize(infoPtr);
9111 LISTVIEW_UpdateScroll(infoPtr);
9112 LISTVIEW_InvalidateList(infoPtr);
9114 TRACE("nView=%d\n", nView);
9119 /* LISTVIEW_SetWorkAreas */
9123 * Callback internally used by LISTVIEW_SortItems() in response of LVM_SORTITEMS
9126 * [I] first : pointer to first ITEM_INFO to compare
9127 * [I] second : pointer to second ITEM_INFO to compare
9128 * [I] lParam : HWND of control
9131 * if first comes before second : negative
9132 * if first comes after second : positive
9133 * if first and second are equivalent : zero
9135 static INT WINAPI LISTVIEW_CallBackCompare(LPVOID first, LPVOID second, LPARAM lParam)
9137 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)lParam;
9138 ITEM_INFO* lv_first = DPA_GetPtr( first, 0 );
9139 ITEM_INFO* lv_second = DPA_GetPtr( second, 0 );
9141 /* Forward the call to the client defined callback */
9142 return (infoPtr->pfnCompare)( lv_first->lParam , lv_second->lParam, infoPtr->lParamSort );
9147 * Callback internally used by LISTVIEW_SortItems() in response of LVM_SORTITEMSEX
9150 * [I] first : pointer to first ITEM_INFO to compare
9151 * [I] second : pointer to second ITEM_INFO to compare
9152 * [I] lParam : HWND of control
9155 * if first comes before second : negative
9156 * if first comes after second : positive
9157 * if first and second are equivalent : zero
9159 static INT WINAPI LISTVIEW_CallBackCompareEx(LPVOID first, LPVOID second, LPARAM lParam)
9161 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)lParam;
9162 INT first_idx = DPA_GetPtrIndex( infoPtr->hdpaItems, first );
9163 INT second_idx = DPA_GetPtrIndex( infoPtr->hdpaItems, second );
9165 /* Forward the call to the client defined callback */
9166 return (infoPtr->pfnCompare)( first_idx, second_idx, infoPtr->lParamSort );
9171 * Sorts the listview items.
9174 * [I] infoPtr : valid pointer to the listview structure
9175 * [I] pfnCompare : application-defined value
9176 * [I] lParamSort : pointer to comparison callback
9177 * [I] IsEx : TRUE when LVM_SORTITEMSEX used
9183 static BOOL LISTVIEW_SortItems(LISTVIEW_INFO *infoPtr, PFNLVCOMPARE pfnCompare,
9184 LPARAM lParamSort, BOOL IsEx)
9188 LPVOID selectionMarkItem = NULL;
9189 LPVOID focusedItem = NULL;
9192 TRACE("(pfnCompare=%p, lParamSort=%lx)\n", pfnCompare, lParamSort);
9194 if (infoPtr->dwStyle & LVS_OWNERDATA) return FALSE;
9196 if (!pfnCompare) return FALSE;
9197 if (!infoPtr->hdpaItems) return FALSE;
9199 /* if there are 0 or 1 items, there is no need to sort */
9200 if (infoPtr->nItemCount < 2) return TRUE;
9202 /* clear selection */
9203 ranges_clear(infoPtr->selectionRanges);
9205 /* save selection mark and focused item */
9206 if (infoPtr->nSelectionMark >= 0)
9207 selectionMarkItem = DPA_GetPtr(infoPtr->hdpaItems, infoPtr->nSelectionMark);
9208 if (infoPtr->nFocusedItem >= 0)
9209 focusedItem = DPA_GetPtr(infoPtr->hdpaItems, infoPtr->nFocusedItem);
9211 infoPtr->pfnCompare = pfnCompare;
9212 infoPtr->lParamSort = lParamSort;
9214 DPA_Sort(infoPtr->hdpaItems, LISTVIEW_CallBackCompareEx, (LPARAM)infoPtr);
9216 DPA_Sort(infoPtr->hdpaItems, LISTVIEW_CallBackCompare, (LPARAM)infoPtr);
9218 /* restore selection ranges */
9219 for (i=0; i < infoPtr->nItemCount; i++)
9221 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, i);
9222 lpItem = DPA_GetPtr(hdpaSubItems, 0);
9224 if (lpItem->state & LVIS_SELECTED)
9225 ranges_additem(infoPtr->selectionRanges, i);
9227 /* restore selection mark and focused item */
9228 infoPtr->nSelectionMark = DPA_GetPtrIndex(infoPtr->hdpaItems, selectionMarkItem);
9229 infoPtr->nFocusedItem = DPA_GetPtrIndex(infoPtr->hdpaItems, focusedItem);
9231 /* I believe nHotItem should be left alone, see LISTVIEW_ShiftIndices */
9233 /* refresh the display */
9234 LISTVIEW_InvalidateList(infoPtr);
9240 * Update theme handle after a theme change.
9243 * [I] infoPtr : valid pointer to the listview structure
9247 * FAILURE : something else
9249 static LRESULT LISTVIEW_ThemeChanged(const LISTVIEW_INFO *infoPtr)
9251 HTHEME theme = GetWindowTheme(infoPtr->hwndSelf);
9252 CloseThemeData(theme);
9253 OpenThemeData(infoPtr->hwndSelf, themeClass);
9259 * Updates an items or rearranges the listview control.
9262 * [I] infoPtr : valid pointer to the listview structure
9263 * [I] nItem : item index
9269 static BOOL LISTVIEW_Update(LISTVIEW_INFO *infoPtr, INT nItem)
9271 TRACE("(nItem=%d)\n", nItem);
9273 if (nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
9275 /* rearrange with default alignment style */
9276 if (is_autoarrange(infoPtr))
9277 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
9279 LISTVIEW_InvalidateItem(infoPtr, nItem);
9286 * Draw the track line at the place defined in the infoPtr structure.
9287 * The line is drawn with a XOR pen so drawing the line for the second time
9288 * in the same place erases the line.
9291 * [I] infoPtr : valid pointer to the listview structure
9297 static BOOL LISTVIEW_DrawTrackLine(const LISTVIEW_INFO *infoPtr)
9301 if (infoPtr->xTrackLine == -1)
9304 if (!(hdc = GetDC(infoPtr->hwndSelf)))
9306 PatBlt( hdc, infoPtr->xTrackLine, infoPtr->rcList.top,
9307 1, infoPtr->rcList.bottom - infoPtr->rcList.top, DSTINVERT );
9308 ReleaseDC(infoPtr->hwndSelf, hdc);
9314 * Called when an edit control should be displayed. This function is called after
9315 * we are sure that there was a single click - not a double click (this is a TIMERPROC).
9318 * [I] hwnd : Handle to the listview
9319 * [I] uMsg : WM_TIMER (ignored)
9320 * [I] idEvent : The timer ID interpreted as a pointer to a DELAYED_EDIT_ITEM struct
9321 * [I] dwTimer : The elapsed time (ignored)
9326 static VOID CALLBACK LISTVIEW_DelayedEditItem(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
9328 DELAYED_ITEM_EDIT *editItem = (DELAYED_ITEM_EDIT *)idEvent;
9329 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongPtrW(hwnd, 0);
9331 KillTimer(hwnd, idEvent);
9332 editItem->fEnabled = FALSE;
9333 /* check if the item is still selected */
9334 if (infoPtr->bFocus && LISTVIEW_GetItemState(infoPtr, editItem->iItem, LVIS_SELECTED))
9335 LISTVIEW_EditLabelT(infoPtr, editItem->iItem, TRUE);
9340 * Creates the listview control - the WM_NCCREATE phase.
9343 * [I] hwnd : window handle
9344 * [I] lpcs : the create parameters
9350 static LRESULT LISTVIEW_NCCreate(HWND hwnd, const CREATESTRUCTW *lpcs)
9352 LISTVIEW_INFO *infoPtr;
9355 TRACE("(lpcs=%p)\n", lpcs);
9357 /* initialize info pointer */
9358 infoPtr = Alloc(sizeof(LISTVIEW_INFO));
9359 if (!infoPtr) return FALSE;
9361 SetWindowLongPtrW(hwnd, 0, (DWORD_PTR)infoPtr);
9363 infoPtr->hwndSelf = hwnd;
9364 infoPtr->dwStyle = lpcs->style; /* Note: may be changed in WM_CREATE */
9365 map_style_view(infoPtr);
9366 /* determine the type of structures to use */
9367 infoPtr->hwndNotify = lpcs->hwndParent;
9368 /* infoPtr->notifyFormat will be filled in WM_CREATE */
9370 /* initialize color information */
9371 infoPtr->clrBk = CLR_NONE;
9372 infoPtr->clrText = CLR_DEFAULT;
9373 infoPtr->clrTextBk = CLR_DEFAULT;
9374 LISTVIEW_SetBkColor(infoPtr, comctl32_color.clrWindow);
9376 /* set default values */
9377 infoPtr->nFocusedItem = -1;
9378 infoPtr->nSelectionMark = -1;
9379 infoPtr->nHotItem = -1;
9380 infoPtr->bRedraw = TRUE;
9381 infoPtr->bNoItemMetrics = TRUE;
9382 infoPtr->bDoChangeNotify = TRUE;
9383 infoPtr->autoSpacing = TRUE;
9384 infoPtr->iconSpacing.cx = GetSystemMetrics(SM_CXICONSPACING) - GetSystemMetrics(SM_CXICON);
9385 infoPtr->iconSpacing.cy = GetSystemMetrics(SM_CYICONSPACING) - GetSystemMetrics(SM_CYICON);
9386 infoPtr->nEditLabelItem = -1;
9387 infoPtr->nLButtonDownItem = -1;
9388 infoPtr->dwHoverTime = HOVER_DEFAULT; /* default system hover time */
9389 infoPtr->cWheelRemainder = 0;
9390 infoPtr->nMeasureItemHeight = 0;
9391 infoPtr->xTrackLine = -1; /* no track line */
9392 infoPtr->itemEdit.fEnabled = FALSE;
9393 infoPtr->iVersion = COMCTL32_VERSION;
9394 infoPtr->colRectsDirty = FALSE;
9396 /* get default font (icon title) */
9397 SystemParametersInfoW(SPI_GETICONTITLELOGFONT, 0, &logFont, 0);
9398 infoPtr->hDefaultFont = CreateFontIndirectW(&logFont);
9399 infoPtr->hFont = infoPtr->hDefaultFont;
9400 LISTVIEW_SaveTextMetrics(infoPtr);
9402 /* allocate memory for the data structure */
9403 if (!(infoPtr->selectionRanges = ranges_create(10))) goto fail;
9404 if (!(infoPtr->hdpaItems = DPA_Create(10))) goto fail;
9405 if (!(infoPtr->hdpaItemIds = DPA_Create(10))) goto fail;
9406 if (!(infoPtr->hdpaPosX = DPA_Create(10))) goto fail;
9407 if (!(infoPtr->hdpaPosY = DPA_Create(10))) goto fail;
9408 if (!(infoPtr->hdpaColumns = DPA_Create(10))) goto fail;
9412 DestroyWindow(infoPtr->hwndHeader);
9413 ranges_destroy(infoPtr->selectionRanges);
9414 DPA_Destroy(infoPtr->hdpaItems);
9415 DPA_Destroy(infoPtr->hdpaItemIds);
9416 DPA_Destroy(infoPtr->hdpaPosX);
9417 DPA_Destroy(infoPtr->hdpaPosY);
9418 DPA_Destroy(infoPtr->hdpaColumns);
9425 * Creates the listview control - the WM_CREATE phase. Most of the data is
9426 * already set up in LISTVIEW_NCCreate
9429 * [I] hwnd : window handle
9430 * [I] lpcs : the create parameters
9436 static LRESULT LISTVIEW_Create(HWND hwnd, const CREATESTRUCTW *lpcs)
9438 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongPtrW(hwnd, 0);
9440 TRACE("(lpcs=%p, style=0x%08x)\n", lpcs, lpcs->style);
9442 infoPtr->dwStyle = lpcs->style;
9443 map_style_view(infoPtr);
9445 infoPtr->notifyFormat = SendMessageW(infoPtr->hwndNotify, WM_NOTIFYFORMAT,
9446 (WPARAM)infoPtr->hwndSelf, NF_QUERY);
9447 /* on error defaulting to ANSI notifications */
9448 if (infoPtr->notifyFormat == 0) infoPtr->notifyFormat = NFR_ANSI;
9449 TRACE("notify format=%d\n", infoPtr->notifyFormat);
9451 if ((infoPtr->uView == LV_VIEW_DETAILS) && (lpcs->style & WS_VISIBLE))
9453 if (LISTVIEW_CreateHeader(infoPtr) < 0) return -1;
9456 infoPtr->hwndHeader = 0;
9458 /* init item size to avoid division by 0 */
9459 LISTVIEW_UpdateItemSize (infoPtr);
9460 LISTVIEW_UpdateSize (infoPtr);
9462 if (infoPtr->uView == LV_VIEW_DETAILS)
9464 if (!(LVS_NOCOLUMNHEADER & lpcs->style) && (WS_VISIBLE & lpcs->style))
9466 ShowWindow(infoPtr->hwndHeader, SW_SHOWNORMAL);
9468 LISTVIEW_UpdateScroll(infoPtr);
9469 /* send WM_MEASUREITEM notification */
9470 if (infoPtr->dwStyle & LVS_OWNERDRAWFIXED) notify_measureitem(infoPtr);
9473 OpenThemeData(hwnd, themeClass);
9475 /* initialize the icon sizes */
9476 set_icon_size(&infoPtr->iconSize, infoPtr->himlNormal, infoPtr->uView != LV_VIEW_ICON);
9477 set_icon_size(&infoPtr->iconStateSize, infoPtr->himlState, TRUE);
9483 * Destroys the listview control.
9486 * [I] infoPtr : valid pointer to the listview structure
9492 static LRESULT LISTVIEW_Destroy(LISTVIEW_INFO *infoPtr)
9494 HTHEME theme = GetWindowTheme(infoPtr->hwndSelf);
9495 CloseThemeData(theme);
9497 /* delete all items */
9498 LISTVIEW_DeleteAllItems(infoPtr, TRUE);
9505 * Enables the listview control.
9508 * [I] infoPtr : valid pointer to the listview structure
9509 * [I] bEnable : specifies whether to enable or disable the window
9515 static BOOL LISTVIEW_Enable(const LISTVIEW_INFO *infoPtr)
9517 if (infoPtr->dwStyle & LVS_OWNERDRAWFIXED)
9518 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
9524 * Erases the background of the listview control.
9527 * [I] infoPtr : valid pointer to the listview structure
9528 * [I] hdc : device context handle
9534 static inline BOOL LISTVIEW_EraseBkgnd(const LISTVIEW_INFO *infoPtr, HDC hdc)
9538 TRACE("(hdc=%p)\n", hdc);
9540 if (!GetClipBox(hdc, &rc)) return FALSE;
9542 if (infoPtr->clrBk == CLR_NONE)
9544 if (infoPtr->dwLvExStyle & LVS_EX_TRANSPARENTBKGND)
9545 return SendMessageW(infoPtr->hwndNotify, WM_PRINTCLIENT,
9546 (WPARAM)hdc, PRF_ERASEBKGND);
9548 return SendMessageW(infoPtr->hwndNotify, WM_ERASEBKGND, (WPARAM)hdc, 0);
9551 /* for double buffered controls we need to do this during refresh */
9552 if (infoPtr->dwLvExStyle & LVS_EX_DOUBLEBUFFER) return FALSE;
9554 return LISTVIEW_FillBkgnd(infoPtr, hdc, &rc);
9560 * Helper function for LISTVIEW_[HV]Scroll *only*.
9561 * Performs vertical/horizontal scrolling by a give amount.
9564 * [I] infoPtr : valid pointer to the listview structure
9565 * [I] dx : amount of horizontal scroll
9566 * [I] dy : amount of vertical scroll
9568 static void scroll_list(LISTVIEW_INFO *infoPtr, INT dx, INT dy)
9570 /* now we can scroll the list */
9571 ScrollWindowEx(infoPtr->hwndSelf, dx, dy, &infoPtr->rcList,
9572 &infoPtr->rcList, 0, 0, SW_ERASE | SW_INVALIDATE);
9573 /* if we have focus, adjust rect */
9574 OffsetRect(&infoPtr->rcFocus, dx, dy);
9575 UpdateWindow(infoPtr->hwndSelf);
9580 * Performs vertical scrolling.
9583 * [I] infoPtr : valid pointer to the listview structure
9584 * [I] nScrollCode : scroll code
9585 * [I] nScrollDiff : units to scroll in SB_INTERNAL mode, 0 otherwise
9586 * [I] hScrollWnd : scrollbar control window handle
9592 * SB_LINEUP/SB_LINEDOWN:
9593 * for LVS_ICON, LVS_SMALLICON is 37 by experiment
9594 * for LVS_REPORT is 1 line
9595 * for LVS_LIST cannot occur
9598 static LRESULT LISTVIEW_VScroll(LISTVIEW_INFO *infoPtr, INT nScrollCode,
9601 INT nOldScrollPos, nNewScrollPos;
9602 SCROLLINFO scrollInfo;
9605 TRACE("(nScrollCode=%d(%s), nScrollDiff=%d)\n", nScrollCode,
9606 debugscrollcode(nScrollCode), nScrollDiff);
9608 if (infoPtr->hwndEdit) SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
9610 scrollInfo.cbSize = sizeof(SCROLLINFO);
9611 scrollInfo.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_TRACKPOS;
9613 is_an_icon = ((infoPtr->uView == LV_VIEW_ICON) || (infoPtr->uView == LV_VIEW_SMALLICON));
9615 if (!GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo)) return 1;
9617 nOldScrollPos = scrollInfo.nPos;
9618 switch (nScrollCode)
9624 nScrollDiff = (is_an_icon) ? -LISTVIEW_SCROLL_ICON_LINE_SIZE : -1;
9628 nScrollDiff = (is_an_icon) ? LISTVIEW_SCROLL_ICON_LINE_SIZE : 1;
9632 nScrollDiff = -scrollInfo.nPage;
9636 nScrollDiff = scrollInfo.nPage;
9639 case SB_THUMBPOSITION:
9641 nScrollDiff = scrollInfo.nTrackPos - scrollInfo.nPos;
9648 /* quit right away if pos isn't changing */
9649 if (nScrollDiff == 0) return 0;
9651 /* calculate new position, and handle overflows */
9652 nNewScrollPos = scrollInfo.nPos + nScrollDiff;
9653 if (nScrollDiff > 0) {
9654 if (nNewScrollPos < nOldScrollPos ||
9655 nNewScrollPos > scrollInfo.nMax)
9656 nNewScrollPos = scrollInfo.nMax;
9658 if (nNewScrollPos > nOldScrollPos ||
9659 nNewScrollPos < scrollInfo.nMin)
9660 nNewScrollPos = scrollInfo.nMin;
9663 /* set the new position, and reread in case it changed */
9664 scrollInfo.fMask = SIF_POS;
9665 scrollInfo.nPos = nNewScrollPos;
9666 nNewScrollPos = SetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo, TRUE);
9668 /* carry on only if it really changed */
9669 if (nNewScrollPos == nOldScrollPos) return 0;
9671 /* now adjust to client coordinates */
9672 nScrollDiff = nOldScrollPos - nNewScrollPos;
9673 if (infoPtr->uView == LV_VIEW_DETAILS) nScrollDiff *= infoPtr->nItemHeight;
9675 /* and scroll the window */
9676 scroll_list(infoPtr, 0, nScrollDiff);
9683 * Performs horizontal scrolling.
9686 * [I] infoPtr : valid pointer to the listview structure
9687 * [I] nScrollCode : scroll code
9688 * [I] nScrollDiff : units to scroll in SB_INTERNAL mode, 0 otherwise
9689 * [I] hScrollWnd : scrollbar control window handle
9695 * SB_LINELEFT/SB_LINERIGHT:
9696 * for LVS_ICON, LVS_SMALLICON 1 pixel
9697 * for LVS_REPORT is 1 pixel
9698 * for LVS_LIST is 1 column --> which is a 1 because the
9699 * scroll is based on columns not pixels
9702 static LRESULT LISTVIEW_HScroll(LISTVIEW_INFO *infoPtr, INT nScrollCode,
9705 INT nOldScrollPos, nNewScrollPos;
9706 SCROLLINFO scrollInfo;
9709 TRACE("(nScrollCode=%d(%s), nScrollDiff=%d)\n", nScrollCode,
9710 debugscrollcode(nScrollCode), nScrollDiff);
9712 if (infoPtr->hwndEdit) SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
9714 scrollInfo.cbSize = sizeof(SCROLLINFO);
9715 scrollInfo.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_TRACKPOS;
9717 is_an_icon = ((infoPtr->uView == LV_VIEW_ICON) || (infoPtr->uView == LV_VIEW_SMALLICON));
9719 if (!GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo)) return 1;
9721 nOldScrollPos = scrollInfo.nPos;
9723 switch (nScrollCode)
9729 nScrollDiff = (is_an_icon) ? -LISTVIEW_SCROLL_ICON_LINE_SIZE : -1;
9733 nScrollDiff = (is_an_icon) ? LISTVIEW_SCROLL_ICON_LINE_SIZE : 1;
9737 nScrollDiff = -scrollInfo.nPage;
9741 nScrollDiff = scrollInfo.nPage;
9744 case SB_THUMBPOSITION:
9746 nScrollDiff = scrollInfo.nTrackPos - scrollInfo.nPos;
9753 /* quit right away if pos isn't changing */
9754 if (nScrollDiff == 0) return 0;
9756 /* calculate new position, and handle overflows */
9757 nNewScrollPos = scrollInfo.nPos + nScrollDiff;
9758 if (nScrollDiff > 0) {
9759 if (nNewScrollPos < nOldScrollPos ||
9760 nNewScrollPos > scrollInfo.nMax)
9761 nNewScrollPos = scrollInfo.nMax;
9763 if (nNewScrollPos > nOldScrollPos ||
9764 nNewScrollPos < scrollInfo.nMin)
9765 nNewScrollPos = scrollInfo.nMin;
9768 /* set the new position, and reread in case it changed */
9769 scrollInfo.fMask = SIF_POS;
9770 scrollInfo.nPos = nNewScrollPos;
9771 nNewScrollPos = SetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo, TRUE);
9773 /* carry on only if it really changed */
9774 if (nNewScrollPos == nOldScrollPos) return 0;
9776 if (infoPtr->hwndHeader) LISTVIEW_UpdateHeaderSize(infoPtr, nNewScrollPos);
9778 /* now adjust to client coordinates */
9779 nScrollDiff = nOldScrollPos - nNewScrollPos;
9780 if (infoPtr->uView == LV_VIEW_LIST) nScrollDiff *= infoPtr->nItemWidth;
9782 /* and scroll the window */
9783 scroll_list(infoPtr, nScrollDiff, 0);
9788 static LRESULT LISTVIEW_MouseWheel(LISTVIEW_INFO *infoPtr, INT wheelDelta)
9790 UINT pulScrollLines = 3;
9792 TRACE("(wheelDelta=%d)\n", wheelDelta);
9794 switch(infoPtr->uView)
9797 case LV_VIEW_SMALLICON:
9799 * listview should be scrolled by a multiple of 37 dependently on its dimension or its visible item number
9800 * should be fixed in the future.
9802 LISTVIEW_VScroll(infoPtr, SB_INTERNAL, (wheelDelta > 0) ?
9803 -LISTVIEW_SCROLL_ICON_LINE_SIZE : LISTVIEW_SCROLL_ICON_LINE_SIZE);
9806 case LV_VIEW_DETAILS:
9807 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
9809 /* if scrolling changes direction, ignore left overs */
9810 if ((wheelDelta < 0 && infoPtr->cWheelRemainder < 0) ||
9811 (wheelDelta > 0 && infoPtr->cWheelRemainder > 0))
9812 infoPtr->cWheelRemainder += wheelDelta;
9814 infoPtr->cWheelRemainder = wheelDelta;
9815 if (infoPtr->cWheelRemainder && pulScrollLines)
9818 pulScrollLines = min((UINT)LISTVIEW_GetCountPerColumn(infoPtr), pulScrollLines);
9819 cLineScroll = pulScrollLines * (float)infoPtr->cWheelRemainder / WHEEL_DELTA;
9820 infoPtr->cWheelRemainder -= WHEEL_DELTA * cLineScroll / (int)pulScrollLines;
9821 LISTVIEW_VScroll(infoPtr, SB_INTERNAL, -cLineScroll);
9826 LISTVIEW_HScroll(infoPtr, (wheelDelta > 0) ? SB_LINELEFT : SB_LINERIGHT, 0);
9837 * [I] infoPtr : valid pointer to the listview structure
9838 * [I] nVirtualKey : virtual key
9839 * [I] lKeyData : key data
9844 static LRESULT LISTVIEW_KeyDown(LISTVIEW_INFO *infoPtr, INT nVirtualKey, LONG lKeyData)
9846 HWND hwndSelf = infoPtr->hwndSelf;
9848 NMLVKEYDOWN nmKeyDown;
9850 TRACE("(nVirtualKey=%d, lKeyData=%d)\n", nVirtualKey, lKeyData);
9852 /* send LVN_KEYDOWN notification */
9853 nmKeyDown.wVKey = nVirtualKey;
9854 nmKeyDown.flags = 0;
9855 notify_hdr(infoPtr, LVN_KEYDOWN, &nmKeyDown.hdr);
9856 if (!IsWindow(hwndSelf))
9859 switch (nVirtualKey)
9862 nItem = infoPtr->nFocusedItem;
9863 if (infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES)
9864 toggle_checkbox_state(infoPtr, infoPtr->nFocusedItem);
9868 if ((infoPtr->nItemCount > 0) && (infoPtr->nFocusedItem != -1))
9870 if (!notify(infoPtr, NM_RETURN)) return 0;
9871 if (!notify(infoPtr, LVN_ITEMACTIVATE)) return 0;
9876 if (infoPtr->nItemCount > 0)
9881 if (infoPtr->nItemCount > 0)
9882 nItem = infoPtr->nItemCount - 1;
9886 nItem = LISTVIEW_GetNextItem(infoPtr, infoPtr->nFocusedItem, LVNI_TOLEFT);
9890 nItem = LISTVIEW_GetNextItem(infoPtr, infoPtr->nFocusedItem, LVNI_ABOVE);
9894 nItem = LISTVIEW_GetNextItem(infoPtr, infoPtr->nFocusedItem, LVNI_TORIGHT);
9898 nItem = LISTVIEW_GetNextItem(infoPtr, infoPtr->nFocusedItem, LVNI_BELOW);
9902 if (infoPtr->uView == LV_VIEW_DETAILS)
9904 INT topidx = LISTVIEW_GetTopIndex(infoPtr);
9905 if (infoPtr->nFocusedItem == topidx)
9906 nItem = topidx - LISTVIEW_GetCountPerColumn(infoPtr) + 1;
9911 nItem = infoPtr->nFocusedItem - LISTVIEW_GetCountPerColumn(infoPtr)
9912 * LISTVIEW_GetCountPerRow(infoPtr);
9913 if(nItem < 0) nItem = 0;
9917 if (infoPtr->uView == LV_VIEW_DETAILS)
9919 INT topidx = LISTVIEW_GetTopIndex(infoPtr);
9920 INT cnt = LISTVIEW_GetCountPerColumn(infoPtr);
9921 if (infoPtr->nFocusedItem == topidx + cnt - 1)
9922 nItem = infoPtr->nFocusedItem + cnt - 1;
9924 nItem = topidx + cnt - 1;
9927 nItem = infoPtr->nFocusedItem + LISTVIEW_GetCountPerColumn(infoPtr)
9928 * LISTVIEW_GetCountPerRow(infoPtr);
9929 if(nItem >= infoPtr->nItemCount) nItem = infoPtr->nItemCount - 1;
9933 if ((nItem != -1) && (nItem != infoPtr->nFocusedItem || nVirtualKey == VK_SPACE))
9934 LISTVIEW_KeySelection(infoPtr, nItem, nVirtualKey == VK_SPACE);
9944 * [I] infoPtr : valid pointer to the listview structure
9949 static LRESULT LISTVIEW_KillFocus(LISTVIEW_INFO *infoPtr)
9953 /* drop any left over scroll amount */
9954 infoPtr->cWheelRemainder = 0;
9956 /* if we did not have the focus, there's nothing more to do */
9957 if (!infoPtr->bFocus) return 0;
9959 /* send NM_KILLFOCUS notification */
9960 if (!notify(infoPtr, NM_KILLFOCUS)) return 0;
9962 /* if we have a focus rectangle, get rid of it */
9963 LISTVIEW_ShowFocusRect(infoPtr, FALSE);
9965 /* if have a marquee selection, stop it */
9966 if (infoPtr->bMarqueeSelect)
9968 /* Remove the marquee rectangle and release our mouse capture */
9969 LISTVIEW_InvalidateRect(infoPtr, &infoPtr->marqueeRect);
9972 SetRect(&infoPtr->marqueeRect, 0, 0, 0, 0);
9974 infoPtr->bMarqueeSelect = FALSE;
9975 infoPtr->bScrolling = FALSE;
9976 KillTimer(infoPtr->hwndSelf, (UINT_PTR) infoPtr);
9979 /* set window focus flag */
9980 infoPtr->bFocus = FALSE;
9982 /* invalidate the selected items before resetting focus flag */
9983 LISTVIEW_InvalidateSelectedItems(infoPtr);
9990 * Processes double click messages (left mouse button).
9993 * [I] infoPtr : valid pointer to the listview structure
9994 * [I] wKey : key flag
9995 * [I] x,y : mouse coordinate
10000 static LRESULT LISTVIEW_LButtonDblClk(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
10002 LVHITTESTINFO htInfo;
10004 TRACE("(key=%hu, X=%u, Y=%u)\n", wKey, x, y);
10006 /* Cancel the item edition if any */
10007 if (infoPtr->itemEdit.fEnabled)
10009 KillTimer(infoPtr->hwndSelf, (UINT_PTR)&infoPtr->itemEdit);
10010 infoPtr->itemEdit.fEnabled = FALSE;
10013 /* send NM_RELEASEDCAPTURE notification */
10014 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
10019 /* send NM_DBLCLK notification */
10020 LISTVIEW_HitTest(infoPtr, &htInfo, TRUE, FALSE);
10021 if (!notify_click(infoPtr, NM_DBLCLK, &htInfo)) return 0;
10023 /* To send the LVN_ITEMACTIVATE, it must be on an Item */
10024 if(htInfo.iItem != -1) notify_itemactivate(infoPtr,&htInfo);
10029 static LRESULT LISTVIEW_TrackMouse(const LISTVIEW_INFO *infoPtr, POINT pt)
10034 r.top = r.bottom = pt.y;
10035 r.left = r.right = pt.x;
10037 InflateRect(&r, GetSystemMetrics(SM_CXDRAG), GetSystemMetrics(SM_CYDRAG));
10039 SetCapture(infoPtr->hwndSelf);
10043 if (PeekMessageW(&msg, 0, 0, 0, PM_REMOVE | PM_NOYIELD))
10045 if (msg.message == WM_MOUSEMOVE)
10047 pt.x = (short)LOWORD(msg.lParam);
10048 pt.y = (short)HIWORD(msg.lParam);
10049 if (PtInRect(&r, pt))
10057 else if (msg.message >= WM_LBUTTONDOWN &&
10058 msg.message <= WM_RBUTTONDBLCLK)
10063 DispatchMessageW(&msg);
10066 if (GetCapture() != infoPtr->hwndSelf)
10077 * Processes mouse down messages (left mouse button).
10080 * infoPtr [I ] valid pointer to the listview structure
10081 * wKey [I ] key flag
10082 * x,y [I ] mouse coordinate
10087 static LRESULT LISTVIEW_LButtonDown(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
10089 LVHITTESTINFO lvHitTestInfo;
10090 static BOOL bGroupSelect = TRUE;
10091 POINT pt = { x, y };
10094 TRACE("(key=%hu, X=%u, Y=%u)\n", wKey, x, y);
10096 /* send NM_RELEASEDCAPTURE notification */
10097 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
10099 /* set left button down flag and record the click position */
10100 infoPtr->bLButtonDown = TRUE;
10101 infoPtr->ptClickPos = pt;
10102 infoPtr->bDragging = FALSE;
10103 infoPtr->bMarqueeSelect = FALSE;
10104 infoPtr->bScrolling = FALSE;
10106 lvHitTestInfo.pt.x = x;
10107 lvHitTestInfo.pt.y = y;
10109 nItem = LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, TRUE);
10110 TRACE("at %s, nItem=%d\n", wine_dbgstr_point(&pt), nItem);
10111 if ((nItem >= 0) && (nItem < infoPtr->nItemCount))
10113 if ((infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES) && (lvHitTestInfo.flags & LVHT_ONITEMSTATEICON))
10115 toggle_checkbox_state(infoPtr, nItem);
10119 if (infoPtr->dwStyle & LVS_SINGLESEL)
10121 if (LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED))
10122 infoPtr->nEditLabelItem = nItem;
10124 LISTVIEW_SetSelection(infoPtr, nItem);
10128 if ((wKey & MK_CONTROL) && (wKey & MK_SHIFT))
10132 if (!LISTVIEW_AddGroupSelection(infoPtr, nItem)) return 0;
10133 LISTVIEW_SetItemFocus(infoPtr, nItem);
10134 infoPtr->nSelectionMark = nItem;
10140 item.state = LVIS_SELECTED | LVIS_FOCUSED;
10141 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
10143 LISTVIEW_SetItemState(infoPtr,nItem,&item);
10144 infoPtr->nSelectionMark = nItem;
10147 else if (wKey & MK_CONTROL)
10151 bGroupSelect = (LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED) == 0);
10153 item.state = (bGroupSelect ? LVIS_SELECTED : 0) | LVIS_FOCUSED;
10154 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
10155 LISTVIEW_SetItemState(infoPtr, nItem, &item);
10156 infoPtr->nSelectionMark = nItem;
10158 else if (wKey & MK_SHIFT)
10160 LISTVIEW_SetGroupSelection(infoPtr, nItem);
10164 if (LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED))
10166 infoPtr->nEditLabelItem = nItem;
10167 infoPtr->nLButtonDownItem = nItem;
10169 LISTVIEW_SetItemFocus(infoPtr, nItem);
10172 /* set selection (clears other pre-existing selections) */
10173 LISTVIEW_SetSelection(infoPtr, nItem);
10177 if (infoPtr->dwLvExStyle & LVS_EX_ONECLICKACTIVATE)
10178 if(lvHitTestInfo.iItem != -1) notify_itemactivate(infoPtr,&lvHitTestInfo);
10182 if (!infoPtr->bFocus)
10183 SetFocus(infoPtr->hwndSelf);
10185 /* remove all selections */
10186 if (!(wKey & MK_CONTROL) && !(wKey & MK_SHIFT))
10187 LISTVIEW_DeselectAll(infoPtr);
10196 * Processes mouse up messages (left mouse button).
10199 * infoPtr [I ] valid pointer to the listview structure
10200 * wKey [I ] key flag
10201 * x,y [I ] mouse coordinate
10206 static LRESULT LISTVIEW_LButtonUp(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
10208 LVHITTESTINFO lvHitTestInfo;
10210 TRACE("(key=%hu, X=%u, Y=%u)\n", wKey, x, y);
10212 if (!infoPtr->bLButtonDown) return 0;
10214 lvHitTestInfo.pt.x = x;
10215 lvHitTestInfo.pt.y = y;
10217 /* send NM_CLICK notification */
10218 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
10219 if (!notify_click(infoPtr, NM_CLICK, &lvHitTestInfo)) return 0;
10221 /* set left button flag */
10222 infoPtr->bLButtonDown = FALSE;
10224 /* set a single selection, reset others */
10225 if(lvHitTestInfo.iItem == infoPtr->nLButtonDownItem && lvHitTestInfo.iItem != -1)
10226 LISTVIEW_SetSelection(infoPtr, infoPtr->nLButtonDownItem);
10227 infoPtr->nLButtonDownItem = -1;
10229 if (infoPtr->bDragging || infoPtr->bMarqueeSelect)
10231 /* Remove the marquee rectangle and release our mouse capture */
10232 if (infoPtr->bMarqueeSelect)
10234 LISTVIEW_InvalidateRect(infoPtr, &infoPtr->marqueeDrawRect);
10238 SetRect(&infoPtr->marqueeRect, 0, 0, 0, 0);
10239 SetRect(&infoPtr->marqueeDrawRect, 0, 0, 0, 0);
10241 infoPtr->bDragging = FALSE;
10242 infoPtr->bMarqueeSelect = FALSE;
10243 infoPtr->bScrolling = FALSE;
10245 KillTimer(infoPtr->hwndSelf, (UINT_PTR) infoPtr);
10249 /* if we clicked on a selected item, edit the label */
10250 if(lvHitTestInfo.iItem == infoPtr->nEditLabelItem && (lvHitTestInfo.flags & LVHT_ONITEMLABEL))
10252 /* we want to make sure the user doesn't want to do a double click. So we will
10253 * delay the edit. WM_LBUTTONDBLCLICK will cancel the timer
10255 infoPtr->itemEdit.fEnabled = TRUE;
10256 infoPtr->itemEdit.iItem = lvHitTestInfo.iItem;
10257 SetTimer(infoPtr->hwndSelf,
10258 (UINT_PTR)&infoPtr->itemEdit,
10259 GetDoubleClickTime(),
10260 LISTVIEW_DelayedEditItem);
10263 if (!infoPtr->bFocus)
10264 SetFocus(infoPtr->hwndSelf);
10271 * Destroys the listview control (called after WM_DESTROY).
10274 * [I] infoPtr : valid pointer to the listview structure
10279 static LRESULT LISTVIEW_NCDestroy(LISTVIEW_INFO *infoPtr)
10285 /* destroy data structure */
10286 DPA_Destroy(infoPtr->hdpaItems);
10287 DPA_Destroy(infoPtr->hdpaItemIds);
10288 DPA_Destroy(infoPtr->hdpaPosX);
10289 DPA_Destroy(infoPtr->hdpaPosY);
10291 for (i = 0; i < DPA_GetPtrCount(infoPtr->hdpaColumns); i++)
10292 Free(DPA_GetPtr(infoPtr->hdpaColumns, i));
10293 DPA_Destroy(infoPtr->hdpaColumns);
10294 ranges_destroy(infoPtr->selectionRanges);
10296 /* destroy image lists */
10297 if (!(infoPtr->dwStyle & LVS_SHAREIMAGELISTS))
10299 ImageList_Destroy(infoPtr->himlNormal);
10300 ImageList_Destroy(infoPtr->himlSmall);
10301 ImageList_Destroy(infoPtr->himlState);
10304 /* destroy font, bkgnd brush */
10305 infoPtr->hFont = 0;
10306 if (infoPtr->hDefaultFont) DeleteObject(infoPtr->hDefaultFont);
10307 if (infoPtr->clrBk != CLR_NONE) DeleteObject(infoPtr->hBkBrush);
10309 SetWindowLongPtrW(infoPtr->hwndSelf, 0, 0);
10311 /* free listview info pointer*/
10319 * Handles notifications.
10322 * [I] infoPtr : valid pointer to the listview structure
10323 * [I] lpnmhdr : notification information
10328 static LRESULT LISTVIEW_Notify(LISTVIEW_INFO *infoPtr, NMHDR *lpnmhdr)
10332 TRACE("(lpnmhdr=%p)\n", lpnmhdr);
10334 if (!lpnmhdr || lpnmhdr->hwndFrom != infoPtr->hwndHeader) return 0;
10336 /* remember: HDN_LAST < HDN_FIRST */
10337 if (lpnmhdr->code > HDN_FIRST || lpnmhdr->code < HDN_LAST) return 0;
10338 lpnmh = (NMHEADERW *)lpnmhdr;
10340 if (lpnmh->iItem < 0 || lpnmh->iItem >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return 0;
10342 switch (lpnmhdr->code)
10347 COLUMN_INFO *lpColumnInfo;
10351 if (!lpnmh->pitem || !(lpnmh->pitem->mask & HDI_WIDTH))
10354 /* remove the old line (if any) */
10355 LISTVIEW_DrawTrackLine(infoPtr);
10357 /* compute & draw the new line */
10358 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, lpnmh->iItem);
10359 x = lpColumnInfo->rcHeader.left + lpnmh->pitem->cxy;
10360 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
10361 infoPtr->xTrackLine = x + ptOrigin.x;
10362 LISTVIEW_DrawTrackLine(infoPtr);
10363 return notify_forward_header(infoPtr, lpnmh);
10366 case HDN_ENDTRACKA:
10367 case HDN_ENDTRACKW:
10368 /* remove the track line (if any) */
10369 LISTVIEW_DrawTrackLine(infoPtr);
10370 infoPtr->xTrackLine = -1;
10371 return notify_forward_header(infoPtr, lpnmh);
10373 case HDN_BEGINDRAG:
10374 if ((infoPtr->dwLvExStyle & LVS_EX_HEADERDRAGDROP) == 0) return 1;
10375 return notify_forward_header(infoPtr, lpnmh);
10378 infoPtr->colRectsDirty = TRUE;
10379 LISTVIEW_InvalidateList(infoPtr);
10380 return notify_forward_header(infoPtr, lpnmh);
10382 case HDN_ITEMCHANGEDW:
10383 case HDN_ITEMCHANGEDA:
10385 COLUMN_INFO *lpColumnInfo;
10389 if (!lpnmh->pitem || !(lpnmh->pitem->mask & HDI_WIDTH))
10391 hdi.mask = HDI_WIDTH;
10392 if (!SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, lpnmh->iItem, (LPARAM)&hdi)) return 0;
10396 cxy = lpnmh->pitem->cxy;
10398 /* determine how much we change since the last know position */
10399 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, lpnmh->iItem);
10400 dx = cxy - (lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left);
10403 lpColumnInfo->rcHeader.right += dx;
10405 hdi.mask = HDI_ORDER;
10406 SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, lpnmh->iItem, (LPARAM)&hdi);
10408 /* not the rightmost one */
10409 if (hdi.iOrder + 1 < DPA_GetPtrCount(infoPtr->hdpaColumns))
10411 INT nIndex = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX,
10412 hdi.iOrder + 1, 0);
10413 LISTVIEW_ScrollColumns(infoPtr, nIndex, dx);
10417 /* only needs to update the scrolls */
10418 infoPtr->nItemWidth += dx;
10419 LISTVIEW_UpdateScroll(infoPtr);
10421 LISTVIEW_UpdateItemSize(infoPtr);
10422 if (infoPtr->uView == LV_VIEW_DETAILS && is_redrawing(infoPtr))
10425 RECT rcCol = lpColumnInfo->rcHeader;
10427 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
10428 OffsetRect(&rcCol, ptOrigin.x, 0);
10430 rcCol.top = infoPtr->rcList.top;
10431 rcCol.bottom = infoPtr->rcList.bottom;
10433 /* resizing left-aligned columns leaves most of the left side untouched */
10434 if ((lpColumnInfo->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_LEFT)
10436 INT nMaxDirty = infoPtr->nEllipsisWidth + infoPtr->ntmMaxCharWidth;
10439 rcCol.left = max (rcCol.left, rcCol.right - nMaxDirty);
10442 /* when shrinking the last column clear the now unused field */
10443 if (hdi.iOrder == DPA_GetPtrCount(infoPtr->hdpaColumns) - 1)
10449 /* deal with right from rightmost column area */
10450 right.left = rcCol.right;
10451 right.top = rcCol.top;
10452 right.bottom = rcCol.bottom;
10453 right.right = infoPtr->rcList.right;
10455 LISTVIEW_InvalidateRect(infoPtr, &right);
10458 LISTVIEW_InvalidateRect(infoPtr, &rcCol);
10464 case HDN_ITEMCLICKW:
10465 case HDN_ITEMCLICKA:
10467 /* Handle sorting by Header Column */
10470 ZeroMemory(&nmlv, sizeof(NMLISTVIEW));
10472 nmlv.iSubItem = lpnmh->iItem;
10473 notify_listview(infoPtr, LVN_COLUMNCLICK, &nmlv);
10474 return notify_forward_header(infoPtr, lpnmh);
10477 case HDN_DIVIDERDBLCLICKW:
10478 case HDN_DIVIDERDBLCLICKA:
10479 /* FIXME: for LVS_EX_HEADERINALLVIEWS and not LV_VIEW_DETAILS
10480 we should use LVSCW_AUTOSIZE_USEHEADER, helper rework or
10481 split needed for that */
10482 LISTVIEW_SetColumnWidth(infoPtr, lpnmh->iItem, LVSCW_AUTOSIZE);
10483 return notify_forward_header(infoPtr, lpnmh);
10490 * Paint non-client area of control.
10493 * [I] infoPtr : valid pointer to the listview structureof the sender
10494 * [I] region : update region
10497 * TRUE - frame was painted
10498 * FALSE - call default window proc
10500 static BOOL LISTVIEW_NCPaint(const LISTVIEW_INFO *infoPtr, HRGN region)
10502 HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
10506 int cxEdge = GetSystemMetrics (SM_CXEDGE),
10507 cyEdge = GetSystemMetrics (SM_CYEDGE);
10510 return DefWindowProcW (infoPtr->hwndSelf, WM_NCPAINT, (WPARAM)region, 0);
10512 GetWindowRect(infoPtr->hwndSelf, &r);
10514 cliprgn = CreateRectRgn (r.left + cxEdge, r.top + cyEdge,
10515 r.right - cxEdge, r.bottom - cyEdge);
10516 if (region != (HRGN)1)
10517 CombineRgn (cliprgn, cliprgn, region, RGN_AND);
10518 OffsetRect(&r, -r.left, -r.top);
10520 dc = GetDCEx(infoPtr->hwndSelf, region, DCX_WINDOW|DCX_INTERSECTRGN);
10521 OffsetRect(&r, -r.left, -r.top);
10523 if (IsThemeBackgroundPartiallyTransparent (theme, 0, 0))
10524 DrawThemeParentBackground(infoPtr->hwndSelf, dc, &r);
10525 DrawThemeBackground (theme, dc, 0, 0, &r, 0);
10526 ReleaseDC(infoPtr->hwndSelf, dc);
10528 /* Call default proc to get the scrollbars etc. painted */
10529 DefWindowProcW (infoPtr->hwndSelf, WM_NCPAINT, (WPARAM)cliprgn, 0);
10536 * Determines the type of structure to use.
10539 * [I] infoPtr : valid pointer to the listview structureof the sender
10540 * [I] hwndFrom : listview window handle
10541 * [I] nCommand : command specifying the nature of the WM_NOTIFYFORMAT
10546 static LRESULT LISTVIEW_NotifyFormat(LISTVIEW_INFO *infoPtr, HWND hwndFrom, INT nCommand)
10548 TRACE("(hwndFrom=%p, nCommand=%d)\n", hwndFrom, nCommand);
10550 if (nCommand == NF_REQUERY)
10551 infoPtr->notifyFormat = SendMessageW(infoPtr->hwndNotify, WM_NOTIFYFORMAT, (WPARAM)infoPtr->hwndSelf, NF_QUERY);
10553 return infoPtr->notifyFormat;
10558 * Paints/Repaints the listview control. Internal use.
10561 * [I] infoPtr : valid pointer to the listview structure
10562 * [I] hdc : device context handle
10567 static LRESULT LISTVIEW_Paint(LISTVIEW_INFO *infoPtr, HDC hdc)
10569 TRACE("(hdc=%p)\n", hdc);
10571 if (infoPtr->bNoItemMetrics && infoPtr->nItemCount)
10573 infoPtr->bNoItemMetrics = FALSE;
10574 LISTVIEW_UpdateItemSize(infoPtr);
10575 if (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON)
10576 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
10577 LISTVIEW_UpdateScroll(infoPtr);
10580 if (infoPtr->hwndHeader) UpdateWindow(infoPtr->hwndHeader);
10583 LISTVIEW_Refresh(infoPtr, hdc, NULL);
10588 hdc = BeginPaint(infoPtr->hwndSelf, &ps);
10589 if (!hdc) return 1;
10590 LISTVIEW_Refresh(infoPtr, hdc, ps.fErase ? &ps.rcPaint : NULL);
10591 EndPaint(infoPtr->hwndSelf, &ps);
10599 * Paints/Repaints the listview control, WM_PAINT handler.
10602 * [I] infoPtr : valid pointer to the listview structure
10603 * [I] hdc : device context handle
10608 static inline LRESULT LISTVIEW_WMPaint(LISTVIEW_INFO *infoPtr, HDC hdc)
10610 TRACE("(hdc=%p)\n", hdc);
10612 if (!is_redrawing(infoPtr))
10613 return DefWindowProcW (infoPtr->hwndSelf, WM_PAINT, (WPARAM)hdc, 0);
10615 return LISTVIEW_Paint(infoPtr, hdc);
10620 * Paints/Repaints the listview control.
10623 * [I] infoPtr : valid pointer to the listview structure
10624 * [I] hdc : device context handle
10625 * [I] options : drawing options
10630 static LRESULT LISTVIEW_PrintClient(LISTVIEW_INFO *infoPtr, HDC hdc, DWORD options)
10632 FIXME("Partial Stub: (hdc=%p options=0x%08x)\n", hdc, options);
10634 if ((options & PRF_CHECKVISIBLE) && !IsWindowVisible(infoPtr->hwndSelf))
10637 if (options & PRF_ERASEBKGND)
10638 LISTVIEW_EraseBkgnd(infoPtr, hdc);
10640 if (options & PRF_CLIENT)
10641 LISTVIEW_Paint(infoPtr, hdc);
10649 * Processes double click messages (right mouse button).
10652 * [I] infoPtr : valid pointer to the listview structure
10653 * [I] wKey : key flag
10654 * [I] x,y : mouse coordinate
10659 static LRESULT LISTVIEW_RButtonDblClk(const LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
10661 LVHITTESTINFO lvHitTestInfo;
10663 TRACE("(key=%hu,X=%u,Y=%u)\n", wKey, x, y);
10665 /* send NM_RELEASEDCAPTURE notification */
10666 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
10668 /* send NM_RDBLCLK notification */
10669 lvHitTestInfo.pt.x = x;
10670 lvHitTestInfo.pt.y = y;
10671 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
10672 notify_click(infoPtr, NM_RDBLCLK, &lvHitTestInfo);
10679 * Processes WM_RBUTTONDOWN message and corresponding drag operation.
10682 * [I] infoPtr : valid pointer to the listview structure
10683 * [I] wKey : key flag
10684 * [I] x, y : mouse coordinate
10689 static LRESULT LISTVIEW_RButtonDown(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
10694 TRACE("(key=%hu, x=%d, y=%d)\n", wKey, x, y);
10696 /* send NM_RELEASEDCAPTURE notification */
10697 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
10699 /* determine the index of the selected item */
10702 item = LISTVIEW_HitTest(infoPtr, &ht, TRUE, TRUE);
10704 /* make sure the listview control window has the focus */
10705 if (!infoPtr->bFocus) SetFocus(infoPtr->hwndSelf);
10707 if ((item >= 0) && (item < infoPtr->nItemCount))
10709 LISTVIEW_SetItemFocus(infoPtr, item);
10710 if (!((wKey & MK_SHIFT) || (wKey & MK_CONTROL)) &&
10711 !LISTVIEW_GetItemState(infoPtr, item, LVIS_SELECTED))
10712 LISTVIEW_SetSelection(infoPtr, item);
10715 LISTVIEW_DeselectAll(infoPtr);
10717 if (LISTVIEW_TrackMouse(infoPtr, ht.pt))
10719 if (ht.iItem != -1)
10723 memset(&nmlv, 0, sizeof(nmlv));
10724 nmlv.iItem = ht.iItem;
10725 nmlv.ptAction = ht.pt;
10727 notify_listview(infoPtr, LVN_BEGINRDRAG, &nmlv);
10732 SetFocus(infoPtr->hwndSelf);
10736 LISTVIEW_HitTest(infoPtr, &ht, TRUE, FALSE);
10738 if (notify_click(infoPtr, NM_RCLICK, &ht))
10740 /* Send a WM_CONTEXTMENU message in response to the WM_RBUTTONUP */
10741 SendMessageW(infoPtr->hwndSelf, WM_CONTEXTMENU,
10742 (WPARAM)infoPtr->hwndSelf, (LPARAM)GetMessagePos());
10754 * [I] infoPtr : valid pointer to the listview structure
10755 * [I] hwnd : window handle of window containing the cursor
10756 * [I] nHittest : hit-test code
10757 * [I] wMouseMsg : ideintifier of the mouse message
10760 * TRUE if cursor is set
10763 static BOOL LISTVIEW_SetCursor(const LISTVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
10765 LVHITTESTINFO lvHitTestInfo;
10767 if (!LISTVIEW_IsHotTracking(infoPtr)) goto forward;
10769 if (!infoPtr->hHotCursor) goto forward;
10771 GetCursorPos(&lvHitTestInfo.pt);
10772 if (LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, FALSE, FALSE) < 0) goto forward;
10774 SetCursor(infoPtr->hHotCursor);
10780 return DefWindowProcW(infoPtr->hwndSelf, WM_SETCURSOR, wParam, lParam);
10788 * [I] infoPtr : valid pointer to the listview structure
10789 * [I] hwndLoseFocus : handle of previously focused window
10794 static LRESULT LISTVIEW_SetFocus(LISTVIEW_INFO *infoPtr, HWND hwndLoseFocus)
10796 TRACE("(hwndLoseFocus=%p)\n", hwndLoseFocus);
10798 /* if we have the focus already, there's nothing to do */
10799 if (infoPtr->bFocus) return 0;
10801 /* send NM_SETFOCUS notification */
10802 if (!notify(infoPtr, NM_SETFOCUS)) return 0;
10804 /* set window focus flag */
10805 infoPtr->bFocus = TRUE;
10807 /* put the focus rect back on */
10808 LISTVIEW_ShowFocusRect(infoPtr, TRUE);
10810 /* redraw all visible selected items */
10811 LISTVIEW_InvalidateSelectedItems(infoPtr);
10821 * [I] infoPtr : valid pointer to the listview structure
10822 * [I] fRedraw : font handle
10823 * [I] fRedraw : redraw flag
10828 static LRESULT LISTVIEW_SetFont(LISTVIEW_INFO *infoPtr, HFONT hFont, WORD fRedraw)
10830 HFONT oldFont = infoPtr->hFont;
10831 INT oldHeight = infoPtr->nItemHeight;
10833 TRACE("(hfont=%p,redraw=%hu)\n", hFont, fRedraw);
10835 infoPtr->hFont = hFont ? hFont : infoPtr->hDefaultFont;
10836 if (infoPtr->hFont == oldFont) return 0;
10838 LISTVIEW_SaveTextMetrics(infoPtr);
10840 infoPtr->nItemHeight = LISTVIEW_CalculateItemHeight(infoPtr);
10842 if (infoPtr->uView == LV_VIEW_DETAILS)
10844 SendMessageW(infoPtr->hwndHeader, WM_SETFONT, (WPARAM)hFont, MAKELPARAM(fRedraw, 0));
10845 LISTVIEW_UpdateSize(infoPtr);
10846 LISTVIEW_UpdateScroll(infoPtr);
10848 else if (infoPtr->nItemHeight != oldHeight)
10849 LISTVIEW_UpdateScroll(infoPtr);
10851 if (fRedraw) LISTVIEW_InvalidateList(infoPtr);
10858 * Message handling for WM_SETREDRAW.
10859 * For the Listview, it invalidates the entire window (the doc specifies otherwise)
10862 * [I] infoPtr : valid pointer to the listview structure
10863 * [I] bRedraw: state of redraw flag
10866 * DefWinProc return value
10868 static LRESULT LISTVIEW_SetRedraw(LISTVIEW_INFO *infoPtr, BOOL bRedraw)
10870 TRACE("infoPtr->bRedraw=%d, bRedraw=%d\n", infoPtr->bRedraw, bRedraw);
10872 /* we cannot use straight equality here because _any_ non-zero value is TRUE */
10873 if ((infoPtr->bRedraw && bRedraw) || (!infoPtr->bRedraw && !bRedraw)) return 0;
10875 infoPtr->bRedraw = bRedraw;
10877 if(!bRedraw) return 0;
10879 if (is_autoarrange(infoPtr))
10880 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
10881 LISTVIEW_UpdateScroll(infoPtr);
10883 /* despite what the WM_SETREDRAW docs says, apps expect us
10884 * to invalidate the listview here... stupid! */
10885 LISTVIEW_InvalidateList(infoPtr);
10892 * Resizes the listview control. This function processes WM_SIZE
10893 * messages. At this time, the width and height are not used.
10896 * [I] infoPtr : valid pointer to the listview structure
10897 * [I] Width : new width
10898 * [I] Height : new height
10903 static LRESULT LISTVIEW_Size(LISTVIEW_INFO *infoPtr, int Width, int Height)
10905 RECT rcOld = infoPtr->rcList;
10907 TRACE("(width=%d, height=%d)\n", Width, Height);
10909 LISTVIEW_UpdateSize(infoPtr);
10910 if (EqualRect(&rcOld, &infoPtr->rcList)) return 0;
10912 /* do not bother with display related stuff if we're not redrawing */
10913 if (!is_redrawing(infoPtr)) return 0;
10915 if (is_autoarrange(infoPtr))
10916 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
10918 LISTVIEW_UpdateScroll(infoPtr);
10920 /* refresh all only for lists whose height changed significantly */
10921 if ((infoPtr->uView == LV_VIEW_LIST) &&
10922 (rcOld.bottom - rcOld.top) / infoPtr->nItemHeight !=
10923 (infoPtr->rcList.bottom - infoPtr->rcList.top) / infoPtr->nItemHeight)
10924 LISTVIEW_InvalidateList(infoPtr);
10931 * Sets the size information.
10934 * [I] infoPtr : valid pointer to the listview structure
10939 static void LISTVIEW_UpdateSize(LISTVIEW_INFO *infoPtr)
10941 TRACE("uView=%d, rcList(old)=%s\n", infoPtr->uView, wine_dbgstr_rect(&infoPtr->rcList));
10943 GetClientRect(infoPtr->hwndSelf, &infoPtr->rcList);
10945 if (infoPtr->uView == LV_VIEW_LIST)
10947 /* Apparently the "LIST" style is supposed to have the same
10948 * number of items in a column even if there is no scroll bar.
10949 * Since if a scroll bar already exists then the bottom is already
10950 * reduced, only reduce if the scroll bar does not currently exist.
10951 * The "2" is there to mimic the native control. I think it may be
10952 * related to either padding or edges. (GLA 7/2002)
10954 if (!(GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE) & WS_HSCROLL))
10955 infoPtr->rcList.bottom -= GetSystemMetrics(SM_CYHSCROLL);
10956 infoPtr->rcList.bottom = max (infoPtr->rcList.bottom - 2, 0);
10959 /* if control created invisible header isn't created */
10960 if (infoPtr->hwndHeader)
10965 hl.prc = &infoPtr->rcList;
10967 SendMessageW( infoPtr->hwndHeader, HDM_LAYOUT, 0, (LPARAM)&hl );
10968 TRACE(" wp.flags=0x%08x, wp=%d,%d (%dx%d)\n", wp.flags, wp.x, wp.y, wp.cx, wp.cy);
10970 if (LISTVIEW_IsHeaderEnabled(infoPtr))
10971 wp.flags |= SWP_SHOWWINDOW;
10974 wp.flags |= SWP_HIDEWINDOW;
10978 SetWindowPos(wp.hwnd, wp.hwndInsertAfter, wp.x, wp.y, wp.cx, wp.cy, wp.flags);
10979 TRACE(" after SWP wp=%d,%d (%dx%d)\n", wp.x, wp.y, wp.cx, wp.cy);
10981 infoPtr->rcList.top = max(wp.cy, 0);
10983 /* extra padding for grid */
10984 if (infoPtr->uView == LV_VIEW_DETAILS && infoPtr->dwLvExStyle & LVS_EX_GRIDLINES)
10985 infoPtr->rcList.top += 2;
10987 TRACE(" rcList=%s\n", wine_dbgstr_rect(&infoPtr->rcList));
10992 * Processes WM_STYLECHANGED messages.
10995 * [I] infoPtr : valid pointer to the listview structure
10996 * [I] wStyleType : window style type (normal or extended)
10997 * [I] lpss : window style information
11002 static INT LISTVIEW_StyleChanged(LISTVIEW_INFO *infoPtr, WPARAM wStyleType,
11003 const STYLESTRUCT *lpss)
11005 UINT uNewView = lpss->styleNew & LVS_TYPEMASK;
11006 UINT uOldView = lpss->styleOld & LVS_TYPEMASK;
11009 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
11010 wStyleType, lpss->styleOld, lpss->styleNew);
11012 if (wStyleType != GWL_STYLE) return 0;
11014 infoPtr->dwStyle = lpss->styleNew;
11015 map_style_view(infoPtr);
11017 if (((lpss->styleOld & WS_HSCROLL) != 0)&&
11018 ((lpss->styleNew & WS_HSCROLL) == 0))
11019 ShowScrollBar(infoPtr->hwndSelf, SB_HORZ, FALSE);
11021 if (((lpss->styleOld & WS_VSCROLL) != 0)&&
11022 ((lpss->styleNew & WS_VSCROLL) == 0))
11023 ShowScrollBar(infoPtr->hwndSelf, SB_VERT, FALSE);
11025 if (uNewView != uOldView)
11029 SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
11030 ShowWindow(infoPtr->hwndHeader, SW_HIDE);
11032 ShowScrollBar(infoPtr->hwndSelf, SB_BOTH, FALSE);
11033 SetRectEmpty(&infoPtr->rcFocus);
11035 himl = (uNewView == LVS_ICON ? infoPtr->himlNormal : infoPtr->himlSmall);
11036 set_icon_size(&infoPtr->iconSize, himl, uNewView != LVS_ICON);
11038 if (uNewView == LVS_REPORT)
11043 LISTVIEW_CreateHeader( infoPtr );
11045 hl.prc = &infoPtr->rcList;
11047 SendMessageW( infoPtr->hwndHeader, HDM_LAYOUT, 0, (LPARAM)&hl );
11048 SetWindowPos(infoPtr->hwndHeader, infoPtr->hwndSelf, wp.x, wp.y, wp.cx, wp.cy,
11049 wp.flags | ((infoPtr->dwStyle & LVS_NOCOLUMNHEADER)
11050 ? SWP_HIDEWINDOW : SWP_SHOWWINDOW));
11053 LISTVIEW_UpdateItemSize(infoPtr);
11056 if (uNewView == LVS_REPORT || infoPtr->dwLvExStyle & LVS_EX_HEADERINALLVIEWS)
11058 if ((lpss->styleOld ^ lpss->styleNew) & LVS_NOCOLUMNHEADER)
11060 if (lpss->styleNew & LVS_NOCOLUMNHEADER)
11062 /* Turn off the header control */
11063 style = GetWindowLongW(infoPtr->hwndHeader, GWL_STYLE);
11064 TRACE("Hide header control, was 0x%08x\n", style);
11065 SetWindowLongW(infoPtr->hwndHeader, GWL_STYLE, style | HDS_HIDDEN);
11067 /* Turn on the header control */
11068 if ((style = GetWindowLongW(infoPtr->hwndHeader, GWL_STYLE)) & HDS_HIDDEN)
11070 TRACE("Show header control, was 0x%08x\n", style);
11071 SetWindowLongW(infoPtr->hwndHeader, GWL_STYLE, (style & ~HDS_HIDDEN) | WS_VISIBLE);
11077 if ( (uNewView == LVS_ICON || uNewView == LVS_SMALLICON) &&
11078 (uNewView != uOldView || ((lpss->styleNew ^ lpss->styleOld) & LVS_ALIGNMASK)) )
11079 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
11081 /* update the size of the client area */
11082 LISTVIEW_UpdateSize(infoPtr);
11084 /* add scrollbars if needed */
11085 LISTVIEW_UpdateScroll(infoPtr);
11087 /* invalidate client area + erase background */
11088 LISTVIEW_InvalidateList(infoPtr);
11095 * Processes WM_STYLECHANGING messages.
11098 * [I] wStyleType : window style type (normal or extended)
11099 * [I0] lpss : window style information
11104 static INT LISTVIEW_StyleChanging(WPARAM wStyleType,
11107 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
11108 wStyleType, lpss->styleOld, lpss->styleNew);
11110 /* don't forward LVS_OWNERDATA only if not already set to */
11111 if ((lpss->styleNew ^ lpss->styleOld) & LVS_OWNERDATA)
11113 if (lpss->styleOld & LVS_OWNERDATA)
11114 lpss->styleNew |= LVS_OWNERDATA;
11116 lpss->styleNew &= ~LVS_OWNERDATA;
11124 * Processes WM_SHOWWINDOW messages.
11127 * [I] infoPtr : valid pointer to the listview structure
11128 * [I] bShown : window is being shown (FALSE when hidden)
11129 * [I] iStatus : window show status
11134 static LRESULT LISTVIEW_ShowWindow(LISTVIEW_INFO *infoPtr, WPARAM bShown, LPARAM iStatus)
11136 /* header delayed creation */
11137 if ((infoPtr->uView == LV_VIEW_DETAILS) && bShown)
11139 LISTVIEW_CreateHeader(infoPtr);
11141 if (!(LVS_NOCOLUMNHEADER & infoPtr->dwStyle))
11142 ShowWindow(infoPtr->hwndHeader, SW_SHOWNORMAL);
11145 return DefWindowProcW(infoPtr->hwndSelf, WM_SHOWWINDOW, bShown, iStatus);
11150 * Processes CCM_GETVERSION messages.
11153 * [I] infoPtr : valid pointer to the listview structure
11158 static inline LRESULT LISTVIEW_GetVersion(const LISTVIEW_INFO *infoPtr)
11160 return infoPtr->iVersion;
11165 * Processes CCM_SETVERSION messages.
11168 * [I] infoPtr : valid pointer to the listview structure
11169 * [I] iVersion : version to be set
11172 * -1 when requested version is greater than DLL version;
11173 * previous version otherwise
11175 static LRESULT LISTVIEW_SetVersion(LISTVIEW_INFO *infoPtr, DWORD iVersion)
11177 INT iOldVersion = infoPtr->iVersion;
11179 if (iVersion > COMCTL32_VERSION)
11182 infoPtr->iVersion = iVersion;
11184 TRACE("new version %d\n", iVersion);
11186 return iOldVersion;
11191 * Window procedure of the listview control.
11194 static LRESULT WINAPI
11195 LISTVIEW_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
11197 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongPtrW(hwnd, 0);
11199 TRACE("(hwnd=%p uMsg=%x wParam=%lx lParam=%lx)\n", hwnd, uMsg, wParam, lParam);
11201 if (!infoPtr && (uMsg != WM_NCCREATE))
11202 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
11206 case LVM_APPROXIMATEVIEWRECT:
11207 return LISTVIEW_ApproximateViewRect(infoPtr, (INT)wParam,
11208 LOWORD(lParam), HIWORD(lParam));
11210 return LISTVIEW_Arrange(infoPtr, (INT)wParam);
11212 case LVM_CANCELEDITLABEL:
11213 return LISTVIEW_CancelEditLabel(infoPtr);
11215 case LVM_CREATEDRAGIMAGE:
11216 return (LRESULT)LISTVIEW_CreateDragImage(infoPtr, (INT)wParam, (LPPOINT)lParam);
11218 case LVM_DELETEALLITEMS:
11219 return LISTVIEW_DeleteAllItems(infoPtr, FALSE);
11221 case LVM_DELETECOLUMN:
11222 return LISTVIEW_DeleteColumn(infoPtr, (INT)wParam);
11224 case LVM_DELETEITEM:
11225 return LISTVIEW_DeleteItem(infoPtr, (INT)wParam);
11227 case LVM_EDITLABELA:
11228 case LVM_EDITLABELW:
11229 return (LRESULT)LISTVIEW_EditLabelT(infoPtr, (INT)wParam,
11230 uMsg == LVM_EDITLABELW);
11231 /* case LVM_ENABLEGROUPVIEW: */
11233 case LVM_ENSUREVISIBLE:
11234 return LISTVIEW_EnsureVisible(infoPtr, (INT)wParam, (BOOL)lParam);
11236 case LVM_FINDITEMW:
11237 return LISTVIEW_FindItemW(infoPtr, (INT)wParam, (LPLVFINDINFOW)lParam);
11239 case LVM_FINDITEMA:
11240 return LISTVIEW_FindItemA(infoPtr, (INT)wParam, (LPLVFINDINFOA)lParam);
11242 case LVM_GETBKCOLOR:
11243 return infoPtr->clrBk;
11245 /* case LVM_GETBKIMAGE: */
11247 case LVM_GETCALLBACKMASK:
11248 return infoPtr->uCallbackMask;
11250 case LVM_GETCOLUMNA:
11251 case LVM_GETCOLUMNW:
11252 return LISTVIEW_GetColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam,
11253 uMsg == LVM_GETCOLUMNW);
11255 case LVM_GETCOLUMNORDERARRAY:
11256 return LISTVIEW_GetColumnOrderArray(infoPtr, (INT)wParam, (LPINT)lParam);
11258 case LVM_GETCOLUMNWIDTH:
11259 return LISTVIEW_GetColumnWidth(infoPtr, (INT)wParam);
11261 case LVM_GETCOUNTPERPAGE:
11262 return LISTVIEW_GetCountPerPage(infoPtr);
11264 case LVM_GETEDITCONTROL:
11265 return (LRESULT)infoPtr->hwndEdit;
11267 case LVM_GETEXTENDEDLISTVIEWSTYLE:
11268 return infoPtr->dwLvExStyle;
11270 /* case LVM_GETGROUPINFO: */
11272 /* case LVM_GETGROUPMETRICS: */
11274 case LVM_GETHEADER:
11275 return (LRESULT)infoPtr->hwndHeader;
11277 case LVM_GETHOTCURSOR:
11278 return (LRESULT)infoPtr->hHotCursor;
11280 case LVM_GETHOTITEM:
11281 return infoPtr->nHotItem;
11283 case LVM_GETHOVERTIME:
11284 return infoPtr->dwHoverTime;
11286 case LVM_GETIMAGELIST:
11287 return (LRESULT)LISTVIEW_GetImageList(infoPtr, (INT)wParam);
11289 /* case LVM_GETINSERTMARK: */
11291 /* case LVM_GETINSERTMARKCOLOR: */
11293 /* case LVM_GETINSERTMARKRECT: */
11295 case LVM_GETISEARCHSTRINGA:
11296 case LVM_GETISEARCHSTRINGW:
11297 FIXME("LVM_GETISEARCHSTRING: unimplemented\n");
11302 return LISTVIEW_GetItemExtT(infoPtr, (LPLVITEMW)lParam, uMsg == LVM_GETITEMW);
11304 case LVM_GETITEMCOUNT:
11305 return infoPtr->nItemCount;
11307 case LVM_GETITEMPOSITION:
11308 return LISTVIEW_GetItemPosition(infoPtr, (INT)wParam, (LPPOINT)lParam);
11310 case LVM_GETITEMRECT:
11311 return LISTVIEW_GetItemRect(infoPtr, (INT)wParam, (LPRECT)lParam);
11313 case LVM_GETITEMSPACING:
11314 return LISTVIEW_GetItemSpacing(infoPtr, (BOOL)wParam);
11316 case LVM_GETITEMSTATE:
11317 return LISTVIEW_GetItemState(infoPtr, (INT)wParam, (UINT)lParam);
11319 case LVM_GETITEMTEXTA:
11320 case LVM_GETITEMTEXTW:
11321 return LISTVIEW_GetItemTextT(infoPtr, (INT)wParam, (LPLVITEMW)lParam,
11322 uMsg == LVM_GETITEMTEXTW);
11324 case LVM_GETNEXTITEM:
11325 return LISTVIEW_GetNextItem(infoPtr, (INT)wParam, LOWORD(lParam));
11327 case LVM_GETNUMBEROFWORKAREAS:
11328 FIXME("LVM_GETNUMBEROFWORKAREAS: unimplemented\n");
11331 case LVM_GETORIGIN:
11332 if (!lParam) return FALSE;
11333 if (infoPtr->uView == LV_VIEW_DETAILS ||
11334 infoPtr->uView == LV_VIEW_LIST) return FALSE;
11335 LISTVIEW_GetOrigin(infoPtr, (LPPOINT)lParam);
11338 /* case LVM_GETOUTLINECOLOR: */
11340 /* case LVM_GETSELECTEDCOLUMN: */
11342 case LVM_GETSELECTEDCOUNT:
11343 return LISTVIEW_GetSelectedCount(infoPtr);
11345 case LVM_GETSELECTIONMARK:
11346 return infoPtr->nSelectionMark;
11348 case LVM_GETSTRINGWIDTHA:
11349 case LVM_GETSTRINGWIDTHW:
11350 return LISTVIEW_GetStringWidthT(infoPtr, (LPCWSTR)lParam,
11351 uMsg == LVM_GETSTRINGWIDTHW);
11353 case LVM_GETSUBITEMRECT:
11354 return LISTVIEW_GetSubItemRect(infoPtr, (UINT)wParam, (LPRECT)lParam);
11356 case LVM_GETTEXTBKCOLOR:
11357 return infoPtr->clrTextBk;
11359 case LVM_GETTEXTCOLOR:
11360 return infoPtr->clrText;
11362 /* case LVM_GETTILEINFO: */
11364 /* case LVM_GETTILEVIEWINFO: */
11366 case LVM_GETTOOLTIPS:
11367 if( !infoPtr->hwndToolTip )
11368 infoPtr->hwndToolTip = COMCTL32_CreateToolTip( hwnd );
11369 return (LRESULT)infoPtr->hwndToolTip;
11371 case LVM_GETTOPINDEX:
11372 return LISTVIEW_GetTopIndex(infoPtr);
11374 case LVM_GETUNICODEFORMAT:
11375 return (infoPtr->notifyFormat == NFR_UNICODE);
11378 return infoPtr->uView;
11380 case LVM_GETVIEWRECT:
11381 return LISTVIEW_GetViewRect(infoPtr, (LPRECT)lParam);
11383 case LVM_GETWORKAREAS:
11384 FIXME("LVM_GETWORKAREAS: unimplemented\n");
11387 /* case LVM_HASGROUP: */
11390 return LISTVIEW_HitTest(infoPtr, (LPLVHITTESTINFO)lParam, FALSE, TRUE);
11392 case LVM_INSERTCOLUMNA:
11393 case LVM_INSERTCOLUMNW:
11394 return LISTVIEW_InsertColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam,
11395 uMsg == LVM_INSERTCOLUMNW);
11397 /* case LVM_INSERTGROUP: */
11399 /* case LVM_INSERTGROUPSORTED: */
11401 case LVM_INSERTITEMA:
11402 case LVM_INSERTITEMW:
11403 return LISTVIEW_InsertItemT(infoPtr, (LPLVITEMW)lParam, uMsg == LVM_INSERTITEMW);
11405 /* case LVM_INSERTMARKHITTEST: */
11407 /* case LVM_ISGROUPVIEWENABLED: */
11409 case LVM_ISITEMVISIBLE:
11410 return LISTVIEW_IsItemVisible(infoPtr, (INT)wParam);
11412 case LVM_MAPIDTOINDEX:
11413 return LISTVIEW_MapIdToIndex(infoPtr, (UINT)wParam);
11415 case LVM_MAPINDEXTOID:
11416 return LISTVIEW_MapIndexToId(infoPtr, (INT)wParam);
11418 /* case LVM_MOVEGROUP: */
11420 /* case LVM_MOVEITEMTOGROUP: */
11422 case LVM_REDRAWITEMS:
11423 return LISTVIEW_RedrawItems(infoPtr, (INT)wParam, (INT)lParam);
11425 /* case LVM_REMOVEALLGROUPS: */
11427 /* case LVM_REMOVEGROUP: */
11430 return LISTVIEW_Scroll(infoPtr, (INT)wParam, (INT)lParam);
11432 case LVM_SETBKCOLOR:
11433 return LISTVIEW_SetBkColor(infoPtr, (COLORREF)lParam);
11435 /* case LVM_SETBKIMAGE: */
11437 case LVM_SETCALLBACKMASK:
11438 infoPtr->uCallbackMask = (UINT)wParam;
11441 case LVM_SETCOLUMNA:
11442 case LVM_SETCOLUMNW:
11443 return LISTVIEW_SetColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam,
11444 uMsg == LVM_SETCOLUMNW);
11446 case LVM_SETCOLUMNORDERARRAY:
11447 return LISTVIEW_SetColumnOrderArray(infoPtr, (INT)wParam, (LPINT)lParam);
11449 case LVM_SETCOLUMNWIDTH:
11450 return LISTVIEW_SetColumnWidth(infoPtr, (INT)wParam, (short)LOWORD(lParam));
11452 case LVM_SETEXTENDEDLISTVIEWSTYLE:
11453 return LISTVIEW_SetExtendedListViewStyle(infoPtr, (DWORD)wParam, (DWORD)lParam);
11455 /* case LVM_SETGROUPINFO: */
11457 /* case LVM_SETGROUPMETRICS: */
11459 case LVM_SETHOTCURSOR:
11460 return (LRESULT)LISTVIEW_SetHotCursor(infoPtr, (HCURSOR)lParam);
11462 case LVM_SETHOTITEM:
11463 return LISTVIEW_SetHotItem(infoPtr, (INT)wParam);
11465 case LVM_SETHOVERTIME:
11466 return LISTVIEW_SetHoverTime(infoPtr, (DWORD)lParam);
11468 case LVM_SETICONSPACING:
11469 return LISTVIEW_SetIconSpacing(infoPtr, (short)LOWORD(lParam), (short)HIWORD(lParam));
11471 case LVM_SETIMAGELIST:
11472 return (LRESULT)LISTVIEW_SetImageList(infoPtr, (INT)wParam, (HIMAGELIST)lParam);
11474 /* case LVM_SETINFOTIP: */
11476 /* case LVM_SETINSERTMARK: */
11478 /* case LVM_SETINSERTMARKCOLOR: */
11483 if (infoPtr->dwStyle & LVS_OWNERDATA) return FALSE;
11484 return LISTVIEW_SetItemT(infoPtr, (LPLVITEMW)lParam, (uMsg == LVM_SETITEMW));
11487 case LVM_SETITEMCOUNT:
11488 return LISTVIEW_SetItemCount(infoPtr, (INT)wParam, (DWORD)lParam);
11490 case LVM_SETITEMPOSITION:
11493 pt.x = (short)LOWORD(lParam);
11494 pt.y = (short)HIWORD(lParam);
11495 return LISTVIEW_SetItemPosition(infoPtr, (INT)wParam, &pt);
11498 case LVM_SETITEMPOSITION32:
11499 return LISTVIEW_SetItemPosition(infoPtr, (INT)wParam, (POINT*)lParam);
11501 case LVM_SETITEMSTATE:
11502 return LISTVIEW_SetItemState(infoPtr, (INT)wParam, (LPLVITEMW)lParam);
11504 case LVM_SETITEMTEXTA:
11505 case LVM_SETITEMTEXTW:
11506 return LISTVIEW_SetItemTextT(infoPtr, (INT)wParam, (LPLVITEMW)lParam,
11507 uMsg == LVM_SETITEMTEXTW);
11509 /* case LVM_SETOUTLINECOLOR: */
11511 /* case LVM_SETSELECTEDCOLUMN: */
11513 case LVM_SETSELECTIONMARK:
11514 return LISTVIEW_SetSelectionMark(infoPtr, (INT)lParam);
11516 case LVM_SETTEXTBKCOLOR:
11517 return LISTVIEW_SetTextBkColor(infoPtr, (COLORREF)lParam);
11519 case LVM_SETTEXTCOLOR:
11520 return LISTVIEW_SetTextColor(infoPtr, (COLORREF)lParam);
11522 /* case LVM_SETTILEINFO: */
11524 /* case LVM_SETTILEVIEWINFO: */
11526 /* case LVM_SETTILEWIDTH: */
11528 case LVM_SETTOOLTIPS:
11529 return (LRESULT)LISTVIEW_SetToolTips(infoPtr, (HWND)lParam);
11531 case LVM_SETUNICODEFORMAT:
11532 return LISTVIEW_SetUnicodeFormat(infoPtr, wParam);
11535 return LISTVIEW_SetView(infoPtr, wParam);
11537 /* case LVM_SETWORKAREAS: */
11539 /* case LVM_SORTGROUPS: */
11541 case LVM_SORTITEMS:
11542 case LVM_SORTITEMSEX:
11543 return LISTVIEW_SortItems(infoPtr, (PFNLVCOMPARE)lParam, wParam,
11544 uMsg == LVM_SORTITEMSEX);
11545 case LVM_SUBITEMHITTEST:
11546 return LISTVIEW_HitTest(infoPtr, (LPLVHITTESTINFO)lParam, TRUE, FALSE);
11549 return LISTVIEW_Update(infoPtr, (INT)wParam);
11551 case CCM_GETVERSION:
11552 return LISTVIEW_GetVersion(infoPtr);
11554 case CCM_SETVERSION:
11555 return LISTVIEW_SetVersion(infoPtr, wParam);
11558 return LISTVIEW_ProcessLetterKeys( infoPtr, wParam, lParam );
11561 return LISTVIEW_Command(infoPtr, wParam, lParam);
11564 return LISTVIEW_NCCreate(hwnd, (LPCREATESTRUCTW)lParam);
11567 return LISTVIEW_Create(hwnd, (LPCREATESTRUCTW)lParam);
11570 return LISTVIEW_Destroy(infoPtr);
11573 return LISTVIEW_Enable(infoPtr);
11575 case WM_ERASEBKGND:
11576 return LISTVIEW_EraseBkgnd(infoPtr, (HDC)wParam);
11578 case WM_GETDLGCODE:
11579 return DLGC_WANTCHARS | DLGC_WANTARROWS;
11582 return (LRESULT)infoPtr->hFont;
11585 return LISTVIEW_HScroll(infoPtr, (INT)LOWORD(wParam), 0);
11588 return LISTVIEW_KeyDown(infoPtr, (INT)wParam, (LONG)lParam);
11591 return LISTVIEW_KillFocus(infoPtr);
11593 case WM_LBUTTONDBLCLK:
11594 return LISTVIEW_LButtonDblClk(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11596 case WM_LBUTTONDOWN:
11597 return LISTVIEW_LButtonDown(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11600 return LISTVIEW_LButtonUp(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11603 return LISTVIEW_MouseMove (infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11605 case WM_MOUSEHOVER:
11606 return LISTVIEW_MouseHover(infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11609 return LISTVIEW_NCDestroy(infoPtr);
11612 return LISTVIEW_NCPaint(infoPtr, (HRGN)wParam);
11615 return LISTVIEW_Notify(infoPtr, (LPNMHDR)lParam);
11617 case WM_NOTIFYFORMAT:
11618 return LISTVIEW_NotifyFormat(infoPtr, (HWND)wParam, (INT)lParam);
11620 case WM_PRINTCLIENT:
11621 return LISTVIEW_PrintClient(infoPtr, (HDC)wParam, (DWORD)lParam);
11624 return LISTVIEW_WMPaint(infoPtr, (HDC)wParam);
11626 case WM_RBUTTONDBLCLK:
11627 return LISTVIEW_RButtonDblClk(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11629 case WM_RBUTTONDOWN:
11630 return LISTVIEW_RButtonDown(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11633 return LISTVIEW_SetCursor(infoPtr, wParam, lParam);
11636 return LISTVIEW_SetFocus(infoPtr, (HWND)wParam);
11639 return LISTVIEW_SetFont(infoPtr, (HFONT)wParam, (WORD)lParam);
11642 return LISTVIEW_SetRedraw(infoPtr, (BOOL)wParam);
11644 case WM_SHOWWINDOW:
11645 return LISTVIEW_ShowWindow(infoPtr, wParam, lParam);
11647 case WM_STYLECHANGED:
11648 return LISTVIEW_StyleChanged(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
11650 case WM_STYLECHANGING:
11651 return LISTVIEW_StyleChanging(wParam, (LPSTYLESTRUCT)lParam);
11653 case WM_SYSCOLORCHANGE:
11654 COMCTL32_RefreshSysColors();
11657 /* case WM_TIMER: */
11658 case WM_THEMECHANGED:
11659 return LISTVIEW_ThemeChanged(infoPtr);
11662 return LISTVIEW_VScroll(infoPtr, (INT)LOWORD(wParam), 0);
11664 case WM_MOUSEWHEEL:
11665 if (wParam & (MK_SHIFT | MK_CONTROL))
11666 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
11667 return LISTVIEW_MouseWheel(infoPtr, (short int)HIWORD(wParam));
11669 case WM_WINDOWPOSCHANGED:
11670 if (!(((WINDOWPOS *)lParam)->flags & SWP_NOSIZE))
11672 SetWindowPos(infoPtr->hwndSelf, 0, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOACTIVATE |
11673 SWP_NOZORDER | SWP_NOMOVE | SWP_NOSIZE);
11675 if ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (infoPtr->uView == LV_VIEW_DETAILS))
11677 if (notify_measureitem(infoPtr)) LISTVIEW_InvalidateList(infoPtr);
11679 LISTVIEW_Size(infoPtr, ((WINDOWPOS *)lParam)->cx, ((WINDOWPOS *)lParam)->cy);
11681 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
11683 /* case WM_WININICHANGE: */
11686 if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
11687 ERR("unknown msg %04x wp=%08lx lp=%08lx\n", uMsg, wParam, lParam);
11689 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
11696 * Registers the window class.
11704 void LISTVIEW_Register(void)
11706 WNDCLASSW wndClass;
11708 ZeroMemory(&wndClass, sizeof(WNDCLASSW));
11709 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS;
11710 wndClass.lpfnWndProc = LISTVIEW_WindowProc;
11711 wndClass.cbClsExtra = 0;
11712 wndClass.cbWndExtra = sizeof(LISTVIEW_INFO *);
11713 wndClass.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
11714 wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
11715 wndClass.lpszClassName = WC_LISTVIEWW;
11716 RegisterClassW(&wndClass);
11721 * Unregisters the window class.
11729 void LISTVIEW_Unregister(void)
11731 UnregisterClassW(WC_LISTVIEWW, NULL);
11736 * Handle any WM_COMMAND messages
11739 * [I] infoPtr : valid pointer to the listview structure
11740 * [I] wParam : the first message parameter
11741 * [I] lParam : the second message parameter
11746 static LRESULT LISTVIEW_Command(LISTVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
11749 TRACE("(%p %x %x %lx)\n", infoPtr, HIWORD(wParam), LOWORD(wParam), lParam);
11751 if (!infoPtr->hwndEdit) return 0;
11753 switch (HIWORD(wParam))
11758 * Adjust the edit window size
11760 WCHAR buffer[1024];
11761 HDC hdc = GetDC(infoPtr->hwndEdit);
11762 HFONT hFont, hOldFont = 0;
11766 if (!infoPtr->hwndEdit || !hdc) return 0;
11767 GetWindowTextW(infoPtr->hwndEdit, buffer, sizeof(buffer)/sizeof(buffer[0]));
11768 GetWindowRect(infoPtr->hwndEdit, &rect);
11770 /* Select font to get the right dimension of the string */
11771 hFont = (HFONT)SendMessageW(infoPtr->hwndEdit, WM_GETFONT, 0, 0);
11774 hOldFont = SelectObject(hdc, hFont);
11777 if (GetTextExtentPoint32W(hdc, buffer, lstrlenW(buffer), &sz))
11779 TEXTMETRICW textMetric;
11781 /* Add Extra spacing for the next character */
11782 GetTextMetricsW(hdc, &textMetric);
11783 sz.cx += (textMetric.tmMaxCharWidth * 2);
11785 SetWindowPos(infoPtr->hwndEdit, NULL, 0, 0, sz.cx,
11786 rect.bottom - rect.top, SWP_DRAWFRAME | SWP_NOMOVE | SWP_NOZORDER);
11789 SelectObject(hdc, hOldFont);
11791 ReleaseDC(infoPtr->hwndEdit, hdc);
11797 LISTVIEW_CancelEditLabel(infoPtr);
11802 return SendMessageW (infoPtr->hwndNotify, WM_COMMAND, wParam, lParam);