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-2011 Nikolay Sivov
10 * Copyright 2009 Owen Rudge for CodeWeavers
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; either
15 * version 2.1 of the License, or (at your option) any later version.
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
28 * This code was audited for completeness against the documented features
29 * of Comctl32.dll version 6.0 on May. 20, 2005, by James Hawkins.
31 * Unless otherwise noted, we believe this code to be complete, as per
32 * the specification mentioned above.
33 * If you discover missing features, or bugs, please note them below.
37 * Default Message Processing
38 * -- WM_CREATE: create the icon and small icon image lists at this point only if
39 * the LVS_SHAREIMAGELISTS style is not specified.
40 * -- WM_WINDOWPOSCHANGED: arrange the list items if the current view is icon
41 * or small icon and the LVS_AUTOARRANGE style is specified.
46 * -- Hot item handling, mouse hovering
47 * -- Workareas support
52 * -- Expand large item in ICON mode when the cursor is flying over the icon or text.
53 * -- Support CustomDraw options for _WIN32_IE >= 0x560 (see NMLVCUSTOMDRAW docs).
54 * -- LVA_SNAPTOGRID not implemented
55 * -- LISTVIEW_ApproximateViewRect partially implemented
56 * -- LISTVIEW_SetColumnWidth ignores header images & bitmap
57 * -- LISTVIEW_SetIconSpacing is incomplete
58 * -- LISTVIEW_StyleChanged doesn't handle some changes too well
61 * -- LISTVIEW_GetNextItem needs to be rewritten. It is currently
62 * linear in the number of items in the list, and this is
63 * unacceptable for large lists.
64 * -- if list is sorted by item text LISTVIEW_InsertItemT could use
65 * binary search to calculate item index (e.g. DPA_Search()).
66 * This requires sorted state to be reliably tracked in item modifiers.
67 * -- we should keep an ordered array of coordinates in iconic mode
68 * this would allow to frame items (iterator_frameditems),
69 * and find nearest item (LVFI_NEARESTXY) a lot more efficiently
76 * -- LVIS_ACTIVATING (not currently supported by comctl32.dll version 6.0)
82 * -- LVS_NOSCROLL (see Q137520)
86 * -- LVS_EX_BORDERSELECT
90 * -- LVS_EX_MULTIWORKAREAS
92 * -- LVS_EX_SIMPLESELECT
93 * -- LVS_EX_TWOCLICKACTIVATE
94 * -- LVS_EX_UNDERLINECOLD
95 * -- LVS_EX_UNDERLINEHOT
98 * -- LVN_BEGINSCROLL, LVN_ENDSCROLL
105 * -- LVM_ENABLEGROUPVIEW
106 * -- LVM_GETBKIMAGE, LVM_SETBKIMAGE
107 * -- LVM_GETGROUPINFO, LVM_SETGROUPINFO
108 * -- LVM_GETGROUPMETRICS, LVM_SETGROUPMETRICS
109 * -- LVM_GETINSERTMARK, LVM_SETINSERTMARK
110 * -- LVM_GETINSERTMARKCOLOR, LVM_SETINSERTMARKCOLOR
111 * -- LVM_GETINSERTMARKRECT
112 * -- LVM_GETNUMBEROFWORKAREAS
113 * -- LVM_GETOUTLINECOLOR, LVM_SETOUTLINECOLOR
114 * -- LVM_GETSELECTEDCOLUMN, LVM_SETSELECTEDCOLUMN
115 * -- LVM_GETISEARCHSTRINGW, LVM_GETISEARCHSTRINGA
116 * -- LVM_GETTILEINFO, LVM_SETTILEINFO
117 * -- LVM_GETTILEVIEWINFO, LVM_SETTILEVIEWINFO
118 * -- LVM_GETWORKAREAS, LVM_SETWORKAREAS
119 * -- LVM_HASGROUP, LVM_INSERTGROUP, LVM_REMOVEGROUP, LVM_REMOVEALLGROUPS
120 * -- LVM_INSERTGROUPSORTED
121 * -- LVM_INSERTMARKHITTEST
122 * -- LVM_ISGROUPVIEWENABLED
124 * -- LVM_MOVEITEMTOGROUP
126 * -- LVM_SETTILEWIDTH
130 * -- ListView_GetHoverTime, ListView_SetHoverTime
131 * -- ListView_GetISearchString
132 * -- ListView_GetNumberOfWorkAreas
133 * -- ListView_GetWorkAreas, ListView_SetWorkAreas
138 * Known differences in message stream from native control (not known if
139 * these differences cause problems):
140 * LVM_INSERTITEM issues LVM_SETITEMSTATE and LVM_SETITEM in certain cases.
141 * LVM_SETITEM does not always issue LVN_ITEMCHANGING/LVN_ITEMCHANGED.
142 * WM_CREATE does not issue WM_QUERYUISTATE and associated registry
143 * processing for "USEDOUBLECLICKTIME".
147 #include "wine/port.h"
162 #include "commctrl.h"
163 #include "comctl32.h"
166 #include "wine/debug.h"
167 #include "wine/unicode.h"
169 WINE_DEFAULT_DEBUG_CHANNEL(listview);
171 typedef struct tagCOLUMN_INFO
173 RECT rcHeader; /* tracks the header's rectangle */
174 INT fmt; /* same as LVCOLUMN.fmt */
178 typedef struct tagITEMHDR
182 } ITEMHDR, *LPITEMHDR;
184 typedef struct tagSUBITEM_INFO
190 typedef struct tagITEM_ID ITEM_ID;
192 typedef struct tagITEM_INFO
203 UINT id; /* item id */
204 HDPA item; /* link to item data */
207 typedef struct tagRANGE
213 typedef struct tagRANGES
218 typedef struct tagITERATOR
227 typedef struct tagDELAYED_ITEM_EDIT
233 typedef struct tagLISTVIEW_INFO
237 RECT rcList; /* This rectangle is really the window
238 * client rectangle possibly reduced by the
239 * horizontal scroll bar and/or header - see
240 * LISTVIEW_UpdateSize. This rectangle offset
241 * by the LISTVIEW_GetOrigin value is in
242 * client coordinates */
244 /* notification window */
247 BOOL bDoChangeNotify; /* send change notification messages? */
254 INT nItemCount; /* the number of items in the list */
255 HDPA hdpaItems; /* array ITEM_INFO pointers */
256 HDPA hdpaItemIds; /* array of ITEM_ID pointers */
257 HDPA hdpaPosX; /* maintains the (X, Y) coordinates of the */
258 HDPA hdpaPosY; /* items in LVS_ICON, and LVS_SMALLICON modes */
259 RANGES selectionRanges;
260 INT nSelectionMark; /* item to start next multiselection from */
262 BOOL bAutoarrange; /* Autoarrange flag when NOT in LVS_AUTOARRANGE */
265 HDPA hdpaColumns; /* array of COLUMN_INFO pointers */
266 BOOL colRectsDirty; /* trigger column rectangles requery from header */
269 BOOL bNoItemMetrics; /* flags if item metrics are not yet computed */
274 PFNLVCOMPARE pfnCompare; /* sorting callback pointer */
278 DWORD dwStyle; /* the cached window GWL_STYLE */
279 DWORD dwLvExStyle; /* extended listview style */
280 DWORD uView; /* current view available through LVM_[G,S]ETVIEW */
286 DELAYED_ITEM_EDIT itemEdit; /* Pointer to this structure will be the timer ID */
289 HIMAGELIST himlNormal;
290 HIMAGELIST himlSmall;
291 HIMAGELIST himlState;
295 POINT currIconPos; /* this is the position next icon will be placed */
299 INT xTrackLine; /* The x coefficient of the track line or -1 if none */
301 /* marquee selection */
302 BOOL bMarqueeSelect; /* marquee selection/highlight underway */
304 RECT marqueeRect; /* absolute coordinates of marquee selection */
305 RECT marqueeDrawRect; /* relative coordinates for drawing marquee */
306 POINT marqueeOrigin; /* absolute coordinates of marquee click origin */
309 BOOL bFocus; /* control has focus */
311 RECT rcFocus; /* focus bounds */
322 INT ntmHeight; /* Some cached metrics of the font used */
323 INT ntmMaxCharWidth; /* by the listview to draw items */
326 /* mouse operation */
330 POINT ptClickPos; /* point where the user clicked */
331 INT nLButtonDownItem; /* tracks item to reset multiselection on WM_LBUTTONUP */
335 /* keyboard operation */
336 DWORD lastKeyPressTimestamp;
338 INT nSearchParamLength;
339 WCHAR szSearchParam[ MAX_PATH ];
342 DWORD cditemmode; /* Keep the custom draw flags for an item/row */
343 BOOL bIsDrawing; /* Drawing in progress */
344 INT nMeasureItemHeight; /* WM_MEASUREITEM result */
345 BOOL bRedraw; /* WM_SETREDRAW switch */
348 DWORD iVersion; /* CCM_[G,S]ETVERSION */
354 /* How many we debug buffer to allocate */
355 #define DEBUG_BUFFERS 20
356 /* The size of a single debug buffer */
357 #define DEBUG_BUFFER_SIZE 256
359 /* Internal interface to LISTVIEW_HScroll and LISTVIEW_VScroll */
360 #define SB_INTERNAL -1
362 /* maximum size of a label */
363 #define DISP_TEXT_SIZE 260
365 /* padding for items in list and small icon display modes */
366 #define WIDTH_PADDING 12
368 /* padding for items in list, report and small icon display modes */
369 #define HEIGHT_PADDING 1
371 /* offset of items in report display mode */
372 #define REPORT_MARGINX 2
374 /* padding for icon in large icon display mode
375 * ICON_TOP_PADDING_NOTHITABLE - space between top of box and area
376 * that HITTEST will see.
377 * ICON_TOP_PADDING_HITABLE - spacing between above and icon.
378 * ICON_TOP_PADDING - sum of the two above.
379 * ICON_BOTTOM_PADDING - between bottom of icon and top of text
380 * LABEL_HOR_PADDING - between text and sides of box
381 * LABEL_VERT_PADDING - between bottom of text and end of box
383 * ICON_LR_PADDING - additional width above icon size.
384 * ICON_LR_HALF - half of the above value
386 #define ICON_TOP_PADDING_NOTHITABLE 2
387 #define ICON_TOP_PADDING_HITABLE 2
388 #define ICON_TOP_PADDING (ICON_TOP_PADDING_NOTHITABLE + ICON_TOP_PADDING_HITABLE)
389 #define ICON_BOTTOM_PADDING 4
390 #define LABEL_HOR_PADDING 5
391 #define LABEL_VERT_PADDING 7
392 #define ICON_LR_PADDING 16
393 #define ICON_LR_HALF (ICON_LR_PADDING/2)
395 /* default label width for items in list and small icon display modes */
396 #define DEFAULT_LABEL_WIDTH 40
397 /* maximum select rectangle width for empty text item in LV_VIEW_DETAILS */
398 #define MAX_EMPTYTEXT_SELECT_WIDTH 80
400 /* default column width for items in list display mode */
401 #define DEFAULT_COLUMN_WIDTH 128
403 /* Size of "line" scroll for V & H scrolls */
404 #define LISTVIEW_SCROLL_ICON_LINE_SIZE 37
406 /* Padding between image and label */
407 #define IMAGE_PADDING 2
409 /* Padding behind the label */
410 #define TRAILING_LABEL_PADDING 12
411 #define TRAILING_HEADER_PADDING 11
413 /* Border for the icon caption */
414 #define CAPTION_BORDER 2
416 /* Standard DrawText flags */
417 #define LV_ML_DT_FLAGS (DT_TOP | DT_NOPREFIX | DT_EDITCONTROL | DT_CENTER | DT_WORDBREAK | DT_WORD_ELLIPSIS | DT_END_ELLIPSIS)
418 #define LV_FL_DT_FLAGS (DT_TOP | DT_NOPREFIX | DT_EDITCONTROL | DT_CENTER | DT_WORDBREAK | DT_NOCLIP)
419 #define LV_SL_DT_FLAGS (DT_VCENTER | DT_NOPREFIX | DT_EDITCONTROL | DT_SINGLELINE | DT_WORD_ELLIPSIS | DT_END_ELLIPSIS)
421 /* Image index from state */
422 #define STATEIMAGEINDEX(x) (((x) & LVIS_STATEIMAGEMASK) >> 12)
424 /* The time in milliseconds to reset the search in the list */
425 #define KEY_DELAY 450
427 /* Dump the LISTVIEW_INFO structure to the debug channel */
428 #define LISTVIEW_DUMP(iP) do { \
429 TRACE("hwndSelf=%p, clrBk=0x%06x, clrText=0x%06x, clrTextBk=0x%06x, ItemHeight=%d, ItemWidth=%d, Style=0x%08x\n", \
430 iP->hwndSelf, iP->clrBk, iP->clrText, iP->clrTextBk, \
431 iP->nItemHeight, iP->nItemWidth, iP->dwStyle); \
432 TRACE("hwndSelf=%p, himlNor=%p, himlSml=%p, himlState=%p, Focused=%d, Hot=%d, exStyle=0x%08x, Focus=%d\n", \
433 iP->hwndSelf, iP->himlNormal, iP->himlSmall, iP->himlState, \
434 iP->nFocusedItem, iP->nHotItem, iP->dwLvExStyle, iP->bFocus ); \
435 TRACE("hwndSelf=%p, ntmH=%d, icSz.cx=%d, icSz.cy=%d, icSp.cx=%d, icSp.cy=%d, notifyFmt=%d\n", \
436 iP->hwndSelf, iP->ntmHeight, iP->iconSize.cx, iP->iconSize.cy, \
437 iP->iconSpacing.cx, iP->iconSpacing.cy, iP->notifyFormat); \
438 TRACE("hwndSelf=%p, rcList=%s\n", iP->hwndSelf, wine_dbgstr_rect(&iP->rcList)); \
441 static const WCHAR themeClass[] = {'L','i','s','t','V','i','e','w',0};
444 * forward declarations
446 static BOOL LISTVIEW_GetItemT(const LISTVIEW_INFO *, LPLVITEMW, BOOL);
447 static void LISTVIEW_GetItemBox(const LISTVIEW_INFO *, INT, LPRECT);
448 static void LISTVIEW_GetItemOrigin(const LISTVIEW_INFO *, INT, LPPOINT);
449 static BOOL LISTVIEW_GetItemPosition(const LISTVIEW_INFO *, INT, LPPOINT);
450 static BOOL LISTVIEW_GetItemRect(const LISTVIEW_INFO *, INT, LPRECT);
451 static void LISTVIEW_GetOrigin(const LISTVIEW_INFO *, LPPOINT);
452 static BOOL LISTVIEW_GetViewRect(const LISTVIEW_INFO *, LPRECT);
453 static void LISTVIEW_UpdateSize(LISTVIEW_INFO *);
454 static LRESULT LISTVIEW_Command(LISTVIEW_INFO *, WPARAM, LPARAM);
455 static INT LISTVIEW_GetStringWidthT(const LISTVIEW_INFO *, LPCWSTR, BOOL);
456 static BOOL LISTVIEW_KeySelection(LISTVIEW_INFO *, INT, BOOL);
457 static UINT LISTVIEW_GetItemState(const LISTVIEW_INFO *, INT, UINT);
458 static BOOL LISTVIEW_SetItemState(LISTVIEW_INFO *, INT, const LVITEMW *);
459 static LRESULT LISTVIEW_VScroll(LISTVIEW_INFO *, INT, INT);
460 static LRESULT LISTVIEW_HScroll(LISTVIEW_INFO *, INT, INT);
461 static BOOL LISTVIEW_EnsureVisible(LISTVIEW_INFO *, INT, BOOL);
462 static HIMAGELIST LISTVIEW_SetImageList(LISTVIEW_INFO *, INT, HIMAGELIST);
463 static INT LISTVIEW_HitTest(const LISTVIEW_INFO *, LPLVHITTESTINFO, BOOL, BOOL);
464 static BOOL LISTVIEW_EndEditLabelT(LISTVIEW_INFO *, BOOL, BOOL);
465 static BOOL LISTVIEW_Scroll(LISTVIEW_INFO *, INT, INT);
467 /******** Text handling functions *************************************/
469 /* A text pointer is either NULL, LPSTR_TEXTCALLBACK, or points to a
470 * text string. The string may be ANSI or Unicode, in which case
471 * the boolean isW tells us the type of the string.
473 * The name of the function tell what type of strings it expects:
474 * W: Unicode, T: ANSI/Unicode - function of isW
477 static inline BOOL is_text(LPCWSTR text)
479 return text != NULL && text != LPSTR_TEXTCALLBACKW;
482 static inline int textlenT(LPCWSTR text, BOOL isW)
484 return !is_text(text) ? 0 :
485 isW ? lstrlenW(text) : lstrlenA((LPCSTR)text);
488 static inline void textcpynT(LPWSTR dest, BOOL isDestW, LPCWSTR src, BOOL isSrcW, INT max)
491 if (isSrcW) lstrcpynW(dest, src, max);
492 else MultiByteToWideChar(CP_ACP, 0, (LPCSTR)src, -1, dest, max);
494 if (isSrcW) WideCharToMultiByte(CP_ACP, 0, src, -1, (LPSTR)dest, max, NULL, NULL);
495 else lstrcpynA((LPSTR)dest, (LPCSTR)src, max);
498 static inline LPWSTR textdupTtoW(LPCWSTR text, BOOL isW)
500 LPWSTR wstr = (LPWSTR)text;
502 if (!isW && is_text(text))
504 INT len = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)text, -1, NULL, 0);
505 wstr = Alloc(len * sizeof(WCHAR));
506 if (wstr) MultiByteToWideChar(CP_ACP, 0, (LPCSTR)text, -1, wstr, len);
508 TRACE(" wstr=%s\n", text == LPSTR_TEXTCALLBACKW ? "(callback)" : debugstr_w(wstr));
512 static inline void textfreeT(LPWSTR wstr, BOOL isW)
514 if (!isW && is_text(wstr)) Free (wstr);
518 * dest is a pointer to a Unicode string
519 * src is a pointer to a string (Unicode if isW, ANSI if !isW)
521 static BOOL textsetptrT(LPWSTR *dest, LPCWSTR src, BOOL isW)
525 if (src == LPSTR_TEXTCALLBACKW)
527 if (is_text(*dest)) Free(*dest);
528 *dest = LPSTR_TEXTCALLBACKW;
532 LPWSTR pszText = textdupTtoW(src, isW);
533 if (*dest == LPSTR_TEXTCALLBACKW) *dest = NULL;
534 bResult = Str_SetPtrW(dest, pszText);
535 textfreeT(pszText, isW);
541 * compares a Unicode to a Unicode/ANSI text string
543 static inline int textcmpWT(LPCWSTR aw, LPCWSTR bt, BOOL isW)
545 if (!aw) return bt ? -1 : 0;
547 if (aw == LPSTR_TEXTCALLBACKW)
548 return bt == LPSTR_TEXTCALLBACKW ? 1 : -1;
549 if (bt != LPSTR_TEXTCALLBACKW)
551 LPWSTR bw = textdupTtoW(bt, isW);
552 int r = bw ? lstrcmpW(aw, bw) : 1;
560 static inline int lstrncmpiW(LPCWSTR s1, LPCWSTR s2, int n)
564 n = min(min(n, lstrlenW(s1)), lstrlenW(s2));
565 res = CompareStringW(LOCALE_USER_DEFAULT, NORM_IGNORECASE, s1, n, s2, n);
566 return res ? res - sizeof(WCHAR) : res;
569 /******** Debugging functions *****************************************/
571 static inline LPCSTR debugtext_t(LPCWSTR text, BOOL isW)
573 if (text == LPSTR_TEXTCALLBACKW) return "(callback)";
574 return isW ? debugstr_w(text) : debugstr_a((LPCSTR)text);
577 static inline LPCSTR debugtext_tn(LPCWSTR text, BOOL isW, INT n)
579 if (text == LPSTR_TEXTCALLBACKW) return "(callback)";
580 n = min(textlenT(text, isW), n);
581 return isW ? debugstr_wn(text, n) : debugstr_an((LPCSTR)text, n);
584 static char* debug_getbuf(void)
586 static int index = 0;
587 static char buffers[DEBUG_BUFFERS][DEBUG_BUFFER_SIZE];
588 return buffers[index++ % DEBUG_BUFFERS];
591 static inline const char* debugrange(const RANGE *lprng)
593 if (!lprng) return "(null)";
594 return wine_dbg_sprintf("[%d, %d]", lprng->lower, lprng->upper);
597 static const char* debugscrollinfo(const SCROLLINFO *pScrollInfo)
599 char* buf = debug_getbuf(), *text = buf;
600 int len, size = DEBUG_BUFFER_SIZE;
602 if (pScrollInfo == NULL) return "(null)";
603 len = snprintf(buf, size, "{cbSize=%d, ", pScrollInfo->cbSize);
604 if (len == -1) goto end; buf += len; size -= len;
605 if (pScrollInfo->fMask & SIF_RANGE)
606 len = snprintf(buf, size, "nMin=%d, nMax=%d, ", pScrollInfo->nMin, pScrollInfo->nMax);
608 if (len == -1) goto end; buf += len; size -= len;
609 if (pScrollInfo->fMask & SIF_PAGE)
610 len = snprintf(buf, size, "nPage=%u, ", pScrollInfo->nPage);
612 if (len == -1) goto end; buf += len; size -= len;
613 if (pScrollInfo->fMask & SIF_POS)
614 len = snprintf(buf, size, "nPos=%d, ", pScrollInfo->nPos);
616 if (len == -1) goto end; buf += len; size -= len;
617 if (pScrollInfo->fMask & SIF_TRACKPOS)
618 len = snprintf(buf, size, "nTrackPos=%d, ", pScrollInfo->nTrackPos);
620 if (len == -1) goto end; buf += len;
623 buf = text + strlen(text);
625 if (buf - text > 2) { buf[-2] = '}'; buf[-1] = 0; }
629 static const char* debugnmlistview(const NMLISTVIEW *plvnm)
631 if (!plvnm) return "(null)";
632 return wine_dbg_sprintf("iItem=%d, iSubItem=%d, uNewState=0x%x,"
633 " uOldState=0x%x, uChanged=0x%x, ptAction=%s, lParam=%ld",
634 plvnm->iItem, plvnm->iSubItem, plvnm->uNewState, plvnm->uOldState,
635 plvnm->uChanged, wine_dbgstr_point(&plvnm->ptAction), plvnm->lParam);
638 static const char* debuglvitem_t(const LVITEMW *lpLVItem, BOOL isW)
640 char* buf = debug_getbuf(), *text = buf;
641 int len, size = DEBUG_BUFFER_SIZE;
643 if (lpLVItem == NULL) return "(null)";
644 len = snprintf(buf, size, "{iItem=%d, iSubItem=%d, ", lpLVItem->iItem, lpLVItem->iSubItem);
645 if (len == -1) goto end; buf += len; size -= len;
646 if (lpLVItem->mask & LVIF_STATE)
647 len = snprintf(buf, size, "state=%x, stateMask=%x, ", lpLVItem->state, lpLVItem->stateMask);
649 if (len == -1) goto end; buf += len; size -= len;
650 if (lpLVItem->mask & LVIF_TEXT)
651 len = snprintf(buf, size, "pszText=%s, cchTextMax=%d, ", debugtext_tn(lpLVItem->pszText, isW, 80), lpLVItem->cchTextMax);
653 if (len == -1) goto end; buf += len; size -= len;
654 if (lpLVItem->mask & LVIF_IMAGE)
655 len = snprintf(buf, size, "iImage=%d, ", lpLVItem->iImage);
657 if (len == -1) goto end; buf += len; size -= len;
658 if (lpLVItem->mask & LVIF_PARAM)
659 len = snprintf(buf, size, "lParam=%lx, ", lpLVItem->lParam);
661 if (len == -1) goto end; buf += len; size -= len;
662 if (lpLVItem->mask & LVIF_INDENT)
663 len = snprintf(buf, size, "iIndent=%d, ", lpLVItem->iIndent);
665 if (len == -1) goto end; buf += len;
668 buf = text + strlen(text);
670 if (buf - text > 2) { buf[-2] = '}'; buf[-1] = 0; }
674 static const char* debuglvcolumn_t(const LVCOLUMNW *lpColumn, BOOL isW)
676 char* buf = debug_getbuf(), *text = buf;
677 int len, size = DEBUG_BUFFER_SIZE;
679 if (lpColumn == NULL) return "(null)";
680 len = snprintf(buf, size, "{");
681 if (len == -1) goto end; buf += len; size -= len;
682 if (lpColumn->mask & LVCF_SUBITEM)
683 len = snprintf(buf, size, "iSubItem=%d, ", lpColumn->iSubItem);
685 if (len == -1) goto end; buf += len; size -= len;
686 if (lpColumn->mask & LVCF_FMT)
687 len = snprintf(buf, size, "fmt=%x, ", lpColumn->fmt);
689 if (len == -1) goto end; buf += len; size -= len;
690 if (lpColumn->mask & LVCF_WIDTH)
691 len = snprintf(buf, size, "cx=%d, ", lpColumn->cx);
693 if (len == -1) goto end; buf += len; size -= len;
694 if (lpColumn->mask & LVCF_TEXT)
695 len = snprintf(buf, size, "pszText=%s, cchTextMax=%d, ", debugtext_tn(lpColumn->pszText, isW, 80), lpColumn->cchTextMax);
697 if (len == -1) goto end; buf += len; size -= len;
698 if (lpColumn->mask & LVCF_IMAGE)
699 len = snprintf(buf, size, "iImage=%d, ", lpColumn->iImage);
701 if (len == -1) goto end; buf += len; size -= len;
702 if (lpColumn->mask & LVCF_ORDER)
703 len = snprintf(buf, size, "iOrder=%d, ", lpColumn->iOrder);
705 if (len == -1) goto end; buf += len;
708 buf = text + strlen(text);
710 if (buf - text > 2) { buf[-2] = '}'; buf[-1] = 0; }
714 static const char* debuglvhittestinfo(const LVHITTESTINFO *lpht)
716 if (!lpht) return "(null)";
718 return wine_dbg_sprintf("{pt=%s, flags=0x%x, iItem=%d, iSubItem=%d}",
719 wine_dbgstr_point(&lpht->pt), lpht->flags, lpht->iItem, lpht->iSubItem);
722 /* Return the corresponding text for a given scroll value */
723 static inline LPCSTR debugscrollcode(int nScrollCode)
727 case SB_LINELEFT: return "SB_LINELEFT";
728 case SB_LINERIGHT: return "SB_LINERIGHT";
729 case SB_PAGELEFT: return "SB_PAGELEFT";
730 case SB_PAGERIGHT: return "SB_PAGERIGHT";
731 case SB_THUMBPOSITION: return "SB_THUMBPOSITION";
732 case SB_THUMBTRACK: return "SB_THUMBTRACK";
733 case SB_ENDSCROLL: return "SB_ENDSCROLL";
734 case SB_INTERNAL: return "SB_INTERNAL";
735 default: return "unknown";
740 /******** Notification functions ************************************/
742 static int get_ansi_notification(UINT unicodeNotificationCode)
744 switch (unicodeNotificationCode)
746 case LVN_BEGINLABELEDITA:
747 case LVN_BEGINLABELEDITW: return LVN_BEGINLABELEDITA;
748 case LVN_ENDLABELEDITA:
749 case LVN_ENDLABELEDITW: return LVN_ENDLABELEDITA;
750 case LVN_GETDISPINFOA:
751 case LVN_GETDISPINFOW: return LVN_GETDISPINFOA;
752 case LVN_SETDISPINFOA:
753 case LVN_SETDISPINFOW: return LVN_SETDISPINFOA;
754 case LVN_ODFINDITEMA:
755 case LVN_ODFINDITEMW: return LVN_ODFINDITEMA;
756 case LVN_GETINFOTIPA:
757 case LVN_GETINFOTIPW: return LVN_GETINFOTIPA;
758 /* header forwards */
760 case HDN_TRACKW: return HDN_TRACKA;
762 case HDN_ENDTRACKW: return HDN_ENDTRACKA;
763 case HDN_BEGINDRAG: return HDN_BEGINDRAG;
764 case HDN_ENDDRAG: return HDN_ENDDRAG;
765 case HDN_ITEMCHANGINGA:
766 case HDN_ITEMCHANGINGW: return HDN_ITEMCHANGINGA;
767 case HDN_ITEMCHANGEDA:
768 case HDN_ITEMCHANGEDW: return HDN_ITEMCHANGEDA;
770 case HDN_ITEMCLICKW: return HDN_ITEMCLICKA;
771 case HDN_DIVIDERDBLCLICKA:
772 case HDN_DIVIDERDBLCLICKW: return HDN_DIVIDERDBLCLICKA;
775 FIXME("unknown notification %x\n", unicodeNotificationCode);
776 return unicodeNotificationCode;
779 /* forwards header notifications to listview parent */
780 static LRESULT notify_forward_header(const LISTVIEW_INFO *infoPtr, const NMHEADERW *lpnmh)
784 HD_TEXTFILTERA textfilter;
785 LPSTR text = NULL, filter = NULL;
788 /* on unicode format exit earlier */
789 if (infoPtr->notifyFormat == NFR_UNICODE)
790 return SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, lpnmh->hdr.idFrom,
793 /* header always supplies unicode notifications,
794 all we have to do is to convert strings to ANSI */
795 nmhA = *(const NMHEADERA*)lpnmh;
798 hditema = *(HDITEMA*)lpnmh->pitem;
799 nmhA.pitem = &hditema;
800 /* convert item text */
801 if (lpnmh->pitem->mask & HDI_TEXT)
803 hditema.pszText = NULL;
804 Str_SetPtrWtoA(&hditema.pszText, lpnmh->pitem->pszText);
805 text = hditema.pszText;
807 /* convert filter text */
808 if ((lpnmh->pitem->mask & HDI_FILTER) && (lpnmh->pitem->type == HDFT_ISSTRING) &&
809 lpnmh->pitem->pvFilter)
811 hditema.pvFilter = &textfilter;
812 textfilter = *(HD_TEXTFILTERA*)(lpnmh->pitem->pvFilter);
813 textfilter.pszText = NULL;
814 Str_SetPtrWtoA(&textfilter.pszText, ((HD_TEXTFILTERW*)lpnmh->pitem->pvFilter)->pszText);
815 filter = textfilter.pszText;
818 nmhA.hdr.code = get_ansi_notification(lpnmh->hdr.code);
820 ret = SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmhA.hdr.idFrom,
830 static LRESULT notify_hdr(const LISTVIEW_INFO *infoPtr, INT code, LPNMHDR pnmh)
834 TRACE("(code=%d)\n", code);
836 pnmh->hwndFrom = infoPtr->hwndSelf;
837 pnmh->idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
839 result = SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, pnmh->idFrom, (LPARAM)pnmh);
841 TRACE(" <= %ld\n", result);
846 static inline BOOL notify(const LISTVIEW_INFO *infoPtr, INT code)
849 HWND hwnd = infoPtr->hwndSelf;
850 notify_hdr(infoPtr, code, &nmh);
851 return IsWindow(hwnd);
854 static inline void notify_itemactivate(const LISTVIEW_INFO *infoPtr, const LVHITTESTINFO *htInfo)
865 item.mask = LVIF_PARAM|LVIF_STATE;
866 item.iItem = htInfo->iItem;
868 item.stateMask = (UINT)-1;
869 if (LISTVIEW_GetItemT(infoPtr, &item, TRUE)) {
870 nmia.lParam = item.lParam;
871 nmia.uOldState = item.state;
872 nmia.uNewState = item.state | LVIS_ACTIVATING;
873 nmia.uChanged = LVIF_STATE;
876 nmia.iItem = htInfo->iItem;
877 nmia.iSubItem = htInfo->iSubItem;
878 nmia.ptAction = htInfo->pt;
880 if (GetKeyState(VK_SHIFT) & 0x8000) nmia.uKeyFlags |= LVKF_SHIFT;
881 if (GetKeyState(VK_CONTROL) & 0x8000) nmia.uKeyFlags |= LVKF_CONTROL;
882 if (GetKeyState(VK_MENU) & 0x8000) nmia.uKeyFlags |= LVKF_ALT;
884 notify_hdr(infoPtr, LVN_ITEMACTIVATE, (LPNMHDR)&nmia);
887 static inline LRESULT notify_listview(const LISTVIEW_INFO *infoPtr, INT code, LPNMLISTVIEW plvnm)
889 TRACE("(code=%d, plvnm=%s)\n", code, debugnmlistview(plvnm));
890 return notify_hdr(infoPtr, code, (LPNMHDR)plvnm);
893 static BOOL notify_click(const LISTVIEW_INFO *infoPtr, INT code, const LVHITTESTINFO *lvht)
897 HWND hwnd = infoPtr->hwndSelf;
899 TRACE("code=%d, lvht=%s\n", code, debuglvhittestinfo(lvht));
900 ZeroMemory(&nmia, sizeof(nmia));
901 nmia.iItem = lvht->iItem;
902 nmia.iSubItem = lvht->iSubItem;
903 nmia.ptAction = lvht->pt;
904 item.mask = LVIF_PARAM;
905 item.iItem = lvht->iItem;
907 if (LISTVIEW_GetItemT(infoPtr, &item, TRUE)) nmia.lParam = item.lParam;
908 notify_hdr(infoPtr, code, (LPNMHDR)&nmia);
909 return IsWindow(hwnd);
912 static BOOL notify_deleteitem(const LISTVIEW_INFO *infoPtr, INT nItem)
916 HWND hwnd = infoPtr->hwndSelf;
918 ZeroMemory(&nmlv, sizeof (NMLISTVIEW));
920 item.mask = LVIF_PARAM;
923 if (LISTVIEW_GetItemT(infoPtr, &item, TRUE)) nmlv.lParam = item.lParam;
924 notify_listview(infoPtr, LVN_DELETEITEM, &nmlv);
925 return IsWindow(hwnd);
929 Send notification. depends on dispinfoW having same
930 structure as dispinfoA.
931 infoPtr : listview struct
932 code : *Unicode* notification code
933 pdi : dispinfo structure (can be unicode or ansi)
934 isW : TRUE if dispinfo is Unicode
936 static BOOL notify_dispinfoT(const LISTVIEW_INFO *infoPtr, UINT code, LPNMLVDISPINFOW pdi, BOOL isW)
938 INT length = 0, ret_length;
939 LPWSTR buffer = NULL, ret_text;
940 BOOL return_ansi = FALSE;
941 BOOL return_unicode = FALSE;
944 if ((pdi->item.mask & LVIF_TEXT) && is_text(pdi->item.pszText))
946 return_unicode = ( isW && infoPtr->notifyFormat == NFR_ANSI);
947 return_ansi = (!isW && infoPtr->notifyFormat == NFR_UNICODE);
950 ret_length = pdi->item.cchTextMax;
951 ret_text = pdi->item.pszText;
953 if (return_unicode || return_ansi)
955 if (code != LVN_GETDISPINFOW)
957 length = return_ansi ?
958 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pdi->item.pszText, -1, NULL, 0):
959 WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, NULL, 0, NULL, NULL);
963 length = pdi->item.cchTextMax;
964 *pdi->item.pszText = 0; /* make sure we don't process garbage */
967 buffer = Alloc( (return_ansi ? sizeof(WCHAR) : sizeof(CHAR)) * length);
968 if (!buffer) return FALSE;
971 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pdi->item.pszText, -1,
974 WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, (LPSTR) buffer,
977 pdi->item.pszText = buffer;
978 pdi->item.cchTextMax = length;
981 if (infoPtr->notifyFormat == NFR_ANSI)
982 code = get_ansi_notification(code);
984 TRACE(" pdi->item=%s\n", debuglvitem_t(&pdi->item, infoPtr->notifyFormat != NFR_ANSI));
985 ret = notify_hdr(infoPtr, code, &pdi->hdr);
986 TRACE(" resulting code=%d\n", pdi->hdr.code);
988 if (return_ansi || return_unicode)
990 if (return_ansi && (pdi->hdr.code == LVN_GETDISPINFOA))
992 strcpy((char*)ret_text, (char*)pdi->item.pszText);
994 else if (return_unicode && (pdi->hdr.code == LVN_GETDISPINFOW))
996 strcpyW(ret_text, pdi->item.pszText);
998 else if (return_ansi) /* note : pointer can be changed by app ! */
1000 WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, (LPSTR) ret_text,
1001 ret_length, NULL, NULL);
1004 MultiByteToWideChar(CP_ACP, 0, (LPSTR) pdi->item.pszText, -1,
1005 ret_text, ret_length);
1007 pdi->item.pszText = ret_text; /* restores our buffer */
1008 pdi->item.cchTextMax = ret_length;
1014 /* if dipsinfo holder changed notification code then convert */
1015 if (!isW && (pdi->hdr.code == LVN_GETDISPINFOW) && (pdi->item.mask & LVIF_TEXT))
1017 length = WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, NULL, 0, NULL, NULL);
1019 buffer = Alloc(length * sizeof(CHAR));
1020 if (!buffer) return FALSE;
1022 WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, (LPSTR) buffer,
1023 ret_length, NULL, NULL);
1025 strcpy((LPSTR)pdi->item.pszText, (LPSTR)buffer);
1032 static void customdraw_fill(NMLVCUSTOMDRAW *lpnmlvcd, const LISTVIEW_INFO *infoPtr, HDC hdc,
1033 const RECT *rcBounds, const LVITEMW *lplvItem)
1035 ZeroMemory(lpnmlvcd, sizeof(NMLVCUSTOMDRAW));
1036 lpnmlvcd->nmcd.hdc = hdc;
1037 lpnmlvcd->nmcd.rc = *rcBounds;
1038 lpnmlvcd->clrTextBk = infoPtr->clrTextBk;
1039 lpnmlvcd->clrText = infoPtr->clrText;
1040 if (!lplvItem) return;
1041 lpnmlvcd->nmcd.dwItemSpec = lplvItem->iItem + 1;
1042 lpnmlvcd->iSubItem = lplvItem->iSubItem;
1043 if (lplvItem->state & LVIS_SELECTED) lpnmlvcd->nmcd.uItemState |= CDIS_SELECTED;
1044 if (lplvItem->state & LVIS_FOCUSED) lpnmlvcd->nmcd.uItemState |= CDIS_FOCUS;
1045 if (lplvItem->iItem == infoPtr->nHotItem) lpnmlvcd->nmcd.uItemState |= CDIS_HOT;
1046 lpnmlvcd->nmcd.lItemlParam = lplvItem->lParam;
1049 static inline DWORD notify_customdraw (const LISTVIEW_INFO *infoPtr, DWORD dwDrawStage, NMLVCUSTOMDRAW *lpnmlvcd)
1051 BOOL isForItem = (lpnmlvcd->nmcd.dwItemSpec != 0);
1054 lpnmlvcd->nmcd.dwDrawStage = dwDrawStage;
1055 if (isForItem) lpnmlvcd->nmcd.dwDrawStage |= CDDS_ITEM;
1056 if (lpnmlvcd->iSubItem) lpnmlvcd->nmcd.dwDrawStage |= CDDS_SUBITEM;
1057 if (isForItem) lpnmlvcd->nmcd.dwItemSpec--;
1058 result = notify_hdr(infoPtr, NM_CUSTOMDRAW, &lpnmlvcd->nmcd.hdr);
1059 if (isForItem) lpnmlvcd->nmcd.dwItemSpec++;
1063 static void prepaint_setup (const LISTVIEW_INFO *infoPtr, HDC hdc, NMLVCUSTOMDRAW *lpnmlvcd, BOOL SubItem)
1065 if (lpnmlvcd->clrTextBk == CLR_DEFAULT)
1066 lpnmlvcd->clrTextBk = comctl32_color.clrWindow;
1067 if (lpnmlvcd->clrText == CLR_DEFAULT)
1068 lpnmlvcd->clrText = comctl32_color.clrWindowText;
1070 /* apparently, for selected items, we have to override the returned values */
1073 if (lpnmlvcd->nmcd.uItemState & CDIS_SELECTED)
1075 if (infoPtr->bFocus)
1077 lpnmlvcd->clrTextBk = comctl32_color.clrHighlight;
1078 lpnmlvcd->clrText = comctl32_color.clrHighlightText;
1080 else if (infoPtr->dwStyle & LVS_SHOWSELALWAYS)
1082 lpnmlvcd->clrTextBk = comctl32_color.clr3dFace;
1083 lpnmlvcd->clrText = comctl32_color.clrBtnText;
1088 /* Set the text attributes */
1089 if (lpnmlvcd->clrTextBk != CLR_NONE)
1091 SetBkMode(hdc, OPAQUE);
1092 SetBkColor(hdc,lpnmlvcd->clrTextBk);
1095 SetBkMode(hdc, TRANSPARENT);
1096 SetTextColor(hdc, lpnmlvcd->clrText);
1099 static inline DWORD notify_postpaint (const LISTVIEW_INFO *infoPtr, NMLVCUSTOMDRAW *lpnmlvcd)
1101 return notify_customdraw(infoPtr, CDDS_POSTPAINT, lpnmlvcd);
1104 /* returns TRUE when repaint needed, FALSE otherwise */
1105 static BOOL notify_measureitem(LISTVIEW_INFO *infoPtr)
1107 MEASUREITEMSTRUCT mis;
1108 mis.CtlType = ODT_LISTVIEW;
1109 mis.CtlID = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1113 mis.itemHeight= infoPtr->nItemHeight;
1114 SendMessageW(infoPtr->hwndNotify, WM_MEASUREITEM, mis.CtlID, (LPARAM)&mis);
1115 if (infoPtr->nItemHeight != max(mis.itemHeight, 1))
1117 infoPtr->nMeasureItemHeight = infoPtr->nItemHeight = max(mis.itemHeight, 1);
1123 /******** Item iterator functions **********************************/
1125 static RANGES ranges_create(int count);
1126 static void ranges_destroy(RANGES ranges);
1127 static BOOL ranges_add(RANGES ranges, RANGE range);
1128 static BOOL ranges_del(RANGES ranges, RANGE range);
1129 static void ranges_dump(RANGES ranges);
1131 static inline BOOL ranges_additem(RANGES ranges, INT nItem)
1133 RANGE range = { nItem, nItem + 1 };
1135 return ranges_add(ranges, range);
1138 static inline BOOL ranges_delitem(RANGES ranges, INT nItem)
1140 RANGE range = { nItem, nItem + 1 };
1142 return ranges_del(ranges, range);
1146 * ITERATOR DOCUMENTATION
1148 * The iterator functions allow for easy, and convenient iteration
1149 * over items of interest in the list. Typically, you create a
1150 * iterator, use it, and destroy it, as such:
1153 * iterator_xxxitems(&i, ...);
1154 * while (iterator_{prev,next}(&i)
1156 * //code which uses i.nItem
1158 * iterator_destroy(&i);
1160 * where xxx is either: framed, or visible.
1161 * Note that it is important that the code destroys the iterator
1162 * after it's done with it, as the creation of the iterator may
1163 * allocate memory, which thus needs to be freed.
1165 * You can iterate both forwards, and backwards through the list,
1166 * by using iterator_next or iterator_prev respectively.
1168 * Lower numbered items are draw on top of higher number items in
1169 * LVS_ICON, and LVS_SMALLICON (which are the only modes where
1170 * items may overlap). So, to test items, you should use
1172 * which lists the items top to bottom (in Z-order).
1173 * For drawing items, you should use
1175 * which lists the items bottom to top (in Z-order).
1176 * If you keep iterating over the items after the end-of-items
1177 * marker (-1) is returned, the iterator will start from the
1178 * beginning. Typically, you don't need to test for -1,
1179 * because iterator_{next,prev} will return TRUE if more items
1180 * are to be iterated over, or FALSE otherwise.
1182 * Note: the iterator is defined to be bidirectional. That is,
1183 * any number of prev followed by any number of next, or
1184 * five versa, should leave the iterator at the same item:
1185 * prev * n, next * n = next * n, prev * n
1187 * The iterator has a notion of an out-of-order, special item,
1188 * which sits at the start of the list. This is used in
1189 * LVS_ICON, and LVS_SMALLICON mode to handle the focused item,
1190 * which needs to be first, as it may overlap other items.
1192 * The code is a bit messy because we have:
1193 * - a special item to deal with
1194 * - simple range, or composite range
1196 * If you find bugs, or want to add features, please make sure you
1197 * always check/modify *both* iterator_prev, and iterator_next.
1201 * This function iterates through the items in increasing order,
1202 * but prefixed by the special item, then -1. That is:
1203 * special, 1, 2, 3, ..., n, -1.
1204 * Each item is listed only once.
1206 static inline BOOL iterator_next(ITERATOR* i)
1210 i->nItem = i->nSpecial;
1211 if (i->nItem != -1) return TRUE;
1213 if (i->nItem == i->nSpecial)
1215 if (i->ranges) i->index = 0;
1221 if (i->nItem == i->nSpecial) i->nItem++;
1222 if (i->nItem < i->range.upper) return TRUE;
1227 if (i->index < DPA_GetPtrCount(i->ranges->hdpa))
1228 i->range = *(RANGE*)DPA_GetPtr(i->ranges->hdpa, i->index++);
1231 else if (i->nItem >= i->range.upper) goto end;
1233 i->nItem = i->range.lower;
1234 if (i->nItem >= 0) goto testitem;
1241 * This function iterates through the items in decreasing order,
1242 * followed by the special item, then -1. That is:
1243 * n, n-1, ..., 3, 2, 1, special, -1.
1244 * Each item is listed only once.
1246 static inline BOOL iterator_prev(ITERATOR* i)
1253 if (i->ranges) i->index = DPA_GetPtrCount(i->ranges->hdpa);
1256 if (i->nItem == i->nSpecial)
1264 if (i->nItem == i->nSpecial) i->nItem--;
1265 if (i->nItem >= i->range.lower) return TRUE;
1271 i->range = *(RANGE*)DPA_GetPtr(i->ranges->hdpa, --i->index);
1274 else if (!start && i->nItem < i->range.lower) goto end;
1276 i->nItem = i->range.upper;
1277 if (i->nItem > 0) goto testitem;
1279 return (i->nItem = i->nSpecial) != -1;
1282 static RANGE iterator_range(const ITERATOR *i)
1286 if (!i->ranges) return i->range;
1288 if (DPA_GetPtrCount(i->ranges->hdpa) > 0)
1290 range.lower = (*(RANGE*)DPA_GetPtr(i->ranges->hdpa, 0)).lower;
1291 range.upper = (*(RANGE*)DPA_GetPtr(i->ranges->hdpa, DPA_GetPtrCount(i->ranges->hdpa) - 1)).upper;
1293 else range.lower = range.upper = 0;
1299 * Releases resources associated with this ierator.
1301 static inline void iterator_destroy(const ITERATOR *i)
1303 ranges_destroy(i->ranges);
1307 * Create an empty iterator.
1309 static inline BOOL iterator_empty(ITERATOR* i)
1311 ZeroMemory(i, sizeof(*i));
1312 i->nItem = i->nSpecial = i->range.lower = i->range.upper = -1;
1317 * Create an iterator over a range.
1319 static inline BOOL iterator_rangeitems(ITERATOR* i, RANGE range)
1327 * Create an iterator over a bunch of ranges.
1328 * Please note that the iterator will take ownership of the ranges,
1329 * and will free them upon destruction.
1331 static inline BOOL iterator_rangesitems(ITERATOR* i, RANGES ranges)
1339 * Creates an iterator over the items which intersect frame.
1340 * Uses absolute coordinates rather than compensating for the current offset.
1342 static BOOL iterator_frameditems_absolute(ITERATOR* i, const LISTVIEW_INFO* infoPtr, const RECT *frame)
1344 RECT rcItem, rcTemp;
1346 /* in case we fail, we want to return an empty iterator */
1347 if (!iterator_empty(i)) return FALSE;
1349 TRACE("(frame=%s)\n", wine_dbgstr_rect(frame));
1351 if (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON)
1355 if (infoPtr->uView == LV_VIEW_ICON && infoPtr->nFocusedItem != -1)
1357 LISTVIEW_GetItemBox(infoPtr, infoPtr->nFocusedItem, &rcItem);
1358 if (IntersectRect(&rcTemp, &rcItem, frame))
1359 i->nSpecial = infoPtr->nFocusedItem;
1361 if (!(iterator_rangesitems(i, ranges_create(50)))) return FALSE;
1362 /* to do better here, we need to have PosX, and PosY sorted */
1363 TRACE("building icon ranges:\n");
1364 for (nItem = 0; nItem < infoPtr->nItemCount; nItem++)
1366 rcItem.left = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosX, nItem);
1367 rcItem.top = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosY, nItem);
1368 rcItem.right = rcItem.left + infoPtr->nItemWidth;
1369 rcItem.bottom = rcItem.top + infoPtr->nItemHeight;
1370 if (IntersectRect(&rcTemp, &rcItem, frame))
1371 ranges_additem(i->ranges, nItem);
1375 else if (infoPtr->uView == LV_VIEW_DETAILS)
1379 if (frame->left >= infoPtr->nItemWidth) return TRUE;
1380 if (frame->top >= infoPtr->nItemHeight * infoPtr->nItemCount) return TRUE;
1382 range.lower = max(frame->top / infoPtr->nItemHeight, 0);
1383 range.upper = min((frame->bottom - 1) / infoPtr->nItemHeight, infoPtr->nItemCount - 1) + 1;
1384 if (range.upper <= range.lower) return TRUE;
1385 if (!iterator_rangeitems(i, range)) return FALSE;
1386 TRACE(" report=%s\n", debugrange(&i->range));
1390 INT nPerCol = max((infoPtr->rcList.bottom - infoPtr->rcList.top) / infoPtr->nItemHeight, 1);
1391 INT nFirstRow = max(frame->top / infoPtr->nItemHeight, 0);
1392 INT nLastRow = min((frame->bottom - 1) / infoPtr->nItemHeight, nPerCol - 1);
1399 if (infoPtr->nItemWidth)
1401 nFirstCol = max(frame->left / infoPtr->nItemWidth, 0);
1402 nLastCol = min((frame->right - 1) / infoPtr->nItemWidth, (infoPtr->nItemCount + nPerCol - 1) / nPerCol);
1406 nFirstCol = max(frame->left, 0);
1407 nLastCol = min(frame->right - 1, (infoPtr->nItemCount + nPerCol - 1) / nPerCol);
1410 lower = nFirstCol * nPerCol + nFirstRow;
1412 TRACE("nPerCol=%d, nFirstRow=%d, nLastRow=%d, nFirstCol=%d, nLastCol=%d, lower=%d\n",
1413 nPerCol, nFirstRow, nLastRow, nFirstCol, nLastCol, lower);
1415 if (nLastCol < nFirstCol || nLastRow < nFirstRow) return TRUE;
1417 if (!(iterator_rangesitems(i, ranges_create(nLastCol - nFirstCol + 1)))) return FALSE;
1418 TRACE("building list ranges:\n");
1419 for (nCol = nFirstCol; nCol <= nLastCol; nCol++)
1421 item_range.lower = nCol * nPerCol + nFirstRow;
1422 if(item_range.lower >= infoPtr->nItemCount) break;
1423 item_range.upper = min(nCol * nPerCol + nLastRow + 1, infoPtr->nItemCount);
1424 TRACE(" list=%s\n", debugrange(&item_range));
1425 ranges_add(i->ranges, item_range);
1433 * Creates an iterator over the items which intersect lprc.
1435 static BOOL iterator_frameditems(ITERATOR* i, const LISTVIEW_INFO* infoPtr, const RECT *lprc)
1440 TRACE("(lprc=%s)\n", wine_dbgstr_rect(lprc));
1442 LISTVIEW_GetOrigin(infoPtr, &Origin);
1443 OffsetRect(&frame, -Origin.x, -Origin.y);
1445 return iterator_frameditems_absolute(i, infoPtr, &frame);
1449 * Creates an iterator over the items which intersect the visible region of hdc.
1451 static BOOL iterator_visibleitems(ITERATOR *i, const LISTVIEW_INFO *infoPtr, HDC hdc)
1453 POINT Origin, Position;
1454 RECT rcItem, rcClip;
1457 rgntype = GetClipBox(hdc, &rcClip);
1458 if (rgntype == NULLREGION) return iterator_empty(i);
1459 if (!iterator_frameditems(i, infoPtr, &rcClip)) return FALSE;
1460 if (rgntype == SIMPLEREGION) return TRUE;
1462 /* first deal with the special item */
1463 if (i->nSpecial != -1)
1465 LISTVIEW_GetItemBox(infoPtr, i->nSpecial, &rcItem);
1466 if (!RectVisible(hdc, &rcItem)) i->nSpecial = -1;
1469 /* if we can't deal with the region, we'll just go with the simple range */
1470 LISTVIEW_GetOrigin(infoPtr, &Origin);
1471 TRACE("building visible range:\n");
1472 if (!i->ranges && i->range.lower < i->range.upper)
1474 if (!(i->ranges = ranges_create(50))) return TRUE;
1475 if (!ranges_add(i->ranges, i->range))
1477 ranges_destroy(i->ranges);
1483 /* now delete the invisible items from the list */
1484 while(iterator_next(i))
1486 LISTVIEW_GetItemOrigin(infoPtr, i->nItem, &Position);
1487 rcItem.left = (infoPtr->uView == LV_VIEW_DETAILS) ? Origin.x : Position.x + Origin.x;
1488 rcItem.top = Position.y + Origin.y;
1489 rcItem.right = rcItem.left + infoPtr->nItemWidth;
1490 rcItem.bottom = rcItem.top + infoPtr->nItemHeight;
1491 if (!RectVisible(hdc, &rcItem))
1492 ranges_delitem(i->ranges, i->nItem);
1494 /* the iterator should restart on the next iterator_next */
1500 /* Remove common elements from two iterators */
1501 /* Passed iterators have to point on the first elements */
1502 static BOOL iterator_remove_common_items(ITERATOR *iter1, ITERATOR *iter2)
1504 iterator_next(iter1);
1505 iterator_next(iter2);
1508 if(iter1->nItem==-1 || iter2->nItem==-1)
1511 if(iter1->nItem == iter2->nItem) {
1512 int delete = iter1->nItem;
1514 iterator_prev(iter1);
1515 iterator_prev(iter2);
1516 ranges_delitem(iter1->ranges, delete);
1517 ranges_delitem(iter2->ranges, delete);
1518 iterator_next(iter1);
1519 iterator_next(iter2);
1520 } else if(iter1->nItem > iter2->nItem)
1521 iterator_next(iter2);
1523 iterator_next(iter1);
1526 iter1->nItem = iter1->range.lower = iter1->range.upper = -1;
1527 iter2->nItem = iter2->range.lower = iter2->range.upper = -1;
1531 /******** Misc helper functions ************************************/
1533 static inline LRESULT CallWindowProcT(WNDPROC proc, HWND hwnd, UINT uMsg,
1534 WPARAM wParam, LPARAM lParam, BOOL isW)
1536 if (isW) return CallWindowProcW(proc, hwnd, uMsg, wParam, lParam);
1537 else return CallWindowProcA(proc, hwnd, uMsg, wParam, lParam);
1540 static inline BOOL is_autoarrange(const LISTVIEW_INFO *infoPtr)
1542 return ((infoPtr->dwStyle & LVS_AUTOARRANGE) || infoPtr->bAutoarrange) &&
1543 (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON);
1546 static void toggle_checkbox_state(LISTVIEW_INFO *infoPtr, INT nItem)
1548 DWORD state = STATEIMAGEINDEX(LISTVIEW_GetItemState(infoPtr, nItem, LVIS_STATEIMAGEMASK));
1549 if(state == 1 || state == 2)
1553 lvitem.state = INDEXTOSTATEIMAGEMASK(state);
1554 lvitem.stateMask = LVIS_STATEIMAGEMASK;
1555 LISTVIEW_SetItemState(infoPtr, nItem, &lvitem);
1559 /* this should be called after window style got updated,
1560 it used to reset view state to match current window style */
1561 static inline void map_style_view(LISTVIEW_INFO *infoPtr)
1563 switch (infoPtr->dwStyle & LVS_TYPEMASK)
1566 infoPtr->uView = LV_VIEW_ICON;
1569 infoPtr->uView = LV_VIEW_DETAILS;
1572 infoPtr->uView = LV_VIEW_SMALLICON;
1575 infoPtr->uView = LV_VIEW_LIST;
1579 /* computes next item id value */
1580 static DWORD get_next_itemid(const LISTVIEW_INFO *infoPtr)
1582 INT count = DPA_GetPtrCount(infoPtr->hdpaItemIds);
1586 ITEM_ID *lpID = DPA_GetPtr(infoPtr->hdpaItemIds, count - 1);
1587 return lpID->id + 1;
1592 /******** Internal API functions ************************************/
1594 static inline COLUMN_INFO * LISTVIEW_GetColumnInfo(const LISTVIEW_INFO *infoPtr, INT nSubItem)
1596 static COLUMN_INFO mainItem;
1598 if (nSubItem == 0 && DPA_GetPtrCount(infoPtr->hdpaColumns) == 0) return &mainItem;
1599 assert (nSubItem >= 0 && nSubItem < DPA_GetPtrCount(infoPtr->hdpaColumns));
1601 /* update cached column rectangles */
1602 if (infoPtr->colRectsDirty)
1605 LISTVIEW_INFO *Ptr = (LISTVIEW_INFO*)infoPtr;
1608 for (i = 0; i < DPA_GetPtrCount(infoPtr->hdpaColumns); i++) {
1609 info = DPA_GetPtr(infoPtr->hdpaColumns, i);
1610 SendMessageW(infoPtr->hwndHeader, HDM_GETITEMRECT, i, (LPARAM)&info->rcHeader);
1612 Ptr->colRectsDirty = FALSE;
1615 return DPA_GetPtr(infoPtr->hdpaColumns, nSubItem);
1618 static INT LISTVIEW_CreateHeader(LISTVIEW_INFO *infoPtr)
1620 DWORD dFlags = WS_CHILD | HDS_HORZ | HDS_FULLDRAG | HDS_DRAGDROP;
1623 if (infoPtr->hwndHeader) return 0;
1625 TRACE("Creating header for list %p\n", infoPtr->hwndSelf);
1627 /* setup creation flags */
1628 dFlags |= (LVS_NOSORTHEADER & infoPtr->dwStyle) ? 0 : HDS_BUTTONS;
1629 dFlags |= (LVS_NOCOLUMNHEADER & infoPtr->dwStyle) ? HDS_HIDDEN : 0;
1631 hInst = (HINSTANCE)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_HINSTANCE);
1634 infoPtr->hwndHeader = CreateWindowW(WC_HEADERW, NULL, dFlags,
1635 0, 0, 0, 0, infoPtr->hwndSelf, NULL, hInst, NULL);
1636 if (!infoPtr->hwndHeader) return -1;
1638 /* set header unicode format */
1639 SendMessageW(infoPtr->hwndHeader, HDM_SETUNICODEFORMAT, TRUE, 0);
1641 /* set header font */
1642 SendMessageW(infoPtr->hwndHeader, WM_SETFONT, (WPARAM)infoPtr->hFont, TRUE);
1644 LISTVIEW_UpdateSize(infoPtr);
1649 static inline void LISTVIEW_GetHeaderRect(const LISTVIEW_INFO *infoPtr, INT nSubItem, LPRECT lprc)
1651 *lprc = LISTVIEW_GetColumnInfo(infoPtr, nSubItem)->rcHeader;
1654 static inline BOOL LISTVIEW_IsHeaderEnabled(const LISTVIEW_INFO *infoPtr)
1656 return (infoPtr->uView == LV_VIEW_DETAILS ||
1657 infoPtr->dwLvExStyle & LVS_EX_HEADERINALLVIEWS) &&
1658 !(infoPtr->dwStyle & LVS_NOCOLUMNHEADER);
1661 static inline BOOL LISTVIEW_GetItemW(const LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem)
1663 return LISTVIEW_GetItemT(infoPtr, lpLVItem, TRUE);
1666 /* used to handle collapse main item column case */
1667 static inline BOOL LISTVIEW_DrawFocusRect(const LISTVIEW_INFO *infoPtr, HDC hdc)
1669 return (infoPtr->rcFocus.left < infoPtr->rcFocus.right) ?
1670 DrawFocusRect(hdc, &infoPtr->rcFocus) : FALSE;
1673 /* Listview invalidation functions: use _only_ these functions to invalidate */
1675 static inline BOOL is_redrawing(const LISTVIEW_INFO *infoPtr)
1677 return infoPtr->bRedraw;
1680 static inline void LISTVIEW_InvalidateRect(const LISTVIEW_INFO *infoPtr, const RECT* rect)
1682 if(!is_redrawing(infoPtr)) return;
1683 TRACE(" invalidating rect=%s\n", wine_dbgstr_rect(rect));
1684 InvalidateRect(infoPtr->hwndSelf, rect, TRUE);
1687 static inline void LISTVIEW_InvalidateItem(const LISTVIEW_INFO *infoPtr, INT nItem)
1691 if(!is_redrawing(infoPtr)) return;
1692 LISTVIEW_GetItemBox(infoPtr, nItem, &rcBox);
1693 LISTVIEW_InvalidateRect(infoPtr, &rcBox);
1696 static inline void LISTVIEW_InvalidateSubItem(const LISTVIEW_INFO *infoPtr, INT nItem, INT nSubItem)
1698 POINT Origin, Position;
1701 if(!is_redrawing(infoPtr)) return;
1702 assert (infoPtr->uView == LV_VIEW_DETAILS);
1703 LISTVIEW_GetOrigin(infoPtr, &Origin);
1704 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
1705 LISTVIEW_GetHeaderRect(infoPtr, nSubItem, &rcBox);
1707 rcBox.bottom = infoPtr->nItemHeight;
1708 OffsetRect(&rcBox, Origin.x + Position.x, Origin.y + Position.y);
1709 LISTVIEW_InvalidateRect(infoPtr, &rcBox);
1712 static inline void LISTVIEW_InvalidateList(const LISTVIEW_INFO *infoPtr)
1714 LISTVIEW_InvalidateRect(infoPtr, NULL);
1717 static inline void LISTVIEW_InvalidateColumn(const LISTVIEW_INFO *infoPtr, INT nColumn)
1721 if(!is_redrawing(infoPtr)) return;
1722 LISTVIEW_GetHeaderRect(infoPtr, nColumn, &rcCol);
1723 rcCol.top = infoPtr->rcList.top;
1724 rcCol.bottom = infoPtr->rcList.bottom;
1725 LISTVIEW_InvalidateRect(infoPtr, &rcCol);
1730 * Retrieves the number of items that can fit vertically in the client area.
1733 * [I] infoPtr : valid pointer to the listview structure
1736 * Number of items per row.
1738 static inline INT LISTVIEW_GetCountPerRow(const LISTVIEW_INFO *infoPtr)
1740 INT nListWidth = infoPtr->rcList.right - infoPtr->rcList.left;
1742 return max(nListWidth/(infoPtr->nItemWidth ? infoPtr->nItemWidth : 1), 1);
1747 * Retrieves the number of items that can fit horizontally in the client
1751 * [I] infoPtr : valid pointer to the listview structure
1754 * Number of items per column.
1756 static inline INT LISTVIEW_GetCountPerColumn(const LISTVIEW_INFO *infoPtr)
1758 INT nListHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
1760 return max(nListHeight / infoPtr->nItemHeight, 1);
1764 /*************************************************************************
1765 * LISTVIEW_ProcessLetterKeys
1767 * Processes keyboard messages generated by pressing the letter keys
1769 * What this does is perform a case insensitive search from the
1770 * current position with the following quirks:
1771 * - If two chars or more are pressed in quick succession we search
1772 * for the corresponding string (e.g. 'abc').
1773 * - If there is a delay we wipe away the current search string and
1774 * restart with just that char.
1775 * - If the user keeps pressing the same character, whether slowly or
1776 * fast, so that the search string is entirely composed of this
1777 * character ('aaaaa' for instance), then we search for first item
1778 * that starting with that character.
1779 * - If the user types the above character in quick succession, then
1780 * we must also search for the corresponding string ('aaaaa'), and
1781 * go to that string if there is a match.
1784 * [I] hwnd : handle to the window
1785 * [I] charCode : the character code, the actual character
1786 * [I] keyData : key data
1794 * - The current implementation has a list of characters it will
1795 * accept and it ignores everything else. In particular it will
1796 * ignore accentuated characters which seems to match what
1797 * Windows does. But I'm not sure it makes sense to follow
1799 * - We don't sound a beep when the search fails.
1803 * TREEVIEW_ProcessLetterKeys
1805 static INT LISTVIEW_ProcessLetterKeys(LISTVIEW_INFO *infoPtr, WPARAM charCode, LPARAM keyData)
1807 WCHAR buffer[MAX_PATH];
1808 INT endidx, startidx;
1814 /* simple parameter checking */
1815 if (!charCode || !keyData || infoPtr->nItemCount == 0) return 0;
1817 /* only allow the valid WM_CHARs through */
1818 if (!isalnumW(charCode) &&
1819 charCode != '.' && charCode != '`' && charCode != '!' &&
1820 charCode != '@' && charCode != '#' && charCode != '$' &&
1821 charCode != '%' && charCode != '^' && charCode != '&' &&
1822 charCode != '*' && charCode != '(' && charCode != ')' &&
1823 charCode != '-' && charCode != '_' && charCode != '+' &&
1824 charCode != '=' && charCode != '\\'&& charCode != ']' &&
1825 charCode != '}' && charCode != '[' && charCode != '{' &&
1826 charCode != '/' && charCode != '?' && charCode != '>' &&
1827 charCode != '<' && charCode != ',' && charCode != '~')
1830 /* update the search parameters */
1831 prevTime = infoPtr->lastKeyPressTimestamp;
1832 infoPtr->lastKeyPressTimestamp = GetTickCount();
1833 diff = infoPtr->lastKeyPressTimestamp - prevTime;
1835 if (diff >= 0 && diff < KEY_DELAY)
1837 if (infoPtr->nSearchParamLength < MAX_PATH - 1)
1838 infoPtr->szSearchParam[infoPtr->nSearchParamLength++] = charCode;
1840 if (infoPtr->charCode != charCode)
1841 infoPtr->charCode = charCode = 0;
1845 infoPtr->charCode = charCode;
1846 infoPtr->szSearchParam[0] = charCode;
1847 infoPtr->nSearchParamLength = 1;
1850 /* and search from the current position */
1852 endidx = infoPtr->nItemCount;
1854 /* should start from next after focused item, so next item that matches
1855 will be selected, if there isn't any and focused matches it will be selected
1856 on second search stage from beginning of the list */
1857 if (infoPtr->nFocusedItem >= 0 && infoPtr->nItemCount > 1)
1858 startidx = infoPtr->nFocusedItem + 1;
1862 /* let application handle this for virtual listview */
1863 if (infoPtr->dwStyle & LVS_OWNERDATA)
1867 memset(&nmlv.lvfi, 0, sizeof(nmlv.lvfi));
1868 nmlv.lvfi.flags = (LVFI_WRAP | LVFI_PARTIAL);
1869 nmlv.lvfi.psz = infoPtr->szSearchParam;
1870 nmlv.iStart = startidx;
1872 infoPtr->szSearchParam[infoPtr->nSearchParamLength] = 0;
1874 nItem = notify_hdr(infoPtr, LVN_ODFINDITEMW, (LPNMHDR)&nmlv.hdr);
1880 /* first search in [startidx, endidx), on failure continue in [0, startidx) */
1883 /* start from first item if not found with >= startidx */
1884 if (i == infoPtr->nItemCount && startidx > 0)
1890 for (i = startidx; i < endidx; i++)
1893 item.mask = LVIF_TEXT;
1896 item.pszText = buffer;
1897 item.cchTextMax = MAX_PATH;
1898 if (!LISTVIEW_GetItemW(infoPtr, &item)) return 0;
1900 if (lstrncmpiW(item.pszText, infoPtr->szSearchParam, infoPtr->nSearchParamLength) == 0)
1905 else if (nItem == -1 && lstrncmpiW(item.pszText, infoPtr->szSearchParam, 1) == 0)
1907 /* this would work but we must keep looking for a longer match */
1912 if ( nItem != -1 || /* found something */
1913 endidx != infoPtr->nItemCount || /* second search done */
1914 (startidx == 0 && endidx == infoPtr->nItemCount) /* full range for first search */ )
1920 LISTVIEW_KeySelection(infoPtr, nItem, FALSE);
1925 /*************************************************************************
1926 * LISTVIEW_UpdateHeaderSize [Internal]
1928 * Function to resize the header control
1931 * [I] hwnd : handle to a window
1932 * [I] nNewScrollPos : scroll pos to set
1937 static void LISTVIEW_UpdateHeaderSize(const LISTVIEW_INFO *infoPtr, INT nNewScrollPos)
1942 TRACE("nNewScrollPos=%d\n", nNewScrollPos);
1944 if (!infoPtr->hwndHeader) return;
1946 GetWindowRect(infoPtr->hwndHeader, &winRect);
1947 point[0].x = winRect.left;
1948 point[0].y = winRect.top;
1949 point[1].x = winRect.right;
1950 point[1].y = winRect.bottom;
1952 MapWindowPoints(HWND_DESKTOP, infoPtr->hwndSelf, point, 2);
1953 point[0].x = -nNewScrollPos;
1954 point[1].x += nNewScrollPos;
1956 SetWindowPos(infoPtr->hwndHeader,0,
1957 point[0].x,point[0].y,point[1].x,point[1].y,
1958 (infoPtr->dwStyle & LVS_NOCOLUMNHEADER) ? SWP_HIDEWINDOW : SWP_SHOWWINDOW |
1959 SWP_NOZORDER | SWP_NOACTIVATE);
1964 * Update the scrollbars. This functions should be called whenever
1965 * the content, size or view changes.
1968 * [I] infoPtr : valid pointer to the listview structure
1973 static void LISTVIEW_UpdateScroll(const LISTVIEW_INFO *infoPtr)
1975 SCROLLINFO horzInfo, vertInfo;
1978 if ((infoPtr->dwStyle & LVS_NOSCROLL) || !is_redrawing(infoPtr)) return;
1980 ZeroMemory(&horzInfo, sizeof(SCROLLINFO));
1981 horzInfo.cbSize = sizeof(SCROLLINFO);
1982 horzInfo.nPage = infoPtr->rcList.right - infoPtr->rcList.left;
1984 /* for now, we'll set info.nMax to the _count_, and adjust it later */
1985 if (infoPtr->uView == LV_VIEW_LIST)
1987 INT nPerCol = LISTVIEW_GetCountPerColumn(infoPtr);
1988 horzInfo.nMax = (infoPtr->nItemCount + nPerCol - 1) / nPerCol;
1990 /* scroll by at least one column per page */
1991 if(horzInfo.nPage < infoPtr->nItemWidth)
1992 horzInfo.nPage = infoPtr->nItemWidth;
1994 if (infoPtr->nItemWidth)
1995 horzInfo.nPage /= infoPtr->nItemWidth;
1997 else if (infoPtr->uView == LV_VIEW_DETAILS)
1999 horzInfo.nMax = infoPtr->nItemWidth;
2001 else /* LV_VIEW_ICON, or LV_VIEW_SMALLICON */
2005 if (LISTVIEW_GetViewRect(infoPtr, &rcView)) horzInfo.nMax = rcView.right - rcView.left;
2008 if (LISTVIEW_IsHeaderEnabled(infoPtr))
2010 if (DPA_GetPtrCount(infoPtr->hdpaColumns))
2015 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX,
2016 DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, 0);
2018 LISTVIEW_GetHeaderRect(infoPtr, index, &rcHeader);
2019 horzInfo.nMax = rcHeader.right;
2020 TRACE("horzInfo.nMax=%d\n", horzInfo.nMax);
2024 horzInfo.fMask = SIF_RANGE | SIF_PAGE;
2025 horzInfo.nMax = max(horzInfo.nMax - 1, 0);
2026 dx = GetScrollPos(infoPtr->hwndSelf, SB_HORZ);
2027 dx -= SetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &horzInfo, TRUE);
2028 TRACE("horzInfo=%s\n", debugscrollinfo(&horzInfo));
2030 /* Setting the horizontal scroll can change the listview size
2031 * (and potentially everything else) so we need to recompute
2032 * everything again for the vertical scroll
2035 ZeroMemory(&vertInfo, sizeof(SCROLLINFO));
2036 vertInfo.cbSize = sizeof(SCROLLINFO);
2037 vertInfo.nPage = infoPtr->rcList.bottom - infoPtr->rcList.top;
2039 if (infoPtr->uView == LV_VIEW_DETAILS)
2041 vertInfo.nMax = infoPtr->nItemCount;
2043 /* scroll by at least one page */
2044 if(vertInfo.nPage < infoPtr->nItemHeight)
2045 vertInfo.nPage = infoPtr->nItemHeight;
2047 if (infoPtr->nItemHeight > 0)
2048 vertInfo.nPage /= infoPtr->nItemHeight;
2050 else if (infoPtr->uView != LV_VIEW_LIST) /* LV_VIEW_ICON, or LV_VIEW_SMALLICON */
2054 if (LISTVIEW_GetViewRect(infoPtr, &rcView)) vertInfo.nMax = rcView.bottom - rcView.top;
2057 vertInfo.fMask = SIF_RANGE | SIF_PAGE;
2058 vertInfo.nMax = max(vertInfo.nMax - 1, 0);
2059 dy = GetScrollPos(infoPtr->hwndSelf, SB_VERT);
2060 dy -= SetScrollInfo(infoPtr->hwndSelf, SB_VERT, &vertInfo, TRUE);
2061 TRACE("vertInfo=%s\n", debugscrollinfo(&vertInfo));
2063 /* Change of the range may have changed the scroll pos. If so move the content */
2064 if (dx != 0 || dy != 0)
2067 listRect = infoPtr->rcList;
2068 ScrollWindowEx(infoPtr->hwndSelf, dx, dy, &listRect, &listRect, 0, 0,
2069 SW_ERASE | SW_INVALIDATE);
2072 /* Update the Header Control */
2073 if (infoPtr->hwndHeader)
2075 horzInfo.fMask = SIF_POS;
2076 GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &horzInfo);
2077 LISTVIEW_UpdateHeaderSize(infoPtr, horzInfo.nPos);
2084 * Shows/hides the focus rectangle.
2087 * [I] infoPtr : valid pointer to the listview structure
2088 * [I] fShow : TRUE to show the focus, FALSE to hide it.
2093 static void LISTVIEW_ShowFocusRect(const LISTVIEW_INFO *infoPtr, BOOL fShow)
2097 TRACE("fShow=%d, nItem=%d\n", fShow, infoPtr->nFocusedItem);
2099 if (infoPtr->nFocusedItem < 0) return;
2101 /* we need some gymnastics in ICON mode to handle large items */
2102 if (infoPtr->uView == LV_VIEW_ICON)
2106 LISTVIEW_GetItemBox(infoPtr, infoPtr->nFocusedItem, &rcBox);
2107 if ((rcBox.bottom - rcBox.top) > infoPtr->nItemHeight)
2109 LISTVIEW_InvalidateRect(infoPtr, &rcBox);
2114 if (!(hdc = GetDC(infoPtr->hwndSelf))) return;
2116 /* for some reason, owner draw should work only in report mode */
2117 if ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (infoPtr->uView == LV_VIEW_DETAILS))
2122 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
2123 HFONT hOldFont = SelectObject(hdc, hFont);
2125 item.iItem = infoPtr->nFocusedItem;
2127 item.mask = LVIF_PARAM;
2128 if (!LISTVIEW_GetItemW(infoPtr, &item)) goto done;
2130 ZeroMemory(&dis, sizeof(dis));
2131 dis.CtlType = ODT_LISTVIEW;
2132 dis.CtlID = (UINT)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
2133 dis.itemID = item.iItem;
2134 dis.itemAction = ODA_FOCUS;
2135 if (fShow) dis.itemState |= ODS_FOCUS;
2136 dis.hwndItem = infoPtr->hwndSelf;
2138 LISTVIEW_GetItemBox(infoPtr, dis.itemID, &dis.rcItem);
2139 dis.itemData = item.lParam;
2141 SendMessageW(infoPtr->hwndNotify, WM_DRAWITEM, dis.CtlID, (LPARAM)&dis);
2143 SelectObject(hdc, hOldFont);
2147 LISTVIEW_DrawFocusRect(infoPtr, hdc);
2150 ReleaseDC(infoPtr->hwndSelf, hdc);
2154 * Invalidates all visible selected items.
2156 static void LISTVIEW_InvalidateSelectedItems(const LISTVIEW_INFO *infoPtr)
2160 iterator_frameditems(&i, infoPtr, &infoPtr->rcList);
2161 while(iterator_next(&i))
2163 if (LISTVIEW_GetItemState(infoPtr, i.nItem, LVIS_SELECTED))
2164 LISTVIEW_InvalidateItem(infoPtr, i.nItem);
2166 iterator_destroy(&i);
2171 * DESCRIPTION: [INTERNAL]
2172 * Computes an item's (left,top) corner, relative to rcView.
2173 * That is, the position has NOT been made relative to the Origin.
2174 * This is deliberate, to avoid computing the Origin over, and
2175 * over again, when this function is called in a loop. Instead,
2176 * one can factor the computation of the Origin before the loop,
2177 * and offset the value returned by this function, on every iteration.
2180 * [I] infoPtr : valid pointer to the listview structure
2181 * [I] nItem : item number
2182 * [O] lpptOrig : item top, left corner
2187 static void LISTVIEW_GetItemOrigin(const LISTVIEW_INFO *infoPtr, INT nItem, LPPOINT lpptPosition)
2189 assert(nItem >= 0 && nItem < infoPtr->nItemCount);
2191 if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
2193 lpptPosition->x = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosX, nItem);
2194 lpptPosition->y = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosY, nItem);
2196 else if (infoPtr->uView == LV_VIEW_LIST)
2198 INT nCountPerColumn = LISTVIEW_GetCountPerColumn(infoPtr);
2199 lpptPosition->x = nItem / nCountPerColumn * infoPtr->nItemWidth;
2200 lpptPosition->y = nItem % nCountPerColumn * infoPtr->nItemHeight;
2202 else /* LV_VIEW_DETAILS */
2204 lpptPosition->x = REPORT_MARGINX;
2205 /* item is always at zero indexed column */
2206 if (DPA_GetPtrCount(infoPtr->hdpaColumns) > 0)
2207 lpptPosition->x += LISTVIEW_GetColumnInfo(infoPtr, 0)->rcHeader.left;
2208 lpptPosition->y = nItem * infoPtr->nItemHeight;
2213 * DESCRIPTION: [INTERNAL]
2214 * Compute the rectangles of an item. This is to localize all
2215 * the computations in one place. If you are not interested in some
2216 * of these values, simply pass in a NULL -- the function is smart
2217 * enough to compute only what's necessary. The function computes
2218 * the standard rectangles (BOUNDS, ICON, LABEL) plus a non-standard
2219 * one, the BOX rectangle. This rectangle is very cheap to compute,
2220 * and is guaranteed to contain all the other rectangles. Computing
2221 * the ICON rect is also cheap, but all the others are potentially
2222 * expensive. This gives an easy and effective optimization when
2223 * searching (like point inclusion, or rectangle intersection):
2224 * first test against the BOX, and if TRUE, test against the desired
2226 * If the function does not have all the necessary information
2227 * to computed the requested rectangles, will crash with a
2228 * failed assertion. This is done so we catch all programming
2229 * errors, given that the function is called only from our code.
2231 * We have the following 'special' meanings for a few fields:
2232 * * If LVIS_FOCUSED is set, we assume the item has the focus
2233 * This is important in ICON mode, where it might get a larger
2234 * then usual rectangle
2236 * Please note that subitem support works only in REPORT mode.
2239 * [I] infoPtr : valid pointer to the listview structure
2240 * [I] lpLVItem : item to compute the measures for
2241 * [O] lprcBox : ptr to Box rectangle
2242 * Same as LVM_GETITEMRECT with LVIR_BOUNDS
2243 * [0] lprcSelectBox : ptr to select box rectangle
2244 * Same as LVM_GETITEMRECT with LVIR_SELECTEDBOUNDS
2245 * [O] lprcIcon : ptr to Icon rectangle
2246 * Same as LVM_GETITEMRECT with LVIR_ICON
2247 * [O] lprcStateIcon: ptr to State Icon rectangle
2248 * [O] lprcLabel : ptr to Label rectangle
2249 * Same as LVM_GETITEMRECT with LVIR_LABEL
2254 static void LISTVIEW_GetItemMetrics(const LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem,
2255 LPRECT lprcBox, LPRECT lprcSelectBox,
2256 LPRECT lprcIcon, LPRECT lprcStateIcon, LPRECT lprcLabel)
2258 BOOL doSelectBox = FALSE, doIcon = FALSE, doLabel = FALSE, oversizedBox = FALSE;
2259 RECT Box, SelectBox, Icon, Label;
2260 COLUMN_INFO *lpColumnInfo = NULL;
2261 SIZE labelSize = { 0, 0 };
2263 TRACE("(lpLVItem=%s)\n", debuglvitem_t(lpLVItem, TRUE));
2265 /* Be smart and try to figure out the minimum we have to do */
2266 if (lpLVItem->iSubItem) assert(infoPtr->uView == LV_VIEW_DETAILS);
2267 if (infoPtr->uView == LV_VIEW_ICON && (lprcBox || lprcLabel))
2269 assert((lpLVItem->mask & LVIF_STATE) && (lpLVItem->stateMask & LVIS_FOCUSED));
2270 if (lpLVItem->state & LVIS_FOCUSED) oversizedBox = doLabel = TRUE;
2272 if (lprcSelectBox) doSelectBox = TRUE;
2273 if (lprcLabel) doLabel = TRUE;
2274 if (doLabel || lprcIcon || lprcStateIcon) doIcon = TRUE;
2281 /************************************************************/
2282 /* compute the box rectangle (it should be cheap to do) */
2283 /************************************************************/
2284 if (lpLVItem->iSubItem || infoPtr->uView == LV_VIEW_DETAILS)
2285 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, lpLVItem->iSubItem);
2287 if (lpLVItem->iSubItem)
2289 Box = lpColumnInfo->rcHeader;
2294 Box.right = infoPtr->nItemWidth;
2297 Box.bottom = infoPtr->nItemHeight;
2299 /******************************************************************/
2300 /* compute ICON bounding box (ala LVM_GETITEMRECT) and STATEICON */
2301 /******************************************************************/
2304 LONG state_width = 0;
2306 if (infoPtr->himlState && lpLVItem->iSubItem == 0)
2307 state_width = infoPtr->iconStateSize.cx;
2309 if (infoPtr->uView == LV_VIEW_ICON)
2311 Icon.left = Box.left + state_width;
2312 if (infoPtr->himlNormal)
2313 Icon.left += (infoPtr->nItemWidth - infoPtr->iconSize.cx - state_width) / 2;
2314 Icon.top = Box.top + ICON_TOP_PADDING;
2315 Icon.right = Icon.left;
2316 Icon.bottom = Icon.top;
2317 if (infoPtr->himlNormal)
2319 Icon.right += infoPtr->iconSize.cx;
2320 Icon.bottom += infoPtr->iconSize.cy;
2323 else /* LV_VIEW_SMALLICON, LV_VIEW_LIST or LV_VIEW_DETAILS */
2325 Icon.left = Box.left + state_width;
2327 if (infoPtr->uView == LV_VIEW_DETAILS && lpLVItem->iSubItem == 0)
2329 /* we need the indent in report mode */
2330 assert(lpLVItem->mask & LVIF_INDENT);
2331 Icon.left += infoPtr->iconSize.cx * lpLVItem->iIndent + REPORT_MARGINX;
2335 Icon.right = Icon.left;
2336 if (infoPtr->himlSmall &&
2337 (!lpColumnInfo || lpLVItem->iSubItem == 0 || (lpColumnInfo->fmt & LVCFMT_IMAGE) ||
2338 ((infoPtr->dwLvExStyle & LVS_EX_SUBITEMIMAGES) && lpLVItem->iImage != I_IMAGECALLBACK)))
2339 Icon.right += infoPtr->iconSize.cx;
2340 Icon.bottom = Icon.top + infoPtr->iconSize.cy;
2342 if(lprcIcon) *lprcIcon = Icon;
2343 TRACE(" - icon=%s\n", wine_dbgstr_rect(&Icon));
2345 /* TODO: is this correct? */
2348 lprcStateIcon->left = Icon.left - state_width;
2349 lprcStateIcon->right = Icon.left;
2350 lprcStateIcon->top = Icon.top;
2351 lprcStateIcon->bottom = lprcStateIcon->top + infoPtr->iconSize.cy;
2352 TRACE(" - state icon=%s\n", wine_dbgstr_rect(lprcStateIcon));
2355 else Icon.right = 0;
2357 /************************************************************/
2358 /* compute LABEL bounding box (ala LVM_GETITEMRECT) */
2359 /************************************************************/
2362 /* calculate how far to the right can the label stretch */
2363 Label.right = Box.right;
2364 if (infoPtr->uView == LV_VIEW_DETAILS)
2366 if (lpLVItem->iSubItem == 0)
2368 /* we need a zero based rect here */
2369 Label = lpColumnInfo->rcHeader;
2370 OffsetRect(&Label, -Label.left, 0);
2374 if (lpLVItem->iSubItem || ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && infoPtr->uView == LV_VIEW_DETAILS))
2376 labelSize.cx = infoPtr->nItemWidth;
2377 labelSize.cy = infoPtr->nItemHeight;
2381 /* we need the text in non owner draw mode */
2382 assert(lpLVItem->mask & LVIF_TEXT);
2383 if (is_text(lpLVItem->pszText))
2385 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
2386 HDC hdc = GetDC(infoPtr->hwndSelf);
2387 HFONT hOldFont = SelectObject(hdc, hFont);
2391 /* compute rough rectangle where the label will go */
2392 SetRectEmpty(&rcText);
2393 rcText.right = infoPtr->nItemWidth - TRAILING_LABEL_PADDING;
2394 rcText.bottom = infoPtr->nItemHeight;
2395 if (infoPtr->uView == LV_VIEW_ICON)
2396 rcText.bottom -= ICON_TOP_PADDING + infoPtr->iconSize.cy + ICON_BOTTOM_PADDING;
2398 /* now figure out the flags */
2399 if (infoPtr->uView == LV_VIEW_ICON)
2400 uFormat = oversizedBox ? LV_FL_DT_FLAGS : LV_ML_DT_FLAGS;
2402 uFormat = LV_SL_DT_FLAGS;
2404 DrawTextW (hdc, lpLVItem->pszText, -1, &rcText, uFormat | DT_CALCRECT);
2406 if (rcText.right != rcText.left)
2407 labelSize.cx = min(rcText.right - rcText.left + TRAILING_LABEL_PADDING, infoPtr->nItemWidth);
2409 labelSize.cy = rcText.bottom - rcText.top;
2411 SelectObject(hdc, hOldFont);
2412 ReleaseDC(infoPtr->hwndSelf, hdc);
2416 if (infoPtr->uView == LV_VIEW_ICON)
2418 Label.left = Box.left + (infoPtr->nItemWidth - labelSize.cx) / 2;
2419 Label.top = Box.top + ICON_TOP_PADDING_HITABLE +
2420 infoPtr->iconSize.cy + ICON_BOTTOM_PADDING;
2421 Label.right = Label.left + labelSize.cx;
2422 Label.bottom = Label.top + infoPtr->nItemHeight;
2423 if (!oversizedBox && labelSize.cy > infoPtr->ntmHeight)
2425 labelSize.cy = min(Box.bottom - Label.top, labelSize.cy);
2426 labelSize.cy /= infoPtr->ntmHeight;
2427 labelSize.cy = max(labelSize.cy, 1);
2428 labelSize.cy *= infoPtr->ntmHeight;
2430 Label.bottom = Label.top + labelSize.cy + HEIGHT_PADDING;
2432 else if (infoPtr->uView == LV_VIEW_DETAILS)
2434 Label.left = Icon.right;
2435 Label.top = Box.top;
2436 Label.right = lpLVItem->iSubItem ? lpColumnInfo->rcHeader.right :
2437 lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left;
2438 Label.bottom = Label.top + infoPtr->nItemHeight;
2440 else /* LV_VIEW_SMALLICON or LV_VIEW_LIST */
2442 Label.left = Icon.right;
2443 Label.top = Box.top;
2444 Label.right = min(Label.left + labelSize.cx, Label.right);
2445 Label.bottom = Label.top + infoPtr->nItemHeight;
2448 if (lprcLabel) *lprcLabel = Label;
2449 TRACE(" - label=%s\n", wine_dbgstr_rect(&Label));
2452 /************************************************************/
2453 /* compute SELECT bounding box */
2454 /************************************************************/
2457 if (infoPtr->uView == LV_VIEW_DETAILS)
2459 SelectBox.left = Icon.left;
2460 SelectBox.top = Box.top;
2461 SelectBox.bottom = Box.bottom;
2464 SelectBox.right = min(Label.left + labelSize.cx, Label.right);
2466 SelectBox.right = min(Label.left + MAX_EMPTYTEXT_SELECT_WIDTH, Label.right);
2470 UnionRect(&SelectBox, &Icon, &Label);
2472 if (lprcSelectBox) *lprcSelectBox = SelectBox;
2473 TRACE(" - select box=%s\n", wine_dbgstr_rect(&SelectBox));
2476 /* Fix the Box if necessary */
2479 if (oversizedBox) UnionRect(lprcBox, &Box, &Label);
2480 else *lprcBox = Box;
2482 TRACE(" - box=%s\n", wine_dbgstr_rect(&Box));
2486 * DESCRIPTION: [INTERNAL]
2489 * [I] infoPtr : valid pointer to the listview structure
2490 * [I] nItem : item number
2491 * [O] lprcBox : ptr to Box rectangle
2496 static void LISTVIEW_GetItemBox(const LISTVIEW_INFO *infoPtr, INT nItem, LPRECT lprcBox)
2498 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
2499 POINT Position, Origin;
2502 LISTVIEW_GetOrigin(infoPtr, &Origin);
2503 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
2505 /* Be smart and try to figure out the minimum we have to do */
2507 if (infoPtr->uView == LV_VIEW_ICON && infoPtr->bFocus && LISTVIEW_GetItemState(infoPtr, nItem, LVIS_FOCUSED))
2508 lvItem.mask |= LVIF_TEXT;
2509 lvItem.iItem = nItem;
2510 lvItem.iSubItem = 0;
2511 lvItem.pszText = szDispText;
2512 lvItem.cchTextMax = DISP_TEXT_SIZE;
2513 if (lvItem.mask) LISTVIEW_GetItemW(infoPtr, &lvItem);
2514 if (infoPtr->uView == LV_VIEW_ICON)
2516 lvItem.mask |= LVIF_STATE;
2517 lvItem.stateMask = LVIS_FOCUSED;
2518 lvItem.state = (lvItem.mask & LVIF_TEXT ? LVIS_FOCUSED : 0);
2520 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, lprcBox, 0, 0, 0, 0);
2522 if (infoPtr->uView == LV_VIEW_DETAILS && infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT &&
2523 SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, 0, 0))
2525 OffsetRect(lprcBox, Origin.x, Position.y + Origin.y);
2528 OffsetRect(lprcBox, Position.x + Origin.x, Position.y + Origin.y);
2531 /* LISTVIEW_MapIdToIndex helper */
2532 static INT CALLBACK MapIdSearchCompare(LPVOID p1, LPVOID p2, LPARAM lParam)
2534 ITEM_ID *id1 = (ITEM_ID*)p1;
2535 ITEM_ID *id2 = (ITEM_ID*)p2;
2537 if (id1->id == id2->id) return 0;
2539 return (id1->id < id2->id) ? -1 : 1;
2544 * Returns the item index for id specified.
2547 * [I] infoPtr : valid pointer to the listview structure
2548 * [I] iID : item id to get index for
2551 * Item index, or -1 on failure.
2553 static INT LISTVIEW_MapIdToIndex(const LISTVIEW_INFO *infoPtr, UINT iID)
2558 TRACE("iID=%d\n", iID);
2560 if (infoPtr->dwStyle & LVS_OWNERDATA) return -1;
2561 if (infoPtr->nItemCount == 0) return -1;
2564 index = DPA_Search(infoPtr->hdpaItemIds, &ID, -1, &MapIdSearchCompare, 0, DPAS_SORTED);
2568 ITEM_ID *lpID = DPA_GetPtr(infoPtr->hdpaItemIds, index);
2569 return DPA_GetPtrIndex(infoPtr->hdpaItems, lpID->item);
2577 * Returns the item id for index given.
2580 * [I] infoPtr : valid pointer to the listview structure
2581 * [I] iItem : item index to get id for
2586 static DWORD LISTVIEW_MapIndexToId(const LISTVIEW_INFO *infoPtr, INT iItem)
2591 TRACE("iItem=%d\n", iItem);
2593 if (infoPtr->dwStyle & LVS_OWNERDATA) return -1;
2594 if (iItem < 0 || iItem >= infoPtr->nItemCount) return -1;
2596 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, iItem);
2597 lpItem = DPA_GetPtr(hdpaSubItems, 0);
2599 return lpItem->id->id;
2604 * Returns the current icon position, and advances it along the top.
2605 * The returned position is not offset by Origin.
2608 * [I] infoPtr : valid pointer to the listview structure
2609 * [O] lpPos : will get the current icon position
2614 static void LISTVIEW_NextIconPosTop(LISTVIEW_INFO *infoPtr, LPPOINT lpPos)
2616 INT nListWidth = infoPtr->rcList.right - infoPtr->rcList.left;
2618 *lpPos = infoPtr->currIconPos;
2620 infoPtr->currIconPos.x += infoPtr->nItemWidth;
2621 if (infoPtr->currIconPos.x + infoPtr->nItemWidth <= nListWidth) return;
2623 infoPtr->currIconPos.x = 0;
2624 infoPtr->currIconPos.y += infoPtr->nItemHeight;
2630 * Returns the current icon position, and advances it down the left edge.
2631 * The returned position is not offset by Origin.
2634 * [I] infoPtr : valid pointer to the listview structure
2635 * [O] lpPos : will get the current icon position
2640 static void LISTVIEW_NextIconPosLeft(LISTVIEW_INFO *infoPtr, LPPOINT lpPos)
2642 INT nListHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
2644 *lpPos = infoPtr->currIconPos;
2646 infoPtr->currIconPos.y += infoPtr->nItemHeight;
2647 if (infoPtr->currIconPos.y + infoPtr->nItemHeight <= nListHeight) return;
2649 infoPtr->currIconPos.x += infoPtr->nItemWidth;
2650 infoPtr->currIconPos.y = 0;
2656 * Moves an icon to the specified position.
2657 * It takes care of invalidating the item, etc.
2660 * [I] infoPtr : valid pointer to the listview structure
2661 * [I] nItem : the item to move
2662 * [I] lpPos : the new icon position
2663 * [I] isNew : flags the item as being new
2669 static BOOL LISTVIEW_MoveIconTo(const LISTVIEW_INFO *infoPtr, INT nItem, const POINT *lppt, BOOL isNew)
2675 old.x = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosX, nItem);
2676 old.y = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosY, nItem);
2678 if (lppt->x == old.x && lppt->y == old.y) return TRUE;
2679 LISTVIEW_InvalidateItem(infoPtr, nItem);
2682 /* Allocating a POINTER for every item is too resource intensive,
2683 * so we'll keep the (x,y) in different arrays */
2684 if (!DPA_SetPtr(infoPtr->hdpaPosX, nItem, (void *)(LONG_PTR)lppt->x)) return FALSE;
2685 if (!DPA_SetPtr(infoPtr->hdpaPosY, nItem, (void *)(LONG_PTR)lppt->y)) return FALSE;
2687 LISTVIEW_InvalidateItem(infoPtr, nItem);
2694 * Arranges listview items in icon display mode.
2697 * [I] infoPtr : valid pointer to the listview structure
2698 * [I] nAlignCode : alignment code
2704 static BOOL LISTVIEW_Arrange(LISTVIEW_INFO *infoPtr, INT nAlignCode)
2706 void (*next_pos)(LISTVIEW_INFO *, LPPOINT);
2710 if (infoPtr->uView != LV_VIEW_ICON && infoPtr->uView != LV_VIEW_SMALLICON) return FALSE;
2712 TRACE("nAlignCode=%d\n", nAlignCode);
2714 if (nAlignCode == LVA_DEFAULT)
2716 if (infoPtr->dwStyle & LVS_ALIGNLEFT) nAlignCode = LVA_ALIGNLEFT;
2717 else nAlignCode = LVA_ALIGNTOP;
2722 case LVA_ALIGNLEFT: next_pos = LISTVIEW_NextIconPosLeft; break;
2723 case LVA_ALIGNTOP: next_pos = LISTVIEW_NextIconPosTop; break;
2724 case LVA_SNAPTOGRID: next_pos = LISTVIEW_NextIconPosTop; break; /* FIXME */
2725 default: return FALSE;
2728 infoPtr->bAutoarrange = TRUE;
2729 infoPtr->currIconPos.x = infoPtr->currIconPos.y = 0;
2730 for (i = 0; i < infoPtr->nItemCount; i++)
2732 next_pos(infoPtr, &pos);
2733 LISTVIEW_MoveIconTo(infoPtr, i, &pos, FALSE);
2741 * Retrieves the bounding rectangle of all the items, not offset by Origin.
2742 * For LVS_REPORT always returns empty rectangle.
2745 * [I] infoPtr : valid pointer to the listview structure
2746 * [O] lprcView : bounding rectangle
2752 static void LISTVIEW_GetAreaRect(const LISTVIEW_INFO *infoPtr, LPRECT lprcView)
2756 SetRectEmpty(lprcView);
2758 switch (infoPtr->uView)
2761 case LV_VIEW_SMALLICON:
2762 for (i = 0; i < infoPtr->nItemCount; i++)
2764 x = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosX, i);
2765 y = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosY, i);
2766 lprcView->right = max(lprcView->right, x);
2767 lprcView->bottom = max(lprcView->bottom, y);
2769 if (infoPtr->nItemCount > 0)
2771 lprcView->right += infoPtr->nItemWidth;
2772 lprcView->bottom += infoPtr->nItemHeight;
2777 y = LISTVIEW_GetCountPerColumn(infoPtr);
2778 x = infoPtr->nItemCount / y;
2779 if (infoPtr->nItemCount % y) x++;
2780 lprcView->right = x * infoPtr->nItemWidth;
2781 lprcView->bottom = y * infoPtr->nItemHeight;
2788 * Retrieves the bounding rectangle of all the items.
2791 * [I] infoPtr : valid pointer to the listview structure
2792 * [O] lprcView : bounding rectangle
2798 static BOOL LISTVIEW_GetViewRect(const LISTVIEW_INFO *infoPtr, LPRECT lprcView)
2802 TRACE("(lprcView=%p)\n", lprcView);
2804 if (!lprcView) return FALSE;
2806 LISTVIEW_GetAreaRect(infoPtr, lprcView);
2808 if (infoPtr->uView != LV_VIEW_DETAILS)
2810 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
2811 OffsetRect(lprcView, ptOrigin.x, ptOrigin.y);
2814 TRACE("lprcView=%s\n", wine_dbgstr_rect(lprcView));
2821 * Retrieves the subitem pointer associated with the subitem index.
2824 * [I] hdpaSubItems : DPA handle for a specific item
2825 * [I] nSubItem : index of subitem
2828 * SUCCESS : subitem pointer
2831 static SUBITEM_INFO* LISTVIEW_GetSubItemPtr(HDPA hdpaSubItems, INT nSubItem)
2833 SUBITEM_INFO *lpSubItem;
2836 /* we should binary search here if need be */
2837 for (i = 1; i < DPA_GetPtrCount(hdpaSubItems); i++)
2839 lpSubItem = DPA_GetPtr(hdpaSubItems, i);
2840 if (lpSubItem->iSubItem == nSubItem)
2850 * Calculates the desired item width.
2853 * [I] infoPtr : valid pointer to the listview structure
2856 * The desired item width.
2858 static INT LISTVIEW_CalculateItemWidth(const LISTVIEW_INFO *infoPtr)
2862 TRACE("uView=%d\n", infoPtr->uView);
2864 if (infoPtr->uView == LV_VIEW_ICON)
2865 nItemWidth = infoPtr->iconSpacing.cx;
2866 else if (infoPtr->uView == LV_VIEW_DETAILS)
2868 if (DPA_GetPtrCount(infoPtr->hdpaColumns) > 0)
2873 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX,
2874 DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, 0);
2876 LISTVIEW_GetHeaderRect(infoPtr, index, &rcHeader);
2877 nItemWidth = rcHeader.right;
2880 else /* LV_VIEW_SMALLICON, or LV_VIEW_LIST */
2882 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
2886 lvItem.mask = LVIF_TEXT;
2887 lvItem.iSubItem = 0;
2889 for (i = 0; i < infoPtr->nItemCount; i++)
2892 lvItem.pszText = szDispText;
2893 lvItem.cchTextMax = DISP_TEXT_SIZE;
2894 if (LISTVIEW_GetItemW(infoPtr, &lvItem))
2895 nItemWidth = max(LISTVIEW_GetStringWidthT(infoPtr, lvItem.pszText, TRUE),
2899 if (infoPtr->himlSmall) nItemWidth += infoPtr->iconSize.cx;
2900 if (infoPtr->himlState) nItemWidth += infoPtr->iconStateSize.cx;
2902 nItemWidth = max(DEFAULT_COLUMN_WIDTH, nItemWidth + WIDTH_PADDING);
2910 * Calculates the desired item height.
2913 * [I] infoPtr : valid pointer to the listview structure
2916 * The desired item height.
2918 static INT LISTVIEW_CalculateItemHeight(const LISTVIEW_INFO *infoPtr)
2922 TRACE("uView=%d\n", infoPtr->uView);
2924 if (infoPtr->uView == LV_VIEW_ICON)
2925 nItemHeight = infoPtr->iconSpacing.cy;
2928 nItemHeight = infoPtr->ntmHeight;
2929 if (infoPtr->uView == LV_VIEW_DETAILS && infoPtr->dwLvExStyle & LVS_EX_GRIDLINES)
2931 if (infoPtr->himlState)
2932 nItemHeight = max(nItemHeight, infoPtr->iconStateSize.cy);
2933 if (infoPtr->himlSmall)
2934 nItemHeight = max(nItemHeight, infoPtr->iconSize.cy);
2935 if (infoPtr->himlState || infoPtr->himlSmall)
2936 nItemHeight += HEIGHT_PADDING;
2937 if (infoPtr->nMeasureItemHeight > 0)
2938 nItemHeight = infoPtr->nMeasureItemHeight;
2941 return max(nItemHeight, 1);
2946 * Updates the width, and height of an item.
2949 * [I] infoPtr : valid pointer to the listview structure
2954 static inline void LISTVIEW_UpdateItemSize(LISTVIEW_INFO *infoPtr)
2956 infoPtr->nItemWidth = LISTVIEW_CalculateItemWidth(infoPtr);
2957 infoPtr->nItemHeight = LISTVIEW_CalculateItemHeight(infoPtr);
2963 * Retrieves and saves important text metrics info for the current
2967 * [I] infoPtr : valid pointer to the listview structure
2970 static void LISTVIEW_SaveTextMetrics(LISTVIEW_INFO *infoPtr)
2972 HDC hdc = GetDC(infoPtr->hwndSelf);
2973 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
2974 HFONT hOldFont = SelectObject(hdc, hFont);
2978 if (GetTextMetricsW(hdc, &tm))
2980 infoPtr->ntmHeight = tm.tmHeight;
2981 infoPtr->ntmMaxCharWidth = tm.tmMaxCharWidth;
2984 if (GetTextExtentPoint32A(hdc, "...", 3, &sz))
2985 infoPtr->nEllipsisWidth = sz.cx;
2987 SelectObject(hdc, hOldFont);
2988 ReleaseDC(infoPtr->hwndSelf, hdc);
2990 TRACE("tmHeight=%d\n", infoPtr->ntmHeight);
2995 * A compare function for ranges
2998 * [I] range1 : pointer to range 1;
2999 * [I] range2 : pointer to range 2;
3003 * > 0 : if range 1 > range 2
3004 * < 0 : if range 2 > range 1
3005 * = 0 : if range intersects range 2
3007 static INT CALLBACK ranges_cmp(LPVOID range1, LPVOID range2, LPARAM flags)
3011 if (((RANGE*)range1)->upper <= ((RANGE*)range2)->lower)
3013 else if (((RANGE*)range2)->upper <= ((RANGE*)range1)->lower)
3018 TRACE("range1=%s, range2=%s, cmp=%d\n", debugrange(range1), debugrange(range2), cmp);
3023 #define ranges_check(ranges, desc) if (TRACE_ON(listview)) ranges_assert(ranges, desc, __FUNCTION__, __LINE__)
3025 static void ranges_assert(RANGES ranges, LPCSTR desc, const char *func, int line)
3030 TRACE("*** Checking %s:%d:%s ***\n", func, line, desc);
3032 assert (DPA_GetPtrCount(ranges->hdpa) >= 0);
3033 ranges_dump(ranges);
3034 if (DPA_GetPtrCount(ranges->hdpa) > 0)
3036 prev = DPA_GetPtr(ranges->hdpa, 0);
3037 assert (prev->lower >= 0 && prev->lower < prev->upper);
3038 for (i = 1; i < DPA_GetPtrCount(ranges->hdpa); i++)
3040 curr = DPA_GetPtr(ranges->hdpa, i);
3041 assert (prev->upper <= curr->lower);
3042 assert (curr->lower < curr->upper);
3046 TRACE("--- Done checking---\n");
3049 static RANGES ranges_create(int count)
3051 RANGES ranges = Alloc(sizeof(struct tagRANGES));
3052 if (!ranges) return NULL;
3053 ranges->hdpa = DPA_Create(count);
3054 if (ranges->hdpa) return ranges;
3059 static void ranges_clear(RANGES ranges)
3063 for(i = 0; i < DPA_GetPtrCount(ranges->hdpa); i++)
3064 Free(DPA_GetPtr(ranges->hdpa, i));
3065 DPA_DeleteAllPtrs(ranges->hdpa);
3069 static void ranges_destroy(RANGES ranges)
3071 if (!ranges) return;
3072 ranges_clear(ranges);
3073 DPA_Destroy(ranges->hdpa);
3077 static RANGES ranges_clone(RANGES ranges)
3082 if (!(clone = ranges_create(DPA_GetPtrCount(ranges->hdpa)))) goto fail;
3084 for (i = 0; i < DPA_GetPtrCount(ranges->hdpa); i++)
3086 RANGE *newrng = Alloc(sizeof(RANGE));
3087 if (!newrng) goto fail;
3088 *newrng = *((RANGE*)DPA_GetPtr(ranges->hdpa, i));
3089 DPA_SetPtr(clone->hdpa, i, newrng);
3094 TRACE ("clone failed\n");
3095 ranges_destroy(clone);
3099 static RANGES ranges_diff(RANGES ranges, RANGES sub)
3103 for (i = 0; i < DPA_GetPtrCount(sub->hdpa); i++)
3104 ranges_del(ranges, *((RANGE *)DPA_GetPtr(sub->hdpa, i)));
3109 static void ranges_dump(RANGES ranges)
3113 for (i = 0; i < DPA_GetPtrCount(ranges->hdpa); i++)
3114 TRACE(" %s\n", debugrange(DPA_GetPtr(ranges->hdpa, i)));
3117 static inline BOOL ranges_contain(RANGES ranges, INT nItem)
3119 RANGE srchrng = { nItem, nItem + 1 };
3121 TRACE("(nItem=%d)\n", nItem);
3122 ranges_check(ranges, "before contain");
3123 return DPA_Search(ranges->hdpa, &srchrng, 0, ranges_cmp, 0, DPAS_SORTED) != -1;
3126 static INT ranges_itemcount(RANGES ranges)
3130 for (i = 0; i < DPA_GetPtrCount(ranges->hdpa); i++)
3132 RANGE *sel = DPA_GetPtr(ranges->hdpa, i);
3133 count += sel->upper - sel->lower;
3139 static BOOL ranges_shift(RANGES ranges, INT nItem, INT delta, INT nUpper)
3141 RANGE srchrng = { nItem, nItem + 1 }, *chkrng;
3144 index = DPA_Search(ranges->hdpa, &srchrng, 0, ranges_cmp, 0, DPAS_SORTED | DPAS_INSERTAFTER);
3145 if (index == -1) return TRUE;
3147 for (; index < DPA_GetPtrCount(ranges->hdpa); index++)
3149 chkrng = DPA_GetPtr(ranges->hdpa, index);
3150 if (chkrng->lower >= nItem)
3151 chkrng->lower = max(min(chkrng->lower + delta, nUpper - 1), 0);
3152 if (chkrng->upper > nItem)
3153 chkrng->upper = max(min(chkrng->upper + delta, nUpper), 0);
3158 static BOOL ranges_add(RANGES ranges, RANGE range)
3163 TRACE("(%s)\n", debugrange(&range));
3164 ranges_check(ranges, "before add");
3166 /* try find overlapping regions first */
3167 srchrgn.lower = range.lower - 1;
3168 srchrgn.upper = range.upper + 1;
3169 index = DPA_Search(ranges->hdpa, &srchrgn, 0, ranges_cmp, 0, DPAS_SORTED);
3175 TRACE("Adding new range\n");
3177 /* create the brand new range to insert */
3178 newrgn = Alloc(sizeof(RANGE));
3179 if(!newrgn) goto fail;
3182 /* figure out where to insert it */
3183 index = DPA_Search(ranges->hdpa, newrgn, 0, ranges_cmp, 0, DPAS_SORTED | DPAS_INSERTAFTER);
3184 TRACE("index=%d\n", index);
3185 if (index == -1) index = 0;
3187 /* and get it over with */
3188 if (DPA_InsertPtr(ranges->hdpa, index, newrgn) == -1)
3196 RANGE *chkrgn, *mrgrgn;
3197 INT fromindex, mergeindex;
3199 chkrgn = DPA_GetPtr(ranges->hdpa, index);
3200 TRACE("Merge with %s @%d\n", debugrange(chkrgn), index);
3202 chkrgn->lower = min(range.lower, chkrgn->lower);
3203 chkrgn->upper = max(range.upper, chkrgn->upper);
3205 TRACE("New range %s @%d\n", debugrange(chkrgn), index);
3207 /* merge now common ranges */
3209 srchrgn.lower = chkrgn->lower - 1;
3210 srchrgn.upper = chkrgn->upper + 1;
3214 mergeindex = DPA_Search(ranges->hdpa, &srchrgn, fromindex, ranges_cmp, 0, 0);
3215 if (mergeindex == -1) break;
3216 if (mergeindex == index)
3218 fromindex = index + 1;
3222 TRACE("Merge with index %i\n", mergeindex);
3224 mrgrgn = DPA_GetPtr(ranges->hdpa, mergeindex);
3225 chkrgn->lower = min(chkrgn->lower, mrgrgn->lower);
3226 chkrgn->upper = max(chkrgn->upper, mrgrgn->upper);
3228 DPA_DeletePtr(ranges->hdpa, mergeindex);
3229 if (mergeindex < index) index --;
3233 ranges_check(ranges, "after add");
3237 ranges_check(ranges, "failed add");
3241 static BOOL ranges_del(RANGES ranges, RANGE range)
3246 TRACE("(%s)\n", debugrange(&range));
3247 ranges_check(ranges, "before del");
3249 /* we don't use DPAS_SORTED here, since we need *
3250 * to find the first overlapping range */
3251 index = DPA_Search(ranges->hdpa, &range, 0, ranges_cmp, 0, 0);
3254 chkrgn = DPA_GetPtr(ranges->hdpa, index);
3256 TRACE("Matches range %s @%d\n", debugrange(chkrgn), index);
3258 /* case 1: Same range */
3259 if ( (chkrgn->upper == range.upper) &&
3260 (chkrgn->lower == range.lower) )
3262 DPA_DeletePtr(ranges->hdpa, index);
3266 /* case 2: engulf */
3267 else if ( (chkrgn->upper <= range.upper) &&
3268 (chkrgn->lower >= range.lower) )
3270 DPA_DeletePtr(ranges->hdpa, index);
3273 /* case 3: overlap upper */
3274 else if ( (chkrgn->upper <= range.upper) &&
3275 (chkrgn->lower < range.lower) )
3277 chkrgn->upper = range.lower;
3279 /* case 4: overlap lower */
3280 else if ( (chkrgn->upper > range.upper) &&
3281 (chkrgn->lower >= range.lower) )
3283 chkrgn->lower = range.upper;
3286 /* case 5: fully internal */
3291 if (!(newrgn = Alloc(sizeof(RANGE)))) goto fail;
3292 newrgn->lower = chkrgn->lower;
3293 newrgn->upper = range.lower;
3294 chkrgn->lower = range.upper;
3295 if (DPA_InsertPtr(ranges->hdpa, index, newrgn) == -1)
3303 index = DPA_Search(ranges->hdpa, &range, index, ranges_cmp, 0, 0);
3306 ranges_check(ranges, "after del");
3310 ranges_check(ranges, "failed del");
3316 * Removes all selection ranges
3319 * [I] infoPtr : valid pointer to the listview structure
3320 * [I] toSkip : item range to skip removing the selection
3326 static BOOL LISTVIEW_DeselectAllSkipItems(LISTVIEW_INFO *infoPtr, RANGES toSkip)
3335 lvItem.stateMask = LVIS_SELECTED;
3337 /* need to clone the DPA because callbacks can change it */
3338 if (!(clone = ranges_clone(infoPtr->selectionRanges))) return FALSE;
3339 iterator_rangesitems(&i, ranges_diff(clone, toSkip));
3340 while(iterator_next(&i))
3341 LISTVIEW_SetItemState(infoPtr, i.nItem, &lvItem);
3342 /* note that the iterator destructor will free the cloned range */
3343 iterator_destroy(&i);
3348 static inline BOOL LISTVIEW_DeselectAllSkipItem(LISTVIEW_INFO *infoPtr, INT nItem)
3352 if (!(toSkip = ranges_create(1))) return FALSE;
3353 if (nItem != -1) ranges_additem(toSkip, nItem);
3354 LISTVIEW_DeselectAllSkipItems(infoPtr, toSkip);
3355 ranges_destroy(toSkip);
3359 static inline BOOL LISTVIEW_DeselectAll(LISTVIEW_INFO *infoPtr)
3361 return LISTVIEW_DeselectAllSkipItem(infoPtr, -1);
3366 * Retrieves the number of items that are marked as selected.
3369 * [I] infoPtr : valid pointer to the listview structure
3372 * Number of items selected.
3374 static INT LISTVIEW_GetSelectedCount(const LISTVIEW_INFO *infoPtr)
3376 INT nSelectedCount = 0;
3378 if (infoPtr->uCallbackMask & LVIS_SELECTED)
3381 for (i = 0; i < infoPtr->nItemCount; i++)
3383 if (LISTVIEW_GetItemState(infoPtr, i, LVIS_SELECTED))
3388 nSelectedCount = ranges_itemcount(infoPtr->selectionRanges);
3390 TRACE("nSelectedCount=%d\n", nSelectedCount);
3391 return nSelectedCount;
3396 * Manages the item focus.
3399 * [I] infoPtr : valid pointer to the listview structure
3400 * [I] nItem : item index
3403 * TRUE : focused item changed
3404 * FALSE : focused item has NOT changed
3406 static inline BOOL LISTVIEW_SetItemFocus(LISTVIEW_INFO *infoPtr, INT nItem)
3408 INT oldFocus = infoPtr->nFocusedItem;
3411 if (nItem == infoPtr->nFocusedItem) return FALSE;
3413 lvItem.state = nItem == -1 ? 0 : LVIS_FOCUSED;
3414 lvItem.stateMask = LVIS_FOCUSED;
3415 LISTVIEW_SetItemState(infoPtr, nItem == -1 ? infoPtr->nFocusedItem : nItem, &lvItem);
3417 return oldFocus != infoPtr->nFocusedItem;
3420 /* Helper function for LISTVIEW_ShiftIndices *only* */
3421 static INT shift_item(const LISTVIEW_INFO *infoPtr, INT nShiftItem, INT nItem, INT direction)
3423 if (nShiftItem < nItem) return nShiftItem;
3425 if (nShiftItem > nItem) return nShiftItem + direction;
3427 if (direction > 0) return nShiftItem + direction;
3429 return min(nShiftItem, infoPtr->nItemCount - 1);
3434 * Updates the various indices after an item has been inserted or deleted.
3437 * [I] infoPtr : valid pointer to the listview structure
3438 * [I] nItem : item index
3439 * [I] direction : Direction of shift, +1 or -1.
3444 static void LISTVIEW_ShiftIndices(LISTVIEW_INFO *infoPtr, INT nItem, INT direction)
3449 /* temporarily disable change notification while shifting items */
3450 bOldChange = infoPtr->bDoChangeNotify;
3451 infoPtr->bDoChangeNotify = FALSE;
3453 TRACE("Shifting %iu, %i steps\n", nItem, direction);
3455 ranges_shift(infoPtr->selectionRanges, nItem, direction, infoPtr->nItemCount);
3457 assert(abs(direction) == 1);
3459 infoPtr->nSelectionMark = shift_item(infoPtr, infoPtr->nSelectionMark, nItem, direction);
3461 nNewFocus = shift_item(infoPtr, infoPtr->nFocusedItem, nItem, direction);
3462 if (nNewFocus != infoPtr->nFocusedItem)
3463 LISTVIEW_SetItemFocus(infoPtr, nNewFocus);
3465 /* But we are not supposed to modify nHotItem! */
3467 infoPtr->bDoChangeNotify = bOldChange;
3473 * Adds a block of selections.
3476 * [I] infoPtr : valid pointer to the listview structure
3477 * [I] nItem : item index
3480 * Whether the window is still valid.
3482 static BOOL LISTVIEW_AddGroupSelection(LISTVIEW_INFO *infoPtr, INT nItem)
3484 INT nFirst = min(infoPtr->nSelectionMark, nItem);
3485 INT nLast = max(infoPtr->nSelectionMark, nItem);
3486 HWND hwndSelf = infoPtr->hwndSelf;
3487 NMLVODSTATECHANGE nmlv;
3492 /* Temporarily disable change notification
3493 * If the control is LVS_OWNERDATA, we need to send
3494 * only one LVN_ODSTATECHANGED notification.
3495 * See MSDN documentation for LVN_ITEMCHANGED.
3497 bOldChange = infoPtr->bDoChangeNotify;
3498 if (infoPtr->dwStyle & LVS_OWNERDATA) infoPtr->bDoChangeNotify = FALSE;
3500 if (nFirst == -1) nFirst = nItem;
3502 item.state = LVIS_SELECTED;
3503 item.stateMask = LVIS_SELECTED;
3505 for (i = nFirst; i <= nLast; i++)
3506 LISTVIEW_SetItemState(infoPtr,i,&item);
3508 ZeroMemory(&nmlv, sizeof(nmlv));
3509 nmlv.iFrom = nFirst;
3512 nmlv.uOldState = item.state;
3514 notify_hdr(infoPtr, LVN_ODSTATECHANGED, (LPNMHDR)&nmlv);
3515 if (!IsWindow(hwndSelf))
3517 infoPtr->bDoChangeNotify = bOldChange;
3524 * Sets a single group selection.
3527 * [I] infoPtr : valid pointer to the listview structure
3528 * [I] nItem : item index
3533 static void LISTVIEW_SetGroupSelection(LISTVIEW_INFO *infoPtr, INT nItem)
3540 if (!(selection = ranges_create(100))) return;
3542 item.state = LVIS_SELECTED;
3543 item.stateMask = LVIS_SELECTED;
3545 if ((infoPtr->uView == LV_VIEW_LIST) || (infoPtr->uView == LV_VIEW_DETAILS))
3547 if (infoPtr->nSelectionMark == -1)
3549 infoPtr->nSelectionMark = nItem;
3550 ranges_additem(selection, nItem);
3556 sel.lower = min(infoPtr->nSelectionMark, nItem);
3557 sel.upper = max(infoPtr->nSelectionMark, nItem) + 1;
3558 ranges_add(selection, sel);
3563 RECT rcItem, rcSel, rcSelMark;
3566 rcItem.left = LVIR_BOUNDS;
3567 if (!LISTVIEW_GetItemRect(infoPtr, nItem, &rcItem)) return;
3568 rcSelMark.left = LVIR_BOUNDS;
3569 if (!LISTVIEW_GetItemRect(infoPtr, infoPtr->nSelectionMark, &rcSelMark)) return;
3570 UnionRect(&rcSel, &rcItem, &rcSelMark);
3571 iterator_frameditems(&i, infoPtr, &rcSel);
3572 while(iterator_next(&i))
3574 LISTVIEW_GetItemPosition(infoPtr, i.nItem, &ptItem);
3575 if (PtInRect(&rcSel, ptItem)) ranges_additem(selection, i.nItem);
3577 iterator_destroy(&i);
3580 /* disable per item notifications on LVS_OWNERDATA style
3581 FIXME: single LVN_ODSTATECHANGED should be used */
3582 bOldChange = infoPtr->bDoChangeNotify;
3583 if (infoPtr->dwStyle & LVS_OWNERDATA) infoPtr->bDoChangeNotify = FALSE;
3585 LISTVIEW_DeselectAllSkipItems(infoPtr, selection);
3588 iterator_rangesitems(&i, selection);
3589 while(iterator_next(&i))
3590 LISTVIEW_SetItemState(infoPtr, i.nItem, &item);
3591 /* this will also destroy the selection */
3592 iterator_destroy(&i);
3594 infoPtr->bDoChangeNotify = bOldChange;
3596 LISTVIEW_SetItemFocus(infoPtr, nItem);
3601 * Sets a single selection.
3604 * [I] infoPtr : valid pointer to the listview structure
3605 * [I] nItem : item index
3610 static void LISTVIEW_SetSelection(LISTVIEW_INFO *infoPtr, INT nItem)
3614 TRACE("nItem=%d\n", nItem);
3616 LISTVIEW_DeselectAllSkipItem(infoPtr, nItem);
3618 lvItem.state = LVIS_FOCUSED | LVIS_SELECTED;
3619 lvItem.stateMask = LVIS_FOCUSED | LVIS_SELECTED;
3620 LISTVIEW_SetItemState(infoPtr, nItem, &lvItem);
3622 infoPtr->nSelectionMark = nItem;
3627 * Set selection(s) with keyboard.
3630 * [I] infoPtr : valid pointer to the listview structure
3631 * [I] nItem : item index
3632 * [I] space : VK_SPACE code sent
3635 * SUCCESS : TRUE (needs to be repainted)
3636 * FAILURE : FALSE (nothing has changed)
3638 static BOOL LISTVIEW_KeySelection(LISTVIEW_INFO *infoPtr, INT nItem, BOOL space)
3640 /* FIXME: pass in the state */
3641 WORD wShift = HIWORD(GetKeyState(VK_SHIFT));
3642 WORD wCtrl = HIWORD(GetKeyState(VK_CONTROL));
3643 BOOL bResult = FALSE;
3645 TRACE("nItem=%d, wShift=%d, wCtrl=%d\n", nItem, wShift, wCtrl);
3646 if ((nItem >= 0) && (nItem < infoPtr->nItemCount))
3650 if (infoPtr->dwStyle & LVS_SINGLESEL || (wShift == 0 && wCtrl == 0))
3651 LISTVIEW_SetSelection(infoPtr, nItem);
3655 LISTVIEW_SetGroupSelection(infoPtr, nItem);
3659 lvItem.state = ~LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED);
3660 lvItem.stateMask = LVIS_SELECTED;
3663 LISTVIEW_SetItemState(infoPtr, nItem, &lvItem);
3664 if (lvItem.state & LVIS_SELECTED)
3665 infoPtr->nSelectionMark = nItem;
3667 bResult = LISTVIEW_SetItemFocus(infoPtr, nItem);
3670 LISTVIEW_EnsureVisible(infoPtr, nItem, FALSE);
3673 UpdateWindow(infoPtr->hwndSelf); /* update client area */
3677 static BOOL LISTVIEW_GetItemAtPt(const LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem, POINT pt)
3679 LVHITTESTINFO lvHitTestInfo;
3681 ZeroMemory(&lvHitTestInfo, sizeof(lvHitTestInfo));
3682 lvHitTestInfo.pt.x = pt.x;
3683 lvHitTestInfo.pt.y = pt.y;
3685 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
3687 lpLVItem->mask = LVIF_PARAM;
3688 lpLVItem->iItem = lvHitTestInfo.iItem;
3689 lpLVItem->iSubItem = 0;
3691 return LISTVIEW_GetItemT(infoPtr, lpLVItem, TRUE);
3694 static inline BOOL LISTVIEW_IsHotTracking(const LISTVIEW_INFO *infoPtr)
3696 return ((infoPtr->dwLvExStyle & LVS_EX_TRACKSELECT) ||
3697 (infoPtr->dwLvExStyle & LVS_EX_ONECLICKACTIVATE) ||
3698 (infoPtr->dwLvExStyle & LVS_EX_TWOCLICKACTIVATE));
3703 * Called when the mouse is being actively tracked and has hovered for a specified
3707 * [I] infoPtr : valid pointer to the listview structure
3708 * [I] fwKeys : key indicator
3709 * [I] x,y : mouse position
3712 * 0 if the message was processed, non-zero if there was an error
3715 * LVS_EX_TRACKSELECT: An item is automatically selected when the cursor remains
3716 * over the item for a certain period of time.
3719 static LRESULT LISTVIEW_MouseHover(LISTVIEW_INFO *infoPtr, INT x, INT y)
3723 if (notify_hdr(infoPtr, NM_HOVER, &hdr)) return 0;
3725 if (LISTVIEW_IsHotTracking(infoPtr))
3733 if (LISTVIEW_GetItemAtPt(infoPtr, &item, pt))
3734 LISTVIEW_SetSelection(infoPtr, item.iItem);
3736 SetFocus(infoPtr->hwndSelf);
3742 #define SCROLL_LEFT 0x1
3743 #define SCROLL_RIGHT 0x2
3744 #define SCROLL_UP 0x4
3745 #define SCROLL_DOWN 0x8
3749 * Utility routine to draw and highlight items within a marquee selection rectangle.
3752 * [I] infoPtr : valid pointer to the listview structure
3753 * [I] coords_orig : original co-ordinates of the cursor
3754 * [I] coords_offs : offsetted coordinates of the cursor
3755 * [I] offset : offset amount
3756 * [I] scroll : Bitmask of which directions we should scroll, if at all
3761 static void LISTVIEW_MarqueeHighlight(LISTVIEW_INFO *infoPtr, const POINT *coords_orig,
3762 const POINT *coords_offs, const POINT *offset,
3765 BOOL controlDown = FALSE;
3767 ITERATOR old_elems, new_elems;
3770 if (coords_offs->x > infoPtr->marqueeOrigin.x)
3772 rect.left = infoPtr->marqueeOrigin.x;
3773 rect.right = coords_offs->x;
3777 rect.left = coords_offs->x;
3778 rect.right = infoPtr->marqueeOrigin.x;
3781 if (coords_offs->y > infoPtr->marqueeOrigin.y)
3783 rect.top = infoPtr->marqueeOrigin.y;
3784 rect.bottom = coords_offs->y;
3788 rect.top = coords_offs->y;
3789 rect.bottom = infoPtr->marqueeOrigin.y;
3792 /* Cancel out the old marquee rectangle and draw the new one */
3793 LISTVIEW_InvalidateRect(infoPtr, &infoPtr->marqueeDrawRect);
3795 /* Scroll by the appropriate distance if applicable - speed up scrolling as
3796 the cursor is further away */
3798 if ((scroll & SCROLL_LEFT) && (coords_orig->x <= 0))
3799 LISTVIEW_Scroll(infoPtr, coords_orig->x, 0);
3801 if ((scroll & SCROLL_RIGHT) && (coords_orig->x >= infoPtr->rcList.right))
3802 LISTVIEW_Scroll(infoPtr, (coords_orig->x - infoPtr->rcList.right), 0);
3804 if ((scroll & SCROLL_UP) && (coords_orig->y <= 0))
3805 LISTVIEW_Scroll(infoPtr, 0, coords_orig->y);
3807 if ((scroll & SCROLL_DOWN) && (coords_orig->y >= infoPtr->rcList.bottom))
3808 LISTVIEW_Scroll(infoPtr, 0, (coords_orig->y - infoPtr->rcList.bottom));
3810 iterator_frameditems_absolute(&old_elems, infoPtr, &infoPtr->marqueeRect);
3812 CopyRect(&infoPtr->marqueeRect, &rect);
3814 CopyRect(&infoPtr->marqueeDrawRect, &rect);
3815 OffsetRect(&infoPtr->marqueeDrawRect, offset->x, offset->y);
3817 iterator_frameditems_absolute(&new_elems, infoPtr, &infoPtr->marqueeRect);
3818 iterator_remove_common_items(&old_elems, &new_elems);
3820 /* Iterate over no longer selected items */
3821 while (iterator_next(&old_elems))
3823 if (old_elems.nItem > -1)
3825 if (LISTVIEW_GetItemState(infoPtr, old_elems.nItem, LVIS_SELECTED) == LVIS_SELECTED)
3828 item.state = LVIS_SELECTED;
3830 item.stateMask = LVIS_SELECTED;
3832 LISTVIEW_SetItemState(infoPtr, old_elems.nItem, &item);
3835 iterator_destroy(&old_elems);
3838 /* Iterate over newly selected items */
3839 if (GetKeyState(VK_CONTROL) & 0x8000)
3842 while (iterator_next(&new_elems))
3844 if (new_elems.nItem > -1)
3846 /* If CTRL is pressed, invert. If not, always select the item. */
3847 if ((controlDown) && (LISTVIEW_GetItemState(infoPtr, new_elems.nItem, LVIS_SELECTED)))
3850 item.state = LVIS_SELECTED;
3852 item.stateMask = LVIS_SELECTED;
3854 LISTVIEW_SetItemState(infoPtr, new_elems.nItem, &item);
3857 iterator_destroy(&new_elems);
3859 LISTVIEW_InvalidateRect(infoPtr, &rect);
3864 * Called when we are in a marquee selection that involves scrolling the listview (ie,
3865 * the cursor is outside the bounds of the client area). This is a TIMERPROC.
3868 * [I] hwnd : Handle to the listview
3869 * [I] uMsg : WM_TIMER (ignored)
3870 * [I] idEvent : The timer ID interpreted as a pointer to a LISTVIEW_INFO struct
3871 * [I] dwTimer : The elapsed time (ignored)
3876 static VOID CALLBACK LISTVIEW_ScrollTimer(HWND hWnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
3878 LISTVIEW_INFO *infoPtr;
3879 SCROLLINFO scrollInfo;
3885 infoPtr = (LISTVIEW_INFO *) idEvent;
3890 /* Get the current cursor position and convert to client coordinates */
3891 GetCursorPos(&coords_orig);
3892 ScreenToClient(hWnd, &coords_orig);
3894 /* Ensure coordinates are within client bounds */
3895 coords_offs.x = max(min(coords_orig.x, infoPtr->rcList.right), 0);
3896 coords_offs.y = max(min(coords_orig.y, infoPtr->rcList.bottom), 0);
3899 LISTVIEW_GetOrigin(infoPtr, &offset);
3901 /* Offset coordinates by the appropriate amount */
3902 coords_offs.x -= offset.x;
3903 coords_offs.y -= offset.y;
3905 scrollInfo.cbSize = sizeof(SCROLLINFO);
3906 scrollInfo.fMask = SIF_ALL;
3908 /* Work out in which directions we can scroll */
3909 if (GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
3911 if (scrollInfo.nPos != scrollInfo.nMin)
3912 scroll |= SCROLL_UP;
3914 if (((scrollInfo.nPage + scrollInfo.nPos) - 1) != scrollInfo.nMax)
3915 scroll |= SCROLL_DOWN;
3918 if (GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo))
3920 if (scrollInfo.nPos != scrollInfo.nMin)
3921 scroll |= SCROLL_LEFT;
3923 if (((scrollInfo.nPage + scrollInfo.nPos) - 1) != scrollInfo.nMax)
3924 scroll |= SCROLL_RIGHT;
3927 if (((coords_orig.x <= 0) && (scroll & SCROLL_LEFT)) ||
3928 ((coords_orig.y <= 0) && (scroll & SCROLL_UP)) ||
3929 ((coords_orig.x >= infoPtr->rcList.right) && (scroll & SCROLL_RIGHT)) ||
3930 ((coords_orig.y >= infoPtr->rcList.bottom) && (scroll & SCROLL_DOWN)))
3932 LISTVIEW_MarqueeHighlight(infoPtr, &coords_orig, &coords_offs, &offset, scroll);
3938 * Called whenever WM_MOUSEMOVE is received.
3941 * [I] infoPtr : valid pointer to the listview structure
3942 * [I] fwKeys : key indicator
3943 * [I] x,y : mouse position
3946 * 0 if the message is processed, non-zero if there was an error
3948 static LRESULT LISTVIEW_MouseMove(LISTVIEW_INFO *infoPtr, WORD fwKeys, INT x, INT y)
3950 if (!(fwKeys & MK_LBUTTON))
3951 infoPtr->bLButtonDown = FALSE;
3953 if (infoPtr->bLButtonDown)
3957 LVHITTESTINFO lvHitTestInfo;
3958 WORD wDragWidth = GetSystemMetrics(SM_CXDRAG);
3959 WORD wDragHeight= GetSystemMetrics(SM_CYDRAG);
3961 if (infoPtr->bMarqueeSelect)
3971 LISTVIEW_GetOrigin(infoPtr, &offset);
3973 /* Ensure coordinates are within client bounds */
3974 coords_offs.x = max(min(x, infoPtr->rcList.right), 0);
3975 coords_offs.y = max(min(y, infoPtr->rcList.bottom), 0);
3977 /* Offset coordinates by the appropriate amount */
3978 coords_offs.x -= offset.x;
3979 coords_offs.y -= offset.y;
3981 /* Enable the timer if we're going outside our bounds, in case the user doesn't
3982 move the mouse again */
3984 if ((x <= 0) || (y <= 0) || (x >= infoPtr->rcList.right) ||
3985 (y >= infoPtr->rcList.bottom))
3987 if (!infoPtr->bScrolling)
3989 infoPtr->bScrolling = TRUE;
3990 SetTimer(infoPtr->hwndSelf, (UINT_PTR) infoPtr, 1, LISTVIEW_ScrollTimer);
3995 infoPtr->bScrolling = FALSE;
3996 KillTimer(infoPtr->hwndSelf, (UINT_PTR) infoPtr);
3999 LISTVIEW_MarqueeHighlight(infoPtr, &coords_orig, &coords_offs, &offset, 0);
4003 rect.left = infoPtr->ptClickPos.x - wDragWidth;
4004 rect.right = infoPtr->ptClickPos.x + wDragWidth;
4005 rect.top = infoPtr->ptClickPos.y - wDragHeight;
4006 rect.bottom = infoPtr->ptClickPos.y + wDragHeight;
4011 lvHitTestInfo.pt = tmp;
4012 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, TRUE);
4014 /* reset item marker */
4015 if (infoPtr->nLButtonDownItem != lvHitTestInfo.iItem)
4016 infoPtr->nLButtonDownItem = -1;
4018 if (!PtInRect(&rect, tmp))
4020 /* this path covers the following:
4021 1. WM_LBUTTONDOWN over selected item (sets focus on it)
4022 2. change focus with keys
4023 3. move mouse over item from step 1 selects it and moves focus on it */
4024 if (infoPtr->nLButtonDownItem != -1 &&
4025 !LISTVIEW_GetItemState(infoPtr, infoPtr->nLButtonDownItem, LVIS_SELECTED))
4029 lvItem.state = LVIS_FOCUSED | LVIS_SELECTED;
4030 lvItem.stateMask = LVIS_FOCUSED | LVIS_SELECTED;
4032 LISTVIEW_SetItemState(infoPtr, infoPtr->nLButtonDownItem, &lvItem);
4033 infoPtr->nLButtonDownItem = -1;
4036 if (!infoPtr->bDragging)
4038 lvHitTestInfo.pt = infoPtr->ptClickPos;
4039 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, TRUE);
4041 /* If the click is outside the range of an item, begin a
4042 highlight. If not, begin an item drag. */
4043 if (lvHitTestInfo.iItem == -1)
4047 /* If we're allowing multiple selections, send notification.
4048 If return value is non-zero, cancel. */
4049 if (!(infoPtr->dwStyle & LVS_SINGLESEL) && (notify_hdr(infoPtr, LVN_MARQUEEBEGIN, &hdr) == 0))
4051 /* Store the absolute coordinates of the click */
4053 LISTVIEW_GetOrigin(infoPtr, &offset);
4055 infoPtr->marqueeOrigin.x = infoPtr->ptClickPos.x - offset.x;
4056 infoPtr->marqueeOrigin.y = infoPtr->ptClickPos.y - offset.y;
4058 /* Begin selection and capture mouse */
4059 infoPtr->bMarqueeSelect = TRUE;
4060 SetCapture(infoPtr->hwndSelf);
4067 ZeroMemory(&nmlv, sizeof(nmlv));
4068 nmlv.iItem = lvHitTestInfo.iItem;
4069 nmlv.ptAction = infoPtr->ptClickPos;
4071 notify_listview(infoPtr, LVN_BEGINDRAG, &nmlv);
4072 infoPtr->bDragging = TRUE;
4080 /* see if we are supposed to be tracking mouse hovering */
4081 if (LISTVIEW_IsHotTracking(infoPtr)) {
4082 TRACKMOUSEEVENT trackinfo;
4084 trackinfo.cbSize = sizeof(TRACKMOUSEEVENT);
4085 trackinfo.dwFlags = TME_QUERY;
4087 /* see if we are already tracking this hwnd */
4088 _TrackMouseEvent(&trackinfo);
4090 if(!(trackinfo.dwFlags & TME_HOVER) || trackinfo.hwndTrack != infoPtr->hwndSelf) {
4091 trackinfo.dwFlags = TME_HOVER;
4092 trackinfo.dwHoverTime = infoPtr->dwHoverTime;
4093 trackinfo.hwndTrack = infoPtr->hwndSelf;
4095 /* call TRACKMOUSEEVENT so we receive WM_MOUSEHOVER messages */
4096 _TrackMouseEvent(&trackinfo);
4105 * Tests whether the item is assignable to a list with style lStyle
4107 static inline BOOL is_assignable_item(const LVITEMW *lpLVItem, LONG lStyle)
4109 if ( (lpLVItem->mask & LVIF_TEXT) &&
4110 (lpLVItem->pszText == LPSTR_TEXTCALLBACKW) &&
4111 (lStyle & (LVS_SORTASCENDING | LVS_SORTDESCENDING)) ) return FALSE;
4119 * Helper for LISTVIEW_SetItemT *only*: sets item attributes.
4122 * [I] infoPtr : valid pointer to the listview structure
4123 * [I] lpLVItem : valid pointer to new item attributes
4124 * [I] isNew : the item being set is being inserted
4125 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
4126 * [O] bChanged : will be set to TRUE if the item really changed
4132 static BOOL set_main_item(LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem, BOOL isNew, BOOL isW, BOOL *bChanged)
4138 /* stateMask is ignored for LVM_INSERTITEM */
4139 UINT stateMask = isNew ? ~0 : lpLVItem->stateMask;
4143 assert(lpLVItem->iItem >= 0 && lpLVItem->iItem < infoPtr->nItemCount);
4145 if (lpLVItem->mask == 0) return TRUE;
4147 if (infoPtr->dwStyle & LVS_OWNERDATA)
4149 /* a virtual listview only stores selection and focus */
4150 if (lpLVItem->mask & ~LVIF_STATE)
4156 HDPA hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, lpLVItem->iItem);
4157 lpItem = DPA_GetPtr(hdpaSubItems, 0);
4161 /* we need to get the lParam and state of the item */
4162 item.iItem = lpLVItem->iItem;
4163 item.iSubItem = lpLVItem->iSubItem;
4164 item.mask = LVIF_STATE | LVIF_PARAM;
4165 item.stateMask = (infoPtr->dwStyle & LVS_OWNERDATA) ? LVIS_FOCUSED | LVIS_SELECTED : ~0;
4169 if (!isNew && !LISTVIEW_GetItemW(infoPtr, &item)) return FALSE;
4171 TRACE("oldState=%x, newState=%x\n", item.state, lpLVItem->state);
4172 /* determine what fields will change */
4173 if ((lpLVItem->mask & LVIF_STATE) && ((item.state ^ lpLVItem->state) & stateMask & ~infoPtr->uCallbackMask))
4174 uChanged |= LVIF_STATE;
4176 if ((lpLVItem->mask & LVIF_IMAGE) && (lpItem->hdr.iImage != lpLVItem->iImage))
4177 uChanged |= LVIF_IMAGE;
4179 if ((lpLVItem->mask & LVIF_PARAM) && (lpItem->lParam != lpLVItem->lParam))
4180 uChanged |= LVIF_PARAM;
4182 if ((lpLVItem->mask & LVIF_INDENT) && (lpItem->iIndent != lpLVItem->iIndent))
4183 uChanged |= LVIF_INDENT;
4185 if ((lpLVItem->mask & LVIF_TEXT) && textcmpWT(lpItem->hdr.pszText, lpLVItem->pszText, isW))
4186 uChanged |= LVIF_TEXT;
4188 TRACE("uChanged=0x%x\n", uChanged);
4189 if (!uChanged) return TRUE;
4192 ZeroMemory(&nmlv, sizeof(NMLISTVIEW));
4193 nmlv.iItem = lpLVItem->iItem;
4194 nmlv.uNewState = (item.state & ~stateMask) | (lpLVItem->state & stateMask);
4195 nmlv.uOldState = item.state;
4196 nmlv.uChanged = uChanged;
4197 nmlv.lParam = item.lParam;
4199 /* send LVN_ITEMCHANGING notification, if the item is not being inserted */
4200 /* and we are _NOT_ virtual (LVS_OWNERDATA), and change notifications */
4202 if(lpItem && !isNew && infoPtr->bDoChangeNotify)
4204 HWND hwndSelf = infoPtr->hwndSelf;
4206 if (notify_listview(infoPtr, LVN_ITEMCHANGING, &nmlv))
4208 if (!IsWindow(hwndSelf))
4212 /* copy information */
4213 if (lpLVItem->mask & LVIF_TEXT)
4214 textsetptrT(&lpItem->hdr.pszText, lpLVItem->pszText, isW);
4216 if (lpLVItem->mask & LVIF_IMAGE)
4217 lpItem->hdr.iImage = lpLVItem->iImage;
4219 if (lpLVItem->mask & LVIF_PARAM)
4220 lpItem->lParam = lpLVItem->lParam;
4222 if (lpLVItem->mask & LVIF_INDENT)
4223 lpItem->iIndent = lpLVItem->iIndent;
4225 if (uChanged & LVIF_STATE)
4227 if (lpItem && (stateMask & ~infoPtr->uCallbackMask))
4229 lpItem->state &= ~stateMask;
4230 lpItem->state |= (lpLVItem->state & stateMask);
4232 if (lpLVItem->state & stateMask & ~infoPtr->uCallbackMask & LVIS_SELECTED)
4234 if (infoPtr->dwStyle & LVS_SINGLESEL) LISTVIEW_DeselectAllSkipItem(infoPtr, lpLVItem->iItem);
4235 ranges_additem(infoPtr->selectionRanges, lpLVItem->iItem);
4237 else if (stateMask & LVIS_SELECTED)
4239 ranges_delitem(infoPtr->selectionRanges, lpLVItem->iItem);
4241 /* if we are asked to change focus, and we manage it, do it */
4242 if (stateMask & ~infoPtr->uCallbackMask & LVIS_FOCUSED)
4244 if (lpLVItem->state & LVIS_FOCUSED)
4246 if (infoPtr->nFocusedItem != -1)
4248 /* remove current focus */
4249 item.mask = LVIF_STATE;
4251 item.stateMask = LVIS_FOCUSED;
4253 /* recurse with redrawing an item */
4254 LISTVIEW_SetItemState(infoPtr, infoPtr->nFocusedItem, &item);
4257 infoPtr->nFocusedItem = lpLVItem->iItem;
4258 LISTVIEW_EnsureVisible(infoPtr, lpLVItem->iItem, infoPtr->uView == LV_VIEW_LIST);
4260 else if (infoPtr->nFocusedItem == lpLVItem->iItem)
4262 infoPtr->nFocusedItem = -1;
4267 /* if we're inserting the item, we're done */
4268 if (isNew) return TRUE;
4270 /* send LVN_ITEMCHANGED notification */
4271 if (lpLVItem->mask & LVIF_PARAM) nmlv.lParam = lpLVItem->lParam;
4272 if (infoPtr->bDoChangeNotify) notify_listview(infoPtr, LVN_ITEMCHANGED, &nmlv);
4279 * Helper for LISTVIEW_{Set,Insert}ItemT *only*: sets subitem attributes.
4282 * [I] infoPtr : valid pointer to the listview structure
4283 * [I] lpLVItem : valid pointer to new subitem attributes
4284 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
4285 * [O] bChanged : will be set to TRUE if the item really changed
4291 static BOOL set_sub_item(const LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem, BOOL isW, BOOL *bChanged)
4294 SUBITEM_INFO *lpSubItem;
4296 /* we do not support subitems for virtual listviews */
4297 if (infoPtr->dwStyle & LVS_OWNERDATA) return FALSE;
4299 /* set subitem only if column is present */
4300 if (lpLVItem->iSubItem >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
4302 /* First do some sanity checks */
4303 /* The LVIF_STATE flag is valid for subitems, but does not appear to be
4304 particularly useful. We currently do not actually do anything with
4305 the flag on subitems.
4307 if (lpLVItem->mask & ~(LVIF_TEXT | LVIF_IMAGE | LVIF_STATE | LVIF_DI_SETITEM)) return FALSE;
4308 if (!(lpLVItem->mask & (LVIF_TEXT | LVIF_IMAGE | LVIF_STATE))) return TRUE;
4310 /* get the subitem structure, and create it if not there */
4311 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, lpLVItem->iItem);
4312 assert (hdpaSubItems);
4314 lpSubItem = LISTVIEW_GetSubItemPtr(hdpaSubItems, lpLVItem->iSubItem);
4317 SUBITEM_INFO *tmpSubItem;
4320 lpSubItem = Alloc(sizeof(SUBITEM_INFO));
4321 if (!lpSubItem) return FALSE;
4322 /* we could binary search here, if need be...*/
4323 for (i = 1; i < DPA_GetPtrCount(hdpaSubItems); i++)
4325 tmpSubItem = DPA_GetPtr(hdpaSubItems, i);
4326 if (tmpSubItem->iSubItem > lpLVItem->iSubItem) break;
4328 if (DPA_InsertPtr(hdpaSubItems, i, lpSubItem) == -1)
4333 lpSubItem->iSubItem = lpLVItem->iSubItem;
4334 lpSubItem->hdr.iImage = I_IMAGECALLBACK;
4338 if ((lpLVItem->mask & LVIF_IMAGE) && (lpSubItem->hdr.iImage != lpLVItem->iImage))
4340 lpSubItem->hdr.iImage = lpLVItem->iImage;
4344 if ((lpLVItem->mask & LVIF_TEXT) && textcmpWT(lpSubItem->hdr.pszText, lpLVItem->pszText, isW))
4346 textsetptrT(&lpSubItem->hdr.pszText, lpLVItem->pszText, isW);
4355 * Sets item attributes.
4358 * [I] infoPtr : valid pointer to the listview structure
4359 * [I] lpLVItem : new item attributes
4360 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
4366 static BOOL LISTVIEW_SetItemT(LISTVIEW_INFO *infoPtr, LVITEMW *lpLVItem, BOOL isW)
4368 HWND hwndSelf = infoPtr->hwndSelf;
4369 LPWSTR pszText = NULL;
4370 BOOL bResult, bChanged = FALSE;
4372 TRACE("(lpLVItem=%s, isW=%d)\n", debuglvitem_t(lpLVItem, isW), isW);
4374 if (!lpLVItem || lpLVItem->iItem < 0 || lpLVItem->iItem >= infoPtr->nItemCount)
4377 /* For efficiency, we transform the lpLVItem->pszText to Unicode here */
4378 if ((lpLVItem->mask & LVIF_TEXT) && is_text(lpLVItem->pszText))
4380 pszText = lpLVItem->pszText;
4381 lpLVItem->pszText = textdupTtoW(lpLVItem->pszText, isW);
4384 /* actually set the fields */
4385 if (!is_assignable_item(lpLVItem, infoPtr->dwStyle)) return FALSE;
4387 if (lpLVItem->iSubItem)
4388 bResult = set_sub_item(infoPtr, lpLVItem, TRUE, &bChanged);
4390 bResult = set_main_item(infoPtr, lpLVItem, FALSE, TRUE, &bChanged);
4391 if (!IsWindow(hwndSelf))
4394 /* redraw item, if necessary */
4395 if (bChanged && !infoPtr->bIsDrawing)
4397 /* this little optimization eliminates some nasty flicker */
4398 if ( infoPtr->uView == LV_VIEW_DETAILS && !(infoPtr->dwStyle & LVS_OWNERDRAWFIXED) &&
4399 !(infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) &&
4400 lpLVItem->iSubItem > 0 && lpLVItem->iSubItem <= DPA_GetPtrCount(infoPtr->hdpaColumns) )
4401 LISTVIEW_InvalidateSubItem(infoPtr, lpLVItem->iItem, lpLVItem->iSubItem);
4403 LISTVIEW_InvalidateItem(infoPtr, lpLVItem->iItem);
4408 textfreeT(lpLVItem->pszText, isW);
4409 lpLVItem->pszText = pszText;
4417 * Retrieves the index of the item at coordinate (0, 0) of the client area.
4420 * [I] infoPtr : valid pointer to the listview structure
4425 static INT LISTVIEW_GetTopIndex(const LISTVIEW_INFO *infoPtr)
4428 SCROLLINFO scrollInfo;
4430 scrollInfo.cbSize = sizeof(SCROLLINFO);
4431 scrollInfo.fMask = SIF_POS;
4433 if (infoPtr->uView == LV_VIEW_LIST)
4435 if (GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo))
4436 nItem = scrollInfo.nPos * LISTVIEW_GetCountPerColumn(infoPtr);
4438 else if (infoPtr->uView == LV_VIEW_DETAILS)
4440 if (GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
4441 nItem = scrollInfo.nPos;
4445 if (GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
4446 nItem = LISTVIEW_GetCountPerRow(infoPtr) * (scrollInfo.nPos / infoPtr->nItemHeight);
4449 TRACE("nItem=%d\n", nItem);
4457 * Erases the background of the given rectangle
4460 * [I] infoPtr : valid pointer to the listview structure
4461 * [I] hdc : device context handle
4462 * [I] lprcBox : clipping rectangle
4468 static inline BOOL LISTVIEW_FillBkgnd(const LISTVIEW_INFO *infoPtr, HDC hdc, const RECT *lprcBox)
4470 if (!infoPtr->hBkBrush) return FALSE;
4472 TRACE("(hdc=%p, lprcBox=%s, hBkBrush=%p)\n", hdc, wine_dbgstr_rect(lprcBox), infoPtr->hBkBrush);
4474 return FillRect(hdc, lprcBox, infoPtr->hBkBrush);
4482 * [I] infoPtr : valid pointer to the listview structure
4483 * [I] hdc : device context handle
4484 * [I] nItem : item index
4485 * [I] nSubItem : subitem index
4486 * [I] pos : item position in client coordinates
4487 * [I] cdmode : custom draw mode
4493 static BOOL LISTVIEW_DrawItem(LISTVIEW_INFO *infoPtr, HDC hdc, INT nItem, INT nSubItem, POINT pos, DWORD cdmode)
4496 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
4497 static WCHAR szCallback[] = { '(', 'c', 'a', 'l', 'l', 'b', 'a', 'c', 'k', ')', 0 };
4498 DWORD cdsubitemmode = CDRF_DODEFAULT;
4500 RECT rcSelect, rcBox, rcIcon, rcLabel, rcStateIcon;
4501 NMLVCUSTOMDRAW nmlvcd;
4506 TRACE("(hdc=%p, nItem=%d, nSubItem=%d, pos=%s)\n", hdc, nItem, nSubItem, wine_dbgstr_point(&pos));
4508 /* get information needed for drawing the item */
4509 lvItem.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_PARAM;
4510 if (nSubItem == 0) lvItem.mask |= LVIF_STATE;
4511 if (infoPtr->uView == LV_VIEW_DETAILS) lvItem.mask |= LVIF_INDENT;
4512 lvItem.stateMask = LVIS_SELECTED | LVIS_FOCUSED | LVIS_STATEIMAGEMASK | LVIS_CUT;
4513 lvItem.iItem = nItem;
4514 lvItem.iSubItem = nSubItem;
4517 lvItem.cchTextMax = DISP_TEXT_SIZE;
4518 lvItem.pszText = szDispText;
4519 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return FALSE;
4520 if (nSubItem > 0 && (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT))
4521 lvItem.state = LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED);
4522 if (lvItem.pszText == LPSTR_TEXTCALLBACKW) lvItem.pszText = szCallback;
4523 TRACE(" lvItem=%s\n", debuglvitem_t(&lvItem, TRUE));
4525 /* now check if we need to update the focus rectangle */
4526 lprcFocus = infoPtr->bFocus && (lvItem.state & LVIS_FOCUSED) ? &infoPtr->rcFocus : 0;
4528 if (!lprcFocus) lvItem.state &= ~LVIS_FOCUSED;
4529 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, &rcBox, &rcSelect, &rcIcon, &rcStateIcon, &rcLabel);
4530 OffsetRect(&rcBox, pos.x, pos.y);
4531 OffsetRect(&rcSelect, pos.x, pos.y);
4532 OffsetRect(&rcIcon, pos.x, pos.y);
4533 OffsetRect(&rcStateIcon, pos.x, pos.y);
4534 OffsetRect(&rcLabel, pos.x, pos.y);
4535 TRACE(" rcBox=%s, rcSelect=%s, rcIcon=%s. rcLabel=%s\n",
4536 wine_dbgstr_rect(&rcBox), wine_dbgstr_rect(&rcSelect),
4537 wine_dbgstr_rect(&rcIcon), wine_dbgstr_rect(&rcLabel));
4539 /* fill in the custom draw structure */
4540 customdraw_fill(&nmlvcd, infoPtr, hdc, &rcBox, &lvItem);
4542 hOldFont = GetCurrentObject(hdc, OBJ_FONT);
4543 if (nSubItem > 0) cdmode = infoPtr->cditemmode;
4544 if (cdmode & CDRF_SKIPDEFAULT) goto postpaint;
4545 if (cdmode & CDRF_NOTIFYITEMDRAW)
4546 cdsubitemmode = notify_customdraw(infoPtr, CDDS_PREPAINT, &nmlvcd);
4547 if (nSubItem == 0) infoPtr->cditemmode = cdsubitemmode;
4548 if (cdsubitemmode & CDRF_SKIPDEFAULT) goto postpaint;
4549 /* we have to send a CDDS_SUBITEM customdraw explicitly for subitem 0 */
4550 if (nSubItem == 0 && cdsubitemmode == CDRF_NOTIFYITEMDRAW)
4552 cdsubitemmode = notify_customdraw(infoPtr, CDDS_SUBITEM | CDDS_ITEMPREPAINT, &nmlvcd);
4553 if (cdsubitemmode & CDRF_SKIPDEFAULT) goto postpaint;
4555 if (nSubItem == 0 || (cdmode & CDRF_NOTIFYITEMDRAW))
4556 prepaint_setup(infoPtr, hdc, &nmlvcd, FALSE);
4557 else if ((infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) == FALSE)
4558 prepaint_setup(infoPtr, hdc, &nmlvcd, TRUE);
4560 /* in full row select, subitems, will just use main item's colors */
4561 if (nSubItem && infoPtr->uView == LV_VIEW_DETAILS && (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT))
4562 nmlvcd.clrTextBk = CLR_NONE;
4564 /* FIXME: temporary hack */
4565 rcSelect.left = rcLabel.left;
4567 /* draw the selection background, if we're drawing the main item */
4570 /* in icon mode, the label rect is really what we want to draw the
4572 if (infoPtr->uView == LV_VIEW_ICON)
4575 if (infoPtr->uView == LV_VIEW_DETAILS && (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT))
4577 /* we have to update left focus bound too if item isn't in leftmost column
4578 and reduce right box bound */
4579 if (DPA_GetPtrCount(infoPtr->hdpaColumns) > 0)
4583 if ((leftmost = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, 0, 0)))
4585 INT Originx = pos.x - LISTVIEW_GetColumnInfo(infoPtr, 0)->rcHeader.left;
4586 INT index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX,
4587 DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, 0);
4589 rcBox.right = LISTVIEW_GetColumnInfo(infoPtr, index)->rcHeader.right + Originx;
4590 rcSelect.left = LISTVIEW_GetColumnInfo(infoPtr, leftmost)->rcHeader.left + Originx;
4594 rcSelect.right = rcBox.right;
4597 if (nmlvcd.clrTextBk != CLR_NONE)
4598 ExtTextOutW(hdc, rcSelect.left, rcSelect.top, ETO_OPAQUE, &rcSelect, NULL, 0, NULL);
4599 /* store new focus rectangle */
4600 if (infoPtr->nFocusedItem == nItem) infoPtr->rcFocus = rcSelect;
4604 if (infoPtr->himlState && STATEIMAGEINDEX(lvItem.state) && (nSubItem == 0))
4606 UINT uStateImage = STATEIMAGEINDEX(lvItem.state);
4609 TRACE("uStateImage=%d\n", uStateImage);
4610 ImageList_Draw(infoPtr->himlState, uStateImage - 1, hdc,
4611 rcStateIcon.left, rcStateIcon.top, ILD_NORMAL);
4616 himl = (infoPtr->uView == LV_VIEW_ICON ? infoPtr->himlNormal : infoPtr->himlSmall);
4617 if (himl && lvItem.iImage >= 0 && !IsRectEmpty(&rcIcon))
4621 TRACE("iImage=%d\n", lvItem.iImage);
4623 if (lvItem.state & (LVIS_SELECTED | LVIS_CUT) && infoPtr->bFocus)
4624 style = ILD_SELECTED;
4628 ImageList_DrawEx(himl, lvItem.iImage, hdc, rcIcon.left, rcIcon.top,
4629 rcIcon.right - rcIcon.left, rcIcon.bottom - rcIcon.top, infoPtr->clrBk,
4630 lvItem.state & LVIS_CUT ? RGB(255, 255, 255) : CLR_DEFAULT,
4634 /* Don't bother painting item being edited */
4635 if (infoPtr->hwndEdit && nItem == infoPtr->nEditLabelItem && nSubItem == 0) goto postpaint;
4637 /* figure out the text drawing flags */
4638 uFormat = (infoPtr->uView == LV_VIEW_ICON ? (lprcFocus ? LV_FL_DT_FLAGS : LV_ML_DT_FLAGS) : LV_SL_DT_FLAGS);
4639 if (infoPtr->uView == LV_VIEW_ICON)
4640 uFormat = (lprcFocus ? LV_FL_DT_FLAGS : LV_ML_DT_FLAGS);
4643 switch (LISTVIEW_GetColumnInfo(infoPtr, nSubItem)->fmt & LVCFMT_JUSTIFYMASK)
4645 case LVCFMT_RIGHT: uFormat |= DT_RIGHT; break;
4646 case LVCFMT_CENTER: uFormat |= DT_CENTER; break;
4647 default: uFormat |= DT_LEFT;
4650 if (!(uFormat & (DT_RIGHT | DT_CENTER)))
4652 if (himl && lvItem.iImage >= 0 && !IsRectEmpty(&rcIcon)) rcLabel.left += IMAGE_PADDING;
4653 else rcLabel.left += LABEL_HOR_PADDING;
4655 else if (uFormat & DT_RIGHT) rcLabel.right -= LABEL_HOR_PADDING;
4657 /* for GRIDLINES reduce the bottom so the text formats correctly */
4658 if (infoPtr->uView == LV_VIEW_DETAILS && infoPtr->dwLvExStyle & LVS_EX_GRIDLINES)
4661 DrawTextW(hdc, lvItem.pszText, -1, &rcLabel, uFormat);
4664 if (cdsubitemmode & CDRF_NOTIFYPOSTPAINT)
4665 notify_postpaint(infoPtr, &nmlvcd);
4666 if (cdsubitemmode & CDRF_NEWFONT)
4667 SelectObject(hdc, hOldFont);
4673 * Draws listview items when in owner draw mode.
4676 * [I] infoPtr : valid pointer to the listview structure
4677 * [I] hdc : device context handle
4682 static void LISTVIEW_RefreshOwnerDraw(const LISTVIEW_INFO *infoPtr, ITERATOR *i, HDC hdc, DWORD cdmode)
4684 UINT uID = (UINT)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
4685 DWORD cditemmode = CDRF_DODEFAULT;
4686 NMLVCUSTOMDRAW nmlvcd;
4687 POINT Origin, Position;
4693 ZeroMemory(&dis, sizeof(dis));
4695 /* Get scroll info once before loop */
4696 LISTVIEW_GetOrigin(infoPtr, &Origin);
4698 /* iterate through the invalidated rows */
4699 while(iterator_next(i))
4701 item.iItem = i->nItem;
4703 item.mask = LVIF_PARAM | LVIF_STATE;
4704 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
4705 if (!LISTVIEW_GetItemW(infoPtr, &item)) continue;
4707 dis.CtlType = ODT_LISTVIEW;
4709 dis.itemID = item.iItem;
4710 dis.itemAction = ODA_DRAWENTIRE;
4712 if (item.state & LVIS_SELECTED) dis.itemState |= ODS_SELECTED;
4713 if (infoPtr->bFocus && (item.state & LVIS_FOCUSED)) dis.itemState |= ODS_FOCUS;
4714 dis.hwndItem = infoPtr->hwndSelf;
4716 LISTVIEW_GetItemOrigin(infoPtr, dis.itemID, &Position);
4717 dis.rcItem.left = Position.x + Origin.x;
4718 dis.rcItem.right = dis.rcItem.left + infoPtr->nItemWidth;
4719 dis.rcItem.top = Position.y + Origin.y;
4720 dis.rcItem.bottom = dis.rcItem.top + infoPtr->nItemHeight;
4721 dis.itemData = item.lParam;
4723 TRACE("item=%s, rcItem=%s\n", debuglvitem_t(&item, TRUE), wine_dbgstr_rect(&dis.rcItem));
4726 * Even if we do not send the CDRF_NOTIFYITEMDRAW we need to fill the nmlvcd
4727 * structure for the rest. of the paint cycle
4729 customdraw_fill(&nmlvcd, infoPtr, hdc, &dis.rcItem, &item);
4730 if (cdmode & CDRF_NOTIFYITEMDRAW)
4731 cditemmode = notify_customdraw(infoPtr, CDDS_PREPAINT, &nmlvcd);
4733 if (!(cditemmode & CDRF_SKIPDEFAULT))
4735 prepaint_setup (infoPtr, hdc, &nmlvcd, FALSE);
4736 SendMessageW(infoPtr->hwndNotify, WM_DRAWITEM, dis.CtlID, (LPARAM)&dis);
4739 if (cditemmode & CDRF_NOTIFYPOSTPAINT)
4740 notify_postpaint(infoPtr, &nmlvcd);
4746 * Draws listview items when in report display mode.
4749 * [I] infoPtr : valid pointer to the listview structure
4750 * [I] hdc : device context handle
4751 * [I] cdmode : custom draw mode
4756 static void LISTVIEW_RefreshReport(LISTVIEW_INFO *infoPtr, ITERATOR *i, HDC hdc, DWORD cdmode)
4759 RECT rcClip, rcItem;
4760 POINT Origin, Position;
4767 /* figure out what to draw */
4768 rgntype = GetClipBox(hdc, &rcClip);
4769 if (rgntype == NULLREGION) return;
4771 /* Get scroll info once before loop */
4772 LISTVIEW_GetOrigin(infoPtr, &Origin);
4774 colRanges = ranges_create(DPA_GetPtrCount(infoPtr->hdpaColumns));
4776 /* narrow down the columns we need to paint */
4777 for(col = 0; col < DPA_GetPtrCount(infoPtr->hdpaColumns); col++)
4779 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, col, 0);
4781 LISTVIEW_GetHeaderRect(infoPtr, index, &rcItem);
4782 if ((rcItem.right + Origin.x >= rcClip.left) && (rcItem.left + Origin.x < rcClip.right))
4783 ranges_additem(colRanges, index);
4785 iterator_rangesitems(&j, colRanges);
4787 /* in full row select, we _have_ to draw the main item */
4788 if (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT)
4791 /* iterate through the invalidated rows */
4792 while(iterator_next(i))
4794 LISTVIEW_GetItemOrigin(infoPtr, i->nItem, &Position);
4795 Position.y += Origin.y;
4797 /* iterate through the invalidated columns */
4798 while(iterator_next(&j))
4800 LISTVIEW_GetHeaderRect(infoPtr, j.nItem, &rcItem);
4801 Position.x = (j.nItem == 0) ? rcItem.left + Origin.x : Origin.x;
4803 if (rgntype == COMPLEXREGION && !((infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) && j.nItem == 0))
4806 rcItem.bottom = infoPtr->nItemHeight;
4807 OffsetRect(&rcItem, Origin.x, Position.y);
4808 if (!RectVisible(hdc, &rcItem)) continue;
4811 LISTVIEW_DrawItem(infoPtr, hdc, i->nItem, j.nItem, Position, cdmode);
4814 iterator_destroy(&j);
4819 * Draws the gridlines if necessary when in report display mode.
4822 * [I] infoPtr : valid pointer to the listview structure
4823 * [I] hdc : device context handle
4828 static void LISTVIEW_RefreshReportGrid(LISTVIEW_INFO *infoPtr, HDC hdc)
4834 RECT rcClip, rcItem = {0};
4842 /* figure out what to draw */
4843 rgntype = GetClipBox(hdc, &rcClip);
4844 if (rgntype == NULLREGION) return;
4846 /* Get scroll info once before loop */
4847 LISTVIEW_GetOrigin(infoPtr, &Origin);
4849 colRanges = ranges_create(DPA_GetPtrCount(infoPtr->hdpaColumns));
4851 /* narrow down the columns we need to paint */
4852 for(col = 0; col < DPA_GetPtrCount(infoPtr->hdpaColumns); col++)
4854 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, col, 0);
4856 LISTVIEW_GetHeaderRect(infoPtr, index, &rcItem);
4857 if ((rcItem.right + Origin.x >= rcClip.left) && (rcItem.left + Origin.x < rcClip.right))
4858 ranges_additem(colRanges, index);
4861 /* is right most vertical line visible? */
4862 if (DPA_GetPtrCount(infoPtr->hdpaColumns) > 0)
4864 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, 0);
4865 LISTVIEW_GetHeaderRect(infoPtr, index, &rcItem);
4866 rmost = (rcItem.right + Origin.x < rcClip.right);
4869 if ((hPen = CreatePen( PS_SOLID, 1, comctl32_color.clr3dFace )))
4871 hOldPen = SelectObject ( hdc, hPen );
4873 /* draw the vertical lines for the columns */
4874 iterator_rangesitems(&j, colRanges);
4875 while(iterator_next(&j))
4877 LISTVIEW_GetHeaderRect(infoPtr, j.nItem, &rcItem);
4878 if (rcItem.left == 0) continue; /* skip leftmost column */
4879 rcItem.left += Origin.x;
4880 rcItem.right += Origin.x;
4881 rcItem.top = infoPtr->rcList.top;
4882 rcItem.bottom = infoPtr->rcList.bottom;
4883 TRACE("vert col=%d, rcItem=%s\n", j.nItem, wine_dbgstr_rect(&rcItem));
4884 MoveToEx (hdc, rcItem.left, rcItem.top, NULL);
4885 LineTo (hdc, rcItem.left, rcItem.bottom);
4887 iterator_destroy(&j);
4888 /* draw rightmost grid line if visible */
4891 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX,
4892 DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, 0);
4893 LISTVIEW_GetHeaderRect(infoPtr, index, &rcItem);
4895 rcItem.right += Origin.x;
4897 MoveToEx (hdc, rcItem.right, infoPtr->rcList.top, NULL);
4898 LineTo (hdc, rcItem.right, infoPtr->rcList.bottom);
4901 /* draw the horizontial lines for the rows */
4902 itemheight = LISTVIEW_CalculateItemHeight(infoPtr);
4903 rcItem.left = infoPtr->rcList.left;
4904 rcItem.right = infoPtr->rcList.right;
4905 rcItem.bottom = rcItem.top = Origin.y - 1;
4906 MoveToEx(hdc, rcItem.left, rcItem.top, NULL);
4907 LineTo(hdc, rcItem.right, rcItem.top);
4908 for(y=itemheight-1+Origin.y; y<=infoPtr->rcList.bottom; y+=itemheight)
4910 rcItem.bottom = rcItem.top = y;
4911 TRACE("horz rcItem=%s\n", wine_dbgstr_rect(&rcItem));
4912 MoveToEx (hdc, rcItem.left, rcItem.top, NULL);
4913 LineTo (hdc, rcItem.right, rcItem.top);
4916 SelectObject( hdc, hOldPen );
4917 DeleteObject( hPen );
4923 * Draws listview items when in list display mode.
4926 * [I] infoPtr : valid pointer to the listview structure
4927 * [I] hdc : device context handle
4928 * [I] cdmode : custom draw mode
4933 static void LISTVIEW_RefreshList(LISTVIEW_INFO *infoPtr, ITERATOR *i, HDC hdc, DWORD cdmode)
4935 POINT Origin, Position;
4937 /* Get scroll info once before loop */
4938 LISTVIEW_GetOrigin(infoPtr, &Origin);
4940 while(iterator_prev(i))
4942 LISTVIEW_GetItemOrigin(infoPtr, i->nItem, &Position);
4943 Position.x += Origin.x;
4944 Position.y += Origin.y;
4946 LISTVIEW_DrawItem(infoPtr, hdc, i->nItem, 0, Position, cdmode);
4953 * Draws listview items.
4956 * [I] infoPtr : valid pointer to the listview structure
4957 * [I] hdc : device context handle
4958 * [I] prcErase : rect to be erased before refresh (may be NULL)
4963 static void LISTVIEW_Refresh(LISTVIEW_INFO *infoPtr, HDC hdc, const RECT *prcErase)
4965 COLORREF oldTextColor = 0, oldBkColor = 0, oldClrTextBk, oldClrText;
4966 NMLVCUSTOMDRAW nmlvcd;
4973 HBITMAP hbmp = NULL;
4976 LISTVIEW_DUMP(infoPtr);
4978 if (infoPtr->dwLvExStyle & LVS_EX_DOUBLEBUFFER) {
4979 TRACE("double buffering\n");
4981 hdc = CreateCompatibleDC(hdcOrig);
4983 ERR("Failed to create DC for backbuffer\n");
4986 hbmp = CreateCompatibleBitmap(hdcOrig, infoPtr->rcList.right,
4987 infoPtr->rcList.bottom);
4989 ERR("Failed to create bitmap for backbuffer\n");
4994 SelectObject(hdc, hbmp);
4995 SelectObject(hdc, infoPtr->hFont);
4997 /* Save dc values we're gonna trash while drawing
4998 * FIXME: Should be done in LISTVIEW_DrawItem() */
4999 hOldFont = SelectObject(hdc, infoPtr->hFont);
5000 oldBkMode = GetBkMode(hdc);
5001 oldBkColor = GetBkColor(hdc);
5002 oldTextColor = GetTextColor(hdc);
5005 infoPtr->bIsDrawing = TRUE;
5008 LISTVIEW_FillBkgnd(infoPtr, hdc, prcErase);
5009 } else if (infoPtr->dwLvExStyle & LVS_EX_DOUBLEBUFFER) {
5010 /* If no erasing was done (usually because RedrawWindow was called
5011 * with RDW_INVALIDATE only) we need to copy the old contents into
5012 * the backbuffer before continuing. */
5013 BitBlt(hdc, infoPtr->rcList.left, infoPtr->rcList.top,
5014 infoPtr->rcList.right - infoPtr->rcList.left,
5015 infoPtr->rcList.bottom - infoPtr->rcList.top,
5016 hdcOrig, infoPtr->rcList.left, infoPtr->rcList.top, SRCCOPY);
5019 /* FIXME: Shouldn't need to do this */
5020 oldClrTextBk = infoPtr->clrTextBk;
5021 oldClrText = infoPtr->clrText;
5023 infoPtr->cditemmode = CDRF_DODEFAULT;
5025 GetClientRect(infoPtr->hwndSelf, &rcClient);
5026 customdraw_fill(&nmlvcd, infoPtr, hdc, &rcClient, 0);
5027 cdmode = notify_customdraw(infoPtr, CDDS_PREPAINT, &nmlvcd);
5028 if (cdmode & CDRF_SKIPDEFAULT) goto enddraw;
5029 prepaint_setup(infoPtr, hdc, &nmlvcd, FALSE);
5031 /* Use these colors to draw the items */
5032 infoPtr->clrTextBk = nmlvcd.clrTextBk;
5033 infoPtr->clrText = nmlvcd.clrText;
5035 /* nothing to draw */
5036 if(infoPtr->nItemCount == 0) goto enddraw;
5038 /* figure out what we need to draw */
5039 iterator_visibleitems(&i, infoPtr, hdc);
5040 range = iterator_range(&i);
5042 /* send cache hint notification */
5043 if (infoPtr->dwStyle & LVS_OWNERDATA)
5047 ZeroMemory(&nmlv, sizeof(NMLVCACHEHINT));
5048 nmlv.iFrom = range.lower;
5049 nmlv.iTo = range.upper - 1;
5050 notify_hdr(infoPtr, LVN_ODCACHEHINT, &nmlv.hdr);
5053 if ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (infoPtr->uView == LV_VIEW_DETAILS))
5054 LISTVIEW_RefreshOwnerDraw(infoPtr, &i, hdc, cdmode);
5057 if (infoPtr->uView == LV_VIEW_DETAILS)
5058 LISTVIEW_RefreshReport(infoPtr, &i, hdc, cdmode);
5059 else /* LV_VIEW_LIST, LV_VIEW_ICON or LV_VIEW_SMALLICON */
5060 LISTVIEW_RefreshList(infoPtr, &i, hdc, cdmode);
5062 /* if we have a focus rect and it's visible, draw it */
5063 if (infoPtr->bFocus && range.lower <= infoPtr->nFocusedItem &&
5064 (range.upper - 1) >= infoPtr->nFocusedItem)
5065 LISTVIEW_DrawFocusRect(infoPtr, hdc);
5067 iterator_destroy(&i);
5070 /* For LVS_EX_GRIDLINES go and draw lines */
5071 /* This includes the case where there were *no* items */
5072 if ((infoPtr->uView == LV_VIEW_DETAILS) && infoPtr->dwLvExStyle & LVS_EX_GRIDLINES)
5073 LISTVIEW_RefreshReportGrid(infoPtr, hdc);
5075 /* Draw marquee rectangle if appropriate */
5076 if (infoPtr->bMarqueeSelect)
5077 DrawFocusRect(hdc, &infoPtr->marqueeDrawRect);
5079 if (cdmode & CDRF_NOTIFYPOSTPAINT)
5080 notify_postpaint(infoPtr, &nmlvcd);
5082 infoPtr->clrTextBk = oldClrTextBk;
5083 infoPtr->clrText = oldClrText;
5086 BitBlt(hdcOrig, infoPtr->rcList.left, infoPtr->rcList.top,
5087 infoPtr->rcList.right - infoPtr->rcList.left,
5088 infoPtr->rcList.bottom - infoPtr->rcList.top,
5089 hdc, infoPtr->rcList.left, infoPtr->rcList.top, SRCCOPY);
5094 SelectObject(hdc, hOldFont);
5095 SetBkMode(hdc, oldBkMode);
5096 SetBkColor(hdc, oldBkColor);
5097 SetTextColor(hdc, oldTextColor);
5100 infoPtr->bIsDrawing = FALSE;
5106 * Calculates the approximate width and height of a given number of items.
5109 * [I] infoPtr : valid pointer to the listview structure
5110 * [I] nItemCount : number of items
5111 * [I] wWidth : width
5112 * [I] wHeight : height
5115 * Returns a DWORD. The width in the low word and the height in high word.
5117 static DWORD LISTVIEW_ApproximateViewRect(const LISTVIEW_INFO *infoPtr, INT nItemCount,
5118 WORD wWidth, WORD wHeight)
5120 DWORD dwViewRect = 0;
5122 if (nItemCount == -1)
5123 nItemCount = infoPtr->nItemCount;
5125 if (infoPtr->uView == LV_VIEW_LIST)
5127 INT nItemCountPerColumn = 1;
5128 INT nColumnCount = 0;
5130 if (wHeight == 0xFFFF)
5132 /* use current height */
5133 wHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
5136 if (wHeight < infoPtr->nItemHeight)
5137 wHeight = infoPtr->nItemHeight;
5141 if (infoPtr->nItemHeight > 0)
5143 nItemCountPerColumn = wHeight / infoPtr->nItemHeight;
5144 if (nItemCountPerColumn == 0)
5145 nItemCountPerColumn = 1;
5147 if (nItemCount % nItemCountPerColumn != 0)
5148 nColumnCount = nItemCount / nItemCountPerColumn;
5150 nColumnCount = nItemCount / nItemCountPerColumn + 1;
5154 /* Microsoft padding magic */
5155 wHeight = nItemCountPerColumn * infoPtr->nItemHeight + 2;
5156 wWidth = nColumnCount * infoPtr->nItemWidth + 2;
5158 dwViewRect = MAKELONG(wWidth, wHeight);
5160 else if (infoPtr->uView == LV_VIEW_DETAILS)
5164 if (infoPtr->nItemCount > 0)
5166 LISTVIEW_GetItemBox(infoPtr, 0, &rcBox);
5167 wWidth = rcBox.right - rcBox.left;
5168 wHeight = (rcBox.bottom - rcBox.top) * nItemCount;
5172 /* use current height and width */
5173 if (wHeight == 0xffff)
5174 wHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
5175 if (wWidth == 0xffff)
5176 wWidth = infoPtr->rcList.right - infoPtr->rcList.left;
5179 dwViewRect = MAKELONG(wWidth, wHeight);
5181 else if (infoPtr->uView == LV_VIEW_ICON)
5187 nItemWidth = infoPtr->iconSpacing.cx;
5188 nItemHeight = infoPtr->iconSpacing.cy;
5190 if (wWidth == 0xffff)
5191 wWidth = infoPtr->rcList.right - infoPtr->rcList.left;
5193 if (wWidth < nItemWidth)
5194 wWidth = nItemWidth;
5196 cols = wWidth / nItemWidth;
5197 if (cols > nItemCount)
5204 rows = nItemCount / cols;
5205 if (nItemCount % cols)
5211 wHeight = (nItemHeight * rows)+2;
5212 wWidth = (nItemWidth * cols)+2;
5214 dwViewRect = MAKELONG(wWidth, wHeight);
5216 else if (infoPtr->uView == LV_VIEW_SMALLICON)
5217 FIXME("uView == LV_VIEW_SMALLICON: not implemented\n");
5224 * Cancel edit label with saving item text.
5227 * [I] infoPtr : valid pointer to the listview structure
5230 * Always returns TRUE.
5232 static LRESULT LISTVIEW_CancelEditLabel(LISTVIEW_INFO *infoPtr)
5234 if (infoPtr->hwndEdit)
5236 /* handle value will be lost after LISTVIEW_EndEditLabelT */
5237 HWND edit = infoPtr->hwndEdit;
5239 LISTVIEW_EndEditLabelT(infoPtr, TRUE, IsWindowUnicode(infoPtr->hwndEdit));
5240 SendMessageW(edit, WM_CLOSE, 0, 0);
5248 * Create a drag image list for the specified item.
5251 * [I] infoPtr : valid pointer to the listview structure
5252 * [I] iItem : index of item
5253 * [O] lppt : Upper-left corner of the image
5256 * Returns a handle to the image list if successful, NULL otherwise.
5258 static HIMAGELIST LISTVIEW_CreateDragImage(LISTVIEW_INFO *infoPtr, INT iItem, LPPOINT lppt)
5264 HBITMAP hbmp, hOldbmp;
5265 HIMAGELIST dragList = 0;
5266 TRACE("iItem=%d Count=%d\n", iItem, infoPtr->nItemCount);
5268 if (iItem < 0 || iItem >= infoPtr->nItemCount || !lppt)
5271 rcItem.left = LVIR_BOUNDS;
5272 if (!LISTVIEW_GetItemRect(infoPtr, iItem, &rcItem))
5275 lppt->x = rcItem.left;
5276 lppt->y = rcItem.top;
5278 size.cx = rcItem.right - rcItem.left;
5279 size.cy = rcItem.bottom - rcItem.top;
5281 hdcOrig = GetDC(infoPtr->hwndSelf);
5282 hdc = CreateCompatibleDC(hdcOrig);
5283 hbmp = CreateCompatibleBitmap(hdcOrig, size.cx, size.cy);
5284 hOldbmp = SelectObject(hdc, hbmp);
5286 rcItem.left = rcItem.top = 0;
5287 rcItem.right = size.cx;
5288 rcItem.bottom = size.cy;
5289 FillRect(hdc, &rcItem, infoPtr->hBkBrush);
5292 if (LISTVIEW_DrawItem(infoPtr, hdc, iItem, 0, pos, infoPtr->cditemmode))
5294 dragList = ImageList_Create(size.cx, size.cy, ILC_COLOR, 10, 10);
5295 SelectObject(hdc, hOldbmp);
5296 ImageList_Add(dragList, hbmp, 0);
5299 SelectObject(hdc, hOldbmp);
5303 ReleaseDC(infoPtr->hwndSelf, hdcOrig);
5305 TRACE("ret=%p\n", dragList);
5313 * Removes all listview items and subitems.
5316 * [I] infoPtr : valid pointer to the listview structure
5322 static BOOL LISTVIEW_DeleteAllItems(LISTVIEW_INFO *infoPtr, BOOL destroy)
5325 HDPA hdpaSubItems = NULL;
5334 /* we do it directly, to avoid notifications */
5335 ranges_clear(infoPtr->selectionRanges);
5336 infoPtr->nSelectionMark = -1;
5337 infoPtr->nFocusedItem = -1;
5338 SetRectEmpty(&infoPtr->rcFocus);
5339 /* But we are supposed to leave nHotItem as is! */
5342 /* send LVN_DELETEALLITEMS notification */
5343 ZeroMemory(&nmlv, sizeof(NMLISTVIEW));
5345 bSuppress = notify_listview(infoPtr, LVN_DELETEALLITEMS, &nmlv);
5347 for (i = infoPtr->nItemCount - 1; i >= 0; i--)
5349 if (!(infoPtr->dwStyle & LVS_OWNERDATA))
5351 /* send LVN_DELETEITEM notification, if not suppressed
5352 and if it is not a virtual listview */
5353 if (!bSuppress) notify_deleteitem(infoPtr, i);
5354 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, i);
5355 lpItem = DPA_GetPtr(hdpaSubItems, 0);
5356 /* free id struct */
5357 j = DPA_GetPtrIndex(infoPtr->hdpaItemIds, lpItem->id);
5358 lpID = DPA_GetPtr(infoPtr->hdpaItemIds, j);
5359 DPA_DeletePtr(infoPtr->hdpaItemIds, j);
5361 /* both item and subitem start with ITEMHDR header */
5362 for (j = 0; j < DPA_GetPtrCount(hdpaSubItems); j++)
5364 hdrItem = DPA_GetPtr(hdpaSubItems, j);
5365 if (is_text(hdrItem->pszText)) Free(hdrItem->pszText);
5368 DPA_Destroy(hdpaSubItems);
5369 DPA_DeletePtr(infoPtr->hdpaItems, i);
5371 DPA_DeletePtr(infoPtr->hdpaPosX, i);
5372 DPA_DeletePtr(infoPtr->hdpaPosY, i);
5373 infoPtr->nItemCount --;
5378 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
5379 LISTVIEW_UpdateScroll(infoPtr);
5381 LISTVIEW_InvalidateList(infoPtr);
5388 * Scrolls, and updates the columns, when a column is changing width.
5391 * [I] infoPtr : valid pointer to the listview structure
5392 * [I] nColumn : column to scroll
5393 * [I] dx : amount of scroll, in pixels
5398 static void LISTVIEW_ScrollColumns(LISTVIEW_INFO *infoPtr, INT nColumn, INT dx)
5400 COLUMN_INFO *lpColumnInfo;
5406 if (nColumn < 0 || DPA_GetPtrCount(infoPtr->hdpaColumns) < 1) return;
5407 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, min(nColumn, DPA_GetPtrCount(infoPtr->hdpaColumns) - 1));
5408 rcCol = lpColumnInfo->rcHeader;
5409 if (nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns))
5410 rcCol.left = rcCol.right;
5412 /* adjust the other columns */
5413 hdi.mask = HDI_ORDER;
5414 if (SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nColumn, (LPARAM)&hdi))
5416 INT nOrder = hdi.iOrder;
5417 for (nCol = 0; nCol < DPA_GetPtrCount(infoPtr->hdpaColumns); nCol++)
5419 hdi.mask = HDI_ORDER;
5420 SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nCol, (LPARAM)&hdi);
5421 if (hdi.iOrder >= nOrder) {
5422 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, nCol);
5423 lpColumnInfo->rcHeader.left += dx;
5424 lpColumnInfo->rcHeader.right += dx;
5429 /* do not update screen if not in report mode */
5430 if (!is_redrawing(infoPtr) || infoPtr->uView != LV_VIEW_DETAILS) return;
5432 /* Need to reset the item width when inserting a new column */
5433 infoPtr->nItemWidth += dx;
5435 LISTVIEW_UpdateScroll(infoPtr);
5436 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
5438 /* scroll to cover the deleted column, and invalidate for redraw */
5439 rcOld = infoPtr->rcList;
5440 rcOld.left = ptOrigin.x + rcCol.left + dx;
5441 ScrollWindowEx(infoPtr->hwndSelf, dx, 0, &rcOld, &rcOld, 0, 0, SW_ERASE | SW_INVALIDATE);
5446 * Removes a column from the listview control.
5449 * [I] infoPtr : valid pointer to the listview structure
5450 * [I] nColumn : column index
5456 static BOOL LISTVIEW_DeleteColumn(LISTVIEW_INFO *infoPtr, INT nColumn)
5460 TRACE("nColumn=%d\n", nColumn);
5462 if (nColumn < 0 || DPA_GetPtrCount(infoPtr->hdpaColumns) == 0
5463 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
5465 /* While the MSDN specifically says that column zero should not be deleted,
5466 what actually happens is that the column itself is deleted but no items or subitems
5470 LISTVIEW_GetHeaderRect(infoPtr, nColumn, &rcCol);
5472 if (!SendMessageW(infoPtr->hwndHeader, HDM_DELETEITEM, nColumn, 0))
5475 Free(DPA_GetPtr(infoPtr->hdpaColumns, nColumn));
5476 DPA_DeletePtr(infoPtr->hdpaColumns, nColumn);
5478 if (!(infoPtr->dwStyle & LVS_OWNERDATA) && nColumn)
5480 SUBITEM_INFO *lpSubItem, *lpDelItem;
5482 INT nItem, nSubItem, i;
5484 for (nItem = 0; nItem < infoPtr->nItemCount; nItem++)
5486 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, nItem);
5489 for (i = 1; i < DPA_GetPtrCount(hdpaSubItems); i++)
5491 lpSubItem = DPA_GetPtr(hdpaSubItems, i);
5492 if (lpSubItem->iSubItem == nColumn)
5495 lpDelItem = lpSubItem;
5497 else if (lpSubItem->iSubItem > nColumn)
5499 lpSubItem->iSubItem--;
5503 /* if we found our subitem, zapp it */
5507 if (is_text(lpDelItem->hdr.pszText))
5508 Free(lpDelItem->hdr.pszText);
5513 /* free dpa memory */
5514 DPA_DeletePtr(hdpaSubItems, nSubItem);
5519 /* update the other column info */
5520 LISTVIEW_UpdateItemSize(infoPtr);
5521 if(DPA_GetPtrCount(infoPtr->hdpaColumns) == 0)
5522 LISTVIEW_InvalidateList(infoPtr);
5524 LISTVIEW_ScrollColumns(infoPtr, nColumn, -(rcCol.right - rcCol.left));
5531 * Invalidates the listview after an item's insertion or deletion.
5534 * [I] infoPtr : valid pointer to the listview structure
5535 * [I] nItem : item index
5536 * [I] dir : -1 if deleting, 1 if inserting
5541 static void LISTVIEW_ScrollOnInsert(LISTVIEW_INFO *infoPtr, INT nItem, INT dir)
5543 INT nPerCol, nItemCol, nItemRow;
5547 /* if we don't refresh, what's the point of scrolling? */
5548 if (!is_redrawing(infoPtr)) return;
5550 assert (abs(dir) == 1);
5552 /* arrange icons if autoarrange is on */
5553 if (is_autoarrange(infoPtr))
5555 BOOL arrange = TRUE;
5556 if (dir < 0 && nItem >= infoPtr->nItemCount) arrange = FALSE;
5557 if (dir > 0 && nItem == infoPtr->nItemCount - 1) arrange = FALSE;
5558 if (arrange) LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
5561 /* scrollbars need updating */
5562 LISTVIEW_UpdateScroll(infoPtr);
5564 /* figure out the item's position */
5565 if (infoPtr->uView == LV_VIEW_DETAILS)
5566 nPerCol = infoPtr->nItemCount + 1;
5567 else if (infoPtr->uView == LV_VIEW_LIST)
5568 nPerCol = LISTVIEW_GetCountPerColumn(infoPtr);
5569 else /* LV_VIEW_ICON, or LV_VIEW_SMALLICON */
5572 nItemCol = nItem / nPerCol;
5573 nItemRow = nItem % nPerCol;
5574 LISTVIEW_GetOrigin(infoPtr, &Origin);
5576 /* move the items below up a slot */
5577 rcScroll.left = nItemCol * infoPtr->nItemWidth;
5578 rcScroll.top = nItemRow * infoPtr->nItemHeight;
5579 rcScroll.right = rcScroll.left + infoPtr->nItemWidth;
5580 rcScroll.bottom = nPerCol * infoPtr->nItemHeight;
5581 OffsetRect(&rcScroll, Origin.x, Origin.y);
5582 TRACE("rcScroll=%s, dx=%d\n", wine_dbgstr_rect(&rcScroll), dir * infoPtr->nItemHeight);
5583 if (IntersectRect(&rcScroll, &rcScroll, &infoPtr->rcList))
5585 TRACE("Scrolling rcScroll=%s, rcList=%s\n", wine_dbgstr_rect(&rcScroll), wine_dbgstr_rect(&infoPtr->rcList));
5586 ScrollWindowEx(infoPtr->hwndSelf, 0, dir * infoPtr->nItemHeight,
5587 &rcScroll, &rcScroll, 0, 0, SW_ERASE | SW_INVALIDATE);
5590 /* report has only that column, so we're done */
5591 if (infoPtr->uView == LV_VIEW_DETAILS) return;
5593 /* now for LISTs, we have to deal with the columns to the right */
5594 rcScroll.left = (nItemCol + 1) * infoPtr->nItemWidth;
5596 rcScroll.right = (infoPtr->nItemCount / nPerCol + 1) * infoPtr->nItemWidth;
5597 rcScroll.bottom = nPerCol * infoPtr->nItemHeight;
5598 OffsetRect(&rcScroll, Origin.x, Origin.y);
5599 if (IntersectRect(&rcScroll, &rcScroll, &infoPtr->rcList))
5600 ScrollWindowEx(infoPtr->hwndSelf, 0, dir * infoPtr->nItemHeight,
5601 &rcScroll, &rcScroll, 0, 0, SW_ERASE | SW_INVALIDATE);
5606 * Removes an item from the listview control.
5609 * [I] infoPtr : valid pointer to the listview structure
5610 * [I] nItem : item index
5616 static BOOL LISTVIEW_DeleteItem(LISTVIEW_INFO *infoPtr, INT nItem)
5619 const BOOL is_icon = (infoPtr->uView == LV_VIEW_SMALLICON || infoPtr->uView == LV_VIEW_ICON);
5621 TRACE("(nItem=%d)\n", nItem);
5623 if (nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
5625 /* remove selection, and focus */
5627 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
5628 LISTVIEW_SetItemState(infoPtr, nItem, &item);
5630 /* send LVN_DELETEITEM notification. */
5631 if (!notify_deleteitem(infoPtr, nItem)) return FALSE;
5633 /* we need to do this here, because we'll be deleting stuff */
5635 LISTVIEW_InvalidateItem(infoPtr, nItem);
5637 if (!(infoPtr->dwStyle & LVS_OWNERDATA))
5645 hdpaSubItems = DPA_DeletePtr(infoPtr->hdpaItems, nItem);
5646 lpItem = DPA_GetPtr(hdpaSubItems, 0);
5648 /* free id struct */
5649 i = DPA_GetPtrIndex(infoPtr->hdpaItemIds, lpItem->id);
5650 lpID = DPA_GetPtr(infoPtr->hdpaItemIds, i);
5651 DPA_DeletePtr(infoPtr->hdpaItemIds, i);
5653 for (i = 0; i < DPA_GetPtrCount(hdpaSubItems); i++)
5655 hdrItem = DPA_GetPtr(hdpaSubItems, i);
5656 if (is_text(hdrItem->pszText)) Free(hdrItem->pszText);
5659 DPA_Destroy(hdpaSubItems);
5664 DPA_DeletePtr(infoPtr->hdpaPosX, nItem);
5665 DPA_DeletePtr(infoPtr->hdpaPosY, nItem);
5668 infoPtr->nItemCount--;
5669 LISTVIEW_ShiftIndices(infoPtr, nItem, -1);
5671 /* now is the invalidation fun */
5673 LISTVIEW_ScrollOnInsert(infoPtr, nItem, -1);
5680 * Callback implementation for editlabel control
5683 * [I] infoPtr : valid pointer to the listview structure
5684 * [I] storeText : store edit box text as item text
5685 * [I] isW : TRUE if psxText is Unicode, FALSE if it's ANSI
5691 static BOOL LISTVIEW_EndEditLabelT(LISTVIEW_INFO *infoPtr, BOOL storeText, BOOL isW)
5693 HWND hwndSelf = infoPtr->hwndSelf;
5694 WCHAR szDispText[DISP_TEXT_SIZE] = { 0 };
5695 NMLVDISPINFOW dispInfo;
5696 INT editedItem = infoPtr->nEditLabelItem;
5698 WCHAR *pszText = NULL;
5703 DWORD len = isW ? GetWindowTextLengthW(infoPtr->hwndEdit) : GetWindowTextLengthA(infoPtr->hwndEdit);
5707 if ((pszText = Alloc((len+1) * (isW ? sizeof(WCHAR) : sizeof(CHAR)))))
5709 if (isW) GetWindowTextW(infoPtr->hwndEdit, pszText, len+1);
5710 else GetWindowTextA(infoPtr->hwndEdit, (CHAR*)pszText, len+1);
5715 TRACE("(pszText=%s, isW=%d)\n", debugtext_t(pszText, isW), isW);
5717 ZeroMemory(&dispInfo, sizeof(dispInfo));
5718 dispInfo.item.mask = LVIF_PARAM | LVIF_STATE | LVIF_TEXT;
5719 dispInfo.item.iItem = editedItem;
5720 dispInfo.item.iSubItem = 0;
5721 dispInfo.item.stateMask = ~0;
5722 dispInfo.item.pszText = szDispText;
5723 dispInfo.item.cchTextMax = DISP_TEXT_SIZE;
5724 if (!LISTVIEW_GetItemT(infoPtr, &dispInfo.item, isW))
5731 same = (lstrcmpW(dispInfo.item.pszText, pszText) == 0);
5734 LPWSTR tmp = textdupTtoW(pszText, FALSE);
5735 same = (lstrcmpW(dispInfo.item.pszText, tmp) == 0);
5736 textfreeT(tmp, FALSE);
5739 /* add the text from the edit in */
5740 dispInfo.item.mask |= LVIF_TEXT;
5741 dispInfo.item.pszText = same ? NULL : pszText;
5742 dispInfo.item.cchTextMax = textlenT(dispInfo.item.pszText, isW);
5744 /* Do we need to update the Item Text */
5745 res = notify_dispinfoT(infoPtr, LVN_ENDLABELEDITW, &dispInfo, isW);
5747 infoPtr->nEditLabelItem = -1;
5748 infoPtr->hwndEdit = 0;
5750 if (!res) goto cleanup;
5752 if (!IsWindow(hwndSelf))
5757 if (!pszText) return TRUE;
5764 if (!(infoPtr->dwStyle & LVS_OWNERDATA))
5766 HDPA hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, editedItem);
5767 ITEM_INFO* lpItem = DPA_GetPtr(hdpaSubItems, 0);
5768 if (lpItem && lpItem->hdr.pszText == LPSTR_TEXTCALLBACKW)
5770 LISTVIEW_InvalidateItem(infoPtr, editedItem);
5776 ZeroMemory(&dispInfo, sizeof(dispInfo));
5777 dispInfo.item.mask = LVIF_TEXT;
5778 dispInfo.item.iItem = editedItem;
5779 dispInfo.item.iSubItem = 0;
5780 dispInfo.item.pszText = pszText;
5781 dispInfo.item.cchTextMax = textlenT(pszText, isW);
5782 res = LISTVIEW_SetItemT(infoPtr, &dispInfo.item, isW);
5792 * Subclassed edit control windproc function
5795 * [I] hwnd : the edit window handle
5796 * [I] uMsg : the message that is to be processed
5797 * [I] wParam : first message parameter
5798 * [I] lParam : second message parameter
5799 * [I] isW : TRUE if input is Unicode
5804 static LRESULT EditLblWndProcT(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL isW)
5806 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongPtrW(GetParent(hwnd), 0);
5809 TRACE("(hwnd=%p, uMsg=%x, wParam=%lx, lParam=%lx, isW=%d)\n",
5810 hwnd, uMsg, wParam, lParam, isW);
5815 return DLGC_WANTARROWS | DLGC_WANTALLKEYS;
5819 WNDPROC editProc = infoPtr->EditWndProc;
5820 infoPtr->EditWndProc = 0;
5821 SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (DWORD_PTR)editProc);
5822 return CallWindowProcT(editProc, hwnd, uMsg, wParam, lParam, isW);
5826 if (VK_ESCAPE == (INT)wParam)
5831 else if (VK_RETURN == (INT)wParam)
5835 return CallWindowProcT(infoPtr->EditWndProc, hwnd, uMsg, wParam, lParam, isW);
5839 if (infoPtr->hwndEdit)
5840 LISTVIEW_EndEditLabelT(infoPtr, save, isW);
5842 SendMessageW(hwnd, WM_CLOSE, 0, 0);
5848 * Subclassed edit control Unicode windproc function
5851 * [I] hwnd : the edit window handle
5852 * [I] uMsg : the message that is to be processed
5853 * [I] wParam : first message parameter
5854 * [I] lParam : second message parameter
5858 static LRESULT CALLBACK EditLblWndProcW(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
5860 return EditLblWndProcT(hwnd, uMsg, wParam, lParam, TRUE);
5865 * Subclassed edit control ANSI windproc function
5868 * [I] hwnd : the edit window handle
5869 * [I] uMsg : the message that is to be processed
5870 * [I] wParam : first message parameter
5871 * [I] lParam : second message parameter
5875 static LRESULT CALLBACK EditLblWndProcA(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
5877 return EditLblWndProcT(hwnd, uMsg, wParam, lParam, FALSE);
5882 * Creates a subclassed edit control
5885 * [I] infoPtr : valid pointer to the listview structure
5886 * [I] text : initial text for the edit
5887 * [I] style : the window style
5888 * [I] isW : TRUE if input is Unicode
5892 static HWND CreateEditLabelT(LISTVIEW_INFO *infoPtr, LPCWSTR text, BOOL isW)
5894 static const DWORD style = WS_CHILDWINDOW|WS_CLIPSIBLINGS|ES_LEFT|ES_AUTOHSCROLL|WS_BORDER|WS_VISIBLE;
5895 HINSTANCE hinst = (HINSTANCE)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_HINSTANCE);
5898 TRACE("(%p, text=%s, isW=%d)\n", infoPtr, debugtext_t(text, isW), isW);
5900 /* window will be resized and positioned after LVN_BEGINLABELEDIT */
5902 hedit = CreateWindowW(WC_EDITW, text, style, 0, 0, 0, 0, infoPtr->hwndSelf, 0, hinst, 0);
5904 hedit = CreateWindowA(WC_EDITA, (LPCSTR)text, style, 0, 0, 0, 0, infoPtr->hwndSelf, 0, hinst, 0);
5906 if (!hedit) return 0;
5908 infoPtr->EditWndProc = (WNDPROC)
5909 (isW ? SetWindowLongPtrW(hedit, GWLP_WNDPROC, (DWORD_PTR)EditLblWndProcW) :
5910 SetWindowLongPtrA(hedit, GWLP_WNDPROC, (DWORD_PTR)EditLblWndProcA) );
5912 SendMessageW(hedit, WM_SETFONT, (WPARAM)infoPtr->hFont, FALSE);
5913 SendMessageW(hedit, EM_SETLIMITTEXT, DISP_TEXT_SIZE-1, 0);
5920 * Begin in place editing of specified list view item
5923 * [I] infoPtr : valid pointer to the listview structure
5924 * [I] nItem : item index
5925 * [I] isW : TRUE if it's a Unicode req, FALSE if ASCII
5931 static HWND LISTVIEW_EditLabelT(LISTVIEW_INFO *infoPtr, INT nItem, BOOL isW)
5933 WCHAR disptextW[DISP_TEXT_SIZE] = { 0 };
5934 HWND hwndSelf = infoPtr->hwndSelf;
5935 NMLVDISPINFOW dispInfo;
5936 HFONT hOldFont = NULL;
5942 TRACE("(nItem=%d, isW=%d)\n", nItem, isW);
5944 if (~infoPtr->dwStyle & LVS_EDITLABELS) return 0;
5946 /* remove existing edit box */
5947 if (infoPtr->hwndEdit)
5949 SetFocus(infoPtr->hwndSelf);
5950 infoPtr->hwndEdit = 0;
5953 if (nItem < 0 || nItem >= infoPtr->nItemCount) return 0;
5955 infoPtr->nEditLabelItem = nItem;
5957 LISTVIEW_SetSelection(infoPtr, nItem);
5958 LISTVIEW_SetItemFocus(infoPtr, nItem);
5959 LISTVIEW_InvalidateItem(infoPtr, nItem);
5961 rect.left = LVIR_LABEL;
5962 if (!LISTVIEW_GetItemRect(infoPtr, nItem, &rect)) return 0;
5964 ZeroMemory(&dispInfo, sizeof(dispInfo));
5965 dispInfo.item.mask = LVIF_PARAM | LVIF_STATE | LVIF_TEXT;
5966 dispInfo.item.iItem = nItem;
5967 dispInfo.item.iSubItem = 0;
5968 dispInfo.item.stateMask = ~0;
5969 dispInfo.item.pszText = disptextW;
5970 dispInfo.item.cchTextMax = DISP_TEXT_SIZE;
5971 if (!LISTVIEW_GetItemT(infoPtr, &dispInfo.item, isW)) return 0;
5973 infoPtr->hwndEdit = CreateEditLabelT(infoPtr, dispInfo.item.pszText, isW);
5974 if (!infoPtr->hwndEdit) return 0;
5976 if (notify_dispinfoT(infoPtr, LVN_BEGINLABELEDITW, &dispInfo, isW))
5978 if (!IsWindow(hwndSelf))
5980 SendMessageW(infoPtr->hwndEdit, WM_CLOSE, 0, 0);
5981 infoPtr->hwndEdit = 0;
5985 TRACE("disp text=%s\n", debugtext_t(dispInfo.item.pszText, isW));
5987 /* position and display edit box */
5988 hdc = GetDC(infoPtr->hwndSelf);
5990 /* select the font to get appropriate metric dimensions */
5992 hOldFont = SelectObject(hdc, infoPtr->hFont);
5994 /* use real edit box content, it could be altered during LVN_BEGINLABELEDIT notification */
5995 GetWindowTextW(infoPtr->hwndEdit, disptextW, DISP_TEXT_SIZE);
5996 TRACE("edit box text=%s\n", debugstr_w(disptextW));
5998 /* get string length in pixels */
5999 GetTextExtentPoint32W(hdc, disptextW, lstrlenW(disptextW), &sz);
6001 /* add extra spacing for the next character */
6002 GetTextMetricsW(hdc, &tm);
6003 sz.cx += tm.tmMaxCharWidth * 2;
6006 SelectObject(hdc, hOldFont);
6008 ReleaseDC(infoPtr->hwndSelf, hdc);
6010 sz.cy = rect.bottom - rect.top + 2;
6013 TRACE("moving edit=(%d,%d)-(%d,%d)\n", rect.left, rect.top, sz.cx, sz.cy);
6014 MoveWindow(infoPtr->hwndEdit, rect.left, rect.top, sz.cx, sz.cy, FALSE);
6015 ShowWindow(infoPtr->hwndEdit, SW_NORMAL);
6016 SetFocus(infoPtr->hwndEdit);
6017 SendMessageW(infoPtr->hwndEdit, EM_SETSEL, 0, -1);
6018 return infoPtr->hwndEdit;
6024 * Ensures the specified item is visible, scrolling into view if necessary.
6027 * [I] infoPtr : valid pointer to the listview structure
6028 * [I] nItem : item index
6029 * [I] bPartial : partially or entirely visible
6035 static BOOL LISTVIEW_EnsureVisible(LISTVIEW_INFO *infoPtr, INT nItem, BOOL bPartial)
6037 INT nScrollPosHeight = 0;
6038 INT nScrollPosWidth = 0;
6039 INT nHorzAdjust = 0;
6040 INT nVertAdjust = 0;
6043 RECT rcItem, rcTemp;
6045 rcItem.left = LVIR_BOUNDS;
6046 if (!LISTVIEW_GetItemRect(infoPtr, nItem, &rcItem)) return FALSE;
6048 if (bPartial && IntersectRect(&rcTemp, &infoPtr->rcList, &rcItem)) return TRUE;
6050 if (rcItem.left < infoPtr->rcList.left || rcItem.right > infoPtr->rcList.right)
6052 /* scroll left/right, but in LV_VIEW_DETAILS mode */
6053 if (infoPtr->uView == LV_VIEW_LIST)
6054 nScrollPosWidth = infoPtr->nItemWidth;
6055 else if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
6056 nScrollPosWidth = 1;
6058 if (rcItem.left < infoPtr->rcList.left)
6061 if (infoPtr->uView != LV_VIEW_DETAILS) nHorzDiff = rcItem.left - infoPtr->rcList.left;
6066 if (infoPtr->uView != LV_VIEW_DETAILS) nHorzDiff = rcItem.right - infoPtr->rcList.right;
6070 if (rcItem.top < infoPtr->rcList.top || rcItem.bottom > infoPtr->rcList.bottom)
6072 /* scroll up/down, but not in LVS_LIST mode */
6073 if (infoPtr->uView == LV_VIEW_DETAILS)
6074 nScrollPosHeight = infoPtr->nItemHeight;
6075 else if ((infoPtr->uView == LV_VIEW_ICON) || (infoPtr->uView == LV_VIEW_SMALLICON))
6076 nScrollPosHeight = 1;
6078 if (rcItem.top < infoPtr->rcList.top)
6081 if (infoPtr->uView != LV_VIEW_LIST) nVertDiff = rcItem.top - infoPtr->rcList.top;
6086 if (infoPtr->uView != LV_VIEW_LIST) nVertDiff = rcItem.bottom - infoPtr->rcList.bottom;
6090 if (!nScrollPosWidth && !nScrollPosHeight) return TRUE;
6092 if (nScrollPosWidth)
6094 INT diff = nHorzDiff / nScrollPosWidth;
6095 if (nHorzDiff % nScrollPosWidth) diff += nHorzAdjust;
6096 LISTVIEW_HScroll(infoPtr, SB_INTERNAL, diff);
6099 if (nScrollPosHeight)
6101 INT diff = nVertDiff / nScrollPosHeight;
6102 if (nVertDiff % nScrollPosHeight) diff += nVertAdjust;
6103 LISTVIEW_VScroll(infoPtr, SB_INTERNAL, diff);
6111 * Searches for an item with specific characteristics.
6114 * [I] hwnd : window handle
6115 * [I] nStart : base item index
6116 * [I] lpFindInfo : item information to look for
6119 * SUCCESS : index of item
6122 static INT LISTVIEW_FindItemW(const LISTVIEW_INFO *infoPtr, INT nStart,
6123 const LVFINDINFOW *lpFindInfo)
6125 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
6126 BOOL bWrap = FALSE, bNearest = FALSE;
6127 INT nItem = nStart + 1, nLast = infoPtr->nItemCount, nNearestItem = -1;
6128 ULONG xdist, ydist, dist, mindist = 0x7fffffff;
6129 POINT Position, Destination;
6132 /* Search in virtual listviews should be done by application, not by
6133 listview control, so we just send LVN_ODFINDITEMW and return the result */
6134 if (infoPtr->dwStyle & LVS_OWNERDATA)
6138 nmlv.iStart = nStart;
6139 nmlv.lvfi = *lpFindInfo;
6140 return notify_hdr(infoPtr, LVN_ODFINDITEMW, (LPNMHDR)&nmlv.hdr);
6143 if (!lpFindInfo || nItem < 0) return -1;
6146 if (lpFindInfo->flags & (LVFI_STRING | LVFI_PARTIAL) ||
6147 lpFindInfo->flags & LVFI_SUBSTRING)
6149 lvItem.mask |= LVIF_TEXT;
6150 lvItem.pszText = szDispText;
6151 lvItem.cchTextMax = DISP_TEXT_SIZE;
6154 if (lpFindInfo->flags & LVFI_WRAP)
6157 if ((lpFindInfo->flags & LVFI_NEARESTXY) &&
6158 (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON))
6163 LISTVIEW_GetOrigin(infoPtr, &Origin);
6164 Destination.x = lpFindInfo->pt.x - Origin.x;
6165 Destination.y = lpFindInfo->pt.y - Origin.y;
6166 switch(lpFindInfo->vkDirection)
6168 case VK_DOWN: Destination.y += infoPtr->nItemHeight; break;
6169 case VK_UP: Destination.y -= infoPtr->nItemHeight; break;
6170 case VK_RIGHT: Destination.x += infoPtr->nItemWidth; break;
6171 case VK_LEFT: Destination.x -= infoPtr->nItemWidth; break;
6172 case VK_HOME: Destination.x = Destination.y = 0; break;
6173 case VK_NEXT: Destination.y += infoPtr->rcList.bottom - infoPtr->rcList.top; break;
6174 case VK_PRIOR: Destination.y -= infoPtr->rcList.bottom - infoPtr->rcList.top; break;
6176 LISTVIEW_GetAreaRect(infoPtr, &rcArea);
6177 Destination.x = rcArea.right;
6178 Destination.y = rcArea.bottom;
6180 default: ERR("Unknown vkDirection=%d\n", lpFindInfo->vkDirection);
6184 else Destination.x = Destination.y = 0;
6186 /* if LVFI_PARAM is specified, all other flags are ignored */
6187 if (lpFindInfo->flags & LVFI_PARAM)
6189 lvItem.mask |= LVIF_PARAM;
6191 lvItem.mask &= ~LVIF_TEXT;
6195 for (; nItem < nLast; nItem++)
6197 lvItem.iItem = nItem;
6198 lvItem.iSubItem = 0;
6199 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) continue;
6201 if (lvItem.mask & LVIF_PARAM)
6203 if (lpFindInfo->lParam == lvItem.lParam)
6209 if (lvItem.mask & LVIF_TEXT)
6211 if (lpFindInfo->flags & (LVFI_PARTIAL | LVFI_SUBSTRING))
6213 WCHAR *p = strstrW(lvItem.pszText, lpFindInfo->psz);
6214 if (!p || p != lvItem.pszText) continue;
6218 if (lstrcmpW(lvItem.pszText, lpFindInfo->psz) != 0) continue;
6222 if (!bNearest) return nItem;
6224 /* This is very inefficient. To do a good job here,
6225 * we need a sorted array of (x,y) item positions */
6226 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
6228 /* compute the distance^2 to the destination */
6229 xdist = Destination.x - Position.x;
6230 ydist = Destination.y - Position.y;
6231 dist = xdist * xdist + ydist * ydist;
6233 /* remember the distance, and item if it's closer */
6237 nNearestItem = nItem;
6244 nLast = min(nStart + 1, infoPtr->nItemCount);
6249 return nNearestItem;
6254 * Searches for an item with specific characteristics.
6257 * [I] hwnd : window handle
6258 * [I] nStart : base item index
6259 * [I] lpFindInfo : item information to look for
6262 * SUCCESS : index of item
6265 static INT LISTVIEW_FindItemA(const LISTVIEW_INFO *infoPtr, INT nStart,
6266 const LVFINDINFOA *lpFindInfo)
6268 BOOL hasText = lpFindInfo->flags & (LVFI_STRING | LVFI_PARTIAL) ||
6269 lpFindInfo->flags & LVFI_SUBSTRING;
6274 memcpy(&fiw, lpFindInfo, sizeof(fiw));
6275 if (hasText) fiw.psz = strW = textdupTtoW((LPCWSTR)lpFindInfo->psz, FALSE);
6276 res = LISTVIEW_FindItemW(infoPtr, nStart, &fiw);
6277 textfreeT(strW, FALSE);
6283 * Retrieves column attributes.
6286 * [I] infoPtr : valid pointer to the listview structure
6287 * [I] nColumn : column index
6288 * [IO] lpColumn : column information
6289 * [I] isW : if TRUE, then lpColumn is a LPLVCOLUMNW
6290 * otherwise it is in fact a LPLVCOLUMNA
6296 static BOOL LISTVIEW_GetColumnT(const LISTVIEW_INFO *infoPtr, INT nColumn, LPLVCOLUMNW lpColumn, BOOL isW)
6298 COLUMN_INFO *lpColumnInfo;
6301 if (!lpColumn || nColumn < 0 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
6302 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, nColumn);
6304 /* initialize memory */
6305 ZeroMemory(&hdi, sizeof(hdi));
6307 if (lpColumn->mask & LVCF_TEXT)
6309 hdi.mask |= HDI_TEXT;
6310 hdi.pszText = lpColumn->pszText;
6311 hdi.cchTextMax = lpColumn->cchTextMax;
6314 if (lpColumn->mask & LVCF_IMAGE)
6315 hdi.mask |= HDI_IMAGE;
6317 if (lpColumn->mask & LVCF_ORDER)
6318 hdi.mask |= HDI_ORDER;
6320 if (lpColumn->mask & LVCF_SUBITEM)
6321 hdi.mask |= HDI_LPARAM;
6323 if (!SendMessageW(infoPtr->hwndHeader, isW ? HDM_GETITEMW : HDM_GETITEMA, nColumn, (LPARAM)&hdi)) return FALSE;
6325 if (lpColumn->mask & LVCF_FMT)
6326 lpColumn->fmt = lpColumnInfo->fmt;
6328 if (lpColumn->mask & LVCF_WIDTH)
6329 lpColumn->cx = lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left;
6331 if (lpColumn->mask & LVCF_IMAGE)
6332 lpColumn->iImage = hdi.iImage;
6334 if (lpColumn->mask & LVCF_ORDER)
6335 lpColumn->iOrder = hdi.iOrder;
6337 if (lpColumn->mask & LVCF_SUBITEM)
6338 lpColumn->iSubItem = hdi.lParam;
6340 if (lpColumn->mask & LVCF_MINWIDTH)
6341 lpColumn->cxMin = lpColumnInfo->cxMin;
6347 static BOOL LISTVIEW_GetColumnOrderArray(const LISTVIEW_INFO *infoPtr, INT iCount, LPINT lpiArray)
6349 TRACE("iCount=%d, lpiArray=%p\n", iCount, lpiArray);
6354 return SendMessageW(infoPtr->hwndHeader, HDM_GETORDERARRAY, iCount, (LPARAM)lpiArray);
6359 * Retrieves the column width.
6362 * [I] infoPtr : valid pointer to the listview structure
6363 * [I] int : column index
6366 * SUCCESS : column width
6369 static INT LISTVIEW_GetColumnWidth(const LISTVIEW_INFO *infoPtr, INT nColumn)
6371 INT nColumnWidth = 0;
6374 TRACE("nColumn=%d\n", nColumn);
6376 /* we have a 'column' in LIST and REPORT mode only */
6377 switch(infoPtr->uView)
6380 nColumnWidth = infoPtr->nItemWidth;
6382 case LV_VIEW_DETAILS:
6383 /* We are not using LISTVIEW_GetHeaderRect as this data is updated only after a HDN_ITEMCHANGED.
6384 * There is an application that subclasses the listview, calls LVM_GETCOLUMNWIDTH in the
6385 * HDN_ITEMCHANGED handler and goes into infinite recursion if it receives old data.
6387 * TODO: should we do the same in LVM_GETCOLUMN?
6389 hdItem.mask = HDI_WIDTH;
6390 if (!SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nColumn, (LPARAM)&hdItem))
6392 WARN("(%p): HDM_GETITEMW failed for item %d\n", infoPtr->hwndSelf, nColumn);
6395 nColumnWidth = hdItem.cxy;
6399 TRACE("nColumnWidth=%d\n", nColumnWidth);
6400 return nColumnWidth;
6405 * In list or report display mode, retrieves the number of items that can fit
6406 * vertically in the visible area. In icon or small icon display mode,
6407 * retrieves the total number of visible items.
6410 * [I] infoPtr : valid pointer to the listview structure
6413 * Number of fully visible items.
6415 static INT LISTVIEW_GetCountPerPage(const LISTVIEW_INFO *infoPtr)
6417 switch (infoPtr->uView)
6420 case LV_VIEW_SMALLICON:
6421 return infoPtr->nItemCount;
6422 case LV_VIEW_DETAILS:
6423 return LISTVIEW_GetCountPerColumn(infoPtr);
6425 return LISTVIEW_GetCountPerRow(infoPtr) * LISTVIEW_GetCountPerColumn(infoPtr);
6433 * Retrieves an image list handle.
6436 * [I] infoPtr : valid pointer to the listview structure
6437 * [I] nImageList : image list identifier
6440 * SUCCESS : image list handle
6443 static HIMAGELIST LISTVIEW_GetImageList(const LISTVIEW_INFO *infoPtr, INT nImageList)
6447 case LVSIL_NORMAL: return infoPtr->himlNormal;
6448 case LVSIL_SMALL: return infoPtr->himlSmall;
6449 case LVSIL_STATE: return infoPtr->himlState;
6450 case LVSIL_GROUPHEADER:
6451 FIXME("LVSIL_GROUPHEADER not supported\n");
6454 WARN("got unknown imagelist index - %d\n", nImageList);
6459 /* LISTVIEW_GetISearchString */
6463 * Retrieves item attributes.
6466 * [I] hwnd : window handle
6467 * [IO] lpLVItem : item info
6468 * [I] isW : if TRUE, then lpLVItem is a LPLVITEMW,
6469 * if FALSE, then lpLVItem is a LPLVITEMA.
6472 * This is the internal 'GetItem' interface -- it tries to
6473 * be smart and avoid text copies, if possible, by modifying
6474 * lpLVItem->pszText to point to the text string. Please note
6475 * that this is not always possible (e.g. OWNERDATA), so on
6476 * entry you *must* supply valid values for pszText, and cchTextMax.
6477 * The only difference to the documented interface is that upon
6478 * return, you should use *only* the lpLVItem->pszText, rather than
6479 * the buffer pointer you provided on input. Most code already does
6480 * that, so it's not a problem.
6481 * For the two cases when the text must be copied (that is,
6482 * for LVM_GETITEM, and LVM_GETITEMTEXT), use LISTVIEW_GetItemExtT.
6488 static BOOL LISTVIEW_GetItemT(const LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem, BOOL isW)
6490 ITEMHDR callbackHdr = { LPSTR_TEXTCALLBACKW, I_IMAGECALLBACK };
6491 NMLVDISPINFOW dispInfo;
6497 TRACE("(lpLVItem=%s, isW=%d)\n", debuglvitem_t(lpLVItem, isW), isW);
6499 if (!lpLVItem || lpLVItem->iItem < 0 || lpLVItem->iItem >= infoPtr->nItemCount)
6502 if (lpLVItem->mask == 0) return TRUE;
6503 TRACE("mask=%x\n", lpLVItem->mask);
6505 /* make a local copy */
6506 isubitem = lpLVItem->iSubItem;
6508 /* a quick optimization if all we're asked is the focus state
6509 * these queries are worth optimising since they are common,
6510 * and can be answered in constant time, without the heavy accesses */
6511 if ( (lpLVItem->mask == LVIF_STATE) && (lpLVItem->stateMask == LVIS_FOCUSED) &&
6512 !(infoPtr->uCallbackMask & LVIS_FOCUSED) )
6514 lpLVItem->state = 0;
6515 if (infoPtr->nFocusedItem == lpLVItem->iItem)
6516 lpLVItem->state |= LVIS_FOCUSED;
6520 ZeroMemory(&dispInfo, sizeof(dispInfo));
6522 /* if the app stores all the data, handle it separately */
6523 if (infoPtr->dwStyle & LVS_OWNERDATA)
6525 dispInfo.item.state = 0;
6527 /* apparently, we should not callback for lParam in LVS_OWNERDATA */
6528 if ((lpLVItem->mask & ~(LVIF_STATE | LVIF_PARAM)) ||
6529 ((lpLVItem->mask & LVIF_STATE) && (infoPtr->uCallbackMask & lpLVItem->stateMask)))
6531 UINT mask = lpLVItem->mask;
6533 /* NOTE: copy only fields which we _know_ are initialized, some apps
6534 * depend on the uninitialized fields being 0 */
6535 dispInfo.item.mask = lpLVItem->mask & ~LVIF_PARAM;
6536 dispInfo.item.iItem = lpLVItem->iItem;
6537 dispInfo.item.iSubItem = isubitem;
6538 if (lpLVItem->mask & LVIF_TEXT)
6540 if (lpLVItem->mask & LVIF_NORECOMPUTE)
6542 dispInfo.item.mask &= ~(LVIF_TEXT | LVIF_NORECOMPUTE);
6545 dispInfo.item.pszText = lpLVItem->pszText;
6546 dispInfo.item.cchTextMax = lpLVItem->cchTextMax;
6549 if (lpLVItem->mask & LVIF_STATE)
6550 dispInfo.item.stateMask = lpLVItem->stateMask & infoPtr->uCallbackMask;
6551 /* could be zeroed on LVIF_NORECOMPUTE case */
6552 if (dispInfo.item.mask)
6554 notify_dispinfoT(infoPtr, LVN_GETDISPINFOW, &dispInfo, isW);
6555 dispInfo.item.stateMask = lpLVItem->stateMask;
6556 if (lpLVItem->mask & (LVIF_GROUPID|LVIF_COLUMNS))
6558 /* full size structure expected - _WIN32IE >= 0x560 */
6559 *lpLVItem = dispInfo.item;
6561 else if (lpLVItem->mask & LVIF_INDENT)
6563 /* indent member expected - _WIN32IE >= 0x300 */
6564 memcpy(lpLVItem, &dispInfo.item, offsetof( LVITEMW, iGroupId ));
6568 /* minimal structure expected */
6569 memcpy(lpLVItem, &dispInfo.item, offsetof( LVITEMW, iIndent ));
6571 lpLVItem->mask = mask;
6572 TRACE(" getdispinfo(1):lpLVItem=%s\n", debuglvitem_t(lpLVItem, isW));
6576 /* make sure lParam is zeroed out */
6577 if (lpLVItem->mask & LVIF_PARAM) lpLVItem->lParam = 0;
6579 /* callback marked pointer required here */
6580 if ((lpLVItem->mask & LVIF_TEXT) && (lpLVItem->mask & LVIF_NORECOMPUTE))
6581 lpLVItem->pszText = LPSTR_TEXTCALLBACKW;
6583 /* we store only a little state, so if we're not asked, we're done */
6584 if (!(lpLVItem->mask & LVIF_STATE) || isubitem) return TRUE;
6586 /* if focus is handled by us, report it */
6587 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_FOCUSED )
6589 lpLVItem->state &= ~LVIS_FOCUSED;
6590 if (infoPtr->nFocusedItem == lpLVItem->iItem)
6591 lpLVItem->state |= LVIS_FOCUSED;
6594 /* and do the same for selection, if we handle it */
6595 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_SELECTED )
6597 lpLVItem->state &= ~LVIS_SELECTED;
6598 if (ranges_contain(infoPtr->selectionRanges, lpLVItem->iItem))
6599 lpLVItem->state |= LVIS_SELECTED;
6605 /* find the item and subitem structures before we proceed */
6606 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, lpLVItem->iItem);
6607 lpItem = DPA_GetPtr(hdpaSubItems, 0);
6612 SUBITEM_INFO *lpSubItem = LISTVIEW_GetSubItemPtr(hdpaSubItems, isubitem);
6613 pItemHdr = lpSubItem ? &lpSubItem->hdr : &callbackHdr;
6616 WARN(" iSubItem invalid (%08x), ignored.\n", isubitem);
6621 pItemHdr = &lpItem->hdr;
6623 /* Do we need to query the state from the app? */
6624 if ((lpLVItem->mask & LVIF_STATE) && infoPtr->uCallbackMask && isubitem == 0)
6626 dispInfo.item.mask |= LVIF_STATE;
6627 dispInfo.item.stateMask = infoPtr->uCallbackMask;
6630 /* Do we need to enquire about the image? */
6631 if ((lpLVItem->mask & LVIF_IMAGE) && pItemHdr->iImage == I_IMAGECALLBACK &&
6632 (isubitem == 0 || (infoPtr->dwLvExStyle & LVS_EX_SUBITEMIMAGES)))
6634 dispInfo.item.mask |= LVIF_IMAGE;
6635 dispInfo.item.iImage = I_IMAGECALLBACK;
6638 /* Only items support indentation */
6639 if ((lpLVItem->mask & LVIF_INDENT) && lpItem->iIndent == I_INDENTCALLBACK &&
6642 dispInfo.item.mask |= LVIF_INDENT;
6643 dispInfo.item.iIndent = I_INDENTCALLBACK;
6646 /* Apps depend on calling back for text if it is NULL or LPSTR_TEXTCALLBACKW */
6647 if ((lpLVItem->mask & LVIF_TEXT) && !(lpLVItem->mask & LVIF_NORECOMPUTE) &&
6648 !is_text(pItemHdr->pszText))
6650 dispInfo.item.mask |= LVIF_TEXT;
6651 dispInfo.item.pszText = lpLVItem->pszText;
6652 dispInfo.item.cchTextMax = lpLVItem->cchTextMax;
6653 if (dispInfo.item.pszText && dispInfo.item.cchTextMax > 0)
6654 *dispInfo.item.pszText = '\0';
6657 /* If we don't have all the requested info, query the application */
6658 if (dispInfo.item.mask)
6660 dispInfo.item.iItem = lpLVItem->iItem;
6661 dispInfo.item.iSubItem = lpLVItem->iSubItem; /* yes: the original subitem */
6662 dispInfo.item.lParam = lpItem->lParam;
6663 notify_dispinfoT(infoPtr, LVN_GETDISPINFOW, &dispInfo, isW);
6664 TRACE(" getdispinfo(2):item=%s\n", debuglvitem_t(&dispInfo.item, isW));
6667 /* we should not store values for subitems */
6668 if (isubitem) dispInfo.item.mask &= ~LVIF_DI_SETITEM;
6670 /* Now, handle the iImage field */
6671 if (dispInfo.item.mask & LVIF_IMAGE)
6673 lpLVItem->iImage = dispInfo.item.iImage;
6674 if ((dispInfo.item.mask & LVIF_DI_SETITEM) && pItemHdr->iImage == I_IMAGECALLBACK)
6675 pItemHdr->iImage = dispInfo.item.iImage;
6677 else if (lpLVItem->mask & LVIF_IMAGE)
6679 if(isubitem == 0 || (infoPtr->dwLvExStyle & LVS_EX_SUBITEMIMAGES))
6680 lpLVItem->iImage = pItemHdr->iImage;
6682 lpLVItem->iImage = 0;
6685 /* The pszText field */
6686 if (dispInfo.item.mask & LVIF_TEXT)
6688 if ((dispInfo.item.mask & LVIF_DI_SETITEM) && pItemHdr->pszText)
6689 textsetptrT(&pItemHdr->pszText, dispInfo.item.pszText, isW);
6691 lpLVItem->pszText = dispInfo.item.pszText;
6693 else if (lpLVItem->mask & LVIF_TEXT)
6695 /* if LVN_GETDISPINFO's disabled with LVIF_NORECOMPUTE return callback placeholder */
6696 if (isW || !is_text(pItemHdr->pszText)) lpLVItem->pszText = pItemHdr->pszText;
6697 else textcpynT(lpLVItem->pszText, isW, pItemHdr->pszText, TRUE, lpLVItem->cchTextMax);
6700 /* Next is the lParam field */
6701 if (dispInfo.item.mask & LVIF_PARAM)
6703 lpLVItem->lParam = dispInfo.item.lParam;
6704 if ((dispInfo.item.mask & LVIF_DI_SETITEM))
6705 lpItem->lParam = dispInfo.item.lParam;
6707 else if (lpLVItem->mask & LVIF_PARAM)
6708 lpLVItem->lParam = lpItem->lParam;
6710 /* if this is a subitem, we're done */
6711 if (isubitem) return TRUE;
6713 /* ... the state field (this one is different due to uCallbackmask) */
6714 if (lpLVItem->mask & LVIF_STATE)
6716 lpLVItem->state = lpItem->state & lpLVItem->stateMask;
6717 if (dispInfo.item.mask & LVIF_STATE)
6719 lpLVItem->state &= ~dispInfo.item.stateMask;
6720 lpLVItem->state |= (dispInfo.item.state & dispInfo.item.stateMask);
6722 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_FOCUSED )
6724 lpLVItem->state &= ~LVIS_FOCUSED;
6725 if (infoPtr->nFocusedItem == lpLVItem->iItem)
6726 lpLVItem->state |= LVIS_FOCUSED;
6728 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_SELECTED )
6730 lpLVItem->state &= ~LVIS_SELECTED;
6731 if (ranges_contain(infoPtr->selectionRanges, lpLVItem->iItem))
6732 lpLVItem->state |= LVIS_SELECTED;
6736 /* and last, but not least, the indent field */
6737 if (dispInfo.item.mask & LVIF_INDENT)
6739 lpLVItem->iIndent = dispInfo.item.iIndent;
6740 if ((dispInfo.item.mask & LVIF_DI_SETITEM) && lpItem->iIndent == I_INDENTCALLBACK)
6741 lpItem->iIndent = dispInfo.item.iIndent;
6743 else if (lpLVItem->mask & LVIF_INDENT)
6745 lpLVItem->iIndent = lpItem->iIndent;
6753 * Retrieves item attributes.
6756 * [I] hwnd : window handle
6757 * [IO] lpLVItem : item info
6758 * [I] isW : if TRUE, then lpLVItem is a LPLVITEMW,
6759 * if FALSE, then lpLVItem is a LPLVITEMA.
6762 * This is the external 'GetItem' interface -- it properly copies
6763 * the text in the provided buffer.
6769 static BOOL LISTVIEW_GetItemExtT(const LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem, BOOL isW)
6774 if (!lpLVItem || lpLVItem->iItem < 0 || lpLVItem->iItem >= infoPtr->nItemCount)
6777 pszText = lpLVItem->pszText;
6778 bResult = LISTVIEW_GetItemT(infoPtr, lpLVItem, isW);
6779 if (bResult && (lpLVItem->mask & LVIF_TEXT) && lpLVItem->pszText != pszText)
6781 if (lpLVItem->pszText != LPSTR_TEXTCALLBACKW)
6782 textcpynT(pszText, isW, lpLVItem->pszText, isW, lpLVItem->cchTextMax);
6784 pszText = LPSTR_TEXTCALLBACKW;
6786 lpLVItem->pszText = pszText;
6794 * Retrieves the position (upper-left) of the listview control item.
6795 * Note that for LVS_ICON style, the upper-left is that of the icon
6796 * and not the bounding box.
6799 * [I] infoPtr : valid pointer to the listview structure
6800 * [I] nItem : item index
6801 * [O] lpptPosition : coordinate information
6807 static BOOL LISTVIEW_GetItemPosition(const LISTVIEW_INFO *infoPtr, INT nItem, LPPOINT lpptPosition)
6811 TRACE("(nItem=%d, lpptPosition=%p)\n", nItem, lpptPosition);
6813 if (!lpptPosition || nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
6815 LISTVIEW_GetOrigin(infoPtr, &Origin);
6816 LISTVIEW_GetItemOrigin(infoPtr, nItem, lpptPosition);
6818 if (infoPtr->uView == LV_VIEW_ICON)
6820 lpptPosition->x += (infoPtr->nItemWidth - infoPtr->iconSize.cx) / 2;
6821 lpptPosition->y += ICON_TOP_PADDING;
6823 lpptPosition->x += Origin.x;
6824 lpptPosition->y += Origin.y;
6826 TRACE (" lpptPosition=%s\n", wine_dbgstr_point(lpptPosition));
6833 * Retrieves the bounding rectangle for a listview control item.
6836 * [I] infoPtr : valid pointer to the listview structure
6837 * [I] nItem : item index
6838 * [IO] lprc : bounding rectangle coordinates
6839 * lprc->left specifies the portion of the item for which the bounding
6840 * rectangle will be retrieved.
6842 * LVIR_BOUNDS Returns the bounding rectangle of the entire item,
6843 * including the icon and label.
6846 * * Experiment shows that native control returns:
6847 * * width = min (48, length of text line)
6848 * * .left = position.x - (width - iconsize.cx)/2
6849 * * .right = .left + width
6850 * * height = #lines of text * ntmHeight + icon height + 8
6851 * * .top = position.y - 2
6852 * * .bottom = .top + height
6853 * * separation between items .y = itemSpacing.cy - height
6854 * * .x = itemSpacing.cx - width
6855 * LVIR_ICON Returns the bounding rectangle of the icon or small icon.
6858 * * Experiment shows that native control returns:
6859 * * width = iconSize.cx + 16
6860 * * .left = position.x - (width - iconsize.cx)/2
6861 * * .right = .left + width
6862 * * height = iconSize.cy + 4
6863 * * .top = position.y - 2
6864 * * .bottom = .top + height
6865 * * separation between items .y = itemSpacing.cy - height
6866 * * .x = itemSpacing.cx - width
6867 * LVIR_LABEL Returns the bounding rectangle of the item text.
6870 * * Experiment shows that native control returns:
6871 * * width = text length
6872 * * .left = position.x - width/2
6873 * * .right = .left + width
6874 * * height = ntmH * linecount + 2
6875 * * .top = position.y + iconSize.cy + 6
6876 * * .bottom = .top + height
6877 * * separation between items .y = itemSpacing.cy - height
6878 * * .x = itemSpacing.cx - width
6879 * LVIR_SELECTBOUNDS Returns the union of the LVIR_ICON and LVIR_LABEL
6880 * rectangles, but excludes columns in report view.
6887 * Note that the bounding rectangle of the label in the LVS_ICON view depends
6888 * upon whether the window has the focus currently and on whether the item
6889 * is the one with the focus. Ensure that the control's record of which
6890 * item has the focus agrees with the items' records.
6892 static BOOL LISTVIEW_GetItemRect(const LISTVIEW_INFO *infoPtr, INT nItem, LPRECT lprc)
6894 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
6895 BOOL doLabel = TRUE, oversizedBox = FALSE;
6896 POINT Position, Origin;
6900 TRACE("(hwnd=%p, nItem=%d, lprc=%p)\n", infoPtr->hwndSelf, nItem, lprc);
6902 if (!lprc || nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
6904 LISTVIEW_GetOrigin(infoPtr, &Origin);
6905 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
6907 /* Be smart and try to figure out the minimum we have to do */
6908 if (lprc->left == LVIR_ICON) doLabel = FALSE;
6909 if (infoPtr->uView == LV_VIEW_DETAILS && lprc->left == LVIR_BOUNDS) doLabel = FALSE;
6910 if (infoPtr->uView == LV_VIEW_ICON && lprc->left != LVIR_ICON &&
6911 infoPtr->bFocus && LISTVIEW_GetItemState(infoPtr, nItem, LVIS_FOCUSED))
6912 oversizedBox = TRUE;
6914 /* get what we need from the item before hand, so we make
6915 * only one request. This can speed up things, if data
6916 * is stored on the app side */
6918 if (infoPtr->uView == LV_VIEW_DETAILS) lvItem.mask |= LVIF_INDENT;
6919 if (doLabel) lvItem.mask |= LVIF_TEXT;
6920 lvItem.iItem = nItem;
6921 lvItem.iSubItem = 0;
6922 lvItem.pszText = szDispText;
6923 lvItem.cchTextMax = DISP_TEXT_SIZE;
6924 if (lvItem.mask && !LISTVIEW_GetItemW(infoPtr, &lvItem)) return FALSE;
6925 /* we got the state already up, simulate it here, to avoid a reget */
6926 if (infoPtr->uView == LV_VIEW_ICON && (lprc->left != LVIR_ICON))
6928 lvItem.mask |= LVIF_STATE;
6929 lvItem.stateMask = LVIS_FOCUSED;
6930 lvItem.state = (oversizedBox ? LVIS_FOCUSED : 0);
6933 if (infoPtr->uView == LV_VIEW_DETAILS && (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) && lprc->left == LVIR_SELECTBOUNDS)
6934 lprc->left = LVIR_BOUNDS;
6940 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, NULL, lprc, NULL, NULL);
6944 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, NULL, NULL, NULL, lprc);
6948 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, lprc, NULL, NULL, NULL, NULL);
6951 case LVIR_SELECTBOUNDS:
6952 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, lprc, NULL, NULL, NULL);
6956 WARN("Unknown value: %d\n", lprc->left);
6960 if (infoPtr->uView == LV_VIEW_DETAILS)
6962 if (mode != LVIR_BOUNDS)
6963 OffsetRect(lprc, Origin.x + LISTVIEW_GetColumnInfo(infoPtr, 0)->rcHeader.left,
6964 Position.y + Origin.y);
6966 OffsetRect(lprc, Origin.x, Position.y + Origin.y);
6969 OffsetRect(lprc, Position.x + Origin.x, Position.y + Origin.y);
6971 TRACE(" rect=%s\n", wine_dbgstr_rect(lprc));
6978 * Retrieves the spacing between listview control items.
6981 * [I] infoPtr : valid pointer to the listview structure
6982 * [IO] lprc : rectangle to receive the output
6983 * on input, lprc->top = nSubItem
6984 * lprc->left = LVIR_ICON | LVIR_BOUNDS | LVIR_LABEL
6986 * NOTE: for subItem = 0, we should return the bounds of the _entire_ item,
6987 * not only those of the first column.
6988 * Fortunately, LISTVIEW_GetItemMetrics does the right thing.
6994 static BOOL LISTVIEW_GetSubItemRect(const LISTVIEW_INFO *infoPtr, INT nItem, LPRECT lprc)
6996 POINT Position, Origin;
7000 if (!lprc) return FALSE;
7002 nColumn = lprc->top;
7004 TRACE("(nItem=%d, nSubItem=%d, type=%d)\n", nItem, lprc->top, lprc->left);
7005 /* On WinNT, a subitem of '0' calls LISTVIEW_GetItemRect */
7007 return LISTVIEW_GetItemRect(infoPtr, nItem, lprc);
7009 if (infoPtr->uView != LV_VIEW_DETAILS) return FALSE;
7011 /* special case for header items */
7014 if (lprc->left != LVIR_BOUNDS)
7016 FIXME("Only LVIR_BOUNDS is implemented for header, got %d\n", lprc->left);
7020 if (infoPtr->hwndHeader)
7021 return SendMessageW(infoPtr->hwndHeader, HDM_GETITEMRECT, lprc->top, (LPARAM)lprc);
7024 memset(lprc, 0, sizeof(RECT));
7029 if (!LISTVIEW_GetItemPosition(infoPtr, nItem, &Position)) return FALSE;
7030 LISTVIEW_GetOrigin(infoPtr, &Origin);
7032 if (nColumn < 0 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
7035 lvItem.iItem = nItem;
7036 lvItem.iSubItem = nColumn;
7041 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, NULL, lprc, NULL, NULL);
7046 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, lprc, NULL, NULL, NULL, NULL);
7050 ERR("Unknown bounds=%d\n", lprc->left);
7054 OffsetRect(lprc, Origin.x, Position.y);
7055 TRACE("return rect %s\n", wine_dbgstr_rect(lprc));
7062 * Retrieves the spacing between listview control items.
7065 * [I] infoPtr : valid pointer to the listview structure
7066 * [I] bSmall : flag for small or large icon
7069 * Horizontal + vertical spacing
7071 static LONG LISTVIEW_GetItemSpacing(const LISTVIEW_INFO *infoPtr, BOOL bSmall)
7077 lResult = MAKELONG(infoPtr->iconSpacing.cx, infoPtr->iconSpacing.cy);
7081 if (infoPtr->uView == LV_VIEW_ICON)
7082 lResult = MAKELONG(DEFAULT_COLUMN_WIDTH, GetSystemMetrics(SM_CXSMICON)+HEIGHT_PADDING);
7084 lResult = MAKELONG(infoPtr->nItemWidth, infoPtr->nItemHeight);
7091 * Retrieves the state of a listview control item.
7094 * [I] infoPtr : valid pointer to the listview structure
7095 * [I] nItem : item index
7096 * [I] uMask : state mask
7099 * State specified by the mask.
7101 static UINT LISTVIEW_GetItemState(const LISTVIEW_INFO *infoPtr, INT nItem, UINT uMask)
7105 if (nItem < 0 || nItem >= infoPtr->nItemCount) return 0;
7107 lvItem.iItem = nItem;
7108 lvItem.iSubItem = 0;
7109 lvItem.mask = LVIF_STATE;
7110 lvItem.stateMask = uMask;
7111 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return 0;
7113 return lvItem.state & uMask;
7118 * Retrieves the text of a listview control item or subitem.
7121 * [I] hwnd : window handle
7122 * [I] nItem : item index
7123 * [IO] lpLVItem : item information
7124 * [I] isW : TRUE if lpLVItem is Unicode
7127 * SUCCESS : string length
7130 static INT LISTVIEW_GetItemTextT(const LISTVIEW_INFO *infoPtr, INT nItem, LPLVITEMW lpLVItem, BOOL isW)
7132 if (!lpLVItem || nItem < 0 || nItem >= infoPtr->nItemCount) return 0;
7134 lpLVItem->mask = LVIF_TEXT;
7135 lpLVItem->iItem = nItem;
7136 if (!LISTVIEW_GetItemExtT(infoPtr, lpLVItem, isW)) return 0;
7138 return textlenT(lpLVItem->pszText, isW);
7143 * Searches for an item based on properties + relationships.
7146 * [I] infoPtr : valid pointer to the listview structure
7147 * [I] nItem : item index
7148 * [I] uFlags : relationship flag
7151 * SUCCESS : item index
7154 static INT LISTVIEW_GetNextItem(const LISTVIEW_INFO *infoPtr, INT nItem, UINT uFlags)
7157 LVFINDINFOW lvFindInfo;
7158 INT nCountPerColumn;
7162 TRACE("nItem=%d, uFlags=%x, nItemCount=%d\n", nItem, uFlags, infoPtr->nItemCount);
7163 if (nItem < -1 || nItem >= infoPtr->nItemCount) return -1;
7165 ZeroMemory(&lvFindInfo, sizeof(lvFindInfo));
7167 if (uFlags & LVNI_CUT)
7170 if (uFlags & LVNI_DROPHILITED)
7171 uMask |= LVIS_DROPHILITED;
7173 if (uFlags & LVNI_FOCUSED)
7174 uMask |= LVIS_FOCUSED;
7176 if (uFlags & LVNI_SELECTED)
7177 uMask |= LVIS_SELECTED;
7179 /* if we're asked for the focused item, that's only one,
7180 * so it's worth optimizing */
7181 if (uFlags & LVNI_FOCUSED)
7183 if ((LISTVIEW_GetItemState(infoPtr, infoPtr->nFocusedItem, uMask) & uMask) != uMask) return -1;
7184 return (infoPtr->nFocusedItem == nItem) ? -1 : infoPtr->nFocusedItem;
7187 if (uFlags & LVNI_ABOVE)
7189 if ((infoPtr->uView == LV_VIEW_LIST) || (infoPtr->uView == LV_VIEW_DETAILS))
7194 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7200 /* Special case for autoarrange - move 'til the top of a list */
7201 if (is_autoarrange(infoPtr))
7203 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
7204 while (nItem - nCountPerRow >= 0)
7206 nItem -= nCountPerRow;
7207 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7212 lvFindInfo.flags = LVFI_NEARESTXY;
7213 lvFindInfo.vkDirection = VK_UP;
7214 LISTVIEW_GetItemPosition(infoPtr, nItem, &lvFindInfo.pt);
7215 while ((nItem = LISTVIEW_FindItemW(infoPtr, nItem, &lvFindInfo)) != -1)
7217 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7222 else if (uFlags & LVNI_BELOW)
7224 if ((infoPtr->uView == LV_VIEW_LIST) || (infoPtr->uView == LV_VIEW_DETAILS))
7226 while (nItem < infoPtr->nItemCount)
7229 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7235 /* Special case for autoarrange - move 'til the bottom of a list */
7236 if (is_autoarrange(infoPtr))
7238 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
7239 while (nItem + nCountPerRow < infoPtr->nItemCount )
7241 nItem += nCountPerRow;
7242 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7247 lvFindInfo.flags = LVFI_NEARESTXY;
7248 lvFindInfo.vkDirection = VK_DOWN;
7249 LISTVIEW_GetItemPosition(infoPtr, nItem, &lvFindInfo.pt);
7250 while ((nItem = LISTVIEW_FindItemW(infoPtr, nItem, &lvFindInfo)) != -1)
7252 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7257 else if (uFlags & LVNI_TOLEFT)
7259 if (infoPtr->uView == LV_VIEW_LIST)
7261 nCountPerColumn = LISTVIEW_GetCountPerColumn(infoPtr);
7262 while (nItem - nCountPerColumn >= 0)
7264 nItem -= nCountPerColumn;
7265 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7269 else if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
7271 /* Special case for autoarrange - move 'til the beginning of a row */
7272 if (is_autoarrange(infoPtr))
7274 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
7275 while (nItem % nCountPerRow > 0)
7278 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7283 lvFindInfo.flags = LVFI_NEARESTXY;
7284 lvFindInfo.vkDirection = VK_LEFT;
7285 LISTVIEW_GetItemPosition(infoPtr, nItem, &lvFindInfo.pt);
7286 while ((nItem = LISTVIEW_FindItemW(infoPtr, nItem, &lvFindInfo)) != -1)
7288 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7293 else if (uFlags & LVNI_TORIGHT)
7295 if (infoPtr->uView == LV_VIEW_LIST)
7297 nCountPerColumn = LISTVIEW_GetCountPerColumn(infoPtr);
7298 while (nItem + nCountPerColumn < infoPtr->nItemCount)
7300 nItem += nCountPerColumn;
7301 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7305 else if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
7307 /* Special case for autoarrange - move 'til the end of a row */
7308 if (is_autoarrange(infoPtr))
7310 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
7311 while (nItem % nCountPerRow < nCountPerRow - 1 )
7314 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7319 lvFindInfo.flags = LVFI_NEARESTXY;
7320 lvFindInfo.vkDirection = VK_RIGHT;
7321 LISTVIEW_GetItemPosition(infoPtr, nItem, &lvFindInfo.pt);
7322 while ((nItem = LISTVIEW_FindItemW(infoPtr, nItem, &lvFindInfo)) != -1)
7324 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7333 /* search by index */
7334 for (i = nItem; i < infoPtr->nItemCount; i++)
7336 if ((LISTVIEW_GetItemState(infoPtr, i, uMask) & uMask) == uMask)
7344 /* LISTVIEW_GetNumberOfWorkAreas */
7348 * Retrieves the origin coordinates when in icon or small icon display mode.
7351 * [I] infoPtr : valid pointer to the listview structure
7352 * [O] lpptOrigin : coordinate information
7357 static void LISTVIEW_GetOrigin(const LISTVIEW_INFO *infoPtr, LPPOINT lpptOrigin)
7359 INT nHorzPos = 0, nVertPos = 0;
7360 SCROLLINFO scrollInfo;
7362 scrollInfo.cbSize = sizeof(SCROLLINFO);
7363 scrollInfo.fMask = SIF_POS;
7365 if (GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo))
7366 nHorzPos = scrollInfo.nPos;
7367 if (GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
7368 nVertPos = scrollInfo.nPos;
7370 TRACE("nHorzPos=%d, nVertPos=%d\n", nHorzPos, nVertPos);
7372 lpptOrigin->x = infoPtr->rcList.left;
7373 lpptOrigin->y = infoPtr->rcList.top;
7374 if (infoPtr->uView == LV_VIEW_LIST)
7375 nHorzPos *= infoPtr->nItemWidth;
7376 else if (infoPtr->uView == LV_VIEW_DETAILS)
7377 nVertPos *= infoPtr->nItemHeight;
7379 lpptOrigin->x -= nHorzPos;
7380 lpptOrigin->y -= nVertPos;
7382 TRACE(" origin=%s\n", wine_dbgstr_point(lpptOrigin));
7387 * Retrieves the width of a string.
7390 * [I] hwnd : window handle
7391 * [I] lpszText : text string to process
7392 * [I] isW : TRUE if lpszText is Unicode, FALSE otherwise
7395 * SUCCESS : string width (in pixels)
7398 static INT LISTVIEW_GetStringWidthT(const LISTVIEW_INFO *infoPtr, LPCWSTR lpszText, BOOL isW)
7403 if (is_text(lpszText))
7405 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
7406 HDC hdc = GetDC(infoPtr->hwndSelf);
7407 HFONT hOldFont = SelectObject(hdc, hFont);
7410 GetTextExtentPointW(hdc, lpszText, lstrlenW(lpszText), &stringSize);
7412 GetTextExtentPointA(hdc, (LPCSTR)lpszText, lstrlenA((LPCSTR)lpszText), &stringSize);
7413 SelectObject(hdc, hOldFont);
7414 ReleaseDC(infoPtr->hwndSelf, hdc);
7416 return stringSize.cx;
7421 * Determines which listview item is located at the specified position.
7424 * [I] infoPtr : valid pointer to the listview structure
7425 * [IO] lpht : hit test information
7426 * [I] subitem : fill out iSubItem.
7427 * [I] select : return the index only if the hit selects the item
7430 * (mm 20001022): We must not allow iSubItem to be touched, for
7431 * an app might pass only a structure with space up to iItem!
7432 * (MS Office 97 does that for instance in the file open dialog)
7435 * SUCCESS : item index
7438 static INT LISTVIEW_HitTest(const LISTVIEW_INFO *infoPtr, LPLVHITTESTINFO lpht, BOOL subitem, BOOL select)
7440 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
7441 RECT rcBox, rcBounds, rcState, rcIcon, rcLabel, rcSearch;
7442 POINT Origin, Position, opt;
7447 TRACE("(pt=%s, subitem=%d, select=%d)\n", wine_dbgstr_point(&lpht->pt), subitem, select);
7451 if (subitem) lpht->iSubItem = 0;
7453 LISTVIEW_GetOrigin(infoPtr, &Origin);
7455 /* set whole list relation flags */
7456 if (subitem && infoPtr->uView == LV_VIEW_DETAILS)
7458 /* LVM_SUBITEMHITTEST checks left bound of possible client area */
7459 if (infoPtr->rcList.left > lpht->pt.x && Origin.x < lpht->pt.x)
7460 lpht->flags |= LVHT_TOLEFT;
7462 if (lpht->pt.y < infoPtr->rcList.top && lpht->pt.y >= 0)
7463 opt.y = lpht->pt.y + infoPtr->rcList.top;
7467 if (infoPtr->rcList.bottom < opt.y)
7468 lpht->flags |= LVHT_BELOW;
7472 if (infoPtr->rcList.left > lpht->pt.x)
7473 lpht->flags |= LVHT_TOLEFT;
7474 else if (infoPtr->rcList.right < lpht->pt.x)
7475 lpht->flags |= LVHT_TORIGHT;
7477 if (infoPtr->rcList.top > lpht->pt.y)
7478 lpht->flags |= LVHT_ABOVE;
7479 else if (infoPtr->rcList.bottom < lpht->pt.y)
7480 lpht->flags |= LVHT_BELOW;
7483 /* even if item is invalid try to find subitem */
7484 if (infoPtr->uView == LV_VIEW_DETAILS && subitem)
7489 opt.x = lpht->pt.x - Origin.x;
7491 lpht->iSubItem = -1;
7492 for (j = 0; j < DPA_GetPtrCount(infoPtr->hdpaColumns); j++)
7494 pRect = &LISTVIEW_GetColumnInfo(infoPtr, j)->rcHeader;
7496 if ((opt.x >= pRect->left) && (opt.x < pRect->right))
7502 TRACE("lpht->iSubItem=%d\n", lpht->iSubItem);
7504 /* if we're outside horizontal columns bounds there's nothing to test further */
7505 if (lpht->iSubItem == -1)
7508 lpht->flags = LVHT_NOWHERE;
7513 TRACE("lpht->flags=0x%x\n", lpht->flags);
7514 if (lpht->flags) return -1;
7516 lpht->flags |= LVHT_NOWHERE;
7518 /* first deal with the large items */
7519 rcSearch.left = lpht->pt.x;
7520 rcSearch.top = lpht->pt.y;
7521 rcSearch.right = rcSearch.left + 1;
7522 rcSearch.bottom = rcSearch.top + 1;
7524 iterator_frameditems(&i, infoPtr, &rcSearch);
7525 iterator_next(&i); /* go to first item in the sequence */
7527 iterator_destroy(&i);
7529 TRACE("lpht->iItem=%d\n", iItem);
7530 if (iItem == -1) return -1;
7532 lvItem.mask = LVIF_STATE | LVIF_TEXT;
7533 if (infoPtr->uView == LV_VIEW_DETAILS) lvItem.mask |= LVIF_INDENT;
7534 lvItem.stateMask = LVIS_STATEIMAGEMASK;
7535 if (infoPtr->uView == LV_VIEW_ICON) lvItem.stateMask |= LVIS_FOCUSED;
7536 lvItem.iItem = iItem;
7537 lvItem.iSubItem = subitem ? lpht->iSubItem : 0;
7538 lvItem.pszText = szDispText;
7539 lvItem.cchTextMax = DISP_TEXT_SIZE;
7540 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return -1;
7541 if (!infoPtr->bFocus) lvItem.state &= ~LVIS_FOCUSED;
7543 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, &rcBox, NULL, &rcIcon, &rcState, &rcLabel);
7544 LISTVIEW_GetItemOrigin(infoPtr, iItem, &Position);
7545 opt.x = lpht->pt.x - Position.x - Origin.x;
7547 if (lpht->pt.y < infoPtr->rcList.top && lpht->pt.y >= 0)
7548 opt.y = lpht->pt.y - Position.y - Origin.y + infoPtr->rcList.top;
7550 opt.y = lpht->pt.y - Position.y - Origin.y;
7552 if (infoPtr->uView == LV_VIEW_DETAILS)
7555 if (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT)
7556 opt.x = lpht->pt.x - Origin.x;
7560 UnionRect(&rcBounds, &rcIcon, &rcLabel);
7561 UnionRect(&rcBounds, &rcBounds, &rcState);
7563 TRACE("rcBounds=%s\n", wine_dbgstr_rect(&rcBounds));
7564 if (!PtInRect(&rcBounds, opt)) return -1;
7566 if (PtInRect(&rcIcon, opt))
7567 lpht->flags |= LVHT_ONITEMICON;
7568 else if (PtInRect(&rcLabel, opt))
7569 lpht->flags |= LVHT_ONITEMLABEL;
7570 else if (infoPtr->himlState && PtInRect(&rcState, opt))
7571 lpht->flags |= LVHT_ONITEMSTATEICON;
7572 /* special case for LVS_EX_FULLROWSELECT */
7573 if (infoPtr->uView == LV_VIEW_DETAILS && infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT &&
7574 !(lpht->flags & LVHT_ONITEM))
7576 lpht->flags = LVHT_ONITEM | LVHT_ABOVE;
7578 if (lpht->flags & LVHT_ONITEM)
7579 lpht->flags &= ~LVHT_NOWHERE;
7580 TRACE("lpht->flags=0x%x\n", lpht->flags);
7582 if (select && !(infoPtr->uView == LV_VIEW_DETAILS &&
7583 ((infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) ||
7584 (infoPtr->dwStyle & LVS_OWNERDRAWFIXED))))
7586 if (infoPtr->uView == LV_VIEW_DETAILS)
7588 /* get main item bounds */
7589 lvItem.iSubItem = 0;
7590 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, &rcBox, NULL, &rcIcon, &rcState, &rcLabel);
7591 UnionRect(&rcBounds, &rcIcon, &rcLabel);
7592 UnionRect(&rcBounds, &rcBounds, &rcState);
7594 if (!PtInRect(&rcBounds, opt)) iItem = -1;
7596 return lpht->iItem = iItem;
7601 * Inserts a new item in the listview control.
7604 * [I] infoPtr : valid pointer to the listview structure
7605 * [I] lpLVItem : item information
7606 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
7609 * SUCCESS : new item index
7612 static INT LISTVIEW_InsertItemT(LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem, BOOL isW)
7619 BOOL is_sorted, has_changed;
7621 HWND hwndSelf = infoPtr->hwndSelf;
7623 TRACE("(lpLVItem=%s, isW=%d)\n", debuglvitem_t(lpLVItem, isW), isW);
7625 if (infoPtr->dwStyle & LVS_OWNERDATA) return infoPtr->nItemCount++;
7627 /* make sure it's an item, and not a subitem; cannot insert a subitem */
7628 if (!lpLVItem || lpLVItem->iSubItem) return -1;
7630 if (!is_assignable_item(lpLVItem, infoPtr->dwStyle)) return -1;
7632 if (!(lpItem = Alloc(sizeof(ITEM_INFO)))) return -1;
7634 /* insert item in listview control data structure */
7635 if ( !(hdpaSubItems = DPA_Create(8)) ) goto fail;
7636 if ( !DPA_SetPtr(hdpaSubItems, 0, lpItem) ) assert (FALSE);
7638 /* link with id struct */
7639 if (!(lpID = Alloc(sizeof(ITEM_ID)))) goto fail;
7641 lpID->item = hdpaSubItems;
7642 lpID->id = get_next_itemid(infoPtr);
7643 if ( DPA_InsertPtr(infoPtr->hdpaItemIds, infoPtr->nItemCount, lpID) == -1) goto fail;
7645 is_sorted = (infoPtr->dwStyle & (LVS_SORTASCENDING | LVS_SORTDESCENDING)) &&
7646 !(infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (LPSTR_TEXTCALLBACKW != lpLVItem->pszText);
7648 if (lpLVItem->iItem < 0 && !is_sorted) return -1;
7650 /* calculate new item index */
7657 while (i < infoPtr->nItemCount)
7659 hItem = DPA_GetPtr( infoPtr->hdpaItems, i);
7660 item_s = DPA_GetPtr(hItem, 0);
7662 cmpv = textcmpWT(item_s->hdr.pszText, lpLVItem->pszText, isW);
7663 if (infoPtr->dwStyle & LVS_SORTDESCENDING) cmpv *= -1;
7665 if (cmpv >= 0) break;
7671 nItem = min(lpLVItem->iItem, infoPtr->nItemCount);
7673 TRACE(" inserting at %d, sorted=%d, count=%d, iItem=%d\n", nItem, is_sorted, infoPtr->nItemCount, lpLVItem->iItem);
7674 nItem = DPA_InsertPtr( infoPtr->hdpaItems, nItem, hdpaSubItems );
7675 if (nItem == -1) goto fail;
7676 infoPtr->nItemCount++;
7678 /* shift indices first so they don't get tangled */
7679 LISTVIEW_ShiftIndices(infoPtr, nItem, 1);
7681 /* set the item attributes */
7682 if (lpLVItem->mask & (LVIF_GROUPID|LVIF_COLUMNS))
7684 /* full size structure expected - _WIN32IE >= 0x560 */
7687 else if (lpLVItem->mask & LVIF_INDENT)
7689 /* indent member expected - _WIN32IE >= 0x300 */
7690 memcpy(&item, lpLVItem, offsetof( LVITEMW, iGroupId ));
7694 /* minimal structure expected */
7695 memcpy(&item, lpLVItem, offsetof( LVITEMW, iIndent ));
7698 if (infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES)
7700 item.mask |= LVIF_STATE;
7701 item.stateMask |= LVIS_STATEIMAGEMASK;
7702 item.state &= ~LVIS_STATEIMAGEMASK;
7703 item.state |= INDEXTOSTATEIMAGEMASK(1);
7705 if (!set_main_item(infoPtr, &item, TRUE, isW, &has_changed)) goto undo;
7707 /* make room for the position, if we are in the right mode */
7708 if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
7710 if (DPA_InsertPtr(infoPtr->hdpaPosX, nItem, 0) == -1)
7712 if (DPA_InsertPtr(infoPtr->hdpaPosY, nItem, 0) == -1)
7714 DPA_DeletePtr(infoPtr->hdpaPosX, nItem);
7719 /* send LVN_INSERTITEM notification */
7720 ZeroMemory(&nmlv, sizeof(NMLISTVIEW));
7722 nmlv.lParam = lpItem->lParam;
7723 notify_listview(infoPtr, LVN_INSERTITEM, &nmlv);
7724 if (!IsWindow(hwndSelf))
7727 /* align items (set position of each item) */
7728 if (infoPtr->uView == LV_VIEW_SMALLICON || infoPtr->uView == LV_VIEW_ICON)
7732 if (infoPtr->dwStyle & LVS_ALIGNLEFT)
7733 LISTVIEW_NextIconPosLeft(infoPtr, &pt);
7735 LISTVIEW_NextIconPosTop(infoPtr, &pt);
7737 LISTVIEW_MoveIconTo(infoPtr, nItem, &pt, TRUE);
7740 /* now is the invalidation fun */
7741 LISTVIEW_ScrollOnInsert(infoPtr, nItem, 1);
7745 LISTVIEW_ShiftIndices(infoPtr, nItem, -1);
7746 DPA_DeletePtr(infoPtr->hdpaItems, nItem);
7747 infoPtr->nItemCount--;
7749 DPA_DeletePtr(hdpaSubItems, 0);
7750 DPA_Destroy (hdpaSubItems);
7757 * Checks item visibility.
7760 * [I] infoPtr : valid pointer to the listview structure
7761 * [I] nFirst : item index to check for
7764 * Item visible : TRUE
7765 * Item invisible or failure : FALSE
7767 static BOOL LISTVIEW_IsItemVisible(const LISTVIEW_INFO *infoPtr, INT nItem)
7769 POINT Origin, Position;
7774 TRACE("nItem=%d\n", nItem);
7776 if (nItem < 0 || nItem >= DPA_GetPtrCount(infoPtr->hdpaItems)) return FALSE;
7778 LISTVIEW_GetOrigin(infoPtr, &Origin);
7779 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
7780 rcItem.left = Position.x + Origin.x;
7781 rcItem.top = Position.y + Origin.y;
7782 rcItem.right = rcItem.left + infoPtr->nItemWidth;
7783 rcItem.bottom = rcItem.top + infoPtr->nItemHeight;
7785 hdc = GetDC(infoPtr->hwndSelf);
7786 if (!hdc) return FALSE;
7787 ret = RectVisible(hdc, &rcItem);
7788 ReleaseDC(infoPtr->hwndSelf, hdc);
7795 * Redraws a range of items.
7798 * [I] infoPtr : valid pointer to the listview structure
7799 * [I] nFirst : first item
7800 * [I] nLast : last item
7806 static BOOL LISTVIEW_RedrawItems(const LISTVIEW_INFO *infoPtr, INT nFirst, INT nLast)
7810 if (nLast < nFirst || min(nFirst, nLast) < 0 ||
7811 max(nFirst, nLast) >= infoPtr->nItemCount)
7814 for (i = nFirst; i <= nLast; i++)
7815 LISTVIEW_InvalidateItem(infoPtr, i);
7822 * Scroll the content of a listview.
7825 * [I] infoPtr : valid pointer to the listview structure
7826 * [I] dx : horizontal scroll amount in pixels
7827 * [I] dy : vertical scroll amount in pixels
7834 * If the control is in report view (LV_VIEW_DETAILS) the control can
7835 * be scrolled only in line increments. "dy" will be rounded to the
7836 * nearest number of pixels that are a whole line. Ex: if line height
7837 * is 16 and an 8 is passed, the list will be scrolled by 16. If a 7
7838 * is passed, then the scroll will be 0. (per MSDN 7/2002)
7840 * For: (per experimentation with native control and CSpy ListView)
7841 * LV_VIEW_ICON scrolling in any direction is allowed
7842 * LV_VIEW_SMALLICON scrolling in any direction is allowed
7843 * LV_VIEW_LIST dx=1 = 1 column (horizontal only)
7844 * but will only scroll 1 column per message
7845 * no matter what the value.
7846 * dy must be 0 or FALSE returned.
7847 * LV_VIEW_DETAILS dx=1 = 1 pixel
7851 static BOOL LISTVIEW_Scroll(LISTVIEW_INFO *infoPtr, INT dx, INT dy)
7853 switch(infoPtr->uView) {
7854 case LV_VIEW_DETAILS:
7855 dy += (dy < 0 ? -1 : 1) * infoPtr->nItemHeight/2;
7856 dy /= infoPtr->nItemHeight;
7859 if (dy != 0) return FALSE;
7865 if (dx != 0) LISTVIEW_HScroll(infoPtr, SB_INTERNAL, dx);
7866 if (dy != 0) LISTVIEW_VScroll(infoPtr, SB_INTERNAL, dy);
7873 * Sets the background color.
7876 * [I] infoPtr : valid pointer to the listview structure
7877 * [I] clrBk : background color
7883 static BOOL LISTVIEW_SetBkColor(LISTVIEW_INFO *infoPtr, COLORREF clrBk)
7885 TRACE("(clrBk=%x)\n", clrBk);
7887 if(infoPtr->clrBk != clrBk) {
7888 if (infoPtr->clrBk != CLR_NONE) DeleteObject(infoPtr->hBkBrush);
7889 infoPtr->clrBk = clrBk;
7890 if (clrBk == CLR_NONE)
7891 infoPtr->hBkBrush = (HBRUSH)GetClassLongPtrW(infoPtr->hwndSelf, GCLP_HBRBACKGROUND);
7894 infoPtr->hBkBrush = CreateSolidBrush(clrBk);
7895 infoPtr->dwLvExStyle &= ~LVS_EX_TRANSPARENTBKGND;
7897 LISTVIEW_InvalidateList(infoPtr);
7903 /* LISTVIEW_SetBkImage */
7905 /*** Helper for {Insert,Set}ColumnT *only* */
7906 static void column_fill_hditem(const LISTVIEW_INFO *infoPtr, HDITEMW *lphdi, INT nColumn,
7907 const LVCOLUMNW *lpColumn, BOOL isW)
7909 if (lpColumn->mask & LVCF_FMT)
7911 /* format member is valid */
7912 lphdi->mask |= HDI_FORMAT;
7914 /* set text alignment (leftmost column must be left-aligned) */
7915 if (nColumn == 0 || (lpColumn->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_LEFT)
7916 lphdi->fmt |= HDF_LEFT;
7917 else if ((lpColumn->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_RIGHT)
7918 lphdi->fmt |= HDF_RIGHT;
7919 else if ((lpColumn->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_CENTER)
7920 lphdi->fmt |= HDF_CENTER;
7922 if (lpColumn->fmt & LVCFMT_BITMAP_ON_RIGHT)
7923 lphdi->fmt |= HDF_BITMAP_ON_RIGHT;
7925 if (lpColumn->fmt & LVCFMT_COL_HAS_IMAGES)
7927 lphdi->fmt |= HDF_IMAGE;
7928 lphdi->iImage = I_IMAGECALLBACK;
7931 if (lpColumn->fmt & LVCFMT_FIXED_WIDTH)
7932 lphdi->fmt |= HDF_FIXEDWIDTH;
7935 if (lpColumn->mask & LVCF_WIDTH)
7937 lphdi->mask |= HDI_WIDTH;
7938 if(lpColumn->cx == LVSCW_AUTOSIZE_USEHEADER)
7940 /* make it fill the remainder of the controls width */
7944 for(item_index = 0; item_index < (nColumn - 1); item_index++)
7946 LISTVIEW_GetHeaderRect(infoPtr, item_index, &rcHeader);
7947 lphdi->cxy += rcHeader.right - rcHeader.left;
7950 /* retrieve the layout of the header */
7951 GetClientRect(infoPtr->hwndSelf, &rcHeader);
7952 TRACE("start cxy=%d rcHeader=%s\n", lphdi->cxy, wine_dbgstr_rect(&rcHeader));
7954 lphdi->cxy = (rcHeader.right - rcHeader.left) - lphdi->cxy;
7957 lphdi->cxy = lpColumn->cx;
7960 if (lpColumn->mask & LVCF_TEXT)
7962 lphdi->mask |= HDI_TEXT | HDI_FORMAT;
7963 lphdi->fmt |= HDF_STRING;
7964 lphdi->pszText = lpColumn->pszText;
7965 lphdi->cchTextMax = textlenT(lpColumn->pszText, isW);
7968 if (lpColumn->mask & LVCF_IMAGE)
7970 lphdi->mask |= HDI_IMAGE;
7971 lphdi->iImage = lpColumn->iImage;
7974 if (lpColumn->mask & LVCF_ORDER)
7976 lphdi->mask |= HDI_ORDER;
7977 lphdi->iOrder = lpColumn->iOrder;
7984 * Inserts a new column.
7987 * [I] infoPtr : valid pointer to the listview structure
7988 * [I] nColumn : column index
7989 * [I] lpColumn : column information
7990 * [I] isW : TRUE if lpColumn is Unicode, FALSE otherwise
7993 * SUCCESS : new column index
7996 static INT LISTVIEW_InsertColumnT(LISTVIEW_INFO *infoPtr, INT nColumn,
7997 const LVCOLUMNW *lpColumn, BOOL isW)
7999 COLUMN_INFO *lpColumnInfo;
8003 TRACE("(nColumn=%d, lpColumn=%s, isW=%d)\n", nColumn, debuglvcolumn_t(lpColumn, isW), isW);
8005 if (!lpColumn || nColumn < 0) return -1;
8006 nColumn = min(nColumn, DPA_GetPtrCount(infoPtr->hdpaColumns));
8008 ZeroMemory(&hdi, sizeof(HDITEMW));
8009 column_fill_hditem(infoPtr, &hdi, nColumn, lpColumn, isW);
8012 * A mask not including LVCF_WIDTH turns into a mask of width, width 10
8013 * (can be seen in SPY) otherwise column never gets added.
8015 if (!(lpColumn->mask & LVCF_WIDTH)) {
8016 hdi.mask |= HDI_WIDTH;
8021 * when the iSubItem is available Windows copies it to the header lParam. It seems
8022 * to happen only in LVM_INSERTCOLUMN - not in LVM_SETCOLUMN
8024 if (lpColumn->mask & LVCF_SUBITEM)
8026 hdi.mask |= HDI_LPARAM;
8027 hdi.lParam = lpColumn->iSubItem;
8030 /* create header if not present */
8031 LISTVIEW_CreateHeader(infoPtr);
8032 if (!(LVS_NOCOLUMNHEADER & infoPtr->dwStyle) &&
8033 (infoPtr->uView == LV_VIEW_DETAILS) && (WS_VISIBLE & infoPtr->dwStyle))
8035 ShowWindow(infoPtr->hwndHeader, SW_SHOWNORMAL);
8038 /* insert item in header control */
8039 nNewColumn = SendMessageW(infoPtr->hwndHeader,
8040 isW ? HDM_INSERTITEMW : HDM_INSERTITEMA,
8041 nColumn, (LPARAM)&hdi);
8042 if (nNewColumn == -1) return -1;
8043 if (nNewColumn != nColumn) ERR("nColumn=%d, nNewColumn=%d\n", nColumn, nNewColumn);
8045 /* create our own column info */
8046 if (!(lpColumnInfo = Alloc(sizeof(COLUMN_INFO)))) goto fail;
8047 if (DPA_InsertPtr(infoPtr->hdpaColumns, nNewColumn, lpColumnInfo) == -1) goto fail;
8049 if (lpColumn->mask & LVCF_FMT) lpColumnInfo->fmt = lpColumn->fmt;
8050 if (lpColumn->mask & LVCF_MINWIDTH) lpColumnInfo->cxMin = lpColumn->cxMin;
8051 if (!SendMessageW(infoPtr->hwndHeader, HDM_GETITEMRECT, nNewColumn, (LPARAM)&lpColumnInfo->rcHeader))
8054 /* now we have to actually adjust the data */
8055 if (!(infoPtr->dwStyle & LVS_OWNERDATA) && infoPtr->nItemCount > 0)
8057 SUBITEM_INFO *lpSubItem;
8063 item.iSubItem = nNewColumn;
8064 item.mask = LVIF_TEXT | LVIF_IMAGE;
8065 item.iImage = I_IMAGECALLBACK;
8066 item.pszText = LPSTR_TEXTCALLBACKW;
8068 for (nItem = 0; nItem < infoPtr->nItemCount; nItem++)
8070 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, nItem);
8071 for (i = 1; i < DPA_GetPtrCount(hdpaSubItems); i++)
8073 lpSubItem = DPA_GetPtr(hdpaSubItems, i);
8074 if (lpSubItem->iSubItem >= nNewColumn)
8075 lpSubItem->iSubItem++;
8078 /* add new subitem for each item */
8080 set_sub_item(infoPtr, &item, isW, &changed);
8084 /* make space for the new column */
8085 LISTVIEW_ScrollColumns(infoPtr, nNewColumn + 1, lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left);
8086 LISTVIEW_UpdateItemSize(infoPtr);
8091 if (nNewColumn != -1) SendMessageW(infoPtr->hwndHeader, HDM_DELETEITEM, nNewColumn, 0);
8094 DPA_DeletePtr(infoPtr->hdpaColumns, nNewColumn);
8102 * Sets the attributes of a header item.
8105 * [I] infoPtr : valid pointer to the listview structure
8106 * [I] nColumn : column index
8107 * [I] lpColumn : column attributes
8108 * [I] isW: if TRUE, then lpColumn is a LPLVCOLUMNW, else it is a LPLVCOLUMNA
8114 static BOOL LISTVIEW_SetColumnT(const LISTVIEW_INFO *infoPtr, INT nColumn,
8115 const LVCOLUMNW *lpColumn, BOOL isW)
8117 HDITEMW hdi, hdiget;
8120 TRACE("(nColumn=%d, lpColumn=%s, isW=%d)\n", nColumn, debuglvcolumn_t(lpColumn, isW), isW);
8122 if (!lpColumn || nColumn < 0 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
8124 ZeroMemory(&hdi, sizeof(HDITEMW));
8125 if (lpColumn->mask & LVCF_FMT)
8127 hdi.mask |= HDI_FORMAT;
8128 hdiget.mask = HDI_FORMAT;
8129 if (SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nColumn, (LPARAM)&hdiget))
8130 hdi.fmt = hdiget.fmt & HDF_STRING;
8132 column_fill_hditem(infoPtr, &hdi, nColumn, lpColumn, isW);
8134 /* set header item attributes */
8135 bResult = SendMessageW(infoPtr->hwndHeader, isW ? HDM_SETITEMW : HDM_SETITEMA, nColumn, (LPARAM)&hdi);
8136 if (!bResult) return FALSE;
8138 if (lpColumn->mask & LVCF_FMT)
8140 COLUMN_INFO *lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, nColumn);
8141 INT oldFmt = lpColumnInfo->fmt;
8143 lpColumnInfo->fmt = lpColumn->fmt;
8144 if ((oldFmt ^ lpColumn->fmt) & (LVCFMT_JUSTIFYMASK | LVCFMT_IMAGE))
8146 if (infoPtr->uView == LV_VIEW_DETAILS) LISTVIEW_InvalidateColumn(infoPtr, nColumn);
8150 if (lpColumn->mask & LVCF_MINWIDTH)
8151 LISTVIEW_GetColumnInfo(infoPtr, nColumn)->cxMin = lpColumn->cxMin;
8158 * Sets the column order array
8161 * [I] infoPtr : valid pointer to the listview structure
8162 * [I] iCount : number of elements in column order array
8163 * [I] lpiArray : pointer to column order array
8169 static BOOL LISTVIEW_SetColumnOrderArray(LISTVIEW_INFO *infoPtr, INT iCount, const INT *lpiArray)
8171 TRACE("iCount %d lpiArray %p\n", iCount, lpiArray);
8173 if (!lpiArray || !IsWindow(infoPtr->hwndHeader)) return FALSE;
8175 infoPtr->colRectsDirty = TRUE;
8177 return SendMessageW(infoPtr->hwndHeader, HDM_SETORDERARRAY, iCount, (LPARAM)lpiArray);
8182 * Sets the width of a column
8185 * [I] infoPtr : valid pointer to the listview structure
8186 * [I] nColumn : column index
8187 * [I] cx : column width
8193 static BOOL LISTVIEW_SetColumnWidth(LISTVIEW_INFO *infoPtr, INT nColumn, INT cx)
8195 WCHAR szDispText[DISP_TEXT_SIZE] = { 0 };
8199 TRACE("(nColumn=%d, cx=%d\n", nColumn, cx);
8201 /* set column width only if in report or list mode */
8202 if (infoPtr->uView != LV_VIEW_DETAILS && infoPtr->uView != LV_VIEW_LIST) return FALSE;
8204 /* take care of invalid cx values */
8205 if(infoPtr->uView == LV_VIEW_DETAILS && cx < -2) cx = LVSCW_AUTOSIZE;
8206 else if (infoPtr->uView == LV_VIEW_LIST && cx < 1) return FALSE;
8208 /* resize all columns if in LV_VIEW_LIST mode */
8209 if(infoPtr->uView == LV_VIEW_LIST)
8211 infoPtr->nItemWidth = cx;
8212 LISTVIEW_InvalidateList(infoPtr);
8216 if (nColumn < 0 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
8218 if (cx == LVSCW_AUTOSIZE || (cx == LVSCW_AUTOSIZE_USEHEADER && nColumn < DPA_GetPtrCount(infoPtr->hdpaColumns) -1))
8223 lvItem.mask = LVIF_TEXT;
8225 lvItem.iSubItem = nColumn;
8226 lvItem.pszText = szDispText;
8227 lvItem.cchTextMax = DISP_TEXT_SIZE;
8228 for (; lvItem.iItem < infoPtr->nItemCount; lvItem.iItem++)
8230 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) continue;
8231 nLabelWidth = LISTVIEW_GetStringWidthT(infoPtr, lvItem.pszText, TRUE);
8232 if (max_cx < nLabelWidth) max_cx = nLabelWidth;
8234 if (infoPtr->himlSmall && (nColumn == 0 || (LISTVIEW_GetColumnInfo(infoPtr, nColumn)->fmt & LVCFMT_IMAGE)))
8235 max_cx += infoPtr->iconSize.cx;
8236 max_cx += TRAILING_LABEL_PADDING;
8239 /* autosize based on listview items width */
8240 if(cx == LVSCW_AUTOSIZE)
8242 else if(cx == LVSCW_AUTOSIZE_USEHEADER)
8244 /* if iCol is the last column make it fill the remainder of the controls width */
8245 if(nColumn == DPA_GetPtrCount(infoPtr->hdpaColumns) - 1)
8250 LISTVIEW_GetOrigin(infoPtr, &Origin);
8251 LISTVIEW_GetHeaderRect(infoPtr, nColumn, &rcHeader);
8253 cx = infoPtr->rcList.right - Origin.x - rcHeader.left;
8257 /* Despite what the MS docs say, if this is not the last
8258 column, then MS resizes the column to the width of the
8259 largest text string in the column, including headers
8260 and items. This is different from LVSCW_AUTOSIZE in that
8261 LVSCW_AUTOSIZE ignores the header string length. */
8264 /* retrieve header text */
8265 hdi.mask = HDI_TEXT;
8266 hdi.cchTextMax = DISP_TEXT_SIZE;
8267 hdi.pszText = szDispText;
8268 if (SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nColumn, (LPARAM)&hdi))
8270 HDC hdc = GetDC(infoPtr->hwndSelf);
8271 HFONT old_font = SelectObject(hdc, (HFONT)SendMessageW(infoPtr->hwndHeader, WM_GETFONT, 0, 0));
8274 if (GetTextExtentPoint32W(hdc, hdi.pszText, lstrlenW(hdi.pszText), &size))
8275 cx = size.cx + TRAILING_HEADER_PADDING;
8276 /* FIXME: Take into account the header image, if one is present */
8277 SelectObject(hdc, old_font);
8278 ReleaseDC(infoPtr->hwndSelf, hdc);
8280 cx = max (cx, max_cx);
8284 if (cx < 0) return FALSE;
8286 /* call header to update the column change */
8287 hdi.mask = HDI_WIDTH;
8288 hdi.cxy = max(cx, LISTVIEW_GetColumnInfo(infoPtr, nColumn)->cxMin);
8289 TRACE("hdi.cxy=%d\n", hdi.cxy);
8290 return SendMessageW(infoPtr->hwndHeader, HDM_SETITEMW, nColumn, (LPARAM)&hdi);
8294 * Creates the checkbox imagelist. Helper for LISTVIEW_SetExtendedListViewStyle
8297 static HIMAGELIST LISTVIEW_CreateCheckBoxIL(const LISTVIEW_INFO *infoPtr)
8300 HBITMAP hbm_im, hbm_mask, hbm_orig;
8302 HBRUSH hbr_white = GetStockObject(WHITE_BRUSH);
8303 HBRUSH hbr_black = GetStockObject(BLACK_BRUSH);
8306 himl = ImageList_Create(GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON),
8307 ILC_COLOR | ILC_MASK, 2, 2);
8308 hdc_wnd = GetDC(infoPtr->hwndSelf);
8309 hdc = CreateCompatibleDC(hdc_wnd);
8310 hbm_im = CreateCompatibleBitmap(hdc_wnd, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON));
8311 hbm_mask = CreateBitmap(GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), 1, 1, NULL);
8312 ReleaseDC(infoPtr->hwndSelf, hdc_wnd);
8314 rc.left = rc.top = 0;
8315 rc.right = GetSystemMetrics(SM_CXSMICON);
8316 rc.bottom = GetSystemMetrics(SM_CYSMICON);
8318 hbm_orig = SelectObject(hdc, hbm_mask);
8319 FillRect(hdc, &rc, hbr_white);
8320 InflateRect(&rc, -2, -2);
8321 FillRect(hdc, &rc, hbr_black);
8323 SelectObject(hdc, hbm_im);
8324 DrawFrameControl(hdc, &rc, DFC_BUTTON, DFCS_BUTTONCHECK | DFCS_MONO);
8325 SelectObject(hdc, hbm_orig);
8326 ImageList_Add(himl, hbm_im, hbm_mask);
8328 SelectObject(hdc, hbm_im);
8329 DrawFrameControl(hdc, &rc, DFC_BUTTON, DFCS_BUTTONCHECK | DFCS_MONO | DFCS_CHECKED);
8330 SelectObject(hdc, hbm_orig);
8331 ImageList_Add(himl, hbm_im, hbm_mask);
8333 DeleteObject(hbm_mask);
8334 DeleteObject(hbm_im);
8342 * Sets the extended listview style.
8345 * [I] infoPtr : valid pointer to the listview structure
8347 * [I] dwStyle : style
8350 * SUCCESS : previous style
8353 static DWORD LISTVIEW_SetExtendedListViewStyle(LISTVIEW_INFO *infoPtr, DWORD dwMask, DWORD dwExStyle)
8355 DWORD dwOldExStyle = infoPtr->dwLvExStyle;
8359 infoPtr->dwLvExStyle = (dwOldExStyle & ~dwMask) | (dwExStyle & dwMask);
8361 infoPtr->dwLvExStyle = dwExStyle;
8363 if((infoPtr->dwLvExStyle ^ dwOldExStyle) & LVS_EX_CHECKBOXES)
8365 HIMAGELIST himl = 0;
8366 if(infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES)
8369 item.mask = LVIF_STATE;
8370 item.stateMask = LVIS_STATEIMAGEMASK;
8371 item.state = INDEXTOSTATEIMAGEMASK(1);
8372 LISTVIEW_SetItemState(infoPtr, -1, &item);
8374 himl = LISTVIEW_CreateCheckBoxIL(infoPtr);
8375 if(!(infoPtr->dwStyle & LVS_SHAREIMAGELISTS))
8376 ImageList_Destroy(infoPtr->himlState);
8378 himl = LISTVIEW_SetImageList(infoPtr, LVSIL_STATE, himl);
8379 /* checkbox list replaces prevous custom list or... */
8380 if(((infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES) &&
8381 !(infoPtr->dwStyle & LVS_SHAREIMAGELISTS)) ||
8382 /* ...previous was checkbox list */
8383 (dwOldExStyle & LVS_EX_CHECKBOXES))
8384 ImageList_Destroy(himl);
8387 if((infoPtr->dwLvExStyle ^ dwOldExStyle) & LVS_EX_HEADERDRAGDROP)
8391 /* if not already created */
8392 LISTVIEW_CreateHeader(infoPtr);
8394 dwStyle = GetWindowLongW(infoPtr->hwndHeader, GWL_STYLE);
8395 if (infoPtr->dwLvExStyle & LVS_EX_HEADERDRAGDROP)
8396 dwStyle |= HDS_DRAGDROP;
8398 dwStyle &= ~HDS_DRAGDROP;
8399 SetWindowLongW(infoPtr->hwndHeader, GWL_STYLE, dwStyle);
8402 /* GRIDLINES adds decoration at top so changes sizes */
8403 if((infoPtr->dwLvExStyle ^ dwOldExStyle) & LVS_EX_GRIDLINES)
8405 LISTVIEW_UpdateSize(infoPtr);
8408 if((infoPtr->dwLvExStyle ^ dwOldExStyle) & LVS_EX_TRANSPARENTBKGND)
8410 if (infoPtr->dwLvExStyle & LVS_EX_TRANSPARENTBKGND)
8411 LISTVIEW_SetBkColor(infoPtr, CLR_NONE);
8414 if((infoPtr->dwLvExStyle ^ dwOldExStyle) & LVS_EX_HEADERINALLVIEWS)
8416 if (infoPtr->dwLvExStyle & LVS_EX_HEADERINALLVIEWS)
8417 LISTVIEW_CreateHeader(infoPtr);
8419 ShowWindow(infoPtr->hwndHeader, SW_HIDE);
8420 LISTVIEW_UpdateSize(infoPtr);
8421 LISTVIEW_UpdateScroll(infoPtr);
8424 LISTVIEW_InvalidateList(infoPtr);
8425 return dwOldExStyle;
8430 * Sets the new hot cursor used during hot tracking and hover selection.
8433 * [I] infoPtr : valid pointer to the listview structure
8434 * [I] hCursor : the new hot cursor handle
8437 * Returns the previous hot cursor
8439 static HCURSOR LISTVIEW_SetHotCursor(LISTVIEW_INFO *infoPtr, HCURSOR hCursor)
8441 HCURSOR oldCursor = infoPtr->hHotCursor;
8443 infoPtr->hHotCursor = hCursor;
8451 * Sets the hot item index.
8454 * [I] infoPtr : valid pointer to the listview structure
8455 * [I] iIndex : index
8458 * SUCCESS : previous hot item index
8459 * FAILURE : -1 (no hot item)
8461 static INT LISTVIEW_SetHotItem(LISTVIEW_INFO *infoPtr, INT iIndex)
8463 INT iOldIndex = infoPtr->nHotItem;
8465 infoPtr->nHotItem = iIndex;
8473 * Sets the amount of time the cursor must hover over an item before it is selected.
8476 * [I] infoPtr : valid pointer to the listview structure
8477 * [I] dwHoverTime : hover time, if -1 the hover time is set to the default
8480 * Returns the previous hover time
8482 static DWORD LISTVIEW_SetHoverTime(LISTVIEW_INFO *infoPtr, DWORD dwHoverTime)
8484 DWORD oldHoverTime = infoPtr->dwHoverTime;
8486 infoPtr->dwHoverTime = dwHoverTime;
8488 return oldHoverTime;
8493 * Sets spacing for icons of LVS_ICON style.
8496 * [I] infoPtr : valid pointer to the listview structure
8497 * [I] cx : horizontal spacing (-1 = system spacing, 0 = autosize)
8498 * [I] cy : vertical spacing (-1 = system spacing, 0 = autosize)
8501 * MAKELONG(oldcx, oldcy)
8503 static DWORD LISTVIEW_SetIconSpacing(LISTVIEW_INFO *infoPtr, INT cx, INT cy)
8505 DWORD oldspacing = MAKELONG(infoPtr->iconSpacing.cx, infoPtr->iconSpacing.cy);
8507 TRACE("requested=(%d,%d)\n", cx, cy);
8509 /* this is supported only for LVS_ICON style */
8510 if (infoPtr->uView != LV_VIEW_ICON) return oldspacing;
8512 /* set to defaults, if instructed to */
8513 if (cx == -1) cx = GetSystemMetrics(SM_CXICONSPACING);
8514 if (cy == -1) cy = GetSystemMetrics(SM_CYICONSPACING);
8516 /* if 0 then compute width
8517 * FIXME: Should scan each item and determine max width of
8518 * icon or label, then make that the width */
8520 cx = infoPtr->iconSpacing.cx;
8522 /* if 0 then compute height */
8524 cy = infoPtr->iconSize.cy + 2 * infoPtr->ntmHeight +
8525 ICON_BOTTOM_PADDING + ICON_TOP_PADDING + LABEL_VERT_PADDING;
8528 infoPtr->iconSpacing.cx = cx;
8529 infoPtr->iconSpacing.cy = cy;
8531 TRACE("old=(%d,%d), new=(%d,%d), iconSize=(%d,%d), ntmH=%d\n",
8532 LOWORD(oldspacing), HIWORD(oldspacing), cx, cy,
8533 infoPtr->iconSize.cx, infoPtr->iconSize.cy,
8534 infoPtr->ntmHeight);
8536 /* these depend on the iconSpacing */
8537 LISTVIEW_UpdateItemSize(infoPtr);
8542 static inline void set_icon_size(SIZE *size, HIMAGELIST himl, BOOL small)
8546 if (himl && ImageList_GetIconSize(himl, &cx, &cy))
8553 size->cx = GetSystemMetrics(small ? SM_CXSMICON : SM_CXICON);
8554 size->cy = GetSystemMetrics(small ? SM_CYSMICON : SM_CYICON);
8563 * [I] infoPtr : valid pointer to the listview structure
8564 * [I] nType : image list type
8565 * [I] himl : image list handle
8568 * SUCCESS : old image list
8571 static HIMAGELIST LISTVIEW_SetImageList(LISTVIEW_INFO *infoPtr, INT nType, HIMAGELIST himl)
8573 INT oldHeight = infoPtr->nItemHeight;
8574 HIMAGELIST himlOld = 0;
8576 TRACE("(nType=%d, himl=%p\n", nType, himl);
8581 himlOld = infoPtr->himlNormal;
8582 infoPtr->himlNormal = himl;
8583 if (infoPtr->uView == LV_VIEW_ICON) set_icon_size(&infoPtr->iconSize, himl, FALSE);
8584 LISTVIEW_SetIconSpacing(infoPtr, 0, 0);
8588 himlOld = infoPtr->himlSmall;
8589 infoPtr->himlSmall = himl;
8590 if (infoPtr->uView != LV_VIEW_ICON) set_icon_size(&infoPtr->iconSize, himl, TRUE);
8594 himlOld = infoPtr->himlState;
8595 infoPtr->himlState = himl;
8596 set_icon_size(&infoPtr->iconStateSize, himl, TRUE);
8597 ImageList_SetBkColor(infoPtr->himlState, CLR_NONE);
8601 ERR("Unknown icon type=%d\n", nType);
8605 infoPtr->nItemHeight = LISTVIEW_CalculateItemHeight(infoPtr);
8606 if (infoPtr->nItemHeight != oldHeight)
8607 LISTVIEW_UpdateScroll(infoPtr);
8614 * Preallocates memory (does *not* set the actual count of items !)
8617 * [I] infoPtr : valid pointer to the listview structure
8618 * [I] nItems : item count (projected number of items to allocate)
8619 * [I] dwFlags : update flags
8625 static BOOL LISTVIEW_SetItemCount(LISTVIEW_INFO *infoPtr, INT nItems, DWORD dwFlags)
8627 TRACE("(nItems=%d, dwFlags=%x)\n", nItems, dwFlags);
8629 if (infoPtr->dwStyle & LVS_OWNERDATA)
8631 INT nOldCount = infoPtr->nItemCount;
8633 if (nItems < nOldCount)
8635 RANGE range = { nItems, nOldCount };
8636 ranges_del(infoPtr->selectionRanges, range);
8637 if (infoPtr->nFocusedItem >= nItems)
8639 LISTVIEW_SetItemFocus(infoPtr, -1);
8640 SetRectEmpty(&infoPtr->rcFocus);
8644 infoPtr->nItemCount = nItems;
8645 LISTVIEW_UpdateScroll(infoPtr);
8647 /* the flags are valid only in ownerdata report and list modes */
8648 if (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON) dwFlags = 0;
8650 if (!(dwFlags & LVSICF_NOSCROLL) && infoPtr->nFocusedItem != -1)
8651 LISTVIEW_EnsureVisible(infoPtr, infoPtr->nFocusedItem, FALSE);
8653 if (!(dwFlags & LVSICF_NOINVALIDATEALL))
8654 LISTVIEW_InvalidateList(infoPtr);
8661 LISTVIEW_GetOrigin(infoPtr, &Origin);
8662 nFrom = min(nOldCount, nItems);
8663 nTo = max(nOldCount, nItems);
8665 if (infoPtr->uView == LV_VIEW_DETAILS)
8668 rcErase.top = nFrom * infoPtr->nItemHeight;
8669 rcErase.right = infoPtr->nItemWidth;
8670 rcErase.bottom = nTo * infoPtr->nItemHeight;
8671 OffsetRect(&rcErase, Origin.x, Origin.y);
8672 if (IntersectRect(&rcErase, &rcErase, &infoPtr->rcList))
8673 LISTVIEW_InvalidateRect(infoPtr, &rcErase);
8675 else /* LV_VIEW_LIST */
8677 INT nPerCol = LISTVIEW_GetCountPerColumn(infoPtr);
8679 rcErase.left = (nFrom / nPerCol) * infoPtr->nItemWidth;
8680 rcErase.top = (nFrom % nPerCol) * infoPtr->nItemHeight;
8681 rcErase.right = rcErase.left + infoPtr->nItemWidth;
8682 rcErase.bottom = nPerCol * infoPtr->nItemHeight;
8683 OffsetRect(&rcErase, Origin.x, Origin.y);
8684 if (IntersectRect(&rcErase, &rcErase, &infoPtr->rcList))
8685 LISTVIEW_InvalidateRect(infoPtr, &rcErase);
8687 rcErase.left = (nFrom / nPerCol + 1) * infoPtr->nItemWidth;
8689 rcErase.right = (nTo / nPerCol + 1) * infoPtr->nItemWidth;
8690 rcErase.bottom = nPerCol * infoPtr->nItemHeight;
8691 OffsetRect(&rcErase, Origin.x, Origin.y);
8692 if (IntersectRect(&rcErase, &rcErase, &infoPtr->rcList))
8693 LISTVIEW_InvalidateRect(infoPtr, &rcErase);
8699 /* According to MSDN for non-LVS_OWNERDATA this is just
8700 * a performance issue. The control allocates its internal
8701 * data structures for the number of items specified. It
8702 * cuts down on the number of memory allocations. Therefore
8703 * we will just issue a WARN here
8705 WARN("for non-ownerdata performance option not implemented.\n");
8713 * Sets the position of an item.
8716 * [I] infoPtr : valid pointer to the listview structure
8717 * [I] nItem : item index
8718 * [I] pt : coordinate
8724 static BOOL LISTVIEW_SetItemPosition(LISTVIEW_INFO *infoPtr, INT nItem, const POINT *pt)
8728 TRACE("(nItem=%d, pt=%s\n", nItem, wine_dbgstr_point(pt));
8730 if (!pt || nItem < 0 || nItem >= infoPtr->nItemCount ||
8731 !(infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON)) return FALSE;
8734 LISTVIEW_GetOrigin(infoPtr, &Origin);
8736 /* This point value seems to be an undocumented feature.
8737 * The best guess is that it means either at the origin,
8738 * or at true beginning of the list. I will assume the origin. */
8739 if ((Pt.x == -1) && (Pt.y == -1))
8742 if (infoPtr->uView == LV_VIEW_ICON)
8744 Pt.x -= (infoPtr->nItemWidth - infoPtr->iconSize.cx) / 2;
8745 Pt.y -= ICON_TOP_PADDING;
8750 infoPtr->bAutoarrange = FALSE;
8752 return LISTVIEW_MoveIconTo(infoPtr, nItem, &Pt, FALSE);
8757 * Sets the state of one or many items.
8760 * [I] infoPtr : valid pointer to the listview structure
8761 * [I] nItem : item index
8762 * [I] lpLVItem : item or subitem info
8768 static BOOL LISTVIEW_SetItemState(LISTVIEW_INFO *infoPtr, INT nItem, const LVITEMW *lpLVItem)
8770 BOOL bResult = TRUE;
8773 lvItem.iItem = nItem;
8774 lvItem.iSubItem = 0;
8775 lvItem.mask = LVIF_STATE;
8776 lvItem.state = lpLVItem->state;
8777 lvItem.stateMask = lpLVItem->stateMask;
8778 TRACE("lvItem=%s\n", debuglvitem_t(&lvItem, TRUE));
8782 /* select all isn't allowed in LVS_SINGLESEL */
8783 if ((lvItem.state & lvItem.stateMask & LVIS_SELECTED) && (infoPtr->dwStyle & LVS_SINGLESEL))
8786 /* focus all isn't allowed */
8787 if (lvItem.state & lvItem.stateMask & LVIS_FOCUSED) return FALSE;
8789 /* apply to all items */
8790 for (lvItem.iItem = 0; lvItem.iItem < infoPtr->nItemCount; lvItem.iItem++)
8791 if (!LISTVIEW_SetItemT(infoPtr, &lvItem, TRUE)) bResult = FALSE;
8794 bResult = LISTVIEW_SetItemT(infoPtr, &lvItem, TRUE);
8801 * Sets the text of an item or subitem.
8804 * [I] hwnd : window handle
8805 * [I] nItem : item index
8806 * [I] lpLVItem : item or subitem info
8807 * [I] isW : TRUE if input is Unicode
8813 static BOOL LISTVIEW_SetItemTextT(LISTVIEW_INFO *infoPtr, INT nItem, const LVITEMW *lpLVItem, BOOL isW)
8817 if (!lpLVItem || nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
8819 lvItem.iItem = nItem;
8820 lvItem.iSubItem = lpLVItem->iSubItem;
8821 lvItem.mask = LVIF_TEXT;
8822 lvItem.pszText = lpLVItem->pszText;
8823 lvItem.cchTextMax = lpLVItem->cchTextMax;
8825 TRACE("(nItem=%d, lpLVItem=%s, isW=%d)\n", nItem, debuglvitem_t(&lvItem, isW), isW);
8827 return LISTVIEW_SetItemT(infoPtr, &lvItem, isW);
8832 * Set item index that marks the start of a multiple selection.
8835 * [I] infoPtr : valid pointer to the listview structure
8836 * [I] nIndex : index
8839 * Index number or -1 if there is no selection mark.
8841 static INT LISTVIEW_SetSelectionMark(LISTVIEW_INFO *infoPtr, INT nIndex)
8843 INT nOldIndex = infoPtr->nSelectionMark;
8845 TRACE("(nIndex=%d)\n", nIndex);
8847 infoPtr->nSelectionMark = nIndex;
8854 * Sets the text background color.
8857 * [I] infoPtr : valid pointer to the listview structure
8858 * [I] clrTextBk : text background color
8864 static BOOL LISTVIEW_SetTextBkColor(LISTVIEW_INFO *infoPtr, COLORREF clrTextBk)
8866 TRACE("(clrTextBk=%x)\n", clrTextBk);
8868 if (infoPtr->clrTextBk != clrTextBk)
8870 infoPtr->clrTextBk = clrTextBk;
8871 LISTVIEW_InvalidateList(infoPtr);
8879 * Sets the text foreground color.
8882 * [I] infoPtr : valid pointer to the listview structure
8883 * [I] clrText : text color
8889 static BOOL LISTVIEW_SetTextColor (LISTVIEW_INFO *infoPtr, COLORREF clrText)
8891 TRACE("(clrText=%x)\n", clrText);
8893 if (infoPtr->clrText != clrText)
8895 infoPtr->clrText = clrText;
8896 LISTVIEW_InvalidateList(infoPtr);
8904 * Sets new ToolTip window to ListView control.
8907 * [I] infoPtr : valid pointer to the listview structure
8908 * [I] hwndNewToolTip : handle to new ToolTip
8913 static HWND LISTVIEW_SetToolTips( LISTVIEW_INFO *infoPtr, HWND hwndNewToolTip)
8915 HWND hwndOldToolTip = infoPtr->hwndToolTip;
8916 infoPtr->hwndToolTip = hwndNewToolTip;
8917 return hwndOldToolTip;
8922 * sets the Unicode character format flag for the control
8924 * [I] infoPtr :valid pointer to the listview structure
8925 * [I] fUnicode :true to switch to UNICODE false to switch to ANSI
8928 * Old Unicode Format
8930 static BOOL LISTVIEW_SetUnicodeFormat( LISTVIEW_INFO *infoPtr, BOOL unicode)
8932 SHORT rc = infoPtr->notifyFormat;
8933 infoPtr->notifyFormat = (unicode) ? NFR_UNICODE : NFR_ANSI;
8934 return rc == NFR_UNICODE;
8939 * sets the control view mode
8941 * [I] infoPtr :valid pointer to the listview structure
8942 * [I] nView :new view mode value
8948 static INT LISTVIEW_SetView(LISTVIEW_INFO *infoPtr, DWORD nView)
8950 SIZE oldIconSize = infoPtr->iconSize;
8953 if (infoPtr->uView == nView) return 1;
8955 if ((INT)nView < 0 || nView > LV_VIEW_MAX) return -1;
8956 if (nView == LV_VIEW_TILE)
8958 FIXME("View LV_VIEW_TILE unimplemented\n");
8962 infoPtr->uView = nView;
8964 SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
8965 ShowWindow(infoPtr->hwndHeader, SW_HIDE);
8967 ShowScrollBar(infoPtr->hwndSelf, SB_BOTH, FALSE);
8968 SetRectEmpty(&infoPtr->rcFocus);
8970 himl = (nView == LV_VIEW_ICON ? infoPtr->himlNormal : infoPtr->himlSmall);
8971 set_icon_size(&infoPtr->iconSize, himl, nView != LV_VIEW_ICON);
8976 if ((infoPtr->iconSize.cx != oldIconSize.cx) || (infoPtr->iconSize.cy != oldIconSize.cy))
8978 TRACE("icon old size=(%d,%d), new size=(%d,%d)\n",
8979 oldIconSize.cx, oldIconSize.cy, infoPtr->iconSize.cx, infoPtr->iconSize.cy);
8980 LISTVIEW_SetIconSpacing(infoPtr, 0, 0);
8982 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
8984 case LV_VIEW_SMALLICON:
8985 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
8987 case LV_VIEW_DETAILS:
8992 LISTVIEW_CreateHeader( infoPtr );
8994 hl.prc = &infoPtr->rcList;
8996 SendMessageW(infoPtr->hwndHeader, HDM_LAYOUT, 0, (LPARAM)&hl);
8997 SetWindowPos(infoPtr->hwndHeader, infoPtr->hwndSelf, wp.x, wp.y, wp.cx, wp.cy,
8998 wp.flags | ((infoPtr->dwStyle & LVS_NOCOLUMNHEADER) ? SWP_HIDEWINDOW : SWP_SHOWWINDOW));
9005 LISTVIEW_UpdateItemSize(infoPtr);
9006 LISTVIEW_UpdateSize(infoPtr);
9007 LISTVIEW_UpdateScroll(infoPtr);
9008 LISTVIEW_InvalidateList(infoPtr);
9010 TRACE("nView=%d\n", nView);
9015 /* LISTVIEW_SetWorkAreas */
9019 * Callback internally used by LISTVIEW_SortItems() in response of LVM_SORTITEMS
9022 * [I] first : pointer to first ITEM_INFO to compare
9023 * [I] second : pointer to second ITEM_INFO to compare
9024 * [I] lParam : HWND of control
9027 * if first comes before second : negative
9028 * if first comes after second : positive
9029 * if first and second are equivalent : zero
9031 static INT WINAPI LISTVIEW_CallBackCompare(LPVOID first, LPVOID second, LPARAM lParam)
9033 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)lParam;
9034 ITEM_INFO* lv_first = DPA_GetPtr( first, 0 );
9035 ITEM_INFO* lv_second = DPA_GetPtr( second, 0 );
9037 /* Forward the call to the client defined callback */
9038 return (infoPtr->pfnCompare)( lv_first->lParam , lv_second->lParam, infoPtr->lParamSort );
9043 * Callback internally used by LISTVIEW_SortItems() in response of LVM_SORTITEMSEX
9046 * [I] first : pointer to first ITEM_INFO to compare
9047 * [I] second : pointer to second ITEM_INFO to compare
9048 * [I] lParam : HWND of control
9051 * if first comes before second : negative
9052 * if first comes after second : positive
9053 * if first and second are equivalent : zero
9055 static INT WINAPI LISTVIEW_CallBackCompareEx(LPVOID first, LPVOID second, LPARAM lParam)
9057 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)lParam;
9058 INT first_idx = DPA_GetPtrIndex( infoPtr->hdpaItems, first );
9059 INT second_idx = DPA_GetPtrIndex( infoPtr->hdpaItems, second );
9061 /* Forward the call to the client defined callback */
9062 return (infoPtr->pfnCompare)( first_idx, second_idx, infoPtr->lParamSort );
9067 * Sorts the listview items.
9070 * [I] infoPtr : valid pointer to the listview structure
9071 * [I] pfnCompare : application-defined value
9072 * [I] lParamSort : pointer to comparison callback
9073 * [I] IsEx : TRUE when LVM_SORTITEMSEX used
9079 static BOOL LISTVIEW_SortItems(LISTVIEW_INFO *infoPtr, PFNLVCOMPARE pfnCompare,
9080 LPARAM lParamSort, BOOL IsEx)
9084 LPVOID selectionMarkItem = NULL;
9085 LPVOID focusedItem = NULL;
9088 TRACE("(pfnCompare=%p, lParamSort=%lx)\n", pfnCompare, lParamSort);
9090 if (infoPtr->dwStyle & LVS_OWNERDATA) return FALSE;
9092 if (!pfnCompare) return FALSE;
9093 if (!infoPtr->hdpaItems) return FALSE;
9095 /* if there are 0 or 1 items, there is no need to sort */
9096 if (infoPtr->nItemCount < 2) return TRUE;
9098 /* clear selection */
9099 ranges_clear(infoPtr->selectionRanges);
9101 /* save selection mark and focused item */
9102 if (infoPtr->nSelectionMark >= 0)
9103 selectionMarkItem = DPA_GetPtr(infoPtr->hdpaItems, infoPtr->nSelectionMark);
9104 if (infoPtr->nFocusedItem >= 0)
9105 focusedItem = DPA_GetPtr(infoPtr->hdpaItems, infoPtr->nFocusedItem);
9107 infoPtr->pfnCompare = pfnCompare;
9108 infoPtr->lParamSort = lParamSort;
9110 DPA_Sort(infoPtr->hdpaItems, LISTVIEW_CallBackCompareEx, (LPARAM)infoPtr);
9112 DPA_Sort(infoPtr->hdpaItems, LISTVIEW_CallBackCompare, (LPARAM)infoPtr);
9114 /* restore selection ranges */
9115 for (i=0; i < infoPtr->nItemCount; i++)
9117 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, i);
9118 lpItem = DPA_GetPtr(hdpaSubItems, 0);
9120 if (lpItem->state & LVIS_SELECTED)
9121 ranges_additem(infoPtr->selectionRanges, i);
9123 /* restore selection mark and focused item */
9124 infoPtr->nSelectionMark = DPA_GetPtrIndex(infoPtr->hdpaItems, selectionMarkItem);
9125 infoPtr->nFocusedItem = DPA_GetPtrIndex(infoPtr->hdpaItems, focusedItem);
9127 /* I believe nHotItem should be left alone, see LISTVIEW_ShiftIndices */
9129 /* refresh the display */
9130 if (infoPtr->uView != LV_VIEW_ICON && infoPtr->uView != LV_VIEW_SMALLICON)
9131 LISTVIEW_InvalidateList(infoPtr);
9138 * Update theme handle after a theme change.
9141 * [I] infoPtr : valid pointer to the listview structure
9145 * FAILURE : something else
9147 static LRESULT LISTVIEW_ThemeChanged(const LISTVIEW_INFO *infoPtr)
9149 HTHEME theme = GetWindowTheme(infoPtr->hwndSelf);
9150 CloseThemeData(theme);
9151 OpenThemeData(infoPtr->hwndSelf, themeClass);
9157 * Updates an items or rearranges the listview control.
9160 * [I] infoPtr : valid pointer to the listview structure
9161 * [I] nItem : item index
9167 static BOOL LISTVIEW_Update(LISTVIEW_INFO *infoPtr, INT nItem)
9169 TRACE("(nItem=%d)\n", nItem);
9171 if (nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
9173 /* rearrange with default alignment style */
9174 if (is_autoarrange(infoPtr))
9175 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
9177 LISTVIEW_InvalidateItem(infoPtr, nItem);
9184 * Draw the track line at the place defined in the infoPtr structure.
9185 * The line is drawn with a XOR pen so drawing the line for the second time
9186 * in the same place erases the line.
9189 * [I] infoPtr : valid pointer to the listview structure
9195 static BOOL LISTVIEW_DrawTrackLine(const LISTVIEW_INFO *infoPtr)
9201 if (infoPtr->xTrackLine == -1)
9204 if (!(hdc = GetDC(infoPtr->hwndSelf)))
9206 hOldPen = SelectObject(hdc, GetStockObject(BLACK_PEN));
9207 oldROP = SetROP2(hdc, R2_XORPEN);
9208 MoveToEx(hdc, infoPtr->xTrackLine, infoPtr->rcList.top, NULL);
9209 LineTo(hdc, infoPtr->xTrackLine, infoPtr->rcList.bottom);
9210 SetROP2(hdc, oldROP);
9211 SelectObject(hdc, hOldPen);
9212 ReleaseDC(infoPtr->hwndSelf, hdc);
9218 * Called when an edit control should be displayed. This function is called after
9219 * we are sure that there was a single click - not a double click (this is a TIMERPROC).
9222 * [I] hwnd : Handle to the listview
9223 * [I] uMsg : WM_TIMER (ignored)
9224 * [I] idEvent : The timer ID interpreted as a pointer to a DELAYED_EDIT_ITEM struct
9225 * [I] dwTimer : The elapsed time (ignored)
9230 static VOID CALLBACK LISTVIEW_DelayedEditItem(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
9232 DELAYED_ITEM_EDIT *editItem = (DELAYED_ITEM_EDIT *)idEvent;
9233 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongPtrW(hwnd, 0);
9235 KillTimer(hwnd, idEvent);
9236 editItem->fEnabled = FALSE;
9237 /* check if the item is still selected */
9238 if (infoPtr->bFocus && LISTVIEW_GetItemState(infoPtr, editItem->iItem, LVIS_SELECTED))
9239 LISTVIEW_EditLabelT(infoPtr, editItem->iItem, TRUE);
9244 * Creates the listview control - the WM_NCCREATE phase.
9247 * [I] hwnd : window handle
9248 * [I] lpcs : the create parameters
9254 static LRESULT LISTVIEW_NCCreate(HWND hwnd, const CREATESTRUCTW *lpcs)
9256 LISTVIEW_INFO *infoPtr;
9259 TRACE("(lpcs=%p)\n", lpcs);
9261 /* initialize info pointer */
9262 infoPtr = Alloc(sizeof(LISTVIEW_INFO));
9263 if (!infoPtr) return FALSE;
9265 SetWindowLongPtrW(hwnd, 0, (DWORD_PTR)infoPtr);
9267 infoPtr->hwndSelf = hwnd;
9268 infoPtr->dwStyle = lpcs->style; /* Note: may be changed in WM_CREATE */
9269 map_style_view(infoPtr);
9270 /* determine the type of structures to use */
9271 infoPtr->hwndNotify = lpcs->hwndParent;
9272 /* infoPtr->notifyFormat will be filled in WM_CREATE */
9274 /* initialize color information */
9275 infoPtr->clrBk = CLR_NONE;
9276 infoPtr->clrText = CLR_DEFAULT;
9277 infoPtr->clrTextBk = CLR_DEFAULT;
9278 LISTVIEW_SetBkColor(infoPtr, comctl32_color.clrWindow);
9280 /* set default values */
9281 infoPtr->nFocusedItem = -1;
9282 infoPtr->nSelectionMark = -1;
9283 infoPtr->nHotItem = -1;
9284 infoPtr->bRedraw = TRUE;
9285 infoPtr->bNoItemMetrics = TRUE;
9286 infoPtr->bDoChangeNotify = TRUE;
9287 infoPtr->iconSpacing.cx = GetSystemMetrics(SM_CXICONSPACING);
9288 infoPtr->iconSpacing.cy = GetSystemMetrics(SM_CYICONSPACING);
9289 infoPtr->nEditLabelItem = -1;
9290 infoPtr->nLButtonDownItem = -1;
9291 infoPtr->dwHoverTime = HOVER_DEFAULT; /* default system hover time */
9292 infoPtr->nMeasureItemHeight = 0;
9293 infoPtr->xTrackLine = -1; /* no track line */
9294 infoPtr->itemEdit.fEnabled = FALSE;
9295 infoPtr->iVersion = COMCTL32_VERSION;
9296 infoPtr->colRectsDirty = FALSE;
9298 /* get default font (icon title) */
9299 SystemParametersInfoW(SPI_GETICONTITLELOGFONT, 0, &logFont, 0);
9300 infoPtr->hDefaultFont = CreateFontIndirectW(&logFont);
9301 infoPtr->hFont = infoPtr->hDefaultFont;
9302 LISTVIEW_SaveTextMetrics(infoPtr);
9304 /* allocate memory for the data structure */
9305 if (!(infoPtr->selectionRanges = ranges_create(10))) goto fail;
9306 if (!(infoPtr->hdpaItems = DPA_Create(10))) goto fail;
9307 if (!(infoPtr->hdpaItemIds = DPA_Create(10))) goto fail;
9308 if (!(infoPtr->hdpaPosX = DPA_Create(10))) goto fail;
9309 if (!(infoPtr->hdpaPosY = DPA_Create(10))) goto fail;
9310 if (!(infoPtr->hdpaColumns = DPA_Create(10))) goto fail;
9314 DestroyWindow(infoPtr->hwndHeader);
9315 ranges_destroy(infoPtr->selectionRanges);
9316 DPA_Destroy(infoPtr->hdpaItems);
9317 DPA_Destroy(infoPtr->hdpaItemIds);
9318 DPA_Destroy(infoPtr->hdpaPosX);
9319 DPA_Destroy(infoPtr->hdpaPosY);
9320 DPA_Destroy(infoPtr->hdpaColumns);
9327 * Creates the listview control - the WM_CREATE phase. Most of the data is
9328 * already set up in LISTVIEW_NCCreate
9331 * [I] hwnd : window handle
9332 * [I] lpcs : the create parameters
9338 static LRESULT LISTVIEW_Create(HWND hwnd, const CREATESTRUCTW *lpcs)
9340 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongPtrW(hwnd, 0);
9342 TRACE("(lpcs=%p)\n", lpcs);
9344 infoPtr->dwStyle = lpcs->style;
9345 map_style_view(infoPtr);
9347 infoPtr->notifyFormat = SendMessageW(infoPtr->hwndNotify, WM_NOTIFYFORMAT,
9348 (WPARAM)infoPtr->hwndSelf, NF_QUERY);
9349 /* on error defaulting to ANSI notifications */
9350 if (infoPtr->notifyFormat == 0) infoPtr->notifyFormat = NFR_ANSI;
9351 TRACE("notify format=%d\n", infoPtr->notifyFormat);
9353 if ((infoPtr->uView == LV_VIEW_DETAILS) && (lpcs->style & WS_VISIBLE))
9355 if (LISTVIEW_CreateHeader(infoPtr) < 0) return -1;
9358 infoPtr->hwndHeader = 0;
9360 /* init item size to avoid division by 0 */
9361 LISTVIEW_UpdateItemSize (infoPtr);
9363 if (infoPtr->uView == LV_VIEW_DETAILS)
9365 if (!(LVS_NOCOLUMNHEADER & lpcs->style) && (WS_VISIBLE & lpcs->style))
9367 ShowWindow(infoPtr->hwndHeader, SW_SHOWNORMAL);
9369 LISTVIEW_UpdateScroll(infoPtr);
9370 /* send WM_MEASUREITEM notification */
9371 if (infoPtr->dwStyle & LVS_OWNERDRAWFIXED) notify_measureitem(infoPtr);
9374 OpenThemeData(hwnd, themeClass);
9376 /* initialize the icon sizes */
9377 set_icon_size(&infoPtr->iconSize, infoPtr->himlNormal, infoPtr->uView != LV_VIEW_ICON);
9378 set_icon_size(&infoPtr->iconStateSize, infoPtr->himlState, TRUE);
9384 * Destroys the listview control.
9387 * [I] infoPtr : valid pointer to the listview structure
9393 static LRESULT LISTVIEW_Destroy(LISTVIEW_INFO *infoPtr)
9395 HTHEME theme = GetWindowTheme(infoPtr->hwndSelf);
9396 CloseThemeData(theme);
9398 /* delete all items */
9399 LISTVIEW_DeleteAllItems(infoPtr, TRUE);
9406 * Enables the listview control.
9409 * [I] infoPtr : valid pointer to the listview structure
9410 * [I] bEnable : specifies whether to enable or disable the window
9416 static BOOL LISTVIEW_Enable(const LISTVIEW_INFO *infoPtr)
9418 if (infoPtr->dwStyle & LVS_OWNERDRAWFIXED)
9419 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
9425 * Erases the background of the listview control.
9428 * [I] infoPtr : valid pointer to the listview structure
9429 * [I] hdc : device context handle
9435 static inline BOOL LISTVIEW_EraseBkgnd(const LISTVIEW_INFO *infoPtr, HDC hdc)
9439 TRACE("(hdc=%p)\n", hdc);
9441 if (!GetClipBox(hdc, &rc)) return FALSE;
9443 if (infoPtr->clrBk == CLR_NONE)
9445 if (infoPtr->dwLvExStyle & LVS_EX_TRANSPARENTBKGND)
9446 return SendMessageW(infoPtr->hwndNotify, WM_PRINTCLIENT,
9447 (WPARAM)hdc, PRF_ERASEBKGND);
9449 return SendMessageW(infoPtr->hwndNotify, WM_ERASEBKGND, (WPARAM)hdc, 0);
9452 /* for double buffered controls we need to do this during refresh */
9453 if (infoPtr->dwLvExStyle & LVS_EX_DOUBLEBUFFER) return FALSE;
9455 return LISTVIEW_FillBkgnd(infoPtr, hdc, &rc);
9461 * Helper function for LISTVIEW_[HV]Scroll *only*.
9462 * Performs vertical/horizontal scrolling by a give amount.
9465 * [I] infoPtr : valid pointer to the listview structure
9466 * [I] dx : amount of horizontal scroll
9467 * [I] dy : amount of vertical scroll
9469 static void scroll_list(LISTVIEW_INFO *infoPtr, INT dx, INT dy)
9471 /* now we can scroll the list */
9472 ScrollWindowEx(infoPtr->hwndSelf, dx, dy, &infoPtr->rcList,
9473 &infoPtr->rcList, 0, 0, SW_ERASE | SW_INVALIDATE);
9474 /* if we have focus, adjust rect */
9475 OffsetRect(&infoPtr->rcFocus, dx, dy);
9476 UpdateWindow(infoPtr->hwndSelf);
9481 * Performs vertical scrolling.
9484 * [I] infoPtr : valid pointer to the listview structure
9485 * [I] nScrollCode : scroll code
9486 * [I] nScrollDiff : units to scroll in SB_INTERNAL mode, 0 otherwise
9487 * [I] hScrollWnd : scrollbar control window handle
9493 * SB_LINEUP/SB_LINEDOWN:
9494 * for LVS_ICON, LVS_SMALLICON is 37 by experiment
9495 * for LVS_REPORT is 1 line
9496 * for LVS_LIST cannot occur
9499 static LRESULT LISTVIEW_VScroll(LISTVIEW_INFO *infoPtr, INT nScrollCode,
9502 INT nOldScrollPos, nNewScrollPos;
9503 SCROLLINFO scrollInfo;
9506 TRACE("(nScrollCode=%d(%s), nScrollDiff=%d)\n", nScrollCode,
9507 debugscrollcode(nScrollCode), nScrollDiff);
9509 if (infoPtr->hwndEdit) SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
9511 scrollInfo.cbSize = sizeof(SCROLLINFO);
9512 scrollInfo.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_TRACKPOS;
9514 is_an_icon = ((infoPtr->uView == LV_VIEW_ICON) || (infoPtr->uView == LV_VIEW_SMALLICON));
9516 if (!GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo)) return 1;
9518 nOldScrollPos = scrollInfo.nPos;
9519 switch (nScrollCode)
9525 nScrollDiff = (is_an_icon) ? -LISTVIEW_SCROLL_ICON_LINE_SIZE : -1;
9529 nScrollDiff = (is_an_icon) ? LISTVIEW_SCROLL_ICON_LINE_SIZE : 1;
9533 nScrollDiff = -scrollInfo.nPage;
9537 nScrollDiff = scrollInfo.nPage;
9540 case SB_THUMBPOSITION:
9542 nScrollDiff = scrollInfo.nTrackPos - scrollInfo.nPos;
9549 /* quit right away if pos isn't changing */
9550 if (nScrollDiff == 0) return 0;
9552 /* calculate new position, and handle overflows */
9553 nNewScrollPos = scrollInfo.nPos + nScrollDiff;
9554 if (nScrollDiff > 0) {
9555 if (nNewScrollPos < nOldScrollPos ||
9556 nNewScrollPos > scrollInfo.nMax)
9557 nNewScrollPos = scrollInfo.nMax;
9559 if (nNewScrollPos > nOldScrollPos ||
9560 nNewScrollPos < scrollInfo.nMin)
9561 nNewScrollPos = scrollInfo.nMin;
9564 /* set the new position, and reread in case it changed */
9565 scrollInfo.fMask = SIF_POS;
9566 scrollInfo.nPos = nNewScrollPos;
9567 nNewScrollPos = SetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo, TRUE);
9569 /* carry on only if it really changed */
9570 if (nNewScrollPos == nOldScrollPos) return 0;
9572 /* now adjust to client coordinates */
9573 nScrollDiff = nOldScrollPos - nNewScrollPos;
9574 if (infoPtr->uView == LV_VIEW_DETAILS) nScrollDiff *= infoPtr->nItemHeight;
9576 /* and scroll the window */
9577 scroll_list(infoPtr, 0, nScrollDiff);
9584 * Performs horizontal scrolling.
9587 * [I] infoPtr : valid pointer to the listview structure
9588 * [I] nScrollCode : scroll code
9589 * [I] nScrollDiff : units to scroll in SB_INTERNAL mode, 0 otherwise
9590 * [I] hScrollWnd : scrollbar control window handle
9596 * SB_LINELEFT/SB_LINERIGHT:
9597 * for LVS_ICON, LVS_SMALLICON 1 pixel
9598 * for LVS_REPORT is 1 pixel
9599 * for LVS_LIST is 1 column --> which is a 1 because the
9600 * scroll is based on columns not pixels
9603 static LRESULT LISTVIEW_HScroll(LISTVIEW_INFO *infoPtr, INT nScrollCode,
9606 INT nOldScrollPos, nNewScrollPos;
9607 SCROLLINFO scrollInfo;
9610 TRACE("(nScrollCode=%d(%s), nScrollDiff=%d)\n", nScrollCode,
9611 debugscrollcode(nScrollCode), nScrollDiff);
9613 if (infoPtr->hwndEdit) SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
9615 scrollInfo.cbSize = sizeof(SCROLLINFO);
9616 scrollInfo.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_TRACKPOS;
9618 is_an_icon = ((infoPtr->uView == LV_VIEW_ICON) || (infoPtr->uView == LV_VIEW_SMALLICON));
9620 if (!GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo)) return 1;
9622 nOldScrollPos = scrollInfo.nPos;
9624 switch (nScrollCode)
9630 nScrollDiff = (is_an_icon) ? -LISTVIEW_SCROLL_ICON_LINE_SIZE : -1;
9634 nScrollDiff = (is_an_icon) ? LISTVIEW_SCROLL_ICON_LINE_SIZE : 1;
9638 nScrollDiff = -scrollInfo.nPage;
9642 nScrollDiff = scrollInfo.nPage;
9645 case SB_THUMBPOSITION:
9647 nScrollDiff = scrollInfo.nTrackPos - scrollInfo.nPos;
9654 /* quit right away if pos isn't changing */
9655 if (nScrollDiff == 0) return 0;
9657 /* calculate new position, and handle overflows */
9658 nNewScrollPos = scrollInfo.nPos + nScrollDiff;
9659 if (nScrollDiff > 0) {
9660 if (nNewScrollPos < nOldScrollPos ||
9661 nNewScrollPos > scrollInfo.nMax)
9662 nNewScrollPos = scrollInfo.nMax;
9664 if (nNewScrollPos > nOldScrollPos ||
9665 nNewScrollPos < scrollInfo.nMin)
9666 nNewScrollPos = scrollInfo.nMin;
9669 /* set the new position, and reread in case it changed */
9670 scrollInfo.fMask = SIF_POS;
9671 scrollInfo.nPos = nNewScrollPos;
9672 nNewScrollPos = SetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo, TRUE);
9674 /* carry on only if it really changed */
9675 if (nNewScrollPos == nOldScrollPos) return 0;
9677 if (infoPtr->hwndHeader) LISTVIEW_UpdateHeaderSize(infoPtr, nNewScrollPos);
9679 /* now adjust to client coordinates */
9680 nScrollDiff = nOldScrollPos - nNewScrollPos;
9681 if (infoPtr->uView == LV_VIEW_LIST) nScrollDiff *= infoPtr->nItemWidth;
9683 /* and scroll the window */
9684 scroll_list(infoPtr, nScrollDiff, 0);
9689 static LRESULT LISTVIEW_MouseWheel(LISTVIEW_INFO *infoPtr, INT wheelDelta)
9691 INT gcWheelDelta = 0;
9692 INT pulScrollLines = 3;
9694 TRACE("(wheelDelta=%d)\n", wheelDelta);
9696 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
9697 gcWheelDelta -= wheelDelta;
9699 switch(infoPtr->uView)
9702 case LV_VIEW_SMALLICON:
9704 * listview should be scrolled by a multiple of 37 dependently on its dimension or its visible item number
9705 * should be fixed in the future.
9707 LISTVIEW_VScroll(infoPtr, SB_INTERNAL, (gcWheelDelta < 0) ?
9708 -LISTVIEW_SCROLL_ICON_LINE_SIZE : LISTVIEW_SCROLL_ICON_LINE_SIZE);
9711 case LV_VIEW_DETAILS:
9712 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
9714 int cLineScroll = min(LISTVIEW_GetCountPerColumn(infoPtr), pulScrollLines);
9715 cLineScroll *= (gcWheelDelta / WHEEL_DELTA);
9716 LISTVIEW_VScroll(infoPtr, SB_INTERNAL, cLineScroll);
9721 LISTVIEW_HScroll(infoPtr, (gcWheelDelta < 0) ? SB_LINELEFT : SB_LINERIGHT, 0);
9732 * [I] infoPtr : valid pointer to the listview structure
9733 * [I] nVirtualKey : virtual key
9734 * [I] lKeyData : key data
9739 static LRESULT LISTVIEW_KeyDown(LISTVIEW_INFO *infoPtr, INT nVirtualKey, LONG lKeyData)
9741 HWND hwndSelf = infoPtr->hwndSelf;
9743 NMLVKEYDOWN nmKeyDown;
9745 TRACE("(nVirtualKey=%d, lKeyData=%d)\n", nVirtualKey, lKeyData);
9747 /* send LVN_KEYDOWN notification */
9748 nmKeyDown.wVKey = nVirtualKey;
9749 nmKeyDown.flags = 0;
9750 notify_hdr(infoPtr, LVN_KEYDOWN, &nmKeyDown.hdr);
9751 if (!IsWindow(hwndSelf))
9754 switch (nVirtualKey)
9757 nItem = infoPtr->nFocusedItem;
9758 if (infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES)
9759 toggle_checkbox_state(infoPtr, infoPtr->nFocusedItem);
9763 if ((infoPtr->nItemCount > 0) && (infoPtr->nFocusedItem != -1))
9765 if (!notify(infoPtr, NM_RETURN)) return 0;
9766 if (!notify(infoPtr, LVN_ITEMACTIVATE)) return 0;
9771 if (infoPtr->nItemCount > 0)
9776 if (infoPtr->nItemCount > 0)
9777 nItem = infoPtr->nItemCount - 1;
9781 nItem = LISTVIEW_GetNextItem(infoPtr, infoPtr->nFocusedItem, LVNI_TOLEFT);
9785 nItem = LISTVIEW_GetNextItem(infoPtr, infoPtr->nFocusedItem, LVNI_ABOVE);
9789 nItem = LISTVIEW_GetNextItem(infoPtr, infoPtr->nFocusedItem, LVNI_TORIGHT);
9793 nItem = LISTVIEW_GetNextItem(infoPtr, infoPtr->nFocusedItem, LVNI_BELOW);
9797 if (infoPtr->uView == LV_VIEW_DETAILS)
9799 INT topidx = LISTVIEW_GetTopIndex(infoPtr);
9800 if (infoPtr->nFocusedItem == topidx)
9801 nItem = topidx - LISTVIEW_GetCountPerColumn(infoPtr) + 1;
9806 nItem = infoPtr->nFocusedItem - LISTVIEW_GetCountPerColumn(infoPtr)
9807 * LISTVIEW_GetCountPerRow(infoPtr);
9808 if(nItem < 0) nItem = 0;
9812 if (infoPtr->uView == LV_VIEW_DETAILS)
9814 INT topidx = LISTVIEW_GetTopIndex(infoPtr);
9815 INT cnt = LISTVIEW_GetCountPerColumn(infoPtr);
9816 if (infoPtr->nFocusedItem == topidx + cnt - 1)
9817 nItem = infoPtr->nFocusedItem + cnt - 1;
9819 nItem = topidx + cnt - 1;
9822 nItem = infoPtr->nFocusedItem + LISTVIEW_GetCountPerColumn(infoPtr)
9823 * LISTVIEW_GetCountPerRow(infoPtr);
9824 if(nItem >= infoPtr->nItemCount) nItem = infoPtr->nItemCount - 1;
9828 if ((nItem != -1) && (nItem != infoPtr->nFocusedItem || nVirtualKey == VK_SPACE))
9829 LISTVIEW_KeySelection(infoPtr, nItem, nVirtualKey == VK_SPACE);
9839 * [I] infoPtr : valid pointer to the listview structure
9844 static LRESULT LISTVIEW_KillFocus(LISTVIEW_INFO *infoPtr)
9848 /* if we did not have the focus, there's nothing to do */
9849 if (!infoPtr->bFocus) return 0;
9851 /* send NM_KILLFOCUS notification */
9852 if (!notify(infoPtr, NM_KILLFOCUS)) return 0;
9854 /* if we have a focus rectangle, get rid of it */
9855 LISTVIEW_ShowFocusRect(infoPtr, FALSE);
9857 /* if have a marquee selection, stop it */
9858 if (infoPtr->bMarqueeSelect)
9860 /* Remove the marquee rectangle and release our mouse capture */
9861 LISTVIEW_InvalidateRect(infoPtr, &infoPtr->marqueeRect);
9864 SetRect(&infoPtr->marqueeRect, 0, 0, 0, 0);
9866 infoPtr->bMarqueeSelect = FALSE;
9867 infoPtr->bScrolling = FALSE;
9868 KillTimer(infoPtr->hwndSelf, (UINT_PTR) infoPtr);
9871 /* set window focus flag */
9872 infoPtr->bFocus = FALSE;
9874 /* invalidate the selected items before resetting focus flag */
9875 LISTVIEW_InvalidateSelectedItems(infoPtr);
9882 * Processes double click messages (left mouse button).
9885 * [I] infoPtr : valid pointer to the listview structure
9886 * [I] wKey : key flag
9887 * [I] x,y : mouse coordinate
9892 static LRESULT LISTVIEW_LButtonDblClk(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
9894 LVHITTESTINFO htInfo;
9896 TRACE("(key=%hu, X=%u, Y=%u)\n", wKey, x, y);
9898 /* Cancel the item edition if any */
9899 if (infoPtr->itemEdit.fEnabled)
9901 KillTimer(infoPtr->hwndSelf, (UINT_PTR)&infoPtr->itemEdit);
9902 infoPtr->itemEdit.fEnabled = FALSE;
9905 /* send NM_RELEASEDCAPTURE notification */
9906 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
9911 /* send NM_DBLCLK notification */
9912 LISTVIEW_HitTest(infoPtr, &htInfo, TRUE, FALSE);
9913 if (!notify_click(infoPtr, NM_DBLCLK, &htInfo)) return 0;
9915 /* To send the LVN_ITEMACTIVATE, it must be on an Item */
9916 if(htInfo.iItem != -1) notify_itemactivate(infoPtr,&htInfo);
9923 * Processes mouse down messages (left mouse button).
9926 * infoPtr [I ] valid pointer to the listview structure
9927 * wKey [I ] key flag
9928 * x,y [I ] mouse coordinate
9933 static LRESULT LISTVIEW_LButtonDown(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
9935 LVHITTESTINFO lvHitTestInfo;
9936 static BOOL bGroupSelect = TRUE;
9937 POINT pt = { x, y };
9940 TRACE("(key=%hu, X=%u, Y=%u)\n", wKey, x, y);
9942 /* send NM_RELEASEDCAPTURE notification */
9943 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
9945 /* set left button down flag and record the click position */
9946 infoPtr->bLButtonDown = TRUE;
9947 infoPtr->ptClickPos = pt;
9948 infoPtr->bDragging = FALSE;
9949 infoPtr->bMarqueeSelect = FALSE;
9950 infoPtr->bScrolling = FALSE;
9952 lvHitTestInfo.pt.x = x;
9953 lvHitTestInfo.pt.y = y;
9955 nItem = LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, TRUE);
9956 TRACE("at %s, nItem=%d\n", wine_dbgstr_point(&pt), nItem);
9957 if ((nItem >= 0) && (nItem < infoPtr->nItemCount))
9959 if ((infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES) && (lvHitTestInfo.flags & LVHT_ONITEMSTATEICON))
9961 toggle_checkbox_state(infoPtr, nItem);
9965 if (infoPtr->dwStyle & LVS_SINGLESEL)
9967 if (LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED))
9968 infoPtr->nEditLabelItem = nItem;
9970 LISTVIEW_SetSelection(infoPtr, nItem);
9974 if ((wKey & MK_CONTROL) && (wKey & MK_SHIFT))
9978 if (!LISTVIEW_AddGroupSelection(infoPtr, nItem)) return 0;
9979 LISTVIEW_SetItemFocus(infoPtr, nItem);
9980 infoPtr->nSelectionMark = nItem;
9986 item.state = LVIS_SELECTED | LVIS_FOCUSED;
9987 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
9989 LISTVIEW_SetItemState(infoPtr,nItem,&item);
9990 infoPtr->nSelectionMark = nItem;
9993 else if (wKey & MK_CONTROL)
9997 bGroupSelect = (LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED) == 0);
9999 item.state = (bGroupSelect ? LVIS_SELECTED : 0) | LVIS_FOCUSED;
10000 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
10001 LISTVIEW_SetItemState(infoPtr, nItem, &item);
10002 infoPtr->nSelectionMark = nItem;
10004 else if (wKey & MK_SHIFT)
10006 LISTVIEW_SetGroupSelection(infoPtr, nItem);
10010 if (LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED))
10012 infoPtr->nEditLabelItem = nItem;
10013 infoPtr->nLButtonDownItem = nItem;
10015 LISTVIEW_SetItemFocus(infoPtr, nItem);
10018 /* set selection (clears other pre-existing selections) */
10019 LISTVIEW_SetSelection(infoPtr, nItem);
10023 if (infoPtr->dwLvExStyle & LVS_EX_ONECLICKACTIVATE)
10024 if(lvHitTestInfo.iItem != -1) notify_itemactivate(infoPtr,&lvHitTestInfo);
10028 if (!infoPtr->bFocus)
10029 SetFocus(infoPtr->hwndSelf);
10031 /* remove all selections */
10032 if (!(wKey & MK_CONTROL) && !(wKey & MK_SHIFT))
10033 LISTVIEW_DeselectAll(infoPtr);
10042 * Processes mouse up messages (left mouse button).
10045 * infoPtr [I ] valid pointer to the listview structure
10046 * wKey [I ] key flag
10047 * x,y [I ] mouse coordinate
10052 static LRESULT LISTVIEW_LButtonUp(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
10054 LVHITTESTINFO lvHitTestInfo;
10056 TRACE("(key=%hu, X=%u, Y=%u)\n", wKey, x, y);
10058 if (!infoPtr->bLButtonDown) return 0;
10060 lvHitTestInfo.pt.x = x;
10061 lvHitTestInfo.pt.y = y;
10063 /* send NM_CLICK notification */
10064 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
10065 if (!notify_click(infoPtr, NM_CLICK, &lvHitTestInfo)) return 0;
10067 /* set left button flag */
10068 infoPtr->bLButtonDown = FALSE;
10070 /* set a single selection, reset others */
10071 if(lvHitTestInfo.iItem == infoPtr->nLButtonDownItem && lvHitTestInfo.iItem != -1)
10072 LISTVIEW_SetSelection(infoPtr, infoPtr->nLButtonDownItem);
10073 infoPtr->nLButtonDownItem = -1;
10075 if (infoPtr->bDragging || infoPtr->bMarqueeSelect)
10077 /* Remove the marquee rectangle and release our mouse capture */
10078 if (infoPtr->bMarqueeSelect)
10080 LISTVIEW_InvalidateRect(infoPtr, &infoPtr->marqueeDrawRect);
10084 SetRect(&infoPtr->marqueeRect, 0, 0, 0, 0);
10085 SetRect(&infoPtr->marqueeDrawRect, 0, 0, 0, 0);
10087 infoPtr->bDragging = FALSE;
10088 infoPtr->bMarqueeSelect = FALSE;
10089 infoPtr->bScrolling = FALSE;
10091 KillTimer(infoPtr->hwndSelf, (UINT_PTR) infoPtr);
10095 /* if we clicked on a selected item, edit the label */
10096 if(lvHitTestInfo.iItem == infoPtr->nEditLabelItem && (lvHitTestInfo.flags & LVHT_ONITEMLABEL))
10098 /* we want to make sure the user doesn't want to do a double click. So we will
10099 * delay the edit. WM_LBUTTONDBLCLICK will cancel the timer
10101 infoPtr->itemEdit.fEnabled = TRUE;
10102 infoPtr->itemEdit.iItem = lvHitTestInfo.iItem;
10103 SetTimer(infoPtr->hwndSelf,
10104 (UINT_PTR)&infoPtr->itemEdit,
10105 GetDoubleClickTime(),
10106 LISTVIEW_DelayedEditItem);
10109 if (!infoPtr->bFocus)
10110 SetFocus(infoPtr->hwndSelf);
10117 * Destroys the listview control (called after WM_DESTROY).
10120 * [I] infoPtr : valid pointer to the listview structure
10125 static LRESULT LISTVIEW_NCDestroy(LISTVIEW_INFO *infoPtr)
10131 /* destroy data structure */
10132 DPA_Destroy(infoPtr->hdpaItems);
10133 DPA_Destroy(infoPtr->hdpaItemIds);
10134 DPA_Destroy(infoPtr->hdpaPosX);
10135 DPA_Destroy(infoPtr->hdpaPosY);
10137 for (i = 0; i < DPA_GetPtrCount(infoPtr->hdpaColumns); i++)
10138 Free(DPA_GetPtr(infoPtr->hdpaColumns, i));
10139 DPA_Destroy(infoPtr->hdpaColumns);
10140 ranges_destroy(infoPtr->selectionRanges);
10142 /* destroy image lists */
10143 if (!(infoPtr->dwStyle & LVS_SHAREIMAGELISTS))
10145 ImageList_Destroy(infoPtr->himlNormal);
10146 ImageList_Destroy(infoPtr->himlSmall);
10147 ImageList_Destroy(infoPtr->himlState);
10150 /* destroy font, bkgnd brush */
10151 infoPtr->hFont = 0;
10152 if (infoPtr->hDefaultFont) DeleteObject(infoPtr->hDefaultFont);
10153 if (infoPtr->clrBk != CLR_NONE) DeleteObject(infoPtr->hBkBrush);
10155 SetWindowLongPtrW(infoPtr->hwndSelf, 0, 0);
10157 /* free listview info pointer*/
10165 * Handles notifications.
10168 * [I] infoPtr : valid pointer to the listview structure
10169 * [I] lpnmhdr : notification information
10174 static LRESULT LISTVIEW_Notify(LISTVIEW_INFO *infoPtr, const NMHDR *lpnmhdr)
10176 const NMHEADERW *lpnmh;
10178 TRACE("(lpnmhdr=%p)\n", lpnmhdr);
10180 if (!lpnmhdr || lpnmhdr->hwndFrom != infoPtr->hwndHeader) return 0;
10182 /* remember: HDN_LAST < HDN_FIRST */
10183 if (lpnmhdr->code > HDN_FIRST || lpnmhdr->code < HDN_LAST) return 0;
10184 lpnmh = (const NMHEADERW *)lpnmhdr;
10186 if (lpnmh->iItem < 0 || lpnmh->iItem >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return 0;
10188 switch (lpnmhdr->code)
10193 COLUMN_INFO *lpColumnInfo;
10197 if (!lpnmh->pitem || !(lpnmh->pitem->mask & HDI_WIDTH))
10200 /* remove the old line (if any) */
10201 LISTVIEW_DrawTrackLine(infoPtr);
10203 /* compute & draw the new line */
10204 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, lpnmh->iItem);
10205 x = lpColumnInfo->rcHeader.left + lpnmh->pitem->cxy;
10206 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
10207 infoPtr->xTrackLine = x + ptOrigin.x;
10208 LISTVIEW_DrawTrackLine(infoPtr);
10212 case HDN_ENDTRACKA:
10213 case HDN_ENDTRACKW:
10214 /* remove the track line (if any) */
10215 LISTVIEW_DrawTrackLine(infoPtr);
10216 infoPtr->xTrackLine = -1;
10219 case HDN_BEGINDRAG:
10220 notify_forward_header(infoPtr, lpnmh);
10221 return (infoPtr->dwLvExStyle & LVS_EX_HEADERDRAGDROP) == 0;
10224 infoPtr->colRectsDirty = TRUE;
10225 LISTVIEW_InvalidateList(infoPtr);
10226 notify_forward_header(infoPtr, lpnmh);
10229 case HDN_ITEMCHANGEDW:
10230 case HDN_ITEMCHANGEDA:
10232 COLUMN_INFO *lpColumnInfo;
10236 if (!lpnmh->pitem || !(lpnmh->pitem->mask & HDI_WIDTH))
10238 hdi.mask = HDI_WIDTH;
10239 if (!SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, lpnmh->iItem, (LPARAM)&hdi)) return 0;
10243 cxy = lpnmh->pitem->cxy;
10245 /* determine how much we change since the last know position */
10246 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, lpnmh->iItem);
10247 dx = cxy - (lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left);
10250 lpColumnInfo->rcHeader.right += dx;
10252 hdi.mask = HDI_ORDER;
10253 SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, lpnmh->iItem, (LPARAM)&hdi);
10255 /* not the rightmost one */
10256 if (hdi.iOrder + 1 < DPA_GetPtrCount(infoPtr->hdpaColumns))
10258 INT nIndex = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX,
10259 hdi.iOrder + 1, 0);
10260 LISTVIEW_ScrollColumns(infoPtr, nIndex, dx);
10264 /* only needs to update the scrolls */
10265 infoPtr->nItemWidth += dx;
10266 LISTVIEW_UpdateScroll(infoPtr);
10268 LISTVIEW_UpdateItemSize(infoPtr);
10269 if (infoPtr->uView == LV_VIEW_DETAILS && is_redrawing(infoPtr))
10272 RECT rcCol = lpColumnInfo->rcHeader;
10274 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
10275 OffsetRect(&rcCol, ptOrigin.x, 0);
10277 rcCol.top = infoPtr->rcList.top;
10278 rcCol.bottom = infoPtr->rcList.bottom;
10280 /* resizing left-aligned columns leaves most of the left side untouched */
10281 if ((lpColumnInfo->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_LEFT)
10283 INT nMaxDirty = infoPtr->nEllipsisWidth + infoPtr->ntmMaxCharWidth;
10286 rcCol.left = max (rcCol.left, rcCol.right - nMaxDirty);
10289 /* when shrinking the last column clear the now unused field */
10290 if (hdi.iOrder == DPA_GetPtrCount(infoPtr->hdpaColumns) - 1)
10296 /* deal with right from rightmost column area */
10297 right.left = rcCol.right;
10298 right.top = rcCol.top;
10299 right.bottom = rcCol.bottom;
10300 right.right = infoPtr->rcList.right;
10302 LISTVIEW_InvalidateRect(infoPtr, &right);
10305 LISTVIEW_InvalidateRect(infoPtr, &rcCol);
10311 case HDN_ITEMCLICKW:
10312 case HDN_ITEMCLICKA:
10314 /* Handle sorting by Header Column */
10317 ZeroMemory(&nmlv, sizeof(NMLISTVIEW));
10319 nmlv.iSubItem = lpnmh->iItem;
10320 notify_listview(infoPtr, LVN_COLUMNCLICK, &nmlv);
10321 notify_forward_header(infoPtr, lpnmh);
10325 case HDN_DIVIDERDBLCLICKW:
10326 case HDN_DIVIDERDBLCLICKA:
10327 /* FIXME: for LVS_EX_HEADERINALLVIEWS and not LV_VIEW_DETAILS
10328 we should use LVSCW_AUTOSIZE_USEHEADER, helper rework or
10329 split needed for that */
10330 LISTVIEW_SetColumnWidth(infoPtr, lpnmh->iItem, LVSCW_AUTOSIZE);
10331 notify_forward_header(infoPtr, lpnmh);
10340 * Paint non-client area of control.
10343 * [I] infoPtr : valid pointer to the listview structureof the sender
10344 * [I] region : update region
10347 * TRUE - frame was painted
10348 * FALSE - call default window proc
10350 static BOOL LISTVIEW_NCPaint(const LISTVIEW_INFO *infoPtr, HRGN region)
10352 HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
10356 int cxEdge = GetSystemMetrics (SM_CXEDGE),
10357 cyEdge = GetSystemMetrics (SM_CYEDGE);
10360 return DefWindowProcW (infoPtr->hwndSelf, WM_NCPAINT, (WPARAM)region, 0);
10362 GetWindowRect(infoPtr->hwndSelf, &r);
10364 cliprgn = CreateRectRgn (r.left + cxEdge, r.top + cyEdge,
10365 r.right - cxEdge, r.bottom - cyEdge);
10366 if (region != (HRGN)1)
10367 CombineRgn (cliprgn, cliprgn, region, RGN_AND);
10368 OffsetRect(&r, -r.left, -r.top);
10370 dc = GetDCEx(infoPtr->hwndSelf, region, DCX_WINDOW|DCX_INTERSECTRGN);
10371 OffsetRect(&r, -r.left, -r.top);
10373 if (IsThemeBackgroundPartiallyTransparent (theme, 0, 0))
10374 DrawThemeParentBackground(infoPtr->hwndSelf, dc, &r);
10375 DrawThemeBackground (theme, dc, 0, 0, &r, 0);
10376 ReleaseDC(infoPtr->hwndSelf, dc);
10378 /* Call default proc to get the scrollbars etc. painted */
10379 DefWindowProcW (infoPtr->hwndSelf, WM_NCPAINT, (WPARAM)cliprgn, 0);
10386 * Determines the type of structure to use.
10389 * [I] infoPtr : valid pointer to the listview structureof the sender
10390 * [I] hwndFrom : listview window handle
10391 * [I] nCommand : command specifying the nature of the WM_NOTIFYFORMAT
10396 static LRESULT LISTVIEW_NotifyFormat(LISTVIEW_INFO *infoPtr, HWND hwndFrom, INT nCommand)
10398 TRACE("(hwndFrom=%p, nCommand=%d)\n", hwndFrom, nCommand);
10400 if (nCommand == NF_REQUERY)
10401 infoPtr->notifyFormat = SendMessageW(infoPtr->hwndNotify, WM_NOTIFYFORMAT, (WPARAM)infoPtr->hwndSelf, NF_QUERY);
10403 return infoPtr->notifyFormat;
10408 * Paints/Repaints the listview control. Internal use.
10411 * [I] infoPtr : valid pointer to the listview structure
10412 * [I] hdc : device context handle
10417 static LRESULT LISTVIEW_Paint(LISTVIEW_INFO *infoPtr, HDC hdc)
10419 TRACE("(hdc=%p)\n", hdc);
10421 if (infoPtr->bNoItemMetrics && infoPtr->nItemCount)
10423 infoPtr->bNoItemMetrics = FALSE;
10424 LISTVIEW_UpdateItemSize(infoPtr);
10425 if (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON)
10426 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
10427 LISTVIEW_UpdateScroll(infoPtr);
10430 if (infoPtr->hwndHeader) UpdateWindow(infoPtr->hwndHeader);
10433 LISTVIEW_Refresh(infoPtr, hdc, NULL);
10438 hdc = BeginPaint(infoPtr->hwndSelf, &ps);
10439 if (!hdc) return 1;
10440 LISTVIEW_Refresh(infoPtr, hdc, ps.fErase ? &ps.rcPaint : NULL);
10441 EndPaint(infoPtr->hwndSelf, &ps);
10449 * Paints/Repaints the listview control, WM_PAINT handler.
10452 * [I] infoPtr : valid pointer to the listview structure
10453 * [I] hdc : device context handle
10458 static inline LRESULT LISTVIEW_WMPaint(LISTVIEW_INFO *infoPtr, HDC hdc)
10460 TRACE("(hdc=%p)\n", hdc);
10462 if (!is_redrawing(infoPtr))
10463 return DefWindowProcW (infoPtr->hwndSelf, WM_PAINT, (WPARAM)hdc, 0);
10465 return LISTVIEW_Paint(infoPtr, hdc);
10470 * Paints/Repaints the listview control.
10473 * [I] infoPtr : valid pointer to the listview structure
10474 * [I] hdc : device context handle
10475 * [I] options : drawing options
10480 static LRESULT LISTVIEW_PrintClient(LISTVIEW_INFO *infoPtr, HDC hdc, DWORD options)
10482 FIXME("Partial Stub: (hdc=%p options=0x%08x)\n", hdc, options);
10484 if ((options & PRF_CHECKVISIBLE) && !IsWindowVisible(infoPtr->hwndSelf))
10487 if (options & PRF_ERASEBKGND)
10488 LISTVIEW_EraseBkgnd(infoPtr, hdc);
10490 if (options & PRF_CLIENT)
10491 LISTVIEW_Paint(infoPtr, hdc);
10499 * Processes double click messages (right mouse button).
10502 * [I] infoPtr : valid pointer to the listview structure
10503 * [I] wKey : key flag
10504 * [I] x,y : mouse coordinate
10509 static LRESULT LISTVIEW_RButtonDblClk(const LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
10511 LVHITTESTINFO lvHitTestInfo;
10513 TRACE("(key=%hu,X=%u,Y=%u)\n", wKey, x, y);
10515 /* send NM_RELEASEDCAPTURE notification */
10516 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
10518 /* send NM_RDBLCLK notification */
10519 lvHitTestInfo.pt.x = x;
10520 lvHitTestInfo.pt.y = y;
10521 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
10522 notify_click(infoPtr, NM_RDBLCLK, &lvHitTestInfo);
10529 * Processes mouse down messages (right mouse button).
10532 * [I] infoPtr : valid pointer to the listview structure
10533 * [I] wKey : key flag
10534 * [I] x,y : mouse coordinate
10539 static LRESULT LISTVIEW_RButtonDown(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
10541 LVHITTESTINFO lvHitTestInfo;
10544 TRACE("(key=%hu,X=%u,Y=%u)\n", wKey, x, y);
10546 /* send NM_RELEASEDCAPTURE notification */
10547 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
10549 /* make sure the listview control window has the focus */
10550 if (!infoPtr->bFocus) SetFocus(infoPtr->hwndSelf);
10552 /* set right button down flag */
10553 infoPtr->bRButtonDown = TRUE;
10555 /* determine the index of the selected item */
10556 lvHitTestInfo.pt.x = x;
10557 lvHitTestInfo.pt.y = y;
10558 nItem = LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, TRUE);
10560 if ((nItem >= 0) && (nItem < infoPtr->nItemCount))
10562 LISTVIEW_SetItemFocus(infoPtr, nItem);
10563 if (!((wKey & MK_SHIFT) || (wKey & MK_CONTROL)) &&
10564 !LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED))
10565 LISTVIEW_SetSelection(infoPtr, nItem);
10569 LISTVIEW_DeselectAll(infoPtr);
10577 * Processes mouse up messages (right mouse button).
10580 * [I] infoPtr : valid pointer to the listview structure
10581 * [I] wKey : key flag
10582 * [I] x,y : mouse coordinate
10587 static LRESULT LISTVIEW_RButtonUp(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
10589 LVHITTESTINFO lvHitTestInfo;
10592 TRACE("(key=%hu,X=%u,Y=%u)\n", wKey, x, y);
10594 if (!infoPtr->bRButtonDown) return 0;
10596 /* set button flag */
10597 infoPtr->bRButtonDown = FALSE;
10599 /* Send NM_RCLICK notification */
10600 lvHitTestInfo.pt.x = x;
10601 lvHitTestInfo.pt.y = y;
10602 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
10603 if (!notify_click(infoPtr, NM_RCLICK, &lvHitTestInfo)) return 0;
10605 /* Change to screen coordinate for WM_CONTEXTMENU */
10606 pt = lvHitTestInfo.pt;
10607 ClientToScreen(infoPtr->hwndSelf, &pt);
10609 /* Send a WM_CONTEXTMENU message in response to the RBUTTONUP */
10610 SendMessageW(infoPtr->hwndSelf, WM_CONTEXTMENU,
10611 (WPARAM)infoPtr->hwndSelf, MAKELPARAM(pt.x, pt.y));
10622 * [I] infoPtr : valid pointer to the listview structure
10623 * [I] hwnd : window handle of window containing the cursor
10624 * [I] nHittest : hit-test code
10625 * [I] wMouseMsg : ideintifier of the mouse message
10628 * TRUE if cursor is set
10631 static BOOL LISTVIEW_SetCursor(const LISTVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
10633 LVHITTESTINFO lvHitTestInfo;
10635 if (!LISTVIEW_IsHotTracking(infoPtr)) goto forward;
10637 if (!infoPtr->hHotCursor) goto forward;
10639 GetCursorPos(&lvHitTestInfo.pt);
10640 if (LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, FALSE, FALSE) < 0) goto forward;
10642 SetCursor(infoPtr->hHotCursor);
10648 return DefWindowProcW(infoPtr->hwndSelf, WM_SETCURSOR, wParam, lParam);
10656 * [I] infoPtr : valid pointer to the listview structure
10657 * [I] hwndLoseFocus : handle of previously focused window
10662 static LRESULT LISTVIEW_SetFocus(LISTVIEW_INFO *infoPtr, HWND hwndLoseFocus)
10664 TRACE("(hwndLoseFocus=%p)\n", hwndLoseFocus);
10666 /* if we have the focus already, there's nothing to do */
10667 if (infoPtr->bFocus) return 0;
10669 /* send NM_SETFOCUS notification */
10670 if (!notify(infoPtr, NM_SETFOCUS)) return 0;
10672 /* set window focus flag */
10673 infoPtr->bFocus = TRUE;
10675 /* put the focus rect back on */
10676 LISTVIEW_ShowFocusRect(infoPtr, TRUE);
10678 /* redraw all visible selected items */
10679 LISTVIEW_InvalidateSelectedItems(infoPtr);
10689 * [I] infoPtr : valid pointer to the listview structure
10690 * [I] fRedraw : font handle
10691 * [I] fRedraw : redraw flag
10696 static LRESULT LISTVIEW_SetFont(LISTVIEW_INFO *infoPtr, HFONT hFont, WORD fRedraw)
10698 HFONT oldFont = infoPtr->hFont;
10700 TRACE("(hfont=%p,redraw=%hu)\n", hFont, fRedraw);
10702 infoPtr->hFont = hFont ? hFont : infoPtr->hDefaultFont;
10703 if (infoPtr->hFont == oldFont) return 0;
10705 LISTVIEW_SaveTextMetrics(infoPtr);
10707 if (infoPtr->uView == LV_VIEW_DETAILS)
10709 SendMessageW(infoPtr->hwndHeader, WM_SETFONT, (WPARAM)hFont, MAKELPARAM(fRedraw, 0));
10710 LISTVIEW_UpdateSize(infoPtr);
10711 LISTVIEW_UpdateScroll(infoPtr);
10714 if (fRedraw) LISTVIEW_InvalidateList(infoPtr);
10721 * Message handling for WM_SETREDRAW.
10722 * For the Listview, it invalidates the entire window (the doc specifies otherwise)
10725 * [I] infoPtr : valid pointer to the listview structure
10726 * [I] bRedraw: state of redraw flag
10729 * DefWinProc return value
10731 static LRESULT LISTVIEW_SetRedraw(LISTVIEW_INFO *infoPtr, BOOL bRedraw)
10733 TRACE("infoPtr->bRedraw=%d, bRedraw=%d\n", infoPtr->bRedraw, bRedraw);
10735 /* we cannot use straight equality here because _any_ non-zero value is TRUE */
10736 if ((infoPtr->bRedraw && bRedraw) || (!infoPtr->bRedraw && !bRedraw)) return 0;
10738 infoPtr->bRedraw = bRedraw;
10740 if(!bRedraw) return 0;
10742 if (is_autoarrange(infoPtr))
10743 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
10744 LISTVIEW_UpdateScroll(infoPtr);
10746 /* despite what the WM_SETREDRAW docs says, apps expect us
10747 * to invalidate the listview here... stupid! */
10748 LISTVIEW_InvalidateList(infoPtr);
10755 * Resizes the listview control. This function processes WM_SIZE
10756 * messages. At this time, the width and height are not used.
10759 * [I] infoPtr : valid pointer to the listview structure
10760 * [I] Width : new width
10761 * [I] Height : new height
10766 static LRESULT LISTVIEW_Size(LISTVIEW_INFO *infoPtr, int Width, int Height)
10768 RECT rcOld = infoPtr->rcList;
10770 TRACE("(width=%d, height=%d)\n", Width, Height);
10772 LISTVIEW_UpdateSize(infoPtr);
10773 if (EqualRect(&rcOld, &infoPtr->rcList)) return 0;
10775 /* do not bother with display related stuff if we're not redrawing */
10776 if (!is_redrawing(infoPtr)) return 0;
10778 if (is_autoarrange(infoPtr))
10779 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
10781 LISTVIEW_UpdateScroll(infoPtr);
10783 /* refresh all only for lists whose height changed significantly */
10784 if ((infoPtr->uView == LV_VIEW_LIST) &&
10785 (rcOld.bottom - rcOld.top) / infoPtr->nItemHeight !=
10786 (infoPtr->rcList.bottom - infoPtr->rcList.top) / infoPtr->nItemHeight)
10787 LISTVIEW_InvalidateList(infoPtr);
10794 * Sets the size information.
10797 * [I] infoPtr : valid pointer to the listview structure
10802 static void LISTVIEW_UpdateSize(LISTVIEW_INFO *infoPtr)
10804 TRACE("uView=%d, rcList(old)=%s\n", infoPtr->uView, wine_dbgstr_rect(&infoPtr->rcList));
10806 GetClientRect(infoPtr->hwndSelf, &infoPtr->rcList);
10808 if (infoPtr->uView == LV_VIEW_LIST)
10810 /* Apparently the "LIST" style is supposed to have the same
10811 * number of items in a column even if there is no scroll bar.
10812 * Since if a scroll bar already exists then the bottom is already
10813 * reduced, only reduce if the scroll bar does not currently exist.
10814 * The "2" is there to mimic the native control. I think it may be
10815 * related to either padding or edges. (GLA 7/2002)
10817 if (!(infoPtr->dwStyle & WS_HSCROLL))
10818 infoPtr->rcList.bottom -= GetSystemMetrics(SM_CYHSCROLL);
10819 infoPtr->rcList.bottom = max (infoPtr->rcList.bottom - 2, 0);
10822 /* if control created invisible header isn't created */
10823 if (infoPtr->hwndHeader)
10828 hl.prc = &infoPtr->rcList;
10830 SendMessageW( infoPtr->hwndHeader, HDM_LAYOUT, 0, (LPARAM)&hl );
10831 TRACE(" wp.flags=0x%08x, wp=%d,%d (%dx%d)\n", wp.flags, wp.x, wp.y, wp.cx, wp.cy);
10833 if (LISTVIEW_IsHeaderEnabled(infoPtr))
10834 wp.flags |= SWP_SHOWWINDOW;
10837 wp.flags |= SWP_HIDEWINDOW;
10841 SetWindowPos(wp.hwnd, wp.hwndInsertAfter, wp.x, wp.y, wp.cx, wp.cy, wp.flags);
10842 TRACE(" after SWP wp=%d,%d (%dx%d)\n", wp.x, wp.y, wp.cx, wp.cy);
10844 infoPtr->rcList.top = max(wp.cy, 0);
10846 /* extra padding for grid */
10847 if (infoPtr->uView == LV_VIEW_DETAILS && infoPtr->dwLvExStyle & LVS_EX_GRIDLINES)
10848 infoPtr->rcList.top += 2;
10850 TRACE(" rcList=%s\n", wine_dbgstr_rect(&infoPtr->rcList));
10855 * Processes WM_STYLECHANGED messages.
10858 * [I] infoPtr : valid pointer to the listview structure
10859 * [I] wStyleType : window style type (normal or extended)
10860 * [I] lpss : window style information
10865 static INT LISTVIEW_StyleChanged(LISTVIEW_INFO *infoPtr, WPARAM wStyleType,
10866 const STYLESTRUCT *lpss)
10868 UINT uNewView = lpss->styleNew & LVS_TYPEMASK;
10869 UINT uOldView = lpss->styleOld & LVS_TYPEMASK;
10872 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
10873 wStyleType, lpss->styleOld, lpss->styleNew);
10875 if (wStyleType != GWL_STYLE) return 0;
10877 infoPtr->dwStyle = lpss->styleNew;
10878 map_style_view(infoPtr);
10880 if (((lpss->styleOld & WS_HSCROLL) != 0)&&
10881 ((lpss->styleNew & WS_HSCROLL) == 0))
10882 ShowScrollBar(infoPtr->hwndSelf, SB_HORZ, FALSE);
10884 if (((lpss->styleOld & WS_VSCROLL) != 0)&&
10885 ((lpss->styleNew & WS_VSCROLL) == 0))
10886 ShowScrollBar(infoPtr->hwndSelf, SB_VERT, FALSE);
10888 if (uNewView != uOldView)
10890 SIZE oldIconSize = infoPtr->iconSize;
10893 SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
10894 ShowWindow(infoPtr->hwndHeader, SW_HIDE);
10896 ShowScrollBar(infoPtr->hwndSelf, SB_BOTH, FALSE);
10897 SetRectEmpty(&infoPtr->rcFocus);
10899 himl = (uNewView == LVS_ICON ? infoPtr->himlNormal : infoPtr->himlSmall);
10900 set_icon_size(&infoPtr->iconSize, himl, uNewView != LVS_ICON);
10902 if (uNewView == LVS_ICON)
10904 if ((infoPtr->iconSize.cx != oldIconSize.cx) || (infoPtr->iconSize.cy != oldIconSize.cy))
10906 TRACE("icon old size=(%d,%d), new size=(%d,%d)\n",
10907 oldIconSize.cx, oldIconSize.cy, infoPtr->iconSize.cx, infoPtr->iconSize.cy);
10908 LISTVIEW_SetIconSpacing(infoPtr, 0, 0);
10911 else if (uNewView == LVS_REPORT)
10916 LISTVIEW_CreateHeader( infoPtr );
10918 hl.prc = &infoPtr->rcList;
10920 SendMessageW( infoPtr->hwndHeader, HDM_LAYOUT, 0, (LPARAM)&hl );
10921 SetWindowPos(infoPtr->hwndHeader, infoPtr->hwndSelf, wp.x, wp.y, wp.cx, wp.cy,
10922 wp.flags | ((infoPtr->dwStyle & LVS_NOCOLUMNHEADER)
10923 ? SWP_HIDEWINDOW : SWP_SHOWWINDOW));
10926 LISTVIEW_UpdateItemSize(infoPtr);
10929 if (uNewView == LVS_REPORT || infoPtr->dwLvExStyle & LVS_EX_HEADERINALLVIEWS)
10931 if ((lpss->styleOld ^ lpss->styleNew) & LVS_NOCOLUMNHEADER)
10933 if (lpss->styleNew & LVS_NOCOLUMNHEADER)
10935 /* Turn off the header control */
10936 style = GetWindowLongW(infoPtr->hwndHeader, GWL_STYLE);
10937 TRACE("Hide header control, was 0x%08x\n", style);
10938 SetWindowLongW(infoPtr->hwndHeader, GWL_STYLE, style | HDS_HIDDEN);
10940 /* Turn on the header control */
10941 if ((style = GetWindowLongW(infoPtr->hwndHeader, GWL_STYLE)) & HDS_HIDDEN)
10943 TRACE("Show header control, was 0x%08x\n", style);
10944 SetWindowLongW(infoPtr->hwndHeader, GWL_STYLE, (style & ~HDS_HIDDEN) | WS_VISIBLE);
10950 if ( (uNewView == LVS_ICON || uNewView == LVS_SMALLICON) &&
10951 (uNewView != uOldView || ((lpss->styleNew ^ lpss->styleOld) & LVS_ALIGNMASK)) )
10952 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
10954 /* update the size of the client area */
10955 LISTVIEW_UpdateSize(infoPtr);
10957 /* add scrollbars if needed */
10958 LISTVIEW_UpdateScroll(infoPtr);
10960 /* invalidate client area + erase background */
10961 LISTVIEW_InvalidateList(infoPtr);
10968 * Processes WM_STYLECHANGING messages.
10971 * [I] wStyleType : window style type (normal or extended)
10972 * [I0] lpss : window style information
10977 static INT LISTVIEW_StyleChanging(WPARAM wStyleType,
10980 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
10981 wStyleType, lpss->styleOld, lpss->styleNew);
10983 /* don't forward LVS_OWNERDATA only if not already set to */
10984 if ((lpss->styleNew ^ lpss->styleOld) & LVS_OWNERDATA)
10986 if (lpss->styleOld & LVS_OWNERDATA)
10987 lpss->styleNew |= LVS_OWNERDATA;
10989 lpss->styleNew &= ~LVS_OWNERDATA;
10997 * Processes WM_SHOWWINDOW messages.
11000 * [I] infoPtr : valid pointer to the listview structure
11001 * [I] bShown : window is being shown (FALSE when hidden)
11002 * [I] iStatus : window show status
11007 static LRESULT LISTVIEW_ShowWindow(LISTVIEW_INFO *infoPtr, WPARAM bShown, LPARAM iStatus)
11009 /* header delayed creation */
11010 if ((infoPtr->uView == LV_VIEW_DETAILS) && bShown)
11012 LISTVIEW_CreateHeader(infoPtr);
11014 if (!(LVS_NOCOLUMNHEADER & infoPtr->dwStyle))
11015 ShowWindow(infoPtr->hwndHeader, SW_SHOWNORMAL);
11018 return DefWindowProcW(infoPtr->hwndSelf, WM_SHOWWINDOW, bShown, iStatus);
11023 * Processes CCM_GETVERSION messages.
11026 * [I] infoPtr : valid pointer to the listview structure
11031 static inline LRESULT LISTVIEW_GetVersion(const LISTVIEW_INFO *infoPtr)
11033 return infoPtr->iVersion;
11038 * Processes CCM_SETVERSION messages.
11041 * [I] infoPtr : valid pointer to the listview structure
11042 * [I] iVersion : version to be set
11045 * -1 when requested version is greater than DLL version;
11046 * previous version otherwise
11048 static LRESULT LISTVIEW_SetVersion(LISTVIEW_INFO *infoPtr, DWORD iVersion)
11050 INT iOldVersion = infoPtr->iVersion;
11052 if (iVersion > COMCTL32_VERSION)
11055 infoPtr->iVersion = iVersion;
11057 TRACE("new version %d\n", iVersion);
11059 return iOldVersion;
11064 * Window procedure of the listview control.
11067 static LRESULT WINAPI
11068 LISTVIEW_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
11070 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongPtrW(hwnd, 0);
11072 TRACE("(hwnd=%p uMsg=%x wParam=%lx lParam=%lx)\n", hwnd, uMsg, wParam, lParam);
11074 if (!infoPtr && (uMsg != WM_NCCREATE))
11075 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
11079 case LVM_APPROXIMATEVIEWRECT:
11080 return LISTVIEW_ApproximateViewRect(infoPtr, (INT)wParam,
11081 LOWORD(lParam), HIWORD(lParam));
11083 return LISTVIEW_Arrange(infoPtr, (INT)wParam);
11085 case LVM_CANCELEDITLABEL:
11086 return LISTVIEW_CancelEditLabel(infoPtr);
11088 case LVM_CREATEDRAGIMAGE:
11089 return (LRESULT)LISTVIEW_CreateDragImage(infoPtr, (INT)wParam, (LPPOINT)lParam);
11091 case LVM_DELETEALLITEMS:
11092 return LISTVIEW_DeleteAllItems(infoPtr, FALSE);
11094 case LVM_DELETECOLUMN:
11095 return LISTVIEW_DeleteColumn(infoPtr, (INT)wParam);
11097 case LVM_DELETEITEM:
11098 return LISTVIEW_DeleteItem(infoPtr, (INT)wParam);
11100 case LVM_EDITLABELA:
11101 case LVM_EDITLABELW:
11102 return (LRESULT)LISTVIEW_EditLabelT(infoPtr, (INT)wParam,
11103 uMsg == LVM_EDITLABELW);
11104 /* case LVM_ENABLEGROUPVIEW: */
11106 case LVM_ENSUREVISIBLE:
11107 return LISTVIEW_EnsureVisible(infoPtr, (INT)wParam, (BOOL)lParam);
11109 case LVM_FINDITEMW:
11110 return LISTVIEW_FindItemW(infoPtr, (INT)wParam, (LPLVFINDINFOW)lParam);
11112 case LVM_FINDITEMA:
11113 return LISTVIEW_FindItemA(infoPtr, (INT)wParam, (LPLVFINDINFOA)lParam);
11115 case LVM_GETBKCOLOR:
11116 return infoPtr->clrBk;
11118 /* case LVM_GETBKIMAGE: */
11120 case LVM_GETCALLBACKMASK:
11121 return infoPtr->uCallbackMask;
11123 case LVM_GETCOLUMNA:
11124 case LVM_GETCOLUMNW:
11125 return LISTVIEW_GetColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam,
11126 uMsg == LVM_GETCOLUMNW);
11128 case LVM_GETCOLUMNORDERARRAY:
11129 return LISTVIEW_GetColumnOrderArray(infoPtr, (INT)wParam, (LPINT)lParam);
11131 case LVM_GETCOLUMNWIDTH:
11132 return LISTVIEW_GetColumnWidth(infoPtr, (INT)wParam);
11134 case LVM_GETCOUNTPERPAGE:
11135 return LISTVIEW_GetCountPerPage(infoPtr);
11137 case LVM_GETEDITCONTROL:
11138 return (LRESULT)infoPtr->hwndEdit;
11140 case LVM_GETEXTENDEDLISTVIEWSTYLE:
11141 return infoPtr->dwLvExStyle;
11143 /* case LVM_GETGROUPINFO: */
11145 /* case LVM_GETGROUPMETRICS: */
11147 case LVM_GETHEADER:
11148 return (LRESULT)infoPtr->hwndHeader;
11150 case LVM_GETHOTCURSOR:
11151 return (LRESULT)infoPtr->hHotCursor;
11153 case LVM_GETHOTITEM:
11154 return infoPtr->nHotItem;
11156 case LVM_GETHOVERTIME:
11157 return infoPtr->dwHoverTime;
11159 case LVM_GETIMAGELIST:
11160 return (LRESULT)LISTVIEW_GetImageList(infoPtr, (INT)wParam);
11162 /* case LVM_GETINSERTMARK: */
11164 /* case LVM_GETINSERTMARKCOLOR: */
11166 /* case LVM_GETINSERTMARKRECT: */
11168 case LVM_GETISEARCHSTRINGA:
11169 case LVM_GETISEARCHSTRINGW:
11170 FIXME("LVM_GETISEARCHSTRING: unimplemented\n");
11175 return LISTVIEW_GetItemExtT(infoPtr, (LPLVITEMW)lParam, uMsg == LVM_GETITEMW);
11177 case LVM_GETITEMCOUNT:
11178 return infoPtr->nItemCount;
11180 case LVM_GETITEMPOSITION:
11181 return LISTVIEW_GetItemPosition(infoPtr, (INT)wParam, (LPPOINT)lParam);
11183 case LVM_GETITEMRECT:
11184 return LISTVIEW_GetItemRect(infoPtr, (INT)wParam, (LPRECT)lParam);
11186 case LVM_GETITEMSPACING:
11187 return LISTVIEW_GetItemSpacing(infoPtr, (BOOL)wParam);
11189 case LVM_GETITEMSTATE:
11190 return LISTVIEW_GetItemState(infoPtr, (INT)wParam, (UINT)lParam);
11192 case LVM_GETITEMTEXTA:
11193 case LVM_GETITEMTEXTW:
11194 return LISTVIEW_GetItemTextT(infoPtr, (INT)wParam, (LPLVITEMW)lParam,
11195 uMsg == LVM_GETITEMTEXTW);
11197 case LVM_GETNEXTITEM:
11198 return LISTVIEW_GetNextItem(infoPtr, (INT)wParam, LOWORD(lParam));
11200 case LVM_GETNUMBEROFWORKAREAS:
11201 FIXME("LVM_GETNUMBEROFWORKAREAS: unimplemented\n");
11204 case LVM_GETORIGIN:
11205 if (!lParam) return FALSE;
11206 if (infoPtr->uView == LV_VIEW_DETAILS ||
11207 infoPtr->uView == LV_VIEW_LIST) return FALSE;
11208 LISTVIEW_GetOrigin(infoPtr, (LPPOINT)lParam);
11211 /* case LVM_GETOUTLINECOLOR: */
11213 /* case LVM_GETSELECTEDCOLUMN: */
11215 case LVM_GETSELECTEDCOUNT:
11216 return LISTVIEW_GetSelectedCount(infoPtr);
11218 case LVM_GETSELECTIONMARK:
11219 return infoPtr->nSelectionMark;
11221 case LVM_GETSTRINGWIDTHA:
11222 case LVM_GETSTRINGWIDTHW:
11223 return LISTVIEW_GetStringWidthT(infoPtr, (LPCWSTR)lParam,
11224 uMsg == LVM_GETSTRINGWIDTHW);
11226 case LVM_GETSUBITEMRECT:
11227 return LISTVIEW_GetSubItemRect(infoPtr, (UINT)wParam, (LPRECT)lParam);
11229 case LVM_GETTEXTBKCOLOR:
11230 return infoPtr->clrTextBk;
11232 case LVM_GETTEXTCOLOR:
11233 return infoPtr->clrText;
11235 /* case LVM_GETTILEINFO: */
11237 /* case LVM_GETTILEVIEWINFO: */
11239 case LVM_GETTOOLTIPS:
11240 if( !infoPtr->hwndToolTip )
11241 infoPtr->hwndToolTip = COMCTL32_CreateToolTip( hwnd );
11242 return (LRESULT)infoPtr->hwndToolTip;
11244 case LVM_GETTOPINDEX:
11245 return LISTVIEW_GetTopIndex(infoPtr);
11247 case LVM_GETUNICODEFORMAT:
11248 return (infoPtr->notifyFormat == NFR_UNICODE);
11251 return infoPtr->uView;
11253 case LVM_GETVIEWRECT:
11254 return LISTVIEW_GetViewRect(infoPtr, (LPRECT)lParam);
11256 case LVM_GETWORKAREAS:
11257 FIXME("LVM_GETWORKAREAS: unimplemented\n");
11260 /* case LVM_HASGROUP: */
11263 return LISTVIEW_HitTest(infoPtr, (LPLVHITTESTINFO)lParam, FALSE, TRUE);
11265 case LVM_INSERTCOLUMNA:
11266 case LVM_INSERTCOLUMNW:
11267 return LISTVIEW_InsertColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam,
11268 uMsg == LVM_INSERTCOLUMNW);
11270 /* case LVM_INSERTGROUP: */
11272 /* case LVM_INSERTGROUPSORTED: */
11274 case LVM_INSERTITEMA:
11275 case LVM_INSERTITEMW:
11276 return LISTVIEW_InsertItemT(infoPtr, (LPLVITEMW)lParam, uMsg == LVM_INSERTITEMW);
11278 /* case LVM_INSERTMARKHITTEST: */
11280 /* case LVM_ISGROUPVIEWENABLED: */
11282 case LVM_ISITEMVISIBLE:
11283 return LISTVIEW_IsItemVisible(infoPtr, (INT)wParam);
11285 case LVM_MAPIDTOINDEX:
11286 return LISTVIEW_MapIdToIndex(infoPtr, (UINT)wParam);
11288 case LVM_MAPINDEXTOID:
11289 return LISTVIEW_MapIndexToId(infoPtr, (INT)wParam);
11291 /* case LVM_MOVEGROUP: */
11293 /* case LVM_MOVEITEMTOGROUP: */
11295 case LVM_REDRAWITEMS:
11296 return LISTVIEW_RedrawItems(infoPtr, (INT)wParam, (INT)lParam);
11298 /* case LVM_REMOVEALLGROUPS: */
11300 /* case LVM_REMOVEGROUP: */
11303 return LISTVIEW_Scroll(infoPtr, (INT)wParam, (INT)lParam);
11305 case LVM_SETBKCOLOR:
11306 return LISTVIEW_SetBkColor(infoPtr, (COLORREF)lParam);
11308 /* case LVM_SETBKIMAGE: */
11310 case LVM_SETCALLBACKMASK:
11311 infoPtr->uCallbackMask = (UINT)wParam;
11314 case LVM_SETCOLUMNA:
11315 case LVM_SETCOLUMNW:
11316 return LISTVIEW_SetColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam,
11317 uMsg == LVM_SETCOLUMNW);
11319 case LVM_SETCOLUMNORDERARRAY:
11320 return LISTVIEW_SetColumnOrderArray(infoPtr, (INT)wParam, (LPINT)lParam);
11322 case LVM_SETCOLUMNWIDTH:
11323 return LISTVIEW_SetColumnWidth(infoPtr, (INT)wParam, (short)LOWORD(lParam));
11325 case LVM_SETEXTENDEDLISTVIEWSTYLE:
11326 return LISTVIEW_SetExtendedListViewStyle(infoPtr, (DWORD)wParam, (DWORD)lParam);
11328 /* case LVM_SETGROUPINFO: */
11330 /* case LVM_SETGROUPMETRICS: */
11332 case LVM_SETHOTCURSOR:
11333 return (LRESULT)LISTVIEW_SetHotCursor(infoPtr, (HCURSOR)lParam);
11335 case LVM_SETHOTITEM:
11336 return LISTVIEW_SetHotItem(infoPtr, (INT)wParam);
11338 case LVM_SETHOVERTIME:
11339 return LISTVIEW_SetHoverTime(infoPtr, (DWORD)lParam);
11341 case LVM_SETICONSPACING:
11342 return LISTVIEW_SetIconSpacing(infoPtr, (short)LOWORD(lParam), (short)HIWORD(lParam));
11344 case LVM_SETIMAGELIST:
11345 return (LRESULT)LISTVIEW_SetImageList(infoPtr, (INT)wParam, (HIMAGELIST)lParam);
11347 /* case LVM_SETINFOTIP: */
11349 /* case LVM_SETINSERTMARK: */
11351 /* case LVM_SETINSERTMARKCOLOR: */
11356 if (infoPtr->dwStyle & LVS_OWNERDATA) return FALSE;
11357 return LISTVIEW_SetItemT(infoPtr, (LPLVITEMW)lParam, (uMsg == LVM_SETITEMW));
11360 case LVM_SETITEMCOUNT:
11361 return LISTVIEW_SetItemCount(infoPtr, (INT)wParam, (DWORD)lParam);
11363 case LVM_SETITEMPOSITION:
11366 pt.x = (short)LOWORD(lParam);
11367 pt.y = (short)HIWORD(lParam);
11368 return LISTVIEW_SetItemPosition(infoPtr, (INT)wParam, &pt);
11371 case LVM_SETITEMPOSITION32:
11372 return LISTVIEW_SetItemPosition(infoPtr, (INT)wParam, (POINT*)lParam);
11374 case LVM_SETITEMSTATE:
11375 if (lParam == 0) return FALSE;
11376 return LISTVIEW_SetItemState(infoPtr, (INT)wParam, (LPLVITEMW)lParam);
11378 case LVM_SETITEMTEXTA:
11379 case LVM_SETITEMTEXTW:
11380 return LISTVIEW_SetItemTextT(infoPtr, (INT)wParam, (LPLVITEMW)lParam,
11381 uMsg == LVM_SETITEMTEXTW);
11383 /* case LVM_SETOUTLINECOLOR: */
11385 /* case LVM_SETSELECTEDCOLUMN: */
11387 case LVM_SETSELECTIONMARK:
11388 return LISTVIEW_SetSelectionMark(infoPtr, (INT)lParam);
11390 case LVM_SETTEXTBKCOLOR:
11391 return LISTVIEW_SetTextBkColor(infoPtr, (COLORREF)lParam);
11393 case LVM_SETTEXTCOLOR:
11394 return LISTVIEW_SetTextColor(infoPtr, (COLORREF)lParam);
11396 /* case LVM_SETTILEINFO: */
11398 /* case LVM_SETTILEVIEWINFO: */
11400 /* case LVM_SETTILEWIDTH: */
11402 case LVM_SETTOOLTIPS:
11403 return (LRESULT)LISTVIEW_SetToolTips(infoPtr, (HWND)lParam);
11405 case LVM_SETUNICODEFORMAT:
11406 return LISTVIEW_SetUnicodeFormat(infoPtr, wParam);
11409 return LISTVIEW_SetView(infoPtr, wParam);
11411 /* case LVM_SETWORKAREAS: */
11413 /* case LVM_SORTGROUPS: */
11415 case LVM_SORTITEMS:
11416 case LVM_SORTITEMSEX:
11417 return LISTVIEW_SortItems(infoPtr, (PFNLVCOMPARE)lParam, wParam,
11418 uMsg == LVM_SORTITEMSEX);
11419 case LVM_SUBITEMHITTEST:
11420 return LISTVIEW_HitTest(infoPtr, (LPLVHITTESTINFO)lParam, TRUE, FALSE);
11423 return LISTVIEW_Update(infoPtr, (INT)wParam);
11425 case CCM_GETVERSION:
11426 return LISTVIEW_GetVersion(infoPtr);
11428 case CCM_SETVERSION:
11429 return LISTVIEW_SetVersion(infoPtr, wParam);
11432 return LISTVIEW_ProcessLetterKeys( infoPtr, wParam, lParam );
11435 return LISTVIEW_Command(infoPtr, wParam, lParam);
11438 return LISTVIEW_NCCreate(hwnd, (LPCREATESTRUCTW)lParam);
11441 return LISTVIEW_Create(hwnd, (LPCREATESTRUCTW)lParam);
11444 return LISTVIEW_Destroy(infoPtr);
11447 return LISTVIEW_Enable(infoPtr);
11449 case WM_ERASEBKGND:
11450 return LISTVIEW_EraseBkgnd(infoPtr, (HDC)wParam);
11452 case WM_GETDLGCODE:
11453 return DLGC_WANTCHARS | DLGC_WANTARROWS;
11456 return (LRESULT)infoPtr->hFont;
11459 return LISTVIEW_HScroll(infoPtr, (INT)LOWORD(wParam), 0);
11462 return LISTVIEW_KeyDown(infoPtr, (INT)wParam, (LONG)lParam);
11465 return LISTVIEW_KillFocus(infoPtr);
11467 case WM_LBUTTONDBLCLK:
11468 return LISTVIEW_LButtonDblClk(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11470 case WM_LBUTTONDOWN:
11471 return LISTVIEW_LButtonDown(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11474 return LISTVIEW_LButtonUp(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11477 return LISTVIEW_MouseMove (infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11479 case WM_MOUSEHOVER:
11480 return LISTVIEW_MouseHover(infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11483 return LISTVIEW_NCDestroy(infoPtr);
11486 return LISTVIEW_NCPaint(infoPtr, (HRGN)wParam);
11489 return LISTVIEW_Notify(infoPtr, (LPNMHDR)lParam);
11491 case WM_NOTIFYFORMAT:
11492 return LISTVIEW_NotifyFormat(infoPtr, (HWND)wParam, (INT)lParam);
11494 case WM_PRINTCLIENT:
11495 return LISTVIEW_PrintClient(infoPtr, (HDC)wParam, (DWORD)lParam);
11498 return LISTVIEW_WMPaint(infoPtr, (HDC)wParam);
11500 case WM_RBUTTONDBLCLK:
11501 return LISTVIEW_RButtonDblClk(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11503 case WM_RBUTTONDOWN:
11504 return LISTVIEW_RButtonDown(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11507 return LISTVIEW_RButtonUp(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11510 return LISTVIEW_SetCursor(infoPtr, wParam, lParam);
11513 return LISTVIEW_SetFocus(infoPtr, (HWND)wParam);
11516 return LISTVIEW_SetFont(infoPtr, (HFONT)wParam, (WORD)lParam);
11519 return LISTVIEW_SetRedraw(infoPtr, (BOOL)wParam);
11521 case WM_SHOWWINDOW:
11522 return LISTVIEW_ShowWindow(infoPtr, wParam, lParam);
11525 return LISTVIEW_Size(infoPtr, (short)LOWORD(lParam), (short)HIWORD(lParam));
11527 case WM_STYLECHANGED:
11528 return LISTVIEW_StyleChanged(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
11530 case WM_STYLECHANGING:
11531 return LISTVIEW_StyleChanging(wParam, (LPSTYLESTRUCT)lParam);
11533 case WM_SYSCOLORCHANGE:
11534 COMCTL32_RefreshSysColors();
11537 /* case WM_TIMER: */
11538 case WM_THEMECHANGED:
11539 return LISTVIEW_ThemeChanged(infoPtr);
11542 return LISTVIEW_VScroll(infoPtr, (INT)LOWORD(wParam), 0);
11544 case WM_MOUSEWHEEL:
11545 if (wParam & (MK_SHIFT | MK_CONTROL))
11546 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
11547 return LISTVIEW_MouseWheel(infoPtr, (short int)HIWORD(wParam));
11549 case WM_WINDOWPOSCHANGED:
11550 if (!(((WINDOWPOS *)lParam)->flags & SWP_NOSIZE))
11552 SetWindowPos(infoPtr->hwndSelf, 0, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOACTIVATE |
11553 SWP_NOZORDER | SWP_NOMOVE | SWP_NOSIZE);
11555 if ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (infoPtr->uView == LV_VIEW_DETAILS))
11557 if (notify_measureitem(infoPtr)) LISTVIEW_InvalidateList(infoPtr);
11560 LISTVIEW_UpdateSize(infoPtr);
11561 LISTVIEW_UpdateScroll(infoPtr);
11563 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
11565 /* case WM_WININICHANGE: */
11568 if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
11569 ERR("unknown msg %04x wp=%08lx lp=%08lx\n", uMsg, wParam, lParam);
11571 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
11578 * Registers the window class.
11586 void LISTVIEW_Register(void)
11588 WNDCLASSW wndClass;
11590 ZeroMemory(&wndClass, sizeof(WNDCLASSW));
11591 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS;
11592 wndClass.lpfnWndProc = LISTVIEW_WindowProc;
11593 wndClass.cbClsExtra = 0;
11594 wndClass.cbWndExtra = sizeof(LISTVIEW_INFO *);
11595 wndClass.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
11596 wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
11597 wndClass.lpszClassName = WC_LISTVIEWW;
11598 RegisterClassW(&wndClass);
11603 * Unregisters the window class.
11611 void LISTVIEW_Unregister(void)
11613 UnregisterClassW(WC_LISTVIEWW, NULL);
11618 * Handle any WM_COMMAND messages
11621 * [I] infoPtr : valid pointer to the listview structure
11622 * [I] wParam : the first message parameter
11623 * [I] lParam : the second message parameter
11628 static LRESULT LISTVIEW_Command(LISTVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
11631 TRACE("(%p %x %x %lx)\n", infoPtr, HIWORD(wParam), LOWORD(wParam), lParam);
11633 if (!infoPtr->hwndEdit) return 0;
11635 switch (HIWORD(wParam))
11640 * Adjust the edit window size
11642 WCHAR buffer[1024];
11643 HDC hdc = GetDC(infoPtr->hwndEdit);
11644 HFONT hFont, hOldFont = 0;
11648 if (!infoPtr->hwndEdit || !hdc) return 0;
11649 GetWindowTextW(infoPtr->hwndEdit, buffer, sizeof(buffer)/sizeof(buffer[0]));
11650 GetWindowRect(infoPtr->hwndEdit, &rect);
11652 /* Select font to get the right dimension of the string */
11653 hFont = (HFONT)SendMessageW(infoPtr->hwndEdit, WM_GETFONT, 0, 0);
11656 hOldFont = SelectObject(hdc, hFont);
11659 if (GetTextExtentPoint32W(hdc, buffer, lstrlenW(buffer), &sz))
11661 TEXTMETRICW textMetric;
11663 /* Add Extra spacing for the next character */
11664 GetTextMetricsW(hdc, &textMetric);
11665 sz.cx += (textMetric.tmMaxCharWidth * 2);
11667 SetWindowPos(infoPtr->hwndEdit, NULL, 0, 0, sz.cx,
11668 rect.bottom - rect.top, SWP_DRAWFRAME | SWP_NOMOVE | SWP_NOZORDER);
11671 SelectObject(hdc, hOldFont);
11673 ReleaseDC(infoPtr->hwndEdit, hdc);
11679 LISTVIEW_CancelEditLabel(infoPtr);
11684 return SendMessageW (infoPtr->hwndNotify, WM_COMMAND, wParam, lParam);