4 * Copyright 1998, 1999 Eric Kohl
5 * Copyright 1999 Luc Tourangeau
6 * Copyright 2000 Jason Mawdsley
7 * Copyright 2001 CodeWeavers Inc.
8 * Copyright 2002 Dimitrie O. Paun
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 * This code was audited for completeness against the documented features
27 * of Comctl32.dll version 6.0 on May. 20, 2005, by James Hawkins.
29 * Unless otherwise noted, we believe this code to be complete, as per
30 * the specification mentioned above.
31 * If you discover missing features, or bugs, please note them below.
35 * Default Message Processing
36 * -- EN_KILLFOCUS should be handled in WM_COMMAND
37 * -- WM_CREATE: create the icon and small icon image lists at this point only if
38 * the LVS_SHAREIMAGELISTS style is not specified.
39 * -- WM_ERASEBKGND: forward this message to the parent window if the bkgnd
41 * -- WM_WINDOWPOSCHANGED: arrange the list items if the current view is icon
42 * or small icon and the LVS_AUTOARRANGE style is specified.
47 * -- Hot item handling, mouse hovering
48 * -- Workareas support
53 * -- Expand large item in ICON mode when the cursor is flying over the icon or text.
54 * -- Support CustomDraw options for _WIN32_IE >= 0x560 (see NMLVCUSTOMDRAW docs).
55 * -- LVA_SNAPTOGRID not implemented
56 * -- LISTVIEW_ApproximateViewRect partially implemented
57 * -- LISTVIEW_[GS]etColumnOrderArray stubs
58 * -- LISTVIEW_SetColumnWidth ignores header images & bitmap
59 * -- LISTVIEW_SetIconSpacing is incomplete
60 * -- LISTVIEW_SortItems is broken
61 * -- LISTVIEW_StyleChanged doesn't handle some changes too well
64 * -- LISTVIEW_GetNextItem needs to be rewritten. It is currently
65 * linear in the number of items in the list, and this is
66 * unacceptable for large lists.
67 * -- in sorted mode, LISTVIEW_InsertItemT sorts the array,
68 * instead of inserting in the right spot
69 * -- we should keep an ordered array of coordinates in iconic mode
70 * this would allow to frame items (iterator_frameditems),
71 * and find nearest item (LVFI_NEARESTXY) a lot more efficiently
79 * -- LVIS_ACTIVATING (not currently supported by comctl32.dll version 6.0)
86 * -- LVS_NOSCROLL (see Q137520)
87 * -- LVS_SORTASCENDING, LVS_SORTDESCENDING
89 * -- LVS_TYPESTYLEMASK
92 * -- LVS_EX_BORDERSELECT
94 * -- LVS_EX_HEADERDRAGDROP
97 * -- LVS_EX_MULTIWORKAREAS
98 * -- LVS_EX_ONECLICKACTIVATE
100 * -- LVS_EX_SIMPLESELECT
101 * -- LVS_EX_TRACKSELECT
102 * -- LVS_EX_TWOCLICKACTIVATE
103 * -- LVS_EX_UNDERLINECOLD
104 * -- LVS_EX_UNDERLINEHOT
107 * -- LVN_BEGINSCROLL, LVN_ENDSCROLL
110 * -- LVN_MARQUEEBEGIN
117 * -- LVM_CANCELEDITLABEL
118 * -- LVM_ENABLEGROUPVIEW
119 * -- LVM_GETBKIMAGE, LVM_SETBKIMAGE
120 * -- LVM_GETGROUPINFO, LVM_SETGROUPINFO
121 * -- LVM_GETGROUPMETRICS, LVM_SETGROUPMETRICS
122 * -- LVM_GETINSERTMARK, LVM_SETINSERTMARK
123 * -- LVM_GETINSERTMARKCOLOR, LVM_SETINSERTMARKCOLOR
124 * -- LVM_GETINSERTMARKRECT
125 * -- LVM_GETNUMBEROFWORKAREAS
126 * -- LVM_GETOUTLINECOLOR, LVM_SETOUTLINECOLOR
127 * -- LVM_GETSELECTEDCOLUMN, LVM_SETSELECTEDCOLUMN
128 * -- LVM_GETISEARCHSTRINGW, LVM_GETISEARCHSTRINGA
129 * -- LVM_GETTILEINFO, LVM_SETTILEINFO
130 * -- LVM_GETTILEVIEWINFO, LVM_SETTILEVIEWINFO
131 * -- LVM_GETUNICODEFORMAT, LVM_SETUNICODEFORMAT
132 * -- LVM_GETVIEW, LVM_SETVIEW
133 * -- LVM_GETWORKAREAS, LVM_SETWORKAREAS
134 * -- LVM_HASGROUP, LVM_INSERTGROUP, LVM_REMOVEGROUP, LVM_REMOVEALLGROUPS
135 * -- LVM_INSERTGROUPSORTED
136 * -- LVM_INSERTMARKHITTEST
137 * -- LVM_ISGROUPVIEWENABLED
138 * -- LVM_MAPIDTOINDEX, LVM_MAPINDEXTOID
140 * -- LVM_MOVEITEMTOGROUP
142 * -- LVM_SETTILEWIDTH
147 * -- ListView_GetCheckSate, ListView_SetCheckState
148 * -- ListView_GetHoverTime, ListView_SetHoverTime
149 * -- ListView_GetISearchString
150 * -- ListView_GetNumberOfWorkAreas
151 * -- ListView_GetOrigin
152 * -- ListView_GetTextBkColor
153 * -- ListView_GetUnicodeFormat, ListView_SetUnicodeFormat
154 * -- ListView_GetWorkAreas, ListView_SetWorkAreas
155 * -- ListView_SortItemsEx
160 * Known differences in message stream from native control (not known if
161 * these differences cause problems):
162 * LVM_INSERTITEM issues LVM_SETITEMSTATE and LVM_SETITEM in certain cases.
163 * LVM_SETITEM does not always issue LVN_ITEMCHANGING/LVN_ITEMCHANGED.
164 * WM_CREATE does not issue WM_QUERYUISTATE and associated registry
165 * processing for "USEDOUBLECLICKTIME".
169 #include "wine/port.h"
184 #include "commctrl.h"
185 #include "comctl32.h"
188 #include "wine/debug.h"
189 #include "wine/unicode.h"
191 WINE_DEFAULT_DEBUG_CHANNEL(listview);
193 /* make sure you set this to 0 for production use! */
194 #define DEBUG_RANGES 1
196 typedef struct tagCOLUMN_INFO
198 RECT rcHeader; /* tracks the header's rectangle */
199 int fmt; /* same as LVCOLUMN.fmt */
202 typedef struct tagITEMHDR
206 } ITEMHDR, *LPITEMHDR;
208 typedef struct tagSUBITEM_INFO
214 typedef struct tagITEM_INFO
222 typedef struct tagRANGE
228 typedef struct tagRANGES
233 typedef struct tagITERATOR
242 typedef struct tagDELAYED_ITEM_EDIT
248 typedef struct tagLISTVIEW_INFO
255 HIMAGELIST himlNormal;
256 HIMAGELIST himlSmall;
257 HIMAGELIST himlState;
260 POINT ptClickPos; /* point where the user clicked */
261 BOOL bNoItemMetrics; /* flags if item metrics are not yet computed */
264 RANGES selectionRanges;
269 RECT rcList; /* This rectangle is really the window
270 * client rectangle possibly reduced by the
271 * horizontal scroll bar and/or header - see
272 * LISTVIEW_UpdateSize. This rectangle offset
273 * by the LISTVIEW_GetOrigin value is in
274 * client coordinates */
283 INT ntmHeight; /* Some cached metrics of the font used */
284 INT ntmMaxCharWidth; /* by the listview to draw items */
286 BOOL bRedraw; /* Turns on/off repaints & invalidations */
287 BOOL bAutoarrange; /* Autoarrange flag when NOT in LVS_AUTOARRANGE */
289 BOOL bDoChangeNotify; /* send change notification messages? */
292 DWORD dwStyle; /* the cached window GWL_STYLE */
293 DWORD dwLvExStyle; /* extended listview style */
294 INT nItemCount; /* the number of items in the list */
295 HDPA hdpaItems; /* array ITEM_INFO pointers */
296 HDPA hdpaPosX; /* maintains the (X, Y) coordinates of the */
297 HDPA hdpaPosY; /* items in LVS_ICON, and LVS_SMALLICON modes */
298 HDPA hdpaColumns; /* array of COLUMN_INFO pointers */
299 POINT currIconPos; /* this is the position next icon will be placed */
300 PFNLVCOMPARE pfnCompare;
308 DWORD cditemmode; /* Keep the custom draw flags for an item/row */
310 DWORD lastKeyPressTimestamp;
312 INT nSearchParamLength;
313 WCHAR szSearchParam[ MAX_PATH ];
315 INT nMeasureItemHeight;
316 INT xTrackLine; /* The x coefficient of the track line or -1 if none */
317 DELAYED_ITEM_EDIT itemEdit; /* Pointer to this structure will be the timer ID */
323 /* How many we debug buffer to allocate */
324 #define DEBUG_BUFFERS 20
325 /* The size of a single debug bbuffer */
326 #define DEBUG_BUFFER_SIZE 256
328 /* Internal interface to LISTVIEW_HScroll and LISTVIEW_VScroll */
329 #define SB_INTERNAL -1
331 /* maximum size of a label */
332 #define DISP_TEXT_SIZE 512
334 /* padding for items in list and small icon display modes */
335 #define WIDTH_PADDING 12
337 /* padding for items in list, report and small icon display modes */
338 #define HEIGHT_PADDING 1
340 /* offset of items in report display mode */
341 #define REPORT_MARGINX 2
343 /* padding for icon in large icon display mode
344 * ICON_TOP_PADDING_NOTHITABLE - space between top of box and area
345 * that HITTEST will see.
346 * ICON_TOP_PADDING_HITABLE - spacing between above and icon.
347 * ICON_TOP_PADDING - sum of the two above.
348 * ICON_BOTTOM_PADDING - between bottom of icon and top of text
349 * LABEL_HOR_PADDING - between text and sides of box
350 * LABEL_VERT_PADDING - between bottom of text and end of box
352 * ICON_LR_PADDING - additional width above icon size.
353 * ICON_LR_HALF - half of the above value
355 #define ICON_TOP_PADDING_NOTHITABLE 2
356 #define ICON_TOP_PADDING_HITABLE 2
357 #define ICON_TOP_PADDING (ICON_TOP_PADDING_NOTHITABLE + ICON_TOP_PADDING_HITABLE)
358 #define ICON_BOTTOM_PADDING 4
359 #define LABEL_HOR_PADDING 5
360 #define LABEL_VERT_PADDING 7
361 #define ICON_LR_PADDING 16
362 #define ICON_LR_HALF (ICON_LR_PADDING/2)
364 /* default label width for items in list and small icon display modes */
365 #define DEFAULT_LABEL_WIDTH 40
367 /* default column width for items in list display mode */
368 #define DEFAULT_COLUMN_WIDTH 128
370 /* Size of "line" scroll for V & H scrolls */
371 #define LISTVIEW_SCROLL_ICON_LINE_SIZE 37
373 /* Padding between image and label */
374 #define IMAGE_PADDING 2
376 /* Padding behind the label */
377 #define TRAILING_LABEL_PADDING 12
378 #define TRAILING_HEADER_PADDING 11
380 /* Border for the icon caption */
381 #define CAPTION_BORDER 2
383 /* Standard DrawText flags */
384 #define LV_ML_DT_FLAGS (DT_TOP | DT_NOPREFIX | DT_EDITCONTROL | DT_CENTER | DT_WORDBREAK | DT_WORD_ELLIPSIS | DT_END_ELLIPSIS)
385 #define LV_FL_DT_FLAGS (DT_TOP | DT_NOPREFIX | DT_EDITCONTROL | DT_CENTER | DT_WORDBREAK | DT_NOCLIP)
386 #define LV_SL_DT_FLAGS (DT_VCENTER | DT_NOPREFIX | DT_EDITCONTROL | DT_SINGLELINE | DT_WORD_ELLIPSIS | DT_END_ELLIPSIS)
388 /* Image index from state */
389 #define STATEIMAGEINDEX(x) (((x) & LVIS_STATEIMAGEMASK) >> 12)
391 /* The time in milliseconds to reset the search in the list */
392 #define KEY_DELAY 450
394 /* Dump the LISTVIEW_INFO structure to the debug channel */
395 #define LISTVIEW_DUMP(iP) do { \
396 TRACE("hwndSelf=%p, clrBk=0x%06x, clrText=0x%06x, clrTextBk=0x%06x, ItemHeight=%d, ItemWidth=%d, Style=0x%08x\n", \
397 iP->hwndSelf, iP->clrBk, iP->clrText, iP->clrTextBk, \
398 iP->nItemHeight, iP->nItemWidth, iP->dwStyle); \
399 TRACE("hwndSelf=%p, himlNor=%p, himlSml=%p, himlState=%p, Focused=%d, Hot=%d, exStyle=0x%08x, Focus=%d\n", \
400 iP->hwndSelf, iP->himlNormal, iP->himlSmall, iP->himlState, \
401 iP->nFocusedItem, iP->nHotItem, iP->dwLvExStyle, iP->bFocus ); \
402 TRACE("hwndSelf=%p, ntmH=%d, icSz.cx=%d, icSz.cy=%d, icSp.cx=%d, icSp.cy=%d, notifyFmt=%d\n", \
403 iP->hwndSelf, iP->ntmHeight, iP->iconSize.cx, iP->iconSize.cy, \
404 iP->iconSpacing.cx, iP->iconSpacing.cy, iP->notifyFormat); \
405 TRACE("hwndSelf=%p, rcList=%s\n", iP->hwndSelf, wine_dbgstr_rect(&iP->rcList)); \
408 static const WCHAR themeClass[] = {'L','i','s','t','V','i','e','w',0};
411 * forward declarations
413 static BOOL LISTVIEW_GetItemT(const LISTVIEW_INFO *, LPLVITEMW, BOOL);
414 static void LISTVIEW_GetItemBox(const LISTVIEW_INFO *, INT, LPRECT);
415 static void LISTVIEW_GetItemOrigin(const LISTVIEW_INFO *, INT, LPPOINT);
416 static BOOL LISTVIEW_GetItemPosition(const LISTVIEW_INFO *, INT, LPPOINT);
417 static BOOL LISTVIEW_GetItemRect(const LISTVIEW_INFO *, INT, LPRECT);
418 static INT LISTVIEW_GetLabelWidth(const LISTVIEW_INFO *, INT);
419 static void LISTVIEW_GetOrigin(const LISTVIEW_INFO *, LPPOINT);
420 static BOOL LISTVIEW_GetViewRect(const LISTVIEW_INFO *, LPRECT);
421 static void LISTVIEW_SetGroupSelection(LISTVIEW_INFO *, INT);
422 static BOOL LISTVIEW_SetItemT(LISTVIEW_INFO *, LVITEMW *, BOOL);
423 static void LISTVIEW_UpdateScroll(const LISTVIEW_INFO *);
424 static void LISTVIEW_SetSelection(LISTVIEW_INFO *, INT);
425 static void LISTVIEW_UpdateSize(LISTVIEW_INFO *);
426 static HWND LISTVIEW_EditLabelT(LISTVIEW_INFO *, INT, BOOL);
427 static LRESULT LISTVIEW_Command(const LISTVIEW_INFO *, WPARAM, LPARAM);
428 static BOOL LISTVIEW_SortItems(LISTVIEW_INFO *, PFNLVCOMPARE, LPARAM);
429 static INT LISTVIEW_GetStringWidthT(const LISTVIEW_INFO *, LPCWSTR, BOOL);
430 static BOOL LISTVIEW_KeySelection(LISTVIEW_INFO *, INT);
431 static UINT LISTVIEW_GetItemState(const LISTVIEW_INFO *, INT, UINT);
432 static BOOL LISTVIEW_SetItemState(LISTVIEW_INFO *, INT, const LVITEMW *);
433 static LRESULT LISTVIEW_VScroll(LISTVIEW_INFO *, INT, INT, HWND);
434 static LRESULT LISTVIEW_HScroll(LISTVIEW_INFO *, INT, INT, HWND);
435 static INT LISTVIEW_GetTopIndex(const LISTVIEW_INFO *);
436 static BOOL LISTVIEW_EnsureVisible(LISTVIEW_INFO *, INT, BOOL);
437 static HWND CreateEditLabelT(LISTVIEW_INFO *, LPCWSTR, DWORD, INT, INT, INT, INT, BOOL);
438 static HIMAGELIST LISTVIEW_SetImageList(LISTVIEW_INFO *, INT, HIMAGELIST);
439 static INT LISTVIEW_HitTest(const LISTVIEW_INFO *, LPLVHITTESTINFO, BOOL, BOOL);
441 /******** Text handling functions *************************************/
443 /* A text pointer is either NULL, LPSTR_TEXTCALLBACK, or points to a
444 * text string. The string may be ANSI or Unicode, in which case
445 * the boolean isW tells us the type of the string.
447 * The name of the function tell what type of strings it expects:
448 * W: Unicode, T: ANSI/Unicode - function of isW
451 static inline BOOL is_textW(LPCWSTR text)
453 return text != NULL && text != LPSTR_TEXTCALLBACKW;
456 static inline BOOL is_textT(LPCWSTR text, BOOL isW)
458 /* we can ignore isW since LPSTR_TEXTCALLBACKW == LPSTR_TEXTCALLBACKA */
459 return is_textW(text);
462 static inline int textlenT(LPCWSTR text, BOOL isW)
464 return !is_textT(text, isW) ? 0 :
465 isW ? lstrlenW(text) : lstrlenA((LPCSTR)text);
468 static inline void textcpynT(LPWSTR dest, BOOL isDestW, LPCWSTR src, BOOL isSrcW, INT max)
471 if (isSrcW) lstrcpynW(dest, src, max);
472 else MultiByteToWideChar(CP_ACP, 0, (LPCSTR)src, -1, dest, max);
474 if (isSrcW) WideCharToMultiByte(CP_ACP, 0, src, -1, (LPSTR)dest, max, NULL, NULL);
475 else lstrcpynA((LPSTR)dest, (LPCSTR)src, max);
478 static inline LPWSTR textdupTtoW(LPCWSTR text, BOOL isW)
480 LPWSTR wstr = (LPWSTR)text;
482 if (!isW && is_textT(text, isW))
484 INT len = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)text, -1, NULL, 0);
485 wstr = Alloc(len * sizeof(WCHAR));
486 if (wstr) MultiByteToWideChar(CP_ACP, 0, (LPCSTR)text, -1, wstr, len);
488 TRACE(" wstr=%s\n", text == LPSTR_TEXTCALLBACKW ? "(callback)" : debugstr_w(wstr));
492 static inline void textfreeT(LPWSTR wstr, BOOL isW)
494 if (!isW && is_textT(wstr, isW)) Free (wstr);
498 * dest is a pointer to a Unicode string
499 * src is a pointer to a string (Unicode if isW, ANSI if !isW)
501 static BOOL textsetptrT(LPWSTR *dest, LPCWSTR src, BOOL isW)
505 if (src == LPSTR_TEXTCALLBACKW)
507 if (is_textW(*dest)) Free(*dest);
508 *dest = LPSTR_TEXTCALLBACKW;
512 LPWSTR pszText = textdupTtoW(src, isW);
513 if (*dest == LPSTR_TEXTCALLBACKW) *dest = NULL;
514 bResult = Str_SetPtrW(dest, pszText);
515 textfreeT(pszText, isW);
521 * compares a Unicode to a Unicode/ANSI text string
523 static inline int textcmpWT(LPCWSTR aw, LPCWSTR bt, BOOL isW)
525 if (!aw) return bt ? -1 : 0;
526 if (!bt) return aw ? 1 : 0;
527 if (aw == LPSTR_TEXTCALLBACKW)
528 return bt == LPSTR_TEXTCALLBACKW ? 0 : -1;
529 if (bt != LPSTR_TEXTCALLBACKW)
531 LPWSTR bw = textdupTtoW(bt, isW);
532 int r = bw ? lstrcmpW(aw, bw) : 1;
540 static inline int lstrncmpiW(LPCWSTR s1, LPCWSTR s2, int n)
544 n = min(min(n, strlenW(s1)), strlenW(s2));
545 res = CompareStringW(LOCALE_USER_DEFAULT, NORM_IGNORECASE, s1, n, s2, n);
546 return res ? res - sizeof(WCHAR) : res;
549 /******** Debugging functions *****************************************/
551 static inline LPCSTR debugtext_t(LPCWSTR text, BOOL isW)
553 if (text == LPSTR_TEXTCALLBACKW) return "(callback)";
554 return isW ? debugstr_w(text) : debugstr_a((LPCSTR)text);
557 static inline LPCSTR debugtext_tn(LPCWSTR text, BOOL isW, INT n)
559 if (text == LPSTR_TEXTCALLBACKW) return "(callback)";
560 n = min(textlenT(text, isW), n);
561 return isW ? debugstr_wn(text, n) : debugstr_an((LPCSTR)text, n);
564 static char* debug_getbuf(void)
566 static int index = 0;
567 static char buffers[DEBUG_BUFFERS][DEBUG_BUFFER_SIZE];
568 return buffers[index++ % DEBUG_BUFFERS];
571 static inline const char* debugrange(const RANGE *lprng)
573 if (!lprng) return "(null)";
574 return wine_dbg_sprintf("[%d, %d]", lprng->lower, lprng->upper);
577 static const char* debugscrollinfo(const SCROLLINFO *pScrollInfo)
579 char* buf = debug_getbuf(), *text = buf;
580 int len, size = DEBUG_BUFFER_SIZE;
582 if (pScrollInfo == NULL) return "(null)";
583 len = snprintf(buf, size, "{cbSize=%d, ", pScrollInfo->cbSize);
584 if (len == -1) goto end; buf += len; size -= len;
585 if (pScrollInfo->fMask & SIF_RANGE)
586 len = snprintf(buf, size, "nMin=%d, nMax=%d, ", pScrollInfo->nMin, pScrollInfo->nMax);
588 if (len == -1) goto end; buf += len; size -= len;
589 if (pScrollInfo->fMask & SIF_PAGE)
590 len = snprintf(buf, size, "nPage=%u, ", pScrollInfo->nPage);
592 if (len == -1) goto end; buf += len; size -= len;
593 if (pScrollInfo->fMask & SIF_POS)
594 len = snprintf(buf, size, "nPos=%d, ", pScrollInfo->nPos);
596 if (len == -1) goto end; buf += len; size -= len;
597 if (pScrollInfo->fMask & SIF_TRACKPOS)
598 len = snprintf(buf, size, "nTrackPos=%d, ", pScrollInfo->nTrackPos);
600 if (len == -1) goto end; buf += len; size -= len;
603 buf = text + strlen(text);
605 if (buf - text > 2) { buf[-2] = '}'; buf[-1] = 0; }
609 static const char* debugnmlistview(const NMLISTVIEW *plvnm)
611 if (!plvnm) return "(null)";
612 return wine_dbg_sprintf("iItem=%d, iSubItem=%d, uNewState=0x%x,"
613 " uOldState=0x%x, uChanged=0x%x, ptAction=%s, lParam=%ld",
614 plvnm->iItem, plvnm->iSubItem, plvnm->uNewState, plvnm->uOldState,
615 plvnm->uChanged, wine_dbgstr_point(&plvnm->ptAction), plvnm->lParam);
618 static const char* debuglvitem_t(const LVITEMW *lpLVItem, BOOL isW)
620 char* buf = debug_getbuf(), *text = buf;
621 int len, size = DEBUG_BUFFER_SIZE;
623 if (lpLVItem == NULL) return "(null)";
624 len = snprintf(buf, size, "{iItem=%d, iSubItem=%d, ", lpLVItem->iItem, lpLVItem->iSubItem);
625 if (len == -1) goto end; buf += len; size -= len;
626 if (lpLVItem->mask & LVIF_STATE)
627 len = snprintf(buf, size, "state=%x, stateMask=%x, ", lpLVItem->state, lpLVItem->stateMask);
629 if (len == -1) goto end; buf += len; size -= len;
630 if (lpLVItem->mask & LVIF_TEXT)
631 len = snprintf(buf, size, "pszText=%s, cchTextMax=%d, ", debugtext_tn(lpLVItem->pszText, isW, 80), lpLVItem->cchTextMax);
633 if (len == -1) goto end; buf += len; size -= len;
634 if (lpLVItem->mask & LVIF_IMAGE)
635 len = snprintf(buf, size, "iImage=%d, ", lpLVItem->iImage);
637 if (len == -1) goto end; buf += len; size -= len;
638 if (lpLVItem->mask & LVIF_PARAM)
639 len = snprintf(buf, size, "lParam=%lx, ", lpLVItem->lParam);
641 if (len == -1) goto end; buf += len; size -= len;
642 if (lpLVItem->mask & LVIF_INDENT)
643 len = snprintf(buf, size, "iIndent=%d, ", lpLVItem->iIndent);
645 if (len == -1) goto end; buf += len; size -= len;
648 buf = text + strlen(text);
650 if (buf - text > 2) { buf[-2] = '}'; buf[-1] = 0; }
654 static const char* debuglvcolumn_t(const LVCOLUMNW *lpColumn, BOOL isW)
656 char* buf = debug_getbuf(), *text = buf;
657 int len, size = DEBUG_BUFFER_SIZE;
659 if (lpColumn == NULL) return "(null)";
660 len = snprintf(buf, size, "{");
661 if (len == -1) goto end; buf += len; size -= len;
662 if (lpColumn->mask & LVCF_SUBITEM)
663 len = snprintf(buf, size, "iSubItem=%d, ", lpColumn->iSubItem);
665 if (len == -1) goto end; buf += len; size -= len;
666 if (lpColumn->mask & LVCF_FMT)
667 len = snprintf(buf, size, "fmt=%x, ", lpColumn->fmt);
669 if (len == -1) goto end; buf += len; size -= len;
670 if (lpColumn->mask & LVCF_WIDTH)
671 len = snprintf(buf, size, "cx=%d, ", lpColumn->cx);
673 if (len == -1) goto end; buf += len; size -= len;
674 if (lpColumn->mask & LVCF_TEXT)
675 len = snprintf(buf, size, "pszText=%s, cchTextMax=%d, ", debugtext_tn(lpColumn->pszText, isW, 80), lpColumn->cchTextMax);
677 if (len == -1) goto end; buf += len; size -= len;
678 if (lpColumn->mask & LVCF_IMAGE)
679 len = snprintf(buf, size, "iImage=%d, ", lpColumn->iImage);
681 if (len == -1) goto end; buf += len; size -= len;
682 if (lpColumn->mask & LVCF_ORDER)
683 len = snprintf(buf, size, "iOrder=%d, ", lpColumn->iOrder);
685 if (len == -1) goto end; buf += len; size -= len;
688 buf = text + strlen(text);
690 if (buf - text > 2) { buf[-2] = '}'; buf[-1] = 0; }
694 static const char* debuglvhittestinfo(const LVHITTESTINFO *lpht)
696 if (!lpht) return "(null)";
698 return wine_dbg_sprintf("{pt=%s, flags=0x%x, iItem=%d, iSubItem=%d}",
699 wine_dbgstr_point(&lpht->pt), lpht->flags, lpht->iItem, lpht->iSubItem);
702 /* Return the corresponding text for a given scroll value */
703 static inline LPCSTR debugscrollcode(int nScrollCode)
707 case SB_LINELEFT: return "SB_LINELEFT";
708 case SB_LINERIGHT: return "SB_LINERIGHT";
709 case SB_PAGELEFT: return "SB_PAGELEFT";
710 case SB_PAGERIGHT: return "SB_PAGERIGHT";
711 case SB_THUMBPOSITION: return "SB_THUMBPOSITION";
712 case SB_THUMBTRACK: return "SB_THUMBTRACK";
713 case SB_ENDSCROLL: return "SB_ENDSCROLL";
714 case SB_INTERNAL: return "SB_INTERNAL";
715 default: return "unknown";
720 /******** Notification functions i************************************/
722 static LRESULT notify_forward_header(const LISTVIEW_INFO *infoPtr, const NMHEADERW *lpnmh)
724 return SendMessageW(infoPtr->hwndNotify, WM_NOTIFY,
725 (WPARAM)lpnmh->hdr.idFrom, (LPARAM)lpnmh);
728 static LRESULT notify_hdr(const LISTVIEW_INFO *infoPtr, INT code, LPNMHDR pnmh)
732 TRACE("(code=%d)\n", code);
734 pnmh->hwndFrom = infoPtr->hwndSelf;
735 pnmh->idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
737 result = SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, pnmh->idFrom, (LPARAM)pnmh);
739 TRACE(" <= %ld\n", result);
744 static inline BOOL notify(const LISTVIEW_INFO *infoPtr, INT code)
747 HWND hwnd = infoPtr->hwndSelf;
748 notify_hdr(infoPtr, code, &nmh);
749 return IsWindow(hwnd);
752 static inline void notify_itemactivate(const LISTVIEW_INFO *infoPtr, const LVHITTESTINFO *htInfo)
763 item.mask = LVIF_PARAM|LVIF_STATE;
764 item.iItem = htInfo->iItem;
766 if (LISTVIEW_GetItemT(infoPtr, &item, TRUE)) {
767 nmia.lParam = item.lParam;
768 nmia.uOldState = item.state;
769 nmia.uNewState = item.state | LVIS_ACTIVATING;
770 nmia.uChanged = LVIF_STATE;
773 nmia.iItem = htInfo->iItem;
774 nmia.iSubItem = htInfo->iSubItem;
775 nmia.ptAction = htInfo->pt;
777 if (GetKeyState(VK_SHIFT) & 0x8000) nmia.uKeyFlags |= LVKF_SHIFT;
778 if (GetKeyState(VK_CONTROL) & 0x8000) nmia.uKeyFlags |= LVKF_CONTROL;
779 if (GetKeyState(VK_MENU) & 0x8000) nmia.uKeyFlags |= LVKF_ALT;
781 notify_hdr(infoPtr, LVN_ITEMACTIVATE, (LPNMHDR)&nmia);
784 static inline LRESULT notify_listview(const LISTVIEW_INFO *infoPtr, INT code, LPNMLISTVIEW plvnm)
786 TRACE("(code=%d, plvnm=%s)\n", code, debugnmlistview(plvnm));
787 return notify_hdr(infoPtr, code, (LPNMHDR)plvnm);
790 static BOOL notify_click(const LISTVIEW_INFO *infoPtr, INT code, const LVHITTESTINFO *lvht)
794 HWND hwnd = infoPtr->hwndSelf;
796 TRACE("code=%d, lvht=%s\n", code, debuglvhittestinfo(lvht));
797 ZeroMemory(&nmlv, sizeof(nmlv));
798 nmlv.iItem = lvht->iItem;
799 nmlv.iSubItem = lvht->iSubItem;
800 nmlv.ptAction = lvht->pt;
801 item.mask = LVIF_PARAM;
802 item.iItem = lvht->iItem;
804 if (LISTVIEW_GetItemT(infoPtr, &item, TRUE)) nmlv.lParam = item.lParam;
805 notify_listview(infoPtr, code, &nmlv);
806 return IsWindow(hwnd);
809 static BOOL notify_deleteitem(const LISTVIEW_INFO *infoPtr, INT nItem)
813 HWND hwnd = infoPtr->hwndSelf;
815 ZeroMemory(&nmlv, sizeof (NMLISTVIEW));
817 item.mask = LVIF_PARAM;
820 if (LISTVIEW_GetItemT(infoPtr, &item, TRUE)) nmlv.lParam = item.lParam;
821 notify_listview(infoPtr, LVN_DELETEITEM, &nmlv);
822 return IsWindow(hwnd);
825 static int get_ansi_notification(INT unicodeNotificationCode)
827 switch (unicodeNotificationCode)
829 case LVN_BEGINLABELEDITW: return LVN_BEGINLABELEDITA;
830 case LVN_ENDLABELEDITW: return LVN_ENDLABELEDITA;
831 case LVN_GETDISPINFOW: return LVN_GETDISPINFOA;
832 case LVN_SETDISPINFOW: return LVN_SETDISPINFOA;
833 case LVN_ODFINDITEMW: return LVN_ODFINDITEMA;
834 case LVN_GETINFOTIPW: return LVN_GETINFOTIPA;
836 ERR("unknown notification %x\n", unicodeNotificationCode);
842 Send notification. depends on dispinfoW having same
843 structure as dispinfoA.
844 infoPtr : listview struct
845 notificationCode : *Unicode* notification code
846 pdi : dispinfo structure (can be unicode or ansi)
847 isW : TRUE if dispinfo is Unicode
849 static BOOL notify_dispinfoT(const LISTVIEW_INFO *infoPtr, INT notificationCode, LPNMLVDISPINFOW pdi, BOOL isW)
851 BOOL bResult = FALSE;
852 BOOL convertToAnsi = FALSE, convertToUnicode = FALSE;
853 INT cchTempBufMax = 0, savCchTextMax = 0, realNotifCode;
854 LPWSTR pszTempBuf = NULL, savPszText = NULL;
856 if ((pdi->item.mask & LVIF_TEXT) && is_textT(pdi->item.pszText, isW))
858 convertToAnsi = (isW && infoPtr->notifyFormat == NFR_ANSI);
859 convertToUnicode = (!isW && infoPtr->notifyFormat == NFR_UNICODE);
862 if (convertToAnsi || convertToUnicode)
864 if (notificationCode != LVN_GETDISPINFOW)
866 cchTempBufMax = convertToUnicode ?
867 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pdi->item.pszText, -1, NULL, 0):
868 WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, NULL, 0, NULL, NULL);
872 cchTempBufMax = pdi->item.cchTextMax;
873 *pdi->item.pszText = 0; /* make sure we don't process garbage */
876 pszTempBuf = Alloc( (convertToUnicode ? sizeof(WCHAR) : sizeof(CHAR)) * cchTempBufMax);
877 if (!pszTempBuf) return FALSE;
879 if (convertToUnicode)
880 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pdi->item.pszText, -1,
881 pszTempBuf, cchTempBufMax);
883 WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, (LPSTR) pszTempBuf,
884 cchTempBufMax, NULL, NULL);
886 savCchTextMax = pdi->item.cchTextMax;
887 savPszText = pdi->item.pszText;
888 pdi->item.pszText = pszTempBuf;
889 pdi->item.cchTextMax = cchTempBufMax;
892 if (infoPtr->notifyFormat == NFR_ANSI)
893 realNotifCode = get_ansi_notification(notificationCode);
895 realNotifCode = notificationCode;
896 TRACE(" pdi->item=%s\n", debuglvitem_t(&pdi->item, infoPtr->notifyFormat != NFR_ANSI));
897 bResult = notify_hdr(infoPtr, realNotifCode, &pdi->hdr);
899 if (convertToUnicode || convertToAnsi)
901 if (convertToUnicode) /* note : pointer can be changed by app ! */
902 WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, (LPSTR) savPszText,
903 savCchTextMax, NULL, NULL);
905 MultiByteToWideChar(CP_ACP, 0, (LPSTR) pdi->item.pszText, -1,
906 savPszText, savCchTextMax);
907 pdi->item.pszText = savPszText; /* restores our buffer */
908 pdi->item.cchTextMax = savCchTextMax;
914 static void customdraw_fill(NMLVCUSTOMDRAW *lpnmlvcd, const LISTVIEW_INFO *infoPtr, HDC hdc,
915 const RECT *rcBounds, const LVITEMW *lplvItem)
917 ZeroMemory(lpnmlvcd, sizeof(NMLVCUSTOMDRAW));
918 lpnmlvcd->nmcd.hdc = hdc;
919 lpnmlvcd->nmcd.rc = *rcBounds;
920 lpnmlvcd->clrTextBk = infoPtr->clrTextBk;
921 lpnmlvcd->clrText = infoPtr->clrText;
922 if (!lplvItem) return;
923 lpnmlvcd->nmcd.dwItemSpec = lplvItem->iItem + 1;
924 lpnmlvcd->iSubItem = lplvItem->iSubItem;
925 if (lplvItem->state & LVIS_SELECTED) lpnmlvcd->nmcd.uItemState |= CDIS_SELECTED;
926 if (lplvItem->state & LVIS_FOCUSED) lpnmlvcd->nmcd.uItemState |= CDIS_FOCUS;
927 if (lplvItem->iItem == infoPtr->nHotItem) lpnmlvcd->nmcd.uItemState |= CDIS_HOT;
928 lpnmlvcd->nmcd.lItemlParam = lplvItem->lParam;
931 static inline DWORD notify_customdraw (const LISTVIEW_INFO *infoPtr, DWORD dwDrawStage, NMLVCUSTOMDRAW *lpnmlvcd)
933 BOOL isForItem = (lpnmlvcd->nmcd.dwItemSpec != 0);
936 lpnmlvcd->nmcd.dwDrawStage = dwDrawStage;
937 if (isForItem) lpnmlvcd->nmcd.dwDrawStage |= CDDS_ITEM;
938 if (lpnmlvcd->iSubItem) lpnmlvcd->nmcd.dwDrawStage |= CDDS_SUBITEM;
939 if (isForItem) lpnmlvcd->nmcd.dwItemSpec--;
940 result = notify_hdr(infoPtr, NM_CUSTOMDRAW, &lpnmlvcd->nmcd.hdr);
941 if (isForItem) lpnmlvcd->nmcd.dwItemSpec++;
945 static void prepaint_setup (const LISTVIEW_INFO *infoPtr, HDC hdc, NMLVCUSTOMDRAW *lpnmlvcd, BOOL SubItem)
947 if (lpnmlvcd->clrTextBk == CLR_DEFAULT)
948 lpnmlvcd->clrTextBk = comctl32_color.clrWindow;
949 if (lpnmlvcd->clrText == CLR_DEFAULT)
950 lpnmlvcd->clrText = comctl32_color.clrWindowText;
952 /* apparently, for selected items, we have to override the returned values */
955 if (lpnmlvcd->nmcd.uItemState & CDIS_SELECTED)
959 lpnmlvcd->clrTextBk = comctl32_color.clrHighlight;
960 lpnmlvcd->clrText = comctl32_color.clrHighlightText;
962 else if (infoPtr->dwStyle & LVS_SHOWSELALWAYS)
964 lpnmlvcd->clrTextBk = comctl32_color.clr3dFace;
965 lpnmlvcd->clrText = comctl32_color.clrBtnText;
970 /* Set the text attributes */
971 if (lpnmlvcd->clrTextBk != CLR_NONE)
973 SetBkMode(hdc, OPAQUE);
974 SetBkColor(hdc,lpnmlvcd->clrTextBk);
977 SetBkMode(hdc, TRANSPARENT);
978 SetTextColor(hdc, lpnmlvcd->clrText);
981 static inline DWORD notify_postpaint (const LISTVIEW_INFO *infoPtr, NMLVCUSTOMDRAW *lpnmlvcd)
983 return notify_customdraw(infoPtr, CDDS_POSTPAINT, lpnmlvcd);
986 /******** Item iterator functions **********************************/
988 static RANGES ranges_create(int count);
989 static void ranges_destroy(RANGES ranges);
990 static BOOL ranges_add(RANGES ranges, RANGE range);
991 static BOOL ranges_del(RANGES ranges, RANGE range);
992 static void ranges_dump(RANGES ranges);
994 static inline BOOL ranges_additem(RANGES ranges, INT nItem)
996 RANGE range = { nItem, nItem + 1 };
998 return ranges_add(ranges, range);
1001 static inline BOOL ranges_delitem(RANGES ranges, INT nItem)
1003 RANGE range = { nItem, nItem + 1 };
1005 return ranges_del(ranges, range);
1009 * ITERATOR DOCUMENTATION
1011 * The iterator functions allow for easy, and convenient iteration
1012 * over items of interest in the list. Typically, you create a
1013 * iterator, use it, and destroy it, as such:
1016 * iterator_xxxitems(&i, ...);
1017 * while (iterator_{prev,next}(&i)
1019 * //code which uses i.nItem
1021 * iterator_destroy(&i);
1023 * where xxx is either: framed, or visible.
1024 * Note that it is important that the code destroys the iterator
1025 * after it's done with it, as the creation of the iterator may
1026 * allocate memory, which thus needs to be freed.
1028 * You can iterate both forwards, and backwards through the list,
1029 * by using iterator_next or iterator_prev respectively.
1031 * Lower numbered items are draw on top of higher number items in
1032 * LVS_ICON, and LVS_SMALLICON (which are the only modes where
1033 * items may overlap). So, to test items, you should use
1035 * which lists the items top to bottom (in Z-order).
1036 * For drawing items, you should use
1038 * which lists the items bottom to top (in Z-order).
1039 * If you keep iterating over the items after the end-of-items
1040 * marker (-1) is returned, the iterator will start from the
1041 * beginning. Typically, you don't need to test for -1,
1042 * because iterator_{next,prev} will return TRUE if more items
1043 * are to be iterated over, or FALSE otherwise.
1045 * Note: the iterator is defined to be bidirectional. That is,
1046 * any number of prev followed by any number of next, or
1047 * five versa, should leave the iterator at the same item:
1048 * prev * n, next * n = next * n, prev * n
1050 * The iterator has a notion of an out-of-order, special item,
1051 * which sits at the start of the list. This is used in
1052 * LVS_ICON, and LVS_SMALLICON mode to handle the focused item,
1053 * which needs to be first, as it may overlap other items.
1055 * The code is a bit messy because we have:
1056 * - a special item to deal with
1057 * - simple range, or composite range
1059 * If you find bugs, or want to add features, please make sure you
1060 * always check/modify *both* iterator_prev, and iterator_next.
1064 * This function iterates through the items in increasing order,
1065 * but prefixed by the special item, then -1. That is:
1066 * special, 1, 2, 3, ..., n, -1.
1067 * Each item is listed only once.
1069 static inline BOOL iterator_next(ITERATOR* i)
1073 i->nItem = i->nSpecial;
1074 if (i->nItem != -1) return TRUE;
1076 if (i->nItem == i->nSpecial)
1078 if (i->ranges) i->index = 0;
1084 if (i->nItem == i->nSpecial) i->nItem++;
1085 if (i->nItem < i->range.upper) return TRUE;
1090 if (i->index < DPA_GetPtrCount(i->ranges->hdpa))
1091 i->range = *(RANGE*)DPA_GetPtr(i->ranges->hdpa, i->index++);
1094 else if (i->nItem >= i->range.upper) goto end;
1096 i->nItem = i->range.lower;
1097 if (i->nItem >= 0) goto testitem;
1104 * This function iterates through the items in decreasing order,
1105 * followed by the special item, then -1. That is:
1106 * n, n-1, ..., 3, 2, 1, special, -1.
1107 * Each item is listed only once.
1109 static inline BOOL iterator_prev(ITERATOR* i)
1116 if (i->ranges) i->index = DPA_GetPtrCount(i->ranges->hdpa);
1119 if (i->nItem == i->nSpecial)
1127 if (i->nItem == i->nSpecial) i->nItem--;
1128 if (i->nItem >= i->range.lower) return TRUE;
1134 i->range = *(RANGE*)DPA_GetPtr(i->ranges->hdpa, --i->index);
1137 else if (!start && i->nItem < i->range.lower) goto end;
1139 i->nItem = i->range.upper;
1140 if (i->nItem > 0) goto testitem;
1142 return (i->nItem = i->nSpecial) != -1;
1145 static RANGE iterator_range(const ITERATOR *i)
1149 if (!i->ranges) return i->range;
1151 if (DPA_GetPtrCount(i->ranges->hdpa) > 0)
1153 range.lower = (*(RANGE*)DPA_GetPtr(i->ranges->hdpa, 0)).lower;
1154 range.upper = (*(RANGE*)DPA_GetPtr(i->ranges->hdpa, DPA_GetPtrCount(i->ranges->hdpa) - 1)).upper;
1156 else range.lower = range.upper = 0;
1162 * Releases resources associated with this ierator.
1164 static inline void iterator_destroy(const ITERATOR *i)
1166 ranges_destroy(i->ranges);
1170 * Create an empty iterator.
1172 static inline BOOL iterator_empty(ITERATOR* i)
1174 ZeroMemory(i, sizeof(*i));
1175 i->nItem = i->nSpecial = i->range.lower = i->range.upper = -1;
1180 * Create an iterator over a range.
1182 static inline BOOL iterator_rangeitems(ITERATOR* i, RANGE range)
1190 * Create an iterator over a bunch of ranges.
1191 * Please note that the iterator will take ownership of the ranges,
1192 * and will free them upon destruction.
1194 static inline BOOL iterator_rangesitems(ITERATOR* i, RANGES ranges)
1202 * Creates an iterator over the items which intersect lprc.
1204 static BOOL iterator_frameditems(ITERATOR* i, const LISTVIEW_INFO* infoPtr, const RECT *lprc)
1206 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
1207 RECT frame = *lprc, rcItem, rcTemp;
1210 /* in case we fail, we want to return an empty iterator */
1211 if (!iterator_empty(i)) return FALSE;
1213 LISTVIEW_GetOrigin(infoPtr, &Origin);
1215 TRACE("(lprc=%s)\n", wine_dbgstr_rect(lprc));
1216 OffsetRect(&frame, -Origin.x, -Origin.y);
1218 if (uView == LVS_ICON || uView == LVS_SMALLICON)
1222 if (uView == LVS_ICON && infoPtr->nFocusedItem != -1)
1224 LISTVIEW_GetItemBox(infoPtr, infoPtr->nFocusedItem, &rcItem);
1225 if (IntersectRect(&rcTemp, &rcItem, lprc))
1226 i->nSpecial = infoPtr->nFocusedItem;
1228 if (!(iterator_rangesitems(i, ranges_create(50)))) return FALSE;
1229 /* to do better here, we need to have PosX, and PosY sorted */
1230 TRACE("building icon ranges:\n");
1231 for (nItem = 0; nItem < infoPtr->nItemCount; nItem++)
1233 rcItem.left = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosX, nItem);
1234 rcItem.top = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosY, nItem);
1235 rcItem.right = rcItem.left + infoPtr->nItemWidth;
1236 rcItem.bottom = rcItem.top + infoPtr->nItemHeight;
1237 if (IntersectRect(&rcTemp, &rcItem, &frame))
1238 ranges_additem(i->ranges, nItem);
1242 else if (uView == LVS_REPORT)
1246 if (frame.left >= infoPtr->nItemWidth) return TRUE;
1247 if (frame.top >= infoPtr->nItemHeight * infoPtr->nItemCount) return TRUE;
1249 range.lower = max(frame.top / infoPtr->nItemHeight, 0);
1250 range.upper = min((frame.bottom - 1) / infoPtr->nItemHeight, infoPtr->nItemCount - 1) + 1;
1251 if (range.upper <= range.lower) return TRUE;
1252 if (!iterator_rangeitems(i, range)) return FALSE;
1253 TRACE(" report=%s\n", debugrange(&i->range));
1257 INT nPerCol = max((infoPtr->rcList.bottom - infoPtr->rcList.top) / infoPtr->nItemHeight, 1);
1258 INT nFirstRow = max(frame.top / infoPtr->nItemHeight, 0);
1259 INT nLastRow = min((frame.bottom - 1) / infoPtr->nItemHeight, nPerCol - 1);
1260 INT nFirstCol = max(frame.left / infoPtr->nItemWidth, 0);
1261 INT nLastCol = min((frame.right - 1) / infoPtr->nItemWidth, (infoPtr->nItemCount + nPerCol - 1) / nPerCol);
1262 INT lower = nFirstCol * nPerCol + nFirstRow;
1266 TRACE("nPerCol=%d, nFirstRow=%d, nLastRow=%d, nFirstCol=%d, nLastCol=%d, lower=%d\n",
1267 nPerCol, nFirstRow, nLastRow, nFirstCol, nLastCol, lower);
1269 if (nLastCol < nFirstCol || nLastRow < nFirstRow) return TRUE;
1271 if (!(iterator_rangesitems(i, ranges_create(nLastCol - nFirstCol + 1)))) return FALSE;
1272 TRACE("building list ranges:\n");
1273 for (nCol = nFirstCol; nCol <= nLastCol; nCol++)
1275 item_range.lower = nCol * nPerCol + nFirstRow;
1276 if(item_range.lower >= infoPtr->nItemCount) break;
1277 item_range.upper = min(nCol * nPerCol + nLastRow + 1, infoPtr->nItemCount);
1278 TRACE(" list=%s\n", debugrange(&item_range));
1279 ranges_add(i->ranges, item_range);
1287 * Creates an iterator over the items which intersect the visible region of hdc.
1289 static BOOL iterator_visibleitems(ITERATOR *i, const LISTVIEW_INFO *infoPtr, HDC hdc)
1291 POINT Origin, Position;
1292 RECT rcItem, rcClip;
1295 rgntype = GetClipBox(hdc, &rcClip);
1296 if (rgntype == NULLREGION) return iterator_empty(i);
1297 if (!iterator_frameditems(i, infoPtr, &rcClip)) return FALSE;
1298 if (rgntype == SIMPLEREGION) return TRUE;
1300 /* first deal with the special item */
1301 if (i->nSpecial != -1)
1303 LISTVIEW_GetItemBox(infoPtr, i->nSpecial, &rcItem);
1304 if (!RectVisible(hdc, &rcItem)) i->nSpecial = -1;
1307 /* if we can't deal with the region, we'll just go with the simple range */
1308 LISTVIEW_GetOrigin(infoPtr, &Origin);
1309 TRACE("building visible range:\n");
1310 if (!i->ranges && i->range.lower < i->range.upper)
1312 if (!(i->ranges = ranges_create(50))) return TRUE;
1313 if (!ranges_add(i->ranges, i->range))
1315 ranges_destroy(i->ranges);
1321 /* now delete the invisible items from the list */
1322 while(iterator_next(i))
1324 LISTVIEW_GetItemOrigin(infoPtr, i->nItem, &Position);
1325 rcItem.left = Position.x + Origin.x;
1326 rcItem.top = Position.y + Origin.y;
1327 rcItem.right = rcItem.left + infoPtr->nItemWidth;
1328 rcItem.bottom = rcItem.top + infoPtr->nItemHeight;
1329 if (!RectVisible(hdc, &rcItem))
1330 ranges_delitem(i->ranges, i->nItem);
1332 /* the iterator should restart on the next iterator_next */
1338 /******** Misc helper functions ************************************/
1340 static inline LRESULT CallWindowProcT(WNDPROC proc, HWND hwnd, UINT uMsg,
1341 WPARAM wParam, LPARAM lParam, BOOL isW)
1343 if (isW) return CallWindowProcW(proc, hwnd, uMsg, wParam, lParam);
1344 else return CallWindowProcA(proc, hwnd, uMsg, wParam, lParam);
1347 static inline BOOL is_autoarrange(const LISTVIEW_INFO *infoPtr)
1349 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
1351 return ((infoPtr->dwStyle & LVS_AUTOARRANGE) || infoPtr->bAutoarrange) &&
1352 (uView == LVS_ICON || uView == LVS_SMALLICON);
1355 static void toggle_checkbox_state(LISTVIEW_INFO *infoPtr, INT nItem)
1357 DWORD state = STATEIMAGEINDEX(LISTVIEW_GetItemState(infoPtr, nItem, LVIS_STATEIMAGEMASK));
1358 if(state == 1 || state == 2)
1362 lvitem.state = INDEXTOSTATEIMAGEMASK(state);
1363 lvitem.stateMask = LVIS_STATEIMAGEMASK;
1364 LISTVIEW_SetItemState(infoPtr, nItem, &lvitem);
1368 /******** Internal API functions ************************************/
1370 static inline COLUMN_INFO * LISTVIEW_GetColumnInfo(const LISTVIEW_INFO *infoPtr, INT nSubItem)
1372 static COLUMN_INFO mainItem;
1374 if (nSubItem == 0 && DPA_GetPtrCount(infoPtr->hdpaColumns) == 0) return &mainItem;
1375 assert (nSubItem >= 0 && nSubItem < DPA_GetPtrCount(infoPtr->hdpaColumns));
1376 return (COLUMN_INFO *)DPA_GetPtr(infoPtr->hdpaColumns, nSubItem);
1379 static inline void LISTVIEW_GetHeaderRect(const LISTVIEW_INFO *infoPtr, INT nSubItem, LPRECT lprc)
1381 *lprc = LISTVIEW_GetColumnInfo(infoPtr, nSubItem)->rcHeader;
1384 static inline BOOL LISTVIEW_GetItemW(const LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem)
1386 return LISTVIEW_GetItemT(infoPtr, lpLVItem, TRUE);
1389 /* Listview invalidation functions: use _only_ these functions to invalidate */
1391 static inline BOOL is_redrawing(const LISTVIEW_INFO *infoPtr)
1393 return infoPtr->bRedraw;
1396 static inline void LISTVIEW_InvalidateRect(const LISTVIEW_INFO *infoPtr, const RECT* rect)
1398 if(!is_redrawing(infoPtr)) return;
1399 TRACE(" invalidating rect=%s\n", wine_dbgstr_rect(rect));
1400 InvalidateRect(infoPtr->hwndSelf, rect, TRUE);
1403 static inline void LISTVIEW_InvalidateItem(const LISTVIEW_INFO *infoPtr, INT nItem)
1407 if(!is_redrawing(infoPtr)) return;
1408 LISTVIEW_GetItemBox(infoPtr, nItem, &rcBox);
1409 LISTVIEW_InvalidateRect(infoPtr, &rcBox);
1412 static inline void LISTVIEW_InvalidateSubItem(const LISTVIEW_INFO *infoPtr, INT nItem, INT nSubItem)
1414 POINT Origin, Position;
1417 if(!is_redrawing(infoPtr)) return;
1418 assert ((infoPtr->dwStyle & LVS_TYPEMASK) == LVS_REPORT);
1419 LISTVIEW_GetOrigin(infoPtr, &Origin);
1420 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
1421 LISTVIEW_GetHeaderRect(infoPtr, nSubItem, &rcBox);
1423 rcBox.bottom = infoPtr->nItemHeight;
1424 OffsetRect(&rcBox, Origin.x + Position.x, Origin.y + Position.y);
1425 LISTVIEW_InvalidateRect(infoPtr, &rcBox);
1428 static inline void LISTVIEW_InvalidateList(const LISTVIEW_INFO *infoPtr)
1430 LISTVIEW_InvalidateRect(infoPtr, NULL);
1433 static inline void LISTVIEW_InvalidateColumn(const LISTVIEW_INFO *infoPtr, INT nColumn)
1437 if(!is_redrawing(infoPtr)) return;
1438 LISTVIEW_GetHeaderRect(infoPtr, nColumn, &rcCol);
1439 rcCol.top = infoPtr->rcList.top;
1440 rcCol.bottom = infoPtr->rcList.bottom;
1441 LISTVIEW_InvalidateRect(infoPtr, &rcCol);
1446 * Retrieves the number of items that can fit vertically in the client area.
1449 * [I] infoPtr : valid pointer to the listview structure
1452 * Number of items per row.
1454 static inline INT LISTVIEW_GetCountPerRow(const LISTVIEW_INFO *infoPtr)
1456 INT nListWidth = infoPtr->rcList.right - infoPtr->rcList.left;
1458 return max(nListWidth/infoPtr->nItemWidth, 1);
1463 * Retrieves the number of items that can fit horizontally in the client
1467 * [I] infoPtr : valid pointer to the listview structure
1470 * Number of items per column.
1472 static inline INT LISTVIEW_GetCountPerColumn(const LISTVIEW_INFO *infoPtr)
1474 INT nListHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
1476 return max(nListHeight / infoPtr->nItemHeight, 1);
1480 /*************************************************************************
1481 * LISTVIEW_ProcessLetterKeys
1483 * Processes keyboard messages generated by pressing the letter keys
1485 * What this does is perform a case insensitive search from the
1486 * current position with the following quirks:
1487 * - If two chars or more are pressed in quick succession we search
1488 * for the corresponding string (e.g. 'abc').
1489 * - If there is a delay we wipe away the current search string and
1490 * restart with just that char.
1491 * - If the user keeps pressing the same character, whether slowly or
1492 * fast, so that the search string is entirely composed of this
1493 * character ('aaaaa' for instance), then we search for first item
1494 * that starting with that character.
1495 * - If the user types the above character in quick succession, then
1496 * we must also search for the corresponding string ('aaaaa'), and
1497 * go to that string if there is a match.
1500 * [I] hwnd : handle to the window
1501 * [I] charCode : the character code, the actual character
1502 * [I] keyData : key data
1510 * - The current implementation has a list of characters it will
1511 * accept and it ignores everything else. In particular it will
1512 * ignore accentuated characters which seems to match what
1513 * Windows does. But I'm not sure it makes sense to follow
1515 * - We don't sound a beep when the search fails.
1519 * TREEVIEW_ProcessLetterKeys
1521 static INT LISTVIEW_ProcessLetterKeys(LISTVIEW_INFO *infoPtr, WPARAM charCode, LPARAM keyData)
1526 WCHAR buffer[MAX_PATH];
1527 DWORD lastKeyPressTimestamp = infoPtr->lastKeyPressTimestamp;
1529 /* simple parameter checking */
1530 if (!charCode || !keyData) return 0;
1532 /* only allow the valid WM_CHARs through */
1533 if (!isalnum(charCode) &&
1534 charCode != '.' && charCode != '`' && charCode != '!' &&
1535 charCode != '@' && charCode != '#' && charCode != '$' &&
1536 charCode != '%' && charCode != '^' && charCode != '&' &&
1537 charCode != '*' && charCode != '(' && charCode != ')' &&
1538 charCode != '-' && charCode != '_' && charCode != '+' &&
1539 charCode != '=' && charCode != '\\'&& charCode != ']' &&
1540 charCode != '}' && charCode != '[' && charCode != '{' &&
1541 charCode != '/' && charCode != '?' && charCode != '>' &&
1542 charCode != '<' && charCode != ',' && charCode != '~')
1545 /* if there's one item or less, there is no where to go */
1546 if (infoPtr->nItemCount <= 1) return 0;
1548 /* update the search parameters */
1549 infoPtr->lastKeyPressTimestamp = GetTickCount();
1550 if (infoPtr->lastKeyPressTimestamp - lastKeyPressTimestamp < KEY_DELAY) {
1551 if (infoPtr->nSearchParamLength < MAX_PATH)
1552 infoPtr->szSearchParam[infoPtr->nSearchParamLength++]=charCode;
1553 if (infoPtr->charCode != charCode)
1554 infoPtr->charCode = charCode = 0;
1556 infoPtr->charCode=charCode;
1557 infoPtr->szSearchParam[0]=charCode;
1558 infoPtr->nSearchParamLength=1;
1559 /* Redundant with the 1 char string */
1563 /* and search from the current position */
1565 if (infoPtr->nFocusedItem >= 0) {
1566 endidx=infoPtr->nFocusedItem;
1568 /* if looking for single character match,
1569 * then we must always move forward
1571 if (infoPtr->nSearchParamLength == 1)
1574 endidx=infoPtr->nItemCount;
1578 if (idx == infoPtr->nItemCount) {
1579 if (endidx == infoPtr->nItemCount || endidx == 0)
1585 item.mask = LVIF_TEXT;
1588 item.pszText = buffer;
1589 item.cchTextMax = MAX_PATH;
1590 if (!LISTVIEW_GetItemW(infoPtr, &item)) return 0;
1592 /* check for a match */
1593 if (lstrncmpiW(item.pszText,infoPtr->szSearchParam,infoPtr->nSearchParamLength) == 0) {
1596 } else if ( (charCode != 0) && (nItem == -1) && (nItem != infoPtr->nFocusedItem) &&
1597 (lstrncmpiW(item.pszText,infoPtr->szSearchParam,1) == 0) ) {
1598 /* This would work but we must keep looking for a longer match */
1602 } while (idx != endidx);
1605 LISTVIEW_KeySelection(infoPtr, nItem);
1610 /*************************************************************************
1611 * LISTVIEW_UpdateHeaderSize [Internal]
1613 * Function to resize the header control
1616 * [I] hwnd : handle to a window
1617 * [I] nNewScrollPos : scroll pos to set
1622 static void LISTVIEW_UpdateHeaderSize(const LISTVIEW_INFO *infoPtr, INT nNewScrollPos)
1627 TRACE("nNewScrollPos=%d\n", nNewScrollPos);
1629 GetWindowRect(infoPtr->hwndHeader, &winRect);
1630 point[0].x = winRect.left;
1631 point[0].y = winRect.top;
1632 point[1].x = winRect.right;
1633 point[1].y = winRect.bottom;
1635 MapWindowPoints(HWND_DESKTOP, infoPtr->hwndSelf, point, 2);
1636 point[0].x = -nNewScrollPos;
1637 point[1].x += nNewScrollPos;
1639 SetWindowPos(infoPtr->hwndHeader,0,
1640 point[0].x,point[0].y,point[1].x,point[1].y,
1641 SWP_NOZORDER | SWP_NOACTIVATE);
1646 * Update the scrollbars. This functions should be called whenever
1647 * the content, size or view changes.
1650 * [I] infoPtr : valid pointer to the listview structure
1655 static void LISTVIEW_UpdateScroll(const LISTVIEW_INFO *infoPtr)
1657 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
1658 SCROLLINFO horzInfo, vertInfo;
1661 if ((infoPtr->dwStyle & LVS_NOSCROLL) || !is_redrawing(infoPtr)) return;
1663 ZeroMemory(&horzInfo, sizeof(SCROLLINFO));
1664 horzInfo.cbSize = sizeof(SCROLLINFO);
1665 horzInfo.nPage = infoPtr->rcList.right - infoPtr->rcList.left;
1667 /* for now, we'll set info.nMax to the _count_, and adjust it later */
1668 if (uView == LVS_LIST)
1670 INT nPerCol = LISTVIEW_GetCountPerColumn(infoPtr);
1671 horzInfo.nMax = (infoPtr->nItemCount + nPerCol - 1) / nPerCol;
1673 /* scroll by at least one column per page */
1674 if(horzInfo.nPage < infoPtr->nItemWidth)
1675 horzInfo.nPage = infoPtr->nItemWidth;
1677 horzInfo.nPage /= infoPtr->nItemWidth;
1679 else if (uView == LVS_REPORT)
1681 horzInfo.nMax = infoPtr->nItemWidth;
1683 else /* LVS_ICON, or LVS_SMALLICON */
1687 if (LISTVIEW_GetViewRect(infoPtr, &rcView)) horzInfo.nMax = rcView.right - rcView.left;
1690 horzInfo.fMask = SIF_RANGE | SIF_PAGE;
1691 horzInfo.nMax = max(horzInfo.nMax - 1, 0);
1692 dx = GetScrollPos(infoPtr->hwndSelf, SB_HORZ);
1693 dx -= SetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &horzInfo, TRUE);
1694 TRACE("horzInfo=%s\n", debugscrollinfo(&horzInfo));
1696 /* Setting the horizontal scroll can change the listview size
1697 * (and potentially everything else) so we need to recompute
1698 * everything again for the vertical scroll
1701 ZeroMemory(&vertInfo, sizeof(SCROLLINFO));
1702 vertInfo.cbSize = sizeof(SCROLLINFO);
1703 vertInfo.nPage = infoPtr->rcList.bottom - infoPtr->rcList.top;
1705 if (uView == LVS_REPORT)
1707 vertInfo.nMax = infoPtr->nItemCount;
1709 /* scroll by at least one page */
1710 if(vertInfo.nPage < infoPtr->nItemHeight)
1711 vertInfo.nPage = infoPtr->nItemHeight;
1713 vertInfo.nPage /= infoPtr->nItemHeight;
1715 else if (uView != LVS_LIST) /* LVS_ICON, or LVS_SMALLICON */
1719 if (LISTVIEW_GetViewRect(infoPtr, &rcView)) vertInfo.nMax = rcView.bottom - rcView.top;
1722 vertInfo.fMask = SIF_RANGE | SIF_PAGE;
1723 vertInfo.nMax = max(vertInfo.nMax - 1, 0);
1724 dy = GetScrollPos(infoPtr->hwndSelf, SB_VERT);
1725 dy -= SetScrollInfo(infoPtr->hwndSelf, SB_VERT, &vertInfo, TRUE);
1726 TRACE("vertInfo=%s\n", debugscrollinfo(&vertInfo));
1728 /* Change of the range may have changed the scroll pos. If so move the content */
1729 if (dx != 0 || dy != 0)
1732 listRect = infoPtr->rcList;
1733 ScrollWindowEx(infoPtr->hwndSelf, dx, dy, &listRect, &listRect, 0, 0,
1734 SW_ERASE | SW_INVALIDATE);
1737 /* Update the Header Control */
1738 if (uView == LVS_REPORT)
1740 horzInfo.fMask = SIF_POS;
1741 GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &horzInfo);
1742 LISTVIEW_UpdateHeaderSize(infoPtr, horzInfo.nPos);
1749 * Shows/hides the focus rectangle.
1752 * [I] infoPtr : valid pointer to the listview structure
1753 * [I] fShow : TRUE to show the focus, FALSE to hide it.
1758 static void LISTVIEW_ShowFocusRect(const LISTVIEW_INFO *infoPtr, BOOL fShow)
1760 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
1763 TRACE("fShow=%d, nItem=%d\n", fShow, infoPtr->nFocusedItem);
1765 if (infoPtr->nFocusedItem < 0) return;
1767 /* we need some gymnastics in ICON mode to handle large items */
1768 if ( (infoPtr->dwStyle & LVS_TYPEMASK) == LVS_ICON )
1772 LISTVIEW_GetItemBox(infoPtr, infoPtr->nFocusedItem, &rcBox);
1773 if ((rcBox.bottom - rcBox.top) > infoPtr->nItemHeight)
1775 LISTVIEW_InvalidateRect(infoPtr, &rcBox);
1780 if (!(hdc = GetDC(infoPtr->hwndSelf))) return;
1782 /* for some reason, owner draw should work only in report mode */
1783 if ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (uView == LVS_REPORT))
1788 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
1789 HFONT hOldFont = SelectObject(hdc, hFont);
1791 item.iItem = infoPtr->nFocusedItem;
1793 item.mask = LVIF_PARAM;
1794 if (!LISTVIEW_GetItemW(infoPtr, &item)) goto done;
1796 ZeroMemory(&dis, sizeof(dis));
1797 dis.CtlType = ODT_LISTVIEW;
1798 dis.CtlID = (UINT)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1799 dis.itemID = item.iItem;
1800 dis.itemAction = ODA_FOCUS;
1801 if (fShow) dis.itemState |= ODS_FOCUS;
1802 dis.hwndItem = infoPtr->hwndSelf;
1804 LISTVIEW_GetItemBox(infoPtr, dis.itemID, &dis.rcItem);
1805 dis.itemData = item.lParam;
1807 SendMessageW(infoPtr->hwndNotify, WM_DRAWITEM, dis.CtlID, (LPARAM)&dis);
1809 SelectObject(hdc, hOldFont);
1813 DrawFocusRect(hdc, &infoPtr->rcFocus);
1816 ReleaseDC(infoPtr->hwndSelf, hdc);
1820 * Invalidates all visible selected items.
1822 static void LISTVIEW_InvalidateSelectedItems(const LISTVIEW_INFO *infoPtr)
1826 iterator_frameditems(&i, infoPtr, &infoPtr->rcList);
1827 while(iterator_next(&i))
1829 if (LISTVIEW_GetItemState(infoPtr, i.nItem, LVIS_SELECTED))
1830 LISTVIEW_InvalidateItem(infoPtr, i.nItem);
1832 iterator_destroy(&i);
1837 * DESCRIPTION: [INTERNAL]
1838 * Computes an item's (left,top) corner, relative to rcView.
1839 * That is, the position has NOT been made relative to the Origin.
1840 * This is deliberate, to avoid computing the Origin over, and
1841 * over again, when this function is called in a loop. Instead,
1842 * one can factor the computation of the Origin before the loop,
1843 * and offset the value returned by this function, on every iteration.
1846 * [I] infoPtr : valid pointer to the listview structure
1847 * [I] nItem : item number
1848 * [O] lpptOrig : item top, left corner
1853 static void LISTVIEW_GetItemOrigin(const LISTVIEW_INFO *infoPtr, INT nItem, LPPOINT lpptPosition)
1855 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
1857 assert(nItem >= 0 && nItem < infoPtr->nItemCount);
1859 if ((uView == LVS_SMALLICON) || (uView == LVS_ICON))
1861 lpptPosition->x = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosX, nItem);
1862 lpptPosition->y = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosY, nItem);
1864 else if (uView == LVS_LIST)
1866 INT nCountPerColumn = LISTVIEW_GetCountPerColumn(infoPtr);
1867 lpptPosition->x = nItem / nCountPerColumn * infoPtr->nItemWidth;
1868 lpptPosition->y = nItem % nCountPerColumn * infoPtr->nItemHeight;
1870 else /* LVS_REPORT */
1872 lpptPosition->x = 0;
1873 lpptPosition->y = nItem * infoPtr->nItemHeight;
1878 * DESCRIPTION: [INTERNAL]
1879 * Compute the rectangles of an item. This is to localize all
1880 * the computations in one place. If you are not interested in some
1881 * of these values, simply pass in a NULL -- the function is smart
1882 * enough to compute only what's necessary. The function computes
1883 * the standard rectangles (BOUNDS, ICON, LABEL) plus a non-standard
1884 * one, the BOX rectangle. This rectangle is very cheap to compute,
1885 * and is guaranteed to contain all the other rectangles. Computing
1886 * the ICON rect is also cheap, but all the others are potentially
1887 * expensive. This gives an easy and effective optimization when
1888 * searching (like point inclusion, or rectangle intersection):
1889 * first test against the BOX, and if TRUE, test against the desired
1891 * If the function does not have all the necessary information
1892 * to computed the requested rectangles, will crash with a
1893 * failed assertion. This is done so we catch all programming
1894 * errors, given that the function is called only from our code.
1896 * We have the following 'special' meanings for a few fields:
1897 * * If LVIS_FOCUSED is set, we assume the item has the focus
1898 * This is important in ICON mode, where it might get a larger
1899 * then usual rectangle
1901 * Please note that subitem support works only in REPORT mode.
1904 * [I] infoPtr : valid pointer to the listview structure
1905 * [I] lpLVItem : item to compute the measures for
1906 * [O] lprcBox : ptr to Box rectangle
1907 * Same as LVM_GETITEMRECT with LVIR_BOUNDS
1908 * [0] lprcSelectBox : ptr to select box rectangle
1909 * Same as LVM_GETITEMRECT with LVIR_SELECTEDBOUNDS
1910 * [O] lprcIcon : ptr to Icon rectangle
1911 * Same as LVM_GETITEMRECT with LVIR_ICON
1912 * [O] lprcStateIcon: ptr to State Icon rectangle
1913 * [O] lprcLabel : ptr to Label rectangle
1914 * Same as LVM_GETITEMRECT with LVIR_LABEL
1919 static void LISTVIEW_GetItemMetrics(const LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem,
1920 LPRECT lprcBox, LPRECT lprcSelectBox,
1921 LPRECT lprcIcon, LPRECT lprcStateIcon, LPRECT lprcLabel)
1923 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
1924 BOOL doSelectBox = FALSE, doIcon = FALSE, doLabel = FALSE, oversizedBox = FALSE;
1925 RECT Box, SelectBox, Icon, Label;
1926 COLUMN_INFO *lpColumnInfo = NULL;
1927 SIZE labelSize = { 0, 0 };
1929 TRACE("(lpLVItem=%s)\n", debuglvitem_t(lpLVItem, TRUE));
1931 /* Be smart and try to figure out the minimum we have to do */
1932 if (lpLVItem->iSubItem) assert(uView == LVS_REPORT);
1933 if (uView == LVS_ICON && (lprcBox || lprcLabel))
1935 assert((lpLVItem->mask & LVIF_STATE) && (lpLVItem->stateMask & LVIS_FOCUSED));
1936 if (lpLVItem->state & LVIS_FOCUSED) oversizedBox = doLabel = TRUE;
1938 if (lprcSelectBox) doSelectBox = TRUE;
1939 if (lprcLabel) doLabel = TRUE;
1940 if (doLabel || lprcIcon || lprcStateIcon) doIcon = TRUE;
1947 /************************************************************/
1948 /* compute the box rectangle (it should be cheap to do) */
1949 /************************************************************/
1950 if (lpLVItem->iSubItem || uView == LVS_REPORT)
1951 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, lpLVItem->iSubItem);
1953 if (lpLVItem->iSubItem)
1955 Box = lpColumnInfo->rcHeader;
1960 Box.right = infoPtr->nItemWidth;
1963 Box.bottom = infoPtr->nItemHeight;
1965 /******************************************************************/
1966 /* compute ICON bounding box (ala LVM_GETITEMRECT) and STATEICON */
1967 /******************************************************************/
1970 LONG state_width = 0;
1972 if (infoPtr->himlState && lpLVItem->iSubItem == 0)
1973 state_width = infoPtr->iconStateSize.cx;
1975 if (uView == LVS_ICON)
1977 Icon.left = Box.left + state_width;
1978 if (infoPtr->himlNormal)
1979 Icon.left += (infoPtr->nItemWidth - infoPtr->iconSize.cx - state_width) / 2;
1980 Icon.top = Box.top + ICON_TOP_PADDING;
1981 Icon.right = Icon.left;
1982 Icon.bottom = Icon.top;
1983 if (infoPtr->himlNormal)
1985 Icon.right += infoPtr->iconSize.cx;
1986 Icon.bottom += infoPtr->iconSize.cy;
1989 else /* LVS_SMALLICON, LVS_LIST or LVS_REPORT */
1991 Icon.left = Box.left + state_width;
1993 if (uView == LVS_REPORT)
1994 Icon.left += REPORT_MARGINX;
1997 Icon.right = Icon.left;
1998 if (infoPtr->himlSmall &&
1999 (!lpColumnInfo || lpLVItem->iSubItem == 0 || (lpColumnInfo->fmt & LVCFMT_IMAGE) ||
2000 ((infoPtr->dwLvExStyle & LVS_EX_SUBITEMIMAGES) && lpLVItem->iImage != I_IMAGECALLBACK)))
2001 Icon.right += infoPtr->iconSize.cx;
2002 Icon.bottom = Icon.top + infoPtr->iconSize.cy;
2004 if(lprcIcon) *lprcIcon = Icon;
2005 TRACE(" - icon=%s\n", wine_dbgstr_rect(&Icon));
2007 /* TODO: is this correct? */
2010 lprcStateIcon->left = Icon.left - state_width;
2011 lprcStateIcon->right = Icon.left;
2012 lprcStateIcon->top = Icon.top;
2013 lprcStateIcon->bottom = lprcStateIcon->top + infoPtr->iconSize.cy;
2014 TRACE(" - state icon=%s\n", wine_dbgstr_rect(lprcStateIcon));
2017 else Icon.right = 0;
2019 /************************************************************/
2020 /* compute LABEL bounding box (ala LVM_GETITEMRECT) */
2021 /************************************************************/
2024 /* calculate how far to the right can the label stretch */
2025 Label.right = Box.right;
2026 if (uView == LVS_REPORT)
2028 if (lpLVItem->iSubItem == 0) Label = lpColumnInfo->rcHeader;
2031 if (lpLVItem->iSubItem || ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && uView == LVS_REPORT))
2033 labelSize.cx = infoPtr->nItemWidth;
2034 labelSize.cy = infoPtr->nItemHeight;
2038 /* we need the text in non owner draw mode */
2039 assert(lpLVItem->mask & LVIF_TEXT);
2040 if (is_textT(lpLVItem->pszText, TRUE))
2042 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
2043 HDC hdc = GetDC(infoPtr->hwndSelf);
2044 HFONT hOldFont = SelectObject(hdc, hFont);
2048 /* compute rough rectangle where the label will go */
2049 SetRectEmpty(&rcText);
2050 rcText.right = infoPtr->nItemWidth - TRAILING_LABEL_PADDING;
2051 rcText.bottom = infoPtr->nItemHeight;
2052 if (uView == LVS_ICON)
2053 rcText.bottom -= ICON_TOP_PADDING + infoPtr->iconSize.cy + ICON_BOTTOM_PADDING;
2055 /* now figure out the flags */
2056 if (uView == LVS_ICON)
2057 uFormat = oversizedBox ? LV_FL_DT_FLAGS : LV_ML_DT_FLAGS;
2059 uFormat = LV_SL_DT_FLAGS;
2061 DrawTextW (hdc, lpLVItem->pszText, -1, &rcText, uFormat | DT_CALCRECT);
2063 labelSize.cx = min(rcText.right - rcText.left + TRAILING_LABEL_PADDING, infoPtr->nItemWidth);
2064 labelSize.cy = rcText.bottom - rcText.top;
2066 SelectObject(hdc, hOldFont);
2067 ReleaseDC(infoPtr->hwndSelf, hdc);
2071 if (uView == LVS_ICON)
2073 Label.left = Box.left + (infoPtr->nItemWidth - labelSize.cx) / 2;
2074 Label.top = Box.top + ICON_TOP_PADDING_HITABLE +
2075 infoPtr->iconSize.cy + ICON_BOTTOM_PADDING;
2076 Label.right = Label.left + labelSize.cx;
2077 Label.bottom = Label.top + infoPtr->nItemHeight;
2078 if (!oversizedBox && labelSize.cy > infoPtr->ntmHeight)
2080 labelSize.cy = min(Box.bottom - Label.top, labelSize.cy);
2081 labelSize.cy /= infoPtr->ntmHeight;
2082 labelSize.cy = max(labelSize.cy, 1);
2083 labelSize.cy *= infoPtr->ntmHeight;
2085 Label.bottom = Label.top + labelSize.cy + HEIGHT_PADDING;
2087 else if (uView == LVS_REPORT)
2089 Label.left = Icon.right;
2090 Label.top = Box.top;
2091 Label.right = lpColumnInfo->rcHeader.right;
2092 Label.bottom = Label.top + infoPtr->nItemHeight;
2094 else /* LVS_SMALLICON, LVS_LIST or LVS_REPORT */
2096 Label.left = Icon.right;
2097 Label.top = Box.top;
2098 Label.right = min(Label.left + labelSize.cx, Label.right);
2099 Label.bottom = Label.top + infoPtr->nItemHeight;
2102 if (lprcLabel) *lprcLabel = Label;
2103 TRACE(" - label=%s\n", wine_dbgstr_rect(&Label));
2106 /************************************************************/
2107 /* compute STATEICON bounding box */
2108 /************************************************************/
2111 if (uView == LVS_REPORT)
2113 SelectBox.left = Icon.right; /* FIXME: should be Icon.left */
2114 SelectBox.top = Box.top;
2115 SelectBox.bottom = Box.bottom;
2116 if (lpLVItem->iSubItem == 0)
2118 /* we need the indent in report mode */
2119 assert(lpLVItem->mask & LVIF_INDENT);
2120 SelectBox.left += infoPtr->iconSize.cx * lpLVItem->iIndent;
2122 SelectBox.right = min(SelectBox.left + labelSize.cx, Label.right);
2126 UnionRect(&SelectBox, &Icon, &Label);
2128 if (lprcSelectBox) *lprcSelectBox = SelectBox;
2129 TRACE(" - select box=%s\n", wine_dbgstr_rect(&SelectBox));
2132 /* Fix the Box if necessary */
2135 if (oversizedBox) UnionRect(lprcBox, &Box, &Label);
2136 else *lprcBox = Box;
2138 TRACE(" - box=%s\n", wine_dbgstr_rect(&Box));
2142 * DESCRIPTION: [INTERNAL]
2145 * [I] infoPtr : valid pointer to the listview structure
2146 * [I] nItem : item number
2147 * [O] lprcBox : ptr to Box rectangle
2152 static void LISTVIEW_GetItemBox(const LISTVIEW_INFO *infoPtr, INT nItem, LPRECT lprcBox)
2154 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
2155 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
2156 POINT Position, Origin;
2159 LISTVIEW_GetOrigin(infoPtr, &Origin);
2160 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
2162 /* Be smart and try to figure out the minimum we have to do */
2164 if (uView == LVS_ICON && infoPtr->bFocus && LISTVIEW_GetItemState(infoPtr, nItem, LVIS_FOCUSED))
2165 lvItem.mask |= LVIF_TEXT;
2166 lvItem.iItem = nItem;
2167 lvItem.iSubItem = 0;
2168 lvItem.pszText = szDispText;
2169 lvItem.cchTextMax = DISP_TEXT_SIZE;
2170 if (lvItem.mask) LISTVIEW_GetItemW(infoPtr, &lvItem);
2171 if (uView == LVS_ICON)
2173 lvItem.mask |= LVIF_STATE;
2174 lvItem.stateMask = LVIS_FOCUSED;
2175 lvItem.state = (lvItem.mask & LVIF_TEXT ? LVIS_FOCUSED : 0);
2177 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, lprcBox, 0, 0, 0, 0);
2179 OffsetRect(lprcBox, Position.x + Origin.x, Position.y + Origin.y);
2185 * Returns the current icon position, and advances it along the top.
2186 * The returned position is not offset by Origin.
2189 * [I] infoPtr : valid pointer to the listview structure
2190 * [O] lpPos : will get the current icon position
2195 static void LISTVIEW_NextIconPosTop(LISTVIEW_INFO *infoPtr, LPPOINT lpPos)
2197 INT nListWidth = infoPtr->rcList.right - infoPtr->rcList.left;
2199 *lpPos = infoPtr->currIconPos;
2201 infoPtr->currIconPos.x += infoPtr->nItemWidth;
2202 if (infoPtr->currIconPos.x + infoPtr->nItemWidth <= nListWidth) return;
2204 infoPtr->currIconPos.x = 0;
2205 infoPtr->currIconPos.y += infoPtr->nItemHeight;
2211 * Returns the current icon position, and advances it down the left edge.
2212 * The returned position is not offset by Origin.
2215 * [I] infoPtr : valid pointer to the listview structure
2216 * [O] lpPos : will get the current icon position
2221 static void LISTVIEW_NextIconPosLeft(LISTVIEW_INFO *infoPtr, LPPOINT lpPos)
2223 INT nListHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
2225 *lpPos = infoPtr->currIconPos;
2227 infoPtr->currIconPos.y += infoPtr->nItemHeight;
2228 if (infoPtr->currIconPos.y + infoPtr->nItemHeight <= nListHeight) return;
2230 infoPtr->currIconPos.x += infoPtr->nItemWidth;
2231 infoPtr->currIconPos.y = 0;
2237 * Moves an icon to the specified position.
2238 * It takes care of invalidating the item, etc.
2241 * [I] infoPtr : valid pointer to the listview structure
2242 * [I] nItem : the item to move
2243 * [I] lpPos : the new icon position
2244 * [I] isNew : flags the item as being new
2250 static BOOL LISTVIEW_MoveIconTo(const LISTVIEW_INFO *infoPtr, INT nItem, const POINT *lppt, BOOL isNew)
2256 old.x = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosX, nItem);
2257 old.y = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosY, nItem);
2259 if (lppt->x == old.x && lppt->y == old.y) return TRUE;
2260 LISTVIEW_InvalidateItem(infoPtr, nItem);
2263 /* Allocating a POINTER for every item is too resource intensive,
2264 * so we'll keep the (x,y) in different arrays */
2265 if (!DPA_SetPtr(infoPtr->hdpaPosX, nItem, (void *)(LONG_PTR)lppt->x)) return FALSE;
2266 if (!DPA_SetPtr(infoPtr->hdpaPosY, nItem, (void *)(LONG_PTR)lppt->y)) return FALSE;
2268 LISTVIEW_InvalidateItem(infoPtr, nItem);
2275 * Arranges listview items in icon display mode.
2278 * [I] infoPtr : valid pointer to the listview structure
2279 * [I] nAlignCode : alignment code
2285 static BOOL LISTVIEW_Arrange(LISTVIEW_INFO *infoPtr, INT nAlignCode)
2287 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
2288 void (*next_pos)(LISTVIEW_INFO *, LPPOINT);
2292 if (uView != LVS_ICON && uView != LVS_SMALLICON) return FALSE;
2294 TRACE("nAlignCode=%d\n", nAlignCode);
2296 if (nAlignCode == LVA_DEFAULT)
2298 if (infoPtr->dwStyle & LVS_ALIGNLEFT) nAlignCode = LVA_ALIGNLEFT;
2299 else nAlignCode = LVA_ALIGNTOP;
2304 case LVA_ALIGNLEFT: next_pos = LISTVIEW_NextIconPosLeft; break;
2305 case LVA_ALIGNTOP: next_pos = LISTVIEW_NextIconPosTop; break;
2306 case LVA_SNAPTOGRID: next_pos = LISTVIEW_NextIconPosTop; break; /* FIXME */
2307 default: return FALSE;
2310 infoPtr->bAutoarrange = TRUE;
2311 infoPtr->currIconPos.x = infoPtr->currIconPos.y = 0;
2312 for (i = 0; i < infoPtr->nItemCount; i++)
2314 next_pos(infoPtr, &pos);
2315 LISTVIEW_MoveIconTo(infoPtr, i, &pos, FALSE);
2323 * Retrieves the bounding rectangle of all the items, not offset by Origin.
2326 * [I] infoPtr : valid pointer to the listview structure
2327 * [O] lprcView : bounding rectangle
2333 static void LISTVIEW_GetAreaRect(const LISTVIEW_INFO *infoPtr, LPRECT lprcView)
2337 SetRectEmpty(lprcView);
2339 switch (infoPtr->dwStyle & LVS_TYPEMASK)
2343 for (i = 0; i < infoPtr->nItemCount; i++)
2345 x = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosX, i);
2346 y = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosY, i);
2347 lprcView->right = max(lprcView->right, x);
2348 lprcView->bottom = max(lprcView->bottom, y);
2350 if (infoPtr->nItemCount > 0)
2352 lprcView->right += infoPtr->nItemWidth;
2353 lprcView->bottom += infoPtr->nItemHeight;
2358 y = LISTVIEW_GetCountPerColumn(infoPtr);
2359 x = infoPtr->nItemCount / y;
2360 if (infoPtr->nItemCount % y) x++;
2361 lprcView->right = x * infoPtr->nItemWidth;
2362 lprcView->bottom = y * infoPtr->nItemHeight;
2366 lprcView->right = infoPtr->nItemWidth;
2367 lprcView->bottom = infoPtr->nItemCount * infoPtr->nItemHeight;
2374 * Retrieves the bounding rectangle of all the items.
2377 * [I] infoPtr : valid pointer to the listview structure
2378 * [O] lprcView : bounding rectangle
2384 static BOOL LISTVIEW_GetViewRect(const LISTVIEW_INFO *infoPtr, LPRECT lprcView)
2388 TRACE("(lprcView=%p)\n", lprcView);
2390 if (!lprcView) return FALSE;
2392 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
2393 LISTVIEW_GetAreaRect(infoPtr, lprcView);
2394 OffsetRect(lprcView, ptOrigin.x, ptOrigin.y);
2396 TRACE("lprcView=%s\n", wine_dbgstr_rect(lprcView));
2403 * Retrieves the subitem pointer associated with the subitem index.
2406 * [I] hdpaSubItems : DPA handle for a specific item
2407 * [I] nSubItem : index of subitem
2410 * SUCCESS : subitem pointer
2413 static SUBITEM_INFO* LISTVIEW_GetSubItemPtr(HDPA hdpaSubItems, INT nSubItem)
2415 SUBITEM_INFO *lpSubItem;
2418 /* we should binary search here if need be */
2419 for (i = 1; i < DPA_GetPtrCount(hdpaSubItems); i++)
2421 lpSubItem = (SUBITEM_INFO *)DPA_GetPtr(hdpaSubItems, i);
2422 if (lpSubItem->iSubItem == nSubItem)
2432 * Calculates the desired item width.
2435 * [I] infoPtr : valid pointer to the listview structure
2438 * The desired item width.
2440 static INT LISTVIEW_CalculateItemWidth(const LISTVIEW_INFO *infoPtr)
2442 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
2445 TRACE("uView=%d\n", uView);
2447 if (uView == LVS_ICON)
2448 nItemWidth = infoPtr->iconSpacing.cx;
2449 else if (uView == LVS_REPORT)
2453 if (DPA_GetPtrCount(infoPtr->hdpaColumns) > 0)
2455 LISTVIEW_GetHeaderRect(infoPtr, DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, &rcHeader);
2456 nItemWidth = rcHeader.right;
2459 else /* LVS_SMALLICON, or LVS_LIST */
2463 for (i = 0; i < infoPtr->nItemCount; i++)
2464 nItemWidth = max(LISTVIEW_GetLabelWidth(infoPtr, i), nItemWidth);
2466 if (infoPtr->himlSmall) nItemWidth += infoPtr->iconSize.cx;
2467 if (infoPtr->himlState) nItemWidth += infoPtr->iconStateSize.cx;
2469 nItemWidth = max(DEFAULT_COLUMN_WIDTH, nItemWidth + WIDTH_PADDING);
2472 return max(nItemWidth, 1);
2477 * Calculates the desired item height.
2480 * [I] infoPtr : valid pointer to the listview structure
2483 * The desired item height.
2485 static INT LISTVIEW_CalculateItemHeight(const LISTVIEW_INFO *infoPtr)
2487 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
2490 TRACE("uView=%d\n", uView);
2492 if (uView == LVS_ICON)
2493 nItemHeight = infoPtr->iconSpacing.cy;
2496 nItemHeight = infoPtr->ntmHeight;
2497 if (uView == LVS_REPORT && infoPtr->dwLvExStyle & LVS_EX_GRIDLINES)
2499 if (infoPtr->himlState)
2500 nItemHeight = max(nItemHeight, infoPtr->iconStateSize.cy);
2501 if (infoPtr->himlSmall)
2502 nItemHeight = max(nItemHeight, infoPtr->iconSize.cy);
2503 if (infoPtr->himlState || infoPtr->himlSmall)
2504 nItemHeight += HEIGHT_PADDING;
2505 if (infoPtr->nMeasureItemHeight > 0)
2506 nItemHeight = infoPtr->nMeasureItemHeight;
2509 return max(nItemHeight, 1);
2514 * Updates the width, and height of an item.
2517 * [I] infoPtr : valid pointer to the listview structure
2522 static inline void LISTVIEW_UpdateItemSize(LISTVIEW_INFO *infoPtr)
2524 infoPtr->nItemWidth = LISTVIEW_CalculateItemWidth(infoPtr);
2525 infoPtr->nItemHeight = LISTVIEW_CalculateItemHeight(infoPtr);
2531 * Retrieves and saves important text metrics info for the current
2535 * [I] infoPtr : valid pointer to the listview structure
2538 static void LISTVIEW_SaveTextMetrics(LISTVIEW_INFO *infoPtr)
2540 HDC hdc = GetDC(infoPtr->hwndSelf);
2541 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
2542 HFONT hOldFont = SelectObject(hdc, hFont);
2546 if (GetTextMetricsW(hdc, &tm))
2548 infoPtr->ntmHeight = tm.tmHeight;
2549 infoPtr->ntmMaxCharWidth = tm.tmMaxCharWidth;
2552 if (GetTextExtentPoint32A(hdc, "...", 3, &sz))
2553 infoPtr->nEllipsisWidth = sz.cx;
2555 SelectObject(hdc, hOldFont);
2556 ReleaseDC(infoPtr->hwndSelf, hdc);
2558 TRACE("tmHeight=%d\n", infoPtr->ntmHeight);
2563 * A compare function for ranges
2566 * [I] range1 : pointer to range 1;
2567 * [I] range2 : pointer to range 2;
2571 * > 0 : if range 1 > range 2
2572 * < 0 : if range 2 > range 1
2573 * = 0 : if range intersects range 2
2575 static INT CALLBACK ranges_cmp(LPVOID range1, LPVOID range2, LPARAM flags)
2579 if (((RANGE*)range1)->upper <= ((RANGE*)range2)->lower)
2581 else if (((RANGE*)range2)->upper <= ((RANGE*)range1)->lower)
2586 TRACE("range1=%s, range2=%s, cmp=%d\n", debugrange((RANGE*)range1), debugrange((RANGE*)range2), cmp);
2592 #define ranges_check(ranges, desc) ranges_assert(ranges, desc, __FUNCTION__, __LINE__)
2594 #define ranges_check(ranges, desc) do { } while(0)
2597 static void ranges_assert(RANGES ranges, LPCSTR desc, const char *func, int line)
2602 TRACE("*** Checking %s:%d:%s ***\n", func, line, desc);
2604 assert (DPA_GetPtrCount(ranges->hdpa) >= 0);
2605 ranges_dump(ranges);
2606 prev = (RANGE *)DPA_GetPtr(ranges->hdpa, 0);
2607 if (DPA_GetPtrCount(ranges->hdpa) > 0)
2608 assert (prev->lower >= 0 && prev->lower < prev->upper);
2609 for (i = 1; i < DPA_GetPtrCount(ranges->hdpa); i++)
2611 curr = (RANGE *)DPA_GetPtr(ranges->hdpa, i);
2612 assert (prev->upper <= curr->lower);
2613 assert (curr->lower < curr->upper);
2616 TRACE("--- Done checking---\n");
2619 static RANGES ranges_create(int count)
2621 RANGES ranges = Alloc(sizeof(struct tagRANGES));
2622 if (!ranges) return NULL;
2623 ranges->hdpa = DPA_Create(count);
2624 if (ranges->hdpa) return ranges;
2629 static void ranges_clear(RANGES ranges)
2633 for(i = 0; i < DPA_GetPtrCount(ranges->hdpa); i++)
2634 Free(DPA_GetPtr(ranges->hdpa, i));
2635 DPA_DeleteAllPtrs(ranges->hdpa);
2639 static void ranges_destroy(RANGES ranges)
2641 if (!ranges) return;
2642 ranges_clear(ranges);
2643 DPA_Destroy(ranges->hdpa);
2647 static RANGES ranges_clone(RANGES ranges)
2652 if (!(clone = ranges_create(DPA_GetPtrCount(ranges->hdpa)))) goto fail;
2654 for (i = 0; i < DPA_GetPtrCount(ranges->hdpa); i++)
2656 RANGE *newrng = Alloc(sizeof(RANGE));
2657 if (!newrng) goto fail;
2658 *newrng = *((RANGE*)DPA_GetPtr(ranges->hdpa, i));
2659 DPA_SetPtr(clone->hdpa, i, newrng);
2664 TRACE ("clone failed\n");
2665 ranges_destroy(clone);
2669 static RANGES ranges_diff(RANGES ranges, RANGES sub)
2673 for (i = 0; i < DPA_GetPtrCount(sub->hdpa); i++)
2674 ranges_del(ranges, *((RANGE *)DPA_GetPtr(sub->hdpa, i)));
2679 static void ranges_dump(RANGES ranges)
2683 for (i = 0; i < DPA_GetPtrCount(ranges->hdpa); i++)
2684 TRACE(" %s\n", debugrange(DPA_GetPtr(ranges->hdpa, i)));
2687 static inline BOOL ranges_contain(RANGES ranges, INT nItem)
2689 RANGE srchrng = { nItem, nItem + 1 };
2691 TRACE("(nItem=%d)\n", nItem);
2692 ranges_check(ranges, "before contain");
2693 return DPA_Search(ranges->hdpa, &srchrng, 0, ranges_cmp, 0, DPAS_SORTED) != -1;
2696 static INT ranges_itemcount(RANGES ranges)
2700 for (i = 0; i < DPA_GetPtrCount(ranges->hdpa); i++)
2702 RANGE *sel = DPA_GetPtr(ranges->hdpa, i);
2703 count += sel->upper - sel->lower;
2709 static BOOL ranges_shift(RANGES ranges, INT nItem, INT delta, INT nUpper)
2711 RANGE srchrng = { nItem, nItem + 1 }, *chkrng;
2714 index = DPA_Search(ranges->hdpa, &srchrng, 0, ranges_cmp, 0, DPAS_SORTED | DPAS_INSERTAFTER);
2715 if (index == -1) return TRUE;
2717 for (; index < DPA_GetPtrCount(ranges->hdpa); index++)
2719 chkrng = DPA_GetPtr(ranges->hdpa, index);
2720 if (chkrng->lower >= nItem)
2721 chkrng->lower = max(min(chkrng->lower + delta, nUpper - 1), 0);
2722 if (chkrng->upper > nItem)
2723 chkrng->upper = max(min(chkrng->upper + delta, nUpper), 0);
2728 static BOOL ranges_add(RANGES ranges, RANGE range)
2733 TRACE("(%s)\n", debugrange(&range));
2734 ranges_check(ranges, "before add");
2736 /* try find overlapping regions first */
2737 srchrgn.lower = range.lower - 1;
2738 srchrgn.upper = range.upper + 1;
2739 index = DPA_Search(ranges->hdpa, &srchrgn, 0, ranges_cmp, 0, DPAS_SORTED);
2745 TRACE("Adding new range\n");
2747 /* create the brand new range to insert */
2748 newrgn = Alloc(sizeof(RANGE));
2749 if(!newrgn) goto fail;
2752 /* figure out where to insert it */
2753 index = DPA_Search(ranges->hdpa, newrgn, 0, ranges_cmp, 0, DPAS_SORTED | DPAS_INSERTAFTER);
2754 TRACE("index=%d\n", index);
2755 if (index == -1) index = 0;
2757 /* and get it over with */
2758 if (DPA_InsertPtr(ranges->hdpa, index, newrgn) == -1)
2766 RANGE *chkrgn, *mrgrgn;
2767 INT fromindex, mergeindex;
2769 chkrgn = DPA_GetPtr(ranges->hdpa, index);
2770 TRACE("Merge with %s @%d\n", debugrange(chkrgn), index);
2772 chkrgn->lower = min(range.lower, chkrgn->lower);
2773 chkrgn->upper = max(range.upper, chkrgn->upper);
2775 TRACE("New range %s @%d\n", debugrange(chkrgn), index);
2777 /* merge now common ranges */
2779 srchrgn.lower = chkrgn->lower - 1;
2780 srchrgn.upper = chkrgn->upper + 1;
2784 mergeindex = DPA_Search(ranges->hdpa, &srchrgn, fromindex, ranges_cmp, 0, 0);
2785 if (mergeindex == -1) break;
2786 if (mergeindex == index)
2788 fromindex = index + 1;
2792 TRACE("Merge with index %i\n", mergeindex);
2794 mrgrgn = DPA_GetPtr(ranges->hdpa, mergeindex);
2795 chkrgn->lower = min(chkrgn->lower, mrgrgn->lower);
2796 chkrgn->upper = max(chkrgn->upper, mrgrgn->upper);
2798 DPA_DeletePtr(ranges->hdpa, mergeindex);
2799 if (mergeindex < index) index --;
2803 ranges_check(ranges, "after add");
2807 ranges_check(ranges, "failed add");
2811 static BOOL ranges_del(RANGES ranges, RANGE range)
2816 TRACE("(%s)\n", debugrange(&range));
2817 ranges_check(ranges, "before del");
2819 /* we don't use DPAS_SORTED here, since we need *
2820 * to find the first overlapping range */
2821 index = DPA_Search(ranges->hdpa, &range, 0, ranges_cmp, 0, 0);
2824 chkrgn = DPA_GetPtr(ranges->hdpa, index);
2826 TRACE("Matches range %s @%d\n", debugrange(chkrgn), index);
2828 /* case 1: Same range */
2829 if ( (chkrgn->upper == range.upper) &&
2830 (chkrgn->lower == range.lower) )
2832 DPA_DeletePtr(ranges->hdpa, index);
2835 /* case 2: engulf */
2836 else if ( (chkrgn->upper <= range.upper) &&
2837 (chkrgn->lower >= range.lower) )
2839 DPA_DeletePtr(ranges->hdpa, index);
2841 /* case 3: overlap upper */
2842 else if ( (chkrgn->upper <= range.upper) &&
2843 (chkrgn->lower < range.lower) )
2845 chkrgn->upper = range.lower;
2847 /* case 4: overlap lower */
2848 else if ( (chkrgn->upper > range.upper) &&
2849 (chkrgn->lower >= range.lower) )
2851 chkrgn->lower = range.upper;
2854 /* case 5: fully internal */
2857 RANGE tmprgn = *chkrgn, *newrgn;
2859 if (!(newrgn = Alloc(sizeof(RANGE)))) goto fail;
2860 newrgn->lower = chkrgn->lower;
2861 newrgn->upper = range.lower;
2862 chkrgn->lower = range.upper;
2863 if (DPA_InsertPtr(ranges->hdpa, index, newrgn) == -1)
2872 index = DPA_Search(ranges->hdpa, &range, index, ranges_cmp, 0, 0);
2875 ranges_check(ranges, "after del");
2879 ranges_check(ranges, "failed del");
2885 * Removes all selection ranges
2888 * [I] infoPtr : valid pointer to the listview structure
2889 * [I] toSkip : item range to skip removing the selection
2895 static BOOL LISTVIEW_DeselectAllSkipItems(LISTVIEW_INFO *infoPtr, RANGES toSkip)
2904 lvItem.stateMask = LVIS_SELECTED;
2906 /* need to clone the DPA because callbacks can change it */
2907 if (!(clone = ranges_clone(infoPtr->selectionRanges))) return FALSE;
2908 iterator_rangesitems(&i, ranges_diff(clone, toSkip));
2909 while(iterator_next(&i))
2910 LISTVIEW_SetItemState(infoPtr, i.nItem, &lvItem);
2911 /* note that the iterator destructor will free the cloned range */
2912 iterator_destroy(&i);
2917 static inline BOOL LISTVIEW_DeselectAllSkipItem(LISTVIEW_INFO *infoPtr, INT nItem)
2921 if (!(toSkip = ranges_create(1))) return FALSE;
2922 if (nItem != -1) ranges_additem(toSkip, nItem);
2923 LISTVIEW_DeselectAllSkipItems(infoPtr, toSkip);
2924 ranges_destroy(toSkip);
2928 static inline BOOL LISTVIEW_DeselectAll(LISTVIEW_INFO *infoPtr)
2930 return LISTVIEW_DeselectAllSkipItem(infoPtr, -1);
2935 * Retrieves the number of items that are marked as selected.
2938 * [I] infoPtr : valid pointer to the listview structure
2941 * Number of items selected.
2943 static INT LISTVIEW_GetSelectedCount(const LISTVIEW_INFO *infoPtr)
2945 INT nSelectedCount = 0;
2947 if (infoPtr->uCallbackMask & LVIS_SELECTED)
2950 for (i = 0; i < infoPtr->nItemCount; i++)
2952 if (LISTVIEW_GetItemState(infoPtr, i, LVIS_SELECTED))
2957 nSelectedCount = ranges_itemcount(infoPtr->selectionRanges);
2959 TRACE("nSelectedCount=%d\n", nSelectedCount);
2960 return nSelectedCount;
2965 * Manages the item focus.
2968 * [I] infoPtr : valid pointer to the listview structure
2969 * [I] nItem : item index
2972 * TRUE : focused item changed
2973 * FALSE : focused item has NOT changed
2975 static inline BOOL LISTVIEW_SetItemFocus(LISTVIEW_INFO *infoPtr, INT nItem)
2977 INT oldFocus = infoPtr->nFocusedItem;
2980 if (nItem == infoPtr->nFocusedItem) return FALSE;
2982 lvItem.state = nItem == -1 ? 0 : LVIS_FOCUSED;
2983 lvItem.stateMask = LVIS_FOCUSED;
2984 LISTVIEW_SetItemState(infoPtr, nItem == -1 ? infoPtr->nFocusedItem : nItem, &lvItem);
2986 return oldFocus != infoPtr->nFocusedItem;
2989 /* Helper function for LISTVIEW_ShiftIndices *only* */
2990 static INT shift_item(const LISTVIEW_INFO *infoPtr, INT nShiftItem, INT nItem, INT direction)
2992 if (nShiftItem < nItem) return nShiftItem;
2994 if (nShiftItem > nItem) return nShiftItem + direction;
2996 if (direction > 0) return nShiftItem + direction;
2998 return min(nShiftItem, infoPtr->nItemCount - 1);
3003 * Updates the various indices after an item has been inserted or deleted.
3006 * [I] infoPtr : valid pointer to the listview structure
3007 * [I] nItem : item index
3008 * [I] direction : Direction of shift, +1 or -1.
3013 static void LISTVIEW_ShiftIndices(LISTVIEW_INFO *infoPtr, INT nItem, INT direction)
3018 /* temporarily disable change notification while shifting items */
3019 bOldChange = infoPtr->bDoChangeNotify;
3020 infoPtr->bDoChangeNotify = FALSE;
3022 TRACE("Shifting %iu, %i steps\n", nItem, direction);
3024 ranges_shift(infoPtr->selectionRanges, nItem, direction, infoPtr->nItemCount);
3026 assert(abs(direction) == 1);
3028 infoPtr->nSelectionMark = shift_item(infoPtr, infoPtr->nSelectionMark, nItem, direction);
3030 nNewFocus = shift_item(infoPtr, infoPtr->nFocusedItem, nItem, direction);
3031 if (nNewFocus != infoPtr->nFocusedItem)
3032 LISTVIEW_SetItemFocus(infoPtr, nNewFocus);
3034 /* But we are not supposed to modify nHotItem! */
3036 infoPtr->bDoChangeNotify = bOldChange;
3042 * Adds a block of selections.
3045 * [I] infoPtr : valid pointer to the listview structure
3046 * [I] nItem : item index
3049 * Whether the window is still valid.
3051 static BOOL LISTVIEW_AddGroupSelection(LISTVIEW_INFO *infoPtr, INT nItem)
3053 INT nFirst = min(infoPtr->nSelectionMark, nItem);
3054 INT nLast = max(infoPtr->nSelectionMark, nItem);
3055 HWND hwndSelf = infoPtr->hwndSelf;
3056 NMLVODSTATECHANGE nmlv;
3061 /* Temporarily disable change notification
3062 * If the control is LVS_OWNERDATA, we need to send
3063 * only one LVN_ODSTATECHANGED notification.
3064 * See MSDN documentation for LVN_ITEMCHANGED.
3066 bOldChange = infoPtr->bDoChangeNotify;
3067 if (infoPtr->dwStyle & LVS_OWNERDATA) infoPtr->bDoChangeNotify = FALSE;
3069 if (nFirst == -1) nFirst = nItem;
3071 item.state = LVIS_SELECTED;
3072 item.stateMask = LVIS_SELECTED;
3074 for (i = nFirst; i <= nLast; i++)
3075 LISTVIEW_SetItemState(infoPtr,i,&item);
3077 ZeroMemory(&nmlv, sizeof(nmlv));
3078 nmlv.iFrom = nFirst;
3081 nmlv.uOldState = item.state;
3083 notify_hdr(infoPtr, LVN_ODSTATECHANGED, (LPNMHDR)&nmlv);
3084 if (!IsWindow(hwndSelf))
3086 infoPtr->bDoChangeNotify = bOldChange;
3093 * Sets a single group selection.
3096 * [I] infoPtr : valid pointer to the listview structure
3097 * [I] nItem : item index
3102 static void LISTVIEW_SetGroupSelection(LISTVIEW_INFO *infoPtr, INT nItem)
3104 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
3110 if (!(selection = ranges_create(100))) return;
3112 item.state = LVIS_SELECTED;
3113 item.stateMask = LVIS_SELECTED;
3115 if ((uView == LVS_LIST) || (uView == LVS_REPORT))
3117 if (infoPtr->nSelectionMark == -1)
3119 infoPtr->nSelectionMark = nItem;
3120 ranges_additem(selection, nItem);
3126 sel.lower = min(infoPtr->nSelectionMark, nItem);
3127 sel.upper = max(infoPtr->nSelectionMark, nItem) + 1;
3128 ranges_add(selection, sel);
3133 RECT rcItem, rcSel, rcSelMark;
3136 rcItem.left = LVIR_BOUNDS;
3137 if (!LISTVIEW_GetItemRect(infoPtr, nItem, &rcItem)) return;
3138 rcSelMark.left = LVIR_BOUNDS;
3139 if (!LISTVIEW_GetItemRect(infoPtr, infoPtr->nSelectionMark, &rcSelMark)) return;
3140 UnionRect(&rcSel, &rcItem, &rcSelMark);
3141 iterator_frameditems(&i, infoPtr, &rcSel);
3142 while(iterator_next(&i))
3144 LISTVIEW_GetItemPosition(infoPtr, i.nItem, &ptItem);
3145 if (PtInRect(&rcSel, ptItem)) ranges_additem(selection, i.nItem);
3147 iterator_destroy(&i);
3150 bOldChange = infoPtr->bDoChangeNotify;
3151 infoPtr->bDoChangeNotify = FALSE;
3153 LISTVIEW_DeselectAllSkipItems(infoPtr, selection);
3156 iterator_rangesitems(&i, selection);
3157 while(iterator_next(&i))
3158 LISTVIEW_SetItemState(infoPtr, i.nItem, &item);
3159 /* this will also destroy the selection */
3160 iterator_destroy(&i);
3162 infoPtr->bDoChangeNotify = bOldChange;
3164 LISTVIEW_SetItemFocus(infoPtr, nItem);
3169 * Sets a single selection.
3172 * [I] infoPtr : valid pointer to the listview structure
3173 * [I] nItem : item index
3178 static void LISTVIEW_SetSelection(LISTVIEW_INFO *infoPtr, INT nItem)
3182 TRACE("nItem=%d\n", nItem);
3184 LISTVIEW_DeselectAllSkipItem(infoPtr, nItem);
3186 lvItem.state = LVIS_FOCUSED | LVIS_SELECTED;
3187 lvItem.stateMask = LVIS_FOCUSED | LVIS_SELECTED;
3188 LISTVIEW_SetItemState(infoPtr, nItem, &lvItem);
3190 infoPtr->nSelectionMark = nItem;
3195 * Set selection(s) with keyboard.
3198 * [I] infoPtr : valid pointer to the listview structure
3199 * [I] nItem : item index
3202 * SUCCESS : TRUE (needs to be repainted)
3203 * FAILURE : FALSE (nothing has changed)
3205 static BOOL LISTVIEW_KeySelection(LISTVIEW_INFO *infoPtr, INT nItem)
3207 /* FIXME: pass in the state */
3208 WORD wShift = HIWORD(GetKeyState(VK_SHIFT));
3209 WORD wCtrl = HIWORD(GetKeyState(VK_CONTROL));
3210 BOOL bResult = FALSE;
3212 TRACE("nItem=%d, wShift=%d, wCtrl=%d\n", nItem, wShift, wCtrl);
3213 if ((nItem >= 0) && (nItem < infoPtr->nItemCount))
3215 if (infoPtr->dwStyle & LVS_SINGLESEL)
3218 LISTVIEW_SetSelection(infoPtr, nItem);
3225 LISTVIEW_SetGroupSelection(infoPtr, nItem);
3230 lvItem.state = ~LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED);
3231 lvItem.stateMask = LVIS_SELECTED;
3232 LISTVIEW_SetItemState(infoPtr, nItem, &lvItem);
3234 if (lvItem.state & LVIS_SELECTED)
3235 infoPtr->nSelectionMark = nItem;
3237 bResult = LISTVIEW_SetItemFocus(infoPtr, nItem);
3242 LISTVIEW_SetSelection(infoPtr, nItem);
3245 LISTVIEW_EnsureVisible(infoPtr, nItem, FALSE);
3248 UpdateWindow(infoPtr->hwndSelf); /* update client area */
3252 static BOOL LISTVIEW_GetItemAtPt(const LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem, POINT pt)
3254 LVHITTESTINFO lvHitTestInfo;
3256 ZeroMemory(&lvHitTestInfo, sizeof(lvHitTestInfo));
3257 lvHitTestInfo.pt.x = pt.x;
3258 lvHitTestInfo.pt.y = pt.y;
3260 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
3262 lpLVItem->mask = LVIF_PARAM;
3263 lpLVItem->iItem = lvHitTestInfo.iItem;
3264 lpLVItem->iSubItem = 0;
3266 return LISTVIEW_GetItemT(infoPtr, lpLVItem, TRUE);
3271 * Called when the mouse is being actively tracked and has hovered for a specified
3275 * [I] infoPtr : valid pointer to the listview structure
3276 * [I] fwKeys : key indicator
3277 * [I] x,y : mouse position
3280 * 0 if the message was processed, non-zero if there was an error
3283 * LVS_EX_TRACKSELECT: An item is automatically selected when the cursor remains
3284 * over the item for a certain period of time.
3287 static LRESULT LISTVIEW_MouseHover(LISTVIEW_INFO *infoPtr, WORD fwKeys, INT x, INT y)
3289 if (infoPtr->dwLvExStyle & LVS_EX_TRACKSELECT)
3297 if (LISTVIEW_GetItemAtPt(infoPtr, &item, pt))
3298 LISTVIEW_SetSelection(infoPtr, item.iItem);
3306 * Called whenever WM_MOUSEMOVE is received.
3309 * [I] infoPtr : valid pointer to the listview structure
3310 * [I] fwKeys : key indicator
3311 * [I] x,y : mouse position
3314 * 0 if the message is processed, non-zero if there was an error
3316 static LRESULT LISTVIEW_MouseMove(LISTVIEW_INFO *infoPtr, WORD fwKeys, INT x, INT y)
3318 TRACKMOUSEEVENT trackinfo;
3320 if (!(fwKeys & MK_LBUTTON))
3321 infoPtr->bLButtonDown = FALSE;
3323 if (infoPtr->bLButtonDown && DragDetect(infoPtr->hwndSelf, infoPtr->ptClickPos))
3325 LVHITTESTINFO lvHitTestInfo;
3328 lvHitTestInfo.pt = infoPtr->ptClickPos;
3329 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, TRUE);
3331 ZeroMemory(&nmlv, sizeof(nmlv));
3332 nmlv.iItem = lvHitTestInfo.iItem;
3333 nmlv.ptAction = infoPtr->ptClickPos;
3335 notify_listview(infoPtr, LVN_BEGINDRAG, &nmlv);
3340 infoPtr->bLButtonDown = FALSE;
3342 /* see if we are supposed to be tracking mouse hovering */
3343 if(infoPtr->dwLvExStyle & LVS_EX_TRACKSELECT) {
3344 /* fill in the trackinfo struct */
3345 trackinfo.cbSize = sizeof(TRACKMOUSEEVENT);
3346 trackinfo.dwFlags = TME_QUERY;
3347 trackinfo.hwndTrack = infoPtr->hwndSelf;
3348 trackinfo.dwHoverTime = infoPtr->dwHoverTime;
3350 /* see if we are already tracking this hwnd */
3351 _TrackMouseEvent(&trackinfo);
3353 if(!(trackinfo.dwFlags & TME_HOVER)) {
3354 trackinfo.dwFlags = TME_HOVER;
3356 /* call TRACKMOUSEEVENT so we receive WM_MOUSEHOVER messages */
3357 _TrackMouseEvent(&trackinfo);
3366 * Tests whether the item is assignable to a list with style lStyle
3368 static inline BOOL is_assignable_item(const LVITEMW *lpLVItem, LONG lStyle)
3370 if ( (lpLVItem->mask & LVIF_TEXT) &&
3371 (lpLVItem->pszText == LPSTR_TEXTCALLBACKW) &&
3372 (lStyle & (LVS_SORTASCENDING | LVS_SORTDESCENDING)) ) return FALSE;
3380 * Helper for LISTVIEW_SetItemT *only*: sets item attributes.
3383 * [I] infoPtr : valid pointer to the listview structure
3384 * [I] lpLVItem : valid pointer to new item attributes
3385 * [I] isNew : the item being set is being inserted
3386 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
3387 * [O] bChanged : will be set to TRUE if the item really changed
3393 static BOOL set_main_item(LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem, BOOL isNew, BOOL isW, BOOL *bChanged)
3395 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
3403 assert(lpLVItem->iItem >= 0 && lpLVItem->iItem < infoPtr->nItemCount);
3405 if (lpLVItem->mask == 0) return TRUE;
3407 if (infoPtr->dwStyle & LVS_OWNERDATA)
3409 /* a virtual listview only stores selection and focus */
3410 if (lpLVItem->mask & ~LVIF_STATE)
3416 HDPA hdpaSubItems = (HDPA)DPA_GetPtr(infoPtr->hdpaItems, lpLVItem->iItem);
3417 lpItem = (ITEM_INFO *)DPA_GetPtr(hdpaSubItems, 0);
3421 /* we need to get the lParam and state of the item */
3422 item.iItem = lpLVItem->iItem;
3423 item.iSubItem = lpLVItem->iSubItem;
3424 item.mask = LVIF_STATE | LVIF_PARAM;
3425 item.stateMask = ~0;
3428 if (!isNew && !LISTVIEW_GetItemW(infoPtr, &item)) return FALSE;
3430 TRACE("oldState=%x, newState=%x\n", item.state, lpLVItem->state);
3431 /* determine what fields will change */
3432 if ((lpLVItem->mask & LVIF_STATE) && ((item.state ^ lpLVItem->state) & lpLVItem->stateMask & ~infoPtr->uCallbackMask))
3433 uChanged |= LVIF_STATE;
3435 if ((lpLVItem->mask & LVIF_IMAGE) && (lpItem->hdr.iImage != lpLVItem->iImage))
3436 uChanged |= LVIF_IMAGE;
3438 if ((lpLVItem->mask & LVIF_PARAM) && (lpItem->lParam != lpLVItem->lParam))
3439 uChanged |= LVIF_PARAM;
3441 if ((lpLVItem->mask & LVIF_INDENT) && (lpItem->iIndent != lpLVItem->iIndent))
3442 uChanged |= LVIF_INDENT;
3444 if ((lpLVItem->mask & LVIF_TEXT) && textcmpWT(lpItem->hdr.pszText, lpLVItem->pszText, isW))
3445 uChanged |= LVIF_TEXT;
3447 TRACE("uChanged=0x%x\n", uChanged);
3448 if (!uChanged) return TRUE;
3451 ZeroMemory(&nmlv, sizeof(NMLISTVIEW));
3452 nmlv.iItem = lpLVItem->iItem;
3453 nmlv.uNewState = (item.state & ~lpLVItem->stateMask) | (lpLVItem->state & lpLVItem->stateMask);
3454 nmlv.uOldState = item.state;
3455 nmlv.uChanged = uChanged;
3456 nmlv.lParam = item.lParam;
3458 /* send LVN_ITEMCHANGING notification, if the item is not being inserted */
3459 /* and we are _NOT_ virtual (LVS_OWNERDATA), and change notifications */
3461 if(lpItem && !isNew && infoPtr->bDoChangeNotify)
3463 HWND hwndSelf = infoPtr->hwndSelf;
3465 if (notify_listview(infoPtr, LVN_ITEMCHANGING, &nmlv))
3467 if (!IsWindow(hwndSelf))
3471 /* copy information */
3472 if (lpLVItem->mask & LVIF_TEXT)
3473 textsetptrT(&lpItem->hdr.pszText, lpLVItem->pszText, isW);
3475 if (lpLVItem->mask & LVIF_IMAGE)
3476 lpItem->hdr.iImage = lpLVItem->iImage;
3478 if (lpLVItem->mask & LVIF_PARAM)
3479 lpItem->lParam = lpLVItem->lParam;
3481 if (lpLVItem->mask & LVIF_INDENT)
3482 lpItem->iIndent = lpLVItem->iIndent;
3484 if (uChanged & LVIF_STATE)
3486 if (lpItem && (lpLVItem->stateMask & ~infoPtr->uCallbackMask & ~(LVIS_FOCUSED | LVIS_SELECTED)))
3488 lpItem->state &= ~lpLVItem->stateMask;
3489 lpItem->state |= (lpLVItem->state & lpLVItem->stateMask);
3491 if (lpLVItem->state & lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_SELECTED)
3493 if (infoPtr->dwStyle & LVS_SINGLESEL) LISTVIEW_DeselectAllSkipItem(infoPtr, lpLVItem->iItem);
3494 ranges_additem(infoPtr->selectionRanges, lpLVItem->iItem);
3496 else if (lpLVItem->stateMask & LVIS_SELECTED)
3497 ranges_delitem(infoPtr->selectionRanges, lpLVItem->iItem);
3499 /* if we are asked to change focus, and we manage it, do it */
3500 if (lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_FOCUSED)
3502 if (lpLVItem->state & LVIS_FOCUSED)
3504 LISTVIEW_SetItemFocus(infoPtr, -1);
3505 infoPtr->nFocusedItem = lpLVItem->iItem;
3506 LISTVIEW_EnsureVisible(infoPtr, lpLVItem->iItem, uView == LVS_LIST);
3508 else if (infoPtr->nFocusedItem == lpLVItem->iItem)
3509 infoPtr->nFocusedItem = -1;
3513 /* if we're inserting the item, we're done */
3514 if (isNew) return TRUE;
3516 /* send LVN_ITEMCHANGED notification */
3517 if (lpLVItem->mask & LVIF_PARAM) nmlv.lParam = lpLVItem->lParam;
3518 if (infoPtr->bDoChangeNotify) notify_listview(infoPtr, LVN_ITEMCHANGED, &nmlv);
3525 * Helper for LISTVIEW_{Set,Insert}ItemT *only*: sets subitem attributes.
3528 * [I] infoPtr : valid pointer to the listview structure
3529 * [I] lpLVItem : valid pointer to new subitem attributes
3530 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
3531 * [O] bChanged : will be set to TRUE if the item really changed
3537 static BOOL set_sub_item(const LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem, BOOL isW, BOOL *bChanged)
3540 SUBITEM_INFO *lpSubItem;
3542 /* we do not support subitems for virtual listviews */
3543 if (infoPtr->dwStyle & LVS_OWNERDATA) return FALSE;
3545 /* set subitem only if column is present */
3546 if (lpLVItem->iSubItem >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
3548 /* First do some sanity checks */
3549 /* The LVIF_STATE flag is valid for subitems, but does not appear to be
3550 particularly useful. We currently do not actually do anything with
3551 the flag on subitems.
3553 if (lpLVItem->mask & ~(LVIF_TEXT | LVIF_IMAGE | LVIF_STATE)) return FALSE;
3554 if (!(lpLVItem->mask & (LVIF_TEXT | LVIF_IMAGE | LVIF_STATE))) return TRUE;
3556 /* get the subitem structure, and create it if not there */
3557 hdpaSubItems = (HDPA)DPA_GetPtr(infoPtr->hdpaItems, lpLVItem->iItem);
3558 assert (hdpaSubItems);
3560 lpSubItem = LISTVIEW_GetSubItemPtr(hdpaSubItems, lpLVItem->iSubItem);
3563 SUBITEM_INFO *tmpSubItem;
3566 lpSubItem = Alloc(sizeof(SUBITEM_INFO));
3567 if (!lpSubItem) return FALSE;
3568 /* we could binary search here, if need be...*/
3569 for (i = 1; i < DPA_GetPtrCount(hdpaSubItems); i++)
3571 tmpSubItem = (SUBITEM_INFO *)DPA_GetPtr(hdpaSubItems, i);
3572 if (tmpSubItem->iSubItem > lpLVItem->iSubItem) break;
3574 if (DPA_InsertPtr(hdpaSubItems, i, lpSubItem) == -1)
3579 lpSubItem->iSubItem = lpLVItem->iSubItem;
3580 lpSubItem->hdr.iImage = I_IMAGECALLBACK;
3584 if (lpLVItem->mask & LVIF_IMAGE)
3585 if (lpSubItem->hdr.iImage != lpLVItem->iImage)
3587 lpSubItem->hdr.iImage = lpLVItem->iImage;
3591 if (lpLVItem->mask & LVIF_TEXT)
3592 if (lpSubItem->hdr.pszText != lpLVItem->pszText)
3594 textsetptrT(&lpSubItem->hdr.pszText, lpLVItem->pszText, isW);
3603 * Sets item attributes.
3606 * [I] infoPtr : valid pointer to the listview structure
3607 * [I] lpLVItem : new item attributes
3608 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
3614 static BOOL LISTVIEW_SetItemT(LISTVIEW_INFO *infoPtr, LVITEMW *lpLVItem, BOOL isW)
3616 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
3617 HWND hwndSelf = infoPtr->hwndSelf;
3618 LPWSTR pszText = NULL;
3619 BOOL bResult, bChanged = FALSE;
3621 TRACE("(lpLVItem=%s, isW=%d)\n", debuglvitem_t(lpLVItem, isW), isW);
3623 if (!lpLVItem || lpLVItem->iItem < 0 || lpLVItem->iItem >= infoPtr->nItemCount)
3626 /* For efficiency, we transform the lpLVItem->pszText to Unicode here */
3627 if ((lpLVItem->mask & LVIF_TEXT) && is_textW(lpLVItem->pszText))
3629 pszText = lpLVItem->pszText;
3630 lpLVItem->pszText = textdupTtoW(lpLVItem->pszText, isW);
3633 /* actually set the fields */
3634 if (!is_assignable_item(lpLVItem, infoPtr->dwStyle)) return FALSE;
3636 if (lpLVItem->iSubItem)
3637 bResult = set_sub_item(infoPtr, lpLVItem, TRUE, &bChanged);
3639 bResult = set_main_item(infoPtr, lpLVItem, FALSE, TRUE, &bChanged);
3640 if (!IsWindow(hwndSelf))
3643 /* redraw item, if necessary */
3644 if (bChanged && !infoPtr->bIsDrawing)
3646 /* this little optimization eliminates some nasty flicker */
3647 if ( uView == LVS_REPORT && !(infoPtr->dwStyle & LVS_OWNERDRAWFIXED) &&
3648 !(infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) &&
3649 lpLVItem->iSubItem > 0 && lpLVItem->iSubItem <= DPA_GetPtrCount(infoPtr->hdpaColumns) )
3650 LISTVIEW_InvalidateSubItem(infoPtr, lpLVItem->iItem, lpLVItem->iSubItem);
3652 LISTVIEW_InvalidateItem(infoPtr, lpLVItem->iItem);
3657 textfreeT(lpLVItem->pszText, isW);
3658 lpLVItem->pszText = pszText;
3666 * Retrieves the index of the item at coordinate (0, 0) of the client area.
3669 * [I] infoPtr : valid pointer to the listview structure
3674 static INT LISTVIEW_GetTopIndex(const LISTVIEW_INFO *infoPtr)
3676 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
3678 SCROLLINFO scrollInfo;
3680 scrollInfo.cbSize = sizeof(SCROLLINFO);
3681 scrollInfo.fMask = SIF_POS;
3683 if (uView == LVS_LIST)
3685 if (GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo))
3686 nItem = scrollInfo.nPos * LISTVIEW_GetCountPerColumn(infoPtr);
3688 else if (uView == LVS_REPORT)
3690 if (GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
3691 nItem = scrollInfo.nPos;
3695 if (GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
3696 nItem = LISTVIEW_GetCountPerRow(infoPtr) * (scrollInfo.nPos / infoPtr->nItemHeight);
3699 TRACE("nItem=%d\n", nItem);
3707 * Erases the background of the given rectangle
3710 * [I] infoPtr : valid pointer to the listview structure
3711 * [I] hdc : device context handle
3712 * [I] lprcBox : clipping rectangle
3718 static inline BOOL LISTVIEW_FillBkgnd(const LISTVIEW_INFO *infoPtr, HDC hdc, const RECT *lprcBox)
3720 if (!infoPtr->hBkBrush) return FALSE;
3722 TRACE("(hdc=%p, lprcBox=%s, hBkBrush=%p)\n", hdc, wine_dbgstr_rect(lprcBox), infoPtr->hBkBrush);
3724 return FillRect(hdc, lprcBox, infoPtr->hBkBrush);
3732 * [I] infoPtr : valid pointer to the listview structure
3733 * [I] hdc : device context handle
3734 * [I] nItem : item index
3735 * [I] nSubItem : subitem index
3736 * [I] pos : item position in client coordinates
3737 * [I] cdmode : custom draw mode
3743 static BOOL LISTVIEW_DrawItem(LISTVIEW_INFO *infoPtr, HDC hdc, INT nItem, INT nSubItem, POINT pos, DWORD cdmode)
3745 UINT uFormat, uView = infoPtr->dwStyle & LVS_TYPEMASK;
3746 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
3747 static WCHAR szCallback[] = { '(', 'c', 'a', 'l', 'l', 'b', 'a', 'c', 'k', ')', 0 };
3748 DWORD cdsubitemmode = CDRF_DODEFAULT;
3750 RECT rcSelect, rcBox, rcIcon, rcLabel, rcStateIcon;
3751 NMLVCUSTOMDRAW nmlvcd;
3756 TRACE("(hdc=%p, nItem=%d, nSubItem=%d, pos=%s)\n", hdc, nItem, nSubItem, wine_dbgstr_point(&pos));
3758 /* get information needed for drawing the item */
3759 lvItem.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_PARAM;
3760 if (nSubItem == 0) lvItem.mask |= LVIF_STATE;
3761 if (uView == LVS_REPORT) lvItem.mask |= LVIF_INDENT;
3762 lvItem.stateMask = LVIS_SELECTED | LVIS_FOCUSED | LVIS_STATEIMAGEMASK;
3763 lvItem.iItem = nItem;
3764 lvItem.iSubItem = nSubItem;
3767 lvItem.cchTextMax = DISP_TEXT_SIZE;
3768 lvItem.pszText = szDispText;
3769 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return FALSE;
3770 if (nSubItem > 0 && (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT))
3771 lvItem.state = LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED);
3772 if (lvItem.pszText == LPSTR_TEXTCALLBACKW) lvItem.pszText = szCallback;
3773 TRACE(" lvItem=%s\n", debuglvitem_t(&lvItem, TRUE));
3775 /* now check if we need to update the focus rectangle */
3776 lprcFocus = infoPtr->bFocus && (lvItem.state & LVIS_FOCUSED) ? &infoPtr->rcFocus : 0;
3778 if (!lprcFocus) lvItem.state &= ~LVIS_FOCUSED;
3779 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, &rcBox, &rcSelect, &rcIcon, &rcStateIcon, &rcLabel);
3780 OffsetRect(&rcBox, pos.x, pos.y);
3781 OffsetRect(&rcSelect, pos.x, pos.y);
3782 OffsetRect(&rcIcon, pos.x, pos.y);
3783 OffsetRect(&rcStateIcon, pos.x, pos.y);
3784 OffsetRect(&rcLabel, pos.x, pos.y);
3785 TRACE(" rcBox=%s, rcSelect=%s, rcIcon=%s. rcLabel=%s\n",
3786 wine_dbgstr_rect(&rcBox), wine_dbgstr_rect(&rcSelect),
3787 wine_dbgstr_rect(&rcIcon), wine_dbgstr_rect(&rcLabel));
3789 /* fill in the custom draw structure */
3790 customdraw_fill(&nmlvcd, infoPtr, hdc, &rcBox, &lvItem);
3792 hOldFont = GetCurrentObject(hdc, OBJ_FONT);
3793 if (nSubItem > 0) cdmode = infoPtr->cditemmode;
3794 if (cdmode & CDRF_SKIPDEFAULT) goto postpaint;
3795 if (cdmode & CDRF_NOTIFYITEMDRAW)
3796 cdsubitemmode = notify_customdraw(infoPtr, CDDS_PREPAINT, &nmlvcd);
3797 if (nSubItem == 0) infoPtr->cditemmode = cdsubitemmode;
3798 if (cdsubitemmode & CDRF_SKIPDEFAULT) goto postpaint;
3799 /* we have to send a CDDS_SUBITEM customdraw explicitly for subitem 0 */
3800 if (nSubItem == 0 && cdsubitemmode == CDRF_NOTIFYITEMDRAW)
3802 cdsubitemmode = notify_customdraw(infoPtr, CDDS_SUBITEM | CDDS_ITEMPREPAINT, &nmlvcd);
3803 if (cdsubitemmode & CDRF_SKIPDEFAULT) goto postpaint;
3805 if (nSubItem == 0 || (cdmode & CDRF_NOTIFYITEMDRAW))
3806 prepaint_setup(infoPtr, hdc, &nmlvcd, FALSE);
3807 else if ((infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) == FALSE)
3808 prepaint_setup(infoPtr, hdc, &nmlvcd, TRUE);
3810 /* in full row select, subitems, will just use main item's colors */
3811 if (nSubItem && uView == LVS_REPORT && (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT))
3812 nmlvcd.clrTextBk = CLR_NONE;
3815 if (infoPtr->himlState && STATEIMAGEINDEX(lvItem.state) && (nSubItem == 0))
3817 UINT uStateImage = STATEIMAGEINDEX(lvItem.state);
3820 TRACE("uStateImage=%d\n", uStateImage);
3821 ImageList_Draw(infoPtr->himlState, uStateImage - 1, hdc,
3822 rcStateIcon.left, rcStateIcon.top, ILD_NORMAL);
3827 himl = (uView == LVS_ICON ? infoPtr->himlNormal : infoPtr->himlSmall);
3828 if (himl && lvItem.iImage >= 0 && !IsRectEmpty(&rcIcon))
3830 TRACE("iImage=%d\n", lvItem.iImage);
3831 ImageList_DrawEx(himl, lvItem.iImage, hdc, rcIcon.left, rcIcon.top,
3832 rcIcon.right - rcIcon.left, rcIcon.bottom - rcIcon.top, infoPtr->clrBk, CLR_DEFAULT,
3833 (lvItem.state & LVIS_SELECTED) && (infoPtr->bFocus) ? ILD_SELECTED : ILD_NORMAL);
3836 /* Don't bother painting item being edited */
3837 if (infoPtr->hwndEdit && nItem == infoPtr->nEditLabelItem && nSubItem == 0) goto postpaint;
3839 /* FIXME: temporary hack */
3840 rcSelect.left = rcLabel.left;
3842 /* draw the selection background, if we're drawing the main item */
3845 /* in icon mode, the label rect is really what we want to draw the
3847 if (uView == LVS_ICON)
3850 if (uView == LVS_REPORT && (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT))
3851 rcSelect.right = rcBox.right;
3853 if (nmlvcd.clrTextBk != CLR_NONE)
3854 ExtTextOutW(hdc, rcSelect.left, rcSelect.top, ETO_OPAQUE, &rcSelect, 0, 0, 0);
3855 if(lprcFocus) *lprcFocus = rcSelect;
3858 /* figure out the text drawing flags */
3859 uFormat = (uView == LVS_ICON ? (lprcFocus ? LV_FL_DT_FLAGS : LV_ML_DT_FLAGS) : LV_SL_DT_FLAGS);
3860 if (uView == LVS_ICON)
3861 uFormat = (lprcFocus ? LV_FL_DT_FLAGS : LV_ML_DT_FLAGS);
3864 switch (LISTVIEW_GetColumnInfo(infoPtr, nSubItem)->fmt & LVCFMT_JUSTIFYMASK)
3866 case LVCFMT_RIGHT: uFormat |= DT_RIGHT; break;
3867 case LVCFMT_CENTER: uFormat |= DT_CENTER; break;
3868 default: uFormat |= DT_LEFT;
3871 if (!(uFormat & (DT_RIGHT | DT_CENTER)))
3873 if (himl && lvItem.iImage >= 0 && !IsRectEmpty(&rcIcon)) rcLabel.left += IMAGE_PADDING;
3874 else rcLabel.left += LABEL_HOR_PADDING;
3876 else if (uFormat & DT_RIGHT) rcLabel.right -= LABEL_HOR_PADDING;
3878 /* for GRIDLINES reduce the bottom so the text formats correctly */
3879 if (uView == LVS_REPORT && infoPtr->dwLvExStyle & LVS_EX_GRIDLINES)
3882 DrawTextW(hdc, lvItem.pszText, -1, &rcLabel, uFormat);
3885 if (cdsubitemmode & CDRF_NOTIFYPOSTPAINT)
3886 notify_postpaint(infoPtr, &nmlvcd);
3887 if (cdsubitemmode & CDRF_NEWFONT)
3888 SelectObject(hdc, hOldFont);
3894 * Draws listview items when in owner draw mode.
3897 * [I] infoPtr : valid pointer to the listview structure
3898 * [I] hdc : device context handle
3903 static void LISTVIEW_RefreshOwnerDraw(const LISTVIEW_INFO *infoPtr, ITERATOR *i, HDC hdc, DWORD cdmode)
3905 UINT uID = (UINT)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
3906 DWORD cditemmode = CDRF_DODEFAULT;
3907 NMLVCUSTOMDRAW nmlvcd;
3908 POINT Origin, Position;
3914 ZeroMemory(&dis, sizeof(dis));
3916 /* Get scroll info once before loop */
3917 LISTVIEW_GetOrigin(infoPtr, &Origin);
3919 /* iterate through the invalidated rows */
3920 while(iterator_next(i))
3922 item.iItem = i->nItem;
3924 item.mask = LVIF_PARAM | LVIF_STATE;
3925 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
3926 if (!LISTVIEW_GetItemW(infoPtr, &item)) continue;
3928 dis.CtlType = ODT_LISTVIEW;
3930 dis.itemID = item.iItem;
3931 dis.itemAction = ODA_DRAWENTIRE;
3933 if (item.state & LVIS_SELECTED) dis.itemState |= ODS_SELECTED;
3934 if (infoPtr->bFocus && (item.state & LVIS_FOCUSED)) dis.itemState |= ODS_FOCUS;
3935 dis.hwndItem = infoPtr->hwndSelf;
3937 LISTVIEW_GetItemOrigin(infoPtr, dis.itemID, &Position);
3938 dis.rcItem.left = Position.x + Origin.x;
3939 dis.rcItem.right = dis.rcItem.left + infoPtr->nItemWidth;
3940 dis.rcItem.top = Position.y + Origin.y;
3941 dis.rcItem.bottom = dis.rcItem.top + infoPtr->nItemHeight;
3942 dis.itemData = item.lParam;
3944 TRACE("item=%s, rcItem=%s\n", debuglvitem_t(&item, TRUE), wine_dbgstr_rect(&dis.rcItem));
3947 * Even if we do not send the CDRF_NOTIFYITEMDRAW we need to fill the nmlvcd
3948 * structure for the rest. of the paint cycle
3950 customdraw_fill(&nmlvcd, infoPtr, hdc, &dis.rcItem, &item);
3951 if (cdmode & CDRF_NOTIFYITEMDRAW)
3952 cditemmode = notify_customdraw(infoPtr, CDDS_PREPAINT, &nmlvcd);
3954 if (!(cditemmode & CDRF_SKIPDEFAULT))
3956 prepaint_setup (infoPtr, hdc, &nmlvcd, FALSE);
3957 SendMessageW(infoPtr->hwndNotify, WM_DRAWITEM, dis.CtlID, (LPARAM)&dis);
3960 if (cditemmode & CDRF_NOTIFYPOSTPAINT)
3961 notify_postpaint(infoPtr, &nmlvcd);
3967 * Draws listview items when in report display mode.
3970 * [I] infoPtr : valid pointer to the listview structure
3971 * [I] hdc : device context handle
3972 * [I] cdmode : custom draw mode
3977 static void LISTVIEW_RefreshReport(LISTVIEW_INFO *infoPtr, ITERATOR *i, HDC hdc, DWORD cdmode)
3980 RECT rcClip, rcItem;
3981 POINT Origin, Position;
3987 /* figure out what to draw */
3988 rgntype = GetClipBox(hdc, &rcClip);
3989 if (rgntype == NULLREGION) return;
3991 /* Get scroll info once before loop */
3992 LISTVIEW_GetOrigin(infoPtr, &Origin);
3994 /* narrow down the columns we need to paint */
3995 for(colRange.lower = 0; colRange.lower < DPA_GetPtrCount(infoPtr->hdpaColumns); colRange.lower++)
3997 LISTVIEW_GetHeaderRect(infoPtr, colRange.lower, &rcItem);
3998 if (rcItem.right + Origin.x >= rcClip.left) break;
4000 for(colRange.upper = DPA_GetPtrCount(infoPtr->hdpaColumns); colRange.upper > 0; colRange.upper--)
4002 LISTVIEW_GetHeaderRect(infoPtr, colRange.upper - 1, &rcItem);
4003 if (rcItem.left + Origin.x < rcClip.right) break;
4005 iterator_rangeitems(&j, colRange);
4007 /* in full row select, we _have_ to draw the main item */
4008 if (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT)
4011 /* iterate through the invalidated rows */
4012 while(iterator_next(i))
4014 /* iterate through the invalidated columns */
4015 while(iterator_next(&j))
4017 LISTVIEW_GetItemOrigin(infoPtr, i->nItem, &Position);
4018 Position.x += Origin.x;
4019 Position.y += Origin.y;
4021 if (rgntype == COMPLEXREGION && !((infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) && j.nItem == 0))
4023 LISTVIEW_GetHeaderRect(infoPtr, j.nItem, &rcItem);
4025 rcItem.bottom = infoPtr->nItemHeight;
4026 OffsetRect(&rcItem, Position.x, Position.y);
4027 if (!RectVisible(hdc, &rcItem)) continue;
4030 LISTVIEW_DrawItem(infoPtr, hdc, i->nItem, j.nItem, Position, cdmode);
4033 iterator_destroy(&j);
4038 * Draws the gridlines if necessary when in report display mode.
4041 * [I] infoPtr : valid pointer to the listview structure
4042 * [I] hdc : device context handle
4047 static void LISTVIEW_RefreshReportGrid(LISTVIEW_INFO *infoPtr, HDC hdc)
4052 RECT rcClip, rcItem;
4059 /* figure out what to draw */
4060 rgntype = GetClipBox(hdc, &rcClip);
4061 if (rgntype == NULLREGION) return;
4063 /* Get scroll info once before loop */
4064 LISTVIEW_GetOrigin(infoPtr, &Origin);
4066 /* narrow down the columns we need to paint */
4067 for(colRange.lower = 0; colRange.lower < DPA_GetPtrCount(infoPtr->hdpaColumns); colRange.lower++)
4069 LISTVIEW_GetHeaderRect(infoPtr, colRange.lower, &rcItem);
4070 if (rcItem.right + Origin.x >= rcClip.left) break;
4072 for(colRange.upper = DPA_GetPtrCount(infoPtr->hdpaColumns); colRange.upper > 0; colRange.upper--)
4074 LISTVIEW_GetHeaderRect(infoPtr, colRange.upper - 1, &rcItem);
4075 if (rcItem.left + Origin.x < rcClip.right) break;
4077 iterator_rangeitems(&j, colRange);
4079 if ((hPen = CreatePen( PS_SOLID, 1, comctl32_color.clr3dFace )))
4081 hOldPen = SelectObject ( hdc, hPen );
4083 /* draw the vertical lines for the columns */
4084 iterator_rangeitems(&j, colRange);
4085 while(iterator_next(&j))
4087 LISTVIEW_GetHeaderRect(infoPtr, j.nItem, &rcItem);
4088 if (rcItem.left == 0) continue; /* skip first column */
4089 rcItem.left += Origin.x;
4090 rcItem.right += Origin.x;
4091 rcItem.top = infoPtr->rcList.top;
4092 rcItem.bottom = infoPtr->rcList.bottom;
4093 TRACE("vert col=%d, rcItem=%s\n", j.nItem, wine_dbgstr_rect(&rcItem));
4094 MoveToEx (hdc, rcItem.left, rcItem.top, NULL);
4095 LineTo (hdc, rcItem.left, rcItem.bottom);
4097 iterator_destroy(&j);
4099 /* draw the horizontial lines for the rows */
4100 itemheight = LISTVIEW_CalculateItemHeight(infoPtr);
4101 rcItem.left = infoPtr->rcList.left + Origin.x;
4102 rcItem.right = infoPtr->rcList.right + Origin.x;
4103 rcItem.bottom = rcItem.top = Origin.y - 1;
4104 MoveToEx(hdc, rcItem.left, rcItem.top, NULL);
4105 LineTo(hdc, rcItem.right, rcItem.top);
4106 for(y=itemheight-1+Origin.y; y<=infoPtr->rcList.bottom; y+=itemheight)
4108 rcItem.bottom = rcItem.top = y;
4109 TRACE("horz rcItem=%s\n", wine_dbgstr_rect(&rcItem));
4110 MoveToEx (hdc, rcItem.left, rcItem.top, NULL);
4111 LineTo (hdc, rcItem.right, rcItem.top);
4114 SelectObject( hdc, hOldPen );
4115 DeleteObject( hPen );
4121 * Draws listview items when in list display mode.
4124 * [I] infoPtr : valid pointer to the listview structure
4125 * [I] hdc : device context handle
4126 * [I] cdmode : custom draw mode
4131 static void LISTVIEW_RefreshList(LISTVIEW_INFO *infoPtr, ITERATOR *i, HDC hdc, DWORD cdmode)
4133 POINT Origin, Position;
4135 /* Get scroll info once before loop */
4136 LISTVIEW_GetOrigin(infoPtr, &Origin);
4138 while(iterator_prev(i))
4140 LISTVIEW_GetItemOrigin(infoPtr, i->nItem, &Position);
4141 Position.x += Origin.x;
4142 Position.y += Origin.y;
4144 LISTVIEW_DrawItem(infoPtr, hdc, i->nItem, 0, Position, cdmode);
4151 * Draws listview items.
4154 * [I] infoPtr : valid pointer to the listview structure
4155 * [I] hdc : device context handle
4156 * [I] prcErase : rect to be erased before refresh (may be NULL)
4161 static void LISTVIEW_Refresh(LISTVIEW_INFO *infoPtr, HDC hdc, const RECT *prcErase)
4163 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
4164 COLORREF oldTextColor = 0, oldBkColor = 0, oldClrTextBk, oldClrText;
4165 NMLVCUSTOMDRAW nmlvcd;
4172 HBITMAP hbmp = NULL;
4174 LISTVIEW_DUMP(infoPtr);
4176 if (infoPtr->dwLvExStyle & LVS_EX_DOUBLEBUFFER) {
4177 TRACE("double buffering\n");
4179 hdc = CreateCompatibleDC(hdcOrig);
4181 ERR("Failed to create DC for backbuffer\n");
4184 hbmp = CreateCompatibleBitmap(hdcOrig, infoPtr->rcList.right,
4185 infoPtr->rcList.bottom);
4187 ERR("Failed to create bitmap for backbuffer\n");
4192 SelectObject(hdc, hbmp);
4193 SelectObject(hdc, infoPtr->hFont);
4195 /* Save dc values we're gonna trash while drawing
4196 * FIXME: Should be done in LISTVIEW_DrawItem() */
4197 hOldFont = SelectObject(hdc, infoPtr->hFont);
4198 oldBkMode = GetBkMode(hdc);
4199 oldBkColor = GetBkColor(hdc);
4200 oldTextColor = GetTextColor(hdc);
4203 infoPtr->bIsDrawing = TRUE;
4206 LISTVIEW_FillBkgnd(infoPtr, hdc, prcErase);
4207 } else if (infoPtr->dwLvExStyle & LVS_EX_DOUBLEBUFFER) {
4208 /* If no erasing was done (usually because RedrawWindow was called
4209 * with RDW_INVALIDATE only) we need to copy the old contents into
4210 * the backbuffer before continuing. */
4211 BitBlt(hdc, infoPtr->rcList.left, infoPtr->rcList.top,
4212 infoPtr->rcList.right - infoPtr->rcList.left,
4213 infoPtr->rcList.bottom - infoPtr->rcList.top,
4214 hdcOrig, infoPtr->rcList.left, infoPtr->rcList.top, SRCCOPY);
4217 /* FIXME: Shouldn't need to do this */
4218 oldClrTextBk = infoPtr->clrTextBk;
4219 oldClrText = infoPtr->clrText;
4221 infoPtr->cditemmode = CDRF_DODEFAULT;
4223 GetClientRect(infoPtr->hwndSelf, &rcClient);
4224 customdraw_fill(&nmlvcd, infoPtr, hdc, &rcClient, 0);
4225 cdmode = notify_customdraw(infoPtr, CDDS_PREPAINT, &nmlvcd);
4226 if (cdmode & CDRF_SKIPDEFAULT) goto enddraw;
4227 prepaint_setup(infoPtr, hdc, &nmlvcd, FALSE);
4229 /* Use these colors to draw the items */
4230 infoPtr->clrTextBk = nmlvcd.clrTextBk;
4231 infoPtr->clrText = nmlvcd.clrText;
4233 /* nothing to draw */
4234 if(infoPtr->nItemCount == 0) goto enddraw;
4236 /* figure out what we need to draw */
4237 iterator_visibleitems(&i, infoPtr, hdc);
4239 /* send cache hint notification */
4240 if (infoPtr->dwStyle & LVS_OWNERDATA)
4242 RANGE range = iterator_range(&i);
4245 ZeroMemory(&nmlv, sizeof(NMLVCACHEHINT));
4246 nmlv.iFrom = range.lower;
4247 nmlv.iTo = range.upper - 1;
4248 notify_hdr(infoPtr, LVN_ODCACHEHINT, &nmlv.hdr);
4251 if ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (uView == LVS_REPORT))
4252 LISTVIEW_RefreshOwnerDraw(infoPtr, &i, hdc, cdmode);
4255 if (uView == LVS_REPORT)
4256 LISTVIEW_RefreshReport(infoPtr, &i, hdc, cdmode);
4257 else /* LVS_LIST, LVS_ICON or LVS_SMALLICON */
4258 LISTVIEW_RefreshList(infoPtr, &i, hdc, cdmode);
4260 /* if we have a focus rect, draw it */
4261 if (infoPtr->bFocus)
4262 DrawFocusRect(hdc, &infoPtr->rcFocus);
4264 iterator_destroy(&i);
4267 /* For LVS_EX_GRIDLINES go and draw lines */
4268 /* This includes the case where there were *no* items */
4269 if ((infoPtr->dwStyle & LVS_TYPEMASK) == LVS_REPORT &&
4270 infoPtr->dwLvExStyle & LVS_EX_GRIDLINES)
4271 LISTVIEW_RefreshReportGrid(infoPtr, hdc);
4273 if (cdmode & CDRF_NOTIFYPOSTPAINT)
4274 notify_postpaint(infoPtr, &nmlvcd);
4276 infoPtr->clrTextBk = oldClrTextBk;
4277 infoPtr->clrText = oldClrText;
4280 BitBlt(hdcOrig, infoPtr->rcList.left, infoPtr->rcList.top,
4281 infoPtr->rcList.right - infoPtr->rcList.left,
4282 infoPtr->rcList.bottom - infoPtr->rcList.top,
4283 hdc, infoPtr->rcList.left, infoPtr->rcList.top, SRCCOPY);
4288 SelectObject(hdc, hOldFont);
4289 SetBkMode(hdc, oldBkMode);
4290 SetBkColor(hdc, oldBkColor);
4291 SetTextColor(hdc, oldTextColor);
4294 infoPtr->bIsDrawing = FALSE;
4300 * Calculates the approximate width and height of a given number of items.
4303 * [I] infoPtr : valid pointer to the listview structure
4304 * [I] nItemCount : number of items
4305 * [I] wWidth : width
4306 * [I] wHeight : height
4309 * Returns a DWORD. The width in the low word and the height in high word.
4311 static DWORD LISTVIEW_ApproximateViewRect(const LISTVIEW_INFO *infoPtr, INT nItemCount,
4312 WORD wWidth, WORD wHeight)
4314 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
4315 INT nItemCountPerColumn = 1;
4316 INT nColumnCount = 0;
4317 DWORD dwViewRect = 0;
4319 if (nItemCount == -1)
4320 nItemCount = infoPtr->nItemCount;
4322 if (uView == LVS_LIST)
4324 if (wHeight == 0xFFFF)
4326 /* use current height */
4327 wHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
4330 if (wHeight < infoPtr->nItemHeight)
4331 wHeight = infoPtr->nItemHeight;
4335 if (infoPtr->nItemHeight > 0)
4337 nItemCountPerColumn = wHeight / infoPtr->nItemHeight;
4338 if (nItemCountPerColumn == 0)
4339 nItemCountPerColumn = 1;
4341 if (nItemCount % nItemCountPerColumn != 0)
4342 nColumnCount = nItemCount / nItemCountPerColumn;
4344 nColumnCount = nItemCount / nItemCountPerColumn + 1;
4348 /* Microsoft padding magic */
4349 wHeight = nItemCountPerColumn * infoPtr->nItemHeight + 2;
4350 wWidth = nColumnCount * infoPtr->nItemWidth + 2;
4352 dwViewRect = MAKELONG(wWidth, wHeight);
4354 else if (uView == LVS_REPORT)
4358 if (infoPtr->nItemCount > 0)
4360 LISTVIEW_GetItemBox(infoPtr, 0, &rcBox);
4361 wWidth = rcBox.right - rcBox.left;
4362 wHeight = (rcBox.bottom - rcBox.top) * nItemCount;
4366 /* use current height and width */
4367 if (wHeight == 0xffff)
4368 wHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
4369 if (wWidth == 0xffff)
4370 wWidth = infoPtr->rcList.right - infoPtr->rcList.left;
4373 dwViewRect = MAKELONG(wWidth, wHeight);
4375 else if (uView == LVS_SMALLICON)
4376 FIXME("uView == LVS_SMALLICON: not implemented\n");
4377 else if (uView == LVS_ICON)
4378 FIXME("uView == LVS_ICON: not implemented\n");
4386 * Create a drag image list for the specified item.
4389 * [I] infoPtr : valid pointer to the listview structure
4390 * [I] iItem : index of item
4391 * [O] lppt : Upperr-left corner of the image
4394 * Returns a handle to the image list if successful, NULL otherwise.
4396 static HIMAGELIST LISTVIEW_CreateDragImage(LISTVIEW_INFO *infoPtr, INT iItem, LPPOINT lppt)
4402 HBITMAP hbmp, hOldbmp;
4403 HIMAGELIST dragList = 0;
4404 TRACE("iItem=%d Count=%d\n", iItem, infoPtr->nItemCount);
4406 if (iItem < 0 || iItem >= infoPtr->nItemCount)
4409 rcItem.left = LVIR_BOUNDS;
4410 if (!LISTVIEW_GetItemRect(infoPtr, iItem, &rcItem))
4413 lppt->x = rcItem.left;
4414 lppt->y = rcItem.top;
4416 size.cx = rcItem.right - rcItem.left;
4417 size.cy = rcItem.bottom - rcItem.top;
4419 hdcOrig = GetDC(infoPtr->hwndSelf);
4420 hdc = CreateCompatibleDC(hdcOrig);
4421 hbmp = CreateCompatibleBitmap(hdcOrig, size.cx, size.cy);
4422 hOldbmp = SelectObject(hdc, hbmp);
4424 rcItem.left = rcItem.top = 0;
4425 rcItem.right = size.cx;
4426 rcItem.bottom = size.cy;
4427 FillRect(hdc, &rcItem, infoPtr->hBkBrush);
4430 if (LISTVIEW_DrawItem(infoPtr, hdc, iItem, 0, pos, infoPtr->cditemmode))
4432 dragList = ImageList_Create(size.cx, size.cy, ILC_COLOR, 10, 10);
4433 SelectObject(hdc, hOldbmp);
4434 ImageList_Add(dragList, hbmp, 0);
4437 SelectObject(hdc, hOldbmp);
4441 ReleaseDC(infoPtr->hwndSelf, hdcOrig);
4443 TRACE("ret=%p\n", dragList);
4451 * Removes all listview items and subitems.
4454 * [I] infoPtr : valid pointer to the listview structure
4460 static BOOL LISTVIEW_DeleteAllItems(LISTVIEW_INFO *infoPtr, BOOL destroy)
4463 HDPA hdpaSubItems = NULL;
4470 /* we do it directly, to avoid notifications */
4471 ranges_clear(infoPtr->selectionRanges);
4472 infoPtr->nSelectionMark = -1;
4473 infoPtr->nFocusedItem = -1;
4474 SetRectEmpty(&infoPtr->rcFocus);
4475 /* But we are supposed to leave nHotItem as is! */
4478 /* send LVN_DELETEALLITEMS notification */
4479 ZeroMemory(&nmlv, sizeof(NMLISTVIEW));
4481 bSuppress = notify_listview(infoPtr, LVN_DELETEALLITEMS, &nmlv);
4483 for (i = infoPtr->nItemCount - 1; i >= 0; i--)
4485 /* send LVN_DELETEITEM notification, if not suppressed */
4486 if (!bSuppress) notify_deleteitem(infoPtr, i);
4487 if (!(infoPtr->dwStyle & LVS_OWNERDATA))
4489 hdpaSubItems = (HDPA)DPA_GetPtr(infoPtr->hdpaItems, i);
4490 for (j = 0; j < DPA_GetPtrCount(hdpaSubItems); j++)
4492 hdrItem = (ITEMHDR *)DPA_GetPtr(hdpaSubItems, j);
4493 if (is_textW(hdrItem->pszText)) Free(hdrItem->pszText);
4496 DPA_Destroy(hdpaSubItems);
4497 DPA_DeletePtr(infoPtr->hdpaItems, i);
4499 DPA_DeletePtr(infoPtr->hdpaPosX, i);
4500 DPA_DeletePtr(infoPtr->hdpaPosY, i);
4501 infoPtr->nItemCount --;
4506 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
4507 LISTVIEW_UpdateScroll(infoPtr);
4509 LISTVIEW_InvalidateList(infoPtr);
4516 * Scrolls, and updates the columns, when a column is changing width.
4519 * [I] infoPtr : valid pointer to the listview structure
4520 * [I] nColumn : column to scroll
4521 * [I] dx : amount of scroll, in pixels
4526 static void LISTVIEW_ScrollColumns(LISTVIEW_INFO *infoPtr, INT nColumn, INT dx)
4528 COLUMN_INFO *lpColumnInfo;
4533 if (nColumn < 0 || DPA_GetPtrCount(infoPtr->hdpaColumns) < 1) return;
4534 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, min(nColumn, DPA_GetPtrCount(infoPtr->hdpaColumns) - 1));
4535 rcCol = lpColumnInfo->rcHeader;
4536 if (nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns))
4537 rcCol.left = rcCol.right;
4539 /* adjust the other columns */
4540 for (nCol = nColumn; nCol < DPA_GetPtrCount(infoPtr->hdpaColumns); nCol++)
4542 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, nCol);
4543 lpColumnInfo->rcHeader.left += dx;
4544 lpColumnInfo->rcHeader.right += dx;
4547 /* do not update screen if not in report mode */
4548 if (!is_redrawing(infoPtr) || (infoPtr->dwStyle & LVS_TYPEMASK) != LVS_REPORT) return;
4550 /* if we have a focus, we must first erase the focus rect */
4551 if (infoPtr->bFocus) LISTVIEW_ShowFocusRect(infoPtr, FALSE);
4553 /* Need to reset the item width when inserting a new column */
4554 infoPtr->nItemWidth += dx;
4556 LISTVIEW_UpdateScroll(infoPtr);
4557 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
4559 /* scroll to cover the deleted column, and invalidate for redraw */
4560 rcOld = infoPtr->rcList;
4561 rcOld.left = ptOrigin.x + rcCol.left + dx;
4562 ScrollWindowEx(infoPtr->hwndSelf, dx, 0, &rcOld, &rcOld, 0, 0, SW_ERASE | SW_INVALIDATE);
4564 /* we can restore focus now */
4565 if (infoPtr->bFocus) LISTVIEW_ShowFocusRect(infoPtr, TRUE);
4570 * Removes a column from the listview control.
4573 * [I] infoPtr : valid pointer to the listview structure
4574 * [I] nColumn : column index
4580 static BOOL LISTVIEW_DeleteColumn(LISTVIEW_INFO *infoPtr, INT nColumn)
4584 TRACE("nColumn=%d\n", nColumn);
4586 if (nColumn < 0 || DPA_GetPtrCount(infoPtr->hdpaColumns) == 0
4587 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
4589 /* While the MSDN specifically says that column zero should not be deleted,
4590 what actually happens is that the column itself is deleted but no items or subitems
4594 LISTVIEW_GetHeaderRect(infoPtr, nColumn, &rcCol);
4596 if (!Header_DeleteItem(infoPtr->hwndHeader, nColumn))
4599 Free(DPA_GetPtr(infoPtr->hdpaColumns, nColumn));
4600 DPA_DeletePtr(infoPtr->hdpaColumns, nColumn);
4602 if (!(infoPtr->dwStyle & LVS_OWNERDATA) && nColumn)
4604 SUBITEM_INFO *lpSubItem, *lpDelItem;
4606 INT nItem, nSubItem, i;
4608 for (nItem = 0; nItem < infoPtr->nItemCount; nItem++)
4610 hdpaSubItems = (HDPA)DPA_GetPtr(infoPtr->hdpaItems, nItem);
4613 for (i = 1; i < DPA_GetPtrCount(hdpaSubItems); i++)
4615 lpSubItem = (SUBITEM_INFO *)DPA_GetPtr(hdpaSubItems, i);
4616 if (lpSubItem->iSubItem == nColumn)
4619 lpDelItem = lpSubItem;
4621 else if (lpSubItem->iSubItem > nColumn)
4623 lpSubItem->iSubItem--;
4627 /* if we found our subitem, zapp it */
4631 if (is_textW(lpDelItem->hdr.pszText))
4632 Free(lpDelItem->hdr.pszText);
4637 /* free dpa memory */
4638 DPA_DeletePtr(hdpaSubItems, nSubItem);
4643 /* update the other column info */
4644 LISTVIEW_UpdateItemSize(infoPtr);
4645 if(DPA_GetPtrCount(infoPtr->hdpaColumns) == 0)
4646 LISTVIEW_InvalidateList(infoPtr);
4648 LISTVIEW_ScrollColumns(infoPtr, nColumn, -(rcCol.right - rcCol.left));
4655 * Invalidates the listview after an item's insertion or deletion.
4658 * [I] infoPtr : valid pointer to the listview structure
4659 * [I] nItem : item index
4660 * [I] dir : -1 if deleting, 1 if inserting
4665 static void LISTVIEW_ScrollOnInsert(LISTVIEW_INFO *infoPtr, INT nItem, INT dir)
4667 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
4668 INT nPerCol, nItemCol, nItemRow;
4672 /* if we don't refresh, what's the point of scrolling? */
4673 if (!is_redrawing(infoPtr)) return;
4675 assert (abs(dir) == 1);
4677 /* arrange icons if autoarrange is on */
4678 if (is_autoarrange(infoPtr))
4680 BOOL arrange = TRUE;
4681 if (dir < 0 && nItem >= infoPtr->nItemCount) arrange = FALSE;
4682 if (dir > 0 && nItem == infoPtr->nItemCount - 1) arrange = FALSE;
4683 if (arrange) LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
4686 /* scrollbars need updating */
4687 LISTVIEW_UpdateScroll(infoPtr);
4689 /* figure out the item's position */
4690 if (uView == LVS_REPORT)
4691 nPerCol = infoPtr->nItemCount + 1;
4692 else if (uView == LVS_LIST)
4693 nPerCol = LISTVIEW_GetCountPerColumn(infoPtr);
4694 else /* LVS_ICON, or LVS_SMALLICON */
4697 nItemCol = nItem / nPerCol;
4698 nItemRow = nItem % nPerCol;
4699 LISTVIEW_GetOrigin(infoPtr, &Origin);
4701 /* move the items below up a slot */
4702 rcScroll.left = nItemCol * infoPtr->nItemWidth;
4703 rcScroll.top = nItemRow * infoPtr->nItemHeight;
4704 rcScroll.right = rcScroll.left + infoPtr->nItemWidth;
4705 rcScroll.bottom = nPerCol * infoPtr->nItemHeight;
4706 OffsetRect(&rcScroll, Origin.x, Origin.y);
4707 TRACE("rcScroll=%s, dx=%d\n", wine_dbgstr_rect(&rcScroll), dir * infoPtr->nItemHeight);
4708 if (IntersectRect(&rcScroll, &rcScroll, &infoPtr->rcList))
4710 TRACE("Scrolling rcScroll=%s, rcList=%s\n", wine_dbgstr_rect(&rcScroll), wine_dbgstr_rect(&infoPtr->rcList));
4711 ScrollWindowEx(infoPtr->hwndSelf, 0, dir * infoPtr->nItemHeight,
4712 &rcScroll, &rcScroll, 0, 0, SW_ERASE | SW_INVALIDATE);
4715 /* report has only that column, so we're done */
4716 if (uView == LVS_REPORT) return;
4718 /* now for LISTs, we have to deal with the columns to the right */
4719 rcScroll.left = (nItemCol + 1) * infoPtr->nItemWidth;
4721 rcScroll.right = (infoPtr->nItemCount / nPerCol + 1) * infoPtr->nItemWidth;
4722 rcScroll.bottom = nPerCol * infoPtr->nItemHeight;
4723 OffsetRect(&rcScroll, Origin.x, Origin.y);
4724 if (IntersectRect(&rcScroll, &rcScroll, &infoPtr->rcList))
4725 ScrollWindowEx(infoPtr->hwndSelf, 0, dir * infoPtr->nItemHeight,
4726 &rcScroll, &rcScroll, 0, 0, SW_ERASE | SW_INVALIDATE);
4731 * Removes an item from the listview control.
4734 * [I] infoPtr : valid pointer to the listview structure
4735 * [I] nItem : item index
4741 static BOOL LISTVIEW_DeleteItem(LISTVIEW_INFO *infoPtr, INT nItem)
4744 const UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
4745 const BOOL is_icon = (uView == LVS_SMALLICON || uView == LVS_ICON);
4747 TRACE("(nItem=%d)\n", nItem);
4749 if (nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
4751 /* remove selection, and focus */
4753 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
4754 LISTVIEW_SetItemState(infoPtr, nItem, &item);
4756 /* send LVN_DELETEITEM notification. */
4757 if (!notify_deleteitem(infoPtr, nItem)) return FALSE;
4759 /* we need to do this here, because we'll be deleting stuff */
4761 LISTVIEW_InvalidateItem(infoPtr, nItem);
4763 if (!(infoPtr->dwStyle & LVS_OWNERDATA))
4769 hdpaSubItems = (HDPA)DPA_DeletePtr(infoPtr->hdpaItems, nItem);
4770 for (i = 0; i < DPA_GetPtrCount(hdpaSubItems); i++)
4772 hdrItem = (ITEMHDR *)DPA_GetPtr(hdpaSubItems, i);
4773 if (is_textW(hdrItem->pszText)) Free(hdrItem->pszText);
4776 DPA_Destroy(hdpaSubItems);
4781 DPA_DeletePtr(infoPtr->hdpaPosX, nItem);
4782 DPA_DeletePtr(infoPtr->hdpaPosY, nItem);
4785 infoPtr->nItemCount--;
4786 LISTVIEW_ShiftIndices(infoPtr, nItem, -1);
4788 /* now is the invalidation fun */
4790 LISTVIEW_ScrollOnInsert(infoPtr, nItem, -1);
4797 * Callback implementation for editlabel control
4800 * [I] infoPtr : valid pointer to the listview structure
4801 * [I] pszText : modified text
4802 * [I] isW : TRUE if psxText is Unicode, FALSE if it's ANSI
4808 static BOOL LISTVIEW_EndEditLabelT(LISTVIEW_INFO *infoPtr, LPWSTR pszText, BOOL isW)
4810 HWND hwndSelf = infoPtr->hwndSelf;
4811 NMLVDISPINFOW dispInfo;
4813 TRACE("(pszText=%s, isW=%d)\n", debugtext_t(pszText, isW), isW);
4815 ZeroMemory(&dispInfo, sizeof(dispInfo));
4816 dispInfo.item.mask = LVIF_PARAM | LVIF_STATE;
4817 dispInfo.item.iItem = infoPtr->nEditLabelItem;
4818 dispInfo.item.iSubItem = 0;
4819 dispInfo.item.stateMask = ~0;
4820 if (!LISTVIEW_GetItemW(infoPtr, &dispInfo.item)) return FALSE;
4821 /* add the text from the edit in */
4822 dispInfo.item.mask |= LVIF_TEXT;
4823 dispInfo.item.pszText = pszText;
4824 dispInfo.item.cchTextMax = textlenT(pszText, isW);
4826 /* Do we need to update the Item Text */
4827 if (!notify_dispinfoT(infoPtr, LVN_ENDLABELEDITW, &dispInfo, isW)) return FALSE;
4828 if (!IsWindow(hwndSelf))
4830 if (!pszText) return TRUE;
4832 if (!(infoPtr->dwStyle & LVS_OWNERDATA))
4834 HDPA hdpaSubItems = (HDPA)DPA_GetPtr(infoPtr->hdpaItems, infoPtr->nEditLabelItem);
4835 ITEM_INFO* lpItem = (ITEM_INFO *)DPA_GetPtr(hdpaSubItems, 0);
4836 if (lpItem && lpItem->hdr.pszText == LPSTR_TEXTCALLBACKW)
4838 LISTVIEW_InvalidateItem(infoPtr, infoPtr->nEditLabelItem);
4843 ZeroMemory(&dispInfo, sizeof(dispInfo));
4844 dispInfo.item.mask = LVIF_TEXT;
4845 dispInfo.item.iItem = infoPtr->nEditLabelItem;
4846 dispInfo.item.iSubItem = 0;
4847 dispInfo.item.pszText = pszText;
4848 dispInfo.item.cchTextMax = textlenT(pszText, isW);
4849 return LISTVIEW_SetItemT(infoPtr, &dispInfo.item, isW);
4854 * Begin in place editing of specified list view item
4857 * [I] infoPtr : valid pointer to the listview structure
4858 * [I] nItem : item index
4859 * [I] isW : TRUE if it's a Unicode req, FALSE if ASCII
4865 static HWND LISTVIEW_EditLabelT(LISTVIEW_INFO *infoPtr, INT nItem, BOOL isW)
4867 WCHAR szDispText[DISP_TEXT_SIZE] = { 0 };
4868 NMLVDISPINFOW dispInfo;
4870 HWND hwndSelf = infoPtr->hwndSelf;
4872 TRACE("(nItem=%d, isW=%d)\n", nItem, isW);
4874 if (~infoPtr->dwStyle & LVS_EDITLABELS) return 0;
4875 if (nItem < 0 || nItem >= infoPtr->nItemCount) return 0;
4877 infoPtr->nEditLabelItem = nItem;
4879 /* Is the EditBox still there, if so remove it */
4880 if(infoPtr->hwndEdit != 0)
4882 SetFocus(infoPtr->hwndSelf);
4883 infoPtr->hwndEdit = 0;
4886 LISTVIEW_SetSelection(infoPtr, nItem);
4887 LISTVIEW_SetItemFocus(infoPtr, nItem);
4888 LISTVIEW_InvalidateItem(infoPtr, nItem);
4890 rect.left = LVIR_LABEL;
4891 if (!LISTVIEW_GetItemRect(infoPtr, nItem, &rect)) return 0;
4893 ZeroMemory(&dispInfo, sizeof(dispInfo));
4894 dispInfo.item.mask = LVIF_PARAM | LVIF_STATE | LVIF_TEXT;
4895 dispInfo.item.iItem = nItem;
4896 dispInfo.item.iSubItem = 0;
4897 dispInfo.item.stateMask = ~0;
4898 dispInfo.item.pszText = szDispText;
4899 dispInfo.item.cchTextMax = DISP_TEXT_SIZE;
4900 if (!LISTVIEW_GetItemT(infoPtr, &dispInfo.item, isW)) return 0;
4902 infoPtr->hwndEdit = CreateEditLabelT(infoPtr, dispInfo.item.pszText, WS_VISIBLE,
4903 rect.left-2, rect.top-1, 0, rect.bottom - rect.top+2, isW);
4904 if (!infoPtr->hwndEdit) return 0;
4906 if (notify_dispinfoT(infoPtr, LVN_BEGINLABELEDITW, &dispInfo, isW))
4908 if (!IsWindow(hwndSelf))
4910 SendMessageW(infoPtr->hwndEdit, WM_CLOSE, 0, 0);
4911 infoPtr->hwndEdit = 0;
4915 ShowWindow(infoPtr->hwndEdit, SW_NORMAL);
4916 SetFocus(infoPtr->hwndEdit);
4917 SendMessageW(infoPtr->hwndEdit, EM_SETSEL, 0, -1);
4918 return infoPtr->hwndEdit;
4924 * Ensures the specified item is visible, scrolling into view if necessary.
4927 * [I] infoPtr : valid pointer to the listview structure
4928 * [I] nItem : item index
4929 * [I] bPartial : partially or entirely visible
4935 static BOOL LISTVIEW_EnsureVisible(LISTVIEW_INFO *infoPtr, INT nItem, BOOL bPartial)
4937 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
4938 INT nScrollPosHeight = 0;
4939 INT nScrollPosWidth = 0;
4940 INT nHorzAdjust = 0;
4941 INT nVertAdjust = 0;
4944 RECT rcItem, rcTemp;
4946 rcItem.left = LVIR_BOUNDS;
4947 if (!LISTVIEW_GetItemRect(infoPtr, nItem, &rcItem)) return FALSE;
4949 if (bPartial && IntersectRect(&rcTemp, &infoPtr->rcList, &rcItem)) return TRUE;
4951 if (rcItem.left < infoPtr->rcList.left || rcItem.right > infoPtr->rcList.right)
4953 /* scroll left/right, but in LVS_REPORT mode */
4954 if (uView == LVS_LIST)
4955 nScrollPosWidth = infoPtr->nItemWidth;
4956 else if ((uView == LVS_SMALLICON) || (uView == LVS_ICON))
4957 nScrollPosWidth = 1;
4959 if (rcItem.left < infoPtr->rcList.left)
4962 if (uView != LVS_REPORT) nHorzDiff = rcItem.left - infoPtr->rcList.left;
4967 if (uView != LVS_REPORT) nHorzDiff = rcItem.right - infoPtr->rcList.right;
4971 if (rcItem.top < infoPtr->rcList.top || rcItem.bottom > infoPtr->rcList.bottom)
4973 /* scroll up/down, but not in LVS_LIST mode */
4974 if (uView == LVS_REPORT)
4975 nScrollPosHeight = infoPtr->nItemHeight;
4976 else if ((uView == LVS_ICON) || (uView == LVS_SMALLICON))
4977 nScrollPosHeight = 1;
4979 if (rcItem.top < infoPtr->rcList.top)
4982 if (uView != LVS_LIST) nVertDiff = rcItem.top - infoPtr->rcList.top;
4987 if (uView != LVS_LIST) nVertDiff = rcItem.bottom - infoPtr->rcList.bottom;
4991 if (!nScrollPosWidth && !nScrollPosHeight) return TRUE;
4993 if (nScrollPosWidth)
4995 INT diff = nHorzDiff / nScrollPosWidth;
4996 if (nHorzDiff % nScrollPosWidth) diff += nHorzAdjust;
4997 LISTVIEW_HScroll(infoPtr, SB_INTERNAL, diff, 0);
5000 if (nScrollPosHeight)
5002 INT diff = nVertDiff / nScrollPosHeight;
5003 if (nVertDiff % nScrollPosHeight) diff += nVertAdjust;
5004 LISTVIEW_VScroll(infoPtr, SB_INTERNAL, diff, 0);
5012 * Searches for an item with specific characteristics.
5015 * [I] hwnd : window handle
5016 * [I] nStart : base item index
5017 * [I] lpFindInfo : item information to look for
5020 * SUCCESS : index of item
5023 static INT LISTVIEW_FindItemW(const LISTVIEW_INFO *infoPtr, INT nStart,
5024 const LVFINDINFOW *lpFindInfo)
5026 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
5027 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
5028 BOOL bWrap = FALSE, bNearest = FALSE;
5029 INT nItem = nStart + 1, nLast = infoPtr->nItemCount, nNearestItem = -1;
5030 ULONG xdist, ydist, dist, mindist = 0x7fffffff;
5031 POINT Position, Destination;
5034 if (!lpFindInfo || nItem < 0) return -1;
5037 if (lpFindInfo->flags & (LVFI_STRING | LVFI_PARTIAL))
5039 lvItem.mask |= LVIF_TEXT;
5040 lvItem.pszText = szDispText;
5041 lvItem.cchTextMax = DISP_TEXT_SIZE;
5044 if (lpFindInfo->flags & LVFI_WRAP)
5047 if ((lpFindInfo->flags & LVFI_NEARESTXY) &&
5048 (uView == LVS_ICON || uView ==LVS_SMALLICON))
5053 LISTVIEW_GetOrigin(infoPtr, &Origin);
5054 Destination.x = lpFindInfo->pt.x - Origin.x;
5055 Destination.y = lpFindInfo->pt.y - Origin.y;
5056 switch(lpFindInfo->vkDirection)
5058 case VK_DOWN: Destination.y += infoPtr->nItemHeight; break;
5059 case VK_UP: Destination.y -= infoPtr->nItemHeight; break;
5060 case VK_RIGHT: Destination.x += infoPtr->nItemWidth; break;
5061 case VK_LEFT: Destination.x -= infoPtr->nItemWidth; break;
5062 case VK_HOME: Destination.x = Destination.y = 0; break;
5063 case VK_NEXT: Destination.y += infoPtr->rcList.bottom - infoPtr->rcList.top; break;
5064 case VK_PRIOR: Destination.y -= infoPtr->rcList.bottom - infoPtr->rcList.top; break;
5066 LISTVIEW_GetAreaRect(infoPtr, &rcArea);
5067 Destination.x = rcArea.right;
5068 Destination.y = rcArea.bottom;
5070 default: ERR("Unknown vkDirection=%d\n", lpFindInfo->vkDirection);
5074 else Destination.x = Destination.y = 0;
5076 /* if LVFI_PARAM is specified, all other flags are ignored */
5077 if (lpFindInfo->flags & LVFI_PARAM)
5079 lvItem.mask |= LVIF_PARAM;
5081 lvItem.mask &= ~LVIF_TEXT;
5085 for (; nItem < nLast; nItem++)
5087 lvItem.iItem = nItem;
5088 lvItem.iSubItem = 0;
5089 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) continue;
5091 if (lvItem.mask & LVIF_PARAM)
5093 if (lpFindInfo->lParam == lvItem.lParam)
5099 if (lvItem.mask & LVIF_TEXT)
5101 if (lpFindInfo->flags & LVFI_PARTIAL)
5103 if (strstrW(lvItem.pszText, lpFindInfo->psz) == NULL) continue;
5107 if (lstrcmpW(lvItem.pszText, lpFindInfo->psz) != 0) continue;
5111 if (!bNearest) return nItem;
5113 /* This is very inefficient. To do a good job here,
5114 * we need a sorted array of (x,y) item positions */
5115 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
5117 /* compute the distance^2 to the destination */
5118 xdist = Destination.x - Position.x;
5119 ydist = Destination.y - Position.y;
5120 dist = xdist * xdist + ydist * ydist;
5122 /* remember the distance, and item if it's closer */
5126 nNearestItem = nItem;
5133 nLast = min(nStart + 1, infoPtr->nItemCount);
5138 return nNearestItem;
5143 * Searches for an item with specific characteristics.
5146 * [I] hwnd : window handle
5147 * [I] nStart : base item index
5148 * [I] lpFindInfo : item information to look for
5151 * SUCCESS : index of item
5154 static INT LISTVIEW_FindItemA(const LISTVIEW_INFO *infoPtr, INT nStart,
5155 const LVFINDINFOA *lpFindInfo)
5157 BOOL hasText = lpFindInfo->flags & (LVFI_STRING | LVFI_PARTIAL);
5162 memcpy(&fiw, lpFindInfo, sizeof(fiw));
5163 if (hasText) fiw.psz = strW = textdupTtoW((LPCWSTR)lpFindInfo->psz, FALSE);
5164 res = LISTVIEW_FindItemW(infoPtr, nStart, &fiw);
5165 textfreeT(strW, FALSE);
5171 * Retrieves the background image of the listview control.
5174 * [I] infoPtr : valid pointer to the listview structure
5175 * [O] lpBkImage : background image attributes
5181 /* static BOOL LISTVIEW_GetBkImage(const LISTVIEW_INFO *infoPtr, LPLVBKIMAGE lpBkImage) */
5183 /* FIXME (listview, "empty stub!\n"); */
5189 * Retrieves column attributes.
5192 * [I] infoPtr : valid pointer to the listview structure
5193 * [I] nColumn : column index
5194 * [IO] lpColumn : column information
5195 * [I] isW : if TRUE, then lpColumn is a LPLVCOLUMNW
5196 * otherwise it is in fact a LPLVCOLUMNA
5202 static BOOL LISTVIEW_GetColumnT(const LISTVIEW_INFO *infoPtr, INT nColumn, LPLVCOLUMNW lpColumn, BOOL isW)
5204 COLUMN_INFO *lpColumnInfo;
5207 if (!lpColumn || nColumn < 0 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
5208 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, nColumn);
5210 /* initialize memory */
5211 ZeroMemory(&hdi, sizeof(hdi));
5213 if (lpColumn->mask & LVCF_TEXT)
5215 hdi.mask |= HDI_TEXT;
5216 hdi.pszText = lpColumn->pszText;
5217 hdi.cchTextMax = lpColumn->cchTextMax;
5220 if (lpColumn->mask & LVCF_IMAGE)
5221 hdi.mask |= HDI_IMAGE;
5223 if (lpColumn->mask & LVCF_ORDER)
5224 hdi.mask |= HDI_ORDER;
5226 if (lpColumn->mask & LVCF_SUBITEM)
5227 hdi.mask |= HDI_LPARAM;
5229 if (!SendMessageW(infoPtr->hwndHeader, isW ? HDM_GETITEMW : HDM_GETITEMA, nColumn, (LPARAM)&hdi)) return FALSE;
5231 if (lpColumn->mask & LVCF_FMT)
5232 lpColumn->fmt = lpColumnInfo->fmt;
5234 if (lpColumn->mask & LVCF_WIDTH)
5235 lpColumn->cx = lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left;
5237 if (lpColumn->mask & LVCF_IMAGE)
5238 lpColumn->iImage = hdi.iImage;
5240 if (lpColumn->mask & LVCF_ORDER)
5241 lpColumn->iOrder = hdi.iOrder;
5243 if (lpColumn->mask & LVCF_SUBITEM)
5244 lpColumn->iSubItem = hdi.lParam;
5250 static BOOL LISTVIEW_GetColumnOrderArray(const LISTVIEW_INFO *infoPtr, INT iCount, LPINT lpiArray)
5257 /* FIXME: little hack */
5258 for (i = 0; i < iCount; i++)
5266 * Retrieves the column width.
5269 * [I] infoPtr : valid pointer to the listview structure
5270 * [I] int : column index
5273 * SUCCESS : column width
5276 static INT LISTVIEW_GetColumnWidth(const LISTVIEW_INFO *infoPtr, INT nColumn)
5278 INT nColumnWidth = 0;
5281 TRACE("nColumn=%d\n", nColumn);
5283 /* we have a 'column' in LIST and REPORT mode only */
5284 switch(infoPtr->dwStyle & LVS_TYPEMASK)
5287 nColumnWidth = infoPtr->nItemWidth;
5290 /* We are not using LISTVIEW_GetHeaderRect as this data is updated only after a HDM_ITEMCHANGED.
5291 * There is an application that subclasses the listview, calls LVM_GETCOLUMNWIDTH in the
5292 * HDM_ITEMCHANGED handler and goes into infinite recursion if it receives old data.
5294 * TODO: should we do the same in LVM_GETCOLUMN?
5296 hdItem.mask = HDI_WIDTH;
5297 if (!SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nColumn, (LPARAM)&hdItem))
5299 WARN("(%p): HDM_GETITEMW failed for item %d\n", infoPtr->hwndSelf, nColumn);
5302 nColumnWidth = hdItem.cxy;
5306 TRACE("nColumnWidth=%d\n", nColumnWidth);
5307 return nColumnWidth;
5312 * In list or report display mode, retrieves the number of items that can fit
5313 * vertically in the visible area. In icon or small icon display mode,
5314 * retrieves the total number of visible items.
5317 * [I] infoPtr : valid pointer to the listview structure
5320 * Number of fully visible items.
5322 static INT LISTVIEW_GetCountPerPage(const LISTVIEW_INFO *infoPtr)
5324 switch (infoPtr->dwStyle & LVS_TYPEMASK)
5328 return infoPtr->nItemCount;
5330 return LISTVIEW_GetCountPerColumn(infoPtr);
5332 return LISTVIEW_GetCountPerRow(infoPtr) * LISTVIEW_GetCountPerColumn(infoPtr);
5340 * Retrieves an image list handle.
5343 * [I] infoPtr : valid pointer to the listview structure
5344 * [I] nImageList : image list identifier
5347 * SUCCESS : image list handle
5350 static HIMAGELIST LISTVIEW_GetImageList(const LISTVIEW_INFO *infoPtr, INT nImageList)
5354 case LVSIL_NORMAL: return infoPtr->himlNormal;
5355 case LVSIL_SMALL: return infoPtr->himlSmall;
5356 case LVSIL_STATE: return infoPtr->himlState;
5361 /* LISTVIEW_GetISearchString */
5365 * Retrieves item attributes.
5368 * [I] hwnd : window handle
5369 * [IO] lpLVItem : item info
5370 * [I] isW : if TRUE, then lpLVItem is a LPLVITEMW,
5371 * if FALSE, then lpLVItem is a LPLVITEMA.
5374 * This is the internal 'GetItem' interface -- it tries to
5375 * be smart and avoid text copies, if possible, by modifying
5376 * lpLVItem->pszText to point to the text string. Please note
5377 * that this is not always possible (e.g. OWNERDATA), so on
5378 * entry you *must* supply valid values for pszText, and cchTextMax.
5379 * The only difference to the documented interface is that upon
5380 * return, you should use *only* the lpLVItem->pszText, rather than
5381 * the buffer pointer you provided on input. Most code already does
5382 * that, so it's not a problem.
5383 * For the two cases when the text must be copied (that is,
5384 * for LVM_GETITEM, and LVM_GETITEMTEXT), use LISTVIEW_GetItemExtT.
5390 static BOOL LISTVIEW_GetItemT(const LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem, BOOL isW)
5392 ITEMHDR callbackHdr = { LPSTR_TEXTCALLBACKW, I_IMAGECALLBACK };
5393 NMLVDISPINFOW dispInfo;
5399 TRACE("(lpLVItem=%s, isW=%d)\n", debuglvitem_t(lpLVItem, isW), isW);
5401 if (!lpLVItem || lpLVItem->iItem < 0 || lpLVItem->iItem >= infoPtr->nItemCount)
5404 if (lpLVItem->mask == 0) return TRUE;
5406 /* make a local copy */
5407 isubitem = lpLVItem->iSubItem;
5409 /* a quick optimization if all we're asked is the focus state
5410 * these queries are worth optimising since they are common,
5411 * and can be answered in constant time, without the heavy accesses */
5412 if ( (lpLVItem->mask == LVIF_STATE) && (lpLVItem->stateMask == LVIS_FOCUSED) &&
5413 !(infoPtr->uCallbackMask & LVIS_FOCUSED) )
5415 lpLVItem->state = 0;
5416 if (infoPtr->nFocusedItem == lpLVItem->iItem)
5417 lpLVItem->state |= LVIS_FOCUSED;
5421 ZeroMemory(&dispInfo, sizeof(dispInfo));
5423 /* if the app stores all the data, handle it separately */
5424 if (infoPtr->dwStyle & LVS_OWNERDATA)
5426 dispInfo.item.state = 0;
5428 /* apparently, we should not callback for lParam in LVS_OWNERDATA */
5429 if ((lpLVItem->mask & ~(LVIF_STATE | LVIF_PARAM)) || infoPtr->uCallbackMask)
5431 /* NOTE: copy only fields which we _know_ are initialized, some apps
5432 * depend on the uninitialized fields being 0 */
5433 dispInfo.item.mask = lpLVItem->mask & ~LVIF_PARAM;
5434 dispInfo.item.iItem = lpLVItem->iItem;
5435 dispInfo.item.iSubItem = isubitem;
5436 if (lpLVItem->mask & LVIF_TEXT)
5438 dispInfo.item.pszText = lpLVItem->pszText;
5439 dispInfo.item.cchTextMax = lpLVItem->cchTextMax;
5441 if (lpLVItem->mask & LVIF_STATE)
5442 dispInfo.item.stateMask = lpLVItem->stateMask & infoPtr->uCallbackMask;
5443 notify_dispinfoT(infoPtr, LVN_GETDISPINFOW, &dispInfo, isW);
5444 dispInfo.item.stateMask = lpLVItem->stateMask;
5445 if (lpLVItem->mask & (LVIF_GROUPID|LVIF_COLUMNS))
5447 /* full size structure expected - _WIN32IE >= 0x560 */
5448 *lpLVItem = dispInfo.item;
5450 else if (lpLVItem->mask & LVIF_INDENT)
5452 /* indent member expected - _WIN32IE >= 0x300 */
5453 memcpy(lpLVItem, &dispInfo.item, offsetof( LVITEMW, iGroupId ));
5457 /* minimal structure expected */
5458 memcpy(lpLVItem, &dispInfo.item, offsetof( LVITEMW, iIndent ));
5460 TRACE(" getdispinfo(1):lpLVItem=%s\n", debuglvitem_t(lpLVItem, isW));
5463 /* make sure lParam is zeroed out */
5464 if (lpLVItem->mask & LVIF_PARAM) lpLVItem->lParam = 0;
5466 /* we store only a little state, so if we're not asked, we're done */
5467 if (!(lpLVItem->mask & LVIF_STATE) || isubitem) return TRUE;
5469 /* if focus is handled by us, report it */
5470 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_FOCUSED )
5472 lpLVItem->state &= ~LVIS_FOCUSED;
5473 if (infoPtr->nFocusedItem == lpLVItem->iItem)
5474 lpLVItem->state |= LVIS_FOCUSED;
5477 /* and do the same for selection, if we handle it */
5478 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_SELECTED )
5480 lpLVItem->state &= ~LVIS_SELECTED;
5481 if (ranges_contain(infoPtr->selectionRanges, lpLVItem->iItem))
5482 lpLVItem->state |= LVIS_SELECTED;
5488 /* find the item and subitem structures before we proceed */
5489 hdpaSubItems = (HDPA)DPA_GetPtr(infoPtr->hdpaItems, lpLVItem->iItem);
5490 lpItem = (ITEM_INFO *)DPA_GetPtr(hdpaSubItems, 0);
5495 SUBITEM_INFO *lpSubItem = LISTVIEW_GetSubItemPtr(hdpaSubItems, isubitem);
5496 pItemHdr = lpSubItem ? &lpSubItem->hdr : &callbackHdr;
5499 WARN(" iSubItem invalid (%08x), ignored.\n", isubitem);
5504 pItemHdr = &lpItem->hdr;
5506 /* Do we need to query the state from the app? */
5507 if ((lpLVItem->mask & LVIF_STATE) && infoPtr->uCallbackMask && isubitem == 0)
5509 dispInfo.item.mask |= LVIF_STATE;
5510 dispInfo.item.stateMask = infoPtr->uCallbackMask;
5513 /* Do we need to enquire about the image? */
5514 if ((lpLVItem->mask & LVIF_IMAGE) && pItemHdr->iImage == I_IMAGECALLBACK &&
5515 (isubitem == 0 || (infoPtr->dwLvExStyle & LVS_EX_SUBITEMIMAGES)))
5517 dispInfo.item.mask |= LVIF_IMAGE;
5518 dispInfo.item.iImage = I_IMAGECALLBACK;
5521 /* Apps depend on calling back for text if it is NULL or LPSTR_TEXTCALLBACKW */
5522 if ((lpLVItem->mask & LVIF_TEXT) && !is_textW(pItemHdr->pszText))
5524 dispInfo.item.mask |= LVIF_TEXT;
5525 dispInfo.item.pszText = lpLVItem->pszText;
5526 dispInfo.item.cchTextMax = lpLVItem->cchTextMax;
5527 if (dispInfo.item.pszText && dispInfo.item.cchTextMax > 0)
5528 *dispInfo.item.pszText = '\0';
5531 /* If we don't have all the requested info, query the application */
5532 if (dispInfo.item.mask != 0)
5534 dispInfo.item.iItem = lpLVItem->iItem;
5535 dispInfo.item.iSubItem = lpLVItem->iSubItem; /* yes: the original subitem */
5536 dispInfo.item.lParam = lpItem->lParam;
5537 notify_dispinfoT(infoPtr, LVN_GETDISPINFOW, &dispInfo, isW);
5538 TRACE(" getdispinfo(2):item=%s\n", debuglvitem_t(&dispInfo.item, isW));
5541 /* we should not store values for subitems */
5542 if (isubitem) dispInfo.item.mask &= ~LVIF_DI_SETITEM;
5544 /* Now, handle the iImage field */
5545 if (dispInfo.item.mask & LVIF_IMAGE)
5547 lpLVItem->iImage = dispInfo.item.iImage;
5548 if ((dispInfo.item.mask & LVIF_DI_SETITEM) && pItemHdr->iImage == I_IMAGECALLBACK)
5549 pItemHdr->iImage = dispInfo.item.iImage;
5551 else if (lpLVItem->mask & LVIF_IMAGE)
5553 if(isubitem == 0 || (infoPtr->dwLvExStyle & LVS_EX_SUBITEMIMAGES))
5554 lpLVItem->iImage = pItemHdr->iImage;
5556 lpLVItem->iImage = 0;
5559 /* The pszText field */
5560 if (dispInfo.item.mask & LVIF_TEXT)
5562 if ((dispInfo.item.mask & LVIF_DI_SETITEM) && pItemHdr->pszText)
5563 textsetptrT(&pItemHdr->pszText, dispInfo.item.pszText, isW);
5565 lpLVItem->pszText = dispInfo.item.pszText;
5567 else if (lpLVItem->mask & LVIF_TEXT)
5569 if (isW) lpLVItem->pszText = pItemHdr->pszText;
5570 else textcpynT(lpLVItem->pszText, isW, pItemHdr->pszText, TRUE, lpLVItem->cchTextMax);
5573 /* Next is the lParam field */
5574 if (dispInfo.item.mask & LVIF_PARAM)
5576 lpLVItem->lParam = dispInfo.item.lParam;
5577 if ((dispInfo.item.mask & LVIF_DI_SETITEM))
5578 lpItem->lParam = dispInfo.item.lParam;
5580 else if (lpLVItem->mask & LVIF_PARAM)
5581 lpLVItem->lParam = lpItem->lParam;
5583 /* if this is a subitem, we're done */
5584 if (isubitem) return TRUE;
5586 /* ... the state field (this one is different due to uCallbackmask) */
5587 if (lpLVItem->mask & LVIF_STATE)
5589 lpLVItem->state = lpItem->state & lpLVItem->stateMask;
5590 if (dispInfo.item.mask & LVIF_STATE)
5592 lpLVItem->state &= ~dispInfo.item.stateMask;
5593 lpLVItem->state |= (dispInfo.item.state & dispInfo.item.stateMask);
5595 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_FOCUSED )
5597 lpLVItem->state &= ~LVIS_FOCUSED;
5598 if (infoPtr->nFocusedItem == lpLVItem->iItem)
5599 lpLVItem->state |= LVIS_FOCUSED;
5601 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_SELECTED )
5603 lpLVItem->state &= ~LVIS_SELECTED;
5604 if (ranges_contain(infoPtr->selectionRanges, lpLVItem->iItem))
5605 lpLVItem->state |= LVIS_SELECTED;
5609 /* and last, but not least, the indent field */
5610 if (lpLVItem->mask & LVIF_INDENT)
5611 lpLVItem->iIndent = lpItem->iIndent;
5618 * Retrieves item attributes.
5621 * [I] hwnd : window handle
5622 * [IO] lpLVItem : item info
5623 * [I] isW : if TRUE, then lpLVItem is a LPLVITEMW,
5624 * if FALSE, then lpLVItem is a LPLVITEMA.
5627 * This is the external 'GetItem' interface -- it properly copies
5628 * the text in the provided buffer.
5634 static BOOL LISTVIEW_GetItemExtT(const LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem, BOOL isW)
5639 if (!lpLVItem || lpLVItem->iItem < 0 || lpLVItem->iItem >= infoPtr->nItemCount)
5642 pszText = lpLVItem->pszText;
5643 bResult = LISTVIEW_GetItemT(infoPtr, lpLVItem, isW);
5644 if (bResult && lpLVItem->pszText != pszText)
5645 textcpynT(pszText, isW, lpLVItem->pszText, isW, lpLVItem->cchTextMax);
5646 lpLVItem->pszText = pszText;
5654 * Retrieves the position (upper-left) of the listview control item.
5655 * Note that for LVS_ICON style, the upper-left is that of the icon
5656 * and not the bounding box.
5659 * [I] infoPtr : valid pointer to the listview structure
5660 * [I] nItem : item index
5661 * [O] lpptPosition : coordinate information
5667 static BOOL LISTVIEW_GetItemPosition(const LISTVIEW_INFO *infoPtr, INT nItem, LPPOINT lpptPosition)
5669 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
5672 TRACE("(nItem=%d, lpptPosition=%p)\n", nItem, lpptPosition);
5674 if (!lpptPosition || nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
5676 LISTVIEW_GetOrigin(infoPtr, &Origin);
5677 LISTVIEW_GetItemOrigin(infoPtr, nItem, lpptPosition);
5679 if (uView == LVS_ICON)
5681 lpptPosition->x += (infoPtr->nItemWidth - infoPtr->iconSize.cx) / 2;
5682 lpptPosition->y += ICON_TOP_PADDING;
5684 lpptPosition->x += Origin.x;
5685 lpptPosition->y += Origin.y;
5687 TRACE (" lpptPosition=%s\n", wine_dbgstr_point(lpptPosition));
5694 * Retrieves the bounding rectangle for a listview control item.
5697 * [I] infoPtr : valid pointer to the listview structure
5698 * [I] nItem : item index
5699 * [IO] lprc : bounding rectangle coordinates
5700 * lprc->left specifies the portion of the item for which the bounding
5701 * rectangle will be retrieved.
5703 * LVIR_BOUNDS Returns the bounding rectangle of the entire item,
5704 * including the icon and label.
5707 * * Experiment shows that native control returns:
5708 * * width = min (48, length of text line)
5709 * * .left = position.x - (width - iconsize.cx)/2
5710 * * .right = .left + width
5711 * * height = #lines of text * ntmHeight + icon height + 8
5712 * * .top = position.y - 2
5713 * * .bottom = .top + height
5714 * * separation between items .y = itemSpacing.cy - height
5715 * * .x = itemSpacing.cx - width
5716 * LVIR_ICON Returns the bounding rectangle of the icon or small icon.
5719 * * Experiment shows that native control returns:
5720 * * width = iconSize.cx + 16
5721 * * .left = position.x - (width - iconsize.cx)/2
5722 * * .right = .left + width
5723 * * height = iconSize.cy + 4
5724 * * .top = position.y - 2
5725 * * .bottom = .top + height
5726 * * separation between items .y = itemSpacing.cy - height
5727 * * .x = itemSpacing.cx - width
5728 * LVIR_LABEL Returns the bounding rectangle of the item text.
5731 * * Experiment shows that native control returns:
5732 * * width = text length
5733 * * .left = position.x - width/2
5734 * * .right = .left + width
5735 * * height = ntmH * linecount + 2
5736 * * .top = position.y + iconSize.cy + 6
5737 * * .bottom = .top + height
5738 * * separation between items .y = itemSpacing.cy - height
5739 * * .x = itemSpacing.cx - width
5740 * LVIR_SELECTBOUNDS Returns the union of the LVIR_ICON and LVIR_LABEL
5741 * rectangles, but excludes columns in report view.
5748 * Note that the bounding rectangle of the label in the LVS_ICON view depends
5749 * upon whether the window has the focus currently and on whether the item
5750 * is the one with the focus. Ensure that the control's record of which
5751 * item has the focus agrees with the items' records.
5753 static BOOL LISTVIEW_GetItemRect(const LISTVIEW_INFO *infoPtr, INT nItem, LPRECT lprc)
5755 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
5756 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
5757 BOOL doLabel = TRUE, oversizedBox = FALSE;
5758 POINT Position, Origin;
5761 TRACE("(hwnd=%p, nItem=%d, lprc=%p)\n", infoPtr->hwndSelf, nItem, lprc);
5763 if (!lprc || nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
5765 LISTVIEW_GetOrigin(infoPtr, &Origin);
5766 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
5768 /* Be smart and try to figure out the minimum we have to do */
5769 if (lprc->left == LVIR_ICON) doLabel = FALSE;
5770 if (uView == LVS_REPORT && lprc->left == LVIR_BOUNDS) doLabel = FALSE;
5771 if (uView == LVS_ICON && lprc->left != LVIR_ICON &&
5772 infoPtr->bFocus && LISTVIEW_GetItemState(infoPtr, nItem, LVIS_FOCUSED))
5773 oversizedBox = TRUE;
5775 /* get what we need from the item before hand, so we make
5776 * only one request. This can speed up things, if data
5777 * is stored on the app side */
5779 if (uView == LVS_REPORT) lvItem.mask |= LVIF_INDENT;
5780 if (doLabel) lvItem.mask |= LVIF_TEXT;
5781 lvItem.iItem = nItem;
5782 lvItem.iSubItem = 0;
5783 lvItem.pszText = szDispText;
5784 lvItem.cchTextMax = DISP_TEXT_SIZE;
5785 if (lvItem.mask && !LISTVIEW_GetItemW(infoPtr, &lvItem)) return FALSE;
5786 /* we got the state already up, simulate it here, to avoid a reget */
5787 if (uView == LVS_ICON && (lprc->left != LVIR_ICON))
5789 lvItem.mask |= LVIF_STATE;
5790 lvItem.stateMask = LVIS_FOCUSED;
5791 lvItem.state = (oversizedBox ? LVIS_FOCUSED : 0);
5794 if (uView == LVS_REPORT && (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) && lprc->left == LVIR_SELECTBOUNDS)
5795 lprc->left = LVIR_BOUNDS;
5799 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, NULL, lprc, NULL, NULL);
5803 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, NULL, NULL, NULL, lprc);
5807 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, lprc, NULL, NULL, NULL, NULL);
5810 case LVIR_SELECTBOUNDS:
5811 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, lprc, NULL, NULL, NULL);
5815 WARN("Unknown value: %d\n", lprc->left);
5819 OffsetRect(lprc, Position.x + Origin.x, Position.y + Origin.y);
5821 TRACE(" rect=%s\n", wine_dbgstr_rect(lprc));
5828 * Retrieves the spacing between listview control items.
5831 * [I] infoPtr : valid pointer to the listview structure
5832 * [IO] lprc : rectangle to receive the output
5833 * on input, lprc->top = nSubItem
5834 * lprc->left = LVIR_ICON | LVIR_BOUNDS | LVIR_LABEL
5836 * NOTE: for subItem = 0, we should return the bounds of the _entire_ item,
5837 * not only those of the first column.
5838 * Fortunately, LISTVIEW_GetItemMetrics does the right thing.
5844 static BOOL LISTVIEW_GetSubItemRect(const LISTVIEW_INFO *infoPtr, INT nItem, LPRECT lprc)
5850 if (!lprc) return FALSE;
5852 nColumn = lprc->top;
5854 TRACE("(nItem=%d, nSubItem=%d)\n", nItem, lprc->top);
5855 /* On WinNT, a subitem of '0' calls LISTVIEW_GetItemRect */
5857 return LISTVIEW_GetItemRect(infoPtr, nItem, lprc);
5859 if ((infoPtr->dwStyle & LVS_TYPEMASK) != LVS_REPORT) return FALSE;
5861 if (!LISTVIEW_GetItemPosition(infoPtr, nItem, &Position)) return FALSE;
5863 if (nColumn < 0 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
5866 lvItem.iItem = nItem;
5867 lvItem.iSubItem = nColumn;
5869 if (lvItem.mask && !LISTVIEW_GetItemW(infoPtr, &lvItem)) return FALSE;
5873 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, NULL, lprc, NULL, NULL);
5878 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, lprc, NULL, NULL, NULL, NULL);
5882 ERR("Unknown bounds=%d\n", lprc->left);
5886 OffsetRect(lprc, Position.x, Position.y);
5893 * Retrieves the width of a label.
5896 * [I] infoPtr : valid pointer to the listview structure
5899 * SUCCESS : string width (in pixels)
5902 static INT LISTVIEW_GetLabelWidth(const LISTVIEW_INFO *infoPtr, INT nItem)
5904 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
5907 TRACE("(nItem=%d)\n", nItem);
5909 lvItem.mask = LVIF_TEXT;
5910 lvItem.iItem = nItem;
5911 lvItem.iSubItem = 0;
5912 lvItem.pszText = szDispText;
5913 lvItem.cchTextMax = DISP_TEXT_SIZE;
5914 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return 0;
5916 return LISTVIEW_GetStringWidthT(infoPtr, lvItem.pszText, TRUE);
5921 * Retrieves the spacing between listview control items.
5924 * [I] infoPtr : valid pointer to the listview structure
5925 * [I] bSmall : flag for small or large icon
5928 * Horizontal + vertical spacing
5930 static LONG LISTVIEW_GetItemSpacing(const LISTVIEW_INFO *infoPtr, BOOL bSmall)
5936 lResult = MAKELONG(infoPtr->iconSpacing.cx, infoPtr->iconSpacing.cy);
5940 if ((infoPtr->dwStyle & LVS_TYPEMASK) == LVS_ICON)
5941 lResult = MAKELONG(DEFAULT_COLUMN_WIDTH, GetSystemMetrics(SM_CXSMICON)+HEIGHT_PADDING);
5943 lResult = MAKELONG(infoPtr->nItemWidth, infoPtr->nItemHeight);
5950 * Retrieves the state of a listview control item.
5953 * [I] infoPtr : valid pointer to the listview structure
5954 * [I] nItem : item index
5955 * [I] uMask : state mask
5958 * State specified by the mask.
5960 static UINT LISTVIEW_GetItemState(const LISTVIEW_INFO *infoPtr, INT nItem, UINT uMask)
5964 if (nItem < 0 || nItem >= infoPtr->nItemCount) return 0;
5966 lvItem.iItem = nItem;
5967 lvItem.iSubItem = 0;
5968 lvItem.mask = LVIF_STATE;
5969 lvItem.stateMask = uMask;
5970 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return 0;
5972 return lvItem.state & uMask;
5977 * Retrieves the text of a listview control item or subitem.
5980 * [I] hwnd : window handle
5981 * [I] nItem : item index
5982 * [IO] lpLVItem : item information
5983 * [I] isW : TRUE if lpLVItem is Unicode
5986 * SUCCESS : string length
5989 static INT LISTVIEW_GetItemTextT(const LISTVIEW_INFO *infoPtr, INT nItem, LPLVITEMW lpLVItem, BOOL isW)
5991 if (!lpLVItem || nItem < 0 || nItem >= infoPtr->nItemCount) return 0;
5993 lpLVItem->mask = LVIF_TEXT;
5994 lpLVItem->iItem = nItem;
5995 if (!LISTVIEW_GetItemExtT(infoPtr, lpLVItem, isW)) return 0;
5997 return textlenT(lpLVItem->pszText, isW);
6002 * Searches for an item based on properties + relationships.
6005 * [I] infoPtr : valid pointer to the listview structure
6006 * [I] nItem : item index
6007 * [I] uFlags : relationship flag
6010 * SUCCESS : item index
6013 static INT LISTVIEW_GetNextItem(const LISTVIEW_INFO *infoPtr, INT nItem, UINT uFlags)
6015 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
6017 LVFINDINFOW lvFindInfo;
6018 INT nCountPerColumn;
6022 TRACE("nItem=%d, uFlags=%x, nItemCount=%d\n", nItem, uFlags, infoPtr->nItemCount);
6023 if (nItem < -1 || nItem >= infoPtr->nItemCount) return -1;
6025 ZeroMemory(&lvFindInfo, sizeof(lvFindInfo));
6027 if (uFlags & LVNI_CUT)
6030 if (uFlags & LVNI_DROPHILITED)
6031 uMask |= LVIS_DROPHILITED;
6033 if (uFlags & LVNI_FOCUSED)
6034 uMask |= LVIS_FOCUSED;
6036 if (uFlags & LVNI_SELECTED)
6037 uMask |= LVIS_SELECTED;
6039 /* if we're asked for the focused item, that's only one,
6040 * so it's worth optimizing */
6041 if (uFlags & LVNI_FOCUSED)
6043 if ((LISTVIEW_GetItemState(infoPtr, infoPtr->nFocusedItem, uMask) & uMask) != uMask) return -1;
6044 return (infoPtr->nFocusedItem == nItem) ? -1 : infoPtr->nFocusedItem;
6047 if (uFlags & LVNI_ABOVE)
6049 if ((uView == LVS_LIST) || (uView == LVS_REPORT))
6054 if ((ListView_GetItemState(infoPtr->hwndSelf, nItem, uMask) & uMask) == uMask)
6060 /* Special case for autoarrange - move 'til the top of a list */
6061 if (is_autoarrange(infoPtr))
6063 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
6064 while (nItem - nCountPerRow >= 0)
6066 nItem -= nCountPerRow;
6067 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
6072 lvFindInfo.flags = LVFI_NEARESTXY;
6073 lvFindInfo.vkDirection = VK_UP;
6074 SendMessageW( infoPtr->hwndSelf, LVM_GETITEMPOSITION, nItem, (LPARAM)&lvFindInfo.pt );
6075 while ((nItem = ListView_FindItemW(infoPtr->hwndSelf, nItem, &lvFindInfo)) != -1)
6077 if ((ListView_GetItemState(infoPtr->hwndSelf, nItem, uMask) & uMask) == uMask)
6082 else if (uFlags & LVNI_BELOW)
6084 if ((uView == LVS_LIST) || (uView == LVS_REPORT))
6086 while (nItem < infoPtr->nItemCount)
6089 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
6095 /* Special case for autoarrange - move 'til the bottom of a list */
6096 if (is_autoarrange(infoPtr))
6098 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
6099 while (nItem + nCountPerRow < infoPtr->nItemCount )
6101 nItem += nCountPerRow;
6102 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
6107 lvFindInfo.flags = LVFI_NEARESTXY;
6108 lvFindInfo.vkDirection = VK_DOWN;
6109 SendMessageW( infoPtr->hwndSelf, LVM_GETITEMPOSITION, nItem, (LPARAM)&lvFindInfo.pt );
6110 while ((nItem = ListView_FindItemW(infoPtr->hwndSelf, nItem, &lvFindInfo)) != -1)
6112 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
6117 else if (uFlags & LVNI_TOLEFT)
6119 if (uView == LVS_LIST)
6121 nCountPerColumn = LISTVIEW_GetCountPerColumn(infoPtr);
6122 while (nItem - nCountPerColumn >= 0)
6124 nItem -= nCountPerColumn;
6125 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
6129 else if ((uView == LVS_SMALLICON) || (uView == LVS_ICON))
6131 /* Special case for autoarrange - move 'ti the beginning of a row */
6132 if (is_autoarrange(infoPtr))
6134 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
6135 while (nItem % nCountPerRow > 0)
6138 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
6143 lvFindInfo.flags = LVFI_NEARESTXY;
6144 lvFindInfo.vkDirection = VK_LEFT;
6145 SendMessageW( infoPtr->hwndSelf, LVM_GETITEMPOSITION, nItem, (LPARAM)&lvFindInfo.pt );
6146 while ((nItem = ListView_FindItemW(infoPtr->hwndSelf, nItem, &lvFindInfo)) != -1)
6148 if ((ListView_GetItemState(infoPtr->hwndSelf, nItem, uMask) & uMask) == uMask)
6153 else if (uFlags & LVNI_TORIGHT)
6155 if (uView == LVS_LIST)
6157 nCountPerColumn = LISTVIEW_GetCountPerColumn(infoPtr);
6158 while (nItem + nCountPerColumn < infoPtr->nItemCount)
6160 nItem += nCountPerColumn;
6161 if ((ListView_GetItemState(infoPtr->hwndSelf, nItem, uMask) & uMask) == uMask)
6165 else if ((uView == LVS_SMALLICON) || (uView == LVS_ICON))
6167 /* Special case for autoarrange - move 'til the end of a row */
6168 if (is_autoarrange(infoPtr))
6170 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
6171 while (nItem % nCountPerRow < nCountPerRow - 1 )
6174 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
6179 lvFindInfo.flags = LVFI_NEARESTXY;
6180 lvFindInfo.vkDirection = VK_RIGHT;
6181 SendMessageW( infoPtr->hwndSelf, LVM_GETITEMPOSITION, nItem, (LPARAM)&lvFindInfo.pt );
6182 while ((nItem = ListView_FindItemW(infoPtr->hwndSelf, nItem, &lvFindInfo)) != -1)
6184 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
6193 /* search by index */
6194 for (i = nItem; i < infoPtr->nItemCount; i++)
6196 if ((LISTVIEW_GetItemState(infoPtr, i, uMask) & uMask) == uMask)
6204 /* LISTVIEW_GetNumberOfWorkAreas */
6208 * Retrieves the origin coordinates when in icon or small icon display mode.
6211 * [I] infoPtr : valid pointer to the listview structure
6212 * [O] lpptOrigin : coordinate information
6217 static void LISTVIEW_GetOrigin(const LISTVIEW_INFO *infoPtr, LPPOINT lpptOrigin)
6219 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
6220 INT nHorzPos = 0, nVertPos = 0;
6221 SCROLLINFO scrollInfo;
6223 scrollInfo.cbSize = sizeof(SCROLLINFO);
6224 scrollInfo.fMask = SIF_POS;
6226 if (GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo))
6227 nHorzPos = scrollInfo.nPos;
6228 if (GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
6229 nVertPos = scrollInfo.nPos;
6231 TRACE("nHorzPos=%d, nVertPos=%d\n", nHorzPos, nVertPos);
6233 lpptOrigin->x = infoPtr->rcList.left;
6234 lpptOrigin->y = infoPtr->rcList.top;
6235 if (uView == LVS_LIST)
6236 nHorzPos *= infoPtr->nItemWidth;
6237 else if (uView == LVS_REPORT)
6238 nVertPos *= infoPtr->nItemHeight;
6240 lpptOrigin->x -= nHorzPos;
6241 lpptOrigin->y -= nVertPos;
6243 TRACE(" origin=%s\n", wine_dbgstr_point(lpptOrigin));
6248 * Retrieves the width of a string.
6251 * [I] hwnd : window handle
6252 * [I] lpszText : text string to process
6253 * [I] isW : TRUE if lpszText is Unicode, FALSE otherwise
6256 * SUCCESS : string width (in pixels)
6259 static INT LISTVIEW_GetStringWidthT(const LISTVIEW_INFO *infoPtr, LPCWSTR lpszText, BOOL isW)
6264 if (is_textT(lpszText, isW))
6266 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
6267 HDC hdc = GetDC(infoPtr->hwndSelf);
6268 HFONT hOldFont = SelectObject(hdc, hFont);
6271 GetTextExtentPointW(hdc, lpszText, lstrlenW(lpszText), &stringSize);
6273 GetTextExtentPointA(hdc, (LPCSTR)lpszText, lstrlenA((LPCSTR)lpszText), &stringSize);
6274 SelectObject(hdc, hOldFont);
6275 ReleaseDC(infoPtr->hwndSelf, hdc);
6277 return stringSize.cx;
6282 * Determines which listview item is located at the specified position.
6285 * [I] infoPtr : valid pointer to the listview structure
6286 * [IO] lpht : hit test information
6287 * [I] subitem : fill out iSubItem.
6288 * [I] select : return the index only if the hit selects the item
6291 * (mm 20001022): We must not allow iSubItem to be touched, for
6292 * an app might pass only a structure with space up to iItem!
6293 * (MS Office 97 does that for instance in the file open dialog)
6296 * SUCCESS : item index
6299 static INT LISTVIEW_HitTest(const LISTVIEW_INFO *infoPtr, LPLVHITTESTINFO lpht, BOOL subitem, BOOL select)
6301 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
6302 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
6303 RECT rcBox, rcBounds, rcState, rcIcon, rcLabel, rcSearch;
6304 POINT Origin, Position, opt;
6309 TRACE("(pt=%s, subitem=%d, select=%d)\n", wine_dbgstr_point(&lpht->pt), subitem, select);
6313 if (subitem) lpht->iSubItem = 0;
6315 if (infoPtr->rcList.left > lpht->pt.x)
6316 lpht->flags |= LVHT_TOLEFT;
6317 else if (infoPtr->rcList.right < lpht->pt.x)
6318 lpht->flags |= LVHT_TORIGHT;
6320 if (infoPtr->rcList.top > lpht->pt.y)
6321 lpht->flags |= LVHT_ABOVE;
6322 else if (infoPtr->rcList.bottom < lpht->pt.y)
6323 lpht->flags |= LVHT_BELOW;
6325 TRACE("lpht->flags=0x%x\n", lpht->flags);
6326 if (lpht->flags) return -1;
6328 lpht->flags |= LVHT_NOWHERE;
6330 LISTVIEW_GetOrigin(infoPtr, &Origin);
6332 /* first deal with the large items */
6333 rcSearch.left = lpht->pt.x;
6334 rcSearch.top = lpht->pt.y;
6335 rcSearch.right = rcSearch.left + 1;
6336 rcSearch.bottom = rcSearch.top + 1;
6338 iterator_frameditems(&i, infoPtr, &rcSearch);
6339 iterator_next(&i); /* go to first item in the sequence */
6341 iterator_destroy(&i);
6343 TRACE("lpht->iItem=%d\n", iItem);
6344 if (iItem == -1) return -1;
6346 lvItem.mask = LVIF_STATE | LVIF_TEXT;
6347 if (uView == LVS_REPORT) lvItem.mask |= LVIF_INDENT;
6348 lvItem.stateMask = LVIS_STATEIMAGEMASK;
6349 if (uView == LVS_ICON) lvItem.stateMask |= LVIS_FOCUSED;
6350 lvItem.iItem = iItem;
6351 lvItem.iSubItem = 0;
6352 lvItem.pszText = szDispText;
6353 lvItem.cchTextMax = DISP_TEXT_SIZE;
6354 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return -1;
6355 if (!infoPtr->bFocus) lvItem.state &= ~LVIS_FOCUSED;
6357 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, &rcBox, NULL, &rcIcon, &rcState, &rcLabel);
6358 LISTVIEW_GetItemOrigin(infoPtr, iItem, &Position);
6359 opt.x = lpht->pt.x - Position.x - Origin.x;
6360 opt.y = lpht->pt.y - Position.y - Origin.y;
6362 if (uView == LVS_REPORT)
6366 UnionRect(&rcBounds, &rcIcon, &rcLabel);
6367 UnionRect(&rcBounds, &rcBounds, &rcState);
6369 TRACE("rcBounds=%s\n", wine_dbgstr_rect(&rcBounds));
6370 if (!PtInRect(&rcBounds, opt)) return -1;
6372 if (PtInRect(&rcIcon, opt))
6373 lpht->flags |= LVHT_ONITEMICON;
6374 else if (PtInRect(&rcLabel, opt))
6375 lpht->flags |= LVHT_ONITEMLABEL;
6376 else if (infoPtr->himlState && STATEIMAGEINDEX(lvItem.state) && PtInRect(&rcState, opt))
6377 lpht->flags |= LVHT_ONITEMSTATEICON;
6378 if (lpht->flags & LVHT_ONITEM)
6379 lpht->flags &= ~LVHT_NOWHERE;
6381 TRACE("lpht->flags=0x%x\n", lpht->flags);
6382 if (uView == LVS_REPORT && subitem)
6386 rcBounds.right = rcBounds.left;
6387 for (j = 0; j < DPA_GetPtrCount(infoPtr->hdpaColumns); j++)
6389 rcBounds.left = rcBounds.right;
6390 rcBounds.right += LISTVIEW_GetColumnWidth(infoPtr, j);
6391 if (PtInRect(&rcBounds, opt))
6399 if (select && !(uView == LVS_REPORT &&
6400 ((infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) ||
6401 (infoPtr->dwStyle & LVS_OWNERDRAWFIXED))))
6403 if (uView == LVS_REPORT)
6405 UnionRect(&rcBounds, &rcIcon, &rcLabel);
6406 UnionRect(&rcBounds, &rcBounds, &rcState);
6408 if (!PtInRect(&rcBounds, opt)) iItem = -1;
6410 return lpht->iItem = iItem;
6414 /* LISTVIEW_InsertCompare: callback routine for comparing pszText members of the LV_ITEMS
6415 in a LISTVIEW on insert. Passed to DPA_Sort in LISTVIEW_InsertItem.
6416 This function should only be used for inserting items into a sorted list (LVM_INSERTITEM)
6417 and not during the processing of a LVM_SORTITEMS message. Applications should provide
6418 their own sort proc. when sending LVM_SORTITEMS.
6421 (remarks on LVITEM: LVM_INSERTITEM will insert the new item in the proper sort postion...
6423 LVS_SORTXXX must be specified,
6424 LVS_OWNERDRAW is not set,
6425 <item>.pszText is not LPSTR_TEXTCALLBACK.
6427 (LVS_SORT* flags): "For the LVS_SORTASCENDING... styles, item indices
6428 are sorted based on item text..."
6430 static INT WINAPI LISTVIEW_InsertCompare( LPVOID first, LPVOID second, LPARAM lParam)
6432 ITEM_INFO* lv_first = (ITEM_INFO*) DPA_GetPtr( (HDPA)first, 0 );
6433 ITEM_INFO* lv_second = (ITEM_INFO*) DPA_GetPtr( (HDPA)second, 0 );
6434 INT cmpv = textcmpWT(lv_first->hdr.pszText, lv_second->hdr.pszText, TRUE);
6436 /* if we're sorting descending, negate the return value */
6437 return (((const LISTVIEW_INFO *)lParam)->dwStyle & LVS_SORTDESCENDING) ? -cmpv : cmpv;
6442 * Inserts a new item in the listview control.
6445 * [I] infoPtr : valid pointer to the listview structure
6446 * [I] lpLVItem : item information
6447 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
6450 * SUCCESS : new item index
6453 static INT LISTVIEW_InsertItemT(LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem, BOOL isW)
6455 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
6460 BOOL is_sorted, has_changed;
6462 HWND hwndSelf = infoPtr->hwndSelf;
6464 TRACE("(lpLVItem=%s, isW=%d)\n", debuglvitem_t(lpLVItem, isW), isW);
6466 if (infoPtr->dwStyle & LVS_OWNERDATA) return infoPtr->nItemCount++;
6468 /* make sure it's an item, and not a subitem; cannot insert a subitem */
6469 if (!lpLVItem || lpLVItem->iSubItem) return -1;
6471 if (!is_assignable_item(lpLVItem, infoPtr->dwStyle)) return -1;
6473 if (!(lpItem = Alloc(sizeof(ITEM_INFO)))) return -1;
6475 /* insert item in listview control data structure */
6476 if ( !(hdpaSubItems = DPA_Create(8)) ) goto fail;
6477 if ( !DPA_SetPtr(hdpaSubItems, 0, lpItem) ) assert (FALSE);
6479 is_sorted = (infoPtr->dwStyle & (LVS_SORTASCENDING | LVS_SORTDESCENDING)) &&
6480 !(infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (LPSTR_TEXTCALLBACKW != lpLVItem->pszText);
6482 if (lpLVItem->iItem < 0 && !is_sorted) return -1;
6484 nItem = is_sorted ? infoPtr->nItemCount : min(lpLVItem->iItem, infoPtr->nItemCount);
6485 TRACE(" inserting at %d, sorted=%d, count=%d, iItem=%d\n", nItem, is_sorted, infoPtr->nItemCount, lpLVItem->iItem);
6486 nItem = DPA_InsertPtr( infoPtr->hdpaItems, nItem, hdpaSubItems );
6487 if (nItem == -1) goto fail;
6488 infoPtr->nItemCount++;
6490 /* shift indices first so they don't get tangled */
6491 LISTVIEW_ShiftIndices(infoPtr, nItem, 1);
6493 /* set the item attributes */
6494 if (lpLVItem->mask & (LVIF_GROUPID|LVIF_COLUMNS))
6496 /* full size structure expected - _WIN32IE >= 0x560 */
6499 else if (lpLVItem->mask & LVIF_INDENT)
6501 /* indent member expected - _WIN32IE >= 0x300 */
6502 memcpy(&item, lpLVItem, offsetof( LVITEMW, iGroupId ));
6506 /* minimal structure expected */
6507 memcpy(&item, lpLVItem, offsetof( LVITEMW, iIndent ));
6510 if (infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES)
6512 item.mask |= LVIF_STATE;
6513 item.stateMask |= LVIS_STATEIMAGEMASK;
6514 item.state &= ~LVIS_STATEIMAGEMASK;
6515 item.state |= INDEXTOSTATEIMAGEMASK(1);
6517 if (!set_main_item(infoPtr, &item, TRUE, isW, &has_changed)) goto undo;
6519 /* if we're sorted, sort the list, and update the index */
6522 DPA_Sort( infoPtr->hdpaItems, LISTVIEW_InsertCompare, (LPARAM)infoPtr );
6523 nItem = DPA_GetPtrIndex( infoPtr->hdpaItems, hdpaSubItems );
6524 assert(nItem != -1);
6527 /* make room for the position, if we are in the right mode */
6528 if ((uView == LVS_SMALLICON) || (uView == LVS_ICON))
6530 if (DPA_InsertPtr(infoPtr->hdpaPosX, nItem, 0) == -1)
6532 if (DPA_InsertPtr(infoPtr->hdpaPosY, nItem, 0) == -1)
6534 DPA_DeletePtr(infoPtr->hdpaPosX, nItem);
6539 /* send LVN_INSERTITEM notification */
6540 ZeroMemory(&nmlv, sizeof(NMLISTVIEW));
6542 nmlv.lParam = lpItem->lParam;
6543 notify_listview(infoPtr, LVN_INSERTITEM, &nmlv);
6544 if (!IsWindow(hwndSelf))
6547 /* align items (set position of each item) */
6548 if ((uView == LVS_SMALLICON || uView == LVS_ICON))
6552 if (infoPtr->dwStyle & LVS_ALIGNLEFT)
6553 LISTVIEW_NextIconPosLeft(infoPtr, &pt);
6555 LISTVIEW_NextIconPosTop(infoPtr, &pt);
6557 LISTVIEW_MoveIconTo(infoPtr, nItem, &pt, TRUE);
6560 /* now is the invalidation fun */
6561 LISTVIEW_ScrollOnInsert(infoPtr, nItem, 1);
6565 LISTVIEW_ShiftIndices(infoPtr, nItem, -1);
6566 DPA_DeletePtr(infoPtr->hdpaItems, nItem);
6567 infoPtr->nItemCount--;
6569 DPA_DeletePtr(hdpaSubItems, 0);
6570 DPA_Destroy (hdpaSubItems);
6577 * Redraws a range of items.
6580 * [I] infoPtr : valid pointer to the listview structure
6581 * [I] nFirst : first item
6582 * [I] nLast : last item
6588 static BOOL LISTVIEW_RedrawItems(const LISTVIEW_INFO *infoPtr, INT nFirst, INT nLast)
6592 if (nLast < nFirst || min(nFirst, nLast) < 0 ||
6593 max(nFirst, nLast) >= infoPtr->nItemCount)
6596 for (i = nFirst; i <= nLast; i++)
6597 LISTVIEW_InvalidateItem(infoPtr, i);
6604 * Scroll the content of a listview.
6607 * [I] infoPtr : valid pointer to the listview structure
6608 * [I] dx : horizontal scroll amount in pixels
6609 * [I] dy : vertical scroll amount in pixels
6616 * If the control is in report mode (LVS_REPORT) the control can
6617 * be scrolled only in line increments. "dy" will be rounded to the
6618 * nearest number of pixels that are a whole line. Ex: if line height
6619 * is 16 and an 8 is passed, the list will be scrolled by 16. If a 7
6620 * is passed, then the scroll will be 0. (per MSDN 7/2002)
6622 * For: (per experimentation with native control and CSpy ListView)
6623 * LVS_ICON dy=1 = 1 pixel (vertical only)
6625 * LVS_SMALLICON dy=1 = 1 pixel (vertical only)
6627 * LVS_LIST dx=1 = 1 column (horizontal only)
6628 * but will only scroll 1 column per message
6629 * no matter what the value.
6630 * dy must be 0 or FALSE returned.
6631 * LVS_REPORT dx=1 = 1 pixel
6635 static BOOL LISTVIEW_Scroll(LISTVIEW_INFO *infoPtr, INT dx, INT dy)
6637 switch(infoPtr->dwStyle & LVS_TYPEMASK) {
6639 dy += (dy < 0 ? -1 : 1) * infoPtr->nItemHeight/2;
6640 dy /= infoPtr->nItemHeight;
6643 if (dy != 0) return FALSE;
6650 if (dx != 0) LISTVIEW_HScroll(infoPtr, SB_INTERNAL, dx, 0);
6651 if (dy != 0) LISTVIEW_VScroll(infoPtr, SB_INTERNAL, dy, 0);
6658 * Sets the background color.
6661 * [I] infoPtr : valid pointer to the listview structure
6662 * [I] clrBk : background color
6668 static BOOL LISTVIEW_SetBkColor(LISTVIEW_INFO *infoPtr, COLORREF clrBk)
6670 TRACE("(clrBk=%x)\n", clrBk);
6672 if(infoPtr->clrBk != clrBk) {
6673 if (infoPtr->clrBk != CLR_NONE) DeleteObject(infoPtr->hBkBrush);
6674 infoPtr->clrBk = clrBk;
6675 if (clrBk == CLR_NONE)
6676 infoPtr->hBkBrush = (HBRUSH)GetClassLongPtrW(infoPtr->hwndSelf, GCLP_HBRBACKGROUND);
6678 infoPtr->hBkBrush = CreateSolidBrush(clrBk);
6679 LISTVIEW_InvalidateList(infoPtr);
6685 /* LISTVIEW_SetBkImage */
6687 /*** Helper for {Insert,Set}ColumnT *only* */
6688 static void column_fill_hditem(const LISTVIEW_INFO *infoPtr, HDITEMW *lphdi, INT nColumn,
6689 const LVCOLUMNW *lpColumn, BOOL isW)
6691 if (lpColumn->mask & LVCF_FMT)
6693 /* format member is valid */
6694 lphdi->mask |= HDI_FORMAT;
6696 /* set text alignment (leftmost column must be left-aligned) */
6697 if (nColumn == 0 || (lpColumn->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_LEFT)
6698 lphdi->fmt |= HDF_LEFT;
6699 else if ((lpColumn->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_RIGHT)
6700 lphdi->fmt |= HDF_RIGHT;
6701 else if ((lpColumn->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_CENTER)
6702 lphdi->fmt |= HDF_CENTER;
6704 if (lpColumn->fmt & LVCFMT_BITMAP_ON_RIGHT)
6705 lphdi->fmt |= HDF_BITMAP_ON_RIGHT;
6707 if (lpColumn->fmt & LVCFMT_COL_HAS_IMAGES)
6709 lphdi->fmt |= HDF_IMAGE;
6710 lphdi->iImage = I_IMAGECALLBACK;
6714 if (lpColumn->mask & LVCF_WIDTH)
6716 lphdi->mask |= HDI_WIDTH;
6717 if(lpColumn->cx == LVSCW_AUTOSIZE_USEHEADER)
6719 /* make it fill the remainder of the controls width */
6723 for(item_index = 0; item_index < (nColumn - 1); item_index++)
6725 LISTVIEW_GetHeaderRect(infoPtr, item_index, &rcHeader);
6726 lphdi->cxy += rcHeader.right - rcHeader.left;
6729 /* retrieve the layout of the header */
6730 GetClientRect(infoPtr->hwndSelf, &rcHeader);
6731 TRACE("start cxy=%d rcHeader=%s\n", lphdi->cxy, wine_dbgstr_rect(&rcHeader));
6733 lphdi->cxy = (rcHeader.right - rcHeader.left) - lphdi->cxy;
6736 lphdi->cxy = lpColumn->cx;
6739 if (lpColumn->mask & LVCF_TEXT)
6741 lphdi->mask |= HDI_TEXT | HDI_FORMAT;
6742 lphdi->fmt |= HDF_STRING;
6743 lphdi->pszText = lpColumn->pszText;
6744 lphdi->cchTextMax = textlenT(lpColumn->pszText, isW);
6747 if (lpColumn->mask & LVCF_IMAGE)
6749 lphdi->mask |= HDI_IMAGE;
6750 lphdi->iImage = lpColumn->iImage;
6753 if (lpColumn->mask & LVCF_ORDER)
6755 lphdi->mask |= HDI_ORDER;
6756 lphdi->iOrder = lpColumn->iOrder;
6763 * Inserts a new column.
6766 * [I] infoPtr : valid pointer to the listview structure
6767 * [I] nColumn : column index
6768 * [I] lpColumn : column information
6769 * [I] isW : TRUE if lpColumn is Unicode, FALSE otherwise
6772 * SUCCESS : new column index
6775 static INT LISTVIEW_InsertColumnT(LISTVIEW_INFO *infoPtr, INT nColumn,
6776 const LVCOLUMNW *lpColumn, BOOL isW)
6778 COLUMN_INFO *lpColumnInfo;
6782 TRACE("(nColumn=%d, lpColumn=%s, isW=%d)\n", nColumn, debuglvcolumn_t(lpColumn, isW), isW);
6784 if (!lpColumn || nColumn < 0) return -1;
6785 nColumn = min(nColumn, DPA_GetPtrCount(infoPtr->hdpaColumns));
6787 ZeroMemory(&hdi, sizeof(HDITEMW));
6788 column_fill_hditem(infoPtr, &hdi, nColumn, lpColumn, isW);
6791 * A mask not including LVCF_WIDTH turns into a mask of width, width 10
6792 * (can be seen in SPY) otherwise column never gets added.
6794 if (!(lpColumn->mask & LVCF_WIDTH)) {
6795 hdi.mask |= HDI_WIDTH;
6800 * when the iSubItem is available Windows copies it to the header lParam. It seems
6801 * to happen only in LVM_INSERTCOLUMN - not in LVM_SETCOLUMN
6803 if (lpColumn->mask & LVCF_SUBITEM)
6805 hdi.mask |= HDI_LPARAM;
6806 hdi.lParam = lpColumn->iSubItem;
6809 /* insert item in header control */
6810 nNewColumn = SendMessageW(infoPtr->hwndHeader,
6811 isW ? HDM_INSERTITEMW : HDM_INSERTITEMA,
6812 (WPARAM)nColumn, (LPARAM)&hdi);
6813 if (nNewColumn == -1) return -1;
6814 if (nNewColumn != nColumn) ERR("nColumn=%d, nNewColumn=%d\n", nColumn, nNewColumn);
6816 /* create our own column info */
6817 if (!(lpColumnInfo = Alloc(sizeof(COLUMN_INFO)))) goto fail;
6818 if (DPA_InsertPtr(infoPtr->hdpaColumns, nNewColumn, lpColumnInfo) == -1) goto fail;
6820 if (lpColumn->mask & LVCF_FMT) lpColumnInfo->fmt = lpColumn->fmt;
6821 if (!Header_GetItemRect(infoPtr->hwndHeader, nNewColumn, &lpColumnInfo->rcHeader)) goto fail;
6823 /* now we have to actually adjust the data */
6824 if (!(infoPtr->dwStyle & LVS_OWNERDATA) && infoPtr->nItemCount > 0)
6826 SUBITEM_INFO *lpSubItem;
6830 for (nItem = 0; nItem < infoPtr->nItemCount; nItem++)
6832 hdpaSubItems = (HDPA)DPA_GetPtr(infoPtr->hdpaItems, nItem);
6833 for (i = 1; i < DPA_GetPtrCount(hdpaSubItems); i++)
6835 lpSubItem = (SUBITEM_INFO *)DPA_GetPtr(hdpaSubItems, i);
6836 if (lpSubItem->iSubItem >= nNewColumn)
6837 lpSubItem->iSubItem++;
6842 /* make space for the new column */
6843 LISTVIEW_ScrollColumns(infoPtr, nNewColumn + 1, lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left);
6844 LISTVIEW_UpdateItemSize(infoPtr);
6849 if (nNewColumn != -1) SendMessageW(infoPtr->hwndHeader, HDM_DELETEITEM, nNewColumn, 0);
6852 DPA_DeletePtr(infoPtr->hdpaColumns, nNewColumn);
6860 * Sets the attributes of a header item.
6863 * [I] infoPtr : valid pointer to the listview structure
6864 * [I] nColumn : column index
6865 * [I] lpColumn : column attributes
6866 * [I] isW: if TRUE, then lpColumn is a LPLVCOLUMNW, else it is a LPLVCOLUMNA
6872 static BOOL LISTVIEW_SetColumnT(const LISTVIEW_INFO *infoPtr, INT nColumn,
6873 const LVCOLUMNW *lpColumn, BOOL isW)
6875 HDITEMW hdi, hdiget;
6878 TRACE("(nColumn=%d, lpColumn=%s, isW=%d)\n", nColumn, debuglvcolumn_t(lpColumn, isW), isW);
6880 if (!lpColumn || nColumn < 0 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
6882 ZeroMemory(&hdi, sizeof(HDITEMW));
6883 if (lpColumn->mask & LVCF_FMT)
6885 hdi.mask |= HDI_FORMAT;
6886 hdiget.mask = HDI_FORMAT;
6887 if (Header_GetItemW(infoPtr->hwndHeader, nColumn, &hdiget))
6888 hdi.fmt = hdiget.fmt & HDF_STRING;
6890 column_fill_hditem(infoPtr, &hdi, nColumn, lpColumn, isW);
6892 /* set header item attributes */
6893 bResult = SendMessageW(infoPtr->hwndHeader, isW ? HDM_SETITEMW : HDM_SETITEMA, (WPARAM)nColumn, (LPARAM)&hdi);
6894 if (!bResult) return FALSE;
6896 if (lpColumn->mask & LVCF_FMT)
6898 COLUMN_INFO *lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, nColumn);
6899 int oldFmt = lpColumnInfo->fmt;
6901 lpColumnInfo->fmt = lpColumn->fmt;
6902 if ((oldFmt ^ lpColumn->fmt) & (LVCFMT_JUSTIFYMASK | LVCFMT_IMAGE))
6904 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
6905 if (uView == LVS_REPORT) LISTVIEW_InvalidateColumn(infoPtr, nColumn);
6914 * Sets the column order array
6917 * [I] infoPtr : valid pointer to the listview structure
6918 * [I] iCount : number of elements in column order array
6919 * [I] lpiArray : pointer to column order array
6925 static BOOL LISTVIEW_SetColumnOrderArray(const LISTVIEW_INFO *infoPtr, INT iCount, const INT *lpiArray)
6927 FIXME("iCount %d lpiArray %p\n", iCount, lpiArray);
6938 * Sets the width of a column
6941 * [I] infoPtr : valid pointer to the listview structure
6942 * [I] nColumn : column index
6943 * [I] cx : column width
6949 static BOOL LISTVIEW_SetColumnWidth(LISTVIEW_INFO *infoPtr, INT nColumn, INT cx)
6951 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
6952 WCHAR szDispText[DISP_TEXT_SIZE] = { 0 };
6956 TRACE("(nColumn=%d, cx=%d\n", nColumn, cx);
6958 /* set column width only if in report or list mode */
6959 if (uView != LVS_REPORT && uView != LVS_LIST) return FALSE;
6961 /* take care of invalid cx values */
6962 if(uView == LVS_REPORT && cx < -2) cx = LVSCW_AUTOSIZE;
6963 else if (uView == LVS_LIST && cx < 1) return FALSE;
6965 /* resize all columns if in LVS_LIST mode */
6966 if(uView == LVS_LIST)
6968 infoPtr->nItemWidth = cx;
6969 LISTVIEW_InvalidateList(infoPtr);
6973 if (nColumn < 0 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
6975 if (cx == LVSCW_AUTOSIZE || (cx == LVSCW_AUTOSIZE_USEHEADER && nColumn < DPA_GetPtrCount(infoPtr->hdpaColumns) -1))
6980 lvItem.mask = LVIF_TEXT;
6982 lvItem.iSubItem = nColumn;
6983 lvItem.pszText = szDispText;
6984 lvItem.cchTextMax = DISP_TEXT_SIZE;
6985 for (; lvItem.iItem < infoPtr->nItemCount; lvItem.iItem++)
6987 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) continue;
6988 nLabelWidth = LISTVIEW_GetStringWidthT(infoPtr, lvItem.pszText, TRUE);
6989 if (max_cx < nLabelWidth) max_cx = nLabelWidth;
6991 if (infoPtr->himlSmall && (nColumn == 0 || (LISTVIEW_GetColumnInfo(infoPtr, nColumn)->fmt & LVCFMT_IMAGE)))
6992 max_cx += infoPtr->iconSize.cx;
6993 max_cx += TRAILING_LABEL_PADDING;
6996 /* autosize based on listview items width */
6997 if(cx == LVSCW_AUTOSIZE)
6999 else if(cx == LVSCW_AUTOSIZE_USEHEADER)
7001 /* if iCol is the last column make it fill the remainder of the controls width */
7002 if(nColumn == DPA_GetPtrCount(infoPtr->hdpaColumns) - 1)
7007 LISTVIEW_GetOrigin(infoPtr, &Origin);
7008 LISTVIEW_GetHeaderRect(infoPtr, nColumn, &rcHeader);
7010 cx = infoPtr->rcList.right - Origin.x - rcHeader.left;
7014 /* Despite what the MS docs say, if this is not the last
7015 column, then MS resizes the column to the width of the
7016 largest text string in the column, including headers
7017 and items. This is different from LVSCW_AUTOSIZE in that
7018 LVSCW_AUTOSIZE ignores the header string length. */
7021 /* retrieve header text */
7022 hdi.mask = HDI_TEXT;
7023 hdi.cchTextMax = DISP_TEXT_SIZE;
7024 hdi.pszText = szDispText;
7025 if (Header_GetItemW(infoPtr->hwndHeader, nColumn, &hdi))
7027 HDC hdc = GetDC(infoPtr->hwndSelf);
7028 HFONT old_font = SelectObject(hdc, (HFONT)SendMessageW(infoPtr->hwndHeader, WM_GETFONT, 0, 0));
7031 if (GetTextExtentPoint32W(hdc, hdi.pszText, lstrlenW(hdi.pszText), &size))
7032 cx = size.cx + TRAILING_HEADER_PADDING;
7033 /* FIXME: Take into account the header image, if one is present */
7034 SelectObject(hdc, old_font);
7035 ReleaseDC(infoPtr->hwndSelf, hdc);
7037 cx = max (cx, max_cx);
7041 if (cx < 0) return FALSE;
7043 /* call header to update the column change */
7044 hdi.mask = HDI_WIDTH;
7046 TRACE("hdi.cxy=%d\n", hdi.cxy);
7047 return Header_SetItemW(infoPtr->hwndHeader, nColumn, &hdi);
7051 * Creates the checkbox imagelist. Helper for LISTVIEW_SetExtendedListViewStyle
7054 static HIMAGELIST LISTVIEW_CreateCheckBoxIL(const LISTVIEW_INFO *infoPtr)
7057 HBITMAP hbm_im, hbm_mask, hbm_orig;
7059 HBRUSH hbr_white = GetStockObject(WHITE_BRUSH);
7060 HBRUSH hbr_black = GetStockObject(BLACK_BRUSH);
7063 himl = ImageList_Create(GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON),
7064 ILC_COLOR | ILC_MASK, 2, 2);
7065 hdc_wnd = GetDC(infoPtr->hwndSelf);
7066 hdc = CreateCompatibleDC(hdc_wnd);
7067 hbm_im = CreateCompatibleBitmap(hdc_wnd, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON));
7068 hbm_mask = CreateBitmap(GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), 1, 1, NULL);
7069 ReleaseDC(infoPtr->hwndSelf, hdc_wnd);
7071 rc.left = rc.top = 0;
7072 rc.right = GetSystemMetrics(SM_CXSMICON);
7073 rc.bottom = GetSystemMetrics(SM_CYSMICON);
7075 hbm_orig = SelectObject(hdc, hbm_mask);
7076 FillRect(hdc, &rc, hbr_white);
7077 InflateRect(&rc, -3, -3);
7078 FillRect(hdc, &rc, hbr_black);
7080 SelectObject(hdc, hbm_im);
7081 DrawFrameControl(hdc, &rc, DFC_BUTTON, DFCS_BUTTONCHECK | DFCS_MONO);
7082 SelectObject(hdc, hbm_orig);
7083 ImageList_Add(himl, hbm_im, hbm_mask);
7085 SelectObject(hdc, hbm_im);
7086 DrawFrameControl(hdc, &rc, DFC_BUTTON, DFCS_BUTTONCHECK | DFCS_MONO | DFCS_CHECKED);
7087 SelectObject(hdc, hbm_orig);
7088 ImageList_Add(himl, hbm_im, hbm_mask);
7090 DeleteObject(hbm_mask);
7091 DeleteObject(hbm_im);
7099 * Sets the extended listview style.
7102 * [I] infoPtr : valid pointer to the listview structure
7104 * [I] dwStyle : style
7107 * SUCCESS : previous style
7110 static DWORD LISTVIEW_SetExtendedListViewStyle(LISTVIEW_INFO *infoPtr, DWORD dwMask, DWORD dwExStyle)
7112 DWORD dwOldExStyle = infoPtr->dwLvExStyle;
7116 infoPtr->dwLvExStyle = (dwOldExStyle & ~dwMask) | (dwExStyle & dwMask);
7118 infoPtr->dwLvExStyle = dwExStyle;
7120 if((infoPtr->dwLvExStyle ^ dwOldExStyle) & LVS_EX_CHECKBOXES)
7122 HIMAGELIST himl = 0;
7123 if(infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES)
7126 item.mask = LVIF_STATE;
7127 item.stateMask = LVIS_STATEIMAGEMASK;
7128 item.state = INDEXTOSTATEIMAGEMASK(1);
7129 LISTVIEW_SetItemState(infoPtr, -1, &item);
7131 himl = LISTVIEW_CreateCheckBoxIL(infoPtr);
7133 LISTVIEW_SetImageList(infoPtr, LVSIL_STATE, himl);
7136 if((infoPtr->dwLvExStyle ^ dwOldExStyle) & LVS_EX_HEADERDRAGDROP)
7138 DWORD dwStyle = GetWindowLongW(infoPtr->hwndHeader, GWL_STYLE);
7139 if (infoPtr->dwLvExStyle & LVS_EX_HEADERDRAGDROP)
7140 dwStyle |= HDS_DRAGDROP;
7142 dwStyle &= ~HDS_DRAGDROP;
7143 SetWindowLongW(infoPtr->hwndHeader, GWL_STYLE, dwStyle);
7146 /* GRIDLINES adds decoration at top so changes sizes */
7147 if((infoPtr->dwLvExStyle ^ dwOldExStyle) & LVS_EX_GRIDLINES)
7149 LISTVIEW_UpdateSize(infoPtr);
7153 LISTVIEW_InvalidateList(infoPtr);
7154 return dwOldExStyle;
7159 * Sets the new hot cursor used during hot tracking and hover selection.
7162 * [I] infoPtr : valid pointer to the listview structure
7163 * [I] hCursor : the new hot cursor handle
7166 * Returns the previous hot cursor
7168 static HCURSOR LISTVIEW_SetHotCursor(LISTVIEW_INFO *infoPtr, HCURSOR hCursor)
7170 HCURSOR oldCursor = infoPtr->hHotCursor;
7172 infoPtr->hHotCursor = hCursor;
7180 * Sets the hot item index.
7183 * [I] infoPtr : valid pointer to the listview structure
7184 * [I] iIndex : index
7187 * SUCCESS : previous hot item index
7188 * FAILURE : -1 (no hot item)
7190 static INT LISTVIEW_SetHotItem(LISTVIEW_INFO *infoPtr, INT iIndex)
7192 INT iOldIndex = infoPtr->nHotItem;
7194 infoPtr->nHotItem = iIndex;
7202 * Sets the amount of time the cursor must hover over an item before it is selected.
7205 * [I] infoPtr : valid pointer to the listview structure
7206 * [I] dwHoverTime : hover time, if -1 the hover time is set to the default
7209 * Returns the previous hover time
7211 static DWORD LISTVIEW_SetHoverTime(LISTVIEW_INFO *infoPtr, DWORD dwHoverTime)
7213 DWORD oldHoverTime = infoPtr->dwHoverTime;
7215 infoPtr->dwHoverTime = dwHoverTime;
7217 return oldHoverTime;
7222 * Sets spacing for icons of LVS_ICON style.
7225 * [I] infoPtr : valid pointer to the listview structure
7226 * [I] cx : horizontal spacing (-1 = system spacing, 0 = autosize)
7227 * [I] cy : vertical spacing (-1 = system spacing, 0 = autosize)
7230 * MAKELONG(oldcx, oldcy)
7232 static DWORD LISTVIEW_SetIconSpacing(LISTVIEW_INFO *infoPtr, INT cx, INT cy)
7234 DWORD oldspacing = MAKELONG(infoPtr->iconSpacing.cx, infoPtr->iconSpacing.cy);
7235 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
7237 TRACE("requested=(%d,%d)\n", cx, cy);
7239 /* this is supported only for LVS_ICON style */
7240 if (uView != LVS_ICON) return oldspacing;
7242 /* set to defaults, if instructed to */
7243 if (cx == -1) cx = GetSystemMetrics(SM_CXICONSPACING);
7244 if (cy == -1) cy = GetSystemMetrics(SM_CYICONSPACING);
7246 /* if 0 then compute width
7247 * FIXME: Should scan each item and determine max width of
7248 * icon or label, then make that the width */
7250 cx = infoPtr->iconSpacing.cx;
7252 /* if 0 then compute height */
7254 cy = infoPtr->iconSize.cy + 2 * infoPtr->ntmHeight +
7255 ICON_BOTTOM_PADDING + ICON_TOP_PADDING + LABEL_VERT_PADDING;
7258 infoPtr->iconSpacing.cx = cx;
7259 infoPtr->iconSpacing.cy = cy;
7261 TRACE("old=(%d,%d), new=(%d,%d), iconSize=(%d,%d), ntmH=%d\n",
7262 LOWORD(oldspacing), HIWORD(oldspacing), cx, cy,
7263 infoPtr->iconSize.cx, infoPtr->iconSize.cy,
7264 infoPtr->ntmHeight);
7266 /* these depend on the iconSpacing */
7267 LISTVIEW_UpdateItemSize(infoPtr);
7272 static inline void set_icon_size(SIZE *size, HIMAGELIST himl, BOOL small)
7276 if (himl && ImageList_GetIconSize(himl, &cx, &cy))
7283 size->cx = GetSystemMetrics(small ? SM_CXSMICON : SM_CXICON);
7284 size->cy = GetSystemMetrics(small ? SM_CYSMICON : SM_CYICON);
7293 * [I] infoPtr : valid pointer to the listview structure
7294 * [I] nType : image list type
7295 * [I] himl : image list handle
7298 * SUCCESS : old image list
7301 static HIMAGELIST LISTVIEW_SetImageList(LISTVIEW_INFO *infoPtr, INT nType, HIMAGELIST himl)
7303 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
7304 INT oldHeight = infoPtr->nItemHeight;
7305 HIMAGELIST himlOld = 0;
7307 TRACE("(nType=%d, himl=%p\n", nType, himl);
7312 himlOld = infoPtr->himlNormal;
7313 infoPtr->himlNormal = himl;
7314 if (uView == LVS_ICON) set_icon_size(&infoPtr->iconSize, himl, FALSE);
7315 LISTVIEW_SetIconSpacing(infoPtr, 0, 0);
7319 himlOld = infoPtr->himlSmall;
7320 infoPtr->himlSmall = himl;
7321 if (uView != LVS_ICON) set_icon_size(&infoPtr->iconSize, himl, TRUE);
7325 himlOld = infoPtr->himlState;
7326 infoPtr->himlState = himl;
7327 set_icon_size(&infoPtr->iconStateSize, himl, TRUE);
7328 ImageList_SetBkColor(infoPtr->himlState, CLR_NONE);
7332 ERR("Unknown icon type=%d\n", nType);
7336 infoPtr->nItemHeight = LISTVIEW_CalculateItemHeight(infoPtr);
7337 if (infoPtr->nItemHeight != oldHeight)
7338 LISTVIEW_UpdateScroll(infoPtr);
7345 * Preallocates memory (does *not* set the actual count of items !)
7348 * [I] infoPtr : valid pointer to the listview structure
7349 * [I] nItems : item count (projected number of items to allocate)
7350 * [I] dwFlags : update flags
7356 static BOOL LISTVIEW_SetItemCount(LISTVIEW_INFO *infoPtr, INT nItems, DWORD dwFlags)
7358 TRACE("(nItems=%d, dwFlags=%x)\n", nItems, dwFlags);
7360 if (infoPtr->dwStyle & LVS_OWNERDATA)
7362 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
7363 INT nOldCount = infoPtr->nItemCount;
7365 if (nItems < nOldCount)
7367 RANGE range = { nItems, nOldCount };
7368 ranges_del(infoPtr->selectionRanges, range);
7369 if (infoPtr->nFocusedItem >= nItems)
7371 infoPtr->nFocusedItem = -1;
7372 SetRectEmpty(&infoPtr->rcFocus);
7376 infoPtr->nItemCount = nItems;
7377 LISTVIEW_UpdateScroll(infoPtr);
7379 /* the flags are valid only in ownerdata report and list modes */
7380 if (uView == LVS_ICON || uView == LVS_SMALLICON) dwFlags = 0;
7382 if (!(dwFlags & LVSICF_NOSCROLL) && infoPtr->nFocusedItem != -1)
7383 LISTVIEW_EnsureVisible(infoPtr, infoPtr->nFocusedItem, FALSE);
7385 if (!(dwFlags & LVSICF_NOINVALIDATEALL))
7386 LISTVIEW_InvalidateList(infoPtr);
7393 LISTVIEW_GetOrigin(infoPtr, &Origin);
7394 nFrom = min(nOldCount, nItems);
7395 nTo = max(nOldCount, nItems);
7397 if (uView == LVS_REPORT)
7400 rcErase.top = nFrom * infoPtr->nItemHeight;
7401 rcErase.right = infoPtr->nItemWidth;
7402 rcErase.bottom = nTo * infoPtr->nItemHeight;
7403 OffsetRect(&rcErase, Origin.x, Origin.y);
7404 if (IntersectRect(&rcErase, &rcErase, &infoPtr->rcList))
7405 LISTVIEW_InvalidateRect(infoPtr, &rcErase);
7409 INT nPerCol = LISTVIEW_GetCountPerColumn(infoPtr);
7411 rcErase.left = (nFrom / nPerCol) * infoPtr->nItemWidth;
7412 rcErase.top = (nFrom % nPerCol) * infoPtr->nItemHeight;
7413 rcErase.right = rcErase.left + infoPtr->nItemWidth;
7414 rcErase.bottom = nPerCol * infoPtr->nItemHeight;
7415 OffsetRect(&rcErase, Origin.x, Origin.y);
7416 if (IntersectRect(&rcErase, &rcErase, &infoPtr->rcList))
7417 LISTVIEW_InvalidateRect(infoPtr, &rcErase);
7419 rcErase.left = (nFrom / nPerCol + 1) * infoPtr->nItemWidth;
7421 rcErase.right = (nTo / nPerCol + 1) * infoPtr->nItemWidth;
7422 rcErase.bottom = nPerCol * infoPtr->nItemHeight;
7423 OffsetRect(&rcErase, Origin.x, Origin.y);
7424 if (IntersectRect(&rcErase, &rcErase, &infoPtr->rcList))
7425 LISTVIEW_InvalidateRect(infoPtr, &rcErase);
7431 /* According to MSDN for non-LVS_OWNERDATA this is just
7432 * a performance issue. The control allocates its internal
7433 * data structures for the number of items specified. It
7434 * cuts down on the number of memory allocations. Therefore
7435 * we will just issue a WARN here
7437 WARN("for non-ownerdata performance option not implemented.\n");
7445 * Sets the position of an item.
7448 * [I] infoPtr : valid pointer to the listview structure
7449 * [I] nItem : item index
7450 * [I] pt : coordinate
7456 static BOOL LISTVIEW_SetItemPosition(LISTVIEW_INFO *infoPtr, INT nItem, POINT pt)
7458 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
7461 TRACE("(nItem=%d, &pt=%s\n", nItem, wine_dbgstr_point(&pt));
7463 if (nItem < 0 || nItem >= infoPtr->nItemCount ||
7464 !(uView == LVS_ICON || uView == LVS_SMALLICON)) return FALSE;
7466 LISTVIEW_GetOrigin(infoPtr, &Origin);
7468 /* This point value seems to be an undocumented feature.
7469 * The best guess is that it means either at the origin,
7470 * or at true beginning of the list. I will assume the origin. */
7471 if ((pt.x == -1) && (pt.y == -1))
7474 if (uView == LVS_ICON)
7476 pt.x -= (infoPtr->nItemWidth - infoPtr->iconSize.cx) / 2;
7477 pt.y -= ICON_TOP_PADDING;
7482 infoPtr->bAutoarrange = FALSE;
7484 return LISTVIEW_MoveIconTo(infoPtr, nItem, &pt, FALSE);
7489 * Sets the state of one or many items.
7492 * [I] infoPtr : valid pointer to the listview structure
7493 * [I] nItem : item index
7494 * [I] lpLVItem : item or subitem info
7500 static BOOL LISTVIEW_SetItemState(LISTVIEW_INFO *infoPtr, INT nItem, const LVITEMW *lpLVItem)
7502 BOOL bResult = TRUE;
7505 lvItem.iItem = nItem;
7506 lvItem.iSubItem = 0;
7507 lvItem.mask = LVIF_STATE;
7508 lvItem.state = lpLVItem->state;
7509 lvItem.stateMask = lpLVItem->stateMask;
7510 TRACE("lvItem=%s\n", debuglvitem_t(&lvItem, TRUE));
7514 /* apply to all items */
7515 for (lvItem.iItem = 0; lvItem.iItem < infoPtr->nItemCount; lvItem.iItem++)
7516 if (!LISTVIEW_SetItemT(infoPtr, &lvItem, TRUE)) bResult = FALSE;
7519 bResult = LISTVIEW_SetItemT(infoPtr, &lvItem, TRUE);
7522 * Update selection mark
7524 * Investigation on windows 2k showed that selection mark was updated
7525 * whenever a new selection was made, but if the selected item was
7526 * unselected it was not updated.
7528 * we are probably still not 100% accurate, but this at least sets the
7529 * proper selection mark when it is needed
7532 if (bResult && (lvItem.state & lvItem.stateMask & LVIS_SELECTED) &&
7533 (infoPtr->nSelectionMark == -1))
7536 for (i = 0; i < infoPtr->nItemCount; i++)
7538 if (infoPtr->uCallbackMask & LVIS_SELECTED)
7540 if (LISTVIEW_GetItemState(infoPtr, i, LVIS_SELECTED))
7542 infoPtr->nSelectionMark = i;
7546 else if (ranges_contain(infoPtr->selectionRanges, i))
7548 infoPtr->nSelectionMark = i;
7559 * Sets the text of an item or subitem.
7562 * [I] hwnd : window handle
7563 * [I] nItem : item index
7564 * [I] lpLVItem : item or subitem info
7565 * [I] isW : TRUE if input is Unicode
7571 static BOOL LISTVIEW_SetItemTextT(LISTVIEW_INFO *infoPtr, INT nItem, const LVITEMW *lpLVItem, BOOL isW)
7575 if (nItem < 0 && nItem >= infoPtr->nItemCount) return FALSE;
7577 lvItem.iItem = nItem;
7578 lvItem.iSubItem = lpLVItem->iSubItem;
7579 lvItem.mask = LVIF_TEXT;
7580 lvItem.pszText = lpLVItem->pszText;
7581 lvItem.cchTextMax = lpLVItem->cchTextMax;
7583 TRACE("(nItem=%d, lpLVItem=%s, isW=%d)\n", nItem, debuglvitem_t(&lvItem, isW), isW);
7585 return LISTVIEW_SetItemT(infoPtr, &lvItem, isW);
7590 * Set item index that marks the start of a multiple selection.
7593 * [I] infoPtr : valid pointer to the listview structure
7594 * [I] nIndex : index
7597 * Index number or -1 if there is no selection mark.
7599 static INT LISTVIEW_SetSelectionMark(LISTVIEW_INFO *infoPtr, INT nIndex)
7601 INT nOldIndex = infoPtr->nSelectionMark;
7603 TRACE("(nIndex=%d)\n", nIndex);
7605 infoPtr->nSelectionMark = nIndex;
7612 * Sets the text background color.
7615 * [I] infoPtr : valid pointer to the listview structure
7616 * [I] clrTextBk : text background color
7622 static BOOL LISTVIEW_SetTextBkColor(LISTVIEW_INFO *infoPtr, COLORREF clrTextBk)
7624 TRACE("(clrTextBk=%x)\n", clrTextBk);
7626 if (infoPtr->clrTextBk != clrTextBk)
7628 infoPtr->clrTextBk = clrTextBk;
7629 LISTVIEW_InvalidateList(infoPtr);
7637 * Sets the text foreground color.
7640 * [I] infoPtr : valid pointer to the listview structure
7641 * [I] clrText : text color
7647 static BOOL LISTVIEW_SetTextColor (LISTVIEW_INFO *infoPtr, COLORREF clrText)
7649 TRACE("(clrText=%x)\n", clrText);
7651 if (infoPtr->clrText != clrText)
7653 infoPtr->clrText = clrText;
7654 LISTVIEW_InvalidateList(infoPtr);
7662 * Determines which listview item is located at the specified position.
7665 * [I] infoPtr : valid pointer to the listview structure
7666 * [I] hwndNewToolTip : handle to new ToolTip
7671 static HWND LISTVIEW_SetToolTips( LISTVIEW_INFO *infoPtr, HWND hwndNewToolTip)
7673 HWND hwndOldToolTip = infoPtr->hwndToolTip;
7674 infoPtr->hwndToolTip = hwndNewToolTip;
7675 return hwndOldToolTip;
7680 * sets the Unicode character format flag for the control
7682 * [I] infoPtr :valid pointer to the listview structure
7683 * [I] fUnicode :true to switch to UNICODE false to switch to ANSI
7686 * Old Unicode Format
7688 static BOOL LISTVIEW_SetUnicodeFormat( LISTVIEW_INFO *infoPtr, BOOL fUnicode)
7690 BOOL rc = infoPtr->notifyFormat;
7691 infoPtr->notifyFormat = (fUnicode)?NFR_UNICODE:NFR_ANSI;
7695 /* LISTVIEW_SetWorkAreas */
7699 * Callback internally used by LISTVIEW_SortItems()
7702 * [I] first : pointer to first ITEM_INFO to compare
7703 * [I] second : pointer to second ITEM_INFO to compare
7704 * [I] lParam : HWND of control
7707 * if first comes before second : negative
7708 * if first comes after second : positive
7709 * if first and second are equivalent : zero
7711 static INT WINAPI LISTVIEW_CallBackCompare(LPVOID first, LPVOID second, LPARAM lParam)
7713 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)lParam;
7714 ITEM_INFO* lv_first = (ITEM_INFO*) DPA_GetPtr( (HDPA)first, 0 );
7715 ITEM_INFO* lv_second = (ITEM_INFO*) DPA_GetPtr( (HDPA)second, 0 );
7717 /* Forward the call to the client defined callback */
7718 return (infoPtr->pfnCompare)( lv_first->lParam , lv_second->lParam, infoPtr->lParamSort );
7723 * Sorts the listview items.
7726 * [I] infoPtr : valid pointer to the listview structure
7727 * [I] pfnCompare : application-defined value
7728 * [I] lParamSort : pointer to comparison callback
7734 static BOOL LISTVIEW_SortItems(LISTVIEW_INFO *infoPtr, PFNLVCOMPARE pfnCompare, LPARAM lParamSort)
7736 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
7739 LPVOID selectionMarkItem;
7743 TRACE("(pfnCompare=%p, lParamSort=%lx)\n", pfnCompare, lParamSort);
7745 if (infoPtr->dwStyle & LVS_OWNERDATA) return FALSE;
7747 if (!pfnCompare) return FALSE;
7748 if (!infoPtr->hdpaItems) return FALSE;
7750 /* if there are 0 or 1 items, there is no need to sort */
7751 if (infoPtr->nItemCount < 2) return TRUE;
7753 if (infoPtr->nFocusedItem >= 0)
7755 hdpaSubItems = (HDPA)DPA_GetPtr(infoPtr->hdpaItems, infoPtr->nFocusedItem);
7756 lpItem = (ITEM_INFO *)DPA_GetPtr(hdpaSubItems, 0);
7757 if (lpItem) lpItem->state |= LVIS_FOCUSED;
7759 /* FIXME: go thorugh selected items and mark them so in lpItem->state */
7760 /* clear the lpItem->state for non-selected ones */
7761 /* remove the selection ranges */
7763 infoPtr->pfnCompare = pfnCompare;
7764 infoPtr->lParamSort = lParamSort;
7765 DPA_Sort(infoPtr->hdpaItems, LISTVIEW_CallBackCompare, (LPARAM)infoPtr);
7767 /* Adjust selections and indices so that they are the way they should
7768 * be after the sort (otherwise, the list items move around, but
7769 * whatever is at the item's previous original position will be
7772 selectionMarkItem=(infoPtr->nSelectionMark>=0)?DPA_GetPtr(infoPtr->hdpaItems, infoPtr->nSelectionMark):NULL;
7773 for (i=0; i < infoPtr->nItemCount; i++)
7775 hdpaSubItems = (HDPA)DPA_GetPtr(infoPtr->hdpaItems, i);
7776 lpItem = (ITEM_INFO *)DPA_GetPtr(hdpaSubItems, 0);
7778 if (lpItem->state & LVIS_SELECTED)
7780 item.state = LVIS_SELECTED;
7781 item.stateMask = LVIS_SELECTED;
7782 LISTVIEW_SetItemState(infoPtr, i, &item);
7784 if (lpItem->state & LVIS_FOCUSED)
7786 infoPtr->nFocusedItem = i;
7787 lpItem->state &= ~LVIS_FOCUSED;
7790 if (selectionMarkItem != NULL)
7791 infoPtr->nSelectionMark = DPA_GetPtrIndex(infoPtr->hdpaItems, selectionMarkItem);
7792 /* I believe nHotItem should be left alone, see LISTVIEW_ShiftIndices */
7794 /* refresh the display */
7795 if (uView != LVS_ICON && uView != LVS_SMALLICON)
7796 LISTVIEW_InvalidateList(infoPtr);
7803 * Update theme handle after a theme change.
7806 * [I] infoPtr : valid pointer to the listview structure
7810 * FAILURE : something else
7812 static LRESULT LISTVIEW_ThemeChanged(const LISTVIEW_INFO *infoPtr)
7814 HTHEME theme = GetWindowTheme(infoPtr->hwndSelf);
7815 CloseThemeData(theme);
7816 OpenThemeData(infoPtr->hwndSelf, themeClass);
7822 * Updates an items or rearranges the listview control.
7825 * [I] infoPtr : valid pointer to the listview structure
7826 * [I] nItem : item index
7832 static BOOL LISTVIEW_Update(LISTVIEW_INFO *infoPtr, INT nItem)
7834 TRACE("(nItem=%d)\n", nItem);
7836 if (nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
7838 /* rearrange with default alignment style */
7839 if (is_autoarrange(infoPtr))
7840 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
7842 LISTVIEW_InvalidateItem(infoPtr, nItem);
7849 * Draw the track line at the place defined in the infoPtr structure.
7850 * The line is drawn with a XOR pen so drawing the line for the second time
7851 * in the same place erases the line.
7854 * [I] infoPtr : valid pointer to the listview structure
7860 static BOOL LISTVIEW_DrawTrackLine(const LISTVIEW_INFO *infoPtr)
7866 if (infoPtr->xTrackLine == -1)
7869 if (!(hdc = GetDC(infoPtr->hwndSelf)))
7871 hOldPen = SelectObject(hdc, GetStockObject(BLACK_PEN));
7872 oldROP = SetROP2(hdc, R2_XORPEN);
7873 MoveToEx(hdc, infoPtr->xTrackLine, infoPtr->rcList.top, NULL);
7874 LineTo(hdc, infoPtr->xTrackLine, infoPtr->rcList.bottom);
7875 SetROP2(hdc, oldROP);
7876 SelectObject(hdc, hOldPen);
7877 ReleaseDC(infoPtr->hwndSelf, hdc);
7883 * Called when an edit control should be displayed. This function is called after
7884 * we are sure that there was a single click - not a double click (this is a TIMERPROC).
7887 * [I] hwnd : Handle to the listview
7888 * [I] uMsg : WM_TIMER (ignored)
7889 * [I] idEvent : The timer ID interpreted as a pointer to a DELAYED_EDIT_ITEM struct
7890 * [I] dwTimer : The elapsed time (ignored)
7895 static VOID CALLBACK LISTVIEW_DelayedEditItem(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
7897 DELAYED_ITEM_EDIT *editItem = (DELAYED_ITEM_EDIT *)idEvent;
7898 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongPtrW(hwnd, 0);
7900 KillTimer(hwnd, idEvent);
7901 editItem->fEnabled = FALSE;
7902 /* check if the item is still selected */
7903 if (infoPtr->bFocus && LISTVIEW_GetItemState(infoPtr, editItem->iItem, LVIS_SELECTED))
7904 LISTVIEW_EditLabelT(infoPtr, editItem->iItem, TRUE);
7909 * Creates the listview control - the WM_NCCREATE phase.
7912 * [I] hwnd : window handle
7913 * [I] lpcs : the create parameters
7919 static LRESULT LISTVIEW_NCCreate(HWND hwnd, const CREATESTRUCTW *lpcs)
7921 LISTVIEW_INFO *infoPtr;
7924 TRACE("(lpcs=%p)\n", lpcs);
7926 /* initialize info pointer */
7927 infoPtr = Alloc(sizeof(LISTVIEW_INFO));
7928 if (!infoPtr) return FALSE;
7930 SetWindowLongPtrW(hwnd, 0, (DWORD_PTR)infoPtr);
7932 infoPtr->hwndSelf = hwnd;
7933 infoPtr->dwStyle = lpcs->style; /* Note: may be changed in WM_CREATE */
7934 /* determine the type of structures to use */
7935 infoPtr->hwndNotify = lpcs->hwndParent;
7936 /* infoPtr->notifyFormat will be filled in WM_CREATE */
7938 /* initialize color information */
7939 infoPtr->clrBk = CLR_NONE;
7940 infoPtr->clrText = CLR_DEFAULT;
7941 infoPtr->clrTextBk = CLR_DEFAULT;
7942 LISTVIEW_SetBkColor(infoPtr, comctl32_color.clrWindow);
7944 /* set default values */
7945 infoPtr->nFocusedItem = -1;
7946 infoPtr->nSelectionMark = -1;
7947 infoPtr->nHotItem = -1;
7948 infoPtr->bRedraw = TRUE;
7949 infoPtr->bNoItemMetrics = TRUE;
7950 infoPtr->bDoChangeNotify = TRUE;
7951 infoPtr->iconSpacing.cx = GetSystemMetrics(SM_CXICONSPACING);
7952 infoPtr->iconSpacing.cy = GetSystemMetrics(SM_CYICONSPACING);
7953 infoPtr->nEditLabelItem = -1;
7954 infoPtr->dwHoverTime = -1; /* default system hover time */
7955 infoPtr->nMeasureItemHeight = 0;
7956 infoPtr->xTrackLine = -1; /* no track line */
7957 infoPtr->itemEdit.fEnabled = FALSE;
7959 /* get default font (icon title) */
7960 SystemParametersInfoW(SPI_GETICONTITLELOGFONT, 0, &logFont, 0);
7961 infoPtr->hDefaultFont = CreateFontIndirectW(&logFont);
7962 infoPtr->hFont = infoPtr->hDefaultFont;
7963 LISTVIEW_SaveTextMetrics(infoPtr);
7965 /* allocate memory for the data structure */
7966 if (!(infoPtr->selectionRanges = ranges_create(10))) goto fail;
7967 if (!(infoPtr->hdpaItems = DPA_Create(10))) goto fail;
7968 if (!(infoPtr->hdpaPosX = DPA_Create(10))) goto fail;
7969 if (!(infoPtr->hdpaPosY = DPA_Create(10))) goto fail;
7970 if (!(infoPtr->hdpaColumns = DPA_Create(10))) goto fail;
7974 DestroyWindow(infoPtr->hwndHeader);
7975 ranges_destroy(infoPtr->selectionRanges);
7976 DPA_Destroy(infoPtr->hdpaItems);
7977 DPA_Destroy(infoPtr->hdpaPosX);
7978 DPA_Destroy(infoPtr->hdpaPosY);
7979 DPA_Destroy(infoPtr->hdpaColumns);
7986 * Creates the listview control - the WM_CREATE phase. Most of the data is
7987 * already set up in LISTVIEW_NCCreate
7990 * [I] hwnd : window handle
7991 * [I] lpcs : the create parameters
7997 static LRESULT LISTVIEW_Create(HWND hwnd, const CREATESTRUCTW *lpcs)
7999 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongPtrW(hwnd, 0);
8000 UINT uView = lpcs->style & LVS_TYPEMASK;
8002 TRACE("(lpcs=%p)\n", lpcs);
8004 infoPtr->dwStyle = lpcs->style;
8005 infoPtr->notifyFormat = SendMessageW(infoPtr->hwndNotify, WM_NOTIFYFORMAT,
8006 (WPARAM)infoPtr->hwndSelf, (LPARAM)NF_QUERY);
8009 infoPtr->hwndHeader = CreateWindowW(WC_HEADERW, NULL,
8010 WS_CHILD | HDS_HORZ | HDS_FULLDRAG | (DWORD)((LVS_NOSORTHEADER & lpcs->style)?0:HDS_BUTTONS),
8011 0, 0, 0, 0, hwnd, NULL,
8012 lpcs->hInstance, NULL);
8013 if (!infoPtr->hwndHeader) return -1;
8015 /* set header unicode format */
8016 SendMessageW(infoPtr->hwndHeader, HDM_SETUNICODEFORMAT, (WPARAM)TRUE, (LPARAM)NULL);
8018 /* set header font */
8019 SendMessageW(infoPtr->hwndHeader, WM_SETFONT, (WPARAM)infoPtr->hFont, (LPARAM)TRUE);
8021 /* init item size to avoid division by 0 */
8022 LISTVIEW_UpdateItemSize (infoPtr);
8024 if (uView == LVS_REPORT)
8026 if (!(LVS_NOCOLUMNHEADER & lpcs->style))
8028 ShowWindow(infoPtr->hwndHeader, SW_SHOWNORMAL);
8032 /* set HDS_HIDDEN flag to hide the header bar */
8033 SetWindowLongW(infoPtr->hwndHeader, GWL_STYLE,
8034 GetWindowLongW(infoPtr->hwndHeader, GWL_STYLE) | HDS_HIDDEN);
8038 OpenThemeData(hwnd, themeClass);
8040 /* initialize the icon sizes */
8041 set_icon_size(&infoPtr->iconSize, infoPtr->himlNormal, uView != LVS_ICON);
8042 set_icon_size(&infoPtr->iconStateSize, infoPtr->himlState, TRUE);
8048 * Destroys the listview control.
8051 * [I] infoPtr : valid pointer to the listview structure
8057 static LRESULT LISTVIEW_Destroy(const LISTVIEW_INFO *infoPtr)
8059 HTHEME theme = GetWindowTheme(infoPtr->hwndSelf);
8060 CloseThemeData(theme);
8066 * Enables the listview control.
8069 * [I] infoPtr : valid pointer to the listview structure
8070 * [I] bEnable : specifies whether to enable or disable the window
8076 static BOOL LISTVIEW_Enable(const LISTVIEW_INFO *infoPtr, BOOL bEnable)
8078 if (infoPtr->dwStyle & LVS_OWNERDRAWFIXED)
8079 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
8085 * Erases the background of the listview control.
8088 * [I] infoPtr : valid pointer to the listview structure
8089 * [I] hdc : device context handle
8095 static inline BOOL LISTVIEW_EraseBkgnd(const LISTVIEW_INFO *infoPtr, HDC hdc)
8099 TRACE("(hdc=%p)\n", hdc);
8101 if (!GetClipBox(hdc, &rc)) return FALSE;
8103 /* for double buffered controls we need to do this during refresh */
8104 if (infoPtr->dwLvExStyle & LVS_EX_DOUBLEBUFFER) return FALSE;
8106 return LISTVIEW_FillBkgnd(infoPtr, hdc, &rc);
8112 * Helper function for LISTVIEW_[HV]Scroll *only*.
8113 * Performs vertical/horizontal scrolling by a give amount.
8116 * [I] infoPtr : valid pointer to the listview structure
8117 * [I] dx : amount of horizontal scroll
8118 * [I] dy : amount of vertical scroll
8120 static void scroll_list(LISTVIEW_INFO *infoPtr, INT dx, INT dy)
8122 /* now we can scroll the list */
8123 ScrollWindowEx(infoPtr->hwndSelf, dx, dy, &infoPtr->rcList,
8124 &infoPtr->rcList, 0, 0, SW_ERASE | SW_INVALIDATE);
8125 /* if we have focus, adjust rect */
8126 OffsetRect(&infoPtr->rcFocus, dx, dy);
8127 UpdateWindow(infoPtr->hwndSelf);
8132 * Performs vertical scrolling.
8135 * [I] infoPtr : valid pointer to the listview structure
8136 * [I] nScrollCode : scroll code
8137 * [I] nScrollDiff : units to scroll in SB_INTERNAL mode, 0 otherwise
8138 * [I] hScrollWnd : scrollbar control window handle
8144 * SB_LINEUP/SB_LINEDOWN:
8145 * for LVS_ICON, LVS_SMALLICON is 37 by experiment
8146 * for LVS_REPORT is 1 line
8147 * for LVS_LIST cannot occur
8150 static LRESULT LISTVIEW_VScroll(LISTVIEW_INFO *infoPtr, INT nScrollCode,
8151 INT nScrollDiff, HWND hScrollWnd)
8153 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
8154 INT nOldScrollPos, nNewScrollPos;
8155 SCROLLINFO scrollInfo;
8158 TRACE("(nScrollCode=%d(%s), nScrollDiff=%d)\n", nScrollCode,
8159 debugscrollcode(nScrollCode), nScrollDiff);
8161 if (infoPtr->hwndEdit) SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
8163 scrollInfo.cbSize = sizeof(SCROLLINFO);
8164 scrollInfo.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_TRACKPOS;
8166 is_an_icon = ((uView == LVS_ICON) || (uView == LVS_SMALLICON));
8168 if (!GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo)) return 1;
8170 nOldScrollPos = scrollInfo.nPos;
8171 switch (nScrollCode)
8177 nScrollDiff = (is_an_icon) ? -LISTVIEW_SCROLL_ICON_LINE_SIZE : -1;
8181 nScrollDiff = (is_an_icon) ? LISTVIEW_SCROLL_ICON_LINE_SIZE : 1;
8185 nScrollDiff = -scrollInfo.nPage;
8189 nScrollDiff = scrollInfo.nPage;
8192 case SB_THUMBPOSITION:
8194 nScrollDiff = scrollInfo.nTrackPos - scrollInfo.nPos;
8201 /* quit right away if pos isn't changing */
8202 if (nScrollDiff == 0) return 0;
8204 /* calculate new position, and handle overflows */
8205 nNewScrollPos = scrollInfo.nPos + nScrollDiff;
8206 if (nScrollDiff > 0) {
8207 if (nNewScrollPos < nOldScrollPos ||
8208 nNewScrollPos > scrollInfo.nMax)
8209 nNewScrollPos = scrollInfo.nMax;
8211 if (nNewScrollPos > nOldScrollPos ||
8212 nNewScrollPos < scrollInfo.nMin)
8213 nNewScrollPos = scrollInfo.nMin;
8216 /* set the new position, and reread in case it changed */
8217 scrollInfo.fMask = SIF_POS;
8218 scrollInfo.nPos = nNewScrollPos;
8219 nNewScrollPos = SetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo, TRUE);
8221 /* carry on only if it really changed */
8222 if (nNewScrollPos == nOldScrollPos) return 0;
8224 /* now adjust to client coordinates */
8225 nScrollDiff = nOldScrollPos - nNewScrollPos;
8226 if (uView == LVS_REPORT) nScrollDiff *= infoPtr->nItemHeight;
8228 /* and scroll the window */
8229 scroll_list(infoPtr, 0, nScrollDiff);
8236 * Performs horizontal scrolling.
8239 * [I] infoPtr : valid pointer to the listview structure
8240 * [I] nScrollCode : scroll code
8241 * [I] nScrollDiff : units to scroll in SB_INTERNAL mode, 0 otherwise
8242 * [I] hScrollWnd : scrollbar control window handle
8248 * SB_LINELEFT/SB_LINERIGHT:
8249 * for LVS_ICON, LVS_SMALLICON 1 pixel
8250 * for LVS_REPORT is 1 pixel
8251 * for LVS_LIST is 1 column --> which is a 1 because the
8252 * scroll is based on columns not pixels
8255 static LRESULT LISTVIEW_HScroll(LISTVIEW_INFO *infoPtr, INT nScrollCode,
8256 INT nScrollDiff, HWND hScrollWnd)
8258 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
8259 INT nOldScrollPos, nNewScrollPos;
8260 SCROLLINFO scrollInfo;
8262 TRACE("(nScrollCode=%d(%s), nScrollDiff=%d)\n", nScrollCode,
8263 debugscrollcode(nScrollCode), nScrollDiff);
8265 if (infoPtr->hwndEdit) SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
8267 scrollInfo.cbSize = sizeof(SCROLLINFO);
8268 scrollInfo.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_TRACKPOS;
8270 if (!GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo)) return 1;
8272 nOldScrollPos = scrollInfo.nPos;
8274 switch (nScrollCode)
8288 nScrollDiff = -scrollInfo.nPage;
8292 nScrollDiff = scrollInfo.nPage;
8295 case SB_THUMBPOSITION:
8297 nScrollDiff = scrollInfo.nTrackPos - scrollInfo.nPos;
8304 /* quit right away if pos isn't changing */
8305 if (nScrollDiff == 0) return 0;
8307 /* calculate new position, and handle overflows */
8308 nNewScrollPos = scrollInfo.nPos + nScrollDiff;
8309 if (nScrollDiff > 0) {
8310 if (nNewScrollPos < nOldScrollPos ||
8311 nNewScrollPos > scrollInfo.nMax)
8312 nNewScrollPos = scrollInfo.nMax;
8314 if (nNewScrollPos > nOldScrollPos ||
8315 nNewScrollPos < scrollInfo.nMin)
8316 nNewScrollPos = scrollInfo.nMin;
8319 /* set the new position, and reread in case it changed */
8320 scrollInfo.fMask = SIF_POS;
8321 scrollInfo.nPos = nNewScrollPos;
8322 nNewScrollPos = SetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo, TRUE);
8324 /* carry on only if it really changed */
8325 if (nNewScrollPos == nOldScrollPos) return 0;
8327 if(uView == LVS_REPORT)
8328 LISTVIEW_UpdateHeaderSize(infoPtr, nNewScrollPos);
8330 /* now adjust to client coordinates */
8331 nScrollDiff = nOldScrollPos - nNewScrollPos;
8332 if (uView == LVS_LIST) nScrollDiff *= infoPtr->nItemWidth;
8334 /* and scroll the window */
8335 scroll_list(infoPtr, nScrollDiff, 0);
8340 static LRESULT LISTVIEW_MouseWheel(LISTVIEW_INFO *infoPtr, INT wheelDelta)
8342 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
8343 INT gcWheelDelta = 0;
8344 INT pulScrollLines = 3;
8345 SCROLLINFO scrollInfo;
8347 TRACE("(wheelDelta=%d)\n", wheelDelta);
8349 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
8350 gcWheelDelta -= wheelDelta;
8352 scrollInfo.cbSize = sizeof(SCROLLINFO);
8353 scrollInfo.fMask = SIF_POS;
8360 * listview should be scrolled by a multiple of 37 dependently on its dimension or its visible item number
8361 * should be fixed in the future.
8363 LISTVIEW_VScroll(infoPtr, SB_INTERNAL, (gcWheelDelta < 0) ?
8364 -LISTVIEW_SCROLL_ICON_LINE_SIZE : LISTVIEW_SCROLL_ICON_LINE_SIZE, 0);
8368 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
8370 int cLineScroll = min(LISTVIEW_GetCountPerColumn(infoPtr), pulScrollLines);
8371 cLineScroll *= (gcWheelDelta / WHEEL_DELTA);
8372 LISTVIEW_VScroll(infoPtr, SB_INTERNAL, cLineScroll, 0);
8377 LISTVIEW_HScroll(infoPtr, (gcWheelDelta < 0) ? SB_LINELEFT : SB_LINERIGHT, 0, 0);
8388 * [I] infoPtr : valid pointer to the listview structure
8389 * [I] nVirtualKey : virtual key
8390 * [I] lKeyData : key data
8395 static LRESULT LISTVIEW_KeyDown(LISTVIEW_INFO *infoPtr, INT nVirtualKey, LONG lKeyData)
8397 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
8398 HWND hwndSelf = infoPtr->hwndSelf;
8400 NMLVKEYDOWN nmKeyDown;
8402 TRACE("(nVirtualKey=%d, lKeyData=%d)\n", nVirtualKey, lKeyData);
8404 /* send LVN_KEYDOWN notification */
8405 nmKeyDown.wVKey = nVirtualKey;
8406 nmKeyDown.flags = 0;
8407 notify_hdr(infoPtr, LVN_KEYDOWN, &nmKeyDown.hdr);
8408 if (!IsWindow(hwndSelf))
8411 switch (nVirtualKey)
8414 nItem = infoPtr->nFocusedItem;
8415 if (infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES)
8416 toggle_checkbox_state(infoPtr, infoPtr->nFocusedItem);
8420 if ((infoPtr->nItemCount > 0) && (infoPtr->nFocusedItem != -1))
8422 if (!notify(infoPtr, NM_RETURN)) return 0;
8423 if (!notify(infoPtr, LVN_ITEMACTIVATE)) return 0;
8428 if (infoPtr->nItemCount > 0)
8433 if (infoPtr->nItemCount > 0)
8434 nItem = infoPtr->nItemCount - 1;
8438 nItem = ListView_GetNextItem(infoPtr->hwndSelf, infoPtr->nFocusedItem, LVNI_TOLEFT);
8442 nItem = ListView_GetNextItem(infoPtr->hwndSelf, infoPtr->nFocusedItem, LVNI_ABOVE);
8446 nItem = ListView_GetNextItem(infoPtr->hwndSelf, infoPtr->nFocusedItem, LVNI_TORIGHT);
8450 nItem = ListView_GetNextItem(infoPtr->hwndSelf, infoPtr->nFocusedItem, LVNI_BELOW);
8454 if (uView == LVS_REPORT)
8456 INT topidx = LISTVIEW_GetTopIndex(infoPtr);
8457 if (infoPtr->nFocusedItem == topidx)
8458 nItem = topidx - LISTVIEW_GetCountPerColumn(infoPtr) + 1;
8463 nItem = infoPtr->nFocusedItem - LISTVIEW_GetCountPerColumn(infoPtr)
8464 * LISTVIEW_GetCountPerRow(infoPtr);
8465 if(nItem < 0) nItem = 0;
8469 if (uView == LVS_REPORT)
8471 INT topidx = LISTVIEW_GetTopIndex(infoPtr);
8472 INT cnt = LISTVIEW_GetCountPerColumn(infoPtr);
8473 if (infoPtr->nFocusedItem == topidx + cnt - 1)
8474 nItem = infoPtr->nFocusedItem + cnt - 1;
8476 nItem = topidx + cnt - 1;
8479 nItem = infoPtr->nFocusedItem + LISTVIEW_GetCountPerColumn(infoPtr)
8480 * LISTVIEW_GetCountPerRow(infoPtr);
8481 if(nItem >= infoPtr->nItemCount) nItem = infoPtr->nItemCount - 1;
8485 if ((nItem != -1) && (nItem != infoPtr->nFocusedItem || nVirtualKey == VK_SPACE))
8486 LISTVIEW_KeySelection(infoPtr, nItem);
8496 * [I] infoPtr : valid pointer to the listview structure
8501 static LRESULT LISTVIEW_KillFocus(LISTVIEW_INFO *infoPtr)
8505 /* if we did not have the focus, there's nothing to do */
8506 if (!infoPtr->bFocus) return 0;
8508 /* send NM_KILLFOCUS notification */
8509 if (!notify(infoPtr, NM_KILLFOCUS)) return 0;
8511 /* if we have a focus rectagle, get rid of it */
8512 LISTVIEW_ShowFocusRect(infoPtr, FALSE);
8514 /* set window focus flag */
8515 infoPtr->bFocus = FALSE;
8517 /* invalidate the selected items before resetting focus flag */
8518 LISTVIEW_InvalidateSelectedItems(infoPtr);
8525 * Processes double click messages (left mouse button).
8528 * [I] infoPtr : valid pointer to the listview structure
8529 * [I] wKey : key flag
8530 * [I] x,y : mouse coordinate
8535 static LRESULT LISTVIEW_LButtonDblClk(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
8537 LVHITTESTINFO htInfo;
8539 TRACE("(key=%hu, X=%hu, Y=%hu)\n", wKey, x, y);
8541 /* Cancel the item edition if any */
8542 if (infoPtr->itemEdit.fEnabled)
8544 KillTimer(infoPtr->hwndSelf, (UINT_PTR)&infoPtr->itemEdit);
8545 infoPtr->itemEdit.fEnabled = FALSE;
8548 /* send NM_RELEASEDCAPTURE notification */
8549 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
8554 /* send NM_DBLCLK notification */
8555 LISTVIEW_HitTest(infoPtr, &htInfo, TRUE, FALSE);
8556 if (!notify_click(infoPtr, NM_DBLCLK, &htInfo)) return 0;
8558 /* To send the LVN_ITEMACTIVATE, it must be on an Item */
8559 if(htInfo.iItem != -1) notify_itemactivate(infoPtr,&htInfo);
8566 * Processes mouse down messages (left mouse button).
8569 * infoPtr [I ] valid pointer to the listview structure
8570 * wKey [I ] key flag
8571 * x,y [I ] mouse coordinate
8576 static LRESULT LISTVIEW_LButtonDown(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
8578 LVHITTESTINFO lvHitTestInfo;
8579 static BOOL bGroupSelect = TRUE;
8580 BOOL bReceivedFocus = FALSE;
8581 POINT pt = { x, y };
8584 TRACE("(key=%hu, X=%hu, Y=%hu)\n", wKey, x, y);
8586 /* send NM_RELEASEDCAPTURE notification */
8587 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
8589 if (!infoPtr->bFocus)
8591 bReceivedFocus = TRUE;
8592 SetFocus(infoPtr->hwndSelf);
8595 /* set left button down flag and record the click position */
8596 infoPtr->bLButtonDown = TRUE;
8597 infoPtr->ptClickPos = pt;
8599 lvHitTestInfo.pt.x = x;
8600 lvHitTestInfo.pt.y = y;
8602 nItem = LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, TRUE);
8603 TRACE("at %s, nItem=%d\n", wine_dbgstr_point(&pt), nItem);
8604 infoPtr->nEditLabelItem = -1;
8605 if ((nItem >= 0) && (nItem < infoPtr->nItemCount))
8607 if ((infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES) && (lvHitTestInfo.flags & LVHT_ONITEMSTATEICON))
8609 toggle_checkbox_state(infoPtr, nItem);
8613 if (infoPtr->dwStyle & LVS_SINGLESEL)
8615 if (LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED))
8616 infoPtr->nEditLabelItem = nItem;
8618 LISTVIEW_SetSelection(infoPtr, nItem);
8622 if ((wKey & MK_CONTROL) && (wKey & MK_SHIFT))
8626 if (!LISTVIEW_AddGroupSelection(infoPtr, nItem)) return 0;
8627 LISTVIEW_SetItemFocus(infoPtr, nItem);
8628 infoPtr->nSelectionMark = nItem;
8634 item.state = LVIS_SELECTED | LVIS_FOCUSED;
8635 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
8637 LISTVIEW_SetItemState(infoPtr,nItem,&item);
8638 infoPtr->nSelectionMark = nItem;
8641 else if (wKey & MK_CONTROL)
8645 bGroupSelect = (LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED) == 0);
8647 item.state = (bGroupSelect ? LVIS_SELECTED : 0) | LVIS_FOCUSED;
8648 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
8649 LISTVIEW_SetItemState(infoPtr, nItem, &item);
8650 infoPtr->nSelectionMark = nItem;
8652 else if (wKey & MK_SHIFT)
8654 LISTVIEW_SetGroupSelection(infoPtr, nItem);
8658 if (LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED))
8659 infoPtr->nEditLabelItem = nItem;
8661 /* set selection (clears other pre-existing selections) */
8662 LISTVIEW_SetSelection(infoPtr, nItem);
8668 /* remove all selections */
8669 LISTVIEW_DeselectAll(infoPtr);
8674 infoPtr->nEditLabelItem = -1;
8681 * Processes mouse up messages (left mouse button).
8684 * infoPtr [I ] valid pointer to the listview structure
8685 * wKey [I ] key flag
8686 * x,y [I ] mouse coordinate
8691 static LRESULT LISTVIEW_LButtonUp(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
8693 LVHITTESTINFO lvHitTestInfo;
8695 TRACE("(key=%hu, X=%hu, Y=%hu)\n", wKey, x, y);
8697 if (!infoPtr->bLButtonDown) return 0;
8699 lvHitTestInfo.pt.x = x;
8700 lvHitTestInfo.pt.y = y;
8702 /* send NM_CLICK notification */
8703 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
8704 if (!notify_click(infoPtr, NM_CLICK, &lvHitTestInfo)) return 0;
8706 /* set left button flag */
8707 infoPtr->bLButtonDown = FALSE;
8709 /* if we clicked on a selected item, edit the label */
8710 if(lvHitTestInfo.iItem == infoPtr->nEditLabelItem && (lvHitTestInfo.flags & LVHT_ONITEMLABEL))
8712 /* we want to make sure the user doesn't want to do a double click. So we will
8713 * delay the edit. WM_LBUTTONDBLCLICK will cancel the timer
8715 infoPtr->itemEdit.fEnabled = TRUE;
8716 infoPtr->itemEdit.iItem = lvHitTestInfo.iItem;
8717 SetTimer(infoPtr->hwndSelf,
8718 (UINT_PTR)&infoPtr->itemEdit,
8719 GetDoubleClickTime(),
8720 LISTVIEW_DelayedEditItem);
8728 * Destroys the listview control (called after WM_DESTROY).
8731 * [I] infoPtr : valid pointer to the listview structure
8736 static LRESULT LISTVIEW_NCDestroy(LISTVIEW_INFO *infoPtr)
8740 /* delete all items */
8741 LISTVIEW_DeleteAllItems(infoPtr, TRUE);
8743 /* destroy data structure */
8744 DPA_Destroy(infoPtr->hdpaItems);
8745 DPA_Destroy(infoPtr->hdpaPosX);
8746 DPA_Destroy(infoPtr->hdpaPosY);
8747 DPA_Destroy(infoPtr->hdpaColumns);
8748 ranges_destroy(infoPtr->selectionRanges);
8750 /* destroy image lists */
8751 if (!(infoPtr->dwStyle & LVS_SHAREIMAGELISTS))
8753 if (infoPtr->himlNormal)
8754 ImageList_Destroy(infoPtr->himlNormal);
8755 if (infoPtr->himlSmall)
8756 ImageList_Destroy(infoPtr->himlSmall);
8757 if (infoPtr->himlState)
8758 ImageList_Destroy(infoPtr->himlState);
8761 /* destroy font, bkgnd brush */
8763 if (infoPtr->hDefaultFont) DeleteObject(infoPtr->hDefaultFont);
8764 if (infoPtr->clrBk != CLR_NONE) DeleteObject(infoPtr->hBkBrush);
8766 SetWindowLongPtrW(infoPtr->hwndSelf, 0, 0);
8768 /* free listview info pointer*/
8776 * Handles notifications from header.
8779 * [I] infoPtr : valid pointer to the listview structure
8780 * [I] nCtrlId : control identifier
8781 * [I] lpnmh : notification information
8786 static LRESULT LISTVIEW_HeaderNotification(LISTVIEW_INFO *infoPtr, const NMHEADERW *lpnmh)
8788 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
8789 HWND hwndSelf = infoPtr->hwndSelf;
8791 TRACE("(lpnmh=%p)\n", lpnmh);
8793 if (!lpnmh || lpnmh->iItem < 0 || lpnmh->iItem >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return 0;
8795 switch (lpnmh->hdr.code)
8800 COLUMN_INFO *lpColumnInfo;
8804 if (!lpnmh->pitem || !(lpnmh->pitem->mask & HDI_WIDTH))
8807 /* remove the old line (if any) */
8808 LISTVIEW_DrawTrackLine(infoPtr);
8810 /* compute & draw the new line */
8811 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, lpnmh->iItem);
8812 x = lpColumnInfo->rcHeader.left + lpnmh->pitem->cxy;
8813 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
8814 infoPtr->xTrackLine = x + ptOrigin.x;
8815 LISTVIEW_DrawTrackLine(infoPtr);
8821 /* remove the track line (if any) */
8822 LISTVIEW_DrawTrackLine(infoPtr);
8823 infoPtr->xTrackLine = -1;
8827 FIXME("Changing column order not implemented\n");
8830 case HDN_ITEMCHANGINGW:
8831 case HDN_ITEMCHANGINGA:
8832 return notify_forward_header(infoPtr, lpnmh);
8834 case HDN_ITEMCHANGEDW:
8835 case HDN_ITEMCHANGEDA:
8837 COLUMN_INFO *lpColumnInfo;
8840 notify_forward_header(infoPtr, lpnmh);
8841 if (!IsWindow(hwndSelf))
8844 if (!lpnmh->pitem || !(lpnmh->pitem->mask & HDI_WIDTH))
8848 hdi.mask = HDI_WIDTH;
8849 if (!Header_GetItemW(infoPtr->hwndHeader, lpnmh->iItem, &hdi)) return 0;
8853 cxy = lpnmh->pitem->cxy;
8855 /* determine how much we change since the last know position */
8856 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, lpnmh->iItem);
8857 dx = cxy - (lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left);
8860 lpColumnInfo->rcHeader.right += dx;
8861 if (lpnmh->iItem + 1 < DPA_GetPtrCount(infoPtr->hdpaColumns))
8862 LISTVIEW_ScrollColumns(infoPtr, lpnmh->iItem + 1, dx);
8865 /* only needs to update the scrolls */
8866 infoPtr->nItemWidth += dx;
8867 LISTVIEW_UpdateScroll(infoPtr);
8869 LISTVIEW_UpdateItemSize(infoPtr);
8870 if (uView == LVS_REPORT && is_redrawing(infoPtr))
8873 RECT rcCol = lpColumnInfo->rcHeader;
8875 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
8876 OffsetRect(&rcCol, ptOrigin.x, 0);
8878 rcCol.top = infoPtr->rcList.top;
8879 rcCol.bottom = infoPtr->rcList.bottom;
8881 /* resizing left-aligned columns leaves most of the left side untouched */
8882 if ((lpColumnInfo->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_LEFT)
8884 INT nMaxDirty = infoPtr->nEllipsisWidth + infoPtr->ntmMaxCharWidth;
8887 rcCol.left = max (rcCol.left, rcCol.right - nMaxDirty);
8890 /* when shrinking the last column clear the now unused field */
8891 if (lpnmh->iItem == DPA_GetPtrCount(infoPtr->hdpaColumns) - 1 && dx < 0)
8894 LISTVIEW_InvalidateRect(infoPtr, &rcCol);
8900 case HDN_ITEMCLICKW:
8901 case HDN_ITEMCLICKA:
8903 /* Handle sorting by Header Column */
8906 ZeroMemory(&nmlv, sizeof(NMLISTVIEW));
8908 nmlv.iSubItem = lpnmh->iItem;
8909 notify_listview(infoPtr, LVN_COLUMNCLICK, &nmlv);
8913 case HDN_DIVIDERDBLCLICKW:
8914 case HDN_DIVIDERDBLCLICKA:
8915 LISTVIEW_SetColumnWidth(infoPtr, lpnmh->iItem, LVSCW_AUTOSIZE);
8924 * Paint non-client area of control.
8927 * [I] infoPtr : valid pointer to the listview structureof the sender
8928 * [I] region : update region
8931 * TRUE - frame was painted
8932 * FALSE - call default window proc
8934 static BOOL LISTVIEW_NCPaint(const LISTVIEW_INFO *infoPtr, HRGN region)
8936 HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
8940 int cxEdge = GetSystemMetrics (SM_CXEDGE),
8941 cyEdge = GetSystemMetrics (SM_CYEDGE);
8943 if (!theme) return FALSE;
8945 GetWindowRect(infoPtr->hwndSelf, &r);
8947 cliprgn = CreateRectRgn (r.left + cxEdge, r.top + cyEdge,
8948 r.right - cxEdge, r.bottom - cyEdge);
8949 if (region != (HRGN)1)
8950 CombineRgn (cliprgn, cliprgn, region, RGN_AND);
8951 OffsetRect(&r, -r.left, -r.top);
8953 dc = GetDCEx(infoPtr->hwndSelf, region, DCX_WINDOW|DCX_INTERSECTRGN);
8954 OffsetRect(&r, -r.left, -r.top);
8956 if (IsThemeBackgroundPartiallyTransparent (theme, 0, 0))
8957 DrawThemeParentBackground(infoPtr->hwndSelf, dc, &r);
8958 DrawThemeBackground (theme, dc, 0, 0, &r, 0);
8959 ReleaseDC(infoPtr->hwndSelf, dc);
8961 /* Call default proc to get the scrollbars etc. painted */
8962 DefWindowProcW (infoPtr->hwndSelf, WM_NCPAINT, (WPARAM)cliprgn, 0);
8969 * Determines the type of structure to use.
8972 * [I] infoPtr : valid pointer to the listview structureof the sender
8973 * [I] hwndFrom : listview window handle
8974 * [I] nCommand : command specifying the nature of the WM_NOTIFYFORMAT
8979 static LRESULT LISTVIEW_NotifyFormat(LISTVIEW_INFO *infoPtr, HWND hwndFrom, INT nCommand)
8981 TRACE("(hwndFrom=%p, nCommand=%d)\n", hwndFrom, nCommand);
8983 if (nCommand == NF_REQUERY)
8984 infoPtr->notifyFormat = SendMessageW(hwndFrom, WM_NOTIFYFORMAT, (WPARAM)infoPtr->hwndSelf, NF_QUERY);
8986 return infoPtr->notifyFormat;
8991 * Paints/Repaints the listview control.
8994 * [I] infoPtr : valid pointer to the listview structure
8995 * [I] hdc : device context handle
9000 static LRESULT LISTVIEW_Paint(LISTVIEW_INFO *infoPtr, HDC hdc)
9002 TRACE("(hdc=%p)\n", hdc);
9004 if (infoPtr->bNoItemMetrics && infoPtr->nItemCount)
9006 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
9008 infoPtr->bNoItemMetrics = FALSE;
9009 LISTVIEW_UpdateItemSize(infoPtr);
9010 if (uView == LVS_ICON || uView == LVS_SMALLICON)
9011 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
9012 LISTVIEW_UpdateScroll(infoPtr);
9015 UpdateWindow(infoPtr->hwndHeader);
9018 LISTVIEW_Refresh(infoPtr, hdc, NULL);
9023 hdc = BeginPaint(infoPtr->hwndSelf, &ps);
9025 LISTVIEW_Refresh(infoPtr, hdc, ps.fErase ? &ps.rcPaint : NULL);
9026 EndPaint(infoPtr->hwndSelf, &ps);
9035 * Paints/Repaints the listview control.
9038 * [I] infoPtr : valid pointer to the listview structure
9039 * [I] hdc : device context handle
9040 * [I] options : drawing options
9045 static LRESULT LISTVIEW_PrintClient(LISTVIEW_INFO *infoPtr, HDC hdc, DWORD options)
9047 FIXME("Partial Stub: (hdc=%p options=0x%08x)\n", hdc, options);
9049 if ((options & PRF_CHECKVISIBLE) && !IsWindowVisible(infoPtr->hwndSelf))
9052 if (options & PRF_ERASEBKGND)
9053 LISTVIEW_EraseBkgnd(infoPtr, hdc);
9055 if (options & PRF_CLIENT)
9056 LISTVIEW_Paint(infoPtr, hdc);
9064 * Processes double click messages (right mouse button).
9067 * [I] infoPtr : valid pointer to the listview structure
9068 * [I] wKey : key flag
9069 * [I] x,y : mouse coordinate
9074 static LRESULT LISTVIEW_RButtonDblClk(const LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
9076 LVHITTESTINFO lvHitTestInfo;
9078 TRACE("(key=%hu,X=%hu,Y=%hu)\n", wKey, x, y);
9080 /* send NM_RELEASEDCAPTURE notification */
9081 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
9083 /* send NM_RDBLCLK notification */
9084 lvHitTestInfo.pt.x = x;
9085 lvHitTestInfo.pt.y = y;
9086 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
9087 notify_click(infoPtr, NM_RDBLCLK, &lvHitTestInfo);
9094 * Processes mouse down messages (right mouse button).
9097 * [I] infoPtr : valid pointer to the listview structure
9098 * [I] wKey : key flag
9099 * [I] x,y : mouse coordinate
9104 static LRESULT LISTVIEW_RButtonDown(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
9106 LVHITTESTINFO lvHitTestInfo;
9109 TRACE("(key=%hu,X=%hu,Y=%hu)\n", wKey, x, y);
9111 /* send NM_RELEASEDCAPTURE notification */
9112 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
9114 /* make sure the listview control window has the focus */
9115 if (!infoPtr->bFocus) SetFocus(infoPtr->hwndSelf);
9117 /* set right button down flag */
9118 infoPtr->bRButtonDown = TRUE;
9120 /* determine the index of the selected item */
9121 lvHitTestInfo.pt.x = x;
9122 lvHitTestInfo.pt.y = y;
9123 nItem = LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, TRUE);
9125 if ((nItem >= 0) && (nItem < infoPtr->nItemCount))
9127 LISTVIEW_SetItemFocus(infoPtr, nItem);
9128 if (!((wKey & MK_SHIFT) || (wKey & MK_CONTROL)) &&
9129 !LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED))
9130 LISTVIEW_SetSelection(infoPtr, nItem);
9134 LISTVIEW_DeselectAll(infoPtr);
9142 * Processes mouse up messages (right mouse button).
9145 * [I] infoPtr : valid pointer to the listview structure
9146 * [I] wKey : key flag
9147 * [I] x,y : mouse coordinate
9152 static LRESULT LISTVIEW_RButtonUp(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
9154 LVHITTESTINFO lvHitTestInfo;
9157 TRACE("(key=%hu,X=%hu,Y=%hu)\n", wKey, x, y);
9159 if (!infoPtr->bRButtonDown) return 0;
9161 /* set button flag */
9162 infoPtr->bRButtonDown = FALSE;
9164 /* Send NM_RClICK notification */
9165 lvHitTestInfo.pt.x = x;
9166 lvHitTestInfo.pt.y = y;
9167 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
9168 if (!notify_click(infoPtr, NM_RCLICK, &lvHitTestInfo)) return 0;
9170 /* Change to screen coordinate for WM_CONTEXTMENU */
9171 pt = lvHitTestInfo.pt;
9172 ClientToScreen(infoPtr->hwndSelf, &pt);
9174 /* Send a WM_CONTEXTMENU message in response to the RBUTTONUP */
9175 SendMessageW(infoPtr->hwndSelf, WM_CONTEXTMENU,
9176 (WPARAM)infoPtr->hwndSelf, MAKELPARAM(pt.x, pt.y));
9187 * [I] infoPtr : valid pointer to the listview structure
9188 * [I] hwnd : window handle of window containing the cursor
9189 * [I] nHittest : hit-test code
9190 * [I] wMouseMsg : ideintifier of the mouse message
9193 * TRUE if cursor is set
9196 static BOOL LISTVIEW_SetCursor(const LISTVIEW_INFO *infoPtr, HWND hwnd, UINT nHittest, UINT wMouseMsg)
9198 LVHITTESTINFO lvHitTestInfo;
9200 if(!(infoPtr->dwLvExStyle & LVS_EX_TRACKSELECT)) return FALSE;
9202 if(!infoPtr->hHotCursor) return FALSE;
9204 GetCursorPos(&lvHitTestInfo.pt);
9205 if (LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, FALSE, FALSE) < 0) return FALSE;
9207 SetCursor(infoPtr->hHotCursor);
9217 * [I] infoPtr : valid pointer to the listview structure
9218 * [I] hwndLoseFocus : handle of previously focused window
9223 static LRESULT LISTVIEW_SetFocus(LISTVIEW_INFO *infoPtr, HWND hwndLoseFocus)
9225 TRACE("(hwndLoseFocus=%p)\n", hwndLoseFocus);
9227 /* if we have the focus already, there's nothing to do */
9228 if (infoPtr->bFocus) return 0;
9230 /* send NM_SETFOCUS notification */
9231 if (!notify(infoPtr, NM_SETFOCUS)) return 0;
9233 /* set window focus flag */
9234 infoPtr->bFocus = TRUE;
9236 /* put the focus rect back on */
9237 LISTVIEW_ShowFocusRect(infoPtr, TRUE);
9239 /* redraw all visible selected items */
9240 LISTVIEW_InvalidateSelectedItems(infoPtr);
9250 * [I] infoPtr : valid pointer to the listview structure
9251 * [I] fRedraw : font handle
9252 * [I] fRedraw : redraw flag
9257 static LRESULT LISTVIEW_SetFont(LISTVIEW_INFO *infoPtr, HFONT hFont, WORD fRedraw)
9259 HFONT oldFont = infoPtr->hFont;
9261 TRACE("(hfont=%p,redraw=%hu)\n", hFont, fRedraw);
9263 infoPtr->hFont = hFont ? hFont : infoPtr->hDefaultFont;
9264 if (infoPtr->hFont == oldFont) return 0;
9266 LISTVIEW_SaveTextMetrics(infoPtr);
9268 if ((infoPtr->dwStyle & LVS_TYPEMASK) == LVS_REPORT)
9269 SendMessageW(infoPtr->hwndHeader, WM_SETFONT, (WPARAM)hFont, MAKELPARAM(fRedraw, 0));
9271 if (fRedraw) LISTVIEW_InvalidateList(infoPtr);
9278 * Message handling for WM_SETREDRAW.
9279 * For the Listview, it invalidates the entire window (the doc specifies otherwise)
9282 * [I] infoPtr : valid pointer to the listview structure
9283 * [I] bRedraw: state of redraw flag
9286 * DefWinProc return value
9288 static LRESULT LISTVIEW_SetRedraw(LISTVIEW_INFO *infoPtr, BOOL bRedraw)
9290 TRACE("infoPtr->bRedraw=%d, bRedraw=%d\n", infoPtr->bRedraw, bRedraw);
9292 /* we cannot use straight equality here because _any_ non-zero value is TRUE */
9293 if ((infoPtr->bRedraw && bRedraw) || (!infoPtr->bRedraw && !bRedraw)) return 0;
9295 infoPtr->bRedraw = bRedraw;
9297 if(!bRedraw) return 0;
9299 if (is_autoarrange(infoPtr))
9300 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
9301 LISTVIEW_UpdateScroll(infoPtr);
9303 /* despite what the WM_SETREDRAW docs says, apps expect us
9304 * to invalidate the listview here... stupid! */
9305 LISTVIEW_InvalidateList(infoPtr);
9312 * Resizes the listview control. This function processes WM_SIZE
9313 * messages. At this time, the width and height are not used.
9316 * [I] infoPtr : valid pointer to the listview structure
9317 * [I] Width : new width
9318 * [I] Height : new height
9323 static LRESULT LISTVIEW_Size(LISTVIEW_INFO *infoPtr, int Width, int Height)
9325 RECT rcOld = infoPtr->rcList;
9327 TRACE("(width=%d, height=%d)\n", Width, Height);
9329 LISTVIEW_UpdateSize(infoPtr);
9330 if (EqualRect(&rcOld, &infoPtr->rcList)) return 0;
9332 /* do not bother with display related stuff if we're not redrawing */
9333 if (!is_redrawing(infoPtr)) return 0;
9335 if (is_autoarrange(infoPtr))
9336 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
9338 LISTVIEW_UpdateScroll(infoPtr);
9340 /* refresh all only for lists whose height changed significantly */
9341 if ((infoPtr->dwStyle & LVS_TYPEMASK) == LVS_LIST &&
9342 (rcOld.bottom - rcOld.top) / infoPtr->nItemHeight !=
9343 (infoPtr->rcList.bottom - infoPtr->rcList.top) / infoPtr->nItemHeight)
9344 LISTVIEW_InvalidateList(infoPtr);
9351 * Sets the size information.
9354 * [I] infoPtr : valid pointer to the listview structure
9359 static void LISTVIEW_UpdateSize(LISTVIEW_INFO *infoPtr)
9361 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
9363 TRACE("uView=%d, rcList(old)=%s\n", uView, wine_dbgstr_rect(&infoPtr->rcList));
9365 GetClientRect(infoPtr->hwndSelf, &infoPtr->rcList);
9367 if (uView == LVS_LIST)
9369 /* Apparently the "LIST" style is supposed to have the same
9370 * number of items in a column even if there is no scroll bar.
9371 * Since if a scroll bar already exists then the bottom is already
9372 * reduced, only reduce if the scroll bar does not currently exist.
9373 * The "2" is there to mimic the native control. I think it may be
9374 * related to either padding or edges. (GLA 7/2002)
9376 if (!(GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE) & WS_HSCROLL))
9377 infoPtr->rcList.bottom -= GetSystemMetrics(SM_CYHSCROLL);
9378 infoPtr->rcList.bottom = max (infoPtr->rcList.bottom - 2, 0);
9380 else if (uView == LVS_REPORT && !(infoPtr->dwStyle & LVS_NOCOLUMNHEADER))
9385 hl.prc = &infoPtr->rcList;
9387 SendMessageW( infoPtr->hwndHeader, HDM_LAYOUT, 0, (LPARAM)&hl );
9388 TRACE(" wp.flags=0x%08x, wp=%d,%d (%dx%d)\n", wp.flags, wp.x, wp.y, wp.cx, wp.cy);
9389 SetWindowPos(wp.hwnd, wp.hwndInsertAfter, wp.x, wp.y, wp.cx, wp.cy, wp.flags);
9390 TRACE(" after SWP wp=%d,%d (%dx%d)\n", wp.x, wp.y, wp.cx, wp.cy);
9392 infoPtr->rcList.top = max(wp.cy, 0);
9393 infoPtr->rcList.top += (infoPtr->dwLvExStyle & LVS_EX_GRIDLINES) ? 2 : 0;
9396 TRACE(" rcList=%s\n", wine_dbgstr_rect(&infoPtr->rcList));
9401 * Processes WM_STYLECHANGED messages.
9404 * [I] infoPtr : valid pointer to the listview structure
9405 * [I] wStyleType : window style type (normal or extended)
9406 * [I] lpss : window style information
9411 static INT LISTVIEW_StyleChanged(LISTVIEW_INFO *infoPtr, WPARAM wStyleType,
9412 const STYLESTRUCT *lpss)
9414 UINT uNewView = lpss->styleNew & LVS_TYPEMASK;
9415 UINT uOldView = lpss->styleOld & LVS_TYPEMASK;
9417 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
9418 wStyleType, lpss->styleOld, lpss->styleNew);
9420 if (wStyleType != GWL_STYLE) return 0;
9422 /* FIXME: if LVS_NOSORTHEADER changed, update header */
9423 /* what if LVS_OWNERDATA changed? */
9424 /* or LVS_SINGLESEL */
9425 /* or LVS_SORT{AS,DES}CENDING */
9427 infoPtr->dwStyle = lpss->styleNew;
9429 if (((lpss->styleOld & WS_HSCROLL) != 0)&&
9430 ((lpss->styleNew & WS_HSCROLL) == 0))
9431 ShowScrollBar(infoPtr->hwndSelf, SB_HORZ, FALSE);
9433 if (((lpss->styleOld & WS_VSCROLL) != 0)&&
9434 ((lpss->styleNew & WS_VSCROLL) == 0))
9435 ShowScrollBar(infoPtr->hwndSelf, SB_VERT, FALSE);
9437 if (uNewView != uOldView)
9439 SIZE oldIconSize = infoPtr->iconSize;
9442 SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
9443 ShowWindow(infoPtr->hwndHeader, SW_HIDE);
9445 ShowScrollBar(infoPtr->hwndSelf, SB_BOTH, FALSE);
9446 SetRectEmpty(&infoPtr->rcFocus);
9448 himl = (uNewView == LVS_ICON ? infoPtr->himlNormal : infoPtr->himlSmall);
9449 set_icon_size(&infoPtr->iconSize, himl, uNewView != LVS_ICON);
9451 if (uNewView == LVS_ICON)
9453 if ((infoPtr->iconSize.cx != oldIconSize.cx) || (infoPtr->iconSize.cy != oldIconSize.cy))
9455 TRACE("icon old size=(%d,%d), new size=(%d,%d)\n",
9456 oldIconSize.cx, oldIconSize.cy, infoPtr->iconSize.cx, infoPtr->iconSize.cy);
9457 LISTVIEW_SetIconSpacing(infoPtr, 0, 0);
9460 else if (uNewView == LVS_REPORT)
9465 hl.prc = &infoPtr->rcList;
9467 SendMessageW( infoPtr->hwndHeader, HDM_LAYOUT, 0, (LPARAM)&hl );
9468 SetWindowPos(infoPtr->hwndHeader, infoPtr->hwndSelf, wp.x, wp.y, wp.cx, wp.cy, wp.flags);
9471 LISTVIEW_UpdateItemSize(infoPtr);
9474 if (uNewView == LVS_REPORT)
9475 ShowWindow(infoPtr->hwndHeader, (lpss->styleNew & LVS_NOCOLUMNHEADER) ? SW_HIDE : SW_SHOWNORMAL);
9477 if ( (uNewView == LVS_ICON || uNewView == LVS_SMALLICON) &&
9478 (uNewView != uOldView || ((lpss->styleNew ^ lpss->styleOld) & LVS_ALIGNMASK)) )
9479 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
9481 /* update the size of the client area */
9482 LISTVIEW_UpdateSize(infoPtr);
9484 /* add scrollbars if needed */
9485 LISTVIEW_UpdateScroll(infoPtr);
9487 /* invalidate client area + erase background */
9488 LISTVIEW_InvalidateList(infoPtr);
9495 * Window procedure of the listview control.
9498 static LRESULT WINAPI
9499 LISTVIEW_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
9501 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongPtrW(hwnd, 0);
9503 TRACE("(uMsg=%x wParam=%lx lParam=%lx)\n", uMsg, wParam, lParam);
9505 if (!infoPtr && (uMsg != WM_NCCREATE))
9506 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
9510 case LVM_APPROXIMATEVIEWRECT:
9511 return LISTVIEW_ApproximateViewRect(infoPtr, (INT)wParam,
9512 LOWORD(lParam), HIWORD(lParam));
9514 return LISTVIEW_Arrange(infoPtr, (INT)wParam);
9516 /* case LVM_CANCELEDITLABEL: */
9518 case LVM_CREATEDRAGIMAGE:
9519 return (LRESULT)LISTVIEW_CreateDragImage(infoPtr, (INT)wParam, (LPPOINT)lParam);
9521 case LVM_DELETEALLITEMS:
9522 return LISTVIEW_DeleteAllItems(infoPtr, FALSE);
9524 case LVM_DELETECOLUMN:
9525 return LISTVIEW_DeleteColumn(infoPtr, (INT)wParam);
9527 case LVM_DELETEITEM:
9528 return LISTVIEW_DeleteItem(infoPtr, (INT)wParam);
9530 case LVM_EDITLABELW:
9531 return (LRESULT)LISTVIEW_EditLabelT(infoPtr, (INT)wParam, TRUE);
9533 case LVM_EDITLABELA:
9534 return (LRESULT)LISTVIEW_EditLabelT(infoPtr, (INT)wParam, FALSE);
9536 /* case LVM_ENABLEGROUPVIEW: */
9538 case LVM_ENSUREVISIBLE:
9539 return LISTVIEW_EnsureVisible(infoPtr, (INT)wParam, (BOOL)lParam);
9542 return LISTVIEW_FindItemW(infoPtr, (INT)wParam, (LPLVFINDINFOW)lParam);
9545 return LISTVIEW_FindItemA(infoPtr, (INT)wParam, (LPLVFINDINFOA)lParam);
9547 case LVM_GETBKCOLOR:
9548 return infoPtr->clrBk;
9550 /* case LVM_GETBKIMAGE: */
9552 case LVM_GETCALLBACKMASK:
9553 return infoPtr->uCallbackMask;
9555 case LVM_GETCOLUMNA:
9556 return LISTVIEW_GetColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam, FALSE);
9558 case LVM_GETCOLUMNW:
9559 return LISTVIEW_GetColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam, TRUE);
9561 case LVM_GETCOLUMNORDERARRAY:
9562 return LISTVIEW_GetColumnOrderArray(infoPtr, (INT)wParam, (LPINT)lParam);
9564 case LVM_GETCOLUMNWIDTH:
9565 return LISTVIEW_GetColumnWidth(infoPtr, (INT)wParam);
9567 case LVM_GETCOUNTPERPAGE:
9568 return LISTVIEW_GetCountPerPage(infoPtr);
9570 case LVM_GETEDITCONTROL:
9571 return (LRESULT)infoPtr->hwndEdit;
9573 case LVM_GETEXTENDEDLISTVIEWSTYLE:
9574 return infoPtr->dwLvExStyle;
9576 /* case LVM_GETGROUPINFO: */
9578 /* case LVM_GETGROUPMETRICS: */
9581 return (LRESULT)infoPtr->hwndHeader;
9583 case LVM_GETHOTCURSOR:
9584 return (LRESULT)infoPtr->hHotCursor;
9586 case LVM_GETHOTITEM:
9587 return infoPtr->nHotItem;
9589 case LVM_GETHOVERTIME:
9590 return infoPtr->dwHoverTime;
9592 case LVM_GETIMAGELIST:
9593 return (LRESULT)LISTVIEW_GetImageList(infoPtr, (INT)wParam);
9595 /* case LVM_GETINSERTMARK: */
9597 /* case LVM_GETINSERTMARKCOLOR: */
9599 /* case LVM_GETINSERTMARKRECT: */
9601 case LVM_GETISEARCHSTRINGA:
9602 case LVM_GETISEARCHSTRINGW:
9603 FIXME("LVM_GETISEARCHSTRING: unimplemented\n");
9607 return LISTVIEW_GetItemExtT(infoPtr, (LPLVITEMW)lParam, FALSE);
9610 return LISTVIEW_GetItemExtT(infoPtr, (LPLVITEMW)lParam, TRUE);
9612 case LVM_GETITEMCOUNT:
9613 return infoPtr->nItemCount;
9615 case LVM_GETITEMPOSITION:
9616 return LISTVIEW_GetItemPosition(infoPtr, (INT)wParam, (LPPOINT)lParam);
9618 case LVM_GETITEMRECT:
9619 return LISTVIEW_GetItemRect(infoPtr, (INT)wParam, (LPRECT)lParam);
9621 case LVM_GETITEMSPACING:
9622 return LISTVIEW_GetItemSpacing(infoPtr, (BOOL)wParam);
9624 case LVM_GETITEMSTATE:
9625 return LISTVIEW_GetItemState(infoPtr, (INT)wParam, (UINT)lParam);
9627 case LVM_GETITEMTEXTA:
9628 return LISTVIEW_GetItemTextT(infoPtr, (INT)wParam, (LPLVITEMW)lParam, FALSE);
9630 case LVM_GETITEMTEXTW:
9631 return LISTVIEW_GetItemTextT(infoPtr, (INT)wParam, (LPLVITEMW)lParam, TRUE);
9633 case LVM_GETNEXTITEM:
9634 return LISTVIEW_GetNextItem(infoPtr, (INT)wParam, LOWORD(lParam));
9636 case LVM_GETNUMBEROFWORKAREAS:
9637 FIXME("LVM_GETNUMBEROFWORKAREAS: unimplemented\n");
9641 if (!lParam) return FALSE;
9642 if ((infoPtr->dwStyle & LVS_TYPEMASK) == LVS_REPORT ||
9643 (infoPtr->dwStyle & LVS_TYPEMASK) == LVS_LIST) return FALSE;
9644 LISTVIEW_GetOrigin(infoPtr, (LPPOINT)lParam);
9647 /* case LVM_GETOUTLINECOLOR: */
9649 /* case LVM_GETSELECTEDCOLUMN: */
9651 case LVM_GETSELECTEDCOUNT:
9652 return LISTVIEW_GetSelectedCount(infoPtr);
9654 case LVM_GETSELECTIONMARK:
9655 return infoPtr->nSelectionMark;
9657 case LVM_GETSTRINGWIDTHA:
9658 return LISTVIEW_GetStringWidthT(infoPtr, (LPCWSTR)lParam, FALSE);
9660 case LVM_GETSTRINGWIDTHW:
9661 return LISTVIEW_GetStringWidthT(infoPtr, (LPCWSTR)lParam, TRUE);
9663 case LVM_GETSUBITEMRECT:
9664 return LISTVIEW_GetSubItemRect(infoPtr, (UINT)wParam, (LPRECT)lParam);
9666 case LVM_GETTEXTBKCOLOR:
9667 return infoPtr->clrTextBk;
9669 case LVM_GETTEXTCOLOR:
9670 return infoPtr->clrText;
9672 /* case LVM_GETTILEINFO: */
9674 /* case LVM_GETTILEVIEWINFO: */
9676 case LVM_GETTOOLTIPS:
9677 if( !infoPtr->hwndToolTip )
9678 infoPtr->hwndToolTip = COMCTL32_CreateToolTip( hwnd );
9679 return (LRESULT)infoPtr->hwndToolTip;
9681 case LVM_GETTOPINDEX:
9682 return LISTVIEW_GetTopIndex(infoPtr);
9684 /*case LVM_GETUNICODEFORMAT:
9685 FIXME("LVM_GETUNICODEFORMAT: unimplemented\n");
9688 /* case LVM_GETVIEW: */
9690 case LVM_GETVIEWRECT:
9691 return LISTVIEW_GetViewRect(infoPtr, (LPRECT)lParam);
9693 case LVM_GETWORKAREAS:
9694 FIXME("LVM_GETWORKAREAS: unimplemented\n");
9697 /* case LVM_HASGROUP: */
9700 return LISTVIEW_HitTest(infoPtr, (LPLVHITTESTINFO)lParam, FALSE, FALSE);
9702 case LVM_INSERTCOLUMNA:
9703 return LISTVIEW_InsertColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam, FALSE);
9705 case LVM_INSERTCOLUMNW:
9706 return LISTVIEW_InsertColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam, TRUE);
9708 /* case LVM_INSERTGROUP: */
9710 /* case LVM_INSERTGROUPSORTED: */
9712 case LVM_INSERTITEMA:
9713 return LISTVIEW_InsertItemT(infoPtr, (LPLVITEMW)lParam, FALSE);
9715 case LVM_INSERTITEMW:
9716 return LISTVIEW_InsertItemT(infoPtr, (LPLVITEMW)lParam, TRUE);
9718 /* case LVM_INSERTMARKHITTEST: */
9720 /* case LVM_ISGROUPVIEWENABLED: */
9722 /* case LVM_MAPIDTOINDEX: */
9724 /* case LVM_MAPINDEXTOID: */
9726 /* case LVM_MOVEGROUP: */
9728 /* case LVM_MOVEITEMTOGROUP: */
9730 case LVM_REDRAWITEMS:
9731 return LISTVIEW_RedrawItems(infoPtr, (INT)wParam, (INT)lParam);
9733 /* case LVM_REMOVEALLGROUPS: */
9735 /* case LVM_REMOVEGROUP: */
9738 return LISTVIEW_Scroll(infoPtr, (INT)wParam, (INT)lParam);
9740 case LVM_SETBKCOLOR:
9741 return LISTVIEW_SetBkColor(infoPtr, (COLORREF)lParam);
9743 /* case LVM_SETBKIMAGE: */
9745 case LVM_SETCALLBACKMASK:
9746 infoPtr->uCallbackMask = (UINT)wParam;
9749 case LVM_SETCOLUMNA:
9750 return LISTVIEW_SetColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam, FALSE);
9752 case LVM_SETCOLUMNW:
9753 return LISTVIEW_SetColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam, TRUE);
9755 case LVM_SETCOLUMNORDERARRAY:
9756 return LISTVIEW_SetColumnOrderArray(infoPtr, (INT)wParam, (LPINT)lParam);
9758 case LVM_SETCOLUMNWIDTH:
9759 return LISTVIEW_SetColumnWidth(infoPtr, (INT)wParam, (short)LOWORD(lParam));
9761 case LVM_SETEXTENDEDLISTVIEWSTYLE:
9762 return LISTVIEW_SetExtendedListViewStyle(infoPtr, (DWORD)wParam, (DWORD)lParam);
9764 /* case LVM_SETGROUPINFO: */
9766 /* case LVM_SETGROUPMETRICS: */
9768 case LVM_SETHOTCURSOR:
9769 return (LRESULT)LISTVIEW_SetHotCursor(infoPtr, (HCURSOR)lParam);
9771 case LVM_SETHOTITEM:
9772 return LISTVIEW_SetHotItem(infoPtr, (INT)wParam);
9774 case LVM_SETHOVERTIME:
9775 return LISTVIEW_SetHoverTime(infoPtr, (DWORD)wParam);
9777 case LVM_SETICONSPACING:
9778 return LISTVIEW_SetIconSpacing(infoPtr, (short)LOWORD(lParam), (short)HIWORD(lParam));
9780 case LVM_SETIMAGELIST:
9781 return (LRESULT)LISTVIEW_SetImageList(infoPtr, (INT)wParam, (HIMAGELIST)lParam);
9783 /* case LVM_SETINFOTIP: */
9785 /* case LVM_SETINSERTMARK: */
9787 /* case LVM_SETINSERTMARKCOLOR: */
9790 return LISTVIEW_SetItemT(infoPtr, (LPLVITEMW)lParam, FALSE);
9793 return LISTVIEW_SetItemT(infoPtr, (LPLVITEMW)lParam, TRUE);
9795 case LVM_SETITEMCOUNT:
9796 return LISTVIEW_SetItemCount(infoPtr, (INT)wParam, (DWORD)lParam);
9798 case LVM_SETITEMPOSITION:
9801 pt.x = (short)LOWORD(lParam);
9802 pt.y = (short)HIWORD(lParam);
9803 return LISTVIEW_SetItemPosition(infoPtr, (INT)wParam, pt);
9806 case LVM_SETITEMPOSITION32:
9807 if (lParam == 0) return FALSE;
9808 return LISTVIEW_SetItemPosition(infoPtr, (INT)wParam, *((POINT*)lParam));
9810 case LVM_SETITEMSTATE:
9811 return LISTVIEW_SetItemState(infoPtr, (INT)wParam, (LPLVITEMW)lParam);
9813 case LVM_SETITEMTEXTA:
9814 return LISTVIEW_SetItemTextT(infoPtr, (INT)wParam, (LPLVITEMW)lParam, FALSE);
9816 case LVM_SETITEMTEXTW:
9817 return LISTVIEW_SetItemTextT(infoPtr, (INT)wParam, (LPLVITEMW)lParam, TRUE);
9819 /* case LVM_SETOUTLINECOLOR: */
9821 /* case LVM_SETSELECTEDCOLUMN: */
9823 case LVM_SETSELECTIONMARK:
9824 return LISTVIEW_SetSelectionMark(infoPtr, (INT)lParam);
9826 case LVM_SETTEXTBKCOLOR:
9827 return LISTVIEW_SetTextBkColor(infoPtr, (COLORREF)lParam);
9829 case LVM_SETTEXTCOLOR:
9830 return LISTVIEW_SetTextColor(infoPtr, (COLORREF)lParam);
9832 /* case LVM_SETTILEINFO: */
9834 /* case LVM_SETTILEVIEWINFO: */
9836 /* case LVM_SETTILEWIDTH: */
9838 case LVM_SETTOOLTIPS:
9839 return (LRESULT)LISTVIEW_SetToolTips(infoPtr, (HWND)lParam);
9841 case LVM_SETUNICODEFORMAT:
9842 return LISTVIEW_SetUnicodeFormat(infoPtr, wParam);
9844 /* case LVM_SETVIEW: */
9846 /* case LVM_SETWORKAREAS: */
9848 /* case LVM_SORTGROUPS: */
9851 return LISTVIEW_SortItems(infoPtr, (PFNLVCOMPARE)lParam, (LPARAM)wParam);
9853 /* LVM_SORTITEMSEX: */
9855 case LVM_SUBITEMHITTEST:
9856 return LISTVIEW_HitTest(infoPtr, (LPLVHITTESTINFO)lParam, TRUE, FALSE);
9859 return LISTVIEW_Update(infoPtr, (INT)wParam);
9862 return LISTVIEW_ProcessLetterKeys( infoPtr, wParam, lParam );
9865 return LISTVIEW_Command(infoPtr, wParam, lParam);
9868 return LISTVIEW_NCCreate(hwnd, (LPCREATESTRUCTW)lParam);
9871 return LISTVIEW_Create(hwnd, (LPCREATESTRUCTW)lParam);
9874 return LISTVIEW_Destroy(infoPtr);
9877 return LISTVIEW_Enable(infoPtr, (BOOL)wParam);
9880 return LISTVIEW_EraseBkgnd(infoPtr, (HDC)wParam);
9883 return DLGC_WANTCHARS | DLGC_WANTARROWS;
9886 return (LRESULT)infoPtr->hFont;
9889 return LISTVIEW_HScroll(infoPtr, (INT)LOWORD(wParam), 0, (HWND)lParam);
9892 return LISTVIEW_KeyDown(infoPtr, (INT)wParam, (LONG)lParam);
9895 return LISTVIEW_KillFocus(infoPtr);
9897 case WM_LBUTTONDBLCLK:
9898 return LISTVIEW_LButtonDblClk(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
9900 case WM_LBUTTONDOWN:
9901 return LISTVIEW_LButtonDown(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
9904 return LISTVIEW_LButtonUp(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
9907 return LISTVIEW_MouseMove (infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
9910 return LISTVIEW_MouseHover(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
9913 return LISTVIEW_NCDestroy(infoPtr);
9916 if (LISTVIEW_NCPaint(infoPtr, (HRGN)wParam))
9921 if (lParam && ((LPNMHDR)lParam)->hwndFrom == infoPtr->hwndHeader)
9922 return LISTVIEW_HeaderNotification(infoPtr, (LPNMHEADERW)lParam);
9925 case WM_NOTIFYFORMAT:
9926 return LISTVIEW_NotifyFormat(infoPtr, (HWND)wParam, (INT)lParam);
9928 case WM_PRINTCLIENT:
9929 return LISTVIEW_PrintClient(infoPtr, (HDC)wParam, (DWORD)lParam);
9932 return LISTVIEW_Paint(infoPtr, (HDC)wParam);
9934 case WM_RBUTTONDBLCLK:
9935 return LISTVIEW_RButtonDblClk(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
9937 case WM_RBUTTONDOWN:
9938 return LISTVIEW_RButtonDown(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
9941 return LISTVIEW_RButtonUp(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
9944 if(LISTVIEW_SetCursor(infoPtr, (HWND)wParam, LOWORD(lParam), HIWORD(lParam)))
9949 return LISTVIEW_SetFocus(infoPtr, (HWND)wParam);
9952 return LISTVIEW_SetFont(infoPtr, (HFONT)wParam, (WORD)lParam);
9955 return LISTVIEW_SetRedraw(infoPtr, (BOOL)wParam);
9958 return LISTVIEW_Size(infoPtr, (short)LOWORD(lParam), (short)HIWORD(lParam));
9960 case WM_STYLECHANGED:
9961 return LISTVIEW_StyleChanged(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
9963 case WM_SYSCOLORCHANGE:
9964 COMCTL32_RefreshSysColors();
9967 /* case WM_TIMER: */
9968 case WM_THEMECHANGED:
9969 return LISTVIEW_ThemeChanged(infoPtr);
9972 return LISTVIEW_VScroll(infoPtr, (INT)LOWORD(wParam), 0, (HWND)lParam);
9975 if (wParam & (MK_SHIFT | MK_CONTROL))
9976 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
9977 return LISTVIEW_MouseWheel(infoPtr, (short int)HIWORD(wParam));
9979 case WM_WINDOWPOSCHANGED:
9980 if (!(((WINDOWPOS *)lParam)->flags & SWP_NOSIZE))
9982 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
9983 SetWindowPos(infoPtr->hwndSelf, 0, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOACTIVATE |
9984 SWP_NOZORDER | SWP_NOMOVE | SWP_NOSIZE);
9986 if ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (uView == LVS_REPORT))
9988 MEASUREITEMSTRUCT mis;
9989 mis.CtlType = ODT_LISTVIEW;
9990 mis.CtlID = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
9994 mis.itemHeight= infoPtr->nItemHeight;
9995 SendMessageW(infoPtr->hwndNotify, WM_MEASUREITEM, mis.CtlID, (LPARAM)&mis);
9996 if (infoPtr->nItemHeight != max(mis.itemHeight, 1))
9997 infoPtr->nMeasureItemHeight = infoPtr->nItemHeight = max(mis.itemHeight, 1);
10000 LISTVIEW_UpdateSize(infoPtr);
10001 LISTVIEW_UpdateScroll(infoPtr);
10003 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
10005 /* case WM_WININICHANGE: */
10008 if ((uMsg >= WM_USER) && (uMsg < WM_APP))
10009 ERR("unknown msg %04x wp=%08lx lp=%08lx\n", uMsg, wParam, lParam);
10012 /* call default window procedure */
10013 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
10020 * Registers the window class.
10028 void LISTVIEW_Register(void)
10030 WNDCLASSW wndClass;
10032 ZeroMemory(&wndClass, sizeof(WNDCLASSW));
10033 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS;
10034 wndClass.lpfnWndProc = LISTVIEW_WindowProc;
10035 wndClass.cbClsExtra = 0;
10036 wndClass.cbWndExtra = sizeof(LISTVIEW_INFO *);
10037 wndClass.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
10038 wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
10039 wndClass.lpszClassName = WC_LISTVIEWW;
10040 RegisterClassW(&wndClass);
10045 * Unregisters the window class.
10053 void LISTVIEW_Unregister(void)
10055 UnregisterClassW(WC_LISTVIEWW, NULL);
10060 * Handle any WM_COMMAND messages
10063 * [I] infoPtr : valid pointer to the listview structure
10064 * [I] wParam : the first message parameter
10065 * [I] lParam : the second message parameter
10070 static LRESULT LISTVIEW_Command(const LISTVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
10072 switch (HIWORD(wParam))
10077 * Adjust the edit window size
10079 WCHAR buffer[1024];
10080 HDC hdc = GetDC(infoPtr->hwndEdit);
10081 HFONT hFont, hOldFont = 0;
10085 if (!infoPtr->hwndEdit || !hdc) return 0;
10086 GetWindowTextW(infoPtr->hwndEdit, buffer, sizeof(buffer)/sizeof(buffer[0]));
10087 GetWindowRect(infoPtr->hwndEdit, &rect);
10089 /* Select font to get the right dimension of the string */
10090 hFont = (HFONT)SendMessageW(infoPtr->hwndEdit, WM_GETFONT, 0, 0);
10093 hOldFont = SelectObject(hdc, hFont);
10096 if (GetTextExtentPoint32W(hdc, buffer, lstrlenW(buffer), &sz))
10098 TEXTMETRICW textMetric;
10100 /* Add Extra spacing for the next character */
10101 GetTextMetricsW(hdc, &textMetric);
10102 sz.cx += (textMetric.tmMaxCharWidth * 2);
10110 rect.bottom - rect.top,
10111 SWP_DRAWFRAME|SWP_NOMOVE);
10114 SelectObject(hdc, hOldFont);
10116 ReleaseDC(infoPtr->hwndEdit, hdc);
10122 return SendMessageW (infoPtr->hwndNotify, WM_COMMAND, wParam, lParam);
10131 * Subclassed edit control windproc function
10134 * [I] hwnd : the edit window handle
10135 * [I] uMsg : the message that is to be processed
10136 * [I] wParam : first message parameter
10137 * [I] lParam : second message parameter
10138 * [I] isW : TRUE if input is Unicode
10143 static LRESULT EditLblWndProcT(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL isW)
10145 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongPtrW(GetParent(hwnd), 0);
10146 BOOL cancel = FALSE;
10148 TRACE("(hwnd=%p, uMsg=%x, wParam=%lx, lParam=%lx, isW=%d)\n",
10149 hwnd, uMsg, wParam, lParam, isW);
10153 case WM_GETDLGCODE:
10154 return DLGC_WANTARROWS | DLGC_WANTALLKEYS;
10161 WNDPROC editProc = infoPtr->EditWndProc;
10162 infoPtr->EditWndProc = 0;
10163 SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (DWORD_PTR)editProc);
10164 return CallWindowProcT(editProc, hwnd, uMsg, wParam, lParam, isW);
10168 if (VK_ESCAPE == (INT)wParam)
10173 else if (VK_RETURN == (INT)wParam)
10177 return CallWindowProcT(infoPtr->EditWndProc, hwnd, uMsg, wParam, lParam, isW);
10180 /* kill the edit */
10181 if (infoPtr->hwndEdit)
10183 LPWSTR buffer = NULL;
10185 infoPtr->hwndEdit = 0;
10188 DWORD len = isW ? GetWindowTextLengthW(hwnd) : GetWindowTextLengthA(hwnd);
10192 if ( (buffer = Alloc((len+1) * (isW ? sizeof(WCHAR) : sizeof(CHAR)))) )
10194 if (isW) GetWindowTextW(hwnd, buffer, len+1);
10195 else GetWindowTextA(hwnd, (CHAR*)buffer, len+1);
10199 LISTVIEW_EndEditLabelT(infoPtr, buffer, isW);
10204 SendMessageW(hwnd, WM_CLOSE, 0, 0);
10210 * Subclassed edit control Unicode windproc function
10213 * [I] hwnd : the edit window handle
10214 * [I] uMsg : the message that is to be processed
10215 * [I] wParam : first message parameter
10216 * [I] lParam : second message parameter
10220 static LRESULT CALLBACK EditLblWndProcW(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
10222 return EditLblWndProcT(hwnd, uMsg, wParam, lParam, TRUE);
10227 * Subclassed edit control ANSI windproc function
10230 * [I] hwnd : the edit window handle
10231 * [I] uMsg : the message that is to be processed
10232 * [I] wParam : first message parameter
10233 * [I] lParam : second message parameter
10237 static LRESULT CALLBACK EditLblWndProcA(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
10239 return EditLblWndProcT(hwnd, uMsg, wParam, lParam, FALSE);
10244 * Creates a subclassed edit control
10247 * [I] infoPtr : valid pointer to the listview structure
10248 * [I] text : initial text for the edit
10249 * [I] style : the window style
10250 * [I] isW : TRUE if input is Unicode
10254 static HWND CreateEditLabelT(LISTVIEW_INFO *infoPtr, LPCWSTR text, DWORD style,
10255 INT x, INT y, INT width, INT height, BOOL isW)
10257 WCHAR editName[5] = { 'E', 'd', 'i', 't', '\0' };
10262 TEXTMETRICW textMetric;
10263 HINSTANCE hinst = (HINSTANCE)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_HINSTANCE);
10265 TRACE("(text=%s, ..., isW=%d)\n", debugtext_t(text, isW), isW);
10267 style |= WS_CHILDWINDOW|WS_CLIPSIBLINGS|ES_LEFT|ES_AUTOHSCROLL|WS_BORDER;
10268 hdc = GetDC(infoPtr->hwndSelf);
10270 /* Select the font to get appropriate metric dimensions */
10271 if(infoPtr->hFont != 0)
10272 hOldFont = SelectObject(hdc, infoPtr->hFont);
10274 /*Get String Length in pixels */
10275 GetTextExtentPoint32W(hdc, text, lstrlenW(text), &sz);
10277 /*Add Extra spacing for the next character */
10278 GetTextMetricsW(hdc, &textMetric);
10279 sz.cx += (textMetric.tmMaxCharWidth * 2);
10281 if(infoPtr->hFont != 0)
10282 SelectObject(hdc, hOldFont);
10284 ReleaseDC(infoPtr->hwndSelf, hdc);
10286 hedit = CreateWindowW(editName, text, style, x, y, sz.cx, height, infoPtr->hwndSelf, 0, hinst, 0);
10288 hedit = CreateWindowA("Edit", (LPCSTR)text, style, x, y, sz.cx, height, infoPtr->hwndSelf, 0, hinst, 0);
10290 if (!hedit) return 0;
10292 infoPtr->EditWndProc = (WNDPROC)
10293 (isW ? SetWindowLongPtrW(hedit, GWLP_WNDPROC, (DWORD_PTR)EditLblWndProcW) :
10294 SetWindowLongPtrA(hedit, GWLP_WNDPROC, (DWORD_PTR)EditLblWndProcA) );
10296 SendMessageW(hedit, WM_SETFONT, (WPARAM)infoPtr->hFont, FALSE);