4 * Copyright 1998, 1999 Eric Kohl
5 * Copyright 1999 Luc Tourangeau
6 * Copyright 2000 Jason Mawdsley
7 * Copyright 2001 CodeWeavers Inc.
8 * Copyright 2002 Dimitrie O. Paun
9 * Copyright 2009 Nikolay Sivov
10 * Copyright 2009 Owen Rudge for CodeWeavers
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; either
15 * version 2.1 of the License, or (at your option) any later version.
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
28 * This code was audited for completeness against the documented features
29 * of Comctl32.dll version 6.0 on May. 20, 2005, by James Hawkins.
31 * Unless otherwise noted, we believe this code to be complete, as per
32 * the specification mentioned above.
33 * If you discover missing features, or bugs, please note them below.
37 * Default Message Processing
38 * -- WM_CREATE: create the icon and small icon image lists at this point only if
39 * the LVS_SHAREIMAGELISTS style is not specified.
40 * -- WM_WINDOWPOSCHANGED: arrange the list items if the current view is icon
41 * or small icon and the LVS_AUTOARRANGE style is specified.
46 * -- Hot item handling, mouse hovering
47 * -- Workareas support
52 * -- Expand large item in ICON mode when the cursor is flying over the icon or text.
53 * -- Support CustomDraw options for _WIN32_IE >= 0x560 (see NMLVCUSTOMDRAW docs).
54 * -- LVA_SNAPTOGRID not implemented
55 * -- LISTVIEW_ApproximateViewRect partially implemented
56 * -- LISTVIEW_SetColumnWidth ignores header images & bitmap
57 * -- LISTVIEW_SetIconSpacing is incomplete
58 * -- LISTVIEW_StyleChanged doesn't handle some changes too well
61 * -- LISTVIEW_GetNextItem needs to be rewritten. It is currently
62 * linear in the number of items in the list, and this is
63 * unacceptable for large lists.
64 * -- if list is sorted by item text LISTVIEW_InsertItemT could use
65 * binary search to calculate item index (e.g. DPA_Search()).
66 * This requires sorted state to be reliably tracked in item modifiers.
67 * -- we should keep an ordered array of coordinates in iconic mode
68 * this would allow to frame items (iterator_frameditems),
69 * and find nearest item (LVFI_NEARESTXY) a lot more efficiently
76 * -- LVIS_ACTIVATING (not currently supported by comctl32.dll version 6.0)
83 * -- LVS_NOSCROLL (see Q137520)
87 * -- LVS_EX_BORDERSELECT
91 * -- LVS_EX_MULTIWORKAREAS
93 * -- LVS_EX_SIMPLESELECT
94 * -- LVS_EX_TWOCLICKACTIVATE
95 * -- LVS_EX_UNDERLINECOLD
96 * -- LVS_EX_UNDERLINEHOT
99 * -- LVN_BEGINSCROLL, LVN_ENDSCROLL
107 * -- LVM_ENABLEGROUPVIEW
108 * -- LVM_GETBKIMAGE, LVM_SETBKIMAGE
109 * -- LVM_GETGROUPINFO, LVM_SETGROUPINFO
110 * -- LVM_GETGROUPMETRICS, LVM_SETGROUPMETRICS
111 * -- LVM_GETINSERTMARK, LVM_SETINSERTMARK
112 * -- LVM_GETINSERTMARKCOLOR, LVM_SETINSERTMARKCOLOR
113 * -- LVM_GETINSERTMARKRECT
114 * -- LVM_GETNUMBEROFWORKAREAS
115 * -- LVM_GETOUTLINECOLOR, LVM_SETOUTLINECOLOR
116 * -- LVM_GETSELECTEDCOLUMN, LVM_SETSELECTEDCOLUMN
117 * -- LVM_GETISEARCHSTRINGW, LVM_GETISEARCHSTRINGA
118 * -- LVM_GETTILEINFO, LVM_SETTILEINFO
119 * -- LVM_GETTILEVIEWINFO, LVM_SETTILEVIEWINFO
120 * -- LVM_GETWORKAREAS, LVM_SETWORKAREAS
121 * -- LVM_HASGROUP, LVM_INSERTGROUP, LVM_REMOVEGROUP, LVM_REMOVEALLGROUPS
122 * -- LVM_INSERTGROUPSORTED
123 * -- LVM_INSERTMARKHITTEST
124 * -- LVM_ISGROUPVIEWENABLED
126 * -- LVM_MOVEITEMTOGROUP
128 * -- LVM_SETTILEWIDTH
132 * -- ListView_GetHoverTime, ListView_SetHoverTime
133 * -- ListView_GetISearchString
134 * -- ListView_GetNumberOfWorkAreas
135 * -- ListView_GetWorkAreas, ListView_SetWorkAreas
140 * Known differences in message stream from native control (not known if
141 * these differences cause problems):
142 * LVM_INSERTITEM issues LVM_SETITEMSTATE and LVM_SETITEM in certain cases.
143 * LVM_SETITEM does not always issue LVN_ITEMCHANGING/LVN_ITEMCHANGED.
144 * WM_CREATE does not issue WM_QUERYUISTATE and associated registry
145 * processing for "USEDOUBLECLICKTIME".
149 #include "wine/port.h"
164 #include "commctrl.h"
165 #include "comctl32.h"
168 #include "wine/debug.h"
169 #include "wine/unicode.h"
171 WINE_DEFAULT_DEBUG_CHANNEL(listview);
173 /* make sure you set this to 0 for production use! */
174 #define DEBUG_RANGES 1
176 typedef struct tagCOLUMN_INFO
178 RECT rcHeader; /* tracks the header's rectangle */
179 INT fmt; /* same as LVCOLUMN.fmt */
183 typedef struct tagITEMHDR
187 } ITEMHDR, *LPITEMHDR;
189 typedef struct tagSUBITEM_INFO
195 typedef struct tagITEM_ID ITEM_ID;
197 typedef struct tagITEM_INFO
208 UINT id; /* item id */
209 HDPA item; /* link to item data */
212 typedef struct tagRANGE
218 typedef struct tagRANGES
223 typedef struct tagITERATOR
232 typedef struct tagDELAYED_ITEM_EDIT
238 typedef struct tagLISTVIEW_INFO
245 HIMAGELIST himlNormal;
246 HIMAGELIST himlSmall;
247 HIMAGELIST himlState;
251 BOOL bMarqueeSelect; /* marquee selection/highlight underway */
252 RECT marqueeRect; /* absolute coordinates of marquee selection */
253 RECT marqueeDrawRect; /* relative coordinates for drawing marquee */
254 POINT marqueeOrigin; /* absolute coordinates of marquee click origin */
255 POINT ptClickPos; /* point where the user clicked */
256 BOOL bNoItemMetrics; /* flags if item metrics are not yet computed */
259 RANGES selectionRanges;
264 RECT rcList; /* This rectangle is really the window
265 * client rectangle possibly reduced by the
266 * horizontal scroll bar and/or header - see
267 * LISTVIEW_UpdateSize. This rectangle offset
268 * by the LISTVIEW_GetOrigin value is in
269 * client coordinates */
278 INT ntmHeight; /* Some cached metrics of the font used */
279 INT ntmMaxCharWidth; /* by the listview to draw items */
281 BOOL bRedraw; /* Turns on/off repaints & invalidations */
282 BOOL bAutoarrange; /* Autoarrange flag when NOT in LVS_AUTOARRANGE */
284 BOOL bDoChangeNotify; /* send change notification messages? */
287 DWORD dwStyle; /* the cached window GWL_STYLE */
288 DWORD dwLvExStyle; /* extended listview style */
289 DWORD uView; /* current view available through LVM_[G,S]ETVIEW */
290 INT nItemCount; /* the number of items in the list */
291 HDPA hdpaItems; /* array ITEM_INFO pointers */
292 HDPA hdpaItemIds; /* array of ITEM_ID pointers */
293 HDPA hdpaPosX; /* maintains the (X, Y) coordinates of the */
294 HDPA hdpaPosY; /* items in LVS_ICON, and LVS_SMALLICON modes */
295 HDPA hdpaColumns; /* array of COLUMN_INFO pointers */
296 BOOL colRectsDirty; /* trigger column rectangles requery from header */
297 POINT currIconPos; /* this is the position next icon will be placed */
298 PFNLVCOMPARE pfnCompare;
303 INT nLButtonDownItem; /* tracks item to reset multiselection on WM_LBUTTONUP */
307 DWORD cditemmode; /* Keep the custom draw flags for an item/row */
309 DWORD lastKeyPressTimestamp;
311 INT nSearchParamLength;
312 WCHAR szSearchParam[ MAX_PATH ];
314 INT nMeasureItemHeight;
315 INT xTrackLine; /* The x coefficient of the track line or -1 if none */
316 DELAYED_ITEM_EDIT itemEdit; /* Pointer to this structure will be the timer ID */
318 DWORD iVersion; /* CCM_[G,S]ETVERSION */
324 /* How many we debug buffer to allocate */
325 #define DEBUG_BUFFERS 20
326 /* The size of a single debug buffer */
327 #define DEBUG_BUFFER_SIZE 256
329 /* Internal interface to LISTVIEW_HScroll and LISTVIEW_VScroll */
330 #define SB_INTERNAL -1
332 /* maximum size of a label */
333 #define DISP_TEXT_SIZE 512
335 /* padding for items in list and small icon display modes */
336 #define WIDTH_PADDING 12
338 /* padding for items in list, report and small icon display modes */
339 #define HEIGHT_PADDING 1
341 /* offset of items in report display mode */
342 #define REPORT_MARGINX 2
344 /* padding for icon in large icon display mode
345 * ICON_TOP_PADDING_NOTHITABLE - space between top of box and area
346 * that HITTEST will see.
347 * ICON_TOP_PADDING_HITABLE - spacing between above and icon.
348 * ICON_TOP_PADDING - sum of the two above.
349 * ICON_BOTTOM_PADDING - between bottom of icon and top of text
350 * LABEL_HOR_PADDING - between text and sides of box
351 * LABEL_VERT_PADDING - between bottom of text and end of box
353 * ICON_LR_PADDING - additional width above icon size.
354 * ICON_LR_HALF - half of the above value
356 #define ICON_TOP_PADDING_NOTHITABLE 2
357 #define ICON_TOP_PADDING_HITABLE 2
358 #define ICON_TOP_PADDING (ICON_TOP_PADDING_NOTHITABLE + ICON_TOP_PADDING_HITABLE)
359 #define ICON_BOTTOM_PADDING 4
360 #define LABEL_HOR_PADDING 5
361 #define LABEL_VERT_PADDING 7
362 #define ICON_LR_PADDING 16
363 #define ICON_LR_HALF (ICON_LR_PADDING/2)
365 /* default label width for items in list and small icon display modes */
366 #define DEFAULT_LABEL_WIDTH 40
367 /* maximum select rectangle width for empty text item in LV_VIEW_DETAILS */
368 #define MAX_EMPTYTEXT_SELECT_WIDTH 80
370 /* default column width for items in list display mode */
371 #define DEFAULT_COLUMN_WIDTH 128
373 /* Size of "line" scroll for V & H scrolls */
374 #define LISTVIEW_SCROLL_ICON_LINE_SIZE 37
376 /* Padding between image and label */
377 #define IMAGE_PADDING 2
379 /* Padding behind the label */
380 #define TRAILING_LABEL_PADDING 12
381 #define TRAILING_HEADER_PADDING 11
383 /* Border for the icon caption */
384 #define CAPTION_BORDER 2
386 /* Standard DrawText flags */
387 #define LV_ML_DT_FLAGS (DT_TOP | DT_NOPREFIX | DT_EDITCONTROL | DT_CENTER | DT_WORDBREAK | DT_WORD_ELLIPSIS | DT_END_ELLIPSIS)
388 #define LV_FL_DT_FLAGS (DT_TOP | DT_NOPREFIX | DT_EDITCONTROL | DT_CENTER | DT_WORDBREAK | DT_NOCLIP)
389 #define LV_SL_DT_FLAGS (DT_VCENTER | DT_NOPREFIX | DT_EDITCONTROL | DT_SINGLELINE | DT_WORD_ELLIPSIS | DT_END_ELLIPSIS)
391 /* Image index from state */
392 #define STATEIMAGEINDEX(x) (((x) & LVIS_STATEIMAGEMASK) >> 12)
394 /* The time in milliseconds to reset the search in the list */
395 #define KEY_DELAY 450
397 /* Dump the LISTVIEW_INFO structure to the debug channel */
398 #define LISTVIEW_DUMP(iP) do { \
399 TRACE("hwndSelf=%p, clrBk=0x%06x, clrText=0x%06x, clrTextBk=0x%06x, ItemHeight=%d, ItemWidth=%d, Style=0x%08x\n", \
400 iP->hwndSelf, iP->clrBk, iP->clrText, iP->clrTextBk, \
401 iP->nItemHeight, iP->nItemWidth, iP->dwStyle); \
402 TRACE("hwndSelf=%p, himlNor=%p, himlSml=%p, himlState=%p, Focused=%d, Hot=%d, exStyle=0x%08x, Focus=%d\n", \
403 iP->hwndSelf, iP->himlNormal, iP->himlSmall, iP->himlState, \
404 iP->nFocusedItem, iP->nHotItem, iP->dwLvExStyle, iP->bFocus ); \
405 TRACE("hwndSelf=%p, ntmH=%d, icSz.cx=%d, icSz.cy=%d, icSp.cx=%d, icSp.cy=%d, notifyFmt=%d\n", \
406 iP->hwndSelf, iP->ntmHeight, iP->iconSize.cx, iP->iconSize.cy, \
407 iP->iconSpacing.cx, iP->iconSpacing.cy, iP->notifyFormat); \
408 TRACE("hwndSelf=%p, rcList=%s\n", iP->hwndSelf, wine_dbgstr_rect(&iP->rcList)); \
411 static const WCHAR themeClass[] = {'L','i','s','t','V','i','e','w',0};
414 * forward declarations
416 static BOOL LISTVIEW_GetItemT(const LISTVIEW_INFO *, LPLVITEMW, BOOL);
417 static void LISTVIEW_GetItemBox(const LISTVIEW_INFO *, INT, LPRECT);
418 static void LISTVIEW_GetItemOrigin(const LISTVIEW_INFO *, INT, LPPOINT);
419 static BOOL LISTVIEW_GetItemPosition(const LISTVIEW_INFO *, INT, LPPOINT);
420 static BOOL LISTVIEW_GetItemRect(const LISTVIEW_INFO *, INT, LPRECT);
421 static INT LISTVIEW_GetLabelWidth(const LISTVIEW_INFO *, INT);
422 static void LISTVIEW_GetOrigin(const LISTVIEW_INFO *, LPPOINT);
423 static BOOL LISTVIEW_GetViewRect(const LISTVIEW_INFO *, LPRECT);
424 static void LISTVIEW_UpdateSize(LISTVIEW_INFO *);
425 static LRESULT LISTVIEW_Command(LISTVIEW_INFO *, WPARAM, LPARAM);
426 static INT LISTVIEW_GetStringWidthT(const LISTVIEW_INFO *, LPCWSTR, BOOL);
427 static BOOL LISTVIEW_KeySelection(LISTVIEW_INFO *, INT, BOOL);
428 static UINT LISTVIEW_GetItemState(const LISTVIEW_INFO *, INT, UINT);
429 static BOOL LISTVIEW_SetItemState(LISTVIEW_INFO *, INT, const LVITEMW *);
430 static LRESULT LISTVIEW_VScroll(LISTVIEW_INFO *, INT, INT, HWND);
431 static LRESULT LISTVIEW_HScroll(LISTVIEW_INFO *, INT, INT, HWND);
432 static BOOL LISTVIEW_EnsureVisible(LISTVIEW_INFO *, INT, BOOL);
433 static HWND CreateEditLabelT(LISTVIEW_INFO *, LPCWSTR, DWORD, BOOL);
434 static HIMAGELIST LISTVIEW_SetImageList(LISTVIEW_INFO *, INT, HIMAGELIST);
435 static INT LISTVIEW_HitTest(const LISTVIEW_INFO *, LPLVHITTESTINFO, BOOL, BOOL);
436 static BOOL LISTVIEW_EndEditLabelT(LISTVIEW_INFO *, BOOL, BOOL);
438 /******** Text handling functions *************************************/
440 /* A text pointer is either NULL, LPSTR_TEXTCALLBACK, or points to a
441 * text string. The string may be ANSI or Unicode, in which case
442 * the boolean isW tells us the type of the string.
444 * The name of the function tell what type of strings it expects:
445 * W: Unicode, T: ANSI/Unicode - function of isW
448 static inline BOOL is_textW(LPCWSTR text)
450 return text != NULL && text != LPSTR_TEXTCALLBACKW;
453 static inline BOOL is_textT(LPCWSTR text, BOOL isW)
455 /* we can ignore isW since LPSTR_TEXTCALLBACKW == LPSTR_TEXTCALLBACKA */
456 return is_textW(text);
459 static inline int textlenT(LPCWSTR text, BOOL isW)
461 return !is_textT(text, isW) ? 0 :
462 isW ? lstrlenW(text) : lstrlenA((LPCSTR)text);
465 static inline void textcpynT(LPWSTR dest, BOOL isDestW, LPCWSTR src, BOOL isSrcW, INT max)
468 if (isSrcW) lstrcpynW(dest, src, max);
469 else MultiByteToWideChar(CP_ACP, 0, (LPCSTR)src, -1, dest, max);
471 if (isSrcW) WideCharToMultiByte(CP_ACP, 0, src, -1, (LPSTR)dest, max, NULL, NULL);
472 else lstrcpynA((LPSTR)dest, (LPCSTR)src, max);
475 static inline LPWSTR textdupTtoW(LPCWSTR text, BOOL isW)
477 LPWSTR wstr = (LPWSTR)text;
479 if (!isW && is_textT(text, isW))
481 INT len = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)text, -1, NULL, 0);
482 wstr = Alloc(len * sizeof(WCHAR));
483 if (wstr) MultiByteToWideChar(CP_ACP, 0, (LPCSTR)text, -1, wstr, len);
485 TRACE(" wstr=%s\n", text == LPSTR_TEXTCALLBACKW ? "(callback)" : debugstr_w(wstr));
489 static inline void textfreeT(LPWSTR wstr, BOOL isW)
491 if (!isW && is_textT(wstr, isW)) Free (wstr);
495 * dest is a pointer to a Unicode string
496 * src is a pointer to a string (Unicode if isW, ANSI if !isW)
498 static BOOL textsetptrT(LPWSTR *dest, LPCWSTR src, BOOL isW)
502 if (src == LPSTR_TEXTCALLBACKW)
504 if (is_textW(*dest)) Free(*dest);
505 *dest = LPSTR_TEXTCALLBACKW;
509 LPWSTR pszText = textdupTtoW(src, isW);
510 if (*dest == LPSTR_TEXTCALLBACKW) *dest = NULL;
511 bResult = Str_SetPtrW(dest, pszText);
512 textfreeT(pszText, isW);
518 * compares a Unicode to a Unicode/ANSI text string
520 static inline int textcmpWT(LPCWSTR aw, LPCWSTR bt, BOOL isW)
522 if (!aw) return bt ? -1 : 0;
523 if (!bt) return aw ? 1 : 0;
524 if (aw == LPSTR_TEXTCALLBACKW)
525 return bt == LPSTR_TEXTCALLBACKW ? 1 : -1;
526 if (bt != LPSTR_TEXTCALLBACKW)
528 LPWSTR bw = textdupTtoW(bt, isW);
529 int r = bw ? lstrcmpW(aw, bw) : 1;
537 static inline int lstrncmpiW(LPCWSTR s1, LPCWSTR s2, int n)
541 n = min(min(n, lstrlenW(s1)), lstrlenW(s2));
542 res = CompareStringW(LOCALE_USER_DEFAULT, NORM_IGNORECASE, s1, n, s2, n);
543 return res ? res - sizeof(WCHAR) : res;
546 /******** Debugging functions *****************************************/
548 static inline LPCSTR debugtext_t(LPCWSTR text, BOOL isW)
550 if (text == LPSTR_TEXTCALLBACKW) return "(callback)";
551 return isW ? debugstr_w(text) : debugstr_a((LPCSTR)text);
554 static inline LPCSTR debugtext_tn(LPCWSTR text, BOOL isW, INT n)
556 if (text == LPSTR_TEXTCALLBACKW) return "(callback)";
557 n = min(textlenT(text, isW), n);
558 return isW ? debugstr_wn(text, n) : debugstr_an((LPCSTR)text, n);
561 static char* debug_getbuf(void)
563 static int index = 0;
564 static char buffers[DEBUG_BUFFERS][DEBUG_BUFFER_SIZE];
565 return buffers[index++ % DEBUG_BUFFERS];
568 static inline const char* debugrange(const RANGE *lprng)
570 if (!lprng) return "(null)";
571 return wine_dbg_sprintf("[%d, %d]", lprng->lower, lprng->upper);
574 static const char* debugscrollinfo(const SCROLLINFO *pScrollInfo)
576 char* buf = debug_getbuf(), *text = buf;
577 int len, size = DEBUG_BUFFER_SIZE;
579 if (pScrollInfo == NULL) return "(null)";
580 len = snprintf(buf, size, "{cbSize=%d, ", pScrollInfo->cbSize);
581 if (len == -1) goto end; buf += len; size -= len;
582 if (pScrollInfo->fMask & SIF_RANGE)
583 len = snprintf(buf, size, "nMin=%d, nMax=%d, ", pScrollInfo->nMin, pScrollInfo->nMax);
585 if (len == -1) goto end; buf += len; size -= len;
586 if (pScrollInfo->fMask & SIF_PAGE)
587 len = snprintf(buf, size, "nPage=%u, ", pScrollInfo->nPage);
589 if (len == -1) goto end; buf += len; size -= len;
590 if (pScrollInfo->fMask & SIF_POS)
591 len = snprintf(buf, size, "nPos=%d, ", pScrollInfo->nPos);
593 if (len == -1) goto end; buf += len; size -= len;
594 if (pScrollInfo->fMask & SIF_TRACKPOS)
595 len = snprintf(buf, size, "nTrackPos=%d, ", pScrollInfo->nTrackPos);
597 if (len == -1) goto end; buf += len; size -= len;
600 buf = text + strlen(text);
602 if (buf - text > 2) { buf[-2] = '}'; buf[-1] = 0; }
606 static const char* debugnmlistview(const NMLISTVIEW *plvnm)
608 if (!plvnm) return "(null)";
609 return wine_dbg_sprintf("iItem=%d, iSubItem=%d, uNewState=0x%x,"
610 " uOldState=0x%x, uChanged=0x%x, ptAction=%s, lParam=%ld",
611 plvnm->iItem, plvnm->iSubItem, plvnm->uNewState, plvnm->uOldState,
612 plvnm->uChanged, wine_dbgstr_point(&plvnm->ptAction), plvnm->lParam);
615 static const char* debuglvitem_t(const LVITEMW *lpLVItem, BOOL isW)
617 char* buf = debug_getbuf(), *text = buf;
618 int len, size = DEBUG_BUFFER_SIZE;
620 if (lpLVItem == NULL) return "(null)";
621 len = snprintf(buf, size, "{iItem=%d, iSubItem=%d, ", lpLVItem->iItem, lpLVItem->iSubItem);
622 if (len == -1) goto end; buf += len; size -= len;
623 if (lpLVItem->mask & LVIF_STATE)
624 len = snprintf(buf, size, "state=%x, stateMask=%x, ", lpLVItem->state, lpLVItem->stateMask);
626 if (len == -1) goto end; buf += len; size -= len;
627 if (lpLVItem->mask & LVIF_TEXT)
628 len = snprintf(buf, size, "pszText=%s, cchTextMax=%d, ", debugtext_tn(lpLVItem->pszText, isW, 80), lpLVItem->cchTextMax);
630 if (len == -1) goto end; buf += len; size -= len;
631 if (lpLVItem->mask & LVIF_IMAGE)
632 len = snprintf(buf, size, "iImage=%d, ", lpLVItem->iImage);
634 if (len == -1) goto end; buf += len; size -= len;
635 if (lpLVItem->mask & LVIF_PARAM)
636 len = snprintf(buf, size, "lParam=%lx, ", lpLVItem->lParam);
638 if (len == -1) goto end; buf += len; size -= len;
639 if (lpLVItem->mask & LVIF_INDENT)
640 len = snprintf(buf, size, "iIndent=%d, ", lpLVItem->iIndent);
642 if (len == -1) goto end; buf += len; size -= len;
645 buf = text + strlen(text);
647 if (buf - text > 2) { buf[-2] = '}'; buf[-1] = 0; }
651 static const char* debuglvcolumn_t(const LVCOLUMNW *lpColumn, BOOL isW)
653 char* buf = debug_getbuf(), *text = buf;
654 int len, size = DEBUG_BUFFER_SIZE;
656 if (lpColumn == NULL) return "(null)";
657 len = snprintf(buf, size, "{");
658 if (len == -1) goto end; buf += len; size -= len;
659 if (lpColumn->mask & LVCF_SUBITEM)
660 len = snprintf(buf, size, "iSubItem=%d, ", lpColumn->iSubItem);
662 if (len == -1) goto end; buf += len; size -= len;
663 if (lpColumn->mask & LVCF_FMT)
664 len = snprintf(buf, size, "fmt=%x, ", lpColumn->fmt);
666 if (len == -1) goto end; buf += len; size -= len;
667 if (lpColumn->mask & LVCF_WIDTH)
668 len = snprintf(buf, size, "cx=%d, ", lpColumn->cx);
670 if (len == -1) goto end; buf += len; size -= len;
671 if (lpColumn->mask & LVCF_TEXT)
672 len = snprintf(buf, size, "pszText=%s, cchTextMax=%d, ", debugtext_tn(lpColumn->pszText, isW, 80), lpColumn->cchTextMax);
674 if (len == -1) goto end; buf += len; size -= len;
675 if (lpColumn->mask & LVCF_IMAGE)
676 len = snprintf(buf, size, "iImage=%d, ", lpColumn->iImage);
678 if (len == -1) goto end; buf += len; size -= len;
679 if (lpColumn->mask & LVCF_ORDER)
680 len = snprintf(buf, size, "iOrder=%d, ", lpColumn->iOrder);
682 if (len == -1) goto end; buf += len; size -= len;
685 buf = text + strlen(text);
687 if (buf - text > 2) { buf[-2] = '}'; buf[-1] = 0; }
691 static const char* debuglvhittestinfo(const LVHITTESTINFO *lpht)
693 if (!lpht) return "(null)";
695 return wine_dbg_sprintf("{pt=%s, flags=0x%x, iItem=%d, iSubItem=%d}",
696 wine_dbgstr_point(&lpht->pt), lpht->flags, lpht->iItem, lpht->iSubItem);
699 /* Return the corresponding text for a given scroll value */
700 static inline LPCSTR debugscrollcode(int nScrollCode)
704 case SB_LINELEFT: return "SB_LINELEFT";
705 case SB_LINERIGHT: return "SB_LINERIGHT";
706 case SB_PAGELEFT: return "SB_PAGELEFT";
707 case SB_PAGERIGHT: return "SB_PAGERIGHT";
708 case SB_THUMBPOSITION: return "SB_THUMBPOSITION";
709 case SB_THUMBTRACK: return "SB_THUMBTRACK";
710 case SB_ENDSCROLL: return "SB_ENDSCROLL";
711 case SB_INTERNAL: return "SB_INTERNAL";
712 default: return "unknown";
717 /******** Notification functions ************************************/
719 static int get_ansi_notification(UINT unicodeNotificationCode)
721 switch (unicodeNotificationCode)
723 case LVN_BEGINLABELEDITW: return LVN_BEGINLABELEDITA;
724 case LVN_ENDLABELEDITW: return LVN_ENDLABELEDITA;
725 case LVN_GETDISPINFOW: return LVN_GETDISPINFOA;
726 case LVN_SETDISPINFOW: return LVN_SETDISPINFOA;
727 case LVN_ODFINDITEMW: return LVN_ODFINDITEMA;
728 case LVN_GETINFOTIPW: return LVN_GETINFOTIPA;
729 /* header forwards */
730 case HDN_TRACKW: return HDN_TRACKA;
731 case HDN_ENDTRACKW: return HDN_ENDTRACKA;
732 case HDN_BEGINDRAG: return HDN_BEGINDRAG;
733 case HDN_ENDDRAG: return HDN_ENDDRAG;
734 case HDN_ITEMCHANGINGW: return HDN_ITEMCHANGINGA;
735 case HDN_ITEMCHANGEDW: return HDN_ITEMCHANGEDA;
736 case HDN_ITEMCLICKW: return HDN_ITEMCLICKA;
737 case HDN_DIVIDERDBLCLICKW: return HDN_DIVIDERDBLCLICKA;
739 ERR("unknown notification %x\n", unicodeNotificationCode);
744 /* forwards header notifications to listview parent */
745 static LRESULT notify_forward_header(const LISTVIEW_INFO *infoPtr, const NMHEADERW *lpnmh)
749 HD_TEXTFILTERA textfilter;
750 LPSTR text = NULL, filter = NULL;
753 /* on unicode format exit earlier */
754 if (infoPtr->notifyFormat == NFR_UNICODE)
755 return SendMessageW(infoPtr->hwndNotify, WM_NOTIFY,
756 (WPARAM)lpnmh->hdr.idFrom, (LPARAM)lpnmh);
758 /* header always supplies unicode notifications,
759 all we have to do is to convert strings to ANSI */
760 nmhA = *(NMHEADERA*)lpnmh;
763 hditema = *(HDITEMA*)lpnmh->pitem;
764 nmhA.pitem = &hditema;
765 /* convert item text */
766 if (lpnmh->pitem->mask & HDI_TEXT)
768 hditema.pszText = NULL;
769 Str_SetPtrWtoA(&hditema.pszText, lpnmh->pitem->pszText);
770 text = hditema.pszText;
772 /* convert filter text */
773 if ((lpnmh->pitem->mask & HDI_FILTER) && (lpnmh->pitem->type == HDFT_ISSTRING) &&
774 lpnmh->pitem->pvFilter)
776 hditema.pvFilter = &textfilter;
777 textfilter = *(HD_TEXTFILTERA*)(lpnmh->pitem->pvFilter);
778 textfilter.pszText = NULL;
779 Str_SetPtrWtoA(&textfilter.pszText, ((HD_TEXTFILTERW*)lpnmh->pitem->pvFilter)->pszText);
780 filter = textfilter.pszText;
783 nmhA.hdr.code = get_ansi_notification(lpnmh->hdr.code);
785 ret = SendMessageW(infoPtr->hwndNotify, WM_NOTIFY,
786 (WPARAM)nmhA.hdr.idFrom, (LPARAM)&nmhA);
795 static LRESULT notify_hdr(const LISTVIEW_INFO *infoPtr, INT code, LPNMHDR pnmh)
799 TRACE("(code=%d)\n", code);
801 pnmh->hwndFrom = infoPtr->hwndSelf;
802 pnmh->idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
804 result = SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, pnmh->idFrom, (LPARAM)pnmh);
806 TRACE(" <= %ld\n", result);
811 static inline BOOL notify(const LISTVIEW_INFO *infoPtr, INT code)
814 HWND hwnd = infoPtr->hwndSelf;
815 notify_hdr(infoPtr, code, &nmh);
816 return IsWindow(hwnd);
819 static inline void notify_itemactivate(const LISTVIEW_INFO *infoPtr, const LVHITTESTINFO *htInfo)
830 item.mask = LVIF_PARAM|LVIF_STATE;
831 item.iItem = htInfo->iItem;
833 if (LISTVIEW_GetItemT(infoPtr, &item, TRUE)) {
834 nmia.lParam = item.lParam;
835 nmia.uOldState = item.state;
836 nmia.uNewState = item.state | LVIS_ACTIVATING;
837 nmia.uChanged = LVIF_STATE;
840 nmia.iItem = htInfo->iItem;
841 nmia.iSubItem = htInfo->iSubItem;
842 nmia.ptAction = htInfo->pt;
844 if (GetKeyState(VK_SHIFT) & 0x8000) nmia.uKeyFlags |= LVKF_SHIFT;
845 if (GetKeyState(VK_CONTROL) & 0x8000) nmia.uKeyFlags |= LVKF_CONTROL;
846 if (GetKeyState(VK_MENU) & 0x8000) nmia.uKeyFlags |= LVKF_ALT;
848 notify_hdr(infoPtr, LVN_ITEMACTIVATE, (LPNMHDR)&nmia);
851 static inline LRESULT notify_listview(const LISTVIEW_INFO *infoPtr, INT code, LPNMLISTVIEW plvnm)
853 TRACE("(code=%d, plvnm=%s)\n", code, debugnmlistview(plvnm));
854 return notify_hdr(infoPtr, code, (LPNMHDR)plvnm);
857 static BOOL notify_click(const LISTVIEW_INFO *infoPtr, INT code, const LVHITTESTINFO *lvht)
861 HWND hwnd = infoPtr->hwndSelf;
863 TRACE("code=%d, lvht=%s\n", code, debuglvhittestinfo(lvht));
864 ZeroMemory(&nmia, sizeof(nmia));
865 nmia.iItem = lvht->iItem;
866 nmia.iSubItem = lvht->iSubItem;
867 nmia.ptAction = lvht->pt;
868 item.mask = LVIF_PARAM;
869 item.iItem = lvht->iItem;
871 if (LISTVIEW_GetItemT(infoPtr, &item, TRUE)) nmia.lParam = item.lParam;
872 notify_hdr(infoPtr, code, (LPNMHDR)&nmia);
873 return IsWindow(hwnd);
876 static BOOL notify_deleteitem(const LISTVIEW_INFO *infoPtr, INT nItem)
880 HWND hwnd = infoPtr->hwndSelf;
882 ZeroMemory(&nmlv, sizeof (NMLISTVIEW));
884 item.mask = LVIF_PARAM;
887 if (LISTVIEW_GetItemT(infoPtr, &item, TRUE)) nmlv.lParam = item.lParam;
888 notify_listview(infoPtr, LVN_DELETEITEM, &nmlv);
889 return IsWindow(hwnd);
893 Send notification. depends on dispinfoW having same
894 structure as dispinfoA.
895 infoPtr : listview struct
896 notificationCode : *Unicode* notification code
897 pdi : dispinfo structure (can be unicode or ansi)
898 isW : TRUE if dispinfo is Unicode
900 static BOOL notify_dispinfoT(const LISTVIEW_INFO *infoPtr, UINT notificationCode, LPNMLVDISPINFOW pdi, BOOL isW)
902 BOOL bResult = FALSE;
903 BOOL convertToAnsi = FALSE, convertToUnicode = FALSE;
904 INT cchTempBufMax = 0, savCchTextMax = 0;
906 LPWSTR pszTempBuf = NULL, savPszText = NULL;
908 if ((pdi->item.mask & LVIF_TEXT) && is_textT(pdi->item.pszText, isW))
910 convertToAnsi = (isW && infoPtr->notifyFormat == NFR_ANSI);
911 convertToUnicode = (!isW && infoPtr->notifyFormat == NFR_UNICODE);
914 if (convertToAnsi || convertToUnicode)
916 if (notificationCode != LVN_GETDISPINFOW)
918 cchTempBufMax = convertToUnicode ?
919 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pdi->item.pszText, -1, NULL, 0):
920 WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, NULL, 0, NULL, NULL);
924 cchTempBufMax = pdi->item.cchTextMax;
925 *pdi->item.pszText = 0; /* make sure we don't process garbage */
928 pszTempBuf = Alloc( (convertToUnicode ? sizeof(WCHAR) : sizeof(CHAR)) * cchTempBufMax);
929 if (!pszTempBuf) return FALSE;
931 if (convertToUnicode)
932 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pdi->item.pszText, -1,
933 pszTempBuf, cchTempBufMax);
935 WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, (LPSTR) pszTempBuf,
936 cchTempBufMax, NULL, NULL);
938 savCchTextMax = pdi->item.cchTextMax;
939 savPszText = pdi->item.pszText;
940 pdi->item.pszText = pszTempBuf;
941 pdi->item.cchTextMax = cchTempBufMax;
944 if (infoPtr->notifyFormat == NFR_ANSI)
945 realNotifCode = get_ansi_notification(notificationCode);
947 realNotifCode = notificationCode;
948 TRACE(" pdi->item=%s\n", debuglvitem_t(&pdi->item, infoPtr->notifyFormat != NFR_ANSI));
949 bResult = notify_hdr(infoPtr, realNotifCode, &pdi->hdr);
951 if (convertToUnicode || convertToAnsi)
953 if (convertToUnicode) /* note : pointer can be changed by app ! */
954 WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, (LPSTR) savPszText,
955 savCchTextMax, NULL, NULL);
957 MultiByteToWideChar(CP_ACP, 0, (LPSTR) pdi->item.pszText, -1,
958 savPszText, savCchTextMax);
959 pdi->item.pszText = savPszText; /* restores our buffer */
960 pdi->item.cchTextMax = savCchTextMax;
966 static void customdraw_fill(NMLVCUSTOMDRAW *lpnmlvcd, const LISTVIEW_INFO *infoPtr, HDC hdc,
967 const RECT *rcBounds, const LVITEMW *lplvItem)
969 ZeroMemory(lpnmlvcd, sizeof(NMLVCUSTOMDRAW));
970 lpnmlvcd->nmcd.hdc = hdc;
971 lpnmlvcd->nmcd.rc = *rcBounds;
972 lpnmlvcd->clrTextBk = infoPtr->clrTextBk;
973 lpnmlvcd->clrText = infoPtr->clrText;
974 if (!lplvItem) return;
975 lpnmlvcd->nmcd.dwItemSpec = lplvItem->iItem + 1;
976 lpnmlvcd->iSubItem = lplvItem->iSubItem;
977 if (lplvItem->state & LVIS_SELECTED) lpnmlvcd->nmcd.uItemState |= CDIS_SELECTED;
978 if (lplvItem->state & LVIS_FOCUSED) lpnmlvcd->nmcd.uItemState |= CDIS_FOCUS;
979 if (lplvItem->iItem == infoPtr->nHotItem) lpnmlvcd->nmcd.uItemState |= CDIS_HOT;
980 lpnmlvcd->nmcd.lItemlParam = lplvItem->lParam;
983 static inline DWORD notify_customdraw (const LISTVIEW_INFO *infoPtr, DWORD dwDrawStage, NMLVCUSTOMDRAW *lpnmlvcd)
985 BOOL isForItem = (lpnmlvcd->nmcd.dwItemSpec != 0);
988 lpnmlvcd->nmcd.dwDrawStage = dwDrawStage;
989 if (isForItem) lpnmlvcd->nmcd.dwDrawStage |= CDDS_ITEM;
990 if (lpnmlvcd->iSubItem) lpnmlvcd->nmcd.dwDrawStage |= CDDS_SUBITEM;
991 if (isForItem) lpnmlvcd->nmcd.dwItemSpec--;
992 result = notify_hdr(infoPtr, NM_CUSTOMDRAW, &lpnmlvcd->nmcd.hdr);
993 if (isForItem) lpnmlvcd->nmcd.dwItemSpec++;
997 static void prepaint_setup (const LISTVIEW_INFO *infoPtr, HDC hdc, NMLVCUSTOMDRAW *lpnmlvcd, BOOL SubItem)
999 if (lpnmlvcd->clrTextBk == CLR_DEFAULT)
1000 lpnmlvcd->clrTextBk = comctl32_color.clrWindow;
1001 if (lpnmlvcd->clrText == CLR_DEFAULT)
1002 lpnmlvcd->clrText = comctl32_color.clrWindowText;
1004 /* apparently, for selected items, we have to override the returned values */
1007 if (lpnmlvcd->nmcd.uItemState & CDIS_SELECTED)
1009 if (infoPtr->bFocus)
1011 lpnmlvcd->clrTextBk = comctl32_color.clrHighlight;
1012 lpnmlvcd->clrText = comctl32_color.clrHighlightText;
1014 else if (infoPtr->dwStyle & LVS_SHOWSELALWAYS)
1016 lpnmlvcd->clrTextBk = comctl32_color.clr3dFace;
1017 lpnmlvcd->clrText = comctl32_color.clrBtnText;
1022 /* Set the text attributes */
1023 if (lpnmlvcd->clrTextBk != CLR_NONE)
1025 SetBkMode(hdc, OPAQUE);
1026 SetBkColor(hdc,lpnmlvcd->clrTextBk);
1029 SetBkMode(hdc, TRANSPARENT);
1030 SetTextColor(hdc, lpnmlvcd->clrText);
1033 static inline DWORD notify_postpaint (const LISTVIEW_INFO *infoPtr, NMLVCUSTOMDRAW *lpnmlvcd)
1035 return notify_customdraw(infoPtr, CDDS_POSTPAINT, lpnmlvcd);
1038 /* returns TRUE when repaint needed, FALSE otherwise */
1039 static BOOL notify_measureitem(LISTVIEW_INFO *infoPtr)
1041 MEASUREITEMSTRUCT mis;
1042 mis.CtlType = ODT_LISTVIEW;
1043 mis.CtlID = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1047 mis.itemHeight= infoPtr->nItemHeight;
1048 SendMessageW(infoPtr->hwndNotify, WM_MEASUREITEM, mis.CtlID, (LPARAM)&mis);
1049 if (infoPtr->nItemHeight != max(mis.itemHeight, 1))
1051 infoPtr->nMeasureItemHeight = infoPtr->nItemHeight = max(mis.itemHeight, 1);
1057 /******** Item iterator functions **********************************/
1059 static RANGES ranges_create(int count);
1060 static void ranges_destroy(RANGES ranges);
1061 static BOOL ranges_add(RANGES ranges, RANGE range);
1062 static BOOL ranges_del(RANGES ranges, RANGE range);
1063 static void ranges_dump(RANGES ranges);
1065 static inline BOOL ranges_additem(RANGES ranges, INT nItem)
1067 RANGE range = { nItem, nItem + 1 };
1069 return ranges_add(ranges, range);
1072 static inline BOOL ranges_delitem(RANGES ranges, INT nItem)
1074 RANGE range = { nItem, nItem + 1 };
1076 return ranges_del(ranges, range);
1080 * ITERATOR DOCUMENTATION
1082 * The iterator functions allow for easy, and convenient iteration
1083 * over items of interest in the list. Typically, you create a
1084 * iterator, use it, and destroy it, as such:
1087 * iterator_xxxitems(&i, ...);
1088 * while (iterator_{prev,next}(&i)
1090 * //code which uses i.nItem
1092 * iterator_destroy(&i);
1094 * where xxx is either: framed, or visible.
1095 * Note that it is important that the code destroys the iterator
1096 * after it's done with it, as the creation of the iterator may
1097 * allocate memory, which thus needs to be freed.
1099 * You can iterate both forwards, and backwards through the list,
1100 * by using iterator_next or iterator_prev respectively.
1102 * Lower numbered items are draw on top of higher number items in
1103 * LVS_ICON, and LVS_SMALLICON (which are the only modes where
1104 * items may overlap). So, to test items, you should use
1106 * which lists the items top to bottom (in Z-order).
1107 * For drawing items, you should use
1109 * which lists the items bottom to top (in Z-order).
1110 * If you keep iterating over the items after the end-of-items
1111 * marker (-1) is returned, the iterator will start from the
1112 * beginning. Typically, you don't need to test for -1,
1113 * because iterator_{next,prev} will return TRUE if more items
1114 * are to be iterated over, or FALSE otherwise.
1116 * Note: the iterator is defined to be bidirectional. That is,
1117 * any number of prev followed by any number of next, or
1118 * five versa, should leave the iterator at the same item:
1119 * prev * n, next * n = next * n, prev * n
1121 * The iterator has a notion of an out-of-order, special item,
1122 * which sits at the start of the list. This is used in
1123 * LVS_ICON, and LVS_SMALLICON mode to handle the focused item,
1124 * which needs to be first, as it may overlap other items.
1126 * The code is a bit messy because we have:
1127 * - a special item to deal with
1128 * - simple range, or composite range
1130 * If you find bugs, or want to add features, please make sure you
1131 * always check/modify *both* iterator_prev, and iterator_next.
1135 * This function iterates through the items in increasing order,
1136 * but prefixed by the special item, then -1. That is:
1137 * special, 1, 2, 3, ..., n, -1.
1138 * Each item is listed only once.
1140 static inline BOOL iterator_next(ITERATOR* i)
1144 i->nItem = i->nSpecial;
1145 if (i->nItem != -1) return TRUE;
1147 if (i->nItem == i->nSpecial)
1149 if (i->ranges) i->index = 0;
1155 if (i->nItem == i->nSpecial) i->nItem++;
1156 if (i->nItem < i->range.upper) return TRUE;
1161 if (i->index < DPA_GetPtrCount(i->ranges->hdpa))
1162 i->range = *(RANGE*)DPA_GetPtr(i->ranges->hdpa, i->index++);
1165 else if (i->nItem >= i->range.upper) goto end;
1167 i->nItem = i->range.lower;
1168 if (i->nItem >= 0) goto testitem;
1175 * This function iterates through the items in decreasing order,
1176 * followed by the special item, then -1. That is:
1177 * n, n-1, ..., 3, 2, 1, special, -1.
1178 * Each item is listed only once.
1180 static inline BOOL iterator_prev(ITERATOR* i)
1187 if (i->ranges) i->index = DPA_GetPtrCount(i->ranges->hdpa);
1190 if (i->nItem == i->nSpecial)
1198 if (i->nItem == i->nSpecial) i->nItem--;
1199 if (i->nItem >= i->range.lower) return TRUE;
1205 i->range = *(RANGE*)DPA_GetPtr(i->ranges->hdpa, --i->index);
1208 else if (!start && i->nItem < i->range.lower) goto end;
1210 i->nItem = i->range.upper;
1211 if (i->nItem > 0) goto testitem;
1213 return (i->nItem = i->nSpecial) != -1;
1216 static RANGE iterator_range(const ITERATOR *i)
1220 if (!i->ranges) return i->range;
1222 if (DPA_GetPtrCount(i->ranges->hdpa) > 0)
1224 range.lower = (*(RANGE*)DPA_GetPtr(i->ranges->hdpa, 0)).lower;
1225 range.upper = (*(RANGE*)DPA_GetPtr(i->ranges->hdpa, DPA_GetPtrCount(i->ranges->hdpa) - 1)).upper;
1227 else range.lower = range.upper = 0;
1233 * Releases resources associated with this ierator.
1235 static inline void iterator_destroy(const ITERATOR *i)
1237 ranges_destroy(i->ranges);
1241 * Create an empty iterator.
1243 static inline BOOL iterator_empty(ITERATOR* i)
1245 ZeroMemory(i, sizeof(*i));
1246 i->nItem = i->nSpecial = i->range.lower = i->range.upper = -1;
1251 * Create an iterator over a range.
1253 static inline BOOL iterator_rangeitems(ITERATOR* i, RANGE range)
1261 * Create an iterator over a bunch of ranges.
1262 * Please note that the iterator will take ownership of the ranges,
1263 * and will free them upon destruction.
1265 static inline BOOL iterator_rangesitems(ITERATOR* i, RANGES ranges)
1273 * Creates an iterator over the items which intersect frame.
1274 * Uses absolute coordinates rather than compensating for the current offset.
1276 static BOOL iterator_frameditems_absolute(ITERATOR* i, const LISTVIEW_INFO* infoPtr, const RECT *frame)
1278 RECT rcItem, rcTemp;
1280 /* in case we fail, we want to return an empty iterator */
1281 if (!iterator_empty(i)) return FALSE;
1283 TRACE("(frame=%s)\n", wine_dbgstr_rect(frame));
1285 if (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON)
1289 if (infoPtr->uView == LV_VIEW_ICON && infoPtr->nFocusedItem != -1)
1291 LISTVIEW_GetItemBox(infoPtr, infoPtr->nFocusedItem, &rcItem);
1292 if (IntersectRect(&rcTemp, &rcItem, frame))
1293 i->nSpecial = infoPtr->nFocusedItem;
1295 if (!(iterator_rangesitems(i, ranges_create(50)))) return FALSE;
1296 /* to do better here, we need to have PosX, and PosY sorted */
1297 TRACE("building icon ranges:\n");
1298 for (nItem = 0; nItem < infoPtr->nItemCount; nItem++)
1300 rcItem.left = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosX, nItem);
1301 rcItem.top = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosY, nItem);
1302 rcItem.right = rcItem.left + infoPtr->nItemWidth;
1303 rcItem.bottom = rcItem.top + infoPtr->nItemHeight;
1304 if (IntersectRect(&rcTemp, &rcItem, frame))
1305 ranges_additem(i->ranges, nItem);
1309 else if (infoPtr->uView == LV_VIEW_DETAILS)
1313 if (frame->left >= infoPtr->nItemWidth) return TRUE;
1314 if (frame->top >= infoPtr->nItemHeight * infoPtr->nItemCount) return TRUE;
1316 range.lower = max(frame->top / infoPtr->nItemHeight, 0);
1317 range.upper = min((frame->bottom - 1) / infoPtr->nItemHeight, infoPtr->nItemCount - 1) + 1;
1318 if (range.upper <= range.lower) return TRUE;
1319 if (!iterator_rangeitems(i, range)) return FALSE;
1320 TRACE(" report=%s\n", debugrange(&i->range));
1324 INT nPerCol = max((infoPtr->rcList.bottom - infoPtr->rcList.top) / infoPtr->nItemHeight, 1);
1325 INT nFirstRow = max(frame->top / infoPtr->nItemHeight, 0);
1326 INT nLastRow = min((frame->bottom - 1) / infoPtr->nItemHeight, nPerCol - 1);
1333 if (infoPtr->nItemWidth)
1335 nFirstCol = max(frame->left / infoPtr->nItemWidth, 0);
1336 nLastCol = min((frame->right - 1) / infoPtr->nItemWidth, (infoPtr->nItemCount + nPerCol - 1) / nPerCol);
1340 nFirstCol = max(frame->left, 0);
1341 nLastCol = min(frame->right - 1, (infoPtr->nItemCount + nPerCol - 1) / nPerCol);
1344 lower = nFirstCol * nPerCol + nFirstRow;
1346 TRACE("nPerCol=%d, nFirstRow=%d, nLastRow=%d, nFirstCol=%d, nLastCol=%d, lower=%d\n",
1347 nPerCol, nFirstRow, nLastRow, nFirstCol, nLastCol, lower);
1349 if (nLastCol < nFirstCol || nLastRow < nFirstRow) return TRUE;
1351 if (!(iterator_rangesitems(i, ranges_create(nLastCol - nFirstCol + 1)))) return FALSE;
1352 TRACE("building list ranges:\n");
1353 for (nCol = nFirstCol; nCol <= nLastCol; nCol++)
1355 item_range.lower = nCol * nPerCol + nFirstRow;
1356 if(item_range.lower >= infoPtr->nItemCount) break;
1357 item_range.upper = min(nCol * nPerCol + nLastRow + 1, infoPtr->nItemCount);
1358 TRACE(" list=%s\n", debugrange(&item_range));
1359 ranges_add(i->ranges, item_range);
1367 * Creates an iterator over the items which intersect lprc.
1369 static BOOL iterator_frameditems(ITERATOR* i, const LISTVIEW_INFO* infoPtr, const RECT *lprc)
1374 TRACE("(lprc=%s)\n", wine_dbgstr_rect(lprc));
1376 LISTVIEW_GetOrigin(infoPtr, &Origin);
1377 OffsetRect(&frame, -Origin.x, -Origin.y);
1379 return iterator_frameditems_absolute(i, infoPtr, &frame);
1383 * Creates an iterator over the items which intersect the visible region of hdc.
1385 static BOOL iterator_visibleitems(ITERATOR *i, const LISTVIEW_INFO *infoPtr, HDC hdc)
1387 POINT Origin, Position;
1388 RECT rcItem, rcClip;
1391 rgntype = GetClipBox(hdc, &rcClip);
1392 if (rgntype == NULLREGION) return iterator_empty(i);
1393 if (!iterator_frameditems(i, infoPtr, &rcClip)) return FALSE;
1394 if (rgntype == SIMPLEREGION) return TRUE;
1396 /* first deal with the special item */
1397 if (i->nSpecial != -1)
1399 LISTVIEW_GetItemBox(infoPtr, i->nSpecial, &rcItem);
1400 if (!RectVisible(hdc, &rcItem)) i->nSpecial = -1;
1403 /* if we can't deal with the region, we'll just go with the simple range */
1404 LISTVIEW_GetOrigin(infoPtr, &Origin);
1405 TRACE("building visible range:\n");
1406 if (!i->ranges && i->range.lower < i->range.upper)
1408 if (!(i->ranges = ranges_create(50))) return TRUE;
1409 if (!ranges_add(i->ranges, i->range))
1411 ranges_destroy(i->ranges);
1417 /* now delete the invisible items from the list */
1418 while(iterator_next(i))
1420 LISTVIEW_GetItemOrigin(infoPtr, i->nItem, &Position);
1421 rcItem.left = (infoPtr->uView == LV_VIEW_DETAILS) ? Origin.x : Position.x + Origin.x;
1422 rcItem.top = Position.y + Origin.y;
1423 rcItem.right = rcItem.left + infoPtr->nItemWidth;
1424 rcItem.bottom = rcItem.top + infoPtr->nItemHeight;
1425 if (!RectVisible(hdc, &rcItem))
1426 ranges_delitem(i->ranges, i->nItem);
1428 /* the iterator should restart on the next iterator_next */
1434 /******** Misc helper functions ************************************/
1436 static inline LRESULT CallWindowProcT(WNDPROC proc, HWND hwnd, UINT uMsg,
1437 WPARAM wParam, LPARAM lParam, BOOL isW)
1439 if (isW) return CallWindowProcW(proc, hwnd, uMsg, wParam, lParam);
1440 else return CallWindowProcA(proc, hwnd, uMsg, wParam, lParam);
1443 static inline BOOL is_autoarrange(const LISTVIEW_INFO *infoPtr)
1445 return ((infoPtr->dwStyle & LVS_AUTOARRANGE) || infoPtr->bAutoarrange) &&
1446 (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON);
1449 static void toggle_checkbox_state(LISTVIEW_INFO *infoPtr, INT nItem)
1451 DWORD state = STATEIMAGEINDEX(LISTVIEW_GetItemState(infoPtr, nItem, LVIS_STATEIMAGEMASK));
1452 if(state == 1 || state == 2)
1456 lvitem.state = INDEXTOSTATEIMAGEMASK(state);
1457 lvitem.stateMask = LVIS_STATEIMAGEMASK;
1458 LISTVIEW_SetItemState(infoPtr, nItem, &lvitem);
1462 /* this should be called after window style got updated,
1463 it used to reset view state to match current window style */
1464 static inline void map_style_view(LISTVIEW_INFO *infoPtr)
1466 switch (infoPtr->dwStyle & LVS_TYPEMASK)
1469 infoPtr->uView = LV_VIEW_ICON;
1472 infoPtr->uView = LV_VIEW_DETAILS;
1475 infoPtr->uView = LV_VIEW_SMALLICON;
1478 infoPtr->uView = LV_VIEW_LIST;
1482 /* computes next item id value */
1483 static DWORD get_next_itemid(const LISTVIEW_INFO *infoPtr)
1485 INT count = DPA_GetPtrCount(infoPtr->hdpaItemIds);
1489 ITEM_ID *lpID = DPA_GetPtr(infoPtr->hdpaItemIds, count - 1);
1490 return lpID->id + 1;
1495 /******** Internal API functions ************************************/
1497 static inline COLUMN_INFO * LISTVIEW_GetColumnInfo(const LISTVIEW_INFO *infoPtr, INT nSubItem)
1499 static COLUMN_INFO mainItem;
1501 if (nSubItem == 0 && DPA_GetPtrCount(infoPtr->hdpaColumns) == 0) return &mainItem;
1502 assert (nSubItem >= 0 && nSubItem < DPA_GetPtrCount(infoPtr->hdpaColumns));
1504 /* update cached column rectangles */
1505 if (infoPtr->colRectsDirty)
1508 LISTVIEW_INFO *Ptr = (LISTVIEW_INFO*)infoPtr;
1511 for (i = 0; i < DPA_GetPtrCount(infoPtr->hdpaColumns); i++) {
1512 info = DPA_GetPtr(infoPtr->hdpaColumns, i);
1513 SendMessageW(infoPtr->hwndHeader, HDM_GETITEMRECT, i, (LPARAM)&info->rcHeader);
1515 Ptr->colRectsDirty = FALSE;
1518 return DPA_GetPtr(infoPtr->hdpaColumns, nSubItem);
1521 static INT LISTVIEW_CreateHeader(LISTVIEW_INFO *infoPtr)
1523 DWORD dFlags = WS_CHILD | HDS_HORZ | HDS_FULLDRAG | HDS_DRAGDROP;
1526 if (infoPtr->hwndHeader) return 0;
1528 TRACE("Creating header for list %p\n", infoPtr->hwndSelf);
1530 /* setup creation flags */
1531 dFlags |= (LVS_NOSORTHEADER & infoPtr->dwStyle) ? 0 : HDS_BUTTONS;
1532 dFlags |= (LVS_NOCOLUMNHEADER & infoPtr->dwStyle) ? HDS_HIDDEN : 0;
1534 hInst = (HINSTANCE)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_HINSTANCE);
1537 infoPtr->hwndHeader = CreateWindowW(WC_HEADERW, NULL, dFlags,
1538 0, 0, 0, 0, infoPtr->hwndSelf, NULL, hInst, NULL);
1539 if (!infoPtr->hwndHeader) return -1;
1541 /* set header unicode format */
1542 SendMessageW(infoPtr->hwndHeader, HDM_SETUNICODEFORMAT, TRUE, 0);
1544 /* set header font */
1545 SendMessageW(infoPtr->hwndHeader, WM_SETFONT, (WPARAM)infoPtr->hFont, (LPARAM)TRUE);
1547 LISTVIEW_UpdateSize(infoPtr);
1552 static inline void LISTVIEW_GetHeaderRect(const LISTVIEW_INFO *infoPtr, INT nSubItem, LPRECT lprc)
1554 *lprc = LISTVIEW_GetColumnInfo(infoPtr, nSubItem)->rcHeader;
1557 static inline BOOL LISTVIEW_GetItemW(const LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem)
1559 return LISTVIEW_GetItemT(infoPtr, lpLVItem, TRUE);
1562 /* used to handle collapse main item column case */
1563 static inline BOOL LISTVIEW_DrawFocusRect(const LISTVIEW_INFO *infoPtr, HDC hdc)
1565 return (infoPtr->rcFocus.left < infoPtr->rcFocus.right) ?
1566 DrawFocusRect(hdc, &infoPtr->rcFocus) : FALSE;
1569 /* Listview invalidation functions: use _only_ these functions to invalidate */
1571 static inline BOOL is_redrawing(const LISTVIEW_INFO *infoPtr)
1573 return infoPtr->bRedraw;
1576 static inline void LISTVIEW_InvalidateRect(const LISTVIEW_INFO *infoPtr, const RECT* rect)
1578 if(!is_redrawing(infoPtr)) return;
1579 TRACE(" invalidating rect=%s\n", wine_dbgstr_rect(rect));
1580 InvalidateRect(infoPtr->hwndSelf, rect, TRUE);
1583 static inline void LISTVIEW_InvalidateItem(const LISTVIEW_INFO *infoPtr, INT nItem)
1587 if(!is_redrawing(infoPtr)) return;
1588 LISTVIEW_GetItemBox(infoPtr, nItem, &rcBox);
1589 LISTVIEW_InvalidateRect(infoPtr, &rcBox);
1592 static inline void LISTVIEW_InvalidateSubItem(const LISTVIEW_INFO *infoPtr, INT nItem, INT nSubItem)
1594 POINT Origin, Position;
1597 if(!is_redrawing(infoPtr)) return;
1598 assert (infoPtr->uView == LV_VIEW_DETAILS);
1599 LISTVIEW_GetOrigin(infoPtr, &Origin);
1600 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
1601 LISTVIEW_GetHeaderRect(infoPtr, nSubItem, &rcBox);
1603 rcBox.bottom = infoPtr->nItemHeight;
1604 OffsetRect(&rcBox, Origin.x + Position.x, Origin.y + Position.y);
1605 LISTVIEW_InvalidateRect(infoPtr, &rcBox);
1608 static inline void LISTVIEW_InvalidateList(const LISTVIEW_INFO *infoPtr)
1610 LISTVIEW_InvalidateRect(infoPtr, NULL);
1613 static inline void LISTVIEW_InvalidateColumn(const LISTVIEW_INFO *infoPtr, INT nColumn)
1617 if(!is_redrawing(infoPtr)) return;
1618 LISTVIEW_GetHeaderRect(infoPtr, nColumn, &rcCol);
1619 rcCol.top = infoPtr->rcList.top;
1620 rcCol.bottom = infoPtr->rcList.bottom;
1621 LISTVIEW_InvalidateRect(infoPtr, &rcCol);
1626 * Retrieves the number of items that can fit vertically in the client area.
1629 * [I] infoPtr : valid pointer to the listview structure
1632 * Number of items per row.
1634 static inline INT LISTVIEW_GetCountPerRow(const LISTVIEW_INFO *infoPtr)
1636 INT nListWidth = infoPtr->rcList.right - infoPtr->rcList.left;
1638 return max(nListWidth/(infoPtr->nItemWidth ? infoPtr->nItemWidth : 1), 1);
1643 * Retrieves the number of items that can fit horizontally in the client
1647 * [I] infoPtr : valid pointer to the listview structure
1650 * Number of items per column.
1652 static inline INT LISTVIEW_GetCountPerColumn(const LISTVIEW_INFO *infoPtr)
1654 INT nListHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
1656 return max(nListHeight / infoPtr->nItemHeight, 1);
1660 /*************************************************************************
1661 * LISTVIEW_ProcessLetterKeys
1663 * Processes keyboard messages generated by pressing the letter keys
1665 * What this does is perform a case insensitive search from the
1666 * current position with the following quirks:
1667 * - If two chars or more are pressed in quick succession we search
1668 * for the corresponding string (e.g. 'abc').
1669 * - If there is a delay we wipe away the current search string and
1670 * restart with just that char.
1671 * - If the user keeps pressing the same character, whether slowly or
1672 * fast, so that the search string is entirely composed of this
1673 * character ('aaaaa' for instance), then we search for first item
1674 * that starting with that character.
1675 * - If the user types the above character in quick succession, then
1676 * we must also search for the corresponding string ('aaaaa'), and
1677 * go to that string if there is a match.
1680 * [I] hwnd : handle to the window
1681 * [I] charCode : the character code, the actual character
1682 * [I] keyData : key data
1690 * - The current implementation has a list of characters it will
1691 * accept and it ignores everything else. In particular it will
1692 * ignore accentuated characters which seems to match what
1693 * Windows does. But I'm not sure it makes sense to follow
1695 * - We don't sound a beep when the search fails.
1699 * TREEVIEW_ProcessLetterKeys
1701 static INT LISTVIEW_ProcessLetterKeys(LISTVIEW_INFO *infoPtr, WPARAM charCode, LPARAM keyData)
1706 WCHAR buffer[MAX_PATH];
1707 DWORD lastKeyPressTimestamp = infoPtr->lastKeyPressTimestamp;
1709 /* simple parameter checking */
1710 if (!charCode || !keyData) return 0;
1712 /* only allow the valid WM_CHARs through */
1713 if (!isalnumW(charCode) &&
1714 charCode != '.' && charCode != '`' && charCode != '!' &&
1715 charCode != '@' && charCode != '#' && charCode != '$' &&
1716 charCode != '%' && charCode != '^' && charCode != '&' &&
1717 charCode != '*' && charCode != '(' && charCode != ')' &&
1718 charCode != '-' && charCode != '_' && charCode != '+' &&
1719 charCode != '=' && charCode != '\\'&& charCode != ']' &&
1720 charCode != '}' && charCode != '[' && charCode != '{' &&
1721 charCode != '/' && charCode != '?' && charCode != '>' &&
1722 charCode != '<' && charCode != ',' && charCode != '~')
1725 /* if there's one item or less, there is no where to go */
1726 if (infoPtr->nItemCount <= 1) return 0;
1728 /* update the search parameters */
1729 infoPtr->lastKeyPressTimestamp = GetTickCount();
1730 if (infoPtr->lastKeyPressTimestamp - lastKeyPressTimestamp < KEY_DELAY) {
1731 if (infoPtr->nSearchParamLength < MAX_PATH-1)
1732 infoPtr->szSearchParam[infoPtr->nSearchParamLength++]=charCode;
1733 if (infoPtr->charCode != charCode)
1734 infoPtr->charCode = charCode = 0;
1736 infoPtr->charCode=charCode;
1737 infoPtr->szSearchParam[0]=charCode;
1738 infoPtr->nSearchParamLength=1;
1739 /* Redundant with the 1 char string */
1743 /* and search from the current position */
1745 if (infoPtr->nFocusedItem >= 0) {
1746 endidx=infoPtr->nFocusedItem;
1748 /* if looking for single character match,
1749 * then we must always move forward
1751 if (infoPtr->nSearchParamLength == 1)
1754 endidx=infoPtr->nItemCount;
1758 /* Let application handle this for virtual listview */
1759 if (infoPtr->dwStyle & LVS_OWNERDATA)
1764 ZeroMemory(&lvfi, sizeof(lvfi));
1765 lvfi.flags = (LVFI_WRAP | LVFI_PARTIAL);
1766 infoPtr->szSearchParam[infoPtr->nSearchParamLength] = '\0';
1767 lvfi.psz = infoPtr->szSearchParam;
1771 nItem = notify_hdr(infoPtr, LVN_ODFINDITEMW, (LPNMHDR)&nmlv.hdr);
1774 LISTVIEW_KeySelection(infoPtr, nItem, FALSE);
1780 if (idx == infoPtr->nItemCount) {
1781 if (endidx == infoPtr->nItemCount || endidx == 0)
1787 item.mask = LVIF_TEXT;
1790 item.pszText = buffer;
1791 item.cchTextMax = MAX_PATH;
1792 if (!LISTVIEW_GetItemW(infoPtr, &item)) return 0;
1794 /* check for a match */
1795 if (lstrncmpiW(item.pszText,infoPtr->szSearchParam,infoPtr->nSearchParamLength) == 0) {
1798 } else if ( (charCode != 0) && (nItem == -1) && (nItem != infoPtr->nFocusedItem) &&
1799 (lstrncmpiW(item.pszText,infoPtr->szSearchParam,1) == 0) ) {
1800 /* This would work but we must keep looking for a longer match */
1804 } while (idx != endidx);
1807 LISTVIEW_KeySelection(infoPtr, nItem, FALSE);
1812 /*************************************************************************
1813 * LISTVIEW_UpdateHeaderSize [Internal]
1815 * Function to resize the header control
1818 * [I] hwnd : handle to a window
1819 * [I] nNewScrollPos : scroll pos to set
1824 static void LISTVIEW_UpdateHeaderSize(const LISTVIEW_INFO *infoPtr, INT nNewScrollPos)
1829 TRACE("nNewScrollPos=%d\n", nNewScrollPos);
1831 if (!infoPtr->hwndHeader) return;
1833 GetWindowRect(infoPtr->hwndHeader, &winRect);
1834 point[0].x = winRect.left;
1835 point[0].y = winRect.top;
1836 point[1].x = winRect.right;
1837 point[1].y = winRect.bottom;
1839 MapWindowPoints(HWND_DESKTOP, infoPtr->hwndSelf, point, 2);
1840 point[0].x = -nNewScrollPos;
1841 point[1].x += nNewScrollPos;
1843 SetWindowPos(infoPtr->hwndHeader,0,
1844 point[0].x,point[0].y,point[1].x,point[1].y,
1845 (infoPtr->dwStyle & LVS_NOCOLUMNHEADER) ? SWP_HIDEWINDOW : SWP_SHOWWINDOW |
1846 SWP_NOZORDER | SWP_NOACTIVATE);
1851 * Update the scrollbars. This functions should be called whenever
1852 * the content, size or view changes.
1855 * [I] infoPtr : valid pointer to the listview structure
1860 static void LISTVIEW_UpdateScroll(const LISTVIEW_INFO *infoPtr)
1862 SCROLLINFO horzInfo, vertInfo;
1865 if ((infoPtr->dwStyle & LVS_NOSCROLL) || !is_redrawing(infoPtr)) return;
1867 ZeroMemory(&horzInfo, sizeof(SCROLLINFO));
1868 horzInfo.cbSize = sizeof(SCROLLINFO);
1869 horzInfo.nPage = infoPtr->rcList.right - infoPtr->rcList.left;
1871 /* for now, we'll set info.nMax to the _count_, and adjust it later */
1872 if (infoPtr->uView == LV_VIEW_LIST)
1874 INT nPerCol = LISTVIEW_GetCountPerColumn(infoPtr);
1875 horzInfo.nMax = (infoPtr->nItemCount + nPerCol - 1) / nPerCol;
1877 /* scroll by at least one column per page */
1878 if(horzInfo.nPage < infoPtr->nItemWidth)
1879 horzInfo.nPage = infoPtr->nItemWidth;
1881 if (infoPtr->nItemWidth)
1882 horzInfo.nPage /= infoPtr->nItemWidth;
1884 else if (infoPtr->uView == LV_VIEW_DETAILS)
1886 horzInfo.nMax = infoPtr->nItemWidth;
1888 else /* LV_VIEW_ICON, or LV_VIEW_SMALLICON */
1892 if (LISTVIEW_GetViewRect(infoPtr, &rcView)) horzInfo.nMax = rcView.right - rcView.left;
1895 horzInfo.fMask = SIF_RANGE | SIF_PAGE;
1896 horzInfo.nMax = max(horzInfo.nMax - 1, 0);
1897 dx = GetScrollPos(infoPtr->hwndSelf, SB_HORZ);
1898 dx -= SetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &horzInfo, TRUE);
1899 TRACE("horzInfo=%s\n", debugscrollinfo(&horzInfo));
1901 /* Setting the horizontal scroll can change the listview size
1902 * (and potentially everything else) so we need to recompute
1903 * everything again for the vertical scroll
1906 ZeroMemory(&vertInfo, sizeof(SCROLLINFO));
1907 vertInfo.cbSize = sizeof(SCROLLINFO);
1908 vertInfo.nPage = infoPtr->rcList.bottom - infoPtr->rcList.top;
1910 if (infoPtr->uView == LV_VIEW_DETAILS)
1912 vertInfo.nMax = infoPtr->nItemCount;
1914 /* scroll by at least one page */
1915 if(vertInfo.nPage < infoPtr->nItemHeight)
1916 vertInfo.nPage = infoPtr->nItemHeight;
1918 if (infoPtr->nItemHeight > 0)
1919 vertInfo.nPage /= infoPtr->nItemHeight;
1921 else if (infoPtr->uView != LV_VIEW_LIST) /* LV_VIEW_ICON, or LV_VIEW_SMALLICON */
1925 if (LISTVIEW_GetViewRect(infoPtr, &rcView)) vertInfo.nMax = rcView.bottom - rcView.top;
1928 vertInfo.fMask = SIF_RANGE | SIF_PAGE;
1929 vertInfo.nMax = max(vertInfo.nMax - 1, 0);
1930 dy = GetScrollPos(infoPtr->hwndSelf, SB_VERT);
1931 dy -= SetScrollInfo(infoPtr->hwndSelf, SB_VERT, &vertInfo, TRUE);
1932 TRACE("vertInfo=%s\n", debugscrollinfo(&vertInfo));
1934 /* Change of the range may have changed the scroll pos. If so move the content */
1935 if (dx != 0 || dy != 0)
1938 listRect = infoPtr->rcList;
1939 ScrollWindowEx(infoPtr->hwndSelf, dx, dy, &listRect, &listRect, 0, 0,
1940 SW_ERASE | SW_INVALIDATE);
1943 /* Update the Header Control */
1944 if (infoPtr->uView == LV_VIEW_DETAILS)
1946 horzInfo.fMask = SIF_POS;
1947 GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &horzInfo);
1948 LISTVIEW_UpdateHeaderSize(infoPtr, horzInfo.nPos);
1955 * Shows/hides the focus rectangle.
1958 * [I] infoPtr : valid pointer to the listview structure
1959 * [I] fShow : TRUE to show the focus, FALSE to hide it.
1964 static void LISTVIEW_ShowFocusRect(const LISTVIEW_INFO *infoPtr, BOOL fShow)
1968 TRACE("fShow=%d, nItem=%d\n", fShow, infoPtr->nFocusedItem);
1970 if (infoPtr->nFocusedItem < 0) return;
1972 /* we need some gymnastics in ICON mode to handle large items */
1973 if (infoPtr->uView == LV_VIEW_ICON)
1977 LISTVIEW_GetItemBox(infoPtr, infoPtr->nFocusedItem, &rcBox);
1978 if ((rcBox.bottom - rcBox.top) > infoPtr->nItemHeight)
1980 LISTVIEW_InvalidateRect(infoPtr, &rcBox);
1985 if (!(hdc = GetDC(infoPtr->hwndSelf))) return;
1987 /* for some reason, owner draw should work only in report mode */
1988 if ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (infoPtr->uView == LV_VIEW_DETAILS))
1993 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
1994 HFONT hOldFont = SelectObject(hdc, hFont);
1996 item.iItem = infoPtr->nFocusedItem;
1998 item.mask = LVIF_PARAM;
1999 if (!LISTVIEW_GetItemW(infoPtr, &item)) goto done;
2001 ZeroMemory(&dis, sizeof(dis));
2002 dis.CtlType = ODT_LISTVIEW;
2003 dis.CtlID = (UINT)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
2004 dis.itemID = item.iItem;
2005 dis.itemAction = ODA_FOCUS;
2006 if (fShow) dis.itemState |= ODS_FOCUS;
2007 dis.hwndItem = infoPtr->hwndSelf;
2009 LISTVIEW_GetItemBox(infoPtr, dis.itemID, &dis.rcItem);
2010 dis.itemData = item.lParam;
2012 SendMessageW(infoPtr->hwndNotify, WM_DRAWITEM, dis.CtlID, (LPARAM)&dis);
2014 SelectObject(hdc, hOldFont);
2018 LISTVIEW_DrawFocusRect(infoPtr, hdc);
2021 ReleaseDC(infoPtr->hwndSelf, hdc);
2025 * Invalidates all visible selected items.
2027 static void LISTVIEW_InvalidateSelectedItems(const LISTVIEW_INFO *infoPtr)
2031 iterator_frameditems(&i, infoPtr, &infoPtr->rcList);
2032 while(iterator_next(&i))
2034 if (LISTVIEW_GetItemState(infoPtr, i.nItem, LVIS_SELECTED))
2035 LISTVIEW_InvalidateItem(infoPtr, i.nItem);
2037 iterator_destroy(&i);
2042 * DESCRIPTION: [INTERNAL]
2043 * Computes an item's (left,top) corner, relative to rcView.
2044 * That is, the position has NOT been made relative to the Origin.
2045 * This is deliberate, to avoid computing the Origin over, and
2046 * over again, when this function is called in a loop. Instead,
2047 * one can factor the computation of the Origin before the loop,
2048 * and offset the value returned by this function, on every iteration.
2051 * [I] infoPtr : valid pointer to the listview structure
2052 * [I] nItem : item number
2053 * [O] lpptOrig : item top, left corner
2058 static void LISTVIEW_GetItemOrigin(const LISTVIEW_INFO *infoPtr, INT nItem, LPPOINT lpptPosition)
2060 assert(nItem >= 0 && nItem < infoPtr->nItemCount);
2062 if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
2064 lpptPosition->x = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosX, nItem);
2065 lpptPosition->y = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosY, nItem);
2067 else if (infoPtr->uView == LV_VIEW_LIST)
2069 INT nCountPerColumn = LISTVIEW_GetCountPerColumn(infoPtr);
2070 lpptPosition->x = nItem / nCountPerColumn * infoPtr->nItemWidth;
2071 lpptPosition->y = nItem % nCountPerColumn * infoPtr->nItemHeight;
2073 else /* LV_VIEW_DETAILS */
2075 lpptPosition->x = REPORT_MARGINX;
2076 /* item is always at zero indexed column */
2077 if (DPA_GetPtrCount(infoPtr->hdpaColumns) > 0)
2078 lpptPosition->x += LISTVIEW_GetColumnInfo(infoPtr, 0)->rcHeader.left;
2079 lpptPosition->y = nItem * infoPtr->nItemHeight;
2084 * DESCRIPTION: [INTERNAL]
2085 * Compute the rectangles of an item. This is to localize all
2086 * the computations in one place. If you are not interested in some
2087 * of these values, simply pass in a NULL -- the function is smart
2088 * enough to compute only what's necessary. The function computes
2089 * the standard rectangles (BOUNDS, ICON, LABEL) plus a non-standard
2090 * one, the BOX rectangle. This rectangle is very cheap to compute,
2091 * and is guaranteed to contain all the other rectangles. Computing
2092 * the ICON rect is also cheap, but all the others are potentially
2093 * expensive. This gives an easy and effective optimization when
2094 * searching (like point inclusion, or rectangle intersection):
2095 * first test against the BOX, and if TRUE, test against the desired
2097 * If the function does not have all the necessary information
2098 * to computed the requested rectangles, will crash with a
2099 * failed assertion. This is done so we catch all programming
2100 * errors, given that the function is called only from our code.
2102 * We have the following 'special' meanings for a few fields:
2103 * * If LVIS_FOCUSED is set, we assume the item has the focus
2104 * This is important in ICON mode, where it might get a larger
2105 * then usual rectangle
2107 * Please note that subitem support works only in REPORT mode.
2110 * [I] infoPtr : valid pointer to the listview structure
2111 * [I] lpLVItem : item to compute the measures for
2112 * [O] lprcBox : ptr to Box rectangle
2113 * Same as LVM_GETITEMRECT with LVIR_BOUNDS
2114 * [0] lprcSelectBox : ptr to select box rectangle
2115 * Same as LVM_GETITEMRECT with LVIR_SELECTEDBOUNDS
2116 * [O] lprcIcon : ptr to Icon rectangle
2117 * Same as LVM_GETITEMRECT with LVIR_ICON
2118 * [O] lprcStateIcon: ptr to State Icon rectangle
2119 * [O] lprcLabel : ptr to Label rectangle
2120 * Same as LVM_GETITEMRECT with LVIR_LABEL
2125 static void LISTVIEW_GetItemMetrics(const LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem,
2126 LPRECT lprcBox, LPRECT lprcSelectBox,
2127 LPRECT lprcIcon, LPRECT lprcStateIcon, LPRECT lprcLabel)
2129 BOOL doSelectBox = FALSE, doIcon = FALSE, doLabel = FALSE, oversizedBox = FALSE;
2130 RECT Box, SelectBox, Icon, Label;
2131 COLUMN_INFO *lpColumnInfo = NULL;
2132 SIZE labelSize = { 0, 0 };
2134 TRACE("(lpLVItem=%s)\n", debuglvitem_t(lpLVItem, TRUE));
2136 /* Be smart and try to figure out the minimum we have to do */
2137 if (lpLVItem->iSubItem) assert(infoPtr->uView == LV_VIEW_DETAILS);
2138 if (infoPtr->uView == LV_VIEW_ICON && (lprcBox || lprcLabel))
2140 assert((lpLVItem->mask & LVIF_STATE) && (lpLVItem->stateMask & LVIS_FOCUSED));
2141 if (lpLVItem->state & LVIS_FOCUSED) oversizedBox = doLabel = TRUE;
2143 if (lprcSelectBox) doSelectBox = TRUE;
2144 if (lprcLabel) doLabel = TRUE;
2145 if (doLabel || lprcIcon || lprcStateIcon) doIcon = TRUE;
2152 /************************************************************/
2153 /* compute the box rectangle (it should be cheap to do) */
2154 /************************************************************/
2155 if (lpLVItem->iSubItem || infoPtr->uView == LV_VIEW_DETAILS)
2156 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, lpLVItem->iSubItem);
2158 if (lpLVItem->iSubItem)
2160 Box = lpColumnInfo->rcHeader;
2165 Box.right = infoPtr->nItemWidth;
2168 Box.bottom = infoPtr->nItemHeight;
2170 /******************************************************************/
2171 /* compute ICON bounding box (ala LVM_GETITEMRECT) and STATEICON */
2172 /******************************************************************/
2175 LONG state_width = 0;
2177 if (infoPtr->himlState && lpLVItem->iSubItem == 0)
2178 state_width = infoPtr->iconStateSize.cx;
2180 if (infoPtr->uView == LV_VIEW_ICON)
2182 Icon.left = Box.left + state_width;
2183 if (infoPtr->himlNormal)
2184 Icon.left += (infoPtr->nItemWidth - infoPtr->iconSize.cx - state_width) / 2;
2185 Icon.top = Box.top + ICON_TOP_PADDING;
2186 Icon.right = Icon.left;
2187 Icon.bottom = Icon.top;
2188 if (infoPtr->himlNormal)
2190 Icon.right += infoPtr->iconSize.cx;
2191 Icon.bottom += infoPtr->iconSize.cy;
2194 else /* LV_VIEW_SMALLICON, LV_VIEW_LIST or LV_VIEW_DETAILS */
2196 Icon.left = Box.left + state_width;
2198 if (infoPtr->uView == LV_VIEW_DETAILS && lpLVItem->iSubItem == 0)
2200 /* we need the indent in report mode */
2201 assert(lpLVItem->mask & LVIF_INDENT);
2202 Icon.left += infoPtr->iconSize.cx * lpLVItem->iIndent + REPORT_MARGINX;
2206 Icon.right = Icon.left;
2207 if (infoPtr->himlSmall &&
2208 (!lpColumnInfo || lpLVItem->iSubItem == 0 || (lpColumnInfo->fmt & LVCFMT_IMAGE) ||
2209 ((infoPtr->dwLvExStyle & LVS_EX_SUBITEMIMAGES) && lpLVItem->iImage != I_IMAGECALLBACK)))
2210 Icon.right += infoPtr->iconSize.cx;
2211 Icon.bottom = Icon.top + infoPtr->iconSize.cy;
2213 if(lprcIcon) *lprcIcon = Icon;
2214 TRACE(" - icon=%s\n", wine_dbgstr_rect(&Icon));
2216 /* TODO: is this correct? */
2219 lprcStateIcon->left = Icon.left - state_width;
2220 lprcStateIcon->right = Icon.left;
2221 lprcStateIcon->top = Icon.top;
2222 lprcStateIcon->bottom = lprcStateIcon->top + infoPtr->iconSize.cy;
2223 TRACE(" - state icon=%s\n", wine_dbgstr_rect(lprcStateIcon));
2226 else Icon.right = 0;
2228 /************************************************************/
2229 /* compute LABEL bounding box (ala LVM_GETITEMRECT) */
2230 /************************************************************/
2233 /* calculate how far to the right can the label stretch */
2234 Label.right = Box.right;
2235 if (infoPtr->uView == LV_VIEW_DETAILS)
2237 if (lpLVItem->iSubItem == 0)
2239 /* we need a zero based rect here */
2240 Label = lpColumnInfo->rcHeader;
2241 OffsetRect(&Label, -Label.left, 0);
2245 if (lpLVItem->iSubItem || ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && infoPtr->uView == LV_VIEW_DETAILS))
2247 labelSize.cx = infoPtr->nItemWidth;
2248 labelSize.cy = infoPtr->nItemHeight;
2252 /* we need the text in non owner draw mode */
2253 assert(lpLVItem->mask & LVIF_TEXT);
2254 if (is_textT(lpLVItem->pszText, TRUE))
2256 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
2257 HDC hdc = GetDC(infoPtr->hwndSelf);
2258 HFONT hOldFont = SelectObject(hdc, hFont);
2262 /* compute rough rectangle where the label will go */
2263 SetRectEmpty(&rcText);
2264 rcText.right = infoPtr->nItemWidth - TRAILING_LABEL_PADDING;
2265 rcText.bottom = infoPtr->nItemHeight;
2266 if (infoPtr->uView == LV_VIEW_ICON)
2267 rcText.bottom -= ICON_TOP_PADDING + infoPtr->iconSize.cy + ICON_BOTTOM_PADDING;
2269 /* now figure out the flags */
2270 if (infoPtr->uView == LV_VIEW_ICON)
2271 uFormat = oversizedBox ? LV_FL_DT_FLAGS : LV_ML_DT_FLAGS;
2273 uFormat = LV_SL_DT_FLAGS;
2275 DrawTextW (hdc, lpLVItem->pszText, -1, &rcText, uFormat | DT_CALCRECT);
2277 if (rcText.right != rcText.left)
2278 labelSize.cx = min(rcText.right - rcText.left + TRAILING_LABEL_PADDING, infoPtr->nItemWidth);
2280 labelSize.cy = rcText.bottom - rcText.top;
2282 SelectObject(hdc, hOldFont);
2283 ReleaseDC(infoPtr->hwndSelf, hdc);
2287 if (infoPtr->uView == LV_VIEW_ICON)
2289 Label.left = Box.left + (infoPtr->nItemWidth - labelSize.cx) / 2;
2290 Label.top = Box.top + ICON_TOP_PADDING_HITABLE +
2291 infoPtr->iconSize.cy + ICON_BOTTOM_PADDING;
2292 Label.right = Label.left + labelSize.cx;
2293 Label.bottom = Label.top + infoPtr->nItemHeight;
2294 if (!oversizedBox && labelSize.cy > infoPtr->ntmHeight)
2296 labelSize.cy = min(Box.bottom - Label.top, labelSize.cy);
2297 labelSize.cy /= infoPtr->ntmHeight;
2298 labelSize.cy = max(labelSize.cy, 1);
2299 labelSize.cy *= infoPtr->ntmHeight;
2301 Label.bottom = Label.top + labelSize.cy + HEIGHT_PADDING;
2303 else if (infoPtr->uView == LV_VIEW_DETAILS)
2305 Label.left = Icon.right;
2306 Label.top = Box.top;
2307 Label.right = lpLVItem->iSubItem ? lpColumnInfo->rcHeader.right :
2308 lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left;
2309 Label.bottom = Label.top + infoPtr->nItemHeight;
2311 else /* LV_VIEW_SMALLICON or LV_VIEW_LIST */
2313 Label.left = Icon.right;
2314 Label.top = Box.top;
2315 Label.right = min(Label.left + labelSize.cx, Label.right);
2316 Label.bottom = Label.top + infoPtr->nItemHeight;
2319 if (lprcLabel) *lprcLabel = Label;
2320 TRACE(" - label=%s\n", wine_dbgstr_rect(&Label));
2323 /************************************************************/
2324 /* compute SELECT bounding box */
2325 /************************************************************/
2328 if (infoPtr->uView == LV_VIEW_DETAILS)
2330 SelectBox.left = Icon.left;
2331 SelectBox.top = Box.top;
2332 SelectBox.bottom = Box.bottom;
2335 SelectBox.right = min(Label.left + labelSize.cx, Label.right);
2337 SelectBox.right = min(Label.left + MAX_EMPTYTEXT_SELECT_WIDTH, Label.right);
2341 UnionRect(&SelectBox, &Icon, &Label);
2343 if (lprcSelectBox) *lprcSelectBox = SelectBox;
2344 TRACE(" - select box=%s\n", wine_dbgstr_rect(&SelectBox));
2347 /* Fix the Box if necessary */
2350 if (oversizedBox) UnionRect(lprcBox, &Box, &Label);
2351 else *lprcBox = Box;
2353 TRACE(" - box=%s\n", wine_dbgstr_rect(&Box));
2357 * DESCRIPTION: [INTERNAL]
2360 * [I] infoPtr : valid pointer to the listview structure
2361 * [I] nItem : item number
2362 * [O] lprcBox : ptr to Box rectangle
2367 static void LISTVIEW_GetItemBox(const LISTVIEW_INFO *infoPtr, INT nItem, LPRECT lprcBox)
2369 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
2370 POINT Position, Origin;
2373 LISTVIEW_GetOrigin(infoPtr, &Origin);
2374 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
2376 /* Be smart and try to figure out the minimum we have to do */
2378 if (infoPtr->uView == LV_VIEW_ICON && infoPtr->bFocus && LISTVIEW_GetItemState(infoPtr, nItem, LVIS_FOCUSED))
2379 lvItem.mask |= LVIF_TEXT;
2380 lvItem.iItem = nItem;
2381 lvItem.iSubItem = 0;
2382 lvItem.pszText = szDispText;
2383 lvItem.cchTextMax = DISP_TEXT_SIZE;
2384 if (lvItem.mask) LISTVIEW_GetItemW(infoPtr, &lvItem);
2385 if (infoPtr->uView == LV_VIEW_ICON)
2387 lvItem.mask |= LVIF_STATE;
2388 lvItem.stateMask = LVIS_FOCUSED;
2389 lvItem.state = (lvItem.mask & LVIF_TEXT ? LVIS_FOCUSED : 0);
2391 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, lprcBox, 0, 0, 0, 0);
2393 if (infoPtr->uView == LV_VIEW_DETAILS && infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT &&
2394 SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, 0, 0))
2396 OffsetRect(lprcBox, Origin.x, Position.y + Origin.y);
2399 OffsetRect(lprcBox, Position.x + Origin.x, Position.y + Origin.y);
2402 /* LISTVIEW_MapIdToIndex helper */
2403 static INT CALLBACK MapIdSearchCompare(LPVOID p1, LPVOID p2, LPARAM lParam)
2405 ITEM_ID *id1 = (ITEM_ID*)p1;
2406 ITEM_ID *id2 = (ITEM_ID*)p2;
2408 if (id1->id == id2->id) return 0;
2410 return (id1->id < id2->id) ? -1 : 1;
2415 * Returns the item index for id specified.
2418 * [I] infoPtr : valid pointer to the listview structure
2419 * [I] iID : item id to get index for
2422 * Item index, or -1 on failure.
2424 static INT LISTVIEW_MapIdToIndex(const LISTVIEW_INFO *infoPtr, UINT iID)
2429 TRACE("iID=%d\n", iID);
2431 if (infoPtr->dwStyle & LVS_OWNERDATA) return -1;
2432 if (infoPtr->nItemCount == 0) return -1;
2435 index = DPA_Search(infoPtr->hdpaItemIds, &ID, -1, &MapIdSearchCompare, 0, DPAS_SORTED);
2439 ITEM_ID *lpID = DPA_GetPtr(infoPtr->hdpaItemIds, index);
2440 return DPA_GetPtrIndex(infoPtr->hdpaItems, lpID->item);
2448 * Returns the item id for index given.
2451 * [I] infoPtr : valid pointer to the listview structure
2452 * [I] iItem : item index to get id for
2457 static DWORD LISTVIEW_MapIndexToId(const LISTVIEW_INFO *infoPtr, INT iItem)
2462 TRACE("iItem=%d\n", iItem);
2464 if (infoPtr->dwStyle & LVS_OWNERDATA) return -1;
2465 if (iItem < 0 || iItem >= infoPtr->nItemCount) return -1;
2467 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, iItem);
2468 lpItem = DPA_GetPtr(hdpaSubItems, 0);
2470 return lpItem->id->id;
2475 * Returns the current icon position, and advances it along the top.
2476 * The returned position is not offset by Origin.
2479 * [I] infoPtr : valid pointer to the listview structure
2480 * [O] lpPos : will get the current icon position
2485 static void LISTVIEW_NextIconPosTop(LISTVIEW_INFO *infoPtr, LPPOINT lpPos)
2487 INT nListWidth = infoPtr->rcList.right - infoPtr->rcList.left;
2489 *lpPos = infoPtr->currIconPos;
2491 infoPtr->currIconPos.x += infoPtr->nItemWidth;
2492 if (infoPtr->currIconPos.x + infoPtr->nItemWidth <= nListWidth) return;
2494 infoPtr->currIconPos.x = 0;
2495 infoPtr->currIconPos.y += infoPtr->nItemHeight;
2501 * Returns the current icon position, and advances it down the left edge.
2502 * The returned position is not offset by Origin.
2505 * [I] infoPtr : valid pointer to the listview structure
2506 * [O] lpPos : will get the current icon position
2511 static void LISTVIEW_NextIconPosLeft(LISTVIEW_INFO *infoPtr, LPPOINT lpPos)
2513 INT nListHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
2515 *lpPos = infoPtr->currIconPos;
2517 infoPtr->currIconPos.y += infoPtr->nItemHeight;
2518 if (infoPtr->currIconPos.y + infoPtr->nItemHeight <= nListHeight) return;
2520 infoPtr->currIconPos.x += infoPtr->nItemWidth;
2521 infoPtr->currIconPos.y = 0;
2527 * Moves an icon to the specified position.
2528 * It takes care of invalidating the item, etc.
2531 * [I] infoPtr : valid pointer to the listview structure
2532 * [I] nItem : the item to move
2533 * [I] lpPos : the new icon position
2534 * [I] isNew : flags the item as being new
2540 static BOOL LISTVIEW_MoveIconTo(const LISTVIEW_INFO *infoPtr, INT nItem, const POINT *lppt, BOOL isNew)
2546 old.x = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosX, nItem);
2547 old.y = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosY, nItem);
2549 if (lppt->x == old.x && lppt->y == old.y) return TRUE;
2550 LISTVIEW_InvalidateItem(infoPtr, nItem);
2553 /* Allocating a POINTER for every item is too resource intensive,
2554 * so we'll keep the (x,y) in different arrays */
2555 if (!DPA_SetPtr(infoPtr->hdpaPosX, nItem, (void *)(LONG_PTR)lppt->x)) return FALSE;
2556 if (!DPA_SetPtr(infoPtr->hdpaPosY, nItem, (void *)(LONG_PTR)lppt->y)) return FALSE;
2558 LISTVIEW_InvalidateItem(infoPtr, nItem);
2565 * Arranges listview items in icon display mode.
2568 * [I] infoPtr : valid pointer to the listview structure
2569 * [I] nAlignCode : alignment code
2575 static BOOL LISTVIEW_Arrange(LISTVIEW_INFO *infoPtr, INT nAlignCode)
2577 void (*next_pos)(LISTVIEW_INFO *, LPPOINT);
2581 if (infoPtr->uView != LV_VIEW_ICON && infoPtr->uView != LV_VIEW_SMALLICON) return FALSE;
2583 TRACE("nAlignCode=%d\n", nAlignCode);
2585 if (nAlignCode == LVA_DEFAULT)
2587 if (infoPtr->dwStyle & LVS_ALIGNLEFT) nAlignCode = LVA_ALIGNLEFT;
2588 else nAlignCode = LVA_ALIGNTOP;
2593 case LVA_ALIGNLEFT: next_pos = LISTVIEW_NextIconPosLeft; break;
2594 case LVA_ALIGNTOP: next_pos = LISTVIEW_NextIconPosTop; break;
2595 case LVA_SNAPTOGRID: next_pos = LISTVIEW_NextIconPosTop; break; /* FIXME */
2596 default: return FALSE;
2599 infoPtr->bAutoarrange = TRUE;
2600 infoPtr->currIconPos.x = infoPtr->currIconPos.y = 0;
2601 for (i = 0; i < infoPtr->nItemCount; i++)
2603 next_pos(infoPtr, &pos);
2604 LISTVIEW_MoveIconTo(infoPtr, i, &pos, FALSE);
2612 * Retrieves the bounding rectangle of all the items, not offset by Origin.
2613 * For LVS_REPORT always returns empty rectangle.
2616 * [I] infoPtr : valid pointer to the listview structure
2617 * [O] lprcView : bounding rectangle
2623 static void LISTVIEW_GetAreaRect(const LISTVIEW_INFO *infoPtr, LPRECT lprcView)
2627 SetRectEmpty(lprcView);
2629 switch (infoPtr->uView)
2632 case LV_VIEW_SMALLICON:
2633 for (i = 0; i < infoPtr->nItemCount; i++)
2635 x = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosX, i);
2636 y = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosY, i);
2637 lprcView->right = max(lprcView->right, x);
2638 lprcView->bottom = max(lprcView->bottom, y);
2640 if (infoPtr->nItemCount > 0)
2642 lprcView->right += infoPtr->nItemWidth;
2643 lprcView->bottom += infoPtr->nItemHeight;
2648 y = LISTVIEW_GetCountPerColumn(infoPtr);
2649 x = infoPtr->nItemCount / y;
2650 if (infoPtr->nItemCount % y) x++;
2651 lprcView->right = x * infoPtr->nItemWidth;
2652 lprcView->bottom = y * infoPtr->nItemHeight;
2659 * Retrieves the bounding rectangle of all the items.
2662 * [I] infoPtr : valid pointer to the listview structure
2663 * [O] lprcView : bounding rectangle
2669 static BOOL LISTVIEW_GetViewRect(const LISTVIEW_INFO *infoPtr, LPRECT lprcView)
2673 TRACE("(lprcView=%p)\n", lprcView);
2675 if (!lprcView) return FALSE;
2677 LISTVIEW_GetAreaRect(infoPtr, lprcView);
2679 if (infoPtr->uView != LV_VIEW_DETAILS)
2681 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
2682 OffsetRect(lprcView, ptOrigin.x, ptOrigin.y);
2685 TRACE("lprcView=%s\n", wine_dbgstr_rect(lprcView));
2692 * Retrieves the subitem pointer associated with the subitem index.
2695 * [I] hdpaSubItems : DPA handle for a specific item
2696 * [I] nSubItem : index of subitem
2699 * SUCCESS : subitem pointer
2702 static SUBITEM_INFO* LISTVIEW_GetSubItemPtr(HDPA hdpaSubItems, INT nSubItem)
2704 SUBITEM_INFO *lpSubItem;
2707 /* we should binary search here if need be */
2708 for (i = 1; i < DPA_GetPtrCount(hdpaSubItems); i++)
2710 lpSubItem = DPA_GetPtr(hdpaSubItems, i);
2711 if (lpSubItem->iSubItem == nSubItem)
2721 * Calculates the desired item width.
2724 * [I] infoPtr : valid pointer to the listview structure
2727 * The desired item width.
2729 static INT LISTVIEW_CalculateItemWidth(const LISTVIEW_INFO *infoPtr)
2733 TRACE("uView=%d\n", infoPtr->uView);
2735 if (infoPtr->uView == LV_VIEW_ICON)
2736 nItemWidth = infoPtr->iconSpacing.cx;
2737 else if (infoPtr->uView == LV_VIEW_DETAILS)
2739 if (DPA_GetPtrCount(infoPtr->hdpaColumns) > 0)
2744 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX,
2745 DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, 0);
2747 LISTVIEW_GetHeaderRect(infoPtr, index, &rcHeader);
2748 nItemWidth = rcHeader.right;
2751 else /* LV_VIEW_SMALLICON, or LV_VIEW_LIST */
2755 for (i = 0; i < infoPtr->nItemCount; i++)
2756 nItemWidth = max(LISTVIEW_GetLabelWidth(infoPtr, i), nItemWidth);
2758 if (infoPtr->himlSmall) nItemWidth += infoPtr->iconSize.cx;
2759 if (infoPtr->himlState) nItemWidth += infoPtr->iconStateSize.cx;
2761 nItemWidth = max(DEFAULT_COLUMN_WIDTH, nItemWidth + WIDTH_PADDING);
2769 * Calculates the desired item height.
2772 * [I] infoPtr : valid pointer to the listview structure
2775 * The desired item height.
2777 static INT LISTVIEW_CalculateItemHeight(const LISTVIEW_INFO *infoPtr)
2781 TRACE("uView=%d\n", infoPtr->uView);
2783 if (infoPtr->uView == LV_VIEW_ICON)
2784 nItemHeight = infoPtr->iconSpacing.cy;
2787 nItemHeight = infoPtr->ntmHeight;
2788 if (infoPtr->uView == LV_VIEW_DETAILS && infoPtr->dwLvExStyle & LVS_EX_GRIDLINES)
2790 if (infoPtr->himlState)
2791 nItemHeight = max(nItemHeight, infoPtr->iconStateSize.cy);
2792 if (infoPtr->himlSmall)
2793 nItemHeight = max(nItemHeight, infoPtr->iconSize.cy);
2794 if (infoPtr->himlState || infoPtr->himlSmall)
2795 nItemHeight += HEIGHT_PADDING;
2796 if (infoPtr->nMeasureItemHeight > 0)
2797 nItemHeight = infoPtr->nMeasureItemHeight;
2800 return max(nItemHeight, 1);
2805 * Updates the width, and height of an item.
2808 * [I] infoPtr : valid pointer to the listview structure
2813 static inline void LISTVIEW_UpdateItemSize(LISTVIEW_INFO *infoPtr)
2815 infoPtr->nItemWidth = LISTVIEW_CalculateItemWidth(infoPtr);
2816 infoPtr->nItemHeight = LISTVIEW_CalculateItemHeight(infoPtr);
2822 * Retrieves and saves important text metrics info for the current
2826 * [I] infoPtr : valid pointer to the listview structure
2829 static void LISTVIEW_SaveTextMetrics(LISTVIEW_INFO *infoPtr)
2831 HDC hdc = GetDC(infoPtr->hwndSelf);
2832 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
2833 HFONT hOldFont = SelectObject(hdc, hFont);
2837 if (GetTextMetricsW(hdc, &tm))
2839 infoPtr->ntmHeight = tm.tmHeight;
2840 infoPtr->ntmMaxCharWidth = tm.tmMaxCharWidth;
2843 if (GetTextExtentPoint32A(hdc, "...", 3, &sz))
2844 infoPtr->nEllipsisWidth = sz.cx;
2846 SelectObject(hdc, hOldFont);
2847 ReleaseDC(infoPtr->hwndSelf, hdc);
2849 TRACE("tmHeight=%d\n", infoPtr->ntmHeight);
2854 * A compare function for ranges
2857 * [I] range1 : pointer to range 1;
2858 * [I] range2 : pointer to range 2;
2862 * > 0 : if range 1 > range 2
2863 * < 0 : if range 2 > range 1
2864 * = 0 : if range intersects range 2
2866 static INT CALLBACK ranges_cmp(LPVOID range1, LPVOID range2, LPARAM flags)
2870 if (((RANGE*)range1)->upper <= ((RANGE*)range2)->lower)
2872 else if (((RANGE*)range2)->upper <= ((RANGE*)range1)->lower)
2877 TRACE("range1=%s, range2=%s, cmp=%d\n", debugrange(range1), debugrange(range2), cmp);
2883 #define ranges_check(ranges, desc) ranges_assert(ranges, desc, __FUNCTION__, __LINE__)
2885 #define ranges_check(ranges, desc) do { } while(0)
2888 static void ranges_assert(RANGES ranges, LPCSTR desc, const char *func, int line)
2893 TRACE("*** Checking %s:%d:%s ***\n", func, line, desc);
2895 assert (DPA_GetPtrCount(ranges->hdpa) >= 0);
2896 ranges_dump(ranges);
2897 if (DPA_GetPtrCount(ranges->hdpa) > 0)
2899 prev = DPA_GetPtr(ranges->hdpa, 0);
2900 assert (prev->lower >= 0 && prev->lower < prev->upper);
2901 for (i = 1; i < DPA_GetPtrCount(ranges->hdpa); i++)
2903 curr = DPA_GetPtr(ranges->hdpa, i);
2904 assert (prev->upper <= curr->lower);
2905 assert (curr->lower < curr->upper);
2909 TRACE("--- Done checking---\n");
2912 static RANGES ranges_create(int count)
2914 RANGES ranges = Alloc(sizeof(struct tagRANGES));
2915 if (!ranges) return NULL;
2916 ranges->hdpa = DPA_Create(count);
2917 if (ranges->hdpa) return ranges;
2922 static void ranges_clear(RANGES ranges)
2926 for(i = 0; i < DPA_GetPtrCount(ranges->hdpa); i++)
2927 Free(DPA_GetPtr(ranges->hdpa, i));
2928 DPA_DeleteAllPtrs(ranges->hdpa);
2932 static void ranges_destroy(RANGES ranges)
2934 if (!ranges) return;
2935 ranges_clear(ranges);
2936 DPA_Destroy(ranges->hdpa);
2940 static RANGES ranges_clone(RANGES ranges)
2945 if (!(clone = ranges_create(DPA_GetPtrCount(ranges->hdpa)))) goto fail;
2947 for (i = 0; i < DPA_GetPtrCount(ranges->hdpa); i++)
2949 RANGE *newrng = Alloc(sizeof(RANGE));
2950 if (!newrng) goto fail;
2951 *newrng = *((RANGE*)DPA_GetPtr(ranges->hdpa, i));
2952 DPA_SetPtr(clone->hdpa, i, newrng);
2957 TRACE ("clone failed\n");
2958 ranges_destroy(clone);
2962 static RANGES ranges_diff(RANGES ranges, RANGES sub)
2966 for (i = 0; i < DPA_GetPtrCount(sub->hdpa); i++)
2967 ranges_del(ranges, *((RANGE *)DPA_GetPtr(sub->hdpa, i)));
2972 static void ranges_dump(RANGES ranges)
2976 for (i = 0; i < DPA_GetPtrCount(ranges->hdpa); i++)
2977 TRACE(" %s\n", debugrange(DPA_GetPtr(ranges->hdpa, i)));
2980 static inline BOOL ranges_contain(RANGES ranges, INT nItem)
2982 RANGE srchrng = { nItem, nItem + 1 };
2984 TRACE("(nItem=%d)\n", nItem);
2985 ranges_check(ranges, "before contain");
2986 return DPA_Search(ranges->hdpa, &srchrng, 0, ranges_cmp, 0, DPAS_SORTED) != -1;
2989 static INT ranges_itemcount(RANGES ranges)
2993 for (i = 0; i < DPA_GetPtrCount(ranges->hdpa); i++)
2995 RANGE *sel = DPA_GetPtr(ranges->hdpa, i);
2996 count += sel->upper - sel->lower;
3002 static BOOL ranges_shift(RANGES ranges, INT nItem, INT delta, INT nUpper)
3004 RANGE srchrng = { nItem, nItem + 1 }, *chkrng;
3007 index = DPA_Search(ranges->hdpa, &srchrng, 0, ranges_cmp, 0, DPAS_SORTED | DPAS_INSERTAFTER);
3008 if (index == -1) return TRUE;
3010 for (; index < DPA_GetPtrCount(ranges->hdpa); index++)
3012 chkrng = DPA_GetPtr(ranges->hdpa, index);
3013 if (chkrng->lower >= nItem)
3014 chkrng->lower = max(min(chkrng->lower + delta, nUpper - 1), 0);
3015 if (chkrng->upper > nItem)
3016 chkrng->upper = max(min(chkrng->upper + delta, nUpper), 0);
3021 static BOOL ranges_add(RANGES ranges, RANGE range)
3026 TRACE("(%s)\n", debugrange(&range));
3027 ranges_check(ranges, "before add");
3029 /* try find overlapping regions first */
3030 srchrgn.lower = range.lower - 1;
3031 srchrgn.upper = range.upper + 1;
3032 index = DPA_Search(ranges->hdpa, &srchrgn, 0, ranges_cmp, 0, DPAS_SORTED);
3038 TRACE("Adding new range\n");
3040 /* create the brand new range to insert */
3041 newrgn = Alloc(sizeof(RANGE));
3042 if(!newrgn) goto fail;
3045 /* figure out where to insert it */
3046 index = DPA_Search(ranges->hdpa, newrgn, 0, ranges_cmp, 0, DPAS_SORTED | DPAS_INSERTAFTER);
3047 TRACE("index=%d\n", index);
3048 if (index == -1) index = 0;
3050 /* and get it over with */
3051 if (DPA_InsertPtr(ranges->hdpa, index, newrgn) == -1)
3059 RANGE *chkrgn, *mrgrgn;
3060 INT fromindex, mergeindex;
3062 chkrgn = DPA_GetPtr(ranges->hdpa, index);
3063 TRACE("Merge with %s @%d\n", debugrange(chkrgn), index);
3065 chkrgn->lower = min(range.lower, chkrgn->lower);
3066 chkrgn->upper = max(range.upper, chkrgn->upper);
3068 TRACE("New range %s @%d\n", debugrange(chkrgn), index);
3070 /* merge now common ranges */
3072 srchrgn.lower = chkrgn->lower - 1;
3073 srchrgn.upper = chkrgn->upper + 1;
3077 mergeindex = DPA_Search(ranges->hdpa, &srchrgn, fromindex, ranges_cmp, 0, 0);
3078 if (mergeindex == -1) break;
3079 if (mergeindex == index)
3081 fromindex = index + 1;
3085 TRACE("Merge with index %i\n", mergeindex);
3087 mrgrgn = DPA_GetPtr(ranges->hdpa, mergeindex);
3088 chkrgn->lower = min(chkrgn->lower, mrgrgn->lower);
3089 chkrgn->upper = max(chkrgn->upper, mrgrgn->upper);
3091 DPA_DeletePtr(ranges->hdpa, mergeindex);
3092 if (mergeindex < index) index --;
3096 ranges_check(ranges, "after add");
3100 ranges_check(ranges, "failed add");
3104 static BOOL ranges_del(RANGES ranges, RANGE range)
3109 TRACE("(%s)\n", debugrange(&range));
3110 ranges_check(ranges, "before del");
3112 /* we don't use DPAS_SORTED here, since we need *
3113 * to find the first overlapping range */
3114 index = DPA_Search(ranges->hdpa, &range, 0, ranges_cmp, 0, 0);
3117 chkrgn = DPA_GetPtr(ranges->hdpa, index);
3119 TRACE("Matches range %s @%d\n", debugrange(chkrgn), index);
3121 /* case 1: Same range */
3122 if ( (chkrgn->upper == range.upper) &&
3123 (chkrgn->lower == range.lower) )
3125 DPA_DeletePtr(ranges->hdpa, index);
3128 /* case 2: engulf */
3129 else if ( (chkrgn->upper <= range.upper) &&
3130 (chkrgn->lower >= range.lower) )
3132 DPA_DeletePtr(ranges->hdpa, index);
3134 /* case 3: overlap upper */
3135 else if ( (chkrgn->upper <= range.upper) &&
3136 (chkrgn->lower < range.lower) )
3138 chkrgn->upper = range.lower;
3140 /* case 4: overlap lower */
3141 else if ( (chkrgn->upper > range.upper) &&
3142 (chkrgn->lower >= range.lower) )
3144 chkrgn->lower = range.upper;
3147 /* case 5: fully internal */
3150 RANGE tmprgn = *chkrgn, *newrgn;
3152 if (!(newrgn = Alloc(sizeof(RANGE)))) goto fail;
3153 newrgn->lower = chkrgn->lower;
3154 newrgn->upper = range.lower;
3155 chkrgn->lower = range.upper;
3156 if (DPA_InsertPtr(ranges->hdpa, index, newrgn) == -1)
3165 index = DPA_Search(ranges->hdpa, &range, index, ranges_cmp, 0, 0);
3168 ranges_check(ranges, "after del");
3172 ranges_check(ranges, "failed del");
3178 * Removes all selection ranges
3181 * [I] infoPtr : valid pointer to the listview structure
3182 * [I] toSkip : item range to skip removing the selection
3188 static BOOL LISTVIEW_DeselectAllSkipItems(LISTVIEW_INFO *infoPtr, RANGES toSkip)
3197 lvItem.stateMask = LVIS_SELECTED;
3199 /* need to clone the DPA because callbacks can change it */
3200 if (!(clone = ranges_clone(infoPtr->selectionRanges))) return FALSE;
3201 iterator_rangesitems(&i, ranges_diff(clone, toSkip));
3202 while(iterator_next(&i))
3203 LISTVIEW_SetItemState(infoPtr, i.nItem, &lvItem);
3204 /* note that the iterator destructor will free the cloned range */
3205 iterator_destroy(&i);
3210 static inline BOOL LISTVIEW_DeselectAllSkipItem(LISTVIEW_INFO *infoPtr, INT nItem)
3214 if (!(toSkip = ranges_create(1))) return FALSE;
3215 if (nItem != -1) ranges_additem(toSkip, nItem);
3216 LISTVIEW_DeselectAllSkipItems(infoPtr, toSkip);
3217 ranges_destroy(toSkip);
3221 static inline BOOL LISTVIEW_DeselectAll(LISTVIEW_INFO *infoPtr)
3223 return LISTVIEW_DeselectAllSkipItem(infoPtr, -1);
3228 * Retrieves the number of items that are marked as selected.
3231 * [I] infoPtr : valid pointer to the listview structure
3234 * Number of items selected.
3236 static INT LISTVIEW_GetSelectedCount(const LISTVIEW_INFO *infoPtr)
3238 INT nSelectedCount = 0;
3240 if (infoPtr->uCallbackMask & LVIS_SELECTED)
3243 for (i = 0; i < infoPtr->nItemCount; i++)
3245 if (LISTVIEW_GetItemState(infoPtr, i, LVIS_SELECTED))
3250 nSelectedCount = ranges_itemcount(infoPtr->selectionRanges);
3252 TRACE("nSelectedCount=%d\n", nSelectedCount);
3253 return nSelectedCount;
3258 * Manages the item focus.
3261 * [I] infoPtr : valid pointer to the listview structure
3262 * [I] nItem : item index
3265 * TRUE : focused item changed
3266 * FALSE : focused item has NOT changed
3268 static inline BOOL LISTVIEW_SetItemFocus(LISTVIEW_INFO *infoPtr, INT nItem)
3270 INT oldFocus = infoPtr->nFocusedItem;
3273 if (nItem == infoPtr->nFocusedItem) return FALSE;
3275 lvItem.state = nItem == -1 ? 0 : LVIS_FOCUSED;
3276 lvItem.stateMask = LVIS_FOCUSED;
3277 LISTVIEW_SetItemState(infoPtr, nItem == -1 ? infoPtr->nFocusedItem : nItem, &lvItem);
3279 return oldFocus != infoPtr->nFocusedItem;
3282 /* Helper function for LISTVIEW_ShiftIndices *only* */
3283 static INT shift_item(const LISTVIEW_INFO *infoPtr, INT nShiftItem, INT nItem, INT direction)
3285 if (nShiftItem < nItem) return nShiftItem;
3287 if (nShiftItem > nItem) return nShiftItem + direction;
3289 if (direction > 0) return nShiftItem + direction;
3291 return min(nShiftItem, infoPtr->nItemCount - 1);
3296 * Updates the various indices after an item has been inserted or deleted.
3299 * [I] infoPtr : valid pointer to the listview structure
3300 * [I] nItem : item index
3301 * [I] direction : Direction of shift, +1 or -1.
3306 static void LISTVIEW_ShiftIndices(LISTVIEW_INFO *infoPtr, INT nItem, INT direction)
3311 /* temporarily disable change notification while shifting items */
3312 bOldChange = infoPtr->bDoChangeNotify;
3313 infoPtr->bDoChangeNotify = FALSE;
3315 TRACE("Shifting %iu, %i steps\n", nItem, direction);
3317 ranges_shift(infoPtr->selectionRanges, nItem, direction, infoPtr->nItemCount);
3319 assert(abs(direction) == 1);
3321 infoPtr->nSelectionMark = shift_item(infoPtr, infoPtr->nSelectionMark, nItem, direction);
3323 nNewFocus = shift_item(infoPtr, infoPtr->nFocusedItem, nItem, direction);
3324 if (nNewFocus != infoPtr->nFocusedItem)
3325 LISTVIEW_SetItemFocus(infoPtr, nNewFocus);
3327 /* But we are not supposed to modify nHotItem! */
3329 infoPtr->bDoChangeNotify = bOldChange;
3335 * Adds a block of selections.
3338 * [I] infoPtr : valid pointer to the listview structure
3339 * [I] nItem : item index
3342 * Whether the window is still valid.
3344 static BOOL LISTVIEW_AddGroupSelection(LISTVIEW_INFO *infoPtr, INT nItem)
3346 INT nFirst = min(infoPtr->nSelectionMark, nItem);
3347 INT nLast = max(infoPtr->nSelectionMark, nItem);
3348 HWND hwndSelf = infoPtr->hwndSelf;
3349 NMLVODSTATECHANGE nmlv;
3354 /* Temporarily disable change notification
3355 * If the control is LVS_OWNERDATA, we need to send
3356 * only one LVN_ODSTATECHANGED notification.
3357 * See MSDN documentation for LVN_ITEMCHANGED.
3359 bOldChange = infoPtr->bDoChangeNotify;
3360 if (infoPtr->dwStyle & LVS_OWNERDATA) infoPtr->bDoChangeNotify = FALSE;
3362 if (nFirst == -1) nFirst = nItem;
3364 item.state = LVIS_SELECTED;
3365 item.stateMask = LVIS_SELECTED;
3367 for (i = nFirst; i <= nLast; i++)
3368 LISTVIEW_SetItemState(infoPtr,i,&item);
3370 ZeroMemory(&nmlv, sizeof(nmlv));
3371 nmlv.iFrom = nFirst;
3374 nmlv.uOldState = item.state;
3376 notify_hdr(infoPtr, LVN_ODSTATECHANGED, (LPNMHDR)&nmlv);
3377 if (!IsWindow(hwndSelf))
3379 infoPtr->bDoChangeNotify = bOldChange;
3386 * Sets a single group selection.
3389 * [I] infoPtr : valid pointer to the listview structure
3390 * [I] nItem : item index
3395 static void LISTVIEW_SetGroupSelection(LISTVIEW_INFO *infoPtr, INT nItem)
3402 if (!(selection = ranges_create(100))) return;
3404 item.state = LVIS_SELECTED;
3405 item.stateMask = LVIS_SELECTED;
3407 if ((infoPtr->uView == LV_VIEW_LIST) || (infoPtr->uView == LV_VIEW_DETAILS))
3409 if (infoPtr->nSelectionMark == -1)
3411 infoPtr->nSelectionMark = nItem;
3412 ranges_additem(selection, nItem);
3418 sel.lower = min(infoPtr->nSelectionMark, nItem);
3419 sel.upper = max(infoPtr->nSelectionMark, nItem) + 1;
3420 ranges_add(selection, sel);
3425 RECT rcItem, rcSel, rcSelMark;
3428 rcItem.left = LVIR_BOUNDS;
3429 if (!LISTVIEW_GetItemRect(infoPtr, nItem, &rcItem)) return;
3430 rcSelMark.left = LVIR_BOUNDS;
3431 if (!LISTVIEW_GetItemRect(infoPtr, infoPtr->nSelectionMark, &rcSelMark)) return;
3432 UnionRect(&rcSel, &rcItem, &rcSelMark);
3433 iterator_frameditems(&i, infoPtr, &rcSel);
3434 while(iterator_next(&i))
3436 LISTVIEW_GetItemPosition(infoPtr, i.nItem, &ptItem);
3437 if (PtInRect(&rcSel, ptItem)) ranges_additem(selection, i.nItem);
3439 iterator_destroy(&i);
3442 /* disable per item notifications on LVS_OWNERDATA style
3443 FIXME: single LVN_ODSTATECHANGED should be used */
3444 bOldChange = infoPtr->bDoChangeNotify;
3445 if (infoPtr->dwStyle & LVS_OWNERDATA) infoPtr->bDoChangeNotify = FALSE;
3447 LISTVIEW_DeselectAllSkipItems(infoPtr, selection);
3450 iterator_rangesitems(&i, selection);
3451 while(iterator_next(&i))
3452 LISTVIEW_SetItemState(infoPtr, i.nItem, &item);
3453 /* this will also destroy the selection */
3454 iterator_destroy(&i);
3456 infoPtr->bDoChangeNotify = bOldChange;
3458 LISTVIEW_SetItemFocus(infoPtr, nItem);
3463 * Sets a single selection.
3466 * [I] infoPtr : valid pointer to the listview structure
3467 * [I] nItem : item index
3472 static void LISTVIEW_SetSelection(LISTVIEW_INFO *infoPtr, INT nItem)
3476 TRACE("nItem=%d\n", nItem);
3478 LISTVIEW_DeselectAllSkipItem(infoPtr, nItem);
3480 lvItem.state = LVIS_FOCUSED | LVIS_SELECTED;
3481 lvItem.stateMask = LVIS_FOCUSED | LVIS_SELECTED;
3482 LISTVIEW_SetItemState(infoPtr, nItem, &lvItem);
3484 infoPtr->nSelectionMark = nItem;
3489 * Set selection(s) with keyboard.
3492 * [I] infoPtr : valid pointer to the listview structure
3493 * [I] nItem : item index
3494 * [I] space : VK_SPACE code sent
3497 * SUCCESS : TRUE (needs to be repainted)
3498 * FAILURE : FALSE (nothing has changed)
3500 static BOOL LISTVIEW_KeySelection(LISTVIEW_INFO *infoPtr, INT nItem, BOOL space)
3502 /* FIXME: pass in the state */
3503 WORD wShift = HIWORD(GetKeyState(VK_SHIFT));
3504 WORD wCtrl = HIWORD(GetKeyState(VK_CONTROL));
3505 BOOL bResult = FALSE;
3507 TRACE("nItem=%d, wShift=%d, wCtrl=%d\n", nItem, wShift, wCtrl);
3508 if ((nItem >= 0) && (nItem < infoPtr->nItemCount))
3512 if (infoPtr->dwStyle & LVS_SINGLESEL || (wShift == 0 && wCtrl == 0))
3513 LISTVIEW_SetSelection(infoPtr, nItem);
3517 LISTVIEW_SetGroupSelection(infoPtr, nItem);
3521 lvItem.state = ~LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED);
3522 lvItem.stateMask = LVIS_SELECTED;
3525 LISTVIEW_SetItemState(infoPtr, nItem, &lvItem);
3526 if (lvItem.state & LVIS_SELECTED)
3527 infoPtr->nSelectionMark = nItem;
3529 bResult = LISTVIEW_SetItemFocus(infoPtr, nItem);
3532 LISTVIEW_EnsureVisible(infoPtr, nItem, FALSE);
3535 UpdateWindow(infoPtr->hwndSelf); /* update client area */
3539 static BOOL LISTVIEW_GetItemAtPt(const LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem, POINT pt)
3541 LVHITTESTINFO lvHitTestInfo;
3543 ZeroMemory(&lvHitTestInfo, sizeof(lvHitTestInfo));
3544 lvHitTestInfo.pt.x = pt.x;
3545 lvHitTestInfo.pt.y = pt.y;
3547 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
3549 lpLVItem->mask = LVIF_PARAM;
3550 lpLVItem->iItem = lvHitTestInfo.iItem;
3551 lpLVItem->iSubItem = 0;
3553 return LISTVIEW_GetItemT(infoPtr, lpLVItem, TRUE);
3556 static inline BOOL LISTVIEW_isHotTracking(const LISTVIEW_INFO *infoPtr)
3558 return ((infoPtr->dwLvExStyle & LVS_EX_TRACKSELECT) ||
3559 (infoPtr->dwLvExStyle & LVS_EX_ONECLICKACTIVATE) ||
3560 (infoPtr->dwLvExStyle & LVS_EX_TWOCLICKACTIVATE));
3565 * Called when the mouse is being actively tracked and has hovered for a specified
3569 * [I] infoPtr : valid pointer to the listview structure
3570 * [I] fwKeys : key indicator
3571 * [I] x,y : mouse position
3574 * 0 if the message was processed, non-zero if there was an error
3577 * LVS_EX_TRACKSELECT: An item is automatically selected when the cursor remains
3578 * over the item for a certain period of time.
3581 static LRESULT LISTVIEW_MouseHover(LISTVIEW_INFO *infoPtr, WORD fwKeys, INT x, INT y)
3583 if (LISTVIEW_isHotTracking(infoPtr))
3591 if (LISTVIEW_GetItemAtPt(infoPtr, &item, pt))
3592 LISTVIEW_SetSelection(infoPtr, item.iItem);
3600 * Utility routine to draw and highlight items within a marquee selection rectangle.
3603 * [I] infoPtr : valid pointer to the listview structure
3604 * [I] coords_orig : original co-ordinates of the cursor
3605 * [I] coords_offs : offsetted coordinates of the cursor
3606 * [I] offset : offset amount
3607 * [I] scroll : Bitmask of which directions we should scroll, if at all
3612 static void LISTVIEW_MarqueeHighlight(LISTVIEW_INFO *infoPtr, LPPOINT coords_orig, LPPOINT coords_offs, LPPOINT offset, INT scroll)
3614 BOOL controlDown = FALSE;
3619 if (coords_offs->x > infoPtr->marqueeOrigin.x)
3621 rect.left = infoPtr->marqueeOrigin.x;
3622 rect.right = coords_offs->x;
3626 rect.left = coords_offs->x;
3627 rect.right = infoPtr->marqueeOrigin.x;
3630 if (coords_offs->y > infoPtr->marqueeOrigin.y)
3632 rect.top = infoPtr->marqueeOrigin.y;
3633 rect.bottom = coords_offs->y;
3637 rect.top = coords_offs->y;
3638 rect.bottom = infoPtr->marqueeOrigin.y;
3641 /* Cancel out the old marquee rectangle and draw the new one */
3642 LISTVIEW_InvalidateRect(infoPtr, &infoPtr->marqueeDrawRect);
3644 /* Invert the items in the old marquee rectangle */
3645 iterator_frameditems_absolute(&i, infoPtr, &infoPtr->marqueeRect);
3647 while (iterator_next(&i))
3651 if (LISTVIEW_GetItemState(infoPtr, i.nItem, LVIS_SELECTED) == LVIS_SELECTED)
3654 item.state = LVIS_SELECTED;
3656 item.stateMask = LVIS_SELECTED;
3658 LISTVIEW_SetItemState(infoPtr, i.nItem, &item);
3662 iterator_destroy(&i);
3664 CopyRect(&infoPtr->marqueeRect, &rect);
3666 CopyRect(&infoPtr->marqueeDrawRect, &rect);
3667 OffsetRect(&infoPtr->marqueeDrawRect, offset->x, offset->y);
3669 /* Iterate over the items within our marquee rectangle */
3670 iterator_frameditems_absolute(&i, infoPtr, &infoPtr->marqueeRect);
3672 if (GetKeyState(VK_CONTROL) & 0x8000)
3675 while (iterator_next(&i))
3679 /* If CTRL is pressed, invert. If not, always select the item. */
3680 if ((controlDown) && (LISTVIEW_GetItemState(infoPtr, i.nItem, LVIS_SELECTED)))
3683 item.state = LVIS_SELECTED;
3685 item.stateMask = LVIS_SELECTED;
3687 LISTVIEW_SetItemState(infoPtr, i.nItem, &item);
3691 iterator_destroy(&i);
3692 LISTVIEW_InvalidateRect(infoPtr, &rect);
3697 * Called whenever WM_MOUSEMOVE is received.
3700 * [I] infoPtr : valid pointer to the listview structure
3701 * [I] fwKeys : key indicator
3702 * [I] x,y : mouse position
3705 * 0 if the message is processed, non-zero if there was an error
3707 static LRESULT LISTVIEW_MouseMove(LISTVIEW_INFO *infoPtr, WORD fwKeys, INT x, INT y)
3709 TRACKMOUSEEVENT trackinfo;
3711 if (!(fwKeys & MK_LBUTTON))
3712 infoPtr->bLButtonDown = FALSE;
3714 if (infoPtr->bLButtonDown)
3718 LVHITTESTINFO lvHitTestInfo;
3719 WORD wDragWidth = GetSystemMetrics(SM_CXDRAG);
3720 WORD wDragHeight= GetSystemMetrics(SM_CYDRAG);
3722 if (infoPtr->bMarqueeSelect)
3732 LISTVIEW_GetOrigin(infoPtr, &offset);
3734 /* Ensure coordinates are within client bounds */
3735 coords_offs.x = max(min(x, infoPtr->rcList.right), 0);
3736 coords_offs.y = max(min(y, infoPtr->rcList.bottom), 0);
3738 /* Offset coordinates by the appropriate amount */
3739 coords_offs.x -= offset.x;
3740 coords_offs.y -= offset.y;
3742 LISTVIEW_MarqueeHighlight(infoPtr, &coords_orig, &coords_offs, &offset, 0);
3746 rect.left = infoPtr->ptClickPos.x - wDragWidth;
3747 rect.right = infoPtr->ptClickPos.x + wDragWidth;
3748 rect.top = infoPtr->ptClickPos.y - wDragHeight;
3749 rect.bottom = infoPtr->ptClickPos.y + wDragHeight;
3754 lvHitTestInfo.pt = tmp;
3755 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, TRUE);
3757 /* reset item marker */
3758 if (infoPtr->nLButtonDownItem != lvHitTestInfo.iItem)
3759 infoPtr->nLButtonDownItem = -1;
3761 if (!PtInRect(&rect, tmp))
3763 /* this path covers the following:
3764 1. WM_LBUTTONDOWN over selected item (sets focus on it)
3765 2. change focus with keys
3766 3. move mouse over item from step 1 selects it and moves focus on it */
3767 if (infoPtr->nLButtonDownItem != -1 &&
3768 !LISTVIEW_GetItemState(infoPtr, infoPtr->nLButtonDownItem, LVIS_SELECTED))
3772 lvItem.state = LVIS_FOCUSED | LVIS_SELECTED;
3773 lvItem.stateMask = LVIS_FOCUSED | LVIS_SELECTED;
3775 LISTVIEW_SetItemState(infoPtr, infoPtr->nLButtonDownItem, &lvItem);
3776 infoPtr->nLButtonDownItem = -1;
3779 if (!infoPtr->bDragging)
3781 lvHitTestInfo.pt = infoPtr->ptClickPos;
3782 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, TRUE);
3784 /* If the click is outside the range of an item, begin a
3785 highlight. If not, begin an item drag. */
3786 if (lvHitTestInfo.iItem == -1)
3790 /* If we're allowing multiple selections, send notification.
3791 If return value is non-zero, cancel. */
3792 if (!(infoPtr->dwStyle & LVS_SINGLESEL) && (notify_hdr(infoPtr, LVN_MARQUEEBEGIN, &hdr) == 0))
3794 /* Store the absolute coordinates of the click */
3796 LISTVIEW_GetOrigin(infoPtr, &offset);
3798 infoPtr->marqueeOrigin.x = infoPtr->ptClickPos.x - offset.x;
3799 infoPtr->marqueeOrigin.y = infoPtr->ptClickPos.y - offset.y;
3801 /* Begin selection and capture mouse */
3802 infoPtr->bMarqueeSelect = TRUE;
3803 SetCapture(infoPtr->hwndSelf);
3810 ZeroMemory(&nmlv, sizeof(nmlv));
3811 nmlv.iItem = lvHitTestInfo.iItem;
3812 nmlv.ptAction = infoPtr->ptClickPos;
3814 notify_listview(infoPtr, LVN_BEGINDRAG, &nmlv);
3815 infoPtr->bDragging = TRUE;
3823 /* see if we are supposed to be tracking mouse hovering */
3824 if (LISTVIEW_isHotTracking(infoPtr)) {
3825 /* fill in the trackinfo struct */
3826 trackinfo.cbSize = sizeof(TRACKMOUSEEVENT);
3827 trackinfo.dwFlags = TME_QUERY;
3828 trackinfo.hwndTrack = infoPtr->hwndSelf;
3829 trackinfo.dwHoverTime = infoPtr->dwHoverTime;
3831 /* see if we are already tracking this hwnd */
3832 _TrackMouseEvent(&trackinfo);
3834 if(!(trackinfo.dwFlags & TME_HOVER)) {
3835 trackinfo.dwFlags = TME_HOVER;
3837 /* call TRACKMOUSEEVENT so we receive WM_MOUSEHOVER messages */
3838 _TrackMouseEvent(&trackinfo);
3847 * Tests whether the item is assignable to a list with style lStyle
3849 static inline BOOL is_assignable_item(const LVITEMW *lpLVItem, LONG lStyle)
3851 if ( (lpLVItem->mask & LVIF_TEXT) &&
3852 (lpLVItem->pszText == LPSTR_TEXTCALLBACKW) &&
3853 (lStyle & (LVS_SORTASCENDING | LVS_SORTDESCENDING)) ) return FALSE;
3861 * Helper for LISTVIEW_SetItemT *only*: sets item attributes.
3864 * [I] infoPtr : valid pointer to the listview structure
3865 * [I] lpLVItem : valid pointer to new item attributes
3866 * [I] isNew : the item being set is being inserted
3867 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
3868 * [O] bChanged : will be set to TRUE if the item really changed
3874 static BOOL set_main_item(LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem, BOOL isNew, BOOL isW, BOOL *bChanged)
3880 /* stateMask is ignored for LVM_INSERTITEM */
3881 UINT stateMask = isNew ? ~0 : lpLVItem->stateMask;
3885 assert(lpLVItem->iItem >= 0 && lpLVItem->iItem < infoPtr->nItemCount);
3887 if (lpLVItem->mask == 0) return TRUE;
3889 if (infoPtr->dwStyle & LVS_OWNERDATA)
3891 /* a virtual listview only stores selection and focus */
3892 if (lpLVItem->mask & ~LVIF_STATE)
3898 HDPA hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, lpLVItem->iItem);
3899 lpItem = DPA_GetPtr(hdpaSubItems, 0);
3903 /* we need to get the lParam and state of the item */
3904 item.iItem = lpLVItem->iItem;
3905 item.iSubItem = lpLVItem->iSubItem;
3906 item.mask = LVIF_STATE | LVIF_PARAM;
3907 item.stateMask = (infoPtr->dwStyle & LVS_OWNERDATA) ? LVIS_FOCUSED | LVIS_SELECTED : ~0;
3911 if (!isNew && !LISTVIEW_GetItemW(infoPtr, &item)) return FALSE;
3913 TRACE("oldState=%x, newState=%x\n", item.state, lpLVItem->state);
3914 /* determine what fields will change */
3915 if ((lpLVItem->mask & LVIF_STATE) && ((item.state ^ lpLVItem->state) & stateMask & ~infoPtr->uCallbackMask))
3916 uChanged |= LVIF_STATE;
3918 if ((lpLVItem->mask & LVIF_IMAGE) && (lpItem->hdr.iImage != lpLVItem->iImage))
3919 uChanged |= LVIF_IMAGE;
3921 if ((lpLVItem->mask & LVIF_PARAM) && (lpItem->lParam != lpLVItem->lParam))
3922 uChanged |= LVIF_PARAM;
3924 if ((lpLVItem->mask & LVIF_INDENT) && (lpItem->iIndent != lpLVItem->iIndent))
3925 uChanged |= LVIF_INDENT;
3927 if ((lpLVItem->mask & LVIF_TEXT) && textcmpWT(lpItem->hdr.pszText, lpLVItem->pszText, isW))
3928 uChanged |= LVIF_TEXT;
3930 TRACE("uChanged=0x%x\n", uChanged);
3931 if (!uChanged) return TRUE;
3934 ZeroMemory(&nmlv, sizeof(NMLISTVIEW));
3935 nmlv.iItem = lpLVItem->iItem;
3936 nmlv.uNewState = (item.state & ~stateMask) | (lpLVItem->state & stateMask);
3937 nmlv.uOldState = item.state;
3938 nmlv.uChanged = uChanged;
3939 nmlv.lParam = item.lParam;
3941 /* send LVN_ITEMCHANGING notification, if the item is not being inserted */
3942 /* and we are _NOT_ virtual (LVS_OWNERDATA), and change notifications */
3944 if(lpItem && !isNew && infoPtr->bDoChangeNotify)
3946 HWND hwndSelf = infoPtr->hwndSelf;
3948 if (notify_listview(infoPtr, LVN_ITEMCHANGING, &nmlv))
3950 if (!IsWindow(hwndSelf))
3954 /* copy information */
3955 if (lpLVItem->mask & LVIF_TEXT)
3956 textsetptrT(&lpItem->hdr.pszText, lpLVItem->pszText, isW);
3958 if (lpLVItem->mask & LVIF_IMAGE)
3959 lpItem->hdr.iImage = lpLVItem->iImage;
3961 if (lpLVItem->mask & LVIF_PARAM)
3962 lpItem->lParam = lpLVItem->lParam;
3964 if (lpLVItem->mask & LVIF_INDENT)
3965 lpItem->iIndent = lpLVItem->iIndent;
3967 if (uChanged & LVIF_STATE)
3969 if (lpItem && (stateMask & ~infoPtr->uCallbackMask))
3971 lpItem->state &= ~stateMask;
3972 lpItem->state |= (lpLVItem->state & stateMask);
3974 if (lpLVItem->state & stateMask & ~infoPtr->uCallbackMask & LVIS_SELECTED)
3976 if (infoPtr->dwStyle & LVS_SINGLESEL) LISTVIEW_DeselectAllSkipItem(infoPtr, lpLVItem->iItem);
3977 ranges_additem(infoPtr->selectionRanges, lpLVItem->iItem);
3979 else if (stateMask & LVIS_SELECTED)
3981 ranges_delitem(infoPtr->selectionRanges, lpLVItem->iItem);
3983 /* if we are asked to change focus, and we manage it, do it */
3984 if (stateMask & ~infoPtr->uCallbackMask & LVIS_FOCUSED)
3986 if (lpLVItem->state & LVIS_FOCUSED)
3988 if (infoPtr->nFocusedItem != -1)
3990 /* remove current focus */
3991 item.mask = LVIF_STATE;
3993 item.stateMask = LVIS_FOCUSED;
3995 /* recurse with redrawing an item */
3996 LISTVIEW_SetItemState(infoPtr, infoPtr->nFocusedItem, &item);
3999 infoPtr->nFocusedItem = lpLVItem->iItem;
4000 LISTVIEW_EnsureVisible(infoPtr, lpLVItem->iItem, infoPtr->uView == LV_VIEW_LIST);
4002 else if (infoPtr->nFocusedItem == lpLVItem->iItem)
4004 infoPtr->nFocusedItem = -1;
4009 /* if we're inserting the item, we're done */
4010 if (isNew) return TRUE;
4012 /* send LVN_ITEMCHANGED notification */
4013 if (lpLVItem->mask & LVIF_PARAM) nmlv.lParam = lpLVItem->lParam;
4014 if (infoPtr->bDoChangeNotify) notify_listview(infoPtr, LVN_ITEMCHANGED, &nmlv);
4021 * Helper for LISTVIEW_{Set,Insert}ItemT *only*: sets subitem attributes.
4024 * [I] infoPtr : valid pointer to the listview structure
4025 * [I] lpLVItem : valid pointer to new subitem attributes
4026 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
4027 * [O] bChanged : will be set to TRUE if the item really changed
4033 static BOOL set_sub_item(const LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem, BOOL isW, BOOL *bChanged)
4036 SUBITEM_INFO *lpSubItem;
4038 /* we do not support subitems for virtual listviews */
4039 if (infoPtr->dwStyle & LVS_OWNERDATA) return FALSE;
4041 /* set subitem only if column is present */
4042 if (lpLVItem->iSubItem >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
4044 /* First do some sanity checks */
4045 /* The LVIF_STATE flag is valid for subitems, but does not appear to be
4046 particularly useful. We currently do not actually do anything with
4047 the flag on subitems.
4049 if (lpLVItem->mask & ~(LVIF_TEXT | LVIF_IMAGE | LVIF_STATE)) return FALSE;
4050 if (!(lpLVItem->mask & (LVIF_TEXT | LVIF_IMAGE | LVIF_STATE))) return TRUE;
4052 /* get the subitem structure, and create it if not there */
4053 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, lpLVItem->iItem);
4054 assert (hdpaSubItems);
4056 lpSubItem = LISTVIEW_GetSubItemPtr(hdpaSubItems, lpLVItem->iSubItem);
4059 SUBITEM_INFO *tmpSubItem;
4062 lpSubItem = Alloc(sizeof(SUBITEM_INFO));
4063 if (!lpSubItem) return FALSE;
4064 /* we could binary search here, if need be...*/
4065 for (i = 1; i < DPA_GetPtrCount(hdpaSubItems); i++)
4067 tmpSubItem = DPA_GetPtr(hdpaSubItems, i);
4068 if (tmpSubItem->iSubItem > lpLVItem->iSubItem) break;
4070 if (DPA_InsertPtr(hdpaSubItems, i, lpSubItem) == -1)
4075 lpSubItem->iSubItem = lpLVItem->iSubItem;
4076 lpSubItem->hdr.iImage = I_IMAGECALLBACK;
4080 if (lpLVItem->mask & LVIF_IMAGE)
4081 if (lpSubItem->hdr.iImage != lpLVItem->iImage)
4083 lpSubItem->hdr.iImage = lpLVItem->iImage;
4087 if ((lpLVItem->mask & LVIF_TEXT) && textcmpWT(lpSubItem->hdr.pszText, lpLVItem->pszText, isW))
4089 textsetptrT(&lpSubItem->hdr.pszText, lpLVItem->pszText, isW);
4098 * Sets item attributes.
4101 * [I] infoPtr : valid pointer to the listview structure
4102 * [I] lpLVItem : new item attributes
4103 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
4109 static BOOL LISTVIEW_SetItemT(LISTVIEW_INFO *infoPtr, LVITEMW *lpLVItem, BOOL isW)
4111 HWND hwndSelf = infoPtr->hwndSelf;
4112 LPWSTR pszText = NULL;
4113 BOOL bResult, bChanged = FALSE;
4115 TRACE("(lpLVItem=%s, isW=%d)\n", debuglvitem_t(lpLVItem, isW), isW);
4117 if (!lpLVItem || lpLVItem->iItem < 0 || lpLVItem->iItem >= infoPtr->nItemCount)
4120 /* For efficiency, we transform the lpLVItem->pszText to Unicode here */
4121 if ((lpLVItem->mask & LVIF_TEXT) && is_textW(lpLVItem->pszText))
4123 pszText = lpLVItem->pszText;
4124 lpLVItem->pszText = textdupTtoW(lpLVItem->pszText, isW);
4127 /* actually set the fields */
4128 if (!is_assignable_item(lpLVItem, infoPtr->dwStyle)) return FALSE;
4130 if (lpLVItem->iSubItem)
4131 bResult = set_sub_item(infoPtr, lpLVItem, TRUE, &bChanged);
4133 bResult = set_main_item(infoPtr, lpLVItem, FALSE, TRUE, &bChanged);
4134 if (!IsWindow(hwndSelf))
4137 /* redraw item, if necessary */
4138 if (bChanged && !infoPtr->bIsDrawing)
4140 /* this little optimization eliminates some nasty flicker */
4141 if ( infoPtr->uView == LV_VIEW_DETAILS && !(infoPtr->dwStyle & LVS_OWNERDRAWFIXED) &&
4142 !(infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) &&
4143 lpLVItem->iSubItem > 0 && lpLVItem->iSubItem <= DPA_GetPtrCount(infoPtr->hdpaColumns) )
4144 LISTVIEW_InvalidateSubItem(infoPtr, lpLVItem->iItem, lpLVItem->iSubItem);
4146 LISTVIEW_InvalidateItem(infoPtr, lpLVItem->iItem);
4151 textfreeT(lpLVItem->pszText, isW);
4152 lpLVItem->pszText = pszText;
4160 * Retrieves the index of the item at coordinate (0, 0) of the client area.
4163 * [I] infoPtr : valid pointer to the listview structure
4168 static INT LISTVIEW_GetTopIndex(const LISTVIEW_INFO *infoPtr)
4171 SCROLLINFO scrollInfo;
4173 scrollInfo.cbSize = sizeof(SCROLLINFO);
4174 scrollInfo.fMask = SIF_POS;
4176 if (infoPtr->uView == LV_VIEW_LIST)
4178 if (GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo))
4179 nItem = scrollInfo.nPos * LISTVIEW_GetCountPerColumn(infoPtr);
4181 else if (infoPtr->uView == LV_VIEW_DETAILS)
4183 if (GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
4184 nItem = scrollInfo.nPos;
4188 if (GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
4189 nItem = LISTVIEW_GetCountPerRow(infoPtr) * (scrollInfo.nPos / infoPtr->nItemHeight);
4192 TRACE("nItem=%d\n", nItem);
4200 * Erases the background of the given rectangle
4203 * [I] infoPtr : valid pointer to the listview structure
4204 * [I] hdc : device context handle
4205 * [I] lprcBox : clipping rectangle
4211 static inline BOOL LISTVIEW_FillBkgnd(const LISTVIEW_INFO *infoPtr, HDC hdc, const RECT *lprcBox)
4213 if (!infoPtr->hBkBrush) return FALSE;
4215 TRACE("(hdc=%p, lprcBox=%s, hBkBrush=%p)\n", hdc, wine_dbgstr_rect(lprcBox), infoPtr->hBkBrush);
4217 return FillRect(hdc, lprcBox, infoPtr->hBkBrush);
4225 * [I] infoPtr : valid pointer to the listview structure
4226 * [I] hdc : device context handle
4227 * [I] nItem : item index
4228 * [I] nSubItem : subitem index
4229 * [I] pos : item position in client coordinates
4230 * [I] cdmode : custom draw mode
4236 static BOOL LISTVIEW_DrawItem(LISTVIEW_INFO *infoPtr, HDC hdc, INT nItem, INT nSubItem, POINT pos, DWORD cdmode)
4239 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
4240 static WCHAR szCallback[] = { '(', 'c', 'a', 'l', 'l', 'b', 'a', 'c', 'k', ')', 0 };
4241 DWORD cdsubitemmode = CDRF_DODEFAULT;
4243 RECT rcSelect, rcBox, rcIcon, rcLabel, rcStateIcon;
4244 NMLVCUSTOMDRAW nmlvcd;
4249 TRACE("(hdc=%p, nItem=%d, nSubItem=%d, pos=%s)\n", hdc, nItem, nSubItem, wine_dbgstr_point(&pos));
4251 /* get information needed for drawing the item */
4252 lvItem.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_PARAM;
4253 if (nSubItem == 0) lvItem.mask |= LVIF_STATE;
4254 if (infoPtr->uView == LV_VIEW_DETAILS) lvItem.mask |= LVIF_INDENT;
4255 lvItem.stateMask = LVIS_SELECTED | LVIS_FOCUSED | LVIS_STATEIMAGEMASK;
4256 lvItem.iItem = nItem;
4257 lvItem.iSubItem = nSubItem;
4260 lvItem.cchTextMax = DISP_TEXT_SIZE;
4261 lvItem.pszText = szDispText;
4262 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return FALSE;
4263 if (nSubItem > 0 && (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT))
4264 lvItem.state = LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED);
4265 if (lvItem.pszText == LPSTR_TEXTCALLBACKW) lvItem.pszText = szCallback;
4266 TRACE(" lvItem=%s\n", debuglvitem_t(&lvItem, TRUE));
4268 /* now check if we need to update the focus rectangle */
4269 lprcFocus = infoPtr->bFocus && (lvItem.state & LVIS_FOCUSED) ? &infoPtr->rcFocus : 0;
4271 if (!lprcFocus) lvItem.state &= ~LVIS_FOCUSED;
4272 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, &rcBox, &rcSelect, &rcIcon, &rcStateIcon, &rcLabel);
4273 OffsetRect(&rcBox, pos.x, pos.y);
4274 OffsetRect(&rcSelect, pos.x, pos.y);
4275 OffsetRect(&rcIcon, pos.x, pos.y);
4276 OffsetRect(&rcStateIcon, pos.x, pos.y);
4277 OffsetRect(&rcLabel, pos.x, pos.y);
4278 TRACE(" rcBox=%s, rcSelect=%s, rcIcon=%s. rcLabel=%s\n",
4279 wine_dbgstr_rect(&rcBox), wine_dbgstr_rect(&rcSelect),
4280 wine_dbgstr_rect(&rcIcon), wine_dbgstr_rect(&rcLabel));
4282 /* fill in the custom draw structure */
4283 customdraw_fill(&nmlvcd, infoPtr, hdc, &rcBox, &lvItem);
4285 hOldFont = GetCurrentObject(hdc, OBJ_FONT);
4286 if (nSubItem > 0) cdmode = infoPtr->cditemmode;
4287 if (cdmode & CDRF_SKIPDEFAULT) goto postpaint;
4288 if (cdmode & CDRF_NOTIFYITEMDRAW)
4289 cdsubitemmode = notify_customdraw(infoPtr, CDDS_PREPAINT, &nmlvcd);
4290 if (nSubItem == 0) infoPtr->cditemmode = cdsubitemmode;
4291 if (cdsubitemmode & CDRF_SKIPDEFAULT) goto postpaint;
4292 /* we have to send a CDDS_SUBITEM customdraw explicitly for subitem 0 */
4293 if (nSubItem == 0 && cdsubitemmode == CDRF_NOTIFYITEMDRAW)
4295 cdsubitemmode = notify_customdraw(infoPtr, CDDS_SUBITEM | CDDS_ITEMPREPAINT, &nmlvcd);
4296 if (cdsubitemmode & CDRF_SKIPDEFAULT) goto postpaint;
4298 if (nSubItem == 0 || (cdmode & CDRF_NOTIFYITEMDRAW))
4299 prepaint_setup(infoPtr, hdc, &nmlvcd, FALSE);
4300 else if ((infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) == FALSE)
4301 prepaint_setup(infoPtr, hdc, &nmlvcd, TRUE);
4303 /* in full row select, subitems, will just use main item's colors */
4304 if (nSubItem && infoPtr->uView == LV_VIEW_DETAILS && (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT))
4305 nmlvcd.clrTextBk = CLR_NONE;
4307 /* FIXME: temporary hack */
4308 rcSelect.left = rcLabel.left;
4310 /* draw the selection background, if we're drawing the main item */
4313 /* in icon mode, the label rect is really what we want to draw the
4315 if (infoPtr->uView == LV_VIEW_ICON)
4318 if (infoPtr->uView == LV_VIEW_DETAILS && (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT))
4320 /* we have to update left focus bound too if item isn't in leftmost column
4321 and reduce right box bound */
4322 if (DPA_GetPtrCount(infoPtr->hdpaColumns) > 0)
4326 if ((leftmost = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, 0, 0)))
4328 INT Originx = pos.x - LISTVIEW_GetColumnInfo(infoPtr, 0)->rcHeader.left;
4329 INT index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX,
4330 DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, 0);
4332 rcBox.right = LISTVIEW_GetColumnInfo(infoPtr, index)->rcHeader.right + Originx;
4333 rcSelect.left = LISTVIEW_GetColumnInfo(infoPtr, leftmost)->rcHeader.left + Originx;
4337 rcSelect.right = rcBox.right;
4340 if (nmlvcd.clrTextBk != CLR_NONE)
4341 ExtTextOutW(hdc, rcSelect.left, rcSelect.top, ETO_OPAQUE, &rcSelect, NULL, 0, NULL);
4342 /* store new focus rectangle */
4343 if (infoPtr->nFocusedItem == nItem) infoPtr->rcFocus = rcSelect;
4347 if (infoPtr->himlState && STATEIMAGEINDEX(lvItem.state) && (nSubItem == 0))
4349 UINT uStateImage = STATEIMAGEINDEX(lvItem.state);
4352 TRACE("uStateImage=%d\n", uStateImage);
4353 ImageList_Draw(infoPtr->himlState, uStateImage - 1, hdc,
4354 rcStateIcon.left, rcStateIcon.top, ILD_NORMAL);
4359 himl = (infoPtr->uView == LV_VIEW_ICON ? infoPtr->himlNormal : infoPtr->himlSmall);
4360 if (himl && lvItem.iImage >= 0 && !IsRectEmpty(&rcIcon))
4362 TRACE("iImage=%d\n", lvItem.iImage);
4363 ImageList_DrawEx(himl, lvItem.iImage, hdc, rcIcon.left, rcIcon.top,
4364 rcIcon.right - rcIcon.left, rcIcon.bottom - rcIcon.top, infoPtr->clrBk, CLR_DEFAULT,
4365 (lvItem.state & LVIS_SELECTED) && (infoPtr->bFocus) ? ILD_SELECTED : ILD_NORMAL);
4368 /* Don't bother painting item being edited */
4369 if (infoPtr->hwndEdit && nItem == infoPtr->nEditLabelItem && nSubItem == 0) goto postpaint;
4371 /* figure out the text drawing flags */
4372 uFormat = (infoPtr->uView == LV_VIEW_ICON ? (lprcFocus ? LV_FL_DT_FLAGS : LV_ML_DT_FLAGS) : LV_SL_DT_FLAGS);
4373 if (infoPtr->uView == LV_VIEW_ICON)
4374 uFormat = (lprcFocus ? LV_FL_DT_FLAGS : LV_ML_DT_FLAGS);
4377 switch (LISTVIEW_GetColumnInfo(infoPtr, nSubItem)->fmt & LVCFMT_JUSTIFYMASK)
4379 case LVCFMT_RIGHT: uFormat |= DT_RIGHT; break;
4380 case LVCFMT_CENTER: uFormat |= DT_CENTER; break;
4381 default: uFormat |= DT_LEFT;
4384 if (!(uFormat & (DT_RIGHT | DT_CENTER)))
4386 if (himl && lvItem.iImage >= 0 && !IsRectEmpty(&rcIcon)) rcLabel.left += IMAGE_PADDING;
4387 else rcLabel.left += LABEL_HOR_PADDING;
4389 else if (uFormat & DT_RIGHT) rcLabel.right -= LABEL_HOR_PADDING;
4391 /* for GRIDLINES reduce the bottom so the text formats correctly */
4392 if (infoPtr->uView == LV_VIEW_DETAILS && infoPtr->dwLvExStyle & LVS_EX_GRIDLINES)
4395 DrawTextW(hdc, lvItem.pszText, -1, &rcLabel, uFormat);
4398 if (cdsubitemmode & CDRF_NOTIFYPOSTPAINT)
4399 notify_postpaint(infoPtr, &nmlvcd);
4400 if (cdsubitemmode & CDRF_NEWFONT)
4401 SelectObject(hdc, hOldFont);
4407 * Draws listview items when in owner draw mode.
4410 * [I] infoPtr : valid pointer to the listview structure
4411 * [I] hdc : device context handle
4416 static void LISTVIEW_RefreshOwnerDraw(const LISTVIEW_INFO *infoPtr, ITERATOR *i, HDC hdc, DWORD cdmode)
4418 UINT uID = (UINT)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
4419 DWORD cditemmode = CDRF_DODEFAULT;
4420 NMLVCUSTOMDRAW nmlvcd;
4421 POINT Origin, Position;
4427 ZeroMemory(&dis, sizeof(dis));
4429 /* Get scroll info once before loop */
4430 LISTVIEW_GetOrigin(infoPtr, &Origin);
4432 /* iterate through the invalidated rows */
4433 while(iterator_next(i))
4435 item.iItem = i->nItem;
4437 item.mask = LVIF_PARAM | LVIF_STATE;
4438 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
4439 if (!LISTVIEW_GetItemW(infoPtr, &item)) continue;
4441 dis.CtlType = ODT_LISTVIEW;
4443 dis.itemID = item.iItem;
4444 dis.itemAction = ODA_DRAWENTIRE;
4446 if (item.state & LVIS_SELECTED) dis.itemState |= ODS_SELECTED;
4447 if (infoPtr->bFocus && (item.state & LVIS_FOCUSED)) dis.itemState |= ODS_FOCUS;
4448 dis.hwndItem = infoPtr->hwndSelf;
4450 LISTVIEW_GetItemOrigin(infoPtr, dis.itemID, &Position);
4451 dis.rcItem.left = Position.x + Origin.x;
4452 dis.rcItem.right = dis.rcItem.left + infoPtr->nItemWidth;
4453 dis.rcItem.top = Position.y + Origin.y;
4454 dis.rcItem.bottom = dis.rcItem.top + infoPtr->nItemHeight;
4455 dis.itemData = item.lParam;
4457 TRACE("item=%s, rcItem=%s\n", debuglvitem_t(&item, TRUE), wine_dbgstr_rect(&dis.rcItem));
4460 * Even if we do not send the CDRF_NOTIFYITEMDRAW we need to fill the nmlvcd
4461 * structure for the rest. of the paint cycle
4463 customdraw_fill(&nmlvcd, infoPtr, hdc, &dis.rcItem, &item);
4464 if (cdmode & CDRF_NOTIFYITEMDRAW)
4465 cditemmode = notify_customdraw(infoPtr, CDDS_PREPAINT, &nmlvcd);
4467 if (!(cditemmode & CDRF_SKIPDEFAULT))
4469 prepaint_setup (infoPtr, hdc, &nmlvcd, FALSE);
4470 SendMessageW(infoPtr->hwndNotify, WM_DRAWITEM, dis.CtlID, (LPARAM)&dis);
4473 if (cditemmode & CDRF_NOTIFYPOSTPAINT)
4474 notify_postpaint(infoPtr, &nmlvcd);
4480 * Draws listview items when in report display mode.
4483 * [I] infoPtr : valid pointer to the listview structure
4484 * [I] hdc : device context handle
4485 * [I] cdmode : custom draw mode
4490 static void LISTVIEW_RefreshReport(LISTVIEW_INFO *infoPtr, ITERATOR *i, HDC hdc, DWORD cdmode)
4493 RECT rcClip, rcItem;
4494 POINT Origin, Position;
4501 /* figure out what to draw */
4502 rgntype = GetClipBox(hdc, &rcClip);
4503 if (rgntype == NULLREGION) return;
4505 /* Get scroll info once before loop */
4506 LISTVIEW_GetOrigin(infoPtr, &Origin);
4508 colRanges = ranges_create(DPA_GetPtrCount(infoPtr->hdpaColumns));
4510 /* narrow down the columns we need to paint */
4511 for(col = 0; col < DPA_GetPtrCount(infoPtr->hdpaColumns); col++)
4513 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, col, 0);
4515 LISTVIEW_GetHeaderRect(infoPtr, index, &rcItem);
4516 if ((rcItem.right + Origin.x >= rcClip.left) && (rcItem.left + Origin.x < rcClip.right))
4517 ranges_additem(colRanges, index);
4519 iterator_rangesitems(&j, colRanges);
4521 /* in full row select, we _have_ to draw the main item */
4522 if (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT)
4525 /* iterate through the invalidated rows */
4526 while(iterator_next(i))
4528 LISTVIEW_GetItemOrigin(infoPtr, i->nItem, &Position);
4529 Position.y += Origin.y;
4531 /* iterate through the invalidated columns */
4532 while(iterator_next(&j))
4534 LISTVIEW_GetHeaderRect(infoPtr, j.nItem, &rcItem);
4535 Position.x = (j.nItem == 0) ? rcItem.left + Origin.x : Origin.x;
4537 if (rgntype == COMPLEXREGION && !((infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) && j.nItem == 0))
4540 rcItem.bottom = infoPtr->nItemHeight;
4541 OffsetRect(&rcItem, Origin.x, Position.y);
4542 if (!RectVisible(hdc, &rcItem)) continue;
4545 LISTVIEW_DrawItem(infoPtr, hdc, i->nItem, j.nItem, Position, cdmode);
4548 iterator_destroy(&j);
4553 * Draws the gridlines if necessary when in report display mode.
4556 * [I] infoPtr : valid pointer to the listview structure
4557 * [I] hdc : device context handle
4562 static void LISTVIEW_RefreshReportGrid(LISTVIEW_INFO *infoPtr, HDC hdc)
4568 RECT rcClip, rcItem = {0};
4576 /* figure out what to draw */
4577 rgntype = GetClipBox(hdc, &rcClip);
4578 if (rgntype == NULLREGION) return;
4580 /* Get scroll info once before loop */
4581 LISTVIEW_GetOrigin(infoPtr, &Origin);
4583 colRanges = ranges_create(DPA_GetPtrCount(infoPtr->hdpaColumns));
4585 /* narrow down the columns we need to paint */
4586 for(col = 0; col < DPA_GetPtrCount(infoPtr->hdpaColumns); col++)
4588 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, col, 0);
4590 LISTVIEW_GetHeaderRect(infoPtr, index, &rcItem);
4591 if ((rcItem.right + Origin.x >= rcClip.left) && (rcItem.left + Origin.x < rcClip.right))
4592 ranges_additem(colRanges, index);
4595 /* is right most vertical line visible? */
4596 if (DPA_GetPtrCount(infoPtr->hdpaColumns) > 0)
4598 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, 0);
4599 LISTVIEW_GetHeaderRect(infoPtr, index, &rcItem);
4600 rmost = (rcItem.right + Origin.x < rcClip.right);
4603 if ((hPen = CreatePen( PS_SOLID, 1, comctl32_color.clr3dFace )))
4605 hOldPen = SelectObject ( hdc, hPen );
4607 /* draw the vertical lines for the columns */
4608 iterator_rangesitems(&j, colRanges);
4609 while(iterator_next(&j))
4611 LISTVIEW_GetHeaderRect(infoPtr, j.nItem, &rcItem);
4612 if (rcItem.left == 0) continue; /* skip leftmost column */
4613 rcItem.left += Origin.x;
4614 rcItem.right += Origin.x;
4615 rcItem.top = infoPtr->rcList.top;
4616 rcItem.bottom = infoPtr->rcList.bottom;
4617 TRACE("vert col=%d, rcItem=%s\n", j.nItem, wine_dbgstr_rect(&rcItem));
4618 MoveToEx (hdc, rcItem.left, rcItem.top, NULL);
4619 LineTo (hdc, rcItem.left, rcItem.bottom);
4621 iterator_destroy(&j);
4622 /* draw rightmost grid line if visible */
4625 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX,
4626 DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, 0);
4627 LISTVIEW_GetHeaderRect(infoPtr, index, &rcItem);
4629 rcItem.right += Origin.x;
4631 MoveToEx (hdc, rcItem.right, infoPtr->rcList.top, NULL);
4632 LineTo (hdc, rcItem.right, infoPtr->rcList.bottom);
4635 /* draw the horizontial lines for the rows */
4636 itemheight = LISTVIEW_CalculateItemHeight(infoPtr);
4637 rcItem.left = infoPtr->rcList.left;
4638 rcItem.right = infoPtr->rcList.right;
4639 rcItem.bottom = rcItem.top = Origin.y - 1;
4640 MoveToEx(hdc, rcItem.left, rcItem.top, NULL);
4641 LineTo(hdc, rcItem.right, rcItem.top);
4642 for(y=itemheight-1+Origin.y; y<=infoPtr->rcList.bottom; y+=itemheight)
4644 rcItem.bottom = rcItem.top = y;
4645 TRACE("horz rcItem=%s\n", wine_dbgstr_rect(&rcItem));
4646 MoveToEx (hdc, rcItem.left, rcItem.top, NULL);
4647 LineTo (hdc, rcItem.right, rcItem.top);
4650 SelectObject( hdc, hOldPen );
4651 DeleteObject( hPen );
4657 * Draws listview items when in list display mode.
4660 * [I] infoPtr : valid pointer to the listview structure
4661 * [I] hdc : device context handle
4662 * [I] cdmode : custom draw mode
4667 static void LISTVIEW_RefreshList(LISTVIEW_INFO *infoPtr, ITERATOR *i, HDC hdc, DWORD cdmode)
4669 POINT Origin, Position;
4671 /* Get scroll info once before loop */
4672 LISTVIEW_GetOrigin(infoPtr, &Origin);
4674 while(iterator_prev(i))
4676 LISTVIEW_GetItemOrigin(infoPtr, i->nItem, &Position);
4677 Position.x += Origin.x;
4678 Position.y += Origin.y;
4680 LISTVIEW_DrawItem(infoPtr, hdc, i->nItem, 0, Position, cdmode);
4687 * Draws listview items.
4690 * [I] infoPtr : valid pointer to the listview structure
4691 * [I] hdc : device context handle
4692 * [I] prcErase : rect to be erased before refresh (may be NULL)
4697 static void LISTVIEW_Refresh(LISTVIEW_INFO *infoPtr, HDC hdc, const RECT *prcErase)
4699 COLORREF oldTextColor = 0, oldBkColor = 0, oldClrTextBk, oldClrText;
4700 NMLVCUSTOMDRAW nmlvcd;
4707 HBITMAP hbmp = NULL;
4710 LISTVIEW_DUMP(infoPtr);
4712 if (infoPtr->dwLvExStyle & LVS_EX_DOUBLEBUFFER) {
4713 TRACE("double buffering\n");
4715 hdc = CreateCompatibleDC(hdcOrig);
4717 ERR("Failed to create DC for backbuffer\n");
4720 hbmp = CreateCompatibleBitmap(hdcOrig, infoPtr->rcList.right,
4721 infoPtr->rcList.bottom);
4723 ERR("Failed to create bitmap for backbuffer\n");
4728 SelectObject(hdc, hbmp);
4729 SelectObject(hdc, infoPtr->hFont);
4731 /* Save dc values we're gonna trash while drawing
4732 * FIXME: Should be done in LISTVIEW_DrawItem() */
4733 hOldFont = SelectObject(hdc, infoPtr->hFont);
4734 oldBkMode = GetBkMode(hdc);
4735 oldBkColor = GetBkColor(hdc);
4736 oldTextColor = GetTextColor(hdc);
4739 infoPtr->bIsDrawing = TRUE;
4742 LISTVIEW_FillBkgnd(infoPtr, hdc, prcErase);
4743 } else if (infoPtr->dwLvExStyle & LVS_EX_DOUBLEBUFFER) {
4744 /* If no erasing was done (usually because RedrawWindow was called
4745 * with RDW_INVALIDATE only) we need to copy the old contents into
4746 * the backbuffer before continuing. */
4747 BitBlt(hdc, infoPtr->rcList.left, infoPtr->rcList.top,
4748 infoPtr->rcList.right - infoPtr->rcList.left,
4749 infoPtr->rcList.bottom - infoPtr->rcList.top,
4750 hdcOrig, infoPtr->rcList.left, infoPtr->rcList.top, SRCCOPY);
4753 /* FIXME: Shouldn't need to do this */
4754 oldClrTextBk = infoPtr->clrTextBk;
4755 oldClrText = infoPtr->clrText;
4757 infoPtr->cditemmode = CDRF_DODEFAULT;
4759 GetClientRect(infoPtr->hwndSelf, &rcClient);
4760 customdraw_fill(&nmlvcd, infoPtr, hdc, &rcClient, 0);
4761 cdmode = notify_customdraw(infoPtr, CDDS_PREPAINT, &nmlvcd);
4762 if (cdmode & CDRF_SKIPDEFAULT) goto enddraw;
4763 prepaint_setup(infoPtr, hdc, &nmlvcd, FALSE);
4765 /* Use these colors to draw the items */
4766 infoPtr->clrTextBk = nmlvcd.clrTextBk;
4767 infoPtr->clrText = nmlvcd.clrText;
4769 /* nothing to draw */
4770 if(infoPtr->nItemCount == 0) goto enddraw;
4772 /* figure out what we need to draw */
4773 iterator_visibleitems(&i, infoPtr, hdc);
4774 range = iterator_range(&i);
4776 /* send cache hint notification */
4777 if (infoPtr->dwStyle & LVS_OWNERDATA)
4781 ZeroMemory(&nmlv, sizeof(NMLVCACHEHINT));
4782 nmlv.iFrom = range.lower;
4783 nmlv.iTo = range.upper - 1;
4784 notify_hdr(infoPtr, LVN_ODCACHEHINT, &nmlv.hdr);
4787 if ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (infoPtr->uView == LV_VIEW_DETAILS))
4788 LISTVIEW_RefreshOwnerDraw(infoPtr, &i, hdc, cdmode);
4791 if (infoPtr->uView == LV_VIEW_DETAILS)
4792 LISTVIEW_RefreshReport(infoPtr, &i, hdc, cdmode);
4793 else /* LV_VIEW_LIST, LV_VIEW_ICON or LV_VIEW_SMALLICON */
4794 LISTVIEW_RefreshList(infoPtr, &i, hdc, cdmode);
4796 /* if we have a focus rect and it's visible, draw it */
4797 if (infoPtr->bFocus && range.lower <= infoPtr->nFocusedItem &&
4798 (range.upper - 1) >= infoPtr->nFocusedItem)
4799 LISTVIEW_DrawFocusRect(infoPtr, hdc);
4801 iterator_destroy(&i);
4804 /* For LVS_EX_GRIDLINES go and draw lines */
4805 /* This includes the case where there were *no* items */
4806 if ((infoPtr->uView == LV_VIEW_DETAILS) && infoPtr->dwLvExStyle & LVS_EX_GRIDLINES)
4807 LISTVIEW_RefreshReportGrid(infoPtr, hdc);
4809 /* Draw marquee rectangle if appropriate */
4810 if (infoPtr->bMarqueeSelect)
4811 DrawFocusRect(hdc, &infoPtr->marqueeDrawRect);
4813 if (cdmode & CDRF_NOTIFYPOSTPAINT)
4814 notify_postpaint(infoPtr, &nmlvcd);
4816 infoPtr->clrTextBk = oldClrTextBk;
4817 infoPtr->clrText = oldClrText;
4820 BitBlt(hdcOrig, infoPtr->rcList.left, infoPtr->rcList.top,
4821 infoPtr->rcList.right - infoPtr->rcList.left,
4822 infoPtr->rcList.bottom - infoPtr->rcList.top,
4823 hdc, infoPtr->rcList.left, infoPtr->rcList.top, SRCCOPY);
4828 SelectObject(hdc, hOldFont);
4829 SetBkMode(hdc, oldBkMode);
4830 SetBkColor(hdc, oldBkColor);
4831 SetTextColor(hdc, oldTextColor);
4834 infoPtr->bIsDrawing = FALSE;
4840 * Calculates the approximate width and height of a given number of items.
4843 * [I] infoPtr : valid pointer to the listview structure
4844 * [I] nItemCount : number of items
4845 * [I] wWidth : width
4846 * [I] wHeight : height
4849 * Returns a DWORD. The width in the low word and the height in high word.
4851 static DWORD LISTVIEW_ApproximateViewRect(const LISTVIEW_INFO *infoPtr, INT nItemCount,
4852 WORD wWidth, WORD wHeight)
4854 INT nItemCountPerColumn = 1;
4855 INT nColumnCount = 0;
4856 DWORD dwViewRect = 0;
4858 if (nItemCount == -1)
4859 nItemCount = infoPtr->nItemCount;
4861 if (infoPtr->uView == LV_VIEW_LIST)
4863 if (wHeight == 0xFFFF)
4865 /* use current height */
4866 wHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
4869 if (wHeight < infoPtr->nItemHeight)
4870 wHeight = infoPtr->nItemHeight;
4874 if (infoPtr->nItemHeight > 0)
4876 nItemCountPerColumn = wHeight / infoPtr->nItemHeight;
4877 if (nItemCountPerColumn == 0)
4878 nItemCountPerColumn = 1;
4880 if (nItemCount % nItemCountPerColumn != 0)
4881 nColumnCount = nItemCount / nItemCountPerColumn;
4883 nColumnCount = nItemCount / nItemCountPerColumn + 1;
4887 /* Microsoft padding magic */
4888 wHeight = nItemCountPerColumn * infoPtr->nItemHeight + 2;
4889 wWidth = nColumnCount * infoPtr->nItemWidth + 2;
4891 dwViewRect = MAKELONG(wWidth, wHeight);
4893 else if (infoPtr->uView == LV_VIEW_DETAILS)
4897 if (infoPtr->nItemCount > 0)
4899 LISTVIEW_GetItemBox(infoPtr, 0, &rcBox);
4900 wWidth = rcBox.right - rcBox.left;
4901 wHeight = (rcBox.bottom - rcBox.top) * nItemCount;
4905 /* use current height and width */
4906 if (wHeight == 0xffff)
4907 wHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
4908 if (wWidth == 0xffff)
4909 wWidth = infoPtr->rcList.right - infoPtr->rcList.left;
4912 dwViewRect = MAKELONG(wWidth, wHeight);
4914 else if (infoPtr->uView == LV_VIEW_ICON)
4920 nItemWidth = infoPtr->iconSpacing.cx;
4921 nItemHeight = infoPtr->iconSpacing.cy;
4923 if (nItemCount == -1)
4924 nItemCount = infoPtr->nItemCount;
4926 if (wWidth == 0xffff)
4927 wWidth = infoPtr->rcList.right - infoPtr->rcList.left;
4929 if (wWidth < nItemWidth)
4930 wWidth = nItemWidth;
4932 cols = wWidth / nItemWidth;
4933 if (cols > nItemCount)
4940 rows = nItemCount / cols;
4941 if (nItemCount % cols)
4947 wHeight = (nItemHeight * rows)+2;
4948 wWidth = (nItemWidth * cols)+2;
4950 dwViewRect = MAKELONG(wWidth, wHeight);
4952 else if (infoPtr->uView == LV_VIEW_SMALLICON)
4953 FIXME("uView == LV_VIEW_SMALLICON: not implemented\n");
4960 * Cancel edit label with saving item text.
4963 * [I] infoPtr : valid pointer to the listview structure
4966 * Always returns TRUE.
4968 static LRESULT LISTVIEW_CancelEditLabel(LISTVIEW_INFO *infoPtr)
4970 /* handle value will be lost after LISTVIEW_EndEditLabelT */
4971 HWND edit = infoPtr->hwndEdit;
4973 LISTVIEW_EndEditLabelT(infoPtr, TRUE, IsWindowUnicode(infoPtr->hwndEdit));
4974 SendMessageW(edit, WM_CLOSE, 0, 0);
4981 * Create a drag image list for the specified item.
4984 * [I] infoPtr : valid pointer to the listview structure
4985 * [I] iItem : index of item
4986 * [O] lppt : Upper-left corner of the image
4989 * Returns a handle to the image list if successful, NULL otherwise.
4991 static HIMAGELIST LISTVIEW_CreateDragImage(LISTVIEW_INFO *infoPtr, INT iItem, LPPOINT lppt)
4997 HBITMAP hbmp, hOldbmp;
4998 HIMAGELIST dragList = 0;
4999 TRACE("iItem=%d Count=%d\n", iItem, infoPtr->nItemCount);
5001 if (iItem < 0 || iItem >= infoPtr->nItemCount)
5004 rcItem.left = LVIR_BOUNDS;
5005 if (!LISTVIEW_GetItemRect(infoPtr, iItem, &rcItem))
5008 lppt->x = rcItem.left;
5009 lppt->y = rcItem.top;
5011 size.cx = rcItem.right - rcItem.left;
5012 size.cy = rcItem.bottom - rcItem.top;
5014 hdcOrig = GetDC(infoPtr->hwndSelf);
5015 hdc = CreateCompatibleDC(hdcOrig);
5016 hbmp = CreateCompatibleBitmap(hdcOrig, size.cx, size.cy);
5017 hOldbmp = SelectObject(hdc, hbmp);
5019 rcItem.left = rcItem.top = 0;
5020 rcItem.right = size.cx;
5021 rcItem.bottom = size.cy;
5022 FillRect(hdc, &rcItem, infoPtr->hBkBrush);
5025 if (LISTVIEW_DrawItem(infoPtr, hdc, iItem, 0, pos, infoPtr->cditemmode))
5027 dragList = ImageList_Create(size.cx, size.cy, ILC_COLOR, 10, 10);
5028 SelectObject(hdc, hOldbmp);
5029 ImageList_Add(dragList, hbmp, 0);
5032 SelectObject(hdc, hOldbmp);
5036 ReleaseDC(infoPtr->hwndSelf, hdcOrig);
5038 TRACE("ret=%p\n", dragList);
5046 * Removes all listview items and subitems.
5049 * [I] infoPtr : valid pointer to the listview structure
5055 static BOOL LISTVIEW_DeleteAllItems(LISTVIEW_INFO *infoPtr, BOOL destroy)
5058 HDPA hdpaSubItems = NULL;
5067 /* we do it directly, to avoid notifications */
5068 ranges_clear(infoPtr->selectionRanges);
5069 infoPtr->nSelectionMark = -1;
5070 infoPtr->nFocusedItem = -1;
5071 SetRectEmpty(&infoPtr->rcFocus);
5072 /* But we are supposed to leave nHotItem as is! */
5075 /* send LVN_DELETEALLITEMS notification */
5076 ZeroMemory(&nmlv, sizeof(NMLISTVIEW));
5078 bSuppress = notify_listview(infoPtr, LVN_DELETEALLITEMS, &nmlv);
5080 for (i = infoPtr->nItemCount - 1; i >= 0; i--)
5082 if (!(infoPtr->dwStyle & LVS_OWNERDATA))
5084 /* send LVN_DELETEITEM notification, if not suppressed
5085 and if it is not a virtual listview */
5086 if (!bSuppress) notify_deleteitem(infoPtr, i);
5087 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, i);
5088 lpItem = DPA_GetPtr(hdpaSubItems, 0);
5089 /* free id struct */
5090 j = DPA_GetPtrIndex(infoPtr->hdpaItemIds, lpItem->id);
5091 lpID = DPA_GetPtr(infoPtr->hdpaItemIds, j);
5092 DPA_DeletePtr(infoPtr->hdpaItemIds, j);
5094 /* both item and subitem start with ITEMHDR header */
5095 for (j = 0; j < DPA_GetPtrCount(hdpaSubItems); j++)
5097 hdrItem = DPA_GetPtr(hdpaSubItems, j);
5098 if (is_textW(hdrItem->pszText)) Free(hdrItem->pszText);
5101 DPA_Destroy(hdpaSubItems);
5102 DPA_DeletePtr(infoPtr->hdpaItems, i);
5104 DPA_DeletePtr(infoPtr->hdpaPosX, i);
5105 DPA_DeletePtr(infoPtr->hdpaPosY, i);
5106 infoPtr->nItemCount --;
5111 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
5112 LISTVIEW_UpdateScroll(infoPtr);
5114 LISTVIEW_InvalidateList(infoPtr);
5121 * Scrolls, and updates the columns, when a column is changing width.
5124 * [I] infoPtr : valid pointer to the listview structure
5125 * [I] nColumn : column to scroll
5126 * [I] dx : amount of scroll, in pixels
5131 static void LISTVIEW_ScrollColumns(LISTVIEW_INFO *infoPtr, INT nColumn, INT dx)
5133 COLUMN_INFO *lpColumnInfo;
5139 if (nColumn < 0 || DPA_GetPtrCount(infoPtr->hdpaColumns) < 1) return;
5140 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, min(nColumn, DPA_GetPtrCount(infoPtr->hdpaColumns) - 1));
5141 rcCol = lpColumnInfo->rcHeader;
5142 if (nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns))
5143 rcCol.left = rcCol.right;
5145 /* adjust the other columns */
5146 hdi.mask = HDI_ORDER;
5147 if (SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nColumn, (LPARAM)&hdi))
5149 INT nOrder = hdi.iOrder;
5150 for (nCol = 0; nCol < DPA_GetPtrCount(infoPtr->hdpaColumns); nCol++)
5152 hdi.mask = HDI_ORDER;
5153 SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nCol, (LPARAM)&hdi);
5154 if (hdi.iOrder >= nOrder) {
5155 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, nCol);
5156 lpColumnInfo->rcHeader.left += dx;
5157 lpColumnInfo->rcHeader.right += dx;
5162 /* do not update screen if not in report mode */
5163 if (!is_redrawing(infoPtr) || infoPtr->uView != LV_VIEW_DETAILS) return;
5165 /* Need to reset the item width when inserting a new column */
5166 infoPtr->nItemWidth += dx;
5168 LISTVIEW_UpdateScroll(infoPtr);
5169 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
5171 /* scroll to cover the deleted column, and invalidate for redraw */
5172 rcOld = infoPtr->rcList;
5173 rcOld.left = ptOrigin.x + rcCol.left + dx;
5174 ScrollWindowEx(infoPtr->hwndSelf, dx, 0, &rcOld, &rcOld, 0, 0, SW_ERASE | SW_INVALIDATE);
5179 * Removes a column from the listview control.
5182 * [I] infoPtr : valid pointer to the listview structure
5183 * [I] nColumn : column index
5189 static BOOL LISTVIEW_DeleteColumn(LISTVIEW_INFO *infoPtr, INT nColumn)
5193 TRACE("nColumn=%d\n", nColumn);
5195 if (nColumn < 0 || DPA_GetPtrCount(infoPtr->hdpaColumns) == 0
5196 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
5198 /* While the MSDN specifically says that column zero should not be deleted,
5199 what actually happens is that the column itself is deleted but no items or subitems
5203 LISTVIEW_GetHeaderRect(infoPtr, nColumn, &rcCol);
5205 if (!SendMessageW(infoPtr->hwndHeader, HDM_DELETEITEM, nColumn, 0))
5208 Free(DPA_GetPtr(infoPtr->hdpaColumns, nColumn));
5209 DPA_DeletePtr(infoPtr->hdpaColumns, nColumn);
5211 if (!(infoPtr->dwStyle & LVS_OWNERDATA) && nColumn)
5213 SUBITEM_INFO *lpSubItem, *lpDelItem;
5215 INT nItem, nSubItem, i;
5217 for (nItem = 0; nItem < infoPtr->nItemCount; nItem++)
5219 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, nItem);
5222 for (i = 1; i < DPA_GetPtrCount(hdpaSubItems); i++)
5224 lpSubItem = DPA_GetPtr(hdpaSubItems, i);
5225 if (lpSubItem->iSubItem == nColumn)
5228 lpDelItem = lpSubItem;
5230 else if (lpSubItem->iSubItem > nColumn)
5232 lpSubItem->iSubItem--;
5236 /* if we found our subitem, zapp it */
5240 if (is_textW(lpDelItem->hdr.pszText))
5241 Free(lpDelItem->hdr.pszText);
5246 /* free dpa memory */
5247 DPA_DeletePtr(hdpaSubItems, nSubItem);
5252 /* update the other column info */
5253 LISTVIEW_UpdateItemSize(infoPtr);
5254 if(DPA_GetPtrCount(infoPtr->hdpaColumns) == 0)
5255 LISTVIEW_InvalidateList(infoPtr);
5257 LISTVIEW_ScrollColumns(infoPtr, nColumn, -(rcCol.right - rcCol.left));
5264 * Invalidates the listview after an item's insertion or deletion.
5267 * [I] infoPtr : valid pointer to the listview structure
5268 * [I] nItem : item index
5269 * [I] dir : -1 if deleting, 1 if inserting
5274 static void LISTVIEW_ScrollOnInsert(LISTVIEW_INFO *infoPtr, INT nItem, INT dir)
5276 INT nPerCol, nItemCol, nItemRow;
5280 /* if we don't refresh, what's the point of scrolling? */
5281 if (!is_redrawing(infoPtr)) return;
5283 assert (abs(dir) == 1);
5285 /* arrange icons if autoarrange is on */
5286 if (is_autoarrange(infoPtr))
5288 BOOL arrange = TRUE;
5289 if (dir < 0 && nItem >= infoPtr->nItemCount) arrange = FALSE;
5290 if (dir > 0 && nItem == infoPtr->nItemCount - 1) arrange = FALSE;
5291 if (arrange) LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
5294 /* scrollbars need updating */
5295 LISTVIEW_UpdateScroll(infoPtr);
5297 /* figure out the item's position */
5298 if (infoPtr->uView == LV_VIEW_DETAILS)
5299 nPerCol = infoPtr->nItemCount + 1;
5300 else if (infoPtr->uView == LV_VIEW_LIST)
5301 nPerCol = LISTVIEW_GetCountPerColumn(infoPtr);
5302 else /* LV_VIEW_ICON, or LV_VIEW_SMALLICON */
5305 nItemCol = nItem / nPerCol;
5306 nItemRow = nItem % nPerCol;
5307 LISTVIEW_GetOrigin(infoPtr, &Origin);
5309 /* move the items below up a slot */
5310 rcScroll.left = nItemCol * infoPtr->nItemWidth;
5311 rcScroll.top = nItemRow * infoPtr->nItemHeight;
5312 rcScroll.right = rcScroll.left + infoPtr->nItemWidth;
5313 rcScroll.bottom = nPerCol * infoPtr->nItemHeight;
5314 OffsetRect(&rcScroll, Origin.x, Origin.y);
5315 TRACE("rcScroll=%s, dx=%d\n", wine_dbgstr_rect(&rcScroll), dir * infoPtr->nItemHeight);
5316 if (IntersectRect(&rcScroll, &rcScroll, &infoPtr->rcList))
5318 TRACE("Scrolling rcScroll=%s, rcList=%s\n", wine_dbgstr_rect(&rcScroll), wine_dbgstr_rect(&infoPtr->rcList));
5319 ScrollWindowEx(infoPtr->hwndSelf, 0, dir * infoPtr->nItemHeight,
5320 &rcScroll, &rcScroll, 0, 0, SW_ERASE | SW_INVALIDATE);
5323 /* report has only that column, so we're done */
5324 if (infoPtr->uView == LV_VIEW_DETAILS) return;
5326 /* now for LISTs, we have to deal with the columns to the right */
5327 rcScroll.left = (nItemCol + 1) * infoPtr->nItemWidth;
5329 rcScroll.right = (infoPtr->nItemCount / nPerCol + 1) * infoPtr->nItemWidth;
5330 rcScroll.bottom = nPerCol * infoPtr->nItemHeight;
5331 OffsetRect(&rcScroll, Origin.x, Origin.y);
5332 if (IntersectRect(&rcScroll, &rcScroll, &infoPtr->rcList))
5333 ScrollWindowEx(infoPtr->hwndSelf, 0, dir * infoPtr->nItemHeight,
5334 &rcScroll, &rcScroll, 0, 0, SW_ERASE | SW_INVALIDATE);
5339 * Removes an item from the listview control.
5342 * [I] infoPtr : valid pointer to the listview structure
5343 * [I] nItem : item index
5349 static BOOL LISTVIEW_DeleteItem(LISTVIEW_INFO *infoPtr, INT nItem)
5352 const BOOL is_icon = (infoPtr->uView == LV_VIEW_SMALLICON || infoPtr->uView == LV_VIEW_ICON);
5354 TRACE("(nItem=%d)\n", nItem);
5356 if (nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
5358 /* remove selection, and focus */
5360 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
5361 LISTVIEW_SetItemState(infoPtr, nItem, &item);
5363 /* send LVN_DELETEITEM notification. */
5364 if (!notify_deleteitem(infoPtr, nItem)) return FALSE;
5366 /* we need to do this here, because we'll be deleting stuff */
5368 LISTVIEW_InvalidateItem(infoPtr, nItem);
5370 if (!(infoPtr->dwStyle & LVS_OWNERDATA))
5378 hdpaSubItems = DPA_DeletePtr(infoPtr->hdpaItems, nItem);
5379 lpItem = DPA_GetPtr(hdpaSubItems, 0);
5381 /* free id struct */
5382 i = DPA_GetPtrIndex(infoPtr->hdpaItemIds, lpItem->id);
5383 lpID = DPA_GetPtr(infoPtr->hdpaItemIds, i);
5384 DPA_DeletePtr(infoPtr->hdpaItemIds, i);
5386 for (i = 0; i < DPA_GetPtrCount(hdpaSubItems); i++)
5388 hdrItem = DPA_GetPtr(hdpaSubItems, i);
5389 if (is_textW(hdrItem->pszText)) Free(hdrItem->pszText);
5392 DPA_Destroy(hdpaSubItems);
5397 DPA_DeletePtr(infoPtr->hdpaPosX, nItem);
5398 DPA_DeletePtr(infoPtr->hdpaPosY, nItem);
5401 infoPtr->nItemCount--;
5402 LISTVIEW_ShiftIndices(infoPtr, nItem, -1);
5404 /* now is the invalidation fun */
5406 LISTVIEW_ScrollOnInsert(infoPtr, nItem, -1);
5413 * Callback implementation for editlabel control
5416 * [I] infoPtr : valid pointer to the listview structure
5417 * [I] storeText : store edit box text as item text
5418 * [I] isW : TRUE if psxText is Unicode, FALSE if it's ANSI
5424 static BOOL LISTVIEW_EndEditLabelT(LISTVIEW_INFO *infoPtr, BOOL storeText, BOOL isW)
5426 HWND hwndSelf = infoPtr->hwndSelf;
5427 NMLVDISPINFOW dispInfo;
5428 INT editedItem = infoPtr->nEditLabelItem;
5430 WCHAR *pszText = NULL;
5435 DWORD len = isW ? GetWindowTextLengthW(infoPtr->hwndEdit) : GetWindowTextLengthA(infoPtr->hwndEdit);
5439 if ((pszText = Alloc((len+1) * (isW ? sizeof(WCHAR) : sizeof(CHAR)))))
5441 if (isW) GetWindowTextW(infoPtr->hwndEdit, pszText, len+1);
5442 else GetWindowTextA(infoPtr->hwndEdit, (CHAR*)pszText, len+1);
5447 TRACE("(pszText=%s, isW=%d)\n", debugtext_t(pszText, isW), isW);
5449 infoPtr->nEditLabelItem = -1;
5450 infoPtr->hwndEdit = 0;
5452 ZeroMemory(&dispInfo, sizeof(dispInfo));
5453 dispInfo.item.mask = LVIF_PARAM | LVIF_STATE | LVIF_TEXT;
5454 dispInfo.item.iItem = editedItem;
5455 dispInfo.item.iSubItem = 0;
5456 dispInfo.item.stateMask = ~0;
5457 if (!LISTVIEW_GetItemW(infoPtr, &dispInfo.item))
5464 bSame = (lstrcmpW(dispInfo.item.pszText, pszText) == 0);
5467 LPWSTR tmp = textdupTtoW(pszText, FALSE);
5468 bSame = (lstrcmpW(dispInfo.item.pszText, tmp) == 0);
5469 textfreeT(tmp, FALSE);
5477 /* add the text from the edit in */
5478 dispInfo.item.mask |= LVIF_TEXT;
5479 dispInfo.item.pszText = pszText;
5480 dispInfo.item.cchTextMax = textlenT(pszText, isW);
5482 /* Do we need to update the Item Text */
5483 if (!notify_dispinfoT(infoPtr, LVN_ENDLABELEDITW, &dispInfo, isW))
5488 if (!IsWindow(hwndSelf))
5493 if (!pszText) return TRUE;
5495 if (!(infoPtr->dwStyle & LVS_OWNERDATA))
5497 HDPA hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, editedItem);
5498 ITEM_INFO* lpItem = DPA_GetPtr(hdpaSubItems, 0);
5499 if (lpItem && lpItem->hdr.pszText == LPSTR_TEXTCALLBACKW)
5501 LISTVIEW_InvalidateItem(infoPtr, editedItem);
5507 ZeroMemory(&dispInfo, sizeof(dispInfo));
5508 dispInfo.item.mask = LVIF_TEXT;
5509 dispInfo.item.iItem = editedItem;
5510 dispInfo.item.iSubItem = 0;
5511 dispInfo.item.pszText = pszText;
5512 dispInfo.item.cchTextMax = textlenT(pszText, isW);
5513 res = LISTVIEW_SetItemT(infoPtr, &dispInfo.item, isW);
5523 * Begin in place editing of specified list view item
5526 * [I] infoPtr : valid pointer to the listview structure
5527 * [I] nItem : item index
5528 * [I] isW : TRUE if it's a Unicode req, FALSE if ASCII
5534 static HWND LISTVIEW_EditLabelT(LISTVIEW_INFO *infoPtr, INT nItem, BOOL isW)
5536 WCHAR szDispText[DISP_TEXT_SIZE] = { 0 };
5537 NMLVDISPINFOW dispInfo;
5540 HWND hwndSelf = infoPtr->hwndSelf;
5542 HFONT hOldFont = NULL;
5543 TEXTMETRICW textMetric;
5545 TRACE("(nItem=%d, isW=%d)\n", nItem, isW);
5547 if (~infoPtr->dwStyle & LVS_EDITLABELS) return 0;
5549 /* Is the EditBox still there, if so remove it */
5550 if(infoPtr->hwndEdit != 0)
5552 SetFocus(infoPtr->hwndSelf);
5553 infoPtr->hwndEdit = 0;
5556 if (nItem < 0 || nItem >= infoPtr->nItemCount) return 0;
5558 infoPtr->nEditLabelItem = nItem;
5560 LISTVIEW_SetSelection(infoPtr, nItem);
5561 LISTVIEW_SetItemFocus(infoPtr, nItem);
5562 LISTVIEW_InvalidateItem(infoPtr, nItem);
5564 rect.left = LVIR_LABEL;
5565 if (!LISTVIEW_GetItemRect(infoPtr, nItem, &rect)) return 0;
5567 ZeroMemory(&dispInfo, sizeof(dispInfo));
5568 dispInfo.item.mask = LVIF_PARAM | LVIF_STATE | LVIF_TEXT;
5569 dispInfo.item.iItem = nItem;
5570 dispInfo.item.iSubItem = 0;
5571 dispInfo.item.stateMask = ~0;
5572 dispInfo.item.pszText = szDispText;
5573 dispInfo.item.cchTextMax = DISP_TEXT_SIZE;
5574 if (!LISTVIEW_GetItemT(infoPtr, &dispInfo.item, isW)) return 0;
5576 infoPtr->hwndEdit = CreateEditLabelT(infoPtr, dispInfo.item.pszText, WS_VISIBLE, isW);
5577 if (!infoPtr->hwndEdit) return 0;
5579 if (notify_dispinfoT(infoPtr, LVN_BEGINLABELEDITW, &dispInfo, isW))
5581 if (!IsWindow(hwndSelf))
5583 SendMessageW(infoPtr->hwndEdit, WM_CLOSE, 0, 0);
5584 infoPtr->hwndEdit = 0;
5588 /* Now position and display edit box */
5589 hdc = GetDC(infoPtr->hwndSelf);
5591 /* Select the font to get appropriate metric dimensions */
5592 if(infoPtr->hFont != 0)
5593 hOldFont = SelectObject(hdc, infoPtr->hFont);
5595 /* Get String Length in pixels */
5596 GetTextExtentPoint32W(hdc, dispInfo.item.pszText, lstrlenW(dispInfo.item.pszText), &sz);
5598 /* Add Extra spacing for the next character */
5599 GetTextMetricsW(hdc, &textMetric);
5600 sz.cx += (textMetric.tmMaxCharWidth * 2);
5602 if(infoPtr->hFont != 0)
5603 SelectObject(hdc, hOldFont);
5605 ReleaseDC(infoPtr->hwndSelf, hdc);
5607 MoveWindow(infoPtr->hwndEdit, rect.left - 2, rect.top - 1, sz.cx,
5608 rect.bottom - rect.top + 2, FALSE);
5609 ShowWindow(infoPtr->hwndEdit, SW_NORMAL);
5610 SetFocus(infoPtr->hwndEdit);
5611 SendMessageW(infoPtr->hwndEdit, EM_SETSEL, 0, -1);
5612 return infoPtr->hwndEdit;
5618 * Ensures the specified item is visible, scrolling into view if necessary.
5621 * [I] infoPtr : valid pointer to the listview structure
5622 * [I] nItem : item index
5623 * [I] bPartial : partially or entirely visible
5629 static BOOL LISTVIEW_EnsureVisible(LISTVIEW_INFO *infoPtr, INT nItem, BOOL bPartial)
5631 INT nScrollPosHeight = 0;
5632 INT nScrollPosWidth = 0;
5633 INT nHorzAdjust = 0;
5634 INT nVertAdjust = 0;
5637 RECT rcItem, rcTemp;
5639 rcItem.left = LVIR_BOUNDS;
5640 if (!LISTVIEW_GetItemRect(infoPtr, nItem, &rcItem)) return FALSE;
5642 if (bPartial && IntersectRect(&rcTemp, &infoPtr->rcList, &rcItem)) return TRUE;
5644 if (rcItem.left < infoPtr->rcList.left || rcItem.right > infoPtr->rcList.right)
5646 /* scroll left/right, but in LV_VIEW_DETAILS mode */
5647 if (infoPtr->uView == LV_VIEW_LIST)
5648 nScrollPosWidth = infoPtr->nItemWidth;
5649 else if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
5650 nScrollPosWidth = 1;
5652 if (rcItem.left < infoPtr->rcList.left)
5655 if (infoPtr->uView != LV_VIEW_DETAILS) nHorzDiff = rcItem.left - infoPtr->rcList.left;
5660 if (infoPtr->uView != LV_VIEW_DETAILS) nHorzDiff = rcItem.right - infoPtr->rcList.right;
5664 if (rcItem.top < infoPtr->rcList.top || rcItem.bottom > infoPtr->rcList.bottom)
5666 /* scroll up/down, but not in LVS_LIST mode */
5667 if (infoPtr->uView == LV_VIEW_DETAILS)
5668 nScrollPosHeight = infoPtr->nItemHeight;
5669 else if ((infoPtr->uView == LV_VIEW_ICON) || (infoPtr->uView == LV_VIEW_SMALLICON))
5670 nScrollPosHeight = 1;
5672 if (rcItem.top < infoPtr->rcList.top)
5675 if (infoPtr->uView != LV_VIEW_LIST) nVertDiff = rcItem.top - infoPtr->rcList.top;
5680 if (infoPtr->uView != LV_VIEW_LIST) nVertDiff = rcItem.bottom - infoPtr->rcList.bottom;
5684 if (!nScrollPosWidth && !nScrollPosHeight) return TRUE;
5686 if (nScrollPosWidth)
5688 INT diff = nHorzDiff / nScrollPosWidth;
5689 if (nHorzDiff % nScrollPosWidth) diff += nHorzAdjust;
5690 LISTVIEW_HScroll(infoPtr, SB_INTERNAL, diff, 0);
5693 if (nScrollPosHeight)
5695 INT diff = nVertDiff / nScrollPosHeight;
5696 if (nVertDiff % nScrollPosHeight) diff += nVertAdjust;
5697 LISTVIEW_VScroll(infoPtr, SB_INTERNAL, diff, 0);
5705 * Searches for an item with specific characteristics.
5708 * [I] hwnd : window handle
5709 * [I] nStart : base item index
5710 * [I] lpFindInfo : item information to look for
5713 * SUCCESS : index of item
5716 static INT LISTVIEW_FindItemW(const LISTVIEW_INFO *infoPtr, INT nStart,
5717 const LVFINDINFOW *lpFindInfo)
5719 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
5720 BOOL bWrap = FALSE, bNearest = FALSE;
5721 INT nItem = nStart + 1, nLast = infoPtr->nItemCount, nNearestItem = -1;
5722 ULONG xdist, ydist, dist, mindist = 0x7fffffff;
5723 POINT Position, Destination;
5726 /* Search in virtual listviews should be done by application, not by
5727 listview control, so we just send LVN_ODFINDITEMW and return the result */
5728 if (infoPtr->dwStyle & LVS_OWNERDATA)
5732 nmlv.iStart = nStart;
5733 nmlv.lvfi = *lpFindInfo;
5734 return notify_hdr(infoPtr, LVN_ODFINDITEMW, (LPNMHDR)&nmlv.hdr);
5737 if (!lpFindInfo || nItem < 0) return -1;
5740 if (lpFindInfo->flags & (LVFI_STRING | LVFI_PARTIAL))
5742 lvItem.mask |= LVIF_TEXT;
5743 lvItem.pszText = szDispText;
5744 lvItem.cchTextMax = DISP_TEXT_SIZE;
5747 if (lpFindInfo->flags & LVFI_WRAP)
5750 if ((lpFindInfo->flags & LVFI_NEARESTXY) &&
5751 (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON))
5756 LISTVIEW_GetOrigin(infoPtr, &Origin);
5757 Destination.x = lpFindInfo->pt.x - Origin.x;
5758 Destination.y = lpFindInfo->pt.y - Origin.y;
5759 switch(lpFindInfo->vkDirection)
5761 case VK_DOWN: Destination.y += infoPtr->nItemHeight; break;
5762 case VK_UP: Destination.y -= infoPtr->nItemHeight; break;
5763 case VK_RIGHT: Destination.x += infoPtr->nItemWidth; break;
5764 case VK_LEFT: Destination.x -= infoPtr->nItemWidth; break;
5765 case VK_HOME: Destination.x = Destination.y = 0; break;
5766 case VK_NEXT: Destination.y += infoPtr->rcList.bottom - infoPtr->rcList.top; break;
5767 case VK_PRIOR: Destination.y -= infoPtr->rcList.bottom - infoPtr->rcList.top; break;
5769 LISTVIEW_GetAreaRect(infoPtr, &rcArea);
5770 Destination.x = rcArea.right;
5771 Destination.y = rcArea.bottom;
5773 default: ERR("Unknown vkDirection=%d\n", lpFindInfo->vkDirection);
5777 else Destination.x = Destination.y = 0;
5779 /* if LVFI_PARAM is specified, all other flags are ignored */
5780 if (lpFindInfo->flags & LVFI_PARAM)
5782 lvItem.mask |= LVIF_PARAM;
5784 lvItem.mask &= ~LVIF_TEXT;
5788 for (; nItem < nLast; nItem++)
5790 lvItem.iItem = nItem;
5791 lvItem.iSubItem = 0;
5792 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) continue;
5794 if (lvItem.mask & LVIF_PARAM)
5796 if (lpFindInfo->lParam == lvItem.lParam)
5802 if (lvItem.mask & LVIF_TEXT)
5804 if (lpFindInfo->flags & LVFI_PARTIAL)
5806 if (strstrW(lvItem.pszText, lpFindInfo->psz) == NULL) continue;
5810 if (lstrcmpW(lvItem.pszText, lpFindInfo->psz) != 0) continue;
5814 if (!bNearest) return nItem;
5816 /* This is very inefficient. To do a good job here,
5817 * we need a sorted array of (x,y) item positions */
5818 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
5820 /* compute the distance^2 to the destination */
5821 xdist = Destination.x - Position.x;
5822 ydist = Destination.y - Position.y;
5823 dist = xdist * xdist + ydist * ydist;
5825 /* remember the distance, and item if it's closer */
5829 nNearestItem = nItem;
5836 nLast = min(nStart + 1, infoPtr->nItemCount);
5841 return nNearestItem;
5846 * Searches for an item with specific characteristics.
5849 * [I] hwnd : window handle
5850 * [I] nStart : base item index
5851 * [I] lpFindInfo : item information to look for
5854 * SUCCESS : index of item
5857 static INT LISTVIEW_FindItemA(const LISTVIEW_INFO *infoPtr, INT nStart,
5858 const LVFINDINFOA *lpFindInfo)
5860 BOOL hasText = lpFindInfo->flags & (LVFI_STRING | LVFI_PARTIAL);
5865 memcpy(&fiw, lpFindInfo, sizeof(fiw));
5866 if (hasText) fiw.psz = strW = textdupTtoW((LPCWSTR)lpFindInfo->psz, FALSE);
5867 res = LISTVIEW_FindItemW(infoPtr, nStart, &fiw);
5868 textfreeT(strW, FALSE);
5874 * Retrieves the background image of the listview control.
5877 * [I] infoPtr : valid pointer to the listview structure
5878 * [O] lpBkImage : background image attributes
5884 /* static BOOL LISTVIEW_GetBkImage(const LISTVIEW_INFO *infoPtr, LPLVBKIMAGE lpBkImage) */
5886 /* FIXME (listview, "empty stub!\n"); */
5892 * Retrieves column attributes.
5895 * [I] infoPtr : valid pointer to the listview structure
5896 * [I] nColumn : column index
5897 * [IO] lpColumn : column information
5898 * [I] isW : if TRUE, then lpColumn is a LPLVCOLUMNW
5899 * otherwise it is in fact a LPLVCOLUMNA
5905 static BOOL LISTVIEW_GetColumnT(const LISTVIEW_INFO *infoPtr, INT nColumn, LPLVCOLUMNW lpColumn, BOOL isW)
5907 COLUMN_INFO *lpColumnInfo;
5910 if (!lpColumn || nColumn < 0 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
5911 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, nColumn);
5913 /* initialize memory */
5914 ZeroMemory(&hdi, sizeof(hdi));
5916 if (lpColumn->mask & LVCF_TEXT)
5918 hdi.mask |= HDI_TEXT;
5919 hdi.pszText = lpColumn->pszText;
5920 hdi.cchTextMax = lpColumn->cchTextMax;
5923 if (lpColumn->mask & LVCF_IMAGE)
5924 hdi.mask |= HDI_IMAGE;
5926 if (lpColumn->mask & LVCF_ORDER)
5927 hdi.mask |= HDI_ORDER;
5929 if (lpColumn->mask & LVCF_SUBITEM)
5930 hdi.mask |= HDI_LPARAM;
5932 if (!SendMessageW(infoPtr->hwndHeader, isW ? HDM_GETITEMW : HDM_GETITEMA, nColumn, (LPARAM)&hdi)) return FALSE;
5934 if (lpColumn->mask & LVCF_FMT)
5935 lpColumn->fmt = lpColumnInfo->fmt;
5937 if (lpColumn->mask & LVCF_WIDTH)
5938 lpColumn->cx = lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left;
5940 if (lpColumn->mask & LVCF_IMAGE)
5941 lpColumn->iImage = hdi.iImage;
5943 if (lpColumn->mask & LVCF_ORDER)
5944 lpColumn->iOrder = hdi.iOrder;
5946 if (lpColumn->mask & LVCF_SUBITEM)
5947 lpColumn->iSubItem = hdi.lParam;
5949 if (lpColumn->mask & LVCF_MINWIDTH)
5950 lpColumn->cxMin = lpColumnInfo->cxMin;
5956 static BOOL LISTVIEW_GetColumnOrderArray(const LISTVIEW_INFO *infoPtr, INT iCount, LPINT lpiArray)
5958 TRACE("iCount=%d, lpiArray=%p\n", iCount, lpiArray);
5963 return SendMessageW(infoPtr->hwndHeader, HDM_GETORDERARRAY, iCount, (LPARAM)lpiArray);
5968 * Retrieves the column width.
5971 * [I] infoPtr : valid pointer to the listview structure
5972 * [I] int : column index
5975 * SUCCESS : column width
5978 static INT LISTVIEW_GetColumnWidth(const LISTVIEW_INFO *infoPtr, INT nColumn)
5980 INT nColumnWidth = 0;
5983 TRACE("nColumn=%d\n", nColumn);
5985 /* we have a 'column' in LIST and REPORT mode only */
5986 switch(infoPtr->uView)
5989 nColumnWidth = infoPtr->nItemWidth;
5991 case LV_VIEW_DETAILS:
5992 /* We are not using LISTVIEW_GetHeaderRect as this data is updated only after a HDN_ITEMCHANGED.
5993 * There is an application that subclasses the listview, calls LVM_GETCOLUMNWIDTH in the
5994 * HDN_ITEMCHANGED handler and goes into infinite recursion if it receives old data.
5996 * TODO: should we do the same in LVM_GETCOLUMN?
5998 hdItem.mask = HDI_WIDTH;
5999 if (!SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nColumn, (LPARAM)&hdItem))
6001 WARN("(%p): HDM_GETITEMW failed for item %d\n", infoPtr->hwndSelf, nColumn);
6004 nColumnWidth = hdItem.cxy;
6008 TRACE("nColumnWidth=%d\n", nColumnWidth);
6009 return nColumnWidth;
6014 * In list or report display mode, retrieves the number of items that can fit
6015 * vertically in the visible area. In icon or small icon display mode,
6016 * retrieves the total number of visible items.
6019 * [I] infoPtr : valid pointer to the listview structure
6022 * Number of fully visible items.
6024 static INT LISTVIEW_GetCountPerPage(const LISTVIEW_INFO *infoPtr)
6026 switch (infoPtr->uView)
6029 case LV_VIEW_SMALLICON:
6030 return infoPtr->nItemCount;
6031 case LV_VIEW_DETAILS:
6032 return LISTVIEW_GetCountPerColumn(infoPtr);
6034 return LISTVIEW_GetCountPerRow(infoPtr) * LISTVIEW_GetCountPerColumn(infoPtr);
6042 * Retrieves an image list handle.
6045 * [I] infoPtr : valid pointer to the listview structure
6046 * [I] nImageList : image list identifier
6049 * SUCCESS : image list handle
6052 static HIMAGELIST LISTVIEW_GetImageList(const LISTVIEW_INFO *infoPtr, INT nImageList)
6056 case LVSIL_NORMAL: return infoPtr->himlNormal;
6057 case LVSIL_SMALL: return infoPtr->himlSmall;
6058 case LVSIL_STATE: return infoPtr->himlState;
6063 /* LISTVIEW_GetISearchString */
6067 * Retrieves item attributes.
6070 * [I] hwnd : window handle
6071 * [IO] lpLVItem : item info
6072 * [I] isW : if TRUE, then lpLVItem is a LPLVITEMW,
6073 * if FALSE, then lpLVItem is a LPLVITEMA.
6076 * This is the internal 'GetItem' interface -- it tries to
6077 * be smart and avoid text copies, if possible, by modifying
6078 * lpLVItem->pszText to point to the text string. Please note
6079 * that this is not always possible (e.g. OWNERDATA), so on
6080 * entry you *must* supply valid values for pszText, and cchTextMax.
6081 * The only difference to the documented interface is that upon
6082 * return, you should use *only* the lpLVItem->pszText, rather than
6083 * the buffer pointer you provided on input. Most code already does
6084 * that, so it's not a problem.
6085 * For the two cases when the text must be copied (that is,
6086 * for LVM_GETITEM, and LVM_GETITEMTEXT), use LISTVIEW_GetItemExtT.
6092 static BOOL LISTVIEW_GetItemT(const LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem, BOOL isW)
6094 ITEMHDR callbackHdr = { LPSTR_TEXTCALLBACKW, I_IMAGECALLBACK };
6095 NMLVDISPINFOW dispInfo;
6101 TRACE("(lpLVItem=%s, isW=%d)\n", debuglvitem_t(lpLVItem, isW), isW);
6103 if (!lpLVItem || lpLVItem->iItem < 0 || lpLVItem->iItem >= infoPtr->nItemCount)
6106 if (lpLVItem->mask == 0) return TRUE;
6108 /* make a local copy */
6109 isubitem = lpLVItem->iSubItem;
6111 /* a quick optimization if all we're asked is the focus state
6112 * these queries are worth optimising since they are common,
6113 * and can be answered in constant time, without the heavy accesses */
6114 if ( (lpLVItem->mask == LVIF_STATE) && (lpLVItem->stateMask == LVIS_FOCUSED) &&
6115 !(infoPtr->uCallbackMask & LVIS_FOCUSED) )
6117 lpLVItem->state = 0;
6118 if (infoPtr->nFocusedItem == lpLVItem->iItem)
6119 lpLVItem->state |= LVIS_FOCUSED;
6123 ZeroMemory(&dispInfo, sizeof(dispInfo));
6125 /* if the app stores all the data, handle it separately */
6126 if (infoPtr->dwStyle & LVS_OWNERDATA)
6128 dispInfo.item.state = 0;
6130 /* apparently, we should not callback for lParam in LVS_OWNERDATA */
6131 if ((lpLVItem->mask & ~(LVIF_STATE | LVIF_PARAM)) ||
6132 ((lpLVItem->mask & LVIF_STATE) && (infoPtr->uCallbackMask & lpLVItem->stateMask)))
6134 UINT mask = lpLVItem->mask;
6136 /* NOTE: copy only fields which we _know_ are initialized, some apps
6137 * depend on the uninitialized fields being 0 */
6138 dispInfo.item.mask = lpLVItem->mask & ~LVIF_PARAM;
6139 dispInfo.item.iItem = lpLVItem->iItem;
6140 dispInfo.item.iSubItem = isubitem;
6141 if (lpLVItem->mask & LVIF_TEXT)
6143 if (lpLVItem->mask & LVIF_NORECOMPUTE)
6145 dispInfo.item.mask &= ~(LVIF_TEXT | LVIF_NORECOMPUTE);
6148 dispInfo.item.pszText = lpLVItem->pszText;
6149 dispInfo.item.cchTextMax = lpLVItem->cchTextMax;
6152 if (lpLVItem->mask & LVIF_STATE)
6153 dispInfo.item.stateMask = lpLVItem->stateMask & infoPtr->uCallbackMask;
6154 /* could be zeroed on LVIF_NORECOMPUTE case */
6155 if (dispInfo.item.mask != 0)
6157 notify_dispinfoT(infoPtr, LVN_GETDISPINFOW, &dispInfo, isW);
6158 dispInfo.item.stateMask = lpLVItem->stateMask;
6159 if (lpLVItem->mask & (LVIF_GROUPID|LVIF_COLUMNS))
6161 /* full size structure expected - _WIN32IE >= 0x560 */
6162 *lpLVItem = dispInfo.item;
6164 else if (lpLVItem->mask & LVIF_INDENT)
6166 /* indent member expected - _WIN32IE >= 0x300 */
6167 memcpy(lpLVItem, &dispInfo.item, offsetof( LVITEMW, iGroupId ));
6171 /* minimal structure expected */
6172 memcpy(lpLVItem, &dispInfo.item, offsetof( LVITEMW, iIndent ));
6174 lpLVItem->mask = mask;
6175 TRACE(" getdispinfo(1):lpLVItem=%s\n", debuglvitem_t(lpLVItem, isW));
6179 /* make sure lParam is zeroed out */
6180 if (lpLVItem->mask & LVIF_PARAM) lpLVItem->lParam = 0;
6182 /* callback marked pointer required here */
6183 if ((lpLVItem->mask & LVIF_TEXT) && (lpLVItem->mask & LVIF_NORECOMPUTE))
6184 lpLVItem->pszText = LPSTR_TEXTCALLBACKW;
6186 /* we store only a little state, so if we're not asked, we're done */
6187 if (!(lpLVItem->mask & LVIF_STATE) || isubitem) return TRUE;
6189 /* if focus is handled by us, report it */
6190 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_FOCUSED )
6192 lpLVItem->state &= ~LVIS_FOCUSED;
6193 if (infoPtr->nFocusedItem == lpLVItem->iItem)
6194 lpLVItem->state |= LVIS_FOCUSED;
6197 /* and do the same for selection, if we handle it */
6198 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_SELECTED )
6200 lpLVItem->state &= ~LVIS_SELECTED;
6201 if (ranges_contain(infoPtr->selectionRanges, lpLVItem->iItem))
6202 lpLVItem->state |= LVIS_SELECTED;
6208 /* find the item and subitem structures before we proceed */
6209 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, lpLVItem->iItem);
6210 lpItem = DPA_GetPtr(hdpaSubItems, 0);
6215 SUBITEM_INFO *lpSubItem = LISTVIEW_GetSubItemPtr(hdpaSubItems, isubitem);
6216 pItemHdr = lpSubItem ? &lpSubItem->hdr : &callbackHdr;
6219 WARN(" iSubItem invalid (%08x), ignored.\n", isubitem);
6224 pItemHdr = &lpItem->hdr;
6226 /* Do we need to query the state from the app? */
6227 if ((lpLVItem->mask & LVIF_STATE) && infoPtr->uCallbackMask && isubitem == 0)
6229 dispInfo.item.mask |= LVIF_STATE;
6230 dispInfo.item.stateMask = infoPtr->uCallbackMask;
6233 /* Do we need to enquire about the image? */
6234 if ((lpLVItem->mask & LVIF_IMAGE) && pItemHdr->iImage == I_IMAGECALLBACK &&
6235 (isubitem == 0 || (infoPtr->dwLvExStyle & LVS_EX_SUBITEMIMAGES)))
6237 dispInfo.item.mask |= LVIF_IMAGE;
6238 dispInfo.item.iImage = I_IMAGECALLBACK;
6241 /* Only items support indentation */
6242 if ((lpLVItem->mask & LVIF_INDENT) && lpItem->iIndent == I_INDENTCALLBACK &&
6245 dispInfo.item.mask |= LVIF_INDENT;
6246 dispInfo.item.iIndent = I_INDENTCALLBACK;
6249 /* Apps depend on calling back for text if it is NULL or LPSTR_TEXTCALLBACKW */
6250 if ((lpLVItem->mask & LVIF_TEXT) && !(lpLVItem->mask & LVIF_NORECOMPUTE) &&
6251 !is_textW(pItemHdr->pszText))
6253 dispInfo.item.mask |= LVIF_TEXT;
6254 dispInfo.item.pszText = lpLVItem->pszText;
6255 dispInfo.item.cchTextMax = lpLVItem->cchTextMax;
6256 if (dispInfo.item.pszText && dispInfo.item.cchTextMax > 0)
6257 *dispInfo.item.pszText = '\0';
6260 /* If we don't have all the requested info, query the application */
6261 if (dispInfo.item.mask != 0)
6263 dispInfo.item.iItem = lpLVItem->iItem;
6264 dispInfo.item.iSubItem = lpLVItem->iSubItem; /* yes: the original subitem */
6265 dispInfo.item.lParam = lpItem->lParam;
6266 notify_dispinfoT(infoPtr, LVN_GETDISPINFOW, &dispInfo, isW);
6267 TRACE(" getdispinfo(2):item=%s\n", debuglvitem_t(&dispInfo.item, isW));
6270 /* we should not store values for subitems */
6271 if (isubitem) dispInfo.item.mask &= ~LVIF_DI_SETITEM;
6273 /* Now, handle the iImage field */
6274 if (dispInfo.item.mask & LVIF_IMAGE)
6276 lpLVItem->iImage = dispInfo.item.iImage;
6277 if ((dispInfo.item.mask & LVIF_DI_SETITEM) && pItemHdr->iImage == I_IMAGECALLBACK)
6278 pItemHdr->iImage = dispInfo.item.iImage;
6280 else if (lpLVItem->mask & LVIF_IMAGE)
6282 if(isubitem == 0 || (infoPtr->dwLvExStyle & LVS_EX_SUBITEMIMAGES))
6283 lpLVItem->iImage = pItemHdr->iImage;
6285 lpLVItem->iImage = 0;
6288 /* The pszText field */
6289 if (dispInfo.item.mask & LVIF_TEXT)
6291 if ((dispInfo.item.mask & LVIF_DI_SETITEM) && pItemHdr->pszText)
6292 textsetptrT(&pItemHdr->pszText, dispInfo.item.pszText, isW);
6294 lpLVItem->pszText = dispInfo.item.pszText;
6296 else if (lpLVItem->mask & LVIF_TEXT)
6298 /* if LVN_GETDISPINFO's disabled with LVIF_NORECOMPUTE return callback placeholder */
6299 if (isW || !is_textW(pItemHdr->pszText)) lpLVItem->pszText = pItemHdr->pszText;
6300 else textcpynT(lpLVItem->pszText, isW, pItemHdr->pszText, TRUE, lpLVItem->cchTextMax);
6303 /* Next is the lParam field */
6304 if (dispInfo.item.mask & LVIF_PARAM)
6306 lpLVItem->lParam = dispInfo.item.lParam;
6307 if ((dispInfo.item.mask & LVIF_DI_SETITEM))
6308 lpItem->lParam = dispInfo.item.lParam;
6310 else if (lpLVItem->mask & LVIF_PARAM)
6311 lpLVItem->lParam = lpItem->lParam;
6313 /* if this is a subitem, we're done */
6314 if (isubitem) return TRUE;
6316 /* ... the state field (this one is different due to uCallbackmask) */
6317 if (lpLVItem->mask & LVIF_STATE)
6319 lpLVItem->state = lpItem->state & lpLVItem->stateMask;
6320 if (dispInfo.item.mask & LVIF_STATE)
6322 lpLVItem->state &= ~dispInfo.item.stateMask;
6323 lpLVItem->state |= (dispInfo.item.state & dispInfo.item.stateMask);
6325 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_FOCUSED )
6327 lpLVItem->state &= ~LVIS_FOCUSED;
6328 if (infoPtr->nFocusedItem == lpLVItem->iItem)
6329 lpLVItem->state |= LVIS_FOCUSED;
6331 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_SELECTED )
6333 lpLVItem->state &= ~LVIS_SELECTED;
6334 if (ranges_contain(infoPtr->selectionRanges, lpLVItem->iItem))
6335 lpLVItem->state |= LVIS_SELECTED;
6339 /* and last, but not least, the indent field */
6340 if (dispInfo.item.mask & LVIF_INDENT)
6342 lpLVItem->iIndent = dispInfo.item.iIndent;
6343 if ((dispInfo.item.mask & LVIF_DI_SETITEM) && lpItem->iIndent == I_INDENTCALLBACK)
6344 lpItem->iIndent = dispInfo.item.iIndent;
6346 else if (lpLVItem->mask & LVIF_INDENT)
6348 lpLVItem->iIndent = lpItem->iIndent;
6356 * Retrieves item attributes.
6359 * [I] hwnd : window handle
6360 * [IO] lpLVItem : item info
6361 * [I] isW : if TRUE, then lpLVItem is a LPLVITEMW,
6362 * if FALSE, then lpLVItem is a LPLVITEMA.
6365 * This is the external 'GetItem' interface -- it properly copies
6366 * the text in the provided buffer.
6372 static BOOL LISTVIEW_GetItemExtT(const LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem, BOOL isW)
6377 if (!lpLVItem || lpLVItem->iItem < 0 || lpLVItem->iItem >= infoPtr->nItemCount)
6380 pszText = lpLVItem->pszText;
6381 bResult = LISTVIEW_GetItemT(infoPtr, lpLVItem, isW);
6382 if (bResult && lpLVItem->pszText != pszText)
6384 if (lpLVItem->pszText != LPSTR_TEXTCALLBACKW)
6385 textcpynT(pszText, isW, lpLVItem->pszText, isW, lpLVItem->cchTextMax);
6387 pszText = LPSTR_TEXTCALLBACKW;
6389 lpLVItem->pszText = pszText;
6397 * Retrieves the position (upper-left) of the listview control item.
6398 * Note that for LVS_ICON style, the upper-left is that of the icon
6399 * and not the bounding box.
6402 * [I] infoPtr : valid pointer to the listview structure
6403 * [I] nItem : item index
6404 * [O] lpptPosition : coordinate information
6410 static BOOL LISTVIEW_GetItemPosition(const LISTVIEW_INFO *infoPtr, INT nItem, LPPOINT lpptPosition)
6414 TRACE("(nItem=%d, lpptPosition=%p)\n", nItem, lpptPosition);
6416 if (!lpptPosition || nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
6418 LISTVIEW_GetOrigin(infoPtr, &Origin);
6419 LISTVIEW_GetItemOrigin(infoPtr, nItem, lpptPosition);
6421 if (infoPtr->uView == LV_VIEW_ICON)
6423 lpptPosition->x += (infoPtr->nItemWidth - infoPtr->iconSize.cx) / 2;
6424 lpptPosition->y += ICON_TOP_PADDING;
6426 lpptPosition->x += Origin.x;
6427 lpptPosition->y += Origin.y;
6429 TRACE (" lpptPosition=%s\n", wine_dbgstr_point(lpptPosition));
6436 * Retrieves the bounding rectangle for a listview control item.
6439 * [I] infoPtr : valid pointer to the listview structure
6440 * [I] nItem : item index
6441 * [IO] lprc : bounding rectangle coordinates
6442 * lprc->left specifies the portion of the item for which the bounding
6443 * rectangle will be retrieved.
6445 * LVIR_BOUNDS Returns the bounding rectangle of the entire item,
6446 * including the icon and label.
6449 * * Experiment shows that native control returns:
6450 * * width = min (48, length of text line)
6451 * * .left = position.x - (width - iconsize.cx)/2
6452 * * .right = .left + width
6453 * * height = #lines of text * ntmHeight + icon height + 8
6454 * * .top = position.y - 2
6455 * * .bottom = .top + height
6456 * * separation between items .y = itemSpacing.cy - height
6457 * * .x = itemSpacing.cx - width
6458 * LVIR_ICON Returns the bounding rectangle of the icon or small icon.
6461 * * Experiment shows that native control returns:
6462 * * width = iconSize.cx + 16
6463 * * .left = position.x - (width - iconsize.cx)/2
6464 * * .right = .left + width
6465 * * height = iconSize.cy + 4
6466 * * .top = position.y - 2
6467 * * .bottom = .top + height
6468 * * separation between items .y = itemSpacing.cy - height
6469 * * .x = itemSpacing.cx - width
6470 * LVIR_LABEL Returns the bounding rectangle of the item text.
6473 * * Experiment shows that native control returns:
6474 * * width = text length
6475 * * .left = position.x - width/2
6476 * * .right = .left + width
6477 * * height = ntmH * linecount + 2
6478 * * .top = position.y + iconSize.cy + 6
6479 * * .bottom = .top + height
6480 * * separation between items .y = itemSpacing.cy - height
6481 * * .x = itemSpacing.cx - width
6482 * LVIR_SELECTBOUNDS Returns the union of the LVIR_ICON and LVIR_LABEL
6483 * rectangles, but excludes columns in report view.
6490 * Note that the bounding rectangle of the label in the LVS_ICON view depends
6491 * upon whether the window has the focus currently and on whether the item
6492 * is the one with the focus. Ensure that the control's record of which
6493 * item has the focus agrees with the items' records.
6495 static BOOL LISTVIEW_GetItemRect(const LISTVIEW_INFO *infoPtr, INT nItem, LPRECT lprc)
6497 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
6498 BOOL doLabel = TRUE, oversizedBox = FALSE;
6499 POINT Position, Origin;
6503 TRACE("(hwnd=%p, nItem=%d, lprc=%p)\n", infoPtr->hwndSelf, nItem, lprc);
6505 if (!lprc || nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
6507 LISTVIEW_GetOrigin(infoPtr, &Origin);
6508 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
6510 /* Be smart and try to figure out the minimum we have to do */
6511 if (lprc->left == LVIR_ICON) doLabel = FALSE;
6512 if (infoPtr->uView == LV_VIEW_DETAILS && lprc->left == LVIR_BOUNDS) doLabel = FALSE;
6513 if (infoPtr->uView == LV_VIEW_ICON && lprc->left != LVIR_ICON &&
6514 infoPtr->bFocus && LISTVIEW_GetItemState(infoPtr, nItem, LVIS_FOCUSED))
6515 oversizedBox = TRUE;
6517 /* get what we need from the item before hand, so we make
6518 * only one request. This can speed up things, if data
6519 * is stored on the app side */
6521 if (infoPtr->uView == LV_VIEW_DETAILS) lvItem.mask |= LVIF_INDENT;
6522 if (doLabel) lvItem.mask |= LVIF_TEXT;
6523 lvItem.iItem = nItem;
6524 lvItem.iSubItem = 0;
6525 lvItem.pszText = szDispText;
6526 lvItem.cchTextMax = DISP_TEXT_SIZE;
6527 if (lvItem.mask && !LISTVIEW_GetItemW(infoPtr, &lvItem)) return FALSE;
6528 /* we got the state already up, simulate it here, to avoid a reget */
6529 if (infoPtr->uView == LV_VIEW_ICON && (lprc->left != LVIR_ICON))
6531 lvItem.mask |= LVIF_STATE;
6532 lvItem.stateMask = LVIS_FOCUSED;
6533 lvItem.state = (oversizedBox ? LVIS_FOCUSED : 0);
6536 if (infoPtr->uView == LV_VIEW_DETAILS && (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) && lprc->left == LVIR_SELECTBOUNDS)
6537 lprc->left = LVIR_BOUNDS;
6543 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, NULL, lprc, NULL, NULL);
6547 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, NULL, NULL, NULL, lprc);
6551 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, lprc, NULL, NULL, NULL, NULL);
6554 case LVIR_SELECTBOUNDS:
6555 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, lprc, NULL, NULL, NULL);
6559 WARN("Unknown value: %d\n", lprc->left);
6563 if (infoPtr->uView == LV_VIEW_DETAILS)
6565 if (mode != LVIR_BOUNDS)
6566 OffsetRect(lprc, Origin.x + LISTVIEW_GetColumnInfo(infoPtr, 0)->rcHeader.left,
6567 Position.y + Origin.y);
6569 OffsetRect(lprc, Origin.x, Position.y + Origin.y);
6572 OffsetRect(lprc, Position.x + Origin.x, Position.y + Origin.y);
6574 TRACE(" rect=%s\n", wine_dbgstr_rect(lprc));
6581 * Retrieves the spacing between listview control items.
6584 * [I] infoPtr : valid pointer to the listview structure
6585 * [IO] lprc : rectangle to receive the output
6586 * on input, lprc->top = nSubItem
6587 * lprc->left = LVIR_ICON | LVIR_BOUNDS | LVIR_LABEL
6589 * NOTE: for subItem = 0, we should return the bounds of the _entire_ item,
6590 * not only those of the first column.
6591 * Fortunately, LISTVIEW_GetItemMetrics does the right thing.
6597 static BOOL LISTVIEW_GetSubItemRect(const LISTVIEW_INFO *infoPtr, INT nItem, LPRECT lprc)
6603 if (!lprc) return FALSE;
6605 nColumn = lprc->top;
6607 TRACE("(nItem=%d, nSubItem=%d)\n", nItem, lprc->top);
6608 /* On WinNT, a subitem of '0' calls LISTVIEW_GetItemRect */
6610 return LISTVIEW_GetItemRect(infoPtr, nItem, lprc);
6612 if (infoPtr->uView != LV_VIEW_DETAILS) return FALSE;
6614 /* special case for header items */
6617 if (lprc->left != LVIR_BOUNDS)
6619 FIXME("Only LVIR_BOUNDS is implemented for header, got %d\n", lprc->left);
6623 if (infoPtr->hwndHeader)
6624 return SendMessageW(infoPtr->hwndHeader, HDM_GETITEMRECT, lprc->top, (LPARAM)lprc);
6627 memset(lprc, 0, sizeof(RECT));
6632 if (!LISTVIEW_GetItemPosition(infoPtr, nItem, &Position)) return FALSE;
6634 if (nColumn < 0 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
6637 lvItem.iItem = nItem;
6638 lvItem.iSubItem = nColumn;
6640 if (lvItem.mask && !LISTVIEW_GetItemW(infoPtr, &lvItem)) return FALSE;
6644 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, NULL, lprc, NULL, NULL);
6649 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, lprc, NULL, NULL, NULL, NULL);
6653 ERR("Unknown bounds=%d\n", lprc->left);
6657 OffsetRect(lprc, 0, Position.y);
6664 * Retrieves the width of a label.
6667 * [I] infoPtr : valid pointer to the listview structure
6670 * SUCCESS : string width (in pixels)
6673 static INT LISTVIEW_GetLabelWidth(const LISTVIEW_INFO *infoPtr, INT nItem)
6675 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
6678 TRACE("(nItem=%d)\n", nItem);
6680 lvItem.mask = LVIF_TEXT;
6681 lvItem.iItem = nItem;
6682 lvItem.iSubItem = 0;
6683 lvItem.pszText = szDispText;
6684 lvItem.cchTextMax = DISP_TEXT_SIZE;
6685 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return 0;
6687 return LISTVIEW_GetStringWidthT(infoPtr, lvItem.pszText, TRUE);
6692 * Retrieves the spacing between listview control items.
6695 * [I] infoPtr : valid pointer to the listview structure
6696 * [I] bSmall : flag for small or large icon
6699 * Horizontal + vertical spacing
6701 static LONG LISTVIEW_GetItemSpacing(const LISTVIEW_INFO *infoPtr, BOOL bSmall)
6707 lResult = MAKELONG(infoPtr->iconSpacing.cx, infoPtr->iconSpacing.cy);
6711 if (infoPtr->uView == LV_VIEW_ICON)
6712 lResult = MAKELONG(DEFAULT_COLUMN_WIDTH, GetSystemMetrics(SM_CXSMICON)+HEIGHT_PADDING);
6714 lResult = MAKELONG(infoPtr->nItemWidth, infoPtr->nItemHeight);
6721 * Retrieves the state of a listview control item.
6724 * [I] infoPtr : valid pointer to the listview structure
6725 * [I] nItem : item index
6726 * [I] uMask : state mask
6729 * State specified by the mask.
6731 static UINT LISTVIEW_GetItemState(const LISTVIEW_INFO *infoPtr, INT nItem, UINT uMask)
6735 if (nItem < 0 || nItem >= infoPtr->nItemCount) return 0;
6737 lvItem.iItem = nItem;
6738 lvItem.iSubItem = 0;
6739 lvItem.mask = LVIF_STATE;
6740 lvItem.stateMask = uMask;
6741 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return 0;
6743 return lvItem.state & uMask;
6748 * Retrieves the text of a listview control item or subitem.
6751 * [I] hwnd : window handle
6752 * [I] nItem : item index
6753 * [IO] lpLVItem : item information
6754 * [I] isW : TRUE if lpLVItem is Unicode
6757 * SUCCESS : string length
6760 static INT LISTVIEW_GetItemTextT(const LISTVIEW_INFO *infoPtr, INT nItem, LPLVITEMW lpLVItem, BOOL isW)
6762 if (!lpLVItem || nItem < 0 || nItem >= infoPtr->nItemCount) return 0;
6764 lpLVItem->mask = LVIF_TEXT;
6765 lpLVItem->iItem = nItem;
6766 if (!LISTVIEW_GetItemExtT(infoPtr, lpLVItem, isW)) return 0;
6768 return textlenT(lpLVItem->pszText, isW);
6773 * Searches for an item based on properties + relationships.
6776 * [I] infoPtr : valid pointer to the listview structure
6777 * [I] nItem : item index
6778 * [I] uFlags : relationship flag
6781 * SUCCESS : item index
6784 static INT LISTVIEW_GetNextItem(const LISTVIEW_INFO *infoPtr, INT nItem, UINT uFlags)
6787 LVFINDINFOW lvFindInfo;
6788 INT nCountPerColumn;
6792 TRACE("nItem=%d, uFlags=%x, nItemCount=%d\n", nItem, uFlags, infoPtr->nItemCount);
6793 if (nItem < -1 || nItem >= infoPtr->nItemCount) return -1;
6795 ZeroMemory(&lvFindInfo, sizeof(lvFindInfo));
6797 if (uFlags & LVNI_CUT)
6800 if (uFlags & LVNI_DROPHILITED)
6801 uMask |= LVIS_DROPHILITED;
6803 if (uFlags & LVNI_FOCUSED)
6804 uMask |= LVIS_FOCUSED;
6806 if (uFlags & LVNI_SELECTED)
6807 uMask |= LVIS_SELECTED;
6809 /* if we're asked for the focused item, that's only one,
6810 * so it's worth optimizing */
6811 if (uFlags & LVNI_FOCUSED)
6813 if ((LISTVIEW_GetItemState(infoPtr, infoPtr->nFocusedItem, uMask) & uMask) != uMask) return -1;
6814 return (infoPtr->nFocusedItem == nItem) ? -1 : infoPtr->nFocusedItem;
6817 if (uFlags & LVNI_ABOVE)
6819 if ((infoPtr->uView == LV_VIEW_LIST) || (infoPtr->uView == LV_VIEW_DETAILS))
6824 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
6830 /* Special case for autoarrange - move 'til the top of a list */
6831 if (is_autoarrange(infoPtr))
6833 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
6834 while (nItem - nCountPerRow >= 0)
6836 nItem -= nCountPerRow;
6837 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
6842 lvFindInfo.flags = LVFI_NEARESTXY;
6843 lvFindInfo.vkDirection = VK_UP;
6844 LISTVIEW_GetItemPosition(infoPtr, nItem, &lvFindInfo.pt);
6845 while ((nItem = LISTVIEW_FindItemW(infoPtr, nItem, &lvFindInfo)) != -1)
6847 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
6852 else if (uFlags & LVNI_BELOW)
6854 if ((infoPtr->uView == LV_VIEW_LIST) || (infoPtr->uView == LV_VIEW_DETAILS))
6856 while (nItem < infoPtr->nItemCount)
6859 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
6865 /* Special case for autoarrange - move 'til the bottom of a list */
6866 if (is_autoarrange(infoPtr))
6868 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
6869 while (nItem + nCountPerRow < infoPtr->nItemCount )
6871 nItem += nCountPerRow;
6872 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
6877 lvFindInfo.flags = LVFI_NEARESTXY;
6878 lvFindInfo.vkDirection = VK_DOWN;
6879 LISTVIEW_GetItemPosition(infoPtr, nItem, &lvFindInfo.pt);
6880 while ((nItem = LISTVIEW_FindItemW(infoPtr, nItem, &lvFindInfo)) != -1)
6882 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
6887 else if (uFlags & LVNI_TOLEFT)
6889 if (infoPtr->uView == LV_VIEW_LIST)
6891 nCountPerColumn = LISTVIEW_GetCountPerColumn(infoPtr);
6892 while (nItem - nCountPerColumn >= 0)
6894 nItem -= nCountPerColumn;
6895 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
6899 else if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
6901 /* Special case for autoarrange - move 'til the beginning of a row */
6902 if (is_autoarrange(infoPtr))
6904 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
6905 while (nItem % nCountPerRow > 0)
6908 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
6913 lvFindInfo.flags = LVFI_NEARESTXY;
6914 lvFindInfo.vkDirection = VK_LEFT;
6915 LISTVIEW_GetItemPosition(infoPtr, nItem, &lvFindInfo.pt);
6916 while ((nItem = LISTVIEW_FindItemW(infoPtr, nItem, &lvFindInfo)) != -1)
6918 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
6923 else if (uFlags & LVNI_TORIGHT)
6925 if (infoPtr->uView == LV_VIEW_LIST)
6927 nCountPerColumn = LISTVIEW_GetCountPerColumn(infoPtr);
6928 while (nItem + nCountPerColumn < infoPtr->nItemCount)
6930 nItem += nCountPerColumn;
6931 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
6935 else if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
6937 /* Special case for autoarrange - move 'til the end of a row */
6938 if (is_autoarrange(infoPtr))
6940 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
6941 while (nItem % nCountPerRow < nCountPerRow - 1 )
6944 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
6949 lvFindInfo.flags = LVFI_NEARESTXY;
6950 lvFindInfo.vkDirection = VK_RIGHT;
6951 LISTVIEW_GetItemPosition(infoPtr, nItem, &lvFindInfo.pt);
6952 while ((nItem = LISTVIEW_FindItemW(infoPtr, nItem, &lvFindInfo)) != -1)
6954 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
6963 /* search by index */
6964 for (i = nItem; i < infoPtr->nItemCount; i++)
6966 if ((LISTVIEW_GetItemState(infoPtr, i, uMask) & uMask) == uMask)
6974 /* LISTVIEW_GetNumberOfWorkAreas */
6978 * Retrieves the origin coordinates when in icon or small icon display mode.
6981 * [I] infoPtr : valid pointer to the listview structure
6982 * [O] lpptOrigin : coordinate information
6987 static void LISTVIEW_GetOrigin(const LISTVIEW_INFO *infoPtr, LPPOINT lpptOrigin)
6989 INT nHorzPos = 0, nVertPos = 0;
6990 SCROLLINFO scrollInfo;
6992 scrollInfo.cbSize = sizeof(SCROLLINFO);
6993 scrollInfo.fMask = SIF_POS;
6995 if (GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo))
6996 nHorzPos = scrollInfo.nPos;
6997 if (GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
6998 nVertPos = scrollInfo.nPos;
7000 TRACE("nHorzPos=%d, nVertPos=%d\n", nHorzPos, nVertPos);
7002 lpptOrigin->x = infoPtr->rcList.left;
7003 lpptOrigin->y = infoPtr->rcList.top;
7004 if (infoPtr->uView == LV_VIEW_LIST)
7005 nHorzPos *= infoPtr->nItemWidth;
7006 else if (infoPtr->uView == LV_VIEW_DETAILS)
7007 nVertPos *= infoPtr->nItemHeight;
7009 lpptOrigin->x -= nHorzPos;
7010 lpptOrigin->y -= nVertPos;
7012 TRACE(" origin=%s\n", wine_dbgstr_point(lpptOrigin));
7017 * Retrieves the width of a string.
7020 * [I] hwnd : window handle
7021 * [I] lpszText : text string to process
7022 * [I] isW : TRUE if lpszText is Unicode, FALSE otherwise
7025 * SUCCESS : string width (in pixels)
7028 static INT LISTVIEW_GetStringWidthT(const LISTVIEW_INFO *infoPtr, LPCWSTR lpszText, BOOL isW)
7033 if (is_textT(lpszText, isW))
7035 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
7036 HDC hdc = GetDC(infoPtr->hwndSelf);
7037 HFONT hOldFont = SelectObject(hdc, hFont);
7040 GetTextExtentPointW(hdc, lpszText, lstrlenW(lpszText), &stringSize);
7042 GetTextExtentPointA(hdc, (LPCSTR)lpszText, lstrlenA((LPCSTR)lpszText), &stringSize);
7043 SelectObject(hdc, hOldFont);
7044 ReleaseDC(infoPtr->hwndSelf, hdc);
7046 return stringSize.cx;
7051 * Determines which listview item is located at the specified position.
7054 * [I] infoPtr : valid pointer to the listview structure
7055 * [IO] lpht : hit test information
7056 * [I] subitem : fill out iSubItem.
7057 * [I] select : return the index only if the hit selects the item
7060 * (mm 20001022): We must not allow iSubItem to be touched, for
7061 * an app might pass only a structure with space up to iItem!
7062 * (MS Office 97 does that for instance in the file open dialog)
7065 * SUCCESS : item index
7068 static INT LISTVIEW_HitTest(const LISTVIEW_INFO *infoPtr, LPLVHITTESTINFO lpht, BOOL subitem, BOOL select)
7070 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
7071 RECT rcBox, rcBounds, rcState, rcIcon, rcLabel, rcSearch;
7072 POINT Origin, Position, opt;
7077 TRACE("(pt=%s, subitem=%d, select=%d)\n", wine_dbgstr_point(&lpht->pt), subitem, select);
7081 if (subitem) lpht->iSubItem = 0;
7083 LISTVIEW_GetOrigin(infoPtr, &Origin);
7085 /* set whole list relation flags */
7086 if (subitem && infoPtr->uView == LV_VIEW_DETAILS)
7088 /* LVM_SUBITEMHITTEST checks left bound of possible client area */
7089 if (infoPtr->rcList.left > lpht->pt.x && Origin.x < lpht->pt.x)
7090 lpht->flags |= LVHT_TOLEFT;
7092 if (lpht->pt.y < infoPtr->rcList.top && lpht->pt.y >= 0)
7093 opt.y = lpht->pt.y + infoPtr->rcList.top;
7097 if (infoPtr->rcList.bottom < opt.y)
7098 lpht->flags |= LVHT_BELOW;
7102 if (infoPtr->rcList.left > lpht->pt.x)
7103 lpht->flags |= LVHT_TOLEFT;
7104 else if (infoPtr->rcList.right < lpht->pt.x)
7105 lpht->flags |= LVHT_TORIGHT;
7107 if (infoPtr->rcList.top > lpht->pt.y)
7108 lpht->flags |= LVHT_ABOVE;
7109 else if (infoPtr->rcList.bottom < lpht->pt.y)
7110 lpht->flags |= LVHT_BELOW;
7113 /* even if item is invalid try to find subitem */
7114 if (infoPtr->uView == LV_VIEW_DETAILS && subitem)
7119 opt.x = lpht->pt.x - Origin.x;
7121 lpht->iSubItem = -1;
7122 for (j = 0; j < DPA_GetPtrCount(infoPtr->hdpaColumns); j++)
7124 pRect = &LISTVIEW_GetColumnInfo(infoPtr, j)->rcHeader;
7126 if ((opt.x >= pRect->left) && (opt.x < pRect->right))
7132 TRACE("lpht->iSubItem=%d\n", lpht->iSubItem);
7135 TRACE("lpht->flags=0x%x\n", lpht->flags);
7136 if (lpht->flags) return -1;
7138 lpht->flags |= LVHT_NOWHERE;
7140 /* first deal with the large items */
7141 rcSearch.left = lpht->pt.x;
7142 rcSearch.top = lpht->pt.y;
7143 rcSearch.right = rcSearch.left + 1;
7144 rcSearch.bottom = rcSearch.top + 1;
7146 iterator_frameditems(&i, infoPtr, &rcSearch);
7147 iterator_next(&i); /* go to first item in the sequence */
7149 iterator_destroy(&i);
7151 TRACE("lpht->iItem=%d\n", iItem);
7152 if (iItem == -1) return -1;
7154 lvItem.mask = LVIF_STATE | LVIF_TEXT;
7155 if (infoPtr->uView == LV_VIEW_DETAILS) lvItem.mask |= LVIF_INDENT;
7156 lvItem.stateMask = LVIS_STATEIMAGEMASK;
7157 if (infoPtr->uView == LV_VIEW_ICON) lvItem.stateMask |= LVIS_FOCUSED;
7158 lvItem.iItem = iItem;
7159 lvItem.iSubItem = subitem ? lpht->iSubItem : 0;
7160 lvItem.pszText = szDispText;
7161 lvItem.cchTextMax = DISP_TEXT_SIZE;
7162 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return -1;
7163 if (!infoPtr->bFocus) lvItem.state &= ~LVIS_FOCUSED;
7165 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, &rcBox, NULL, &rcIcon, &rcState, &rcLabel);
7166 LISTVIEW_GetItemOrigin(infoPtr, iItem, &Position);
7167 opt.x = lpht->pt.x - Position.x - Origin.x;
7169 if (lpht->pt.y < infoPtr->rcList.top && lpht->pt.y >= 0)
7170 opt.y = lpht->pt.y - Position.y - Origin.y + infoPtr->rcList.top;
7172 opt.y = lpht->pt.y - Position.y - Origin.y;
7174 if (infoPtr->uView == LV_VIEW_DETAILS)
7177 if (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT)
7178 opt.x = lpht->pt.x - Origin.x;
7182 UnionRect(&rcBounds, &rcIcon, &rcLabel);
7183 UnionRect(&rcBounds, &rcBounds, &rcState);
7185 TRACE("rcBounds=%s\n", wine_dbgstr_rect(&rcBounds));
7186 if (!PtInRect(&rcBounds, opt)) return -1;
7188 if (PtInRect(&rcIcon, opt))
7189 lpht->flags |= LVHT_ONITEMICON;
7190 else if (PtInRect(&rcLabel, opt))
7191 lpht->flags |= LVHT_ONITEMLABEL;
7192 else if (infoPtr->himlState && PtInRect(&rcState, opt))
7193 lpht->flags |= LVHT_ONITEMSTATEICON;
7194 /* special case for LVS_EX_FULLROWSELECT */
7195 if (infoPtr->uView == LV_VIEW_DETAILS && infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT &&
7196 !(lpht->flags & LVHT_ONITEM))
7198 lpht->flags = LVHT_ONITEM | LVHT_ABOVE;
7200 if (lpht->flags & LVHT_ONITEM)
7201 lpht->flags &= ~LVHT_NOWHERE;
7202 TRACE("lpht->flags=0x%x\n", lpht->flags);
7204 if (select && !(infoPtr->uView == LV_VIEW_DETAILS &&
7205 ((infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) ||
7206 (infoPtr->dwStyle & LVS_OWNERDRAWFIXED))))
7208 if (infoPtr->uView == LV_VIEW_DETAILS)
7210 /* get main item bounds */
7211 lvItem.iSubItem = 0;
7212 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, &rcBox, NULL, &rcIcon, &rcState, &rcLabel);
7213 UnionRect(&rcBounds, &rcIcon, &rcLabel);
7214 UnionRect(&rcBounds, &rcBounds, &rcState);
7216 if (!PtInRect(&rcBounds, opt)) iItem = -1;
7218 return lpht->iItem = iItem;
7223 * Inserts a new item in the listview control.
7226 * [I] infoPtr : valid pointer to the listview structure
7227 * [I] lpLVItem : item information
7228 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
7231 * SUCCESS : new item index
7234 static INT LISTVIEW_InsertItemT(LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem, BOOL isW)
7241 BOOL is_sorted, has_changed;
7243 HWND hwndSelf = infoPtr->hwndSelf;
7245 TRACE("(lpLVItem=%s, isW=%d)\n", debuglvitem_t(lpLVItem, isW), isW);
7247 if (infoPtr->dwStyle & LVS_OWNERDATA) return infoPtr->nItemCount++;
7249 /* make sure it's an item, and not a subitem; cannot insert a subitem */
7250 if (!lpLVItem || lpLVItem->iSubItem) return -1;
7252 if (!is_assignable_item(lpLVItem, infoPtr->dwStyle)) return -1;
7254 if (!(lpItem = Alloc(sizeof(ITEM_INFO)))) return -1;
7256 /* insert item in listview control data structure */
7257 if ( !(hdpaSubItems = DPA_Create(8)) ) goto fail;
7258 if ( !DPA_SetPtr(hdpaSubItems, 0, lpItem) ) assert (FALSE);
7260 /* link with id struct */
7261 if (!(lpID = Alloc(sizeof(ITEM_ID)))) goto fail;
7263 lpID->item = hdpaSubItems;
7264 lpID->id = get_next_itemid(infoPtr);
7265 if ( DPA_InsertPtr(infoPtr->hdpaItemIds, infoPtr->nItemCount, lpID) == -1) goto fail;
7267 is_sorted = (infoPtr->dwStyle & (LVS_SORTASCENDING | LVS_SORTDESCENDING)) &&
7268 !(infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (LPSTR_TEXTCALLBACKW != lpLVItem->pszText);
7270 if (lpLVItem->iItem < 0 && !is_sorted) return -1;
7272 /* calculate new item index */
7279 while (i < infoPtr->nItemCount)
7281 hItem = DPA_GetPtr( infoPtr->hdpaItems, i);
7282 item_s = (ITEM_INFO*)DPA_GetPtr(hItem, 0);
7284 cmpv = textcmpWT(item_s->hdr.pszText, lpLVItem->pszText, TRUE);
7285 if (infoPtr->dwStyle & LVS_SORTDESCENDING) cmpv *= -1;
7287 if (cmpv >= 0) break;
7293 nItem = min(lpLVItem->iItem, infoPtr->nItemCount);
7295 TRACE(" inserting at %d, sorted=%d, count=%d, iItem=%d\n", nItem, is_sorted, infoPtr->nItemCount, lpLVItem->iItem);
7296 nItem = DPA_InsertPtr( infoPtr->hdpaItems, nItem, hdpaSubItems );
7297 if (nItem == -1) goto fail;
7298 infoPtr->nItemCount++;
7300 /* shift indices first so they don't get tangled */
7301 LISTVIEW_ShiftIndices(infoPtr, nItem, 1);
7303 /* set the item attributes */
7304 if (lpLVItem->mask & (LVIF_GROUPID|LVIF_COLUMNS))
7306 /* full size structure expected - _WIN32IE >= 0x560 */
7309 else if (lpLVItem->mask & LVIF_INDENT)
7311 /* indent member expected - _WIN32IE >= 0x300 */
7312 memcpy(&item, lpLVItem, offsetof( LVITEMW, iGroupId ));
7316 /* minimal structure expected */
7317 memcpy(&item, lpLVItem, offsetof( LVITEMW, iIndent ));
7320 if (infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES)
7322 item.mask |= LVIF_STATE;
7323 item.stateMask |= LVIS_STATEIMAGEMASK;
7324 item.state &= ~LVIS_STATEIMAGEMASK;
7325 item.state |= INDEXTOSTATEIMAGEMASK(1);
7327 if (!set_main_item(infoPtr, &item, TRUE, isW, &has_changed)) goto undo;
7329 /* make room for the position, if we are in the right mode */
7330 if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
7332 if (DPA_InsertPtr(infoPtr->hdpaPosX, nItem, 0) == -1)
7334 if (DPA_InsertPtr(infoPtr->hdpaPosY, nItem, 0) == -1)
7336 DPA_DeletePtr(infoPtr->hdpaPosX, nItem);
7341 /* send LVN_INSERTITEM notification */
7342 ZeroMemory(&nmlv, sizeof(NMLISTVIEW));
7344 nmlv.lParam = lpItem->lParam;
7345 notify_listview(infoPtr, LVN_INSERTITEM, &nmlv);
7346 if (!IsWindow(hwndSelf))
7349 /* align items (set position of each item) */
7350 if (infoPtr->uView == LV_VIEW_SMALLICON || infoPtr->uView == LV_VIEW_ICON)
7354 if (infoPtr->dwStyle & LVS_ALIGNLEFT)
7355 LISTVIEW_NextIconPosLeft(infoPtr, &pt);
7357 LISTVIEW_NextIconPosTop(infoPtr, &pt);
7359 LISTVIEW_MoveIconTo(infoPtr, nItem, &pt, TRUE);
7362 /* now is the invalidation fun */
7363 LISTVIEW_ScrollOnInsert(infoPtr, nItem, 1);
7367 LISTVIEW_ShiftIndices(infoPtr, nItem, -1);
7368 DPA_DeletePtr(infoPtr->hdpaItems, nItem);
7369 infoPtr->nItemCount--;
7371 DPA_DeletePtr(hdpaSubItems, 0);
7372 DPA_Destroy (hdpaSubItems);
7379 * Checks item visibility.
7382 * [I] infoPtr : valid pointer to the listview structure
7383 * [I] nFirst : item index to check for
7386 * Item visible : TRUE
7387 * Item invisible or failure : FALSE
7389 static BOOL LISTVIEW_IsItemVisible(const LISTVIEW_INFO *infoPtr, INT nItem)
7391 POINT Origin, Position;
7396 TRACE("nItem=%d\n", nItem);
7398 if (nItem < 0 || nItem >= DPA_GetPtrCount(infoPtr->hdpaItems)) return FALSE;
7400 LISTVIEW_GetOrigin(infoPtr, &Origin);
7401 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
7402 rcItem.left = Position.x + Origin.x;
7403 rcItem.top = Position.y + Origin.y;
7404 rcItem.right = rcItem.left + infoPtr->nItemWidth;
7405 rcItem.bottom = rcItem.top + infoPtr->nItemHeight;
7407 hdc = GetDC(infoPtr->hwndSelf);
7408 if (!hdc) return FALSE;
7409 ret = RectVisible(hdc, &rcItem);
7410 ReleaseDC(infoPtr->hwndSelf, hdc);
7417 * Redraws a range of items.
7420 * [I] infoPtr : valid pointer to the listview structure
7421 * [I] nFirst : first item
7422 * [I] nLast : last item
7428 static BOOL LISTVIEW_RedrawItems(const LISTVIEW_INFO *infoPtr, INT nFirst, INT nLast)
7432 if (nLast < nFirst || min(nFirst, nLast) < 0 ||
7433 max(nFirst, nLast) >= infoPtr->nItemCount)
7436 for (i = nFirst; i <= nLast; i++)
7437 LISTVIEW_InvalidateItem(infoPtr, i);
7444 * Scroll the content of a listview.
7447 * [I] infoPtr : valid pointer to the listview structure
7448 * [I] dx : horizontal scroll amount in pixels
7449 * [I] dy : vertical scroll amount in pixels
7456 * If the control is in report view (LV_VIEW_DETAILS) the control can
7457 * be scrolled only in line increments. "dy" will be rounded to the
7458 * nearest number of pixels that are a whole line. Ex: if line height
7459 * is 16 and an 8 is passed, the list will be scrolled by 16. If a 7
7460 * is passed, then the scroll will be 0. (per MSDN 7/2002)
7462 * For: (per experimentation with native control and CSpy ListView)
7463 * LV_VIEW_ICON dy=1 = 1 pixel (vertical only)
7465 * LV_VIEW_SMALLICON dy=1 = 1 pixel (vertical only)
7467 * LV_VIEW_LIST dx=1 = 1 column (horizontal only)
7468 * but will only scroll 1 column per message
7469 * no matter what the value.
7470 * dy must be 0 or FALSE returned.
7471 * LV_VIEW_DETAILS dx=1 = 1 pixel
7475 static BOOL LISTVIEW_Scroll(LISTVIEW_INFO *infoPtr, INT dx, INT dy)
7477 switch(infoPtr->uView) {
7478 case LV_VIEW_DETAILS:
7479 dy += (dy < 0 ? -1 : 1) * infoPtr->nItemHeight/2;
7480 dy /= infoPtr->nItemHeight;
7483 if (dy != 0) return FALSE;
7490 if (dx != 0) LISTVIEW_HScroll(infoPtr, SB_INTERNAL, dx, 0);
7491 if (dy != 0) LISTVIEW_VScroll(infoPtr, SB_INTERNAL, dy, 0);
7498 * Sets the background color.
7501 * [I] infoPtr : valid pointer to the listview structure
7502 * [I] clrBk : background color
7508 static BOOL LISTVIEW_SetBkColor(LISTVIEW_INFO *infoPtr, COLORREF clrBk)
7510 TRACE("(clrBk=%x)\n", clrBk);
7512 if(infoPtr->clrBk != clrBk) {
7513 if (infoPtr->clrBk != CLR_NONE) DeleteObject(infoPtr->hBkBrush);
7514 infoPtr->clrBk = clrBk;
7515 if (clrBk == CLR_NONE)
7516 infoPtr->hBkBrush = (HBRUSH)GetClassLongPtrW(infoPtr->hwndSelf, GCLP_HBRBACKGROUND);
7519 infoPtr->hBkBrush = CreateSolidBrush(clrBk);
7520 infoPtr->dwLvExStyle &= ~LVS_EX_TRANSPARENTBKGND;
7522 LISTVIEW_InvalidateList(infoPtr);
7528 /* LISTVIEW_SetBkImage */
7530 /*** Helper for {Insert,Set}ColumnT *only* */
7531 static void column_fill_hditem(const LISTVIEW_INFO *infoPtr, HDITEMW *lphdi, INT nColumn,
7532 const LVCOLUMNW *lpColumn, BOOL isW)
7534 if (lpColumn->mask & LVCF_FMT)
7536 /* format member is valid */
7537 lphdi->mask |= HDI_FORMAT;
7539 /* set text alignment (leftmost column must be left-aligned) */
7540 if (nColumn == 0 || (lpColumn->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_LEFT)
7541 lphdi->fmt |= HDF_LEFT;
7542 else if ((lpColumn->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_RIGHT)
7543 lphdi->fmt |= HDF_RIGHT;
7544 else if ((lpColumn->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_CENTER)
7545 lphdi->fmt |= HDF_CENTER;
7547 if (lpColumn->fmt & LVCFMT_BITMAP_ON_RIGHT)
7548 lphdi->fmt |= HDF_BITMAP_ON_RIGHT;
7550 if (lpColumn->fmt & LVCFMT_COL_HAS_IMAGES)
7552 lphdi->fmt |= HDF_IMAGE;
7553 lphdi->iImage = I_IMAGECALLBACK;
7556 if (lpColumn->fmt & LVCFMT_FIXED_WIDTH)
7557 lphdi->fmt |= HDF_FIXEDWIDTH;
7560 if (lpColumn->mask & LVCF_WIDTH)
7562 lphdi->mask |= HDI_WIDTH;
7563 if(lpColumn->cx == LVSCW_AUTOSIZE_USEHEADER)
7565 /* make it fill the remainder of the controls width */
7569 for(item_index = 0; item_index < (nColumn - 1); item_index++)
7571 LISTVIEW_GetHeaderRect(infoPtr, item_index, &rcHeader);
7572 lphdi->cxy += rcHeader.right - rcHeader.left;
7575 /* retrieve the layout of the header */
7576 GetClientRect(infoPtr->hwndSelf, &rcHeader);
7577 TRACE("start cxy=%d rcHeader=%s\n", lphdi->cxy, wine_dbgstr_rect(&rcHeader));
7579 lphdi->cxy = (rcHeader.right - rcHeader.left) - lphdi->cxy;
7582 lphdi->cxy = lpColumn->cx;
7585 if (lpColumn->mask & LVCF_TEXT)
7587 lphdi->mask |= HDI_TEXT | HDI_FORMAT;
7588 lphdi->fmt |= HDF_STRING;
7589 lphdi->pszText = lpColumn->pszText;
7590 lphdi->cchTextMax = textlenT(lpColumn->pszText, isW);
7593 if (lpColumn->mask & LVCF_IMAGE)
7595 lphdi->mask |= HDI_IMAGE;
7596 lphdi->iImage = lpColumn->iImage;
7599 if (lpColumn->mask & LVCF_ORDER)
7601 lphdi->mask |= HDI_ORDER;
7602 lphdi->iOrder = lpColumn->iOrder;
7609 * Inserts a new column.
7612 * [I] infoPtr : valid pointer to the listview structure
7613 * [I] nColumn : column index
7614 * [I] lpColumn : column information
7615 * [I] isW : TRUE if lpColumn is Unicode, FALSE otherwise
7618 * SUCCESS : new column index
7621 static INT LISTVIEW_InsertColumnT(LISTVIEW_INFO *infoPtr, INT nColumn,
7622 const LVCOLUMNW *lpColumn, BOOL isW)
7624 COLUMN_INFO *lpColumnInfo;
7628 TRACE("(nColumn=%d, lpColumn=%s, isW=%d)\n", nColumn, debuglvcolumn_t(lpColumn, isW), isW);
7630 if (!lpColumn || nColumn < 0) return -1;
7631 nColumn = min(nColumn, DPA_GetPtrCount(infoPtr->hdpaColumns));
7633 ZeroMemory(&hdi, sizeof(HDITEMW));
7634 column_fill_hditem(infoPtr, &hdi, nColumn, lpColumn, isW);
7637 * A mask not including LVCF_WIDTH turns into a mask of width, width 10
7638 * (can be seen in SPY) otherwise column never gets added.
7640 if (!(lpColumn->mask & LVCF_WIDTH)) {
7641 hdi.mask |= HDI_WIDTH;
7646 * when the iSubItem is available Windows copies it to the header lParam. It seems
7647 * to happen only in LVM_INSERTCOLUMN - not in LVM_SETCOLUMN
7649 if (lpColumn->mask & LVCF_SUBITEM)
7651 hdi.mask |= HDI_LPARAM;
7652 hdi.lParam = lpColumn->iSubItem;
7655 /* create header if not present */
7656 LISTVIEW_CreateHeader(infoPtr);
7657 if (!(LVS_NOCOLUMNHEADER & infoPtr->dwStyle) &&
7658 (infoPtr->uView == LV_VIEW_DETAILS) && (WS_VISIBLE & infoPtr->dwStyle))
7660 ShowWindow(infoPtr->hwndHeader, SW_SHOWNORMAL);
7663 /* insert item in header control */
7664 nNewColumn = SendMessageW(infoPtr->hwndHeader,
7665 isW ? HDM_INSERTITEMW : HDM_INSERTITEMA,
7666 (WPARAM)nColumn, (LPARAM)&hdi);
7667 if (nNewColumn == -1) return -1;
7668 if (nNewColumn != nColumn) ERR("nColumn=%d, nNewColumn=%d\n", nColumn, nNewColumn);
7670 /* create our own column info */
7671 if (!(lpColumnInfo = Alloc(sizeof(COLUMN_INFO)))) goto fail;
7672 if (DPA_InsertPtr(infoPtr->hdpaColumns, nNewColumn, lpColumnInfo) == -1) goto fail;
7674 if (lpColumn->mask & LVCF_FMT) lpColumnInfo->fmt = lpColumn->fmt;
7675 if (lpColumn->mask & LVCF_MINWIDTH) lpColumnInfo->cxMin = lpColumn->cxMin;
7676 if (!SendMessageW(infoPtr->hwndHeader, HDM_GETITEMRECT, nNewColumn, (LPARAM)&lpColumnInfo->rcHeader))
7679 /* now we have to actually adjust the data */
7680 if (!(infoPtr->dwStyle & LVS_OWNERDATA) && infoPtr->nItemCount > 0)
7682 SUBITEM_INFO *lpSubItem;
7686 for (nItem = 0; nItem < infoPtr->nItemCount; nItem++)
7688 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, nItem);
7689 for (i = 1; i < DPA_GetPtrCount(hdpaSubItems); i++)
7691 lpSubItem = DPA_GetPtr(hdpaSubItems, i);
7692 if (lpSubItem->iSubItem >= nNewColumn)
7693 lpSubItem->iSubItem++;
7698 /* make space for the new column */
7699 LISTVIEW_ScrollColumns(infoPtr, nNewColumn + 1, lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left);
7700 LISTVIEW_UpdateItemSize(infoPtr);
7705 if (nNewColumn != -1) SendMessageW(infoPtr->hwndHeader, HDM_DELETEITEM, nNewColumn, 0);
7708 DPA_DeletePtr(infoPtr->hdpaColumns, nNewColumn);
7716 * Sets the attributes of a header item.
7719 * [I] infoPtr : valid pointer to the listview structure
7720 * [I] nColumn : column index
7721 * [I] lpColumn : column attributes
7722 * [I] isW: if TRUE, then lpColumn is a LPLVCOLUMNW, else it is a LPLVCOLUMNA
7728 static BOOL LISTVIEW_SetColumnT(const LISTVIEW_INFO *infoPtr, INT nColumn,
7729 const LVCOLUMNW *lpColumn, BOOL isW)
7731 HDITEMW hdi, hdiget;
7734 TRACE("(nColumn=%d, lpColumn=%s, isW=%d)\n", nColumn, debuglvcolumn_t(lpColumn, isW), isW);
7736 if (!lpColumn || nColumn < 0 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
7738 ZeroMemory(&hdi, sizeof(HDITEMW));
7739 if (lpColumn->mask & LVCF_FMT)
7741 hdi.mask |= HDI_FORMAT;
7742 hdiget.mask = HDI_FORMAT;
7743 if (SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nColumn, (LPARAM)&hdiget))
7744 hdi.fmt = hdiget.fmt & HDF_STRING;
7746 column_fill_hditem(infoPtr, &hdi, nColumn, lpColumn, isW);
7748 /* set header item attributes */
7749 bResult = SendMessageW(infoPtr->hwndHeader, isW ? HDM_SETITEMW : HDM_SETITEMA, (WPARAM)nColumn, (LPARAM)&hdi);
7750 if (!bResult) return FALSE;
7752 if (lpColumn->mask & LVCF_FMT)
7754 COLUMN_INFO *lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, nColumn);
7755 INT oldFmt = lpColumnInfo->fmt;
7757 lpColumnInfo->fmt = lpColumn->fmt;
7758 if ((oldFmt ^ lpColumn->fmt) & (LVCFMT_JUSTIFYMASK | LVCFMT_IMAGE))
7760 if (infoPtr->uView == LV_VIEW_DETAILS) LISTVIEW_InvalidateColumn(infoPtr, nColumn);
7764 if (lpColumn->mask & LVCF_MINWIDTH)
7765 LISTVIEW_GetColumnInfo(infoPtr, nColumn)->cxMin = lpColumn->cxMin;
7772 * Sets the column order array
7775 * [I] infoPtr : valid pointer to the listview structure
7776 * [I] iCount : number of elements in column order array
7777 * [I] lpiArray : pointer to column order array
7783 static BOOL LISTVIEW_SetColumnOrderArray(LISTVIEW_INFO *infoPtr, INT iCount, const INT *lpiArray)
7785 TRACE("iCount %d lpiArray %p\n", iCount, lpiArray);
7787 if (!lpiArray || !IsWindow(infoPtr->hwndHeader)) return FALSE;
7789 infoPtr->colRectsDirty = TRUE;
7791 return SendMessageW(infoPtr->hwndHeader, HDM_SETORDERARRAY, iCount, (LPARAM)lpiArray);
7796 * Sets the width of a column
7799 * [I] infoPtr : valid pointer to the listview structure
7800 * [I] nColumn : column index
7801 * [I] cx : column width
7807 static BOOL LISTVIEW_SetColumnWidth(LISTVIEW_INFO *infoPtr, INT nColumn, INT cx)
7809 WCHAR szDispText[DISP_TEXT_SIZE] = { 0 };
7813 TRACE("(nColumn=%d, cx=%d\n", nColumn, cx);
7815 /* set column width only if in report or list mode */
7816 if (infoPtr->uView != LV_VIEW_DETAILS && infoPtr->uView != LV_VIEW_LIST) return FALSE;
7818 /* take care of invalid cx values */
7819 if(infoPtr->uView == LV_VIEW_DETAILS && cx < -2) cx = LVSCW_AUTOSIZE;
7820 else if (infoPtr->uView == LV_VIEW_LIST && cx < 1) return FALSE;
7822 /* resize all columns if in LV_VIEW_LIST mode */
7823 if(infoPtr->uView == LV_VIEW_LIST)
7825 infoPtr->nItemWidth = cx;
7826 LISTVIEW_InvalidateList(infoPtr);
7830 if (nColumn < 0 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
7832 if (cx == LVSCW_AUTOSIZE || (cx == LVSCW_AUTOSIZE_USEHEADER && nColumn < DPA_GetPtrCount(infoPtr->hdpaColumns) -1))
7837 lvItem.mask = LVIF_TEXT;
7839 lvItem.iSubItem = nColumn;
7840 lvItem.pszText = szDispText;
7841 lvItem.cchTextMax = DISP_TEXT_SIZE;
7842 for (; lvItem.iItem < infoPtr->nItemCount; lvItem.iItem++)
7844 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) continue;
7845 nLabelWidth = LISTVIEW_GetStringWidthT(infoPtr, lvItem.pszText, TRUE);
7846 if (max_cx < nLabelWidth) max_cx = nLabelWidth;
7848 if (infoPtr->himlSmall && (nColumn == 0 || (LISTVIEW_GetColumnInfo(infoPtr, nColumn)->fmt & LVCFMT_IMAGE)))
7849 max_cx += infoPtr->iconSize.cx;
7850 max_cx += TRAILING_LABEL_PADDING;
7853 /* autosize based on listview items width */
7854 if(cx == LVSCW_AUTOSIZE)
7856 else if(cx == LVSCW_AUTOSIZE_USEHEADER)
7858 /* if iCol is the last column make it fill the remainder of the controls width */
7859 if(nColumn == DPA_GetPtrCount(infoPtr->hdpaColumns) - 1)
7864 LISTVIEW_GetOrigin(infoPtr, &Origin);
7865 LISTVIEW_GetHeaderRect(infoPtr, nColumn, &rcHeader);
7867 cx = infoPtr->rcList.right - Origin.x - rcHeader.left;
7871 /* Despite what the MS docs say, if this is not the last
7872 column, then MS resizes the column to the width of the
7873 largest text string in the column, including headers
7874 and items. This is different from LVSCW_AUTOSIZE in that
7875 LVSCW_AUTOSIZE ignores the header string length. */
7878 /* retrieve header text */
7879 hdi.mask = HDI_TEXT;
7880 hdi.cchTextMax = DISP_TEXT_SIZE;
7881 hdi.pszText = szDispText;
7882 if (SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nColumn, (LPARAM)&hdi))
7884 HDC hdc = GetDC(infoPtr->hwndSelf);
7885 HFONT old_font = SelectObject(hdc, (HFONT)SendMessageW(infoPtr->hwndHeader, WM_GETFONT, 0, 0));
7888 if (GetTextExtentPoint32W(hdc, hdi.pszText, lstrlenW(hdi.pszText), &size))
7889 cx = size.cx + TRAILING_HEADER_PADDING;
7890 /* FIXME: Take into account the header image, if one is present */
7891 SelectObject(hdc, old_font);
7892 ReleaseDC(infoPtr->hwndSelf, hdc);
7894 cx = max (cx, max_cx);
7898 if (cx < 0) return FALSE;
7900 /* call header to update the column change */
7901 hdi.mask = HDI_WIDTH;
7902 hdi.cxy = max(cx, LISTVIEW_GetColumnInfo(infoPtr, nColumn)->cxMin);
7903 TRACE("hdi.cxy=%d\n", hdi.cxy);
7904 return SendMessageW(infoPtr->hwndHeader, HDM_SETITEMW, nColumn, (LPARAM)&hdi);
7908 * Creates the checkbox imagelist. Helper for LISTVIEW_SetExtendedListViewStyle
7911 static HIMAGELIST LISTVIEW_CreateCheckBoxIL(const LISTVIEW_INFO *infoPtr)
7914 HBITMAP hbm_im, hbm_mask, hbm_orig;
7916 HBRUSH hbr_white = GetStockObject(WHITE_BRUSH);
7917 HBRUSH hbr_black = GetStockObject(BLACK_BRUSH);
7920 himl = ImageList_Create(GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON),
7921 ILC_COLOR | ILC_MASK, 2, 2);
7922 hdc_wnd = GetDC(infoPtr->hwndSelf);
7923 hdc = CreateCompatibleDC(hdc_wnd);
7924 hbm_im = CreateCompatibleBitmap(hdc_wnd, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON));
7925 hbm_mask = CreateBitmap(GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), 1, 1, NULL);
7926 ReleaseDC(infoPtr->hwndSelf, hdc_wnd);
7928 rc.left = rc.top = 0;
7929 rc.right = GetSystemMetrics(SM_CXSMICON);
7930 rc.bottom = GetSystemMetrics(SM_CYSMICON);
7932 hbm_orig = SelectObject(hdc, hbm_mask);
7933 FillRect(hdc, &rc, hbr_white);
7934 InflateRect(&rc, -2, -2);
7935 FillRect(hdc, &rc, hbr_black);
7937 SelectObject(hdc, hbm_im);
7938 DrawFrameControl(hdc, &rc, DFC_BUTTON, DFCS_BUTTONCHECK | DFCS_MONO);
7939 SelectObject(hdc, hbm_orig);
7940 ImageList_Add(himl, hbm_im, hbm_mask);
7942 SelectObject(hdc, hbm_im);
7943 DrawFrameControl(hdc, &rc, DFC_BUTTON, DFCS_BUTTONCHECK | DFCS_MONO | DFCS_CHECKED);
7944 SelectObject(hdc, hbm_orig);
7945 ImageList_Add(himl, hbm_im, hbm_mask);
7947 DeleteObject(hbm_mask);
7948 DeleteObject(hbm_im);
7956 * Sets the extended listview style.
7959 * [I] infoPtr : valid pointer to the listview structure
7961 * [I] dwStyle : style
7964 * SUCCESS : previous style
7967 static DWORD LISTVIEW_SetExtendedListViewStyle(LISTVIEW_INFO *infoPtr, DWORD dwMask, DWORD dwExStyle)
7969 DWORD dwOldExStyle = infoPtr->dwLvExStyle;
7973 infoPtr->dwLvExStyle = (dwOldExStyle & ~dwMask) | (dwExStyle & dwMask);
7975 infoPtr->dwLvExStyle = dwExStyle;
7977 if((infoPtr->dwLvExStyle ^ dwOldExStyle) & LVS_EX_CHECKBOXES)
7979 HIMAGELIST himl = 0;
7980 if(infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES)
7983 item.mask = LVIF_STATE;
7984 item.stateMask = LVIS_STATEIMAGEMASK;
7985 item.state = INDEXTOSTATEIMAGEMASK(1);
7986 LISTVIEW_SetItemState(infoPtr, -1, &item);
7988 himl = LISTVIEW_CreateCheckBoxIL(infoPtr);
7989 if(!(infoPtr->dwStyle & LVS_SHAREIMAGELISTS))
7990 ImageList_Destroy(infoPtr->himlState);
7992 himl = LISTVIEW_SetImageList(infoPtr, LVSIL_STATE, himl);
7993 /* checkbox list replaces prevous custom list or... */
7994 if(((infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES) &&
7995 !(infoPtr->dwStyle & LVS_SHAREIMAGELISTS)) ||
7996 /* ...previous was checkbox list */
7997 (dwOldExStyle & LVS_EX_CHECKBOXES))
7998 ImageList_Destroy(himl);
8001 if((infoPtr->dwLvExStyle ^ dwOldExStyle) & LVS_EX_HEADERDRAGDROP)
8005 /* if not already created */
8006 LISTVIEW_CreateHeader(infoPtr);
8008 dwStyle = GetWindowLongW(infoPtr->hwndHeader, GWL_STYLE);
8009 if (infoPtr->dwLvExStyle & LVS_EX_HEADERDRAGDROP)
8010 dwStyle |= HDS_DRAGDROP;
8012 dwStyle &= ~HDS_DRAGDROP;
8013 SetWindowLongW(infoPtr->hwndHeader, GWL_STYLE, dwStyle);
8016 /* GRIDLINES adds decoration at top so changes sizes */
8017 if((infoPtr->dwLvExStyle ^ dwOldExStyle) & LVS_EX_GRIDLINES)
8019 LISTVIEW_UpdateSize(infoPtr);
8022 if((infoPtr->dwLvExStyle ^ dwOldExStyle) & LVS_EX_TRANSPARENTBKGND)
8024 if (infoPtr->dwLvExStyle & LVS_EX_TRANSPARENTBKGND)
8025 LISTVIEW_SetBkColor(infoPtr, CLR_NONE);
8028 LISTVIEW_InvalidateList(infoPtr);
8029 return dwOldExStyle;
8034 * Sets the new hot cursor used during hot tracking and hover selection.
8037 * [I] infoPtr : valid pointer to the listview structure
8038 * [I] hCursor : the new hot cursor handle
8041 * Returns the previous hot cursor
8043 static HCURSOR LISTVIEW_SetHotCursor(LISTVIEW_INFO *infoPtr, HCURSOR hCursor)
8045 HCURSOR oldCursor = infoPtr->hHotCursor;
8047 infoPtr->hHotCursor = hCursor;
8055 * Sets the hot item index.
8058 * [I] infoPtr : valid pointer to the listview structure
8059 * [I] iIndex : index
8062 * SUCCESS : previous hot item index
8063 * FAILURE : -1 (no hot item)
8065 static INT LISTVIEW_SetHotItem(LISTVIEW_INFO *infoPtr, INT iIndex)
8067 INT iOldIndex = infoPtr->nHotItem;
8069 infoPtr->nHotItem = iIndex;
8077 * Sets the amount of time the cursor must hover over an item before it is selected.
8080 * [I] infoPtr : valid pointer to the listview structure
8081 * [I] dwHoverTime : hover time, if -1 the hover time is set to the default
8084 * Returns the previous hover time
8086 static DWORD LISTVIEW_SetHoverTime(LISTVIEW_INFO *infoPtr, DWORD dwHoverTime)
8088 DWORD oldHoverTime = infoPtr->dwHoverTime;
8090 infoPtr->dwHoverTime = dwHoverTime;
8092 return oldHoverTime;
8097 * Sets spacing for icons of LVS_ICON style.
8100 * [I] infoPtr : valid pointer to the listview structure
8101 * [I] cx : horizontal spacing (-1 = system spacing, 0 = autosize)
8102 * [I] cy : vertical spacing (-1 = system spacing, 0 = autosize)
8105 * MAKELONG(oldcx, oldcy)
8107 static DWORD LISTVIEW_SetIconSpacing(LISTVIEW_INFO *infoPtr, INT cx, INT cy)
8109 DWORD oldspacing = MAKELONG(infoPtr->iconSpacing.cx, infoPtr->iconSpacing.cy);
8111 TRACE("requested=(%d,%d)\n", cx, cy);
8113 /* this is supported only for LVS_ICON style */
8114 if (infoPtr->uView != LV_VIEW_ICON) return oldspacing;
8116 /* set to defaults, if instructed to */
8117 if (cx == -1) cx = GetSystemMetrics(SM_CXICONSPACING);
8118 if (cy == -1) cy = GetSystemMetrics(SM_CYICONSPACING);
8120 /* if 0 then compute width
8121 * FIXME: Should scan each item and determine max width of
8122 * icon or label, then make that the width */
8124 cx = infoPtr->iconSpacing.cx;
8126 /* if 0 then compute height */
8128 cy = infoPtr->iconSize.cy + 2 * infoPtr->ntmHeight +
8129 ICON_BOTTOM_PADDING + ICON_TOP_PADDING + LABEL_VERT_PADDING;
8132 infoPtr->iconSpacing.cx = cx;
8133 infoPtr->iconSpacing.cy = cy;
8135 TRACE("old=(%d,%d), new=(%d,%d), iconSize=(%d,%d), ntmH=%d\n",
8136 LOWORD(oldspacing), HIWORD(oldspacing), cx, cy,
8137 infoPtr->iconSize.cx, infoPtr->iconSize.cy,
8138 infoPtr->ntmHeight);
8140 /* these depend on the iconSpacing */
8141 LISTVIEW_UpdateItemSize(infoPtr);
8146 static inline void set_icon_size(SIZE *size, HIMAGELIST himl, BOOL small)
8150 if (himl && ImageList_GetIconSize(himl, &cx, &cy))
8157 size->cx = GetSystemMetrics(small ? SM_CXSMICON : SM_CXICON);
8158 size->cy = GetSystemMetrics(small ? SM_CYSMICON : SM_CYICON);
8167 * [I] infoPtr : valid pointer to the listview structure
8168 * [I] nType : image list type
8169 * [I] himl : image list handle
8172 * SUCCESS : old image list
8175 static HIMAGELIST LISTVIEW_SetImageList(LISTVIEW_INFO *infoPtr, INT nType, HIMAGELIST himl)
8177 INT oldHeight = infoPtr->nItemHeight;
8178 HIMAGELIST himlOld = 0;
8180 TRACE("(nType=%d, himl=%p\n", nType, himl);
8185 himlOld = infoPtr->himlNormal;
8186 infoPtr->himlNormal = himl;
8187 if (infoPtr->uView == LV_VIEW_ICON) set_icon_size(&infoPtr->iconSize, himl, FALSE);
8188 LISTVIEW_SetIconSpacing(infoPtr, 0, 0);
8192 himlOld = infoPtr->himlSmall;
8193 infoPtr->himlSmall = himl;
8194 if (infoPtr->uView != LV_VIEW_ICON) set_icon_size(&infoPtr->iconSize, himl, TRUE);
8198 himlOld = infoPtr->himlState;
8199 infoPtr->himlState = himl;
8200 set_icon_size(&infoPtr->iconStateSize, himl, TRUE);
8201 ImageList_SetBkColor(infoPtr->himlState, CLR_NONE);
8205 ERR("Unknown icon type=%d\n", nType);
8209 infoPtr->nItemHeight = LISTVIEW_CalculateItemHeight(infoPtr);
8210 if (infoPtr->nItemHeight != oldHeight)
8211 LISTVIEW_UpdateScroll(infoPtr);
8218 * Preallocates memory (does *not* set the actual count of items !)
8221 * [I] infoPtr : valid pointer to the listview structure
8222 * [I] nItems : item count (projected number of items to allocate)
8223 * [I] dwFlags : update flags
8229 static BOOL LISTVIEW_SetItemCount(LISTVIEW_INFO *infoPtr, INT nItems, DWORD dwFlags)
8231 TRACE("(nItems=%d, dwFlags=%x)\n", nItems, dwFlags);
8233 if (infoPtr->dwStyle & LVS_OWNERDATA)
8235 INT nOldCount = infoPtr->nItemCount;
8237 if (nItems < nOldCount)
8239 RANGE range = { nItems, nOldCount };
8240 ranges_del(infoPtr->selectionRanges, range);
8241 if (infoPtr->nFocusedItem >= nItems)
8243 LISTVIEW_SetItemFocus(infoPtr, -1);
8244 SetRectEmpty(&infoPtr->rcFocus);
8248 infoPtr->nItemCount = nItems;
8249 LISTVIEW_UpdateScroll(infoPtr);
8251 /* the flags are valid only in ownerdata report and list modes */
8252 if (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON) dwFlags = 0;
8254 if (!(dwFlags & LVSICF_NOSCROLL) && infoPtr->nFocusedItem != -1)
8255 LISTVIEW_EnsureVisible(infoPtr, infoPtr->nFocusedItem, FALSE);
8257 if (!(dwFlags & LVSICF_NOINVALIDATEALL))
8258 LISTVIEW_InvalidateList(infoPtr);
8265 LISTVIEW_GetOrigin(infoPtr, &Origin);
8266 nFrom = min(nOldCount, nItems);
8267 nTo = max(nOldCount, nItems);
8269 if (infoPtr->uView == LV_VIEW_DETAILS)
8272 rcErase.top = nFrom * infoPtr->nItemHeight;
8273 rcErase.right = infoPtr->nItemWidth;
8274 rcErase.bottom = nTo * infoPtr->nItemHeight;
8275 OffsetRect(&rcErase, Origin.x, Origin.y);
8276 if (IntersectRect(&rcErase, &rcErase, &infoPtr->rcList))
8277 LISTVIEW_InvalidateRect(infoPtr, &rcErase);
8279 else /* LV_VIEW_LIST */
8281 INT nPerCol = LISTVIEW_GetCountPerColumn(infoPtr);
8283 rcErase.left = (nFrom / nPerCol) * infoPtr->nItemWidth;
8284 rcErase.top = (nFrom % nPerCol) * infoPtr->nItemHeight;
8285 rcErase.right = rcErase.left + infoPtr->nItemWidth;
8286 rcErase.bottom = nPerCol * infoPtr->nItemHeight;
8287 OffsetRect(&rcErase, Origin.x, Origin.y);
8288 if (IntersectRect(&rcErase, &rcErase, &infoPtr->rcList))
8289 LISTVIEW_InvalidateRect(infoPtr, &rcErase);
8291 rcErase.left = (nFrom / nPerCol + 1) * infoPtr->nItemWidth;
8293 rcErase.right = (nTo / nPerCol + 1) * infoPtr->nItemWidth;
8294 rcErase.bottom = nPerCol * infoPtr->nItemHeight;
8295 OffsetRect(&rcErase, Origin.x, Origin.y);
8296 if (IntersectRect(&rcErase, &rcErase, &infoPtr->rcList))
8297 LISTVIEW_InvalidateRect(infoPtr, &rcErase);
8303 /* According to MSDN for non-LVS_OWNERDATA this is just
8304 * a performance issue. The control allocates its internal
8305 * data structures for the number of items specified. It
8306 * cuts down on the number of memory allocations. Therefore
8307 * we will just issue a WARN here
8309 WARN("for non-ownerdata performance option not implemented.\n");
8317 * Sets the position of an item.
8320 * [I] infoPtr : valid pointer to the listview structure
8321 * [I] nItem : item index
8322 * [I] pt : coordinate
8328 static BOOL LISTVIEW_SetItemPosition(LISTVIEW_INFO *infoPtr, INT nItem, POINT pt)
8332 TRACE("(nItem=%d, &pt=%s\n", nItem, wine_dbgstr_point(&pt));
8334 if (nItem < 0 || nItem >= infoPtr->nItemCount ||
8335 !(infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON)) return FALSE;
8337 LISTVIEW_GetOrigin(infoPtr, &Origin);
8339 /* This point value seems to be an undocumented feature.
8340 * The best guess is that it means either at the origin,
8341 * or at true beginning of the list. I will assume the origin. */
8342 if ((pt.x == -1) && (pt.y == -1))
8345 if (infoPtr->uView == LV_VIEW_ICON)
8347 pt.x -= (infoPtr->nItemWidth - infoPtr->iconSize.cx) / 2;
8348 pt.y -= ICON_TOP_PADDING;
8353 infoPtr->bAutoarrange = FALSE;
8355 return LISTVIEW_MoveIconTo(infoPtr, nItem, &pt, FALSE);
8360 * Sets the state of one or many items.
8363 * [I] infoPtr : valid pointer to the listview structure
8364 * [I] nItem : item index
8365 * [I] lpLVItem : item or subitem info
8371 static BOOL LISTVIEW_SetItemState(LISTVIEW_INFO *infoPtr, INT nItem, const LVITEMW *lpLVItem)
8373 BOOL bResult = TRUE;
8376 lvItem.iItem = nItem;
8377 lvItem.iSubItem = 0;
8378 lvItem.mask = LVIF_STATE;
8379 lvItem.state = lpLVItem->state;
8380 lvItem.stateMask = lpLVItem->stateMask;
8381 TRACE("lvItem=%s\n", debuglvitem_t(&lvItem, TRUE));
8385 /* select all isn't allowed in LVS_SINGLESEL */
8386 if ((lvItem.state & lvItem.stateMask & LVIS_SELECTED) && (infoPtr->dwStyle & LVS_SINGLESEL))
8389 /* focus all isn't allowed */
8390 if (lvItem.state & lvItem.stateMask & LVIS_FOCUSED) return FALSE;
8392 /* apply to all items */
8393 for (lvItem.iItem = 0; lvItem.iItem < infoPtr->nItemCount; lvItem.iItem++)
8394 if (!LISTVIEW_SetItemT(infoPtr, &lvItem, TRUE)) bResult = FALSE;
8397 bResult = LISTVIEW_SetItemT(infoPtr, &lvItem, TRUE);
8404 * Sets the text of an item or subitem.
8407 * [I] hwnd : window handle
8408 * [I] nItem : item index
8409 * [I] lpLVItem : item or subitem info
8410 * [I] isW : TRUE if input is Unicode
8416 static BOOL LISTVIEW_SetItemTextT(LISTVIEW_INFO *infoPtr, INT nItem, const LVITEMW *lpLVItem, BOOL isW)
8420 if (nItem < 0 && nItem >= infoPtr->nItemCount) return FALSE;
8422 lvItem.iItem = nItem;
8423 lvItem.iSubItem = lpLVItem->iSubItem;
8424 lvItem.mask = LVIF_TEXT;
8425 lvItem.pszText = lpLVItem->pszText;
8426 lvItem.cchTextMax = lpLVItem->cchTextMax;
8428 TRACE("(nItem=%d, lpLVItem=%s, isW=%d)\n", nItem, debuglvitem_t(&lvItem, isW), isW);
8430 return LISTVIEW_SetItemT(infoPtr, &lvItem, isW);
8435 * Set item index that marks the start of a multiple selection.
8438 * [I] infoPtr : valid pointer to the listview structure
8439 * [I] nIndex : index
8442 * Index number or -1 if there is no selection mark.
8444 static INT LISTVIEW_SetSelectionMark(LISTVIEW_INFO *infoPtr, INT nIndex)
8446 INT nOldIndex = infoPtr->nSelectionMark;
8448 TRACE("(nIndex=%d)\n", nIndex);
8450 infoPtr->nSelectionMark = nIndex;
8457 * Sets the text background color.
8460 * [I] infoPtr : valid pointer to the listview structure
8461 * [I] clrTextBk : text background color
8467 static BOOL LISTVIEW_SetTextBkColor(LISTVIEW_INFO *infoPtr, COLORREF clrTextBk)
8469 TRACE("(clrTextBk=%x)\n", clrTextBk);
8471 if (infoPtr->clrTextBk != clrTextBk)
8473 infoPtr->clrTextBk = clrTextBk;
8474 LISTVIEW_InvalidateList(infoPtr);
8482 * Sets the text foreground color.
8485 * [I] infoPtr : valid pointer to the listview structure
8486 * [I] clrText : text color
8492 static BOOL LISTVIEW_SetTextColor (LISTVIEW_INFO *infoPtr, COLORREF clrText)
8494 TRACE("(clrText=%x)\n", clrText);
8496 if (infoPtr->clrText != clrText)
8498 infoPtr->clrText = clrText;
8499 LISTVIEW_InvalidateList(infoPtr);
8507 * Sets new ToolTip window to ListView control.
8510 * [I] infoPtr : valid pointer to the listview structure
8511 * [I] hwndNewToolTip : handle to new ToolTip
8516 static HWND LISTVIEW_SetToolTips( LISTVIEW_INFO *infoPtr, HWND hwndNewToolTip)
8518 HWND hwndOldToolTip = infoPtr->hwndToolTip;
8519 infoPtr->hwndToolTip = hwndNewToolTip;
8520 return hwndOldToolTip;
8525 * sets the Unicode character format flag for the control
8527 * [I] infoPtr :valid pointer to the listview structure
8528 * [I] fUnicode :true to switch to UNICODE false to switch to ANSI
8531 * Old Unicode Format
8533 static BOOL LISTVIEW_SetUnicodeFormat( LISTVIEW_INFO *infoPtr, BOOL fUnicode)
8535 SHORT rc = infoPtr->notifyFormat;
8536 infoPtr->notifyFormat = (fUnicode) ? NFR_UNICODE : NFR_ANSI;
8537 return rc == NFR_UNICODE;
8542 * sets the control view mode
8544 * [I] infoPtr :valid pointer to the listview structure
8545 * [I] nView :new view mode value
8551 static INT LISTVIEW_SetView(LISTVIEW_INFO *infoPtr, DWORD nView)
8553 SIZE oldIconSize = infoPtr->iconSize;
8556 if (infoPtr->uView == nView) return 1;
8558 if ((INT)nView < 0 || nView > LV_VIEW_MAX) return -1;
8559 if (nView == LV_VIEW_TILE)
8561 FIXME("View LV_VIEW_TILE unimplemented\n");
8565 infoPtr->uView = nView;
8567 SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
8568 ShowWindow(infoPtr->hwndHeader, SW_HIDE);
8570 ShowScrollBar(infoPtr->hwndSelf, SB_BOTH, FALSE);
8571 SetRectEmpty(&infoPtr->rcFocus);
8573 himl = (nView == LV_VIEW_ICON ? infoPtr->himlNormal : infoPtr->himlSmall);
8574 set_icon_size(&infoPtr->iconSize, himl, nView != LV_VIEW_ICON);
8579 if ((infoPtr->iconSize.cx != oldIconSize.cx) || (infoPtr->iconSize.cy != oldIconSize.cy))
8581 TRACE("icon old size=(%d,%d), new size=(%d,%d)\n",
8582 oldIconSize.cx, oldIconSize.cy, infoPtr->iconSize.cx, infoPtr->iconSize.cy);
8583 LISTVIEW_SetIconSpacing(infoPtr, 0, 0);
8585 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
8587 case LV_VIEW_SMALLICON:
8588 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
8590 case LV_VIEW_DETAILS:
8595 LISTVIEW_CreateHeader( infoPtr );
8597 hl.prc = &infoPtr->rcList;
8599 SendMessageW(infoPtr->hwndHeader, HDM_LAYOUT, 0, (LPARAM)&hl);
8600 SetWindowPos(infoPtr->hwndHeader, infoPtr->hwndSelf, wp.x, wp.y, wp.cx, wp.cy,
8601 wp.flags | ((infoPtr->dwStyle & LVS_NOCOLUMNHEADER) ? SWP_HIDEWINDOW : SWP_SHOWWINDOW));
8608 LISTVIEW_UpdateItemSize(infoPtr);
8609 LISTVIEW_UpdateSize(infoPtr);
8610 LISTVIEW_UpdateScroll(infoPtr);
8611 LISTVIEW_InvalidateList(infoPtr);
8613 TRACE("nView=%d\n", nView);
8618 /* LISTVIEW_SetWorkAreas */
8622 * Callback internally used by LISTVIEW_SortItems() in response of LVM_SORTITEMS
8625 * [I] first : pointer to first ITEM_INFO to compare
8626 * [I] second : pointer to second ITEM_INFO to compare
8627 * [I] lParam : HWND of control
8630 * if first comes before second : negative
8631 * if first comes after second : positive
8632 * if first and second are equivalent : zero
8634 static INT WINAPI LISTVIEW_CallBackCompare(LPVOID first, LPVOID second, LPARAM lParam)
8636 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)lParam;
8637 ITEM_INFO* lv_first = DPA_GetPtr( first, 0 );
8638 ITEM_INFO* lv_second = DPA_GetPtr( second, 0 );
8640 /* Forward the call to the client defined callback */
8641 return (infoPtr->pfnCompare)( lv_first->lParam , lv_second->lParam, infoPtr->lParamSort );
8646 * Callback internally used by LISTVIEW_SortItems() in response of LVM_SORTITEMSEX
8649 * [I] first : pointer to first ITEM_INFO to compare
8650 * [I] second : pointer to second ITEM_INFO to compare
8651 * [I] lParam : HWND of control
8654 * if first comes before second : negative
8655 * if first comes after second : positive
8656 * if first and second are equivalent : zero
8658 static INT WINAPI LISTVIEW_CallBackCompareEx(LPVOID first, LPVOID second, LPARAM lParam)
8660 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)lParam;
8661 INT first_idx = DPA_GetPtrIndex( infoPtr->hdpaItems, first );
8662 INT second_idx = DPA_GetPtrIndex( infoPtr->hdpaItems, second );
8664 /* Forward the call to the client defined callback */
8665 return (infoPtr->pfnCompare)( first_idx, second_idx, infoPtr->lParamSort );
8670 * Sorts the listview items.
8673 * [I] infoPtr : valid pointer to the listview structure
8674 * [I] pfnCompare : application-defined value
8675 * [I] lParamSort : pointer to comparison callback
8676 * [I] IsEx : TRUE when LVM_SORTITEMSEX used
8682 static BOOL LISTVIEW_SortItems(LISTVIEW_INFO *infoPtr, PFNLVCOMPARE pfnCompare,
8683 LPARAM lParamSort, BOOL IsEx)
8687 LPVOID selectionMarkItem = NULL;
8688 LPVOID focusedItem = NULL;
8691 TRACE("(pfnCompare=%p, lParamSort=%lx)\n", pfnCompare, lParamSort);
8693 if (infoPtr->dwStyle & LVS_OWNERDATA) return FALSE;
8695 if (!pfnCompare) return FALSE;
8696 if (!infoPtr->hdpaItems) return FALSE;
8698 /* if there are 0 or 1 items, there is no need to sort */
8699 if (infoPtr->nItemCount < 2) return TRUE;
8701 /* clear selection */
8702 ranges_clear(infoPtr->selectionRanges);
8704 /* save selection mark and focused item */
8705 if (infoPtr->nSelectionMark >= 0)
8706 selectionMarkItem = DPA_GetPtr(infoPtr->hdpaItems, infoPtr->nSelectionMark);
8707 if (infoPtr->nFocusedItem >= 0)
8708 focusedItem = DPA_GetPtr(infoPtr->hdpaItems, infoPtr->nFocusedItem);
8710 infoPtr->pfnCompare = pfnCompare;
8711 infoPtr->lParamSort = lParamSort;
8713 DPA_Sort(infoPtr->hdpaItems, LISTVIEW_CallBackCompareEx, (LPARAM)infoPtr);
8715 DPA_Sort(infoPtr->hdpaItems, LISTVIEW_CallBackCompare, (LPARAM)infoPtr);
8717 /* restore selection ranges */
8718 for (i=0; i < infoPtr->nItemCount; i++)
8720 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, i);
8721 lpItem = DPA_GetPtr(hdpaSubItems, 0);
8723 if (lpItem->state & LVIS_SELECTED)
8724 ranges_additem(infoPtr->selectionRanges, i);
8726 /* restore selection mark and focused item */
8727 infoPtr->nSelectionMark = DPA_GetPtrIndex(infoPtr->hdpaItems, selectionMarkItem);
8728 infoPtr->nFocusedItem = DPA_GetPtrIndex(infoPtr->hdpaItems, focusedItem);
8730 /* I believe nHotItem should be left alone, see LISTVIEW_ShiftIndices */
8732 /* refresh the display */
8733 if (infoPtr->uView != LV_VIEW_ICON && infoPtr->uView != LV_VIEW_SMALLICON)
8734 LISTVIEW_InvalidateList(infoPtr);
8741 * Update theme handle after a theme change.
8744 * [I] infoPtr : valid pointer to the listview structure
8748 * FAILURE : something else
8750 static LRESULT LISTVIEW_ThemeChanged(const LISTVIEW_INFO *infoPtr)
8752 HTHEME theme = GetWindowTheme(infoPtr->hwndSelf);
8753 CloseThemeData(theme);
8754 OpenThemeData(infoPtr->hwndSelf, themeClass);
8760 * Updates an items or rearranges the listview control.
8763 * [I] infoPtr : valid pointer to the listview structure
8764 * [I] nItem : item index
8770 static BOOL LISTVIEW_Update(LISTVIEW_INFO *infoPtr, INT nItem)
8772 TRACE("(nItem=%d)\n", nItem);
8774 if (nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
8776 /* rearrange with default alignment style */
8777 if (is_autoarrange(infoPtr))
8778 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
8780 LISTVIEW_InvalidateItem(infoPtr, nItem);
8787 * Draw the track line at the place defined in the infoPtr structure.
8788 * The line is drawn with a XOR pen so drawing the line for the second time
8789 * in the same place erases the line.
8792 * [I] infoPtr : valid pointer to the listview structure
8798 static BOOL LISTVIEW_DrawTrackLine(const LISTVIEW_INFO *infoPtr)
8804 if (infoPtr->xTrackLine == -1)
8807 if (!(hdc = GetDC(infoPtr->hwndSelf)))
8809 hOldPen = SelectObject(hdc, GetStockObject(BLACK_PEN));
8810 oldROP = SetROP2(hdc, R2_XORPEN);
8811 MoveToEx(hdc, infoPtr->xTrackLine, infoPtr->rcList.top, NULL);
8812 LineTo(hdc, infoPtr->xTrackLine, infoPtr->rcList.bottom);
8813 SetROP2(hdc, oldROP);
8814 SelectObject(hdc, hOldPen);
8815 ReleaseDC(infoPtr->hwndSelf, hdc);
8821 * Called when an edit control should be displayed. This function is called after
8822 * we are sure that there was a single click - not a double click (this is a TIMERPROC).
8825 * [I] hwnd : Handle to the listview
8826 * [I] uMsg : WM_TIMER (ignored)
8827 * [I] idEvent : The timer ID interpreted as a pointer to a DELAYED_EDIT_ITEM struct
8828 * [I] dwTimer : The elapsed time (ignored)
8833 static VOID CALLBACK LISTVIEW_DelayedEditItem(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
8835 DELAYED_ITEM_EDIT *editItem = (DELAYED_ITEM_EDIT *)idEvent;
8836 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongPtrW(hwnd, 0);
8838 KillTimer(hwnd, idEvent);
8839 editItem->fEnabled = FALSE;
8840 /* check if the item is still selected */
8841 if (infoPtr->bFocus && LISTVIEW_GetItemState(infoPtr, editItem->iItem, LVIS_SELECTED))
8842 LISTVIEW_EditLabelT(infoPtr, editItem->iItem, TRUE);
8847 * Creates the listview control - the WM_NCCREATE phase.
8850 * [I] hwnd : window handle
8851 * [I] lpcs : the create parameters
8857 static LRESULT LISTVIEW_NCCreate(HWND hwnd, const CREATESTRUCTW *lpcs)
8859 LISTVIEW_INFO *infoPtr;
8862 TRACE("(lpcs=%p)\n", lpcs);
8864 /* initialize info pointer */
8865 infoPtr = Alloc(sizeof(LISTVIEW_INFO));
8866 if (!infoPtr) return FALSE;
8868 SetWindowLongPtrW(hwnd, 0, (DWORD_PTR)infoPtr);
8870 infoPtr->hwndSelf = hwnd;
8871 infoPtr->dwStyle = lpcs->style; /* Note: may be changed in WM_CREATE */
8872 map_style_view(infoPtr);
8873 /* determine the type of structures to use */
8874 infoPtr->hwndNotify = lpcs->hwndParent;
8875 /* infoPtr->notifyFormat will be filled in WM_CREATE */
8877 /* initialize color information */
8878 infoPtr->clrBk = CLR_NONE;
8879 infoPtr->clrText = CLR_DEFAULT;
8880 infoPtr->clrTextBk = CLR_DEFAULT;
8881 LISTVIEW_SetBkColor(infoPtr, comctl32_color.clrWindow);
8883 /* set default values */
8884 infoPtr->nFocusedItem = -1;
8885 infoPtr->nSelectionMark = -1;
8886 infoPtr->nHotItem = -1;
8887 infoPtr->bRedraw = TRUE;
8888 infoPtr->bNoItemMetrics = TRUE;
8889 infoPtr->bDoChangeNotify = TRUE;
8890 infoPtr->iconSpacing.cx = GetSystemMetrics(SM_CXICONSPACING);
8891 infoPtr->iconSpacing.cy = GetSystemMetrics(SM_CYICONSPACING);
8892 infoPtr->nEditLabelItem = -1;
8893 infoPtr->nLButtonDownItem = -1;
8894 infoPtr->dwHoverTime = -1; /* default system hover time */
8895 infoPtr->nMeasureItemHeight = 0;
8896 infoPtr->xTrackLine = -1; /* no track line */
8897 infoPtr->itemEdit.fEnabled = FALSE;
8898 infoPtr->iVersion = COMCTL32_VERSION;
8899 infoPtr->colRectsDirty = FALSE;
8901 /* get default font (icon title) */
8902 SystemParametersInfoW(SPI_GETICONTITLELOGFONT, 0, &logFont, 0);
8903 infoPtr->hDefaultFont = CreateFontIndirectW(&logFont);
8904 infoPtr->hFont = infoPtr->hDefaultFont;
8905 LISTVIEW_SaveTextMetrics(infoPtr);
8907 /* allocate memory for the data structure */
8908 if (!(infoPtr->selectionRanges = ranges_create(10))) goto fail;
8909 if (!(infoPtr->hdpaItems = DPA_Create(10))) goto fail;
8910 if (!(infoPtr->hdpaItemIds = DPA_Create(10))) goto fail;
8911 if (!(infoPtr->hdpaPosX = DPA_Create(10))) goto fail;
8912 if (!(infoPtr->hdpaPosY = DPA_Create(10))) goto fail;
8913 if (!(infoPtr->hdpaColumns = DPA_Create(10))) goto fail;
8917 DestroyWindow(infoPtr->hwndHeader);
8918 ranges_destroy(infoPtr->selectionRanges);
8919 DPA_Destroy(infoPtr->hdpaItems);
8920 DPA_Destroy(infoPtr->hdpaItemIds);
8921 DPA_Destroy(infoPtr->hdpaPosX);
8922 DPA_Destroy(infoPtr->hdpaPosY);
8923 DPA_Destroy(infoPtr->hdpaColumns);
8930 * Creates the listview control - the WM_CREATE phase. Most of the data is
8931 * already set up in LISTVIEW_NCCreate
8934 * [I] hwnd : window handle
8935 * [I] lpcs : the create parameters
8941 static LRESULT LISTVIEW_Create(HWND hwnd, const CREATESTRUCTW *lpcs)
8943 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongPtrW(hwnd, 0);
8945 TRACE("(lpcs=%p)\n", lpcs);
8947 infoPtr->dwStyle = lpcs->style;
8948 map_style_view(infoPtr);
8950 infoPtr->notifyFormat = SendMessageW(infoPtr->hwndNotify, WM_NOTIFYFORMAT,
8951 (WPARAM)infoPtr->hwndSelf, (LPARAM)NF_QUERY);
8952 /* on error defaulting to ANSI notifications */
8953 if (infoPtr->notifyFormat == 0) infoPtr->notifyFormat = NFR_ANSI;
8955 if ((infoPtr->uView == LV_VIEW_DETAILS) && (lpcs->style & WS_VISIBLE))
8957 if (LISTVIEW_CreateHeader(infoPtr) < 0) return -1;
8960 infoPtr->hwndHeader = 0;
8962 /* init item size to avoid division by 0 */
8963 LISTVIEW_UpdateItemSize (infoPtr);
8965 if (infoPtr->uView == LV_VIEW_DETAILS)
8967 if (!(LVS_NOCOLUMNHEADER & lpcs->style) && (WS_VISIBLE & lpcs->style))
8969 ShowWindow(infoPtr->hwndHeader, SW_SHOWNORMAL);
8971 LISTVIEW_UpdateScroll(infoPtr);
8972 /* send WM_MEASUREITEM notification */
8973 if (infoPtr->dwStyle & LVS_OWNERDRAWFIXED) notify_measureitem(infoPtr);
8976 OpenThemeData(hwnd, themeClass);
8978 /* initialize the icon sizes */
8979 set_icon_size(&infoPtr->iconSize, infoPtr->himlNormal, infoPtr->uView != LV_VIEW_ICON);
8980 set_icon_size(&infoPtr->iconStateSize, infoPtr->himlState, TRUE);
8986 * Destroys the listview control.
8989 * [I] infoPtr : valid pointer to the listview structure
8995 static LRESULT LISTVIEW_Destroy(const LISTVIEW_INFO *infoPtr)
8997 HTHEME theme = GetWindowTheme(infoPtr->hwndSelf);
8998 CloseThemeData(theme);
9004 * Enables the listview control.
9007 * [I] infoPtr : valid pointer to the listview structure
9008 * [I] bEnable : specifies whether to enable or disable the window
9014 static BOOL LISTVIEW_Enable(const LISTVIEW_INFO *infoPtr, BOOL bEnable)
9016 if (infoPtr->dwStyle & LVS_OWNERDRAWFIXED)
9017 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
9023 * Erases the background of the listview control.
9026 * [I] infoPtr : valid pointer to the listview structure
9027 * [I] hdc : device context handle
9033 static inline BOOL LISTVIEW_EraseBkgnd(const LISTVIEW_INFO *infoPtr, HDC hdc)
9037 TRACE("(hdc=%p)\n", hdc);
9039 if (!GetClipBox(hdc, &rc)) return FALSE;
9041 if (infoPtr->clrBk == CLR_NONE)
9043 if (infoPtr->dwLvExStyle & LVS_EX_TRANSPARENTBKGND)
9044 return SendMessageW(infoPtr->hwndNotify, WM_PRINTCLIENT,
9045 (WPARAM)hdc, PRF_ERASEBKGND);
9047 return SendMessageW(infoPtr->hwndNotify, WM_ERASEBKGND, (WPARAM)hdc, 0);
9050 /* for double buffered controls we need to do this during refresh */
9051 if (infoPtr->dwLvExStyle & LVS_EX_DOUBLEBUFFER) return FALSE;
9053 return LISTVIEW_FillBkgnd(infoPtr, hdc, &rc);
9059 * Helper function for LISTVIEW_[HV]Scroll *only*.
9060 * Performs vertical/horizontal scrolling by a give amount.
9063 * [I] infoPtr : valid pointer to the listview structure
9064 * [I] dx : amount of horizontal scroll
9065 * [I] dy : amount of vertical scroll
9067 static void scroll_list(LISTVIEW_INFO *infoPtr, INT dx, INT dy)
9069 /* now we can scroll the list */
9070 ScrollWindowEx(infoPtr->hwndSelf, dx, dy, &infoPtr->rcList,
9071 &infoPtr->rcList, 0, 0, SW_ERASE | SW_INVALIDATE);
9072 /* if we have focus, adjust rect */
9073 OffsetRect(&infoPtr->rcFocus, dx, dy);
9074 UpdateWindow(infoPtr->hwndSelf);
9079 * Performs vertical scrolling.
9082 * [I] infoPtr : valid pointer to the listview structure
9083 * [I] nScrollCode : scroll code
9084 * [I] nScrollDiff : units to scroll in SB_INTERNAL mode, 0 otherwise
9085 * [I] hScrollWnd : scrollbar control window handle
9091 * SB_LINEUP/SB_LINEDOWN:
9092 * for LVS_ICON, LVS_SMALLICON is 37 by experiment
9093 * for LVS_REPORT is 1 line
9094 * for LVS_LIST cannot occur
9097 static LRESULT LISTVIEW_VScroll(LISTVIEW_INFO *infoPtr, INT nScrollCode,
9098 INT nScrollDiff, HWND hScrollWnd)
9100 INT nOldScrollPos, nNewScrollPos;
9101 SCROLLINFO scrollInfo;
9104 TRACE("(nScrollCode=%d(%s), nScrollDiff=%d)\n", nScrollCode,
9105 debugscrollcode(nScrollCode), nScrollDiff);
9107 if (infoPtr->hwndEdit) SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
9109 scrollInfo.cbSize = sizeof(SCROLLINFO);
9110 scrollInfo.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_TRACKPOS;
9112 is_an_icon = ((infoPtr->uView == LV_VIEW_ICON) || (infoPtr->uView == LV_VIEW_SMALLICON));
9114 if (!GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo)) return 1;
9116 nOldScrollPos = scrollInfo.nPos;
9117 switch (nScrollCode)
9123 nScrollDiff = (is_an_icon) ? -LISTVIEW_SCROLL_ICON_LINE_SIZE : -1;
9127 nScrollDiff = (is_an_icon) ? LISTVIEW_SCROLL_ICON_LINE_SIZE : 1;
9131 nScrollDiff = -scrollInfo.nPage;
9135 nScrollDiff = scrollInfo.nPage;
9138 case SB_THUMBPOSITION:
9140 nScrollDiff = scrollInfo.nTrackPos - scrollInfo.nPos;
9147 /* quit right away if pos isn't changing */
9148 if (nScrollDiff == 0) return 0;
9150 /* calculate new position, and handle overflows */
9151 nNewScrollPos = scrollInfo.nPos + nScrollDiff;
9152 if (nScrollDiff > 0) {
9153 if (nNewScrollPos < nOldScrollPos ||
9154 nNewScrollPos > scrollInfo.nMax)
9155 nNewScrollPos = scrollInfo.nMax;
9157 if (nNewScrollPos > nOldScrollPos ||
9158 nNewScrollPos < scrollInfo.nMin)
9159 nNewScrollPos = scrollInfo.nMin;
9162 /* set the new position, and reread in case it changed */
9163 scrollInfo.fMask = SIF_POS;
9164 scrollInfo.nPos = nNewScrollPos;
9165 nNewScrollPos = SetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo, TRUE);
9167 /* carry on only if it really changed */
9168 if (nNewScrollPos == nOldScrollPos) return 0;
9170 /* now adjust to client coordinates */
9171 nScrollDiff = nOldScrollPos - nNewScrollPos;
9172 if (infoPtr->uView == LV_VIEW_DETAILS) nScrollDiff *= infoPtr->nItemHeight;
9174 /* and scroll the window */
9175 scroll_list(infoPtr, 0, nScrollDiff);
9182 * Performs horizontal scrolling.
9185 * [I] infoPtr : valid pointer to the listview structure
9186 * [I] nScrollCode : scroll code
9187 * [I] nScrollDiff : units to scroll in SB_INTERNAL mode, 0 otherwise
9188 * [I] hScrollWnd : scrollbar control window handle
9194 * SB_LINELEFT/SB_LINERIGHT:
9195 * for LVS_ICON, LVS_SMALLICON 1 pixel
9196 * for LVS_REPORT is 1 pixel
9197 * for LVS_LIST is 1 column --> which is a 1 because the
9198 * scroll is based on columns not pixels
9201 static LRESULT LISTVIEW_HScroll(LISTVIEW_INFO *infoPtr, INT nScrollCode,
9202 INT nScrollDiff, HWND hScrollWnd)
9204 INT nOldScrollPos, nNewScrollPos;
9205 SCROLLINFO scrollInfo;
9207 TRACE("(nScrollCode=%d(%s), nScrollDiff=%d)\n", nScrollCode,
9208 debugscrollcode(nScrollCode), nScrollDiff);
9210 if (infoPtr->hwndEdit) SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
9212 scrollInfo.cbSize = sizeof(SCROLLINFO);
9213 scrollInfo.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_TRACKPOS;
9215 if (!GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo)) return 1;
9217 nOldScrollPos = scrollInfo.nPos;
9219 switch (nScrollCode)
9233 nScrollDiff = -scrollInfo.nPage;
9237 nScrollDiff = scrollInfo.nPage;
9240 case SB_THUMBPOSITION:
9242 nScrollDiff = scrollInfo.nTrackPos - scrollInfo.nPos;
9249 /* quit right away if pos isn't changing */
9250 if (nScrollDiff == 0) return 0;
9252 /* calculate new position, and handle overflows */
9253 nNewScrollPos = scrollInfo.nPos + nScrollDiff;
9254 if (nScrollDiff > 0) {
9255 if (nNewScrollPos < nOldScrollPos ||
9256 nNewScrollPos > scrollInfo.nMax)
9257 nNewScrollPos = scrollInfo.nMax;
9259 if (nNewScrollPos > nOldScrollPos ||
9260 nNewScrollPos < scrollInfo.nMin)
9261 nNewScrollPos = scrollInfo.nMin;
9264 /* set the new position, and reread in case it changed */
9265 scrollInfo.fMask = SIF_POS;
9266 scrollInfo.nPos = nNewScrollPos;
9267 nNewScrollPos = SetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo, TRUE);
9269 /* carry on only if it really changed */
9270 if (nNewScrollPos == nOldScrollPos) return 0;
9272 if (infoPtr->uView == LV_VIEW_DETAILS)
9273 LISTVIEW_UpdateHeaderSize(infoPtr, nNewScrollPos);
9275 /* now adjust to client coordinates */
9276 nScrollDiff = nOldScrollPos - nNewScrollPos;
9277 if (infoPtr->uView == LV_VIEW_LIST) nScrollDiff *= infoPtr->nItemWidth;
9279 /* and scroll the window */
9280 scroll_list(infoPtr, nScrollDiff, 0);
9285 static LRESULT LISTVIEW_MouseWheel(LISTVIEW_INFO *infoPtr, INT wheelDelta)
9287 INT gcWheelDelta = 0;
9288 INT pulScrollLines = 3;
9289 SCROLLINFO scrollInfo;
9291 TRACE("(wheelDelta=%d)\n", wheelDelta);
9293 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
9294 gcWheelDelta -= wheelDelta;
9296 scrollInfo.cbSize = sizeof(SCROLLINFO);
9297 scrollInfo.fMask = SIF_POS;
9299 switch(infoPtr->uView)
9302 case LV_VIEW_SMALLICON:
9304 * listview should be scrolled by a multiple of 37 dependently on its dimension or its visible item number
9305 * should be fixed in the future.
9307 LISTVIEW_VScroll(infoPtr, SB_INTERNAL, (gcWheelDelta < 0) ?
9308 -LISTVIEW_SCROLL_ICON_LINE_SIZE : LISTVIEW_SCROLL_ICON_LINE_SIZE, 0);
9311 case LV_VIEW_DETAILS:
9312 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
9314 int cLineScroll = min(LISTVIEW_GetCountPerColumn(infoPtr), pulScrollLines);
9315 cLineScroll *= (gcWheelDelta / WHEEL_DELTA);
9316 LISTVIEW_VScroll(infoPtr, SB_INTERNAL, cLineScroll, 0);
9321 LISTVIEW_HScroll(infoPtr, (gcWheelDelta < 0) ? SB_LINELEFT : SB_LINERIGHT, 0, 0);
9332 * [I] infoPtr : valid pointer to the listview structure
9333 * [I] nVirtualKey : virtual key
9334 * [I] lKeyData : key data
9339 static LRESULT LISTVIEW_KeyDown(LISTVIEW_INFO *infoPtr, INT nVirtualKey, LONG lKeyData)
9341 HWND hwndSelf = infoPtr->hwndSelf;
9343 NMLVKEYDOWN nmKeyDown;
9345 TRACE("(nVirtualKey=%d, lKeyData=%d)\n", nVirtualKey, lKeyData);
9347 /* send LVN_KEYDOWN notification */
9348 nmKeyDown.wVKey = nVirtualKey;
9349 nmKeyDown.flags = 0;
9350 notify_hdr(infoPtr, LVN_KEYDOWN, &nmKeyDown.hdr);
9351 if (!IsWindow(hwndSelf))
9354 switch (nVirtualKey)
9357 nItem = infoPtr->nFocusedItem;
9358 if (infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES)
9359 toggle_checkbox_state(infoPtr, infoPtr->nFocusedItem);
9363 if ((infoPtr->nItemCount > 0) && (infoPtr->nFocusedItem != -1))
9365 if (!notify(infoPtr, NM_RETURN)) return 0;
9366 if (!notify(infoPtr, LVN_ITEMACTIVATE)) return 0;
9371 if (infoPtr->nItemCount > 0)
9376 if (infoPtr->nItemCount > 0)
9377 nItem = infoPtr->nItemCount - 1;
9381 nItem = LISTVIEW_GetNextItem(infoPtr, infoPtr->nFocusedItem, LVNI_TOLEFT);
9385 nItem = LISTVIEW_GetNextItem(infoPtr, infoPtr->nFocusedItem, LVNI_ABOVE);
9389 nItem = LISTVIEW_GetNextItem(infoPtr, infoPtr->nFocusedItem, LVNI_TORIGHT);
9393 nItem = LISTVIEW_GetNextItem(infoPtr, infoPtr->nFocusedItem, LVNI_BELOW);
9397 if (infoPtr->uView == LV_VIEW_DETAILS)
9399 INT topidx = LISTVIEW_GetTopIndex(infoPtr);
9400 if (infoPtr->nFocusedItem == topidx)
9401 nItem = topidx - LISTVIEW_GetCountPerColumn(infoPtr) + 1;
9406 nItem = infoPtr->nFocusedItem - LISTVIEW_GetCountPerColumn(infoPtr)
9407 * LISTVIEW_GetCountPerRow(infoPtr);
9408 if(nItem < 0) nItem = 0;
9412 if (infoPtr->uView == LV_VIEW_DETAILS)
9414 INT topidx = LISTVIEW_GetTopIndex(infoPtr);
9415 INT cnt = LISTVIEW_GetCountPerColumn(infoPtr);
9416 if (infoPtr->nFocusedItem == topidx + cnt - 1)
9417 nItem = infoPtr->nFocusedItem + cnt - 1;
9419 nItem = topidx + cnt - 1;
9422 nItem = infoPtr->nFocusedItem + LISTVIEW_GetCountPerColumn(infoPtr)
9423 * LISTVIEW_GetCountPerRow(infoPtr);
9424 if(nItem >= infoPtr->nItemCount) nItem = infoPtr->nItemCount - 1;
9428 if ((nItem != -1) && (nItem != infoPtr->nFocusedItem || nVirtualKey == VK_SPACE))
9429 LISTVIEW_KeySelection(infoPtr, nItem, nVirtualKey == VK_SPACE);
9439 * [I] infoPtr : valid pointer to the listview structure
9444 static LRESULT LISTVIEW_KillFocus(LISTVIEW_INFO *infoPtr)
9448 /* if we did not have the focus, there's nothing to do */
9449 if (!infoPtr->bFocus) return 0;
9451 /* send NM_KILLFOCUS notification */
9452 if (!notify(infoPtr, NM_KILLFOCUS)) return 0;
9454 /* if we have a focus rectangle, get rid of it */
9455 LISTVIEW_ShowFocusRect(infoPtr, FALSE);
9457 /* set window focus flag */
9458 infoPtr->bFocus = FALSE;
9460 /* invalidate the selected items before resetting focus flag */
9461 LISTVIEW_InvalidateSelectedItems(infoPtr);
9468 * Processes double click messages (left mouse button).
9471 * [I] infoPtr : valid pointer to the listview structure
9472 * [I] wKey : key flag
9473 * [I] x,y : mouse coordinate
9478 static LRESULT LISTVIEW_LButtonDblClk(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
9480 LVHITTESTINFO htInfo;
9482 TRACE("(key=%hu, X=%hu, Y=%hu)\n", wKey, x, y);
9484 /* Cancel the item edition if any */
9485 if (infoPtr->itemEdit.fEnabled)
9487 KillTimer(infoPtr->hwndSelf, (UINT_PTR)&infoPtr->itemEdit);
9488 infoPtr->itemEdit.fEnabled = FALSE;
9491 /* send NM_RELEASEDCAPTURE notification */
9492 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
9497 /* send NM_DBLCLK notification */
9498 LISTVIEW_HitTest(infoPtr, &htInfo, TRUE, FALSE);
9499 if (!notify_click(infoPtr, NM_DBLCLK, &htInfo)) return 0;
9501 /* To send the LVN_ITEMACTIVATE, it must be on an Item */
9502 if(htInfo.iItem != -1) notify_itemactivate(infoPtr,&htInfo);
9509 * Processes mouse down messages (left mouse button).
9512 * infoPtr [I ] valid pointer to the listview structure
9513 * wKey [I ] key flag
9514 * x,y [I ] mouse coordinate
9519 static LRESULT LISTVIEW_LButtonDown(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
9521 LVHITTESTINFO lvHitTestInfo;
9522 static BOOL bGroupSelect = TRUE;
9523 POINT pt = { x, y };
9526 TRACE("(key=%hu, X=%hu, Y=%hu)\n", wKey, x, y);
9528 /* send NM_RELEASEDCAPTURE notification */
9529 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
9531 /* set left button down flag and record the click position */
9532 infoPtr->bLButtonDown = TRUE;
9533 infoPtr->ptClickPos = pt;
9534 infoPtr->bDragging = FALSE;
9535 infoPtr->bMarqueeSelect = FALSE;
9537 lvHitTestInfo.pt.x = x;
9538 lvHitTestInfo.pt.y = y;
9540 nItem = LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, TRUE);
9541 TRACE("at %s, nItem=%d\n", wine_dbgstr_point(&pt), nItem);
9542 if ((nItem >= 0) && (nItem < infoPtr->nItemCount))
9544 if ((infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES) && (lvHitTestInfo.flags & LVHT_ONITEMSTATEICON))
9546 toggle_checkbox_state(infoPtr, nItem);
9550 if (infoPtr->dwStyle & LVS_SINGLESEL)
9552 if (LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED))
9553 infoPtr->nEditLabelItem = nItem;
9555 LISTVIEW_SetSelection(infoPtr, nItem);
9559 if ((wKey & MK_CONTROL) && (wKey & MK_SHIFT))
9563 if (!LISTVIEW_AddGroupSelection(infoPtr, nItem)) return 0;
9564 LISTVIEW_SetItemFocus(infoPtr, nItem);
9565 infoPtr->nSelectionMark = nItem;
9571 item.state = LVIS_SELECTED | LVIS_FOCUSED;
9572 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
9574 LISTVIEW_SetItemState(infoPtr,nItem,&item);
9575 infoPtr->nSelectionMark = nItem;
9578 else if (wKey & MK_CONTROL)
9582 bGroupSelect = (LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED) == 0);
9584 item.state = (bGroupSelect ? LVIS_SELECTED : 0) | LVIS_FOCUSED;
9585 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
9586 LISTVIEW_SetItemState(infoPtr, nItem, &item);
9587 infoPtr->nSelectionMark = nItem;
9589 else if (wKey & MK_SHIFT)
9591 LISTVIEW_SetGroupSelection(infoPtr, nItem);
9595 if (LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED))
9597 infoPtr->nEditLabelItem = nItem;
9598 infoPtr->nLButtonDownItem = nItem;
9600 LISTVIEW_SetItemFocus(infoPtr, nItem);
9603 /* set selection (clears other pre-existing selections) */
9604 LISTVIEW_SetSelection(infoPtr, nItem);
9608 if (infoPtr->dwLvExStyle & LVS_EX_ONECLICKACTIVATE)
9609 if(lvHitTestInfo.iItem != -1) notify_itemactivate(infoPtr,&lvHitTestInfo);
9613 if (!infoPtr->bFocus)
9614 SetFocus(infoPtr->hwndSelf);
9616 /* remove all selections */
9617 if (!(wKey & MK_CONTROL) && !(wKey & MK_SHIFT))
9618 LISTVIEW_DeselectAll(infoPtr);
9627 * Processes mouse up messages (left mouse button).
9630 * infoPtr [I ] valid pointer to the listview structure
9631 * wKey [I ] key flag
9632 * x,y [I ] mouse coordinate
9637 static LRESULT LISTVIEW_LButtonUp(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
9639 LVHITTESTINFO lvHitTestInfo;
9641 TRACE("(key=%hu, X=%hu, Y=%hu)\n", wKey, x, y);
9643 if (!infoPtr->bLButtonDown) return 0;
9645 lvHitTestInfo.pt.x = x;
9646 lvHitTestInfo.pt.y = y;
9648 /* send NM_CLICK notification */
9649 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
9650 if (!notify_click(infoPtr, NM_CLICK, &lvHitTestInfo)) return 0;
9652 /* set left button flag */
9653 infoPtr->bLButtonDown = FALSE;
9655 /* set a single selection, reset others */
9656 if(lvHitTestInfo.iItem == infoPtr->nLButtonDownItem && lvHitTestInfo.iItem != -1)
9657 LISTVIEW_SetSelection(infoPtr, infoPtr->nLButtonDownItem);
9658 infoPtr->nLButtonDownItem = -1;
9660 if (infoPtr->bDragging || infoPtr->bMarqueeSelect)
9662 /* Remove the marquee rectangle and release our mouse capture */
9663 if (infoPtr->bMarqueeSelect)
9665 LISTVIEW_InvalidateRect(infoPtr, &infoPtr->marqueeDrawRect);
9669 SetRect(&infoPtr->marqueeRect, 0, 0, 0, 0);
9670 SetRect(&infoPtr->marqueeDrawRect, 0, 0, 0, 0);
9672 infoPtr->bDragging = FALSE;
9673 infoPtr->bMarqueeSelect = FALSE;
9675 KillTimer(infoPtr->hwndSelf, (UINT_PTR) infoPtr);
9679 /* if we clicked on a selected item, edit the label */
9680 if(lvHitTestInfo.iItem == infoPtr->nEditLabelItem && (lvHitTestInfo.flags & LVHT_ONITEMLABEL))
9682 /* we want to make sure the user doesn't want to do a double click. So we will
9683 * delay the edit. WM_LBUTTONDBLCLICK will cancel the timer
9685 infoPtr->itemEdit.fEnabled = TRUE;
9686 infoPtr->itemEdit.iItem = lvHitTestInfo.iItem;
9687 SetTimer(infoPtr->hwndSelf,
9688 (UINT_PTR)&infoPtr->itemEdit,
9689 GetDoubleClickTime(),
9690 LISTVIEW_DelayedEditItem);
9693 if (!infoPtr->bFocus)
9694 SetFocus(infoPtr->hwndSelf);
9701 * Destroys the listview control (called after WM_DESTROY).
9704 * [I] infoPtr : valid pointer to the listview structure
9709 static LRESULT LISTVIEW_NCDestroy(LISTVIEW_INFO *infoPtr)
9715 /* delete all items */
9716 LISTVIEW_DeleteAllItems(infoPtr, TRUE);
9718 /* destroy data structure */
9719 DPA_Destroy(infoPtr->hdpaItems);
9720 DPA_Destroy(infoPtr->hdpaItemIds);
9721 DPA_Destroy(infoPtr->hdpaPosX);
9722 DPA_Destroy(infoPtr->hdpaPosY);
9724 for (i = 0; i < DPA_GetPtrCount(infoPtr->hdpaColumns); i++)
9725 Free(DPA_GetPtr(infoPtr->hdpaColumns, i));
9726 DPA_Destroy(infoPtr->hdpaColumns);
9727 ranges_destroy(infoPtr->selectionRanges);
9729 /* destroy image lists */
9730 if (!(infoPtr->dwStyle & LVS_SHAREIMAGELISTS))
9732 ImageList_Destroy(infoPtr->himlNormal);
9733 ImageList_Destroy(infoPtr->himlSmall);
9734 ImageList_Destroy(infoPtr->himlState);
9737 /* destroy font, bkgnd brush */
9739 if (infoPtr->hDefaultFont) DeleteObject(infoPtr->hDefaultFont);
9740 if (infoPtr->clrBk != CLR_NONE) DeleteObject(infoPtr->hBkBrush);
9742 SetWindowLongPtrW(infoPtr->hwndSelf, 0, 0);
9744 /* free listview info pointer*/
9752 * Handles notifications from header.
9755 * [I] infoPtr : valid pointer to the listview structure
9756 * [I] nCtrlId : control identifier
9757 * [I] lpnmh : notification information
9762 static LRESULT LISTVIEW_HeaderNotification(LISTVIEW_INFO *infoPtr, const NMHEADERW *lpnmh)
9764 HWND hwndSelf = infoPtr->hwndSelf;
9766 TRACE("(lpnmh=%p)\n", lpnmh);
9768 if (!lpnmh || lpnmh->iItem < 0 || lpnmh->iItem >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return 0;
9770 switch (lpnmh->hdr.code)
9775 COLUMN_INFO *lpColumnInfo;
9779 if (!lpnmh->pitem || !(lpnmh->pitem->mask & HDI_WIDTH))
9782 /* remove the old line (if any) */
9783 LISTVIEW_DrawTrackLine(infoPtr);
9785 /* compute & draw the new line */
9786 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, lpnmh->iItem);
9787 x = lpColumnInfo->rcHeader.left + lpnmh->pitem->cxy;
9788 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
9789 infoPtr->xTrackLine = x + ptOrigin.x;
9790 LISTVIEW_DrawTrackLine(infoPtr);
9796 /* remove the track line (if any) */
9797 LISTVIEW_DrawTrackLine(infoPtr);
9798 infoPtr->xTrackLine = -1;
9802 notify_forward_header(infoPtr, lpnmh);
9803 return (infoPtr->dwLvExStyle & LVS_EX_HEADERDRAGDROP) == 0;
9806 infoPtr->colRectsDirty = TRUE;
9807 LISTVIEW_InvalidateList(infoPtr);
9808 notify_forward_header(infoPtr, lpnmh);
9811 case HDN_ITEMCHANGINGW:
9812 case HDN_ITEMCHANGINGA:
9813 return notify_forward_header(infoPtr, lpnmh);
9815 case HDN_ITEMCHANGEDW:
9816 case HDN_ITEMCHANGEDA:
9818 COLUMN_INFO *lpColumnInfo;
9822 notify_forward_header(infoPtr, lpnmh);
9823 if (!IsWindow(hwndSelf))
9826 if (!lpnmh->pitem || !(lpnmh->pitem->mask & HDI_WIDTH))
9828 hdi.mask = HDI_WIDTH;
9829 if (!SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, lpnmh->iItem, (LPARAM)&hdi)) return 0;
9833 cxy = lpnmh->pitem->cxy;
9835 /* determine how much we change since the last know position */
9836 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, lpnmh->iItem);
9837 dx = cxy - (lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left);
9840 lpColumnInfo->rcHeader.right += dx;
9842 hdi.mask = HDI_ORDER;
9843 SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, lpnmh->iItem, (LPARAM)&hdi);
9845 /* not the rightmost one */
9846 if (hdi.iOrder + 1 < DPA_GetPtrCount(infoPtr->hdpaColumns))
9848 INT nIndex = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX,
9850 LISTVIEW_ScrollColumns(infoPtr, nIndex, dx);
9854 /* only needs to update the scrolls */
9855 infoPtr->nItemWidth += dx;
9856 LISTVIEW_UpdateScroll(infoPtr);
9858 LISTVIEW_UpdateItemSize(infoPtr);
9859 if (infoPtr->uView == LV_VIEW_DETAILS && is_redrawing(infoPtr))
9862 RECT rcCol = lpColumnInfo->rcHeader;
9864 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
9865 OffsetRect(&rcCol, ptOrigin.x, 0);
9867 rcCol.top = infoPtr->rcList.top;
9868 rcCol.bottom = infoPtr->rcList.bottom;
9870 /* resizing left-aligned columns leaves most of the left side untouched */
9871 if ((lpColumnInfo->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_LEFT)
9873 INT nMaxDirty = infoPtr->nEllipsisWidth + infoPtr->ntmMaxCharWidth;
9876 rcCol.left = max (rcCol.left, rcCol.right - nMaxDirty);
9879 /* when shrinking the last column clear the now unused field */
9880 if (hdi.iOrder == DPA_GetPtrCount(infoPtr->hdpaColumns) - 1)
9886 /* deal with right from rightmost column area */
9887 right.left = rcCol.right;
9888 right.top = rcCol.top;
9889 right.bottom = rcCol.bottom;
9890 right.right = infoPtr->rcList.right;
9892 LISTVIEW_InvalidateRect(infoPtr, &right);
9895 LISTVIEW_InvalidateRect(infoPtr, &rcCol);
9901 case HDN_ITEMCLICKW:
9902 case HDN_ITEMCLICKA:
9904 /* Handle sorting by Header Column */
9907 ZeroMemory(&nmlv, sizeof(NMLISTVIEW));
9909 nmlv.iSubItem = lpnmh->iItem;
9910 notify_listview(infoPtr, LVN_COLUMNCLICK, &nmlv);
9911 notify_forward_header(infoPtr, lpnmh);
9915 case HDN_DIVIDERDBLCLICKW:
9916 case HDN_DIVIDERDBLCLICKA:
9917 LISTVIEW_SetColumnWidth(infoPtr, lpnmh->iItem, LVSCW_AUTOSIZE);
9918 notify_forward_header(infoPtr, lpnmh);
9927 * Paint non-client area of control.
9930 * [I] infoPtr : valid pointer to the listview structureof the sender
9931 * [I] region : update region
9934 * TRUE - frame was painted
9935 * FALSE - call default window proc
9937 static BOOL LISTVIEW_NCPaint(const LISTVIEW_INFO *infoPtr, HRGN region)
9939 HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
9943 int cxEdge = GetSystemMetrics (SM_CXEDGE),
9944 cyEdge = GetSystemMetrics (SM_CYEDGE);
9946 if (!theme) return FALSE;
9948 GetWindowRect(infoPtr->hwndSelf, &r);
9950 cliprgn = CreateRectRgn (r.left + cxEdge, r.top + cyEdge,
9951 r.right - cxEdge, r.bottom - cyEdge);
9952 if (region != (HRGN)1)
9953 CombineRgn (cliprgn, cliprgn, region, RGN_AND);
9954 OffsetRect(&r, -r.left, -r.top);
9956 dc = GetDCEx(infoPtr->hwndSelf, region, DCX_WINDOW|DCX_INTERSECTRGN);
9957 OffsetRect(&r, -r.left, -r.top);
9959 if (IsThemeBackgroundPartiallyTransparent (theme, 0, 0))
9960 DrawThemeParentBackground(infoPtr->hwndSelf, dc, &r);
9961 DrawThemeBackground (theme, dc, 0, 0, &r, 0);
9962 ReleaseDC(infoPtr->hwndSelf, dc);
9964 /* Call default proc to get the scrollbars etc. painted */
9965 DefWindowProcW (infoPtr->hwndSelf, WM_NCPAINT, (WPARAM)cliprgn, 0);
9972 * Determines the type of structure to use.
9975 * [I] infoPtr : valid pointer to the listview structureof the sender
9976 * [I] hwndFrom : listview window handle
9977 * [I] nCommand : command specifying the nature of the WM_NOTIFYFORMAT
9982 static LRESULT LISTVIEW_NotifyFormat(LISTVIEW_INFO *infoPtr, HWND hwndFrom, INT nCommand)
9984 TRACE("(hwndFrom=%p, nCommand=%d)\n", hwndFrom, nCommand);
9986 if (nCommand == NF_REQUERY)
9987 infoPtr->notifyFormat = SendMessageW(infoPtr->hwndNotify, WM_NOTIFYFORMAT, (WPARAM)infoPtr->hwndSelf, NF_QUERY);
9989 return infoPtr->notifyFormat;
9994 * Paints/Repaints the listview control. Internal use.
9997 * [I] infoPtr : valid pointer to the listview structure
9998 * [I] hdc : device context handle
10003 static LRESULT LISTVIEW_Paint(LISTVIEW_INFO *infoPtr, HDC hdc)
10005 TRACE("(hdc=%p)\n", hdc);
10007 if (infoPtr->bNoItemMetrics && infoPtr->nItemCount)
10009 infoPtr->bNoItemMetrics = FALSE;
10010 LISTVIEW_UpdateItemSize(infoPtr);
10011 if (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON)
10012 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
10013 LISTVIEW_UpdateScroll(infoPtr);
10016 if (infoPtr->hwndHeader) UpdateWindow(infoPtr->hwndHeader);
10019 LISTVIEW_Refresh(infoPtr, hdc, NULL);
10024 hdc = BeginPaint(infoPtr->hwndSelf, &ps);
10025 if (!hdc) return 1;
10026 LISTVIEW_Refresh(infoPtr, hdc, ps.fErase ? &ps.rcPaint : NULL);
10027 EndPaint(infoPtr->hwndSelf, &ps);
10035 * Paints/Repaints the listview control, WM_PAINT handler.
10038 * [I] infoPtr : valid pointer to the listview structure
10039 * [I] hdc : device context handle
10044 static inline LRESULT LISTVIEW_WMPaint(LISTVIEW_INFO *infoPtr, HDC hdc)
10046 TRACE("(hdc=%p)\n", hdc);
10048 if (!is_redrawing(infoPtr))
10049 return DefWindowProcW (infoPtr->hwndSelf, WM_PAINT, (WPARAM)hdc, 0);
10051 return LISTVIEW_Paint(infoPtr, hdc);
10056 * Paints/Repaints the listview control.
10059 * [I] infoPtr : valid pointer to the listview structure
10060 * [I] hdc : device context handle
10061 * [I] options : drawing options
10066 static LRESULT LISTVIEW_PrintClient(LISTVIEW_INFO *infoPtr, HDC hdc, DWORD options)
10068 FIXME("Partial Stub: (hdc=%p options=0x%08x)\n", hdc, options);
10070 if ((options & PRF_CHECKVISIBLE) && !IsWindowVisible(infoPtr->hwndSelf))
10073 if (options & PRF_ERASEBKGND)
10074 LISTVIEW_EraseBkgnd(infoPtr, hdc);
10076 if (options & PRF_CLIENT)
10077 LISTVIEW_Paint(infoPtr, hdc);
10085 * Processes double click messages (right mouse button).
10088 * [I] infoPtr : valid pointer to the listview structure
10089 * [I] wKey : key flag
10090 * [I] x,y : mouse coordinate
10095 static LRESULT LISTVIEW_RButtonDblClk(const LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
10097 LVHITTESTINFO lvHitTestInfo;
10099 TRACE("(key=%hu,X=%hu,Y=%hu)\n", wKey, x, y);
10101 /* send NM_RELEASEDCAPTURE notification */
10102 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
10104 /* send NM_RDBLCLK notification */
10105 lvHitTestInfo.pt.x = x;
10106 lvHitTestInfo.pt.y = y;
10107 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
10108 notify_click(infoPtr, NM_RDBLCLK, &lvHitTestInfo);
10115 * Processes mouse down messages (right mouse button).
10118 * [I] infoPtr : valid pointer to the listview structure
10119 * [I] wKey : key flag
10120 * [I] x,y : mouse coordinate
10125 static LRESULT LISTVIEW_RButtonDown(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
10127 LVHITTESTINFO lvHitTestInfo;
10130 TRACE("(key=%hu,X=%hu,Y=%hu)\n", wKey, x, y);
10132 /* send NM_RELEASEDCAPTURE notification */
10133 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
10135 /* make sure the listview control window has the focus */
10136 if (!infoPtr->bFocus) SetFocus(infoPtr->hwndSelf);
10138 /* set right button down flag */
10139 infoPtr->bRButtonDown = TRUE;
10141 /* determine the index of the selected item */
10142 lvHitTestInfo.pt.x = x;
10143 lvHitTestInfo.pt.y = y;
10144 nItem = LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, TRUE);
10146 if ((nItem >= 0) && (nItem < infoPtr->nItemCount))
10148 LISTVIEW_SetItemFocus(infoPtr, nItem);
10149 if (!((wKey & MK_SHIFT) || (wKey & MK_CONTROL)) &&
10150 !LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED))
10151 LISTVIEW_SetSelection(infoPtr, nItem);
10155 LISTVIEW_DeselectAll(infoPtr);
10163 * Processes mouse up messages (right mouse button).
10166 * [I] infoPtr : valid pointer to the listview structure
10167 * [I] wKey : key flag
10168 * [I] x,y : mouse coordinate
10173 static LRESULT LISTVIEW_RButtonUp(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
10175 LVHITTESTINFO lvHitTestInfo;
10178 TRACE("(key=%hu,X=%hu,Y=%hu)\n", wKey, x, y);
10180 if (!infoPtr->bRButtonDown) return 0;
10182 /* set button flag */
10183 infoPtr->bRButtonDown = FALSE;
10185 /* Send NM_RCLICK notification */
10186 lvHitTestInfo.pt.x = x;
10187 lvHitTestInfo.pt.y = y;
10188 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
10189 if (!notify_click(infoPtr, NM_RCLICK, &lvHitTestInfo)) return 0;
10191 /* Change to screen coordinate for WM_CONTEXTMENU */
10192 pt = lvHitTestInfo.pt;
10193 ClientToScreen(infoPtr->hwndSelf, &pt);
10195 /* Send a WM_CONTEXTMENU message in response to the RBUTTONUP */
10196 SendMessageW(infoPtr->hwndSelf, WM_CONTEXTMENU,
10197 (WPARAM)infoPtr->hwndSelf, MAKELPARAM(pt.x, pt.y));
10208 * [I] infoPtr : valid pointer to the listview structure
10209 * [I] hwnd : window handle of window containing the cursor
10210 * [I] nHittest : hit-test code
10211 * [I] wMouseMsg : ideintifier of the mouse message
10214 * TRUE if cursor is set
10217 static BOOL LISTVIEW_SetCursor(const LISTVIEW_INFO *infoPtr, HWND hwnd, UINT nHittest, UINT wMouseMsg)
10219 LVHITTESTINFO lvHitTestInfo;
10221 if(!(LISTVIEW_isHotTracking(infoPtr))) return FALSE;
10223 if(!infoPtr->hHotCursor) return FALSE;
10225 GetCursorPos(&lvHitTestInfo.pt);
10226 if (LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, FALSE, FALSE) < 0) return FALSE;
10228 SetCursor(infoPtr->hHotCursor);
10238 * [I] infoPtr : valid pointer to the listview structure
10239 * [I] hwndLoseFocus : handle of previously focused window
10244 static LRESULT LISTVIEW_SetFocus(LISTVIEW_INFO *infoPtr, HWND hwndLoseFocus)
10246 TRACE("(hwndLoseFocus=%p)\n", hwndLoseFocus);
10248 /* if we have the focus already, there's nothing to do */
10249 if (infoPtr->bFocus) return 0;
10251 /* send NM_SETFOCUS notification */
10252 if (!notify(infoPtr, NM_SETFOCUS)) return 0;
10254 /* set window focus flag */
10255 infoPtr->bFocus = TRUE;
10257 /* put the focus rect back on */
10258 LISTVIEW_ShowFocusRect(infoPtr, TRUE);
10260 /* redraw all visible selected items */
10261 LISTVIEW_InvalidateSelectedItems(infoPtr);
10271 * [I] infoPtr : valid pointer to the listview structure
10272 * [I] fRedraw : font handle
10273 * [I] fRedraw : redraw flag
10278 static LRESULT LISTVIEW_SetFont(LISTVIEW_INFO *infoPtr, HFONT hFont, WORD fRedraw)
10280 HFONT oldFont = infoPtr->hFont;
10282 TRACE("(hfont=%p,redraw=%hu)\n", hFont, fRedraw);
10284 infoPtr->hFont = hFont ? hFont : infoPtr->hDefaultFont;
10285 if (infoPtr->hFont == oldFont) return 0;
10287 LISTVIEW_SaveTextMetrics(infoPtr);
10289 if (infoPtr->uView == LV_VIEW_DETAILS)
10291 SendMessageW(infoPtr->hwndHeader, WM_SETFONT, (WPARAM)hFont, MAKELPARAM(fRedraw, 0));
10292 LISTVIEW_UpdateSize(infoPtr);
10293 LISTVIEW_UpdateScroll(infoPtr);
10296 if (fRedraw) LISTVIEW_InvalidateList(infoPtr);
10303 * Message handling for WM_SETREDRAW.
10304 * For the Listview, it invalidates the entire window (the doc specifies otherwise)
10307 * [I] infoPtr : valid pointer to the listview structure
10308 * [I] bRedraw: state of redraw flag
10311 * DefWinProc return value
10313 static LRESULT LISTVIEW_SetRedraw(LISTVIEW_INFO *infoPtr, BOOL bRedraw)
10315 TRACE("infoPtr->bRedraw=%d, bRedraw=%d\n", infoPtr->bRedraw, bRedraw);
10317 /* we cannot use straight equality here because _any_ non-zero value is TRUE */
10318 if ((infoPtr->bRedraw && bRedraw) || (!infoPtr->bRedraw && !bRedraw)) return 0;
10320 infoPtr->bRedraw = bRedraw;
10322 if(!bRedraw) return 0;
10324 if (is_autoarrange(infoPtr))
10325 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
10326 LISTVIEW_UpdateScroll(infoPtr);
10328 /* despite what the WM_SETREDRAW docs says, apps expect us
10329 * to invalidate the listview here... stupid! */
10330 LISTVIEW_InvalidateList(infoPtr);
10337 * Resizes the listview control. This function processes WM_SIZE
10338 * messages. At this time, the width and height are not used.
10341 * [I] infoPtr : valid pointer to the listview structure
10342 * [I] Width : new width
10343 * [I] Height : new height
10348 static LRESULT LISTVIEW_Size(LISTVIEW_INFO *infoPtr, int Width, int Height)
10350 RECT rcOld = infoPtr->rcList;
10352 TRACE("(width=%d, height=%d)\n", Width, Height);
10354 LISTVIEW_UpdateSize(infoPtr);
10355 if (EqualRect(&rcOld, &infoPtr->rcList)) return 0;
10357 /* do not bother with display related stuff if we're not redrawing */
10358 if (!is_redrawing(infoPtr)) return 0;
10360 if (is_autoarrange(infoPtr))
10361 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
10363 LISTVIEW_UpdateScroll(infoPtr);
10365 /* refresh all only for lists whose height changed significantly */
10366 if ((infoPtr->uView == LV_VIEW_LIST) &&
10367 (rcOld.bottom - rcOld.top) / infoPtr->nItemHeight !=
10368 (infoPtr->rcList.bottom - infoPtr->rcList.top) / infoPtr->nItemHeight)
10369 LISTVIEW_InvalidateList(infoPtr);
10376 * Sets the size information.
10379 * [I] infoPtr : valid pointer to the listview structure
10384 static void LISTVIEW_UpdateSize(LISTVIEW_INFO *infoPtr)
10386 TRACE("uView=%d, rcList(old)=%s\n", infoPtr->uView, wine_dbgstr_rect(&infoPtr->rcList));
10388 GetClientRect(infoPtr->hwndSelf, &infoPtr->rcList);
10390 if (infoPtr->uView == LV_VIEW_LIST)
10392 /* Apparently the "LIST" style is supposed to have the same
10393 * number of items in a column even if there is no scroll bar.
10394 * Since if a scroll bar already exists then the bottom is already
10395 * reduced, only reduce if the scroll bar does not currently exist.
10396 * The "2" is there to mimic the native control. I think it may be
10397 * related to either padding or edges. (GLA 7/2002)
10399 if (!(infoPtr->dwStyle & WS_HSCROLL))
10400 infoPtr->rcList.bottom -= GetSystemMetrics(SM_CYHSCROLL);
10401 infoPtr->rcList.bottom = max (infoPtr->rcList.bottom - 2, 0);
10403 else if (infoPtr->uView == LV_VIEW_DETAILS)
10405 /* if control created invisible header isn't created */
10406 if (infoPtr->hwndHeader)
10411 hl.prc = &infoPtr->rcList;
10413 SendMessageW( infoPtr->hwndHeader, HDM_LAYOUT, 0, (LPARAM)&hl );
10414 TRACE(" wp.flags=0x%08x, wp=%d,%d (%dx%d)\n", wp.flags, wp.x, wp.y, wp.cx, wp.cy);
10415 SetWindowPos(wp.hwnd, wp.hwndInsertAfter, wp.x, wp.y, wp.cx, wp.cy,
10416 wp.flags | ((infoPtr->dwStyle & LVS_NOCOLUMNHEADER)
10417 ? SWP_HIDEWINDOW : SWP_SHOWWINDOW));
10418 TRACE(" after SWP wp=%d,%d (%dx%d)\n", wp.x, wp.y, wp.cx, wp.cy);
10420 infoPtr->rcList.top = max(wp.cy, 0);
10422 infoPtr->rcList.top += (infoPtr->dwLvExStyle & LVS_EX_GRIDLINES) ? 2 : 0;
10425 TRACE(" rcList=%s\n", wine_dbgstr_rect(&infoPtr->rcList));
10430 * Processes WM_STYLECHANGED messages.
10433 * [I] infoPtr : valid pointer to the listview structure
10434 * [I] wStyleType : window style type (normal or extended)
10435 * [I] lpss : window style information
10440 static INT LISTVIEW_StyleChanged(LISTVIEW_INFO *infoPtr, WPARAM wStyleType,
10441 const STYLESTRUCT *lpss)
10443 UINT uNewView = lpss->styleNew & LVS_TYPEMASK;
10444 UINT uOldView = lpss->styleOld & LVS_TYPEMASK;
10447 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
10448 wStyleType, lpss->styleOld, lpss->styleNew);
10450 if (wStyleType != GWL_STYLE) return 0;
10452 infoPtr->dwStyle = lpss->styleNew;
10453 map_style_view(infoPtr);
10455 if (((lpss->styleOld & WS_HSCROLL) != 0)&&
10456 ((lpss->styleNew & WS_HSCROLL) == 0))
10457 ShowScrollBar(infoPtr->hwndSelf, SB_HORZ, FALSE);
10459 if (((lpss->styleOld & WS_VSCROLL) != 0)&&
10460 ((lpss->styleNew & WS_VSCROLL) == 0))
10461 ShowScrollBar(infoPtr->hwndSelf, SB_VERT, FALSE);
10463 if (uNewView != uOldView)
10465 SIZE oldIconSize = infoPtr->iconSize;
10468 SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
10469 ShowWindow(infoPtr->hwndHeader, SW_HIDE);
10471 ShowScrollBar(infoPtr->hwndSelf, SB_BOTH, FALSE);
10472 SetRectEmpty(&infoPtr->rcFocus);
10474 himl = (uNewView == LVS_ICON ? infoPtr->himlNormal : infoPtr->himlSmall);
10475 set_icon_size(&infoPtr->iconSize, himl, uNewView != LVS_ICON);
10477 if (uNewView == LVS_ICON)
10479 if ((infoPtr->iconSize.cx != oldIconSize.cx) || (infoPtr->iconSize.cy != oldIconSize.cy))
10481 TRACE("icon old size=(%d,%d), new size=(%d,%d)\n",
10482 oldIconSize.cx, oldIconSize.cy, infoPtr->iconSize.cx, infoPtr->iconSize.cy);
10483 LISTVIEW_SetIconSpacing(infoPtr, 0, 0);
10486 else if (uNewView == LVS_REPORT)
10491 LISTVIEW_CreateHeader( infoPtr );
10493 hl.prc = &infoPtr->rcList;
10495 SendMessageW( infoPtr->hwndHeader, HDM_LAYOUT, 0, (LPARAM)&hl );
10496 SetWindowPos(infoPtr->hwndHeader, infoPtr->hwndSelf, wp.x, wp.y, wp.cx, wp.cy,
10497 wp.flags | ((infoPtr->dwStyle & LVS_NOCOLUMNHEADER)
10498 ? SWP_HIDEWINDOW : SWP_SHOWWINDOW));
10501 LISTVIEW_UpdateItemSize(infoPtr);
10504 if (uNewView == LVS_REPORT)
10506 if ((lpss->styleOld ^ lpss->styleNew) & LVS_NOCOLUMNHEADER)
10508 if (lpss->styleNew & LVS_NOCOLUMNHEADER)
10510 /* Turn off the header control */
10511 style = GetWindowLongW(infoPtr->hwndHeader, GWL_STYLE);
10512 TRACE("Hide header control, was 0x%08x\n", style);
10513 SetWindowLongW(infoPtr->hwndHeader, GWL_STYLE, style | HDS_HIDDEN);
10515 /* Turn on the header control */
10516 if ((style = GetWindowLongW(infoPtr->hwndHeader, GWL_STYLE)) & HDS_HIDDEN)
10518 TRACE("Show header control, was 0x%08x\n", style);
10519 SetWindowLongW(infoPtr->hwndHeader, GWL_STYLE, (style & ~HDS_HIDDEN) | WS_VISIBLE);
10525 if ( (uNewView == LVS_ICON || uNewView == LVS_SMALLICON) &&
10526 (uNewView != uOldView || ((lpss->styleNew ^ lpss->styleOld) & LVS_ALIGNMASK)) )
10527 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
10529 /* update the size of the client area */
10530 LISTVIEW_UpdateSize(infoPtr);
10532 /* add scrollbars if needed */
10533 LISTVIEW_UpdateScroll(infoPtr);
10535 /* invalidate client area + erase background */
10536 LISTVIEW_InvalidateList(infoPtr);
10543 * Processes WM_STYLECHANGING messages.
10546 * [I] infoPtr : valid pointer to the listview structure
10547 * [I] wStyleType : window style type (normal or extended)
10548 * [I0] lpss : window style information
10553 static INT LISTVIEW_StyleChanging(LISTVIEW_INFO *infoPtr, WPARAM wStyleType,
10556 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
10557 wStyleType, lpss->styleOld, lpss->styleNew);
10559 /* don't forward LVS_OWNERDATA only if not already set to */
10560 if ((lpss->styleNew ^ lpss->styleOld) & LVS_OWNERDATA)
10562 if (lpss->styleOld & LVS_OWNERDATA)
10563 lpss->styleNew |= LVS_OWNERDATA;
10565 lpss->styleNew &= ~LVS_OWNERDATA;
10573 * Processes WM_SHOWWINDOW messages.
10576 * [I] infoPtr : valid pointer to the listview structure
10577 * [I] bShown : window is being shown (FALSE when hidden)
10578 * [I] iStatus : window show status
10583 static LRESULT LISTVIEW_ShowWindow(LISTVIEW_INFO *infoPtr, BOOL bShown, INT iStatus)
10585 /* header delayed creation */
10586 if ((infoPtr->uView == LV_VIEW_DETAILS) && bShown)
10588 LISTVIEW_CreateHeader(infoPtr);
10590 if (!(LVS_NOCOLUMNHEADER & infoPtr->dwStyle))
10591 ShowWindow(infoPtr->hwndHeader, SW_SHOWNORMAL);
10599 * Processes CCM_GETVERSION messages.
10602 * [I] infoPtr : valid pointer to the listview structure
10607 static inline LRESULT LISTVIEW_GetVersion(const LISTVIEW_INFO *infoPtr)
10609 return infoPtr->iVersion;
10614 * Processes CCM_SETVERSION messages.
10617 * [I] infoPtr : valid pointer to the listview structure
10618 * [I] iVersion : version to be set
10621 * -1 when requested version is greater than DLL version;
10622 * previous version otherwise
10624 static LRESULT LISTVIEW_SetVersion(LISTVIEW_INFO *infoPtr, DWORD iVersion)
10626 INT iOldVersion = infoPtr->iVersion;
10628 if (iVersion > COMCTL32_VERSION)
10631 infoPtr->iVersion = iVersion;
10633 TRACE("new version %d\n", iVersion);
10635 return iOldVersion;
10640 * Window procedure of the listview control.
10643 static LRESULT WINAPI
10644 LISTVIEW_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
10646 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongPtrW(hwnd, 0);
10648 TRACE("(uMsg=%x wParam=%lx lParam=%lx)\n", uMsg, wParam, lParam);
10650 if (!infoPtr && (uMsg != WM_NCCREATE))
10651 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
10655 case LVM_APPROXIMATEVIEWRECT:
10656 return LISTVIEW_ApproximateViewRect(infoPtr, (INT)wParam,
10657 LOWORD(lParam), HIWORD(lParam));
10659 return LISTVIEW_Arrange(infoPtr, (INT)wParam);
10661 case LVM_CANCELEDITLABEL:
10662 return LISTVIEW_CancelEditLabel(infoPtr);
10664 case LVM_CREATEDRAGIMAGE:
10665 return (LRESULT)LISTVIEW_CreateDragImage(infoPtr, (INT)wParam, (LPPOINT)lParam);
10667 case LVM_DELETEALLITEMS:
10668 return LISTVIEW_DeleteAllItems(infoPtr, FALSE);
10670 case LVM_DELETECOLUMN:
10671 return LISTVIEW_DeleteColumn(infoPtr, (INT)wParam);
10673 case LVM_DELETEITEM:
10674 return LISTVIEW_DeleteItem(infoPtr, (INT)wParam);
10676 case LVM_EDITLABELW:
10677 return (LRESULT)LISTVIEW_EditLabelT(infoPtr, (INT)wParam, TRUE);
10679 case LVM_EDITLABELA:
10680 return (LRESULT)LISTVIEW_EditLabelT(infoPtr, (INT)wParam, FALSE);
10682 /* case LVM_ENABLEGROUPVIEW: */
10684 case LVM_ENSUREVISIBLE:
10685 return LISTVIEW_EnsureVisible(infoPtr, (INT)wParam, (BOOL)lParam);
10687 case LVM_FINDITEMW:
10688 return LISTVIEW_FindItemW(infoPtr, (INT)wParam, (LPLVFINDINFOW)lParam);
10690 case LVM_FINDITEMA:
10691 return LISTVIEW_FindItemA(infoPtr, (INT)wParam, (LPLVFINDINFOA)lParam);
10693 case LVM_GETBKCOLOR:
10694 return infoPtr->clrBk;
10696 /* case LVM_GETBKIMAGE: */
10698 case LVM_GETCALLBACKMASK:
10699 return infoPtr->uCallbackMask;
10701 case LVM_GETCOLUMNA:
10702 return LISTVIEW_GetColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam, FALSE);
10704 case LVM_GETCOLUMNW:
10705 return LISTVIEW_GetColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam, TRUE);
10707 case LVM_GETCOLUMNORDERARRAY:
10708 return LISTVIEW_GetColumnOrderArray(infoPtr, (INT)wParam, (LPINT)lParam);
10710 case LVM_GETCOLUMNWIDTH:
10711 return LISTVIEW_GetColumnWidth(infoPtr, (INT)wParam);
10713 case LVM_GETCOUNTPERPAGE:
10714 return LISTVIEW_GetCountPerPage(infoPtr);
10716 case LVM_GETEDITCONTROL:
10717 return (LRESULT)infoPtr->hwndEdit;
10719 case LVM_GETEXTENDEDLISTVIEWSTYLE:
10720 return infoPtr->dwLvExStyle;
10722 /* case LVM_GETGROUPINFO: */
10724 /* case LVM_GETGROUPMETRICS: */
10726 case LVM_GETHEADER:
10727 return (LRESULT)infoPtr->hwndHeader;
10729 case LVM_GETHOTCURSOR:
10730 return (LRESULT)infoPtr->hHotCursor;
10732 case LVM_GETHOTITEM:
10733 return infoPtr->nHotItem;
10735 case LVM_GETHOVERTIME:
10736 return infoPtr->dwHoverTime;
10738 case LVM_GETIMAGELIST:
10739 return (LRESULT)LISTVIEW_GetImageList(infoPtr, (INT)wParam);
10741 /* case LVM_GETINSERTMARK: */
10743 /* case LVM_GETINSERTMARKCOLOR: */
10745 /* case LVM_GETINSERTMARKRECT: */
10747 case LVM_GETISEARCHSTRINGA:
10748 case LVM_GETISEARCHSTRINGW:
10749 FIXME("LVM_GETISEARCHSTRING: unimplemented\n");
10753 return LISTVIEW_GetItemExtT(infoPtr, (LPLVITEMW)lParam, FALSE);
10756 return LISTVIEW_GetItemExtT(infoPtr, (LPLVITEMW)lParam, TRUE);
10758 case LVM_GETITEMCOUNT:
10759 return infoPtr->nItemCount;
10761 case LVM_GETITEMPOSITION:
10762 return LISTVIEW_GetItemPosition(infoPtr, (INT)wParam, (LPPOINT)lParam);
10764 case LVM_GETITEMRECT:
10765 return LISTVIEW_GetItemRect(infoPtr, (INT)wParam, (LPRECT)lParam);
10767 case LVM_GETITEMSPACING:
10768 return LISTVIEW_GetItemSpacing(infoPtr, (BOOL)wParam);
10770 case LVM_GETITEMSTATE:
10771 return LISTVIEW_GetItemState(infoPtr, (INT)wParam, (UINT)lParam);
10773 case LVM_GETITEMTEXTA:
10774 return LISTVIEW_GetItemTextT(infoPtr, (INT)wParam, (LPLVITEMW)lParam, FALSE);
10776 case LVM_GETITEMTEXTW:
10777 return LISTVIEW_GetItemTextT(infoPtr, (INT)wParam, (LPLVITEMW)lParam, TRUE);
10779 case LVM_GETNEXTITEM:
10780 return LISTVIEW_GetNextItem(infoPtr, (INT)wParam, LOWORD(lParam));
10782 case LVM_GETNUMBEROFWORKAREAS:
10783 FIXME("LVM_GETNUMBEROFWORKAREAS: unimplemented\n");
10786 case LVM_GETORIGIN:
10787 if (!lParam) return FALSE;
10788 if (infoPtr->uView == LV_VIEW_DETAILS ||
10789 infoPtr->uView == LV_VIEW_LIST) return FALSE;
10790 LISTVIEW_GetOrigin(infoPtr, (LPPOINT)lParam);
10793 /* case LVM_GETOUTLINECOLOR: */
10795 /* case LVM_GETSELECTEDCOLUMN: */
10797 case LVM_GETSELECTEDCOUNT:
10798 return LISTVIEW_GetSelectedCount(infoPtr);
10800 case LVM_GETSELECTIONMARK:
10801 return infoPtr->nSelectionMark;
10803 case LVM_GETSTRINGWIDTHA:
10804 return LISTVIEW_GetStringWidthT(infoPtr, (LPCWSTR)lParam, FALSE);
10806 case LVM_GETSTRINGWIDTHW:
10807 return LISTVIEW_GetStringWidthT(infoPtr, (LPCWSTR)lParam, TRUE);
10809 case LVM_GETSUBITEMRECT:
10810 return LISTVIEW_GetSubItemRect(infoPtr, (UINT)wParam, (LPRECT)lParam);
10812 case LVM_GETTEXTBKCOLOR:
10813 return infoPtr->clrTextBk;
10815 case LVM_GETTEXTCOLOR:
10816 return infoPtr->clrText;
10818 /* case LVM_GETTILEINFO: */
10820 /* case LVM_GETTILEVIEWINFO: */
10822 case LVM_GETTOOLTIPS:
10823 if( !infoPtr->hwndToolTip )
10824 infoPtr->hwndToolTip = COMCTL32_CreateToolTip( hwnd );
10825 return (LRESULT)infoPtr->hwndToolTip;
10827 case LVM_GETTOPINDEX:
10828 return LISTVIEW_GetTopIndex(infoPtr);
10830 case LVM_GETUNICODEFORMAT:
10831 return (infoPtr->notifyFormat == NFR_UNICODE);
10834 return infoPtr->uView;
10836 case LVM_GETVIEWRECT:
10837 return LISTVIEW_GetViewRect(infoPtr, (LPRECT)lParam);
10839 case LVM_GETWORKAREAS:
10840 FIXME("LVM_GETWORKAREAS: unimplemented\n");
10843 /* case LVM_HASGROUP: */
10846 return LISTVIEW_HitTest(infoPtr, (LPLVHITTESTINFO)lParam, FALSE, TRUE);
10848 case LVM_INSERTCOLUMNA:
10849 return LISTVIEW_InsertColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam, FALSE);
10851 case LVM_INSERTCOLUMNW:
10852 return LISTVIEW_InsertColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam, TRUE);
10854 /* case LVM_INSERTGROUP: */
10856 /* case LVM_INSERTGROUPSORTED: */
10858 case LVM_INSERTITEMA:
10859 return LISTVIEW_InsertItemT(infoPtr, (LPLVITEMW)lParam, FALSE);
10861 case LVM_INSERTITEMW:
10862 return LISTVIEW_InsertItemT(infoPtr, (LPLVITEMW)lParam, TRUE);
10864 /* case LVM_INSERTMARKHITTEST: */
10866 /* case LVM_ISGROUPVIEWENABLED: */
10868 case LVM_ISITEMVISIBLE:
10869 return LISTVIEW_IsItemVisible(infoPtr, (INT)wParam);
10871 case LVM_MAPIDTOINDEX:
10872 return LISTVIEW_MapIdToIndex(infoPtr, (UINT)wParam);
10874 case LVM_MAPINDEXTOID:
10875 return LISTVIEW_MapIndexToId(infoPtr, (INT)wParam);
10877 /* case LVM_MOVEGROUP: */
10879 /* case LVM_MOVEITEMTOGROUP: */
10881 case LVM_REDRAWITEMS:
10882 return LISTVIEW_RedrawItems(infoPtr, (INT)wParam, (INT)lParam);
10884 /* case LVM_REMOVEALLGROUPS: */
10886 /* case LVM_REMOVEGROUP: */
10889 return LISTVIEW_Scroll(infoPtr, (INT)wParam, (INT)lParam);
10891 case LVM_SETBKCOLOR:
10892 return LISTVIEW_SetBkColor(infoPtr, (COLORREF)lParam);
10894 /* case LVM_SETBKIMAGE: */
10896 case LVM_SETCALLBACKMASK:
10897 infoPtr->uCallbackMask = (UINT)wParam;
10900 case LVM_SETCOLUMNA:
10901 return LISTVIEW_SetColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam, FALSE);
10903 case LVM_SETCOLUMNW:
10904 return LISTVIEW_SetColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam, TRUE);
10906 case LVM_SETCOLUMNORDERARRAY:
10907 return LISTVIEW_SetColumnOrderArray(infoPtr, (INT)wParam, (LPINT)lParam);
10909 case LVM_SETCOLUMNWIDTH:
10910 return LISTVIEW_SetColumnWidth(infoPtr, (INT)wParam, (short)LOWORD(lParam));
10912 case LVM_SETEXTENDEDLISTVIEWSTYLE:
10913 return LISTVIEW_SetExtendedListViewStyle(infoPtr, (DWORD)wParam, (DWORD)lParam);
10915 /* case LVM_SETGROUPINFO: */
10917 /* case LVM_SETGROUPMETRICS: */
10919 case LVM_SETHOTCURSOR:
10920 return (LRESULT)LISTVIEW_SetHotCursor(infoPtr, (HCURSOR)lParam);
10922 case LVM_SETHOTITEM:
10923 return LISTVIEW_SetHotItem(infoPtr, (INT)wParam);
10925 case LVM_SETHOVERTIME:
10926 return LISTVIEW_SetHoverTime(infoPtr, (DWORD)wParam);
10928 case LVM_SETICONSPACING:
10929 return LISTVIEW_SetIconSpacing(infoPtr, (short)LOWORD(lParam), (short)HIWORD(lParam));
10931 case LVM_SETIMAGELIST:
10932 return (LRESULT)LISTVIEW_SetImageList(infoPtr, (INT)wParam, (HIMAGELIST)lParam);
10934 /* case LVM_SETINFOTIP: */
10936 /* case LVM_SETINSERTMARK: */
10938 /* case LVM_SETINSERTMARKCOLOR: */
10943 if (infoPtr->dwStyle & LVS_OWNERDATA) return FALSE;
10944 return LISTVIEW_SetItemT(infoPtr, (LPLVITEMW)lParam, (uMsg == LVM_SETITEMW));
10947 case LVM_SETITEMCOUNT:
10948 return LISTVIEW_SetItemCount(infoPtr, (INT)wParam, (DWORD)lParam);
10950 case LVM_SETITEMPOSITION:
10953 pt.x = (short)LOWORD(lParam);
10954 pt.y = (short)HIWORD(lParam);
10955 return LISTVIEW_SetItemPosition(infoPtr, (INT)wParam, pt);
10958 case LVM_SETITEMPOSITION32:
10959 if (lParam == 0) return FALSE;
10960 return LISTVIEW_SetItemPosition(infoPtr, (INT)wParam, *((POINT*)lParam));
10962 case LVM_SETITEMSTATE:
10963 if (lParam == 0) return FALSE;
10964 return LISTVIEW_SetItemState(infoPtr, (INT)wParam, (LPLVITEMW)lParam);
10966 case LVM_SETITEMTEXTA:
10967 return LISTVIEW_SetItemTextT(infoPtr, (INT)wParam, (LPLVITEMW)lParam, FALSE);
10969 case LVM_SETITEMTEXTW:
10970 return LISTVIEW_SetItemTextT(infoPtr, (INT)wParam, (LPLVITEMW)lParam, TRUE);
10972 /* case LVM_SETOUTLINECOLOR: */
10974 /* case LVM_SETSELECTEDCOLUMN: */
10976 case LVM_SETSELECTIONMARK:
10977 return LISTVIEW_SetSelectionMark(infoPtr, (INT)lParam);
10979 case LVM_SETTEXTBKCOLOR:
10980 return LISTVIEW_SetTextBkColor(infoPtr, (COLORREF)lParam);
10982 case LVM_SETTEXTCOLOR:
10983 return LISTVIEW_SetTextColor(infoPtr, (COLORREF)lParam);
10985 /* case LVM_SETTILEINFO: */
10987 /* case LVM_SETTILEVIEWINFO: */
10989 /* case LVM_SETTILEWIDTH: */
10991 case LVM_SETTOOLTIPS:
10992 return (LRESULT)LISTVIEW_SetToolTips(infoPtr, (HWND)lParam);
10994 case LVM_SETUNICODEFORMAT:
10995 return LISTVIEW_SetUnicodeFormat(infoPtr, wParam);
10998 return LISTVIEW_SetView(infoPtr, wParam);
11000 /* case LVM_SETWORKAREAS: */
11002 /* case LVM_SORTGROUPS: */
11004 case LVM_SORTITEMS:
11005 return LISTVIEW_SortItems(infoPtr, (PFNLVCOMPARE)lParam, (LPARAM)wParam, FALSE);
11007 case LVM_SORTITEMSEX:
11008 return LISTVIEW_SortItems(infoPtr, (PFNLVCOMPARE)lParam, (LPARAM)wParam, TRUE);
11010 case LVM_SUBITEMHITTEST:
11011 return LISTVIEW_HitTest(infoPtr, (LPLVHITTESTINFO)lParam, TRUE, FALSE);
11014 return LISTVIEW_Update(infoPtr, (INT)wParam);
11016 case CCM_GETVERSION:
11017 return LISTVIEW_GetVersion(infoPtr);
11019 case CCM_SETVERSION:
11020 return LISTVIEW_SetVersion(infoPtr, wParam);
11023 return LISTVIEW_ProcessLetterKeys( infoPtr, wParam, lParam );
11026 return LISTVIEW_Command(infoPtr, wParam, lParam);
11029 return LISTVIEW_NCCreate(hwnd, (LPCREATESTRUCTW)lParam);
11032 return LISTVIEW_Create(hwnd, (LPCREATESTRUCTW)lParam);
11035 return LISTVIEW_Destroy(infoPtr);
11038 return LISTVIEW_Enable(infoPtr, (BOOL)wParam);
11040 case WM_ERASEBKGND:
11041 return LISTVIEW_EraseBkgnd(infoPtr, (HDC)wParam);
11043 case WM_GETDLGCODE:
11044 return DLGC_WANTCHARS | DLGC_WANTARROWS;
11047 return (LRESULT)infoPtr->hFont;
11050 return LISTVIEW_HScroll(infoPtr, (INT)LOWORD(wParam), 0, (HWND)lParam);
11053 return LISTVIEW_KeyDown(infoPtr, (INT)wParam, (LONG)lParam);
11056 return LISTVIEW_KillFocus(infoPtr);
11058 case WM_LBUTTONDBLCLK:
11059 return LISTVIEW_LButtonDblClk(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11061 case WM_LBUTTONDOWN:
11062 return LISTVIEW_LButtonDown(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11065 return LISTVIEW_LButtonUp(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11068 return LISTVIEW_MouseMove (infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11070 case WM_MOUSEHOVER:
11071 return LISTVIEW_MouseHover(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11074 return LISTVIEW_NCDestroy(infoPtr);
11077 if (LISTVIEW_NCPaint(infoPtr, (HRGN)wParam))
11082 if (lParam && ((LPNMHDR)lParam)->hwndFrom == infoPtr->hwndHeader)
11083 return LISTVIEW_HeaderNotification(infoPtr, (LPNMHEADERW)lParam);
11086 case WM_NOTIFYFORMAT:
11087 return LISTVIEW_NotifyFormat(infoPtr, (HWND)wParam, (INT)lParam);
11089 case WM_PRINTCLIENT:
11090 return LISTVIEW_PrintClient(infoPtr, (HDC)wParam, (DWORD)lParam);
11093 return LISTVIEW_WMPaint(infoPtr, (HDC)wParam);
11095 case WM_RBUTTONDBLCLK:
11096 return LISTVIEW_RButtonDblClk(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11098 case WM_RBUTTONDOWN:
11099 return LISTVIEW_RButtonDown(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11102 return LISTVIEW_RButtonUp(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11105 if(LISTVIEW_SetCursor(infoPtr, (HWND)wParam, LOWORD(lParam), HIWORD(lParam)))
11110 return LISTVIEW_SetFocus(infoPtr, (HWND)wParam);
11113 return LISTVIEW_SetFont(infoPtr, (HFONT)wParam, (WORD)lParam);
11116 return LISTVIEW_SetRedraw(infoPtr, (BOOL)wParam);
11118 case WM_SHOWWINDOW:
11119 LISTVIEW_ShowWindow(infoPtr, (BOOL)wParam, (INT)lParam);
11120 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
11123 return LISTVIEW_Size(infoPtr, (short)LOWORD(lParam), (short)HIWORD(lParam));
11125 case WM_STYLECHANGED:
11126 return LISTVIEW_StyleChanged(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
11128 case WM_STYLECHANGING:
11129 return LISTVIEW_StyleChanging(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
11131 case WM_SYSCOLORCHANGE:
11132 COMCTL32_RefreshSysColors();
11135 /* case WM_TIMER: */
11136 case WM_THEMECHANGED:
11137 return LISTVIEW_ThemeChanged(infoPtr);
11140 return LISTVIEW_VScroll(infoPtr, (INT)LOWORD(wParam), 0, (HWND)lParam);
11142 case WM_MOUSEWHEEL:
11143 if (wParam & (MK_SHIFT | MK_CONTROL))
11144 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
11145 return LISTVIEW_MouseWheel(infoPtr, (short int)HIWORD(wParam));
11147 case WM_WINDOWPOSCHANGED:
11148 if (!(((WINDOWPOS *)lParam)->flags & SWP_NOSIZE))
11150 SetWindowPos(infoPtr->hwndSelf, 0, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOACTIVATE |
11151 SWP_NOZORDER | SWP_NOMOVE | SWP_NOSIZE);
11153 if ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (infoPtr->uView == LV_VIEW_DETAILS))
11155 if (notify_measureitem(infoPtr)) LISTVIEW_InvalidateList(infoPtr);
11158 LISTVIEW_UpdateSize(infoPtr);
11159 LISTVIEW_UpdateScroll(infoPtr);
11161 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
11163 /* case WM_WININICHANGE: */
11166 if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
11167 ERR("unknown msg %04x wp=%08lx lp=%08lx\n", uMsg, wParam, lParam);
11170 /* call default window procedure */
11171 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
11178 * Registers the window class.
11186 void LISTVIEW_Register(void)
11188 WNDCLASSW wndClass;
11190 ZeroMemory(&wndClass, sizeof(WNDCLASSW));
11191 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS;
11192 wndClass.lpfnWndProc = LISTVIEW_WindowProc;
11193 wndClass.cbClsExtra = 0;
11194 wndClass.cbWndExtra = sizeof(LISTVIEW_INFO *);
11195 wndClass.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
11196 wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
11197 wndClass.lpszClassName = WC_LISTVIEWW;
11198 RegisterClassW(&wndClass);
11203 * Unregisters the window class.
11211 void LISTVIEW_Unregister(void)
11213 UnregisterClassW(WC_LISTVIEWW, NULL);
11218 * Handle any WM_COMMAND messages
11221 * [I] infoPtr : valid pointer to the listview structure
11222 * [I] wParam : the first message parameter
11223 * [I] lParam : the second message parameter
11228 static LRESULT LISTVIEW_Command(LISTVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
11230 switch (HIWORD(wParam))
11235 * Adjust the edit window size
11237 WCHAR buffer[1024];
11238 HDC hdc = GetDC(infoPtr->hwndEdit);
11239 HFONT hFont, hOldFont = 0;
11243 if (!infoPtr->hwndEdit || !hdc) return 0;
11244 GetWindowTextW(infoPtr->hwndEdit, buffer, sizeof(buffer)/sizeof(buffer[0]));
11245 GetWindowRect(infoPtr->hwndEdit, &rect);
11247 /* Select font to get the right dimension of the string */
11248 hFont = (HFONT)SendMessageW(infoPtr->hwndEdit, WM_GETFONT, 0, 0);
11251 hOldFont = SelectObject(hdc, hFont);
11254 if (GetTextExtentPoint32W(hdc, buffer, lstrlenW(buffer), &sz))
11256 TEXTMETRICW textMetric;
11258 /* Add Extra spacing for the next character */
11259 GetTextMetricsW(hdc, &textMetric);
11260 sz.cx += (textMetric.tmMaxCharWidth * 2);
11268 rect.bottom - rect.top,
11269 SWP_DRAWFRAME|SWP_NOMOVE);
11272 SelectObject(hdc, hOldFont);
11274 ReleaseDC(infoPtr->hwndEdit, hdc);
11280 LISTVIEW_CancelEditLabel(infoPtr);
11284 return SendMessageW (infoPtr->hwndNotify, WM_COMMAND, wParam, lParam);
11293 * Subclassed edit control windproc function
11296 * [I] hwnd : the edit window handle
11297 * [I] uMsg : the message that is to be processed
11298 * [I] wParam : first message parameter
11299 * [I] lParam : second message parameter
11300 * [I] isW : TRUE if input is Unicode
11305 static LRESULT EditLblWndProcT(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL isW)
11307 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongPtrW(GetParent(hwnd), 0);
11310 TRACE("(hwnd=%p, uMsg=%x, wParam=%lx, lParam=%lx, isW=%d)\n",
11311 hwnd, uMsg, wParam, lParam, isW);
11315 case WM_GETDLGCODE:
11316 return DLGC_WANTARROWS | DLGC_WANTALLKEYS;
11320 WNDPROC editProc = infoPtr->EditWndProc;
11321 infoPtr->EditWndProc = 0;
11322 SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (DWORD_PTR)editProc);
11323 return CallWindowProcT(editProc, hwnd, uMsg, wParam, lParam, isW);
11327 if (VK_ESCAPE == (INT)wParam)
11332 else if (VK_RETURN == (INT)wParam)
11336 return CallWindowProcT(infoPtr->EditWndProc, hwnd, uMsg, wParam, lParam, isW);
11339 /* kill the edit */
11340 if (infoPtr->hwndEdit)
11341 LISTVIEW_EndEditLabelT(infoPtr, save, isW);
11343 SendMessageW(hwnd, WM_CLOSE, 0, 0);
11349 * Subclassed edit control Unicode windproc function
11352 * [I] hwnd : the edit window handle
11353 * [I] uMsg : the message that is to be processed
11354 * [I] wParam : first message parameter
11355 * [I] lParam : second message parameter
11359 static LRESULT CALLBACK EditLblWndProcW(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
11361 return EditLblWndProcT(hwnd, uMsg, wParam, lParam, TRUE);
11366 * Subclassed edit control ANSI windproc function
11369 * [I] hwnd : the edit window handle
11370 * [I] uMsg : the message that is to be processed
11371 * [I] wParam : first message parameter
11372 * [I] lParam : second message parameter
11376 static LRESULT CALLBACK EditLblWndProcA(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
11378 return EditLblWndProcT(hwnd, uMsg, wParam, lParam, FALSE);
11383 * Creates a subclassed edit control
11386 * [I] infoPtr : valid pointer to the listview structure
11387 * [I] text : initial text for the edit
11388 * [I] style : the window style
11389 * [I] isW : TRUE if input is Unicode
11393 static HWND CreateEditLabelT(LISTVIEW_INFO *infoPtr, LPCWSTR text, DWORD style, BOOL isW)
11396 HINSTANCE hinst = (HINSTANCE)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_HINSTANCE);
11398 TRACE("(text=%s, ..., isW=%d)\n", debugtext_t(text, isW), isW);
11400 style |= WS_CHILDWINDOW|WS_CLIPSIBLINGS|ES_LEFT|ES_AUTOHSCROLL|WS_BORDER;
11402 /* Window will be resized and positioned after LVN_BEGINLABELEDIT */
11404 hedit = CreateWindowW(WC_EDITW, text, style, 0, 0, 0, 0, infoPtr->hwndSelf, 0, hinst, 0);
11406 hedit = CreateWindowA(WC_EDITA, (LPCSTR)text, style, 0, 0, 0, 0, infoPtr->hwndSelf, 0, hinst, 0);
11408 if (!hedit) return 0;
11410 infoPtr->EditWndProc = (WNDPROC)
11411 (isW ? SetWindowLongPtrW(hedit, GWLP_WNDPROC, (DWORD_PTR)EditLblWndProcW) :
11412 SetWindowLongPtrA(hedit, GWLP_WNDPROC, (DWORD_PTR)EditLblWndProcA) );
11414 SendMessageW(hedit, WM_SETFONT, (WPARAM)infoPtr->hFont, FALSE);