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, 2010 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 disptextW[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 = disptextW;
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 TRACE("disp text=%s\n", debugtext_t(dispInfo.item.pszText, isW));
5929 /* position and display edit box */
5930 hdc = GetDC(infoPtr->hwndSelf);
5932 /* select the font to get appropriate metric dimensions */
5934 hOldFont = SelectObject(hdc, infoPtr->hFont);
5936 /* use real edit box content, it could be altered during LVN_BEGINLABELEDIT notification */
5937 GetWindowTextW(infoPtr->hwndEdit, disptextW, DISP_TEXT_SIZE);
5938 TRACE("edit box text=%s\n", debugstr_w(disptextW));
5940 /* get string length in pixels */
5941 GetTextExtentPoint32W(hdc, disptextW, lstrlenW(disptextW), &sz);
5943 /* add extra spacing for the next character */
5944 GetTextMetricsW(hdc, &tm);
5945 sz.cx += tm.tmMaxCharWidth * 2;
5948 SelectObject(hdc, hOldFont);
5950 ReleaseDC(infoPtr->hwndSelf, hdc);
5952 sz.cy = rect.bottom - rect.top + 2;
5955 TRACE("moving edit=(%d,%d)-(%d,%d)\n", rect.left, rect.top, sz.cx, sz.cy);
5956 MoveWindow(infoPtr->hwndEdit, rect.left, rect.top, sz.cx, sz.cy, FALSE);
5957 ShowWindow(infoPtr->hwndEdit, SW_NORMAL);
5958 SetFocus(infoPtr->hwndEdit);
5959 SendMessageW(infoPtr->hwndEdit, EM_SETSEL, 0, -1);
5960 return infoPtr->hwndEdit;
5966 * Ensures the specified item is visible, scrolling into view if necessary.
5969 * [I] infoPtr : valid pointer to the listview structure
5970 * [I] nItem : item index
5971 * [I] bPartial : partially or entirely visible
5977 static BOOL LISTVIEW_EnsureVisible(LISTVIEW_INFO *infoPtr, INT nItem, BOOL bPartial)
5979 INT nScrollPosHeight = 0;
5980 INT nScrollPosWidth = 0;
5981 INT nHorzAdjust = 0;
5982 INT nVertAdjust = 0;
5985 RECT rcItem, rcTemp;
5987 rcItem.left = LVIR_BOUNDS;
5988 if (!LISTVIEW_GetItemRect(infoPtr, nItem, &rcItem)) return FALSE;
5990 if (bPartial && IntersectRect(&rcTemp, &infoPtr->rcList, &rcItem)) return TRUE;
5992 if (rcItem.left < infoPtr->rcList.left || rcItem.right > infoPtr->rcList.right)
5994 /* scroll left/right, but in LV_VIEW_DETAILS mode */
5995 if (infoPtr->uView == LV_VIEW_LIST)
5996 nScrollPosWidth = infoPtr->nItemWidth;
5997 else if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
5998 nScrollPosWidth = 1;
6000 if (rcItem.left < infoPtr->rcList.left)
6003 if (infoPtr->uView != LV_VIEW_DETAILS) nHorzDiff = rcItem.left - infoPtr->rcList.left;
6008 if (infoPtr->uView != LV_VIEW_DETAILS) nHorzDiff = rcItem.right - infoPtr->rcList.right;
6012 if (rcItem.top < infoPtr->rcList.top || rcItem.bottom > infoPtr->rcList.bottom)
6014 /* scroll up/down, but not in LVS_LIST mode */
6015 if (infoPtr->uView == LV_VIEW_DETAILS)
6016 nScrollPosHeight = infoPtr->nItemHeight;
6017 else if ((infoPtr->uView == LV_VIEW_ICON) || (infoPtr->uView == LV_VIEW_SMALLICON))
6018 nScrollPosHeight = 1;
6020 if (rcItem.top < infoPtr->rcList.top)
6023 if (infoPtr->uView != LV_VIEW_LIST) nVertDiff = rcItem.top - infoPtr->rcList.top;
6028 if (infoPtr->uView != LV_VIEW_LIST) nVertDiff = rcItem.bottom - infoPtr->rcList.bottom;
6032 if (!nScrollPosWidth && !nScrollPosHeight) return TRUE;
6034 if (nScrollPosWidth)
6036 INT diff = nHorzDiff / nScrollPosWidth;
6037 if (nHorzDiff % nScrollPosWidth) diff += nHorzAdjust;
6038 LISTVIEW_HScroll(infoPtr, SB_INTERNAL, diff, 0);
6041 if (nScrollPosHeight)
6043 INT diff = nVertDiff / nScrollPosHeight;
6044 if (nVertDiff % nScrollPosHeight) diff += nVertAdjust;
6045 LISTVIEW_VScroll(infoPtr, SB_INTERNAL, diff);
6053 * Searches for an item with specific characteristics.
6056 * [I] hwnd : window handle
6057 * [I] nStart : base item index
6058 * [I] lpFindInfo : item information to look for
6061 * SUCCESS : index of item
6064 static INT LISTVIEW_FindItemW(const LISTVIEW_INFO *infoPtr, INT nStart,
6065 const LVFINDINFOW *lpFindInfo)
6067 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
6068 BOOL bWrap = FALSE, bNearest = FALSE;
6069 INT nItem = nStart + 1, nLast = infoPtr->nItemCount, nNearestItem = -1;
6070 ULONG xdist, ydist, dist, mindist = 0x7fffffff;
6071 POINT Position, Destination;
6074 /* Search in virtual listviews should be done by application, not by
6075 listview control, so we just send LVN_ODFINDITEMW and return the result */
6076 if (infoPtr->dwStyle & LVS_OWNERDATA)
6080 nmlv.iStart = nStart;
6081 nmlv.lvfi = *lpFindInfo;
6082 return notify_hdr(infoPtr, LVN_ODFINDITEMW, (LPNMHDR)&nmlv.hdr);
6085 if (!lpFindInfo || nItem < 0) return -1;
6088 if (lpFindInfo->flags & (LVFI_STRING | LVFI_PARTIAL) ||
6089 lpFindInfo->flags & LVFI_SUBSTRING)
6091 lvItem.mask |= LVIF_TEXT;
6092 lvItem.pszText = szDispText;
6093 lvItem.cchTextMax = DISP_TEXT_SIZE;
6096 if (lpFindInfo->flags & LVFI_WRAP)
6099 if ((lpFindInfo->flags & LVFI_NEARESTXY) &&
6100 (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON))
6105 LISTVIEW_GetOrigin(infoPtr, &Origin);
6106 Destination.x = lpFindInfo->pt.x - Origin.x;
6107 Destination.y = lpFindInfo->pt.y - Origin.y;
6108 switch(lpFindInfo->vkDirection)
6110 case VK_DOWN: Destination.y += infoPtr->nItemHeight; break;
6111 case VK_UP: Destination.y -= infoPtr->nItemHeight; break;
6112 case VK_RIGHT: Destination.x += infoPtr->nItemWidth; break;
6113 case VK_LEFT: Destination.x -= infoPtr->nItemWidth; break;
6114 case VK_HOME: Destination.x = Destination.y = 0; break;
6115 case VK_NEXT: Destination.y += infoPtr->rcList.bottom - infoPtr->rcList.top; break;
6116 case VK_PRIOR: Destination.y -= infoPtr->rcList.bottom - infoPtr->rcList.top; break;
6118 LISTVIEW_GetAreaRect(infoPtr, &rcArea);
6119 Destination.x = rcArea.right;
6120 Destination.y = rcArea.bottom;
6122 default: ERR("Unknown vkDirection=%d\n", lpFindInfo->vkDirection);
6126 else Destination.x = Destination.y = 0;
6128 /* if LVFI_PARAM is specified, all other flags are ignored */
6129 if (lpFindInfo->flags & LVFI_PARAM)
6131 lvItem.mask |= LVIF_PARAM;
6133 lvItem.mask &= ~LVIF_TEXT;
6137 for (; nItem < nLast; nItem++)
6139 lvItem.iItem = nItem;
6140 lvItem.iSubItem = 0;
6141 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) continue;
6143 if (lvItem.mask & LVIF_PARAM)
6145 if (lpFindInfo->lParam == lvItem.lParam)
6151 if (lvItem.mask & LVIF_TEXT)
6153 if (lpFindInfo->flags & (LVFI_PARTIAL | LVFI_SUBSTRING))
6155 WCHAR *p = strstrW(lvItem.pszText, lpFindInfo->psz);
6156 if (!p || p != lvItem.pszText) continue;
6160 if (lstrcmpW(lvItem.pszText, lpFindInfo->psz) != 0) continue;
6164 if (!bNearest) return nItem;
6166 /* This is very inefficient. To do a good job here,
6167 * we need a sorted array of (x,y) item positions */
6168 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
6170 /* compute the distance^2 to the destination */
6171 xdist = Destination.x - Position.x;
6172 ydist = Destination.y - Position.y;
6173 dist = xdist * xdist + ydist * ydist;
6175 /* remember the distance, and item if it's closer */
6179 nNearestItem = nItem;
6186 nLast = min(nStart + 1, infoPtr->nItemCount);
6191 return nNearestItem;
6196 * Searches for an item with specific characteristics.
6199 * [I] hwnd : window handle
6200 * [I] nStart : base item index
6201 * [I] lpFindInfo : item information to look for
6204 * SUCCESS : index of item
6207 static INT LISTVIEW_FindItemA(const LISTVIEW_INFO *infoPtr, INT nStart,
6208 const LVFINDINFOA *lpFindInfo)
6210 BOOL hasText = lpFindInfo->flags & (LVFI_STRING | LVFI_PARTIAL) ||
6211 lpFindInfo->flags & LVFI_SUBSTRING;
6216 memcpy(&fiw, lpFindInfo, sizeof(fiw));
6217 if (hasText) fiw.psz = strW = textdupTtoW((LPCWSTR)lpFindInfo->psz, FALSE);
6218 res = LISTVIEW_FindItemW(infoPtr, nStart, &fiw);
6219 textfreeT(strW, FALSE);
6225 * Retrieves column attributes.
6228 * [I] infoPtr : valid pointer to the listview structure
6229 * [I] nColumn : column index
6230 * [IO] lpColumn : column information
6231 * [I] isW : if TRUE, then lpColumn is a LPLVCOLUMNW
6232 * otherwise it is in fact a LPLVCOLUMNA
6238 static BOOL LISTVIEW_GetColumnT(const LISTVIEW_INFO *infoPtr, INT nColumn, LPLVCOLUMNW lpColumn, BOOL isW)
6240 COLUMN_INFO *lpColumnInfo;
6243 if (!lpColumn || nColumn < 0 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
6244 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, nColumn);
6246 /* initialize memory */
6247 ZeroMemory(&hdi, sizeof(hdi));
6249 if (lpColumn->mask & LVCF_TEXT)
6251 hdi.mask |= HDI_TEXT;
6252 hdi.pszText = lpColumn->pszText;
6253 hdi.cchTextMax = lpColumn->cchTextMax;
6256 if (lpColumn->mask & LVCF_IMAGE)
6257 hdi.mask |= HDI_IMAGE;
6259 if (lpColumn->mask & LVCF_ORDER)
6260 hdi.mask |= HDI_ORDER;
6262 if (lpColumn->mask & LVCF_SUBITEM)
6263 hdi.mask |= HDI_LPARAM;
6265 if (!SendMessageW(infoPtr->hwndHeader, isW ? HDM_GETITEMW : HDM_GETITEMA, nColumn, (LPARAM)&hdi)) return FALSE;
6267 if (lpColumn->mask & LVCF_FMT)
6268 lpColumn->fmt = lpColumnInfo->fmt;
6270 if (lpColumn->mask & LVCF_WIDTH)
6271 lpColumn->cx = lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left;
6273 if (lpColumn->mask & LVCF_IMAGE)
6274 lpColumn->iImage = hdi.iImage;
6276 if (lpColumn->mask & LVCF_ORDER)
6277 lpColumn->iOrder = hdi.iOrder;
6279 if (lpColumn->mask & LVCF_SUBITEM)
6280 lpColumn->iSubItem = hdi.lParam;
6282 if (lpColumn->mask & LVCF_MINWIDTH)
6283 lpColumn->cxMin = lpColumnInfo->cxMin;
6289 static BOOL LISTVIEW_GetColumnOrderArray(const LISTVIEW_INFO *infoPtr, INT iCount, LPINT lpiArray)
6291 TRACE("iCount=%d, lpiArray=%p\n", iCount, lpiArray);
6296 return SendMessageW(infoPtr->hwndHeader, HDM_GETORDERARRAY, iCount, (LPARAM)lpiArray);
6301 * Retrieves the column width.
6304 * [I] infoPtr : valid pointer to the listview structure
6305 * [I] int : column index
6308 * SUCCESS : column width
6311 static INT LISTVIEW_GetColumnWidth(const LISTVIEW_INFO *infoPtr, INT nColumn)
6313 INT nColumnWidth = 0;
6316 TRACE("nColumn=%d\n", nColumn);
6318 /* we have a 'column' in LIST and REPORT mode only */
6319 switch(infoPtr->uView)
6322 nColumnWidth = infoPtr->nItemWidth;
6324 case LV_VIEW_DETAILS:
6325 /* We are not using LISTVIEW_GetHeaderRect as this data is updated only after a HDN_ITEMCHANGED.
6326 * There is an application that subclasses the listview, calls LVM_GETCOLUMNWIDTH in the
6327 * HDN_ITEMCHANGED handler and goes into infinite recursion if it receives old data.
6329 * TODO: should we do the same in LVM_GETCOLUMN?
6331 hdItem.mask = HDI_WIDTH;
6332 if (!SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nColumn, (LPARAM)&hdItem))
6334 WARN("(%p): HDM_GETITEMW failed for item %d\n", infoPtr->hwndSelf, nColumn);
6337 nColumnWidth = hdItem.cxy;
6341 TRACE("nColumnWidth=%d\n", nColumnWidth);
6342 return nColumnWidth;
6347 * In list or report display mode, retrieves the number of items that can fit
6348 * vertically in the visible area. In icon or small icon display mode,
6349 * retrieves the total number of visible items.
6352 * [I] infoPtr : valid pointer to the listview structure
6355 * Number of fully visible items.
6357 static INT LISTVIEW_GetCountPerPage(const LISTVIEW_INFO *infoPtr)
6359 switch (infoPtr->uView)
6362 case LV_VIEW_SMALLICON:
6363 return infoPtr->nItemCount;
6364 case LV_VIEW_DETAILS:
6365 return LISTVIEW_GetCountPerColumn(infoPtr);
6367 return LISTVIEW_GetCountPerRow(infoPtr) * LISTVIEW_GetCountPerColumn(infoPtr);
6375 * Retrieves an image list handle.
6378 * [I] infoPtr : valid pointer to the listview structure
6379 * [I] nImageList : image list identifier
6382 * SUCCESS : image list handle
6385 static HIMAGELIST LISTVIEW_GetImageList(const LISTVIEW_INFO *infoPtr, INT nImageList)
6389 case LVSIL_NORMAL: return infoPtr->himlNormal;
6390 case LVSIL_SMALL: return infoPtr->himlSmall;
6391 case LVSIL_STATE: return infoPtr->himlState;
6392 case LVSIL_GROUPHEADER:
6393 FIXME("LVSIL_GROUPHEADER not supported\n");
6396 WARN("got unknown imagelist index - %d\n", nImageList);
6401 /* LISTVIEW_GetISearchString */
6405 * Retrieves item attributes.
6408 * [I] hwnd : window handle
6409 * [IO] lpLVItem : item info
6410 * [I] isW : if TRUE, then lpLVItem is a LPLVITEMW,
6411 * if FALSE, then lpLVItem is a LPLVITEMA.
6414 * This is the internal 'GetItem' interface -- it tries to
6415 * be smart and avoid text copies, if possible, by modifying
6416 * lpLVItem->pszText to point to the text string. Please note
6417 * that this is not always possible (e.g. OWNERDATA), so on
6418 * entry you *must* supply valid values for pszText, and cchTextMax.
6419 * The only difference to the documented interface is that upon
6420 * return, you should use *only* the lpLVItem->pszText, rather than
6421 * the buffer pointer you provided on input. Most code already does
6422 * that, so it's not a problem.
6423 * For the two cases when the text must be copied (that is,
6424 * for LVM_GETITEM, and LVM_GETITEMTEXT), use LISTVIEW_GetItemExtT.
6430 static BOOL LISTVIEW_GetItemT(const LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem, BOOL isW)
6432 ITEMHDR callbackHdr = { LPSTR_TEXTCALLBACKW, I_IMAGECALLBACK };
6433 NMLVDISPINFOW dispInfo;
6439 TRACE("(lpLVItem=%s, isW=%d)\n", debuglvitem_t(lpLVItem, isW), isW);
6441 if (!lpLVItem || lpLVItem->iItem < 0 || lpLVItem->iItem >= infoPtr->nItemCount)
6444 if (lpLVItem->mask == 0) return TRUE;
6446 /* make a local copy */
6447 isubitem = lpLVItem->iSubItem;
6449 /* a quick optimization if all we're asked is the focus state
6450 * these queries are worth optimising since they are common,
6451 * and can be answered in constant time, without the heavy accesses */
6452 if ( (lpLVItem->mask == LVIF_STATE) && (lpLVItem->stateMask == LVIS_FOCUSED) &&
6453 !(infoPtr->uCallbackMask & LVIS_FOCUSED) )
6455 lpLVItem->state = 0;
6456 if (infoPtr->nFocusedItem == lpLVItem->iItem)
6457 lpLVItem->state |= LVIS_FOCUSED;
6461 ZeroMemory(&dispInfo, sizeof(dispInfo));
6463 /* if the app stores all the data, handle it separately */
6464 if (infoPtr->dwStyle & LVS_OWNERDATA)
6466 dispInfo.item.state = 0;
6468 /* apparently, we should not callback for lParam in LVS_OWNERDATA */
6469 if ((lpLVItem->mask & ~(LVIF_STATE | LVIF_PARAM)) ||
6470 ((lpLVItem->mask & LVIF_STATE) && (infoPtr->uCallbackMask & lpLVItem->stateMask)))
6472 UINT mask = lpLVItem->mask;
6474 /* NOTE: copy only fields which we _know_ are initialized, some apps
6475 * depend on the uninitialized fields being 0 */
6476 dispInfo.item.mask = lpLVItem->mask & ~LVIF_PARAM;
6477 dispInfo.item.iItem = lpLVItem->iItem;
6478 dispInfo.item.iSubItem = isubitem;
6479 if (lpLVItem->mask & LVIF_TEXT)
6481 if (lpLVItem->mask & LVIF_NORECOMPUTE)
6483 dispInfo.item.mask &= ~(LVIF_TEXT | LVIF_NORECOMPUTE);
6486 dispInfo.item.pszText = lpLVItem->pszText;
6487 dispInfo.item.cchTextMax = lpLVItem->cchTextMax;
6490 if (lpLVItem->mask & LVIF_STATE)
6491 dispInfo.item.stateMask = lpLVItem->stateMask & infoPtr->uCallbackMask;
6492 /* could be zeroed on LVIF_NORECOMPUTE case */
6493 if (dispInfo.item.mask != 0)
6495 notify_dispinfoT(infoPtr, LVN_GETDISPINFOW, &dispInfo, isW);
6496 dispInfo.item.stateMask = lpLVItem->stateMask;
6497 if (lpLVItem->mask & (LVIF_GROUPID|LVIF_COLUMNS))
6499 /* full size structure expected - _WIN32IE >= 0x560 */
6500 *lpLVItem = dispInfo.item;
6502 else if (lpLVItem->mask & LVIF_INDENT)
6504 /* indent member expected - _WIN32IE >= 0x300 */
6505 memcpy(lpLVItem, &dispInfo.item, offsetof( LVITEMW, iGroupId ));
6509 /* minimal structure expected */
6510 memcpy(lpLVItem, &dispInfo.item, offsetof( LVITEMW, iIndent ));
6512 lpLVItem->mask = mask;
6513 TRACE(" getdispinfo(1):lpLVItem=%s\n", debuglvitem_t(lpLVItem, isW));
6517 /* make sure lParam is zeroed out */
6518 if (lpLVItem->mask & LVIF_PARAM) lpLVItem->lParam = 0;
6520 /* callback marked pointer required here */
6521 if ((lpLVItem->mask & LVIF_TEXT) && (lpLVItem->mask & LVIF_NORECOMPUTE))
6522 lpLVItem->pszText = LPSTR_TEXTCALLBACKW;
6524 /* we store only a little state, so if we're not asked, we're done */
6525 if (!(lpLVItem->mask & LVIF_STATE) || isubitem) return TRUE;
6527 /* if focus is handled by us, report it */
6528 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_FOCUSED )
6530 lpLVItem->state &= ~LVIS_FOCUSED;
6531 if (infoPtr->nFocusedItem == lpLVItem->iItem)
6532 lpLVItem->state |= LVIS_FOCUSED;
6535 /* and do the same for selection, if we handle it */
6536 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_SELECTED )
6538 lpLVItem->state &= ~LVIS_SELECTED;
6539 if (ranges_contain(infoPtr->selectionRanges, lpLVItem->iItem))
6540 lpLVItem->state |= LVIS_SELECTED;
6546 /* find the item and subitem structures before we proceed */
6547 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, lpLVItem->iItem);
6548 lpItem = DPA_GetPtr(hdpaSubItems, 0);
6553 SUBITEM_INFO *lpSubItem = LISTVIEW_GetSubItemPtr(hdpaSubItems, isubitem);
6554 pItemHdr = lpSubItem ? &lpSubItem->hdr : &callbackHdr;
6557 WARN(" iSubItem invalid (%08x), ignored.\n", isubitem);
6562 pItemHdr = &lpItem->hdr;
6564 /* Do we need to query the state from the app? */
6565 if ((lpLVItem->mask & LVIF_STATE) && infoPtr->uCallbackMask && isubitem == 0)
6567 dispInfo.item.mask |= LVIF_STATE;
6568 dispInfo.item.stateMask = infoPtr->uCallbackMask;
6571 /* Do we need to enquire about the image? */
6572 if ((lpLVItem->mask & LVIF_IMAGE) && pItemHdr->iImage == I_IMAGECALLBACK &&
6573 (isubitem == 0 || (infoPtr->dwLvExStyle & LVS_EX_SUBITEMIMAGES)))
6575 dispInfo.item.mask |= LVIF_IMAGE;
6576 dispInfo.item.iImage = I_IMAGECALLBACK;
6579 /* Only items support indentation */
6580 if ((lpLVItem->mask & LVIF_INDENT) && lpItem->iIndent == I_INDENTCALLBACK &&
6583 dispInfo.item.mask |= LVIF_INDENT;
6584 dispInfo.item.iIndent = I_INDENTCALLBACK;
6587 /* Apps depend on calling back for text if it is NULL or LPSTR_TEXTCALLBACKW */
6588 if ((lpLVItem->mask & LVIF_TEXT) && !(lpLVItem->mask & LVIF_NORECOMPUTE) &&
6589 !is_text(pItemHdr->pszText))
6591 dispInfo.item.mask |= LVIF_TEXT;
6592 dispInfo.item.pszText = lpLVItem->pszText;
6593 dispInfo.item.cchTextMax = lpLVItem->cchTextMax;
6594 if (dispInfo.item.pszText && dispInfo.item.cchTextMax > 0)
6595 *dispInfo.item.pszText = '\0';
6598 /* If we don't have all the requested info, query the application */
6599 if (dispInfo.item.mask != 0)
6601 dispInfo.item.iItem = lpLVItem->iItem;
6602 dispInfo.item.iSubItem = lpLVItem->iSubItem; /* yes: the original subitem */
6603 dispInfo.item.lParam = lpItem->lParam;
6604 notify_dispinfoT(infoPtr, LVN_GETDISPINFOW, &dispInfo, isW);
6605 TRACE(" getdispinfo(2):item=%s\n", debuglvitem_t(&dispInfo.item, isW));
6608 /* we should not store values for subitems */
6609 if (isubitem) dispInfo.item.mask &= ~LVIF_DI_SETITEM;
6611 /* Now, handle the iImage field */
6612 if (dispInfo.item.mask & LVIF_IMAGE)
6614 lpLVItem->iImage = dispInfo.item.iImage;
6615 if ((dispInfo.item.mask & LVIF_DI_SETITEM) && pItemHdr->iImage == I_IMAGECALLBACK)
6616 pItemHdr->iImage = dispInfo.item.iImage;
6618 else if (lpLVItem->mask & LVIF_IMAGE)
6620 if(isubitem == 0 || (infoPtr->dwLvExStyle & LVS_EX_SUBITEMIMAGES))
6621 lpLVItem->iImage = pItemHdr->iImage;
6623 lpLVItem->iImage = 0;
6626 /* The pszText field */
6627 if (dispInfo.item.mask & LVIF_TEXT)
6629 if ((dispInfo.item.mask & LVIF_DI_SETITEM) && pItemHdr->pszText)
6630 textsetptrT(&pItemHdr->pszText, dispInfo.item.pszText, isW);
6632 lpLVItem->pszText = dispInfo.item.pszText;
6634 else if (lpLVItem->mask & LVIF_TEXT)
6636 /* if LVN_GETDISPINFO's disabled with LVIF_NORECOMPUTE return callback placeholder */
6637 if (isW || !is_text(pItemHdr->pszText)) lpLVItem->pszText = pItemHdr->pszText;
6638 else textcpynT(lpLVItem->pszText, isW, pItemHdr->pszText, TRUE, lpLVItem->cchTextMax);
6641 /* Next is the lParam field */
6642 if (dispInfo.item.mask & LVIF_PARAM)
6644 lpLVItem->lParam = dispInfo.item.lParam;
6645 if ((dispInfo.item.mask & LVIF_DI_SETITEM))
6646 lpItem->lParam = dispInfo.item.lParam;
6648 else if (lpLVItem->mask & LVIF_PARAM)
6649 lpLVItem->lParam = lpItem->lParam;
6651 /* if this is a subitem, we're done */
6652 if (isubitem) return TRUE;
6654 /* ... the state field (this one is different due to uCallbackmask) */
6655 if (lpLVItem->mask & LVIF_STATE)
6657 lpLVItem->state = lpItem->state & lpLVItem->stateMask;
6658 if (dispInfo.item.mask & LVIF_STATE)
6660 lpLVItem->state &= ~dispInfo.item.stateMask;
6661 lpLVItem->state |= (dispInfo.item.state & dispInfo.item.stateMask);
6663 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_FOCUSED )
6665 lpLVItem->state &= ~LVIS_FOCUSED;
6666 if (infoPtr->nFocusedItem == lpLVItem->iItem)
6667 lpLVItem->state |= LVIS_FOCUSED;
6669 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_SELECTED )
6671 lpLVItem->state &= ~LVIS_SELECTED;
6672 if (ranges_contain(infoPtr->selectionRanges, lpLVItem->iItem))
6673 lpLVItem->state |= LVIS_SELECTED;
6677 /* and last, but not least, the indent field */
6678 if (dispInfo.item.mask & LVIF_INDENT)
6680 lpLVItem->iIndent = dispInfo.item.iIndent;
6681 if ((dispInfo.item.mask & LVIF_DI_SETITEM) && lpItem->iIndent == I_INDENTCALLBACK)
6682 lpItem->iIndent = dispInfo.item.iIndent;
6684 else if (lpLVItem->mask & LVIF_INDENT)
6686 lpLVItem->iIndent = lpItem->iIndent;
6694 * Retrieves item attributes.
6697 * [I] hwnd : window handle
6698 * [IO] lpLVItem : item info
6699 * [I] isW : if TRUE, then lpLVItem is a LPLVITEMW,
6700 * if FALSE, then lpLVItem is a LPLVITEMA.
6703 * This is the external 'GetItem' interface -- it properly copies
6704 * the text in the provided buffer.
6710 static BOOL LISTVIEW_GetItemExtT(const LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem, BOOL isW)
6715 if (!lpLVItem || lpLVItem->iItem < 0 || lpLVItem->iItem >= infoPtr->nItemCount)
6718 pszText = lpLVItem->pszText;
6719 bResult = LISTVIEW_GetItemT(infoPtr, lpLVItem, isW);
6720 if (bResult && lpLVItem->pszText != pszText)
6722 if (lpLVItem->pszText != LPSTR_TEXTCALLBACKW)
6723 textcpynT(pszText, isW, lpLVItem->pszText, isW, lpLVItem->cchTextMax);
6725 pszText = LPSTR_TEXTCALLBACKW;
6727 lpLVItem->pszText = pszText;
6735 * Retrieves the position (upper-left) of the listview control item.
6736 * Note that for LVS_ICON style, the upper-left is that of the icon
6737 * and not the bounding box.
6740 * [I] infoPtr : valid pointer to the listview structure
6741 * [I] nItem : item index
6742 * [O] lpptPosition : coordinate information
6748 static BOOL LISTVIEW_GetItemPosition(const LISTVIEW_INFO *infoPtr, INT nItem, LPPOINT lpptPosition)
6752 TRACE("(nItem=%d, lpptPosition=%p)\n", nItem, lpptPosition);
6754 if (!lpptPosition || nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
6756 LISTVIEW_GetOrigin(infoPtr, &Origin);
6757 LISTVIEW_GetItemOrigin(infoPtr, nItem, lpptPosition);
6759 if (infoPtr->uView == LV_VIEW_ICON)
6761 lpptPosition->x += (infoPtr->nItemWidth - infoPtr->iconSize.cx) / 2;
6762 lpptPosition->y += ICON_TOP_PADDING;
6764 lpptPosition->x += Origin.x;
6765 lpptPosition->y += Origin.y;
6767 TRACE (" lpptPosition=%s\n", wine_dbgstr_point(lpptPosition));
6774 * Retrieves the bounding rectangle for a listview control item.
6777 * [I] infoPtr : valid pointer to the listview structure
6778 * [I] nItem : item index
6779 * [IO] lprc : bounding rectangle coordinates
6780 * lprc->left specifies the portion of the item for which the bounding
6781 * rectangle will be retrieved.
6783 * LVIR_BOUNDS Returns the bounding rectangle of the entire item,
6784 * including the icon and label.
6787 * * Experiment shows that native control returns:
6788 * * width = min (48, length of text line)
6789 * * .left = position.x - (width - iconsize.cx)/2
6790 * * .right = .left + width
6791 * * height = #lines of text * ntmHeight + icon height + 8
6792 * * .top = position.y - 2
6793 * * .bottom = .top + height
6794 * * separation between items .y = itemSpacing.cy - height
6795 * * .x = itemSpacing.cx - width
6796 * LVIR_ICON Returns the bounding rectangle of the icon or small icon.
6799 * * Experiment shows that native control returns:
6800 * * width = iconSize.cx + 16
6801 * * .left = position.x - (width - iconsize.cx)/2
6802 * * .right = .left + width
6803 * * height = iconSize.cy + 4
6804 * * .top = position.y - 2
6805 * * .bottom = .top + height
6806 * * separation between items .y = itemSpacing.cy - height
6807 * * .x = itemSpacing.cx - width
6808 * LVIR_LABEL Returns the bounding rectangle of the item text.
6811 * * Experiment shows that native control returns:
6812 * * width = text length
6813 * * .left = position.x - width/2
6814 * * .right = .left + width
6815 * * height = ntmH * linecount + 2
6816 * * .top = position.y + iconSize.cy + 6
6817 * * .bottom = .top + height
6818 * * separation between items .y = itemSpacing.cy - height
6819 * * .x = itemSpacing.cx - width
6820 * LVIR_SELECTBOUNDS Returns the union of the LVIR_ICON and LVIR_LABEL
6821 * rectangles, but excludes columns in report view.
6828 * Note that the bounding rectangle of the label in the LVS_ICON view depends
6829 * upon whether the window has the focus currently and on whether the item
6830 * is the one with the focus. Ensure that the control's record of which
6831 * item has the focus agrees with the items' records.
6833 static BOOL LISTVIEW_GetItemRect(const LISTVIEW_INFO *infoPtr, INT nItem, LPRECT lprc)
6835 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
6836 BOOL doLabel = TRUE, oversizedBox = FALSE;
6837 POINT Position, Origin;
6841 TRACE("(hwnd=%p, nItem=%d, lprc=%p)\n", infoPtr->hwndSelf, nItem, lprc);
6843 if (!lprc || nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
6845 LISTVIEW_GetOrigin(infoPtr, &Origin);
6846 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
6848 /* Be smart and try to figure out the minimum we have to do */
6849 if (lprc->left == LVIR_ICON) doLabel = FALSE;
6850 if (infoPtr->uView == LV_VIEW_DETAILS && lprc->left == LVIR_BOUNDS) doLabel = FALSE;
6851 if (infoPtr->uView == LV_VIEW_ICON && lprc->left != LVIR_ICON &&
6852 infoPtr->bFocus && LISTVIEW_GetItemState(infoPtr, nItem, LVIS_FOCUSED))
6853 oversizedBox = TRUE;
6855 /* get what we need from the item before hand, so we make
6856 * only one request. This can speed up things, if data
6857 * is stored on the app side */
6859 if (infoPtr->uView == LV_VIEW_DETAILS) lvItem.mask |= LVIF_INDENT;
6860 if (doLabel) lvItem.mask |= LVIF_TEXT;
6861 lvItem.iItem = nItem;
6862 lvItem.iSubItem = 0;
6863 lvItem.pszText = szDispText;
6864 lvItem.cchTextMax = DISP_TEXT_SIZE;
6865 if (lvItem.mask && !LISTVIEW_GetItemW(infoPtr, &lvItem)) return FALSE;
6866 /* we got the state already up, simulate it here, to avoid a reget */
6867 if (infoPtr->uView == LV_VIEW_ICON && (lprc->left != LVIR_ICON))
6869 lvItem.mask |= LVIF_STATE;
6870 lvItem.stateMask = LVIS_FOCUSED;
6871 lvItem.state = (oversizedBox ? LVIS_FOCUSED : 0);
6874 if (infoPtr->uView == LV_VIEW_DETAILS && (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) && lprc->left == LVIR_SELECTBOUNDS)
6875 lprc->left = LVIR_BOUNDS;
6881 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, NULL, lprc, NULL, NULL);
6885 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, NULL, NULL, NULL, lprc);
6889 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, lprc, NULL, NULL, NULL, NULL);
6892 case LVIR_SELECTBOUNDS:
6893 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, lprc, NULL, NULL, NULL);
6897 WARN("Unknown value: %d\n", lprc->left);
6901 if (infoPtr->uView == LV_VIEW_DETAILS)
6903 if (mode != LVIR_BOUNDS)
6904 OffsetRect(lprc, Origin.x + LISTVIEW_GetColumnInfo(infoPtr, 0)->rcHeader.left,
6905 Position.y + Origin.y);
6907 OffsetRect(lprc, Origin.x, Position.y + Origin.y);
6910 OffsetRect(lprc, Position.x + Origin.x, Position.y + Origin.y);
6912 TRACE(" rect=%s\n", wine_dbgstr_rect(lprc));
6919 * Retrieves the spacing between listview control items.
6922 * [I] infoPtr : valid pointer to the listview structure
6923 * [IO] lprc : rectangle to receive the output
6924 * on input, lprc->top = nSubItem
6925 * lprc->left = LVIR_ICON | LVIR_BOUNDS | LVIR_LABEL
6927 * NOTE: for subItem = 0, we should return the bounds of the _entire_ item,
6928 * not only those of the first column.
6929 * Fortunately, LISTVIEW_GetItemMetrics does the right thing.
6935 static BOOL LISTVIEW_GetSubItemRect(const LISTVIEW_INFO *infoPtr, INT nItem, LPRECT lprc)
6937 POINT Position, Origin;
6941 if (!lprc) return FALSE;
6943 nColumn = lprc->top;
6945 TRACE("(nItem=%d, nSubItem=%d, type=%d)\n", nItem, lprc->top, lprc->left);
6946 /* On WinNT, a subitem of '0' calls LISTVIEW_GetItemRect */
6948 return LISTVIEW_GetItemRect(infoPtr, nItem, lprc);
6950 if (infoPtr->uView != LV_VIEW_DETAILS) return FALSE;
6952 /* special case for header items */
6955 if (lprc->left != LVIR_BOUNDS)
6957 FIXME("Only LVIR_BOUNDS is implemented for header, got %d\n", lprc->left);
6961 if (infoPtr->hwndHeader)
6962 return SendMessageW(infoPtr->hwndHeader, HDM_GETITEMRECT, lprc->top, (LPARAM)lprc);
6965 memset(lprc, 0, sizeof(RECT));
6970 if (!LISTVIEW_GetItemPosition(infoPtr, nItem, &Position)) return FALSE;
6971 LISTVIEW_GetOrigin(infoPtr, &Origin);
6973 if (nColumn < 0 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
6976 lvItem.iItem = nItem;
6977 lvItem.iSubItem = nColumn;
6982 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, NULL, lprc, NULL, NULL);
6987 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, lprc, NULL, NULL, NULL, NULL);
6991 ERR("Unknown bounds=%d\n", lprc->left);
6995 OffsetRect(lprc, Origin.x, Position.y);
6996 TRACE("return rect %s\n", wine_dbgstr_rect(lprc));
7003 * Retrieves the spacing between listview control items.
7006 * [I] infoPtr : valid pointer to the listview structure
7007 * [I] bSmall : flag for small or large icon
7010 * Horizontal + vertical spacing
7012 static LONG LISTVIEW_GetItemSpacing(const LISTVIEW_INFO *infoPtr, BOOL bSmall)
7018 lResult = MAKELONG(infoPtr->iconSpacing.cx, infoPtr->iconSpacing.cy);
7022 if (infoPtr->uView == LV_VIEW_ICON)
7023 lResult = MAKELONG(DEFAULT_COLUMN_WIDTH, GetSystemMetrics(SM_CXSMICON)+HEIGHT_PADDING);
7025 lResult = MAKELONG(infoPtr->nItemWidth, infoPtr->nItemHeight);
7032 * Retrieves the state of a listview control item.
7035 * [I] infoPtr : valid pointer to the listview structure
7036 * [I] nItem : item index
7037 * [I] uMask : state mask
7040 * State specified by the mask.
7042 static UINT LISTVIEW_GetItemState(const LISTVIEW_INFO *infoPtr, INT nItem, UINT uMask)
7046 if (nItem < 0 || nItem >= infoPtr->nItemCount) return 0;
7048 lvItem.iItem = nItem;
7049 lvItem.iSubItem = 0;
7050 lvItem.mask = LVIF_STATE;
7051 lvItem.stateMask = uMask;
7052 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return 0;
7054 return lvItem.state & uMask;
7059 * Retrieves the text of a listview control item or subitem.
7062 * [I] hwnd : window handle
7063 * [I] nItem : item index
7064 * [IO] lpLVItem : item information
7065 * [I] isW : TRUE if lpLVItem is Unicode
7068 * SUCCESS : string length
7071 static INT LISTVIEW_GetItemTextT(const LISTVIEW_INFO *infoPtr, INT nItem, LPLVITEMW lpLVItem, BOOL isW)
7073 if (!lpLVItem || nItem < 0 || nItem >= infoPtr->nItemCount) return 0;
7075 lpLVItem->mask = LVIF_TEXT;
7076 lpLVItem->iItem = nItem;
7077 if (!LISTVIEW_GetItemExtT(infoPtr, lpLVItem, isW)) return 0;
7079 return textlenT(lpLVItem->pszText, isW);
7084 * Searches for an item based on properties + relationships.
7087 * [I] infoPtr : valid pointer to the listview structure
7088 * [I] nItem : item index
7089 * [I] uFlags : relationship flag
7092 * SUCCESS : item index
7095 static INT LISTVIEW_GetNextItem(const LISTVIEW_INFO *infoPtr, INT nItem, UINT uFlags)
7098 LVFINDINFOW lvFindInfo;
7099 INT nCountPerColumn;
7103 TRACE("nItem=%d, uFlags=%x, nItemCount=%d\n", nItem, uFlags, infoPtr->nItemCount);
7104 if (nItem < -1 || nItem >= infoPtr->nItemCount) return -1;
7106 ZeroMemory(&lvFindInfo, sizeof(lvFindInfo));
7108 if (uFlags & LVNI_CUT)
7111 if (uFlags & LVNI_DROPHILITED)
7112 uMask |= LVIS_DROPHILITED;
7114 if (uFlags & LVNI_FOCUSED)
7115 uMask |= LVIS_FOCUSED;
7117 if (uFlags & LVNI_SELECTED)
7118 uMask |= LVIS_SELECTED;
7120 /* if we're asked for the focused item, that's only one,
7121 * so it's worth optimizing */
7122 if (uFlags & LVNI_FOCUSED)
7124 if ((LISTVIEW_GetItemState(infoPtr, infoPtr->nFocusedItem, uMask) & uMask) != uMask) return -1;
7125 return (infoPtr->nFocusedItem == nItem) ? -1 : infoPtr->nFocusedItem;
7128 if (uFlags & LVNI_ABOVE)
7130 if ((infoPtr->uView == LV_VIEW_LIST) || (infoPtr->uView == LV_VIEW_DETAILS))
7135 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7141 /* Special case for autoarrange - move 'til the top of a list */
7142 if (is_autoarrange(infoPtr))
7144 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
7145 while (nItem - nCountPerRow >= 0)
7147 nItem -= nCountPerRow;
7148 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7153 lvFindInfo.flags = LVFI_NEARESTXY;
7154 lvFindInfo.vkDirection = VK_UP;
7155 LISTVIEW_GetItemPosition(infoPtr, nItem, &lvFindInfo.pt);
7156 while ((nItem = LISTVIEW_FindItemW(infoPtr, nItem, &lvFindInfo)) != -1)
7158 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7163 else if (uFlags & LVNI_BELOW)
7165 if ((infoPtr->uView == LV_VIEW_LIST) || (infoPtr->uView == LV_VIEW_DETAILS))
7167 while (nItem < infoPtr->nItemCount)
7170 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7176 /* Special case for autoarrange - move 'til the bottom of a list */
7177 if (is_autoarrange(infoPtr))
7179 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
7180 while (nItem + nCountPerRow < infoPtr->nItemCount )
7182 nItem += nCountPerRow;
7183 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7188 lvFindInfo.flags = LVFI_NEARESTXY;
7189 lvFindInfo.vkDirection = VK_DOWN;
7190 LISTVIEW_GetItemPosition(infoPtr, nItem, &lvFindInfo.pt);
7191 while ((nItem = LISTVIEW_FindItemW(infoPtr, nItem, &lvFindInfo)) != -1)
7193 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7198 else if (uFlags & LVNI_TOLEFT)
7200 if (infoPtr->uView == LV_VIEW_LIST)
7202 nCountPerColumn = LISTVIEW_GetCountPerColumn(infoPtr);
7203 while (nItem - nCountPerColumn >= 0)
7205 nItem -= nCountPerColumn;
7206 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7210 else if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
7212 /* Special case for autoarrange - move 'til the beginning of a row */
7213 if (is_autoarrange(infoPtr))
7215 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
7216 while (nItem % nCountPerRow > 0)
7219 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7224 lvFindInfo.flags = LVFI_NEARESTXY;
7225 lvFindInfo.vkDirection = VK_LEFT;
7226 LISTVIEW_GetItemPosition(infoPtr, nItem, &lvFindInfo.pt);
7227 while ((nItem = LISTVIEW_FindItemW(infoPtr, nItem, &lvFindInfo)) != -1)
7229 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7234 else if (uFlags & LVNI_TORIGHT)
7236 if (infoPtr->uView == LV_VIEW_LIST)
7238 nCountPerColumn = LISTVIEW_GetCountPerColumn(infoPtr);
7239 while (nItem + nCountPerColumn < infoPtr->nItemCount)
7241 nItem += nCountPerColumn;
7242 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7246 else if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
7248 /* Special case for autoarrange - move 'til the end of a row */
7249 if (is_autoarrange(infoPtr))
7251 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
7252 while (nItem % nCountPerRow < nCountPerRow - 1 )
7255 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7260 lvFindInfo.flags = LVFI_NEARESTXY;
7261 lvFindInfo.vkDirection = VK_RIGHT;
7262 LISTVIEW_GetItemPosition(infoPtr, nItem, &lvFindInfo.pt);
7263 while ((nItem = LISTVIEW_FindItemW(infoPtr, nItem, &lvFindInfo)) != -1)
7265 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7274 /* search by index */
7275 for (i = nItem; i < infoPtr->nItemCount; i++)
7277 if ((LISTVIEW_GetItemState(infoPtr, i, uMask) & uMask) == uMask)
7285 /* LISTVIEW_GetNumberOfWorkAreas */
7289 * Retrieves the origin coordinates when in icon or small icon display mode.
7292 * [I] infoPtr : valid pointer to the listview structure
7293 * [O] lpptOrigin : coordinate information
7298 static void LISTVIEW_GetOrigin(const LISTVIEW_INFO *infoPtr, LPPOINT lpptOrigin)
7300 INT nHorzPos = 0, nVertPos = 0;
7301 SCROLLINFO scrollInfo;
7303 scrollInfo.cbSize = sizeof(SCROLLINFO);
7304 scrollInfo.fMask = SIF_POS;
7306 if (GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo))
7307 nHorzPos = scrollInfo.nPos;
7308 if (GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
7309 nVertPos = scrollInfo.nPos;
7311 TRACE("nHorzPos=%d, nVertPos=%d\n", nHorzPos, nVertPos);
7313 lpptOrigin->x = infoPtr->rcList.left;
7314 lpptOrigin->y = infoPtr->rcList.top;
7315 if (infoPtr->uView == LV_VIEW_LIST)
7316 nHorzPos *= infoPtr->nItemWidth;
7317 else if (infoPtr->uView == LV_VIEW_DETAILS)
7318 nVertPos *= infoPtr->nItemHeight;
7320 lpptOrigin->x -= nHorzPos;
7321 lpptOrigin->y -= nVertPos;
7323 TRACE(" origin=%s\n", wine_dbgstr_point(lpptOrigin));
7328 * Retrieves the width of a string.
7331 * [I] hwnd : window handle
7332 * [I] lpszText : text string to process
7333 * [I] isW : TRUE if lpszText is Unicode, FALSE otherwise
7336 * SUCCESS : string width (in pixels)
7339 static INT LISTVIEW_GetStringWidthT(const LISTVIEW_INFO *infoPtr, LPCWSTR lpszText, BOOL isW)
7344 if (is_text(lpszText))
7346 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
7347 HDC hdc = GetDC(infoPtr->hwndSelf);
7348 HFONT hOldFont = SelectObject(hdc, hFont);
7351 GetTextExtentPointW(hdc, lpszText, lstrlenW(lpszText), &stringSize);
7353 GetTextExtentPointA(hdc, (LPCSTR)lpszText, lstrlenA((LPCSTR)lpszText), &stringSize);
7354 SelectObject(hdc, hOldFont);
7355 ReleaseDC(infoPtr->hwndSelf, hdc);
7357 return stringSize.cx;
7362 * Determines which listview item is located at the specified position.
7365 * [I] infoPtr : valid pointer to the listview structure
7366 * [IO] lpht : hit test information
7367 * [I] subitem : fill out iSubItem.
7368 * [I] select : return the index only if the hit selects the item
7371 * (mm 20001022): We must not allow iSubItem to be touched, for
7372 * an app might pass only a structure with space up to iItem!
7373 * (MS Office 97 does that for instance in the file open dialog)
7376 * SUCCESS : item index
7379 static INT LISTVIEW_HitTest(const LISTVIEW_INFO *infoPtr, LPLVHITTESTINFO lpht, BOOL subitem, BOOL select)
7381 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
7382 RECT rcBox, rcBounds, rcState, rcIcon, rcLabel, rcSearch;
7383 POINT Origin, Position, opt;
7388 TRACE("(pt=%s, subitem=%d, select=%d)\n", wine_dbgstr_point(&lpht->pt), subitem, select);
7392 if (subitem) lpht->iSubItem = 0;
7394 LISTVIEW_GetOrigin(infoPtr, &Origin);
7396 /* set whole list relation flags */
7397 if (subitem && infoPtr->uView == LV_VIEW_DETAILS)
7399 /* LVM_SUBITEMHITTEST checks left bound of possible client area */
7400 if (infoPtr->rcList.left > lpht->pt.x && Origin.x < lpht->pt.x)
7401 lpht->flags |= LVHT_TOLEFT;
7403 if (lpht->pt.y < infoPtr->rcList.top && lpht->pt.y >= 0)
7404 opt.y = lpht->pt.y + infoPtr->rcList.top;
7408 if (infoPtr->rcList.bottom < opt.y)
7409 lpht->flags |= LVHT_BELOW;
7413 if (infoPtr->rcList.left > lpht->pt.x)
7414 lpht->flags |= LVHT_TOLEFT;
7415 else if (infoPtr->rcList.right < lpht->pt.x)
7416 lpht->flags |= LVHT_TORIGHT;
7418 if (infoPtr->rcList.top > lpht->pt.y)
7419 lpht->flags |= LVHT_ABOVE;
7420 else if (infoPtr->rcList.bottom < lpht->pt.y)
7421 lpht->flags |= LVHT_BELOW;
7424 /* even if item is invalid try to find subitem */
7425 if (infoPtr->uView == LV_VIEW_DETAILS && subitem)
7430 opt.x = lpht->pt.x - Origin.x;
7432 lpht->iSubItem = -1;
7433 for (j = 0; j < DPA_GetPtrCount(infoPtr->hdpaColumns); j++)
7435 pRect = &LISTVIEW_GetColumnInfo(infoPtr, j)->rcHeader;
7437 if ((opt.x >= pRect->left) && (opt.x < pRect->right))
7443 TRACE("lpht->iSubItem=%d\n", lpht->iSubItem);
7445 /* if we're outside horizontal columns bounds there's nothing to test further */
7446 if (lpht->iSubItem == -1)
7449 lpht->flags = LVHT_NOWHERE;
7454 TRACE("lpht->flags=0x%x\n", lpht->flags);
7455 if (lpht->flags) return -1;
7457 lpht->flags |= LVHT_NOWHERE;
7459 /* first deal with the large items */
7460 rcSearch.left = lpht->pt.x;
7461 rcSearch.top = lpht->pt.y;
7462 rcSearch.right = rcSearch.left + 1;
7463 rcSearch.bottom = rcSearch.top + 1;
7465 iterator_frameditems(&i, infoPtr, &rcSearch);
7466 iterator_next(&i); /* go to first item in the sequence */
7468 iterator_destroy(&i);
7470 TRACE("lpht->iItem=%d\n", iItem);
7471 if (iItem == -1) return -1;
7473 lvItem.mask = LVIF_STATE | LVIF_TEXT;
7474 if (infoPtr->uView == LV_VIEW_DETAILS) lvItem.mask |= LVIF_INDENT;
7475 lvItem.stateMask = LVIS_STATEIMAGEMASK;
7476 if (infoPtr->uView == LV_VIEW_ICON) lvItem.stateMask |= LVIS_FOCUSED;
7477 lvItem.iItem = iItem;
7478 lvItem.iSubItem = subitem ? lpht->iSubItem : 0;
7479 lvItem.pszText = szDispText;
7480 lvItem.cchTextMax = DISP_TEXT_SIZE;
7481 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return -1;
7482 if (!infoPtr->bFocus) lvItem.state &= ~LVIS_FOCUSED;
7484 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, &rcBox, NULL, &rcIcon, &rcState, &rcLabel);
7485 LISTVIEW_GetItemOrigin(infoPtr, iItem, &Position);
7486 opt.x = lpht->pt.x - Position.x - Origin.x;
7488 if (lpht->pt.y < infoPtr->rcList.top && lpht->pt.y >= 0)
7489 opt.y = lpht->pt.y - Position.y - Origin.y + infoPtr->rcList.top;
7491 opt.y = lpht->pt.y - Position.y - Origin.y;
7493 if (infoPtr->uView == LV_VIEW_DETAILS)
7496 if (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT)
7497 opt.x = lpht->pt.x - Origin.x;
7501 UnionRect(&rcBounds, &rcIcon, &rcLabel);
7502 UnionRect(&rcBounds, &rcBounds, &rcState);
7504 TRACE("rcBounds=%s\n", wine_dbgstr_rect(&rcBounds));
7505 if (!PtInRect(&rcBounds, opt)) return -1;
7507 if (PtInRect(&rcIcon, opt))
7508 lpht->flags |= LVHT_ONITEMICON;
7509 else if (PtInRect(&rcLabel, opt))
7510 lpht->flags |= LVHT_ONITEMLABEL;
7511 else if (infoPtr->himlState && PtInRect(&rcState, opt))
7512 lpht->flags |= LVHT_ONITEMSTATEICON;
7513 /* special case for LVS_EX_FULLROWSELECT */
7514 if (infoPtr->uView == LV_VIEW_DETAILS && infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT &&
7515 !(lpht->flags & LVHT_ONITEM))
7517 lpht->flags = LVHT_ONITEM | LVHT_ABOVE;
7519 if (lpht->flags & LVHT_ONITEM)
7520 lpht->flags &= ~LVHT_NOWHERE;
7521 TRACE("lpht->flags=0x%x\n", lpht->flags);
7523 if (select && !(infoPtr->uView == LV_VIEW_DETAILS &&
7524 ((infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) ||
7525 (infoPtr->dwStyle & LVS_OWNERDRAWFIXED))))
7527 if (infoPtr->uView == LV_VIEW_DETAILS)
7529 /* get main item bounds */
7530 lvItem.iSubItem = 0;
7531 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, &rcBox, NULL, &rcIcon, &rcState, &rcLabel);
7532 UnionRect(&rcBounds, &rcIcon, &rcLabel);
7533 UnionRect(&rcBounds, &rcBounds, &rcState);
7535 if (!PtInRect(&rcBounds, opt)) iItem = -1;
7537 return lpht->iItem = iItem;
7542 * Inserts a new item in the listview control.
7545 * [I] infoPtr : valid pointer to the listview structure
7546 * [I] lpLVItem : item information
7547 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
7550 * SUCCESS : new item index
7553 static INT LISTVIEW_InsertItemT(LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem, BOOL isW)
7560 BOOL is_sorted, has_changed;
7562 HWND hwndSelf = infoPtr->hwndSelf;
7564 TRACE("(lpLVItem=%s, isW=%d)\n", debuglvitem_t(lpLVItem, isW), isW);
7566 if (infoPtr->dwStyle & LVS_OWNERDATA) return infoPtr->nItemCount++;
7568 /* make sure it's an item, and not a subitem; cannot insert a subitem */
7569 if (!lpLVItem || lpLVItem->iSubItem) return -1;
7571 if (!is_assignable_item(lpLVItem, infoPtr->dwStyle)) return -1;
7573 if (!(lpItem = Alloc(sizeof(ITEM_INFO)))) return -1;
7575 /* insert item in listview control data structure */
7576 if ( !(hdpaSubItems = DPA_Create(8)) ) goto fail;
7577 if ( !DPA_SetPtr(hdpaSubItems, 0, lpItem) ) assert (FALSE);
7579 /* link with id struct */
7580 if (!(lpID = Alloc(sizeof(ITEM_ID)))) goto fail;
7582 lpID->item = hdpaSubItems;
7583 lpID->id = get_next_itemid(infoPtr);
7584 if ( DPA_InsertPtr(infoPtr->hdpaItemIds, infoPtr->nItemCount, lpID) == -1) goto fail;
7586 is_sorted = (infoPtr->dwStyle & (LVS_SORTASCENDING | LVS_SORTDESCENDING)) &&
7587 !(infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (LPSTR_TEXTCALLBACKW != lpLVItem->pszText);
7589 if (lpLVItem->iItem < 0 && !is_sorted) return -1;
7591 /* calculate new item index */
7598 while (i < infoPtr->nItemCount)
7600 hItem = DPA_GetPtr( infoPtr->hdpaItems, i);
7601 item_s = DPA_GetPtr(hItem, 0);
7603 cmpv = textcmpWT(item_s->hdr.pszText, lpLVItem->pszText, isW);
7604 if (infoPtr->dwStyle & LVS_SORTDESCENDING) cmpv *= -1;
7606 if (cmpv >= 0) break;
7612 nItem = min(lpLVItem->iItem, infoPtr->nItemCount);
7614 TRACE(" inserting at %d, sorted=%d, count=%d, iItem=%d\n", nItem, is_sorted, infoPtr->nItemCount, lpLVItem->iItem);
7615 nItem = DPA_InsertPtr( infoPtr->hdpaItems, nItem, hdpaSubItems );
7616 if (nItem == -1) goto fail;
7617 infoPtr->nItemCount++;
7619 /* shift indices first so they don't get tangled */
7620 LISTVIEW_ShiftIndices(infoPtr, nItem, 1);
7622 /* set the item attributes */
7623 if (lpLVItem->mask & (LVIF_GROUPID|LVIF_COLUMNS))
7625 /* full size structure expected - _WIN32IE >= 0x560 */
7628 else if (lpLVItem->mask & LVIF_INDENT)
7630 /* indent member expected - _WIN32IE >= 0x300 */
7631 memcpy(&item, lpLVItem, offsetof( LVITEMW, iGroupId ));
7635 /* minimal structure expected */
7636 memcpy(&item, lpLVItem, offsetof( LVITEMW, iIndent ));
7639 if (infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES)
7641 item.mask |= LVIF_STATE;
7642 item.stateMask |= LVIS_STATEIMAGEMASK;
7643 item.state &= ~LVIS_STATEIMAGEMASK;
7644 item.state |= INDEXTOSTATEIMAGEMASK(1);
7646 if (!set_main_item(infoPtr, &item, TRUE, isW, &has_changed)) goto undo;
7648 /* make room for the position, if we are in the right mode */
7649 if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
7651 if (DPA_InsertPtr(infoPtr->hdpaPosX, nItem, 0) == -1)
7653 if (DPA_InsertPtr(infoPtr->hdpaPosY, nItem, 0) == -1)
7655 DPA_DeletePtr(infoPtr->hdpaPosX, nItem);
7660 /* send LVN_INSERTITEM notification */
7661 ZeroMemory(&nmlv, sizeof(NMLISTVIEW));
7663 nmlv.lParam = lpItem->lParam;
7664 notify_listview(infoPtr, LVN_INSERTITEM, &nmlv);
7665 if (!IsWindow(hwndSelf))
7668 /* align items (set position of each item) */
7669 if (infoPtr->uView == LV_VIEW_SMALLICON || infoPtr->uView == LV_VIEW_ICON)
7673 if (infoPtr->dwStyle & LVS_ALIGNLEFT)
7674 LISTVIEW_NextIconPosLeft(infoPtr, &pt);
7676 LISTVIEW_NextIconPosTop(infoPtr, &pt);
7678 LISTVIEW_MoveIconTo(infoPtr, nItem, &pt, TRUE);
7681 /* now is the invalidation fun */
7682 LISTVIEW_ScrollOnInsert(infoPtr, nItem, 1);
7686 LISTVIEW_ShiftIndices(infoPtr, nItem, -1);
7687 DPA_DeletePtr(infoPtr->hdpaItems, nItem);
7688 infoPtr->nItemCount--;
7690 DPA_DeletePtr(hdpaSubItems, 0);
7691 DPA_Destroy (hdpaSubItems);
7698 * Checks item visibility.
7701 * [I] infoPtr : valid pointer to the listview structure
7702 * [I] nFirst : item index to check for
7705 * Item visible : TRUE
7706 * Item invisible or failure : FALSE
7708 static BOOL LISTVIEW_IsItemVisible(const LISTVIEW_INFO *infoPtr, INT nItem)
7710 POINT Origin, Position;
7715 TRACE("nItem=%d\n", nItem);
7717 if (nItem < 0 || nItem >= DPA_GetPtrCount(infoPtr->hdpaItems)) return FALSE;
7719 LISTVIEW_GetOrigin(infoPtr, &Origin);
7720 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
7721 rcItem.left = Position.x + Origin.x;
7722 rcItem.top = Position.y + Origin.y;
7723 rcItem.right = rcItem.left + infoPtr->nItemWidth;
7724 rcItem.bottom = rcItem.top + infoPtr->nItemHeight;
7726 hdc = GetDC(infoPtr->hwndSelf);
7727 if (!hdc) return FALSE;
7728 ret = RectVisible(hdc, &rcItem);
7729 ReleaseDC(infoPtr->hwndSelf, hdc);
7736 * Redraws a range of items.
7739 * [I] infoPtr : valid pointer to the listview structure
7740 * [I] nFirst : first item
7741 * [I] nLast : last item
7747 static BOOL LISTVIEW_RedrawItems(const LISTVIEW_INFO *infoPtr, INT nFirst, INT nLast)
7751 if (nLast < nFirst || min(nFirst, nLast) < 0 ||
7752 max(nFirst, nLast) >= infoPtr->nItemCount)
7755 for (i = nFirst; i <= nLast; i++)
7756 LISTVIEW_InvalidateItem(infoPtr, i);
7763 * Scroll the content of a listview.
7766 * [I] infoPtr : valid pointer to the listview structure
7767 * [I] dx : horizontal scroll amount in pixels
7768 * [I] dy : vertical scroll amount in pixels
7775 * If the control is in report view (LV_VIEW_DETAILS) the control can
7776 * be scrolled only in line increments. "dy" will be rounded to the
7777 * nearest number of pixels that are a whole line. Ex: if line height
7778 * is 16 and an 8 is passed, the list will be scrolled by 16. If a 7
7779 * is passed, then the scroll will be 0. (per MSDN 7/2002)
7781 * For: (per experimentation with native control and CSpy ListView)
7782 * LV_VIEW_ICON dy=1 = 1 pixel (vertical only)
7784 * LV_VIEW_SMALLICON dy=1 = 1 pixel (vertical only)
7786 * LV_VIEW_LIST dx=1 = 1 column (horizontal only)
7787 * but will only scroll 1 column per message
7788 * no matter what the value.
7789 * dy must be 0 or FALSE returned.
7790 * LV_VIEW_DETAILS dx=1 = 1 pixel
7794 static BOOL LISTVIEW_Scroll(LISTVIEW_INFO *infoPtr, INT dx, INT dy)
7796 switch(infoPtr->uView) {
7797 case LV_VIEW_DETAILS:
7798 dy += (dy < 0 ? -1 : 1) * infoPtr->nItemHeight/2;
7799 dy /= infoPtr->nItemHeight;
7802 if (dy != 0) return FALSE;
7809 if (dx != 0) LISTVIEW_HScroll(infoPtr, SB_INTERNAL, dx, 0);
7810 if (dy != 0) LISTVIEW_VScroll(infoPtr, SB_INTERNAL, dy);
7817 * Sets the background color.
7820 * [I] infoPtr : valid pointer to the listview structure
7821 * [I] clrBk : background color
7827 static BOOL LISTVIEW_SetBkColor(LISTVIEW_INFO *infoPtr, COLORREF clrBk)
7829 TRACE("(clrBk=%x)\n", clrBk);
7831 if(infoPtr->clrBk != clrBk) {
7832 if (infoPtr->clrBk != CLR_NONE) DeleteObject(infoPtr->hBkBrush);
7833 infoPtr->clrBk = clrBk;
7834 if (clrBk == CLR_NONE)
7835 infoPtr->hBkBrush = (HBRUSH)GetClassLongPtrW(infoPtr->hwndSelf, GCLP_HBRBACKGROUND);
7838 infoPtr->hBkBrush = CreateSolidBrush(clrBk);
7839 infoPtr->dwLvExStyle &= ~LVS_EX_TRANSPARENTBKGND;
7841 LISTVIEW_InvalidateList(infoPtr);
7847 /* LISTVIEW_SetBkImage */
7849 /*** Helper for {Insert,Set}ColumnT *only* */
7850 static void column_fill_hditem(const LISTVIEW_INFO *infoPtr, HDITEMW *lphdi, INT nColumn,
7851 const LVCOLUMNW *lpColumn, BOOL isW)
7853 if (lpColumn->mask & LVCF_FMT)
7855 /* format member is valid */
7856 lphdi->mask |= HDI_FORMAT;
7858 /* set text alignment (leftmost column must be left-aligned) */
7859 if (nColumn == 0 || (lpColumn->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_LEFT)
7860 lphdi->fmt |= HDF_LEFT;
7861 else if ((lpColumn->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_RIGHT)
7862 lphdi->fmt |= HDF_RIGHT;
7863 else if ((lpColumn->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_CENTER)
7864 lphdi->fmt |= HDF_CENTER;
7866 if (lpColumn->fmt & LVCFMT_BITMAP_ON_RIGHT)
7867 lphdi->fmt |= HDF_BITMAP_ON_RIGHT;
7869 if (lpColumn->fmt & LVCFMT_COL_HAS_IMAGES)
7871 lphdi->fmt |= HDF_IMAGE;
7872 lphdi->iImage = I_IMAGECALLBACK;
7875 if (lpColumn->fmt & LVCFMT_FIXED_WIDTH)
7876 lphdi->fmt |= HDF_FIXEDWIDTH;
7879 if (lpColumn->mask & LVCF_WIDTH)
7881 lphdi->mask |= HDI_WIDTH;
7882 if(lpColumn->cx == LVSCW_AUTOSIZE_USEHEADER)
7884 /* make it fill the remainder of the controls width */
7888 for(item_index = 0; item_index < (nColumn - 1); item_index++)
7890 LISTVIEW_GetHeaderRect(infoPtr, item_index, &rcHeader);
7891 lphdi->cxy += rcHeader.right - rcHeader.left;
7894 /* retrieve the layout of the header */
7895 GetClientRect(infoPtr->hwndSelf, &rcHeader);
7896 TRACE("start cxy=%d rcHeader=%s\n", lphdi->cxy, wine_dbgstr_rect(&rcHeader));
7898 lphdi->cxy = (rcHeader.right - rcHeader.left) - lphdi->cxy;
7901 lphdi->cxy = lpColumn->cx;
7904 if (lpColumn->mask & LVCF_TEXT)
7906 lphdi->mask |= HDI_TEXT | HDI_FORMAT;
7907 lphdi->fmt |= HDF_STRING;
7908 lphdi->pszText = lpColumn->pszText;
7909 lphdi->cchTextMax = textlenT(lpColumn->pszText, isW);
7912 if (lpColumn->mask & LVCF_IMAGE)
7914 lphdi->mask |= HDI_IMAGE;
7915 lphdi->iImage = lpColumn->iImage;
7918 if (lpColumn->mask & LVCF_ORDER)
7920 lphdi->mask |= HDI_ORDER;
7921 lphdi->iOrder = lpColumn->iOrder;
7928 * Inserts a new column.
7931 * [I] infoPtr : valid pointer to the listview structure
7932 * [I] nColumn : column index
7933 * [I] lpColumn : column information
7934 * [I] isW : TRUE if lpColumn is Unicode, FALSE otherwise
7937 * SUCCESS : new column index
7940 static INT LISTVIEW_InsertColumnT(LISTVIEW_INFO *infoPtr, INT nColumn,
7941 const LVCOLUMNW *lpColumn, BOOL isW)
7943 COLUMN_INFO *lpColumnInfo;
7947 TRACE("(nColumn=%d, lpColumn=%s, isW=%d)\n", nColumn, debuglvcolumn_t(lpColumn, isW), isW);
7949 if (!lpColumn || nColumn < 0) return -1;
7950 nColumn = min(nColumn, DPA_GetPtrCount(infoPtr->hdpaColumns));
7952 ZeroMemory(&hdi, sizeof(HDITEMW));
7953 column_fill_hditem(infoPtr, &hdi, nColumn, lpColumn, isW);
7956 * A mask not including LVCF_WIDTH turns into a mask of width, width 10
7957 * (can be seen in SPY) otherwise column never gets added.
7959 if (!(lpColumn->mask & LVCF_WIDTH)) {
7960 hdi.mask |= HDI_WIDTH;
7965 * when the iSubItem is available Windows copies it to the header lParam. It seems
7966 * to happen only in LVM_INSERTCOLUMN - not in LVM_SETCOLUMN
7968 if (lpColumn->mask & LVCF_SUBITEM)
7970 hdi.mask |= HDI_LPARAM;
7971 hdi.lParam = lpColumn->iSubItem;
7974 /* create header if not present */
7975 LISTVIEW_CreateHeader(infoPtr);
7976 if (!(LVS_NOCOLUMNHEADER & infoPtr->dwStyle) &&
7977 (infoPtr->uView == LV_VIEW_DETAILS) && (WS_VISIBLE & infoPtr->dwStyle))
7979 ShowWindow(infoPtr->hwndHeader, SW_SHOWNORMAL);
7982 /* insert item in header control */
7983 nNewColumn = SendMessageW(infoPtr->hwndHeader,
7984 isW ? HDM_INSERTITEMW : HDM_INSERTITEMA,
7985 nColumn, (LPARAM)&hdi);
7986 if (nNewColumn == -1) return -1;
7987 if (nNewColumn != nColumn) ERR("nColumn=%d, nNewColumn=%d\n", nColumn, nNewColumn);
7989 /* create our own column info */
7990 if (!(lpColumnInfo = Alloc(sizeof(COLUMN_INFO)))) goto fail;
7991 if (DPA_InsertPtr(infoPtr->hdpaColumns, nNewColumn, lpColumnInfo) == -1) goto fail;
7993 if (lpColumn->mask & LVCF_FMT) lpColumnInfo->fmt = lpColumn->fmt;
7994 if (lpColumn->mask & LVCF_MINWIDTH) lpColumnInfo->cxMin = lpColumn->cxMin;
7995 if (!SendMessageW(infoPtr->hwndHeader, HDM_GETITEMRECT, nNewColumn, (LPARAM)&lpColumnInfo->rcHeader))
7998 /* now we have to actually adjust the data */
7999 if (!(infoPtr->dwStyle & LVS_OWNERDATA) && infoPtr->nItemCount > 0)
8001 SUBITEM_INFO *lpSubItem;
8007 item.iSubItem = nNewColumn;
8008 item.mask = LVIF_TEXT | LVIF_IMAGE;
8009 item.iImage = I_IMAGECALLBACK;
8010 item.pszText = LPSTR_TEXTCALLBACKW;
8012 for (nItem = 0; nItem < infoPtr->nItemCount; nItem++)
8014 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, nItem);
8015 for (i = 1; i < DPA_GetPtrCount(hdpaSubItems); i++)
8017 lpSubItem = DPA_GetPtr(hdpaSubItems, i);
8018 if (lpSubItem->iSubItem >= nNewColumn)
8019 lpSubItem->iSubItem++;
8022 /* add new subitem for each item */
8024 set_sub_item(infoPtr, &item, isW, &changed);
8028 /* make space for the new column */
8029 LISTVIEW_ScrollColumns(infoPtr, nNewColumn + 1, lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left);
8030 LISTVIEW_UpdateItemSize(infoPtr);
8035 if (nNewColumn != -1) SendMessageW(infoPtr->hwndHeader, HDM_DELETEITEM, nNewColumn, 0);
8038 DPA_DeletePtr(infoPtr->hdpaColumns, nNewColumn);
8046 * Sets the attributes of a header item.
8049 * [I] infoPtr : valid pointer to the listview structure
8050 * [I] nColumn : column index
8051 * [I] lpColumn : column attributes
8052 * [I] isW: if TRUE, then lpColumn is a LPLVCOLUMNW, else it is a LPLVCOLUMNA
8058 static BOOL LISTVIEW_SetColumnT(const LISTVIEW_INFO *infoPtr, INT nColumn,
8059 const LVCOLUMNW *lpColumn, BOOL isW)
8061 HDITEMW hdi, hdiget;
8064 TRACE("(nColumn=%d, lpColumn=%s, isW=%d)\n", nColumn, debuglvcolumn_t(lpColumn, isW), isW);
8066 if (!lpColumn || nColumn < 0 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
8068 ZeroMemory(&hdi, sizeof(HDITEMW));
8069 if (lpColumn->mask & LVCF_FMT)
8071 hdi.mask |= HDI_FORMAT;
8072 hdiget.mask = HDI_FORMAT;
8073 if (SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nColumn, (LPARAM)&hdiget))
8074 hdi.fmt = hdiget.fmt & HDF_STRING;
8076 column_fill_hditem(infoPtr, &hdi, nColumn, lpColumn, isW);
8078 /* set header item attributes */
8079 bResult = SendMessageW(infoPtr->hwndHeader, isW ? HDM_SETITEMW : HDM_SETITEMA, nColumn, (LPARAM)&hdi);
8080 if (!bResult) return FALSE;
8082 if (lpColumn->mask & LVCF_FMT)
8084 COLUMN_INFO *lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, nColumn);
8085 INT oldFmt = lpColumnInfo->fmt;
8087 lpColumnInfo->fmt = lpColumn->fmt;
8088 if ((oldFmt ^ lpColumn->fmt) & (LVCFMT_JUSTIFYMASK | LVCFMT_IMAGE))
8090 if (infoPtr->uView == LV_VIEW_DETAILS) LISTVIEW_InvalidateColumn(infoPtr, nColumn);
8094 if (lpColumn->mask & LVCF_MINWIDTH)
8095 LISTVIEW_GetColumnInfo(infoPtr, nColumn)->cxMin = lpColumn->cxMin;
8102 * Sets the column order array
8105 * [I] infoPtr : valid pointer to the listview structure
8106 * [I] iCount : number of elements in column order array
8107 * [I] lpiArray : pointer to column order array
8113 static BOOL LISTVIEW_SetColumnOrderArray(LISTVIEW_INFO *infoPtr, INT iCount, const INT *lpiArray)
8115 TRACE("iCount %d lpiArray %p\n", iCount, lpiArray);
8117 if (!lpiArray || !IsWindow(infoPtr->hwndHeader)) return FALSE;
8119 infoPtr->colRectsDirty = TRUE;
8121 return SendMessageW(infoPtr->hwndHeader, HDM_SETORDERARRAY, iCount, (LPARAM)lpiArray);
8126 * Sets the width of a column
8129 * [I] infoPtr : valid pointer to the listview structure
8130 * [I] nColumn : column index
8131 * [I] cx : column width
8137 static BOOL LISTVIEW_SetColumnWidth(LISTVIEW_INFO *infoPtr, INT nColumn, INT cx)
8139 WCHAR szDispText[DISP_TEXT_SIZE] = { 0 };
8143 TRACE("(nColumn=%d, cx=%d\n", nColumn, cx);
8145 /* set column width only if in report or list mode */
8146 if (infoPtr->uView != LV_VIEW_DETAILS && infoPtr->uView != LV_VIEW_LIST) return FALSE;
8148 /* take care of invalid cx values */
8149 if(infoPtr->uView == LV_VIEW_DETAILS && cx < -2) cx = LVSCW_AUTOSIZE;
8150 else if (infoPtr->uView == LV_VIEW_LIST && cx < 1) return FALSE;
8152 /* resize all columns if in LV_VIEW_LIST mode */
8153 if(infoPtr->uView == LV_VIEW_LIST)
8155 infoPtr->nItemWidth = cx;
8156 LISTVIEW_InvalidateList(infoPtr);
8160 if (nColumn < 0 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
8162 if (cx == LVSCW_AUTOSIZE || (cx == LVSCW_AUTOSIZE_USEHEADER && nColumn < DPA_GetPtrCount(infoPtr->hdpaColumns) -1))
8167 lvItem.mask = LVIF_TEXT;
8169 lvItem.iSubItem = nColumn;
8170 lvItem.pszText = szDispText;
8171 lvItem.cchTextMax = DISP_TEXT_SIZE;
8172 for (; lvItem.iItem < infoPtr->nItemCount; lvItem.iItem++)
8174 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) continue;
8175 nLabelWidth = LISTVIEW_GetStringWidthT(infoPtr, lvItem.pszText, TRUE);
8176 if (max_cx < nLabelWidth) max_cx = nLabelWidth;
8178 if (infoPtr->himlSmall && (nColumn == 0 || (LISTVIEW_GetColumnInfo(infoPtr, nColumn)->fmt & LVCFMT_IMAGE)))
8179 max_cx += infoPtr->iconSize.cx;
8180 max_cx += TRAILING_LABEL_PADDING;
8183 /* autosize based on listview items width */
8184 if(cx == LVSCW_AUTOSIZE)
8186 else if(cx == LVSCW_AUTOSIZE_USEHEADER)
8188 /* if iCol is the last column make it fill the remainder of the controls width */
8189 if(nColumn == DPA_GetPtrCount(infoPtr->hdpaColumns) - 1)
8194 LISTVIEW_GetOrigin(infoPtr, &Origin);
8195 LISTVIEW_GetHeaderRect(infoPtr, nColumn, &rcHeader);
8197 cx = infoPtr->rcList.right - Origin.x - rcHeader.left;
8201 /* Despite what the MS docs say, if this is not the last
8202 column, then MS resizes the column to the width of the
8203 largest text string in the column, including headers
8204 and items. This is different from LVSCW_AUTOSIZE in that
8205 LVSCW_AUTOSIZE ignores the header string length. */
8208 /* retrieve header text */
8209 hdi.mask = HDI_TEXT;
8210 hdi.cchTextMax = DISP_TEXT_SIZE;
8211 hdi.pszText = szDispText;
8212 if (SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nColumn, (LPARAM)&hdi))
8214 HDC hdc = GetDC(infoPtr->hwndSelf);
8215 HFONT old_font = SelectObject(hdc, (HFONT)SendMessageW(infoPtr->hwndHeader, WM_GETFONT, 0, 0));
8218 if (GetTextExtentPoint32W(hdc, hdi.pszText, lstrlenW(hdi.pszText), &size))
8219 cx = size.cx + TRAILING_HEADER_PADDING;
8220 /* FIXME: Take into account the header image, if one is present */
8221 SelectObject(hdc, old_font);
8222 ReleaseDC(infoPtr->hwndSelf, hdc);
8224 cx = max (cx, max_cx);
8228 if (cx < 0) return FALSE;
8230 /* call header to update the column change */
8231 hdi.mask = HDI_WIDTH;
8232 hdi.cxy = max(cx, LISTVIEW_GetColumnInfo(infoPtr, nColumn)->cxMin);
8233 TRACE("hdi.cxy=%d\n", hdi.cxy);
8234 return SendMessageW(infoPtr->hwndHeader, HDM_SETITEMW, nColumn, (LPARAM)&hdi);
8238 * Creates the checkbox imagelist. Helper for LISTVIEW_SetExtendedListViewStyle
8241 static HIMAGELIST LISTVIEW_CreateCheckBoxIL(const LISTVIEW_INFO *infoPtr)
8244 HBITMAP hbm_im, hbm_mask, hbm_orig;
8246 HBRUSH hbr_white = GetStockObject(WHITE_BRUSH);
8247 HBRUSH hbr_black = GetStockObject(BLACK_BRUSH);
8250 himl = ImageList_Create(GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON),
8251 ILC_COLOR | ILC_MASK, 2, 2);
8252 hdc_wnd = GetDC(infoPtr->hwndSelf);
8253 hdc = CreateCompatibleDC(hdc_wnd);
8254 hbm_im = CreateCompatibleBitmap(hdc_wnd, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON));
8255 hbm_mask = CreateBitmap(GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), 1, 1, NULL);
8256 ReleaseDC(infoPtr->hwndSelf, hdc_wnd);
8258 rc.left = rc.top = 0;
8259 rc.right = GetSystemMetrics(SM_CXSMICON);
8260 rc.bottom = GetSystemMetrics(SM_CYSMICON);
8262 hbm_orig = SelectObject(hdc, hbm_mask);
8263 FillRect(hdc, &rc, hbr_white);
8264 InflateRect(&rc, -2, -2);
8265 FillRect(hdc, &rc, hbr_black);
8267 SelectObject(hdc, hbm_im);
8268 DrawFrameControl(hdc, &rc, DFC_BUTTON, DFCS_BUTTONCHECK | DFCS_MONO);
8269 SelectObject(hdc, hbm_orig);
8270 ImageList_Add(himl, hbm_im, hbm_mask);
8272 SelectObject(hdc, hbm_im);
8273 DrawFrameControl(hdc, &rc, DFC_BUTTON, DFCS_BUTTONCHECK | DFCS_MONO | DFCS_CHECKED);
8274 SelectObject(hdc, hbm_orig);
8275 ImageList_Add(himl, hbm_im, hbm_mask);
8277 DeleteObject(hbm_mask);
8278 DeleteObject(hbm_im);
8286 * Sets the extended listview style.
8289 * [I] infoPtr : valid pointer to the listview structure
8291 * [I] dwStyle : style
8294 * SUCCESS : previous style
8297 static DWORD LISTVIEW_SetExtendedListViewStyle(LISTVIEW_INFO *infoPtr, DWORD dwMask, DWORD dwExStyle)
8299 DWORD dwOldExStyle = infoPtr->dwLvExStyle;
8303 infoPtr->dwLvExStyle = (dwOldExStyle & ~dwMask) | (dwExStyle & dwMask);
8305 infoPtr->dwLvExStyle = dwExStyle;
8307 if((infoPtr->dwLvExStyle ^ dwOldExStyle) & LVS_EX_CHECKBOXES)
8309 HIMAGELIST himl = 0;
8310 if(infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES)
8313 item.mask = LVIF_STATE;
8314 item.stateMask = LVIS_STATEIMAGEMASK;
8315 item.state = INDEXTOSTATEIMAGEMASK(1);
8316 LISTVIEW_SetItemState(infoPtr, -1, &item);
8318 himl = LISTVIEW_CreateCheckBoxIL(infoPtr);
8319 if(!(infoPtr->dwStyle & LVS_SHAREIMAGELISTS))
8320 ImageList_Destroy(infoPtr->himlState);
8322 himl = LISTVIEW_SetImageList(infoPtr, LVSIL_STATE, himl);
8323 /* checkbox list replaces prevous custom list or... */
8324 if(((infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES) &&
8325 !(infoPtr->dwStyle & LVS_SHAREIMAGELISTS)) ||
8326 /* ...previous was checkbox list */
8327 (dwOldExStyle & LVS_EX_CHECKBOXES))
8328 ImageList_Destroy(himl);
8331 if((infoPtr->dwLvExStyle ^ dwOldExStyle) & LVS_EX_HEADERDRAGDROP)
8335 /* if not already created */
8336 LISTVIEW_CreateHeader(infoPtr);
8338 dwStyle = GetWindowLongW(infoPtr->hwndHeader, GWL_STYLE);
8339 if (infoPtr->dwLvExStyle & LVS_EX_HEADERDRAGDROP)
8340 dwStyle |= HDS_DRAGDROP;
8342 dwStyle &= ~HDS_DRAGDROP;
8343 SetWindowLongW(infoPtr->hwndHeader, GWL_STYLE, dwStyle);
8346 /* GRIDLINES adds decoration at top so changes sizes */
8347 if((infoPtr->dwLvExStyle ^ dwOldExStyle) & LVS_EX_GRIDLINES)
8349 LISTVIEW_UpdateSize(infoPtr);
8352 if((infoPtr->dwLvExStyle ^ dwOldExStyle) & LVS_EX_TRANSPARENTBKGND)
8354 if (infoPtr->dwLvExStyle & LVS_EX_TRANSPARENTBKGND)
8355 LISTVIEW_SetBkColor(infoPtr, CLR_NONE);
8358 if((infoPtr->dwLvExStyle ^ dwOldExStyle) & LVS_EX_HEADERINALLVIEWS)
8360 if (infoPtr->dwLvExStyle & LVS_EX_HEADERINALLVIEWS)
8361 LISTVIEW_CreateHeader(infoPtr);
8363 ShowWindow(infoPtr->hwndHeader, SW_HIDE);
8364 LISTVIEW_UpdateSize(infoPtr);
8365 LISTVIEW_UpdateScroll(infoPtr);
8368 LISTVIEW_InvalidateList(infoPtr);
8369 return dwOldExStyle;
8374 * Sets the new hot cursor used during hot tracking and hover selection.
8377 * [I] infoPtr : valid pointer to the listview structure
8378 * [I] hCursor : the new hot cursor handle
8381 * Returns the previous hot cursor
8383 static HCURSOR LISTVIEW_SetHotCursor(LISTVIEW_INFO *infoPtr, HCURSOR hCursor)
8385 HCURSOR oldCursor = infoPtr->hHotCursor;
8387 infoPtr->hHotCursor = hCursor;
8395 * Sets the hot item index.
8398 * [I] infoPtr : valid pointer to the listview structure
8399 * [I] iIndex : index
8402 * SUCCESS : previous hot item index
8403 * FAILURE : -1 (no hot item)
8405 static INT LISTVIEW_SetHotItem(LISTVIEW_INFO *infoPtr, INT iIndex)
8407 INT iOldIndex = infoPtr->nHotItem;
8409 infoPtr->nHotItem = iIndex;
8417 * Sets the amount of time the cursor must hover over an item before it is selected.
8420 * [I] infoPtr : valid pointer to the listview structure
8421 * [I] dwHoverTime : hover time, if -1 the hover time is set to the default
8424 * Returns the previous hover time
8426 static DWORD LISTVIEW_SetHoverTime(LISTVIEW_INFO *infoPtr, DWORD dwHoverTime)
8428 DWORD oldHoverTime = infoPtr->dwHoverTime;
8430 infoPtr->dwHoverTime = dwHoverTime;
8432 return oldHoverTime;
8437 * Sets spacing for icons of LVS_ICON style.
8440 * [I] infoPtr : valid pointer to the listview structure
8441 * [I] cx : horizontal spacing (-1 = system spacing, 0 = autosize)
8442 * [I] cy : vertical spacing (-1 = system spacing, 0 = autosize)
8445 * MAKELONG(oldcx, oldcy)
8447 static DWORD LISTVIEW_SetIconSpacing(LISTVIEW_INFO *infoPtr, INT cx, INT cy)
8449 DWORD oldspacing = MAKELONG(infoPtr->iconSpacing.cx, infoPtr->iconSpacing.cy);
8451 TRACE("requested=(%d,%d)\n", cx, cy);
8453 /* this is supported only for LVS_ICON style */
8454 if (infoPtr->uView != LV_VIEW_ICON) return oldspacing;
8456 /* set to defaults, if instructed to */
8457 if (cx == -1) cx = GetSystemMetrics(SM_CXICONSPACING);
8458 if (cy == -1) cy = GetSystemMetrics(SM_CYICONSPACING);
8460 /* if 0 then compute width
8461 * FIXME: Should scan each item and determine max width of
8462 * icon or label, then make that the width */
8464 cx = infoPtr->iconSpacing.cx;
8466 /* if 0 then compute height */
8468 cy = infoPtr->iconSize.cy + 2 * infoPtr->ntmHeight +
8469 ICON_BOTTOM_PADDING + ICON_TOP_PADDING + LABEL_VERT_PADDING;
8472 infoPtr->iconSpacing.cx = cx;
8473 infoPtr->iconSpacing.cy = cy;
8475 TRACE("old=(%d,%d), new=(%d,%d), iconSize=(%d,%d), ntmH=%d\n",
8476 LOWORD(oldspacing), HIWORD(oldspacing), cx, cy,
8477 infoPtr->iconSize.cx, infoPtr->iconSize.cy,
8478 infoPtr->ntmHeight);
8480 /* these depend on the iconSpacing */
8481 LISTVIEW_UpdateItemSize(infoPtr);
8486 static inline void set_icon_size(SIZE *size, HIMAGELIST himl, BOOL small)
8490 if (himl && ImageList_GetIconSize(himl, &cx, &cy))
8497 size->cx = GetSystemMetrics(small ? SM_CXSMICON : SM_CXICON);
8498 size->cy = GetSystemMetrics(small ? SM_CYSMICON : SM_CYICON);
8507 * [I] infoPtr : valid pointer to the listview structure
8508 * [I] nType : image list type
8509 * [I] himl : image list handle
8512 * SUCCESS : old image list
8515 static HIMAGELIST LISTVIEW_SetImageList(LISTVIEW_INFO *infoPtr, INT nType, HIMAGELIST himl)
8517 INT oldHeight = infoPtr->nItemHeight;
8518 HIMAGELIST himlOld = 0;
8520 TRACE("(nType=%d, himl=%p\n", nType, himl);
8525 himlOld = infoPtr->himlNormal;
8526 infoPtr->himlNormal = himl;
8527 if (infoPtr->uView == LV_VIEW_ICON) set_icon_size(&infoPtr->iconSize, himl, FALSE);
8528 LISTVIEW_SetIconSpacing(infoPtr, 0, 0);
8532 himlOld = infoPtr->himlSmall;
8533 infoPtr->himlSmall = himl;
8534 if (infoPtr->uView != LV_VIEW_ICON) set_icon_size(&infoPtr->iconSize, himl, TRUE);
8538 himlOld = infoPtr->himlState;
8539 infoPtr->himlState = himl;
8540 set_icon_size(&infoPtr->iconStateSize, himl, TRUE);
8541 ImageList_SetBkColor(infoPtr->himlState, CLR_NONE);
8545 ERR("Unknown icon type=%d\n", nType);
8549 infoPtr->nItemHeight = LISTVIEW_CalculateItemHeight(infoPtr);
8550 if (infoPtr->nItemHeight != oldHeight)
8551 LISTVIEW_UpdateScroll(infoPtr);
8558 * Preallocates memory (does *not* set the actual count of items !)
8561 * [I] infoPtr : valid pointer to the listview structure
8562 * [I] nItems : item count (projected number of items to allocate)
8563 * [I] dwFlags : update flags
8569 static BOOL LISTVIEW_SetItemCount(LISTVIEW_INFO *infoPtr, INT nItems, DWORD dwFlags)
8571 TRACE("(nItems=%d, dwFlags=%x)\n", nItems, dwFlags);
8573 if (infoPtr->dwStyle & LVS_OWNERDATA)
8575 INT nOldCount = infoPtr->nItemCount;
8577 if (nItems < nOldCount)
8579 RANGE range = { nItems, nOldCount };
8580 ranges_del(infoPtr->selectionRanges, range);
8581 if (infoPtr->nFocusedItem >= nItems)
8583 LISTVIEW_SetItemFocus(infoPtr, -1);
8584 SetRectEmpty(&infoPtr->rcFocus);
8588 infoPtr->nItemCount = nItems;
8589 LISTVIEW_UpdateScroll(infoPtr);
8591 /* the flags are valid only in ownerdata report and list modes */
8592 if (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON) dwFlags = 0;
8594 if (!(dwFlags & LVSICF_NOSCROLL) && infoPtr->nFocusedItem != -1)
8595 LISTVIEW_EnsureVisible(infoPtr, infoPtr->nFocusedItem, FALSE);
8597 if (!(dwFlags & LVSICF_NOINVALIDATEALL))
8598 LISTVIEW_InvalidateList(infoPtr);
8605 LISTVIEW_GetOrigin(infoPtr, &Origin);
8606 nFrom = min(nOldCount, nItems);
8607 nTo = max(nOldCount, nItems);
8609 if (infoPtr->uView == LV_VIEW_DETAILS)
8612 rcErase.top = nFrom * infoPtr->nItemHeight;
8613 rcErase.right = infoPtr->nItemWidth;
8614 rcErase.bottom = nTo * infoPtr->nItemHeight;
8615 OffsetRect(&rcErase, Origin.x, Origin.y);
8616 if (IntersectRect(&rcErase, &rcErase, &infoPtr->rcList))
8617 LISTVIEW_InvalidateRect(infoPtr, &rcErase);
8619 else /* LV_VIEW_LIST */
8621 INT nPerCol = LISTVIEW_GetCountPerColumn(infoPtr);
8623 rcErase.left = (nFrom / nPerCol) * infoPtr->nItemWidth;
8624 rcErase.top = (nFrom % nPerCol) * infoPtr->nItemHeight;
8625 rcErase.right = rcErase.left + infoPtr->nItemWidth;
8626 rcErase.bottom = nPerCol * infoPtr->nItemHeight;
8627 OffsetRect(&rcErase, Origin.x, Origin.y);
8628 if (IntersectRect(&rcErase, &rcErase, &infoPtr->rcList))
8629 LISTVIEW_InvalidateRect(infoPtr, &rcErase);
8631 rcErase.left = (nFrom / nPerCol + 1) * infoPtr->nItemWidth;
8633 rcErase.right = (nTo / nPerCol + 1) * infoPtr->nItemWidth;
8634 rcErase.bottom = nPerCol * infoPtr->nItemHeight;
8635 OffsetRect(&rcErase, Origin.x, Origin.y);
8636 if (IntersectRect(&rcErase, &rcErase, &infoPtr->rcList))
8637 LISTVIEW_InvalidateRect(infoPtr, &rcErase);
8643 /* According to MSDN for non-LVS_OWNERDATA this is just
8644 * a performance issue. The control allocates its internal
8645 * data structures for the number of items specified. It
8646 * cuts down on the number of memory allocations. Therefore
8647 * we will just issue a WARN here
8649 WARN("for non-ownerdata performance option not implemented.\n");
8657 * Sets the position of an item.
8660 * [I] infoPtr : valid pointer to the listview structure
8661 * [I] nItem : item index
8662 * [I] pt : coordinate
8668 static BOOL LISTVIEW_SetItemPosition(LISTVIEW_INFO *infoPtr, INT nItem, const POINT *pt)
8672 TRACE("(nItem=%d, pt=%s\n", nItem, wine_dbgstr_point(pt));
8674 if (!pt || nItem < 0 || nItem >= infoPtr->nItemCount ||
8675 !(infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON)) return FALSE;
8678 LISTVIEW_GetOrigin(infoPtr, &Origin);
8680 /* This point value seems to be an undocumented feature.
8681 * The best guess is that it means either at the origin,
8682 * or at true beginning of the list. I will assume the origin. */
8683 if ((Pt.x == -1) && (Pt.y == -1))
8686 if (infoPtr->uView == LV_VIEW_ICON)
8688 Pt.x -= (infoPtr->nItemWidth - infoPtr->iconSize.cx) / 2;
8689 Pt.y -= ICON_TOP_PADDING;
8694 infoPtr->bAutoarrange = FALSE;
8696 return LISTVIEW_MoveIconTo(infoPtr, nItem, &Pt, FALSE);
8701 * Sets the state of one or many items.
8704 * [I] infoPtr : valid pointer to the listview structure
8705 * [I] nItem : item index
8706 * [I] lpLVItem : item or subitem info
8712 static BOOL LISTVIEW_SetItemState(LISTVIEW_INFO *infoPtr, INT nItem, const LVITEMW *lpLVItem)
8714 BOOL bResult = TRUE;
8717 lvItem.iItem = nItem;
8718 lvItem.iSubItem = 0;
8719 lvItem.mask = LVIF_STATE;
8720 lvItem.state = lpLVItem->state;
8721 lvItem.stateMask = lpLVItem->stateMask;
8722 TRACE("lvItem=%s\n", debuglvitem_t(&lvItem, TRUE));
8726 /* select all isn't allowed in LVS_SINGLESEL */
8727 if ((lvItem.state & lvItem.stateMask & LVIS_SELECTED) && (infoPtr->dwStyle & LVS_SINGLESEL))
8730 /* focus all isn't allowed */
8731 if (lvItem.state & lvItem.stateMask & LVIS_FOCUSED) return FALSE;
8733 /* apply to all items */
8734 for (lvItem.iItem = 0; lvItem.iItem < infoPtr->nItemCount; lvItem.iItem++)
8735 if (!LISTVIEW_SetItemT(infoPtr, &lvItem, TRUE)) bResult = FALSE;
8738 bResult = LISTVIEW_SetItemT(infoPtr, &lvItem, TRUE);
8745 * Sets the text of an item or subitem.
8748 * [I] hwnd : window handle
8749 * [I] nItem : item index
8750 * [I] lpLVItem : item or subitem info
8751 * [I] isW : TRUE if input is Unicode
8757 static BOOL LISTVIEW_SetItemTextT(LISTVIEW_INFO *infoPtr, INT nItem, const LVITEMW *lpLVItem, BOOL isW)
8761 if (nItem < 0 && nItem >= infoPtr->nItemCount) return FALSE;
8763 lvItem.iItem = nItem;
8764 lvItem.iSubItem = lpLVItem->iSubItem;
8765 lvItem.mask = LVIF_TEXT;
8766 lvItem.pszText = lpLVItem->pszText;
8767 lvItem.cchTextMax = lpLVItem->cchTextMax;
8769 TRACE("(nItem=%d, lpLVItem=%s, isW=%d)\n", nItem, debuglvitem_t(&lvItem, isW), isW);
8771 return LISTVIEW_SetItemT(infoPtr, &lvItem, isW);
8776 * Set item index that marks the start of a multiple selection.
8779 * [I] infoPtr : valid pointer to the listview structure
8780 * [I] nIndex : index
8783 * Index number or -1 if there is no selection mark.
8785 static INT LISTVIEW_SetSelectionMark(LISTVIEW_INFO *infoPtr, INT nIndex)
8787 INT nOldIndex = infoPtr->nSelectionMark;
8789 TRACE("(nIndex=%d)\n", nIndex);
8791 infoPtr->nSelectionMark = nIndex;
8798 * Sets the text background color.
8801 * [I] infoPtr : valid pointer to the listview structure
8802 * [I] clrTextBk : text background color
8808 static BOOL LISTVIEW_SetTextBkColor(LISTVIEW_INFO *infoPtr, COLORREF clrTextBk)
8810 TRACE("(clrTextBk=%x)\n", clrTextBk);
8812 if (infoPtr->clrTextBk != clrTextBk)
8814 infoPtr->clrTextBk = clrTextBk;
8815 LISTVIEW_InvalidateList(infoPtr);
8823 * Sets the text foreground color.
8826 * [I] infoPtr : valid pointer to the listview structure
8827 * [I] clrText : text color
8833 static BOOL LISTVIEW_SetTextColor (LISTVIEW_INFO *infoPtr, COLORREF clrText)
8835 TRACE("(clrText=%x)\n", clrText);
8837 if (infoPtr->clrText != clrText)
8839 infoPtr->clrText = clrText;
8840 LISTVIEW_InvalidateList(infoPtr);
8848 * Sets new ToolTip window to ListView control.
8851 * [I] infoPtr : valid pointer to the listview structure
8852 * [I] hwndNewToolTip : handle to new ToolTip
8857 static HWND LISTVIEW_SetToolTips( LISTVIEW_INFO *infoPtr, HWND hwndNewToolTip)
8859 HWND hwndOldToolTip = infoPtr->hwndToolTip;
8860 infoPtr->hwndToolTip = hwndNewToolTip;
8861 return hwndOldToolTip;
8866 * sets the Unicode character format flag for the control
8868 * [I] infoPtr :valid pointer to the listview structure
8869 * [I] fUnicode :true to switch to UNICODE false to switch to ANSI
8872 * Old Unicode Format
8874 static BOOL LISTVIEW_SetUnicodeFormat( LISTVIEW_INFO *infoPtr, BOOL fUnicode)
8876 SHORT rc = infoPtr->notifyFormat;
8877 infoPtr->notifyFormat = (fUnicode) ? NFR_UNICODE : NFR_ANSI;
8878 return rc == NFR_UNICODE;
8883 * sets the control view mode
8885 * [I] infoPtr :valid pointer to the listview structure
8886 * [I] nView :new view mode value
8892 static INT LISTVIEW_SetView(LISTVIEW_INFO *infoPtr, DWORD nView)
8894 SIZE oldIconSize = infoPtr->iconSize;
8897 if (infoPtr->uView == nView) return 1;
8899 if ((INT)nView < 0 || nView > LV_VIEW_MAX) return -1;
8900 if (nView == LV_VIEW_TILE)
8902 FIXME("View LV_VIEW_TILE unimplemented\n");
8906 infoPtr->uView = nView;
8908 SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
8909 ShowWindow(infoPtr->hwndHeader, SW_HIDE);
8911 ShowScrollBar(infoPtr->hwndSelf, SB_BOTH, FALSE);
8912 SetRectEmpty(&infoPtr->rcFocus);
8914 himl = (nView == LV_VIEW_ICON ? infoPtr->himlNormal : infoPtr->himlSmall);
8915 set_icon_size(&infoPtr->iconSize, himl, nView != LV_VIEW_ICON);
8920 if ((infoPtr->iconSize.cx != oldIconSize.cx) || (infoPtr->iconSize.cy != oldIconSize.cy))
8922 TRACE("icon old size=(%d,%d), new size=(%d,%d)\n",
8923 oldIconSize.cx, oldIconSize.cy, infoPtr->iconSize.cx, infoPtr->iconSize.cy);
8924 LISTVIEW_SetIconSpacing(infoPtr, 0, 0);
8926 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
8928 case LV_VIEW_SMALLICON:
8929 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
8931 case LV_VIEW_DETAILS:
8936 LISTVIEW_CreateHeader( infoPtr );
8938 hl.prc = &infoPtr->rcList;
8940 SendMessageW(infoPtr->hwndHeader, HDM_LAYOUT, 0, (LPARAM)&hl);
8941 SetWindowPos(infoPtr->hwndHeader, infoPtr->hwndSelf, wp.x, wp.y, wp.cx, wp.cy,
8942 wp.flags | ((infoPtr->dwStyle & LVS_NOCOLUMNHEADER) ? SWP_HIDEWINDOW : SWP_SHOWWINDOW));
8949 LISTVIEW_UpdateItemSize(infoPtr);
8950 LISTVIEW_UpdateSize(infoPtr);
8951 LISTVIEW_UpdateScroll(infoPtr);
8952 LISTVIEW_InvalidateList(infoPtr);
8954 TRACE("nView=%d\n", nView);
8959 /* LISTVIEW_SetWorkAreas */
8963 * Callback internally used by LISTVIEW_SortItems() in response of LVM_SORTITEMS
8966 * [I] first : pointer to first ITEM_INFO to compare
8967 * [I] second : pointer to second ITEM_INFO to compare
8968 * [I] lParam : HWND of control
8971 * if first comes before second : negative
8972 * if first comes after second : positive
8973 * if first and second are equivalent : zero
8975 static INT WINAPI LISTVIEW_CallBackCompare(LPVOID first, LPVOID second, LPARAM lParam)
8977 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)lParam;
8978 ITEM_INFO* lv_first = DPA_GetPtr( first, 0 );
8979 ITEM_INFO* lv_second = DPA_GetPtr( second, 0 );
8981 /* Forward the call to the client defined callback */
8982 return (infoPtr->pfnCompare)( lv_first->lParam , lv_second->lParam, infoPtr->lParamSort );
8987 * Callback internally used by LISTVIEW_SortItems() in response of LVM_SORTITEMSEX
8990 * [I] first : pointer to first ITEM_INFO to compare
8991 * [I] second : pointer to second ITEM_INFO to compare
8992 * [I] lParam : HWND of control
8995 * if first comes before second : negative
8996 * if first comes after second : positive
8997 * if first and second are equivalent : zero
8999 static INT WINAPI LISTVIEW_CallBackCompareEx(LPVOID first, LPVOID second, LPARAM lParam)
9001 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)lParam;
9002 INT first_idx = DPA_GetPtrIndex( infoPtr->hdpaItems, first );
9003 INT second_idx = DPA_GetPtrIndex( infoPtr->hdpaItems, second );
9005 /* Forward the call to the client defined callback */
9006 return (infoPtr->pfnCompare)( first_idx, second_idx, infoPtr->lParamSort );
9011 * Sorts the listview items.
9014 * [I] infoPtr : valid pointer to the listview structure
9015 * [I] pfnCompare : application-defined value
9016 * [I] lParamSort : pointer to comparison callback
9017 * [I] IsEx : TRUE when LVM_SORTITEMSEX used
9023 static BOOL LISTVIEW_SortItems(LISTVIEW_INFO *infoPtr, PFNLVCOMPARE pfnCompare,
9024 LPARAM lParamSort, BOOL IsEx)
9028 LPVOID selectionMarkItem = NULL;
9029 LPVOID focusedItem = NULL;
9032 TRACE("(pfnCompare=%p, lParamSort=%lx)\n", pfnCompare, lParamSort);
9034 if (infoPtr->dwStyle & LVS_OWNERDATA) return FALSE;
9036 if (!pfnCompare) return FALSE;
9037 if (!infoPtr->hdpaItems) return FALSE;
9039 /* if there are 0 or 1 items, there is no need to sort */
9040 if (infoPtr->nItemCount < 2) return TRUE;
9042 /* clear selection */
9043 ranges_clear(infoPtr->selectionRanges);
9045 /* save selection mark and focused item */
9046 if (infoPtr->nSelectionMark >= 0)
9047 selectionMarkItem = DPA_GetPtr(infoPtr->hdpaItems, infoPtr->nSelectionMark);
9048 if (infoPtr->nFocusedItem >= 0)
9049 focusedItem = DPA_GetPtr(infoPtr->hdpaItems, infoPtr->nFocusedItem);
9051 infoPtr->pfnCompare = pfnCompare;
9052 infoPtr->lParamSort = lParamSort;
9054 DPA_Sort(infoPtr->hdpaItems, LISTVIEW_CallBackCompareEx, (LPARAM)infoPtr);
9056 DPA_Sort(infoPtr->hdpaItems, LISTVIEW_CallBackCompare, (LPARAM)infoPtr);
9058 /* restore selection ranges */
9059 for (i=0; i < infoPtr->nItemCount; i++)
9061 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, i);
9062 lpItem = DPA_GetPtr(hdpaSubItems, 0);
9064 if (lpItem->state & LVIS_SELECTED)
9065 ranges_additem(infoPtr->selectionRanges, i);
9067 /* restore selection mark and focused item */
9068 infoPtr->nSelectionMark = DPA_GetPtrIndex(infoPtr->hdpaItems, selectionMarkItem);
9069 infoPtr->nFocusedItem = DPA_GetPtrIndex(infoPtr->hdpaItems, focusedItem);
9071 /* I believe nHotItem should be left alone, see LISTVIEW_ShiftIndices */
9073 /* refresh the display */
9074 if (infoPtr->uView != LV_VIEW_ICON && infoPtr->uView != LV_VIEW_SMALLICON)
9075 LISTVIEW_InvalidateList(infoPtr);
9082 * Update theme handle after a theme change.
9085 * [I] infoPtr : valid pointer to the listview structure
9089 * FAILURE : something else
9091 static LRESULT LISTVIEW_ThemeChanged(const LISTVIEW_INFO *infoPtr)
9093 HTHEME theme = GetWindowTheme(infoPtr->hwndSelf);
9094 CloseThemeData(theme);
9095 OpenThemeData(infoPtr->hwndSelf, themeClass);
9101 * Updates an items or rearranges the listview control.
9104 * [I] infoPtr : valid pointer to the listview structure
9105 * [I] nItem : item index
9111 static BOOL LISTVIEW_Update(LISTVIEW_INFO *infoPtr, INT nItem)
9113 TRACE("(nItem=%d)\n", nItem);
9115 if (nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
9117 /* rearrange with default alignment style */
9118 if (is_autoarrange(infoPtr))
9119 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
9121 LISTVIEW_InvalidateItem(infoPtr, nItem);
9128 * Draw the track line at the place defined in the infoPtr structure.
9129 * The line is drawn with a XOR pen so drawing the line for the second time
9130 * in the same place erases the line.
9133 * [I] infoPtr : valid pointer to the listview structure
9139 static BOOL LISTVIEW_DrawTrackLine(const LISTVIEW_INFO *infoPtr)
9145 if (infoPtr->xTrackLine == -1)
9148 if (!(hdc = GetDC(infoPtr->hwndSelf)))
9150 hOldPen = SelectObject(hdc, GetStockObject(BLACK_PEN));
9151 oldROP = SetROP2(hdc, R2_XORPEN);
9152 MoveToEx(hdc, infoPtr->xTrackLine, infoPtr->rcList.top, NULL);
9153 LineTo(hdc, infoPtr->xTrackLine, infoPtr->rcList.bottom);
9154 SetROP2(hdc, oldROP);
9155 SelectObject(hdc, hOldPen);
9156 ReleaseDC(infoPtr->hwndSelf, hdc);
9162 * Called when an edit control should be displayed. This function is called after
9163 * we are sure that there was a single click - not a double click (this is a TIMERPROC).
9166 * [I] hwnd : Handle to the listview
9167 * [I] uMsg : WM_TIMER (ignored)
9168 * [I] idEvent : The timer ID interpreted as a pointer to a DELAYED_EDIT_ITEM struct
9169 * [I] dwTimer : The elapsed time (ignored)
9174 static VOID CALLBACK LISTVIEW_DelayedEditItem(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
9176 DELAYED_ITEM_EDIT *editItem = (DELAYED_ITEM_EDIT *)idEvent;
9177 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongPtrW(hwnd, 0);
9179 KillTimer(hwnd, idEvent);
9180 editItem->fEnabled = FALSE;
9181 /* check if the item is still selected */
9182 if (infoPtr->bFocus && LISTVIEW_GetItemState(infoPtr, editItem->iItem, LVIS_SELECTED))
9183 LISTVIEW_EditLabelT(infoPtr, editItem->iItem, TRUE);
9188 * Creates the listview control - the WM_NCCREATE phase.
9191 * [I] hwnd : window handle
9192 * [I] lpcs : the create parameters
9198 static LRESULT LISTVIEW_NCCreate(HWND hwnd, const CREATESTRUCTW *lpcs)
9200 LISTVIEW_INFO *infoPtr;
9203 TRACE("(lpcs=%p)\n", lpcs);
9205 /* initialize info pointer */
9206 infoPtr = Alloc(sizeof(LISTVIEW_INFO));
9207 if (!infoPtr) return FALSE;
9209 SetWindowLongPtrW(hwnd, 0, (DWORD_PTR)infoPtr);
9211 infoPtr->hwndSelf = hwnd;
9212 infoPtr->dwStyle = lpcs->style; /* Note: may be changed in WM_CREATE */
9213 map_style_view(infoPtr);
9214 /* determine the type of structures to use */
9215 infoPtr->hwndNotify = lpcs->hwndParent;
9216 /* infoPtr->notifyFormat will be filled in WM_CREATE */
9218 /* initialize color information */
9219 infoPtr->clrBk = CLR_NONE;
9220 infoPtr->clrText = CLR_DEFAULT;
9221 infoPtr->clrTextBk = CLR_DEFAULT;
9222 LISTVIEW_SetBkColor(infoPtr, comctl32_color.clrWindow);
9224 /* set default values */
9225 infoPtr->nFocusedItem = -1;
9226 infoPtr->nSelectionMark = -1;
9227 infoPtr->nHotItem = -1;
9228 infoPtr->bRedraw = TRUE;
9229 infoPtr->bNoItemMetrics = TRUE;
9230 infoPtr->bDoChangeNotify = TRUE;
9231 infoPtr->iconSpacing.cx = GetSystemMetrics(SM_CXICONSPACING);
9232 infoPtr->iconSpacing.cy = GetSystemMetrics(SM_CYICONSPACING);
9233 infoPtr->nEditLabelItem = -1;
9234 infoPtr->nLButtonDownItem = -1;
9235 infoPtr->dwHoverTime = HOVER_DEFAULT; /* default system hover time */
9236 infoPtr->nMeasureItemHeight = 0;
9237 infoPtr->xTrackLine = -1; /* no track line */
9238 infoPtr->itemEdit.fEnabled = FALSE;
9239 infoPtr->iVersion = COMCTL32_VERSION;
9240 infoPtr->colRectsDirty = FALSE;
9242 /* get default font (icon title) */
9243 SystemParametersInfoW(SPI_GETICONTITLELOGFONT, 0, &logFont, 0);
9244 infoPtr->hDefaultFont = CreateFontIndirectW(&logFont);
9245 infoPtr->hFont = infoPtr->hDefaultFont;
9246 LISTVIEW_SaveTextMetrics(infoPtr);
9248 /* allocate memory for the data structure */
9249 if (!(infoPtr->selectionRanges = ranges_create(10))) goto fail;
9250 if (!(infoPtr->hdpaItems = DPA_Create(10))) goto fail;
9251 if (!(infoPtr->hdpaItemIds = DPA_Create(10))) goto fail;
9252 if (!(infoPtr->hdpaPosX = DPA_Create(10))) goto fail;
9253 if (!(infoPtr->hdpaPosY = DPA_Create(10))) goto fail;
9254 if (!(infoPtr->hdpaColumns = DPA_Create(10))) goto fail;
9258 DestroyWindow(infoPtr->hwndHeader);
9259 ranges_destroy(infoPtr->selectionRanges);
9260 DPA_Destroy(infoPtr->hdpaItems);
9261 DPA_Destroy(infoPtr->hdpaItemIds);
9262 DPA_Destroy(infoPtr->hdpaPosX);
9263 DPA_Destroy(infoPtr->hdpaPosY);
9264 DPA_Destroy(infoPtr->hdpaColumns);
9271 * Creates the listview control - the WM_CREATE phase. Most of the data is
9272 * already set up in LISTVIEW_NCCreate
9275 * [I] hwnd : window handle
9276 * [I] lpcs : the create parameters
9282 static LRESULT LISTVIEW_Create(HWND hwnd, const CREATESTRUCTW *lpcs)
9284 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongPtrW(hwnd, 0);
9286 TRACE("(lpcs=%p)\n", lpcs);
9288 infoPtr->dwStyle = lpcs->style;
9289 map_style_view(infoPtr);
9291 infoPtr->notifyFormat = SendMessageW(infoPtr->hwndNotify, WM_NOTIFYFORMAT,
9292 (WPARAM)infoPtr->hwndSelf, NF_QUERY);
9293 /* on error defaulting to ANSI notifications */
9294 if (infoPtr->notifyFormat == 0) infoPtr->notifyFormat = NFR_ANSI;
9296 if ((infoPtr->uView == LV_VIEW_DETAILS) && (lpcs->style & WS_VISIBLE))
9298 if (LISTVIEW_CreateHeader(infoPtr) < 0) return -1;
9301 infoPtr->hwndHeader = 0;
9303 /* init item size to avoid division by 0 */
9304 LISTVIEW_UpdateItemSize (infoPtr);
9306 if (infoPtr->uView == LV_VIEW_DETAILS)
9308 if (!(LVS_NOCOLUMNHEADER & lpcs->style) && (WS_VISIBLE & lpcs->style))
9310 ShowWindow(infoPtr->hwndHeader, SW_SHOWNORMAL);
9312 LISTVIEW_UpdateScroll(infoPtr);
9313 /* send WM_MEASUREITEM notification */
9314 if (infoPtr->dwStyle & LVS_OWNERDRAWFIXED) notify_measureitem(infoPtr);
9317 OpenThemeData(hwnd, themeClass);
9319 /* initialize the icon sizes */
9320 set_icon_size(&infoPtr->iconSize, infoPtr->himlNormal, infoPtr->uView != LV_VIEW_ICON);
9321 set_icon_size(&infoPtr->iconStateSize, infoPtr->himlState, TRUE);
9327 * Destroys the listview control.
9330 * [I] infoPtr : valid pointer to the listview structure
9336 static LRESULT LISTVIEW_Destroy(LISTVIEW_INFO *infoPtr)
9338 HTHEME theme = GetWindowTheme(infoPtr->hwndSelf);
9339 CloseThemeData(theme);
9341 /* delete all items */
9342 LISTVIEW_DeleteAllItems(infoPtr, TRUE);
9349 * Enables the listview control.
9352 * [I] infoPtr : valid pointer to the listview structure
9353 * [I] bEnable : specifies whether to enable or disable the window
9359 static BOOL LISTVIEW_Enable(const LISTVIEW_INFO *infoPtr)
9361 if (infoPtr->dwStyle & LVS_OWNERDRAWFIXED)
9362 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
9368 * Erases the background of the listview control.
9371 * [I] infoPtr : valid pointer to the listview structure
9372 * [I] hdc : device context handle
9378 static inline BOOL LISTVIEW_EraseBkgnd(const LISTVIEW_INFO *infoPtr, HDC hdc)
9382 TRACE("(hdc=%p)\n", hdc);
9384 if (!GetClipBox(hdc, &rc)) return FALSE;
9386 if (infoPtr->clrBk == CLR_NONE)
9388 if (infoPtr->dwLvExStyle & LVS_EX_TRANSPARENTBKGND)
9389 return SendMessageW(infoPtr->hwndNotify, WM_PRINTCLIENT,
9390 (WPARAM)hdc, PRF_ERASEBKGND);
9392 return SendMessageW(infoPtr->hwndNotify, WM_ERASEBKGND, (WPARAM)hdc, 0);
9395 /* for double buffered controls we need to do this during refresh */
9396 if (infoPtr->dwLvExStyle & LVS_EX_DOUBLEBUFFER) return FALSE;
9398 return LISTVIEW_FillBkgnd(infoPtr, hdc, &rc);
9404 * Helper function for LISTVIEW_[HV]Scroll *only*.
9405 * Performs vertical/horizontal scrolling by a give amount.
9408 * [I] infoPtr : valid pointer to the listview structure
9409 * [I] dx : amount of horizontal scroll
9410 * [I] dy : amount of vertical scroll
9412 static void scroll_list(LISTVIEW_INFO *infoPtr, INT dx, INT dy)
9414 /* now we can scroll the list */
9415 ScrollWindowEx(infoPtr->hwndSelf, dx, dy, &infoPtr->rcList,
9416 &infoPtr->rcList, 0, 0, SW_ERASE | SW_INVALIDATE);
9417 /* if we have focus, adjust rect */
9418 OffsetRect(&infoPtr->rcFocus, dx, dy);
9419 UpdateWindow(infoPtr->hwndSelf);
9424 * Performs vertical scrolling.
9427 * [I] infoPtr : valid pointer to the listview structure
9428 * [I] nScrollCode : scroll code
9429 * [I] nScrollDiff : units to scroll in SB_INTERNAL mode, 0 otherwise
9430 * [I] hScrollWnd : scrollbar control window handle
9436 * SB_LINEUP/SB_LINEDOWN:
9437 * for LVS_ICON, LVS_SMALLICON is 37 by experiment
9438 * for LVS_REPORT is 1 line
9439 * for LVS_LIST cannot occur
9442 static LRESULT LISTVIEW_VScroll(LISTVIEW_INFO *infoPtr, INT nScrollCode,
9445 INT nOldScrollPos, nNewScrollPos;
9446 SCROLLINFO scrollInfo;
9449 TRACE("(nScrollCode=%d(%s), nScrollDiff=%d)\n", nScrollCode,
9450 debugscrollcode(nScrollCode), nScrollDiff);
9452 if (infoPtr->hwndEdit) SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
9454 scrollInfo.cbSize = sizeof(SCROLLINFO);
9455 scrollInfo.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_TRACKPOS;
9457 is_an_icon = ((infoPtr->uView == LV_VIEW_ICON) || (infoPtr->uView == LV_VIEW_SMALLICON));
9459 if (!GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo)) return 1;
9461 nOldScrollPos = scrollInfo.nPos;
9462 switch (nScrollCode)
9468 nScrollDiff = (is_an_icon) ? -LISTVIEW_SCROLL_ICON_LINE_SIZE : -1;
9472 nScrollDiff = (is_an_icon) ? LISTVIEW_SCROLL_ICON_LINE_SIZE : 1;
9476 nScrollDiff = -scrollInfo.nPage;
9480 nScrollDiff = scrollInfo.nPage;
9483 case SB_THUMBPOSITION:
9485 nScrollDiff = scrollInfo.nTrackPos - scrollInfo.nPos;
9492 /* quit right away if pos isn't changing */
9493 if (nScrollDiff == 0) return 0;
9495 /* calculate new position, and handle overflows */
9496 nNewScrollPos = scrollInfo.nPos + nScrollDiff;
9497 if (nScrollDiff > 0) {
9498 if (nNewScrollPos < nOldScrollPos ||
9499 nNewScrollPos > scrollInfo.nMax)
9500 nNewScrollPos = scrollInfo.nMax;
9502 if (nNewScrollPos > nOldScrollPos ||
9503 nNewScrollPos < scrollInfo.nMin)
9504 nNewScrollPos = scrollInfo.nMin;
9507 /* set the new position, and reread in case it changed */
9508 scrollInfo.fMask = SIF_POS;
9509 scrollInfo.nPos = nNewScrollPos;
9510 nNewScrollPos = SetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo, TRUE);
9512 /* carry on only if it really changed */
9513 if (nNewScrollPos == nOldScrollPos) return 0;
9515 /* now adjust to client coordinates */
9516 nScrollDiff = nOldScrollPos - nNewScrollPos;
9517 if (infoPtr->uView == LV_VIEW_DETAILS) nScrollDiff *= infoPtr->nItemHeight;
9519 /* and scroll the window */
9520 scroll_list(infoPtr, 0, nScrollDiff);
9527 * Performs horizontal scrolling.
9530 * [I] infoPtr : valid pointer to the listview structure
9531 * [I] nScrollCode : scroll code
9532 * [I] nScrollDiff : units to scroll in SB_INTERNAL mode, 0 otherwise
9533 * [I] hScrollWnd : scrollbar control window handle
9539 * SB_LINELEFT/SB_LINERIGHT:
9540 * for LVS_ICON, LVS_SMALLICON 1 pixel
9541 * for LVS_REPORT is 1 pixel
9542 * for LVS_LIST is 1 column --> which is a 1 because the
9543 * scroll is based on columns not pixels
9546 static LRESULT LISTVIEW_HScroll(LISTVIEW_INFO *infoPtr, INT nScrollCode,
9547 INT nScrollDiff, HWND hScrollWnd)
9549 INT nOldScrollPos, nNewScrollPos;
9550 SCROLLINFO scrollInfo;
9552 TRACE("(nScrollCode=%d(%s), nScrollDiff=%d)\n", nScrollCode,
9553 debugscrollcode(nScrollCode), nScrollDiff);
9555 if (infoPtr->hwndEdit) SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
9557 scrollInfo.cbSize = sizeof(SCROLLINFO);
9558 scrollInfo.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_TRACKPOS;
9560 if (!GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo)) return 1;
9562 nOldScrollPos = scrollInfo.nPos;
9564 switch (nScrollCode)
9578 nScrollDiff = -scrollInfo.nPage;
9582 nScrollDiff = scrollInfo.nPage;
9585 case SB_THUMBPOSITION:
9587 nScrollDiff = scrollInfo.nTrackPos - scrollInfo.nPos;
9594 /* quit right away if pos isn't changing */
9595 if (nScrollDiff == 0) return 0;
9597 /* calculate new position, and handle overflows */
9598 nNewScrollPos = scrollInfo.nPos + nScrollDiff;
9599 if (nScrollDiff > 0) {
9600 if (nNewScrollPos < nOldScrollPos ||
9601 nNewScrollPos > scrollInfo.nMax)
9602 nNewScrollPos = scrollInfo.nMax;
9604 if (nNewScrollPos > nOldScrollPos ||
9605 nNewScrollPos < scrollInfo.nMin)
9606 nNewScrollPos = scrollInfo.nMin;
9609 /* set the new position, and reread in case it changed */
9610 scrollInfo.fMask = SIF_POS;
9611 scrollInfo.nPos = nNewScrollPos;
9612 nNewScrollPos = SetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo, TRUE);
9614 /* carry on only if it really changed */
9615 if (nNewScrollPos == nOldScrollPos) return 0;
9617 if (infoPtr->hwndHeader) LISTVIEW_UpdateHeaderSize(infoPtr, nNewScrollPos);
9619 /* now adjust to client coordinates */
9620 nScrollDiff = nOldScrollPos - nNewScrollPos;
9621 if (infoPtr->uView == LV_VIEW_LIST) nScrollDiff *= infoPtr->nItemWidth;
9623 /* and scroll the window */
9624 scroll_list(infoPtr, nScrollDiff, 0);
9629 static LRESULT LISTVIEW_MouseWheel(LISTVIEW_INFO *infoPtr, INT wheelDelta)
9631 INT gcWheelDelta = 0;
9632 INT pulScrollLines = 3;
9634 TRACE("(wheelDelta=%d)\n", wheelDelta);
9636 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
9637 gcWheelDelta -= wheelDelta;
9639 switch(infoPtr->uView)
9642 case LV_VIEW_SMALLICON:
9644 * listview should be scrolled by a multiple of 37 dependently on its dimension or its visible item number
9645 * should be fixed in the future.
9647 LISTVIEW_VScroll(infoPtr, SB_INTERNAL, (gcWheelDelta < 0) ?
9648 -LISTVIEW_SCROLL_ICON_LINE_SIZE : LISTVIEW_SCROLL_ICON_LINE_SIZE);
9651 case LV_VIEW_DETAILS:
9652 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
9654 int cLineScroll = min(LISTVIEW_GetCountPerColumn(infoPtr), pulScrollLines);
9655 cLineScroll *= (gcWheelDelta / WHEEL_DELTA);
9656 LISTVIEW_VScroll(infoPtr, SB_INTERNAL, cLineScroll);
9661 LISTVIEW_HScroll(infoPtr, (gcWheelDelta < 0) ? SB_LINELEFT : SB_LINERIGHT, 0, 0);
9672 * [I] infoPtr : valid pointer to the listview structure
9673 * [I] nVirtualKey : virtual key
9674 * [I] lKeyData : key data
9679 static LRESULT LISTVIEW_KeyDown(LISTVIEW_INFO *infoPtr, INT nVirtualKey, LONG lKeyData)
9681 HWND hwndSelf = infoPtr->hwndSelf;
9683 NMLVKEYDOWN nmKeyDown;
9685 TRACE("(nVirtualKey=%d, lKeyData=%d)\n", nVirtualKey, lKeyData);
9687 /* send LVN_KEYDOWN notification */
9688 nmKeyDown.wVKey = nVirtualKey;
9689 nmKeyDown.flags = 0;
9690 notify_hdr(infoPtr, LVN_KEYDOWN, &nmKeyDown.hdr);
9691 if (!IsWindow(hwndSelf))
9694 switch (nVirtualKey)
9697 nItem = infoPtr->nFocusedItem;
9698 if (infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES)
9699 toggle_checkbox_state(infoPtr, infoPtr->nFocusedItem);
9703 if ((infoPtr->nItemCount > 0) && (infoPtr->nFocusedItem != -1))
9705 if (!notify(infoPtr, NM_RETURN)) return 0;
9706 if (!notify(infoPtr, LVN_ITEMACTIVATE)) return 0;
9711 if (infoPtr->nItemCount > 0)
9716 if (infoPtr->nItemCount > 0)
9717 nItem = infoPtr->nItemCount - 1;
9721 nItem = LISTVIEW_GetNextItem(infoPtr, infoPtr->nFocusedItem, LVNI_TOLEFT);
9725 nItem = LISTVIEW_GetNextItem(infoPtr, infoPtr->nFocusedItem, LVNI_ABOVE);
9729 nItem = LISTVIEW_GetNextItem(infoPtr, infoPtr->nFocusedItem, LVNI_TORIGHT);
9733 nItem = LISTVIEW_GetNextItem(infoPtr, infoPtr->nFocusedItem, LVNI_BELOW);
9737 if (infoPtr->uView == LV_VIEW_DETAILS)
9739 INT topidx = LISTVIEW_GetTopIndex(infoPtr);
9740 if (infoPtr->nFocusedItem == topidx)
9741 nItem = topidx - LISTVIEW_GetCountPerColumn(infoPtr) + 1;
9746 nItem = infoPtr->nFocusedItem - LISTVIEW_GetCountPerColumn(infoPtr)
9747 * LISTVIEW_GetCountPerRow(infoPtr);
9748 if(nItem < 0) nItem = 0;
9752 if (infoPtr->uView == LV_VIEW_DETAILS)
9754 INT topidx = LISTVIEW_GetTopIndex(infoPtr);
9755 INT cnt = LISTVIEW_GetCountPerColumn(infoPtr);
9756 if (infoPtr->nFocusedItem == topidx + cnt - 1)
9757 nItem = infoPtr->nFocusedItem + cnt - 1;
9759 nItem = topidx + cnt - 1;
9762 nItem = infoPtr->nFocusedItem + LISTVIEW_GetCountPerColumn(infoPtr)
9763 * LISTVIEW_GetCountPerRow(infoPtr);
9764 if(nItem >= infoPtr->nItemCount) nItem = infoPtr->nItemCount - 1;
9768 if ((nItem != -1) && (nItem != infoPtr->nFocusedItem || nVirtualKey == VK_SPACE))
9769 LISTVIEW_KeySelection(infoPtr, nItem, nVirtualKey == VK_SPACE);
9779 * [I] infoPtr : valid pointer to the listview structure
9784 static LRESULT LISTVIEW_KillFocus(LISTVIEW_INFO *infoPtr)
9788 /* if we did not have the focus, there's nothing to do */
9789 if (!infoPtr->bFocus) return 0;
9791 /* send NM_KILLFOCUS notification */
9792 if (!notify(infoPtr, NM_KILLFOCUS)) return 0;
9794 /* if we have a focus rectangle, get rid of it */
9795 LISTVIEW_ShowFocusRect(infoPtr, FALSE);
9797 /* if have a marquee selection, stop it */
9798 if (infoPtr->bMarqueeSelect)
9800 /* Remove the marquee rectangle and release our mouse capture */
9801 LISTVIEW_InvalidateRect(infoPtr, &infoPtr->marqueeRect);
9804 SetRect(&infoPtr->marqueeRect, 0, 0, 0, 0);
9806 infoPtr->bMarqueeSelect = FALSE;
9807 infoPtr->bScrolling = FALSE;
9808 KillTimer(infoPtr->hwndSelf, (UINT_PTR) infoPtr);
9811 /* set window focus flag */
9812 infoPtr->bFocus = FALSE;
9814 /* invalidate the selected items before resetting focus flag */
9815 LISTVIEW_InvalidateSelectedItems(infoPtr);
9822 * Processes double click messages (left mouse button).
9825 * [I] infoPtr : valid pointer to the listview structure
9826 * [I] wKey : key flag
9827 * [I] x,y : mouse coordinate
9832 static LRESULT LISTVIEW_LButtonDblClk(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
9834 LVHITTESTINFO htInfo;
9836 TRACE("(key=%hu, X=%u, Y=%u)\n", wKey, x, y);
9838 /* Cancel the item edition if any */
9839 if (infoPtr->itemEdit.fEnabled)
9841 KillTimer(infoPtr->hwndSelf, (UINT_PTR)&infoPtr->itemEdit);
9842 infoPtr->itemEdit.fEnabled = FALSE;
9845 /* send NM_RELEASEDCAPTURE notification */
9846 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
9851 /* send NM_DBLCLK notification */
9852 LISTVIEW_HitTest(infoPtr, &htInfo, TRUE, FALSE);
9853 if (!notify_click(infoPtr, NM_DBLCLK, &htInfo)) return 0;
9855 /* To send the LVN_ITEMACTIVATE, it must be on an Item */
9856 if(htInfo.iItem != -1) notify_itemactivate(infoPtr,&htInfo);
9863 * Processes mouse down messages (left mouse button).
9866 * infoPtr [I ] valid pointer to the listview structure
9867 * wKey [I ] key flag
9868 * x,y [I ] mouse coordinate
9873 static LRESULT LISTVIEW_LButtonDown(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
9875 LVHITTESTINFO lvHitTestInfo;
9876 static BOOL bGroupSelect = TRUE;
9877 POINT pt = { x, y };
9880 TRACE("(key=%hu, X=%u, Y=%u)\n", wKey, x, y);
9882 /* send NM_RELEASEDCAPTURE notification */
9883 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
9885 /* set left button down flag and record the click position */
9886 infoPtr->bLButtonDown = TRUE;
9887 infoPtr->ptClickPos = pt;
9888 infoPtr->bDragging = FALSE;
9889 infoPtr->bMarqueeSelect = FALSE;
9890 infoPtr->bScrolling = FALSE;
9892 lvHitTestInfo.pt.x = x;
9893 lvHitTestInfo.pt.y = y;
9895 nItem = LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, TRUE);
9896 TRACE("at %s, nItem=%d\n", wine_dbgstr_point(&pt), nItem);
9897 if ((nItem >= 0) && (nItem < infoPtr->nItemCount))
9899 if ((infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES) && (lvHitTestInfo.flags & LVHT_ONITEMSTATEICON))
9901 toggle_checkbox_state(infoPtr, nItem);
9905 if (infoPtr->dwStyle & LVS_SINGLESEL)
9907 if (LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED))
9908 infoPtr->nEditLabelItem = nItem;
9910 LISTVIEW_SetSelection(infoPtr, nItem);
9914 if ((wKey & MK_CONTROL) && (wKey & MK_SHIFT))
9918 if (!LISTVIEW_AddGroupSelection(infoPtr, nItem)) return 0;
9919 LISTVIEW_SetItemFocus(infoPtr, nItem);
9920 infoPtr->nSelectionMark = nItem;
9926 item.state = LVIS_SELECTED | LVIS_FOCUSED;
9927 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
9929 LISTVIEW_SetItemState(infoPtr,nItem,&item);
9930 infoPtr->nSelectionMark = nItem;
9933 else if (wKey & MK_CONTROL)
9937 bGroupSelect = (LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED) == 0);
9939 item.state = (bGroupSelect ? LVIS_SELECTED : 0) | LVIS_FOCUSED;
9940 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
9941 LISTVIEW_SetItemState(infoPtr, nItem, &item);
9942 infoPtr->nSelectionMark = nItem;
9944 else if (wKey & MK_SHIFT)
9946 LISTVIEW_SetGroupSelection(infoPtr, nItem);
9950 if (LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED))
9952 infoPtr->nEditLabelItem = nItem;
9953 infoPtr->nLButtonDownItem = nItem;
9955 LISTVIEW_SetItemFocus(infoPtr, nItem);
9958 /* set selection (clears other pre-existing selections) */
9959 LISTVIEW_SetSelection(infoPtr, nItem);
9963 if (infoPtr->dwLvExStyle & LVS_EX_ONECLICKACTIVATE)
9964 if(lvHitTestInfo.iItem != -1) notify_itemactivate(infoPtr,&lvHitTestInfo);
9968 if (!infoPtr->bFocus)
9969 SetFocus(infoPtr->hwndSelf);
9971 /* remove all selections */
9972 if (!(wKey & MK_CONTROL) && !(wKey & MK_SHIFT))
9973 LISTVIEW_DeselectAll(infoPtr);
9982 * Processes mouse up messages (left mouse button).
9985 * infoPtr [I ] valid pointer to the listview structure
9986 * wKey [I ] key flag
9987 * x,y [I ] mouse coordinate
9992 static LRESULT LISTVIEW_LButtonUp(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
9994 LVHITTESTINFO lvHitTestInfo;
9996 TRACE("(key=%hu, X=%u, Y=%u)\n", wKey, x, y);
9998 if (!infoPtr->bLButtonDown) return 0;
10000 lvHitTestInfo.pt.x = x;
10001 lvHitTestInfo.pt.y = y;
10003 /* send NM_CLICK notification */
10004 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
10005 if (!notify_click(infoPtr, NM_CLICK, &lvHitTestInfo)) return 0;
10007 /* set left button flag */
10008 infoPtr->bLButtonDown = FALSE;
10010 /* set a single selection, reset others */
10011 if(lvHitTestInfo.iItem == infoPtr->nLButtonDownItem && lvHitTestInfo.iItem != -1)
10012 LISTVIEW_SetSelection(infoPtr, infoPtr->nLButtonDownItem);
10013 infoPtr->nLButtonDownItem = -1;
10015 if (infoPtr->bDragging || infoPtr->bMarqueeSelect)
10017 /* Remove the marquee rectangle and release our mouse capture */
10018 if (infoPtr->bMarqueeSelect)
10020 LISTVIEW_InvalidateRect(infoPtr, &infoPtr->marqueeDrawRect);
10024 SetRect(&infoPtr->marqueeRect, 0, 0, 0, 0);
10025 SetRect(&infoPtr->marqueeDrawRect, 0, 0, 0, 0);
10027 infoPtr->bDragging = FALSE;
10028 infoPtr->bMarqueeSelect = FALSE;
10029 infoPtr->bScrolling = FALSE;
10031 KillTimer(infoPtr->hwndSelf, (UINT_PTR) infoPtr);
10035 /* if we clicked on a selected item, edit the label */
10036 if(lvHitTestInfo.iItem == infoPtr->nEditLabelItem && (lvHitTestInfo.flags & LVHT_ONITEMLABEL))
10038 /* we want to make sure the user doesn't want to do a double click. So we will
10039 * delay the edit. WM_LBUTTONDBLCLICK will cancel the timer
10041 infoPtr->itemEdit.fEnabled = TRUE;
10042 infoPtr->itemEdit.iItem = lvHitTestInfo.iItem;
10043 SetTimer(infoPtr->hwndSelf,
10044 (UINT_PTR)&infoPtr->itemEdit,
10045 GetDoubleClickTime(),
10046 LISTVIEW_DelayedEditItem);
10049 if (!infoPtr->bFocus)
10050 SetFocus(infoPtr->hwndSelf);
10057 * Destroys the listview control (called after WM_DESTROY).
10060 * [I] infoPtr : valid pointer to the listview structure
10065 static LRESULT LISTVIEW_NCDestroy(LISTVIEW_INFO *infoPtr)
10071 /* destroy data structure */
10072 DPA_Destroy(infoPtr->hdpaItems);
10073 DPA_Destroy(infoPtr->hdpaItemIds);
10074 DPA_Destroy(infoPtr->hdpaPosX);
10075 DPA_Destroy(infoPtr->hdpaPosY);
10077 for (i = 0; i < DPA_GetPtrCount(infoPtr->hdpaColumns); i++)
10078 Free(DPA_GetPtr(infoPtr->hdpaColumns, i));
10079 DPA_Destroy(infoPtr->hdpaColumns);
10080 ranges_destroy(infoPtr->selectionRanges);
10082 /* destroy image lists */
10083 if (!(infoPtr->dwStyle & LVS_SHAREIMAGELISTS))
10085 ImageList_Destroy(infoPtr->himlNormal);
10086 ImageList_Destroy(infoPtr->himlSmall);
10087 ImageList_Destroy(infoPtr->himlState);
10090 /* destroy font, bkgnd brush */
10091 infoPtr->hFont = 0;
10092 if (infoPtr->hDefaultFont) DeleteObject(infoPtr->hDefaultFont);
10093 if (infoPtr->clrBk != CLR_NONE) DeleteObject(infoPtr->hBkBrush);
10095 SetWindowLongPtrW(infoPtr->hwndSelf, 0, 0);
10097 /* free listview info pointer*/
10105 * Handles notifications from header.
10108 * [I] infoPtr : valid pointer to the listview structure
10109 * [I] nCtrlId : control identifier
10110 * [I] lpnmh : notification information
10115 static LRESULT LISTVIEW_HeaderNotification(LISTVIEW_INFO *infoPtr, const NMHEADERW *lpnmh)
10117 HWND hwndSelf = infoPtr->hwndSelf;
10119 TRACE("(lpnmh=%p)\n", lpnmh);
10121 if (!lpnmh || lpnmh->iItem < 0 || lpnmh->iItem >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return 0;
10123 switch (lpnmh->hdr.code)
10128 COLUMN_INFO *lpColumnInfo;
10132 if (!lpnmh->pitem || !(lpnmh->pitem->mask & HDI_WIDTH))
10135 /* remove the old line (if any) */
10136 LISTVIEW_DrawTrackLine(infoPtr);
10138 /* compute & draw the new line */
10139 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, lpnmh->iItem);
10140 x = lpColumnInfo->rcHeader.left + lpnmh->pitem->cxy;
10141 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
10142 infoPtr->xTrackLine = x + ptOrigin.x;
10143 LISTVIEW_DrawTrackLine(infoPtr);
10147 case HDN_ENDTRACKA:
10148 case HDN_ENDTRACKW:
10149 /* remove the track line (if any) */
10150 LISTVIEW_DrawTrackLine(infoPtr);
10151 infoPtr->xTrackLine = -1;
10154 case HDN_BEGINDRAG:
10155 notify_forward_header(infoPtr, lpnmh);
10156 return (infoPtr->dwLvExStyle & LVS_EX_HEADERDRAGDROP) == 0;
10159 infoPtr->colRectsDirty = TRUE;
10160 LISTVIEW_InvalidateList(infoPtr);
10161 notify_forward_header(infoPtr, lpnmh);
10164 case HDN_ITEMCHANGINGW:
10165 case HDN_ITEMCHANGINGA:
10166 return notify_forward_header(infoPtr, lpnmh);
10168 case HDN_ITEMCHANGEDW:
10169 case HDN_ITEMCHANGEDA:
10171 COLUMN_INFO *lpColumnInfo;
10175 notify_forward_header(infoPtr, lpnmh);
10176 if (!IsWindow(hwndSelf))
10179 if (!lpnmh->pitem || !(lpnmh->pitem->mask & HDI_WIDTH))
10181 hdi.mask = HDI_WIDTH;
10182 if (!SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, lpnmh->iItem, (LPARAM)&hdi)) return 0;
10186 cxy = lpnmh->pitem->cxy;
10188 /* determine how much we change since the last know position */
10189 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, lpnmh->iItem);
10190 dx = cxy - (lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left);
10193 lpColumnInfo->rcHeader.right += dx;
10195 hdi.mask = HDI_ORDER;
10196 SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, lpnmh->iItem, (LPARAM)&hdi);
10198 /* not the rightmost one */
10199 if (hdi.iOrder + 1 < DPA_GetPtrCount(infoPtr->hdpaColumns))
10201 INT nIndex = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX,
10202 hdi.iOrder + 1, 0);
10203 LISTVIEW_ScrollColumns(infoPtr, nIndex, dx);
10207 /* only needs to update the scrolls */
10208 infoPtr->nItemWidth += dx;
10209 LISTVIEW_UpdateScroll(infoPtr);
10211 LISTVIEW_UpdateItemSize(infoPtr);
10212 if (infoPtr->uView == LV_VIEW_DETAILS && is_redrawing(infoPtr))
10215 RECT rcCol = lpColumnInfo->rcHeader;
10217 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
10218 OffsetRect(&rcCol, ptOrigin.x, 0);
10220 rcCol.top = infoPtr->rcList.top;
10221 rcCol.bottom = infoPtr->rcList.bottom;
10223 /* resizing left-aligned columns leaves most of the left side untouched */
10224 if ((lpColumnInfo->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_LEFT)
10226 INT nMaxDirty = infoPtr->nEllipsisWidth + infoPtr->ntmMaxCharWidth;
10229 rcCol.left = max (rcCol.left, rcCol.right - nMaxDirty);
10232 /* when shrinking the last column clear the now unused field */
10233 if (hdi.iOrder == DPA_GetPtrCount(infoPtr->hdpaColumns) - 1)
10239 /* deal with right from rightmost column area */
10240 right.left = rcCol.right;
10241 right.top = rcCol.top;
10242 right.bottom = rcCol.bottom;
10243 right.right = infoPtr->rcList.right;
10245 LISTVIEW_InvalidateRect(infoPtr, &right);
10248 LISTVIEW_InvalidateRect(infoPtr, &rcCol);
10254 case HDN_ITEMCLICKW:
10255 case HDN_ITEMCLICKA:
10257 /* Handle sorting by Header Column */
10260 ZeroMemory(&nmlv, sizeof(NMLISTVIEW));
10262 nmlv.iSubItem = lpnmh->iItem;
10263 notify_listview(infoPtr, LVN_COLUMNCLICK, &nmlv);
10264 notify_forward_header(infoPtr, lpnmh);
10268 case HDN_DIVIDERDBLCLICKW:
10269 case HDN_DIVIDERDBLCLICKA:
10270 /* FIXME: for LVS_EX_HEADERINALLVIEWS and not LV_VIEW_DETAILS
10271 we should use LVSCW_AUTOSIZE_USEHEADER, helper rework or
10272 split needed for that */
10273 LISTVIEW_SetColumnWidth(infoPtr, lpnmh->iItem, LVSCW_AUTOSIZE);
10274 notify_forward_header(infoPtr, lpnmh);
10283 * Paint non-client area of control.
10286 * [I] infoPtr : valid pointer to the listview structureof the sender
10287 * [I] region : update region
10290 * TRUE - frame was painted
10291 * FALSE - call default window proc
10293 static BOOL LISTVIEW_NCPaint(const LISTVIEW_INFO *infoPtr, HRGN region)
10295 HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
10299 int cxEdge = GetSystemMetrics (SM_CXEDGE),
10300 cyEdge = GetSystemMetrics (SM_CYEDGE);
10303 return DefWindowProcW (infoPtr->hwndSelf, WM_NCPAINT, (WPARAM)region, 0);
10305 GetWindowRect(infoPtr->hwndSelf, &r);
10307 cliprgn = CreateRectRgn (r.left + cxEdge, r.top + cyEdge,
10308 r.right - cxEdge, r.bottom - cyEdge);
10309 if (region != (HRGN)1)
10310 CombineRgn (cliprgn, cliprgn, region, RGN_AND);
10311 OffsetRect(&r, -r.left, -r.top);
10313 dc = GetDCEx(infoPtr->hwndSelf, region, DCX_WINDOW|DCX_INTERSECTRGN);
10314 OffsetRect(&r, -r.left, -r.top);
10316 if (IsThemeBackgroundPartiallyTransparent (theme, 0, 0))
10317 DrawThemeParentBackground(infoPtr->hwndSelf, dc, &r);
10318 DrawThemeBackground (theme, dc, 0, 0, &r, 0);
10319 ReleaseDC(infoPtr->hwndSelf, dc);
10321 /* Call default proc to get the scrollbars etc. painted */
10322 DefWindowProcW (infoPtr->hwndSelf, WM_NCPAINT, (WPARAM)cliprgn, 0);
10329 * Determines the type of structure to use.
10332 * [I] infoPtr : valid pointer to the listview structureof the sender
10333 * [I] hwndFrom : listview window handle
10334 * [I] nCommand : command specifying the nature of the WM_NOTIFYFORMAT
10339 static LRESULT LISTVIEW_NotifyFormat(LISTVIEW_INFO *infoPtr, HWND hwndFrom, INT nCommand)
10341 TRACE("(hwndFrom=%p, nCommand=%d)\n", hwndFrom, nCommand);
10343 if (nCommand == NF_REQUERY)
10344 infoPtr->notifyFormat = SendMessageW(infoPtr->hwndNotify, WM_NOTIFYFORMAT, (WPARAM)infoPtr->hwndSelf, NF_QUERY);
10346 return infoPtr->notifyFormat;
10351 * Paints/Repaints the listview control. Internal use.
10354 * [I] infoPtr : valid pointer to the listview structure
10355 * [I] hdc : device context handle
10360 static LRESULT LISTVIEW_Paint(LISTVIEW_INFO *infoPtr, HDC hdc)
10362 TRACE("(hdc=%p)\n", hdc);
10364 if (infoPtr->bNoItemMetrics && infoPtr->nItemCount)
10366 infoPtr->bNoItemMetrics = FALSE;
10367 LISTVIEW_UpdateItemSize(infoPtr);
10368 if (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON)
10369 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
10370 LISTVIEW_UpdateScroll(infoPtr);
10373 if (infoPtr->hwndHeader) UpdateWindow(infoPtr->hwndHeader);
10376 LISTVIEW_Refresh(infoPtr, hdc, NULL);
10381 hdc = BeginPaint(infoPtr->hwndSelf, &ps);
10382 if (!hdc) return 1;
10383 LISTVIEW_Refresh(infoPtr, hdc, ps.fErase ? &ps.rcPaint : NULL);
10384 EndPaint(infoPtr->hwndSelf, &ps);
10392 * Paints/Repaints the listview control, WM_PAINT handler.
10395 * [I] infoPtr : valid pointer to the listview structure
10396 * [I] hdc : device context handle
10401 static inline LRESULT LISTVIEW_WMPaint(LISTVIEW_INFO *infoPtr, HDC hdc)
10403 TRACE("(hdc=%p)\n", hdc);
10405 if (!is_redrawing(infoPtr))
10406 return DefWindowProcW (infoPtr->hwndSelf, WM_PAINT, (WPARAM)hdc, 0);
10408 return LISTVIEW_Paint(infoPtr, hdc);
10413 * Paints/Repaints the listview control.
10416 * [I] infoPtr : valid pointer to the listview structure
10417 * [I] hdc : device context handle
10418 * [I] options : drawing options
10423 static LRESULT LISTVIEW_PrintClient(LISTVIEW_INFO *infoPtr, HDC hdc, DWORD options)
10425 FIXME("Partial Stub: (hdc=%p options=0x%08x)\n", hdc, options);
10427 if ((options & PRF_CHECKVISIBLE) && !IsWindowVisible(infoPtr->hwndSelf))
10430 if (options & PRF_ERASEBKGND)
10431 LISTVIEW_EraseBkgnd(infoPtr, hdc);
10433 if (options & PRF_CLIENT)
10434 LISTVIEW_Paint(infoPtr, hdc);
10442 * Processes double click messages (right mouse button).
10445 * [I] infoPtr : valid pointer to the listview structure
10446 * [I] wKey : key flag
10447 * [I] x,y : mouse coordinate
10452 static LRESULT LISTVIEW_RButtonDblClk(const LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
10454 LVHITTESTINFO lvHitTestInfo;
10456 TRACE("(key=%hu,X=%u,Y=%u)\n", wKey, x, y);
10458 /* send NM_RELEASEDCAPTURE notification */
10459 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
10461 /* send NM_RDBLCLK notification */
10462 lvHitTestInfo.pt.x = x;
10463 lvHitTestInfo.pt.y = y;
10464 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
10465 notify_click(infoPtr, NM_RDBLCLK, &lvHitTestInfo);
10472 * Processes mouse down messages (right mouse button).
10475 * [I] infoPtr : valid pointer to the listview structure
10476 * [I] wKey : key flag
10477 * [I] x,y : mouse coordinate
10482 static LRESULT LISTVIEW_RButtonDown(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
10484 LVHITTESTINFO lvHitTestInfo;
10487 TRACE("(key=%hu,X=%u,Y=%u)\n", wKey, x, y);
10489 /* send NM_RELEASEDCAPTURE notification */
10490 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
10492 /* make sure the listview control window has the focus */
10493 if (!infoPtr->bFocus) SetFocus(infoPtr->hwndSelf);
10495 /* set right button down flag */
10496 infoPtr->bRButtonDown = TRUE;
10498 /* determine the index of the selected item */
10499 lvHitTestInfo.pt.x = x;
10500 lvHitTestInfo.pt.y = y;
10501 nItem = LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, TRUE);
10503 if ((nItem >= 0) && (nItem < infoPtr->nItemCount))
10505 LISTVIEW_SetItemFocus(infoPtr, nItem);
10506 if (!((wKey & MK_SHIFT) || (wKey & MK_CONTROL)) &&
10507 !LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED))
10508 LISTVIEW_SetSelection(infoPtr, nItem);
10512 LISTVIEW_DeselectAll(infoPtr);
10520 * Processes mouse up messages (right mouse button).
10523 * [I] infoPtr : valid pointer to the listview structure
10524 * [I] wKey : key flag
10525 * [I] x,y : mouse coordinate
10530 static LRESULT LISTVIEW_RButtonUp(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
10532 LVHITTESTINFO lvHitTestInfo;
10535 TRACE("(key=%hu,X=%u,Y=%u)\n", wKey, x, y);
10537 if (!infoPtr->bRButtonDown) return 0;
10539 /* set button flag */
10540 infoPtr->bRButtonDown = FALSE;
10542 /* Send NM_RCLICK notification */
10543 lvHitTestInfo.pt.x = x;
10544 lvHitTestInfo.pt.y = y;
10545 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
10546 if (!notify_click(infoPtr, NM_RCLICK, &lvHitTestInfo)) return 0;
10548 /* Change to screen coordinate for WM_CONTEXTMENU */
10549 pt = lvHitTestInfo.pt;
10550 ClientToScreen(infoPtr->hwndSelf, &pt);
10552 /* Send a WM_CONTEXTMENU message in response to the RBUTTONUP */
10553 SendMessageW(infoPtr->hwndSelf, WM_CONTEXTMENU,
10554 (WPARAM)infoPtr->hwndSelf, MAKELPARAM(pt.x, pt.y));
10565 * [I] infoPtr : valid pointer to the listview structure
10566 * [I] hwnd : window handle of window containing the cursor
10567 * [I] nHittest : hit-test code
10568 * [I] wMouseMsg : ideintifier of the mouse message
10571 * TRUE if cursor is set
10574 static BOOL LISTVIEW_SetCursor(const LISTVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
10576 LVHITTESTINFO lvHitTestInfo;
10578 if (!LISTVIEW_IsHotTracking(infoPtr)) goto forward;
10580 if (!infoPtr->hHotCursor) goto forward;
10582 GetCursorPos(&lvHitTestInfo.pt);
10583 if (LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, FALSE, FALSE) < 0) goto forward;
10585 SetCursor(infoPtr->hHotCursor);
10591 return DefWindowProcW(infoPtr->hwndSelf, WM_SETCURSOR, wParam, lParam);
10599 * [I] infoPtr : valid pointer to the listview structure
10600 * [I] hwndLoseFocus : handle of previously focused window
10605 static LRESULT LISTVIEW_SetFocus(LISTVIEW_INFO *infoPtr, HWND hwndLoseFocus)
10607 TRACE("(hwndLoseFocus=%p)\n", hwndLoseFocus);
10609 /* if we have the focus already, there's nothing to do */
10610 if (infoPtr->bFocus) return 0;
10612 /* send NM_SETFOCUS notification */
10613 if (!notify(infoPtr, NM_SETFOCUS)) return 0;
10615 /* set window focus flag */
10616 infoPtr->bFocus = TRUE;
10618 /* put the focus rect back on */
10619 LISTVIEW_ShowFocusRect(infoPtr, TRUE);
10621 /* redraw all visible selected items */
10622 LISTVIEW_InvalidateSelectedItems(infoPtr);
10632 * [I] infoPtr : valid pointer to the listview structure
10633 * [I] fRedraw : font handle
10634 * [I] fRedraw : redraw flag
10639 static LRESULT LISTVIEW_SetFont(LISTVIEW_INFO *infoPtr, HFONT hFont, WORD fRedraw)
10641 HFONT oldFont = infoPtr->hFont;
10643 TRACE("(hfont=%p,redraw=%hu)\n", hFont, fRedraw);
10645 infoPtr->hFont = hFont ? hFont : infoPtr->hDefaultFont;
10646 if (infoPtr->hFont == oldFont) return 0;
10648 LISTVIEW_SaveTextMetrics(infoPtr);
10650 if (infoPtr->uView == LV_VIEW_DETAILS)
10652 SendMessageW(infoPtr->hwndHeader, WM_SETFONT, (WPARAM)hFont, MAKELPARAM(fRedraw, 0));
10653 LISTVIEW_UpdateSize(infoPtr);
10654 LISTVIEW_UpdateScroll(infoPtr);
10657 if (fRedraw) LISTVIEW_InvalidateList(infoPtr);
10664 * Message handling for WM_SETREDRAW.
10665 * For the Listview, it invalidates the entire window (the doc specifies otherwise)
10668 * [I] infoPtr : valid pointer to the listview structure
10669 * [I] bRedraw: state of redraw flag
10672 * DefWinProc return value
10674 static LRESULT LISTVIEW_SetRedraw(LISTVIEW_INFO *infoPtr, BOOL bRedraw)
10676 TRACE("infoPtr->bRedraw=%d, bRedraw=%d\n", infoPtr->bRedraw, bRedraw);
10678 /* we cannot use straight equality here because _any_ non-zero value is TRUE */
10679 if ((infoPtr->bRedraw && bRedraw) || (!infoPtr->bRedraw && !bRedraw)) return 0;
10681 infoPtr->bRedraw = bRedraw;
10683 if(!bRedraw) return 0;
10685 if (is_autoarrange(infoPtr))
10686 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
10687 LISTVIEW_UpdateScroll(infoPtr);
10689 /* despite what the WM_SETREDRAW docs says, apps expect us
10690 * to invalidate the listview here... stupid! */
10691 LISTVIEW_InvalidateList(infoPtr);
10698 * Resizes the listview control. This function processes WM_SIZE
10699 * messages. At this time, the width and height are not used.
10702 * [I] infoPtr : valid pointer to the listview structure
10703 * [I] Width : new width
10704 * [I] Height : new height
10709 static LRESULT LISTVIEW_Size(LISTVIEW_INFO *infoPtr, int Width, int Height)
10711 RECT rcOld = infoPtr->rcList;
10713 TRACE("(width=%d, height=%d)\n", Width, Height);
10715 LISTVIEW_UpdateSize(infoPtr);
10716 if (EqualRect(&rcOld, &infoPtr->rcList)) return 0;
10718 /* do not bother with display related stuff if we're not redrawing */
10719 if (!is_redrawing(infoPtr)) return 0;
10721 if (is_autoarrange(infoPtr))
10722 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
10724 LISTVIEW_UpdateScroll(infoPtr);
10726 /* refresh all only for lists whose height changed significantly */
10727 if ((infoPtr->uView == LV_VIEW_LIST) &&
10728 (rcOld.bottom - rcOld.top) / infoPtr->nItemHeight !=
10729 (infoPtr->rcList.bottom - infoPtr->rcList.top) / infoPtr->nItemHeight)
10730 LISTVIEW_InvalidateList(infoPtr);
10737 * Sets the size information.
10740 * [I] infoPtr : valid pointer to the listview structure
10745 static void LISTVIEW_UpdateSize(LISTVIEW_INFO *infoPtr)
10747 TRACE("uView=%d, rcList(old)=%s\n", infoPtr->uView, wine_dbgstr_rect(&infoPtr->rcList));
10749 GetClientRect(infoPtr->hwndSelf, &infoPtr->rcList);
10751 if (infoPtr->uView == LV_VIEW_LIST)
10753 /* Apparently the "LIST" style is supposed to have the same
10754 * number of items in a column even if there is no scroll bar.
10755 * Since if a scroll bar already exists then the bottom is already
10756 * reduced, only reduce if the scroll bar does not currently exist.
10757 * The "2" is there to mimic the native control. I think it may be
10758 * related to either padding or edges. (GLA 7/2002)
10760 if (!(infoPtr->dwStyle & WS_HSCROLL))
10761 infoPtr->rcList.bottom -= GetSystemMetrics(SM_CYHSCROLL);
10762 infoPtr->rcList.bottom = max (infoPtr->rcList.bottom - 2, 0);
10765 /* if control created invisible header isn't created */
10766 if (infoPtr->hwndHeader)
10771 hl.prc = &infoPtr->rcList;
10773 SendMessageW( infoPtr->hwndHeader, HDM_LAYOUT, 0, (LPARAM)&hl );
10774 TRACE(" wp.flags=0x%08x, wp=%d,%d (%dx%d)\n", wp.flags, wp.x, wp.y, wp.cx, wp.cy);
10776 if (LISTVIEW_IsHeaderEnabled(infoPtr))
10777 wp.flags |= SWP_SHOWWINDOW;
10780 wp.flags |= SWP_HIDEWINDOW;
10784 SetWindowPos(wp.hwnd, wp.hwndInsertAfter, wp.x, wp.y, wp.cx, wp.cy, wp.flags);
10785 TRACE(" after SWP wp=%d,%d (%dx%d)\n", wp.x, wp.y, wp.cx, wp.cy);
10787 infoPtr->rcList.top = max(wp.cy, 0);
10789 /* extra padding for grid */
10790 if (infoPtr->uView == LV_VIEW_DETAILS && infoPtr->dwLvExStyle & LVS_EX_GRIDLINES)
10791 infoPtr->rcList.top += 2;
10793 TRACE(" rcList=%s\n", wine_dbgstr_rect(&infoPtr->rcList));
10798 * Processes WM_STYLECHANGED messages.
10801 * [I] infoPtr : valid pointer to the listview structure
10802 * [I] wStyleType : window style type (normal or extended)
10803 * [I] lpss : window style information
10808 static INT LISTVIEW_StyleChanged(LISTVIEW_INFO *infoPtr, WPARAM wStyleType,
10809 const STYLESTRUCT *lpss)
10811 UINT uNewView = lpss->styleNew & LVS_TYPEMASK;
10812 UINT uOldView = lpss->styleOld & LVS_TYPEMASK;
10815 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
10816 wStyleType, lpss->styleOld, lpss->styleNew);
10818 if (wStyleType != GWL_STYLE) return 0;
10820 infoPtr->dwStyle = lpss->styleNew;
10821 map_style_view(infoPtr);
10823 if (((lpss->styleOld & WS_HSCROLL) != 0)&&
10824 ((lpss->styleNew & WS_HSCROLL) == 0))
10825 ShowScrollBar(infoPtr->hwndSelf, SB_HORZ, FALSE);
10827 if (((lpss->styleOld & WS_VSCROLL) != 0)&&
10828 ((lpss->styleNew & WS_VSCROLL) == 0))
10829 ShowScrollBar(infoPtr->hwndSelf, SB_VERT, FALSE);
10831 if (uNewView != uOldView)
10833 SIZE oldIconSize = infoPtr->iconSize;
10836 SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
10837 ShowWindow(infoPtr->hwndHeader, SW_HIDE);
10839 ShowScrollBar(infoPtr->hwndSelf, SB_BOTH, FALSE);
10840 SetRectEmpty(&infoPtr->rcFocus);
10842 himl = (uNewView == LVS_ICON ? infoPtr->himlNormal : infoPtr->himlSmall);
10843 set_icon_size(&infoPtr->iconSize, himl, uNewView != LVS_ICON);
10845 if (uNewView == LVS_ICON)
10847 if ((infoPtr->iconSize.cx != oldIconSize.cx) || (infoPtr->iconSize.cy != oldIconSize.cy))
10849 TRACE("icon old size=(%d,%d), new size=(%d,%d)\n",
10850 oldIconSize.cx, oldIconSize.cy, infoPtr->iconSize.cx, infoPtr->iconSize.cy);
10851 LISTVIEW_SetIconSpacing(infoPtr, 0, 0);
10854 else if (uNewView == LVS_REPORT)
10859 LISTVIEW_CreateHeader( infoPtr );
10861 hl.prc = &infoPtr->rcList;
10863 SendMessageW( infoPtr->hwndHeader, HDM_LAYOUT, 0, (LPARAM)&hl );
10864 SetWindowPos(infoPtr->hwndHeader, infoPtr->hwndSelf, wp.x, wp.y, wp.cx, wp.cy,
10865 wp.flags | ((infoPtr->dwStyle & LVS_NOCOLUMNHEADER)
10866 ? SWP_HIDEWINDOW : SWP_SHOWWINDOW));
10869 LISTVIEW_UpdateItemSize(infoPtr);
10872 if (uNewView == LVS_REPORT || infoPtr->dwLvExStyle & LVS_EX_HEADERINALLVIEWS)
10874 if ((lpss->styleOld ^ lpss->styleNew) & LVS_NOCOLUMNHEADER)
10876 if (lpss->styleNew & LVS_NOCOLUMNHEADER)
10878 /* Turn off the header control */
10879 style = GetWindowLongW(infoPtr->hwndHeader, GWL_STYLE);
10880 TRACE("Hide header control, was 0x%08x\n", style);
10881 SetWindowLongW(infoPtr->hwndHeader, GWL_STYLE, style | HDS_HIDDEN);
10883 /* Turn on the header control */
10884 if ((style = GetWindowLongW(infoPtr->hwndHeader, GWL_STYLE)) & HDS_HIDDEN)
10886 TRACE("Show header control, was 0x%08x\n", style);
10887 SetWindowLongW(infoPtr->hwndHeader, GWL_STYLE, (style & ~HDS_HIDDEN) | WS_VISIBLE);
10893 if ( (uNewView == LVS_ICON || uNewView == LVS_SMALLICON) &&
10894 (uNewView != uOldView || ((lpss->styleNew ^ lpss->styleOld) & LVS_ALIGNMASK)) )
10895 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
10897 /* update the size of the client area */
10898 LISTVIEW_UpdateSize(infoPtr);
10900 /* add scrollbars if needed */
10901 LISTVIEW_UpdateScroll(infoPtr);
10903 /* invalidate client area + erase background */
10904 LISTVIEW_InvalidateList(infoPtr);
10911 * Processes WM_STYLECHANGING messages.
10914 * [I] wStyleType : window style type (normal or extended)
10915 * [I0] lpss : window style information
10920 static INT LISTVIEW_StyleChanging(WPARAM wStyleType,
10923 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
10924 wStyleType, lpss->styleOld, lpss->styleNew);
10926 /* don't forward LVS_OWNERDATA only if not already set to */
10927 if ((lpss->styleNew ^ lpss->styleOld) & LVS_OWNERDATA)
10929 if (lpss->styleOld & LVS_OWNERDATA)
10930 lpss->styleNew |= LVS_OWNERDATA;
10932 lpss->styleNew &= ~LVS_OWNERDATA;
10940 * Processes WM_SHOWWINDOW messages.
10943 * [I] infoPtr : valid pointer to the listview structure
10944 * [I] bShown : window is being shown (FALSE when hidden)
10945 * [I] iStatus : window show status
10950 static LRESULT LISTVIEW_ShowWindow(LISTVIEW_INFO *infoPtr, WPARAM bShown, LPARAM iStatus)
10952 /* header delayed creation */
10953 if ((infoPtr->uView == LV_VIEW_DETAILS) && bShown)
10955 LISTVIEW_CreateHeader(infoPtr);
10957 if (!(LVS_NOCOLUMNHEADER & infoPtr->dwStyle))
10958 ShowWindow(infoPtr->hwndHeader, SW_SHOWNORMAL);
10961 return DefWindowProcW(infoPtr->hwndSelf, WM_SHOWWINDOW, bShown, iStatus);
10966 * Processes CCM_GETVERSION messages.
10969 * [I] infoPtr : valid pointer to the listview structure
10974 static inline LRESULT LISTVIEW_GetVersion(const LISTVIEW_INFO *infoPtr)
10976 return infoPtr->iVersion;
10981 * Processes CCM_SETVERSION messages.
10984 * [I] infoPtr : valid pointer to the listview structure
10985 * [I] iVersion : version to be set
10988 * -1 when requested version is greater than DLL version;
10989 * previous version otherwise
10991 static LRESULT LISTVIEW_SetVersion(LISTVIEW_INFO *infoPtr, DWORD iVersion)
10993 INT iOldVersion = infoPtr->iVersion;
10995 if (iVersion > COMCTL32_VERSION)
10998 infoPtr->iVersion = iVersion;
11000 TRACE("new version %d\n", iVersion);
11002 return iOldVersion;
11007 * Window procedure of the listview control.
11010 static LRESULT WINAPI
11011 LISTVIEW_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
11013 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongPtrW(hwnd, 0);
11015 TRACE("(hwnd=%p uMsg=%x wParam=%lx lParam=%lx)\n", hwnd, uMsg, wParam, lParam);
11017 if (!infoPtr && (uMsg != WM_NCCREATE))
11018 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
11022 case LVM_APPROXIMATEVIEWRECT:
11023 return LISTVIEW_ApproximateViewRect(infoPtr, (INT)wParam,
11024 LOWORD(lParam), HIWORD(lParam));
11026 return LISTVIEW_Arrange(infoPtr, (INT)wParam);
11028 case LVM_CANCELEDITLABEL:
11029 return LISTVIEW_CancelEditLabel(infoPtr);
11031 case LVM_CREATEDRAGIMAGE:
11032 return (LRESULT)LISTVIEW_CreateDragImage(infoPtr, (INT)wParam, (LPPOINT)lParam);
11034 case LVM_DELETEALLITEMS:
11035 return LISTVIEW_DeleteAllItems(infoPtr, FALSE);
11037 case LVM_DELETECOLUMN:
11038 return LISTVIEW_DeleteColumn(infoPtr, (INT)wParam);
11040 case LVM_DELETEITEM:
11041 return LISTVIEW_DeleteItem(infoPtr, (INT)wParam);
11043 case LVM_EDITLABELA:
11044 case LVM_EDITLABELW:
11045 return (LRESULT)LISTVIEW_EditLabelT(infoPtr, (INT)wParam,
11046 uMsg == LVM_EDITLABELW);
11047 /* case LVM_ENABLEGROUPVIEW: */
11049 case LVM_ENSUREVISIBLE:
11050 return LISTVIEW_EnsureVisible(infoPtr, (INT)wParam, (BOOL)lParam);
11052 case LVM_FINDITEMW:
11053 return LISTVIEW_FindItemW(infoPtr, (INT)wParam, (LPLVFINDINFOW)lParam);
11055 case LVM_FINDITEMA:
11056 return LISTVIEW_FindItemA(infoPtr, (INT)wParam, (LPLVFINDINFOA)lParam);
11058 case LVM_GETBKCOLOR:
11059 return infoPtr->clrBk;
11061 /* case LVM_GETBKIMAGE: */
11063 case LVM_GETCALLBACKMASK:
11064 return infoPtr->uCallbackMask;
11066 case LVM_GETCOLUMNA:
11067 case LVM_GETCOLUMNW:
11068 return LISTVIEW_GetColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam,
11069 uMsg == LVM_GETCOLUMNW);
11071 case LVM_GETCOLUMNORDERARRAY:
11072 return LISTVIEW_GetColumnOrderArray(infoPtr, (INT)wParam, (LPINT)lParam);
11074 case LVM_GETCOLUMNWIDTH:
11075 return LISTVIEW_GetColumnWidth(infoPtr, (INT)wParam);
11077 case LVM_GETCOUNTPERPAGE:
11078 return LISTVIEW_GetCountPerPage(infoPtr);
11080 case LVM_GETEDITCONTROL:
11081 return (LRESULT)infoPtr->hwndEdit;
11083 case LVM_GETEXTENDEDLISTVIEWSTYLE:
11084 return infoPtr->dwLvExStyle;
11086 /* case LVM_GETGROUPINFO: */
11088 /* case LVM_GETGROUPMETRICS: */
11090 case LVM_GETHEADER:
11091 return (LRESULT)infoPtr->hwndHeader;
11093 case LVM_GETHOTCURSOR:
11094 return (LRESULT)infoPtr->hHotCursor;
11096 case LVM_GETHOTITEM:
11097 return infoPtr->nHotItem;
11099 case LVM_GETHOVERTIME:
11100 return infoPtr->dwHoverTime;
11102 case LVM_GETIMAGELIST:
11103 return (LRESULT)LISTVIEW_GetImageList(infoPtr, (INT)wParam);
11105 /* case LVM_GETINSERTMARK: */
11107 /* case LVM_GETINSERTMARKCOLOR: */
11109 /* case LVM_GETINSERTMARKRECT: */
11111 case LVM_GETISEARCHSTRINGA:
11112 case LVM_GETISEARCHSTRINGW:
11113 FIXME("LVM_GETISEARCHSTRING: unimplemented\n");
11118 return LISTVIEW_GetItemExtT(infoPtr, (LPLVITEMW)lParam, uMsg == LVM_GETITEMW);
11120 case LVM_GETITEMCOUNT:
11121 return infoPtr->nItemCount;
11123 case LVM_GETITEMPOSITION:
11124 return LISTVIEW_GetItemPosition(infoPtr, (INT)wParam, (LPPOINT)lParam);
11126 case LVM_GETITEMRECT:
11127 return LISTVIEW_GetItemRect(infoPtr, (INT)wParam, (LPRECT)lParam);
11129 case LVM_GETITEMSPACING:
11130 return LISTVIEW_GetItemSpacing(infoPtr, (BOOL)wParam);
11132 case LVM_GETITEMSTATE:
11133 return LISTVIEW_GetItemState(infoPtr, (INT)wParam, (UINT)lParam);
11135 case LVM_GETITEMTEXTA:
11136 case LVM_GETITEMTEXTW:
11137 return LISTVIEW_GetItemTextT(infoPtr, (INT)wParam, (LPLVITEMW)lParam,
11138 uMsg == LVM_GETITEMTEXTW);
11140 case LVM_GETNEXTITEM:
11141 return LISTVIEW_GetNextItem(infoPtr, (INT)wParam, LOWORD(lParam));
11143 case LVM_GETNUMBEROFWORKAREAS:
11144 FIXME("LVM_GETNUMBEROFWORKAREAS: unimplemented\n");
11147 case LVM_GETORIGIN:
11148 if (!lParam) return FALSE;
11149 if (infoPtr->uView == LV_VIEW_DETAILS ||
11150 infoPtr->uView == LV_VIEW_LIST) return FALSE;
11151 LISTVIEW_GetOrigin(infoPtr, (LPPOINT)lParam);
11154 /* case LVM_GETOUTLINECOLOR: */
11156 /* case LVM_GETSELECTEDCOLUMN: */
11158 case LVM_GETSELECTEDCOUNT:
11159 return LISTVIEW_GetSelectedCount(infoPtr);
11161 case LVM_GETSELECTIONMARK:
11162 return infoPtr->nSelectionMark;
11164 case LVM_GETSTRINGWIDTHA:
11165 case LVM_GETSTRINGWIDTHW:
11166 return LISTVIEW_GetStringWidthT(infoPtr, (LPCWSTR)lParam,
11167 uMsg == LVM_GETSTRINGWIDTHW);
11169 case LVM_GETSUBITEMRECT:
11170 return LISTVIEW_GetSubItemRect(infoPtr, (UINT)wParam, (LPRECT)lParam);
11172 case LVM_GETTEXTBKCOLOR:
11173 return infoPtr->clrTextBk;
11175 case LVM_GETTEXTCOLOR:
11176 return infoPtr->clrText;
11178 /* case LVM_GETTILEINFO: */
11180 /* case LVM_GETTILEVIEWINFO: */
11182 case LVM_GETTOOLTIPS:
11183 if( !infoPtr->hwndToolTip )
11184 infoPtr->hwndToolTip = COMCTL32_CreateToolTip( hwnd );
11185 return (LRESULT)infoPtr->hwndToolTip;
11187 case LVM_GETTOPINDEX:
11188 return LISTVIEW_GetTopIndex(infoPtr);
11190 case LVM_GETUNICODEFORMAT:
11191 return (infoPtr->notifyFormat == NFR_UNICODE);
11194 return infoPtr->uView;
11196 case LVM_GETVIEWRECT:
11197 return LISTVIEW_GetViewRect(infoPtr, (LPRECT)lParam);
11199 case LVM_GETWORKAREAS:
11200 FIXME("LVM_GETWORKAREAS: unimplemented\n");
11203 /* case LVM_HASGROUP: */
11206 return LISTVIEW_HitTest(infoPtr, (LPLVHITTESTINFO)lParam, FALSE, TRUE);
11208 case LVM_INSERTCOLUMNA:
11209 case LVM_INSERTCOLUMNW:
11210 return LISTVIEW_InsertColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam,
11211 uMsg == LVM_INSERTCOLUMNW);
11213 /* case LVM_INSERTGROUP: */
11215 /* case LVM_INSERTGROUPSORTED: */
11217 case LVM_INSERTITEMA:
11218 case LVM_INSERTITEMW:
11219 return LISTVIEW_InsertItemT(infoPtr, (LPLVITEMW)lParam, uMsg == LVM_INSERTITEMW);
11221 /* case LVM_INSERTMARKHITTEST: */
11223 /* case LVM_ISGROUPVIEWENABLED: */
11225 case LVM_ISITEMVISIBLE:
11226 return LISTVIEW_IsItemVisible(infoPtr, (INT)wParam);
11228 case LVM_MAPIDTOINDEX:
11229 return LISTVIEW_MapIdToIndex(infoPtr, (UINT)wParam);
11231 case LVM_MAPINDEXTOID:
11232 return LISTVIEW_MapIndexToId(infoPtr, (INT)wParam);
11234 /* case LVM_MOVEGROUP: */
11236 /* case LVM_MOVEITEMTOGROUP: */
11238 case LVM_REDRAWITEMS:
11239 return LISTVIEW_RedrawItems(infoPtr, (INT)wParam, (INT)lParam);
11241 /* case LVM_REMOVEALLGROUPS: */
11243 /* case LVM_REMOVEGROUP: */
11246 return LISTVIEW_Scroll(infoPtr, (INT)wParam, (INT)lParam);
11248 case LVM_SETBKCOLOR:
11249 return LISTVIEW_SetBkColor(infoPtr, (COLORREF)lParam);
11251 /* case LVM_SETBKIMAGE: */
11253 case LVM_SETCALLBACKMASK:
11254 infoPtr->uCallbackMask = (UINT)wParam;
11257 case LVM_SETCOLUMNA:
11258 case LVM_SETCOLUMNW:
11259 return LISTVIEW_SetColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam,
11260 uMsg == LVM_SETCOLUMNW);
11262 case LVM_SETCOLUMNORDERARRAY:
11263 return LISTVIEW_SetColumnOrderArray(infoPtr, (INT)wParam, (LPINT)lParam);
11265 case LVM_SETCOLUMNWIDTH:
11266 return LISTVIEW_SetColumnWidth(infoPtr, (INT)wParam, (short)LOWORD(lParam));
11268 case LVM_SETEXTENDEDLISTVIEWSTYLE:
11269 return LISTVIEW_SetExtendedListViewStyle(infoPtr, (DWORD)wParam, (DWORD)lParam);
11271 /* case LVM_SETGROUPINFO: */
11273 /* case LVM_SETGROUPMETRICS: */
11275 case LVM_SETHOTCURSOR:
11276 return (LRESULT)LISTVIEW_SetHotCursor(infoPtr, (HCURSOR)lParam);
11278 case LVM_SETHOTITEM:
11279 return LISTVIEW_SetHotItem(infoPtr, (INT)wParam);
11281 case LVM_SETHOVERTIME:
11282 return LISTVIEW_SetHoverTime(infoPtr, (DWORD)lParam);
11284 case LVM_SETICONSPACING:
11285 return LISTVIEW_SetIconSpacing(infoPtr, (short)LOWORD(lParam), (short)HIWORD(lParam));
11287 case LVM_SETIMAGELIST:
11288 return (LRESULT)LISTVIEW_SetImageList(infoPtr, (INT)wParam, (HIMAGELIST)lParam);
11290 /* case LVM_SETINFOTIP: */
11292 /* case LVM_SETINSERTMARK: */
11294 /* case LVM_SETINSERTMARKCOLOR: */
11299 if (infoPtr->dwStyle & LVS_OWNERDATA) return FALSE;
11300 return LISTVIEW_SetItemT(infoPtr, (LPLVITEMW)lParam, (uMsg == LVM_SETITEMW));
11303 case LVM_SETITEMCOUNT:
11304 return LISTVIEW_SetItemCount(infoPtr, (INT)wParam, (DWORD)lParam);
11306 case LVM_SETITEMPOSITION:
11309 pt.x = (short)LOWORD(lParam);
11310 pt.y = (short)HIWORD(lParam);
11311 return LISTVIEW_SetItemPosition(infoPtr, (INT)wParam, &pt);
11314 case LVM_SETITEMPOSITION32:
11315 return LISTVIEW_SetItemPosition(infoPtr, (INT)wParam, (POINT*)lParam);
11317 case LVM_SETITEMSTATE:
11318 if (lParam == 0) return FALSE;
11319 return LISTVIEW_SetItemState(infoPtr, (INT)wParam, (LPLVITEMW)lParam);
11321 case LVM_SETITEMTEXTA:
11322 case LVM_SETITEMTEXTW:
11323 return LISTVIEW_SetItemTextT(infoPtr, (INT)wParam, (LPLVITEMW)lParam,
11324 uMsg == LVM_SETITEMTEXTW);
11326 /* case LVM_SETOUTLINECOLOR: */
11328 /* case LVM_SETSELECTEDCOLUMN: */
11330 case LVM_SETSELECTIONMARK:
11331 return LISTVIEW_SetSelectionMark(infoPtr, (INT)lParam);
11333 case LVM_SETTEXTBKCOLOR:
11334 return LISTVIEW_SetTextBkColor(infoPtr, (COLORREF)lParam);
11336 case LVM_SETTEXTCOLOR:
11337 return LISTVIEW_SetTextColor(infoPtr, (COLORREF)lParam);
11339 /* case LVM_SETTILEINFO: */
11341 /* case LVM_SETTILEVIEWINFO: */
11343 /* case LVM_SETTILEWIDTH: */
11345 case LVM_SETTOOLTIPS:
11346 return (LRESULT)LISTVIEW_SetToolTips(infoPtr, (HWND)lParam);
11348 case LVM_SETUNICODEFORMAT:
11349 return LISTVIEW_SetUnicodeFormat(infoPtr, wParam);
11352 return LISTVIEW_SetView(infoPtr, wParam);
11354 /* case LVM_SETWORKAREAS: */
11356 /* case LVM_SORTGROUPS: */
11358 case LVM_SORTITEMS:
11359 case LVM_SORTITEMSEX:
11360 return LISTVIEW_SortItems(infoPtr, (PFNLVCOMPARE)lParam, wParam,
11361 uMsg == LVM_SORTITEMSEX);
11362 case LVM_SUBITEMHITTEST:
11363 return LISTVIEW_HitTest(infoPtr, (LPLVHITTESTINFO)lParam, TRUE, FALSE);
11366 return LISTVIEW_Update(infoPtr, (INT)wParam);
11368 case CCM_GETVERSION:
11369 return LISTVIEW_GetVersion(infoPtr);
11371 case CCM_SETVERSION:
11372 return LISTVIEW_SetVersion(infoPtr, wParam);
11375 return LISTVIEW_ProcessLetterKeys( infoPtr, wParam, lParam );
11378 return LISTVIEW_Command(infoPtr, wParam, lParam);
11381 return LISTVIEW_NCCreate(hwnd, (LPCREATESTRUCTW)lParam);
11384 return LISTVIEW_Create(hwnd, (LPCREATESTRUCTW)lParam);
11387 return LISTVIEW_Destroy(infoPtr);
11390 return LISTVIEW_Enable(infoPtr);
11392 case WM_ERASEBKGND:
11393 return LISTVIEW_EraseBkgnd(infoPtr, (HDC)wParam);
11395 case WM_GETDLGCODE:
11396 return DLGC_WANTCHARS | DLGC_WANTARROWS;
11399 return (LRESULT)infoPtr->hFont;
11402 return LISTVIEW_HScroll(infoPtr, (INT)LOWORD(wParam), 0, (HWND)lParam);
11405 return LISTVIEW_KeyDown(infoPtr, (INT)wParam, (LONG)lParam);
11408 return LISTVIEW_KillFocus(infoPtr);
11410 case WM_LBUTTONDBLCLK:
11411 return LISTVIEW_LButtonDblClk(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11413 case WM_LBUTTONDOWN:
11414 return LISTVIEW_LButtonDown(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11417 return LISTVIEW_LButtonUp(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11420 return LISTVIEW_MouseMove (infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11422 case WM_MOUSEHOVER:
11423 return LISTVIEW_MouseHover(infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11426 return LISTVIEW_NCDestroy(infoPtr);
11429 return LISTVIEW_NCPaint(infoPtr, (HRGN)wParam);
11432 if (lParam && ((LPNMHDR)lParam)->hwndFrom == infoPtr->hwndHeader)
11433 return LISTVIEW_HeaderNotification(infoPtr, (LPNMHEADERW)lParam);
11436 case WM_NOTIFYFORMAT:
11437 return LISTVIEW_NotifyFormat(infoPtr, (HWND)wParam, (INT)lParam);
11439 case WM_PRINTCLIENT:
11440 return LISTVIEW_PrintClient(infoPtr, (HDC)wParam, (DWORD)lParam);
11443 return LISTVIEW_WMPaint(infoPtr, (HDC)wParam);
11445 case WM_RBUTTONDBLCLK:
11446 return LISTVIEW_RButtonDblClk(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11448 case WM_RBUTTONDOWN:
11449 return LISTVIEW_RButtonDown(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11452 return LISTVIEW_RButtonUp(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11455 return LISTVIEW_SetCursor(infoPtr, wParam, lParam);
11458 return LISTVIEW_SetFocus(infoPtr, (HWND)wParam);
11461 return LISTVIEW_SetFont(infoPtr, (HFONT)wParam, (WORD)lParam);
11464 return LISTVIEW_SetRedraw(infoPtr, (BOOL)wParam);
11466 case WM_SHOWWINDOW:
11467 return LISTVIEW_ShowWindow(infoPtr, wParam, lParam);
11470 return LISTVIEW_Size(infoPtr, (short)LOWORD(lParam), (short)HIWORD(lParam));
11472 case WM_STYLECHANGED:
11473 return LISTVIEW_StyleChanged(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
11475 case WM_STYLECHANGING:
11476 return LISTVIEW_StyleChanging(wParam, (LPSTYLESTRUCT)lParam);
11478 case WM_SYSCOLORCHANGE:
11479 COMCTL32_RefreshSysColors();
11482 /* case WM_TIMER: */
11483 case WM_THEMECHANGED:
11484 return LISTVIEW_ThemeChanged(infoPtr);
11487 return LISTVIEW_VScroll(infoPtr, (INT)LOWORD(wParam), 0);
11489 case WM_MOUSEWHEEL:
11490 if (wParam & (MK_SHIFT | MK_CONTROL))
11491 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
11492 return LISTVIEW_MouseWheel(infoPtr, (short int)HIWORD(wParam));
11494 case WM_WINDOWPOSCHANGED:
11495 if (!(((WINDOWPOS *)lParam)->flags & SWP_NOSIZE))
11497 SetWindowPos(infoPtr->hwndSelf, 0, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOACTIVATE |
11498 SWP_NOZORDER | SWP_NOMOVE | SWP_NOSIZE);
11500 if ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (infoPtr->uView == LV_VIEW_DETAILS))
11502 if (notify_measureitem(infoPtr)) LISTVIEW_InvalidateList(infoPtr);
11505 LISTVIEW_UpdateSize(infoPtr);
11506 LISTVIEW_UpdateScroll(infoPtr);
11508 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
11510 /* case WM_WININICHANGE: */
11513 if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
11514 ERR("unknown msg %04x wp=%08lx lp=%08lx\n", uMsg, wParam, lParam);
11516 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
11523 * Registers the window class.
11531 void LISTVIEW_Register(void)
11533 WNDCLASSW wndClass;
11535 ZeroMemory(&wndClass, sizeof(WNDCLASSW));
11536 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS;
11537 wndClass.lpfnWndProc = LISTVIEW_WindowProc;
11538 wndClass.cbClsExtra = 0;
11539 wndClass.cbWndExtra = sizeof(LISTVIEW_INFO *);
11540 wndClass.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
11541 wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
11542 wndClass.lpszClassName = WC_LISTVIEWW;
11543 RegisterClassW(&wndClass);
11548 * Unregisters the window class.
11556 void LISTVIEW_Unregister(void)
11558 UnregisterClassW(WC_LISTVIEWW, NULL);
11563 * Handle any WM_COMMAND messages
11566 * [I] infoPtr : valid pointer to the listview structure
11567 * [I] wParam : the first message parameter
11568 * [I] lParam : the second message parameter
11573 static LRESULT LISTVIEW_Command(LISTVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
11576 TRACE("(%p %x %x %lx)\n", infoPtr, HIWORD(wParam), LOWORD(wParam), lParam);
11578 if (!infoPtr->hwndEdit) return 0;
11580 switch (HIWORD(wParam))
11585 * Adjust the edit window size
11587 WCHAR buffer[1024];
11588 HDC hdc = GetDC(infoPtr->hwndEdit);
11589 HFONT hFont, hOldFont = 0;
11593 if (!infoPtr->hwndEdit || !hdc) return 0;
11594 GetWindowTextW(infoPtr->hwndEdit, buffer, sizeof(buffer)/sizeof(buffer[0]));
11595 GetWindowRect(infoPtr->hwndEdit, &rect);
11597 /* Select font to get the right dimension of the string */
11598 hFont = (HFONT)SendMessageW(infoPtr->hwndEdit, WM_GETFONT, 0, 0);
11601 hOldFont = SelectObject(hdc, hFont);
11604 if (GetTextExtentPoint32W(hdc, buffer, lstrlenW(buffer), &sz))
11606 TEXTMETRICW textMetric;
11608 /* Add Extra spacing for the next character */
11609 GetTextMetricsW(hdc, &textMetric);
11610 sz.cx += (textMetric.tmMaxCharWidth * 2);
11612 SetWindowPos(infoPtr->hwndEdit, NULL, 0, 0, sz.cx,
11613 rect.bottom - rect.top, SWP_DRAWFRAME | SWP_NOMOVE | SWP_NOZORDER);
11616 SelectObject(hdc, hOldFont);
11618 ReleaseDC(infoPtr->hwndEdit, hdc);
11624 LISTVIEW_CancelEditLabel(infoPtr);
11629 return SendMessageW (infoPtr->hwndNotify, WM_COMMAND, wParam, lParam);