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 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)
83 * -- LVS_NOSCROLL (see Q137520)
87 * -- LVS_EX_BORDERSELECT
91 * -- LVS_EX_MULTIWORKAREAS
93 * -- LVS_EX_SIMPLESELECT
94 * -- LVS_EX_TWOCLICKACTIVATE
95 * -- LVS_EX_UNDERLINECOLD
96 * -- LVS_EX_UNDERLINEHOT
99 * -- LVN_BEGINSCROLL, LVN_ENDSCROLL
107 * -- LVM_ENABLEGROUPVIEW
108 * -- LVM_GETBKIMAGE, LVM_SETBKIMAGE
109 * -- LVM_GETGROUPINFO, LVM_SETGROUPINFO
110 * -- LVM_GETGROUPMETRICS, LVM_SETGROUPMETRICS
111 * -- LVM_GETINSERTMARK, LVM_SETINSERTMARK
112 * -- LVM_GETINSERTMARKCOLOR, LVM_SETINSERTMARKCOLOR
113 * -- LVM_GETINSERTMARKRECT
114 * -- LVM_GETNUMBEROFWORKAREAS
115 * -- LVM_GETOUTLINECOLOR, LVM_SETOUTLINECOLOR
116 * -- LVM_GETSELECTEDCOLUMN, LVM_SETSELECTEDCOLUMN
117 * -- LVM_GETISEARCHSTRINGW, LVM_GETISEARCHSTRINGA
118 * -- LVM_GETTILEINFO, LVM_SETTILEINFO
119 * -- LVM_GETTILEVIEWINFO, LVM_SETTILEVIEWINFO
120 * -- LVM_GETWORKAREAS, LVM_SETWORKAREAS
121 * -- LVM_HASGROUP, LVM_INSERTGROUP, LVM_REMOVEGROUP, LVM_REMOVEALLGROUPS
122 * -- LVM_INSERTGROUPSORTED
123 * -- LVM_INSERTMARKHITTEST
124 * -- LVM_ISGROUPVIEWENABLED
126 * -- LVM_MOVEITEMTOGROUP
128 * -- LVM_SETTILEWIDTH
132 * -- ListView_GetHoverTime, ListView_SetHoverTime
133 * -- ListView_GetISearchString
134 * -- ListView_GetNumberOfWorkAreas
135 * -- ListView_GetWorkAreas, ListView_SetWorkAreas
140 * Known differences in message stream from native control (not known if
141 * these differences cause problems):
142 * LVM_INSERTITEM issues LVM_SETITEMSTATE and LVM_SETITEM in certain cases.
143 * LVM_SETITEM does not always issue LVN_ITEMCHANGING/LVN_ITEMCHANGED.
144 * WM_CREATE does not issue WM_QUERYUISTATE and associated registry
145 * processing for "USEDOUBLECLICKTIME".
149 #include "wine/port.h"
164 #include "commctrl.h"
165 #include "comctl32.h"
168 #include "wine/debug.h"
169 #include "wine/unicode.h"
171 WINE_DEFAULT_DEBUG_CHANNEL(listview);
173 /* make sure you set this to 0 for production use! */
174 #define DEBUG_RANGES 1
176 typedef struct tagCOLUMN_INFO
178 RECT rcHeader; /* tracks the header's rectangle */
179 INT fmt; /* same as LVCOLUMN.fmt */
183 typedef struct tagITEMHDR
187 } ITEMHDR, *LPITEMHDR;
189 typedef struct tagSUBITEM_INFO
195 typedef struct tagITEM_ID ITEM_ID;
197 typedef struct tagITEM_INFO
208 UINT id; /* item id */
209 HDPA item; /* link to item data */
212 typedef struct tagRANGE
218 typedef struct tagRANGES
223 typedef struct tagITERATOR
232 typedef struct tagDELAYED_ITEM_EDIT
238 typedef struct tagLISTVIEW_INFO
242 RECT rcList; /* This rectangle is really the window
243 * client rectangle possibly reduced by the
244 * horizontal scroll bar and/or header - see
245 * LISTVIEW_UpdateSize. This rectangle offset
246 * by the LISTVIEW_GetOrigin value is in
247 * client coordinates */
249 /* notification window */
252 BOOL bDoChangeNotify; /* send change notification messages? */
259 INT nItemCount; /* the number of items in the list */
260 HDPA hdpaItems; /* array ITEM_INFO pointers */
261 HDPA hdpaItemIds; /* array of ITEM_ID pointers */
262 HDPA hdpaPosX; /* maintains the (X, Y) coordinates of the */
263 HDPA hdpaPosY; /* items in LVS_ICON, and LVS_SMALLICON modes */
264 RANGES selectionRanges;
265 INT nSelectionMark; /* item to start next multiselection from */
267 BOOL bAutoarrange; /* Autoarrange flag when NOT in LVS_AUTOARRANGE */
270 HDPA hdpaColumns; /* array of COLUMN_INFO pointers */
271 BOOL colRectsDirty; /* trigger column rectangles requery from header */
274 BOOL bNoItemMetrics; /* flags if item metrics are not yet computed */
279 PFNLVCOMPARE pfnCompare; /* sorting callback pointer */
283 DWORD dwStyle; /* the cached window GWL_STYLE */
284 DWORD dwLvExStyle; /* extended listview style */
285 DWORD uView; /* current view available through LVM_[G,S]ETVIEW */
291 DELAYED_ITEM_EDIT itemEdit; /* Pointer to this structure will be the timer ID */
294 HIMAGELIST himlNormal;
295 HIMAGELIST himlSmall;
296 HIMAGELIST himlState;
300 POINT currIconPos; /* this is the position next icon will be placed */
304 INT xTrackLine; /* The x coefficient of the track line or -1 if none */
306 /* marquee selection */
307 BOOL bMarqueeSelect; /* marquee selection/highlight underway */
309 RECT marqueeRect; /* absolute coordinates of marquee selection */
310 RECT marqueeDrawRect; /* relative coordinates for drawing marquee */
311 POINT marqueeOrigin; /* absolute coordinates of marquee click origin */
314 BOOL bFocus; /* control has focus */
316 RECT rcFocus; /* focus bounds */
327 INT ntmHeight; /* Some cached metrics of the font used */
328 INT ntmMaxCharWidth; /* by the listview to draw items */
331 /* mouse operation */
335 POINT ptClickPos; /* point where the user clicked */
336 INT nLButtonDownItem; /* tracks item to reset multiselection on WM_LBUTTONUP */
340 /* keyboard operation */
341 DWORD lastKeyPressTimestamp;
343 INT nSearchParamLength;
344 WCHAR szSearchParam[ MAX_PATH ];
347 DWORD cditemmode; /* Keep the custom draw flags for an item/row */
348 BOOL bIsDrawing; /* Drawing in progress */
349 INT nMeasureItemHeight; /* WM_MEASUREITEM result */
350 BOOL bRedraw; /* WM_SETREDRAW switch */
353 DWORD iVersion; /* CCM_[G,S]ETVERSION */
359 /* How many we debug buffer to allocate */
360 #define DEBUG_BUFFERS 20
361 /* The size of a single debug buffer */
362 #define DEBUG_BUFFER_SIZE 256
364 /* Internal interface to LISTVIEW_HScroll and LISTVIEW_VScroll */
365 #define SB_INTERNAL -1
367 /* maximum size of a label */
368 #define DISP_TEXT_SIZE 512
370 /* padding for items in list and small icon display modes */
371 #define WIDTH_PADDING 12
373 /* padding for items in list, report and small icon display modes */
374 #define HEIGHT_PADDING 1
376 /* offset of items in report display mode */
377 #define REPORT_MARGINX 2
379 /* padding for icon in large icon display mode
380 * ICON_TOP_PADDING_NOTHITABLE - space between top of box and area
381 * that HITTEST will see.
382 * ICON_TOP_PADDING_HITABLE - spacing between above and icon.
383 * ICON_TOP_PADDING - sum of the two above.
384 * ICON_BOTTOM_PADDING - between bottom of icon and top of text
385 * LABEL_HOR_PADDING - between text and sides of box
386 * LABEL_VERT_PADDING - between bottom of text and end of box
388 * ICON_LR_PADDING - additional width above icon size.
389 * ICON_LR_HALF - half of the above value
391 #define ICON_TOP_PADDING_NOTHITABLE 2
392 #define ICON_TOP_PADDING_HITABLE 2
393 #define ICON_TOP_PADDING (ICON_TOP_PADDING_NOTHITABLE + ICON_TOP_PADDING_HITABLE)
394 #define ICON_BOTTOM_PADDING 4
395 #define LABEL_HOR_PADDING 5
396 #define LABEL_VERT_PADDING 7
397 #define ICON_LR_PADDING 16
398 #define ICON_LR_HALF (ICON_LR_PADDING/2)
400 /* default label width for items in list and small icon display modes */
401 #define DEFAULT_LABEL_WIDTH 40
402 /* maximum select rectangle width for empty text item in LV_VIEW_DETAILS */
403 #define MAX_EMPTYTEXT_SELECT_WIDTH 80
405 /* default column width for items in list display mode */
406 #define DEFAULT_COLUMN_WIDTH 128
408 /* Size of "line" scroll for V & H scrolls */
409 #define LISTVIEW_SCROLL_ICON_LINE_SIZE 37
411 /* Padding between image and label */
412 #define IMAGE_PADDING 2
414 /* Padding behind the label */
415 #define TRAILING_LABEL_PADDING 12
416 #define TRAILING_HEADER_PADDING 11
418 /* Border for the icon caption */
419 #define CAPTION_BORDER 2
421 /* Standard DrawText flags */
422 #define LV_ML_DT_FLAGS (DT_TOP | DT_NOPREFIX | DT_EDITCONTROL | DT_CENTER | DT_WORDBREAK | DT_WORD_ELLIPSIS | DT_END_ELLIPSIS)
423 #define LV_FL_DT_FLAGS (DT_TOP | DT_NOPREFIX | DT_EDITCONTROL | DT_CENTER | DT_WORDBREAK | DT_NOCLIP)
424 #define LV_SL_DT_FLAGS (DT_VCENTER | DT_NOPREFIX | DT_EDITCONTROL | DT_SINGLELINE | DT_WORD_ELLIPSIS | DT_END_ELLIPSIS)
426 /* Image index from state */
427 #define STATEIMAGEINDEX(x) (((x) & LVIS_STATEIMAGEMASK) >> 12)
429 /* The time in milliseconds to reset the search in the list */
430 #define KEY_DELAY 450
432 /* Dump the LISTVIEW_INFO structure to the debug channel */
433 #define LISTVIEW_DUMP(iP) do { \
434 TRACE("hwndSelf=%p, clrBk=0x%06x, clrText=0x%06x, clrTextBk=0x%06x, ItemHeight=%d, ItemWidth=%d, Style=0x%08x\n", \
435 iP->hwndSelf, iP->clrBk, iP->clrText, iP->clrTextBk, \
436 iP->nItemHeight, iP->nItemWidth, iP->dwStyle); \
437 TRACE("hwndSelf=%p, himlNor=%p, himlSml=%p, himlState=%p, Focused=%d, Hot=%d, exStyle=0x%08x, Focus=%d\n", \
438 iP->hwndSelf, iP->himlNormal, iP->himlSmall, iP->himlState, \
439 iP->nFocusedItem, iP->nHotItem, iP->dwLvExStyle, iP->bFocus ); \
440 TRACE("hwndSelf=%p, ntmH=%d, icSz.cx=%d, icSz.cy=%d, icSp.cx=%d, icSp.cy=%d, notifyFmt=%d\n", \
441 iP->hwndSelf, iP->ntmHeight, iP->iconSize.cx, iP->iconSize.cy, \
442 iP->iconSpacing.cx, iP->iconSpacing.cy, iP->notifyFormat); \
443 TRACE("hwndSelf=%p, rcList=%s\n", iP->hwndSelf, wine_dbgstr_rect(&iP->rcList)); \
446 static const WCHAR themeClass[] = {'L','i','s','t','V','i','e','w',0};
449 * forward declarations
451 static BOOL LISTVIEW_GetItemT(const LISTVIEW_INFO *, LPLVITEMW, BOOL);
452 static void LISTVIEW_GetItemBox(const LISTVIEW_INFO *, INT, LPRECT);
453 static void LISTVIEW_GetItemOrigin(const LISTVIEW_INFO *, INT, LPPOINT);
454 static BOOL LISTVIEW_GetItemPosition(const LISTVIEW_INFO *, INT, LPPOINT);
455 static BOOL LISTVIEW_GetItemRect(const LISTVIEW_INFO *, INT, LPRECT);
456 static INT LISTVIEW_GetLabelWidth(const LISTVIEW_INFO *, INT);
457 static void LISTVIEW_GetOrigin(const LISTVIEW_INFO *, LPPOINT);
458 static BOOL LISTVIEW_GetViewRect(const LISTVIEW_INFO *, LPRECT);
459 static void LISTVIEW_UpdateSize(LISTVIEW_INFO *);
460 static LRESULT LISTVIEW_Command(LISTVIEW_INFO *, WPARAM, LPARAM);
461 static INT LISTVIEW_GetStringWidthT(const LISTVIEW_INFO *, LPCWSTR, BOOL);
462 static BOOL LISTVIEW_KeySelection(LISTVIEW_INFO *, INT, BOOL);
463 static UINT LISTVIEW_GetItemState(const LISTVIEW_INFO *, INT, UINT);
464 static BOOL LISTVIEW_SetItemState(LISTVIEW_INFO *, INT, const LVITEMW *);
465 static LRESULT LISTVIEW_VScroll(LISTVIEW_INFO *, INT, INT, HWND);
466 static LRESULT LISTVIEW_HScroll(LISTVIEW_INFO *, INT, INT, HWND);
467 static BOOL LISTVIEW_EnsureVisible(LISTVIEW_INFO *, INT, BOOL);
468 static HWND CreateEditLabelT(LISTVIEW_INFO *, LPCWSTR, DWORD, BOOL);
469 static HIMAGELIST LISTVIEW_SetImageList(LISTVIEW_INFO *, INT, HIMAGELIST);
470 static INT LISTVIEW_HitTest(const LISTVIEW_INFO *, LPLVHITTESTINFO, BOOL, BOOL);
471 static BOOL LISTVIEW_EndEditLabelT(LISTVIEW_INFO *, BOOL, BOOL);
472 static BOOL LISTVIEW_Scroll(LISTVIEW_INFO *, INT, INT);
474 /******** Text handling functions *************************************/
476 /* A text pointer is either NULL, LPSTR_TEXTCALLBACK, or points to a
477 * text string. The string may be ANSI or Unicode, in which case
478 * the boolean isW tells us the type of the string.
480 * The name of the function tell what type of strings it expects:
481 * W: Unicode, T: ANSI/Unicode - function of isW
484 static inline BOOL is_textW(LPCWSTR text)
486 return text != NULL && text != LPSTR_TEXTCALLBACKW;
489 static inline BOOL is_textT(LPCWSTR text, BOOL isW)
491 /* we can ignore isW since LPSTR_TEXTCALLBACKW == LPSTR_TEXTCALLBACKA */
492 return is_textW(text);
495 static inline int textlenT(LPCWSTR text, BOOL isW)
497 return !is_textT(text, isW) ? 0 :
498 isW ? lstrlenW(text) : lstrlenA((LPCSTR)text);
501 static inline void textcpynT(LPWSTR dest, BOOL isDestW, LPCWSTR src, BOOL isSrcW, INT max)
504 if (isSrcW) lstrcpynW(dest, src, max);
505 else MultiByteToWideChar(CP_ACP, 0, (LPCSTR)src, -1, dest, max);
507 if (isSrcW) WideCharToMultiByte(CP_ACP, 0, src, -1, (LPSTR)dest, max, NULL, NULL);
508 else lstrcpynA((LPSTR)dest, (LPCSTR)src, max);
511 static inline LPWSTR textdupTtoW(LPCWSTR text, BOOL isW)
513 LPWSTR wstr = (LPWSTR)text;
515 if (!isW && is_textT(text, isW))
517 INT len = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)text, -1, NULL, 0);
518 wstr = Alloc(len * sizeof(WCHAR));
519 if (wstr) MultiByteToWideChar(CP_ACP, 0, (LPCSTR)text, -1, wstr, len);
521 TRACE(" wstr=%s\n", text == LPSTR_TEXTCALLBACKW ? "(callback)" : debugstr_w(wstr));
525 static inline void textfreeT(LPWSTR wstr, BOOL isW)
527 if (!isW && is_textT(wstr, isW)) Free (wstr);
531 * dest is a pointer to a Unicode string
532 * src is a pointer to a string (Unicode if isW, ANSI if !isW)
534 static BOOL textsetptrT(LPWSTR *dest, LPCWSTR src, BOOL isW)
538 if (src == LPSTR_TEXTCALLBACKW)
540 if (is_textW(*dest)) Free(*dest);
541 *dest = LPSTR_TEXTCALLBACKW;
545 LPWSTR pszText = textdupTtoW(src, isW);
546 if (*dest == LPSTR_TEXTCALLBACKW) *dest = NULL;
547 bResult = Str_SetPtrW(dest, pszText);
548 textfreeT(pszText, isW);
554 * compares a Unicode to a Unicode/ANSI text string
556 static inline int textcmpWT(LPCWSTR aw, LPCWSTR bt, BOOL isW)
558 if (!aw) return bt ? -1 : 0;
559 if (!bt) return aw ? 1 : 0;
560 if (aw == LPSTR_TEXTCALLBACKW)
561 return bt == LPSTR_TEXTCALLBACKW ? 1 : -1;
562 if (bt != LPSTR_TEXTCALLBACKW)
564 LPWSTR bw = textdupTtoW(bt, isW);
565 int r = bw ? lstrcmpW(aw, bw) : 1;
573 static inline int lstrncmpiW(LPCWSTR s1, LPCWSTR s2, int n)
577 n = min(min(n, lstrlenW(s1)), lstrlenW(s2));
578 res = CompareStringW(LOCALE_USER_DEFAULT, NORM_IGNORECASE, s1, n, s2, n);
579 return res ? res - sizeof(WCHAR) : res;
582 /******** Debugging functions *****************************************/
584 static inline LPCSTR debugtext_t(LPCWSTR text, BOOL isW)
586 if (text == LPSTR_TEXTCALLBACKW) return "(callback)";
587 return isW ? debugstr_w(text) : debugstr_a((LPCSTR)text);
590 static inline LPCSTR debugtext_tn(LPCWSTR text, BOOL isW, INT n)
592 if (text == LPSTR_TEXTCALLBACKW) return "(callback)";
593 n = min(textlenT(text, isW), n);
594 return isW ? debugstr_wn(text, n) : debugstr_an((LPCSTR)text, n);
597 static char* debug_getbuf(void)
599 static int index = 0;
600 static char buffers[DEBUG_BUFFERS][DEBUG_BUFFER_SIZE];
601 return buffers[index++ % DEBUG_BUFFERS];
604 static inline const char* debugrange(const RANGE *lprng)
606 if (!lprng) return "(null)";
607 return wine_dbg_sprintf("[%d, %d]", lprng->lower, lprng->upper);
610 static const char* debugscrollinfo(const SCROLLINFO *pScrollInfo)
612 char* buf = debug_getbuf(), *text = buf;
613 int len, size = DEBUG_BUFFER_SIZE;
615 if (pScrollInfo == NULL) return "(null)";
616 len = snprintf(buf, size, "{cbSize=%d, ", pScrollInfo->cbSize);
617 if (len == -1) goto end; buf += len; size -= len;
618 if (pScrollInfo->fMask & SIF_RANGE)
619 len = snprintf(buf, size, "nMin=%d, nMax=%d, ", pScrollInfo->nMin, pScrollInfo->nMax);
621 if (len == -1) goto end; buf += len; size -= len;
622 if (pScrollInfo->fMask & SIF_PAGE)
623 len = snprintf(buf, size, "nPage=%u, ", pScrollInfo->nPage);
625 if (len == -1) goto end; buf += len; size -= len;
626 if (pScrollInfo->fMask & SIF_POS)
627 len = snprintf(buf, size, "nPos=%d, ", pScrollInfo->nPos);
629 if (len == -1) goto end; buf += len; size -= len;
630 if (pScrollInfo->fMask & SIF_TRACKPOS)
631 len = snprintf(buf, size, "nTrackPos=%d, ", pScrollInfo->nTrackPos);
633 if (len == -1) goto end; buf += len; size -= len;
636 buf = text + strlen(text);
638 if (buf - text > 2) { buf[-2] = '}'; buf[-1] = 0; }
642 static const char* debugnmlistview(const NMLISTVIEW *plvnm)
644 if (!plvnm) return "(null)";
645 return wine_dbg_sprintf("iItem=%d, iSubItem=%d, uNewState=0x%x,"
646 " uOldState=0x%x, uChanged=0x%x, ptAction=%s, lParam=%ld",
647 plvnm->iItem, plvnm->iSubItem, plvnm->uNewState, plvnm->uOldState,
648 plvnm->uChanged, wine_dbgstr_point(&plvnm->ptAction), plvnm->lParam);
651 static const char* debuglvitem_t(const LVITEMW *lpLVItem, BOOL isW)
653 char* buf = debug_getbuf(), *text = buf;
654 int len, size = DEBUG_BUFFER_SIZE;
656 if (lpLVItem == NULL) return "(null)";
657 len = snprintf(buf, size, "{iItem=%d, iSubItem=%d, ", lpLVItem->iItem, lpLVItem->iSubItem);
658 if (len == -1) goto end; buf += len; size -= len;
659 if (lpLVItem->mask & LVIF_STATE)
660 len = snprintf(buf, size, "state=%x, stateMask=%x, ", lpLVItem->state, lpLVItem->stateMask);
662 if (len == -1) goto end; buf += len; size -= len;
663 if (lpLVItem->mask & LVIF_TEXT)
664 len = snprintf(buf, size, "pszText=%s, cchTextMax=%d, ", debugtext_tn(lpLVItem->pszText, isW, 80), lpLVItem->cchTextMax);
666 if (len == -1) goto end; buf += len; size -= len;
667 if (lpLVItem->mask & LVIF_IMAGE)
668 len = snprintf(buf, size, "iImage=%d, ", lpLVItem->iImage);
670 if (len == -1) goto end; buf += len; size -= len;
671 if (lpLVItem->mask & LVIF_PARAM)
672 len = snprintf(buf, size, "lParam=%lx, ", lpLVItem->lParam);
674 if (len == -1) goto end; buf += len; size -= len;
675 if (lpLVItem->mask & LVIF_INDENT)
676 len = snprintf(buf, size, "iIndent=%d, ", lpLVItem->iIndent);
678 if (len == -1) goto end; buf += len; size -= len;
681 buf = text + strlen(text);
683 if (buf - text > 2) { buf[-2] = '}'; buf[-1] = 0; }
687 static const char* debuglvcolumn_t(const LVCOLUMNW *lpColumn, BOOL isW)
689 char* buf = debug_getbuf(), *text = buf;
690 int len, size = DEBUG_BUFFER_SIZE;
692 if (lpColumn == NULL) return "(null)";
693 len = snprintf(buf, size, "{");
694 if (len == -1) goto end; buf += len; size -= len;
695 if (lpColumn->mask & LVCF_SUBITEM)
696 len = snprintf(buf, size, "iSubItem=%d, ", lpColumn->iSubItem);
698 if (len == -1) goto end; buf += len; size -= len;
699 if (lpColumn->mask & LVCF_FMT)
700 len = snprintf(buf, size, "fmt=%x, ", lpColumn->fmt);
702 if (len == -1) goto end; buf += len; size -= len;
703 if (lpColumn->mask & LVCF_WIDTH)
704 len = snprintf(buf, size, "cx=%d, ", lpColumn->cx);
706 if (len == -1) goto end; buf += len; size -= len;
707 if (lpColumn->mask & LVCF_TEXT)
708 len = snprintf(buf, size, "pszText=%s, cchTextMax=%d, ", debugtext_tn(lpColumn->pszText, isW, 80), lpColumn->cchTextMax);
710 if (len == -1) goto end; buf += len; size -= len;
711 if (lpColumn->mask & LVCF_IMAGE)
712 len = snprintf(buf, size, "iImage=%d, ", lpColumn->iImage);
714 if (len == -1) goto end; buf += len; size -= len;
715 if (lpColumn->mask & LVCF_ORDER)
716 len = snprintf(buf, size, "iOrder=%d, ", lpColumn->iOrder);
718 if (len == -1) goto end; buf += len; size -= len;
721 buf = text + strlen(text);
723 if (buf - text > 2) { buf[-2] = '}'; buf[-1] = 0; }
727 static const char* debuglvhittestinfo(const LVHITTESTINFO *lpht)
729 if (!lpht) return "(null)";
731 return wine_dbg_sprintf("{pt=%s, flags=0x%x, iItem=%d, iSubItem=%d}",
732 wine_dbgstr_point(&lpht->pt), lpht->flags, lpht->iItem, lpht->iSubItem);
735 /* Return the corresponding text for a given scroll value */
736 static inline LPCSTR debugscrollcode(int nScrollCode)
740 case SB_LINELEFT: return "SB_LINELEFT";
741 case SB_LINERIGHT: return "SB_LINERIGHT";
742 case SB_PAGELEFT: return "SB_PAGELEFT";
743 case SB_PAGERIGHT: return "SB_PAGERIGHT";
744 case SB_THUMBPOSITION: return "SB_THUMBPOSITION";
745 case SB_THUMBTRACK: return "SB_THUMBTRACK";
746 case SB_ENDSCROLL: return "SB_ENDSCROLL";
747 case SB_INTERNAL: return "SB_INTERNAL";
748 default: return "unknown";
753 /******** Notification functions ************************************/
755 static int get_ansi_notification(UINT unicodeNotificationCode)
757 switch (unicodeNotificationCode)
759 case LVN_BEGINLABELEDITW: return LVN_BEGINLABELEDITA;
760 case LVN_ENDLABELEDITW: return LVN_ENDLABELEDITA;
761 case LVN_GETDISPINFOW: return LVN_GETDISPINFOA;
762 case LVN_SETDISPINFOW: return LVN_SETDISPINFOA;
763 case LVN_ODFINDITEMW: return LVN_ODFINDITEMA;
764 case LVN_GETINFOTIPW: return LVN_GETINFOTIPA;
765 /* header forwards */
766 case HDN_TRACKW: return HDN_TRACKA;
767 case HDN_ENDTRACKW: return HDN_ENDTRACKA;
768 case HDN_BEGINDRAG: return HDN_BEGINDRAG;
769 case HDN_ENDDRAG: return HDN_ENDDRAG;
770 case HDN_ITEMCHANGINGW: return HDN_ITEMCHANGINGA;
771 case HDN_ITEMCHANGEDW: return HDN_ITEMCHANGEDA;
772 case HDN_ITEMCLICKW: return HDN_ITEMCLICKA;
773 case HDN_DIVIDERDBLCLICKW: return HDN_DIVIDERDBLCLICKA;
775 ERR("unknown notification %x\n", unicodeNotificationCode);
780 /* forwards header notifications to listview parent */
781 static LRESULT notify_forward_header(const LISTVIEW_INFO *infoPtr, const NMHEADERW *lpnmh)
785 HD_TEXTFILTERA textfilter;
786 LPSTR text = NULL, filter = NULL;
789 /* on unicode format exit earlier */
790 if (infoPtr->notifyFormat == NFR_UNICODE)
791 return SendMessageW(infoPtr->hwndNotify, WM_NOTIFY,
792 (WPARAM)lpnmh->hdr.idFrom, (LPARAM)lpnmh);
794 /* header always supplies unicode notifications,
795 all we have to do is to convert strings to ANSI */
796 nmhA = *(NMHEADERA*)lpnmh;
799 hditema = *(HDITEMA*)lpnmh->pitem;
800 nmhA.pitem = &hditema;
801 /* convert item text */
802 if (lpnmh->pitem->mask & HDI_TEXT)
804 hditema.pszText = NULL;
805 Str_SetPtrWtoA(&hditema.pszText, lpnmh->pitem->pszText);
806 text = hditema.pszText;
808 /* convert filter text */
809 if ((lpnmh->pitem->mask & HDI_FILTER) && (lpnmh->pitem->type == HDFT_ISSTRING) &&
810 lpnmh->pitem->pvFilter)
812 hditema.pvFilter = &textfilter;
813 textfilter = *(HD_TEXTFILTERA*)(lpnmh->pitem->pvFilter);
814 textfilter.pszText = NULL;
815 Str_SetPtrWtoA(&textfilter.pszText, ((HD_TEXTFILTERW*)lpnmh->pitem->pvFilter)->pszText);
816 filter = textfilter.pszText;
819 nmhA.hdr.code = get_ansi_notification(lpnmh->hdr.code);
821 ret = SendMessageW(infoPtr->hwndNotify, WM_NOTIFY,
822 (WPARAM)nmhA.hdr.idFrom, (LPARAM)&nmhA);
831 static LRESULT notify_hdr(const LISTVIEW_INFO *infoPtr, INT code, LPNMHDR pnmh)
835 TRACE("(code=%d)\n", code);
837 pnmh->hwndFrom = infoPtr->hwndSelf;
838 pnmh->idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
840 result = SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, pnmh->idFrom, (LPARAM)pnmh);
842 TRACE(" <= %ld\n", result);
847 static inline BOOL notify(const LISTVIEW_INFO *infoPtr, INT code)
850 HWND hwnd = infoPtr->hwndSelf;
851 notify_hdr(infoPtr, code, &nmh);
852 return IsWindow(hwnd);
855 static inline void notify_itemactivate(const LISTVIEW_INFO *infoPtr, const LVHITTESTINFO *htInfo)
866 item.mask = LVIF_PARAM|LVIF_STATE;
867 item.iItem = htInfo->iItem;
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 notificationCode : *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 notificationCode, LPNMLVDISPINFOW pdi, BOOL isW)
938 BOOL bResult = FALSE;
939 BOOL convertToAnsi = FALSE, convertToUnicode = FALSE;
940 INT cchTempBufMax = 0, savCchTextMax = 0;
942 LPWSTR pszTempBuf = NULL, savPszText = NULL;
944 if ((pdi->item.mask & LVIF_TEXT) && is_textT(pdi->item.pszText, isW))
946 convertToAnsi = (isW && infoPtr->notifyFormat == NFR_ANSI);
947 convertToUnicode = (!isW && infoPtr->notifyFormat == NFR_UNICODE);
950 if (convertToAnsi || convertToUnicode)
952 if (notificationCode != LVN_GETDISPINFOW)
954 cchTempBufMax = convertToUnicode ?
955 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pdi->item.pszText, -1, NULL, 0):
956 WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, NULL, 0, NULL, NULL);
960 cchTempBufMax = pdi->item.cchTextMax;
961 *pdi->item.pszText = 0; /* make sure we don't process garbage */
964 pszTempBuf = Alloc( (convertToUnicode ? sizeof(WCHAR) : sizeof(CHAR)) * cchTempBufMax);
965 if (!pszTempBuf) return FALSE;
967 if (convertToUnicode)
968 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pdi->item.pszText, -1,
969 pszTempBuf, cchTempBufMax);
971 WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, (LPSTR) pszTempBuf,
972 cchTempBufMax, NULL, NULL);
974 savCchTextMax = pdi->item.cchTextMax;
975 savPszText = pdi->item.pszText;
976 pdi->item.pszText = pszTempBuf;
977 pdi->item.cchTextMax = cchTempBufMax;
980 if (infoPtr->notifyFormat == NFR_ANSI)
981 realNotifCode = get_ansi_notification(notificationCode);
983 realNotifCode = notificationCode;
984 TRACE(" pdi->item=%s\n", debuglvitem_t(&pdi->item, infoPtr->notifyFormat != NFR_ANSI));
985 bResult = notify_hdr(infoPtr, realNotifCode, &pdi->hdr);
987 if (convertToUnicode || convertToAnsi)
989 if (convertToUnicode) /* note : pointer can be changed by app ! */
990 WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, (LPSTR) savPszText,
991 savCchTextMax, NULL, NULL);
993 MultiByteToWideChar(CP_ACP, 0, (LPSTR) pdi->item.pszText, -1,
994 savPszText, savCchTextMax);
995 pdi->item.pszText = savPszText; /* restores our buffer */
996 pdi->item.cchTextMax = savCchTextMax;
1002 static void customdraw_fill(NMLVCUSTOMDRAW *lpnmlvcd, const LISTVIEW_INFO *infoPtr, HDC hdc,
1003 const RECT *rcBounds, const LVITEMW *lplvItem)
1005 ZeroMemory(lpnmlvcd, sizeof(NMLVCUSTOMDRAW));
1006 lpnmlvcd->nmcd.hdc = hdc;
1007 lpnmlvcd->nmcd.rc = *rcBounds;
1008 lpnmlvcd->clrTextBk = infoPtr->clrTextBk;
1009 lpnmlvcd->clrText = infoPtr->clrText;
1010 if (!lplvItem) return;
1011 lpnmlvcd->nmcd.dwItemSpec = lplvItem->iItem + 1;
1012 lpnmlvcd->iSubItem = lplvItem->iSubItem;
1013 if (lplvItem->state & LVIS_SELECTED) lpnmlvcd->nmcd.uItemState |= CDIS_SELECTED;
1014 if (lplvItem->state & LVIS_FOCUSED) lpnmlvcd->nmcd.uItemState |= CDIS_FOCUS;
1015 if (lplvItem->iItem == infoPtr->nHotItem) lpnmlvcd->nmcd.uItemState |= CDIS_HOT;
1016 lpnmlvcd->nmcd.lItemlParam = lplvItem->lParam;
1019 static inline DWORD notify_customdraw (const LISTVIEW_INFO *infoPtr, DWORD dwDrawStage, NMLVCUSTOMDRAW *lpnmlvcd)
1021 BOOL isForItem = (lpnmlvcd->nmcd.dwItemSpec != 0);
1024 lpnmlvcd->nmcd.dwDrawStage = dwDrawStage;
1025 if (isForItem) lpnmlvcd->nmcd.dwDrawStage |= CDDS_ITEM;
1026 if (lpnmlvcd->iSubItem) lpnmlvcd->nmcd.dwDrawStage |= CDDS_SUBITEM;
1027 if (isForItem) lpnmlvcd->nmcd.dwItemSpec--;
1028 result = notify_hdr(infoPtr, NM_CUSTOMDRAW, &lpnmlvcd->nmcd.hdr);
1029 if (isForItem) lpnmlvcd->nmcd.dwItemSpec++;
1033 static void prepaint_setup (const LISTVIEW_INFO *infoPtr, HDC hdc, NMLVCUSTOMDRAW *lpnmlvcd, BOOL SubItem)
1035 if (lpnmlvcd->clrTextBk == CLR_DEFAULT)
1036 lpnmlvcd->clrTextBk = comctl32_color.clrWindow;
1037 if (lpnmlvcd->clrText == CLR_DEFAULT)
1038 lpnmlvcd->clrText = comctl32_color.clrWindowText;
1040 /* apparently, for selected items, we have to override the returned values */
1043 if (lpnmlvcd->nmcd.uItemState & CDIS_SELECTED)
1045 if (infoPtr->bFocus)
1047 lpnmlvcd->clrTextBk = comctl32_color.clrHighlight;
1048 lpnmlvcd->clrText = comctl32_color.clrHighlightText;
1050 else if (infoPtr->dwStyle & LVS_SHOWSELALWAYS)
1052 lpnmlvcd->clrTextBk = comctl32_color.clr3dFace;
1053 lpnmlvcd->clrText = comctl32_color.clrBtnText;
1058 /* Set the text attributes */
1059 if (lpnmlvcd->clrTextBk != CLR_NONE)
1061 SetBkMode(hdc, OPAQUE);
1062 SetBkColor(hdc,lpnmlvcd->clrTextBk);
1065 SetBkMode(hdc, TRANSPARENT);
1066 SetTextColor(hdc, lpnmlvcd->clrText);
1069 static inline DWORD notify_postpaint (const LISTVIEW_INFO *infoPtr, NMLVCUSTOMDRAW *lpnmlvcd)
1071 return notify_customdraw(infoPtr, CDDS_POSTPAINT, lpnmlvcd);
1074 /* returns TRUE when repaint needed, FALSE otherwise */
1075 static BOOL notify_measureitem(LISTVIEW_INFO *infoPtr)
1077 MEASUREITEMSTRUCT mis;
1078 mis.CtlType = ODT_LISTVIEW;
1079 mis.CtlID = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1083 mis.itemHeight= infoPtr->nItemHeight;
1084 SendMessageW(infoPtr->hwndNotify, WM_MEASUREITEM, mis.CtlID, (LPARAM)&mis);
1085 if (infoPtr->nItemHeight != max(mis.itemHeight, 1))
1087 infoPtr->nMeasureItemHeight = infoPtr->nItemHeight = max(mis.itemHeight, 1);
1093 /******** Item iterator functions **********************************/
1095 static RANGES ranges_create(int count);
1096 static void ranges_destroy(RANGES ranges);
1097 static BOOL ranges_add(RANGES ranges, RANGE range);
1098 static BOOL ranges_del(RANGES ranges, RANGE range);
1099 static void ranges_dump(RANGES ranges);
1101 static inline BOOL ranges_additem(RANGES ranges, INT nItem)
1103 RANGE range = { nItem, nItem + 1 };
1105 return ranges_add(ranges, range);
1108 static inline BOOL ranges_delitem(RANGES ranges, INT nItem)
1110 RANGE range = { nItem, nItem + 1 };
1112 return ranges_del(ranges, range);
1116 * ITERATOR DOCUMENTATION
1118 * The iterator functions allow for easy, and convenient iteration
1119 * over items of interest in the list. Typically, you create a
1120 * iterator, use it, and destroy it, as such:
1123 * iterator_xxxitems(&i, ...);
1124 * while (iterator_{prev,next}(&i)
1126 * //code which uses i.nItem
1128 * iterator_destroy(&i);
1130 * where xxx is either: framed, or visible.
1131 * Note that it is important that the code destroys the iterator
1132 * after it's done with it, as the creation of the iterator may
1133 * allocate memory, which thus needs to be freed.
1135 * You can iterate both forwards, and backwards through the list,
1136 * by using iterator_next or iterator_prev respectively.
1138 * Lower numbered items are draw on top of higher number items in
1139 * LVS_ICON, and LVS_SMALLICON (which are the only modes where
1140 * items may overlap). So, to test items, you should use
1142 * which lists the items top to bottom (in Z-order).
1143 * For drawing items, you should use
1145 * which lists the items bottom to top (in Z-order).
1146 * If you keep iterating over the items after the end-of-items
1147 * marker (-1) is returned, the iterator will start from the
1148 * beginning. Typically, you don't need to test for -1,
1149 * because iterator_{next,prev} will return TRUE if more items
1150 * are to be iterated over, or FALSE otherwise.
1152 * Note: the iterator is defined to be bidirectional. That is,
1153 * any number of prev followed by any number of next, or
1154 * five versa, should leave the iterator at the same item:
1155 * prev * n, next * n = next * n, prev * n
1157 * The iterator has a notion of an out-of-order, special item,
1158 * which sits at the start of the list. This is used in
1159 * LVS_ICON, and LVS_SMALLICON mode to handle the focused item,
1160 * which needs to be first, as it may overlap other items.
1162 * The code is a bit messy because we have:
1163 * - a special item to deal with
1164 * - simple range, or composite range
1166 * If you find bugs, or want to add features, please make sure you
1167 * always check/modify *both* iterator_prev, and iterator_next.
1171 * This function iterates through the items in increasing order,
1172 * but prefixed by the special item, then -1. That is:
1173 * special, 1, 2, 3, ..., n, -1.
1174 * Each item is listed only once.
1176 static inline BOOL iterator_next(ITERATOR* i)
1180 i->nItem = i->nSpecial;
1181 if (i->nItem != -1) return TRUE;
1183 if (i->nItem == i->nSpecial)
1185 if (i->ranges) i->index = 0;
1191 if (i->nItem == i->nSpecial) i->nItem++;
1192 if (i->nItem < i->range.upper) return TRUE;
1197 if (i->index < DPA_GetPtrCount(i->ranges->hdpa))
1198 i->range = *(RANGE*)DPA_GetPtr(i->ranges->hdpa, i->index++);
1201 else if (i->nItem >= i->range.upper) goto end;
1203 i->nItem = i->range.lower;
1204 if (i->nItem >= 0) goto testitem;
1211 * This function iterates through the items in decreasing order,
1212 * followed by the special item, then -1. That is:
1213 * n, n-1, ..., 3, 2, 1, special, -1.
1214 * Each item is listed only once.
1216 static inline BOOL iterator_prev(ITERATOR* i)
1223 if (i->ranges) i->index = DPA_GetPtrCount(i->ranges->hdpa);
1226 if (i->nItem == i->nSpecial)
1234 if (i->nItem == i->nSpecial) i->nItem--;
1235 if (i->nItem >= i->range.lower) return TRUE;
1241 i->range = *(RANGE*)DPA_GetPtr(i->ranges->hdpa, --i->index);
1244 else if (!start && i->nItem < i->range.lower) goto end;
1246 i->nItem = i->range.upper;
1247 if (i->nItem > 0) goto testitem;
1249 return (i->nItem = i->nSpecial) != -1;
1252 static RANGE iterator_range(const ITERATOR *i)
1256 if (!i->ranges) return i->range;
1258 if (DPA_GetPtrCount(i->ranges->hdpa) > 0)
1260 range.lower = (*(RANGE*)DPA_GetPtr(i->ranges->hdpa, 0)).lower;
1261 range.upper = (*(RANGE*)DPA_GetPtr(i->ranges->hdpa, DPA_GetPtrCount(i->ranges->hdpa) - 1)).upper;
1263 else range.lower = range.upper = 0;
1269 * Releases resources associated with this ierator.
1271 static inline void iterator_destroy(const ITERATOR *i)
1273 ranges_destroy(i->ranges);
1277 * Create an empty iterator.
1279 static inline BOOL iterator_empty(ITERATOR* i)
1281 ZeroMemory(i, sizeof(*i));
1282 i->nItem = i->nSpecial = i->range.lower = i->range.upper = -1;
1287 * Create an iterator over a range.
1289 static inline BOOL iterator_rangeitems(ITERATOR* i, RANGE range)
1297 * Create an iterator over a bunch of ranges.
1298 * Please note that the iterator will take ownership of the ranges,
1299 * and will free them upon destruction.
1301 static inline BOOL iterator_rangesitems(ITERATOR* i, RANGES ranges)
1309 * Creates an iterator over the items which intersect frame.
1310 * Uses absolute coordinates rather than compensating for the current offset.
1312 static BOOL iterator_frameditems_absolute(ITERATOR* i, const LISTVIEW_INFO* infoPtr, const RECT *frame)
1314 RECT rcItem, rcTemp;
1316 /* in case we fail, we want to return an empty iterator */
1317 if (!iterator_empty(i)) return FALSE;
1319 TRACE("(frame=%s)\n", wine_dbgstr_rect(frame));
1321 if (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON)
1325 if (infoPtr->uView == LV_VIEW_ICON && infoPtr->nFocusedItem != -1)
1327 LISTVIEW_GetItemBox(infoPtr, infoPtr->nFocusedItem, &rcItem);
1328 if (IntersectRect(&rcTemp, &rcItem, frame))
1329 i->nSpecial = infoPtr->nFocusedItem;
1331 if (!(iterator_rangesitems(i, ranges_create(50)))) return FALSE;
1332 /* to do better here, we need to have PosX, and PosY sorted */
1333 TRACE("building icon ranges:\n");
1334 for (nItem = 0; nItem < infoPtr->nItemCount; nItem++)
1336 rcItem.left = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosX, nItem);
1337 rcItem.top = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosY, nItem);
1338 rcItem.right = rcItem.left + infoPtr->nItemWidth;
1339 rcItem.bottom = rcItem.top + infoPtr->nItemHeight;
1340 if (IntersectRect(&rcTemp, &rcItem, frame))
1341 ranges_additem(i->ranges, nItem);
1345 else if (infoPtr->uView == LV_VIEW_DETAILS)
1349 if (frame->left >= infoPtr->nItemWidth) return TRUE;
1350 if (frame->top >= infoPtr->nItemHeight * infoPtr->nItemCount) return TRUE;
1352 range.lower = max(frame->top / infoPtr->nItemHeight, 0);
1353 range.upper = min((frame->bottom - 1) / infoPtr->nItemHeight, infoPtr->nItemCount - 1) + 1;
1354 if (range.upper <= range.lower) return TRUE;
1355 if (!iterator_rangeitems(i, range)) return FALSE;
1356 TRACE(" report=%s\n", debugrange(&i->range));
1360 INT nPerCol = max((infoPtr->rcList.bottom - infoPtr->rcList.top) / infoPtr->nItemHeight, 1);
1361 INT nFirstRow = max(frame->top / infoPtr->nItemHeight, 0);
1362 INT nLastRow = min((frame->bottom - 1) / infoPtr->nItemHeight, nPerCol - 1);
1369 if (infoPtr->nItemWidth)
1371 nFirstCol = max(frame->left / infoPtr->nItemWidth, 0);
1372 nLastCol = min((frame->right - 1) / infoPtr->nItemWidth, (infoPtr->nItemCount + nPerCol - 1) / nPerCol);
1376 nFirstCol = max(frame->left, 0);
1377 nLastCol = min(frame->right - 1, (infoPtr->nItemCount + nPerCol - 1) / nPerCol);
1380 lower = nFirstCol * nPerCol + nFirstRow;
1382 TRACE("nPerCol=%d, nFirstRow=%d, nLastRow=%d, nFirstCol=%d, nLastCol=%d, lower=%d\n",
1383 nPerCol, nFirstRow, nLastRow, nFirstCol, nLastCol, lower);
1385 if (nLastCol < nFirstCol || nLastRow < nFirstRow) return TRUE;
1387 if (!(iterator_rangesitems(i, ranges_create(nLastCol - nFirstCol + 1)))) return FALSE;
1388 TRACE("building list ranges:\n");
1389 for (nCol = nFirstCol; nCol <= nLastCol; nCol++)
1391 item_range.lower = nCol * nPerCol + nFirstRow;
1392 if(item_range.lower >= infoPtr->nItemCount) break;
1393 item_range.upper = min(nCol * nPerCol + nLastRow + 1, infoPtr->nItemCount);
1394 TRACE(" list=%s\n", debugrange(&item_range));
1395 ranges_add(i->ranges, item_range);
1403 * Creates an iterator over the items which intersect lprc.
1405 static BOOL iterator_frameditems(ITERATOR* i, const LISTVIEW_INFO* infoPtr, const RECT *lprc)
1410 TRACE("(lprc=%s)\n", wine_dbgstr_rect(lprc));
1412 LISTVIEW_GetOrigin(infoPtr, &Origin);
1413 OffsetRect(&frame, -Origin.x, -Origin.y);
1415 return iterator_frameditems_absolute(i, infoPtr, &frame);
1419 * Creates an iterator over the items which intersect the visible region of hdc.
1421 static BOOL iterator_visibleitems(ITERATOR *i, const LISTVIEW_INFO *infoPtr, HDC hdc)
1423 POINT Origin, Position;
1424 RECT rcItem, rcClip;
1427 rgntype = GetClipBox(hdc, &rcClip);
1428 if (rgntype == NULLREGION) return iterator_empty(i);
1429 if (!iterator_frameditems(i, infoPtr, &rcClip)) return FALSE;
1430 if (rgntype == SIMPLEREGION) return TRUE;
1432 /* first deal with the special item */
1433 if (i->nSpecial != -1)
1435 LISTVIEW_GetItemBox(infoPtr, i->nSpecial, &rcItem);
1436 if (!RectVisible(hdc, &rcItem)) i->nSpecial = -1;
1439 /* if we can't deal with the region, we'll just go with the simple range */
1440 LISTVIEW_GetOrigin(infoPtr, &Origin);
1441 TRACE("building visible range:\n");
1442 if (!i->ranges && i->range.lower < i->range.upper)
1444 if (!(i->ranges = ranges_create(50))) return TRUE;
1445 if (!ranges_add(i->ranges, i->range))
1447 ranges_destroy(i->ranges);
1453 /* now delete the invisible items from the list */
1454 while(iterator_next(i))
1456 LISTVIEW_GetItemOrigin(infoPtr, i->nItem, &Position);
1457 rcItem.left = (infoPtr->uView == LV_VIEW_DETAILS) ? Origin.x : Position.x + Origin.x;
1458 rcItem.top = Position.y + Origin.y;
1459 rcItem.right = rcItem.left + infoPtr->nItemWidth;
1460 rcItem.bottom = rcItem.top + infoPtr->nItemHeight;
1461 if (!RectVisible(hdc, &rcItem))
1462 ranges_delitem(i->ranges, i->nItem);
1464 /* the iterator should restart on the next iterator_next */
1470 /******** Misc helper functions ************************************/
1472 static inline LRESULT CallWindowProcT(WNDPROC proc, HWND hwnd, UINT uMsg,
1473 WPARAM wParam, LPARAM lParam, BOOL isW)
1475 if (isW) return CallWindowProcW(proc, hwnd, uMsg, wParam, lParam);
1476 else return CallWindowProcA(proc, hwnd, uMsg, wParam, lParam);
1479 static inline BOOL is_autoarrange(const LISTVIEW_INFO *infoPtr)
1481 return ((infoPtr->dwStyle & LVS_AUTOARRANGE) || infoPtr->bAutoarrange) &&
1482 (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON);
1485 static void toggle_checkbox_state(LISTVIEW_INFO *infoPtr, INT nItem)
1487 DWORD state = STATEIMAGEINDEX(LISTVIEW_GetItemState(infoPtr, nItem, LVIS_STATEIMAGEMASK));
1488 if(state == 1 || state == 2)
1492 lvitem.state = INDEXTOSTATEIMAGEMASK(state);
1493 lvitem.stateMask = LVIS_STATEIMAGEMASK;
1494 LISTVIEW_SetItemState(infoPtr, nItem, &lvitem);
1498 /* this should be called after window style got updated,
1499 it used to reset view state to match current window style */
1500 static inline void map_style_view(LISTVIEW_INFO *infoPtr)
1502 switch (infoPtr->dwStyle & LVS_TYPEMASK)
1505 infoPtr->uView = LV_VIEW_ICON;
1508 infoPtr->uView = LV_VIEW_DETAILS;
1511 infoPtr->uView = LV_VIEW_SMALLICON;
1514 infoPtr->uView = LV_VIEW_LIST;
1518 /* computes next item id value */
1519 static DWORD get_next_itemid(const LISTVIEW_INFO *infoPtr)
1521 INT count = DPA_GetPtrCount(infoPtr->hdpaItemIds);
1525 ITEM_ID *lpID = DPA_GetPtr(infoPtr->hdpaItemIds, count - 1);
1526 return lpID->id + 1;
1531 /******** Internal API functions ************************************/
1533 static inline COLUMN_INFO * LISTVIEW_GetColumnInfo(const LISTVIEW_INFO *infoPtr, INT nSubItem)
1535 static COLUMN_INFO mainItem;
1537 if (nSubItem == 0 && DPA_GetPtrCount(infoPtr->hdpaColumns) == 0) return &mainItem;
1538 assert (nSubItem >= 0 && nSubItem < DPA_GetPtrCount(infoPtr->hdpaColumns));
1540 /* update cached column rectangles */
1541 if (infoPtr->colRectsDirty)
1544 LISTVIEW_INFO *Ptr = (LISTVIEW_INFO*)infoPtr;
1547 for (i = 0; i < DPA_GetPtrCount(infoPtr->hdpaColumns); i++) {
1548 info = DPA_GetPtr(infoPtr->hdpaColumns, i);
1549 SendMessageW(infoPtr->hwndHeader, HDM_GETITEMRECT, i, (LPARAM)&info->rcHeader);
1551 Ptr->colRectsDirty = FALSE;
1554 return DPA_GetPtr(infoPtr->hdpaColumns, nSubItem);
1557 static INT LISTVIEW_CreateHeader(LISTVIEW_INFO *infoPtr)
1559 DWORD dFlags = WS_CHILD | HDS_HORZ | HDS_FULLDRAG | HDS_DRAGDROP;
1562 if (infoPtr->hwndHeader) return 0;
1564 TRACE("Creating header for list %p\n", infoPtr->hwndSelf);
1566 /* setup creation flags */
1567 dFlags |= (LVS_NOSORTHEADER & infoPtr->dwStyle) ? 0 : HDS_BUTTONS;
1568 dFlags |= (LVS_NOCOLUMNHEADER & infoPtr->dwStyle) ? HDS_HIDDEN : 0;
1570 hInst = (HINSTANCE)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_HINSTANCE);
1573 infoPtr->hwndHeader = CreateWindowW(WC_HEADERW, NULL, dFlags,
1574 0, 0, 0, 0, infoPtr->hwndSelf, NULL, hInst, NULL);
1575 if (!infoPtr->hwndHeader) return -1;
1577 /* set header unicode format */
1578 SendMessageW(infoPtr->hwndHeader, HDM_SETUNICODEFORMAT, TRUE, 0);
1580 /* set header font */
1581 SendMessageW(infoPtr->hwndHeader, WM_SETFONT, (WPARAM)infoPtr->hFont, (LPARAM)TRUE);
1583 LISTVIEW_UpdateSize(infoPtr);
1588 static inline void LISTVIEW_GetHeaderRect(const LISTVIEW_INFO *infoPtr, INT nSubItem, LPRECT lprc)
1590 *lprc = LISTVIEW_GetColumnInfo(infoPtr, nSubItem)->rcHeader;
1593 static inline BOOL LISTVIEW_GetItemW(const LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem)
1595 return LISTVIEW_GetItemT(infoPtr, lpLVItem, TRUE);
1598 /* used to handle collapse main item column case */
1599 static inline BOOL LISTVIEW_DrawFocusRect(const LISTVIEW_INFO *infoPtr, HDC hdc)
1601 return (infoPtr->rcFocus.left < infoPtr->rcFocus.right) ?
1602 DrawFocusRect(hdc, &infoPtr->rcFocus) : FALSE;
1605 /* Listview invalidation functions: use _only_ these functions to invalidate */
1607 static inline BOOL is_redrawing(const LISTVIEW_INFO *infoPtr)
1609 return infoPtr->bRedraw;
1612 static inline void LISTVIEW_InvalidateRect(const LISTVIEW_INFO *infoPtr, const RECT* rect)
1614 if(!is_redrawing(infoPtr)) return;
1615 TRACE(" invalidating rect=%s\n", wine_dbgstr_rect(rect));
1616 InvalidateRect(infoPtr->hwndSelf, rect, TRUE);
1619 static inline void LISTVIEW_InvalidateItem(const LISTVIEW_INFO *infoPtr, INT nItem)
1623 if(!is_redrawing(infoPtr)) return;
1624 LISTVIEW_GetItemBox(infoPtr, nItem, &rcBox);
1625 LISTVIEW_InvalidateRect(infoPtr, &rcBox);
1628 static inline void LISTVIEW_InvalidateSubItem(const LISTVIEW_INFO *infoPtr, INT nItem, INT nSubItem)
1630 POINT Origin, Position;
1633 if(!is_redrawing(infoPtr)) return;
1634 assert (infoPtr->uView == LV_VIEW_DETAILS);
1635 LISTVIEW_GetOrigin(infoPtr, &Origin);
1636 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
1637 LISTVIEW_GetHeaderRect(infoPtr, nSubItem, &rcBox);
1639 rcBox.bottom = infoPtr->nItemHeight;
1640 OffsetRect(&rcBox, Origin.x + Position.x, Origin.y + Position.y);
1641 LISTVIEW_InvalidateRect(infoPtr, &rcBox);
1644 static inline void LISTVIEW_InvalidateList(const LISTVIEW_INFO *infoPtr)
1646 LISTVIEW_InvalidateRect(infoPtr, NULL);
1649 static inline void LISTVIEW_InvalidateColumn(const LISTVIEW_INFO *infoPtr, INT nColumn)
1653 if(!is_redrawing(infoPtr)) return;
1654 LISTVIEW_GetHeaderRect(infoPtr, nColumn, &rcCol);
1655 rcCol.top = infoPtr->rcList.top;
1656 rcCol.bottom = infoPtr->rcList.bottom;
1657 LISTVIEW_InvalidateRect(infoPtr, &rcCol);
1662 * Retrieves the number of items that can fit vertically in the client area.
1665 * [I] infoPtr : valid pointer to the listview structure
1668 * Number of items per row.
1670 static inline INT LISTVIEW_GetCountPerRow(const LISTVIEW_INFO *infoPtr)
1672 INT nListWidth = infoPtr->rcList.right - infoPtr->rcList.left;
1674 return max(nListWidth/(infoPtr->nItemWidth ? infoPtr->nItemWidth : 1), 1);
1679 * Retrieves the number of items that can fit horizontally in the client
1683 * [I] infoPtr : valid pointer to the listview structure
1686 * Number of items per column.
1688 static inline INT LISTVIEW_GetCountPerColumn(const LISTVIEW_INFO *infoPtr)
1690 INT nListHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
1692 return max(nListHeight / infoPtr->nItemHeight, 1);
1696 /*************************************************************************
1697 * LISTVIEW_ProcessLetterKeys
1699 * Processes keyboard messages generated by pressing the letter keys
1701 * What this does is perform a case insensitive search from the
1702 * current position with the following quirks:
1703 * - If two chars or more are pressed in quick succession we search
1704 * for the corresponding string (e.g. 'abc').
1705 * - If there is a delay we wipe away the current search string and
1706 * restart with just that char.
1707 * - If the user keeps pressing the same character, whether slowly or
1708 * fast, so that the search string is entirely composed of this
1709 * character ('aaaaa' for instance), then we search for first item
1710 * that starting with that character.
1711 * - If the user types the above character in quick succession, then
1712 * we must also search for the corresponding string ('aaaaa'), and
1713 * go to that string if there is a match.
1716 * [I] hwnd : handle to the window
1717 * [I] charCode : the character code, the actual character
1718 * [I] keyData : key data
1726 * - The current implementation has a list of characters it will
1727 * accept and it ignores everything else. In particular it will
1728 * ignore accentuated characters which seems to match what
1729 * Windows does. But I'm not sure it makes sense to follow
1731 * - We don't sound a beep when the search fails.
1735 * TREEVIEW_ProcessLetterKeys
1737 static INT LISTVIEW_ProcessLetterKeys(LISTVIEW_INFO *infoPtr, WPARAM charCode, LPARAM keyData)
1742 WCHAR buffer[MAX_PATH];
1743 DWORD lastKeyPressTimestamp = infoPtr->lastKeyPressTimestamp;
1745 /* simple parameter checking */
1746 if (!charCode || !keyData) return 0;
1748 /* only allow the valid WM_CHARs through */
1749 if (!isalnumW(charCode) &&
1750 charCode != '.' && charCode != '`' && charCode != '!' &&
1751 charCode != '@' && charCode != '#' && charCode != '$' &&
1752 charCode != '%' && charCode != '^' && charCode != '&' &&
1753 charCode != '*' && charCode != '(' && charCode != ')' &&
1754 charCode != '-' && charCode != '_' && charCode != '+' &&
1755 charCode != '=' && charCode != '\\'&& charCode != ']' &&
1756 charCode != '}' && charCode != '[' && charCode != '{' &&
1757 charCode != '/' && charCode != '?' && charCode != '>' &&
1758 charCode != '<' && charCode != ',' && charCode != '~')
1761 /* if there's one item or less, there is no where to go */
1762 if (infoPtr->nItemCount <= 1) return 0;
1764 /* update the search parameters */
1765 infoPtr->lastKeyPressTimestamp = GetTickCount();
1766 if (infoPtr->lastKeyPressTimestamp - lastKeyPressTimestamp < KEY_DELAY) {
1767 if (infoPtr->nSearchParamLength < MAX_PATH-1)
1768 infoPtr->szSearchParam[infoPtr->nSearchParamLength++]=charCode;
1769 if (infoPtr->charCode != charCode)
1770 infoPtr->charCode = charCode = 0;
1772 infoPtr->charCode=charCode;
1773 infoPtr->szSearchParam[0]=charCode;
1774 infoPtr->nSearchParamLength=1;
1775 /* Redundant with the 1 char string */
1779 /* and search from the current position */
1781 if (infoPtr->nFocusedItem >= 0) {
1782 endidx=infoPtr->nFocusedItem;
1784 /* if looking for single character match,
1785 * then we must always move forward
1787 if (infoPtr->nSearchParamLength == 1)
1790 endidx=infoPtr->nItemCount;
1794 /* Let application handle this for virtual listview */
1795 if (infoPtr->dwStyle & LVS_OWNERDATA)
1800 ZeroMemory(&lvfi, sizeof(lvfi));
1801 lvfi.flags = (LVFI_WRAP | LVFI_PARTIAL);
1802 infoPtr->szSearchParam[infoPtr->nSearchParamLength] = '\0';
1803 lvfi.psz = infoPtr->szSearchParam;
1807 nItem = notify_hdr(infoPtr, LVN_ODFINDITEMW, (LPNMHDR)&nmlv.hdr);
1810 LISTVIEW_KeySelection(infoPtr, nItem, FALSE);
1816 if (idx == infoPtr->nItemCount) {
1817 if (endidx == infoPtr->nItemCount || endidx == 0)
1823 item.mask = LVIF_TEXT;
1826 item.pszText = buffer;
1827 item.cchTextMax = MAX_PATH;
1828 if (!LISTVIEW_GetItemW(infoPtr, &item)) return 0;
1830 /* check for a match */
1831 if (lstrncmpiW(item.pszText,infoPtr->szSearchParam,infoPtr->nSearchParamLength) == 0) {
1834 } else if ( (charCode != 0) && (nItem == -1) && (nItem != infoPtr->nFocusedItem) &&
1835 (lstrncmpiW(item.pszText,infoPtr->szSearchParam,1) == 0) ) {
1836 /* This would work but we must keep looking for a longer match */
1840 } while (idx != endidx);
1843 LISTVIEW_KeySelection(infoPtr, nItem, FALSE);
1848 /*************************************************************************
1849 * LISTVIEW_UpdateHeaderSize [Internal]
1851 * Function to resize the header control
1854 * [I] hwnd : handle to a window
1855 * [I] nNewScrollPos : scroll pos to set
1860 static void LISTVIEW_UpdateHeaderSize(const LISTVIEW_INFO *infoPtr, INT nNewScrollPos)
1865 TRACE("nNewScrollPos=%d\n", nNewScrollPos);
1867 if (!infoPtr->hwndHeader) return;
1869 GetWindowRect(infoPtr->hwndHeader, &winRect);
1870 point[0].x = winRect.left;
1871 point[0].y = winRect.top;
1872 point[1].x = winRect.right;
1873 point[1].y = winRect.bottom;
1875 MapWindowPoints(HWND_DESKTOP, infoPtr->hwndSelf, point, 2);
1876 point[0].x = -nNewScrollPos;
1877 point[1].x += nNewScrollPos;
1879 SetWindowPos(infoPtr->hwndHeader,0,
1880 point[0].x,point[0].y,point[1].x,point[1].y,
1881 (infoPtr->dwStyle & LVS_NOCOLUMNHEADER) ? SWP_HIDEWINDOW : SWP_SHOWWINDOW |
1882 SWP_NOZORDER | SWP_NOACTIVATE);
1887 * Update the scrollbars. This functions should be called whenever
1888 * the content, size or view changes.
1891 * [I] infoPtr : valid pointer to the listview structure
1896 static void LISTVIEW_UpdateScroll(const LISTVIEW_INFO *infoPtr)
1898 SCROLLINFO horzInfo, vertInfo;
1901 if ((infoPtr->dwStyle & LVS_NOSCROLL) || !is_redrawing(infoPtr)) return;
1903 ZeroMemory(&horzInfo, sizeof(SCROLLINFO));
1904 horzInfo.cbSize = sizeof(SCROLLINFO);
1905 horzInfo.nPage = infoPtr->rcList.right - infoPtr->rcList.left;
1907 /* for now, we'll set info.nMax to the _count_, and adjust it later */
1908 if (infoPtr->uView == LV_VIEW_LIST)
1910 INT nPerCol = LISTVIEW_GetCountPerColumn(infoPtr);
1911 horzInfo.nMax = (infoPtr->nItemCount + nPerCol - 1) / nPerCol;
1913 /* scroll by at least one column per page */
1914 if(horzInfo.nPage < infoPtr->nItemWidth)
1915 horzInfo.nPage = infoPtr->nItemWidth;
1917 if (infoPtr->nItemWidth)
1918 horzInfo.nPage /= infoPtr->nItemWidth;
1920 else if (infoPtr->uView == LV_VIEW_DETAILS)
1922 horzInfo.nMax = infoPtr->nItemWidth;
1924 else /* LV_VIEW_ICON, or LV_VIEW_SMALLICON */
1928 if (LISTVIEW_GetViewRect(infoPtr, &rcView)) horzInfo.nMax = rcView.right - rcView.left;
1931 horzInfo.fMask = SIF_RANGE | SIF_PAGE;
1932 horzInfo.nMax = max(horzInfo.nMax - 1, 0);
1933 dx = GetScrollPos(infoPtr->hwndSelf, SB_HORZ);
1934 dx -= SetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &horzInfo, TRUE);
1935 TRACE("horzInfo=%s\n", debugscrollinfo(&horzInfo));
1937 /* Setting the horizontal scroll can change the listview size
1938 * (and potentially everything else) so we need to recompute
1939 * everything again for the vertical scroll
1942 ZeroMemory(&vertInfo, sizeof(SCROLLINFO));
1943 vertInfo.cbSize = sizeof(SCROLLINFO);
1944 vertInfo.nPage = infoPtr->rcList.bottom - infoPtr->rcList.top;
1946 if (infoPtr->uView == LV_VIEW_DETAILS)
1948 vertInfo.nMax = infoPtr->nItemCount;
1950 /* scroll by at least one page */
1951 if(vertInfo.nPage < infoPtr->nItemHeight)
1952 vertInfo.nPage = infoPtr->nItemHeight;
1954 if (infoPtr->nItemHeight > 0)
1955 vertInfo.nPage /= infoPtr->nItemHeight;
1957 else if (infoPtr->uView != LV_VIEW_LIST) /* LV_VIEW_ICON, or LV_VIEW_SMALLICON */
1961 if (LISTVIEW_GetViewRect(infoPtr, &rcView)) vertInfo.nMax = rcView.bottom - rcView.top;
1964 vertInfo.fMask = SIF_RANGE | SIF_PAGE;
1965 vertInfo.nMax = max(vertInfo.nMax - 1, 0);
1966 dy = GetScrollPos(infoPtr->hwndSelf, SB_VERT);
1967 dy -= SetScrollInfo(infoPtr->hwndSelf, SB_VERT, &vertInfo, TRUE);
1968 TRACE("vertInfo=%s\n", debugscrollinfo(&vertInfo));
1970 /* Change of the range may have changed the scroll pos. If so move the content */
1971 if (dx != 0 || dy != 0)
1974 listRect = infoPtr->rcList;
1975 ScrollWindowEx(infoPtr->hwndSelf, dx, dy, &listRect, &listRect, 0, 0,
1976 SW_ERASE | SW_INVALIDATE);
1979 /* Update the Header Control */
1980 if (infoPtr->uView == LV_VIEW_DETAILS)
1982 horzInfo.fMask = SIF_POS;
1983 GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &horzInfo);
1984 LISTVIEW_UpdateHeaderSize(infoPtr, horzInfo.nPos);
1991 * Shows/hides the focus rectangle.
1994 * [I] infoPtr : valid pointer to the listview structure
1995 * [I] fShow : TRUE to show the focus, FALSE to hide it.
2000 static void LISTVIEW_ShowFocusRect(const LISTVIEW_INFO *infoPtr, BOOL fShow)
2004 TRACE("fShow=%d, nItem=%d\n", fShow, infoPtr->nFocusedItem);
2006 if (infoPtr->nFocusedItem < 0) return;
2008 /* we need some gymnastics in ICON mode to handle large items */
2009 if (infoPtr->uView == LV_VIEW_ICON)
2013 LISTVIEW_GetItemBox(infoPtr, infoPtr->nFocusedItem, &rcBox);
2014 if ((rcBox.bottom - rcBox.top) > infoPtr->nItemHeight)
2016 LISTVIEW_InvalidateRect(infoPtr, &rcBox);
2021 if (!(hdc = GetDC(infoPtr->hwndSelf))) return;
2023 /* for some reason, owner draw should work only in report mode */
2024 if ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (infoPtr->uView == LV_VIEW_DETAILS))
2029 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
2030 HFONT hOldFont = SelectObject(hdc, hFont);
2032 item.iItem = infoPtr->nFocusedItem;
2034 item.mask = LVIF_PARAM;
2035 if (!LISTVIEW_GetItemW(infoPtr, &item)) goto done;
2037 ZeroMemory(&dis, sizeof(dis));
2038 dis.CtlType = ODT_LISTVIEW;
2039 dis.CtlID = (UINT)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
2040 dis.itemID = item.iItem;
2041 dis.itemAction = ODA_FOCUS;
2042 if (fShow) dis.itemState |= ODS_FOCUS;
2043 dis.hwndItem = infoPtr->hwndSelf;
2045 LISTVIEW_GetItemBox(infoPtr, dis.itemID, &dis.rcItem);
2046 dis.itemData = item.lParam;
2048 SendMessageW(infoPtr->hwndNotify, WM_DRAWITEM, dis.CtlID, (LPARAM)&dis);
2050 SelectObject(hdc, hOldFont);
2054 LISTVIEW_DrawFocusRect(infoPtr, hdc);
2057 ReleaseDC(infoPtr->hwndSelf, hdc);
2061 * Invalidates all visible selected items.
2063 static void LISTVIEW_InvalidateSelectedItems(const LISTVIEW_INFO *infoPtr)
2067 iterator_frameditems(&i, infoPtr, &infoPtr->rcList);
2068 while(iterator_next(&i))
2070 if (LISTVIEW_GetItemState(infoPtr, i.nItem, LVIS_SELECTED))
2071 LISTVIEW_InvalidateItem(infoPtr, i.nItem);
2073 iterator_destroy(&i);
2078 * DESCRIPTION: [INTERNAL]
2079 * Computes an item's (left,top) corner, relative to rcView.
2080 * That is, the position has NOT been made relative to the Origin.
2081 * This is deliberate, to avoid computing the Origin over, and
2082 * over again, when this function is called in a loop. Instead,
2083 * one can factor the computation of the Origin before the loop,
2084 * and offset the value returned by this function, on every iteration.
2087 * [I] infoPtr : valid pointer to the listview structure
2088 * [I] nItem : item number
2089 * [O] lpptOrig : item top, left corner
2094 static void LISTVIEW_GetItemOrigin(const LISTVIEW_INFO *infoPtr, INT nItem, LPPOINT lpptPosition)
2096 assert(nItem >= 0 && nItem < infoPtr->nItemCount);
2098 if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
2100 lpptPosition->x = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosX, nItem);
2101 lpptPosition->y = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosY, nItem);
2103 else if (infoPtr->uView == LV_VIEW_LIST)
2105 INT nCountPerColumn = LISTVIEW_GetCountPerColumn(infoPtr);
2106 lpptPosition->x = nItem / nCountPerColumn * infoPtr->nItemWidth;
2107 lpptPosition->y = nItem % nCountPerColumn * infoPtr->nItemHeight;
2109 else /* LV_VIEW_DETAILS */
2111 lpptPosition->x = REPORT_MARGINX;
2112 /* item is always at zero indexed column */
2113 if (DPA_GetPtrCount(infoPtr->hdpaColumns) > 0)
2114 lpptPosition->x += LISTVIEW_GetColumnInfo(infoPtr, 0)->rcHeader.left;
2115 lpptPosition->y = nItem * infoPtr->nItemHeight;
2120 * DESCRIPTION: [INTERNAL]
2121 * Compute the rectangles of an item. This is to localize all
2122 * the computations in one place. If you are not interested in some
2123 * of these values, simply pass in a NULL -- the function is smart
2124 * enough to compute only what's necessary. The function computes
2125 * the standard rectangles (BOUNDS, ICON, LABEL) plus a non-standard
2126 * one, the BOX rectangle. This rectangle is very cheap to compute,
2127 * and is guaranteed to contain all the other rectangles. Computing
2128 * the ICON rect is also cheap, but all the others are potentially
2129 * expensive. This gives an easy and effective optimization when
2130 * searching (like point inclusion, or rectangle intersection):
2131 * first test against the BOX, and if TRUE, test against the desired
2133 * If the function does not have all the necessary information
2134 * to computed the requested rectangles, will crash with a
2135 * failed assertion. This is done so we catch all programming
2136 * errors, given that the function is called only from our code.
2138 * We have the following 'special' meanings for a few fields:
2139 * * If LVIS_FOCUSED is set, we assume the item has the focus
2140 * This is important in ICON mode, where it might get a larger
2141 * then usual rectangle
2143 * Please note that subitem support works only in REPORT mode.
2146 * [I] infoPtr : valid pointer to the listview structure
2147 * [I] lpLVItem : item to compute the measures for
2148 * [O] lprcBox : ptr to Box rectangle
2149 * Same as LVM_GETITEMRECT with LVIR_BOUNDS
2150 * [0] lprcSelectBox : ptr to select box rectangle
2151 * Same as LVM_GETITEMRECT with LVIR_SELECTEDBOUNDS
2152 * [O] lprcIcon : ptr to Icon rectangle
2153 * Same as LVM_GETITEMRECT with LVIR_ICON
2154 * [O] lprcStateIcon: ptr to State Icon rectangle
2155 * [O] lprcLabel : ptr to Label rectangle
2156 * Same as LVM_GETITEMRECT with LVIR_LABEL
2161 static void LISTVIEW_GetItemMetrics(const LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem,
2162 LPRECT lprcBox, LPRECT lprcSelectBox,
2163 LPRECT lprcIcon, LPRECT lprcStateIcon, LPRECT lprcLabel)
2165 BOOL doSelectBox = FALSE, doIcon = FALSE, doLabel = FALSE, oversizedBox = FALSE;
2166 RECT Box, SelectBox, Icon, Label;
2167 COLUMN_INFO *lpColumnInfo = NULL;
2168 SIZE labelSize = { 0, 0 };
2170 TRACE("(lpLVItem=%s)\n", debuglvitem_t(lpLVItem, TRUE));
2172 /* Be smart and try to figure out the minimum we have to do */
2173 if (lpLVItem->iSubItem) assert(infoPtr->uView == LV_VIEW_DETAILS);
2174 if (infoPtr->uView == LV_VIEW_ICON && (lprcBox || lprcLabel))
2176 assert((lpLVItem->mask & LVIF_STATE) && (lpLVItem->stateMask & LVIS_FOCUSED));
2177 if (lpLVItem->state & LVIS_FOCUSED) oversizedBox = doLabel = TRUE;
2179 if (lprcSelectBox) doSelectBox = TRUE;
2180 if (lprcLabel) doLabel = TRUE;
2181 if (doLabel || lprcIcon || lprcStateIcon) doIcon = TRUE;
2188 /************************************************************/
2189 /* compute the box rectangle (it should be cheap to do) */
2190 /************************************************************/
2191 if (lpLVItem->iSubItem || infoPtr->uView == LV_VIEW_DETAILS)
2192 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, lpLVItem->iSubItem);
2194 if (lpLVItem->iSubItem)
2196 Box = lpColumnInfo->rcHeader;
2201 Box.right = infoPtr->nItemWidth;
2204 Box.bottom = infoPtr->nItemHeight;
2206 /******************************************************************/
2207 /* compute ICON bounding box (ala LVM_GETITEMRECT) and STATEICON */
2208 /******************************************************************/
2211 LONG state_width = 0;
2213 if (infoPtr->himlState && lpLVItem->iSubItem == 0)
2214 state_width = infoPtr->iconStateSize.cx;
2216 if (infoPtr->uView == LV_VIEW_ICON)
2218 Icon.left = Box.left + state_width;
2219 if (infoPtr->himlNormal)
2220 Icon.left += (infoPtr->nItemWidth - infoPtr->iconSize.cx - state_width) / 2;
2221 Icon.top = Box.top + ICON_TOP_PADDING;
2222 Icon.right = Icon.left;
2223 Icon.bottom = Icon.top;
2224 if (infoPtr->himlNormal)
2226 Icon.right += infoPtr->iconSize.cx;
2227 Icon.bottom += infoPtr->iconSize.cy;
2230 else /* LV_VIEW_SMALLICON, LV_VIEW_LIST or LV_VIEW_DETAILS */
2232 Icon.left = Box.left + state_width;
2234 if (infoPtr->uView == LV_VIEW_DETAILS && lpLVItem->iSubItem == 0)
2236 /* we need the indent in report mode */
2237 assert(lpLVItem->mask & LVIF_INDENT);
2238 Icon.left += infoPtr->iconSize.cx * lpLVItem->iIndent + REPORT_MARGINX;
2242 Icon.right = Icon.left;
2243 if (infoPtr->himlSmall &&
2244 (!lpColumnInfo || lpLVItem->iSubItem == 0 || (lpColumnInfo->fmt & LVCFMT_IMAGE) ||
2245 ((infoPtr->dwLvExStyle & LVS_EX_SUBITEMIMAGES) && lpLVItem->iImage != I_IMAGECALLBACK)))
2246 Icon.right += infoPtr->iconSize.cx;
2247 Icon.bottom = Icon.top + infoPtr->iconSize.cy;
2249 if(lprcIcon) *lprcIcon = Icon;
2250 TRACE(" - icon=%s\n", wine_dbgstr_rect(&Icon));
2252 /* TODO: is this correct? */
2255 lprcStateIcon->left = Icon.left - state_width;
2256 lprcStateIcon->right = Icon.left;
2257 lprcStateIcon->top = Icon.top;
2258 lprcStateIcon->bottom = lprcStateIcon->top + infoPtr->iconSize.cy;
2259 TRACE(" - state icon=%s\n", wine_dbgstr_rect(lprcStateIcon));
2262 else Icon.right = 0;
2264 /************************************************************/
2265 /* compute LABEL bounding box (ala LVM_GETITEMRECT) */
2266 /************************************************************/
2269 /* calculate how far to the right can the label stretch */
2270 Label.right = Box.right;
2271 if (infoPtr->uView == LV_VIEW_DETAILS)
2273 if (lpLVItem->iSubItem == 0)
2275 /* we need a zero based rect here */
2276 Label = lpColumnInfo->rcHeader;
2277 OffsetRect(&Label, -Label.left, 0);
2281 if (lpLVItem->iSubItem || ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && infoPtr->uView == LV_VIEW_DETAILS))
2283 labelSize.cx = infoPtr->nItemWidth;
2284 labelSize.cy = infoPtr->nItemHeight;
2288 /* we need the text in non owner draw mode */
2289 assert(lpLVItem->mask & LVIF_TEXT);
2290 if (is_textT(lpLVItem->pszText, TRUE))
2292 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
2293 HDC hdc = GetDC(infoPtr->hwndSelf);
2294 HFONT hOldFont = SelectObject(hdc, hFont);
2298 /* compute rough rectangle where the label will go */
2299 SetRectEmpty(&rcText);
2300 rcText.right = infoPtr->nItemWidth - TRAILING_LABEL_PADDING;
2301 rcText.bottom = infoPtr->nItemHeight;
2302 if (infoPtr->uView == LV_VIEW_ICON)
2303 rcText.bottom -= ICON_TOP_PADDING + infoPtr->iconSize.cy + ICON_BOTTOM_PADDING;
2305 /* now figure out the flags */
2306 if (infoPtr->uView == LV_VIEW_ICON)
2307 uFormat = oversizedBox ? LV_FL_DT_FLAGS : LV_ML_DT_FLAGS;
2309 uFormat = LV_SL_DT_FLAGS;
2311 DrawTextW (hdc, lpLVItem->pszText, -1, &rcText, uFormat | DT_CALCRECT);
2313 if (rcText.right != rcText.left)
2314 labelSize.cx = min(rcText.right - rcText.left + TRAILING_LABEL_PADDING, infoPtr->nItemWidth);
2316 labelSize.cy = rcText.bottom - rcText.top;
2318 SelectObject(hdc, hOldFont);
2319 ReleaseDC(infoPtr->hwndSelf, hdc);
2323 if (infoPtr->uView == LV_VIEW_ICON)
2325 Label.left = Box.left + (infoPtr->nItemWidth - labelSize.cx) / 2;
2326 Label.top = Box.top + ICON_TOP_PADDING_HITABLE +
2327 infoPtr->iconSize.cy + ICON_BOTTOM_PADDING;
2328 Label.right = Label.left + labelSize.cx;
2329 Label.bottom = Label.top + infoPtr->nItemHeight;
2330 if (!oversizedBox && labelSize.cy > infoPtr->ntmHeight)
2332 labelSize.cy = min(Box.bottom - Label.top, labelSize.cy);
2333 labelSize.cy /= infoPtr->ntmHeight;
2334 labelSize.cy = max(labelSize.cy, 1);
2335 labelSize.cy *= infoPtr->ntmHeight;
2337 Label.bottom = Label.top + labelSize.cy + HEIGHT_PADDING;
2339 else if (infoPtr->uView == LV_VIEW_DETAILS)
2341 Label.left = Icon.right;
2342 Label.top = Box.top;
2343 Label.right = lpLVItem->iSubItem ? lpColumnInfo->rcHeader.right :
2344 lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left;
2345 Label.bottom = Label.top + infoPtr->nItemHeight;
2347 else /* LV_VIEW_SMALLICON or LV_VIEW_LIST */
2349 Label.left = Icon.right;
2350 Label.top = Box.top;
2351 Label.right = min(Label.left + labelSize.cx, Label.right);
2352 Label.bottom = Label.top + infoPtr->nItemHeight;
2355 if (lprcLabel) *lprcLabel = Label;
2356 TRACE(" - label=%s\n", wine_dbgstr_rect(&Label));
2359 /************************************************************/
2360 /* compute SELECT bounding box */
2361 /************************************************************/
2364 if (infoPtr->uView == LV_VIEW_DETAILS)
2366 SelectBox.left = Icon.left;
2367 SelectBox.top = Box.top;
2368 SelectBox.bottom = Box.bottom;
2371 SelectBox.right = min(Label.left + labelSize.cx, Label.right);
2373 SelectBox.right = min(Label.left + MAX_EMPTYTEXT_SELECT_WIDTH, Label.right);
2377 UnionRect(&SelectBox, &Icon, &Label);
2379 if (lprcSelectBox) *lprcSelectBox = SelectBox;
2380 TRACE(" - select box=%s\n", wine_dbgstr_rect(&SelectBox));
2383 /* Fix the Box if necessary */
2386 if (oversizedBox) UnionRect(lprcBox, &Box, &Label);
2387 else *lprcBox = Box;
2389 TRACE(" - box=%s\n", wine_dbgstr_rect(&Box));
2393 * DESCRIPTION: [INTERNAL]
2396 * [I] infoPtr : valid pointer to the listview structure
2397 * [I] nItem : item number
2398 * [O] lprcBox : ptr to Box rectangle
2403 static void LISTVIEW_GetItemBox(const LISTVIEW_INFO *infoPtr, INT nItem, LPRECT lprcBox)
2405 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
2406 POINT Position, Origin;
2409 LISTVIEW_GetOrigin(infoPtr, &Origin);
2410 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
2412 /* Be smart and try to figure out the minimum we have to do */
2414 if (infoPtr->uView == LV_VIEW_ICON && infoPtr->bFocus && LISTVIEW_GetItemState(infoPtr, nItem, LVIS_FOCUSED))
2415 lvItem.mask |= LVIF_TEXT;
2416 lvItem.iItem = nItem;
2417 lvItem.iSubItem = 0;
2418 lvItem.pszText = szDispText;
2419 lvItem.cchTextMax = DISP_TEXT_SIZE;
2420 if (lvItem.mask) LISTVIEW_GetItemW(infoPtr, &lvItem);
2421 if (infoPtr->uView == LV_VIEW_ICON)
2423 lvItem.mask |= LVIF_STATE;
2424 lvItem.stateMask = LVIS_FOCUSED;
2425 lvItem.state = (lvItem.mask & LVIF_TEXT ? LVIS_FOCUSED : 0);
2427 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, lprcBox, 0, 0, 0, 0);
2429 if (infoPtr->uView == LV_VIEW_DETAILS && infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT &&
2430 SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, 0, 0))
2432 OffsetRect(lprcBox, Origin.x, Position.y + Origin.y);
2435 OffsetRect(lprcBox, Position.x + Origin.x, Position.y + Origin.y);
2438 /* LISTVIEW_MapIdToIndex helper */
2439 static INT CALLBACK MapIdSearchCompare(LPVOID p1, LPVOID p2, LPARAM lParam)
2441 ITEM_ID *id1 = (ITEM_ID*)p1;
2442 ITEM_ID *id2 = (ITEM_ID*)p2;
2444 if (id1->id == id2->id) return 0;
2446 return (id1->id < id2->id) ? -1 : 1;
2451 * Returns the item index for id specified.
2454 * [I] infoPtr : valid pointer to the listview structure
2455 * [I] iID : item id to get index for
2458 * Item index, or -1 on failure.
2460 static INT LISTVIEW_MapIdToIndex(const LISTVIEW_INFO *infoPtr, UINT iID)
2465 TRACE("iID=%d\n", iID);
2467 if (infoPtr->dwStyle & LVS_OWNERDATA) return -1;
2468 if (infoPtr->nItemCount == 0) return -1;
2471 index = DPA_Search(infoPtr->hdpaItemIds, &ID, -1, &MapIdSearchCompare, 0, DPAS_SORTED);
2475 ITEM_ID *lpID = DPA_GetPtr(infoPtr->hdpaItemIds, index);
2476 return DPA_GetPtrIndex(infoPtr->hdpaItems, lpID->item);
2484 * Returns the item id for index given.
2487 * [I] infoPtr : valid pointer to the listview structure
2488 * [I] iItem : item index to get id for
2493 static DWORD LISTVIEW_MapIndexToId(const LISTVIEW_INFO *infoPtr, INT iItem)
2498 TRACE("iItem=%d\n", iItem);
2500 if (infoPtr->dwStyle & LVS_OWNERDATA) return -1;
2501 if (iItem < 0 || iItem >= infoPtr->nItemCount) return -1;
2503 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, iItem);
2504 lpItem = DPA_GetPtr(hdpaSubItems, 0);
2506 return lpItem->id->id;
2511 * Returns the current icon position, and advances it along the top.
2512 * The returned position is not offset by Origin.
2515 * [I] infoPtr : valid pointer to the listview structure
2516 * [O] lpPos : will get the current icon position
2521 static void LISTVIEW_NextIconPosTop(LISTVIEW_INFO *infoPtr, LPPOINT lpPos)
2523 INT nListWidth = infoPtr->rcList.right - infoPtr->rcList.left;
2525 *lpPos = infoPtr->currIconPos;
2527 infoPtr->currIconPos.x += infoPtr->nItemWidth;
2528 if (infoPtr->currIconPos.x + infoPtr->nItemWidth <= nListWidth) return;
2530 infoPtr->currIconPos.x = 0;
2531 infoPtr->currIconPos.y += infoPtr->nItemHeight;
2537 * Returns the current icon position, and advances it down the left edge.
2538 * The returned position is not offset by Origin.
2541 * [I] infoPtr : valid pointer to the listview structure
2542 * [O] lpPos : will get the current icon position
2547 static void LISTVIEW_NextIconPosLeft(LISTVIEW_INFO *infoPtr, LPPOINT lpPos)
2549 INT nListHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
2551 *lpPos = infoPtr->currIconPos;
2553 infoPtr->currIconPos.y += infoPtr->nItemHeight;
2554 if (infoPtr->currIconPos.y + infoPtr->nItemHeight <= nListHeight) return;
2556 infoPtr->currIconPos.x += infoPtr->nItemWidth;
2557 infoPtr->currIconPos.y = 0;
2563 * Moves an icon to the specified position.
2564 * It takes care of invalidating the item, etc.
2567 * [I] infoPtr : valid pointer to the listview structure
2568 * [I] nItem : the item to move
2569 * [I] lpPos : the new icon position
2570 * [I] isNew : flags the item as being new
2576 static BOOL LISTVIEW_MoveIconTo(const LISTVIEW_INFO *infoPtr, INT nItem, const POINT *lppt, BOOL isNew)
2582 old.x = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosX, nItem);
2583 old.y = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosY, nItem);
2585 if (lppt->x == old.x && lppt->y == old.y) return TRUE;
2586 LISTVIEW_InvalidateItem(infoPtr, nItem);
2589 /* Allocating a POINTER for every item is too resource intensive,
2590 * so we'll keep the (x,y) in different arrays */
2591 if (!DPA_SetPtr(infoPtr->hdpaPosX, nItem, (void *)(LONG_PTR)lppt->x)) return FALSE;
2592 if (!DPA_SetPtr(infoPtr->hdpaPosY, nItem, (void *)(LONG_PTR)lppt->y)) return FALSE;
2594 LISTVIEW_InvalidateItem(infoPtr, nItem);
2601 * Arranges listview items in icon display mode.
2604 * [I] infoPtr : valid pointer to the listview structure
2605 * [I] nAlignCode : alignment code
2611 static BOOL LISTVIEW_Arrange(LISTVIEW_INFO *infoPtr, INT nAlignCode)
2613 void (*next_pos)(LISTVIEW_INFO *, LPPOINT);
2617 if (infoPtr->uView != LV_VIEW_ICON && infoPtr->uView != LV_VIEW_SMALLICON) return FALSE;
2619 TRACE("nAlignCode=%d\n", nAlignCode);
2621 if (nAlignCode == LVA_DEFAULT)
2623 if (infoPtr->dwStyle & LVS_ALIGNLEFT) nAlignCode = LVA_ALIGNLEFT;
2624 else nAlignCode = LVA_ALIGNTOP;
2629 case LVA_ALIGNLEFT: next_pos = LISTVIEW_NextIconPosLeft; break;
2630 case LVA_ALIGNTOP: next_pos = LISTVIEW_NextIconPosTop; break;
2631 case LVA_SNAPTOGRID: next_pos = LISTVIEW_NextIconPosTop; break; /* FIXME */
2632 default: return FALSE;
2635 infoPtr->bAutoarrange = TRUE;
2636 infoPtr->currIconPos.x = infoPtr->currIconPos.y = 0;
2637 for (i = 0; i < infoPtr->nItemCount; i++)
2639 next_pos(infoPtr, &pos);
2640 LISTVIEW_MoveIconTo(infoPtr, i, &pos, FALSE);
2648 * Retrieves the bounding rectangle of all the items, not offset by Origin.
2649 * For LVS_REPORT always returns empty rectangle.
2652 * [I] infoPtr : valid pointer to the listview structure
2653 * [O] lprcView : bounding rectangle
2659 static void LISTVIEW_GetAreaRect(const LISTVIEW_INFO *infoPtr, LPRECT lprcView)
2663 SetRectEmpty(lprcView);
2665 switch (infoPtr->uView)
2668 case LV_VIEW_SMALLICON:
2669 for (i = 0; i < infoPtr->nItemCount; i++)
2671 x = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosX, i);
2672 y = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosY, i);
2673 lprcView->right = max(lprcView->right, x);
2674 lprcView->bottom = max(lprcView->bottom, y);
2676 if (infoPtr->nItemCount > 0)
2678 lprcView->right += infoPtr->nItemWidth;
2679 lprcView->bottom += infoPtr->nItemHeight;
2684 y = LISTVIEW_GetCountPerColumn(infoPtr);
2685 x = infoPtr->nItemCount / y;
2686 if (infoPtr->nItemCount % y) x++;
2687 lprcView->right = x * infoPtr->nItemWidth;
2688 lprcView->bottom = y * infoPtr->nItemHeight;
2695 * Retrieves the bounding rectangle of all the items.
2698 * [I] infoPtr : valid pointer to the listview structure
2699 * [O] lprcView : bounding rectangle
2705 static BOOL LISTVIEW_GetViewRect(const LISTVIEW_INFO *infoPtr, LPRECT lprcView)
2709 TRACE("(lprcView=%p)\n", lprcView);
2711 if (!lprcView) return FALSE;
2713 LISTVIEW_GetAreaRect(infoPtr, lprcView);
2715 if (infoPtr->uView != LV_VIEW_DETAILS)
2717 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
2718 OffsetRect(lprcView, ptOrigin.x, ptOrigin.y);
2721 TRACE("lprcView=%s\n", wine_dbgstr_rect(lprcView));
2728 * Retrieves the subitem pointer associated with the subitem index.
2731 * [I] hdpaSubItems : DPA handle for a specific item
2732 * [I] nSubItem : index of subitem
2735 * SUCCESS : subitem pointer
2738 static SUBITEM_INFO* LISTVIEW_GetSubItemPtr(HDPA hdpaSubItems, INT nSubItem)
2740 SUBITEM_INFO *lpSubItem;
2743 /* we should binary search here if need be */
2744 for (i = 1; i < DPA_GetPtrCount(hdpaSubItems); i++)
2746 lpSubItem = DPA_GetPtr(hdpaSubItems, i);
2747 if (lpSubItem->iSubItem == nSubItem)
2757 * Calculates the desired item width.
2760 * [I] infoPtr : valid pointer to the listview structure
2763 * The desired item width.
2765 static INT LISTVIEW_CalculateItemWidth(const LISTVIEW_INFO *infoPtr)
2769 TRACE("uView=%d\n", infoPtr->uView);
2771 if (infoPtr->uView == LV_VIEW_ICON)
2772 nItemWidth = infoPtr->iconSpacing.cx;
2773 else if (infoPtr->uView == LV_VIEW_DETAILS)
2775 if (DPA_GetPtrCount(infoPtr->hdpaColumns) > 0)
2780 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX,
2781 DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, 0);
2783 LISTVIEW_GetHeaderRect(infoPtr, index, &rcHeader);
2784 nItemWidth = rcHeader.right;
2787 else /* LV_VIEW_SMALLICON, or LV_VIEW_LIST */
2791 for (i = 0; i < infoPtr->nItemCount; i++)
2792 nItemWidth = max(LISTVIEW_GetLabelWidth(infoPtr, i), nItemWidth);
2794 if (infoPtr->himlSmall) nItemWidth += infoPtr->iconSize.cx;
2795 if (infoPtr->himlState) nItemWidth += infoPtr->iconStateSize.cx;
2797 nItemWidth = max(DEFAULT_COLUMN_WIDTH, nItemWidth + WIDTH_PADDING);
2805 * Calculates the desired item height.
2808 * [I] infoPtr : valid pointer to the listview structure
2811 * The desired item height.
2813 static INT LISTVIEW_CalculateItemHeight(const LISTVIEW_INFO *infoPtr)
2817 TRACE("uView=%d\n", infoPtr->uView);
2819 if (infoPtr->uView == LV_VIEW_ICON)
2820 nItemHeight = infoPtr->iconSpacing.cy;
2823 nItemHeight = infoPtr->ntmHeight;
2824 if (infoPtr->uView == LV_VIEW_DETAILS && infoPtr->dwLvExStyle & LVS_EX_GRIDLINES)
2826 if (infoPtr->himlState)
2827 nItemHeight = max(nItemHeight, infoPtr->iconStateSize.cy);
2828 if (infoPtr->himlSmall)
2829 nItemHeight = max(nItemHeight, infoPtr->iconSize.cy);
2830 if (infoPtr->himlState || infoPtr->himlSmall)
2831 nItemHeight += HEIGHT_PADDING;
2832 if (infoPtr->nMeasureItemHeight > 0)
2833 nItemHeight = infoPtr->nMeasureItemHeight;
2836 return max(nItemHeight, 1);
2841 * Updates the width, and height of an item.
2844 * [I] infoPtr : valid pointer to the listview structure
2849 static inline void LISTVIEW_UpdateItemSize(LISTVIEW_INFO *infoPtr)
2851 infoPtr->nItemWidth = LISTVIEW_CalculateItemWidth(infoPtr);
2852 infoPtr->nItemHeight = LISTVIEW_CalculateItemHeight(infoPtr);
2858 * Retrieves and saves important text metrics info for the current
2862 * [I] infoPtr : valid pointer to the listview structure
2865 static void LISTVIEW_SaveTextMetrics(LISTVIEW_INFO *infoPtr)
2867 HDC hdc = GetDC(infoPtr->hwndSelf);
2868 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
2869 HFONT hOldFont = SelectObject(hdc, hFont);
2873 if (GetTextMetricsW(hdc, &tm))
2875 infoPtr->ntmHeight = tm.tmHeight;
2876 infoPtr->ntmMaxCharWidth = tm.tmMaxCharWidth;
2879 if (GetTextExtentPoint32A(hdc, "...", 3, &sz))
2880 infoPtr->nEllipsisWidth = sz.cx;
2882 SelectObject(hdc, hOldFont);
2883 ReleaseDC(infoPtr->hwndSelf, hdc);
2885 TRACE("tmHeight=%d\n", infoPtr->ntmHeight);
2890 * A compare function for ranges
2893 * [I] range1 : pointer to range 1;
2894 * [I] range2 : pointer to range 2;
2898 * > 0 : if range 1 > range 2
2899 * < 0 : if range 2 > range 1
2900 * = 0 : if range intersects range 2
2902 static INT CALLBACK ranges_cmp(LPVOID range1, LPVOID range2, LPARAM flags)
2906 if (((RANGE*)range1)->upper <= ((RANGE*)range2)->lower)
2908 else if (((RANGE*)range2)->upper <= ((RANGE*)range1)->lower)
2913 TRACE("range1=%s, range2=%s, cmp=%d\n", debugrange(range1), debugrange(range2), cmp);
2919 #define ranges_check(ranges, desc) ranges_assert(ranges, desc, __FUNCTION__, __LINE__)
2921 #define ranges_check(ranges, desc) do { } while(0)
2924 static void ranges_assert(RANGES ranges, LPCSTR desc, const char *func, int line)
2929 TRACE("*** Checking %s:%d:%s ***\n", func, line, desc);
2931 assert (DPA_GetPtrCount(ranges->hdpa) >= 0);
2932 ranges_dump(ranges);
2933 if (DPA_GetPtrCount(ranges->hdpa) > 0)
2935 prev = DPA_GetPtr(ranges->hdpa, 0);
2936 assert (prev->lower >= 0 && prev->lower < prev->upper);
2937 for (i = 1; i < DPA_GetPtrCount(ranges->hdpa); i++)
2939 curr = DPA_GetPtr(ranges->hdpa, i);
2940 assert (prev->upper <= curr->lower);
2941 assert (curr->lower < curr->upper);
2945 TRACE("--- Done checking---\n");
2948 static RANGES ranges_create(int count)
2950 RANGES ranges = Alloc(sizeof(struct tagRANGES));
2951 if (!ranges) return NULL;
2952 ranges->hdpa = DPA_Create(count);
2953 if (ranges->hdpa) return ranges;
2958 static void ranges_clear(RANGES ranges)
2962 for(i = 0; i < DPA_GetPtrCount(ranges->hdpa); i++)
2963 Free(DPA_GetPtr(ranges->hdpa, i));
2964 DPA_DeleteAllPtrs(ranges->hdpa);
2968 static void ranges_destroy(RANGES ranges)
2970 if (!ranges) return;
2971 ranges_clear(ranges);
2972 DPA_Destroy(ranges->hdpa);
2976 static RANGES ranges_clone(RANGES ranges)
2981 if (!(clone = ranges_create(DPA_GetPtrCount(ranges->hdpa)))) goto fail;
2983 for (i = 0; i < DPA_GetPtrCount(ranges->hdpa); i++)
2985 RANGE *newrng = Alloc(sizeof(RANGE));
2986 if (!newrng) goto fail;
2987 *newrng = *((RANGE*)DPA_GetPtr(ranges->hdpa, i));
2988 DPA_SetPtr(clone->hdpa, i, newrng);
2993 TRACE ("clone failed\n");
2994 ranges_destroy(clone);
2998 static RANGES ranges_diff(RANGES ranges, RANGES sub)
3002 for (i = 0; i < DPA_GetPtrCount(sub->hdpa); i++)
3003 ranges_del(ranges, *((RANGE *)DPA_GetPtr(sub->hdpa, i)));
3008 static void ranges_dump(RANGES ranges)
3012 for (i = 0; i < DPA_GetPtrCount(ranges->hdpa); i++)
3013 TRACE(" %s\n", debugrange(DPA_GetPtr(ranges->hdpa, i)));
3016 static inline BOOL ranges_contain(RANGES ranges, INT nItem)
3018 RANGE srchrng = { nItem, nItem + 1 };
3020 TRACE("(nItem=%d)\n", nItem);
3021 ranges_check(ranges, "before contain");
3022 return DPA_Search(ranges->hdpa, &srchrng, 0, ranges_cmp, 0, DPAS_SORTED) != -1;
3025 static INT ranges_itemcount(RANGES ranges)
3029 for (i = 0; i < DPA_GetPtrCount(ranges->hdpa); i++)
3031 RANGE *sel = DPA_GetPtr(ranges->hdpa, i);
3032 count += sel->upper - sel->lower;
3038 static BOOL ranges_shift(RANGES ranges, INT nItem, INT delta, INT nUpper)
3040 RANGE srchrng = { nItem, nItem + 1 }, *chkrng;
3043 index = DPA_Search(ranges->hdpa, &srchrng, 0, ranges_cmp, 0, DPAS_SORTED | DPAS_INSERTAFTER);
3044 if (index == -1) return TRUE;
3046 for (; index < DPA_GetPtrCount(ranges->hdpa); index++)
3048 chkrng = DPA_GetPtr(ranges->hdpa, index);
3049 if (chkrng->lower >= nItem)
3050 chkrng->lower = max(min(chkrng->lower + delta, nUpper - 1), 0);
3051 if (chkrng->upper > nItem)
3052 chkrng->upper = max(min(chkrng->upper + delta, nUpper), 0);
3057 static BOOL ranges_add(RANGES ranges, RANGE range)
3062 TRACE("(%s)\n", debugrange(&range));
3063 ranges_check(ranges, "before add");
3065 /* try find overlapping regions first */
3066 srchrgn.lower = range.lower - 1;
3067 srchrgn.upper = range.upper + 1;
3068 index = DPA_Search(ranges->hdpa, &srchrgn, 0, ranges_cmp, 0, DPAS_SORTED);
3074 TRACE("Adding new range\n");
3076 /* create the brand new range to insert */
3077 newrgn = Alloc(sizeof(RANGE));
3078 if(!newrgn) goto fail;
3081 /* figure out where to insert it */
3082 index = DPA_Search(ranges->hdpa, newrgn, 0, ranges_cmp, 0, DPAS_SORTED | DPAS_INSERTAFTER);
3083 TRACE("index=%d\n", index);
3084 if (index == -1) index = 0;
3086 /* and get it over with */
3087 if (DPA_InsertPtr(ranges->hdpa, index, newrgn) == -1)
3095 RANGE *chkrgn, *mrgrgn;
3096 INT fromindex, mergeindex;
3098 chkrgn = DPA_GetPtr(ranges->hdpa, index);
3099 TRACE("Merge with %s @%d\n", debugrange(chkrgn), index);
3101 chkrgn->lower = min(range.lower, chkrgn->lower);
3102 chkrgn->upper = max(range.upper, chkrgn->upper);
3104 TRACE("New range %s @%d\n", debugrange(chkrgn), index);
3106 /* merge now common ranges */
3108 srchrgn.lower = chkrgn->lower - 1;
3109 srchrgn.upper = chkrgn->upper + 1;
3113 mergeindex = DPA_Search(ranges->hdpa, &srchrgn, fromindex, ranges_cmp, 0, 0);
3114 if (mergeindex == -1) break;
3115 if (mergeindex == index)
3117 fromindex = index + 1;
3121 TRACE("Merge with index %i\n", mergeindex);
3123 mrgrgn = DPA_GetPtr(ranges->hdpa, mergeindex);
3124 chkrgn->lower = min(chkrgn->lower, mrgrgn->lower);
3125 chkrgn->upper = max(chkrgn->upper, mrgrgn->upper);
3127 DPA_DeletePtr(ranges->hdpa, mergeindex);
3128 if (mergeindex < index) index --;
3132 ranges_check(ranges, "after add");
3136 ranges_check(ranges, "failed add");
3140 static BOOL ranges_del(RANGES ranges, RANGE range)
3145 TRACE("(%s)\n", debugrange(&range));
3146 ranges_check(ranges, "before del");
3148 /* we don't use DPAS_SORTED here, since we need *
3149 * to find the first overlapping range */
3150 index = DPA_Search(ranges->hdpa, &range, 0, ranges_cmp, 0, 0);
3153 chkrgn = DPA_GetPtr(ranges->hdpa, index);
3155 TRACE("Matches range %s @%d\n", debugrange(chkrgn), index);
3157 /* case 1: Same range */
3158 if ( (chkrgn->upper == range.upper) &&
3159 (chkrgn->lower == range.lower) )
3161 DPA_DeletePtr(ranges->hdpa, index);
3164 /* case 2: engulf */
3165 else if ( (chkrgn->upper <= range.upper) &&
3166 (chkrgn->lower >= range.lower) )
3168 DPA_DeletePtr(ranges->hdpa, index);
3170 /* case 3: overlap upper */
3171 else if ( (chkrgn->upper <= range.upper) &&
3172 (chkrgn->lower < range.lower) )
3174 chkrgn->upper = range.lower;
3176 /* case 4: overlap lower */
3177 else if ( (chkrgn->upper > range.upper) &&
3178 (chkrgn->lower >= range.lower) )
3180 chkrgn->lower = range.upper;
3183 /* case 5: fully internal */
3186 RANGE tmprgn = *chkrgn, *newrgn;
3188 if (!(newrgn = Alloc(sizeof(RANGE)))) goto fail;
3189 newrgn->lower = chkrgn->lower;
3190 newrgn->upper = range.lower;
3191 chkrgn->lower = range.upper;
3192 if (DPA_InsertPtr(ranges->hdpa, index, newrgn) == -1)
3201 index = DPA_Search(ranges->hdpa, &range, index, ranges_cmp, 0, 0);
3204 ranges_check(ranges, "after del");
3208 ranges_check(ranges, "failed del");
3214 * Removes all selection ranges
3217 * [I] infoPtr : valid pointer to the listview structure
3218 * [I] toSkip : item range to skip removing the selection
3224 static BOOL LISTVIEW_DeselectAllSkipItems(LISTVIEW_INFO *infoPtr, RANGES toSkip)
3233 lvItem.stateMask = LVIS_SELECTED;
3235 /* need to clone the DPA because callbacks can change it */
3236 if (!(clone = ranges_clone(infoPtr->selectionRanges))) return FALSE;
3237 iterator_rangesitems(&i, ranges_diff(clone, toSkip));
3238 while(iterator_next(&i))
3239 LISTVIEW_SetItemState(infoPtr, i.nItem, &lvItem);
3240 /* note that the iterator destructor will free the cloned range */
3241 iterator_destroy(&i);
3246 static inline BOOL LISTVIEW_DeselectAllSkipItem(LISTVIEW_INFO *infoPtr, INT nItem)
3250 if (!(toSkip = ranges_create(1))) return FALSE;
3251 if (nItem != -1) ranges_additem(toSkip, nItem);
3252 LISTVIEW_DeselectAllSkipItems(infoPtr, toSkip);
3253 ranges_destroy(toSkip);
3257 static inline BOOL LISTVIEW_DeselectAll(LISTVIEW_INFO *infoPtr)
3259 return LISTVIEW_DeselectAllSkipItem(infoPtr, -1);
3264 * Retrieves the number of items that are marked as selected.
3267 * [I] infoPtr : valid pointer to the listview structure
3270 * Number of items selected.
3272 static INT LISTVIEW_GetSelectedCount(const LISTVIEW_INFO *infoPtr)
3274 INT nSelectedCount = 0;
3276 if (infoPtr->uCallbackMask & LVIS_SELECTED)
3279 for (i = 0; i < infoPtr->nItemCount; i++)
3281 if (LISTVIEW_GetItemState(infoPtr, i, LVIS_SELECTED))
3286 nSelectedCount = ranges_itemcount(infoPtr->selectionRanges);
3288 TRACE("nSelectedCount=%d\n", nSelectedCount);
3289 return nSelectedCount;
3294 * Manages the item focus.
3297 * [I] infoPtr : valid pointer to the listview structure
3298 * [I] nItem : item index
3301 * TRUE : focused item changed
3302 * FALSE : focused item has NOT changed
3304 static inline BOOL LISTVIEW_SetItemFocus(LISTVIEW_INFO *infoPtr, INT nItem)
3306 INT oldFocus = infoPtr->nFocusedItem;
3309 if (nItem == infoPtr->nFocusedItem) return FALSE;
3311 lvItem.state = nItem == -1 ? 0 : LVIS_FOCUSED;
3312 lvItem.stateMask = LVIS_FOCUSED;
3313 LISTVIEW_SetItemState(infoPtr, nItem == -1 ? infoPtr->nFocusedItem : nItem, &lvItem);
3315 return oldFocus != infoPtr->nFocusedItem;
3318 /* Helper function for LISTVIEW_ShiftIndices *only* */
3319 static INT shift_item(const LISTVIEW_INFO *infoPtr, INT nShiftItem, INT nItem, INT direction)
3321 if (nShiftItem < nItem) return nShiftItem;
3323 if (nShiftItem > nItem) return nShiftItem + direction;
3325 if (direction > 0) return nShiftItem + direction;
3327 return min(nShiftItem, infoPtr->nItemCount - 1);
3332 * Updates the various indices after an item has been inserted or deleted.
3335 * [I] infoPtr : valid pointer to the listview structure
3336 * [I] nItem : item index
3337 * [I] direction : Direction of shift, +1 or -1.
3342 static void LISTVIEW_ShiftIndices(LISTVIEW_INFO *infoPtr, INT nItem, INT direction)
3347 /* temporarily disable change notification while shifting items */
3348 bOldChange = infoPtr->bDoChangeNotify;
3349 infoPtr->bDoChangeNotify = FALSE;
3351 TRACE("Shifting %iu, %i steps\n", nItem, direction);
3353 ranges_shift(infoPtr->selectionRanges, nItem, direction, infoPtr->nItemCount);
3355 assert(abs(direction) == 1);
3357 infoPtr->nSelectionMark = shift_item(infoPtr, infoPtr->nSelectionMark, nItem, direction);
3359 nNewFocus = shift_item(infoPtr, infoPtr->nFocusedItem, nItem, direction);
3360 if (nNewFocus != infoPtr->nFocusedItem)
3361 LISTVIEW_SetItemFocus(infoPtr, nNewFocus);
3363 /* But we are not supposed to modify nHotItem! */
3365 infoPtr->bDoChangeNotify = bOldChange;
3371 * Adds a block of selections.
3374 * [I] infoPtr : valid pointer to the listview structure
3375 * [I] nItem : item index
3378 * Whether the window is still valid.
3380 static BOOL LISTVIEW_AddGroupSelection(LISTVIEW_INFO *infoPtr, INT nItem)
3382 INT nFirst = min(infoPtr->nSelectionMark, nItem);
3383 INT nLast = max(infoPtr->nSelectionMark, nItem);
3384 HWND hwndSelf = infoPtr->hwndSelf;
3385 NMLVODSTATECHANGE nmlv;
3390 /* Temporarily disable change notification
3391 * If the control is LVS_OWNERDATA, we need to send
3392 * only one LVN_ODSTATECHANGED notification.
3393 * See MSDN documentation for LVN_ITEMCHANGED.
3395 bOldChange = infoPtr->bDoChangeNotify;
3396 if (infoPtr->dwStyle & LVS_OWNERDATA) infoPtr->bDoChangeNotify = FALSE;
3398 if (nFirst == -1) nFirst = nItem;
3400 item.state = LVIS_SELECTED;
3401 item.stateMask = LVIS_SELECTED;
3403 for (i = nFirst; i <= nLast; i++)
3404 LISTVIEW_SetItemState(infoPtr,i,&item);
3406 ZeroMemory(&nmlv, sizeof(nmlv));
3407 nmlv.iFrom = nFirst;
3410 nmlv.uOldState = item.state;
3412 notify_hdr(infoPtr, LVN_ODSTATECHANGED, (LPNMHDR)&nmlv);
3413 if (!IsWindow(hwndSelf))
3415 infoPtr->bDoChangeNotify = bOldChange;
3422 * Sets a single group selection.
3425 * [I] infoPtr : valid pointer to the listview structure
3426 * [I] nItem : item index
3431 static void LISTVIEW_SetGroupSelection(LISTVIEW_INFO *infoPtr, INT nItem)
3438 if (!(selection = ranges_create(100))) return;
3440 item.state = LVIS_SELECTED;
3441 item.stateMask = LVIS_SELECTED;
3443 if ((infoPtr->uView == LV_VIEW_LIST) || (infoPtr->uView == LV_VIEW_DETAILS))
3445 if (infoPtr->nSelectionMark == -1)
3447 infoPtr->nSelectionMark = nItem;
3448 ranges_additem(selection, nItem);
3454 sel.lower = min(infoPtr->nSelectionMark, nItem);
3455 sel.upper = max(infoPtr->nSelectionMark, nItem) + 1;
3456 ranges_add(selection, sel);
3461 RECT rcItem, rcSel, rcSelMark;
3464 rcItem.left = LVIR_BOUNDS;
3465 if (!LISTVIEW_GetItemRect(infoPtr, nItem, &rcItem)) return;
3466 rcSelMark.left = LVIR_BOUNDS;
3467 if (!LISTVIEW_GetItemRect(infoPtr, infoPtr->nSelectionMark, &rcSelMark)) return;
3468 UnionRect(&rcSel, &rcItem, &rcSelMark);
3469 iterator_frameditems(&i, infoPtr, &rcSel);
3470 while(iterator_next(&i))
3472 LISTVIEW_GetItemPosition(infoPtr, i.nItem, &ptItem);
3473 if (PtInRect(&rcSel, ptItem)) ranges_additem(selection, i.nItem);
3475 iterator_destroy(&i);
3478 /* disable per item notifications on LVS_OWNERDATA style
3479 FIXME: single LVN_ODSTATECHANGED should be used */
3480 bOldChange = infoPtr->bDoChangeNotify;
3481 if (infoPtr->dwStyle & LVS_OWNERDATA) infoPtr->bDoChangeNotify = FALSE;
3483 LISTVIEW_DeselectAllSkipItems(infoPtr, selection);
3486 iterator_rangesitems(&i, selection);
3487 while(iterator_next(&i))
3488 LISTVIEW_SetItemState(infoPtr, i.nItem, &item);
3489 /* this will also destroy the selection */
3490 iterator_destroy(&i);
3492 infoPtr->bDoChangeNotify = bOldChange;
3494 LISTVIEW_SetItemFocus(infoPtr, nItem);
3499 * Sets a single selection.
3502 * [I] infoPtr : valid pointer to the listview structure
3503 * [I] nItem : item index
3508 static void LISTVIEW_SetSelection(LISTVIEW_INFO *infoPtr, INT nItem)
3512 TRACE("nItem=%d\n", nItem);
3514 LISTVIEW_DeselectAllSkipItem(infoPtr, nItem);
3516 lvItem.state = LVIS_FOCUSED | LVIS_SELECTED;
3517 lvItem.stateMask = LVIS_FOCUSED | LVIS_SELECTED;
3518 LISTVIEW_SetItemState(infoPtr, nItem, &lvItem);
3520 infoPtr->nSelectionMark = nItem;
3525 * Set selection(s) with keyboard.
3528 * [I] infoPtr : valid pointer to the listview structure
3529 * [I] nItem : item index
3530 * [I] space : VK_SPACE code sent
3533 * SUCCESS : TRUE (needs to be repainted)
3534 * FAILURE : FALSE (nothing has changed)
3536 static BOOL LISTVIEW_KeySelection(LISTVIEW_INFO *infoPtr, INT nItem, BOOL space)
3538 /* FIXME: pass in the state */
3539 WORD wShift = HIWORD(GetKeyState(VK_SHIFT));
3540 WORD wCtrl = HIWORD(GetKeyState(VK_CONTROL));
3541 BOOL bResult = FALSE;
3543 TRACE("nItem=%d, wShift=%d, wCtrl=%d\n", nItem, wShift, wCtrl);
3544 if ((nItem >= 0) && (nItem < infoPtr->nItemCount))
3548 if (infoPtr->dwStyle & LVS_SINGLESEL || (wShift == 0 && wCtrl == 0))
3549 LISTVIEW_SetSelection(infoPtr, nItem);
3553 LISTVIEW_SetGroupSelection(infoPtr, nItem);
3557 lvItem.state = ~LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED);
3558 lvItem.stateMask = LVIS_SELECTED;
3561 LISTVIEW_SetItemState(infoPtr, nItem, &lvItem);
3562 if (lvItem.state & LVIS_SELECTED)
3563 infoPtr->nSelectionMark = nItem;
3565 bResult = LISTVIEW_SetItemFocus(infoPtr, nItem);
3568 LISTVIEW_EnsureVisible(infoPtr, nItem, FALSE);
3571 UpdateWindow(infoPtr->hwndSelf); /* update client area */
3575 static BOOL LISTVIEW_GetItemAtPt(const LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem, POINT pt)
3577 LVHITTESTINFO lvHitTestInfo;
3579 ZeroMemory(&lvHitTestInfo, sizeof(lvHitTestInfo));
3580 lvHitTestInfo.pt.x = pt.x;
3581 lvHitTestInfo.pt.y = pt.y;
3583 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
3585 lpLVItem->mask = LVIF_PARAM;
3586 lpLVItem->iItem = lvHitTestInfo.iItem;
3587 lpLVItem->iSubItem = 0;
3589 return LISTVIEW_GetItemT(infoPtr, lpLVItem, TRUE);
3592 static inline BOOL LISTVIEW_isHotTracking(const LISTVIEW_INFO *infoPtr)
3594 return ((infoPtr->dwLvExStyle & LVS_EX_TRACKSELECT) ||
3595 (infoPtr->dwLvExStyle & LVS_EX_ONECLICKACTIVATE) ||
3596 (infoPtr->dwLvExStyle & LVS_EX_TWOCLICKACTIVATE));
3601 * Called when the mouse is being actively tracked and has hovered for a specified
3605 * [I] infoPtr : valid pointer to the listview structure
3606 * [I] fwKeys : key indicator
3607 * [I] x,y : mouse position
3610 * 0 if the message was processed, non-zero if there was an error
3613 * LVS_EX_TRACKSELECT: An item is automatically selected when the cursor remains
3614 * over the item for a certain period of time.
3617 static LRESULT LISTVIEW_MouseHover(LISTVIEW_INFO *infoPtr, WORD fwKeys, INT x, INT y)
3619 if (LISTVIEW_isHotTracking(infoPtr))
3627 if (LISTVIEW_GetItemAtPt(infoPtr, &item, pt))
3628 LISTVIEW_SetSelection(infoPtr, item.iItem);
3634 #define SCROLL_LEFT 0x1
3635 #define SCROLL_RIGHT 0x2
3636 #define SCROLL_UP 0x4
3637 #define SCROLL_DOWN 0x8
3641 * Utility routine to draw and highlight items within a marquee selection rectangle.
3644 * [I] infoPtr : valid pointer to the listview structure
3645 * [I] coords_orig : original co-ordinates of the cursor
3646 * [I] coords_offs : offsetted coordinates of the cursor
3647 * [I] offset : offset amount
3648 * [I] scroll : Bitmask of which directions we should scroll, if at all
3653 static void LISTVIEW_MarqueeHighlight(LISTVIEW_INFO *infoPtr, LPPOINT coords_orig, LPPOINT coords_offs, LPPOINT offset, INT scroll)
3655 BOOL controlDown = FALSE;
3660 if (coords_offs->x > infoPtr->marqueeOrigin.x)
3662 rect.left = infoPtr->marqueeOrigin.x;
3663 rect.right = coords_offs->x;
3667 rect.left = coords_offs->x;
3668 rect.right = infoPtr->marqueeOrigin.x;
3671 if (coords_offs->y > infoPtr->marqueeOrigin.y)
3673 rect.top = infoPtr->marqueeOrigin.y;
3674 rect.bottom = coords_offs->y;
3678 rect.top = coords_offs->y;
3679 rect.bottom = infoPtr->marqueeOrigin.y;
3682 /* Cancel out the old marquee rectangle and draw the new one */
3683 LISTVIEW_InvalidateRect(infoPtr, &infoPtr->marqueeDrawRect);
3685 /* Scroll by the appropriate distance if applicable - speed up scrolling as
3686 the cursor is further away */
3688 if ((scroll & SCROLL_LEFT) && (coords_orig->x <= 0))
3689 LISTVIEW_Scroll(infoPtr, coords_orig->x, 0);
3691 if ((scroll & SCROLL_RIGHT) && (coords_orig->x >= infoPtr->rcList.right))
3692 LISTVIEW_Scroll(infoPtr, (coords_orig->x - infoPtr->rcList.right), 0);
3694 if ((scroll & SCROLL_UP) && (coords_orig->y <= 0))
3695 LISTVIEW_Scroll(infoPtr, 0, coords_orig->y);
3697 if ((scroll & SCROLL_DOWN) && (coords_orig->y >= infoPtr->rcList.bottom))
3698 LISTVIEW_Scroll(infoPtr, 0, (coords_orig->y - infoPtr->rcList.bottom));
3700 /* Invert the items in the old marquee rectangle */
3701 iterator_frameditems_absolute(&i, infoPtr, &infoPtr->marqueeRect);
3703 while (iterator_next(&i))
3707 if (LISTVIEW_GetItemState(infoPtr, i.nItem, LVIS_SELECTED) == LVIS_SELECTED)
3710 item.state = LVIS_SELECTED;
3712 item.stateMask = LVIS_SELECTED;
3714 LISTVIEW_SetItemState(infoPtr, i.nItem, &item);
3718 iterator_destroy(&i);
3720 CopyRect(&infoPtr->marqueeRect, &rect);
3722 CopyRect(&infoPtr->marqueeDrawRect, &rect);
3723 OffsetRect(&infoPtr->marqueeDrawRect, offset->x, offset->y);
3725 /* Iterate over the items within our marquee rectangle */
3726 iterator_frameditems_absolute(&i, infoPtr, &infoPtr->marqueeRect);
3728 if (GetKeyState(VK_CONTROL) & 0x8000)
3731 while (iterator_next(&i))
3735 /* If CTRL is pressed, invert. If not, always select the item. */
3736 if ((controlDown) && (LISTVIEW_GetItemState(infoPtr, i.nItem, LVIS_SELECTED)))
3739 item.state = LVIS_SELECTED;
3741 item.stateMask = LVIS_SELECTED;
3743 LISTVIEW_SetItemState(infoPtr, i.nItem, &item);
3747 iterator_destroy(&i);
3748 LISTVIEW_InvalidateRect(infoPtr, &rect);
3753 * Called when we are in a marquee selection that involves scrolling the listview (ie,
3754 * the cursor is outside the bounds of the client area). This is a TIMERPROC.
3757 * [I] hwnd : Handle to the listview
3758 * [I] uMsg : WM_TIMER (ignored)
3759 * [I] idEvent : The timer ID interpreted as a pointer to a LISTVIEW_INFO struct
3760 * [I] dwTimer : The elapsed time (ignored)
3765 static VOID CALLBACK LISTVIEW_ScrollTimer(HWND hWnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
3767 LISTVIEW_INFO *infoPtr;
3768 SCROLLINFO scrollInfo;
3774 infoPtr = (LISTVIEW_INFO *) idEvent;
3779 /* Get the current cursor position and convert to client coordinates */
3780 GetCursorPos(&coords_orig);
3781 ScreenToClient(hWnd, &coords_orig);
3783 /* Ensure coordinates are within client bounds */
3784 coords_offs.x = max(min(coords_orig.x, infoPtr->rcList.right), 0);
3785 coords_offs.y = max(min(coords_orig.y, infoPtr->rcList.bottom), 0);
3788 LISTVIEW_GetOrigin(infoPtr, &offset);
3790 /* Offset coordinates by the appropriate amount */
3791 coords_offs.x -= offset.x;
3792 coords_offs.y -= offset.y;
3794 scrollInfo.cbSize = sizeof(SCROLLINFO);
3795 scrollInfo.fMask = SIF_ALL;
3797 /* Work out in which directions we can scroll */
3798 if (GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
3800 if (scrollInfo.nPos != scrollInfo.nMin)
3801 scroll |= SCROLL_UP;
3803 if (((scrollInfo.nPage + scrollInfo.nPos) - 1) != scrollInfo.nMax)
3804 scroll |= SCROLL_DOWN;
3807 if (GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo))
3809 if (scrollInfo.nPos != scrollInfo.nMin)
3810 scroll |= SCROLL_LEFT;
3812 if (((scrollInfo.nPage + scrollInfo.nPos) - 1) != scrollInfo.nMax)
3813 scroll |= SCROLL_RIGHT;
3816 if (((coords_orig.x <= 0) && (scroll & SCROLL_LEFT)) ||
3817 ((coords_orig.y <= 0) && (scroll & SCROLL_UP)) ||
3818 ((coords_orig.x >= infoPtr->rcList.right) && (scroll & SCROLL_RIGHT)) ||
3819 ((coords_orig.y >= infoPtr->rcList.bottom) && (scroll & SCROLL_DOWN)))
3821 LISTVIEW_MarqueeHighlight(infoPtr, &coords_orig, &coords_offs, &offset, scroll);
3827 * Called whenever WM_MOUSEMOVE is received.
3830 * [I] infoPtr : valid pointer to the listview structure
3831 * [I] fwKeys : key indicator
3832 * [I] x,y : mouse position
3835 * 0 if the message is processed, non-zero if there was an error
3837 static LRESULT LISTVIEW_MouseMove(LISTVIEW_INFO *infoPtr, WORD fwKeys, INT x, INT y)
3839 TRACKMOUSEEVENT trackinfo;
3841 if (!(fwKeys & MK_LBUTTON))
3842 infoPtr->bLButtonDown = FALSE;
3844 if (infoPtr->bLButtonDown)
3848 LVHITTESTINFO lvHitTestInfo;
3849 WORD wDragWidth = GetSystemMetrics(SM_CXDRAG);
3850 WORD wDragHeight= GetSystemMetrics(SM_CYDRAG);
3852 if (infoPtr->bMarqueeSelect)
3862 LISTVIEW_GetOrigin(infoPtr, &offset);
3864 /* Ensure coordinates are within client bounds */
3865 coords_offs.x = max(min(x, infoPtr->rcList.right), 0);
3866 coords_offs.y = max(min(y, infoPtr->rcList.bottom), 0);
3868 /* Offset coordinates by the appropriate amount */
3869 coords_offs.x -= offset.x;
3870 coords_offs.y -= offset.y;
3872 /* Enable the timer if we're going outside our bounds, in case the user doesn't
3873 move the mouse again */
3875 if ((x <= 0) || (y <= 0) || (x >= infoPtr->rcList.right) ||
3876 (y >= infoPtr->rcList.bottom))
3878 if (!infoPtr->bScrolling)
3880 infoPtr->bScrolling = TRUE;
3881 SetTimer(infoPtr->hwndSelf, (UINT_PTR) infoPtr, 1, LISTVIEW_ScrollTimer);
3886 infoPtr->bScrolling = FALSE;
3887 KillTimer(infoPtr->hwndSelf, (UINT_PTR) infoPtr);
3890 LISTVIEW_MarqueeHighlight(infoPtr, &coords_orig, &coords_offs, &offset, 0);
3894 rect.left = infoPtr->ptClickPos.x - wDragWidth;
3895 rect.right = infoPtr->ptClickPos.x + wDragWidth;
3896 rect.top = infoPtr->ptClickPos.y - wDragHeight;
3897 rect.bottom = infoPtr->ptClickPos.y + wDragHeight;
3902 lvHitTestInfo.pt = tmp;
3903 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, TRUE);
3905 /* reset item marker */
3906 if (infoPtr->nLButtonDownItem != lvHitTestInfo.iItem)
3907 infoPtr->nLButtonDownItem = -1;
3909 if (!PtInRect(&rect, tmp))
3911 /* this path covers the following:
3912 1. WM_LBUTTONDOWN over selected item (sets focus on it)
3913 2. change focus with keys
3914 3. move mouse over item from step 1 selects it and moves focus on it */
3915 if (infoPtr->nLButtonDownItem != -1 &&
3916 !LISTVIEW_GetItemState(infoPtr, infoPtr->nLButtonDownItem, LVIS_SELECTED))
3920 lvItem.state = LVIS_FOCUSED | LVIS_SELECTED;
3921 lvItem.stateMask = LVIS_FOCUSED | LVIS_SELECTED;
3923 LISTVIEW_SetItemState(infoPtr, infoPtr->nLButtonDownItem, &lvItem);
3924 infoPtr->nLButtonDownItem = -1;
3927 if (!infoPtr->bDragging)
3929 lvHitTestInfo.pt = infoPtr->ptClickPos;
3930 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, TRUE);
3932 /* If the click is outside the range of an item, begin a
3933 highlight. If not, begin an item drag. */
3934 if (lvHitTestInfo.iItem == -1)
3938 /* If we're allowing multiple selections, send notification.
3939 If return value is non-zero, cancel. */
3940 if (!(infoPtr->dwStyle & LVS_SINGLESEL) && (notify_hdr(infoPtr, LVN_MARQUEEBEGIN, &hdr) == 0))
3942 /* Store the absolute coordinates of the click */
3944 LISTVIEW_GetOrigin(infoPtr, &offset);
3946 infoPtr->marqueeOrigin.x = infoPtr->ptClickPos.x - offset.x;
3947 infoPtr->marqueeOrigin.y = infoPtr->ptClickPos.y - offset.y;
3949 /* Begin selection and capture mouse */
3950 infoPtr->bMarqueeSelect = TRUE;
3951 SetCapture(infoPtr->hwndSelf);
3958 ZeroMemory(&nmlv, sizeof(nmlv));
3959 nmlv.iItem = lvHitTestInfo.iItem;
3960 nmlv.ptAction = infoPtr->ptClickPos;
3962 notify_listview(infoPtr, LVN_BEGINDRAG, &nmlv);
3963 infoPtr->bDragging = TRUE;
3971 /* see if we are supposed to be tracking mouse hovering */
3972 if (LISTVIEW_isHotTracking(infoPtr)) {
3973 /* fill in the trackinfo struct */
3974 trackinfo.cbSize = sizeof(TRACKMOUSEEVENT);
3975 trackinfo.dwFlags = TME_QUERY;
3976 trackinfo.hwndTrack = infoPtr->hwndSelf;
3977 trackinfo.dwHoverTime = infoPtr->dwHoverTime;
3979 /* see if we are already tracking this hwnd */
3980 _TrackMouseEvent(&trackinfo);
3982 if(!(trackinfo.dwFlags & TME_HOVER)) {
3983 trackinfo.dwFlags = TME_HOVER;
3985 /* call TRACKMOUSEEVENT so we receive WM_MOUSEHOVER messages */
3986 _TrackMouseEvent(&trackinfo);
3995 * Tests whether the item is assignable to a list with style lStyle
3997 static inline BOOL is_assignable_item(const LVITEMW *lpLVItem, LONG lStyle)
3999 if ( (lpLVItem->mask & LVIF_TEXT) &&
4000 (lpLVItem->pszText == LPSTR_TEXTCALLBACKW) &&
4001 (lStyle & (LVS_SORTASCENDING | LVS_SORTDESCENDING)) ) return FALSE;
4009 * Helper for LISTVIEW_SetItemT *only*: sets item attributes.
4012 * [I] infoPtr : valid pointer to the listview structure
4013 * [I] lpLVItem : valid pointer to new item attributes
4014 * [I] isNew : the item being set is being inserted
4015 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
4016 * [O] bChanged : will be set to TRUE if the item really changed
4022 static BOOL set_main_item(LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem, BOOL isNew, BOOL isW, BOOL *bChanged)
4028 /* stateMask is ignored for LVM_INSERTITEM */
4029 UINT stateMask = isNew ? ~0 : lpLVItem->stateMask;
4033 assert(lpLVItem->iItem >= 0 && lpLVItem->iItem < infoPtr->nItemCount);
4035 if (lpLVItem->mask == 0) return TRUE;
4037 if (infoPtr->dwStyle & LVS_OWNERDATA)
4039 /* a virtual listview only stores selection and focus */
4040 if (lpLVItem->mask & ~LVIF_STATE)
4046 HDPA hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, lpLVItem->iItem);
4047 lpItem = DPA_GetPtr(hdpaSubItems, 0);
4051 /* we need to get the lParam and state of the item */
4052 item.iItem = lpLVItem->iItem;
4053 item.iSubItem = lpLVItem->iSubItem;
4054 item.mask = LVIF_STATE | LVIF_PARAM;
4055 item.stateMask = (infoPtr->dwStyle & LVS_OWNERDATA) ? LVIS_FOCUSED | LVIS_SELECTED : ~0;
4059 if (!isNew && !LISTVIEW_GetItemW(infoPtr, &item)) return FALSE;
4061 TRACE("oldState=%x, newState=%x\n", item.state, lpLVItem->state);
4062 /* determine what fields will change */
4063 if ((lpLVItem->mask & LVIF_STATE) && ((item.state ^ lpLVItem->state) & stateMask & ~infoPtr->uCallbackMask))
4064 uChanged |= LVIF_STATE;
4066 if ((lpLVItem->mask & LVIF_IMAGE) && (lpItem->hdr.iImage != lpLVItem->iImage))
4067 uChanged |= LVIF_IMAGE;
4069 if ((lpLVItem->mask & LVIF_PARAM) && (lpItem->lParam != lpLVItem->lParam))
4070 uChanged |= LVIF_PARAM;
4072 if ((lpLVItem->mask & LVIF_INDENT) && (lpItem->iIndent != lpLVItem->iIndent))
4073 uChanged |= LVIF_INDENT;
4075 if ((lpLVItem->mask & LVIF_TEXT) && textcmpWT(lpItem->hdr.pszText, lpLVItem->pszText, isW))
4076 uChanged |= LVIF_TEXT;
4078 TRACE("uChanged=0x%x\n", uChanged);
4079 if (!uChanged) return TRUE;
4082 ZeroMemory(&nmlv, sizeof(NMLISTVIEW));
4083 nmlv.iItem = lpLVItem->iItem;
4084 nmlv.uNewState = (item.state & ~stateMask) | (lpLVItem->state & stateMask);
4085 nmlv.uOldState = item.state;
4086 nmlv.uChanged = uChanged;
4087 nmlv.lParam = item.lParam;
4089 /* send LVN_ITEMCHANGING notification, if the item is not being inserted */
4090 /* and we are _NOT_ virtual (LVS_OWNERDATA), and change notifications */
4092 if(lpItem && !isNew && infoPtr->bDoChangeNotify)
4094 HWND hwndSelf = infoPtr->hwndSelf;
4096 if (notify_listview(infoPtr, LVN_ITEMCHANGING, &nmlv))
4098 if (!IsWindow(hwndSelf))
4102 /* copy information */
4103 if (lpLVItem->mask & LVIF_TEXT)
4104 textsetptrT(&lpItem->hdr.pszText, lpLVItem->pszText, isW);
4106 if (lpLVItem->mask & LVIF_IMAGE)
4107 lpItem->hdr.iImage = lpLVItem->iImage;
4109 if (lpLVItem->mask & LVIF_PARAM)
4110 lpItem->lParam = lpLVItem->lParam;
4112 if (lpLVItem->mask & LVIF_INDENT)
4113 lpItem->iIndent = lpLVItem->iIndent;
4115 if (uChanged & LVIF_STATE)
4117 if (lpItem && (stateMask & ~infoPtr->uCallbackMask))
4119 lpItem->state &= ~stateMask;
4120 lpItem->state |= (lpLVItem->state & stateMask);
4122 if (lpLVItem->state & stateMask & ~infoPtr->uCallbackMask & LVIS_SELECTED)
4124 if (infoPtr->dwStyle & LVS_SINGLESEL) LISTVIEW_DeselectAllSkipItem(infoPtr, lpLVItem->iItem);
4125 ranges_additem(infoPtr->selectionRanges, lpLVItem->iItem);
4127 else if (stateMask & LVIS_SELECTED)
4129 ranges_delitem(infoPtr->selectionRanges, lpLVItem->iItem);
4131 /* if we are asked to change focus, and we manage it, do it */
4132 if (stateMask & ~infoPtr->uCallbackMask & LVIS_FOCUSED)
4134 if (lpLVItem->state & LVIS_FOCUSED)
4136 if (infoPtr->nFocusedItem != -1)
4138 /* remove current focus */
4139 item.mask = LVIF_STATE;
4141 item.stateMask = LVIS_FOCUSED;
4143 /* recurse with redrawing an item */
4144 LISTVIEW_SetItemState(infoPtr, infoPtr->nFocusedItem, &item);
4147 infoPtr->nFocusedItem = lpLVItem->iItem;
4148 LISTVIEW_EnsureVisible(infoPtr, lpLVItem->iItem, infoPtr->uView == LV_VIEW_LIST);
4150 else if (infoPtr->nFocusedItem == lpLVItem->iItem)
4152 infoPtr->nFocusedItem = -1;
4157 /* if we're inserting the item, we're done */
4158 if (isNew) return TRUE;
4160 /* send LVN_ITEMCHANGED notification */
4161 if (lpLVItem->mask & LVIF_PARAM) nmlv.lParam = lpLVItem->lParam;
4162 if (infoPtr->bDoChangeNotify) notify_listview(infoPtr, LVN_ITEMCHANGED, &nmlv);
4169 * Helper for LISTVIEW_{Set,Insert}ItemT *only*: sets subitem attributes.
4172 * [I] infoPtr : valid pointer to the listview structure
4173 * [I] lpLVItem : valid pointer to new subitem attributes
4174 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
4175 * [O] bChanged : will be set to TRUE if the item really changed
4181 static BOOL set_sub_item(const LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem, BOOL isW, BOOL *bChanged)
4184 SUBITEM_INFO *lpSubItem;
4186 /* we do not support subitems for virtual listviews */
4187 if (infoPtr->dwStyle & LVS_OWNERDATA) return FALSE;
4189 /* set subitem only if column is present */
4190 if (lpLVItem->iSubItem >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
4192 /* First do some sanity checks */
4193 /* The LVIF_STATE flag is valid for subitems, but does not appear to be
4194 particularly useful. We currently do not actually do anything with
4195 the flag on subitems.
4197 if (lpLVItem->mask & ~(LVIF_TEXT | LVIF_IMAGE | LVIF_STATE)) return FALSE;
4198 if (!(lpLVItem->mask & (LVIF_TEXT | LVIF_IMAGE | LVIF_STATE))) return TRUE;
4200 /* get the subitem structure, and create it if not there */
4201 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, lpLVItem->iItem);
4202 assert (hdpaSubItems);
4204 lpSubItem = LISTVIEW_GetSubItemPtr(hdpaSubItems, lpLVItem->iSubItem);
4207 SUBITEM_INFO *tmpSubItem;
4210 lpSubItem = Alloc(sizeof(SUBITEM_INFO));
4211 if (!lpSubItem) return FALSE;
4212 /* we could binary search here, if need be...*/
4213 for (i = 1; i < DPA_GetPtrCount(hdpaSubItems); i++)
4215 tmpSubItem = DPA_GetPtr(hdpaSubItems, i);
4216 if (tmpSubItem->iSubItem > lpLVItem->iSubItem) break;
4218 if (DPA_InsertPtr(hdpaSubItems, i, lpSubItem) == -1)
4223 lpSubItem->iSubItem = lpLVItem->iSubItem;
4224 lpSubItem->hdr.iImage = I_IMAGECALLBACK;
4228 if (lpLVItem->mask & LVIF_IMAGE)
4229 if (lpSubItem->hdr.iImage != lpLVItem->iImage)
4231 lpSubItem->hdr.iImage = lpLVItem->iImage;
4235 if ((lpLVItem->mask & LVIF_TEXT) && textcmpWT(lpSubItem->hdr.pszText, lpLVItem->pszText, isW))
4237 textsetptrT(&lpSubItem->hdr.pszText, lpLVItem->pszText, isW);
4246 * Sets item attributes.
4249 * [I] infoPtr : valid pointer to the listview structure
4250 * [I] lpLVItem : new item attributes
4251 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
4257 static BOOL LISTVIEW_SetItemT(LISTVIEW_INFO *infoPtr, LVITEMW *lpLVItem, BOOL isW)
4259 HWND hwndSelf = infoPtr->hwndSelf;
4260 LPWSTR pszText = NULL;
4261 BOOL bResult, bChanged = FALSE;
4263 TRACE("(lpLVItem=%s, isW=%d)\n", debuglvitem_t(lpLVItem, isW), isW);
4265 if (!lpLVItem || lpLVItem->iItem < 0 || lpLVItem->iItem >= infoPtr->nItemCount)
4268 /* For efficiency, we transform the lpLVItem->pszText to Unicode here */
4269 if ((lpLVItem->mask & LVIF_TEXT) && is_textW(lpLVItem->pszText))
4271 pszText = lpLVItem->pszText;
4272 lpLVItem->pszText = textdupTtoW(lpLVItem->pszText, isW);
4275 /* actually set the fields */
4276 if (!is_assignable_item(lpLVItem, infoPtr->dwStyle)) return FALSE;
4278 if (lpLVItem->iSubItem)
4279 bResult = set_sub_item(infoPtr, lpLVItem, TRUE, &bChanged);
4281 bResult = set_main_item(infoPtr, lpLVItem, FALSE, TRUE, &bChanged);
4282 if (!IsWindow(hwndSelf))
4285 /* redraw item, if necessary */
4286 if (bChanged && !infoPtr->bIsDrawing)
4288 /* this little optimization eliminates some nasty flicker */
4289 if ( infoPtr->uView == LV_VIEW_DETAILS && !(infoPtr->dwStyle & LVS_OWNERDRAWFIXED) &&
4290 !(infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) &&
4291 lpLVItem->iSubItem > 0 && lpLVItem->iSubItem <= DPA_GetPtrCount(infoPtr->hdpaColumns) )
4292 LISTVIEW_InvalidateSubItem(infoPtr, lpLVItem->iItem, lpLVItem->iSubItem);
4294 LISTVIEW_InvalidateItem(infoPtr, lpLVItem->iItem);
4299 textfreeT(lpLVItem->pszText, isW);
4300 lpLVItem->pszText = pszText;
4308 * Retrieves the index of the item at coordinate (0, 0) of the client area.
4311 * [I] infoPtr : valid pointer to the listview structure
4316 static INT LISTVIEW_GetTopIndex(const LISTVIEW_INFO *infoPtr)
4319 SCROLLINFO scrollInfo;
4321 scrollInfo.cbSize = sizeof(SCROLLINFO);
4322 scrollInfo.fMask = SIF_POS;
4324 if (infoPtr->uView == LV_VIEW_LIST)
4326 if (GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo))
4327 nItem = scrollInfo.nPos * LISTVIEW_GetCountPerColumn(infoPtr);
4329 else if (infoPtr->uView == LV_VIEW_DETAILS)
4331 if (GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
4332 nItem = scrollInfo.nPos;
4336 if (GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
4337 nItem = LISTVIEW_GetCountPerRow(infoPtr) * (scrollInfo.nPos / infoPtr->nItemHeight);
4340 TRACE("nItem=%d\n", nItem);
4348 * Erases the background of the given rectangle
4351 * [I] infoPtr : valid pointer to the listview structure
4352 * [I] hdc : device context handle
4353 * [I] lprcBox : clipping rectangle
4359 static inline BOOL LISTVIEW_FillBkgnd(const LISTVIEW_INFO *infoPtr, HDC hdc, const RECT *lprcBox)
4361 if (!infoPtr->hBkBrush) return FALSE;
4363 TRACE("(hdc=%p, lprcBox=%s, hBkBrush=%p)\n", hdc, wine_dbgstr_rect(lprcBox), infoPtr->hBkBrush);
4365 return FillRect(hdc, lprcBox, infoPtr->hBkBrush);
4373 * [I] infoPtr : valid pointer to the listview structure
4374 * [I] hdc : device context handle
4375 * [I] nItem : item index
4376 * [I] nSubItem : subitem index
4377 * [I] pos : item position in client coordinates
4378 * [I] cdmode : custom draw mode
4384 static BOOL LISTVIEW_DrawItem(LISTVIEW_INFO *infoPtr, HDC hdc, INT nItem, INT nSubItem, POINT pos, DWORD cdmode)
4387 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
4388 static WCHAR szCallback[] = { '(', 'c', 'a', 'l', 'l', 'b', 'a', 'c', 'k', ')', 0 };
4389 DWORD cdsubitemmode = CDRF_DODEFAULT;
4391 RECT rcSelect, rcBox, rcIcon, rcLabel, rcStateIcon;
4392 NMLVCUSTOMDRAW nmlvcd;
4397 TRACE("(hdc=%p, nItem=%d, nSubItem=%d, pos=%s)\n", hdc, nItem, nSubItem, wine_dbgstr_point(&pos));
4399 /* get information needed for drawing the item */
4400 lvItem.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_PARAM;
4401 if (nSubItem == 0) lvItem.mask |= LVIF_STATE;
4402 if (infoPtr->uView == LV_VIEW_DETAILS) lvItem.mask |= LVIF_INDENT;
4403 lvItem.stateMask = LVIS_SELECTED | LVIS_FOCUSED | LVIS_STATEIMAGEMASK;
4404 lvItem.iItem = nItem;
4405 lvItem.iSubItem = nSubItem;
4408 lvItem.cchTextMax = DISP_TEXT_SIZE;
4409 lvItem.pszText = szDispText;
4410 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return FALSE;
4411 if (nSubItem > 0 && (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT))
4412 lvItem.state = LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED);
4413 if (lvItem.pszText == LPSTR_TEXTCALLBACKW) lvItem.pszText = szCallback;
4414 TRACE(" lvItem=%s\n", debuglvitem_t(&lvItem, TRUE));
4416 /* now check if we need to update the focus rectangle */
4417 lprcFocus = infoPtr->bFocus && (lvItem.state & LVIS_FOCUSED) ? &infoPtr->rcFocus : 0;
4419 if (!lprcFocus) lvItem.state &= ~LVIS_FOCUSED;
4420 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, &rcBox, &rcSelect, &rcIcon, &rcStateIcon, &rcLabel);
4421 OffsetRect(&rcBox, pos.x, pos.y);
4422 OffsetRect(&rcSelect, pos.x, pos.y);
4423 OffsetRect(&rcIcon, pos.x, pos.y);
4424 OffsetRect(&rcStateIcon, pos.x, pos.y);
4425 OffsetRect(&rcLabel, pos.x, pos.y);
4426 TRACE(" rcBox=%s, rcSelect=%s, rcIcon=%s. rcLabel=%s\n",
4427 wine_dbgstr_rect(&rcBox), wine_dbgstr_rect(&rcSelect),
4428 wine_dbgstr_rect(&rcIcon), wine_dbgstr_rect(&rcLabel));
4430 /* fill in the custom draw structure */
4431 customdraw_fill(&nmlvcd, infoPtr, hdc, &rcBox, &lvItem);
4433 hOldFont = GetCurrentObject(hdc, OBJ_FONT);
4434 if (nSubItem > 0) cdmode = infoPtr->cditemmode;
4435 if (cdmode & CDRF_SKIPDEFAULT) goto postpaint;
4436 if (cdmode & CDRF_NOTIFYITEMDRAW)
4437 cdsubitemmode = notify_customdraw(infoPtr, CDDS_PREPAINT, &nmlvcd);
4438 if (nSubItem == 0) infoPtr->cditemmode = cdsubitemmode;
4439 if (cdsubitemmode & CDRF_SKIPDEFAULT) goto postpaint;
4440 /* we have to send a CDDS_SUBITEM customdraw explicitly for subitem 0 */
4441 if (nSubItem == 0 && cdsubitemmode == CDRF_NOTIFYITEMDRAW)
4443 cdsubitemmode = notify_customdraw(infoPtr, CDDS_SUBITEM | CDDS_ITEMPREPAINT, &nmlvcd);
4444 if (cdsubitemmode & CDRF_SKIPDEFAULT) goto postpaint;
4446 if (nSubItem == 0 || (cdmode & CDRF_NOTIFYITEMDRAW))
4447 prepaint_setup(infoPtr, hdc, &nmlvcd, FALSE);
4448 else if ((infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) == FALSE)
4449 prepaint_setup(infoPtr, hdc, &nmlvcd, TRUE);
4451 /* in full row select, subitems, will just use main item's colors */
4452 if (nSubItem && infoPtr->uView == LV_VIEW_DETAILS && (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT))
4453 nmlvcd.clrTextBk = CLR_NONE;
4455 /* FIXME: temporary hack */
4456 rcSelect.left = rcLabel.left;
4458 /* draw the selection background, if we're drawing the main item */
4461 /* in icon mode, the label rect is really what we want to draw the
4463 if (infoPtr->uView == LV_VIEW_ICON)
4466 if (infoPtr->uView == LV_VIEW_DETAILS && (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT))
4468 /* we have to update left focus bound too if item isn't in leftmost column
4469 and reduce right box bound */
4470 if (DPA_GetPtrCount(infoPtr->hdpaColumns) > 0)
4474 if ((leftmost = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, 0, 0)))
4476 INT Originx = pos.x - LISTVIEW_GetColumnInfo(infoPtr, 0)->rcHeader.left;
4477 INT index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX,
4478 DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, 0);
4480 rcBox.right = LISTVIEW_GetColumnInfo(infoPtr, index)->rcHeader.right + Originx;
4481 rcSelect.left = LISTVIEW_GetColumnInfo(infoPtr, leftmost)->rcHeader.left + Originx;
4485 rcSelect.right = rcBox.right;
4488 if (nmlvcd.clrTextBk != CLR_NONE)
4489 ExtTextOutW(hdc, rcSelect.left, rcSelect.top, ETO_OPAQUE, &rcSelect, NULL, 0, NULL);
4490 /* store new focus rectangle */
4491 if (infoPtr->nFocusedItem == nItem) infoPtr->rcFocus = rcSelect;
4495 if (infoPtr->himlState && STATEIMAGEINDEX(lvItem.state) && (nSubItem == 0))
4497 UINT uStateImage = STATEIMAGEINDEX(lvItem.state);
4500 TRACE("uStateImage=%d\n", uStateImage);
4501 ImageList_Draw(infoPtr->himlState, uStateImage - 1, hdc,
4502 rcStateIcon.left, rcStateIcon.top, ILD_NORMAL);
4507 himl = (infoPtr->uView == LV_VIEW_ICON ? infoPtr->himlNormal : infoPtr->himlSmall);
4508 if (himl && lvItem.iImage >= 0 && !IsRectEmpty(&rcIcon))
4510 TRACE("iImage=%d\n", lvItem.iImage);
4511 ImageList_DrawEx(himl, lvItem.iImage, hdc, rcIcon.left, rcIcon.top,
4512 rcIcon.right - rcIcon.left, rcIcon.bottom - rcIcon.top, infoPtr->clrBk, CLR_DEFAULT,
4513 (lvItem.state & LVIS_SELECTED) && (infoPtr->bFocus) ? ILD_SELECTED : ILD_NORMAL);
4516 /* Don't bother painting item being edited */
4517 if (infoPtr->hwndEdit && nItem == infoPtr->nEditLabelItem && nSubItem == 0) goto postpaint;
4519 /* figure out the text drawing flags */
4520 uFormat = (infoPtr->uView == LV_VIEW_ICON ? (lprcFocus ? LV_FL_DT_FLAGS : LV_ML_DT_FLAGS) : LV_SL_DT_FLAGS);
4521 if (infoPtr->uView == LV_VIEW_ICON)
4522 uFormat = (lprcFocus ? LV_FL_DT_FLAGS : LV_ML_DT_FLAGS);
4525 switch (LISTVIEW_GetColumnInfo(infoPtr, nSubItem)->fmt & LVCFMT_JUSTIFYMASK)
4527 case LVCFMT_RIGHT: uFormat |= DT_RIGHT; break;
4528 case LVCFMT_CENTER: uFormat |= DT_CENTER; break;
4529 default: uFormat |= DT_LEFT;
4532 if (!(uFormat & (DT_RIGHT | DT_CENTER)))
4534 if (himl && lvItem.iImage >= 0 && !IsRectEmpty(&rcIcon)) rcLabel.left += IMAGE_PADDING;
4535 else rcLabel.left += LABEL_HOR_PADDING;
4537 else if (uFormat & DT_RIGHT) rcLabel.right -= LABEL_HOR_PADDING;
4539 /* for GRIDLINES reduce the bottom so the text formats correctly */
4540 if (infoPtr->uView == LV_VIEW_DETAILS && infoPtr->dwLvExStyle & LVS_EX_GRIDLINES)
4543 DrawTextW(hdc, lvItem.pszText, -1, &rcLabel, uFormat);
4546 if (cdsubitemmode & CDRF_NOTIFYPOSTPAINT)
4547 notify_postpaint(infoPtr, &nmlvcd);
4548 if (cdsubitemmode & CDRF_NEWFONT)
4549 SelectObject(hdc, hOldFont);
4555 * Draws listview items when in owner draw mode.
4558 * [I] infoPtr : valid pointer to the listview structure
4559 * [I] hdc : device context handle
4564 static void LISTVIEW_RefreshOwnerDraw(const LISTVIEW_INFO *infoPtr, ITERATOR *i, HDC hdc, DWORD cdmode)
4566 UINT uID = (UINT)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
4567 DWORD cditemmode = CDRF_DODEFAULT;
4568 NMLVCUSTOMDRAW nmlvcd;
4569 POINT Origin, Position;
4575 ZeroMemory(&dis, sizeof(dis));
4577 /* Get scroll info once before loop */
4578 LISTVIEW_GetOrigin(infoPtr, &Origin);
4580 /* iterate through the invalidated rows */
4581 while(iterator_next(i))
4583 item.iItem = i->nItem;
4585 item.mask = LVIF_PARAM | LVIF_STATE;
4586 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
4587 if (!LISTVIEW_GetItemW(infoPtr, &item)) continue;
4589 dis.CtlType = ODT_LISTVIEW;
4591 dis.itemID = item.iItem;
4592 dis.itemAction = ODA_DRAWENTIRE;
4594 if (item.state & LVIS_SELECTED) dis.itemState |= ODS_SELECTED;
4595 if (infoPtr->bFocus && (item.state & LVIS_FOCUSED)) dis.itemState |= ODS_FOCUS;
4596 dis.hwndItem = infoPtr->hwndSelf;
4598 LISTVIEW_GetItemOrigin(infoPtr, dis.itemID, &Position);
4599 dis.rcItem.left = Position.x + Origin.x;
4600 dis.rcItem.right = dis.rcItem.left + infoPtr->nItemWidth;
4601 dis.rcItem.top = Position.y + Origin.y;
4602 dis.rcItem.bottom = dis.rcItem.top + infoPtr->nItemHeight;
4603 dis.itemData = item.lParam;
4605 TRACE("item=%s, rcItem=%s\n", debuglvitem_t(&item, TRUE), wine_dbgstr_rect(&dis.rcItem));
4608 * Even if we do not send the CDRF_NOTIFYITEMDRAW we need to fill the nmlvcd
4609 * structure for the rest. of the paint cycle
4611 customdraw_fill(&nmlvcd, infoPtr, hdc, &dis.rcItem, &item);
4612 if (cdmode & CDRF_NOTIFYITEMDRAW)
4613 cditemmode = notify_customdraw(infoPtr, CDDS_PREPAINT, &nmlvcd);
4615 if (!(cditemmode & CDRF_SKIPDEFAULT))
4617 prepaint_setup (infoPtr, hdc, &nmlvcd, FALSE);
4618 SendMessageW(infoPtr->hwndNotify, WM_DRAWITEM, dis.CtlID, (LPARAM)&dis);
4621 if (cditemmode & CDRF_NOTIFYPOSTPAINT)
4622 notify_postpaint(infoPtr, &nmlvcd);
4628 * Draws listview items when in report display mode.
4631 * [I] infoPtr : valid pointer to the listview structure
4632 * [I] hdc : device context handle
4633 * [I] cdmode : custom draw mode
4638 static void LISTVIEW_RefreshReport(LISTVIEW_INFO *infoPtr, ITERATOR *i, HDC hdc, DWORD cdmode)
4641 RECT rcClip, rcItem;
4642 POINT Origin, Position;
4649 /* figure out what to draw */
4650 rgntype = GetClipBox(hdc, &rcClip);
4651 if (rgntype == NULLREGION) return;
4653 /* Get scroll info once before loop */
4654 LISTVIEW_GetOrigin(infoPtr, &Origin);
4656 colRanges = ranges_create(DPA_GetPtrCount(infoPtr->hdpaColumns));
4658 /* narrow down the columns we need to paint */
4659 for(col = 0; col < DPA_GetPtrCount(infoPtr->hdpaColumns); col++)
4661 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, col, 0);
4663 LISTVIEW_GetHeaderRect(infoPtr, index, &rcItem);
4664 if ((rcItem.right + Origin.x >= rcClip.left) && (rcItem.left + Origin.x < rcClip.right))
4665 ranges_additem(colRanges, index);
4667 iterator_rangesitems(&j, colRanges);
4669 /* in full row select, we _have_ to draw the main item */
4670 if (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT)
4673 /* iterate through the invalidated rows */
4674 while(iterator_next(i))
4676 LISTVIEW_GetItemOrigin(infoPtr, i->nItem, &Position);
4677 Position.y += Origin.y;
4679 /* iterate through the invalidated columns */
4680 while(iterator_next(&j))
4682 LISTVIEW_GetHeaderRect(infoPtr, j.nItem, &rcItem);
4683 Position.x = (j.nItem == 0) ? rcItem.left + Origin.x : Origin.x;
4685 if (rgntype == COMPLEXREGION && !((infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) && j.nItem == 0))
4688 rcItem.bottom = infoPtr->nItemHeight;
4689 OffsetRect(&rcItem, Origin.x, Position.y);
4690 if (!RectVisible(hdc, &rcItem)) continue;
4693 LISTVIEW_DrawItem(infoPtr, hdc, i->nItem, j.nItem, Position, cdmode);
4696 iterator_destroy(&j);
4701 * Draws the gridlines if necessary when in report display mode.
4704 * [I] infoPtr : valid pointer to the listview structure
4705 * [I] hdc : device context handle
4710 static void LISTVIEW_RefreshReportGrid(LISTVIEW_INFO *infoPtr, HDC hdc)
4716 RECT rcClip, rcItem = {0};
4724 /* figure out what to draw */
4725 rgntype = GetClipBox(hdc, &rcClip);
4726 if (rgntype == NULLREGION) return;
4728 /* Get scroll info once before loop */
4729 LISTVIEW_GetOrigin(infoPtr, &Origin);
4731 colRanges = ranges_create(DPA_GetPtrCount(infoPtr->hdpaColumns));
4733 /* narrow down the columns we need to paint */
4734 for(col = 0; col < DPA_GetPtrCount(infoPtr->hdpaColumns); col++)
4736 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, col, 0);
4738 LISTVIEW_GetHeaderRect(infoPtr, index, &rcItem);
4739 if ((rcItem.right + Origin.x >= rcClip.left) && (rcItem.left + Origin.x < rcClip.right))
4740 ranges_additem(colRanges, index);
4743 /* is right most vertical line visible? */
4744 if (DPA_GetPtrCount(infoPtr->hdpaColumns) > 0)
4746 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, 0);
4747 LISTVIEW_GetHeaderRect(infoPtr, index, &rcItem);
4748 rmost = (rcItem.right + Origin.x < rcClip.right);
4751 if ((hPen = CreatePen( PS_SOLID, 1, comctl32_color.clr3dFace )))
4753 hOldPen = SelectObject ( hdc, hPen );
4755 /* draw the vertical lines for the columns */
4756 iterator_rangesitems(&j, colRanges);
4757 while(iterator_next(&j))
4759 LISTVIEW_GetHeaderRect(infoPtr, j.nItem, &rcItem);
4760 if (rcItem.left == 0) continue; /* skip leftmost column */
4761 rcItem.left += Origin.x;
4762 rcItem.right += Origin.x;
4763 rcItem.top = infoPtr->rcList.top;
4764 rcItem.bottom = infoPtr->rcList.bottom;
4765 TRACE("vert col=%d, rcItem=%s\n", j.nItem, wine_dbgstr_rect(&rcItem));
4766 MoveToEx (hdc, rcItem.left, rcItem.top, NULL);
4767 LineTo (hdc, rcItem.left, rcItem.bottom);
4769 iterator_destroy(&j);
4770 /* draw rightmost grid line if visible */
4773 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX,
4774 DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, 0);
4775 LISTVIEW_GetHeaderRect(infoPtr, index, &rcItem);
4777 rcItem.right += Origin.x;
4779 MoveToEx (hdc, rcItem.right, infoPtr->rcList.top, NULL);
4780 LineTo (hdc, rcItem.right, infoPtr->rcList.bottom);
4783 /* draw the horizontial lines for the rows */
4784 itemheight = LISTVIEW_CalculateItemHeight(infoPtr);
4785 rcItem.left = infoPtr->rcList.left;
4786 rcItem.right = infoPtr->rcList.right;
4787 rcItem.bottom = rcItem.top = Origin.y - 1;
4788 MoveToEx(hdc, rcItem.left, rcItem.top, NULL);
4789 LineTo(hdc, rcItem.right, rcItem.top);
4790 for(y=itemheight-1+Origin.y; y<=infoPtr->rcList.bottom; y+=itemheight)
4792 rcItem.bottom = rcItem.top = y;
4793 TRACE("horz rcItem=%s\n", wine_dbgstr_rect(&rcItem));
4794 MoveToEx (hdc, rcItem.left, rcItem.top, NULL);
4795 LineTo (hdc, rcItem.right, rcItem.top);
4798 SelectObject( hdc, hOldPen );
4799 DeleteObject( hPen );
4805 * Draws listview items when in list display mode.
4808 * [I] infoPtr : valid pointer to the listview structure
4809 * [I] hdc : device context handle
4810 * [I] cdmode : custom draw mode
4815 static void LISTVIEW_RefreshList(LISTVIEW_INFO *infoPtr, ITERATOR *i, HDC hdc, DWORD cdmode)
4817 POINT Origin, Position;
4819 /* Get scroll info once before loop */
4820 LISTVIEW_GetOrigin(infoPtr, &Origin);
4822 while(iterator_prev(i))
4824 LISTVIEW_GetItemOrigin(infoPtr, i->nItem, &Position);
4825 Position.x += Origin.x;
4826 Position.y += Origin.y;
4828 LISTVIEW_DrawItem(infoPtr, hdc, i->nItem, 0, Position, cdmode);
4835 * Draws listview items.
4838 * [I] infoPtr : valid pointer to the listview structure
4839 * [I] hdc : device context handle
4840 * [I] prcErase : rect to be erased before refresh (may be NULL)
4845 static void LISTVIEW_Refresh(LISTVIEW_INFO *infoPtr, HDC hdc, const RECT *prcErase)
4847 COLORREF oldTextColor = 0, oldBkColor = 0, oldClrTextBk, oldClrText;
4848 NMLVCUSTOMDRAW nmlvcd;
4855 HBITMAP hbmp = NULL;
4858 LISTVIEW_DUMP(infoPtr);
4860 if (infoPtr->dwLvExStyle & LVS_EX_DOUBLEBUFFER) {
4861 TRACE("double buffering\n");
4863 hdc = CreateCompatibleDC(hdcOrig);
4865 ERR("Failed to create DC for backbuffer\n");
4868 hbmp = CreateCompatibleBitmap(hdcOrig, infoPtr->rcList.right,
4869 infoPtr->rcList.bottom);
4871 ERR("Failed to create bitmap for backbuffer\n");
4876 SelectObject(hdc, hbmp);
4877 SelectObject(hdc, infoPtr->hFont);
4879 /* Save dc values we're gonna trash while drawing
4880 * FIXME: Should be done in LISTVIEW_DrawItem() */
4881 hOldFont = SelectObject(hdc, infoPtr->hFont);
4882 oldBkMode = GetBkMode(hdc);
4883 oldBkColor = GetBkColor(hdc);
4884 oldTextColor = GetTextColor(hdc);
4887 infoPtr->bIsDrawing = TRUE;
4890 LISTVIEW_FillBkgnd(infoPtr, hdc, prcErase);
4891 } else if (infoPtr->dwLvExStyle & LVS_EX_DOUBLEBUFFER) {
4892 /* If no erasing was done (usually because RedrawWindow was called
4893 * with RDW_INVALIDATE only) we need to copy the old contents into
4894 * the backbuffer before continuing. */
4895 BitBlt(hdc, infoPtr->rcList.left, infoPtr->rcList.top,
4896 infoPtr->rcList.right - infoPtr->rcList.left,
4897 infoPtr->rcList.bottom - infoPtr->rcList.top,
4898 hdcOrig, infoPtr->rcList.left, infoPtr->rcList.top, SRCCOPY);
4901 /* FIXME: Shouldn't need to do this */
4902 oldClrTextBk = infoPtr->clrTextBk;
4903 oldClrText = infoPtr->clrText;
4905 infoPtr->cditemmode = CDRF_DODEFAULT;
4907 GetClientRect(infoPtr->hwndSelf, &rcClient);
4908 customdraw_fill(&nmlvcd, infoPtr, hdc, &rcClient, 0);
4909 cdmode = notify_customdraw(infoPtr, CDDS_PREPAINT, &nmlvcd);
4910 if (cdmode & CDRF_SKIPDEFAULT) goto enddraw;
4911 prepaint_setup(infoPtr, hdc, &nmlvcd, FALSE);
4913 /* Use these colors to draw the items */
4914 infoPtr->clrTextBk = nmlvcd.clrTextBk;
4915 infoPtr->clrText = nmlvcd.clrText;
4917 /* nothing to draw */
4918 if(infoPtr->nItemCount == 0) goto enddraw;
4920 /* figure out what we need to draw */
4921 iterator_visibleitems(&i, infoPtr, hdc);
4922 range = iterator_range(&i);
4924 /* send cache hint notification */
4925 if (infoPtr->dwStyle & LVS_OWNERDATA)
4929 ZeroMemory(&nmlv, sizeof(NMLVCACHEHINT));
4930 nmlv.iFrom = range.lower;
4931 nmlv.iTo = range.upper - 1;
4932 notify_hdr(infoPtr, LVN_ODCACHEHINT, &nmlv.hdr);
4935 if ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (infoPtr->uView == LV_VIEW_DETAILS))
4936 LISTVIEW_RefreshOwnerDraw(infoPtr, &i, hdc, cdmode);
4939 if (infoPtr->uView == LV_VIEW_DETAILS)
4940 LISTVIEW_RefreshReport(infoPtr, &i, hdc, cdmode);
4941 else /* LV_VIEW_LIST, LV_VIEW_ICON or LV_VIEW_SMALLICON */
4942 LISTVIEW_RefreshList(infoPtr, &i, hdc, cdmode);
4944 /* if we have a focus rect and it's visible, draw it */
4945 if (infoPtr->bFocus && range.lower <= infoPtr->nFocusedItem &&
4946 (range.upper - 1) >= infoPtr->nFocusedItem)
4947 LISTVIEW_DrawFocusRect(infoPtr, hdc);
4949 iterator_destroy(&i);
4952 /* For LVS_EX_GRIDLINES go and draw lines */
4953 /* This includes the case where there were *no* items */
4954 if ((infoPtr->uView == LV_VIEW_DETAILS) && infoPtr->dwLvExStyle & LVS_EX_GRIDLINES)
4955 LISTVIEW_RefreshReportGrid(infoPtr, hdc);
4957 /* Draw marquee rectangle if appropriate */
4958 if (infoPtr->bMarqueeSelect)
4959 DrawFocusRect(hdc, &infoPtr->marqueeDrawRect);
4961 if (cdmode & CDRF_NOTIFYPOSTPAINT)
4962 notify_postpaint(infoPtr, &nmlvcd);
4964 infoPtr->clrTextBk = oldClrTextBk;
4965 infoPtr->clrText = oldClrText;
4968 BitBlt(hdcOrig, infoPtr->rcList.left, infoPtr->rcList.top,
4969 infoPtr->rcList.right - infoPtr->rcList.left,
4970 infoPtr->rcList.bottom - infoPtr->rcList.top,
4971 hdc, infoPtr->rcList.left, infoPtr->rcList.top, SRCCOPY);
4976 SelectObject(hdc, hOldFont);
4977 SetBkMode(hdc, oldBkMode);
4978 SetBkColor(hdc, oldBkColor);
4979 SetTextColor(hdc, oldTextColor);
4982 infoPtr->bIsDrawing = FALSE;
4988 * Calculates the approximate width and height of a given number of items.
4991 * [I] infoPtr : valid pointer to the listview structure
4992 * [I] nItemCount : number of items
4993 * [I] wWidth : width
4994 * [I] wHeight : height
4997 * Returns a DWORD. The width in the low word and the height in high word.
4999 static DWORD LISTVIEW_ApproximateViewRect(const LISTVIEW_INFO *infoPtr, INT nItemCount,
5000 WORD wWidth, WORD wHeight)
5002 INT nItemCountPerColumn = 1;
5003 INT nColumnCount = 0;
5004 DWORD dwViewRect = 0;
5006 if (nItemCount == -1)
5007 nItemCount = infoPtr->nItemCount;
5009 if (infoPtr->uView == LV_VIEW_LIST)
5011 if (wHeight == 0xFFFF)
5013 /* use current height */
5014 wHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
5017 if (wHeight < infoPtr->nItemHeight)
5018 wHeight = infoPtr->nItemHeight;
5022 if (infoPtr->nItemHeight > 0)
5024 nItemCountPerColumn = wHeight / infoPtr->nItemHeight;
5025 if (nItemCountPerColumn == 0)
5026 nItemCountPerColumn = 1;
5028 if (nItemCount % nItemCountPerColumn != 0)
5029 nColumnCount = nItemCount / nItemCountPerColumn;
5031 nColumnCount = nItemCount / nItemCountPerColumn + 1;
5035 /* Microsoft padding magic */
5036 wHeight = nItemCountPerColumn * infoPtr->nItemHeight + 2;
5037 wWidth = nColumnCount * infoPtr->nItemWidth + 2;
5039 dwViewRect = MAKELONG(wWidth, wHeight);
5041 else if (infoPtr->uView == LV_VIEW_DETAILS)
5045 if (infoPtr->nItemCount > 0)
5047 LISTVIEW_GetItemBox(infoPtr, 0, &rcBox);
5048 wWidth = rcBox.right - rcBox.left;
5049 wHeight = (rcBox.bottom - rcBox.top) * nItemCount;
5053 /* use current height and width */
5054 if (wHeight == 0xffff)
5055 wHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
5056 if (wWidth == 0xffff)
5057 wWidth = infoPtr->rcList.right - infoPtr->rcList.left;
5060 dwViewRect = MAKELONG(wWidth, wHeight);
5062 else if (infoPtr->uView == LV_VIEW_ICON)
5068 nItemWidth = infoPtr->iconSpacing.cx;
5069 nItemHeight = infoPtr->iconSpacing.cy;
5071 if (nItemCount == -1)
5072 nItemCount = infoPtr->nItemCount;
5074 if (wWidth == 0xffff)
5075 wWidth = infoPtr->rcList.right - infoPtr->rcList.left;
5077 if (wWidth < nItemWidth)
5078 wWidth = nItemWidth;
5080 cols = wWidth / nItemWidth;
5081 if (cols > nItemCount)
5088 rows = nItemCount / cols;
5089 if (nItemCount % cols)
5095 wHeight = (nItemHeight * rows)+2;
5096 wWidth = (nItemWidth * cols)+2;
5098 dwViewRect = MAKELONG(wWidth, wHeight);
5100 else if (infoPtr->uView == LV_VIEW_SMALLICON)
5101 FIXME("uView == LV_VIEW_SMALLICON: not implemented\n");
5108 * Cancel edit label with saving item text.
5111 * [I] infoPtr : valid pointer to the listview structure
5114 * Always returns TRUE.
5116 static LRESULT LISTVIEW_CancelEditLabel(LISTVIEW_INFO *infoPtr)
5118 if (infoPtr->hwndEdit)
5120 /* handle value will be lost after LISTVIEW_EndEditLabelT */
5121 HWND edit = infoPtr->hwndEdit;
5123 LISTVIEW_EndEditLabelT(infoPtr, TRUE, IsWindowUnicode(infoPtr->hwndEdit));
5124 SendMessageW(edit, WM_CLOSE, 0, 0);
5132 * Create a drag image list for the specified item.
5135 * [I] infoPtr : valid pointer to the listview structure
5136 * [I] iItem : index of item
5137 * [O] lppt : Upper-left corner of the image
5140 * Returns a handle to the image list if successful, NULL otherwise.
5142 static HIMAGELIST LISTVIEW_CreateDragImage(LISTVIEW_INFO *infoPtr, INT iItem, LPPOINT lppt)
5148 HBITMAP hbmp, hOldbmp;
5149 HIMAGELIST dragList = 0;
5150 TRACE("iItem=%d Count=%d\n", iItem, infoPtr->nItemCount);
5152 if (iItem < 0 || iItem >= infoPtr->nItemCount)
5155 rcItem.left = LVIR_BOUNDS;
5156 if (!LISTVIEW_GetItemRect(infoPtr, iItem, &rcItem))
5159 lppt->x = rcItem.left;
5160 lppt->y = rcItem.top;
5162 size.cx = rcItem.right - rcItem.left;
5163 size.cy = rcItem.bottom - rcItem.top;
5165 hdcOrig = GetDC(infoPtr->hwndSelf);
5166 hdc = CreateCompatibleDC(hdcOrig);
5167 hbmp = CreateCompatibleBitmap(hdcOrig, size.cx, size.cy);
5168 hOldbmp = SelectObject(hdc, hbmp);
5170 rcItem.left = rcItem.top = 0;
5171 rcItem.right = size.cx;
5172 rcItem.bottom = size.cy;
5173 FillRect(hdc, &rcItem, infoPtr->hBkBrush);
5176 if (LISTVIEW_DrawItem(infoPtr, hdc, iItem, 0, pos, infoPtr->cditemmode))
5178 dragList = ImageList_Create(size.cx, size.cy, ILC_COLOR, 10, 10);
5179 SelectObject(hdc, hOldbmp);
5180 ImageList_Add(dragList, hbmp, 0);
5183 SelectObject(hdc, hOldbmp);
5187 ReleaseDC(infoPtr->hwndSelf, hdcOrig);
5189 TRACE("ret=%p\n", dragList);
5197 * Removes all listview items and subitems.
5200 * [I] infoPtr : valid pointer to the listview structure
5206 static BOOL LISTVIEW_DeleteAllItems(LISTVIEW_INFO *infoPtr, BOOL destroy)
5209 HDPA hdpaSubItems = NULL;
5218 /* we do it directly, to avoid notifications */
5219 ranges_clear(infoPtr->selectionRanges);
5220 infoPtr->nSelectionMark = -1;
5221 infoPtr->nFocusedItem = -1;
5222 SetRectEmpty(&infoPtr->rcFocus);
5223 /* But we are supposed to leave nHotItem as is! */
5226 /* send LVN_DELETEALLITEMS notification */
5227 ZeroMemory(&nmlv, sizeof(NMLISTVIEW));
5229 bSuppress = notify_listview(infoPtr, LVN_DELETEALLITEMS, &nmlv);
5231 for (i = infoPtr->nItemCount - 1; i >= 0; i--)
5233 if (!(infoPtr->dwStyle & LVS_OWNERDATA))
5235 /* send LVN_DELETEITEM notification, if not suppressed
5236 and if it is not a virtual listview */
5237 if (!bSuppress) notify_deleteitem(infoPtr, i);
5238 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, i);
5239 lpItem = DPA_GetPtr(hdpaSubItems, 0);
5240 /* free id struct */
5241 j = DPA_GetPtrIndex(infoPtr->hdpaItemIds, lpItem->id);
5242 lpID = DPA_GetPtr(infoPtr->hdpaItemIds, j);
5243 DPA_DeletePtr(infoPtr->hdpaItemIds, j);
5245 /* both item and subitem start with ITEMHDR header */
5246 for (j = 0; j < DPA_GetPtrCount(hdpaSubItems); j++)
5248 hdrItem = DPA_GetPtr(hdpaSubItems, j);
5249 if (is_textW(hdrItem->pszText)) Free(hdrItem->pszText);
5252 DPA_Destroy(hdpaSubItems);
5253 DPA_DeletePtr(infoPtr->hdpaItems, i);
5255 DPA_DeletePtr(infoPtr->hdpaPosX, i);
5256 DPA_DeletePtr(infoPtr->hdpaPosY, i);
5257 infoPtr->nItemCount --;
5262 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
5263 LISTVIEW_UpdateScroll(infoPtr);
5265 LISTVIEW_InvalidateList(infoPtr);
5272 * Scrolls, and updates the columns, when a column is changing width.
5275 * [I] infoPtr : valid pointer to the listview structure
5276 * [I] nColumn : column to scroll
5277 * [I] dx : amount of scroll, in pixels
5282 static void LISTVIEW_ScrollColumns(LISTVIEW_INFO *infoPtr, INT nColumn, INT dx)
5284 COLUMN_INFO *lpColumnInfo;
5290 if (nColumn < 0 || DPA_GetPtrCount(infoPtr->hdpaColumns) < 1) return;
5291 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, min(nColumn, DPA_GetPtrCount(infoPtr->hdpaColumns) - 1));
5292 rcCol = lpColumnInfo->rcHeader;
5293 if (nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns))
5294 rcCol.left = rcCol.right;
5296 /* adjust the other columns */
5297 hdi.mask = HDI_ORDER;
5298 if (SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nColumn, (LPARAM)&hdi))
5300 INT nOrder = hdi.iOrder;
5301 for (nCol = 0; nCol < DPA_GetPtrCount(infoPtr->hdpaColumns); nCol++)
5303 hdi.mask = HDI_ORDER;
5304 SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nCol, (LPARAM)&hdi);
5305 if (hdi.iOrder >= nOrder) {
5306 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, nCol);
5307 lpColumnInfo->rcHeader.left += dx;
5308 lpColumnInfo->rcHeader.right += dx;
5313 /* do not update screen if not in report mode */
5314 if (!is_redrawing(infoPtr) || infoPtr->uView != LV_VIEW_DETAILS) return;
5316 /* Need to reset the item width when inserting a new column */
5317 infoPtr->nItemWidth += dx;
5319 LISTVIEW_UpdateScroll(infoPtr);
5320 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
5322 /* scroll to cover the deleted column, and invalidate for redraw */
5323 rcOld = infoPtr->rcList;
5324 rcOld.left = ptOrigin.x + rcCol.left + dx;
5325 ScrollWindowEx(infoPtr->hwndSelf, dx, 0, &rcOld, &rcOld, 0, 0, SW_ERASE | SW_INVALIDATE);
5330 * Removes a column from the listview control.
5333 * [I] infoPtr : valid pointer to the listview structure
5334 * [I] nColumn : column index
5340 static BOOL LISTVIEW_DeleteColumn(LISTVIEW_INFO *infoPtr, INT nColumn)
5344 TRACE("nColumn=%d\n", nColumn);
5346 if (nColumn < 0 || DPA_GetPtrCount(infoPtr->hdpaColumns) == 0
5347 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
5349 /* While the MSDN specifically says that column zero should not be deleted,
5350 what actually happens is that the column itself is deleted but no items or subitems
5354 LISTVIEW_GetHeaderRect(infoPtr, nColumn, &rcCol);
5356 if (!SendMessageW(infoPtr->hwndHeader, HDM_DELETEITEM, nColumn, 0))
5359 Free(DPA_GetPtr(infoPtr->hdpaColumns, nColumn));
5360 DPA_DeletePtr(infoPtr->hdpaColumns, nColumn);
5362 if (!(infoPtr->dwStyle & LVS_OWNERDATA) && nColumn)
5364 SUBITEM_INFO *lpSubItem, *lpDelItem;
5366 INT nItem, nSubItem, i;
5368 for (nItem = 0; nItem < infoPtr->nItemCount; nItem++)
5370 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, nItem);
5373 for (i = 1; i < DPA_GetPtrCount(hdpaSubItems); i++)
5375 lpSubItem = DPA_GetPtr(hdpaSubItems, i);
5376 if (lpSubItem->iSubItem == nColumn)
5379 lpDelItem = lpSubItem;
5381 else if (lpSubItem->iSubItem > nColumn)
5383 lpSubItem->iSubItem--;
5387 /* if we found our subitem, zapp it */
5391 if (is_textW(lpDelItem->hdr.pszText))
5392 Free(lpDelItem->hdr.pszText);
5397 /* free dpa memory */
5398 DPA_DeletePtr(hdpaSubItems, nSubItem);
5403 /* update the other column info */
5404 LISTVIEW_UpdateItemSize(infoPtr);
5405 if(DPA_GetPtrCount(infoPtr->hdpaColumns) == 0)
5406 LISTVIEW_InvalidateList(infoPtr);
5408 LISTVIEW_ScrollColumns(infoPtr, nColumn, -(rcCol.right - rcCol.left));
5415 * Invalidates the listview after an item's insertion or deletion.
5418 * [I] infoPtr : valid pointer to the listview structure
5419 * [I] nItem : item index
5420 * [I] dir : -1 if deleting, 1 if inserting
5425 static void LISTVIEW_ScrollOnInsert(LISTVIEW_INFO *infoPtr, INT nItem, INT dir)
5427 INT nPerCol, nItemCol, nItemRow;
5431 /* if we don't refresh, what's the point of scrolling? */
5432 if (!is_redrawing(infoPtr)) return;
5434 assert (abs(dir) == 1);
5436 /* arrange icons if autoarrange is on */
5437 if (is_autoarrange(infoPtr))
5439 BOOL arrange = TRUE;
5440 if (dir < 0 && nItem >= infoPtr->nItemCount) arrange = FALSE;
5441 if (dir > 0 && nItem == infoPtr->nItemCount - 1) arrange = FALSE;
5442 if (arrange) LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
5445 /* scrollbars need updating */
5446 LISTVIEW_UpdateScroll(infoPtr);
5448 /* figure out the item's position */
5449 if (infoPtr->uView == LV_VIEW_DETAILS)
5450 nPerCol = infoPtr->nItemCount + 1;
5451 else if (infoPtr->uView == LV_VIEW_LIST)
5452 nPerCol = LISTVIEW_GetCountPerColumn(infoPtr);
5453 else /* LV_VIEW_ICON, or LV_VIEW_SMALLICON */
5456 nItemCol = nItem / nPerCol;
5457 nItemRow = nItem % nPerCol;
5458 LISTVIEW_GetOrigin(infoPtr, &Origin);
5460 /* move the items below up a slot */
5461 rcScroll.left = nItemCol * infoPtr->nItemWidth;
5462 rcScroll.top = nItemRow * infoPtr->nItemHeight;
5463 rcScroll.right = rcScroll.left + infoPtr->nItemWidth;
5464 rcScroll.bottom = nPerCol * infoPtr->nItemHeight;
5465 OffsetRect(&rcScroll, Origin.x, Origin.y);
5466 TRACE("rcScroll=%s, dx=%d\n", wine_dbgstr_rect(&rcScroll), dir * infoPtr->nItemHeight);
5467 if (IntersectRect(&rcScroll, &rcScroll, &infoPtr->rcList))
5469 TRACE("Scrolling rcScroll=%s, rcList=%s\n", wine_dbgstr_rect(&rcScroll), wine_dbgstr_rect(&infoPtr->rcList));
5470 ScrollWindowEx(infoPtr->hwndSelf, 0, dir * infoPtr->nItemHeight,
5471 &rcScroll, &rcScroll, 0, 0, SW_ERASE | SW_INVALIDATE);
5474 /* report has only that column, so we're done */
5475 if (infoPtr->uView == LV_VIEW_DETAILS) return;
5477 /* now for LISTs, we have to deal with the columns to the right */
5478 rcScroll.left = (nItemCol + 1) * infoPtr->nItemWidth;
5480 rcScroll.right = (infoPtr->nItemCount / nPerCol + 1) * infoPtr->nItemWidth;
5481 rcScroll.bottom = nPerCol * infoPtr->nItemHeight;
5482 OffsetRect(&rcScroll, Origin.x, Origin.y);
5483 if (IntersectRect(&rcScroll, &rcScroll, &infoPtr->rcList))
5484 ScrollWindowEx(infoPtr->hwndSelf, 0, dir * infoPtr->nItemHeight,
5485 &rcScroll, &rcScroll, 0, 0, SW_ERASE | SW_INVALIDATE);
5490 * Removes an item from the listview control.
5493 * [I] infoPtr : valid pointer to the listview structure
5494 * [I] nItem : item index
5500 static BOOL LISTVIEW_DeleteItem(LISTVIEW_INFO *infoPtr, INT nItem)
5503 const BOOL is_icon = (infoPtr->uView == LV_VIEW_SMALLICON || infoPtr->uView == LV_VIEW_ICON);
5505 TRACE("(nItem=%d)\n", nItem);
5507 if (nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
5509 /* remove selection, and focus */
5511 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
5512 LISTVIEW_SetItemState(infoPtr, nItem, &item);
5514 /* send LVN_DELETEITEM notification. */
5515 if (!notify_deleteitem(infoPtr, nItem)) return FALSE;
5517 /* we need to do this here, because we'll be deleting stuff */
5519 LISTVIEW_InvalidateItem(infoPtr, nItem);
5521 if (!(infoPtr->dwStyle & LVS_OWNERDATA))
5529 hdpaSubItems = DPA_DeletePtr(infoPtr->hdpaItems, nItem);
5530 lpItem = DPA_GetPtr(hdpaSubItems, 0);
5532 /* free id struct */
5533 i = DPA_GetPtrIndex(infoPtr->hdpaItemIds, lpItem->id);
5534 lpID = DPA_GetPtr(infoPtr->hdpaItemIds, i);
5535 DPA_DeletePtr(infoPtr->hdpaItemIds, i);
5537 for (i = 0; i < DPA_GetPtrCount(hdpaSubItems); i++)
5539 hdrItem = DPA_GetPtr(hdpaSubItems, i);
5540 if (is_textW(hdrItem->pszText)) Free(hdrItem->pszText);
5543 DPA_Destroy(hdpaSubItems);
5548 DPA_DeletePtr(infoPtr->hdpaPosX, nItem);
5549 DPA_DeletePtr(infoPtr->hdpaPosY, nItem);
5552 infoPtr->nItemCount--;
5553 LISTVIEW_ShiftIndices(infoPtr, nItem, -1);
5555 /* now is the invalidation fun */
5557 LISTVIEW_ScrollOnInsert(infoPtr, nItem, -1);
5564 * Callback implementation for editlabel control
5567 * [I] infoPtr : valid pointer to the listview structure
5568 * [I] storeText : store edit box text as item text
5569 * [I] isW : TRUE if psxText is Unicode, FALSE if it's ANSI
5575 static BOOL LISTVIEW_EndEditLabelT(LISTVIEW_INFO *infoPtr, BOOL storeText, BOOL isW)
5577 HWND hwndSelf = infoPtr->hwndSelf;
5578 NMLVDISPINFOW dispInfo;
5579 INT editedItem = infoPtr->nEditLabelItem;
5581 WCHAR *pszText = NULL;
5586 DWORD len = isW ? GetWindowTextLengthW(infoPtr->hwndEdit) : GetWindowTextLengthA(infoPtr->hwndEdit);
5590 if ((pszText = Alloc((len+1) * (isW ? sizeof(WCHAR) : sizeof(CHAR)))))
5592 if (isW) GetWindowTextW(infoPtr->hwndEdit, pszText, len+1);
5593 else GetWindowTextA(infoPtr->hwndEdit, (CHAR*)pszText, len+1);
5598 TRACE("(pszText=%s, isW=%d)\n", debugtext_t(pszText, isW), isW);
5600 infoPtr->nEditLabelItem = -1;
5601 infoPtr->hwndEdit = 0;
5603 ZeroMemory(&dispInfo, sizeof(dispInfo));
5604 dispInfo.item.mask = LVIF_PARAM | LVIF_STATE | LVIF_TEXT;
5605 dispInfo.item.iItem = editedItem;
5606 dispInfo.item.iSubItem = 0;
5607 dispInfo.item.stateMask = ~0;
5608 if (!LISTVIEW_GetItemW(infoPtr, &dispInfo.item))
5615 bSame = (lstrcmpW(dispInfo.item.pszText, pszText) == 0);
5618 LPWSTR tmp = textdupTtoW(pszText, FALSE);
5619 bSame = (lstrcmpW(dispInfo.item.pszText, tmp) == 0);
5620 textfreeT(tmp, FALSE);
5623 /* add the text from the edit in */
5624 dispInfo.item.mask |= LVIF_TEXT;
5625 dispInfo.item.pszText = bSame ? NULL : pszText;
5626 dispInfo.item.cchTextMax = bSame ? 0 : textlenT(pszText, isW);
5628 /* Do we need to update the Item Text */
5629 if (!notify_dispinfoT(infoPtr, LVN_ENDLABELEDITW, &dispInfo, isW))
5634 if (!IsWindow(hwndSelf))
5639 if (!pszText) return TRUE;
5646 if (!(infoPtr->dwStyle & LVS_OWNERDATA))
5648 HDPA hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, editedItem);
5649 ITEM_INFO* lpItem = DPA_GetPtr(hdpaSubItems, 0);
5650 if (lpItem && lpItem->hdr.pszText == LPSTR_TEXTCALLBACKW)
5652 LISTVIEW_InvalidateItem(infoPtr, editedItem);
5658 ZeroMemory(&dispInfo, sizeof(dispInfo));
5659 dispInfo.item.mask = LVIF_TEXT;
5660 dispInfo.item.iItem = editedItem;
5661 dispInfo.item.iSubItem = 0;
5662 dispInfo.item.pszText = pszText;
5663 dispInfo.item.cchTextMax = textlenT(pszText, isW);
5664 res = LISTVIEW_SetItemT(infoPtr, &dispInfo.item, isW);
5674 * Begin in place editing of specified list view item
5677 * [I] infoPtr : valid pointer to the listview structure
5678 * [I] nItem : item index
5679 * [I] isW : TRUE if it's a Unicode req, FALSE if ASCII
5685 static HWND LISTVIEW_EditLabelT(LISTVIEW_INFO *infoPtr, INT nItem, BOOL isW)
5687 WCHAR szDispText[DISP_TEXT_SIZE] = { 0 };
5688 NMLVDISPINFOW dispInfo;
5691 HWND hwndSelf = infoPtr->hwndSelf;
5693 HFONT hOldFont = NULL;
5694 TEXTMETRICW textMetric;
5696 TRACE("(nItem=%d, isW=%d)\n", nItem, isW);
5698 if (~infoPtr->dwStyle & LVS_EDITLABELS) return 0;
5700 /* Is the EditBox still there, if so remove it */
5701 if(infoPtr->hwndEdit != 0)
5703 SetFocus(infoPtr->hwndSelf);
5704 infoPtr->hwndEdit = 0;
5707 if (nItem < 0 || nItem >= infoPtr->nItemCount) return 0;
5709 infoPtr->nEditLabelItem = nItem;
5711 LISTVIEW_SetSelection(infoPtr, nItem);
5712 LISTVIEW_SetItemFocus(infoPtr, nItem);
5713 LISTVIEW_InvalidateItem(infoPtr, nItem);
5715 rect.left = LVIR_LABEL;
5716 if (!LISTVIEW_GetItemRect(infoPtr, nItem, &rect)) return 0;
5718 ZeroMemory(&dispInfo, sizeof(dispInfo));
5719 dispInfo.item.mask = LVIF_PARAM | LVIF_STATE | LVIF_TEXT;
5720 dispInfo.item.iItem = nItem;
5721 dispInfo.item.iSubItem = 0;
5722 dispInfo.item.stateMask = ~0;
5723 dispInfo.item.pszText = szDispText;
5724 dispInfo.item.cchTextMax = DISP_TEXT_SIZE;
5725 if (!LISTVIEW_GetItemT(infoPtr, &dispInfo.item, isW)) return 0;
5727 infoPtr->hwndEdit = CreateEditLabelT(infoPtr, dispInfo.item.pszText, WS_VISIBLE, isW);
5728 if (!infoPtr->hwndEdit) return 0;
5730 if (notify_dispinfoT(infoPtr, LVN_BEGINLABELEDITW, &dispInfo, isW))
5732 if (!IsWindow(hwndSelf))
5734 SendMessageW(infoPtr->hwndEdit, WM_CLOSE, 0, 0);
5735 infoPtr->hwndEdit = 0;
5739 /* Now position and display edit box */
5740 hdc = GetDC(infoPtr->hwndSelf);
5742 /* Select the font to get appropriate metric dimensions */
5743 if(infoPtr->hFont != 0)
5744 hOldFont = SelectObject(hdc, infoPtr->hFont);
5746 /* Get String Length in pixels */
5747 GetTextExtentPoint32W(hdc, dispInfo.item.pszText, lstrlenW(dispInfo.item.pszText), &sz);
5749 /* Add Extra spacing for the next character */
5750 GetTextMetricsW(hdc, &textMetric);
5751 sz.cx += (textMetric.tmMaxCharWidth * 2);
5753 if(infoPtr->hFont != 0)
5754 SelectObject(hdc, hOldFont);
5756 ReleaseDC(infoPtr->hwndSelf, hdc);
5758 MoveWindow(infoPtr->hwndEdit, rect.left - 2, rect.top - 1, sz.cx,
5759 rect.bottom - rect.top + 2, FALSE);
5760 ShowWindow(infoPtr->hwndEdit, SW_NORMAL);
5761 SetFocus(infoPtr->hwndEdit);
5762 SendMessageW(infoPtr->hwndEdit, EM_SETSEL, 0, -1);
5763 return infoPtr->hwndEdit;
5769 * Ensures the specified item is visible, scrolling into view if necessary.
5772 * [I] infoPtr : valid pointer to the listview structure
5773 * [I] nItem : item index
5774 * [I] bPartial : partially or entirely visible
5780 static BOOL LISTVIEW_EnsureVisible(LISTVIEW_INFO *infoPtr, INT nItem, BOOL bPartial)
5782 INT nScrollPosHeight = 0;
5783 INT nScrollPosWidth = 0;
5784 INT nHorzAdjust = 0;
5785 INT nVertAdjust = 0;
5788 RECT rcItem, rcTemp;
5790 rcItem.left = LVIR_BOUNDS;
5791 if (!LISTVIEW_GetItemRect(infoPtr, nItem, &rcItem)) return FALSE;
5793 if (bPartial && IntersectRect(&rcTemp, &infoPtr->rcList, &rcItem)) return TRUE;
5795 if (rcItem.left < infoPtr->rcList.left || rcItem.right > infoPtr->rcList.right)
5797 /* scroll left/right, but in LV_VIEW_DETAILS mode */
5798 if (infoPtr->uView == LV_VIEW_LIST)
5799 nScrollPosWidth = infoPtr->nItemWidth;
5800 else if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
5801 nScrollPosWidth = 1;
5803 if (rcItem.left < infoPtr->rcList.left)
5806 if (infoPtr->uView != LV_VIEW_DETAILS) nHorzDiff = rcItem.left - infoPtr->rcList.left;
5811 if (infoPtr->uView != LV_VIEW_DETAILS) nHorzDiff = rcItem.right - infoPtr->rcList.right;
5815 if (rcItem.top < infoPtr->rcList.top || rcItem.bottom > infoPtr->rcList.bottom)
5817 /* scroll up/down, but not in LVS_LIST mode */
5818 if (infoPtr->uView == LV_VIEW_DETAILS)
5819 nScrollPosHeight = infoPtr->nItemHeight;
5820 else if ((infoPtr->uView == LV_VIEW_ICON) || (infoPtr->uView == LV_VIEW_SMALLICON))
5821 nScrollPosHeight = 1;
5823 if (rcItem.top < infoPtr->rcList.top)
5826 if (infoPtr->uView != LV_VIEW_LIST) nVertDiff = rcItem.top - infoPtr->rcList.top;
5831 if (infoPtr->uView != LV_VIEW_LIST) nVertDiff = rcItem.bottom - infoPtr->rcList.bottom;
5835 if (!nScrollPosWidth && !nScrollPosHeight) return TRUE;
5837 if (nScrollPosWidth)
5839 INT diff = nHorzDiff / nScrollPosWidth;
5840 if (nHorzDiff % nScrollPosWidth) diff += nHorzAdjust;
5841 LISTVIEW_HScroll(infoPtr, SB_INTERNAL, diff, 0);
5844 if (nScrollPosHeight)
5846 INT diff = nVertDiff / nScrollPosHeight;
5847 if (nVertDiff % nScrollPosHeight) diff += nVertAdjust;
5848 LISTVIEW_VScroll(infoPtr, SB_INTERNAL, diff, 0);
5856 * Searches for an item with specific characteristics.
5859 * [I] hwnd : window handle
5860 * [I] nStart : base item index
5861 * [I] lpFindInfo : item information to look for
5864 * SUCCESS : index of item
5867 static INT LISTVIEW_FindItemW(const LISTVIEW_INFO *infoPtr, INT nStart,
5868 const LVFINDINFOW *lpFindInfo)
5870 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
5871 BOOL bWrap = FALSE, bNearest = FALSE;
5872 INT nItem = nStart + 1, nLast = infoPtr->nItemCount, nNearestItem = -1;
5873 ULONG xdist, ydist, dist, mindist = 0x7fffffff;
5874 POINT Position, Destination;
5877 /* Search in virtual listviews should be done by application, not by
5878 listview control, so we just send LVN_ODFINDITEMW and return the result */
5879 if (infoPtr->dwStyle & LVS_OWNERDATA)
5883 nmlv.iStart = nStart;
5884 nmlv.lvfi = *lpFindInfo;
5885 return notify_hdr(infoPtr, LVN_ODFINDITEMW, (LPNMHDR)&nmlv.hdr);
5888 if (!lpFindInfo || nItem < 0) return -1;
5891 if (lpFindInfo->flags & (LVFI_STRING | LVFI_PARTIAL) ||
5892 lpFindInfo->flags & LVFI_SUBSTRING)
5894 lvItem.mask |= LVIF_TEXT;
5895 lvItem.pszText = szDispText;
5896 lvItem.cchTextMax = DISP_TEXT_SIZE;
5899 if (lpFindInfo->flags & LVFI_WRAP)
5902 if ((lpFindInfo->flags & LVFI_NEARESTXY) &&
5903 (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON))
5908 LISTVIEW_GetOrigin(infoPtr, &Origin);
5909 Destination.x = lpFindInfo->pt.x - Origin.x;
5910 Destination.y = lpFindInfo->pt.y - Origin.y;
5911 switch(lpFindInfo->vkDirection)
5913 case VK_DOWN: Destination.y += infoPtr->nItemHeight; break;
5914 case VK_UP: Destination.y -= infoPtr->nItemHeight; break;
5915 case VK_RIGHT: Destination.x += infoPtr->nItemWidth; break;
5916 case VK_LEFT: Destination.x -= infoPtr->nItemWidth; break;
5917 case VK_HOME: Destination.x = Destination.y = 0; break;
5918 case VK_NEXT: Destination.y += infoPtr->rcList.bottom - infoPtr->rcList.top; break;
5919 case VK_PRIOR: Destination.y -= infoPtr->rcList.bottom - infoPtr->rcList.top; break;
5921 LISTVIEW_GetAreaRect(infoPtr, &rcArea);
5922 Destination.x = rcArea.right;
5923 Destination.y = rcArea.bottom;
5925 default: ERR("Unknown vkDirection=%d\n", lpFindInfo->vkDirection);
5929 else Destination.x = Destination.y = 0;
5931 /* if LVFI_PARAM is specified, all other flags are ignored */
5932 if (lpFindInfo->flags & LVFI_PARAM)
5934 lvItem.mask |= LVIF_PARAM;
5936 lvItem.mask &= ~LVIF_TEXT;
5940 for (; nItem < nLast; nItem++)
5942 lvItem.iItem = nItem;
5943 lvItem.iSubItem = 0;
5944 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) continue;
5946 if (lvItem.mask & LVIF_PARAM)
5948 if (lpFindInfo->lParam == lvItem.lParam)
5954 if (lvItem.mask & LVIF_TEXT)
5956 if (lpFindInfo->flags & (LVFI_PARTIAL | LVFI_SUBSTRING))
5958 WCHAR *p = strstrW(lvItem.pszText, lpFindInfo->psz);
5959 if (!p || p != lvItem.pszText) continue;
5963 if (lstrcmpW(lvItem.pszText, lpFindInfo->psz) != 0) continue;
5967 if (!bNearest) return nItem;
5969 /* This is very inefficient. To do a good job here,
5970 * we need a sorted array of (x,y) item positions */
5971 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
5973 /* compute the distance^2 to the destination */
5974 xdist = Destination.x - Position.x;
5975 ydist = Destination.y - Position.y;
5976 dist = xdist * xdist + ydist * ydist;
5978 /* remember the distance, and item if it's closer */
5982 nNearestItem = nItem;
5989 nLast = min(nStart + 1, infoPtr->nItemCount);
5994 return nNearestItem;
5999 * Searches for an item with specific characteristics.
6002 * [I] hwnd : window handle
6003 * [I] nStart : base item index
6004 * [I] lpFindInfo : item information to look for
6007 * SUCCESS : index of item
6010 static INT LISTVIEW_FindItemA(const LISTVIEW_INFO *infoPtr, INT nStart,
6011 const LVFINDINFOA *lpFindInfo)
6013 BOOL hasText = lpFindInfo->flags & (LVFI_STRING | LVFI_PARTIAL) ||
6014 lpFindInfo->flags & LVFI_SUBSTRING;
6019 memcpy(&fiw, lpFindInfo, sizeof(fiw));
6020 if (hasText) fiw.psz = strW = textdupTtoW((LPCWSTR)lpFindInfo->psz, FALSE);
6021 res = LISTVIEW_FindItemW(infoPtr, nStart, &fiw);
6022 textfreeT(strW, FALSE);
6028 * Retrieves the background image of the listview control.
6031 * [I] infoPtr : valid pointer to the listview structure
6032 * [O] lpBkImage : background image attributes
6038 /* static BOOL LISTVIEW_GetBkImage(const LISTVIEW_INFO *infoPtr, LPLVBKIMAGE lpBkImage) */
6040 /* FIXME (listview, "empty stub!\n"); */
6046 * Retrieves column attributes.
6049 * [I] infoPtr : valid pointer to the listview structure
6050 * [I] nColumn : column index
6051 * [IO] lpColumn : column information
6052 * [I] isW : if TRUE, then lpColumn is a LPLVCOLUMNW
6053 * otherwise it is in fact a LPLVCOLUMNA
6059 static BOOL LISTVIEW_GetColumnT(const LISTVIEW_INFO *infoPtr, INT nColumn, LPLVCOLUMNW lpColumn, BOOL isW)
6061 COLUMN_INFO *lpColumnInfo;
6064 if (!lpColumn || nColumn < 0 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
6065 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, nColumn);
6067 /* initialize memory */
6068 ZeroMemory(&hdi, sizeof(hdi));
6070 if (lpColumn->mask & LVCF_TEXT)
6072 hdi.mask |= HDI_TEXT;
6073 hdi.pszText = lpColumn->pszText;
6074 hdi.cchTextMax = lpColumn->cchTextMax;
6077 if (lpColumn->mask & LVCF_IMAGE)
6078 hdi.mask |= HDI_IMAGE;
6080 if (lpColumn->mask & LVCF_ORDER)
6081 hdi.mask |= HDI_ORDER;
6083 if (lpColumn->mask & LVCF_SUBITEM)
6084 hdi.mask |= HDI_LPARAM;
6086 if (!SendMessageW(infoPtr->hwndHeader, isW ? HDM_GETITEMW : HDM_GETITEMA, nColumn, (LPARAM)&hdi)) return FALSE;
6088 if (lpColumn->mask & LVCF_FMT)
6089 lpColumn->fmt = lpColumnInfo->fmt;
6091 if (lpColumn->mask & LVCF_WIDTH)
6092 lpColumn->cx = lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left;
6094 if (lpColumn->mask & LVCF_IMAGE)
6095 lpColumn->iImage = hdi.iImage;
6097 if (lpColumn->mask & LVCF_ORDER)
6098 lpColumn->iOrder = hdi.iOrder;
6100 if (lpColumn->mask & LVCF_SUBITEM)
6101 lpColumn->iSubItem = hdi.lParam;
6103 if (lpColumn->mask & LVCF_MINWIDTH)
6104 lpColumn->cxMin = lpColumnInfo->cxMin;
6110 static BOOL LISTVIEW_GetColumnOrderArray(const LISTVIEW_INFO *infoPtr, INT iCount, LPINT lpiArray)
6112 TRACE("iCount=%d, lpiArray=%p\n", iCount, lpiArray);
6117 return SendMessageW(infoPtr->hwndHeader, HDM_GETORDERARRAY, iCount, (LPARAM)lpiArray);
6122 * Retrieves the column width.
6125 * [I] infoPtr : valid pointer to the listview structure
6126 * [I] int : column index
6129 * SUCCESS : column width
6132 static INT LISTVIEW_GetColumnWidth(const LISTVIEW_INFO *infoPtr, INT nColumn)
6134 INT nColumnWidth = 0;
6137 TRACE("nColumn=%d\n", nColumn);
6139 /* we have a 'column' in LIST and REPORT mode only */
6140 switch(infoPtr->uView)
6143 nColumnWidth = infoPtr->nItemWidth;
6145 case LV_VIEW_DETAILS:
6146 /* We are not using LISTVIEW_GetHeaderRect as this data is updated only after a HDN_ITEMCHANGED.
6147 * There is an application that subclasses the listview, calls LVM_GETCOLUMNWIDTH in the
6148 * HDN_ITEMCHANGED handler and goes into infinite recursion if it receives old data.
6150 * TODO: should we do the same in LVM_GETCOLUMN?
6152 hdItem.mask = HDI_WIDTH;
6153 if (!SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nColumn, (LPARAM)&hdItem))
6155 WARN("(%p): HDM_GETITEMW failed for item %d\n", infoPtr->hwndSelf, nColumn);
6158 nColumnWidth = hdItem.cxy;
6162 TRACE("nColumnWidth=%d\n", nColumnWidth);
6163 return nColumnWidth;
6168 * In list or report display mode, retrieves the number of items that can fit
6169 * vertically in the visible area. In icon or small icon display mode,
6170 * retrieves the total number of visible items.
6173 * [I] infoPtr : valid pointer to the listview structure
6176 * Number of fully visible items.
6178 static INT LISTVIEW_GetCountPerPage(const LISTVIEW_INFO *infoPtr)
6180 switch (infoPtr->uView)
6183 case LV_VIEW_SMALLICON:
6184 return infoPtr->nItemCount;
6185 case LV_VIEW_DETAILS:
6186 return LISTVIEW_GetCountPerColumn(infoPtr);
6188 return LISTVIEW_GetCountPerRow(infoPtr) * LISTVIEW_GetCountPerColumn(infoPtr);
6196 * Retrieves an image list handle.
6199 * [I] infoPtr : valid pointer to the listview structure
6200 * [I] nImageList : image list identifier
6203 * SUCCESS : image list handle
6206 static HIMAGELIST LISTVIEW_GetImageList(const LISTVIEW_INFO *infoPtr, INT nImageList)
6210 case LVSIL_NORMAL: return infoPtr->himlNormal;
6211 case LVSIL_SMALL: return infoPtr->himlSmall;
6212 case LVSIL_STATE: return infoPtr->himlState;
6213 case LVSIL_GROUPHEADER:
6214 FIXME("LVSIL_GROUPHEADER not supported\n");
6217 WARN("got unknown imagelist index - %d\n", nImageList);
6222 /* LISTVIEW_GetISearchString */
6226 * Retrieves item attributes.
6229 * [I] hwnd : window handle
6230 * [IO] lpLVItem : item info
6231 * [I] isW : if TRUE, then lpLVItem is a LPLVITEMW,
6232 * if FALSE, then lpLVItem is a LPLVITEMA.
6235 * This is the internal 'GetItem' interface -- it tries to
6236 * be smart and avoid text copies, if possible, by modifying
6237 * lpLVItem->pszText to point to the text string. Please note
6238 * that this is not always possible (e.g. OWNERDATA), so on
6239 * entry you *must* supply valid values for pszText, and cchTextMax.
6240 * The only difference to the documented interface is that upon
6241 * return, you should use *only* the lpLVItem->pszText, rather than
6242 * the buffer pointer you provided on input. Most code already does
6243 * that, so it's not a problem.
6244 * For the two cases when the text must be copied (that is,
6245 * for LVM_GETITEM, and LVM_GETITEMTEXT), use LISTVIEW_GetItemExtT.
6251 static BOOL LISTVIEW_GetItemT(const LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem, BOOL isW)
6253 ITEMHDR callbackHdr = { LPSTR_TEXTCALLBACKW, I_IMAGECALLBACK };
6254 NMLVDISPINFOW dispInfo;
6260 TRACE("(lpLVItem=%s, isW=%d)\n", debuglvitem_t(lpLVItem, isW), isW);
6262 if (!lpLVItem || lpLVItem->iItem < 0 || lpLVItem->iItem >= infoPtr->nItemCount)
6265 if (lpLVItem->mask == 0) return TRUE;
6267 /* make a local copy */
6268 isubitem = lpLVItem->iSubItem;
6270 /* a quick optimization if all we're asked is the focus state
6271 * these queries are worth optimising since they are common,
6272 * and can be answered in constant time, without the heavy accesses */
6273 if ( (lpLVItem->mask == LVIF_STATE) && (lpLVItem->stateMask == LVIS_FOCUSED) &&
6274 !(infoPtr->uCallbackMask & LVIS_FOCUSED) )
6276 lpLVItem->state = 0;
6277 if (infoPtr->nFocusedItem == lpLVItem->iItem)
6278 lpLVItem->state |= LVIS_FOCUSED;
6282 ZeroMemory(&dispInfo, sizeof(dispInfo));
6284 /* if the app stores all the data, handle it separately */
6285 if (infoPtr->dwStyle & LVS_OWNERDATA)
6287 dispInfo.item.state = 0;
6289 /* apparently, we should not callback for lParam in LVS_OWNERDATA */
6290 if ((lpLVItem->mask & ~(LVIF_STATE | LVIF_PARAM)) ||
6291 ((lpLVItem->mask & LVIF_STATE) && (infoPtr->uCallbackMask & lpLVItem->stateMask)))
6293 UINT mask = lpLVItem->mask;
6295 /* NOTE: copy only fields which we _know_ are initialized, some apps
6296 * depend on the uninitialized fields being 0 */
6297 dispInfo.item.mask = lpLVItem->mask & ~LVIF_PARAM;
6298 dispInfo.item.iItem = lpLVItem->iItem;
6299 dispInfo.item.iSubItem = isubitem;
6300 if (lpLVItem->mask & LVIF_TEXT)
6302 if (lpLVItem->mask & LVIF_NORECOMPUTE)
6304 dispInfo.item.mask &= ~(LVIF_TEXT | LVIF_NORECOMPUTE);
6307 dispInfo.item.pszText = lpLVItem->pszText;
6308 dispInfo.item.cchTextMax = lpLVItem->cchTextMax;
6311 if (lpLVItem->mask & LVIF_STATE)
6312 dispInfo.item.stateMask = lpLVItem->stateMask & infoPtr->uCallbackMask;
6313 /* could be zeroed on LVIF_NORECOMPUTE case */
6314 if (dispInfo.item.mask != 0)
6316 notify_dispinfoT(infoPtr, LVN_GETDISPINFOW, &dispInfo, isW);
6317 dispInfo.item.stateMask = lpLVItem->stateMask;
6318 if (lpLVItem->mask & (LVIF_GROUPID|LVIF_COLUMNS))
6320 /* full size structure expected - _WIN32IE >= 0x560 */
6321 *lpLVItem = dispInfo.item;
6323 else if (lpLVItem->mask & LVIF_INDENT)
6325 /* indent member expected - _WIN32IE >= 0x300 */
6326 memcpy(lpLVItem, &dispInfo.item, offsetof( LVITEMW, iGroupId ));
6330 /* minimal structure expected */
6331 memcpy(lpLVItem, &dispInfo.item, offsetof( LVITEMW, iIndent ));
6333 lpLVItem->mask = mask;
6334 TRACE(" getdispinfo(1):lpLVItem=%s\n", debuglvitem_t(lpLVItem, isW));
6338 /* make sure lParam is zeroed out */
6339 if (lpLVItem->mask & LVIF_PARAM) lpLVItem->lParam = 0;
6341 /* callback marked pointer required here */
6342 if ((lpLVItem->mask & LVIF_TEXT) && (lpLVItem->mask & LVIF_NORECOMPUTE))
6343 lpLVItem->pszText = LPSTR_TEXTCALLBACKW;
6345 /* we store only a little state, so if we're not asked, we're done */
6346 if (!(lpLVItem->mask & LVIF_STATE) || isubitem) return TRUE;
6348 /* if focus is handled by us, report it */
6349 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_FOCUSED )
6351 lpLVItem->state &= ~LVIS_FOCUSED;
6352 if (infoPtr->nFocusedItem == lpLVItem->iItem)
6353 lpLVItem->state |= LVIS_FOCUSED;
6356 /* and do the same for selection, if we handle it */
6357 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_SELECTED )
6359 lpLVItem->state &= ~LVIS_SELECTED;
6360 if (ranges_contain(infoPtr->selectionRanges, lpLVItem->iItem))
6361 lpLVItem->state |= LVIS_SELECTED;
6367 /* find the item and subitem structures before we proceed */
6368 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, lpLVItem->iItem);
6369 lpItem = DPA_GetPtr(hdpaSubItems, 0);
6374 SUBITEM_INFO *lpSubItem = LISTVIEW_GetSubItemPtr(hdpaSubItems, isubitem);
6375 pItemHdr = lpSubItem ? &lpSubItem->hdr : &callbackHdr;
6378 WARN(" iSubItem invalid (%08x), ignored.\n", isubitem);
6383 pItemHdr = &lpItem->hdr;
6385 /* Do we need to query the state from the app? */
6386 if ((lpLVItem->mask & LVIF_STATE) && infoPtr->uCallbackMask && isubitem == 0)
6388 dispInfo.item.mask |= LVIF_STATE;
6389 dispInfo.item.stateMask = infoPtr->uCallbackMask;
6392 /* Do we need to enquire about the image? */
6393 if ((lpLVItem->mask & LVIF_IMAGE) && pItemHdr->iImage == I_IMAGECALLBACK &&
6394 (isubitem == 0 || (infoPtr->dwLvExStyle & LVS_EX_SUBITEMIMAGES)))
6396 dispInfo.item.mask |= LVIF_IMAGE;
6397 dispInfo.item.iImage = I_IMAGECALLBACK;
6400 /* Only items support indentation */
6401 if ((lpLVItem->mask & LVIF_INDENT) && lpItem->iIndent == I_INDENTCALLBACK &&
6404 dispInfo.item.mask |= LVIF_INDENT;
6405 dispInfo.item.iIndent = I_INDENTCALLBACK;
6408 /* Apps depend on calling back for text if it is NULL or LPSTR_TEXTCALLBACKW */
6409 if ((lpLVItem->mask & LVIF_TEXT) && !(lpLVItem->mask & LVIF_NORECOMPUTE) &&
6410 !is_textW(pItemHdr->pszText))
6412 dispInfo.item.mask |= LVIF_TEXT;
6413 dispInfo.item.pszText = lpLVItem->pszText;
6414 dispInfo.item.cchTextMax = lpLVItem->cchTextMax;
6415 if (dispInfo.item.pszText && dispInfo.item.cchTextMax > 0)
6416 *dispInfo.item.pszText = '\0';
6419 /* If we don't have all the requested info, query the application */
6420 if (dispInfo.item.mask != 0)
6422 dispInfo.item.iItem = lpLVItem->iItem;
6423 dispInfo.item.iSubItem = lpLVItem->iSubItem; /* yes: the original subitem */
6424 dispInfo.item.lParam = lpItem->lParam;
6425 notify_dispinfoT(infoPtr, LVN_GETDISPINFOW, &dispInfo, isW);
6426 TRACE(" getdispinfo(2):item=%s\n", debuglvitem_t(&dispInfo.item, isW));
6429 /* we should not store values for subitems */
6430 if (isubitem) dispInfo.item.mask &= ~LVIF_DI_SETITEM;
6432 /* Now, handle the iImage field */
6433 if (dispInfo.item.mask & LVIF_IMAGE)
6435 lpLVItem->iImage = dispInfo.item.iImage;
6436 if ((dispInfo.item.mask & LVIF_DI_SETITEM) && pItemHdr->iImage == I_IMAGECALLBACK)
6437 pItemHdr->iImage = dispInfo.item.iImage;
6439 else if (lpLVItem->mask & LVIF_IMAGE)
6441 if(isubitem == 0 || (infoPtr->dwLvExStyle & LVS_EX_SUBITEMIMAGES))
6442 lpLVItem->iImage = pItemHdr->iImage;
6444 lpLVItem->iImage = 0;
6447 /* The pszText field */
6448 if (dispInfo.item.mask & LVIF_TEXT)
6450 if ((dispInfo.item.mask & LVIF_DI_SETITEM) && pItemHdr->pszText)
6451 textsetptrT(&pItemHdr->pszText, dispInfo.item.pszText, isW);
6453 lpLVItem->pszText = dispInfo.item.pszText;
6455 else if (lpLVItem->mask & LVIF_TEXT)
6457 /* if LVN_GETDISPINFO's disabled with LVIF_NORECOMPUTE return callback placeholder */
6458 if (isW || !is_textW(pItemHdr->pszText)) lpLVItem->pszText = pItemHdr->pszText;
6459 else textcpynT(lpLVItem->pszText, isW, pItemHdr->pszText, TRUE, lpLVItem->cchTextMax);
6462 /* Next is the lParam field */
6463 if (dispInfo.item.mask & LVIF_PARAM)
6465 lpLVItem->lParam = dispInfo.item.lParam;
6466 if ((dispInfo.item.mask & LVIF_DI_SETITEM))
6467 lpItem->lParam = dispInfo.item.lParam;
6469 else if (lpLVItem->mask & LVIF_PARAM)
6470 lpLVItem->lParam = lpItem->lParam;
6472 /* if this is a subitem, we're done */
6473 if (isubitem) return TRUE;
6475 /* ... the state field (this one is different due to uCallbackmask) */
6476 if (lpLVItem->mask & LVIF_STATE)
6478 lpLVItem->state = lpItem->state & lpLVItem->stateMask;
6479 if (dispInfo.item.mask & LVIF_STATE)
6481 lpLVItem->state &= ~dispInfo.item.stateMask;
6482 lpLVItem->state |= (dispInfo.item.state & dispInfo.item.stateMask);
6484 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_FOCUSED )
6486 lpLVItem->state &= ~LVIS_FOCUSED;
6487 if (infoPtr->nFocusedItem == lpLVItem->iItem)
6488 lpLVItem->state |= LVIS_FOCUSED;
6490 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_SELECTED )
6492 lpLVItem->state &= ~LVIS_SELECTED;
6493 if (ranges_contain(infoPtr->selectionRanges, lpLVItem->iItem))
6494 lpLVItem->state |= LVIS_SELECTED;
6498 /* and last, but not least, the indent field */
6499 if (dispInfo.item.mask & LVIF_INDENT)
6501 lpLVItem->iIndent = dispInfo.item.iIndent;
6502 if ((dispInfo.item.mask & LVIF_DI_SETITEM) && lpItem->iIndent == I_INDENTCALLBACK)
6503 lpItem->iIndent = dispInfo.item.iIndent;
6505 else if (lpLVItem->mask & LVIF_INDENT)
6507 lpLVItem->iIndent = lpItem->iIndent;
6515 * Retrieves item attributes.
6518 * [I] hwnd : window handle
6519 * [IO] lpLVItem : item info
6520 * [I] isW : if TRUE, then lpLVItem is a LPLVITEMW,
6521 * if FALSE, then lpLVItem is a LPLVITEMA.
6524 * This is the external 'GetItem' interface -- it properly copies
6525 * the text in the provided buffer.
6531 static BOOL LISTVIEW_GetItemExtT(const LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem, BOOL isW)
6536 if (!lpLVItem || lpLVItem->iItem < 0 || lpLVItem->iItem >= infoPtr->nItemCount)
6539 pszText = lpLVItem->pszText;
6540 bResult = LISTVIEW_GetItemT(infoPtr, lpLVItem, isW);
6541 if (bResult && lpLVItem->pszText != pszText)
6543 if (lpLVItem->pszText != LPSTR_TEXTCALLBACKW)
6544 textcpynT(pszText, isW, lpLVItem->pszText, isW, lpLVItem->cchTextMax);
6546 pszText = LPSTR_TEXTCALLBACKW;
6548 lpLVItem->pszText = pszText;
6556 * Retrieves the position (upper-left) of the listview control item.
6557 * Note that for LVS_ICON style, the upper-left is that of the icon
6558 * and not the bounding box.
6561 * [I] infoPtr : valid pointer to the listview structure
6562 * [I] nItem : item index
6563 * [O] lpptPosition : coordinate information
6569 static BOOL LISTVIEW_GetItemPosition(const LISTVIEW_INFO *infoPtr, INT nItem, LPPOINT lpptPosition)
6573 TRACE("(nItem=%d, lpptPosition=%p)\n", nItem, lpptPosition);
6575 if (!lpptPosition || nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
6577 LISTVIEW_GetOrigin(infoPtr, &Origin);
6578 LISTVIEW_GetItemOrigin(infoPtr, nItem, lpptPosition);
6580 if (infoPtr->uView == LV_VIEW_ICON)
6582 lpptPosition->x += (infoPtr->nItemWidth - infoPtr->iconSize.cx) / 2;
6583 lpptPosition->y += ICON_TOP_PADDING;
6585 lpptPosition->x += Origin.x;
6586 lpptPosition->y += Origin.y;
6588 TRACE (" lpptPosition=%s\n", wine_dbgstr_point(lpptPosition));
6595 * Retrieves the bounding rectangle for a listview control item.
6598 * [I] infoPtr : valid pointer to the listview structure
6599 * [I] nItem : item index
6600 * [IO] lprc : bounding rectangle coordinates
6601 * lprc->left specifies the portion of the item for which the bounding
6602 * rectangle will be retrieved.
6604 * LVIR_BOUNDS Returns the bounding rectangle of the entire item,
6605 * including the icon and label.
6608 * * Experiment shows that native control returns:
6609 * * width = min (48, length of text line)
6610 * * .left = position.x - (width - iconsize.cx)/2
6611 * * .right = .left + width
6612 * * height = #lines of text * ntmHeight + icon height + 8
6613 * * .top = position.y - 2
6614 * * .bottom = .top + height
6615 * * separation between items .y = itemSpacing.cy - height
6616 * * .x = itemSpacing.cx - width
6617 * LVIR_ICON Returns the bounding rectangle of the icon or small icon.
6620 * * Experiment shows that native control returns:
6621 * * width = iconSize.cx + 16
6622 * * .left = position.x - (width - iconsize.cx)/2
6623 * * .right = .left + width
6624 * * height = iconSize.cy + 4
6625 * * .top = position.y - 2
6626 * * .bottom = .top + height
6627 * * separation between items .y = itemSpacing.cy - height
6628 * * .x = itemSpacing.cx - width
6629 * LVIR_LABEL Returns the bounding rectangle of the item text.
6632 * * Experiment shows that native control returns:
6633 * * width = text length
6634 * * .left = position.x - width/2
6635 * * .right = .left + width
6636 * * height = ntmH * linecount + 2
6637 * * .top = position.y + iconSize.cy + 6
6638 * * .bottom = .top + height
6639 * * separation between items .y = itemSpacing.cy - height
6640 * * .x = itemSpacing.cx - width
6641 * LVIR_SELECTBOUNDS Returns the union of the LVIR_ICON and LVIR_LABEL
6642 * rectangles, but excludes columns in report view.
6649 * Note that the bounding rectangle of the label in the LVS_ICON view depends
6650 * upon whether the window has the focus currently and on whether the item
6651 * is the one with the focus. Ensure that the control's record of which
6652 * item has the focus agrees with the items' records.
6654 static BOOL LISTVIEW_GetItemRect(const LISTVIEW_INFO *infoPtr, INT nItem, LPRECT lprc)
6656 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
6657 BOOL doLabel = TRUE, oversizedBox = FALSE;
6658 POINT Position, Origin;
6662 TRACE("(hwnd=%p, nItem=%d, lprc=%p)\n", infoPtr->hwndSelf, nItem, lprc);
6664 if (!lprc || nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
6666 LISTVIEW_GetOrigin(infoPtr, &Origin);
6667 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
6669 /* Be smart and try to figure out the minimum we have to do */
6670 if (lprc->left == LVIR_ICON) doLabel = FALSE;
6671 if (infoPtr->uView == LV_VIEW_DETAILS && lprc->left == LVIR_BOUNDS) doLabel = FALSE;
6672 if (infoPtr->uView == LV_VIEW_ICON && lprc->left != LVIR_ICON &&
6673 infoPtr->bFocus && LISTVIEW_GetItemState(infoPtr, nItem, LVIS_FOCUSED))
6674 oversizedBox = TRUE;
6676 /* get what we need from the item before hand, so we make
6677 * only one request. This can speed up things, if data
6678 * is stored on the app side */
6680 if (infoPtr->uView == LV_VIEW_DETAILS) lvItem.mask |= LVIF_INDENT;
6681 if (doLabel) lvItem.mask |= LVIF_TEXT;
6682 lvItem.iItem = nItem;
6683 lvItem.iSubItem = 0;
6684 lvItem.pszText = szDispText;
6685 lvItem.cchTextMax = DISP_TEXT_SIZE;
6686 if (lvItem.mask && !LISTVIEW_GetItemW(infoPtr, &lvItem)) return FALSE;
6687 /* we got the state already up, simulate it here, to avoid a reget */
6688 if (infoPtr->uView == LV_VIEW_ICON && (lprc->left != LVIR_ICON))
6690 lvItem.mask |= LVIF_STATE;
6691 lvItem.stateMask = LVIS_FOCUSED;
6692 lvItem.state = (oversizedBox ? LVIS_FOCUSED : 0);
6695 if (infoPtr->uView == LV_VIEW_DETAILS && (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) && lprc->left == LVIR_SELECTBOUNDS)
6696 lprc->left = LVIR_BOUNDS;
6702 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, NULL, lprc, NULL, NULL);
6706 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, NULL, NULL, NULL, lprc);
6710 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, lprc, NULL, NULL, NULL, NULL);
6713 case LVIR_SELECTBOUNDS:
6714 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, lprc, NULL, NULL, NULL);
6718 WARN("Unknown value: %d\n", lprc->left);
6722 if (infoPtr->uView == LV_VIEW_DETAILS)
6724 if (mode != LVIR_BOUNDS)
6725 OffsetRect(lprc, Origin.x + LISTVIEW_GetColumnInfo(infoPtr, 0)->rcHeader.left,
6726 Position.y + Origin.y);
6728 OffsetRect(lprc, Origin.x, Position.y + Origin.y);
6731 OffsetRect(lprc, Position.x + Origin.x, Position.y + Origin.y);
6733 TRACE(" rect=%s\n", wine_dbgstr_rect(lprc));
6740 * Retrieves the spacing between listview control items.
6743 * [I] infoPtr : valid pointer to the listview structure
6744 * [IO] lprc : rectangle to receive the output
6745 * on input, lprc->top = nSubItem
6746 * lprc->left = LVIR_ICON | LVIR_BOUNDS | LVIR_LABEL
6748 * NOTE: for subItem = 0, we should return the bounds of the _entire_ item,
6749 * not only those of the first column.
6750 * Fortunately, LISTVIEW_GetItemMetrics does the right thing.
6756 static BOOL LISTVIEW_GetSubItemRect(const LISTVIEW_INFO *infoPtr, INT nItem, LPRECT lprc)
6762 if (!lprc) return FALSE;
6764 nColumn = lprc->top;
6766 TRACE("(nItem=%d, nSubItem=%d)\n", nItem, lprc->top);
6767 /* On WinNT, a subitem of '0' calls LISTVIEW_GetItemRect */
6769 return LISTVIEW_GetItemRect(infoPtr, nItem, lprc);
6771 if (infoPtr->uView != LV_VIEW_DETAILS) return FALSE;
6773 /* special case for header items */
6776 if (lprc->left != LVIR_BOUNDS)
6778 FIXME("Only LVIR_BOUNDS is implemented for header, got %d\n", lprc->left);
6782 if (infoPtr->hwndHeader)
6783 return SendMessageW(infoPtr->hwndHeader, HDM_GETITEMRECT, lprc->top, (LPARAM)lprc);
6786 memset(lprc, 0, sizeof(RECT));
6791 if (!LISTVIEW_GetItemPosition(infoPtr, nItem, &Position)) return FALSE;
6793 if (nColumn < 0 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
6796 lvItem.iItem = nItem;
6797 lvItem.iSubItem = nColumn;
6799 if (lvItem.mask && !LISTVIEW_GetItemW(infoPtr, &lvItem)) return FALSE;
6803 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, NULL, lprc, NULL, NULL);
6808 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, lprc, NULL, NULL, NULL, NULL);
6812 ERR("Unknown bounds=%d\n", lprc->left);
6816 OffsetRect(lprc, 0, Position.y);
6823 * Retrieves the width of a label.
6826 * [I] infoPtr : valid pointer to the listview structure
6829 * SUCCESS : string width (in pixels)
6832 static INT LISTVIEW_GetLabelWidth(const LISTVIEW_INFO *infoPtr, INT nItem)
6834 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
6837 TRACE("(nItem=%d)\n", nItem);
6839 lvItem.mask = LVIF_TEXT;
6840 lvItem.iItem = nItem;
6841 lvItem.iSubItem = 0;
6842 lvItem.pszText = szDispText;
6843 lvItem.cchTextMax = DISP_TEXT_SIZE;
6844 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return 0;
6846 return LISTVIEW_GetStringWidthT(infoPtr, lvItem.pszText, TRUE);
6851 * Retrieves the spacing between listview control items.
6854 * [I] infoPtr : valid pointer to the listview structure
6855 * [I] bSmall : flag for small or large icon
6858 * Horizontal + vertical spacing
6860 static LONG LISTVIEW_GetItemSpacing(const LISTVIEW_INFO *infoPtr, BOOL bSmall)
6866 lResult = MAKELONG(infoPtr->iconSpacing.cx, infoPtr->iconSpacing.cy);
6870 if (infoPtr->uView == LV_VIEW_ICON)
6871 lResult = MAKELONG(DEFAULT_COLUMN_WIDTH, GetSystemMetrics(SM_CXSMICON)+HEIGHT_PADDING);
6873 lResult = MAKELONG(infoPtr->nItemWidth, infoPtr->nItemHeight);
6880 * Retrieves the state of a listview control item.
6883 * [I] infoPtr : valid pointer to the listview structure
6884 * [I] nItem : item index
6885 * [I] uMask : state mask
6888 * State specified by the mask.
6890 static UINT LISTVIEW_GetItemState(const LISTVIEW_INFO *infoPtr, INT nItem, UINT uMask)
6894 if (nItem < 0 || nItem >= infoPtr->nItemCount) return 0;
6896 lvItem.iItem = nItem;
6897 lvItem.iSubItem = 0;
6898 lvItem.mask = LVIF_STATE;
6899 lvItem.stateMask = uMask;
6900 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return 0;
6902 return lvItem.state & uMask;
6907 * Retrieves the text of a listview control item or subitem.
6910 * [I] hwnd : window handle
6911 * [I] nItem : item index
6912 * [IO] lpLVItem : item information
6913 * [I] isW : TRUE if lpLVItem is Unicode
6916 * SUCCESS : string length
6919 static INT LISTVIEW_GetItemTextT(const LISTVIEW_INFO *infoPtr, INT nItem, LPLVITEMW lpLVItem, BOOL isW)
6921 if (!lpLVItem || nItem < 0 || nItem >= infoPtr->nItemCount) return 0;
6923 lpLVItem->mask = LVIF_TEXT;
6924 lpLVItem->iItem = nItem;
6925 if (!LISTVIEW_GetItemExtT(infoPtr, lpLVItem, isW)) return 0;
6927 return textlenT(lpLVItem->pszText, isW);
6932 * Searches for an item based on properties + relationships.
6935 * [I] infoPtr : valid pointer to the listview structure
6936 * [I] nItem : item index
6937 * [I] uFlags : relationship flag
6940 * SUCCESS : item index
6943 static INT LISTVIEW_GetNextItem(const LISTVIEW_INFO *infoPtr, INT nItem, UINT uFlags)
6946 LVFINDINFOW lvFindInfo;
6947 INT nCountPerColumn;
6951 TRACE("nItem=%d, uFlags=%x, nItemCount=%d\n", nItem, uFlags, infoPtr->nItemCount);
6952 if (nItem < -1 || nItem >= infoPtr->nItemCount) return -1;
6954 ZeroMemory(&lvFindInfo, sizeof(lvFindInfo));
6956 if (uFlags & LVNI_CUT)
6959 if (uFlags & LVNI_DROPHILITED)
6960 uMask |= LVIS_DROPHILITED;
6962 if (uFlags & LVNI_FOCUSED)
6963 uMask |= LVIS_FOCUSED;
6965 if (uFlags & LVNI_SELECTED)
6966 uMask |= LVIS_SELECTED;
6968 /* if we're asked for the focused item, that's only one,
6969 * so it's worth optimizing */
6970 if (uFlags & LVNI_FOCUSED)
6972 if ((LISTVIEW_GetItemState(infoPtr, infoPtr->nFocusedItem, uMask) & uMask) != uMask) return -1;
6973 return (infoPtr->nFocusedItem == nItem) ? -1 : infoPtr->nFocusedItem;
6976 if (uFlags & LVNI_ABOVE)
6978 if ((infoPtr->uView == LV_VIEW_LIST) || (infoPtr->uView == LV_VIEW_DETAILS))
6983 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
6989 /* Special case for autoarrange - move 'til the top of a list */
6990 if (is_autoarrange(infoPtr))
6992 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
6993 while (nItem - nCountPerRow >= 0)
6995 nItem -= nCountPerRow;
6996 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7001 lvFindInfo.flags = LVFI_NEARESTXY;
7002 lvFindInfo.vkDirection = VK_UP;
7003 LISTVIEW_GetItemPosition(infoPtr, nItem, &lvFindInfo.pt);
7004 while ((nItem = LISTVIEW_FindItemW(infoPtr, nItem, &lvFindInfo)) != -1)
7006 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7011 else if (uFlags & LVNI_BELOW)
7013 if ((infoPtr->uView == LV_VIEW_LIST) || (infoPtr->uView == LV_VIEW_DETAILS))
7015 while (nItem < infoPtr->nItemCount)
7018 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7024 /* Special case for autoarrange - move 'til the bottom of a list */
7025 if (is_autoarrange(infoPtr))
7027 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
7028 while (nItem + nCountPerRow < infoPtr->nItemCount )
7030 nItem += nCountPerRow;
7031 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7036 lvFindInfo.flags = LVFI_NEARESTXY;
7037 lvFindInfo.vkDirection = VK_DOWN;
7038 LISTVIEW_GetItemPosition(infoPtr, nItem, &lvFindInfo.pt);
7039 while ((nItem = LISTVIEW_FindItemW(infoPtr, nItem, &lvFindInfo)) != -1)
7041 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7046 else if (uFlags & LVNI_TOLEFT)
7048 if (infoPtr->uView == LV_VIEW_LIST)
7050 nCountPerColumn = LISTVIEW_GetCountPerColumn(infoPtr);
7051 while (nItem - nCountPerColumn >= 0)
7053 nItem -= nCountPerColumn;
7054 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7058 else if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
7060 /* Special case for autoarrange - move 'til the beginning of a row */
7061 if (is_autoarrange(infoPtr))
7063 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
7064 while (nItem % nCountPerRow > 0)
7067 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7072 lvFindInfo.flags = LVFI_NEARESTXY;
7073 lvFindInfo.vkDirection = VK_LEFT;
7074 LISTVIEW_GetItemPosition(infoPtr, nItem, &lvFindInfo.pt);
7075 while ((nItem = LISTVIEW_FindItemW(infoPtr, nItem, &lvFindInfo)) != -1)
7077 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7082 else if (uFlags & LVNI_TORIGHT)
7084 if (infoPtr->uView == LV_VIEW_LIST)
7086 nCountPerColumn = LISTVIEW_GetCountPerColumn(infoPtr);
7087 while (nItem + nCountPerColumn < infoPtr->nItemCount)
7089 nItem += nCountPerColumn;
7090 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7094 else if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
7096 /* Special case for autoarrange - move 'til the end of a row */
7097 if (is_autoarrange(infoPtr))
7099 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
7100 while (nItem % nCountPerRow < nCountPerRow - 1 )
7103 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7108 lvFindInfo.flags = LVFI_NEARESTXY;
7109 lvFindInfo.vkDirection = VK_RIGHT;
7110 LISTVIEW_GetItemPosition(infoPtr, nItem, &lvFindInfo.pt);
7111 while ((nItem = LISTVIEW_FindItemW(infoPtr, nItem, &lvFindInfo)) != -1)
7113 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7122 /* search by index */
7123 for (i = nItem; i < infoPtr->nItemCount; i++)
7125 if ((LISTVIEW_GetItemState(infoPtr, i, uMask) & uMask) == uMask)
7133 /* LISTVIEW_GetNumberOfWorkAreas */
7137 * Retrieves the origin coordinates when in icon or small icon display mode.
7140 * [I] infoPtr : valid pointer to the listview structure
7141 * [O] lpptOrigin : coordinate information
7146 static void LISTVIEW_GetOrigin(const LISTVIEW_INFO *infoPtr, LPPOINT lpptOrigin)
7148 INT nHorzPos = 0, nVertPos = 0;
7149 SCROLLINFO scrollInfo;
7151 scrollInfo.cbSize = sizeof(SCROLLINFO);
7152 scrollInfo.fMask = SIF_POS;
7154 if (GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo))
7155 nHorzPos = scrollInfo.nPos;
7156 if (GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
7157 nVertPos = scrollInfo.nPos;
7159 TRACE("nHorzPos=%d, nVertPos=%d\n", nHorzPos, nVertPos);
7161 lpptOrigin->x = infoPtr->rcList.left;
7162 lpptOrigin->y = infoPtr->rcList.top;
7163 if (infoPtr->uView == LV_VIEW_LIST)
7164 nHorzPos *= infoPtr->nItemWidth;
7165 else if (infoPtr->uView == LV_VIEW_DETAILS)
7166 nVertPos *= infoPtr->nItemHeight;
7168 lpptOrigin->x -= nHorzPos;
7169 lpptOrigin->y -= nVertPos;
7171 TRACE(" origin=%s\n", wine_dbgstr_point(lpptOrigin));
7176 * Retrieves the width of a string.
7179 * [I] hwnd : window handle
7180 * [I] lpszText : text string to process
7181 * [I] isW : TRUE if lpszText is Unicode, FALSE otherwise
7184 * SUCCESS : string width (in pixels)
7187 static INT LISTVIEW_GetStringWidthT(const LISTVIEW_INFO *infoPtr, LPCWSTR lpszText, BOOL isW)
7192 if (is_textT(lpszText, isW))
7194 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
7195 HDC hdc = GetDC(infoPtr->hwndSelf);
7196 HFONT hOldFont = SelectObject(hdc, hFont);
7199 GetTextExtentPointW(hdc, lpszText, lstrlenW(lpszText), &stringSize);
7201 GetTextExtentPointA(hdc, (LPCSTR)lpszText, lstrlenA((LPCSTR)lpszText), &stringSize);
7202 SelectObject(hdc, hOldFont);
7203 ReleaseDC(infoPtr->hwndSelf, hdc);
7205 return stringSize.cx;
7210 * Determines which listview item is located at the specified position.
7213 * [I] infoPtr : valid pointer to the listview structure
7214 * [IO] lpht : hit test information
7215 * [I] subitem : fill out iSubItem.
7216 * [I] select : return the index only if the hit selects the item
7219 * (mm 20001022): We must not allow iSubItem to be touched, for
7220 * an app might pass only a structure with space up to iItem!
7221 * (MS Office 97 does that for instance in the file open dialog)
7224 * SUCCESS : item index
7227 static INT LISTVIEW_HitTest(const LISTVIEW_INFO *infoPtr, LPLVHITTESTINFO lpht, BOOL subitem, BOOL select)
7229 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
7230 RECT rcBox, rcBounds, rcState, rcIcon, rcLabel, rcSearch;
7231 POINT Origin, Position, opt;
7236 TRACE("(pt=%s, subitem=%d, select=%d)\n", wine_dbgstr_point(&lpht->pt), subitem, select);
7240 if (subitem) lpht->iSubItem = 0;
7242 LISTVIEW_GetOrigin(infoPtr, &Origin);
7244 /* set whole list relation flags */
7245 if (subitem && infoPtr->uView == LV_VIEW_DETAILS)
7247 /* LVM_SUBITEMHITTEST checks left bound of possible client area */
7248 if (infoPtr->rcList.left > lpht->pt.x && Origin.x < lpht->pt.x)
7249 lpht->flags |= LVHT_TOLEFT;
7251 if (lpht->pt.y < infoPtr->rcList.top && lpht->pt.y >= 0)
7252 opt.y = lpht->pt.y + infoPtr->rcList.top;
7256 if (infoPtr->rcList.bottom < opt.y)
7257 lpht->flags |= LVHT_BELOW;
7261 if (infoPtr->rcList.left > lpht->pt.x)
7262 lpht->flags |= LVHT_TOLEFT;
7263 else if (infoPtr->rcList.right < lpht->pt.x)
7264 lpht->flags |= LVHT_TORIGHT;
7266 if (infoPtr->rcList.top > lpht->pt.y)
7267 lpht->flags |= LVHT_ABOVE;
7268 else if (infoPtr->rcList.bottom < lpht->pt.y)
7269 lpht->flags |= LVHT_BELOW;
7272 /* even if item is invalid try to find subitem */
7273 if (infoPtr->uView == LV_VIEW_DETAILS && subitem)
7278 opt.x = lpht->pt.x - Origin.x;
7280 lpht->iSubItem = -1;
7281 for (j = 0; j < DPA_GetPtrCount(infoPtr->hdpaColumns); j++)
7283 pRect = &LISTVIEW_GetColumnInfo(infoPtr, j)->rcHeader;
7285 if ((opt.x >= pRect->left) && (opt.x < pRect->right))
7291 TRACE("lpht->iSubItem=%d\n", lpht->iSubItem);
7294 TRACE("lpht->flags=0x%x\n", lpht->flags);
7295 if (lpht->flags) return -1;
7297 lpht->flags |= LVHT_NOWHERE;
7299 /* first deal with the large items */
7300 rcSearch.left = lpht->pt.x;
7301 rcSearch.top = lpht->pt.y;
7302 rcSearch.right = rcSearch.left + 1;
7303 rcSearch.bottom = rcSearch.top + 1;
7305 iterator_frameditems(&i, infoPtr, &rcSearch);
7306 iterator_next(&i); /* go to first item in the sequence */
7308 iterator_destroy(&i);
7310 TRACE("lpht->iItem=%d\n", iItem);
7311 if (iItem == -1) return -1;
7313 lvItem.mask = LVIF_STATE | LVIF_TEXT;
7314 if (infoPtr->uView == LV_VIEW_DETAILS) lvItem.mask |= LVIF_INDENT;
7315 lvItem.stateMask = LVIS_STATEIMAGEMASK;
7316 if (infoPtr->uView == LV_VIEW_ICON) lvItem.stateMask |= LVIS_FOCUSED;
7317 lvItem.iItem = iItem;
7318 lvItem.iSubItem = subitem ? lpht->iSubItem : 0;
7319 lvItem.pszText = szDispText;
7320 lvItem.cchTextMax = DISP_TEXT_SIZE;
7321 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return -1;
7322 if (!infoPtr->bFocus) lvItem.state &= ~LVIS_FOCUSED;
7324 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, &rcBox, NULL, &rcIcon, &rcState, &rcLabel);
7325 LISTVIEW_GetItemOrigin(infoPtr, iItem, &Position);
7326 opt.x = lpht->pt.x - Position.x - Origin.x;
7328 if (lpht->pt.y < infoPtr->rcList.top && lpht->pt.y >= 0)
7329 opt.y = lpht->pt.y - Position.y - Origin.y + infoPtr->rcList.top;
7331 opt.y = lpht->pt.y - Position.y - Origin.y;
7333 if (infoPtr->uView == LV_VIEW_DETAILS)
7336 if (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT)
7337 opt.x = lpht->pt.x - Origin.x;
7341 UnionRect(&rcBounds, &rcIcon, &rcLabel);
7342 UnionRect(&rcBounds, &rcBounds, &rcState);
7344 TRACE("rcBounds=%s\n", wine_dbgstr_rect(&rcBounds));
7345 if (!PtInRect(&rcBounds, opt)) return -1;
7347 if (PtInRect(&rcIcon, opt))
7348 lpht->flags |= LVHT_ONITEMICON;
7349 else if (PtInRect(&rcLabel, opt))
7350 lpht->flags |= LVHT_ONITEMLABEL;
7351 else if (infoPtr->himlState && PtInRect(&rcState, opt))
7352 lpht->flags |= LVHT_ONITEMSTATEICON;
7353 /* special case for LVS_EX_FULLROWSELECT */
7354 if (infoPtr->uView == LV_VIEW_DETAILS && infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT &&
7355 !(lpht->flags & LVHT_ONITEM))
7357 lpht->flags = LVHT_ONITEM | LVHT_ABOVE;
7359 if (lpht->flags & LVHT_ONITEM)
7360 lpht->flags &= ~LVHT_NOWHERE;
7361 TRACE("lpht->flags=0x%x\n", lpht->flags);
7363 if (select && !(infoPtr->uView == LV_VIEW_DETAILS &&
7364 ((infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) ||
7365 (infoPtr->dwStyle & LVS_OWNERDRAWFIXED))))
7367 if (infoPtr->uView == LV_VIEW_DETAILS)
7369 /* get main item bounds */
7370 lvItem.iSubItem = 0;
7371 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, &rcBox, NULL, &rcIcon, &rcState, &rcLabel);
7372 UnionRect(&rcBounds, &rcIcon, &rcLabel);
7373 UnionRect(&rcBounds, &rcBounds, &rcState);
7375 if (!PtInRect(&rcBounds, opt)) iItem = -1;
7377 return lpht->iItem = iItem;
7382 * Inserts a new item in the listview control.
7385 * [I] infoPtr : valid pointer to the listview structure
7386 * [I] lpLVItem : item information
7387 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
7390 * SUCCESS : new item index
7393 static INT LISTVIEW_InsertItemT(LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem, BOOL isW)
7400 BOOL is_sorted, has_changed;
7402 HWND hwndSelf = infoPtr->hwndSelf;
7404 TRACE("(lpLVItem=%s, isW=%d)\n", debuglvitem_t(lpLVItem, isW), isW);
7406 if (infoPtr->dwStyle & LVS_OWNERDATA) return infoPtr->nItemCount++;
7408 /* make sure it's an item, and not a subitem; cannot insert a subitem */
7409 if (!lpLVItem || lpLVItem->iSubItem) return -1;
7411 if (!is_assignable_item(lpLVItem, infoPtr->dwStyle)) return -1;
7413 if (!(lpItem = Alloc(sizeof(ITEM_INFO)))) return -1;
7415 /* insert item in listview control data structure */
7416 if ( !(hdpaSubItems = DPA_Create(8)) ) goto fail;
7417 if ( !DPA_SetPtr(hdpaSubItems, 0, lpItem) ) assert (FALSE);
7419 /* link with id struct */
7420 if (!(lpID = Alloc(sizeof(ITEM_ID)))) goto fail;
7422 lpID->item = hdpaSubItems;
7423 lpID->id = get_next_itemid(infoPtr);
7424 if ( DPA_InsertPtr(infoPtr->hdpaItemIds, infoPtr->nItemCount, lpID) == -1) goto fail;
7426 is_sorted = (infoPtr->dwStyle & (LVS_SORTASCENDING | LVS_SORTDESCENDING)) &&
7427 !(infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (LPSTR_TEXTCALLBACKW != lpLVItem->pszText);
7429 if (lpLVItem->iItem < 0 && !is_sorted) return -1;
7431 /* calculate new item index */
7438 while (i < infoPtr->nItemCount)
7440 hItem = DPA_GetPtr( infoPtr->hdpaItems, i);
7441 item_s = (ITEM_INFO*)DPA_GetPtr(hItem, 0);
7443 cmpv = textcmpWT(item_s->hdr.pszText, lpLVItem->pszText, isW);
7444 if (infoPtr->dwStyle & LVS_SORTDESCENDING) cmpv *= -1;
7446 if (cmpv >= 0) break;
7452 nItem = min(lpLVItem->iItem, infoPtr->nItemCount);
7454 TRACE(" inserting at %d, sorted=%d, count=%d, iItem=%d\n", nItem, is_sorted, infoPtr->nItemCount, lpLVItem->iItem);
7455 nItem = DPA_InsertPtr( infoPtr->hdpaItems, nItem, hdpaSubItems );
7456 if (nItem == -1) goto fail;
7457 infoPtr->nItemCount++;
7459 /* shift indices first so they don't get tangled */
7460 LISTVIEW_ShiftIndices(infoPtr, nItem, 1);
7462 /* set the item attributes */
7463 if (lpLVItem->mask & (LVIF_GROUPID|LVIF_COLUMNS))
7465 /* full size structure expected - _WIN32IE >= 0x560 */
7468 else if (lpLVItem->mask & LVIF_INDENT)
7470 /* indent member expected - _WIN32IE >= 0x300 */
7471 memcpy(&item, lpLVItem, offsetof( LVITEMW, iGroupId ));
7475 /* minimal structure expected */
7476 memcpy(&item, lpLVItem, offsetof( LVITEMW, iIndent ));
7479 if (infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES)
7481 item.mask |= LVIF_STATE;
7482 item.stateMask |= LVIS_STATEIMAGEMASK;
7483 item.state &= ~LVIS_STATEIMAGEMASK;
7484 item.state |= INDEXTOSTATEIMAGEMASK(1);
7486 if (!set_main_item(infoPtr, &item, TRUE, isW, &has_changed)) goto undo;
7488 /* make room for the position, if we are in the right mode */
7489 if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
7491 if (DPA_InsertPtr(infoPtr->hdpaPosX, nItem, 0) == -1)
7493 if (DPA_InsertPtr(infoPtr->hdpaPosY, nItem, 0) == -1)
7495 DPA_DeletePtr(infoPtr->hdpaPosX, nItem);
7500 /* send LVN_INSERTITEM notification */
7501 ZeroMemory(&nmlv, sizeof(NMLISTVIEW));
7503 nmlv.lParam = lpItem->lParam;
7504 notify_listview(infoPtr, LVN_INSERTITEM, &nmlv);
7505 if (!IsWindow(hwndSelf))
7508 /* align items (set position of each item) */
7509 if (infoPtr->uView == LV_VIEW_SMALLICON || infoPtr->uView == LV_VIEW_ICON)
7513 if (infoPtr->dwStyle & LVS_ALIGNLEFT)
7514 LISTVIEW_NextIconPosLeft(infoPtr, &pt);
7516 LISTVIEW_NextIconPosTop(infoPtr, &pt);
7518 LISTVIEW_MoveIconTo(infoPtr, nItem, &pt, TRUE);
7521 /* now is the invalidation fun */
7522 LISTVIEW_ScrollOnInsert(infoPtr, nItem, 1);
7526 LISTVIEW_ShiftIndices(infoPtr, nItem, -1);
7527 DPA_DeletePtr(infoPtr->hdpaItems, nItem);
7528 infoPtr->nItemCount--;
7530 DPA_DeletePtr(hdpaSubItems, 0);
7531 DPA_Destroy (hdpaSubItems);
7538 * Checks item visibility.
7541 * [I] infoPtr : valid pointer to the listview structure
7542 * [I] nFirst : item index to check for
7545 * Item visible : TRUE
7546 * Item invisible or failure : FALSE
7548 static BOOL LISTVIEW_IsItemVisible(const LISTVIEW_INFO *infoPtr, INT nItem)
7550 POINT Origin, Position;
7555 TRACE("nItem=%d\n", nItem);
7557 if (nItem < 0 || nItem >= DPA_GetPtrCount(infoPtr->hdpaItems)) return FALSE;
7559 LISTVIEW_GetOrigin(infoPtr, &Origin);
7560 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
7561 rcItem.left = Position.x + Origin.x;
7562 rcItem.top = Position.y + Origin.y;
7563 rcItem.right = rcItem.left + infoPtr->nItemWidth;
7564 rcItem.bottom = rcItem.top + infoPtr->nItemHeight;
7566 hdc = GetDC(infoPtr->hwndSelf);
7567 if (!hdc) return FALSE;
7568 ret = RectVisible(hdc, &rcItem);
7569 ReleaseDC(infoPtr->hwndSelf, hdc);
7576 * Redraws a range of items.
7579 * [I] infoPtr : valid pointer to the listview structure
7580 * [I] nFirst : first item
7581 * [I] nLast : last item
7587 static BOOL LISTVIEW_RedrawItems(const LISTVIEW_INFO *infoPtr, INT nFirst, INT nLast)
7591 if (nLast < nFirst || min(nFirst, nLast) < 0 ||
7592 max(nFirst, nLast) >= infoPtr->nItemCount)
7595 for (i = nFirst; i <= nLast; i++)
7596 LISTVIEW_InvalidateItem(infoPtr, i);
7603 * Scroll the content of a listview.
7606 * [I] infoPtr : valid pointer to the listview structure
7607 * [I] dx : horizontal scroll amount in pixels
7608 * [I] dy : vertical scroll amount in pixels
7615 * If the control is in report view (LV_VIEW_DETAILS) the control can
7616 * be scrolled only in line increments. "dy" will be rounded to the
7617 * nearest number of pixels that are a whole line. Ex: if line height
7618 * is 16 and an 8 is passed, the list will be scrolled by 16. If a 7
7619 * is passed, then the scroll will be 0. (per MSDN 7/2002)
7621 * For: (per experimentation with native control and CSpy ListView)
7622 * LV_VIEW_ICON dy=1 = 1 pixel (vertical only)
7624 * LV_VIEW_SMALLICON dy=1 = 1 pixel (vertical only)
7626 * LV_VIEW_LIST dx=1 = 1 column (horizontal only)
7627 * but will only scroll 1 column per message
7628 * no matter what the value.
7629 * dy must be 0 or FALSE returned.
7630 * LV_VIEW_DETAILS dx=1 = 1 pixel
7634 static BOOL LISTVIEW_Scroll(LISTVIEW_INFO *infoPtr, INT dx, INT dy)
7636 switch(infoPtr->uView) {
7637 case LV_VIEW_DETAILS:
7638 dy += (dy < 0 ? -1 : 1) * infoPtr->nItemHeight/2;
7639 dy /= infoPtr->nItemHeight;
7642 if (dy != 0) return FALSE;
7649 if (dx != 0) LISTVIEW_HScroll(infoPtr, SB_INTERNAL, dx, 0);
7650 if (dy != 0) LISTVIEW_VScroll(infoPtr, SB_INTERNAL, dy, 0);
7657 * Sets the background color.
7660 * [I] infoPtr : valid pointer to the listview structure
7661 * [I] clrBk : background color
7667 static BOOL LISTVIEW_SetBkColor(LISTVIEW_INFO *infoPtr, COLORREF clrBk)
7669 TRACE("(clrBk=%x)\n", clrBk);
7671 if(infoPtr->clrBk != clrBk) {
7672 if (infoPtr->clrBk != CLR_NONE) DeleteObject(infoPtr->hBkBrush);
7673 infoPtr->clrBk = clrBk;
7674 if (clrBk == CLR_NONE)
7675 infoPtr->hBkBrush = (HBRUSH)GetClassLongPtrW(infoPtr->hwndSelf, GCLP_HBRBACKGROUND);
7678 infoPtr->hBkBrush = CreateSolidBrush(clrBk);
7679 infoPtr->dwLvExStyle &= ~LVS_EX_TRANSPARENTBKGND;
7681 LISTVIEW_InvalidateList(infoPtr);
7687 /* LISTVIEW_SetBkImage */
7689 /*** Helper for {Insert,Set}ColumnT *only* */
7690 static void column_fill_hditem(const LISTVIEW_INFO *infoPtr, HDITEMW *lphdi, INT nColumn,
7691 const LVCOLUMNW *lpColumn, BOOL isW)
7693 if (lpColumn->mask & LVCF_FMT)
7695 /* format member is valid */
7696 lphdi->mask |= HDI_FORMAT;
7698 /* set text alignment (leftmost column must be left-aligned) */
7699 if (nColumn == 0 || (lpColumn->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_LEFT)
7700 lphdi->fmt |= HDF_LEFT;
7701 else if ((lpColumn->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_RIGHT)
7702 lphdi->fmt |= HDF_RIGHT;
7703 else if ((lpColumn->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_CENTER)
7704 lphdi->fmt |= HDF_CENTER;
7706 if (lpColumn->fmt & LVCFMT_BITMAP_ON_RIGHT)
7707 lphdi->fmt |= HDF_BITMAP_ON_RIGHT;
7709 if (lpColumn->fmt & LVCFMT_COL_HAS_IMAGES)
7711 lphdi->fmt |= HDF_IMAGE;
7712 lphdi->iImage = I_IMAGECALLBACK;
7715 if (lpColumn->fmt & LVCFMT_FIXED_WIDTH)
7716 lphdi->fmt |= HDF_FIXEDWIDTH;
7719 if (lpColumn->mask & LVCF_WIDTH)
7721 lphdi->mask |= HDI_WIDTH;
7722 if(lpColumn->cx == LVSCW_AUTOSIZE_USEHEADER)
7724 /* make it fill the remainder of the controls width */
7728 for(item_index = 0; item_index < (nColumn - 1); item_index++)
7730 LISTVIEW_GetHeaderRect(infoPtr, item_index, &rcHeader);
7731 lphdi->cxy += rcHeader.right - rcHeader.left;
7734 /* retrieve the layout of the header */
7735 GetClientRect(infoPtr->hwndSelf, &rcHeader);
7736 TRACE("start cxy=%d rcHeader=%s\n", lphdi->cxy, wine_dbgstr_rect(&rcHeader));
7738 lphdi->cxy = (rcHeader.right - rcHeader.left) - lphdi->cxy;
7741 lphdi->cxy = lpColumn->cx;
7744 if (lpColumn->mask & LVCF_TEXT)
7746 lphdi->mask |= HDI_TEXT | HDI_FORMAT;
7747 lphdi->fmt |= HDF_STRING;
7748 lphdi->pszText = lpColumn->pszText;
7749 lphdi->cchTextMax = textlenT(lpColumn->pszText, isW);
7752 if (lpColumn->mask & LVCF_IMAGE)
7754 lphdi->mask |= HDI_IMAGE;
7755 lphdi->iImage = lpColumn->iImage;
7758 if (lpColumn->mask & LVCF_ORDER)
7760 lphdi->mask |= HDI_ORDER;
7761 lphdi->iOrder = lpColumn->iOrder;
7768 * Inserts a new column.
7771 * [I] infoPtr : valid pointer to the listview structure
7772 * [I] nColumn : column index
7773 * [I] lpColumn : column information
7774 * [I] isW : TRUE if lpColumn is Unicode, FALSE otherwise
7777 * SUCCESS : new column index
7780 static INT LISTVIEW_InsertColumnT(LISTVIEW_INFO *infoPtr, INT nColumn,
7781 const LVCOLUMNW *lpColumn, BOOL isW)
7783 COLUMN_INFO *lpColumnInfo;
7787 TRACE("(nColumn=%d, lpColumn=%s, isW=%d)\n", nColumn, debuglvcolumn_t(lpColumn, isW), isW);
7789 if (!lpColumn || nColumn < 0) return -1;
7790 nColumn = min(nColumn, DPA_GetPtrCount(infoPtr->hdpaColumns));
7792 ZeroMemory(&hdi, sizeof(HDITEMW));
7793 column_fill_hditem(infoPtr, &hdi, nColumn, lpColumn, isW);
7796 * A mask not including LVCF_WIDTH turns into a mask of width, width 10
7797 * (can be seen in SPY) otherwise column never gets added.
7799 if (!(lpColumn->mask & LVCF_WIDTH)) {
7800 hdi.mask |= HDI_WIDTH;
7805 * when the iSubItem is available Windows copies it to the header lParam. It seems
7806 * to happen only in LVM_INSERTCOLUMN - not in LVM_SETCOLUMN
7808 if (lpColumn->mask & LVCF_SUBITEM)
7810 hdi.mask |= HDI_LPARAM;
7811 hdi.lParam = lpColumn->iSubItem;
7814 /* create header if not present */
7815 LISTVIEW_CreateHeader(infoPtr);
7816 if (!(LVS_NOCOLUMNHEADER & infoPtr->dwStyle) &&
7817 (infoPtr->uView == LV_VIEW_DETAILS) && (WS_VISIBLE & infoPtr->dwStyle))
7819 ShowWindow(infoPtr->hwndHeader, SW_SHOWNORMAL);
7822 /* insert item in header control */
7823 nNewColumn = SendMessageW(infoPtr->hwndHeader,
7824 isW ? HDM_INSERTITEMW : HDM_INSERTITEMA,
7825 (WPARAM)nColumn, (LPARAM)&hdi);
7826 if (nNewColumn == -1) return -1;
7827 if (nNewColumn != nColumn) ERR("nColumn=%d, nNewColumn=%d\n", nColumn, nNewColumn);
7829 /* create our own column info */
7830 if (!(lpColumnInfo = Alloc(sizeof(COLUMN_INFO)))) goto fail;
7831 if (DPA_InsertPtr(infoPtr->hdpaColumns, nNewColumn, lpColumnInfo) == -1) goto fail;
7833 if (lpColumn->mask & LVCF_FMT) lpColumnInfo->fmt = lpColumn->fmt;
7834 if (lpColumn->mask & LVCF_MINWIDTH) lpColumnInfo->cxMin = lpColumn->cxMin;
7835 if (!SendMessageW(infoPtr->hwndHeader, HDM_GETITEMRECT, nNewColumn, (LPARAM)&lpColumnInfo->rcHeader))
7838 /* now we have to actually adjust the data */
7839 if (!(infoPtr->dwStyle & LVS_OWNERDATA) && infoPtr->nItemCount > 0)
7841 SUBITEM_INFO *lpSubItem;
7845 for (nItem = 0; nItem < infoPtr->nItemCount; nItem++)
7847 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, nItem);
7848 for (i = 1; i < DPA_GetPtrCount(hdpaSubItems); i++)
7850 lpSubItem = DPA_GetPtr(hdpaSubItems, i);
7851 if (lpSubItem->iSubItem >= nNewColumn)
7852 lpSubItem->iSubItem++;
7857 /* make space for the new column */
7858 LISTVIEW_ScrollColumns(infoPtr, nNewColumn + 1, lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left);
7859 LISTVIEW_UpdateItemSize(infoPtr);
7864 if (nNewColumn != -1) SendMessageW(infoPtr->hwndHeader, HDM_DELETEITEM, nNewColumn, 0);
7867 DPA_DeletePtr(infoPtr->hdpaColumns, nNewColumn);
7875 * Sets the attributes of a header item.
7878 * [I] infoPtr : valid pointer to the listview structure
7879 * [I] nColumn : column index
7880 * [I] lpColumn : column attributes
7881 * [I] isW: if TRUE, then lpColumn is a LPLVCOLUMNW, else it is a LPLVCOLUMNA
7887 static BOOL LISTVIEW_SetColumnT(const LISTVIEW_INFO *infoPtr, INT nColumn,
7888 const LVCOLUMNW *lpColumn, BOOL isW)
7890 HDITEMW hdi, hdiget;
7893 TRACE("(nColumn=%d, lpColumn=%s, isW=%d)\n", nColumn, debuglvcolumn_t(lpColumn, isW), isW);
7895 if (!lpColumn || nColumn < 0 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
7897 ZeroMemory(&hdi, sizeof(HDITEMW));
7898 if (lpColumn->mask & LVCF_FMT)
7900 hdi.mask |= HDI_FORMAT;
7901 hdiget.mask = HDI_FORMAT;
7902 if (SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nColumn, (LPARAM)&hdiget))
7903 hdi.fmt = hdiget.fmt & HDF_STRING;
7905 column_fill_hditem(infoPtr, &hdi, nColumn, lpColumn, isW);
7907 /* set header item attributes */
7908 bResult = SendMessageW(infoPtr->hwndHeader, isW ? HDM_SETITEMW : HDM_SETITEMA, (WPARAM)nColumn, (LPARAM)&hdi);
7909 if (!bResult) return FALSE;
7911 if (lpColumn->mask & LVCF_FMT)
7913 COLUMN_INFO *lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, nColumn);
7914 INT oldFmt = lpColumnInfo->fmt;
7916 lpColumnInfo->fmt = lpColumn->fmt;
7917 if ((oldFmt ^ lpColumn->fmt) & (LVCFMT_JUSTIFYMASK | LVCFMT_IMAGE))
7919 if (infoPtr->uView == LV_VIEW_DETAILS) LISTVIEW_InvalidateColumn(infoPtr, nColumn);
7923 if (lpColumn->mask & LVCF_MINWIDTH)
7924 LISTVIEW_GetColumnInfo(infoPtr, nColumn)->cxMin = lpColumn->cxMin;
7931 * Sets the column order array
7934 * [I] infoPtr : valid pointer to the listview structure
7935 * [I] iCount : number of elements in column order array
7936 * [I] lpiArray : pointer to column order array
7942 static BOOL LISTVIEW_SetColumnOrderArray(LISTVIEW_INFO *infoPtr, INT iCount, const INT *lpiArray)
7944 TRACE("iCount %d lpiArray %p\n", iCount, lpiArray);
7946 if (!lpiArray || !IsWindow(infoPtr->hwndHeader)) return FALSE;
7948 infoPtr->colRectsDirty = TRUE;
7950 return SendMessageW(infoPtr->hwndHeader, HDM_SETORDERARRAY, iCount, (LPARAM)lpiArray);
7955 * Sets the width of a column
7958 * [I] infoPtr : valid pointer to the listview structure
7959 * [I] nColumn : column index
7960 * [I] cx : column width
7966 static BOOL LISTVIEW_SetColumnWidth(LISTVIEW_INFO *infoPtr, INT nColumn, INT cx)
7968 WCHAR szDispText[DISP_TEXT_SIZE] = { 0 };
7972 TRACE("(nColumn=%d, cx=%d\n", nColumn, cx);
7974 /* set column width only if in report or list mode */
7975 if (infoPtr->uView != LV_VIEW_DETAILS && infoPtr->uView != LV_VIEW_LIST) return FALSE;
7977 /* take care of invalid cx values */
7978 if(infoPtr->uView == LV_VIEW_DETAILS && cx < -2) cx = LVSCW_AUTOSIZE;
7979 else if (infoPtr->uView == LV_VIEW_LIST && cx < 1) return FALSE;
7981 /* resize all columns if in LV_VIEW_LIST mode */
7982 if(infoPtr->uView == LV_VIEW_LIST)
7984 infoPtr->nItemWidth = cx;
7985 LISTVIEW_InvalidateList(infoPtr);
7989 if (nColumn < 0 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
7991 if (cx == LVSCW_AUTOSIZE || (cx == LVSCW_AUTOSIZE_USEHEADER && nColumn < DPA_GetPtrCount(infoPtr->hdpaColumns) -1))
7996 lvItem.mask = LVIF_TEXT;
7998 lvItem.iSubItem = nColumn;
7999 lvItem.pszText = szDispText;
8000 lvItem.cchTextMax = DISP_TEXT_SIZE;
8001 for (; lvItem.iItem < infoPtr->nItemCount; lvItem.iItem++)
8003 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) continue;
8004 nLabelWidth = LISTVIEW_GetStringWidthT(infoPtr, lvItem.pszText, TRUE);
8005 if (max_cx < nLabelWidth) max_cx = nLabelWidth;
8007 if (infoPtr->himlSmall && (nColumn == 0 || (LISTVIEW_GetColumnInfo(infoPtr, nColumn)->fmt & LVCFMT_IMAGE)))
8008 max_cx += infoPtr->iconSize.cx;
8009 max_cx += TRAILING_LABEL_PADDING;
8012 /* autosize based on listview items width */
8013 if(cx == LVSCW_AUTOSIZE)
8015 else if(cx == LVSCW_AUTOSIZE_USEHEADER)
8017 /* if iCol is the last column make it fill the remainder of the controls width */
8018 if(nColumn == DPA_GetPtrCount(infoPtr->hdpaColumns) - 1)
8023 LISTVIEW_GetOrigin(infoPtr, &Origin);
8024 LISTVIEW_GetHeaderRect(infoPtr, nColumn, &rcHeader);
8026 cx = infoPtr->rcList.right - Origin.x - rcHeader.left;
8030 /* Despite what the MS docs say, if this is not the last
8031 column, then MS resizes the column to the width of the
8032 largest text string in the column, including headers
8033 and items. This is different from LVSCW_AUTOSIZE in that
8034 LVSCW_AUTOSIZE ignores the header string length. */
8037 /* retrieve header text */
8038 hdi.mask = HDI_TEXT;
8039 hdi.cchTextMax = DISP_TEXT_SIZE;
8040 hdi.pszText = szDispText;
8041 if (SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nColumn, (LPARAM)&hdi))
8043 HDC hdc = GetDC(infoPtr->hwndSelf);
8044 HFONT old_font = SelectObject(hdc, (HFONT)SendMessageW(infoPtr->hwndHeader, WM_GETFONT, 0, 0));
8047 if (GetTextExtentPoint32W(hdc, hdi.pszText, lstrlenW(hdi.pszText), &size))
8048 cx = size.cx + TRAILING_HEADER_PADDING;
8049 /* FIXME: Take into account the header image, if one is present */
8050 SelectObject(hdc, old_font);
8051 ReleaseDC(infoPtr->hwndSelf, hdc);
8053 cx = max (cx, max_cx);
8057 if (cx < 0) return FALSE;
8059 /* call header to update the column change */
8060 hdi.mask = HDI_WIDTH;
8061 hdi.cxy = max(cx, LISTVIEW_GetColumnInfo(infoPtr, nColumn)->cxMin);
8062 TRACE("hdi.cxy=%d\n", hdi.cxy);
8063 return SendMessageW(infoPtr->hwndHeader, HDM_SETITEMW, nColumn, (LPARAM)&hdi);
8067 * Creates the checkbox imagelist. Helper for LISTVIEW_SetExtendedListViewStyle
8070 static HIMAGELIST LISTVIEW_CreateCheckBoxIL(const LISTVIEW_INFO *infoPtr)
8073 HBITMAP hbm_im, hbm_mask, hbm_orig;
8075 HBRUSH hbr_white = GetStockObject(WHITE_BRUSH);
8076 HBRUSH hbr_black = GetStockObject(BLACK_BRUSH);
8079 himl = ImageList_Create(GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON),
8080 ILC_COLOR | ILC_MASK, 2, 2);
8081 hdc_wnd = GetDC(infoPtr->hwndSelf);
8082 hdc = CreateCompatibleDC(hdc_wnd);
8083 hbm_im = CreateCompatibleBitmap(hdc_wnd, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON));
8084 hbm_mask = CreateBitmap(GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), 1, 1, NULL);
8085 ReleaseDC(infoPtr->hwndSelf, hdc_wnd);
8087 rc.left = rc.top = 0;
8088 rc.right = GetSystemMetrics(SM_CXSMICON);
8089 rc.bottom = GetSystemMetrics(SM_CYSMICON);
8091 hbm_orig = SelectObject(hdc, hbm_mask);
8092 FillRect(hdc, &rc, hbr_white);
8093 InflateRect(&rc, -2, -2);
8094 FillRect(hdc, &rc, hbr_black);
8096 SelectObject(hdc, hbm_im);
8097 DrawFrameControl(hdc, &rc, DFC_BUTTON, DFCS_BUTTONCHECK | DFCS_MONO);
8098 SelectObject(hdc, hbm_orig);
8099 ImageList_Add(himl, hbm_im, hbm_mask);
8101 SelectObject(hdc, hbm_im);
8102 DrawFrameControl(hdc, &rc, DFC_BUTTON, DFCS_BUTTONCHECK | DFCS_MONO | DFCS_CHECKED);
8103 SelectObject(hdc, hbm_orig);
8104 ImageList_Add(himl, hbm_im, hbm_mask);
8106 DeleteObject(hbm_mask);
8107 DeleteObject(hbm_im);
8115 * Sets the extended listview style.
8118 * [I] infoPtr : valid pointer to the listview structure
8120 * [I] dwStyle : style
8123 * SUCCESS : previous style
8126 static DWORD LISTVIEW_SetExtendedListViewStyle(LISTVIEW_INFO *infoPtr, DWORD dwMask, DWORD dwExStyle)
8128 DWORD dwOldExStyle = infoPtr->dwLvExStyle;
8132 infoPtr->dwLvExStyle = (dwOldExStyle & ~dwMask) | (dwExStyle & dwMask);
8134 infoPtr->dwLvExStyle = dwExStyle;
8136 if((infoPtr->dwLvExStyle ^ dwOldExStyle) & LVS_EX_CHECKBOXES)
8138 HIMAGELIST himl = 0;
8139 if(infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES)
8142 item.mask = LVIF_STATE;
8143 item.stateMask = LVIS_STATEIMAGEMASK;
8144 item.state = INDEXTOSTATEIMAGEMASK(1);
8145 LISTVIEW_SetItemState(infoPtr, -1, &item);
8147 himl = LISTVIEW_CreateCheckBoxIL(infoPtr);
8148 if(!(infoPtr->dwStyle & LVS_SHAREIMAGELISTS))
8149 ImageList_Destroy(infoPtr->himlState);
8151 himl = LISTVIEW_SetImageList(infoPtr, LVSIL_STATE, himl);
8152 /* checkbox list replaces prevous custom list or... */
8153 if(((infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES) &&
8154 !(infoPtr->dwStyle & LVS_SHAREIMAGELISTS)) ||
8155 /* ...previous was checkbox list */
8156 (dwOldExStyle & LVS_EX_CHECKBOXES))
8157 ImageList_Destroy(himl);
8160 if((infoPtr->dwLvExStyle ^ dwOldExStyle) & LVS_EX_HEADERDRAGDROP)
8164 /* if not already created */
8165 LISTVIEW_CreateHeader(infoPtr);
8167 dwStyle = GetWindowLongW(infoPtr->hwndHeader, GWL_STYLE);
8168 if (infoPtr->dwLvExStyle & LVS_EX_HEADERDRAGDROP)
8169 dwStyle |= HDS_DRAGDROP;
8171 dwStyle &= ~HDS_DRAGDROP;
8172 SetWindowLongW(infoPtr->hwndHeader, GWL_STYLE, dwStyle);
8175 /* GRIDLINES adds decoration at top so changes sizes */
8176 if((infoPtr->dwLvExStyle ^ dwOldExStyle) & LVS_EX_GRIDLINES)
8178 LISTVIEW_UpdateSize(infoPtr);
8181 if((infoPtr->dwLvExStyle ^ dwOldExStyle) & LVS_EX_TRANSPARENTBKGND)
8183 if (infoPtr->dwLvExStyle & LVS_EX_TRANSPARENTBKGND)
8184 LISTVIEW_SetBkColor(infoPtr, CLR_NONE);
8187 LISTVIEW_InvalidateList(infoPtr);
8188 return dwOldExStyle;
8193 * Sets the new hot cursor used during hot tracking and hover selection.
8196 * [I] infoPtr : valid pointer to the listview structure
8197 * [I] hCursor : the new hot cursor handle
8200 * Returns the previous hot cursor
8202 static HCURSOR LISTVIEW_SetHotCursor(LISTVIEW_INFO *infoPtr, HCURSOR hCursor)
8204 HCURSOR oldCursor = infoPtr->hHotCursor;
8206 infoPtr->hHotCursor = hCursor;
8214 * Sets the hot item index.
8217 * [I] infoPtr : valid pointer to the listview structure
8218 * [I] iIndex : index
8221 * SUCCESS : previous hot item index
8222 * FAILURE : -1 (no hot item)
8224 static INT LISTVIEW_SetHotItem(LISTVIEW_INFO *infoPtr, INT iIndex)
8226 INT iOldIndex = infoPtr->nHotItem;
8228 infoPtr->nHotItem = iIndex;
8236 * Sets the amount of time the cursor must hover over an item before it is selected.
8239 * [I] infoPtr : valid pointer to the listview structure
8240 * [I] dwHoverTime : hover time, if -1 the hover time is set to the default
8243 * Returns the previous hover time
8245 static DWORD LISTVIEW_SetHoverTime(LISTVIEW_INFO *infoPtr, DWORD dwHoverTime)
8247 DWORD oldHoverTime = infoPtr->dwHoverTime;
8249 infoPtr->dwHoverTime = dwHoverTime;
8251 return oldHoverTime;
8256 * Sets spacing for icons of LVS_ICON style.
8259 * [I] infoPtr : valid pointer to the listview structure
8260 * [I] cx : horizontal spacing (-1 = system spacing, 0 = autosize)
8261 * [I] cy : vertical spacing (-1 = system spacing, 0 = autosize)
8264 * MAKELONG(oldcx, oldcy)
8266 static DWORD LISTVIEW_SetIconSpacing(LISTVIEW_INFO *infoPtr, INT cx, INT cy)
8268 DWORD oldspacing = MAKELONG(infoPtr->iconSpacing.cx, infoPtr->iconSpacing.cy);
8270 TRACE("requested=(%d,%d)\n", cx, cy);
8272 /* this is supported only for LVS_ICON style */
8273 if (infoPtr->uView != LV_VIEW_ICON) return oldspacing;
8275 /* set to defaults, if instructed to */
8276 if (cx == -1) cx = GetSystemMetrics(SM_CXICONSPACING);
8277 if (cy == -1) cy = GetSystemMetrics(SM_CYICONSPACING);
8279 /* if 0 then compute width
8280 * FIXME: Should scan each item and determine max width of
8281 * icon or label, then make that the width */
8283 cx = infoPtr->iconSpacing.cx;
8285 /* if 0 then compute height */
8287 cy = infoPtr->iconSize.cy + 2 * infoPtr->ntmHeight +
8288 ICON_BOTTOM_PADDING + ICON_TOP_PADDING + LABEL_VERT_PADDING;
8291 infoPtr->iconSpacing.cx = cx;
8292 infoPtr->iconSpacing.cy = cy;
8294 TRACE("old=(%d,%d), new=(%d,%d), iconSize=(%d,%d), ntmH=%d\n",
8295 LOWORD(oldspacing), HIWORD(oldspacing), cx, cy,
8296 infoPtr->iconSize.cx, infoPtr->iconSize.cy,
8297 infoPtr->ntmHeight);
8299 /* these depend on the iconSpacing */
8300 LISTVIEW_UpdateItemSize(infoPtr);
8305 static inline void set_icon_size(SIZE *size, HIMAGELIST himl, BOOL small)
8309 if (himl && ImageList_GetIconSize(himl, &cx, &cy))
8316 size->cx = GetSystemMetrics(small ? SM_CXSMICON : SM_CXICON);
8317 size->cy = GetSystemMetrics(small ? SM_CYSMICON : SM_CYICON);
8326 * [I] infoPtr : valid pointer to the listview structure
8327 * [I] nType : image list type
8328 * [I] himl : image list handle
8331 * SUCCESS : old image list
8334 static HIMAGELIST LISTVIEW_SetImageList(LISTVIEW_INFO *infoPtr, INT nType, HIMAGELIST himl)
8336 INT oldHeight = infoPtr->nItemHeight;
8337 HIMAGELIST himlOld = 0;
8339 TRACE("(nType=%d, himl=%p\n", nType, himl);
8344 himlOld = infoPtr->himlNormal;
8345 infoPtr->himlNormal = himl;
8346 if (infoPtr->uView == LV_VIEW_ICON) set_icon_size(&infoPtr->iconSize, himl, FALSE);
8347 LISTVIEW_SetIconSpacing(infoPtr, 0, 0);
8351 himlOld = infoPtr->himlSmall;
8352 infoPtr->himlSmall = himl;
8353 if (infoPtr->uView != LV_VIEW_ICON) set_icon_size(&infoPtr->iconSize, himl, TRUE);
8357 himlOld = infoPtr->himlState;
8358 infoPtr->himlState = himl;
8359 set_icon_size(&infoPtr->iconStateSize, himl, TRUE);
8360 ImageList_SetBkColor(infoPtr->himlState, CLR_NONE);
8364 ERR("Unknown icon type=%d\n", nType);
8368 infoPtr->nItemHeight = LISTVIEW_CalculateItemHeight(infoPtr);
8369 if (infoPtr->nItemHeight != oldHeight)
8370 LISTVIEW_UpdateScroll(infoPtr);
8377 * Preallocates memory (does *not* set the actual count of items !)
8380 * [I] infoPtr : valid pointer to the listview structure
8381 * [I] nItems : item count (projected number of items to allocate)
8382 * [I] dwFlags : update flags
8388 static BOOL LISTVIEW_SetItemCount(LISTVIEW_INFO *infoPtr, INT nItems, DWORD dwFlags)
8390 TRACE("(nItems=%d, dwFlags=%x)\n", nItems, dwFlags);
8392 if (infoPtr->dwStyle & LVS_OWNERDATA)
8394 INT nOldCount = infoPtr->nItemCount;
8396 if (nItems < nOldCount)
8398 RANGE range = { nItems, nOldCount };
8399 ranges_del(infoPtr->selectionRanges, range);
8400 if (infoPtr->nFocusedItem >= nItems)
8402 LISTVIEW_SetItemFocus(infoPtr, -1);
8403 SetRectEmpty(&infoPtr->rcFocus);
8407 infoPtr->nItemCount = nItems;
8408 LISTVIEW_UpdateScroll(infoPtr);
8410 /* the flags are valid only in ownerdata report and list modes */
8411 if (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON) dwFlags = 0;
8413 if (!(dwFlags & LVSICF_NOSCROLL) && infoPtr->nFocusedItem != -1)
8414 LISTVIEW_EnsureVisible(infoPtr, infoPtr->nFocusedItem, FALSE);
8416 if (!(dwFlags & LVSICF_NOINVALIDATEALL))
8417 LISTVIEW_InvalidateList(infoPtr);
8424 LISTVIEW_GetOrigin(infoPtr, &Origin);
8425 nFrom = min(nOldCount, nItems);
8426 nTo = max(nOldCount, nItems);
8428 if (infoPtr->uView == LV_VIEW_DETAILS)
8431 rcErase.top = nFrom * infoPtr->nItemHeight;
8432 rcErase.right = infoPtr->nItemWidth;
8433 rcErase.bottom = nTo * infoPtr->nItemHeight;
8434 OffsetRect(&rcErase, Origin.x, Origin.y);
8435 if (IntersectRect(&rcErase, &rcErase, &infoPtr->rcList))
8436 LISTVIEW_InvalidateRect(infoPtr, &rcErase);
8438 else /* LV_VIEW_LIST */
8440 INT nPerCol = LISTVIEW_GetCountPerColumn(infoPtr);
8442 rcErase.left = (nFrom / nPerCol) * infoPtr->nItemWidth;
8443 rcErase.top = (nFrom % nPerCol) * infoPtr->nItemHeight;
8444 rcErase.right = rcErase.left + infoPtr->nItemWidth;
8445 rcErase.bottom = nPerCol * infoPtr->nItemHeight;
8446 OffsetRect(&rcErase, Origin.x, Origin.y);
8447 if (IntersectRect(&rcErase, &rcErase, &infoPtr->rcList))
8448 LISTVIEW_InvalidateRect(infoPtr, &rcErase);
8450 rcErase.left = (nFrom / nPerCol + 1) * infoPtr->nItemWidth;
8452 rcErase.right = (nTo / nPerCol + 1) * infoPtr->nItemWidth;
8453 rcErase.bottom = nPerCol * infoPtr->nItemHeight;
8454 OffsetRect(&rcErase, Origin.x, Origin.y);
8455 if (IntersectRect(&rcErase, &rcErase, &infoPtr->rcList))
8456 LISTVIEW_InvalidateRect(infoPtr, &rcErase);
8462 /* According to MSDN for non-LVS_OWNERDATA this is just
8463 * a performance issue. The control allocates its internal
8464 * data structures for the number of items specified. It
8465 * cuts down on the number of memory allocations. Therefore
8466 * we will just issue a WARN here
8468 WARN("for non-ownerdata performance option not implemented.\n");
8476 * Sets the position of an item.
8479 * [I] infoPtr : valid pointer to the listview structure
8480 * [I] nItem : item index
8481 * [I] pt : coordinate
8487 static BOOL LISTVIEW_SetItemPosition(LISTVIEW_INFO *infoPtr, INT nItem, POINT pt)
8491 TRACE("(nItem=%d, &pt=%s\n", nItem, wine_dbgstr_point(&pt));
8493 if (nItem < 0 || nItem >= infoPtr->nItemCount ||
8494 !(infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON)) return FALSE;
8496 LISTVIEW_GetOrigin(infoPtr, &Origin);
8498 /* This point value seems to be an undocumented feature.
8499 * The best guess is that it means either at the origin,
8500 * or at true beginning of the list. I will assume the origin. */
8501 if ((pt.x == -1) && (pt.y == -1))
8504 if (infoPtr->uView == LV_VIEW_ICON)
8506 pt.x -= (infoPtr->nItemWidth - infoPtr->iconSize.cx) / 2;
8507 pt.y -= ICON_TOP_PADDING;
8512 infoPtr->bAutoarrange = FALSE;
8514 return LISTVIEW_MoveIconTo(infoPtr, nItem, &pt, FALSE);
8519 * Sets the state of one or many items.
8522 * [I] infoPtr : valid pointer to the listview structure
8523 * [I] nItem : item index
8524 * [I] lpLVItem : item or subitem info
8530 static BOOL LISTVIEW_SetItemState(LISTVIEW_INFO *infoPtr, INT nItem, const LVITEMW *lpLVItem)
8532 BOOL bResult = TRUE;
8535 lvItem.iItem = nItem;
8536 lvItem.iSubItem = 0;
8537 lvItem.mask = LVIF_STATE;
8538 lvItem.state = lpLVItem->state;
8539 lvItem.stateMask = lpLVItem->stateMask;
8540 TRACE("lvItem=%s\n", debuglvitem_t(&lvItem, TRUE));
8544 /* select all isn't allowed in LVS_SINGLESEL */
8545 if ((lvItem.state & lvItem.stateMask & LVIS_SELECTED) && (infoPtr->dwStyle & LVS_SINGLESEL))
8548 /* focus all isn't allowed */
8549 if (lvItem.state & lvItem.stateMask & LVIS_FOCUSED) return FALSE;
8551 /* apply to all items */
8552 for (lvItem.iItem = 0; lvItem.iItem < infoPtr->nItemCount; lvItem.iItem++)
8553 if (!LISTVIEW_SetItemT(infoPtr, &lvItem, TRUE)) bResult = FALSE;
8556 bResult = LISTVIEW_SetItemT(infoPtr, &lvItem, TRUE);
8563 * Sets the text of an item or subitem.
8566 * [I] hwnd : window handle
8567 * [I] nItem : item index
8568 * [I] lpLVItem : item or subitem info
8569 * [I] isW : TRUE if input is Unicode
8575 static BOOL LISTVIEW_SetItemTextT(LISTVIEW_INFO *infoPtr, INT nItem, const LVITEMW *lpLVItem, BOOL isW)
8579 if (nItem < 0 && nItem >= infoPtr->nItemCount) return FALSE;
8581 lvItem.iItem = nItem;
8582 lvItem.iSubItem = lpLVItem->iSubItem;
8583 lvItem.mask = LVIF_TEXT;
8584 lvItem.pszText = lpLVItem->pszText;
8585 lvItem.cchTextMax = lpLVItem->cchTextMax;
8587 TRACE("(nItem=%d, lpLVItem=%s, isW=%d)\n", nItem, debuglvitem_t(&lvItem, isW), isW);
8589 return LISTVIEW_SetItemT(infoPtr, &lvItem, isW);
8594 * Set item index that marks the start of a multiple selection.
8597 * [I] infoPtr : valid pointer to the listview structure
8598 * [I] nIndex : index
8601 * Index number or -1 if there is no selection mark.
8603 static INT LISTVIEW_SetSelectionMark(LISTVIEW_INFO *infoPtr, INT nIndex)
8605 INT nOldIndex = infoPtr->nSelectionMark;
8607 TRACE("(nIndex=%d)\n", nIndex);
8609 infoPtr->nSelectionMark = nIndex;
8616 * Sets the text background color.
8619 * [I] infoPtr : valid pointer to the listview structure
8620 * [I] clrTextBk : text background color
8626 static BOOL LISTVIEW_SetTextBkColor(LISTVIEW_INFO *infoPtr, COLORREF clrTextBk)
8628 TRACE("(clrTextBk=%x)\n", clrTextBk);
8630 if (infoPtr->clrTextBk != clrTextBk)
8632 infoPtr->clrTextBk = clrTextBk;
8633 LISTVIEW_InvalidateList(infoPtr);
8641 * Sets the text foreground color.
8644 * [I] infoPtr : valid pointer to the listview structure
8645 * [I] clrText : text color
8651 static BOOL LISTVIEW_SetTextColor (LISTVIEW_INFO *infoPtr, COLORREF clrText)
8653 TRACE("(clrText=%x)\n", clrText);
8655 if (infoPtr->clrText != clrText)
8657 infoPtr->clrText = clrText;
8658 LISTVIEW_InvalidateList(infoPtr);
8666 * Sets new ToolTip window to ListView control.
8669 * [I] infoPtr : valid pointer to the listview structure
8670 * [I] hwndNewToolTip : handle to new ToolTip
8675 static HWND LISTVIEW_SetToolTips( LISTVIEW_INFO *infoPtr, HWND hwndNewToolTip)
8677 HWND hwndOldToolTip = infoPtr->hwndToolTip;
8678 infoPtr->hwndToolTip = hwndNewToolTip;
8679 return hwndOldToolTip;
8684 * sets the Unicode character format flag for the control
8686 * [I] infoPtr :valid pointer to the listview structure
8687 * [I] fUnicode :true to switch to UNICODE false to switch to ANSI
8690 * Old Unicode Format
8692 static BOOL LISTVIEW_SetUnicodeFormat( LISTVIEW_INFO *infoPtr, BOOL fUnicode)
8694 SHORT rc = infoPtr->notifyFormat;
8695 infoPtr->notifyFormat = (fUnicode) ? NFR_UNICODE : NFR_ANSI;
8696 return rc == NFR_UNICODE;
8701 * sets the control view mode
8703 * [I] infoPtr :valid pointer to the listview structure
8704 * [I] nView :new view mode value
8710 static INT LISTVIEW_SetView(LISTVIEW_INFO *infoPtr, DWORD nView)
8712 SIZE oldIconSize = infoPtr->iconSize;
8715 if (infoPtr->uView == nView) return 1;
8717 if ((INT)nView < 0 || nView > LV_VIEW_MAX) return -1;
8718 if (nView == LV_VIEW_TILE)
8720 FIXME("View LV_VIEW_TILE unimplemented\n");
8724 infoPtr->uView = nView;
8726 SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
8727 ShowWindow(infoPtr->hwndHeader, SW_HIDE);
8729 ShowScrollBar(infoPtr->hwndSelf, SB_BOTH, FALSE);
8730 SetRectEmpty(&infoPtr->rcFocus);
8732 himl = (nView == LV_VIEW_ICON ? infoPtr->himlNormal : infoPtr->himlSmall);
8733 set_icon_size(&infoPtr->iconSize, himl, nView != LV_VIEW_ICON);
8738 if ((infoPtr->iconSize.cx != oldIconSize.cx) || (infoPtr->iconSize.cy != oldIconSize.cy))
8740 TRACE("icon old size=(%d,%d), new size=(%d,%d)\n",
8741 oldIconSize.cx, oldIconSize.cy, infoPtr->iconSize.cx, infoPtr->iconSize.cy);
8742 LISTVIEW_SetIconSpacing(infoPtr, 0, 0);
8744 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
8746 case LV_VIEW_SMALLICON:
8747 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
8749 case LV_VIEW_DETAILS:
8754 LISTVIEW_CreateHeader( infoPtr );
8756 hl.prc = &infoPtr->rcList;
8758 SendMessageW(infoPtr->hwndHeader, HDM_LAYOUT, 0, (LPARAM)&hl);
8759 SetWindowPos(infoPtr->hwndHeader, infoPtr->hwndSelf, wp.x, wp.y, wp.cx, wp.cy,
8760 wp.flags | ((infoPtr->dwStyle & LVS_NOCOLUMNHEADER) ? SWP_HIDEWINDOW : SWP_SHOWWINDOW));
8767 LISTVIEW_UpdateItemSize(infoPtr);
8768 LISTVIEW_UpdateSize(infoPtr);
8769 LISTVIEW_UpdateScroll(infoPtr);
8770 LISTVIEW_InvalidateList(infoPtr);
8772 TRACE("nView=%d\n", nView);
8777 /* LISTVIEW_SetWorkAreas */
8781 * Callback internally used by LISTVIEW_SortItems() in response of LVM_SORTITEMS
8784 * [I] first : pointer to first ITEM_INFO to compare
8785 * [I] second : pointer to second ITEM_INFO to compare
8786 * [I] lParam : HWND of control
8789 * if first comes before second : negative
8790 * if first comes after second : positive
8791 * if first and second are equivalent : zero
8793 static INT WINAPI LISTVIEW_CallBackCompare(LPVOID first, LPVOID second, LPARAM lParam)
8795 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)lParam;
8796 ITEM_INFO* lv_first = DPA_GetPtr( first, 0 );
8797 ITEM_INFO* lv_second = DPA_GetPtr( second, 0 );
8799 /* Forward the call to the client defined callback */
8800 return (infoPtr->pfnCompare)( lv_first->lParam , lv_second->lParam, infoPtr->lParamSort );
8805 * Callback internally used by LISTVIEW_SortItems() in response of LVM_SORTITEMSEX
8808 * [I] first : pointer to first ITEM_INFO to compare
8809 * [I] second : pointer to second ITEM_INFO to compare
8810 * [I] lParam : HWND of control
8813 * if first comes before second : negative
8814 * if first comes after second : positive
8815 * if first and second are equivalent : zero
8817 static INT WINAPI LISTVIEW_CallBackCompareEx(LPVOID first, LPVOID second, LPARAM lParam)
8819 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)lParam;
8820 INT first_idx = DPA_GetPtrIndex( infoPtr->hdpaItems, first );
8821 INT second_idx = DPA_GetPtrIndex( infoPtr->hdpaItems, second );
8823 /* Forward the call to the client defined callback */
8824 return (infoPtr->pfnCompare)( first_idx, second_idx, infoPtr->lParamSort );
8829 * Sorts the listview items.
8832 * [I] infoPtr : valid pointer to the listview structure
8833 * [I] pfnCompare : application-defined value
8834 * [I] lParamSort : pointer to comparison callback
8835 * [I] IsEx : TRUE when LVM_SORTITEMSEX used
8841 static BOOL LISTVIEW_SortItems(LISTVIEW_INFO *infoPtr, PFNLVCOMPARE pfnCompare,
8842 LPARAM lParamSort, BOOL IsEx)
8846 LPVOID selectionMarkItem = NULL;
8847 LPVOID focusedItem = NULL;
8850 TRACE("(pfnCompare=%p, lParamSort=%lx)\n", pfnCompare, lParamSort);
8852 if (infoPtr->dwStyle & LVS_OWNERDATA) return FALSE;
8854 if (!pfnCompare) return FALSE;
8855 if (!infoPtr->hdpaItems) return FALSE;
8857 /* if there are 0 or 1 items, there is no need to sort */
8858 if (infoPtr->nItemCount < 2) return TRUE;
8860 /* clear selection */
8861 ranges_clear(infoPtr->selectionRanges);
8863 /* save selection mark and focused item */
8864 if (infoPtr->nSelectionMark >= 0)
8865 selectionMarkItem = DPA_GetPtr(infoPtr->hdpaItems, infoPtr->nSelectionMark);
8866 if (infoPtr->nFocusedItem >= 0)
8867 focusedItem = DPA_GetPtr(infoPtr->hdpaItems, infoPtr->nFocusedItem);
8869 infoPtr->pfnCompare = pfnCompare;
8870 infoPtr->lParamSort = lParamSort;
8872 DPA_Sort(infoPtr->hdpaItems, LISTVIEW_CallBackCompareEx, (LPARAM)infoPtr);
8874 DPA_Sort(infoPtr->hdpaItems, LISTVIEW_CallBackCompare, (LPARAM)infoPtr);
8876 /* restore selection ranges */
8877 for (i=0; i < infoPtr->nItemCount; i++)
8879 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, i);
8880 lpItem = DPA_GetPtr(hdpaSubItems, 0);
8882 if (lpItem->state & LVIS_SELECTED)
8883 ranges_additem(infoPtr->selectionRanges, i);
8885 /* restore selection mark and focused item */
8886 infoPtr->nSelectionMark = DPA_GetPtrIndex(infoPtr->hdpaItems, selectionMarkItem);
8887 infoPtr->nFocusedItem = DPA_GetPtrIndex(infoPtr->hdpaItems, focusedItem);
8889 /* I believe nHotItem should be left alone, see LISTVIEW_ShiftIndices */
8891 /* refresh the display */
8892 if (infoPtr->uView != LV_VIEW_ICON && infoPtr->uView != LV_VIEW_SMALLICON)
8893 LISTVIEW_InvalidateList(infoPtr);
8900 * Update theme handle after a theme change.
8903 * [I] infoPtr : valid pointer to the listview structure
8907 * FAILURE : something else
8909 static LRESULT LISTVIEW_ThemeChanged(const LISTVIEW_INFO *infoPtr)
8911 HTHEME theme = GetWindowTheme(infoPtr->hwndSelf);
8912 CloseThemeData(theme);
8913 OpenThemeData(infoPtr->hwndSelf, themeClass);
8919 * Updates an items or rearranges the listview control.
8922 * [I] infoPtr : valid pointer to the listview structure
8923 * [I] nItem : item index
8929 static BOOL LISTVIEW_Update(LISTVIEW_INFO *infoPtr, INT nItem)
8931 TRACE("(nItem=%d)\n", nItem);
8933 if (nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
8935 /* rearrange with default alignment style */
8936 if (is_autoarrange(infoPtr))
8937 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
8939 LISTVIEW_InvalidateItem(infoPtr, nItem);
8946 * Draw the track line at the place defined in the infoPtr structure.
8947 * The line is drawn with a XOR pen so drawing the line for the second time
8948 * in the same place erases the line.
8951 * [I] infoPtr : valid pointer to the listview structure
8957 static BOOL LISTVIEW_DrawTrackLine(const LISTVIEW_INFO *infoPtr)
8963 if (infoPtr->xTrackLine == -1)
8966 if (!(hdc = GetDC(infoPtr->hwndSelf)))
8968 hOldPen = SelectObject(hdc, GetStockObject(BLACK_PEN));
8969 oldROP = SetROP2(hdc, R2_XORPEN);
8970 MoveToEx(hdc, infoPtr->xTrackLine, infoPtr->rcList.top, NULL);
8971 LineTo(hdc, infoPtr->xTrackLine, infoPtr->rcList.bottom);
8972 SetROP2(hdc, oldROP);
8973 SelectObject(hdc, hOldPen);
8974 ReleaseDC(infoPtr->hwndSelf, hdc);
8980 * Called when an edit control should be displayed. This function is called after
8981 * we are sure that there was a single click - not a double click (this is a TIMERPROC).
8984 * [I] hwnd : Handle to the listview
8985 * [I] uMsg : WM_TIMER (ignored)
8986 * [I] idEvent : The timer ID interpreted as a pointer to a DELAYED_EDIT_ITEM struct
8987 * [I] dwTimer : The elapsed time (ignored)
8992 static VOID CALLBACK LISTVIEW_DelayedEditItem(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
8994 DELAYED_ITEM_EDIT *editItem = (DELAYED_ITEM_EDIT *)idEvent;
8995 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongPtrW(hwnd, 0);
8997 KillTimer(hwnd, idEvent);
8998 editItem->fEnabled = FALSE;
8999 /* check if the item is still selected */
9000 if (infoPtr->bFocus && LISTVIEW_GetItemState(infoPtr, editItem->iItem, LVIS_SELECTED))
9001 LISTVIEW_EditLabelT(infoPtr, editItem->iItem, TRUE);
9006 * Creates the listview control - the WM_NCCREATE phase.
9009 * [I] hwnd : window handle
9010 * [I] lpcs : the create parameters
9016 static LRESULT LISTVIEW_NCCreate(HWND hwnd, const CREATESTRUCTW *lpcs)
9018 LISTVIEW_INFO *infoPtr;
9021 TRACE("(lpcs=%p)\n", lpcs);
9023 /* initialize info pointer */
9024 infoPtr = Alloc(sizeof(LISTVIEW_INFO));
9025 if (!infoPtr) return FALSE;
9027 SetWindowLongPtrW(hwnd, 0, (DWORD_PTR)infoPtr);
9029 infoPtr->hwndSelf = hwnd;
9030 infoPtr->dwStyle = lpcs->style; /* Note: may be changed in WM_CREATE */
9031 map_style_view(infoPtr);
9032 /* determine the type of structures to use */
9033 infoPtr->hwndNotify = lpcs->hwndParent;
9034 /* infoPtr->notifyFormat will be filled in WM_CREATE */
9036 /* initialize color information */
9037 infoPtr->clrBk = CLR_NONE;
9038 infoPtr->clrText = CLR_DEFAULT;
9039 infoPtr->clrTextBk = CLR_DEFAULT;
9040 LISTVIEW_SetBkColor(infoPtr, comctl32_color.clrWindow);
9042 /* set default values */
9043 infoPtr->nFocusedItem = -1;
9044 infoPtr->nSelectionMark = -1;
9045 infoPtr->nHotItem = -1;
9046 infoPtr->bRedraw = TRUE;
9047 infoPtr->bNoItemMetrics = TRUE;
9048 infoPtr->bDoChangeNotify = TRUE;
9049 infoPtr->iconSpacing.cx = GetSystemMetrics(SM_CXICONSPACING);
9050 infoPtr->iconSpacing.cy = GetSystemMetrics(SM_CYICONSPACING);
9051 infoPtr->nEditLabelItem = -1;
9052 infoPtr->nLButtonDownItem = -1;
9053 infoPtr->dwHoverTime = -1; /* default system hover time */
9054 infoPtr->nMeasureItemHeight = 0;
9055 infoPtr->xTrackLine = -1; /* no track line */
9056 infoPtr->itemEdit.fEnabled = FALSE;
9057 infoPtr->iVersion = COMCTL32_VERSION;
9058 infoPtr->colRectsDirty = FALSE;
9060 /* get default font (icon title) */
9061 SystemParametersInfoW(SPI_GETICONTITLELOGFONT, 0, &logFont, 0);
9062 infoPtr->hDefaultFont = CreateFontIndirectW(&logFont);
9063 infoPtr->hFont = infoPtr->hDefaultFont;
9064 LISTVIEW_SaveTextMetrics(infoPtr);
9066 /* allocate memory for the data structure */
9067 if (!(infoPtr->selectionRanges = ranges_create(10))) goto fail;
9068 if (!(infoPtr->hdpaItems = DPA_Create(10))) goto fail;
9069 if (!(infoPtr->hdpaItemIds = DPA_Create(10))) goto fail;
9070 if (!(infoPtr->hdpaPosX = DPA_Create(10))) goto fail;
9071 if (!(infoPtr->hdpaPosY = DPA_Create(10))) goto fail;
9072 if (!(infoPtr->hdpaColumns = DPA_Create(10))) goto fail;
9076 DestroyWindow(infoPtr->hwndHeader);
9077 ranges_destroy(infoPtr->selectionRanges);
9078 DPA_Destroy(infoPtr->hdpaItems);
9079 DPA_Destroy(infoPtr->hdpaItemIds);
9080 DPA_Destroy(infoPtr->hdpaPosX);
9081 DPA_Destroy(infoPtr->hdpaPosY);
9082 DPA_Destroy(infoPtr->hdpaColumns);
9089 * Creates the listview control - the WM_CREATE phase. Most of the data is
9090 * already set up in LISTVIEW_NCCreate
9093 * [I] hwnd : window handle
9094 * [I] lpcs : the create parameters
9100 static LRESULT LISTVIEW_Create(HWND hwnd, const CREATESTRUCTW *lpcs)
9102 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongPtrW(hwnd, 0);
9104 TRACE("(lpcs=%p)\n", lpcs);
9106 infoPtr->dwStyle = lpcs->style;
9107 map_style_view(infoPtr);
9109 infoPtr->notifyFormat = SendMessageW(infoPtr->hwndNotify, WM_NOTIFYFORMAT,
9110 (WPARAM)infoPtr->hwndSelf, (LPARAM)NF_QUERY);
9111 /* on error defaulting to ANSI notifications */
9112 if (infoPtr->notifyFormat == 0) infoPtr->notifyFormat = NFR_ANSI;
9114 if ((infoPtr->uView == LV_VIEW_DETAILS) && (lpcs->style & WS_VISIBLE))
9116 if (LISTVIEW_CreateHeader(infoPtr) < 0) return -1;
9119 infoPtr->hwndHeader = 0;
9121 /* init item size to avoid division by 0 */
9122 LISTVIEW_UpdateItemSize (infoPtr);
9124 if (infoPtr->uView == LV_VIEW_DETAILS)
9126 if (!(LVS_NOCOLUMNHEADER & lpcs->style) && (WS_VISIBLE & lpcs->style))
9128 ShowWindow(infoPtr->hwndHeader, SW_SHOWNORMAL);
9130 LISTVIEW_UpdateScroll(infoPtr);
9131 /* send WM_MEASUREITEM notification */
9132 if (infoPtr->dwStyle & LVS_OWNERDRAWFIXED) notify_measureitem(infoPtr);
9135 OpenThemeData(hwnd, themeClass);
9137 /* initialize the icon sizes */
9138 set_icon_size(&infoPtr->iconSize, infoPtr->himlNormal, infoPtr->uView != LV_VIEW_ICON);
9139 set_icon_size(&infoPtr->iconStateSize, infoPtr->himlState, TRUE);
9145 * Destroys the listview control.
9148 * [I] infoPtr : valid pointer to the listview structure
9154 static LRESULT LISTVIEW_Destroy(const LISTVIEW_INFO *infoPtr)
9156 HTHEME theme = GetWindowTheme(infoPtr->hwndSelf);
9157 CloseThemeData(theme);
9163 * Enables the listview control.
9166 * [I] infoPtr : valid pointer to the listview structure
9167 * [I] bEnable : specifies whether to enable or disable the window
9173 static BOOL LISTVIEW_Enable(const LISTVIEW_INFO *infoPtr, BOOL bEnable)
9175 if (infoPtr->dwStyle & LVS_OWNERDRAWFIXED)
9176 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
9182 * Erases the background of the listview control.
9185 * [I] infoPtr : valid pointer to the listview structure
9186 * [I] hdc : device context handle
9192 static inline BOOL LISTVIEW_EraseBkgnd(const LISTVIEW_INFO *infoPtr, HDC hdc)
9196 TRACE("(hdc=%p)\n", hdc);
9198 if (!GetClipBox(hdc, &rc)) return FALSE;
9200 if (infoPtr->clrBk == CLR_NONE)
9202 if (infoPtr->dwLvExStyle & LVS_EX_TRANSPARENTBKGND)
9203 return SendMessageW(infoPtr->hwndNotify, WM_PRINTCLIENT,
9204 (WPARAM)hdc, PRF_ERASEBKGND);
9206 return SendMessageW(infoPtr->hwndNotify, WM_ERASEBKGND, (WPARAM)hdc, 0);
9209 /* for double buffered controls we need to do this during refresh */
9210 if (infoPtr->dwLvExStyle & LVS_EX_DOUBLEBUFFER) return FALSE;
9212 return LISTVIEW_FillBkgnd(infoPtr, hdc, &rc);
9218 * Helper function for LISTVIEW_[HV]Scroll *only*.
9219 * Performs vertical/horizontal scrolling by a give amount.
9222 * [I] infoPtr : valid pointer to the listview structure
9223 * [I] dx : amount of horizontal scroll
9224 * [I] dy : amount of vertical scroll
9226 static void scroll_list(LISTVIEW_INFO *infoPtr, INT dx, INT dy)
9228 /* now we can scroll the list */
9229 ScrollWindowEx(infoPtr->hwndSelf, dx, dy, &infoPtr->rcList,
9230 &infoPtr->rcList, 0, 0, SW_ERASE | SW_INVALIDATE);
9231 /* if we have focus, adjust rect */
9232 OffsetRect(&infoPtr->rcFocus, dx, dy);
9233 UpdateWindow(infoPtr->hwndSelf);
9238 * Performs vertical scrolling.
9241 * [I] infoPtr : valid pointer to the listview structure
9242 * [I] nScrollCode : scroll code
9243 * [I] nScrollDiff : units to scroll in SB_INTERNAL mode, 0 otherwise
9244 * [I] hScrollWnd : scrollbar control window handle
9250 * SB_LINEUP/SB_LINEDOWN:
9251 * for LVS_ICON, LVS_SMALLICON is 37 by experiment
9252 * for LVS_REPORT is 1 line
9253 * for LVS_LIST cannot occur
9256 static LRESULT LISTVIEW_VScroll(LISTVIEW_INFO *infoPtr, INT nScrollCode,
9257 INT nScrollDiff, HWND hScrollWnd)
9259 INT nOldScrollPos, nNewScrollPos;
9260 SCROLLINFO scrollInfo;
9263 TRACE("(nScrollCode=%d(%s), nScrollDiff=%d)\n", nScrollCode,
9264 debugscrollcode(nScrollCode), nScrollDiff);
9266 if (infoPtr->hwndEdit) SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
9268 scrollInfo.cbSize = sizeof(SCROLLINFO);
9269 scrollInfo.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_TRACKPOS;
9271 is_an_icon = ((infoPtr->uView == LV_VIEW_ICON) || (infoPtr->uView == LV_VIEW_SMALLICON));
9273 if (!GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo)) return 1;
9275 nOldScrollPos = scrollInfo.nPos;
9276 switch (nScrollCode)
9282 nScrollDiff = (is_an_icon) ? -LISTVIEW_SCROLL_ICON_LINE_SIZE : -1;
9286 nScrollDiff = (is_an_icon) ? LISTVIEW_SCROLL_ICON_LINE_SIZE : 1;
9290 nScrollDiff = -scrollInfo.nPage;
9294 nScrollDiff = scrollInfo.nPage;
9297 case SB_THUMBPOSITION:
9299 nScrollDiff = scrollInfo.nTrackPos - scrollInfo.nPos;
9306 /* quit right away if pos isn't changing */
9307 if (nScrollDiff == 0) return 0;
9309 /* calculate new position, and handle overflows */
9310 nNewScrollPos = scrollInfo.nPos + nScrollDiff;
9311 if (nScrollDiff > 0) {
9312 if (nNewScrollPos < nOldScrollPos ||
9313 nNewScrollPos > scrollInfo.nMax)
9314 nNewScrollPos = scrollInfo.nMax;
9316 if (nNewScrollPos > nOldScrollPos ||
9317 nNewScrollPos < scrollInfo.nMin)
9318 nNewScrollPos = scrollInfo.nMin;
9321 /* set the new position, and reread in case it changed */
9322 scrollInfo.fMask = SIF_POS;
9323 scrollInfo.nPos = nNewScrollPos;
9324 nNewScrollPos = SetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo, TRUE);
9326 /* carry on only if it really changed */
9327 if (nNewScrollPos == nOldScrollPos) return 0;
9329 /* now adjust to client coordinates */
9330 nScrollDiff = nOldScrollPos - nNewScrollPos;
9331 if (infoPtr->uView == LV_VIEW_DETAILS) nScrollDiff *= infoPtr->nItemHeight;
9333 /* and scroll the window */
9334 scroll_list(infoPtr, 0, nScrollDiff);
9341 * Performs horizontal scrolling.
9344 * [I] infoPtr : valid pointer to the listview structure
9345 * [I] nScrollCode : scroll code
9346 * [I] nScrollDiff : units to scroll in SB_INTERNAL mode, 0 otherwise
9347 * [I] hScrollWnd : scrollbar control window handle
9353 * SB_LINELEFT/SB_LINERIGHT:
9354 * for LVS_ICON, LVS_SMALLICON 1 pixel
9355 * for LVS_REPORT is 1 pixel
9356 * for LVS_LIST is 1 column --> which is a 1 because the
9357 * scroll is based on columns not pixels
9360 static LRESULT LISTVIEW_HScroll(LISTVIEW_INFO *infoPtr, INT nScrollCode,
9361 INT nScrollDiff, HWND hScrollWnd)
9363 INT nOldScrollPos, nNewScrollPos;
9364 SCROLLINFO scrollInfo;
9366 TRACE("(nScrollCode=%d(%s), nScrollDiff=%d)\n", nScrollCode,
9367 debugscrollcode(nScrollCode), nScrollDiff);
9369 if (infoPtr->hwndEdit) SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
9371 scrollInfo.cbSize = sizeof(SCROLLINFO);
9372 scrollInfo.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_TRACKPOS;
9374 if (!GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo)) return 1;
9376 nOldScrollPos = scrollInfo.nPos;
9378 switch (nScrollCode)
9392 nScrollDiff = -scrollInfo.nPage;
9396 nScrollDiff = scrollInfo.nPage;
9399 case SB_THUMBPOSITION:
9401 nScrollDiff = scrollInfo.nTrackPos - scrollInfo.nPos;
9408 /* quit right away if pos isn't changing */
9409 if (nScrollDiff == 0) return 0;
9411 /* calculate new position, and handle overflows */
9412 nNewScrollPos = scrollInfo.nPos + nScrollDiff;
9413 if (nScrollDiff > 0) {
9414 if (nNewScrollPos < nOldScrollPos ||
9415 nNewScrollPos > scrollInfo.nMax)
9416 nNewScrollPos = scrollInfo.nMax;
9418 if (nNewScrollPos > nOldScrollPos ||
9419 nNewScrollPos < scrollInfo.nMin)
9420 nNewScrollPos = scrollInfo.nMin;
9423 /* set the new position, and reread in case it changed */
9424 scrollInfo.fMask = SIF_POS;
9425 scrollInfo.nPos = nNewScrollPos;
9426 nNewScrollPos = SetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo, TRUE);
9428 /* carry on only if it really changed */
9429 if (nNewScrollPos == nOldScrollPos) return 0;
9431 if (infoPtr->uView == LV_VIEW_DETAILS)
9432 LISTVIEW_UpdateHeaderSize(infoPtr, nNewScrollPos);
9434 /* now adjust to client coordinates */
9435 nScrollDiff = nOldScrollPos - nNewScrollPos;
9436 if (infoPtr->uView == LV_VIEW_LIST) nScrollDiff *= infoPtr->nItemWidth;
9438 /* and scroll the window */
9439 scroll_list(infoPtr, nScrollDiff, 0);
9444 static LRESULT LISTVIEW_MouseWheel(LISTVIEW_INFO *infoPtr, INT wheelDelta)
9446 INT gcWheelDelta = 0;
9447 INT pulScrollLines = 3;
9448 SCROLLINFO scrollInfo;
9450 TRACE("(wheelDelta=%d)\n", wheelDelta);
9452 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
9453 gcWheelDelta -= wheelDelta;
9455 scrollInfo.cbSize = sizeof(SCROLLINFO);
9456 scrollInfo.fMask = SIF_POS;
9458 switch(infoPtr->uView)
9461 case LV_VIEW_SMALLICON:
9463 * listview should be scrolled by a multiple of 37 dependently on its dimension or its visible item number
9464 * should be fixed in the future.
9466 LISTVIEW_VScroll(infoPtr, SB_INTERNAL, (gcWheelDelta < 0) ?
9467 -LISTVIEW_SCROLL_ICON_LINE_SIZE : LISTVIEW_SCROLL_ICON_LINE_SIZE, 0);
9470 case LV_VIEW_DETAILS:
9471 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
9473 int cLineScroll = min(LISTVIEW_GetCountPerColumn(infoPtr), pulScrollLines);
9474 cLineScroll *= (gcWheelDelta / WHEEL_DELTA);
9475 LISTVIEW_VScroll(infoPtr, SB_INTERNAL, cLineScroll, 0);
9480 LISTVIEW_HScroll(infoPtr, (gcWheelDelta < 0) ? SB_LINELEFT : SB_LINERIGHT, 0, 0);
9491 * [I] infoPtr : valid pointer to the listview structure
9492 * [I] nVirtualKey : virtual key
9493 * [I] lKeyData : key data
9498 static LRESULT LISTVIEW_KeyDown(LISTVIEW_INFO *infoPtr, INT nVirtualKey, LONG lKeyData)
9500 HWND hwndSelf = infoPtr->hwndSelf;
9502 NMLVKEYDOWN nmKeyDown;
9504 TRACE("(nVirtualKey=%d, lKeyData=%d)\n", nVirtualKey, lKeyData);
9506 /* send LVN_KEYDOWN notification */
9507 nmKeyDown.wVKey = nVirtualKey;
9508 nmKeyDown.flags = 0;
9509 notify_hdr(infoPtr, LVN_KEYDOWN, &nmKeyDown.hdr);
9510 if (!IsWindow(hwndSelf))
9513 switch (nVirtualKey)
9516 nItem = infoPtr->nFocusedItem;
9517 if (infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES)
9518 toggle_checkbox_state(infoPtr, infoPtr->nFocusedItem);
9522 if ((infoPtr->nItemCount > 0) && (infoPtr->nFocusedItem != -1))
9524 if (!notify(infoPtr, NM_RETURN)) return 0;
9525 if (!notify(infoPtr, LVN_ITEMACTIVATE)) return 0;
9530 if (infoPtr->nItemCount > 0)
9535 if (infoPtr->nItemCount > 0)
9536 nItem = infoPtr->nItemCount - 1;
9540 nItem = LISTVIEW_GetNextItem(infoPtr, infoPtr->nFocusedItem, LVNI_TOLEFT);
9544 nItem = LISTVIEW_GetNextItem(infoPtr, infoPtr->nFocusedItem, LVNI_ABOVE);
9548 nItem = LISTVIEW_GetNextItem(infoPtr, infoPtr->nFocusedItem, LVNI_TORIGHT);
9552 nItem = LISTVIEW_GetNextItem(infoPtr, infoPtr->nFocusedItem, LVNI_BELOW);
9556 if (infoPtr->uView == LV_VIEW_DETAILS)
9558 INT topidx = LISTVIEW_GetTopIndex(infoPtr);
9559 if (infoPtr->nFocusedItem == topidx)
9560 nItem = topidx - LISTVIEW_GetCountPerColumn(infoPtr) + 1;
9565 nItem = infoPtr->nFocusedItem - LISTVIEW_GetCountPerColumn(infoPtr)
9566 * LISTVIEW_GetCountPerRow(infoPtr);
9567 if(nItem < 0) nItem = 0;
9571 if (infoPtr->uView == LV_VIEW_DETAILS)
9573 INT topidx = LISTVIEW_GetTopIndex(infoPtr);
9574 INT cnt = LISTVIEW_GetCountPerColumn(infoPtr);
9575 if (infoPtr->nFocusedItem == topidx + cnt - 1)
9576 nItem = infoPtr->nFocusedItem + cnt - 1;
9578 nItem = topidx + cnt - 1;
9581 nItem = infoPtr->nFocusedItem + LISTVIEW_GetCountPerColumn(infoPtr)
9582 * LISTVIEW_GetCountPerRow(infoPtr);
9583 if(nItem >= infoPtr->nItemCount) nItem = infoPtr->nItemCount - 1;
9587 if ((nItem != -1) && (nItem != infoPtr->nFocusedItem || nVirtualKey == VK_SPACE))
9588 LISTVIEW_KeySelection(infoPtr, nItem, nVirtualKey == VK_SPACE);
9598 * [I] infoPtr : valid pointer to the listview structure
9603 static LRESULT LISTVIEW_KillFocus(LISTVIEW_INFO *infoPtr)
9607 /* if we did not have the focus, there's nothing to do */
9608 if (!infoPtr->bFocus) return 0;
9610 /* send NM_KILLFOCUS notification */
9611 if (!notify(infoPtr, NM_KILLFOCUS)) return 0;
9613 /* if we have a focus rectangle, get rid of it */
9614 LISTVIEW_ShowFocusRect(infoPtr, FALSE);
9616 /* if have a marquee selection, stop it */
9617 if (infoPtr->bMarqueeSelect)
9619 /* Remove the marquee rectangle and release our mouse capture */
9620 LISTVIEW_InvalidateRect(infoPtr, &infoPtr->marqueeRect);
9623 SetRect(&infoPtr->marqueeRect, 0, 0, 0, 0);
9625 infoPtr->bMarqueeSelect = FALSE;
9626 infoPtr->bScrolling = FALSE;
9627 KillTimer(infoPtr->hwndSelf, (UINT_PTR) infoPtr);
9630 /* set window focus flag */
9631 infoPtr->bFocus = FALSE;
9633 /* invalidate the selected items before resetting focus flag */
9634 LISTVIEW_InvalidateSelectedItems(infoPtr);
9641 * Processes double click messages (left mouse button).
9644 * [I] infoPtr : valid pointer to the listview structure
9645 * [I] wKey : key flag
9646 * [I] x,y : mouse coordinate
9651 static LRESULT LISTVIEW_LButtonDblClk(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
9653 LVHITTESTINFO htInfo;
9655 TRACE("(key=%hu, X=%hu, Y=%hu)\n", wKey, x, y);
9657 /* Cancel the item edition if any */
9658 if (infoPtr->itemEdit.fEnabled)
9660 KillTimer(infoPtr->hwndSelf, (UINT_PTR)&infoPtr->itemEdit);
9661 infoPtr->itemEdit.fEnabled = FALSE;
9664 /* send NM_RELEASEDCAPTURE notification */
9665 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
9670 /* send NM_DBLCLK notification */
9671 LISTVIEW_HitTest(infoPtr, &htInfo, TRUE, FALSE);
9672 if (!notify_click(infoPtr, NM_DBLCLK, &htInfo)) return 0;
9674 /* To send the LVN_ITEMACTIVATE, it must be on an Item */
9675 if(htInfo.iItem != -1) notify_itemactivate(infoPtr,&htInfo);
9682 * Processes mouse down messages (left mouse button).
9685 * infoPtr [I ] valid pointer to the listview structure
9686 * wKey [I ] key flag
9687 * x,y [I ] mouse coordinate
9692 static LRESULT LISTVIEW_LButtonDown(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
9694 LVHITTESTINFO lvHitTestInfo;
9695 static BOOL bGroupSelect = TRUE;
9696 POINT pt = { x, y };
9699 TRACE("(key=%hu, X=%hu, Y=%hu)\n", wKey, x, y);
9701 /* send NM_RELEASEDCAPTURE notification */
9702 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
9704 /* set left button down flag and record the click position */
9705 infoPtr->bLButtonDown = TRUE;
9706 infoPtr->ptClickPos = pt;
9707 infoPtr->bDragging = FALSE;
9708 infoPtr->bMarqueeSelect = FALSE;
9709 infoPtr->bScrolling = FALSE;
9711 lvHitTestInfo.pt.x = x;
9712 lvHitTestInfo.pt.y = y;
9714 nItem = LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, TRUE);
9715 TRACE("at %s, nItem=%d\n", wine_dbgstr_point(&pt), nItem);
9716 if ((nItem >= 0) && (nItem < infoPtr->nItemCount))
9718 if ((infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES) && (lvHitTestInfo.flags & LVHT_ONITEMSTATEICON))
9720 toggle_checkbox_state(infoPtr, nItem);
9724 if (infoPtr->dwStyle & LVS_SINGLESEL)
9726 if (LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED))
9727 infoPtr->nEditLabelItem = nItem;
9729 LISTVIEW_SetSelection(infoPtr, nItem);
9733 if ((wKey & MK_CONTROL) && (wKey & MK_SHIFT))
9737 if (!LISTVIEW_AddGroupSelection(infoPtr, nItem)) return 0;
9738 LISTVIEW_SetItemFocus(infoPtr, nItem);
9739 infoPtr->nSelectionMark = nItem;
9745 item.state = LVIS_SELECTED | LVIS_FOCUSED;
9746 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
9748 LISTVIEW_SetItemState(infoPtr,nItem,&item);
9749 infoPtr->nSelectionMark = nItem;
9752 else if (wKey & MK_CONTROL)
9756 bGroupSelect = (LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED) == 0);
9758 item.state = (bGroupSelect ? LVIS_SELECTED : 0) | LVIS_FOCUSED;
9759 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
9760 LISTVIEW_SetItemState(infoPtr, nItem, &item);
9761 infoPtr->nSelectionMark = nItem;
9763 else if (wKey & MK_SHIFT)
9765 LISTVIEW_SetGroupSelection(infoPtr, nItem);
9769 if (LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED))
9771 infoPtr->nEditLabelItem = nItem;
9772 infoPtr->nLButtonDownItem = nItem;
9774 LISTVIEW_SetItemFocus(infoPtr, nItem);
9777 /* set selection (clears other pre-existing selections) */
9778 LISTVIEW_SetSelection(infoPtr, nItem);
9782 if (infoPtr->dwLvExStyle & LVS_EX_ONECLICKACTIVATE)
9783 if(lvHitTestInfo.iItem != -1) notify_itemactivate(infoPtr,&lvHitTestInfo);
9787 if (!infoPtr->bFocus)
9788 SetFocus(infoPtr->hwndSelf);
9790 /* remove all selections */
9791 if (!(wKey & MK_CONTROL) && !(wKey & MK_SHIFT))
9792 LISTVIEW_DeselectAll(infoPtr);
9801 * Processes mouse up messages (left mouse button).
9804 * infoPtr [I ] valid pointer to the listview structure
9805 * wKey [I ] key flag
9806 * x,y [I ] mouse coordinate
9811 static LRESULT LISTVIEW_LButtonUp(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
9813 LVHITTESTINFO lvHitTestInfo;
9815 TRACE("(key=%hu, X=%hu, Y=%hu)\n", wKey, x, y);
9817 if (!infoPtr->bLButtonDown) return 0;
9819 lvHitTestInfo.pt.x = x;
9820 lvHitTestInfo.pt.y = y;
9822 /* send NM_CLICK notification */
9823 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
9824 if (!notify_click(infoPtr, NM_CLICK, &lvHitTestInfo)) return 0;
9826 /* set left button flag */
9827 infoPtr->bLButtonDown = FALSE;
9829 /* set a single selection, reset others */
9830 if(lvHitTestInfo.iItem == infoPtr->nLButtonDownItem && lvHitTestInfo.iItem != -1)
9831 LISTVIEW_SetSelection(infoPtr, infoPtr->nLButtonDownItem);
9832 infoPtr->nLButtonDownItem = -1;
9834 if (infoPtr->bDragging || infoPtr->bMarqueeSelect)
9836 /* Remove the marquee rectangle and release our mouse capture */
9837 if (infoPtr->bMarqueeSelect)
9839 LISTVIEW_InvalidateRect(infoPtr, &infoPtr->marqueeDrawRect);
9843 SetRect(&infoPtr->marqueeRect, 0, 0, 0, 0);
9844 SetRect(&infoPtr->marqueeDrawRect, 0, 0, 0, 0);
9846 infoPtr->bDragging = FALSE;
9847 infoPtr->bMarqueeSelect = FALSE;
9848 infoPtr->bScrolling = FALSE;
9850 KillTimer(infoPtr->hwndSelf, (UINT_PTR) infoPtr);
9854 /* if we clicked on a selected item, edit the label */
9855 if(lvHitTestInfo.iItem == infoPtr->nEditLabelItem && (lvHitTestInfo.flags & LVHT_ONITEMLABEL))
9857 /* we want to make sure the user doesn't want to do a double click. So we will
9858 * delay the edit. WM_LBUTTONDBLCLICK will cancel the timer
9860 infoPtr->itemEdit.fEnabled = TRUE;
9861 infoPtr->itemEdit.iItem = lvHitTestInfo.iItem;
9862 SetTimer(infoPtr->hwndSelf,
9863 (UINT_PTR)&infoPtr->itemEdit,
9864 GetDoubleClickTime(),
9865 LISTVIEW_DelayedEditItem);
9868 if (!infoPtr->bFocus)
9869 SetFocus(infoPtr->hwndSelf);
9876 * Destroys the listview control (called after WM_DESTROY).
9879 * [I] infoPtr : valid pointer to the listview structure
9884 static LRESULT LISTVIEW_NCDestroy(LISTVIEW_INFO *infoPtr)
9890 /* delete all items */
9891 LISTVIEW_DeleteAllItems(infoPtr, TRUE);
9893 /* destroy data structure */
9894 DPA_Destroy(infoPtr->hdpaItems);
9895 DPA_Destroy(infoPtr->hdpaItemIds);
9896 DPA_Destroy(infoPtr->hdpaPosX);
9897 DPA_Destroy(infoPtr->hdpaPosY);
9899 for (i = 0; i < DPA_GetPtrCount(infoPtr->hdpaColumns); i++)
9900 Free(DPA_GetPtr(infoPtr->hdpaColumns, i));
9901 DPA_Destroy(infoPtr->hdpaColumns);
9902 ranges_destroy(infoPtr->selectionRanges);
9904 /* destroy image lists */
9905 if (!(infoPtr->dwStyle & LVS_SHAREIMAGELISTS))
9907 ImageList_Destroy(infoPtr->himlNormal);
9908 ImageList_Destroy(infoPtr->himlSmall);
9909 ImageList_Destroy(infoPtr->himlState);
9912 /* destroy font, bkgnd brush */
9914 if (infoPtr->hDefaultFont) DeleteObject(infoPtr->hDefaultFont);
9915 if (infoPtr->clrBk != CLR_NONE) DeleteObject(infoPtr->hBkBrush);
9917 SetWindowLongPtrW(infoPtr->hwndSelf, 0, 0);
9919 /* free listview info pointer*/
9927 * Handles notifications from header.
9930 * [I] infoPtr : valid pointer to the listview structure
9931 * [I] nCtrlId : control identifier
9932 * [I] lpnmh : notification information
9937 static LRESULT LISTVIEW_HeaderNotification(LISTVIEW_INFO *infoPtr, const NMHEADERW *lpnmh)
9939 HWND hwndSelf = infoPtr->hwndSelf;
9941 TRACE("(lpnmh=%p)\n", lpnmh);
9943 if (!lpnmh || lpnmh->iItem < 0 || lpnmh->iItem >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return 0;
9945 switch (lpnmh->hdr.code)
9950 COLUMN_INFO *lpColumnInfo;
9954 if (!lpnmh->pitem || !(lpnmh->pitem->mask & HDI_WIDTH))
9957 /* remove the old line (if any) */
9958 LISTVIEW_DrawTrackLine(infoPtr);
9960 /* compute & draw the new line */
9961 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, lpnmh->iItem);
9962 x = lpColumnInfo->rcHeader.left + lpnmh->pitem->cxy;
9963 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
9964 infoPtr->xTrackLine = x + ptOrigin.x;
9965 LISTVIEW_DrawTrackLine(infoPtr);
9971 /* remove the track line (if any) */
9972 LISTVIEW_DrawTrackLine(infoPtr);
9973 infoPtr->xTrackLine = -1;
9977 notify_forward_header(infoPtr, lpnmh);
9978 return (infoPtr->dwLvExStyle & LVS_EX_HEADERDRAGDROP) == 0;
9981 infoPtr->colRectsDirty = TRUE;
9982 LISTVIEW_InvalidateList(infoPtr);
9983 notify_forward_header(infoPtr, lpnmh);
9986 case HDN_ITEMCHANGINGW:
9987 case HDN_ITEMCHANGINGA:
9988 return notify_forward_header(infoPtr, lpnmh);
9990 case HDN_ITEMCHANGEDW:
9991 case HDN_ITEMCHANGEDA:
9993 COLUMN_INFO *lpColumnInfo;
9997 notify_forward_header(infoPtr, lpnmh);
9998 if (!IsWindow(hwndSelf))
10001 if (!lpnmh->pitem || !(lpnmh->pitem->mask & HDI_WIDTH))
10003 hdi.mask = HDI_WIDTH;
10004 if (!SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, lpnmh->iItem, (LPARAM)&hdi)) return 0;
10008 cxy = lpnmh->pitem->cxy;
10010 /* determine how much we change since the last know position */
10011 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, lpnmh->iItem);
10012 dx = cxy - (lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left);
10015 lpColumnInfo->rcHeader.right += dx;
10017 hdi.mask = HDI_ORDER;
10018 SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, lpnmh->iItem, (LPARAM)&hdi);
10020 /* not the rightmost one */
10021 if (hdi.iOrder + 1 < DPA_GetPtrCount(infoPtr->hdpaColumns))
10023 INT nIndex = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX,
10024 hdi.iOrder + 1, 0);
10025 LISTVIEW_ScrollColumns(infoPtr, nIndex, dx);
10029 /* only needs to update the scrolls */
10030 infoPtr->nItemWidth += dx;
10031 LISTVIEW_UpdateScroll(infoPtr);
10033 LISTVIEW_UpdateItemSize(infoPtr);
10034 if (infoPtr->uView == LV_VIEW_DETAILS && is_redrawing(infoPtr))
10037 RECT rcCol = lpColumnInfo->rcHeader;
10039 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
10040 OffsetRect(&rcCol, ptOrigin.x, 0);
10042 rcCol.top = infoPtr->rcList.top;
10043 rcCol.bottom = infoPtr->rcList.bottom;
10045 /* resizing left-aligned columns leaves most of the left side untouched */
10046 if ((lpColumnInfo->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_LEFT)
10048 INT nMaxDirty = infoPtr->nEllipsisWidth + infoPtr->ntmMaxCharWidth;
10051 rcCol.left = max (rcCol.left, rcCol.right - nMaxDirty);
10054 /* when shrinking the last column clear the now unused field */
10055 if (hdi.iOrder == DPA_GetPtrCount(infoPtr->hdpaColumns) - 1)
10061 /* deal with right from rightmost column area */
10062 right.left = rcCol.right;
10063 right.top = rcCol.top;
10064 right.bottom = rcCol.bottom;
10065 right.right = infoPtr->rcList.right;
10067 LISTVIEW_InvalidateRect(infoPtr, &right);
10070 LISTVIEW_InvalidateRect(infoPtr, &rcCol);
10076 case HDN_ITEMCLICKW:
10077 case HDN_ITEMCLICKA:
10079 /* Handle sorting by Header Column */
10082 ZeroMemory(&nmlv, sizeof(NMLISTVIEW));
10084 nmlv.iSubItem = lpnmh->iItem;
10085 notify_listview(infoPtr, LVN_COLUMNCLICK, &nmlv);
10086 notify_forward_header(infoPtr, lpnmh);
10090 case HDN_DIVIDERDBLCLICKW:
10091 case HDN_DIVIDERDBLCLICKA:
10092 LISTVIEW_SetColumnWidth(infoPtr, lpnmh->iItem, LVSCW_AUTOSIZE);
10093 notify_forward_header(infoPtr, lpnmh);
10102 * Paint non-client area of control.
10105 * [I] infoPtr : valid pointer to the listview structureof the sender
10106 * [I] region : update region
10109 * TRUE - frame was painted
10110 * FALSE - call default window proc
10112 static BOOL LISTVIEW_NCPaint(const LISTVIEW_INFO *infoPtr, HRGN region)
10114 HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
10118 int cxEdge = GetSystemMetrics (SM_CXEDGE),
10119 cyEdge = GetSystemMetrics (SM_CYEDGE);
10121 if (!theme) return FALSE;
10123 GetWindowRect(infoPtr->hwndSelf, &r);
10125 cliprgn = CreateRectRgn (r.left + cxEdge, r.top + cyEdge,
10126 r.right - cxEdge, r.bottom - cyEdge);
10127 if (region != (HRGN)1)
10128 CombineRgn (cliprgn, cliprgn, region, RGN_AND);
10129 OffsetRect(&r, -r.left, -r.top);
10131 dc = GetDCEx(infoPtr->hwndSelf, region, DCX_WINDOW|DCX_INTERSECTRGN);
10132 OffsetRect(&r, -r.left, -r.top);
10134 if (IsThemeBackgroundPartiallyTransparent (theme, 0, 0))
10135 DrawThemeParentBackground(infoPtr->hwndSelf, dc, &r);
10136 DrawThemeBackground (theme, dc, 0, 0, &r, 0);
10137 ReleaseDC(infoPtr->hwndSelf, dc);
10139 /* Call default proc to get the scrollbars etc. painted */
10140 DefWindowProcW (infoPtr->hwndSelf, WM_NCPAINT, (WPARAM)cliprgn, 0);
10147 * Determines the type of structure to use.
10150 * [I] infoPtr : valid pointer to the listview structureof the sender
10151 * [I] hwndFrom : listview window handle
10152 * [I] nCommand : command specifying the nature of the WM_NOTIFYFORMAT
10157 static LRESULT LISTVIEW_NotifyFormat(LISTVIEW_INFO *infoPtr, HWND hwndFrom, INT nCommand)
10159 TRACE("(hwndFrom=%p, nCommand=%d)\n", hwndFrom, nCommand);
10161 if (nCommand == NF_REQUERY)
10162 infoPtr->notifyFormat = SendMessageW(infoPtr->hwndNotify, WM_NOTIFYFORMAT, (WPARAM)infoPtr->hwndSelf, NF_QUERY);
10164 return infoPtr->notifyFormat;
10169 * Paints/Repaints the listview control. Internal use.
10172 * [I] infoPtr : valid pointer to the listview structure
10173 * [I] hdc : device context handle
10178 static LRESULT LISTVIEW_Paint(LISTVIEW_INFO *infoPtr, HDC hdc)
10180 TRACE("(hdc=%p)\n", hdc);
10182 if (infoPtr->bNoItemMetrics && infoPtr->nItemCount)
10184 infoPtr->bNoItemMetrics = FALSE;
10185 LISTVIEW_UpdateItemSize(infoPtr);
10186 if (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON)
10187 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
10188 LISTVIEW_UpdateScroll(infoPtr);
10191 if (infoPtr->hwndHeader) UpdateWindow(infoPtr->hwndHeader);
10194 LISTVIEW_Refresh(infoPtr, hdc, NULL);
10199 hdc = BeginPaint(infoPtr->hwndSelf, &ps);
10200 if (!hdc) return 1;
10201 LISTVIEW_Refresh(infoPtr, hdc, ps.fErase ? &ps.rcPaint : NULL);
10202 EndPaint(infoPtr->hwndSelf, &ps);
10210 * Paints/Repaints the listview control, WM_PAINT handler.
10213 * [I] infoPtr : valid pointer to the listview structure
10214 * [I] hdc : device context handle
10219 static inline LRESULT LISTVIEW_WMPaint(LISTVIEW_INFO *infoPtr, HDC hdc)
10221 TRACE("(hdc=%p)\n", hdc);
10223 if (!is_redrawing(infoPtr))
10224 return DefWindowProcW (infoPtr->hwndSelf, WM_PAINT, (WPARAM)hdc, 0);
10226 return LISTVIEW_Paint(infoPtr, hdc);
10231 * Paints/Repaints the listview control.
10234 * [I] infoPtr : valid pointer to the listview structure
10235 * [I] hdc : device context handle
10236 * [I] options : drawing options
10241 static LRESULT LISTVIEW_PrintClient(LISTVIEW_INFO *infoPtr, HDC hdc, DWORD options)
10243 FIXME("Partial Stub: (hdc=%p options=0x%08x)\n", hdc, options);
10245 if ((options & PRF_CHECKVISIBLE) && !IsWindowVisible(infoPtr->hwndSelf))
10248 if (options & PRF_ERASEBKGND)
10249 LISTVIEW_EraseBkgnd(infoPtr, hdc);
10251 if (options & PRF_CLIENT)
10252 LISTVIEW_Paint(infoPtr, hdc);
10260 * Processes double click messages (right mouse button).
10263 * [I] infoPtr : valid pointer to the listview structure
10264 * [I] wKey : key flag
10265 * [I] x,y : mouse coordinate
10270 static LRESULT LISTVIEW_RButtonDblClk(const LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
10272 LVHITTESTINFO lvHitTestInfo;
10274 TRACE("(key=%hu,X=%hu,Y=%hu)\n", wKey, x, y);
10276 /* send NM_RELEASEDCAPTURE notification */
10277 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
10279 /* send NM_RDBLCLK notification */
10280 lvHitTestInfo.pt.x = x;
10281 lvHitTestInfo.pt.y = y;
10282 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
10283 notify_click(infoPtr, NM_RDBLCLK, &lvHitTestInfo);
10290 * Processes mouse down messages (right mouse button).
10293 * [I] infoPtr : valid pointer to the listview structure
10294 * [I] wKey : key flag
10295 * [I] x,y : mouse coordinate
10300 static LRESULT LISTVIEW_RButtonDown(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
10302 LVHITTESTINFO lvHitTestInfo;
10305 TRACE("(key=%hu,X=%hu,Y=%hu)\n", wKey, x, y);
10307 /* send NM_RELEASEDCAPTURE notification */
10308 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
10310 /* make sure the listview control window has the focus */
10311 if (!infoPtr->bFocus) SetFocus(infoPtr->hwndSelf);
10313 /* set right button down flag */
10314 infoPtr->bRButtonDown = TRUE;
10316 /* determine the index of the selected item */
10317 lvHitTestInfo.pt.x = x;
10318 lvHitTestInfo.pt.y = y;
10319 nItem = LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, TRUE);
10321 if ((nItem >= 0) && (nItem < infoPtr->nItemCount))
10323 LISTVIEW_SetItemFocus(infoPtr, nItem);
10324 if (!((wKey & MK_SHIFT) || (wKey & MK_CONTROL)) &&
10325 !LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED))
10326 LISTVIEW_SetSelection(infoPtr, nItem);
10330 LISTVIEW_DeselectAll(infoPtr);
10338 * Processes mouse up messages (right mouse button).
10341 * [I] infoPtr : valid pointer to the listview structure
10342 * [I] wKey : key flag
10343 * [I] x,y : mouse coordinate
10348 static LRESULT LISTVIEW_RButtonUp(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
10350 LVHITTESTINFO lvHitTestInfo;
10353 TRACE("(key=%hu,X=%hu,Y=%hu)\n", wKey, x, y);
10355 if (!infoPtr->bRButtonDown) return 0;
10357 /* set button flag */
10358 infoPtr->bRButtonDown = FALSE;
10360 /* Send NM_RCLICK notification */
10361 lvHitTestInfo.pt.x = x;
10362 lvHitTestInfo.pt.y = y;
10363 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
10364 if (!notify_click(infoPtr, NM_RCLICK, &lvHitTestInfo)) return 0;
10366 /* Change to screen coordinate for WM_CONTEXTMENU */
10367 pt = lvHitTestInfo.pt;
10368 ClientToScreen(infoPtr->hwndSelf, &pt);
10370 /* Send a WM_CONTEXTMENU message in response to the RBUTTONUP */
10371 SendMessageW(infoPtr->hwndSelf, WM_CONTEXTMENU,
10372 (WPARAM)infoPtr->hwndSelf, MAKELPARAM(pt.x, pt.y));
10383 * [I] infoPtr : valid pointer to the listview structure
10384 * [I] hwnd : window handle of window containing the cursor
10385 * [I] nHittest : hit-test code
10386 * [I] wMouseMsg : ideintifier of the mouse message
10389 * TRUE if cursor is set
10392 static BOOL LISTVIEW_SetCursor(const LISTVIEW_INFO *infoPtr, HWND hwnd, UINT nHittest, UINT wMouseMsg)
10394 LVHITTESTINFO lvHitTestInfo;
10396 if(!(LISTVIEW_isHotTracking(infoPtr))) return FALSE;
10398 if(!infoPtr->hHotCursor) return FALSE;
10400 GetCursorPos(&lvHitTestInfo.pt);
10401 if (LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, FALSE, FALSE) < 0) return FALSE;
10403 SetCursor(infoPtr->hHotCursor);
10413 * [I] infoPtr : valid pointer to the listview structure
10414 * [I] hwndLoseFocus : handle of previously focused window
10419 static LRESULT LISTVIEW_SetFocus(LISTVIEW_INFO *infoPtr, HWND hwndLoseFocus)
10421 TRACE("(hwndLoseFocus=%p)\n", hwndLoseFocus);
10423 /* if we have the focus already, there's nothing to do */
10424 if (infoPtr->bFocus) return 0;
10426 /* send NM_SETFOCUS notification */
10427 if (!notify(infoPtr, NM_SETFOCUS)) return 0;
10429 /* set window focus flag */
10430 infoPtr->bFocus = TRUE;
10432 /* put the focus rect back on */
10433 LISTVIEW_ShowFocusRect(infoPtr, TRUE);
10435 /* redraw all visible selected items */
10436 LISTVIEW_InvalidateSelectedItems(infoPtr);
10446 * [I] infoPtr : valid pointer to the listview structure
10447 * [I] fRedraw : font handle
10448 * [I] fRedraw : redraw flag
10453 static LRESULT LISTVIEW_SetFont(LISTVIEW_INFO *infoPtr, HFONT hFont, WORD fRedraw)
10455 HFONT oldFont = infoPtr->hFont;
10457 TRACE("(hfont=%p,redraw=%hu)\n", hFont, fRedraw);
10459 infoPtr->hFont = hFont ? hFont : infoPtr->hDefaultFont;
10460 if (infoPtr->hFont == oldFont) return 0;
10462 LISTVIEW_SaveTextMetrics(infoPtr);
10464 if (infoPtr->uView == LV_VIEW_DETAILS)
10466 SendMessageW(infoPtr->hwndHeader, WM_SETFONT, (WPARAM)hFont, MAKELPARAM(fRedraw, 0));
10467 LISTVIEW_UpdateSize(infoPtr);
10468 LISTVIEW_UpdateScroll(infoPtr);
10471 if (fRedraw) LISTVIEW_InvalidateList(infoPtr);
10478 * Message handling for WM_SETREDRAW.
10479 * For the Listview, it invalidates the entire window (the doc specifies otherwise)
10482 * [I] infoPtr : valid pointer to the listview structure
10483 * [I] bRedraw: state of redraw flag
10486 * DefWinProc return value
10488 static LRESULT LISTVIEW_SetRedraw(LISTVIEW_INFO *infoPtr, BOOL bRedraw)
10490 TRACE("infoPtr->bRedraw=%d, bRedraw=%d\n", infoPtr->bRedraw, bRedraw);
10492 /* we cannot use straight equality here because _any_ non-zero value is TRUE */
10493 if ((infoPtr->bRedraw && bRedraw) || (!infoPtr->bRedraw && !bRedraw)) return 0;
10495 infoPtr->bRedraw = bRedraw;
10497 if(!bRedraw) return 0;
10499 if (is_autoarrange(infoPtr))
10500 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
10501 LISTVIEW_UpdateScroll(infoPtr);
10503 /* despite what the WM_SETREDRAW docs says, apps expect us
10504 * to invalidate the listview here... stupid! */
10505 LISTVIEW_InvalidateList(infoPtr);
10512 * Resizes the listview control. This function processes WM_SIZE
10513 * messages. At this time, the width and height are not used.
10516 * [I] infoPtr : valid pointer to the listview structure
10517 * [I] Width : new width
10518 * [I] Height : new height
10523 static LRESULT LISTVIEW_Size(LISTVIEW_INFO *infoPtr, int Width, int Height)
10525 RECT rcOld = infoPtr->rcList;
10527 TRACE("(width=%d, height=%d)\n", Width, Height);
10529 LISTVIEW_UpdateSize(infoPtr);
10530 if (EqualRect(&rcOld, &infoPtr->rcList)) return 0;
10532 /* do not bother with display related stuff if we're not redrawing */
10533 if (!is_redrawing(infoPtr)) return 0;
10535 if (is_autoarrange(infoPtr))
10536 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
10538 LISTVIEW_UpdateScroll(infoPtr);
10540 /* refresh all only for lists whose height changed significantly */
10541 if ((infoPtr->uView == LV_VIEW_LIST) &&
10542 (rcOld.bottom - rcOld.top) / infoPtr->nItemHeight !=
10543 (infoPtr->rcList.bottom - infoPtr->rcList.top) / infoPtr->nItemHeight)
10544 LISTVIEW_InvalidateList(infoPtr);
10551 * Sets the size information.
10554 * [I] infoPtr : valid pointer to the listview structure
10559 static void LISTVIEW_UpdateSize(LISTVIEW_INFO *infoPtr)
10561 TRACE("uView=%d, rcList(old)=%s\n", infoPtr->uView, wine_dbgstr_rect(&infoPtr->rcList));
10563 GetClientRect(infoPtr->hwndSelf, &infoPtr->rcList);
10565 if (infoPtr->uView == LV_VIEW_LIST)
10567 /* Apparently the "LIST" style is supposed to have the same
10568 * number of items in a column even if there is no scroll bar.
10569 * Since if a scroll bar already exists then the bottom is already
10570 * reduced, only reduce if the scroll bar does not currently exist.
10571 * The "2" is there to mimic the native control. I think it may be
10572 * related to either padding or edges. (GLA 7/2002)
10574 if (!(infoPtr->dwStyle & WS_HSCROLL))
10575 infoPtr->rcList.bottom -= GetSystemMetrics(SM_CYHSCROLL);
10576 infoPtr->rcList.bottom = max (infoPtr->rcList.bottom - 2, 0);
10578 else if (infoPtr->uView == LV_VIEW_DETAILS)
10580 /* if control created invisible header isn't created */
10581 if (infoPtr->hwndHeader)
10586 hl.prc = &infoPtr->rcList;
10588 SendMessageW( infoPtr->hwndHeader, HDM_LAYOUT, 0, (LPARAM)&hl );
10589 TRACE(" wp.flags=0x%08x, wp=%d,%d (%dx%d)\n", wp.flags, wp.x, wp.y, wp.cx, wp.cy);
10590 SetWindowPos(wp.hwnd, wp.hwndInsertAfter, wp.x, wp.y, wp.cx, wp.cy,
10591 wp.flags | ((infoPtr->dwStyle & LVS_NOCOLUMNHEADER)
10592 ? SWP_HIDEWINDOW : SWP_SHOWWINDOW));
10593 TRACE(" after SWP wp=%d,%d (%dx%d)\n", wp.x, wp.y, wp.cx, wp.cy);
10595 infoPtr->rcList.top = max(wp.cy, 0);
10597 infoPtr->rcList.top += (infoPtr->dwLvExStyle & LVS_EX_GRIDLINES) ? 2 : 0;
10600 TRACE(" rcList=%s\n", wine_dbgstr_rect(&infoPtr->rcList));
10605 * Processes WM_STYLECHANGED messages.
10608 * [I] infoPtr : valid pointer to the listview structure
10609 * [I] wStyleType : window style type (normal or extended)
10610 * [I] lpss : window style information
10615 static INT LISTVIEW_StyleChanged(LISTVIEW_INFO *infoPtr, WPARAM wStyleType,
10616 const STYLESTRUCT *lpss)
10618 UINT uNewView = lpss->styleNew & LVS_TYPEMASK;
10619 UINT uOldView = lpss->styleOld & LVS_TYPEMASK;
10622 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
10623 wStyleType, lpss->styleOld, lpss->styleNew);
10625 if (wStyleType != GWL_STYLE) return 0;
10627 infoPtr->dwStyle = lpss->styleNew;
10628 map_style_view(infoPtr);
10630 if (((lpss->styleOld & WS_HSCROLL) != 0)&&
10631 ((lpss->styleNew & WS_HSCROLL) == 0))
10632 ShowScrollBar(infoPtr->hwndSelf, SB_HORZ, FALSE);
10634 if (((lpss->styleOld & WS_VSCROLL) != 0)&&
10635 ((lpss->styleNew & WS_VSCROLL) == 0))
10636 ShowScrollBar(infoPtr->hwndSelf, SB_VERT, FALSE);
10638 if (uNewView != uOldView)
10640 SIZE oldIconSize = infoPtr->iconSize;
10643 SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
10644 ShowWindow(infoPtr->hwndHeader, SW_HIDE);
10646 ShowScrollBar(infoPtr->hwndSelf, SB_BOTH, FALSE);
10647 SetRectEmpty(&infoPtr->rcFocus);
10649 himl = (uNewView == LVS_ICON ? infoPtr->himlNormal : infoPtr->himlSmall);
10650 set_icon_size(&infoPtr->iconSize, himl, uNewView != LVS_ICON);
10652 if (uNewView == LVS_ICON)
10654 if ((infoPtr->iconSize.cx != oldIconSize.cx) || (infoPtr->iconSize.cy != oldIconSize.cy))
10656 TRACE("icon old size=(%d,%d), new size=(%d,%d)\n",
10657 oldIconSize.cx, oldIconSize.cy, infoPtr->iconSize.cx, infoPtr->iconSize.cy);
10658 LISTVIEW_SetIconSpacing(infoPtr, 0, 0);
10661 else if (uNewView == LVS_REPORT)
10666 LISTVIEW_CreateHeader( infoPtr );
10668 hl.prc = &infoPtr->rcList;
10670 SendMessageW( infoPtr->hwndHeader, HDM_LAYOUT, 0, (LPARAM)&hl );
10671 SetWindowPos(infoPtr->hwndHeader, infoPtr->hwndSelf, wp.x, wp.y, wp.cx, wp.cy,
10672 wp.flags | ((infoPtr->dwStyle & LVS_NOCOLUMNHEADER)
10673 ? SWP_HIDEWINDOW : SWP_SHOWWINDOW));
10676 LISTVIEW_UpdateItemSize(infoPtr);
10679 if (uNewView == LVS_REPORT)
10681 if ((lpss->styleOld ^ lpss->styleNew) & LVS_NOCOLUMNHEADER)
10683 if (lpss->styleNew & LVS_NOCOLUMNHEADER)
10685 /* Turn off the header control */
10686 style = GetWindowLongW(infoPtr->hwndHeader, GWL_STYLE);
10687 TRACE("Hide header control, was 0x%08x\n", style);
10688 SetWindowLongW(infoPtr->hwndHeader, GWL_STYLE, style | HDS_HIDDEN);
10690 /* Turn on the header control */
10691 if ((style = GetWindowLongW(infoPtr->hwndHeader, GWL_STYLE)) & HDS_HIDDEN)
10693 TRACE("Show header control, was 0x%08x\n", style);
10694 SetWindowLongW(infoPtr->hwndHeader, GWL_STYLE, (style & ~HDS_HIDDEN) | WS_VISIBLE);
10700 if ( (uNewView == LVS_ICON || uNewView == LVS_SMALLICON) &&
10701 (uNewView != uOldView || ((lpss->styleNew ^ lpss->styleOld) & LVS_ALIGNMASK)) )
10702 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
10704 /* update the size of the client area */
10705 LISTVIEW_UpdateSize(infoPtr);
10707 /* add scrollbars if needed */
10708 LISTVIEW_UpdateScroll(infoPtr);
10710 /* invalidate client area + erase background */
10711 LISTVIEW_InvalidateList(infoPtr);
10718 * Processes WM_STYLECHANGING messages.
10721 * [I] infoPtr : valid pointer to the listview structure
10722 * [I] wStyleType : window style type (normal or extended)
10723 * [I0] lpss : window style information
10728 static INT LISTVIEW_StyleChanging(LISTVIEW_INFO *infoPtr, WPARAM wStyleType,
10731 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
10732 wStyleType, lpss->styleOld, lpss->styleNew);
10734 /* don't forward LVS_OWNERDATA only if not already set to */
10735 if ((lpss->styleNew ^ lpss->styleOld) & LVS_OWNERDATA)
10737 if (lpss->styleOld & LVS_OWNERDATA)
10738 lpss->styleNew |= LVS_OWNERDATA;
10740 lpss->styleNew &= ~LVS_OWNERDATA;
10748 * Processes WM_SHOWWINDOW messages.
10751 * [I] infoPtr : valid pointer to the listview structure
10752 * [I] bShown : window is being shown (FALSE when hidden)
10753 * [I] iStatus : window show status
10758 static LRESULT LISTVIEW_ShowWindow(LISTVIEW_INFO *infoPtr, BOOL bShown, INT iStatus)
10760 /* header delayed creation */
10761 if ((infoPtr->uView == LV_VIEW_DETAILS) && bShown)
10763 LISTVIEW_CreateHeader(infoPtr);
10765 if (!(LVS_NOCOLUMNHEADER & infoPtr->dwStyle))
10766 ShowWindow(infoPtr->hwndHeader, SW_SHOWNORMAL);
10774 * Processes CCM_GETVERSION messages.
10777 * [I] infoPtr : valid pointer to the listview structure
10782 static inline LRESULT LISTVIEW_GetVersion(const LISTVIEW_INFO *infoPtr)
10784 return infoPtr->iVersion;
10789 * Processes CCM_SETVERSION messages.
10792 * [I] infoPtr : valid pointer to the listview structure
10793 * [I] iVersion : version to be set
10796 * -1 when requested version is greater than DLL version;
10797 * previous version otherwise
10799 static LRESULT LISTVIEW_SetVersion(LISTVIEW_INFO *infoPtr, DWORD iVersion)
10801 INT iOldVersion = infoPtr->iVersion;
10803 if (iVersion > COMCTL32_VERSION)
10806 infoPtr->iVersion = iVersion;
10808 TRACE("new version %d\n", iVersion);
10810 return iOldVersion;
10815 * Window procedure of the listview control.
10818 static LRESULT WINAPI
10819 LISTVIEW_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
10821 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongPtrW(hwnd, 0);
10823 TRACE("(uMsg=%x wParam=%lx lParam=%lx)\n", uMsg, wParam, lParam);
10825 if (!infoPtr && (uMsg != WM_NCCREATE))
10826 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
10830 case LVM_APPROXIMATEVIEWRECT:
10831 return LISTVIEW_ApproximateViewRect(infoPtr, (INT)wParam,
10832 LOWORD(lParam), HIWORD(lParam));
10834 return LISTVIEW_Arrange(infoPtr, (INT)wParam);
10836 case LVM_CANCELEDITLABEL:
10837 return LISTVIEW_CancelEditLabel(infoPtr);
10839 case LVM_CREATEDRAGIMAGE:
10840 return (LRESULT)LISTVIEW_CreateDragImage(infoPtr, (INT)wParam, (LPPOINT)lParam);
10842 case LVM_DELETEALLITEMS:
10843 return LISTVIEW_DeleteAllItems(infoPtr, FALSE);
10845 case LVM_DELETECOLUMN:
10846 return LISTVIEW_DeleteColumn(infoPtr, (INT)wParam);
10848 case LVM_DELETEITEM:
10849 return LISTVIEW_DeleteItem(infoPtr, (INT)wParam);
10851 case LVM_EDITLABELW:
10852 return (LRESULT)LISTVIEW_EditLabelT(infoPtr, (INT)wParam, TRUE);
10854 case LVM_EDITLABELA:
10855 return (LRESULT)LISTVIEW_EditLabelT(infoPtr, (INT)wParam, FALSE);
10857 /* case LVM_ENABLEGROUPVIEW: */
10859 case LVM_ENSUREVISIBLE:
10860 return LISTVIEW_EnsureVisible(infoPtr, (INT)wParam, (BOOL)lParam);
10862 case LVM_FINDITEMW:
10863 return LISTVIEW_FindItemW(infoPtr, (INT)wParam, (LPLVFINDINFOW)lParam);
10865 case LVM_FINDITEMA:
10866 return LISTVIEW_FindItemA(infoPtr, (INT)wParam, (LPLVFINDINFOA)lParam);
10868 case LVM_GETBKCOLOR:
10869 return infoPtr->clrBk;
10871 /* case LVM_GETBKIMAGE: */
10873 case LVM_GETCALLBACKMASK:
10874 return infoPtr->uCallbackMask;
10876 case LVM_GETCOLUMNA:
10877 return LISTVIEW_GetColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam, FALSE);
10879 case LVM_GETCOLUMNW:
10880 return LISTVIEW_GetColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam, TRUE);
10882 case LVM_GETCOLUMNORDERARRAY:
10883 return LISTVIEW_GetColumnOrderArray(infoPtr, (INT)wParam, (LPINT)lParam);
10885 case LVM_GETCOLUMNWIDTH:
10886 return LISTVIEW_GetColumnWidth(infoPtr, (INT)wParam);
10888 case LVM_GETCOUNTPERPAGE:
10889 return LISTVIEW_GetCountPerPage(infoPtr);
10891 case LVM_GETEDITCONTROL:
10892 return (LRESULT)infoPtr->hwndEdit;
10894 case LVM_GETEXTENDEDLISTVIEWSTYLE:
10895 return infoPtr->dwLvExStyle;
10897 /* case LVM_GETGROUPINFO: */
10899 /* case LVM_GETGROUPMETRICS: */
10901 case LVM_GETHEADER:
10902 return (LRESULT)infoPtr->hwndHeader;
10904 case LVM_GETHOTCURSOR:
10905 return (LRESULT)infoPtr->hHotCursor;
10907 case LVM_GETHOTITEM:
10908 return infoPtr->nHotItem;
10910 case LVM_GETHOVERTIME:
10911 return infoPtr->dwHoverTime;
10913 case LVM_GETIMAGELIST:
10914 return (LRESULT)LISTVIEW_GetImageList(infoPtr, (INT)wParam);
10916 /* case LVM_GETINSERTMARK: */
10918 /* case LVM_GETINSERTMARKCOLOR: */
10920 /* case LVM_GETINSERTMARKRECT: */
10922 case LVM_GETISEARCHSTRINGA:
10923 case LVM_GETISEARCHSTRINGW:
10924 FIXME("LVM_GETISEARCHSTRING: unimplemented\n");
10928 return LISTVIEW_GetItemExtT(infoPtr, (LPLVITEMW)lParam, FALSE);
10931 return LISTVIEW_GetItemExtT(infoPtr, (LPLVITEMW)lParam, TRUE);
10933 case LVM_GETITEMCOUNT:
10934 return infoPtr->nItemCount;
10936 case LVM_GETITEMPOSITION:
10937 return LISTVIEW_GetItemPosition(infoPtr, (INT)wParam, (LPPOINT)lParam);
10939 case LVM_GETITEMRECT:
10940 return LISTVIEW_GetItemRect(infoPtr, (INT)wParam, (LPRECT)lParam);
10942 case LVM_GETITEMSPACING:
10943 return LISTVIEW_GetItemSpacing(infoPtr, (BOOL)wParam);
10945 case LVM_GETITEMSTATE:
10946 return LISTVIEW_GetItemState(infoPtr, (INT)wParam, (UINT)lParam);
10948 case LVM_GETITEMTEXTA:
10949 return LISTVIEW_GetItemTextT(infoPtr, (INT)wParam, (LPLVITEMW)lParam, FALSE);
10951 case LVM_GETITEMTEXTW:
10952 return LISTVIEW_GetItemTextT(infoPtr, (INT)wParam, (LPLVITEMW)lParam, TRUE);
10954 case LVM_GETNEXTITEM:
10955 return LISTVIEW_GetNextItem(infoPtr, (INT)wParam, LOWORD(lParam));
10957 case LVM_GETNUMBEROFWORKAREAS:
10958 FIXME("LVM_GETNUMBEROFWORKAREAS: unimplemented\n");
10961 case LVM_GETORIGIN:
10962 if (!lParam) return FALSE;
10963 if (infoPtr->uView == LV_VIEW_DETAILS ||
10964 infoPtr->uView == LV_VIEW_LIST) return FALSE;
10965 LISTVIEW_GetOrigin(infoPtr, (LPPOINT)lParam);
10968 /* case LVM_GETOUTLINECOLOR: */
10970 /* case LVM_GETSELECTEDCOLUMN: */
10972 case LVM_GETSELECTEDCOUNT:
10973 return LISTVIEW_GetSelectedCount(infoPtr);
10975 case LVM_GETSELECTIONMARK:
10976 return infoPtr->nSelectionMark;
10978 case LVM_GETSTRINGWIDTHA:
10979 return LISTVIEW_GetStringWidthT(infoPtr, (LPCWSTR)lParam, FALSE);
10981 case LVM_GETSTRINGWIDTHW:
10982 return LISTVIEW_GetStringWidthT(infoPtr, (LPCWSTR)lParam, TRUE);
10984 case LVM_GETSUBITEMRECT:
10985 return LISTVIEW_GetSubItemRect(infoPtr, (UINT)wParam, (LPRECT)lParam);
10987 case LVM_GETTEXTBKCOLOR:
10988 return infoPtr->clrTextBk;
10990 case LVM_GETTEXTCOLOR:
10991 return infoPtr->clrText;
10993 /* case LVM_GETTILEINFO: */
10995 /* case LVM_GETTILEVIEWINFO: */
10997 case LVM_GETTOOLTIPS:
10998 if( !infoPtr->hwndToolTip )
10999 infoPtr->hwndToolTip = COMCTL32_CreateToolTip( hwnd );
11000 return (LRESULT)infoPtr->hwndToolTip;
11002 case LVM_GETTOPINDEX:
11003 return LISTVIEW_GetTopIndex(infoPtr);
11005 case LVM_GETUNICODEFORMAT:
11006 return (infoPtr->notifyFormat == NFR_UNICODE);
11009 return infoPtr->uView;
11011 case LVM_GETVIEWRECT:
11012 return LISTVIEW_GetViewRect(infoPtr, (LPRECT)lParam);
11014 case LVM_GETWORKAREAS:
11015 FIXME("LVM_GETWORKAREAS: unimplemented\n");
11018 /* case LVM_HASGROUP: */
11021 return LISTVIEW_HitTest(infoPtr, (LPLVHITTESTINFO)lParam, FALSE, TRUE);
11023 case LVM_INSERTCOLUMNA:
11024 return LISTVIEW_InsertColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam, FALSE);
11026 case LVM_INSERTCOLUMNW:
11027 return LISTVIEW_InsertColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam, TRUE);
11029 /* case LVM_INSERTGROUP: */
11031 /* case LVM_INSERTGROUPSORTED: */
11033 case LVM_INSERTITEMA:
11034 case LVM_INSERTITEMW:
11035 return LISTVIEW_InsertItemT(infoPtr, (LPLVITEMW)lParam, uMsg == LVM_INSERTITEMW);
11037 /* case LVM_INSERTMARKHITTEST: */
11039 /* case LVM_ISGROUPVIEWENABLED: */
11041 case LVM_ISITEMVISIBLE:
11042 return LISTVIEW_IsItemVisible(infoPtr, (INT)wParam);
11044 case LVM_MAPIDTOINDEX:
11045 return LISTVIEW_MapIdToIndex(infoPtr, (UINT)wParam);
11047 case LVM_MAPINDEXTOID:
11048 return LISTVIEW_MapIndexToId(infoPtr, (INT)wParam);
11050 /* case LVM_MOVEGROUP: */
11052 /* case LVM_MOVEITEMTOGROUP: */
11054 case LVM_REDRAWITEMS:
11055 return LISTVIEW_RedrawItems(infoPtr, (INT)wParam, (INT)lParam);
11057 /* case LVM_REMOVEALLGROUPS: */
11059 /* case LVM_REMOVEGROUP: */
11062 return LISTVIEW_Scroll(infoPtr, (INT)wParam, (INT)lParam);
11064 case LVM_SETBKCOLOR:
11065 return LISTVIEW_SetBkColor(infoPtr, (COLORREF)lParam);
11067 /* case LVM_SETBKIMAGE: */
11069 case LVM_SETCALLBACKMASK:
11070 infoPtr->uCallbackMask = (UINT)wParam;
11073 case LVM_SETCOLUMNA:
11074 return LISTVIEW_SetColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam, FALSE);
11076 case LVM_SETCOLUMNW:
11077 return LISTVIEW_SetColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam, TRUE);
11079 case LVM_SETCOLUMNORDERARRAY:
11080 return LISTVIEW_SetColumnOrderArray(infoPtr, (INT)wParam, (LPINT)lParam);
11082 case LVM_SETCOLUMNWIDTH:
11083 return LISTVIEW_SetColumnWidth(infoPtr, (INT)wParam, (short)LOWORD(lParam));
11085 case LVM_SETEXTENDEDLISTVIEWSTYLE:
11086 return LISTVIEW_SetExtendedListViewStyle(infoPtr, (DWORD)wParam, (DWORD)lParam);
11088 /* case LVM_SETGROUPINFO: */
11090 /* case LVM_SETGROUPMETRICS: */
11092 case LVM_SETHOTCURSOR:
11093 return (LRESULT)LISTVIEW_SetHotCursor(infoPtr, (HCURSOR)lParam);
11095 case LVM_SETHOTITEM:
11096 return LISTVIEW_SetHotItem(infoPtr, (INT)wParam);
11098 case LVM_SETHOVERTIME:
11099 return LISTVIEW_SetHoverTime(infoPtr, (DWORD)wParam);
11101 case LVM_SETICONSPACING:
11102 return LISTVIEW_SetIconSpacing(infoPtr, (short)LOWORD(lParam), (short)HIWORD(lParam));
11104 case LVM_SETIMAGELIST:
11105 return (LRESULT)LISTVIEW_SetImageList(infoPtr, (INT)wParam, (HIMAGELIST)lParam);
11107 /* case LVM_SETINFOTIP: */
11109 /* case LVM_SETINSERTMARK: */
11111 /* case LVM_SETINSERTMARKCOLOR: */
11116 if (infoPtr->dwStyle & LVS_OWNERDATA) return FALSE;
11117 return LISTVIEW_SetItemT(infoPtr, (LPLVITEMW)lParam, (uMsg == LVM_SETITEMW));
11120 case LVM_SETITEMCOUNT:
11121 return LISTVIEW_SetItemCount(infoPtr, (INT)wParam, (DWORD)lParam);
11123 case LVM_SETITEMPOSITION:
11126 pt.x = (short)LOWORD(lParam);
11127 pt.y = (short)HIWORD(lParam);
11128 return LISTVIEW_SetItemPosition(infoPtr, (INT)wParam, pt);
11131 case LVM_SETITEMPOSITION32:
11132 if (lParam == 0) return FALSE;
11133 return LISTVIEW_SetItemPosition(infoPtr, (INT)wParam, *((POINT*)lParam));
11135 case LVM_SETITEMSTATE:
11136 if (lParam == 0) return FALSE;
11137 return LISTVIEW_SetItemState(infoPtr, (INT)wParam, (LPLVITEMW)lParam);
11139 case LVM_SETITEMTEXTA:
11140 return LISTVIEW_SetItemTextT(infoPtr, (INT)wParam, (LPLVITEMW)lParam, FALSE);
11142 case LVM_SETITEMTEXTW:
11143 return LISTVIEW_SetItemTextT(infoPtr, (INT)wParam, (LPLVITEMW)lParam, TRUE);
11145 /* case LVM_SETOUTLINECOLOR: */
11147 /* case LVM_SETSELECTEDCOLUMN: */
11149 case LVM_SETSELECTIONMARK:
11150 return LISTVIEW_SetSelectionMark(infoPtr, (INT)lParam);
11152 case LVM_SETTEXTBKCOLOR:
11153 return LISTVIEW_SetTextBkColor(infoPtr, (COLORREF)lParam);
11155 case LVM_SETTEXTCOLOR:
11156 return LISTVIEW_SetTextColor(infoPtr, (COLORREF)lParam);
11158 /* case LVM_SETTILEINFO: */
11160 /* case LVM_SETTILEVIEWINFO: */
11162 /* case LVM_SETTILEWIDTH: */
11164 case LVM_SETTOOLTIPS:
11165 return (LRESULT)LISTVIEW_SetToolTips(infoPtr, (HWND)lParam);
11167 case LVM_SETUNICODEFORMAT:
11168 return LISTVIEW_SetUnicodeFormat(infoPtr, wParam);
11171 return LISTVIEW_SetView(infoPtr, wParam);
11173 /* case LVM_SETWORKAREAS: */
11175 /* case LVM_SORTGROUPS: */
11177 case LVM_SORTITEMS:
11178 return LISTVIEW_SortItems(infoPtr, (PFNLVCOMPARE)lParam, (LPARAM)wParam, FALSE);
11180 case LVM_SORTITEMSEX:
11181 return LISTVIEW_SortItems(infoPtr, (PFNLVCOMPARE)lParam, (LPARAM)wParam, TRUE);
11183 case LVM_SUBITEMHITTEST:
11184 return LISTVIEW_HitTest(infoPtr, (LPLVHITTESTINFO)lParam, TRUE, FALSE);
11187 return LISTVIEW_Update(infoPtr, (INT)wParam);
11189 case CCM_GETVERSION:
11190 return LISTVIEW_GetVersion(infoPtr);
11192 case CCM_SETVERSION:
11193 return LISTVIEW_SetVersion(infoPtr, wParam);
11196 return LISTVIEW_ProcessLetterKeys( infoPtr, wParam, lParam );
11199 return LISTVIEW_Command(infoPtr, wParam, lParam);
11202 return LISTVIEW_NCCreate(hwnd, (LPCREATESTRUCTW)lParam);
11205 return LISTVIEW_Create(hwnd, (LPCREATESTRUCTW)lParam);
11208 return LISTVIEW_Destroy(infoPtr);
11211 return LISTVIEW_Enable(infoPtr, (BOOL)wParam);
11213 case WM_ERASEBKGND:
11214 return LISTVIEW_EraseBkgnd(infoPtr, (HDC)wParam);
11216 case WM_GETDLGCODE:
11217 return DLGC_WANTCHARS | DLGC_WANTARROWS;
11220 return (LRESULT)infoPtr->hFont;
11223 return LISTVIEW_HScroll(infoPtr, (INT)LOWORD(wParam), 0, (HWND)lParam);
11226 return LISTVIEW_KeyDown(infoPtr, (INT)wParam, (LONG)lParam);
11229 return LISTVIEW_KillFocus(infoPtr);
11231 case WM_LBUTTONDBLCLK:
11232 return LISTVIEW_LButtonDblClk(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11234 case WM_LBUTTONDOWN:
11235 return LISTVIEW_LButtonDown(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11238 return LISTVIEW_LButtonUp(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11241 return LISTVIEW_MouseMove (infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11243 case WM_MOUSEHOVER:
11244 return LISTVIEW_MouseHover(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11247 return LISTVIEW_NCDestroy(infoPtr);
11250 if (LISTVIEW_NCPaint(infoPtr, (HRGN)wParam))
11255 if (lParam && ((LPNMHDR)lParam)->hwndFrom == infoPtr->hwndHeader)
11256 return LISTVIEW_HeaderNotification(infoPtr, (LPNMHEADERW)lParam);
11259 case WM_NOTIFYFORMAT:
11260 return LISTVIEW_NotifyFormat(infoPtr, (HWND)wParam, (INT)lParam);
11262 case WM_PRINTCLIENT:
11263 return LISTVIEW_PrintClient(infoPtr, (HDC)wParam, (DWORD)lParam);
11266 return LISTVIEW_WMPaint(infoPtr, (HDC)wParam);
11268 case WM_RBUTTONDBLCLK:
11269 return LISTVIEW_RButtonDblClk(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11271 case WM_RBUTTONDOWN:
11272 return LISTVIEW_RButtonDown(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11275 return LISTVIEW_RButtonUp(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11278 if(LISTVIEW_SetCursor(infoPtr, (HWND)wParam, LOWORD(lParam), HIWORD(lParam)))
11283 return LISTVIEW_SetFocus(infoPtr, (HWND)wParam);
11286 return LISTVIEW_SetFont(infoPtr, (HFONT)wParam, (WORD)lParam);
11289 return LISTVIEW_SetRedraw(infoPtr, (BOOL)wParam);
11291 case WM_SHOWWINDOW:
11292 LISTVIEW_ShowWindow(infoPtr, (BOOL)wParam, (INT)lParam);
11293 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
11296 return LISTVIEW_Size(infoPtr, (short)LOWORD(lParam), (short)HIWORD(lParam));
11298 case WM_STYLECHANGED:
11299 return LISTVIEW_StyleChanged(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
11301 case WM_STYLECHANGING:
11302 return LISTVIEW_StyleChanging(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
11304 case WM_SYSCOLORCHANGE:
11305 COMCTL32_RefreshSysColors();
11308 /* case WM_TIMER: */
11309 case WM_THEMECHANGED:
11310 return LISTVIEW_ThemeChanged(infoPtr);
11313 return LISTVIEW_VScroll(infoPtr, (INT)LOWORD(wParam), 0, (HWND)lParam);
11315 case WM_MOUSEWHEEL:
11316 if (wParam & (MK_SHIFT | MK_CONTROL))
11317 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
11318 return LISTVIEW_MouseWheel(infoPtr, (short int)HIWORD(wParam));
11320 case WM_WINDOWPOSCHANGED:
11321 if (!(((WINDOWPOS *)lParam)->flags & SWP_NOSIZE))
11323 SetWindowPos(infoPtr->hwndSelf, 0, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOACTIVATE |
11324 SWP_NOZORDER | SWP_NOMOVE | SWP_NOSIZE);
11326 if ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (infoPtr->uView == LV_VIEW_DETAILS))
11328 if (notify_measureitem(infoPtr)) LISTVIEW_InvalidateList(infoPtr);
11331 LISTVIEW_UpdateSize(infoPtr);
11332 LISTVIEW_UpdateScroll(infoPtr);
11334 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
11336 /* case WM_WININICHANGE: */
11339 if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
11340 ERR("unknown msg %04x wp=%08lx lp=%08lx\n", uMsg, wParam, lParam);
11343 /* call default window procedure */
11344 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
11351 * Registers the window class.
11359 void LISTVIEW_Register(void)
11361 WNDCLASSW wndClass;
11363 ZeroMemory(&wndClass, sizeof(WNDCLASSW));
11364 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS;
11365 wndClass.lpfnWndProc = LISTVIEW_WindowProc;
11366 wndClass.cbClsExtra = 0;
11367 wndClass.cbWndExtra = sizeof(LISTVIEW_INFO *);
11368 wndClass.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
11369 wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
11370 wndClass.lpszClassName = WC_LISTVIEWW;
11371 RegisterClassW(&wndClass);
11376 * Unregisters the window class.
11384 void LISTVIEW_Unregister(void)
11386 UnregisterClassW(WC_LISTVIEWW, NULL);
11391 * Handle any WM_COMMAND messages
11394 * [I] infoPtr : valid pointer to the listview structure
11395 * [I] wParam : the first message parameter
11396 * [I] lParam : the second message parameter
11401 static LRESULT LISTVIEW_Command(LISTVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
11403 switch (HIWORD(wParam))
11408 * Adjust the edit window size
11410 WCHAR buffer[1024];
11411 HDC hdc = GetDC(infoPtr->hwndEdit);
11412 HFONT hFont, hOldFont = 0;
11416 if (!infoPtr->hwndEdit || !hdc) return 0;
11417 GetWindowTextW(infoPtr->hwndEdit, buffer, sizeof(buffer)/sizeof(buffer[0]));
11418 GetWindowRect(infoPtr->hwndEdit, &rect);
11420 /* Select font to get the right dimension of the string */
11421 hFont = (HFONT)SendMessageW(infoPtr->hwndEdit, WM_GETFONT, 0, 0);
11424 hOldFont = SelectObject(hdc, hFont);
11427 if (GetTextExtentPoint32W(hdc, buffer, lstrlenW(buffer), &sz))
11429 TEXTMETRICW textMetric;
11431 /* Add Extra spacing for the next character */
11432 GetTextMetricsW(hdc, &textMetric);
11433 sz.cx += (textMetric.tmMaxCharWidth * 2);
11441 rect.bottom - rect.top,
11442 SWP_DRAWFRAME|SWP_NOMOVE);
11445 SelectObject(hdc, hOldFont);
11447 ReleaseDC(infoPtr->hwndEdit, hdc);
11453 LISTVIEW_CancelEditLabel(infoPtr);
11458 return SendMessageW (infoPtr->hwndNotify, WM_COMMAND, wParam, lParam);
11467 * Subclassed edit control windproc function
11470 * [I] hwnd : the edit window handle
11471 * [I] uMsg : the message that is to be processed
11472 * [I] wParam : first message parameter
11473 * [I] lParam : second message parameter
11474 * [I] isW : TRUE if input is Unicode
11479 static LRESULT EditLblWndProcT(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL isW)
11481 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongPtrW(GetParent(hwnd), 0);
11484 TRACE("(hwnd=%p, uMsg=%x, wParam=%lx, lParam=%lx, isW=%d)\n",
11485 hwnd, uMsg, wParam, lParam, isW);
11489 case WM_GETDLGCODE:
11490 return DLGC_WANTARROWS | DLGC_WANTALLKEYS;
11494 WNDPROC editProc = infoPtr->EditWndProc;
11495 infoPtr->EditWndProc = 0;
11496 SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (DWORD_PTR)editProc);
11497 return CallWindowProcT(editProc, hwnd, uMsg, wParam, lParam, isW);
11501 if (VK_ESCAPE == (INT)wParam)
11506 else if (VK_RETURN == (INT)wParam)
11510 return CallWindowProcT(infoPtr->EditWndProc, hwnd, uMsg, wParam, lParam, isW);
11513 /* kill the edit */
11514 if (infoPtr->hwndEdit)
11515 LISTVIEW_EndEditLabelT(infoPtr, save, isW);
11517 SendMessageW(hwnd, WM_CLOSE, 0, 0);
11523 * Subclassed edit control Unicode windproc function
11526 * [I] hwnd : the edit window handle
11527 * [I] uMsg : the message that is to be processed
11528 * [I] wParam : first message parameter
11529 * [I] lParam : second message parameter
11533 static LRESULT CALLBACK EditLblWndProcW(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
11535 return EditLblWndProcT(hwnd, uMsg, wParam, lParam, TRUE);
11540 * Subclassed edit control ANSI windproc function
11543 * [I] hwnd : the edit window handle
11544 * [I] uMsg : the message that is to be processed
11545 * [I] wParam : first message parameter
11546 * [I] lParam : second message parameter
11550 static LRESULT CALLBACK EditLblWndProcA(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
11552 return EditLblWndProcT(hwnd, uMsg, wParam, lParam, FALSE);
11557 * Creates a subclassed edit control
11560 * [I] infoPtr : valid pointer to the listview structure
11561 * [I] text : initial text for the edit
11562 * [I] style : the window style
11563 * [I] isW : TRUE if input is Unicode
11567 static HWND CreateEditLabelT(LISTVIEW_INFO *infoPtr, LPCWSTR text, DWORD style, BOOL isW)
11570 HINSTANCE hinst = (HINSTANCE)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_HINSTANCE);
11572 TRACE("(text=%s, ..., isW=%d)\n", debugtext_t(text, isW), isW);
11574 style |= WS_CHILDWINDOW|WS_CLIPSIBLINGS|ES_LEFT|ES_AUTOHSCROLL|WS_BORDER;
11576 /* Window will be resized and positioned after LVN_BEGINLABELEDIT */
11578 hedit = CreateWindowW(WC_EDITW, text, style, 0, 0, 0, 0, infoPtr->hwndSelf, 0, hinst, 0);
11580 hedit = CreateWindowA(WC_EDITA, (LPCSTR)text, style, 0, 0, 0, 0, infoPtr->hwndSelf, 0, hinst, 0);
11582 if (!hedit) return 0;
11584 infoPtr->EditWndProc = (WNDPROC)
11585 (isW ? SetWindowLongPtrW(hedit, GWLP_WNDPROC, (DWORD_PTR)EditLblWndProcW) :
11586 SetWindowLongPtrA(hedit, GWLP_WNDPROC, (DWORD_PTR)EditLblWndProcA) );
11588 SendMessageW(hedit, WM_SETFONT, (WPARAM)infoPtr->hFont, FALSE);