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)
82 * -- LVS_NOSCROLL (see Q137520)
86 * -- LVS_EX_BORDERSELECT
90 * -- LVS_EX_MULTIWORKAREAS
92 * -- LVS_EX_SIMPLESELECT
93 * -- LVS_EX_TWOCLICKACTIVATE
94 * -- LVS_EX_UNDERLINECOLD
95 * -- LVS_EX_UNDERLINEHOT
98 * -- LVN_BEGINSCROLL, LVN_ENDSCROLL
105 * -- LVM_ENABLEGROUPVIEW
106 * -- LVM_GETBKIMAGE, LVM_SETBKIMAGE
107 * -- LVM_GETGROUPINFO, LVM_SETGROUPINFO
108 * -- LVM_GETGROUPMETRICS, LVM_SETGROUPMETRICS
109 * -- LVM_GETINSERTMARK, LVM_SETINSERTMARK
110 * -- LVM_GETINSERTMARKCOLOR, LVM_SETINSERTMARKCOLOR
111 * -- LVM_GETINSERTMARKRECT
112 * -- LVM_GETNUMBEROFWORKAREAS
113 * -- LVM_GETOUTLINECOLOR, LVM_SETOUTLINECOLOR
114 * -- LVM_GETSELECTEDCOLUMN, LVM_SETSELECTEDCOLUMN
115 * -- LVM_GETISEARCHSTRINGW, LVM_GETISEARCHSTRINGA
116 * -- LVM_GETTILEINFO, LVM_SETTILEINFO
117 * -- LVM_GETTILEVIEWINFO, LVM_SETTILEVIEWINFO
118 * -- LVM_GETWORKAREAS, LVM_SETWORKAREAS
119 * -- LVM_HASGROUP, LVM_INSERTGROUP, LVM_REMOVEGROUP, LVM_REMOVEALLGROUPS
120 * -- LVM_INSERTGROUPSORTED
121 * -- LVM_INSERTMARKHITTEST
122 * -- LVM_ISGROUPVIEWENABLED
124 * -- LVM_MOVEITEMTOGROUP
126 * -- LVM_SETTILEWIDTH
130 * -- ListView_GetHoverTime, ListView_SetHoverTime
131 * -- ListView_GetISearchString
132 * -- ListView_GetNumberOfWorkAreas
133 * -- ListView_GetWorkAreas, ListView_SetWorkAreas
138 * Known differences in message stream from native control (not known if
139 * these differences cause problems):
140 * LVM_INSERTITEM issues LVM_SETITEMSTATE and LVM_SETITEM in certain cases.
141 * LVM_SETITEM does not always issue LVN_ITEMCHANGING/LVN_ITEMCHANGED.
142 * WM_CREATE does not issue WM_QUERYUISTATE and associated registry
143 * processing for "USEDOUBLECLICKTIME".
147 #include "wine/port.h"
162 #include "commctrl.h"
163 #include "comctl32.h"
166 #include "wine/debug.h"
167 #include "wine/unicode.h"
169 WINE_DEFAULT_DEBUG_CHANNEL(listview);
171 typedef struct tagCOLUMN_INFO
173 RECT rcHeader; /* tracks the header's rectangle */
174 INT fmt; /* same as LVCOLUMN.fmt */
178 typedef struct tagITEMHDR
182 } ITEMHDR, *LPITEMHDR;
184 typedef struct tagSUBITEM_INFO
190 typedef struct tagITEM_ID ITEM_ID;
192 typedef struct tagITEM_INFO
203 UINT id; /* item id */
204 HDPA item; /* link to item data */
207 typedef struct tagRANGE
213 typedef struct tagRANGES
218 typedef struct tagITERATOR
227 typedef struct tagDELAYED_ITEM_EDIT
233 typedef struct tagLISTVIEW_INFO
237 RECT rcList; /* This rectangle is really the window
238 * client rectangle possibly reduced by the
239 * horizontal scroll bar and/or header - see
240 * LISTVIEW_UpdateSize. This rectangle offset
241 * by the LISTVIEW_GetOrigin value is in
242 * client coordinates */
244 /* notification window */
247 BOOL bDoChangeNotify; /* send change notification messages? */
254 INT nItemCount; /* the number of items in the list */
255 HDPA hdpaItems; /* array ITEM_INFO pointers */
256 HDPA hdpaItemIds; /* array of ITEM_ID pointers */
257 HDPA hdpaPosX; /* maintains the (X, Y) coordinates of the */
258 HDPA hdpaPosY; /* items in LVS_ICON, and LVS_SMALLICON modes */
259 RANGES selectionRanges;
260 INT nSelectionMark; /* item to start next multiselection from */
262 BOOL bAutoarrange; /* Autoarrange flag when NOT in LVS_AUTOARRANGE */
265 HDPA hdpaColumns; /* array of COLUMN_INFO pointers */
266 BOOL colRectsDirty; /* trigger column rectangles requery from header */
269 BOOL bNoItemMetrics; /* flags if item metrics are not yet computed */
274 PFNLVCOMPARE pfnCompare; /* sorting callback pointer */
278 DWORD dwStyle; /* the cached window GWL_STYLE */
279 DWORD dwLvExStyle; /* extended listview style */
280 DWORD uView; /* current view available through LVM_[G,S]ETVIEW */
286 DELAYED_ITEM_EDIT itemEdit; /* Pointer to this structure will be the timer ID */
289 HIMAGELIST himlNormal;
290 HIMAGELIST himlSmall;
291 HIMAGELIST himlState;
295 POINT currIconPos; /* this is the position next icon will be placed */
299 INT xTrackLine; /* The x coefficient of the track line or -1 if none */
301 /* marquee selection */
302 BOOL bMarqueeSelect; /* marquee selection/highlight underway */
304 RECT marqueeRect; /* absolute coordinates of marquee selection */
305 RECT marqueeDrawRect; /* relative coordinates for drawing marquee */
306 POINT marqueeOrigin; /* absolute coordinates of marquee click origin */
309 BOOL bFocus; /* control has focus */
311 RECT rcFocus; /* focus bounds */
322 INT ntmHeight; /* Some cached metrics of the font used */
323 INT ntmMaxCharWidth; /* by the listview to draw items */
326 /* mouse operation */
330 POINT ptClickPos; /* point where the user clicked */
331 INT nLButtonDownItem; /* tracks item to reset multiselection on WM_LBUTTONUP */
335 /* keyboard operation */
336 DWORD lastKeyPressTimestamp;
338 INT nSearchParamLength;
339 WCHAR szSearchParam[ MAX_PATH ];
342 DWORD cditemmode; /* Keep the custom draw flags for an item/row */
343 BOOL bIsDrawing; /* Drawing in progress */
344 INT nMeasureItemHeight; /* WM_MEASUREITEM result */
345 BOOL bRedraw; /* WM_SETREDRAW switch */
348 DWORD iVersion; /* CCM_[G,S]ETVERSION */
354 /* How many we debug buffer to allocate */
355 #define DEBUG_BUFFERS 20
356 /* The size of a single debug buffer */
357 #define DEBUG_BUFFER_SIZE 256
359 /* Internal interface to LISTVIEW_HScroll and LISTVIEW_VScroll */
360 #define SB_INTERNAL -1
362 /* maximum size of a label */
363 #define DISP_TEXT_SIZE 512
365 /* padding for items in list and small icon display modes */
366 #define WIDTH_PADDING 12
368 /* padding for items in list, report and small icon display modes */
369 #define HEIGHT_PADDING 1
371 /* offset of items in report display mode */
372 #define REPORT_MARGINX 2
374 /* padding for icon in large icon display mode
375 * ICON_TOP_PADDING_NOTHITABLE - space between top of box and area
376 * that HITTEST will see.
377 * ICON_TOP_PADDING_HITABLE - spacing between above and icon.
378 * ICON_TOP_PADDING - sum of the two above.
379 * ICON_BOTTOM_PADDING - between bottom of icon and top of text
380 * LABEL_HOR_PADDING - between text and sides of box
381 * LABEL_VERT_PADDING - between bottom of text and end of box
383 * ICON_LR_PADDING - additional width above icon size.
384 * ICON_LR_HALF - half of the above value
386 #define ICON_TOP_PADDING_NOTHITABLE 2
387 #define ICON_TOP_PADDING_HITABLE 2
388 #define ICON_TOP_PADDING (ICON_TOP_PADDING_NOTHITABLE + ICON_TOP_PADDING_HITABLE)
389 #define ICON_BOTTOM_PADDING 4
390 #define LABEL_HOR_PADDING 5
391 #define LABEL_VERT_PADDING 7
392 #define ICON_LR_PADDING 16
393 #define ICON_LR_HALF (ICON_LR_PADDING/2)
395 /* default label width for items in list and small icon display modes */
396 #define DEFAULT_LABEL_WIDTH 40
397 /* maximum select rectangle width for empty text item in LV_VIEW_DETAILS */
398 #define MAX_EMPTYTEXT_SELECT_WIDTH 80
400 /* default column width for items in list display mode */
401 #define DEFAULT_COLUMN_WIDTH 128
403 /* Size of "line" scroll for V & H scrolls */
404 #define LISTVIEW_SCROLL_ICON_LINE_SIZE 37
406 /* Padding between image and label */
407 #define IMAGE_PADDING 2
409 /* Padding behind the label */
410 #define TRAILING_LABEL_PADDING 12
411 #define TRAILING_HEADER_PADDING 11
413 /* Border for the icon caption */
414 #define CAPTION_BORDER 2
416 /* Standard DrawText flags */
417 #define LV_ML_DT_FLAGS (DT_TOP | DT_NOPREFIX | DT_EDITCONTROL | DT_CENTER | DT_WORDBREAK | DT_WORD_ELLIPSIS | DT_END_ELLIPSIS)
418 #define LV_FL_DT_FLAGS (DT_TOP | DT_NOPREFIX | DT_EDITCONTROL | DT_CENTER | DT_WORDBREAK | DT_NOCLIP)
419 #define LV_SL_DT_FLAGS (DT_VCENTER | DT_NOPREFIX | DT_EDITCONTROL | DT_SINGLELINE | DT_WORD_ELLIPSIS | DT_END_ELLIPSIS)
421 /* Image index from state */
422 #define STATEIMAGEINDEX(x) (((x) & LVIS_STATEIMAGEMASK) >> 12)
424 /* The time in milliseconds to reset the search in the list */
425 #define KEY_DELAY 450
427 /* Dump the LISTVIEW_INFO structure to the debug channel */
428 #define LISTVIEW_DUMP(iP) do { \
429 TRACE("hwndSelf=%p, clrBk=0x%06x, clrText=0x%06x, clrTextBk=0x%06x, ItemHeight=%d, ItemWidth=%d, Style=0x%08x\n", \
430 iP->hwndSelf, iP->clrBk, iP->clrText, iP->clrTextBk, \
431 iP->nItemHeight, iP->nItemWidth, iP->dwStyle); \
432 TRACE("hwndSelf=%p, himlNor=%p, himlSml=%p, himlState=%p, Focused=%d, Hot=%d, exStyle=0x%08x, Focus=%d\n", \
433 iP->hwndSelf, iP->himlNormal, iP->himlSmall, iP->himlState, \
434 iP->nFocusedItem, iP->nHotItem, iP->dwLvExStyle, iP->bFocus ); \
435 TRACE("hwndSelf=%p, ntmH=%d, icSz.cx=%d, icSz.cy=%d, icSp.cx=%d, icSp.cy=%d, notifyFmt=%d\n", \
436 iP->hwndSelf, iP->ntmHeight, iP->iconSize.cx, iP->iconSize.cy, \
437 iP->iconSpacing.cx, iP->iconSpacing.cy, iP->notifyFormat); \
438 TRACE("hwndSelf=%p, rcList=%s\n", iP->hwndSelf, wine_dbgstr_rect(&iP->rcList)); \
441 static const WCHAR themeClass[] = {'L','i','s','t','V','i','e','w',0};
444 * forward declarations
446 static BOOL LISTVIEW_GetItemT(const LISTVIEW_INFO *, LPLVITEMW, BOOL);
447 static void LISTVIEW_GetItemBox(const LISTVIEW_INFO *, INT, LPRECT);
448 static void LISTVIEW_GetItemOrigin(const LISTVIEW_INFO *, INT, LPPOINT);
449 static BOOL LISTVIEW_GetItemPosition(const LISTVIEW_INFO *, INT, LPPOINT);
450 static BOOL LISTVIEW_GetItemRect(const LISTVIEW_INFO *, INT, LPRECT);
451 static void LISTVIEW_GetOrigin(const LISTVIEW_INFO *, LPPOINT);
452 static BOOL LISTVIEW_GetViewRect(const LISTVIEW_INFO *, LPRECT);
453 static void LISTVIEW_UpdateSize(LISTVIEW_INFO *);
454 static LRESULT LISTVIEW_Command(LISTVIEW_INFO *, WPARAM, LPARAM);
455 static INT LISTVIEW_GetStringWidthT(const LISTVIEW_INFO *, LPCWSTR, BOOL);
456 static BOOL LISTVIEW_KeySelection(LISTVIEW_INFO *, INT, BOOL);
457 static UINT LISTVIEW_GetItemState(const LISTVIEW_INFO *, INT, UINT);
458 static BOOL LISTVIEW_SetItemState(LISTVIEW_INFO *, INT, const LVITEMW *);
459 static LRESULT LISTVIEW_VScroll(LISTVIEW_INFO *, INT, INT);
460 static LRESULT LISTVIEW_HScroll(LISTVIEW_INFO *, INT, INT, HWND);
461 static BOOL LISTVIEW_EnsureVisible(LISTVIEW_INFO *, INT, BOOL);
462 static HIMAGELIST LISTVIEW_SetImageList(LISTVIEW_INFO *, INT, HIMAGELIST);
463 static INT LISTVIEW_HitTest(const LISTVIEW_INFO *, LPLVHITTESTINFO, BOOL, BOOL);
464 static BOOL LISTVIEW_EndEditLabelT(LISTVIEW_INFO *, BOOL, BOOL);
465 static BOOL LISTVIEW_Scroll(LISTVIEW_INFO *, INT, INT);
467 /******** Text handling functions *************************************/
469 /* A text pointer is either NULL, LPSTR_TEXTCALLBACK, or points to a
470 * text string. The string may be ANSI or Unicode, in which case
471 * the boolean isW tells us the type of the string.
473 * The name of the function tell what type of strings it expects:
474 * W: Unicode, T: ANSI/Unicode - function of isW
477 static inline BOOL is_text(LPCWSTR text)
479 return text != NULL && text != LPSTR_TEXTCALLBACKW;
482 static inline int textlenT(LPCWSTR text, BOOL isW)
484 return !is_text(text) ? 0 :
485 isW ? lstrlenW(text) : lstrlenA((LPCSTR)text);
488 static inline void textcpynT(LPWSTR dest, BOOL isDestW, LPCWSTR src, BOOL isSrcW, INT max)
491 if (isSrcW) lstrcpynW(dest, src, max);
492 else MultiByteToWideChar(CP_ACP, 0, (LPCSTR)src, -1, dest, max);
494 if (isSrcW) WideCharToMultiByte(CP_ACP, 0, src, -1, (LPSTR)dest, max, NULL, NULL);
495 else lstrcpynA((LPSTR)dest, (LPCSTR)src, max);
498 static inline LPWSTR textdupTtoW(LPCWSTR text, BOOL isW)
500 LPWSTR wstr = (LPWSTR)text;
502 if (!isW && is_text(text))
504 INT len = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)text, -1, NULL, 0);
505 wstr = Alloc(len * sizeof(WCHAR));
506 if (wstr) MultiByteToWideChar(CP_ACP, 0, (LPCSTR)text, -1, wstr, len);
508 TRACE(" wstr=%s\n", text == LPSTR_TEXTCALLBACKW ? "(callback)" : debugstr_w(wstr));
512 static inline void textfreeT(LPWSTR wstr, BOOL isW)
514 if (!isW && is_text(wstr)) Free (wstr);
518 * dest is a pointer to a Unicode string
519 * src is a pointer to a string (Unicode if isW, ANSI if !isW)
521 static BOOL textsetptrT(LPWSTR *dest, LPCWSTR src, BOOL isW)
525 if (src == LPSTR_TEXTCALLBACKW)
527 if (is_text(*dest)) Free(*dest);
528 *dest = LPSTR_TEXTCALLBACKW;
532 LPWSTR pszText = textdupTtoW(src, isW);
533 if (*dest == LPSTR_TEXTCALLBACKW) *dest = NULL;
534 bResult = Str_SetPtrW(dest, pszText);
535 textfreeT(pszText, isW);
541 * compares a Unicode to a Unicode/ANSI text string
543 static inline int textcmpWT(LPCWSTR aw, LPCWSTR bt, BOOL isW)
545 if (!aw) return bt ? -1 : 0;
546 if (!bt) return aw ? 1 : 0;
547 if (aw == LPSTR_TEXTCALLBACKW)
548 return bt == LPSTR_TEXTCALLBACKW ? 1 : -1;
549 if (bt != LPSTR_TEXTCALLBACKW)
551 LPWSTR bw = textdupTtoW(bt, isW);
552 int r = bw ? lstrcmpW(aw, bw) : 1;
560 static inline int lstrncmpiW(LPCWSTR s1, LPCWSTR s2, int n)
564 n = min(min(n, lstrlenW(s1)), lstrlenW(s2));
565 res = CompareStringW(LOCALE_USER_DEFAULT, NORM_IGNORECASE, s1, n, s2, n);
566 return res ? res - sizeof(WCHAR) : res;
569 /******** Debugging functions *****************************************/
571 static inline LPCSTR debugtext_t(LPCWSTR text, BOOL isW)
573 if (text == LPSTR_TEXTCALLBACKW) return "(callback)";
574 return isW ? debugstr_w(text) : debugstr_a((LPCSTR)text);
577 static inline LPCSTR debugtext_tn(LPCWSTR text, BOOL isW, INT n)
579 if (text == LPSTR_TEXTCALLBACKW) return "(callback)";
580 n = min(textlenT(text, isW), n);
581 return isW ? debugstr_wn(text, n) : debugstr_an((LPCSTR)text, n);
584 static char* debug_getbuf(void)
586 static int index = 0;
587 static char buffers[DEBUG_BUFFERS][DEBUG_BUFFER_SIZE];
588 return buffers[index++ % DEBUG_BUFFERS];
591 static inline const char* debugrange(const RANGE *lprng)
593 if (!lprng) return "(null)";
594 return wine_dbg_sprintf("[%d, %d]", lprng->lower, lprng->upper);
597 static const char* debugscrollinfo(const SCROLLINFO *pScrollInfo)
599 char* buf = debug_getbuf(), *text = buf;
600 int len, size = DEBUG_BUFFER_SIZE;
602 if (pScrollInfo == NULL) return "(null)";
603 len = snprintf(buf, size, "{cbSize=%d, ", pScrollInfo->cbSize);
604 if (len == -1) goto end; buf += len; size -= len;
605 if (pScrollInfo->fMask & SIF_RANGE)
606 len = snprintf(buf, size, "nMin=%d, nMax=%d, ", pScrollInfo->nMin, pScrollInfo->nMax);
608 if (len == -1) goto end; buf += len; size -= len;
609 if (pScrollInfo->fMask & SIF_PAGE)
610 len = snprintf(buf, size, "nPage=%u, ", pScrollInfo->nPage);
612 if (len == -1) goto end; buf += len; size -= len;
613 if (pScrollInfo->fMask & SIF_POS)
614 len = snprintf(buf, size, "nPos=%d, ", pScrollInfo->nPos);
616 if (len == -1) goto end; buf += len; size -= len;
617 if (pScrollInfo->fMask & SIF_TRACKPOS)
618 len = snprintf(buf, size, "nTrackPos=%d, ", pScrollInfo->nTrackPos);
620 if (len == -1) goto end; buf += len; size -= len;
623 buf = text + strlen(text);
625 if (buf - text > 2) { buf[-2] = '}'; buf[-1] = 0; }
629 static const char* debugnmlistview(const NMLISTVIEW *plvnm)
631 if (!plvnm) return "(null)";
632 return wine_dbg_sprintf("iItem=%d, iSubItem=%d, uNewState=0x%x,"
633 " uOldState=0x%x, uChanged=0x%x, ptAction=%s, lParam=%ld",
634 plvnm->iItem, plvnm->iSubItem, plvnm->uNewState, plvnm->uOldState,
635 plvnm->uChanged, wine_dbgstr_point(&plvnm->ptAction), plvnm->lParam);
638 static const char* debuglvitem_t(const LVITEMW *lpLVItem, BOOL isW)
640 char* buf = debug_getbuf(), *text = buf;
641 int len, size = DEBUG_BUFFER_SIZE;
643 if (lpLVItem == NULL) return "(null)";
644 len = snprintf(buf, size, "{iItem=%d, iSubItem=%d, ", lpLVItem->iItem, lpLVItem->iSubItem);
645 if (len == -1) goto end; buf += len; size -= len;
646 if (lpLVItem->mask & LVIF_STATE)
647 len = snprintf(buf, size, "state=%x, stateMask=%x, ", lpLVItem->state, lpLVItem->stateMask);
649 if (len == -1) goto end; buf += len; size -= len;
650 if (lpLVItem->mask & LVIF_TEXT)
651 len = snprintf(buf, size, "pszText=%s, cchTextMax=%d, ", debugtext_tn(lpLVItem->pszText, isW, 80), lpLVItem->cchTextMax);
653 if (len == -1) goto end; buf += len; size -= len;
654 if (lpLVItem->mask & LVIF_IMAGE)
655 len = snprintf(buf, size, "iImage=%d, ", lpLVItem->iImage);
657 if (len == -1) goto end; buf += len; size -= len;
658 if (lpLVItem->mask & LVIF_PARAM)
659 len = snprintf(buf, size, "lParam=%lx, ", lpLVItem->lParam);
661 if (len == -1) goto end; buf += len; size -= len;
662 if (lpLVItem->mask & LVIF_INDENT)
663 len = snprintf(buf, size, "iIndent=%d, ", lpLVItem->iIndent);
665 if (len == -1) goto end; buf += len; size -= len;
668 buf = text + strlen(text);
670 if (buf - text > 2) { buf[-2] = '}'; buf[-1] = 0; }
674 static const char* debuglvcolumn_t(const LVCOLUMNW *lpColumn, BOOL isW)
676 char* buf = debug_getbuf(), *text = buf;
677 int len, size = DEBUG_BUFFER_SIZE;
679 if (lpColumn == NULL) return "(null)";
680 len = snprintf(buf, size, "{");
681 if (len == -1) goto end; buf += len; size -= len;
682 if (lpColumn->mask & LVCF_SUBITEM)
683 len = snprintf(buf, size, "iSubItem=%d, ", lpColumn->iSubItem);
685 if (len == -1) goto end; buf += len; size -= len;
686 if (lpColumn->mask & LVCF_FMT)
687 len = snprintf(buf, size, "fmt=%x, ", lpColumn->fmt);
689 if (len == -1) goto end; buf += len; size -= len;
690 if (lpColumn->mask & LVCF_WIDTH)
691 len = snprintf(buf, size, "cx=%d, ", lpColumn->cx);
693 if (len == -1) goto end; buf += len; size -= len;
694 if (lpColumn->mask & LVCF_TEXT)
695 len = snprintf(buf, size, "pszText=%s, cchTextMax=%d, ", debugtext_tn(lpColumn->pszText, isW, 80), lpColumn->cchTextMax);
697 if (len == -1) goto end; buf += len; size -= len;
698 if (lpColumn->mask & LVCF_IMAGE)
699 len = snprintf(buf, size, "iImage=%d, ", lpColumn->iImage);
701 if (len == -1) goto end; buf += len; size -= len;
702 if (lpColumn->mask & LVCF_ORDER)
703 len = snprintf(buf, size, "iOrder=%d, ", lpColumn->iOrder);
705 if (len == -1) goto end; buf += len; size -= len;
708 buf = text + strlen(text);
710 if (buf - text > 2) { buf[-2] = '}'; buf[-1] = 0; }
714 static const char* debuglvhittestinfo(const LVHITTESTINFO *lpht)
716 if (!lpht) return "(null)";
718 return wine_dbg_sprintf("{pt=%s, flags=0x%x, iItem=%d, iSubItem=%d}",
719 wine_dbgstr_point(&lpht->pt), lpht->flags, lpht->iItem, lpht->iSubItem);
722 /* Return the corresponding text for a given scroll value */
723 static inline LPCSTR debugscrollcode(int nScrollCode)
727 case SB_LINELEFT: return "SB_LINELEFT";
728 case SB_LINERIGHT: return "SB_LINERIGHT";
729 case SB_PAGELEFT: return "SB_PAGELEFT";
730 case SB_PAGERIGHT: return "SB_PAGERIGHT";
731 case SB_THUMBPOSITION: return "SB_THUMBPOSITION";
732 case SB_THUMBTRACK: return "SB_THUMBTRACK";
733 case SB_ENDSCROLL: return "SB_ENDSCROLL";
734 case SB_INTERNAL: return "SB_INTERNAL";
735 default: return "unknown";
740 /******** Notification functions ************************************/
742 static int get_ansi_notification(UINT unicodeNotificationCode)
744 switch (unicodeNotificationCode)
746 case LVN_BEGINLABELEDITA:
747 case LVN_BEGINLABELEDITW: return LVN_BEGINLABELEDITA;
748 case LVN_ENDLABELEDITA:
749 case LVN_ENDLABELEDITW: return LVN_ENDLABELEDITA;
750 case LVN_GETDISPINFOA:
751 case LVN_GETDISPINFOW: return LVN_GETDISPINFOA;
752 case LVN_SETDISPINFOA:
753 case LVN_SETDISPINFOW: return LVN_SETDISPINFOA;
754 case LVN_ODFINDITEMA:
755 case LVN_ODFINDITEMW: return LVN_ODFINDITEMA;
756 case LVN_GETINFOTIPA:
757 case LVN_GETINFOTIPW: return LVN_GETINFOTIPA;
758 /* header forwards */
760 case HDN_TRACKW: return HDN_TRACKA;
762 case HDN_ENDTRACKW: return HDN_ENDTRACKA;
763 case HDN_BEGINDRAG: return HDN_BEGINDRAG;
764 case HDN_ENDDRAG: return HDN_ENDDRAG;
765 case HDN_ITEMCHANGINGA:
766 case HDN_ITEMCHANGINGW: return HDN_ITEMCHANGINGA;
767 case HDN_ITEMCHANGEDA:
768 case HDN_ITEMCHANGEDW: return HDN_ITEMCHANGEDA;
770 case HDN_ITEMCLICKW: return HDN_ITEMCLICKA;
771 case HDN_DIVIDERDBLCLICKA:
772 case HDN_DIVIDERDBLCLICKW: return HDN_DIVIDERDBLCLICKA;
775 FIXME("unknown notification %x\n", unicodeNotificationCode);
776 return unicodeNotificationCode;
779 /* forwards header notifications to listview parent */
780 static LRESULT notify_forward_header(const LISTVIEW_INFO *infoPtr, const NMHEADERW *lpnmh)
784 HD_TEXTFILTERA textfilter;
785 LPSTR text = NULL, filter = NULL;
788 /* on unicode format exit earlier */
789 if (infoPtr->notifyFormat == NFR_UNICODE)
790 return SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, lpnmh->hdr.idFrom,
793 /* header always supplies unicode notifications,
794 all we have to do is to convert strings to ANSI */
795 nmhA = *(const NMHEADERA*)lpnmh;
798 hditema = *(HDITEMA*)lpnmh->pitem;
799 nmhA.pitem = &hditema;
800 /* convert item text */
801 if (lpnmh->pitem->mask & HDI_TEXT)
803 hditema.pszText = NULL;
804 Str_SetPtrWtoA(&hditema.pszText, lpnmh->pitem->pszText);
805 text = hditema.pszText;
807 /* convert filter text */
808 if ((lpnmh->pitem->mask & HDI_FILTER) && (lpnmh->pitem->type == HDFT_ISSTRING) &&
809 lpnmh->pitem->pvFilter)
811 hditema.pvFilter = &textfilter;
812 textfilter = *(HD_TEXTFILTERA*)(lpnmh->pitem->pvFilter);
813 textfilter.pszText = NULL;
814 Str_SetPtrWtoA(&textfilter.pszText, ((HD_TEXTFILTERW*)lpnmh->pitem->pvFilter)->pszText);
815 filter = textfilter.pszText;
818 nmhA.hdr.code = get_ansi_notification(lpnmh->hdr.code);
820 ret = SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmhA.hdr.idFrom,
830 static LRESULT notify_hdr(const LISTVIEW_INFO *infoPtr, INT code, LPNMHDR pnmh)
834 TRACE("(code=%d)\n", code);
836 pnmh->hwndFrom = infoPtr->hwndSelf;
837 pnmh->idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
839 result = SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, pnmh->idFrom, (LPARAM)pnmh);
841 TRACE(" <= %ld\n", result);
846 static inline BOOL notify(const LISTVIEW_INFO *infoPtr, INT code)
849 HWND hwnd = infoPtr->hwndSelf;
850 notify_hdr(infoPtr, code, &nmh);
851 return IsWindow(hwnd);
854 static inline void notify_itemactivate(const LISTVIEW_INFO *infoPtr, const LVHITTESTINFO *htInfo)
865 item.mask = LVIF_PARAM|LVIF_STATE;
866 item.iItem = htInfo->iItem;
868 if (LISTVIEW_GetItemT(infoPtr, &item, TRUE)) {
869 nmia.lParam = item.lParam;
870 nmia.uOldState = item.state;
871 nmia.uNewState = item.state | LVIS_ACTIVATING;
872 nmia.uChanged = LVIF_STATE;
875 nmia.iItem = htInfo->iItem;
876 nmia.iSubItem = htInfo->iSubItem;
877 nmia.ptAction = htInfo->pt;
879 if (GetKeyState(VK_SHIFT) & 0x8000) nmia.uKeyFlags |= LVKF_SHIFT;
880 if (GetKeyState(VK_CONTROL) & 0x8000) nmia.uKeyFlags |= LVKF_CONTROL;
881 if (GetKeyState(VK_MENU) & 0x8000) nmia.uKeyFlags |= LVKF_ALT;
883 notify_hdr(infoPtr, LVN_ITEMACTIVATE, (LPNMHDR)&nmia);
886 static inline LRESULT notify_listview(const LISTVIEW_INFO *infoPtr, INT code, LPNMLISTVIEW plvnm)
888 TRACE("(code=%d, plvnm=%s)\n", code, debugnmlistview(plvnm));
889 return notify_hdr(infoPtr, code, (LPNMHDR)plvnm);
892 static BOOL notify_click(const LISTVIEW_INFO *infoPtr, INT code, const LVHITTESTINFO *lvht)
896 HWND hwnd = infoPtr->hwndSelf;
898 TRACE("code=%d, lvht=%s\n", code, debuglvhittestinfo(lvht));
899 ZeroMemory(&nmia, sizeof(nmia));
900 nmia.iItem = lvht->iItem;
901 nmia.iSubItem = lvht->iSubItem;
902 nmia.ptAction = lvht->pt;
903 item.mask = LVIF_PARAM;
904 item.iItem = lvht->iItem;
906 if (LISTVIEW_GetItemT(infoPtr, &item, TRUE)) nmia.lParam = item.lParam;
907 notify_hdr(infoPtr, code, (LPNMHDR)&nmia);
908 return IsWindow(hwnd);
911 static BOOL notify_deleteitem(const LISTVIEW_INFO *infoPtr, INT nItem)
915 HWND hwnd = infoPtr->hwndSelf;
917 ZeroMemory(&nmlv, sizeof (NMLISTVIEW));
919 item.mask = LVIF_PARAM;
922 if (LISTVIEW_GetItemT(infoPtr, &item, TRUE)) nmlv.lParam = item.lParam;
923 notify_listview(infoPtr, LVN_DELETEITEM, &nmlv);
924 return IsWindow(hwnd);
928 Send notification. depends on dispinfoW having same
929 structure as dispinfoA.
930 infoPtr : listview struct
931 notificationCode : *Unicode* notification code
932 pdi : dispinfo structure (can be unicode or ansi)
933 isW : TRUE if dispinfo is Unicode
935 static BOOL notify_dispinfoT(const LISTVIEW_INFO *infoPtr, UINT notificationCode, LPNMLVDISPINFOW pdi, BOOL isW)
937 BOOL bResult = FALSE;
938 BOOL convertToAnsi = FALSE, convertToUnicode = FALSE;
939 INT cchTempBufMax = 0, savCchTextMax = 0;
941 LPWSTR pszTempBuf = NULL, savPszText = NULL;
943 if ((pdi->item.mask & LVIF_TEXT) && is_text(pdi->item.pszText))
945 convertToAnsi = (isW && infoPtr->notifyFormat == NFR_ANSI);
946 convertToUnicode = (!isW && infoPtr->notifyFormat == NFR_UNICODE);
949 if (convertToAnsi || convertToUnicode)
951 if (notificationCode != LVN_GETDISPINFOW)
953 cchTempBufMax = convertToUnicode ?
954 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pdi->item.pszText, -1, NULL, 0):
955 WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, NULL, 0, NULL, NULL);
959 cchTempBufMax = pdi->item.cchTextMax;
960 *pdi->item.pszText = 0; /* make sure we don't process garbage */
963 pszTempBuf = Alloc( (convertToUnicode ? sizeof(WCHAR) : sizeof(CHAR)) * cchTempBufMax);
964 if (!pszTempBuf) return FALSE;
966 if (convertToUnicode)
967 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pdi->item.pszText, -1,
968 pszTempBuf, cchTempBufMax);
970 WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, (LPSTR) pszTempBuf,
971 cchTempBufMax, NULL, NULL);
973 savCchTextMax = pdi->item.cchTextMax;
974 savPszText = pdi->item.pszText;
975 pdi->item.pszText = pszTempBuf;
976 pdi->item.cchTextMax = cchTempBufMax;
979 if (infoPtr->notifyFormat == NFR_ANSI)
980 realNotifCode = get_ansi_notification(notificationCode);
982 realNotifCode = notificationCode;
983 TRACE(" pdi->item=%s\n", debuglvitem_t(&pdi->item, infoPtr->notifyFormat != NFR_ANSI));
984 bResult = notify_hdr(infoPtr, realNotifCode, &pdi->hdr);
986 if (convertToUnicode || convertToAnsi)
988 if (convertToUnicode) /* note : pointer can be changed by app ! */
989 WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, (LPSTR) savPszText,
990 savCchTextMax, NULL, NULL);
992 MultiByteToWideChar(CP_ACP, 0, (LPSTR) pdi->item.pszText, -1,
993 savPszText, savCchTextMax);
994 pdi->item.pszText = savPszText; /* restores our buffer */
995 pdi->item.cchTextMax = savCchTextMax;
1001 static void customdraw_fill(NMLVCUSTOMDRAW *lpnmlvcd, const LISTVIEW_INFO *infoPtr, HDC hdc,
1002 const RECT *rcBounds, const LVITEMW *lplvItem)
1004 ZeroMemory(lpnmlvcd, sizeof(NMLVCUSTOMDRAW));
1005 lpnmlvcd->nmcd.hdc = hdc;
1006 lpnmlvcd->nmcd.rc = *rcBounds;
1007 lpnmlvcd->clrTextBk = infoPtr->clrTextBk;
1008 lpnmlvcd->clrText = infoPtr->clrText;
1009 if (!lplvItem) return;
1010 lpnmlvcd->nmcd.dwItemSpec = lplvItem->iItem + 1;
1011 lpnmlvcd->iSubItem = lplvItem->iSubItem;
1012 if (lplvItem->state & LVIS_SELECTED) lpnmlvcd->nmcd.uItemState |= CDIS_SELECTED;
1013 if (lplvItem->state & LVIS_FOCUSED) lpnmlvcd->nmcd.uItemState |= CDIS_FOCUS;
1014 if (lplvItem->iItem == infoPtr->nHotItem) lpnmlvcd->nmcd.uItemState |= CDIS_HOT;
1015 lpnmlvcd->nmcd.lItemlParam = lplvItem->lParam;
1018 static inline DWORD notify_customdraw (const LISTVIEW_INFO *infoPtr, DWORD dwDrawStage, NMLVCUSTOMDRAW *lpnmlvcd)
1020 BOOL isForItem = (lpnmlvcd->nmcd.dwItemSpec != 0);
1023 lpnmlvcd->nmcd.dwDrawStage = dwDrawStage;
1024 if (isForItem) lpnmlvcd->nmcd.dwDrawStage |= CDDS_ITEM;
1025 if (lpnmlvcd->iSubItem) lpnmlvcd->nmcd.dwDrawStage |= CDDS_SUBITEM;
1026 if (isForItem) lpnmlvcd->nmcd.dwItemSpec--;
1027 result = notify_hdr(infoPtr, NM_CUSTOMDRAW, &lpnmlvcd->nmcd.hdr);
1028 if (isForItem) lpnmlvcd->nmcd.dwItemSpec++;
1032 static void prepaint_setup (const LISTVIEW_INFO *infoPtr, HDC hdc, NMLVCUSTOMDRAW *lpnmlvcd, BOOL SubItem)
1034 if (lpnmlvcd->clrTextBk == CLR_DEFAULT)
1035 lpnmlvcd->clrTextBk = comctl32_color.clrWindow;
1036 if (lpnmlvcd->clrText == CLR_DEFAULT)
1037 lpnmlvcd->clrText = comctl32_color.clrWindowText;
1039 /* apparently, for selected items, we have to override the returned values */
1042 if (lpnmlvcd->nmcd.uItemState & CDIS_SELECTED)
1044 if (infoPtr->bFocus)
1046 lpnmlvcd->clrTextBk = comctl32_color.clrHighlight;
1047 lpnmlvcd->clrText = comctl32_color.clrHighlightText;
1049 else if (infoPtr->dwStyle & LVS_SHOWSELALWAYS)
1051 lpnmlvcd->clrTextBk = comctl32_color.clr3dFace;
1052 lpnmlvcd->clrText = comctl32_color.clrBtnText;
1057 /* Set the text attributes */
1058 if (lpnmlvcd->clrTextBk != CLR_NONE)
1060 SetBkMode(hdc, OPAQUE);
1061 SetBkColor(hdc,lpnmlvcd->clrTextBk);
1064 SetBkMode(hdc, TRANSPARENT);
1065 SetTextColor(hdc, lpnmlvcd->clrText);
1068 static inline DWORD notify_postpaint (const LISTVIEW_INFO *infoPtr, NMLVCUSTOMDRAW *lpnmlvcd)
1070 return notify_customdraw(infoPtr, CDDS_POSTPAINT, lpnmlvcd);
1073 /* returns TRUE when repaint needed, FALSE otherwise */
1074 static BOOL notify_measureitem(LISTVIEW_INFO *infoPtr)
1076 MEASUREITEMSTRUCT mis;
1077 mis.CtlType = ODT_LISTVIEW;
1078 mis.CtlID = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1082 mis.itemHeight= infoPtr->nItemHeight;
1083 SendMessageW(infoPtr->hwndNotify, WM_MEASUREITEM, mis.CtlID, (LPARAM)&mis);
1084 if (infoPtr->nItemHeight != max(mis.itemHeight, 1))
1086 infoPtr->nMeasureItemHeight = infoPtr->nItemHeight = max(mis.itemHeight, 1);
1092 /******** Item iterator functions **********************************/
1094 static RANGES ranges_create(int count);
1095 static void ranges_destroy(RANGES ranges);
1096 static BOOL ranges_add(RANGES ranges, RANGE range);
1097 static BOOL ranges_del(RANGES ranges, RANGE range);
1098 static void ranges_dump(RANGES ranges);
1100 static inline BOOL ranges_additem(RANGES ranges, INT nItem)
1102 RANGE range = { nItem, nItem + 1 };
1104 return ranges_add(ranges, range);
1107 static inline BOOL ranges_delitem(RANGES ranges, INT nItem)
1109 RANGE range = { nItem, nItem + 1 };
1111 return ranges_del(ranges, range);
1115 * ITERATOR DOCUMENTATION
1117 * The iterator functions allow for easy, and convenient iteration
1118 * over items of interest in the list. Typically, you create a
1119 * iterator, use it, and destroy it, as such:
1122 * iterator_xxxitems(&i, ...);
1123 * while (iterator_{prev,next}(&i)
1125 * //code which uses i.nItem
1127 * iterator_destroy(&i);
1129 * where xxx is either: framed, or visible.
1130 * Note that it is important that the code destroys the iterator
1131 * after it's done with it, as the creation of the iterator may
1132 * allocate memory, which thus needs to be freed.
1134 * You can iterate both forwards, and backwards through the list,
1135 * by using iterator_next or iterator_prev respectively.
1137 * Lower numbered items are draw on top of higher number items in
1138 * LVS_ICON, and LVS_SMALLICON (which are the only modes where
1139 * items may overlap). So, to test items, you should use
1141 * which lists the items top to bottom (in Z-order).
1142 * For drawing items, you should use
1144 * which lists the items bottom to top (in Z-order).
1145 * If you keep iterating over the items after the end-of-items
1146 * marker (-1) is returned, the iterator will start from the
1147 * beginning. Typically, you don't need to test for -1,
1148 * because iterator_{next,prev} will return TRUE if more items
1149 * are to be iterated over, or FALSE otherwise.
1151 * Note: the iterator is defined to be bidirectional. That is,
1152 * any number of prev followed by any number of next, or
1153 * five versa, should leave the iterator at the same item:
1154 * prev * n, next * n = next * n, prev * n
1156 * The iterator has a notion of an out-of-order, special item,
1157 * which sits at the start of the list. This is used in
1158 * LVS_ICON, and LVS_SMALLICON mode to handle the focused item,
1159 * which needs to be first, as it may overlap other items.
1161 * The code is a bit messy because we have:
1162 * - a special item to deal with
1163 * - simple range, or composite range
1165 * If you find bugs, or want to add features, please make sure you
1166 * always check/modify *both* iterator_prev, and iterator_next.
1170 * This function iterates through the items in increasing order,
1171 * but prefixed by the special item, then -1. That is:
1172 * special, 1, 2, 3, ..., n, -1.
1173 * Each item is listed only once.
1175 static inline BOOL iterator_next(ITERATOR* i)
1179 i->nItem = i->nSpecial;
1180 if (i->nItem != -1) return TRUE;
1182 if (i->nItem == i->nSpecial)
1184 if (i->ranges) i->index = 0;
1190 if (i->nItem == i->nSpecial) i->nItem++;
1191 if (i->nItem < i->range.upper) return TRUE;
1196 if (i->index < DPA_GetPtrCount(i->ranges->hdpa))
1197 i->range = *(RANGE*)DPA_GetPtr(i->ranges->hdpa, i->index++);
1200 else if (i->nItem >= i->range.upper) goto end;
1202 i->nItem = i->range.lower;
1203 if (i->nItem >= 0) goto testitem;
1210 * This function iterates through the items in decreasing order,
1211 * followed by the special item, then -1. That is:
1212 * n, n-1, ..., 3, 2, 1, special, -1.
1213 * Each item is listed only once.
1215 static inline BOOL iterator_prev(ITERATOR* i)
1222 if (i->ranges) i->index = DPA_GetPtrCount(i->ranges->hdpa);
1225 if (i->nItem == i->nSpecial)
1233 if (i->nItem == i->nSpecial) i->nItem--;
1234 if (i->nItem >= i->range.lower) return TRUE;
1240 i->range = *(RANGE*)DPA_GetPtr(i->ranges->hdpa, --i->index);
1243 else if (!start && i->nItem < i->range.lower) goto end;
1245 i->nItem = i->range.upper;
1246 if (i->nItem > 0) goto testitem;
1248 return (i->nItem = i->nSpecial) != -1;
1251 static RANGE iterator_range(const ITERATOR *i)
1255 if (!i->ranges) return i->range;
1257 if (DPA_GetPtrCount(i->ranges->hdpa) > 0)
1259 range.lower = (*(RANGE*)DPA_GetPtr(i->ranges->hdpa, 0)).lower;
1260 range.upper = (*(RANGE*)DPA_GetPtr(i->ranges->hdpa, DPA_GetPtrCount(i->ranges->hdpa) - 1)).upper;
1262 else range.lower = range.upper = 0;
1268 * Releases resources associated with this ierator.
1270 static inline void iterator_destroy(const ITERATOR *i)
1272 ranges_destroy(i->ranges);
1276 * Create an empty iterator.
1278 static inline BOOL iterator_empty(ITERATOR* i)
1280 ZeroMemory(i, sizeof(*i));
1281 i->nItem = i->nSpecial = i->range.lower = i->range.upper = -1;
1286 * Create an iterator over a range.
1288 static inline BOOL iterator_rangeitems(ITERATOR* i, RANGE range)
1296 * Create an iterator over a bunch of ranges.
1297 * Please note that the iterator will take ownership of the ranges,
1298 * and will free them upon destruction.
1300 static inline BOOL iterator_rangesitems(ITERATOR* i, RANGES ranges)
1308 * Creates an iterator over the items which intersect frame.
1309 * Uses absolute coordinates rather than compensating for the current offset.
1311 static BOOL iterator_frameditems_absolute(ITERATOR* i, const LISTVIEW_INFO* infoPtr, const RECT *frame)
1313 RECT rcItem, rcTemp;
1315 /* in case we fail, we want to return an empty iterator */
1316 if (!iterator_empty(i)) return FALSE;
1318 TRACE("(frame=%s)\n", wine_dbgstr_rect(frame));
1320 if (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON)
1324 if (infoPtr->uView == LV_VIEW_ICON && infoPtr->nFocusedItem != -1)
1326 LISTVIEW_GetItemBox(infoPtr, infoPtr->nFocusedItem, &rcItem);
1327 if (IntersectRect(&rcTemp, &rcItem, frame))
1328 i->nSpecial = infoPtr->nFocusedItem;
1330 if (!(iterator_rangesitems(i, ranges_create(50)))) return FALSE;
1331 /* to do better here, we need to have PosX, and PosY sorted */
1332 TRACE("building icon ranges:\n");
1333 for (nItem = 0; nItem < infoPtr->nItemCount; nItem++)
1335 rcItem.left = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosX, nItem);
1336 rcItem.top = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosY, nItem);
1337 rcItem.right = rcItem.left + infoPtr->nItemWidth;
1338 rcItem.bottom = rcItem.top + infoPtr->nItemHeight;
1339 if (IntersectRect(&rcTemp, &rcItem, frame))
1340 ranges_additem(i->ranges, nItem);
1344 else if (infoPtr->uView == LV_VIEW_DETAILS)
1348 if (frame->left >= infoPtr->nItemWidth) return TRUE;
1349 if (frame->top >= infoPtr->nItemHeight * infoPtr->nItemCount) return TRUE;
1351 range.lower = max(frame->top / infoPtr->nItemHeight, 0);
1352 range.upper = min((frame->bottom - 1) / infoPtr->nItemHeight, infoPtr->nItemCount - 1) + 1;
1353 if (range.upper <= range.lower) return TRUE;
1354 if (!iterator_rangeitems(i, range)) return FALSE;
1355 TRACE(" report=%s\n", debugrange(&i->range));
1359 INT nPerCol = max((infoPtr->rcList.bottom - infoPtr->rcList.top) / infoPtr->nItemHeight, 1);
1360 INT nFirstRow = max(frame->top / infoPtr->nItemHeight, 0);
1361 INT nLastRow = min((frame->bottom - 1) / infoPtr->nItemHeight, nPerCol - 1);
1368 if (infoPtr->nItemWidth)
1370 nFirstCol = max(frame->left / infoPtr->nItemWidth, 0);
1371 nLastCol = min((frame->right - 1) / infoPtr->nItemWidth, (infoPtr->nItemCount + nPerCol - 1) / nPerCol);
1375 nFirstCol = max(frame->left, 0);
1376 nLastCol = min(frame->right - 1, (infoPtr->nItemCount + nPerCol - 1) / nPerCol);
1379 lower = nFirstCol * nPerCol + nFirstRow;
1381 TRACE("nPerCol=%d, nFirstRow=%d, nLastRow=%d, nFirstCol=%d, nLastCol=%d, lower=%d\n",
1382 nPerCol, nFirstRow, nLastRow, nFirstCol, nLastCol, lower);
1384 if (nLastCol < nFirstCol || nLastRow < nFirstRow) return TRUE;
1386 if (!(iterator_rangesitems(i, ranges_create(nLastCol - nFirstCol + 1)))) return FALSE;
1387 TRACE("building list ranges:\n");
1388 for (nCol = nFirstCol; nCol <= nLastCol; nCol++)
1390 item_range.lower = nCol * nPerCol + nFirstRow;
1391 if(item_range.lower >= infoPtr->nItemCount) break;
1392 item_range.upper = min(nCol * nPerCol + nLastRow + 1, infoPtr->nItemCount);
1393 TRACE(" list=%s\n", debugrange(&item_range));
1394 ranges_add(i->ranges, item_range);
1402 * Creates an iterator over the items which intersect lprc.
1404 static BOOL iterator_frameditems(ITERATOR* i, const LISTVIEW_INFO* infoPtr, const RECT *lprc)
1409 TRACE("(lprc=%s)\n", wine_dbgstr_rect(lprc));
1411 LISTVIEW_GetOrigin(infoPtr, &Origin);
1412 OffsetRect(&frame, -Origin.x, -Origin.y);
1414 return iterator_frameditems_absolute(i, infoPtr, &frame);
1418 * Creates an iterator over the items which intersect the visible region of hdc.
1420 static BOOL iterator_visibleitems(ITERATOR *i, const LISTVIEW_INFO *infoPtr, HDC hdc)
1422 POINT Origin, Position;
1423 RECT rcItem, rcClip;
1426 rgntype = GetClipBox(hdc, &rcClip);
1427 if (rgntype == NULLREGION) return iterator_empty(i);
1428 if (!iterator_frameditems(i, infoPtr, &rcClip)) return FALSE;
1429 if (rgntype == SIMPLEREGION) return TRUE;
1431 /* first deal with the special item */
1432 if (i->nSpecial != -1)
1434 LISTVIEW_GetItemBox(infoPtr, i->nSpecial, &rcItem);
1435 if (!RectVisible(hdc, &rcItem)) i->nSpecial = -1;
1438 /* if we can't deal with the region, we'll just go with the simple range */
1439 LISTVIEW_GetOrigin(infoPtr, &Origin);
1440 TRACE("building visible range:\n");
1441 if (!i->ranges && i->range.lower < i->range.upper)
1443 if (!(i->ranges = ranges_create(50))) return TRUE;
1444 if (!ranges_add(i->ranges, i->range))
1446 ranges_destroy(i->ranges);
1452 /* now delete the invisible items from the list */
1453 while(iterator_next(i))
1455 LISTVIEW_GetItemOrigin(infoPtr, i->nItem, &Position);
1456 rcItem.left = (infoPtr->uView == LV_VIEW_DETAILS) ? Origin.x : Position.x + Origin.x;
1457 rcItem.top = Position.y + Origin.y;
1458 rcItem.right = rcItem.left + infoPtr->nItemWidth;
1459 rcItem.bottom = rcItem.top + infoPtr->nItemHeight;
1460 if (!RectVisible(hdc, &rcItem))
1461 ranges_delitem(i->ranges, i->nItem);
1463 /* the iterator should restart on the next iterator_next */
1469 /******** Misc helper functions ************************************/
1471 static inline LRESULT CallWindowProcT(WNDPROC proc, HWND hwnd, UINT uMsg,
1472 WPARAM wParam, LPARAM lParam, BOOL isW)
1474 if (isW) return CallWindowProcW(proc, hwnd, uMsg, wParam, lParam);
1475 else return CallWindowProcA(proc, hwnd, uMsg, wParam, lParam);
1478 static inline BOOL is_autoarrange(const LISTVIEW_INFO *infoPtr)
1480 return ((infoPtr->dwStyle & LVS_AUTOARRANGE) || infoPtr->bAutoarrange) &&
1481 (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON);
1484 static void toggle_checkbox_state(LISTVIEW_INFO *infoPtr, INT nItem)
1486 DWORD state = STATEIMAGEINDEX(LISTVIEW_GetItemState(infoPtr, nItem, LVIS_STATEIMAGEMASK));
1487 if(state == 1 || state == 2)
1491 lvitem.state = INDEXTOSTATEIMAGEMASK(state);
1492 lvitem.stateMask = LVIS_STATEIMAGEMASK;
1493 LISTVIEW_SetItemState(infoPtr, nItem, &lvitem);
1497 /* this should be called after window style got updated,
1498 it used to reset view state to match current window style */
1499 static inline void map_style_view(LISTVIEW_INFO *infoPtr)
1501 switch (infoPtr->dwStyle & LVS_TYPEMASK)
1504 infoPtr->uView = LV_VIEW_ICON;
1507 infoPtr->uView = LV_VIEW_DETAILS;
1510 infoPtr->uView = LV_VIEW_SMALLICON;
1513 infoPtr->uView = LV_VIEW_LIST;
1517 /* computes next item id value */
1518 static DWORD get_next_itemid(const LISTVIEW_INFO *infoPtr)
1520 INT count = DPA_GetPtrCount(infoPtr->hdpaItemIds);
1524 ITEM_ID *lpID = DPA_GetPtr(infoPtr->hdpaItemIds, count - 1);
1525 return lpID->id + 1;
1530 /******** Internal API functions ************************************/
1532 static inline COLUMN_INFO * LISTVIEW_GetColumnInfo(const LISTVIEW_INFO *infoPtr, INT nSubItem)
1534 static COLUMN_INFO mainItem;
1536 if (nSubItem == 0 && DPA_GetPtrCount(infoPtr->hdpaColumns) == 0) return &mainItem;
1537 assert (nSubItem >= 0 && nSubItem < DPA_GetPtrCount(infoPtr->hdpaColumns));
1539 /* update cached column rectangles */
1540 if (infoPtr->colRectsDirty)
1543 LISTVIEW_INFO *Ptr = (LISTVIEW_INFO*)infoPtr;
1546 for (i = 0; i < DPA_GetPtrCount(infoPtr->hdpaColumns); i++) {
1547 info = DPA_GetPtr(infoPtr->hdpaColumns, i);
1548 SendMessageW(infoPtr->hwndHeader, HDM_GETITEMRECT, i, (LPARAM)&info->rcHeader);
1550 Ptr->colRectsDirty = FALSE;
1553 return DPA_GetPtr(infoPtr->hdpaColumns, nSubItem);
1556 static INT LISTVIEW_CreateHeader(LISTVIEW_INFO *infoPtr)
1558 DWORD dFlags = WS_CHILD | HDS_HORZ | HDS_FULLDRAG | HDS_DRAGDROP;
1561 if (infoPtr->hwndHeader) return 0;
1563 TRACE("Creating header for list %p\n", infoPtr->hwndSelf);
1565 /* setup creation flags */
1566 dFlags |= (LVS_NOSORTHEADER & infoPtr->dwStyle) ? 0 : HDS_BUTTONS;
1567 dFlags |= (LVS_NOCOLUMNHEADER & infoPtr->dwStyle) ? HDS_HIDDEN : 0;
1569 hInst = (HINSTANCE)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_HINSTANCE);
1572 infoPtr->hwndHeader = CreateWindowW(WC_HEADERW, NULL, dFlags,
1573 0, 0, 0, 0, infoPtr->hwndSelf, NULL, hInst, NULL);
1574 if (!infoPtr->hwndHeader) return -1;
1576 /* set header unicode format */
1577 SendMessageW(infoPtr->hwndHeader, HDM_SETUNICODEFORMAT, TRUE, 0);
1579 /* set header font */
1580 SendMessageW(infoPtr->hwndHeader, WM_SETFONT, (WPARAM)infoPtr->hFont, TRUE);
1582 LISTVIEW_UpdateSize(infoPtr);
1587 static inline void LISTVIEW_GetHeaderRect(const LISTVIEW_INFO *infoPtr, INT nSubItem, LPRECT lprc)
1589 *lprc = LISTVIEW_GetColumnInfo(infoPtr, nSubItem)->rcHeader;
1592 static inline BOOL LISTVIEW_IsHeaderEnabled(const LISTVIEW_INFO *infoPtr)
1594 return (infoPtr->uView == LV_VIEW_DETAILS ||
1595 infoPtr->dwLvExStyle & LVS_EX_HEADERINALLVIEWS) &&
1596 !(infoPtr->dwStyle & LVS_NOCOLUMNHEADER);
1599 static inline BOOL LISTVIEW_GetItemW(const LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem)
1601 return LISTVIEW_GetItemT(infoPtr, lpLVItem, TRUE);
1604 /* used to handle collapse main item column case */
1605 static inline BOOL LISTVIEW_DrawFocusRect(const LISTVIEW_INFO *infoPtr, HDC hdc)
1607 return (infoPtr->rcFocus.left < infoPtr->rcFocus.right) ?
1608 DrawFocusRect(hdc, &infoPtr->rcFocus) : FALSE;
1611 /* Listview invalidation functions: use _only_ these functions to invalidate */
1613 static inline BOOL is_redrawing(const LISTVIEW_INFO *infoPtr)
1615 return infoPtr->bRedraw;
1618 static inline void LISTVIEW_InvalidateRect(const LISTVIEW_INFO *infoPtr, const RECT* rect)
1620 if(!is_redrawing(infoPtr)) return;
1621 TRACE(" invalidating rect=%s\n", wine_dbgstr_rect(rect));
1622 InvalidateRect(infoPtr->hwndSelf, rect, TRUE);
1625 static inline void LISTVIEW_InvalidateItem(const LISTVIEW_INFO *infoPtr, INT nItem)
1629 if(!is_redrawing(infoPtr)) return;
1630 LISTVIEW_GetItemBox(infoPtr, nItem, &rcBox);
1631 LISTVIEW_InvalidateRect(infoPtr, &rcBox);
1634 static inline void LISTVIEW_InvalidateSubItem(const LISTVIEW_INFO *infoPtr, INT nItem, INT nSubItem)
1636 POINT Origin, Position;
1639 if(!is_redrawing(infoPtr)) return;
1640 assert (infoPtr->uView == LV_VIEW_DETAILS);
1641 LISTVIEW_GetOrigin(infoPtr, &Origin);
1642 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
1643 LISTVIEW_GetHeaderRect(infoPtr, nSubItem, &rcBox);
1645 rcBox.bottom = infoPtr->nItemHeight;
1646 OffsetRect(&rcBox, Origin.x + Position.x, Origin.y + Position.y);
1647 LISTVIEW_InvalidateRect(infoPtr, &rcBox);
1650 static inline void LISTVIEW_InvalidateList(const LISTVIEW_INFO *infoPtr)
1652 LISTVIEW_InvalidateRect(infoPtr, NULL);
1655 static inline void LISTVIEW_InvalidateColumn(const LISTVIEW_INFO *infoPtr, INT nColumn)
1659 if(!is_redrawing(infoPtr)) return;
1660 LISTVIEW_GetHeaderRect(infoPtr, nColumn, &rcCol);
1661 rcCol.top = infoPtr->rcList.top;
1662 rcCol.bottom = infoPtr->rcList.bottom;
1663 LISTVIEW_InvalidateRect(infoPtr, &rcCol);
1668 * Retrieves the number of items that can fit vertically in the client area.
1671 * [I] infoPtr : valid pointer to the listview structure
1674 * Number of items per row.
1676 static inline INT LISTVIEW_GetCountPerRow(const LISTVIEW_INFO *infoPtr)
1678 INT nListWidth = infoPtr->rcList.right - infoPtr->rcList.left;
1680 return max(nListWidth/(infoPtr->nItemWidth ? infoPtr->nItemWidth : 1), 1);
1685 * Retrieves the number of items that can fit horizontally in the client
1689 * [I] infoPtr : valid pointer to the listview structure
1692 * Number of items per column.
1694 static inline INT LISTVIEW_GetCountPerColumn(const LISTVIEW_INFO *infoPtr)
1696 INT nListHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
1698 return max(nListHeight / infoPtr->nItemHeight, 1);
1702 /*************************************************************************
1703 * LISTVIEW_ProcessLetterKeys
1705 * Processes keyboard messages generated by pressing the letter keys
1707 * What this does is perform a case insensitive search from the
1708 * current position with the following quirks:
1709 * - If two chars or more are pressed in quick succession we search
1710 * for the corresponding string (e.g. 'abc').
1711 * - If there is a delay we wipe away the current search string and
1712 * restart with just that char.
1713 * - If the user keeps pressing the same character, whether slowly or
1714 * fast, so that the search string is entirely composed of this
1715 * character ('aaaaa' for instance), then we search for first item
1716 * that starting with that character.
1717 * - If the user types the above character in quick succession, then
1718 * we must also search for the corresponding string ('aaaaa'), and
1719 * go to that string if there is a match.
1722 * [I] hwnd : handle to the window
1723 * [I] charCode : the character code, the actual character
1724 * [I] keyData : key data
1732 * - The current implementation has a list of characters it will
1733 * accept and it ignores everything else. In particular it will
1734 * ignore accentuated characters which seems to match what
1735 * Windows does. But I'm not sure it makes sense to follow
1737 * - We don't sound a beep when the search fails.
1741 * TREEVIEW_ProcessLetterKeys
1743 static INT LISTVIEW_ProcessLetterKeys(LISTVIEW_INFO *infoPtr, WPARAM charCode, LPARAM keyData)
1745 WCHAR buffer[MAX_PATH];
1746 INT endidx, startidx;
1752 /* simple parameter checking */
1753 if (!charCode || !keyData || infoPtr->nItemCount == 0) return 0;
1755 /* only allow the valid WM_CHARs through */
1756 if (!isalnumW(charCode) &&
1757 charCode != '.' && charCode != '`' && charCode != '!' &&
1758 charCode != '@' && charCode != '#' && charCode != '$' &&
1759 charCode != '%' && charCode != '^' && charCode != '&' &&
1760 charCode != '*' && charCode != '(' && charCode != ')' &&
1761 charCode != '-' && charCode != '_' && charCode != '+' &&
1762 charCode != '=' && charCode != '\\'&& charCode != ']' &&
1763 charCode != '}' && charCode != '[' && charCode != '{' &&
1764 charCode != '/' && charCode != '?' && charCode != '>' &&
1765 charCode != '<' && charCode != ',' && charCode != '~')
1768 /* update the search parameters */
1769 prevTime = infoPtr->lastKeyPressTimestamp;
1770 infoPtr->lastKeyPressTimestamp = GetTickCount();
1771 diff = infoPtr->lastKeyPressTimestamp - prevTime;
1773 if (diff >= 0 && diff < KEY_DELAY)
1775 if (infoPtr->nSearchParamLength < MAX_PATH - 1)
1776 infoPtr->szSearchParam[infoPtr->nSearchParamLength++] = charCode;
1778 if (infoPtr->charCode != charCode)
1779 infoPtr->charCode = charCode = 0;
1783 infoPtr->charCode = charCode;
1784 infoPtr->szSearchParam[0] = charCode;
1785 infoPtr->nSearchParamLength = 1;
1786 /* redundant with the 1 char string */
1790 /* and search from the current position */
1792 endidx = infoPtr->nItemCount;
1794 /* should start from next after focused item, so next item that matches
1795 will be selected, if there isn't any and focused matches it will be selected
1796 on second search stage from beginning of the list */
1797 if (infoPtr->nFocusedItem >= 0 && infoPtr->nItemCount > 1)
1798 startidx = infoPtr->nFocusedItem + 1;
1802 /* let application handle this for virtual listview */
1803 if (infoPtr->dwStyle & LVS_OWNERDATA)
1807 memset(&nmlv.lvfi, 0, sizeof(nmlv.lvfi));
1808 nmlv.lvfi.flags = (LVFI_WRAP | LVFI_PARTIAL);
1809 nmlv.lvfi.psz = infoPtr->szSearchParam;
1810 nmlv.iStart = startidx;
1812 infoPtr->szSearchParam[infoPtr->nSearchParamLength] = 0;
1814 nItem = notify_hdr(infoPtr, LVN_ODFINDITEMW, (LPNMHDR)&nmlv.hdr);
1820 /* first search in [startidx, endidx), on failure continue in [0, startidx) */
1823 /* start from first item if not found with >= startidx */
1824 if (i == infoPtr->nItemCount && startidx > 0)
1830 for (i = startidx; i < endidx; i++)
1833 item.mask = LVIF_TEXT;
1836 item.pszText = buffer;
1837 item.cchTextMax = MAX_PATH;
1838 if (!LISTVIEW_GetItemW(infoPtr, &item)) return 0;
1840 if (lstrncmpiW(item.pszText, infoPtr->szSearchParam, infoPtr->nSearchParamLength) == 0)
1845 else if (nItem == -1 && lstrncmpiW(item.pszText, infoPtr->szSearchParam, 1) == 0)
1847 /* this would work but we must keep looking for a longer match */
1852 if ( nItem != -1 || /* found something */
1853 endidx != infoPtr->nItemCount || /* second search done */
1854 (startidx == 0 && endidx == infoPtr->nItemCount) /* full range for first search */ )
1860 LISTVIEW_KeySelection(infoPtr, nItem, FALSE);
1865 /*************************************************************************
1866 * LISTVIEW_UpdateHeaderSize [Internal]
1868 * Function to resize the header control
1871 * [I] hwnd : handle to a window
1872 * [I] nNewScrollPos : scroll pos to set
1877 static void LISTVIEW_UpdateHeaderSize(const LISTVIEW_INFO *infoPtr, INT nNewScrollPos)
1882 TRACE("nNewScrollPos=%d\n", nNewScrollPos);
1884 if (!infoPtr->hwndHeader) return;
1886 GetWindowRect(infoPtr->hwndHeader, &winRect);
1887 point[0].x = winRect.left;
1888 point[0].y = winRect.top;
1889 point[1].x = winRect.right;
1890 point[1].y = winRect.bottom;
1892 MapWindowPoints(HWND_DESKTOP, infoPtr->hwndSelf, point, 2);
1893 point[0].x = -nNewScrollPos;
1894 point[1].x += nNewScrollPos;
1896 SetWindowPos(infoPtr->hwndHeader,0,
1897 point[0].x,point[0].y,point[1].x,point[1].y,
1898 (infoPtr->dwStyle & LVS_NOCOLUMNHEADER) ? SWP_HIDEWINDOW : SWP_SHOWWINDOW |
1899 SWP_NOZORDER | SWP_NOACTIVATE);
1904 * Update the scrollbars. This functions should be called whenever
1905 * the content, size or view changes.
1908 * [I] infoPtr : valid pointer to the listview structure
1913 static void LISTVIEW_UpdateScroll(const LISTVIEW_INFO *infoPtr)
1915 SCROLLINFO horzInfo, vertInfo;
1918 if ((infoPtr->dwStyle & LVS_NOSCROLL) || !is_redrawing(infoPtr)) return;
1920 ZeroMemory(&horzInfo, sizeof(SCROLLINFO));
1921 horzInfo.cbSize = sizeof(SCROLLINFO);
1922 horzInfo.nPage = infoPtr->rcList.right - infoPtr->rcList.left;
1924 /* for now, we'll set info.nMax to the _count_, and adjust it later */
1925 if (infoPtr->uView == LV_VIEW_LIST)
1927 INT nPerCol = LISTVIEW_GetCountPerColumn(infoPtr);
1928 horzInfo.nMax = (infoPtr->nItemCount + nPerCol - 1) / nPerCol;
1930 /* scroll by at least one column per page */
1931 if(horzInfo.nPage < infoPtr->nItemWidth)
1932 horzInfo.nPage = infoPtr->nItemWidth;
1934 if (infoPtr->nItemWidth)
1935 horzInfo.nPage /= infoPtr->nItemWidth;
1937 else if (infoPtr->uView == LV_VIEW_DETAILS)
1939 horzInfo.nMax = infoPtr->nItemWidth;
1941 else /* LV_VIEW_ICON, or LV_VIEW_SMALLICON */
1945 if (LISTVIEW_GetViewRect(infoPtr, &rcView)) horzInfo.nMax = rcView.right - rcView.left;
1948 if (LISTVIEW_IsHeaderEnabled(infoPtr))
1950 if (DPA_GetPtrCount(infoPtr->hdpaColumns))
1955 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX,
1956 DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, 0);
1958 LISTVIEW_GetHeaderRect(infoPtr, index, &rcHeader);
1959 horzInfo.nMax = rcHeader.right;
1960 TRACE("horzInfo.nMax=%d\n", horzInfo.nMax);
1964 horzInfo.fMask = SIF_RANGE | SIF_PAGE;
1965 horzInfo.nMax = max(horzInfo.nMax - 1, 0);
1966 dx = GetScrollPos(infoPtr->hwndSelf, SB_HORZ);
1967 dx -= SetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &horzInfo, TRUE);
1968 TRACE("horzInfo=%s\n", debugscrollinfo(&horzInfo));
1970 /* Setting the horizontal scroll can change the listview size
1971 * (and potentially everything else) so we need to recompute
1972 * everything again for the vertical scroll
1975 ZeroMemory(&vertInfo, sizeof(SCROLLINFO));
1976 vertInfo.cbSize = sizeof(SCROLLINFO);
1977 vertInfo.nPage = infoPtr->rcList.bottom - infoPtr->rcList.top;
1979 if (infoPtr->uView == LV_VIEW_DETAILS)
1981 vertInfo.nMax = infoPtr->nItemCount;
1983 /* scroll by at least one page */
1984 if(vertInfo.nPage < infoPtr->nItemHeight)
1985 vertInfo.nPage = infoPtr->nItemHeight;
1987 if (infoPtr->nItemHeight > 0)
1988 vertInfo.nPage /= infoPtr->nItemHeight;
1990 else if (infoPtr->uView != LV_VIEW_LIST) /* LV_VIEW_ICON, or LV_VIEW_SMALLICON */
1994 if (LISTVIEW_GetViewRect(infoPtr, &rcView)) vertInfo.nMax = rcView.bottom - rcView.top;
1997 vertInfo.fMask = SIF_RANGE | SIF_PAGE;
1998 vertInfo.nMax = max(vertInfo.nMax - 1, 0);
1999 dy = GetScrollPos(infoPtr->hwndSelf, SB_VERT);
2000 dy -= SetScrollInfo(infoPtr->hwndSelf, SB_VERT, &vertInfo, TRUE);
2001 TRACE("vertInfo=%s\n", debugscrollinfo(&vertInfo));
2003 /* Change of the range may have changed the scroll pos. If so move the content */
2004 if (dx != 0 || dy != 0)
2007 listRect = infoPtr->rcList;
2008 ScrollWindowEx(infoPtr->hwndSelf, dx, dy, &listRect, &listRect, 0, 0,
2009 SW_ERASE | SW_INVALIDATE);
2012 /* Update the Header Control */
2013 if (infoPtr->hwndHeader)
2015 horzInfo.fMask = SIF_POS;
2016 GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &horzInfo);
2017 LISTVIEW_UpdateHeaderSize(infoPtr, horzInfo.nPos);
2024 * Shows/hides the focus rectangle.
2027 * [I] infoPtr : valid pointer to the listview structure
2028 * [I] fShow : TRUE to show the focus, FALSE to hide it.
2033 static void LISTVIEW_ShowFocusRect(const LISTVIEW_INFO *infoPtr, BOOL fShow)
2037 TRACE("fShow=%d, nItem=%d\n", fShow, infoPtr->nFocusedItem);
2039 if (infoPtr->nFocusedItem < 0) return;
2041 /* we need some gymnastics in ICON mode to handle large items */
2042 if (infoPtr->uView == LV_VIEW_ICON)
2046 LISTVIEW_GetItemBox(infoPtr, infoPtr->nFocusedItem, &rcBox);
2047 if ((rcBox.bottom - rcBox.top) > infoPtr->nItemHeight)
2049 LISTVIEW_InvalidateRect(infoPtr, &rcBox);
2054 if (!(hdc = GetDC(infoPtr->hwndSelf))) return;
2056 /* for some reason, owner draw should work only in report mode */
2057 if ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (infoPtr->uView == LV_VIEW_DETAILS))
2062 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
2063 HFONT hOldFont = SelectObject(hdc, hFont);
2065 item.iItem = infoPtr->nFocusedItem;
2067 item.mask = LVIF_PARAM;
2068 if (!LISTVIEW_GetItemW(infoPtr, &item)) goto done;
2070 ZeroMemory(&dis, sizeof(dis));
2071 dis.CtlType = ODT_LISTVIEW;
2072 dis.CtlID = (UINT)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
2073 dis.itemID = item.iItem;
2074 dis.itemAction = ODA_FOCUS;
2075 if (fShow) dis.itemState |= ODS_FOCUS;
2076 dis.hwndItem = infoPtr->hwndSelf;
2078 LISTVIEW_GetItemBox(infoPtr, dis.itemID, &dis.rcItem);
2079 dis.itemData = item.lParam;
2081 SendMessageW(infoPtr->hwndNotify, WM_DRAWITEM, dis.CtlID, (LPARAM)&dis);
2083 SelectObject(hdc, hOldFont);
2087 LISTVIEW_DrawFocusRect(infoPtr, hdc);
2090 ReleaseDC(infoPtr->hwndSelf, hdc);
2094 * Invalidates all visible selected items.
2096 static void LISTVIEW_InvalidateSelectedItems(const LISTVIEW_INFO *infoPtr)
2100 iterator_frameditems(&i, infoPtr, &infoPtr->rcList);
2101 while(iterator_next(&i))
2103 if (LISTVIEW_GetItemState(infoPtr, i.nItem, LVIS_SELECTED))
2104 LISTVIEW_InvalidateItem(infoPtr, i.nItem);
2106 iterator_destroy(&i);
2111 * DESCRIPTION: [INTERNAL]
2112 * Computes an item's (left,top) corner, relative to rcView.
2113 * That is, the position has NOT been made relative to the Origin.
2114 * This is deliberate, to avoid computing the Origin over, and
2115 * over again, when this function is called in a loop. Instead,
2116 * one can factor the computation of the Origin before the loop,
2117 * and offset the value returned by this function, on every iteration.
2120 * [I] infoPtr : valid pointer to the listview structure
2121 * [I] nItem : item number
2122 * [O] lpptOrig : item top, left corner
2127 static void LISTVIEW_GetItemOrigin(const LISTVIEW_INFO *infoPtr, INT nItem, LPPOINT lpptPosition)
2129 assert(nItem >= 0 && nItem < infoPtr->nItemCount);
2131 if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
2133 lpptPosition->x = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosX, nItem);
2134 lpptPosition->y = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosY, nItem);
2136 else if (infoPtr->uView == LV_VIEW_LIST)
2138 INT nCountPerColumn = LISTVIEW_GetCountPerColumn(infoPtr);
2139 lpptPosition->x = nItem / nCountPerColumn * infoPtr->nItemWidth;
2140 lpptPosition->y = nItem % nCountPerColumn * infoPtr->nItemHeight;
2142 else /* LV_VIEW_DETAILS */
2144 lpptPosition->x = REPORT_MARGINX;
2145 /* item is always at zero indexed column */
2146 if (DPA_GetPtrCount(infoPtr->hdpaColumns) > 0)
2147 lpptPosition->x += LISTVIEW_GetColumnInfo(infoPtr, 0)->rcHeader.left;
2148 lpptPosition->y = nItem * infoPtr->nItemHeight;
2153 * DESCRIPTION: [INTERNAL]
2154 * Compute the rectangles of an item. This is to localize all
2155 * the computations in one place. If you are not interested in some
2156 * of these values, simply pass in a NULL -- the function is smart
2157 * enough to compute only what's necessary. The function computes
2158 * the standard rectangles (BOUNDS, ICON, LABEL) plus a non-standard
2159 * one, the BOX rectangle. This rectangle is very cheap to compute,
2160 * and is guaranteed to contain all the other rectangles. Computing
2161 * the ICON rect is also cheap, but all the others are potentially
2162 * expensive. This gives an easy and effective optimization when
2163 * searching (like point inclusion, or rectangle intersection):
2164 * first test against the BOX, and if TRUE, test against the desired
2166 * If the function does not have all the necessary information
2167 * to computed the requested rectangles, will crash with a
2168 * failed assertion. This is done so we catch all programming
2169 * errors, given that the function is called only from our code.
2171 * We have the following 'special' meanings for a few fields:
2172 * * If LVIS_FOCUSED is set, we assume the item has the focus
2173 * This is important in ICON mode, where it might get a larger
2174 * then usual rectangle
2176 * Please note that subitem support works only in REPORT mode.
2179 * [I] infoPtr : valid pointer to the listview structure
2180 * [I] lpLVItem : item to compute the measures for
2181 * [O] lprcBox : ptr to Box rectangle
2182 * Same as LVM_GETITEMRECT with LVIR_BOUNDS
2183 * [0] lprcSelectBox : ptr to select box rectangle
2184 * Same as LVM_GETITEMRECT with LVIR_SELECTEDBOUNDS
2185 * [O] lprcIcon : ptr to Icon rectangle
2186 * Same as LVM_GETITEMRECT with LVIR_ICON
2187 * [O] lprcStateIcon: ptr to State Icon rectangle
2188 * [O] lprcLabel : ptr to Label rectangle
2189 * Same as LVM_GETITEMRECT with LVIR_LABEL
2194 static void LISTVIEW_GetItemMetrics(const LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem,
2195 LPRECT lprcBox, LPRECT lprcSelectBox,
2196 LPRECT lprcIcon, LPRECT lprcStateIcon, LPRECT lprcLabel)
2198 BOOL doSelectBox = FALSE, doIcon = FALSE, doLabel = FALSE, oversizedBox = FALSE;
2199 RECT Box, SelectBox, Icon, Label;
2200 COLUMN_INFO *lpColumnInfo = NULL;
2201 SIZE labelSize = { 0, 0 };
2203 TRACE("(lpLVItem=%s)\n", debuglvitem_t(lpLVItem, TRUE));
2205 /* Be smart and try to figure out the minimum we have to do */
2206 if (lpLVItem->iSubItem) assert(infoPtr->uView == LV_VIEW_DETAILS);
2207 if (infoPtr->uView == LV_VIEW_ICON && (lprcBox || lprcLabel))
2209 assert((lpLVItem->mask & LVIF_STATE) && (lpLVItem->stateMask & LVIS_FOCUSED));
2210 if (lpLVItem->state & LVIS_FOCUSED) oversizedBox = doLabel = TRUE;
2212 if (lprcSelectBox) doSelectBox = TRUE;
2213 if (lprcLabel) doLabel = TRUE;
2214 if (doLabel || lprcIcon || lprcStateIcon) doIcon = TRUE;
2221 /************************************************************/
2222 /* compute the box rectangle (it should be cheap to do) */
2223 /************************************************************/
2224 if (lpLVItem->iSubItem || infoPtr->uView == LV_VIEW_DETAILS)
2225 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, lpLVItem->iSubItem);
2227 if (lpLVItem->iSubItem)
2229 Box = lpColumnInfo->rcHeader;
2234 Box.right = infoPtr->nItemWidth;
2237 Box.bottom = infoPtr->nItemHeight;
2239 /******************************************************************/
2240 /* compute ICON bounding box (ala LVM_GETITEMRECT) and STATEICON */
2241 /******************************************************************/
2244 LONG state_width = 0;
2246 if (infoPtr->himlState && lpLVItem->iSubItem == 0)
2247 state_width = infoPtr->iconStateSize.cx;
2249 if (infoPtr->uView == LV_VIEW_ICON)
2251 Icon.left = Box.left + state_width;
2252 if (infoPtr->himlNormal)
2253 Icon.left += (infoPtr->nItemWidth - infoPtr->iconSize.cx - state_width) / 2;
2254 Icon.top = Box.top + ICON_TOP_PADDING;
2255 Icon.right = Icon.left;
2256 Icon.bottom = Icon.top;
2257 if (infoPtr->himlNormal)
2259 Icon.right += infoPtr->iconSize.cx;
2260 Icon.bottom += infoPtr->iconSize.cy;
2263 else /* LV_VIEW_SMALLICON, LV_VIEW_LIST or LV_VIEW_DETAILS */
2265 Icon.left = Box.left + state_width;
2267 if (infoPtr->uView == LV_VIEW_DETAILS && lpLVItem->iSubItem == 0)
2269 /* we need the indent in report mode */
2270 assert(lpLVItem->mask & LVIF_INDENT);
2271 Icon.left += infoPtr->iconSize.cx * lpLVItem->iIndent + REPORT_MARGINX;
2275 Icon.right = Icon.left;
2276 if (infoPtr->himlSmall &&
2277 (!lpColumnInfo || lpLVItem->iSubItem == 0 || (lpColumnInfo->fmt & LVCFMT_IMAGE) ||
2278 ((infoPtr->dwLvExStyle & LVS_EX_SUBITEMIMAGES) && lpLVItem->iImage != I_IMAGECALLBACK)))
2279 Icon.right += infoPtr->iconSize.cx;
2280 Icon.bottom = Icon.top + infoPtr->iconSize.cy;
2282 if(lprcIcon) *lprcIcon = Icon;
2283 TRACE(" - icon=%s\n", wine_dbgstr_rect(&Icon));
2285 /* TODO: is this correct? */
2288 lprcStateIcon->left = Icon.left - state_width;
2289 lprcStateIcon->right = Icon.left;
2290 lprcStateIcon->top = Icon.top;
2291 lprcStateIcon->bottom = lprcStateIcon->top + infoPtr->iconSize.cy;
2292 TRACE(" - state icon=%s\n", wine_dbgstr_rect(lprcStateIcon));
2295 else Icon.right = 0;
2297 /************************************************************/
2298 /* compute LABEL bounding box (ala LVM_GETITEMRECT) */
2299 /************************************************************/
2302 /* calculate how far to the right can the label stretch */
2303 Label.right = Box.right;
2304 if (infoPtr->uView == LV_VIEW_DETAILS)
2306 if (lpLVItem->iSubItem == 0)
2308 /* we need a zero based rect here */
2309 Label = lpColumnInfo->rcHeader;
2310 OffsetRect(&Label, -Label.left, 0);
2314 if (lpLVItem->iSubItem || ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && infoPtr->uView == LV_VIEW_DETAILS))
2316 labelSize.cx = infoPtr->nItemWidth;
2317 labelSize.cy = infoPtr->nItemHeight;
2321 /* we need the text in non owner draw mode */
2322 assert(lpLVItem->mask & LVIF_TEXT);
2323 if (is_text(lpLVItem->pszText))
2325 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
2326 HDC hdc = GetDC(infoPtr->hwndSelf);
2327 HFONT hOldFont = SelectObject(hdc, hFont);
2331 /* compute rough rectangle where the label will go */
2332 SetRectEmpty(&rcText);
2333 rcText.right = infoPtr->nItemWidth - TRAILING_LABEL_PADDING;
2334 rcText.bottom = infoPtr->nItemHeight;
2335 if (infoPtr->uView == LV_VIEW_ICON)
2336 rcText.bottom -= ICON_TOP_PADDING + infoPtr->iconSize.cy + ICON_BOTTOM_PADDING;
2338 /* now figure out the flags */
2339 if (infoPtr->uView == LV_VIEW_ICON)
2340 uFormat = oversizedBox ? LV_FL_DT_FLAGS : LV_ML_DT_FLAGS;
2342 uFormat = LV_SL_DT_FLAGS;
2344 DrawTextW (hdc, lpLVItem->pszText, -1, &rcText, uFormat | DT_CALCRECT);
2346 if (rcText.right != rcText.left)
2347 labelSize.cx = min(rcText.right - rcText.left + TRAILING_LABEL_PADDING, infoPtr->nItemWidth);
2349 labelSize.cy = rcText.bottom - rcText.top;
2351 SelectObject(hdc, hOldFont);
2352 ReleaseDC(infoPtr->hwndSelf, hdc);
2356 if (infoPtr->uView == LV_VIEW_ICON)
2358 Label.left = Box.left + (infoPtr->nItemWidth - labelSize.cx) / 2;
2359 Label.top = Box.top + ICON_TOP_PADDING_HITABLE +
2360 infoPtr->iconSize.cy + ICON_BOTTOM_PADDING;
2361 Label.right = Label.left + labelSize.cx;
2362 Label.bottom = Label.top + infoPtr->nItemHeight;
2363 if (!oversizedBox && labelSize.cy > infoPtr->ntmHeight)
2365 labelSize.cy = min(Box.bottom - Label.top, labelSize.cy);
2366 labelSize.cy /= infoPtr->ntmHeight;
2367 labelSize.cy = max(labelSize.cy, 1);
2368 labelSize.cy *= infoPtr->ntmHeight;
2370 Label.bottom = Label.top + labelSize.cy + HEIGHT_PADDING;
2372 else if (infoPtr->uView == LV_VIEW_DETAILS)
2374 Label.left = Icon.right;
2375 Label.top = Box.top;
2376 Label.right = lpLVItem->iSubItem ? lpColumnInfo->rcHeader.right :
2377 lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left;
2378 Label.bottom = Label.top + infoPtr->nItemHeight;
2380 else /* LV_VIEW_SMALLICON or LV_VIEW_LIST */
2382 Label.left = Icon.right;
2383 Label.top = Box.top;
2384 Label.right = min(Label.left + labelSize.cx, Label.right);
2385 Label.bottom = Label.top + infoPtr->nItemHeight;
2388 if (lprcLabel) *lprcLabel = Label;
2389 TRACE(" - label=%s\n", wine_dbgstr_rect(&Label));
2392 /************************************************************/
2393 /* compute SELECT bounding box */
2394 /************************************************************/
2397 if (infoPtr->uView == LV_VIEW_DETAILS)
2399 SelectBox.left = Icon.left;
2400 SelectBox.top = Box.top;
2401 SelectBox.bottom = Box.bottom;
2404 SelectBox.right = min(Label.left + labelSize.cx, Label.right);
2406 SelectBox.right = min(Label.left + MAX_EMPTYTEXT_SELECT_WIDTH, Label.right);
2410 UnionRect(&SelectBox, &Icon, &Label);
2412 if (lprcSelectBox) *lprcSelectBox = SelectBox;
2413 TRACE(" - select box=%s\n", wine_dbgstr_rect(&SelectBox));
2416 /* Fix the Box if necessary */
2419 if (oversizedBox) UnionRect(lprcBox, &Box, &Label);
2420 else *lprcBox = Box;
2422 TRACE(" - box=%s\n", wine_dbgstr_rect(&Box));
2426 * DESCRIPTION: [INTERNAL]
2429 * [I] infoPtr : valid pointer to the listview structure
2430 * [I] nItem : item number
2431 * [O] lprcBox : ptr to Box rectangle
2436 static void LISTVIEW_GetItemBox(const LISTVIEW_INFO *infoPtr, INT nItem, LPRECT lprcBox)
2438 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
2439 POINT Position, Origin;
2442 LISTVIEW_GetOrigin(infoPtr, &Origin);
2443 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
2445 /* Be smart and try to figure out the minimum we have to do */
2447 if (infoPtr->uView == LV_VIEW_ICON && infoPtr->bFocus && LISTVIEW_GetItemState(infoPtr, nItem, LVIS_FOCUSED))
2448 lvItem.mask |= LVIF_TEXT;
2449 lvItem.iItem = nItem;
2450 lvItem.iSubItem = 0;
2451 lvItem.pszText = szDispText;
2452 lvItem.cchTextMax = DISP_TEXT_SIZE;
2453 if (lvItem.mask) LISTVIEW_GetItemW(infoPtr, &lvItem);
2454 if (infoPtr->uView == LV_VIEW_ICON)
2456 lvItem.mask |= LVIF_STATE;
2457 lvItem.stateMask = LVIS_FOCUSED;
2458 lvItem.state = (lvItem.mask & LVIF_TEXT ? LVIS_FOCUSED : 0);
2460 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, lprcBox, 0, 0, 0, 0);
2462 if (infoPtr->uView == LV_VIEW_DETAILS && infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT &&
2463 SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, 0, 0))
2465 OffsetRect(lprcBox, Origin.x, Position.y + Origin.y);
2468 OffsetRect(lprcBox, Position.x + Origin.x, Position.y + Origin.y);
2471 /* LISTVIEW_MapIdToIndex helper */
2472 static INT CALLBACK MapIdSearchCompare(LPVOID p1, LPVOID p2, LPARAM lParam)
2474 ITEM_ID *id1 = (ITEM_ID*)p1;
2475 ITEM_ID *id2 = (ITEM_ID*)p2;
2477 if (id1->id == id2->id) return 0;
2479 return (id1->id < id2->id) ? -1 : 1;
2484 * Returns the item index for id specified.
2487 * [I] infoPtr : valid pointer to the listview structure
2488 * [I] iID : item id to get index for
2491 * Item index, or -1 on failure.
2493 static INT LISTVIEW_MapIdToIndex(const LISTVIEW_INFO *infoPtr, UINT iID)
2498 TRACE("iID=%d\n", iID);
2500 if (infoPtr->dwStyle & LVS_OWNERDATA) return -1;
2501 if (infoPtr->nItemCount == 0) return -1;
2504 index = DPA_Search(infoPtr->hdpaItemIds, &ID, -1, &MapIdSearchCompare, 0, DPAS_SORTED);
2508 ITEM_ID *lpID = DPA_GetPtr(infoPtr->hdpaItemIds, index);
2509 return DPA_GetPtrIndex(infoPtr->hdpaItems, lpID->item);
2517 * Returns the item id for index given.
2520 * [I] infoPtr : valid pointer to the listview structure
2521 * [I] iItem : item index to get id for
2526 static DWORD LISTVIEW_MapIndexToId(const LISTVIEW_INFO *infoPtr, INT iItem)
2531 TRACE("iItem=%d\n", iItem);
2533 if (infoPtr->dwStyle & LVS_OWNERDATA) return -1;
2534 if (iItem < 0 || iItem >= infoPtr->nItemCount) return -1;
2536 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, iItem);
2537 lpItem = DPA_GetPtr(hdpaSubItems, 0);
2539 return lpItem->id->id;
2544 * Returns the current icon position, and advances it along the top.
2545 * The returned position is not offset by Origin.
2548 * [I] infoPtr : valid pointer to the listview structure
2549 * [O] lpPos : will get the current icon position
2554 static void LISTVIEW_NextIconPosTop(LISTVIEW_INFO *infoPtr, LPPOINT lpPos)
2556 INT nListWidth = infoPtr->rcList.right - infoPtr->rcList.left;
2558 *lpPos = infoPtr->currIconPos;
2560 infoPtr->currIconPos.x += infoPtr->nItemWidth;
2561 if (infoPtr->currIconPos.x + infoPtr->nItemWidth <= nListWidth) return;
2563 infoPtr->currIconPos.x = 0;
2564 infoPtr->currIconPos.y += infoPtr->nItemHeight;
2570 * Returns the current icon position, and advances it down the left edge.
2571 * The returned position is not offset by Origin.
2574 * [I] infoPtr : valid pointer to the listview structure
2575 * [O] lpPos : will get the current icon position
2580 static void LISTVIEW_NextIconPosLeft(LISTVIEW_INFO *infoPtr, LPPOINT lpPos)
2582 INT nListHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
2584 *lpPos = infoPtr->currIconPos;
2586 infoPtr->currIconPos.y += infoPtr->nItemHeight;
2587 if (infoPtr->currIconPos.y + infoPtr->nItemHeight <= nListHeight) return;
2589 infoPtr->currIconPos.x += infoPtr->nItemWidth;
2590 infoPtr->currIconPos.y = 0;
2596 * Moves an icon to the specified position.
2597 * It takes care of invalidating the item, etc.
2600 * [I] infoPtr : valid pointer to the listview structure
2601 * [I] nItem : the item to move
2602 * [I] lpPos : the new icon position
2603 * [I] isNew : flags the item as being new
2609 static BOOL LISTVIEW_MoveIconTo(const LISTVIEW_INFO *infoPtr, INT nItem, const POINT *lppt, BOOL isNew)
2615 old.x = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosX, nItem);
2616 old.y = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosY, nItem);
2618 if (lppt->x == old.x && lppt->y == old.y) return TRUE;
2619 LISTVIEW_InvalidateItem(infoPtr, nItem);
2622 /* Allocating a POINTER for every item is too resource intensive,
2623 * so we'll keep the (x,y) in different arrays */
2624 if (!DPA_SetPtr(infoPtr->hdpaPosX, nItem, (void *)(LONG_PTR)lppt->x)) return FALSE;
2625 if (!DPA_SetPtr(infoPtr->hdpaPosY, nItem, (void *)(LONG_PTR)lppt->y)) return FALSE;
2627 LISTVIEW_InvalidateItem(infoPtr, nItem);
2634 * Arranges listview items in icon display mode.
2637 * [I] infoPtr : valid pointer to the listview structure
2638 * [I] nAlignCode : alignment code
2644 static BOOL LISTVIEW_Arrange(LISTVIEW_INFO *infoPtr, INT nAlignCode)
2646 void (*next_pos)(LISTVIEW_INFO *, LPPOINT);
2650 if (infoPtr->uView != LV_VIEW_ICON && infoPtr->uView != LV_VIEW_SMALLICON) return FALSE;
2652 TRACE("nAlignCode=%d\n", nAlignCode);
2654 if (nAlignCode == LVA_DEFAULT)
2656 if (infoPtr->dwStyle & LVS_ALIGNLEFT) nAlignCode = LVA_ALIGNLEFT;
2657 else nAlignCode = LVA_ALIGNTOP;
2662 case LVA_ALIGNLEFT: next_pos = LISTVIEW_NextIconPosLeft; break;
2663 case LVA_ALIGNTOP: next_pos = LISTVIEW_NextIconPosTop; break;
2664 case LVA_SNAPTOGRID: next_pos = LISTVIEW_NextIconPosTop; break; /* FIXME */
2665 default: return FALSE;
2668 infoPtr->bAutoarrange = TRUE;
2669 infoPtr->currIconPos.x = infoPtr->currIconPos.y = 0;
2670 for (i = 0; i < infoPtr->nItemCount; i++)
2672 next_pos(infoPtr, &pos);
2673 LISTVIEW_MoveIconTo(infoPtr, i, &pos, FALSE);
2681 * Retrieves the bounding rectangle of all the items, not offset by Origin.
2682 * For LVS_REPORT always returns empty rectangle.
2685 * [I] infoPtr : valid pointer to the listview structure
2686 * [O] lprcView : bounding rectangle
2692 static void LISTVIEW_GetAreaRect(const LISTVIEW_INFO *infoPtr, LPRECT lprcView)
2696 SetRectEmpty(lprcView);
2698 switch (infoPtr->uView)
2701 case LV_VIEW_SMALLICON:
2702 for (i = 0; i < infoPtr->nItemCount; i++)
2704 x = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosX, i);
2705 y = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosY, i);
2706 lprcView->right = max(lprcView->right, x);
2707 lprcView->bottom = max(lprcView->bottom, y);
2709 if (infoPtr->nItemCount > 0)
2711 lprcView->right += infoPtr->nItemWidth;
2712 lprcView->bottom += infoPtr->nItemHeight;
2717 y = LISTVIEW_GetCountPerColumn(infoPtr);
2718 x = infoPtr->nItemCount / y;
2719 if (infoPtr->nItemCount % y) x++;
2720 lprcView->right = x * infoPtr->nItemWidth;
2721 lprcView->bottom = y * infoPtr->nItemHeight;
2728 * Retrieves the bounding rectangle of all the items.
2731 * [I] infoPtr : valid pointer to the listview structure
2732 * [O] lprcView : bounding rectangle
2738 static BOOL LISTVIEW_GetViewRect(const LISTVIEW_INFO *infoPtr, LPRECT lprcView)
2742 TRACE("(lprcView=%p)\n", lprcView);
2744 if (!lprcView) return FALSE;
2746 LISTVIEW_GetAreaRect(infoPtr, lprcView);
2748 if (infoPtr->uView != LV_VIEW_DETAILS)
2750 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
2751 OffsetRect(lprcView, ptOrigin.x, ptOrigin.y);
2754 TRACE("lprcView=%s\n", wine_dbgstr_rect(lprcView));
2761 * Retrieves the subitem pointer associated with the subitem index.
2764 * [I] hdpaSubItems : DPA handle for a specific item
2765 * [I] nSubItem : index of subitem
2768 * SUCCESS : subitem pointer
2771 static SUBITEM_INFO* LISTVIEW_GetSubItemPtr(HDPA hdpaSubItems, INT nSubItem)
2773 SUBITEM_INFO *lpSubItem;
2776 /* we should binary search here if need be */
2777 for (i = 1; i < DPA_GetPtrCount(hdpaSubItems); i++)
2779 lpSubItem = DPA_GetPtr(hdpaSubItems, i);
2780 if (lpSubItem->iSubItem == nSubItem)
2790 * Calculates the desired item width.
2793 * [I] infoPtr : valid pointer to the listview structure
2796 * The desired item width.
2798 static INT LISTVIEW_CalculateItemWidth(const LISTVIEW_INFO *infoPtr)
2802 TRACE("uView=%d\n", infoPtr->uView);
2804 if (infoPtr->uView == LV_VIEW_ICON)
2805 nItemWidth = infoPtr->iconSpacing.cx;
2806 else if (infoPtr->uView == LV_VIEW_DETAILS)
2808 if (DPA_GetPtrCount(infoPtr->hdpaColumns) > 0)
2813 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX,
2814 DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, 0);
2816 LISTVIEW_GetHeaderRect(infoPtr, index, &rcHeader);
2817 nItemWidth = rcHeader.right;
2820 else /* LV_VIEW_SMALLICON, or LV_VIEW_LIST */
2822 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
2826 lvItem.mask = LVIF_TEXT;
2827 lvItem.iSubItem = 0;
2829 for (i = 0; i < infoPtr->nItemCount; i++)
2832 lvItem.pszText = szDispText;
2833 lvItem.cchTextMax = DISP_TEXT_SIZE;
2834 if (LISTVIEW_GetItemW(infoPtr, &lvItem))
2835 nItemWidth = max(LISTVIEW_GetStringWidthT(infoPtr, lvItem.pszText, TRUE),
2839 if (infoPtr->himlSmall) nItemWidth += infoPtr->iconSize.cx;
2840 if (infoPtr->himlState) nItemWidth += infoPtr->iconStateSize.cx;
2842 nItemWidth = max(DEFAULT_COLUMN_WIDTH, nItemWidth + WIDTH_PADDING);
2850 * Calculates the desired item height.
2853 * [I] infoPtr : valid pointer to the listview structure
2856 * The desired item height.
2858 static INT LISTVIEW_CalculateItemHeight(const LISTVIEW_INFO *infoPtr)
2862 TRACE("uView=%d\n", infoPtr->uView);
2864 if (infoPtr->uView == LV_VIEW_ICON)
2865 nItemHeight = infoPtr->iconSpacing.cy;
2868 nItemHeight = infoPtr->ntmHeight;
2869 if (infoPtr->uView == LV_VIEW_DETAILS && infoPtr->dwLvExStyle & LVS_EX_GRIDLINES)
2871 if (infoPtr->himlState)
2872 nItemHeight = max(nItemHeight, infoPtr->iconStateSize.cy);
2873 if (infoPtr->himlSmall)
2874 nItemHeight = max(nItemHeight, infoPtr->iconSize.cy);
2875 if (infoPtr->himlState || infoPtr->himlSmall)
2876 nItemHeight += HEIGHT_PADDING;
2877 if (infoPtr->nMeasureItemHeight > 0)
2878 nItemHeight = infoPtr->nMeasureItemHeight;
2881 return max(nItemHeight, 1);
2886 * Updates the width, and height of an item.
2889 * [I] infoPtr : valid pointer to the listview structure
2894 static inline void LISTVIEW_UpdateItemSize(LISTVIEW_INFO *infoPtr)
2896 infoPtr->nItemWidth = LISTVIEW_CalculateItemWidth(infoPtr);
2897 infoPtr->nItemHeight = LISTVIEW_CalculateItemHeight(infoPtr);
2903 * Retrieves and saves important text metrics info for the current
2907 * [I] infoPtr : valid pointer to the listview structure
2910 static void LISTVIEW_SaveTextMetrics(LISTVIEW_INFO *infoPtr)
2912 HDC hdc = GetDC(infoPtr->hwndSelf);
2913 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
2914 HFONT hOldFont = SelectObject(hdc, hFont);
2918 if (GetTextMetricsW(hdc, &tm))
2920 infoPtr->ntmHeight = tm.tmHeight;
2921 infoPtr->ntmMaxCharWidth = tm.tmMaxCharWidth;
2924 if (GetTextExtentPoint32A(hdc, "...", 3, &sz))
2925 infoPtr->nEllipsisWidth = sz.cx;
2927 SelectObject(hdc, hOldFont);
2928 ReleaseDC(infoPtr->hwndSelf, hdc);
2930 TRACE("tmHeight=%d\n", infoPtr->ntmHeight);
2935 * A compare function for ranges
2938 * [I] range1 : pointer to range 1;
2939 * [I] range2 : pointer to range 2;
2943 * > 0 : if range 1 > range 2
2944 * < 0 : if range 2 > range 1
2945 * = 0 : if range intersects range 2
2947 static INT CALLBACK ranges_cmp(LPVOID range1, LPVOID range2, LPARAM flags)
2951 if (((RANGE*)range1)->upper <= ((RANGE*)range2)->lower)
2953 else if (((RANGE*)range2)->upper <= ((RANGE*)range1)->lower)
2958 TRACE("range1=%s, range2=%s, cmp=%d\n", debugrange(range1), debugrange(range2), cmp);
2963 #define ranges_check(ranges, desc) if (TRACE_ON(listview)) ranges_assert(ranges, desc, __FUNCTION__, __LINE__)
2965 static void ranges_assert(RANGES ranges, LPCSTR desc, const char *func, int line)
2970 TRACE("*** Checking %s:%d:%s ***\n", func, line, desc);
2972 assert (DPA_GetPtrCount(ranges->hdpa) >= 0);
2973 ranges_dump(ranges);
2974 if (DPA_GetPtrCount(ranges->hdpa) > 0)
2976 prev = DPA_GetPtr(ranges->hdpa, 0);
2977 assert (prev->lower >= 0 && prev->lower < prev->upper);
2978 for (i = 1; i < DPA_GetPtrCount(ranges->hdpa); i++)
2980 curr = DPA_GetPtr(ranges->hdpa, i);
2981 assert (prev->upper <= curr->lower);
2982 assert (curr->lower < curr->upper);
2986 TRACE("--- Done checking---\n");
2989 static RANGES ranges_create(int count)
2991 RANGES ranges = Alloc(sizeof(struct tagRANGES));
2992 if (!ranges) return NULL;
2993 ranges->hdpa = DPA_Create(count);
2994 if (ranges->hdpa) return ranges;
2999 static void ranges_clear(RANGES ranges)
3003 for(i = 0; i < DPA_GetPtrCount(ranges->hdpa); i++)
3004 Free(DPA_GetPtr(ranges->hdpa, i));
3005 DPA_DeleteAllPtrs(ranges->hdpa);
3009 static void ranges_destroy(RANGES ranges)
3011 if (!ranges) return;
3012 ranges_clear(ranges);
3013 DPA_Destroy(ranges->hdpa);
3017 static RANGES ranges_clone(RANGES ranges)
3022 if (!(clone = ranges_create(DPA_GetPtrCount(ranges->hdpa)))) goto fail;
3024 for (i = 0; i < DPA_GetPtrCount(ranges->hdpa); i++)
3026 RANGE *newrng = Alloc(sizeof(RANGE));
3027 if (!newrng) goto fail;
3028 *newrng = *((RANGE*)DPA_GetPtr(ranges->hdpa, i));
3029 DPA_SetPtr(clone->hdpa, i, newrng);
3034 TRACE ("clone failed\n");
3035 ranges_destroy(clone);
3039 static RANGES ranges_diff(RANGES ranges, RANGES sub)
3043 for (i = 0; i < DPA_GetPtrCount(sub->hdpa); i++)
3044 ranges_del(ranges, *((RANGE *)DPA_GetPtr(sub->hdpa, i)));
3049 static void ranges_dump(RANGES ranges)
3053 for (i = 0; i < DPA_GetPtrCount(ranges->hdpa); i++)
3054 TRACE(" %s\n", debugrange(DPA_GetPtr(ranges->hdpa, i)));
3057 static inline BOOL ranges_contain(RANGES ranges, INT nItem)
3059 RANGE srchrng = { nItem, nItem + 1 };
3061 TRACE("(nItem=%d)\n", nItem);
3062 ranges_check(ranges, "before contain");
3063 return DPA_Search(ranges->hdpa, &srchrng, 0, ranges_cmp, 0, DPAS_SORTED) != -1;
3066 static INT ranges_itemcount(RANGES ranges)
3070 for (i = 0; i < DPA_GetPtrCount(ranges->hdpa); i++)
3072 RANGE *sel = DPA_GetPtr(ranges->hdpa, i);
3073 count += sel->upper - sel->lower;
3079 static BOOL ranges_shift(RANGES ranges, INT nItem, INT delta, INT nUpper)
3081 RANGE srchrng = { nItem, nItem + 1 }, *chkrng;
3084 index = DPA_Search(ranges->hdpa, &srchrng, 0, ranges_cmp, 0, DPAS_SORTED | DPAS_INSERTAFTER);
3085 if (index == -1) return TRUE;
3087 for (; index < DPA_GetPtrCount(ranges->hdpa); index++)
3089 chkrng = DPA_GetPtr(ranges->hdpa, index);
3090 if (chkrng->lower >= nItem)
3091 chkrng->lower = max(min(chkrng->lower + delta, nUpper - 1), 0);
3092 if (chkrng->upper > nItem)
3093 chkrng->upper = max(min(chkrng->upper + delta, nUpper), 0);
3098 static BOOL ranges_add(RANGES ranges, RANGE range)
3103 TRACE("(%s)\n", debugrange(&range));
3104 ranges_check(ranges, "before add");
3106 /* try find overlapping regions first */
3107 srchrgn.lower = range.lower - 1;
3108 srchrgn.upper = range.upper + 1;
3109 index = DPA_Search(ranges->hdpa, &srchrgn, 0, ranges_cmp, 0, DPAS_SORTED);
3115 TRACE("Adding new range\n");
3117 /* create the brand new range to insert */
3118 newrgn = Alloc(sizeof(RANGE));
3119 if(!newrgn) goto fail;
3122 /* figure out where to insert it */
3123 index = DPA_Search(ranges->hdpa, newrgn, 0, ranges_cmp, 0, DPAS_SORTED | DPAS_INSERTAFTER);
3124 TRACE("index=%d\n", index);
3125 if (index == -1) index = 0;
3127 /* and get it over with */
3128 if (DPA_InsertPtr(ranges->hdpa, index, newrgn) == -1)
3136 RANGE *chkrgn, *mrgrgn;
3137 INT fromindex, mergeindex;
3139 chkrgn = DPA_GetPtr(ranges->hdpa, index);
3140 TRACE("Merge with %s @%d\n", debugrange(chkrgn), index);
3142 chkrgn->lower = min(range.lower, chkrgn->lower);
3143 chkrgn->upper = max(range.upper, chkrgn->upper);
3145 TRACE("New range %s @%d\n", debugrange(chkrgn), index);
3147 /* merge now common ranges */
3149 srchrgn.lower = chkrgn->lower - 1;
3150 srchrgn.upper = chkrgn->upper + 1;
3154 mergeindex = DPA_Search(ranges->hdpa, &srchrgn, fromindex, ranges_cmp, 0, 0);
3155 if (mergeindex == -1) break;
3156 if (mergeindex == index)
3158 fromindex = index + 1;
3162 TRACE("Merge with index %i\n", mergeindex);
3164 mrgrgn = DPA_GetPtr(ranges->hdpa, mergeindex);
3165 chkrgn->lower = min(chkrgn->lower, mrgrgn->lower);
3166 chkrgn->upper = max(chkrgn->upper, mrgrgn->upper);
3168 DPA_DeletePtr(ranges->hdpa, mergeindex);
3169 if (mergeindex < index) index --;
3173 ranges_check(ranges, "after add");
3177 ranges_check(ranges, "failed add");
3181 static BOOL ranges_del(RANGES ranges, RANGE range)
3186 TRACE("(%s)\n", debugrange(&range));
3187 ranges_check(ranges, "before del");
3189 /* we don't use DPAS_SORTED here, since we need *
3190 * to find the first overlapping range */
3191 index = DPA_Search(ranges->hdpa, &range, 0, ranges_cmp, 0, 0);
3194 chkrgn = DPA_GetPtr(ranges->hdpa, index);
3196 TRACE("Matches range %s @%d\n", debugrange(chkrgn), index);
3198 /* case 1: Same range */
3199 if ( (chkrgn->upper == range.upper) &&
3200 (chkrgn->lower == range.lower) )
3202 DPA_DeletePtr(ranges->hdpa, index);
3206 /* case 2: engulf */
3207 else if ( (chkrgn->upper <= range.upper) &&
3208 (chkrgn->lower >= range.lower) )
3210 DPA_DeletePtr(ranges->hdpa, index);
3213 /* case 3: overlap upper */
3214 else if ( (chkrgn->upper <= range.upper) &&
3215 (chkrgn->lower < range.lower) )
3217 chkrgn->upper = range.lower;
3219 /* case 4: overlap lower */
3220 else if ( (chkrgn->upper > range.upper) &&
3221 (chkrgn->lower >= range.lower) )
3223 chkrgn->lower = range.upper;
3226 /* case 5: fully internal */
3229 RANGE tmprgn = *chkrgn, *newrgn;
3231 if (!(newrgn = Alloc(sizeof(RANGE)))) goto fail;
3232 newrgn->lower = chkrgn->lower;
3233 newrgn->upper = range.lower;
3234 chkrgn->lower = range.upper;
3235 if (DPA_InsertPtr(ranges->hdpa, index, newrgn) == -1)
3244 index = DPA_Search(ranges->hdpa, &range, index, ranges_cmp, 0, 0);
3247 ranges_check(ranges, "after del");
3251 ranges_check(ranges, "failed del");
3257 * Removes all selection ranges
3260 * [I] infoPtr : valid pointer to the listview structure
3261 * [I] toSkip : item range to skip removing the selection
3267 static BOOL LISTVIEW_DeselectAllSkipItems(LISTVIEW_INFO *infoPtr, RANGES toSkip)
3276 lvItem.stateMask = LVIS_SELECTED;
3278 /* need to clone the DPA because callbacks can change it */
3279 if (!(clone = ranges_clone(infoPtr->selectionRanges))) return FALSE;
3280 iterator_rangesitems(&i, ranges_diff(clone, toSkip));
3281 while(iterator_next(&i))
3282 LISTVIEW_SetItemState(infoPtr, i.nItem, &lvItem);
3283 /* note that the iterator destructor will free the cloned range */
3284 iterator_destroy(&i);
3289 static inline BOOL LISTVIEW_DeselectAllSkipItem(LISTVIEW_INFO *infoPtr, INT nItem)
3293 if (!(toSkip = ranges_create(1))) return FALSE;
3294 if (nItem != -1) ranges_additem(toSkip, nItem);
3295 LISTVIEW_DeselectAllSkipItems(infoPtr, toSkip);
3296 ranges_destroy(toSkip);
3300 static inline BOOL LISTVIEW_DeselectAll(LISTVIEW_INFO *infoPtr)
3302 return LISTVIEW_DeselectAllSkipItem(infoPtr, -1);
3307 * Retrieves the number of items that are marked as selected.
3310 * [I] infoPtr : valid pointer to the listview structure
3313 * Number of items selected.
3315 static INT LISTVIEW_GetSelectedCount(const LISTVIEW_INFO *infoPtr)
3317 INT nSelectedCount = 0;
3319 if (infoPtr->uCallbackMask & LVIS_SELECTED)
3322 for (i = 0; i < infoPtr->nItemCount; i++)
3324 if (LISTVIEW_GetItemState(infoPtr, i, LVIS_SELECTED))
3329 nSelectedCount = ranges_itemcount(infoPtr->selectionRanges);
3331 TRACE("nSelectedCount=%d\n", nSelectedCount);
3332 return nSelectedCount;
3337 * Manages the item focus.
3340 * [I] infoPtr : valid pointer to the listview structure
3341 * [I] nItem : item index
3344 * TRUE : focused item changed
3345 * FALSE : focused item has NOT changed
3347 static inline BOOL LISTVIEW_SetItemFocus(LISTVIEW_INFO *infoPtr, INT nItem)
3349 INT oldFocus = infoPtr->nFocusedItem;
3352 if (nItem == infoPtr->nFocusedItem) return FALSE;
3354 lvItem.state = nItem == -1 ? 0 : LVIS_FOCUSED;
3355 lvItem.stateMask = LVIS_FOCUSED;
3356 LISTVIEW_SetItemState(infoPtr, nItem == -1 ? infoPtr->nFocusedItem : nItem, &lvItem);
3358 return oldFocus != infoPtr->nFocusedItem;
3361 /* Helper function for LISTVIEW_ShiftIndices *only* */
3362 static INT shift_item(const LISTVIEW_INFO *infoPtr, INT nShiftItem, INT nItem, INT direction)
3364 if (nShiftItem < nItem) return nShiftItem;
3366 if (nShiftItem > nItem) return nShiftItem + direction;
3368 if (direction > 0) return nShiftItem + direction;
3370 return min(nShiftItem, infoPtr->nItemCount - 1);
3375 * Updates the various indices after an item has been inserted or deleted.
3378 * [I] infoPtr : valid pointer to the listview structure
3379 * [I] nItem : item index
3380 * [I] direction : Direction of shift, +1 or -1.
3385 static void LISTVIEW_ShiftIndices(LISTVIEW_INFO *infoPtr, INT nItem, INT direction)
3390 /* temporarily disable change notification while shifting items */
3391 bOldChange = infoPtr->bDoChangeNotify;
3392 infoPtr->bDoChangeNotify = FALSE;
3394 TRACE("Shifting %iu, %i steps\n", nItem, direction);
3396 ranges_shift(infoPtr->selectionRanges, nItem, direction, infoPtr->nItemCount);
3398 assert(abs(direction) == 1);
3400 infoPtr->nSelectionMark = shift_item(infoPtr, infoPtr->nSelectionMark, nItem, direction);
3402 nNewFocus = shift_item(infoPtr, infoPtr->nFocusedItem, nItem, direction);
3403 if (nNewFocus != infoPtr->nFocusedItem)
3404 LISTVIEW_SetItemFocus(infoPtr, nNewFocus);
3406 /* But we are not supposed to modify nHotItem! */
3408 infoPtr->bDoChangeNotify = bOldChange;
3414 * Adds a block of selections.
3417 * [I] infoPtr : valid pointer to the listview structure
3418 * [I] nItem : item index
3421 * Whether the window is still valid.
3423 static BOOL LISTVIEW_AddGroupSelection(LISTVIEW_INFO *infoPtr, INT nItem)
3425 INT nFirst = min(infoPtr->nSelectionMark, nItem);
3426 INT nLast = max(infoPtr->nSelectionMark, nItem);
3427 HWND hwndSelf = infoPtr->hwndSelf;
3428 NMLVODSTATECHANGE nmlv;
3433 /* Temporarily disable change notification
3434 * If the control is LVS_OWNERDATA, we need to send
3435 * only one LVN_ODSTATECHANGED notification.
3436 * See MSDN documentation for LVN_ITEMCHANGED.
3438 bOldChange = infoPtr->bDoChangeNotify;
3439 if (infoPtr->dwStyle & LVS_OWNERDATA) infoPtr->bDoChangeNotify = FALSE;
3441 if (nFirst == -1) nFirst = nItem;
3443 item.state = LVIS_SELECTED;
3444 item.stateMask = LVIS_SELECTED;
3446 for (i = nFirst; i <= nLast; i++)
3447 LISTVIEW_SetItemState(infoPtr,i,&item);
3449 ZeroMemory(&nmlv, sizeof(nmlv));
3450 nmlv.iFrom = nFirst;
3453 nmlv.uOldState = item.state;
3455 notify_hdr(infoPtr, LVN_ODSTATECHANGED, (LPNMHDR)&nmlv);
3456 if (!IsWindow(hwndSelf))
3458 infoPtr->bDoChangeNotify = bOldChange;
3465 * Sets a single group selection.
3468 * [I] infoPtr : valid pointer to the listview structure
3469 * [I] nItem : item index
3474 static void LISTVIEW_SetGroupSelection(LISTVIEW_INFO *infoPtr, INT nItem)
3481 if (!(selection = ranges_create(100))) return;
3483 item.state = LVIS_SELECTED;
3484 item.stateMask = LVIS_SELECTED;
3486 if ((infoPtr->uView == LV_VIEW_LIST) || (infoPtr->uView == LV_VIEW_DETAILS))
3488 if (infoPtr->nSelectionMark == -1)
3490 infoPtr->nSelectionMark = nItem;
3491 ranges_additem(selection, nItem);
3497 sel.lower = min(infoPtr->nSelectionMark, nItem);
3498 sel.upper = max(infoPtr->nSelectionMark, nItem) + 1;
3499 ranges_add(selection, sel);
3504 RECT rcItem, rcSel, rcSelMark;
3507 rcItem.left = LVIR_BOUNDS;
3508 if (!LISTVIEW_GetItemRect(infoPtr, nItem, &rcItem)) return;
3509 rcSelMark.left = LVIR_BOUNDS;
3510 if (!LISTVIEW_GetItemRect(infoPtr, infoPtr->nSelectionMark, &rcSelMark)) return;
3511 UnionRect(&rcSel, &rcItem, &rcSelMark);
3512 iterator_frameditems(&i, infoPtr, &rcSel);
3513 while(iterator_next(&i))
3515 LISTVIEW_GetItemPosition(infoPtr, i.nItem, &ptItem);
3516 if (PtInRect(&rcSel, ptItem)) ranges_additem(selection, i.nItem);
3518 iterator_destroy(&i);
3521 /* disable per item notifications on LVS_OWNERDATA style
3522 FIXME: single LVN_ODSTATECHANGED should be used */
3523 bOldChange = infoPtr->bDoChangeNotify;
3524 if (infoPtr->dwStyle & LVS_OWNERDATA) infoPtr->bDoChangeNotify = FALSE;
3526 LISTVIEW_DeselectAllSkipItems(infoPtr, selection);
3529 iterator_rangesitems(&i, selection);
3530 while(iterator_next(&i))
3531 LISTVIEW_SetItemState(infoPtr, i.nItem, &item);
3532 /* this will also destroy the selection */
3533 iterator_destroy(&i);
3535 infoPtr->bDoChangeNotify = bOldChange;
3537 LISTVIEW_SetItemFocus(infoPtr, nItem);
3542 * Sets a single selection.
3545 * [I] infoPtr : valid pointer to the listview structure
3546 * [I] nItem : item index
3551 static void LISTVIEW_SetSelection(LISTVIEW_INFO *infoPtr, INT nItem)
3555 TRACE("nItem=%d\n", nItem);
3557 LISTVIEW_DeselectAllSkipItem(infoPtr, nItem);
3559 lvItem.state = LVIS_FOCUSED | LVIS_SELECTED;
3560 lvItem.stateMask = LVIS_FOCUSED | LVIS_SELECTED;
3561 LISTVIEW_SetItemState(infoPtr, nItem, &lvItem);
3563 infoPtr->nSelectionMark = nItem;
3568 * Set selection(s) with keyboard.
3571 * [I] infoPtr : valid pointer to the listview structure
3572 * [I] nItem : item index
3573 * [I] space : VK_SPACE code sent
3576 * SUCCESS : TRUE (needs to be repainted)
3577 * FAILURE : FALSE (nothing has changed)
3579 static BOOL LISTVIEW_KeySelection(LISTVIEW_INFO *infoPtr, INT nItem, BOOL space)
3581 /* FIXME: pass in the state */
3582 WORD wShift = HIWORD(GetKeyState(VK_SHIFT));
3583 WORD wCtrl = HIWORD(GetKeyState(VK_CONTROL));
3584 BOOL bResult = FALSE;
3586 TRACE("nItem=%d, wShift=%d, wCtrl=%d\n", nItem, wShift, wCtrl);
3587 if ((nItem >= 0) && (nItem < infoPtr->nItemCount))
3591 if (infoPtr->dwStyle & LVS_SINGLESEL || (wShift == 0 && wCtrl == 0))
3592 LISTVIEW_SetSelection(infoPtr, nItem);
3596 LISTVIEW_SetGroupSelection(infoPtr, nItem);
3600 lvItem.state = ~LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED);
3601 lvItem.stateMask = LVIS_SELECTED;
3604 LISTVIEW_SetItemState(infoPtr, nItem, &lvItem);
3605 if (lvItem.state & LVIS_SELECTED)
3606 infoPtr->nSelectionMark = nItem;
3608 bResult = LISTVIEW_SetItemFocus(infoPtr, nItem);
3611 LISTVIEW_EnsureVisible(infoPtr, nItem, FALSE);
3614 UpdateWindow(infoPtr->hwndSelf); /* update client area */
3618 static BOOL LISTVIEW_GetItemAtPt(const LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem, POINT pt)
3620 LVHITTESTINFO lvHitTestInfo;
3622 ZeroMemory(&lvHitTestInfo, sizeof(lvHitTestInfo));
3623 lvHitTestInfo.pt.x = pt.x;
3624 lvHitTestInfo.pt.y = pt.y;
3626 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
3628 lpLVItem->mask = LVIF_PARAM;
3629 lpLVItem->iItem = lvHitTestInfo.iItem;
3630 lpLVItem->iSubItem = 0;
3632 return LISTVIEW_GetItemT(infoPtr, lpLVItem, TRUE);
3635 static inline BOOL LISTVIEW_IsHotTracking(const LISTVIEW_INFO *infoPtr)
3637 return ((infoPtr->dwLvExStyle & LVS_EX_TRACKSELECT) ||
3638 (infoPtr->dwLvExStyle & LVS_EX_ONECLICKACTIVATE) ||
3639 (infoPtr->dwLvExStyle & LVS_EX_TWOCLICKACTIVATE));
3644 * Called when the mouse is being actively tracked and has hovered for a specified
3648 * [I] infoPtr : valid pointer to the listview structure
3649 * [I] fwKeys : key indicator
3650 * [I] x,y : mouse position
3653 * 0 if the message was processed, non-zero if there was an error
3656 * LVS_EX_TRACKSELECT: An item is automatically selected when the cursor remains
3657 * over the item for a certain period of time.
3660 static LRESULT LISTVIEW_MouseHover(LISTVIEW_INFO *infoPtr, INT x, INT y)
3664 if (notify_hdr(infoPtr, NM_HOVER, &hdr)) return 0;
3666 if (LISTVIEW_IsHotTracking(infoPtr))
3674 if (LISTVIEW_GetItemAtPt(infoPtr, &item, pt))
3675 LISTVIEW_SetSelection(infoPtr, item.iItem);
3677 SetFocus(infoPtr->hwndSelf);
3683 #define SCROLL_LEFT 0x1
3684 #define SCROLL_RIGHT 0x2
3685 #define SCROLL_UP 0x4
3686 #define SCROLL_DOWN 0x8
3690 * Utility routine to draw and highlight items within a marquee selection rectangle.
3693 * [I] infoPtr : valid pointer to the listview structure
3694 * [I] coords_orig : original co-ordinates of the cursor
3695 * [I] coords_offs : offsetted coordinates of the cursor
3696 * [I] offset : offset amount
3697 * [I] scroll : Bitmask of which directions we should scroll, if at all
3702 static void LISTVIEW_MarqueeHighlight(LISTVIEW_INFO *infoPtr, const POINT *coords_orig,
3703 const POINT *coords_offs, const POINT *offset,
3706 BOOL controlDown = FALSE;
3711 if (coords_offs->x > infoPtr->marqueeOrigin.x)
3713 rect.left = infoPtr->marqueeOrigin.x;
3714 rect.right = coords_offs->x;
3718 rect.left = coords_offs->x;
3719 rect.right = infoPtr->marqueeOrigin.x;
3722 if (coords_offs->y > infoPtr->marqueeOrigin.y)
3724 rect.top = infoPtr->marqueeOrigin.y;
3725 rect.bottom = coords_offs->y;
3729 rect.top = coords_offs->y;
3730 rect.bottom = infoPtr->marqueeOrigin.y;
3733 /* Cancel out the old marquee rectangle and draw the new one */
3734 LISTVIEW_InvalidateRect(infoPtr, &infoPtr->marqueeDrawRect);
3736 /* Scroll by the appropriate distance if applicable - speed up scrolling as
3737 the cursor is further away */
3739 if ((scroll & SCROLL_LEFT) && (coords_orig->x <= 0))
3740 LISTVIEW_Scroll(infoPtr, coords_orig->x, 0);
3742 if ((scroll & SCROLL_RIGHT) && (coords_orig->x >= infoPtr->rcList.right))
3743 LISTVIEW_Scroll(infoPtr, (coords_orig->x - infoPtr->rcList.right), 0);
3745 if ((scroll & SCROLL_UP) && (coords_orig->y <= 0))
3746 LISTVIEW_Scroll(infoPtr, 0, coords_orig->y);
3748 if ((scroll & SCROLL_DOWN) && (coords_orig->y >= infoPtr->rcList.bottom))
3749 LISTVIEW_Scroll(infoPtr, 0, (coords_orig->y - infoPtr->rcList.bottom));
3751 /* Invert the items in the old marquee rectangle */
3752 iterator_frameditems_absolute(&i, infoPtr, &infoPtr->marqueeRect);
3754 while (iterator_next(&i))
3758 if (LISTVIEW_GetItemState(infoPtr, i.nItem, LVIS_SELECTED) == LVIS_SELECTED)
3761 item.state = LVIS_SELECTED;
3763 item.stateMask = LVIS_SELECTED;
3765 LISTVIEW_SetItemState(infoPtr, i.nItem, &item);
3769 iterator_destroy(&i);
3771 CopyRect(&infoPtr->marqueeRect, &rect);
3773 CopyRect(&infoPtr->marqueeDrawRect, &rect);
3774 OffsetRect(&infoPtr->marqueeDrawRect, offset->x, offset->y);
3776 /* Iterate over the items within our marquee rectangle */
3777 iterator_frameditems_absolute(&i, infoPtr, &infoPtr->marqueeRect);
3779 if (GetKeyState(VK_CONTROL) & 0x8000)
3782 while (iterator_next(&i))
3786 /* If CTRL is pressed, invert. If not, always select the item. */
3787 if ((controlDown) && (LISTVIEW_GetItemState(infoPtr, i.nItem, LVIS_SELECTED)))
3790 item.state = LVIS_SELECTED;
3792 item.stateMask = LVIS_SELECTED;
3794 LISTVIEW_SetItemState(infoPtr, i.nItem, &item);
3798 iterator_destroy(&i);
3799 LISTVIEW_InvalidateRect(infoPtr, &rect);
3804 * Called when we are in a marquee selection that involves scrolling the listview (ie,
3805 * the cursor is outside the bounds of the client area). This is a TIMERPROC.
3808 * [I] hwnd : Handle to the listview
3809 * [I] uMsg : WM_TIMER (ignored)
3810 * [I] idEvent : The timer ID interpreted as a pointer to a LISTVIEW_INFO struct
3811 * [I] dwTimer : The elapsed time (ignored)
3816 static VOID CALLBACK LISTVIEW_ScrollTimer(HWND hWnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
3818 LISTVIEW_INFO *infoPtr;
3819 SCROLLINFO scrollInfo;
3825 infoPtr = (LISTVIEW_INFO *) idEvent;
3830 /* Get the current cursor position and convert to client coordinates */
3831 GetCursorPos(&coords_orig);
3832 ScreenToClient(hWnd, &coords_orig);
3834 /* Ensure coordinates are within client bounds */
3835 coords_offs.x = max(min(coords_orig.x, infoPtr->rcList.right), 0);
3836 coords_offs.y = max(min(coords_orig.y, infoPtr->rcList.bottom), 0);
3839 LISTVIEW_GetOrigin(infoPtr, &offset);
3841 /* Offset coordinates by the appropriate amount */
3842 coords_offs.x -= offset.x;
3843 coords_offs.y -= offset.y;
3845 scrollInfo.cbSize = sizeof(SCROLLINFO);
3846 scrollInfo.fMask = SIF_ALL;
3848 /* Work out in which directions we can scroll */
3849 if (GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
3851 if (scrollInfo.nPos != scrollInfo.nMin)
3852 scroll |= SCROLL_UP;
3854 if (((scrollInfo.nPage + scrollInfo.nPos) - 1) != scrollInfo.nMax)
3855 scroll |= SCROLL_DOWN;
3858 if (GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo))
3860 if (scrollInfo.nPos != scrollInfo.nMin)
3861 scroll |= SCROLL_LEFT;
3863 if (((scrollInfo.nPage + scrollInfo.nPos) - 1) != scrollInfo.nMax)
3864 scroll |= SCROLL_RIGHT;
3867 if (((coords_orig.x <= 0) && (scroll & SCROLL_LEFT)) ||
3868 ((coords_orig.y <= 0) && (scroll & SCROLL_UP)) ||
3869 ((coords_orig.x >= infoPtr->rcList.right) && (scroll & SCROLL_RIGHT)) ||
3870 ((coords_orig.y >= infoPtr->rcList.bottom) && (scroll & SCROLL_DOWN)))
3872 LISTVIEW_MarqueeHighlight(infoPtr, &coords_orig, &coords_offs, &offset, scroll);
3878 * Called whenever WM_MOUSEMOVE is received.
3881 * [I] infoPtr : valid pointer to the listview structure
3882 * [I] fwKeys : key indicator
3883 * [I] x,y : mouse position
3886 * 0 if the message is processed, non-zero if there was an error
3888 static LRESULT LISTVIEW_MouseMove(LISTVIEW_INFO *infoPtr, WORD fwKeys, INT x, INT y)
3890 if (!(fwKeys & MK_LBUTTON))
3891 infoPtr->bLButtonDown = FALSE;
3893 if (infoPtr->bLButtonDown)
3897 LVHITTESTINFO lvHitTestInfo;
3898 WORD wDragWidth = GetSystemMetrics(SM_CXDRAG);
3899 WORD wDragHeight= GetSystemMetrics(SM_CYDRAG);
3901 if (infoPtr->bMarqueeSelect)
3911 LISTVIEW_GetOrigin(infoPtr, &offset);
3913 /* Ensure coordinates are within client bounds */
3914 coords_offs.x = max(min(x, infoPtr->rcList.right), 0);
3915 coords_offs.y = max(min(y, infoPtr->rcList.bottom), 0);
3917 /* Offset coordinates by the appropriate amount */
3918 coords_offs.x -= offset.x;
3919 coords_offs.y -= offset.y;
3921 /* Enable the timer if we're going outside our bounds, in case the user doesn't
3922 move the mouse again */
3924 if ((x <= 0) || (y <= 0) || (x >= infoPtr->rcList.right) ||
3925 (y >= infoPtr->rcList.bottom))
3927 if (!infoPtr->bScrolling)
3929 infoPtr->bScrolling = TRUE;
3930 SetTimer(infoPtr->hwndSelf, (UINT_PTR) infoPtr, 1, LISTVIEW_ScrollTimer);
3935 infoPtr->bScrolling = FALSE;
3936 KillTimer(infoPtr->hwndSelf, (UINT_PTR) infoPtr);
3939 LISTVIEW_MarqueeHighlight(infoPtr, &coords_orig, &coords_offs, &offset, 0);
3943 rect.left = infoPtr->ptClickPos.x - wDragWidth;
3944 rect.right = infoPtr->ptClickPos.x + wDragWidth;
3945 rect.top = infoPtr->ptClickPos.y - wDragHeight;
3946 rect.bottom = infoPtr->ptClickPos.y + wDragHeight;
3951 lvHitTestInfo.pt = tmp;
3952 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, TRUE);
3954 /* reset item marker */
3955 if (infoPtr->nLButtonDownItem != lvHitTestInfo.iItem)
3956 infoPtr->nLButtonDownItem = -1;
3958 if (!PtInRect(&rect, tmp))
3960 /* this path covers the following:
3961 1. WM_LBUTTONDOWN over selected item (sets focus on it)
3962 2. change focus with keys
3963 3. move mouse over item from step 1 selects it and moves focus on it */
3964 if (infoPtr->nLButtonDownItem != -1 &&
3965 !LISTVIEW_GetItemState(infoPtr, infoPtr->nLButtonDownItem, LVIS_SELECTED))
3969 lvItem.state = LVIS_FOCUSED | LVIS_SELECTED;
3970 lvItem.stateMask = LVIS_FOCUSED | LVIS_SELECTED;
3972 LISTVIEW_SetItemState(infoPtr, infoPtr->nLButtonDownItem, &lvItem);
3973 infoPtr->nLButtonDownItem = -1;
3976 if (!infoPtr->bDragging)
3978 lvHitTestInfo.pt = infoPtr->ptClickPos;
3979 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, TRUE);
3981 /* If the click is outside the range of an item, begin a
3982 highlight. If not, begin an item drag. */
3983 if (lvHitTestInfo.iItem == -1)
3987 /* If we're allowing multiple selections, send notification.
3988 If return value is non-zero, cancel. */
3989 if (!(infoPtr->dwStyle & LVS_SINGLESEL) && (notify_hdr(infoPtr, LVN_MARQUEEBEGIN, &hdr) == 0))
3991 /* Store the absolute coordinates of the click */
3993 LISTVIEW_GetOrigin(infoPtr, &offset);
3995 infoPtr->marqueeOrigin.x = infoPtr->ptClickPos.x - offset.x;
3996 infoPtr->marqueeOrigin.y = infoPtr->ptClickPos.y - offset.y;
3998 /* Begin selection and capture mouse */
3999 infoPtr->bMarqueeSelect = TRUE;
4000 SetCapture(infoPtr->hwndSelf);
4007 ZeroMemory(&nmlv, sizeof(nmlv));
4008 nmlv.iItem = lvHitTestInfo.iItem;
4009 nmlv.ptAction = infoPtr->ptClickPos;
4011 notify_listview(infoPtr, LVN_BEGINDRAG, &nmlv);
4012 infoPtr->bDragging = TRUE;
4020 /* see if we are supposed to be tracking mouse hovering */
4021 if (LISTVIEW_IsHotTracking(infoPtr)) {
4022 TRACKMOUSEEVENT trackinfo;
4024 trackinfo.cbSize = sizeof(TRACKMOUSEEVENT);
4025 trackinfo.dwFlags = TME_QUERY;
4027 /* see if we are already tracking this hwnd */
4028 _TrackMouseEvent(&trackinfo);
4030 if(!(trackinfo.dwFlags & TME_HOVER) || trackinfo.hwndTrack != infoPtr->hwndSelf) {
4031 trackinfo.dwFlags = TME_HOVER;
4032 trackinfo.dwHoverTime = infoPtr->dwHoverTime;
4033 trackinfo.hwndTrack = infoPtr->hwndSelf;
4035 /* call TRACKMOUSEEVENT so we receive WM_MOUSEHOVER messages */
4036 _TrackMouseEvent(&trackinfo);
4045 * Tests whether the item is assignable to a list with style lStyle
4047 static inline BOOL is_assignable_item(const LVITEMW *lpLVItem, LONG lStyle)
4049 if ( (lpLVItem->mask & LVIF_TEXT) &&
4050 (lpLVItem->pszText == LPSTR_TEXTCALLBACKW) &&
4051 (lStyle & (LVS_SORTASCENDING | LVS_SORTDESCENDING)) ) return FALSE;
4059 * Helper for LISTVIEW_SetItemT *only*: sets item attributes.
4062 * [I] infoPtr : valid pointer to the listview structure
4063 * [I] lpLVItem : valid pointer to new item attributes
4064 * [I] isNew : the item being set is being inserted
4065 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
4066 * [O] bChanged : will be set to TRUE if the item really changed
4072 static BOOL set_main_item(LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem, BOOL isNew, BOOL isW, BOOL *bChanged)
4078 /* stateMask is ignored for LVM_INSERTITEM */
4079 UINT stateMask = isNew ? ~0 : lpLVItem->stateMask;
4083 assert(lpLVItem->iItem >= 0 && lpLVItem->iItem < infoPtr->nItemCount);
4085 if (lpLVItem->mask == 0) return TRUE;
4087 if (infoPtr->dwStyle & LVS_OWNERDATA)
4089 /* a virtual listview only stores selection and focus */
4090 if (lpLVItem->mask & ~LVIF_STATE)
4096 HDPA hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, lpLVItem->iItem);
4097 lpItem = DPA_GetPtr(hdpaSubItems, 0);
4101 /* we need to get the lParam and state of the item */
4102 item.iItem = lpLVItem->iItem;
4103 item.iSubItem = lpLVItem->iSubItem;
4104 item.mask = LVIF_STATE | LVIF_PARAM;
4105 item.stateMask = (infoPtr->dwStyle & LVS_OWNERDATA) ? LVIS_FOCUSED | LVIS_SELECTED : ~0;
4109 if (!isNew && !LISTVIEW_GetItemW(infoPtr, &item)) return FALSE;
4111 TRACE("oldState=%x, newState=%x\n", item.state, lpLVItem->state);
4112 /* determine what fields will change */
4113 if ((lpLVItem->mask & LVIF_STATE) && ((item.state ^ lpLVItem->state) & stateMask & ~infoPtr->uCallbackMask))
4114 uChanged |= LVIF_STATE;
4116 if ((lpLVItem->mask & LVIF_IMAGE) && (lpItem->hdr.iImage != lpLVItem->iImage))
4117 uChanged |= LVIF_IMAGE;
4119 if ((lpLVItem->mask & LVIF_PARAM) && (lpItem->lParam != lpLVItem->lParam))
4120 uChanged |= LVIF_PARAM;
4122 if ((lpLVItem->mask & LVIF_INDENT) && (lpItem->iIndent != lpLVItem->iIndent))
4123 uChanged |= LVIF_INDENT;
4125 if ((lpLVItem->mask & LVIF_TEXT) && textcmpWT(lpItem->hdr.pszText, lpLVItem->pszText, isW))
4126 uChanged |= LVIF_TEXT;
4128 TRACE("uChanged=0x%x\n", uChanged);
4129 if (!uChanged) return TRUE;
4132 ZeroMemory(&nmlv, sizeof(NMLISTVIEW));
4133 nmlv.iItem = lpLVItem->iItem;
4134 nmlv.uNewState = (item.state & ~stateMask) | (lpLVItem->state & stateMask);
4135 nmlv.uOldState = item.state;
4136 nmlv.uChanged = uChanged;
4137 nmlv.lParam = item.lParam;
4139 /* send LVN_ITEMCHANGING notification, if the item is not being inserted */
4140 /* and we are _NOT_ virtual (LVS_OWNERDATA), and change notifications */
4142 if(lpItem && !isNew && infoPtr->bDoChangeNotify)
4144 HWND hwndSelf = infoPtr->hwndSelf;
4146 if (notify_listview(infoPtr, LVN_ITEMCHANGING, &nmlv))
4148 if (!IsWindow(hwndSelf))
4152 /* copy information */
4153 if (lpLVItem->mask & LVIF_TEXT)
4154 textsetptrT(&lpItem->hdr.pszText, lpLVItem->pszText, isW);
4156 if (lpLVItem->mask & LVIF_IMAGE)
4157 lpItem->hdr.iImage = lpLVItem->iImage;
4159 if (lpLVItem->mask & LVIF_PARAM)
4160 lpItem->lParam = lpLVItem->lParam;
4162 if (lpLVItem->mask & LVIF_INDENT)
4163 lpItem->iIndent = lpLVItem->iIndent;
4165 if (uChanged & LVIF_STATE)
4167 if (lpItem && (stateMask & ~infoPtr->uCallbackMask))
4169 lpItem->state &= ~stateMask;
4170 lpItem->state |= (lpLVItem->state & stateMask);
4172 if (lpLVItem->state & stateMask & ~infoPtr->uCallbackMask & LVIS_SELECTED)
4174 if (infoPtr->dwStyle & LVS_SINGLESEL) LISTVIEW_DeselectAllSkipItem(infoPtr, lpLVItem->iItem);
4175 ranges_additem(infoPtr->selectionRanges, lpLVItem->iItem);
4177 else if (stateMask & LVIS_SELECTED)
4179 ranges_delitem(infoPtr->selectionRanges, lpLVItem->iItem);
4181 /* if we are asked to change focus, and we manage it, do it */
4182 if (stateMask & ~infoPtr->uCallbackMask & LVIS_FOCUSED)
4184 if (lpLVItem->state & LVIS_FOCUSED)
4186 if (infoPtr->nFocusedItem != -1)
4188 /* remove current focus */
4189 item.mask = LVIF_STATE;
4191 item.stateMask = LVIS_FOCUSED;
4193 /* recurse with redrawing an item */
4194 LISTVIEW_SetItemState(infoPtr, infoPtr->nFocusedItem, &item);
4197 infoPtr->nFocusedItem = lpLVItem->iItem;
4198 LISTVIEW_EnsureVisible(infoPtr, lpLVItem->iItem, infoPtr->uView == LV_VIEW_LIST);
4200 else if (infoPtr->nFocusedItem == lpLVItem->iItem)
4202 infoPtr->nFocusedItem = -1;
4207 /* if we're inserting the item, we're done */
4208 if (isNew) return TRUE;
4210 /* send LVN_ITEMCHANGED notification */
4211 if (lpLVItem->mask & LVIF_PARAM) nmlv.lParam = lpLVItem->lParam;
4212 if (infoPtr->bDoChangeNotify) notify_listview(infoPtr, LVN_ITEMCHANGED, &nmlv);
4219 * Helper for LISTVIEW_{Set,Insert}ItemT *only*: sets subitem attributes.
4222 * [I] infoPtr : valid pointer to the listview structure
4223 * [I] lpLVItem : valid pointer to new subitem attributes
4224 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
4225 * [O] bChanged : will be set to TRUE if the item really changed
4231 static BOOL set_sub_item(const LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem, BOOL isW, BOOL *bChanged)
4234 SUBITEM_INFO *lpSubItem;
4236 /* we do not support subitems for virtual listviews */
4237 if (infoPtr->dwStyle & LVS_OWNERDATA) return FALSE;
4239 /* set subitem only if column is present */
4240 if (lpLVItem->iSubItem >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
4242 /* First do some sanity checks */
4243 /* The LVIF_STATE flag is valid for subitems, but does not appear to be
4244 particularly useful. We currently do not actually do anything with
4245 the flag on subitems.
4247 if (lpLVItem->mask & ~(LVIF_TEXT | LVIF_IMAGE | LVIF_STATE)) return FALSE;
4248 if (!(lpLVItem->mask & (LVIF_TEXT | LVIF_IMAGE | LVIF_STATE))) return TRUE;
4250 /* get the subitem structure, and create it if not there */
4251 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, lpLVItem->iItem);
4252 assert (hdpaSubItems);
4254 lpSubItem = LISTVIEW_GetSubItemPtr(hdpaSubItems, lpLVItem->iSubItem);
4257 SUBITEM_INFO *tmpSubItem;
4260 lpSubItem = Alloc(sizeof(SUBITEM_INFO));
4261 if (!lpSubItem) return FALSE;
4262 /* we could binary search here, if need be...*/
4263 for (i = 1; i < DPA_GetPtrCount(hdpaSubItems); i++)
4265 tmpSubItem = DPA_GetPtr(hdpaSubItems, i);
4266 if (tmpSubItem->iSubItem > lpLVItem->iSubItem) break;
4268 if (DPA_InsertPtr(hdpaSubItems, i, lpSubItem) == -1)
4273 lpSubItem->iSubItem = lpLVItem->iSubItem;
4274 lpSubItem->hdr.iImage = I_IMAGECALLBACK;
4278 if ((lpLVItem->mask & LVIF_IMAGE) && (lpSubItem->hdr.iImage != lpLVItem->iImage))
4280 lpSubItem->hdr.iImage = lpLVItem->iImage;
4284 if ((lpLVItem->mask & LVIF_TEXT) && textcmpWT(lpSubItem->hdr.pszText, lpLVItem->pszText, isW))
4286 textsetptrT(&lpSubItem->hdr.pszText, lpLVItem->pszText, isW);
4295 * Sets item attributes.
4298 * [I] infoPtr : valid pointer to the listview structure
4299 * [I] lpLVItem : new item attributes
4300 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
4306 static BOOL LISTVIEW_SetItemT(LISTVIEW_INFO *infoPtr, LVITEMW *lpLVItem, BOOL isW)
4308 HWND hwndSelf = infoPtr->hwndSelf;
4309 LPWSTR pszText = NULL;
4310 BOOL bResult, bChanged = FALSE;
4312 TRACE("(lpLVItem=%s, isW=%d)\n", debuglvitem_t(lpLVItem, isW), isW);
4314 if (!lpLVItem || lpLVItem->iItem < 0 || lpLVItem->iItem >= infoPtr->nItemCount)
4317 /* For efficiency, we transform the lpLVItem->pszText to Unicode here */
4318 if ((lpLVItem->mask & LVIF_TEXT) && is_text(lpLVItem->pszText))
4320 pszText = lpLVItem->pszText;
4321 lpLVItem->pszText = textdupTtoW(lpLVItem->pszText, isW);
4324 /* actually set the fields */
4325 if (!is_assignable_item(lpLVItem, infoPtr->dwStyle)) return FALSE;
4327 if (lpLVItem->iSubItem)
4328 bResult = set_sub_item(infoPtr, lpLVItem, TRUE, &bChanged);
4330 bResult = set_main_item(infoPtr, lpLVItem, FALSE, TRUE, &bChanged);
4331 if (!IsWindow(hwndSelf))
4334 /* redraw item, if necessary */
4335 if (bChanged && !infoPtr->bIsDrawing)
4337 /* this little optimization eliminates some nasty flicker */
4338 if ( infoPtr->uView == LV_VIEW_DETAILS && !(infoPtr->dwStyle & LVS_OWNERDRAWFIXED) &&
4339 !(infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) &&
4340 lpLVItem->iSubItem > 0 && lpLVItem->iSubItem <= DPA_GetPtrCount(infoPtr->hdpaColumns) )
4341 LISTVIEW_InvalidateSubItem(infoPtr, lpLVItem->iItem, lpLVItem->iSubItem);
4343 LISTVIEW_InvalidateItem(infoPtr, lpLVItem->iItem);
4348 textfreeT(lpLVItem->pszText, isW);
4349 lpLVItem->pszText = pszText;
4357 * Retrieves the index of the item at coordinate (0, 0) of the client area.
4360 * [I] infoPtr : valid pointer to the listview structure
4365 static INT LISTVIEW_GetTopIndex(const LISTVIEW_INFO *infoPtr)
4368 SCROLLINFO scrollInfo;
4370 scrollInfo.cbSize = sizeof(SCROLLINFO);
4371 scrollInfo.fMask = SIF_POS;
4373 if (infoPtr->uView == LV_VIEW_LIST)
4375 if (GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo))
4376 nItem = scrollInfo.nPos * LISTVIEW_GetCountPerColumn(infoPtr);
4378 else if (infoPtr->uView == LV_VIEW_DETAILS)
4380 if (GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
4381 nItem = scrollInfo.nPos;
4385 if (GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
4386 nItem = LISTVIEW_GetCountPerRow(infoPtr) * (scrollInfo.nPos / infoPtr->nItemHeight);
4389 TRACE("nItem=%d\n", nItem);
4397 * Erases the background of the given rectangle
4400 * [I] infoPtr : valid pointer to the listview structure
4401 * [I] hdc : device context handle
4402 * [I] lprcBox : clipping rectangle
4408 static inline BOOL LISTVIEW_FillBkgnd(const LISTVIEW_INFO *infoPtr, HDC hdc, const RECT *lprcBox)
4410 if (!infoPtr->hBkBrush) return FALSE;
4412 TRACE("(hdc=%p, lprcBox=%s, hBkBrush=%p)\n", hdc, wine_dbgstr_rect(lprcBox), infoPtr->hBkBrush);
4414 return FillRect(hdc, lprcBox, infoPtr->hBkBrush);
4422 * [I] infoPtr : valid pointer to the listview structure
4423 * [I] hdc : device context handle
4424 * [I] nItem : item index
4425 * [I] nSubItem : subitem index
4426 * [I] pos : item position in client coordinates
4427 * [I] cdmode : custom draw mode
4433 static BOOL LISTVIEW_DrawItem(LISTVIEW_INFO *infoPtr, HDC hdc, INT nItem, INT nSubItem, POINT pos, DWORD cdmode)
4436 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
4437 static WCHAR szCallback[] = { '(', 'c', 'a', 'l', 'l', 'b', 'a', 'c', 'k', ')', 0 };
4438 DWORD cdsubitemmode = CDRF_DODEFAULT;
4440 RECT rcSelect, rcBox, rcIcon, rcLabel, rcStateIcon;
4441 NMLVCUSTOMDRAW nmlvcd;
4446 TRACE("(hdc=%p, nItem=%d, nSubItem=%d, pos=%s)\n", hdc, nItem, nSubItem, wine_dbgstr_point(&pos));
4448 /* get information needed for drawing the item */
4449 lvItem.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_PARAM;
4450 if (nSubItem == 0) lvItem.mask |= LVIF_STATE;
4451 if (infoPtr->uView == LV_VIEW_DETAILS) lvItem.mask |= LVIF_INDENT;
4452 lvItem.stateMask = LVIS_SELECTED | LVIS_FOCUSED | LVIS_STATEIMAGEMASK | LVIS_CUT;
4453 lvItem.iItem = nItem;
4454 lvItem.iSubItem = nSubItem;
4457 lvItem.cchTextMax = DISP_TEXT_SIZE;
4458 lvItem.pszText = szDispText;
4459 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return FALSE;
4460 if (nSubItem > 0 && (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT))
4461 lvItem.state = LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED);
4462 if (lvItem.pszText == LPSTR_TEXTCALLBACKW) lvItem.pszText = szCallback;
4463 TRACE(" lvItem=%s\n", debuglvitem_t(&lvItem, TRUE));
4465 /* now check if we need to update the focus rectangle */
4466 lprcFocus = infoPtr->bFocus && (lvItem.state & LVIS_FOCUSED) ? &infoPtr->rcFocus : 0;
4468 if (!lprcFocus) lvItem.state &= ~LVIS_FOCUSED;
4469 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, &rcBox, &rcSelect, &rcIcon, &rcStateIcon, &rcLabel);
4470 OffsetRect(&rcBox, pos.x, pos.y);
4471 OffsetRect(&rcSelect, pos.x, pos.y);
4472 OffsetRect(&rcIcon, pos.x, pos.y);
4473 OffsetRect(&rcStateIcon, pos.x, pos.y);
4474 OffsetRect(&rcLabel, pos.x, pos.y);
4475 TRACE(" rcBox=%s, rcSelect=%s, rcIcon=%s. rcLabel=%s\n",
4476 wine_dbgstr_rect(&rcBox), wine_dbgstr_rect(&rcSelect),
4477 wine_dbgstr_rect(&rcIcon), wine_dbgstr_rect(&rcLabel));
4479 /* fill in the custom draw structure */
4480 customdraw_fill(&nmlvcd, infoPtr, hdc, &rcBox, &lvItem);
4482 hOldFont = GetCurrentObject(hdc, OBJ_FONT);
4483 if (nSubItem > 0) cdmode = infoPtr->cditemmode;
4484 if (cdmode & CDRF_SKIPDEFAULT) goto postpaint;
4485 if (cdmode & CDRF_NOTIFYITEMDRAW)
4486 cdsubitemmode = notify_customdraw(infoPtr, CDDS_PREPAINT, &nmlvcd);
4487 if (nSubItem == 0) infoPtr->cditemmode = cdsubitemmode;
4488 if (cdsubitemmode & CDRF_SKIPDEFAULT) goto postpaint;
4489 /* we have to send a CDDS_SUBITEM customdraw explicitly for subitem 0 */
4490 if (nSubItem == 0 && cdsubitemmode == CDRF_NOTIFYITEMDRAW)
4492 cdsubitemmode = notify_customdraw(infoPtr, CDDS_SUBITEM | CDDS_ITEMPREPAINT, &nmlvcd);
4493 if (cdsubitemmode & CDRF_SKIPDEFAULT) goto postpaint;
4495 if (nSubItem == 0 || (cdmode & CDRF_NOTIFYITEMDRAW))
4496 prepaint_setup(infoPtr, hdc, &nmlvcd, FALSE);
4497 else if ((infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) == FALSE)
4498 prepaint_setup(infoPtr, hdc, &nmlvcd, TRUE);
4500 /* in full row select, subitems, will just use main item's colors */
4501 if (nSubItem && infoPtr->uView == LV_VIEW_DETAILS && (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT))
4502 nmlvcd.clrTextBk = CLR_NONE;
4504 /* FIXME: temporary hack */
4505 rcSelect.left = rcLabel.left;
4507 /* draw the selection background, if we're drawing the main item */
4510 /* in icon mode, the label rect is really what we want to draw the
4512 if (infoPtr->uView == LV_VIEW_ICON)
4515 if (infoPtr->uView == LV_VIEW_DETAILS && (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT))
4517 /* we have to update left focus bound too if item isn't in leftmost column
4518 and reduce right box bound */
4519 if (DPA_GetPtrCount(infoPtr->hdpaColumns) > 0)
4523 if ((leftmost = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, 0, 0)))
4525 INT Originx = pos.x - LISTVIEW_GetColumnInfo(infoPtr, 0)->rcHeader.left;
4526 INT index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX,
4527 DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, 0);
4529 rcBox.right = LISTVIEW_GetColumnInfo(infoPtr, index)->rcHeader.right + Originx;
4530 rcSelect.left = LISTVIEW_GetColumnInfo(infoPtr, leftmost)->rcHeader.left + Originx;
4534 rcSelect.right = rcBox.right;
4537 if (nmlvcd.clrTextBk != CLR_NONE)
4538 ExtTextOutW(hdc, rcSelect.left, rcSelect.top, ETO_OPAQUE, &rcSelect, NULL, 0, NULL);
4539 /* store new focus rectangle */
4540 if (infoPtr->nFocusedItem == nItem) infoPtr->rcFocus = rcSelect;
4544 if (infoPtr->himlState && STATEIMAGEINDEX(lvItem.state) && (nSubItem == 0))
4546 UINT uStateImage = STATEIMAGEINDEX(lvItem.state);
4549 TRACE("uStateImage=%d\n", uStateImage);
4550 ImageList_Draw(infoPtr->himlState, uStateImage - 1, hdc,
4551 rcStateIcon.left, rcStateIcon.top, ILD_NORMAL);
4556 himl = (infoPtr->uView == LV_VIEW_ICON ? infoPtr->himlNormal : infoPtr->himlSmall);
4557 if (himl && lvItem.iImage >= 0 && !IsRectEmpty(&rcIcon))
4561 TRACE("iImage=%d\n", lvItem.iImage);
4563 if (lvItem.state & (LVIS_SELECTED | LVIS_CUT) && infoPtr->bFocus)
4564 style = ILD_SELECTED;
4568 ImageList_DrawEx(himl, lvItem.iImage, hdc, rcIcon.left, rcIcon.top,
4569 rcIcon.right - rcIcon.left, rcIcon.bottom - rcIcon.top, infoPtr->clrBk,
4570 lvItem.state & LVIS_CUT ? RGB(255, 255, 255) : CLR_DEFAULT,
4574 /* Don't bother painting item being edited */
4575 if (infoPtr->hwndEdit && nItem == infoPtr->nEditLabelItem && nSubItem == 0) goto postpaint;
4577 /* figure out the text drawing flags */
4578 uFormat = (infoPtr->uView == LV_VIEW_ICON ? (lprcFocus ? LV_FL_DT_FLAGS : LV_ML_DT_FLAGS) : LV_SL_DT_FLAGS);
4579 if (infoPtr->uView == LV_VIEW_ICON)
4580 uFormat = (lprcFocus ? LV_FL_DT_FLAGS : LV_ML_DT_FLAGS);
4583 switch (LISTVIEW_GetColumnInfo(infoPtr, nSubItem)->fmt & LVCFMT_JUSTIFYMASK)
4585 case LVCFMT_RIGHT: uFormat |= DT_RIGHT; break;
4586 case LVCFMT_CENTER: uFormat |= DT_CENTER; break;
4587 default: uFormat |= DT_LEFT;
4590 if (!(uFormat & (DT_RIGHT | DT_CENTER)))
4592 if (himl && lvItem.iImage >= 0 && !IsRectEmpty(&rcIcon)) rcLabel.left += IMAGE_PADDING;
4593 else rcLabel.left += LABEL_HOR_PADDING;
4595 else if (uFormat & DT_RIGHT) rcLabel.right -= LABEL_HOR_PADDING;
4597 /* for GRIDLINES reduce the bottom so the text formats correctly */
4598 if (infoPtr->uView == LV_VIEW_DETAILS && infoPtr->dwLvExStyle & LVS_EX_GRIDLINES)
4601 DrawTextW(hdc, lvItem.pszText, -1, &rcLabel, uFormat);
4604 if (cdsubitemmode & CDRF_NOTIFYPOSTPAINT)
4605 notify_postpaint(infoPtr, &nmlvcd);
4606 if (cdsubitemmode & CDRF_NEWFONT)
4607 SelectObject(hdc, hOldFont);
4613 * Draws listview items when in owner draw mode.
4616 * [I] infoPtr : valid pointer to the listview structure
4617 * [I] hdc : device context handle
4622 static void LISTVIEW_RefreshOwnerDraw(const LISTVIEW_INFO *infoPtr, ITERATOR *i, HDC hdc, DWORD cdmode)
4624 UINT uID = (UINT)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
4625 DWORD cditemmode = CDRF_DODEFAULT;
4626 NMLVCUSTOMDRAW nmlvcd;
4627 POINT Origin, Position;
4633 ZeroMemory(&dis, sizeof(dis));
4635 /* Get scroll info once before loop */
4636 LISTVIEW_GetOrigin(infoPtr, &Origin);
4638 /* iterate through the invalidated rows */
4639 while(iterator_next(i))
4641 item.iItem = i->nItem;
4643 item.mask = LVIF_PARAM | LVIF_STATE;
4644 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
4645 if (!LISTVIEW_GetItemW(infoPtr, &item)) continue;
4647 dis.CtlType = ODT_LISTVIEW;
4649 dis.itemID = item.iItem;
4650 dis.itemAction = ODA_DRAWENTIRE;
4652 if (item.state & LVIS_SELECTED) dis.itemState |= ODS_SELECTED;
4653 if (infoPtr->bFocus && (item.state & LVIS_FOCUSED)) dis.itemState |= ODS_FOCUS;
4654 dis.hwndItem = infoPtr->hwndSelf;
4656 LISTVIEW_GetItemOrigin(infoPtr, dis.itemID, &Position);
4657 dis.rcItem.left = Position.x + Origin.x;
4658 dis.rcItem.right = dis.rcItem.left + infoPtr->nItemWidth;
4659 dis.rcItem.top = Position.y + Origin.y;
4660 dis.rcItem.bottom = dis.rcItem.top + infoPtr->nItemHeight;
4661 dis.itemData = item.lParam;
4663 TRACE("item=%s, rcItem=%s\n", debuglvitem_t(&item, TRUE), wine_dbgstr_rect(&dis.rcItem));
4666 * Even if we do not send the CDRF_NOTIFYITEMDRAW we need to fill the nmlvcd
4667 * structure for the rest. of the paint cycle
4669 customdraw_fill(&nmlvcd, infoPtr, hdc, &dis.rcItem, &item);
4670 if (cdmode & CDRF_NOTIFYITEMDRAW)
4671 cditemmode = notify_customdraw(infoPtr, CDDS_PREPAINT, &nmlvcd);
4673 if (!(cditemmode & CDRF_SKIPDEFAULT))
4675 prepaint_setup (infoPtr, hdc, &nmlvcd, FALSE);
4676 SendMessageW(infoPtr->hwndNotify, WM_DRAWITEM, dis.CtlID, (LPARAM)&dis);
4679 if (cditemmode & CDRF_NOTIFYPOSTPAINT)
4680 notify_postpaint(infoPtr, &nmlvcd);
4686 * Draws listview items when in report display mode.
4689 * [I] infoPtr : valid pointer to the listview structure
4690 * [I] hdc : device context handle
4691 * [I] cdmode : custom draw mode
4696 static void LISTVIEW_RefreshReport(LISTVIEW_INFO *infoPtr, ITERATOR *i, HDC hdc, DWORD cdmode)
4699 RECT rcClip, rcItem;
4700 POINT Origin, Position;
4707 /* figure out what to draw */
4708 rgntype = GetClipBox(hdc, &rcClip);
4709 if (rgntype == NULLREGION) return;
4711 /* Get scroll info once before loop */
4712 LISTVIEW_GetOrigin(infoPtr, &Origin);
4714 colRanges = ranges_create(DPA_GetPtrCount(infoPtr->hdpaColumns));
4716 /* narrow down the columns we need to paint */
4717 for(col = 0; col < DPA_GetPtrCount(infoPtr->hdpaColumns); col++)
4719 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, col, 0);
4721 LISTVIEW_GetHeaderRect(infoPtr, index, &rcItem);
4722 if ((rcItem.right + Origin.x >= rcClip.left) && (rcItem.left + Origin.x < rcClip.right))
4723 ranges_additem(colRanges, index);
4725 iterator_rangesitems(&j, colRanges);
4727 /* in full row select, we _have_ to draw the main item */
4728 if (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT)
4731 /* iterate through the invalidated rows */
4732 while(iterator_next(i))
4734 LISTVIEW_GetItemOrigin(infoPtr, i->nItem, &Position);
4735 Position.y += Origin.y;
4737 /* iterate through the invalidated columns */
4738 while(iterator_next(&j))
4740 LISTVIEW_GetHeaderRect(infoPtr, j.nItem, &rcItem);
4741 Position.x = (j.nItem == 0) ? rcItem.left + Origin.x : Origin.x;
4743 if (rgntype == COMPLEXREGION && !((infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) && j.nItem == 0))
4746 rcItem.bottom = infoPtr->nItemHeight;
4747 OffsetRect(&rcItem, Origin.x, Position.y);
4748 if (!RectVisible(hdc, &rcItem)) continue;
4751 LISTVIEW_DrawItem(infoPtr, hdc, i->nItem, j.nItem, Position, cdmode);
4754 iterator_destroy(&j);
4759 * Draws the gridlines if necessary when in report display mode.
4762 * [I] infoPtr : valid pointer to the listview structure
4763 * [I] hdc : device context handle
4768 static void LISTVIEW_RefreshReportGrid(LISTVIEW_INFO *infoPtr, HDC hdc)
4774 RECT rcClip, rcItem = {0};
4782 /* figure out what to draw */
4783 rgntype = GetClipBox(hdc, &rcClip);
4784 if (rgntype == NULLREGION) return;
4786 /* Get scroll info once before loop */
4787 LISTVIEW_GetOrigin(infoPtr, &Origin);
4789 colRanges = ranges_create(DPA_GetPtrCount(infoPtr->hdpaColumns));
4791 /* narrow down the columns we need to paint */
4792 for(col = 0; col < DPA_GetPtrCount(infoPtr->hdpaColumns); col++)
4794 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, col, 0);
4796 LISTVIEW_GetHeaderRect(infoPtr, index, &rcItem);
4797 if ((rcItem.right + Origin.x >= rcClip.left) && (rcItem.left + Origin.x < rcClip.right))
4798 ranges_additem(colRanges, index);
4801 /* is right most vertical line visible? */
4802 if (DPA_GetPtrCount(infoPtr->hdpaColumns) > 0)
4804 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, 0);
4805 LISTVIEW_GetHeaderRect(infoPtr, index, &rcItem);
4806 rmost = (rcItem.right + Origin.x < rcClip.right);
4809 if ((hPen = CreatePen( PS_SOLID, 1, comctl32_color.clr3dFace )))
4811 hOldPen = SelectObject ( hdc, hPen );
4813 /* draw the vertical lines for the columns */
4814 iterator_rangesitems(&j, colRanges);
4815 while(iterator_next(&j))
4817 LISTVIEW_GetHeaderRect(infoPtr, j.nItem, &rcItem);
4818 if (rcItem.left == 0) continue; /* skip leftmost column */
4819 rcItem.left += Origin.x;
4820 rcItem.right += Origin.x;
4821 rcItem.top = infoPtr->rcList.top;
4822 rcItem.bottom = infoPtr->rcList.bottom;
4823 TRACE("vert col=%d, rcItem=%s\n", j.nItem, wine_dbgstr_rect(&rcItem));
4824 MoveToEx (hdc, rcItem.left, rcItem.top, NULL);
4825 LineTo (hdc, rcItem.left, rcItem.bottom);
4827 iterator_destroy(&j);
4828 /* draw rightmost grid line if visible */
4831 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX,
4832 DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, 0);
4833 LISTVIEW_GetHeaderRect(infoPtr, index, &rcItem);
4835 rcItem.right += Origin.x;
4837 MoveToEx (hdc, rcItem.right, infoPtr->rcList.top, NULL);
4838 LineTo (hdc, rcItem.right, infoPtr->rcList.bottom);
4841 /* draw the horizontial lines for the rows */
4842 itemheight = LISTVIEW_CalculateItemHeight(infoPtr);
4843 rcItem.left = infoPtr->rcList.left;
4844 rcItem.right = infoPtr->rcList.right;
4845 rcItem.bottom = rcItem.top = Origin.y - 1;
4846 MoveToEx(hdc, rcItem.left, rcItem.top, NULL);
4847 LineTo(hdc, rcItem.right, rcItem.top);
4848 for(y=itemheight-1+Origin.y; y<=infoPtr->rcList.bottom; y+=itemheight)
4850 rcItem.bottom = rcItem.top = y;
4851 TRACE("horz rcItem=%s\n", wine_dbgstr_rect(&rcItem));
4852 MoveToEx (hdc, rcItem.left, rcItem.top, NULL);
4853 LineTo (hdc, rcItem.right, rcItem.top);
4856 SelectObject( hdc, hOldPen );
4857 DeleteObject( hPen );
4863 * Draws listview items when in list display mode.
4866 * [I] infoPtr : valid pointer to the listview structure
4867 * [I] hdc : device context handle
4868 * [I] cdmode : custom draw mode
4873 static void LISTVIEW_RefreshList(LISTVIEW_INFO *infoPtr, ITERATOR *i, HDC hdc, DWORD cdmode)
4875 POINT Origin, Position;
4877 /* Get scroll info once before loop */
4878 LISTVIEW_GetOrigin(infoPtr, &Origin);
4880 while(iterator_prev(i))
4882 LISTVIEW_GetItemOrigin(infoPtr, i->nItem, &Position);
4883 Position.x += Origin.x;
4884 Position.y += Origin.y;
4886 LISTVIEW_DrawItem(infoPtr, hdc, i->nItem, 0, Position, cdmode);
4893 * Draws listview items.
4896 * [I] infoPtr : valid pointer to the listview structure
4897 * [I] hdc : device context handle
4898 * [I] prcErase : rect to be erased before refresh (may be NULL)
4903 static void LISTVIEW_Refresh(LISTVIEW_INFO *infoPtr, HDC hdc, const RECT *prcErase)
4905 COLORREF oldTextColor = 0, oldBkColor = 0, oldClrTextBk, oldClrText;
4906 NMLVCUSTOMDRAW nmlvcd;
4913 HBITMAP hbmp = NULL;
4916 LISTVIEW_DUMP(infoPtr);
4918 if (infoPtr->dwLvExStyle & LVS_EX_DOUBLEBUFFER) {
4919 TRACE("double buffering\n");
4921 hdc = CreateCompatibleDC(hdcOrig);
4923 ERR("Failed to create DC for backbuffer\n");
4926 hbmp = CreateCompatibleBitmap(hdcOrig, infoPtr->rcList.right,
4927 infoPtr->rcList.bottom);
4929 ERR("Failed to create bitmap for backbuffer\n");
4934 SelectObject(hdc, hbmp);
4935 SelectObject(hdc, infoPtr->hFont);
4937 /* Save dc values we're gonna trash while drawing
4938 * FIXME: Should be done in LISTVIEW_DrawItem() */
4939 hOldFont = SelectObject(hdc, infoPtr->hFont);
4940 oldBkMode = GetBkMode(hdc);
4941 oldBkColor = GetBkColor(hdc);
4942 oldTextColor = GetTextColor(hdc);
4945 infoPtr->bIsDrawing = TRUE;
4948 LISTVIEW_FillBkgnd(infoPtr, hdc, prcErase);
4949 } else if (infoPtr->dwLvExStyle & LVS_EX_DOUBLEBUFFER) {
4950 /* If no erasing was done (usually because RedrawWindow was called
4951 * with RDW_INVALIDATE only) we need to copy the old contents into
4952 * the backbuffer before continuing. */
4953 BitBlt(hdc, infoPtr->rcList.left, infoPtr->rcList.top,
4954 infoPtr->rcList.right - infoPtr->rcList.left,
4955 infoPtr->rcList.bottom - infoPtr->rcList.top,
4956 hdcOrig, infoPtr->rcList.left, infoPtr->rcList.top, SRCCOPY);
4959 /* FIXME: Shouldn't need to do this */
4960 oldClrTextBk = infoPtr->clrTextBk;
4961 oldClrText = infoPtr->clrText;
4963 infoPtr->cditemmode = CDRF_DODEFAULT;
4965 GetClientRect(infoPtr->hwndSelf, &rcClient);
4966 customdraw_fill(&nmlvcd, infoPtr, hdc, &rcClient, 0);
4967 cdmode = notify_customdraw(infoPtr, CDDS_PREPAINT, &nmlvcd);
4968 if (cdmode & CDRF_SKIPDEFAULT) goto enddraw;
4969 prepaint_setup(infoPtr, hdc, &nmlvcd, FALSE);
4971 /* Use these colors to draw the items */
4972 infoPtr->clrTextBk = nmlvcd.clrTextBk;
4973 infoPtr->clrText = nmlvcd.clrText;
4975 /* nothing to draw */
4976 if(infoPtr->nItemCount == 0) goto enddraw;
4978 /* figure out what we need to draw */
4979 iterator_visibleitems(&i, infoPtr, hdc);
4980 range = iterator_range(&i);
4982 /* send cache hint notification */
4983 if (infoPtr->dwStyle & LVS_OWNERDATA)
4987 ZeroMemory(&nmlv, sizeof(NMLVCACHEHINT));
4988 nmlv.iFrom = range.lower;
4989 nmlv.iTo = range.upper - 1;
4990 notify_hdr(infoPtr, LVN_ODCACHEHINT, &nmlv.hdr);
4993 if ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (infoPtr->uView == LV_VIEW_DETAILS))
4994 LISTVIEW_RefreshOwnerDraw(infoPtr, &i, hdc, cdmode);
4997 if (infoPtr->uView == LV_VIEW_DETAILS)
4998 LISTVIEW_RefreshReport(infoPtr, &i, hdc, cdmode);
4999 else /* LV_VIEW_LIST, LV_VIEW_ICON or LV_VIEW_SMALLICON */
5000 LISTVIEW_RefreshList(infoPtr, &i, hdc, cdmode);
5002 /* if we have a focus rect and it's visible, draw it */
5003 if (infoPtr->bFocus && range.lower <= infoPtr->nFocusedItem &&
5004 (range.upper - 1) >= infoPtr->nFocusedItem)
5005 LISTVIEW_DrawFocusRect(infoPtr, hdc);
5007 iterator_destroy(&i);
5010 /* For LVS_EX_GRIDLINES go and draw lines */
5011 /* This includes the case where there were *no* items */
5012 if ((infoPtr->uView == LV_VIEW_DETAILS) && infoPtr->dwLvExStyle & LVS_EX_GRIDLINES)
5013 LISTVIEW_RefreshReportGrid(infoPtr, hdc);
5015 /* Draw marquee rectangle if appropriate */
5016 if (infoPtr->bMarqueeSelect)
5017 DrawFocusRect(hdc, &infoPtr->marqueeDrawRect);
5019 if (cdmode & CDRF_NOTIFYPOSTPAINT)
5020 notify_postpaint(infoPtr, &nmlvcd);
5022 infoPtr->clrTextBk = oldClrTextBk;
5023 infoPtr->clrText = oldClrText;
5026 BitBlt(hdcOrig, infoPtr->rcList.left, infoPtr->rcList.top,
5027 infoPtr->rcList.right - infoPtr->rcList.left,
5028 infoPtr->rcList.bottom - infoPtr->rcList.top,
5029 hdc, infoPtr->rcList.left, infoPtr->rcList.top, SRCCOPY);
5034 SelectObject(hdc, hOldFont);
5035 SetBkMode(hdc, oldBkMode);
5036 SetBkColor(hdc, oldBkColor);
5037 SetTextColor(hdc, oldTextColor);
5040 infoPtr->bIsDrawing = FALSE;
5046 * Calculates the approximate width and height of a given number of items.
5049 * [I] infoPtr : valid pointer to the listview structure
5050 * [I] nItemCount : number of items
5051 * [I] wWidth : width
5052 * [I] wHeight : height
5055 * Returns a DWORD. The width in the low word and the height in high word.
5057 static DWORD LISTVIEW_ApproximateViewRect(const LISTVIEW_INFO *infoPtr, INT nItemCount,
5058 WORD wWidth, WORD wHeight)
5060 INT nItemCountPerColumn = 1;
5061 INT nColumnCount = 0;
5062 DWORD dwViewRect = 0;
5064 if (nItemCount == -1)
5065 nItemCount = infoPtr->nItemCount;
5067 if (infoPtr->uView == LV_VIEW_LIST)
5069 if (wHeight == 0xFFFF)
5071 /* use current height */
5072 wHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
5075 if (wHeight < infoPtr->nItemHeight)
5076 wHeight = infoPtr->nItemHeight;
5080 if (infoPtr->nItemHeight > 0)
5082 nItemCountPerColumn = wHeight / infoPtr->nItemHeight;
5083 if (nItemCountPerColumn == 0)
5084 nItemCountPerColumn = 1;
5086 if (nItemCount % nItemCountPerColumn != 0)
5087 nColumnCount = nItemCount / nItemCountPerColumn;
5089 nColumnCount = nItemCount / nItemCountPerColumn + 1;
5093 /* Microsoft padding magic */
5094 wHeight = nItemCountPerColumn * infoPtr->nItemHeight + 2;
5095 wWidth = nColumnCount * infoPtr->nItemWidth + 2;
5097 dwViewRect = MAKELONG(wWidth, wHeight);
5099 else if (infoPtr->uView == LV_VIEW_DETAILS)
5103 if (infoPtr->nItemCount > 0)
5105 LISTVIEW_GetItemBox(infoPtr, 0, &rcBox);
5106 wWidth = rcBox.right - rcBox.left;
5107 wHeight = (rcBox.bottom - rcBox.top) * nItemCount;
5111 /* use current height and width */
5112 if (wHeight == 0xffff)
5113 wHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
5114 if (wWidth == 0xffff)
5115 wWidth = infoPtr->rcList.right - infoPtr->rcList.left;
5118 dwViewRect = MAKELONG(wWidth, wHeight);
5120 else if (infoPtr->uView == LV_VIEW_ICON)
5126 nItemWidth = infoPtr->iconSpacing.cx;
5127 nItemHeight = infoPtr->iconSpacing.cy;
5129 if (nItemCount == -1)
5130 nItemCount = infoPtr->nItemCount;
5132 if (wWidth == 0xffff)
5133 wWidth = infoPtr->rcList.right - infoPtr->rcList.left;
5135 if (wWidth < nItemWidth)
5136 wWidth = nItemWidth;
5138 cols = wWidth / nItemWidth;
5139 if (cols > nItemCount)
5146 rows = nItemCount / cols;
5147 if (nItemCount % cols)
5153 wHeight = (nItemHeight * rows)+2;
5154 wWidth = (nItemWidth * cols)+2;
5156 dwViewRect = MAKELONG(wWidth, wHeight);
5158 else if (infoPtr->uView == LV_VIEW_SMALLICON)
5159 FIXME("uView == LV_VIEW_SMALLICON: not implemented\n");
5166 * Cancel edit label with saving item text.
5169 * [I] infoPtr : valid pointer to the listview structure
5172 * Always returns TRUE.
5174 static LRESULT LISTVIEW_CancelEditLabel(LISTVIEW_INFO *infoPtr)
5176 if (infoPtr->hwndEdit)
5178 /* handle value will be lost after LISTVIEW_EndEditLabelT */
5179 HWND edit = infoPtr->hwndEdit;
5181 LISTVIEW_EndEditLabelT(infoPtr, TRUE, IsWindowUnicode(infoPtr->hwndEdit));
5182 SendMessageW(edit, WM_CLOSE, 0, 0);
5190 * Create a drag image list for the specified item.
5193 * [I] infoPtr : valid pointer to the listview structure
5194 * [I] iItem : index of item
5195 * [O] lppt : Upper-left corner of the image
5198 * Returns a handle to the image list if successful, NULL otherwise.
5200 static HIMAGELIST LISTVIEW_CreateDragImage(LISTVIEW_INFO *infoPtr, INT iItem, LPPOINT lppt)
5206 HBITMAP hbmp, hOldbmp;
5207 HIMAGELIST dragList = 0;
5208 TRACE("iItem=%d Count=%d\n", iItem, infoPtr->nItemCount);
5210 if (iItem < 0 || iItem >= infoPtr->nItemCount || !lppt)
5213 rcItem.left = LVIR_BOUNDS;
5214 if (!LISTVIEW_GetItemRect(infoPtr, iItem, &rcItem))
5217 lppt->x = rcItem.left;
5218 lppt->y = rcItem.top;
5220 size.cx = rcItem.right - rcItem.left;
5221 size.cy = rcItem.bottom - rcItem.top;
5223 hdcOrig = GetDC(infoPtr->hwndSelf);
5224 hdc = CreateCompatibleDC(hdcOrig);
5225 hbmp = CreateCompatibleBitmap(hdcOrig, size.cx, size.cy);
5226 hOldbmp = SelectObject(hdc, hbmp);
5228 rcItem.left = rcItem.top = 0;
5229 rcItem.right = size.cx;
5230 rcItem.bottom = size.cy;
5231 FillRect(hdc, &rcItem, infoPtr->hBkBrush);
5234 if (LISTVIEW_DrawItem(infoPtr, hdc, iItem, 0, pos, infoPtr->cditemmode))
5236 dragList = ImageList_Create(size.cx, size.cy, ILC_COLOR, 10, 10);
5237 SelectObject(hdc, hOldbmp);
5238 ImageList_Add(dragList, hbmp, 0);
5241 SelectObject(hdc, hOldbmp);
5245 ReleaseDC(infoPtr->hwndSelf, hdcOrig);
5247 TRACE("ret=%p\n", dragList);
5255 * Removes all listview items and subitems.
5258 * [I] infoPtr : valid pointer to the listview structure
5264 static BOOL LISTVIEW_DeleteAllItems(LISTVIEW_INFO *infoPtr, BOOL destroy)
5267 HDPA hdpaSubItems = NULL;
5276 /* we do it directly, to avoid notifications */
5277 ranges_clear(infoPtr->selectionRanges);
5278 infoPtr->nSelectionMark = -1;
5279 infoPtr->nFocusedItem = -1;
5280 SetRectEmpty(&infoPtr->rcFocus);
5281 /* But we are supposed to leave nHotItem as is! */
5284 /* send LVN_DELETEALLITEMS notification */
5285 ZeroMemory(&nmlv, sizeof(NMLISTVIEW));
5287 bSuppress = notify_listview(infoPtr, LVN_DELETEALLITEMS, &nmlv);
5289 for (i = infoPtr->nItemCount - 1; i >= 0; i--)
5291 if (!(infoPtr->dwStyle & LVS_OWNERDATA))
5293 /* send LVN_DELETEITEM notification, if not suppressed
5294 and if it is not a virtual listview */
5295 if (!bSuppress) notify_deleteitem(infoPtr, i);
5296 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, i);
5297 lpItem = DPA_GetPtr(hdpaSubItems, 0);
5298 /* free id struct */
5299 j = DPA_GetPtrIndex(infoPtr->hdpaItemIds, lpItem->id);
5300 lpID = DPA_GetPtr(infoPtr->hdpaItemIds, j);
5301 DPA_DeletePtr(infoPtr->hdpaItemIds, j);
5303 /* both item and subitem start with ITEMHDR header */
5304 for (j = 0; j < DPA_GetPtrCount(hdpaSubItems); j++)
5306 hdrItem = DPA_GetPtr(hdpaSubItems, j);
5307 if (is_text(hdrItem->pszText)) Free(hdrItem->pszText);
5310 DPA_Destroy(hdpaSubItems);
5311 DPA_DeletePtr(infoPtr->hdpaItems, i);
5313 DPA_DeletePtr(infoPtr->hdpaPosX, i);
5314 DPA_DeletePtr(infoPtr->hdpaPosY, i);
5315 infoPtr->nItemCount --;
5320 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
5321 LISTVIEW_UpdateScroll(infoPtr);
5323 LISTVIEW_InvalidateList(infoPtr);
5330 * Scrolls, and updates the columns, when a column is changing width.
5333 * [I] infoPtr : valid pointer to the listview structure
5334 * [I] nColumn : column to scroll
5335 * [I] dx : amount of scroll, in pixels
5340 static void LISTVIEW_ScrollColumns(LISTVIEW_INFO *infoPtr, INT nColumn, INT dx)
5342 COLUMN_INFO *lpColumnInfo;
5348 if (nColumn < 0 || DPA_GetPtrCount(infoPtr->hdpaColumns) < 1) return;
5349 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, min(nColumn, DPA_GetPtrCount(infoPtr->hdpaColumns) - 1));
5350 rcCol = lpColumnInfo->rcHeader;
5351 if (nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns))
5352 rcCol.left = rcCol.right;
5354 /* adjust the other columns */
5355 hdi.mask = HDI_ORDER;
5356 if (SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nColumn, (LPARAM)&hdi))
5358 INT nOrder = hdi.iOrder;
5359 for (nCol = 0; nCol < DPA_GetPtrCount(infoPtr->hdpaColumns); nCol++)
5361 hdi.mask = HDI_ORDER;
5362 SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nCol, (LPARAM)&hdi);
5363 if (hdi.iOrder >= nOrder) {
5364 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, nCol);
5365 lpColumnInfo->rcHeader.left += dx;
5366 lpColumnInfo->rcHeader.right += dx;
5371 /* do not update screen if not in report mode */
5372 if (!is_redrawing(infoPtr) || infoPtr->uView != LV_VIEW_DETAILS) return;
5374 /* Need to reset the item width when inserting a new column */
5375 infoPtr->nItemWidth += dx;
5377 LISTVIEW_UpdateScroll(infoPtr);
5378 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
5380 /* scroll to cover the deleted column, and invalidate for redraw */
5381 rcOld = infoPtr->rcList;
5382 rcOld.left = ptOrigin.x + rcCol.left + dx;
5383 ScrollWindowEx(infoPtr->hwndSelf, dx, 0, &rcOld, &rcOld, 0, 0, SW_ERASE | SW_INVALIDATE);
5388 * Removes a column from the listview control.
5391 * [I] infoPtr : valid pointer to the listview structure
5392 * [I] nColumn : column index
5398 static BOOL LISTVIEW_DeleteColumn(LISTVIEW_INFO *infoPtr, INT nColumn)
5402 TRACE("nColumn=%d\n", nColumn);
5404 if (nColumn < 0 || DPA_GetPtrCount(infoPtr->hdpaColumns) == 0
5405 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
5407 /* While the MSDN specifically says that column zero should not be deleted,
5408 what actually happens is that the column itself is deleted but no items or subitems
5412 LISTVIEW_GetHeaderRect(infoPtr, nColumn, &rcCol);
5414 if (!SendMessageW(infoPtr->hwndHeader, HDM_DELETEITEM, nColumn, 0))
5417 Free(DPA_GetPtr(infoPtr->hdpaColumns, nColumn));
5418 DPA_DeletePtr(infoPtr->hdpaColumns, nColumn);
5420 if (!(infoPtr->dwStyle & LVS_OWNERDATA) && nColumn)
5422 SUBITEM_INFO *lpSubItem, *lpDelItem;
5424 INT nItem, nSubItem, i;
5426 for (nItem = 0; nItem < infoPtr->nItemCount; nItem++)
5428 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, nItem);
5431 for (i = 1; i < DPA_GetPtrCount(hdpaSubItems); i++)
5433 lpSubItem = DPA_GetPtr(hdpaSubItems, i);
5434 if (lpSubItem->iSubItem == nColumn)
5437 lpDelItem = lpSubItem;
5439 else if (lpSubItem->iSubItem > nColumn)
5441 lpSubItem->iSubItem--;
5445 /* if we found our subitem, zapp it */
5449 if (is_text(lpDelItem->hdr.pszText))
5450 Free(lpDelItem->hdr.pszText);
5455 /* free dpa memory */
5456 DPA_DeletePtr(hdpaSubItems, nSubItem);
5461 /* update the other column info */
5462 LISTVIEW_UpdateItemSize(infoPtr);
5463 if(DPA_GetPtrCount(infoPtr->hdpaColumns) == 0)
5464 LISTVIEW_InvalidateList(infoPtr);
5466 LISTVIEW_ScrollColumns(infoPtr, nColumn, -(rcCol.right - rcCol.left));
5473 * Invalidates the listview after an item's insertion or deletion.
5476 * [I] infoPtr : valid pointer to the listview structure
5477 * [I] nItem : item index
5478 * [I] dir : -1 if deleting, 1 if inserting
5483 static void LISTVIEW_ScrollOnInsert(LISTVIEW_INFO *infoPtr, INT nItem, INT dir)
5485 INT nPerCol, nItemCol, nItemRow;
5489 /* if we don't refresh, what's the point of scrolling? */
5490 if (!is_redrawing(infoPtr)) return;
5492 assert (abs(dir) == 1);
5494 /* arrange icons if autoarrange is on */
5495 if (is_autoarrange(infoPtr))
5497 BOOL arrange = TRUE;
5498 if (dir < 0 && nItem >= infoPtr->nItemCount) arrange = FALSE;
5499 if (dir > 0 && nItem == infoPtr->nItemCount - 1) arrange = FALSE;
5500 if (arrange) LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
5503 /* scrollbars need updating */
5504 LISTVIEW_UpdateScroll(infoPtr);
5506 /* figure out the item's position */
5507 if (infoPtr->uView == LV_VIEW_DETAILS)
5508 nPerCol = infoPtr->nItemCount + 1;
5509 else if (infoPtr->uView == LV_VIEW_LIST)
5510 nPerCol = LISTVIEW_GetCountPerColumn(infoPtr);
5511 else /* LV_VIEW_ICON, or LV_VIEW_SMALLICON */
5514 nItemCol = nItem / nPerCol;
5515 nItemRow = nItem % nPerCol;
5516 LISTVIEW_GetOrigin(infoPtr, &Origin);
5518 /* move the items below up a slot */
5519 rcScroll.left = nItemCol * infoPtr->nItemWidth;
5520 rcScroll.top = nItemRow * infoPtr->nItemHeight;
5521 rcScroll.right = rcScroll.left + infoPtr->nItemWidth;
5522 rcScroll.bottom = nPerCol * infoPtr->nItemHeight;
5523 OffsetRect(&rcScroll, Origin.x, Origin.y);
5524 TRACE("rcScroll=%s, dx=%d\n", wine_dbgstr_rect(&rcScroll), dir * infoPtr->nItemHeight);
5525 if (IntersectRect(&rcScroll, &rcScroll, &infoPtr->rcList))
5527 TRACE("Scrolling rcScroll=%s, rcList=%s\n", wine_dbgstr_rect(&rcScroll), wine_dbgstr_rect(&infoPtr->rcList));
5528 ScrollWindowEx(infoPtr->hwndSelf, 0, dir * infoPtr->nItemHeight,
5529 &rcScroll, &rcScroll, 0, 0, SW_ERASE | SW_INVALIDATE);
5532 /* report has only that column, so we're done */
5533 if (infoPtr->uView == LV_VIEW_DETAILS) return;
5535 /* now for LISTs, we have to deal with the columns to the right */
5536 rcScroll.left = (nItemCol + 1) * infoPtr->nItemWidth;
5538 rcScroll.right = (infoPtr->nItemCount / nPerCol + 1) * infoPtr->nItemWidth;
5539 rcScroll.bottom = nPerCol * infoPtr->nItemHeight;
5540 OffsetRect(&rcScroll, Origin.x, Origin.y);
5541 if (IntersectRect(&rcScroll, &rcScroll, &infoPtr->rcList))
5542 ScrollWindowEx(infoPtr->hwndSelf, 0, dir * infoPtr->nItemHeight,
5543 &rcScroll, &rcScroll, 0, 0, SW_ERASE | SW_INVALIDATE);
5548 * Removes an item from the listview control.
5551 * [I] infoPtr : valid pointer to the listview structure
5552 * [I] nItem : item index
5558 static BOOL LISTVIEW_DeleteItem(LISTVIEW_INFO *infoPtr, INT nItem)
5561 const BOOL is_icon = (infoPtr->uView == LV_VIEW_SMALLICON || infoPtr->uView == LV_VIEW_ICON);
5563 TRACE("(nItem=%d)\n", nItem);
5565 if (nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
5567 /* remove selection, and focus */
5569 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
5570 LISTVIEW_SetItemState(infoPtr, nItem, &item);
5572 /* send LVN_DELETEITEM notification. */
5573 if (!notify_deleteitem(infoPtr, nItem)) return FALSE;
5575 /* we need to do this here, because we'll be deleting stuff */
5577 LISTVIEW_InvalidateItem(infoPtr, nItem);
5579 if (!(infoPtr->dwStyle & LVS_OWNERDATA))
5587 hdpaSubItems = DPA_DeletePtr(infoPtr->hdpaItems, nItem);
5588 lpItem = DPA_GetPtr(hdpaSubItems, 0);
5590 /* free id struct */
5591 i = DPA_GetPtrIndex(infoPtr->hdpaItemIds, lpItem->id);
5592 lpID = DPA_GetPtr(infoPtr->hdpaItemIds, i);
5593 DPA_DeletePtr(infoPtr->hdpaItemIds, i);
5595 for (i = 0; i < DPA_GetPtrCount(hdpaSubItems); i++)
5597 hdrItem = DPA_GetPtr(hdpaSubItems, i);
5598 if (is_text(hdrItem->pszText)) Free(hdrItem->pszText);
5601 DPA_Destroy(hdpaSubItems);
5606 DPA_DeletePtr(infoPtr->hdpaPosX, nItem);
5607 DPA_DeletePtr(infoPtr->hdpaPosY, nItem);
5610 infoPtr->nItemCount--;
5611 LISTVIEW_ShiftIndices(infoPtr, nItem, -1);
5613 /* now is the invalidation fun */
5615 LISTVIEW_ScrollOnInsert(infoPtr, nItem, -1);
5622 * Callback implementation for editlabel control
5625 * [I] infoPtr : valid pointer to the listview structure
5626 * [I] storeText : store edit box text as item text
5627 * [I] isW : TRUE if psxText is Unicode, FALSE if it's ANSI
5633 static BOOL LISTVIEW_EndEditLabelT(LISTVIEW_INFO *infoPtr, BOOL storeText, BOOL isW)
5635 HWND hwndSelf = infoPtr->hwndSelf;
5636 WCHAR szDispText[DISP_TEXT_SIZE] = { 0 };
5637 NMLVDISPINFOW dispInfo;
5638 INT editedItem = infoPtr->nEditLabelItem;
5640 WCHAR *pszText = NULL;
5645 DWORD len = isW ? GetWindowTextLengthW(infoPtr->hwndEdit) : GetWindowTextLengthA(infoPtr->hwndEdit);
5649 if ((pszText = Alloc((len+1) * (isW ? sizeof(WCHAR) : sizeof(CHAR)))))
5651 if (isW) GetWindowTextW(infoPtr->hwndEdit, pszText, len+1);
5652 else GetWindowTextA(infoPtr->hwndEdit, (CHAR*)pszText, len+1);
5657 TRACE("(pszText=%s, isW=%d)\n", debugtext_t(pszText, isW), isW);
5659 infoPtr->nEditLabelItem = -1;
5660 infoPtr->hwndEdit = 0;
5662 ZeroMemory(&dispInfo, sizeof(dispInfo));
5663 dispInfo.item.mask = LVIF_PARAM | LVIF_STATE | LVIF_TEXT;
5664 dispInfo.item.iItem = editedItem;
5665 dispInfo.item.iSubItem = 0;
5666 dispInfo.item.stateMask = ~0;
5667 dispInfo.item.pszText = szDispText;
5668 dispInfo.item.cchTextMax = DISP_TEXT_SIZE;
5669 if (!LISTVIEW_GetItemT(infoPtr, &dispInfo.item, isW))
5676 bSame = (lstrcmpW(dispInfo.item.pszText, pszText) == 0);
5679 LPWSTR tmp = textdupTtoW(pszText, FALSE);
5680 bSame = (lstrcmpW(dispInfo.item.pszText, tmp) == 0);
5681 textfreeT(tmp, FALSE);
5684 /* add the text from the edit in */
5685 dispInfo.item.mask |= LVIF_TEXT;
5686 dispInfo.item.pszText = bSame ? NULL : pszText;
5687 dispInfo.item.cchTextMax = bSame ? 0 : textlenT(pszText, isW);
5689 /* Do we need to update the Item Text */
5690 if (!notify_dispinfoT(infoPtr, LVN_ENDLABELEDITW, &dispInfo, isW))
5695 if (!IsWindow(hwndSelf))
5700 if (!pszText) return TRUE;
5707 if (!(infoPtr->dwStyle & LVS_OWNERDATA))
5709 HDPA hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, editedItem);
5710 ITEM_INFO* lpItem = DPA_GetPtr(hdpaSubItems, 0);
5711 if (lpItem && lpItem->hdr.pszText == LPSTR_TEXTCALLBACKW)
5713 LISTVIEW_InvalidateItem(infoPtr, editedItem);
5719 ZeroMemory(&dispInfo, sizeof(dispInfo));
5720 dispInfo.item.mask = LVIF_TEXT;
5721 dispInfo.item.iItem = editedItem;
5722 dispInfo.item.iSubItem = 0;
5723 dispInfo.item.pszText = pszText;
5724 dispInfo.item.cchTextMax = textlenT(pszText, isW);
5725 res = LISTVIEW_SetItemT(infoPtr, &dispInfo.item, isW);
5735 * Subclassed edit control windproc function
5738 * [I] hwnd : the edit window handle
5739 * [I] uMsg : the message that is to be processed
5740 * [I] wParam : first message parameter
5741 * [I] lParam : second message parameter
5742 * [I] isW : TRUE if input is Unicode
5747 static LRESULT EditLblWndProcT(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL isW)
5749 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongPtrW(GetParent(hwnd), 0);
5752 TRACE("(hwnd=%p, uMsg=%x, wParam=%lx, lParam=%lx, isW=%d)\n",
5753 hwnd, uMsg, wParam, lParam, isW);
5758 return DLGC_WANTARROWS | DLGC_WANTALLKEYS;
5762 WNDPROC editProc = infoPtr->EditWndProc;
5763 infoPtr->EditWndProc = 0;
5764 SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (DWORD_PTR)editProc);
5765 return CallWindowProcT(editProc, hwnd, uMsg, wParam, lParam, isW);
5769 if (VK_ESCAPE == (INT)wParam)
5774 else if (VK_RETURN == (INT)wParam)
5778 return CallWindowProcT(infoPtr->EditWndProc, hwnd, uMsg, wParam, lParam, isW);
5782 if (infoPtr->hwndEdit)
5783 LISTVIEW_EndEditLabelT(infoPtr, save, isW);
5785 SendMessageW(hwnd, WM_CLOSE, 0, 0);
5791 * Subclassed edit control Unicode windproc function
5794 * [I] hwnd : the edit window handle
5795 * [I] uMsg : the message that is to be processed
5796 * [I] wParam : first message parameter
5797 * [I] lParam : second message parameter
5801 static LRESULT CALLBACK EditLblWndProcW(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
5803 return EditLblWndProcT(hwnd, uMsg, wParam, lParam, TRUE);
5808 * Subclassed edit control ANSI windproc function
5811 * [I] hwnd : the edit window handle
5812 * [I] uMsg : the message that is to be processed
5813 * [I] wParam : first message parameter
5814 * [I] lParam : second message parameter
5818 static LRESULT CALLBACK EditLblWndProcA(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
5820 return EditLblWndProcT(hwnd, uMsg, wParam, lParam, FALSE);
5825 * Creates a subclassed edit control
5828 * [I] infoPtr : valid pointer to the listview structure
5829 * [I] text : initial text for the edit
5830 * [I] style : the window style
5831 * [I] isW : TRUE if input is Unicode
5835 static HWND CreateEditLabelT(LISTVIEW_INFO *infoPtr, LPCWSTR text, BOOL isW)
5837 static const DWORD style = WS_CHILDWINDOW|WS_CLIPSIBLINGS|ES_LEFT|ES_AUTOHSCROLL|WS_BORDER|WS_VISIBLE;
5838 HINSTANCE hinst = (HINSTANCE)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_HINSTANCE);
5841 TRACE("(%p, text=%s, isW=%d)\n", infoPtr, debugtext_t(text, isW), isW);
5843 /* Window will be resized and positioned after LVN_BEGINLABELEDIT */
5845 hedit = CreateWindowW(WC_EDITW, text, style, 0, 0, 0, 0, infoPtr->hwndSelf, 0, hinst, 0);
5847 hedit = CreateWindowA(WC_EDITA, (LPCSTR)text, style, 0, 0, 0, 0, infoPtr->hwndSelf, 0, hinst, 0);
5849 if (!hedit) return 0;
5851 infoPtr->EditWndProc = (WNDPROC)
5852 (isW ? SetWindowLongPtrW(hedit, GWLP_WNDPROC, (DWORD_PTR)EditLblWndProcW) :
5853 SetWindowLongPtrA(hedit, GWLP_WNDPROC, (DWORD_PTR)EditLblWndProcA) );
5855 SendMessageW(hedit, WM_SETFONT, (WPARAM)infoPtr->hFont, FALSE);
5862 * Begin in place editing of specified list view item
5865 * [I] infoPtr : valid pointer to the listview structure
5866 * [I] nItem : item index
5867 * [I] isW : TRUE if it's a Unicode req, FALSE if ASCII
5873 static HWND LISTVIEW_EditLabelT(LISTVIEW_INFO *infoPtr, INT nItem, BOOL isW)
5875 WCHAR szDispText[DISP_TEXT_SIZE] = { 0 };
5876 HWND hwndSelf = infoPtr->hwndSelf;
5877 NMLVDISPINFOW dispInfo;
5878 HFONT hOldFont = NULL;
5884 TRACE("(nItem=%d, isW=%d)\n", nItem, isW);
5886 if (~infoPtr->dwStyle & LVS_EDITLABELS) return 0;
5888 /* remove existing edit box */
5889 if (infoPtr->hwndEdit)
5891 SetFocus(infoPtr->hwndSelf);
5892 infoPtr->hwndEdit = 0;
5895 if (nItem < 0 || nItem >= infoPtr->nItemCount) return 0;
5897 infoPtr->nEditLabelItem = nItem;
5899 LISTVIEW_SetSelection(infoPtr, nItem);
5900 LISTVIEW_SetItemFocus(infoPtr, nItem);
5901 LISTVIEW_InvalidateItem(infoPtr, nItem);
5903 rect.left = LVIR_LABEL;
5904 if (!LISTVIEW_GetItemRect(infoPtr, nItem, &rect)) return 0;
5906 ZeroMemory(&dispInfo, sizeof(dispInfo));
5907 dispInfo.item.mask = LVIF_PARAM | LVIF_STATE | LVIF_TEXT;
5908 dispInfo.item.iItem = nItem;
5909 dispInfo.item.iSubItem = 0;
5910 dispInfo.item.stateMask = ~0;
5911 dispInfo.item.pszText = szDispText;
5912 dispInfo.item.cchTextMax = DISP_TEXT_SIZE;
5913 if (!LISTVIEW_GetItemT(infoPtr, &dispInfo.item, isW)) return 0;
5915 infoPtr->hwndEdit = CreateEditLabelT(infoPtr, dispInfo.item.pszText, isW);
5916 if (!infoPtr->hwndEdit) return 0;
5918 if (notify_dispinfoT(infoPtr, LVN_BEGINLABELEDITW, &dispInfo, isW))
5920 if (!IsWindow(hwndSelf))
5922 SendMessageW(infoPtr->hwndEdit, WM_CLOSE, 0, 0);
5923 infoPtr->hwndEdit = 0;
5927 /* position and display edit box */
5928 hdc = GetDC(infoPtr->hwndSelf);
5930 /* select the font to get appropriate metric dimensions */
5932 hOldFont = SelectObject(hdc, infoPtr->hFont);
5934 /* get string length in pixels */
5935 TRACE("text=%s\n", debugtext_t(dispInfo.item.pszText, isW));
5938 GetTextExtentPoint32W(hdc, dispInfo.item.pszText, lstrlenW(dispInfo.item.pszText), &sz);
5941 const CHAR *textA = (CHAR*)dispInfo.item.pszText;
5942 GetTextExtentPoint32A(hdc, textA, lstrlenA(textA), &sz);
5945 /* add extra spacing for the next character */
5946 GetTextMetricsW(hdc, &tm);
5947 sz.cx += tm.tmMaxCharWidth * 2;
5950 SelectObject(hdc, hOldFont);
5952 ReleaseDC(infoPtr->hwndSelf, hdc);
5954 sz.cy = rect.bottom - rect.top + 2;
5957 TRACE("moving edit=(%d,%d)-(%d,%d)\n", rect.left, rect.top, sz.cx, sz.cy);
5958 MoveWindow(infoPtr->hwndEdit, rect.left, rect.top, sz.cx, sz.cy, FALSE);
5959 ShowWindow(infoPtr->hwndEdit, SW_NORMAL);
5960 SetFocus(infoPtr->hwndEdit);
5961 SendMessageW(infoPtr->hwndEdit, EM_SETSEL, 0, -1);
5962 return infoPtr->hwndEdit;
5968 * Ensures the specified item is visible, scrolling into view if necessary.
5971 * [I] infoPtr : valid pointer to the listview structure
5972 * [I] nItem : item index
5973 * [I] bPartial : partially or entirely visible
5979 static BOOL LISTVIEW_EnsureVisible(LISTVIEW_INFO *infoPtr, INT nItem, BOOL bPartial)
5981 INT nScrollPosHeight = 0;
5982 INT nScrollPosWidth = 0;
5983 INT nHorzAdjust = 0;
5984 INT nVertAdjust = 0;
5987 RECT rcItem, rcTemp;
5989 rcItem.left = LVIR_BOUNDS;
5990 if (!LISTVIEW_GetItemRect(infoPtr, nItem, &rcItem)) return FALSE;
5992 if (bPartial && IntersectRect(&rcTemp, &infoPtr->rcList, &rcItem)) return TRUE;
5994 if (rcItem.left < infoPtr->rcList.left || rcItem.right > infoPtr->rcList.right)
5996 /* scroll left/right, but in LV_VIEW_DETAILS mode */
5997 if (infoPtr->uView == LV_VIEW_LIST)
5998 nScrollPosWidth = infoPtr->nItemWidth;
5999 else if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
6000 nScrollPosWidth = 1;
6002 if (rcItem.left < infoPtr->rcList.left)
6005 if (infoPtr->uView != LV_VIEW_DETAILS) nHorzDiff = rcItem.left - infoPtr->rcList.left;
6010 if (infoPtr->uView != LV_VIEW_DETAILS) nHorzDiff = rcItem.right - infoPtr->rcList.right;
6014 if (rcItem.top < infoPtr->rcList.top || rcItem.bottom > infoPtr->rcList.bottom)
6016 /* scroll up/down, but not in LVS_LIST mode */
6017 if (infoPtr->uView == LV_VIEW_DETAILS)
6018 nScrollPosHeight = infoPtr->nItemHeight;
6019 else if ((infoPtr->uView == LV_VIEW_ICON) || (infoPtr->uView == LV_VIEW_SMALLICON))
6020 nScrollPosHeight = 1;
6022 if (rcItem.top < infoPtr->rcList.top)
6025 if (infoPtr->uView != LV_VIEW_LIST) nVertDiff = rcItem.top - infoPtr->rcList.top;
6030 if (infoPtr->uView != LV_VIEW_LIST) nVertDiff = rcItem.bottom - infoPtr->rcList.bottom;
6034 if (!nScrollPosWidth && !nScrollPosHeight) return TRUE;
6036 if (nScrollPosWidth)
6038 INT diff = nHorzDiff / nScrollPosWidth;
6039 if (nHorzDiff % nScrollPosWidth) diff += nHorzAdjust;
6040 LISTVIEW_HScroll(infoPtr, SB_INTERNAL, diff, 0);
6043 if (nScrollPosHeight)
6045 INT diff = nVertDiff / nScrollPosHeight;
6046 if (nVertDiff % nScrollPosHeight) diff += nVertAdjust;
6047 LISTVIEW_VScroll(infoPtr, SB_INTERNAL, diff);
6055 * Searches for an item with specific characteristics.
6058 * [I] hwnd : window handle
6059 * [I] nStart : base item index
6060 * [I] lpFindInfo : item information to look for
6063 * SUCCESS : index of item
6066 static INT LISTVIEW_FindItemW(const LISTVIEW_INFO *infoPtr, INT nStart,
6067 const LVFINDINFOW *lpFindInfo)
6069 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
6070 BOOL bWrap = FALSE, bNearest = FALSE;
6071 INT nItem = nStart + 1, nLast = infoPtr->nItemCount, nNearestItem = -1;
6072 ULONG xdist, ydist, dist, mindist = 0x7fffffff;
6073 POINT Position, Destination;
6076 /* Search in virtual listviews should be done by application, not by
6077 listview control, so we just send LVN_ODFINDITEMW and return the result */
6078 if (infoPtr->dwStyle & LVS_OWNERDATA)
6082 nmlv.iStart = nStart;
6083 nmlv.lvfi = *lpFindInfo;
6084 return notify_hdr(infoPtr, LVN_ODFINDITEMW, (LPNMHDR)&nmlv.hdr);
6087 if (!lpFindInfo || nItem < 0) return -1;
6090 if (lpFindInfo->flags & (LVFI_STRING | LVFI_PARTIAL) ||
6091 lpFindInfo->flags & LVFI_SUBSTRING)
6093 lvItem.mask |= LVIF_TEXT;
6094 lvItem.pszText = szDispText;
6095 lvItem.cchTextMax = DISP_TEXT_SIZE;
6098 if (lpFindInfo->flags & LVFI_WRAP)
6101 if ((lpFindInfo->flags & LVFI_NEARESTXY) &&
6102 (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON))
6107 LISTVIEW_GetOrigin(infoPtr, &Origin);
6108 Destination.x = lpFindInfo->pt.x - Origin.x;
6109 Destination.y = lpFindInfo->pt.y - Origin.y;
6110 switch(lpFindInfo->vkDirection)
6112 case VK_DOWN: Destination.y += infoPtr->nItemHeight; break;
6113 case VK_UP: Destination.y -= infoPtr->nItemHeight; break;
6114 case VK_RIGHT: Destination.x += infoPtr->nItemWidth; break;
6115 case VK_LEFT: Destination.x -= infoPtr->nItemWidth; break;
6116 case VK_HOME: Destination.x = Destination.y = 0; break;
6117 case VK_NEXT: Destination.y += infoPtr->rcList.bottom - infoPtr->rcList.top; break;
6118 case VK_PRIOR: Destination.y -= infoPtr->rcList.bottom - infoPtr->rcList.top; break;
6120 LISTVIEW_GetAreaRect(infoPtr, &rcArea);
6121 Destination.x = rcArea.right;
6122 Destination.y = rcArea.bottom;
6124 default: ERR("Unknown vkDirection=%d\n", lpFindInfo->vkDirection);
6128 else Destination.x = Destination.y = 0;
6130 /* if LVFI_PARAM is specified, all other flags are ignored */
6131 if (lpFindInfo->flags & LVFI_PARAM)
6133 lvItem.mask |= LVIF_PARAM;
6135 lvItem.mask &= ~LVIF_TEXT;
6139 for (; nItem < nLast; nItem++)
6141 lvItem.iItem = nItem;
6142 lvItem.iSubItem = 0;
6143 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) continue;
6145 if (lvItem.mask & LVIF_PARAM)
6147 if (lpFindInfo->lParam == lvItem.lParam)
6153 if (lvItem.mask & LVIF_TEXT)
6155 if (lpFindInfo->flags & (LVFI_PARTIAL | LVFI_SUBSTRING))
6157 WCHAR *p = strstrW(lvItem.pszText, lpFindInfo->psz);
6158 if (!p || p != lvItem.pszText) continue;
6162 if (lstrcmpW(lvItem.pszText, lpFindInfo->psz) != 0) continue;
6166 if (!bNearest) return nItem;
6168 /* This is very inefficient. To do a good job here,
6169 * we need a sorted array of (x,y) item positions */
6170 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
6172 /* compute the distance^2 to the destination */
6173 xdist = Destination.x - Position.x;
6174 ydist = Destination.y - Position.y;
6175 dist = xdist * xdist + ydist * ydist;
6177 /* remember the distance, and item if it's closer */
6181 nNearestItem = nItem;
6188 nLast = min(nStart + 1, infoPtr->nItemCount);
6193 return nNearestItem;
6198 * Searches for an item with specific characteristics.
6201 * [I] hwnd : window handle
6202 * [I] nStart : base item index
6203 * [I] lpFindInfo : item information to look for
6206 * SUCCESS : index of item
6209 static INT LISTVIEW_FindItemA(const LISTVIEW_INFO *infoPtr, INT nStart,
6210 const LVFINDINFOA *lpFindInfo)
6212 BOOL hasText = lpFindInfo->flags & (LVFI_STRING | LVFI_PARTIAL) ||
6213 lpFindInfo->flags & LVFI_SUBSTRING;
6218 memcpy(&fiw, lpFindInfo, sizeof(fiw));
6219 if (hasText) fiw.psz = strW = textdupTtoW((LPCWSTR)lpFindInfo->psz, FALSE);
6220 res = LISTVIEW_FindItemW(infoPtr, nStart, &fiw);
6221 textfreeT(strW, FALSE);
6227 * Retrieves column attributes.
6230 * [I] infoPtr : valid pointer to the listview structure
6231 * [I] nColumn : column index
6232 * [IO] lpColumn : column information
6233 * [I] isW : if TRUE, then lpColumn is a LPLVCOLUMNW
6234 * otherwise it is in fact a LPLVCOLUMNA
6240 static BOOL LISTVIEW_GetColumnT(const LISTVIEW_INFO *infoPtr, INT nColumn, LPLVCOLUMNW lpColumn, BOOL isW)
6242 COLUMN_INFO *lpColumnInfo;
6245 if (!lpColumn || nColumn < 0 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
6246 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, nColumn);
6248 /* initialize memory */
6249 ZeroMemory(&hdi, sizeof(hdi));
6251 if (lpColumn->mask & LVCF_TEXT)
6253 hdi.mask |= HDI_TEXT;
6254 hdi.pszText = lpColumn->pszText;
6255 hdi.cchTextMax = lpColumn->cchTextMax;
6258 if (lpColumn->mask & LVCF_IMAGE)
6259 hdi.mask |= HDI_IMAGE;
6261 if (lpColumn->mask & LVCF_ORDER)
6262 hdi.mask |= HDI_ORDER;
6264 if (lpColumn->mask & LVCF_SUBITEM)
6265 hdi.mask |= HDI_LPARAM;
6267 if (!SendMessageW(infoPtr->hwndHeader, isW ? HDM_GETITEMW : HDM_GETITEMA, nColumn, (LPARAM)&hdi)) return FALSE;
6269 if (lpColumn->mask & LVCF_FMT)
6270 lpColumn->fmt = lpColumnInfo->fmt;
6272 if (lpColumn->mask & LVCF_WIDTH)
6273 lpColumn->cx = lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left;
6275 if (lpColumn->mask & LVCF_IMAGE)
6276 lpColumn->iImage = hdi.iImage;
6278 if (lpColumn->mask & LVCF_ORDER)
6279 lpColumn->iOrder = hdi.iOrder;
6281 if (lpColumn->mask & LVCF_SUBITEM)
6282 lpColumn->iSubItem = hdi.lParam;
6284 if (lpColumn->mask & LVCF_MINWIDTH)
6285 lpColumn->cxMin = lpColumnInfo->cxMin;
6291 static BOOL LISTVIEW_GetColumnOrderArray(const LISTVIEW_INFO *infoPtr, INT iCount, LPINT lpiArray)
6293 TRACE("iCount=%d, lpiArray=%p\n", iCount, lpiArray);
6298 return SendMessageW(infoPtr->hwndHeader, HDM_GETORDERARRAY, iCount, (LPARAM)lpiArray);
6303 * Retrieves the column width.
6306 * [I] infoPtr : valid pointer to the listview structure
6307 * [I] int : column index
6310 * SUCCESS : column width
6313 static INT LISTVIEW_GetColumnWidth(const LISTVIEW_INFO *infoPtr, INT nColumn)
6315 INT nColumnWidth = 0;
6318 TRACE("nColumn=%d\n", nColumn);
6320 /* we have a 'column' in LIST and REPORT mode only */
6321 switch(infoPtr->uView)
6324 nColumnWidth = infoPtr->nItemWidth;
6326 case LV_VIEW_DETAILS:
6327 /* We are not using LISTVIEW_GetHeaderRect as this data is updated only after a HDN_ITEMCHANGED.
6328 * There is an application that subclasses the listview, calls LVM_GETCOLUMNWIDTH in the
6329 * HDN_ITEMCHANGED handler and goes into infinite recursion if it receives old data.
6331 * TODO: should we do the same in LVM_GETCOLUMN?
6333 hdItem.mask = HDI_WIDTH;
6334 if (!SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nColumn, (LPARAM)&hdItem))
6336 WARN("(%p): HDM_GETITEMW failed for item %d\n", infoPtr->hwndSelf, nColumn);
6339 nColumnWidth = hdItem.cxy;
6343 TRACE("nColumnWidth=%d\n", nColumnWidth);
6344 return nColumnWidth;
6349 * In list or report display mode, retrieves the number of items that can fit
6350 * vertically in the visible area. In icon or small icon display mode,
6351 * retrieves the total number of visible items.
6354 * [I] infoPtr : valid pointer to the listview structure
6357 * Number of fully visible items.
6359 static INT LISTVIEW_GetCountPerPage(const LISTVIEW_INFO *infoPtr)
6361 switch (infoPtr->uView)
6364 case LV_VIEW_SMALLICON:
6365 return infoPtr->nItemCount;
6366 case LV_VIEW_DETAILS:
6367 return LISTVIEW_GetCountPerColumn(infoPtr);
6369 return LISTVIEW_GetCountPerRow(infoPtr) * LISTVIEW_GetCountPerColumn(infoPtr);
6377 * Retrieves an image list handle.
6380 * [I] infoPtr : valid pointer to the listview structure
6381 * [I] nImageList : image list identifier
6384 * SUCCESS : image list handle
6387 static HIMAGELIST LISTVIEW_GetImageList(const LISTVIEW_INFO *infoPtr, INT nImageList)
6391 case LVSIL_NORMAL: return infoPtr->himlNormal;
6392 case LVSIL_SMALL: return infoPtr->himlSmall;
6393 case LVSIL_STATE: return infoPtr->himlState;
6394 case LVSIL_GROUPHEADER:
6395 FIXME("LVSIL_GROUPHEADER not supported\n");
6398 WARN("got unknown imagelist index - %d\n", nImageList);
6403 /* LISTVIEW_GetISearchString */
6407 * Retrieves item attributes.
6410 * [I] hwnd : window handle
6411 * [IO] lpLVItem : item info
6412 * [I] isW : if TRUE, then lpLVItem is a LPLVITEMW,
6413 * if FALSE, then lpLVItem is a LPLVITEMA.
6416 * This is the internal 'GetItem' interface -- it tries to
6417 * be smart and avoid text copies, if possible, by modifying
6418 * lpLVItem->pszText to point to the text string. Please note
6419 * that this is not always possible (e.g. OWNERDATA), so on
6420 * entry you *must* supply valid values for pszText, and cchTextMax.
6421 * The only difference to the documented interface is that upon
6422 * return, you should use *only* the lpLVItem->pszText, rather than
6423 * the buffer pointer you provided on input. Most code already does
6424 * that, so it's not a problem.
6425 * For the two cases when the text must be copied (that is,
6426 * for LVM_GETITEM, and LVM_GETITEMTEXT), use LISTVIEW_GetItemExtT.
6432 static BOOL LISTVIEW_GetItemT(const LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem, BOOL isW)
6434 ITEMHDR callbackHdr = { LPSTR_TEXTCALLBACKW, I_IMAGECALLBACK };
6435 NMLVDISPINFOW dispInfo;
6441 TRACE("(lpLVItem=%s, isW=%d)\n", debuglvitem_t(lpLVItem, isW), isW);
6443 if (!lpLVItem || lpLVItem->iItem < 0 || lpLVItem->iItem >= infoPtr->nItemCount)
6446 if (lpLVItem->mask == 0) return TRUE;
6448 /* make a local copy */
6449 isubitem = lpLVItem->iSubItem;
6451 /* a quick optimization if all we're asked is the focus state
6452 * these queries are worth optimising since they are common,
6453 * and can be answered in constant time, without the heavy accesses */
6454 if ( (lpLVItem->mask == LVIF_STATE) && (lpLVItem->stateMask == LVIS_FOCUSED) &&
6455 !(infoPtr->uCallbackMask & LVIS_FOCUSED) )
6457 lpLVItem->state = 0;
6458 if (infoPtr->nFocusedItem == lpLVItem->iItem)
6459 lpLVItem->state |= LVIS_FOCUSED;
6463 ZeroMemory(&dispInfo, sizeof(dispInfo));
6465 /* if the app stores all the data, handle it separately */
6466 if (infoPtr->dwStyle & LVS_OWNERDATA)
6468 dispInfo.item.state = 0;
6470 /* apparently, we should not callback for lParam in LVS_OWNERDATA */
6471 if ((lpLVItem->mask & ~(LVIF_STATE | LVIF_PARAM)) ||
6472 ((lpLVItem->mask & LVIF_STATE) && (infoPtr->uCallbackMask & lpLVItem->stateMask)))
6474 UINT mask = lpLVItem->mask;
6476 /* NOTE: copy only fields which we _know_ are initialized, some apps
6477 * depend on the uninitialized fields being 0 */
6478 dispInfo.item.mask = lpLVItem->mask & ~LVIF_PARAM;
6479 dispInfo.item.iItem = lpLVItem->iItem;
6480 dispInfo.item.iSubItem = isubitem;
6481 if (lpLVItem->mask & LVIF_TEXT)
6483 if (lpLVItem->mask & LVIF_NORECOMPUTE)
6485 dispInfo.item.mask &= ~(LVIF_TEXT | LVIF_NORECOMPUTE);
6488 dispInfo.item.pszText = lpLVItem->pszText;
6489 dispInfo.item.cchTextMax = lpLVItem->cchTextMax;
6492 if (lpLVItem->mask & LVIF_STATE)
6493 dispInfo.item.stateMask = lpLVItem->stateMask & infoPtr->uCallbackMask;
6494 /* could be zeroed on LVIF_NORECOMPUTE case */
6495 if (dispInfo.item.mask != 0)
6497 notify_dispinfoT(infoPtr, LVN_GETDISPINFOW, &dispInfo, isW);
6498 dispInfo.item.stateMask = lpLVItem->stateMask;
6499 if (lpLVItem->mask & (LVIF_GROUPID|LVIF_COLUMNS))
6501 /* full size structure expected - _WIN32IE >= 0x560 */
6502 *lpLVItem = dispInfo.item;
6504 else if (lpLVItem->mask & LVIF_INDENT)
6506 /* indent member expected - _WIN32IE >= 0x300 */
6507 memcpy(lpLVItem, &dispInfo.item, offsetof( LVITEMW, iGroupId ));
6511 /* minimal structure expected */
6512 memcpy(lpLVItem, &dispInfo.item, offsetof( LVITEMW, iIndent ));
6514 lpLVItem->mask = mask;
6515 TRACE(" getdispinfo(1):lpLVItem=%s\n", debuglvitem_t(lpLVItem, isW));
6519 /* make sure lParam is zeroed out */
6520 if (lpLVItem->mask & LVIF_PARAM) lpLVItem->lParam = 0;
6522 /* callback marked pointer required here */
6523 if ((lpLVItem->mask & LVIF_TEXT) && (lpLVItem->mask & LVIF_NORECOMPUTE))
6524 lpLVItem->pszText = LPSTR_TEXTCALLBACKW;
6526 /* we store only a little state, so if we're not asked, we're done */
6527 if (!(lpLVItem->mask & LVIF_STATE) || isubitem) return TRUE;
6529 /* if focus is handled by us, report it */
6530 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_FOCUSED )
6532 lpLVItem->state &= ~LVIS_FOCUSED;
6533 if (infoPtr->nFocusedItem == lpLVItem->iItem)
6534 lpLVItem->state |= LVIS_FOCUSED;
6537 /* and do the same for selection, if we handle it */
6538 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_SELECTED )
6540 lpLVItem->state &= ~LVIS_SELECTED;
6541 if (ranges_contain(infoPtr->selectionRanges, lpLVItem->iItem))
6542 lpLVItem->state |= LVIS_SELECTED;
6548 /* find the item and subitem structures before we proceed */
6549 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, lpLVItem->iItem);
6550 lpItem = DPA_GetPtr(hdpaSubItems, 0);
6555 SUBITEM_INFO *lpSubItem = LISTVIEW_GetSubItemPtr(hdpaSubItems, isubitem);
6556 pItemHdr = lpSubItem ? &lpSubItem->hdr : &callbackHdr;
6559 WARN(" iSubItem invalid (%08x), ignored.\n", isubitem);
6564 pItemHdr = &lpItem->hdr;
6566 /* Do we need to query the state from the app? */
6567 if ((lpLVItem->mask & LVIF_STATE) && infoPtr->uCallbackMask && isubitem == 0)
6569 dispInfo.item.mask |= LVIF_STATE;
6570 dispInfo.item.stateMask = infoPtr->uCallbackMask;
6573 /* Do we need to enquire about the image? */
6574 if ((lpLVItem->mask & LVIF_IMAGE) && pItemHdr->iImage == I_IMAGECALLBACK &&
6575 (isubitem == 0 || (infoPtr->dwLvExStyle & LVS_EX_SUBITEMIMAGES)))
6577 dispInfo.item.mask |= LVIF_IMAGE;
6578 dispInfo.item.iImage = I_IMAGECALLBACK;
6581 /* Only items support indentation */
6582 if ((lpLVItem->mask & LVIF_INDENT) && lpItem->iIndent == I_INDENTCALLBACK &&
6585 dispInfo.item.mask |= LVIF_INDENT;
6586 dispInfo.item.iIndent = I_INDENTCALLBACK;
6589 /* Apps depend on calling back for text if it is NULL or LPSTR_TEXTCALLBACKW */
6590 if ((lpLVItem->mask & LVIF_TEXT) && !(lpLVItem->mask & LVIF_NORECOMPUTE) &&
6591 !is_text(pItemHdr->pszText))
6593 dispInfo.item.mask |= LVIF_TEXT;
6594 dispInfo.item.pszText = lpLVItem->pszText;
6595 dispInfo.item.cchTextMax = lpLVItem->cchTextMax;
6596 if (dispInfo.item.pszText && dispInfo.item.cchTextMax > 0)
6597 *dispInfo.item.pszText = '\0';
6600 /* If we don't have all the requested info, query the application */
6601 if (dispInfo.item.mask != 0)
6603 dispInfo.item.iItem = lpLVItem->iItem;
6604 dispInfo.item.iSubItem = lpLVItem->iSubItem; /* yes: the original subitem */
6605 dispInfo.item.lParam = lpItem->lParam;
6606 notify_dispinfoT(infoPtr, LVN_GETDISPINFOW, &dispInfo, isW);
6607 TRACE(" getdispinfo(2):item=%s\n", debuglvitem_t(&dispInfo.item, isW));
6610 /* we should not store values for subitems */
6611 if (isubitem) dispInfo.item.mask &= ~LVIF_DI_SETITEM;
6613 /* Now, handle the iImage field */
6614 if (dispInfo.item.mask & LVIF_IMAGE)
6616 lpLVItem->iImage = dispInfo.item.iImage;
6617 if ((dispInfo.item.mask & LVIF_DI_SETITEM) && pItemHdr->iImage == I_IMAGECALLBACK)
6618 pItemHdr->iImage = dispInfo.item.iImage;
6620 else if (lpLVItem->mask & LVIF_IMAGE)
6622 if(isubitem == 0 || (infoPtr->dwLvExStyle & LVS_EX_SUBITEMIMAGES))
6623 lpLVItem->iImage = pItemHdr->iImage;
6625 lpLVItem->iImage = 0;
6628 /* The pszText field */
6629 if (dispInfo.item.mask & LVIF_TEXT)
6631 if ((dispInfo.item.mask & LVIF_DI_SETITEM) && pItemHdr->pszText)
6632 textsetptrT(&pItemHdr->pszText, dispInfo.item.pszText, isW);
6634 lpLVItem->pszText = dispInfo.item.pszText;
6636 else if (lpLVItem->mask & LVIF_TEXT)
6638 /* if LVN_GETDISPINFO's disabled with LVIF_NORECOMPUTE return callback placeholder */
6639 if (isW || !is_text(pItemHdr->pszText)) lpLVItem->pszText = pItemHdr->pszText;
6640 else textcpynT(lpLVItem->pszText, isW, pItemHdr->pszText, TRUE, lpLVItem->cchTextMax);
6643 /* Next is the lParam field */
6644 if (dispInfo.item.mask & LVIF_PARAM)
6646 lpLVItem->lParam = dispInfo.item.lParam;
6647 if ((dispInfo.item.mask & LVIF_DI_SETITEM))
6648 lpItem->lParam = dispInfo.item.lParam;
6650 else if (lpLVItem->mask & LVIF_PARAM)
6651 lpLVItem->lParam = lpItem->lParam;
6653 /* if this is a subitem, we're done */
6654 if (isubitem) return TRUE;
6656 /* ... the state field (this one is different due to uCallbackmask) */
6657 if (lpLVItem->mask & LVIF_STATE)
6659 lpLVItem->state = lpItem->state & lpLVItem->stateMask;
6660 if (dispInfo.item.mask & LVIF_STATE)
6662 lpLVItem->state &= ~dispInfo.item.stateMask;
6663 lpLVItem->state |= (dispInfo.item.state & dispInfo.item.stateMask);
6665 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_FOCUSED )
6667 lpLVItem->state &= ~LVIS_FOCUSED;
6668 if (infoPtr->nFocusedItem == lpLVItem->iItem)
6669 lpLVItem->state |= LVIS_FOCUSED;
6671 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_SELECTED )
6673 lpLVItem->state &= ~LVIS_SELECTED;
6674 if (ranges_contain(infoPtr->selectionRanges, lpLVItem->iItem))
6675 lpLVItem->state |= LVIS_SELECTED;
6679 /* and last, but not least, the indent field */
6680 if (dispInfo.item.mask & LVIF_INDENT)
6682 lpLVItem->iIndent = dispInfo.item.iIndent;
6683 if ((dispInfo.item.mask & LVIF_DI_SETITEM) && lpItem->iIndent == I_INDENTCALLBACK)
6684 lpItem->iIndent = dispInfo.item.iIndent;
6686 else if (lpLVItem->mask & LVIF_INDENT)
6688 lpLVItem->iIndent = lpItem->iIndent;
6696 * Retrieves item attributes.
6699 * [I] hwnd : window handle
6700 * [IO] lpLVItem : item info
6701 * [I] isW : if TRUE, then lpLVItem is a LPLVITEMW,
6702 * if FALSE, then lpLVItem is a LPLVITEMA.
6705 * This is the external 'GetItem' interface -- it properly copies
6706 * the text in the provided buffer.
6712 static BOOL LISTVIEW_GetItemExtT(const LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem, BOOL isW)
6717 if (!lpLVItem || lpLVItem->iItem < 0 || lpLVItem->iItem >= infoPtr->nItemCount)
6720 pszText = lpLVItem->pszText;
6721 bResult = LISTVIEW_GetItemT(infoPtr, lpLVItem, isW);
6722 if (bResult && lpLVItem->pszText != pszText)
6724 if (lpLVItem->pszText != LPSTR_TEXTCALLBACKW)
6725 textcpynT(pszText, isW, lpLVItem->pszText, isW, lpLVItem->cchTextMax);
6727 pszText = LPSTR_TEXTCALLBACKW;
6729 lpLVItem->pszText = pszText;
6737 * Retrieves the position (upper-left) of the listview control item.
6738 * Note that for LVS_ICON style, the upper-left is that of the icon
6739 * and not the bounding box.
6742 * [I] infoPtr : valid pointer to the listview structure
6743 * [I] nItem : item index
6744 * [O] lpptPosition : coordinate information
6750 static BOOL LISTVIEW_GetItemPosition(const LISTVIEW_INFO *infoPtr, INT nItem, LPPOINT lpptPosition)
6754 TRACE("(nItem=%d, lpptPosition=%p)\n", nItem, lpptPosition);
6756 if (!lpptPosition || nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
6758 LISTVIEW_GetOrigin(infoPtr, &Origin);
6759 LISTVIEW_GetItemOrigin(infoPtr, nItem, lpptPosition);
6761 if (infoPtr->uView == LV_VIEW_ICON)
6763 lpptPosition->x += (infoPtr->nItemWidth - infoPtr->iconSize.cx) / 2;
6764 lpptPosition->y += ICON_TOP_PADDING;
6766 lpptPosition->x += Origin.x;
6767 lpptPosition->y += Origin.y;
6769 TRACE (" lpptPosition=%s\n", wine_dbgstr_point(lpptPosition));
6776 * Retrieves the bounding rectangle for a listview control item.
6779 * [I] infoPtr : valid pointer to the listview structure
6780 * [I] nItem : item index
6781 * [IO] lprc : bounding rectangle coordinates
6782 * lprc->left specifies the portion of the item for which the bounding
6783 * rectangle will be retrieved.
6785 * LVIR_BOUNDS Returns the bounding rectangle of the entire item,
6786 * including the icon and label.
6789 * * Experiment shows that native control returns:
6790 * * width = min (48, length of text line)
6791 * * .left = position.x - (width - iconsize.cx)/2
6792 * * .right = .left + width
6793 * * height = #lines of text * ntmHeight + icon height + 8
6794 * * .top = position.y - 2
6795 * * .bottom = .top + height
6796 * * separation between items .y = itemSpacing.cy - height
6797 * * .x = itemSpacing.cx - width
6798 * LVIR_ICON Returns the bounding rectangle of the icon or small icon.
6801 * * Experiment shows that native control returns:
6802 * * width = iconSize.cx + 16
6803 * * .left = position.x - (width - iconsize.cx)/2
6804 * * .right = .left + width
6805 * * height = iconSize.cy + 4
6806 * * .top = position.y - 2
6807 * * .bottom = .top + height
6808 * * separation between items .y = itemSpacing.cy - height
6809 * * .x = itemSpacing.cx - width
6810 * LVIR_LABEL Returns the bounding rectangle of the item text.
6813 * * Experiment shows that native control returns:
6814 * * width = text length
6815 * * .left = position.x - width/2
6816 * * .right = .left + width
6817 * * height = ntmH * linecount + 2
6818 * * .top = position.y + iconSize.cy + 6
6819 * * .bottom = .top + height
6820 * * separation between items .y = itemSpacing.cy - height
6821 * * .x = itemSpacing.cx - width
6822 * LVIR_SELECTBOUNDS Returns the union of the LVIR_ICON and LVIR_LABEL
6823 * rectangles, but excludes columns in report view.
6830 * Note that the bounding rectangle of the label in the LVS_ICON view depends
6831 * upon whether the window has the focus currently and on whether the item
6832 * is the one with the focus. Ensure that the control's record of which
6833 * item has the focus agrees with the items' records.
6835 static BOOL LISTVIEW_GetItemRect(const LISTVIEW_INFO *infoPtr, INT nItem, LPRECT lprc)
6837 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
6838 BOOL doLabel = TRUE, oversizedBox = FALSE;
6839 POINT Position, Origin;
6843 TRACE("(hwnd=%p, nItem=%d, lprc=%p)\n", infoPtr->hwndSelf, nItem, lprc);
6845 if (!lprc || nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
6847 LISTVIEW_GetOrigin(infoPtr, &Origin);
6848 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
6850 /* Be smart and try to figure out the minimum we have to do */
6851 if (lprc->left == LVIR_ICON) doLabel = FALSE;
6852 if (infoPtr->uView == LV_VIEW_DETAILS && lprc->left == LVIR_BOUNDS) doLabel = FALSE;
6853 if (infoPtr->uView == LV_VIEW_ICON && lprc->left != LVIR_ICON &&
6854 infoPtr->bFocus && LISTVIEW_GetItemState(infoPtr, nItem, LVIS_FOCUSED))
6855 oversizedBox = TRUE;
6857 /* get what we need from the item before hand, so we make
6858 * only one request. This can speed up things, if data
6859 * is stored on the app side */
6861 if (infoPtr->uView == LV_VIEW_DETAILS) lvItem.mask |= LVIF_INDENT;
6862 if (doLabel) lvItem.mask |= LVIF_TEXT;
6863 lvItem.iItem = nItem;
6864 lvItem.iSubItem = 0;
6865 lvItem.pszText = szDispText;
6866 lvItem.cchTextMax = DISP_TEXT_SIZE;
6867 if (lvItem.mask && !LISTVIEW_GetItemW(infoPtr, &lvItem)) return FALSE;
6868 /* we got the state already up, simulate it here, to avoid a reget */
6869 if (infoPtr->uView == LV_VIEW_ICON && (lprc->left != LVIR_ICON))
6871 lvItem.mask |= LVIF_STATE;
6872 lvItem.stateMask = LVIS_FOCUSED;
6873 lvItem.state = (oversizedBox ? LVIS_FOCUSED : 0);
6876 if (infoPtr->uView == LV_VIEW_DETAILS && (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) && lprc->left == LVIR_SELECTBOUNDS)
6877 lprc->left = LVIR_BOUNDS;
6883 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, NULL, lprc, NULL, NULL);
6887 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, NULL, NULL, NULL, lprc);
6891 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, lprc, NULL, NULL, NULL, NULL);
6894 case LVIR_SELECTBOUNDS:
6895 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, lprc, NULL, NULL, NULL);
6899 WARN("Unknown value: %d\n", lprc->left);
6903 if (infoPtr->uView == LV_VIEW_DETAILS)
6905 if (mode != LVIR_BOUNDS)
6906 OffsetRect(lprc, Origin.x + LISTVIEW_GetColumnInfo(infoPtr, 0)->rcHeader.left,
6907 Position.y + Origin.y);
6909 OffsetRect(lprc, Origin.x, Position.y + Origin.y);
6912 OffsetRect(lprc, Position.x + Origin.x, Position.y + Origin.y);
6914 TRACE(" rect=%s\n", wine_dbgstr_rect(lprc));
6921 * Retrieves the spacing between listview control items.
6924 * [I] infoPtr : valid pointer to the listview structure
6925 * [IO] lprc : rectangle to receive the output
6926 * on input, lprc->top = nSubItem
6927 * lprc->left = LVIR_ICON | LVIR_BOUNDS | LVIR_LABEL
6929 * NOTE: for subItem = 0, we should return the bounds of the _entire_ item,
6930 * not only those of the first column.
6931 * Fortunately, LISTVIEW_GetItemMetrics does the right thing.
6937 static BOOL LISTVIEW_GetSubItemRect(const LISTVIEW_INFO *infoPtr, INT nItem, LPRECT lprc)
6939 POINT Position, Origin;
6943 if (!lprc) return FALSE;
6945 nColumn = lprc->top;
6947 TRACE("(nItem=%d, nSubItem=%d, type=%d)\n", nItem, lprc->top, lprc->left);
6948 /* On WinNT, a subitem of '0' calls LISTVIEW_GetItemRect */
6950 return LISTVIEW_GetItemRect(infoPtr, nItem, lprc);
6952 if (infoPtr->uView != LV_VIEW_DETAILS) return FALSE;
6954 /* special case for header items */
6957 if (lprc->left != LVIR_BOUNDS)
6959 FIXME("Only LVIR_BOUNDS is implemented for header, got %d\n", lprc->left);
6963 if (infoPtr->hwndHeader)
6964 return SendMessageW(infoPtr->hwndHeader, HDM_GETITEMRECT, lprc->top, (LPARAM)lprc);
6967 memset(lprc, 0, sizeof(RECT));
6972 if (!LISTVIEW_GetItemPosition(infoPtr, nItem, &Position)) return FALSE;
6973 LISTVIEW_GetOrigin(infoPtr, &Origin);
6975 if (nColumn < 0 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
6978 lvItem.iItem = nItem;
6979 lvItem.iSubItem = nColumn;
6984 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, NULL, lprc, NULL, NULL);
6989 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, lprc, NULL, NULL, NULL, NULL);
6993 ERR("Unknown bounds=%d\n", lprc->left);
6997 OffsetRect(lprc, Origin.x, Position.y);
6998 TRACE("return rect %s\n", wine_dbgstr_rect(lprc));
7005 * Retrieves the spacing between listview control items.
7008 * [I] infoPtr : valid pointer to the listview structure
7009 * [I] bSmall : flag for small or large icon
7012 * Horizontal + vertical spacing
7014 static LONG LISTVIEW_GetItemSpacing(const LISTVIEW_INFO *infoPtr, BOOL bSmall)
7020 lResult = MAKELONG(infoPtr->iconSpacing.cx, infoPtr->iconSpacing.cy);
7024 if (infoPtr->uView == LV_VIEW_ICON)
7025 lResult = MAKELONG(DEFAULT_COLUMN_WIDTH, GetSystemMetrics(SM_CXSMICON)+HEIGHT_PADDING);
7027 lResult = MAKELONG(infoPtr->nItemWidth, infoPtr->nItemHeight);
7034 * Retrieves the state of a listview control item.
7037 * [I] infoPtr : valid pointer to the listview structure
7038 * [I] nItem : item index
7039 * [I] uMask : state mask
7042 * State specified by the mask.
7044 static UINT LISTVIEW_GetItemState(const LISTVIEW_INFO *infoPtr, INT nItem, UINT uMask)
7048 if (nItem < 0 || nItem >= infoPtr->nItemCount) return 0;
7050 lvItem.iItem = nItem;
7051 lvItem.iSubItem = 0;
7052 lvItem.mask = LVIF_STATE;
7053 lvItem.stateMask = uMask;
7054 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return 0;
7056 return lvItem.state & uMask;
7061 * Retrieves the text of a listview control item or subitem.
7064 * [I] hwnd : window handle
7065 * [I] nItem : item index
7066 * [IO] lpLVItem : item information
7067 * [I] isW : TRUE if lpLVItem is Unicode
7070 * SUCCESS : string length
7073 static INT LISTVIEW_GetItemTextT(const LISTVIEW_INFO *infoPtr, INT nItem, LPLVITEMW lpLVItem, BOOL isW)
7075 if (!lpLVItem || nItem < 0 || nItem >= infoPtr->nItemCount) return 0;
7077 lpLVItem->mask = LVIF_TEXT;
7078 lpLVItem->iItem = nItem;
7079 if (!LISTVIEW_GetItemExtT(infoPtr, lpLVItem, isW)) return 0;
7081 return textlenT(lpLVItem->pszText, isW);
7086 * Searches for an item based on properties + relationships.
7089 * [I] infoPtr : valid pointer to the listview structure
7090 * [I] nItem : item index
7091 * [I] uFlags : relationship flag
7094 * SUCCESS : item index
7097 static INT LISTVIEW_GetNextItem(const LISTVIEW_INFO *infoPtr, INT nItem, UINT uFlags)
7100 LVFINDINFOW lvFindInfo;
7101 INT nCountPerColumn;
7105 TRACE("nItem=%d, uFlags=%x, nItemCount=%d\n", nItem, uFlags, infoPtr->nItemCount);
7106 if (nItem < -1 || nItem >= infoPtr->nItemCount) return -1;
7108 ZeroMemory(&lvFindInfo, sizeof(lvFindInfo));
7110 if (uFlags & LVNI_CUT)
7113 if (uFlags & LVNI_DROPHILITED)
7114 uMask |= LVIS_DROPHILITED;
7116 if (uFlags & LVNI_FOCUSED)
7117 uMask |= LVIS_FOCUSED;
7119 if (uFlags & LVNI_SELECTED)
7120 uMask |= LVIS_SELECTED;
7122 /* if we're asked for the focused item, that's only one,
7123 * so it's worth optimizing */
7124 if (uFlags & LVNI_FOCUSED)
7126 if ((LISTVIEW_GetItemState(infoPtr, infoPtr->nFocusedItem, uMask) & uMask) != uMask) return -1;
7127 return (infoPtr->nFocusedItem == nItem) ? -1 : infoPtr->nFocusedItem;
7130 if (uFlags & LVNI_ABOVE)
7132 if ((infoPtr->uView == LV_VIEW_LIST) || (infoPtr->uView == LV_VIEW_DETAILS))
7137 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7143 /* Special case for autoarrange - move 'til the top of a list */
7144 if (is_autoarrange(infoPtr))
7146 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
7147 while (nItem - nCountPerRow >= 0)
7149 nItem -= nCountPerRow;
7150 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7155 lvFindInfo.flags = LVFI_NEARESTXY;
7156 lvFindInfo.vkDirection = VK_UP;
7157 LISTVIEW_GetItemPosition(infoPtr, nItem, &lvFindInfo.pt);
7158 while ((nItem = LISTVIEW_FindItemW(infoPtr, nItem, &lvFindInfo)) != -1)
7160 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7165 else if (uFlags & LVNI_BELOW)
7167 if ((infoPtr->uView == LV_VIEW_LIST) || (infoPtr->uView == LV_VIEW_DETAILS))
7169 while (nItem < infoPtr->nItemCount)
7172 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7178 /* Special case for autoarrange - move 'til the bottom of a list */
7179 if (is_autoarrange(infoPtr))
7181 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
7182 while (nItem + nCountPerRow < infoPtr->nItemCount )
7184 nItem += nCountPerRow;
7185 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7190 lvFindInfo.flags = LVFI_NEARESTXY;
7191 lvFindInfo.vkDirection = VK_DOWN;
7192 LISTVIEW_GetItemPosition(infoPtr, nItem, &lvFindInfo.pt);
7193 while ((nItem = LISTVIEW_FindItemW(infoPtr, nItem, &lvFindInfo)) != -1)
7195 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7200 else if (uFlags & LVNI_TOLEFT)
7202 if (infoPtr->uView == LV_VIEW_LIST)
7204 nCountPerColumn = LISTVIEW_GetCountPerColumn(infoPtr);
7205 while (nItem - nCountPerColumn >= 0)
7207 nItem -= nCountPerColumn;
7208 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7212 else if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
7214 /* Special case for autoarrange - move 'til the beginning of a row */
7215 if (is_autoarrange(infoPtr))
7217 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
7218 while (nItem % nCountPerRow > 0)
7221 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7226 lvFindInfo.flags = LVFI_NEARESTXY;
7227 lvFindInfo.vkDirection = VK_LEFT;
7228 LISTVIEW_GetItemPosition(infoPtr, nItem, &lvFindInfo.pt);
7229 while ((nItem = LISTVIEW_FindItemW(infoPtr, nItem, &lvFindInfo)) != -1)
7231 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7236 else if (uFlags & LVNI_TORIGHT)
7238 if (infoPtr->uView == LV_VIEW_LIST)
7240 nCountPerColumn = LISTVIEW_GetCountPerColumn(infoPtr);
7241 while (nItem + nCountPerColumn < infoPtr->nItemCount)
7243 nItem += nCountPerColumn;
7244 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7248 else if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
7250 /* Special case for autoarrange - move 'til the end of a row */
7251 if (is_autoarrange(infoPtr))
7253 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
7254 while (nItem % nCountPerRow < nCountPerRow - 1 )
7257 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7262 lvFindInfo.flags = LVFI_NEARESTXY;
7263 lvFindInfo.vkDirection = VK_RIGHT;
7264 LISTVIEW_GetItemPosition(infoPtr, nItem, &lvFindInfo.pt);
7265 while ((nItem = LISTVIEW_FindItemW(infoPtr, nItem, &lvFindInfo)) != -1)
7267 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7276 /* search by index */
7277 for (i = nItem; i < infoPtr->nItemCount; i++)
7279 if ((LISTVIEW_GetItemState(infoPtr, i, uMask) & uMask) == uMask)
7287 /* LISTVIEW_GetNumberOfWorkAreas */
7291 * Retrieves the origin coordinates when in icon or small icon display mode.
7294 * [I] infoPtr : valid pointer to the listview structure
7295 * [O] lpptOrigin : coordinate information
7300 static void LISTVIEW_GetOrigin(const LISTVIEW_INFO *infoPtr, LPPOINT lpptOrigin)
7302 INT nHorzPos = 0, nVertPos = 0;
7303 SCROLLINFO scrollInfo;
7305 scrollInfo.cbSize = sizeof(SCROLLINFO);
7306 scrollInfo.fMask = SIF_POS;
7308 if (GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo))
7309 nHorzPos = scrollInfo.nPos;
7310 if (GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
7311 nVertPos = scrollInfo.nPos;
7313 TRACE("nHorzPos=%d, nVertPos=%d\n", nHorzPos, nVertPos);
7315 lpptOrigin->x = infoPtr->rcList.left;
7316 lpptOrigin->y = infoPtr->rcList.top;
7317 if (infoPtr->uView == LV_VIEW_LIST)
7318 nHorzPos *= infoPtr->nItemWidth;
7319 else if (infoPtr->uView == LV_VIEW_DETAILS)
7320 nVertPos *= infoPtr->nItemHeight;
7322 lpptOrigin->x -= nHorzPos;
7323 lpptOrigin->y -= nVertPos;
7325 TRACE(" origin=%s\n", wine_dbgstr_point(lpptOrigin));
7330 * Retrieves the width of a string.
7333 * [I] hwnd : window handle
7334 * [I] lpszText : text string to process
7335 * [I] isW : TRUE if lpszText is Unicode, FALSE otherwise
7338 * SUCCESS : string width (in pixels)
7341 static INT LISTVIEW_GetStringWidthT(const LISTVIEW_INFO *infoPtr, LPCWSTR lpszText, BOOL isW)
7346 if (is_text(lpszText))
7348 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
7349 HDC hdc = GetDC(infoPtr->hwndSelf);
7350 HFONT hOldFont = SelectObject(hdc, hFont);
7353 GetTextExtentPointW(hdc, lpszText, lstrlenW(lpszText), &stringSize);
7355 GetTextExtentPointA(hdc, (LPCSTR)lpszText, lstrlenA((LPCSTR)lpszText), &stringSize);
7356 SelectObject(hdc, hOldFont);
7357 ReleaseDC(infoPtr->hwndSelf, hdc);
7359 return stringSize.cx;
7364 * Determines which listview item is located at the specified position.
7367 * [I] infoPtr : valid pointer to the listview structure
7368 * [IO] lpht : hit test information
7369 * [I] subitem : fill out iSubItem.
7370 * [I] select : return the index only if the hit selects the item
7373 * (mm 20001022): We must not allow iSubItem to be touched, for
7374 * an app might pass only a structure with space up to iItem!
7375 * (MS Office 97 does that for instance in the file open dialog)
7378 * SUCCESS : item index
7381 static INT LISTVIEW_HitTest(const LISTVIEW_INFO *infoPtr, LPLVHITTESTINFO lpht, BOOL subitem, BOOL select)
7383 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
7384 RECT rcBox, rcBounds, rcState, rcIcon, rcLabel, rcSearch;
7385 POINT Origin, Position, opt;
7390 TRACE("(pt=%s, subitem=%d, select=%d)\n", wine_dbgstr_point(&lpht->pt), subitem, select);
7394 if (subitem) lpht->iSubItem = 0;
7396 LISTVIEW_GetOrigin(infoPtr, &Origin);
7398 /* set whole list relation flags */
7399 if (subitem && infoPtr->uView == LV_VIEW_DETAILS)
7401 /* LVM_SUBITEMHITTEST checks left bound of possible client area */
7402 if (infoPtr->rcList.left > lpht->pt.x && Origin.x < lpht->pt.x)
7403 lpht->flags |= LVHT_TOLEFT;
7405 if (lpht->pt.y < infoPtr->rcList.top && lpht->pt.y >= 0)
7406 opt.y = lpht->pt.y + infoPtr->rcList.top;
7410 if (infoPtr->rcList.bottom < opt.y)
7411 lpht->flags |= LVHT_BELOW;
7415 if (infoPtr->rcList.left > lpht->pt.x)
7416 lpht->flags |= LVHT_TOLEFT;
7417 else if (infoPtr->rcList.right < lpht->pt.x)
7418 lpht->flags |= LVHT_TORIGHT;
7420 if (infoPtr->rcList.top > lpht->pt.y)
7421 lpht->flags |= LVHT_ABOVE;
7422 else if (infoPtr->rcList.bottom < lpht->pt.y)
7423 lpht->flags |= LVHT_BELOW;
7426 /* even if item is invalid try to find subitem */
7427 if (infoPtr->uView == LV_VIEW_DETAILS && subitem)
7432 opt.x = lpht->pt.x - Origin.x;
7434 lpht->iSubItem = -1;
7435 for (j = 0; j < DPA_GetPtrCount(infoPtr->hdpaColumns); j++)
7437 pRect = &LISTVIEW_GetColumnInfo(infoPtr, j)->rcHeader;
7439 if ((opt.x >= pRect->left) && (opt.x < pRect->right))
7445 TRACE("lpht->iSubItem=%d\n", lpht->iSubItem);
7447 /* if we're outside horizontal columns bounds there's nothing to test further */
7448 if (lpht->iSubItem == -1)
7451 lpht->flags = LVHT_NOWHERE;
7456 TRACE("lpht->flags=0x%x\n", lpht->flags);
7457 if (lpht->flags) return -1;
7459 lpht->flags |= LVHT_NOWHERE;
7461 /* first deal with the large items */
7462 rcSearch.left = lpht->pt.x;
7463 rcSearch.top = lpht->pt.y;
7464 rcSearch.right = rcSearch.left + 1;
7465 rcSearch.bottom = rcSearch.top + 1;
7467 iterator_frameditems(&i, infoPtr, &rcSearch);
7468 iterator_next(&i); /* go to first item in the sequence */
7470 iterator_destroy(&i);
7472 TRACE("lpht->iItem=%d\n", iItem);
7473 if (iItem == -1) return -1;
7475 lvItem.mask = LVIF_STATE | LVIF_TEXT;
7476 if (infoPtr->uView == LV_VIEW_DETAILS) lvItem.mask |= LVIF_INDENT;
7477 lvItem.stateMask = LVIS_STATEIMAGEMASK;
7478 if (infoPtr->uView == LV_VIEW_ICON) lvItem.stateMask |= LVIS_FOCUSED;
7479 lvItem.iItem = iItem;
7480 lvItem.iSubItem = subitem ? lpht->iSubItem : 0;
7481 lvItem.pszText = szDispText;
7482 lvItem.cchTextMax = DISP_TEXT_SIZE;
7483 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return -1;
7484 if (!infoPtr->bFocus) lvItem.state &= ~LVIS_FOCUSED;
7486 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, &rcBox, NULL, &rcIcon, &rcState, &rcLabel);
7487 LISTVIEW_GetItemOrigin(infoPtr, iItem, &Position);
7488 opt.x = lpht->pt.x - Position.x - Origin.x;
7490 if (lpht->pt.y < infoPtr->rcList.top && lpht->pt.y >= 0)
7491 opt.y = lpht->pt.y - Position.y - Origin.y + infoPtr->rcList.top;
7493 opt.y = lpht->pt.y - Position.y - Origin.y;
7495 if (infoPtr->uView == LV_VIEW_DETAILS)
7498 if (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT)
7499 opt.x = lpht->pt.x - Origin.x;
7503 UnionRect(&rcBounds, &rcIcon, &rcLabel);
7504 UnionRect(&rcBounds, &rcBounds, &rcState);
7506 TRACE("rcBounds=%s\n", wine_dbgstr_rect(&rcBounds));
7507 if (!PtInRect(&rcBounds, opt)) return -1;
7509 if (PtInRect(&rcIcon, opt))
7510 lpht->flags |= LVHT_ONITEMICON;
7511 else if (PtInRect(&rcLabel, opt))
7512 lpht->flags |= LVHT_ONITEMLABEL;
7513 else if (infoPtr->himlState && PtInRect(&rcState, opt))
7514 lpht->flags |= LVHT_ONITEMSTATEICON;
7515 /* special case for LVS_EX_FULLROWSELECT */
7516 if (infoPtr->uView == LV_VIEW_DETAILS && infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT &&
7517 !(lpht->flags & LVHT_ONITEM))
7519 lpht->flags = LVHT_ONITEM | LVHT_ABOVE;
7521 if (lpht->flags & LVHT_ONITEM)
7522 lpht->flags &= ~LVHT_NOWHERE;
7523 TRACE("lpht->flags=0x%x\n", lpht->flags);
7525 if (select && !(infoPtr->uView == LV_VIEW_DETAILS &&
7526 ((infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) ||
7527 (infoPtr->dwStyle & LVS_OWNERDRAWFIXED))))
7529 if (infoPtr->uView == LV_VIEW_DETAILS)
7531 /* get main item bounds */
7532 lvItem.iSubItem = 0;
7533 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, &rcBox, NULL, &rcIcon, &rcState, &rcLabel);
7534 UnionRect(&rcBounds, &rcIcon, &rcLabel);
7535 UnionRect(&rcBounds, &rcBounds, &rcState);
7537 if (!PtInRect(&rcBounds, opt)) iItem = -1;
7539 return lpht->iItem = iItem;
7544 * Inserts a new item in the listview control.
7547 * [I] infoPtr : valid pointer to the listview structure
7548 * [I] lpLVItem : item information
7549 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
7552 * SUCCESS : new item index
7555 static INT LISTVIEW_InsertItemT(LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem, BOOL isW)
7562 BOOL is_sorted, has_changed;
7564 HWND hwndSelf = infoPtr->hwndSelf;
7566 TRACE("(lpLVItem=%s, isW=%d)\n", debuglvitem_t(lpLVItem, isW), isW);
7568 if (infoPtr->dwStyle & LVS_OWNERDATA) return infoPtr->nItemCount++;
7570 /* make sure it's an item, and not a subitem; cannot insert a subitem */
7571 if (!lpLVItem || lpLVItem->iSubItem) return -1;
7573 if (!is_assignable_item(lpLVItem, infoPtr->dwStyle)) return -1;
7575 if (!(lpItem = Alloc(sizeof(ITEM_INFO)))) return -1;
7577 /* insert item in listview control data structure */
7578 if ( !(hdpaSubItems = DPA_Create(8)) ) goto fail;
7579 if ( !DPA_SetPtr(hdpaSubItems, 0, lpItem) ) assert (FALSE);
7581 /* link with id struct */
7582 if (!(lpID = Alloc(sizeof(ITEM_ID)))) goto fail;
7584 lpID->item = hdpaSubItems;
7585 lpID->id = get_next_itemid(infoPtr);
7586 if ( DPA_InsertPtr(infoPtr->hdpaItemIds, infoPtr->nItemCount, lpID) == -1) goto fail;
7588 is_sorted = (infoPtr->dwStyle & (LVS_SORTASCENDING | LVS_SORTDESCENDING)) &&
7589 !(infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (LPSTR_TEXTCALLBACKW != lpLVItem->pszText);
7591 if (lpLVItem->iItem < 0 && !is_sorted) return -1;
7593 /* calculate new item index */
7600 while (i < infoPtr->nItemCount)
7602 hItem = DPA_GetPtr( infoPtr->hdpaItems, i);
7603 item_s = DPA_GetPtr(hItem, 0);
7605 cmpv = textcmpWT(item_s->hdr.pszText, lpLVItem->pszText, isW);
7606 if (infoPtr->dwStyle & LVS_SORTDESCENDING) cmpv *= -1;
7608 if (cmpv >= 0) break;
7614 nItem = min(lpLVItem->iItem, infoPtr->nItemCount);
7616 TRACE(" inserting at %d, sorted=%d, count=%d, iItem=%d\n", nItem, is_sorted, infoPtr->nItemCount, lpLVItem->iItem);
7617 nItem = DPA_InsertPtr( infoPtr->hdpaItems, nItem, hdpaSubItems );
7618 if (nItem == -1) goto fail;
7619 infoPtr->nItemCount++;
7621 /* shift indices first so they don't get tangled */
7622 LISTVIEW_ShiftIndices(infoPtr, nItem, 1);
7624 /* set the item attributes */
7625 if (lpLVItem->mask & (LVIF_GROUPID|LVIF_COLUMNS))
7627 /* full size structure expected - _WIN32IE >= 0x560 */
7630 else if (lpLVItem->mask & LVIF_INDENT)
7632 /* indent member expected - _WIN32IE >= 0x300 */
7633 memcpy(&item, lpLVItem, offsetof( LVITEMW, iGroupId ));
7637 /* minimal structure expected */
7638 memcpy(&item, lpLVItem, offsetof( LVITEMW, iIndent ));
7641 if (infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES)
7643 item.mask |= LVIF_STATE;
7644 item.stateMask |= LVIS_STATEIMAGEMASK;
7645 item.state &= ~LVIS_STATEIMAGEMASK;
7646 item.state |= INDEXTOSTATEIMAGEMASK(1);
7648 if (!set_main_item(infoPtr, &item, TRUE, isW, &has_changed)) goto undo;
7650 /* make room for the position, if we are in the right mode */
7651 if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
7653 if (DPA_InsertPtr(infoPtr->hdpaPosX, nItem, 0) == -1)
7655 if (DPA_InsertPtr(infoPtr->hdpaPosY, nItem, 0) == -1)
7657 DPA_DeletePtr(infoPtr->hdpaPosX, nItem);
7662 /* send LVN_INSERTITEM notification */
7663 ZeroMemory(&nmlv, sizeof(NMLISTVIEW));
7665 nmlv.lParam = lpItem->lParam;
7666 notify_listview(infoPtr, LVN_INSERTITEM, &nmlv);
7667 if (!IsWindow(hwndSelf))
7670 /* align items (set position of each item) */
7671 if (infoPtr->uView == LV_VIEW_SMALLICON || infoPtr->uView == LV_VIEW_ICON)
7675 if (infoPtr->dwStyle & LVS_ALIGNLEFT)
7676 LISTVIEW_NextIconPosLeft(infoPtr, &pt);
7678 LISTVIEW_NextIconPosTop(infoPtr, &pt);
7680 LISTVIEW_MoveIconTo(infoPtr, nItem, &pt, TRUE);
7683 /* now is the invalidation fun */
7684 LISTVIEW_ScrollOnInsert(infoPtr, nItem, 1);
7688 LISTVIEW_ShiftIndices(infoPtr, nItem, -1);
7689 DPA_DeletePtr(infoPtr->hdpaItems, nItem);
7690 infoPtr->nItemCount--;
7692 DPA_DeletePtr(hdpaSubItems, 0);
7693 DPA_Destroy (hdpaSubItems);
7700 * Checks item visibility.
7703 * [I] infoPtr : valid pointer to the listview structure
7704 * [I] nFirst : item index to check for
7707 * Item visible : TRUE
7708 * Item invisible or failure : FALSE
7710 static BOOL LISTVIEW_IsItemVisible(const LISTVIEW_INFO *infoPtr, INT nItem)
7712 POINT Origin, Position;
7717 TRACE("nItem=%d\n", nItem);
7719 if (nItem < 0 || nItem >= DPA_GetPtrCount(infoPtr->hdpaItems)) return FALSE;
7721 LISTVIEW_GetOrigin(infoPtr, &Origin);
7722 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
7723 rcItem.left = Position.x + Origin.x;
7724 rcItem.top = Position.y + Origin.y;
7725 rcItem.right = rcItem.left + infoPtr->nItemWidth;
7726 rcItem.bottom = rcItem.top + infoPtr->nItemHeight;
7728 hdc = GetDC(infoPtr->hwndSelf);
7729 if (!hdc) return FALSE;
7730 ret = RectVisible(hdc, &rcItem);
7731 ReleaseDC(infoPtr->hwndSelf, hdc);
7738 * Redraws a range of items.
7741 * [I] infoPtr : valid pointer to the listview structure
7742 * [I] nFirst : first item
7743 * [I] nLast : last item
7749 static BOOL LISTVIEW_RedrawItems(const LISTVIEW_INFO *infoPtr, INT nFirst, INT nLast)
7753 if (nLast < nFirst || min(nFirst, nLast) < 0 ||
7754 max(nFirst, nLast) >= infoPtr->nItemCount)
7757 for (i = nFirst; i <= nLast; i++)
7758 LISTVIEW_InvalidateItem(infoPtr, i);
7765 * Scroll the content of a listview.
7768 * [I] infoPtr : valid pointer to the listview structure
7769 * [I] dx : horizontal scroll amount in pixels
7770 * [I] dy : vertical scroll amount in pixels
7777 * If the control is in report view (LV_VIEW_DETAILS) the control can
7778 * be scrolled only in line increments. "dy" will be rounded to the
7779 * nearest number of pixels that are a whole line. Ex: if line height
7780 * is 16 and an 8 is passed, the list will be scrolled by 16. If a 7
7781 * is passed, then the scroll will be 0. (per MSDN 7/2002)
7783 * For: (per experimentation with native control and CSpy ListView)
7784 * LV_VIEW_ICON dy=1 = 1 pixel (vertical only)
7786 * LV_VIEW_SMALLICON dy=1 = 1 pixel (vertical only)
7788 * LV_VIEW_LIST dx=1 = 1 column (horizontal only)
7789 * but will only scroll 1 column per message
7790 * no matter what the value.
7791 * dy must be 0 or FALSE returned.
7792 * LV_VIEW_DETAILS dx=1 = 1 pixel
7796 static BOOL LISTVIEW_Scroll(LISTVIEW_INFO *infoPtr, INT dx, INT dy)
7798 switch(infoPtr->uView) {
7799 case LV_VIEW_DETAILS:
7800 dy += (dy < 0 ? -1 : 1) * infoPtr->nItemHeight/2;
7801 dy /= infoPtr->nItemHeight;
7804 if (dy != 0) return FALSE;
7811 if (dx != 0) LISTVIEW_HScroll(infoPtr, SB_INTERNAL, dx, 0);
7812 if (dy != 0) LISTVIEW_VScroll(infoPtr, SB_INTERNAL, dy);
7819 * Sets the background color.
7822 * [I] infoPtr : valid pointer to the listview structure
7823 * [I] clrBk : background color
7829 static BOOL LISTVIEW_SetBkColor(LISTVIEW_INFO *infoPtr, COLORREF clrBk)
7831 TRACE("(clrBk=%x)\n", clrBk);
7833 if(infoPtr->clrBk != clrBk) {
7834 if (infoPtr->clrBk != CLR_NONE) DeleteObject(infoPtr->hBkBrush);
7835 infoPtr->clrBk = clrBk;
7836 if (clrBk == CLR_NONE)
7837 infoPtr->hBkBrush = (HBRUSH)GetClassLongPtrW(infoPtr->hwndSelf, GCLP_HBRBACKGROUND);
7840 infoPtr->hBkBrush = CreateSolidBrush(clrBk);
7841 infoPtr->dwLvExStyle &= ~LVS_EX_TRANSPARENTBKGND;
7843 LISTVIEW_InvalidateList(infoPtr);
7849 /* LISTVIEW_SetBkImage */
7851 /*** Helper for {Insert,Set}ColumnT *only* */
7852 static void column_fill_hditem(const LISTVIEW_INFO *infoPtr, HDITEMW *lphdi, INT nColumn,
7853 const LVCOLUMNW *lpColumn, BOOL isW)
7855 if (lpColumn->mask & LVCF_FMT)
7857 /* format member is valid */
7858 lphdi->mask |= HDI_FORMAT;
7860 /* set text alignment (leftmost column must be left-aligned) */
7861 if (nColumn == 0 || (lpColumn->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_LEFT)
7862 lphdi->fmt |= HDF_LEFT;
7863 else if ((lpColumn->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_RIGHT)
7864 lphdi->fmt |= HDF_RIGHT;
7865 else if ((lpColumn->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_CENTER)
7866 lphdi->fmt |= HDF_CENTER;
7868 if (lpColumn->fmt & LVCFMT_BITMAP_ON_RIGHT)
7869 lphdi->fmt |= HDF_BITMAP_ON_RIGHT;
7871 if (lpColumn->fmt & LVCFMT_COL_HAS_IMAGES)
7873 lphdi->fmt |= HDF_IMAGE;
7874 lphdi->iImage = I_IMAGECALLBACK;
7877 if (lpColumn->fmt & LVCFMT_FIXED_WIDTH)
7878 lphdi->fmt |= HDF_FIXEDWIDTH;
7881 if (lpColumn->mask & LVCF_WIDTH)
7883 lphdi->mask |= HDI_WIDTH;
7884 if(lpColumn->cx == LVSCW_AUTOSIZE_USEHEADER)
7886 /* make it fill the remainder of the controls width */
7890 for(item_index = 0; item_index < (nColumn - 1); item_index++)
7892 LISTVIEW_GetHeaderRect(infoPtr, item_index, &rcHeader);
7893 lphdi->cxy += rcHeader.right - rcHeader.left;
7896 /* retrieve the layout of the header */
7897 GetClientRect(infoPtr->hwndSelf, &rcHeader);
7898 TRACE("start cxy=%d rcHeader=%s\n", lphdi->cxy, wine_dbgstr_rect(&rcHeader));
7900 lphdi->cxy = (rcHeader.right - rcHeader.left) - lphdi->cxy;
7903 lphdi->cxy = lpColumn->cx;
7906 if (lpColumn->mask & LVCF_TEXT)
7908 lphdi->mask |= HDI_TEXT | HDI_FORMAT;
7909 lphdi->fmt |= HDF_STRING;
7910 lphdi->pszText = lpColumn->pszText;
7911 lphdi->cchTextMax = textlenT(lpColumn->pszText, isW);
7914 if (lpColumn->mask & LVCF_IMAGE)
7916 lphdi->mask |= HDI_IMAGE;
7917 lphdi->iImage = lpColumn->iImage;
7920 if (lpColumn->mask & LVCF_ORDER)
7922 lphdi->mask |= HDI_ORDER;
7923 lphdi->iOrder = lpColumn->iOrder;
7930 * Inserts a new column.
7933 * [I] infoPtr : valid pointer to the listview structure
7934 * [I] nColumn : column index
7935 * [I] lpColumn : column information
7936 * [I] isW : TRUE if lpColumn is Unicode, FALSE otherwise
7939 * SUCCESS : new column index
7942 static INT LISTVIEW_InsertColumnT(LISTVIEW_INFO *infoPtr, INT nColumn,
7943 const LVCOLUMNW *lpColumn, BOOL isW)
7945 COLUMN_INFO *lpColumnInfo;
7949 TRACE("(nColumn=%d, lpColumn=%s, isW=%d)\n", nColumn, debuglvcolumn_t(lpColumn, isW), isW);
7951 if (!lpColumn || nColumn < 0) return -1;
7952 nColumn = min(nColumn, DPA_GetPtrCount(infoPtr->hdpaColumns));
7954 ZeroMemory(&hdi, sizeof(HDITEMW));
7955 column_fill_hditem(infoPtr, &hdi, nColumn, lpColumn, isW);
7958 * A mask not including LVCF_WIDTH turns into a mask of width, width 10
7959 * (can be seen in SPY) otherwise column never gets added.
7961 if (!(lpColumn->mask & LVCF_WIDTH)) {
7962 hdi.mask |= HDI_WIDTH;
7967 * when the iSubItem is available Windows copies it to the header lParam. It seems
7968 * to happen only in LVM_INSERTCOLUMN - not in LVM_SETCOLUMN
7970 if (lpColumn->mask & LVCF_SUBITEM)
7972 hdi.mask |= HDI_LPARAM;
7973 hdi.lParam = lpColumn->iSubItem;
7976 /* create header if not present */
7977 LISTVIEW_CreateHeader(infoPtr);
7978 if (!(LVS_NOCOLUMNHEADER & infoPtr->dwStyle) &&
7979 (infoPtr->uView == LV_VIEW_DETAILS) && (WS_VISIBLE & infoPtr->dwStyle))
7981 ShowWindow(infoPtr->hwndHeader, SW_SHOWNORMAL);
7984 /* insert item in header control */
7985 nNewColumn = SendMessageW(infoPtr->hwndHeader,
7986 isW ? HDM_INSERTITEMW : HDM_INSERTITEMA,
7987 nColumn, (LPARAM)&hdi);
7988 if (nNewColumn == -1) return -1;
7989 if (nNewColumn != nColumn) ERR("nColumn=%d, nNewColumn=%d\n", nColumn, nNewColumn);
7991 /* create our own column info */
7992 if (!(lpColumnInfo = Alloc(sizeof(COLUMN_INFO)))) goto fail;
7993 if (DPA_InsertPtr(infoPtr->hdpaColumns, nNewColumn, lpColumnInfo) == -1) goto fail;
7995 if (lpColumn->mask & LVCF_FMT) lpColumnInfo->fmt = lpColumn->fmt;
7996 if (lpColumn->mask & LVCF_MINWIDTH) lpColumnInfo->cxMin = lpColumn->cxMin;
7997 if (!SendMessageW(infoPtr->hwndHeader, HDM_GETITEMRECT, nNewColumn, (LPARAM)&lpColumnInfo->rcHeader))
8000 /* now we have to actually adjust the data */
8001 if (!(infoPtr->dwStyle & LVS_OWNERDATA) && infoPtr->nItemCount > 0)
8003 SUBITEM_INFO *lpSubItem;
8009 item.iSubItem = nNewColumn;
8010 item.mask = LVIF_TEXT | LVIF_IMAGE;
8011 item.iImage = I_IMAGECALLBACK;
8012 item.pszText = LPSTR_TEXTCALLBACKW;
8014 for (nItem = 0; nItem < infoPtr->nItemCount; nItem++)
8016 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, nItem);
8017 for (i = 1; i < DPA_GetPtrCount(hdpaSubItems); i++)
8019 lpSubItem = DPA_GetPtr(hdpaSubItems, i);
8020 if (lpSubItem->iSubItem >= nNewColumn)
8021 lpSubItem->iSubItem++;
8024 /* add new subitem for each item */
8026 set_sub_item(infoPtr, &item, isW, &changed);
8030 /* make space for the new column */
8031 LISTVIEW_ScrollColumns(infoPtr, nNewColumn + 1, lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left);
8032 LISTVIEW_UpdateItemSize(infoPtr);
8037 if (nNewColumn != -1) SendMessageW(infoPtr->hwndHeader, HDM_DELETEITEM, nNewColumn, 0);
8040 DPA_DeletePtr(infoPtr->hdpaColumns, nNewColumn);
8048 * Sets the attributes of a header item.
8051 * [I] infoPtr : valid pointer to the listview structure
8052 * [I] nColumn : column index
8053 * [I] lpColumn : column attributes
8054 * [I] isW: if TRUE, then lpColumn is a LPLVCOLUMNW, else it is a LPLVCOLUMNA
8060 static BOOL LISTVIEW_SetColumnT(const LISTVIEW_INFO *infoPtr, INT nColumn,
8061 const LVCOLUMNW *lpColumn, BOOL isW)
8063 HDITEMW hdi, hdiget;
8066 TRACE("(nColumn=%d, lpColumn=%s, isW=%d)\n", nColumn, debuglvcolumn_t(lpColumn, isW), isW);
8068 if (!lpColumn || nColumn < 0 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
8070 ZeroMemory(&hdi, sizeof(HDITEMW));
8071 if (lpColumn->mask & LVCF_FMT)
8073 hdi.mask |= HDI_FORMAT;
8074 hdiget.mask = HDI_FORMAT;
8075 if (SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nColumn, (LPARAM)&hdiget))
8076 hdi.fmt = hdiget.fmt & HDF_STRING;
8078 column_fill_hditem(infoPtr, &hdi, nColumn, lpColumn, isW);
8080 /* set header item attributes */
8081 bResult = SendMessageW(infoPtr->hwndHeader, isW ? HDM_SETITEMW : HDM_SETITEMA, nColumn, (LPARAM)&hdi);
8082 if (!bResult) return FALSE;
8084 if (lpColumn->mask & LVCF_FMT)
8086 COLUMN_INFO *lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, nColumn);
8087 INT oldFmt = lpColumnInfo->fmt;
8089 lpColumnInfo->fmt = lpColumn->fmt;
8090 if ((oldFmt ^ lpColumn->fmt) & (LVCFMT_JUSTIFYMASK | LVCFMT_IMAGE))
8092 if (infoPtr->uView == LV_VIEW_DETAILS) LISTVIEW_InvalidateColumn(infoPtr, nColumn);
8096 if (lpColumn->mask & LVCF_MINWIDTH)
8097 LISTVIEW_GetColumnInfo(infoPtr, nColumn)->cxMin = lpColumn->cxMin;
8104 * Sets the column order array
8107 * [I] infoPtr : valid pointer to the listview structure
8108 * [I] iCount : number of elements in column order array
8109 * [I] lpiArray : pointer to column order array
8115 static BOOL LISTVIEW_SetColumnOrderArray(LISTVIEW_INFO *infoPtr, INT iCount, const INT *lpiArray)
8117 TRACE("iCount %d lpiArray %p\n", iCount, lpiArray);
8119 if (!lpiArray || !IsWindow(infoPtr->hwndHeader)) return FALSE;
8121 infoPtr->colRectsDirty = TRUE;
8123 return SendMessageW(infoPtr->hwndHeader, HDM_SETORDERARRAY, iCount, (LPARAM)lpiArray);
8128 * Sets the width of a column
8131 * [I] infoPtr : valid pointer to the listview structure
8132 * [I] nColumn : column index
8133 * [I] cx : column width
8139 static BOOL LISTVIEW_SetColumnWidth(LISTVIEW_INFO *infoPtr, INT nColumn, INT cx)
8141 WCHAR szDispText[DISP_TEXT_SIZE] = { 0 };
8145 TRACE("(nColumn=%d, cx=%d\n", nColumn, cx);
8147 /* set column width only if in report or list mode */
8148 if (infoPtr->uView != LV_VIEW_DETAILS && infoPtr->uView != LV_VIEW_LIST) return FALSE;
8150 /* take care of invalid cx values */
8151 if(infoPtr->uView == LV_VIEW_DETAILS && cx < -2) cx = LVSCW_AUTOSIZE;
8152 else if (infoPtr->uView == LV_VIEW_LIST && cx < 1) return FALSE;
8154 /* resize all columns if in LV_VIEW_LIST mode */
8155 if(infoPtr->uView == LV_VIEW_LIST)
8157 infoPtr->nItemWidth = cx;
8158 LISTVIEW_InvalidateList(infoPtr);
8162 if (nColumn < 0 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
8164 if (cx == LVSCW_AUTOSIZE || (cx == LVSCW_AUTOSIZE_USEHEADER && nColumn < DPA_GetPtrCount(infoPtr->hdpaColumns) -1))
8169 lvItem.mask = LVIF_TEXT;
8171 lvItem.iSubItem = nColumn;
8172 lvItem.pszText = szDispText;
8173 lvItem.cchTextMax = DISP_TEXT_SIZE;
8174 for (; lvItem.iItem < infoPtr->nItemCount; lvItem.iItem++)
8176 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) continue;
8177 nLabelWidth = LISTVIEW_GetStringWidthT(infoPtr, lvItem.pszText, TRUE);
8178 if (max_cx < nLabelWidth) max_cx = nLabelWidth;
8180 if (infoPtr->himlSmall && (nColumn == 0 || (LISTVIEW_GetColumnInfo(infoPtr, nColumn)->fmt & LVCFMT_IMAGE)))
8181 max_cx += infoPtr->iconSize.cx;
8182 max_cx += TRAILING_LABEL_PADDING;
8185 /* autosize based on listview items width */
8186 if(cx == LVSCW_AUTOSIZE)
8188 else if(cx == LVSCW_AUTOSIZE_USEHEADER)
8190 /* if iCol is the last column make it fill the remainder of the controls width */
8191 if(nColumn == DPA_GetPtrCount(infoPtr->hdpaColumns) - 1)
8196 LISTVIEW_GetOrigin(infoPtr, &Origin);
8197 LISTVIEW_GetHeaderRect(infoPtr, nColumn, &rcHeader);
8199 cx = infoPtr->rcList.right - Origin.x - rcHeader.left;
8203 /* Despite what the MS docs say, if this is not the last
8204 column, then MS resizes the column to the width of the
8205 largest text string in the column, including headers
8206 and items. This is different from LVSCW_AUTOSIZE in that
8207 LVSCW_AUTOSIZE ignores the header string length. */
8210 /* retrieve header text */
8211 hdi.mask = HDI_TEXT;
8212 hdi.cchTextMax = DISP_TEXT_SIZE;
8213 hdi.pszText = szDispText;
8214 if (SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nColumn, (LPARAM)&hdi))
8216 HDC hdc = GetDC(infoPtr->hwndSelf);
8217 HFONT old_font = SelectObject(hdc, (HFONT)SendMessageW(infoPtr->hwndHeader, WM_GETFONT, 0, 0));
8220 if (GetTextExtentPoint32W(hdc, hdi.pszText, lstrlenW(hdi.pszText), &size))
8221 cx = size.cx + TRAILING_HEADER_PADDING;
8222 /* FIXME: Take into account the header image, if one is present */
8223 SelectObject(hdc, old_font);
8224 ReleaseDC(infoPtr->hwndSelf, hdc);
8226 cx = max (cx, max_cx);
8230 if (cx < 0) return FALSE;
8232 /* call header to update the column change */
8233 hdi.mask = HDI_WIDTH;
8234 hdi.cxy = max(cx, LISTVIEW_GetColumnInfo(infoPtr, nColumn)->cxMin);
8235 TRACE("hdi.cxy=%d\n", hdi.cxy);
8236 return SendMessageW(infoPtr->hwndHeader, HDM_SETITEMW, nColumn, (LPARAM)&hdi);
8240 * Creates the checkbox imagelist. Helper for LISTVIEW_SetExtendedListViewStyle
8243 static HIMAGELIST LISTVIEW_CreateCheckBoxIL(const LISTVIEW_INFO *infoPtr)
8246 HBITMAP hbm_im, hbm_mask, hbm_orig;
8248 HBRUSH hbr_white = GetStockObject(WHITE_BRUSH);
8249 HBRUSH hbr_black = GetStockObject(BLACK_BRUSH);
8252 himl = ImageList_Create(GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON),
8253 ILC_COLOR | ILC_MASK, 2, 2);
8254 hdc_wnd = GetDC(infoPtr->hwndSelf);
8255 hdc = CreateCompatibleDC(hdc_wnd);
8256 hbm_im = CreateCompatibleBitmap(hdc_wnd, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON));
8257 hbm_mask = CreateBitmap(GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), 1, 1, NULL);
8258 ReleaseDC(infoPtr->hwndSelf, hdc_wnd);
8260 rc.left = rc.top = 0;
8261 rc.right = GetSystemMetrics(SM_CXSMICON);
8262 rc.bottom = GetSystemMetrics(SM_CYSMICON);
8264 hbm_orig = SelectObject(hdc, hbm_mask);
8265 FillRect(hdc, &rc, hbr_white);
8266 InflateRect(&rc, -2, -2);
8267 FillRect(hdc, &rc, hbr_black);
8269 SelectObject(hdc, hbm_im);
8270 DrawFrameControl(hdc, &rc, DFC_BUTTON, DFCS_BUTTONCHECK | DFCS_MONO);
8271 SelectObject(hdc, hbm_orig);
8272 ImageList_Add(himl, hbm_im, hbm_mask);
8274 SelectObject(hdc, hbm_im);
8275 DrawFrameControl(hdc, &rc, DFC_BUTTON, DFCS_BUTTONCHECK | DFCS_MONO | DFCS_CHECKED);
8276 SelectObject(hdc, hbm_orig);
8277 ImageList_Add(himl, hbm_im, hbm_mask);
8279 DeleteObject(hbm_mask);
8280 DeleteObject(hbm_im);
8288 * Sets the extended listview style.
8291 * [I] infoPtr : valid pointer to the listview structure
8293 * [I] dwStyle : style
8296 * SUCCESS : previous style
8299 static DWORD LISTVIEW_SetExtendedListViewStyle(LISTVIEW_INFO *infoPtr, DWORD dwMask, DWORD dwExStyle)
8301 DWORD dwOldExStyle = infoPtr->dwLvExStyle;
8305 infoPtr->dwLvExStyle = (dwOldExStyle & ~dwMask) | (dwExStyle & dwMask);
8307 infoPtr->dwLvExStyle = dwExStyle;
8309 if((infoPtr->dwLvExStyle ^ dwOldExStyle) & LVS_EX_CHECKBOXES)
8311 HIMAGELIST himl = 0;
8312 if(infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES)
8315 item.mask = LVIF_STATE;
8316 item.stateMask = LVIS_STATEIMAGEMASK;
8317 item.state = INDEXTOSTATEIMAGEMASK(1);
8318 LISTVIEW_SetItemState(infoPtr, -1, &item);
8320 himl = LISTVIEW_CreateCheckBoxIL(infoPtr);
8321 if(!(infoPtr->dwStyle & LVS_SHAREIMAGELISTS))
8322 ImageList_Destroy(infoPtr->himlState);
8324 himl = LISTVIEW_SetImageList(infoPtr, LVSIL_STATE, himl);
8325 /* checkbox list replaces prevous custom list or... */
8326 if(((infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES) &&
8327 !(infoPtr->dwStyle & LVS_SHAREIMAGELISTS)) ||
8328 /* ...previous was checkbox list */
8329 (dwOldExStyle & LVS_EX_CHECKBOXES))
8330 ImageList_Destroy(himl);
8333 if((infoPtr->dwLvExStyle ^ dwOldExStyle) & LVS_EX_HEADERDRAGDROP)
8337 /* if not already created */
8338 LISTVIEW_CreateHeader(infoPtr);
8340 dwStyle = GetWindowLongW(infoPtr->hwndHeader, GWL_STYLE);
8341 if (infoPtr->dwLvExStyle & LVS_EX_HEADERDRAGDROP)
8342 dwStyle |= HDS_DRAGDROP;
8344 dwStyle &= ~HDS_DRAGDROP;
8345 SetWindowLongW(infoPtr->hwndHeader, GWL_STYLE, dwStyle);
8348 /* GRIDLINES adds decoration at top so changes sizes */
8349 if((infoPtr->dwLvExStyle ^ dwOldExStyle) & LVS_EX_GRIDLINES)
8351 LISTVIEW_UpdateSize(infoPtr);
8354 if((infoPtr->dwLvExStyle ^ dwOldExStyle) & LVS_EX_TRANSPARENTBKGND)
8356 if (infoPtr->dwLvExStyle & LVS_EX_TRANSPARENTBKGND)
8357 LISTVIEW_SetBkColor(infoPtr, CLR_NONE);
8360 if((infoPtr->dwLvExStyle ^ dwOldExStyle) & LVS_EX_HEADERINALLVIEWS)
8362 if (infoPtr->dwLvExStyle & LVS_EX_HEADERINALLVIEWS)
8363 LISTVIEW_CreateHeader(infoPtr);
8365 ShowWindow(infoPtr->hwndHeader, SW_HIDE);
8366 LISTVIEW_UpdateSize(infoPtr);
8367 LISTVIEW_UpdateScroll(infoPtr);
8370 LISTVIEW_InvalidateList(infoPtr);
8371 return dwOldExStyle;
8376 * Sets the new hot cursor used during hot tracking and hover selection.
8379 * [I] infoPtr : valid pointer to the listview structure
8380 * [I] hCursor : the new hot cursor handle
8383 * Returns the previous hot cursor
8385 static HCURSOR LISTVIEW_SetHotCursor(LISTVIEW_INFO *infoPtr, HCURSOR hCursor)
8387 HCURSOR oldCursor = infoPtr->hHotCursor;
8389 infoPtr->hHotCursor = hCursor;
8397 * Sets the hot item index.
8400 * [I] infoPtr : valid pointer to the listview structure
8401 * [I] iIndex : index
8404 * SUCCESS : previous hot item index
8405 * FAILURE : -1 (no hot item)
8407 static INT LISTVIEW_SetHotItem(LISTVIEW_INFO *infoPtr, INT iIndex)
8409 INT iOldIndex = infoPtr->nHotItem;
8411 infoPtr->nHotItem = iIndex;
8419 * Sets the amount of time the cursor must hover over an item before it is selected.
8422 * [I] infoPtr : valid pointer to the listview structure
8423 * [I] dwHoverTime : hover time, if -1 the hover time is set to the default
8426 * Returns the previous hover time
8428 static DWORD LISTVIEW_SetHoverTime(LISTVIEW_INFO *infoPtr, DWORD dwHoverTime)
8430 DWORD oldHoverTime = infoPtr->dwHoverTime;
8432 infoPtr->dwHoverTime = dwHoverTime;
8434 return oldHoverTime;
8439 * Sets spacing for icons of LVS_ICON style.
8442 * [I] infoPtr : valid pointer to the listview structure
8443 * [I] cx : horizontal spacing (-1 = system spacing, 0 = autosize)
8444 * [I] cy : vertical spacing (-1 = system spacing, 0 = autosize)
8447 * MAKELONG(oldcx, oldcy)
8449 static DWORD LISTVIEW_SetIconSpacing(LISTVIEW_INFO *infoPtr, INT cx, INT cy)
8451 DWORD oldspacing = MAKELONG(infoPtr->iconSpacing.cx, infoPtr->iconSpacing.cy);
8453 TRACE("requested=(%d,%d)\n", cx, cy);
8455 /* this is supported only for LVS_ICON style */
8456 if (infoPtr->uView != LV_VIEW_ICON) return oldspacing;
8458 /* set to defaults, if instructed to */
8459 if (cx == -1) cx = GetSystemMetrics(SM_CXICONSPACING);
8460 if (cy == -1) cy = GetSystemMetrics(SM_CYICONSPACING);
8462 /* if 0 then compute width
8463 * FIXME: Should scan each item and determine max width of
8464 * icon or label, then make that the width */
8466 cx = infoPtr->iconSpacing.cx;
8468 /* if 0 then compute height */
8470 cy = infoPtr->iconSize.cy + 2 * infoPtr->ntmHeight +
8471 ICON_BOTTOM_PADDING + ICON_TOP_PADDING + LABEL_VERT_PADDING;
8474 infoPtr->iconSpacing.cx = cx;
8475 infoPtr->iconSpacing.cy = cy;
8477 TRACE("old=(%d,%d), new=(%d,%d), iconSize=(%d,%d), ntmH=%d\n",
8478 LOWORD(oldspacing), HIWORD(oldspacing), cx, cy,
8479 infoPtr->iconSize.cx, infoPtr->iconSize.cy,
8480 infoPtr->ntmHeight);
8482 /* these depend on the iconSpacing */
8483 LISTVIEW_UpdateItemSize(infoPtr);
8488 static inline void set_icon_size(SIZE *size, HIMAGELIST himl, BOOL small)
8492 if (himl && ImageList_GetIconSize(himl, &cx, &cy))
8499 size->cx = GetSystemMetrics(small ? SM_CXSMICON : SM_CXICON);
8500 size->cy = GetSystemMetrics(small ? SM_CYSMICON : SM_CYICON);
8509 * [I] infoPtr : valid pointer to the listview structure
8510 * [I] nType : image list type
8511 * [I] himl : image list handle
8514 * SUCCESS : old image list
8517 static HIMAGELIST LISTVIEW_SetImageList(LISTVIEW_INFO *infoPtr, INT nType, HIMAGELIST himl)
8519 INT oldHeight = infoPtr->nItemHeight;
8520 HIMAGELIST himlOld = 0;
8522 TRACE("(nType=%d, himl=%p\n", nType, himl);
8527 himlOld = infoPtr->himlNormal;
8528 infoPtr->himlNormal = himl;
8529 if (infoPtr->uView == LV_VIEW_ICON) set_icon_size(&infoPtr->iconSize, himl, FALSE);
8530 LISTVIEW_SetIconSpacing(infoPtr, 0, 0);
8534 himlOld = infoPtr->himlSmall;
8535 infoPtr->himlSmall = himl;
8536 if (infoPtr->uView != LV_VIEW_ICON) set_icon_size(&infoPtr->iconSize, himl, TRUE);
8540 himlOld = infoPtr->himlState;
8541 infoPtr->himlState = himl;
8542 set_icon_size(&infoPtr->iconStateSize, himl, TRUE);
8543 ImageList_SetBkColor(infoPtr->himlState, CLR_NONE);
8547 ERR("Unknown icon type=%d\n", nType);
8551 infoPtr->nItemHeight = LISTVIEW_CalculateItemHeight(infoPtr);
8552 if (infoPtr->nItemHeight != oldHeight)
8553 LISTVIEW_UpdateScroll(infoPtr);
8560 * Preallocates memory (does *not* set the actual count of items !)
8563 * [I] infoPtr : valid pointer to the listview structure
8564 * [I] nItems : item count (projected number of items to allocate)
8565 * [I] dwFlags : update flags
8571 static BOOL LISTVIEW_SetItemCount(LISTVIEW_INFO *infoPtr, INT nItems, DWORD dwFlags)
8573 TRACE("(nItems=%d, dwFlags=%x)\n", nItems, dwFlags);
8575 if (infoPtr->dwStyle & LVS_OWNERDATA)
8577 INT nOldCount = infoPtr->nItemCount;
8579 if (nItems < nOldCount)
8581 RANGE range = { nItems, nOldCount };
8582 ranges_del(infoPtr->selectionRanges, range);
8583 if (infoPtr->nFocusedItem >= nItems)
8585 LISTVIEW_SetItemFocus(infoPtr, -1);
8586 SetRectEmpty(&infoPtr->rcFocus);
8590 infoPtr->nItemCount = nItems;
8591 LISTVIEW_UpdateScroll(infoPtr);
8593 /* the flags are valid only in ownerdata report and list modes */
8594 if (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON) dwFlags = 0;
8596 if (!(dwFlags & LVSICF_NOSCROLL) && infoPtr->nFocusedItem != -1)
8597 LISTVIEW_EnsureVisible(infoPtr, infoPtr->nFocusedItem, FALSE);
8599 if (!(dwFlags & LVSICF_NOINVALIDATEALL))
8600 LISTVIEW_InvalidateList(infoPtr);
8607 LISTVIEW_GetOrigin(infoPtr, &Origin);
8608 nFrom = min(nOldCount, nItems);
8609 nTo = max(nOldCount, nItems);
8611 if (infoPtr->uView == LV_VIEW_DETAILS)
8614 rcErase.top = nFrom * infoPtr->nItemHeight;
8615 rcErase.right = infoPtr->nItemWidth;
8616 rcErase.bottom = nTo * infoPtr->nItemHeight;
8617 OffsetRect(&rcErase, Origin.x, Origin.y);
8618 if (IntersectRect(&rcErase, &rcErase, &infoPtr->rcList))
8619 LISTVIEW_InvalidateRect(infoPtr, &rcErase);
8621 else /* LV_VIEW_LIST */
8623 INT nPerCol = LISTVIEW_GetCountPerColumn(infoPtr);
8625 rcErase.left = (nFrom / nPerCol) * infoPtr->nItemWidth;
8626 rcErase.top = (nFrom % nPerCol) * infoPtr->nItemHeight;
8627 rcErase.right = rcErase.left + infoPtr->nItemWidth;
8628 rcErase.bottom = nPerCol * infoPtr->nItemHeight;
8629 OffsetRect(&rcErase, Origin.x, Origin.y);
8630 if (IntersectRect(&rcErase, &rcErase, &infoPtr->rcList))
8631 LISTVIEW_InvalidateRect(infoPtr, &rcErase);
8633 rcErase.left = (nFrom / nPerCol + 1) * infoPtr->nItemWidth;
8635 rcErase.right = (nTo / nPerCol + 1) * infoPtr->nItemWidth;
8636 rcErase.bottom = nPerCol * infoPtr->nItemHeight;
8637 OffsetRect(&rcErase, Origin.x, Origin.y);
8638 if (IntersectRect(&rcErase, &rcErase, &infoPtr->rcList))
8639 LISTVIEW_InvalidateRect(infoPtr, &rcErase);
8645 /* According to MSDN for non-LVS_OWNERDATA this is just
8646 * a performance issue. The control allocates its internal
8647 * data structures for the number of items specified. It
8648 * cuts down on the number of memory allocations. Therefore
8649 * we will just issue a WARN here
8651 WARN("for non-ownerdata performance option not implemented.\n");
8659 * Sets the position of an item.
8662 * [I] infoPtr : valid pointer to the listview structure
8663 * [I] nItem : item index
8664 * [I] pt : coordinate
8670 static BOOL LISTVIEW_SetItemPosition(LISTVIEW_INFO *infoPtr, INT nItem, const POINT *pt)
8674 TRACE("(nItem=%d, pt=%s\n", nItem, wine_dbgstr_point(pt));
8676 if (!pt || nItem < 0 || nItem >= infoPtr->nItemCount ||
8677 !(infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON)) return FALSE;
8680 LISTVIEW_GetOrigin(infoPtr, &Origin);
8682 /* This point value seems to be an undocumented feature.
8683 * The best guess is that it means either at the origin,
8684 * or at true beginning of the list. I will assume the origin. */
8685 if ((Pt.x == -1) && (Pt.y == -1))
8688 if (infoPtr->uView == LV_VIEW_ICON)
8690 Pt.x -= (infoPtr->nItemWidth - infoPtr->iconSize.cx) / 2;
8691 Pt.y -= ICON_TOP_PADDING;
8696 infoPtr->bAutoarrange = FALSE;
8698 return LISTVIEW_MoveIconTo(infoPtr, nItem, &Pt, FALSE);
8703 * Sets the state of one or many items.
8706 * [I] infoPtr : valid pointer to the listview structure
8707 * [I] nItem : item index
8708 * [I] lpLVItem : item or subitem info
8714 static BOOL LISTVIEW_SetItemState(LISTVIEW_INFO *infoPtr, INT nItem, const LVITEMW *lpLVItem)
8716 BOOL bResult = TRUE;
8719 lvItem.iItem = nItem;
8720 lvItem.iSubItem = 0;
8721 lvItem.mask = LVIF_STATE;
8722 lvItem.state = lpLVItem->state;
8723 lvItem.stateMask = lpLVItem->stateMask;
8724 TRACE("lvItem=%s\n", debuglvitem_t(&lvItem, TRUE));
8728 /* select all isn't allowed in LVS_SINGLESEL */
8729 if ((lvItem.state & lvItem.stateMask & LVIS_SELECTED) && (infoPtr->dwStyle & LVS_SINGLESEL))
8732 /* focus all isn't allowed */
8733 if (lvItem.state & lvItem.stateMask & LVIS_FOCUSED) return FALSE;
8735 /* apply to all items */
8736 for (lvItem.iItem = 0; lvItem.iItem < infoPtr->nItemCount; lvItem.iItem++)
8737 if (!LISTVIEW_SetItemT(infoPtr, &lvItem, TRUE)) bResult = FALSE;
8740 bResult = LISTVIEW_SetItemT(infoPtr, &lvItem, TRUE);
8747 * Sets the text of an item or subitem.
8750 * [I] hwnd : window handle
8751 * [I] nItem : item index
8752 * [I] lpLVItem : item or subitem info
8753 * [I] isW : TRUE if input is Unicode
8759 static BOOL LISTVIEW_SetItemTextT(LISTVIEW_INFO *infoPtr, INT nItem, const LVITEMW *lpLVItem, BOOL isW)
8763 if (nItem < 0 && nItem >= infoPtr->nItemCount) return FALSE;
8765 lvItem.iItem = nItem;
8766 lvItem.iSubItem = lpLVItem->iSubItem;
8767 lvItem.mask = LVIF_TEXT;
8768 lvItem.pszText = lpLVItem->pszText;
8769 lvItem.cchTextMax = lpLVItem->cchTextMax;
8771 TRACE("(nItem=%d, lpLVItem=%s, isW=%d)\n", nItem, debuglvitem_t(&lvItem, isW), isW);
8773 return LISTVIEW_SetItemT(infoPtr, &lvItem, isW);
8778 * Set item index that marks the start of a multiple selection.
8781 * [I] infoPtr : valid pointer to the listview structure
8782 * [I] nIndex : index
8785 * Index number or -1 if there is no selection mark.
8787 static INT LISTVIEW_SetSelectionMark(LISTVIEW_INFO *infoPtr, INT nIndex)
8789 INT nOldIndex = infoPtr->nSelectionMark;
8791 TRACE("(nIndex=%d)\n", nIndex);
8793 infoPtr->nSelectionMark = nIndex;
8800 * Sets the text background color.
8803 * [I] infoPtr : valid pointer to the listview structure
8804 * [I] clrTextBk : text background color
8810 static BOOL LISTVIEW_SetTextBkColor(LISTVIEW_INFO *infoPtr, COLORREF clrTextBk)
8812 TRACE("(clrTextBk=%x)\n", clrTextBk);
8814 if (infoPtr->clrTextBk != clrTextBk)
8816 infoPtr->clrTextBk = clrTextBk;
8817 LISTVIEW_InvalidateList(infoPtr);
8825 * Sets the text foreground color.
8828 * [I] infoPtr : valid pointer to the listview structure
8829 * [I] clrText : text color
8835 static BOOL LISTVIEW_SetTextColor (LISTVIEW_INFO *infoPtr, COLORREF clrText)
8837 TRACE("(clrText=%x)\n", clrText);
8839 if (infoPtr->clrText != clrText)
8841 infoPtr->clrText = clrText;
8842 LISTVIEW_InvalidateList(infoPtr);
8850 * Sets new ToolTip window to ListView control.
8853 * [I] infoPtr : valid pointer to the listview structure
8854 * [I] hwndNewToolTip : handle to new ToolTip
8859 static HWND LISTVIEW_SetToolTips( LISTVIEW_INFO *infoPtr, HWND hwndNewToolTip)
8861 HWND hwndOldToolTip = infoPtr->hwndToolTip;
8862 infoPtr->hwndToolTip = hwndNewToolTip;
8863 return hwndOldToolTip;
8868 * sets the Unicode character format flag for the control
8870 * [I] infoPtr :valid pointer to the listview structure
8871 * [I] fUnicode :true to switch to UNICODE false to switch to ANSI
8874 * Old Unicode Format
8876 static BOOL LISTVIEW_SetUnicodeFormat( LISTVIEW_INFO *infoPtr, BOOL fUnicode)
8878 SHORT rc = infoPtr->notifyFormat;
8879 infoPtr->notifyFormat = (fUnicode) ? NFR_UNICODE : NFR_ANSI;
8880 return rc == NFR_UNICODE;
8885 * sets the control view mode
8887 * [I] infoPtr :valid pointer to the listview structure
8888 * [I] nView :new view mode value
8894 static INT LISTVIEW_SetView(LISTVIEW_INFO *infoPtr, DWORD nView)
8896 SIZE oldIconSize = infoPtr->iconSize;
8899 if (infoPtr->uView == nView) return 1;
8901 if ((INT)nView < 0 || nView > LV_VIEW_MAX) return -1;
8902 if (nView == LV_VIEW_TILE)
8904 FIXME("View LV_VIEW_TILE unimplemented\n");
8908 infoPtr->uView = nView;
8910 SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
8911 ShowWindow(infoPtr->hwndHeader, SW_HIDE);
8913 ShowScrollBar(infoPtr->hwndSelf, SB_BOTH, FALSE);
8914 SetRectEmpty(&infoPtr->rcFocus);
8916 himl = (nView == LV_VIEW_ICON ? infoPtr->himlNormal : infoPtr->himlSmall);
8917 set_icon_size(&infoPtr->iconSize, himl, nView != LV_VIEW_ICON);
8922 if ((infoPtr->iconSize.cx != oldIconSize.cx) || (infoPtr->iconSize.cy != oldIconSize.cy))
8924 TRACE("icon old size=(%d,%d), new size=(%d,%d)\n",
8925 oldIconSize.cx, oldIconSize.cy, infoPtr->iconSize.cx, infoPtr->iconSize.cy);
8926 LISTVIEW_SetIconSpacing(infoPtr, 0, 0);
8928 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
8930 case LV_VIEW_SMALLICON:
8931 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
8933 case LV_VIEW_DETAILS:
8938 LISTVIEW_CreateHeader( infoPtr );
8940 hl.prc = &infoPtr->rcList;
8942 SendMessageW(infoPtr->hwndHeader, HDM_LAYOUT, 0, (LPARAM)&hl);
8943 SetWindowPos(infoPtr->hwndHeader, infoPtr->hwndSelf, wp.x, wp.y, wp.cx, wp.cy,
8944 wp.flags | ((infoPtr->dwStyle & LVS_NOCOLUMNHEADER) ? SWP_HIDEWINDOW : SWP_SHOWWINDOW));
8951 LISTVIEW_UpdateItemSize(infoPtr);
8952 LISTVIEW_UpdateSize(infoPtr);
8953 LISTVIEW_UpdateScroll(infoPtr);
8954 LISTVIEW_InvalidateList(infoPtr);
8956 TRACE("nView=%d\n", nView);
8961 /* LISTVIEW_SetWorkAreas */
8965 * Callback internally used by LISTVIEW_SortItems() in response of LVM_SORTITEMS
8968 * [I] first : pointer to first ITEM_INFO to compare
8969 * [I] second : pointer to second ITEM_INFO to compare
8970 * [I] lParam : HWND of control
8973 * if first comes before second : negative
8974 * if first comes after second : positive
8975 * if first and second are equivalent : zero
8977 static INT WINAPI LISTVIEW_CallBackCompare(LPVOID first, LPVOID second, LPARAM lParam)
8979 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)lParam;
8980 ITEM_INFO* lv_first = DPA_GetPtr( first, 0 );
8981 ITEM_INFO* lv_second = DPA_GetPtr( second, 0 );
8983 /* Forward the call to the client defined callback */
8984 return (infoPtr->pfnCompare)( lv_first->lParam , lv_second->lParam, infoPtr->lParamSort );
8989 * Callback internally used by LISTVIEW_SortItems() in response of LVM_SORTITEMSEX
8992 * [I] first : pointer to first ITEM_INFO to compare
8993 * [I] second : pointer to second ITEM_INFO to compare
8994 * [I] lParam : HWND of control
8997 * if first comes before second : negative
8998 * if first comes after second : positive
8999 * if first and second are equivalent : zero
9001 static INT WINAPI LISTVIEW_CallBackCompareEx(LPVOID first, LPVOID second, LPARAM lParam)
9003 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)lParam;
9004 INT first_idx = DPA_GetPtrIndex( infoPtr->hdpaItems, first );
9005 INT second_idx = DPA_GetPtrIndex( infoPtr->hdpaItems, second );
9007 /* Forward the call to the client defined callback */
9008 return (infoPtr->pfnCompare)( first_idx, second_idx, infoPtr->lParamSort );
9013 * Sorts the listview items.
9016 * [I] infoPtr : valid pointer to the listview structure
9017 * [I] pfnCompare : application-defined value
9018 * [I] lParamSort : pointer to comparison callback
9019 * [I] IsEx : TRUE when LVM_SORTITEMSEX used
9025 static BOOL LISTVIEW_SortItems(LISTVIEW_INFO *infoPtr, PFNLVCOMPARE pfnCompare,
9026 LPARAM lParamSort, BOOL IsEx)
9030 LPVOID selectionMarkItem = NULL;
9031 LPVOID focusedItem = NULL;
9034 TRACE("(pfnCompare=%p, lParamSort=%lx)\n", pfnCompare, lParamSort);
9036 if (infoPtr->dwStyle & LVS_OWNERDATA) return FALSE;
9038 if (!pfnCompare) return FALSE;
9039 if (!infoPtr->hdpaItems) return FALSE;
9041 /* if there are 0 or 1 items, there is no need to sort */
9042 if (infoPtr->nItemCount < 2) return TRUE;
9044 /* clear selection */
9045 ranges_clear(infoPtr->selectionRanges);
9047 /* save selection mark and focused item */
9048 if (infoPtr->nSelectionMark >= 0)
9049 selectionMarkItem = DPA_GetPtr(infoPtr->hdpaItems, infoPtr->nSelectionMark);
9050 if (infoPtr->nFocusedItem >= 0)
9051 focusedItem = DPA_GetPtr(infoPtr->hdpaItems, infoPtr->nFocusedItem);
9053 infoPtr->pfnCompare = pfnCompare;
9054 infoPtr->lParamSort = lParamSort;
9056 DPA_Sort(infoPtr->hdpaItems, LISTVIEW_CallBackCompareEx, (LPARAM)infoPtr);
9058 DPA_Sort(infoPtr->hdpaItems, LISTVIEW_CallBackCompare, (LPARAM)infoPtr);
9060 /* restore selection ranges */
9061 for (i=0; i < infoPtr->nItemCount; i++)
9063 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, i);
9064 lpItem = DPA_GetPtr(hdpaSubItems, 0);
9066 if (lpItem->state & LVIS_SELECTED)
9067 ranges_additem(infoPtr->selectionRanges, i);
9069 /* restore selection mark and focused item */
9070 infoPtr->nSelectionMark = DPA_GetPtrIndex(infoPtr->hdpaItems, selectionMarkItem);
9071 infoPtr->nFocusedItem = DPA_GetPtrIndex(infoPtr->hdpaItems, focusedItem);
9073 /* I believe nHotItem should be left alone, see LISTVIEW_ShiftIndices */
9075 /* refresh the display */
9076 if (infoPtr->uView != LV_VIEW_ICON && infoPtr->uView != LV_VIEW_SMALLICON)
9077 LISTVIEW_InvalidateList(infoPtr);
9084 * Update theme handle after a theme change.
9087 * [I] infoPtr : valid pointer to the listview structure
9091 * FAILURE : something else
9093 static LRESULT LISTVIEW_ThemeChanged(const LISTVIEW_INFO *infoPtr)
9095 HTHEME theme = GetWindowTheme(infoPtr->hwndSelf);
9096 CloseThemeData(theme);
9097 OpenThemeData(infoPtr->hwndSelf, themeClass);
9103 * Updates an items or rearranges the listview control.
9106 * [I] infoPtr : valid pointer to the listview structure
9107 * [I] nItem : item index
9113 static BOOL LISTVIEW_Update(LISTVIEW_INFO *infoPtr, INT nItem)
9115 TRACE("(nItem=%d)\n", nItem);
9117 if (nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
9119 /* rearrange with default alignment style */
9120 if (is_autoarrange(infoPtr))
9121 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
9123 LISTVIEW_InvalidateItem(infoPtr, nItem);
9130 * Draw the track line at the place defined in the infoPtr structure.
9131 * The line is drawn with a XOR pen so drawing the line for the second time
9132 * in the same place erases the line.
9135 * [I] infoPtr : valid pointer to the listview structure
9141 static BOOL LISTVIEW_DrawTrackLine(const LISTVIEW_INFO *infoPtr)
9147 if (infoPtr->xTrackLine == -1)
9150 if (!(hdc = GetDC(infoPtr->hwndSelf)))
9152 hOldPen = SelectObject(hdc, GetStockObject(BLACK_PEN));
9153 oldROP = SetROP2(hdc, R2_XORPEN);
9154 MoveToEx(hdc, infoPtr->xTrackLine, infoPtr->rcList.top, NULL);
9155 LineTo(hdc, infoPtr->xTrackLine, infoPtr->rcList.bottom);
9156 SetROP2(hdc, oldROP);
9157 SelectObject(hdc, hOldPen);
9158 ReleaseDC(infoPtr->hwndSelf, hdc);
9164 * Called when an edit control should be displayed. This function is called after
9165 * we are sure that there was a single click - not a double click (this is a TIMERPROC).
9168 * [I] hwnd : Handle to the listview
9169 * [I] uMsg : WM_TIMER (ignored)
9170 * [I] idEvent : The timer ID interpreted as a pointer to a DELAYED_EDIT_ITEM struct
9171 * [I] dwTimer : The elapsed time (ignored)
9176 static VOID CALLBACK LISTVIEW_DelayedEditItem(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
9178 DELAYED_ITEM_EDIT *editItem = (DELAYED_ITEM_EDIT *)idEvent;
9179 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongPtrW(hwnd, 0);
9181 KillTimer(hwnd, idEvent);
9182 editItem->fEnabled = FALSE;
9183 /* check if the item is still selected */
9184 if (infoPtr->bFocus && LISTVIEW_GetItemState(infoPtr, editItem->iItem, LVIS_SELECTED))
9185 LISTVIEW_EditLabelT(infoPtr, editItem->iItem, TRUE);
9190 * Creates the listview control - the WM_NCCREATE phase.
9193 * [I] hwnd : window handle
9194 * [I] lpcs : the create parameters
9200 static LRESULT LISTVIEW_NCCreate(HWND hwnd, const CREATESTRUCTW *lpcs)
9202 LISTVIEW_INFO *infoPtr;
9205 TRACE("(lpcs=%p)\n", lpcs);
9207 /* initialize info pointer */
9208 infoPtr = Alloc(sizeof(LISTVIEW_INFO));
9209 if (!infoPtr) return FALSE;
9211 SetWindowLongPtrW(hwnd, 0, (DWORD_PTR)infoPtr);
9213 infoPtr->hwndSelf = hwnd;
9214 infoPtr->dwStyle = lpcs->style; /* Note: may be changed in WM_CREATE */
9215 map_style_view(infoPtr);
9216 /* determine the type of structures to use */
9217 infoPtr->hwndNotify = lpcs->hwndParent;
9218 /* infoPtr->notifyFormat will be filled in WM_CREATE */
9220 /* initialize color information */
9221 infoPtr->clrBk = CLR_NONE;
9222 infoPtr->clrText = CLR_DEFAULT;
9223 infoPtr->clrTextBk = CLR_DEFAULT;
9224 LISTVIEW_SetBkColor(infoPtr, comctl32_color.clrWindow);
9226 /* set default values */
9227 infoPtr->nFocusedItem = -1;
9228 infoPtr->nSelectionMark = -1;
9229 infoPtr->nHotItem = -1;
9230 infoPtr->bRedraw = TRUE;
9231 infoPtr->bNoItemMetrics = TRUE;
9232 infoPtr->bDoChangeNotify = TRUE;
9233 infoPtr->iconSpacing.cx = GetSystemMetrics(SM_CXICONSPACING);
9234 infoPtr->iconSpacing.cy = GetSystemMetrics(SM_CYICONSPACING);
9235 infoPtr->nEditLabelItem = -1;
9236 infoPtr->nLButtonDownItem = -1;
9237 infoPtr->dwHoverTime = HOVER_DEFAULT; /* default system hover time */
9238 infoPtr->nMeasureItemHeight = 0;
9239 infoPtr->xTrackLine = -1; /* no track line */
9240 infoPtr->itemEdit.fEnabled = FALSE;
9241 infoPtr->iVersion = COMCTL32_VERSION;
9242 infoPtr->colRectsDirty = FALSE;
9244 /* get default font (icon title) */
9245 SystemParametersInfoW(SPI_GETICONTITLELOGFONT, 0, &logFont, 0);
9246 infoPtr->hDefaultFont = CreateFontIndirectW(&logFont);
9247 infoPtr->hFont = infoPtr->hDefaultFont;
9248 LISTVIEW_SaveTextMetrics(infoPtr);
9250 /* allocate memory for the data structure */
9251 if (!(infoPtr->selectionRanges = ranges_create(10))) goto fail;
9252 if (!(infoPtr->hdpaItems = DPA_Create(10))) goto fail;
9253 if (!(infoPtr->hdpaItemIds = DPA_Create(10))) goto fail;
9254 if (!(infoPtr->hdpaPosX = DPA_Create(10))) goto fail;
9255 if (!(infoPtr->hdpaPosY = DPA_Create(10))) goto fail;
9256 if (!(infoPtr->hdpaColumns = DPA_Create(10))) goto fail;
9260 DestroyWindow(infoPtr->hwndHeader);
9261 ranges_destroy(infoPtr->selectionRanges);
9262 DPA_Destroy(infoPtr->hdpaItems);
9263 DPA_Destroy(infoPtr->hdpaItemIds);
9264 DPA_Destroy(infoPtr->hdpaPosX);
9265 DPA_Destroy(infoPtr->hdpaPosY);
9266 DPA_Destroy(infoPtr->hdpaColumns);
9273 * Creates the listview control - the WM_CREATE phase. Most of the data is
9274 * already set up in LISTVIEW_NCCreate
9277 * [I] hwnd : window handle
9278 * [I] lpcs : the create parameters
9284 static LRESULT LISTVIEW_Create(HWND hwnd, const CREATESTRUCTW *lpcs)
9286 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongPtrW(hwnd, 0);
9288 TRACE("(lpcs=%p)\n", lpcs);
9290 infoPtr->dwStyle = lpcs->style;
9291 map_style_view(infoPtr);
9293 infoPtr->notifyFormat = SendMessageW(infoPtr->hwndNotify, WM_NOTIFYFORMAT,
9294 (WPARAM)infoPtr->hwndSelf, NF_QUERY);
9295 /* on error defaulting to ANSI notifications */
9296 if (infoPtr->notifyFormat == 0) infoPtr->notifyFormat = NFR_ANSI;
9298 if ((infoPtr->uView == LV_VIEW_DETAILS) && (lpcs->style & WS_VISIBLE))
9300 if (LISTVIEW_CreateHeader(infoPtr) < 0) return -1;
9303 infoPtr->hwndHeader = 0;
9305 /* init item size to avoid division by 0 */
9306 LISTVIEW_UpdateItemSize (infoPtr);
9308 if (infoPtr->uView == LV_VIEW_DETAILS)
9310 if (!(LVS_NOCOLUMNHEADER & lpcs->style) && (WS_VISIBLE & lpcs->style))
9312 ShowWindow(infoPtr->hwndHeader, SW_SHOWNORMAL);
9314 LISTVIEW_UpdateScroll(infoPtr);
9315 /* send WM_MEASUREITEM notification */
9316 if (infoPtr->dwStyle & LVS_OWNERDRAWFIXED) notify_measureitem(infoPtr);
9319 OpenThemeData(hwnd, themeClass);
9321 /* initialize the icon sizes */
9322 set_icon_size(&infoPtr->iconSize, infoPtr->himlNormal, infoPtr->uView != LV_VIEW_ICON);
9323 set_icon_size(&infoPtr->iconStateSize, infoPtr->himlState, TRUE);
9329 * Destroys the listview control.
9332 * [I] infoPtr : valid pointer to the listview structure
9338 static LRESULT LISTVIEW_Destroy(LISTVIEW_INFO *infoPtr)
9340 HTHEME theme = GetWindowTheme(infoPtr->hwndSelf);
9341 CloseThemeData(theme);
9343 /* delete all items */
9344 LISTVIEW_DeleteAllItems(infoPtr, TRUE);
9351 * Enables the listview control.
9354 * [I] infoPtr : valid pointer to the listview structure
9355 * [I] bEnable : specifies whether to enable or disable the window
9361 static BOOL LISTVIEW_Enable(const LISTVIEW_INFO *infoPtr)
9363 if (infoPtr->dwStyle & LVS_OWNERDRAWFIXED)
9364 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
9370 * Erases the background of the listview control.
9373 * [I] infoPtr : valid pointer to the listview structure
9374 * [I] hdc : device context handle
9380 static inline BOOL LISTVIEW_EraseBkgnd(const LISTVIEW_INFO *infoPtr, HDC hdc)
9384 TRACE("(hdc=%p)\n", hdc);
9386 if (!GetClipBox(hdc, &rc)) return FALSE;
9388 if (infoPtr->clrBk == CLR_NONE)
9390 if (infoPtr->dwLvExStyle & LVS_EX_TRANSPARENTBKGND)
9391 return SendMessageW(infoPtr->hwndNotify, WM_PRINTCLIENT,
9392 (WPARAM)hdc, PRF_ERASEBKGND);
9394 return SendMessageW(infoPtr->hwndNotify, WM_ERASEBKGND, (WPARAM)hdc, 0);
9397 /* for double buffered controls we need to do this during refresh */
9398 if (infoPtr->dwLvExStyle & LVS_EX_DOUBLEBUFFER) return FALSE;
9400 return LISTVIEW_FillBkgnd(infoPtr, hdc, &rc);
9406 * Helper function for LISTVIEW_[HV]Scroll *only*.
9407 * Performs vertical/horizontal scrolling by a give amount.
9410 * [I] infoPtr : valid pointer to the listview structure
9411 * [I] dx : amount of horizontal scroll
9412 * [I] dy : amount of vertical scroll
9414 static void scroll_list(LISTVIEW_INFO *infoPtr, INT dx, INT dy)
9416 /* now we can scroll the list */
9417 ScrollWindowEx(infoPtr->hwndSelf, dx, dy, &infoPtr->rcList,
9418 &infoPtr->rcList, 0, 0, SW_ERASE | SW_INVALIDATE);
9419 /* if we have focus, adjust rect */
9420 OffsetRect(&infoPtr->rcFocus, dx, dy);
9421 UpdateWindow(infoPtr->hwndSelf);
9426 * Performs vertical scrolling.
9429 * [I] infoPtr : valid pointer to the listview structure
9430 * [I] nScrollCode : scroll code
9431 * [I] nScrollDiff : units to scroll in SB_INTERNAL mode, 0 otherwise
9432 * [I] hScrollWnd : scrollbar control window handle
9438 * SB_LINEUP/SB_LINEDOWN:
9439 * for LVS_ICON, LVS_SMALLICON is 37 by experiment
9440 * for LVS_REPORT is 1 line
9441 * for LVS_LIST cannot occur
9444 static LRESULT LISTVIEW_VScroll(LISTVIEW_INFO *infoPtr, INT nScrollCode,
9447 INT nOldScrollPos, nNewScrollPos;
9448 SCROLLINFO scrollInfo;
9451 TRACE("(nScrollCode=%d(%s), nScrollDiff=%d)\n", nScrollCode,
9452 debugscrollcode(nScrollCode), nScrollDiff);
9454 if (infoPtr->hwndEdit) SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
9456 scrollInfo.cbSize = sizeof(SCROLLINFO);
9457 scrollInfo.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_TRACKPOS;
9459 is_an_icon = ((infoPtr->uView == LV_VIEW_ICON) || (infoPtr->uView == LV_VIEW_SMALLICON));
9461 if (!GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo)) return 1;
9463 nOldScrollPos = scrollInfo.nPos;
9464 switch (nScrollCode)
9470 nScrollDiff = (is_an_icon) ? -LISTVIEW_SCROLL_ICON_LINE_SIZE : -1;
9474 nScrollDiff = (is_an_icon) ? LISTVIEW_SCROLL_ICON_LINE_SIZE : 1;
9478 nScrollDiff = -scrollInfo.nPage;
9482 nScrollDiff = scrollInfo.nPage;
9485 case SB_THUMBPOSITION:
9487 nScrollDiff = scrollInfo.nTrackPos - scrollInfo.nPos;
9494 /* quit right away if pos isn't changing */
9495 if (nScrollDiff == 0) return 0;
9497 /* calculate new position, and handle overflows */
9498 nNewScrollPos = scrollInfo.nPos + nScrollDiff;
9499 if (nScrollDiff > 0) {
9500 if (nNewScrollPos < nOldScrollPos ||
9501 nNewScrollPos > scrollInfo.nMax)
9502 nNewScrollPos = scrollInfo.nMax;
9504 if (nNewScrollPos > nOldScrollPos ||
9505 nNewScrollPos < scrollInfo.nMin)
9506 nNewScrollPos = scrollInfo.nMin;
9509 /* set the new position, and reread in case it changed */
9510 scrollInfo.fMask = SIF_POS;
9511 scrollInfo.nPos = nNewScrollPos;
9512 nNewScrollPos = SetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo, TRUE);
9514 /* carry on only if it really changed */
9515 if (nNewScrollPos == nOldScrollPos) return 0;
9517 /* now adjust to client coordinates */
9518 nScrollDiff = nOldScrollPos - nNewScrollPos;
9519 if (infoPtr->uView == LV_VIEW_DETAILS) nScrollDiff *= infoPtr->nItemHeight;
9521 /* and scroll the window */
9522 scroll_list(infoPtr, 0, nScrollDiff);
9529 * Performs horizontal scrolling.
9532 * [I] infoPtr : valid pointer to the listview structure
9533 * [I] nScrollCode : scroll code
9534 * [I] nScrollDiff : units to scroll in SB_INTERNAL mode, 0 otherwise
9535 * [I] hScrollWnd : scrollbar control window handle
9541 * SB_LINELEFT/SB_LINERIGHT:
9542 * for LVS_ICON, LVS_SMALLICON 1 pixel
9543 * for LVS_REPORT is 1 pixel
9544 * for LVS_LIST is 1 column --> which is a 1 because the
9545 * scroll is based on columns not pixels
9548 static LRESULT LISTVIEW_HScroll(LISTVIEW_INFO *infoPtr, INT nScrollCode,
9549 INT nScrollDiff, HWND hScrollWnd)
9551 INT nOldScrollPos, nNewScrollPos;
9552 SCROLLINFO scrollInfo;
9554 TRACE("(nScrollCode=%d(%s), nScrollDiff=%d)\n", nScrollCode,
9555 debugscrollcode(nScrollCode), nScrollDiff);
9557 if (infoPtr->hwndEdit) SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
9559 scrollInfo.cbSize = sizeof(SCROLLINFO);
9560 scrollInfo.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_TRACKPOS;
9562 if (!GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo)) return 1;
9564 nOldScrollPos = scrollInfo.nPos;
9566 switch (nScrollCode)
9580 nScrollDiff = -scrollInfo.nPage;
9584 nScrollDiff = scrollInfo.nPage;
9587 case SB_THUMBPOSITION:
9589 nScrollDiff = scrollInfo.nTrackPos - scrollInfo.nPos;
9596 /* quit right away if pos isn't changing */
9597 if (nScrollDiff == 0) return 0;
9599 /* calculate new position, and handle overflows */
9600 nNewScrollPos = scrollInfo.nPos + nScrollDiff;
9601 if (nScrollDiff > 0) {
9602 if (nNewScrollPos < nOldScrollPos ||
9603 nNewScrollPos > scrollInfo.nMax)
9604 nNewScrollPos = scrollInfo.nMax;
9606 if (nNewScrollPos > nOldScrollPos ||
9607 nNewScrollPos < scrollInfo.nMin)
9608 nNewScrollPos = scrollInfo.nMin;
9611 /* set the new position, and reread in case it changed */
9612 scrollInfo.fMask = SIF_POS;
9613 scrollInfo.nPos = nNewScrollPos;
9614 nNewScrollPos = SetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo, TRUE);
9616 /* carry on only if it really changed */
9617 if (nNewScrollPos == nOldScrollPos) return 0;
9619 if (infoPtr->hwndHeader) LISTVIEW_UpdateHeaderSize(infoPtr, nNewScrollPos);
9621 /* now adjust to client coordinates */
9622 nScrollDiff = nOldScrollPos - nNewScrollPos;
9623 if (infoPtr->uView == LV_VIEW_LIST) nScrollDiff *= infoPtr->nItemWidth;
9625 /* and scroll the window */
9626 scroll_list(infoPtr, nScrollDiff, 0);
9631 static LRESULT LISTVIEW_MouseWheel(LISTVIEW_INFO *infoPtr, INT wheelDelta)
9633 INT gcWheelDelta = 0;
9634 INT pulScrollLines = 3;
9636 TRACE("(wheelDelta=%d)\n", wheelDelta);
9638 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
9639 gcWheelDelta -= wheelDelta;
9641 switch(infoPtr->uView)
9644 case LV_VIEW_SMALLICON:
9646 * listview should be scrolled by a multiple of 37 dependently on its dimension or its visible item number
9647 * should be fixed in the future.
9649 LISTVIEW_VScroll(infoPtr, SB_INTERNAL, (gcWheelDelta < 0) ?
9650 -LISTVIEW_SCROLL_ICON_LINE_SIZE : LISTVIEW_SCROLL_ICON_LINE_SIZE);
9653 case LV_VIEW_DETAILS:
9654 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
9656 int cLineScroll = min(LISTVIEW_GetCountPerColumn(infoPtr), pulScrollLines);
9657 cLineScroll *= (gcWheelDelta / WHEEL_DELTA);
9658 LISTVIEW_VScroll(infoPtr, SB_INTERNAL, cLineScroll);
9663 LISTVIEW_HScroll(infoPtr, (gcWheelDelta < 0) ? SB_LINELEFT : SB_LINERIGHT, 0, 0);
9674 * [I] infoPtr : valid pointer to the listview structure
9675 * [I] nVirtualKey : virtual key
9676 * [I] lKeyData : key data
9681 static LRESULT LISTVIEW_KeyDown(LISTVIEW_INFO *infoPtr, INT nVirtualKey, LONG lKeyData)
9683 HWND hwndSelf = infoPtr->hwndSelf;
9685 NMLVKEYDOWN nmKeyDown;
9687 TRACE("(nVirtualKey=%d, lKeyData=%d)\n", nVirtualKey, lKeyData);
9689 /* send LVN_KEYDOWN notification */
9690 nmKeyDown.wVKey = nVirtualKey;
9691 nmKeyDown.flags = 0;
9692 notify_hdr(infoPtr, LVN_KEYDOWN, &nmKeyDown.hdr);
9693 if (!IsWindow(hwndSelf))
9696 switch (nVirtualKey)
9699 nItem = infoPtr->nFocusedItem;
9700 if (infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES)
9701 toggle_checkbox_state(infoPtr, infoPtr->nFocusedItem);
9705 if ((infoPtr->nItemCount > 0) && (infoPtr->nFocusedItem != -1))
9707 if (!notify(infoPtr, NM_RETURN)) return 0;
9708 if (!notify(infoPtr, LVN_ITEMACTIVATE)) return 0;
9713 if (infoPtr->nItemCount > 0)
9718 if (infoPtr->nItemCount > 0)
9719 nItem = infoPtr->nItemCount - 1;
9723 nItem = LISTVIEW_GetNextItem(infoPtr, infoPtr->nFocusedItem, LVNI_TOLEFT);
9727 nItem = LISTVIEW_GetNextItem(infoPtr, infoPtr->nFocusedItem, LVNI_ABOVE);
9731 nItem = LISTVIEW_GetNextItem(infoPtr, infoPtr->nFocusedItem, LVNI_TORIGHT);
9735 nItem = LISTVIEW_GetNextItem(infoPtr, infoPtr->nFocusedItem, LVNI_BELOW);
9739 if (infoPtr->uView == LV_VIEW_DETAILS)
9741 INT topidx = LISTVIEW_GetTopIndex(infoPtr);
9742 if (infoPtr->nFocusedItem == topidx)
9743 nItem = topidx - LISTVIEW_GetCountPerColumn(infoPtr) + 1;
9748 nItem = infoPtr->nFocusedItem - LISTVIEW_GetCountPerColumn(infoPtr)
9749 * LISTVIEW_GetCountPerRow(infoPtr);
9750 if(nItem < 0) nItem = 0;
9754 if (infoPtr->uView == LV_VIEW_DETAILS)
9756 INT topidx = LISTVIEW_GetTopIndex(infoPtr);
9757 INT cnt = LISTVIEW_GetCountPerColumn(infoPtr);
9758 if (infoPtr->nFocusedItem == topidx + cnt - 1)
9759 nItem = infoPtr->nFocusedItem + cnt - 1;
9761 nItem = topidx + cnt - 1;
9764 nItem = infoPtr->nFocusedItem + LISTVIEW_GetCountPerColumn(infoPtr)
9765 * LISTVIEW_GetCountPerRow(infoPtr);
9766 if(nItem >= infoPtr->nItemCount) nItem = infoPtr->nItemCount - 1;
9770 if ((nItem != -1) && (nItem != infoPtr->nFocusedItem || nVirtualKey == VK_SPACE))
9771 LISTVIEW_KeySelection(infoPtr, nItem, nVirtualKey == VK_SPACE);
9781 * [I] infoPtr : valid pointer to the listview structure
9786 static LRESULT LISTVIEW_KillFocus(LISTVIEW_INFO *infoPtr)
9790 /* if we did not have the focus, there's nothing to do */
9791 if (!infoPtr->bFocus) return 0;
9793 /* send NM_KILLFOCUS notification */
9794 if (!notify(infoPtr, NM_KILLFOCUS)) return 0;
9796 /* if we have a focus rectangle, get rid of it */
9797 LISTVIEW_ShowFocusRect(infoPtr, FALSE);
9799 /* if have a marquee selection, stop it */
9800 if (infoPtr->bMarqueeSelect)
9802 /* Remove the marquee rectangle and release our mouse capture */
9803 LISTVIEW_InvalidateRect(infoPtr, &infoPtr->marqueeRect);
9806 SetRect(&infoPtr->marqueeRect, 0, 0, 0, 0);
9808 infoPtr->bMarqueeSelect = FALSE;
9809 infoPtr->bScrolling = FALSE;
9810 KillTimer(infoPtr->hwndSelf, (UINT_PTR) infoPtr);
9813 /* set window focus flag */
9814 infoPtr->bFocus = FALSE;
9816 /* invalidate the selected items before resetting focus flag */
9817 LISTVIEW_InvalidateSelectedItems(infoPtr);
9824 * Processes double click messages (left mouse button).
9827 * [I] infoPtr : valid pointer to the listview structure
9828 * [I] wKey : key flag
9829 * [I] x,y : mouse coordinate
9834 static LRESULT LISTVIEW_LButtonDblClk(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
9836 LVHITTESTINFO htInfo;
9838 TRACE("(key=%hu, X=%u, Y=%u)\n", wKey, x, y);
9840 /* Cancel the item edition if any */
9841 if (infoPtr->itemEdit.fEnabled)
9843 KillTimer(infoPtr->hwndSelf, (UINT_PTR)&infoPtr->itemEdit);
9844 infoPtr->itemEdit.fEnabled = FALSE;
9847 /* send NM_RELEASEDCAPTURE notification */
9848 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
9853 /* send NM_DBLCLK notification */
9854 LISTVIEW_HitTest(infoPtr, &htInfo, TRUE, FALSE);
9855 if (!notify_click(infoPtr, NM_DBLCLK, &htInfo)) return 0;
9857 /* To send the LVN_ITEMACTIVATE, it must be on an Item */
9858 if(htInfo.iItem != -1) notify_itemactivate(infoPtr,&htInfo);
9865 * Processes mouse down messages (left mouse button).
9868 * infoPtr [I ] valid pointer to the listview structure
9869 * wKey [I ] key flag
9870 * x,y [I ] mouse coordinate
9875 static LRESULT LISTVIEW_LButtonDown(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
9877 LVHITTESTINFO lvHitTestInfo;
9878 static BOOL bGroupSelect = TRUE;
9879 POINT pt = { x, y };
9882 TRACE("(key=%hu, X=%u, Y=%u)\n", wKey, x, y);
9884 /* send NM_RELEASEDCAPTURE notification */
9885 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
9887 /* set left button down flag and record the click position */
9888 infoPtr->bLButtonDown = TRUE;
9889 infoPtr->ptClickPos = pt;
9890 infoPtr->bDragging = FALSE;
9891 infoPtr->bMarqueeSelect = FALSE;
9892 infoPtr->bScrolling = FALSE;
9894 lvHitTestInfo.pt.x = x;
9895 lvHitTestInfo.pt.y = y;
9897 nItem = LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, TRUE);
9898 TRACE("at %s, nItem=%d\n", wine_dbgstr_point(&pt), nItem);
9899 if ((nItem >= 0) && (nItem < infoPtr->nItemCount))
9901 if ((infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES) && (lvHitTestInfo.flags & LVHT_ONITEMSTATEICON))
9903 toggle_checkbox_state(infoPtr, nItem);
9907 if (infoPtr->dwStyle & LVS_SINGLESEL)
9909 if (LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED))
9910 infoPtr->nEditLabelItem = nItem;
9912 LISTVIEW_SetSelection(infoPtr, nItem);
9916 if ((wKey & MK_CONTROL) && (wKey & MK_SHIFT))
9920 if (!LISTVIEW_AddGroupSelection(infoPtr, nItem)) return 0;
9921 LISTVIEW_SetItemFocus(infoPtr, nItem);
9922 infoPtr->nSelectionMark = nItem;
9928 item.state = LVIS_SELECTED | LVIS_FOCUSED;
9929 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
9931 LISTVIEW_SetItemState(infoPtr,nItem,&item);
9932 infoPtr->nSelectionMark = nItem;
9935 else if (wKey & MK_CONTROL)
9939 bGroupSelect = (LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED) == 0);
9941 item.state = (bGroupSelect ? LVIS_SELECTED : 0) | LVIS_FOCUSED;
9942 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
9943 LISTVIEW_SetItemState(infoPtr, nItem, &item);
9944 infoPtr->nSelectionMark = nItem;
9946 else if (wKey & MK_SHIFT)
9948 LISTVIEW_SetGroupSelection(infoPtr, nItem);
9952 if (LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED))
9954 infoPtr->nEditLabelItem = nItem;
9955 infoPtr->nLButtonDownItem = nItem;
9957 LISTVIEW_SetItemFocus(infoPtr, nItem);
9960 /* set selection (clears other pre-existing selections) */
9961 LISTVIEW_SetSelection(infoPtr, nItem);
9965 if (infoPtr->dwLvExStyle & LVS_EX_ONECLICKACTIVATE)
9966 if(lvHitTestInfo.iItem != -1) notify_itemactivate(infoPtr,&lvHitTestInfo);
9970 if (!infoPtr->bFocus)
9971 SetFocus(infoPtr->hwndSelf);
9973 /* remove all selections */
9974 if (!(wKey & MK_CONTROL) && !(wKey & MK_SHIFT))
9975 LISTVIEW_DeselectAll(infoPtr);
9984 * Processes mouse up messages (left mouse button).
9987 * infoPtr [I ] valid pointer to the listview structure
9988 * wKey [I ] key flag
9989 * x,y [I ] mouse coordinate
9994 static LRESULT LISTVIEW_LButtonUp(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
9996 LVHITTESTINFO lvHitTestInfo;
9998 TRACE("(key=%hu, X=%u, Y=%u)\n", wKey, x, y);
10000 if (!infoPtr->bLButtonDown) return 0;
10002 lvHitTestInfo.pt.x = x;
10003 lvHitTestInfo.pt.y = y;
10005 /* send NM_CLICK notification */
10006 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
10007 if (!notify_click(infoPtr, NM_CLICK, &lvHitTestInfo)) return 0;
10009 /* set left button flag */
10010 infoPtr->bLButtonDown = FALSE;
10012 /* set a single selection, reset others */
10013 if(lvHitTestInfo.iItem == infoPtr->nLButtonDownItem && lvHitTestInfo.iItem != -1)
10014 LISTVIEW_SetSelection(infoPtr, infoPtr->nLButtonDownItem);
10015 infoPtr->nLButtonDownItem = -1;
10017 if (infoPtr->bDragging || infoPtr->bMarqueeSelect)
10019 /* Remove the marquee rectangle and release our mouse capture */
10020 if (infoPtr->bMarqueeSelect)
10022 LISTVIEW_InvalidateRect(infoPtr, &infoPtr->marqueeDrawRect);
10026 SetRect(&infoPtr->marqueeRect, 0, 0, 0, 0);
10027 SetRect(&infoPtr->marqueeDrawRect, 0, 0, 0, 0);
10029 infoPtr->bDragging = FALSE;
10030 infoPtr->bMarqueeSelect = FALSE;
10031 infoPtr->bScrolling = FALSE;
10033 KillTimer(infoPtr->hwndSelf, (UINT_PTR) infoPtr);
10037 /* if we clicked on a selected item, edit the label */
10038 if(lvHitTestInfo.iItem == infoPtr->nEditLabelItem && (lvHitTestInfo.flags & LVHT_ONITEMLABEL))
10040 /* we want to make sure the user doesn't want to do a double click. So we will
10041 * delay the edit. WM_LBUTTONDBLCLICK will cancel the timer
10043 infoPtr->itemEdit.fEnabled = TRUE;
10044 infoPtr->itemEdit.iItem = lvHitTestInfo.iItem;
10045 SetTimer(infoPtr->hwndSelf,
10046 (UINT_PTR)&infoPtr->itemEdit,
10047 GetDoubleClickTime(),
10048 LISTVIEW_DelayedEditItem);
10051 if (!infoPtr->bFocus)
10052 SetFocus(infoPtr->hwndSelf);
10059 * Destroys the listview control (called after WM_DESTROY).
10062 * [I] infoPtr : valid pointer to the listview structure
10067 static LRESULT LISTVIEW_NCDestroy(LISTVIEW_INFO *infoPtr)
10073 /* destroy data structure */
10074 DPA_Destroy(infoPtr->hdpaItems);
10075 DPA_Destroy(infoPtr->hdpaItemIds);
10076 DPA_Destroy(infoPtr->hdpaPosX);
10077 DPA_Destroy(infoPtr->hdpaPosY);
10079 for (i = 0; i < DPA_GetPtrCount(infoPtr->hdpaColumns); i++)
10080 Free(DPA_GetPtr(infoPtr->hdpaColumns, i));
10081 DPA_Destroy(infoPtr->hdpaColumns);
10082 ranges_destroy(infoPtr->selectionRanges);
10084 /* destroy image lists */
10085 if (!(infoPtr->dwStyle & LVS_SHAREIMAGELISTS))
10087 ImageList_Destroy(infoPtr->himlNormal);
10088 ImageList_Destroy(infoPtr->himlSmall);
10089 ImageList_Destroy(infoPtr->himlState);
10092 /* destroy font, bkgnd brush */
10093 infoPtr->hFont = 0;
10094 if (infoPtr->hDefaultFont) DeleteObject(infoPtr->hDefaultFont);
10095 if (infoPtr->clrBk != CLR_NONE) DeleteObject(infoPtr->hBkBrush);
10097 SetWindowLongPtrW(infoPtr->hwndSelf, 0, 0);
10099 /* free listview info pointer*/
10107 * Handles notifications from header.
10110 * [I] infoPtr : valid pointer to the listview structure
10111 * [I] nCtrlId : control identifier
10112 * [I] lpnmh : notification information
10117 static LRESULT LISTVIEW_HeaderNotification(LISTVIEW_INFO *infoPtr, const NMHEADERW *lpnmh)
10119 HWND hwndSelf = infoPtr->hwndSelf;
10121 TRACE("(lpnmh=%p)\n", lpnmh);
10123 if (!lpnmh || lpnmh->iItem < 0 || lpnmh->iItem >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return 0;
10125 switch (lpnmh->hdr.code)
10130 COLUMN_INFO *lpColumnInfo;
10134 if (!lpnmh->pitem || !(lpnmh->pitem->mask & HDI_WIDTH))
10137 /* remove the old line (if any) */
10138 LISTVIEW_DrawTrackLine(infoPtr);
10140 /* compute & draw the new line */
10141 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, lpnmh->iItem);
10142 x = lpColumnInfo->rcHeader.left + lpnmh->pitem->cxy;
10143 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
10144 infoPtr->xTrackLine = x + ptOrigin.x;
10145 LISTVIEW_DrawTrackLine(infoPtr);
10149 case HDN_ENDTRACKA:
10150 case HDN_ENDTRACKW:
10151 /* remove the track line (if any) */
10152 LISTVIEW_DrawTrackLine(infoPtr);
10153 infoPtr->xTrackLine = -1;
10156 case HDN_BEGINDRAG:
10157 notify_forward_header(infoPtr, lpnmh);
10158 return (infoPtr->dwLvExStyle & LVS_EX_HEADERDRAGDROP) == 0;
10161 infoPtr->colRectsDirty = TRUE;
10162 LISTVIEW_InvalidateList(infoPtr);
10163 notify_forward_header(infoPtr, lpnmh);
10166 case HDN_ITEMCHANGINGW:
10167 case HDN_ITEMCHANGINGA:
10168 return notify_forward_header(infoPtr, lpnmh);
10170 case HDN_ITEMCHANGEDW:
10171 case HDN_ITEMCHANGEDA:
10173 COLUMN_INFO *lpColumnInfo;
10177 notify_forward_header(infoPtr, lpnmh);
10178 if (!IsWindow(hwndSelf))
10181 if (!lpnmh->pitem || !(lpnmh->pitem->mask & HDI_WIDTH))
10183 hdi.mask = HDI_WIDTH;
10184 if (!SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, lpnmh->iItem, (LPARAM)&hdi)) return 0;
10188 cxy = lpnmh->pitem->cxy;
10190 /* determine how much we change since the last know position */
10191 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, lpnmh->iItem);
10192 dx = cxy - (lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left);
10195 lpColumnInfo->rcHeader.right += dx;
10197 hdi.mask = HDI_ORDER;
10198 SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, lpnmh->iItem, (LPARAM)&hdi);
10200 /* not the rightmost one */
10201 if (hdi.iOrder + 1 < DPA_GetPtrCount(infoPtr->hdpaColumns))
10203 INT nIndex = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX,
10204 hdi.iOrder + 1, 0);
10205 LISTVIEW_ScrollColumns(infoPtr, nIndex, dx);
10209 /* only needs to update the scrolls */
10210 infoPtr->nItemWidth += dx;
10211 LISTVIEW_UpdateScroll(infoPtr);
10213 LISTVIEW_UpdateItemSize(infoPtr);
10214 if (infoPtr->uView == LV_VIEW_DETAILS && is_redrawing(infoPtr))
10217 RECT rcCol = lpColumnInfo->rcHeader;
10219 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
10220 OffsetRect(&rcCol, ptOrigin.x, 0);
10222 rcCol.top = infoPtr->rcList.top;
10223 rcCol.bottom = infoPtr->rcList.bottom;
10225 /* resizing left-aligned columns leaves most of the left side untouched */
10226 if ((lpColumnInfo->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_LEFT)
10228 INT nMaxDirty = infoPtr->nEllipsisWidth + infoPtr->ntmMaxCharWidth;
10231 rcCol.left = max (rcCol.left, rcCol.right - nMaxDirty);
10234 /* when shrinking the last column clear the now unused field */
10235 if (hdi.iOrder == DPA_GetPtrCount(infoPtr->hdpaColumns) - 1)
10241 /* deal with right from rightmost column area */
10242 right.left = rcCol.right;
10243 right.top = rcCol.top;
10244 right.bottom = rcCol.bottom;
10245 right.right = infoPtr->rcList.right;
10247 LISTVIEW_InvalidateRect(infoPtr, &right);
10250 LISTVIEW_InvalidateRect(infoPtr, &rcCol);
10256 case HDN_ITEMCLICKW:
10257 case HDN_ITEMCLICKA:
10259 /* Handle sorting by Header Column */
10262 ZeroMemory(&nmlv, sizeof(NMLISTVIEW));
10264 nmlv.iSubItem = lpnmh->iItem;
10265 notify_listview(infoPtr, LVN_COLUMNCLICK, &nmlv);
10266 notify_forward_header(infoPtr, lpnmh);
10270 case HDN_DIVIDERDBLCLICKW:
10271 case HDN_DIVIDERDBLCLICKA:
10272 /* FIXME: for LVS_EX_HEADERINALLVIEWS and not LV_VIEW_DETAILS
10273 we should use LVSCW_AUTOSIZE_USEHEADER, helper rework or
10274 split needed for that */
10275 LISTVIEW_SetColumnWidth(infoPtr, lpnmh->iItem, LVSCW_AUTOSIZE);
10276 notify_forward_header(infoPtr, lpnmh);
10285 * Paint non-client area of control.
10288 * [I] infoPtr : valid pointer to the listview structureof the sender
10289 * [I] region : update region
10292 * TRUE - frame was painted
10293 * FALSE - call default window proc
10295 static BOOL LISTVIEW_NCPaint(const LISTVIEW_INFO *infoPtr, HRGN region)
10297 HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
10301 int cxEdge = GetSystemMetrics (SM_CXEDGE),
10302 cyEdge = GetSystemMetrics (SM_CYEDGE);
10305 return DefWindowProcW (infoPtr->hwndSelf, WM_NCPAINT, (WPARAM)region, 0);
10307 GetWindowRect(infoPtr->hwndSelf, &r);
10309 cliprgn = CreateRectRgn (r.left + cxEdge, r.top + cyEdge,
10310 r.right - cxEdge, r.bottom - cyEdge);
10311 if (region != (HRGN)1)
10312 CombineRgn (cliprgn, cliprgn, region, RGN_AND);
10313 OffsetRect(&r, -r.left, -r.top);
10315 dc = GetDCEx(infoPtr->hwndSelf, region, DCX_WINDOW|DCX_INTERSECTRGN);
10316 OffsetRect(&r, -r.left, -r.top);
10318 if (IsThemeBackgroundPartiallyTransparent (theme, 0, 0))
10319 DrawThemeParentBackground(infoPtr->hwndSelf, dc, &r);
10320 DrawThemeBackground (theme, dc, 0, 0, &r, 0);
10321 ReleaseDC(infoPtr->hwndSelf, dc);
10323 /* Call default proc to get the scrollbars etc. painted */
10324 DefWindowProcW (infoPtr->hwndSelf, WM_NCPAINT, (WPARAM)cliprgn, 0);
10331 * Determines the type of structure to use.
10334 * [I] infoPtr : valid pointer to the listview structureof the sender
10335 * [I] hwndFrom : listview window handle
10336 * [I] nCommand : command specifying the nature of the WM_NOTIFYFORMAT
10341 static LRESULT LISTVIEW_NotifyFormat(LISTVIEW_INFO *infoPtr, HWND hwndFrom, INT nCommand)
10343 TRACE("(hwndFrom=%p, nCommand=%d)\n", hwndFrom, nCommand);
10345 if (nCommand == NF_REQUERY)
10346 infoPtr->notifyFormat = SendMessageW(infoPtr->hwndNotify, WM_NOTIFYFORMAT, (WPARAM)infoPtr->hwndSelf, NF_QUERY);
10348 return infoPtr->notifyFormat;
10353 * Paints/Repaints the listview control. Internal use.
10356 * [I] infoPtr : valid pointer to the listview structure
10357 * [I] hdc : device context handle
10362 static LRESULT LISTVIEW_Paint(LISTVIEW_INFO *infoPtr, HDC hdc)
10364 TRACE("(hdc=%p)\n", hdc);
10366 if (infoPtr->bNoItemMetrics && infoPtr->nItemCount)
10368 infoPtr->bNoItemMetrics = FALSE;
10369 LISTVIEW_UpdateItemSize(infoPtr);
10370 if (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON)
10371 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
10372 LISTVIEW_UpdateScroll(infoPtr);
10375 if (infoPtr->hwndHeader) UpdateWindow(infoPtr->hwndHeader);
10378 LISTVIEW_Refresh(infoPtr, hdc, NULL);
10383 hdc = BeginPaint(infoPtr->hwndSelf, &ps);
10384 if (!hdc) return 1;
10385 LISTVIEW_Refresh(infoPtr, hdc, ps.fErase ? &ps.rcPaint : NULL);
10386 EndPaint(infoPtr->hwndSelf, &ps);
10394 * Paints/Repaints the listview control, WM_PAINT handler.
10397 * [I] infoPtr : valid pointer to the listview structure
10398 * [I] hdc : device context handle
10403 static inline LRESULT LISTVIEW_WMPaint(LISTVIEW_INFO *infoPtr, HDC hdc)
10405 TRACE("(hdc=%p)\n", hdc);
10407 if (!is_redrawing(infoPtr))
10408 return DefWindowProcW (infoPtr->hwndSelf, WM_PAINT, (WPARAM)hdc, 0);
10410 return LISTVIEW_Paint(infoPtr, hdc);
10415 * Paints/Repaints the listview control.
10418 * [I] infoPtr : valid pointer to the listview structure
10419 * [I] hdc : device context handle
10420 * [I] options : drawing options
10425 static LRESULT LISTVIEW_PrintClient(LISTVIEW_INFO *infoPtr, HDC hdc, DWORD options)
10427 FIXME("Partial Stub: (hdc=%p options=0x%08x)\n", hdc, options);
10429 if ((options & PRF_CHECKVISIBLE) && !IsWindowVisible(infoPtr->hwndSelf))
10432 if (options & PRF_ERASEBKGND)
10433 LISTVIEW_EraseBkgnd(infoPtr, hdc);
10435 if (options & PRF_CLIENT)
10436 LISTVIEW_Paint(infoPtr, hdc);
10444 * Processes double click messages (right mouse button).
10447 * [I] infoPtr : valid pointer to the listview structure
10448 * [I] wKey : key flag
10449 * [I] x,y : mouse coordinate
10454 static LRESULT LISTVIEW_RButtonDblClk(const LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
10456 LVHITTESTINFO lvHitTestInfo;
10458 TRACE("(key=%hu,X=%u,Y=%u)\n", wKey, x, y);
10460 /* send NM_RELEASEDCAPTURE notification */
10461 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
10463 /* send NM_RDBLCLK notification */
10464 lvHitTestInfo.pt.x = x;
10465 lvHitTestInfo.pt.y = y;
10466 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
10467 notify_click(infoPtr, NM_RDBLCLK, &lvHitTestInfo);
10474 * Processes mouse down messages (right mouse button).
10477 * [I] infoPtr : valid pointer to the listview structure
10478 * [I] wKey : key flag
10479 * [I] x,y : mouse coordinate
10484 static LRESULT LISTVIEW_RButtonDown(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
10486 LVHITTESTINFO lvHitTestInfo;
10489 TRACE("(key=%hu,X=%u,Y=%u)\n", wKey, x, y);
10491 /* send NM_RELEASEDCAPTURE notification */
10492 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
10494 /* make sure the listview control window has the focus */
10495 if (!infoPtr->bFocus) SetFocus(infoPtr->hwndSelf);
10497 /* set right button down flag */
10498 infoPtr->bRButtonDown = TRUE;
10500 /* determine the index of the selected item */
10501 lvHitTestInfo.pt.x = x;
10502 lvHitTestInfo.pt.y = y;
10503 nItem = LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, TRUE);
10505 if ((nItem >= 0) && (nItem < infoPtr->nItemCount))
10507 LISTVIEW_SetItemFocus(infoPtr, nItem);
10508 if (!((wKey & MK_SHIFT) || (wKey & MK_CONTROL)) &&
10509 !LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED))
10510 LISTVIEW_SetSelection(infoPtr, nItem);
10514 LISTVIEW_DeselectAll(infoPtr);
10522 * Processes mouse up messages (right mouse button).
10525 * [I] infoPtr : valid pointer to the listview structure
10526 * [I] wKey : key flag
10527 * [I] x,y : mouse coordinate
10532 static LRESULT LISTVIEW_RButtonUp(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
10534 LVHITTESTINFO lvHitTestInfo;
10537 TRACE("(key=%hu,X=%u,Y=%u)\n", wKey, x, y);
10539 if (!infoPtr->bRButtonDown) return 0;
10541 /* set button flag */
10542 infoPtr->bRButtonDown = FALSE;
10544 /* Send NM_RCLICK notification */
10545 lvHitTestInfo.pt.x = x;
10546 lvHitTestInfo.pt.y = y;
10547 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
10548 if (!notify_click(infoPtr, NM_RCLICK, &lvHitTestInfo)) return 0;
10550 /* Change to screen coordinate for WM_CONTEXTMENU */
10551 pt = lvHitTestInfo.pt;
10552 ClientToScreen(infoPtr->hwndSelf, &pt);
10554 /* Send a WM_CONTEXTMENU message in response to the RBUTTONUP */
10555 SendMessageW(infoPtr->hwndSelf, WM_CONTEXTMENU,
10556 (WPARAM)infoPtr->hwndSelf, MAKELPARAM(pt.x, pt.y));
10567 * [I] infoPtr : valid pointer to the listview structure
10568 * [I] hwnd : window handle of window containing the cursor
10569 * [I] nHittest : hit-test code
10570 * [I] wMouseMsg : ideintifier of the mouse message
10573 * TRUE if cursor is set
10576 static BOOL LISTVIEW_SetCursor(const LISTVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
10578 LVHITTESTINFO lvHitTestInfo;
10580 if (!LISTVIEW_IsHotTracking(infoPtr)) goto forward;
10582 if (!infoPtr->hHotCursor) goto forward;
10584 GetCursorPos(&lvHitTestInfo.pt);
10585 if (LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, FALSE, FALSE) < 0) goto forward;
10587 SetCursor(infoPtr->hHotCursor);
10593 return DefWindowProcW(infoPtr->hwndSelf, WM_SETCURSOR, wParam, lParam);
10601 * [I] infoPtr : valid pointer to the listview structure
10602 * [I] hwndLoseFocus : handle of previously focused window
10607 static LRESULT LISTVIEW_SetFocus(LISTVIEW_INFO *infoPtr, HWND hwndLoseFocus)
10609 TRACE("(hwndLoseFocus=%p)\n", hwndLoseFocus);
10611 /* if we have the focus already, there's nothing to do */
10612 if (infoPtr->bFocus) return 0;
10614 /* send NM_SETFOCUS notification */
10615 if (!notify(infoPtr, NM_SETFOCUS)) return 0;
10617 /* set window focus flag */
10618 infoPtr->bFocus = TRUE;
10620 /* put the focus rect back on */
10621 LISTVIEW_ShowFocusRect(infoPtr, TRUE);
10623 /* redraw all visible selected items */
10624 LISTVIEW_InvalidateSelectedItems(infoPtr);
10634 * [I] infoPtr : valid pointer to the listview structure
10635 * [I] fRedraw : font handle
10636 * [I] fRedraw : redraw flag
10641 static LRESULT LISTVIEW_SetFont(LISTVIEW_INFO *infoPtr, HFONT hFont, WORD fRedraw)
10643 HFONT oldFont = infoPtr->hFont;
10645 TRACE("(hfont=%p,redraw=%hu)\n", hFont, fRedraw);
10647 infoPtr->hFont = hFont ? hFont : infoPtr->hDefaultFont;
10648 if (infoPtr->hFont == oldFont) return 0;
10650 LISTVIEW_SaveTextMetrics(infoPtr);
10652 if (infoPtr->uView == LV_VIEW_DETAILS)
10654 SendMessageW(infoPtr->hwndHeader, WM_SETFONT, (WPARAM)hFont, MAKELPARAM(fRedraw, 0));
10655 LISTVIEW_UpdateSize(infoPtr);
10656 LISTVIEW_UpdateScroll(infoPtr);
10659 if (fRedraw) LISTVIEW_InvalidateList(infoPtr);
10666 * Message handling for WM_SETREDRAW.
10667 * For the Listview, it invalidates the entire window (the doc specifies otherwise)
10670 * [I] infoPtr : valid pointer to the listview structure
10671 * [I] bRedraw: state of redraw flag
10674 * DefWinProc return value
10676 static LRESULT LISTVIEW_SetRedraw(LISTVIEW_INFO *infoPtr, BOOL bRedraw)
10678 TRACE("infoPtr->bRedraw=%d, bRedraw=%d\n", infoPtr->bRedraw, bRedraw);
10680 /* we cannot use straight equality here because _any_ non-zero value is TRUE */
10681 if ((infoPtr->bRedraw && bRedraw) || (!infoPtr->bRedraw && !bRedraw)) return 0;
10683 infoPtr->bRedraw = bRedraw;
10685 if(!bRedraw) return 0;
10687 if (is_autoarrange(infoPtr))
10688 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
10689 LISTVIEW_UpdateScroll(infoPtr);
10691 /* despite what the WM_SETREDRAW docs says, apps expect us
10692 * to invalidate the listview here... stupid! */
10693 LISTVIEW_InvalidateList(infoPtr);
10700 * Resizes the listview control. This function processes WM_SIZE
10701 * messages. At this time, the width and height are not used.
10704 * [I] infoPtr : valid pointer to the listview structure
10705 * [I] Width : new width
10706 * [I] Height : new height
10711 static LRESULT LISTVIEW_Size(LISTVIEW_INFO *infoPtr, int Width, int Height)
10713 RECT rcOld = infoPtr->rcList;
10715 TRACE("(width=%d, height=%d)\n", Width, Height);
10717 LISTVIEW_UpdateSize(infoPtr);
10718 if (EqualRect(&rcOld, &infoPtr->rcList)) return 0;
10720 /* do not bother with display related stuff if we're not redrawing */
10721 if (!is_redrawing(infoPtr)) return 0;
10723 if (is_autoarrange(infoPtr))
10724 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
10726 LISTVIEW_UpdateScroll(infoPtr);
10728 /* refresh all only for lists whose height changed significantly */
10729 if ((infoPtr->uView == LV_VIEW_LIST) &&
10730 (rcOld.bottom - rcOld.top) / infoPtr->nItemHeight !=
10731 (infoPtr->rcList.bottom - infoPtr->rcList.top) / infoPtr->nItemHeight)
10732 LISTVIEW_InvalidateList(infoPtr);
10739 * Sets the size information.
10742 * [I] infoPtr : valid pointer to the listview structure
10747 static void LISTVIEW_UpdateSize(LISTVIEW_INFO *infoPtr)
10749 TRACE("uView=%d, rcList(old)=%s\n", infoPtr->uView, wine_dbgstr_rect(&infoPtr->rcList));
10751 GetClientRect(infoPtr->hwndSelf, &infoPtr->rcList);
10753 if (infoPtr->uView == LV_VIEW_LIST)
10755 /* Apparently the "LIST" style is supposed to have the same
10756 * number of items in a column even if there is no scroll bar.
10757 * Since if a scroll bar already exists then the bottom is already
10758 * reduced, only reduce if the scroll bar does not currently exist.
10759 * The "2" is there to mimic the native control. I think it may be
10760 * related to either padding or edges. (GLA 7/2002)
10762 if (!(infoPtr->dwStyle & WS_HSCROLL))
10763 infoPtr->rcList.bottom -= GetSystemMetrics(SM_CYHSCROLL);
10764 infoPtr->rcList.bottom = max (infoPtr->rcList.bottom - 2, 0);
10767 /* if control created invisible header isn't created */
10768 if (infoPtr->hwndHeader)
10773 hl.prc = &infoPtr->rcList;
10775 SendMessageW( infoPtr->hwndHeader, HDM_LAYOUT, 0, (LPARAM)&hl );
10776 TRACE(" wp.flags=0x%08x, wp=%d,%d (%dx%d)\n", wp.flags, wp.x, wp.y, wp.cx, wp.cy);
10778 if (LISTVIEW_IsHeaderEnabled(infoPtr))
10779 wp.flags |= SWP_SHOWWINDOW;
10782 wp.flags |= SWP_HIDEWINDOW;
10786 SetWindowPos(wp.hwnd, wp.hwndInsertAfter, wp.x, wp.y, wp.cx, wp.cy, wp.flags);
10787 TRACE(" after SWP wp=%d,%d (%dx%d)\n", wp.x, wp.y, wp.cx, wp.cy);
10789 infoPtr->rcList.top = max(wp.cy, 0);
10791 /* extra padding for grid */
10792 if (infoPtr->uView == LV_VIEW_DETAILS && infoPtr->dwLvExStyle & LVS_EX_GRIDLINES)
10793 infoPtr->rcList.top += 2;
10795 TRACE(" rcList=%s\n", wine_dbgstr_rect(&infoPtr->rcList));
10800 * Processes WM_STYLECHANGED messages.
10803 * [I] infoPtr : valid pointer to the listview structure
10804 * [I] wStyleType : window style type (normal or extended)
10805 * [I] lpss : window style information
10810 static INT LISTVIEW_StyleChanged(LISTVIEW_INFO *infoPtr, WPARAM wStyleType,
10811 const STYLESTRUCT *lpss)
10813 UINT uNewView = lpss->styleNew & LVS_TYPEMASK;
10814 UINT uOldView = lpss->styleOld & LVS_TYPEMASK;
10817 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
10818 wStyleType, lpss->styleOld, lpss->styleNew);
10820 if (wStyleType != GWL_STYLE) return 0;
10822 infoPtr->dwStyle = lpss->styleNew;
10823 map_style_view(infoPtr);
10825 if (((lpss->styleOld & WS_HSCROLL) != 0)&&
10826 ((lpss->styleNew & WS_HSCROLL) == 0))
10827 ShowScrollBar(infoPtr->hwndSelf, SB_HORZ, FALSE);
10829 if (((lpss->styleOld & WS_VSCROLL) != 0)&&
10830 ((lpss->styleNew & WS_VSCROLL) == 0))
10831 ShowScrollBar(infoPtr->hwndSelf, SB_VERT, FALSE);
10833 if (uNewView != uOldView)
10835 SIZE oldIconSize = infoPtr->iconSize;
10838 SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
10839 ShowWindow(infoPtr->hwndHeader, SW_HIDE);
10841 ShowScrollBar(infoPtr->hwndSelf, SB_BOTH, FALSE);
10842 SetRectEmpty(&infoPtr->rcFocus);
10844 himl = (uNewView == LVS_ICON ? infoPtr->himlNormal : infoPtr->himlSmall);
10845 set_icon_size(&infoPtr->iconSize, himl, uNewView != LVS_ICON);
10847 if (uNewView == LVS_ICON)
10849 if ((infoPtr->iconSize.cx != oldIconSize.cx) || (infoPtr->iconSize.cy != oldIconSize.cy))
10851 TRACE("icon old size=(%d,%d), new size=(%d,%d)\n",
10852 oldIconSize.cx, oldIconSize.cy, infoPtr->iconSize.cx, infoPtr->iconSize.cy);
10853 LISTVIEW_SetIconSpacing(infoPtr, 0, 0);
10856 else if (uNewView == LVS_REPORT)
10861 LISTVIEW_CreateHeader( infoPtr );
10863 hl.prc = &infoPtr->rcList;
10865 SendMessageW( infoPtr->hwndHeader, HDM_LAYOUT, 0, (LPARAM)&hl );
10866 SetWindowPos(infoPtr->hwndHeader, infoPtr->hwndSelf, wp.x, wp.y, wp.cx, wp.cy,
10867 wp.flags | ((infoPtr->dwStyle & LVS_NOCOLUMNHEADER)
10868 ? SWP_HIDEWINDOW : SWP_SHOWWINDOW));
10871 LISTVIEW_UpdateItemSize(infoPtr);
10874 if (uNewView == LVS_REPORT || infoPtr->dwLvExStyle & LVS_EX_HEADERINALLVIEWS)
10876 if ((lpss->styleOld ^ lpss->styleNew) & LVS_NOCOLUMNHEADER)
10878 if (lpss->styleNew & LVS_NOCOLUMNHEADER)
10880 /* Turn off the header control */
10881 style = GetWindowLongW(infoPtr->hwndHeader, GWL_STYLE);
10882 TRACE("Hide header control, was 0x%08x\n", style);
10883 SetWindowLongW(infoPtr->hwndHeader, GWL_STYLE, style | HDS_HIDDEN);
10885 /* Turn on the header control */
10886 if ((style = GetWindowLongW(infoPtr->hwndHeader, GWL_STYLE)) & HDS_HIDDEN)
10888 TRACE("Show header control, was 0x%08x\n", style);
10889 SetWindowLongW(infoPtr->hwndHeader, GWL_STYLE, (style & ~HDS_HIDDEN) | WS_VISIBLE);
10895 if ( (uNewView == LVS_ICON || uNewView == LVS_SMALLICON) &&
10896 (uNewView != uOldView || ((lpss->styleNew ^ lpss->styleOld) & LVS_ALIGNMASK)) )
10897 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
10899 /* update the size of the client area */
10900 LISTVIEW_UpdateSize(infoPtr);
10902 /* add scrollbars if needed */
10903 LISTVIEW_UpdateScroll(infoPtr);
10905 /* invalidate client area + erase background */
10906 LISTVIEW_InvalidateList(infoPtr);
10913 * Processes WM_STYLECHANGING messages.
10916 * [I] wStyleType : window style type (normal or extended)
10917 * [I0] lpss : window style information
10922 static INT LISTVIEW_StyleChanging(WPARAM wStyleType,
10925 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
10926 wStyleType, lpss->styleOld, lpss->styleNew);
10928 /* don't forward LVS_OWNERDATA only if not already set to */
10929 if ((lpss->styleNew ^ lpss->styleOld) & LVS_OWNERDATA)
10931 if (lpss->styleOld & LVS_OWNERDATA)
10932 lpss->styleNew |= LVS_OWNERDATA;
10934 lpss->styleNew &= ~LVS_OWNERDATA;
10942 * Processes WM_SHOWWINDOW messages.
10945 * [I] infoPtr : valid pointer to the listview structure
10946 * [I] bShown : window is being shown (FALSE when hidden)
10947 * [I] iStatus : window show status
10952 static LRESULT LISTVIEW_ShowWindow(LISTVIEW_INFO *infoPtr, WPARAM bShown, LPARAM iStatus)
10954 /* header delayed creation */
10955 if ((infoPtr->uView == LV_VIEW_DETAILS) && bShown)
10957 LISTVIEW_CreateHeader(infoPtr);
10959 if (!(LVS_NOCOLUMNHEADER & infoPtr->dwStyle))
10960 ShowWindow(infoPtr->hwndHeader, SW_SHOWNORMAL);
10963 return DefWindowProcW(infoPtr->hwndSelf, WM_SHOWWINDOW, bShown, iStatus);
10968 * Processes CCM_GETVERSION messages.
10971 * [I] infoPtr : valid pointer to the listview structure
10976 static inline LRESULT LISTVIEW_GetVersion(const LISTVIEW_INFO *infoPtr)
10978 return infoPtr->iVersion;
10983 * Processes CCM_SETVERSION messages.
10986 * [I] infoPtr : valid pointer to the listview structure
10987 * [I] iVersion : version to be set
10990 * -1 when requested version is greater than DLL version;
10991 * previous version otherwise
10993 static LRESULT LISTVIEW_SetVersion(LISTVIEW_INFO *infoPtr, DWORD iVersion)
10995 INT iOldVersion = infoPtr->iVersion;
10997 if (iVersion > COMCTL32_VERSION)
11000 infoPtr->iVersion = iVersion;
11002 TRACE("new version %d\n", iVersion);
11004 return iOldVersion;
11009 * Window procedure of the listview control.
11012 static LRESULT WINAPI
11013 LISTVIEW_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
11015 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongPtrW(hwnd, 0);
11017 TRACE("(hwnd=%p uMsg=%x wParam=%lx lParam=%lx)\n", hwnd, uMsg, wParam, lParam);
11019 if (!infoPtr && (uMsg != WM_NCCREATE))
11020 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
11024 case LVM_APPROXIMATEVIEWRECT:
11025 return LISTVIEW_ApproximateViewRect(infoPtr, (INT)wParam,
11026 LOWORD(lParam), HIWORD(lParam));
11028 return LISTVIEW_Arrange(infoPtr, (INT)wParam);
11030 case LVM_CANCELEDITLABEL:
11031 return LISTVIEW_CancelEditLabel(infoPtr);
11033 case LVM_CREATEDRAGIMAGE:
11034 return (LRESULT)LISTVIEW_CreateDragImage(infoPtr, (INT)wParam, (LPPOINT)lParam);
11036 case LVM_DELETEALLITEMS:
11037 return LISTVIEW_DeleteAllItems(infoPtr, FALSE);
11039 case LVM_DELETECOLUMN:
11040 return LISTVIEW_DeleteColumn(infoPtr, (INT)wParam);
11042 case LVM_DELETEITEM:
11043 return LISTVIEW_DeleteItem(infoPtr, (INT)wParam);
11045 case LVM_EDITLABELA:
11046 case LVM_EDITLABELW:
11047 return (LRESULT)LISTVIEW_EditLabelT(infoPtr, (INT)wParam,
11048 uMsg == LVM_EDITLABELW);
11049 /* case LVM_ENABLEGROUPVIEW: */
11051 case LVM_ENSUREVISIBLE:
11052 return LISTVIEW_EnsureVisible(infoPtr, (INT)wParam, (BOOL)lParam);
11054 case LVM_FINDITEMW:
11055 return LISTVIEW_FindItemW(infoPtr, (INT)wParam, (LPLVFINDINFOW)lParam);
11057 case LVM_FINDITEMA:
11058 return LISTVIEW_FindItemA(infoPtr, (INT)wParam, (LPLVFINDINFOA)lParam);
11060 case LVM_GETBKCOLOR:
11061 return infoPtr->clrBk;
11063 /* case LVM_GETBKIMAGE: */
11065 case LVM_GETCALLBACKMASK:
11066 return infoPtr->uCallbackMask;
11068 case LVM_GETCOLUMNA:
11069 case LVM_GETCOLUMNW:
11070 return LISTVIEW_GetColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam,
11071 uMsg == LVM_GETCOLUMNW);
11073 case LVM_GETCOLUMNORDERARRAY:
11074 return LISTVIEW_GetColumnOrderArray(infoPtr, (INT)wParam, (LPINT)lParam);
11076 case LVM_GETCOLUMNWIDTH:
11077 return LISTVIEW_GetColumnWidth(infoPtr, (INT)wParam);
11079 case LVM_GETCOUNTPERPAGE:
11080 return LISTVIEW_GetCountPerPage(infoPtr);
11082 case LVM_GETEDITCONTROL:
11083 return (LRESULT)infoPtr->hwndEdit;
11085 case LVM_GETEXTENDEDLISTVIEWSTYLE:
11086 return infoPtr->dwLvExStyle;
11088 /* case LVM_GETGROUPINFO: */
11090 /* case LVM_GETGROUPMETRICS: */
11092 case LVM_GETHEADER:
11093 return (LRESULT)infoPtr->hwndHeader;
11095 case LVM_GETHOTCURSOR:
11096 return (LRESULT)infoPtr->hHotCursor;
11098 case LVM_GETHOTITEM:
11099 return infoPtr->nHotItem;
11101 case LVM_GETHOVERTIME:
11102 return infoPtr->dwHoverTime;
11104 case LVM_GETIMAGELIST:
11105 return (LRESULT)LISTVIEW_GetImageList(infoPtr, (INT)wParam);
11107 /* case LVM_GETINSERTMARK: */
11109 /* case LVM_GETINSERTMARKCOLOR: */
11111 /* case LVM_GETINSERTMARKRECT: */
11113 case LVM_GETISEARCHSTRINGA:
11114 case LVM_GETISEARCHSTRINGW:
11115 FIXME("LVM_GETISEARCHSTRING: unimplemented\n");
11120 return LISTVIEW_GetItemExtT(infoPtr, (LPLVITEMW)lParam, uMsg == LVM_GETITEMW);
11122 case LVM_GETITEMCOUNT:
11123 return infoPtr->nItemCount;
11125 case LVM_GETITEMPOSITION:
11126 return LISTVIEW_GetItemPosition(infoPtr, (INT)wParam, (LPPOINT)lParam);
11128 case LVM_GETITEMRECT:
11129 return LISTVIEW_GetItemRect(infoPtr, (INT)wParam, (LPRECT)lParam);
11131 case LVM_GETITEMSPACING:
11132 return LISTVIEW_GetItemSpacing(infoPtr, (BOOL)wParam);
11134 case LVM_GETITEMSTATE:
11135 return LISTVIEW_GetItemState(infoPtr, (INT)wParam, (UINT)lParam);
11137 case LVM_GETITEMTEXTA:
11138 case LVM_GETITEMTEXTW:
11139 return LISTVIEW_GetItemTextT(infoPtr, (INT)wParam, (LPLVITEMW)lParam,
11140 uMsg == LVM_GETITEMTEXTW);
11142 case LVM_GETNEXTITEM:
11143 return LISTVIEW_GetNextItem(infoPtr, (INT)wParam, LOWORD(lParam));
11145 case LVM_GETNUMBEROFWORKAREAS:
11146 FIXME("LVM_GETNUMBEROFWORKAREAS: unimplemented\n");
11149 case LVM_GETORIGIN:
11150 if (!lParam) return FALSE;
11151 if (infoPtr->uView == LV_VIEW_DETAILS ||
11152 infoPtr->uView == LV_VIEW_LIST) return FALSE;
11153 LISTVIEW_GetOrigin(infoPtr, (LPPOINT)lParam);
11156 /* case LVM_GETOUTLINECOLOR: */
11158 /* case LVM_GETSELECTEDCOLUMN: */
11160 case LVM_GETSELECTEDCOUNT:
11161 return LISTVIEW_GetSelectedCount(infoPtr);
11163 case LVM_GETSELECTIONMARK:
11164 return infoPtr->nSelectionMark;
11166 case LVM_GETSTRINGWIDTHA:
11167 case LVM_GETSTRINGWIDTHW:
11168 return LISTVIEW_GetStringWidthT(infoPtr, (LPCWSTR)lParam,
11169 uMsg == LVM_GETSTRINGWIDTHW);
11171 case LVM_GETSUBITEMRECT:
11172 return LISTVIEW_GetSubItemRect(infoPtr, (UINT)wParam, (LPRECT)lParam);
11174 case LVM_GETTEXTBKCOLOR:
11175 return infoPtr->clrTextBk;
11177 case LVM_GETTEXTCOLOR:
11178 return infoPtr->clrText;
11180 /* case LVM_GETTILEINFO: */
11182 /* case LVM_GETTILEVIEWINFO: */
11184 case LVM_GETTOOLTIPS:
11185 if( !infoPtr->hwndToolTip )
11186 infoPtr->hwndToolTip = COMCTL32_CreateToolTip( hwnd );
11187 return (LRESULT)infoPtr->hwndToolTip;
11189 case LVM_GETTOPINDEX:
11190 return LISTVIEW_GetTopIndex(infoPtr);
11192 case LVM_GETUNICODEFORMAT:
11193 return (infoPtr->notifyFormat == NFR_UNICODE);
11196 return infoPtr->uView;
11198 case LVM_GETVIEWRECT:
11199 return LISTVIEW_GetViewRect(infoPtr, (LPRECT)lParam);
11201 case LVM_GETWORKAREAS:
11202 FIXME("LVM_GETWORKAREAS: unimplemented\n");
11205 /* case LVM_HASGROUP: */
11208 return LISTVIEW_HitTest(infoPtr, (LPLVHITTESTINFO)lParam, FALSE, TRUE);
11210 case LVM_INSERTCOLUMNA:
11211 case LVM_INSERTCOLUMNW:
11212 return LISTVIEW_InsertColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam,
11213 uMsg == LVM_INSERTCOLUMNW);
11215 /* case LVM_INSERTGROUP: */
11217 /* case LVM_INSERTGROUPSORTED: */
11219 case LVM_INSERTITEMA:
11220 case LVM_INSERTITEMW:
11221 return LISTVIEW_InsertItemT(infoPtr, (LPLVITEMW)lParam, uMsg == LVM_INSERTITEMW);
11223 /* case LVM_INSERTMARKHITTEST: */
11225 /* case LVM_ISGROUPVIEWENABLED: */
11227 case LVM_ISITEMVISIBLE:
11228 return LISTVIEW_IsItemVisible(infoPtr, (INT)wParam);
11230 case LVM_MAPIDTOINDEX:
11231 return LISTVIEW_MapIdToIndex(infoPtr, (UINT)wParam);
11233 case LVM_MAPINDEXTOID:
11234 return LISTVIEW_MapIndexToId(infoPtr, (INT)wParam);
11236 /* case LVM_MOVEGROUP: */
11238 /* case LVM_MOVEITEMTOGROUP: */
11240 case LVM_REDRAWITEMS:
11241 return LISTVIEW_RedrawItems(infoPtr, (INT)wParam, (INT)lParam);
11243 /* case LVM_REMOVEALLGROUPS: */
11245 /* case LVM_REMOVEGROUP: */
11248 return LISTVIEW_Scroll(infoPtr, (INT)wParam, (INT)lParam);
11250 case LVM_SETBKCOLOR:
11251 return LISTVIEW_SetBkColor(infoPtr, (COLORREF)lParam);
11253 /* case LVM_SETBKIMAGE: */
11255 case LVM_SETCALLBACKMASK:
11256 infoPtr->uCallbackMask = (UINT)wParam;
11259 case LVM_SETCOLUMNA:
11260 case LVM_SETCOLUMNW:
11261 return LISTVIEW_SetColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam,
11262 uMsg == LVM_SETCOLUMNW);
11264 case LVM_SETCOLUMNORDERARRAY:
11265 return LISTVIEW_SetColumnOrderArray(infoPtr, (INT)wParam, (LPINT)lParam);
11267 case LVM_SETCOLUMNWIDTH:
11268 return LISTVIEW_SetColumnWidth(infoPtr, (INT)wParam, (short)LOWORD(lParam));
11270 case LVM_SETEXTENDEDLISTVIEWSTYLE:
11271 return LISTVIEW_SetExtendedListViewStyle(infoPtr, (DWORD)wParam, (DWORD)lParam);
11273 /* case LVM_SETGROUPINFO: */
11275 /* case LVM_SETGROUPMETRICS: */
11277 case LVM_SETHOTCURSOR:
11278 return (LRESULT)LISTVIEW_SetHotCursor(infoPtr, (HCURSOR)lParam);
11280 case LVM_SETHOTITEM:
11281 return LISTVIEW_SetHotItem(infoPtr, (INT)wParam);
11283 case LVM_SETHOVERTIME:
11284 return LISTVIEW_SetHoverTime(infoPtr, (DWORD)lParam);
11286 case LVM_SETICONSPACING:
11287 return LISTVIEW_SetIconSpacing(infoPtr, (short)LOWORD(lParam), (short)HIWORD(lParam));
11289 case LVM_SETIMAGELIST:
11290 return (LRESULT)LISTVIEW_SetImageList(infoPtr, (INT)wParam, (HIMAGELIST)lParam);
11292 /* case LVM_SETINFOTIP: */
11294 /* case LVM_SETINSERTMARK: */
11296 /* case LVM_SETINSERTMARKCOLOR: */
11301 if (infoPtr->dwStyle & LVS_OWNERDATA) return FALSE;
11302 return LISTVIEW_SetItemT(infoPtr, (LPLVITEMW)lParam, (uMsg == LVM_SETITEMW));
11305 case LVM_SETITEMCOUNT:
11306 return LISTVIEW_SetItemCount(infoPtr, (INT)wParam, (DWORD)lParam);
11308 case LVM_SETITEMPOSITION:
11311 pt.x = (short)LOWORD(lParam);
11312 pt.y = (short)HIWORD(lParam);
11313 return LISTVIEW_SetItemPosition(infoPtr, (INT)wParam, &pt);
11316 case LVM_SETITEMPOSITION32:
11317 return LISTVIEW_SetItemPosition(infoPtr, (INT)wParam, (POINT*)lParam);
11319 case LVM_SETITEMSTATE:
11320 if (lParam == 0) return FALSE;
11321 return LISTVIEW_SetItemState(infoPtr, (INT)wParam, (LPLVITEMW)lParam);
11323 case LVM_SETITEMTEXTA:
11324 case LVM_SETITEMTEXTW:
11325 return LISTVIEW_SetItemTextT(infoPtr, (INT)wParam, (LPLVITEMW)lParam,
11326 uMsg == LVM_SETITEMTEXTW);
11328 /* case LVM_SETOUTLINECOLOR: */
11330 /* case LVM_SETSELECTEDCOLUMN: */
11332 case LVM_SETSELECTIONMARK:
11333 return LISTVIEW_SetSelectionMark(infoPtr, (INT)lParam);
11335 case LVM_SETTEXTBKCOLOR:
11336 return LISTVIEW_SetTextBkColor(infoPtr, (COLORREF)lParam);
11338 case LVM_SETTEXTCOLOR:
11339 return LISTVIEW_SetTextColor(infoPtr, (COLORREF)lParam);
11341 /* case LVM_SETTILEINFO: */
11343 /* case LVM_SETTILEVIEWINFO: */
11345 /* case LVM_SETTILEWIDTH: */
11347 case LVM_SETTOOLTIPS:
11348 return (LRESULT)LISTVIEW_SetToolTips(infoPtr, (HWND)lParam);
11350 case LVM_SETUNICODEFORMAT:
11351 return LISTVIEW_SetUnicodeFormat(infoPtr, wParam);
11354 return LISTVIEW_SetView(infoPtr, wParam);
11356 /* case LVM_SETWORKAREAS: */
11358 /* case LVM_SORTGROUPS: */
11360 case LVM_SORTITEMS:
11361 case LVM_SORTITEMSEX:
11362 return LISTVIEW_SortItems(infoPtr, (PFNLVCOMPARE)lParam, wParam,
11363 uMsg == LVM_SORTITEMSEX);
11364 case LVM_SUBITEMHITTEST:
11365 return LISTVIEW_HitTest(infoPtr, (LPLVHITTESTINFO)lParam, TRUE, FALSE);
11368 return LISTVIEW_Update(infoPtr, (INT)wParam);
11370 case CCM_GETVERSION:
11371 return LISTVIEW_GetVersion(infoPtr);
11373 case CCM_SETVERSION:
11374 return LISTVIEW_SetVersion(infoPtr, wParam);
11377 return LISTVIEW_ProcessLetterKeys( infoPtr, wParam, lParam );
11380 return LISTVIEW_Command(infoPtr, wParam, lParam);
11383 return LISTVIEW_NCCreate(hwnd, (LPCREATESTRUCTW)lParam);
11386 return LISTVIEW_Create(hwnd, (LPCREATESTRUCTW)lParam);
11389 return LISTVIEW_Destroy(infoPtr);
11392 return LISTVIEW_Enable(infoPtr);
11394 case WM_ERASEBKGND:
11395 return LISTVIEW_EraseBkgnd(infoPtr, (HDC)wParam);
11397 case WM_GETDLGCODE:
11398 return DLGC_WANTCHARS | DLGC_WANTARROWS;
11401 return (LRESULT)infoPtr->hFont;
11404 return LISTVIEW_HScroll(infoPtr, (INT)LOWORD(wParam), 0, (HWND)lParam);
11407 return LISTVIEW_KeyDown(infoPtr, (INT)wParam, (LONG)lParam);
11410 return LISTVIEW_KillFocus(infoPtr);
11412 case WM_LBUTTONDBLCLK:
11413 return LISTVIEW_LButtonDblClk(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11415 case WM_LBUTTONDOWN:
11416 return LISTVIEW_LButtonDown(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11419 return LISTVIEW_LButtonUp(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11422 return LISTVIEW_MouseMove (infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11424 case WM_MOUSEHOVER:
11425 return LISTVIEW_MouseHover(infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11428 return LISTVIEW_NCDestroy(infoPtr);
11431 return LISTVIEW_NCPaint(infoPtr, (HRGN)wParam);
11434 if (lParam && ((LPNMHDR)lParam)->hwndFrom == infoPtr->hwndHeader)
11435 return LISTVIEW_HeaderNotification(infoPtr, (LPNMHEADERW)lParam);
11438 case WM_NOTIFYFORMAT:
11439 return LISTVIEW_NotifyFormat(infoPtr, (HWND)wParam, (INT)lParam);
11441 case WM_PRINTCLIENT:
11442 return LISTVIEW_PrintClient(infoPtr, (HDC)wParam, (DWORD)lParam);
11445 return LISTVIEW_WMPaint(infoPtr, (HDC)wParam);
11447 case WM_RBUTTONDBLCLK:
11448 return LISTVIEW_RButtonDblClk(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11450 case WM_RBUTTONDOWN:
11451 return LISTVIEW_RButtonDown(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11454 return LISTVIEW_RButtonUp(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11457 return LISTVIEW_SetCursor(infoPtr, wParam, lParam);
11460 return LISTVIEW_SetFocus(infoPtr, (HWND)wParam);
11463 return LISTVIEW_SetFont(infoPtr, (HFONT)wParam, (WORD)lParam);
11466 return LISTVIEW_SetRedraw(infoPtr, (BOOL)wParam);
11468 case WM_SHOWWINDOW:
11469 return LISTVIEW_ShowWindow(infoPtr, wParam, lParam);
11472 return LISTVIEW_Size(infoPtr, (short)LOWORD(lParam), (short)HIWORD(lParam));
11474 case WM_STYLECHANGED:
11475 return LISTVIEW_StyleChanged(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
11477 case WM_STYLECHANGING:
11478 return LISTVIEW_StyleChanging(wParam, (LPSTYLESTRUCT)lParam);
11480 case WM_SYSCOLORCHANGE:
11481 COMCTL32_RefreshSysColors();
11484 /* case WM_TIMER: */
11485 case WM_THEMECHANGED:
11486 return LISTVIEW_ThemeChanged(infoPtr);
11489 return LISTVIEW_VScroll(infoPtr, (INT)LOWORD(wParam), 0);
11491 case WM_MOUSEWHEEL:
11492 if (wParam & (MK_SHIFT | MK_CONTROL))
11493 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
11494 return LISTVIEW_MouseWheel(infoPtr, (short int)HIWORD(wParam));
11496 case WM_WINDOWPOSCHANGED:
11497 if (!(((WINDOWPOS *)lParam)->flags & SWP_NOSIZE))
11499 SetWindowPos(infoPtr->hwndSelf, 0, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOACTIVATE |
11500 SWP_NOZORDER | SWP_NOMOVE | SWP_NOSIZE);
11502 if ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (infoPtr->uView == LV_VIEW_DETAILS))
11504 if (notify_measureitem(infoPtr)) LISTVIEW_InvalidateList(infoPtr);
11507 LISTVIEW_UpdateSize(infoPtr);
11508 LISTVIEW_UpdateScroll(infoPtr);
11510 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
11512 /* case WM_WININICHANGE: */
11515 if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
11516 ERR("unknown msg %04x wp=%08lx lp=%08lx\n", uMsg, wParam, lParam);
11518 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
11525 * Registers the window class.
11533 void LISTVIEW_Register(void)
11535 WNDCLASSW wndClass;
11537 ZeroMemory(&wndClass, sizeof(WNDCLASSW));
11538 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS;
11539 wndClass.lpfnWndProc = LISTVIEW_WindowProc;
11540 wndClass.cbClsExtra = 0;
11541 wndClass.cbWndExtra = sizeof(LISTVIEW_INFO *);
11542 wndClass.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
11543 wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
11544 wndClass.lpszClassName = WC_LISTVIEWW;
11545 RegisterClassW(&wndClass);
11550 * Unregisters the window class.
11558 void LISTVIEW_Unregister(void)
11560 UnregisterClassW(WC_LISTVIEWW, NULL);
11565 * Handle any WM_COMMAND messages
11568 * [I] infoPtr : valid pointer to the listview structure
11569 * [I] wParam : the first message parameter
11570 * [I] lParam : the second message parameter
11575 static LRESULT LISTVIEW_Command(LISTVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
11578 TRACE("(%p %x %x %lx)\n", infoPtr, HIWORD(wParam), LOWORD(wParam), lParam);
11580 if (!infoPtr->hwndEdit) return 0;
11582 switch (HIWORD(wParam))
11587 * Adjust the edit window size
11589 WCHAR buffer[1024];
11590 HDC hdc = GetDC(infoPtr->hwndEdit);
11591 HFONT hFont, hOldFont = 0;
11595 if (!infoPtr->hwndEdit || !hdc) return 0;
11596 GetWindowTextW(infoPtr->hwndEdit, buffer, sizeof(buffer)/sizeof(buffer[0]));
11597 GetWindowRect(infoPtr->hwndEdit, &rect);
11599 /* Select font to get the right dimension of the string */
11600 hFont = (HFONT)SendMessageW(infoPtr->hwndEdit, WM_GETFONT, 0, 0);
11603 hOldFont = SelectObject(hdc, hFont);
11606 if (GetTextExtentPoint32W(hdc, buffer, lstrlenW(buffer), &sz))
11608 TEXTMETRICW textMetric;
11610 /* Add Extra spacing for the next character */
11611 GetTextMetricsW(hdc, &textMetric);
11612 sz.cx += (textMetric.tmMaxCharWidth * 2);
11614 SetWindowPos(infoPtr->hwndEdit, NULL, 0, 0, sz.cx,
11615 rect.bottom - rect.top, SWP_DRAWFRAME | SWP_NOMOVE | SWP_NOZORDER);
11618 SelectObject(hdc, hOldFont);
11620 ReleaseDC(infoPtr->hwndEdit, hdc);
11626 LISTVIEW_CancelEditLabel(infoPtr);
11631 return SendMessageW (infoPtr->hwndNotify, WM_COMMAND, wParam, lParam);