comctl32/listview: Move sending LVN_ODSTATECHANGED notifications to a function.
[wine.git] / dlls / comctl32 / listview.c
bloba12b19b808346a0cdb28bd47d9ad27b7a4762a8c
1 /*
2 * Listview control
4 * Copyright 1998, 1999 Eric Kohl
5 * Copyright 1999 Luc Tourangeau
6 * Copyright 2000 Jason Mawdsley
7 * Copyright 2001 CodeWeavers Inc.
8 * Copyright 2002 Dimitrie O. Paun
9 * Copyright 2009-2015 Nikolay Sivov
10 * Copyright 2009 Owen Rudge for CodeWeavers
11 * Copyright 2012-2013 Daniel Jelinski
13 * This library is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU Lesser General Public
15 * License as published by the Free Software Foundation; either
16 * version 2.1 of the License, or (at your option) any later version.
18 * This library is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * Lesser General Public License for more details.
23 * You should have received a copy of the GNU Lesser General Public
24 * License along with this library; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27 * TODO:
29 * Default Message Processing
30 * -- WM_CREATE: create the icon and small icon image lists at this point only if
31 * the LVS_SHAREIMAGELISTS style is not specified.
32 * -- WM_WINDOWPOSCHANGED: arrange the list items if the current view is icon
33 * or small icon and the LVS_AUTOARRANGE style is specified.
34 * -- WM_TIMER
35 * -- WM_WININICHANGE
37 * Features
38 * -- Hot item handling, mouse hovering
39 * -- Workareas support
40 * -- Tilemode support
41 * -- Groups support
43 * Bugs
44 * -- Expand large item in ICON mode when the cursor is flying over the icon or text.
45 * -- Support CustomDraw options for _WIN32_IE >= 0x560 (see NMLVCUSTOMDRAW docs).
46 * -- LVA_SNAPTOGRID not implemented
47 * -- LISTVIEW_ApproximateViewRect partially implemented
48 * -- LISTVIEW_StyleChanged doesn't handle some changes too well
50 * Speedups
51 * -- LISTVIEW_GetNextItem needs to be rewritten. It is currently
52 * linear in the number of items in the list, and this is
53 * unacceptable for large lists.
54 * -- if list is sorted by item text LISTVIEW_InsertItemT could use
55 * binary search to calculate item index (e.g. DPA_Search()).
56 * This requires sorted state to be reliably tracked in item modifiers.
57 * -- we should keep an ordered array of coordinates in iconic mode.
58 * This would allow framing items (iterator_frameditems),
59 * and finding the nearest item (LVFI_NEARESTXY) a lot more efficiently.
61 * Flags
62 * -- LVIF_COLUMNS
63 * -- LVIF_GROUPID
65 * States
66 * -- LVIS_ACTIVATING (not currently supported by comctl32.dll version 6.0)
67 * -- LVIS_DROPHILITED
69 * Styles
70 * -- LVS_NOLABELWRAP
71 * -- LVS_NOSCROLL (see Q137520)
72 * -- LVS_ALIGNTOP
74 * Extended Styles
75 * -- LVS_EX_BORDERSELECT
76 * -- LVS_EX_FLATSB
77 * -- LVS_EX_INFOTIP
78 * -- LVS_EX_LABELTIP
79 * -- LVS_EX_MULTIWORKAREAS
80 * -- LVS_EX_REGIONAL
81 * -- LVS_EX_SIMPLESELECT
82 * -- LVS_EX_TWOCLICKACTIVATE
83 * -- LVS_EX_UNDERLINECOLD
84 * -- LVS_EX_UNDERLINEHOT
86 * Notifications:
87 * -- LVN_BEGINSCROLL, LVN_ENDSCROLL
88 * -- LVN_GETINFOTIP
89 * -- LVN_HOTTRACK
90 * -- LVN_SETDISPINFO
92 * Messages:
93 * -- LVM_ENABLEGROUPVIEW
94 * -- LVM_GETBKIMAGE
95 * -- LVM_GETGROUPINFO, LVM_SETGROUPINFO
96 * -- LVM_GETGROUPMETRICS, LVM_SETGROUPMETRICS
97 * -- LVM_GETINSERTMARK, LVM_SETINSERTMARK
98 * -- LVM_GETINSERTMARKCOLOR, LVM_SETINSERTMARKCOLOR
99 * -- LVM_GETINSERTMARKRECT
100 * -- LVM_GETNUMBEROFWORKAREAS
101 * -- LVM_GETOUTLINECOLOR, LVM_SETOUTLINECOLOR
102 * -- LVM_GETISEARCHSTRINGW, LVM_GETISEARCHSTRINGA
103 * -- LVM_GETTILEINFO, LVM_SETTILEINFO
104 * -- LVM_GETTILEVIEWINFO, LVM_SETTILEVIEWINFO
105 * -- LVM_GETWORKAREAS, LVM_SETWORKAREAS
106 * -- LVM_HASGROUP, LVM_INSERTGROUP, LVM_REMOVEGROUP, LVM_REMOVEALLGROUPS
107 * -- LVM_INSERTGROUPSORTED
108 * -- LVM_INSERTMARKHITTEST
109 * -- LVM_ISGROUPVIEWENABLED
110 * -- LVM_MOVEGROUP
111 * -- LVM_MOVEITEMTOGROUP
112 * -- LVM_SETINFOTIP
113 * -- LVM_SETTILEWIDTH
114 * -- LVM_SORTGROUPS
116 * Macros:
117 * -- ListView_GetHoverTime, ListView_SetHoverTime
118 * -- ListView_GetISearchString
119 * -- ListView_GetNumberOfWorkAreas
120 * -- ListView_GetWorkAreas, ListView_SetWorkAreas
122 * Functions:
123 * -- LVGroupComparE
126 #include <assert.h>
127 #include <ctype.h>
128 #include <string.h>
129 #include <stdlib.h>
130 #include <stdarg.h>
131 #include <stdio.h>
133 #include "windef.h"
134 #include "winbase.h"
135 #include "winnt.h"
136 #include "wingdi.h"
137 #include "winuser.h"
138 #include "winnls.h"
139 #include "commctrl.h"
140 #include "comctl32.h"
141 #include "uxtheme.h"
142 #include "vsstyle.h"
143 #include "shlwapi.h"
145 #include "wine/debug.h"
147 WINE_DEFAULT_DEBUG_CHANNEL(listview);
149 typedef struct tagCOLUMN_INFO
151 RECT rcHeader; /* tracks the header's rectangle */
152 INT fmt; /* same as LVCOLUMN.fmt */
153 INT cxMin;
154 } COLUMN_INFO;
156 typedef struct tagITEMHDR
158 LPWSTR pszText;
159 INT iImage;
160 } ITEMHDR, *LPITEMHDR;
162 typedef struct tagSUBITEM_INFO
164 ITEMHDR hdr;
165 INT iSubItem;
166 } SUBITEM_INFO;
168 typedef struct tagITEM_ID ITEM_ID;
170 typedef struct tagITEM_INFO
172 ITEMHDR hdr;
173 UINT state;
174 LPARAM lParam;
175 INT iIndent;
176 ITEM_ID *id;
177 } ITEM_INFO;
179 struct tagITEM_ID
181 UINT id; /* item id */
182 HDPA item; /* link to item data */
185 typedef struct tagRANGE
187 INT lower;
188 INT upper;
189 } RANGE;
191 typedef struct tagRANGES
193 HDPA hdpa;
194 } *RANGES;
196 typedef struct tagITERATOR
198 INT nItem;
199 INT nSpecial;
200 RANGE range;
201 RANGES ranges;
202 INT index;
203 } ITERATOR;
205 typedef struct tagDELAYED_ITEM_EDIT
207 BOOL fEnabled;
208 INT iItem;
209 } DELAYED_ITEM_EDIT;
211 enum notification_mask
213 NOTIFY_MASK_ITEM_CHANGE = 0x1,
214 NOTIFY_MASK_END_LABEL_EDIT = 0x2,
215 NOTIFY_MASK_UNMASK_ALL = 0xffffffff
218 typedef struct tagLISTVIEW_INFO
220 /* control window */
221 HWND hwndSelf;
222 RECT rcList; /* This rectangle is really the window
223 * client rectangle possibly reduced by the
224 * horizontal scroll bar and/or header - see
225 * LISTVIEW_UpdateSize. This rectangle offset
226 * by the LISTVIEW_GetOrigin value is in
227 * client coordinates */
229 /* notification window */
230 SHORT notifyFormat;
231 HWND hwndNotify;
232 DWORD notify_mask;
233 UINT uCallbackMask;
235 /* tooltips */
236 HWND hwndToolTip;
238 /* items */
239 INT nItemCount; /* the number of items in the list */
240 HDPA hdpaItems; /* array ITEM_INFO pointers */
241 HDPA hdpaItemIds; /* array of ITEM_ID pointers */
242 HDPA hdpaPosX; /* maintains the (X, Y) coordinates of the */
243 HDPA hdpaPosY; /* items in LVS_ICON, and LVS_SMALLICON modes */
244 RANGES selectionRanges;
245 INT nSelectionMark; /* item to start next multiselection from */
246 INT nHotItem;
248 /* columns */
249 HDPA hdpaColumns; /* array of COLUMN_INFO pointers */
250 BOOL colRectsDirty; /* trigger column rectangles requery from header */
251 INT selected_column; /* index for LVM_SETSELECTEDCOLUMN/LVM_GETSELECTEDCOLUMN */
253 /* item metrics */
254 BOOL bNoItemMetrics; /* flags if item metrics are not yet computed */
255 INT nItemHeight;
256 INT nItemWidth;
258 /* style */
259 DWORD dwStyle; /* the cached window GWL_STYLE */
260 DWORD dwLvExStyle; /* extended listview style */
261 DWORD uView; /* current view available through LVM_[G,S]ETVIEW */
263 /* edit item */
264 HWND hwndEdit;
265 WNDPROC EditWndProc;
266 INT nEditLabelItem;
267 DELAYED_ITEM_EDIT itemEdit; /* Pointer to this structure will be the timer ID */
269 /* icons */
270 HIMAGELIST himlNormal;
271 HIMAGELIST himlSmall;
272 HIMAGELIST himlState;
273 SIZE iconSize;
274 BOOL autoSpacing;
275 SIZE iconSpacing;
276 SIZE iconStateSize;
277 POINT currIconPos; /* this is the position next icon will be placed */
279 /* header */
280 HWND hwndHeader;
281 INT xTrackLine; /* The x coefficient of the track line or -1 if none */
283 /* marquee selection */
284 BOOL bMarqueeSelect; /* marquee selection/highlight underway */
285 BOOL bScrolling;
286 RECT marqueeRect; /* absolute coordinates of marquee selection */
287 RECT marqueeDrawRect; /* relative coordinates for drawing marquee */
288 POINT marqueeOrigin; /* absolute coordinates of marquee click origin */
290 /* focus drawing */
291 BOOL bFocus; /* control has focus */
292 INT nFocusedItem;
293 RECT rcFocus; /* focus bounds */
295 /* colors */
296 HBRUSH hBkBrush;
297 COLORREF clrBk;
298 COLORREF clrText;
299 COLORREF clrTextBk;
300 HBITMAP hBkBitmap;
302 /* font */
303 HFONT hDefaultFont;
304 HFONT hFont;
305 INT ntmHeight; /* Some cached metrics of the font used */
306 INT ntmMaxCharWidth; /* by the listview to draw items */
307 INT nEllipsisWidth;
309 /* mouse operation */
310 BOOL bLButtonDown;
311 BOOL bDragging;
312 POINT ptClickPos; /* point where the user clicked */
313 INT nLButtonDownItem; /* tracks item to reset multiselection on WM_LBUTTONUP */
314 DWORD dwHoverTime;
315 HCURSOR hHotCursor;
316 INT cWheelRemainder;
318 /* keyboard operation */
319 DWORD lastKeyPressTimestamp;
320 WPARAM charCode;
321 INT nSearchParamLength;
322 WCHAR szSearchParam[ MAX_PATH ];
324 /* painting */
325 BOOL bIsDrawing; /* Drawing in progress */
326 INT nMeasureItemHeight; /* WM_MEASUREITEM result */
327 BOOL redraw; /* WM_SETREDRAW switch */
329 /* misc */
330 DWORD iVersion; /* CCM_[G,S]ETVERSION */
331 } LISTVIEW_INFO;
334 * constants
336 /* How many we debug buffer to allocate */
337 #define DEBUG_BUFFERS 20
338 /* The size of a single debug buffer */
339 #define DEBUG_BUFFER_SIZE 256
341 /* Internal interface to LISTVIEW_HScroll and LISTVIEW_VScroll */
342 #define SB_INTERNAL -1
344 /* maximum size of a label */
345 #define DISP_TEXT_SIZE 260
347 /* padding for items in list and small icon display modes */
348 #define WIDTH_PADDING 12
350 /* padding for items in list, report and small icon display modes */
351 #define HEIGHT_PADDING 1
353 /* offset of items in report display mode */
354 #define REPORT_MARGINX 2
356 /* padding for icon in large icon display mode
357 * ICON_TOP_PADDING_NOTHITABLE - space between top of box and area
358 * that HITTEST will see.
359 * ICON_TOP_PADDING_HITABLE - spacing between above and icon.
360 * ICON_TOP_PADDING - sum of the two above.
361 * ICON_BOTTOM_PADDING - between bottom of icon and top of text
362 * LABEL_HOR_PADDING - between text and sides of box
363 * LABEL_VERT_PADDING - between bottom of text and end of box
365 * ICON_LR_PADDING - additional width above icon size.
366 * ICON_LR_HALF - half of the above value
368 #define ICON_TOP_PADDING_NOTHITABLE 2
369 #define ICON_TOP_PADDING_HITABLE 2
370 #define ICON_TOP_PADDING (ICON_TOP_PADDING_NOTHITABLE + ICON_TOP_PADDING_HITABLE)
371 #define ICON_BOTTOM_PADDING 4
372 #define LABEL_HOR_PADDING 5
373 #define LABEL_VERT_PADDING 7
374 #define ICON_LR_PADDING 16
375 #define ICON_LR_HALF (ICON_LR_PADDING/2)
377 /* default label width for items in list and small icon display modes */
378 #define DEFAULT_LABEL_WIDTH 40
379 /* maximum select rectangle width for empty text item in LV_VIEW_DETAILS */
380 #define MAX_EMPTYTEXT_SELECT_WIDTH 80
382 /* default column width for items in list display mode */
383 #define DEFAULT_COLUMN_WIDTH 128
385 /* Size of "line" scroll for V & H scrolls */
386 #define LISTVIEW_SCROLL_ICON_LINE_SIZE 37
388 /* Padding between image and label */
389 #define IMAGE_PADDING 2
391 /* Padding behind the label */
392 #define TRAILING_LABEL_PADDING 12
393 #define TRAILING_HEADER_PADDING 11
395 /* Border for the icon caption */
396 #define CAPTION_BORDER 2
398 /* Standard DrawText flags */
399 #define LV_ML_DT_FLAGS (DT_TOP | DT_NOPREFIX | DT_EDITCONTROL | DT_CENTER | DT_WORDBREAK | DT_WORD_ELLIPSIS | DT_END_ELLIPSIS)
400 #define LV_FL_DT_FLAGS (DT_TOP | DT_NOPREFIX | DT_EDITCONTROL | DT_CENTER | DT_WORDBREAK | DT_NOCLIP)
401 #define LV_SL_DT_FLAGS (DT_VCENTER | DT_NOPREFIX | DT_EDITCONTROL | DT_SINGLELINE | DT_WORD_ELLIPSIS | DT_END_ELLIPSIS)
403 /* Image index from state */
404 #define STATEIMAGEINDEX(x) (((x) & LVIS_STATEIMAGEMASK) >> 12)
406 /* The time in milliseconds to reset the search in the list */
407 #define KEY_DELAY 450
409 /* Dump the LISTVIEW_INFO structure to the debug channel */
410 #define LISTVIEW_DUMP(iP) do { \
411 TRACE("hwndSelf=%p, clrBk=%#lx, clrText=%#lx, clrTextBk=%#lx, ItemHeight=%d, ItemWidth=%d, Style=%#lx\n", \
412 iP->hwndSelf, iP->clrBk, iP->clrText, iP->clrTextBk, \
413 iP->nItemHeight, iP->nItemWidth, iP->dwStyle); \
414 TRACE("hwndSelf=%p, himlNor=%p, himlSml=%p, himlState=%p, Focused=%d, Hot=%d, exStyle=%#lx, Focus=%d\n", \
415 iP->hwndSelf, iP->himlNormal, iP->himlSmall, iP->himlState, \
416 iP->nFocusedItem, iP->nHotItem, iP->dwLvExStyle, iP->bFocus ); \
417 TRACE("hwndSelf=%p, ntmH=%d, icSz.cx=%ld, icSz.cy=%ld, icSp.cx=%ld, icSp.cy=%ld, notifyFmt=%d\n", \
418 iP->hwndSelf, iP->ntmHeight, iP->iconSize.cx, iP->iconSize.cy, \
419 iP->iconSpacing.cx, iP->iconSpacing.cy, iP->notifyFormat); \
420 TRACE("hwndSelf=%p, rcList=%s\n", iP->hwndSelf, wine_dbgstr_rect(&iP->rcList)); \
421 } while(0)
423 static const WCHAR themeClass[] = L"ListView";
426 * forward declarations
428 static BOOL LISTVIEW_GetItemT(const LISTVIEW_INFO *, LPLVITEMW, BOOL);
429 static void LISTVIEW_GetItemBox(const LISTVIEW_INFO *, INT, LPRECT);
430 static void LISTVIEW_GetItemOrigin(const LISTVIEW_INFO *, INT, LPPOINT);
431 static BOOL LISTVIEW_GetItemPosition(const LISTVIEW_INFO *, INT, LPPOINT);
432 static BOOL LISTVIEW_GetItemRect(const LISTVIEW_INFO *, INT, LPRECT);
433 static void LISTVIEW_GetOrigin(const LISTVIEW_INFO *, LPPOINT);
434 static BOOL LISTVIEW_GetViewRect(const LISTVIEW_INFO *, LPRECT);
435 static void LISTVIEW_UpdateSize(LISTVIEW_INFO *);
436 static LRESULT LISTVIEW_Command(LISTVIEW_INFO *, WPARAM, LPARAM);
437 static INT LISTVIEW_GetStringWidthT(const LISTVIEW_INFO *, LPCWSTR, BOOL);
438 static BOOL LISTVIEW_KeySelection(LISTVIEW_INFO *, INT, BOOL);
439 static UINT LISTVIEW_GetItemState(const LISTVIEW_INFO *, INT, UINT);
440 static BOOL LISTVIEW_SetItemState(LISTVIEW_INFO *, INT, const LVITEMW *);
441 static VOID LISTVIEW_SetOwnerDataState(LISTVIEW_INFO *, INT, INT, const LVITEMW *);
442 static LRESULT LISTVIEW_VScroll(LISTVIEW_INFO *, INT, INT);
443 static LRESULT LISTVIEW_HScroll(LISTVIEW_INFO *, INT, INT);
444 static BOOL LISTVIEW_EnsureVisible(LISTVIEW_INFO *, INT, BOOL);
445 static HIMAGELIST LISTVIEW_SetImageList(LISTVIEW_INFO *, INT, HIMAGELIST);
446 static INT LISTVIEW_HitTest(const LISTVIEW_INFO *, LPLVHITTESTINFO, BOOL, BOOL);
447 static BOOL LISTVIEW_EndEditLabelT(LISTVIEW_INFO *, BOOL, BOOL);
448 static BOOL LISTVIEW_Scroll(LISTVIEW_INFO *, INT, INT);
450 /******** Text handling functions *************************************/
452 /* A text pointer is either NULL, LPSTR_TEXTCALLBACK, or points to a
453 * text string. The string may be ANSI or Unicode, in which case
454 * the boolean isW tells us the type of the string.
456 * The name of the function tell what type of strings it expects:
457 * W: Unicode, T: ANSI/Unicode - function of isW
460 static inline BOOL is_text(LPCWSTR text)
462 return text != NULL && text != LPSTR_TEXTCALLBACKW;
465 static inline int textlenT(LPCWSTR text, BOOL isW)
467 return !is_text(text) ? 0 :
468 isW ? lstrlenW(text) : lstrlenA((LPCSTR)text);
471 static inline void textcpynT(LPWSTR dest, BOOL isDestW, LPCWSTR src, BOOL isSrcW, INT max)
473 if (isDestW)
474 if (isSrcW) lstrcpynW(dest, src, max);
475 else MultiByteToWideChar(CP_ACP, 0, (LPCSTR)src, -1, dest, max);
476 else
477 if (isSrcW) WideCharToMultiByte(CP_ACP, 0, src, -1, (LPSTR)dest, max, NULL, NULL);
478 else lstrcpynA((LPSTR)dest, (LPCSTR)src, max);
481 static inline LPWSTR textdupTtoW(LPCWSTR text, BOOL isW)
483 LPWSTR wstr = (LPWSTR)text;
485 if (!isW && is_text(text))
487 INT len = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)text, -1, NULL, 0);
488 wstr = Alloc(len * sizeof(WCHAR));
489 if (wstr) MultiByteToWideChar(CP_ACP, 0, (LPCSTR)text, -1, wstr, len);
491 TRACE(" wstr=%s\n", text == LPSTR_TEXTCALLBACKW ? "(callback)" : debugstr_w(wstr));
492 return wstr;
495 static inline void textfreeT(LPWSTR wstr, BOOL isW)
497 if (!isW && is_text(wstr)) Free (wstr);
501 * dest is a pointer to a Unicode string
502 * src is a pointer to a string (Unicode if isW, ANSI if !isW)
504 static BOOL textsetptrT(LPWSTR *dest, LPCWSTR src, BOOL isW)
506 BOOL bResult = TRUE;
508 if (src == LPSTR_TEXTCALLBACKW)
510 if (is_text(*dest)) Free(*dest);
511 *dest = LPSTR_TEXTCALLBACKW;
513 else
515 LPWSTR pszText = textdupTtoW(src, isW);
516 if (*dest == LPSTR_TEXTCALLBACKW) *dest = NULL;
517 bResult = Str_SetPtrW(dest, pszText);
518 textfreeT(pszText, isW);
520 return bResult;
524 * compares a Unicode to a Unicode/ANSI text string
526 static inline int textcmpWT(LPCWSTR aw, LPCWSTR bt, BOOL isW)
528 if (!aw) return bt ? -1 : 0;
529 if (!bt) return 1;
530 if (aw == LPSTR_TEXTCALLBACKW)
531 return bt == LPSTR_TEXTCALLBACKW ? 1 : -1;
532 if (bt != LPSTR_TEXTCALLBACKW)
534 LPWSTR bw = textdupTtoW(bt, isW);
535 int r = bw ? lstrcmpW(aw, bw) : 1;
536 textfreeT(bw, isW);
537 return r;
540 return 1;
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)
567 if (!lprng) return "(null)";
568 return wine_dbg_sprintf("[%d, %d]", lprng->lower, lprng->upper);
571 static const char* debugscrollinfo(const SCROLLINFO *pScrollInfo)
573 char* buf = debug_getbuf(), *text = buf;
574 int len, size = DEBUG_BUFFER_SIZE;
576 if (pScrollInfo == NULL) return "(null)";
577 len = snprintf(buf, size, "{cbSize=%u, ", pScrollInfo->cbSize);
578 if (len == -1) goto end;
579 buf += len; size -= len;
580 if (pScrollInfo->fMask & SIF_RANGE)
581 len = snprintf(buf, size, "nMin=%d, nMax=%d, ", pScrollInfo->nMin, pScrollInfo->nMax);
582 else len = 0;
583 if (len == -1) goto end;
584 buf += len; size -= len;
585 if (pScrollInfo->fMask & SIF_PAGE)
586 len = snprintf(buf, size, "nPage=%u, ", pScrollInfo->nPage);
587 else len = 0;
588 if (len == -1) goto end;
589 buf += len; size -= len;
590 if (pScrollInfo->fMask & SIF_POS)
591 len = snprintf(buf, size, "nPos=%d, ", pScrollInfo->nPos);
592 else len = 0;
593 if (len == -1) goto end;
594 buf += len; size -= len;
595 if (pScrollInfo->fMask & SIF_TRACKPOS)
596 len = snprintf(buf, size, "nTrackPos=%d, ", pScrollInfo->nTrackPos);
597 else len = 0;
598 if (len == -1) goto end;
599 buf += len;
600 goto undo;
601 end:
602 buf = text + strlen(text);
603 undo:
604 if (buf - text > 2) { buf[-2] = '}'; buf[-1] = 0; }
605 return text;
608 static const char* debugnmlistview(const NMLISTVIEW *plvnm)
610 if (!plvnm) return "(null)";
611 return wine_dbg_sprintf("iItem=%d, iSubItem=%d, uNewState=0x%x,"
612 " uOldState=0x%x, uChanged=0x%x, ptAction=%s, lParam=%Id",
613 plvnm->iItem, plvnm->iSubItem, plvnm->uNewState, plvnm->uOldState,
614 plvnm->uChanged, wine_dbgstr_point(&plvnm->ptAction), plvnm->lParam);
617 static const char* debuglvitem_t(const LVITEMW *lpLVItem, BOOL isW)
619 char* buf = debug_getbuf(), *text = buf;
620 int len, size = DEBUG_BUFFER_SIZE;
622 if (lpLVItem == NULL) return "(null)";
623 len = snprintf(buf, size, "{iItem=%d, iSubItem=%d, ", lpLVItem->iItem, lpLVItem->iSubItem);
624 if (len == -1) goto end;
625 buf += len; size -= len;
626 if (lpLVItem->mask & LVIF_STATE)
627 len = snprintf(buf, size, "state=%x, stateMask=%x, ", lpLVItem->state, lpLVItem->stateMask);
628 else len = 0;
629 if (len == -1) goto end;
630 buf += len; size -= len;
631 if (lpLVItem->mask & LVIF_TEXT)
632 len = snprintf(buf, size, "pszText=%s, cchTextMax=%d, ", debugtext_tn(lpLVItem->pszText, isW, 80), lpLVItem->cchTextMax);
633 else len = 0;
634 if (len == -1) goto end;
635 buf += len; size -= len;
636 if (lpLVItem->mask & LVIF_IMAGE)
637 len = snprintf(buf, size, "iImage=%d, ", lpLVItem->iImage);
638 else len = 0;
639 if (len == -1) goto end;
640 buf += len; size -= len;
641 if (lpLVItem->mask & LVIF_PARAM)
642 len = snprintf(buf, size, "lParam=%Ix, ", lpLVItem->lParam);
643 else len = 0;
644 if (len == -1) goto end;
645 buf += len; size -= len;
646 if (lpLVItem->mask & LVIF_INDENT)
647 len = snprintf(buf, size, "iIndent=%d, ", lpLVItem->iIndent);
648 else len = 0;
649 if (len == -1) goto end;
650 buf += len;
651 goto undo;
652 end:
653 buf = text + strlen(text);
654 undo:
655 if (buf - text > 2) { buf[-2] = '}'; buf[-1] = 0; }
656 return text;
659 static const char* debuglvcolumn_t(const LVCOLUMNW *lpColumn, BOOL isW)
661 char* buf = debug_getbuf(), *text = buf;
662 int len, size = DEBUG_BUFFER_SIZE;
664 if (lpColumn == NULL) return "(null)";
665 len = snprintf(buf, size, "{");
666 if (len == -1) goto end;
667 buf += len; size -= len;
668 if (lpColumn->mask & LVCF_SUBITEM)
669 len = snprintf(buf, size, "iSubItem=%d, ", lpColumn->iSubItem);
670 else len = 0;
671 if (len == -1) goto end;
672 buf += len; size -= len;
673 if (lpColumn->mask & LVCF_FMT)
674 len = snprintf(buf, size, "fmt=%x, ", lpColumn->fmt);
675 else len = 0;
676 if (len == -1) goto end;
677 buf += len; size -= len;
678 if (lpColumn->mask & LVCF_WIDTH)
679 len = snprintf(buf, size, "cx=%d, ", lpColumn->cx);
680 else len = 0;
681 if (len == -1) goto end;
682 buf += len; size -= len;
683 if (lpColumn->mask & LVCF_TEXT)
684 len = snprintf(buf, size, "pszText=%s, cchTextMax=%d, ", debugtext_tn(lpColumn->pszText, isW, 80), lpColumn->cchTextMax);
685 else len = 0;
686 if (len == -1) goto end;
687 buf += len; size -= len;
688 if (lpColumn->mask & LVCF_IMAGE)
689 len = snprintf(buf, size, "iImage=%d, ", lpColumn->iImage);
690 else len = 0;
691 if (len == -1) goto end;
692 buf += len; size -= len;
693 if (lpColumn->mask & LVCF_ORDER)
694 len = snprintf(buf, size, "iOrder=%d, ", lpColumn->iOrder);
695 else len = 0;
696 if (len == -1) goto end;
697 buf += len;
698 goto undo;
699 end:
700 buf = text + strlen(text);
701 undo:
702 if (buf - text > 2) { buf[-2] = '}'; buf[-1] = 0; }
703 return text;
706 static const char* debuglvhittestinfo(const LVHITTESTINFO *lpht)
708 if (!lpht) return "(null)";
710 return wine_dbg_sprintf("{pt=%s, flags=0x%x, iItem=%d, iSubItem=%d}",
711 wine_dbgstr_point(&lpht->pt), lpht->flags, lpht->iItem, lpht->iSubItem);
714 /* Return the corresponding text for a given scroll value */
715 static inline LPCSTR debugscrollcode(int nScrollCode)
717 switch(nScrollCode)
719 case SB_LINELEFT: return "SB_LINELEFT";
720 case SB_LINERIGHT: return "SB_LINERIGHT";
721 case SB_PAGELEFT: return "SB_PAGELEFT";
722 case SB_PAGERIGHT: return "SB_PAGERIGHT";
723 case SB_THUMBPOSITION: return "SB_THUMBPOSITION";
724 case SB_THUMBTRACK: return "SB_THUMBTRACK";
725 case SB_ENDSCROLL: return "SB_ENDSCROLL";
726 case SB_INTERNAL: return "SB_INTERNAL";
727 default: return "unknown";
732 /******** Notification functions ************************************/
734 static int get_ansi_notification(UINT unicodeNotificationCode)
736 switch (unicodeNotificationCode)
738 case LVN_BEGINLABELEDITA:
739 case LVN_BEGINLABELEDITW: return LVN_BEGINLABELEDITA;
740 case LVN_ENDLABELEDITA:
741 case LVN_ENDLABELEDITW: return LVN_ENDLABELEDITA;
742 case LVN_GETDISPINFOA:
743 case LVN_GETDISPINFOW: return LVN_GETDISPINFOA;
744 case LVN_SETDISPINFOA:
745 case LVN_SETDISPINFOW: return LVN_SETDISPINFOA;
746 case LVN_ODFINDITEMA:
747 case LVN_ODFINDITEMW: return LVN_ODFINDITEMA;
748 case LVN_GETINFOTIPA:
749 case LVN_GETINFOTIPW: return LVN_GETINFOTIPA;
750 /* header forwards */
751 case HDN_TRACKA:
752 case HDN_TRACKW: return HDN_TRACKA;
753 case HDN_ENDTRACKA:
754 case HDN_ENDTRACKW: return HDN_ENDTRACKA;
755 case HDN_BEGINDRAG: return HDN_BEGINDRAG;
756 case HDN_ENDDRAG: return HDN_ENDDRAG;
757 case HDN_ITEMCHANGINGA:
758 case HDN_ITEMCHANGINGW: return HDN_ITEMCHANGINGA;
759 case HDN_ITEMCHANGEDA:
760 case HDN_ITEMCHANGEDW: return HDN_ITEMCHANGEDA;
761 case HDN_ITEMCLICKA:
762 case HDN_ITEMCLICKW: return HDN_ITEMCLICKA;
763 case HDN_DIVIDERDBLCLICKA:
764 case HDN_DIVIDERDBLCLICKW: return HDN_DIVIDERDBLCLICKA;
765 default: break;
767 FIXME("unknown notification %x\n", unicodeNotificationCode);
768 return unicodeNotificationCode;
771 /* forwards header notifications to listview parent */
772 static LRESULT notify_forward_header(const LISTVIEW_INFO *infoPtr, NMHEADERW *lpnmhW)
774 LPCWSTR text = NULL, filter = NULL;
775 LRESULT ret;
776 NMHEADERA *lpnmh = (NMHEADERA*) lpnmhW;
778 /* on unicode format exit earlier */
779 if (infoPtr->notifyFormat == NFR_UNICODE)
780 return SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, lpnmh->hdr.idFrom,
781 (LPARAM)lpnmh);
783 /* header always supplies unicode notifications,
784 all we have to do is to convert strings to ANSI */
785 if (lpnmh->pitem)
787 /* convert item text */
788 if (lpnmh->pitem->mask & HDI_TEXT)
790 text = (LPCWSTR)lpnmh->pitem->pszText;
791 lpnmh->pitem->pszText = NULL;
792 Str_SetPtrWtoA(&lpnmh->pitem->pszText, text);
794 /* convert filter text */
795 if ((lpnmh->pitem->mask & HDI_FILTER) && (lpnmh->pitem->type == HDFT_ISSTRING) &&
796 lpnmh->pitem->pvFilter)
798 filter = (LPCWSTR)((HD_TEXTFILTERA*)lpnmh->pitem->pvFilter)->pszText;
799 ((HD_TEXTFILTERA*)lpnmh->pitem->pvFilter)->pszText = NULL;
800 Str_SetPtrWtoA(&((HD_TEXTFILTERA*)lpnmh->pitem->pvFilter)->pszText, filter);
803 lpnmh->hdr.code = get_ansi_notification(lpnmh->hdr.code);
805 ret = SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, lpnmh->hdr.idFrom,
806 (LPARAM)lpnmh);
808 /* cleanup */
809 if(text)
811 Free(lpnmh->pitem->pszText);
812 lpnmh->pitem->pszText = (LPSTR)text;
814 if(filter)
816 Free(((HD_TEXTFILTERA*)lpnmh->pitem->pvFilter)->pszText);
817 ((HD_TEXTFILTERA*)lpnmh->pitem->pvFilter)->pszText = (LPSTR)filter;
820 return ret;
823 static LRESULT notify_hdr(const LISTVIEW_INFO *infoPtr, INT code, LPNMHDR pnmh)
825 LRESULT result;
827 TRACE("(code=%d)\n", code);
829 pnmh->hwndFrom = infoPtr->hwndSelf;
830 pnmh->idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
831 pnmh->code = code;
832 result = SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, pnmh->idFrom, (LPARAM)pnmh);
834 TRACE(" <= %Id\n", result);
836 return result;
839 static inline BOOL notify(const LISTVIEW_INFO *infoPtr, INT code)
841 NMHDR nmh;
842 HWND hwnd = infoPtr->hwndSelf;
843 notify_hdr(infoPtr, code, &nmh);
844 return IsWindow(hwnd);
847 static inline void notify_itemactivate(const LISTVIEW_INFO *infoPtr, const LVHITTESTINFO *htInfo)
849 NMITEMACTIVATE nmia;
850 LVITEMW item;
852 nmia.uNewState = 0;
853 nmia.uOldState = 0;
854 nmia.uChanged = 0;
855 nmia.uKeyFlags = 0;
857 item.mask = LVIF_PARAM|LVIF_STATE;
858 item.iItem = htInfo->iItem;
859 item.iSubItem = 0;
860 item.stateMask = (UINT)-1;
861 if (LISTVIEW_GetItemT(infoPtr, &item, TRUE)) {
862 nmia.lParam = item.lParam;
863 nmia.uOldState = item.state;
864 nmia.uNewState = item.state | LVIS_ACTIVATING;
865 nmia.uChanged = LVIF_STATE;
868 nmia.iItem = htInfo->iItem;
869 nmia.iSubItem = htInfo->iSubItem;
870 nmia.ptAction = htInfo->pt;
872 if (GetKeyState(VK_SHIFT) & 0x8000) nmia.uKeyFlags |= LVKF_SHIFT;
873 if (GetKeyState(VK_CONTROL) & 0x8000) nmia.uKeyFlags |= LVKF_CONTROL;
874 if (GetKeyState(VK_MENU) & 0x8000) nmia.uKeyFlags |= LVKF_ALT;
876 notify_hdr(infoPtr, LVN_ITEMACTIVATE, (LPNMHDR)&nmia);
879 static inline LRESULT notify_listview(const LISTVIEW_INFO *infoPtr, INT code, LPNMLISTVIEW plvnm)
881 TRACE("(code=%d, plvnm=%s)\n", code, debugnmlistview(plvnm));
882 return notify_hdr(infoPtr, code, (LPNMHDR)plvnm);
885 /* Handles NM_DBLCLK, NM_CLICK, NM_RDBLCLK, NM_RCLICK. Only NM_RCLICK return value is used. */
886 static BOOL notify_click(const LISTVIEW_INFO *infoPtr, INT code, const LVHITTESTINFO *lvht)
888 NMITEMACTIVATE nmia;
889 LVITEMW item;
890 HWND hwnd = infoPtr->hwndSelf;
891 LRESULT ret;
893 TRACE("code=%d, lvht=%s\n", code, debuglvhittestinfo(lvht));
894 ZeroMemory(&nmia, sizeof(nmia));
895 nmia.iItem = lvht->iItem;
896 nmia.iSubItem = lvht->iSubItem;
897 nmia.ptAction = lvht->pt;
898 item.mask = LVIF_PARAM;
899 item.iItem = lvht->iItem;
900 item.iSubItem = 0;
901 if (LISTVIEW_GetItemT(infoPtr, &item, TRUE)) nmia.lParam = item.lParam;
902 ret = notify_hdr(infoPtr, code, (NMHDR*)&nmia);
903 return IsWindow(hwnd) && (code == NM_RCLICK ? !ret : TRUE);
906 static BOOL notify_deleteitem(const LISTVIEW_INFO *infoPtr, INT nItem)
908 NMLISTVIEW nmlv;
909 LVITEMW item;
910 HWND hwnd = infoPtr->hwndSelf;
912 ZeroMemory(&nmlv, sizeof (NMLISTVIEW));
913 nmlv.iItem = nItem;
914 item.mask = LVIF_PARAM;
915 item.iItem = nItem;
916 item.iSubItem = 0;
917 if (LISTVIEW_GetItemT(infoPtr, &item, TRUE)) nmlv.lParam = item.lParam;
918 notify_listview(infoPtr, LVN_DELETEITEM, &nmlv);
919 return IsWindow(hwnd);
923 Send notification. depends on dispinfoW having same
924 structure as dispinfoA.
925 infoPtr : listview struct
926 code : *Unicode* notification code
927 pdi : dispinfo structure (can be unicode or ansi)
928 isW : TRUE if dispinfo is Unicode
930 static BOOL notify_dispinfoT(const LISTVIEW_INFO *infoPtr, UINT code, LPNMLVDISPINFOW pdi, BOOL isW)
932 INT length = 0, ret_length;
933 LPWSTR buffer = NULL, ret_text;
934 BOOL return_ansi = FALSE;
935 BOOL return_unicode = FALSE;
936 BOOL ret;
938 if ((pdi->item.mask & LVIF_TEXT) && is_text(pdi->item.pszText))
940 return_unicode = ( isW && infoPtr->notifyFormat == NFR_ANSI);
941 return_ansi = (!isW && infoPtr->notifyFormat == NFR_UNICODE);
944 ret_length = pdi->item.cchTextMax;
945 ret_text = pdi->item.pszText;
947 if (return_unicode || return_ansi)
949 if (code != LVN_GETDISPINFOW)
951 length = return_ansi ?
952 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pdi->item.pszText, -1, NULL, 0):
953 WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, NULL, 0, NULL, NULL);
955 else
957 length = pdi->item.cchTextMax;
958 *pdi->item.pszText = 0; /* make sure we don't process garbage */
961 buffer = Alloc( (return_ansi ? sizeof(WCHAR) : sizeof(CHAR)) * length);
962 if (!buffer) return FALSE;
964 if (return_ansi)
965 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pdi->item.pszText, -1,
966 buffer, length);
967 else
968 WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, (LPSTR) buffer,
969 length, NULL, NULL);
971 pdi->item.pszText = buffer;
972 pdi->item.cchTextMax = length;
975 if (infoPtr->notifyFormat == NFR_ANSI)
976 code = get_ansi_notification(code);
978 TRACE(" pdi->item=%s\n", debuglvitem_t(&pdi->item, infoPtr->notifyFormat != NFR_ANSI));
979 ret = notify_hdr(infoPtr, code, &pdi->hdr);
980 TRACE(" resulting code=%d\n", pdi->hdr.code);
982 if (return_ansi || return_unicode)
984 if (return_ansi && (pdi->hdr.code == LVN_GETDISPINFOA))
986 strcpy((char*)ret_text, (char*)pdi->item.pszText);
988 else if (return_unicode && (pdi->hdr.code == LVN_GETDISPINFOW))
990 lstrcpyW(ret_text, pdi->item.pszText);
992 else if (return_ansi) /* note : pointer can be changed by app ! */
994 WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, (LPSTR) ret_text,
995 ret_length, NULL, NULL);
997 else
998 MultiByteToWideChar(CP_ACP, 0, (LPSTR) pdi->item.pszText, -1,
999 ret_text, ret_length);
1001 pdi->item.pszText = ret_text; /* restores our buffer */
1002 pdi->item.cchTextMax = ret_length;
1004 Free(buffer);
1005 return ret;
1008 /* if dispinfo holder changed notification code then convert */
1009 if (!isW && (pdi->hdr.code == LVN_GETDISPINFOW) && (pdi->item.mask & LVIF_TEXT))
1011 length = WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, NULL, 0, NULL, NULL);
1013 buffer = Alloc(length * sizeof(CHAR));
1014 if (!buffer) return FALSE;
1016 WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, (LPSTR) buffer,
1017 ret_length, NULL, NULL);
1019 strcpy((LPSTR)pdi->item.pszText, (LPSTR)buffer);
1020 Free(buffer);
1023 return ret;
1026 static void customdraw_fill(NMLVCUSTOMDRAW *lpnmlvcd, const LISTVIEW_INFO *infoPtr, HDC hdc,
1027 const RECT *rcBounds, const LVITEMW *lplvItem)
1029 ZeroMemory(lpnmlvcd, sizeof(NMLVCUSTOMDRAW));
1030 lpnmlvcd->nmcd.hdc = hdc;
1031 lpnmlvcd->nmcd.rc = *rcBounds;
1032 lpnmlvcd->clrTextBk = infoPtr->clrTextBk;
1033 lpnmlvcd->clrText = infoPtr->clrText;
1034 if (!lplvItem) return;
1035 lpnmlvcd->nmcd.dwItemSpec = lplvItem->iItem + 1;
1036 lpnmlvcd->iSubItem = lplvItem->iSubItem;
1037 if (lplvItem->state & LVIS_SELECTED) lpnmlvcd->nmcd.uItemState |= CDIS_SELECTED;
1038 if (lplvItem->state & LVIS_FOCUSED) lpnmlvcd->nmcd.uItemState |= CDIS_FOCUS;
1039 if (lplvItem->iItem == infoPtr->nHotItem) lpnmlvcd->nmcd.uItemState |= CDIS_HOT;
1040 lpnmlvcd->nmcd.lItemlParam = lplvItem->lParam;
1043 static inline DWORD notify_customdraw (const LISTVIEW_INFO *infoPtr, DWORD dwDrawStage, NMLVCUSTOMDRAW *lpnmlvcd)
1045 BOOL isForItem = (lpnmlvcd->nmcd.dwItemSpec != 0);
1046 DWORD result;
1048 lpnmlvcd->nmcd.dwDrawStage = dwDrawStage;
1049 if (isForItem) lpnmlvcd->nmcd.dwDrawStage |= CDDS_ITEM;
1050 if (lpnmlvcd->iSubItem) lpnmlvcd->nmcd.dwDrawStage |= CDDS_SUBITEM;
1051 if (isForItem) lpnmlvcd->nmcd.dwItemSpec--;
1052 result = notify_hdr(infoPtr, NM_CUSTOMDRAW, &lpnmlvcd->nmcd.hdr);
1053 if (isForItem) lpnmlvcd->nmcd.dwItemSpec++;
1054 return result;
1057 static void prepaint_setup (const LISTVIEW_INFO *infoPtr, HDC hdc, const NMLVCUSTOMDRAW *cd, BOOL SubItem)
1059 COLORREF backcolor, textcolor;
1061 backcolor = cd->clrTextBk;
1062 textcolor = cd->clrText;
1064 /* apparently, for selected items, we have to override the returned values */
1065 if (!SubItem)
1067 if (cd->nmcd.uItemState & CDIS_SELECTED)
1069 if (infoPtr->bFocus)
1071 backcolor = comctl32_color.clrHighlight;
1072 textcolor = comctl32_color.clrHighlightText;
1074 else if (infoPtr->dwStyle & LVS_SHOWSELALWAYS)
1076 backcolor = comctl32_color.clr3dFace;
1077 textcolor = comctl32_color.clrBtnText;
1082 if (backcolor == CLR_DEFAULT)
1083 backcolor = comctl32_color.clrWindow;
1084 if (textcolor == CLR_DEFAULT)
1085 textcolor = comctl32_color.clrWindowText;
1087 /* Set the text attributes */
1088 if (backcolor != CLR_NONE)
1090 SetBkMode(hdc, OPAQUE);
1091 SetBkColor(hdc, backcolor);
1093 else
1094 SetBkMode(hdc, TRANSPARENT);
1095 SetTextColor(hdc, textcolor);
1098 static inline DWORD notify_postpaint (const LISTVIEW_INFO *infoPtr, NMLVCUSTOMDRAW *lpnmlvcd)
1100 return notify_customdraw(infoPtr, CDDS_POSTPAINT, lpnmlvcd);
1103 /* returns TRUE when repaint needed, FALSE otherwise */
1104 static BOOL notify_measureitem(LISTVIEW_INFO *infoPtr)
1106 MEASUREITEMSTRUCT mis;
1107 mis.CtlType = ODT_LISTVIEW;
1108 mis.CtlID = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1109 mis.itemID = -1;
1110 mis.itemWidth = 0;
1111 mis.itemData = 0;
1112 mis.itemHeight= infoPtr->nItemHeight;
1113 SendMessageW(infoPtr->hwndNotify, WM_MEASUREITEM, mis.CtlID, (LPARAM)&mis);
1114 if (infoPtr->nItemHeight != max(mis.itemHeight, 1))
1116 infoPtr->nMeasureItemHeight = infoPtr->nItemHeight = max(mis.itemHeight, 1);
1117 return TRUE;
1119 return FALSE;
1122 /******** Item iterator functions **********************************/
1124 static RANGES ranges_create(int count);
1125 static void ranges_destroy(RANGES ranges);
1126 static BOOL ranges_add(RANGES ranges, RANGE range);
1127 static BOOL ranges_del(RANGES ranges, RANGE range);
1128 static void ranges_dump(RANGES ranges);
1130 static inline BOOL ranges_additem(RANGES ranges, INT nItem)
1132 RANGE range = { nItem, nItem + 1 };
1134 return ranges_add(ranges, range);
1137 static inline BOOL ranges_delitem(RANGES ranges, INT nItem)
1139 RANGE range = { nItem, nItem + 1 };
1141 return ranges_del(ranges, range);
1144 /***
1145 * ITERATOR DOCUMENTATION
1147 * The iterator functions allow for easy, and convenient iteration
1148 * over items of interest in the list. Typically, you create an
1149 * iterator, use it, and destroy it, as such:
1150 * ITERATOR i;
1152 * iterator_xxxitems(&i, ...);
1153 * while (iterator_{prev,next}(&i)
1155 * //code which uses i.nItem
1157 * iterator_destroy(&i);
1159 * where xxx is either: framed, or visible.
1160 * Note that it is important that the code destroys the iterator
1161 * after it's done with it, as the creation of the iterator may
1162 * allocate memory, which thus needs to be freed.
1164 * You can iterate both forwards, and backwards through the list,
1165 * by using iterator_next or iterator_prev respectively.
1167 * Lower numbered items are draw on top of higher number items in
1168 * LVS_ICON, and LVS_SMALLICON (which are the only modes where
1169 * items may overlap). So, to test items, you should use
1170 * iterator_next
1171 * which lists the items top to bottom (in Z-order).
1172 * For drawing items, you should use
1173 * iterator_prev
1174 * which lists the items bottom to top (in Z-order).
1175 * If you keep iterating over the items after the end-of-items
1176 * marker (-1) is returned, the iterator will start from the
1177 * beginning. Typically, you don't need to test for -1,
1178 * because iterator_{next,prev} will return TRUE if more items
1179 * are to be iterated over, or FALSE otherwise.
1181 * Note: the iterator is defined to be bidirectional. That is,
1182 * any number of prev followed by any number of next, or
1183 * five versa, should leave the iterator at the same item:
1184 * prev * n, next * n = next * n, prev * n
1186 * The iterator has a notion of an out-of-order, special item,
1187 * which sits at the start of the list. This is used in
1188 * LVS_ICON, and LVS_SMALLICON mode to handle the focused item,
1189 * which needs to be first, as it may overlap other items.
1191 * The code is a bit messy because we have:
1192 * - a special item to deal with
1193 * - simple range, or composite range
1194 * - empty range.
1195 * If you find bugs, or want to add features, please make sure you
1196 * always check/modify *both* iterator_prev, and iterator_next.
1199 /****
1200 * This function iterates through the items in increasing order,
1201 * but prefixed by the special item, then -1. That is:
1202 * special, 1, 2, 3, ..., n, -1.
1203 * Each item is listed only once.
1205 static inline BOOL iterator_next(ITERATOR* i)
1207 if (i->nItem == -1)
1209 i->nItem = i->nSpecial;
1210 if (i->nItem != -1) return TRUE;
1212 if (i->nItem == i->nSpecial)
1214 if (i->ranges) i->index = 0;
1215 goto pickarange;
1218 i->nItem++;
1219 testitem:
1220 if (i->nItem == i->nSpecial) i->nItem++;
1221 if (i->nItem < i->range.upper) return TRUE;
1223 pickarange:
1224 if (i->ranges)
1226 if (i->index < DPA_GetPtrCount(i->ranges->hdpa))
1227 i->range = *(RANGE*)DPA_GetPtr(i->ranges->hdpa, i->index++);
1228 else goto end;
1230 else if (i->nItem >= i->range.upper) goto end;
1232 i->nItem = i->range.lower;
1233 if (i->nItem >= 0) goto testitem;
1234 end:
1235 i->nItem = -1;
1236 return FALSE;
1239 /****
1240 * This function iterates through the items in decreasing order,
1241 * followed by the special item, then -1. That is:
1242 * n, n-1, ..., 3, 2, 1, special, -1.
1243 * Each item is listed only once.
1245 static inline BOOL iterator_prev(ITERATOR* i)
1247 BOOL start = FALSE;
1249 if (i->nItem == -1)
1251 start = TRUE;
1252 if (i->ranges) i->index = DPA_GetPtrCount(i->ranges->hdpa);
1253 goto pickarange;
1255 if (i->nItem == i->nSpecial)
1257 i->nItem = -1;
1258 return FALSE;
1261 testitem:
1262 i->nItem--;
1263 if (i->nItem == i->nSpecial) i->nItem--;
1264 if (i->nItem >= i->range.lower) return TRUE;
1266 pickarange:
1267 if (i->ranges)
1269 if (i->index > 0)
1270 i->range = *(RANGE*)DPA_GetPtr(i->ranges->hdpa, --i->index);
1271 else goto end;
1273 else if (!start && i->nItem < i->range.lower) goto end;
1275 i->nItem = i->range.upper;
1276 if (i->nItem > 0) goto testitem;
1277 end:
1278 return (i->nItem = i->nSpecial) != -1;
1281 static RANGE iterator_range(const ITERATOR *i)
1283 RANGE range;
1285 if (!i->ranges) return i->range;
1287 if (DPA_GetPtrCount(i->ranges->hdpa) > 0)
1289 range.lower = (*(RANGE*)DPA_GetPtr(i->ranges->hdpa, 0)).lower;
1290 range.upper = (*(RANGE*)DPA_GetPtr(i->ranges->hdpa, DPA_GetPtrCount(i->ranges->hdpa) - 1)).upper;
1292 else range.lower = range.upper = 0;
1294 return range;
1297 /***
1298 * Releases resources associated with this iterator.
1300 static inline void iterator_destroy(const ITERATOR *i)
1302 ranges_destroy(i->ranges);
1305 /***
1306 * Create an empty iterator.
1308 static inline void iterator_empty(ITERATOR* i)
1310 ZeroMemory(i, sizeof(*i));
1311 i->nItem = i->nSpecial = i->range.lower = i->range.upper = -1;
1314 /***
1315 * Create an iterator over a range.
1317 static inline void iterator_rangeitems(ITERATOR* i, RANGE range)
1319 iterator_empty(i);
1320 i->range = range;
1323 /***
1324 * Create an iterator over a bunch of ranges.
1325 * Please note that the iterator will take ownership of the ranges,
1326 * and will free them upon destruction.
1328 static inline void iterator_rangesitems(ITERATOR* i, RANGES ranges)
1330 iterator_empty(i);
1331 i->ranges = ranges;
1334 /***
1335 * Creates an iterator over the items which intersect frame.
1336 * Uses absolute coordinates rather than compensating for the current offset.
1338 static BOOL iterator_frameditems_absolute(ITERATOR* i, const LISTVIEW_INFO* infoPtr, const RECT *frame)
1340 RECT rcItem, rcTemp;
1341 RANGES ranges;
1343 TRACE("(frame=%s)\n", wine_dbgstr_rect(frame));
1345 /* in case we fail, we want to return an empty iterator */
1346 iterator_empty(i);
1348 if (infoPtr->nItemCount == 0)
1349 return TRUE;
1351 if (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON)
1353 INT nItem;
1355 if (infoPtr->uView == LV_VIEW_ICON && infoPtr->nFocusedItem != -1)
1357 LISTVIEW_GetItemBox(infoPtr, infoPtr->nFocusedItem, &rcItem);
1358 if (IntersectRect(&rcTemp, &rcItem, frame))
1359 i->nSpecial = infoPtr->nFocusedItem;
1361 if (!(ranges = ranges_create(50))) return FALSE;
1362 iterator_rangesitems(i, ranges);
1363 /* to do better here, we need to have PosX, and PosY sorted */
1364 TRACE("building icon ranges:\n");
1365 for (nItem = 0; nItem < infoPtr->nItemCount; nItem++)
1367 rcItem.left = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosX, nItem);
1368 rcItem.top = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosY, nItem);
1369 rcItem.right = rcItem.left + infoPtr->nItemWidth;
1370 rcItem.bottom = rcItem.top + infoPtr->nItemHeight;
1371 if (IntersectRect(&rcTemp, &rcItem, frame))
1372 ranges_additem(i->ranges, nItem);
1374 return TRUE;
1376 else if (infoPtr->uView == LV_VIEW_DETAILS)
1378 RANGE range;
1380 if (frame->left >= infoPtr->nItemWidth) return TRUE;
1381 if (frame->top >= infoPtr->nItemHeight * infoPtr->nItemCount) return TRUE;
1383 range.lower = max(frame->top / infoPtr->nItemHeight, 0);
1384 range.upper = min((frame->bottom - 1) / infoPtr->nItemHeight, infoPtr->nItemCount - 1) + 1;
1385 if (range.upper <= range.lower) return TRUE;
1386 iterator_rangeitems(i, range);
1387 TRACE(" report=%s\n", debugrange(&i->range));
1389 else
1391 INT nPerCol = max((infoPtr->rcList.bottom - infoPtr->rcList.top) / infoPtr->nItemHeight, 1);
1392 INT nFirstRow = max(frame->top / infoPtr->nItemHeight, 0);
1393 INT nLastRow = min((frame->bottom - 1) / infoPtr->nItemHeight, nPerCol - 1);
1394 INT nFirstCol;
1395 INT nLastCol;
1396 INT lower;
1397 RANGE item_range;
1398 INT nCol;
1400 if (infoPtr->nItemWidth)
1402 nFirstCol = max(frame->left / infoPtr->nItemWidth, 0);
1403 nLastCol = min((frame->right - 1) / infoPtr->nItemWidth, (infoPtr->nItemCount + nPerCol - 1) / nPerCol);
1405 else
1407 nFirstCol = max(frame->left, 0);
1408 nLastCol = min(frame->right - 1, (infoPtr->nItemCount + nPerCol - 1) / nPerCol);
1411 lower = nFirstCol * nPerCol + nFirstRow;
1413 TRACE("nPerCol=%d, nFirstRow=%d, nLastRow=%d, nFirstCol=%d, nLastCol=%d, lower=%d\n",
1414 nPerCol, nFirstRow, nLastRow, nFirstCol, nLastCol, lower);
1416 if (nLastCol < nFirstCol || nLastRow < nFirstRow) return TRUE;
1418 if (!(ranges = ranges_create(nLastCol - nFirstCol + 1))) return FALSE;
1419 iterator_rangesitems(i, ranges);
1420 TRACE("building list ranges:\n");
1421 for (nCol = nFirstCol; nCol <= nLastCol; nCol++)
1423 item_range.lower = nCol * nPerCol + nFirstRow;
1424 if(item_range.lower >= infoPtr->nItemCount) break;
1425 item_range.upper = min(nCol * nPerCol + nLastRow + 1, infoPtr->nItemCount);
1426 TRACE(" list=%s\n", debugrange(&item_range));
1427 ranges_add(i->ranges, item_range);
1431 return TRUE;
1434 /***
1435 * Creates an iterator over the items which intersect lprc.
1437 static BOOL iterator_frameditems(ITERATOR* i, const LISTVIEW_INFO* infoPtr, const RECT *lprc)
1439 RECT frame = *lprc;
1440 POINT Origin;
1442 TRACE("(lprc=%s)\n", wine_dbgstr_rect(lprc));
1444 LISTVIEW_GetOrigin(infoPtr, &Origin);
1445 OffsetRect(&frame, -Origin.x, -Origin.y);
1447 return iterator_frameditems_absolute(i, infoPtr, &frame);
1450 /***
1451 * Creates an iterator over the items which intersect the visible region of hdc.
1453 static BOOL iterator_visibleitems(ITERATOR *i, const LISTVIEW_INFO *infoPtr, HDC hdc)
1455 POINT Origin, Position;
1456 RECT rcItem, rcClip;
1457 INT rgntype;
1459 rgntype = GetClipBox(hdc, &rcClip);
1460 if (rgntype == NULLREGION)
1462 iterator_empty(i);
1463 return TRUE;
1465 if (!iterator_frameditems(i, infoPtr, &rcClip)) return FALSE;
1466 if (rgntype == SIMPLEREGION) return TRUE;
1468 /* first deal with the special item */
1469 if (i->nSpecial != -1)
1471 LISTVIEW_GetItemBox(infoPtr, i->nSpecial, &rcItem);
1472 if (!RectVisible(hdc, &rcItem)) i->nSpecial = -1;
1475 /* if we can't deal with the region, we'll just go with the simple range */
1476 LISTVIEW_GetOrigin(infoPtr, &Origin);
1477 TRACE("building visible range:\n");
1478 if (!i->ranges && i->range.lower < i->range.upper)
1480 if (!(i->ranges = ranges_create(50))) return TRUE;
1481 if (!ranges_add(i->ranges, i->range))
1483 ranges_destroy(i->ranges);
1484 i->ranges = 0;
1485 return TRUE;
1489 /* now delete the invisible items from the list */
1490 while(iterator_next(i))
1492 LISTVIEW_GetItemOrigin(infoPtr, i->nItem, &Position);
1493 rcItem.left = (infoPtr->uView == LV_VIEW_DETAILS) ? Origin.x : Position.x + Origin.x;
1494 rcItem.top = Position.y + Origin.y;
1495 rcItem.right = rcItem.left + infoPtr->nItemWidth;
1496 rcItem.bottom = rcItem.top + infoPtr->nItemHeight;
1497 if (!RectVisible(hdc, &rcItem))
1498 ranges_delitem(i->ranges, i->nItem);
1500 /* the iterator should restart on the next iterator_next */
1501 TRACE("done\n");
1503 return TRUE;
1506 /* Remove common elements from two iterators */
1507 /* Passed iterators have to point on the first elements */
1508 static BOOL iterator_remove_common_items(ITERATOR *iter1, ITERATOR *iter2)
1510 if(!iter1->ranges || !iter2->ranges) {
1511 int lower, upper;
1513 if(iter1->ranges || iter2->ranges ||
1514 (iter1->range.lower<iter2->range.lower && iter1->range.upper>iter2->range.upper) ||
1515 (iter1->range.lower>iter2->range.lower && iter1->range.upper<iter2->range.upper)) {
1516 ERR("result is not a one range iterator\n");
1517 return FALSE;
1520 if(iter1->range.lower==-1 || iter2->range.lower==-1)
1521 return TRUE;
1523 lower = iter1->range.lower;
1524 upper = iter1->range.upper;
1526 if(lower < iter2->range.lower)
1527 iter1->range.upper = iter2->range.lower;
1528 else if(upper > iter2->range.upper)
1529 iter1->range.lower = iter2->range.upper;
1530 else
1531 iter1->range.lower = iter1->range.upper = -1;
1533 if(iter2->range.lower < lower)
1534 iter2->range.upper = lower;
1535 else if(iter2->range.upper > upper)
1536 iter2->range.lower = upper;
1537 else
1538 iter2->range.lower = iter2->range.upper = -1;
1540 return TRUE;
1543 iterator_next(iter1);
1544 iterator_next(iter2);
1546 while(1) {
1547 if(iter1->nItem==-1 || iter2->nItem==-1)
1548 break;
1550 if(iter1->nItem == iter2->nItem) {
1551 int delete = iter1->nItem;
1553 iterator_prev(iter1);
1554 iterator_prev(iter2);
1555 ranges_delitem(iter1->ranges, delete);
1556 ranges_delitem(iter2->ranges, delete);
1557 iterator_next(iter1);
1558 iterator_next(iter2);
1559 } else if(iter1->nItem > iter2->nItem)
1560 iterator_next(iter2);
1561 else
1562 iterator_next(iter1);
1565 iter1->nItem = iter1->range.lower = iter1->range.upper = -1;
1566 iter2->nItem = iter2->range.lower = iter2->range.upper = -1;
1567 return TRUE;
1570 /******** Misc helper functions ************************************/
1572 static inline LRESULT CallWindowProcT(WNDPROC proc, HWND hwnd, UINT uMsg,
1573 WPARAM wParam, LPARAM lParam, BOOL isW)
1575 if (isW) return CallWindowProcW(proc, hwnd, uMsg, wParam, lParam);
1576 else return CallWindowProcA(proc, hwnd, uMsg, wParam, lParam);
1579 static inline BOOL is_autoarrange(const LISTVIEW_INFO *infoPtr)
1581 return (infoPtr->dwStyle & LVS_AUTOARRANGE) &&
1582 (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON);
1585 static void toggle_checkbox_state(LISTVIEW_INFO *infoPtr, INT nItem)
1587 DWORD state = STATEIMAGEINDEX(LISTVIEW_GetItemState(infoPtr, nItem, LVIS_STATEIMAGEMASK));
1588 if(state == 1 || state == 2)
1590 LVITEMW lvitem;
1591 state ^= 3;
1592 lvitem.state = INDEXTOSTATEIMAGEMASK(state);
1593 lvitem.stateMask = LVIS_STATEIMAGEMASK;
1594 LISTVIEW_SetItemState(infoPtr, nItem, &lvitem);
1598 /* this should be called after window style got updated,
1599 it used to reset view state to match current window style */
1600 static inline void map_style_view(LISTVIEW_INFO *infoPtr)
1602 switch (infoPtr->dwStyle & LVS_TYPEMASK)
1604 case LVS_ICON:
1605 infoPtr->uView = LV_VIEW_ICON;
1606 break;
1607 case LVS_REPORT:
1608 infoPtr->uView = LV_VIEW_DETAILS;
1609 break;
1610 case LVS_SMALLICON:
1611 infoPtr->uView = LV_VIEW_SMALLICON;
1612 break;
1613 case LVS_LIST:
1614 infoPtr->uView = LV_VIEW_LIST;
1618 /* computes next item id value */
1619 static DWORD get_next_itemid(const LISTVIEW_INFO *infoPtr)
1621 INT count = DPA_GetPtrCount(infoPtr->hdpaItemIds);
1623 if (count > 0)
1625 ITEM_ID *lpID = DPA_GetPtr(infoPtr->hdpaItemIds, count - 1);
1626 return lpID->id + 1;
1628 return 0;
1631 /******** Internal API functions ************************************/
1633 static inline COLUMN_INFO * LISTVIEW_GetColumnInfo(const LISTVIEW_INFO *infoPtr, INT nSubItem)
1635 static COLUMN_INFO mainItem;
1637 if (nSubItem == 0 && DPA_GetPtrCount(infoPtr->hdpaColumns) == 0) return &mainItem;
1638 assert (nSubItem >= 0 && nSubItem < DPA_GetPtrCount(infoPtr->hdpaColumns));
1640 /* update cached column rectangles */
1641 if (infoPtr->colRectsDirty)
1643 COLUMN_INFO *info;
1644 LISTVIEW_INFO *Ptr = (LISTVIEW_INFO*)infoPtr;
1645 INT i;
1647 for (i = 0; i < DPA_GetPtrCount(infoPtr->hdpaColumns); i++) {
1648 info = DPA_GetPtr(infoPtr->hdpaColumns, i);
1649 SendMessageW(infoPtr->hwndHeader, HDM_GETITEMRECT, i, (LPARAM)&info->rcHeader);
1651 Ptr->colRectsDirty = FALSE;
1654 return DPA_GetPtr(infoPtr->hdpaColumns, nSubItem);
1657 static INT LISTVIEW_CreateHeader(LISTVIEW_INFO *infoPtr)
1659 DWORD dFlags = WS_CHILD | HDS_HORZ | HDS_FULLDRAG | HDS_DRAGDROP;
1660 HINSTANCE hInst;
1662 if (infoPtr->hwndHeader) return 0;
1664 TRACE("Creating header for list %p\n", infoPtr->hwndSelf);
1666 /* setup creation flags */
1667 dFlags |= (LVS_NOSORTHEADER & infoPtr->dwStyle) ? 0 : HDS_BUTTONS;
1668 dFlags |= (LVS_NOCOLUMNHEADER & infoPtr->dwStyle) ? HDS_HIDDEN : 0;
1670 hInst = (HINSTANCE)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_HINSTANCE);
1672 /* create header */
1673 infoPtr->hwndHeader = CreateWindowW(WC_HEADERW, NULL, dFlags,
1674 0, 0, 0, 0, infoPtr->hwndSelf, NULL, hInst, NULL);
1675 if (!infoPtr->hwndHeader) return -1;
1677 /* set header unicode format */
1678 SendMessageW(infoPtr->hwndHeader, HDM_SETUNICODEFORMAT, TRUE, 0);
1680 /* set header font */
1681 SendMessageW(infoPtr->hwndHeader, WM_SETFONT, (WPARAM)infoPtr->hFont, TRUE);
1683 /* set header image list */
1684 if (infoPtr->himlSmall)
1685 SendMessageW(infoPtr->hwndHeader, HDM_SETIMAGELIST, 0, (LPARAM)infoPtr->himlSmall);
1687 LISTVIEW_UpdateSize(infoPtr);
1689 return 0;
1692 static inline void LISTVIEW_GetHeaderRect(const LISTVIEW_INFO *infoPtr, INT nSubItem, LPRECT lprc)
1694 *lprc = LISTVIEW_GetColumnInfo(infoPtr, nSubItem)->rcHeader;
1697 static inline BOOL LISTVIEW_IsHeaderEnabled(const LISTVIEW_INFO *infoPtr)
1699 return (infoPtr->uView == LV_VIEW_DETAILS ||
1700 infoPtr->dwLvExStyle & LVS_EX_HEADERINALLVIEWS) &&
1701 !(infoPtr->dwStyle & LVS_NOCOLUMNHEADER);
1704 static inline BOOL LISTVIEW_GetItemW(const LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem)
1706 return LISTVIEW_GetItemT(infoPtr, lpLVItem, TRUE);
1709 /* used to handle collapse main item column case */
1710 static inline BOOL LISTVIEW_DrawFocusRect(const LISTVIEW_INFO *infoPtr, HDC hdc)
1712 return (infoPtr->rcFocus.left < infoPtr->rcFocus.right) ?
1713 DrawFocusRect(hdc, &infoPtr->rcFocus) : FALSE;
1716 /* Listview invalidation functions: use _only_ these functions to invalidate */
1718 static inline BOOL is_redrawing(const LISTVIEW_INFO *infoPtr)
1720 return infoPtr->redraw;
1723 static inline void LISTVIEW_InvalidateRect(const LISTVIEW_INFO *infoPtr, const RECT* rect)
1725 if(!is_redrawing(infoPtr)) return;
1726 TRACE(" invalidating rect=%s\n", wine_dbgstr_rect(rect));
1727 InvalidateRect(infoPtr->hwndSelf, rect, TRUE);
1730 static inline void LISTVIEW_InvalidateItem(const LISTVIEW_INFO *infoPtr, INT nItem)
1732 RECT rcBox;
1734 if (!is_redrawing(infoPtr) || nItem < 0 || nItem >= infoPtr->nItemCount)
1735 return;
1737 LISTVIEW_GetItemBox(infoPtr, nItem, &rcBox);
1738 LISTVIEW_InvalidateRect(infoPtr, &rcBox);
1741 static inline void LISTVIEW_InvalidateSubItem(const LISTVIEW_INFO *infoPtr, INT nItem, INT nSubItem)
1743 POINT Origin, Position;
1744 RECT rcBox;
1746 if(!is_redrawing(infoPtr)) return;
1747 assert (infoPtr->uView == LV_VIEW_DETAILS);
1748 LISTVIEW_GetOrigin(infoPtr, &Origin);
1749 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
1750 LISTVIEW_GetHeaderRect(infoPtr, nSubItem, &rcBox);
1751 rcBox.top = 0;
1752 rcBox.bottom = infoPtr->nItemHeight;
1753 OffsetRect(&rcBox, Origin.x, Origin.y + Position.y);
1754 LISTVIEW_InvalidateRect(infoPtr, &rcBox);
1757 static inline void LISTVIEW_InvalidateList(const LISTVIEW_INFO *infoPtr)
1759 LISTVIEW_InvalidateRect(infoPtr, NULL);
1762 static inline void LISTVIEW_InvalidateColumn(const LISTVIEW_INFO *infoPtr, INT nColumn)
1764 RECT rcCol;
1766 if(!is_redrawing(infoPtr)) return;
1767 LISTVIEW_GetHeaderRect(infoPtr, nColumn, &rcCol);
1768 rcCol.top = infoPtr->rcList.top;
1769 rcCol.bottom = infoPtr->rcList.bottom;
1770 LISTVIEW_InvalidateRect(infoPtr, &rcCol);
1773 /***
1774 * DESCRIPTION:
1775 * Retrieves the number of items that can fit vertically in the client area.
1777 * PARAMETER(S):
1778 * [I] infoPtr : valid pointer to the listview structure
1780 * RETURN:
1781 * Number of items per row.
1783 static inline INT LISTVIEW_GetCountPerRow(const LISTVIEW_INFO *infoPtr)
1785 INT nListWidth = infoPtr->rcList.right - infoPtr->rcList.left;
1787 return max(nListWidth/(infoPtr->nItemWidth ? infoPtr->nItemWidth : 1), 1);
1790 /***
1791 * DESCRIPTION:
1792 * Retrieves the number of items that can fit horizontally in the client
1793 * area.
1795 * PARAMETER(S):
1796 * [I] infoPtr : valid pointer to the listview structure
1798 * RETURN:
1799 * Number of items per column.
1801 static inline INT LISTVIEW_GetCountPerColumn(const LISTVIEW_INFO *infoPtr)
1803 INT nListHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
1805 return infoPtr->nItemHeight ? max(nListHeight / infoPtr->nItemHeight, 1) : 0;
1809 /*************************************************************************
1810 * LISTVIEW_ProcessLetterKeys
1812 * Processes keyboard messages generated by pressing the letter keys
1813 * on the keyboard.
1814 * What this does is perform a case insensitive search from the
1815 * current position with the following quirks:
1816 * - If two chars or more are pressed in quick succession we search
1817 * for the corresponding string (e.g. 'abc').
1818 * - If there is a delay we wipe away the current search string and
1819 * restart with just that char.
1820 * - If the user keeps pressing the same character, whether slowly or
1821 * fast, so that the search string is entirely composed of this
1822 * character ('aaaaa' for instance), then we search for first item
1823 * that starting with that character.
1824 * - If the user types the above character in quick succession, then
1825 * we must also search for the corresponding string ('aaaaa'), and
1826 * go to that string if there is a match.
1828 * PARAMETERS
1829 * [I] hwnd : handle to the window
1830 * [I] charCode : the character code, the actual character
1831 * [I] keyData : key data
1833 * RETURNS
1835 * Zero.
1837 * BUGS
1839 * - The current implementation has a list of characters it will
1840 * accept and it ignores everything else. In particular it will
1841 * ignore accentuated characters which seems to match what
1842 * Windows does. But I'm not sure it makes sense to follow
1843 * Windows there.
1844 * - We don't sound a beep when the search fails.
1846 * SEE ALSO
1848 * TREEVIEW_ProcessLetterKeys
1850 static INT LISTVIEW_ProcessLetterKeys(LISTVIEW_INFO *infoPtr, WPARAM charCode, LPARAM keyData)
1852 WCHAR buffer[MAX_PATH];
1853 DWORD prevTime;
1854 LVITEMW item;
1855 int startidx;
1856 INT nItem;
1857 INT diff;
1859 /* simple parameter checking */
1860 if (!charCode || !keyData || infoPtr->nItemCount == 0) return 0;
1862 /* only allow the valid WM_CHARs through */
1863 if (!iswalnum(charCode) &&
1864 charCode != '.' && charCode != '`' && charCode != '!' &&
1865 charCode != '@' && charCode != '#' && charCode != '$' &&
1866 charCode != '%' && charCode != '^' && charCode != '&' &&
1867 charCode != '*' && charCode != '(' && charCode != ')' &&
1868 charCode != '-' && charCode != '_' && charCode != '+' &&
1869 charCode != '=' && charCode != '\\'&& charCode != ']' &&
1870 charCode != '}' && charCode != '[' && charCode != '{' &&
1871 charCode != '/' && charCode != '?' && charCode != '>' &&
1872 charCode != '<' && charCode != ',' && charCode != '~')
1873 return 0;
1875 /* update the search parameters */
1876 prevTime = infoPtr->lastKeyPressTimestamp;
1877 infoPtr->lastKeyPressTimestamp = GetTickCount();
1878 diff = infoPtr->lastKeyPressTimestamp - prevTime;
1880 if (diff >= 0 && diff < KEY_DELAY)
1882 if (infoPtr->nSearchParamLength < MAX_PATH - 1)
1883 infoPtr->szSearchParam[infoPtr->nSearchParamLength++] = charCode;
1885 if (infoPtr->charCode != charCode)
1886 infoPtr->charCode = charCode = 0;
1888 else
1890 infoPtr->charCode = charCode;
1891 infoPtr->szSearchParam[0] = charCode;
1892 infoPtr->nSearchParamLength = 1;
1895 /* should start from next after focused item, so next item that matches
1896 will be selected, if there isn't any and focused matches it will be selected
1897 on second search stage from beginning of the list */
1898 if (infoPtr->nFocusedItem >= 0 && infoPtr->nItemCount > 1)
1900 /* with some accumulated search data available start with current focus, otherwise
1901 it's excluded from search */
1902 startidx = infoPtr->nSearchParamLength > 1 ? infoPtr->nFocusedItem : infoPtr->nFocusedItem + 1;
1903 if (startidx == infoPtr->nItemCount) startidx = 0;
1905 else
1906 startidx = 0;
1908 /* let application handle this for virtual listview */
1909 if (infoPtr->dwStyle & LVS_OWNERDATA)
1911 NMLVFINDITEMW nmlv;
1913 memset(&nmlv.lvfi, 0, sizeof(nmlv.lvfi));
1914 nmlv.lvfi.flags = (LVFI_WRAP | LVFI_PARTIAL);
1915 nmlv.lvfi.psz = infoPtr->szSearchParam;
1916 nmlv.iStart = startidx;
1918 infoPtr->szSearchParam[infoPtr->nSearchParamLength] = 0;
1920 nItem = notify_hdr(infoPtr, LVN_ODFINDITEMW, (LPNMHDR)&nmlv.hdr);
1922 else
1924 int i = startidx, endidx;
1926 /* and search from the current position */
1927 nItem = -1;
1928 endidx = infoPtr->nItemCount;
1930 /* first search in [startidx, endidx), on failure continue in [0, startidx) */
1931 while (1)
1933 /* start from first item if not found with >= startidx */
1934 if (i == infoPtr->nItemCount && startidx > 0)
1936 endidx = startidx;
1937 startidx = 0;
1940 for (i = startidx; i < endidx; i++)
1942 /* retrieve text */
1943 item.mask = LVIF_TEXT;
1944 item.iItem = i;
1945 item.iSubItem = 0;
1946 item.pszText = buffer;
1947 item.cchTextMax = MAX_PATH;
1948 if (!LISTVIEW_GetItemW(infoPtr, &item)) return 0;
1950 if (!wcsnicmp(item.pszText, infoPtr->szSearchParam, infoPtr->nSearchParamLength))
1952 nItem = i;
1953 break;
1955 /* this is used to find first char match when search string is not available yet,
1956 otherwise every WM_CHAR will search to next item by first char, ignoring that we're
1957 already waiting for user to complete a string */
1958 else if (nItem == -1 && infoPtr->nSearchParamLength == 1 && !wcsnicmp(item.pszText, infoPtr->szSearchParam, 1))
1960 /* this would work but we must keep looking for a longer match */
1961 nItem = i;
1965 if ( nItem != -1 || /* found something */
1966 endidx != infoPtr->nItemCount || /* second search done */
1967 (startidx == 0 && endidx == infoPtr->nItemCount) /* full range for first search */ )
1968 break;
1972 if (nItem != -1)
1973 LISTVIEW_KeySelection(infoPtr, nItem, FALSE);
1975 return 0;
1978 /*************************************************************************
1979 * LISTVIEW_UpdateHeaderSize [Internal]
1981 * Function to resize the header control
1983 * PARAMS
1984 * [I] hwnd : handle to a window
1985 * [I] nNewScrollPos : scroll pos to set
1987 * RETURNS
1988 * None.
1990 static void LISTVIEW_UpdateHeaderSize(const LISTVIEW_INFO *infoPtr, INT nNewScrollPos)
1992 RECT winRect;
1993 POINT point[2];
1995 TRACE("nNewScrollPos=%d\n", nNewScrollPos);
1997 if (!infoPtr->hwndHeader) return;
1999 GetWindowRect(infoPtr->hwndHeader, &winRect);
2000 point[0].x = winRect.left;
2001 point[0].y = winRect.top;
2002 point[1].x = winRect.right;
2003 point[1].y = winRect.bottom;
2005 MapWindowPoints(HWND_DESKTOP, infoPtr->hwndSelf, point, 2);
2006 point[0].x = -nNewScrollPos;
2007 point[1].x += nNewScrollPos;
2009 SetWindowPos(infoPtr->hwndHeader,0,
2010 point[0].x,point[0].y,point[1].x,point[1].y,
2011 (infoPtr->dwStyle & LVS_NOCOLUMNHEADER) ? SWP_HIDEWINDOW : SWP_SHOWWINDOW |
2012 SWP_NOZORDER | SWP_NOACTIVATE);
2015 static INT LISTVIEW_UpdateHScroll(LISTVIEW_INFO *infoPtr)
2017 SCROLLINFO horzInfo;
2018 INT dx;
2020 ZeroMemory(&horzInfo, sizeof(SCROLLINFO));
2021 horzInfo.cbSize = sizeof(SCROLLINFO);
2022 horzInfo.nPage = infoPtr->rcList.right - infoPtr->rcList.left;
2024 /* for now, we'll set info.nMax to the _count_, and adjust it later */
2025 if (infoPtr->uView == LV_VIEW_LIST)
2027 INT nPerCol = LISTVIEW_GetCountPerColumn(infoPtr);
2028 horzInfo.nMax = (infoPtr->nItemCount + nPerCol - 1) / nPerCol;
2030 /* scroll by at least one column per page */
2031 if(horzInfo.nPage < infoPtr->nItemWidth)
2032 horzInfo.nPage = infoPtr->nItemWidth;
2034 if (infoPtr->nItemWidth)
2035 horzInfo.nPage /= infoPtr->nItemWidth;
2037 else if (infoPtr->uView == LV_VIEW_DETAILS)
2039 horzInfo.nMax = infoPtr->nItemWidth;
2041 else /* LV_VIEW_ICON, or LV_VIEW_SMALLICON */
2043 RECT rcView;
2045 if (LISTVIEW_GetViewRect(infoPtr, &rcView)) horzInfo.nMax = rcView.right - rcView.left;
2048 if (LISTVIEW_IsHeaderEnabled(infoPtr))
2050 if (DPA_GetPtrCount(infoPtr->hdpaColumns))
2052 RECT rcHeader;
2053 INT index;
2055 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX,
2056 DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, 0);
2058 LISTVIEW_GetHeaderRect(infoPtr, index, &rcHeader);
2059 horzInfo.nMax = rcHeader.right;
2060 TRACE("horzInfo.nMax=%d\n", horzInfo.nMax);
2064 horzInfo.fMask = SIF_RANGE | SIF_PAGE;
2065 horzInfo.nMax = max(horzInfo.nMax - 1, 0);
2066 dx = GetScrollPos(infoPtr->hwndSelf, SB_HORZ);
2067 dx -= SetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &horzInfo, TRUE);
2068 TRACE("horzInfo=%s\n", debugscrollinfo(&horzInfo));
2070 /* Update the Header Control */
2071 if (infoPtr->hwndHeader)
2073 horzInfo.fMask = SIF_POS;
2074 GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &horzInfo);
2075 LISTVIEW_UpdateHeaderSize(infoPtr, horzInfo.nPos);
2078 LISTVIEW_UpdateSize(infoPtr);
2079 return dx;
2082 static INT LISTVIEW_UpdateVScroll(LISTVIEW_INFO *infoPtr)
2084 SCROLLINFO vertInfo;
2085 INT dy;
2087 ZeroMemory(&vertInfo, sizeof(SCROLLINFO));
2088 vertInfo.cbSize = sizeof(SCROLLINFO);
2089 vertInfo.nPage = infoPtr->rcList.bottom - infoPtr->rcList.top;
2091 if (infoPtr->uView == LV_VIEW_DETAILS)
2093 vertInfo.nMax = infoPtr->nItemCount;
2095 /* scroll by at least one page */
2096 if(vertInfo.nPage < infoPtr->nItemHeight)
2097 vertInfo.nPage = infoPtr->nItemHeight;
2099 if (infoPtr->nItemHeight > 0)
2100 vertInfo.nPage /= infoPtr->nItemHeight;
2102 else if (infoPtr->uView != LV_VIEW_LIST) /* LV_VIEW_ICON, or LV_VIEW_SMALLICON */
2104 RECT rcView;
2106 if (LISTVIEW_GetViewRect(infoPtr, &rcView)) vertInfo.nMax = rcView.bottom - rcView.top;
2109 vertInfo.fMask = SIF_RANGE | SIF_PAGE;
2110 vertInfo.nMax = max(vertInfo.nMax - 1, 0);
2111 dy = GetScrollPos(infoPtr->hwndSelf, SB_VERT);
2112 dy -= SetScrollInfo(infoPtr->hwndSelf, SB_VERT, &vertInfo, TRUE);
2113 TRACE("vertInfo=%s\n", debugscrollinfo(&vertInfo));
2115 LISTVIEW_UpdateSize(infoPtr);
2116 return dy;
2119 /***
2120 * DESCRIPTION:
2121 * Update the scrollbars. This function should be called whenever
2122 * the content, size or view changes.
2124 * PARAMETER(S):
2125 * [I] infoPtr : valid pointer to the listview structure
2127 * RETURN:
2128 * None
2130 static void LISTVIEW_UpdateScroll(LISTVIEW_INFO *infoPtr)
2132 INT dx, dy, pass;
2134 if ((infoPtr->dwStyle & LVS_NOSCROLL) || !is_redrawing(infoPtr)) return;
2136 /* Setting the horizontal scroll can change the listview size
2137 * (and potentially everything else) so we need to recompute
2138 * everything again for the vertical scroll and vice-versa
2140 for (dx = 0, dy = 0, pass = 0; pass <= 1; pass++)
2142 dx += LISTVIEW_UpdateHScroll(infoPtr);
2143 dy += LISTVIEW_UpdateVScroll(infoPtr);
2146 /* Change of the range may have changed the scroll pos. If so move the content */
2147 if (dx != 0 || dy != 0)
2149 RECT listRect;
2150 listRect = infoPtr->rcList;
2151 ScrollWindowEx(infoPtr->hwndSelf, dx, dy, &listRect, &listRect, 0, 0,
2152 SW_ERASE | SW_INVALIDATE);
2157 /***
2158 * DESCRIPTION:
2159 * Shows/hides the focus rectangle.
2161 * PARAMETER(S):
2162 * [I] infoPtr : valid pointer to the listview structure
2163 * [I] fShow : TRUE to show the focus, FALSE to hide it.
2165 * RETURN:
2166 * None
2168 static void LISTVIEW_ShowFocusRect(const LISTVIEW_INFO *infoPtr, BOOL fShow)
2170 HDC hdc;
2172 TRACE("fShow=%d, nItem=%d\n", fShow, infoPtr->nFocusedItem);
2174 if (infoPtr->nFocusedItem < 0) return;
2176 /* we need some gymnastics in ICON mode to handle large items */
2177 if (infoPtr->uView == LV_VIEW_ICON)
2179 RECT rcBox;
2181 LISTVIEW_GetItemBox(infoPtr, infoPtr->nFocusedItem, &rcBox);
2182 if ((rcBox.bottom - rcBox.top) > infoPtr->nItemHeight)
2184 LISTVIEW_InvalidateRect(infoPtr, &rcBox);
2185 return;
2189 if (!(hdc = GetDC(infoPtr->hwndSelf))) return;
2191 /* for some reason, owner draw should work only in report mode */
2192 if ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (infoPtr->uView == LV_VIEW_DETAILS))
2194 DRAWITEMSTRUCT dis;
2195 LVITEMW item;
2197 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
2198 HFONT hOldFont = SelectObject(hdc, hFont);
2200 item.iItem = infoPtr->nFocusedItem;
2201 item.iSubItem = 0;
2202 item.mask = LVIF_PARAM;
2203 if (!LISTVIEW_GetItemW(infoPtr, &item)) goto done;
2205 ZeroMemory(&dis, sizeof(dis));
2206 dis.CtlType = ODT_LISTVIEW;
2207 dis.CtlID = (UINT)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
2208 dis.itemID = item.iItem;
2209 dis.itemAction = ODA_FOCUS;
2210 if (fShow) dis.itemState |= ODS_FOCUS;
2211 dis.hwndItem = infoPtr->hwndSelf;
2212 dis.hDC = hdc;
2213 LISTVIEW_GetItemBox(infoPtr, dis.itemID, &dis.rcItem);
2214 dis.itemData = item.lParam;
2216 SendMessageW(infoPtr->hwndNotify, WM_DRAWITEM, dis.CtlID, (LPARAM)&dis);
2218 SelectObject(hdc, hOldFont);
2220 else
2221 LISTVIEW_InvalidateItem(infoPtr, infoPtr->nFocusedItem);
2223 done:
2224 ReleaseDC(infoPtr->hwndSelf, hdc);
2227 /***
2228 * Invalidates all visible selected items.
2230 static void LISTVIEW_InvalidateSelectedItems(const LISTVIEW_INFO *infoPtr)
2232 ITERATOR i;
2234 iterator_frameditems(&i, infoPtr, &infoPtr->rcList);
2235 while(iterator_next(&i))
2237 if (LISTVIEW_GetItemState(infoPtr, i.nItem, LVIS_SELECTED))
2238 LISTVIEW_InvalidateItem(infoPtr, i.nItem);
2240 iterator_destroy(&i);
2244 /***
2245 * DESCRIPTION: [INTERNAL]
2246 * Computes an item's (left,top) corner, relative to rcView.
2247 * That is, the position has NOT been made relative to the Origin.
2248 * This is deliberate, to avoid computing the Origin over, and
2249 * over again, when this function is called in a loop. Instead,
2250 * one can factor the computation of the Origin before the loop,
2251 * and offset the value returned by this function, on every iteration.
2253 * PARAMETER(S):
2254 * [I] infoPtr : valid pointer to the listview structure
2255 * [I] nItem : item number
2256 * [O] lpptOrig : item top, left corner
2258 * RETURN:
2259 * None.
2261 static void LISTVIEW_GetItemOrigin(const LISTVIEW_INFO *infoPtr, INT nItem, LPPOINT lpptPosition)
2263 assert(nItem >= 0 && nItem < infoPtr->nItemCount);
2265 if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
2267 lpptPosition->x = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosX, nItem);
2268 lpptPosition->y = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosY, nItem);
2270 else if (infoPtr->uView == LV_VIEW_LIST)
2272 INT nCountPerColumn = LISTVIEW_GetCountPerColumn(infoPtr);
2273 lpptPosition->x = nItem / nCountPerColumn * infoPtr->nItemWidth;
2274 lpptPosition->y = nItem % nCountPerColumn * infoPtr->nItemHeight;
2276 else /* LV_VIEW_DETAILS */
2278 lpptPosition->x = REPORT_MARGINX;
2279 /* item is always at zero indexed column */
2280 if (DPA_GetPtrCount(infoPtr->hdpaColumns) > 0)
2281 lpptPosition->x += LISTVIEW_GetColumnInfo(infoPtr, 0)->rcHeader.left;
2282 lpptPosition->y = nItem * infoPtr->nItemHeight;
2286 /***
2287 * DESCRIPTION: [INTERNAL]
2288 * Compute the rectangles of an item. This is to localize all
2289 * the computations in one place. If you are not interested in some
2290 * of these values, simply pass in a NULL -- the function is smart
2291 * enough to compute only what's necessary. The function computes
2292 * the standard rectangles (BOUNDS, ICON, LABEL) plus a non-standard
2293 * one, the BOX rectangle. This rectangle is very cheap to compute,
2294 * and is guaranteed to contain all the other rectangles. Computing
2295 * the ICON rect is also cheap, but all the others are potentially
2296 * expensive. This gives an easy and effective optimization when
2297 * searching (like point inclusion, or rectangle intersection):
2298 * first test against the BOX, and if TRUE, test against the desired
2299 * rectangle.
2300 * If the function does not have all the necessary information
2301 * to computed the requested rectangles, will crash with a
2302 * failed assertion. This is done so we catch all programming
2303 * errors, given that the function is called only from our code.
2305 * We have the following 'special' meanings for a few fields:
2306 * * If LVIS_FOCUSED is set, we assume the item has the focus
2307 * This is important in ICON mode, where it might get a larger
2308 * then usual rectangle
2310 * Please note that subitem support works only in REPORT mode.
2312 * PARAMETER(S):
2313 * [I] infoPtr : valid pointer to the listview structure
2314 * [I] lpLVItem : item to compute the measures for
2315 * [O] lprcBox : ptr to Box rectangle
2316 * Same as LVM_GETITEMRECT with LVIR_BOUNDS
2317 * [0] lprcSelectBox : ptr to select box rectangle
2318 * Same as LVM_GETITEMRECT with LVIR_SELECTEDBOUNDS
2319 * [O] lprcIcon : ptr to Icon rectangle
2320 * Same as LVM_GETITEMRECT with LVIR_ICON
2321 * [O] lprcStateIcon: ptr to State Icon rectangle
2322 * [O] lprcLabel : ptr to Label rectangle
2323 * Same as LVM_GETITEMRECT with LVIR_LABEL
2325 * RETURN:
2326 * None.
2328 static void LISTVIEW_GetItemMetrics(const LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem,
2329 LPRECT lprcBox, LPRECT lprcSelectBox,
2330 LPRECT lprcIcon, LPRECT lprcStateIcon, LPRECT lprcLabel)
2332 BOOL doSelectBox = FALSE, doIcon = FALSE, doLabel = FALSE, oversizedBox = FALSE;
2333 RECT Box, SelectBox, Icon, Label;
2334 COLUMN_INFO *lpColumnInfo = NULL;
2335 SIZE labelSize = { 0, 0 };
2337 TRACE("(lpLVItem=%s)\n", debuglvitem_t(lpLVItem, TRUE));
2339 /* Be smart and try to figure out the minimum we have to do */
2340 if (lpLVItem->iSubItem) assert(infoPtr->uView == LV_VIEW_DETAILS);
2341 if (infoPtr->uView == LV_VIEW_ICON && (lprcBox || lprcLabel))
2343 assert((lpLVItem->mask & LVIF_STATE) && (lpLVItem->stateMask & LVIS_FOCUSED));
2344 if (lpLVItem->state & LVIS_FOCUSED) oversizedBox = doLabel = TRUE;
2346 if (lprcSelectBox) doSelectBox = TRUE;
2347 if (lprcLabel) doLabel = TRUE;
2348 if (doLabel || lprcIcon || lprcStateIcon) doIcon = TRUE;
2349 if (doSelectBox)
2351 doIcon = TRUE;
2352 doLabel = TRUE;
2355 /************************************************************/
2356 /* compute the box rectangle (it should be cheap to do) */
2357 /************************************************************/
2358 if (lpLVItem->iSubItem || infoPtr->uView == LV_VIEW_DETAILS)
2359 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, lpLVItem->iSubItem);
2361 if (lpLVItem->iSubItem)
2363 Box = lpColumnInfo->rcHeader;
2365 else
2367 Box.left = 0;
2368 Box.right = infoPtr->nItemWidth;
2370 Box.top = 0;
2371 Box.bottom = infoPtr->nItemHeight;
2373 /******************************************************************/
2374 /* compute ICON bounding box (ala LVM_GETITEMRECT) and STATEICON */
2375 /******************************************************************/
2376 if (doIcon)
2378 LONG state_width = 0;
2380 if (infoPtr->himlState && lpLVItem->iSubItem == 0)
2381 state_width = infoPtr->iconStateSize.cx;
2383 if (infoPtr->uView == LV_VIEW_ICON)
2385 Icon.left = Box.left + state_width;
2386 if (infoPtr->himlNormal)
2387 Icon.left += (infoPtr->nItemWidth - infoPtr->iconSize.cx - state_width) / 2;
2388 Icon.top = Box.top + ICON_TOP_PADDING;
2389 Icon.right = Icon.left;
2390 Icon.bottom = Icon.top;
2391 if (infoPtr->himlNormal)
2393 Icon.right += infoPtr->iconSize.cx;
2394 Icon.bottom += infoPtr->iconSize.cy;
2397 else /* LV_VIEW_SMALLICON, LV_VIEW_LIST or LV_VIEW_DETAILS */
2399 Icon.left = Box.left + state_width;
2401 if (infoPtr->uView == LV_VIEW_DETAILS && lpLVItem->iSubItem == 0)
2403 /* we need the indent in report mode */
2404 assert(lpLVItem->mask & LVIF_INDENT);
2405 Icon.left += infoPtr->iconSize.cx * lpLVItem->iIndent + REPORT_MARGINX;
2408 Icon.top = Box.top;
2409 Icon.right = Icon.left;
2410 if (infoPtr->himlSmall &&
2411 (!lpColumnInfo || lpLVItem->iSubItem == 0 ||
2412 ((infoPtr->dwLvExStyle & LVS_EX_SUBITEMIMAGES) && lpLVItem->iImage != I_IMAGECALLBACK)))
2413 Icon.right += infoPtr->iconSize.cx;
2414 Icon.bottom = Icon.top + infoPtr->iconSize.cy;
2416 if(lprcIcon) *lprcIcon = Icon;
2417 TRACE(" - icon=%s\n", wine_dbgstr_rect(&Icon));
2419 /* TODO: is this correct? */
2420 if (lprcStateIcon)
2422 lprcStateIcon->left = Icon.left - state_width;
2423 lprcStateIcon->right = Icon.left;
2424 lprcStateIcon->top = Icon.top;
2425 lprcStateIcon->bottom = lprcStateIcon->top + infoPtr->iconSize.cy;
2426 TRACE(" - state icon=%s\n", wine_dbgstr_rect(lprcStateIcon));
2429 else Icon.right = 0;
2431 /************************************************************/
2432 /* compute LABEL bounding box (ala LVM_GETITEMRECT) */
2433 /************************************************************/
2434 if (doLabel)
2436 /* calculate how far to the right can the label stretch */
2437 Label.right = Box.right;
2438 if (infoPtr->uView == LV_VIEW_DETAILS)
2440 if (lpLVItem->iSubItem == 0)
2442 /* we need a zero based rect here */
2443 Label = lpColumnInfo->rcHeader;
2444 OffsetRect(&Label, -Label.left, 0);
2448 if (lpLVItem->iSubItem || ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && infoPtr->uView == LV_VIEW_DETAILS))
2450 labelSize.cx = infoPtr->nItemWidth;
2451 labelSize.cy = infoPtr->nItemHeight;
2452 goto calc_label;
2455 /* we need the text in non owner draw mode */
2456 assert(lpLVItem->mask & LVIF_TEXT);
2457 if (is_text(lpLVItem->pszText))
2459 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
2460 HDC hdc = GetDC(infoPtr->hwndSelf);
2461 HFONT hOldFont = SelectObject(hdc, hFont);
2462 UINT uFormat;
2463 RECT rcText;
2465 /* compute rough rectangle where the label will go */
2466 SetRectEmpty(&rcText);
2467 rcText.right = infoPtr->nItemWidth - TRAILING_LABEL_PADDING;
2468 rcText.bottom = infoPtr->nItemHeight;
2469 if (infoPtr->uView == LV_VIEW_ICON)
2470 rcText.bottom -= ICON_TOP_PADDING + infoPtr->iconSize.cy + ICON_BOTTOM_PADDING;
2472 /* now figure out the flags */
2473 if (infoPtr->uView == LV_VIEW_ICON)
2474 uFormat = oversizedBox ? LV_FL_DT_FLAGS : LV_ML_DT_FLAGS;
2475 else
2476 uFormat = LV_SL_DT_FLAGS;
2478 DrawTextW (hdc, lpLVItem->pszText, -1, &rcText, uFormat | DT_CALCRECT);
2480 if (rcText.right != rcText.left)
2481 labelSize.cx = min(rcText.right - rcText.left + TRAILING_LABEL_PADDING, infoPtr->nItemWidth);
2483 labelSize.cy = rcText.bottom - rcText.top;
2485 SelectObject(hdc, hOldFont);
2486 ReleaseDC(infoPtr->hwndSelf, hdc);
2489 calc_label:
2490 if (infoPtr->uView == LV_VIEW_ICON)
2492 Label.left = Box.left + (infoPtr->nItemWidth - labelSize.cx) / 2;
2493 Label.top = Box.top + ICON_TOP_PADDING_HITABLE +
2494 infoPtr->iconSize.cy + ICON_BOTTOM_PADDING;
2495 Label.right = Label.left + labelSize.cx;
2496 Label.bottom = Label.top + infoPtr->nItemHeight;
2497 if (!oversizedBox && labelSize.cy > infoPtr->ntmHeight)
2499 labelSize.cy = min(Box.bottom - Label.top, labelSize.cy);
2500 labelSize.cy /= infoPtr->ntmHeight;
2501 labelSize.cy = max(labelSize.cy, 1);
2502 labelSize.cy *= infoPtr->ntmHeight;
2504 Label.bottom = Label.top + labelSize.cy + HEIGHT_PADDING;
2506 else if (infoPtr->uView == LV_VIEW_DETAILS)
2508 Label.left = Icon.right;
2509 Label.top = Box.top;
2510 Label.right = lpLVItem->iSubItem ? lpColumnInfo->rcHeader.right :
2511 lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left;
2512 Label.bottom = Label.top + infoPtr->nItemHeight;
2514 else /* LV_VIEW_SMALLICON or LV_VIEW_LIST */
2516 Label.left = Icon.right;
2517 Label.top = Box.top;
2518 Label.right = min(Label.left + labelSize.cx, Label.right);
2519 Label.bottom = Label.top + infoPtr->nItemHeight;
2522 if (lprcLabel) *lprcLabel = Label;
2523 TRACE(" - label=%s\n", wine_dbgstr_rect(&Label));
2526 /************************************************************/
2527 /* compute SELECT bounding box */
2528 /************************************************************/
2529 if (doSelectBox)
2531 if (infoPtr->uView == LV_VIEW_DETAILS)
2533 SelectBox.left = Icon.left;
2534 SelectBox.top = Box.top;
2535 SelectBox.bottom = Box.bottom;
2537 if (labelSize.cx)
2538 SelectBox.right = min(Label.left + labelSize.cx, Label.right);
2539 else
2540 SelectBox.right = min(Label.left + MAX_EMPTYTEXT_SELECT_WIDTH, Label.right);
2542 else
2544 UnionRect(&SelectBox, &Icon, &Label);
2546 if (lprcSelectBox) *lprcSelectBox = SelectBox;
2547 TRACE(" - select box=%s\n", wine_dbgstr_rect(&SelectBox));
2550 /* Fix the Box if necessary */
2551 if (lprcBox)
2553 if (oversizedBox) UnionRect(lprcBox, &Box, &Label);
2554 else *lprcBox = Box;
2556 TRACE(" - box=%s\n", wine_dbgstr_rect(&Box));
2559 /***
2560 * DESCRIPTION: [INTERNAL]
2562 * PARAMETER(S):
2563 * [I] infoPtr : valid pointer to the listview structure
2564 * [I] nItem : item number
2565 * [O] lprcBox : ptr to Box rectangle
2567 * RETURN:
2568 * None.
2570 static void LISTVIEW_GetItemBox(const LISTVIEW_INFO *infoPtr, INT nItem, LPRECT lprcBox)
2572 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
2573 POINT Position, Origin;
2574 LVITEMW lvItem;
2576 LISTVIEW_GetOrigin(infoPtr, &Origin);
2577 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
2579 /* Be smart and try to figure out the minimum we have to do */
2580 lvItem.mask = 0;
2581 if (infoPtr->uView == LV_VIEW_ICON && infoPtr->bFocus && LISTVIEW_GetItemState(infoPtr, nItem, LVIS_FOCUSED))
2582 lvItem.mask |= LVIF_TEXT;
2583 lvItem.iItem = nItem;
2584 lvItem.iSubItem = 0;
2585 lvItem.pszText = szDispText;
2586 lvItem.cchTextMax = DISP_TEXT_SIZE;
2587 if (lvItem.mask) LISTVIEW_GetItemW(infoPtr, &lvItem);
2588 if (infoPtr->uView == LV_VIEW_ICON)
2590 lvItem.mask |= LVIF_STATE;
2591 lvItem.stateMask = LVIS_FOCUSED;
2592 lvItem.state = (lvItem.mask & LVIF_TEXT ? LVIS_FOCUSED : 0);
2594 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, lprcBox, 0, 0, 0, 0);
2596 if (infoPtr->uView == LV_VIEW_DETAILS && infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT &&
2597 SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, 0, 0))
2599 OffsetRect(lprcBox, Origin.x, Position.y + Origin.y);
2601 else
2602 OffsetRect(lprcBox, Position.x + Origin.x, Position.y + Origin.y);
2605 /* LISTVIEW_MapIdToIndex helper */
2606 static INT CALLBACK MapIdSearchCompare(LPVOID p1, LPVOID p2, LPARAM lParam)
2608 ITEM_ID *id1 = (ITEM_ID*)p1;
2609 ITEM_ID *id2 = (ITEM_ID*)p2;
2611 if (id1->id == id2->id) return 0;
2613 return (id1->id < id2->id) ? -1 : 1;
2616 /***
2617 * DESCRIPTION:
2618 * Returns the item index for id specified.
2620 * PARAMETER(S):
2621 * [I] infoPtr : valid pointer to the listview structure
2622 * [I] iID : item id to get index for
2624 * RETURN:
2625 * Item index, or -1 on failure.
2627 static INT LISTVIEW_MapIdToIndex(const LISTVIEW_INFO *infoPtr, UINT iID)
2629 ITEM_ID ID;
2630 INT index;
2632 TRACE("iID=%d\n", iID);
2634 if (infoPtr->dwStyle & LVS_OWNERDATA) return -1;
2635 if (infoPtr->nItemCount == 0) return -1;
2637 ID.id = iID;
2638 index = DPA_Search(infoPtr->hdpaItemIds, &ID, -1, MapIdSearchCompare, 0, DPAS_SORTED);
2640 if (index != -1)
2642 ITEM_ID *lpID = DPA_GetPtr(infoPtr->hdpaItemIds, index);
2643 return DPA_GetPtrIndex(infoPtr->hdpaItems, lpID->item);
2646 return -1;
2649 /***
2650 * DESCRIPTION:
2651 * Returns the item id for index given.
2653 * PARAMETER(S):
2654 * [I] infoPtr : valid pointer to the listview structure
2655 * [I] iItem : item index to get id for
2657 * RETURN:
2658 * Item id.
2660 static DWORD LISTVIEW_MapIndexToId(const LISTVIEW_INFO *infoPtr, INT iItem)
2662 ITEM_INFO *lpItem;
2663 HDPA hdpaSubItems;
2665 TRACE("iItem=%d\n", iItem);
2667 if (infoPtr->dwStyle & LVS_OWNERDATA) return -1;
2668 if (iItem < 0 || iItem >= infoPtr->nItemCount) return -1;
2670 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, iItem);
2671 lpItem = DPA_GetPtr(hdpaSubItems, 0);
2673 return lpItem->id->id;
2676 /***
2677 * DESCRIPTION:
2678 * Returns the current icon position, and advances it along the top.
2679 * The returned position is not offset by Origin.
2681 * PARAMETER(S):
2682 * [I] infoPtr : valid pointer to the listview structure
2683 * [O] lpPos : will get the current icon position
2685 * RETURN:
2686 * None
2688 static void LISTVIEW_NextIconPosTop(LISTVIEW_INFO *infoPtr, LPPOINT lpPos)
2690 INT nListWidth = infoPtr->rcList.right - infoPtr->rcList.left;
2692 *lpPos = infoPtr->currIconPos;
2694 infoPtr->currIconPos.x += infoPtr->nItemWidth;
2695 if (infoPtr->currIconPos.x + infoPtr->nItemWidth <= nListWidth) return;
2697 infoPtr->currIconPos.x = 0;
2698 infoPtr->currIconPos.y += infoPtr->nItemHeight;
2702 /***
2703 * DESCRIPTION:
2704 * Returns the current icon position, and advances it down the left edge.
2705 * The returned position is not offset by Origin.
2707 * PARAMETER(S):
2708 * [I] infoPtr : valid pointer to the listview structure
2709 * [O] lpPos : will get the current icon position
2711 * RETURN:
2712 * None
2714 static void LISTVIEW_NextIconPosLeft(LISTVIEW_INFO *infoPtr, LPPOINT lpPos)
2716 INT nListHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
2718 *lpPos = infoPtr->currIconPos;
2720 infoPtr->currIconPos.y += infoPtr->nItemHeight;
2721 if (infoPtr->currIconPos.y + infoPtr->nItemHeight <= nListHeight) return;
2723 infoPtr->currIconPos.x += infoPtr->nItemWidth;
2724 infoPtr->currIconPos.y = 0;
2728 /***
2729 * DESCRIPTION:
2730 * Moves an icon to the specified position.
2731 * It takes care of invalidating the item, etc.
2733 * PARAMETER(S):
2734 * [I] infoPtr : valid pointer to the listview structure
2735 * [I] nItem : the item to move
2736 * [I] lpPos : the new icon position
2737 * [I] isNew : flags the item as being new
2739 * RETURN:
2740 * Success: TRUE
2741 * Failure: FALSE
2743 static BOOL LISTVIEW_MoveIconTo(const LISTVIEW_INFO *infoPtr, INT nItem, const POINT *lppt, BOOL isNew)
2745 POINT old;
2747 if (!isNew)
2749 old.x = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosX, nItem);
2750 old.y = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosY, nItem);
2752 if (lppt->x == old.x && lppt->y == old.y) return TRUE;
2753 LISTVIEW_InvalidateItem(infoPtr, nItem);
2756 /* Allocating a POINTER for every item is too resource intensive,
2757 * so we'll keep the (x,y) in different arrays */
2758 if (!DPA_SetPtr(infoPtr->hdpaPosX, nItem, (void *)(LONG_PTR)lppt->x)) return FALSE;
2759 if (!DPA_SetPtr(infoPtr->hdpaPosY, nItem, (void *)(LONG_PTR)lppt->y)) return FALSE;
2761 LISTVIEW_InvalidateItem(infoPtr, nItem);
2763 return TRUE;
2766 /***
2767 * DESCRIPTION:
2768 * Arranges listview items in icon display mode.
2770 * PARAMETER(S):
2771 * [I] infoPtr : valid pointer to the listview structure
2772 * [I] nAlignCode : alignment code
2774 * RETURN:
2775 * SUCCESS : TRUE
2776 * FAILURE : FALSE
2778 static BOOL LISTVIEW_Arrange(LISTVIEW_INFO *infoPtr, INT nAlignCode)
2780 void (*next_pos)(LISTVIEW_INFO *, LPPOINT);
2781 POINT pos;
2782 INT i;
2784 if (infoPtr->uView != LV_VIEW_ICON && infoPtr->uView != LV_VIEW_SMALLICON) return FALSE;
2786 TRACE("nAlignCode=%d\n", nAlignCode);
2788 if (nAlignCode == LVA_DEFAULT)
2790 if (infoPtr->dwStyle & LVS_ALIGNLEFT) nAlignCode = LVA_ALIGNLEFT;
2791 else nAlignCode = LVA_ALIGNTOP;
2794 switch (nAlignCode)
2796 case LVA_ALIGNLEFT: next_pos = LISTVIEW_NextIconPosLeft; break;
2797 case LVA_ALIGNTOP: next_pos = LISTVIEW_NextIconPosTop; break;
2798 case LVA_SNAPTOGRID: next_pos = LISTVIEW_NextIconPosTop; break; /* FIXME */
2799 default: return FALSE;
2802 infoPtr->currIconPos.x = infoPtr->currIconPos.y = 0;
2803 for (i = 0; i < infoPtr->nItemCount; i++)
2805 next_pos(infoPtr, &pos);
2806 LISTVIEW_MoveIconTo(infoPtr, i, &pos, FALSE);
2809 return TRUE;
2812 /***
2813 * DESCRIPTION:
2814 * Retrieves the bounding rectangle of all the items, not offset by Origin.
2815 * For LVS_REPORT always returns empty rectangle.
2817 * PARAMETER(S):
2818 * [I] infoPtr : valid pointer to the listview structure
2819 * [O] lprcView : bounding rectangle
2821 * RETURN:
2822 * SUCCESS : TRUE
2823 * FAILURE : FALSE
2825 static void LISTVIEW_GetAreaRect(const LISTVIEW_INFO *infoPtr, LPRECT lprcView)
2827 INT i, x, y;
2829 SetRectEmpty(lprcView);
2831 switch (infoPtr->uView)
2833 case LV_VIEW_ICON:
2834 case LV_VIEW_SMALLICON:
2835 for (i = 0; i < infoPtr->nItemCount; i++)
2837 x = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosX, i);
2838 y = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosY, i);
2839 lprcView->right = max(lprcView->right, x);
2840 lprcView->bottom = max(lprcView->bottom, y);
2842 if (infoPtr->nItemCount > 0)
2844 lprcView->right += infoPtr->nItemWidth;
2845 lprcView->bottom += infoPtr->nItemHeight;
2847 break;
2849 case LV_VIEW_LIST:
2850 y = LISTVIEW_GetCountPerColumn(infoPtr);
2851 x = infoPtr->nItemCount / y;
2852 if (infoPtr->nItemCount % y) x++;
2853 lprcView->right = x * infoPtr->nItemWidth;
2854 lprcView->bottom = y * infoPtr->nItemHeight;
2855 break;
2859 /***
2860 * DESCRIPTION:
2861 * Retrieves the bounding rectangle of all the items.
2863 * PARAMETER(S):
2864 * [I] infoPtr : valid pointer to the listview structure
2865 * [O] lprcView : bounding rectangle
2867 * RETURN:
2868 * SUCCESS : TRUE
2869 * FAILURE : FALSE
2871 static BOOL LISTVIEW_GetViewRect(const LISTVIEW_INFO *infoPtr, LPRECT lprcView)
2873 POINT ptOrigin;
2875 TRACE("(lprcView=%p)\n", lprcView);
2877 if (!lprcView) return FALSE;
2879 LISTVIEW_GetAreaRect(infoPtr, lprcView);
2881 if (infoPtr->uView != LV_VIEW_DETAILS)
2883 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
2884 OffsetRect(lprcView, ptOrigin.x, ptOrigin.y);
2887 TRACE("lprcView=%s\n", wine_dbgstr_rect(lprcView));
2889 return TRUE;
2892 /***
2893 * DESCRIPTION:
2894 * Retrieves the subitem pointer associated with the subitem index.
2896 * PARAMETER(S):
2897 * [I] hdpaSubItems : DPA handle for a specific item
2898 * [I] nSubItem : index of subitem
2900 * RETURN:
2901 * SUCCESS : subitem pointer
2902 * FAILURE : NULL
2904 static SUBITEM_INFO* LISTVIEW_GetSubItemPtr(HDPA hdpaSubItems, INT nSubItem)
2906 SUBITEM_INFO *lpSubItem;
2907 INT i;
2909 /* we should binary search here if need be */
2910 for (i = 1; i < DPA_GetPtrCount(hdpaSubItems); i++)
2912 lpSubItem = DPA_GetPtr(hdpaSubItems, i);
2913 if (lpSubItem->iSubItem == nSubItem)
2914 return lpSubItem;
2917 return NULL;
2921 /***
2922 * DESCRIPTION:
2923 * Calculates the desired item width.
2925 * PARAMETER(S):
2926 * [I] infoPtr : valid pointer to the listview structure
2928 * RETURN:
2929 * The desired item width.
2931 static INT LISTVIEW_CalculateItemWidth(const LISTVIEW_INFO *infoPtr)
2933 INT nItemWidth = 0;
2935 TRACE("view %ld\n", infoPtr->uView);
2937 if (infoPtr->uView == LV_VIEW_ICON)
2938 nItemWidth = infoPtr->iconSpacing.cx;
2939 else if (infoPtr->uView == LV_VIEW_DETAILS)
2941 if (DPA_GetPtrCount(infoPtr->hdpaColumns) > 0)
2943 RECT rcHeader;
2944 INT index;
2946 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX,
2947 DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, 0);
2949 LISTVIEW_GetHeaderRect(infoPtr, index, &rcHeader);
2950 nItemWidth = rcHeader.right;
2953 else /* LV_VIEW_SMALLICON, or LV_VIEW_LIST */
2955 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
2956 LVITEMW lvItem;
2957 INT i;
2959 lvItem.mask = LVIF_TEXT;
2960 lvItem.iSubItem = 0;
2962 for (i = 0; i < infoPtr->nItemCount; i++)
2964 lvItem.iItem = i;
2965 lvItem.pszText = szDispText;
2966 lvItem.cchTextMax = DISP_TEXT_SIZE;
2967 if (LISTVIEW_GetItemW(infoPtr, &lvItem))
2968 nItemWidth = max(LISTVIEW_GetStringWidthT(infoPtr, lvItem.pszText, TRUE),
2969 nItemWidth);
2972 if (infoPtr->himlSmall) nItemWidth += infoPtr->iconSize.cx;
2973 if (infoPtr->himlState) nItemWidth += infoPtr->iconStateSize.cx;
2975 nItemWidth = max(DEFAULT_COLUMN_WIDTH, nItemWidth + WIDTH_PADDING);
2978 return nItemWidth;
2981 /***
2982 * DESCRIPTION:
2983 * Calculates the desired item height.
2985 * PARAMETER(S):
2986 * [I] infoPtr : valid pointer to the listview structure
2988 * RETURN:
2989 * The desired item height.
2991 static INT LISTVIEW_CalculateItemHeight(const LISTVIEW_INFO *infoPtr)
2993 INT nItemHeight;
2995 TRACE("view %ld\n", infoPtr->uView);
2997 if (infoPtr->uView == LV_VIEW_ICON)
2998 nItemHeight = infoPtr->iconSpacing.cy;
2999 else
3001 nItemHeight = infoPtr->ntmHeight;
3002 if (infoPtr->himlState)
3003 nItemHeight = max(nItemHeight, infoPtr->iconStateSize.cy);
3004 if (infoPtr->himlSmall)
3005 nItemHeight = max(nItemHeight, infoPtr->iconSize.cy);
3006 nItemHeight += HEIGHT_PADDING;
3007 if (infoPtr->nMeasureItemHeight > 0)
3008 nItemHeight = infoPtr->nMeasureItemHeight;
3011 return max(nItemHeight, 1);
3014 /***
3015 * DESCRIPTION:
3016 * Updates the width, and height of an item.
3018 * PARAMETER(S):
3019 * [I] infoPtr : valid pointer to the listview structure
3021 * RETURN:
3022 * None.
3024 static inline void LISTVIEW_UpdateItemSize(LISTVIEW_INFO *infoPtr)
3026 infoPtr->nItemWidth = LISTVIEW_CalculateItemWidth(infoPtr);
3027 infoPtr->nItemHeight = LISTVIEW_CalculateItemHeight(infoPtr);
3031 /***
3032 * DESCRIPTION:
3033 * Retrieves and saves important text metrics info for the current
3034 * Listview font.
3036 * PARAMETER(S):
3037 * [I] infoPtr : valid pointer to the listview structure
3040 static void LISTVIEW_SaveTextMetrics(LISTVIEW_INFO *infoPtr)
3042 HDC hdc = GetDC(infoPtr->hwndSelf);
3043 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
3044 HFONT hOldFont = SelectObject(hdc, hFont);
3045 TEXTMETRICW tm;
3046 SIZE sz;
3048 if (GetTextMetricsW(hdc, &tm))
3050 infoPtr->ntmHeight = tm.tmHeight;
3051 infoPtr->ntmMaxCharWidth = tm.tmMaxCharWidth;
3054 if (GetTextExtentPoint32A(hdc, "...", 3, &sz))
3055 infoPtr->nEllipsisWidth = sz.cx;
3057 SelectObject(hdc, hOldFont);
3058 ReleaseDC(infoPtr->hwndSelf, hdc);
3060 TRACE("tmHeight=%d\n", infoPtr->ntmHeight);
3063 /***
3064 * DESCRIPTION:
3065 * A compare function for ranges
3067 * PARAMETER(S)
3068 * [I] range1 : pointer to range 1;
3069 * [I] range2 : pointer to range 2;
3070 * [I] flags : flags
3072 * RETURNS:
3073 * > 0 : if range 1 > range 2
3074 * < 0 : if range 2 > range 1
3075 * = 0 : if range intersects range 2
3077 static INT CALLBACK ranges_cmp(LPVOID range1, LPVOID range2, LPARAM flags)
3079 INT cmp;
3081 if (((RANGE*)range1)->upper <= ((RANGE*)range2)->lower)
3082 cmp = -1;
3083 else if (((RANGE*)range2)->upper <= ((RANGE*)range1)->lower)
3084 cmp = 1;
3085 else
3086 cmp = 0;
3088 TRACE("range1=%s, range2=%s, cmp=%d\n", debugrange(range1), debugrange(range2), cmp);
3090 return cmp;
3093 #define ranges_check(ranges, desc) if (TRACE_ON(listview)) ranges_assert(ranges, desc, __FILE__, __LINE__)
3095 static void ranges_assert(RANGES ranges, LPCSTR desc, const char *file, int line)
3097 INT i;
3098 RANGE *prev, *curr;
3100 TRACE("*** Checking %s:%d:%s ***\n", file, line, desc);
3101 assert (ranges);
3102 assert (DPA_GetPtrCount(ranges->hdpa) >= 0);
3103 ranges_dump(ranges);
3104 if (DPA_GetPtrCount(ranges->hdpa) > 0)
3106 prev = DPA_GetPtr(ranges->hdpa, 0);
3107 assert (prev->lower >= 0 && prev->lower < prev->upper);
3108 for (i = 1; i < DPA_GetPtrCount(ranges->hdpa); i++)
3110 curr = DPA_GetPtr(ranges->hdpa, i);
3111 assert (prev->upper <= curr->lower);
3112 assert (curr->lower < curr->upper);
3113 prev = curr;
3116 TRACE("--- Done checking---\n");
3119 static RANGES ranges_create(int count)
3121 RANGES ranges = Alloc(sizeof(struct tagRANGES));
3122 if (!ranges) return NULL;
3123 ranges->hdpa = DPA_Create(count);
3124 if (ranges->hdpa) return ranges;
3125 Free(ranges);
3126 return NULL;
3129 static void ranges_clear(RANGES ranges)
3131 INT i;
3133 for(i = 0; i < DPA_GetPtrCount(ranges->hdpa); i++)
3134 Free(DPA_GetPtr(ranges->hdpa, i));
3135 DPA_DeleteAllPtrs(ranges->hdpa);
3139 static void ranges_destroy(RANGES ranges)
3141 if (!ranges) return;
3142 ranges_clear(ranges);
3143 DPA_Destroy(ranges->hdpa);
3144 Free(ranges);
3147 static RANGES ranges_clone(RANGES ranges)
3149 RANGES clone;
3150 INT i;
3152 if (!(clone = ranges_create(DPA_GetPtrCount(ranges->hdpa)))) goto fail;
3154 for (i = 0; i < DPA_GetPtrCount(ranges->hdpa); i++)
3156 RANGE *newrng = Alloc(sizeof(RANGE));
3157 if (!newrng) goto fail;
3158 *newrng = *((RANGE*)DPA_GetPtr(ranges->hdpa, i));
3159 if (!DPA_SetPtr(clone->hdpa, i, newrng))
3161 Free(newrng);
3162 goto fail;
3165 return clone;
3167 fail:
3168 TRACE ("clone failed\n");
3169 ranges_destroy(clone);
3170 return NULL;
3173 static RANGES ranges_diff(RANGES ranges, RANGES sub)
3175 INT i;
3177 for (i = 0; i < DPA_GetPtrCount(sub->hdpa); i++)
3178 ranges_del(ranges, *((RANGE *)DPA_GetPtr(sub->hdpa, i)));
3180 return ranges;
3183 static void ranges_dump(RANGES ranges)
3185 INT i;
3187 for (i = 0; i < DPA_GetPtrCount(ranges->hdpa); i++)
3188 TRACE(" %s\n", debugrange(DPA_GetPtr(ranges->hdpa, i)));
3191 static inline BOOL ranges_contain(RANGES ranges, INT nItem)
3193 RANGE srchrng = { nItem, nItem + 1 };
3195 TRACE("(nItem=%d)\n", nItem);
3196 ranges_check(ranges, "before contain");
3197 return DPA_Search(ranges->hdpa, &srchrng, 0, ranges_cmp, 0, DPAS_SORTED) != -1;
3200 static INT ranges_itemcount(RANGES ranges)
3202 INT i, count = 0;
3204 for (i = 0; i < DPA_GetPtrCount(ranges->hdpa); i++)
3206 RANGE *sel = DPA_GetPtr(ranges->hdpa, i);
3207 count += sel->upper - sel->lower;
3210 return count;
3213 static BOOL ranges_shift(RANGES ranges, INT nItem, INT delta, INT nUpper)
3215 RANGE srchrng = { nItem, nItem + 1 }, *chkrng;
3216 INT index;
3218 index = DPA_Search(ranges->hdpa, &srchrng, 0, ranges_cmp, 0, DPAS_SORTED | DPAS_INSERTAFTER);
3219 if (index == -1) return TRUE;
3221 for (; index < DPA_GetPtrCount(ranges->hdpa); index++)
3223 chkrng = DPA_GetPtr(ranges->hdpa, index);
3224 if (chkrng->lower >= nItem)
3225 chkrng->lower = max(min(chkrng->lower + delta, nUpper - 1), 0);
3226 if (chkrng->upper > nItem)
3227 chkrng->upper = max(min(chkrng->upper + delta, nUpper), 0);
3229 return TRUE;
3232 static BOOL ranges_add(RANGES ranges, RANGE range)
3234 RANGE srchrgn;
3235 INT index;
3237 TRACE("(%s)\n", debugrange(&range));
3238 ranges_check(ranges, "before add");
3240 /* try find overlapping regions first */
3241 srchrgn.lower = range.lower - 1;
3242 srchrgn.upper = range.upper + 1;
3243 index = DPA_Search(ranges->hdpa, &srchrgn, 0, ranges_cmp, 0, DPAS_SORTED);
3245 if (index == -1)
3247 RANGE *newrgn;
3249 TRACE("Adding new range\n");
3251 /* create the brand new range to insert */
3252 newrgn = Alloc(sizeof(RANGE));
3253 if(!newrgn) goto fail;
3254 *newrgn = range;
3256 /* figure out where to insert it */
3257 index = DPA_Search(ranges->hdpa, newrgn, 0, ranges_cmp, 0, DPAS_SORTED | DPAS_INSERTAFTER);
3258 TRACE("index=%d\n", index);
3259 if (index == -1) index = 0;
3261 /* and get it over with */
3262 if (DPA_InsertPtr(ranges->hdpa, index, newrgn) == -1)
3264 Free(newrgn);
3265 goto fail;
3268 else
3270 RANGE *chkrgn, *mrgrgn;
3271 INT fromindex, mergeindex;
3273 chkrgn = DPA_GetPtr(ranges->hdpa, index);
3274 TRACE("Merge with %s @%d\n", debugrange(chkrgn), index);
3276 chkrgn->lower = min(range.lower, chkrgn->lower);
3277 chkrgn->upper = max(range.upper, chkrgn->upper);
3279 TRACE("New range %s @%d\n", debugrange(chkrgn), index);
3281 /* merge now common ranges */
3282 fromindex = 0;
3283 srchrgn.lower = chkrgn->lower - 1;
3284 srchrgn.upper = chkrgn->upper + 1;
3288 mergeindex = DPA_Search(ranges->hdpa, &srchrgn, fromindex, ranges_cmp, 0, 0);
3289 if (mergeindex == -1) break;
3290 if (mergeindex == index)
3292 fromindex = index + 1;
3293 continue;
3296 TRACE("Merge with index %i\n", mergeindex);
3298 mrgrgn = DPA_GetPtr(ranges->hdpa, mergeindex);
3299 chkrgn->lower = min(chkrgn->lower, mrgrgn->lower);
3300 chkrgn->upper = max(chkrgn->upper, mrgrgn->upper);
3301 Free(mrgrgn);
3302 DPA_DeletePtr(ranges->hdpa, mergeindex);
3303 if (mergeindex < index) index --;
3304 } while(1);
3307 ranges_check(ranges, "after add");
3308 return TRUE;
3310 fail:
3311 ranges_check(ranges, "failed add");
3312 return FALSE;
3315 static BOOL ranges_del(RANGES ranges, RANGE range)
3317 RANGE *chkrgn;
3318 INT index;
3320 TRACE("(%s)\n", debugrange(&range));
3321 ranges_check(ranges, "before del");
3323 /* we don't use DPAS_SORTED here, since we need *
3324 * to find the first overlapping range */
3325 index = DPA_Search(ranges->hdpa, &range, 0, ranges_cmp, 0, 0);
3326 while(index != -1)
3328 chkrgn = DPA_GetPtr(ranges->hdpa, index);
3330 TRACE("Matches range %s @%d\n", debugrange(chkrgn), index);
3332 /* case 1: Same range */
3333 if ( (chkrgn->upper == range.upper) &&
3334 (chkrgn->lower == range.lower) )
3336 DPA_DeletePtr(ranges->hdpa, index);
3337 Free(chkrgn);
3338 break;
3340 /* case 2: engulf */
3341 else if ( (chkrgn->upper <= range.upper) &&
3342 (chkrgn->lower >= range.lower) )
3344 DPA_DeletePtr(ranges->hdpa, index);
3345 Free(chkrgn);
3347 /* case 3: overlap upper */
3348 else if ( (chkrgn->upper <= range.upper) &&
3349 (chkrgn->lower < range.lower) )
3351 chkrgn->upper = range.lower;
3353 /* case 4: overlap lower */
3354 else if ( (chkrgn->upper > range.upper) &&
3355 (chkrgn->lower >= range.lower) )
3357 chkrgn->lower = range.upper;
3358 break;
3360 /* case 5: fully internal */
3361 else
3363 RANGE *newrgn;
3365 if (!(newrgn = Alloc(sizeof(RANGE)))) goto fail;
3366 newrgn->lower = chkrgn->lower;
3367 newrgn->upper = range.lower;
3368 chkrgn->lower = range.upper;
3369 if (DPA_InsertPtr(ranges->hdpa, index, newrgn) == -1)
3371 Free(newrgn);
3372 goto fail;
3374 break;
3377 index = DPA_Search(ranges->hdpa, &range, index, ranges_cmp, 0, 0);
3380 ranges_check(ranges, "after del");
3381 return TRUE;
3383 fail:
3384 ranges_check(ranges, "failed del");
3385 return FALSE;
3388 /***
3389 * DESCRIPTION:
3390 * Removes all selection ranges
3392 * Parameters(s):
3393 * [I] infoPtr : valid pointer to the listview structure
3394 * [I] toSkip : item range to skip removing the selection
3396 * RETURNS:
3397 * SUCCESS : TRUE
3398 * FAILURE : FALSE
3400 static BOOL LISTVIEW_DeselectAllSkipItems(LISTVIEW_INFO *infoPtr, RANGES toSkip)
3402 LVITEMW lvItem;
3403 ITERATOR i;
3404 RANGES clone;
3406 TRACE("()\n");
3408 lvItem.state = 0;
3409 lvItem.stateMask = LVIS_SELECTED;
3411 /* Only send one deselect all (-1) notification for LVS_OWNERDATA style */
3412 if (infoPtr->dwStyle & LVS_OWNERDATA)
3414 LISTVIEW_SetItemState(infoPtr, -1, &lvItem);
3415 return TRUE;
3418 /* need to clone the DPA because callbacks can change it */
3419 if (!(clone = ranges_clone(infoPtr->selectionRanges))) return FALSE;
3420 iterator_rangesitems(&i, ranges_diff(clone, toSkip));
3421 while(iterator_next(&i))
3422 LISTVIEW_SetItemState(infoPtr, i.nItem, &lvItem);
3423 /* note that the iterator destructor will free the cloned range */
3424 iterator_destroy(&i);
3426 return TRUE;
3429 static inline BOOL LISTVIEW_DeselectAllSkipItem(LISTVIEW_INFO *infoPtr, INT nItem)
3431 RANGES toSkip;
3433 if (!(toSkip = ranges_create(1))) return FALSE;
3434 if (nItem != -1) ranges_additem(toSkip, nItem);
3435 LISTVIEW_DeselectAllSkipItems(infoPtr, toSkip);
3436 ranges_destroy(toSkip);
3437 return TRUE;
3440 static inline BOOL LISTVIEW_DeselectAll(LISTVIEW_INFO *infoPtr)
3442 return LISTVIEW_DeselectAllSkipItem(infoPtr, -1);
3445 /***
3446 * DESCRIPTION:
3447 * Retrieves the number of items that are marked as selected.
3449 * PARAMETER(S):
3450 * [I] infoPtr : valid pointer to the listview structure
3452 * RETURN:
3453 * Number of items selected.
3455 static INT LISTVIEW_GetSelectedCount(const LISTVIEW_INFO *infoPtr)
3457 INT nSelectedCount = 0;
3459 if (infoPtr->uCallbackMask & LVIS_SELECTED)
3461 INT i;
3462 for (i = 0; i < infoPtr->nItemCount; i++)
3464 if (LISTVIEW_GetItemState(infoPtr, i, LVIS_SELECTED))
3465 nSelectedCount++;
3468 else
3469 nSelectedCount = ranges_itemcount(infoPtr->selectionRanges);
3471 TRACE("nSelectedCount=%d\n", nSelectedCount);
3472 return nSelectedCount;
3475 /***
3476 * DESCRIPTION:
3477 * Manages the item focus.
3479 * PARAMETER(S):
3480 * [I] infoPtr : valid pointer to the listview structure
3481 * [I] nItem : item index
3483 * RETURN:
3484 * TRUE : focused item changed
3485 * FALSE : focused item has NOT changed
3487 static inline BOOL LISTVIEW_SetItemFocus(LISTVIEW_INFO *infoPtr, INT nItem)
3489 INT oldFocus = infoPtr->nFocusedItem;
3490 LVITEMW lvItem;
3492 if (nItem == infoPtr->nFocusedItem) return FALSE;
3494 lvItem.state = nItem == -1 ? 0 : LVIS_FOCUSED;
3495 lvItem.stateMask = LVIS_FOCUSED;
3496 LISTVIEW_SetItemState(infoPtr, nItem == -1 ? infoPtr->nFocusedItem : nItem, &lvItem);
3498 return oldFocus != infoPtr->nFocusedItem;
3501 static INT shift_item(const LISTVIEW_INFO *infoPtr, INT nShiftItem, INT nItem, INT direction)
3503 if (nShiftItem < nItem) return nShiftItem;
3505 if (nShiftItem > nItem) return nShiftItem + direction;
3507 if (direction > 0) return nShiftItem + direction;
3509 return min(nShiftItem, infoPtr->nItemCount - 1);
3512 /* This function updates focus index.
3514 Parameters:
3515 focus : current focus index
3516 item : index of item to be added/removed
3517 direction : add/remove flag
3519 static void LISTVIEW_ShiftFocus(LISTVIEW_INFO *infoPtr, INT focus, INT item, INT direction)
3521 DWORD old_mask = infoPtr->notify_mask & NOTIFY_MASK_ITEM_CHANGE;
3523 infoPtr->notify_mask &= ~NOTIFY_MASK_ITEM_CHANGE;
3524 focus = shift_item(infoPtr, focus, item, direction);
3525 if (focus != infoPtr->nFocusedItem)
3526 LISTVIEW_SetItemFocus(infoPtr, focus);
3527 infoPtr->notify_mask |= old_mask;
3531 * DESCRIPTION:
3532 * Updates the various indices after an item has been inserted or deleted.
3534 * PARAMETER(S):
3535 * [I] infoPtr : valid pointer to the listview structure
3536 * [I] nItem : item index
3537 * [I] direction : Direction of shift, +1 or -1.
3539 * RETURN:
3540 * None
3542 static void LISTVIEW_ShiftIndices(LISTVIEW_INFO *infoPtr, INT nItem, INT direction)
3544 TRACE("Shifting %i, %i steps\n", nItem, direction);
3546 ranges_shift(infoPtr->selectionRanges, nItem, direction, infoPtr->nItemCount);
3547 assert(abs(direction) == 1);
3548 infoPtr->nSelectionMark = shift_item(infoPtr, infoPtr->nSelectionMark, nItem, direction);
3550 /* But we are not supposed to modify nHotItem! */
3554 * DESCRIPTION:
3555 * Adds a block of selections.
3557 * PARAMETER(S):
3558 * [I] infoPtr : valid pointer to the listview structure
3559 * [I] nItem : item index
3561 * RETURN:
3562 * Whether the window is still valid.
3564 static BOOL LISTVIEW_AddGroupSelection(LISTVIEW_INFO *infoPtr, INT nItem)
3566 INT nFirst = min(infoPtr->nSelectionMark, nItem);
3567 INT nLast = max(infoPtr->nSelectionMark, nItem);
3568 HWND hwndSelf = infoPtr->hwndSelf;
3569 DWORD old_mask;
3570 LVITEMW item;
3571 INT i;
3573 /* Temporarily disable change notification
3574 * If the control is LVS_OWNERDATA, we need to send
3575 * only one LVN_ODSTATECHANGED notification.
3576 * See MSDN documentation for LVN_ITEMCHANGED.
3578 old_mask = infoPtr->notify_mask & NOTIFY_MASK_ITEM_CHANGE;
3579 if (infoPtr->dwStyle & LVS_OWNERDATA)
3580 infoPtr->notify_mask &= ~NOTIFY_MASK_ITEM_CHANGE;
3582 if (nFirst == -1) nFirst = nItem;
3584 item.state = LVIS_SELECTED;
3585 item.stateMask = LVIS_SELECTED;
3587 for (i = nFirst; i <= nLast; i++)
3588 LISTVIEW_SetItemState(infoPtr,i,&item);
3590 LISTVIEW_SetOwnerDataState(infoPtr, nFirst, nLast, &item);
3592 if (!IsWindow(hwndSelf))
3593 return FALSE;
3594 infoPtr->notify_mask |= old_mask;
3595 return TRUE;
3599 /***
3600 * DESCRIPTION:
3601 * Sets a single group selection.
3603 * PARAMETER(S):
3604 * [I] infoPtr : valid pointer to the listview structure
3605 * [I] nItem : item index
3607 * RETURN:
3608 * None
3610 static void LISTVIEW_SetGroupSelection(LISTVIEW_INFO *infoPtr, INT nItem)
3612 RANGES selection;
3613 DWORD old_mask;
3614 LVITEMW item;
3615 ITERATOR i;
3617 if (!(selection = ranges_create(100))) return;
3619 item.state = LVIS_SELECTED;
3620 item.stateMask = LVIS_SELECTED;
3622 if ((infoPtr->uView == LV_VIEW_LIST) || (infoPtr->uView == LV_VIEW_DETAILS))
3624 if (infoPtr->nSelectionMark == -1)
3626 infoPtr->nSelectionMark = nItem;
3627 ranges_additem(selection, nItem);
3629 else
3631 RANGE sel;
3633 sel.lower = min(infoPtr->nSelectionMark, nItem);
3634 sel.upper = max(infoPtr->nSelectionMark, nItem) + 1;
3635 ranges_add(selection, sel);
3638 else
3640 RECT rcItem, rcSel, rcSelMark;
3641 POINT ptItem;
3643 rcItem.left = LVIR_BOUNDS;
3644 if (!LISTVIEW_GetItemRect(infoPtr, nItem, &rcItem)) {
3645 ranges_destroy (selection);
3646 return;
3648 rcSelMark.left = LVIR_BOUNDS;
3649 if (!LISTVIEW_GetItemRect(infoPtr, infoPtr->nSelectionMark, &rcSelMark)) {
3650 ranges_destroy (selection);
3651 return;
3653 UnionRect(&rcSel, &rcItem, &rcSelMark);
3654 iterator_frameditems(&i, infoPtr, &rcSel);
3655 while(iterator_next(&i))
3657 LISTVIEW_GetItemPosition(infoPtr, i.nItem, &ptItem);
3658 if (PtInRect(&rcSel, ptItem)) ranges_additem(selection, i.nItem);
3660 iterator_destroy(&i);
3663 /* disable per item notifications on LVS_OWNERDATA style
3664 FIXME: single LVN_ODSTATECHANGED should be used */
3665 old_mask = infoPtr->notify_mask & NOTIFY_MASK_ITEM_CHANGE;
3666 if (infoPtr->dwStyle & LVS_OWNERDATA)
3667 infoPtr->notify_mask &= ~NOTIFY_MASK_ITEM_CHANGE;
3669 LISTVIEW_DeselectAllSkipItems(infoPtr, selection);
3672 iterator_rangesitems(&i, selection);
3673 while(iterator_next(&i))
3674 LISTVIEW_SetItemState(infoPtr, i.nItem, &item);
3675 /* this will also destroy the selection */
3676 iterator_destroy(&i);
3678 infoPtr->notify_mask |= old_mask;
3679 LISTVIEW_SetItemFocus(infoPtr, nItem);
3682 /***
3683 * DESCRIPTION:
3684 * Sets a single selection.
3686 * PARAMETER(S):
3687 * [I] infoPtr : valid pointer to the listview structure
3688 * [I] nItem : item index
3690 * RETURN:
3691 * None
3693 static void LISTVIEW_SetSelection(LISTVIEW_INFO *infoPtr, INT nItem)
3695 LVITEMW lvItem;
3697 TRACE("nItem=%d\n", nItem);
3699 LISTVIEW_DeselectAllSkipItem(infoPtr, nItem);
3701 lvItem.state = LVIS_FOCUSED | LVIS_SELECTED;
3702 lvItem.stateMask = LVIS_FOCUSED | LVIS_SELECTED;
3703 LISTVIEW_SetItemState(infoPtr, nItem, &lvItem);
3705 infoPtr->nSelectionMark = nItem;
3708 /***
3709 * DESCRIPTION:
3710 * Set selection(s) with keyboard.
3712 * PARAMETER(S):
3713 * [I] infoPtr : valid pointer to the listview structure
3714 * [I] nItem : item index
3715 * [I] space : VK_SPACE code sent
3717 * RETURN:
3718 * SUCCESS : TRUE (needs to be repainted)
3719 * FAILURE : FALSE (nothing has changed)
3721 static BOOL LISTVIEW_KeySelection(LISTVIEW_INFO *infoPtr, INT nItem, BOOL space)
3723 /* FIXME: pass in the state */
3724 WORD wShift = GetKeyState(VK_SHIFT) & 0x8000;
3725 WORD wCtrl = GetKeyState(VK_CONTROL) & 0x8000;
3726 BOOL bResult = FALSE;
3728 TRACE("nItem=%d, wShift=%d, wCtrl=%d\n", nItem, wShift, wCtrl);
3729 if ((nItem >= 0) && (nItem < infoPtr->nItemCount))
3731 bResult = TRUE;
3733 if (infoPtr->dwStyle & LVS_SINGLESEL || (wShift == 0 && wCtrl == 0))
3734 LISTVIEW_SetSelection(infoPtr, nItem);
3735 else
3737 if (wShift)
3738 LISTVIEW_SetGroupSelection(infoPtr, nItem);
3739 else if (wCtrl)
3741 LVITEMW lvItem;
3742 lvItem.state = ~LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED);
3743 lvItem.stateMask = LVIS_SELECTED;
3744 if (space)
3746 LISTVIEW_SetItemState(infoPtr, nItem, &lvItem);
3747 if (lvItem.state & LVIS_SELECTED)
3748 infoPtr->nSelectionMark = nItem;
3750 bResult = LISTVIEW_SetItemFocus(infoPtr, nItem);
3753 LISTVIEW_EnsureVisible(infoPtr, nItem, FALSE);
3756 UpdateWindow(infoPtr->hwndSelf); /* update client area */
3757 return bResult;
3760 static BOOL LISTVIEW_GetItemAtPt(const LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem, POINT pt)
3762 LVHITTESTINFO lvHitTestInfo;
3764 ZeroMemory(&lvHitTestInfo, sizeof(lvHitTestInfo));
3765 lvHitTestInfo.pt.x = pt.x;
3766 lvHitTestInfo.pt.y = pt.y;
3768 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
3770 lpLVItem->mask = LVIF_PARAM;
3771 lpLVItem->iItem = lvHitTestInfo.iItem;
3772 lpLVItem->iSubItem = 0;
3774 return LISTVIEW_GetItemT(infoPtr, lpLVItem, TRUE);
3777 static inline BOOL LISTVIEW_IsHotTracking(const LISTVIEW_INFO *infoPtr)
3779 return ((infoPtr->dwLvExStyle & LVS_EX_TRACKSELECT) ||
3780 (infoPtr->dwLvExStyle & LVS_EX_ONECLICKACTIVATE) ||
3781 (infoPtr->dwLvExStyle & LVS_EX_TWOCLICKACTIVATE));
3784 /***
3785 * DESCRIPTION:
3786 * Called when the mouse is being actively tracked and has hovered for a specified
3787 * amount of time
3789 * PARAMETER(S):
3790 * [I] infoPtr : valid pointer to the listview structure
3791 * [I] fwKeys : key indicator
3792 * [I] x,y : mouse position
3794 * RETURN:
3795 * 0 if the message was processed, non-zero if there was an error
3797 * INFO:
3798 * LVS_EX_TRACKSELECT: An item is automatically selected when the cursor remains
3799 * over the item for a certain period of time.
3802 static LRESULT LISTVIEW_MouseHover(LISTVIEW_INFO *infoPtr, INT x, INT y)
3804 NMHDR hdr;
3806 if (notify_hdr(infoPtr, NM_HOVER, &hdr)) return 0;
3808 if (LISTVIEW_IsHotTracking(infoPtr))
3810 LVITEMW item;
3811 POINT pt;
3813 pt.x = x;
3814 pt.y = y;
3816 if (LISTVIEW_GetItemAtPt(infoPtr, &item, pt))
3817 LISTVIEW_SetSelection(infoPtr, item.iItem);
3819 SetFocus(infoPtr->hwndSelf);
3822 return 0;
3825 #define SCROLL_LEFT 0x1
3826 #define SCROLL_RIGHT 0x2
3827 #define SCROLL_UP 0x4
3828 #define SCROLL_DOWN 0x8
3830 /***
3831 * DESCRIPTION:
3832 * Utility routine to draw and highlight items within a marquee selection rectangle.
3834 * PARAMETER(S):
3835 * [I] infoPtr : valid pointer to the listview structure
3836 * [I] coords_orig : original co-ordinates of the cursor
3837 * [I] coords_offs : offsetted coordinates of the cursor
3838 * [I] offset : offset amount
3839 * [I] scroll : Bitmask of which directions we should scroll, if at all
3841 * RETURN:
3842 * None.
3844 static void LISTVIEW_MarqueeHighlight(LISTVIEW_INFO *infoPtr, const POINT *coords_orig,
3845 INT scroll)
3847 BOOL controlDown = FALSE;
3848 LVITEMW item;
3849 ITERATOR old_elems, new_elems;
3850 RECT rect;
3851 POINT coords_offs, offset;
3853 /* Ensure coordinates are within client bounds */
3854 coords_offs.x = max(min(coords_orig->x, infoPtr->rcList.right), 0);
3855 coords_offs.y = max(min(coords_orig->y, infoPtr->rcList.bottom), 0);
3857 /* Get offset */
3858 LISTVIEW_GetOrigin(infoPtr, &offset);
3860 /* Offset coordinates by the appropriate amount */
3861 coords_offs.x -= offset.x;
3862 coords_offs.y -= offset.y;
3864 if (coords_offs.x > infoPtr->marqueeOrigin.x)
3866 rect.left = infoPtr->marqueeOrigin.x;
3867 rect.right = coords_offs.x;
3869 else
3871 rect.left = coords_offs.x;
3872 rect.right = infoPtr->marqueeOrigin.x;
3875 if (coords_offs.y > infoPtr->marqueeOrigin.y)
3877 rect.top = infoPtr->marqueeOrigin.y;
3878 rect.bottom = coords_offs.y;
3880 else
3882 rect.top = coords_offs.y;
3883 rect.bottom = infoPtr->marqueeOrigin.y;
3886 /* Cancel out the old marquee rectangle and draw the new one */
3887 LISTVIEW_InvalidateRect(infoPtr, &infoPtr->marqueeDrawRect);
3889 /* Scroll by the appropriate distance if applicable - speed up scrolling as
3890 the cursor is further away */
3892 if ((scroll & SCROLL_LEFT) && (coords_orig->x <= 0))
3893 LISTVIEW_Scroll(infoPtr, coords_orig->x, 0);
3895 if ((scroll & SCROLL_RIGHT) && (coords_orig->x >= infoPtr->rcList.right))
3896 LISTVIEW_Scroll(infoPtr, (coords_orig->x - infoPtr->rcList.right), 0);
3898 if ((scroll & SCROLL_UP) && (coords_orig->y <= 0))
3899 LISTVIEW_Scroll(infoPtr, 0, coords_orig->y);
3901 if ((scroll & SCROLL_DOWN) && (coords_orig->y >= infoPtr->rcList.bottom))
3902 LISTVIEW_Scroll(infoPtr, 0, (coords_orig->y - infoPtr->rcList.bottom));
3904 iterator_frameditems_absolute(&old_elems, infoPtr, &infoPtr->marqueeRect);
3906 infoPtr->marqueeRect = rect;
3907 infoPtr->marqueeDrawRect = rect;
3908 OffsetRect(&infoPtr->marqueeDrawRect, offset.x, offset.y);
3910 iterator_frameditems_absolute(&new_elems, infoPtr, &infoPtr->marqueeRect);
3911 iterator_remove_common_items(&old_elems, &new_elems);
3913 /* Iterate over no longer selected items */
3914 while (iterator_next(&old_elems))
3916 if (old_elems.nItem > -1)
3918 if (LISTVIEW_GetItemState(infoPtr, old_elems.nItem, LVIS_SELECTED) == LVIS_SELECTED)
3919 item.state = 0;
3920 else
3921 item.state = LVIS_SELECTED;
3923 item.stateMask = LVIS_SELECTED;
3925 LISTVIEW_SetItemState(infoPtr, old_elems.nItem, &item);
3928 iterator_destroy(&old_elems);
3931 /* Iterate over newly selected items */
3932 if (GetKeyState(VK_CONTROL) & 0x8000)
3933 controlDown = TRUE;
3935 while (iterator_next(&new_elems))
3937 if (new_elems.nItem > -1)
3939 /* If CTRL is pressed, invert. If not, always select the item. */
3940 if ((controlDown) && (LISTVIEW_GetItemState(infoPtr, new_elems.nItem, LVIS_SELECTED)))
3941 item.state = 0;
3942 else
3943 item.state = LVIS_SELECTED;
3945 item.stateMask = LVIS_SELECTED;
3947 LISTVIEW_SetItemState(infoPtr, new_elems.nItem, &item);
3950 iterator_destroy(&new_elems);
3952 LISTVIEW_InvalidateRect(infoPtr, &infoPtr->marqueeDrawRect);
3955 /***
3956 * DESCRIPTION:
3957 * Called when we are in a marquee selection that involves scrolling the listview (ie,
3958 * the cursor is outside the bounds of the client area). This is a TIMERPROC.
3960 * PARAMETER(S):
3961 * [I] hwnd : Handle to the listview
3962 * [I] uMsg : WM_TIMER (ignored)
3963 * [I] idEvent : The timer ID interpreted as a pointer to a LISTVIEW_INFO struct
3964 * [I] dwTimer : The elapsed time (ignored)
3966 * RETURN:
3967 * None.
3969 static VOID CALLBACK LISTVIEW_ScrollTimer(HWND hWnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
3971 LISTVIEW_INFO *infoPtr;
3972 SCROLLINFO scrollInfo;
3973 POINT coords;
3974 INT scroll = 0;
3976 infoPtr = (LISTVIEW_INFO *) idEvent;
3978 if (!infoPtr)
3979 return;
3981 /* Get the current cursor position and convert to client coordinates */
3982 GetCursorPos(&coords);
3983 ScreenToClient(hWnd, &coords);
3985 scrollInfo.cbSize = sizeof(SCROLLINFO);
3986 scrollInfo.fMask = SIF_ALL;
3988 /* Work out in which directions we can scroll */
3989 if (GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
3991 if (scrollInfo.nPos != scrollInfo.nMin)
3992 scroll |= SCROLL_UP;
3994 if (((scrollInfo.nPage + scrollInfo.nPos) - 1) != scrollInfo.nMax)
3995 scroll |= SCROLL_DOWN;
3998 if (GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo))
4000 if (scrollInfo.nPos != scrollInfo.nMin)
4001 scroll |= SCROLL_LEFT;
4003 if (((scrollInfo.nPage + scrollInfo.nPos) - 1) != scrollInfo.nMax)
4004 scroll |= SCROLL_RIGHT;
4007 if (((coords.x <= 0) && (scroll & SCROLL_LEFT)) ||
4008 ((coords.y <= 0) && (scroll & SCROLL_UP)) ||
4009 ((coords.x >= infoPtr->rcList.right) && (scroll & SCROLL_RIGHT)) ||
4010 ((coords.y >= infoPtr->rcList.bottom) && (scroll & SCROLL_DOWN)))
4012 LISTVIEW_MarqueeHighlight(infoPtr, &coords, scroll);
4016 /***
4017 * DESCRIPTION:
4018 * Called whenever WM_MOUSEMOVE is received.
4020 * PARAMETER(S):
4021 * [I] infoPtr : valid pointer to the listview structure
4022 * [I] fwKeys : key indicator
4023 * [I] x,y : mouse position
4025 * RETURN:
4026 * 0 if the message is processed, non-zero if there was an error
4028 static LRESULT LISTVIEW_MouseMove(LISTVIEW_INFO *infoPtr, WORD fwKeys, INT x, INT y)
4030 LVHITTESTINFO ht;
4031 RECT rect;
4032 POINT pt;
4034 pt.x = x;
4035 pt.y = y;
4037 if (!(fwKeys & MK_LBUTTON))
4038 infoPtr->bLButtonDown = FALSE;
4040 if (infoPtr->bLButtonDown)
4042 rect.left = rect.right = infoPtr->ptClickPos.x;
4043 rect.top = rect.bottom = infoPtr->ptClickPos.y;
4045 InflateRect(&rect, GetSystemMetrics(SM_CXDRAG), GetSystemMetrics(SM_CYDRAG));
4047 if (infoPtr->bMarqueeSelect)
4049 /* Enable the timer if we're going outside our bounds, in case the user doesn't
4050 move the mouse again */
4052 if ((x <= 0) || (y <= 0) || (x >= infoPtr->rcList.right) ||
4053 (y >= infoPtr->rcList.bottom))
4055 if (!infoPtr->bScrolling)
4057 infoPtr->bScrolling = TRUE;
4058 SetTimer(infoPtr->hwndSelf, (UINT_PTR) infoPtr, 1, LISTVIEW_ScrollTimer);
4061 else
4063 infoPtr->bScrolling = FALSE;
4064 KillTimer(infoPtr->hwndSelf, (UINT_PTR) infoPtr);
4067 LISTVIEW_MarqueeHighlight(infoPtr, &pt, 0);
4068 return 0;
4071 ht.pt = pt;
4072 LISTVIEW_HitTest(infoPtr, &ht, TRUE, TRUE);
4074 /* reset item marker */
4075 if (infoPtr->nLButtonDownItem != ht.iItem)
4076 infoPtr->nLButtonDownItem = -1;
4078 if (!PtInRect(&rect, pt))
4080 /* this path covers the following:
4081 1. WM_LBUTTONDOWN over selected item (sets focus on it)
4082 2. change focus with keys
4083 3. move mouse over item from step 1 selects it and moves focus on it */
4084 if (infoPtr->nLButtonDownItem != -1 &&
4085 !LISTVIEW_GetItemState(infoPtr, infoPtr->nLButtonDownItem, LVIS_SELECTED))
4087 LVITEMW lvItem;
4089 lvItem.state = LVIS_FOCUSED | LVIS_SELECTED;
4090 lvItem.stateMask = LVIS_FOCUSED | LVIS_SELECTED;
4092 LISTVIEW_SetItemState(infoPtr, infoPtr->nLButtonDownItem, &lvItem);
4093 infoPtr->nLButtonDownItem = -1;
4096 if (!infoPtr->bDragging)
4098 ht.pt = infoPtr->ptClickPos;
4099 LISTVIEW_HitTest(infoPtr, &ht, TRUE, TRUE);
4101 /* If the click is outside the range of an item, begin a
4102 highlight. If not, begin an item drag. */
4103 if (ht.iItem == -1)
4105 NMHDR hdr;
4107 /* If we're allowing multiple selections, send notification.
4108 If return value is non-zero, cancel. */
4109 if (!(infoPtr->dwStyle & LVS_SINGLESEL) && (notify_hdr(infoPtr, LVN_MARQUEEBEGIN, &hdr) == 0))
4111 /* Store the absolute coordinates of the click */
4112 POINT offset;
4113 LISTVIEW_GetOrigin(infoPtr, &offset);
4115 infoPtr->marqueeOrigin.x = infoPtr->ptClickPos.x - offset.x;
4116 infoPtr->marqueeOrigin.y = infoPtr->ptClickPos.y - offset.y;
4118 /* Begin selection and capture mouse */
4119 infoPtr->bMarqueeSelect = TRUE;
4120 infoPtr->marqueeRect = rect;
4121 SetCapture(infoPtr->hwndSelf);
4124 else
4126 NMLISTVIEW nmlv;
4128 ZeroMemory(&nmlv, sizeof(nmlv));
4129 nmlv.iItem = ht.iItem;
4130 nmlv.ptAction = infoPtr->ptClickPos;
4132 notify_listview(infoPtr, LVN_BEGINDRAG, &nmlv);
4133 infoPtr->bDragging = TRUE;
4137 return 0;
4141 /* see if we are supposed to be tracking mouse hovering */
4142 if (LISTVIEW_IsHotTracking(infoPtr)) {
4143 TRACKMOUSEEVENT trackinfo;
4144 DWORD flags;
4146 trackinfo.cbSize = sizeof(TRACKMOUSEEVENT);
4147 trackinfo.dwFlags = TME_QUERY;
4149 /* see if we are already tracking this hwnd */
4150 _TrackMouseEvent(&trackinfo);
4152 flags = TME_LEAVE;
4153 if(infoPtr->dwLvExStyle & LVS_EX_TRACKSELECT)
4154 flags |= TME_HOVER;
4156 if((trackinfo.dwFlags & flags) != flags || trackinfo.hwndTrack != infoPtr->hwndSelf) {
4157 trackinfo.dwFlags = flags;
4158 trackinfo.dwHoverTime = infoPtr->dwHoverTime;
4159 trackinfo.hwndTrack = infoPtr->hwndSelf;
4161 /* call TRACKMOUSEEVENT so we receive WM_MOUSEHOVER messages */
4162 _TrackMouseEvent(&trackinfo);
4166 return 0;
4170 /***
4171 * Tests whether the item is assignable to a list with style lStyle
4173 static inline BOOL is_assignable_item(const LVITEMW *lpLVItem, LONG lStyle)
4175 if ( (lpLVItem->mask & LVIF_TEXT) &&
4176 (lpLVItem->pszText == LPSTR_TEXTCALLBACKW) &&
4177 (lStyle & (LVS_SORTASCENDING | LVS_SORTDESCENDING)) ) return FALSE;
4179 return TRUE;
4183 /***
4184 * DESCRIPTION:
4185 * Helper for LISTVIEW_SetItemT and LISTVIEW_InsertItemT: sets item attributes.
4187 * PARAMETER(S):
4188 * [I] infoPtr : valid pointer to the listview structure
4189 * [I] lpLVItem : valid pointer to new item attributes
4190 * [I] isNew : the item being set is being inserted
4191 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
4192 * [O] bChanged : will be set to TRUE if the item really changed
4194 * RETURN:
4195 * SUCCESS : TRUE
4196 * FAILURE : FALSE
4198 static BOOL set_main_item(LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem, BOOL isNew, BOOL isW, BOOL *bChanged)
4200 ITEM_INFO *lpItem;
4201 NMLISTVIEW nmlv;
4202 UINT uChanged = 0;
4203 LVITEMW item;
4204 /* stateMask is ignored for LVM_INSERTITEM */
4205 UINT stateMask = isNew ? ~0 : lpLVItem->stateMask;
4206 BOOL send_change_notification;
4208 TRACE("()\n");
4210 assert(lpLVItem->iItem >= 0 && lpLVItem->iItem < infoPtr->nItemCount);
4212 if (lpLVItem->mask == 0) return TRUE;
4214 if (infoPtr->dwStyle & LVS_OWNERDATA)
4216 /* a virtual listview only stores selection and focus */
4217 if (lpLVItem->mask & ~LVIF_STATE)
4218 return FALSE;
4219 lpItem = NULL;
4221 else
4223 HDPA hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, lpLVItem->iItem);
4224 lpItem = DPA_GetPtr(hdpaSubItems, 0);
4225 assert (lpItem);
4228 /* we need to get the lParam and state of the item */
4229 item.iItem = lpLVItem->iItem;
4230 item.iSubItem = lpLVItem->iSubItem;
4231 item.mask = LVIF_STATE | LVIF_PARAM;
4232 item.stateMask = (infoPtr->dwStyle & LVS_OWNERDATA) ? LVIS_FOCUSED | LVIS_SELECTED : ~0;
4234 item.state = 0;
4235 item.lParam = 0;
4236 if (!isNew && !LISTVIEW_GetItemW(infoPtr, &item)) return FALSE;
4238 TRACE("oldState=%x, newState=%x\n", item.state, lpLVItem->state);
4239 /* determine what fields will change */
4240 if ((lpLVItem->mask & LVIF_STATE) && ((item.state ^ lpLVItem->state) & stateMask & ~infoPtr->uCallbackMask))
4241 uChanged |= LVIF_STATE;
4243 if ((lpLVItem->mask & LVIF_IMAGE) && (lpItem->hdr.iImage != lpLVItem->iImage))
4244 uChanged |= LVIF_IMAGE;
4246 if ((lpLVItem->mask & LVIF_PARAM) && (lpItem->lParam != lpLVItem->lParam))
4247 uChanged |= LVIF_PARAM;
4249 if ((lpLVItem->mask & LVIF_INDENT) && (lpItem->iIndent != lpLVItem->iIndent))
4250 uChanged |= LVIF_INDENT;
4252 if ((lpLVItem->mask & LVIF_TEXT) && textcmpWT(lpItem->hdr.pszText, lpLVItem->pszText, isW))
4253 uChanged |= LVIF_TEXT;
4255 TRACE("change mask=0x%x\n", uChanged);
4257 memset(&nmlv, 0, sizeof(NMLISTVIEW));
4258 nmlv.iItem = lpLVItem->iItem;
4259 if (lpLVItem->mask & LVIF_STATE)
4261 nmlv.uNewState = (item.state & ~stateMask) | (lpLVItem->state & stateMask);
4262 nmlv.uOldState = item.state;
4264 nmlv.uChanged = isNew ? LVIF_STATE : (uChanged ? uChanged : lpLVItem->mask);
4265 nmlv.lParam = item.lParam;
4267 /* Send change notification if the item is not being inserted, or inserted (selected|focused),
4268 and we are _NOT_ virtual (LVS_OWNERDATA), and change notifications
4269 are enabled. Even if nothing really changed we still need to send this,
4270 in this case uChanged mask is just set to passed item mask. */
4272 send_change_notification = !isNew;
4273 send_change_notification |= (uChanged & LVIF_STATE) && (lpLVItem->state & (LVIS_FOCUSED | LVIS_SELECTED));
4274 send_change_notification &= !!(infoPtr->notify_mask & NOTIFY_MASK_ITEM_CHANGE);
4276 if (lpItem && send_change_notification)
4278 HWND hwndSelf = infoPtr->hwndSelf;
4280 if (notify_listview(infoPtr, LVN_ITEMCHANGING, &nmlv))
4281 return FALSE;
4282 if (!IsWindow(hwndSelf))
4283 return FALSE;
4286 /* When item is inserted we need to shift existing focus index if new item has lower index. */
4287 if (isNew && (stateMask & ~infoPtr->uCallbackMask & LVIS_FOCUSED) &&
4288 /* this means we won't hit a focus change path later */
4289 ((uChanged & LVIF_STATE) == 0 || (!(lpLVItem->state & LVIS_FOCUSED) && (infoPtr->nFocusedItem != lpLVItem->iItem))))
4291 if (infoPtr->nFocusedItem != -1 && (lpLVItem->iItem <= infoPtr->nFocusedItem))
4292 infoPtr->nFocusedItem++;
4295 if (!uChanged) return TRUE;
4296 *bChanged = TRUE;
4298 /* copy information */
4299 if (lpLVItem->mask & LVIF_TEXT)
4300 textsetptrT(&lpItem->hdr.pszText, lpLVItem->pszText, isW);
4302 if (lpLVItem->mask & LVIF_IMAGE)
4303 lpItem->hdr.iImage = lpLVItem->iImage;
4305 if (lpLVItem->mask & LVIF_PARAM)
4306 lpItem->lParam = lpLVItem->lParam;
4308 if (lpLVItem->mask & LVIF_INDENT)
4309 lpItem->iIndent = lpLVItem->iIndent;
4311 if (uChanged & LVIF_STATE)
4313 if (lpItem && (stateMask & ~infoPtr->uCallbackMask))
4315 lpItem->state &= ~stateMask;
4316 lpItem->state |= (lpLVItem->state & stateMask);
4318 if (lpLVItem->state & stateMask & ~infoPtr->uCallbackMask & LVIS_SELECTED)
4320 if (infoPtr->dwStyle & LVS_SINGLESEL) LISTVIEW_DeselectAllSkipItem(infoPtr, lpLVItem->iItem);
4321 ranges_additem(infoPtr->selectionRanges, lpLVItem->iItem);
4323 else if (stateMask & LVIS_SELECTED)
4325 ranges_delitem(infoPtr->selectionRanges, lpLVItem->iItem);
4327 /* If we are asked to change focus, and we manage it, do it.
4328 It's important to have all new item data stored at this point,
4329 because changing existing focus could result in a redrawing operation,
4330 which in turn could ask for disp data, application should see all data
4331 for inserted item when processing LVN_GETDISPINFO.
4333 The way this works application will see nested item change notifications -
4334 changed item notifications interrupted by ones from item losing focus. */
4335 if (stateMask & ~infoPtr->uCallbackMask & LVIS_FOCUSED)
4337 if (lpLVItem->state & LVIS_FOCUSED)
4339 /* update selection mark */
4340 if (infoPtr->nFocusedItem == -1 && infoPtr->nSelectionMark == -1)
4341 infoPtr->nSelectionMark = lpLVItem->iItem;
4343 if (infoPtr->nFocusedItem != -1)
4345 /* remove current focus */
4346 item.mask = LVIF_STATE;
4347 item.state = 0;
4348 item.stateMask = LVIS_FOCUSED;
4350 /* recurse with redrawing an item */
4351 LISTVIEW_SetItemState(infoPtr, infoPtr->nFocusedItem, &item);
4354 infoPtr->nFocusedItem = lpLVItem->iItem;
4355 LISTVIEW_EnsureVisible(infoPtr, lpLVItem->iItem, infoPtr->uView == LV_VIEW_LIST);
4357 else if (infoPtr->nFocusedItem == lpLVItem->iItem)
4359 infoPtr->nFocusedItem = -1;
4364 if (send_change_notification)
4366 if (lpLVItem->mask & LVIF_PARAM) nmlv.lParam = lpLVItem->lParam;
4367 notify_listview(infoPtr, LVN_ITEMCHANGED, &nmlv);
4370 return TRUE;
4373 /***
4374 * DESCRIPTION:
4375 * Helper for LISTVIEW_{Set,Insert}ItemT *only*: sets subitem attributes.
4377 * PARAMETER(S):
4378 * [I] infoPtr : valid pointer to the listview structure
4379 * [I] lpLVItem : valid pointer to new subitem attributes
4380 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
4381 * [O] bChanged : will be set to TRUE if the item really changed
4383 * RETURN:
4384 * SUCCESS : TRUE
4385 * FAILURE : FALSE
4387 static BOOL set_sub_item(const LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem, BOOL isW, BOOL *bChanged)
4389 HDPA hdpaSubItems;
4390 SUBITEM_INFO *lpSubItem;
4392 /* we do not support subitems for virtual listviews */
4393 if (infoPtr->dwStyle & LVS_OWNERDATA) return FALSE;
4395 /* set subitem only if column is present */
4396 if (lpLVItem->iSubItem >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
4398 /* First do some sanity checks */
4399 /* The LVIF_STATE flag is valid for subitems, but does not appear to be
4400 particularly useful. We currently do not actually do anything with
4401 the flag on subitems.
4403 if (lpLVItem->mask & ~(LVIF_TEXT | LVIF_IMAGE | LVIF_STATE | LVIF_DI_SETITEM)) return FALSE;
4404 if (!(lpLVItem->mask & (LVIF_TEXT | LVIF_IMAGE | LVIF_STATE))) return TRUE;
4406 /* get the subitem structure, and create it if not there */
4407 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, lpLVItem->iItem);
4408 assert (hdpaSubItems);
4410 lpSubItem = LISTVIEW_GetSubItemPtr(hdpaSubItems, lpLVItem->iSubItem);
4411 if (!lpSubItem)
4413 SUBITEM_INFO *tmpSubItem;
4414 INT i;
4416 lpSubItem = Alloc(sizeof(SUBITEM_INFO));
4417 if (!lpSubItem) return FALSE;
4418 /* we could binary search here, if need be...*/
4419 for (i = 1; i < DPA_GetPtrCount(hdpaSubItems); i++)
4421 tmpSubItem = DPA_GetPtr(hdpaSubItems, i);
4422 if (tmpSubItem->iSubItem > lpLVItem->iSubItem) break;
4424 if (DPA_InsertPtr(hdpaSubItems, i, lpSubItem) == -1)
4426 Free(lpSubItem);
4427 return FALSE;
4429 lpSubItem->iSubItem = lpLVItem->iSubItem;
4430 lpSubItem->hdr.iImage = I_IMAGECALLBACK;
4431 *bChanged = TRUE;
4434 if ((lpLVItem->mask & LVIF_IMAGE) && (lpSubItem->hdr.iImage != lpLVItem->iImage))
4436 lpSubItem->hdr.iImage = lpLVItem->iImage;
4437 *bChanged = TRUE;
4440 if ((lpLVItem->mask & LVIF_TEXT) && textcmpWT(lpSubItem->hdr.pszText, lpLVItem->pszText, isW))
4442 textsetptrT(&lpSubItem->hdr.pszText, lpLVItem->pszText, isW);
4443 *bChanged = TRUE;
4446 return TRUE;
4449 /***
4450 * DESCRIPTION:
4451 * Sets item attributes.
4453 * PARAMETER(S):
4454 * [I] infoPtr : valid pointer to the listview structure
4455 * [I] lpLVItem : new item attributes
4456 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
4458 * RETURN:
4459 * SUCCESS : TRUE
4460 * FAILURE : FALSE
4462 static BOOL LISTVIEW_SetItemT(LISTVIEW_INFO *infoPtr, LVITEMW *lpLVItem, BOOL isW)
4464 HWND hwndSelf = infoPtr->hwndSelf;
4465 LPWSTR pszText = NULL;
4466 BOOL bResult, bChanged = FALSE;
4467 RECT oldItemArea;
4469 TRACE("(lpLVItem=%s, isW=%d)\n", debuglvitem_t(lpLVItem, isW), isW);
4471 if (!lpLVItem || lpLVItem->iItem < 0 || lpLVItem->iItem >= infoPtr->nItemCount)
4472 return FALSE;
4474 /* Store old item area */
4475 LISTVIEW_GetItemBox(infoPtr, lpLVItem->iItem, &oldItemArea);
4477 /* For efficiency, we transform the lpLVItem->pszText to Unicode here */
4478 if ((lpLVItem->mask & LVIF_TEXT) && is_text(lpLVItem->pszText))
4480 pszText = lpLVItem->pszText;
4481 lpLVItem->pszText = textdupTtoW(lpLVItem->pszText, isW);
4484 /* actually set the fields */
4485 if (!is_assignable_item(lpLVItem, infoPtr->dwStyle)) return FALSE;
4487 if (lpLVItem->iSubItem)
4488 bResult = set_sub_item(infoPtr, lpLVItem, TRUE, &bChanged);
4489 else
4490 bResult = set_main_item(infoPtr, lpLVItem, FALSE, TRUE, &bChanged);
4491 if (!IsWindow(hwndSelf))
4492 return FALSE;
4494 /* redraw item, if necessary */
4495 if (bChanged && !infoPtr->bIsDrawing)
4497 /* this little optimization eliminates some nasty flicker */
4498 if ( infoPtr->uView == LV_VIEW_DETAILS && !(infoPtr->dwStyle & LVS_OWNERDRAWFIXED) &&
4499 !(infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) &&
4500 lpLVItem->iSubItem > 0 && lpLVItem->iSubItem <= DPA_GetPtrCount(infoPtr->hdpaColumns) )
4501 LISTVIEW_InvalidateSubItem(infoPtr, lpLVItem->iItem, lpLVItem->iSubItem);
4502 else
4504 LISTVIEW_InvalidateRect(infoPtr, &oldItemArea);
4505 LISTVIEW_InvalidateItem(infoPtr, lpLVItem->iItem);
4508 /* restore text */
4509 if (pszText)
4511 textfreeT(lpLVItem->pszText, isW);
4512 lpLVItem->pszText = pszText;
4515 return bResult;
4518 /***
4519 * DESCRIPTION:
4520 * Retrieves the index of the item at coordinate (0, 0) of the client area.
4522 * PARAMETER(S):
4523 * [I] infoPtr : valid pointer to the listview structure
4525 * RETURN:
4526 * item index
4528 static INT LISTVIEW_GetTopIndex(const LISTVIEW_INFO *infoPtr)
4530 INT nItem = 0;
4531 SCROLLINFO scrollInfo;
4533 scrollInfo.cbSize = sizeof(SCROLLINFO);
4534 scrollInfo.fMask = SIF_POS;
4536 if (infoPtr->uView == LV_VIEW_LIST)
4538 if (GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo))
4539 nItem = scrollInfo.nPos * LISTVIEW_GetCountPerColumn(infoPtr);
4541 else if (infoPtr->uView == LV_VIEW_DETAILS)
4543 if (GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
4544 nItem = scrollInfo.nPos;
4546 else
4548 if (GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
4549 nItem = LISTVIEW_GetCountPerRow(infoPtr) * (scrollInfo.nPos / infoPtr->nItemHeight);
4552 TRACE("nItem=%d\n", nItem);
4554 return nItem;
4557 static void LISTVIEW_DrawBackgroundBitmap(const LISTVIEW_INFO *infoPtr, HDC hdc, const RECT *lprcBox)
4559 HDC mem_hdc;
4561 if (!infoPtr->hBkBitmap)
4562 return;
4564 TRACE("(hdc=%p, lprcBox=%s, hBkBitmap=%p)\n", hdc, wine_dbgstr_rect(lprcBox), infoPtr->hBkBitmap);
4566 mem_hdc = CreateCompatibleDC(hdc);
4567 SelectObject(mem_hdc, infoPtr->hBkBitmap);
4568 BitBlt(hdc, lprcBox->left, lprcBox->top, lprcBox->right - lprcBox->left,
4569 lprcBox->bottom - lprcBox->top, mem_hdc, lprcBox->left, lprcBox->top, SRCCOPY);
4570 DeleteDC(mem_hdc);
4573 /***
4574 * DESCRIPTION:
4575 * Erases the background of the given rectangle
4577 * PARAMETER(S):
4578 * [I] infoPtr : valid pointer to the listview structure
4579 * [I] hdc : device context handle
4580 * [I] lprcBox : clipping rectangle
4582 * RETURN:
4583 * Success: TRUE
4584 * Failure: FALSE
4586 static BOOL LISTVIEW_FillBkgnd(const LISTVIEW_INFO *infoPtr, HDC hdc, const RECT *lprcBox)
4588 if (infoPtr->hBkBrush)
4590 TRACE("(hdc=%p, lprcBox=%s, hBkBrush=%p)\n", hdc, wine_dbgstr_rect(lprcBox), infoPtr->hBkBrush);
4592 FillRect(hdc, lprcBox, infoPtr->hBkBrush);
4595 LISTVIEW_DrawBackgroundBitmap(infoPtr, hdc, lprcBox);
4596 return TRUE;
4599 /* Draw main item or subitem */
4600 static void LISTVIEW_DrawItemPart(LISTVIEW_INFO *infoPtr, LVITEMW *item, const NMLVCUSTOMDRAW *nmlvcd, const POINT *pos)
4602 RECT rcSelect, rcLabel, rcBox, rcStateIcon, rcIcon;
4603 const RECT *background;
4604 HIMAGELIST himl;
4605 UINT format;
4606 RECT *focus;
4608 /* now check if we need to update the focus rectangle */
4609 focus = infoPtr->bFocus && (item->state & LVIS_FOCUSED) ? &infoPtr->rcFocus : 0;
4610 if (!focus) item->state &= ~LVIS_FOCUSED;
4612 LISTVIEW_GetItemMetrics(infoPtr, item, &rcBox, &rcSelect, &rcIcon, &rcStateIcon, &rcLabel);
4613 OffsetRect(&rcBox, pos->x, pos->y);
4614 OffsetRect(&rcSelect, pos->x, pos->y);
4615 OffsetRect(&rcIcon, pos->x, pos->y);
4616 OffsetRect(&rcStateIcon, pos->x, pos->y);
4617 OffsetRect(&rcLabel, pos->x, pos->y);
4618 TRACE("%d: box=%s, select=%s, icon=%s. label=%s\n", item->iSubItem,
4619 wine_dbgstr_rect(&rcBox), wine_dbgstr_rect(&rcSelect),
4620 wine_dbgstr_rect(&rcIcon), wine_dbgstr_rect(&rcLabel));
4622 /* FIXME: temporary hack */
4623 rcSelect.left = rcLabel.left;
4625 if (infoPtr->uView == LV_VIEW_DETAILS && item->iSubItem == 0)
4627 if (!(infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT))
4628 OffsetRect(&rcSelect, LISTVIEW_GetColumnInfo(infoPtr, 0)->rcHeader.left, 0);
4629 OffsetRect(&rcIcon, LISTVIEW_GetColumnInfo(infoPtr, 0)->rcHeader.left, 0);
4630 OffsetRect(&rcStateIcon, LISTVIEW_GetColumnInfo(infoPtr, 0)->rcHeader.left, 0);
4631 OffsetRect(&rcLabel, LISTVIEW_GetColumnInfo(infoPtr, 0)->rcHeader.left, 0);
4634 /* in icon mode, the label rect is really what we want to draw the
4635 * background for */
4636 /* in detail mode, we want to paint background for label rect when
4637 * item is not selected or listview has full row select; otherwise paint
4638 * background for text only */
4639 if ( infoPtr->uView == LV_VIEW_ICON ||
4640 (infoPtr->uView == LV_VIEW_DETAILS && (!(item->state & LVIS_SELECTED) ||
4641 (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT))))
4642 background = &rcLabel;
4643 else
4644 background = &rcSelect;
4646 if (nmlvcd->clrTextBk != CLR_NONE)
4647 ExtTextOutW(nmlvcd->nmcd.hdc, background->left, background->top, ETO_OPAQUE, background, NULL, 0, NULL);
4649 if (item->state & LVIS_FOCUSED)
4651 if (infoPtr->uView == LV_VIEW_DETAILS)
4653 if (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT)
4655 /* we have to update left focus bound too if item isn't in leftmost column
4656 and reduce right box bound */
4657 if (DPA_GetPtrCount(infoPtr->hdpaColumns) > 0)
4659 INT leftmost;
4661 if ((leftmost = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, 0, 0)))
4663 INT Originx = pos->x - LISTVIEW_GetColumnInfo(infoPtr, leftmost)->rcHeader.left;
4664 INT rightmost = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX,
4665 DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, 0);
4667 rcBox.right = LISTVIEW_GetColumnInfo(infoPtr, rightmost)->rcHeader.right + Originx;
4668 rcSelect.left = LISTVIEW_GetColumnInfo(infoPtr, leftmost)->rcHeader.left + Originx;
4671 rcSelect.right = rcBox.right;
4673 infoPtr->rcFocus = rcSelect;
4675 else
4676 infoPtr->rcFocus = rcLabel;
4679 /* state icons */
4680 if (infoPtr->himlState && STATEIMAGEINDEX(item->state) && (item->iSubItem == 0))
4682 UINT stateimage = STATEIMAGEINDEX(item->state);
4683 if (stateimage)
4685 TRACE("stateimage=%d\n", stateimage);
4686 ImageList_Draw(infoPtr->himlState, stateimage-1, nmlvcd->nmcd.hdc, rcStateIcon.left, rcStateIcon.top, ILD_NORMAL);
4690 /* item icons */
4691 himl = (infoPtr->uView == LV_VIEW_ICON ? infoPtr->himlNormal : infoPtr->himlSmall);
4692 if (himl && item->iImage >= 0 && !IsRectEmpty(&rcIcon))
4694 UINT style;
4696 TRACE("iImage=%d\n", item->iImage);
4698 if (item->state & (LVIS_SELECTED | LVIS_CUT) && infoPtr->bFocus)
4699 style = ILD_SELECTED;
4700 else
4701 style = ILD_NORMAL;
4703 ImageList_DrawEx(himl, item->iImage, nmlvcd->nmcd.hdc, rcIcon.left, rcIcon.top,
4704 rcIcon.right - rcIcon.left, rcIcon.bottom - rcIcon.top, infoPtr->clrBk,
4705 item->state & LVIS_CUT ? RGB(255, 255, 255) : CLR_DEFAULT,
4706 style | (item->state & LVIS_OVERLAYMASK));
4709 /* Don't bother painting item being edited */
4710 if (infoPtr->hwndEdit && item->iItem == infoPtr->nEditLabelItem && item->iSubItem == 0) return;
4712 /* figure out the text drawing flags */
4713 format = (infoPtr->uView == LV_VIEW_ICON ? (focus ? LV_FL_DT_FLAGS : LV_ML_DT_FLAGS) : LV_SL_DT_FLAGS);
4714 if (infoPtr->uView == LV_VIEW_ICON)
4715 format = (focus ? LV_FL_DT_FLAGS : LV_ML_DT_FLAGS);
4716 else if (item->iSubItem)
4718 switch (LISTVIEW_GetColumnInfo(infoPtr, item->iSubItem)->fmt & LVCFMT_JUSTIFYMASK)
4720 case LVCFMT_RIGHT: format |= DT_RIGHT; break;
4721 case LVCFMT_CENTER: format |= DT_CENTER; break;
4722 default: format |= DT_LEFT;
4725 if (!(format & (DT_RIGHT | DT_CENTER)))
4727 if (himl && item->iImage >= 0 && !IsRectEmpty(&rcIcon)) rcLabel.left += IMAGE_PADDING;
4728 else rcLabel.left += LABEL_HOR_PADDING;
4730 else if (format & DT_RIGHT) rcLabel.right -= LABEL_HOR_PADDING;
4732 /* for GRIDLINES reduce the bottom so the text formats correctly */
4733 if (infoPtr->uView == LV_VIEW_DETAILS && infoPtr->dwLvExStyle & LVS_EX_GRIDLINES)
4734 rcLabel.bottom--;
4736 DrawTextW(nmlvcd->nmcd.hdc, item->pszText, -1, &rcLabel, format);
4739 /***
4740 * DESCRIPTION:
4741 * Draws an item.
4743 * PARAMETER(S):
4744 * [I] infoPtr : valid pointer to the listview structure
4745 * [I] hdc : device context handle
4746 * [I] nItem : item index
4747 * [I] nSubItem : subitem index
4748 * [I] pos : item position in client coordinates
4749 * [I] cdmode : custom draw mode
4751 * RETURN:
4752 * Success: TRUE
4753 * Failure: FALSE
4755 static BOOL LISTVIEW_DrawItem(LISTVIEW_INFO *infoPtr, HDC hdc, INT nItem, ITERATOR *subitems, POINT pos, DWORD cdmode)
4757 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
4758 static WCHAR callbackW[] = L"(callback)";
4759 DWORD cdsubitemmode = CDRF_DODEFAULT;
4760 RECT *focus, rcBox;
4761 NMLVCUSTOMDRAW nmlvcd;
4762 LVITEMW lvItem;
4764 TRACE("(hdc=%p, nItem=%d, subitems=%p, pos=%s)\n", hdc, nItem, subitems, wine_dbgstr_point(&pos));
4766 /* get information needed for drawing the item */
4767 lvItem.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_PARAM | LVIF_STATE;
4768 if (infoPtr->uView == LV_VIEW_DETAILS) lvItem.mask |= LVIF_INDENT;
4769 lvItem.stateMask = LVIS_SELECTED | LVIS_FOCUSED | LVIS_STATEIMAGEMASK | LVIS_CUT | LVIS_OVERLAYMASK;
4770 lvItem.iItem = nItem;
4771 lvItem.iSubItem = 0;
4772 lvItem.state = 0;
4773 lvItem.lParam = 0;
4774 lvItem.cchTextMax = DISP_TEXT_SIZE;
4775 lvItem.pszText = szDispText;
4776 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return FALSE;
4777 if (lvItem.pszText == LPSTR_TEXTCALLBACKW) lvItem.pszText = callbackW;
4778 TRACE(" lvItem=%s\n", debuglvitem_t(&lvItem, TRUE));
4780 /* now check if we need to update the focus rectangle */
4781 focus = infoPtr->bFocus && (lvItem.state & LVIS_FOCUSED) ? &infoPtr->rcFocus : 0;
4782 if (!focus) lvItem.state &= ~LVIS_FOCUSED;
4784 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, &rcBox, NULL, NULL, NULL, NULL);
4785 OffsetRect(&rcBox, pos.x, pos.y);
4787 /* Full custom draw stage sequence looks like this:
4789 LV_VIEW_DETAILS:
4791 - CDDS_ITEMPREPAINT
4792 - CDDS_ITEMPREPAINT|CDDS_SUBITEM | => sent n times, where n is number of subitems,
4793 CDDS_ITEMPOSTPAINT|CDDS_SUBITEM | including item itself
4794 - CDDS_ITEMPOSTPAINT
4796 other styles:
4798 - CDDS_ITEMPREPAINT
4799 - CDDS_ITEMPOSTPAINT
4802 /* fill in the custom draw structure */
4803 customdraw_fill(&nmlvcd, infoPtr, hdc, &rcBox, &lvItem);
4804 if (cdmode & CDRF_NOTIFYITEMDRAW)
4805 cdsubitemmode = notify_customdraw(infoPtr, CDDS_ITEMPREPAINT, &nmlvcd);
4806 if (cdsubitemmode & CDRF_SKIPDEFAULT) goto postpaint;
4808 if (subitems)
4810 while (iterator_next(subitems))
4812 DWORD subitemstage = CDRF_DODEFAULT;
4814 /* We need to query for each subitem, item's data (subitem == 0) is already here at this point */
4815 if (subitems->nItem)
4817 lvItem.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_PARAM | LVIF_INDENT;
4818 lvItem.stateMask = LVIS_SELECTED | LVIS_FOCUSED | LVIS_STATEIMAGEMASK | LVIS_CUT | LVIS_OVERLAYMASK;
4819 lvItem.iItem = nItem;
4820 lvItem.iSubItem = subitems->nItem;
4821 lvItem.state = 0;
4822 lvItem.lParam = 0;
4823 lvItem.cchTextMax = DISP_TEXT_SIZE;
4824 lvItem.pszText = szDispText;
4825 szDispText[0] = 0;
4826 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return FALSE;
4827 if (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT)
4828 lvItem.state = LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED);
4829 if (lvItem.pszText == LPSTR_TEXTCALLBACKW) lvItem.pszText = callbackW;
4830 TRACE(" lvItem=%s\n", debuglvitem_t(&lvItem, TRUE));
4832 /* update custom draw data */
4833 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, &nmlvcd.nmcd.rc, NULL, NULL, NULL, NULL);
4834 OffsetRect(&nmlvcd.nmcd.rc, pos.x, pos.y);
4835 nmlvcd.iSubItem = subitems->nItem;
4838 if (cdsubitemmode & CDRF_NOTIFYSUBITEMDRAW)
4839 subitemstage = notify_customdraw(infoPtr, CDDS_SUBITEM | CDDS_ITEMPREPAINT, &nmlvcd);
4841 if (subitems->nItem == 0 || (cdmode & CDRF_NOTIFYITEMDRAW))
4842 prepaint_setup(infoPtr, hdc, &nmlvcd, FALSE);
4843 else if (!(infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT))
4844 prepaint_setup(infoPtr, hdc, &nmlvcd, TRUE);
4846 if (!(subitemstage & CDRF_SKIPDEFAULT))
4847 LISTVIEW_DrawItemPart(infoPtr, &lvItem, &nmlvcd, &pos);
4849 if (subitemstage & CDRF_NOTIFYPOSTPAINT)
4850 subitemstage = notify_customdraw(infoPtr, CDDS_SUBITEM | CDDS_ITEMPOSTPAINT, &nmlvcd);
4853 else
4855 prepaint_setup(infoPtr, hdc, &nmlvcd, FALSE);
4856 LISTVIEW_DrawItemPart(infoPtr, &lvItem, &nmlvcd, &pos);
4859 postpaint:
4860 if (cdsubitemmode & CDRF_NOTIFYPOSTPAINT)
4862 nmlvcd.iSubItem = 0;
4863 notify_customdraw(infoPtr, CDDS_ITEMPOSTPAINT, &nmlvcd);
4866 return TRUE;
4869 /***
4870 * DESCRIPTION:
4871 * Draws listview items when in owner draw mode.
4873 * PARAMETER(S):
4874 * [I] infoPtr : valid pointer to the listview structure
4875 * [I] hdc : device context handle
4877 * RETURN:
4878 * None
4880 static void LISTVIEW_RefreshOwnerDraw(const LISTVIEW_INFO *infoPtr, ITERATOR *i, HDC hdc, DWORD cdmode)
4882 UINT uID = (UINT)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
4883 DWORD cditemmode = CDRF_DODEFAULT;
4884 NMLVCUSTOMDRAW nmlvcd;
4885 POINT Origin, Position;
4886 DRAWITEMSTRUCT dis;
4887 LVITEMW item;
4889 TRACE("()\n");
4891 ZeroMemory(&dis, sizeof(dis));
4893 /* Get scroll info once before loop */
4894 LISTVIEW_GetOrigin(infoPtr, &Origin);
4896 /* iterate through the invalidated rows */
4897 while(iterator_next(i))
4899 item.iItem = i->nItem;
4900 item.iSubItem = 0;
4901 item.mask = LVIF_PARAM | LVIF_STATE;
4902 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
4903 if (!LISTVIEW_GetItemW(infoPtr, &item)) continue;
4905 dis.CtlType = ODT_LISTVIEW;
4906 dis.CtlID = uID;
4907 dis.itemID = item.iItem;
4908 dis.itemAction = ODA_DRAWENTIRE;
4909 dis.itemState = 0;
4910 if (item.state & LVIS_SELECTED) dis.itemState |= ODS_SELECTED;
4911 if (infoPtr->bFocus && (item.state & LVIS_FOCUSED)) dis.itemState |= ODS_FOCUS;
4912 dis.hwndItem = infoPtr->hwndSelf;
4913 dis.hDC = hdc;
4914 LISTVIEW_GetItemOrigin(infoPtr, dis.itemID, &Position);
4915 dis.rcItem.left = Position.x + Origin.x;
4916 dis.rcItem.right = dis.rcItem.left + infoPtr->nItemWidth;
4917 dis.rcItem.top = Position.y + Origin.y;
4918 dis.rcItem.bottom = dis.rcItem.top + infoPtr->nItemHeight;
4919 dis.itemData = item.lParam;
4921 TRACE("item=%s, rcItem=%s\n", debuglvitem_t(&item, TRUE), wine_dbgstr_rect(&dis.rcItem));
4924 * Even if we do not send the CDRF_NOTIFYITEMDRAW we need to fill the nmlvcd
4925 * structure for the rest. of the paint cycle
4927 customdraw_fill(&nmlvcd, infoPtr, hdc, &dis.rcItem, &item);
4928 if (cdmode & CDRF_NOTIFYITEMDRAW)
4929 cditemmode = notify_customdraw(infoPtr, CDDS_PREPAINT, &nmlvcd);
4931 if (!(cditemmode & CDRF_SKIPDEFAULT))
4933 prepaint_setup (infoPtr, hdc, &nmlvcd, FALSE);
4934 SendMessageW(infoPtr->hwndNotify, WM_DRAWITEM, dis.CtlID, (LPARAM)&dis);
4937 if (cditemmode & CDRF_NOTIFYPOSTPAINT)
4938 notify_postpaint(infoPtr, &nmlvcd);
4942 /***
4943 * DESCRIPTION:
4944 * Draws listview items when in report display mode.
4946 * PARAMETER(S):
4947 * [I] infoPtr : valid pointer to the listview structure
4948 * [I] hdc : device context handle
4949 * [I] cdmode : custom draw mode
4951 * RETURN:
4952 * None
4954 static void LISTVIEW_RefreshReport(LISTVIEW_INFO *infoPtr, ITERATOR *i, HDC hdc, DWORD cdmode)
4956 INT rgntype;
4957 RECT rcClip, rcItem;
4958 POINT Origin;
4959 RANGES colRanges;
4960 INT col;
4961 ITERATOR j;
4963 TRACE("()\n");
4965 /* figure out what to draw */
4966 rgntype = GetClipBox(hdc, &rcClip);
4967 if (rgntype == NULLREGION) return;
4969 /* Get scroll info once before loop */
4970 LISTVIEW_GetOrigin(infoPtr, &Origin);
4972 colRanges = ranges_create(DPA_GetPtrCount(infoPtr->hdpaColumns));
4974 /* narrow down the columns we need to paint */
4975 for(col = 0; col < DPA_GetPtrCount(infoPtr->hdpaColumns); col++)
4977 INT index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, col, 0);
4979 LISTVIEW_GetHeaderRect(infoPtr, index, &rcItem);
4980 if ((rcItem.right + Origin.x >= rcClip.left) && (rcItem.left + Origin.x < rcClip.right))
4981 ranges_additem(colRanges, index);
4983 iterator_rangesitems(&j, colRanges);
4985 /* in full row select, we _have_ to draw the main item */
4986 if (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT)
4987 j.nSpecial = 0;
4989 /* iterate through the invalidated rows */
4990 while(iterator_next(i))
4992 RANGES subitems;
4993 POINT Position;
4994 ITERATOR k;
4996 SelectObject(hdc, infoPtr->hFont);
4997 LISTVIEW_GetItemOrigin(infoPtr, i->nItem, &Position);
4998 Position.x = Origin.x;
4999 Position.y += Origin.y;
5001 subitems = ranges_create(DPA_GetPtrCount(infoPtr->hdpaColumns));
5003 /* iterate through the invalidated columns */
5004 while(iterator_next(&j))
5006 LISTVIEW_GetHeaderRect(infoPtr, j.nItem, &rcItem);
5008 if (rgntype == COMPLEXREGION && !((infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) && j.nItem == 0))
5010 rcItem.top = 0;
5011 rcItem.bottom = infoPtr->nItemHeight;
5012 OffsetRect(&rcItem, Origin.x, Position.y);
5013 if (!RectVisible(hdc, &rcItem)) continue;
5016 ranges_additem(subitems, j.nItem);
5019 iterator_rangesitems(&k, subitems);
5020 LISTVIEW_DrawItem(infoPtr, hdc, i->nItem, &k, Position, cdmode);
5021 iterator_destroy(&k);
5023 iterator_destroy(&j);
5026 /***
5027 * DESCRIPTION:
5028 * Draws the gridlines if necessary when in report display mode.
5030 * PARAMETER(S):
5031 * [I] infoPtr : valid pointer to the listview structure
5032 * [I] hdc : device context handle
5034 * RETURN:
5035 * None
5037 static void LISTVIEW_RefreshReportGrid(LISTVIEW_INFO *infoPtr, HDC hdc)
5039 INT rgntype;
5040 INT y, itemheight;
5041 INT col, index;
5042 HPEN hPen, hOldPen;
5043 RECT rcClip, rcItem = {0};
5044 POINT Origin;
5045 RANGES colRanges;
5046 ITERATOR j;
5047 BOOL rmost = FALSE;
5049 TRACE("()\n");
5051 /* figure out what to draw */
5052 rgntype = GetClipBox(hdc, &rcClip);
5053 if (rgntype == NULLREGION) return;
5055 /* Get scroll info once before loop */
5056 LISTVIEW_GetOrigin(infoPtr, &Origin);
5058 colRanges = ranges_create(DPA_GetPtrCount(infoPtr->hdpaColumns));
5060 /* narrow down the columns we need to paint */
5061 for(col = 0; col < DPA_GetPtrCount(infoPtr->hdpaColumns); col++)
5063 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, col, 0);
5065 LISTVIEW_GetHeaderRect(infoPtr, index, &rcItem);
5066 if ((rcItem.right + Origin.x >= rcClip.left) && (rcItem.left + Origin.x < rcClip.right))
5067 ranges_additem(colRanges, index);
5070 /* is right most vertical line visible? */
5071 if (DPA_GetPtrCount(infoPtr->hdpaColumns) > 0)
5073 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, 0);
5074 LISTVIEW_GetHeaderRect(infoPtr, index, &rcItem);
5075 rmost = (rcItem.right + Origin.x < rcClip.right);
5078 if ((hPen = CreatePen( PS_SOLID, 1, comctl32_color.clr3dFace )))
5080 hOldPen = SelectObject ( hdc, hPen );
5082 /* draw the vertical lines for the columns */
5083 iterator_rangesitems(&j, colRanges);
5084 while(iterator_next(&j))
5086 LISTVIEW_GetHeaderRect(infoPtr, j.nItem, &rcItem);
5087 if (rcItem.left == 0) continue; /* skip leftmost column */
5088 rcItem.left += Origin.x;
5089 rcItem.right += Origin.x;
5090 rcItem.top = infoPtr->rcList.top;
5091 rcItem.bottom = infoPtr->rcList.bottom;
5092 TRACE("vert col=%d, rcItem=%s\n", j.nItem, wine_dbgstr_rect(&rcItem));
5093 MoveToEx (hdc, rcItem.left, rcItem.top, NULL);
5094 LineTo (hdc, rcItem.left, rcItem.bottom);
5096 iterator_destroy(&j);
5097 /* draw rightmost grid line if visible */
5098 if (rmost)
5100 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX,
5101 DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, 0);
5102 LISTVIEW_GetHeaderRect(infoPtr, index, &rcItem);
5104 rcItem.right += Origin.x;
5106 MoveToEx (hdc, rcItem.right, infoPtr->rcList.top, NULL);
5107 LineTo (hdc, rcItem.right, infoPtr->rcList.bottom);
5110 /* draw the horizontal lines for the rows */
5111 itemheight = LISTVIEW_CalculateItemHeight(infoPtr);
5112 rcItem.left = infoPtr->rcList.left;
5113 rcItem.right = infoPtr->rcList.right;
5114 for(y = Origin.y > 1 ? Origin.y - 1 : itemheight - 1 + Origin.y % itemheight; y<=infoPtr->rcList.bottom; y+=itemheight)
5116 rcItem.bottom = rcItem.top = y;
5117 TRACE("horz rcItem=%s\n", wine_dbgstr_rect(&rcItem));
5118 MoveToEx (hdc, rcItem.left, rcItem.top, NULL);
5119 LineTo (hdc, rcItem.right, rcItem.top);
5122 SelectObject( hdc, hOldPen );
5123 DeleteObject( hPen );
5125 else
5126 ranges_destroy(colRanges);
5129 /***
5130 * DESCRIPTION:
5131 * Draws listview items when in list display mode.
5133 * PARAMETER(S):
5134 * [I] infoPtr : valid pointer to the listview structure
5135 * [I] hdc : device context handle
5136 * [I] cdmode : custom draw mode
5138 * RETURN:
5139 * None
5141 static void LISTVIEW_RefreshList(LISTVIEW_INFO *infoPtr, ITERATOR *i, HDC hdc, DWORD cdmode)
5143 POINT Origin, Position;
5145 /* Get scroll info once before loop */
5146 LISTVIEW_GetOrigin(infoPtr, &Origin);
5148 while(iterator_prev(i))
5150 SelectObject(hdc, infoPtr->hFont);
5151 LISTVIEW_GetItemOrigin(infoPtr, i->nItem, &Position);
5152 Position.x += Origin.x;
5153 Position.y += Origin.y;
5155 LISTVIEW_DrawItem(infoPtr, hdc, i->nItem, NULL, Position, cdmode);
5160 /***
5161 * DESCRIPTION:
5162 * Draws listview items.
5164 * PARAMETER(S):
5165 * [I] infoPtr : valid pointer to the listview structure
5166 * [I] hdc : device context handle
5167 * [I] prcErase : rect to be erased before refresh (may be NULL)
5169 * RETURN:
5170 * NoneX
5172 static void LISTVIEW_Refresh(LISTVIEW_INFO *infoPtr, HDC hdc, const RECT *prcErase)
5174 COLORREF oldTextColor = 0, oldBkColor = 0;
5175 NMLVCUSTOMDRAW nmlvcd;
5176 HFONT hOldFont = 0;
5177 DWORD cdmode;
5178 INT oldBkMode = 0;
5179 RECT rcClient;
5180 ITERATOR i;
5181 HDC hdcOrig = hdc;
5182 HBITMAP hbmp = NULL;
5183 RANGE range;
5185 LISTVIEW_DUMP(infoPtr);
5187 if (infoPtr->dwLvExStyle & LVS_EX_DOUBLEBUFFER) {
5188 TRACE("double buffering\n");
5190 hdc = CreateCompatibleDC(hdcOrig);
5191 if (!hdc) {
5192 ERR("Failed to create DC for backbuffer\n");
5193 return;
5195 hbmp = CreateCompatibleBitmap(hdcOrig, infoPtr->rcList.right,
5196 infoPtr->rcList.bottom);
5197 if (!hbmp) {
5198 ERR("Failed to create bitmap for backbuffer\n");
5199 DeleteDC(hdc);
5200 return;
5203 SelectObject(hdc, hbmp);
5204 SelectObject(hdc, infoPtr->hFont);
5206 if(GetClipBox(hdcOrig, &rcClient))
5207 IntersectClipRect(hdc, rcClient.left, rcClient.top, rcClient.right, rcClient.bottom);
5208 } else {
5209 /* Save dc values we're gonna trash while drawing
5210 * FIXME: Should be done in LISTVIEW_DrawItem() */
5211 hOldFont = SelectObject(hdc, infoPtr->hFont);
5212 oldBkMode = GetBkMode(hdc);
5213 oldBkColor = GetBkColor(hdc);
5214 oldTextColor = GetTextColor(hdc);
5217 infoPtr->bIsDrawing = TRUE;
5219 if (prcErase) {
5220 LISTVIEW_FillBkgnd(infoPtr, hdc, prcErase);
5221 } else if (infoPtr->dwLvExStyle & LVS_EX_DOUBLEBUFFER) {
5222 /* If no erasing was done (usually because RedrawWindow was called
5223 * with RDW_INVALIDATE only) we need to copy the old contents into
5224 * the backbuffer before continuing. */
5225 BitBlt(hdc, infoPtr->rcList.left, infoPtr->rcList.top,
5226 infoPtr->rcList.right - infoPtr->rcList.left,
5227 infoPtr->rcList.bottom - infoPtr->rcList.top,
5228 hdcOrig, infoPtr->rcList.left, infoPtr->rcList.top, SRCCOPY);
5231 GetClientRect(infoPtr->hwndSelf, &rcClient);
5232 customdraw_fill(&nmlvcd, infoPtr, hdc, &rcClient, 0);
5233 cdmode = notify_customdraw(infoPtr, CDDS_PREPAINT, &nmlvcd);
5234 if (cdmode & CDRF_SKIPDEFAULT) goto enddraw;
5236 /* nothing to draw */
5237 if(infoPtr->nItemCount == 0) goto enddraw;
5239 /* figure out what we need to draw */
5240 iterator_visibleitems(&i, infoPtr, hdc);
5241 range = iterator_range(&i);
5243 /* send cache hint notification */
5244 if (infoPtr->dwStyle & LVS_OWNERDATA)
5246 NMLVCACHEHINT nmlv;
5248 ZeroMemory(&nmlv, sizeof(NMLVCACHEHINT));
5249 nmlv.iFrom = range.lower;
5250 nmlv.iTo = range.upper - 1;
5251 notify_hdr(infoPtr, LVN_ODCACHEHINT, &nmlv.hdr);
5254 if ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (infoPtr->uView == LV_VIEW_DETAILS))
5255 LISTVIEW_RefreshOwnerDraw(infoPtr, &i, hdc, cdmode);
5256 else
5258 if (infoPtr->uView == LV_VIEW_DETAILS)
5259 LISTVIEW_RefreshReport(infoPtr, &i, hdc, cdmode);
5260 else /* LV_VIEW_LIST, LV_VIEW_ICON or LV_VIEW_SMALLICON */
5261 LISTVIEW_RefreshList(infoPtr, &i, hdc, cdmode);
5263 /* if we have a focus rect and it's visible, draw it */
5264 if (infoPtr->bFocus && range.lower <= infoPtr->nFocusedItem &&
5265 (range.upper - 1) >= infoPtr->nFocusedItem)
5266 LISTVIEW_DrawFocusRect(infoPtr, hdc);
5268 iterator_destroy(&i);
5270 enddraw:
5271 /* For LVS_EX_GRIDLINES go and draw lines */
5272 /* This includes the case where there were *no* items */
5273 if ((infoPtr->uView == LV_VIEW_DETAILS) && infoPtr->dwLvExStyle & LVS_EX_GRIDLINES)
5274 LISTVIEW_RefreshReportGrid(infoPtr, hdc);
5276 /* Draw marquee rectangle if appropriate */
5277 if (infoPtr->bMarqueeSelect)
5278 DrawFocusRect(hdc, &infoPtr->marqueeDrawRect);
5280 if (cdmode & CDRF_NOTIFYPOSTPAINT)
5281 notify_postpaint(infoPtr, &nmlvcd);
5283 if(hbmp) {
5284 BitBlt(hdcOrig, infoPtr->rcList.left, infoPtr->rcList.top,
5285 infoPtr->rcList.right - infoPtr->rcList.left,
5286 infoPtr->rcList.bottom - infoPtr->rcList.top,
5287 hdc, infoPtr->rcList.left, infoPtr->rcList.top, SRCCOPY);
5289 DeleteObject(hbmp);
5290 DeleteDC(hdc);
5291 } else {
5292 SelectObject(hdc, hOldFont);
5293 SetBkMode(hdc, oldBkMode);
5294 SetBkColor(hdc, oldBkColor);
5295 SetTextColor(hdc, oldTextColor);
5298 infoPtr->bIsDrawing = FALSE;
5302 /***
5303 * DESCRIPTION:
5304 * Calculates the approximate width and height of a given number of items.
5306 * PARAMETER(S):
5307 * [I] infoPtr : valid pointer to the listview structure
5308 * [I] nItemCount : number of items
5309 * [I] wWidth : width
5310 * [I] wHeight : height
5312 * RETURN:
5313 * Returns a DWORD. The width in the low word and the height in high word.
5315 static DWORD LISTVIEW_ApproximateViewRect(const LISTVIEW_INFO *infoPtr, INT nItemCount,
5316 WORD wWidth, WORD wHeight)
5318 DWORD dwViewRect = 0;
5320 if (nItemCount == -1)
5321 nItemCount = infoPtr->nItemCount;
5323 if (infoPtr->uView == LV_VIEW_LIST)
5325 INT nItemCountPerColumn = 1;
5326 INT nColumnCount = 0;
5328 if (wHeight == 0xFFFF)
5330 /* use current height */
5331 wHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
5334 if (wHeight < infoPtr->nItemHeight)
5335 wHeight = infoPtr->nItemHeight;
5337 if (nItemCount > 0)
5339 if (infoPtr->nItemHeight > 0)
5341 nItemCountPerColumn = wHeight / infoPtr->nItemHeight;
5342 if (nItemCountPerColumn == 0)
5343 nItemCountPerColumn = 1;
5345 if (nItemCount % nItemCountPerColumn != 0)
5346 nColumnCount = nItemCount / nItemCountPerColumn;
5347 else
5348 nColumnCount = nItemCount / nItemCountPerColumn + 1;
5352 /* Microsoft padding magic */
5353 wHeight = nItemCountPerColumn * infoPtr->nItemHeight + 2;
5354 wWidth = nColumnCount * infoPtr->nItemWidth + 2;
5356 dwViewRect = MAKELONG(wWidth, wHeight);
5358 else if (infoPtr->uView == LV_VIEW_DETAILS)
5360 RECT rcBox;
5362 if (infoPtr->nItemCount > 0)
5364 LISTVIEW_GetItemBox(infoPtr, 0, &rcBox);
5365 wWidth = rcBox.right - rcBox.left;
5366 wHeight = (rcBox.bottom - rcBox.top) * nItemCount;
5368 else
5370 /* use current height and width */
5371 if (wHeight == 0xffff)
5372 wHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
5373 if (wWidth == 0xffff)
5374 wWidth = infoPtr->rcList.right - infoPtr->rcList.left;
5377 dwViewRect = MAKELONG(wWidth, wHeight);
5379 else if (infoPtr->uView == LV_VIEW_ICON)
5381 UINT rows,cols;
5382 UINT nItemWidth;
5383 UINT nItemHeight;
5385 nItemWidth = infoPtr->iconSpacing.cx;
5386 nItemHeight = infoPtr->iconSpacing.cy;
5388 if (wWidth == 0xffff)
5389 wWidth = infoPtr->rcList.right - infoPtr->rcList.left;
5391 if (wWidth < nItemWidth)
5392 wWidth = nItemWidth;
5394 cols = wWidth / nItemWidth;
5395 if (cols > nItemCount)
5396 cols = nItemCount;
5397 if (cols < 1)
5398 cols = 1;
5400 if (nItemCount)
5402 rows = nItemCount / cols;
5403 if (nItemCount % cols)
5404 rows++;
5406 else
5407 rows = 0;
5409 wHeight = (nItemHeight * rows)+2;
5410 wWidth = (nItemWidth * cols)+2;
5412 dwViewRect = MAKELONG(wWidth, wHeight);
5414 else if (infoPtr->uView == LV_VIEW_SMALLICON)
5415 FIXME("uView == LV_VIEW_SMALLICON: not implemented\n");
5417 return dwViewRect;
5420 /***
5421 * DESCRIPTION:
5422 * Cancel edit label with saving item text.
5424 * PARAMETER(S):
5425 * [I] infoPtr : valid pointer to the listview structure
5427 * RETURN:
5428 * Always returns TRUE.
5430 static LRESULT LISTVIEW_CancelEditLabel(LISTVIEW_INFO *infoPtr)
5432 if (infoPtr->hwndEdit)
5434 /* handle value will be lost after LISTVIEW_EndEditLabelT */
5435 HWND edit = infoPtr->hwndEdit;
5437 LISTVIEW_EndEditLabelT(infoPtr, TRUE, IsWindowUnicode(infoPtr->hwndEdit));
5438 SendMessageW(edit, WM_CLOSE, 0, 0);
5441 return TRUE;
5444 /***
5445 * DESCRIPTION:
5446 * Create a drag image list for the specified item.
5448 * PARAMETER(S):
5449 * [I] infoPtr : valid pointer to the listview structure
5450 * [I] iItem : index of item
5451 * [O] lppt : Upper-left corner of the image
5453 * RETURN:
5454 * Returns a handle to the image list if successful, NULL otherwise.
5456 static HIMAGELIST LISTVIEW_CreateDragImage(LISTVIEW_INFO *infoPtr, INT iItem, LPPOINT lppt)
5458 RECT rcItem;
5459 SIZE size;
5460 POINT pos;
5461 HDC hdc, hdcOrig;
5462 HBITMAP hbmp, hOldbmp;
5463 HFONT hOldFont;
5464 HIMAGELIST dragList = 0;
5465 TRACE("iItem=%d Count=%d\n", iItem, infoPtr->nItemCount);
5467 if (iItem < 0 || iItem >= infoPtr->nItemCount || !lppt)
5468 return 0;
5470 rcItem.left = LVIR_BOUNDS;
5471 if (!LISTVIEW_GetItemRect(infoPtr, iItem, &rcItem))
5472 return 0;
5474 lppt->x = rcItem.left;
5475 lppt->y = rcItem.top;
5477 size.cx = rcItem.right - rcItem.left;
5478 size.cy = rcItem.bottom - rcItem.top;
5480 hdcOrig = GetDC(infoPtr->hwndSelf);
5481 hdc = CreateCompatibleDC(hdcOrig);
5482 hbmp = CreateCompatibleBitmap(hdcOrig, size.cx, size.cy);
5483 hOldbmp = SelectObject(hdc, hbmp);
5484 hOldFont = SelectObject(hdc, infoPtr->hFont);
5486 SetRect(&rcItem, 0, 0, size.cx, size.cy);
5487 FillRect(hdc, &rcItem, infoPtr->hBkBrush);
5489 pos.x = pos.y = 0;
5490 if (LISTVIEW_DrawItem(infoPtr, hdc, iItem, NULL, pos, CDRF_DODEFAULT))
5492 dragList = ImageList_Create(size.cx, size.cy, ILC_COLOR, 10, 10);
5493 SelectObject(hdc, hOldbmp);
5494 ImageList_Add(dragList, hbmp, 0);
5496 else
5497 SelectObject(hdc, hOldbmp);
5499 SelectObject(hdc, hOldFont);
5500 DeleteObject(hbmp);
5501 DeleteDC(hdc);
5502 ReleaseDC(infoPtr->hwndSelf, hdcOrig);
5504 TRACE("ret=%p\n", dragList);
5506 return dragList;
5510 /***
5511 * DESCRIPTION:
5512 * Removes all listview items and subitems.
5514 * PARAMETER(S):
5515 * [I] infoPtr : valid pointer to the listview structure
5517 * RETURN:
5518 * SUCCESS : TRUE
5519 * FAILURE : FALSE
5521 static BOOL LISTVIEW_DeleteAllItems(LISTVIEW_INFO *infoPtr, BOOL destroy)
5523 HDPA hdpaSubItems = NULL;
5524 BOOL suppress = FALSE;
5525 ITEMHDR *hdrItem;
5526 ITEM_INFO *lpItem;
5527 ITEM_ID *lpID;
5528 INT i, j;
5530 TRACE("()\n");
5532 /* we do it directly, to avoid notifications */
5533 ranges_clear(infoPtr->selectionRanges);
5534 infoPtr->nSelectionMark = -1;
5535 infoPtr->nFocusedItem = -1;
5536 SetRectEmpty(&infoPtr->rcFocus);
5537 /* But we are supposed to leave nHotItem as is! */
5539 /* send LVN_DELETEALLITEMS notification */
5540 if (!(infoPtr->dwStyle & LVS_OWNERDATA) || !destroy)
5542 NMLISTVIEW nmlv;
5544 memset(&nmlv, 0, sizeof(NMLISTVIEW));
5545 nmlv.iItem = -1;
5546 suppress = notify_listview(infoPtr, LVN_DELETEALLITEMS, &nmlv);
5549 for (i = infoPtr->nItemCount - 1; i >= 0; i--)
5551 if (!(infoPtr->dwStyle & LVS_OWNERDATA))
5553 /* send LVN_DELETEITEM notification, if not suppressed
5554 and if it is not a virtual listview */
5555 if (!suppress) notify_deleteitem(infoPtr, i);
5556 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, i);
5557 lpItem = DPA_GetPtr(hdpaSubItems, 0);
5558 /* free id struct */
5559 j = DPA_GetPtrIndex(infoPtr->hdpaItemIds, lpItem->id);
5560 lpID = DPA_GetPtr(infoPtr->hdpaItemIds, j);
5561 DPA_DeletePtr(infoPtr->hdpaItemIds, j);
5562 Free(lpID);
5563 /* both item and subitem start with ITEMHDR header */
5564 for (j = 0; j < DPA_GetPtrCount(hdpaSubItems); j++)
5566 hdrItem = DPA_GetPtr(hdpaSubItems, j);
5567 if (is_text(hdrItem->pszText)) Free(hdrItem->pszText);
5568 Free(hdrItem);
5570 DPA_Destroy(hdpaSubItems);
5571 DPA_DeletePtr(infoPtr->hdpaItems, i);
5573 DPA_DeletePtr(infoPtr->hdpaPosX, i);
5574 DPA_DeletePtr(infoPtr->hdpaPosY, i);
5575 infoPtr->nItemCount --;
5578 if (!destroy)
5580 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
5581 LISTVIEW_UpdateScroll(infoPtr);
5583 LISTVIEW_InvalidateList(infoPtr);
5585 return TRUE;
5588 /***
5589 * DESCRIPTION:
5590 * Scrolls, and updates the columns, when a column is changing width.
5592 * PARAMETER(S):
5593 * [I] infoPtr : valid pointer to the listview structure
5594 * [I] nColumn : column to scroll
5595 * [I] dx : amount of scroll, in pixels
5597 * RETURN:
5598 * None.
5600 static void LISTVIEW_ScrollColumns(LISTVIEW_INFO *infoPtr, INT nColumn, INT dx)
5602 COLUMN_INFO *lpColumnInfo;
5603 RECT rcOld, rcCol;
5604 POINT ptOrigin;
5605 INT nCol;
5606 HDITEMW hdi;
5608 if (nColumn < 0 || DPA_GetPtrCount(infoPtr->hdpaColumns) < 1) return;
5609 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, min(nColumn, DPA_GetPtrCount(infoPtr->hdpaColumns) - 1));
5610 rcCol = lpColumnInfo->rcHeader;
5611 if (nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns))
5612 rcCol.left = rcCol.right;
5614 /* adjust the other columns */
5615 hdi.mask = HDI_ORDER;
5616 if (SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nColumn, (LPARAM)&hdi))
5618 INT nOrder = hdi.iOrder;
5619 for (nCol = 0; nCol < DPA_GetPtrCount(infoPtr->hdpaColumns); nCol++)
5621 hdi.mask = HDI_ORDER;
5622 SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nCol, (LPARAM)&hdi);
5623 if (hdi.iOrder >= nOrder) {
5624 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, nCol);
5625 lpColumnInfo->rcHeader.left += dx;
5626 lpColumnInfo->rcHeader.right += dx;
5631 /* do not update screen if not in report mode */
5632 if (!is_redrawing(infoPtr) || infoPtr->uView != LV_VIEW_DETAILS) return;
5634 /* Need to reset the item width when inserting a new column */
5635 infoPtr->nItemWidth += dx;
5637 LISTVIEW_UpdateScroll(infoPtr);
5638 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
5640 /* scroll to cover the deleted column, and invalidate for redraw */
5641 rcOld = infoPtr->rcList;
5642 rcOld.left = ptOrigin.x + rcCol.left + dx;
5643 ScrollWindowEx(infoPtr->hwndSelf, dx, 0, &rcOld, &rcOld, 0, 0, SW_ERASE | SW_INVALIDATE);
5646 /***
5647 * DESCRIPTION:
5648 * Removes a column from the listview control.
5650 * PARAMETER(S):
5651 * [I] infoPtr : valid pointer to the listview structure
5652 * [I] nColumn : column index
5654 * RETURN:
5655 * SUCCESS : TRUE
5656 * FAILURE : FALSE
5658 static BOOL LISTVIEW_DeleteColumn(LISTVIEW_INFO *infoPtr, INT nColumn)
5660 RECT rcCol;
5662 TRACE("nColumn=%d\n", nColumn);
5664 if (nColumn < 0 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns))
5665 return FALSE;
5667 /* While the MSDN specifically says that column zero should not be deleted,
5668 what actually happens is that the column itself is deleted but no items or subitems
5669 are removed.
5672 LISTVIEW_GetHeaderRect(infoPtr, nColumn, &rcCol);
5674 if (!SendMessageW(infoPtr->hwndHeader, HDM_DELETEITEM, nColumn, 0))
5675 return FALSE;
5677 Free(DPA_GetPtr(infoPtr->hdpaColumns, nColumn));
5678 DPA_DeletePtr(infoPtr->hdpaColumns, nColumn);
5680 if (!(infoPtr->dwStyle & LVS_OWNERDATA) && nColumn)
5682 SUBITEM_INFO *lpSubItem, *lpDelItem;
5683 HDPA hdpaSubItems;
5684 INT nItem, nSubItem, i;
5686 for (nItem = 0; nItem < infoPtr->nItemCount; nItem++)
5688 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, nItem);
5689 nSubItem = 0;
5690 lpDelItem = 0;
5691 for (i = 1; i < DPA_GetPtrCount(hdpaSubItems); i++)
5693 lpSubItem = DPA_GetPtr(hdpaSubItems, i);
5694 if (lpSubItem->iSubItem == nColumn)
5696 nSubItem = i;
5697 lpDelItem = lpSubItem;
5699 else if (lpSubItem->iSubItem > nColumn)
5701 lpSubItem->iSubItem--;
5705 /* if we found our subitem, zap it */
5706 if (nSubItem > 0)
5708 /* free string */
5709 if (is_text(lpDelItem->hdr.pszText))
5710 Free(lpDelItem->hdr.pszText);
5712 /* free item */
5713 Free(lpDelItem);
5715 /* free dpa memory */
5716 DPA_DeletePtr(hdpaSubItems, nSubItem);
5721 /* update the other column info */
5722 if(DPA_GetPtrCount(infoPtr->hdpaColumns) == 0)
5723 LISTVIEW_InvalidateList(infoPtr);
5724 else
5725 LISTVIEW_ScrollColumns(infoPtr, nColumn, -(rcCol.right - rcCol.left));
5726 LISTVIEW_UpdateItemSize(infoPtr);
5728 return TRUE;
5731 /***
5732 * DESCRIPTION:
5733 * Invalidates the listview after an item's insertion or deletion.
5735 * PARAMETER(S):
5736 * [I] infoPtr : valid pointer to the listview structure
5737 * [I] nItem : item index
5738 * [I] dir : -1 if deleting, 1 if inserting
5740 * RETURN:
5741 * None
5743 static void LISTVIEW_ScrollOnInsert(LISTVIEW_INFO *infoPtr, INT nItem, INT dir)
5745 INT nPerCol, nItemCol, nItemRow;
5746 RECT rcScroll;
5747 POINT Origin;
5749 /* if we don't refresh, what's the point of scrolling? */
5750 if (!is_redrawing(infoPtr)) return;
5752 assert (abs(dir) == 1);
5754 /* arrange icons if autoarrange is on */
5755 if (is_autoarrange(infoPtr))
5757 BOOL arrange = TRUE;
5758 if (dir < 0 && nItem >= infoPtr->nItemCount) arrange = FALSE;
5759 if (dir > 0 && nItem == infoPtr->nItemCount - 1) arrange = FALSE;
5760 if (arrange) LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
5763 /* scrollbars need updating */
5764 LISTVIEW_UpdateScroll(infoPtr);
5766 /* figure out the item's position */
5767 if (infoPtr->uView == LV_VIEW_DETAILS)
5768 nPerCol = infoPtr->nItemCount + 1;
5769 else if (infoPtr->uView == LV_VIEW_LIST)
5770 nPerCol = LISTVIEW_GetCountPerColumn(infoPtr);
5771 else /* LV_VIEW_ICON, or LV_VIEW_SMALLICON */
5772 return;
5774 nItemCol = nItem / nPerCol;
5775 nItemRow = nItem % nPerCol;
5776 LISTVIEW_GetOrigin(infoPtr, &Origin);
5778 /* move the items below up a slot */
5779 rcScroll.left = nItemCol * infoPtr->nItemWidth;
5780 rcScroll.top = nItemRow * infoPtr->nItemHeight;
5781 rcScroll.right = rcScroll.left + infoPtr->nItemWidth;
5782 rcScroll.bottom = nPerCol * infoPtr->nItemHeight;
5783 OffsetRect(&rcScroll, Origin.x, Origin.y);
5784 TRACE("rcScroll=%s, dx=%d\n", wine_dbgstr_rect(&rcScroll), dir * infoPtr->nItemHeight);
5785 if (IntersectRect(&rcScroll, &rcScroll, &infoPtr->rcList))
5787 TRACE("Invalidating rcScroll=%s, rcList=%s\n", wine_dbgstr_rect(&rcScroll), wine_dbgstr_rect(&infoPtr->rcList));
5788 InvalidateRect(infoPtr->hwndSelf, &rcScroll, TRUE);
5791 /* report has only that column, so we're done */
5792 if (infoPtr->uView == LV_VIEW_DETAILS) return;
5794 /* now for LISTs, we have to deal with the columns to the right */
5795 SetRect(&rcScroll, (nItemCol + 1) * infoPtr->nItemWidth, 0,
5796 (infoPtr->nItemCount / nPerCol + 1) * infoPtr->nItemWidth,
5797 nPerCol * infoPtr->nItemHeight);
5798 OffsetRect(&rcScroll, Origin.x, Origin.y);
5799 if (IntersectRect(&rcScroll, &rcScroll, &infoPtr->rcList))
5800 InvalidateRect(infoPtr->hwndSelf, &rcScroll, TRUE);
5803 /***
5804 * DESCRIPTION:
5805 * Removes an item from the listview control.
5807 * PARAMETER(S):
5808 * [I] infoPtr : valid pointer to the listview structure
5809 * [I] nItem : item index
5811 * RETURN:
5812 * SUCCESS : TRUE
5813 * FAILURE : FALSE
5815 static BOOL LISTVIEW_DeleteItem(LISTVIEW_INFO *infoPtr, INT nItem)
5817 LVITEMW item;
5818 const BOOL is_icon = (infoPtr->uView == LV_VIEW_SMALLICON || infoPtr->uView == LV_VIEW_ICON);
5819 INT focus = infoPtr->nFocusedItem;
5821 TRACE("(nItem=%d)\n", nItem);
5823 if (nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
5825 /* remove selection, and focus */
5826 item.state = 0;
5827 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
5828 LISTVIEW_SetItemState(infoPtr, nItem, &item);
5830 /* send LVN_DELETEITEM notification. */
5831 if (!notify_deleteitem(infoPtr, nItem)) return FALSE;
5833 /* we need to do this here, because we'll be deleting stuff */
5834 if (is_icon)
5835 LISTVIEW_InvalidateItem(infoPtr, nItem);
5837 if (!(infoPtr->dwStyle & LVS_OWNERDATA))
5839 HDPA hdpaSubItems;
5840 ITEMHDR *hdrItem;
5841 ITEM_INFO *lpItem;
5842 ITEM_ID *lpID;
5843 INT i;
5845 hdpaSubItems = DPA_DeletePtr(infoPtr->hdpaItems, nItem);
5846 lpItem = DPA_GetPtr(hdpaSubItems, 0);
5848 /* free id struct */
5849 i = DPA_GetPtrIndex(infoPtr->hdpaItemIds, lpItem->id);
5850 lpID = DPA_GetPtr(infoPtr->hdpaItemIds, i);
5851 DPA_DeletePtr(infoPtr->hdpaItemIds, i);
5852 Free(lpID);
5853 for (i = 0; i < DPA_GetPtrCount(hdpaSubItems); i++)
5855 hdrItem = DPA_GetPtr(hdpaSubItems, i);
5856 if (is_text(hdrItem->pszText)) Free(hdrItem->pszText);
5857 Free(hdrItem);
5859 DPA_Destroy(hdpaSubItems);
5862 if (is_icon)
5864 DPA_DeletePtr(infoPtr->hdpaPosX, nItem);
5865 DPA_DeletePtr(infoPtr->hdpaPosY, nItem);
5868 infoPtr->nItemCount--;
5869 LISTVIEW_ShiftIndices(infoPtr, nItem, -1);
5870 LISTVIEW_ShiftFocus(infoPtr, focus, nItem, -1);
5872 /* now is the invalidation fun */
5873 if (!is_icon)
5874 LISTVIEW_ScrollOnInsert(infoPtr, nItem, -1);
5875 return TRUE;
5879 /***
5880 * DESCRIPTION:
5881 * Callback implementation for editlabel control
5883 * PARAMETER(S):
5884 * [I] infoPtr : valid pointer to the listview structure
5885 * [I] storeText : store edit box text as item text
5886 * [I] isW : TRUE if psxText is Unicode, FALSE if it's ANSI
5888 * RETURN:
5889 * SUCCESS : TRUE
5890 * FAILURE : FALSE
5892 static BOOL LISTVIEW_EndEditLabelT(LISTVIEW_INFO *infoPtr, BOOL storeText, BOOL isW)
5894 HWND hwndSelf = infoPtr->hwndSelf;
5895 WCHAR szDispText[DISP_TEXT_SIZE] = { 0 };
5896 NMLVDISPINFOW dispInfo;
5897 INT editedItem = infoPtr->nEditLabelItem;
5898 BOOL same;
5899 WCHAR *pszText = NULL;
5900 BOOL res;
5902 if (storeText)
5904 DWORD len = isW ? GetWindowTextLengthW(infoPtr->hwndEdit) : GetWindowTextLengthA(infoPtr->hwndEdit);
5906 if (len++)
5908 if (!(pszText = Alloc(len * (isW ? sizeof(WCHAR) : sizeof(CHAR)))))
5909 return FALSE;
5911 if (isW)
5912 GetWindowTextW(infoPtr->hwndEdit, pszText, len);
5913 else
5914 GetWindowTextA(infoPtr->hwndEdit, (CHAR*)pszText, len);
5918 TRACE("(pszText=%s, isW=%d)\n", debugtext_t(pszText, isW), isW);
5920 ZeroMemory(&dispInfo, sizeof(dispInfo));
5921 dispInfo.item.mask = LVIF_PARAM | LVIF_STATE | LVIF_TEXT;
5922 dispInfo.item.iItem = editedItem;
5923 dispInfo.item.iSubItem = 0;
5924 dispInfo.item.stateMask = ~0;
5925 dispInfo.item.pszText = szDispText;
5926 dispInfo.item.cchTextMax = DISP_TEXT_SIZE;
5927 if (!LISTVIEW_GetItemT(infoPtr, &dispInfo.item, isW))
5929 res = FALSE;
5930 goto cleanup;
5933 if (isW)
5934 same = (lstrcmpW(dispInfo.item.pszText, pszText) == 0);
5935 else
5937 LPWSTR tmp = textdupTtoW(pszText, FALSE);
5938 same = (lstrcmpW(dispInfo.item.pszText, tmp) == 0);
5939 textfreeT(tmp, FALSE);
5942 /* add the text from the edit in */
5943 dispInfo.item.mask |= LVIF_TEXT;
5944 dispInfo.item.pszText = same ? NULL : pszText;
5945 dispInfo.item.cchTextMax = textlenT(dispInfo.item.pszText, isW);
5947 infoPtr->notify_mask &= ~NOTIFY_MASK_END_LABEL_EDIT;
5949 /* Do we need to update the Item Text */
5950 res = notify_dispinfoT(infoPtr, LVN_ENDLABELEDITW, &dispInfo, isW);
5952 infoPtr->notify_mask |= NOTIFY_MASK_END_LABEL_EDIT;
5954 infoPtr->nEditLabelItem = -1;
5955 infoPtr->hwndEdit = 0;
5957 if (!res) goto cleanup;
5959 if (!IsWindow(hwndSelf))
5961 res = FALSE;
5962 goto cleanup;
5964 if (!pszText) return TRUE;
5965 if (same)
5967 res = TRUE;
5968 goto cleanup;
5971 if (!(infoPtr->dwStyle & LVS_OWNERDATA))
5973 HDPA hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, editedItem);
5974 ITEM_INFO* lpItem = DPA_GetPtr(hdpaSubItems, 0);
5975 if (lpItem && lpItem->hdr.pszText == LPSTR_TEXTCALLBACKW)
5977 LISTVIEW_InvalidateItem(infoPtr, editedItem);
5978 res = TRUE;
5979 goto cleanup;
5983 ZeroMemory(&dispInfo, sizeof(dispInfo));
5984 dispInfo.item.mask = LVIF_TEXT;
5985 dispInfo.item.iItem = editedItem;
5986 dispInfo.item.iSubItem = 0;
5987 dispInfo.item.pszText = pszText;
5988 dispInfo.item.cchTextMax = textlenT(pszText, isW);
5989 res = LISTVIEW_SetItemT(infoPtr, &dispInfo.item, isW);
5991 cleanup:
5992 Free(pszText);
5994 return res;
5997 static LRESULT EditLblWndProcT(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL isW)
5999 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongPtrW(GetParent(hwnd), 0);
6000 BOOL save = TRUE;
6002 TRACE("hwnd %p, uMsg %x, wParam %Ix, lParam %Ix, isW %d\n", hwnd, uMsg, wParam, lParam, isW);
6004 switch (uMsg)
6006 case WM_GETDLGCODE:
6007 return DLGC_WANTARROWS | DLGC_WANTALLKEYS;
6009 case WM_DESTROY:
6011 WNDPROC editProc = infoPtr->EditWndProc;
6012 infoPtr->EditWndProc = 0;
6013 SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (DWORD_PTR)editProc);
6014 return CallWindowProcT(editProc, hwnd, uMsg, wParam, lParam, isW);
6017 case WM_KEYDOWN:
6018 if (VK_ESCAPE == (INT)wParam)
6020 save = FALSE;
6021 break;
6023 else if (VK_RETURN == (INT)wParam)
6024 break;
6026 default:
6027 return CallWindowProcT(infoPtr->EditWndProc, hwnd, uMsg, wParam, lParam, isW);
6030 /* kill the edit */
6031 if (infoPtr->hwndEdit)
6032 LISTVIEW_EndEditLabelT(infoPtr, save, isW);
6034 SendMessageW(hwnd, WM_CLOSE, 0, 0);
6035 return 0;
6038 static LRESULT CALLBACK EditLblWndProcW(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
6040 return EditLblWndProcT(hwnd, uMsg, wParam, lParam, TRUE);
6043 static LRESULT CALLBACK EditLblWndProcA(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
6045 return EditLblWndProcT(hwnd, uMsg, wParam, lParam, FALSE);
6048 static HWND CreateEditLabelT(LISTVIEW_INFO *infoPtr, LPCWSTR text, BOOL isW)
6050 static const DWORD style = WS_CHILDWINDOW|WS_CLIPSIBLINGS|ES_LEFT|ES_AUTOHSCROLL|WS_BORDER|WS_VISIBLE;
6051 HINSTANCE hinst = (HINSTANCE)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_HINSTANCE);
6052 HWND hedit;
6054 TRACE("(%p, text=%s, isW=%d)\n", infoPtr, debugtext_t(text, isW), isW);
6056 /* window will be resized and positioned after LVN_BEGINLABELEDIT */
6057 if (isW)
6058 hedit = CreateWindowW(WC_EDITW, text, style, 0, 0, 0, 0, infoPtr->hwndSelf, 0, hinst, 0);
6059 else
6060 hedit = CreateWindowA(WC_EDITA, (LPCSTR)text, style, 0, 0, 0, 0, infoPtr->hwndSelf, 0, hinst, 0);
6062 if (!hedit) return 0;
6064 infoPtr->EditWndProc = (WNDPROC)
6065 (isW ? SetWindowLongPtrW(hedit, GWLP_WNDPROC, (DWORD_PTR)EditLblWndProcW) :
6066 SetWindowLongPtrA(hedit, GWLP_WNDPROC, (DWORD_PTR)EditLblWndProcA) );
6068 SendMessageW(hedit, WM_SETFONT, (WPARAM)infoPtr->hFont, FALSE);
6069 SendMessageW(hedit, EM_SETLIMITTEXT, DISP_TEXT_SIZE-1, 0);
6071 return hedit;
6074 static HWND LISTVIEW_EditLabelT(LISTVIEW_INFO *infoPtr, INT nItem, BOOL isW)
6076 WCHAR disptextW[DISP_TEXT_SIZE] = { 0 };
6077 HWND hwndSelf = infoPtr->hwndSelf;
6078 NMLVDISPINFOW dispInfo;
6079 HFONT hOldFont = NULL;
6080 TEXTMETRICW tm;
6081 RECT rect;
6082 SIZE sz;
6083 HDC hdc;
6085 TRACE("(nItem=%d, isW=%d)\n", nItem, isW);
6087 if (~infoPtr->dwStyle & LVS_EDITLABELS) return 0;
6089 /* remove existing edit box */
6090 if (infoPtr->hwndEdit)
6092 SetFocus(infoPtr->hwndSelf);
6093 infoPtr->hwndEdit = 0;
6096 if (nItem < 0 || nItem >= infoPtr->nItemCount) return 0;
6098 infoPtr->nEditLabelItem = nItem;
6100 LISTVIEW_SetSelection(infoPtr, nItem);
6101 LISTVIEW_SetItemFocus(infoPtr, nItem);
6102 LISTVIEW_InvalidateItem(infoPtr, nItem);
6104 rect.left = LVIR_LABEL;
6105 if (!LISTVIEW_GetItemRect(infoPtr, nItem, &rect)) return 0;
6107 ZeroMemory(&dispInfo, sizeof(dispInfo));
6108 dispInfo.item.mask = LVIF_PARAM | LVIF_STATE | LVIF_TEXT;
6109 dispInfo.item.iItem = nItem;
6110 dispInfo.item.iSubItem = 0;
6111 dispInfo.item.stateMask = ~0;
6112 dispInfo.item.pszText = disptextW;
6113 dispInfo.item.cchTextMax = DISP_TEXT_SIZE;
6114 if (!LISTVIEW_GetItemT(infoPtr, &dispInfo.item, isW)) return 0;
6116 infoPtr->hwndEdit = CreateEditLabelT(infoPtr, dispInfo.item.pszText, isW);
6117 if (!infoPtr->hwndEdit) return 0;
6119 if (notify_dispinfoT(infoPtr, LVN_BEGINLABELEDITW, &dispInfo, isW))
6121 if (!IsWindow(hwndSelf))
6122 return 0;
6123 SendMessageW(infoPtr->hwndEdit, WM_CLOSE, 0, 0);
6124 infoPtr->hwndEdit = 0;
6125 return 0;
6128 TRACE("disp text=%s\n", debugtext_t(dispInfo.item.pszText, isW));
6130 /* position and display edit box */
6131 hdc = GetDC(infoPtr->hwndSelf);
6133 /* select the font to get appropriate metric dimensions */
6134 if (infoPtr->hFont)
6135 hOldFont = SelectObject(hdc, infoPtr->hFont);
6137 /* use real edit box content, it could be altered during LVN_BEGINLABELEDIT notification */
6138 GetWindowTextW(infoPtr->hwndEdit, disptextW, DISP_TEXT_SIZE);
6139 TRACE("edit box text=%s\n", debugstr_w(disptextW));
6141 /* get string length in pixels */
6142 GetTextExtentPoint32W(hdc, disptextW, lstrlenW(disptextW), &sz);
6144 /* add extra spacing for the next character */
6145 GetTextMetricsW(hdc, &tm);
6146 sz.cx += tm.tmMaxCharWidth * 2;
6148 if (infoPtr->hFont)
6149 SelectObject(hdc, hOldFont);
6151 ReleaseDC(infoPtr->hwndSelf, hdc);
6153 sz.cy = rect.bottom - rect.top + 2;
6154 rect.left -= 2;
6155 rect.top -= 1;
6156 TRACE("moving edit (%ld,%ld)-(%ld,%ld)\n", rect.left, rect.top, sz.cx, sz.cy);
6157 MoveWindow(infoPtr->hwndEdit, rect.left, rect.top, sz.cx, sz.cy, FALSE);
6158 ShowWindow(infoPtr->hwndEdit, SW_NORMAL);
6159 SetFocus(infoPtr->hwndEdit);
6160 SendMessageW(infoPtr->hwndEdit, EM_SETSEL, 0, -1);
6161 return infoPtr->hwndEdit;
6165 /***
6166 * DESCRIPTION:
6167 * Ensures the specified item is visible, scrolling into view if necessary.
6169 * PARAMETER(S):
6170 * [I] infoPtr : valid pointer to the listview structure
6171 * [I] nItem : item index
6172 * [I] bPartial : partially or entirely visible
6174 * RETURN:
6175 * SUCCESS : TRUE
6176 * FAILURE : FALSE
6178 static BOOL LISTVIEW_EnsureVisible(LISTVIEW_INFO *infoPtr, INT nItem, BOOL bPartial)
6180 INT nScrollPosHeight = 0;
6181 INT nScrollPosWidth = 0;
6182 INT nHorzAdjust = 0;
6183 INT nVertAdjust = 0;
6184 INT nHorzDiff = 0;
6185 INT nVertDiff = 0;
6186 RECT rcItem, rcTemp;
6188 rcItem.left = LVIR_BOUNDS;
6189 if (!LISTVIEW_GetItemRect(infoPtr, nItem, &rcItem)) return FALSE;
6191 if (bPartial && IntersectRect(&rcTemp, &infoPtr->rcList, &rcItem)) return TRUE;
6193 if (rcItem.left < infoPtr->rcList.left || rcItem.right > infoPtr->rcList.right)
6195 /* scroll left/right, but in LV_VIEW_DETAILS mode */
6196 if (infoPtr->uView == LV_VIEW_LIST)
6197 nScrollPosWidth = infoPtr->nItemWidth;
6198 else if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
6199 nScrollPosWidth = 1;
6201 if (rcItem.left < infoPtr->rcList.left)
6203 nHorzAdjust = -1;
6204 if (infoPtr->uView != LV_VIEW_DETAILS) nHorzDiff = rcItem.left - infoPtr->rcList.left;
6206 else
6208 nHorzAdjust = 1;
6209 if (infoPtr->uView != LV_VIEW_DETAILS) nHorzDiff = rcItem.right - infoPtr->rcList.right;
6213 if (rcItem.top < infoPtr->rcList.top || rcItem.bottom > infoPtr->rcList.bottom)
6215 /* scroll up/down, but not in LVS_LIST mode */
6216 if (infoPtr->uView == LV_VIEW_DETAILS)
6217 nScrollPosHeight = infoPtr->nItemHeight;
6218 else if ((infoPtr->uView == LV_VIEW_ICON) || (infoPtr->uView == LV_VIEW_SMALLICON))
6219 nScrollPosHeight = 1;
6221 if (rcItem.top < infoPtr->rcList.top)
6223 nVertAdjust = -1;
6224 if (infoPtr->uView != LV_VIEW_LIST) nVertDiff = rcItem.top - infoPtr->rcList.top;
6226 else
6228 nVertAdjust = 1;
6229 if (infoPtr->uView != LV_VIEW_LIST) nVertDiff = rcItem.bottom - infoPtr->rcList.bottom;
6233 if (!nScrollPosWidth && !nScrollPosHeight) return TRUE;
6235 if (nScrollPosWidth)
6237 INT diff = nHorzDiff / nScrollPosWidth;
6238 if (nHorzDiff % nScrollPosWidth) diff += nHorzAdjust;
6239 LISTVIEW_HScroll(infoPtr, SB_INTERNAL, diff);
6242 if (nScrollPosHeight)
6244 INT diff = nVertDiff / nScrollPosHeight;
6245 if (nVertDiff % nScrollPosHeight) diff += nVertAdjust;
6246 LISTVIEW_VScroll(infoPtr, SB_INTERNAL, diff);
6249 return TRUE;
6252 /***
6253 * DESCRIPTION:
6254 * Searches for an item with specific characteristics.
6256 * PARAMETER(S):
6257 * [I] hwnd : window handle
6258 * [I] nStart : base item index
6259 * [I] lpFindInfo : item information to look for
6261 * RETURN:
6262 * SUCCESS : index of item
6263 * FAILURE : -1
6265 static INT LISTVIEW_FindItemW(const LISTVIEW_INFO *infoPtr, INT nStart,
6266 const LVFINDINFOW *lpFindInfo)
6268 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
6269 BOOL bWrap = FALSE, bNearest = FALSE;
6270 INT nItem = nStart + 1, nLast = infoPtr->nItemCount, nNearestItem = -1;
6271 ULONG xdist, ydist, dist, mindist = 0x7fffffff;
6272 POINT Position, Destination;
6273 int search_len = 0;
6274 LVITEMW lvItem;
6276 /* Search in virtual listviews should be done by application, not by
6277 listview control, so we just send LVN_ODFINDITEMW and return the result */
6278 if (infoPtr->dwStyle & LVS_OWNERDATA)
6280 NMLVFINDITEMW nmlv;
6282 nmlv.iStart = nStart;
6283 nmlv.lvfi = *lpFindInfo;
6284 return notify_hdr(infoPtr, LVN_ODFINDITEMW, (LPNMHDR)&nmlv.hdr);
6287 if (!lpFindInfo || nItem < 0) return -1;
6289 lvItem.mask = 0;
6290 if (lpFindInfo->flags & (LVFI_STRING | LVFI_PARTIAL | LVFI_SUBSTRING))
6292 lvItem.mask |= LVIF_TEXT;
6293 lvItem.pszText = szDispText;
6294 lvItem.cchTextMax = DISP_TEXT_SIZE;
6297 if (lpFindInfo->flags & LVFI_WRAP)
6298 bWrap = TRUE;
6300 if ((lpFindInfo->flags & LVFI_NEARESTXY) &&
6301 (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON))
6303 POINT Origin;
6304 RECT rcArea;
6306 LISTVIEW_GetOrigin(infoPtr, &Origin);
6307 Destination.x = lpFindInfo->pt.x - Origin.x;
6308 Destination.y = lpFindInfo->pt.y - Origin.y;
6309 switch(lpFindInfo->vkDirection)
6311 case VK_DOWN: Destination.y += infoPtr->nItemHeight; break;
6312 case VK_UP: Destination.y -= infoPtr->nItemHeight; break;
6313 case VK_RIGHT: Destination.x += infoPtr->nItemWidth; break;
6314 case VK_LEFT: Destination.x -= infoPtr->nItemWidth; break;
6315 case VK_HOME: Destination.x = Destination.y = 0; break;
6316 case VK_NEXT: Destination.y += infoPtr->rcList.bottom - infoPtr->rcList.top; break;
6317 case VK_PRIOR: Destination.y -= infoPtr->rcList.bottom - infoPtr->rcList.top; break;
6318 case VK_END:
6319 LISTVIEW_GetAreaRect(infoPtr, &rcArea);
6320 Destination.x = rcArea.right;
6321 Destination.y = rcArea.bottom;
6322 break;
6323 default: ERR("Unknown vkDirection=%d\n", lpFindInfo->vkDirection);
6325 bNearest = TRUE;
6327 else Destination.x = Destination.y = 0;
6329 /* if LVFI_PARAM is specified, all other flags are ignored */
6330 if (lpFindInfo->flags & LVFI_PARAM)
6332 lvItem.mask |= LVIF_PARAM;
6333 bNearest = FALSE;
6334 lvItem.mask &= ~LVIF_TEXT;
6337 nItem = bNearest ? -1 : nStart + 1;
6339 if (lpFindInfo->flags & (LVFI_PARTIAL | LVFI_SUBSTRING))
6340 search_len = lstrlenW(lpFindInfo->psz);
6342 again:
6343 for (; nItem < nLast; nItem++)
6345 lvItem.iItem = nItem;
6346 lvItem.iSubItem = 0;
6347 lvItem.pszText = szDispText;
6348 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) continue;
6350 if (lvItem.mask & LVIF_PARAM)
6352 if (lpFindInfo->lParam == lvItem.lParam)
6353 return nItem;
6354 else
6355 continue;
6358 if (lvItem.mask & LVIF_TEXT)
6360 if (lpFindInfo->flags & (LVFI_PARTIAL | LVFI_SUBSTRING))
6362 if (StrCmpNIW(lvItem.pszText, lpFindInfo->psz, search_len)) continue;
6364 else
6366 if (StrCmpIW(lvItem.pszText, lpFindInfo->psz)) continue;
6370 if (!bNearest) return nItem;
6372 /* This is very inefficient. To do a good job here,
6373 * we need a sorted array of (x,y) item positions */
6374 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
6376 /* compute the distance^2 to the destination */
6377 xdist = Destination.x - Position.x;
6378 ydist = Destination.y - Position.y;
6379 dist = xdist * xdist + ydist * ydist;
6381 /* remember the distance, and item if it's closer */
6382 if (dist < mindist)
6384 mindist = dist;
6385 nNearestItem = nItem;
6389 if (bWrap)
6391 nItem = 0;
6392 nLast = min(nStart + 1, infoPtr->nItemCount);
6393 bWrap = FALSE;
6394 goto again;
6397 return nNearestItem;
6400 /***
6401 * DESCRIPTION:
6402 * Searches for an item with specific characteristics.
6404 * PARAMETER(S):
6405 * [I] hwnd : window handle
6406 * [I] nStart : base item index
6407 * [I] lpFindInfo : item information to look for
6409 * RETURN:
6410 * SUCCESS : index of item
6411 * FAILURE : -1
6413 static INT LISTVIEW_FindItemA(const LISTVIEW_INFO *infoPtr, INT nStart,
6414 const LVFINDINFOA *lpFindInfo)
6416 LVFINDINFOW fiw;
6417 INT res;
6418 LPWSTR strW = NULL;
6420 memcpy(&fiw, lpFindInfo, sizeof(fiw));
6421 if (lpFindInfo->flags & (LVFI_STRING | LVFI_PARTIAL | LVFI_SUBSTRING))
6422 fiw.psz = strW = textdupTtoW((LPCWSTR)lpFindInfo->psz, FALSE);
6423 res = LISTVIEW_FindItemW(infoPtr, nStart, &fiw);
6424 textfreeT(strW, FALSE);
6425 return res;
6428 /***
6429 * DESCRIPTION:
6430 * Retrieves column attributes.
6432 * PARAMETER(S):
6433 * [I] infoPtr : valid pointer to the listview structure
6434 * [I] nColumn : column index
6435 * [IO] lpColumn : column information
6436 * [I] isW : if TRUE, then lpColumn is a LPLVCOLUMNW
6437 * otherwise it is in fact a LPLVCOLUMNA
6439 * RETURN:
6440 * SUCCESS : TRUE
6441 * FAILURE : FALSE
6443 static BOOL LISTVIEW_GetColumnT(const LISTVIEW_INFO *infoPtr, INT nColumn, LPLVCOLUMNW lpColumn, BOOL isW)
6445 COLUMN_INFO *lpColumnInfo;
6446 HDITEMW hdi;
6448 if (!lpColumn || nColumn < 0 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
6449 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, nColumn);
6451 /* initialize memory */
6452 ZeroMemory(&hdi, sizeof(hdi));
6454 if (lpColumn->mask & LVCF_TEXT)
6456 hdi.mask |= HDI_TEXT;
6457 hdi.pszText = lpColumn->pszText;
6458 hdi.cchTextMax = lpColumn->cchTextMax;
6461 if (lpColumn->mask & LVCF_IMAGE)
6462 hdi.mask |= HDI_IMAGE;
6464 if (lpColumn->mask & LVCF_ORDER)
6465 hdi.mask |= HDI_ORDER;
6467 if (lpColumn->mask & LVCF_SUBITEM)
6468 hdi.mask |= HDI_LPARAM;
6470 if (!SendMessageW(infoPtr->hwndHeader, isW ? HDM_GETITEMW : HDM_GETITEMA, nColumn, (LPARAM)&hdi)) return FALSE;
6472 if (lpColumn->mask & LVCF_FMT)
6473 lpColumn->fmt = lpColumnInfo->fmt;
6475 if (lpColumn->mask & LVCF_WIDTH)
6476 lpColumn->cx = lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left;
6478 if (lpColumn->mask & LVCF_IMAGE)
6479 lpColumn->iImage = hdi.iImage;
6481 if (lpColumn->mask & LVCF_ORDER)
6482 lpColumn->iOrder = hdi.iOrder;
6484 if (lpColumn->mask & LVCF_SUBITEM)
6485 lpColumn->iSubItem = hdi.lParam;
6487 if (lpColumn->mask & LVCF_MINWIDTH)
6488 lpColumn->cxMin = lpColumnInfo->cxMin;
6490 return TRUE;
6493 static inline BOOL LISTVIEW_GetColumnOrderArray(const LISTVIEW_INFO *infoPtr, INT iCount, LPINT lpiArray)
6495 if (!infoPtr->hwndHeader) return FALSE;
6496 return SendMessageW(infoPtr->hwndHeader, HDM_GETORDERARRAY, iCount, (LPARAM)lpiArray);
6499 /***
6500 * DESCRIPTION:
6501 * Retrieves the column width.
6503 * PARAMETER(S):
6504 * [I] infoPtr : valid pointer to the listview structure
6505 * [I] int : column index
6507 * RETURN:
6508 * SUCCESS : column width
6509 * FAILURE : zero
6511 static INT LISTVIEW_GetColumnWidth(const LISTVIEW_INFO *infoPtr, INT nColumn)
6513 INT nColumnWidth = 0;
6514 HDITEMW hdItem;
6516 TRACE("nColumn=%d\n", nColumn);
6518 /* we have a 'column' in LIST and REPORT mode only */
6519 switch(infoPtr->uView)
6521 case LV_VIEW_LIST:
6522 nColumnWidth = infoPtr->nItemWidth;
6523 break;
6524 case LV_VIEW_DETAILS:
6525 /* We are not using LISTVIEW_GetHeaderRect as this data is updated only after a HDN_ITEMCHANGED.
6526 * There is an application that subclasses the listview, calls LVM_GETCOLUMNWIDTH in the
6527 * HDN_ITEMCHANGED handler and goes into infinite recursion if it receives old data.
6529 hdItem.mask = HDI_WIDTH;
6530 if (!SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nColumn, (LPARAM)&hdItem))
6532 WARN("(%p): HDM_GETITEMW failed for item %d\n", infoPtr->hwndSelf, nColumn);
6533 return 0;
6535 nColumnWidth = hdItem.cxy;
6536 break;
6539 TRACE("nColumnWidth=%d\n", nColumnWidth);
6540 return nColumnWidth;
6543 /***
6544 * DESCRIPTION:
6545 * In list or report display mode, retrieves the number of items that can fit
6546 * vertically in the visible area. In icon or small icon display mode,
6547 * retrieves the total number of visible items.
6549 * PARAMETER(S):
6550 * [I] infoPtr : valid pointer to the listview structure
6552 * RETURN:
6553 * Number of fully visible items.
6555 static INT LISTVIEW_GetCountPerPage(const LISTVIEW_INFO *infoPtr)
6557 switch (infoPtr->uView)
6559 case LV_VIEW_ICON:
6560 case LV_VIEW_SMALLICON:
6561 return infoPtr->nItemCount;
6562 case LV_VIEW_DETAILS:
6563 return LISTVIEW_GetCountPerColumn(infoPtr);
6564 case LV_VIEW_LIST:
6565 return LISTVIEW_GetCountPerRow(infoPtr) * LISTVIEW_GetCountPerColumn(infoPtr);
6567 assert(FALSE);
6568 return 0;
6571 /***
6572 * DESCRIPTION:
6573 * Retrieves an image list handle.
6575 * PARAMETER(S):
6576 * [I] infoPtr : valid pointer to the listview structure
6577 * [I] nImageList : image list identifier
6579 * RETURN:
6580 * SUCCESS : image list handle
6581 * FAILURE : NULL
6583 static HIMAGELIST LISTVIEW_GetImageList(const LISTVIEW_INFO *infoPtr, INT nImageList)
6585 switch (nImageList)
6587 case LVSIL_NORMAL: return infoPtr->himlNormal;
6588 case LVSIL_SMALL: return infoPtr->himlSmall;
6589 case LVSIL_STATE: return infoPtr->himlState;
6590 case LVSIL_GROUPHEADER:
6591 FIXME("LVSIL_GROUPHEADER not supported\n");
6592 break;
6593 default:
6594 WARN("got unknown imagelist index - %d\n", nImageList);
6596 return NULL;
6599 /* LISTVIEW_GetISearchString */
6601 /***
6602 * DESCRIPTION:
6603 * Retrieves item attributes.
6605 * PARAMETER(S):
6606 * [I] hwnd : window handle
6607 * [IO] lpLVItem : item info
6608 * [I] isW : if TRUE, then lpLVItem is a LPLVITEMW,
6609 * if FALSE, then lpLVItem is a LPLVITEMA.
6611 * NOTE:
6612 * This is the internal 'GetItem' interface -- it tries to
6613 * be smart and avoid text copies, if possible, by modifying
6614 * lpLVItem->pszText to point to the text string. Please note
6615 * that this is not always possible (e.g. OWNERDATA), so on
6616 * entry you *must* supply valid values for pszText, and cchTextMax.
6617 * The only difference to the documented interface is that upon
6618 * return, you should use *only* the lpLVItem->pszText, rather than
6619 * the buffer pointer you provided on input. Most code already does
6620 * that, so it's not a problem.
6621 * For the two cases when the text must be copied (that is,
6622 * for LVM_GETITEM, and LVM_GETITEMTEXT), use LISTVIEW_GetItemExtT.
6624 * RETURN:
6625 * SUCCESS : TRUE
6626 * FAILURE : FALSE
6628 static BOOL LISTVIEW_GetItemT(const LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem, BOOL isW)
6630 ITEMHDR callbackHdr = { LPSTR_TEXTCALLBACKW, I_IMAGECALLBACK };
6631 BOOL is_subitem_invalid = FALSE;
6632 NMLVDISPINFOW dispInfo;
6633 ITEM_INFO *lpItem;
6634 ITEMHDR* pItemHdr;
6635 HDPA hdpaSubItems;
6637 TRACE("(item=%s, isW=%d)\n", debuglvitem_t(lpLVItem, isW), isW);
6639 if (!lpLVItem || lpLVItem->iItem < 0 || lpLVItem->iItem >= infoPtr->nItemCount)
6640 return FALSE;
6642 if (lpLVItem->mask == 0) return TRUE;
6643 TRACE("mask=%x\n", lpLVItem->mask);
6645 if (lpLVItem->iSubItem && (lpLVItem->mask & LVIF_STATE))
6646 lpLVItem->state = 0;
6648 /* a quick optimization if all we're asked is the focus state
6649 * these queries are worth optimising since they are common,
6650 * and can be answered in constant time, without the heavy accesses */
6651 if ( (lpLVItem->mask == LVIF_STATE) && (lpLVItem->stateMask == LVIS_FOCUSED) &&
6652 !(infoPtr->uCallbackMask & LVIS_FOCUSED) )
6654 lpLVItem->state = 0;
6655 if (infoPtr->nFocusedItem == lpLVItem->iItem && !lpLVItem->iSubItem)
6656 lpLVItem->state |= LVIS_FOCUSED;
6657 return TRUE;
6660 ZeroMemory(&dispInfo, sizeof(dispInfo));
6662 /* if the app stores all the data, handle it separately */
6663 if (infoPtr->dwStyle & LVS_OWNERDATA)
6665 dispInfo.item.state = 0;
6667 /* apparently, we should not callback for lParam in LVS_OWNERDATA */
6668 if ((lpLVItem->mask & ~(LVIF_STATE | LVIF_PARAM)) ||
6669 ((lpLVItem->mask & LVIF_STATE) && (infoPtr->uCallbackMask & lpLVItem->stateMask)))
6671 UINT mask = lpLVItem->mask;
6673 /* NOTE: copy only fields which we _know_ are initialized, some apps
6674 * depend on the uninitialized fields being 0 */
6675 dispInfo.item.mask = lpLVItem->mask & ~LVIF_PARAM;
6676 dispInfo.item.iItem = lpLVItem->iItem;
6677 dispInfo.item.iSubItem = lpLVItem->iSubItem;
6678 if (lpLVItem->mask & LVIF_TEXT)
6680 if (lpLVItem->mask & LVIF_NORECOMPUTE)
6681 /* reset mask */
6682 dispInfo.item.mask &= ~(LVIF_TEXT | LVIF_NORECOMPUTE);
6683 else
6685 dispInfo.item.pszText = lpLVItem->pszText;
6686 dispInfo.item.cchTextMax = lpLVItem->cchTextMax;
6689 if (lpLVItem->mask & LVIF_STATE)
6690 dispInfo.item.stateMask = lpLVItem->stateMask & infoPtr->uCallbackMask;
6691 /* could be zeroed on LVIF_NORECOMPUTE case */
6692 if (dispInfo.item.mask)
6694 notify_dispinfoT(infoPtr, LVN_GETDISPINFOW, &dispInfo, isW);
6695 dispInfo.item.stateMask = lpLVItem->stateMask;
6696 if (lpLVItem->mask & (LVIF_GROUPID|LVIF_COLUMNS))
6698 /* full size structure expected - _WIN32IE >= 0x560 */
6699 *lpLVItem = dispInfo.item;
6701 else if (lpLVItem->mask & LVIF_INDENT)
6703 /* indent member expected - _WIN32IE >= 0x300 */
6704 memcpy(lpLVItem, &dispInfo.item, offsetof( LVITEMW, iGroupId ));
6706 else
6708 /* minimal structure expected */
6709 memcpy(lpLVItem, &dispInfo.item, offsetof( LVITEMW, iIndent ));
6711 lpLVItem->mask = mask;
6712 TRACE(" getdispinfo(1):lpLVItem=%s\n", debuglvitem_t(lpLVItem, isW));
6716 /* make sure lParam is zeroed out */
6717 if (lpLVItem->mask & LVIF_PARAM) lpLVItem->lParam = 0;
6719 /* callback marked pointer required here */
6720 if ((lpLVItem->mask & LVIF_TEXT) && (lpLVItem->mask & LVIF_NORECOMPUTE))
6721 lpLVItem->pszText = LPSTR_TEXTCALLBACKW;
6723 /* we store only a little state, so if we're not asked, we're done */
6724 if (!(lpLVItem->mask & LVIF_STATE) || lpLVItem->iSubItem) return TRUE;
6726 /* if focus is handled by us, report it */
6727 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_FOCUSED )
6729 lpLVItem->state &= ~LVIS_FOCUSED;
6730 if (infoPtr->nFocusedItem == lpLVItem->iItem)
6731 lpLVItem->state |= LVIS_FOCUSED;
6734 /* and do the same for selection, if we handle it */
6735 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_SELECTED )
6737 lpLVItem->state &= ~LVIS_SELECTED;
6738 if (ranges_contain(infoPtr->selectionRanges, lpLVItem->iItem))
6739 lpLVItem->state |= LVIS_SELECTED;
6742 return TRUE;
6745 /* find the item and subitem structures before we proceed */
6746 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, lpLVItem->iItem);
6747 lpItem = DPA_GetPtr(hdpaSubItems, 0);
6748 assert (lpItem);
6750 if (lpLVItem->iSubItem)
6752 SUBITEM_INFO *lpSubItem = LISTVIEW_GetSubItemPtr(hdpaSubItems, lpLVItem->iSubItem);
6753 if (lpSubItem)
6754 pItemHdr = &lpSubItem->hdr;
6755 else
6757 pItemHdr = &callbackHdr;
6758 is_subitem_invalid = TRUE;
6761 else
6762 pItemHdr = &lpItem->hdr;
6764 /* Do we need to query the state from the app? */
6765 if ((lpLVItem->mask & LVIF_STATE) && infoPtr->uCallbackMask && (!lpLVItem->iSubItem || is_subitem_invalid))
6767 dispInfo.item.mask |= LVIF_STATE;
6768 dispInfo.item.stateMask = infoPtr->uCallbackMask;
6771 /* Do we need to enquire about the image? */
6772 if ((lpLVItem->mask & LVIF_IMAGE) && pItemHdr->iImage == I_IMAGECALLBACK &&
6773 (!lpLVItem->iSubItem || (infoPtr->dwLvExStyle & LVS_EX_SUBITEMIMAGES)))
6775 dispInfo.item.mask |= LVIF_IMAGE;
6776 dispInfo.item.iImage = I_IMAGECALLBACK;
6779 /* Only items support indentation */
6780 if ((lpLVItem->mask & LVIF_INDENT) && lpItem->iIndent == I_INDENTCALLBACK && !lpLVItem->iSubItem)
6782 dispInfo.item.mask |= LVIF_INDENT;
6783 dispInfo.item.iIndent = I_INDENTCALLBACK;
6786 /* Apps depend on calling back for text if it is NULL or LPSTR_TEXTCALLBACKW */
6787 if ((lpLVItem->mask & LVIF_TEXT) && !(lpLVItem->mask & LVIF_NORECOMPUTE) &&
6788 !is_text(pItemHdr->pszText))
6790 dispInfo.item.mask |= LVIF_TEXT;
6791 dispInfo.item.pszText = lpLVItem->pszText;
6792 dispInfo.item.cchTextMax = lpLVItem->cchTextMax;
6793 if (dispInfo.item.pszText && dispInfo.item.cchTextMax > 0)
6794 *dispInfo.item.pszText = '\0';
6797 /* If we don't have all the requested info, query the application */
6798 if (dispInfo.item.mask)
6800 dispInfo.item.iItem = lpLVItem->iItem;
6801 dispInfo.item.iSubItem = lpLVItem->iSubItem;
6802 dispInfo.item.lParam = lpItem->lParam;
6803 notify_dispinfoT(infoPtr, LVN_GETDISPINFOW, &dispInfo, isW);
6804 TRACE(" getdispinfo(2):item=%s\n", debuglvitem_t(&dispInfo.item, isW));
6807 /* we should not store values for subitems */
6808 if (lpLVItem->iSubItem) dispInfo.item.mask &= ~LVIF_DI_SETITEM;
6810 /* Now, handle the iImage field */
6811 if (dispInfo.item.mask & LVIF_IMAGE)
6813 lpLVItem->iImage = dispInfo.item.iImage;
6814 if ((dispInfo.item.mask & LVIF_DI_SETITEM) && pItemHdr->iImage == I_IMAGECALLBACK)
6815 pItemHdr->iImage = dispInfo.item.iImage;
6817 else if (lpLVItem->mask & LVIF_IMAGE)
6819 if (!lpLVItem->iSubItem || (infoPtr->dwLvExStyle & LVS_EX_SUBITEMIMAGES))
6820 lpLVItem->iImage = pItemHdr->iImage;
6821 else
6822 lpLVItem->iImage = 0;
6825 /* The pszText field */
6826 if (dispInfo.item.mask & LVIF_TEXT)
6828 if ((dispInfo.item.mask & LVIF_DI_SETITEM) && pItemHdr->pszText)
6829 textsetptrT(&pItemHdr->pszText, dispInfo.item.pszText, isW);
6831 lpLVItem->pszText = dispInfo.item.pszText;
6833 else if (lpLVItem->mask & LVIF_TEXT)
6835 /* if LVN_GETDISPINFO's disabled with LVIF_NORECOMPUTE return callback placeholder */
6836 if (isW || !is_text(pItemHdr->pszText)) lpLVItem->pszText = pItemHdr->pszText;
6837 else textcpynT(lpLVItem->pszText, isW, pItemHdr->pszText, TRUE, lpLVItem->cchTextMax);
6840 /* Next is the lParam field */
6841 if (dispInfo.item.mask & LVIF_PARAM)
6843 lpLVItem->lParam = dispInfo.item.lParam;
6844 if ((dispInfo.item.mask & LVIF_DI_SETITEM))
6845 lpItem->lParam = dispInfo.item.lParam;
6847 else if (lpLVItem->mask & LVIF_PARAM)
6848 lpLVItem->lParam = lpItem->lParam;
6850 /* if this is a subitem, we're done */
6851 if (lpLVItem->iSubItem) return TRUE;
6853 /* ... the state field (this one is different due to uCallbackmask) */
6854 if (lpLVItem->mask & LVIF_STATE)
6856 lpLVItem->state = lpItem->state & lpLVItem->stateMask;
6857 if (dispInfo.item.mask & LVIF_STATE)
6859 lpLVItem->state &= ~dispInfo.item.stateMask;
6860 lpLVItem->state |= (dispInfo.item.state & dispInfo.item.stateMask);
6862 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_FOCUSED )
6864 lpLVItem->state &= ~LVIS_FOCUSED;
6865 if (infoPtr->nFocusedItem == lpLVItem->iItem)
6866 lpLVItem->state |= LVIS_FOCUSED;
6868 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_SELECTED )
6870 lpLVItem->state &= ~LVIS_SELECTED;
6871 if (ranges_contain(infoPtr->selectionRanges, lpLVItem->iItem))
6872 lpLVItem->state |= LVIS_SELECTED;
6876 /* and last, but not least, the indent field */
6877 if (dispInfo.item.mask & LVIF_INDENT)
6879 lpLVItem->iIndent = dispInfo.item.iIndent;
6880 if ((dispInfo.item.mask & LVIF_DI_SETITEM) && lpItem->iIndent == I_INDENTCALLBACK)
6881 lpItem->iIndent = dispInfo.item.iIndent;
6883 else if (lpLVItem->mask & LVIF_INDENT)
6885 lpLVItem->iIndent = lpItem->iIndent;
6888 return TRUE;
6891 /***
6892 * DESCRIPTION:
6893 * Retrieves item attributes.
6895 * PARAMETER(S):
6896 * [I] hwnd : window handle
6897 * [IO] lpLVItem : item info
6898 * [I] isW : if TRUE, then lpLVItem is a LPLVITEMW,
6899 * if FALSE, then lpLVItem is a LPLVITEMA.
6901 * NOTE:
6902 * This is the external 'GetItem' interface -- it properly copies
6903 * the text in the provided buffer.
6905 * RETURN:
6906 * SUCCESS : TRUE
6907 * FAILURE : FALSE
6909 static BOOL LISTVIEW_GetItemExtT(const LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem, BOOL isW)
6911 LPWSTR pszText;
6912 BOOL bResult;
6914 if (!lpLVItem || lpLVItem->iItem < 0 || lpLVItem->iItem >= infoPtr->nItemCount)
6915 return FALSE;
6917 pszText = lpLVItem->pszText;
6918 bResult = LISTVIEW_GetItemT(infoPtr, lpLVItem, isW);
6919 if (bResult && (lpLVItem->mask & LVIF_TEXT) && lpLVItem->pszText != pszText)
6921 if (lpLVItem->pszText != LPSTR_TEXTCALLBACKW)
6922 textcpynT(pszText, isW, lpLVItem->pszText, isW, lpLVItem->cchTextMax);
6923 else
6924 pszText = LPSTR_TEXTCALLBACKW;
6926 lpLVItem->pszText = pszText;
6928 return bResult;
6932 /***
6933 * DESCRIPTION:
6934 * Retrieves the position (upper-left) of the listview control item.
6935 * Note that for LVS_ICON style, the upper-left is that of the icon
6936 * and not the bounding box.
6938 * PARAMETER(S):
6939 * [I] infoPtr : valid pointer to the listview structure
6940 * [I] nItem : item index
6941 * [O] lpptPosition : coordinate information
6943 * RETURN:
6944 * SUCCESS : TRUE
6945 * FAILURE : FALSE
6947 static BOOL LISTVIEW_GetItemPosition(const LISTVIEW_INFO *infoPtr, INT nItem, LPPOINT lpptPosition)
6949 POINT Origin;
6951 TRACE("(nItem=%d, lpptPosition=%p)\n", nItem, lpptPosition);
6953 if (!lpptPosition || nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
6955 LISTVIEW_GetOrigin(infoPtr, &Origin);
6956 LISTVIEW_GetItemOrigin(infoPtr, nItem, lpptPosition);
6958 if (infoPtr->uView == LV_VIEW_ICON)
6960 lpptPosition->x += (infoPtr->nItemWidth - infoPtr->iconSize.cx) / 2;
6961 lpptPosition->y += ICON_TOP_PADDING;
6963 lpptPosition->x += Origin.x;
6964 lpptPosition->y += Origin.y;
6966 TRACE (" lpptPosition=%s\n", wine_dbgstr_point(lpptPosition));
6967 return TRUE;
6971 /***
6972 * DESCRIPTION:
6973 * Retrieves the bounding rectangle for a listview control item.
6975 * PARAMETER(S):
6976 * [I] infoPtr : valid pointer to the listview structure
6977 * [I] nItem : item index
6978 * [IO] lprc : bounding rectangle coordinates
6979 * lprc->left specifies the portion of the item for which the bounding
6980 * rectangle will be retrieved.
6982 * LVIR_BOUNDS Returns the bounding rectangle of the entire item,
6983 * including the icon and label.
6985 * * For LVS_ICON
6986 * * Experiment shows that native control returns:
6987 * * width = min (48, length of text line)
6988 * * .left = position.x - (width - iconsize.cx)/2
6989 * * .right = .left + width
6990 * * height = #lines of text * ntmHeight + icon height + 8
6991 * * .top = position.y - 2
6992 * * .bottom = .top + height
6993 * * separation between items .y = itemSpacing.cy - height
6994 * * .x = itemSpacing.cx - width
6995 * LVIR_ICON Returns the bounding rectangle of the icon or small icon.
6997 * * For LVS_ICON
6998 * * Experiment shows that native control returns:
6999 * * width = iconSize.cx + 16
7000 * * .left = position.x - (width - iconsize.cx)/2
7001 * * .right = .left + width
7002 * * height = iconSize.cy + 4
7003 * * .top = position.y - 2
7004 * * .bottom = .top + height
7005 * * separation between items .y = itemSpacing.cy - height
7006 * * .x = itemSpacing.cx - width
7007 * LVIR_LABEL Returns the bounding rectangle of the item text.
7009 * * For LVS_ICON
7010 * * Experiment shows that native control returns:
7011 * * width = text length
7012 * * .left = position.x - width/2
7013 * * .right = .left + width
7014 * * height = ntmH * linecount + 2
7015 * * .top = position.y + iconSize.cy + 6
7016 * * .bottom = .top + height
7017 * * separation between items .y = itemSpacing.cy - height
7018 * * .x = itemSpacing.cx - width
7019 * LVIR_SELECTBOUNDS Returns the union of the LVIR_ICON and LVIR_LABEL
7020 * rectangles, but excludes columns in report view.
7022 * RETURN:
7023 * SUCCESS : TRUE
7024 * FAILURE : FALSE
7026 * NOTES
7027 * Note that the bounding rectangle of the label in the LVS_ICON view depends
7028 * upon whether the window has the focus currently and on whether the item
7029 * is the one with the focus. Ensure that the control's record of which
7030 * item has the focus agrees with the items' records.
7032 static BOOL LISTVIEW_GetItemRect(const LISTVIEW_INFO *infoPtr, INT nItem, LPRECT lprc)
7034 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
7035 BOOL doLabel = TRUE, oversizedBox = FALSE;
7036 POINT Position, Origin;
7037 LVITEMW lvItem;
7038 LONG mode;
7040 TRACE("(hwnd=%p, nItem=%d, lprc=%p)\n", infoPtr->hwndSelf, nItem, lprc);
7042 if (!lprc || nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
7044 LISTVIEW_GetOrigin(infoPtr, &Origin);
7045 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
7047 /* Be smart and try to figure out the minimum we have to do */
7048 if (lprc->left == LVIR_ICON) doLabel = FALSE;
7049 if (infoPtr->uView == LV_VIEW_DETAILS && lprc->left == LVIR_BOUNDS) doLabel = FALSE;
7050 if (infoPtr->uView == LV_VIEW_ICON && lprc->left != LVIR_ICON &&
7051 infoPtr->bFocus && LISTVIEW_GetItemState(infoPtr, nItem, LVIS_FOCUSED))
7052 oversizedBox = TRUE;
7054 /* get what we need from the item before hand, so we make
7055 * only one request. This can speed up things, if data
7056 * is stored on the app side */
7057 lvItem.mask = 0;
7058 if (infoPtr->uView == LV_VIEW_DETAILS) lvItem.mask |= LVIF_INDENT;
7059 if (doLabel) lvItem.mask |= LVIF_TEXT;
7060 lvItem.iItem = nItem;
7061 lvItem.iSubItem = 0;
7062 lvItem.pszText = szDispText;
7063 lvItem.cchTextMax = DISP_TEXT_SIZE;
7064 if (lvItem.mask && !LISTVIEW_GetItemW(infoPtr, &lvItem)) return FALSE;
7065 /* we got the state already up, simulate it here, to avoid a reget */
7066 if (infoPtr->uView == LV_VIEW_ICON && (lprc->left != LVIR_ICON))
7068 lvItem.mask |= LVIF_STATE;
7069 lvItem.stateMask = LVIS_FOCUSED;
7070 lvItem.state = (oversizedBox ? LVIS_FOCUSED : 0);
7073 if (infoPtr->uView == LV_VIEW_DETAILS && (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) && lprc->left == LVIR_SELECTBOUNDS)
7074 lprc->left = LVIR_BOUNDS;
7076 mode = lprc->left;
7077 switch(lprc->left)
7079 case LVIR_ICON:
7080 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, NULL, lprc, NULL, NULL);
7081 break;
7083 case LVIR_LABEL:
7084 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, NULL, NULL, NULL, lprc);
7085 break;
7087 case LVIR_BOUNDS:
7088 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, lprc, NULL, NULL, NULL, NULL);
7089 break;
7091 case LVIR_SELECTBOUNDS:
7092 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, lprc, NULL, NULL, NULL);
7093 break;
7095 default:
7096 WARN("Unknown value: %ld\n", lprc->left);
7097 return FALSE;
7100 if (infoPtr->uView == LV_VIEW_DETAILS)
7102 if (mode != LVIR_BOUNDS)
7103 OffsetRect(lprc, Origin.x + LISTVIEW_GetColumnInfo(infoPtr, 0)->rcHeader.left,
7104 Position.y + Origin.y);
7105 else
7106 OffsetRect(lprc, Origin.x, Position.y + Origin.y);
7108 else
7109 OffsetRect(lprc, Position.x + Origin.x, Position.y + Origin.y);
7111 TRACE(" rect=%s\n", wine_dbgstr_rect(lprc));
7113 return TRUE;
7116 /***
7117 * DESCRIPTION:
7118 * Retrieves the spacing between listview control items.
7120 * PARAMETER(S):
7121 * [I] infoPtr : valid pointer to the listview structure
7122 * [IO] lprc : rectangle to receive the output
7123 * on input, lprc->top = nSubItem
7124 * lprc->left = LVIR_ICON | LVIR_BOUNDS | LVIR_LABEL
7126 * NOTE: for subItem = 0, we should return the bounds of the _entire_ item,
7127 * not only those of the first column.
7129 * RETURN:
7130 * TRUE: success
7131 * FALSE: failure
7133 static BOOL LISTVIEW_GetSubItemRect(const LISTVIEW_INFO *infoPtr, INT item, LPRECT lprc)
7135 RECT rect = { 0, 0, 0, 0 };
7136 POINT origin;
7137 INT y;
7139 if (!lprc) return FALSE;
7141 TRACE("item %d, subitem %ld, type %ld\n", item, lprc->top, lprc->left);
7143 /* Subitem of '0' means item itself, and this works for all control view modes */
7144 if (lprc->top == 0)
7145 return LISTVIEW_GetItemRect(infoPtr, item, lprc);
7147 if (infoPtr->uView != LV_VIEW_DETAILS) return FALSE;
7149 LISTVIEW_GetOrigin(infoPtr, &origin);
7150 /* this works for any item index, no matter if it exists or not */
7151 y = item * infoPtr->nItemHeight + origin.y;
7153 if (infoPtr->hwndHeader && SendMessageW(infoPtr->hwndHeader, HDM_GETITEMRECT, lprc->top, (LPARAM)&rect))
7155 rect.top = 0;
7156 rect.bottom = infoPtr->nItemHeight;
7158 else
7160 /* Native implementation is broken for this case and garbage is left for left and right fields,
7161 we zero them to get predictable output */
7162 lprc->left = lprc->right = lprc->top = 0;
7163 lprc->bottom = infoPtr->nItemHeight;
7164 OffsetRect(lprc, origin.x, y);
7165 TRACE("return rect %s\n", wine_dbgstr_rect(lprc));
7166 return TRUE;
7169 switch (lprc->left)
7171 case LVIR_ICON:
7173 /* it doesn't matter if main item actually has an icon, if imagelist is set icon width is returned */
7174 if (infoPtr->himlSmall)
7175 rect.right = rect.left + infoPtr->iconSize.cx;
7176 else
7177 rect.right = rect.left;
7179 rect.bottom = rect.top + infoPtr->iconSize.cy;
7180 break;
7182 case LVIR_LABEL:
7183 case LVIR_BOUNDS:
7184 break;
7186 default:
7187 ERR("Unknown bounds %ld\n", lprc->left);
7188 return FALSE;
7191 OffsetRect(&rect, origin.x, y);
7192 *lprc = rect;
7193 TRACE("return rect %s\n", wine_dbgstr_rect(lprc));
7195 return TRUE;
7198 /***
7199 * DESCRIPTION:
7200 * Retrieves the spacing between listview control items.
7202 * PARAMETER(S):
7203 * [I] infoPtr : valid pointer to the listview structure
7204 * [I] bSmall : flag for small or large icon
7206 * RETURN:
7207 * Horizontal + vertical spacing
7209 static LONG LISTVIEW_GetItemSpacing(const LISTVIEW_INFO *infoPtr, BOOL bSmall)
7211 LONG lResult;
7213 if (!bSmall)
7215 lResult = MAKELONG(infoPtr->iconSpacing.cx, infoPtr->iconSpacing.cy);
7217 else
7219 if (infoPtr->uView == LV_VIEW_ICON)
7220 lResult = MAKELONG(DEFAULT_COLUMN_WIDTH, GetSystemMetrics(SM_CXSMICON)+HEIGHT_PADDING);
7221 else
7222 lResult = MAKELONG(infoPtr->nItemWidth, infoPtr->nItemHeight);
7224 return lResult;
7227 /***
7228 * DESCRIPTION:
7229 * Retrieves the state of a listview control item.
7231 * PARAMETER(S):
7232 * [I] infoPtr : valid pointer to the listview structure
7233 * [I] nItem : item index
7234 * [I] uMask : state mask
7236 * RETURN:
7237 * State specified by the mask.
7239 static UINT LISTVIEW_GetItemState(const LISTVIEW_INFO *infoPtr, INT nItem, UINT uMask)
7241 LVITEMW lvItem;
7243 if (nItem < 0 || nItem >= infoPtr->nItemCount) return 0;
7245 lvItem.iItem = nItem;
7246 lvItem.iSubItem = 0;
7247 lvItem.mask = LVIF_STATE;
7248 lvItem.stateMask = uMask;
7249 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return 0;
7251 return lvItem.state & uMask;
7254 /***
7255 * DESCRIPTION:
7256 * Retrieves the text of a listview control item or subitem.
7258 * PARAMETER(S):
7259 * [I] hwnd : window handle
7260 * [I] nItem : item index
7261 * [IO] lpLVItem : item information
7262 * [I] isW : TRUE if lpLVItem is Unicode
7264 * RETURN:
7265 * SUCCESS : string length
7266 * FAILURE : 0
7268 static INT LISTVIEW_GetItemTextT(const LISTVIEW_INFO *infoPtr, INT nItem, LPLVITEMW lpLVItem, BOOL isW)
7270 if (!lpLVItem || nItem < 0 || nItem >= infoPtr->nItemCount) return 0;
7272 lpLVItem->mask = LVIF_TEXT;
7273 lpLVItem->iItem = nItem;
7274 if (!LISTVIEW_GetItemExtT(infoPtr, lpLVItem, isW)) return 0;
7276 return textlenT(lpLVItem->pszText, isW);
7279 /***
7280 * DESCRIPTION:
7281 * Searches for an item based on properties + relationships.
7283 * PARAMETER(S):
7284 * [I] infoPtr : valid pointer to the listview structure
7285 * [I] nItem : item index
7286 * [I] uFlags : relationship flag
7288 * RETURN:
7289 * SUCCESS : item index
7290 * FAILURE : -1
7292 static INT LISTVIEW_GetNextItem(const LISTVIEW_INFO *infoPtr, INT nItem, UINT uFlags)
7294 UINT uMask = 0;
7295 LVFINDINFOW lvFindInfo;
7296 INT nCountPerColumn;
7297 INT nCountPerRow;
7298 INT i;
7300 TRACE("nItem=%d, uFlags=%x, nItemCount=%d\n", nItem, uFlags, infoPtr->nItemCount);
7301 if (nItem < -1 || nItem >= infoPtr->nItemCount) return -1;
7303 ZeroMemory(&lvFindInfo, sizeof(lvFindInfo));
7305 if (uFlags & LVNI_CUT)
7306 uMask |= LVIS_CUT;
7308 if (uFlags & LVNI_DROPHILITED)
7309 uMask |= LVIS_DROPHILITED;
7311 if (uFlags & LVNI_FOCUSED)
7312 uMask |= LVIS_FOCUSED;
7314 if (uFlags & LVNI_SELECTED)
7315 uMask |= LVIS_SELECTED;
7317 /* if we're asked for the focused item, that's only one,
7318 * so it's worth optimizing */
7319 if (uFlags & LVNI_FOCUSED)
7321 if ((LISTVIEW_GetItemState(infoPtr, infoPtr->nFocusedItem, uMask) & uMask) != uMask) return -1;
7322 return (infoPtr->nFocusedItem == nItem) ? -1 : infoPtr->nFocusedItem;
7325 if (uFlags & LVNI_ABOVE)
7327 if ((infoPtr->uView == LV_VIEW_LIST) || (infoPtr->uView == LV_VIEW_DETAILS))
7329 while (nItem >= 0)
7331 nItem--;
7332 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7333 return nItem;
7336 else
7338 /* Special case for autoarrange - move 'til the top of a list */
7339 if (is_autoarrange(infoPtr))
7341 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
7342 while (nItem - nCountPerRow >= 0)
7344 nItem -= nCountPerRow;
7345 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7346 return nItem;
7348 return -1;
7350 lvFindInfo.flags = LVFI_NEARESTXY;
7351 lvFindInfo.vkDirection = VK_UP;
7352 LISTVIEW_GetItemPosition(infoPtr, nItem, &lvFindInfo.pt);
7353 while ((nItem = LISTVIEW_FindItemW(infoPtr, nItem, &lvFindInfo)) != -1)
7355 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7356 return nItem;
7360 else if (uFlags & LVNI_BELOW)
7362 if ((infoPtr->uView == LV_VIEW_LIST) || (infoPtr->uView == LV_VIEW_DETAILS))
7364 while (nItem < infoPtr->nItemCount)
7366 nItem++;
7367 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7368 return nItem;
7371 else
7373 /* Special case for autoarrange - move 'til the bottom of a list */
7374 if (is_autoarrange(infoPtr))
7376 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
7377 while (nItem + nCountPerRow < infoPtr->nItemCount )
7379 nItem += nCountPerRow;
7380 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7381 return nItem;
7383 return -1;
7385 lvFindInfo.flags = LVFI_NEARESTXY;
7386 lvFindInfo.vkDirection = VK_DOWN;
7387 LISTVIEW_GetItemPosition(infoPtr, nItem, &lvFindInfo.pt);
7388 while ((nItem = LISTVIEW_FindItemW(infoPtr, nItem, &lvFindInfo)) != -1)
7390 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7391 return nItem;
7395 else if (uFlags & LVNI_TOLEFT)
7397 if (infoPtr->uView == LV_VIEW_LIST)
7399 nCountPerColumn = LISTVIEW_GetCountPerColumn(infoPtr);
7400 while (nItem - nCountPerColumn >= 0)
7402 nItem -= nCountPerColumn;
7403 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7404 return nItem;
7407 else if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
7409 /* Special case for autoarrange - move 'til the beginning of a row */
7410 if (is_autoarrange(infoPtr))
7412 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
7413 while (nItem % nCountPerRow > 0)
7415 nItem --;
7416 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7417 return nItem;
7419 return -1;
7421 lvFindInfo.flags = LVFI_NEARESTXY;
7422 lvFindInfo.vkDirection = VK_LEFT;
7423 LISTVIEW_GetItemPosition(infoPtr, nItem, &lvFindInfo.pt);
7424 while ((nItem = LISTVIEW_FindItemW(infoPtr, nItem, &lvFindInfo)) != -1)
7426 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7427 return nItem;
7431 else if (uFlags & LVNI_TORIGHT)
7433 if (infoPtr->uView == LV_VIEW_LIST)
7435 nCountPerColumn = LISTVIEW_GetCountPerColumn(infoPtr);
7436 while (nItem + nCountPerColumn < infoPtr->nItemCount)
7438 nItem += nCountPerColumn;
7439 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7440 return nItem;
7443 else if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
7445 /* Special case for autoarrange - move 'til the end of a row */
7446 if (is_autoarrange(infoPtr))
7448 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
7449 while (nItem % nCountPerRow < nCountPerRow - 1 )
7451 nItem ++;
7452 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7453 return nItem;
7455 return -1;
7457 lvFindInfo.flags = LVFI_NEARESTXY;
7458 lvFindInfo.vkDirection = VK_RIGHT;
7459 LISTVIEW_GetItemPosition(infoPtr, nItem, &lvFindInfo.pt);
7460 while ((nItem = LISTVIEW_FindItemW(infoPtr, nItem, &lvFindInfo)) != -1)
7462 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7463 return nItem;
7467 else
7469 nItem++;
7471 /* search by index */
7472 for (i = nItem; i < infoPtr->nItemCount; i++)
7474 if ((LISTVIEW_GetItemState(infoPtr, i, uMask) & uMask) == uMask)
7475 return i;
7479 return -1;
7482 static BOOL LISTVIEW_GetNextItemIndex(const LISTVIEW_INFO *infoPtr, LVITEMINDEX *index, UINT flags)
7484 /* FIXME: specified item group is ignored */
7486 if (!index)
7487 return FALSE;
7489 index->iItem = LISTVIEW_GetNextItem(infoPtr, index->iItem, flags);
7490 return index->iItem != -1;
7493 /* LISTVIEW_GetNumberOfWorkAreas */
7495 /***
7496 * DESCRIPTION:
7497 * Retrieves the origin coordinates when in icon or small icon display mode.
7499 * PARAMETER(S):
7500 * [I] infoPtr : valid pointer to the listview structure
7501 * [O] lpptOrigin : coordinate information
7503 * RETURN:
7504 * None.
7506 static void LISTVIEW_GetOrigin(const LISTVIEW_INFO *infoPtr, LPPOINT lpptOrigin)
7508 INT nHorzPos = 0, nVertPos = 0;
7509 SCROLLINFO scrollInfo;
7511 scrollInfo.cbSize = sizeof(SCROLLINFO);
7512 scrollInfo.fMask = SIF_POS;
7514 if (GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo))
7515 nHorzPos = scrollInfo.nPos;
7516 if (GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
7517 nVertPos = scrollInfo.nPos;
7519 TRACE("nHorzPos=%d, nVertPos=%d\n", nHorzPos, nVertPos);
7521 lpptOrigin->x = infoPtr->rcList.left;
7522 lpptOrigin->y = infoPtr->rcList.top;
7523 if (infoPtr->uView == LV_VIEW_LIST)
7524 nHorzPos *= infoPtr->nItemWidth;
7525 else if (infoPtr->uView == LV_VIEW_DETAILS)
7526 nVertPos *= infoPtr->nItemHeight;
7528 lpptOrigin->x -= nHorzPos;
7529 lpptOrigin->y -= nVertPos;
7531 TRACE(" origin=%s\n", wine_dbgstr_point(lpptOrigin));
7534 /***
7535 * DESCRIPTION:
7536 * Retrieves the width of a string.
7538 * PARAMETER(S):
7539 * [I] hwnd : window handle
7540 * [I] lpszText : text string to process
7541 * [I] isW : TRUE if lpszText is Unicode, FALSE otherwise
7543 * RETURN:
7544 * SUCCESS : string width (in pixels)
7545 * FAILURE : zero
7547 static INT LISTVIEW_GetStringWidthT(const LISTVIEW_INFO *infoPtr, LPCWSTR lpszText, BOOL isW)
7549 SIZE stringSize;
7551 stringSize.cx = 0;
7552 if (is_text(lpszText))
7554 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
7555 HDC hdc = GetDC(infoPtr->hwndSelf);
7556 HFONT hOldFont = SelectObject(hdc, hFont);
7558 if (isW)
7559 GetTextExtentPointW(hdc, lpszText, lstrlenW(lpszText), &stringSize);
7560 else
7561 GetTextExtentPointA(hdc, (LPCSTR)lpszText, lstrlenA((LPCSTR)lpszText), &stringSize);
7562 SelectObject(hdc, hOldFont);
7563 ReleaseDC(infoPtr->hwndSelf, hdc);
7565 return stringSize.cx;
7568 /***
7569 * DESCRIPTION:
7570 * Determines which listview item is located at the specified position.
7572 * PARAMETER(S):
7573 * [I] infoPtr : valid pointer to the listview structure
7574 * [IO] lpht : hit test information
7575 * [I] subitem : fill out iSubItem.
7576 * [I] select : return the index only if the hit selects the item
7578 * NOTE:
7579 * (mm 20001022): We must not allow iSubItem to be touched, for
7580 * an app might pass only a structure with space up to iItem!
7581 * (MS Office 97 does that for instance in the file open dialog)
7583 * RETURN:
7584 * SUCCESS : item index
7585 * FAILURE : -1
7587 static INT LISTVIEW_HitTest(const LISTVIEW_INFO *infoPtr, LPLVHITTESTINFO lpht, BOOL subitem, BOOL select)
7589 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
7590 RECT rcBox, rcBounds, rcState, rcIcon, rcLabel, rcSearch;
7591 POINT Origin, Position, opt;
7592 BOOL is_fullrow;
7593 LVITEMW lvItem;
7594 ITERATOR i;
7595 INT iItem;
7597 TRACE("(pt=%s, subitem=%d, select=%d)\n", wine_dbgstr_point(&lpht->pt), subitem, select);
7599 lpht->flags = 0;
7600 lpht->iItem = -1;
7601 if (subitem) lpht->iSubItem = 0;
7603 LISTVIEW_GetOrigin(infoPtr, &Origin);
7605 /* set whole list relation flags */
7606 if (subitem && infoPtr->uView == LV_VIEW_DETAILS)
7608 /* LVM_SUBITEMHITTEST checks left bound of possible client area */
7609 if (infoPtr->rcList.left > lpht->pt.x && Origin.x < lpht->pt.x)
7610 lpht->flags |= LVHT_TOLEFT;
7612 if (lpht->pt.y < infoPtr->rcList.top && lpht->pt.y >= 0)
7613 opt.y = lpht->pt.y + infoPtr->rcList.top;
7614 else
7615 opt.y = lpht->pt.y;
7617 if (infoPtr->rcList.bottom < opt.y)
7618 lpht->flags |= LVHT_BELOW;
7620 else
7622 if (infoPtr->rcList.left > lpht->pt.x)
7623 lpht->flags |= LVHT_TOLEFT;
7624 else if (infoPtr->rcList.right < lpht->pt.x)
7625 lpht->flags |= LVHT_TORIGHT;
7627 if (infoPtr->rcList.top > lpht->pt.y)
7628 lpht->flags |= LVHT_ABOVE;
7629 else if (infoPtr->rcList.bottom < lpht->pt.y)
7630 lpht->flags |= LVHT_BELOW;
7633 /* even if item is invalid try to find subitem */
7634 if (infoPtr->uView == LV_VIEW_DETAILS && subitem)
7636 RECT *pRect;
7637 INT j;
7639 opt.x = lpht->pt.x - Origin.x;
7641 lpht->iSubItem = -1;
7642 for (j = 0; j < DPA_GetPtrCount(infoPtr->hdpaColumns); j++)
7644 pRect = &LISTVIEW_GetColumnInfo(infoPtr, j)->rcHeader;
7646 if ((opt.x >= pRect->left) && (opt.x < pRect->right))
7648 lpht->iSubItem = j;
7649 break;
7652 TRACE("lpht->iSubItem=%d\n", lpht->iSubItem);
7654 /* if we're outside horizontal columns bounds there's nothing to test further */
7655 if (lpht->iSubItem == -1)
7657 lpht->iItem = -1;
7658 lpht->flags = LVHT_NOWHERE;
7659 return -1;
7663 TRACE("lpht->flags=0x%x\n", lpht->flags);
7664 if (lpht->flags) return -1;
7666 lpht->flags |= LVHT_NOWHERE;
7668 /* first deal with the large items */
7669 rcSearch.left = lpht->pt.x;
7670 rcSearch.top = lpht->pt.y;
7671 rcSearch.right = rcSearch.left + 1;
7672 rcSearch.bottom = rcSearch.top + 1;
7674 iterator_frameditems(&i, infoPtr, &rcSearch);
7675 iterator_next(&i); /* go to first item in the sequence */
7676 iItem = i.nItem;
7677 iterator_destroy(&i);
7679 TRACE("lpht->iItem=%d\n", iItem);
7680 if (iItem == -1) return -1;
7682 lvItem.mask = LVIF_STATE | LVIF_TEXT;
7683 if (infoPtr->uView == LV_VIEW_DETAILS) lvItem.mask |= LVIF_INDENT;
7684 lvItem.stateMask = LVIS_STATEIMAGEMASK;
7685 if (infoPtr->uView == LV_VIEW_ICON) lvItem.stateMask |= LVIS_FOCUSED;
7686 lvItem.iItem = iItem;
7687 lvItem.iSubItem = subitem ? lpht->iSubItem : 0;
7688 lvItem.pszText = szDispText;
7689 lvItem.cchTextMax = DISP_TEXT_SIZE;
7690 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return -1;
7691 if (!infoPtr->bFocus) lvItem.state &= ~LVIS_FOCUSED;
7693 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, &rcBox, NULL, &rcIcon, &rcState, &rcLabel);
7694 LISTVIEW_GetItemOrigin(infoPtr, iItem, &Position);
7695 opt.x = lpht->pt.x - Position.x - Origin.x;
7697 if (lpht->pt.y < infoPtr->rcList.top && lpht->pt.y >= 0)
7698 opt.y = lpht->pt.y - Position.y - Origin.y + infoPtr->rcList.top;
7699 else
7700 opt.y = lpht->pt.y - Position.y - Origin.y;
7702 if (infoPtr->uView == LV_VIEW_DETAILS)
7704 rcBounds = rcBox;
7705 if (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT)
7706 opt.x = lpht->pt.x - Origin.x;
7708 else
7710 UnionRect(&rcBounds, &rcIcon, &rcLabel);
7711 UnionRect(&rcBounds, &rcBounds, &rcState);
7713 TRACE("rcBounds=%s\n", wine_dbgstr_rect(&rcBounds));
7714 if (!PtInRect(&rcBounds, opt)) return -1;
7716 /* That's a special case - row rectangle is used as item rectangle and
7717 returned flags contain all item parts. */
7718 is_fullrow = (infoPtr->uView == LV_VIEW_DETAILS) && ((infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) || (infoPtr->dwStyle & LVS_OWNERDRAWFIXED));
7720 if (PtInRect(&rcIcon, opt))
7721 lpht->flags |= LVHT_ONITEMICON;
7722 else if (PtInRect(&rcLabel, opt))
7723 lpht->flags |= LVHT_ONITEMLABEL;
7724 else if (infoPtr->himlState && PtInRect(&rcState, opt))
7725 lpht->flags |= LVHT_ONITEMSTATEICON;
7726 if (is_fullrow && !(lpht->flags & LVHT_ONITEM))
7728 lpht->flags = LVHT_ONITEM | LVHT_ABOVE;
7730 if (lpht->flags & LVHT_ONITEM)
7731 lpht->flags &= ~LVHT_NOWHERE;
7732 TRACE("lpht->flags=0x%x\n", lpht->flags);
7734 if (select && !is_fullrow)
7736 if (infoPtr->uView == LV_VIEW_DETAILS)
7738 /* get main item bounds */
7739 lvItem.iSubItem = 0;
7740 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, &rcBox, NULL, &rcIcon, &rcState, &rcLabel);
7741 UnionRect(&rcBounds, &rcIcon, &rcLabel);
7742 UnionRect(&rcBounds, &rcBounds, &rcState);
7744 if (!PtInRect(&rcBounds, opt)) iItem = -1;
7746 return lpht->iItem = iItem;
7749 /***
7750 * DESCRIPTION:
7751 * Inserts a new item in the listview control.
7753 * PARAMETER(S):
7754 * [I] infoPtr : valid pointer to the listview structure
7755 * [I] lpLVItem : item information
7756 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
7758 * RETURN:
7759 * SUCCESS : new item index
7760 * FAILURE : -1
7762 static INT LISTVIEW_InsertItemT(LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem, BOOL isW)
7764 INT nItem;
7765 HDPA hdpaSubItems;
7766 NMLISTVIEW nmlv;
7767 ITEM_INFO *lpItem;
7768 ITEM_ID *lpID;
7769 BOOL is_sorted, has_changed;
7770 LVITEMW item;
7771 HWND hwndSelf = infoPtr->hwndSelf;
7773 TRACE("(item=%s, isW=%d)\n", debuglvitem_t(lpLVItem, isW), isW);
7775 if (infoPtr->dwStyle & LVS_OWNERDATA) return infoPtr->nItemCount++;
7777 /* make sure it's an item, and not a subitem; cannot insert a subitem */
7778 if (!lpLVItem || lpLVItem->iSubItem) return -1;
7780 if (!is_assignable_item(lpLVItem, infoPtr->dwStyle)) return -1;
7782 if (!(lpItem = Alloc(sizeof(ITEM_INFO)))) return -1;
7784 /* insert item in listview control data structure */
7785 if ( !(hdpaSubItems = DPA_Create(8)) ) goto fail;
7786 if ( !DPA_SetPtr(hdpaSubItems, 0, lpItem) ) assert (FALSE);
7788 /* link with id struct */
7789 if (!(lpID = Alloc(sizeof(ITEM_ID)))) goto fail;
7790 lpItem->id = lpID;
7791 lpID->item = hdpaSubItems;
7792 lpID->id = get_next_itemid(infoPtr);
7793 if ( DPA_InsertPtr(infoPtr->hdpaItemIds, infoPtr->nItemCount, lpID) == -1) goto fail;
7795 is_sorted = (infoPtr->dwStyle & (LVS_SORTASCENDING | LVS_SORTDESCENDING)) &&
7796 !(infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (LPSTR_TEXTCALLBACKW != lpLVItem->pszText);
7798 if (lpLVItem->iItem < 0 && !is_sorted) return -1;
7800 /* calculate new item index */
7801 if (is_sorted)
7803 HDPA hItem;
7804 ITEM_INFO *item_s;
7805 INT i = 0, cmpv;
7806 WCHAR *textW;
7808 textW = textdupTtoW(lpLVItem->pszText, isW);
7810 while (i < infoPtr->nItemCount)
7812 hItem = DPA_GetPtr( infoPtr->hdpaItems, i);
7813 item_s = DPA_GetPtr(hItem, 0);
7815 cmpv = textcmpWT(item_s->hdr.pszText, textW, TRUE);
7816 if (infoPtr->dwStyle & LVS_SORTDESCENDING) cmpv *= -1;
7818 if (cmpv >= 0) break;
7819 i++;
7822 textfreeT(textW, isW);
7824 nItem = i;
7826 else
7827 nItem = min(lpLVItem->iItem, infoPtr->nItemCount);
7829 TRACE("inserting at %d, sorted=%d, count=%d, iItem=%d\n", nItem, is_sorted, infoPtr->nItemCount, lpLVItem->iItem);
7830 nItem = DPA_InsertPtr( infoPtr->hdpaItems, nItem, hdpaSubItems );
7831 if (nItem == -1) goto fail;
7832 infoPtr->nItemCount++;
7834 /* shift indices first so they don't get tangled */
7835 LISTVIEW_ShiftIndices(infoPtr, nItem, 1);
7837 /* set the item attributes */
7838 if (lpLVItem->mask & (LVIF_GROUPID|LVIF_COLUMNS))
7840 /* full size structure expected - _WIN32IE >= 0x560 */
7841 item = *lpLVItem;
7843 else if (lpLVItem->mask & LVIF_INDENT)
7845 /* indent member expected - _WIN32IE >= 0x300 */
7846 memcpy(&item, lpLVItem, offsetof( LVITEMW, iGroupId ));
7848 else
7850 /* minimal structure expected */
7851 memcpy(&item, lpLVItem, offsetof( LVITEMW, iIndent ));
7853 item.iItem = nItem;
7854 if (infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES)
7856 if (item.mask & LVIF_STATE)
7858 item.stateMask |= LVIS_STATEIMAGEMASK;
7859 item.state &= ~LVIS_STATEIMAGEMASK;
7860 item.state |= INDEXTOSTATEIMAGEMASK(1);
7862 else
7864 item.mask |= LVIF_STATE;
7865 item.stateMask = LVIS_STATEIMAGEMASK;
7866 item.state = INDEXTOSTATEIMAGEMASK(1);
7870 if (!set_main_item(infoPtr, &item, TRUE, isW, &has_changed)) goto undo;
7872 /* make room for the position, if we are in the right mode */
7873 if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
7875 if (DPA_InsertPtr(infoPtr->hdpaPosX, nItem, 0) == -1)
7876 goto undo;
7877 if (DPA_InsertPtr(infoPtr->hdpaPosY, nItem, 0) == -1)
7879 DPA_DeletePtr(infoPtr->hdpaPosX, nItem);
7880 goto undo;
7884 /* send LVN_INSERTITEM notification */
7885 memset(&nmlv, 0, sizeof(NMLISTVIEW));
7886 nmlv.iItem = nItem;
7887 nmlv.lParam = lpItem->lParam;
7888 notify_listview(infoPtr, LVN_INSERTITEM, &nmlv);
7889 if (!IsWindow(hwndSelf))
7890 return -1;
7892 /* align items (set position of each item) */
7893 if (infoPtr->uView == LV_VIEW_SMALLICON || infoPtr->uView == LV_VIEW_ICON)
7895 POINT pt;
7897 if (infoPtr->dwStyle & LVS_ALIGNLEFT)
7898 LISTVIEW_NextIconPosLeft(infoPtr, &pt);
7899 else
7900 LISTVIEW_NextIconPosTop(infoPtr, &pt);
7902 LISTVIEW_MoveIconTo(infoPtr, nItem, &pt, TRUE);
7905 /* now is the invalidation fun */
7906 LISTVIEW_ScrollOnInsert(infoPtr, nItem, 1);
7907 return nItem;
7909 undo:
7910 LISTVIEW_ShiftIndices(infoPtr, nItem, -1);
7911 LISTVIEW_ShiftFocus(infoPtr, infoPtr->nFocusedItem, nItem, -1);
7912 DPA_DeletePtr(infoPtr->hdpaItems, nItem);
7913 infoPtr->nItemCount--;
7914 fail:
7915 DPA_DeletePtr(hdpaSubItems, 0);
7916 DPA_Destroy (hdpaSubItems);
7917 Free (lpItem);
7918 return -1;
7921 /***
7922 * DESCRIPTION:
7923 * Checks item visibility.
7925 * PARAMETER(S):
7926 * [I] infoPtr : valid pointer to the listview structure
7927 * [I] nFirst : item index to check for
7929 * RETURN:
7930 * Item visible : TRUE
7931 * Item invisible or failure : FALSE
7933 static BOOL LISTVIEW_IsItemVisible(const LISTVIEW_INFO *infoPtr, INT nItem)
7935 POINT Origin, Position;
7936 RECT rcItem;
7937 HDC hdc;
7938 BOOL ret;
7940 TRACE("nItem=%d\n", nItem);
7942 if (nItem < 0 || nItem >= DPA_GetPtrCount(infoPtr->hdpaItems)) return FALSE;
7944 LISTVIEW_GetOrigin(infoPtr, &Origin);
7945 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
7946 rcItem.left = Position.x + Origin.x;
7947 rcItem.top = Position.y + Origin.y;
7948 rcItem.right = rcItem.left + infoPtr->nItemWidth;
7949 rcItem.bottom = rcItem.top + infoPtr->nItemHeight;
7951 hdc = GetDC(infoPtr->hwndSelf);
7952 if (!hdc) return FALSE;
7953 ret = RectVisible(hdc, &rcItem);
7954 ReleaseDC(infoPtr->hwndSelf, hdc);
7956 return ret;
7959 /***
7960 * DESCRIPTION:
7961 * Redraws a range of items.
7963 * PARAMETER(S):
7964 * [I] infoPtr : valid pointer to the listview structure
7965 * [I] nFirst : first item
7966 * [I] nLast : last item
7968 * RETURN:
7969 * SUCCESS : TRUE
7970 * FAILURE : FALSE
7972 static BOOL LISTVIEW_RedrawItems(const LISTVIEW_INFO *infoPtr, INT nFirst, INT nLast)
7974 INT i;
7976 for (i = max(nFirst, 0); i <= min(nLast, infoPtr->nItemCount - 1); i++)
7977 LISTVIEW_InvalidateItem(infoPtr, i);
7979 return TRUE;
7982 /***
7983 * DESCRIPTION:
7984 * Scroll the content of a listview.
7986 * PARAMETER(S):
7987 * [I] infoPtr : valid pointer to the listview structure
7988 * [I] dx : horizontal scroll amount in pixels
7989 * [I] dy : vertical scroll amount in pixels
7991 * RETURN:
7992 * SUCCESS : TRUE
7993 * FAILURE : FALSE
7995 * COMMENTS:
7996 * If the control is in report view (LV_VIEW_DETAILS) the control can
7997 * be scrolled only in line increments. "dy" will be rounded to the
7998 * nearest number of pixels that are a whole line. Ex: if line height
7999 * is 16 and an 8 is passed, the list will be scrolled by 16. If a 7
8000 * is passed, then the scroll will be 0. (per MSDN 7/2002)
8002 static BOOL LISTVIEW_Scroll(LISTVIEW_INFO *infoPtr, INT dx, INT dy)
8004 switch(infoPtr->uView) {
8005 case LV_VIEW_DETAILS:
8006 dy += (dy < 0 ? -1 : 1) * infoPtr->nItemHeight/2;
8007 dy /= infoPtr->nItemHeight;
8008 break;
8009 case LV_VIEW_LIST:
8010 if (dy != 0) return FALSE;
8011 break;
8012 default: /* icon */
8013 break;
8016 if (dx != 0) LISTVIEW_HScroll(infoPtr, SB_INTERNAL, dx);
8017 if (dy != 0) LISTVIEW_VScroll(infoPtr, SB_INTERNAL, dy);
8019 return TRUE;
8022 /***
8023 * DESCRIPTION:
8024 * Sets the background color.
8026 * PARAMETER(S):
8027 * [I] infoPtr : valid pointer to the listview structure
8028 * [I] color : background color
8030 * RETURN:
8031 * SUCCESS : TRUE
8032 * FAILURE : FALSE
8034 static BOOL LISTVIEW_SetBkColor(LISTVIEW_INFO *infoPtr, COLORREF color)
8036 TRACE("color %lx\n", color);
8038 if(infoPtr->clrBk != color) {
8039 if (infoPtr->clrBk != CLR_NONE) DeleteObject(infoPtr->hBkBrush);
8040 infoPtr->clrBk = color;
8041 if (color == CLR_NONE)
8042 infoPtr->hBkBrush = (HBRUSH)GetClassLongPtrW(infoPtr->hwndSelf, GCLP_HBRBACKGROUND);
8043 else
8045 infoPtr->hBkBrush = CreateSolidBrush(color);
8046 infoPtr->dwLvExStyle &= ~LVS_EX_TRANSPARENTBKGND;
8050 return TRUE;
8053 static BOOL LISTVIEW_SetBkImage(LISTVIEW_INFO *infoPtr, const LVBKIMAGEW *image, BOOL isW)
8055 TRACE("%08lx, %p, %p, %u, %d, %d\n", image->ulFlags, image->hbm, image->pszImage,
8056 image->cchImageMax, image->xOffsetPercent, image->yOffsetPercent);
8058 if (image->ulFlags & ~LVBKIF_SOURCE_MASK)
8059 FIXME("unsupported flags %08lx\n", image->ulFlags & ~LVBKIF_SOURCE_MASK);
8061 if (image->xOffsetPercent || image->yOffsetPercent)
8062 FIXME("unsupported offset %d,%d\n", image->xOffsetPercent, image->yOffsetPercent);
8064 switch (image->ulFlags & LVBKIF_SOURCE_MASK)
8066 case LVBKIF_SOURCE_NONE:
8067 if (infoPtr->hBkBitmap)
8069 DeleteObject(infoPtr->hBkBitmap);
8070 infoPtr->hBkBitmap = NULL;
8072 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
8073 break;
8075 case LVBKIF_SOURCE_HBITMAP:
8077 BITMAP bm;
8079 if (infoPtr->hBkBitmap)
8081 DeleteObject(infoPtr->hBkBitmap);
8082 infoPtr->hBkBitmap = NULL;
8084 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
8085 if (GetObjectW(image->hbm, sizeof(bm), &bm) == sizeof(bm))
8087 infoPtr->hBkBitmap = image->hbm;
8088 return TRUE;
8090 break;
8093 case LVBKIF_SOURCE_URL:
8094 FIXME("LVBKIF_SOURCE_URL: %s\n", isW ? debugstr_w(image->pszImage) : debugstr_a((LPCSTR)image->pszImage));
8095 break;
8098 return FALSE;
8101 /*** Helper for {Insert,Set}ColumnT *only* */
8102 static void column_fill_hditem(const LISTVIEW_INFO *infoPtr, HDITEMW *lphdi, INT nColumn,
8103 const LVCOLUMNW *lpColumn, BOOL isW)
8105 if (lpColumn->mask & LVCF_FMT)
8107 /* format member is valid */
8108 lphdi->mask |= HDI_FORMAT;
8110 /* set text alignment (leftmost column must be left-aligned) */
8111 if (nColumn == 0 || (lpColumn->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_LEFT)
8112 lphdi->fmt |= HDF_LEFT;
8113 else if ((lpColumn->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_RIGHT)
8114 lphdi->fmt |= HDF_RIGHT;
8115 else if ((lpColumn->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_CENTER)
8116 lphdi->fmt |= HDF_CENTER;
8118 if (lpColumn->fmt & LVCFMT_BITMAP_ON_RIGHT)
8119 lphdi->fmt |= HDF_BITMAP_ON_RIGHT;
8121 if (lpColumn->fmt & LVCFMT_COL_HAS_IMAGES)
8123 lphdi->fmt |= HDF_IMAGE;
8124 lphdi->iImage = I_IMAGECALLBACK;
8127 if (lpColumn->fmt & LVCFMT_FIXED_WIDTH)
8128 lphdi->fmt |= HDF_FIXEDWIDTH;
8131 if (lpColumn->mask & LVCF_WIDTH)
8133 lphdi->mask |= HDI_WIDTH;
8134 if(lpColumn->cx == LVSCW_AUTOSIZE_USEHEADER)
8136 /* make it fill the remainder of the controls width */
8137 RECT rcHeader;
8138 INT item_index;
8140 for(item_index = 0; item_index < (nColumn - 1); item_index++)
8142 LISTVIEW_GetHeaderRect(infoPtr, item_index, &rcHeader);
8143 lphdi->cxy += rcHeader.right - rcHeader.left;
8146 /* retrieve the layout of the header */
8147 GetClientRect(infoPtr->hwndSelf, &rcHeader);
8148 TRACE("start cxy=%d rcHeader=%s\n", lphdi->cxy, wine_dbgstr_rect(&rcHeader));
8150 lphdi->cxy = (rcHeader.right - rcHeader.left) - lphdi->cxy;
8152 else
8153 lphdi->cxy = lpColumn->cx;
8156 if (lpColumn->mask & LVCF_TEXT)
8158 lphdi->mask |= HDI_TEXT | HDI_FORMAT;
8159 lphdi->fmt |= HDF_STRING;
8160 lphdi->pszText = lpColumn->pszText;
8161 lphdi->cchTextMax = textlenT(lpColumn->pszText, isW);
8164 if (lpColumn->mask & LVCF_IMAGE)
8166 lphdi->mask |= HDI_IMAGE;
8167 lphdi->iImage = lpColumn->iImage;
8170 if (lpColumn->mask & LVCF_ORDER)
8172 lphdi->mask |= HDI_ORDER;
8173 lphdi->iOrder = lpColumn->iOrder;
8178 /***
8179 * DESCRIPTION:
8180 * Inserts a new column.
8182 * PARAMETER(S):
8183 * [I] infoPtr : valid pointer to the listview structure
8184 * [I] nColumn : column index
8185 * [I] lpColumn : column information
8186 * [I] isW : TRUE if lpColumn is Unicode, FALSE otherwise
8188 * RETURN:
8189 * SUCCESS : new column index
8190 * FAILURE : -1
8192 static INT LISTVIEW_InsertColumnT(LISTVIEW_INFO *infoPtr, INT nColumn,
8193 const LVCOLUMNW *lpColumn, BOOL isW)
8195 COLUMN_INFO *lpColumnInfo;
8196 INT nNewColumn;
8197 HDITEMW hdi;
8199 TRACE("(nColumn=%d, lpColumn=%s, isW=%d)\n", nColumn, debuglvcolumn_t(lpColumn, isW), isW);
8201 if (!lpColumn || nColumn < 0) return -1;
8202 nColumn = min(nColumn, DPA_GetPtrCount(infoPtr->hdpaColumns));
8204 ZeroMemory(&hdi, sizeof(HDITEMW));
8205 column_fill_hditem(infoPtr, &hdi, nColumn, lpColumn, isW);
8208 * A mask not including LVCF_WIDTH turns into a mask of width, width 10
8209 * (can be seen in SPY) otherwise column never gets added.
8211 if (!(lpColumn->mask & LVCF_WIDTH)) {
8212 hdi.mask |= HDI_WIDTH;
8213 hdi.cxy = 10;
8217 * when the iSubItem is available Windows copies it to the header lParam. It seems
8218 * to happen only in LVM_INSERTCOLUMN - not in LVM_SETCOLUMN
8220 if (lpColumn->mask & LVCF_SUBITEM)
8222 hdi.mask |= HDI_LPARAM;
8223 hdi.lParam = lpColumn->iSubItem;
8226 /* create header if not present */
8227 LISTVIEW_CreateHeader(infoPtr);
8228 if (!(LVS_NOCOLUMNHEADER & infoPtr->dwStyle) &&
8229 (infoPtr->uView == LV_VIEW_DETAILS) && (WS_VISIBLE & infoPtr->dwStyle))
8231 ShowWindow(infoPtr->hwndHeader, SW_SHOWNORMAL);
8234 /* insert item in header control */
8235 nNewColumn = SendMessageW(infoPtr->hwndHeader,
8236 isW ? HDM_INSERTITEMW : HDM_INSERTITEMA,
8237 nColumn, (LPARAM)&hdi);
8238 if (nNewColumn == -1) return -1;
8239 if (nNewColumn != nColumn) ERR("nColumn=%d, nNewColumn=%d\n", nColumn, nNewColumn);
8241 /* create our own column info */
8242 if (!(lpColumnInfo = Alloc(sizeof(COLUMN_INFO)))) goto fail;
8243 if (DPA_InsertPtr(infoPtr->hdpaColumns, nNewColumn, lpColumnInfo) == -1) goto fail;
8245 if (lpColumn->mask & LVCF_FMT) lpColumnInfo->fmt = lpColumn->fmt;
8246 if (lpColumn->mask & LVCF_MINWIDTH) lpColumnInfo->cxMin = lpColumn->cxMin;
8247 if (!SendMessageW(infoPtr->hwndHeader, HDM_GETITEMRECT, nNewColumn, (LPARAM)&lpColumnInfo->rcHeader))
8248 goto fail;
8250 /* now we have to actually adjust the data */
8251 if (!(infoPtr->dwStyle & LVS_OWNERDATA) && infoPtr->nItemCount > 0)
8253 SUBITEM_INFO *lpSubItem;
8254 HDPA hdpaSubItems;
8255 INT nItem, i;
8256 LVITEMW item;
8257 BOOL changed;
8259 item.iSubItem = nNewColumn;
8260 item.mask = LVIF_TEXT | LVIF_IMAGE;
8261 item.iImage = I_IMAGECALLBACK;
8262 item.pszText = LPSTR_TEXTCALLBACKW;
8264 for (nItem = 0; nItem < infoPtr->nItemCount; nItem++)
8266 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, nItem);
8267 for (i = 1; i < DPA_GetPtrCount(hdpaSubItems); i++)
8269 lpSubItem = DPA_GetPtr(hdpaSubItems, i);
8270 if (lpSubItem->iSubItem >= nNewColumn)
8271 lpSubItem->iSubItem++;
8274 /* add new subitem for each item */
8275 item.iItem = nItem;
8276 set_sub_item(infoPtr, &item, isW, &changed);
8280 /* make space for the new column */
8281 LISTVIEW_ScrollColumns(infoPtr, nNewColumn + 1, lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left);
8282 LISTVIEW_UpdateItemSize(infoPtr);
8284 return nNewColumn;
8286 fail:
8287 if (nNewColumn != -1) SendMessageW(infoPtr->hwndHeader, HDM_DELETEITEM, nNewColumn, 0);
8288 if (lpColumnInfo)
8290 DPA_DeletePtr(infoPtr->hdpaColumns, nNewColumn);
8291 Free(lpColumnInfo);
8293 return -1;
8296 /***
8297 * DESCRIPTION:
8298 * Sets the attributes of a header item.
8300 * PARAMETER(S):
8301 * [I] infoPtr : valid pointer to the listview structure
8302 * [I] nColumn : column index
8303 * [I] lpColumn : column attributes
8304 * [I] isW: if TRUE, then lpColumn is a LPLVCOLUMNW, else it is a LPLVCOLUMNA
8306 * RETURN:
8307 * SUCCESS : TRUE
8308 * FAILURE : FALSE
8310 static BOOL LISTVIEW_SetColumnT(const LISTVIEW_INFO *infoPtr, INT nColumn,
8311 const LVCOLUMNW *lpColumn, BOOL isW)
8313 HDITEMW hdi, hdiget;
8314 BOOL bResult;
8316 TRACE("(nColumn=%d, lpColumn=%s, isW=%d)\n", nColumn, debuglvcolumn_t(lpColumn, isW), isW);
8318 if (!lpColumn || nColumn < 0 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
8320 ZeroMemory(&hdi, sizeof(HDITEMW));
8321 if (lpColumn->mask & LVCF_FMT)
8323 hdi.mask |= HDI_FORMAT;
8324 hdiget.mask = HDI_FORMAT;
8325 if (SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nColumn, (LPARAM)&hdiget))
8326 hdi.fmt = hdiget.fmt & HDF_STRING;
8328 column_fill_hditem(infoPtr, &hdi, nColumn, lpColumn, isW);
8330 /* set header item attributes */
8331 bResult = SendMessageW(infoPtr->hwndHeader, isW ? HDM_SETITEMW : HDM_SETITEMA, nColumn, (LPARAM)&hdi);
8332 if (!bResult) return FALSE;
8334 if (lpColumn->mask & LVCF_FMT)
8336 COLUMN_INFO *lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, nColumn);
8337 INT oldFmt = lpColumnInfo->fmt;
8339 lpColumnInfo->fmt = lpColumn->fmt;
8340 if ((oldFmt ^ lpColumn->fmt) & (LVCFMT_JUSTIFYMASK | LVCFMT_IMAGE))
8342 if (infoPtr->uView == LV_VIEW_DETAILS) LISTVIEW_InvalidateColumn(infoPtr, nColumn);
8346 if (lpColumn->mask & LVCF_MINWIDTH)
8347 LISTVIEW_GetColumnInfo(infoPtr, nColumn)->cxMin = lpColumn->cxMin;
8349 return TRUE;
8352 /***
8353 * DESCRIPTION:
8354 * Sets the column order array
8356 * PARAMETERS:
8357 * [I] infoPtr : valid pointer to the listview structure
8358 * [I] iCount : number of elements in column order array
8359 * [I] lpiArray : pointer to column order array
8361 * RETURN:
8362 * SUCCESS : TRUE
8363 * FAILURE : FALSE
8365 static BOOL LISTVIEW_SetColumnOrderArray(LISTVIEW_INFO *infoPtr, INT iCount, const INT *lpiArray)
8367 if (!infoPtr->hwndHeader) return FALSE;
8368 infoPtr->colRectsDirty = TRUE;
8369 return SendMessageW(infoPtr->hwndHeader, HDM_SETORDERARRAY, iCount, (LPARAM)lpiArray);
8372 /***
8373 * DESCRIPTION:
8374 * Sets the width of a column
8376 * PARAMETERS:
8377 * [I] infoPtr : valid pointer to the listview structure
8378 * [I] nColumn : column index
8379 * [I] cx : column width
8381 * RETURN:
8382 * SUCCESS : TRUE
8383 * FAILURE : FALSE
8385 static BOOL LISTVIEW_SetColumnWidth(LISTVIEW_INFO *infoPtr, INT nColumn, INT cx)
8387 WCHAR szDispText[DISP_TEXT_SIZE] = { 0 };
8388 INT max_cx = 0;
8389 HDITEMW hdi;
8391 TRACE("(nColumn=%d, cx=%d)\n", nColumn, cx);
8393 /* set column width only if in report or list mode */
8394 if (infoPtr->uView != LV_VIEW_DETAILS && infoPtr->uView != LV_VIEW_LIST) return FALSE;
8396 /* take care of invalid cx values - LVSCW_AUTOSIZE_* values are negative,
8397 with _USEHEADER being the lowest */
8398 if (infoPtr->uView == LV_VIEW_DETAILS && cx < LVSCW_AUTOSIZE_USEHEADER) cx = LVSCW_AUTOSIZE;
8399 else if (infoPtr->uView == LV_VIEW_LIST && cx <= 0) return FALSE;
8401 /* resize all columns if in LV_VIEW_LIST mode */
8402 if(infoPtr->uView == LV_VIEW_LIST)
8404 infoPtr->nItemWidth = cx;
8405 LISTVIEW_InvalidateList(infoPtr);
8406 return TRUE;
8409 if (nColumn < 0 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
8411 if (cx == LVSCW_AUTOSIZE || (cx == LVSCW_AUTOSIZE_USEHEADER && nColumn < DPA_GetPtrCount(infoPtr->hdpaColumns) -1))
8413 INT nLabelWidth;
8414 LVITEMW lvItem;
8416 lvItem.mask = LVIF_TEXT;
8417 lvItem.iItem = 0;
8418 lvItem.iSubItem = nColumn;
8419 lvItem.cchTextMax = DISP_TEXT_SIZE;
8420 for (; lvItem.iItem < infoPtr->nItemCount; lvItem.iItem++)
8422 lvItem.pszText = szDispText;
8423 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) continue;
8424 nLabelWidth = LISTVIEW_GetStringWidthT(infoPtr, lvItem.pszText, TRUE);
8425 if (max_cx < nLabelWidth) max_cx = nLabelWidth;
8427 if (infoPtr->himlSmall && (nColumn == 0 || (LISTVIEW_GetColumnInfo(infoPtr, nColumn)->fmt & LVCFMT_IMAGE)))
8428 max_cx += infoPtr->iconSize.cx;
8429 max_cx += TRAILING_LABEL_PADDING;
8430 if (nColumn == 0 && (infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES))
8431 max_cx += GetSystemMetrics(SM_CXSMICON);
8434 /* autosize based on listview items width */
8435 if(cx == LVSCW_AUTOSIZE)
8436 cx = max_cx;
8437 else if(cx == LVSCW_AUTOSIZE_USEHEADER)
8439 /* if iCol is the last column make it fill the remainder of the controls width */
8440 if(nColumn == DPA_GetPtrCount(infoPtr->hdpaColumns) - 1)
8442 RECT rcHeader;
8443 POINT Origin;
8445 LISTVIEW_GetOrigin(infoPtr, &Origin);
8446 LISTVIEW_GetHeaderRect(infoPtr, nColumn, &rcHeader);
8448 cx = infoPtr->rcList.right - Origin.x - rcHeader.left;
8450 else
8452 /* Despite what the MS docs say, if this is not the last
8453 column, then MS resizes the column to the width of the
8454 largest text string in the column, including headers
8455 and items. This is different from LVSCW_AUTOSIZE in that
8456 LVSCW_AUTOSIZE ignores the header string length. */
8457 cx = 0;
8459 /* retrieve header text */
8460 hdi.mask = HDI_TEXT|HDI_FORMAT|HDI_IMAGE|HDI_BITMAP;
8461 hdi.cchTextMax = DISP_TEXT_SIZE;
8462 hdi.pszText = szDispText;
8463 if (SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nColumn, (LPARAM)&hdi))
8465 HDC hdc = GetDC(infoPtr->hwndSelf);
8466 HFONT old_font = SelectObject(hdc, (HFONT)SendMessageW(infoPtr->hwndHeader, WM_GETFONT, 0, 0));
8467 HIMAGELIST himl = (HIMAGELIST)SendMessageW(infoPtr->hwndHeader, HDM_GETIMAGELIST, 0, 0);
8468 INT bitmap_margin = 0;
8469 SIZE size;
8471 if (GetTextExtentPoint32W(hdc, hdi.pszText, lstrlenW(hdi.pszText), &size))
8472 cx = size.cx + TRAILING_HEADER_PADDING;
8474 if (hdi.fmt & (HDF_IMAGE|HDF_BITMAP))
8475 bitmap_margin = SendMessageW(infoPtr->hwndHeader, HDM_GETBITMAPMARGIN, 0, 0);
8477 if ((hdi.fmt & HDF_IMAGE) && himl)
8479 INT icon_cx, icon_cy;
8481 if (!ImageList_GetIconSize(himl, &icon_cx, &icon_cy))
8482 cx += icon_cx + 2*bitmap_margin;
8484 else if (hdi.fmt & HDF_BITMAP)
8486 BITMAP bmp;
8488 GetObjectW(hdi.hbm, sizeof(BITMAP), &bmp);
8489 cx += bmp.bmWidth + 2*bitmap_margin;
8492 SelectObject(hdc, old_font);
8493 ReleaseDC(infoPtr->hwndSelf, hdc);
8495 cx = max (cx, max_cx);
8499 if (cx < 0) return FALSE;
8501 /* call header to update the column change */
8502 hdi.mask = HDI_WIDTH;
8503 hdi.cxy = max(cx, LISTVIEW_GetColumnInfo(infoPtr, nColumn)->cxMin);
8504 TRACE("hdi.cxy=%d\n", hdi.cxy);
8505 return SendMessageW(infoPtr->hwndHeader, HDM_SETITEMW, nColumn, (LPARAM)&hdi);
8508 /***
8509 * Creates the checkbox imagelist. Helper for LISTVIEW_SetExtendedListViewStyle
8512 static HIMAGELIST LISTVIEW_CreateThemedCheckBoxImageList(const LISTVIEW_INFO *info)
8514 HBITMAP bitmap, old_bitmap;
8515 HIMAGELIST image_list;
8516 HDC hdc, mem_hdc;
8517 HTHEME theme;
8518 RECT rect;
8519 SIZE size;
8521 if (!GetWindowTheme(info->hwndSelf))
8522 return NULL;
8524 theme = OpenThemeDataForDpi(NULL, L"Button", GetDpiForWindow(info->hwndSelf));
8525 if (!theme)
8526 return NULL;
8528 hdc = GetDC(info->hwndSelf);
8529 GetThemePartSize(theme, hdc, BP_CHECKBOX, 0, NULL, TS_DRAW, &size);
8530 SetRect(&rect, 0, 0, size.cx, size.cy);
8531 image_list = ImageList_Create(size.cx, size.cy, ILC_COLOR32, 2, 2);
8532 mem_hdc = CreateCompatibleDC(hdc);
8533 bitmap = CreateCompatibleBitmap(hdc, size.cx, size.cy);
8534 old_bitmap = SelectObject(mem_hdc, bitmap);
8535 ReleaseDC(info->hwndSelf, hdc);
8537 if (IsThemeBackgroundPartiallyTransparent(theme, BP_CHECKBOX, CBS_UNCHECKEDNORMAL))
8538 FillRect(mem_hdc, &rect, (HBRUSH)(COLOR_WINDOW + 1));
8539 DrawThemeBackground(theme, mem_hdc, BP_CHECKBOX, CBS_UNCHECKEDNORMAL, &rect, NULL);
8540 ImageList_Add(image_list, bitmap, NULL);
8542 if (IsThemeBackgroundPartiallyTransparent(theme, BP_CHECKBOX, CBS_CHECKEDNORMAL))
8543 FillRect(mem_hdc, &rect, (HBRUSH)(COLOR_WINDOW + 1));
8544 DrawThemeBackground(theme, mem_hdc, BP_CHECKBOX, CBS_CHECKEDNORMAL, &rect, NULL);
8545 ImageList_Add(image_list, bitmap, NULL);
8547 SelectObject(mem_hdc, old_bitmap);
8548 DeleteObject(bitmap);
8549 DeleteDC(mem_hdc);
8550 CloseThemeData(theme);
8551 return image_list;
8554 static HIMAGELIST LISTVIEW_CreateCheckBoxIL(const LISTVIEW_INFO *infoPtr)
8556 HDC hdc_wnd, hdc;
8557 HBITMAP hbm_im, hbm_mask, hbm_orig;
8558 RECT rc;
8559 HBRUSH hbr_white, hbr_black;
8560 HIMAGELIST himl;
8562 himl = LISTVIEW_CreateThemedCheckBoxImageList(infoPtr);
8563 if (himl)
8564 return himl;
8566 hbr_white = GetStockObject(WHITE_BRUSH);
8567 hbr_black = GetStockObject(BLACK_BRUSH);
8568 himl = ImageList_Create(GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON),
8569 ILC_COLOR | ILC_MASK, 2, 2);
8570 hdc_wnd = GetDC(infoPtr->hwndSelf);
8571 hdc = CreateCompatibleDC(hdc_wnd);
8572 hbm_im = CreateCompatibleBitmap(hdc_wnd, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON));
8573 hbm_mask = CreateBitmap(GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), 1, 1, NULL);
8574 ReleaseDC(infoPtr->hwndSelf, hdc_wnd);
8576 SetRect(&rc, 0, 0, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON));
8577 hbm_orig = SelectObject(hdc, hbm_mask);
8578 FillRect(hdc, &rc, hbr_white);
8579 InflateRect(&rc, -2, -2);
8580 FillRect(hdc, &rc, hbr_black);
8582 SelectObject(hdc, hbm_im);
8583 DrawFrameControl(hdc, &rc, DFC_BUTTON, DFCS_BUTTONCHECK | DFCS_MONO);
8584 SelectObject(hdc, hbm_orig);
8585 ImageList_Add(himl, hbm_im, hbm_mask);
8587 SelectObject(hdc, hbm_im);
8588 DrawFrameControl(hdc, &rc, DFC_BUTTON, DFCS_BUTTONCHECK | DFCS_MONO | DFCS_CHECKED);
8589 SelectObject(hdc, hbm_orig);
8590 ImageList_Add(himl, hbm_im, hbm_mask);
8592 DeleteObject(hbm_mask);
8593 DeleteObject(hbm_im);
8594 DeleteDC(hdc);
8596 return himl;
8599 /***
8600 * DESCRIPTION:
8601 * Sets the extended listview style.
8603 * PARAMETERS:
8604 * [I] infoPtr : valid pointer to the listview structure
8605 * [I] dwMask : mask
8606 * [I] dwStyle : style
8608 * RETURN:
8609 * SUCCESS : previous style
8610 * FAILURE : 0
8612 static DWORD LISTVIEW_SetExtendedListViewStyle(LISTVIEW_INFO *infoPtr, DWORD mask, DWORD ex_style)
8614 DWORD old_ex_style = infoPtr->dwLvExStyle;
8616 TRACE("mask %#lx, ex_style %#lx\n", mask, ex_style);
8618 /* set new style */
8619 if (mask)
8620 infoPtr->dwLvExStyle = (old_ex_style & ~mask) | (ex_style & mask);
8621 else
8622 infoPtr->dwLvExStyle = ex_style;
8624 if((infoPtr->dwLvExStyle ^ old_ex_style) & LVS_EX_CHECKBOXES)
8626 HIMAGELIST himl = 0;
8627 if(infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES)
8629 LVITEMW item;
8630 item.mask = LVIF_STATE;
8631 item.stateMask = LVIS_STATEIMAGEMASK;
8632 item.state = INDEXTOSTATEIMAGEMASK(1);
8633 LISTVIEW_SetItemState(infoPtr, -1, &item);
8635 himl = LISTVIEW_CreateCheckBoxIL(infoPtr);
8636 if(!(infoPtr->dwStyle & LVS_SHAREIMAGELISTS))
8637 ImageList_Destroy(infoPtr->himlState);
8639 himl = LISTVIEW_SetImageList(infoPtr, LVSIL_STATE, himl);
8640 /* checkbox list replaces previous custom list or... */
8641 if(((infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES) &&
8642 !(infoPtr->dwStyle & LVS_SHAREIMAGELISTS)) ||
8643 /* ...previous was checkbox list */
8644 (old_ex_style & LVS_EX_CHECKBOXES))
8645 ImageList_Destroy(himl);
8648 if((infoPtr->dwLvExStyle ^ old_ex_style) & LVS_EX_HEADERDRAGDROP)
8650 DWORD style;
8652 /* if not already created */
8653 LISTVIEW_CreateHeader(infoPtr);
8655 style = GetWindowLongW(infoPtr->hwndHeader, GWL_STYLE);
8656 if (infoPtr->dwLvExStyle & LVS_EX_HEADERDRAGDROP)
8657 style |= HDS_DRAGDROP;
8658 else
8659 style &= ~HDS_DRAGDROP;
8660 SetWindowLongW(infoPtr->hwndHeader, GWL_STYLE, style);
8663 /* GRIDLINES adds decoration at top so changes sizes */
8664 if((infoPtr->dwLvExStyle ^ old_ex_style) & LVS_EX_GRIDLINES)
8666 LISTVIEW_CreateHeader(infoPtr);
8667 LISTVIEW_UpdateSize(infoPtr);
8670 if((infoPtr->dwLvExStyle ^ old_ex_style) & LVS_EX_FULLROWSELECT)
8672 LISTVIEW_CreateHeader(infoPtr);
8675 if((infoPtr->dwLvExStyle ^ old_ex_style) & LVS_EX_TRANSPARENTBKGND)
8677 if (infoPtr->dwLvExStyle & LVS_EX_TRANSPARENTBKGND)
8678 LISTVIEW_SetBkColor(infoPtr, CLR_NONE);
8681 if((infoPtr->dwLvExStyle ^ old_ex_style) & LVS_EX_HEADERINALLVIEWS)
8683 if (infoPtr->dwLvExStyle & LVS_EX_HEADERINALLVIEWS)
8684 LISTVIEW_CreateHeader(infoPtr);
8685 else
8686 ShowWindow(infoPtr->hwndHeader, SW_HIDE);
8687 LISTVIEW_UpdateSize(infoPtr);
8688 LISTVIEW_UpdateScroll(infoPtr);
8691 LISTVIEW_InvalidateList(infoPtr);
8692 return old_ex_style;
8695 /***
8696 * DESCRIPTION:
8697 * Sets the new hot cursor used during hot tracking and hover selection.
8699 * PARAMETER(S):
8700 * [I] infoPtr : valid pointer to the listview structure
8701 * [I] hCursor : the new hot cursor handle
8703 * RETURN:
8704 * Returns the previous hot cursor
8706 static HCURSOR LISTVIEW_SetHotCursor(LISTVIEW_INFO *infoPtr, HCURSOR hCursor)
8708 HCURSOR oldCursor = infoPtr->hHotCursor;
8710 infoPtr->hHotCursor = hCursor;
8712 return oldCursor;
8716 /***
8717 * DESCRIPTION:
8718 * Sets the hot item index.
8720 * PARAMETERS:
8721 * [I] infoPtr : valid pointer to the listview structure
8722 * [I] iIndex : index
8724 * RETURN:
8725 * SUCCESS : previous hot item index
8726 * FAILURE : -1 (no hot item)
8728 static INT LISTVIEW_SetHotItem(LISTVIEW_INFO *infoPtr, INT iIndex)
8730 INT iOldIndex = infoPtr->nHotItem;
8732 infoPtr->nHotItem = iIndex;
8734 return iOldIndex;
8738 /***
8739 * DESCRIPTION:
8740 * Sets the amount of time the cursor must hover over an item before it is selected.
8742 * PARAMETER(S):
8743 * [I] infoPtr : valid pointer to the listview structure
8744 * [I] dwHoverTime : hover time, if -1 the hover time is set to the default
8746 * RETURN:
8747 * Returns the previous hover time
8749 static DWORD LISTVIEW_SetHoverTime(LISTVIEW_INFO *infoPtr, DWORD dwHoverTime)
8751 DWORD oldHoverTime = infoPtr->dwHoverTime;
8753 infoPtr->dwHoverTime = dwHoverTime;
8755 return oldHoverTime;
8758 /***
8759 * DESCRIPTION:
8760 * Sets spacing for icons of LVS_ICON style.
8762 * PARAMETER(S):
8763 * [I] infoPtr : valid pointer to the listview structure
8764 * [I] cx : horizontal spacing (-1 = system spacing, 0 = autosize)
8765 * [I] cy : vertical spacing (-1 = system spacing, 0 = autosize)
8767 * RETURN:
8768 * MAKELONG(oldcx, oldcy)
8770 static DWORD LISTVIEW_SetIconSpacing(LISTVIEW_INFO *infoPtr, INT cx, INT cy)
8772 INT iconWidth = 0, iconHeight = 0;
8773 DWORD oldspacing = MAKELONG(infoPtr->iconSpacing.cx, infoPtr->iconSpacing.cy);
8775 TRACE("requested=(%d,%d)\n", cx, cy);
8777 /* set to defaults, if instructed to */
8778 if (cx == -1 && cy == -1)
8780 infoPtr->autoSpacing = TRUE;
8781 if (infoPtr->himlNormal)
8782 ImageList_GetIconSize(infoPtr->himlNormal, &iconWidth, &iconHeight);
8783 cx = GetSystemMetrics(SM_CXICONSPACING) - GetSystemMetrics(SM_CXICON) + iconWidth;
8784 cy = GetSystemMetrics(SM_CYICONSPACING) - GetSystemMetrics(SM_CYICON) + iconHeight;
8786 else
8787 infoPtr->autoSpacing = FALSE;
8789 /* if 0 then keep width */
8790 if (cx != 0)
8791 infoPtr->iconSpacing.cx = cx;
8793 /* if 0 then keep height */
8794 if (cy != 0)
8795 infoPtr->iconSpacing.cy = cy;
8797 TRACE("old=(%d,%d), new=(%ld,%ld), iconSize=(%ld,%ld), ntmH=%d\n",
8798 LOWORD(oldspacing), HIWORD(oldspacing), infoPtr->iconSpacing.cx, infoPtr->iconSpacing.cy,
8799 infoPtr->iconSize.cx, infoPtr->iconSize.cy,
8800 infoPtr->ntmHeight);
8802 /* these depend on the iconSpacing */
8803 LISTVIEW_UpdateItemSize(infoPtr);
8805 return oldspacing;
8808 static inline void set_icon_size(SIZE *size, HIMAGELIST himl, BOOL is_small)
8810 INT cx, cy;
8812 if (himl && ImageList_GetIconSize(himl, &cx, &cy))
8814 size->cx = cx;
8815 size->cy = cy;
8817 else
8819 size->cx = GetSystemMetrics(is_small ? SM_CXSMICON : SM_CXICON);
8820 size->cy = GetSystemMetrics(is_small ? SM_CYSMICON : SM_CYICON);
8824 /***
8825 * DESCRIPTION:
8826 * Sets image lists.
8828 * PARAMETER(S):
8829 * [I] infoPtr : valid pointer to the listview structure
8830 * [I] nType : image list type
8831 * [I] himl : image list handle
8833 * RETURN:
8834 * SUCCESS : old image list
8835 * FAILURE : NULL
8837 static HIMAGELIST LISTVIEW_SetImageList(LISTVIEW_INFO *infoPtr, INT nType, HIMAGELIST himl)
8839 INT oldHeight = infoPtr->nItemHeight;
8840 HIMAGELIST himlOld = 0;
8842 TRACE("(nType=%d, himl=%p)\n", nType, himl);
8844 switch (nType)
8846 case LVSIL_NORMAL:
8847 himlOld = infoPtr->himlNormal;
8848 infoPtr->himlNormal = himl;
8849 if (infoPtr->uView == LV_VIEW_ICON) set_icon_size(&infoPtr->iconSize, himl, FALSE);
8850 if (infoPtr->autoSpacing)
8851 LISTVIEW_SetIconSpacing(infoPtr, -1, -1);
8852 break;
8854 case LVSIL_SMALL:
8855 himlOld = infoPtr->himlSmall;
8856 infoPtr->himlSmall = himl;
8857 if (infoPtr->uView != LV_VIEW_ICON) set_icon_size(&infoPtr->iconSize, himl, TRUE);
8858 if (infoPtr->hwndHeader)
8859 SendMessageW(infoPtr->hwndHeader, HDM_SETIMAGELIST, 0, (LPARAM)himl);
8860 break;
8862 case LVSIL_STATE:
8863 himlOld = infoPtr->himlState;
8864 infoPtr->himlState = himl;
8865 set_icon_size(&infoPtr->iconStateSize, himl, TRUE);
8866 ImageList_SetBkColor(infoPtr->himlState, CLR_NONE);
8867 break;
8869 default:
8870 ERR("Unknown icon type=%d\n", nType);
8871 return NULL;
8874 infoPtr->nItemHeight = LISTVIEW_CalculateItemHeight(infoPtr);
8875 if (infoPtr->nItemHeight != oldHeight)
8876 LISTVIEW_UpdateScroll(infoPtr);
8878 return himlOld;
8881 /***
8882 * DESCRIPTION:
8883 * Preallocates memory (does *not* set the actual count of items !)
8885 * PARAMETER(S):
8886 * [I] infoPtr : valid pointer to the listview structure
8887 * [I] nItems : item count (projected number of items to allocate)
8888 * [I] dwFlags : update flags
8890 * RETURN:
8891 * SUCCESS : TRUE
8892 * FAILURE : FALSE
8894 static BOOL LISTVIEW_SetItemCount(LISTVIEW_INFO *infoPtr, INT nItems, DWORD dwFlags)
8896 TRACE("nItems %d, flags %#lx\n", nItems, dwFlags);
8898 if (infoPtr->dwStyle & LVS_OWNERDATA)
8900 INT nOldCount = infoPtr->nItemCount;
8901 infoPtr->nItemCount = nItems;
8903 if (nItems < nOldCount)
8905 RANGE range = { nItems, nOldCount };
8906 ranges_del(infoPtr->selectionRanges, range);
8907 if (infoPtr->nFocusedItem >= nItems)
8909 LISTVIEW_SetItemFocus(infoPtr, -1);
8910 infoPtr->nFocusedItem = -1;
8911 SetRectEmpty(&infoPtr->rcFocus);
8915 LISTVIEW_UpdateScroll(infoPtr);
8917 /* the flags are valid only in ownerdata report and list modes */
8918 if (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON) dwFlags = 0;
8920 if (!(dwFlags & LVSICF_NOSCROLL) && infoPtr->nFocusedItem != -1)
8921 LISTVIEW_EnsureVisible(infoPtr, infoPtr->nFocusedItem, FALSE);
8923 if (!(dwFlags & LVSICF_NOINVALIDATEALL))
8924 LISTVIEW_InvalidateList(infoPtr);
8925 else
8927 INT nFrom, nTo;
8928 POINT Origin;
8929 RECT rcErase;
8931 LISTVIEW_GetOrigin(infoPtr, &Origin);
8932 nFrom = min(nOldCount, nItems);
8933 nTo = max(nOldCount, nItems);
8935 if (infoPtr->uView == LV_VIEW_DETAILS)
8937 SetRect(&rcErase, 0, nFrom * infoPtr->nItemHeight, infoPtr->nItemWidth,
8938 nTo * infoPtr->nItemHeight);
8939 OffsetRect(&rcErase, Origin.x, Origin.y);
8940 if (IntersectRect(&rcErase, &rcErase, &infoPtr->rcList))
8941 LISTVIEW_InvalidateRect(infoPtr, &rcErase);
8943 else /* LV_VIEW_LIST */
8945 INT nPerCol = LISTVIEW_GetCountPerColumn(infoPtr);
8947 rcErase.left = (nFrom / nPerCol) * infoPtr->nItemWidth;
8948 rcErase.top = (nFrom % nPerCol) * infoPtr->nItemHeight;
8949 rcErase.right = rcErase.left + infoPtr->nItemWidth;
8950 rcErase.bottom = nPerCol * infoPtr->nItemHeight;
8951 OffsetRect(&rcErase, Origin.x, Origin.y);
8952 if (IntersectRect(&rcErase, &rcErase, &infoPtr->rcList))
8953 LISTVIEW_InvalidateRect(infoPtr, &rcErase);
8955 rcErase.left = (nFrom / nPerCol + 1) * infoPtr->nItemWidth;
8956 rcErase.top = 0;
8957 rcErase.right = (nTo / nPerCol + 1) * infoPtr->nItemWidth;
8958 rcErase.bottom = nPerCol * infoPtr->nItemHeight;
8959 OffsetRect(&rcErase, Origin.x, Origin.y);
8960 if (IntersectRect(&rcErase, &rcErase, &infoPtr->rcList))
8961 LISTVIEW_InvalidateRect(infoPtr, &rcErase);
8965 else
8967 /* According to MSDN for non-LVS_OWNERDATA this is just
8968 * a performance issue. The control allocates its internal
8969 * data structures for the number of items specified. It
8970 * cuts down on the number of memory allocations. Therefore
8971 * we will just issue a WARN here
8973 WARN("for non-ownerdata performance option not implemented.\n");
8976 return TRUE;
8979 /***
8980 * DESCRIPTION:
8981 * Sets the position of an item.
8983 * PARAMETER(S):
8984 * [I] infoPtr : valid pointer to the listview structure
8985 * [I] nItem : item index
8986 * [I] pt : coordinate
8988 * RETURN:
8989 * SUCCESS : TRUE
8990 * FAILURE : FALSE
8992 static BOOL LISTVIEW_SetItemPosition(LISTVIEW_INFO *infoPtr, INT nItem, const POINT *pt)
8994 POINT Origin, Pt;
8996 TRACE("(nItem=%d, pt=%s)\n", nItem, wine_dbgstr_point(pt));
8998 if (!pt || nItem < 0 || nItem >= infoPtr->nItemCount ||
8999 !(infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON)) return FALSE;
9001 Pt = *pt;
9002 LISTVIEW_GetOrigin(infoPtr, &Origin);
9004 /* This point value seems to be an undocumented feature.
9005 * The best guess is that it means either at the origin,
9006 * or at true beginning of the list. I will assume the origin. */
9007 if ((Pt.x == -1) && (Pt.y == -1))
9008 Pt = Origin;
9010 if (infoPtr->uView == LV_VIEW_ICON)
9012 Pt.x -= (infoPtr->nItemWidth - infoPtr->iconSize.cx) / 2;
9013 Pt.y -= ICON_TOP_PADDING;
9015 Pt.x -= Origin.x;
9016 Pt.y -= Origin.y;
9018 return LISTVIEW_MoveIconTo(infoPtr, nItem, &Pt, FALSE);
9021 /* Make sure to also disable per item notifications via the notification mask. */
9022 static VOID LISTVIEW_SetOwnerDataState(LISTVIEW_INFO *infoPtr, INT nFirst, INT nLast, const LVITEMW *item)
9024 NMLVODSTATECHANGE nmlv;
9026 if (!item) return;
9028 ZeroMemory(&nmlv, sizeof(nmlv));
9029 nmlv.iFrom = nFirst;
9030 nmlv.iTo = nLast;
9031 nmlv.uOldState = 0;
9032 nmlv.uNewState = item->state;
9034 notify_hdr(infoPtr, LVN_ODSTATECHANGED, (LPNMHDR)&nmlv);
9037 /***
9038 * DESCRIPTION:
9039 * Sets the state of one or many items.
9041 * PARAMETER(S):
9042 * [I] infoPtr : valid pointer to the listview structure
9043 * [I] nItem : item index
9044 * [I] item : item or subitem info
9046 * RETURN:
9047 * SUCCESS : TRUE
9048 * FAILURE : FALSE
9050 static BOOL LISTVIEW_SetItemState(LISTVIEW_INFO *infoPtr, INT nItem, const LVITEMW *item)
9052 BOOL ret = TRUE;
9053 LVITEMW lvItem;
9055 if (!item) return FALSE;
9057 lvItem.iItem = nItem;
9058 lvItem.iSubItem = 0;
9059 lvItem.mask = LVIF_STATE;
9060 lvItem.state = item->state;
9061 lvItem.stateMask = item->stateMask;
9062 TRACE("item=%s\n", debuglvitem_t(&lvItem, TRUE));
9064 if (nItem == -1)
9066 UINT oldstate = 0;
9067 DWORD old_mask;
9069 /* special case optimization for recurring attempt to deselect all */
9070 if (lvItem.state == 0 && lvItem.stateMask == LVIS_SELECTED && !LISTVIEW_GetSelectedCount(infoPtr))
9071 return TRUE;
9073 /* select all isn't allowed in LVS_SINGLESEL */
9074 if ((lvItem.state & lvItem.stateMask & LVIS_SELECTED) && (infoPtr->dwStyle & LVS_SINGLESEL))
9075 return FALSE;
9077 /* focus all isn't allowed */
9078 if (lvItem.state & lvItem.stateMask & LVIS_FOCUSED) return FALSE;
9080 old_mask = infoPtr->notify_mask & NOTIFY_MASK_ITEM_CHANGE;
9081 if (infoPtr->dwStyle & LVS_OWNERDATA)
9083 infoPtr->notify_mask &= ~NOTIFY_MASK_ITEM_CHANGE;
9084 if (!(lvItem.state & LVIS_SELECTED) && LISTVIEW_GetSelectedCount(infoPtr))
9085 oldstate |= LVIS_SELECTED;
9086 if (infoPtr->nFocusedItem != -1) oldstate |= LVIS_FOCUSED;
9089 /* apply to all items */
9090 for (lvItem.iItem = 0; lvItem.iItem < infoPtr->nItemCount; lvItem.iItem++)
9091 if (!LISTVIEW_SetItemT(infoPtr, &lvItem, TRUE)) ret = FALSE;
9093 if (infoPtr->dwStyle & LVS_OWNERDATA)
9095 NMLISTVIEW nmlv;
9097 infoPtr->notify_mask |= old_mask;
9099 nmlv.iItem = -1;
9100 nmlv.iSubItem = 0;
9101 nmlv.uNewState = lvItem.state & lvItem.stateMask;
9102 nmlv.uOldState = oldstate & lvItem.stateMask;
9103 nmlv.uChanged = LVIF_STATE;
9104 nmlv.ptAction.x = nmlv.ptAction.y = 0;
9105 nmlv.lParam = 0;
9107 notify_listview(infoPtr, LVN_ITEMCHANGED, &nmlv);
9110 else
9111 ret = LISTVIEW_SetItemT(infoPtr, &lvItem, TRUE);
9113 return ret;
9116 /***
9117 * DESCRIPTION:
9118 * Sets the text of an item or subitem.
9120 * PARAMETER(S):
9121 * [I] hwnd : window handle
9122 * [I] nItem : item index
9123 * [I] lpLVItem : item or subitem info
9124 * [I] isW : TRUE if input is Unicode
9126 * RETURN:
9127 * SUCCESS : TRUE
9128 * FAILURE : FALSE
9130 static BOOL LISTVIEW_SetItemTextT(LISTVIEW_INFO *infoPtr, INT nItem, const LVITEMW *lpLVItem, BOOL isW)
9132 LVITEMW lvItem;
9134 if (!lpLVItem || nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
9135 if (infoPtr->dwStyle & LVS_OWNERDATA) return FALSE;
9137 lvItem.iItem = nItem;
9138 lvItem.iSubItem = lpLVItem->iSubItem;
9139 lvItem.mask = LVIF_TEXT;
9140 lvItem.pszText = lpLVItem->pszText;
9141 lvItem.cchTextMax = lpLVItem->cchTextMax;
9143 TRACE("(nItem=%d, lpLVItem=%s, isW=%d)\n", nItem, debuglvitem_t(&lvItem, isW), isW);
9145 return LISTVIEW_SetItemT(infoPtr, &lvItem, isW);
9148 /***
9149 * DESCRIPTION:
9150 * Set item index that marks the start of a multiple selection.
9152 * PARAMETER(S):
9153 * [I] infoPtr : valid pointer to the listview structure
9154 * [I] nIndex : index
9156 * RETURN:
9157 * Index number or -1 if there is no selection mark.
9159 static INT LISTVIEW_SetSelectionMark(LISTVIEW_INFO *infoPtr, INT nIndex)
9161 INT nOldIndex = infoPtr->nSelectionMark;
9163 TRACE("(nIndex=%d)\n", nIndex);
9165 if (nIndex >= -1 && nIndex < infoPtr->nItemCount)
9166 infoPtr->nSelectionMark = nIndex;
9168 return nOldIndex;
9171 /***
9172 * DESCRIPTION:
9173 * Sets the text background color.
9175 * PARAMETER(S):
9176 * [I] infoPtr : valid pointer to the listview structure
9177 * [I] color : text background color
9179 * RETURN:
9180 * SUCCESS : TRUE
9181 * FAILURE : FALSE
9183 static BOOL LISTVIEW_SetTextBkColor(LISTVIEW_INFO *infoPtr, COLORREF color)
9185 TRACE("color %#lx\n", color);
9187 infoPtr->clrTextBk = color;
9188 return TRUE;
9191 /***
9192 * DESCRIPTION:
9193 * Sets the text foreground color.
9195 * PARAMETER(S):
9196 * [I] infoPtr : valid pointer to the listview structure
9197 * [I] color : text color
9199 * RETURN:
9200 * SUCCESS : TRUE
9201 * FAILURE : FALSE
9203 static BOOL LISTVIEW_SetTextColor (LISTVIEW_INFO *infoPtr, COLORREF color)
9205 TRACE("color %#lx\n", color);
9207 infoPtr->clrText = color;
9208 return TRUE;
9211 /***
9212 * DESCRIPTION:
9213 * Sets new ToolTip window to ListView control.
9215 * PARAMETER(S):
9216 * [I] infoPtr : valid pointer to the listview structure
9217 * [I] hwndNewToolTip : handle to new ToolTip
9219 * RETURN:
9220 * old tool tip
9222 static HWND LISTVIEW_SetToolTips( LISTVIEW_INFO *infoPtr, HWND hwndNewToolTip)
9224 HWND hwndOldToolTip = infoPtr->hwndToolTip;
9225 infoPtr->hwndToolTip = hwndNewToolTip;
9226 return hwndOldToolTip;
9230 * DESCRIPTION:
9231 * sets the Unicode character format flag for the control
9232 * PARAMETER(S):
9233 * [I] infoPtr :valid pointer to the listview structure
9234 * [I] fUnicode :true to switch to UNICODE false to switch to ANSI
9236 * RETURN:
9237 * Old Unicode Format
9239 static BOOL LISTVIEW_SetUnicodeFormat( LISTVIEW_INFO *infoPtr, BOOL unicode)
9241 SHORT rc = infoPtr->notifyFormat;
9242 infoPtr->notifyFormat = (unicode) ? NFR_UNICODE : NFR_ANSI;
9243 return rc == NFR_UNICODE;
9247 * DESCRIPTION:
9248 * sets the control view mode
9249 * PARAMETER(S):
9250 * [I] infoPtr :valid pointer to the listview structure
9251 * [I] nView :new view mode value
9253 * RETURN:
9254 * SUCCESS: 1
9255 * FAILURE: -1
9257 static INT LISTVIEW_SetView(LISTVIEW_INFO *infoPtr, DWORD nView)
9259 HIMAGELIST himl;
9261 if (infoPtr->uView == nView) return 1;
9263 if ((INT)nView < 0 || nView > LV_VIEW_MAX) return -1;
9264 if (nView == LV_VIEW_TILE)
9266 FIXME("View LV_VIEW_TILE unimplemented\n");
9267 return -1;
9270 infoPtr->uView = nView;
9272 SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
9273 ShowWindow(infoPtr->hwndHeader, SW_HIDE);
9275 ShowScrollBar(infoPtr->hwndSelf, SB_BOTH, FALSE);
9276 SetRectEmpty(&infoPtr->rcFocus);
9278 himl = (nView == LV_VIEW_ICON ? infoPtr->himlNormal : infoPtr->himlSmall);
9279 set_icon_size(&infoPtr->iconSize, himl, nView != LV_VIEW_ICON);
9281 switch (nView)
9283 case LV_VIEW_ICON:
9284 case LV_VIEW_SMALLICON:
9285 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
9286 break;
9287 case LV_VIEW_DETAILS:
9289 HDLAYOUT hl;
9290 WINDOWPOS wp;
9292 LISTVIEW_CreateHeader( infoPtr );
9294 hl.prc = &infoPtr->rcList;
9295 hl.pwpos = &wp;
9296 SendMessageW(infoPtr->hwndHeader, HDM_LAYOUT, 0, (LPARAM)&hl);
9297 SetWindowPos(infoPtr->hwndHeader, infoPtr->hwndSelf, wp.x, wp.y, wp.cx, wp.cy,
9298 wp.flags | ((infoPtr->dwStyle & LVS_NOCOLUMNHEADER) ? SWP_HIDEWINDOW : SWP_SHOWWINDOW));
9299 break;
9301 case LV_VIEW_LIST:
9302 break;
9305 LISTVIEW_UpdateItemSize(infoPtr);
9306 LISTVIEW_UpdateSize(infoPtr);
9307 LISTVIEW_UpdateScroll(infoPtr);
9308 LISTVIEW_InvalidateList(infoPtr);
9310 TRACE("nView %ld\n", nView);
9312 return 1;
9315 /* LISTVIEW_SetWorkAreas */
9317 struct sorting_context
9319 HDPA items;
9320 PFNLVCOMPARE compare_func;
9321 LPARAM lParam;
9324 /* DPA_Sort() callback used for LVM_SORTITEMS */
9325 static INT WINAPI LISTVIEW_CallBackCompare(LPVOID first, LPVOID second, LPARAM lParam)
9327 struct sorting_context *context = (struct sorting_context *)lParam;
9328 ITEM_INFO* lv_first = DPA_GetPtr( first, 0 );
9329 ITEM_INFO* lv_second = DPA_GetPtr( second, 0 );
9331 return context->compare_func(lv_first->lParam, lv_second->lParam, context->lParam);
9334 /* DPA_Sort() callback used for LVM_SORTITEMSEX */
9335 static INT WINAPI LISTVIEW_CallBackCompareEx(LPVOID first, LPVOID second, LPARAM lParam)
9337 struct sorting_context *context = (struct sorting_context *)lParam;
9338 INT first_idx = DPA_GetPtrIndex( context->items, first );
9339 INT second_idx = DPA_GetPtrIndex( context->items, second );
9341 return context->compare_func(first_idx, second_idx, context->lParam);
9344 /***
9345 * DESCRIPTION:
9346 * Sorts the listview items.
9348 * PARAMETER(S):
9349 * [I] infoPtr : valid pointer to the listview structure
9350 * [I] pfnCompare : application-defined value
9351 * [I] lParamSort : pointer to comparison callback
9352 * [I] IsEx : TRUE when LVM_SORTITEMSEX used
9354 * RETURN:
9355 * SUCCESS : TRUE
9356 * FAILURE : FALSE
9358 static BOOL LISTVIEW_SortItems(LISTVIEW_INFO *infoPtr, PFNLVCOMPARE pfnCompare,
9359 LPARAM lParamSort, BOOL IsEx)
9361 HDPA hdpaSubItems, hdpaItems;
9362 ITEM_INFO *lpItem;
9363 LPVOID selectionMarkItem = NULL;
9364 LPVOID focusedItem = NULL;
9365 struct sorting_context context;
9366 int i;
9368 TRACE("pfnCompare %p, lParamSort %Ix\n", pfnCompare, lParamSort);
9370 if (infoPtr->dwStyle & LVS_OWNERDATA) return FALSE;
9372 if (!pfnCompare) return FALSE;
9373 if (!infoPtr->hdpaItems) return FALSE;
9375 /* if there are 0 or 1 items, there is no need to sort */
9376 if (infoPtr->nItemCount < 2) return TRUE;
9377 if (!(hdpaItems = DPA_Clone(infoPtr->hdpaItems, NULL))) return FALSE;
9379 /* clear selection */
9380 ranges_clear(infoPtr->selectionRanges);
9382 /* save selection mark and focused item */
9383 if (infoPtr->nSelectionMark >= 0)
9384 selectionMarkItem = DPA_GetPtr(infoPtr->hdpaItems, infoPtr->nSelectionMark);
9385 if (infoPtr->nFocusedItem >= 0)
9386 focusedItem = DPA_GetPtr(infoPtr->hdpaItems, infoPtr->nFocusedItem);
9388 context.items = hdpaItems;
9389 context.compare_func = pfnCompare;
9390 context.lParam = lParamSort;
9391 if (IsEx)
9392 DPA_Sort(hdpaItems, LISTVIEW_CallBackCompareEx, (LPARAM)&context);
9393 else
9394 DPA_Sort(hdpaItems, LISTVIEW_CallBackCompare, (LPARAM)&context);
9395 DPA_Destroy(infoPtr->hdpaItems);
9396 infoPtr->hdpaItems = hdpaItems;
9398 /* restore selection ranges */
9399 for (i=0; i < infoPtr->nItemCount; i++)
9401 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, i);
9402 lpItem = DPA_GetPtr(hdpaSubItems, 0);
9404 if (lpItem->state & LVIS_SELECTED)
9405 ranges_additem(infoPtr->selectionRanges, i);
9407 /* restore selection mark and focused item */
9408 infoPtr->nSelectionMark = DPA_GetPtrIndex(infoPtr->hdpaItems, selectionMarkItem);
9409 infoPtr->nFocusedItem = DPA_GetPtrIndex(infoPtr->hdpaItems, focusedItem);
9411 /* I believe nHotItem should be left alone, see LISTVIEW_ShiftIndices */
9413 /* refresh the display */
9414 LISTVIEW_InvalidateList(infoPtr);
9415 return TRUE;
9418 /***
9419 * DESCRIPTION:
9420 * Update theme handle after a theme change.
9422 * PARAMETER(S):
9423 * [I] infoPtr : valid pointer to the listview structure
9425 * RETURN:
9426 * SUCCESS : 0
9427 * FAILURE : something else
9429 static LRESULT LISTVIEW_ThemeChanged(const LISTVIEW_INFO *infoPtr)
9431 HTHEME theme = GetWindowTheme(infoPtr->hwndSelf);
9432 CloseThemeData(theme);
9433 OpenThemeData(infoPtr->hwndSelf, themeClass);
9434 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
9435 return 0;
9438 /***
9439 * DESCRIPTION:
9440 * Updates an items or rearranges the listview control.
9442 * PARAMETER(S):
9443 * [I] infoPtr : valid pointer to the listview structure
9444 * [I] nItem : item index
9446 * RETURN:
9447 * SUCCESS : TRUE
9448 * FAILURE : FALSE
9450 static BOOL LISTVIEW_Update(LISTVIEW_INFO *infoPtr, INT nItem)
9452 TRACE("(nItem=%d)\n", nItem);
9454 if (nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
9456 /* rearrange with default alignment style */
9457 if (is_autoarrange(infoPtr))
9458 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
9459 else
9460 LISTVIEW_InvalidateItem(infoPtr, nItem);
9462 return TRUE;
9465 /***
9466 * DESCRIPTION:
9467 * Draw the track line at the place defined in the infoPtr structure.
9468 * The line is drawn with a XOR pen so drawing the line for the second time
9469 * in the same place erases the line.
9471 * PARAMETER(S):
9472 * [I] infoPtr : valid pointer to the listview structure
9474 * RETURN:
9475 * SUCCESS : TRUE
9476 * FAILURE : FALSE
9478 static BOOL LISTVIEW_DrawTrackLine(const LISTVIEW_INFO *infoPtr)
9480 HDC hdc;
9482 if (infoPtr->xTrackLine == -1)
9483 return FALSE;
9485 if (!(hdc = GetDC(infoPtr->hwndSelf)))
9486 return FALSE;
9487 PatBlt( hdc, infoPtr->xTrackLine, infoPtr->rcList.top,
9488 1, infoPtr->rcList.bottom - infoPtr->rcList.top, DSTINVERT );
9489 ReleaseDC(infoPtr->hwndSelf, hdc);
9490 return TRUE;
9493 /***
9494 * DESCRIPTION:
9495 * Called when an edit control should be displayed. This function is called after
9496 * we are sure that there was a single click - not a double click (this is a TIMERPROC).
9498 * PARAMETER(S):
9499 * [I] hwnd : Handle to the listview
9500 * [I] uMsg : WM_TIMER (ignored)
9501 * [I] idEvent : The timer ID interpreted as a pointer to a DELAYED_EDIT_ITEM struct
9502 * [I] dwTimer : The elapsed time (ignored)
9504 * RETURN:
9505 * None.
9507 static VOID CALLBACK LISTVIEW_DelayedEditItem(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
9509 DELAYED_ITEM_EDIT *editItem = (DELAYED_ITEM_EDIT *)idEvent;
9510 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongPtrW(hwnd, 0);
9512 KillTimer(hwnd, idEvent);
9513 editItem->fEnabled = FALSE;
9514 /* check if the item is still selected */
9515 if (infoPtr->bFocus && LISTVIEW_GetItemState(infoPtr, editItem->iItem, LVIS_SELECTED))
9516 LISTVIEW_EditLabelT(infoPtr, editItem->iItem, TRUE);
9519 /***
9520 * DESCRIPTION:
9521 * Creates the listview control - the WM_NCCREATE phase.
9523 * PARAMETER(S):
9524 * [I] hwnd : window handle
9525 * [I] lpcs : the create parameters
9527 * RETURN:
9528 * Success: TRUE
9529 * Failure: FALSE
9531 static LRESULT LISTVIEW_NCCreate(HWND hwnd, WPARAM wParam, const CREATESTRUCTW *lpcs)
9533 LISTVIEW_INFO *infoPtr;
9534 LOGFONTW logFont;
9536 TRACE("(lpcs=%p)\n", lpcs);
9538 /* initialize info pointer */
9539 infoPtr = Alloc(sizeof(LISTVIEW_INFO));
9540 if (!infoPtr) return FALSE;
9542 SetWindowLongPtrW(hwnd, 0, (DWORD_PTR)infoPtr);
9544 infoPtr->hwndSelf = hwnd;
9545 infoPtr->dwStyle = lpcs->style; /* Note: may be changed in WM_CREATE */
9546 map_style_view(infoPtr);
9547 /* determine the type of structures to use */
9548 infoPtr->hwndNotify = lpcs->hwndParent;
9549 /* infoPtr->notifyFormat will be filled in WM_CREATE */
9551 /* initialize color information */
9552 infoPtr->clrBk = CLR_NONE;
9553 infoPtr->clrText = CLR_DEFAULT;
9554 infoPtr->clrTextBk = CLR_DEFAULT;
9555 LISTVIEW_SetBkColor(infoPtr, comctl32_color.clrWindow);
9557 /* set default values */
9558 infoPtr->nFocusedItem = -1;
9559 infoPtr->nSelectionMark = -1;
9560 infoPtr->nHotItem = -1;
9561 infoPtr->redraw = TRUE;
9562 infoPtr->bNoItemMetrics = TRUE;
9563 infoPtr->notify_mask = NOTIFY_MASK_UNMASK_ALL;
9564 infoPtr->autoSpacing = TRUE;
9565 infoPtr->iconSpacing.cx = GetSystemMetrics(SM_CXICONSPACING) - GetSystemMetrics(SM_CXICON);
9566 infoPtr->iconSpacing.cy = GetSystemMetrics(SM_CYICONSPACING) - GetSystemMetrics(SM_CYICON);
9567 infoPtr->nEditLabelItem = -1;
9568 infoPtr->nLButtonDownItem = -1;
9569 infoPtr->dwHoverTime = HOVER_DEFAULT; /* default system hover time */
9570 infoPtr->cWheelRemainder = 0;
9571 infoPtr->nMeasureItemHeight = 0;
9572 infoPtr->xTrackLine = -1; /* no track line */
9573 infoPtr->itemEdit.fEnabled = FALSE;
9574 infoPtr->iVersion = COMCTL32_VERSION;
9575 infoPtr->colRectsDirty = FALSE;
9576 infoPtr->selected_column = -1;
9578 /* get default font (icon title) */
9579 SystemParametersInfoW(SPI_GETICONTITLELOGFONT, 0, &logFont, 0);
9580 infoPtr->hDefaultFont = CreateFontIndirectW(&logFont);
9581 infoPtr->hFont = infoPtr->hDefaultFont;
9582 LISTVIEW_SaveTextMetrics(infoPtr);
9584 /* allocate memory for the data structure */
9585 if (!(infoPtr->selectionRanges = ranges_create(10))) goto fail;
9586 if (!(infoPtr->hdpaItems = DPA_Create(10))) goto fail;
9587 if (!(infoPtr->hdpaItemIds = DPA_Create(10))) goto fail;
9588 if (!(infoPtr->hdpaPosX = DPA_Create(10))) goto fail;
9589 if (!(infoPtr->hdpaPosY = DPA_Create(10))) goto fail;
9590 if (!(infoPtr->hdpaColumns = DPA_Create(10))) goto fail;
9592 return DefWindowProcW(hwnd, WM_NCCREATE, wParam, (LPARAM)lpcs);
9594 fail:
9595 DestroyWindow(infoPtr->hwndHeader);
9596 ranges_destroy(infoPtr->selectionRanges);
9597 DPA_Destroy(infoPtr->hdpaItems);
9598 DPA_Destroy(infoPtr->hdpaItemIds);
9599 DPA_Destroy(infoPtr->hdpaPosX);
9600 DPA_Destroy(infoPtr->hdpaPosY);
9601 DPA_Destroy(infoPtr->hdpaColumns);
9602 Free(infoPtr);
9603 return FALSE;
9606 /***
9607 * DESCRIPTION:
9608 * Creates the listview control - the WM_CREATE phase. Most of the data is
9609 * already set up in LISTVIEW_NCCreate
9611 * PARAMETER(S):
9612 * [I] hwnd : window handle
9613 * [I] lpcs : the create parameters
9615 * RETURN:
9616 * Success: 0
9617 * Failure: -1
9619 static LRESULT LISTVIEW_Create(HWND hwnd, const CREATESTRUCTW *lpcs)
9621 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongPtrW(hwnd, 0);
9623 TRACE("lpcs %p, style %#lx\n", lpcs, lpcs->style);
9625 infoPtr->dwStyle = lpcs->style;
9626 map_style_view(infoPtr);
9628 infoPtr->notifyFormat = SendMessageW(infoPtr->hwndNotify, WM_NOTIFYFORMAT,
9629 (WPARAM)infoPtr->hwndSelf, NF_QUERY);
9630 /* on error defaulting to ANSI notifications */
9631 if (infoPtr->notifyFormat == 0) infoPtr->notifyFormat = NFR_ANSI;
9632 TRACE("notify format=%d\n", infoPtr->notifyFormat);
9634 if ((infoPtr->uView == LV_VIEW_DETAILS) && (lpcs->style & WS_VISIBLE))
9636 if (LISTVIEW_CreateHeader(infoPtr) < 0) return -1;
9638 else
9639 infoPtr->hwndHeader = 0;
9641 /* init item size to avoid division by 0 */
9642 LISTVIEW_UpdateItemSize (infoPtr);
9643 LISTVIEW_UpdateSize (infoPtr);
9645 if (infoPtr->uView == LV_VIEW_DETAILS)
9647 if (!(LVS_NOCOLUMNHEADER & lpcs->style) && (WS_VISIBLE & lpcs->style))
9649 ShowWindow(infoPtr->hwndHeader, SW_SHOWNORMAL);
9651 LISTVIEW_UpdateScroll(infoPtr);
9652 /* send WM_MEASUREITEM notification */
9653 if (infoPtr->dwStyle & LVS_OWNERDRAWFIXED) notify_measureitem(infoPtr);
9656 OpenThemeData(hwnd, themeClass);
9658 /* initialize the icon sizes */
9659 set_icon_size(&infoPtr->iconSize, infoPtr->himlNormal, infoPtr->uView != LV_VIEW_ICON);
9660 set_icon_size(&infoPtr->iconStateSize, infoPtr->himlState, TRUE);
9661 return 0;
9664 /***
9665 * DESCRIPTION:
9666 * Destroys the listview control.
9668 * PARAMETER(S):
9669 * [I] infoPtr : valid pointer to the listview structure
9671 * RETURN:
9672 * Success: 0
9673 * Failure: -1
9675 static LRESULT LISTVIEW_Destroy(LISTVIEW_INFO *infoPtr)
9677 HTHEME theme = GetWindowTheme(infoPtr->hwndSelf);
9678 CloseThemeData(theme);
9680 /* delete all items */
9681 LISTVIEW_DeleteAllItems(infoPtr, TRUE);
9683 return 0;
9686 /***
9687 * DESCRIPTION:
9688 * Enables the listview control.
9690 * PARAMETER(S):
9691 * [I] infoPtr : valid pointer to the listview structure
9692 * [I] bEnable : specifies whether to enable or disable the window
9694 * RETURN:
9695 * SUCCESS : TRUE
9696 * FAILURE : FALSE
9698 static BOOL LISTVIEW_Enable(const LISTVIEW_INFO *infoPtr)
9700 if (infoPtr->dwStyle & LVS_OWNERDRAWFIXED)
9701 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
9702 return TRUE;
9705 /***
9706 * DESCRIPTION:
9707 * Erases the background of the listview control.
9709 * PARAMETER(S):
9710 * [I] infoPtr : valid pointer to the listview structure
9711 * [I] hdc : device context handle
9713 * RETURN:
9714 * SUCCESS : TRUE
9715 * FAILURE : FALSE
9717 static inline BOOL LISTVIEW_EraseBkgnd(const LISTVIEW_INFO *infoPtr, HDC hdc)
9719 RECT rc;
9721 TRACE("(hdc=%p)\n", hdc);
9723 if (!GetClipBox(hdc, &rc)) return FALSE;
9725 if (infoPtr->clrBk == CLR_NONE)
9727 if (infoPtr->dwLvExStyle & LVS_EX_TRANSPARENTBKGND)
9728 SendMessageW(infoPtr->hwndNotify, WM_PRINTCLIENT, (WPARAM)hdc, PRF_ERASEBKGND);
9729 else
9730 SendMessageW(infoPtr->hwndNotify, WM_ERASEBKGND, (WPARAM)hdc, 0);
9731 LISTVIEW_DrawBackgroundBitmap(infoPtr, hdc, &rc);
9732 return TRUE;
9735 /* for double buffered controls we need to do this during refresh */
9736 if (infoPtr->dwLvExStyle & LVS_EX_DOUBLEBUFFER) return FALSE;
9738 return LISTVIEW_FillBkgnd(infoPtr, hdc, &rc);
9742 /***
9743 * DESCRIPTION:
9744 * Helper function for LISTVIEW_[HV]Scroll *only*.
9745 * Performs vertical/horizontal scrolling by a give amount.
9747 * PARAMETER(S):
9748 * [I] infoPtr : valid pointer to the listview structure
9749 * [I] dx : amount of horizontal scroll
9750 * [I] dy : amount of vertical scroll
9752 static void scroll_list(LISTVIEW_INFO *infoPtr, INT dx, INT dy)
9754 /* now we can scroll the list */
9755 ScrollWindowEx(infoPtr->hwndSelf, dx, dy, &infoPtr->rcList,
9756 &infoPtr->rcList, 0, 0, SW_ERASE | SW_INVALIDATE);
9757 /* if we have focus, adjust rect */
9758 OffsetRect(&infoPtr->rcFocus, dx, dy);
9759 UpdateWindow(infoPtr->hwndSelf);
9762 /***
9763 * DESCRIPTION:
9764 * Performs vertical scrolling.
9766 * PARAMETER(S):
9767 * [I] infoPtr : valid pointer to the listview structure
9768 * [I] nScrollCode : scroll code
9769 * [I] nScrollDiff : units to scroll in SB_INTERNAL mode, 0 otherwise
9770 * [I] hScrollWnd : scrollbar control window handle
9772 * RETURN:
9773 * Zero
9775 * NOTES:
9776 * SB_LINEUP/SB_LINEDOWN:
9777 * for LVS_ICON, LVS_SMALLICON is 37 by experiment
9778 * for LVS_REPORT is 1 line
9779 * for LVS_LIST cannot occur
9782 static LRESULT LISTVIEW_VScroll(LISTVIEW_INFO *infoPtr, INT nScrollCode,
9783 INT nScrollDiff)
9785 INT nOldScrollPos, nNewScrollPos;
9786 SCROLLINFO scrollInfo;
9787 BOOL is_an_icon;
9789 TRACE("(nScrollCode=%d(%s), nScrollDiff=%d)\n", nScrollCode,
9790 debugscrollcode(nScrollCode), nScrollDiff);
9792 if (infoPtr->hwndEdit) SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
9794 scrollInfo.cbSize = sizeof(SCROLLINFO);
9795 scrollInfo.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_TRACKPOS;
9797 is_an_icon = ((infoPtr->uView == LV_VIEW_ICON) || (infoPtr->uView == LV_VIEW_SMALLICON));
9799 if (!GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo)) return 1;
9801 nOldScrollPos = scrollInfo.nPos;
9802 switch (nScrollCode)
9804 case SB_INTERNAL:
9805 break;
9807 case SB_LINEUP:
9808 nScrollDiff = (is_an_icon) ? -LISTVIEW_SCROLL_ICON_LINE_SIZE : -1;
9809 break;
9811 case SB_LINEDOWN:
9812 nScrollDiff = (is_an_icon) ? LISTVIEW_SCROLL_ICON_LINE_SIZE : 1;
9813 break;
9815 case SB_PAGEUP:
9816 nScrollDiff = -scrollInfo.nPage;
9817 break;
9819 case SB_PAGEDOWN:
9820 nScrollDiff = scrollInfo.nPage;
9821 break;
9823 case SB_THUMBPOSITION:
9824 case SB_THUMBTRACK:
9825 nScrollDiff = scrollInfo.nTrackPos - scrollInfo.nPos;
9826 break;
9828 default:
9829 nScrollDiff = 0;
9832 /* quit right away if pos isn't changing */
9833 if (nScrollDiff == 0) return 0;
9835 /* calculate new position, and handle overflows */
9836 nNewScrollPos = scrollInfo.nPos + nScrollDiff;
9837 if (nScrollDiff > 0) {
9838 if (nNewScrollPos < nOldScrollPos ||
9839 nNewScrollPos > scrollInfo.nMax)
9840 nNewScrollPos = scrollInfo.nMax;
9841 } else {
9842 if (nNewScrollPos > nOldScrollPos ||
9843 nNewScrollPos < scrollInfo.nMin)
9844 nNewScrollPos = scrollInfo.nMin;
9847 /* set the new position, and reread in case it changed */
9848 scrollInfo.fMask = SIF_POS;
9849 scrollInfo.nPos = nNewScrollPos;
9850 nNewScrollPos = SetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo, TRUE);
9852 /* carry on only if it really changed */
9853 if (nNewScrollPos == nOldScrollPos) return 0;
9855 /* now adjust to client coordinates */
9856 nScrollDiff = nOldScrollPos - nNewScrollPos;
9857 if (infoPtr->uView == LV_VIEW_DETAILS) nScrollDiff *= infoPtr->nItemHeight;
9859 /* and scroll the window */
9860 scroll_list(infoPtr, 0, nScrollDiff);
9862 return 0;
9865 /***
9866 * DESCRIPTION:
9867 * Performs horizontal scrolling.
9869 * PARAMETER(S):
9870 * [I] infoPtr : valid pointer to the listview structure
9871 * [I] nScrollCode : scroll code
9872 * [I] nScrollDiff : units to scroll in SB_INTERNAL mode, 0 otherwise
9873 * [I] hScrollWnd : scrollbar control window handle
9875 * RETURN:
9876 * Zero
9878 * NOTES:
9879 * SB_LINELEFT/SB_LINERIGHT:
9880 * for LVS_ICON, LVS_SMALLICON 1 pixel
9881 * for LVS_REPORT is 1 pixel
9882 * for LVS_LIST is 1 column --> which is a 1 because the
9883 * scroll is based on columns not pixels
9886 static LRESULT LISTVIEW_HScroll(LISTVIEW_INFO *infoPtr, INT nScrollCode,
9887 INT nScrollDiff)
9889 INT nOldScrollPos, nNewScrollPos;
9890 SCROLLINFO scrollInfo;
9891 BOOL is_an_icon;
9893 TRACE("(nScrollCode=%d(%s), nScrollDiff=%d)\n", nScrollCode,
9894 debugscrollcode(nScrollCode), nScrollDiff);
9896 if (infoPtr->hwndEdit) SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
9898 scrollInfo.cbSize = sizeof(SCROLLINFO);
9899 scrollInfo.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_TRACKPOS;
9901 is_an_icon = ((infoPtr->uView == LV_VIEW_ICON) || (infoPtr->uView == LV_VIEW_SMALLICON));
9903 if (!GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo)) return 1;
9905 nOldScrollPos = scrollInfo.nPos;
9907 switch (nScrollCode)
9909 case SB_INTERNAL:
9910 break;
9912 case SB_LINELEFT:
9913 nScrollDiff = (is_an_icon) ? -LISTVIEW_SCROLL_ICON_LINE_SIZE : -1;
9914 break;
9916 case SB_LINERIGHT:
9917 nScrollDiff = (is_an_icon) ? LISTVIEW_SCROLL_ICON_LINE_SIZE : 1;
9918 break;
9920 case SB_PAGELEFT:
9921 nScrollDiff = -scrollInfo.nPage;
9922 break;
9924 case SB_PAGERIGHT:
9925 nScrollDiff = scrollInfo.nPage;
9926 break;
9928 case SB_THUMBPOSITION:
9929 case SB_THUMBTRACK:
9930 nScrollDiff = scrollInfo.nTrackPos - scrollInfo.nPos;
9931 break;
9933 default:
9934 nScrollDiff = 0;
9937 /* quit right away if pos isn't changing */
9938 if (nScrollDiff == 0) return 0;
9940 /* calculate new position, and handle overflows */
9941 nNewScrollPos = scrollInfo.nPos + nScrollDiff;
9942 if (nScrollDiff > 0) {
9943 if (nNewScrollPos < nOldScrollPos ||
9944 nNewScrollPos > scrollInfo.nMax)
9945 nNewScrollPos = scrollInfo.nMax;
9946 } else {
9947 if (nNewScrollPos > nOldScrollPos ||
9948 nNewScrollPos < scrollInfo.nMin)
9949 nNewScrollPos = scrollInfo.nMin;
9952 /* set the new position, and reread in case it changed */
9953 scrollInfo.fMask = SIF_POS;
9954 scrollInfo.nPos = nNewScrollPos;
9955 nNewScrollPos = SetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo, TRUE);
9957 /* carry on only if it really changed */
9958 if (nNewScrollPos == nOldScrollPos) return 0;
9960 LISTVIEW_UpdateHeaderSize(infoPtr, nNewScrollPos);
9962 /* now adjust to client coordinates */
9963 nScrollDiff = nOldScrollPos - nNewScrollPos;
9964 if (infoPtr->uView == LV_VIEW_LIST) nScrollDiff *= infoPtr->nItemWidth;
9966 /* and scroll the window */
9967 scroll_list(infoPtr, nScrollDiff, 0);
9969 return 0;
9972 static LRESULT LISTVIEW_MouseWheel(LISTVIEW_INFO *infoPtr, INT wheelDelta)
9974 INT pulScrollLines = 3;
9976 TRACE("(wheelDelta=%d)\n", wheelDelta);
9978 switch(infoPtr->uView)
9980 case LV_VIEW_ICON:
9981 case LV_VIEW_SMALLICON:
9983 * listview should be scrolled by a multiple of 37 dependently on its dimension or its visible item number
9984 * should be fixed in the future.
9986 LISTVIEW_VScroll(infoPtr, SB_INTERNAL, (wheelDelta > 0) ?
9987 -LISTVIEW_SCROLL_ICON_LINE_SIZE : LISTVIEW_SCROLL_ICON_LINE_SIZE);
9988 break;
9990 case LV_VIEW_DETAILS:
9991 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
9993 /* if scrolling changes direction, ignore left overs */
9994 if ((wheelDelta < 0 && infoPtr->cWheelRemainder < 0) ||
9995 (wheelDelta > 0 && infoPtr->cWheelRemainder > 0))
9996 infoPtr->cWheelRemainder += wheelDelta;
9997 else
9998 infoPtr->cWheelRemainder = wheelDelta;
9999 if (infoPtr->cWheelRemainder && pulScrollLines)
10001 int cLineScroll;
10002 pulScrollLines = min((UINT)LISTVIEW_GetCountPerColumn(infoPtr), pulScrollLines);
10003 cLineScroll = pulScrollLines * infoPtr->cWheelRemainder / WHEEL_DELTA;
10004 infoPtr->cWheelRemainder -= WHEEL_DELTA * cLineScroll / pulScrollLines;
10005 LISTVIEW_VScroll(infoPtr, SB_INTERNAL, -cLineScroll);
10007 break;
10009 case LV_VIEW_LIST:
10010 LISTVIEW_HScroll(infoPtr, (wheelDelta > 0) ? SB_LINELEFT : SB_LINERIGHT, 0);
10011 break;
10013 return 0;
10016 /***
10017 * DESCRIPTION:
10018 * ???
10020 * PARAMETER(S):
10021 * [I] infoPtr : valid pointer to the listview structure
10022 * [I] nVirtualKey : virtual key
10023 * [I] lKeyData : key data
10025 * RETURN:
10026 * Zero
10028 static LRESULT LISTVIEW_KeyDown(LISTVIEW_INFO *infoPtr, INT nVirtualKey, LONG lKeyData)
10030 HWND hwndSelf = infoPtr->hwndSelf;
10031 INT nItem = -1;
10032 NMLVKEYDOWN nmKeyDown;
10034 TRACE("(nVirtualKey=%d, lKeyData=%ld)\n", nVirtualKey, lKeyData);
10036 /* send LVN_KEYDOWN notification */
10037 nmKeyDown.wVKey = nVirtualKey;
10038 nmKeyDown.flags = 0;
10039 notify_hdr(infoPtr, LVN_KEYDOWN, &nmKeyDown.hdr);
10040 if (!IsWindow(hwndSelf))
10041 return 0;
10043 switch (nVirtualKey)
10045 case VK_SPACE:
10046 nItem = infoPtr->nFocusedItem;
10047 if (infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES)
10048 toggle_checkbox_state(infoPtr, infoPtr->nFocusedItem);
10049 break;
10051 case VK_RETURN:
10052 if ((infoPtr->nItemCount > 0) && (infoPtr->nFocusedItem != -1))
10054 if (!notify(infoPtr, NM_RETURN)) return 0;
10055 if (!notify(infoPtr, LVN_ITEMACTIVATE)) return 0;
10057 break;
10059 case VK_HOME:
10060 if (infoPtr->nItemCount > 0)
10061 nItem = 0;
10062 break;
10064 case VK_END:
10065 if (infoPtr->nItemCount > 0)
10066 nItem = infoPtr->nItemCount - 1;
10067 break;
10069 case VK_LEFT:
10070 nItem = LISTVIEW_GetNextItem(infoPtr, infoPtr->nFocusedItem, LVNI_TOLEFT);
10071 break;
10073 case VK_UP:
10074 nItem = LISTVIEW_GetNextItem(infoPtr, infoPtr->nFocusedItem, LVNI_ABOVE);
10075 break;
10077 case VK_RIGHT:
10078 nItem = LISTVIEW_GetNextItem(infoPtr, infoPtr->nFocusedItem, LVNI_TORIGHT);
10079 break;
10081 case VK_DOWN:
10082 nItem = LISTVIEW_GetNextItem(infoPtr, infoPtr->nFocusedItem, LVNI_BELOW);
10083 break;
10085 case VK_PRIOR:
10086 if (infoPtr->uView == LV_VIEW_DETAILS)
10088 INT topidx = LISTVIEW_GetTopIndex(infoPtr);
10089 if (infoPtr->nFocusedItem == topidx)
10090 nItem = topidx - LISTVIEW_GetCountPerColumn(infoPtr) + 1;
10091 else
10092 nItem = topidx;
10094 else
10095 nItem = infoPtr->nFocusedItem - LISTVIEW_GetCountPerColumn(infoPtr)
10096 * LISTVIEW_GetCountPerRow(infoPtr);
10097 if(nItem < 0) nItem = 0;
10098 break;
10100 case VK_NEXT:
10101 if (infoPtr->uView == LV_VIEW_DETAILS)
10103 INT topidx = LISTVIEW_GetTopIndex(infoPtr);
10104 INT cnt = LISTVIEW_GetCountPerColumn(infoPtr);
10105 if (infoPtr->nFocusedItem == topidx + cnt - 1)
10106 nItem = infoPtr->nFocusedItem + cnt - 1;
10107 else
10108 nItem = topidx + cnt - 1;
10110 else
10111 nItem = infoPtr->nFocusedItem + LISTVIEW_GetCountPerColumn(infoPtr)
10112 * LISTVIEW_GetCountPerRow(infoPtr);
10113 if(nItem >= infoPtr->nItemCount) nItem = infoPtr->nItemCount - 1;
10114 break;
10117 if ((nItem != -1) && (nItem != infoPtr->nFocusedItem || nVirtualKey == VK_SPACE))
10118 LISTVIEW_KeySelection(infoPtr, nItem, nVirtualKey == VK_SPACE);
10120 return 0;
10123 /***
10124 * DESCRIPTION:
10125 * Kills the focus.
10127 * PARAMETER(S):
10128 * [I] infoPtr : valid pointer to the listview structure
10130 * RETURN:
10131 * Zero
10133 static LRESULT LISTVIEW_KillFocus(LISTVIEW_INFO *infoPtr)
10135 TRACE("()\n");
10137 /* drop any left over scroll amount */
10138 infoPtr->cWheelRemainder = 0;
10140 /* if we did not have the focus, there's nothing more to do */
10141 if (!infoPtr->bFocus) return 0;
10143 /* send NM_KILLFOCUS notification */
10144 if (!notify(infoPtr, NM_KILLFOCUS)) return 0;
10146 /* if we have a focus rectangle, get rid of it */
10147 LISTVIEW_ShowFocusRect(infoPtr, FALSE);
10149 /* if have a marquee selection, stop it */
10150 if (infoPtr->bMarqueeSelect)
10152 /* Remove the marquee rectangle and release our mouse capture */
10153 LISTVIEW_InvalidateRect(infoPtr, &infoPtr->marqueeRect);
10154 ReleaseCapture();
10156 SetRectEmpty(&infoPtr->marqueeRect);
10158 infoPtr->bMarqueeSelect = FALSE;
10159 infoPtr->bScrolling = FALSE;
10160 KillTimer(infoPtr->hwndSelf, (UINT_PTR) infoPtr);
10163 /* set window focus flag */
10164 infoPtr->bFocus = FALSE;
10166 /* invalidate the selected items before resetting focus flag */
10167 LISTVIEW_InvalidateSelectedItems(infoPtr);
10169 return 0;
10172 /***
10173 * DESCRIPTION:
10174 * Processes double click messages (left mouse button).
10176 * PARAMETER(S):
10177 * [I] infoPtr : valid pointer to the listview structure
10178 * [I] wKey : key flag
10179 * [I] x,y : mouse coordinate
10181 * RETURN:
10182 * Zero
10184 static LRESULT LISTVIEW_LButtonDblClk(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
10186 LVHITTESTINFO htInfo;
10188 TRACE("(key=%hu, X=%u, Y=%u)\n", wKey, x, y);
10190 /* Cancel the item edition if any */
10191 if (infoPtr->itemEdit.fEnabled)
10193 KillTimer(infoPtr->hwndSelf, (UINT_PTR)&infoPtr->itemEdit);
10194 infoPtr->itemEdit.fEnabled = FALSE;
10197 /* send NM_RELEASEDCAPTURE notification */
10198 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
10200 htInfo.pt.x = x;
10201 htInfo.pt.y = y;
10203 /* send NM_DBLCLK notification */
10204 LISTVIEW_HitTest(infoPtr, &htInfo, TRUE, FALSE);
10205 if (!notify_click(infoPtr, NM_DBLCLK, &htInfo)) return 0;
10207 /* To send the LVN_ITEMACTIVATE, it must be on an Item */
10208 if(htInfo.iItem != -1) notify_itemactivate(infoPtr,&htInfo);
10210 return 0;
10213 static LRESULT LISTVIEW_TrackMouse(const LISTVIEW_INFO *infoPtr, POINT pt)
10215 MSG msg;
10216 RECT r;
10218 r.top = r.bottom = pt.y;
10219 r.left = r.right = pt.x;
10221 InflateRect(&r, GetSystemMetrics(SM_CXDRAG), GetSystemMetrics(SM_CYDRAG));
10223 SetCapture(infoPtr->hwndSelf);
10225 while (1)
10227 if (PeekMessageW(&msg, 0, 0, 0, PM_REMOVE | PM_NOYIELD))
10229 if (msg.message == WM_MOUSEMOVE)
10231 pt.x = (short)LOWORD(msg.lParam);
10232 pt.y = (short)HIWORD(msg.lParam);
10233 if (PtInRect(&r, pt))
10234 continue;
10235 else
10237 ReleaseCapture();
10238 return 1;
10241 else if (msg.message >= WM_LBUTTONDOWN &&
10242 msg.message <= WM_RBUTTONDBLCLK)
10244 break;
10247 DispatchMessageW(&msg);
10250 if (GetCapture() != infoPtr->hwndSelf)
10251 return 0;
10254 ReleaseCapture();
10255 return 0;
10259 /***
10260 * DESCRIPTION:
10261 * Processes mouse down messages (left mouse button).
10263 * PARAMETERS:
10264 * infoPtr [I ] valid pointer to the listview structure
10265 * wKey [I ] key flag
10266 * x,y [I ] mouse coordinate
10268 * RETURN:
10269 * Zero
10271 static LRESULT LISTVIEW_LButtonDown(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
10273 LVHITTESTINFO lvHitTestInfo;
10274 static BOOL bGroupSelect = TRUE;
10275 POINT pt = { x, y };
10276 INT nItem;
10278 TRACE("(key=%hu, X=%u, Y=%u)\n", wKey, x, y);
10280 /* send NM_RELEASEDCAPTURE notification */
10281 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
10283 /* set left button down flag and record the click position */
10284 infoPtr->bLButtonDown = TRUE;
10285 infoPtr->ptClickPos = pt;
10286 infoPtr->bDragging = FALSE;
10287 infoPtr->bMarqueeSelect = FALSE;
10288 infoPtr->bScrolling = FALSE;
10290 lvHitTestInfo.pt.x = x;
10291 lvHitTestInfo.pt.y = y;
10293 nItem = LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, TRUE);
10294 TRACE("at %s, nItem=%d\n", wine_dbgstr_point(&pt), nItem);
10295 if ((nItem >= 0) && (nItem < infoPtr->nItemCount))
10297 if ((infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES) && (lvHitTestInfo.flags & LVHT_ONITEMSTATEICON))
10299 notify_click(infoPtr, NM_CLICK, &lvHitTestInfo);
10300 toggle_checkbox_state(infoPtr, nItem);
10301 infoPtr->bLButtonDown = FALSE;
10302 return 0;
10305 if (infoPtr->dwStyle & LVS_SINGLESEL)
10307 if (LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED))
10308 infoPtr->nEditLabelItem = nItem;
10309 else
10310 LISTVIEW_SetSelection(infoPtr, nItem);
10312 else
10314 if ((wKey & MK_CONTROL) && (wKey & MK_SHIFT))
10316 if (bGroupSelect)
10318 if (!LISTVIEW_AddGroupSelection(infoPtr, nItem)) return 0;
10319 LISTVIEW_SetItemFocus(infoPtr, nItem);
10320 infoPtr->nSelectionMark = nItem;
10322 else
10324 LVITEMW item;
10326 item.state = LVIS_SELECTED | LVIS_FOCUSED;
10327 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
10329 LISTVIEW_SetItemState(infoPtr,nItem,&item);
10330 infoPtr->nSelectionMark = nItem;
10333 else if (wKey & MK_CONTROL)
10335 LVITEMW item;
10337 bGroupSelect = (LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED) == 0);
10339 item.state = (bGroupSelect ? LVIS_SELECTED : 0) | LVIS_FOCUSED;
10340 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
10341 LISTVIEW_SetItemState(infoPtr, nItem, &item);
10342 infoPtr->nSelectionMark = nItem;
10344 else if (wKey & MK_SHIFT)
10346 LISTVIEW_SetGroupSelection(infoPtr, nItem);
10348 else
10350 if (LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED))
10352 infoPtr->nEditLabelItem = nItem;
10353 infoPtr->nLButtonDownItem = nItem;
10355 LISTVIEW_SetItemFocus(infoPtr, nItem);
10357 else
10358 /* set selection (clears other pre-existing selections) */
10359 LISTVIEW_SetSelection(infoPtr, nItem);
10363 if (!infoPtr->bFocus)
10364 SetFocus(infoPtr->hwndSelf);
10366 if (infoPtr->dwLvExStyle & LVS_EX_ONECLICKACTIVATE)
10367 if(lvHitTestInfo.iItem != -1) notify_itemactivate(infoPtr,&lvHitTestInfo);
10369 else
10371 if (!infoPtr->bFocus)
10372 SetFocus(infoPtr->hwndSelf);
10374 /* remove all selections */
10375 if (!(wKey & MK_CONTROL) && !(wKey & MK_SHIFT))
10376 LISTVIEW_DeselectAll(infoPtr);
10377 ReleaseCapture();
10380 return 0;
10383 /***
10384 * DESCRIPTION:
10385 * Processes mouse up messages (left mouse button).
10387 * PARAMETERS:
10388 * infoPtr [I ] valid pointer to the listview structure
10389 * wKey [I ] key flag
10390 * x,y [I ] mouse coordinate
10392 * RETURN:
10393 * Zero
10395 static LRESULT LISTVIEW_LButtonUp(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
10397 LVHITTESTINFO lvHitTestInfo;
10399 TRACE("(key=%hu, X=%u, Y=%u)\n", wKey, x, y);
10401 if (!infoPtr->bLButtonDown) return 0;
10403 lvHitTestInfo.pt.x = x;
10404 lvHitTestInfo.pt.y = y;
10406 /* send NM_CLICK notification */
10407 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
10408 if (!notify_click(infoPtr, NM_CLICK, &lvHitTestInfo)) return 0;
10410 /* set left button flag */
10411 infoPtr->bLButtonDown = FALSE;
10413 /* set a single selection, reset others */
10414 if(lvHitTestInfo.iItem == infoPtr->nLButtonDownItem && lvHitTestInfo.iItem != -1)
10415 LISTVIEW_SetSelection(infoPtr, infoPtr->nLButtonDownItem);
10416 infoPtr->nLButtonDownItem = -1;
10418 if (infoPtr->bDragging || infoPtr->bMarqueeSelect)
10420 /* Remove the marquee rectangle and release our mouse capture */
10421 if (infoPtr->bMarqueeSelect)
10423 LISTVIEW_InvalidateRect(infoPtr, &infoPtr->marqueeDrawRect);
10424 ReleaseCapture();
10427 SetRectEmpty(&infoPtr->marqueeRect);
10428 SetRectEmpty(&infoPtr->marqueeDrawRect);
10430 infoPtr->bDragging = FALSE;
10431 infoPtr->bMarqueeSelect = FALSE;
10432 infoPtr->bScrolling = FALSE;
10434 KillTimer(infoPtr->hwndSelf, (UINT_PTR) infoPtr);
10435 return 0;
10438 /* if we clicked on a selected item, edit the label */
10439 if(lvHitTestInfo.iItem == infoPtr->nEditLabelItem && (lvHitTestInfo.flags & LVHT_ONITEMLABEL))
10441 /* we want to make sure the user doesn't want to do a double click. So we will
10442 * delay the edit. WM_LBUTTONDBLCLICK will cancel the timer
10444 infoPtr->itemEdit.fEnabled = TRUE;
10445 infoPtr->itemEdit.iItem = lvHitTestInfo.iItem;
10446 SetTimer(infoPtr->hwndSelf,
10447 (UINT_PTR)&infoPtr->itemEdit,
10448 GetDoubleClickTime(),
10449 LISTVIEW_DelayedEditItem);
10452 return 0;
10455 /***
10456 * DESCRIPTION:
10457 * Destroys the listview control (called after WM_DESTROY).
10459 * PARAMETER(S):
10460 * [I] infoPtr : valid pointer to the listview structure
10462 * RETURN:
10463 * Zero
10465 static LRESULT LISTVIEW_NCDestroy(LISTVIEW_INFO *infoPtr)
10467 INT i;
10469 TRACE("()\n");
10471 /* destroy data structure */
10472 DPA_Destroy(infoPtr->hdpaItems);
10473 DPA_Destroy(infoPtr->hdpaItemIds);
10474 DPA_Destroy(infoPtr->hdpaPosX);
10475 DPA_Destroy(infoPtr->hdpaPosY);
10476 /* columns */
10477 for (i = 0; i < DPA_GetPtrCount(infoPtr->hdpaColumns); i++)
10478 Free(DPA_GetPtr(infoPtr->hdpaColumns, i));
10479 DPA_Destroy(infoPtr->hdpaColumns);
10480 ranges_destroy(infoPtr->selectionRanges);
10482 /* destroy image lists */
10483 if (!(infoPtr->dwStyle & LVS_SHAREIMAGELISTS))
10485 ImageList_Destroy(infoPtr->himlNormal);
10486 ImageList_Destroy(infoPtr->himlSmall);
10487 ImageList_Destroy(infoPtr->himlState);
10490 /* destroy font, bkgnd brush */
10491 infoPtr->hFont = 0;
10492 if (infoPtr->hDefaultFont) DeleteObject(infoPtr->hDefaultFont);
10493 if (infoPtr->clrBk != CLR_NONE) DeleteObject(infoPtr->hBkBrush);
10494 if (infoPtr->hBkBitmap) DeleteObject(infoPtr->hBkBitmap);
10496 SetWindowLongPtrW(infoPtr->hwndSelf, 0, 0);
10498 /* free listview info pointer*/
10499 Free(infoPtr);
10501 return 0;
10504 /***
10505 * DESCRIPTION:
10506 * Handles notifications.
10508 * PARAMETER(S):
10509 * [I] infoPtr : valid pointer to the listview structure
10510 * [I] lpnmhdr : notification information
10512 * RETURN:
10513 * Zero
10515 static LRESULT LISTVIEW_Notify(LISTVIEW_INFO *infoPtr, NMHDR *lpnmhdr)
10517 NMHEADERW *lpnmh;
10519 TRACE("(lpnmhdr=%p)\n", lpnmhdr);
10521 if (!lpnmhdr || lpnmhdr->hwndFrom != infoPtr->hwndHeader) return 0;
10523 /* remember: HDN_LAST < HDN_FIRST */
10524 if (lpnmhdr->code > HDN_FIRST || lpnmhdr->code < HDN_LAST) return 0;
10525 lpnmh = (NMHEADERW *)lpnmhdr;
10527 if (lpnmh->iItem < 0 || lpnmh->iItem >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return 0;
10529 switch (lpnmhdr->code)
10531 case HDN_TRACKW:
10532 case HDN_TRACKA:
10534 COLUMN_INFO *lpColumnInfo;
10535 POINT ptOrigin;
10536 INT x;
10538 if (!lpnmh->pitem || !(lpnmh->pitem->mask & HDI_WIDTH))
10539 break;
10541 /* remove the old line (if any) */
10542 LISTVIEW_DrawTrackLine(infoPtr);
10544 /* compute & draw the new line */
10545 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, lpnmh->iItem);
10546 x = lpColumnInfo->rcHeader.left + lpnmh->pitem->cxy;
10547 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
10548 infoPtr->xTrackLine = x + ptOrigin.x;
10549 LISTVIEW_DrawTrackLine(infoPtr);
10550 return notify_forward_header(infoPtr, lpnmh);
10553 case HDN_ENDTRACKA:
10554 case HDN_ENDTRACKW:
10555 /* remove the track line (if any) */
10556 LISTVIEW_DrawTrackLine(infoPtr);
10557 infoPtr->xTrackLine = -1;
10558 return notify_forward_header(infoPtr, lpnmh);
10560 case HDN_BEGINDRAG:
10561 if ((infoPtr->dwLvExStyle & LVS_EX_HEADERDRAGDROP) == 0) return 1;
10562 return notify_forward_header(infoPtr, lpnmh);
10564 case HDN_ENDDRAG:
10565 infoPtr->colRectsDirty = TRUE;
10566 LISTVIEW_InvalidateList(infoPtr);
10567 return notify_forward_header(infoPtr, lpnmh);
10569 case HDN_ITEMCHANGEDW:
10570 case HDN_ITEMCHANGEDA:
10572 COLUMN_INFO *lpColumnInfo;
10573 HDITEMW hdi;
10574 INT dx, cxy;
10576 if (!lpnmh->pitem || !(lpnmh->pitem->mask & HDI_WIDTH))
10578 hdi.mask = HDI_WIDTH;
10579 if (!SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, lpnmh->iItem, (LPARAM)&hdi)) return 0;
10580 cxy = hdi.cxy;
10582 else
10583 cxy = lpnmh->pitem->cxy;
10585 /* determine how much we change since the last know position */
10586 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, lpnmh->iItem);
10587 dx = cxy - (lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left);
10588 if (dx != 0)
10590 lpColumnInfo->rcHeader.right += dx;
10592 hdi.mask = HDI_ORDER;
10593 SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, lpnmh->iItem, (LPARAM)&hdi);
10595 /* not the rightmost one */
10596 if (hdi.iOrder + 1 < DPA_GetPtrCount(infoPtr->hdpaColumns))
10598 INT nIndex = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX,
10599 hdi.iOrder + 1, 0);
10600 LISTVIEW_ScrollColumns(infoPtr, nIndex, dx);
10602 else
10604 /* only needs to update the scrolls */
10605 infoPtr->nItemWidth += dx;
10606 LISTVIEW_UpdateScroll(infoPtr);
10608 LISTVIEW_UpdateItemSize(infoPtr);
10609 if (infoPtr->uView == LV_VIEW_DETAILS && is_redrawing(infoPtr))
10611 POINT ptOrigin;
10612 RECT rcCol = lpColumnInfo->rcHeader;
10614 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
10615 OffsetRect(&rcCol, ptOrigin.x, 0);
10617 rcCol.top = infoPtr->rcList.top;
10618 rcCol.bottom = infoPtr->rcList.bottom;
10620 /* resizing left-aligned columns leaves most of the left side untouched */
10621 if ((lpColumnInfo->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_LEFT)
10623 INT nMaxDirty = infoPtr->nEllipsisWidth + infoPtr->ntmMaxCharWidth;
10624 if (dx > 0)
10625 nMaxDirty += dx;
10626 rcCol.left = max (rcCol.left, rcCol.right - nMaxDirty);
10629 /* when shrinking the last column clear the now unused field */
10630 if (hdi.iOrder == DPA_GetPtrCount(infoPtr->hdpaColumns) - 1)
10632 RECT right;
10634 rcCol.right -= dx;
10636 /* deal with right from rightmost column area */
10637 right.left = rcCol.right;
10638 right.top = rcCol.top;
10639 right.bottom = rcCol.bottom;
10640 right.right = infoPtr->rcList.right;
10642 LISTVIEW_InvalidateRect(infoPtr, &right);
10645 LISTVIEW_InvalidateRect(infoPtr, &rcCol);
10648 break;
10651 case HDN_ITEMCLICKW:
10652 case HDN_ITEMCLICKA:
10654 /* Handle sorting by Header Column */
10655 NMLISTVIEW nmlv;
10657 ZeroMemory(&nmlv, sizeof(NMLISTVIEW));
10658 nmlv.iItem = -1;
10659 nmlv.iSubItem = lpnmh->iItem;
10660 notify_listview(infoPtr, LVN_COLUMNCLICK, &nmlv);
10661 return notify_forward_header(infoPtr, lpnmh);
10664 case HDN_DIVIDERDBLCLICKW:
10665 case HDN_DIVIDERDBLCLICKA:
10666 /* FIXME: for LVS_EX_HEADERINALLVIEWS and not LV_VIEW_DETAILS
10667 we should use LVSCW_AUTOSIZE_USEHEADER, helper rework or
10668 split needed for that */
10669 LISTVIEW_SetColumnWidth(infoPtr, lpnmh->iItem, LVSCW_AUTOSIZE);
10670 return notify_forward_header(infoPtr, lpnmh);
10672 return 0;
10675 /***
10676 * DESCRIPTION:
10677 * Paint non-client area of control.
10679 * PARAMETER(S):
10680 * [I] infoPtr : valid pointer to the listview structureof the sender
10681 * [I] region : update region
10683 * RETURN:
10684 * 0 - frame was painted
10686 static LRESULT LISTVIEW_NCPaint(const LISTVIEW_INFO *infoPtr, HRGN region)
10688 LONG exstyle = GetWindowLongW (infoPtr->hwndSelf, GWL_EXSTYLE);
10689 HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
10690 RECT r, window_rect;
10691 HDC dc;
10692 HRGN cliprgn;
10693 int cxEdge = GetSystemMetrics (SM_CXEDGE),
10694 cyEdge = GetSystemMetrics (SM_CYEDGE);
10696 if (!theme || !(exstyle & WS_EX_CLIENTEDGE))
10697 return DefWindowProcW (infoPtr->hwndSelf, WM_NCPAINT, (WPARAM)region, 0);
10699 GetWindowRect(infoPtr->hwndSelf, &r);
10701 cliprgn = CreateRectRgn (r.left + cxEdge, r.top + cyEdge,
10702 r.right - cxEdge, r.bottom - cyEdge);
10703 if (region != (HRGN)1)
10704 CombineRgn (cliprgn, cliprgn, region, RGN_AND);
10706 OffsetRect(&r, -r.left, -r.top);
10707 if (infoPtr->hwndHeader && LISTVIEW_IsHeaderEnabled(infoPtr))
10709 GetWindowRect(infoPtr->hwndHeader, &window_rect);
10710 r.top = min(r.bottom, r.top + window_rect.bottom - window_rect.top);
10713 dc = GetDCEx(infoPtr->hwndSelf, region, DCX_WINDOW|DCX_INTERSECTRGN);
10715 if (IsThemeBackgroundPartiallyTransparent (theme, 0, 0))
10716 DrawThemeParentBackground(infoPtr->hwndSelf, dc, &r);
10717 DrawThemeBackground (theme, dc, 0, 0, &r, 0);
10718 ReleaseDC(infoPtr->hwndSelf, dc);
10720 /* Call default proc to get the scrollbars etc. painted */
10721 DefWindowProcW (infoPtr->hwndSelf, WM_NCPAINT, (WPARAM)cliprgn, 0);
10722 DeleteObject(cliprgn);
10724 return 0;
10727 /***
10728 * DESCRIPTION:
10729 * Determines the type of structure to use.
10731 * PARAMETER(S):
10732 * [I] infoPtr : valid pointer to the listview structureof the sender
10733 * [I] hwndFrom : listview window handle
10734 * [I] nCommand : command specifying the nature of the WM_NOTIFYFORMAT
10736 * RETURN:
10737 * Zero
10739 static LRESULT LISTVIEW_NotifyFormat(LISTVIEW_INFO *infoPtr, HWND hwndFrom, INT nCommand)
10741 TRACE("(hwndFrom=%p, nCommand=%d)\n", hwndFrom, nCommand);
10743 if (nCommand == NF_REQUERY)
10744 infoPtr->notifyFormat = SendMessageW(infoPtr->hwndNotify, WM_NOTIFYFORMAT, (WPARAM)infoPtr->hwndSelf, NF_QUERY);
10746 return infoPtr->notifyFormat;
10749 /***
10750 * DESCRIPTION:
10751 * Paints/Repaints the listview control. Internal use.
10753 * PARAMETER(S):
10754 * [I] infoPtr : valid pointer to the listview structure
10755 * [I] hdc : device context handle
10757 * RETURN:
10758 * Zero
10760 static LRESULT LISTVIEW_Paint(LISTVIEW_INFO *infoPtr, HDC hdc)
10762 TRACE("(hdc=%p)\n", hdc);
10764 if (infoPtr->bNoItemMetrics && infoPtr->nItemCount)
10766 infoPtr->bNoItemMetrics = FALSE;
10767 LISTVIEW_UpdateItemSize(infoPtr);
10768 if (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON)
10769 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
10770 LISTVIEW_UpdateScroll(infoPtr);
10773 if (infoPtr->hwndHeader) UpdateWindow(infoPtr->hwndHeader);
10775 if (hdc)
10776 LISTVIEW_Refresh(infoPtr, hdc, NULL);
10777 else
10779 PAINTSTRUCT ps;
10781 hdc = BeginPaint(infoPtr->hwndSelf, &ps);
10782 if (!hdc) return 1;
10783 LISTVIEW_Refresh(infoPtr, hdc, ps.fErase ? &ps.rcPaint : NULL);
10784 EndPaint(infoPtr->hwndSelf, &ps);
10787 return 0;
10790 /***
10791 * DESCRIPTION:
10792 * Paints/Repaints the listview control, WM_PAINT handler.
10794 * PARAMETER(S):
10795 * [I] infoPtr : valid pointer to the listview structure
10796 * [I] hdc : device context handle
10798 * RETURN:
10799 * Zero
10801 static inline LRESULT LISTVIEW_WMPaint(LISTVIEW_INFO *infoPtr, HDC hdc)
10803 TRACE("(hdc=%p)\n", hdc);
10805 if (!is_redrawing(infoPtr))
10806 return DefWindowProcW (infoPtr->hwndSelf, WM_PAINT, (WPARAM)hdc, 0);
10808 return LISTVIEW_Paint(infoPtr, hdc);
10811 /***
10812 * DESCRIPTION:
10813 * Paints/Repaints the listview control.
10815 * PARAMETER(S):
10816 * [I] infoPtr : valid pointer to the listview structure
10817 * [I] hdc : device context handle
10818 * [I] options : drawing options
10820 * RETURN:
10821 * Zero
10823 static LRESULT LISTVIEW_PrintClient(LISTVIEW_INFO *infoPtr, HDC hdc, DWORD options)
10825 if ((options & PRF_CHECKVISIBLE) && !IsWindowVisible(infoPtr->hwndSelf))
10826 return 0;
10828 if (options & ~(PRF_ERASEBKGND|PRF_CLIENT))
10829 FIXME("(hdc=%p options %#lx) partial stub\n", hdc, options);
10831 if (options & PRF_ERASEBKGND)
10832 LISTVIEW_EraseBkgnd(infoPtr, hdc);
10834 if (options & PRF_CLIENT)
10835 LISTVIEW_Paint(infoPtr, hdc);
10837 return 0;
10841 /***
10842 * DESCRIPTION:
10843 * Processes double click messages (right mouse button).
10845 * PARAMETER(S):
10846 * [I] infoPtr : valid pointer to the listview structure
10847 * [I] wKey : key flag
10848 * [I] x,y : mouse coordinate
10850 * RETURN:
10851 * Zero
10853 static LRESULT LISTVIEW_RButtonDblClk(const LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
10855 LVHITTESTINFO lvHitTestInfo;
10857 TRACE("(key=%hu,X=%u,Y=%u)\n", wKey, x, y);
10859 /* send NM_RELEASEDCAPTURE notification */
10860 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
10862 /* send NM_RDBLCLK notification */
10863 lvHitTestInfo.pt.x = x;
10864 lvHitTestInfo.pt.y = y;
10865 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
10866 notify_click(infoPtr, NM_RDBLCLK, &lvHitTestInfo);
10868 return 0;
10871 /***
10872 * DESCRIPTION:
10873 * Processes WM_RBUTTONDOWN message and corresponding drag operation.
10875 * PARAMETER(S):
10876 * [I] infoPtr : valid pointer to the listview structure
10877 * [I] wKey : key flag
10878 * [I] x, y : mouse coordinate
10880 * RETURN:
10881 * Zero
10883 static LRESULT LISTVIEW_RButtonDown(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
10885 LVHITTESTINFO ht;
10886 INT item;
10888 TRACE("(key=%hu, x=%d, y=%d)\n", wKey, x, y);
10890 /* send NM_RELEASEDCAPTURE notification */
10891 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
10893 /* determine the index of the selected item */
10894 ht.pt.x = x;
10895 ht.pt.y = y;
10896 item = LISTVIEW_HitTest(infoPtr, &ht, TRUE, TRUE);
10898 /* make sure the listview control window has the focus */
10899 if (!infoPtr->bFocus) SetFocus(infoPtr->hwndSelf);
10901 if ((item >= 0) && (item < infoPtr->nItemCount))
10903 LISTVIEW_SetItemFocus(infoPtr, item);
10904 if (!((wKey & MK_SHIFT) || (wKey & MK_CONTROL)) &&
10905 !LISTVIEW_GetItemState(infoPtr, item, LVIS_SELECTED))
10906 LISTVIEW_SetSelection(infoPtr, item);
10908 else
10909 LISTVIEW_DeselectAll(infoPtr);
10911 if (LISTVIEW_TrackMouse(infoPtr, ht.pt))
10913 if (ht.iItem != -1)
10915 NMLISTVIEW nmlv;
10917 memset(&nmlv, 0, sizeof(nmlv));
10918 nmlv.iItem = ht.iItem;
10919 nmlv.ptAction = ht.pt;
10921 notify_listview(infoPtr, LVN_BEGINRDRAG, &nmlv);
10924 else
10926 SetFocus(infoPtr->hwndSelf);
10928 ht.pt.x = x;
10929 ht.pt.y = y;
10930 LISTVIEW_HitTest(infoPtr, &ht, TRUE, FALSE);
10932 if (notify_click(infoPtr, NM_RCLICK, &ht))
10934 /* Send a WM_CONTEXTMENU message in response to the WM_RBUTTONUP */
10935 SendMessageW(infoPtr->hwndSelf, WM_CONTEXTMENU,
10936 (WPARAM)infoPtr->hwndSelf, (LPARAM)GetMessagePos());
10940 return 0;
10943 /***
10944 * DESCRIPTION:
10945 * Sets the cursor.
10947 * PARAMETER(S):
10948 * [I] infoPtr : valid pointer to the listview structure
10949 * [I] hwnd : window handle of window containing the cursor
10950 * [I] nHittest : hit-test code
10951 * [I] wMouseMsg : ideintifier of the mouse message
10953 * RETURN:
10954 * TRUE if cursor is set
10955 * FALSE otherwise
10957 static BOOL LISTVIEW_SetCursor(const LISTVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
10959 LVHITTESTINFO lvHitTestInfo;
10961 if (!LISTVIEW_IsHotTracking(infoPtr)) goto forward;
10963 if (!infoPtr->hHotCursor) goto forward;
10965 GetCursorPos(&lvHitTestInfo.pt);
10966 if (LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, FALSE, FALSE) < 0) goto forward;
10968 SetCursor(infoPtr->hHotCursor);
10970 return TRUE;
10972 forward:
10974 return DefWindowProcW(infoPtr->hwndSelf, WM_SETCURSOR, wParam, lParam);
10977 /***
10978 * DESCRIPTION:
10979 * Sets the focus.
10981 * PARAMETER(S):
10982 * [I] infoPtr : valid pointer to the listview structure
10983 * [I] hwndLoseFocus : handle of previously focused window
10985 * RETURN:
10986 * Zero
10988 static LRESULT LISTVIEW_SetFocus(LISTVIEW_INFO *infoPtr, HWND hwndLoseFocus)
10990 TRACE("(hwndLoseFocus=%p)\n", hwndLoseFocus);
10992 /* if we have the focus already, there's nothing to do */
10993 if (infoPtr->bFocus) return 0;
10995 /* send NM_SETFOCUS notification */
10996 if (!notify(infoPtr, NM_SETFOCUS)) return 0;
10998 /* set window focus flag */
10999 infoPtr->bFocus = TRUE;
11001 /* put the focus rect back on */
11002 LISTVIEW_ShowFocusRect(infoPtr, TRUE);
11004 /* redraw all visible selected items */
11005 LISTVIEW_InvalidateSelectedItems(infoPtr);
11007 return 0;
11010 /***
11011 * DESCRIPTION:
11012 * Sets the font.
11014 * PARAMETER(S):
11015 * [I] infoPtr : valid pointer to the listview structure
11016 * [I] fRedraw : font handle
11017 * [I] fRedraw : redraw flag
11019 * RETURN:
11020 * Zero
11022 static LRESULT LISTVIEW_SetFont(LISTVIEW_INFO *infoPtr, HFONT hFont, WORD fRedraw)
11024 HFONT oldFont = infoPtr->hFont;
11025 INT oldHeight = infoPtr->nItemHeight;
11027 TRACE("(hfont=%p,redraw=%hu)\n", hFont, fRedraw);
11029 infoPtr->hFont = hFont ? hFont : infoPtr->hDefaultFont;
11030 if (infoPtr->hFont == oldFont) return 0;
11032 LISTVIEW_SaveTextMetrics(infoPtr);
11034 infoPtr->nItemHeight = LISTVIEW_CalculateItemHeight(infoPtr);
11036 if (infoPtr->uView == LV_VIEW_DETAILS)
11038 SendMessageW(infoPtr->hwndHeader, WM_SETFONT, (WPARAM)hFont, MAKELPARAM(fRedraw, 0));
11039 LISTVIEW_UpdateSize(infoPtr);
11040 LISTVIEW_UpdateScroll(infoPtr);
11042 else if (infoPtr->nItemHeight != oldHeight)
11043 LISTVIEW_UpdateScroll(infoPtr);
11045 if (fRedraw) LISTVIEW_InvalidateList(infoPtr);
11047 return 0;
11050 /***
11051 * DESCRIPTION:
11052 * Message handling for WM_SETREDRAW.
11053 * For the Listview, it invalidates the entire window (the doc specifies otherwise)
11055 * PARAMETER(S):
11056 * [I] infoPtr : valid pointer to the listview structure
11057 * [I] redraw: state of redraw flag
11059 * RETURN:
11060 * Zero.
11062 static LRESULT LISTVIEW_SetRedraw(LISTVIEW_INFO *infoPtr, BOOL redraw)
11064 TRACE("old=%d, new=%d\n", infoPtr->redraw, redraw);
11066 if (infoPtr->redraw == !!redraw)
11067 return 0;
11069 if (!(infoPtr->redraw = !!redraw))
11070 return 0;
11072 if (is_autoarrange(infoPtr))
11073 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
11074 LISTVIEW_UpdateScroll(infoPtr);
11076 /* despite what the WM_SETREDRAW docs says, apps expect us
11077 * to invalidate the listview here... stupid! */
11078 LISTVIEW_InvalidateList(infoPtr);
11080 return 0;
11083 /***
11084 * DESCRIPTION:
11085 * Resizes the listview control. This function processes WM_SIZE
11086 * messages. At this time, the width and height are not used.
11088 * PARAMETER(S):
11089 * [I] infoPtr : valid pointer to the listview structure
11090 * [I] Width : new width
11091 * [I] Height : new height
11093 * RETURN:
11094 * Zero
11096 static LRESULT LISTVIEW_Size(LISTVIEW_INFO *infoPtr, int Width, int Height)
11098 RECT rcOld = infoPtr->rcList;
11100 TRACE("(width=%d, height=%d)\n", Width, Height);
11102 LISTVIEW_UpdateSize(infoPtr);
11103 if (EqualRect(&rcOld, &infoPtr->rcList)) return 0;
11105 /* do not bother with display related stuff if we're not redrawing */
11106 if (!is_redrawing(infoPtr)) return 0;
11108 if (is_autoarrange(infoPtr))
11109 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
11111 LISTVIEW_UpdateScroll(infoPtr);
11113 /* refresh all only for lists whose height changed significantly */
11114 if ((infoPtr->uView == LV_VIEW_LIST) &&
11115 (rcOld.bottom - rcOld.top) / infoPtr->nItemHeight !=
11116 (infoPtr->rcList.bottom - infoPtr->rcList.top) / infoPtr->nItemHeight)
11117 LISTVIEW_InvalidateList(infoPtr);
11119 return 0;
11122 /***
11123 * DESCRIPTION:
11124 * Sets the size information.
11126 * PARAMETER(S):
11127 * [I] infoPtr : valid pointer to the listview structure
11129 * RETURN:
11130 * None
11132 static void LISTVIEW_UpdateSize(LISTVIEW_INFO *infoPtr)
11134 TRACE("uView %ld, rcList(old)=%s\n", infoPtr->uView, wine_dbgstr_rect(&infoPtr->rcList));
11136 GetClientRect(infoPtr->hwndSelf, &infoPtr->rcList);
11138 if (infoPtr->uView == LV_VIEW_LIST)
11140 /* Apparently the "LIST" style is supposed to have the same
11141 * number of items in a column even if there is no scroll bar.
11142 * Since if a scroll bar already exists then the bottom is already
11143 * reduced, only reduce if the scroll bar does not currently exist.
11144 * The "2" is there to mimic the native control. I think it may be
11145 * related to either padding or edges. (GLA 7/2002)
11147 if (!(GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE) & WS_HSCROLL))
11148 infoPtr->rcList.bottom -= GetSystemMetrics(SM_CYHSCROLL);
11149 infoPtr->rcList.bottom = max (infoPtr->rcList.bottom - 2, 0);
11152 /* When ListView control is created invisible, header isn't created right away. */
11153 if (infoPtr->hwndHeader)
11155 POINT origin;
11156 WINDOWPOS wp;
11157 HDLAYOUT hl;
11158 RECT rect;
11160 LISTVIEW_GetOrigin(infoPtr, &origin);
11162 rect = infoPtr->rcList;
11163 rect.left += origin.x;
11165 hl.prc = &rect;
11166 hl.pwpos = &wp;
11167 SendMessageW( infoPtr->hwndHeader, HDM_LAYOUT, 0, (LPARAM)&hl );
11168 TRACE(" wp.flags=0x%08x, wp=%d,%d (%dx%d)\n", wp.flags, wp.x, wp.y, wp.cx, wp.cy);
11170 if (LISTVIEW_IsHeaderEnabled(infoPtr))
11171 wp.flags |= SWP_SHOWWINDOW;
11172 else
11174 wp.flags |= SWP_HIDEWINDOW;
11175 wp.cy = 0;
11178 SetWindowPos(wp.hwnd, wp.hwndInsertAfter, wp.x, wp.y, wp.cx, wp.cy, wp.flags);
11179 TRACE(" after SWP wp=%d,%d (%dx%d)\n", wp.x, wp.y, wp.cx, wp.cy);
11181 infoPtr->rcList.top = max(wp.cy, 0);
11183 /* extra padding for grid */
11184 if (infoPtr->uView == LV_VIEW_DETAILS && infoPtr->dwLvExStyle & LVS_EX_GRIDLINES)
11185 infoPtr->rcList.top += 2;
11187 TRACE(" rcList=%s\n", wine_dbgstr_rect(&infoPtr->rcList));
11190 /***
11191 * DESCRIPTION:
11192 * Processes WM_STYLECHANGED messages.
11194 * PARAMETER(S):
11195 * [I] infoPtr : valid pointer to the listview structure
11196 * [I] wStyleType : window style type (normal or extended)
11197 * [I] lpss : window style information
11199 * RETURN:
11200 * Zero
11202 static INT LISTVIEW_StyleChanged(LISTVIEW_INFO *infoPtr, WPARAM wStyleType,
11203 const STYLESTRUCT *lpss)
11205 UINT uNewView, uOldView;
11206 UINT style;
11208 TRACE("styletype %Ix, styleOld %#lx, styleNew %#lx\n",
11209 wStyleType, lpss->styleOld, lpss->styleNew);
11211 if (wStyleType != GWL_STYLE || lpss->styleNew == infoPtr->dwStyle) return 0;
11213 infoPtr->dwStyle = lpss->styleNew;
11215 if (((lpss->styleOld & WS_HSCROLL) != 0)&&
11216 ((lpss->styleNew & WS_HSCROLL) == 0))
11217 ShowScrollBar(infoPtr->hwndSelf, SB_HORZ, FALSE);
11219 if (((lpss->styleOld & WS_VSCROLL) != 0)&&
11220 ((lpss->styleNew & WS_VSCROLL) == 0))
11221 ShowScrollBar(infoPtr->hwndSelf, SB_VERT, FALSE);
11223 uNewView = lpss->styleNew & LVS_TYPEMASK;
11224 uOldView = lpss->styleOld & LVS_TYPEMASK;
11226 if (uNewView != uOldView)
11228 HIMAGELIST himl;
11230 /* LVM_SETVIEW doesn't change window style bits within LVS_TYPEMASK,
11231 changing style updates current view only when view bits change. */
11232 map_style_view(infoPtr);
11233 SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
11234 ShowWindow(infoPtr->hwndHeader, SW_HIDE);
11236 ShowScrollBar(infoPtr->hwndSelf, SB_BOTH, FALSE);
11237 SetRectEmpty(&infoPtr->rcFocus);
11239 himl = (uNewView == LVS_ICON ? infoPtr->himlNormal : infoPtr->himlSmall);
11240 set_icon_size(&infoPtr->iconSize, himl, uNewView != LVS_ICON);
11242 if (uNewView == LVS_REPORT)
11244 HDLAYOUT hl;
11245 WINDOWPOS wp;
11247 LISTVIEW_CreateHeader( infoPtr );
11249 hl.prc = &infoPtr->rcList;
11250 hl.pwpos = &wp;
11251 SendMessageW( infoPtr->hwndHeader, HDM_LAYOUT, 0, (LPARAM)&hl );
11252 SetWindowPos(infoPtr->hwndHeader, infoPtr->hwndSelf, wp.x, wp.y, wp.cx, wp.cy,
11253 wp.flags | ((infoPtr->dwStyle & LVS_NOCOLUMNHEADER)
11254 ? SWP_HIDEWINDOW : SWP_SHOWWINDOW));
11257 LISTVIEW_UpdateItemSize(infoPtr);
11260 if (uNewView == LVS_REPORT || infoPtr->dwLvExStyle & LVS_EX_HEADERINALLVIEWS)
11262 if ((lpss->styleOld ^ lpss->styleNew) & LVS_NOCOLUMNHEADER)
11264 if (lpss->styleNew & LVS_NOCOLUMNHEADER)
11266 /* Turn off the header control */
11267 style = GetWindowLongW(infoPtr->hwndHeader, GWL_STYLE);
11268 TRACE("Hide header control, was 0x%08x\n", style);
11269 SetWindowLongW(infoPtr->hwndHeader, GWL_STYLE, style | HDS_HIDDEN);
11270 } else {
11271 /* Turn on the header control */
11272 if ((style = GetWindowLongW(infoPtr->hwndHeader, GWL_STYLE)) & HDS_HIDDEN)
11274 TRACE("Show header control, was 0x%08x\n", style);
11275 SetWindowLongW(infoPtr->hwndHeader, GWL_STYLE, (style & ~HDS_HIDDEN) | WS_VISIBLE);
11281 if ( (uNewView == LVS_ICON || uNewView == LVS_SMALLICON) &&
11282 (uNewView != uOldView || ((lpss->styleNew ^ lpss->styleOld) & LVS_ALIGNMASK)) )
11283 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
11285 /* update the size of the client area */
11286 LISTVIEW_UpdateSize(infoPtr);
11288 /* add scrollbars if needed */
11289 LISTVIEW_UpdateScroll(infoPtr);
11291 /* invalidate client area + erase background */
11292 LISTVIEW_InvalidateList(infoPtr);
11294 return 0;
11297 /***
11298 * DESCRIPTION:
11299 * Processes WM_STYLECHANGING messages.
11301 * PARAMETER(S):
11302 * [I] wStyleType : window style type (normal or extended)
11303 * [I0] lpss : window style information
11305 * RETURN:
11306 * Zero
11308 static INT LISTVIEW_StyleChanging(WPARAM wStyleType,
11309 STYLESTRUCT *lpss)
11311 TRACE("styletype %Ix, styleOld %#lx, styleNew %#lx\n",
11312 wStyleType, lpss->styleOld, lpss->styleNew);
11314 /* don't forward LVS_OWNERDATA only if not already set to */
11315 if ((lpss->styleNew ^ lpss->styleOld) & LVS_OWNERDATA)
11317 if (lpss->styleOld & LVS_OWNERDATA)
11318 lpss->styleNew |= LVS_OWNERDATA;
11319 else
11320 lpss->styleNew &= ~LVS_OWNERDATA;
11323 return 0;
11326 /***
11327 * DESCRIPTION:
11328 * Processes WM_SHOWWINDOW messages.
11330 * PARAMETER(S):
11331 * [I] infoPtr : valid pointer to the listview structure
11332 * [I] bShown : window is being shown (FALSE when hidden)
11333 * [I] iStatus : window show status
11335 * RETURN:
11336 * Zero
11338 static LRESULT LISTVIEW_ShowWindow(LISTVIEW_INFO *infoPtr, WPARAM bShown, LPARAM iStatus)
11340 /* header delayed creation */
11341 if ((infoPtr->uView == LV_VIEW_DETAILS) && bShown)
11343 LISTVIEW_CreateHeader(infoPtr);
11345 if (!(LVS_NOCOLUMNHEADER & infoPtr->dwStyle))
11346 ShowWindow(infoPtr->hwndHeader, SW_SHOWNORMAL);
11349 return DefWindowProcW(infoPtr->hwndSelf, WM_SHOWWINDOW, bShown, iStatus);
11352 /***
11353 * DESCRIPTION:
11354 * Processes CCM_GETVERSION messages.
11356 * PARAMETER(S):
11357 * [I] infoPtr : valid pointer to the listview structure
11359 * RETURN:
11360 * Current version
11362 static inline LRESULT LISTVIEW_GetVersion(const LISTVIEW_INFO *infoPtr)
11364 return infoPtr->iVersion;
11367 /***
11368 * DESCRIPTION:
11369 * Processes CCM_SETVERSION messages.
11371 * PARAMETER(S):
11372 * [I] infoPtr : valid pointer to the listview structure
11373 * [I] iVersion : version to be set
11375 * RETURN:
11376 * -1 when requested version is greater than DLL version;
11377 * previous version otherwise
11379 static LRESULT LISTVIEW_SetVersion(LISTVIEW_INFO *infoPtr, DWORD iVersion)
11381 INT iOldVersion = infoPtr->iVersion;
11383 if (iVersion > COMCTL32_VERSION)
11384 return -1;
11386 infoPtr->iVersion = iVersion;
11388 TRACE("new version %ld\n", iVersion);
11390 return iOldVersion;
11393 /***
11394 * DESCRIPTION:
11395 * Window procedure of the listview control.
11398 static LRESULT WINAPI
11399 LISTVIEW_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
11401 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongPtrW(hwnd, 0);
11403 TRACE("hwnd %p, uMsg %x, wParam %Ix, lParam %Ix\n", hwnd, uMsg, wParam, lParam);
11405 if (!infoPtr && (uMsg != WM_NCCREATE))
11406 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
11408 switch (uMsg)
11410 case LVM_APPROXIMATEVIEWRECT:
11411 return LISTVIEW_ApproximateViewRect(infoPtr, (INT)wParam,
11412 LOWORD(lParam), HIWORD(lParam));
11413 case LVM_ARRANGE:
11414 return LISTVIEW_Arrange(infoPtr, (INT)wParam);
11416 case LVM_CANCELEDITLABEL:
11417 return LISTVIEW_CancelEditLabel(infoPtr);
11419 case LVM_CREATEDRAGIMAGE:
11420 return (LRESULT)LISTVIEW_CreateDragImage(infoPtr, (INT)wParam, (LPPOINT)lParam);
11422 case LVM_DELETEALLITEMS:
11423 return LISTVIEW_DeleteAllItems(infoPtr, FALSE);
11425 case LVM_DELETECOLUMN:
11426 return LISTVIEW_DeleteColumn(infoPtr, (INT)wParam);
11428 case LVM_DELETEITEM:
11429 return LISTVIEW_DeleteItem(infoPtr, (INT)wParam);
11431 case LVM_EDITLABELA:
11432 case LVM_EDITLABELW:
11433 return (LRESULT)LISTVIEW_EditLabelT(infoPtr, (INT)wParam,
11434 uMsg == LVM_EDITLABELW);
11435 /* case LVM_ENABLEGROUPVIEW: */
11437 case LVM_ENSUREVISIBLE:
11438 return LISTVIEW_EnsureVisible(infoPtr, (INT)wParam, (BOOL)lParam);
11440 case LVM_FINDITEMW:
11441 return LISTVIEW_FindItemW(infoPtr, (INT)wParam, (LPLVFINDINFOW)lParam);
11443 case LVM_FINDITEMA:
11444 return LISTVIEW_FindItemA(infoPtr, (INT)wParam, (LPLVFINDINFOA)lParam);
11446 case LVM_GETBKCOLOR:
11447 return infoPtr->clrBk;
11449 /* case LVM_GETBKIMAGE: */
11451 case LVM_GETCALLBACKMASK:
11452 return infoPtr->uCallbackMask;
11454 case LVM_GETCOLUMNA:
11455 case LVM_GETCOLUMNW:
11456 return LISTVIEW_GetColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam,
11457 uMsg == LVM_GETCOLUMNW);
11459 case LVM_GETCOLUMNORDERARRAY:
11460 return LISTVIEW_GetColumnOrderArray(infoPtr, (INT)wParam, (LPINT)lParam);
11462 case LVM_GETCOLUMNWIDTH:
11463 return LISTVIEW_GetColumnWidth(infoPtr, (INT)wParam);
11465 case LVM_GETCOUNTPERPAGE:
11466 return LISTVIEW_GetCountPerPage(infoPtr);
11468 case LVM_GETEDITCONTROL:
11469 return (LRESULT)infoPtr->hwndEdit;
11471 case LVM_GETEXTENDEDLISTVIEWSTYLE:
11472 return infoPtr->dwLvExStyle;
11474 /* case LVM_GETGROUPINFO: */
11476 /* case LVM_GETGROUPMETRICS: */
11478 case LVM_GETHEADER:
11479 return (LRESULT)infoPtr->hwndHeader;
11481 case LVM_GETHOTCURSOR:
11482 return (LRESULT)infoPtr->hHotCursor;
11484 case LVM_GETHOTITEM:
11485 return infoPtr->nHotItem;
11487 case LVM_GETHOVERTIME:
11488 return infoPtr->dwHoverTime;
11490 case LVM_GETIMAGELIST:
11491 return (LRESULT)LISTVIEW_GetImageList(infoPtr, (INT)wParam);
11493 /* case LVM_GETINSERTMARK: */
11495 /* case LVM_GETINSERTMARKCOLOR: */
11497 /* case LVM_GETINSERTMARKRECT: */
11499 case LVM_GETISEARCHSTRINGA:
11500 case LVM_GETISEARCHSTRINGW:
11501 FIXME("LVM_GETISEARCHSTRING: unimplemented\n");
11502 return FALSE;
11504 case LVM_GETITEMA:
11505 case LVM_GETITEMW:
11506 return LISTVIEW_GetItemExtT(infoPtr, (LPLVITEMW)lParam, uMsg == LVM_GETITEMW);
11508 case LVM_GETITEMCOUNT:
11509 return infoPtr->nItemCount;
11511 case LVM_GETITEMPOSITION:
11512 return LISTVIEW_GetItemPosition(infoPtr, (INT)wParam, (LPPOINT)lParam);
11514 case LVM_GETITEMRECT:
11515 return LISTVIEW_GetItemRect(infoPtr, (INT)wParam, (LPRECT)lParam);
11517 case LVM_GETITEMSPACING:
11518 return LISTVIEW_GetItemSpacing(infoPtr, (BOOL)wParam);
11520 case LVM_GETITEMSTATE:
11521 return LISTVIEW_GetItemState(infoPtr, (INT)wParam, (UINT)lParam);
11523 case LVM_GETITEMTEXTA:
11524 case LVM_GETITEMTEXTW:
11525 return LISTVIEW_GetItemTextT(infoPtr, (INT)wParam, (LPLVITEMW)lParam,
11526 uMsg == LVM_GETITEMTEXTW);
11528 case LVM_GETNEXTITEM:
11529 return LISTVIEW_GetNextItem(infoPtr, (INT)wParam, LOWORD(lParam));
11531 case LVM_GETNEXTITEMINDEX:
11532 return LISTVIEW_GetNextItemIndex(infoPtr, (LVITEMINDEX *)wParam, lParam);
11534 case LVM_GETNUMBEROFWORKAREAS:
11535 FIXME("LVM_GETNUMBEROFWORKAREAS: unimplemented\n");
11536 return 1;
11538 case LVM_GETORIGIN:
11539 if (!lParam) return FALSE;
11540 if (infoPtr->uView == LV_VIEW_DETAILS ||
11541 infoPtr->uView == LV_VIEW_LIST) return FALSE;
11542 LISTVIEW_GetOrigin(infoPtr, (LPPOINT)lParam);
11543 return TRUE;
11545 /* case LVM_GETOUTLINECOLOR: */
11547 case LVM_GETSELECTEDCOLUMN:
11548 return infoPtr->selected_column;
11550 case LVM_GETSELECTEDCOUNT:
11551 return LISTVIEW_GetSelectedCount(infoPtr);
11553 case LVM_GETSELECTIONMARK:
11554 return infoPtr->nSelectionMark;
11556 case LVM_GETSTRINGWIDTHA:
11557 case LVM_GETSTRINGWIDTHW:
11558 return LISTVIEW_GetStringWidthT(infoPtr, (LPCWSTR)lParam,
11559 uMsg == LVM_GETSTRINGWIDTHW);
11561 case LVM_GETSUBITEMRECT:
11562 return LISTVIEW_GetSubItemRect(infoPtr, (UINT)wParam, (LPRECT)lParam);
11564 case LVM_GETTEXTBKCOLOR:
11565 return infoPtr->clrTextBk;
11567 case LVM_GETTEXTCOLOR:
11568 return infoPtr->clrText;
11570 /* case LVM_GETTILEINFO: */
11572 /* case LVM_GETTILEVIEWINFO: */
11574 case LVM_GETTOOLTIPS:
11575 if( !infoPtr->hwndToolTip )
11576 infoPtr->hwndToolTip = COMCTL32_CreateToolTip( hwnd );
11577 return (LRESULT)infoPtr->hwndToolTip;
11579 case LVM_GETTOPINDEX:
11580 return LISTVIEW_GetTopIndex(infoPtr);
11582 case LVM_GETUNICODEFORMAT:
11583 return (infoPtr->notifyFormat == NFR_UNICODE);
11585 case LVM_GETVIEW:
11586 return infoPtr->uView;
11588 case LVM_GETVIEWRECT:
11589 return LISTVIEW_GetViewRect(infoPtr, (LPRECT)lParam);
11591 case LVM_GETWORKAREAS:
11592 FIXME("LVM_GETWORKAREAS: unimplemented\n");
11593 return FALSE;
11595 /* case LVM_HASGROUP: */
11597 case LVM_HITTEST:
11598 return LISTVIEW_HitTest(infoPtr, (LPLVHITTESTINFO)lParam, FALSE, TRUE);
11600 case LVM_INSERTCOLUMNA:
11601 case LVM_INSERTCOLUMNW:
11602 return LISTVIEW_InsertColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam,
11603 uMsg == LVM_INSERTCOLUMNW);
11605 /* case LVM_INSERTGROUP: */
11607 /* case LVM_INSERTGROUPSORTED: */
11609 case LVM_INSERTITEMA:
11610 case LVM_INSERTITEMW:
11611 return LISTVIEW_InsertItemT(infoPtr, (LPLVITEMW)lParam, uMsg == LVM_INSERTITEMW);
11613 /* case LVM_INSERTMARKHITTEST: */
11615 /* case LVM_ISGROUPVIEWENABLED: */
11617 case LVM_ISITEMVISIBLE:
11618 return LISTVIEW_IsItemVisible(infoPtr, (INT)wParam);
11620 case LVM_MAPIDTOINDEX:
11621 return LISTVIEW_MapIdToIndex(infoPtr, (UINT)wParam);
11623 case LVM_MAPINDEXTOID:
11624 return LISTVIEW_MapIndexToId(infoPtr, (INT)wParam);
11626 /* case LVM_MOVEGROUP: */
11628 /* case LVM_MOVEITEMTOGROUP: */
11630 case LVM_REDRAWITEMS:
11631 return LISTVIEW_RedrawItems(infoPtr, (INT)wParam, (INT)lParam);
11633 /* case LVM_REMOVEALLGROUPS: */
11635 /* case LVM_REMOVEGROUP: */
11637 case LVM_SCROLL:
11638 return LISTVIEW_Scroll(infoPtr, (INT)wParam, (INT)lParam);
11640 case LVM_SETBKCOLOR:
11641 return LISTVIEW_SetBkColor(infoPtr, (COLORREF)lParam);
11643 case LVM_SETBKIMAGEA:
11644 case LVM_SETBKIMAGEW:
11645 return LISTVIEW_SetBkImage(infoPtr, (LVBKIMAGEW *)lParam, uMsg == LVM_SETBKIMAGEW);
11647 case LVM_SETCALLBACKMASK:
11648 infoPtr->uCallbackMask = (UINT)wParam;
11649 return TRUE;
11651 case LVM_SETCOLUMNA:
11652 case LVM_SETCOLUMNW:
11653 return LISTVIEW_SetColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam,
11654 uMsg == LVM_SETCOLUMNW);
11656 case LVM_SETCOLUMNORDERARRAY:
11657 return LISTVIEW_SetColumnOrderArray(infoPtr, (INT)wParam, (LPINT)lParam);
11659 case LVM_SETCOLUMNWIDTH:
11660 return LISTVIEW_SetColumnWidth(infoPtr, (INT)wParam, (short)LOWORD(lParam));
11662 case LVM_SETEXTENDEDLISTVIEWSTYLE:
11663 return LISTVIEW_SetExtendedListViewStyle(infoPtr, (DWORD)wParam, (DWORD)lParam);
11665 /* case LVM_SETGROUPINFO: */
11667 /* case LVM_SETGROUPMETRICS: */
11669 case LVM_SETHOTCURSOR:
11670 return (LRESULT)LISTVIEW_SetHotCursor(infoPtr, (HCURSOR)lParam);
11672 case LVM_SETHOTITEM:
11673 return LISTVIEW_SetHotItem(infoPtr, (INT)wParam);
11675 case LVM_SETHOVERTIME:
11676 return LISTVIEW_SetHoverTime(infoPtr, (DWORD)lParam);
11678 case LVM_SETICONSPACING:
11679 if(lParam == -1)
11680 return LISTVIEW_SetIconSpacing(infoPtr, -1, -1);
11681 return LISTVIEW_SetIconSpacing(infoPtr, LOWORD(lParam), HIWORD(lParam));
11683 case LVM_SETIMAGELIST:
11684 return (LRESULT)LISTVIEW_SetImageList(infoPtr, (INT)wParam, (HIMAGELIST)lParam);
11686 /* case LVM_SETINFOTIP: */
11688 /* case LVM_SETINSERTMARK: */
11690 /* case LVM_SETINSERTMARKCOLOR: */
11692 case LVM_SETITEMA:
11693 case LVM_SETITEMW:
11695 if (infoPtr->dwStyle & LVS_OWNERDATA) return FALSE;
11696 return LISTVIEW_SetItemT(infoPtr, (LPLVITEMW)lParam, (uMsg == LVM_SETITEMW));
11699 case LVM_SETITEMCOUNT:
11700 return LISTVIEW_SetItemCount(infoPtr, (INT)wParam, (DWORD)lParam);
11702 case LVM_SETITEMPOSITION:
11704 POINT pt;
11705 pt.x = (short)LOWORD(lParam);
11706 pt.y = (short)HIWORD(lParam);
11707 return LISTVIEW_SetItemPosition(infoPtr, (INT)wParam, &pt);
11710 case LVM_SETITEMPOSITION32:
11711 return LISTVIEW_SetItemPosition(infoPtr, (INT)wParam, (POINT*)lParam);
11713 case LVM_SETITEMSTATE:
11714 return LISTVIEW_SetItemState(infoPtr, (INT)wParam, (LPLVITEMW)lParam);
11716 case LVM_SETITEMTEXTA:
11717 case LVM_SETITEMTEXTW:
11718 return LISTVIEW_SetItemTextT(infoPtr, (INT)wParam, (LPLVITEMW)lParam,
11719 uMsg == LVM_SETITEMTEXTW);
11721 /* case LVM_SETOUTLINECOLOR: */
11723 case LVM_SETSELECTEDCOLUMN:
11724 infoPtr->selected_column = (INT)wParam;
11725 return TRUE;
11727 case LVM_SETSELECTIONMARK:
11728 return LISTVIEW_SetSelectionMark(infoPtr, (INT)lParam);
11730 case LVM_SETTEXTBKCOLOR:
11731 return LISTVIEW_SetTextBkColor(infoPtr, (COLORREF)lParam);
11733 case LVM_SETTEXTCOLOR:
11734 return LISTVIEW_SetTextColor(infoPtr, (COLORREF)lParam);
11736 /* case LVM_SETTILEINFO: */
11738 /* case LVM_SETTILEVIEWINFO: */
11740 /* case LVM_SETTILEWIDTH: */
11742 case LVM_SETTOOLTIPS:
11743 return (LRESULT)LISTVIEW_SetToolTips(infoPtr, (HWND)lParam);
11745 case LVM_SETUNICODEFORMAT:
11746 return LISTVIEW_SetUnicodeFormat(infoPtr, wParam);
11748 case LVM_SETVIEW:
11749 return LISTVIEW_SetView(infoPtr, wParam);
11751 /* case LVM_SETWORKAREAS: */
11753 /* case LVM_SORTGROUPS: */
11755 case LVM_SORTITEMS:
11756 case LVM_SORTITEMSEX:
11757 return LISTVIEW_SortItems(infoPtr, (PFNLVCOMPARE)lParam, wParam,
11758 uMsg == LVM_SORTITEMSEX);
11759 case LVM_SUBITEMHITTEST:
11760 return LISTVIEW_HitTest(infoPtr, (LPLVHITTESTINFO)lParam, TRUE, FALSE);
11762 case LVM_UPDATE:
11763 return LISTVIEW_Update(infoPtr, (INT)wParam);
11765 case CCM_GETVERSION:
11766 return LISTVIEW_GetVersion(infoPtr);
11768 case CCM_SETVERSION:
11769 return LISTVIEW_SetVersion(infoPtr, wParam);
11771 case WM_CHAR:
11772 return LISTVIEW_ProcessLetterKeys( infoPtr, wParam, lParam );
11774 case WM_COMMAND:
11775 return LISTVIEW_Command(infoPtr, wParam, lParam);
11777 case WM_NCCREATE:
11778 return LISTVIEW_NCCreate(hwnd, wParam, (LPCREATESTRUCTW)lParam);
11780 case WM_CREATE:
11781 return LISTVIEW_Create(hwnd, (LPCREATESTRUCTW)lParam);
11783 case WM_DESTROY:
11784 return LISTVIEW_Destroy(infoPtr);
11786 case WM_ENABLE:
11787 return LISTVIEW_Enable(infoPtr);
11789 case WM_ERASEBKGND:
11790 return LISTVIEW_EraseBkgnd(infoPtr, (HDC)wParam);
11792 case WM_GETDLGCODE:
11793 return DLGC_WANTCHARS | DLGC_WANTARROWS;
11795 case WM_GETFONT:
11796 return (LRESULT)infoPtr->hFont;
11798 case WM_HSCROLL:
11799 return LISTVIEW_HScroll(infoPtr, (INT)LOWORD(wParam), 0);
11801 case WM_KEYDOWN:
11802 return LISTVIEW_KeyDown(infoPtr, (INT)wParam, (LONG)lParam);
11804 case WM_KILLFOCUS:
11805 return LISTVIEW_KillFocus(infoPtr);
11807 case WM_LBUTTONDBLCLK:
11808 return LISTVIEW_LButtonDblClk(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11810 case WM_LBUTTONDOWN:
11811 return LISTVIEW_LButtonDown(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11813 case WM_LBUTTONUP:
11814 return LISTVIEW_LButtonUp(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11816 case WM_MOUSEMOVE:
11817 return LISTVIEW_MouseMove (infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11819 case WM_MOUSEHOVER:
11820 return LISTVIEW_MouseHover(infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11822 case WM_NCDESTROY:
11823 return LISTVIEW_NCDestroy(infoPtr);
11825 case WM_NCPAINT:
11826 return LISTVIEW_NCPaint(infoPtr, (HRGN)wParam);
11828 case WM_NOTIFY:
11829 return LISTVIEW_Notify(infoPtr, (LPNMHDR)lParam);
11831 case WM_NOTIFYFORMAT:
11832 return LISTVIEW_NotifyFormat(infoPtr, (HWND)wParam, (INT)lParam);
11834 case WM_PRINTCLIENT:
11835 return LISTVIEW_PrintClient(infoPtr, (HDC)wParam, (DWORD)lParam);
11837 case WM_PAINT:
11838 return LISTVIEW_WMPaint(infoPtr, (HDC)wParam);
11840 case WM_RBUTTONDBLCLK:
11841 return LISTVIEW_RButtonDblClk(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11843 case WM_RBUTTONDOWN:
11844 return LISTVIEW_RButtonDown(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11846 case WM_SETCURSOR:
11847 return LISTVIEW_SetCursor(infoPtr, wParam, lParam);
11849 case WM_SETFOCUS:
11850 return LISTVIEW_SetFocus(infoPtr, (HWND)wParam);
11852 case WM_SETFONT:
11853 return LISTVIEW_SetFont(infoPtr, (HFONT)wParam, (WORD)lParam);
11855 case WM_SETREDRAW:
11856 return LISTVIEW_SetRedraw(infoPtr, (BOOL)wParam);
11858 case WM_SHOWWINDOW:
11859 return LISTVIEW_ShowWindow(infoPtr, wParam, lParam);
11861 case WM_STYLECHANGED:
11862 return LISTVIEW_StyleChanged(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
11864 case WM_STYLECHANGING:
11865 return LISTVIEW_StyleChanging(wParam, (LPSTYLESTRUCT)lParam);
11867 case WM_SYSCOLORCHANGE:
11868 COMCTL32_RefreshSysColors();
11869 return 0;
11871 /* case WM_TIMER: */
11872 case WM_THEMECHANGED:
11873 return LISTVIEW_ThemeChanged(infoPtr);
11875 case WM_VSCROLL:
11876 return LISTVIEW_VScroll(infoPtr, (INT)LOWORD(wParam), 0);
11878 case WM_MOUSEWHEEL:
11879 if (wParam & (MK_SHIFT | MK_CONTROL))
11880 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
11881 return LISTVIEW_MouseWheel(infoPtr, (short int)HIWORD(wParam));
11883 case WM_WINDOWPOSCHANGED:
11884 if (!(((WINDOWPOS *)lParam)->flags & SWP_NOSIZE))
11886 SetWindowPos(infoPtr->hwndSelf, 0, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOACTIVATE |
11887 SWP_NOZORDER | SWP_NOMOVE | SWP_NOSIZE);
11889 if ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (infoPtr->uView == LV_VIEW_DETAILS))
11891 if (notify_measureitem(infoPtr)) LISTVIEW_InvalidateList(infoPtr);
11893 LISTVIEW_Size(infoPtr, ((WINDOWPOS *)lParam)->cx, ((WINDOWPOS *)lParam)->cy);
11895 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
11897 /* case WM_WININICHANGE: */
11899 default:
11900 if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
11901 ERR("unknown msg %04x, wp %Ix, lp %Ix\n", uMsg, wParam, lParam);
11903 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
11908 /***
11909 * DESCRIPTION:
11910 * Registers the window class.
11912 * PARAMETER(S):
11913 * None
11915 * RETURN:
11916 * None
11918 void LISTVIEW_Register(void)
11920 WNDCLASSW wndClass;
11922 ZeroMemory(&wndClass, sizeof(WNDCLASSW));
11923 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS;
11924 wndClass.lpfnWndProc = LISTVIEW_WindowProc;
11925 wndClass.cbClsExtra = 0;
11926 wndClass.cbWndExtra = sizeof(LISTVIEW_INFO *);
11927 wndClass.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
11928 wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
11929 wndClass.lpszClassName = WC_LISTVIEWW;
11930 RegisterClassW(&wndClass);
11933 /***
11934 * DESCRIPTION:
11935 * Unregisters the window class.
11937 * PARAMETER(S):
11938 * None
11940 * RETURN:
11941 * None
11943 void LISTVIEW_Unregister(void)
11945 UnregisterClassW(WC_LISTVIEWW, NULL);
11948 /***
11949 * DESCRIPTION:
11950 * Handle any WM_COMMAND messages
11952 * PARAMETER(S):
11953 * [I] infoPtr : valid pointer to the listview structure
11954 * [I] wParam : the first message parameter
11955 * [I] lParam : the second message parameter
11957 * RETURN:
11958 * Zero.
11960 static LRESULT LISTVIEW_Command(LISTVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
11963 TRACE("%p, %x, %x, %Ix\n", infoPtr, HIWORD(wParam), LOWORD(wParam), lParam);
11965 if (!infoPtr->hwndEdit) return 0;
11967 switch (HIWORD(wParam))
11969 case EN_UPDATE:
11972 * Adjust the edit window size
11974 WCHAR buffer[1024];
11975 HDC hdc = GetDC(infoPtr->hwndEdit);
11976 HFONT hFont, hOldFont = 0;
11977 RECT rect;
11978 SIZE sz;
11980 if (!infoPtr->hwndEdit || !hdc) return 0;
11981 GetWindowTextW(infoPtr->hwndEdit, buffer, ARRAY_SIZE(buffer));
11982 GetWindowRect(infoPtr->hwndEdit, &rect);
11984 /* Select font to get the right dimension of the string */
11985 hFont = (HFONT)SendMessageW(infoPtr->hwndEdit, WM_GETFONT, 0, 0);
11986 if (hFont)
11988 hOldFont = SelectObject(hdc, hFont);
11991 if (GetTextExtentPoint32W(hdc, buffer, lstrlenW(buffer), &sz))
11993 TEXTMETRICW textMetric;
11995 /* Add Extra spacing for the next character */
11996 GetTextMetricsW(hdc, &textMetric);
11997 sz.cx += (textMetric.tmMaxCharWidth * 2);
11999 SetWindowPos(infoPtr->hwndEdit, NULL, 0, 0, sz.cx,
12000 rect.bottom - rect.top, SWP_DRAWFRAME | SWP_NOMOVE | SWP_NOZORDER);
12002 if (hFont)
12003 SelectObject(hdc, hOldFont);
12005 ReleaseDC(infoPtr->hwndEdit, hdc);
12007 break;
12009 case EN_KILLFOCUS:
12011 if (infoPtr->notify_mask & NOTIFY_MASK_END_LABEL_EDIT)
12012 LISTVIEW_CancelEditLabel(infoPtr);
12013 break;
12016 default:
12017 return SendMessageW (infoPtr->hwndNotify, WM_COMMAND, wParam, lParam);
12020 return 0;