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)
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 --;
4504 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
4505 LISTVIEW_UpdateScroll(infoPtr);
4506 LISTVIEW_InvalidateList(infoPtr);
4513 * Scrolls, and updates the columns, when a column is changing width.
4516 * [I] infoPtr : valid pointer to the listview structure
4517 * [I] nColumn : column to scroll
4518 * [I] dx : amount of scroll, in pixels
4523 static void LISTVIEW_ScrollColumns(LISTVIEW_INFO *infoPtr, INT nColumn, INT dx)
4525 COLUMN_INFO *lpColumnInfo;
4530 if (nColumn < 0 || DPA_GetPtrCount(infoPtr->hdpaColumns) < 1) return;
4531 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, min(nColumn, DPA_GetPtrCount(infoPtr->hdpaColumns) - 1));
4532 rcCol = lpColumnInfo->rcHeader;
4533 if (nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns))
4534 rcCol.left = rcCol.right;
4536 /* adjust the other columns */
4537 for (nCol = nColumn; nCol < DPA_GetPtrCount(infoPtr->hdpaColumns); nCol++)
4539 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, nCol);
4540 lpColumnInfo->rcHeader.left += dx;
4541 lpColumnInfo->rcHeader.right += dx;
4544 /* do not update screen if not in report mode */
4545 if (!is_redrawing(infoPtr) || (infoPtr->dwStyle & LVS_TYPEMASK) != LVS_REPORT) return;
4547 /* if we have a focus, we must first erase the focus rect */
4548 if (infoPtr->bFocus) LISTVIEW_ShowFocusRect(infoPtr, FALSE);
4550 /* Need to reset the item width when inserting a new column */
4551 infoPtr->nItemWidth += dx;
4553 LISTVIEW_UpdateScroll(infoPtr);
4554 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
4556 /* scroll to cover the deleted column, and invalidate for redraw */
4557 rcOld = infoPtr->rcList;
4558 rcOld.left = ptOrigin.x + rcCol.left + dx;
4559 ScrollWindowEx(infoPtr->hwndSelf, dx, 0, &rcOld, &rcOld, 0, 0, SW_ERASE | SW_INVALIDATE);
4561 /* we can restore focus now */
4562 if (infoPtr->bFocus) LISTVIEW_ShowFocusRect(infoPtr, TRUE);
4567 * Removes a column from the listview control.
4570 * [I] infoPtr : valid pointer to the listview structure
4571 * [I] nColumn : column index
4577 static BOOL LISTVIEW_DeleteColumn(LISTVIEW_INFO *infoPtr, INT nColumn)
4581 TRACE("nColumn=%d\n", nColumn);
4583 if (nColumn < 0 || DPA_GetPtrCount(infoPtr->hdpaColumns) == 0
4584 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
4586 /* While the MSDN specifically says that column zero should not be deleted,
4587 what actually happens is that the column itself is deleted but no items or subitems
4591 LISTVIEW_GetHeaderRect(infoPtr, nColumn, &rcCol);
4593 if (!Header_DeleteItem(infoPtr->hwndHeader, nColumn))
4596 Free(DPA_GetPtr(infoPtr->hdpaColumns, nColumn));
4597 DPA_DeletePtr(infoPtr->hdpaColumns, nColumn);
4599 if (!(infoPtr->dwStyle & LVS_OWNERDATA) && nColumn)
4601 SUBITEM_INFO *lpSubItem, *lpDelItem;
4603 INT nItem, nSubItem, i;
4605 for (nItem = 0; nItem < infoPtr->nItemCount; nItem++)
4607 hdpaSubItems = (HDPA)DPA_GetPtr(infoPtr->hdpaItems, nItem);
4610 for (i = 1; i < DPA_GetPtrCount(hdpaSubItems); i++)
4612 lpSubItem = (SUBITEM_INFO *)DPA_GetPtr(hdpaSubItems, i);
4613 if (lpSubItem->iSubItem == nColumn)
4616 lpDelItem = lpSubItem;
4618 else if (lpSubItem->iSubItem > nColumn)
4620 lpSubItem->iSubItem--;
4624 /* if we found our subitem, zapp it */
4628 if (is_textW(lpDelItem->hdr.pszText))
4629 Free(lpDelItem->hdr.pszText);
4634 /* free dpa memory */
4635 DPA_DeletePtr(hdpaSubItems, nSubItem);
4640 /* update the other column info */
4641 LISTVIEW_UpdateItemSize(infoPtr);
4642 if(DPA_GetPtrCount(infoPtr->hdpaColumns) == 0)
4643 LISTVIEW_InvalidateList(infoPtr);
4645 LISTVIEW_ScrollColumns(infoPtr, nColumn, -(rcCol.right - rcCol.left));
4652 * Invalidates the listview after an item's insertion or deletion.
4655 * [I] infoPtr : valid pointer to the listview structure
4656 * [I] nItem : item index
4657 * [I] dir : -1 if deleting, 1 if inserting
4662 static void LISTVIEW_ScrollOnInsert(LISTVIEW_INFO *infoPtr, INT nItem, INT dir)
4664 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
4665 INT nPerCol, nItemCol, nItemRow;
4669 /* if we don't refresh, what's the point of scrolling? */
4670 if (!is_redrawing(infoPtr)) return;
4672 assert (abs(dir) == 1);
4674 /* arrange icons if autoarrange is on */
4675 if (is_autoarrange(infoPtr))
4677 BOOL arrange = TRUE;
4678 if (dir < 0 && nItem >= infoPtr->nItemCount) arrange = FALSE;
4679 if (dir > 0 && nItem == infoPtr->nItemCount - 1) arrange = FALSE;
4680 if (arrange) LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
4683 /* scrollbars need updating */
4684 LISTVIEW_UpdateScroll(infoPtr);
4686 /* figure out the item's position */
4687 if (uView == LVS_REPORT)
4688 nPerCol = infoPtr->nItemCount + 1;
4689 else if (uView == LVS_LIST)
4690 nPerCol = LISTVIEW_GetCountPerColumn(infoPtr);
4691 else /* LVS_ICON, or LVS_SMALLICON */
4694 nItemCol = nItem / nPerCol;
4695 nItemRow = nItem % nPerCol;
4696 LISTVIEW_GetOrigin(infoPtr, &Origin);
4698 /* move the items below up a slot */
4699 rcScroll.left = nItemCol * infoPtr->nItemWidth;
4700 rcScroll.top = nItemRow * infoPtr->nItemHeight;
4701 rcScroll.right = rcScroll.left + infoPtr->nItemWidth;
4702 rcScroll.bottom = nPerCol * infoPtr->nItemHeight;
4703 OffsetRect(&rcScroll, Origin.x, Origin.y);
4704 TRACE("rcScroll=%s, dx=%d\n", wine_dbgstr_rect(&rcScroll), dir * infoPtr->nItemHeight);
4705 if (IntersectRect(&rcScroll, &rcScroll, &infoPtr->rcList))
4707 TRACE("Scrolling rcScroll=%s, rcList=%s\n", wine_dbgstr_rect(&rcScroll), wine_dbgstr_rect(&infoPtr->rcList));
4708 ScrollWindowEx(infoPtr->hwndSelf, 0, dir * infoPtr->nItemHeight,
4709 &rcScroll, &rcScroll, 0, 0, SW_ERASE | SW_INVALIDATE);
4712 /* report has only that column, so we're done */
4713 if (uView == LVS_REPORT) return;
4715 /* now for LISTs, we have to deal with the columns to the right */
4716 rcScroll.left = (nItemCol + 1) * infoPtr->nItemWidth;
4718 rcScroll.right = (infoPtr->nItemCount / nPerCol + 1) * infoPtr->nItemWidth;
4719 rcScroll.bottom = nPerCol * infoPtr->nItemHeight;
4720 OffsetRect(&rcScroll, Origin.x, Origin.y);
4721 if (IntersectRect(&rcScroll, &rcScroll, &infoPtr->rcList))
4722 ScrollWindowEx(infoPtr->hwndSelf, 0, dir * infoPtr->nItemHeight,
4723 &rcScroll, &rcScroll, 0, 0, SW_ERASE | SW_INVALIDATE);
4728 * Removes an item from the listview control.
4731 * [I] infoPtr : valid pointer to the listview structure
4732 * [I] nItem : item index
4738 static BOOL LISTVIEW_DeleteItem(LISTVIEW_INFO *infoPtr, INT nItem)
4741 const UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
4742 const BOOL is_icon = (uView == LVS_SMALLICON || uView == LVS_ICON);
4744 TRACE("(nItem=%d)\n", nItem);
4746 if (nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
4748 /* remove selection, and focus */
4750 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
4751 LISTVIEW_SetItemState(infoPtr, nItem, &item);
4753 /* send LVN_DELETEITEM notification. */
4754 if (!notify_deleteitem(infoPtr, nItem)) return FALSE;
4756 /* we need to do this here, because we'll be deleting stuff */
4758 LISTVIEW_InvalidateItem(infoPtr, nItem);
4760 if (!(infoPtr->dwStyle & LVS_OWNERDATA))
4766 hdpaSubItems = (HDPA)DPA_DeletePtr(infoPtr->hdpaItems, nItem);
4767 for (i = 0; i < DPA_GetPtrCount(hdpaSubItems); i++)
4769 hdrItem = (ITEMHDR *)DPA_GetPtr(hdpaSubItems, i);
4770 if (is_textW(hdrItem->pszText)) Free(hdrItem->pszText);
4773 DPA_Destroy(hdpaSubItems);
4778 DPA_DeletePtr(infoPtr->hdpaPosX, nItem);
4779 DPA_DeletePtr(infoPtr->hdpaPosY, nItem);
4782 infoPtr->nItemCount--;
4783 LISTVIEW_ShiftIndices(infoPtr, nItem, -1);
4785 /* now is the invalidation fun */
4787 LISTVIEW_ScrollOnInsert(infoPtr, nItem, -1);
4794 * Callback implementation for editlabel control
4797 * [I] infoPtr : valid pointer to the listview structure
4798 * [I] pszText : modified text
4799 * [I] isW : TRUE if psxText is Unicode, FALSE if it's ANSI
4805 static BOOL LISTVIEW_EndEditLabelT(LISTVIEW_INFO *infoPtr, LPWSTR pszText, BOOL isW)
4807 HWND hwndSelf = infoPtr->hwndSelf;
4808 NMLVDISPINFOW dispInfo;
4810 TRACE("(pszText=%s, isW=%d)\n", debugtext_t(pszText, isW), isW);
4812 ZeroMemory(&dispInfo, sizeof(dispInfo));
4813 dispInfo.item.mask = LVIF_PARAM | LVIF_STATE;
4814 dispInfo.item.iItem = infoPtr->nEditLabelItem;
4815 dispInfo.item.iSubItem = 0;
4816 dispInfo.item.stateMask = ~0;
4817 if (!LISTVIEW_GetItemW(infoPtr, &dispInfo.item)) return FALSE;
4818 /* add the text from the edit in */
4819 dispInfo.item.mask |= LVIF_TEXT;
4820 dispInfo.item.pszText = pszText;
4821 dispInfo.item.cchTextMax = textlenT(pszText, isW);
4823 /* Do we need to update the Item Text */
4824 if (!notify_dispinfoT(infoPtr, LVN_ENDLABELEDITW, &dispInfo, isW)) return FALSE;
4825 if (!IsWindow(hwndSelf))
4827 if (!pszText) return TRUE;
4829 if (!(infoPtr->dwStyle & LVS_OWNERDATA))
4831 HDPA hdpaSubItems = (HDPA)DPA_GetPtr(infoPtr->hdpaItems, infoPtr->nEditLabelItem);
4832 ITEM_INFO* lpItem = (ITEM_INFO *)DPA_GetPtr(hdpaSubItems, 0);
4833 if (lpItem && lpItem->hdr.pszText == LPSTR_TEXTCALLBACKW)
4835 LISTVIEW_InvalidateItem(infoPtr, infoPtr->nEditLabelItem);
4840 ZeroMemory(&dispInfo, sizeof(dispInfo));
4841 dispInfo.item.mask = LVIF_TEXT;
4842 dispInfo.item.iItem = infoPtr->nEditLabelItem;
4843 dispInfo.item.iSubItem = 0;
4844 dispInfo.item.pszText = pszText;
4845 dispInfo.item.cchTextMax = textlenT(pszText, isW);
4846 return LISTVIEW_SetItemT(infoPtr, &dispInfo.item, isW);
4851 * Begin in place editing of specified list view item
4854 * [I] infoPtr : valid pointer to the listview structure
4855 * [I] nItem : item index
4856 * [I] isW : TRUE if it's a Unicode req, FALSE if ASCII
4862 static HWND LISTVIEW_EditLabelT(LISTVIEW_INFO *infoPtr, INT nItem, BOOL isW)
4864 WCHAR szDispText[DISP_TEXT_SIZE] = { 0 };
4865 NMLVDISPINFOW dispInfo;
4867 HWND hwndSelf = infoPtr->hwndSelf;
4869 TRACE("(nItem=%d, isW=%d)\n", nItem, isW);
4871 if (~infoPtr->dwStyle & LVS_EDITLABELS) return 0;
4872 if (nItem < 0 || nItem >= infoPtr->nItemCount) return 0;
4874 infoPtr->nEditLabelItem = nItem;
4876 /* Is the EditBox still there, if so remove it */
4877 if(infoPtr->hwndEdit != 0)
4879 SetFocus(infoPtr->hwndSelf);
4880 infoPtr->hwndEdit = 0;
4883 LISTVIEW_SetSelection(infoPtr, nItem);
4884 LISTVIEW_SetItemFocus(infoPtr, nItem);
4885 LISTVIEW_InvalidateItem(infoPtr, nItem);
4887 rect.left = LVIR_LABEL;
4888 if (!LISTVIEW_GetItemRect(infoPtr, nItem, &rect)) return 0;
4890 ZeroMemory(&dispInfo, sizeof(dispInfo));
4891 dispInfo.item.mask = LVIF_PARAM | LVIF_STATE | LVIF_TEXT;
4892 dispInfo.item.iItem = nItem;
4893 dispInfo.item.iSubItem = 0;
4894 dispInfo.item.stateMask = ~0;
4895 dispInfo.item.pszText = szDispText;
4896 dispInfo.item.cchTextMax = DISP_TEXT_SIZE;
4897 if (!LISTVIEW_GetItemT(infoPtr, &dispInfo.item, isW)) return 0;
4899 infoPtr->hwndEdit = CreateEditLabelT(infoPtr, dispInfo.item.pszText, WS_VISIBLE,
4900 rect.left-2, rect.top-1, 0, rect.bottom - rect.top+2, isW);
4901 if (!infoPtr->hwndEdit) return 0;
4903 if (notify_dispinfoT(infoPtr, LVN_BEGINLABELEDITW, &dispInfo, isW))
4905 if (!IsWindow(hwndSelf))
4907 SendMessageW(infoPtr->hwndEdit, WM_CLOSE, 0, 0);
4908 infoPtr->hwndEdit = 0;
4912 ShowWindow(infoPtr->hwndEdit, SW_NORMAL);
4913 SetFocus(infoPtr->hwndEdit);
4914 SendMessageW(infoPtr->hwndEdit, EM_SETSEL, 0, -1);
4915 return infoPtr->hwndEdit;
4921 * Ensures the specified item is visible, scrolling into view if necessary.
4924 * [I] infoPtr : valid pointer to the listview structure
4925 * [I] nItem : item index
4926 * [I] bPartial : partially or entirely visible
4932 static BOOL LISTVIEW_EnsureVisible(LISTVIEW_INFO *infoPtr, INT nItem, BOOL bPartial)
4934 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
4935 INT nScrollPosHeight = 0;
4936 INT nScrollPosWidth = 0;
4937 INT nHorzAdjust = 0;
4938 INT nVertAdjust = 0;
4941 RECT rcItem, rcTemp;
4943 rcItem.left = LVIR_BOUNDS;
4944 if (!LISTVIEW_GetItemRect(infoPtr, nItem, &rcItem)) return FALSE;
4946 if (bPartial && IntersectRect(&rcTemp, &infoPtr->rcList, &rcItem)) return TRUE;
4948 if (rcItem.left < infoPtr->rcList.left || rcItem.right > infoPtr->rcList.right)
4950 /* scroll left/right, but in LVS_REPORT mode */
4951 if (uView == LVS_LIST)
4952 nScrollPosWidth = infoPtr->nItemWidth;
4953 else if ((uView == LVS_SMALLICON) || (uView == LVS_ICON))
4954 nScrollPosWidth = 1;
4956 if (rcItem.left < infoPtr->rcList.left)
4959 if (uView != LVS_REPORT) nHorzDiff = rcItem.left - infoPtr->rcList.left;
4964 if (uView != LVS_REPORT) nHorzDiff = rcItem.right - infoPtr->rcList.right;
4968 if (rcItem.top < infoPtr->rcList.top || rcItem.bottom > infoPtr->rcList.bottom)
4970 /* scroll up/down, but not in LVS_LIST mode */
4971 if (uView == LVS_REPORT)
4972 nScrollPosHeight = infoPtr->nItemHeight;
4973 else if ((uView == LVS_ICON) || (uView == LVS_SMALLICON))
4974 nScrollPosHeight = 1;
4976 if (rcItem.top < infoPtr->rcList.top)
4979 if (uView != LVS_LIST) nVertDiff = rcItem.top - infoPtr->rcList.top;
4984 if (uView != LVS_LIST) nVertDiff = rcItem.bottom - infoPtr->rcList.bottom;
4988 if (!nScrollPosWidth && !nScrollPosHeight) return TRUE;
4990 if (nScrollPosWidth)
4992 INT diff = nHorzDiff / nScrollPosWidth;
4993 if (nHorzDiff % nScrollPosWidth) diff += nHorzAdjust;
4994 LISTVIEW_HScroll(infoPtr, SB_INTERNAL, diff, 0);
4997 if (nScrollPosHeight)
4999 INT diff = nVertDiff / nScrollPosHeight;
5000 if (nVertDiff % nScrollPosHeight) diff += nVertAdjust;
5001 LISTVIEW_VScroll(infoPtr, SB_INTERNAL, diff, 0);
5009 * Searches for an item with specific characteristics.
5012 * [I] hwnd : window handle
5013 * [I] nStart : base item index
5014 * [I] lpFindInfo : item information to look for
5017 * SUCCESS : index of item
5020 static INT LISTVIEW_FindItemW(const LISTVIEW_INFO *infoPtr, INT nStart,
5021 const LVFINDINFOW *lpFindInfo)
5023 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
5024 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
5025 BOOL bWrap = FALSE, bNearest = FALSE;
5026 INT nItem = nStart + 1, nLast = infoPtr->nItemCount, nNearestItem = -1;
5027 ULONG xdist, ydist, dist, mindist = 0x7fffffff;
5028 POINT Position, Destination;
5031 if (!lpFindInfo || nItem < 0) return -1;
5034 if (lpFindInfo->flags & (LVFI_STRING | LVFI_PARTIAL))
5036 lvItem.mask |= LVIF_TEXT;
5037 lvItem.pszText = szDispText;
5038 lvItem.cchTextMax = DISP_TEXT_SIZE;
5041 if (lpFindInfo->flags & LVFI_WRAP)
5044 if ((lpFindInfo->flags & LVFI_NEARESTXY) &&
5045 (uView == LVS_ICON || uView ==LVS_SMALLICON))
5050 LISTVIEW_GetOrigin(infoPtr, &Origin);
5051 Destination.x = lpFindInfo->pt.x - Origin.x;
5052 Destination.y = lpFindInfo->pt.y - Origin.y;
5053 switch(lpFindInfo->vkDirection)
5055 case VK_DOWN: Destination.y += infoPtr->nItemHeight; break;
5056 case VK_UP: Destination.y -= infoPtr->nItemHeight; break;
5057 case VK_RIGHT: Destination.x += infoPtr->nItemWidth; break;
5058 case VK_LEFT: Destination.x -= infoPtr->nItemWidth; break;
5059 case VK_HOME: Destination.x = Destination.y = 0; break;
5060 case VK_NEXT: Destination.y += infoPtr->rcList.bottom - infoPtr->rcList.top; break;
5061 case VK_PRIOR: Destination.y -= infoPtr->rcList.bottom - infoPtr->rcList.top; break;
5063 LISTVIEW_GetAreaRect(infoPtr, &rcArea);
5064 Destination.x = rcArea.right;
5065 Destination.y = rcArea.bottom;
5067 default: ERR("Unknown vkDirection=%d\n", lpFindInfo->vkDirection);
5071 else Destination.x = Destination.y = 0;
5073 /* if LVFI_PARAM is specified, all other flags are ignored */
5074 if (lpFindInfo->flags & LVFI_PARAM)
5076 lvItem.mask |= LVIF_PARAM;
5078 lvItem.mask &= ~LVIF_TEXT;
5082 for (; nItem < nLast; nItem++)
5084 lvItem.iItem = nItem;
5085 lvItem.iSubItem = 0;
5086 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) continue;
5088 if (lvItem.mask & LVIF_PARAM)
5090 if (lpFindInfo->lParam == lvItem.lParam)
5096 if (lvItem.mask & LVIF_TEXT)
5098 if (lpFindInfo->flags & LVFI_PARTIAL)
5100 if (strstrW(lvItem.pszText, lpFindInfo->psz) == NULL) continue;
5104 if (lstrcmpW(lvItem.pszText, lpFindInfo->psz) != 0) continue;
5108 if (!bNearest) return nItem;
5110 /* This is very inefficient. To do a good job here,
5111 * we need a sorted array of (x,y) item positions */
5112 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
5114 /* compute the distance^2 to the destination */
5115 xdist = Destination.x - Position.x;
5116 ydist = Destination.y - Position.y;
5117 dist = xdist * xdist + ydist * ydist;
5119 /* remember the distance, and item if it's closer */
5123 nNearestItem = nItem;
5130 nLast = min(nStart + 1, infoPtr->nItemCount);
5135 return nNearestItem;
5140 * Searches for an item with specific characteristics.
5143 * [I] hwnd : window handle
5144 * [I] nStart : base item index
5145 * [I] lpFindInfo : item information to look for
5148 * SUCCESS : index of item
5151 static INT LISTVIEW_FindItemA(const LISTVIEW_INFO *infoPtr, INT nStart,
5152 const LVFINDINFOA *lpFindInfo)
5154 BOOL hasText = lpFindInfo->flags & (LVFI_STRING | LVFI_PARTIAL);
5159 memcpy(&fiw, lpFindInfo, sizeof(fiw));
5160 if (hasText) fiw.psz = strW = textdupTtoW((LPCWSTR)lpFindInfo->psz, FALSE);
5161 res = LISTVIEW_FindItemW(infoPtr, nStart, &fiw);
5162 textfreeT(strW, FALSE);
5168 * Retrieves the background image of the listview control.
5171 * [I] infoPtr : valid pointer to the listview structure
5172 * [O] lpBkImage : background image attributes
5178 /* static BOOL LISTVIEW_GetBkImage(const LISTVIEW_INFO *infoPtr, LPLVBKIMAGE lpBkImage) */
5180 /* FIXME (listview, "empty stub!\n"); */
5186 * Retrieves column attributes.
5189 * [I] infoPtr : valid pointer to the listview structure
5190 * [I] nColumn : column index
5191 * [IO] lpColumn : column information
5192 * [I] isW : if TRUE, then lpColumn is a LPLVCOLUMNW
5193 * otherwise it is in fact a LPLVCOLUMNA
5199 static BOOL LISTVIEW_GetColumnT(const LISTVIEW_INFO *infoPtr, INT nColumn, LPLVCOLUMNW lpColumn, BOOL isW)
5201 COLUMN_INFO *lpColumnInfo;
5204 if (!lpColumn || nColumn < 0 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
5205 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, nColumn);
5207 /* initialize memory */
5208 ZeroMemory(&hdi, sizeof(hdi));
5210 if (lpColumn->mask & LVCF_TEXT)
5212 hdi.mask |= HDI_TEXT;
5213 hdi.pszText = lpColumn->pszText;
5214 hdi.cchTextMax = lpColumn->cchTextMax;
5217 if (lpColumn->mask & LVCF_IMAGE)
5218 hdi.mask |= HDI_IMAGE;
5220 if (lpColumn->mask & LVCF_ORDER)
5221 hdi.mask |= HDI_ORDER;
5223 if (!SendMessageW(infoPtr->hwndHeader, isW ? HDM_GETITEMW : HDM_GETITEMA, nColumn, (LPARAM)&hdi)) return FALSE;
5225 if (lpColumn->mask & LVCF_FMT)
5226 lpColumn->fmt = lpColumnInfo->fmt;
5228 if (lpColumn->mask & LVCF_WIDTH)
5229 lpColumn->cx = lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left;
5231 if (lpColumn->mask & LVCF_IMAGE)
5232 lpColumn->iImage = hdi.iImage;
5234 if (lpColumn->mask & LVCF_ORDER)
5235 lpColumn->iOrder = hdi.iOrder;
5241 static BOOL LISTVIEW_GetColumnOrderArray(const LISTVIEW_INFO *infoPtr, INT iCount, LPINT lpiArray)
5248 /* FIXME: little hack */
5249 for (i = 0; i < iCount; i++)
5257 * Retrieves the column width.
5260 * [I] infoPtr : valid pointer to the listview structure
5261 * [I] int : column index
5264 * SUCCESS : column width
5267 static INT LISTVIEW_GetColumnWidth(const LISTVIEW_INFO *infoPtr, INT nColumn)
5269 INT nColumnWidth = 0;
5272 TRACE("nColumn=%d\n", nColumn);
5274 /* we have a 'column' in LIST and REPORT mode only */
5275 switch(infoPtr->dwStyle & LVS_TYPEMASK)
5278 nColumnWidth = infoPtr->nItemWidth;
5281 /* We are not using LISTVIEW_GetHeaderRect as this data is updated only after a HDM_ITEMCHANGED.
5282 * There is an application that subclasses the listview, calls LVM_GETCOLUMNWIDTH in the
5283 * HDM_ITEMCHANGED handler and goes into infinite recursion if it receives old data.
5285 * TODO: should we do the same in LVM_GETCOLUMN?
5287 hdItem.mask = HDI_WIDTH;
5288 if (!SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nColumn, (LPARAM)&hdItem))
5290 WARN("(%p): HDM_GETITEMW failed for item %d\n", infoPtr->hwndSelf, nColumn);
5293 nColumnWidth = hdItem.cxy;
5297 TRACE("nColumnWidth=%d\n", nColumnWidth);
5298 return nColumnWidth;
5303 * In list or report display mode, retrieves the number of items that can fit
5304 * vertically in the visible area. In icon or small icon display mode,
5305 * retrieves the total number of visible items.
5308 * [I] infoPtr : valid pointer to the listview structure
5311 * Number of fully visible items.
5313 static INT LISTVIEW_GetCountPerPage(const LISTVIEW_INFO *infoPtr)
5315 switch (infoPtr->dwStyle & LVS_TYPEMASK)
5319 return infoPtr->nItemCount;
5321 return LISTVIEW_GetCountPerColumn(infoPtr);
5323 return LISTVIEW_GetCountPerRow(infoPtr) * LISTVIEW_GetCountPerColumn(infoPtr);
5331 * Retrieves an image list handle.
5334 * [I] infoPtr : valid pointer to the listview structure
5335 * [I] nImageList : image list identifier
5338 * SUCCESS : image list handle
5341 static HIMAGELIST LISTVIEW_GetImageList(const LISTVIEW_INFO *infoPtr, INT nImageList)
5345 case LVSIL_NORMAL: return infoPtr->himlNormal;
5346 case LVSIL_SMALL: return infoPtr->himlSmall;
5347 case LVSIL_STATE: return infoPtr->himlState;
5352 /* LISTVIEW_GetISearchString */
5356 * Retrieves item attributes.
5359 * [I] hwnd : window handle
5360 * [IO] lpLVItem : item info
5361 * [I] isW : if TRUE, then lpLVItem is a LPLVITEMW,
5362 * if FALSE, then lpLVItem is a LPLVITEMA.
5365 * This is the internal 'GetItem' interface -- it tries to
5366 * be smart and avoid text copies, if possible, by modifying
5367 * lpLVItem->pszText to point to the text string. Please note
5368 * that this is not always possible (e.g. OWNERDATA), so on
5369 * entry you *must* supply valid values for pszText, and cchTextMax.
5370 * The only difference to the documented interface is that upon
5371 * return, you should use *only* the lpLVItem->pszText, rather than
5372 * the buffer pointer you provided on input. Most code already does
5373 * that, so it's not a problem.
5374 * For the two cases when the text must be copied (that is,
5375 * for LVM_GETITEM, and LVM_GETITEMTEXT), use LISTVIEW_GetItemExtT.
5381 static BOOL LISTVIEW_GetItemT(const LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem, BOOL isW)
5383 ITEMHDR callbackHdr = { LPSTR_TEXTCALLBACKW, I_IMAGECALLBACK };
5384 NMLVDISPINFOW dispInfo;
5390 TRACE("(lpLVItem=%s, isW=%d)\n", debuglvitem_t(lpLVItem, isW), isW);
5392 if (!lpLVItem || lpLVItem->iItem < 0 || lpLVItem->iItem >= infoPtr->nItemCount)
5395 if (lpLVItem->mask == 0) return TRUE;
5397 /* make a local copy */
5398 isubitem = lpLVItem->iSubItem;
5400 /* a quick optimization if all we're asked is the focus state
5401 * these queries are worth optimising since they are common,
5402 * and can be answered in constant time, without the heavy accesses */
5403 if ( (lpLVItem->mask == LVIF_STATE) && (lpLVItem->stateMask == LVIS_FOCUSED) &&
5404 !(infoPtr->uCallbackMask & LVIS_FOCUSED) )
5406 lpLVItem->state = 0;
5407 if (infoPtr->nFocusedItem == lpLVItem->iItem)
5408 lpLVItem->state |= LVIS_FOCUSED;
5412 ZeroMemory(&dispInfo, sizeof(dispInfo));
5414 /* if the app stores all the data, handle it separately */
5415 if (infoPtr->dwStyle & LVS_OWNERDATA)
5417 dispInfo.item.state = 0;
5419 /* apparently, we should not callback for lParam in LVS_OWNERDATA */
5420 if ((lpLVItem->mask & ~(LVIF_STATE | LVIF_PARAM)) || infoPtr->uCallbackMask)
5422 /* NOTE: copy only fields which we _know_ are initialized, some apps
5423 * depend on the uninitialized fields being 0 */
5424 dispInfo.item.mask = lpLVItem->mask & ~LVIF_PARAM;
5425 dispInfo.item.iItem = lpLVItem->iItem;
5426 dispInfo.item.iSubItem = isubitem;
5427 if (lpLVItem->mask & LVIF_TEXT)
5429 dispInfo.item.pszText = lpLVItem->pszText;
5430 dispInfo.item.cchTextMax = lpLVItem->cchTextMax;
5432 if (lpLVItem->mask & LVIF_STATE)
5433 dispInfo.item.stateMask = lpLVItem->stateMask & infoPtr->uCallbackMask;
5434 notify_dispinfoT(infoPtr, LVN_GETDISPINFOW, &dispInfo, isW);
5435 dispInfo.item.stateMask = lpLVItem->stateMask;
5436 if (lpLVItem->mask & (LVIF_GROUPID|LVIF_COLUMNS))
5438 /* full size structure expected - _WIN32IE >= 0x560 */
5439 *lpLVItem = dispInfo.item;
5441 else if (lpLVItem->mask & LVIF_INDENT)
5443 /* indent member expected - _WIN32IE >= 0x300 */
5444 memcpy(lpLVItem, &dispInfo.item, offsetof( LVITEMW, iGroupId ));
5448 /* minimal structure expected */
5449 memcpy(lpLVItem, &dispInfo.item, offsetof( LVITEMW, iIndent ));
5451 TRACE(" getdispinfo(1):lpLVItem=%s\n", debuglvitem_t(lpLVItem, isW));
5454 /* make sure lParam is zeroed out */
5455 if (lpLVItem->mask & LVIF_PARAM) lpLVItem->lParam = 0;
5457 /* we store only a little state, so if we're not asked, we're done */
5458 if (!(lpLVItem->mask & LVIF_STATE) || isubitem) return TRUE;
5460 /* if focus is handled by us, report it */
5461 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_FOCUSED )
5463 lpLVItem->state &= ~LVIS_FOCUSED;
5464 if (infoPtr->nFocusedItem == lpLVItem->iItem)
5465 lpLVItem->state |= LVIS_FOCUSED;
5468 /* and do the same for selection, if we handle it */
5469 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_SELECTED )
5471 lpLVItem->state &= ~LVIS_SELECTED;
5472 if (ranges_contain(infoPtr->selectionRanges, lpLVItem->iItem))
5473 lpLVItem->state |= LVIS_SELECTED;
5479 /* find the item and subitem structures before we proceed */
5480 hdpaSubItems = (HDPA)DPA_GetPtr(infoPtr->hdpaItems, lpLVItem->iItem);
5481 lpItem = (ITEM_INFO *)DPA_GetPtr(hdpaSubItems, 0);
5486 SUBITEM_INFO *lpSubItem = LISTVIEW_GetSubItemPtr(hdpaSubItems, isubitem);
5487 pItemHdr = lpSubItem ? &lpSubItem->hdr : &callbackHdr;
5490 WARN(" iSubItem invalid (%08x), ignored.\n", isubitem);
5495 pItemHdr = &lpItem->hdr;
5497 /* Do we need to query the state from the app? */
5498 if ((lpLVItem->mask & LVIF_STATE) && infoPtr->uCallbackMask && isubitem == 0)
5500 dispInfo.item.mask |= LVIF_STATE;
5501 dispInfo.item.stateMask = infoPtr->uCallbackMask;
5504 /* Do we need to enquire about the image? */
5505 if ((lpLVItem->mask & LVIF_IMAGE) && pItemHdr->iImage == I_IMAGECALLBACK &&
5506 (isubitem == 0 || (infoPtr->dwLvExStyle & LVS_EX_SUBITEMIMAGES)))
5508 dispInfo.item.mask |= LVIF_IMAGE;
5509 dispInfo.item.iImage = I_IMAGECALLBACK;
5512 /* Apps depend on calling back for text if it is NULL or LPSTR_TEXTCALLBACKW */
5513 if ((lpLVItem->mask & LVIF_TEXT) && !is_textW(pItemHdr->pszText))
5515 dispInfo.item.mask |= LVIF_TEXT;
5516 dispInfo.item.pszText = lpLVItem->pszText;
5517 dispInfo.item.cchTextMax = lpLVItem->cchTextMax;
5518 if (dispInfo.item.pszText && dispInfo.item.cchTextMax > 0)
5519 *dispInfo.item.pszText = '\0';
5522 /* If we don't have all the requested info, query the application */
5523 if (dispInfo.item.mask != 0)
5525 dispInfo.item.iItem = lpLVItem->iItem;
5526 dispInfo.item.iSubItem = lpLVItem->iSubItem; /* yes: the original subitem */
5527 dispInfo.item.lParam = lpItem->lParam;
5528 notify_dispinfoT(infoPtr, LVN_GETDISPINFOW, &dispInfo, isW);
5529 TRACE(" getdispinfo(2):item=%s\n", debuglvitem_t(&dispInfo.item, isW));
5532 /* we should not store values for subitems */
5533 if (isubitem) dispInfo.item.mask &= ~LVIF_DI_SETITEM;
5535 /* Now, handle the iImage field */
5536 if (dispInfo.item.mask & LVIF_IMAGE)
5538 lpLVItem->iImage = dispInfo.item.iImage;
5539 if ((dispInfo.item.mask & LVIF_DI_SETITEM) && pItemHdr->iImage == I_IMAGECALLBACK)
5540 pItemHdr->iImage = dispInfo.item.iImage;
5542 else if (lpLVItem->mask & LVIF_IMAGE)
5544 if(isubitem == 0 || (infoPtr->dwLvExStyle & LVS_EX_SUBITEMIMAGES))
5545 lpLVItem->iImage = pItemHdr->iImage;
5547 lpLVItem->iImage = 0;
5550 /* The pszText field */
5551 if (dispInfo.item.mask & LVIF_TEXT)
5553 if ((dispInfo.item.mask & LVIF_DI_SETITEM) && pItemHdr->pszText)
5554 textsetptrT(&pItemHdr->pszText, dispInfo.item.pszText, isW);
5556 lpLVItem->pszText = dispInfo.item.pszText;
5558 else if (lpLVItem->mask & LVIF_TEXT)
5560 if (isW) lpLVItem->pszText = pItemHdr->pszText;
5561 else textcpynT(lpLVItem->pszText, isW, pItemHdr->pszText, TRUE, lpLVItem->cchTextMax);
5564 /* Next is the lParam field */
5565 if (dispInfo.item.mask & LVIF_PARAM)
5567 lpLVItem->lParam = dispInfo.item.lParam;
5568 if ((dispInfo.item.mask & LVIF_DI_SETITEM))
5569 lpItem->lParam = dispInfo.item.lParam;
5571 else if (lpLVItem->mask & LVIF_PARAM)
5572 lpLVItem->lParam = lpItem->lParam;
5574 /* if this is a subitem, we're done */
5575 if (isubitem) return TRUE;
5577 /* ... the state field (this one is different due to uCallbackmask) */
5578 if (lpLVItem->mask & LVIF_STATE)
5580 lpLVItem->state = lpItem->state & lpLVItem->stateMask;
5581 if (dispInfo.item.mask & LVIF_STATE)
5583 lpLVItem->state &= ~dispInfo.item.stateMask;
5584 lpLVItem->state |= (dispInfo.item.state & dispInfo.item.stateMask);
5586 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_FOCUSED )
5588 lpLVItem->state &= ~LVIS_FOCUSED;
5589 if (infoPtr->nFocusedItem == lpLVItem->iItem)
5590 lpLVItem->state |= LVIS_FOCUSED;
5592 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_SELECTED )
5594 lpLVItem->state &= ~LVIS_SELECTED;
5595 if (ranges_contain(infoPtr->selectionRanges, lpLVItem->iItem))
5596 lpLVItem->state |= LVIS_SELECTED;
5600 /* and last, but not least, the indent field */
5601 if (lpLVItem->mask & LVIF_INDENT)
5602 lpLVItem->iIndent = lpItem->iIndent;
5609 * Retrieves item attributes.
5612 * [I] hwnd : window handle
5613 * [IO] lpLVItem : item info
5614 * [I] isW : if TRUE, then lpLVItem is a LPLVITEMW,
5615 * if FALSE, then lpLVItem is a LPLVITEMA.
5618 * This is the external 'GetItem' interface -- it properly copies
5619 * the text in the provided buffer.
5625 static BOOL LISTVIEW_GetItemExtT(const LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem, BOOL isW)
5630 if (!lpLVItem || lpLVItem->iItem < 0 || lpLVItem->iItem >= infoPtr->nItemCount)
5633 pszText = lpLVItem->pszText;
5634 bResult = LISTVIEW_GetItemT(infoPtr, lpLVItem, isW);
5635 if (bResult && lpLVItem->pszText != pszText)
5636 textcpynT(pszText, isW, lpLVItem->pszText, isW, lpLVItem->cchTextMax);
5637 lpLVItem->pszText = pszText;
5645 * Retrieves the position (upper-left) of the listview control item.
5646 * Note that for LVS_ICON style, the upper-left is that of the icon
5647 * and not the bounding box.
5650 * [I] infoPtr : valid pointer to the listview structure
5651 * [I] nItem : item index
5652 * [O] lpptPosition : coordinate information
5658 static BOOL LISTVIEW_GetItemPosition(const LISTVIEW_INFO *infoPtr, INT nItem, LPPOINT lpptPosition)
5660 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
5663 TRACE("(nItem=%d, lpptPosition=%p)\n", nItem, lpptPosition);
5665 if (!lpptPosition || nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
5667 LISTVIEW_GetOrigin(infoPtr, &Origin);
5668 LISTVIEW_GetItemOrigin(infoPtr, nItem, lpptPosition);
5670 if (uView == LVS_ICON)
5672 lpptPosition->x += (infoPtr->nItemWidth - infoPtr->iconSize.cx) / 2;
5673 lpptPosition->y += ICON_TOP_PADDING;
5675 lpptPosition->x += Origin.x;
5676 lpptPosition->y += Origin.y;
5678 TRACE (" lpptPosition=%s\n", wine_dbgstr_point(lpptPosition));
5685 * Retrieves the bounding rectangle for a listview control item.
5688 * [I] infoPtr : valid pointer to the listview structure
5689 * [I] nItem : item index
5690 * [IO] lprc : bounding rectangle coordinates
5691 * lprc->left specifies the portion of the item for which the bounding
5692 * rectangle will be retrieved.
5694 * LVIR_BOUNDS Returns the bounding rectangle of the entire item,
5695 * including the icon and label.
5698 * * Experiment shows that native control returns:
5699 * * width = min (48, length of text line)
5700 * * .left = position.x - (width - iconsize.cx)/2
5701 * * .right = .left + width
5702 * * height = #lines of text * ntmHeight + icon height + 8
5703 * * .top = position.y - 2
5704 * * .bottom = .top + height
5705 * * separation between items .y = itemSpacing.cy - height
5706 * * .x = itemSpacing.cx - width
5707 * LVIR_ICON Returns the bounding rectangle of the icon or small icon.
5710 * * Experiment shows that native control returns:
5711 * * width = iconSize.cx + 16
5712 * * .left = position.x - (width - iconsize.cx)/2
5713 * * .right = .left + width
5714 * * height = iconSize.cy + 4
5715 * * .top = position.y - 2
5716 * * .bottom = .top + height
5717 * * separation between items .y = itemSpacing.cy - height
5718 * * .x = itemSpacing.cx - width
5719 * LVIR_LABEL Returns the bounding rectangle of the item text.
5722 * * Experiment shows that native control returns:
5723 * * width = text length
5724 * * .left = position.x - width/2
5725 * * .right = .left + width
5726 * * height = ntmH * linecount + 2
5727 * * .top = position.y + iconSize.cy + 6
5728 * * .bottom = .top + height
5729 * * separation between items .y = itemSpacing.cy - height
5730 * * .x = itemSpacing.cx - width
5731 * LVIR_SELECTBOUNDS Returns the union of the LVIR_ICON and LVIR_LABEL
5732 * rectangles, but excludes columns in report view.
5739 * Note that the bounding rectangle of the label in the LVS_ICON view depends
5740 * upon whether the window has the focus currently and on whether the item
5741 * is the one with the focus. Ensure that the control's record of which
5742 * item has the focus agrees with the items' records.
5744 static BOOL LISTVIEW_GetItemRect(const LISTVIEW_INFO *infoPtr, INT nItem, LPRECT lprc)
5746 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
5747 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
5748 BOOL doLabel = TRUE, oversizedBox = FALSE;
5749 POINT Position, Origin;
5752 TRACE("(hwnd=%p, nItem=%d, lprc=%p)\n", infoPtr->hwndSelf, nItem, lprc);
5754 if (!lprc || nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
5756 LISTVIEW_GetOrigin(infoPtr, &Origin);
5757 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
5759 /* Be smart and try to figure out the minimum we have to do */
5760 if (lprc->left == LVIR_ICON) doLabel = FALSE;
5761 if (uView == LVS_REPORT && lprc->left == LVIR_BOUNDS) doLabel = FALSE;
5762 if (uView == LVS_ICON && lprc->left != LVIR_ICON &&
5763 infoPtr->bFocus && LISTVIEW_GetItemState(infoPtr, nItem, LVIS_FOCUSED))
5764 oversizedBox = TRUE;
5766 /* get what we need from the item before hand, so we make
5767 * only one request. This can speed up things, if data
5768 * is stored on the app side */
5770 if (uView == LVS_REPORT) lvItem.mask |= LVIF_INDENT;
5771 if (doLabel) lvItem.mask |= LVIF_TEXT;
5772 lvItem.iItem = nItem;
5773 lvItem.iSubItem = 0;
5774 lvItem.pszText = szDispText;
5775 lvItem.cchTextMax = DISP_TEXT_SIZE;
5776 if (lvItem.mask && !LISTVIEW_GetItemW(infoPtr, &lvItem)) return FALSE;
5777 /* we got the state already up, simulate it here, to avoid a reget */
5778 if (uView == LVS_ICON && (lprc->left != LVIR_ICON))
5780 lvItem.mask |= LVIF_STATE;
5781 lvItem.stateMask = LVIS_FOCUSED;
5782 lvItem.state = (oversizedBox ? LVIS_FOCUSED : 0);
5785 if (uView == LVS_REPORT && (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) && lprc->left == LVIR_SELECTBOUNDS)
5786 lprc->left = LVIR_BOUNDS;
5790 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, NULL, lprc, NULL, NULL);
5794 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, NULL, NULL, NULL, lprc);
5798 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, lprc, NULL, NULL, NULL, NULL);
5801 case LVIR_SELECTBOUNDS:
5802 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, lprc, NULL, NULL, NULL);
5806 WARN("Unknown value: %d\n", lprc->left);
5810 OffsetRect(lprc, Position.x + Origin.x, Position.y + Origin.y);
5812 TRACE(" rect=%s\n", wine_dbgstr_rect(lprc));
5819 * Retrieves the spacing between listview control items.
5822 * [I] infoPtr : valid pointer to the listview structure
5823 * [IO] lprc : rectangle to receive the output
5824 * on input, lprc->top = nSubItem
5825 * lprc->left = LVIR_ICON | LVIR_BOUNDS | LVIR_LABEL
5827 * NOTE: for subItem = 0, we should return the bounds of the _entire_ item,
5828 * not only those of the first column.
5829 * Fortunately, LISTVIEW_GetItemMetrics does the right thing.
5835 static BOOL LISTVIEW_GetSubItemRect(const LISTVIEW_INFO *infoPtr, INT nItem, LPRECT lprc)
5841 if (!lprc) return FALSE;
5843 nColumn = lprc->top;
5845 TRACE("(nItem=%d, nSubItem=%d)\n", nItem, lprc->top);
5846 /* On WinNT, a subitem of '0' calls LISTVIEW_GetItemRect */
5848 return LISTVIEW_GetItemRect(infoPtr, nItem, lprc);
5850 if ((infoPtr->dwStyle & LVS_TYPEMASK) != LVS_REPORT) return FALSE;
5852 if (!LISTVIEW_GetItemPosition(infoPtr, nItem, &Position)) return FALSE;
5854 if (nColumn < 0 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
5857 lvItem.iItem = nItem;
5858 lvItem.iSubItem = nColumn;
5860 if (lvItem.mask && !LISTVIEW_GetItemW(infoPtr, &lvItem)) return FALSE;
5864 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, NULL, lprc, NULL, NULL);
5869 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, lprc, NULL, NULL, NULL, NULL);
5873 ERR("Unknown bounds=%d\n", lprc->left);
5877 OffsetRect(lprc, Position.x, Position.y);
5884 * Retrieves the width of a label.
5887 * [I] infoPtr : valid pointer to the listview structure
5890 * SUCCESS : string width (in pixels)
5893 static INT LISTVIEW_GetLabelWidth(const LISTVIEW_INFO *infoPtr, INT nItem)
5895 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
5898 TRACE("(nItem=%d)\n", nItem);
5900 lvItem.mask = LVIF_TEXT;
5901 lvItem.iItem = nItem;
5902 lvItem.iSubItem = 0;
5903 lvItem.pszText = szDispText;
5904 lvItem.cchTextMax = DISP_TEXT_SIZE;
5905 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return 0;
5907 return LISTVIEW_GetStringWidthT(infoPtr, lvItem.pszText, TRUE);
5912 * Retrieves the spacing between listview control items.
5915 * [I] infoPtr : valid pointer to the listview structure
5916 * [I] bSmall : flag for small or large icon
5919 * Horizontal + vertical spacing
5921 static LONG LISTVIEW_GetItemSpacing(const LISTVIEW_INFO *infoPtr, BOOL bSmall)
5927 lResult = MAKELONG(infoPtr->iconSpacing.cx, infoPtr->iconSpacing.cy);
5931 if ((infoPtr->dwStyle & LVS_TYPEMASK) == LVS_ICON)
5932 lResult = MAKELONG(DEFAULT_COLUMN_WIDTH, GetSystemMetrics(SM_CXSMICON)+HEIGHT_PADDING);
5934 lResult = MAKELONG(infoPtr->nItemWidth, infoPtr->nItemHeight);
5941 * Retrieves the state of a listview control item.
5944 * [I] infoPtr : valid pointer to the listview structure
5945 * [I] nItem : item index
5946 * [I] uMask : state mask
5949 * State specified by the mask.
5951 static UINT LISTVIEW_GetItemState(const LISTVIEW_INFO *infoPtr, INT nItem, UINT uMask)
5955 if (nItem < 0 || nItem >= infoPtr->nItemCount) return 0;
5957 lvItem.iItem = nItem;
5958 lvItem.iSubItem = 0;
5959 lvItem.mask = LVIF_STATE;
5960 lvItem.stateMask = uMask;
5961 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return 0;
5963 return lvItem.state & uMask;
5968 * Retrieves the text of a listview control item or subitem.
5971 * [I] hwnd : window handle
5972 * [I] nItem : item index
5973 * [IO] lpLVItem : item information
5974 * [I] isW : TRUE if lpLVItem is Unicode
5977 * SUCCESS : string length
5980 static INT LISTVIEW_GetItemTextT(const LISTVIEW_INFO *infoPtr, INT nItem, LPLVITEMW lpLVItem, BOOL isW)
5982 if (!lpLVItem || nItem < 0 || nItem >= infoPtr->nItemCount) return 0;
5984 lpLVItem->mask = LVIF_TEXT;
5985 lpLVItem->iItem = nItem;
5986 if (!LISTVIEW_GetItemExtT(infoPtr, lpLVItem, isW)) return 0;
5988 return textlenT(lpLVItem->pszText, isW);
5993 * Searches for an item based on properties + relationships.
5996 * [I] infoPtr : valid pointer to the listview structure
5997 * [I] nItem : item index
5998 * [I] uFlags : relationship flag
6001 * SUCCESS : item index
6004 static INT LISTVIEW_GetNextItem(const LISTVIEW_INFO *infoPtr, INT nItem, UINT uFlags)
6006 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
6008 LVFINDINFOW lvFindInfo;
6009 INT nCountPerColumn;
6013 TRACE("nItem=%d, uFlags=%x, nItemCount=%d\n", nItem, uFlags, infoPtr->nItemCount);
6014 if (nItem < -1 || nItem >= infoPtr->nItemCount) return -1;
6016 ZeroMemory(&lvFindInfo, sizeof(lvFindInfo));
6018 if (uFlags & LVNI_CUT)
6021 if (uFlags & LVNI_DROPHILITED)
6022 uMask |= LVIS_DROPHILITED;
6024 if (uFlags & LVNI_FOCUSED)
6025 uMask |= LVIS_FOCUSED;
6027 if (uFlags & LVNI_SELECTED)
6028 uMask |= LVIS_SELECTED;
6030 /* if we're asked for the focused item, that's only one,
6031 * so it's worth optimizing */
6032 if (uFlags & LVNI_FOCUSED)
6034 if ((LISTVIEW_GetItemState(infoPtr, infoPtr->nFocusedItem, uMask) & uMask) != uMask) return -1;
6035 return (infoPtr->nFocusedItem == nItem) ? -1 : infoPtr->nFocusedItem;
6038 if (uFlags & LVNI_ABOVE)
6040 if ((uView == LVS_LIST) || (uView == LVS_REPORT))
6045 if ((ListView_GetItemState(infoPtr->hwndSelf, nItem, uMask) & uMask) == uMask)
6051 /* Special case for autoarrange - move 'til the top of a list */
6052 if (is_autoarrange(infoPtr))
6054 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
6055 while (nItem - nCountPerRow >= 0)
6057 nItem -= nCountPerRow;
6058 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
6063 lvFindInfo.flags = LVFI_NEARESTXY;
6064 lvFindInfo.vkDirection = VK_UP;
6065 SendMessageW( infoPtr->hwndSelf, LVM_GETITEMPOSITION, nItem, (LPARAM)&lvFindInfo.pt );
6066 while ((nItem = ListView_FindItemW(infoPtr->hwndSelf, nItem, &lvFindInfo)) != -1)
6068 if ((ListView_GetItemState(infoPtr->hwndSelf, nItem, uMask) & uMask) == uMask)
6073 else if (uFlags & LVNI_BELOW)
6075 if ((uView == LVS_LIST) || (uView == LVS_REPORT))
6077 while (nItem < infoPtr->nItemCount)
6080 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
6086 /* Special case for autoarrange - move 'til the bottom of a list */
6087 if (is_autoarrange(infoPtr))
6089 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
6090 while (nItem + nCountPerRow < infoPtr->nItemCount )
6092 nItem += nCountPerRow;
6093 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
6098 lvFindInfo.flags = LVFI_NEARESTXY;
6099 lvFindInfo.vkDirection = VK_DOWN;
6100 SendMessageW( infoPtr->hwndSelf, LVM_GETITEMPOSITION, nItem, (LPARAM)&lvFindInfo.pt );
6101 while ((nItem = ListView_FindItemW(infoPtr->hwndSelf, nItem, &lvFindInfo)) != -1)
6103 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
6108 else if (uFlags & LVNI_TOLEFT)
6110 if (uView == LVS_LIST)
6112 nCountPerColumn = LISTVIEW_GetCountPerColumn(infoPtr);
6113 while (nItem - nCountPerColumn >= 0)
6115 nItem -= nCountPerColumn;
6116 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
6120 else if ((uView == LVS_SMALLICON) || (uView == LVS_ICON))
6122 /* Special case for autoarrange - move 'ti the beginning of a row */
6123 if (is_autoarrange(infoPtr))
6125 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
6126 while (nItem % nCountPerRow > 0)
6129 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
6134 lvFindInfo.flags = LVFI_NEARESTXY;
6135 lvFindInfo.vkDirection = VK_LEFT;
6136 SendMessageW( infoPtr->hwndSelf, LVM_GETITEMPOSITION, nItem, (LPARAM)&lvFindInfo.pt );
6137 while ((nItem = ListView_FindItemW(infoPtr->hwndSelf, nItem, &lvFindInfo)) != -1)
6139 if ((ListView_GetItemState(infoPtr->hwndSelf, nItem, uMask) & uMask) == uMask)
6144 else if (uFlags & LVNI_TORIGHT)
6146 if (uView == LVS_LIST)
6148 nCountPerColumn = LISTVIEW_GetCountPerColumn(infoPtr);
6149 while (nItem + nCountPerColumn < infoPtr->nItemCount)
6151 nItem += nCountPerColumn;
6152 if ((ListView_GetItemState(infoPtr->hwndSelf, nItem, uMask) & uMask) == uMask)
6156 else if ((uView == LVS_SMALLICON) || (uView == LVS_ICON))
6158 /* Special case for autoarrange - move 'til the end of a row */
6159 if (is_autoarrange(infoPtr))
6161 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
6162 while (nItem % nCountPerRow < nCountPerRow - 1 )
6165 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
6170 lvFindInfo.flags = LVFI_NEARESTXY;
6171 lvFindInfo.vkDirection = VK_RIGHT;
6172 SendMessageW( infoPtr->hwndSelf, LVM_GETITEMPOSITION, nItem, (LPARAM)&lvFindInfo.pt );
6173 while ((nItem = ListView_FindItemW(infoPtr->hwndSelf, nItem, &lvFindInfo)) != -1)
6175 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
6184 /* search by index */
6185 for (i = nItem; i < infoPtr->nItemCount; i++)
6187 if ((LISTVIEW_GetItemState(infoPtr, i, uMask) & uMask) == uMask)
6195 /* LISTVIEW_GetNumberOfWorkAreas */
6199 * Retrieves the origin coordinates when in icon or small icon display mode.
6202 * [I] infoPtr : valid pointer to the listview structure
6203 * [O] lpptOrigin : coordinate information
6208 static void LISTVIEW_GetOrigin(const LISTVIEW_INFO *infoPtr, LPPOINT lpptOrigin)
6210 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
6211 INT nHorzPos = 0, nVertPos = 0;
6212 SCROLLINFO scrollInfo;
6214 scrollInfo.cbSize = sizeof(SCROLLINFO);
6215 scrollInfo.fMask = SIF_POS;
6217 if (GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo))
6218 nHorzPos = scrollInfo.nPos;
6219 if (GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
6220 nVertPos = scrollInfo.nPos;
6222 TRACE("nHorzPos=%d, nVertPos=%d\n", nHorzPos, nVertPos);
6224 lpptOrigin->x = infoPtr->rcList.left;
6225 lpptOrigin->y = infoPtr->rcList.top;
6226 if (uView == LVS_LIST)
6227 nHorzPos *= infoPtr->nItemWidth;
6228 else if (uView == LVS_REPORT)
6229 nVertPos *= infoPtr->nItemHeight;
6231 lpptOrigin->x -= nHorzPos;
6232 lpptOrigin->y -= nVertPos;
6234 TRACE(" origin=%s\n", wine_dbgstr_point(lpptOrigin));
6239 * Retrieves the width of a string.
6242 * [I] hwnd : window handle
6243 * [I] lpszText : text string to process
6244 * [I] isW : TRUE if lpszText is Unicode, FALSE otherwise
6247 * SUCCESS : string width (in pixels)
6250 static INT LISTVIEW_GetStringWidthT(const LISTVIEW_INFO *infoPtr, LPCWSTR lpszText, BOOL isW)
6255 if (is_textT(lpszText, isW))
6257 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
6258 HDC hdc = GetDC(infoPtr->hwndSelf);
6259 HFONT hOldFont = SelectObject(hdc, hFont);
6262 GetTextExtentPointW(hdc, lpszText, lstrlenW(lpszText), &stringSize);
6264 GetTextExtentPointA(hdc, (LPCSTR)lpszText, lstrlenA((LPCSTR)lpszText), &stringSize);
6265 SelectObject(hdc, hOldFont);
6266 ReleaseDC(infoPtr->hwndSelf, hdc);
6268 return stringSize.cx;
6273 * Determines which listview item is located at the specified position.
6276 * [I] infoPtr : valid pointer to the listview structure
6277 * [IO] lpht : hit test information
6278 * [I] subitem : fill out iSubItem.
6279 * [I] select : return the index only if the hit selects the item
6282 * (mm 20001022): We must not allow iSubItem to be touched, for
6283 * an app might pass only a structure with space up to iItem!
6284 * (MS Office 97 does that for instance in the file open dialog)
6287 * SUCCESS : item index
6290 static INT LISTVIEW_HitTest(const LISTVIEW_INFO *infoPtr, LPLVHITTESTINFO lpht, BOOL subitem, BOOL select)
6292 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
6293 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
6294 RECT rcBox, rcBounds, rcState, rcIcon, rcLabel, rcSearch;
6295 POINT Origin, Position, opt;
6300 TRACE("(pt=%s, subitem=%d, select=%d)\n", wine_dbgstr_point(&lpht->pt), subitem, select);
6304 if (subitem) lpht->iSubItem = 0;
6306 if (infoPtr->rcList.left > lpht->pt.x)
6307 lpht->flags |= LVHT_TOLEFT;
6308 else if (infoPtr->rcList.right < lpht->pt.x)
6309 lpht->flags |= LVHT_TORIGHT;
6311 if (infoPtr->rcList.top > lpht->pt.y)
6312 lpht->flags |= LVHT_ABOVE;
6313 else if (infoPtr->rcList.bottom < lpht->pt.y)
6314 lpht->flags |= LVHT_BELOW;
6316 TRACE("lpht->flags=0x%x\n", lpht->flags);
6317 if (lpht->flags) return -1;
6319 lpht->flags |= LVHT_NOWHERE;
6321 LISTVIEW_GetOrigin(infoPtr, &Origin);
6323 /* first deal with the large items */
6324 rcSearch.left = lpht->pt.x;
6325 rcSearch.top = lpht->pt.y;
6326 rcSearch.right = rcSearch.left + 1;
6327 rcSearch.bottom = rcSearch.top + 1;
6329 iterator_frameditems(&i, infoPtr, &rcSearch);
6330 iterator_next(&i); /* go to first item in the sequence */
6332 iterator_destroy(&i);
6334 TRACE("lpht->iItem=%d\n", iItem);
6335 if (iItem == -1) return -1;
6337 lvItem.mask = LVIF_STATE | LVIF_TEXT;
6338 if (uView == LVS_REPORT) lvItem.mask |= LVIF_INDENT;
6339 lvItem.stateMask = LVIS_STATEIMAGEMASK;
6340 if (uView == LVS_ICON) lvItem.stateMask |= LVIS_FOCUSED;
6341 lvItem.iItem = iItem;
6342 lvItem.iSubItem = 0;
6343 lvItem.pszText = szDispText;
6344 lvItem.cchTextMax = DISP_TEXT_SIZE;
6345 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return -1;
6346 if (!infoPtr->bFocus) lvItem.state &= ~LVIS_FOCUSED;
6348 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, &rcBox, NULL, &rcIcon, &rcState, &rcLabel);
6349 LISTVIEW_GetItemOrigin(infoPtr, iItem, &Position);
6350 opt.x = lpht->pt.x - Position.x - Origin.x;
6351 opt.y = lpht->pt.y - Position.y - Origin.y;
6353 if (uView == LVS_REPORT)
6357 UnionRect(&rcBounds, &rcIcon, &rcLabel);
6358 UnionRect(&rcBounds, &rcBounds, &rcState);
6360 TRACE("rcBounds=%s\n", wine_dbgstr_rect(&rcBounds));
6361 if (!PtInRect(&rcBounds, opt)) return -1;
6363 if (PtInRect(&rcIcon, opt))
6364 lpht->flags |= LVHT_ONITEMICON;
6365 else if (PtInRect(&rcLabel, opt))
6366 lpht->flags |= LVHT_ONITEMLABEL;
6367 else if (infoPtr->himlState && STATEIMAGEINDEX(lvItem.state) && PtInRect(&rcState, opt))
6368 lpht->flags |= LVHT_ONITEMSTATEICON;
6369 if (lpht->flags & LVHT_ONITEM)
6370 lpht->flags &= ~LVHT_NOWHERE;
6372 TRACE("lpht->flags=0x%x\n", lpht->flags);
6373 if (uView == LVS_REPORT && subitem)
6377 rcBounds.right = rcBounds.left;
6378 for (j = 0; j < DPA_GetPtrCount(infoPtr->hdpaColumns); j++)
6380 rcBounds.left = rcBounds.right;
6381 rcBounds.right += LISTVIEW_GetColumnWidth(infoPtr, j);
6382 if (PtInRect(&rcBounds, opt))
6390 if (select && !(uView == LVS_REPORT &&
6391 ((infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) ||
6392 (infoPtr->dwStyle & LVS_OWNERDRAWFIXED))))
6394 if (uView == LVS_REPORT)
6396 UnionRect(&rcBounds, &rcIcon, &rcLabel);
6397 UnionRect(&rcBounds, &rcBounds, &rcState);
6399 if (!PtInRect(&rcBounds, opt)) iItem = -1;
6401 return lpht->iItem = iItem;
6405 /* LISTVIEW_InsertCompare: callback routine for comparing pszText members of the LV_ITEMS
6406 in a LISTVIEW on insert. Passed to DPA_Sort in LISTVIEW_InsertItem.
6407 This function should only be used for inserting items into a sorted list (LVM_INSERTITEM)
6408 and not during the processing of a LVM_SORTITEMS message. Applications should provide
6409 their own sort proc. when sending LVM_SORTITEMS.
6412 (remarks on LVITEM: LVM_INSERTITEM will insert the new item in the proper sort postion...
6414 LVS_SORTXXX must be specified,
6415 LVS_OWNERDRAW is not set,
6416 <item>.pszText is not LPSTR_TEXTCALLBACK.
6418 (LVS_SORT* flags): "For the LVS_SORTASCENDING... styles, item indices
6419 are sorted based on item text..."
6421 static INT WINAPI LISTVIEW_InsertCompare( LPVOID first, LPVOID second, LPARAM lParam)
6423 ITEM_INFO* lv_first = (ITEM_INFO*) DPA_GetPtr( (HDPA)first, 0 );
6424 ITEM_INFO* lv_second = (ITEM_INFO*) DPA_GetPtr( (HDPA)second, 0 );
6425 INT cmpv = textcmpWT(lv_first->hdr.pszText, lv_second->hdr.pszText, TRUE);
6427 /* if we're sorting descending, negate the return value */
6428 return (((const LISTVIEW_INFO *)lParam)->dwStyle & LVS_SORTDESCENDING) ? -cmpv : cmpv;
6433 * Inserts a new item in the listview control.
6436 * [I] infoPtr : valid pointer to the listview structure
6437 * [I] lpLVItem : item information
6438 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
6441 * SUCCESS : new item index
6444 static INT LISTVIEW_InsertItemT(LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem, BOOL isW)
6446 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
6451 BOOL is_sorted, has_changed;
6453 HWND hwndSelf = infoPtr->hwndSelf;
6455 TRACE("(lpLVItem=%s, isW=%d)\n", debuglvitem_t(lpLVItem, isW), isW);
6457 if (infoPtr->dwStyle & LVS_OWNERDATA) return infoPtr->nItemCount++;
6459 /* make sure it's an item, and not a subitem; cannot insert a subitem */
6460 if (!lpLVItem || lpLVItem->iSubItem) return -1;
6462 if (!is_assignable_item(lpLVItem, infoPtr->dwStyle)) return -1;
6464 if (!(lpItem = Alloc(sizeof(ITEM_INFO)))) return -1;
6466 /* insert item in listview control data structure */
6467 if ( !(hdpaSubItems = DPA_Create(8)) ) goto fail;
6468 if ( !DPA_SetPtr(hdpaSubItems, 0, lpItem) ) assert (FALSE);
6470 is_sorted = (infoPtr->dwStyle & (LVS_SORTASCENDING | LVS_SORTDESCENDING)) &&
6471 !(infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (LPSTR_TEXTCALLBACKW != lpLVItem->pszText);
6473 if (lpLVItem->iItem < 0 && !is_sorted) return -1;
6475 nItem = is_sorted ? infoPtr->nItemCount : min(lpLVItem->iItem, infoPtr->nItemCount);
6476 TRACE(" inserting at %d, sorted=%d, count=%d, iItem=%d\n", nItem, is_sorted, infoPtr->nItemCount, lpLVItem->iItem);
6477 nItem = DPA_InsertPtr( infoPtr->hdpaItems, nItem, hdpaSubItems );
6478 if (nItem == -1) goto fail;
6479 infoPtr->nItemCount++;
6481 /* shift indices first so they don't get tangled */
6482 LISTVIEW_ShiftIndices(infoPtr, nItem, 1);
6484 /* set the item attributes */
6485 if (lpLVItem->mask & (LVIF_GROUPID|LVIF_COLUMNS))
6487 /* full size structure expected - _WIN32IE >= 0x560 */
6490 else if (lpLVItem->mask & LVIF_INDENT)
6492 /* indent member expected - _WIN32IE >= 0x300 */
6493 memcpy(&item, lpLVItem, offsetof( LVITEMW, iGroupId ));
6497 /* minimal structure expected */
6498 memcpy(&item, lpLVItem, offsetof( LVITEMW, iIndent ));
6501 if (infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES)
6503 item.mask |= LVIF_STATE;
6504 item.stateMask |= LVIS_STATEIMAGEMASK;
6505 item.state &= ~LVIS_STATEIMAGEMASK;
6506 item.state |= INDEXTOSTATEIMAGEMASK(1);
6508 if (!set_main_item(infoPtr, &item, TRUE, isW, &has_changed)) goto undo;
6510 /* if we're sorted, sort the list, and update the index */
6513 DPA_Sort( infoPtr->hdpaItems, LISTVIEW_InsertCompare, (LPARAM)infoPtr );
6514 nItem = DPA_GetPtrIndex( infoPtr->hdpaItems, hdpaSubItems );
6515 assert(nItem != -1);
6518 /* make room for the position, if we are in the right mode */
6519 if ((uView == LVS_SMALLICON) || (uView == LVS_ICON))
6521 if (DPA_InsertPtr(infoPtr->hdpaPosX, nItem, 0) == -1)
6523 if (DPA_InsertPtr(infoPtr->hdpaPosY, nItem, 0) == -1)
6525 DPA_DeletePtr(infoPtr->hdpaPosX, nItem);
6530 /* send LVN_INSERTITEM notification */
6531 ZeroMemory(&nmlv, sizeof(NMLISTVIEW));
6533 nmlv.lParam = lpItem->lParam;
6534 notify_listview(infoPtr, LVN_INSERTITEM, &nmlv);
6535 if (!IsWindow(hwndSelf))
6538 /* align items (set position of each item) */
6539 if ((uView == LVS_SMALLICON || uView == LVS_ICON))
6543 if (infoPtr->dwStyle & LVS_ALIGNLEFT)
6544 LISTVIEW_NextIconPosLeft(infoPtr, &pt);
6546 LISTVIEW_NextIconPosTop(infoPtr, &pt);
6548 LISTVIEW_MoveIconTo(infoPtr, nItem, &pt, TRUE);
6551 /* now is the invalidation fun */
6552 LISTVIEW_ScrollOnInsert(infoPtr, nItem, 1);
6556 LISTVIEW_ShiftIndices(infoPtr, nItem, -1);
6557 DPA_DeletePtr(infoPtr->hdpaItems, nItem);
6558 infoPtr->nItemCount--;
6560 DPA_DeletePtr(hdpaSubItems, 0);
6561 DPA_Destroy (hdpaSubItems);
6568 * Redraws a range of items.
6571 * [I] infoPtr : valid pointer to the listview structure
6572 * [I] nFirst : first item
6573 * [I] nLast : last item
6579 static BOOL LISTVIEW_RedrawItems(const LISTVIEW_INFO *infoPtr, INT nFirst, INT nLast)
6583 if (nLast < nFirst || min(nFirst, nLast) < 0 ||
6584 max(nFirst, nLast) >= infoPtr->nItemCount)
6587 for (i = nFirst; i <= nLast; i++)
6588 LISTVIEW_InvalidateItem(infoPtr, i);
6595 * Scroll the content of a listview.
6598 * [I] infoPtr : valid pointer to the listview structure
6599 * [I] dx : horizontal scroll amount in pixels
6600 * [I] dy : vertical scroll amount in pixels
6607 * If the control is in report mode (LVS_REPORT) the control can
6608 * be scrolled only in line increments. "dy" will be rounded to the
6609 * nearest number of pixels that are a whole line. Ex: if line height
6610 * is 16 and an 8 is passed, the list will be scrolled by 16. If a 7
6611 * is passed, then the scroll will be 0. (per MSDN 7/2002)
6613 * For: (per experimentation with native control and CSpy ListView)
6614 * LVS_ICON dy=1 = 1 pixel (vertical only)
6616 * LVS_SMALLICON dy=1 = 1 pixel (vertical only)
6618 * LVS_LIST dx=1 = 1 column (horizontal only)
6619 * but will only scroll 1 column per message
6620 * no matter what the value.
6621 * dy must be 0 or FALSE returned.
6622 * LVS_REPORT dx=1 = 1 pixel
6626 static BOOL LISTVIEW_Scroll(LISTVIEW_INFO *infoPtr, INT dx, INT dy)
6628 switch(infoPtr->dwStyle & LVS_TYPEMASK) {
6630 dy += (dy < 0 ? -1 : 1) * infoPtr->nItemHeight/2;
6631 dy /= infoPtr->nItemHeight;
6634 if (dy != 0) return FALSE;
6641 if (dx != 0) LISTVIEW_HScroll(infoPtr, SB_INTERNAL, dx, 0);
6642 if (dy != 0) LISTVIEW_VScroll(infoPtr, SB_INTERNAL, dy, 0);
6649 * Sets the background color.
6652 * [I] infoPtr : valid pointer to the listview structure
6653 * [I] clrBk : background color
6659 static BOOL LISTVIEW_SetBkColor(LISTVIEW_INFO *infoPtr, COLORREF clrBk)
6661 TRACE("(clrBk=%x)\n", clrBk);
6663 if(infoPtr->clrBk != clrBk) {
6664 if (infoPtr->clrBk != CLR_NONE) DeleteObject(infoPtr->hBkBrush);
6665 infoPtr->clrBk = clrBk;
6666 if (clrBk == CLR_NONE)
6667 infoPtr->hBkBrush = (HBRUSH)GetClassLongPtrW(infoPtr->hwndSelf, GCLP_HBRBACKGROUND);
6669 infoPtr->hBkBrush = CreateSolidBrush(clrBk);
6670 LISTVIEW_InvalidateList(infoPtr);
6676 /* LISTVIEW_SetBkImage */
6678 /*** Helper for {Insert,Set}ColumnT *only* */
6679 static void column_fill_hditem(const LISTVIEW_INFO *infoPtr, HDITEMW *lphdi, INT nColumn,
6680 const LVCOLUMNW *lpColumn, BOOL isW)
6682 if (lpColumn->mask & LVCF_FMT)
6684 /* format member is valid */
6685 lphdi->mask |= HDI_FORMAT;
6687 /* set text alignment (leftmost column must be left-aligned) */
6688 if (nColumn == 0 || (lpColumn->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_LEFT)
6689 lphdi->fmt |= HDF_LEFT;
6690 else if ((lpColumn->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_RIGHT)
6691 lphdi->fmt |= HDF_RIGHT;
6692 else if ((lpColumn->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_CENTER)
6693 lphdi->fmt |= HDF_CENTER;
6695 if (lpColumn->fmt & LVCFMT_BITMAP_ON_RIGHT)
6696 lphdi->fmt |= HDF_BITMAP_ON_RIGHT;
6698 if (lpColumn->fmt & LVCFMT_COL_HAS_IMAGES)
6700 lphdi->fmt |= HDF_IMAGE;
6701 lphdi->iImage = I_IMAGECALLBACK;
6705 if (lpColumn->mask & LVCF_WIDTH)
6707 lphdi->mask |= HDI_WIDTH;
6708 if(lpColumn->cx == LVSCW_AUTOSIZE_USEHEADER)
6710 /* make it fill the remainder of the controls width */
6714 for(item_index = 0; item_index < (nColumn - 1); item_index++)
6716 LISTVIEW_GetHeaderRect(infoPtr, item_index, &rcHeader);
6717 lphdi->cxy += rcHeader.right - rcHeader.left;
6720 /* retrieve the layout of the header */
6721 GetClientRect(infoPtr->hwndSelf, &rcHeader);
6722 TRACE("start cxy=%d rcHeader=%s\n", lphdi->cxy, wine_dbgstr_rect(&rcHeader));
6724 lphdi->cxy = (rcHeader.right - rcHeader.left) - lphdi->cxy;
6727 lphdi->cxy = lpColumn->cx;
6730 if (lpColumn->mask & LVCF_TEXT)
6732 lphdi->mask |= HDI_TEXT | HDI_FORMAT;
6733 lphdi->fmt |= HDF_STRING;
6734 lphdi->pszText = lpColumn->pszText;
6735 lphdi->cchTextMax = textlenT(lpColumn->pszText, isW);
6738 if (lpColumn->mask & LVCF_IMAGE)
6740 lphdi->mask |= HDI_IMAGE;
6741 lphdi->iImage = lpColumn->iImage;
6744 if (lpColumn->mask & LVCF_ORDER)
6746 lphdi->mask |= HDI_ORDER;
6747 lphdi->iOrder = lpColumn->iOrder;
6754 * Inserts a new column.
6757 * [I] infoPtr : valid pointer to the listview structure
6758 * [I] nColumn : column index
6759 * [I] lpColumn : column information
6760 * [I] isW : TRUE if lpColumn is Unicode, FALSE otherwise
6763 * SUCCESS : new column index
6766 static INT LISTVIEW_InsertColumnT(LISTVIEW_INFO *infoPtr, INT nColumn,
6767 const LVCOLUMNW *lpColumn, BOOL isW)
6769 COLUMN_INFO *lpColumnInfo;
6773 TRACE("(nColumn=%d, lpColumn=%s, isW=%d)\n", nColumn, debuglvcolumn_t(lpColumn, isW), isW);
6775 if (!lpColumn || nColumn < 0) return -1;
6776 nColumn = min(nColumn, DPA_GetPtrCount(infoPtr->hdpaColumns));
6778 ZeroMemory(&hdi, sizeof(HDITEMW));
6779 column_fill_hditem(infoPtr, &hdi, nColumn, lpColumn, isW);
6782 * A mask not including LVCF_WIDTH turns into a mask of width, width 10
6783 * (can be seen in SPY) otherwise column never gets added.
6785 if (!(lpColumn->mask & LVCF_WIDTH)) {
6786 hdi.mask |= HDI_WIDTH;
6791 * when the iSubItem is available Windows copies it to the header lParam. It seems
6792 * to happen only in LVM_INSERTCOLUMN - not in LVM_SETCOLUMN
6794 if (lpColumn->mask & LVCF_SUBITEM)
6796 hdi.mask |= HDI_LPARAM;
6797 hdi.lParam = lpColumn->iSubItem;
6800 /* insert item in header control */
6801 nNewColumn = SendMessageW(infoPtr->hwndHeader,
6802 isW ? HDM_INSERTITEMW : HDM_INSERTITEMA,
6803 (WPARAM)nColumn, (LPARAM)&hdi);
6804 if (nNewColumn == -1) return -1;
6805 if (nNewColumn != nColumn) ERR("nColumn=%d, nNewColumn=%d\n", nColumn, nNewColumn);
6807 /* create our own column info */
6808 if (!(lpColumnInfo = Alloc(sizeof(COLUMN_INFO)))) goto fail;
6809 if (DPA_InsertPtr(infoPtr->hdpaColumns, nNewColumn, lpColumnInfo) == -1) goto fail;
6811 if (lpColumn->mask & LVCF_FMT) lpColumnInfo->fmt = lpColumn->fmt;
6812 if (!Header_GetItemRect(infoPtr->hwndHeader, nNewColumn, &lpColumnInfo->rcHeader)) goto fail;
6814 /* now we have to actually adjust the data */
6815 if (!(infoPtr->dwStyle & LVS_OWNERDATA) && infoPtr->nItemCount > 0)
6817 SUBITEM_INFO *lpSubItem;
6821 for (nItem = 0; nItem < infoPtr->nItemCount; nItem++)
6823 hdpaSubItems = (HDPA)DPA_GetPtr(infoPtr->hdpaItems, nItem);
6824 for (i = 1; i < DPA_GetPtrCount(hdpaSubItems); i++)
6826 lpSubItem = (SUBITEM_INFO *)DPA_GetPtr(hdpaSubItems, i);
6827 if (lpSubItem->iSubItem >= nNewColumn)
6828 lpSubItem->iSubItem++;
6833 /* make space for the new column */
6834 LISTVIEW_ScrollColumns(infoPtr, nNewColumn + 1, lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left);
6835 LISTVIEW_UpdateItemSize(infoPtr);
6840 if (nNewColumn != -1) SendMessageW(infoPtr->hwndHeader, HDM_DELETEITEM, nNewColumn, 0);
6843 DPA_DeletePtr(infoPtr->hdpaColumns, nNewColumn);
6851 * Sets the attributes of a header item.
6854 * [I] infoPtr : valid pointer to the listview structure
6855 * [I] nColumn : column index
6856 * [I] lpColumn : column attributes
6857 * [I] isW: if TRUE, then lpColumn is a LPLVCOLUMNW, else it is a LPLVCOLUMNA
6863 static BOOL LISTVIEW_SetColumnT(const LISTVIEW_INFO *infoPtr, INT nColumn,
6864 const LVCOLUMNW *lpColumn, BOOL isW)
6866 HDITEMW hdi, hdiget;
6869 TRACE("(nColumn=%d, lpColumn=%s, isW=%d)\n", nColumn, debuglvcolumn_t(lpColumn, isW), isW);
6871 if (!lpColumn || nColumn < 0 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
6873 ZeroMemory(&hdi, sizeof(HDITEMW));
6874 if (lpColumn->mask & LVCF_FMT)
6876 hdi.mask |= HDI_FORMAT;
6877 hdiget.mask = HDI_FORMAT;
6878 if (Header_GetItemW(infoPtr->hwndHeader, nColumn, &hdiget))
6879 hdi.fmt = hdiget.fmt & HDF_STRING;
6881 column_fill_hditem(infoPtr, &hdi, nColumn, lpColumn, isW);
6883 /* set header item attributes */
6884 bResult = SendMessageW(infoPtr->hwndHeader, isW ? HDM_SETITEMW : HDM_SETITEMA, (WPARAM)nColumn, (LPARAM)&hdi);
6885 if (!bResult) return FALSE;
6887 if (lpColumn->mask & LVCF_FMT)
6889 COLUMN_INFO *lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, nColumn);
6890 int oldFmt = lpColumnInfo->fmt;
6892 lpColumnInfo->fmt = lpColumn->fmt;
6893 if ((oldFmt ^ lpColumn->fmt) & (LVCFMT_JUSTIFYMASK | LVCFMT_IMAGE))
6895 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
6896 if (uView == LVS_REPORT) LISTVIEW_InvalidateColumn(infoPtr, nColumn);
6905 * Sets the column order array
6908 * [I] infoPtr : valid pointer to the listview structure
6909 * [I] iCount : number of elements in column order array
6910 * [I] lpiArray : pointer to column order array
6916 static BOOL LISTVIEW_SetColumnOrderArray(const LISTVIEW_INFO *infoPtr, INT iCount, const INT *lpiArray)
6918 FIXME("iCount %d lpiArray %p\n", iCount, lpiArray);
6929 * Sets the width of a column
6932 * [I] infoPtr : valid pointer to the listview structure
6933 * [I] nColumn : column index
6934 * [I] cx : column width
6940 static BOOL LISTVIEW_SetColumnWidth(LISTVIEW_INFO *infoPtr, INT nColumn, INT cx)
6942 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
6943 WCHAR szDispText[DISP_TEXT_SIZE] = { 0 };
6947 TRACE("(nColumn=%d, cx=%d\n", nColumn, cx);
6949 /* set column width only if in report or list mode */
6950 if (uView != LVS_REPORT && uView != LVS_LIST) return FALSE;
6952 /* take care of invalid cx values */
6953 if(uView == LVS_REPORT && cx < -2) cx = LVSCW_AUTOSIZE;
6954 else if (uView == LVS_LIST && cx < 1) return FALSE;
6956 /* resize all columns if in LVS_LIST mode */
6957 if(uView == LVS_LIST)
6959 infoPtr->nItemWidth = cx;
6960 LISTVIEW_InvalidateList(infoPtr);
6964 if (nColumn < 0 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
6966 if (cx == LVSCW_AUTOSIZE || (cx == LVSCW_AUTOSIZE_USEHEADER && nColumn < DPA_GetPtrCount(infoPtr->hdpaColumns) -1))
6971 lvItem.mask = LVIF_TEXT;
6973 lvItem.iSubItem = nColumn;
6974 lvItem.pszText = szDispText;
6975 lvItem.cchTextMax = DISP_TEXT_SIZE;
6976 for (; lvItem.iItem < infoPtr->nItemCount; lvItem.iItem++)
6978 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) continue;
6979 nLabelWidth = LISTVIEW_GetStringWidthT(infoPtr, lvItem.pszText, TRUE);
6980 if (max_cx < nLabelWidth) max_cx = nLabelWidth;
6982 if (infoPtr->himlSmall && (nColumn == 0 || (LISTVIEW_GetColumnInfo(infoPtr, nColumn)->fmt & LVCFMT_IMAGE)))
6983 max_cx += infoPtr->iconSize.cx;
6984 max_cx += TRAILING_LABEL_PADDING;
6987 /* autosize based on listview items width */
6988 if(cx == LVSCW_AUTOSIZE)
6990 else if(cx == LVSCW_AUTOSIZE_USEHEADER)
6992 /* if iCol is the last column make it fill the remainder of the controls width */
6993 if(nColumn == DPA_GetPtrCount(infoPtr->hdpaColumns) - 1)
6998 LISTVIEW_GetOrigin(infoPtr, &Origin);
6999 LISTVIEW_GetHeaderRect(infoPtr, nColumn, &rcHeader);
7001 cx = infoPtr->rcList.right - Origin.x - rcHeader.left;
7005 /* Despite what the MS docs say, if this is not the last
7006 column, then MS resizes the column to the width of the
7007 largest text string in the column, including headers
7008 and items. This is different from LVSCW_AUTOSIZE in that
7009 LVSCW_AUTOSIZE ignores the header string length. */
7012 /* retrieve header text */
7013 hdi.mask = HDI_TEXT;
7014 hdi.cchTextMax = DISP_TEXT_SIZE;
7015 hdi.pszText = szDispText;
7016 if (Header_GetItemW(infoPtr->hwndHeader, nColumn, &hdi))
7018 HDC hdc = GetDC(infoPtr->hwndSelf);
7019 HFONT old_font = SelectObject(hdc, (HFONT)SendMessageW(infoPtr->hwndHeader, WM_GETFONT, 0, 0));
7022 if (GetTextExtentPoint32W(hdc, hdi.pszText, lstrlenW(hdi.pszText), &size))
7023 cx = size.cx + TRAILING_HEADER_PADDING;
7024 /* FIXME: Take into account the header image, if one is present */
7025 SelectObject(hdc, old_font);
7026 ReleaseDC(infoPtr->hwndSelf, hdc);
7028 cx = max (cx, max_cx);
7032 if (cx < 0) return FALSE;
7034 /* call header to update the column change */
7035 hdi.mask = HDI_WIDTH;
7037 TRACE("hdi.cxy=%d\n", hdi.cxy);
7038 return Header_SetItemW(infoPtr->hwndHeader, nColumn, &hdi);
7042 * Creates the checkbox imagelist. Helper for LISTVIEW_SetExtendedListViewStyle
7045 static HIMAGELIST LISTVIEW_CreateCheckBoxIL(const LISTVIEW_INFO *infoPtr)
7048 HBITMAP hbm_im, hbm_mask, hbm_orig;
7050 HBRUSH hbr_white = GetStockObject(WHITE_BRUSH);
7051 HBRUSH hbr_black = GetStockObject(BLACK_BRUSH);
7054 himl = ImageList_Create(GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON),
7055 ILC_COLOR | ILC_MASK, 2, 2);
7056 hdc_wnd = GetDC(infoPtr->hwndSelf);
7057 hdc = CreateCompatibleDC(hdc_wnd);
7058 hbm_im = CreateCompatibleBitmap(hdc_wnd, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON));
7059 hbm_mask = CreateBitmap(GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), 1, 1, NULL);
7060 ReleaseDC(infoPtr->hwndSelf, hdc_wnd);
7062 rc.left = rc.top = 0;
7063 rc.right = GetSystemMetrics(SM_CXSMICON);
7064 rc.bottom = GetSystemMetrics(SM_CYSMICON);
7066 hbm_orig = SelectObject(hdc, hbm_mask);
7067 FillRect(hdc, &rc, hbr_white);
7068 InflateRect(&rc, -3, -3);
7069 FillRect(hdc, &rc, hbr_black);
7071 SelectObject(hdc, hbm_im);
7072 DrawFrameControl(hdc, &rc, DFC_BUTTON, DFCS_BUTTONCHECK | DFCS_MONO);
7073 SelectObject(hdc, hbm_orig);
7074 ImageList_Add(himl, hbm_im, hbm_mask);
7076 SelectObject(hdc, hbm_im);
7077 DrawFrameControl(hdc, &rc, DFC_BUTTON, DFCS_BUTTONCHECK | DFCS_MONO | DFCS_CHECKED);
7078 SelectObject(hdc, hbm_orig);
7079 ImageList_Add(himl, hbm_im, hbm_mask);
7081 DeleteObject(hbm_mask);
7082 DeleteObject(hbm_im);
7090 * Sets the extended listview style.
7093 * [I] infoPtr : valid pointer to the listview structure
7095 * [I] dwStyle : style
7098 * SUCCESS : previous style
7101 static DWORD LISTVIEW_SetExtendedListViewStyle(LISTVIEW_INFO *infoPtr, DWORD dwMask, DWORD dwExStyle)
7103 DWORD dwOldExStyle = infoPtr->dwLvExStyle;
7107 infoPtr->dwLvExStyle = (dwOldExStyle & ~dwMask) | (dwExStyle & dwMask);
7109 infoPtr->dwLvExStyle = dwExStyle;
7111 if((infoPtr->dwLvExStyle ^ dwOldExStyle) & LVS_EX_CHECKBOXES)
7113 HIMAGELIST himl = 0;
7114 if(infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES)
7117 item.mask = LVIF_STATE;
7118 item.stateMask = LVIS_STATEIMAGEMASK;
7119 item.state = INDEXTOSTATEIMAGEMASK(1);
7120 LISTVIEW_SetItemState(infoPtr, -1, &item);
7122 himl = LISTVIEW_CreateCheckBoxIL(infoPtr);
7124 LISTVIEW_SetImageList(infoPtr, LVSIL_STATE, himl);
7127 if((infoPtr->dwLvExStyle ^ dwOldExStyle) & LVS_EX_HEADERDRAGDROP)
7129 DWORD dwStyle = GetWindowLongW(infoPtr->hwndHeader, GWL_STYLE);
7130 if (infoPtr->dwLvExStyle & LVS_EX_HEADERDRAGDROP)
7131 dwStyle |= HDS_DRAGDROP;
7133 dwStyle &= ~HDS_DRAGDROP;
7134 SetWindowLongW(infoPtr->hwndHeader, GWL_STYLE, dwStyle);
7137 /* GRIDLINES adds decoration at top so changes sizes */
7138 if((infoPtr->dwLvExStyle ^ dwOldExStyle) & LVS_EX_GRIDLINES)
7140 LISTVIEW_UpdateSize(infoPtr);
7144 LISTVIEW_InvalidateList(infoPtr);
7145 return dwOldExStyle;
7150 * Sets the new hot cursor used during hot tracking and hover selection.
7153 * [I] infoPtr : valid pointer to the listview structure
7154 * [I] hCursor : the new hot cursor handle
7157 * Returns the previous hot cursor
7159 static HCURSOR LISTVIEW_SetHotCursor(LISTVIEW_INFO *infoPtr, HCURSOR hCursor)
7161 HCURSOR oldCursor = infoPtr->hHotCursor;
7163 infoPtr->hHotCursor = hCursor;
7171 * Sets the hot item index.
7174 * [I] infoPtr : valid pointer to the listview structure
7175 * [I] iIndex : index
7178 * SUCCESS : previous hot item index
7179 * FAILURE : -1 (no hot item)
7181 static INT LISTVIEW_SetHotItem(LISTVIEW_INFO *infoPtr, INT iIndex)
7183 INT iOldIndex = infoPtr->nHotItem;
7185 infoPtr->nHotItem = iIndex;
7193 * Sets the amount of time the cursor must hover over an item before it is selected.
7196 * [I] infoPtr : valid pointer to the listview structure
7197 * [I] dwHoverTime : hover time, if -1 the hover time is set to the default
7200 * Returns the previous hover time
7202 static DWORD LISTVIEW_SetHoverTime(LISTVIEW_INFO *infoPtr, DWORD dwHoverTime)
7204 DWORD oldHoverTime = infoPtr->dwHoverTime;
7206 infoPtr->dwHoverTime = dwHoverTime;
7208 return oldHoverTime;
7213 * Sets spacing for icons of LVS_ICON style.
7216 * [I] infoPtr : valid pointer to the listview structure
7217 * [I] cx : horizontal spacing (-1 = system spacing, 0 = autosize)
7218 * [I] cy : vertical spacing (-1 = system spacing, 0 = autosize)
7221 * MAKELONG(oldcx, oldcy)
7223 static DWORD LISTVIEW_SetIconSpacing(LISTVIEW_INFO *infoPtr, INT cx, INT cy)
7225 DWORD oldspacing = MAKELONG(infoPtr->iconSpacing.cx, infoPtr->iconSpacing.cy);
7226 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
7228 TRACE("requested=(%d,%d)\n", cx, cy);
7230 /* this is supported only for LVS_ICON style */
7231 if (uView != LVS_ICON) return oldspacing;
7233 /* set to defaults, if instructed to */
7234 if (cx == -1) cx = GetSystemMetrics(SM_CXICONSPACING);
7235 if (cy == -1) cy = GetSystemMetrics(SM_CYICONSPACING);
7237 /* if 0 then compute width
7238 * FIXME: Should scan each item and determine max width of
7239 * icon or label, then make that the width */
7241 cx = infoPtr->iconSpacing.cx;
7243 /* if 0 then compute height */
7245 cy = infoPtr->iconSize.cy + 2 * infoPtr->ntmHeight +
7246 ICON_BOTTOM_PADDING + ICON_TOP_PADDING + LABEL_VERT_PADDING;
7249 infoPtr->iconSpacing.cx = cx;
7250 infoPtr->iconSpacing.cy = cy;
7252 TRACE("old=(%d,%d), new=(%d,%d), iconSize=(%d,%d), ntmH=%d\n",
7253 LOWORD(oldspacing), HIWORD(oldspacing), cx, cy,
7254 infoPtr->iconSize.cx, infoPtr->iconSize.cy,
7255 infoPtr->ntmHeight);
7257 /* these depend on the iconSpacing */
7258 LISTVIEW_UpdateItemSize(infoPtr);
7263 static inline void set_icon_size(SIZE *size, HIMAGELIST himl, BOOL small)
7267 if (himl && ImageList_GetIconSize(himl, &cx, &cy))
7274 size->cx = GetSystemMetrics(small ? SM_CXSMICON : SM_CXICON);
7275 size->cy = GetSystemMetrics(small ? SM_CYSMICON : SM_CYICON);
7284 * [I] infoPtr : valid pointer to the listview structure
7285 * [I] nType : image list type
7286 * [I] himl : image list handle
7289 * SUCCESS : old image list
7292 static HIMAGELIST LISTVIEW_SetImageList(LISTVIEW_INFO *infoPtr, INT nType, HIMAGELIST himl)
7294 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
7295 INT oldHeight = infoPtr->nItemHeight;
7296 HIMAGELIST himlOld = 0;
7298 TRACE("(nType=%d, himl=%p\n", nType, himl);
7303 himlOld = infoPtr->himlNormal;
7304 infoPtr->himlNormal = himl;
7305 if (uView == LVS_ICON) set_icon_size(&infoPtr->iconSize, himl, FALSE);
7306 LISTVIEW_SetIconSpacing(infoPtr, 0, 0);
7310 himlOld = infoPtr->himlSmall;
7311 infoPtr->himlSmall = himl;
7312 if (uView != LVS_ICON) set_icon_size(&infoPtr->iconSize, himl, TRUE);
7316 himlOld = infoPtr->himlState;
7317 infoPtr->himlState = himl;
7318 set_icon_size(&infoPtr->iconStateSize, himl, TRUE);
7319 ImageList_SetBkColor(infoPtr->himlState, CLR_NONE);
7323 ERR("Unknown icon type=%d\n", nType);
7327 infoPtr->nItemHeight = LISTVIEW_CalculateItemHeight(infoPtr);
7328 if (infoPtr->nItemHeight != oldHeight)
7329 LISTVIEW_UpdateScroll(infoPtr);
7336 * Preallocates memory (does *not* set the actual count of items !)
7339 * [I] infoPtr : valid pointer to the listview structure
7340 * [I] nItems : item count (projected number of items to allocate)
7341 * [I] dwFlags : update flags
7347 static BOOL LISTVIEW_SetItemCount(LISTVIEW_INFO *infoPtr, INT nItems, DWORD dwFlags)
7349 TRACE("(nItems=%d, dwFlags=%x)\n", nItems, dwFlags);
7351 if (infoPtr->dwStyle & LVS_OWNERDATA)
7353 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
7354 INT nOldCount = infoPtr->nItemCount;
7356 if (nItems < nOldCount)
7358 RANGE range = { nItems, nOldCount };
7359 ranges_del(infoPtr->selectionRanges, range);
7360 if (infoPtr->nFocusedItem >= nItems)
7362 infoPtr->nFocusedItem = -1;
7363 SetRectEmpty(&infoPtr->rcFocus);
7367 infoPtr->nItemCount = nItems;
7368 LISTVIEW_UpdateScroll(infoPtr);
7370 /* the flags are valid only in ownerdata report and list modes */
7371 if (uView == LVS_ICON || uView == LVS_SMALLICON) dwFlags = 0;
7373 if (!(dwFlags & LVSICF_NOSCROLL) && infoPtr->nFocusedItem != -1)
7374 LISTVIEW_EnsureVisible(infoPtr, infoPtr->nFocusedItem, FALSE);
7376 if (!(dwFlags & LVSICF_NOINVALIDATEALL))
7377 LISTVIEW_InvalidateList(infoPtr);
7384 LISTVIEW_GetOrigin(infoPtr, &Origin);
7385 nFrom = min(nOldCount, nItems);
7386 nTo = max(nOldCount, nItems);
7388 if (uView == LVS_REPORT)
7391 rcErase.top = nFrom * infoPtr->nItemHeight;
7392 rcErase.right = infoPtr->nItemWidth;
7393 rcErase.bottom = nTo * infoPtr->nItemHeight;
7394 OffsetRect(&rcErase, Origin.x, Origin.y);
7395 if (IntersectRect(&rcErase, &rcErase, &infoPtr->rcList))
7396 LISTVIEW_InvalidateRect(infoPtr, &rcErase);
7400 INT nPerCol = LISTVIEW_GetCountPerColumn(infoPtr);
7402 rcErase.left = (nFrom / nPerCol) * infoPtr->nItemWidth;
7403 rcErase.top = (nFrom % nPerCol) * infoPtr->nItemHeight;
7404 rcErase.right = rcErase.left + infoPtr->nItemWidth;
7405 rcErase.bottom = nPerCol * infoPtr->nItemHeight;
7406 OffsetRect(&rcErase, Origin.x, Origin.y);
7407 if (IntersectRect(&rcErase, &rcErase, &infoPtr->rcList))
7408 LISTVIEW_InvalidateRect(infoPtr, &rcErase);
7410 rcErase.left = (nFrom / nPerCol + 1) * infoPtr->nItemWidth;
7412 rcErase.right = (nTo / nPerCol + 1) * infoPtr->nItemWidth;
7413 rcErase.bottom = nPerCol * infoPtr->nItemHeight;
7414 OffsetRect(&rcErase, Origin.x, Origin.y);
7415 if (IntersectRect(&rcErase, &rcErase, &infoPtr->rcList))
7416 LISTVIEW_InvalidateRect(infoPtr, &rcErase);
7422 /* According to MSDN for non-LVS_OWNERDATA this is just
7423 * a performance issue. The control allocates its internal
7424 * data structures for the number of items specified. It
7425 * cuts down on the number of memory allocations. Therefore
7426 * we will just issue a WARN here
7428 WARN("for non-ownerdata performance option not implemented.\n");
7436 * Sets the position of an item.
7439 * [I] infoPtr : valid pointer to the listview structure
7440 * [I] nItem : item index
7441 * [I] pt : coordinate
7447 static BOOL LISTVIEW_SetItemPosition(LISTVIEW_INFO *infoPtr, INT nItem, POINT pt)
7449 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
7452 TRACE("(nItem=%d, &pt=%s\n", nItem, wine_dbgstr_point(&pt));
7454 if (nItem < 0 || nItem >= infoPtr->nItemCount ||
7455 !(uView == LVS_ICON || uView == LVS_SMALLICON)) return FALSE;
7457 LISTVIEW_GetOrigin(infoPtr, &Origin);
7459 /* This point value seems to be an undocumented feature.
7460 * The best guess is that it means either at the origin,
7461 * or at true beginning of the list. I will assume the origin. */
7462 if ((pt.x == -1) && (pt.y == -1))
7465 if (uView == LVS_ICON)
7467 pt.x -= (infoPtr->nItemWidth - infoPtr->iconSize.cx) / 2;
7468 pt.y -= ICON_TOP_PADDING;
7473 infoPtr->bAutoarrange = FALSE;
7475 return LISTVIEW_MoveIconTo(infoPtr, nItem, &pt, FALSE);
7480 * Sets the state of one or many items.
7483 * [I] infoPtr : valid pointer to the listview structure
7484 * [I] nItem : item index
7485 * [I] lpLVItem : item or subitem info
7491 static BOOL LISTVIEW_SetItemState(LISTVIEW_INFO *infoPtr, INT nItem, const LVITEMW *lpLVItem)
7493 BOOL bResult = TRUE;
7496 lvItem.iItem = nItem;
7497 lvItem.iSubItem = 0;
7498 lvItem.mask = LVIF_STATE;
7499 lvItem.state = lpLVItem->state;
7500 lvItem.stateMask = lpLVItem->stateMask;
7501 TRACE("lvItem=%s\n", debuglvitem_t(&lvItem, TRUE));
7505 /* apply to all items */
7506 for (lvItem.iItem = 0; lvItem.iItem < infoPtr->nItemCount; lvItem.iItem++)
7507 if (!LISTVIEW_SetItemT(infoPtr, &lvItem, TRUE)) bResult = FALSE;
7510 bResult = LISTVIEW_SetItemT(infoPtr, &lvItem, TRUE);
7513 * Update selection mark
7515 * Investigation on windows 2k showed that selection mark was updated
7516 * whenever a new selection was made, but if the selected item was
7517 * unselected it was not updated.
7519 * we are probably still not 100% accurate, but this at least sets the
7520 * proper selection mark when it is needed
7523 if (bResult && (lvItem.state & lvItem.stateMask & LVIS_SELECTED) &&
7524 (infoPtr->nSelectionMark == -1))
7527 for (i = 0; i < infoPtr->nItemCount; i++)
7529 if (infoPtr->uCallbackMask & LVIS_SELECTED)
7531 if (LISTVIEW_GetItemState(infoPtr, i, LVIS_SELECTED))
7533 infoPtr->nSelectionMark = i;
7537 else if (ranges_contain(infoPtr->selectionRanges, i))
7539 infoPtr->nSelectionMark = i;
7550 * Sets the text of an item or subitem.
7553 * [I] hwnd : window handle
7554 * [I] nItem : item index
7555 * [I] lpLVItem : item or subitem info
7556 * [I] isW : TRUE if input is Unicode
7562 static BOOL LISTVIEW_SetItemTextT(LISTVIEW_INFO *infoPtr, INT nItem, const LVITEMW *lpLVItem, BOOL isW)
7566 if (nItem < 0 && nItem >= infoPtr->nItemCount) return FALSE;
7568 lvItem.iItem = nItem;
7569 lvItem.iSubItem = lpLVItem->iSubItem;
7570 lvItem.mask = LVIF_TEXT;
7571 lvItem.pszText = lpLVItem->pszText;
7572 lvItem.cchTextMax = lpLVItem->cchTextMax;
7574 TRACE("(nItem=%d, lpLVItem=%s, isW=%d)\n", nItem, debuglvitem_t(&lvItem, isW), isW);
7576 return LISTVIEW_SetItemT(infoPtr, &lvItem, isW);
7581 * Set item index that marks the start of a multiple selection.
7584 * [I] infoPtr : valid pointer to the listview structure
7585 * [I] nIndex : index
7588 * Index number or -1 if there is no selection mark.
7590 static INT LISTVIEW_SetSelectionMark(LISTVIEW_INFO *infoPtr, INT nIndex)
7592 INT nOldIndex = infoPtr->nSelectionMark;
7594 TRACE("(nIndex=%d)\n", nIndex);
7596 infoPtr->nSelectionMark = nIndex;
7603 * Sets the text background color.
7606 * [I] infoPtr : valid pointer to the listview structure
7607 * [I] clrTextBk : text background color
7613 static BOOL LISTVIEW_SetTextBkColor(LISTVIEW_INFO *infoPtr, COLORREF clrTextBk)
7615 TRACE("(clrTextBk=%x)\n", clrTextBk);
7617 if (infoPtr->clrTextBk != clrTextBk)
7619 infoPtr->clrTextBk = clrTextBk;
7620 LISTVIEW_InvalidateList(infoPtr);
7628 * Sets the text foreground color.
7631 * [I] infoPtr : valid pointer to the listview structure
7632 * [I] clrText : text color
7638 static BOOL LISTVIEW_SetTextColor (LISTVIEW_INFO *infoPtr, COLORREF clrText)
7640 TRACE("(clrText=%x)\n", clrText);
7642 if (infoPtr->clrText != clrText)
7644 infoPtr->clrText = clrText;
7645 LISTVIEW_InvalidateList(infoPtr);
7653 * Determines which listview item is located at the specified position.
7656 * [I] infoPtr : valid pointer to the listview structure
7657 * [I] hwndNewToolTip : handle to new ToolTip
7662 static HWND LISTVIEW_SetToolTips( LISTVIEW_INFO *infoPtr, HWND hwndNewToolTip)
7664 HWND hwndOldToolTip = infoPtr->hwndToolTip;
7665 infoPtr->hwndToolTip = hwndNewToolTip;
7666 return hwndOldToolTip;
7671 * sets the Unicode character format flag for the control
7673 * [I] infoPtr :valid pointer to the listview structure
7674 * [I] fUnicode :true to switch to UNICODE false to switch to ANSI
7677 * Old Unicode Format
7679 static BOOL LISTVIEW_SetUnicodeFormat( LISTVIEW_INFO *infoPtr, BOOL fUnicode)
7681 BOOL rc = infoPtr->notifyFormat;
7682 infoPtr->notifyFormat = (fUnicode)?NFR_UNICODE:NFR_ANSI;
7686 /* LISTVIEW_SetWorkAreas */
7690 * Callback internally used by LISTVIEW_SortItems()
7693 * [I] first : pointer to first ITEM_INFO to compare
7694 * [I] second : pointer to second ITEM_INFO to compare
7695 * [I] lParam : HWND of control
7698 * if first comes before second : negative
7699 * if first comes after second : positive
7700 * if first and second are equivalent : zero
7702 static INT WINAPI LISTVIEW_CallBackCompare(LPVOID first, LPVOID second, LPARAM lParam)
7704 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)lParam;
7705 ITEM_INFO* lv_first = (ITEM_INFO*) DPA_GetPtr( (HDPA)first, 0 );
7706 ITEM_INFO* lv_second = (ITEM_INFO*) DPA_GetPtr( (HDPA)second, 0 );
7708 /* Forward the call to the client defined callback */
7709 return (infoPtr->pfnCompare)( lv_first->lParam , lv_second->lParam, infoPtr->lParamSort );
7714 * Sorts the listview items.
7717 * [I] infoPtr : valid pointer to the listview structure
7718 * [I] pfnCompare : application-defined value
7719 * [I] lParamSort : pointer to comparison callback
7725 static BOOL LISTVIEW_SortItems(LISTVIEW_INFO *infoPtr, PFNLVCOMPARE pfnCompare, LPARAM lParamSort)
7727 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
7730 LPVOID selectionMarkItem;
7734 TRACE("(pfnCompare=%p, lParamSort=%lx)\n", pfnCompare, lParamSort);
7736 if (infoPtr->dwStyle & LVS_OWNERDATA) return FALSE;
7738 if (!pfnCompare) return FALSE;
7739 if (!infoPtr->hdpaItems) return FALSE;
7741 /* if there are 0 or 1 items, there is no need to sort */
7742 if (infoPtr->nItemCount < 2) return TRUE;
7744 if (infoPtr->nFocusedItem >= 0)
7746 hdpaSubItems = (HDPA)DPA_GetPtr(infoPtr->hdpaItems, infoPtr->nFocusedItem);
7747 lpItem = (ITEM_INFO *)DPA_GetPtr(hdpaSubItems, 0);
7748 if (lpItem) lpItem->state |= LVIS_FOCUSED;
7750 /* FIXME: go thorugh selected items and mark them so in lpItem->state */
7751 /* clear the lpItem->state for non-selected ones */
7752 /* remove the selection ranges */
7754 infoPtr->pfnCompare = pfnCompare;
7755 infoPtr->lParamSort = lParamSort;
7756 DPA_Sort(infoPtr->hdpaItems, LISTVIEW_CallBackCompare, (LPARAM)infoPtr);
7758 /* Adjust selections and indices so that they are the way they should
7759 * be after the sort (otherwise, the list items move around, but
7760 * whatever is at the item's previous original position will be
7763 selectionMarkItem=(infoPtr->nSelectionMark>=0)?DPA_GetPtr(infoPtr->hdpaItems, infoPtr->nSelectionMark):NULL;
7764 for (i=0; i < infoPtr->nItemCount; i++)
7766 hdpaSubItems = (HDPA)DPA_GetPtr(infoPtr->hdpaItems, i);
7767 lpItem = (ITEM_INFO *)DPA_GetPtr(hdpaSubItems, 0);
7769 if (lpItem->state & LVIS_SELECTED)
7771 item.state = LVIS_SELECTED;
7772 item.stateMask = LVIS_SELECTED;
7773 LISTVIEW_SetItemState(infoPtr, i, &item);
7775 if (lpItem->state & LVIS_FOCUSED)
7777 infoPtr->nFocusedItem = i;
7778 lpItem->state &= ~LVIS_FOCUSED;
7781 if (selectionMarkItem != NULL)
7782 infoPtr->nSelectionMark = DPA_GetPtrIndex(infoPtr->hdpaItems, selectionMarkItem);
7783 /* I believe nHotItem should be left alone, see LISTVIEW_ShiftIndices */
7785 /* refresh the display */
7786 if (uView != LVS_ICON && uView != LVS_SMALLICON)
7787 LISTVIEW_InvalidateList(infoPtr);
7794 * Update theme handle after a theme change.
7797 * [I] infoPtr : valid pointer to the listview structure
7801 * FAILURE : something else
7803 static LRESULT LISTVIEW_ThemeChanged(const LISTVIEW_INFO *infoPtr)
7805 HTHEME theme = GetWindowTheme(infoPtr->hwndSelf);
7806 CloseThemeData(theme);
7807 OpenThemeData(infoPtr->hwndSelf, themeClass);
7813 * Updates an items or rearranges the listview control.
7816 * [I] infoPtr : valid pointer to the listview structure
7817 * [I] nItem : item index
7823 static BOOL LISTVIEW_Update(LISTVIEW_INFO *infoPtr, INT nItem)
7825 TRACE("(nItem=%d)\n", nItem);
7827 if (nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
7829 /* rearrange with default alignment style */
7830 if (is_autoarrange(infoPtr))
7831 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
7833 LISTVIEW_InvalidateItem(infoPtr, nItem);
7840 * Draw the track line at the place defined in the infoPtr structure.
7841 * The line is drawn with a XOR pen so drawing the line for the second time
7842 * in the same place erases the line.
7845 * [I] infoPtr : valid pointer to the listview structure
7851 static BOOL LISTVIEW_DrawTrackLine(const LISTVIEW_INFO *infoPtr)
7857 if (infoPtr->xTrackLine == -1)
7860 if (!(hdc = GetDC(infoPtr->hwndSelf)))
7862 hOldPen = SelectObject(hdc, GetStockObject(BLACK_PEN));
7863 oldROP = SetROP2(hdc, R2_XORPEN);
7864 MoveToEx(hdc, infoPtr->xTrackLine, infoPtr->rcList.top, NULL);
7865 LineTo(hdc, infoPtr->xTrackLine, infoPtr->rcList.bottom);
7866 SetROP2(hdc, oldROP);
7867 SelectObject(hdc, hOldPen);
7868 ReleaseDC(infoPtr->hwndSelf, hdc);
7874 * Called when an edit control should be displayed. This function is called after
7875 * we are sure that there was a single click - not a double click (this is a TIMERPROC).
7878 * [I] hwnd : Handle to the listview
7879 * [I] uMsg : WM_TIMER (ignored)
7880 * [I] idEvent : The timer ID interpreted as a pointer to a DELAYED_EDIT_ITEM struct
7881 * [I] dwTimer : The elapsed time (ignored)
7886 static VOID CALLBACK LISTVIEW_DelayedEditItem(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
7888 DELAYED_ITEM_EDIT *editItem = (DELAYED_ITEM_EDIT *)idEvent;
7889 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongPtrW(hwnd, 0);
7891 KillTimer(hwnd, idEvent);
7892 editItem->fEnabled = FALSE;
7893 /* check if the item is still selected */
7894 if (infoPtr->bFocus && LISTVIEW_GetItemState(infoPtr, editItem->iItem, LVIS_SELECTED))
7895 LISTVIEW_EditLabelT(infoPtr, editItem->iItem, TRUE);
7900 * Creates the listview control - the WM_NCCREATE phase.
7903 * [I] hwnd : window handle
7904 * [I] lpcs : the create parameters
7910 static LRESULT LISTVIEW_NCCreate(HWND hwnd, const CREATESTRUCTW *lpcs)
7912 LISTVIEW_INFO *infoPtr;
7915 TRACE("(lpcs=%p)\n", lpcs);
7917 /* initialize info pointer */
7918 infoPtr = Alloc(sizeof(LISTVIEW_INFO));
7919 if (!infoPtr) return FALSE;
7921 SetWindowLongPtrW(hwnd, 0, (DWORD_PTR)infoPtr);
7923 infoPtr->hwndSelf = hwnd;
7924 infoPtr->dwStyle = lpcs->style; /* Note: may be changed in WM_CREATE */
7925 /* determine the type of structures to use */
7926 infoPtr->hwndNotify = lpcs->hwndParent;
7927 /* infoPtr->notifyFormat will be filled in WM_CREATE */
7929 /* initialize color information */
7930 infoPtr->clrBk = CLR_NONE;
7931 infoPtr->clrText = CLR_DEFAULT;
7932 infoPtr->clrTextBk = CLR_DEFAULT;
7933 LISTVIEW_SetBkColor(infoPtr, comctl32_color.clrWindow);
7935 /* set default values */
7936 infoPtr->nFocusedItem = -1;
7937 infoPtr->nSelectionMark = -1;
7938 infoPtr->nHotItem = -1;
7939 infoPtr->bRedraw = TRUE;
7940 infoPtr->bNoItemMetrics = TRUE;
7941 infoPtr->bDoChangeNotify = TRUE;
7942 infoPtr->iconSpacing.cx = GetSystemMetrics(SM_CXICONSPACING);
7943 infoPtr->iconSpacing.cy = GetSystemMetrics(SM_CYICONSPACING);
7944 infoPtr->nEditLabelItem = -1;
7945 infoPtr->dwHoverTime = -1; /* default system hover time */
7946 infoPtr->nMeasureItemHeight = 0;
7947 infoPtr->xTrackLine = -1; /* no track line */
7948 infoPtr->itemEdit.fEnabled = FALSE;
7950 /* get default font (icon title) */
7951 SystemParametersInfoW(SPI_GETICONTITLELOGFONT, 0, &logFont, 0);
7952 infoPtr->hDefaultFont = CreateFontIndirectW(&logFont);
7953 infoPtr->hFont = infoPtr->hDefaultFont;
7954 LISTVIEW_SaveTextMetrics(infoPtr);
7956 /* allocate memory for the data structure */
7957 if (!(infoPtr->selectionRanges = ranges_create(10))) goto fail;
7958 if (!(infoPtr->hdpaItems = DPA_Create(10))) goto fail;
7959 if (!(infoPtr->hdpaPosX = DPA_Create(10))) goto fail;
7960 if (!(infoPtr->hdpaPosY = DPA_Create(10))) goto fail;
7961 if (!(infoPtr->hdpaColumns = DPA_Create(10))) goto fail;
7965 DestroyWindow(infoPtr->hwndHeader);
7966 ranges_destroy(infoPtr->selectionRanges);
7967 DPA_Destroy(infoPtr->hdpaItems);
7968 DPA_Destroy(infoPtr->hdpaPosX);
7969 DPA_Destroy(infoPtr->hdpaPosY);
7970 DPA_Destroy(infoPtr->hdpaColumns);
7977 * Creates the listview control - the WM_CREATE phase. Most of the data is
7978 * already set up in LISTVIEW_NCCreate
7981 * [I] hwnd : window handle
7982 * [I] lpcs : the create parameters
7988 static LRESULT LISTVIEW_Create(HWND hwnd, const CREATESTRUCTW *lpcs)
7990 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongPtrW(hwnd, 0);
7991 UINT uView = lpcs->style & LVS_TYPEMASK;
7993 TRACE("(lpcs=%p)\n", lpcs);
7995 infoPtr->dwStyle = lpcs->style;
7996 infoPtr->notifyFormat = SendMessageW(infoPtr->hwndNotify, WM_NOTIFYFORMAT,
7997 (WPARAM)infoPtr->hwndSelf, (LPARAM)NF_QUERY);
8000 infoPtr->hwndHeader = CreateWindowW(WC_HEADERW, NULL,
8001 WS_CHILD | HDS_HORZ | HDS_FULLDRAG | (DWORD)((LVS_NOSORTHEADER & lpcs->style)?0:HDS_BUTTONS),
8002 0, 0, 0, 0, hwnd, NULL,
8003 lpcs->hInstance, NULL);
8004 if (!infoPtr->hwndHeader) return -1;
8006 /* set header unicode format */
8007 SendMessageW(infoPtr->hwndHeader, HDM_SETUNICODEFORMAT, (WPARAM)TRUE, (LPARAM)NULL);
8009 /* set header font */
8010 SendMessageW(infoPtr->hwndHeader, WM_SETFONT, (WPARAM)infoPtr->hFont, (LPARAM)TRUE);
8012 /* init item size to avoid division by 0 */
8013 LISTVIEW_UpdateItemSize (infoPtr);
8015 if (uView == LVS_REPORT)
8017 if (!(LVS_NOCOLUMNHEADER & lpcs->style))
8019 ShowWindow(infoPtr->hwndHeader, SW_SHOWNORMAL);
8023 /* set HDS_HIDDEN flag to hide the header bar */
8024 SetWindowLongW(infoPtr->hwndHeader, GWL_STYLE,
8025 GetWindowLongW(infoPtr->hwndHeader, GWL_STYLE) | HDS_HIDDEN);
8029 OpenThemeData(hwnd, themeClass);
8031 /* initialize the icon sizes */
8032 set_icon_size(&infoPtr->iconSize, infoPtr->himlNormal, uView != LVS_ICON);
8033 set_icon_size(&infoPtr->iconStateSize, infoPtr->himlState, TRUE);
8039 * Destroys the listview control.
8042 * [I] infoPtr : valid pointer to the listview structure
8048 static LRESULT LISTVIEW_Destroy(const LISTVIEW_INFO *infoPtr)
8050 HTHEME theme = GetWindowTheme(infoPtr->hwndSelf);
8051 CloseThemeData(theme);
8057 * Enables the listview control.
8060 * [I] infoPtr : valid pointer to the listview structure
8061 * [I] bEnable : specifies whether to enable or disable the window
8067 static BOOL LISTVIEW_Enable(const LISTVIEW_INFO *infoPtr, BOOL bEnable)
8069 if (infoPtr->dwStyle & LVS_OWNERDRAWFIXED)
8070 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
8076 * Erases the background of the listview control.
8079 * [I] infoPtr : valid pointer to the listview structure
8080 * [I] hdc : device context handle
8086 static inline BOOL LISTVIEW_EraseBkgnd(const LISTVIEW_INFO *infoPtr, HDC hdc)
8090 TRACE("(hdc=%p)\n", hdc);
8092 if (!GetClipBox(hdc, &rc)) return FALSE;
8094 /* for double buffered controls we need to do this during refresh */
8095 if (infoPtr->dwLvExStyle & LVS_EX_DOUBLEBUFFER) return FALSE;
8097 return LISTVIEW_FillBkgnd(infoPtr, hdc, &rc);
8103 * Helper function for LISTVIEW_[HV]Scroll *only*.
8104 * Performs vertical/horizontal scrolling by a give amount.
8107 * [I] infoPtr : valid pointer to the listview structure
8108 * [I] dx : amount of horizontal scroll
8109 * [I] dy : amount of vertical scroll
8111 static void scroll_list(LISTVIEW_INFO *infoPtr, INT dx, INT dy)
8113 /* now we can scroll the list */
8114 ScrollWindowEx(infoPtr->hwndSelf, dx, dy, &infoPtr->rcList,
8115 &infoPtr->rcList, 0, 0, SW_ERASE | SW_INVALIDATE);
8116 /* if we have focus, adjust rect */
8117 OffsetRect(&infoPtr->rcFocus, dx, dy);
8118 UpdateWindow(infoPtr->hwndSelf);
8123 * Performs vertical scrolling.
8126 * [I] infoPtr : valid pointer to the listview structure
8127 * [I] nScrollCode : scroll code
8128 * [I] nScrollDiff : units to scroll in SB_INTERNAL mode, 0 otherwise
8129 * [I] hScrollWnd : scrollbar control window handle
8135 * SB_LINEUP/SB_LINEDOWN:
8136 * for LVS_ICON, LVS_SMALLICON is 37 by experiment
8137 * for LVS_REPORT is 1 line
8138 * for LVS_LIST cannot occur
8141 static LRESULT LISTVIEW_VScroll(LISTVIEW_INFO *infoPtr, INT nScrollCode,
8142 INT nScrollDiff, HWND hScrollWnd)
8144 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
8145 INT nOldScrollPos, nNewScrollPos;
8146 SCROLLINFO scrollInfo;
8149 TRACE("(nScrollCode=%d(%s), nScrollDiff=%d)\n", nScrollCode,
8150 debugscrollcode(nScrollCode), nScrollDiff);
8152 if (infoPtr->hwndEdit) SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
8154 scrollInfo.cbSize = sizeof(SCROLLINFO);
8155 scrollInfo.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_TRACKPOS;
8157 is_an_icon = ((uView == LVS_ICON) || (uView == LVS_SMALLICON));
8159 if (!GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo)) return 1;
8161 nOldScrollPos = scrollInfo.nPos;
8162 switch (nScrollCode)
8168 nScrollDiff = (is_an_icon) ? -LISTVIEW_SCROLL_ICON_LINE_SIZE : -1;
8172 nScrollDiff = (is_an_icon) ? LISTVIEW_SCROLL_ICON_LINE_SIZE : 1;
8176 nScrollDiff = -scrollInfo.nPage;
8180 nScrollDiff = scrollInfo.nPage;
8183 case SB_THUMBPOSITION:
8185 nScrollDiff = scrollInfo.nTrackPos - scrollInfo.nPos;
8192 /* quit right away if pos isn't changing */
8193 if (nScrollDiff == 0) return 0;
8195 /* calculate new position, and handle overflows */
8196 nNewScrollPos = scrollInfo.nPos + nScrollDiff;
8197 if (nScrollDiff > 0) {
8198 if (nNewScrollPos < nOldScrollPos ||
8199 nNewScrollPos > scrollInfo.nMax)
8200 nNewScrollPos = scrollInfo.nMax;
8202 if (nNewScrollPos > nOldScrollPos ||
8203 nNewScrollPos < scrollInfo.nMin)
8204 nNewScrollPos = scrollInfo.nMin;
8207 /* set the new position, and reread in case it changed */
8208 scrollInfo.fMask = SIF_POS;
8209 scrollInfo.nPos = nNewScrollPos;
8210 nNewScrollPos = SetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo, TRUE);
8212 /* carry on only if it really changed */
8213 if (nNewScrollPos == nOldScrollPos) return 0;
8215 /* now adjust to client coordinates */
8216 nScrollDiff = nOldScrollPos - nNewScrollPos;
8217 if (uView == LVS_REPORT) nScrollDiff *= infoPtr->nItemHeight;
8219 /* and scroll the window */
8220 scroll_list(infoPtr, 0, nScrollDiff);
8227 * Performs horizontal scrolling.
8230 * [I] infoPtr : valid pointer to the listview structure
8231 * [I] nScrollCode : scroll code
8232 * [I] nScrollDiff : units to scroll in SB_INTERNAL mode, 0 otherwise
8233 * [I] hScrollWnd : scrollbar control window handle
8239 * SB_LINELEFT/SB_LINERIGHT:
8240 * for LVS_ICON, LVS_SMALLICON 1 pixel
8241 * for LVS_REPORT is 1 pixel
8242 * for LVS_LIST is 1 column --> which is a 1 because the
8243 * scroll is based on columns not pixels
8246 static LRESULT LISTVIEW_HScroll(LISTVIEW_INFO *infoPtr, INT nScrollCode,
8247 INT nScrollDiff, HWND hScrollWnd)
8249 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
8250 INT nOldScrollPos, nNewScrollPos;
8251 SCROLLINFO scrollInfo;
8253 TRACE("(nScrollCode=%d(%s), nScrollDiff=%d)\n", nScrollCode,
8254 debugscrollcode(nScrollCode), nScrollDiff);
8256 if (infoPtr->hwndEdit) SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
8258 scrollInfo.cbSize = sizeof(SCROLLINFO);
8259 scrollInfo.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_TRACKPOS;
8261 if (!GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo)) return 1;
8263 nOldScrollPos = scrollInfo.nPos;
8265 switch (nScrollCode)
8279 nScrollDiff = -scrollInfo.nPage;
8283 nScrollDiff = scrollInfo.nPage;
8286 case SB_THUMBPOSITION:
8288 nScrollDiff = scrollInfo.nTrackPos - scrollInfo.nPos;
8295 /* quit right away if pos isn't changing */
8296 if (nScrollDiff == 0) return 0;
8298 /* calculate new position, and handle overflows */
8299 nNewScrollPos = scrollInfo.nPos + nScrollDiff;
8300 if (nScrollDiff > 0) {
8301 if (nNewScrollPos < nOldScrollPos ||
8302 nNewScrollPos > scrollInfo.nMax)
8303 nNewScrollPos = scrollInfo.nMax;
8305 if (nNewScrollPos > nOldScrollPos ||
8306 nNewScrollPos < scrollInfo.nMin)
8307 nNewScrollPos = scrollInfo.nMin;
8310 /* set the new position, and reread in case it changed */
8311 scrollInfo.fMask = SIF_POS;
8312 scrollInfo.nPos = nNewScrollPos;
8313 nNewScrollPos = SetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo, TRUE);
8315 /* carry on only if it really changed */
8316 if (nNewScrollPos == nOldScrollPos) return 0;
8318 if(uView == LVS_REPORT)
8319 LISTVIEW_UpdateHeaderSize(infoPtr, nNewScrollPos);
8321 /* now adjust to client coordinates */
8322 nScrollDiff = nOldScrollPos - nNewScrollPos;
8323 if (uView == LVS_LIST) nScrollDiff *= infoPtr->nItemWidth;
8325 /* and scroll the window */
8326 scroll_list(infoPtr, nScrollDiff, 0);
8331 static LRESULT LISTVIEW_MouseWheel(LISTVIEW_INFO *infoPtr, INT wheelDelta)
8333 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
8334 INT gcWheelDelta = 0;
8335 INT pulScrollLines = 3;
8336 SCROLLINFO scrollInfo;
8338 TRACE("(wheelDelta=%d)\n", wheelDelta);
8340 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
8341 gcWheelDelta -= wheelDelta;
8343 scrollInfo.cbSize = sizeof(SCROLLINFO);
8344 scrollInfo.fMask = SIF_POS;
8351 * listview should be scrolled by a multiple of 37 dependently on its dimension or its visible item number
8352 * should be fixed in the future.
8354 LISTVIEW_VScroll(infoPtr, SB_INTERNAL, (gcWheelDelta < 0) ?
8355 -LISTVIEW_SCROLL_ICON_LINE_SIZE : LISTVIEW_SCROLL_ICON_LINE_SIZE, 0);
8359 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
8361 int cLineScroll = min(LISTVIEW_GetCountPerColumn(infoPtr), pulScrollLines);
8362 cLineScroll *= (gcWheelDelta / WHEEL_DELTA);
8363 LISTVIEW_VScroll(infoPtr, SB_INTERNAL, cLineScroll, 0);
8368 LISTVIEW_HScroll(infoPtr, (gcWheelDelta < 0) ? SB_LINELEFT : SB_LINERIGHT, 0, 0);
8379 * [I] infoPtr : valid pointer to the listview structure
8380 * [I] nVirtualKey : virtual key
8381 * [I] lKeyData : key data
8386 static LRESULT LISTVIEW_KeyDown(LISTVIEW_INFO *infoPtr, INT nVirtualKey, LONG lKeyData)
8388 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
8389 HWND hwndSelf = infoPtr->hwndSelf;
8391 NMLVKEYDOWN nmKeyDown;
8393 TRACE("(nVirtualKey=%d, lKeyData=%d)\n", nVirtualKey, lKeyData);
8395 /* send LVN_KEYDOWN notification */
8396 nmKeyDown.wVKey = nVirtualKey;
8397 nmKeyDown.flags = 0;
8398 notify_hdr(infoPtr, LVN_KEYDOWN, &nmKeyDown.hdr);
8399 if (!IsWindow(hwndSelf))
8402 switch (nVirtualKey)
8405 nItem = infoPtr->nFocusedItem;
8406 if (infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES)
8407 toggle_checkbox_state(infoPtr, infoPtr->nFocusedItem);
8411 if ((infoPtr->nItemCount > 0) && (infoPtr->nFocusedItem != -1))
8413 if (!notify(infoPtr, NM_RETURN)) return 0;
8414 if (!notify(infoPtr, LVN_ITEMACTIVATE)) return 0;
8419 if (infoPtr->nItemCount > 0)
8424 if (infoPtr->nItemCount > 0)
8425 nItem = infoPtr->nItemCount - 1;
8429 nItem = ListView_GetNextItem(infoPtr->hwndSelf, infoPtr->nFocusedItem, LVNI_TOLEFT);
8433 nItem = ListView_GetNextItem(infoPtr->hwndSelf, infoPtr->nFocusedItem, LVNI_ABOVE);
8437 nItem = ListView_GetNextItem(infoPtr->hwndSelf, infoPtr->nFocusedItem, LVNI_TORIGHT);
8441 nItem = ListView_GetNextItem(infoPtr->hwndSelf, infoPtr->nFocusedItem, LVNI_BELOW);
8445 if (uView == LVS_REPORT)
8447 INT topidx = LISTVIEW_GetTopIndex(infoPtr);
8448 if (infoPtr->nFocusedItem == topidx)
8449 nItem = topidx - LISTVIEW_GetCountPerColumn(infoPtr) + 1;
8454 nItem = infoPtr->nFocusedItem - LISTVIEW_GetCountPerColumn(infoPtr)
8455 * LISTVIEW_GetCountPerRow(infoPtr);
8456 if(nItem < 0) nItem = 0;
8460 if (uView == LVS_REPORT)
8462 INT topidx = LISTVIEW_GetTopIndex(infoPtr);
8463 INT cnt = LISTVIEW_GetCountPerColumn(infoPtr);
8464 if (infoPtr->nFocusedItem == topidx + cnt - 1)
8465 nItem = infoPtr->nFocusedItem + cnt - 1;
8467 nItem = topidx + cnt - 1;
8470 nItem = infoPtr->nFocusedItem + LISTVIEW_GetCountPerColumn(infoPtr)
8471 * LISTVIEW_GetCountPerRow(infoPtr);
8472 if(nItem >= infoPtr->nItemCount) nItem = infoPtr->nItemCount - 1;
8476 if ((nItem != -1) && (nItem != infoPtr->nFocusedItem || nVirtualKey == VK_SPACE))
8477 LISTVIEW_KeySelection(infoPtr, nItem);
8487 * [I] infoPtr : valid pointer to the listview structure
8492 static LRESULT LISTVIEW_KillFocus(LISTVIEW_INFO *infoPtr)
8496 /* if we did not have the focus, there's nothing to do */
8497 if (!infoPtr->bFocus) return 0;
8499 /* send NM_KILLFOCUS notification */
8500 if (!notify(infoPtr, NM_KILLFOCUS)) return 0;
8502 /* if we have a focus rectagle, get rid of it */
8503 LISTVIEW_ShowFocusRect(infoPtr, FALSE);
8505 /* set window focus flag */
8506 infoPtr->bFocus = FALSE;
8508 /* invalidate the selected items before resetting focus flag */
8509 LISTVIEW_InvalidateSelectedItems(infoPtr);
8516 * Processes double click messages (left mouse button).
8519 * [I] infoPtr : valid pointer to the listview structure
8520 * [I] wKey : key flag
8521 * [I] x,y : mouse coordinate
8526 static LRESULT LISTVIEW_LButtonDblClk(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
8528 LVHITTESTINFO htInfo;
8530 TRACE("(key=%hu, X=%hu, Y=%hu)\n", wKey, x, y);
8532 /* Cancel the item edition if any */
8533 if (infoPtr->itemEdit.fEnabled)
8535 KillTimer(infoPtr->hwndSelf, (UINT_PTR)&infoPtr->itemEdit);
8536 infoPtr->itemEdit.fEnabled = FALSE;
8539 /* send NM_RELEASEDCAPTURE notification */
8540 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
8545 /* send NM_DBLCLK notification */
8546 LISTVIEW_HitTest(infoPtr, &htInfo, TRUE, FALSE);
8547 if (!notify_click(infoPtr, NM_DBLCLK, &htInfo)) return 0;
8549 /* To send the LVN_ITEMACTIVATE, it must be on an Item */
8550 if(htInfo.iItem != -1) notify_itemactivate(infoPtr,&htInfo);
8557 * Processes mouse down messages (left mouse button).
8560 * infoPtr [I ] valid pointer to the listview structure
8561 * wKey [I ] key flag
8562 * x,y [I ] mouse coordinate
8567 static LRESULT LISTVIEW_LButtonDown(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
8569 LVHITTESTINFO lvHitTestInfo;
8570 static BOOL bGroupSelect = TRUE;
8571 BOOL bReceivedFocus = FALSE;
8572 POINT pt = { x, y };
8575 TRACE("(key=%hu, X=%hu, Y=%hu)\n", wKey, x, y);
8577 /* send NM_RELEASEDCAPTURE notification */
8578 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
8580 if (!infoPtr->bFocus)
8582 bReceivedFocus = TRUE;
8583 SetFocus(infoPtr->hwndSelf);
8586 /* set left button down flag and record the click position */
8587 infoPtr->bLButtonDown = TRUE;
8588 infoPtr->ptClickPos = pt;
8590 lvHitTestInfo.pt.x = x;
8591 lvHitTestInfo.pt.y = y;
8593 nItem = LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, TRUE);
8594 TRACE("at %s, nItem=%d\n", wine_dbgstr_point(&pt), nItem);
8595 infoPtr->nEditLabelItem = -1;
8596 if ((nItem >= 0) && (nItem < infoPtr->nItemCount))
8598 if ((infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES) && (lvHitTestInfo.flags & LVHT_ONITEMSTATEICON))
8600 toggle_checkbox_state(infoPtr, nItem);
8604 if (infoPtr->dwStyle & LVS_SINGLESEL)
8606 if (LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED))
8607 infoPtr->nEditLabelItem = nItem;
8609 LISTVIEW_SetSelection(infoPtr, nItem);
8613 if ((wKey & MK_CONTROL) && (wKey & MK_SHIFT))
8617 if (!LISTVIEW_AddGroupSelection(infoPtr, nItem)) return 0;
8618 LISTVIEW_SetItemFocus(infoPtr, nItem);
8619 infoPtr->nSelectionMark = nItem;
8625 item.state = LVIS_SELECTED | LVIS_FOCUSED;
8626 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
8628 LISTVIEW_SetItemState(infoPtr,nItem,&item);
8629 infoPtr->nSelectionMark = nItem;
8632 else if (wKey & MK_CONTROL)
8636 bGroupSelect = (LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED) == 0);
8638 item.state = (bGroupSelect ? LVIS_SELECTED : 0) | LVIS_FOCUSED;
8639 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
8640 LISTVIEW_SetItemState(infoPtr, nItem, &item);
8641 infoPtr->nSelectionMark = nItem;
8643 else if (wKey & MK_SHIFT)
8645 LISTVIEW_SetGroupSelection(infoPtr, nItem);
8649 if (LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED))
8650 infoPtr->nEditLabelItem = nItem;
8652 /* set selection (clears other pre-existing selections) */
8653 LISTVIEW_SetSelection(infoPtr, nItem);
8659 /* remove all selections */
8660 LISTVIEW_DeselectAll(infoPtr);
8665 infoPtr->nEditLabelItem = -1;
8672 * Processes mouse up messages (left mouse button).
8675 * infoPtr [I ] valid pointer to the listview structure
8676 * wKey [I ] key flag
8677 * x,y [I ] mouse coordinate
8682 static LRESULT LISTVIEW_LButtonUp(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
8684 LVHITTESTINFO lvHitTestInfo;
8686 TRACE("(key=%hu, X=%hu, Y=%hu)\n", wKey, x, y);
8688 if (!infoPtr->bLButtonDown) return 0;
8690 lvHitTestInfo.pt.x = x;
8691 lvHitTestInfo.pt.y = y;
8693 /* send NM_CLICK notification */
8694 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
8695 if (!notify_click(infoPtr, NM_CLICK, &lvHitTestInfo)) return 0;
8697 /* set left button flag */
8698 infoPtr->bLButtonDown = FALSE;
8700 /* if we clicked on a selected item, edit the label */
8701 if(lvHitTestInfo.iItem == infoPtr->nEditLabelItem && (lvHitTestInfo.flags & LVHT_ONITEMLABEL))
8703 /* we want to make sure the user doesn't want to do a double click. So we will
8704 * delay the edit. WM_LBUTTONDBLCLICK will cancel the timer
8706 infoPtr->itemEdit.fEnabled = TRUE;
8707 infoPtr->itemEdit.iItem = lvHitTestInfo.iItem;
8708 SetTimer(infoPtr->hwndSelf,
8709 (UINT_PTR)&infoPtr->itemEdit,
8710 GetDoubleClickTime(),
8711 LISTVIEW_DelayedEditItem);
8719 * Destroys the listview control (called after WM_DESTROY).
8722 * [I] infoPtr : valid pointer to the listview structure
8727 static LRESULT LISTVIEW_NCDestroy(LISTVIEW_INFO *infoPtr)
8731 /* delete all items */
8732 LISTVIEW_DeleteAllItems(infoPtr);
8734 /* destroy data structure */
8735 DPA_Destroy(infoPtr->hdpaItems);
8736 DPA_Destroy(infoPtr->hdpaPosX);
8737 DPA_Destroy(infoPtr->hdpaPosY);
8738 DPA_Destroy(infoPtr->hdpaColumns);
8739 ranges_destroy(infoPtr->selectionRanges);
8741 /* destroy image lists */
8742 if (!(infoPtr->dwStyle & LVS_SHAREIMAGELISTS))
8744 if (infoPtr->himlNormal)
8745 ImageList_Destroy(infoPtr->himlNormal);
8746 if (infoPtr->himlSmall)
8747 ImageList_Destroy(infoPtr->himlSmall);
8748 if (infoPtr->himlState)
8749 ImageList_Destroy(infoPtr->himlState);
8752 /* destroy font, bkgnd brush */
8754 if (infoPtr->hDefaultFont) DeleteObject(infoPtr->hDefaultFont);
8755 if (infoPtr->clrBk != CLR_NONE) DeleteObject(infoPtr->hBkBrush);
8757 SetWindowLongPtrW(infoPtr->hwndSelf, 0, 0);
8759 /* free listview info pointer*/
8767 * Handles notifications from header.
8770 * [I] infoPtr : valid pointer to the listview structure
8771 * [I] nCtrlId : control identifier
8772 * [I] lpnmh : notification information
8777 static LRESULT LISTVIEW_HeaderNotification(LISTVIEW_INFO *infoPtr, const NMHEADERW *lpnmh)
8779 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
8780 HWND hwndSelf = infoPtr->hwndSelf;
8782 TRACE("(lpnmh=%p)\n", lpnmh);
8784 if (!lpnmh || lpnmh->iItem < 0 || lpnmh->iItem >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return 0;
8786 switch (lpnmh->hdr.code)
8791 COLUMN_INFO *lpColumnInfo;
8795 if (!lpnmh->pitem || !(lpnmh->pitem->mask & HDI_WIDTH))
8798 /* remove the old line (if any) */
8799 LISTVIEW_DrawTrackLine(infoPtr);
8801 /* compute & draw the new line */
8802 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, lpnmh->iItem);
8803 x = lpColumnInfo->rcHeader.left + lpnmh->pitem->cxy;
8804 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
8805 infoPtr->xTrackLine = x + ptOrigin.x;
8806 LISTVIEW_DrawTrackLine(infoPtr);
8812 /* remove the track line (if any) */
8813 LISTVIEW_DrawTrackLine(infoPtr);
8814 infoPtr->xTrackLine = -1;
8818 FIXME("Changing column order not implemented\n");
8821 case HDN_ITEMCHANGINGW:
8822 case HDN_ITEMCHANGINGA:
8823 return notify_forward_header(infoPtr, lpnmh);
8825 case HDN_ITEMCHANGEDW:
8826 case HDN_ITEMCHANGEDA:
8828 COLUMN_INFO *lpColumnInfo;
8831 notify_forward_header(infoPtr, lpnmh);
8832 if (!IsWindow(hwndSelf))
8835 if (!lpnmh->pitem || !(lpnmh->pitem->mask & HDI_WIDTH))
8839 hdi.mask = HDI_WIDTH;
8840 if (!Header_GetItemW(infoPtr->hwndHeader, lpnmh->iItem, &hdi)) return 0;
8844 cxy = lpnmh->pitem->cxy;
8846 /* determine how much we change since the last know position */
8847 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, lpnmh->iItem);
8848 dx = cxy - (lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left);
8851 lpColumnInfo->rcHeader.right += dx;
8852 if (lpnmh->iItem + 1 < DPA_GetPtrCount(infoPtr->hdpaColumns))
8853 LISTVIEW_ScrollColumns(infoPtr, lpnmh->iItem + 1, dx);
8856 /* only needs to update the scrolls */
8857 infoPtr->nItemWidth += dx;
8858 LISTVIEW_UpdateScroll(infoPtr);
8860 LISTVIEW_UpdateItemSize(infoPtr);
8861 if (uView == LVS_REPORT && is_redrawing(infoPtr))
8864 RECT rcCol = lpColumnInfo->rcHeader;
8866 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
8867 OffsetRect(&rcCol, ptOrigin.x, 0);
8869 rcCol.top = infoPtr->rcList.top;
8870 rcCol.bottom = infoPtr->rcList.bottom;
8872 /* resizing left-aligned columns leaves most of the left side untouched */
8873 if ((lpColumnInfo->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_LEFT)
8875 INT nMaxDirty = infoPtr->nEllipsisWidth + infoPtr->ntmMaxCharWidth;
8878 rcCol.left = max (rcCol.left, rcCol.right - nMaxDirty);
8881 /* when shrinking the last column clear the now unused field */
8882 if (lpnmh->iItem == DPA_GetPtrCount(infoPtr->hdpaColumns) - 1 && dx < 0)
8885 LISTVIEW_InvalidateRect(infoPtr, &rcCol);
8891 case HDN_ITEMCLICKW:
8892 case HDN_ITEMCLICKA:
8894 /* Handle sorting by Header Column */
8897 ZeroMemory(&nmlv, sizeof(NMLISTVIEW));
8899 nmlv.iSubItem = lpnmh->iItem;
8900 notify_listview(infoPtr, LVN_COLUMNCLICK, &nmlv);
8904 case HDN_DIVIDERDBLCLICKW:
8905 case HDN_DIVIDERDBLCLICKA:
8906 LISTVIEW_SetColumnWidth(infoPtr, lpnmh->iItem, LVSCW_AUTOSIZE);
8915 * Paint non-client area of control.
8918 * [I] infoPtr : valid pointer to the listview structureof the sender
8919 * [I] region : update region
8922 * TRUE - frame was painted
8923 * FALSE - call default window proc
8925 static BOOL LISTVIEW_NCPaint(const LISTVIEW_INFO *infoPtr, HRGN region)
8927 HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
8931 int cxEdge = GetSystemMetrics (SM_CXEDGE),
8932 cyEdge = GetSystemMetrics (SM_CYEDGE);
8934 if (!theme) return FALSE;
8936 GetWindowRect(infoPtr->hwndSelf, &r);
8938 cliprgn = CreateRectRgn (r.left + cxEdge, r.top + cyEdge,
8939 r.right - cxEdge, r.bottom - cyEdge);
8940 if (region != (HRGN)1)
8941 CombineRgn (cliprgn, cliprgn, region, RGN_AND);
8942 OffsetRect(&r, -r.left, -r.top);
8944 dc = GetDCEx(infoPtr->hwndSelf, region, DCX_WINDOW|DCX_INTERSECTRGN);
8945 OffsetRect(&r, -r.left, -r.top);
8947 if (IsThemeBackgroundPartiallyTransparent (theme, 0, 0))
8948 DrawThemeParentBackground(infoPtr->hwndSelf, dc, &r);
8949 DrawThemeBackground (theme, dc, 0, 0, &r, 0);
8950 ReleaseDC(infoPtr->hwndSelf, dc);
8952 /* Call default proc to get the scrollbars etc. painted */
8953 DefWindowProcW (infoPtr->hwndSelf, WM_NCPAINT, (WPARAM)cliprgn, 0);
8960 * Determines the type of structure to use.
8963 * [I] infoPtr : valid pointer to the listview structureof the sender
8964 * [I] hwndFrom : listview window handle
8965 * [I] nCommand : command specifying the nature of the WM_NOTIFYFORMAT
8970 static LRESULT LISTVIEW_NotifyFormat(LISTVIEW_INFO *infoPtr, HWND hwndFrom, INT nCommand)
8972 TRACE("(hwndFrom=%p, nCommand=%d)\n", hwndFrom, nCommand);
8974 if (nCommand == NF_REQUERY)
8975 infoPtr->notifyFormat = SendMessageW(hwndFrom, WM_NOTIFYFORMAT, (WPARAM)infoPtr->hwndSelf, NF_QUERY);
8977 return infoPtr->notifyFormat;
8982 * Paints/Repaints the listview control.
8985 * [I] infoPtr : valid pointer to the listview structure
8986 * [I] hdc : device context handle
8991 static LRESULT LISTVIEW_Paint(LISTVIEW_INFO *infoPtr, HDC hdc)
8993 TRACE("(hdc=%p)\n", hdc);
8995 if (infoPtr->bNoItemMetrics && infoPtr->nItemCount)
8997 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
8999 infoPtr->bNoItemMetrics = FALSE;
9000 LISTVIEW_UpdateItemSize(infoPtr);
9001 if (uView == LVS_ICON || uView == LVS_SMALLICON)
9002 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
9003 LISTVIEW_UpdateScroll(infoPtr);
9006 UpdateWindow(infoPtr->hwndHeader);
9009 LISTVIEW_Refresh(infoPtr, hdc, NULL);
9014 hdc = BeginPaint(infoPtr->hwndSelf, &ps);
9016 LISTVIEW_Refresh(infoPtr, hdc, ps.fErase ? &ps.rcPaint : NULL);
9017 EndPaint(infoPtr->hwndSelf, &ps);
9026 * Paints/Repaints the listview control.
9029 * [I] infoPtr : valid pointer to the listview structure
9030 * [I] hdc : device context handle
9031 * [I] options : drawing options
9036 static LRESULT LISTVIEW_PrintClient(LISTVIEW_INFO *infoPtr, HDC hdc, DWORD options)
9038 FIXME("Partial Stub: (hdc=%p options=0x%08x)\n", hdc, options);
9040 if ((options & PRF_CHECKVISIBLE) && !IsWindowVisible(infoPtr->hwndSelf))
9043 if (options & PRF_ERASEBKGND)
9044 LISTVIEW_EraseBkgnd(infoPtr, hdc);
9046 if (options & PRF_CLIENT)
9047 LISTVIEW_Paint(infoPtr, hdc);
9055 * Processes double click messages (right mouse button).
9058 * [I] infoPtr : valid pointer to the listview structure
9059 * [I] wKey : key flag
9060 * [I] x,y : mouse coordinate
9065 static LRESULT LISTVIEW_RButtonDblClk(const LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
9067 LVHITTESTINFO lvHitTestInfo;
9069 TRACE("(key=%hu,X=%hu,Y=%hu)\n", wKey, x, y);
9071 /* send NM_RELEASEDCAPTURE notification */
9072 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
9074 /* send NM_RDBLCLK notification */
9075 lvHitTestInfo.pt.x = x;
9076 lvHitTestInfo.pt.y = y;
9077 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
9078 notify_click(infoPtr, NM_RDBLCLK, &lvHitTestInfo);
9085 * Processes mouse down messages (right mouse button).
9088 * [I] infoPtr : valid pointer to the listview structure
9089 * [I] wKey : key flag
9090 * [I] x,y : mouse coordinate
9095 static LRESULT LISTVIEW_RButtonDown(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
9097 LVHITTESTINFO lvHitTestInfo;
9100 TRACE("(key=%hu,X=%hu,Y=%hu)\n", wKey, x, y);
9102 /* send NM_RELEASEDCAPTURE notification */
9103 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
9105 /* make sure the listview control window has the focus */
9106 if (!infoPtr->bFocus) SetFocus(infoPtr->hwndSelf);
9108 /* set right button down flag */
9109 infoPtr->bRButtonDown = TRUE;
9111 /* determine the index of the selected item */
9112 lvHitTestInfo.pt.x = x;
9113 lvHitTestInfo.pt.y = y;
9114 nItem = LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, TRUE);
9116 if ((nItem >= 0) && (nItem < infoPtr->nItemCount))
9118 LISTVIEW_SetItemFocus(infoPtr, nItem);
9119 if (!((wKey & MK_SHIFT) || (wKey & MK_CONTROL)) &&
9120 !LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED))
9121 LISTVIEW_SetSelection(infoPtr, nItem);
9125 LISTVIEW_DeselectAll(infoPtr);
9133 * Processes mouse up messages (right mouse button).
9136 * [I] infoPtr : valid pointer to the listview structure
9137 * [I] wKey : key flag
9138 * [I] x,y : mouse coordinate
9143 static LRESULT LISTVIEW_RButtonUp(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
9145 LVHITTESTINFO lvHitTestInfo;
9148 TRACE("(key=%hu,X=%hu,Y=%hu)\n", wKey, x, y);
9150 if (!infoPtr->bRButtonDown) return 0;
9152 /* set button flag */
9153 infoPtr->bRButtonDown = FALSE;
9155 /* Send NM_RClICK notification */
9156 lvHitTestInfo.pt.x = x;
9157 lvHitTestInfo.pt.y = y;
9158 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
9159 if (!notify_click(infoPtr, NM_RCLICK, &lvHitTestInfo)) return 0;
9161 /* Change to screen coordinate for WM_CONTEXTMENU */
9162 pt = lvHitTestInfo.pt;
9163 ClientToScreen(infoPtr->hwndSelf, &pt);
9165 /* Send a WM_CONTEXTMENU message in response to the RBUTTONUP */
9166 SendMessageW(infoPtr->hwndSelf, WM_CONTEXTMENU,
9167 (WPARAM)infoPtr->hwndSelf, MAKELPARAM(pt.x, pt.y));
9178 * [I] infoPtr : valid pointer to the listview structure
9179 * [I] hwnd : window handle of window containing the cursor
9180 * [I] nHittest : hit-test code
9181 * [I] wMouseMsg : ideintifier of the mouse message
9184 * TRUE if cursor is set
9187 static BOOL LISTVIEW_SetCursor(const LISTVIEW_INFO *infoPtr, HWND hwnd, UINT nHittest, UINT wMouseMsg)
9189 LVHITTESTINFO lvHitTestInfo;
9191 if(!(infoPtr->dwLvExStyle & LVS_EX_TRACKSELECT)) return FALSE;
9193 if(!infoPtr->hHotCursor) return FALSE;
9195 GetCursorPos(&lvHitTestInfo.pt);
9196 if (LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, FALSE, FALSE) < 0) return FALSE;
9198 SetCursor(infoPtr->hHotCursor);
9208 * [I] infoPtr : valid pointer to the listview structure
9209 * [I] hwndLoseFocus : handle of previously focused window
9214 static LRESULT LISTVIEW_SetFocus(LISTVIEW_INFO *infoPtr, HWND hwndLoseFocus)
9216 TRACE("(hwndLoseFocus=%p)\n", hwndLoseFocus);
9218 /* if we have the focus already, there's nothing to do */
9219 if (infoPtr->bFocus) return 0;
9221 /* send NM_SETFOCUS notification */
9222 if (!notify(infoPtr, NM_SETFOCUS)) return 0;
9224 /* set window focus flag */
9225 infoPtr->bFocus = TRUE;
9227 /* put the focus rect back on */
9228 LISTVIEW_ShowFocusRect(infoPtr, TRUE);
9230 /* redraw all visible selected items */
9231 LISTVIEW_InvalidateSelectedItems(infoPtr);
9241 * [I] infoPtr : valid pointer to the listview structure
9242 * [I] fRedraw : font handle
9243 * [I] fRedraw : redraw flag
9248 static LRESULT LISTVIEW_SetFont(LISTVIEW_INFO *infoPtr, HFONT hFont, WORD fRedraw)
9250 HFONT oldFont = infoPtr->hFont;
9252 TRACE("(hfont=%p,redraw=%hu)\n", hFont, fRedraw);
9254 infoPtr->hFont = hFont ? hFont : infoPtr->hDefaultFont;
9255 if (infoPtr->hFont == oldFont) return 0;
9257 LISTVIEW_SaveTextMetrics(infoPtr);
9259 if ((infoPtr->dwStyle & LVS_TYPEMASK) == LVS_REPORT)
9260 SendMessageW(infoPtr->hwndHeader, WM_SETFONT, (WPARAM)hFont, MAKELPARAM(fRedraw, 0));
9262 if (fRedraw) LISTVIEW_InvalidateList(infoPtr);
9269 * Message handling for WM_SETREDRAW.
9270 * For the Listview, it invalidates the entire window (the doc specifies otherwise)
9273 * [I] infoPtr : valid pointer to the listview structure
9274 * [I] bRedraw: state of redraw flag
9277 * DefWinProc return value
9279 static LRESULT LISTVIEW_SetRedraw(LISTVIEW_INFO *infoPtr, BOOL bRedraw)
9281 TRACE("infoPtr->bRedraw=%d, bRedraw=%d\n", infoPtr->bRedraw, bRedraw);
9283 /* we cannot use straight equality here because _any_ non-zero value is TRUE */
9284 if ((infoPtr->bRedraw && bRedraw) || (!infoPtr->bRedraw && !bRedraw)) return 0;
9286 infoPtr->bRedraw = bRedraw;
9288 if(!bRedraw) return 0;
9290 if (is_autoarrange(infoPtr))
9291 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
9292 LISTVIEW_UpdateScroll(infoPtr);
9294 /* despite what the WM_SETREDRAW docs says, apps expect us
9295 * to invalidate the listview here... stupid! */
9296 LISTVIEW_InvalidateList(infoPtr);
9303 * Resizes the listview control. This function processes WM_SIZE
9304 * messages. At this time, the width and height are not used.
9307 * [I] infoPtr : valid pointer to the listview structure
9308 * [I] Width : new width
9309 * [I] Height : new height
9314 static LRESULT LISTVIEW_Size(LISTVIEW_INFO *infoPtr, int Width, int Height)
9316 RECT rcOld = infoPtr->rcList;
9318 TRACE("(width=%d, height=%d)\n", Width, Height);
9320 LISTVIEW_UpdateSize(infoPtr);
9321 if (EqualRect(&rcOld, &infoPtr->rcList)) return 0;
9323 /* do not bother with display related stuff if we're not redrawing */
9324 if (!is_redrawing(infoPtr)) return 0;
9326 if (is_autoarrange(infoPtr))
9327 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
9329 LISTVIEW_UpdateScroll(infoPtr);
9331 /* refresh all only for lists whose height changed significantly */
9332 if ((infoPtr->dwStyle & LVS_TYPEMASK) == LVS_LIST &&
9333 (rcOld.bottom - rcOld.top) / infoPtr->nItemHeight !=
9334 (infoPtr->rcList.bottom - infoPtr->rcList.top) / infoPtr->nItemHeight)
9335 LISTVIEW_InvalidateList(infoPtr);
9342 * Sets the size information.
9345 * [I] infoPtr : valid pointer to the listview structure
9350 static void LISTVIEW_UpdateSize(LISTVIEW_INFO *infoPtr)
9352 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
9354 TRACE("uView=%d, rcList(old)=%s\n", uView, wine_dbgstr_rect(&infoPtr->rcList));
9356 GetClientRect(infoPtr->hwndSelf, &infoPtr->rcList);
9358 if (uView == LVS_LIST)
9360 /* Apparently the "LIST" style is supposed to have the same
9361 * number of items in a column even if there is no scroll bar.
9362 * Since if a scroll bar already exists then the bottom is already
9363 * reduced, only reduce if the scroll bar does not currently exist.
9364 * The "2" is there to mimic the native control. I think it may be
9365 * related to either padding or edges. (GLA 7/2002)
9367 if (!(GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE) & WS_HSCROLL))
9368 infoPtr->rcList.bottom -= GetSystemMetrics(SM_CYHSCROLL);
9369 infoPtr->rcList.bottom = max (infoPtr->rcList.bottom - 2, 0);
9371 else if (uView == LVS_REPORT && !(infoPtr->dwStyle & LVS_NOCOLUMNHEADER))
9376 hl.prc = &infoPtr->rcList;
9378 SendMessageW( infoPtr->hwndHeader, HDM_LAYOUT, 0, (LPARAM)&hl );
9379 TRACE(" wp.flags=0x%08x, wp=%d,%d (%dx%d)\n", wp.flags, wp.x, wp.y, wp.cx, wp.cy);
9380 SetWindowPos(wp.hwnd, wp.hwndInsertAfter, wp.x, wp.y, wp.cx, wp.cy, wp.flags);
9381 TRACE(" after SWP wp=%d,%d (%dx%d)\n", wp.x, wp.y, wp.cx, wp.cy);
9383 infoPtr->rcList.top = max(wp.cy, 0);
9384 infoPtr->rcList.top += (infoPtr->dwLvExStyle & LVS_EX_GRIDLINES) ? 2 : 0;
9387 TRACE(" rcList=%s\n", wine_dbgstr_rect(&infoPtr->rcList));
9392 * Processes WM_STYLECHANGED messages.
9395 * [I] infoPtr : valid pointer to the listview structure
9396 * [I] wStyleType : window style type (normal or extended)
9397 * [I] lpss : window style information
9402 static INT LISTVIEW_StyleChanged(LISTVIEW_INFO *infoPtr, WPARAM wStyleType,
9403 const STYLESTRUCT *lpss)
9405 UINT uNewView = lpss->styleNew & LVS_TYPEMASK;
9406 UINT uOldView = lpss->styleOld & LVS_TYPEMASK;
9408 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
9409 wStyleType, lpss->styleOld, lpss->styleNew);
9411 if (wStyleType != GWL_STYLE) return 0;
9413 /* FIXME: if LVS_NOSORTHEADER changed, update header */
9414 /* what if LVS_OWNERDATA changed? */
9415 /* or LVS_SINGLESEL */
9416 /* or LVS_SORT{AS,DES}CENDING */
9418 infoPtr->dwStyle = lpss->styleNew;
9420 if (((lpss->styleOld & WS_HSCROLL) != 0)&&
9421 ((lpss->styleNew & WS_HSCROLL) == 0))
9422 ShowScrollBar(infoPtr->hwndSelf, SB_HORZ, FALSE);
9424 if (((lpss->styleOld & WS_VSCROLL) != 0)&&
9425 ((lpss->styleNew & WS_VSCROLL) == 0))
9426 ShowScrollBar(infoPtr->hwndSelf, SB_VERT, FALSE);
9428 if (uNewView != uOldView)
9430 SIZE oldIconSize = infoPtr->iconSize;
9433 SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
9434 ShowWindow(infoPtr->hwndHeader, SW_HIDE);
9436 ShowScrollBar(infoPtr->hwndSelf, SB_BOTH, FALSE);
9437 SetRectEmpty(&infoPtr->rcFocus);
9439 himl = (uNewView == LVS_ICON ? infoPtr->himlNormal : infoPtr->himlSmall);
9440 set_icon_size(&infoPtr->iconSize, himl, uNewView != LVS_ICON);
9442 if (uNewView == LVS_ICON)
9444 if ((infoPtr->iconSize.cx != oldIconSize.cx) || (infoPtr->iconSize.cy != oldIconSize.cy))
9446 TRACE("icon old size=(%d,%d), new size=(%d,%d)\n",
9447 oldIconSize.cx, oldIconSize.cy, infoPtr->iconSize.cx, infoPtr->iconSize.cy);
9448 LISTVIEW_SetIconSpacing(infoPtr, 0, 0);
9451 else if (uNewView == LVS_REPORT)
9456 hl.prc = &infoPtr->rcList;
9458 SendMessageW( infoPtr->hwndHeader, HDM_LAYOUT, 0, (LPARAM)&hl );
9459 SetWindowPos(infoPtr->hwndHeader, infoPtr->hwndSelf, wp.x, wp.y, wp.cx, wp.cy, wp.flags);
9462 LISTVIEW_UpdateItemSize(infoPtr);
9465 if (uNewView == LVS_REPORT)
9466 ShowWindow(infoPtr->hwndHeader, (lpss->styleNew & LVS_NOCOLUMNHEADER) ? SW_HIDE : SW_SHOWNORMAL);
9468 if ( (uNewView == LVS_ICON || uNewView == LVS_SMALLICON) &&
9469 (uNewView != uOldView || ((lpss->styleNew ^ lpss->styleOld) & LVS_ALIGNMASK)) )
9470 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
9472 /* update the size of the client area */
9473 LISTVIEW_UpdateSize(infoPtr);
9475 /* add scrollbars if needed */
9476 LISTVIEW_UpdateScroll(infoPtr);
9478 /* invalidate client area + erase background */
9479 LISTVIEW_InvalidateList(infoPtr);
9486 * Window procedure of the listview control.
9489 static LRESULT WINAPI
9490 LISTVIEW_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
9492 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongPtrW(hwnd, 0);
9494 TRACE("(uMsg=%x wParam=%lx lParam=%lx)\n", uMsg, wParam, lParam);
9496 if (!infoPtr && (uMsg != WM_NCCREATE))
9497 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
9501 case LVM_APPROXIMATEVIEWRECT:
9502 return LISTVIEW_ApproximateViewRect(infoPtr, (INT)wParam,
9503 LOWORD(lParam), HIWORD(lParam));
9505 return LISTVIEW_Arrange(infoPtr, (INT)wParam);
9507 /* case LVM_CANCELEDITLABEL: */
9509 case LVM_CREATEDRAGIMAGE:
9510 return (LRESULT)LISTVIEW_CreateDragImage(infoPtr, (INT)wParam, (LPPOINT)lParam);
9512 case LVM_DELETEALLITEMS:
9513 return LISTVIEW_DeleteAllItems(infoPtr);
9515 case LVM_DELETECOLUMN:
9516 return LISTVIEW_DeleteColumn(infoPtr, (INT)wParam);
9518 case LVM_DELETEITEM:
9519 return LISTVIEW_DeleteItem(infoPtr, (INT)wParam);
9521 case LVM_EDITLABELW:
9522 return (LRESULT)LISTVIEW_EditLabelT(infoPtr, (INT)wParam, TRUE);
9524 case LVM_EDITLABELA:
9525 return (LRESULT)LISTVIEW_EditLabelT(infoPtr, (INT)wParam, FALSE);
9527 /* case LVM_ENABLEGROUPVIEW: */
9529 case LVM_ENSUREVISIBLE:
9530 return LISTVIEW_EnsureVisible(infoPtr, (INT)wParam, (BOOL)lParam);
9533 return LISTVIEW_FindItemW(infoPtr, (INT)wParam, (LPLVFINDINFOW)lParam);
9536 return LISTVIEW_FindItemA(infoPtr, (INT)wParam, (LPLVFINDINFOA)lParam);
9538 case LVM_GETBKCOLOR:
9539 return infoPtr->clrBk;
9541 /* case LVM_GETBKIMAGE: */
9543 case LVM_GETCALLBACKMASK:
9544 return infoPtr->uCallbackMask;
9546 case LVM_GETCOLUMNA:
9547 return LISTVIEW_GetColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam, FALSE);
9549 case LVM_GETCOLUMNW:
9550 return LISTVIEW_GetColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam, TRUE);
9552 case LVM_GETCOLUMNORDERARRAY:
9553 return LISTVIEW_GetColumnOrderArray(infoPtr, (INT)wParam, (LPINT)lParam);
9555 case LVM_GETCOLUMNWIDTH:
9556 return LISTVIEW_GetColumnWidth(infoPtr, (INT)wParam);
9558 case LVM_GETCOUNTPERPAGE:
9559 return LISTVIEW_GetCountPerPage(infoPtr);
9561 case LVM_GETEDITCONTROL:
9562 return (LRESULT)infoPtr->hwndEdit;
9564 case LVM_GETEXTENDEDLISTVIEWSTYLE:
9565 return infoPtr->dwLvExStyle;
9567 /* case LVM_GETGROUPINFO: */
9569 /* case LVM_GETGROUPMETRICS: */
9572 return (LRESULT)infoPtr->hwndHeader;
9574 case LVM_GETHOTCURSOR:
9575 return (LRESULT)infoPtr->hHotCursor;
9577 case LVM_GETHOTITEM:
9578 return infoPtr->nHotItem;
9580 case LVM_GETHOVERTIME:
9581 return infoPtr->dwHoverTime;
9583 case LVM_GETIMAGELIST:
9584 return (LRESULT)LISTVIEW_GetImageList(infoPtr, (INT)wParam);
9586 /* case LVM_GETINSERTMARK: */
9588 /* case LVM_GETINSERTMARKCOLOR: */
9590 /* case LVM_GETINSERTMARKRECT: */
9592 case LVM_GETISEARCHSTRINGA:
9593 case LVM_GETISEARCHSTRINGW:
9594 FIXME("LVM_GETISEARCHSTRING: unimplemented\n");
9598 return LISTVIEW_GetItemExtT(infoPtr, (LPLVITEMW)lParam, FALSE);
9601 return LISTVIEW_GetItemExtT(infoPtr, (LPLVITEMW)lParam, TRUE);
9603 case LVM_GETITEMCOUNT:
9604 return infoPtr->nItemCount;
9606 case LVM_GETITEMPOSITION:
9607 return LISTVIEW_GetItemPosition(infoPtr, (INT)wParam, (LPPOINT)lParam);
9609 case LVM_GETITEMRECT:
9610 return LISTVIEW_GetItemRect(infoPtr, (INT)wParam, (LPRECT)lParam);
9612 case LVM_GETITEMSPACING:
9613 return LISTVIEW_GetItemSpacing(infoPtr, (BOOL)wParam);
9615 case LVM_GETITEMSTATE:
9616 return LISTVIEW_GetItemState(infoPtr, (INT)wParam, (UINT)lParam);
9618 case LVM_GETITEMTEXTA:
9619 return LISTVIEW_GetItemTextT(infoPtr, (INT)wParam, (LPLVITEMW)lParam, FALSE);
9621 case LVM_GETITEMTEXTW:
9622 return LISTVIEW_GetItemTextT(infoPtr, (INT)wParam, (LPLVITEMW)lParam, TRUE);
9624 case LVM_GETNEXTITEM:
9625 return LISTVIEW_GetNextItem(infoPtr, (INT)wParam, LOWORD(lParam));
9627 case LVM_GETNUMBEROFWORKAREAS:
9628 FIXME("LVM_GETNUMBEROFWORKAREAS: unimplemented\n");
9632 if (!lParam) return FALSE;
9633 if ((infoPtr->dwStyle & LVS_TYPEMASK) == LVS_REPORT ||
9634 (infoPtr->dwStyle & LVS_TYPEMASK) == LVS_LIST) return FALSE;
9635 LISTVIEW_GetOrigin(infoPtr, (LPPOINT)lParam);
9638 /* case LVM_GETOUTLINECOLOR: */
9640 /* case LVM_GETSELECTEDCOLUMN: */
9642 case LVM_GETSELECTEDCOUNT:
9643 return LISTVIEW_GetSelectedCount(infoPtr);
9645 case LVM_GETSELECTIONMARK:
9646 return infoPtr->nSelectionMark;
9648 case LVM_GETSTRINGWIDTHA:
9649 return LISTVIEW_GetStringWidthT(infoPtr, (LPCWSTR)lParam, FALSE);
9651 case LVM_GETSTRINGWIDTHW:
9652 return LISTVIEW_GetStringWidthT(infoPtr, (LPCWSTR)lParam, TRUE);
9654 case LVM_GETSUBITEMRECT:
9655 return LISTVIEW_GetSubItemRect(infoPtr, (UINT)wParam, (LPRECT)lParam);
9657 case LVM_GETTEXTBKCOLOR:
9658 return infoPtr->clrTextBk;
9660 case LVM_GETTEXTCOLOR:
9661 return infoPtr->clrText;
9663 /* case LVM_GETTILEINFO: */
9665 /* case LVM_GETTILEVIEWINFO: */
9667 case LVM_GETTOOLTIPS:
9668 if( !infoPtr->hwndToolTip )
9669 infoPtr->hwndToolTip = COMCTL32_CreateToolTip( hwnd );
9670 return (LRESULT)infoPtr->hwndToolTip;
9672 case LVM_GETTOPINDEX:
9673 return LISTVIEW_GetTopIndex(infoPtr);
9675 /*case LVM_GETUNICODEFORMAT:
9676 FIXME("LVM_GETUNICODEFORMAT: unimplemented\n");
9679 /* case LVM_GETVIEW: */
9681 case LVM_GETVIEWRECT:
9682 return LISTVIEW_GetViewRect(infoPtr, (LPRECT)lParam);
9684 case LVM_GETWORKAREAS:
9685 FIXME("LVM_GETWORKAREAS: unimplemented\n");
9688 /* case LVM_HASGROUP: */
9691 return LISTVIEW_HitTest(infoPtr, (LPLVHITTESTINFO)lParam, FALSE, FALSE);
9693 case LVM_INSERTCOLUMNA:
9694 return LISTVIEW_InsertColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam, FALSE);
9696 case LVM_INSERTCOLUMNW:
9697 return LISTVIEW_InsertColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam, TRUE);
9699 /* case LVM_INSERTGROUP: */
9701 /* case LVM_INSERTGROUPSORTED: */
9703 case LVM_INSERTITEMA:
9704 return LISTVIEW_InsertItemT(infoPtr, (LPLVITEMW)lParam, FALSE);
9706 case LVM_INSERTITEMW:
9707 return LISTVIEW_InsertItemT(infoPtr, (LPLVITEMW)lParam, TRUE);
9709 /* case LVM_INSERTMARKHITTEST: */
9711 /* case LVM_ISGROUPVIEWENABLED: */
9713 /* case LVM_MAPIDTOINDEX: */
9715 /* case LVM_MAPINDEXTOID: */
9717 /* case LVM_MOVEGROUP: */
9719 /* case LVM_MOVEITEMTOGROUP: */
9721 case LVM_REDRAWITEMS:
9722 return LISTVIEW_RedrawItems(infoPtr, (INT)wParam, (INT)lParam);
9724 /* case LVM_REMOVEALLGROUPS: */
9726 /* case LVM_REMOVEGROUP: */
9729 return LISTVIEW_Scroll(infoPtr, (INT)wParam, (INT)lParam);
9731 case LVM_SETBKCOLOR:
9732 return LISTVIEW_SetBkColor(infoPtr, (COLORREF)lParam);
9734 /* case LVM_SETBKIMAGE: */
9736 case LVM_SETCALLBACKMASK:
9737 infoPtr->uCallbackMask = (UINT)wParam;
9740 case LVM_SETCOLUMNA:
9741 return LISTVIEW_SetColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam, FALSE);
9743 case LVM_SETCOLUMNW:
9744 return LISTVIEW_SetColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam, TRUE);
9746 case LVM_SETCOLUMNORDERARRAY:
9747 return LISTVIEW_SetColumnOrderArray(infoPtr, (INT)wParam, (LPINT)lParam);
9749 case LVM_SETCOLUMNWIDTH:
9750 return LISTVIEW_SetColumnWidth(infoPtr, (INT)wParam, (short)LOWORD(lParam));
9752 case LVM_SETEXTENDEDLISTVIEWSTYLE:
9753 return LISTVIEW_SetExtendedListViewStyle(infoPtr, (DWORD)wParam, (DWORD)lParam);
9755 /* case LVM_SETGROUPINFO: */
9757 /* case LVM_SETGROUPMETRICS: */
9759 case LVM_SETHOTCURSOR:
9760 return (LRESULT)LISTVIEW_SetHotCursor(infoPtr, (HCURSOR)lParam);
9762 case LVM_SETHOTITEM:
9763 return LISTVIEW_SetHotItem(infoPtr, (INT)wParam);
9765 case LVM_SETHOVERTIME:
9766 return LISTVIEW_SetHoverTime(infoPtr, (DWORD)wParam);
9768 case LVM_SETICONSPACING:
9769 return LISTVIEW_SetIconSpacing(infoPtr, (short)LOWORD(lParam), (short)HIWORD(lParam));
9771 case LVM_SETIMAGELIST:
9772 return (LRESULT)LISTVIEW_SetImageList(infoPtr, (INT)wParam, (HIMAGELIST)lParam);
9774 /* case LVM_SETINFOTIP: */
9776 /* case LVM_SETINSERTMARK: */
9778 /* case LVM_SETINSERTMARKCOLOR: */
9781 return LISTVIEW_SetItemT(infoPtr, (LPLVITEMW)lParam, FALSE);
9784 return LISTVIEW_SetItemT(infoPtr, (LPLVITEMW)lParam, TRUE);
9786 case LVM_SETITEMCOUNT:
9787 return LISTVIEW_SetItemCount(infoPtr, (INT)wParam, (DWORD)lParam);
9789 case LVM_SETITEMPOSITION:
9792 pt.x = (short)LOWORD(lParam);
9793 pt.y = (short)HIWORD(lParam);
9794 return LISTVIEW_SetItemPosition(infoPtr, (INT)wParam, pt);
9797 case LVM_SETITEMPOSITION32:
9798 if (lParam == 0) return FALSE;
9799 return LISTVIEW_SetItemPosition(infoPtr, (INT)wParam, *((POINT*)lParam));
9801 case LVM_SETITEMSTATE:
9802 return LISTVIEW_SetItemState(infoPtr, (INT)wParam, (LPLVITEMW)lParam);
9804 case LVM_SETITEMTEXTA:
9805 return LISTVIEW_SetItemTextT(infoPtr, (INT)wParam, (LPLVITEMW)lParam, FALSE);
9807 case LVM_SETITEMTEXTW:
9808 return LISTVIEW_SetItemTextT(infoPtr, (INT)wParam, (LPLVITEMW)lParam, TRUE);
9810 /* case LVM_SETOUTLINECOLOR: */
9812 /* case LVM_SETSELECTEDCOLUMN: */
9814 case LVM_SETSELECTIONMARK:
9815 return LISTVIEW_SetSelectionMark(infoPtr, (INT)lParam);
9817 case LVM_SETTEXTBKCOLOR:
9818 return LISTVIEW_SetTextBkColor(infoPtr, (COLORREF)lParam);
9820 case LVM_SETTEXTCOLOR:
9821 return LISTVIEW_SetTextColor(infoPtr, (COLORREF)lParam);
9823 /* case LVM_SETTILEINFO: */
9825 /* case LVM_SETTILEVIEWINFO: */
9827 /* case LVM_SETTILEWIDTH: */
9829 case LVM_SETTOOLTIPS:
9830 return (LRESULT)LISTVIEW_SetToolTips(infoPtr, (HWND)lParam);
9832 case LVM_SETUNICODEFORMAT:
9833 return LISTVIEW_SetUnicodeFormat(infoPtr, wParam);
9835 /* case LVM_SETVIEW: */
9837 /* case LVM_SETWORKAREAS: */
9839 /* case LVM_SORTGROUPS: */
9842 return LISTVIEW_SortItems(infoPtr, (PFNLVCOMPARE)lParam, (LPARAM)wParam);
9844 /* LVM_SORTITEMSEX: */
9846 case LVM_SUBITEMHITTEST:
9847 return LISTVIEW_HitTest(infoPtr, (LPLVHITTESTINFO)lParam, TRUE, FALSE);
9850 return LISTVIEW_Update(infoPtr, (INT)wParam);
9853 return LISTVIEW_ProcessLetterKeys( infoPtr, wParam, lParam );
9856 return LISTVIEW_Command(infoPtr, wParam, lParam);
9859 return LISTVIEW_NCCreate(hwnd, (LPCREATESTRUCTW)lParam);
9862 return LISTVIEW_Create(hwnd, (LPCREATESTRUCTW)lParam);
9865 return LISTVIEW_Destroy(infoPtr);
9868 return LISTVIEW_Enable(infoPtr, (BOOL)wParam);
9871 return LISTVIEW_EraseBkgnd(infoPtr, (HDC)wParam);
9874 return DLGC_WANTCHARS | DLGC_WANTARROWS;
9877 return (LRESULT)infoPtr->hFont;
9880 return LISTVIEW_HScroll(infoPtr, (INT)LOWORD(wParam), 0, (HWND)lParam);
9883 return LISTVIEW_KeyDown(infoPtr, (INT)wParam, (LONG)lParam);
9886 return LISTVIEW_KillFocus(infoPtr);
9888 case WM_LBUTTONDBLCLK:
9889 return LISTVIEW_LButtonDblClk(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
9891 case WM_LBUTTONDOWN:
9892 return LISTVIEW_LButtonDown(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
9895 return LISTVIEW_LButtonUp(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
9898 return LISTVIEW_MouseMove (infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
9901 return LISTVIEW_MouseHover(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
9904 return LISTVIEW_NCDestroy(infoPtr);
9907 if (LISTVIEW_NCPaint(infoPtr, (HRGN)wParam))
9912 if (lParam && ((LPNMHDR)lParam)->hwndFrom == infoPtr->hwndHeader)
9913 return LISTVIEW_HeaderNotification(infoPtr, (LPNMHEADERW)lParam);
9916 case WM_NOTIFYFORMAT:
9917 return LISTVIEW_NotifyFormat(infoPtr, (HWND)wParam, (INT)lParam);
9919 case WM_PRINTCLIENT:
9920 return LISTVIEW_PrintClient(infoPtr, (HDC)wParam, (DWORD)lParam);
9923 return LISTVIEW_Paint(infoPtr, (HDC)wParam);
9925 case WM_RBUTTONDBLCLK:
9926 return LISTVIEW_RButtonDblClk(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
9928 case WM_RBUTTONDOWN:
9929 return LISTVIEW_RButtonDown(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
9932 return LISTVIEW_RButtonUp(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
9935 if(LISTVIEW_SetCursor(infoPtr, (HWND)wParam, LOWORD(lParam), HIWORD(lParam)))
9940 return LISTVIEW_SetFocus(infoPtr, (HWND)wParam);
9943 return LISTVIEW_SetFont(infoPtr, (HFONT)wParam, (WORD)lParam);
9946 return LISTVIEW_SetRedraw(infoPtr, (BOOL)wParam);
9949 return LISTVIEW_Size(infoPtr, (short)LOWORD(lParam), (short)HIWORD(lParam));
9951 case WM_STYLECHANGED:
9952 return LISTVIEW_StyleChanged(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
9954 case WM_SYSCOLORCHANGE:
9955 COMCTL32_RefreshSysColors();
9958 /* case WM_TIMER: */
9959 case WM_THEMECHANGED:
9960 return LISTVIEW_ThemeChanged(infoPtr);
9963 return LISTVIEW_VScroll(infoPtr, (INT)LOWORD(wParam), 0, (HWND)lParam);
9966 if (wParam & (MK_SHIFT | MK_CONTROL))
9967 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
9968 return LISTVIEW_MouseWheel(infoPtr, (short int)HIWORD(wParam));
9970 case WM_WINDOWPOSCHANGED:
9971 if (!(((WINDOWPOS *)lParam)->flags & SWP_NOSIZE))
9973 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
9974 SetWindowPos(infoPtr->hwndSelf, 0, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOACTIVATE |
9975 SWP_NOZORDER | SWP_NOMOVE | SWP_NOSIZE);
9977 if ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (uView == LVS_REPORT))
9979 MEASUREITEMSTRUCT mis;
9980 mis.CtlType = ODT_LISTVIEW;
9981 mis.CtlID = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
9985 mis.itemHeight= infoPtr->nItemHeight;
9986 SendMessageW(infoPtr->hwndNotify, WM_MEASUREITEM, mis.CtlID, (LPARAM)&mis);
9987 if (infoPtr->nItemHeight != max(mis.itemHeight, 1))
9988 infoPtr->nMeasureItemHeight = infoPtr->nItemHeight = max(mis.itemHeight, 1);
9991 LISTVIEW_UpdateSize(infoPtr);
9992 LISTVIEW_UpdateScroll(infoPtr);
9994 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
9996 /* case WM_WININICHANGE: */
9999 if ((uMsg >= WM_USER) && (uMsg < WM_APP))
10000 ERR("unknown msg %04x wp=%08lx lp=%08lx\n", uMsg, wParam, lParam);
10003 /* call default window procedure */
10004 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
10011 * Registers the window class.
10019 void LISTVIEW_Register(void)
10021 WNDCLASSW wndClass;
10023 ZeroMemory(&wndClass, sizeof(WNDCLASSW));
10024 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS;
10025 wndClass.lpfnWndProc = LISTVIEW_WindowProc;
10026 wndClass.cbClsExtra = 0;
10027 wndClass.cbWndExtra = sizeof(LISTVIEW_INFO *);
10028 wndClass.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
10029 wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
10030 wndClass.lpszClassName = WC_LISTVIEWW;
10031 RegisterClassW(&wndClass);
10036 * Unregisters the window class.
10044 void LISTVIEW_Unregister(void)
10046 UnregisterClassW(WC_LISTVIEWW, NULL);
10051 * Handle any WM_COMMAND messages
10054 * [I] infoPtr : valid pointer to the listview structure
10055 * [I] wParam : the first message parameter
10056 * [I] lParam : the second message parameter
10061 static LRESULT LISTVIEW_Command(const LISTVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
10063 switch (HIWORD(wParam))
10068 * Adjust the edit window size
10070 WCHAR buffer[1024];
10071 HDC hdc = GetDC(infoPtr->hwndEdit);
10072 HFONT hFont, hOldFont = 0;
10076 if (!infoPtr->hwndEdit || !hdc) return 0;
10077 GetWindowTextW(infoPtr->hwndEdit, buffer, sizeof(buffer)/sizeof(buffer[0]));
10078 GetWindowRect(infoPtr->hwndEdit, &rect);
10080 /* Select font to get the right dimension of the string */
10081 hFont = (HFONT)SendMessageW(infoPtr->hwndEdit, WM_GETFONT, 0, 0);
10084 hOldFont = SelectObject(hdc, hFont);
10087 if (GetTextExtentPoint32W(hdc, buffer, lstrlenW(buffer), &sz))
10089 TEXTMETRICW textMetric;
10091 /* Add Extra spacing for the next character */
10092 GetTextMetricsW(hdc, &textMetric);
10093 sz.cx += (textMetric.tmMaxCharWidth * 2);
10101 rect.bottom - rect.top,
10102 SWP_DRAWFRAME|SWP_NOMOVE);
10105 SelectObject(hdc, hOldFont);
10107 ReleaseDC(infoPtr->hwndEdit, hdc);
10113 return SendMessageW (infoPtr->hwndNotify, WM_COMMAND, wParam, lParam);
10122 * Subclassed edit control windproc function
10125 * [I] hwnd : the edit window handle
10126 * [I] uMsg : the message that is to be processed
10127 * [I] wParam : first message parameter
10128 * [I] lParam : second message parameter
10129 * [I] isW : TRUE if input is Unicode
10134 static LRESULT EditLblWndProcT(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL isW)
10136 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongPtrW(GetParent(hwnd), 0);
10137 BOOL cancel = FALSE;
10139 TRACE("(hwnd=%p, uMsg=%x, wParam=%lx, lParam=%lx, isW=%d)\n",
10140 hwnd, uMsg, wParam, lParam, isW);
10144 case WM_GETDLGCODE:
10145 return DLGC_WANTARROWS | DLGC_WANTALLKEYS;
10152 WNDPROC editProc = infoPtr->EditWndProc;
10153 infoPtr->EditWndProc = 0;
10154 SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (DWORD_PTR)editProc);
10155 return CallWindowProcT(editProc, hwnd, uMsg, wParam, lParam, isW);
10159 if (VK_ESCAPE == (INT)wParam)
10164 else if (VK_RETURN == (INT)wParam)
10168 return CallWindowProcT(infoPtr->EditWndProc, hwnd, uMsg, wParam, lParam, isW);
10171 /* kill the edit */
10172 if (infoPtr->hwndEdit)
10174 LPWSTR buffer = NULL;
10176 infoPtr->hwndEdit = 0;
10179 DWORD len = isW ? GetWindowTextLengthW(hwnd) : GetWindowTextLengthA(hwnd);
10183 if ( (buffer = Alloc((len+1) * (isW ? sizeof(WCHAR) : sizeof(CHAR)))) )
10185 if (isW) GetWindowTextW(hwnd, buffer, len+1);
10186 else GetWindowTextA(hwnd, (CHAR*)buffer, len+1);
10190 LISTVIEW_EndEditLabelT(infoPtr, buffer, isW);
10195 SendMessageW(hwnd, WM_CLOSE, 0, 0);
10201 * Subclassed edit control Unicode windproc function
10204 * [I] hwnd : the edit window handle
10205 * [I] uMsg : the message that is to be processed
10206 * [I] wParam : first message parameter
10207 * [I] lParam : second message parameter
10211 static LRESULT CALLBACK EditLblWndProcW(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
10213 return EditLblWndProcT(hwnd, uMsg, wParam, lParam, TRUE);
10218 * Subclassed edit control ANSI windproc function
10221 * [I] hwnd : the edit window handle
10222 * [I] uMsg : the message that is to be processed
10223 * [I] wParam : first message parameter
10224 * [I] lParam : second message parameter
10228 static LRESULT CALLBACK EditLblWndProcA(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
10230 return EditLblWndProcT(hwnd, uMsg, wParam, lParam, FALSE);
10235 * Creates a subclassed edit control
10238 * [I] infoPtr : valid pointer to the listview structure
10239 * [I] text : initial text for the edit
10240 * [I] style : the window style
10241 * [I] isW : TRUE if input is Unicode
10245 static HWND CreateEditLabelT(LISTVIEW_INFO *infoPtr, LPCWSTR text, DWORD style,
10246 INT x, INT y, INT width, INT height, BOOL isW)
10248 WCHAR editName[5] = { 'E', 'd', 'i', 't', '\0' };
10253 TEXTMETRICW textMetric;
10254 HINSTANCE hinst = (HINSTANCE)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_HINSTANCE);
10256 TRACE("(text=%s, ..., isW=%d)\n", debugtext_t(text, isW), isW);
10258 style |= WS_CHILDWINDOW|WS_CLIPSIBLINGS|ES_LEFT|ES_AUTOHSCROLL|WS_BORDER;
10259 hdc = GetDC(infoPtr->hwndSelf);
10261 /* Select the font to get appropriate metric dimensions */
10262 if(infoPtr->hFont != 0)
10263 hOldFont = SelectObject(hdc, infoPtr->hFont);
10265 /*Get String Length in pixels */
10266 GetTextExtentPoint32W(hdc, text, lstrlenW(text), &sz);
10268 /*Add Extra spacing for the next character */
10269 GetTextMetricsW(hdc, &textMetric);
10270 sz.cx += (textMetric.tmMaxCharWidth * 2);
10272 if(infoPtr->hFont != 0)
10273 SelectObject(hdc, hOldFont);
10275 ReleaseDC(infoPtr->hwndSelf, hdc);
10277 hedit = CreateWindowW(editName, text, style, x, y, sz.cx, height, infoPtr->hwndSelf, 0, hinst, 0);
10279 hedit = CreateWindowA("Edit", (LPCSTR)text, style, x, y, sz.cx, height, infoPtr->hwndSelf, 0, hinst, 0);
10281 if (!hedit) return 0;
10283 infoPtr->EditWndProc = (WNDPROC)
10284 (isW ? SetWindowLongPtrW(hedit, GWLP_WNDPROC, (DWORD_PTR)EditLblWndProcW) :
10285 SetWindowLongPtrA(hedit, GWLP_WNDPROC, (DWORD_PTR)EditLblWndProcA) );
10287 SendMessageW(hedit, WM_SETFONT, (WPARAM)infoPtr->hFont, FALSE);