4 * Copyright 1998, 1999 Eric Kohl
5 * Copyright 1999 Luc Tourangeau
6 * Copyright 2000 Jason Mawdsley
7 * Copyright 2001 CodeWeavers Inc.
8 * Copyright 2002 Dimitrie O. Paun
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 * This code was audited for completeness against the documented features
27 * of Comctl32.dll version 6.0 on May. 20, 2005, by James Hawkins.
29 * Unless otherwise noted, we believe this code to be complete, as per
30 * the specification mentioned above.
31 * If you discover missing features, or bugs, please note them below.
35 * Default Message Processing
36 * -- EN_KILLFOCUS should be handled in WM_COMMAND
37 * -- WM_CREATE: create the icon and small icon image lists at this point only if
38 * the LVS_SHAREIMAGELISTS style is not specified.
39 * -- WM_ERASEBKGND: forward this message to the parent window if the bkgnd
41 * -- WM_WINDOWPOSCHANGED: arrange the list items if the current view is icon
42 * or small icon and the LVS_AUTOARRANGE style is specified.
47 * -- Hot item handling, mouse hovering
48 * -- Workareas support
53 * -- Expand large item in ICON mode when the cursor is flying over the icon or text.
54 * -- Support CustomDraw options for _WIN32_IE >= 0x560 (see NMLVCUSTOMDRAW docs).
55 * -- LVA_SNAPTOGRID not implemented
56 * -- LISTVIEW_ApproximateViewRect partially implemented
57 * -- LISTVIEW_[GS]etColumnOrderArray stubs
58 * -- LISTVIEW_SetColumnWidth ignores header images & bitmap
59 * -- LISTVIEW_SetIconSpacing is incomplete
60 * -- LISTVIEW_SortItems is broken
61 * -- LISTVIEW_StyleChanged doesn't handle some changes too well
64 * -- LISTVIEW_GetNextItem needs to be rewritten. It is currently
65 * linear in the number of items in the list, and this is
66 * unacceptable for large lists.
67 * -- in sorted mode, LISTVIEW_InsertItemT sorts the array,
68 * instead of inserting in the right spot
69 * -- we should keep an ordered array of coordinates in iconic mode
70 * this would allow to frame items (iterator_frameditems),
71 * and find nearest item (LVFI_NEARESTXY) a lot more efficiently
79 * -- LVIS_ACTIVATING (not currently supported by comctl32.dll version 6.0)
86 * -- LVS_NOSCROLL (see Q137520)
87 * -- LVS_SORTASCENDING, LVS_SORTDESCENDING
89 * -- LVS_TYPESTYLEMASK
92 * -- LVS_EX_BORDERSELECT
95 * -- LVS_EX_HEADERDRAGDROP
98 * -- LVS_EX_MULTIWORKAREAS
99 * -- LVS_EX_ONECLICKACTIVATE
101 * -- LVS_EX_SIMPLESELECT
102 * -- LVS_EX_TRACKSELECT
103 * -- LVS_EX_TWOCLICKACTIVATE
104 * -- LVS_EX_UNDERLINECOLD
105 * -- LVS_EX_UNDERLINEHOT
108 * -- LVN_BEGINSCROLL, LVN_ENDSCROLL
111 * -- LVN_MARQUEEBEGIN
118 * -- LVM_CANCELEDITLABEL
119 * -- LVM_ENABLEGROUPVIEW
120 * -- LVM_GETBKIMAGE, LVM_SETBKIMAGE
121 * -- LVM_GETGROUPINFO, LVM_SETGROUPINFO
122 * -- LVM_GETGROUPMETRICS, LVM_SETGROUPMETRICS
123 * -- LVM_GETINSERTMARK, LVM_SETINSERTMARK
124 * -- LVM_GETINSERTMARKCOLOR, LVM_SETINSERTMARKCOLOR
125 * -- LVM_GETINSERTMARKRECT
126 * -- LVM_GETNUMBEROFWORKAREAS
127 * -- LVM_GETOUTLINECOLOR, LVM_SETOUTLINECOLOR
128 * -- LVM_GETSELECTEDCOLUMN, LVM_SETSELECTEDCOLUMN
129 * -- LVM_GETISEARCHSTRINGW, LVM_GETISEARCHSTRINGA
130 * -- LVM_GETTILEINFO, LVM_SETTILEINFO
131 * -- LVM_GETTILEVIEWINFO, LVM_SETTILEVIEWINFO
132 * -- LVM_GETUNICODEFORMAT, LVM_SETUNICODEFORMAT
133 * -- LVM_GETVIEW, LVM_SETVIEW
134 * -- LVM_GETWORKAREAS, LVM_SETWORKAREAS
135 * -- LVM_HASGROUP, LVM_INSERTGROUP, LVM_REMOVEGROUP, LVM_REMOVEALLGROUPS
136 * -- LVM_INSERTGROUPSORTED
137 * -- LVM_INSERTMARKHITTEST
138 * -- LVM_ISGROUPVIEWENABLED
139 * -- LVM_MAPIDTOINDEX, LVM_MAPINDEXTOID
141 * -- LVM_MOVEITEMTOGROUP
143 * -- LVM_SETTILEWIDTH
148 * -- ListView_GetCheckSate, ListView_SetCheckState
149 * -- ListView_GetHoverTime, ListView_SetHoverTime
150 * -- ListView_GetISearchString
151 * -- ListView_GetNumberOfWorkAreas
152 * -- ListView_GetOrigin
153 * -- ListView_GetTextBkColor
154 * -- ListView_GetUnicodeFormat, ListView_SetUnicodeFormat
155 * -- ListView_GetWorkAreas, ListView_SetWorkAreas
156 * -- ListView_SortItemsEx
161 * Known differences in message stream from native control (not known if
162 * these differences cause problems):
163 * LVM_INSERTITEM issues LVM_SETITEMSTATE and LVM_SETITEM in certain cases.
164 * LVM_SETITEM does not always issue LVN_ITEMCHANGING/LVN_ITEMCHANGED.
165 * WM_CREATE does not issue WM_QUERYUISTATE and associated registry
166 * processing for "USEDOUBLECLICKTIME".
170 #include "wine/port.h"
185 #include "commctrl.h"
186 #include "comctl32.h"
189 #include "wine/debug.h"
190 #include "wine/unicode.h"
192 WINE_DEFAULT_DEBUG_CHANNEL(listview);
194 /* make sure you set this to 0 for production use! */
195 #define DEBUG_RANGES 1
197 typedef struct tagCOLUMN_INFO
199 RECT rcHeader; /* tracks the header's rectangle */
200 int fmt; /* same as LVCOLUMN.fmt */
203 typedef struct tagITEMHDR
207 } ITEMHDR, *LPITEMHDR;
209 typedef struct tagSUBITEM_INFO
215 typedef struct tagITEM_INFO
223 typedef struct tagRANGE
229 typedef struct tagRANGES
234 typedef struct tagITERATOR
243 typedef struct tagLISTVIEW_INFO
250 COLORREF clrTextBkDefault;
251 HIMAGELIST himlNormal;
252 HIMAGELIST himlSmall;
253 HIMAGELIST himlState;
256 POINT ptClickPos; /* point where the user clicked */
257 BOOL bNoItemMetrics; /* flags if item metrics are not yet computed */
260 RANGES selectionRanges;
265 RECT rcList; /* This rectangle is really the window
266 * client rectangle possibly reduced by the
267 * horizontal scroll bar and/or header - see
268 * LISTVIEW_UpdateSize. This rectangle offset
269 * by the LISTVIEW_GetOrigin value is in
270 * client coordinates */
279 INT ntmHeight; /* Some cached metrics of the font used */
280 INT ntmMaxCharWidth; /* by the listview to draw items */
282 BOOL bRedraw; /* Turns on/off repaints & invalidations */
283 BOOL bAutoarrange; /* Autoarrange flag when NOT in LVS_AUTOARRANGE */
285 BOOL bDoChangeNotify; /* send change notification messages? */
288 DWORD dwStyle; /* the cached window GWL_STYLE */
289 DWORD dwLvExStyle; /* extended listview style */
290 INT nItemCount; /* the number of items in the list */
291 HDPA hdpaItems; /* array ITEM_INFO pointers */
292 HDPA hdpaPosX; /* maintains the (X, Y) coordinates of the */
293 HDPA hdpaPosY; /* items in LVS_ICON, and LVS_SMALLICON modes */
294 HDPA hdpaColumns; /* array of COLUMN_INFO pointers */
295 POINT currIconPos; /* this is the position next icon will be placed */
296 PFNLVCOMPARE pfnCompare;
304 DWORD cditemmode; /* Keep the custom draw flags for an item/row */
306 DWORD lastKeyPressTimestamp;
308 INT nSearchParamLength;
309 WCHAR szSearchParam[ MAX_PATH ];
311 INT nMeasureItemHeight;
317 /* How many we debug buffer to allocate */
318 #define DEBUG_BUFFERS 20
319 /* The size of a single debug bbuffer */
320 #define DEBUG_BUFFER_SIZE 256
322 /* Internal interface to LISTVIEW_HScroll and LISTVIEW_VScroll */
323 #define SB_INTERNAL -1
325 /* maximum size of a label */
326 #define DISP_TEXT_SIZE 512
328 /* padding for items in list and small icon display modes */
329 #define WIDTH_PADDING 12
331 /* padding for items in list, report and small icon display modes */
332 #define HEIGHT_PADDING 1
334 /* offset of items in report display mode */
335 #define REPORT_MARGINX 2
337 /* padding for icon in large icon display mode
338 * ICON_TOP_PADDING_NOTHITABLE - space between top of box and area
339 * that HITTEST will see.
340 * ICON_TOP_PADDING_HITABLE - spacing between above and icon.
341 * ICON_TOP_PADDING - sum of the two above.
342 * ICON_BOTTOM_PADDING - between bottom of icon and top of text
343 * LABEL_HOR_PADDING - between text and sides of box
344 * LABEL_VERT_PADDING - between bottom of text and end of box
346 * ICON_LR_PADDING - additional width above icon size.
347 * ICON_LR_HALF - half of the above value
349 #define ICON_TOP_PADDING_NOTHITABLE 2
350 #define ICON_TOP_PADDING_HITABLE 2
351 #define ICON_TOP_PADDING (ICON_TOP_PADDING_NOTHITABLE + ICON_TOP_PADDING_HITABLE)
352 #define ICON_BOTTOM_PADDING 4
353 #define LABEL_HOR_PADDING 5
354 #define LABEL_VERT_PADDING 7
355 #define ICON_LR_PADDING 16
356 #define ICON_LR_HALF (ICON_LR_PADDING/2)
358 /* default label width for items in list and small icon display modes */
359 #define DEFAULT_LABEL_WIDTH 40
361 /* default column width for items in list display mode */
362 #define DEFAULT_COLUMN_WIDTH 128
364 /* Size of "line" scroll for V & H scrolls */
365 #define LISTVIEW_SCROLL_ICON_LINE_SIZE 37
367 /* Padding betwen image and label */
368 #define IMAGE_PADDING 2
370 /* Padding behind the label */
371 #define TRAILING_LABEL_PADDING 12
372 #define TRAILING_HEADER_PADDING 11
374 /* Border for the icon caption */
375 #define CAPTION_BORDER 2
377 /* Standard DrawText flags */
378 #define LV_ML_DT_FLAGS (DT_TOP | DT_NOPREFIX | DT_EDITCONTROL | DT_CENTER | DT_WORDBREAK | DT_WORD_ELLIPSIS | DT_END_ELLIPSIS)
379 #define LV_FL_DT_FLAGS (DT_TOP | DT_NOPREFIX | DT_EDITCONTROL | DT_CENTER | DT_WORDBREAK | DT_NOCLIP)
380 #define LV_SL_DT_FLAGS (DT_VCENTER | DT_NOPREFIX | DT_EDITCONTROL | DT_SINGLELINE | DT_WORD_ELLIPSIS | DT_END_ELLIPSIS)
382 /* Image index from state */
383 #define STATEIMAGEINDEX(x) (((x) & LVIS_STATEIMAGEMASK) >> 12)
385 /* The time in milliseconds to reset the search in the list */
386 #define KEY_DELAY 450
388 /* Dump the LISTVIEW_INFO structure to the debug channel */
389 #define LISTVIEW_DUMP(iP) do { \
390 TRACE("hwndSelf=%p, clrBk=0x%06lx, clrText=0x%06lx, clrTextBk=0x%06lx, ItemHeight=%d, ItemWidth=%d, Style=0x%08lx\n", \
391 iP->hwndSelf, iP->clrBk, iP->clrText, iP->clrTextBk, \
392 iP->nItemHeight, iP->nItemWidth, iP->dwStyle); \
393 TRACE("hwndSelf=%p, himlNor=%p, himlSml=%p, himlState=%p, Focused=%d, Hot=%d, exStyle=0x%08lx, Focus=%d\n", \
394 iP->hwndSelf, iP->himlNormal, iP->himlSmall, iP->himlState, \
395 iP->nFocusedItem, iP->nHotItem, iP->dwLvExStyle, iP->bFocus ); \
396 TRACE("hwndSelf=%p, ntmH=%d, icSz.cx=%ld, icSz.cy=%ld, icSp.cx=%ld, icSp.cy=%ld, notifyFmt=%d\n", \
397 iP->hwndSelf, iP->ntmHeight, iP->iconSize.cx, iP->iconSize.cy, \
398 iP->iconSpacing.cx, iP->iconSpacing.cy, iP->notifyFormat); \
399 TRACE("hwndSelf=%p, rcList=%s\n", iP->hwndSelf, debugrect(&iP->rcList)); \
402 static const WCHAR themeClass[] = {'L','i','s','t','V','i','e','w',0};
405 * forward declarations
407 static BOOL LISTVIEW_GetItemT(LISTVIEW_INFO *, LPLVITEMW, BOOL);
408 static void LISTVIEW_GetItemBox(LISTVIEW_INFO *, INT, LPRECT);
409 static void LISTVIEW_GetItemOrigin(LISTVIEW_INFO *, INT, LPPOINT);
410 static BOOL LISTVIEW_GetItemPosition(LISTVIEW_INFO *, INT, LPPOINT);
411 static BOOL LISTVIEW_GetItemRect(LISTVIEW_INFO *, INT, LPRECT);
412 static INT LISTVIEW_GetLabelWidth(LISTVIEW_INFO *, INT);
413 static void LISTVIEW_GetOrigin(LISTVIEW_INFO *, LPPOINT);
414 static BOOL LISTVIEW_GetViewRect(LISTVIEW_INFO *, LPRECT);
415 static void LISTVIEW_SetGroupSelection(LISTVIEW_INFO *, INT);
416 static BOOL LISTVIEW_SetItemT(LISTVIEW_INFO *, const LVITEMW *, BOOL);
417 static void LISTVIEW_UpdateScroll(LISTVIEW_INFO *);
418 static void LISTVIEW_SetSelection(LISTVIEW_INFO *, INT);
419 static void LISTVIEW_UpdateSize(LISTVIEW_INFO *);
420 static HWND LISTVIEW_EditLabelT(LISTVIEW_INFO *, INT, BOOL);
421 static LRESULT LISTVIEW_Command(LISTVIEW_INFO *, WPARAM, LPARAM);
422 static BOOL LISTVIEW_SortItems(LISTVIEW_INFO *, PFNLVCOMPARE, LPARAM);
423 static INT LISTVIEW_GetStringWidthT(LISTVIEW_INFO *, LPCWSTR, BOOL);
424 static BOOL LISTVIEW_KeySelection(LISTVIEW_INFO *, INT);
425 static UINT LISTVIEW_GetItemState(LISTVIEW_INFO *, INT, UINT);
426 static BOOL LISTVIEW_SetItemState(LISTVIEW_INFO *, INT, const LVITEMW *);
427 static LRESULT LISTVIEW_VScroll(LISTVIEW_INFO *, INT, INT, HWND);
428 static LRESULT LISTVIEW_HScroll(LISTVIEW_INFO *, INT, INT, HWND);
429 static INT LISTVIEW_GetTopIndex(LISTVIEW_INFO *);
430 static BOOL LISTVIEW_EnsureVisible(LISTVIEW_INFO *, INT, BOOL);
431 static HWND CreateEditLabelT(LISTVIEW_INFO *, LPCWSTR, DWORD, INT, INT, INT, INT, BOOL);
432 static HIMAGELIST LISTVIEW_SetImageList(LISTVIEW_INFO *, INT, HIMAGELIST);
433 static INT LISTVIEW_HitTest(LISTVIEW_INFO *, LPLVHITTESTINFO, BOOL, BOOL);
435 /******** Text handling functions *************************************/
437 /* A text pointer is either NULL, LPSTR_TEXTCALLBACK, or points to a
438 * text string. The string may be ANSI or Unicode, in which case
439 * the boolean isW tells us the type of the string.
441 * The name of the function tell what type of strings it expects:
442 * W: Unicode, T: ANSI/Unicode - function of isW
445 static inline BOOL is_textW(LPCWSTR text)
447 return text != NULL && text != LPSTR_TEXTCALLBACKW;
450 static inline BOOL is_textT(LPCWSTR text, BOOL isW)
452 /* we can ignore isW since LPSTR_TEXTCALLBACKW == LPSTR_TEXTCALLBACKA */
453 return is_textW(text);
456 static inline int textlenT(LPCWSTR text, BOOL isW)
458 return !is_textT(text, isW) ? 0 :
459 isW ? lstrlenW(text) : lstrlenA((LPCSTR)text);
462 static inline void textcpynT(LPWSTR dest, BOOL isDestW, LPCWSTR src, BOOL isSrcW, INT max)
465 if (isSrcW) lstrcpynW(dest, src, max);
466 else MultiByteToWideChar(CP_ACP, 0, (LPCSTR)src, -1, dest, max);
468 if (isSrcW) WideCharToMultiByte(CP_ACP, 0, src, -1, (LPSTR)dest, max, NULL, NULL);
469 else lstrcpynA((LPSTR)dest, (LPCSTR)src, max);
472 static inline LPWSTR textdupTtoW(LPCWSTR text, BOOL isW)
474 LPWSTR wstr = (LPWSTR)text;
476 if (!isW && is_textT(text, isW))
478 INT len = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)text, -1, NULL, 0);
479 wstr = Alloc(len * sizeof(WCHAR));
480 if (wstr) MultiByteToWideChar(CP_ACP, 0, (LPCSTR)text, -1, wstr, len);
482 TRACE(" wstr=%s\n", text == LPSTR_TEXTCALLBACKW ? "(callback)" : debugstr_w(wstr));
486 static inline void textfreeT(LPWSTR wstr, BOOL isW)
488 if (!isW && is_textT(wstr, isW)) Free (wstr);
492 * dest is a pointer to a Unicode string
493 * src is a pointer to a string (Unicode if isW, ANSI if !isW)
495 static BOOL textsetptrT(LPWSTR *dest, LPWSTR src, BOOL isW)
499 if (src == LPSTR_TEXTCALLBACKW)
501 if (is_textW(*dest)) Free(*dest);
502 *dest = LPSTR_TEXTCALLBACKW;
506 LPWSTR pszText = textdupTtoW(src, isW);
507 if (*dest == LPSTR_TEXTCALLBACKW) *dest = NULL;
508 bResult = Str_SetPtrW(dest, pszText);
509 textfreeT(pszText, isW);
515 * compares a Unicode to a Unicode/ANSI text string
517 static inline int textcmpWT(LPCWSTR aw, LPCWSTR bt, BOOL isW)
519 if (!aw) return bt ? -1 : 0;
520 if (!bt) return aw ? 1 : 0;
521 if (aw == LPSTR_TEXTCALLBACKW)
522 return bt == LPSTR_TEXTCALLBACKW ? 0 : -1;
523 if (bt != LPSTR_TEXTCALLBACKW)
525 LPWSTR bw = textdupTtoW(bt, isW);
526 int r = bw ? lstrcmpW(aw, bw) : 1;
534 static inline int lstrncmpiW(LPCWSTR s1, LPCWSTR s2, int n)
538 n = min(min(n, strlenW(s1)), strlenW(s2));
539 res = CompareStringW(LOCALE_USER_DEFAULT, NORM_IGNORECASE, s1, n, s2, n);
540 return res ? res - sizeof(WCHAR) : res;
543 /******** Debugging functions *****************************************/
545 static inline LPCSTR debugtext_t(LPCWSTR text, BOOL isW)
547 if (text == LPSTR_TEXTCALLBACKW) return "(callback)";
548 return isW ? debugstr_w(text) : debugstr_a((LPCSTR)text);
551 static inline LPCSTR debugtext_tn(LPCWSTR text, BOOL isW, INT n)
553 if (text == LPSTR_TEXTCALLBACKW) return "(callback)";
554 n = min(textlenT(text, isW), n);
555 return isW ? debugstr_wn(text, n) : debugstr_an((LPCSTR)text, n);
558 static char* debug_getbuf(void)
560 static int index = 0;
561 static char buffers[DEBUG_BUFFERS][DEBUG_BUFFER_SIZE];
562 return buffers[index++ % DEBUG_BUFFERS];
565 static inline const char* debugrange(const RANGE *lprng)
569 char* buf = debug_getbuf();
570 snprintf(buf, DEBUG_BUFFER_SIZE, "[%d, %d)", lprng->lower, lprng->upper);
572 } else return "(null)";
575 static inline const char* debugpoint(const POINT *lppt)
579 char* buf = debug_getbuf();
580 snprintf(buf, DEBUG_BUFFER_SIZE, "(%ld, %ld)", lppt->x, lppt->y);
582 } else return "(null)";
585 static inline const char* debugrect(const RECT *rect)
589 char* buf = debug_getbuf();
590 snprintf(buf, DEBUG_BUFFER_SIZE, "[(%ld, %ld);(%ld, %ld)]",
591 rect->left, rect->top, rect->right, rect->bottom);
593 } else return "(null)";
596 static const char* debugscrollinfo(const SCROLLINFO *pScrollInfo)
598 char* buf = debug_getbuf(), *text = buf;
599 int len, size = DEBUG_BUFFER_SIZE;
601 if (pScrollInfo == NULL) return "(null)";
602 len = snprintf(buf, size, "{cbSize=%d, ", pScrollInfo->cbSize);
603 if (len == -1) goto end; buf += len; size -= len;
604 if (pScrollInfo->fMask & SIF_RANGE)
605 len = snprintf(buf, size, "nMin=%d, nMax=%d, ", pScrollInfo->nMin, pScrollInfo->nMax);
607 if (len == -1) goto end; buf += len; size -= len;
608 if (pScrollInfo->fMask & SIF_PAGE)
609 len = snprintf(buf, size, "nPage=%u, ", pScrollInfo->nPage);
611 if (len == -1) goto end; buf += len; size -= len;
612 if (pScrollInfo->fMask & SIF_POS)
613 len = snprintf(buf, size, "nPos=%d, ", pScrollInfo->nPos);
615 if (len == -1) goto end; buf += len; size -= len;
616 if (pScrollInfo->fMask & SIF_TRACKPOS)
617 len = snprintf(buf, size, "nTrackPos=%d, ", pScrollInfo->nTrackPos);
619 if (len == -1) goto end; buf += len; size -= len;
622 buf = text + strlen(text);
624 if (buf - text > 2) { buf[-2] = '}'; buf[-1] = 0; }
628 static const char* debugnmlistview(const NMLISTVIEW *plvnm)
632 char* buf = debug_getbuf();
633 snprintf(buf, DEBUG_BUFFER_SIZE, "iItem=%d, iSubItem=%d, uNewState=0x%x,"
634 " uOldState=0x%x, uChanged=0x%x, ptAction=%s, lParam=%ld\n",
635 plvnm->iItem, plvnm->iSubItem, plvnm->uNewState, plvnm->uOldState,
636 plvnm->uChanged, debugpoint(&plvnm->ptAction), plvnm->lParam);
638 } else return "(null)";
641 static const char* debuglvitem_t(const LVITEMW *lpLVItem, BOOL isW)
643 char* buf = debug_getbuf(), *text = buf;
644 int len, size = DEBUG_BUFFER_SIZE;
646 if (lpLVItem == NULL) return "(null)";
647 len = snprintf(buf, size, "{iItem=%d, iSubItem=%d, ", lpLVItem->iItem, lpLVItem->iSubItem);
648 if (len == -1) goto end; buf += len; size -= len;
649 if (lpLVItem->mask & LVIF_STATE)
650 len = snprintf(buf, size, "state=%x, stateMask=%x, ", lpLVItem->state, lpLVItem->stateMask);
652 if (len == -1) goto end; buf += len; size -= len;
653 if (lpLVItem->mask & LVIF_TEXT)
654 len = snprintf(buf, size, "pszText=%s, cchTextMax=%d, ", debugtext_tn(lpLVItem->pszText, isW, 80), lpLVItem->cchTextMax);
656 if (len == -1) goto end; buf += len; size -= len;
657 if (lpLVItem->mask & LVIF_IMAGE)
658 len = snprintf(buf, size, "iImage=%d, ", lpLVItem->iImage);
660 if (len == -1) goto end; buf += len; size -= len;
661 if (lpLVItem->mask & LVIF_PARAM)
662 len = snprintf(buf, size, "lParam=%lx, ", lpLVItem->lParam);
664 if (len == -1) goto end; buf += len; size -= len;
665 if (lpLVItem->mask & LVIF_INDENT)
666 len = snprintf(buf, size, "iIndent=%d, ", lpLVItem->iIndent);
668 if (len == -1) goto end; buf += len; size -= len;
671 buf = text + strlen(text);
673 if (buf - text > 2) { buf[-2] = '}'; buf[-1] = 0; }
677 static const char* debuglvcolumn_t(const LVCOLUMNW *lpColumn, BOOL isW)
679 char* buf = debug_getbuf(), *text = buf;
680 int len, size = DEBUG_BUFFER_SIZE;
682 if (lpColumn == NULL) return "(null)";
683 len = snprintf(buf, size, "{");
684 if (len == -1) goto end; buf += len; size -= len;
685 if (lpColumn->mask & LVCF_SUBITEM)
686 len = snprintf(buf, size, "iSubItem=%d, ", lpColumn->iSubItem);
688 if (len == -1) goto end; buf += len; size -= len;
689 if (lpColumn->mask & LVCF_FMT)
690 len = snprintf(buf, size, "fmt=%x, ", lpColumn->fmt);
692 if (len == -1) goto end; buf += len; size -= len;
693 if (lpColumn->mask & LVCF_WIDTH)
694 len = snprintf(buf, size, "cx=%d, ", lpColumn->cx);
696 if (len == -1) goto end; buf += len; size -= len;
697 if (lpColumn->mask & LVCF_TEXT)
698 len = snprintf(buf, size, "pszText=%s, cchTextMax=%d, ", debugtext_tn(lpColumn->pszText, isW, 80), lpColumn->cchTextMax);
700 if (len == -1) goto end; buf += len; size -= len;
701 if (lpColumn->mask & LVCF_IMAGE)
702 len = snprintf(buf, size, "iImage=%d, ", lpColumn->iImage);
704 if (len == -1) goto end; buf += len; size -= len;
705 if (lpColumn->mask & LVCF_ORDER)
706 len = snprintf(buf, size, "iOrder=%d, ", lpColumn->iOrder);
708 if (len == -1) goto end; buf += len; size -= len;
711 buf = text + strlen(text);
713 if (buf - text > 2) { buf[-2] = '}'; buf[-1] = 0; }
717 static const char* debuglvhittestinfo(const LVHITTESTINFO *lpht)
721 char* buf = debug_getbuf();
722 snprintf(buf, DEBUG_BUFFER_SIZE, "{pt=%s, flags=0x%x, iItem=%d, iSubItem=%d}",
723 debugpoint(&lpht->pt), lpht->flags, lpht->iItem, lpht->iSubItem);
725 } else return "(null)";
728 /* Return the corresponding text for a given scroll value */
729 static inline LPCSTR debugscrollcode(int nScrollCode)
733 case SB_LINELEFT: return "SB_LINELEFT";
734 case SB_LINERIGHT: return "SB_LINERIGHT";
735 case SB_PAGELEFT: return "SB_PAGELEFT";
736 case SB_PAGERIGHT: return "SB_PAGERIGHT";
737 case SB_THUMBPOSITION: return "SB_THUMBPOSITION";
738 case SB_THUMBTRACK: return "SB_THUMBTRACK";
739 case SB_ENDSCROLL: return "SB_ENDSCROLL";
740 case SB_INTERNAL: return "SB_INTERNAL";
741 default: return "unknown";
746 /******** Notification functions i************************************/
748 static LRESULT notify_forward_header(LISTVIEW_INFO *infoPtr, const NMHEADERW *lpnmh)
750 return SendMessageW(infoPtr->hwndNotify, WM_NOTIFY,
751 (WPARAM)lpnmh->hdr.idFrom, (LPARAM)lpnmh);
754 static LRESULT notify_hdr(LISTVIEW_INFO *infoPtr, INT code, LPNMHDR pnmh)
758 TRACE("(code=%d)\n", code);
760 pnmh->hwndFrom = infoPtr->hwndSelf;
761 pnmh->idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
763 result = SendMessageW(infoPtr->hwndNotify, WM_NOTIFY,
764 (WPARAM)pnmh->idFrom, (LPARAM)pnmh);
766 TRACE(" <= %ld\n", result);
771 static inline BOOL notify(LISTVIEW_INFO *infoPtr, INT code)
774 HWND hwnd = infoPtr->hwndSelf;
775 notify_hdr(infoPtr, code, &nmh);
776 return IsWindow(hwnd);
779 static inline void notify_itemactivate(LISTVIEW_INFO *infoPtr, LVHITTESTINFO *htInfo)
790 item.mask = LVIF_PARAM|LVIF_STATE;
791 item.iItem = htInfo->iItem;
793 if (LISTVIEW_GetItemT(infoPtr, &item, TRUE)) {
794 nmia.lParam = item.lParam;
795 nmia.uOldState = item.state;
796 nmia.uNewState = item.state | LVIS_ACTIVATING;
797 nmia.uChanged = LVIF_STATE;
800 nmia.iItem = htInfo->iItem;
801 nmia.iSubItem = htInfo->iSubItem;
802 nmia.ptAction = htInfo->pt;
804 if (GetKeyState(VK_SHIFT) & 0x8000) nmia.uKeyFlags |= LVKF_SHIFT;
805 if (GetKeyState(VK_CONTROL) & 0x8000) nmia.uKeyFlags |= LVKF_CONTROL;
806 if (GetKeyState(VK_MENU) & 0x8000) nmia.uKeyFlags |= LVKF_ALT;
808 notify_hdr(infoPtr, LVN_ITEMACTIVATE, (LPNMHDR)&nmia);
811 static inline LRESULT notify_listview(LISTVIEW_INFO *infoPtr, INT code, LPNMLISTVIEW plvnm)
813 TRACE("(code=%d, plvnm=%s)\n", code, debugnmlistview(plvnm));
814 return notify_hdr(infoPtr, code, (LPNMHDR)plvnm);
817 static BOOL notify_click(LISTVIEW_INFO *infoPtr, INT code, LVHITTESTINFO *lvht)
821 HWND hwnd = infoPtr->hwndSelf;
823 TRACE("code=%d, lvht=%s\n", code, debuglvhittestinfo(lvht));
824 ZeroMemory(&nmlv, sizeof(nmlv));
825 nmlv.iItem = lvht->iItem;
826 nmlv.iSubItem = lvht->iSubItem;
827 nmlv.ptAction = lvht->pt;
828 item.mask = LVIF_PARAM;
829 item.iItem = lvht->iItem;
831 if (LISTVIEW_GetItemT(infoPtr, &item, TRUE)) nmlv.lParam = item.lParam;
832 notify_listview(infoPtr, code, &nmlv);
833 return IsWindow(hwnd);
836 static BOOL notify_deleteitem(LISTVIEW_INFO *infoPtr, INT nItem)
840 HWND hwnd = infoPtr->hwndSelf;
842 ZeroMemory(&nmlv, sizeof (NMLISTVIEW));
844 item.mask = LVIF_PARAM;
847 if (LISTVIEW_GetItemT(infoPtr, &item, TRUE)) nmlv.lParam = item.lParam;
848 notify_listview(infoPtr, LVN_DELETEITEM, &nmlv);
849 return IsWindow(hwnd);
852 static int get_ansi_notification(INT unicodeNotificationCode)
854 switch (unicodeNotificationCode)
856 case LVN_BEGINLABELEDITW: return LVN_BEGINLABELEDITA;
857 case LVN_ENDLABELEDITW: return LVN_ENDLABELEDITA;
858 case LVN_GETDISPINFOW: return LVN_GETDISPINFOA;
859 case LVN_SETDISPINFOW: return LVN_SETDISPINFOA;
860 case LVN_ODFINDITEMW: return LVN_ODFINDITEMA;
861 case LVN_GETINFOTIPW: return LVN_GETINFOTIPA;
863 ERR("unknown notification %x\n", unicodeNotificationCode);
869 Send notification. depends on dispinfoW having same
870 structure as dispinfoA.
871 infoPtr : listview struct
872 notificationCode : *Unicode* notification code
873 pdi : dispinfo structure (can be unicode or ansi)
874 isW : TRUE if dispinfo is Unicode
876 static BOOL notify_dispinfoT(LISTVIEW_INFO *infoPtr, INT notificationCode, LPNMLVDISPINFOW pdi, BOOL isW)
878 BOOL bResult = FALSE;
879 BOOL convertToAnsi = FALSE, convertToUnicode = FALSE;
880 INT cchTempBufMax = 0, savCchTextMax = 0, realNotifCode;
881 LPWSTR pszTempBuf = NULL, savPszText = NULL;
883 if ((pdi->item.mask & LVIF_TEXT) && is_textT(pdi->item.pszText, isW))
885 convertToAnsi = (isW && infoPtr->notifyFormat == NFR_ANSI);
886 convertToUnicode = (!isW && infoPtr->notifyFormat == NFR_UNICODE);
889 if (convertToAnsi || convertToUnicode)
891 if (notificationCode != LVN_GETDISPINFOW)
893 cchTempBufMax = convertToUnicode ?
894 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pdi->item.pszText, -1, NULL, 0):
895 WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, NULL, 0, NULL, NULL);
899 cchTempBufMax = pdi->item.cchTextMax;
900 *pdi->item.pszText = 0; /* make sure we don't process garbage */
903 pszTempBuf = Alloc( (convertToUnicode ? sizeof(WCHAR) : sizeof(CHAR)) * cchTempBufMax);
904 if (!pszTempBuf) return FALSE;
906 if (convertToUnicode)
907 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pdi->item.pszText, -1,
908 pszTempBuf, cchTempBufMax);
910 WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, (LPSTR) pszTempBuf,
911 cchTempBufMax, NULL, NULL);
913 savCchTextMax = pdi->item.cchTextMax;
914 savPszText = pdi->item.pszText;
915 pdi->item.pszText = pszTempBuf;
916 pdi->item.cchTextMax = cchTempBufMax;
919 if (infoPtr->notifyFormat == NFR_ANSI)
920 realNotifCode = get_ansi_notification(notificationCode);
922 realNotifCode = notificationCode;
923 TRACE(" pdi->item=%s\n", debuglvitem_t(&pdi->item, infoPtr->notifyFormat != NFR_ANSI));
924 bResult = notify_hdr(infoPtr, realNotifCode, &pdi->hdr);
926 if (convertToUnicode || convertToAnsi)
928 if (convertToUnicode) /* note : pointer can be changed by app ! */
929 WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, (LPSTR) savPszText,
930 savCchTextMax, NULL, NULL);
932 MultiByteToWideChar(CP_ACP, 0, (LPSTR) pdi->item.pszText, -1,
933 savPszText, savCchTextMax);
934 pdi->item.pszText = savPszText; /* restores our buffer */
935 pdi->item.cchTextMax = savCchTextMax;
941 static void customdraw_fill(NMLVCUSTOMDRAW *lpnmlvcd, LISTVIEW_INFO *infoPtr, HDC hdc,
942 const RECT *rcBounds, const LVITEMW *lplvItem)
944 ZeroMemory(lpnmlvcd, sizeof(NMLVCUSTOMDRAW));
945 lpnmlvcd->nmcd.hdc = hdc;
946 lpnmlvcd->nmcd.rc = *rcBounds;
947 lpnmlvcd->clrTextBk = infoPtr->clrTextBk;
948 lpnmlvcd->clrText = infoPtr->clrText;
949 if (!lplvItem) return;
950 lpnmlvcd->nmcd.dwItemSpec = lplvItem->iItem + 1;
951 lpnmlvcd->iSubItem = lplvItem->iSubItem;
952 if (lplvItem->state & LVIS_SELECTED) lpnmlvcd->nmcd.uItemState |= CDIS_SELECTED;
953 if (lplvItem->state & LVIS_FOCUSED) lpnmlvcd->nmcd.uItemState |= CDIS_FOCUS;
954 if (lplvItem->iItem == infoPtr->nHotItem) lpnmlvcd->nmcd.uItemState |= CDIS_HOT;
955 lpnmlvcd->nmcd.lItemlParam = lplvItem->lParam;
958 static inline DWORD notify_customdraw (LISTVIEW_INFO *infoPtr, DWORD dwDrawStage, NMLVCUSTOMDRAW *lpnmlvcd)
960 BOOL isForItem = (lpnmlvcd->nmcd.dwItemSpec != 0);
963 lpnmlvcd->nmcd.dwDrawStage = dwDrawStage;
964 if (isForItem) lpnmlvcd->nmcd.dwDrawStage |= CDDS_ITEM;
965 if (lpnmlvcd->iSubItem) lpnmlvcd->nmcd.dwDrawStage |= CDDS_SUBITEM;
966 if (isForItem) lpnmlvcd->nmcd.dwItemSpec--;
967 result = notify_hdr(infoPtr, NM_CUSTOMDRAW, &lpnmlvcd->nmcd.hdr);
968 if (isForItem) lpnmlvcd->nmcd.dwItemSpec++;
972 static void prepaint_setup (LISTVIEW_INFO *infoPtr, HDC hdc, NMLVCUSTOMDRAW *lpnmlvcd)
974 /* apprently, for selected items, we have to override the returned values */
975 if (lpnmlvcd->nmcd.uItemState & CDIS_SELECTED)
979 lpnmlvcd->clrTextBk = comctl32_color.clrHighlight;
980 lpnmlvcd->clrText = comctl32_color.clrHighlightText;
982 else if (infoPtr->dwStyle & LVS_SHOWSELALWAYS)
984 lpnmlvcd->clrTextBk = comctl32_color.clr3dFace;
985 lpnmlvcd->clrText = comctl32_color.clrBtnText;
989 /* Set the text attributes */
990 if (lpnmlvcd->clrTextBk != CLR_NONE)
992 SetBkMode(hdc, OPAQUE);
993 if (lpnmlvcd->clrTextBk == CLR_DEFAULT)
994 SetBkColor(hdc, infoPtr->clrTextBkDefault);
996 SetBkColor(hdc,lpnmlvcd->clrTextBk);
999 SetBkMode(hdc, TRANSPARENT);
1000 SetTextColor(hdc, lpnmlvcd->clrText);
1003 static inline DWORD notify_postpaint (LISTVIEW_INFO *infoPtr, NMLVCUSTOMDRAW *lpnmlvcd)
1005 return notify_customdraw(infoPtr, CDDS_POSTPAINT, lpnmlvcd);
1008 /******** Item iterator functions **********************************/
1010 static RANGES ranges_create(int count);
1011 static void ranges_destroy(RANGES ranges);
1012 static BOOL ranges_add(RANGES ranges, RANGE range);
1013 static BOOL ranges_del(RANGES ranges, RANGE range);
1014 static void ranges_dump(RANGES ranges);
1016 static inline BOOL ranges_additem(RANGES ranges, INT nItem)
1018 RANGE range = { nItem, nItem + 1 };
1020 return ranges_add(ranges, range);
1023 static inline BOOL ranges_delitem(RANGES ranges, INT nItem)
1025 RANGE range = { nItem, nItem + 1 };
1027 return ranges_del(ranges, range);
1031 * ITERATOR DOCUMENTATION
1033 * The iterator functions allow for easy, and convenient iteration
1034 * over items of iterest in the list. Typically, you create a
1035 * iterator, use it, and destroy it, as such:
1038 * iterator_xxxitems(&i, ...);
1039 * while (iterator_{prev,next}(&i)
1041 * //code which uses i.nItem
1043 * iterator_destroy(&i);
1045 * where xxx is either: framed, or visible.
1046 * Note that it is important that the code destroys the iterator
1047 * after it's done with it, as the creation of the iterator may
1048 * allocate memory, which thus needs to be freed.
1050 * You can iterate both forwards, and backwards through the list,
1051 * by using iterator_next or iterator_prev respectively.
1053 * Lower numbered items are draw on top of higher number items in
1054 * LVS_ICON, and LVS_SMALLICON (which are the only modes where
1055 * items may overlap). So, to test items, you should use
1057 * which lists the items top to bottom (in Z-order).
1058 * For drawing items, you should use
1060 * which lists the items bottom to top (in Z-order).
1061 * If you keep iterating over the items after the end-of-items
1062 * marker (-1) is returned, the iterator will start from the
1063 * beginning. Typically, you don't need to test for -1,
1064 * because iterator_{next,prev} will return TRUE if more items
1065 * are to be iterated over, or FALSE otherwise.
1067 * Note: the iterator is defined to be bidirectional. That is,
1068 * any number of prev followed by any number of next, or
1069 * five versa, should leave the iterator at the same item:
1070 * prev * n, next * n = next * n, prev * n
1072 * The iterator has a notion of an out-of-order, special item,
1073 * which sits at the start of the list. This is used in
1074 * LVS_ICON, and LVS_SMALLICON mode to handle the focused item,
1075 * which needs to be first, as it may overlap other items.
1077 * The code is a bit messy because we have:
1078 * - a special item to deal with
1079 * - simple range, or composite range
1081 * If you find bugs, or want to add features, please make sure you
1082 * always check/modify *both* iterator_prev, and iterator_next.
1086 * This function iterates through the items in increasing order,
1087 * but prefixed by the special item, then -1. That is:
1088 * special, 1, 2, 3, ..., n, -1.
1089 * Each item is listed only once.
1091 static inline BOOL iterator_next(ITERATOR* i)
1095 i->nItem = i->nSpecial;
1096 if (i->nItem != -1) return TRUE;
1098 if (i->nItem == i->nSpecial)
1100 if (i->ranges) i->index = 0;
1106 if (i->nItem == i->nSpecial) i->nItem++;
1107 if (i->nItem < i->range.upper) return TRUE;
1112 if (i->index < DPA_GetPtrCount(i->ranges->hdpa))
1113 i->range = *(RANGE*)DPA_GetPtr(i->ranges->hdpa, i->index++);
1116 else if (i->nItem >= i->range.upper) goto end;
1118 i->nItem = i->range.lower;
1119 if (i->nItem >= 0) goto testitem;
1126 * This function iterates through the items in decreasing order,
1127 * followed by the special item, then -1. That is:
1128 * n, n-1, ..., 3, 2, 1, special, -1.
1129 * Each item is listed only once.
1131 static inline BOOL iterator_prev(ITERATOR* i)
1138 if (i->ranges) i->index = DPA_GetPtrCount(i->ranges->hdpa);
1141 if (i->nItem == i->nSpecial)
1149 if (i->nItem == i->nSpecial) i->nItem--;
1150 if (i->nItem >= i->range.lower) return TRUE;
1156 i->range = *(RANGE*)DPA_GetPtr(i->ranges->hdpa, --i->index);
1159 else if (!start && i->nItem < i->range.lower) goto end;
1161 i->nItem = i->range.upper;
1162 if (i->nItem > 0) goto testitem;
1164 return (i->nItem = i->nSpecial) != -1;
1167 static RANGE iterator_range(ITERATOR* i)
1171 if (!i->ranges) return i->range;
1173 if (DPA_GetPtrCount(i->ranges->hdpa) > 0)
1175 range.lower = (*(RANGE*)DPA_GetPtr(i->ranges->hdpa, 0)).lower;
1176 range.upper = (*(RANGE*)DPA_GetPtr(i->ranges->hdpa, DPA_GetPtrCount(i->ranges->hdpa) - 1)).upper;
1178 else range.lower = range.upper = 0;
1184 * Releases resources associated with this ierator.
1186 static inline void iterator_destroy(ITERATOR* i)
1188 ranges_destroy(i->ranges);
1192 * Create an empty iterator.
1194 static inline BOOL iterator_empty(ITERATOR* i)
1196 ZeroMemory(i, sizeof(*i));
1197 i->nItem = i->nSpecial = i->range.lower = i->range.upper = -1;
1202 * Create an iterator over a range.
1204 static inline BOOL iterator_rangeitems(ITERATOR* i, RANGE range)
1212 * Create an iterator over a bunch of ranges.
1213 * Please note that the iterator will take ownership of the ranges,
1214 * and will free them upon destruction.
1216 static inline BOOL iterator_rangesitems(ITERATOR* i, RANGES ranges)
1224 * Creates an iterator over the items which intersect lprc.
1226 static BOOL iterator_frameditems(ITERATOR* i, LISTVIEW_INFO* infoPtr, const RECT *lprc)
1228 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
1229 RECT frame = *lprc, rcItem, rcTemp;
1232 /* in case we fail, we want to return an empty iterator */
1233 if (!iterator_empty(i)) return FALSE;
1235 LISTVIEW_GetOrigin(infoPtr, &Origin);
1237 TRACE("(lprc=%s)\n", debugrect(lprc));
1238 OffsetRect(&frame, -Origin.x, -Origin.y);
1240 if (uView == LVS_ICON || uView == LVS_SMALLICON)
1244 if (uView == LVS_ICON && infoPtr->nFocusedItem != -1)
1246 LISTVIEW_GetItemBox(infoPtr, infoPtr->nFocusedItem, &rcItem);
1247 if (IntersectRect(&rcTemp, &rcItem, lprc))
1248 i->nSpecial = infoPtr->nFocusedItem;
1250 if (!(iterator_rangesitems(i, ranges_create(50)))) return FALSE;
1251 /* to do better here, we need to have PosX, and PosY sorted */
1252 TRACE("building icon ranges:\n");
1253 for (nItem = 0; nItem < infoPtr->nItemCount; nItem++)
1255 rcItem.left = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosX, nItem);
1256 rcItem.top = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosY, nItem);
1257 rcItem.right = rcItem.left + infoPtr->nItemWidth;
1258 rcItem.bottom = rcItem.top + infoPtr->nItemHeight;
1259 if (IntersectRect(&rcTemp, &rcItem, &frame))
1260 ranges_additem(i->ranges, nItem);
1264 else if (uView == LVS_REPORT)
1268 if (frame.left >= infoPtr->nItemWidth) return TRUE;
1269 if (frame.top >= infoPtr->nItemHeight * infoPtr->nItemCount) return TRUE;
1271 range.lower = max(frame.top / infoPtr->nItemHeight, 0);
1272 range.upper = min((frame.bottom - 1) / infoPtr->nItemHeight, infoPtr->nItemCount - 1) + 1;
1273 if (range.upper <= range.lower) return TRUE;
1274 if (!iterator_rangeitems(i, range)) return FALSE;
1275 TRACE(" report=%s\n", debugrange(&i->range));
1279 INT nPerCol = max((infoPtr->rcList.bottom - infoPtr->rcList.top) / infoPtr->nItemHeight, 1);
1280 INT nFirstRow = max(frame.top / infoPtr->nItemHeight, 0);
1281 INT nLastRow = min((frame.bottom - 1) / infoPtr->nItemHeight, nPerCol - 1);
1282 INT nFirstCol = max(frame.left / infoPtr->nItemWidth, 0);
1283 INT nLastCol = min((frame.right - 1) / infoPtr->nItemWidth, (infoPtr->nItemCount + nPerCol - 1) / nPerCol);
1284 INT lower = nFirstCol * nPerCol + nFirstRow;
1288 TRACE("nPerCol=%d, nFirstRow=%d, nLastRow=%d, nFirstCol=%d, nLastCol=%d, lower=%d\n",
1289 nPerCol, nFirstRow, nLastRow, nFirstCol, nLastCol, lower);
1291 if (nLastCol < nFirstCol || nLastRow < nFirstRow) return TRUE;
1293 if (!(iterator_rangesitems(i, ranges_create(nLastCol - nFirstCol + 1)))) return FALSE;
1294 TRACE("building list ranges:\n");
1295 for (nCol = nFirstCol; nCol <= nLastCol; nCol++)
1297 item_range.lower = nCol * nPerCol + nFirstRow;
1298 if(item_range.lower >= infoPtr->nItemCount) break;
1299 item_range.upper = min(nCol * nPerCol + nLastRow + 1, infoPtr->nItemCount);
1300 TRACE(" list=%s\n", debugrange(&item_range));
1301 ranges_add(i->ranges, item_range);
1309 * Creates an iterator over the items which intersect the visible region of hdc.
1311 static BOOL iterator_visibleitems(ITERATOR *i, LISTVIEW_INFO *infoPtr, HDC hdc)
1313 POINT Origin, Position;
1314 RECT rcItem, rcClip;
1317 rgntype = GetClipBox(hdc, &rcClip);
1318 if (rgntype == NULLREGION) return iterator_empty(i);
1319 if (!iterator_frameditems(i, infoPtr, &rcClip)) return FALSE;
1320 if (rgntype == SIMPLEREGION) return TRUE;
1322 /* first deal with the special item */
1323 if (i->nSpecial != -1)
1325 LISTVIEW_GetItemBox(infoPtr, i->nSpecial, &rcItem);
1326 if (!RectVisible(hdc, &rcItem)) i->nSpecial = -1;
1329 /* if we can't deal with the region, we'll just go with the simple range */
1330 LISTVIEW_GetOrigin(infoPtr, &Origin);
1331 TRACE("building visible range:\n");
1332 if (!i->ranges && i->range.lower < i->range.upper)
1334 if (!(i->ranges = ranges_create(50))) return TRUE;
1335 if (!ranges_add(i->ranges, i->range))
1337 ranges_destroy(i->ranges);
1343 /* now delete the invisible items from the list */
1344 while(iterator_next(i))
1346 LISTVIEW_GetItemOrigin(infoPtr, i->nItem, &Position);
1347 rcItem.left = Position.x + Origin.x;
1348 rcItem.top = Position.y + Origin.y;
1349 rcItem.right = rcItem.left + infoPtr->nItemWidth;
1350 rcItem.bottom = rcItem.top + infoPtr->nItemHeight;
1351 if (!RectVisible(hdc, &rcItem))
1352 ranges_delitem(i->ranges, i->nItem);
1354 /* the iterator should restart on the next iterator_next */
1360 /******** Misc helper functions ************************************/
1362 static inline LRESULT CallWindowProcT(WNDPROC proc, HWND hwnd, UINT uMsg,
1363 WPARAM wParam, LPARAM lParam, BOOL isW)
1365 if (isW) return CallWindowProcW(proc, hwnd, uMsg, wParam, lParam);
1366 else return CallWindowProcA(proc, hwnd, uMsg, wParam, lParam);
1369 static inline BOOL is_autoarrange(LISTVIEW_INFO *infoPtr)
1371 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
1373 return ((infoPtr->dwStyle & LVS_AUTOARRANGE) || infoPtr->bAutoarrange) &&
1374 (uView == LVS_ICON || uView == LVS_SMALLICON);
1377 /******** Internal API functions ************************************/
1379 static inline COLUMN_INFO * LISTVIEW_GetColumnInfo(LISTVIEW_INFO *infoPtr, INT nSubItem)
1381 static COLUMN_INFO mainItem;
1383 if (nSubItem == 0 && DPA_GetPtrCount(infoPtr->hdpaColumns) == 0) return &mainItem;
1384 assert (nSubItem >= 0 && nSubItem < DPA_GetPtrCount(infoPtr->hdpaColumns));
1385 return (COLUMN_INFO *)DPA_GetPtr(infoPtr->hdpaColumns, nSubItem);
1388 static inline void LISTVIEW_GetHeaderRect(LISTVIEW_INFO *infoPtr, INT nSubItem, RECT *lprc)
1390 *lprc = LISTVIEW_GetColumnInfo(infoPtr, nSubItem)->rcHeader;
1393 static inline BOOL LISTVIEW_GetItemW(LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem)
1395 return LISTVIEW_GetItemT(infoPtr, lpLVItem, TRUE);
1398 /* Listview invalidation functions: use _only_ these functions to invalidate */
1400 static inline BOOL is_redrawing(LISTVIEW_INFO *infoPtr)
1402 return infoPtr->bRedraw;
1405 static inline void LISTVIEW_InvalidateRect(LISTVIEW_INFO *infoPtr, const RECT* rect)
1407 if(!is_redrawing(infoPtr)) return;
1408 TRACE(" invalidating rect=%s\n", debugrect(rect));
1409 InvalidateRect(infoPtr->hwndSelf, rect, TRUE);
1412 static inline void LISTVIEW_InvalidateItem(LISTVIEW_INFO *infoPtr, INT nItem)
1416 if(!is_redrawing(infoPtr)) return;
1417 LISTVIEW_GetItemBox(infoPtr, nItem, &rcBox);
1418 LISTVIEW_InvalidateRect(infoPtr, &rcBox);
1421 static inline void LISTVIEW_InvalidateSubItem(LISTVIEW_INFO *infoPtr, INT nItem, INT nSubItem)
1423 POINT Origin, Position;
1426 if(!is_redrawing(infoPtr)) return;
1427 assert ((infoPtr->dwStyle & LVS_TYPEMASK) == LVS_REPORT);
1428 LISTVIEW_GetOrigin(infoPtr, &Origin);
1429 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
1430 LISTVIEW_GetHeaderRect(infoPtr, nSubItem, &rcBox);
1432 rcBox.bottom = infoPtr->nItemHeight;
1433 OffsetRect(&rcBox, Origin.x + Position.x, Origin.y + Position.y);
1434 LISTVIEW_InvalidateRect(infoPtr, &rcBox);
1437 static inline void LISTVIEW_InvalidateList(LISTVIEW_INFO *infoPtr)
1439 LISTVIEW_InvalidateRect(infoPtr, NULL);
1442 static inline void LISTVIEW_InvalidateColumn(LISTVIEW_INFO *infoPtr, INT nColumn)
1446 if(!is_redrawing(infoPtr)) return;
1447 LISTVIEW_GetHeaderRect(infoPtr, nColumn, &rcCol);
1448 rcCol.top = infoPtr->rcList.top;
1449 rcCol.bottom = infoPtr->rcList.bottom;
1450 LISTVIEW_InvalidateRect(infoPtr, &rcCol);
1455 * Retrieves the number of items that can fit vertically in the client area.
1458 * [I] infoPtr : valid pointer to the listview structure
1461 * Number of items per row.
1463 static inline INT LISTVIEW_GetCountPerRow(LISTVIEW_INFO *infoPtr)
1465 INT nListWidth = infoPtr->rcList.right - infoPtr->rcList.left;
1467 return max(nListWidth/infoPtr->nItemWidth, 1);
1472 * Retrieves the number of items that can fit horizontally in the client
1476 * [I] infoPtr : valid pointer to the listview structure
1479 * Number of items per column.
1481 static inline INT LISTVIEW_GetCountPerColumn(LISTVIEW_INFO *infoPtr)
1483 INT nListHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
1485 return max(nListHeight / infoPtr->nItemHeight, 1);
1489 /*************************************************************************
1490 * LISTVIEW_ProcessLetterKeys
1492 * Processes keyboard messages generated by pressing the letter keys
1494 * What this does is perform a case insensitive search from the
1495 * current position with the following quirks:
1496 * - If two chars or more are pressed in quick succession we search
1497 * for the corresponding string (e.g. 'abc').
1498 * - If there is a delay we wipe away the current search string and
1499 * restart with just that char.
1500 * - If the user keeps pressing the same character, whether slowly or
1501 * fast, so that the search string is entirely composed of this
1502 * character ('aaaaa' for instance), then we search for first item
1503 * that starting with that character.
1504 * - If the user types the above character in quick succession, then
1505 * we must also search for the corresponding string ('aaaaa'), and
1506 * go to that string if there is a match.
1509 * [I] hwnd : handle to the window
1510 * [I] charCode : the character code, the actual character
1511 * [I] keyData : key data
1519 * - The current implementation has a list of characters it will
1520 * accept and it ignores averything else. In particular it will
1521 * ignore accentuated characters which seems to match what
1522 * Windows does. But I'm not sure it makes sense to follow
1524 * - We don't sound a beep when the search fails.
1528 * TREEVIEW_ProcessLetterKeys
1530 static INT LISTVIEW_ProcessLetterKeys(LISTVIEW_INFO *infoPtr, WPARAM charCode, LPARAM keyData)
1535 WCHAR buffer[MAX_PATH];
1536 DWORD lastKeyPressTimestamp = infoPtr->lastKeyPressTimestamp;
1538 /* simple parameter checking */
1539 if (!charCode || !keyData) return 0;
1541 /* only allow the valid WM_CHARs through */
1542 if (!isalnum(charCode) &&
1543 charCode != '.' && charCode != '`' && charCode != '!' &&
1544 charCode != '@' && charCode != '#' && charCode != '$' &&
1545 charCode != '%' && charCode != '^' && charCode != '&' &&
1546 charCode != '*' && charCode != '(' && charCode != ')' &&
1547 charCode != '-' && charCode != '_' && charCode != '+' &&
1548 charCode != '=' && charCode != '\\'&& charCode != ']' &&
1549 charCode != '}' && charCode != '[' && charCode != '{' &&
1550 charCode != '/' && charCode != '?' && charCode != '>' &&
1551 charCode != '<' && charCode != ',' && charCode != '~')
1554 /* if there's one item or less, there is no where to go */
1555 if (infoPtr->nItemCount <= 1) return 0;
1557 /* update the search parameters */
1558 infoPtr->lastKeyPressTimestamp = GetTickCount();
1559 if (infoPtr->lastKeyPressTimestamp - lastKeyPressTimestamp < KEY_DELAY) {
1560 if (infoPtr->nSearchParamLength < MAX_PATH)
1561 infoPtr->szSearchParam[infoPtr->nSearchParamLength++]=charCode;
1562 if (infoPtr->charCode != charCode)
1563 infoPtr->charCode = charCode = 0;
1565 infoPtr->charCode=charCode;
1566 infoPtr->szSearchParam[0]=charCode;
1567 infoPtr->nSearchParamLength=1;
1568 /* Redundant with the 1 char string */
1572 /* and search from the current position */
1574 if (infoPtr->nFocusedItem >= 0) {
1575 endidx=infoPtr->nFocusedItem;
1577 /* if looking for single character match,
1578 * then we must always move forward
1580 if (infoPtr->nSearchParamLength == 1)
1583 endidx=infoPtr->nItemCount;
1587 if (idx == infoPtr->nItemCount) {
1588 if (endidx == infoPtr->nItemCount || endidx == 0)
1594 item.mask = LVIF_TEXT;
1597 item.pszText = buffer;
1598 item.cchTextMax = MAX_PATH;
1599 if (!LISTVIEW_GetItemW(infoPtr, &item)) return 0;
1601 /* check for a match */
1602 if (lstrncmpiW(item.pszText,infoPtr->szSearchParam,infoPtr->nSearchParamLength) == 0) {
1605 } else if ( (charCode != 0) && (nItem == -1) && (nItem != infoPtr->nFocusedItem) &&
1606 (lstrncmpiW(item.pszText,infoPtr->szSearchParam,1) == 0) ) {
1607 /* This would work but we must keep looking for a longer match */
1611 } while (idx != endidx);
1614 LISTVIEW_KeySelection(infoPtr, nItem);
1619 /*************************************************************************
1620 * LISTVIEW_UpdateHeaderSize [Internal]
1622 * Function to resize the header control
1625 * [I] hwnd : handle to a window
1626 * [I] nNewScrollPos : scroll pos to set
1631 static void LISTVIEW_UpdateHeaderSize(LISTVIEW_INFO *infoPtr, INT nNewScrollPos)
1636 TRACE("nNewScrollPos=%d\n", nNewScrollPos);
1638 GetWindowRect(infoPtr->hwndHeader, &winRect);
1639 point[0].x = winRect.left;
1640 point[0].y = winRect.top;
1641 point[1].x = winRect.right;
1642 point[1].y = winRect.bottom;
1644 MapWindowPoints(HWND_DESKTOP, infoPtr->hwndSelf, point, 2);
1645 point[0].x = -nNewScrollPos;
1646 point[1].x += nNewScrollPos;
1648 SetWindowPos(infoPtr->hwndHeader,0,
1649 point[0].x,point[0].y,point[1].x,point[1].y,
1650 SWP_NOZORDER | SWP_NOACTIVATE);
1655 * Update the scrollbars. This functions should be called whenever
1656 * the content, size or view changes.
1659 * [I] infoPtr : valid pointer to the listview structure
1664 static void LISTVIEW_UpdateScroll(LISTVIEW_INFO *infoPtr)
1666 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
1667 SCROLLINFO horzInfo, vertInfo;
1669 if ((infoPtr->dwStyle & LVS_NOSCROLL) || !is_redrawing(infoPtr)) return;
1671 ZeroMemory(&horzInfo, sizeof(SCROLLINFO));
1672 horzInfo.cbSize = sizeof(SCROLLINFO);
1673 horzInfo.nPage = infoPtr->rcList.right - infoPtr->rcList.left;
1675 /* for now, we'll set info.nMax to the _count_, and adjust it later */
1676 if (uView == LVS_LIST)
1678 INT nPerCol = LISTVIEW_GetCountPerColumn(infoPtr);
1679 horzInfo.nMax = (infoPtr->nItemCount + nPerCol - 1) / nPerCol;
1681 /* scroll by at least one column per page */
1682 if(horzInfo.nPage < infoPtr->nItemWidth)
1683 horzInfo.nPage = infoPtr->nItemWidth;
1685 horzInfo.nPage /= infoPtr->nItemWidth;
1687 else if (uView == LVS_REPORT)
1689 horzInfo.nMax = infoPtr->nItemWidth;
1691 else /* LVS_ICON, or LVS_SMALLICON */
1695 if (LISTVIEW_GetViewRect(infoPtr, &rcView)) horzInfo.nMax = rcView.right - rcView.left;
1698 horzInfo.fMask = SIF_RANGE | SIF_PAGE;
1699 horzInfo.nMax = max(horzInfo.nMax - 1, 0);
1700 SetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &horzInfo, TRUE);
1701 TRACE("horzInfo=%s\n", debugscrollinfo(&horzInfo));
1703 /* Setting the horizontal scroll can change the listview size
1704 * (and potentially everything else) so we need to recompute
1705 * everything again for the vertical scroll
1708 ZeroMemory(&vertInfo, sizeof(SCROLLINFO));
1709 vertInfo.cbSize = sizeof(SCROLLINFO);
1710 vertInfo.nPage = infoPtr->rcList.bottom - infoPtr->rcList.top;
1712 if (uView == LVS_REPORT)
1714 vertInfo.nMax = infoPtr->nItemCount;
1716 /* scroll by at least one page */
1717 if(vertInfo.nPage < infoPtr->nItemHeight)
1718 vertInfo.nPage = infoPtr->nItemHeight;
1720 vertInfo.nPage /= infoPtr->nItemHeight;
1722 else if (uView != LVS_LIST) /* LVS_ICON, or LVS_SMALLICON */
1726 if (LISTVIEW_GetViewRect(infoPtr, &rcView)) vertInfo.nMax = rcView.bottom - rcView.top;
1729 vertInfo.fMask = SIF_RANGE | SIF_PAGE;
1730 vertInfo.nMax = max(vertInfo.nMax - 1, 0);
1731 SetScrollInfo(infoPtr->hwndSelf, SB_VERT, &vertInfo, TRUE);
1732 TRACE("vertInfo=%s\n", debugscrollinfo(&vertInfo));
1734 /* Update the Header Control */
1735 if (uView == LVS_REPORT)
1737 horzInfo.fMask = SIF_POS;
1738 GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &horzInfo);
1739 LISTVIEW_UpdateHeaderSize(infoPtr, horzInfo.nPos);
1746 * Shows/hides the focus rectangle.
1749 * [I] infoPtr : valid pointer to the listview structure
1750 * [I] fShow : TRUE to show the focus, FALSE to hide it.
1755 static void LISTVIEW_ShowFocusRect(LISTVIEW_INFO *infoPtr, BOOL fShow)
1757 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
1760 TRACE("fShow=%d, nItem=%d\n", fShow, infoPtr->nFocusedItem);
1762 if (infoPtr->nFocusedItem < 0) return;
1764 /* we need some gymnastics in ICON mode to handle large items */
1765 if ( (infoPtr->dwStyle & LVS_TYPEMASK) == LVS_ICON )
1769 LISTVIEW_GetItemBox(infoPtr, infoPtr->nFocusedItem, &rcBox);
1770 if ((rcBox.bottom - rcBox.top) > infoPtr->nItemHeight)
1772 LISTVIEW_InvalidateRect(infoPtr, &rcBox);
1777 if (!(hdc = GetDC(infoPtr->hwndSelf))) return;
1779 /* for some reason, owner draw should work only in report mode */
1780 if ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (uView == LVS_REPORT))
1785 item.iItem = infoPtr->nFocusedItem;
1787 item.mask = LVIF_PARAM;
1788 if (!LISTVIEW_GetItemW(infoPtr, &item)) goto done;
1790 ZeroMemory(&dis, sizeof(dis));
1791 dis.CtlType = ODT_LISTVIEW;
1792 dis.CtlID = (UINT)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1793 dis.itemID = item.iItem;
1794 dis.itemAction = ODA_FOCUS;
1795 if (fShow) dis.itemState |= ODS_FOCUS;
1796 dis.hwndItem = infoPtr->hwndSelf;
1798 LISTVIEW_GetItemBox(infoPtr, dis.itemID, &dis.rcItem);
1799 dis.itemData = item.lParam;
1801 SendMessageW(infoPtr->hwndNotify, WM_DRAWITEM, dis.CtlID, (LPARAM)&dis);
1805 DrawFocusRect(hdc, &infoPtr->rcFocus);
1808 ReleaseDC(infoPtr->hwndSelf, hdc);
1812 * Invalidates all visible selected items.
1814 static void LISTVIEW_InvalidateSelectedItems(LISTVIEW_INFO *infoPtr)
1818 iterator_frameditems(&i, infoPtr, &infoPtr->rcList);
1819 while(iterator_next(&i))
1821 if (LISTVIEW_GetItemState(infoPtr, i.nItem, LVIS_SELECTED))
1822 LISTVIEW_InvalidateItem(infoPtr, i.nItem);
1824 iterator_destroy(&i);
1829 * DESCRIPTION: [INTERNAL]
1830 * Computes an item's (left,top) corner, relative to rcView.
1831 * That is, the position has NOT been made relative to the Origin.
1832 * This is deliberate, to avoid computing the Origin over, and
1833 * over again, when this function is call in a loop. Instead,
1834 * one ca factor the computation of the Origin before the loop,
1835 * and offset the value retured by this function, on every iteration.
1838 * [I] infoPtr : valid pointer to the listview structure
1839 * [I] nItem : item number
1840 * [O] lpptOrig : item top, left corner
1845 static void LISTVIEW_GetItemOrigin(LISTVIEW_INFO *infoPtr, INT nItem, LPPOINT lpptPosition)
1847 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
1849 assert(nItem >= 0 && nItem < infoPtr->nItemCount);
1851 if ((uView == LVS_SMALLICON) || (uView == LVS_ICON))
1853 lpptPosition->x = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosX, nItem);
1854 lpptPosition->y = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosY, nItem);
1856 else if (uView == LVS_LIST)
1858 INT nCountPerColumn = LISTVIEW_GetCountPerColumn(infoPtr);
1859 lpptPosition->x = nItem / nCountPerColumn * infoPtr->nItemWidth;
1860 lpptPosition->y = nItem % nCountPerColumn * infoPtr->nItemHeight;
1862 else /* LVS_REPORT */
1864 lpptPosition->x = 0;
1865 lpptPosition->y = nItem * infoPtr->nItemHeight;
1870 * DESCRIPTION: [INTERNAL]
1871 * Compute the rectangles of an item. This is to localize all
1872 * the computations in one place. If you are not interested in some
1873 * of these values, simply pass in a NULL -- the fucntion is smart
1874 * enough to compute only what's necessary. The function computes
1875 * the standard rectangles (BOUNDS, ICON, LABEL) plus a non-standard
1876 * one, the BOX rectangle. This rectangle is very cheap to compute,
1877 * and is guaranteed to contain all the other rectangles. Computing
1878 * the ICON rect is also cheap, but all the others are potentaily
1879 * expensive. This gives an easy and effective optimization when
1880 * searching (like point inclusion, or rectangle intersection):
1881 * first test against the BOX, and if TRUE, test agains the desired
1883 * If the function does not have all the necessary information
1884 * to computed the requested rectangles, will crash with a
1885 * failed assertion. This is done so we catch all programming
1886 * errors, given that the function is called only from our code.
1888 * We have the following 'special' meanings for a few fields:
1889 * * If LVIS_FOCUSED is set, we assume the item has the focus
1890 * This is important in ICON mode, where it might get a larger
1891 * then usual rectange
1893 * Please note that subitem support works only in REPORT mode.
1896 * [I] infoPtr : valid pointer to the listview structure
1897 * [I] lpLVItem : item to compute the measures for
1898 * [O] lprcBox : ptr to Box rectangle
1899 * The internal LVIR_BOX rectangle
1900 * [0] lprcState : ptr to State icon rectangle
1901 * The internal LVIR_STATE rectangle
1902 * [O] lprcIcon : ptr to Icon rectangle
1903 * Same as LVM_GETITEMRECT with LVIR_ICON
1904 * [O] lprcLabel : ptr to Label rectangle
1905 * Same as LVM_GETITEMRECT with LVIR_LABEL
1910 static void LISTVIEW_GetItemMetrics(LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem,
1911 LPRECT lprcBox, LPRECT lprcState,
1912 LPRECT lprcIcon, LPRECT lprcLabel)
1914 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
1915 BOOL doState = FALSE, doIcon = FALSE, doLabel = FALSE, oversizedBox = FALSE;
1916 RECT Box, State, Icon, Label;
1917 COLUMN_INFO *lpColumnInfo = NULL;
1919 TRACE("(lpLVItem=%s)\n", debuglvitem_t(lpLVItem, TRUE));
1921 /* Be smart and try to figure out the minimum we have to do */
1922 if (lpLVItem->iSubItem) assert(uView == LVS_REPORT);
1923 if (uView == LVS_ICON && (lprcBox || lprcLabel))
1925 assert((lpLVItem->mask & LVIF_STATE) && (lpLVItem->stateMask & LVIS_FOCUSED));
1926 if (lpLVItem->state & LVIS_FOCUSED) oversizedBox = doLabel = TRUE;
1928 if (lprcLabel) doLabel = TRUE;
1929 if (doLabel || lprcIcon) doIcon = TRUE;
1930 if (doIcon || lprcState) doState = TRUE;
1932 /************************************************************/
1933 /* compute the box rectangle (it should be cheap to do) */
1934 /************************************************************/
1935 if (lpLVItem->iSubItem || uView == LVS_REPORT)
1936 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, lpLVItem->iSubItem);
1938 if (lpLVItem->iSubItem)
1940 Box = lpColumnInfo->rcHeader;
1945 Box.right = infoPtr->nItemWidth;
1948 Box.bottom = infoPtr->nItemHeight;
1950 /************************************************************/
1951 /* compute STATEICON bounding box */
1952 /************************************************************/
1955 if (uView == LVS_ICON)
1957 State.left = Box.left - infoPtr->iconStateSize.cx - 2;
1958 if (infoPtr->himlNormal)
1959 State.left += (infoPtr->nItemWidth - infoPtr->iconSize.cx) / 2;
1960 State.top = Box.top + infoPtr->iconSize.cy - infoPtr->iconStateSize.cy + 4;
1964 /* we need the ident in report mode, if we don't have it, we fail */
1965 State.left = Box.left;
1966 if (uView == LVS_REPORT)
1968 if (lpLVItem->iSubItem == 0)
1970 State.left += REPORT_MARGINX;
1971 assert(lpLVItem->mask & LVIF_INDENT);
1972 State.left += infoPtr->iconSize.cx * lpLVItem->iIndent;
1975 State.top = Box.top;
1977 State.right = State.left;
1978 State.bottom = State.top;
1979 if (infoPtr->himlState && lpLVItem->iSubItem == 0)
1981 State.right += infoPtr->iconStateSize.cx;
1982 State.bottom += infoPtr->iconStateSize.cy;
1984 if (lprcState) *lprcState = State;
1985 TRACE(" - state=%s\n", debugrect(&State));
1987 else State.right = 0;
1989 /************************************************************/
1990 /* compute ICON bounding box (ala LVM_GETITEMRECT) */
1991 /************************************************************/
1994 if (uView == LVS_ICON)
1996 Icon.left = Box.left;
1997 if (infoPtr->himlNormal)
1998 Icon.left += (infoPtr->nItemWidth - infoPtr->iconSize.cx) / 2;
1999 Icon.top = Box.top + ICON_TOP_PADDING;
2000 Icon.right = Icon.left;
2001 Icon.bottom = Icon.top;
2002 if (infoPtr->himlNormal)
2004 Icon.right += infoPtr->iconSize.cx;
2005 Icon.bottom += infoPtr->iconSize.cy;
2008 else /* LVS_SMALLICON, LVS_LIST or LVS_REPORT */
2010 Icon.left = State.right;
2012 Icon.right = Icon.left;
2013 if (infoPtr->himlSmall &&
2014 (!lpColumnInfo || lpLVItem->iSubItem == 0 || (lpColumnInfo->fmt & LVCFMT_IMAGE) ||
2015 ((infoPtr->dwLvExStyle & LVS_EX_SUBITEMIMAGES) && lpLVItem->iImage != I_IMAGECALLBACK)))
2016 Icon.right += infoPtr->iconSize.cx;
2017 Icon.bottom = Icon.top + infoPtr->nItemHeight;
2019 if(lprcIcon) *lprcIcon = Icon;
2020 TRACE(" - icon=%s\n", debugrect(&Icon));
2022 else Icon.right = 0;
2024 /************************************************************/
2025 /* compute LABEL bounding box (ala LVM_GETITEMRECT) */
2026 /************************************************************/
2029 SIZE labelSize = { 0, 0 };
2031 /* calculate how far to the right can the label strech */
2032 Label.right = Box.right;
2033 if (uView == LVS_REPORT)
2035 if (lpLVItem->iSubItem == 0) Label = lpColumnInfo->rcHeader;
2038 if (lpLVItem->iSubItem || ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && uView == LVS_REPORT))
2040 labelSize.cx = infoPtr->nItemWidth;
2041 labelSize.cy = infoPtr->nItemHeight;
2045 /* we need the text in non owner draw mode */
2046 assert(lpLVItem->mask & LVIF_TEXT);
2047 if (is_textT(lpLVItem->pszText, TRUE))
2049 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
2050 HDC hdc = GetDC(infoPtr->hwndSelf);
2051 HFONT hOldFont = SelectObject(hdc, hFont);
2055 /* compute rough rectangle where the label will go */
2056 SetRectEmpty(&rcText);
2057 rcText.right = infoPtr->nItemWidth - TRAILING_LABEL_PADDING;
2058 rcText.bottom = infoPtr->nItemHeight;
2059 if (uView == LVS_ICON)
2060 rcText.bottom -= ICON_TOP_PADDING + infoPtr->iconSize.cy + ICON_BOTTOM_PADDING;
2062 /* now figure out the flags */
2063 if (uView == LVS_ICON)
2064 uFormat = oversizedBox ? LV_FL_DT_FLAGS : LV_ML_DT_FLAGS;
2066 uFormat = LV_SL_DT_FLAGS;
2068 DrawTextW (hdc, lpLVItem->pszText, -1, &rcText, uFormat | DT_CALCRECT);
2070 labelSize.cx = min(rcText.right - rcText.left + TRAILING_LABEL_PADDING, infoPtr->nItemWidth);
2071 labelSize.cy = rcText.bottom - rcText.top;
2073 SelectObject(hdc, hOldFont);
2074 ReleaseDC(infoPtr->hwndSelf, hdc);
2078 if (uView == LVS_ICON)
2080 Label.left = Box.left + (infoPtr->nItemWidth - labelSize.cx) / 2;
2081 Label.top = Box.top + ICON_TOP_PADDING_HITABLE +
2082 infoPtr->iconSize.cy + ICON_BOTTOM_PADDING;
2083 Label.right = Label.left + labelSize.cx;
2084 Label.bottom = Label.top + infoPtr->nItemHeight;
2085 if (!oversizedBox && labelSize.cy > infoPtr->ntmHeight)
2087 labelSize.cy = min(Box.bottom - Label.top, labelSize.cy);
2088 labelSize.cy /= infoPtr->ntmHeight;
2089 labelSize.cy = max(labelSize.cy, 1);
2090 labelSize.cy *= infoPtr->ntmHeight;
2092 Label.bottom = Label.top + labelSize.cy + HEIGHT_PADDING;
2094 else /* LVS_SMALLICON, LVS_LIST or LVS_REPORT */
2096 Label.left = Icon.right;
2097 Label.top = Box.top;
2098 Label.right = min(Label.left + labelSize.cx, Label.right);
2099 Label.bottom = Label.top + infoPtr->nItemHeight;
2102 if (lprcLabel) *lprcLabel = Label;
2103 TRACE(" - label=%s\n", debugrect(&Label));
2106 /* Fix the Box if necessary */
2109 if (oversizedBox) UnionRect(lprcBox, &Box, &Label);
2110 else *lprcBox = Box;
2112 TRACE(" - box=%s\n", debugrect(&Box));
2116 * DESCRIPTION: [INTERNAL]
2119 * [I] infoPtr : valid pointer to the listview structure
2120 * [I] nItem : item number
2121 * [O] lprcBox : ptr to Box rectangle
2126 static void LISTVIEW_GetItemBox(LISTVIEW_INFO *infoPtr, INT nItem, LPRECT lprcBox)
2128 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
2129 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
2130 POINT Position, Origin;
2133 LISTVIEW_GetOrigin(infoPtr, &Origin);
2134 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
2136 /* Be smart and try to figure out the minimum we have to do */
2138 if (uView == LVS_ICON && infoPtr->bFocus && LISTVIEW_GetItemState(infoPtr, nItem, LVIS_FOCUSED))
2139 lvItem.mask |= LVIF_TEXT;
2140 lvItem.iItem = nItem;
2141 lvItem.iSubItem = 0;
2142 lvItem.pszText = szDispText;
2143 lvItem.cchTextMax = DISP_TEXT_SIZE;
2144 if (lvItem.mask) LISTVIEW_GetItemW(infoPtr, &lvItem);
2145 if (uView == LVS_ICON)
2147 lvItem.mask |= LVIF_STATE;
2148 lvItem.stateMask = LVIS_FOCUSED;
2149 lvItem.state = (lvItem.mask & LVIF_TEXT ? LVIS_FOCUSED : 0);
2151 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, lprcBox, 0, 0, 0);
2153 OffsetRect(lprcBox, Position.x + Origin.x, Position.y + Origin.y);
2159 * Returns the current icon position, and advances it along the top.
2160 * The returned position is not offset by Origin.
2163 * [I] infoPtr : valid pointer to the listview structure
2164 * [O] lpPos : will get the current icon position
2169 static void LISTVIEW_NextIconPosTop(LISTVIEW_INFO *infoPtr, LPPOINT lpPos)
2171 INT nListWidth = infoPtr->rcList.right - infoPtr->rcList.left;
2173 *lpPos = infoPtr->currIconPos;
2175 infoPtr->currIconPos.x += infoPtr->nItemWidth;
2176 if (infoPtr->currIconPos.x + infoPtr->nItemWidth <= nListWidth) return;
2178 infoPtr->currIconPos.x = 0;
2179 infoPtr->currIconPos.y += infoPtr->nItemHeight;
2185 * Returns the current icon position, and advances it down the left edge.
2186 * The returned position is not offset by Origin.
2189 * [I] infoPtr : valid pointer to the listview structure
2190 * [O] lpPos : will get the current icon position
2195 static void LISTVIEW_NextIconPosLeft(LISTVIEW_INFO *infoPtr, LPPOINT lpPos)
2197 INT nListHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
2199 *lpPos = infoPtr->currIconPos;
2201 infoPtr->currIconPos.y += infoPtr->nItemHeight;
2202 if (infoPtr->currIconPos.y + infoPtr->nItemHeight <= nListHeight) return;
2204 infoPtr->currIconPos.x += infoPtr->nItemWidth;
2205 infoPtr->currIconPos.y = 0;
2211 * Moves an icon to the specified position.
2212 * It takes care of invalidating the item, etc.
2215 * [I] infoPtr : valid pointer to the listview structure
2216 * [I] nItem : the item to move
2217 * [I] lpPos : the new icon position
2218 * [I] isNew : flags the item as being new
2224 static BOOL LISTVIEW_MoveIconTo(LISTVIEW_INFO *infoPtr, INT nItem, const POINT *lppt, BOOL isNew)
2230 old.x = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosX, nItem);
2231 old.y = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosY, nItem);
2233 if (lppt->x == old.x && lppt->y == old.y) return TRUE;
2234 LISTVIEW_InvalidateItem(infoPtr, nItem);
2237 /* Allocating a POINTER for every item is too resource intensive,
2238 * so we'll keep the (x,y) in different arrays */
2239 if (!DPA_SetPtr(infoPtr->hdpaPosX, nItem, (void *)(LONG_PTR)lppt->x)) return FALSE;
2240 if (!DPA_SetPtr(infoPtr->hdpaPosY, nItem, (void *)(LONG_PTR)lppt->y)) return FALSE;
2242 LISTVIEW_InvalidateItem(infoPtr, nItem);
2249 * Arranges listview items in icon display mode.
2252 * [I] infoPtr : valid pointer to the listview structure
2253 * [I] nAlignCode : alignment code
2259 static BOOL LISTVIEW_Arrange(LISTVIEW_INFO *infoPtr, INT nAlignCode)
2261 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
2262 void (*next_pos)(LISTVIEW_INFO *, LPPOINT);
2266 if (uView != LVS_ICON && uView != LVS_SMALLICON) return FALSE;
2268 TRACE("nAlignCode=%d\n", nAlignCode);
2270 if (nAlignCode == LVA_DEFAULT)
2272 if (infoPtr->dwStyle & LVS_ALIGNLEFT) nAlignCode = LVA_ALIGNLEFT;
2273 else nAlignCode = LVA_ALIGNTOP;
2278 case LVA_ALIGNLEFT: next_pos = LISTVIEW_NextIconPosLeft; break;
2279 case LVA_ALIGNTOP: next_pos = LISTVIEW_NextIconPosTop; break;
2280 case LVA_SNAPTOGRID: next_pos = LISTVIEW_NextIconPosTop; break; /* FIXME */
2281 default: return FALSE;
2284 infoPtr->bAutoarrange = TRUE;
2285 infoPtr->currIconPos.x = infoPtr->currIconPos.y = 0;
2286 for (i = 0; i < infoPtr->nItemCount; i++)
2288 next_pos(infoPtr, &pos);
2289 LISTVIEW_MoveIconTo(infoPtr, i, &pos, FALSE);
2297 * Retrieves the bounding rectangle of all the items, not offset by Origin.
2300 * [I] infoPtr : valid pointer to the listview structure
2301 * [O] lprcView : bounding rectangle
2307 static void LISTVIEW_GetAreaRect(LISTVIEW_INFO *infoPtr, LPRECT lprcView)
2311 SetRectEmpty(lprcView);
2313 switch (infoPtr->dwStyle & LVS_TYPEMASK)
2317 for (i = 0; i < infoPtr->nItemCount; i++)
2319 x = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosX, i);
2320 y = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosY, i);
2321 lprcView->right = max(lprcView->right, x);
2322 lprcView->bottom = max(lprcView->bottom, y);
2324 if (infoPtr->nItemCount > 0)
2326 lprcView->right += infoPtr->nItemWidth;
2327 lprcView->bottom += infoPtr->nItemHeight;
2332 y = LISTVIEW_GetCountPerColumn(infoPtr);
2333 x = infoPtr->nItemCount / y;
2334 if (infoPtr->nItemCount % y) x++;
2335 lprcView->right = x * infoPtr->nItemWidth;
2336 lprcView->bottom = y * infoPtr->nItemHeight;
2340 lprcView->right = infoPtr->nItemWidth;
2341 lprcView->bottom = infoPtr->nItemCount * infoPtr->nItemHeight;
2348 * Retrieves the bounding rectangle of all the items.
2351 * [I] infoPtr : valid pointer to the listview structure
2352 * [O] lprcView : bounding rectangle
2358 static BOOL LISTVIEW_GetViewRect(LISTVIEW_INFO *infoPtr, LPRECT lprcView)
2362 TRACE("(lprcView=%p)\n", lprcView);
2364 if (!lprcView) return FALSE;
2366 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
2367 LISTVIEW_GetAreaRect(infoPtr, lprcView);
2368 OffsetRect(lprcView, ptOrigin.x, ptOrigin.y);
2370 TRACE("lprcView=%s\n", debugrect(lprcView));
2377 * Retrieves the subitem pointer associated with the subitem index.
2380 * [I] hdpaSubItems : DPA handle for a specific item
2381 * [I] nSubItem : index of subitem
2384 * SUCCESS : subitem pointer
2387 static SUBITEM_INFO* LISTVIEW_GetSubItemPtr(HDPA hdpaSubItems, INT nSubItem)
2389 SUBITEM_INFO *lpSubItem;
2392 /* we should binary search here if need be */
2393 for (i = 1; i < DPA_GetPtrCount(hdpaSubItems); i++)
2395 lpSubItem = (SUBITEM_INFO *)DPA_GetPtr(hdpaSubItems, i);
2396 if (lpSubItem->iSubItem == nSubItem)
2406 * Caclulates the desired item width.
2409 * [I] infoPtr : valid pointer to the listview structure
2412 * The desired item width.
2414 static INT LISTVIEW_CalculateItemWidth(LISTVIEW_INFO *infoPtr)
2416 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
2419 TRACE("uView=%d\n", uView);
2421 if (uView == LVS_ICON)
2422 nItemWidth = infoPtr->iconSpacing.cx;
2423 else if (uView == LVS_REPORT)
2427 if (DPA_GetPtrCount(infoPtr->hdpaColumns) > 0)
2429 LISTVIEW_GetHeaderRect(infoPtr, DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, &rcHeader);
2430 nItemWidth = rcHeader.right;
2433 else /* LVS_SMALLICON, or LVS_LIST */
2437 for (i = 0; i < infoPtr->nItemCount; i++)
2438 nItemWidth = max(LISTVIEW_GetLabelWidth(infoPtr, i), nItemWidth);
2440 if (infoPtr->himlSmall) nItemWidth += infoPtr->iconSize.cx;
2441 if (infoPtr->himlState) nItemWidth += infoPtr->iconStateSize.cx;
2443 nItemWidth = max(DEFAULT_COLUMN_WIDTH, nItemWidth + WIDTH_PADDING);
2446 return max(nItemWidth, 1);
2451 * Caclulates the desired item height.
2454 * [I] infoPtr : valid pointer to the listview structure
2457 * The desired item height.
2459 static INT LISTVIEW_CalculateItemHeight(LISTVIEW_INFO *infoPtr)
2461 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
2464 TRACE("uView=%d\n", uView);
2466 if (uView == LVS_ICON)
2467 nItemHeight = infoPtr->iconSpacing.cy;
2470 nItemHeight = infoPtr->ntmHeight;
2471 if (infoPtr->himlState)
2472 nItemHeight = max(nItemHeight, infoPtr->iconStateSize.cy);
2473 if (infoPtr->himlSmall)
2474 nItemHeight = max(nItemHeight, infoPtr->iconSize.cy);
2475 if (infoPtr->himlState || infoPtr->himlSmall)
2476 nItemHeight += HEIGHT_PADDING;
2477 if (infoPtr->nMeasureItemHeight > 0)
2478 nItemHeight = infoPtr->nMeasureItemHeight;
2481 return max(nItemHeight, 1);
2486 * Updates the width, and height of an item.
2489 * [I] infoPtr : valid pointer to the listview structure
2494 static inline void LISTVIEW_UpdateItemSize(LISTVIEW_INFO *infoPtr)
2496 infoPtr->nItemWidth = LISTVIEW_CalculateItemWidth(infoPtr);
2497 infoPtr->nItemHeight = LISTVIEW_CalculateItemHeight(infoPtr);
2503 * Retrieves and saves important text metrics info for the current
2507 * [I] infoPtr : valid pointer to the listview structure
2510 static void LISTVIEW_SaveTextMetrics(LISTVIEW_INFO *infoPtr)
2512 HDC hdc = GetDC(infoPtr->hwndSelf);
2513 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
2514 HFONT hOldFont = SelectObject(hdc, hFont);
2518 if (GetTextMetricsW(hdc, &tm))
2520 infoPtr->ntmHeight = tm.tmHeight;
2521 infoPtr->ntmMaxCharWidth = tm.tmMaxCharWidth;
2524 if (GetTextExtentPoint32A(hdc, "...", 3, &sz))
2525 infoPtr->nEllipsisWidth = sz.cx;
2527 SelectObject(hdc, hOldFont);
2528 ReleaseDC(infoPtr->hwndSelf, hdc);
2530 TRACE("tmHeight=%d\n", infoPtr->ntmHeight);
2535 * A compare function for ranges
2538 * [I] range1 : pointer to range 1;
2539 * [I] range2 : pointer to range 2;
2543 * > 0 : if range 1 > range 2
2544 * < 0 : if range 2 > range 1
2545 * = 0 : if range intersects range 2
2547 static INT CALLBACK ranges_cmp(LPVOID range1, LPVOID range2, LPARAM flags)
2551 if (((RANGE*)range1)->upper <= ((RANGE*)range2)->lower)
2553 else if (((RANGE*)range2)->upper <= ((RANGE*)range1)->lower)
2558 TRACE("range1=%s, range2=%s, cmp=%d\n", debugrange((RANGE*)range1), debugrange((RANGE*)range2), cmp);
2564 #define ranges_check(ranges, desc) ranges_assert(ranges, desc, __FUNCTION__, __LINE__)
2566 #define ranges_check(ranges, desc) do { } while(0)
2569 static void ranges_assert(RANGES ranges, LPCSTR desc, const char *func, int line)
2574 TRACE("*** Checking %s:%d:%s ***\n", func, line, desc);
2576 assert (DPA_GetPtrCount(ranges->hdpa) >= 0);
2577 ranges_dump(ranges);
2578 prev = (RANGE *)DPA_GetPtr(ranges->hdpa, 0);
2579 if (DPA_GetPtrCount(ranges->hdpa) > 0)
2580 assert (prev->lower >= 0 && prev->lower < prev->upper);
2581 for (i = 1; i < DPA_GetPtrCount(ranges->hdpa); i++)
2583 curr = (RANGE *)DPA_GetPtr(ranges->hdpa, i);
2584 assert (prev->upper <= curr->lower);
2585 assert (curr->lower < curr->upper);
2588 TRACE("--- Done checking---\n");
2591 static RANGES ranges_create(int count)
2593 RANGES ranges = (RANGES)Alloc(sizeof(struct tagRANGES));
2594 if (!ranges) return NULL;
2595 ranges->hdpa = DPA_Create(count);
2596 if (ranges->hdpa) return ranges;
2601 static void ranges_clear(RANGES ranges)
2605 for(i = 0; i < DPA_GetPtrCount(ranges->hdpa); i++)
2606 Free(DPA_GetPtr(ranges->hdpa, i));
2607 DPA_DeleteAllPtrs(ranges->hdpa);
2611 static void ranges_destroy(RANGES ranges)
2613 if (!ranges) return;
2614 ranges_clear(ranges);
2615 DPA_Destroy(ranges->hdpa);
2619 static RANGES ranges_clone(RANGES ranges)
2624 if (!(clone = ranges_create(DPA_GetPtrCount(ranges->hdpa)))) goto fail;
2626 for (i = 0; i < DPA_GetPtrCount(ranges->hdpa); i++)
2628 RANGE *newrng = (RANGE *)Alloc(sizeof(RANGE));
2629 if (!newrng) goto fail;
2630 *newrng = *((RANGE*)DPA_GetPtr(ranges->hdpa, i));
2631 DPA_SetPtr(clone->hdpa, i, newrng);
2636 TRACE ("clone failed\n");
2637 ranges_destroy(clone);
2641 static RANGES ranges_diff(RANGES ranges, RANGES sub)
2645 for (i = 0; i < DPA_GetPtrCount(sub->hdpa); i++)
2646 ranges_del(ranges, *((RANGE *)DPA_GetPtr(sub->hdpa, i)));
2651 static void ranges_dump(RANGES ranges)
2655 for (i = 0; i < DPA_GetPtrCount(ranges->hdpa); i++)
2656 TRACE(" %s\n", debugrange(DPA_GetPtr(ranges->hdpa, i)));
2659 static inline BOOL ranges_contain(RANGES ranges, INT nItem)
2661 RANGE srchrng = { nItem, nItem + 1 };
2663 TRACE("(nItem=%d)\n", nItem);
2664 ranges_check(ranges, "before contain");
2665 return DPA_Search(ranges->hdpa, &srchrng, 0, ranges_cmp, 0, DPAS_SORTED) != -1;
2668 static INT ranges_itemcount(RANGES ranges)
2672 for (i = 0; i < DPA_GetPtrCount(ranges->hdpa); i++)
2674 RANGE *sel = DPA_GetPtr(ranges->hdpa, i);
2675 count += sel->upper - sel->lower;
2681 static BOOL ranges_shift(RANGES ranges, INT nItem, INT delta, INT nUpper)
2683 RANGE srchrng = { nItem, nItem + 1 }, *chkrng;
2686 index = DPA_Search(ranges->hdpa, &srchrng, 0, ranges_cmp, 0, DPAS_SORTED | DPAS_INSERTAFTER);
2687 if (index == -1) return TRUE;
2689 for (; index < DPA_GetPtrCount(ranges->hdpa); index++)
2691 chkrng = DPA_GetPtr(ranges->hdpa, index);
2692 if (chkrng->lower >= nItem)
2693 chkrng->lower = max(min(chkrng->lower + delta, nUpper - 1), 0);
2694 if (chkrng->upper > nItem)
2695 chkrng->upper = max(min(chkrng->upper + delta, nUpper), 0);
2700 static BOOL ranges_add(RANGES ranges, RANGE range)
2705 TRACE("(%s)\n", debugrange(&range));
2706 ranges_check(ranges, "before add");
2708 /* try find overlapping regions first */
2709 srchrgn.lower = range.lower - 1;
2710 srchrgn.upper = range.upper + 1;
2711 index = DPA_Search(ranges->hdpa, &srchrgn, 0, ranges_cmp, 0, DPAS_SORTED);
2717 TRACE("Adding new range\n");
2719 /* create the brand new range to insert */
2720 newrgn = (RANGE *)Alloc(sizeof(RANGE));
2721 if(!newrgn) goto fail;
2724 /* figure out where to insert it */
2725 index = DPA_Search(ranges->hdpa, newrgn, 0, ranges_cmp, 0, DPAS_SORTED | DPAS_INSERTAFTER);
2726 TRACE("index=%d\n", index);
2727 if (index == -1) index = 0;
2729 /* and get it over with */
2730 if (DPA_InsertPtr(ranges->hdpa, index, newrgn) == -1)
2738 RANGE *chkrgn, *mrgrgn;
2739 INT fromindex, mergeindex;
2741 chkrgn = DPA_GetPtr(ranges->hdpa, index);
2742 TRACE("Merge with %s @%d\n", debugrange(chkrgn), index);
2744 chkrgn->lower = min(range.lower, chkrgn->lower);
2745 chkrgn->upper = max(range.upper, chkrgn->upper);
2747 TRACE("New range %s @%d\n", debugrange(chkrgn), index);
2749 /* merge now common anges */
2751 srchrgn.lower = chkrgn->lower - 1;
2752 srchrgn.upper = chkrgn->upper + 1;
2756 mergeindex = DPA_Search(ranges->hdpa, &srchrgn, fromindex, ranges_cmp, 0, 0);
2757 if (mergeindex == -1) break;
2758 if (mergeindex == index)
2760 fromindex = index + 1;
2764 TRACE("Merge with index %i\n", mergeindex);
2766 mrgrgn = DPA_GetPtr(ranges->hdpa, mergeindex);
2767 chkrgn->lower = min(chkrgn->lower, mrgrgn->lower);
2768 chkrgn->upper = max(chkrgn->upper, mrgrgn->upper);
2770 DPA_DeletePtr(ranges->hdpa, mergeindex);
2771 if (mergeindex < index) index --;
2775 ranges_check(ranges, "after add");
2779 ranges_check(ranges, "failed add");
2783 static BOOL ranges_del(RANGES ranges, RANGE range)
2788 TRACE("(%s)\n", debugrange(&range));
2789 ranges_check(ranges, "before del");
2791 /* we don't use DPAS_SORTED here, since we need *
2792 * to find the first overlapping range */
2793 index = DPA_Search(ranges->hdpa, &range, 0, ranges_cmp, 0, 0);
2796 chkrgn = DPA_GetPtr(ranges->hdpa, index);
2798 TRACE("Matches range %s @%d\n", debugrange(chkrgn), index);
2800 /* case 1: Same range */
2801 if ( (chkrgn->upper == range.upper) &&
2802 (chkrgn->lower == range.lower) )
2804 DPA_DeletePtr(ranges->hdpa, index);
2807 /* case 2: engulf */
2808 else if ( (chkrgn->upper <= range.upper) &&
2809 (chkrgn->lower >= range.lower) )
2811 DPA_DeletePtr(ranges->hdpa, index);
2813 /* case 3: overlap upper */
2814 else if ( (chkrgn->upper <= range.upper) &&
2815 (chkrgn->lower < range.lower) )
2817 chkrgn->upper = range.lower;
2819 /* case 4: overlap lower */
2820 else if ( (chkrgn->upper > range.upper) &&
2821 (chkrgn->lower >= range.lower) )
2823 chkrgn->lower = range.upper;
2826 /* case 5: fully internal */
2829 RANGE tmprgn = *chkrgn, *newrgn;
2831 if (!(newrgn = (RANGE *)Alloc(sizeof(RANGE)))) goto fail;
2832 newrgn->lower = chkrgn->lower;
2833 newrgn->upper = range.lower;
2834 chkrgn->lower = range.upper;
2835 if (DPA_InsertPtr(ranges->hdpa, index, newrgn) == -1)
2844 index = DPA_Search(ranges->hdpa, &range, index, ranges_cmp, 0, 0);
2847 ranges_check(ranges, "after del");
2851 ranges_check(ranges, "failed del");
2857 * Removes all selection ranges
2860 * [I] infoPtr : valid pointer to the listview structure
2861 * [I] toSkip : item range to skip removing the selection
2867 static BOOL LISTVIEW_DeselectAllSkipItems(LISTVIEW_INFO *infoPtr, RANGES toSkip)
2876 lvItem.stateMask = LVIS_SELECTED;
2878 /* need to clone the DPA because callbacks can change it */
2879 if (!(clone = ranges_clone(infoPtr->selectionRanges))) return FALSE;
2880 iterator_rangesitems(&i, ranges_diff(clone, toSkip));
2881 while(iterator_next(&i))
2882 LISTVIEW_SetItemState(infoPtr, i.nItem, &lvItem);
2883 /* note that the iterator destructor will free the cloned range */
2884 iterator_destroy(&i);
2889 static inline BOOL LISTVIEW_DeselectAllSkipItem(LISTVIEW_INFO *infoPtr, INT nItem)
2893 if (!(toSkip = ranges_create(1))) return FALSE;
2894 if (nItem != -1) ranges_additem(toSkip, nItem);
2895 LISTVIEW_DeselectAllSkipItems(infoPtr, toSkip);
2896 ranges_destroy(toSkip);
2900 static inline BOOL LISTVIEW_DeselectAll(LISTVIEW_INFO *infoPtr)
2902 return LISTVIEW_DeselectAllSkipItem(infoPtr, -1);
2907 * Retrieves the number of items that are marked as selected.
2910 * [I] infoPtr : valid pointer to the listview structure
2913 * Number of items selected.
2915 static INT LISTVIEW_GetSelectedCount(LISTVIEW_INFO *infoPtr)
2917 INT nSelectedCount = 0;
2919 if (infoPtr->uCallbackMask & LVIS_SELECTED)
2922 for (i = 0; i < infoPtr->nItemCount; i++)
2924 if (LISTVIEW_GetItemState(infoPtr, i, LVIS_SELECTED))
2929 nSelectedCount = ranges_itemcount(infoPtr->selectionRanges);
2931 TRACE("nSelectedCount=%d\n", nSelectedCount);
2932 return nSelectedCount;
2937 * Manages the item focus.
2940 * [I] infoPtr : valid pointer to the listview structure
2941 * [I] nItem : item index
2944 * TRUE : focused item changed
2945 * FALSE : focused item has NOT changed
2947 static inline BOOL LISTVIEW_SetItemFocus(LISTVIEW_INFO *infoPtr, INT nItem)
2949 INT oldFocus = infoPtr->nFocusedItem;
2952 if (nItem == infoPtr->nFocusedItem) return FALSE;
2954 lvItem.state = nItem == -1 ? 0 : LVIS_FOCUSED;
2955 lvItem.stateMask = LVIS_FOCUSED;
2956 LISTVIEW_SetItemState(infoPtr, nItem == -1 ? infoPtr->nFocusedItem : nItem, &lvItem);
2958 return oldFocus != infoPtr->nFocusedItem;
2961 /* Helper function for LISTVIEW_ShiftIndices *only* */
2962 static INT shift_item(LISTVIEW_INFO *infoPtr, INT nShiftItem, INT nItem, INT direction)
2964 if (nShiftItem < nItem) return nShiftItem;
2966 if (nShiftItem > nItem) return nShiftItem + direction;
2968 if (direction > 0) return nShiftItem + direction;
2970 return min(nShiftItem, infoPtr->nItemCount - 1);
2975 * Updates the various indices after an item has been inserted or deleted.
2978 * [I] infoPtr : valid pointer to the listview structure
2979 * [I] nItem : item index
2980 * [I] direction : Direction of shift, +1 or -1.
2985 static void LISTVIEW_ShiftIndices(LISTVIEW_INFO *infoPtr, INT nItem, INT direction)
2990 /* temporarily disable change notification while shifting items */
2991 bOldChange = infoPtr->bDoChangeNotify;
2992 infoPtr->bDoChangeNotify = FALSE;
2994 TRACE("Shifting %iu, %i steps\n", nItem, direction);
2996 ranges_shift(infoPtr->selectionRanges, nItem, direction, infoPtr->nItemCount);
2998 assert(abs(direction) == 1);
3000 infoPtr->nSelectionMark = shift_item(infoPtr, infoPtr->nSelectionMark, nItem, direction);
3002 nNewFocus = shift_item(infoPtr, infoPtr->nFocusedItem, nItem, direction);
3003 if (nNewFocus != infoPtr->nFocusedItem)
3004 LISTVIEW_SetItemFocus(infoPtr, nNewFocus);
3006 /* But we are not supposed to modify nHotItem! */
3008 infoPtr->bDoChangeNotify = bOldChange;
3014 * Adds a block of selections.
3017 * [I] infoPtr : valid pointer to the listview structure
3018 * [I] nItem : item index
3021 * Whether the window is still valid.
3023 static BOOL LISTVIEW_AddGroupSelection(LISTVIEW_INFO *infoPtr, INT nItem)
3025 INT nFirst = min(infoPtr->nSelectionMark, nItem);
3026 INT nLast = max(infoPtr->nSelectionMark, nItem);
3027 HWND hwndSelf = infoPtr->hwndSelf;
3028 NMLVODSTATECHANGE nmlv;
3033 /* Temporarily disable change notification
3034 * If the control is LVS_OWNERDATA, we need to send
3035 * only one LVN_ODSTATECHANGED notification.
3036 * See MSDN documentation for LVN_ITEMCHANGED.
3038 bOldChange = infoPtr->bDoChangeNotify;
3039 if (infoPtr->dwStyle & LVS_OWNERDATA) infoPtr->bDoChangeNotify = FALSE;
3041 if (nFirst == -1) nFirst = nItem;
3043 item.state = LVIS_SELECTED;
3044 item.stateMask = LVIS_SELECTED;
3046 for (i = nFirst; i <= nLast; i++)
3047 LISTVIEW_SetItemState(infoPtr,i,&item);
3049 ZeroMemory(&nmlv, sizeof(nmlv));
3050 nmlv.iFrom = nFirst;
3053 nmlv.uOldState = item.state;
3055 notify_hdr(infoPtr, LVN_ODSTATECHANGED, (LPNMHDR)&nmlv);
3056 if (!IsWindow(hwndSelf))
3058 infoPtr->bDoChangeNotify = bOldChange;
3065 * Sets a single group selection.
3068 * [I] infoPtr : valid pointer to the listview structure
3069 * [I] nItem : item index
3074 static void LISTVIEW_SetGroupSelection(LISTVIEW_INFO *infoPtr, INT nItem)
3076 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
3081 if (!(selection = ranges_create(100))) return;
3083 item.state = LVIS_SELECTED;
3084 item.stateMask = LVIS_SELECTED;
3086 if ((uView == LVS_LIST) || (uView == LVS_REPORT))
3088 if (infoPtr->nSelectionMark == -1)
3090 infoPtr->nSelectionMark = nItem;
3091 ranges_additem(selection, nItem);
3097 sel.lower = min(infoPtr->nSelectionMark, nItem);
3098 sel.upper = max(infoPtr->nSelectionMark, nItem) + 1;
3099 ranges_add(selection, sel);
3104 RECT rcItem, rcSel, rcSelMark;
3107 rcItem.left = LVIR_BOUNDS;
3108 if (!LISTVIEW_GetItemRect(infoPtr, nItem, &rcItem)) return;
3109 rcSelMark.left = LVIR_BOUNDS;
3110 if (!LISTVIEW_GetItemRect(infoPtr, infoPtr->nSelectionMark, &rcSelMark)) return;
3111 UnionRect(&rcSel, &rcItem, &rcSelMark);
3112 iterator_frameditems(&i, infoPtr, &rcSel);
3113 while(iterator_next(&i))
3115 LISTVIEW_GetItemPosition(infoPtr, i.nItem, &ptItem);
3116 if (PtInRect(&rcSel, ptItem)) ranges_additem(selection, i.nItem);
3118 iterator_destroy(&i);
3121 LISTVIEW_DeselectAllSkipItems(infoPtr, selection);
3122 iterator_rangesitems(&i, selection);
3123 while(iterator_next(&i))
3124 LISTVIEW_SetItemState(infoPtr, i.nItem, &item);
3125 /* this will also destroy the selection */
3126 iterator_destroy(&i);
3128 LISTVIEW_SetItemFocus(infoPtr, nItem);
3133 * Sets a single selection.
3136 * [I] infoPtr : valid pointer to the listview structure
3137 * [I] nItem : item index
3142 static void LISTVIEW_SetSelection(LISTVIEW_INFO *infoPtr, INT nItem)
3146 TRACE("nItem=%d\n", nItem);
3148 LISTVIEW_DeselectAllSkipItem(infoPtr, nItem);
3150 lvItem.state = LVIS_FOCUSED | LVIS_SELECTED;
3151 lvItem.stateMask = LVIS_FOCUSED | LVIS_SELECTED;
3152 LISTVIEW_SetItemState(infoPtr, nItem, &lvItem);
3154 infoPtr->nSelectionMark = nItem;
3159 * Set selection(s) with keyboard.
3162 * [I] infoPtr : valid pointer to the listview structure
3163 * [I] nItem : item index
3166 * SUCCESS : TRUE (needs to be repainted)
3167 * FAILURE : FALSE (nothing has changed)
3169 static BOOL LISTVIEW_KeySelection(LISTVIEW_INFO *infoPtr, INT nItem)
3171 /* FIXME: pass in the state */
3172 WORD wShift = HIWORD(GetKeyState(VK_SHIFT));
3173 WORD wCtrl = HIWORD(GetKeyState(VK_CONTROL));
3174 BOOL bResult = FALSE;
3176 if ((nItem >= 0) && (nItem < infoPtr->nItemCount))
3178 if (infoPtr->dwStyle & LVS_SINGLESEL)
3181 LISTVIEW_SetSelection(infoPtr, nItem);
3188 LISTVIEW_SetGroupSelection(infoPtr, nItem);
3192 bResult = LISTVIEW_SetItemFocus(infoPtr, nItem);
3197 LISTVIEW_SetSelection(infoPtr, nItem);
3200 LISTVIEW_EnsureVisible(infoPtr, nItem, FALSE);
3203 UpdateWindow(infoPtr->hwndSelf); /* update client area */
3207 static BOOL LISTVIEW_GetItemAtPt(LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem, POINT pt)
3209 LVHITTESTINFO lvHitTestInfo;
3211 ZeroMemory(&lvHitTestInfo, sizeof(lvHitTestInfo));
3212 lvHitTestInfo.pt.x = pt.x;
3213 lvHitTestInfo.pt.y = pt.y;
3215 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
3217 lpLVItem->mask = LVIF_PARAM;
3218 lpLVItem->iItem = lvHitTestInfo.iItem;
3219 lpLVItem->iSubItem = 0;
3221 return LISTVIEW_GetItemT(infoPtr, lpLVItem, TRUE);
3226 * Called when the mouse is being actively tracked and has hovered for a specified
3230 * [I] infoPtr : valid pointer to the listview structure
3231 * [I] fwKeys : key indicator
3232 * [I] x,y : mouse position
3235 * 0 if the message was processed, non-zero if there was an error
3238 * LVS_EX_TRACKSELECT: An item is automatically selected when the cursor remains
3239 * over the item for a certain period of time.
3242 static LRESULT LISTVIEW_MouseHover(LISTVIEW_INFO *infoPtr, WORD fwKyes, INT x, INT y)
3244 if (infoPtr->dwLvExStyle & LVS_EX_TRACKSELECT)
3252 if (LISTVIEW_GetItemAtPt(infoPtr, &item, pt))
3253 LISTVIEW_SetSelection(infoPtr, item.iItem);
3261 * Called whenever WM_MOUSEMOVE is received.
3264 * [I] infoPtr : valid pointer to the listview structure
3265 * [I] fwKeys : key indicator
3266 * [I] x,y : mouse position
3269 * 0 if the message is processed, non-zero if there was an error
3271 static LRESULT LISTVIEW_MouseMove(LISTVIEW_INFO *infoPtr, WORD fwKeys, INT x, INT y)
3273 TRACKMOUSEEVENT trackinfo;
3275 if (!(fwKeys & MK_LBUTTON))
3276 infoPtr->bLButtonDown = FALSE;
3278 if (infoPtr->bLButtonDown && DragDetect(infoPtr->hwndSelf, infoPtr->ptClickPos))
3280 LVHITTESTINFO lvHitTestInfo;
3283 lvHitTestInfo.pt = infoPtr->ptClickPos;
3284 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, TRUE);
3286 ZeroMemory(&nmlv, sizeof(nmlv));
3287 nmlv.iItem = lvHitTestInfo.iItem;
3288 nmlv.ptAction = infoPtr->ptClickPos;
3290 notify_listview(infoPtr, LVN_BEGINDRAG, &nmlv);
3295 infoPtr->bLButtonDown = FALSE;
3297 /* see if we are supposed to be tracking mouse hovering */
3298 if(infoPtr->dwLvExStyle & LVS_EX_TRACKSELECT) {
3299 /* fill in the trackinfo struct */
3300 trackinfo.cbSize = sizeof(TRACKMOUSEEVENT);
3301 trackinfo.dwFlags = TME_QUERY;
3302 trackinfo.hwndTrack = infoPtr->hwndSelf;
3303 trackinfo.dwHoverTime = infoPtr->dwHoverTime;
3305 /* see if we are already tracking this hwnd */
3306 _TrackMouseEvent(&trackinfo);
3308 if(!(trackinfo.dwFlags & TME_HOVER)) {
3309 trackinfo.dwFlags = TME_HOVER;
3311 /* call TRACKMOUSEEVENT so we receive WM_MOUSEHOVER messages */
3312 _TrackMouseEvent(&trackinfo);
3321 * Tests wheather the item is assignable to a list with style lStyle
3323 static inline BOOL is_assignable_item(const LVITEMW *lpLVItem, LONG lStyle)
3325 if ( (lpLVItem->mask & LVIF_TEXT) &&
3326 (lpLVItem->pszText == LPSTR_TEXTCALLBACKW) &&
3327 (lStyle & (LVS_SORTASCENDING | LVS_SORTDESCENDING)) ) return FALSE;
3335 * Helper for LISTVIEW_SetItemT *only*: sets item attributes.
3338 * [I] infoPtr : valid pointer to the listview structure
3339 * [I] lpLVItem : valid pointer to new item atttributes
3340 * [I] isNew : the item being set is being inserted
3341 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
3342 * [O] bChanged : will be set to TRUE if the item really changed
3348 static BOOL set_main_item(LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem, BOOL isNew, BOOL isW, BOOL *bChanged)
3350 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
3358 assert(lpLVItem->iItem >= 0 && lpLVItem->iItem < infoPtr->nItemCount);
3360 if (lpLVItem->mask == 0) return TRUE;
3362 if (infoPtr->dwStyle & LVS_OWNERDATA)
3364 /* a virtual listview we stores only selection and focus */
3365 if (lpLVItem->mask & ~LVIF_STATE)
3371 HDPA hdpaSubItems = (HDPA)DPA_GetPtr(infoPtr->hdpaItems, lpLVItem->iItem);
3372 lpItem = (ITEM_INFO *)DPA_GetPtr(hdpaSubItems, 0);
3376 /* we need to get the lParam and state of the item */
3377 item.iItem = lpLVItem->iItem;
3378 item.iSubItem = lpLVItem->iSubItem;
3379 item.mask = LVIF_STATE | LVIF_PARAM;
3380 item.stateMask = ~0;
3383 if (!isNew && !LISTVIEW_GetItemW(infoPtr, &item)) return FALSE;
3385 TRACE("oldState=%x, newState=%x\n", item.state, lpLVItem->state);
3386 /* determine what fields will change */
3387 if ((lpLVItem->mask & LVIF_STATE) && ((item.state ^ lpLVItem->state) & lpLVItem->stateMask & ~infoPtr->uCallbackMask))
3388 uChanged |= LVIF_STATE;
3390 if ((lpLVItem->mask & LVIF_IMAGE) && (lpItem->hdr.iImage != lpLVItem->iImage))
3391 uChanged |= LVIF_IMAGE;
3393 if ((lpLVItem->mask & LVIF_PARAM) && (lpItem->lParam != lpLVItem->lParam))
3394 uChanged |= LVIF_PARAM;
3396 if ((lpLVItem->mask & LVIF_INDENT) && (lpItem->iIndent != lpLVItem->iIndent))
3397 uChanged |= LVIF_INDENT;
3399 if ((lpLVItem->mask & LVIF_TEXT) && textcmpWT(lpItem->hdr.pszText, lpLVItem->pszText, isW))
3400 uChanged |= LVIF_TEXT;
3402 TRACE("uChanged=0x%x\n", uChanged);
3403 if (!uChanged) return TRUE;
3406 ZeroMemory(&nmlv, sizeof(NMLISTVIEW));
3407 nmlv.iItem = lpLVItem->iItem;
3408 nmlv.uNewState = (item.state & ~lpLVItem->stateMask) | (lpLVItem->state & lpLVItem->stateMask);
3409 nmlv.uOldState = item.state;
3410 nmlv.uChanged = uChanged;
3411 nmlv.lParam = item.lParam;
3413 /* send LVN_ITEMCHANGING notification, if the item is not being inserted */
3414 /* and we are _NOT_ virtual (LVS_OWERNDATA), and change notifications */
3416 if(lpItem && !isNew && infoPtr->bDoChangeNotify)
3418 HWND hwndSelf = infoPtr->hwndSelf;
3420 if (notify_listview(infoPtr, LVN_ITEMCHANGING, &nmlv))
3422 if (!IsWindow(hwndSelf))
3426 /* copy information */
3427 if (lpLVItem->mask & LVIF_TEXT)
3428 textsetptrT(&lpItem->hdr.pszText, lpLVItem->pszText, isW);
3430 if (lpLVItem->mask & LVIF_IMAGE)
3431 lpItem->hdr.iImage = lpLVItem->iImage;
3433 if (lpLVItem->mask & LVIF_PARAM)
3434 lpItem->lParam = lpLVItem->lParam;
3436 if (lpLVItem->mask & LVIF_INDENT)
3437 lpItem->iIndent = lpLVItem->iIndent;
3439 if (uChanged & LVIF_STATE)
3441 if (lpItem && (lpLVItem->stateMask & ~infoPtr->uCallbackMask & ~(LVIS_FOCUSED | LVIS_SELECTED)))
3443 lpItem->state &= ~lpLVItem->stateMask;
3444 lpItem->state |= (lpLVItem->state & lpLVItem->stateMask);
3446 if (lpLVItem->state & lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_SELECTED)
3448 if (infoPtr->dwStyle & LVS_SINGLESEL) LISTVIEW_DeselectAllSkipItem(infoPtr, lpLVItem->iItem);
3449 ranges_additem(infoPtr->selectionRanges, lpLVItem->iItem);
3451 else if (lpLVItem->stateMask & LVIS_SELECTED)
3452 ranges_delitem(infoPtr->selectionRanges, lpLVItem->iItem);
3454 /* if we are asked to change focus, and we manage it, do it */
3455 if (lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_FOCUSED)
3457 if (lpLVItem->state & LVIS_FOCUSED)
3459 LISTVIEW_SetItemFocus(infoPtr, -1);
3460 infoPtr->nFocusedItem = lpLVItem->iItem;
3461 LISTVIEW_EnsureVisible(infoPtr, lpLVItem->iItem, uView == LVS_LIST);
3463 else if (infoPtr->nFocusedItem == lpLVItem->iItem)
3464 infoPtr->nFocusedItem = -1;
3468 /* if we're inserting the item, we're done */
3469 if (isNew) return TRUE;
3471 /* send LVN_ITEMCHANGED notification */
3472 if (lpLVItem->mask & LVIF_PARAM) nmlv.lParam = lpLVItem->lParam;
3473 if (infoPtr->bDoChangeNotify) notify_listview(infoPtr, LVN_ITEMCHANGED, &nmlv);
3480 * Helper for LISTVIEW_{Set,Insert}ItemT *only*: sets subitem attributes.
3483 * [I] infoPtr : valid pointer to the listview structure
3484 * [I] lpLVItem : valid pointer to new subitem atttributes
3485 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
3486 * [O] bChanged : will be set to TRUE if the item really changed
3492 static BOOL set_sub_item(LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem, BOOL isW, BOOL *bChanged)
3495 SUBITEM_INFO *lpSubItem;
3497 /* we do not support subitems for virtual listviews */
3498 if (infoPtr->dwStyle & LVS_OWNERDATA) return FALSE;
3500 /* set subitem only if column is present */
3501 if (lpLVItem->iSubItem >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
3503 /* First do some sanity checks */
3504 if (lpLVItem->mask & ~(LVIF_TEXT | LVIF_IMAGE)) return FALSE;
3505 if (!(lpLVItem->mask & (LVIF_TEXT | LVIF_IMAGE))) return TRUE;
3507 /* get the subitem structure, and create it if not there */
3508 hdpaSubItems = (HDPA)DPA_GetPtr(infoPtr->hdpaItems, lpLVItem->iItem);
3509 assert (hdpaSubItems);
3511 lpSubItem = LISTVIEW_GetSubItemPtr(hdpaSubItems, lpLVItem->iSubItem);
3514 SUBITEM_INFO *tmpSubItem;
3517 lpSubItem = (SUBITEM_INFO *)Alloc(sizeof(SUBITEM_INFO));
3518 if (!lpSubItem) return FALSE;
3519 /* we could binary search here, if need be...*/
3520 for (i = 1; i < DPA_GetPtrCount(hdpaSubItems); i++)
3522 tmpSubItem = (SUBITEM_INFO *)DPA_GetPtr(hdpaSubItems, i);
3523 if (tmpSubItem->iSubItem > lpLVItem->iSubItem) break;
3525 if (DPA_InsertPtr(hdpaSubItems, i, lpSubItem) == -1)
3530 lpSubItem->iSubItem = lpLVItem->iSubItem;
3531 lpSubItem->hdr.iImage = I_IMAGECALLBACK;
3535 if (lpLVItem->mask & LVIF_IMAGE)
3536 if (lpSubItem->hdr.iImage != lpLVItem->iImage)
3538 lpSubItem->hdr.iImage = lpLVItem->iImage;
3542 if (lpLVItem->mask & LVIF_TEXT)
3543 if (lpSubItem->hdr.pszText != lpLVItem->pszText)
3545 textsetptrT(&lpSubItem->hdr.pszText, lpLVItem->pszText, isW);
3554 * Sets item attributes.
3557 * [I] infoPtr : valid pointer to the listview structure
3558 * [I] lpLVItem : new item atttributes
3559 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
3565 static BOOL LISTVIEW_SetItemT(LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem, BOOL isW)
3567 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
3568 HWND hwndSelf = infoPtr->hwndSelf;
3569 LPWSTR pszText = NULL;
3570 BOOL bResult, bChanged = FALSE;
3572 TRACE("(lpLVItem=%s, isW=%d)\n", debuglvitem_t(lpLVItem, isW), isW);
3574 if (!lpLVItem || lpLVItem->iItem < 0 || lpLVItem->iItem >= infoPtr->nItemCount)
3577 /* For efficiency, we transform the lpLVItem->pszText to Unicode here */
3578 if ((lpLVItem->mask & LVIF_TEXT) && is_textW(lpLVItem->pszText))
3580 pszText = lpLVItem->pszText;
3581 ((LVITEMW *)lpLVItem)->pszText = textdupTtoW(lpLVItem->pszText, isW);
3584 /* actually set the fields */
3585 if (!is_assignable_item(lpLVItem, infoPtr->dwStyle)) return FALSE;
3587 if (lpLVItem->iSubItem)
3588 bResult = set_sub_item(infoPtr, lpLVItem, TRUE, &bChanged);
3590 bResult = set_main_item(infoPtr, lpLVItem, FALSE, TRUE, &bChanged);
3591 if (!IsWindow(hwndSelf))
3594 /* redraw item, if necessary */
3595 if (bChanged && !infoPtr->bIsDrawing)
3597 /* this little optimization eliminates some nasty flicker */
3598 if ( uView == LVS_REPORT && !(infoPtr->dwStyle & LVS_OWNERDRAWFIXED) &&
3599 (!(infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) || lpLVItem->iSubItem) )
3600 LISTVIEW_InvalidateSubItem(infoPtr, lpLVItem->iItem, lpLVItem->iSubItem);
3602 LISTVIEW_InvalidateItem(infoPtr, lpLVItem->iItem);
3607 textfreeT(lpLVItem->pszText, isW);
3608 ((LVITEMW *)lpLVItem)->pszText = pszText;
3616 * Retrieves the index of the item at coordinate (0, 0) of the client area.
3619 * [I] infoPtr : valid pointer to the listview structure
3624 static INT LISTVIEW_GetTopIndex(LISTVIEW_INFO *infoPtr)
3626 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
3628 SCROLLINFO scrollInfo;
3630 scrollInfo.cbSize = sizeof(SCROLLINFO);
3631 scrollInfo.fMask = SIF_POS;
3633 if (uView == LVS_LIST)
3635 if (GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo))
3636 nItem = scrollInfo.nPos * LISTVIEW_GetCountPerColumn(infoPtr);
3638 else if (uView == LVS_REPORT)
3640 if (GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
3641 nItem = scrollInfo.nPos;
3645 if (GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
3646 nItem = LISTVIEW_GetCountPerRow(infoPtr) * (scrollInfo.nPos / infoPtr->nItemHeight);
3649 TRACE("nItem=%d\n", nItem);
3657 * Erases the background of the given rectangle
3660 * [I] infoPtr : valid pointer to the listview structure
3661 * [I] hdc : device context handle
3662 * [I] lprcBox : clipping rectangle
3668 static inline BOOL LISTVIEW_FillBkgnd(LISTVIEW_INFO *infoPtr, HDC hdc, const RECT *lprcBox)
3670 if (!infoPtr->hBkBrush) return FALSE;
3672 TRACE("(hdc=%p, lprcBox=%s, hBkBrush=%p)\n", hdc, debugrect(lprcBox), infoPtr->hBkBrush);
3674 return FillRect(hdc, lprcBox, infoPtr->hBkBrush);
3682 * [I] infoPtr : valid pointer to the listview structure
3683 * [I] hdc : device context handle
3684 * [I] nItem : item index
3685 * [I] nSubItem : subitem index
3686 * [I] pos : item position in client coordinates
3687 * [I] cdmode : custom draw mode
3693 static BOOL LISTVIEW_DrawItem(LISTVIEW_INFO *infoPtr, HDC hdc, INT nItem, INT nSubItem, POINT pos, DWORD cdmode)
3695 UINT uFormat, uView = infoPtr->dwStyle & LVS_TYPEMASK;
3696 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
3697 static const WCHAR szCallback[] = { '(', 'c', 'a', 'l', 'l', 'b', 'a', 'c', 'k', ')', 0 };
3698 DWORD cdsubitemmode = CDRF_DODEFAULT;
3699 RECT* lprcFocus, rcSelect, rcBox, rcState, rcIcon, rcLabel;
3700 NMLVCUSTOMDRAW nmlvcd;
3704 TRACE("(hdc=%p, nItem=%d, nSubItem=%d, pos=%s)\n", hdc, nItem, nSubItem, debugpoint(&pos));
3706 /* get information needed for drawing the item */
3707 lvItem.mask = LVIF_TEXT | LVIF_IMAGE;
3708 if (nSubItem == 0) lvItem.mask |= LVIF_STATE | LVIF_PARAM;
3709 if (uView == LVS_REPORT) lvItem.mask |= LVIF_INDENT;
3710 lvItem.stateMask = LVIS_SELECTED | LVIS_FOCUSED | LVIS_STATEIMAGEMASK;
3711 lvItem.iItem = nItem;
3712 lvItem.iSubItem = nSubItem;
3715 lvItem.cchTextMax = DISP_TEXT_SIZE;
3716 lvItem.pszText = szDispText;
3717 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return FALSE;
3718 if (nSubItem > 0 && (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT))
3719 lvItem.state = LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED);
3720 if (lvItem.pszText == LPSTR_TEXTCALLBACKW) lvItem.pszText = (LPWSTR)szCallback;
3721 TRACE(" lvItem=%s\n", debuglvitem_t(&lvItem, TRUE));
3723 /* now check if we need to update the focus rectangle */
3724 lprcFocus = infoPtr->bFocus && (lvItem.state & LVIS_FOCUSED) ? &infoPtr->rcFocus : 0;
3726 if (!lprcFocus) lvItem.state &= ~LVIS_FOCUSED;
3727 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, &rcBox, &rcState, &rcIcon, &rcLabel);
3728 OffsetRect(&rcBox, pos.x, pos.y);
3729 OffsetRect(&rcState, pos.x, pos.y);
3730 OffsetRect(&rcIcon, pos.x, pos.y);
3731 OffsetRect(&rcLabel, pos.x, pos.y);
3732 TRACE(" rcBox=%s, rcState=%s, rcIcon=%s. rcLabel=%s\n",
3733 debugrect(&rcBox), debugrect(&rcState), debugrect(&rcIcon), debugrect(&rcLabel));
3735 /* fill in the custom draw structure */
3736 customdraw_fill(&nmlvcd, infoPtr, hdc, &rcBox, &lvItem);
3738 if (nSubItem > 0) cdmode = infoPtr->cditemmode;
3739 if (cdmode & CDRF_NOTIFYITEMDRAW)
3740 cdsubitemmode = notify_customdraw(infoPtr, CDDS_PREPAINT, &nmlvcd);
3741 if (nSubItem == 0) infoPtr->cditemmode = cdsubitemmode;
3742 if (cdsubitemmode & CDRF_SKIPDEFAULT) goto postpaint;
3743 /* we have to send a CDDS_SUBITEM customdraw explicitly for subitem 0 */
3744 if (nSubItem == 0 && cdsubitemmode == CDRF_NOTIFYITEMDRAW)
3746 cdsubitemmode = notify_customdraw(infoPtr, CDDS_SUBITEM | CDDS_ITEMPREPAINT, &nmlvcd);
3747 if (cdsubitemmode & CDRF_SKIPDEFAULT) goto postpaint;
3749 if (nSubItem == 0 || (cdmode & CDRF_NOTIFYITEMDRAW))
3750 prepaint_setup(infoPtr, hdc, &nmlvcd);
3752 /* in full row select, subitems, will just use main item's colors */
3753 if (nSubItem && uView == LVS_REPORT && (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT))
3754 nmlvcd.clrTextBk = CLR_NONE;
3757 if (infoPtr->himlState && !IsRectEmpty(&rcState))
3759 UINT uStateImage = STATEIMAGEINDEX(lvItem.state);
3762 TRACE("uStateImage=%d\n", uStateImage);
3763 ImageList_Draw(infoPtr->himlState, uStateImage - 1, hdc, rcState.left, rcState.top, ILD_NORMAL);
3768 himl = (uView == LVS_ICON ? infoPtr->himlNormal : infoPtr->himlSmall);
3769 if (himl && lvItem.iImage >= 0 && !IsRectEmpty(&rcIcon))
3771 TRACE("iImage=%d\n", lvItem.iImage);
3772 ImageList_Draw(himl, lvItem.iImage, hdc, rcIcon.left, rcIcon.top,
3773 (lvItem.state & LVIS_SELECTED) && (infoPtr->bFocus) ? ILD_SELECTED : ILD_NORMAL);
3776 /* Don't bother painting item being edited */
3777 if (infoPtr->hwndEdit && nItem == infoPtr->nEditLabelItem && nSubItem == 0) goto postpaint;
3779 /* draw the selection background, if we're drawing the main item */
3783 if (uView == LVS_REPORT && (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT))
3784 rcSelect.right = rcBox.right;
3786 if (nmlvcd.clrTextBk != CLR_NONE)
3787 ExtTextOutW(hdc, rcSelect.left, rcSelect.top, ETO_OPAQUE, &rcSelect, 0, 0, 0);
3788 if(lprcFocus) *lprcFocus = rcSelect;
3791 /* figure out the text drawing flags */
3792 uFormat = (uView == LVS_ICON ? (lprcFocus ? LV_FL_DT_FLAGS : LV_ML_DT_FLAGS) : LV_SL_DT_FLAGS);
3793 if (uView == LVS_ICON)
3794 uFormat = (lprcFocus ? LV_FL_DT_FLAGS : LV_ML_DT_FLAGS);
3797 switch (LISTVIEW_GetColumnInfo(infoPtr, nSubItem)->fmt & LVCFMT_JUSTIFYMASK)
3799 case LVCFMT_RIGHT: uFormat |= DT_RIGHT; break;
3800 case LVCFMT_CENTER: uFormat |= DT_CENTER; break;
3801 default: uFormat |= DT_LEFT;
3804 if (!(uFormat & (DT_RIGHT | DT_CENTER)))
3806 if (himl && lvItem.iImage >= 0 && !IsRectEmpty(&rcIcon)) rcLabel.left += IMAGE_PADDING;
3807 else rcLabel.left += LABEL_HOR_PADDING;
3809 else if (uFormat & DT_RIGHT) rcLabel.right -= LABEL_HOR_PADDING;
3810 DrawTextW(hdc, lvItem.pszText, -1, &rcLabel, uFormat);
3813 if (cdsubitemmode & CDRF_NOTIFYPOSTPAINT)
3814 notify_postpaint(infoPtr, &nmlvcd);
3820 * Draws listview items when in owner draw mode.
3823 * [I] infoPtr : valid pointer to the listview structure
3824 * [I] hdc : device context handle
3829 static void LISTVIEW_RefreshOwnerDraw(LISTVIEW_INFO *infoPtr, ITERATOR *i, HDC hdc, DWORD cdmode)
3831 UINT uID = (UINT)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
3832 DWORD cditemmode = CDRF_DODEFAULT;
3833 NMLVCUSTOMDRAW nmlvcd;
3834 POINT Origin, Position;
3840 ZeroMemory(&dis, sizeof(dis));
3842 /* Get scroll info once before loop */
3843 LISTVIEW_GetOrigin(infoPtr, &Origin);
3845 /* iterate through the invalidated rows */
3846 while(iterator_next(i))
3848 item.iItem = i->nItem;
3850 item.mask = LVIF_PARAM | LVIF_STATE;
3851 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
3852 if (!LISTVIEW_GetItemW(infoPtr, &item)) continue;
3854 dis.CtlType = ODT_LISTVIEW;
3856 dis.itemID = item.iItem;
3857 dis.itemAction = ODA_DRAWENTIRE;
3859 if (item.state & LVIS_SELECTED) dis.itemState |= ODS_SELECTED;
3860 if (infoPtr->bFocus && (item.state & LVIS_FOCUSED)) dis.itemState |= ODS_FOCUS;
3861 dis.hwndItem = infoPtr->hwndSelf;
3863 LISTVIEW_GetItemOrigin(infoPtr, dis.itemID, &Position);
3864 dis.rcItem.left = Position.x + Origin.x;
3865 dis.rcItem.right = dis.rcItem.left + infoPtr->nItemWidth;
3866 dis.rcItem.top = Position.y + Origin.y;
3867 dis.rcItem.bottom = dis.rcItem.top + infoPtr->nItemHeight;
3868 dis.itemData = item.lParam;
3870 TRACE("item=%s, rcItem=%s\n", debuglvitem_t(&item, TRUE), debugrect(&dis.rcItem));
3873 * Even if we do not send the CDRF_NOTIFYITEMDRAW we need to fill the nmlvcd
3874 * structure for the rest. of the paint cycle
3876 customdraw_fill(&nmlvcd, infoPtr, hdc, &dis.rcItem, &item);
3877 if (cdmode & CDRF_NOTIFYITEMDRAW)
3878 cditemmode = notify_customdraw(infoPtr, CDDS_PREPAINT, &nmlvcd);
3880 if (!(cditemmode & CDRF_SKIPDEFAULT))
3882 prepaint_setup (infoPtr, hdc, &nmlvcd);
3883 SendMessageW(infoPtr->hwndNotify, WM_DRAWITEM, dis.CtlID, (LPARAM)&dis);
3886 if (cditemmode & CDRF_NOTIFYPOSTPAINT)
3887 notify_postpaint(infoPtr, &nmlvcd);
3893 * Draws listview items when in report display mode.
3896 * [I] infoPtr : valid pointer to the listview structure
3897 * [I] hdc : device context handle
3898 * [I] cdmode : custom draw mode
3903 static void LISTVIEW_RefreshReport(LISTVIEW_INFO *infoPtr, ITERATOR *i, HDC hdc, DWORD cdmode)
3906 RECT rcClip, rcItem;
3907 POINT Origin, Position;
3913 /* figure out what to draw */
3914 rgntype = GetClipBox(hdc, &rcClip);
3915 if (rgntype == NULLREGION) return;
3917 /* Get scroll info once before loop */
3918 LISTVIEW_GetOrigin(infoPtr, &Origin);
3920 /* narrow down the columns we need to paint */
3921 for(colRange.lower = 0; colRange.lower < DPA_GetPtrCount(infoPtr->hdpaColumns); colRange.lower++)
3923 LISTVIEW_GetHeaderRect(infoPtr, colRange.lower, &rcItem);
3924 if (rcItem.right + Origin.x >= rcClip.left) break;
3926 for(colRange.upper = DPA_GetPtrCount(infoPtr->hdpaColumns); colRange.upper > 0; colRange.upper--)
3928 LISTVIEW_GetHeaderRect(infoPtr, colRange.upper - 1, &rcItem);
3929 if (rcItem.left + Origin.x < rcClip.right) break;
3931 iterator_rangeitems(&j, colRange);
3933 /* in full row select, we _have_ to draw the main item */
3934 if (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT)
3937 /* iterate through the invalidated rows */
3938 while(iterator_next(i))
3940 /* iterate through the invalidated columns */
3941 while(iterator_next(&j))
3943 LISTVIEW_GetItemOrigin(infoPtr, i->nItem, &Position);
3944 Position.x += Origin.x;
3945 Position.y += Origin.y;
3947 if (rgntype == COMPLEXREGION && !((infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) && j.nItem == 0))
3949 LISTVIEW_GetHeaderRect(infoPtr, j.nItem, &rcItem);
3951 rcItem.bottom = infoPtr->nItemHeight;
3952 OffsetRect(&rcItem, Position.x, Position.y);
3953 if (!RectVisible(hdc, &rcItem)) continue;
3956 LISTVIEW_DrawItem(infoPtr, hdc, i->nItem, j.nItem, Position, cdmode);
3959 iterator_destroy(&j);
3964 * Draws listview items when in list display mode.
3967 * [I] infoPtr : valid pointer to the listview structure
3968 * [I] hdc : device context handle
3969 * [I] cdmode : custom draw mode
3974 static void LISTVIEW_RefreshList(LISTVIEW_INFO *infoPtr, ITERATOR *i, HDC hdc, DWORD cdmode)
3976 POINT Origin, Position;
3978 /* Get scroll info once before loop */
3979 LISTVIEW_GetOrigin(infoPtr, &Origin);
3981 while(iterator_prev(i))
3983 LISTVIEW_GetItemOrigin(infoPtr, i->nItem, &Position);
3984 Position.x += Origin.x;
3985 Position.y += Origin.y;
3987 LISTVIEW_DrawItem(infoPtr, hdc, i->nItem, 0, Position, cdmode);
3994 * Draws listview items.
3997 * [I] infoPtr : valid pointer to the listview structure
3998 * [I] hdc : device context handle
4003 static void LISTVIEW_Refresh(LISTVIEW_INFO *infoPtr, HDC hdc)
4005 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
4006 COLORREF oldTextColor, oldClrTextBk, oldClrText;
4007 NMLVCUSTOMDRAW nmlvcd;
4014 LISTVIEW_DUMP(infoPtr);
4016 infoPtr->bIsDrawing = TRUE;
4018 /* save dc values we're gonna trash while drawing */
4019 hOldFont = SelectObject(hdc, infoPtr->hFont);
4020 oldBkMode = GetBkMode(hdc);
4021 infoPtr->clrTextBkDefault = GetBkColor(hdc);
4022 oldTextColor = GetTextColor(hdc);
4024 oldClrTextBk = infoPtr->clrTextBk;
4025 oldClrText = infoPtr->clrText;
4027 infoPtr->cditemmode = CDRF_DODEFAULT;
4029 GetClientRect(infoPtr->hwndSelf, &rcClient);
4030 customdraw_fill(&nmlvcd, infoPtr, hdc, &rcClient, 0);
4031 cdmode = notify_customdraw(infoPtr, CDDS_PREPAINT, &nmlvcd);
4032 if (cdmode & CDRF_SKIPDEFAULT) goto enddraw;
4033 prepaint_setup(infoPtr, hdc, &nmlvcd);
4035 /* Use these colors to draw the items */
4036 infoPtr->clrTextBk = nmlvcd.clrTextBk;
4037 infoPtr->clrText = nmlvcd.clrText;
4039 /* nothing to draw */
4040 if(infoPtr->nItemCount == 0) goto enddraw;
4042 /* figure out what we need to draw */
4043 iterator_visibleitems(&i, infoPtr, hdc);
4045 /* send cache hint notification */
4046 if (infoPtr->dwStyle & LVS_OWNERDATA)
4048 RANGE range = iterator_range(&i);
4051 ZeroMemory(&nmlv, sizeof(NMLVCACHEHINT));
4052 nmlv.iFrom = range.lower;
4053 nmlv.iTo = range.upper - 1;
4054 notify_hdr(infoPtr, LVN_ODCACHEHINT, &nmlv.hdr);
4057 if ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (uView == LVS_REPORT))
4058 LISTVIEW_RefreshOwnerDraw(infoPtr, &i, hdc, cdmode);
4061 if (uView == LVS_REPORT)
4062 LISTVIEW_RefreshReport(infoPtr, &i, hdc, cdmode);
4063 else /* LVS_LIST, LVS_ICON or LVS_SMALLICON */
4064 LISTVIEW_RefreshList(infoPtr, &i, hdc, cdmode);
4066 /* if we have a focus rect, draw it */
4067 if (infoPtr->bFocus)
4068 DrawFocusRect(hdc, &infoPtr->rcFocus);
4070 iterator_destroy(&i);
4073 if (cdmode & CDRF_NOTIFYPOSTPAINT)
4074 notify_postpaint(infoPtr, &nmlvcd);
4076 infoPtr->clrTextBk = oldClrTextBk;
4077 infoPtr->clrText = oldClrText;
4079 SelectObject(hdc, hOldFont);
4080 SetBkMode(hdc, oldBkMode);
4081 SetBkColor(hdc, infoPtr->clrTextBkDefault);
4082 SetTextColor(hdc, oldTextColor);
4083 infoPtr->bIsDrawing = FALSE;
4089 * Calculates the approximate width and height of a given number of items.
4092 * [I] infoPtr : valid pointer to the listview structure
4093 * [I] nItemCount : number of items
4094 * [I] wWidth : width
4095 * [I] wHeight : height
4098 * Returns a DWORD. The width in the low word and the height in high word.
4100 static DWORD LISTVIEW_ApproximateViewRect(LISTVIEW_INFO *infoPtr, INT nItemCount,
4101 WORD wWidth, WORD wHeight)
4103 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
4104 INT nItemCountPerColumn = 1;
4105 INT nColumnCount = 0;
4106 DWORD dwViewRect = 0;
4108 if (nItemCount == -1)
4109 nItemCount = infoPtr->nItemCount;
4111 if (uView == LVS_LIST)
4113 if (wHeight == 0xFFFF)
4115 /* use current height */
4116 wHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
4119 if (wHeight < infoPtr->nItemHeight)
4120 wHeight = infoPtr->nItemHeight;
4124 if (infoPtr->nItemHeight > 0)
4126 nItemCountPerColumn = wHeight / infoPtr->nItemHeight;
4127 if (nItemCountPerColumn == 0)
4128 nItemCountPerColumn = 1;
4130 if (nItemCount % nItemCountPerColumn != 0)
4131 nColumnCount = nItemCount / nItemCountPerColumn;
4133 nColumnCount = nItemCount / nItemCountPerColumn + 1;
4137 /* Microsoft padding magic */
4138 wHeight = nItemCountPerColumn * infoPtr->nItemHeight + 2;
4139 wWidth = nColumnCount * infoPtr->nItemWidth + 2;
4141 dwViewRect = MAKELONG(wWidth, wHeight);
4143 else if (uView == LVS_REPORT)
4147 if (infoPtr->nItemCount > 0)
4149 LISTVIEW_GetItemBox(infoPtr, 0, &rcBox);
4150 wWidth = rcBox.right - rcBox.left;
4151 wHeight = (rcBox.bottom - rcBox.top) * nItemCount;
4155 /* use current height and width */
4156 if (wHeight == 0xffff)
4157 wHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
4158 if (wWidth == 0xffff)
4159 wWidth = infoPtr->rcList.right - infoPtr->rcList.left;
4162 dwViewRect = MAKELONG(wWidth, wHeight);
4164 else if (uView == LVS_SMALLICON)
4165 FIXME("uView == LVS_SMALLICON: not implemented\n");
4166 else if (uView == LVS_ICON)
4167 FIXME("uView == LVS_ICON: not implemented\n");
4175 * Create a drag image list for the specified item.
4178 * [I] infoPtr : valid pointer to the listview structure
4179 * [I] iItem : index of item
4180 * [O] lppt : Upperr-left corner of the image
4183 * Returns a handle to the image list if successful, NULL otherwise.
4185 static HIMAGELIST LISTVIEW_CreateDragImage(LISTVIEW_INFO *infoPtr, INT iItem, LPPOINT lppt)
4191 HBITMAP hbmp, hOldbmp;
4192 HIMAGELIST dragList = 0;
4193 TRACE("iItem=%d Count=%d\n", iItem, infoPtr->nItemCount);
4195 if (iItem < 0 || iItem >= infoPtr->nItemCount)
4198 rcItem.left = LVIR_BOUNDS;
4199 if (!LISTVIEW_GetItemRect(infoPtr, iItem, &rcItem))
4202 lppt->x = rcItem.left;
4203 lppt->y = rcItem.top;
4205 size.cx = rcItem.right - rcItem.left;
4206 size.cy = rcItem.bottom - rcItem.top;
4208 hdcOrig = GetDC(infoPtr->hwndSelf);
4209 hdc = CreateCompatibleDC(hdcOrig);
4210 hbmp = CreateCompatibleBitmap(hdcOrig, size.cx, size.cy);
4211 hOldbmp = SelectObject(hdc, hbmp);
4213 rcItem.left = rcItem.top = 0;
4214 rcItem.right = size.cx;
4215 rcItem.bottom = size.cy;
4216 FillRect(hdc, &rcItem, infoPtr->hBkBrush);
4219 if (LISTVIEW_DrawItem(infoPtr, hdc, iItem, 0, pos, infoPtr->cditemmode))
4221 dragList = ImageList_Create(size.cx, size.cy, ILC_COLOR, 10, 10);
4222 SelectObject(hdc, hOldbmp);
4223 ImageList_Add(dragList, hbmp, 0);
4226 SelectObject(hdc, hOldbmp);
4230 ReleaseDC(infoPtr->hwndSelf, hdcOrig);
4232 TRACE("ret=%p\n", dragList);
4240 * Removes all listview items and subitems.
4243 * [I] infoPtr : valid pointer to the listview structure
4249 static BOOL LISTVIEW_DeleteAllItems(LISTVIEW_INFO *infoPtr)
4252 HDPA hdpaSubItems = NULL;
4259 /* we do it directly, to avoid notifications */
4260 ranges_clear(infoPtr->selectionRanges);
4261 infoPtr->nSelectionMark = -1;
4262 infoPtr->nFocusedItem = -1;
4263 SetRectEmpty(&infoPtr->rcFocus);
4264 /* But we are supposed to leave nHotItem as is! */
4267 /* send LVN_DELETEALLITEMS notification */
4268 ZeroMemory(&nmlv, sizeof(NMLISTVIEW));
4270 bSuppress = notify_listview(infoPtr, LVN_DELETEALLITEMS, &nmlv);
4272 for (i = infoPtr->nItemCount - 1; i >= 0; i--)
4274 /* send LVN_DELETEITEM notification, if not suppressed */
4275 if (!bSuppress) notify_deleteitem(infoPtr, i);
4276 if (!(infoPtr->dwStyle & LVS_OWNERDATA))
4278 hdpaSubItems = (HDPA)DPA_GetPtr(infoPtr->hdpaItems, i);
4279 for (j = 0; j < DPA_GetPtrCount(hdpaSubItems); j++)
4281 hdrItem = (ITEMHDR *)DPA_GetPtr(hdpaSubItems, j);
4282 if (is_textW(hdrItem->pszText)) Free(hdrItem->pszText);
4285 DPA_Destroy(hdpaSubItems);
4286 DPA_DeletePtr(infoPtr->hdpaItems, i);
4288 DPA_DeletePtr(infoPtr->hdpaPosX, i);
4289 DPA_DeletePtr(infoPtr->hdpaPosY, i);
4290 infoPtr->nItemCount --;
4293 LISTVIEW_UpdateScroll(infoPtr);
4295 LISTVIEW_InvalidateList(infoPtr);
4302 * Scrolls, and updates the columns, when a column is changing width.
4305 * [I] infoPtr : valid pointer to the listview structure
4306 * [I] nColumn : column to scroll
4307 * [I] dx : amount of scroll, in pixels
4312 static void LISTVIEW_ScrollColumns(LISTVIEW_INFO *infoPtr, INT nColumn, INT dx)
4314 COLUMN_INFO *lpColumnInfo;
4319 if (nColumn < 0 || DPA_GetPtrCount(infoPtr->hdpaColumns) < 1) return;
4320 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, min(nColumn, DPA_GetPtrCount(infoPtr->hdpaColumns) - 1));
4321 rcCol = lpColumnInfo->rcHeader;
4322 if (nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns))
4323 rcCol.left = rcCol.right;
4325 /* ajust the other columns */
4326 for (nCol = nColumn; nCol < DPA_GetPtrCount(infoPtr->hdpaColumns); nCol++)
4328 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, nCol);
4329 lpColumnInfo->rcHeader.left += dx;
4330 lpColumnInfo->rcHeader.right += dx;
4333 /* do not update screen if not in report mode */
4334 if (!is_redrawing(infoPtr) || (infoPtr->dwStyle & LVS_TYPEMASK) != LVS_REPORT) return;
4336 /* if we have a focus, must first erase the focus rect */
4337 if (infoPtr->bFocus) LISTVIEW_ShowFocusRect(infoPtr, FALSE);
4339 /* Need to reset the item width when inserting a new column */
4340 infoPtr->nItemWidth += dx;
4342 LISTVIEW_UpdateScroll(infoPtr);
4343 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
4345 /* scroll to cover the deleted column, and invalidate for redraw */
4346 rcOld = infoPtr->rcList;
4347 rcOld.left = ptOrigin.x + rcCol.left + dx;
4348 ScrollWindowEx(infoPtr->hwndSelf, dx, 0, &rcOld, &rcOld, 0, 0, SW_ERASE | SW_INVALIDATE);
4350 /* we can restore focus now */
4351 if (infoPtr->bFocus) LISTVIEW_ShowFocusRect(infoPtr, TRUE);
4356 * Removes a column from the listview control.
4359 * [I] infoPtr : valid pointer to the listview structure
4360 * [I] nColumn : column index
4366 static BOOL LISTVIEW_DeleteColumn(LISTVIEW_INFO *infoPtr, INT nColumn)
4370 TRACE("nColumn=%d\n", nColumn);
4372 if (nColumn < 0 || DPA_GetPtrCount(infoPtr->hdpaColumns) == 0
4373 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
4375 /* While the MSDN specifically says that column zero should not be deleted,
4376 what actually happens is that the column itself is deleted but no items or subitems
4380 LISTVIEW_GetHeaderRect(infoPtr, nColumn, &rcCol);
4382 if (!Header_DeleteItem(infoPtr->hwndHeader, nColumn))
4385 Free(DPA_GetPtr(infoPtr->hdpaColumns, nColumn));
4386 DPA_DeletePtr(infoPtr->hdpaColumns, nColumn);
4388 if (!(infoPtr->dwStyle & LVS_OWNERDATA) && nColumn)
4390 SUBITEM_INFO *lpSubItem, *lpDelItem;
4392 INT nItem, nSubItem, i;
4394 for (nItem = 0; nItem < infoPtr->nItemCount; nItem++)
4396 hdpaSubItems = (HDPA)DPA_GetPtr(infoPtr->hdpaItems, nItem);
4399 for (i = 1; i < DPA_GetPtrCount(hdpaSubItems); i++)
4401 lpSubItem = (SUBITEM_INFO *)DPA_GetPtr(hdpaSubItems, i);
4402 if (lpSubItem->iSubItem == nColumn)
4405 lpDelItem = lpSubItem;
4407 else if (lpSubItem->iSubItem > nColumn)
4409 lpSubItem->iSubItem--;
4413 /* if we found our subitem, zapp it */
4417 if (is_textW(lpDelItem->hdr.pszText))
4418 Free(lpDelItem->hdr.pszText);
4423 /* free dpa memory */
4424 DPA_DeletePtr(hdpaSubItems, nSubItem);
4429 /* update the other column info */
4430 if(DPA_GetPtrCount(infoPtr->hdpaColumns) == 0)
4431 LISTVIEW_InvalidateList(infoPtr);
4433 LISTVIEW_ScrollColumns(infoPtr, nColumn, -(rcCol.right - rcCol.left));
4440 * Invalidates the listview after an item's insertion or deletion.
4443 * [I] infoPtr : valid pointer to the listview structure
4444 * [I] nItem : item index
4445 * [I] dir : -1 if deleting, 1 if inserting
4450 static void LISTVIEW_ScrollOnInsert(LISTVIEW_INFO *infoPtr, INT nItem, INT dir)
4452 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
4453 INT nPerCol, nItemCol, nItemRow;
4457 /* if we don't refresh, what's the point of scrolling? */
4458 if (!is_redrawing(infoPtr)) return;
4460 assert (abs(dir) == 1);
4462 /* arrange icons if autoarrange is on */
4463 if (is_autoarrange(infoPtr))
4465 BOOL arrange = TRUE;
4466 if (dir < 0 && nItem >= infoPtr->nItemCount) arrange = FALSE;
4467 if (dir > 0 && nItem == infoPtr->nItemCount - 1) arrange = FALSE;
4468 if (arrange) LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
4471 /* scrollbars need updating */
4472 LISTVIEW_UpdateScroll(infoPtr);
4474 /* figure out the item's position */
4475 if (uView == LVS_REPORT)
4476 nPerCol = infoPtr->nItemCount + 1;
4477 else if (uView == LVS_LIST)
4478 nPerCol = LISTVIEW_GetCountPerColumn(infoPtr);
4479 else /* LVS_ICON, or LVS_SMALLICON */
4482 nItemCol = nItem / nPerCol;
4483 nItemRow = nItem % nPerCol;
4484 LISTVIEW_GetOrigin(infoPtr, &Origin);
4486 /* move the items below up a slot */
4487 rcScroll.left = nItemCol * infoPtr->nItemWidth;
4488 rcScroll.top = nItemRow * infoPtr->nItemHeight;
4489 rcScroll.right = rcScroll.left + infoPtr->nItemWidth;
4490 rcScroll.bottom = nPerCol * infoPtr->nItemHeight;
4491 OffsetRect(&rcScroll, Origin.x, Origin.y);
4492 TRACE("rcScroll=%s, dx=%d\n", debugrect(&rcScroll), dir * infoPtr->nItemHeight);
4493 if (IntersectRect(&rcScroll, &rcScroll, &infoPtr->rcList))
4495 TRACE("Scrolling rcScroll=%s, rcList=%s\n", debugrect(&rcScroll), debugrect(&infoPtr->rcList));
4496 ScrollWindowEx(infoPtr->hwndSelf, 0, dir * infoPtr->nItemHeight,
4497 &rcScroll, &rcScroll, 0, 0, SW_ERASE | SW_INVALIDATE);
4500 /* report has only that column, so we're done */
4501 if (uView == LVS_REPORT) return;
4503 /* now for LISTs, we have to deal with the columns to the right */
4504 rcScroll.left = (nItemCol + 1) * infoPtr->nItemWidth;
4506 rcScroll.right = (infoPtr->nItemCount / nPerCol + 1) * infoPtr->nItemWidth;
4507 rcScroll.bottom = nPerCol * infoPtr->nItemHeight;
4508 OffsetRect(&rcScroll, Origin.x, Origin.y);
4509 if (IntersectRect(&rcScroll, &rcScroll, &infoPtr->rcList))
4510 ScrollWindowEx(infoPtr->hwndSelf, 0, dir * infoPtr->nItemHeight,
4511 &rcScroll, &rcScroll, 0, 0, SW_ERASE | SW_INVALIDATE);
4516 * Removes an item from the listview control.
4519 * [I] infoPtr : valid pointer to the listview structure
4520 * [I] nItem : item index
4526 static BOOL LISTVIEW_DeleteItem(LISTVIEW_INFO *infoPtr, INT nItem)
4528 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
4531 TRACE("(nItem=%d)\n", nItem);
4533 if (nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
4535 /* remove selection, and focus */
4537 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
4538 LISTVIEW_SetItemState(infoPtr, nItem, &item);
4540 /* send LVN_DELETEITEM notification. */
4541 if (!notify_deleteitem(infoPtr, nItem)) return FALSE;
4543 /* we need to do this here, because we'll be deleting stuff */
4544 if (uView == LVS_SMALLICON || uView == LVS_ICON)
4545 LISTVIEW_InvalidateItem(infoPtr, nItem);
4547 if (!(infoPtr->dwStyle & LVS_OWNERDATA))
4553 hdpaSubItems = (HDPA)DPA_DeletePtr(infoPtr->hdpaItems, nItem);
4554 for (i = 0; i < DPA_GetPtrCount(hdpaSubItems); i++)
4556 hdrItem = (ITEMHDR *)DPA_GetPtr(hdpaSubItems, i);
4557 if (is_textW(hdrItem->pszText)) Free(hdrItem->pszText);
4560 DPA_Destroy(hdpaSubItems);
4563 if (uView == LVS_SMALLICON || uView == LVS_ICON)
4565 DPA_DeletePtr(infoPtr->hdpaPosX, nItem);
4566 DPA_DeletePtr(infoPtr->hdpaPosY, nItem);
4569 infoPtr->nItemCount--;
4570 LISTVIEW_ShiftIndices(infoPtr, nItem, -1);
4572 /* now is the invalidation fun */
4573 LISTVIEW_ScrollOnInsert(infoPtr, nItem, -1);
4580 * Callback implementation for editlabel control
4583 * [I] infoPtr : valid pointer to the listview structure
4584 * [I] pszText : modified text
4585 * [I] isW : TRUE if psxText is Unicode, FALSE if it's ANSI
4591 static BOOL LISTVIEW_EndEditLabelT(LISTVIEW_INFO *infoPtr, LPWSTR pszText, BOOL isW)
4593 HWND hwndSelf = infoPtr->hwndSelf;
4594 NMLVDISPINFOW dispInfo;
4596 TRACE("(pszText=%s, isW=%d)\n", debugtext_t(pszText, isW), isW);
4598 ZeroMemory(&dispInfo, sizeof(dispInfo));
4599 dispInfo.item.mask = LVIF_PARAM | LVIF_STATE;
4600 dispInfo.item.iItem = infoPtr->nEditLabelItem;
4601 dispInfo.item.iSubItem = 0;
4602 dispInfo.item.stateMask = ~0;
4603 if (!LISTVIEW_GetItemW(infoPtr, &dispInfo.item)) return FALSE;
4604 /* add the text from the edit in */
4605 dispInfo.item.mask |= LVIF_TEXT;
4606 dispInfo.item.pszText = pszText;
4607 dispInfo.item.cchTextMax = textlenT(pszText, isW);
4609 /* Do we need to update the Item Text */
4610 if (!notify_dispinfoT(infoPtr, LVN_ENDLABELEDITW, &dispInfo, isW)) return FALSE;
4611 if (!IsWindow(hwndSelf))
4613 if (!pszText) return TRUE;
4615 if (!(infoPtr->dwStyle & LVS_OWNERDATA))
4617 HDPA hdpaSubItems = (HDPA)DPA_GetPtr(infoPtr->hdpaItems, infoPtr->nEditLabelItem);
4618 ITEM_INFO* lpItem = (ITEM_INFO *)DPA_GetPtr(hdpaSubItems, 0);
4619 if (lpItem && lpItem->hdr.pszText == LPSTR_TEXTCALLBACKW)
4621 LISTVIEW_InvalidateItem(infoPtr, infoPtr->nEditLabelItem);
4626 ZeroMemory(&dispInfo, sizeof(dispInfo));
4627 dispInfo.item.mask = LVIF_TEXT;
4628 dispInfo.item.iItem = infoPtr->nEditLabelItem;
4629 dispInfo.item.iSubItem = 0;
4630 dispInfo.item.pszText = pszText;
4631 dispInfo.item.cchTextMax = textlenT(pszText, isW);
4632 return LISTVIEW_SetItemT(infoPtr, &dispInfo.item, isW);
4637 * Begin in place editing of specified list view item
4640 * [I] infoPtr : valid pointer to the listview structure
4641 * [I] nItem : item index
4642 * [I] isW : TRUE if it's a Unicode req, FALSE if ASCII
4648 static HWND LISTVIEW_EditLabelT(LISTVIEW_INFO *infoPtr, INT nItem, BOOL isW)
4650 WCHAR szDispText[DISP_TEXT_SIZE] = { 0 };
4651 NMLVDISPINFOW dispInfo;
4653 HWND hwndSelf = infoPtr->hwndSelf;
4655 TRACE("(nItem=%d, isW=%d)\n", nItem, isW);
4657 if (~infoPtr->dwStyle & LVS_EDITLABELS) return 0;
4658 if (nItem < 0 || nItem >= infoPtr->nItemCount) return 0;
4660 infoPtr->nEditLabelItem = nItem;
4662 /* Is the EditBox still there, if so remove it */
4663 if(infoPtr->hwndEdit != 0)
4665 SetFocus(infoPtr->hwndSelf);
4666 infoPtr->hwndEdit = 0;
4669 LISTVIEW_SetSelection(infoPtr, nItem);
4670 LISTVIEW_SetItemFocus(infoPtr, nItem);
4671 LISTVIEW_InvalidateItem(infoPtr, nItem);
4673 rect.left = LVIR_LABEL;
4674 if (!LISTVIEW_GetItemRect(infoPtr, nItem, &rect)) return 0;
4676 ZeroMemory(&dispInfo, sizeof(dispInfo));
4677 dispInfo.item.mask = LVIF_PARAM | LVIF_STATE | LVIF_TEXT;
4678 dispInfo.item.iItem = nItem;
4679 dispInfo.item.iSubItem = 0;
4680 dispInfo.item.stateMask = ~0;
4681 dispInfo.item.pszText = szDispText;
4682 dispInfo.item.cchTextMax = DISP_TEXT_SIZE;
4683 if (!LISTVIEW_GetItemT(infoPtr, &dispInfo.item, isW)) return 0;
4685 infoPtr->hwndEdit = CreateEditLabelT(infoPtr, dispInfo.item.pszText, WS_VISIBLE,
4686 rect.left-2, rect.top-1, 0, rect.bottom - rect.top+2, isW);
4687 if (!infoPtr->hwndEdit) return 0;
4689 if (notify_dispinfoT(infoPtr, LVN_BEGINLABELEDITW, &dispInfo, isW))
4691 if (!IsWindow(hwndSelf))
4693 SendMessageW(infoPtr->hwndEdit, WM_CLOSE, 0, 0);
4694 infoPtr->hwndEdit = 0;
4698 ShowWindow(infoPtr->hwndEdit, SW_NORMAL);
4699 SetFocus(infoPtr->hwndEdit);
4700 SendMessageW(infoPtr->hwndEdit, EM_SETSEL, 0, -1);
4701 return infoPtr->hwndEdit;
4707 * Ensures the specified item is visible, scrolling into view if necessary.
4710 * [I] infoPtr : valid pointer to the listview structure
4711 * [I] nItem : item index
4712 * [I] bPartial : partially or entirely visible
4718 static BOOL LISTVIEW_EnsureVisible(LISTVIEW_INFO *infoPtr, INT nItem, BOOL bPartial)
4720 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
4721 INT nScrollPosHeight = 0;
4722 INT nScrollPosWidth = 0;
4723 INT nHorzAdjust = 0;
4724 INT nVertAdjust = 0;
4727 RECT rcItem, rcTemp;
4729 rcItem.left = LVIR_BOUNDS;
4730 if (!LISTVIEW_GetItemRect(infoPtr, nItem, &rcItem)) return FALSE;
4732 if (bPartial && IntersectRect(&rcTemp, &infoPtr->rcList, &rcItem)) return TRUE;
4734 if (rcItem.left < infoPtr->rcList.left || rcItem.right > infoPtr->rcList.right)
4736 /* scroll left/right, but in LVS_REPORT mode */
4737 if (uView == LVS_LIST)
4738 nScrollPosWidth = infoPtr->nItemWidth;
4739 else if ((uView == LVS_SMALLICON) || (uView == LVS_ICON))
4740 nScrollPosWidth = 1;
4742 if (rcItem.left < infoPtr->rcList.left)
4745 if (uView != LVS_REPORT) nHorzDiff = rcItem.left - infoPtr->rcList.left;
4750 if (uView != LVS_REPORT) nHorzDiff = rcItem.right - infoPtr->rcList.right;
4754 if (rcItem.top < infoPtr->rcList.top || rcItem.bottom > infoPtr->rcList.bottom)
4756 /* scroll up/down, but not in LVS_LIST mode */
4757 if (uView == LVS_REPORT)
4758 nScrollPosHeight = infoPtr->nItemHeight;
4759 else if ((uView == LVS_ICON) || (uView == LVS_SMALLICON))
4760 nScrollPosHeight = 1;
4762 if (rcItem.top < infoPtr->rcList.top)
4765 if (uView != LVS_LIST) nVertDiff = rcItem.top - infoPtr->rcList.top;
4770 if (uView != LVS_LIST) nVertDiff = rcItem.bottom - infoPtr->rcList.bottom;
4774 if (!nScrollPosWidth && !nScrollPosHeight) return TRUE;
4776 if (nScrollPosWidth)
4778 INT diff = nHorzDiff / nScrollPosWidth;
4779 if (nHorzDiff % nScrollPosWidth) diff += nHorzAdjust;
4780 LISTVIEW_HScroll(infoPtr, SB_INTERNAL, diff, 0);
4783 if (nScrollPosHeight)
4785 INT diff = nVertDiff / nScrollPosHeight;
4786 if (nVertDiff % nScrollPosHeight) diff += nVertAdjust;
4787 LISTVIEW_VScroll(infoPtr, SB_INTERNAL, diff, 0);
4795 * Searches for an item with specific characteristics.
4798 * [I] hwnd : window handle
4799 * [I] nStart : base item index
4800 * [I] lpFindInfo : item information to look for
4803 * SUCCESS : index of item
4806 static INT LISTVIEW_FindItemW(LISTVIEW_INFO *infoPtr, INT nStart,
4807 const LVFINDINFOW *lpFindInfo)
4809 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
4810 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
4811 BOOL bWrap = FALSE, bNearest = FALSE;
4812 INT nItem = nStart + 1, nLast = infoPtr->nItemCount, nNearestItem = -1;
4813 ULONG xdist, ydist, dist, mindist = 0x7fffffff;
4814 POINT Position, Destination;
4817 if (!lpFindInfo || nItem < 0) return -1;
4820 if (lpFindInfo->flags & (LVFI_STRING | LVFI_PARTIAL))
4822 lvItem.mask |= LVIF_TEXT;
4823 lvItem.pszText = szDispText;
4824 lvItem.cchTextMax = DISP_TEXT_SIZE;
4827 if (lpFindInfo->flags & LVFI_WRAP)
4830 if ((lpFindInfo->flags & LVFI_NEARESTXY) &&
4831 (uView == LVS_ICON || uView ==LVS_SMALLICON))
4836 LISTVIEW_GetOrigin(infoPtr, &Origin);
4837 Destination.x = lpFindInfo->pt.x - Origin.x;
4838 Destination.y = lpFindInfo->pt.y - Origin.y;
4839 switch(lpFindInfo->vkDirection)
4841 case VK_DOWN: Destination.y += infoPtr->nItemHeight; break;
4842 case VK_UP: Destination.y -= infoPtr->nItemHeight; break;
4843 case VK_RIGHT: Destination.x += infoPtr->nItemWidth; break;
4844 case VK_LEFT: Destination.x -= infoPtr->nItemWidth; break;
4845 case VK_HOME: Destination.x = Destination.y = 0; break;
4846 case VK_NEXT: Destination.y += infoPtr->rcList.bottom - infoPtr->rcList.top; break;
4847 case VK_PRIOR: Destination.y -= infoPtr->rcList.bottom - infoPtr->rcList.top; break;
4849 LISTVIEW_GetAreaRect(infoPtr, &rcArea);
4850 Destination.x = rcArea.right;
4851 Destination.y = rcArea.bottom;
4853 default: ERR("Unknown vkDirection=%d\n", lpFindInfo->vkDirection);
4857 else Destination.x = Destination.y = 0;
4859 /* if LVFI_PARAM is specified, all other flags are ignored */
4860 if (lpFindInfo->flags & LVFI_PARAM)
4862 lvItem.mask |= LVIF_PARAM;
4864 lvItem.mask &= ~LVIF_TEXT;
4868 for (; nItem < nLast; nItem++)
4870 lvItem.iItem = nItem;
4871 lvItem.iSubItem = 0;
4872 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) continue;
4874 if (lvItem.mask & LVIF_PARAM)
4876 if (lpFindInfo->lParam == lvItem.lParam)
4882 if (lvItem.mask & LVIF_TEXT)
4884 if (lpFindInfo->flags & LVFI_PARTIAL)
4886 if (strstrW(lvItem.pszText, lpFindInfo->psz) == NULL) continue;
4890 if (lstrcmpW(lvItem.pszText, lpFindInfo->psz) != 0) continue;
4894 if (!bNearest) return nItem;
4896 /* This is very inefficient. To do a good job here,
4897 * we need a sorted array of (x,y) item positions */
4898 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
4900 /* compute the distance^2 to the destination */
4901 xdist = Destination.x - Position.x;
4902 ydist = Destination.y - Position.y;
4903 dist = xdist * xdist + ydist * ydist;
4905 /* remember the distance, and item if it's closer */
4909 nNearestItem = nItem;
4916 nLast = min(nStart + 1, infoPtr->nItemCount);
4921 return nNearestItem;
4926 * Searches for an item with specific characteristics.
4929 * [I] hwnd : window handle
4930 * [I] nStart : base item index
4931 * [I] lpFindInfo : item information to look for
4934 * SUCCESS : index of item
4937 static INT LISTVIEW_FindItemA(LISTVIEW_INFO *infoPtr, INT nStart,
4938 const LVFINDINFOA *lpFindInfo)
4940 BOOL hasText = lpFindInfo->flags & (LVFI_STRING | LVFI_PARTIAL);
4944 memcpy(&fiw, lpFindInfo, sizeof(fiw));
4945 if (hasText) fiw.psz = textdupTtoW((LPCWSTR)lpFindInfo->psz, FALSE);
4946 res = LISTVIEW_FindItemW(infoPtr, nStart, &fiw);
4947 if (hasText) textfreeT((LPWSTR)fiw.psz, FALSE);
4953 * Retrieves the background image of the listview control.
4956 * [I] infoPtr : valid pointer to the listview structure
4957 * [O] lpBkImage : background image attributes
4963 /* static BOOL LISTVIEW_GetBkImage(LISTVIEW_INFO *infoPtr, LPLVBKIMAGE lpBkImage) */
4965 /* FIXME (listview, "empty stub!\n"); */
4971 * Retrieves column attributes.
4974 * [I] infoPtr : valid pointer to the listview structure
4975 * [I] nColumn : column index
4976 * [IO] lpColumn : column information
4977 * [I] isW : if TRUE, then lpColumn is a LPLVCOLUMNW
4978 * otherwise it is in fact a LPLVCOLUMNA
4984 static BOOL LISTVIEW_GetColumnT(LISTVIEW_INFO *infoPtr, INT nColumn, LPLVCOLUMNW lpColumn, BOOL isW)
4986 COLUMN_INFO *lpColumnInfo;
4989 if (!lpColumn || nColumn < 0 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
4990 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, nColumn);
4992 /* initialize memory */
4993 ZeroMemory(&hdi, sizeof(hdi));
4995 if (lpColumn->mask & LVCF_TEXT)
4997 hdi.mask |= HDI_TEXT;
4998 hdi.pszText = lpColumn->pszText;
4999 hdi.cchTextMax = lpColumn->cchTextMax;
5002 if (lpColumn->mask & LVCF_IMAGE)
5003 hdi.mask |= HDI_IMAGE;
5005 if (lpColumn->mask & LVCF_ORDER)
5006 hdi.mask |= HDI_ORDER;
5008 if (!SendMessageW(infoPtr->hwndHeader, isW ? HDM_GETITEMW : HDM_GETITEMA, nColumn, (LPARAM)&hdi)) return FALSE;
5010 if (lpColumn->mask & LVCF_FMT)
5011 lpColumn->fmt = lpColumnInfo->fmt;
5013 if (lpColumn->mask & LVCF_WIDTH)
5014 lpColumn->cx = lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left;
5016 if (lpColumn->mask & LVCF_IMAGE)
5017 lpColumn->iImage = hdi.iImage;
5019 if (lpColumn->mask & LVCF_ORDER)
5020 lpColumn->iOrder = hdi.iOrder;
5026 static BOOL LISTVIEW_GetColumnOrderArray(LISTVIEW_INFO *infoPtr, INT iCount, LPINT lpiArray)
5033 /* FIXME: little hack */
5034 for (i = 0; i < iCount; i++)
5042 * Retrieves the column width.
5045 * [I] infoPtr : valid pointer to the listview structure
5046 * [I] int : column index
5049 * SUCCESS : column width
5052 static INT LISTVIEW_GetColumnWidth(LISTVIEW_INFO *infoPtr, INT nColumn)
5054 INT nColumnWidth = 0;
5057 TRACE("nColumn=%d\n", nColumn);
5059 /* we have a 'column' in LIST and REPORT mode only */
5060 switch(infoPtr->dwStyle & LVS_TYPEMASK)
5063 nColumnWidth = infoPtr->nItemWidth;
5066 if (nColumn < 0 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return 0;
5067 LISTVIEW_GetHeaderRect(infoPtr, nColumn, &rcHeader);
5068 nColumnWidth = rcHeader.right - rcHeader.left;
5072 TRACE("nColumnWidth=%d\n", nColumnWidth);
5073 return nColumnWidth;
5078 * In list or report display mode, retrieves the number of items that can fit
5079 * vertically in the visible area. In icon or small icon display mode,
5080 * retrieves the total number of visible items.
5083 * [I] infoPtr : valid pointer to the listview structure
5086 * Number of fully visible items.
5088 static INT LISTVIEW_GetCountPerPage(LISTVIEW_INFO *infoPtr)
5090 switch (infoPtr->dwStyle & LVS_TYPEMASK)
5094 return infoPtr->nItemCount;
5096 return LISTVIEW_GetCountPerColumn(infoPtr);
5098 return LISTVIEW_GetCountPerRow(infoPtr) * LISTVIEW_GetCountPerColumn(infoPtr);
5106 * Retrieves an image list handle.
5109 * [I] infoPtr : valid pointer to the listview structure
5110 * [I] nImageList : image list identifier
5113 * SUCCESS : image list handle
5116 static HIMAGELIST LISTVIEW_GetImageList(LISTVIEW_INFO *infoPtr, INT nImageList)
5120 case LVSIL_NORMAL: return infoPtr->himlNormal;
5121 case LVSIL_SMALL: return infoPtr->himlSmall;
5122 case LVSIL_STATE: return infoPtr->himlState;
5127 /* LISTVIEW_GetISearchString */
5131 * Retrieves item attributes.
5134 * [I] hwnd : window handle
5135 * [IO] lpLVItem : item info
5136 * [I] isW : if TRUE, then lpLVItem is a LPLVITEMW,
5137 * if FALSE, the lpLVItem is a LPLVITEMA.
5140 * This is the internal 'GetItem' interface -- it tries to
5141 * be smart and avoid text copies, if possible, by modifying
5142 * lpLVItem->pszText to point to the text string. Please note
5143 * that this is not always possible (e.g. OWNERDATA), so on
5144 * entry you *must* supply valid values for pszText, and cchTextMax.
5145 * The only difference to the documented interface is that upon
5146 * return, you should use *only* the lpLVItem->pszText, rather than
5147 * the buffer pointer you provided on input. Most code already does
5148 * that, so it's not a problem.
5149 * For the two cases when the text must be copied (that is,
5150 * for LVM_GETITEM, and LVM_GETITEMTEXT), use LISTVIEW_GetItemExtT.
5156 static BOOL LISTVIEW_GetItemT(LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem, BOOL isW)
5158 ITEMHDR callbackHdr = { LPSTR_TEXTCALLBACKW, I_IMAGECALLBACK };
5159 NMLVDISPINFOW dispInfo;
5165 TRACE("(lpLVItem=%s, isW=%d)\n", debuglvitem_t(lpLVItem, isW), isW);
5167 if (!lpLVItem || lpLVItem->iItem < 0 || lpLVItem->iItem >= infoPtr->nItemCount)
5170 if (lpLVItem->mask == 0) return TRUE;
5172 /* make a local copy */
5173 isubitem = lpLVItem->iSubItem;
5175 /* a quick optimization if all we're asked is the focus state
5176 * these queries are worth optimising since they are common,
5177 * and can be answered in constant time, without the heavy accesses */
5178 if ( (lpLVItem->mask == LVIF_STATE) && (lpLVItem->stateMask == LVIS_FOCUSED) &&
5179 !(infoPtr->uCallbackMask & LVIS_FOCUSED) )
5181 lpLVItem->state = 0;
5182 if (infoPtr->nFocusedItem == lpLVItem->iItem)
5183 lpLVItem->state |= LVIS_FOCUSED;
5187 ZeroMemory(&dispInfo, sizeof(dispInfo));
5189 /* if the app stores all the data, handle it separately */
5190 if (infoPtr->dwStyle & LVS_OWNERDATA)
5192 dispInfo.item.state = 0;
5194 /* apprently, we should not callback for lParam in LVS_OWNERDATA */
5195 if ((lpLVItem->mask & ~(LVIF_STATE | LVIF_PARAM)) || infoPtr->uCallbackMask)
5197 /* NOTE: copy only fields which we _know_ are initialized, some apps
5198 * depend on the uninitialized fields being 0 */
5199 dispInfo.item.mask = lpLVItem->mask & ~LVIF_PARAM;
5200 dispInfo.item.iItem = lpLVItem->iItem;
5201 dispInfo.item.iSubItem = isubitem;
5202 if (lpLVItem->mask & LVIF_TEXT)
5204 dispInfo.item.pszText = lpLVItem->pszText;
5205 dispInfo.item.cchTextMax = lpLVItem->cchTextMax;
5207 if (lpLVItem->mask & LVIF_STATE)
5208 dispInfo.item.stateMask = lpLVItem->stateMask & infoPtr->uCallbackMask;
5209 notify_dispinfoT(infoPtr, LVN_GETDISPINFOW, &dispInfo, isW);
5210 dispInfo.item.stateMask = lpLVItem->stateMask;
5211 if (lpLVItem->mask & (LVIF_GROUPID|LVIF_COLUMNS))
5213 /* full size structure expected - _WIN32IE >= 0x560 */
5214 *lpLVItem = dispInfo.item;
5216 else if (lpLVItem->mask & LVIF_INDENT)
5218 /* indent member expected - _WIN32IE >= 0x300 */
5219 memcpy(lpLVItem, &dispInfo.item, offsetof( LVITEMW, iGroupId ));
5223 /* minimal structure expected */
5224 memcpy(lpLVItem, &dispInfo.item, offsetof( LVITEMW, iIndent ));
5226 TRACE(" getdispinfo(1):lpLVItem=%s\n", debuglvitem_t(lpLVItem, isW));
5229 /* make sure lParam is zeroed out */
5230 if (lpLVItem->mask & LVIF_PARAM) lpLVItem->lParam = 0;
5232 /* we store only a little state, so if we're not asked, we're done */
5233 if (!(lpLVItem->mask & LVIF_STATE) || isubitem) return TRUE;
5235 /* if focus is handled by us, report it */
5236 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_FOCUSED )
5238 lpLVItem->state &= ~LVIS_FOCUSED;
5239 if (infoPtr->nFocusedItem == lpLVItem->iItem)
5240 lpLVItem->state |= LVIS_FOCUSED;
5243 /* and do the same for selection, if we handle it */
5244 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_SELECTED )
5246 lpLVItem->state &= ~LVIS_SELECTED;
5247 if (ranges_contain(infoPtr->selectionRanges, lpLVItem->iItem))
5248 lpLVItem->state |= LVIS_SELECTED;
5254 /* find the item and subitem structures before we proceed */
5255 hdpaSubItems = (HDPA)DPA_GetPtr(infoPtr->hdpaItems, lpLVItem->iItem);
5256 lpItem = (ITEM_INFO *)DPA_GetPtr(hdpaSubItems, 0);
5261 SUBITEM_INFO *lpSubItem = LISTVIEW_GetSubItemPtr(hdpaSubItems, isubitem);
5262 pItemHdr = lpSubItem ? &lpSubItem->hdr : &callbackHdr;
5265 WARN(" iSubItem invalid (%08x), ignored.\n", isubitem);
5270 pItemHdr = &lpItem->hdr;
5272 /* Do we need to query the state from the app? */
5273 if ((lpLVItem->mask & LVIF_STATE) && infoPtr->uCallbackMask && isubitem == 0)
5275 dispInfo.item.mask |= LVIF_STATE;
5276 dispInfo.item.stateMask = infoPtr->uCallbackMask;
5279 /* Do we need to enquire about the image? */
5280 if ((lpLVItem->mask & LVIF_IMAGE) && pItemHdr->iImage == I_IMAGECALLBACK &&
5281 (isubitem == 0 || (infoPtr->dwLvExStyle & LVS_EX_SUBITEMIMAGES)))
5283 dispInfo.item.mask |= LVIF_IMAGE;
5284 dispInfo.item.iImage = I_IMAGECALLBACK;
5287 /* Apps depend on calling back for text if it is NULL or LPSTR_TEXTCALLBACKW */
5288 if ((lpLVItem->mask & LVIF_TEXT) && !is_textW(pItemHdr->pszText))
5290 dispInfo.item.mask |= LVIF_TEXT;
5291 dispInfo.item.pszText = lpLVItem->pszText;
5292 dispInfo.item.cchTextMax = lpLVItem->cchTextMax;
5293 if (dispInfo.item.pszText && dispInfo.item.cchTextMax > 0)
5294 *dispInfo.item.pszText = '\0';
5297 /* If we don't have all the requested info, query the application */
5298 if (dispInfo.item.mask != 0)
5300 dispInfo.item.iItem = lpLVItem->iItem;
5301 dispInfo.item.iSubItem = lpLVItem->iSubItem; /* yes: the original subitem */
5302 dispInfo.item.lParam = lpItem->lParam;
5303 notify_dispinfoT(infoPtr, LVN_GETDISPINFOW, &dispInfo, isW);
5304 TRACE(" getdispinfo(2):item=%s\n", debuglvitem_t(&dispInfo.item, isW));
5307 /* we should not store values for subitems */
5308 if (isubitem) dispInfo.item.mask &= ~LVIF_DI_SETITEM;
5310 /* Now, handle the iImage field */
5311 if (dispInfo.item.mask & LVIF_IMAGE)
5313 lpLVItem->iImage = dispInfo.item.iImage;
5314 if ((dispInfo.item.mask & LVIF_DI_SETITEM) && pItemHdr->iImage == I_IMAGECALLBACK)
5315 pItemHdr->iImage = dispInfo.item.iImage;
5317 else if (lpLVItem->mask & LVIF_IMAGE)
5319 if(isubitem == 0 || (infoPtr->dwLvExStyle & LVS_EX_SUBITEMIMAGES))
5320 lpLVItem->iImage = pItemHdr->iImage;
5322 lpLVItem->iImage = 0;
5325 /* The pszText field */
5326 if (dispInfo.item.mask & LVIF_TEXT)
5328 if ((dispInfo.item.mask & LVIF_DI_SETITEM) && pItemHdr->pszText)
5329 textsetptrT(&pItemHdr->pszText, dispInfo.item.pszText, isW);
5331 lpLVItem->pszText = dispInfo.item.pszText;
5333 else if (lpLVItem->mask & LVIF_TEXT)
5335 if (isW) lpLVItem->pszText = pItemHdr->pszText;
5336 else textcpynT(lpLVItem->pszText, isW, pItemHdr->pszText, TRUE, lpLVItem->cchTextMax);
5339 /* if this is a subitem, we're done */
5340 if (isubitem) return TRUE;
5342 /* Next is the lParam field */
5343 if (dispInfo.item.mask & LVIF_PARAM)
5345 lpLVItem->lParam = dispInfo.item.lParam;
5346 if ((dispInfo.item.mask & LVIF_DI_SETITEM))
5347 lpItem->lParam = dispInfo.item.lParam;
5349 else if (lpLVItem->mask & LVIF_PARAM)
5350 lpLVItem->lParam = lpItem->lParam;
5352 /* ... the state field (this one is different due to uCallbackmask) */
5353 if (lpLVItem->mask & LVIF_STATE)
5355 lpLVItem->state = lpItem->state;
5356 if (dispInfo.item.mask & LVIF_STATE)
5358 lpLVItem->state &= ~dispInfo.item.stateMask;
5359 lpLVItem->state |= (dispInfo.item.state & dispInfo.item.stateMask);
5361 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_FOCUSED )
5363 lpLVItem->state &= ~LVIS_FOCUSED;
5364 if (infoPtr->nFocusedItem == lpLVItem->iItem)
5365 lpLVItem->state |= LVIS_FOCUSED;
5367 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_SELECTED )
5369 lpLVItem->state &= ~LVIS_SELECTED;
5370 if (ranges_contain(infoPtr->selectionRanges, lpLVItem->iItem))
5371 lpLVItem->state |= LVIS_SELECTED;
5375 /* and last, but not least, the indent field */
5376 if (lpLVItem->mask & LVIF_INDENT)
5377 lpLVItem->iIndent = lpItem->iIndent;
5384 * Retrieves item attributes.
5387 * [I] hwnd : window handle
5388 * [IO] lpLVItem : item info
5389 * [I] isW : if TRUE, then lpLVItem is a LPLVITEMW,
5390 * if FALSE, the lpLVItem is a LPLVITEMA.
5393 * This is the external 'GetItem' interface -- it properly copies
5394 * the text in the provided buffer.
5400 static BOOL LISTVIEW_GetItemExtT(LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem, BOOL isW)
5405 if (!lpLVItem || lpLVItem->iItem < 0 || lpLVItem->iItem >= infoPtr->nItemCount)
5408 pszText = lpLVItem->pszText;
5409 bResult = LISTVIEW_GetItemT(infoPtr, lpLVItem, isW);
5410 if (bResult && lpLVItem->pszText != pszText)
5411 textcpynT(pszText, isW, lpLVItem->pszText, isW, lpLVItem->cchTextMax);
5412 lpLVItem->pszText = pszText;
5420 * Retrieves the position (upper-left) of the listview control item.
5421 * Note that for LVS_ICON style, the upper-left is that of the icon
5422 * and not the bounding box.
5425 * [I] infoPtr : valid pointer to the listview structure
5426 * [I] nItem : item index
5427 * [O] lpptPosition : coordinate information
5433 static BOOL LISTVIEW_GetItemPosition(LISTVIEW_INFO *infoPtr, INT nItem, LPPOINT lpptPosition)
5435 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
5438 TRACE("(nItem=%d, lpptPosition=%p)\n", nItem, lpptPosition);
5440 if (!lpptPosition || nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
5442 LISTVIEW_GetOrigin(infoPtr, &Origin);
5443 LISTVIEW_GetItemOrigin(infoPtr, nItem, lpptPosition);
5445 if (uView == LVS_ICON)
5447 lpptPosition->x += (infoPtr->nItemWidth - infoPtr->iconSize.cx) / 2;
5448 lpptPosition->y += ICON_TOP_PADDING;
5450 lpptPosition->x += Origin.x;
5451 lpptPosition->y += Origin.y;
5453 TRACE (" lpptPosition=%s\n", debugpoint(lpptPosition));
5460 * Retrieves the bounding rectangle for a listview control item.
5463 * [I] infoPtr : valid pointer to the listview structure
5464 * [I] nItem : item index
5465 * [IO] lprc : bounding rectangle coordinates
5466 * lprc->left specifies the portion of the item for which the bounding
5467 * rectangle will be retrieved.
5469 * LVIR_BOUNDS Returns the bounding rectangle of the entire item,
5470 * including the icon and label.
5473 * * Experiment shows that native control returns:
5474 * * width = min (48, length of text line)
5475 * * .left = position.x - (width - iconsize.cx)/2
5476 * * .right = .left + width
5477 * * height = #lines of text * ntmHeight + icon height + 8
5478 * * .top = position.y - 2
5479 * * .bottom = .top + height
5480 * * separation between items .y = itemSpacing.cy - height
5481 * * .x = itemSpacing.cx - width
5482 * LVIR_ICON Returns the bounding rectangle of the icon or small icon.
5485 * * Experiment shows that native control returns:
5486 * * width = iconSize.cx + 16
5487 * * .left = position.x - (width - iconsize.cx)/2
5488 * * .right = .left + width
5489 * * height = iconSize.cy + 4
5490 * * .top = position.y - 2
5491 * * .bottom = .top + height
5492 * * separation between items .y = itemSpacing.cy - height
5493 * * .x = itemSpacing.cx - width
5494 * LVIR_LABEL Returns the bounding rectangle of the item text.
5497 * * Experiment shows that native control returns:
5498 * * width = text length
5499 * * .left = position.x - width/2
5500 * * .right = .left + width
5501 * * height = ntmH * linecount + 2
5502 * * .top = position.y + iconSize.cy + 6
5503 * * .bottom = .top + height
5504 * * separation between items .y = itemSpacing.cy - height
5505 * * .x = itemSpacing.cx - width
5506 * LVIR_SELECTBOUNDS Returns the union of the LVIR_ICON and LVIR_LABEL
5507 * rectangles, but excludes columns in report view.
5514 * Note that the bounding rectangle of the label in the LVS_ICON view depends
5515 * upon whether the window has the focus currently and on whether the item
5516 * is the one with the focus. Ensure that the control's record of which
5517 * item has the focus agrees with the items' records.
5519 static BOOL LISTVIEW_GetItemRect(LISTVIEW_INFO *infoPtr, INT nItem, LPRECT lprc)
5521 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
5522 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
5523 BOOL doLabel = TRUE, oversizedBox = FALSE;
5524 POINT Position, Origin;
5528 TRACE("(hwnd=%p, nItem=%d, lprc=%p)\n", infoPtr->hwndSelf, nItem, lprc);
5530 if (!lprc || nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
5532 LISTVIEW_GetOrigin(infoPtr, &Origin);
5533 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
5535 /* Be smart and try to figure out the minimum we have to do */
5536 if (lprc->left == LVIR_ICON) doLabel = FALSE;
5537 if (uView == LVS_REPORT && lprc->left == LVIR_BOUNDS) doLabel = FALSE;
5538 if (uView == LVS_ICON && lprc->left != LVIR_ICON &&
5539 infoPtr->bFocus && LISTVIEW_GetItemState(infoPtr, nItem, LVIS_FOCUSED))
5540 oversizedBox = TRUE;
5542 /* get what we need from the item before hand, so we make
5543 * only one request. This can speed up things, if data
5544 * is stored on the app side */
5546 if (uView == LVS_REPORT) lvItem.mask |= LVIF_INDENT;
5547 if (doLabel) lvItem.mask |= LVIF_TEXT;
5548 lvItem.iItem = nItem;
5549 lvItem.iSubItem = 0;
5550 lvItem.pszText = szDispText;
5551 lvItem.cchTextMax = DISP_TEXT_SIZE;
5552 if (lvItem.mask && !LISTVIEW_GetItemW(infoPtr, &lvItem)) return FALSE;
5553 /* we got the state already up, simulate it here, to avoid a reget */
5554 if (uView == LVS_ICON && (lprc->left != LVIR_ICON))
5556 lvItem.mask |= LVIF_STATE;
5557 lvItem.stateMask = LVIS_FOCUSED;
5558 lvItem.state = (oversizedBox ? LVIS_FOCUSED : 0);
5561 if (uView == LVS_REPORT && (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) && lprc->left == LVIR_SELECTBOUNDS)
5562 lprc->left = LVIR_BOUNDS;
5566 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, NULL, lprc, NULL);
5570 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, NULL, NULL, lprc);
5574 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, lprc, NULL, NULL, NULL);
5577 case LVIR_SELECTBOUNDS:
5578 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, NULL, lprc, &label_rect);
5579 UnionRect(lprc, lprc, &label_rect);
5583 WARN("Unknown value: %ld\n", lprc->left);
5587 OffsetRect(lprc, Position.x + Origin.x, Position.y + Origin.y);
5589 TRACE(" rect=%s\n", debugrect(lprc));
5596 * Retrieves the spacing between listview control items.
5599 * [I] infoPtr : valid pointer to the listview structure
5600 * [IO] lprc : rectangle to receive the output
5601 * on input, lprc->top = nSubItem
5602 * lprc->left = LVIR_ICON | LVIR_BOUNDS | LVIR_LABEL
5604 * NOTE: for subItem = 0, we should return the bounds of the _entire_ item,
5605 * not only those of the first column.
5606 * Fortunately, LISTVIEW_GetItemMetrics does the right thing.
5612 static BOOL LISTVIEW_GetSubItemRect(LISTVIEW_INFO *infoPtr, INT nItem, LPRECT lprc)
5616 INT nColumn = lprc->top;
5618 if (!lprc) return FALSE;
5620 TRACE("(nItem=%d, nSubItem=%ld)\n", nItem, lprc->top);
5621 /* On WinNT, a subitem of '0' calls LISTVIEW_GetItemRect */
5623 return LISTVIEW_GetItemRect(infoPtr, nItem, lprc);
5625 if ((infoPtr->dwStyle & LVS_TYPEMASK) != LVS_REPORT) return FALSE;
5627 if (!LISTVIEW_GetItemPosition(infoPtr, nItem, &Position)) return FALSE;
5629 if (nColumn < 0 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
5632 lvItem.iItem = nItem;
5633 lvItem.iSubItem = nColumn;
5635 if (lvItem.mask && !LISTVIEW_GetItemW(infoPtr, &lvItem)) return FALSE;
5639 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, NULL, lprc, NULL);
5644 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, lprc, NULL, NULL, NULL);
5648 ERR("Unknown bounds=%ld\n", lprc->left);
5652 OffsetRect(lprc, Position.x, Position.y);
5659 * Retrieves the width of a label.
5662 * [I] infoPtr : valid pointer to the listview structure
5665 * SUCCESS : string width (in pixels)
5668 static INT LISTVIEW_GetLabelWidth(LISTVIEW_INFO *infoPtr, INT nItem)
5670 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
5673 TRACE("(nItem=%d)\n", nItem);
5675 lvItem.mask = LVIF_TEXT;
5676 lvItem.iItem = nItem;
5677 lvItem.iSubItem = 0;
5678 lvItem.pszText = szDispText;
5679 lvItem.cchTextMax = DISP_TEXT_SIZE;
5680 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return 0;
5682 return LISTVIEW_GetStringWidthT(infoPtr, lvItem.pszText, TRUE);
5687 * Retrieves the spacing between listview control items.
5690 * [I] infoPtr : valid pointer to the listview structure
5691 * [I] bSmall : flag for small or large icon
5694 * Horizontal + vertical spacing
5696 static LONG LISTVIEW_GetItemSpacing(LISTVIEW_INFO *infoPtr, BOOL bSmall)
5702 lResult = MAKELONG(infoPtr->iconSpacing.cx, infoPtr->iconSpacing.cy);
5706 if ((infoPtr->dwStyle & LVS_TYPEMASK) == LVS_ICON)
5707 lResult = MAKELONG(DEFAULT_COLUMN_WIDTH, GetSystemMetrics(SM_CXSMICON)+HEIGHT_PADDING);
5709 lResult = MAKELONG(infoPtr->nItemWidth, infoPtr->nItemHeight);
5716 * Retrieves the state of a listview control item.
5719 * [I] infoPtr : valid pointer to the listview structure
5720 * [I] nItem : item index
5721 * [I] uMask : state mask
5724 * State specified by the mask.
5726 static UINT LISTVIEW_GetItemState(LISTVIEW_INFO *infoPtr, INT nItem, UINT uMask)
5730 if (nItem < 0 || nItem >= infoPtr->nItemCount) return 0;
5732 lvItem.iItem = nItem;
5733 lvItem.iSubItem = 0;
5734 lvItem.mask = LVIF_STATE;
5735 lvItem.stateMask = uMask;
5736 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return 0;
5738 return lvItem.state & uMask;
5743 * Retrieves the text of a listview control item or subitem.
5746 * [I] hwnd : window handle
5747 * [I] nItem : item index
5748 * [IO] lpLVItem : item information
5749 * [I] isW : TRUE if lpLVItem is Unicode
5752 * SUCCESS : string length
5755 static INT LISTVIEW_GetItemTextT(LISTVIEW_INFO *infoPtr, INT nItem, LPLVITEMW lpLVItem, BOOL isW)
5757 if (!lpLVItem || nItem < 0 || nItem >= infoPtr->nItemCount) return 0;
5759 lpLVItem->mask = LVIF_TEXT;
5760 lpLVItem->iItem = nItem;
5761 if (!LISTVIEW_GetItemExtT(infoPtr, lpLVItem, isW)) return 0;
5763 return textlenT(lpLVItem->pszText, isW);
5768 * Searches for an item based on properties + relationships.
5771 * [I] infoPtr : valid pointer to the listview structure
5772 * [I] nItem : item index
5773 * [I] uFlags : relationship flag
5776 * SUCCESS : item index
5779 static INT LISTVIEW_GetNextItem(LISTVIEW_INFO *infoPtr, INT nItem, UINT uFlags)
5781 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
5783 LVFINDINFOW lvFindInfo;
5784 INT nCountPerColumn;
5788 TRACE("nItem=%d, uFlags=%x, nItemCount=%d\n", nItem, uFlags, infoPtr->nItemCount);
5789 if (nItem < -1 || nItem >= infoPtr->nItemCount) return -1;
5791 ZeroMemory(&lvFindInfo, sizeof(lvFindInfo));
5793 if (uFlags & LVNI_CUT)
5796 if (uFlags & LVNI_DROPHILITED)
5797 uMask |= LVIS_DROPHILITED;
5799 if (uFlags & LVNI_FOCUSED)
5800 uMask |= LVIS_FOCUSED;
5802 if (uFlags & LVNI_SELECTED)
5803 uMask |= LVIS_SELECTED;
5805 /* if we're asked for the focused item, that's only one,
5806 * so it's worth optimizing */
5807 if (uFlags & LVNI_FOCUSED)
5809 if ((LISTVIEW_GetItemState(infoPtr, infoPtr->nFocusedItem, uMask) & uMask) != uMask) return -1;
5810 return (infoPtr->nFocusedItem == nItem) ? -1 : infoPtr->nFocusedItem;
5813 if (uFlags & LVNI_ABOVE)
5815 if ((uView == LVS_LIST) || (uView == LVS_REPORT))
5820 if ((ListView_GetItemState(infoPtr->hwndSelf, nItem, uMask) & uMask) == uMask)
5826 /* Special case for autoarrange - move 'til the top of a list */
5827 if (is_autoarrange(infoPtr))
5829 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
5830 while (nItem - nCountPerRow >= 0)
5832 nItem -= nCountPerRow;
5833 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
5838 lvFindInfo.flags = LVFI_NEARESTXY;
5839 lvFindInfo.vkDirection = VK_UP;
5840 ListView_GetItemPosition(infoPtr->hwndSelf, nItem, &lvFindInfo.pt);
5841 while ((nItem = ListView_FindItemW(infoPtr->hwndSelf, nItem, &lvFindInfo)) != -1)
5843 if ((ListView_GetItemState(infoPtr->hwndSelf, nItem, uMask) & uMask) == uMask)
5848 else if (uFlags & LVNI_BELOW)
5850 if ((uView == LVS_LIST) || (uView == LVS_REPORT))
5852 while (nItem < infoPtr->nItemCount)
5855 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
5861 /* Special case for autoarrange - move 'til the bottom of a list */
5862 if (is_autoarrange(infoPtr))
5864 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
5865 while (nItem + nCountPerRow < infoPtr->nItemCount )
5867 nItem += nCountPerRow;
5868 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
5873 lvFindInfo.flags = LVFI_NEARESTXY;
5874 lvFindInfo.vkDirection = VK_DOWN;
5875 ListView_GetItemPosition(infoPtr->hwndSelf, nItem, &lvFindInfo.pt);
5876 while ((nItem = ListView_FindItemW(infoPtr->hwndSelf, nItem, &lvFindInfo)) != -1)
5878 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
5883 else if (uFlags & LVNI_TOLEFT)
5885 if (uView == LVS_LIST)
5887 nCountPerColumn = LISTVIEW_GetCountPerColumn(infoPtr);
5888 while (nItem - nCountPerColumn >= 0)
5890 nItem -= nCountPerColumn;
5891 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
5895 else if ((uView == LVS_SMALLICON) || (uView == LVS_ICON))
5897 /* Special case for autoarrange - move 'ti the beginning of a row */
5898 if (is_autoarrange(infoPtr))
5900 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
5901 while (nItem % nCountPerRow > 0)
5904 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
5909 lvFindInfo.flags = LVFI_NEARESTXY;
5910 lvFindInfo.vkDirection = VK_LEFT;
5911 ListView_GetItemPosition(infoPtr->hwndSelf, nItem, &lvFindInfo.pt);
5912 while ((nItem = ListView_FindItemW(infoPtr->hwndSelf, nItem, &lvFindInfo)) != -1)
5914 if ((ListView_GetItemState(infoPtr->hwndSelf, nItem, uMask) & uMask) == uMask)
5919 else if (uFlags & LVNI_TORIGHT)
5921 if (uView == LVS_LIST)
5923 nCountPerColumn = LISTVIEW_GetCountPerColumn(infoPtr);
5924 while (nItem + nCountPerColumn < infoPtr->nItemCount)
5926 nItem += nCountPerColumn;
5927 if ((ListView_GetItemState(infoPtr->hwndSelf, nItem, uMask) & uMask) == uMask)
5931 else if ((uView == LVS_SMALLICON) || (uView == LVS_ICON))
5933 /* Special case for autoarrange - move 'til the end of a row */
5934 if (is_autoarrange(infoPtr))
5936 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
5937 while (nItem % nCountPerRow < nCountPerRow - 1 )
5940 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
5945 lvFindInfo.flags = LVFI_NEARESTXY;
5946 lvFindInfo.vkDirection = VK_RIGHT;
5947 ListView_GetItemPosition(infoPtr->hwndSelf, nItem, &lvFindInfo.pt);
5948 while ((nItem = ListView_FindItemW(infoPtr->hwndSelf, nItem, &lvFindInfo)) != -1)
5950 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
5959 /* search by index */
5960 for (i = nItem; i < infoPtr->nItemCount; i++)
5962 if ((LISTVIEW_GetItemState(infoPtr, i, uMask) & uMask) == uMask)
5970 /* LISTVIEW_GetNumberOfWorkAreas */
5974 * Retrieves the origin coordinates when in icon or small icon display mode.
5977 * [I] infoPtr : valid pointer to the listview structure
5978 * [O] lpptOrigin : coordinate information
5983 static void LISTVIEW_GetOrigin(LISTVIEW_INFO *infoPtr, LPPOINT lpptOrigin)
5985 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
5986 INT nHorzPos = 0, nVertPos = 0;
5987 SCROLLINFO scrollInfo;
5989 scrollInfo.cbSize = sizeof(SCROLLINFO);
5990 scrollInfo.fMask = SIF_POS;
5992 if (GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo))
5993 nHorzPos = scrollInfo.nPos;
5994 if (GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
5995 nVertPos = scrollInfo.nPos;
5997 TRACE("nHorzPos=%d, nVertPos=%d\n", nHorzPos, nVertPos);
5999 lpptOrigin->x = infoPtr->rcList.left;
6000 lpptOrigin->y = infoPtr->rcList.top;
6001 if (uView == LVS_LIST)
6002 nHorzPos *= infoPtr->nItemWidth;
6003 else if (uView == LVS_REPORT)
6004 nVertPos *= infoPtr->nItemHeight;
6006 lpptOrigin->x -= nHorzPos;
6007 lpptOrigin->y -= nVertPos;
6009 TRACE(" origin=%s\n", debugpoint(lpptOrigin));
6014 * Retrieves the width of a string.
6017 * [I] hwnd : window handle
6018 * [I] lpszText : text string to process
6019 * [I] isW : TRUE if lpszText is Unicode, FALSE otherwise
6022 * SUCCESS : string width (in pixels)
6025 static INT LISTVIEW_GetStringWidthT(LISTVIEW_INFO *infoPtr, LPCWSTR lpszText, BOOL isW)
6030 if (is_textT(lpszText, isW))
6032 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
6033 HDC hdc = GetDC(infoPtr->hwndSelf);
6034 HFONT hOldFont = SelectObject(hdc, hFont);
6037 GetTextExtentPointW(hdc, lpszText, lstrlenW(lpszText), &stringSize);
6039 GetTextExtentPointA(hdc, (LPCSTR)lpszText, lstrlenA((LPCSTR)lpszText), &stringSize);
6040 SelectObject(hdc, hOldFont);
6041 ReleaseDC(infoPtr->hwndSelf, hdc);
6043 return stringSize.cx;
6048 * Determines which listview item is located at the specified position.
6051 * [I] infoPtr : valid pointer to the listview structure
6052 * [IO] lpht : hit test information
6053 * [I] subitem : fill out iSubItem.
6054 * [I] select : return the index only if the hit selects the item
6057 * (mm 20001022): We must not allow iSubItem to be touched, for
6058 * an app might pass only a structure with space up to iItem!
6059 * (MS Office 97 does that for instance in the file open dialog)
6062 * SUCCESS : item index
6065 static INT LISTVIEW_HitTest(LISTVIEW_INFO *infoPtr, LPLVHITTESTINFO lpht, BOOL subitem, BOOL select)
6067 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
6068 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
6069 RECT rcBox, rcBounds, rcState, rcIcon, rcLabel, rcSearch;
6070 POINT Origin, Position, opt;
6075 TRACE("(pt=%s, subitem=%d, select=%d)\n", debugpoint(&lpht->pt), subitem, select);
6079 if (subitem) lpht->iSubItem = 0;
6081 if (infoPtr->rcList.left > lpht->pt.x)
6082 lpht->flags |= LVHT_TOLEFT;
6083 else if (infoPtr->rcList.right < lpht->pt.x)
6084 lpht->flags |= LVHT_TORIGHT;
6086 if (infoPtr->rcList.top > lpht->pt.y)
6087 lpht->flags |= LVHT_ABOVE;
6088 else if (infoPtr->rcList.bottom < lpht->pt.y)
6089 lpht->flags |= LVHT_BELOW;
6091 TRACE("lpht->flags=0x%x\n", lpht->flags);
6092 if (lpht->flags) return -1;
6094 lpht->flags |= LVHT_NOWHERE;
6096 LISTVIEW_GetOrigin(infoPtr, &Origin);
6098 /* first deal with the large items */
6099 rcSearch.left = lpht->pt.x;
6100 rcSearch.top = lpht->pt.y;
6101 rcSearch.right = rcSearch.left + 1;
6102 rcSearch.bottom = rcSearch.top + 1;
6104 iterator_frameditems(&i, infoPtr, &rcSearch);
6105 iterator_next(&i); /* go to first item in the sequence */
6107 iterator_destroy(&i);
6109 TRACE("lpht->iItem=%d\n", iItem);
6110 if (iItem == -1) return -1;
6112 lvItem.mask = LVIF_STATE | LVIF_TEXT;
6113 if (uView == LVS_REPORT) lvItem.mask |= LVIF_INDENT;
6114 lvItem.stateMask = LVIS_STATEIMAGEMASK;
6115 if (uView == LVS_ICON) lvItem.stateMask |= LVIS_FOCUSED;
6116 lvItem.iItem = iItem;
6117 lvItem.iSubItem = 0;
6118 lvItem.pszText = szDispText;
6119 lvItem.cchTextMax = DISP_TEXT_SIZE;
6120 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return -1;
6121 if (!infoPtr->bFocus) lvItem.state &= ~LVIS_FOCUSED;
6123 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, &rcBox, &rcState, &rcIcon, &rcLabel);
6124 LISTVIEW_GetItemOrigin(infoPtr, iItem, &Position);
6125 opt.x = lpht->pt.x - Position.x - Origin.x;
6126 opt.y = lpht->pt.y - Position.y - Origin.y;
6128 if (uView == LVS_REPORT)
6131 UnionRect(&rcBounds, &rcIcon, &rcLabel);
6132 TRACE("rcBounds=%s\n", debugrect(&rcBounds));
6133 if (!PtInRect(&rcBounds, opt)) return -1;
6135 if (PtInRect(&rcIcon, opt))
6136 lpht->flags |= LVHT_ONITEMICON;
6137 else if (PtInRect(&rcLabel, opt))
6138 lpht->flags |= LVHT_ONITEMLABEL;
6139 else if (infoPtr->himlState && STATEIMAGEINDEX(lvItem.state) && PtInRect(&rcState, opt))
6140 lpht->flags |= LVHT_ONITEMSTATEICON;
6141 if (lpht->flags & LVHT_ONITEM)
6142 lpht->flags &= ~LVHT_NOWHERE;
6144 TRACE("lpht->flags=0x%x\n", lpht->flags);
6145 if (uView == LVS_REPORT && subitem)
6149 rcBounds.right = rcBounds.left;
6150 for (j = 0; j < DPA_GetPtrCount(infoPtr->hdpaColumns); j++)
6152 rcBounds.left = rcBounds.right;
6153 rcBounds.right += LISTVIEW_GetColumnWidth(infoPtr, j);
6154 if (PtInRect(&rcBounds, opt))
6162 if (select && !(uView == LVS_REPORT &&
6163 ((infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) ||
6164 (infoPtr->dwStyle & LVS_OWNERDRAWFIXED))))
6166 if (uView == LVS_REPORT)
6168 UnionRect(&rcBounds, &rcIcon, &rcLabel);
6169 UnionRect(&rcBounds, &rcBounds, &rcState);
6171 if (!PtInRect(&rcBounds, opt)) iItem = -1;
6173 return lpht->iItem = iItem;
6177 /* LISTVIEW_InsertCompare: callback routine for comparing pszText members of the LV_ITEMS
6178 in a LISTVIEW on insert. Passed to DPA_Sort in LISTVIEW_InsertItem.
6179 This function should only be used for inserting items into a sorted list (LVM_INSERTITEM)
6180 and not during the processing of a LVM_SORTITEMS message. Applications should provide
6181 their own sort proc. when sending LVM_SORTITEMS.
6184 (remarks on LVITEM: LVM_INSERTITEM will insert the new item in the proper sort postion...
6186 LVS_SORTXXX must be specified,
6187 LVS_OWNERDRAW is not set,
6188 <item>.pszText is not LPSTR_TEXTCALLBACK.
6190 (LVS_SORT* flags): "For the LVS_SORTASCENDING... styles, item indices
6191 are sorted based on item text..."
6193 static INT WINAPI LISTVIEW_InsertCompare( LPVOID first, LPVOID second, LPARAM lParam)
6195 ITEM_INFO* lv_first = (ITEM_INFO*) DPA_GetPtr( (HDPA)first, 0 );
6196 ITEM_INFO* lv_second = (ITEM_INFO*) DPA_GetPtr( (HDPA)second, 0 );
6197 INT cmpv = textcmpWT(lv_first->hdr.pszText, lv_second->hdr.pszText, TRUE);
6199 /* if we're sorting descending, negate the return value */
6200 return (((LISTVIEW_INFO *)lParam)->dwStyle & LVS_SORTDESCENDING) ? -cmpv : cmpv;
6205 * Inserts a new item in the listview control.
6208 * [I] infoPtr : valid pointer to the listview structure
6209 * [I] lpLVItem : item information
6210 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
6213 * SUCCESS : new item index
6216 static INT LISTVIEW_InsertItemT(LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem, BOOL isW)
6218 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
6223 BOOL is_sorted, has_changed;
6225 HWND hwndSelf = infoPtr->hwndSelf;
6227 TRACE("(lpLVItem=%s, isW=%d)\n", debuglvitem_t(lpLVItem, isW), isW);
6229 if (infoPtr->dwStyle & LVS_OWNERDATA) return infoPtr->nItemCount++;
6231 /* make sure it's an item, and not a subitem; cannot insert a subitem */
6232 if (!lpLVItem || lpLVItem->iItem < 0 || lpLVItem->iSubItem) return -1;
6234 if (!is_assignable_item(lpLVItem, infoPtr->dwStyle)) return -1;
6236 if (!(lpItem = (ITEM_INFO *)Alloc(sizeof(ITEM_INFO)))) return -1;
6238 /* insert item in listview control data structure */
6239 if ( !(hdpaSubItems = DPA_Create(8)) ) goto fail;
6240 if ( !DPA_SetPtr(hdpaSubItems, 0, lpItem) ) assert (FALSE);
6242 is_sorted = (infoPtr->dwStyle & (LVS_SORTASCENDING | LVS_SORTDESCENDING)) &&
6243 !(infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (LPSTR_TEXTCALLBACKW != lpLVItem->pszText);
6245 nItem = is_sorted ? infoPtr->nItemCount : min(lpLVItem->iItem, infoPtr->nItemCount);
6246 TRACE(" inserting at %d, sorted=%d, count=%d, iItem=%d\n", nItem, is_sorted, infoPtr->nItemCount, lpLVItem->iItem);
6247 nItem = DPA_InsertPtr( infoPtr->hdpaItems, nItem, hdpaSubItems );
6248 if (nItem == -1) goto fail;
6249 infoPtr->nItemCount++;
6251 /* shift indices first so they don't get tangled */
6252 LISTVIEW_ShiftIndices(infoPtr, nItem, 1);
6254 /* set the item attributes */
6255 if (lpLVItem->mask & (LVIF_GROUPID|LVIF_COLUMNS))
6257 /* full size structure expected - _WIN32IE >= 0x560 */
6260 else if (lpLVItem->mask & LVIF_INDENT)
6262 /* indent member expected - _WIN32IE >= 0x300 */
6263 memcpy(&item, lpLVItem, offsetof( LVITEMW, iGroupId ));
6267 /* minimal structure expected */
6268 memcpy(&item, lpLVItem, offsetof( LVITEMW, iIndent ));
6271 if (infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES) item.state &= ~LVIS_STATEIMAGEMASK;
6272 if (!set_main_item(infoPtr, &item, TRUE, isW, &has_changed)) goto undo;
6274 /* if we're sorted, sort the list, and update the index */
6277 DPA_Sort( infoPtr->hdpaItems, LISTVIEW_InsertCompare, (LPARAM)infoPtr );
6278 nItem = DPA_GetPtrIndex( infoPtr->hdpaItems, hdpaSubItems );
6279 assert(nItem != -1);
6282 /* make room for the position, if we are in the right mode */
6283 if ((uView == LVS_SMALLICON) || (uView == LVS_ICON))
6285 if (DPA_InsertPtr(infoPtr->hdpaPosX, nItem, 0) == -1)
6287 if (DPA_InsertPtr(infoPtr->hdpaPosY, nItem, 0) == -1)
6289 DPA_DeletePtr(infoPtr->hdpaPosX, nItem);
6294 /* send LVN_INSERTITEM notification */
6295 ZeroMemory(&nmlv, sizeof(NMLISTVIEW));
6297 nmlv.lParam = lpItem->lParam;
6298 notify_listview(infoPtr, LVN_INSERTITEM, &nmlv);
6299 if (!IsWindow(hwndSelf))
6302 /* align items (set position of each item) */
6303 if ((uView == LVS_SMALLICON || uView == LVS_ICON))
6307 if (infoPtr->dwStyle & LVS_ALIGNLEFT)
6308 LISTVIEW_NextIconPosLeft(infoPtr, &pt);
6310 LISTVIEW_NextIconPosTop(infoPtr, &pt);
6312 LISTVIEW_MoveIconTo(infoPtr, nItem, &pt, TRUE);
6315 /* now is the invalidation fun */
6316 LISTVIEW_ScrollOnInsert(infoPtr, nItem, 1);
6320 LISTVIEW_ShiftIndices(infoPtr, nItem, -1);
6321 DPA_DeletePtr(infoPtr->hdpaItems, nItem);
6322 infoPtr->nItemCount--;
6324 DPA_DeletePtr(hdpaSubItems, 0);
6325 DPA_Destroy (hdpaSubItems);
6332 * Redraws a range of items.
6335 * [I] infoPtr : valid pointer to the listview structure
6336 * [I] nFirst : first item
6337 * [I] nLast : last item
6343 static BOOL LISTVIEW_RedrawItems(LISTVIEW_INFO *infoPtr, INT nFirst, INT nLast)
6347 if (nLast < nFirst || min(nFirst, nLast) < 0 ||
6348 max(nFirst, nLast) >= infoPtr->nItemCount)
6351 for (i = nFirst; i <= nLast; i++)
6352 LISTVIEW_InvalidateItem(infoPtr, i);
6359 * Scroll the content of a listview.
6362 * [I] infoPtr : valid pointer to the listview structure
6363 * [I] dx : horizontal scroll amount in pixels
6364 * [I] dy : vertical scroll amount in pixels
6371 * If the control is in report mode (LVS_REPORT) the control can
6372 * be scrolled only in line increments. "dy" will be rounded to the
6373 * nearest number of pixels that are a whole line. Ex: if line height
6374 * is 16 and an 8 is passed, the list will be scrolled by 16. If a 7
6375 * is passed the the scroll will be 0. (per MSDN 7/2002)
6377 * For: (per experimentaion with native control and CSpy ListView)
6378 * LVS_ICON dy=1 = 1 pixel (vertical only)
6380 * LVS_SMALLICON dy=1 = 1 pixel (vertical only)
6382 * LVS_LIST dx=1 = 1 column (horizontal only)
6383 * but will only scroll 1 column per message
6384 * no matter what the value.
6385 * dy must be 0 or FALSE returned.
6386 * LVS_REPORT dx=1 = 1 pixel
6390 static BOOL LISTVIEW_Scroll(LISTVIEW_INFO *infoPtr, INT dx, INT dy)
6392 switch(infoPtr->dwStyle & LVS_TYPEMASK) {
6394 dy += (dy < 0 ? -1 : 1) * infoPtr->nItemHeight/2;
6395 dy /= infoPtr->nItemHeight;
6398 if (dy != 0) return FALSE;
6405 if (dx != 0) LISTVIEW_HScroll(infoPtr, SB_INTERNAL, dx, 0);
6406 if (dy != 0) LISTVIEW_VScroll(infoPtr, SB_INTERNAL, dy, 0);
6413 * Sets the background color.
6416 * [I] infoPtr : valid pointer to the listview structure
6417 * [I] clrBk : background color
6423 static BOOL LISTVIEW_SetBkColor(LISTVIEW_INFO *infoPtr, COLORREF clrBk)
6425 TRACE("(clrBk=%lx)\n", clrBk);
6427 if(infoPtr->clrBk != clrBk) {
6428 if (infoPtr->clrBk != CLR_NONE) DeleteObject(infoPtr->hBkBrush);
6429 infoPtr->clrBk = clrBk;
6430 if (clrBk == CLR_NONE)
6431 infoPtr->hBkBrush = (HBRUSH)GetClassLongPtrW(infoPtr->hwndSelf, GCLP_HBRBACKGROUND);
6433 infoPtr->hBkBrush = CreateSolidBrush(clrBk);
6434 LISTVIEW_InvalidateList(infoPtr);
6440 /* LISTVIEW_SetBkImage */
6442 /*** Helper for {Insert,Set}ColumnT *only* */
6443 static void column_fill_hditem(LISTVIEW_INFO *infoPtr, HDITEMW *lphdi, INT nColumn, const LVCOLUMNW *lpColumn, BOOL isW)
6445 if (lpColumn->mask & LVCF_FMT)
6447 /* format member is valid */
6448 lphdi->mask |= HDI_FORMAT;
6450 /* set text alignment (leftmost column must be left-aligned) */
6451 if (nColumn == 0 || (lpColumn->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_LEFT)
6452 lphdi->fmt |= HDF_LEFT;
6453 else if ((lpColumn->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_RIGHT)
6454 lphdi->fmt |= HDF_RIGHT;
6455 else if ((lpColumn->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_CENTER)
6456 lphdi->fmt |= HDF_CENTER;
6458 if (lpColumn->fmt & LVCFMT_BITMAP_ON_RIGHT)
6459 lphdi->fmt |= HDF_BITMAP_ON_RIGHT;
6461 if (lpColumn->fmt & LVCFMT_COL_HAS_IMAGES)
6463 lphdi->fmt |= HDF_IMAGE;
6464 lphdi->iImage = I_IMAGECALLBACK;
6468 if (lpColumn->mask & LVCF_WIDTH)
6470 lphdi->mask |= HDI_WIDTH;
6471 if(lpColumn->cx == LVSCW_AUTOSIZE_USEHEADER)
6473 /* make it fill the remainder of the controls width */
6477 for(item_index = 0; item_index < (nColumn - 1); item_index++)
6479 LISTVIEW_GetHeaderRect(infoPtr, item_index, &rcHeader);
6480 lphdi->cxy += rcHeader.right - rcHeader.left;
6483 /* retrieve the layout of the header */
6484 GetClientRect(infoPtr->hwndSelf, &rcHeader);
6485 TRACE("start cxy=%d rcHeader=%s\n", lphdi->cxy, debugrect(&rcHeader));
6487 lphdi->cxy = (rcHeader.right - rcHeader.left) - lphdi->cxy;
6490 lphdi->cxy = lpColumn->cx;
6493 if (lpColumn->mask & LVCF_TEXT)
6495 lphdi->mask |= HDI_TEXT | HDI_FORMAT;
6496 lphdi->fmt |= HDF_STRING;
6497 lphdi->pszText = lpColumn->pszText;
6498 lphdi->cchTextMax = textlenT(lpColumn->pszText, isW);
6501 if (lpColumn->mask & LVCF_IMAGE)
6503 lphdi->mask |= HDI_IMAGE;
6504 lphdi->iImage = lpColumn->iImage;
6507 if (lpColumn->mask & LVCF_ORDER)
6509 lphdi->mask |= HDI_ORDER;
6510 lphdi->iOrder = lpColumn->iOrder;
6517 * Inserts a new column.
6520 * [I] infoPtr : valid pointer to the listview structure
6521 * [I] nColumn : column index
6522 * [I] lpColumn : column information
6523 * [I] isW : TRUE if lpColumn is Unicode, FALSE otherwise
6526 * SUCCESS : new column index
6529 static INT LISTVIEW_InsertColumnT(LISTVIEW_INFO *infoPtr, INT nColumn,
6530 const LVCOLUMNW *lpColumn, BOOL isW)
6532 COLUMN_INFO *lpColumnInfo;
6536 TRACE("(nColumn=%d, lpColumn=%s, isW=%d)\n", nColumn, debuglvcolumn_t(lpColumn, isW), isW);
6538 if (!lpColumn || nColumn < 0) return -1;
6539 nColumn = min(nColumn, DPA_GetPtrCount(infoPtr->hdpaColumns));
6541 ZeroMemory(&hdi, sizeof(HDITEMW));
6542 column_fill_hditem(infoPtr, &hdi, nColumn, lpColumn, isW);
6544 /* insert item in header control */
6545 nNewColumn = SendMessageW(infoPtr->hwndHeader,
6546 isW ? HDM_INSERTITEMW : HDM_INSERTITEMA,
6547 (WPARAM)nColumn, (LPARAM)&hdi);
6548 if (nNewColumn == -1) return -1;
6549 if (nNewColumn != nColumn) ERR("nColumn=%d, nNewColumn=%d\n", nColumn, nNewColumn);
6551 /* create our own column info */
6552 if (!(lpColumnInfo = Alloc(sizeof(COLUMN_INFO)))) goto fail;
6553 if (DPA_InsertPtr(infoPtr->hdpaColumns, nNewColumn, lpColumnInfo) == -1) goto fail;
6555 if (lpColumn->mask & LVCF_FMT) lpColumnInfo->fmt = lpColumn->fmt;
6556 if (!Header_GetItemRect(infoPtr->hwndHeader, nNewColumn, &lpColumnInfo->rcHeader)) goto fail;
6558 /* now we have to actually adjust the data */
6559 if (!(infoPtr->dwStyle & LVS_OWNERDATA) && infoPtr->nItemCount > 0)
6561 SUBITEM_INFO *lpSubItem;
6565 for (nItem = 0; nItem < infoPtr->nItemCount; nItem++)
6567 hdpaSubItems = (HDPA)DPA_GetPtr(infoPtr->hdpaItems, nItem);
6568 for (i = 1; i < DPA_GetPtrCount(hdpaSubItems); i++)
6570 lpSubItem = (SUBITEM_INFO *)DPA_GetPtr(hdpaSubItems, i);
6571 if (lpSubItem->iSubItem >= nNewColumn)
6572 lpSubItem->iSubItem++;
6577 /* make space for the new column */
6578 LISTVIEW_ScrollColumns(infoPtr, nNewColumn + 1, lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left);
6583 if (nNewColumn != -1) SendMessageW(infoPtr->hwndHeader, HDM_DELETEITEM, nNewColumn, 0);
6586 DPA_DeletePtr(infoPtr->hdpaColumns, nNewColumn);
6594 * Sets the attributes of a header item.
6597 * [I] infoPtr : valid pointer to the listview structure
6598 * [I] nColumn : column index
6599 * [I] lpColumn : column attributes
6600 * [I] isW: if TRUE, the lpColumn is a LPLVCOLUMNW, else it is a LPLVCOLUMNA
6606 static BOOL LISTVIEW_SetColumnT(LISTVIEW_INFO *infoPtr, INT nColumn,
6607 const LVCOLUMNW *lpColumn, BOOL isW)
6609 HDITEMW hdi, hdiget;
6612 TRACE("(nColumn=%d, lpColumn=%s, isW=%d)\n", nColumn, debuglvcolumn_t(lpColumn, isW), isW);
6614 if (!lpColumn || nColumn < 0 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
6616 ZeroMemory(&hdi, sizeof(HDITEMW));
6617 if (lpColumn->mask & LVCF_FMT)
6619 hdi.mask |= HDI_FORMAT;
6620 hdiget.mask = HDI_FORMAT;
6621 if (Header_GetItemW(infoPtr->hwndHeader, nColumn, &hdiget))
6622 hdi.fmt = hdiget.fmt & HDF_STRING;
6624 column_fill_hditem(infoPtr, &hdi, nColumn, lpColumn, isW);
6626 /* set header item attributes */
6627 bResult = SendMessageW(infoPtr->hwndHeader, isW ? HDM_SETITEMW : HDM_SETITEMA, (WPARAM)nColumn, (LPARAM)&hdi);
6628 if (!bResult) return FALSE;
6630 if (lpColumn->mask & LVCF_FMT)
6632 COLUMN_INFO *lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, nColumn);
6633 int oldFmt = lpColumnInfo->fmt;
6635 lpColumnInfo->fmt = lpColumn->fmt;
6636 if ((oldFmt ^ lpColumn->fmt) & (LVCFMT_JUSTIFYMASK | LVCFMT_IMAGE))
6638 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
6639 if (uView == LVS_REPORT) LISTVIEW_InvalidateColumn(infoPtr, nColumn);
6648 * Sets the column order array
6651 * [I] infoPtr : valid pointer to the listview structure
6652 * [I] iCount : number of elements in column order array
6653 * [I] lpiArray : pointer to column order array
6659 static BOOL LISTVIEW_SetColumnOrderArray(LISTVIEW_INFO *infoPtr, INT iCount, const INT *lpiArray)
6661 FIXME("iCount %d lpiArray %p\n", iCount, lpiArray);
6672 * Sets the width of a column
6675 * [I] infoPtr : valid pointer to the listview structure
6676 * [I] nColumn : column index
6677 * [I] cx : column width
6683 static BOOL LISTVIEW_SetColumnWidth(LISTVIEW_INFO *infoPtr, INT nColumn, INT cx)
6685 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
6686 WCHAR szDispText[DISP_TEXT_SIZE] = { 0 };
6690 TRACE("(nColumn=%d, cx=%d\n", nColumn, cx);
6692 /* set column width only if in report or list mode */
6693 if (uView != LVS_REPORT && uView != LVS_LIST) return FALSE;
6695 /* take care of invalid cx values */
6696 if(uView == LVS_REPORT && cx < -2) cx = LVSCW_AUTOSIZE;
6697 else if (uView == LVS_LIST && cx < 1) return FALSE;
6699 /* resize all columns if in LVS_LIST mode */
6700 if(uView == LVS_LIST)
6702 infoPtr->nItemWidth = cx;
6703 LISTVIEW_InvalidateList(infoPtr);
6707 if (nColumn < 0 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
6709 if (cx == LVSCW_AUTOSIZE || (cx == LVSCW_AUTOSIZE_USEHEADER && nColumn < DPA_GetPtrCount(infoPtr->hdpaColumns) -1))
6714 lvItem.mask = LVIF_TEXT;
6716 lvItem.iSubItem = nColumn;
6717 lvItem.pszText = szDispText;
6718 lvItem.cchTextMax = DISP_TEXT_SIZE;
6719 for (; lvItem.iItem < infoPtr->nItemCount; lvItem.iItem++)
6721 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) continue;
6722 nLabelWidth = LISTVIEW_GetStringWidthT(infoPtr, lvItem.pszText, TRUE);
6723 if (max_cx < nLabelWidth) max_cx = nLabelWidth;
6725 if (infoPtr->himlSmall && (nColumn == 0 || (LISTVIEW_GetColumnInfo(infoPtr, nColumn)->fmt & LVCFMT_IMAGE)))
6726 max_cx += infoPtr->iconSize.cx;
6727 max_cx += TRAILING_LABEL_PADDING;
6730 /* autosize based on listview items width */
6731 if(cx == LVSCW_AUTOSIZE)
6733 else if(cx == LVSCW_AUTOSIZE_USEHEADER)
6735 /* if iCol is the last column make it fill the remainder of the controls width */
6736 if(nColumn == DPA_GetPtrCount(infoPtr->hdpaColumns) - 1)
6741 LISTVIEW_GetOrigin(infoPtr, &Origin);
6742 LISTVIEW_GetHeaderRect(infoPtr, nColumn, &rcHeader);
6744 cx = infoPtr->rcList.right - Origin.x - rcHeader.left;
6748 /* Despite what the MS docs say, if this is not the last
6749 column, then MS resizes the column to the width of the
6750 largest text string in the column, including headers
6751 and items. This is different from LVSCW_AUTOSIZE in that
6752 LVSCW_AUTOSIZE ignores the header string length. */
6755 /* retrieve header text */
6756 hdi.mask = HDI_TEXT;
6757 hdi.cchTextMax = DISP_TEXT_SIZE;
6758 hdi.pszText = szDispText;
6759 if (Header_GetItemW(infoPtr->hwndHeader, nColumn, (LPARAM)&hdi))
6761 HDC hdc = GetDC(infoPtr->hwndSelf);
6762 HFONT old_font = SelectObject(hdc, (HFONT)SendMessageW(infoPtr->hwndHeader, WM_GETFONT, 0, 0));
6765 if (GetTextExtentPoint32W(hdc, hdi.pszText, lstrlenW(hdi.pszText), &size))
6766 cx = size.cx + TRAILING_HEADER_PADDING;
6767 /* FIXME: Take into account the header image, if one is present */
6768 SelectObject(hdc, old_font);
6769 ReleaseDC(infoPtr->hwndSelf, hdc);
6771 cx = max (cx, max_cx);
6775 if (cx < 0) return FALSE;
6777 /* call header to update the column change */
6778 hdi.mask = HDI_WIDTH;
6780 TRACE("hdi.cxy=%d\n", hdi.cxy);
6781 return Header_SetItemW(infoPtr->hwndHeader, nColumn, (LPARAM)&hdi);
6785 * Creates the checkbox imagelist. Helper for LISTVIEW_SetExtendedListViewStyle
6788 static HIMAGELIST LISTVIEW_CreateCheckBoxIL(LISTVIEW_INFO *infoPtr)
6791 HBITMAP hbm_im, hbm_mask, hbm_orig;
6793 HBRUSH hbr_white = GetStockObject(WHITE_BRUSH);
6794 HBRUSH hbr_black = GetStockObject(BLACK_BRUSH);
6797 himl = ImageList_Create(GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON),
6798 ILC_COLOR | ILC_MASK, 2, 2);
6799 hdc_wnd = GetDC(infoPtr->hwndSelf);
6800 hdc = CreateCompatibleDC(hdc_wnd);
6801 hbm_im = CreateCompatibleBitmap(hdc_wnd, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON));
6802 hbm_mask = CreateBitmap(GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), 1, 1, NULL);
6803 ReleaseDC(infoPtr->hwndSelf, hdc_wnd);
6805 rc.left = rc.top = 0;
6806 rc.right = GetSystemMetrics(SM_CXSMICON);
6807 rc.bottom = GetSystemMetrics(SM_CYSMICON);
6809 hbm_orig = SelectObject(hdc, hbm_mask);
6810 FillRect(hdc, &rc, hbr_white);
6811 InflateRect(&rc, -3, -3);
6812 FillRect(hdc, &rc, hbr_black);
6814 SelectObject(hdc, hbm_im);
6815 DrawFrameControl(hdc, &rc, DFC_BUTTON, DFCS_BUTTONCHECK | DFCS_MONO);
6816 SelectObject(hdc, hbm_orig);
6817 ImageList_Add(himl, hbm_im, hbm_mask);
6819 SelectObject(hdc, hbm_im);
6820 DrawFrameControl(hdc, &rc, DFC_BUTTON, DFCS_BUTTONCHECK | DFCS_MONO | DFCS_CHECKED);
6821 SelectObject(hdc, hbm_orig);
6822 ImageList_Add(himl, hbm_im, hbm_mask);
6824 DeleteObject(hbm_mask);
6825 DeleteObject(hbm_im);
6833 * Sets the extended listview style.
6836 * [I] infoPtr : valid pointer to the listview structure
6838 * [I] dwStyle : style
6841 * SUCCESS : previous style
6844 static DWORD LISTVIEW_SetExtendedListViewStyle(LISTVIEW_INFO *infoPtr, DWORD dwMask, DWORD dwStyle)
6846 DWORD dwOldStyle = infoPtr->dwLvExStyle;
6850 infoPtr->dwLvExStyle = (dwOldStyle & ~dwMask) | (dwStyle & dwMask);
6852 infoPtr->dwLvExStyle = dwStyle;
6854 if((infoPtr->dwLvExStyle ^ dwOldStyle) & LVS_EX_CHECKBOXES)
6856 HIMAGELIST himl = 0;
6857 if(infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES)
6858 himl = LISTVIEW_CreateCheckBoxIL(infoPtr);
6859 LISTVIEW_SetImageList(infoPtr, LVSIL_STATE, himl);
6867 * Sets the new hot cursor used during hot tracking and hover selection.
6870 * [I] infoPtr : valid pointer to the listview structure
6871 * [I] hCursor : the new hot cursor handle
6874 * Returns the previous hot cursor
6876 static HCURSOR LISTVIEW_SetHotCursor(LISTVIEW_INFO *infoPtr, HCURSOR hCursor)
6878 HCURSOR oldCursor = infoPtr->hHotCursor;
6880 infoPtr->hHotCursor = hCursor;
6888 * Sets the hot item index.
6891 * [I] infoPtr : valid pointer to the listview structure
6892 * [I] iIndex : index
6895 * SUCCESS : previous hot item index
6896 * FAILURE : -1 (no hot item)
6898 static INT LISTVIEW_SetHotItem(LISTVIEW_INFO *infoPtr, INT iIndex)
6900 INT iOldIndex = infoPtr->nHotItem;
6902 infoPtr->nHotItem = iIndex;
6910 * Sets the amount of time the cursor must hover over an item before it is selected.
6913 * [I] infoPtr : valid pointer to the listview structure
6914 * [I] dwHoverTime : hover time, if -1 the hover time is set to the default
6917 * Returns the previous hover time
6919 static DWORD LISTVIEW_SetHoverTime(LISTVIEW_INFO *infoPtr, DWORD dwHoverTime)
6921 DWORD oldHoverTime = infoPtr->dwHoverTime;
6923 infoPtr->dwHoverTime = dwHoverTime;
6925 return oldHoverTime;
6930 * Sets spacing for icons of LVS_ICON style.
6933 * [I] infoPtr : valid pointer to the listview structure
6934 * [I] cx : horizontal spacing (-1 = system spacing, 0 = autosize)
6935 * [I] cy : vertical spacing (-1 = system spacing, 0 = autosize)
6938 * MAKELONG(oldcx, oldcy)
6940 static DWORD LISTVIEW_SetIconSpacing(LISTVIEW_INFO *infoPtr, INT cx, INT cy)
6942 DWORD oldspacing = MAKELONG(infoPtr->iconSpacing.cx, infoPtr->iconSpacing.cy);
6943 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
6945 TRACE("requested=(%d,%d)\n", cx, cy);
6947 /* this is supported only for LVS_ICON style */
6948 if (uView != LVS_ICON) return oldspacing;
6950 /* set to defaults, if instructed to */
6951 if (cx == -1) cx = GetSystemMetrics(SM_CXICONSPACING);
6952 if (cy == -1) cy = GetSystemMetrics(SM_CYICONSPACING);
6954 /* if 0 then compute width
6955 * FIXME: Should scan each item and determine max width of
6956 * icon or label, then make that the width */
6958 cx = infoPtr->iconSpacing.cx;
6960 /* if 0 then compute height */
6962 cy = infoPtr->iconSize.cy + 2 * infoPtr->ntmHeight +
6963 ICON_BOTTOM_PADDING + ICON_TOP_PADDING + LABEL_VERT_PADDING;
6966 infoPtr->iconSpacing.cx = cx;
6967 infoPtr->iconSpacing.cy = cy;
6969 TRACE("old=(%d,%d), new=(%d,%d), iconSize=(%ld,%ld), ntmH=%d\n",
6970 LOWORD(oldspacing), HIWORD(oldspacing), cx, cy,
6971 infoPtr->iconSize.cx, infoPtr->iconSize.cy,
6972 infoPtr->ntmHeight);
6974 /* these depend on the iconSpacing */
6975 LISTVIEW_UpdateItemSize(infoPtr);
6980 static inline void set_icon_size(SIZE *size, HIMAGELIST himl, BOOL small)
6984 if (himl && ImageList_GetIconSize(himl, &cx, &cy))
6991 size->cx = GetSystemMetrics(small ? SM_CXSMICON : SM_CXICON);
6992 size->cy = GetSystemMetrics(small ? SM_CYSMICON : SM_CYICON);
7001 * [I] infoPtr : valid pointer to the listview structure
7002 * [I] nType : image list type
7003 * [I] himl : image list handle
7006 * SUCCESS : old image list
7009 static HIMAGELIST LISTVIEW_SetImageList(LISTVIEW_INFO *infoPtr, INT nType, HIMAGELIST himl)
7011 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
7012 INT oldHeight = infoPtr->nItemHeight;
7013 HIMAGELIST himlOld = 0;
7015 TRACE("(nType=%d, himl=%p\n", nType, himl);
7020 himlOld = infoPtr->himlNormal;
7021 infoPtr->himlNormal = himl;
7022 if (uView == LVS_ICON) set_icon_size(&infoPtr->iconSize, himl, FALSE);
7023 LISTVIEW_SetIconSpacing(infoPtr, 0, 0);
7027 himlOld = infoPtr->himlSmall;
7028 infoPtr->himlSmall = himl;
7029 if (uView != LVS_ICON) set_icon_size(&infoPtr->iconSize, himl, TRUE);
7033 himlOld = infoPtr->himlState;
7034 infoPtr->himlState = himl;
7035 set_icon_size(&infoPtr->iconStateSize, himl, TRUE);
7036 ImageList_SetBkColor(infoPtr->himlState, CLR_NONE);
7040 ERR("Unknown icon type=%d\n", nType);
7044 infoPtr->nItemHeight = LISTVIEW_CalculateItemHeight(infoPtr);
7045 if (infoPtr->nItemHeight != oldHeight)
7046 LISTVIEW_UpdateScroll(infoPtr);
7053 * Preallocates memory (does *not* set the actual count of items !)
7056 * [I] infoPtr : valid pointer to the listview structure
7057 * [I] nItems : item count (projected number of items to allocate)
7058 * [I] dwFlags : update flags
7064 static BOOL LISTVIEW_SetItemCount(LISTVIEW_INFO *infoPtr, INT nItems, DWORD dwFlags)
7066 TRACE("(nItems=%d, dwFlags=%lx)\n", nItems, dwFlags);
7068 if (infoPtr->dwStyle & LVS_OWNERDATA)
7070 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
7071 INT nOldCount = infoPtr->nItemCount;
7073 if (nItems < nOldCount)
7075 RANGE range = { nItems, nOldCount };
7076 ranges_del(infoPtr->selectionRanges, range);
7077 if (infoPtr->nFocusedItem >= nItems)
7079 infoPtr->nFocusedItem = -1;
7080 SetRectEmpty(&infoPtr->rcFocus);
7084 infoPtr->nItemCount = nItems;
7085 LISTVIEW_UpdateScroll(infoPtr);
7087 /* the flags are valid only in ownerdata report and list modes */
7088 if (uView == LVS_ICON || uView == LVS_SMALLICON) dwFlags = 0;
7090 if (!(dwFlags & LVSICF_NOSCROLL) && infoPtr->nFocusedItem != -1)
7091 LISTVIEW_EnsureVisible(infoPtr, infoPtr->nFocusedItem, FALSE);
7093 if (!(dwFlags & LVSICF_NOINVALIDATEALL))
7094 LISTVIEW_InvalidateList(infoPtr);
7101 LISTVIEW_GetOrigin(infoPtr, &Origin);
7102 nFrom = min(nOldCount, nItems);
7103 nTo = max(nOldCount, nItems);
7105 if (uView == LVS_REPORT)
7108 rcErase.top = nFrom * infoPtr->nItemHeight;
7109 rcErase.right = infoPtr->nItemWidth;
7110 rcErase.bottom = nTo * infoPtr->nItemHeight;
7111 OffsetRect(&rcErase, Origin.x, Origin.y);
7112 if (IntersectRect(&rcErase, &rcErase, &infoPtr->rcList))
7113 LISTVIEW_InvalidateRect(infoPtr, &rcErase);
7117 INT nPerCol = LISTVIEW_GetCountPerColumn(infoPtr);
7119 rcErase.left = (nFrom / nPerCol) * infoPtr->nItemWidth;
7120 rcErase.top = (nFrom % nPerCol) * infoPtr->nItemHeight;
7121 rcErase.right = rcErase.left + infoPtr->nItemWidth;
7122 rcErase.bottom = nPerCol * infoPtr->nItemHeight;
7123 OffsetRect(&rcErase, Origin.x, Origin.y);
7124 if (IntersectRect(&rcErase, &rcErase, &infoPtr->rcList))
7125 LISTVIEW_InvalidateRect(infoPtr, &rcErase);
7127 rcErase.left = (nFrom / nPerCol + 1) * infoPtr->nItemWidth;
7129 rcErase.right = (nTo / nPerCol + 1) * infoPtr->nItemWidth;
7130 rcErase.bottom = nPerCol * infoPtr->nItemHeight;
7131 OffsetRect(&rcErase, Origin.x, Origin.y);
7132 if (IntersectRect(&rcErase, &rcErase, &infoPtr->rcList))
7133 LISTVIEW_InvalidateRect(infoPtr, &rcErase);
7139 /* According to MSDN for non-LVS_OWNERDATA this is just
7140 * a performance issue. The control allocates its internal
7141 * data structures for the number of items specified. It
7142 * cuts down on the number of memory allocations. Therefore
7143 * we will just issue a WARN here
7145 WARN("for non-ownerdata performance option not implemented.\n");
7153 * Sets the position of an item.
7156 * [I] infoPtr : valid pointer to the listview structure
7157 * [I] nItem : item index
7158 * [I] pt : coordinate
7164 static BOOL LISTVIEW_SetItemPosition(LISTVIEW_INFO *infoPtr, INT nItem, POINT pt)
7166 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
7169 TRACE("(nItem=%d, &pt=%s\n", nItem, debugpoint(&pt));
7171 if (nItem < 0 || nItem >= infoPtr->nItemCount ||
7172 !(uView == LVS_ICON || uView == LVS_SMALLICON)) return FALSE;
7174 LISTVIEW_GetOrigin(infoPtr, &Origin);
7176 /* This point value seems to be an undocumented feature.
7177 * The best guess is that it means either at the origin,
7178 * or at true beginning of the list. I will assume the origin. */
7179 if ((pt.x == -1) && (pt.y == -1))
7182 if (uView == LVS_ICON)
7184 pt.x -= (infoPtr->nItemWidth - infoPtr->iconSize.cx) / 2;
7185 pt.y -= ICON_TOP_PADDING;
7190 infoPtr->bAutoarrange = FALSE;
7192 return LISTVIEW_MoveIconTo(infoPtr, nItem, &pt, FALSE);
7197 * Sets the state of one or many items.
7200 * [I] infoPtr : valid pointer to the listview structure
7201 * [I] nItem : item index
7202 * [I] lpLVItem : item or subitem info
7208 static BOOL LISTVIEW_SetItemState(LISTVIEW_INFO *infoPtr, INT nItem, const LVITEMW *lpLVItem)
7210 BOOL bResult = TRUE;
7213 lvItem.iItem = nItem;
7214 lvItem.iSubItem = 0;
7215 lvItem.mask = LVIF_STATE;
7216 lvItem.state = lpLVItem->state;
7217 lvItem.stateMask = lpLVItem->stateMask;
7218 TRACE("lvItem=%s\n", debuglvitem_t(&lvItem, TRUE));
7222 /* apply to all items */
7223 for (lvItem.iItem = 0; lvItem.iItem < infoPtr->nItemCount; lvItem.iItem++)
7224 if (!LISTVIEW_SetItemT(infoPtr, &lvItem, TRUE)) bResult = FALSE;
7227 bResult = LISTVIEW_SetItemT(infoPtr, &lvItem, TRUE);
7230 * Update selection mark
7232 * Investigation on windows 2k showed that selection mark was updated
7233 * whenever a new selection was made, but if the selected item was
7234 * unselected it was not updated.
7236 * we are probably still not 100% accurate, but this at least sets the
7237 * proper selection mark when it is needed
7240 if (bResult && (lvItem.state & lvItem.stateMask & LVIS_SELECTED) &&
7241 ((infoPtr->nSelectionMark == -1) || (lvItem.iItem <= infoPtr->nSelectionMark)))
7244 infoPtr->nSelectionMark = -1;
7245 for (i = 0; i < infoPtr->nItemCount; i++)
7247 if (infoPtr->uCallbackMask & LVIS_SELECTED)
7249 if (LISTVIEW_GetItemState(infoPtr, i, LVIS_SELECTED))
7251 infoPtr->nSelectionMark = i;
7255 else if (ranges_contain(infoPtr->selectionRanges, i))
7257 infoPtr->nSelectionMark = i;
7268 * Sets the text of an item or subitem.
7271 * [I] hwnd : window handle
7272 * [I] nItem : item index
7273 * [I] lpLVItem : item or subitem info
7274 * [I] isW : TRUE if input is Unicode
7280 static BOOL LISTVIEW_SetItemTextT(LISTVIEW_INFO *infoPtr, INT nItem, const LVITEMW *lpLVItem, BOOL isW)
7284 if (nItem < 0 && nItem >= infoPtr->nItemCount) return FALSE;
7286 lvItem.iItem = nItem;
7287 lvItem.iSubItem = lpLVItem->iSubItem;
7288 lvItem.mask = LVIF_TEXT;
7289 lvItem.pszText = lpLVItem->pszText;
7290 lvItem.cchTextMax = lpLVItem->cchTextMax;
7292 TRACE("(nItem=%d, lpLVItem=%s, isW=%d)\n", nItem, debuglvitem_t(&lvItem, isW), isW);
7294 return LISTVIEW_SetItemT(infoPtr, &lvItem, isW);
7299 * Set item index that marks the start of a multiple selection.
7302 * [I] infoPtr : valid pointer to the listview structure
7303 * [I] nIndex : index
7306 * Index number or -1 if there is no selection mark.
7308 static INT LISTVIEW_SetSelectionMark(LISTVIEW_INFO *infoPtr, INT nIndex)
7310 INT nOldIndex = infoPtr->nSelectionMark;
7312 TRACE("(nIndex=%d)\n", nIndex);
7314 infoPtr->nSelectionMark = nIndex;
7321 * Sets the text background color.
7324 * [I] infoPtr : valid pointer to the listview structure
7325 * [I] clrTextBk : text background color
7331 static BOOL LISTVIEW_SetTextBkColor(LISTVIEW_INFO *infoPtr, COLORREF clrTextBk)
7333 TRACE("(clrTextBk=%lx)\n", clrTextBk);
7335 if (infoPtr->clrTextBk != clrTextBk)
7337 infoPtr->clrTextBk = clrTextBk;
7338 LISTVIEW_InvalidateList(infoPtr);
7346 * Sets the text foreground color.
7349 * [I] infoPtr : valid pointer to the listview structure
7350 * [I] clrText : text color
7356 static BOOL LISTVIEW_SetTextColor (LISTVIEW_INFO *infoPtr, COLORREF clrText)
7358 TRACE("(clrText=%lx)\n", clrText);
7360 if (infoPtr->clrText != clrText)
7362 infoPtr->clrText = clrText;
7363 LISTVIEW_InvalidateList(infoPtr);
7371 * Determines which listview item is located at the specified position.
7374 * [I] infoPtr : valid pointer to the listview structure
7375 * [I] hwndNewToolTip : handle to new ToolTip
7380 static HWND LISTVIEW_SetToolTips( LISTVIEW_INFO *infoPtr, HWND hwndNewToolTip)
7382 HWND hwndOldToolTip = infoPtr->hwndToolTip;
7383 infoPtr->hwndToolTip = hwndNewToolTip;
7384 return hwndOldToolTip;
7387 /* LISTVIEW_SetUnicodeFormat */
7388 /* LISTVIEW_SetWorkAreas */
7392 * Callback internally used by LISTVIEW_SortItems()
7395 * [I] first : pointer to first ITEM_INFO to compare
7396 * [I] second : pointer to second ITEM_INFO to compare
7397 * [I] lParam : HWND of control
7400 * if first comes before second : negative
7401 * if first comes after second : positive
7402 * if first and second are equivalent : zero
7404 static INT WINAPI LISTVIEW_CallBackCompare(LPVOID first, LPVOID second, LPARAM lParam)
7406 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)lParam;
7407 ITEM_INFO* lv_first = (ITEM_INFO*) DPA_GetPtr( (HDPA)first, 0 );
7408 ITEM_INFO* lv_second = (ITEM_INFO*) DPA_GetPtr( (HDPA)second, 0 );
7410 /* Forward the call to the client defined callback */
7411 return (infoPtr->pfnCompare)( lv_first->lParam , lv_second->lParam, infoPtr->lParamSort );
7416 * Sorts the listview items.
7419 * [I] infoPtr : valid pointer to the listview structure
7420 * [I] pfnCompare : application-defined value
7421 * [I] lParamSort : pointer to comparision callback
7427 static BOOL LISTVIEW_SortItems(LISTVIEW_INFO *infoPtr, PFNLVCOMPARE pfnCompare, LPARAM lParamSort)
7429 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
7432 LPVOID selectionMarkItem;
7436 TRACE("(pfnCompare=%p, lParamSort=%lx)\n", pfnCompare, lParamSort);
7438 if (infoPtr->dwStyle & LVS_OWNERDATA) return FALSE;
7440 if (!pfnCompare) return FALSE;
7441 if (!infoPtr->hdpaItems) return FALSE;
7443 /* if there are 0 or 1 items, there is no need to sort */
7444 if (infoPtr->nItemCount < 2) return TRUE;
7446 if (infoPtr->nFocusedItem >= 0)
7448 hdpaSubItems = (HDPA)DPA_GetPtr(infoPtr->hdpaItems, infoPtr->nFocusedItem);
7449 lpItem = (ITEM_INFO *)DPA_GetPtr(hdpaSubItems, 0);
7450 if (lpItem) lpItem->state |= LVIS_FOCUSED;
7452 /* FIXME: go thorugh selected items and mark them so in lpItem->state */
7453 /* clear the lpItem->state for non-selected ones */
7454 /* remove the selection ranges */
7456 infoPtr->pfnCompare = pfnCompare;
7457 infoPtr->lParamSort = lParamSort;
7458 DPA_Sort(infoPtr->hdpaItems, LISTVIEW_CallBackCompare, (LPARAM)infoPtr);
7460 /* Adjust selections and indices so that they are the way they should
7461 * be after the sort (otherwise, the list items move around, but
7462 * whatever is at the item's previous original position will be
7465 selectionMarkItem=(infoPtr->nSelectionMark>=0)?DPA_GetPtr(infoPtr->hdpaItems, infoPtr->nSelectionMark):NULL;
7466 for (i=0; i < infoPtr->nItemCount; i++)
7468 hdpaSubItems = (HDPA)DPA_GetPtr(infoPtr->hdpaItems, i);
7469 lpItem = (ITEM_INFO *)DPA_GetPtr(hdpaSubItems, 0);
7471 if (lpItem->state & LVIS_SELECTED)
7473 item.state = LVIS_SELECTED;
7474 item.stateMask = LVIS_SELECTED;
7475 LISTVIEW_SetItemState(infoPtr, i, &item);
7477 if (lpItem->state & LVIS_FOCUSED)
7479 infoPtr->nFocusedItem = i;
7480 lpItem->state &= ~LVIS_FOCUSED;
7483 if (selectionMarkItem != NULL)
7484 infoPtr->nSelectionMark = DPA_GetPtrIndex(infoPtr->hdpaItems, selectionMarkItem);
7485 /* I believe nHotItem should be left alone, see LISTVIEW_ShiftIndices */
7487 /* refresh the display */
7488 if (uView != LVS_ICON && uView != LVS_SMALLICON)
7489 LISTVIEW_InvalidateList(infoPtr);
7496 * Update theme handle after a theme change.
7499 * [I] infoPtr : valid pointer to the listview structure
7503 * FAILURE : something else
7505 static LRESULT LISTVIEW_ThemeChanged(LISTVIEW_INFO *infoPtr)
7507 HTHEME theme = GetWindowTheme(infoPtr->hwndSelf);
7508 CloseThemeData(theme);
7509 OpenThemeData(infoPtr->hwndSelf, themeClass);
7515 * Updates an items or rearranges the listview control.
7518 * [I] infoPtr : valid pointer to the listview structure
7519 * [I] nItem : item index
7525 static BOOL LISTVIEW_Update(LISTVIEW_INFO *infoPtr, INT nItem)
7527 TRACE("(nItem=%d)\n", nItem);
7529 if (nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
7531 /* rearrange with default alignment style */
7532 if (is_autoarrange(infoPtr))
7533 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
7535 LISTVIEW_InvalidateItem(infoPtr, nItem);
7543 * Creates the listview control.
7546 * [I] hwnd : window handle
7547 * [I] lpcs : the create parameters
7553 static LRESULT LISTVIEW_Create(HWND hwnd, const CREATESTRUCTW *lpcs)
7555 LISTVIEW_INFO *infoPtr;
7556 UINT uView = lpcs->style & LVS_TYPEMASK;
7559 TRACE("(lpcs=%p)\n", lpcs);
7561 /* initialize info pointer */
7562 infoPtr = (LISTVIEW_INFO *)Alloc(sizeof(LISTVIEW_INFO));
7563 if (!infoPtr) return -1;
7565 SetWindowLongPtrW(hwnd, 0, (DWORD_PTR)infoPtr);
7567 infoPtr->hwndSelf = hwnd;
7568 infoPtr->dwStyle = lpcs->style;
7569 /* determine the type of structures to use */
7570 infoPtr->hwndNotify = lpcs->hwndParent;
7571 infoPtr->notifyFormat = SendMessageW(infoPtr->hwndNotify, WM_NOTIFYFORMAT,
7572 (WPARAM)infoPtr->hwndSelf, (LPARAM)NF_QUERY);
7574 /* initialize color information */
7575 infoPtr->clrBk = CLR_NONE;
7576 infoPtr->clrText = comctl32_color.clrWindowText;
7577 infoPtr->clrTextBk = CLR_DEFAULT;
7578 LISTVIEW_SetBkColor(infoPtr, comctl32_color.clrWindow);
7580 /* set default values */
7581 infoPtr->nFocusedItem = -1;
7582 infoPtr->nSelectionMark = -1;
7583 infoPtr->nHotItem = -1;
7584 infoPtr->bRedraw = TRUE;
7585 infoPtr->bNoItemMetrics = TRUE;
7586 infoPtr->bDoChangeNotify = TRUE;
7587 infoPtr->iconSpacing.cx = GetSystemMetrics(SM_CXICONSPACING);
7588 infoPtr->iconSpacing.cy = GetSystemMetrics(SM_CYICONSPACING);
7589 infoPtr->nEditLabelItem = -1;
7590 infoPtr->dwHoverTime = -1; /* default system hover time */
7591 infoPtr->nMeasureItemHeight = 0;
7593 /* get default font (icon title) */
7594 SystemParametersInfoW(SPI_GETICONTITLELOGFONT, 0, &logFont, 0);
7595 infoPtr->hDefaultFont = CreateFontIndirectW(&logFont);
7596 infoPtr->hFont = infoPtr->hDefaultFont;
7597 LISTVIEW_SaveTextMetrics(infoPtr);
7600 infoPtr->hwndHeader = CreateWindowW(WC_HEADERW, NULL,
7601 WS_CHILD | HDS_HORZ | (DWORD)((LVS_NOSORTHEADER & lpcs->style)?0:HDS_BUTTONS),
7602 0, 0, 0, 0, hwnd, NULL,
7603 lpcs->hInstance, NULL);
7604 if (!infoPtr->hwndHeader) goto fail;
7606 /* set header unicode format */
7607 SendMessageW(infoPtr->hwndHeader, HDM_SETUNICODEFORMAT, (WPARAM)TRUE, (LPARAM)NULL);
7609 /* set header font */
7610 SendMessageW(infoPtr->hwndHeader, WM_SETFONT, (WPARAM)infoPtr->hFont, (LPARAM)TRUE);
7612 /* allocate memory for the data structure */
7613 if (!(infoPtr->selectionRanges = ranges_create(10))) goto fail;
7614 if (!(infoPtr->hdpaItems = DPA_Create(10))) goto fail;
7615 if (!(infoPtr->hdpaPosX = DPA_Create(10))) goto fail;
7616 if (!(infoPtr->hdpaPosY = DPA_Create(10))) goto fail;
7617 if (!(infoPtr->hdpaColumns = DPA_Create(10))) goto fail;
7619 /* initialize the icon sizes */
7620 set_icon_size(&infoPtr->iconSize, infoPtr->himlNormal, uView != LVS_ICON);
7621 set_icon_size(&infoPtr->iconStateSize, infoPtr->himlState, TRUE);
7623 /* init item size to avoid division by 0 */
7624 LISTVIEW_UpdateItemSize (infoPtr);
7626 if (uView == LVS_REPORT)
7628 if (!(LVS_NOCOLUMNHEADER & lpcs->style))
7630 ShowWindow(infoPtr->hwndHeader, SW_SHOWNORMAL);
7634 /* set HDS_HIDDEN flag to hide the header bar */
7635 SetWindowLongW(infoPtr->hwndHeader, GWL_STYLE,
7636 GetWindowLongW(infoPtr->hwndHeader, GWL_STYLE) | HDS_HIDDEN);
7640 OpenThemeData(hwnd, themeClass);
7645 DestroyWindow(infoPtr->hwndHeader);
7646 ranges_destroy(infoPtr->selectionRanges);
7647 DPA_Destroy(infoPtr->hdpaItems);
7648 DPA_Destroy(infoPtr->hdpaPosX);
7649 DPA_Destroy(infoPtr->hdpaPosY);
7650 DPA_Destroy(infoPtr->hdpaColumns);
7657 * Destroys the listview control.
7660 * [I] infoPtr : valid pointer to the listview structure
7666 static LRESULT LISTVIEW_Destroy(LISTVIEW_INFO *infoPtr)
7668 HTHEME theme = GetWindowTheme(infoPtr->hwndSelf);
7669 CloseThemeData(theme);
7675 * Enables the listview control.
7678 * [I] infoPtr : valid pointer to the listview structure
7679 * [I] bEnable : specifies whether to enable or disable the window
7685 static BOOL LISTVIEW_Enable(LISTVIEW_INFO *infoPtr, BOOL bEnable)
7687 if (infoPtr->dwStyle & LVS_OWNERDRAWFIXED)
7688 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
7694 * Erases the background of the listview control.
7697 * [I] infoPtr : valid pointer to the listview structure
7698 * [I] hdc : device context handle
7704 static inline BOOL LISTVIEW_EraseBkgnd(LISTVIEW_INFO *infoPtr, HDC hdc)
7708 TRACE("(hdc=%p)\n", hdc);
7710 if (!GetClipBox(hdc, &rc)) return FALSE;
7712 return LISTVIEW_FillBkgnd(infoPtr, hdc, &rc);
7718 * Helper function for LISTVIEW_[HV]Scroll *only*.
7719 * Performs vertical/horizontal scrolling by a give amount.
7722 * [I] infoPtr : valid pointer to the listview structure
7723 * [I] dx : amount of horizontal scroll
7724 * [I] dy : amount of vertical scroll
7726 static void scroll_list(LISTVIEW_INFO *infoPtr, INT dx, INT dy)
7728 /* now we can scroll the list */
7729 ScrollWindowEx(infoPtr->hwndSelf, dx, dy, &infoPtr->rcList,
7730 &infoPtr->rcList, 0, 0, SW_ERASE | SW_INVALIDATE);
7731 /* if we have focus, adjust rect */
7732 OffsetRect(&infoPtr->rcFocus, dx, dy);
7733 UpdateWindow(infoPtr->hwndSelf);
7738 * Performs vertical scrolling.
7741 * [I] infoPtr : valid pointer to the listview structure
7742 * [I] nScrollCode : scroll code
7743 * [I] nScrollDiff : units to scroll in SB_INTERNAL mode, 0 otherwise
7744 * [I] hScrollWnd : scrollbar control window handle
7750 * SB_LINEUP/SB_LINEDOWN:
7751 * for LVS_ICON, LVS_SMALLICON is 37 by experiment
7752 * for LVS_REPORT is 1 line
7753 * for LVS_LIST cannot occur
7756 static LRESULT LISTVIEW_VScroll(LISTVIEW_INFO *infoPtr, INT nScrollCode,
7757 INT nScrollDiff, HWND hScrollWnd)
7759 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
7760 INT nOldScrollPos, nNewScrollPos;
7761 SCROLLINFO scrollInfo;
7764 TRACE("(nScrollCode=%d(%s), nScrollDiff=%d)\n", nScrollCode,
7765 debugscrollcode(nScrollCode), nScrollDiff);
7767 if (infoPtr->hwndEdit) SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
7769 scrollInfo.cbSize = sizeof(SCROLLINFO);
7770 scrollInfo.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_TRACKPOS;
7772 is_an_icon = ((uView == LVS_ICON) || (uView == LVS_SMALLICON));
7774 if (!GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo)) return 1;
7776 nOldScrollPos = scrollInfo.nPos;
7777 switch (nScrollCode)
7783 nScrollDiff = (is_an_icon) ? -LISTVIEW_SCROLL_ICON_LINE_SIZE : -1;
7787 nScrollDiff = (is_an_icon) ? LISTVIEW_SCROLL_ICON_LINE_SIZE : 1;
7791 nScrollDiff = -scrollInfo.nPage;
7795 nScrollDiff = scrollInfo.nPage;
7798 case SB_THUMBPOSITION:
7800 nScrollDiff = scrollInfo.nTrackPos - scrollInfo.nPos;
7807 /* quit right away if pos isn't changing */
7808 if (nScrollDiff == 0) return 0;
7810 /* calculate new position, and handle overflows */
7811 nNewScrollPos = scrollInfo.nPos + nScrollDiff;
7812 if (nScrollDiff > 0) {
7813 if (nNewScrollPos < nOldScrollPos ||
7814 nNewScrollPos > scrollInfo.nMax)
7815 nNewScrollPos = scrollInfo.nMax;
7817 if (nNewScrollPos > nOldScrollPos ||
7818 nNewScrollPos < scrollInfo.nMin)
7819 nNewScrollPos = scrollInfo.nMin;
7822 /* set the new position, and reread in case it changed */
7823 scrollInfo.fMask = SIF_POS;
7824 scrollInfo.nPos = nNewScrollPos;
7825 nNewScrollPos = SetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo, TRUE);
7827 /* carry on only if it really changed */
7828 if (nNewScrollPos == nOldScrollPos) return 0;
7830 /* now adjust to client coordinates */
7831 nScrollDiff = nOldScrollPos - nNewScrollPos;
7832 if (uView == LVS_REPORT) nScrollDiff *= infoPtr->nItemHeight;
7834 /* and scroll the window */
7835 scroll_list(infoPtr, 0, nScrollDiff);
7842 * Performs horizontal scrolling.
7845 * [I] infoPtr : valid pointer to the listview structure
7846 * [I] nScrollCode : scroll code
7847 * [I] nScrollDiff : units to scroll in SB_INTERNAL mode, 0 otherwise
7848 * [I] hScrollWnd : scrollbar control window handle
7854 * SB_LINELEFT/SB_LINERIGHT:
7855 * for LVS_ICON, LVS_SMALLICON 1 pixel
7856 * for LVS_REPORT is 1 pixel
7857 * for LVS_LIST is 1 column --> which is a 1 because the
7858 * scroll is based on columns not pixels
7861 static LRESULT LISTVIEW_HScroll(LISTVIEW_INFO *infoPtr, INT nScrollCode,
7862 INT nScrollDiff, HWND hScrollWnd)
7864 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
7865 INT nOldScrollPos, nNewScrollPos;
7866 SCROLLINFO scrollInfo;
7868 TRACE("(nScrollCode=%d(%s), nScrollDiff=%d)\n", nScrollCode,
7869 debugscrollcode(nScrollCode), nScrollDiff);
7871 if (infoPtr->hwndEdit) SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
7873 scrollInfo.cbSize = sizeof(SCROLLINFO);
7874 scrollInfo.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_TRACKPOS;
7876 if (!GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo)) return 1;
7878 nOldScrollPos = scrollInfo.nPos;
7880 switch (nScrollCode)
7894 nScrollDiff = -scrollInfo.nPage;
7898 nScrollDiff = scrollInfo.nPage;
7901 case SB_THUMBPOSITION:
7903 nScrollDiff = scrollInfo.nTrackPos - scrollInfo.nPos;
7910 /* quit right away if pos isn't changing */
7911 if (nScrollDiff == 0) return 0;
7913 /* calculate new position, and handle overflows */
7914 nNewScrollPos = scrollInfo.nPos + nScrollDiff;
7915 if (nScrollDiff > 0) {
7916 if (nNewScrollPos < nOldScrollPos ||
7917 nNewScrollPos > scrollInfo.nMax)
7918 nNewScrollPos = scrollInfo.nMax;
7920 if (nNewScrollPos > nOldScrollPos ||
7921 nNewScrollPos < scrollInfo.nMin)
7922 nNewScrollPos = scrollInfo.nMin;
7925 /* set the new position, and reread in case it changed */
7926 scrollInfo.fMask = SIF_POS;
7927 scrollInfo.nPos = nNewScrollPos;
7928 nNewScrollPos = SetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo, TRUE);
7930 /* carry on only if it really changed */
7931 if (nNewScrollPos == nOldScrollPos) return 0;
7933 if(uView == LVS_REPORT)
7934 LISTVIEW_UpdateHeaderSize(infoPtr, nNewScrollPos);
7936 /* now adjust to client coordinates */
7937 nScrollDiff = nOldScrollPos - nNewScrollPos;
7938 if (uView == LVS_LIST) nScrollDiff *= infoPtr->nItemWidth;
7940 /* and scroll the window */
7941 scroll_list(infoPtr, nScrollDiff, 0);
7946 static LRESULT LISTVIEW_MouseWheel(LISTVIEW_INFO *infoPtr, INT wheelDelta)
7948 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
7949 INT gcWheelDelta = 0;
7950 INT pulScrollLines = 3;
7951 SCROLLINFO scrollInfo;
7953 TRACE("(wheelDelta=%d)\n", wheelDelta);
7955 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
7956 gcWheelDelta -= wheelDelta;
7958 scrollInfo.cbSize = sizeof(SCROLLINFO);
7959 scrollInfo.fMask = SIF_POS;
7966 * listview should be scrolled by a multiple of 37 dependently on its dimension or its visible item number
7967 * should be fixed in the future.
7969 LISTVIEW_VScroll(infoPtr, SB_INTERNAL, (gcWheelDelta < 0) ?
7970 -LISTVIEW_SCROLL_ICON_LINE_SIZE : LISTVIEW_SCROLL_ICON_LINE_SIZE, 0);
7974 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
7976 int cLineScroll = min(LISTVIEW_GetCountPerColumn(infoPtr), pulScrollLines);
7977 cLineScroll *= (gcWheelDelta / WHEEL_DELTA);
7978 LISTVIEW_VScroll(infoPtr, SB_INTERNAL, cLineScroll, 0);
7983 LISTVIEW_HScroll(infoPtr, (gcWheelDelta < 0) ? SB_LINELEFT : SB_LINERIGHT, 0, 0);
7994 * [I] infoPtr : valid pointer to the listview structure
7995 * [I] nVirtualKey : virtual key
7996 * [I] lKeyData : key data
8001 static LRESULT LISTVIEW_KeyDown(LISTVIEW_INFO *infoPtr, INT nVirtualKey, LONG lKeyData)
8003 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
8004 HWND hwndSelf = infoPtr->hwndSelf;
8006 NMLVKEYDOWN nmKeyDown;
8008 TRACE("(nVirtualKey=%d, lKeyData=%ld)\n", nVirtualKey, lKeyData);
8010 /* send LVN_KEYDOWN notification */
8011 nmKeyDown.wVKey = nVirtualKey;
8012 nmKeyDown.flags = 0;
8013 notify_hdr(infoPtr, LVN_KEYDOWN, &nmKeyDown.hdr);
8014 if (!IsWindow(hwndSelf))
8017 switch (nVirtualKey)
8020 if ((infoPtr->nItemCount > 0) && (infoPtr->nFocusedItem != -1))
8022 if (!notify(infoPtr, NM_RETURN)) return 0;
8023 if (!notify(infoPtr, LVN_ITEMACTIVATE)) return 0;
8028 if (infoPtr->nItemCount > 0)
8033 if (infoPtr->nItemCount > 0)
8034 nItem = infoPtr->nItemCount - 1;
8038 nItem = ListView_GetNextItem(infoPtr->hwndSelf, infoPtr->nFocusedItem, LVNI_TOLEFT);
8042 nItem = ListView_GetNextItem(infoPtr->hwndSelf, infoPtr->nFocusedItem, LVNI_ABOVE);
8046 nItem = ListView_GetNextItem(infoPtr->hwndSelf, infoPtr->nFocusedItem, LVNI_TORIGHT);
8050 nItem = ListView_GetNextItem(infoPtr->hwndSelf, infoPtr->nFocusedItem, LVNI_BELOW);
8054 if (uView == LVS_REPORT)
8056 INT topidx = LISTVIEW_GetTopIndex(infoPtr);
8057 if (infoPtr->nFocusedItem == topidx)
8058 nItem = topidx - LISTVIEW_GetCountPerColumn(infoPtr) + 1;
8063 nItem = infoPtr->nFocusedItem - LISTVIEW_GetCountPerColumn(infoPtr)
8064 * LISTVIEW_GetCountPerRow(infoPtr);
8065 if(nItem < 0) nItem = 0;
8069 if (uView == LVS_REPORT)
8071 INT topidx = LISTVIEW_GetTopIndex(infoPtr);
8072 INT cnt = LISTVIEW_GetCountPerColumn(infoPtr);
8073 if (infoPtr->nFocusedItem == topidx + cnt - 1)
8074 nItem = infoPtr->nFocusedItem + cnt - 1;
8076 nItem = topidx + cnt - 1;
8079 nItem = infoPtr->nFocusedItem + LISTVIEW_GetCountPerColumn(infoPtr)
8080 * LISTVIEW_GetCountPerRow(infoPtr);
8081 if(nItem >= infoPtr->nItemCount) nItem = infoPtr->nItemCount - 1;
8085 if ((nItem != -1) && (nItem != infoPtr->nFocusedItem))
8086 LISTVIEW_KeySelection(infoPtr, nItem);
8096 * [I] infoPtr : valid pointer to the listview structure
8101 static LRESULT LISTVIEW_KillFocus(LISTVIEW_INFO *infoPtr)
8105 /* if we did not have the focus, there's nothing to do */
8106 if (!infoPtr->bFocus) return 0;
8108 /* send NM_KILLFOCUS notification */
8109 if (!notify(infoPtr, NM_KILLFOCUS)) return 0;
8111 /* if we have a focus rectagle, get rid of it */
8112 LISTVIEW_ShowFocusRect(infoPtr, FALSE);
8114 /* set window focus flag */
8115 infoPtr->bFocus = FALSE;
8117 /* invalidate the selected items before reseting focus flag */
8118 LISTVIEW_InvalidateSelectedItems(infoPtr);
8125 * Processes double click messages (left mouse button).
8128 * [I] infoPtr : valid pointer to the listview structure
8129 * [I] wKey : key flag
8130 * [I] x,y : mouse coordinate
8135 static LRESULT LISTVIEW_LButtonDblClk(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
8137 LVHITTESTINFO htInfo;
8139 TRACE("(key=%hu, X=%hu, Y=%hu)\n", wKey, x, y);
8141 /* send NM_RELEASEDCAPTURE notification */
8142 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
8147 /* send NM_DBLCLK notification */
8148 LISTVIEW_HitTest(infoPtr, &htInfo, TRUE, FALSE);
8149 if (!notify_click(infoPtr, NM_DBLCLK, &htInfo)) return 0;
8151 /* To send the LVN_ITEMACTIVATE, it must be on an Item */
8152 if(htInfo.iItem != -1) notify_itemactivate(infoPtr,&htInfo);
8159 * Processes mouse down messages (left mouse button).
8162 * infoPtr [I ] valid pointer to the listview structure
8163 * wKey [I ] key flag
8164 * x,y [I ] mouse coordinate
8169 static LRESULT LISTVIEW_LButtonDown(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
8171 LVHITTESTINFO lvHitTestInfo;
8172 static BOOL bGroupSelect = TRUE;
8173 POINT pt = { x, y };
8176 TRACE("(key=%hu, X=%hu, Y=%hu)\n", wKey, x, y);
8178 /* send NM_RELEASEDCAPTURE notification */
8179 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
8181 if (!infoPtr->bFocus) SetFocus(infoPtr->hwndSelf);
8183 /* set left button down flag and record the click position */
8184 infoPtr->bLButtonDown = TRUE;
8185 infoPtr->ptClickPos = pt;
8187 lvHitTestInfo.pt.x = x;
8188 lvHitTestInfo.pt.y = y;
8190 nItem = LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, TRUE);
8191 TRACE("at %s, nItem=%d\n", debugpoint(&pt), nItem);
8192 infoPtr->nEditLabelItem = -1;
8193 if ((nItem >= 0) && (nItem < infoPtr->nItemCount))
8195 if ((infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES) && (lvHitTestInfo.flags & LVHT_ONITEMSTATEICON))
8197 DWORD state = STATEIMAGEINDEX(LISTVIEW_GetItemState(infoPtr, nItem, LVIS_STATEIMAGEMASK));
8198 if(state == 1 || state == 2)
8202 lvitem.state = INDEXTOSTATEIMAGEMASK(state);
8203 lvitem.stateMask = LVIS_STATEIMAGEMASK;
8204 LISTVIEW_SetItemState(infoPtr, nItem, &lvitem);
8209 if (infoPtr->dwStyle & LVS_SINGLESEL)
8211 if (LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED))
8212 infoPtr->nEditLabelItem = nItem;
8214 LISTVIEW_SetSelection(infoPtr, nItem);
8218 if ((wKey & MK_CONTROL) && (wKey & MK_SHIFT))
8222 if (!LISTVIEW_AddGroupSelection(infoPtr, nItem)) return 0;
8223 LISTVIEW_SetItemFocus(infoPtr, nItem);
8224 infoPtr->nSelectionMark = nItem;
8230 item.state = LVIS_SELECTED | LVIS_FOCUSED;
8231 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
8233 LISTVIEW_SetItemState(infoPtr,nItem,&item);
8234 infoPtr->nSelectionMark = nItem;
8237 else if (wKey & MK_CONTROL)
8241 bGroupSelect = (LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED) == 0);
8243 item.state = (bGroupSelect ? LVIS_SELECTED : 0) | LVIS_FOCUSED;
8244 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
8245 LISTVIEW_SetItemState(infoPtr, nItem, &item);
8246 infoPtr->nSelectionMark = nItem;
8248 else if (wKey & MK_SHIFT)
8250 LISTVIEW_SetGroupSelection(infoPtr, nItem);
8254 if (LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED))
8255 infoPtr->nEditLabelItem = nItem;
8257 /* set selection (clears other pre-existing selections) */
8258 LISTVIEW_SetSelection(infoPtr, nItem);
8264 /* remove all selections */
8265 LISTVIEW_DeselectAll(infoPtr);
8274 * Processes mouse up messages (left mouse button).
8277 * infoPtr [I ] valid pointer to the listview structure
8278 * wKey [I ] key flag
8279 * x,y [I ] mouse coordinate
8284 static LRESULT LISTVIEW_LButtonUp(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
8286 LVHITTESTINFO lvHitTestInfo;
8288 TRACE("(key=%hu, X=%hu, Y=%hu)\n", wKey, x, y);
8290 if (!infoPtr->bLButtonDown) return 0;
8292 lvHitTestInfo.pt.x = x;
8293 lvHitTestInfo.pt.y = y;
8295 /* send NM_CLICK notification */
8296 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
8297 if (!notify_click(infoPtr, NM_CLICK, &lvHitTestInfo)) return 0;
8299 /* set left button flag */
8300 infoPtr->bLButtonDown = FALSE;
8302 /* if we clicked on a selected item, edit the label */
8303 if(lvHitTestInfo.iItem == infoPtr->nEditLabelItem && (lvHitTestInfo.flags & LVHT_ONITEMLABEL))
8304 LISTVIEW_EditLabelT(infoPtr, lvHitTestInfo.iItem, TRUE);
8311 * Destroys the listview control (called after WM_DESTROY).
8314 * [I] infoPtr : valid pointer to the listview structure
8319 static LRESULT LISTVIEW_NCDestroy(LISTVIEW_INFO *infoPtr)
8323 /* delete all items */
8324 LISTVIEW_DeleteAllItems(infoPtr);
8326 /* destroy data structure */
8327 DPA_Destroy(infoPtr->hdpaItems);
8328 DPA_Destroy(infoPtr->hdpaPosX);
8329 DPA_Destroy(infoPtr->hdpaPosY);
8330 DPA_Destroy(infoPtr->hdpaColumns);
8331 ranges_destroy(infoPtr->selectionRanges);
8333 /* destroy image lists */
8334 if (!(infoPtr->dwStyle & LVS_SHAREIMAGELISTS))
8336 if (infoPtr->himlNormal)
8337 ImageList_Destroy(infoPtr->himlNormal);
8338 if (infoPtr->himlSmall)
8339 ImageList_Destroy(infoPtr->himlSmall);
8340 if (infoPtr->himlState)
8341 ImageList_Destroy(infoPtr->himlState);
8344 /* destroy font, bkgnd brush */
8346 if (infoPtr->hDefaultFont) DeleteObject(infoPtr->hDefaultFont);
8347 if (infoPtr->clrBk != CLR_NONE) DeleteObject(infoPtr->hBkBrush);
8349 SetWindowLongPtrW(infoPtr->hwndSelf, 0, 0);
8351 /* free listview info pointer*/
8359 * Handles notifications from header.
8362 * [I] infoPtr : valid pointer to the listview structure
8363 * [I] nCtrlId : control identifier
8364 * [I] lpnmh : notification information
8369 static LRESULT LISTVIEW_HeaderNotification(LISTVIEW_INFO *infoPtr, const NMHEADERW *lpnmh)
8371 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
8372 HWND hwndSelf = infoPtr->hwndSelf;
8374 TRACE("(lpnmh=%p)\n", lpnmh);
8376 if (!lpnmh || lpnmh->iItem < 0 || lpnmh->iItem >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return 0;
8378 switch (lpnmh->hdr.code)
8380 case HDN_ITEMCHANGINGW:
8381 case HDN_ITEMCHANGINGA:
8382 return notify_forward_header(infoPtr, lpnmh);
8383 case HDN_ITEMCHANGEDW:
8384 case HDN_ITEMCHANGEDA:
8385 notify_forward_header(infoPtr, lpnmh);
8386 if (!IsWindow(hwndSelf))
8392 COLUMN_INFO *lpColumnInfo;
8395 if (!lpnmh->pitem || !(lpnmh->pitem->mask & HDI_WIDTH))
8399 hdi.mask = HDI_WIDTH;
8400 if (!Header_GetItemW(infoPtr->hwndHeader, lpnmh->iItem, (LPARAM)&hdi)) return 0;
8404 cxy = lpnmh->pitem->cxy;
8406 /* determine how much we change since the last know position */
8407 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, lpnmh->iItem);
8408 dx = cxy - (lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left);
8411 lpColumnInfo->rcHeader.right += dx;
8412 LISTVIEW_ScrollColumns(infoPtr, lpnmh->iItem + 1, dx);
8413 LISTVIEW_UpdateItemSize(infoPtr);
8414 if (uView == LVS_REPORT && is_redrawing(infoPtr))
8417 RECT rcCol = lpColumnInfo->rcHeader;
8419 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
8420 OffsetRect(&rcCol, ptOrigin.x, 0);
8422 rcCol.top = infoPtr->rcList.top;
8423 rcCol.bottom = infoPtr->rcList.bottom;
8425 /* resizing left-aligned columns leaves most of the left side untouched */
8426 if ((lpColumnInfo->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_LEFT)
8428 INT nMaxDirty = infoPtr->nEllipsisWidth + infoPtr->ntmMaxCharWidth + dx;
8429 rcCol.left = max (rcCol.left, rcCol.right - nMaxDirty);
8432 LISTVIEW_InvalidateRect(infoPtr, &rcCol);
8438 case HDN_ITEMCLICKW:
8439 case HDN_ITEMCLICKA:
8441 /* Handle sorting by Header Column */
8444 ZeroMemory(&nmlv, sizeof(NMLISTVIEW));
8446 nmlv.iSubItem = lpnmh->iItem;
8447 notify_listview(infoPtr, LVN_COLUMNCLICK, &nmlv);
8451 case HDN_DIVIDERDBLCLICKW:
8452 case HDN_DIVIDERDBLCLICKA:
8453 LISTVIEW_SetColumnWidth(infoPtr, lpnmh->iItem, LVSCW_AUTOSIZE);
8462 * Paint non-client area of control.
8465 * [I] infoPtr : valid pointer to the listview structureof the sender
8466 * [I] region : update region
8469 * TRUE - frame was painted
8470 * FALSE - call default window proc
8472 static BOOL LISTVIEW_NCPaint(LISTVIEW_INFO *infoPtr, HRGN region)
8474 HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
8478 int cxEdge = GetSystemMetrics (SM_CXEDGE),
8479 cyEdge = GetSystemMetrics (SM_CYEDGE);
8481 if (!theme) return FALSE;
8483 GetWindowRect(infoPtr->hwndSelf, &r);
8485 cliprgn = CreateRectRgn (r.left + cxEdge, r.top + cyEdge,
8486 r.right - cxEdge, r.bottom - cyEdge);
8487 if (region != (HRGN)1)
8488 CombineRgn (cliprgn, cliprgn, region, RGN_AND);
8489 OffsetRect(&r, -r.left, -r.top);
8491 dc = GetDCEx(infoPtr->hwndSelf, region, DCX_WINDOW|DCX_INTERSECTRGN);
8492 OffsetRect(&r, -r.left, -r.top);
8494 if (IsThemeBackgroundPartiallyTransparent (theme, 0, 0))
8495 DrawThemeParentBackground(infoPtr->hwndSelf, dc, &r);
8496 DrawThemeBackground (theme, dc, 0, 0, &r, 0);
8497 ReleaseDC(infoPtr->hwndSelf, dc);
8499 /* Call default proc to get the scrollbars etc. painted */
8500 DefWindowProcW (infoPtr->hwndSelf, WM_NCPAINT, (WPARAM)cliprgn, 0);
8507 * Determines the type of structure to use.
8510 * [I] infoPtr : valid pointer to the listview structureof the sender
8511 * [I] hwndFrom : listview window handle
8512 * [I] nCommand : command specifying the nature of the WM_NOTIFYFORMAT
8517 static LRESULT LISTVIEW_NotifyFormat(LISTVIEW_INFO *infoPtr, HWND hwndFrom, INT nCommand)
8519 TRACE("(hwndFrom=%p, nCommand=%d)\n", hwndFrom, nCommand);
8521 if (nCommand != NF_REQUERY) return 0;
8523 infoPtr->notifyFormat = SendMessageW(hwndFrom, WM_NOTIFYFORMAT, (WPARAM)infoPtr->hwndSelf, NF_QUERY);
8530 * Paints/Repaints the listview control.
8533 * [I] infoPtr : valid pointer to the listview structure
8534 * [I] hdc : device context handle
8539 static LRESULT LISTVIEW_Paint(LISTVIEW_INFO *infoPtr, HDC hdc)
8541 TRACE("(hdc=%p)\n", hdc);
8543 if (infoPtr->bNoItemMetrics && infoPtr->nItemCount)
8545 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
8547 infoPtr->bNoItemMetrics = FALSE;
8548 LISTVIEW_UpdateItemSize(infoPtr);
8549 if (uView == LVS_ICON || uView == LVS_SMALLICON)
8550 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
8551 LISTVIEW_UpdateScroll(infoPtr);
8554 LISTVIEW_Refresh(infoPtr, hdc);
8559 hdc = BeginPaint(infoPtr->hwndSelf, &ps);
8561 if (ps.fErase) LISTVIEW_FillBkgnd(infoPtr, hdc, &ps.rcPaint);
8562 LISTVIEW_Refresh(infoPtr, hdc);
8563 EndPaint(infoPtr->hwndSelf, &ps);
8572 * Paints/Repaints the listview control.
8575 * [I] infoPtr : valid pointer to the listview structure
8576 * [I] hdc : device context handle
8577 * [I] options : drawing options
8582 static LRESULT LISTVIEW_PrintClient(LISTVIEW_INFO *infoPtr, HDC hdc, DWORD options)
8584 FIXME("Partial Stub: (hdc=%p options=0x%08lx)\n", hdc, options);
8586 if ((options & PRF_CHECKVISIBLE) && !IsWindowVisible(infoPtr->hwndSelf))
8589 if (options & PRF_ERASEBKGND)
8590 LISTVIEW_EraseBkgnd(infoPtr, hdc);
8592 if (options & PRF_CLIENT)
8593 LISTVIEW_Paint(infoPtr, hdc);
8601 * Processes double click messages (right mouse button).
8604 * [I] infoPtr : valid pointer to the listview structure
8605 * [I] wKey : key flag
8606 * [I] x,y : mouse coordinate
8611 static LRESULT LISTVIEW_RButtonDblClk(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
8613 LVHITTESTINFO lvHitTestInfo;
8615 TRACE("(key=%hu,X=%hu,Y=%hu)\n", wKey, x, y);
8617 /* send NM_RELEASEDCAPTURE notification */
8618 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
8620 /* send NM_RDBLCLK notification */
8621 lvHitTestInfo.pt.x = x;
8622 lvHitTestInfo.pt.y = y;
8623 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
8624 notify_click(infoPtr, NM_RDBLCLK, &lvHitTestInfo);
8631 * Processes mouse down messages (right mouse button).
8634 * [I] infoPtr : valid pointer to the listview structure
8635 * [I] wKey : key flag
8636 * [I] x,y : mouse coordinate
8641 static LRESULT LISTVIEW_RButtonDown(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
8643 LVHITTESTINFO lvHitTestInfo;
8646 TRACE("(key=%hu,X=%hu,Y=%hu)\n", wKey, x, y);
8648 /* send NM_RELEASEDCAPTURE notification */
8649 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
8651 /* make sure the listview control window has the focus */
8652 if (!infoPtr->bFocus) SetFocus(infoPtr->hwndSelf);
8654 /* set right button down flag */
8655 infoPtr->bRButtonDown = TRUE;
8657 /* determine the index of the selected item */
8658 lvHitTestInfo.pt.x = x;
8659 lvHitTestInfo.pt.y = y;
8660 nItem = LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, TRUE);
8662 if ((nItem >= 0) && (nItem < infoPtr->nItemCount))
8664 LISTVIEW_SetItemFocus(infoPtr, nItem);
8665 if (!((wKey & MK_SHIFT) || (wKey & MK_CONTROL)) &&
8666 !LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED))
8667 LISTVIEW_SetSelection(infoPtr, nItem);
8671 LISTVIEW_DeselectAll(infoPtr);
8679 * Processes mouse up messages (right mouse button).
8682 * [I] infoPtr : valid pointer to the listview structure
8683 * [I] wKey : key flag
8684 * [I] x,y : mouse coordinate
8689 static LRESULT LISTVIEW_RButtonUp(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
8691 LVHITTESTINFO lvHitTestInfo;
8694 TRACE("(key=%hu,X=%hu,Y=%hu)\n", wKey, x, y);
8696 if (!infoPtr->bRButtonDown) return 0;
8698 /* set button flag */
8699 infoPtr->bRButtonDown = FALSE;
8701 /* Send NM_RClICK notification */
8702 lvHitTestInfo.pt.x = x;
8703 lvHitTestInfo.pt.y = y;
8704 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
8705 if (!notify_click(infoPtr, NM_RCLICK, &lvHitTestInfo)) return 0;
8707 /* Change to screen coordinate for WM_CONTEXTMENU */
8708 pt = lvHitTestInfo.pt;
8709 ClientToScreen(infoPtr->hwndSelf, &pt);
8711 /* Send a WM_CONTEXTMENU message in response to the RBUTTONUP */
8712 SendMessageW(infoPtr->hwndSelf, WM_CONTEXTMENU,
8713 (WPARAM)infoPtr->hwndSelf, MAKELPARAM(pt.x, pt.y));
8724 * [I] infoPtr : valid pointer to the listview structure
8725 * [I] hwnd : window handle of window containing the cursor
8726 * [I] nHittest : hit-test code
8727 * [I] wMouseMsg : ideintifier of the mouse message
8730 * TRUE if cursor is set
8733 static BOOL LISTVIEW_SetCursor(LISTVIEW_INFO *infoPtr, HWND hwnd, UINT nHittest, UINT wMouseMsg)
8735 LVHITTESTINFO lvHitTestInfo;
8737 if(!(infoPtr->dwLvExStyle & LVS_EX_TRACKSELECT)) return FALSE;
8739 if(!infoPtr->hHotCursor) return FALSE;
8741 GetCursorPos(&lvHitTestInfo.pt);
8742 if (LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, FALSE, FALSE) < 0) return FALSE;
8744 SetCursor(infoPtr->hHotCursor);
8754 * [I] infoPtr : valid pointer to the listview structure
8755 * [I] hwndLoseFocus : handle of previously focused window
8760 static LRESULT LISTVIEW_SetFocus(LISTVIEW_INFO *infoPtr, HWND hwndLoseFocus)
8762 TRACE("(hwndLoseFocus=%p)\n", hwndLoseFocus);
8764 /* if we have the focus already, there's nothing to do */
8765 if (infoPtr->bFocus) return 0;
8767 /* send NM_SETFOCUS notification */
8768 if (!notify(infoPtr, NM_SETFOCUS)) return 0;
8770 /* set window focus flag */
8771 infoPtr->bFocus = TRUE;
8773 /* put the focus rect back on */
8774 LISTVIEW_ShowFocusRect(infoPtr, TRUE);
8776 /* redraw all visible selected items */
8777 LISTVIEW_InvalidateSelectedItems(infoPtr);
8787 * [I] infoPtr : valid pointer to the listview structure
8788 * [I] fRedraw : font handle
8789 * [I] fRedraw : redraw flag
8794 static LRESULT LISTVIEW_SetFont(LISTVIEW_INFO *infoPtr, HFONT hFont, WORD fRedraw)
8796 HFONT oldFont = infoPtr->hFont;
8798 TRACE("(hfont=%p,redraw=%hu)\n", hFont, fRedraw);
8800 infoPtr->hFont = hFont ? hFont : infoPtr->hDefaultFont;
8801 if (infoPtr->hFont == oldFont) return 0;
8803 LISTVIEW_SaveTextMetrics(infoPtr);
8805 if ((infoPtr->dwStyle & LVS_TYPEMASK) == LVS_REPORT)
8806 SendMessageW(infoPtr->hwndHeader, WM_SETFONT, (WPARAM)hFont, MAKELPARAM(fRedraw, 0));
8808 if (fRedraw) LISTVIEW_InvalidateList(infoPtr);
8815 * Message handling for WM_SETREDRAW.
8816 * For the Listview, it invalidates the entire window (the doc specifies otherwise)
8819 * [I] infoPtr : valid pointer to the listview structure
8820 * [I] bRedraw: state of redraw flag
8823 * DefWinProc return value
8825 static LRESULT LISTVIEW_SetRedraw(LISTVIEW_INFO *infoPtr, BOOL bRedraw)
8827 TRACE("infoPtr->bRedraw=%d, bRedraw=%d\n", infoPtr->bRedraw, bRedraw);
8829 /* we cannot use straight equality here because _any_ non-zero value is TRUE */
8830 if ((infoPtr->bRedraw && bRedraw) || (!infoPtr->bRedraw && !bRedraw)) return 0;
8832 infoPtr->bRedraw = bRedraw;
8834 if(!bRedraw) return 0;
8836 if (is_autoarrange(infoPtr))
8837 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
8838 LISTVIEW_UpdateScroll(infoPtr);
8840 /* despite what the WM_SETREDRAW docs says, apps expect us
8841 * to invalidate the listview here... stupid! */
8842 LISTVIEW_InvalidateList(infoPtr);
8849 * Resizes the listview control. This function processes WM_SIZE
8850 * messages. At this time, the width and height are not used.
8853 * [I] infoPtr : valid pointer to the listview structure
8854 * [I] Width : new width
8855 * [I] Height : new height
8860 static LRESULT LISTVIEW_Size(LISTVIEW_INFO *infoPtr, int Width, int Height)
8862 RECT rcOld = infoPtr->rcList;
8864 TRACE("(width=%d, height=%d)\n", Width, Height);
8866 LISTVIEW_UpdateSize(infoPtr);
8867 if (EqualRect(&rcOld, &infoPtr->rcList)) return 0;
8869 /* do not bother with display related stuff if we're not redrawing */
8870 if (!is_redrawing(infoPtr)) return 0;
8872 if (is_autoarrange(infoPtr))
8873 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
8875 LISTVIEW_UpdateScroll(infoPtr);
8877 /* refresh all only for lists whose height changed significantly */
8878 if ((infoPtr->dwStyle & LVS_TYPEMASK) == LVS_LIST &&
8879 (rcOld.bottom - rcOld.top) / infoPtr->nItemHeight !=
8880 (infoPtr->rcList.bottom - infoPtr->rcList.top) / infoPtr->nItemHeight)
8881 LISTVIEW_InvalidateList(infoPtr);
8888 * Sets the size information.
8891 * [I] infoPtr : valid pointer to the listview structure
8896 static void LISTVIEW_UpdateSize(LISTVIEW_INFO *infoPtr)
8898 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
8900 TRACE("uView=%d, rcList(old)=%s\n", uView, debugrect(&infoPtr->rcList));
8902 GetClientRect(infoPtr->hwndSelf, &infoPtr->rcList);
8904 if (uView == LVS_LIST)
8906 /* Apparently the "LIST" style is supposed to have the same
8907 * number of items in a column even if there is no scroll bar.
8908 * Since if a scroll bar already exists then the bottom is already
8909 * reduced, only reduce if the scroll bar does not currently exist.
8910 * The "2" is there to mimic the native control. I think it may be
8911 * related to either padding or edges. (GLA 7/2002)
8913 if (!(GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE) & WS_HSCROLL))
8914 infoPtr->rcList.bottom -= GetSystemMetrics(SM_CYHSCROLL);
8915 infoPtr->rcList.bottom = max (infoPtr->rcList.bottom - 2, 0);
8917 else if (uView == LVS_REPORT && !(infoPtr->dwStyle & LVS_NOCOLUMNHEADER))
8922 hl.prc = &infoPtr->rcList;
8924 Header_Layout(infoPtr->hwndHeader, &hl);
8926 SetWindowPos(wp.hwnd, wp.hwndInsertAfter, wp.x, wp.y, wp.cx, wp.cy, wp.flags);
8928 infoPtr->rcList.top = max(wp.cy, 0);
8931 TRACE(" rcList=%s\n", debugrect(&infoPtr->rcList));
8936 * Processes WM_STYLECHANGED messages.
8939 * [I] infoPtr : valid pointer to the listview structure
8940 * [I] wStyleType : window style type (normal or extended)
8941 * [I] lpss : window style information
8946 static INT LISTVIEW_StyleChanged(LISTVIEW_INFO *infoPtr, WPARAM wStyleType,
8947 const STYLESTRUCT *lpss)
8949 UINT uNewView = lpss->styleNew & LVS_TYPEMASK;
8950 UINT uOldView = lpss->styleOld & LVS_TYPEMASK;
8952 TRACE("(styletype=%x, styleOld=0x%08lx, styleNew=0x%08lx)\n",
8953 wStyleType, lpss->styleOld, lpss->styleNew);
8955 if (wStyleType != GWL_STYLE) return 0;
8957 /* FIXME: if LVS_NOSORTHEADER changed, update header */
8958 /* what if LVS_OWNERDATA changed? */
8959 /* or LVS_SINGLESEL */
8960 /* or LVS_SORT{AS,DES}CENDING */
8962 infoPtr->dwStyle = lpss->styleNew;
8964 if (((lpss->styleOld & WS_HSCROLL) != 0)&&
8965 ((lpss->styleNew & WS_HSCROLL) == 0))
8966 ShowScrollBar(infoPtr->hwndSelf, SB_HORZ, FALSE);
8968 if (((lpss->styleOld & WS_VSCROLL) != 0)&&
8969 ((lpss->styleNew & WS_VSCROLL) == 0))
8970 ShowScrollBar(infoPtr->hwndSelf, SB_VERT, FALSE);
8972 if (uNewView != uOldView)
8974 SIZE oldIconSize = infoPtr->iconSize;
8977 SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
8978 ShowWindow(infoPtr->hwndHeader, SW_HIDE);
8980 ShowScrollBar(infoPtr->hwndSelf, SB_BOTH, FALSE);
8981 SetRectEmpty(&infoPtr->rcFocus);
8983 himl = (uNewView == LVS_ICON ? infoPtr->himlNormal : infoPtr->himlSmall);
8984 set_icon_size(&infoPtr->iconSize, himl, uNewView != LVS_ICON);
8986 if (uNewView == LVS_ICON)
8988 if ((infoPtr->iconSize.cx != oldIconSize.cx) || (infoPtr->iconSize.cy != oldIconSize.cy))
8990 TRACE("icon old size=(%ld,%ld), new size=(%ld,%ld)\n",
8991 oldIconSize.cx, oldIconSize.cy, infoPtr->iconSize.cx, infoPtr->iconSize.cy);
8992 LISTVIEW_SetIconSpacing(infoPtr, 0, 0);
8995 else if (uNewView == LVS_REPORT)
9000 hl.prc = &infoPtr->rcList;
9002 Header_Layout(infoPtr->hwndHeader, &hl);
9003 SetWindowPos(infoPtr->hwndHeader, infoPtr->hwndSelf, wp.x, wp.y, wp.cx, wp.cy, wp.flags);
9006 LISTVIEW_UpdateItemSize(infoPtr);
9009 if (uNewView == LVS_REPORT)
9010 ShowWindow(infoPtr->hwndHeader, (lpss->styleNew & LVS_NOCOLUMNHEADER) ? SW_HIDE : SW_SHOWNORMAL);
9012 if ( (uNewView == LVS_ICON || uNewView == LVS_SMALLICON) &&
9013 (uNewView != uOldView || ((lpss->styleNew ^ lpss->styleOld) & LVS_ALIGNMASK)) )
9014 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
9016 /* update the size of the client area */
9017 LISTVIEW_UpdateSize(infoPtr);
9019 /* add scrollbars if needed */
9020 LISTVIEW_UpdateScroll(infoPtr);
9022 /* invalidate client area + erase background */
9023 LISTVIEW_InvalidateList(infoPtr);
9030 * Window procedure of the listview control.
9033 static LRESULT WINAPI
9034 LISTVIEW_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
9036 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongPtrW(hwnd, 0);
9038 TRACE("(uMsg=%x wParam=%x lParam=%lx)\n", uMsg, wParam, lParam);
9040 if (!infoPtr && (uMsg != WM_CREATE))
9041 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
9045 case LVM_APPROXIMATEVIEWRECT:
9046 return LISTVIEW_ApproximateViewRect(infoPtr, (INT)wParam,
9047 LOWORD(lParam), HIWORD(lParam));
9049 return LISTVIEW_Arrange(infoPtr, (INT)wParam);
9051 /* case LVM_CANCELEDITLABEL: */
9053 case LVM_CREATEDRAGIMAGE:
9054 return (LRESULT)LISTVIEW_CreateDragImage(infoPtr, (INT)wParam, (LPPOINT)lParam);
9056 case LVM_DELETEALLITEMS:
9057 return LISTVIEW_DeleteAllItems(infoPtr);
9059 case LVM_DELETECOLUMN:
9060 return LISTVIEW_DeleteColumn(infoPtr, (INT)wParam);
9062 case LVM_DELETEITEM:
9063 return LISTVIEW_DeleteItem(infoPtr, (INT)wParam);
9065 case LVM_EDITLABELW:
9066 return (LRESULT)LISTVIEW_EditLabelT(infoPtr, (INT)wParam, TRUE);
9068 case LVM_EDITLABELA:
9069 return (LRESULT)LISTVIEW_EditLabelT(infoPtr, (INT)wParam, FALSE);
9071 /* case LVM_ENABLEGROUPVIEW: */
9073 case LVM_ENSUREVISIBLE:
9074 return LISTVIEW_EnsureVisible(infoPtr, (INT)wParam, (BOOL)lParam);
9077 return LISTVIEW_FindItemW(infoPtr, (INT)wParam, (LPLVFINDINFOW)lParam);
9080 return LISTVIEW_FindItemA(infoPtr, (INT)wParam, (LPLVFINDINFOA)lParam);
9082 case LVM_GETBKCOLOR:
9083 return infoPtr->clrBk;
9085 /* case LVM_GETBKIMAGE: */
9087 case LVM_GETCALLBACKMASK:
9088 return infoPtr->uCallbackMask;
9090 case LVM_GETCOLUMNA:
9091 return LISTVIEW_GetColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam, FALSE);
9093 case LVM_GETCOLUMNW:
9094 return LISTVIEW_GetColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam, TRUE);
9096 case LVM_GETCOLUMNORDERARRAY:
9097 return LISTVIEW_GetColumnOrderArray(infoPtr, (INT)wParam, (LPINT)lParam);
9099 case LVM_GETCOLUMNWIDTH:
9100 return LISTVIEW_GetColumnWidth(infoPtr, (INT)wParam);
9102 case LVM_GETCOUNTPERPAGE:
9103 return LISTVIEW_GetCountPerPage(infoPtr);
9105 case LVM_GETEDITCONTROL:
9106 return (LRESULT)infoPtr->hwndEdit;
9108 case LVM_GETEXTENDEDLISTVIEWSTYLE:
9109 return infoPtr->dwLvExStyle;
9111 /* case LVM_GETGROUPINFO: */
9113 /* case LVM_GETGROUPMETRICS: */
9116 return (LRESULT)infoPtr->hwndHeader;
9118 case LVM_GETHOTCURSOR:
9119 return (LRESULT)infoPtr->hHotCursor;
9121 case LVM_GETHOTITEM:
9122 return infoPtr->nHotItem;
9124 case LVM_GETHOVERTIME:
9125 return infoPtr->dwHoverTime;
9127 case LVM_GETIMAGELIST:
9128 return (LRESULT)LISTVIEW_GetImageList(infoPtr, (INT)wParam);
9130 /* case LVM_GETINSERTMARK: */
9132 /* case LVM_GETINSERTMARKCOLOR: */
9134 /* case LVM_GETINSERTMARKRECT: */
9136 case LVM_GETISEARCHSTRINGA:
9137 case LVM_GETISEARCHSTRINGW:
9138 FIXME("LVM_GETISEARCHSTRING: unimplemented\n");
9142 return LISTVIEW_GetItemExtT(infoPtr, (LPLVITEMW)lParam, FALSE);
9145 return LISTVIEW_GetItemExtT(infoPtr, (LPLVITEMW)lParam, TRUE);
9147 case LVM_GETITEMCOUNT:
9148 return infoPtr->nItemCount;
9150 case LVM_GETITEMPOSITION:
9151 return LISTVIEW_GetItemPosition(infoPtr, (INT)wParam, (LPPOINT)lParam);
9153 case LVM_GETITEMRECT:
9154 return LISTVIEW_GetItemRect(infoPtr, (INT)wParam, (LPRECT)lParam);
9156 case LVM_GETITEMSPACING:
9157 return LISTVIEW_GetItemSpacing(infoPtr, (BOOL)wParam);
9159 case LVM_GETITEMSTATE:
9160 return LISTVIEW_GetItemState(infoPtr, (INT)wParam, (UINT)lParam);
9162 case LVM_GETITEMTEXTA:
9163 return LISTVIEW_GetItemTextT(infoPtr, (INT)wParam, (LPLVITEMW)lParam, FALSE);
9165 case LVM_GETITEMTEXTW:
9166 return LISTVIEW_GetItemTextT(infoPtr, (INT)wParam, (LPLVITEMW)lParam, TRUE);
9168 case LVM_GETNEXTITEM:
9169 return LISTVIEW_GetNextItem(infoPtr, (INT)wParam, LOWORD(lParam));
9171 case LVM_GETNUMBEROFWORKAREAS:
9172 FIXME("LVM_GETNUMBEROFWORKAREAS: unimplemented\n");
9176 if (!lParam) return FALSE;
9177 LISTVIEW_GetOrigin(infoPtr, (LPPOINT)lParam);
9180 /* case LVM_GETOUTLINECOLOR: */
9182 /* case LVM_GETSELECTEDCOLUMN: */
9184 case LVM_GETSELECTEDCOUNT:
9185 return LISTVIEW_GetSelectedCount(infoPtr);
9187 case LVM_GETSELECTIONMARK:
9188 return infoPtr->nSelectionMark;
9190 case LVM_GETSTRINGWIDTHA:
9191 return LISTVIEW_GetStringWidthT(infoPtr, (LPCWSTR)lParam, FALSE);
9193 case LVM_GETSTRINGWIDTHW:
9194 return LISTVIEW_GetStringWidthT(infoPtr, (LPCWSTR)lParam, TRUE);
9196 case LVM_GETSUBITEMRECT:
9197 return LISTVIEW_GetSubItemRect(infoPtr, (UINT)wParam, (LPRECT)lParam);
9199 case LVM_GETTEXTBKCOLOR:
9200 return infoPtr->clrTextBk;
9202 case LVM_GETTEXTCOLOR:
9203 return infoPtr->clrText;
9205 /* case LVM_GETTILEINFO: */
9207 /* case LVM_GETTILEVIEWINFO: */
9209 case LVM_GETTOOLTIPS:
9210 if( !infoPtr->hwndToolTip )
9211 infoPtr->hwndToolTip = COMCTL32_CreateToolTip( hwnd );
9212 return (LRESULT)infoPtr->hwndToolTip;
9214 case LVM_GETTOPINDEX:
9215 return LISTVIEW_GetTopIndex(infoPtr);
9217 /*case LVM_GETUNICODEFORMAT:
9218 FIXME("LVM_GETUNICODEFORMAT: unimplemented\n");
9221 /* case LVM_GETVIEW: */
9223 case LVM_GETVIEWRECT:
9224 return LISTVIEW_GetViewRect(infoPtr, (LPRECT)lParam);
9226 case LVM_GETWORKAREAS:
9227 FIXME("LVM_GETWORKAREAS: unimplemented\n");
9230 /* case LVM_HASGROUP: */
9233 return LISTVIEW_HitTest(infoPtr, (LPLVHITTESTINFO)lParam, FALSE, FALSE);
9235 case LVM_INSERTCOLUMNA:
9236 return LISTVIEW_InsertColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam, FALSE);
9238 case LVM_INSERTCOLUMNW:
9239 return LISTVIEW_InsertColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam, TRUE);
9241 /* case LVM_INSERTGROUP: */
9243 /* case LVM_INSERTGROUPSORTED: */
9245 case LVM_INSERTITEMA:
9246 return LISTVIEW_InsertItemT(infoPtr, (LPLVITEMW)lParam, FALSE);
9248 case LVM_INSERTITEMW:
9249 return LISTVIEW_InsertItemT(infoPtr, (LPLVITEMW)lParam, TRUE);
9251 /* case LVM_INSERTMARKHITTEST: */
9253 /* case LVM_ISGROUPVIEWENABLED: */
9255 /* case LVM_MAPIDTOINDEX: */
9257 /* case LVM_MAPINDEXTOID: */
9259 /* case LVM_MOVEGROUP: */
9261 /* case LVM_MOVEITEMTOGROUP: */
9263 case LVM_REDRAWITEMS:
9264 return LISTVIEW_RedrawItems(infoPtr, (INT)wParam, (INT)lParam);
9266 /* case LVM_REMOVEALLGROUPS: */
9268 /* case LVM_REMOVEGROUP: */
9271 return LISTVIEW_Scroll(infoPtr, (INT)wParam, (INT)lParam);
9273 case LVM_SETBKCOLOR:
9274 return LISTVIEW_SetBkColor(infoPtr, (COLORREF)lParam);
9276 /* case LVM_SETBKIMAGE: */
9278 case LVM_SETCALLBACKMASK:
9279 infoPtr->uCallbackMask = (UINT)wParam;
9282 case LVM_SETCOLUMNA:
9283 return LISTVIEW_SetColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam, FALSE);
9285 case LVM_SETCOLUMNW:
9286 return LISTVIEW_SetColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam, TRUE);
9288 case LVM_SETCOLUMNORDERARRAY:
9289 return LISTVIEW_SetColumnOrderArray(infoPtr, (INT)wParam, (LPINT)lParam);
9291 case LVM_SETCOLUMNWIDTH:
9292 return LISTVIEW_SetColumnWidth(infoPtr, (INT)wParam, (short)LOWORD(lParam));
9294 case LVM_SETEXTENDEDLISTVIEWSTYLE:
9295 return LISTVIEW_SetExtendedListViewStyle(infoPtr, (DWORD)wParam, (DWORD)lParam);
9297 /* case LVM_SETGROUPINFO: */
9299 /* case LVM_SETGROUPMETRICS: */
9301 case LVM_SETHOTCURSOR:
9302 return (LRESULT)LISTVIEW_SetHotCursor(infoPtr, (HCURSOR)lParam);
9304 case LVM_SETHOTITEM:
9305 return LISTVIEW_SetHotItem(infoPtr, (INT)wParam);
9307 case LVM_SETHOVERTIME:
9308 return LISTVIEW_SetHoverTime(infoPtr, (DWORD)wParam);
9310 case LVM_SETICONSPACING:
9311 return LISTVIEW_SetIconSpacing(infoPtr, (short)LOWORD(lParam), (short)HIWORD(lParam));
9313 case LVM_SETIMAGELIST:
9314 return (LRESULT)LISTVIEW_SetImageList(infoPtr, (INT)wParam, (HIMAGELIST)lParam);
9316 /* case LVM_SETINFOTIP: */
9318 /* case LVM_SETINSERTMARK: */
9320 /* case LVM_SETINSERTMARKCOLOR: */
9323 return LISTVIEW_SetItemT(infoPtr, (LPLVITEMW)lParam, FALSE);
9326 return LISTVIEW_SetItemT(infoPtr, (LPLVITEMW)lParam, TRUE);
9328 case LVM_SETITEMCOUNT:
9329 return LISTVIEW_SetItemCount(infoPtr, (INT)wParam, (DWORD)lParam);
9331 case LVM_SETITEMPOSITION:
9334 pt.x = (short)LOWORD(lParam);
9335 pt.y = (short)HIWORD(lParam);
9336 return LISTVIEW_SetItemPosition(infoPtr, (INT)wParam, pt);
9339 case LVM_SETITEMPOSITION32:
9340 if (lParam == 0) return FALSE;
9341 return LISTVIEW_SetItemPosition(infoPtr, (INT)wParam, *((POINT*)lParam));
9343 case LVM_SETITEMSTATE:
9344 return LISTVIEW_SetItemState(infoPtr, (INT)wParam, (LPLVITEMW)lParam);
9346 case LVM_SETITEMTEXTA:
9347 return LISTVIEW_SetItemTextT(infoPtr, (INT)wParam, (LPLVITEMW)lParam, FALSE);
9349 case LVM_SETITEMTEXTW:
9350 return LISTVIEW_SetItemTextT(infoPtr, (INT)wParam, (LPLVITEMW)lParam, TRUE);
9352 /* case LVM_SETOUTLINECOLOR: */
9354 /* case LVM_SETSELECTEDCOLUMN: */
9356 case LVM_SETSELECTIONMARK:
9357 return LISTVIEW_SetSelectionMark(infoPtr, (INT)lParam);
9359 case LVM_SETTEXTBKCOLOR:
9360 return LISTVIEW_SetTextBkColor(infoPtr, (COLORREF)lParam);
9362 case LVM_SETTEXTCOLOR:
9363 return LISTVIEW_SetTextColor(infoPtr, (COLORREF)lParam);
9365 /* case LVM_SETTILEINFO: */
9367 /* case LVM_SETTILEVIEWINFO: */
9369 /* case LVM_SETTILEWIDTH: */
9371 case LVM_SETTOOLTIPS:
9372 return (LRESULT)LISTVIEW_SetToolTips(infoPtr, (HWND)lParam);
9374 /* case LVM_SETUNICODEFORMAT: */
9376 /* case LVM_SETVIEW: */
9378 /* case LVM_SETWORKAREAS: */
9380 /* case LVM_SORTGROUPS: */
9383 return LISTVIEW_SortItems(infoPtr, (PFNLVCOMPARE)lParam, (LPARAM)wParam);
9385 /* LVM_SORTITEMSEX: */
9387 case LVM_SUBITEMHITTEST:
9388 return LISTVIEW_HitTest(infoPtr, (LPLVHITTESTINFO)lParam, TRUE, FALSE);
9391 return LISTVIEW_Update(infoPtr, (INT)wParam);
9394 return LISTVIEW_ProcessLetterKeys( infoPtr, wParam, lParam );
9397 return LISTVIEW_Command(infoPtr, wParam, lParam);
9400 return LISTVIEW_Create(hwnd, (LPCREATESTRUCTW)lParam);
9403 return LISTVIEW_Destroy(infoPtr);
9406 return LISTVIEW_Enable(infoPtr, (BOOL)wParam);
9409 return LISTVIEW_EraseBkgnd(infoPtr, (HDC)wParam);
9412 return DLGC_WANTCHARS | DLGC_WANTARROWS;
9415 return (LRESULT)infoPtr->hFont;
9418 return LISTVIEW_HScroll(infoPtr, (INT)LOWORD(wParam), 0, (HWND)lParam);
9421 return LISTVIEW_KeyDown(infoPtr, (INT)wParam, (LONG)lParam);
9424 return LISTVIEW_KillFocus(infoPtr);
9426 case WM_LBUTTONDBLCLK:
9427 return LISTVIEW_LButtonDblClk(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
9429 case WM_LBUTTONDOWN:
9430 return LISTVIEW_LButtonDown(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
9433 return LISTVIEW_LButtonUp(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
9436 return LISTVIEW_MouseMove (infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
9439 return LISTVIEW_MouseHover(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
9442 return LISTVIEW_NCDestroy(infoPtr);
9445 if (LISTVIEW_NCPaint(infoPtr, (HRGN)wParam))
9450 if (lParam && ((LPNMHDR)lParam)->hwndFrom == infoPtr->hwndHeader)
9451 return LISTVIEW_HeaderNotification(infoPtr, (LPNMHEADERW)lParam);
9454 case WM_NOTIFYFORMAT:
9455 return LISTVIEW_NotifyFormat(infoPtr, (HWND)wParam, (INT)lParam);
9457 case WM_PRINTCLIENT:
9458 return LISTVIEW_PrintClient(infoPtr, (HDC)wParam, (DWORD)lParam);
9461 return LISTVIEW_Paint(infoPtr, (HDC)wParam);
9463 case WM_RBUTTONDBLCLK:
9464 return LISTVIEW_RButtonDblClk(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
9466 case WM_RBUTTONDOWN:
9467 return LISTVIEW_RButtonDown(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
9470 return LISTVIEW_RButtonUp(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
9473 if(LISTVIEW_SetCursor(infoPtr, (HWND)wParam, LOWORD(lParam), HIWORD(lParam)))
9478 return LISTVIEW_SetFocus(infoPtr, (HWND)wParam);
9481 return LISTVIEW_SetFont(infoPtr, (HFONT)wParam, (WORD)lParam);
9484 return LISTVIEW_SetRedraw(infoPtr, (BOOL)wParam);
9487 return LISTVIEW_Size(infoPtr, (short)LOWORD(lParam), (short)HIWORD(lParam));
9489 case WM_STYLECHANGED:
9490 return LISTVIEW_StyleChanged(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
9492 case WM_SYSCOLORCHANGE:
9493 COMCTL32_RefreshSysColors();
9496 /* case WM_TIMER: */
9497 case WM_THEMECHANGED:
9498 return LISTVIEW_ThemeChanged(infoPtr);
9501 return LISTVIEW_VScroll(infoPtr, (INT)LOWORD(wParam), 0, (HWND)lParam);
9504 if (wParam & (MK_SHIFT | MK_CONTROL))
9505 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
9506 return LISTVIEW_MouseWheel(infoPtr, (short int)HIWORD(wParam));
9508 case WM_WINDOWPOSCHANGED:
9509 if (!(((WINDOWPOS *)lParam)->flags & SWP_NOSIZE))
9511 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
9512 SetWindowPos(infoPtr->hwndSelf, 0, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOACTIVATE |
9513 SWP_NOZORDER | SWP_NOMOVE | SWP_NOSIZE);
9515 if ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (uView == LVS_REPORT))
9517 MEASUREITEMSTRUCT mis;
9518 mis.CtlType = ODT_LISTVIEW;
9519 mis.CtlID = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
9523 mis.itemHeight= infoPtr->nItemHeight;
9524 SendMessageW(infoPtr->hwndNotify, WM_MEASUREITEM, mis.CtlID, (LPARAM)&mis);
9525 if (infoPtr->nItemHeight != max(mis.itemHeight, 1))
9526 infoPtr->nMeasureItemHeight = infoPtr->nItemHeight = max(mis.itemHeight, 1);
9529 LISTVIEW_UpdateSize(infoPtr);
9530 LISTVIEW_UpdateScroll(infoPtr);
9532 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
9534 /* case WM_WININICHANGE: */
9537 if ((uMsg >= WM_USER) && (uMsg < WM_APP))
9538 ERR("unknown msg %04x wp=%08x lp=%08lx\n", uMsg, wParam, lParam);
9541 /* call default window procedure */
9542 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
9549 * Registers the window class.
9557 void LISTVIEW_Register(void)
9561 ZeroMemory(&wndClass, sizeof(WNDCLASSW));
9562 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS;
9563 wndClass.lpfnWndProc = LISTVIEW_WindowProc;
9564 wndClass.cbClsExtra = 0;
9565 wndClass.cbWndExtra = sizeof(LISTVIEW_INFO *);
9566 wndClass.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
9567 wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
9568 wndClass.lpszClassName = WC_LISTVIEWW;
9569 RegisterClassW(&wndClass);
9574 * Unregisters the window class.
9582 void LISTVIEW_Unregister(void)
9584 UnregisterClassW(WC_LISTVIEWW, NULL);
9589 * Handle any WM_COMMAND messages
9592 * [I] infoPtr : valid pointer to the listview structure
9593 * [I] wParam : the first message parameter
9594 * [I] lParam : the second message parameter
9599 static LRESULT LISTVIEW_Command(LISTVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
9601 switch (HIWORD(wParam))
9606 * Adjust the edit window size
9609 HDC hdc = GetDC(infoPtr->hwndEdit);
9610 HFONT hFont, hOldFont = 0;
9615 if (!infoPtr->hwndEdit || !hdc) return 0;
9616 len = GetWindowTextW(infoPtr->hwndEdit, buffer, sizeof(buffer)/sizeof(buffer[0]));
9617 GetWindowRect(infoPtr->hwndEdit, &rect);
9619 /* Select font to get the right dimension of the string */
9620 hFont = (HFONT)SendMessageW(infoPtr->hwndEdit, WM_GETFONT, 0, 0);
9623 hOldFont = SelectObject(hdc, hFont);
9626 if (GetTextExtentPoint32W(hdc, buffer, lstrlenW(buffer), &sz))
9628 TEXTMETRICW textMetric;
9630 /* Add Extra spacing for the next character */
9631 GetTextMetricsW(hdc, &textMetric);
9632 sz.cx += (textMetric.tmMaxCharWidth * 2);
9640 rect.bottom - rect.top,
9641 SWP_DRAWFRAME|SWP_NOMOVE);
9644 SelectObject(hdc, hOldFont);
9646 ReleaseDC(infoPtr->hwndEdit, hdc);
9652 return SendMessageW (infoPtr->hwndNotify, WM_COMMAND, wParam, lParam);
9661 * Subclassed edit control windproc function
9664 * [I] hwnd : the edit window handle
9665 * [I] uMsg : the message that is to be processed
9666 * [I] wParam : first message parameter
9667 * [I] lParam : second message parameter
9668 * [I] isW : TRUE if input is Unicode
9673 static LRESULT EditLblWndProcT(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL isW)
9675 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongPtrW(GetParent(hwnd), 0);
9676 BOOL cancel = FALSE;
9678 TRACE("(hwnd=%p, uMsg=%x, wParam=%x, lParam=%lx, isW=%d)\n",
9679 hwnd, uMsg, wParam, lParam, isW);
9684 return DLGC_WANTARROWS | DLGC_WANTALLKEYS;
9691 WNDPROC editProc = infoPtr->EditWndProc;
9692 infoPtr->EditWndProc = 0;
9693 SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (DWORD_PTR)editProc);
9694 return CallWindowProcT(editProc, hwnd, uMsg, wParam, lParam, isW);
9698 if (VK_ESCAPE == (INT)wParam)
9703 else if (VK_RETURN == (INT)wParam)
9707 return CallWindowProcT(infoPtr->EditWndProc, hwnd, uMsg, wParam, lParam, isW);
9711 if (infoPtr->hwndEdit)
9713 LPWSTR buffer = NULL;
9715 infoPtr->hwndEdit = 0;
9718 DWORD len = isW ? GetWindowTextLengthW(hwnd) : GetWindowTextLengthA(hwnd);
9722 if ( (buffer = Alloc((len+1) * (isW ? sizeof(WCHAR) : sizeof(CHAR)))) )
9724 if (isW) GetWindowTextW(hwnd, buffer, len+1);
9725 else GetWindowTextA(hwnd, (CHAR*)buffer, len+1);
9729 LISTVIEW_EndEditLabelT(infoPtr, buffer, isW);
9731 if (buffer) Free(buffer);
9735 SendMessageW(hwnd, WM_CLOSE, 0, 0);
9741 * Subclassed edit control Unicode windproc function
9744 * [I] hwnd : the edit window handle
9745 * [I] uMsg : the message that is to be processed
9746 * [I] wParam : first message parameter
9747 * [I] lParam : second message parameter
9751 LRESULT CALLBACK EditLblWndProcW(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
9753 return EditLblWndProcT(hwnd, uMsg, wParam, lParam, TRUE);
9758 * Subclassed edit control ANSI windproc function
9761 * [I] hwnd : the edit window handle
9762 * [I] uMsg : the message that is to be processed
9763 * [I] wParam : first message parameter
9764 * [I] lParam : second message parameter
9768 LRESULT CALLBACK EditLblWndProcA(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
9770 return EditLblWndProcT(hwnd, uMsg, wParam, lParam, FALSE);
9775 * Creates a subclassed edit cotrol
9778 * [I] infoPtr : valid pointer to the listview structure
9779 * [I] text : initial text for the edit
9780 * [I] style : the window style
9781 * [I] isW : TRUE if input is Unicode
9785 static HWND CreateEditLabelT(LISTVIEW_INFO *infoPtr, LPCWSTR text, DWORD style,
9786 INT x, INT y, INT width, INT height, BOOL isW)
9788 WCHAR editName[5] = { 'E', 'd', 'i', 't', '\0' };
9793 TEXTMETRICW textMetric;
9794 HINSTANCE hinst = (HINSTANCE)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_HINSTANCE);
9796 TRACE("(text=%s, ..., isW=%d)\n", debugtext_t(text, isW), isW);
9798 style |= WS_CHILDWINDOW|WS_CLIPSIBLINGS|ES_LEFT|ES_AUTOHSCROLL|WS_BORDER;
9799 hdc = GetDC(infoPtr->hwndSelf);
9801 /* Select the font to get appropriate metric dimensions */
9802 if(infoPtr->hFont != 0)
9803 hOldFont = SelectObject(hdc, infoPtr->hFont);
9805 /*Get String Length in pixels */
9806 GetTextExtentPoint32W(hdc, text, lstrlenW(text), &sz);
9808 /*Add Extra spacing for the next character */
9809 GetTextMetricsW(hdc, &textMetric);
9810 sz.cx += (textMetric.tmMaxCharWidth * 2);
9812 if(infoPtr->hFont != 0)
9813 SelectObject(hdc, hOldFont);
9815 ReleaseDC(infoPtr->hwndSelf, hdc);
9817 hedit = CreateWindowW(editName, text, style, x, y, sz.cx, height, infoPtr->hwndSelf, 0, hinst, 0);
9819 hedit = CreateWindowA("Edit", (LPCSTR)text, style, x, y, sz.cx, height, infoPtr->hwndSelf, 0, hinst, 0);
9821 if (!hedit) return 0;
9823 infoPtr->EditWndProc = (WNDPROC)
9824 (isW ? SetWindowLongPtrW(hedit, GWLP_WNDPROC, (DWORD_PTR)EditLblWndProcW) :
9825 SetWindowLongPtrA(hedit, GWLP_WNDPROC, (DWORD_PTR)EditLblWndProcA) );
9827 SendMessageW(hedit, WM_SETFONT, (WPARAM)infoPtr->hFont, FALSE);