mfplat/tests: Mark some tests as broken on Win 8 and 10 v1507.
[wine.git] / dlls / comctl32 / listview.c
blobab328b3e7982b28f9254bd6b49195b837a982363
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 LRESULT LISTVIEW_VScroll(LISTVIEW_INFO *, INT, INT);
442 static LRESULT LISTVIEW_HScroll(LISTVIEW_INFO *, INT, INT);
443 static BOOL LISTVIEW_EnsureVisible(LISTVIEW_INFO *, INT, BOOL);
444 static HIMAGELIST LISTVIEW_SetImageList(LISTVIEW_INFO *, INT, HIMAGELIST);
445 static INT LISTVIEW_HitTest(const LISTVIEW_INFO *, LPLVHITTESTINFO, BOOL, BOOL);
446 static BOOL LISTVIEW_EndEditLabelT(LISTVIEW_INFO *, BOOL, BOOL);
447 static BOOL LISTVIEW_Scroll(LISTVIEW_INFO *, INT, INT);
449 /******** Text handling functions *************************************/
451 /* A text pointer is either NULL, LPSTR_TEXTCALLBACK, or points to a
452 * text string. The string may be ANSI or Unicode, in which case
453 * the boolean isW tells us the type of the string.
455 * The name of the function tell what type of strings it expects:
456 * W: Unicode, T: ANSI/Unicode - function of isW
459 static inline BOOL is_text(LPCWSTR text)
461 return text != NULL && text != LPSTR_TEXTCALLBACKW;
464 static inline int textlenT(LPCWSTR text, BOOL isW)
466 return !is_text(text) ? 0 :
467 isW ? lstrlenW(text) : lstrlenA((LPCSTR)text);
470 static inline void textcpynT(LPWSTR dest, BOOL isDestW, LPCWSTR src, BOOL isSrcW, INT max)
472 if (isDestW)
473 if (isSrcW) lstrcpynW(dest, src, max);
474 else MultiByteToWideChar(CP_ACP, 0, (LPCSTR)src, -1, dest, max);
475 else
476 if (isSrcW) WideCharToMultiByte(CP_ACP, 0, src, -1, (LPSTR)dest, max, NULL, NULL);
477 else lstrcpynA((LPSTR)dest, (LPCSTR)src, max);
480 static inline LPWSTR textdupTtoW(LPCWSTR text, BOOL isW)
482 LPWSTR wstr = (LPWSTR)text;
484 if (!isW && is_text(text))
486 INT len = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)text, -1, NULL, 0);
487 wstr = Alloc(len * sizeof(WCHAR));
488 if (wstr) MultiByteToWideChar(CP_ACP, 0, (LPCSTR)text, -1, wstr, len);
490 TRACE(" wstr=%s\n", text == LPSTR_TEXTCALLBACKW ? "(callback)" : debugstr_w(wstr));
491 return wstr;
494 static inline void textfreeT(LPWSTR wstr, BOOL isW)
496 if (!isW && is_text(wstr)) Free (wstr);
500 * dest is a pointer to a Unicode string
501 * src is a pointer to a string (Unicode if isW, ANSI if !isW)
503 static BOOL textsetptrT(LPWSTR *dest, LPCWSTR src, BOOL isW)
505 BOOL bResult = TRUE;
507 if (src == LPSTR_TEXTCALLBACKW)
509 if (is_text(*dest)) Free(*dest);
510 *dest = LPSTR_TEXTCALLBACKW;
512 else
514 LPWSTR pszText = textdupTtoW(src, isW);
515 if (*dest == LPSTR_TEXTCALLBACKW) *dest = NULL;
516 bResult = Str_SetPtrW(dest, pszText);
517 textfreeT(pszText, isW);
519 return bResult;
523 * compares a Unicode to a Unicode/ANSI text string
525 static inline int textcmpWT(LPCWSTR aw, LPCWSTR bt, BOOL isW)
527 if (!aw) return bt ? -1 : 0;
528 if (!bt) return 1;
529 if (aw == LPSTR_TEXTCALLBACKW)
530 return bt == LPSTR_TEXTCALLBACKW ? 1 : -1;
531 if (bt != LPSTR_TEXTCALLBACKW)
533 LPWSTR bw = textdupTtoW(bt, isW);
534 int r = bw ? lstrcmpW(aw, bw) : 1;
535 textfreeT(bw, isW);
536 return r;
539 return 1;
542 /******** Debugging functions *****************************************/
544 static inline LPCSTR debugtext_t(LPCWSTR text, BOOL isW)
546 if (text == LPSTR_TEXTCALLBACKW) return "(callback)";
547 return isW ? debugstr_w(text) : debugstr_a((LPCSTR)text);
550 static inline LPCSTR debugtext_tn(LPCWSTR text, BOOL isW, INT n)
552 if (text == LPSTR_TEXTCALLBACKW) return "(callback)";
553 n = min(textlenT(text, isW), n);
554 return isW ? debugstr_wn(text, n) : debugstr_an((LPCSTR)text, n);
557 static char* debug_getbuf(void)
559 static int index = 0;
560 static char buffers[DEBUG_BUFFERS][DEBUG_BUFFER_SIZE];
561 return buffers[index++ % DEBUG_BUFFERS];
564 static inline const char* debugrange(const RANGE *lprng)
566 if (!lprng) return "(null)";
567 return wine_dbg_sprintf("[%d, %d]", lprng->lower, lprng->upper);
570 static const char* debugscrollinfo(const SCROLLINFO *pScrollInfo)
572 char* buf = debug_getbuf(), *text = buf;
573 int len, size = DEBUG_BUFFER_SIZE;
575 if (pScrollInfo == NULL) return "(null)";
576 len = snprintf(buf, size, "{cbSize=%u, ", pScrollInfo->cbSize);
577 if (len == -1) goto end;
578 buf += len; size -= len;
579 if (pScrollInfo->fMask & SIF_RANGE)
580 len = snprintf(buf, size, "nMin=%d, nMax=%d, ", pScrollInfo->nMin, pScrollInfo->nMax);
581 else len = 0;
582 if (len == -1) goto end;
583 buf += len; size -= len;
584 if (pScrollInfo->fMask & SIF_PAGE)
585 len = snprintf(buf, size, "nPage=%u, ", pScrollInfo->nPage);
586 else len = 0;
587 if (len == -1) goto end;
588 buf += len; size -= len;
589 if (pScrollInfo->fMask & SIF_POS)
590 len = snprintf(buf, size, "nPos=%d, ", pScrollInfo->nPos);
591 else len = 0;
592 if (len == -1) goto end;
593 buf += len; size -= len;
594 if (pScrollInfo->fMask & SIF_TRACKPOS)
595 len = snprintf(buf, size, "nTrackPos=%d, ", pScrollInfo->nTrackPos);
596 else len = 0;
597 if (len == -1) goto end;
598 buf += len;
599 goto undo;
600 end:
601 buf = text + strlen(text);
602 undo:
603 if (buf - text > 2) { buf[-2] = '}'; buf[-1] = 0; }
604 return text;
607 static const char* debugnmlistview(const NMLISTVIEW *plvnm)
609 if (!plvnm) return "(null)";
610 return wine_dbg_sprintf("iItem=%d, iSubItem=%d, uNewState=0x%x,"
611 " uOldState=0x%x, uChanged=0x%x, ptAction=%s, lParam=%Id",
612 plvnm->iItem, plvnm->iSubItem, plvnm->uNewState, plvnm->uOldState,
613 plvnm->uChanged, wine_dbgstr_point(&plvnm->ptAction), plvnm->lParam);
616 static const char* debuglvitem_t(const LVITEMW *lpLVItem, BOOL isW)
618 char* buf = debug_getbuf(), *text = buf;
619 int len, size = DEBUG_BUFFER_SIZE;
621 if (lpLVItem == NULL) return "(null)";
622 len = snprintf(buf, size, "{iItem=%d, iSubItem=%d, ", lpLVItem->iItem, lpLVItem->iSubItem);
623 if (len == -1) goto end;
624 buf += len; size -= len;
625 if (lpLVItem->mask & LVIF_STATE)
626 len = snprintf(buf, size, "state=%x, stateMask=%x, ", lpLVItem->state, lpLVItem->stateMask);
627 else len = 0;
628 if (len == -1) goto end;
629 buf += len; size -= len;
630 if (lpLVItem->mask & LVIF_TEXT)
631 len = snprintf(buf, size, "pszText=%s, cchTextMax=%d, ", debugtext_tn(lpLVItem->pszText, isW, 80), lpLVItem->cchTextMax);
632 else len = 0;
633 if (len == -1) goto end;
634 buf += len; size -= len;
635 if (lpLVItem->mask & LVIF_IMAGE)
636 len = snprintf(buf, size, "iImage=%d, ", lpLVItem->iImage);
637 else len = 0;
638 if (len == -1) goto end;
639 buf += len; size -= len;
640 if (lpLVItem->mask & LVIF_PARAM)
641 len = snprintf(buf, size, "lParam=%Ix, ", lpLVItem->lParam);
642 else len = 0;
643 if (len == -1) goto end;
644 buf += len; size -= len;
645 if (lpLVItem->mask & LVIF_INDENT)
646 len = snprintf(buf, size, "iIndent=%d, ", lpLVItem->iIndent);
647 else len = 0;
648 if (len == -1) goto end;
649 buf += len;
650 goto undo;
651 end:
652 buf = text + strlen(text);
653 undo:
654 if (buf - text > 2) { buf[-2] = '}'; buf[-1] = 0; }
655 return text;
658 static const char* debuglvcolumn_t(const LVCOLUMNW *lpColumn, BOOL isW)
660 char* buf = debug_getbuf(), *text = buf;
661 int len, size = DEBUG_BUFFER_SIZE;
663 if (lpColumn == NULL) return "(null)";
664 len = snprintf(buf, size, "{");
665 if (len == -1) goto end;
666 buf += len; size -= len;
667 if (lpColumn->mask & LVCF_SUBITEM)
668 len = snprintf(buf, size, "iSubItem=%d, ", lpColumn->iSubItem);
669 else len = 0;
670 if (len == -1) goto end;
671 buf += len; size -= len;
672 if (lpColumn->mask & LVCF_FMT)
673 len = snprintf(buf, size, "fmt=%x, ", lpColumn->fmt);
674 else len = 0;
675 if (len == -1) goto end;
676 buf += len; size -= len;
677 if (lpColumn->mask & LVCF_WIDTH)
678 len = snprintf(buf, size, "cx=%d, ", lpColumn->cx);
679 else len = 0;
680 if (len == -1) goto end;
681 buf += len; size -= len;
682 if (lpColumn->mask & LVCF_TEXT)
683 len = snprintf(buf, size, "pszText=%s, cchTextMax=%d, ", debugtext_tn(lpColumn->pszText, isW, 80), lpColumn->cchTextMax);
684 else len = 0;
685 if (len == -1) goto end;
686 buf += len; size -= len;
687 if (lpColumn->mask & LVCF_IMAGE)
688 len = snprintf(buf, size, "iImage=%d, ", lpColumn->iImage);
689 else len = 0;
690 if (len == -1) goto end;
691 buf += len; size -= len;
692 if (lpColumn->mask & LVCF_ORDER)
693 len = snprintf(buf, size, "iOrder=%d, ", lpColumn->iOrder);
694 else len = 0;
695 if (len == -1) goto end;
696 buf += len;
697 goto undo;
698 end:
699 buf = text + strlen(text);
700 undo:
701 if (buf - text > 2) { buf[-2] = '}'; buf[-1] = 0; }
702 return text;
705 static const char* debuglvhittestinfo(const LVHITTESTINFO *lpht)
707 if (!lpht) return "(null)";
709 return wine_dbg_sprintf("{pt=%s, flags=0x%x, iItem=%d, iSubItem=%d}",
710 wine_dbgstr_point(&lpht->pt), lpht->flags, lpht->iItem, lpht->iSubItem);
713 /* Return the corresponding text for a given scroll value */
714 static inline LPCSTR debugscrollcode(int nScrollCode)
716 switch(nScrollCode)
718 case SB_LINELEFT: return "SB_LINELEFT";
719 case SB_LINERIGHT: return "SB_LINERIGHT";
720 case SB_PAGELEFT: return "SB_PAGELEFT";
721 case SB_PAGERIGHT: return "SB_PAGERIGHT";
722 case SB_THUMBPOSITION: return "SB_THUMBPOSITION";
723 case SB_THUMBTRACK: return "SB_THUMBTRACK";
724 case SB_ENDSCROLL: return "SB_ENDSCROLL";
725 case SB_INTERNAL: return "SB_INTERNAL";
726 default: return "unknown";
731 /******** Notification functions ************************************/
733 static int get_ansi_notification(UINT unicodeNotificationCode)
735 switch (unicodeNotificationCode)
737 case LVN_BEGINLABELEDITA:
738 case LVN_BEGINLABELEDITW: return LVN_BEGINLABELEDITA;
739 case LVN_ENDLABELEDITA:
740 case LVN_ENDLABELEDITW: return LVN_ENDLABELEDITA;
741 case LVN_GETDISPINFOA:
742 case LVN_GETDISPINFOW: return LVN_GETDISPINFOA;
743 case LVN_SETDISPINFOA:
744 case LVN_SETDISPINFOW: return LVN_SETDISPINFOA;
745 case LVN_ODFINDITEMA:
746 case LVN_ODFINDITEMW: return LVN_ODFINDITEMA;
747 case LVN_GETINFOTIPA:
748 case LVN_GETINFOTIPW: return LVN_GETINFOTIPA;
749 /* header forwards */
750 case HDN_TRACKA:
751 case HDN_TRACKW: return HDN_TRACKA;
752 case HDN_ENDTRACKA:
753 case HDN_ENDTRACKW: return HDN_ENDTRACKA;
754 case HDN_BEGINDRAG: return HDN_BEGINDRAG;
755 case HDN_ENDDRAG: return HDN_ENDDRAG;
756 case HDN_ITEMCHANGINGA:
757 case HDN_ITEMCHANGINGW: return HDN_ITEMCHANGINGA;
758 case HDN_ITEMCHANGEDA:
759 case HDN_ITEMCHANGEDW: return HDN_ITEMCHANGEDA;
760 case HDN_ITEMCLICKA:
761 case HDN_ITEMCLICKW: return HDN_ITEMCLICKA;
762 case HDN_DIVIDERDBLCLICKA:
763 case HDN_DIVIDERDBLCLICKW: return HDN_DIVIDERDBLCLICKA;
764 default: break;
766 FIXME("unknown notification %x\n", unicodeNotificationCode);
767 return unicodeNotificationCode;
770 /* forwards header notifications to listview parent */
771 static LRESULT notify_forward_header(const LISTVIEW_INFO *infoPtr, NMHEADERW *lpnmhW)
773 LPCWSTR text = NULL, filter = NULL;
774 LRESULT ret;
775 NMHEADERA *lpnmh = (NMHEADERA*) lpnmhW;
777 /* on unicode format exit earlier */
778 if (infoPtr->notifyFormat == NFR_UNICODE)
779 return SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, lpnmh->hdr.idFrom,
780 (LPARAM)lpnmh);
782 /* header always supplies unicode notifications,
783 all we have to do is to convert strings to ANSI */
784 if (lpnmh->pitem)
786 /* convert item text */
787 if (lpnmh->pitem->mask & HDI_TEXT)
789 text = (LPCWSTR)lpnmh->pitem->pszText;
790 lpnmh->pitem->pszText = NULL;
791 Str_SetPtrWtoA(&lpnmh->pitem->pszText, text);
793 /* convert filter text */
794 if ((lpnmh->pitem->mask & HDI_FILTER) && (lpnmh->pitem->type == HDFT_ISSTRING) &&
795 lpnmh->pitem->pvFilter)
797 filter = (LPCWSTR)((HD_TEXTFILTERA*)lpnmh->pitem->pvFilter)->pszText;
798 ((HD_TEXTFILTERA*)lpnmh->pitem->pvFilter)->pszText = NULL;
799 Str_SetPtrWtoA(&((HD_TEXTFILTERA*)lpnmh->pitem->pvFilter)->pszText, filter);
802 lpnmh->hdr.code = get_ansi_notification(lpnmh->hdr.code);
804 ret = SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, lpnmh->hdr.idFrom,
805 (LPARAM)lpnmh);
807 /* cleanup */
808 if(text)
810 Free(lpnmh->pitem->pszText);
811 lpnmh->pitem->pszText = (LPSTR)text;
813 if(filter)
815 Free(((HD_TEXTFILTERA*)lpnmh->pitem->pvFilter)->pszText);
816 ((HD_TEXTFILTERA*)lpnmh->pitem->pvFilter)->pszText = (LPSTR)filter;
819 return ret;
822 static LRESULT notify_hdr(const LISTVIEW_INFO *infoPtr, INT code, LPNMHDR pnmh)
824 LRESULT result;
826 TRACE("(code=%d)\n", code);
828 pnmh->hwndFrom = infoPtr->hwndSelf;
829 pnmh->idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
830 pnmh->code = code;
831 result = SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, pnmh->idFrom, (LPARAM)pnmh);
833 TRACE(" <= %Id\n", result);
835 return result;
838 static inline BOOL notify(const LISTVIEW_INFO *infoPtr, INT code)
840 NMHDR nmh;
841 HWND hwnd = infoPtr->hwndSelf;
842 notify_hdr(infoPtr, code, &nmh);
843 return IsWindow(hwnd);
846 static inline void notify_itemactivate(const LISTVIEW_INFO *infoPtr, const LVHITTESTINFO *htInfo)
848 NMITEMACTIVATE nmia;
849 LVITEMW item;
851 nmia.uNewState = 0;
852 nmia.uOldState = 0;
853 nmia.uChanged = 0;
854 nmia.uKeyFlags = 0;
856 item.mask = LVIF_PARAM|LVIF_STATE;
857 item.iItem = htInfo->iItem;
858 item.iSubItem = 0;
859 item.stateMask = (UINT)-1;
860 if (LISTVIEW_GetItemT(infoPtr, &item, TRUE)) {
861 nmia.lParam = item.lParam;
862 nmia.uOldState = item.state;
863 nmia.uNewState = item.state | LVIS_ACTIVATING;
864 nmia.uChanged = LVIF_STATE;
867 nmia.iItem = htInfo->iItem;
868 nmia.iSubItem = htInfo->iSubItem;
869 nmia.ptAction = htInfo->pt;
871 if (GetKeyState(VK_SHIFT) & 0x8000) nmia.uKeyFlags |= LVKF_SHIFT;
872 if (GetKeyState(VK_CONTROL) & 0x8000) nmia.uKeyFlags |= LVKF_CONTROL;
873 if (GetKeyState(VK_MENU) & 0x8000) nmia.uKeyFlags |= LVKF_ALT;
875 notify_hdr(infoPtr, LVN_ITEMACTIVATE, (LPNMHDR)&nmia);
878 static inline LRESULT notify_listview(const LISTVIEW_INFO *infoPtr, INT code, LPNMLISTVIEW plvnm)
880 TRACE("(code=%d, plvnm=%s)\n", code, debugnmlistview(plvnm));
881 return notify_hdr(infoPtr, code, (LPNMHDR)plvnm);
884 /* Handles NM_DBLCLK, NM_CLICK, NM_RDBLCLK, NM_RCLICK. Only NM_RCLICK return value is used. */
885 static BOOL notify_click(const LISTVIEW_INFO *infoPtr, INT code, const LVHITTESTINFO *lvht)
887 NMITEMACTIVATE nmia;
888 LVITEMW item;
889 HWND hwnd = infoPtr->hwndSelf;
890 LRESULT ret;
892 TRACE("code=%d, lvht=%s\n", code, debuglvhittestinfo(lvht));
893 ZeroMemory(&nmia, sizeof(nmia));
894 nmia.iItem = lvht->iItem;
895 nmia.iSubItem = lvht->iSubItem;
896 nmia.ptAction = lvht->pt;
897 item.mask = LVIF_PARAM;
898 item.iItem = lvht->iItem;
899 item.iSubItem = 0;
900 if (LISTVIEW_GetItemT(infoPtr, &item, TRUE)) nmia.lParam = item.lParam;
901 ret = notify_hdr(infoPtr, code, (NMHDR*)&nmia);
902 return IsWindow(hwnd) && (code == NM_RCLICK ? !ret : TRUE);
905 static BOOL notify_deleteitem(const LISTVIEW_INFO *infoPtr, INT nItem)
907 NMLISTVIEW nmlv;
908 LVITEMW item;
909 HWND hwnd = infoPtr->hwndSelf;
911 ZeroMemory(&nmlv, sizeof (NMLISTVIEW));
912 nmlv.iItem = nItem;
913 item.mask = LVIF_PARAM;
914 item.iItem = nItem;
915 item.iSubItem = 0;
916 if (LISTVIEW_GetItemT(infoPtr, &item, TRUE)) nmlv.lParam = item.lParam;
917 notify_listview(infoPtr, LVN_DELETEITEM, &nmlv);
918 return IsWindow(hwnd);
922 Send notification. depends on dispinfoW having same
923 structure as dispinfoA.
924 infoPtr : listview struct
925 code : *Unicode* notification code
926 pdi : dispinfo structure (can be unicode or ansi)
927 isW : TRUE if dispinfo is Unicode
929 static BOOL notify_dispinfoT(const LISTVIEW_INFO *infoPtr, UINT code, LPNMLVDISPINFOW pdi, BOOL isW)
931 INT length = 0, ret_length;
932 LPWSTR buffer = NULL, ret_text;
933 BOOL return_ansi = FALSE;
934 BOOL return_unicode = FALSE;
935 BOOL ret;
937 if ((pdi->item.mask & LVIF_TEXT) && is_text(pdi->item.pszText))
939 return_unicode = ( isW && infoPtr->notifyFormat == NFR_ANSI);
940 return_ansi = (!isW && infoPtr->notifyFormat == NFR_UNICODE);
943 ret_length = pdi->item.cchTextMax;
944 ret_text = pdi->item.pszText;
946 if (return_unicode || return_ansi)
948 if (code != LVN_GETDISPINFOW)
950 length = return_ansi ?
951 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pdi->item.pszText, -1, NULL, 0):
952 WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, NULL, 0, NULL, NULL);
954 else
956 length = pdi->item.cchTextMax;
957 *pdi->item.pszText = 0; /* make sure we don't process garbage */
960 buffer = Alloc( (return_ansi ? sizeof(WCHAR) : sizeof(CHAR)) * length);
961 if (!buffer) return FALSE;
963 if (return_ansi)
964 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pdi->item.pszText, -1,
965 buffer, length);
966 else
967 WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, (LPSTR) buffer,
968 length, NULL, NULL);
970 pdi->item.pszText = buffer;
971 pdi->item.cchTextMax = length;
974 if (infoPtr->notifyFormat == NFR_ANSI)
975 code = get_ansi_notification(code);
977 TRACE(" pdi->item=%s\n", debuglvitem_t(&pdi->item, infoPtr->notifyFormat != NFR_ANSI));
978 ret = notify_hdr(infoPtr, code, &pdi->hdr);
979 TRACE(" resulting code=%d\n", pdi->hdr.code);
981 if (return_ansi || return_unicode)
983 if (return_ansi && (pdi->hdr.code == LVN_GETDISPINFOA))
985 strcpy((char*)ret_text, (char*)pdi->item.pszText);
987 else if (return_unicode && (pdi->hdr.code == LVN_GETDISPINFOW))
989 lstrcpyW(ret_text, pdi->item.pszText);
991 else if (return_ansi) /* note : pointer can be changed by app ! */
993 WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, (LPSTR) ret_text,
994 ret_length, NULL, NULL);
996 else
997 MultiByteToWideChar(CP_ACP, 0, (LPSTR) pdi->item.pszText, -1,
998 ret_text, ret_length);
1000 pdi->item.pszText = ret_text; /* restores our buffer */
1001 pdi->item.cchTextMax = ret_length;
1003 Free(buffer);
1004 return ret;
1007 /* if dispinfo holder changed notification code then convert */
1008 if (!isW && (pdi->hdr.code == LVN_GETDISPINFOW) && (pdi->item.mask & LVIF_TEXT))
1010 length = WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, NULL, 0, NULL, NULL);
1012 buffer = Alloc(length * sizeof(CHAR));
1013 if (!buffer) return FALSE;
1015 WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, (LPSTR) buffer,
1016 ret_length, NULL, NULL);
1018 strcpy((LPSTR)pdi->item.pszText, (LPSTR)buffer);
1019 Free(buffer);
1022 return ret;
1025 static void customdraw_fill(NMLVCUSTOMDRAW *lpnmlvcd, const LISTVIEW_INFO *infoPtr, HDC hdc,
1026 const RECT *rcBounds, const LVITEMW *lplvItem)
1028 ZeroMemory(lpnmlvcd, sizeof(NMLVCUSTOMDRAW));
1029 lpnmlvcd->nmcd.hdc = hdc;
1030 lpnmlvcd->nmcd.rc = *rcBounds;
1031 lpnmlvcd->clrTextBk = infoPtr->clrTextBk;
1032 lpnmlvcd->clrText = infoPtr->clrText;
1033 if (!lplvItem) return;
1034 lpnmlvcd->nmcd.dwItemSpec = lplvItem->iItem + 1;
1035 lpnmlvcd->iSubItem = lplvItem->iSubItem;
1036 if (lplvItem->state & LVIS_SELECTED) lpnmlvcd->nmcd.uItemState |= CDIS_SELECTED;
1037 if (lplvItem->state & LVIS_FOCUSED) lpnmlvcd->nmcd.uItemState |= CDIS_FOCUS;
1038 if (lplvItem->iItem == infoPtr->nHotItem) lpnmlvcd->nmcd.uItemState |= CDIS_HOT;
1039 lpnmlvcd->nmcd.lItemlParam = lplvItem->lParam;
1042 static inline DWORD notify_customdraw (const LISTVIEW_INFO *infoPtr, DWORD dwDrawStage, NMLVCUSTOMDRAW *lpnmlvcd)
1044 BOOL isForItem = (lpnmlvcd->nmcd.dwItemSpec != 0);
1045 DWORD result;
1047 lpnmlvcd->nmcd.dwDrawStage = dwDrawStage;
1048 if (isForItem) lpnmlvcd->nmcd.dwDrawStage |= CDDS_ITEM;
1049 if (lpnmlvcd->iSubItem) lpnmlvcd->nmcd.dwDrawStage |= CDDS_SUBITEM;
1050 if (isForItem) lpnmlvcd->nmcd.dwItemSpec--;
1051 result = notify_hdr(infoPtr, NM_CUSTOMDRAW, &lpnmlvcd->nmcd.hdr);
1052 if (isForItem) lpnmlvcd->nmcd.dwItemSpec++;
1053 return result;
1056 static void prepaint_setup (const LISTVIEW_INFO *infoPtr, HDC hdc, const NMLVCUSTOMDRAW *cd, BOOL SubItem)
1058 COLORREF backcolor, textcolor;
1060 backcolor = cd->clrTextBk;
1061 textcolor = cd->clrText;
1063 /* apparently, for selected items, we have to override the returned values */
1064 if (!SubItem)
1066 if (cd->nmcd.uItemState & CDIS_SELECTED)
1068 if (infoPtr->bFocus)
1070 backcolor = comctl32_color.clrHighlight;
1071 textcolor = comctl32_color.clrHighlightText;
1073 else if (infoPtr->dwStyle & LVS_SHOWSELALWAYS)
1075 backcolor = comctl32_color.clr3dFace;
1076 textcolor = comctl32_color.clrBtnText;
1081 if (backcolor == CLR_DEFAULT)
1082 backcolor = comctl32_color.clrWindow;
1083 if (textcolor == CLR_DEFAULT)
1084 textcolor = comctl32_color.clrWindowText;
1086 /* Set the text attributes */
1087 if (backcolor != CLR_NONE)
1089 SetBkMode(hdc, OPAQUE);
1090 SetBkColor(hdc, backcolor);
1092 else
1093 SetBkMode(hdc, TRANSPARENT);
1094 SetTextColor(hdc, textcolor);
1097 static inline DWORD notify_postpaint (const LISTVIEW_INFO *infoPtr, NMLVCUSTOMDRAW *lpnmlvcd)
1099 return notify_customdraw(infoPtr, CDDS_POSTPAINT, lpnmlvcd);
1102 /* returns TRUE when repaint needed, FALSE otherwise */
1103 static BOOL notify_measureitem(LISTVIEW_INFO *infoPtr)
1105 MEASUREITEMSTRUCT mis;
1106 mis.CtlType = ODT_LISTVIEW;
1107 mis.CtlID = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1108 mis.itemID = -1;
1109 mis.itemWidth = 0;
1110 mis.itemData = 0;
1111 mis.itemHeight= infoPtr->nItemHeight;
1112 SendMessageW(infoPtr->hwndNotify, WM_MEASUREITEM, mis.CtlID, (LPARAM)&mis);
1113 if (infoPtr->nItemHeight != max(mis.itemHeight, 1))
1115 infoPtr->nMeasureItemHeight = infoPtr->nItemHeight = max(mis.itemHeight, 1);
1116 return TRUE;
1118 return FALSE;
1121 /******** Item iterator functions **********************************/
1123 static RANGES ranges_create(int count);
1124 static void ranges_destroy(RANGES ranges);
1125 static BOOL ranges_add(RANGES ranges, RANGE range);
1126 static BOOL ranges_del(RANGES ranges, RANGE range);
1127 static void ranges_dump(RANGES ranges);
1129 static inline BOOL ranges_additem(RANGES ranges, INT nItem)
1131 RANGE range = { nItem, nItem + 1 };
1133 return ranges_add(ranges, range);
1136 static inline BOOL ranges_delitem(RANGES ranges, INT nItem)
1138 RANGE range = { nItem, nItem + 1 };
1140 return ranges_del(ranges, range);
1143 /***
1144 * ITERATOR DOCUMENTATION
1146 * The iterator functions allow for easy, and convenient iteration
1147 * over items of interest in the list. Typically, you create an
1148 * iterator, use it, and destroy it, as such:
1149 * ITERATOR i;
1151 * iterator_xxxitems(&i, ...);
1152 * while (iterator_{prev,next}(&i)
1154 * //code which uses i.nItem
1156 * iterator_destroy(&i);
1158 * where xxx is either: framed, or visible.
1159 * Note that it is important that the code destroys the iterator
1160 * after it's done with it, as the creation of the iterator may
1161 * allocate memory, which thus needs to be freed.
1163 * You can iterate both forwards, and backwards through the list,
1164 * by using iterator_next or iterator_prev respectively.
1166 * Lower numbered items are draw on top of higher number items in
1167 * LVS_ICON, and LVS_SMALLICON (which are the only modes where
1168 * items may overlap). So, to test items, you should use
1169 * iterator_next
1170 * which lists the items top to bottom (in Z-order).
1171 * For drawing items, you should use
1172 * iterator_prev
1173 * which lists the items bottom to top (in Z-order).
1174 * If you keep iterating over the items after the end-of-items
1175 * marker (-1) is returned, the iterator will start from the
1176 * beginning. Typically, you don't need to test for -1,
1177 * because iterator_{next,prev} will return TRUE if more items
1178 * are to be iterated over, or FALSE otherwise.
1180 * Note: the iterator is defined to be bidirectional. That is,
1181 * any number of prev followed by any number of next, or
1182 * five versa, should leave the iterator at the same item:
1183 * prev * n, next * n = next * n, prev * n
1185 * The iterator has a notion of an out-of-order, special item,
1186 * which sits at the start of the list. This is used in
1187 * LVS_ICON, and LVS_SMALLICON mode to handle the focused item,
1188 * which needs to be first, as it may overlap other items.
1190 * The code is a bit messy because we have:
1191 * - a special item to deal with
1192 * - simple range, or composite range
1193 * - empty range.
1194 * If you find bugs, or want to add features, please make sure you
1195 * always check/modify *both* iterator_prev, and iterator_next.
1198 /****
1199 * This function iterates through the items in increasing order,
1200 * but prefixed by the special item, then -1. That is:
1201 * special, 1, 2, 3, ..., n, -1.
1202 * Each item is listed only once.
1204 static inline BOOL iterator_next(ITERATOR* i)
1206 if (i->nItem == -1)
1208 i->nItem = i->nSpecial;
1209 if (i->nItem != -1) return TRUE;
1211 if (i->nItem == i->nSpecial)
1213 if (i->ranges) i->index = 0;
1214 goto pickarange;
1217 i->nItem++;
1218 testitem:
1219 if (i->nItem == i->nSpecial) i->nItem++;
1220 if (i->nItem < i->range.upper) return TRUE;
1222 pickarange:
1223 if (i->ranges)
1225 if (i->index < DPA_GetPtrCount(i->ranges->hdpa))
1226 i->range = *(RANGE*)DPA_GetPtr(i->ranges->hdpa, i->index++);
1227 else goto end;
1229 else if (i->nItem >= i->range.upper) goto end;
1231 i->nItem = i->range.lower;
1232 if (i->nItem >= 0) goto testitem;
1233 end:
1234 i->nItem = -1;
1235 return FALSE;
1238 /****
1239 * This function iterates through the items in decreasing order,
1240 * followed by the special item, then -1. That is:
1241 * n, n-1, ..., 3, 2, 1, special, -1.
1242 * Each item is listed only once.
1244 static inline BOOL iterator_prev(ITERATOR* i)
1246 BOOL start = FALSE;
1248 if (i->nItem == -1)
1250 start = TRUE;
1251 if (i->ranges) i->index = DPA_GetPtrCount(i->ranges->hdpa);
1252 goto pickarange;
1254 if (i->nItem == i->nSpecial)
1256 i->nItem = -1;
1257 return FALSE;
1260 testitem:
1261 i->nItem--;
1262 if (i->nItem == i->nSpecial) i->nItem--;
1263 if (i->nItem >= i->range.lower) return TRUE;
1265 pickarange:
1266 if (i->ranges)
1268 if (i->index > 0)
1269 i->range = *(RANGE*)DPA_GetPtr(i->ranges->hdpa, --i->index);
1270 else goto end;
1272 else if (!start && i->nItem < i->range.lower) goto end;
1274 i->nItem = i->range.upper;
1275 if (i->nItem > 0) goto testitem;
1276 end:
1277 return (i->nItem = i->nSpecial) != -1;
1280 static RANGE iterator_range(const ITERATOR *i)
1282 RANGE range;
1284 if (!i->ranges) return i->range;
1286 if (DPA_GetPtrCount(i->ranges->hdpa) > 0)
1288 range.lower = (*(RANGE*)DPA_GetPtr(i->ranges->hdpa, 0)).lower;
1289 range.upper = (*(RANGE*)DPA_GetPtr(i->ranges->hdpa, DPA_GetPtrCount(i->ranges->hdpa) - 1)).upper;
1291 else range.lower = range.upper = 0;
1293 return range;
1296 /***
1297 * Releases resources associated with this iterator.
1299 static inline void iterator_destroy(const ITERATOR *i)
1301 ranges_destroy(i->ranges);
1304 /***
1305 * Create an empty iterator.
1307 static inline void iterator_empty(ITERATOR* i)
1309 ZeroMemory(i, sizeof(*i));
1310 i->nItem = i->nSpecial = i->range.lower = i->range.upper = -1;
1313 /***
1314 * Create an iterator over a range.
1316 static inline void iterator_rangeitems(ITERATOR* i, RANGE range)
1318 iterator_empty(i);
1319 i->range = range;
1322 /***
1323 * Create an iterator over a bunch of ranges.
1324 * Please note that the iterator will take ownership of the ranges,
1325 * and will free them upon destruction.
1327 static inline void iterator_rangesitems(ITERATOR* i, RANGES ranges)
1329 iterator_empty(i);
1330 i->ranges = ranges;
1333 /***
1334 * Creates an iterator over the items which intersect frame.
1335 * Uses absolute coordinates rather than compensating for the current offset.
1337 static BOOL iterator_frameditems_absolute(ITERATOR* i, const LISTVIEW_INFO* infoPtr, const RECT *frame)
1339 RECT rcItem, rcTemp;
1340 RANGES ranges;
1342 TRACE("(frame=%s)\n", wine_dbgstr_rect(frame));
1344 /* in case we fail, we want to return an empty iterator */
1345 iterator_empty(i);
1347 if (infoPtr->nItemCount == 0)
1348 return TRUE;
1350 if (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON)
1352 INT nItem;
1354 if (infoPtr->uView == LV_VIEW_ICON && infoPtr->nFocusedItem != -1)
1356 LISTVIEW_GetItemBox(infoPtr, infoPtr->nFocusedItem, &rcItem);
1357 if (IntersectRect(&rcTemp, &rcItem, frame))
1358 i->nSpecial = infoPtr->nFocusedItem;
1360 if (!(ranges = ranges_create(50))) return FALSE;
1361 iterator_rangesitems(i, ranges);
1362 /* to do better here, we need to have PosX, and PosY sorted */
1363 TRACE("building icon ranges:\n");
1364 for (nItem = 0; nItem < infoPtr->nItemCount; nItem++)
1366 rcItem.left = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosX, nItem);
1367 rcItem.top = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosY, nItem);
1368 rcItem.right = rcItem.left + infoPtr->nItemWidth;
1369 rcItem.bottom = rcItem.top + infoPtr->nItemHeight;
1370 if (IntersectRect(&rcTemp, &rcItem, frame))
1371 ranges_additem(i->ranges, nItem);
1373 return TRUE;
1375 else if (infoPtr->uView == LV_VIEW_DETAILS)
1377 RANGE range;
1379 if (frame->left >= infoPtr->nItemWidth) return TRUE;
1380 if (frame->top >= infoPtr->nItemHeight * infoPtr->nItemCount) return TRUE;
1382 range.lower = max(frame->top / infoPtr->nItemHeight, 0);
1383 range.upper = min((frame->bottom - 1) / infoPtr->nItemHeight, infoPtr->nItemCount - 1) + 1;
1384 if (range.upper <= range.lower) return TRUE;
1385 iterator_rangeitems(i, range);
1386 TRACE(" report=%s\n", debugrange(&i->range));
1388 else
1390 INT nPerCol = max((infoPtr->rcList.bottom - infoPtr->rcList.top) / infoPtr->nItemHeight, 1);
1391 INT nFirstRow = max(frame->top / infoPtr->nItemHeight, 0);
1392 INT nLastRow = min((frame->bottom - 1) / infoPtr->nItemHeight, nPerCol - 1);
1393 INT nFirstCol;
1394 INT nLastCol;
1395 INT lower;
1396 RANGE item_range;
1397 INT nCol;
1399 if (infoPtr->nItemWidth)
1401 nFirstCol = max(frame->left / infoPtr->nItemWidth, 0);
1402 nLastCol = min((frame->right - 1) / infoPtr->nItemWidth, (infoPtr->nItemCount + nPerCol - 1) / nPerCol);
1404 else
1406 nFirstCol = max(frame->left, 0);
1407 nLastCol = min(frame->right - 1, (infoPtr->nItemCount + nPerCol - 1) / nPerCol);
1410 lower = nFirstCol * nPerCol + nFirstRow;
1412 TRACE("nPerCol=%d, nFirstRow=%d, nLastRow=%d, nFirstCol=%d, nLastCol=%d, lower=%d\n",
1413 nPerCol, nFirstRow, nLastRow, nFirstCol, nLastCol, lower);
1415 if (nLastCol < nFirstCol || nLastRow < nFirstRow) return TRUE;
1417 if (!(ranges = ranges_create(nLastCol - nFirstCol + 1))) return FALSE;
1418 iterator_rangesitems(i, ranges);
1419 TRACE("building list ranges:\n");
1420 for (nCol = nFirstCol; nCol <= nLastCol; nCol++)
1422 item_range.lower = nCol * nPerCol + nFirstRow;
1423 if(item_range.lower >= infoPtr->nItemCount) break;
1424 item_range.upper = min(nCol * nPerCol + nLastRow + 1, infoPtr->nItemCount);
1425 TRACE(" list=%s\n", debugrange(&item_range));
1426 ranges_add(i->ranges, item_range);
1430 return TRUE;
1433 /***
1434 * Creates an iterator over the items which intersect lprc.
1436 static BOOL iterator_frameditems(ITERATOR* i, const LISTVIEW_INFO* infoPtr, const RECT *lprc)
1438 RECT frame = *lprc;
1439 POINT Origin;
1441 TRACE("(lprc=%s)\n", wine_dbgstr_rect(lprc));
1443 LISTVIEW_GetOrigin(infoPtr, &Origin);
1444 OffsetRect(&frame, -Origin.x, -Origin.y);
1446 return iterator_frameditems_absolute(i, infoPtr, &frame);
1449 /***
1450 * Creates an iterator over the items which intersect the visible region of hdc.
1452 static BOOL iterator_visibleitems(ITERATOR *i, const LISTVIEW_INFO *infoPtr, HDC hdc)
1454 POINT Origin, Position;
1455 RECT rcItem, rcClip;
1456 INT rgntype;
1458 rgntype = GetClipBox(hdc, &rcClip);
1459 if (rgntype == NULLREGION)
1461 iterator_empty(i);
1462 return TRUE;
1464 if (!iterator_frameditems(i, infoPtr, &rcClip)) return FALSE;
1465 if (rgntype == SIMPLEREGION) return TRUE;
1467 /* first deal with the special item */
1468 if (i->nSpecial != -1)
1470 LISTVIEW_GetItemBox(infoPtr, i->nSpecial, &rcItem);
1471 if (!RectVisible(hdc, &rcItem)) i->nSpecial = -1;
1474 /* if we can't deal with the region, we'll just go with the simple range */
1475 LISTVIEW_GetOrigin(infoPtr, &Origin);
1476 TRACE("building visible range:\n");
1477 if (!i->ranges && i->range.lower < i->range.upper)
1479 if (!(i->ranges = ranges_create(50))) return TRUE;
1480 if (!ranges_add(i->ranges, i->range))
1482 ranges_destroy(i->ranges);
1483 i->ranges = 0;
1484 return TRUE;
1488 /* now delete the invisible items from the list */
1489 while(iterator_next(i))
1491 LISTVIEW_GetItemOrigin(infoPtr, i->nItem, &Position);
1492 rcItem.left = (infoPtr->uView == LV_VIEW_DETAILS) ? Origin.x : Position.x + Origin.x;
1493 rcItem.top = Position.y + Origin.y;
1494 rcItem.right = rcItem.left + infoPtr->nItemWidth;
1495 rcItem.bottom = rcItem.top + infoPtr->nItemHeight;
1496 if (!RectVisible(hdc, &rcItem))
1497 ranges_delitem(i->ranges, i->nItem);
1499 /* the iterator should restart on the next iterator_next */
1500 TRACE("done\n");
1502 return TRUE;
1505 /* Remove common elements from two iterators */
1506 /* Passed iterators have to point on the first elements */
1507 static BOOL iterator_remove_common_items(ITERATOR *iter1, ITERATOR *iter2)
1509 if(!iter1->ranges || !iter2->ranges) {
1510 int lower, upper;
1512 if(iter1->ranges || iter2->ranges ||
1513 (iter1->range.lower<iter2->range.lower && iter1->range.upper>iter2->range.upper) ||
1514 (iter1->range.lower>iter2->range.lower && iter1->range.upper<iter2->range.upper)) {
1515 ERR("result is not a one range iterator\n");
1516 return FALSE;
1519 if(iter1->range.lower==-1 || iter2->range.lower==-1)
1520 return TRUE;
1522 lower = iter1->range.lower;
1523 upper = iter1->range.upper;
1525 if(lower < iter2->range.lower)
1526 iter1->range.upper = iter2->range.lower;
1527 else if(upper > iter2->range.upper)
1528 iter1->range.lower = iter2->range.upper;
1529 else
1530 iter1->range.lower = iter1->range.upper = -1;
1532 if(iter2->range.lower < lower)
1533 iter2->range.upper = lower;
1534 else if(iter2->range.upper > upper)
1535 iter2->range.lower = upper;
1536 else
1537 iter2->range.lower = iter2->range.upper = -1;
1539 return TRUE;
1542 iterator_next(iter1);
1543 iterator_next(iter2);
1545 while(1) {
1546 if(iter1->nItem==-1 || iter2->nItem==-1)
1547 break;
1549 if(iter1->nItem == iter2->nItem) {
1550 int delete = iter1->nItem;
1552 iterator_prev(iter1);
1553 iterator_prev(iter2);
1554 ranges_delitem(iter1->ranges, delete);
1555 ranges_delitem(iter2->ranges, delete);
1556 iterator_next(iter1);
1557 iterator_next(iter2);
1558 } else if(iter1->nItem > iter2->nItem)
1559 iterator_next(iter2);
1560 else
1561 iterator_next(iter1);
1564 iter1->nItem = iter1->range.lower = iter1->range.upper = -1;
1565 iter2->nItem = iter2->range.lower = iter2->range.upper = -1;
1566 return TRUE;
1569 /******** Misc helper functions ************************************/
1571 static inline LRESULT CallWindowProcT(WNDPROC proc, HWND hwnd, UINT uMsg,
1572 WPARAM wParam, LPARAM lParam, BOOL isW)
1574 if (isW) return CallWindowProcW(proc, hwnd, uMsg, wParam, lParam);
1575 else return CallWindowProcA(proc, hwnd, uMsg, wParam, lParam);
1578 static inline BOOL is_autoarrange(const LISTVIEW_INFO *infoPtr)
1580 return (infoPtr->dwStyle & LVS_AUTOARRANGE) &&
1581 (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON);
1584 static void toggle_checkbox_state(LISTVIEW_INFO *infoPtr, INT nItem)
1586 DWORD state = STATEIMAGEINDEX(LISTVIEW_GetItemState(infoPtr, nItem, LVIS_STATEIMAGEMASK));
1587 if(state == 1 || state == 2)
1589 LVITEMW lvitem;
1590 state ^= 3;
1591 lvitem.state = INDEXTOSTATEIMAGEMASK(state);
1592 lvitem.stateMask = LVIS_STATEIMAGEMASK;
1593 LISTVIEW_SetItemState(infoPtr, nItem, &lvitem);
1597 /* this should be called after window style got updated,
1598 it used to reset view state to match current window style */
1599 static inline void map_style_view(LISTVIEW_INFO *infoPtr)
1601 switch (infoPtr->dwStyle & LVS_TYPEMASK)
1603 case LVS_ICON:
1604 infoPtr->uView = LV_VIEW_ICON;
1605 break;
1606 case LVS_REPORT:
1607 infoPtr->uView = LV_VIEW_DETAILS;
1608 break;
1609 case LVS_SMALLICON:
1610 infoPtr->uView = LV_VIEW_SMALLICON;
1611 break;
1612 case LVS_LIST:
1613 infoPtr->uView = LV_VIEW_LIST;
1617 /* computes next item id value */
1618 static DWORD get_next_itemid(const LISTVIEW_INFO *infoPtr)
1620 INT count = DPA_GetPtrCount(infoPtr->hdpaItemIds);
1622 if (count > 0)
1624 ITEM_ID *lpID = DPA_GetPtr(infoPtr->hdpaItemIds, count - 1);
1625 return lpID->id + 1;
1627 return 0;
1630 /******** Internal API functions ************************************/
1632 static inline COLUMN_INFO * LISTVIEW_GetColumnInfo(const LISTVIEW_INFO *infoPtr, INT nSubItem)
1634 static COLUMN_INFO mainItem;
1636 if (nSubItem == 0 && DPA_GetPtrCount(infoPtr->hdpaColumns) == 0) return &mainItem;
1637 assert (nSubItem >= 0 && nSubItem < DPA_GetPtrCount(infoPtr->hdpaColumns));
1639 /* update cached column rectangles */
1640 if (infoPtr->colRectsDirty)
1642 COLUMN_INFO *info;
1643 LISTVIEW_INFO *Ptr = (LISTVIEW_INFO*)infoPtr;
1644 INT i;
1646 for (i = 0; i < DPA_GetPtrCount(infoPtr->hdpaColumns); i++) {
1647 info = DPA_GetPtr(infoPtr->hdpaColumns, i);
1648 SendMessageW(infoPtr->hwndHeader, HDM_GETITEMRECT, i, (LPARAM)&info->rcHeader);
1650 Ptr->colRectsDirty = FALSE;
1653 return DPA_GetPtr(infoPtr->hdpaColumns, nSubItem);
1656 static INT LISTVIEW_CreateHeader(LISTVIEW_INFO *infoPtr)
1658 DWORD dFlags = WS_CHILD | HDS_HORZ | HDS_FULLDRAG | HDS_DRAGDROP;
1659 HINSTANCE hInst;
1661 if (infoPtr->hwndHeader) return 0;
1663 TRACE("Creating header for list %p\n", infoPtr->hwndSelf);
1665 /* setup creation flags */
1666 dFlags |= (LVS_NOSORTHEADER & infoPtr->dwStyle) ? 0 : HDS_BUTTONS;
1667 dFlags |= (LVS_NOCOLUMNHEADER & infoPtr->dwStyle) ? HDS_HIDDEN : 0;
1669 hInst = (HINSTANCE)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_HINSTANCE);
1671 /* create header */
1672 infoPtr->hwndHeader = CreateWindowW(WC_HEADERW, NULL, dFlags,
1673 0, 0, 0, 0, infoPtr->hwndSelf, NULL, hInst, NULL);
1674 if (!infoPtr->hwndHeader) return -1;
1676 /* set header unicode format */
1677 SendMessageW(infoPtr->hwndHeader, HDM_SETUNICODEFORMAT, TRUE, 0);
1679 /* set header font */
1680 SendMessageW(infoPtr->hwndHeader, WM_SETFONT, (WPARAM)infoPtr->hFont, TRUE);
1682 /* set header image list */
1683 if (infoPtr->himlSmall)
1684 SendMessageW(infoPtr->hwndHeader, HDM_SETIMAGELIST, 0, (LPARAM)infoPtr->himlSmall);
1686 LISTVIEW_UpdateSize(infoPtr);
1688 return 0;
1691 static inline void LISTVIEW_GetHeaderRect(const LISTVIEW_INFO *infoPtr, INT nSubItem, LPRECT lprc)
1693 *lprc = LISTVIEW_GetColumnInfo(infoPtr, nSubItem)->rcHeader;
1696 static inline BOOL LISTVIEW_IsHeaderEnabled(const LISTVIEW_INFO *infoPtr)
1698 return (infoPtr->uView == LV_VIEW_DETAILS ||
1699 infoPtr->dwLvExStyle & LVS_EX_HEADERINALLVIEWS) &&
1700 !(infoPtr->dwStyle & LVS_NOCOLUMNHEADER);
1703 static inline BOOL LISTVIEW_GetItemW(const LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem)
1705 return LISTVIEW_GetItemT(infoPtr, lpLVItem, TRUE);
1708 /* used to handle collapse main item column case */
1709 static inline BOOL LISTVIEW_DrawFocusRect(const LISTVIEW_INFO *infoPtr, HDC hdc)
1711 return (infoPtr->rcFocus.left < infoPtr->rcFocus.right) ?
1712 DrawFocusRect(hdc, &infoPtr->rcFocus) : FALSE;
1715 /* Listview invalidation functions: use _only_ these functions to invalidate */
1717 static inline BOOL is_redrawing(const LISTVIEW_INFO *infoPtr)
1719 return infoPtr->redraw;
1722 static inline void LISTVIEW_InvalidateRect(const LISTVIEW_INFO *infoPtr, const RECT* rect)
1724 if(!is_redrawing(infoPtr)) return;
1725 TRACE(" invalidating rect=%s\n", wine_dbgstr_rect(rect));
1726 InvalidateRect(infoPtr->hwndSelf, rect, TRUE);
1729 static inline void LISTVIEW_InvalidateItem(const LISTVIEW_INFO *infoPtr, INT nItem)
1731 RECT rcBox;
1733 if (!is_redrawing(infoPtr) || nItem < 0 || nItem >= infoPtr->nItemCount)
1734 return;
1736 LISTVIEW_GetItemBox(infoPtr, nItem, &rcBox);
1737 LISTVIEW_InvalidateRect(infoPtr, &rcBox);
1740 static inline void LISTVIEW_InvalidateSubItem(const LISTVIEW_INFO *infoPtr, INT nItem, INT nSubItem)
1742 POINT Origin, Position;
1743 RECT rcBox;
1745 if(!is_redrawing(infoPtr)) return;
1746 assert (infoPtr->uView == LV_VIEW_DETAILS);
1747 LISTVIEW_GetOrigin(infoPtr, &Origin);
1748 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
1749 LISTVIEW_GetHeaderRect(infoPtr, nSubItem, &rcBox);
1750 rcBox.top = 0;
1751 rcBox.bottom = infoPtr->nItemHeight;
1752 OffsetRect(&rcBox, Origin.x, Origin.y + Position.y);
1753 LISTVIEW_InvalidateRect(infoPtr, &rcBox);
1756 static inline void LISTVIEW_InvalidateList(const LISTVIEW_INFO *infoPtr)
1758 LISTVIEW_InvalidateRect(infoPtr, NULL);
1761 static inline void LISTVIEW_InvalidateColumn(const LISTVIEW_INFO *infoPtr, INT nColumn)
1763 RECT rcCol;
1765 if(!is_redrawing(infoPtr)) return;
1766 LISTVIEW_GetHeaderRect(infoPtr, nColumn, &rcCol);
1767 rcCol.top = infoPtr->rcList.top;
1768 rcCol.bottom = infoPtr->rcList.bottom;
1769 LISTVIEW_InvalidateRect(infoPtr, &rcCol);
1772 /***
1773 * DESCRIPTION:
1774 * Retrieves the number of items that can fit vertically in the client area.
1776 * PARAMETER(S):
1777 * [I] infoPtr : valid pointer to the listview structure
1779 * RETURN:
1780 * Number of items per row.
1782 static inline INT LISTVIEW_GetCountPerRow(const LISTVIEW_INFO *infoPtr)
1784 INT nListWidth = infoPtr->rcList.right - infoPtr->rcList.left;
1786 return max(nListWidth/(infoPtr->nItemWidth ? infoPtr->nItemWidth : 1), 1);
1789 /***
1790 * DESCRIPTION:
1791 * Retrieves the number of items that can fit horizontally in the client
1792 * area.
1794 * PARAMETER(S):
1795 * [I] infoPtr : valid pointer to the listview structure
1797 * RETURN:
1798 * Number of items per column.
1800 static inline INT LISTVIEW_GetCountPerColumn(const LISTVIEW_INFO *infoPtr)
1802 INT nListHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
1804 return infoPtr->nItemHeight ? max(nListHeight / infoPtr->nItemHeight, 1) : 0;
1808 /*************************************************************************
1809 * LISTVIEW_ProcessLetterKeys
1811 * Processes keyboard messages generated by pressing the letter keys
1812 * on the keyboard.
1813 * What this does is perform a case insensitive search from the
1814 * current position with the following quirks:
1815 * - If two chars or more are pressed in quick succession we search
1816 * for the corresponding string (e.g. 'abc').
1817 * - If there is a delay we wipe away the current search string and
1818 * restart with just that char.
1819 * - If the user keeps pressing the same character, whether slowly or
1820 * fast, so that the search string is entirely composed of this
1821 * character ('aaaaa' for instance), then we search for first item
1822 * that starting with that character.
1823 * - If the user types the above character in quick succession, then
1824 * we must also search for the corresponding string ('aaaaa'), and
1825 * go to that string if there is a match.
1827 * PARAMETERS
1828 * [I] hwnd : handle to the window
1829 * [I] charCode : the character code, the actual character
1830 * [I] keyData : key data
1832 * RETURNS
1834 * Zero.
1836 * BUGS
1838 * - The current implementation has a list of characters it will
1839 * accept and it ignores everything else. In particular it will
1840 * ignore accentuated characters which seems to match what
1841 * Windows does. But I'm not sure it makes sense to follow
1842 * Windows there.
1843 * - We don't sound a beep when the search fails.
1845 * SEE ALSO
1847 * TREEVIEW_ProcessLetterKeys
1849 static INT LISTVIEW_ProcessLetterKeys(LISTVIEW_INFO *infoPtr, WPARAM charCode, LPARAM keyData)
1851 WCHAR buffer[MAX_PATH];
1852 DWORD prevTime;
1853 LVITEMW item;
1854 int startidx;
1855 INT nItem;
1856 INT diff;
1858 /* simple parameter checking */
1859 if (!charCode || !keyData || infoPtr->nItemCount == 0) return 0;
1861 /* only allow the valid WM_CHARs through */
1862 if (!iswalnum(charCode) &&
1863 charCode != '.' && charCode != '`' && 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 return 0;
1874 /* update the search parameters */
1875 prevTime = infoPtr->lastKeyPressTimestamp;
1876 infoPtr->lastKeyPressTimestamp = GetTickCount();
1877 diff = infoPtr->lastKeyPressTimestamp - prevTime;
1879 if (diff >= 0 && diff < KEY_DELAY)
1881 if (infoPtr->nSearchParamLength < MAX_PATH - 1)
1882 infoPtr->szSearchParam[infoPtr->nSearchParamLength++] = charCode;
1884 if (infoPtr->charCode != charCode)
1885 infoPtr->charCode = charCode = 0;
1887 else
1889 infoPtr->charCode = charCode;
1890 infoPtr->szSearchParam[0] = charCode;
1891 infoPtr->nSearchParamLength = 1;
1894 /* should start from next after focused item, so next item that matches
1895 will be selected, if there isn't any and focused matches it will be selected
1896 on second search stage from beginning of the list */
1897 if (infoPtr->nFocusedItem >= 0 && infoPtr->nItemCount > 1)
1899 /* with some accumulated search data available start with current focus, otherwise
1900 it's excluded from search */
1901 startidx = infoPtr->nSearchParamLength > 1 ? infoPtr->nFocusedItem : infoPtr->nFocusedItem + 1;
1902 if (startidx == infoPtr->nItemCount) startidx = 0;
1904 else
1905 startidx = 0;
1907 /* let application handle this for virtual listview */
1908 if (infoPtr->dwStyle & LVS_OWNERDATA)
1910 NMLVFINDITEMW nmlv;
1912 memset(&nmlv.lvfi, 0, sizeof(nmlv.lvfi));
1913 nmlv.lvfi.flags = (LVFI_WRAP | LVFI_PARTIAL);
1914 nmlv.lvfi.psz = infoPtr->szSearchParam;
1915 nmlv.iStart = startidx;
1917 infoPtr->szSearchParam[infoPtr->nSearchParamLength] = 0;
1919 nItem = notify_hdr(infoPtr, LVN_ODFINDITEMW, (LPNMHDR)&nmlv.hdr);
1921 else
1923 int i = startidx, endidx;
1925 /* and search from the current position */
1926 nItem = -1;
1927 endidx = infoPtr->nItemCount;
1929 /* first search in [startidx, endidx), on failure continue in [0, startidx) */
1930 while (1)
1932 /* start from first item if not found with >= startidx */
1933 if (i == infoPtr->nItemCount && startidx > 0)
1935 endidx = startidx;
1936 startidx = 0;
1939 for (i = startidx; i < endidx; i++)
1941 /* retrieve text */
1942 item.mask = LVIF_TEXT;
1943 item.iItem = i;
1944 item.iSubItem = 0;
1945 item.pszText = buffer;
1946 item.cchTextMax = MAX_PATH;
1947 if (!LISTVIEW_GetItemW(infoPtr, &item)) return 0;
1949 if (!wcsnicmp(item.pszText, infoPtr->szSearchParam, infoPtr->nSearchParamLength))
1951 nItem = i;
1952 break;
1954 /* this is used to find first char match when search string is not available yet,
1955 otherwise every WM_CHAR will search to next item by first char, ignoring that we're
1956 already waiting for user to complete a string */
1957 else if (nItem == -1 && infoPtr->nSearchParamLength == 1 && !wcsnicmp(item.pszText, infoPtr->szSearchParam, 1))
1959 /* this would work but we must keep looking for a longer match */
1960 nItem = i;
1964 if ( nItem != -1 || /* found something */
1965 endidx != infoPtr->nItemCount || /* second search done */
1966 (startidx == 0 && endidx == infoPtr->nItemCount) /* full range for first search */ )
1967 break;
1971 if (nItem != -1)
1972 LISTVIEW_KeySelection(infoPtr, nItem, FALSE);
1974 return 0;
1977 /*************************************************************************
1978 * LISTVIEW_UpdateHeaderSize [Internal]
1980 * Function to resize the header control
1982 * PARAMS
1983 * [I] hwnd : handle to a window
1984 * [I] nNewScrollPos : scroll pos to set
1986 * RETURNS
1987 * None.
1989 static void LISTVIEW_UpdateHeaderSize(const LISTVIEW_INFO *infoPtr, INT nNewScrollPos)
1991 RECT winRect;
1992 POINT point[2];
1994 TRACE("nNewScrollPos=%d\n", nNewScrollPos);
1996 if (!infoPtr->hwndHeader) return;
1998 GetWindowRect(infoPtr->hwndHeader, &winRect);
1999 point[0].x = winRect.left;
2000 point[0].y = winRect.top;
2001 point[1].x = winRect.right;
2002 point[1].y = winRect.bottom;
2004 MapWindowPoints(HWND_DESKTOP, infoPtr->hwndSelf, point, 2);
2005 point[0].x = -nNewScrollPos;
2006 point[1].x += nNewScrollPos;
2008 SetWindowPos(infoPtr->hwndHeader,0,
2009 point[0].x,point[0].y,point[1].x,point[1].y,
2010 (infoPtr->dwStyle & LVS_NOCOLUMNHEADER) ? SWP_HIDEWINDOW : SWP_SHOWWINDOW |
2011 SWP_NOZORDER | SWP_NOACTIVATE);
2014 static INT LISTVIEW_UpdateHScroll(LISTVIEW_INFO *infoPtr)
2016 SCROLLINFO horzInfo;
2017 INT dx;
2019 ZeroMemory(&horzInfo, sizeof(SCROLLINFO));
2020 horzInfo.cbSize = sizeof(SCROLLINFO);
2021 horzInfo.nPage = infoPtr->rcList.right - infoPtr->rcList.left;
2023 /* for now, we'll set info.nMax to the _count_, and adjust it later */
2024 if (infoPtr->uView == LV_VIEW_LIST)
2026 INT nPerCol = LISTVIEW_GetCountPerColumn(infoPtr);
2027 horzInfo.nMax = (infoPtr->nItemCount + nPerCol - 1) / nPerCol;
2029 /* scroll by at least one column per page */
2030 if(horzInfo.nPage < infoPtr->nItemWidth)
2031 horzInfo.nPage = infoPtr->nItemWidth;
2033 if (infoPtr->nItemWidth)
2034 horzInfo.nPage /= infoPtr->nItemWidth;
2036 else if (infoPtr->uView == LV_VIEW_DETAILS)
2038 horzInfo.nMax = infoPtr->nItemWidth;
2040 else /* LV_VIEW_ICON, or LV_VIEW_SMALLICON */
2042 RECT rcView;
2044 if (LISTVIEW_GetViewRect(infoPtr, &rcView)) horzInfo.nMax = rcView.right - rcView.left;
2047 if (LISTVIEW_IsHeaderEnabled(infoPtr))
2049 if (DPA_GetPtrCount(infoPtr->hdpaColumns))
2051 RECT rcHeader;
2052 INT index;
2054 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX,
2055 DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, 0);
2057 LISTVIEW_GetHeaderRect(infoPtr, index, &rcHeader);
2058 horzInfo.nMax = rcHeader.right;
2059 TRACE("horzInfo.nMax=%d\n", horzInfo.nMax);
2063 horzInfo.fMask = SIF_RANGE | SIF_PAGE;
2064 horzInfo.nMax = max(horzInfo.nMax - 1, 0);
2065 dx = GetScrollPos(infoPtr->hwndSelf, SB_HORZ);
2066 dx -= SetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &horzInfo, TRUE);
2067 TRACE("horzInfo=%s\n", debugscrollinfo(&horzInfo));
2069 /* Update the Header Control */
2070 if (infoPtr->hwndHeader)
2072 horzInfo.fMask = SIF_POS;
2073 GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &horzInfo);
2074 LISTVIEW_UpdateHeaderSize(infoPtr, horzInfo.nPos);
2077 LISTVIEW_UpdateSize(infoPtr);
2078 return dx;
2081 static INT LISTVIEW_UpdateVScroll(LISTVIEW_INFO *infoPtr)
2083 SCROLLINFO vertInfo;
2084 INT dy;
2086 ZeroMemory(&vertInfo, sizeof(SCROLLINFO));
2087 vertInfo.cbSize = sizeof(SCROLLINFO);
2088 vertInfo.nPage = infoPtr->rcList.bottom - infoPtr->rcList.top;
2090 if (infoPtr->uView == LV_VIEW_DETAILS)
2092 vertInfo.nMax = infoPtr->nItemCount;
2094 /* scroll by at least one page */
2095 if(vertInfo.nPage < infoPtr->nItemHeight)
2096 vertInfo.nPage = infoPtr->nItemHeight;
2098 if (infoPtr->nItemHeight > 0)
2099 vertInfo.nPage /= infoPtr->nItemHeight;
2101 else if (infoPtr->uView != LV_VIEW_LIST) /* LV_VIEW_ICON, or LV_VIEW_SMALLICON */
2103 RECT rcView;
2105 if (LISTVIEW_GetViewRect(infoPtr, &rcView)) vertInfo.nMax = rcView.bottom - rcView.top;
2108 vertInfo.fMask = SIF_RANGE | SIF_PAGE;
2109 vertInfo.nMax = max(vertInfo.nMax - 1, 0);
2110 dy = GetScrollPos(infoPtr->hwndSelf, SB_VERT);
2111 dy -= SetScrollInfo(infoPtr->hwndSelf, SB_VERT, &vertInfo, TRUE);
2112 TRACE("vertInfo=%s\n", debugscrollinfo(&vertInfo));
2114 LISTVIEW_UpdateSize(infoPtr);
2115 return dy;
2118 /***
2119 * DESCRIPTION:
2120 * Update the scrollbars. This function should be called whenever
2121 * the content, size or view changes.
2123 * PARAMETER(S):
2124 * [I] infoPtr : valid pointer to the listview structure
2126 * RETURN:
2127 * None
2129 static void LISTVIEW_UpdateScroll(LISTVIEW_INFO *infoPtr)
2131 INT dx, dy, pass;
2133 if ((infoPtr->dwStyle & LVS_NOSCROLL) || !is_redrawing(infoPtr)) return;
2135 /* Setting the horizontal scroll can change the listview size
2136 * (and potentially everything else) so we need to recompute
2137 * everything again for the vertical scroll and vice-versa
2139 for (dx = 0, dy = 0, pass = 0; pass <= 1; pass++)
2141 dx += LISTVIEW_UpdateHScroll(infoPtr);
2142 dy += LISTVIEW_UpdateVScroll(infoPtr);
2145 /* Change of the range may have changed the scroll pos. If so move the content */
2146 if (dx != 0 || dy != 0)
2148 RECT listRect;
2149 listRect = infoPtr->rcList;
2150 ScrollWindowEx(infoPtr->hwndSelf, dx, dy, &listRect, &listRect, 0, 0,
2151 SW_ERASE | SW_INVALIDATE);
2156 /***
2157 * DESCRIPTION:
2158 * Shows/hides the focus rectangle.
2160 * PARAMETER(S):
2161 * [I] infoPtr : valid pointer to the listview structure
2162 * [I] fShow : TRUE to show the focus, FALSE to hide it.
2164 * RETURN:
2165 * None
2167 static void LISTVIEW_ShowFocusRect(const LISTVIEW_INFO *infoPtr, BOOL fShow)
2169 HDC hdc;
2171 TRACE("fShow=%d, nItem=%d\n", fShow, infoPtr->nFocusedItem);
2173 if (infoPtr->nFocusedItem < 0) return;
2175 /* we need some gymnastics in ICON mode to handle large items */
2176 if (infoPtr->uView == LV_VIEW_ICON)
2178 RECT rcBox;
2180 LISTVIEW_GetItemBox(infoPtr, infoPtr->nFocusedItem, &rcBox);
2181 if ((rcBox.bottom - rcBox.top) > infoPtr->nItemHeight)
2183 LISTVIEW_InvalidateRect(infoPtr, &rcBox);
2184 return;
2188 if (!(hdc = GetDC(infoPtr->hwndSelf))) return;
2190 /* for some reason, owner draw should work only in report mode */
2191 if ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (infoPtr->uView == LV_VIEW_DETAILS))
2193 DRAWITEMSTRUCT dis;
2194 LVITEMW item;
2196 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
2197 HFONT hOldFont = SelectObject(hdc, hFont);
2199 item.iItem = infoPtr->nFocusedItem;
2200 item.iSubItem = 0;
2201 item.mask = LVIF_PARAM;
2202 if (!LISTVIEW_GetItemW(infoPtr, &item)) goto done;
2204 ZeroMemory(&dis, sizeof(dis));
2205 dis.CtlType = ODT_LISTVIEW;
2206 dis.CtlID = (UINT)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
2207 dis.itemID = item.iItem;
2208 dis.itemAction = ODA_FOCUS;
2209 if (fShow) dis.itemState |= ODS_FOCUS;
2210 dis.hwndItem = infoPtr->hwndSelf;
2211 dis.hDC = hdc;
2212 LISTVIEW_GetItemBox(infoPtr, dis.itemID, &dis.rcItem);
2213 dis.itemData = item.lParam;
2215 SendMessageW(infoPtr->hwndNotify, WM_DRAWITEM, dis.CtlID, (LPARAM)&dis);
2217 SelectObject(hdc, hOldFont);
2219 else
2220 LISTVIEW_InvalidateItem(infoPtr, infoPtr->nFocusedItem);
2222 done:
2223 ReleaseDC(infoPtr->hwndSelf, hdc);
2226 /***
2227 * Invalidates all visible selected items.
2229 static void LISTVIEW_InvalidateSelectedItems(const LISTVIEW_INFO *infoPtr)
2231 ITERATOR i;
2233 iterator_frameditems(&i, infoPtr, &infoPtr->rcList);
2234 while(iterator_next(&i))
2236 if (LISTVIEW_GetItemState(infoPtr, i.nItem, LVIS_SELECTED))
2237 LISTVIEW_InvalidateItem(infoPtr, i.nItem);
2239 iterator_destroy(&i);
2243 /***
2244 * DESCRIPTION: [INTERNAL]
2245 * Computes an item's (left,top) corner, relative to rcView.
2246 * That is, the position has NOT been made relative to the Origin.
2247 * This is deliberate, to avoid computing the Origin over, and
2248 * over again, when this function is called in a loop. Instead,
2249 * one can factor the computation of the Origin before the loop,
2250 * and offset the value returned by this function, on every iteration.
2252 * PARAMETER(S):
2253 * [I] infoPtr : valid pointer to the listview structure
2254 * [I] nItem : item number
2255 * [O] lpptOrig : item top, left corner
2257 * RETURN:
2258 * None.
2260 static void LISTVIEW_GetItemOrigin(const LISTVIEW_INFO *infoPtr, INT nItem, LPPOINT lpptPosition)
2262 assert(nItem >= 0 && nItem < infoPtr->nItemCount);
2264 if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
2266 lpptPosition->x = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosX, nItem);
2267 lpptPosition->y = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosY, nItem);
2269 else if (infoPtr->uView == LV_VIEW_LIST)
2271 INT nCountPerColumn = LISTVIEW_GetCountPerColumn(infoPtr);
2272 lpptPosition->x = nItem / nCountPerColumn * infoPtr->nItemWidth;
2273 lpptPosition->y = nItem % nCountPerColumn * infoPtr->nItemHeight;
2275 else /* LV_VIEW_DETAILS */
2277 lpptPosition->x = REPORT_MARGINX;
2278 /* item is always at zero indexed column */
2279 if (DPA_GetPtrCount(infoPtr->hdpaColumns) > 0)
2280 lpptPosition->x += LISTVIEW_GetColumnInfo(infoPtr, 0)->rcHeader.left;
2281 lpptPosition->y = nItem * infoPtr->nItemHeight;
2285 /***
2286 * DESCRIPTION: [INTERNAL]
2287 * Compute the rectangles of an item. This is to localize all
2288 * the computations in one place. If you are not interested in some
2289 * of these values, simply pass in a NULL -- the function is smart
2290 * enough to compute only what's necessary. The function computes
2291 * the standard rectangles (BOUNDS, ICON, LABEL) plus a non-standard
2292 * one, the BOX rectangle. This rectangle is very cheap to compute,
2293 * and is guaranteed to contain all the other rectangles. Computing
2294 * the ICON rect is also cheap, but all the others are potentially
2295 * expensive. This gives an easy and effective optimization when
2296 * searching (like point inclusion, or rectangle intersection):
2297 * first test against the BOX, and if TRUE, test against the desired
2298 * rectangle.
2299 * If the function does not have all the necessary information
2300 * to computed the requested rectangles, will crash with a
2301 * failed assertion. This is done so we catch all programming
2302 * errors, given that the function is called only from our code.
2304 * We have the following 'special' meanings for a few fields:
2305 * * If LVIS_FOCUSED is set, we assume the item has the focus
2306 * This is important in ICON mode, where it might get a larger
2307 * then usual rectangle
2309 * Please note that subitem support works only in REPORT mode.
2311 * PARAMETER(S):
2312 * [I] infoPtr : valid pointer to the listview structure
2313 * [I] lpLVItem : item to compute the measures for
2314 * [O] lprcBox : ptr to Box rectangle
2315 * Same as LVM_GETITEMRECT with LVIR_BOUNDS
2316 * [0] lprcSelectBox : ptr to select box rectangle
2317 * Same as LVM_GETITEMRECT with LVIR_SELECTEDBOUNDS
2318 * [O] lprcIcon : ptr to Icon rectangle
2319 * Same as LVM_GETITEMRECT with LVIR_ICON
2320 * [O] lprcStateIcon: ptr to State Icon rectangle
2321 * [O] lprcLabel : ptr to Label rectangle
2322 * Same as LVM_GETITEMRECT with LVIR_LABEL
2324 * RETURN:
2325 * None.
2327 static void LISTVIEW_GetItemMetrics(const LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem,
2328 LPRECT lprcBox, LPRECT lprcSelectBox,
2329 LPRECT lprcIcon, LPRECT lprcStateIcon, LPRECT lprcLabel)
2331 BOOL doSelectBox = FALSE, doIcon = FALSE, doLabel = FALSE, oversizedBox = FALSE;
2332 RECT Box, SelectBox, Icon, Label;
2333 COLUMN_INFO *lpColumnInfo = NULL;
2334 SIZE labelSize = { 0, 0 };
2336 TRACE("(lpLVItem=%s)\n", debuglvitem_t(lpLVItem, TRUE));
2338 /* Be smart and try to figure out the minimum we have to do */
2339 if (lpLVItem->iSubItem) assert(infoPtr->uView == LV_VIEW_DETAILS);
2340 if (infoPtr->uView == LV_VIEW_ICON && (lprcBox || lprcLabel))
2342 assert((lpLVItem->mask & LVIF_STATE) && (lpLVItem->stateMask & LVIS_FOCUSED));
2343 if (lpLVItem->state & LVIS_FOCUSED) oversizedBox = doLabel = TRUE;
2345 if (lprcSelectBox) doSelectBox = TRUE;
2346 if (lprcLabel) doLabel = TRUE;
2347 if (doLabel || lprcIcon || lprcStateIcon) doIcon = TRUE;
2348 if (doSelectBox)
2350 doIcon = TRUE;
2351 doLabel = TRUE;
2354 /************************************************************/
2355 /* compute the box rectangle (it should be cheap to do) */
2356 /************************************************************/
2357 if (lpLVItem->iSubItem || infoPtr->uView == LV_VIEW_DETAILS)
2358 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, lpLVItem->iSubItem);
2360 if (lpLVItem->iSubItem)
2362 Box = lpColumnInfo->rcHeader;
2364 else
2366 Box.left = 0;
2367 Box.right = infoPtr->nItemWidth;
2369 Box.top = 0;
2370 Box.bottom = infoPtr->nItemHeight;
2372 /******************************************************************/
2373 /* compute ICON bounding box (ala LVM_GETITEMRECT) and STATEICON */
2374 /******************************************************************/
2375 if (doIcon)
2377 LONG state_width = 0;
2379 if (infoPtr->himlState && lpLVItem->iSubItem == 0)
2380 state_width = infoPtr->iconStateSize.cx;
2382 if (infoPtr->uView == LV_VIEW_ICON)
2384 Icon.left = Box.left + state_width;
2385 if (infoPtr->himlNormal)
2386 Icon.left += (infoPtr->nItemWidth - infoPtr->iconSize.cx - state_width) / 2;
2387 Icon.top = Box.top + ICON_TOP_PADDING;
2388 Icon.right = Icon.left;
2389 Icon.bottom = Icon.top;
2390 if (infoPtr->himlNormal)
2392 Icon.right += infoPtr->iconSize.cx;
2393 Icon.bottom += infoPtr->iconSize.cy;
2396 else /* LV_VIEW_SMALLICON, LV_VIEW_LIST or LV_VIEW_DETAILS */
2398 Icon.left = Box.left + state_width;
2400 if (infoPtr->uView == LV_VIEW_DETAILS && lpLVItem->iSubItem == 0)
2402 /* we need the indent in report mode */
2403 assert(lpLVItem->mask & LVIF_INDENT);
2404 Icon.left += infoPtr->iconSize.cx * lpLVItem->iIndent + REPORT_MARGINX;
2407 Icon.top = Box.top;
2408 Icon.right = Icon.left;
2409 if (infoPtr->himlSmall &&
2410 (!lpColumnInfo || lpLVItem->iSubItem == 0 ||
2411 ((infoPtr->dwLvExStyle & LVS_EX_SUBITEMIMAGES) && lpLVItem->iImage != I_IMAGECALLBACK)))
2412 Icon.right += infoPtr->iconSize.cx;
2413 Icon.bottom = Icon.top + infoPtr->iconSize.cy;
2415 if(lprcIcon) *lprcIcon = Icon;
2416 TRACE(" - icon=%s\n", wine_dbgstr_rect(&Icon));
2418 /* TODO: is this correct? */
2419 if (lprcStateIcon)
2421 lprcStateIcon->left = Icon.left - state_width;
2422 lprcStateIcon->right = Icon.left;
2423 lprcStateIcon->top = Icon.top;
2424 lprcStateIcon->bottom = lprcStateIcon->top + infoPtr->iconSize.cy;
2425 TRACE(" - state icon=%s\n", wine_dbgstr_rect(lprcStateIcon));
2428 else Icon.right = 0;
2430 /************************************************************/
2431 /* compute LABEL bounding box (ala LVM_GETITEMRECT) */
2432 /************************************************************/
2433 if (doLabel)
2435 /* calculate how far to the right can the label stretch */
2436 Label.right = Box.right;
2437 if (infoPtr->uView == LV_VIEW_DETAILS)
2439 if (lpLVItem->iSubItem == 0)
2441 /* we need a zero based rect here */
2442 Label = lpColumnInfo->rcHeader;
2443 OffsetRect(&Label, -Label.left, 0);
2447 if (lpLVItem->iSubItem || ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && infoPtr->uView == LV_VIEW_DETAILS))
2449 labelSize.cx = infoPtr->nItemWidth;
2450 labelSize.cy = infoPtr->nItemHeight;
2451 goto calc_label;
2454 /* we need the text in non owner draw mode */
2455 assert(lpLVItem->mask & LVIF_TEXT);
2456 if (is_text(lpLVItem->pszText))
2458 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
2459 HDC hdc = GetDC(infoPtr->hwndSelf);
2460 HFONT hOldFont = SelectObject(hdc, hFont);
2461 UINT uFormat;
2462 RECT rcText;
2464 /* compute rough rectangle where the label will go */
2465 SetRectEmpty(&rcText);
2466 rcText.right = infoPtr->nItemWidth - TRAILING_LABEL_PADDING;
2467 rcText.bottom = infoPtr->nItemHeight;
2468 if (infoPtr->uView == LV_VIEW_ICON)
2469 rcText.bottom -= ICON_TOP_PADDING + infoPtr->iconSize.cy + ICON_BOTTOM_PADDING;
2471 /* now figure out the flags */
2472 if (infoPtr->uView == LV_VIEW_ICON)
2473 uFormat = oversizedBox ? LV_FL_DT_FLAGS : LV_ML_DT_FLAGS;
2474 else
2475 uFormat = LV_SL_DT_FLAGS;
2477 DrawTextW (hdc, lpLVItem->pszText, -1, &rcText, uFormat | DT_CALCRECT);
2479 if (rcText.right != rcText.left)
2480 labelSize.cx = min(rcText.right - rcText.left + TRAILING_LABEL_PADDING, infoPtr->nItemWidth);
2482 labelSize.cy = rcText.bottom - rcText.top;
2484 SelectObject(hdc, hOldFont);
2485 ReleaseDC(infoPtr->hwndSelf, hdc);
2488 calc_label:
2489 if (infoPtr->uView == LV_VIEW_ICON)
2491 Label.left = Box.left + (infoPtr->nItemWidth - labelSize.cx) / 2;
2492 Label.top = Box.top + ICON_TOP_PADDING_HITABLE +
2493 infoPtr->iconSize.cy + ICON_BOTTOM_PADDING;
2494 Label.right = Label.left + labelSize.cx;
2495 Label.bottom = Label.top + infoPtr->nItemHeight;
2496 if (!oversizedBox && labelSize.cy > infoPtr->ntmHeight)
2498 labelSize.cy = min(Box.bottom - Label.top, labelSize.cy);
2499 labelSize.cy /= infoPtr->ntmHeight;
2500 labelSize.cy = max(labelSize.cy, 1);
2501 labelSize.cy *= infoPtr->ntmHeight;
2503 Label.bottom = Label.top + labelSize.cy + HEIGHT_PADDING;
2505 else if (infoPtr->uView == LV_VIEW_DETAILS)
2507 Label.left = Icon.right;
2508 Label.top = Box.top;
2509 Label.right = lpLVItem->iSubItem ? lpColumnInfo->rcHeader.right :
2510 lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left;
2511 Label.bottom = Label.top + infoPtr->nItemHeight;
2513 else /* LV_VIEW_SMALLICON or LV_VIEW_LIST */
2515 Label.left = Icon.right;
2516 Label.top = Box.top;
2517 Label.right = min(Label.left + labelSize.cx, Label.right);
2518 Label.bottom = Label.top + infoPtr->nItemHeight;
2521 if (lprcLabel) *lprcLabel = Label;
2522 TRACE(" - label=%s\n", wine_dbgstr_rect(&Label));
2525 /************************************************************/
2526 /* compute SELECT bounding box */
2527 /************************************************************/
2528 if (doSelectBox)
2530 if (infoPtr->uView == LV_VIEW_DETAILS)
2532 SelectBox.left = Icon.left;
2533 SelectBox.top = Box.top;
2534 SelectBox.bottom = Box.bottom;
2536 if (labelSize.cx)
2537 SelectBox.right = min(Label.left + labelSize.cx, Label.right);
2538 else
2539 SelectBox.right = min(Label.left + MAX_EMPTYTEXT_SELECT_WIDTH, Label.right);
2541 else
2543 UnionRect(&SelectBox, &Icon, &Label);
2545 if (lprcSelectBox) *lprcSelectBox = SelectBox;
2546 TRACE(" - select box=%s\n", wine_dbgstr_rect(&SelectBox));
2549 /* Fix the Box if necessary */
2550 if (lprcBox)
2552 if (oversizedBox) UnionRect(lprcBox, &Box, &Label);
2553 else *lprcBox = Box;
2555 TRACE(" - box=%s\n", wine_dbgstr_rect(&Box));
2558 /***
2559 * DESCRIPTION: [INTERNAL]
2561 * PARAMETER(S):
2562 * [I] infoPtr : valid pointer to the listview structure
2563 * [I] nItem : item number
2564 * [O] lprcBox : ptr to Box rectangle
2566 * RETURN:
2567 * None.
2569 static void LISTVIEW_GetItemBox(const LISTVIEW_INFO *infoPtr, INT nItem, LPRECT lprcBox)
2571 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
2572 POINT Position, Origin;
2573 LVITEMW lvItem;
2575 LISTVIEW_GetOrigin(infoPtr, &Origin);
2576 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
2578 /* Be smart and try to figure out the minimum we have to do */
2579 lvItem.mask = 0;
2580 if (infoPtr->uView == LV_VIEW_ICON && infoPtr->bFocus && LISTVIEW_GetItemState(infoPtr, nItem, LVIS_FOCUSED))
2581 lvItem.mask |= LVIF_TEXT;
2582 lvItem.iItem = nItem;
2583 lvItem.iSubItem = 0;
2584 lvItem.pszText = szDispText;
2585 lvItem.cchTextMax = DISP_TEXT_SIZE;
2586 if (lvItem.mask) LISTVIEW_GetItemW(infoPtr, &lvItem);
2587 if (infoPtr->uView == LV_VIEW_ICON)
2589 lvItem.mask |= LVIF_STATE;
2590 lvItem.stateMask = LVIS_FOCUSED;
2591 lvItem.state = (lvItem.mask & LVIF_TEXT ? LVIS_FOCUSED : 0);
2593 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, lprcBox, 0, 0, 0, 0);
2595 if (infoPtr->uView == LV_VIEW_DETAILS && infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT &&
2596 SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, 0, 0))
2598 OffsetRect(lprcBox, Origin.x, Position.y + Origin.y);
2600 else
2601 OffsetRect(lprcBox, Position.x + Origin.x, Position.y + Origin.y);
2604 /* LISTVIEW_MapIdToIndex helper */
2605 static INT CALLBACK MapIdSearchCompare(LPVOID p1, LPVOID p2, LPARAM lParam)
2607 ITEM_ID *id1 = (ITEM_ID*)p1;
2608 ITEM_ID *id2 = (ITEM_ID*)p2;
2610 if (id1->id == id2->id) return 0;
2612 return (id1->id < id2->id) ? -1 : 1;
2615 /***
2616 * DESCRIPTION:
2617 * Returns the item index for id specified.
2619 * PARAMETER(S):
2620 * [I] infoPtr : valid pointer to the listview structure
2621 * [I] iID : item id to get index for
2623 * RETURN:
2624 * Item index, or -1 on failure.
2626 static INT LISTVIEW_MapIdToIndex(const LISTVIEW_INFO *infoPtr, UINT iID)
2628 ITEM_ID ID;
2629 INT index;
2631 TRACE("iID=%d\n", iID);
2633 if (infoPtr->dwStyle & LVS_OWNERDATA) return -1;
2634 if (infoPtr->nItemCount == 0) return -1;
2636 ID.id = iID;
2637 index = DPA_Search(infoPtr->hdpaItemIds, &ID, -1, MapIdSearchCompare, 0, DPAS_SORTED);
2639 if (index != -1)
2641 ITEM_ID *lpID = DPA_GetPtr(infoPtr->hdpaItemIds, index);
2642 return DPA_GetPtrIndex(infoPtr->hdpaItems, lpID->item);
2645 return -1;
2648 /***
2649 * DESCRIPTION:
2650 * Returns the item id for index given.
2652 * PARAMETER(S):
2653 * [I] infoPtr : valid pointer to the listview structure
2654 * [I] iItem : item index to get id for
2656 * RETURN:
2657 * Item id.
2659 static DWORD LISTVIEW_MapIndexToId(const LISTVIEW_INFO *infoPtr, INT iItem)
2661 ITEM_INFO *lpItem;
2662 HDPA hdpaSubItems;
2664 TRACE("iItem=%d\n", iItem);
2666 if (infoPtr->dwStyle & LVS_OWNERDATA) return -1;
2667 if (iItem < 0 || iItem >= infoPtr->nItemCount) return -1;
2669 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, iItem);
2670 lpItem = DPA_GetPtr(hdpaSubItems, 0);
2672 return lpItem->id->id;
2675 /***
2676 * DESCRIPTION:
2677 * Returns the current icon position, and advances it along the top.
2678 * The returned position is not offset by Origin.
2680 * PARAMETER(S):
2681 * [I] infoPtr : valid pointer to the listview structure
2682 * [O] lpPos : will get the current icon position
2684 * RETURN:
2685 * None
2687 static void LISTVIEW_NextIconPosTop(LISTVIEW_INFO *infoPtr, LPPOINT lpPos)
2689 INT nListWidth = infoPtr->rcList.right - infoPtr->rcList.left;
2691 *lpPos = infoPtr->currIconPos;
2693 infoPtr->currIconPos.x += infoPtr->nItemWidth;
2694 if (infoPtr->currIconPos.x + infoPtr->nItemWidth <= nListWidth) return;
2696 infoPtr->currIconPos.x = 0;
2697 infoPtr->currIconPos.y += infoPtr->nItemHeight;
2701 /***
2702 * DESCRIPTION:
2703 * Returns the current icon position, and advances it down the left edge.
2704 * The returned position is not offset by Origin.
2706 * PARAMETER(S):
2707 * [I] infoPtr : valid pointer to the listview structure
2708 * [O] lpPos : will get the current icon position
2710 * RETURN:
2711 * None
2713 static void LISTVIEW_NextIconPosLeft(LISTVIEW_INFO *infoPtr, LPPOINT lpPos)
2715 INT nListHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
2717 *lpPos = infoPtr->currIconPos;
2719 infoPtr->currIconPos.y += infoPtr->nItemHeight;
2720 if (infoPtr->currIconPos.y + infoPtr->nItemHeight <= nListHeight) return;
2722 infoPtr->currIconPos.x += infoPtr->nItemWidth;
2723 infoPtr->currIconPos.y = 0;
2727 /***
2728 * DESCRIPTION:
2729 * Moves an icon to the specified position.
2730 * It takes care of invalidating the item, etc.
2732 * PARAMETER(S):
2733 * [I] infoPtr : valid pointer to the listview structure
2734 * [I] nItem : the item to move
2735 * [I] lpPos : the new icon position
2736 * [I] isNew : flags the item as being new
2738 * RETURN:
2739 * Success: TRUE
2740 * Failure: FALSE
2742 static BOOL LISTVIEW_MoveIconTo(const LISTVIEW_INFO *infoPtr, INT nItem, const POINT *lppt, BOOL isNew)
2744 POINT old;
2746 if (!isNew)
2748 old.x = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosX, nItem);
2749 old.y = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosY, nItem);
2751 if (lppt->x == old.x && lppt->y == old.y) return TRUE;
2752 LISTVIEW_InvalidateItem(infoPtr, nItem);
2755 /* Allocating a POINTER for every item is too resource intensive,
2756 * so we'll keep the (x,y) in different arrays */
2757 if (!DPA_SetPtr(infoPtr->hdpaPosX, nItem, (void *)(LONG_PTR)lppt->x)) return FALSE;
2758 if (!DPA_SetPtr(infoPtr->hdpaPosY, nItem, (void *)(LONG_PTR)lppt->y)) return FALSE;
2760 LISTVIEW_InvalidateItem(infoPtr, nItem);
2762 return TRUE;
2765 /***
2766 * DESCRIPTION:
2767 * Arranges listview items in icon display mode.
2769 * PARAMETER(S):
2770 * [I] infoPtr : valid pointer to the listview structure
2771 * [I] nAlignCode : alignment code
2773 * RETURN:
2774 * SUCCESS : TRUE
2775 * FAILURE : FALSE
2777 static BOOL LISTVIEW_Arrange(LISTVIEW_INFO *infoPtr, INT nAlignCode)
2779 void (*next_pos)(LISTVIEW_INFO *, LPPOINT);
2780 POINT pos;
2781 INT i;
2783 if (infoPtr->uView != LV_VIEW_ICON && infoPtr->uView != LV_VIEW_SMALLICON) return FALSE;
2785 TRACE("nAlignCode=%d\n", nAlignCode);
2787 if (nAlignCode == LVA_DEFAULT)
2789 if (infoPtr->dwStyle & LVS_ALIGNLEFT) nAlignCode = LVA_ALIGNLEFT;
2790 else nAlignCode = LVA_ALIGNTOP;
2793 switch (nAlignCode)
2795 case LVA_ALIGNLEFT: next_pos = LISTVIEW_NextIconPosLeft; break;
2796 case LVA_ALIGNTOP: next_pos = LISTVIEW_NextIconPosTop; break;
2797 case LVA_SNAPTOGRID: next_pos = LISTVIEW_NextIconPosTop; break; /* FIXME */
2798 default: return FALSE;
2801 infoPtr->currIconPos.x = infoPtr->currIconPos.y = 0;
2802 for (i = 0; i < infoPtr->nItemCount; i++)
2804 next_pos(infoPtr, &pos);
2805 LISTVIEW_MoveIconTo(infoPtr, i, &pos, FALSE);
2808 return TRUE;
2811 /***
2812 * DESCRIPTION:
2813 * Retrieves the bounding rectangle of all the items, not offset by Origin.
2814 * For LVS_REPORT always returns empty rectangle.
2816 * PARAMETER(S):
2817 * [I] infoPtr : valid pointer to the listview structure
2818 * [O] lprcView : bounding rectangle
2820 * RETURN:
2821 * SUCCESS : TRUE
2822 * FAILURE : FALSE
2824 static void LISTVIEW_GetAreaRect(const LISTVIEW_INFO *infoPtr, LPRECT lprcView)
2826 INT i, x, y;
2828 SetRectEmpty(lprcView);
2830 switch (infoPtr->uView)
2832 case LV_VIEW_ICON:
2833 case LV_VIEW_SMALLICON:
2834 for (i = 0; i < infoPtr->nItemCount; i++)
2836 x = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosX, i);
2837 y = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosY, i);
2838 lprcView->right = max(lprcView->right, x);
2839 lprcView->bottom = max(lprcView->bottom, y);
2841 if (infoPtr->nItemCount > 0)
2843 lprcView->right += infoPtr->nItemWidth;
2844 lprcView->bottom += infoPtr->nItemHeight;
2846 break;
2848 case LV_VIEW_LIST:
2849 y = LISTVIEW_GetCountPerColumn(infoPtr);
2850 x = infoPtr->nItemCount / y;
2851 if (infoPtr->nItemCount % y) x++;
2852 lprcView->right = x * infoPtr->nItemWidth;
2853 lprcView->bottom = y * infoPtr->nItemHeight;
2854 break;
2858 /***
2859 * DESCRIPTION:
2860 * Retrieves the bounding rectangle of all the items.
2862 * PARAMETER(S):
2863 * [I] infoPtr : valid pointer to the listview structure
2864 * [O] lprcView : bounding rectangle
2866 * RETURN:
2867 * SUCCESS : TRUE
2868 * FAILURE : FALSE
2870 static BOOL LISTVIEW_GetViewRect(const LISTVIEW_INFO *infoPtr, LPRECT lprcView)
2872 POINT ptOrigin;
2874 TRACE("(lprcView=%p)\n", lprcView);
2876 if (!lprcView) return FALSE;
2878 LISTVIEW_GetAreaRect(infoPtr, lprcView);
2880 if (infoPtr->uView != LV_VIEW_DETAILS)
2882 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
2883 OffsetRect(lprcView, ptOrigin.x, ptOrigin.y);
2886 TRACE("lprcView=%s\n", wine_dbgstr_rect(lprcView));
2888 return TRUE;
2891 /***
2892 * DESCRIPTION:
2893 * Retrieves the subitem pointer associated with the subitem index.
2895 * PARAMETER(S):
2896 * [I] hdpaSubItems : DPA handle for a specific item
2897 * [I] nSubItem : index of subitem
2899 * RETURN:
2900 * SUCCESS : subitem pointer
2901 * FAILURE : NULL
2903 static SUBITEM_INFO* LISTVIEW_GetSubItemPtr(HDPA hdpaSubItems, INT nSubItem)
2905 SUBITEM_INFO *lpSubItem;
2906 INT i;
2908 /* we should binary search here if need be */
2909 for (i = 1; i < DPA_GetPtrCount(hdpaSubItems); i++)
2911 lpSubItem = DPA_GetPtr(hdpaSubItems, i);
2912 if (lpSubItem->iSubItem == nSubItem)
2913 return lpSubItem;
2916 return NULL;
2920 /***
2921 * DESCRIPTION:
2922 * Calculates the desired item width.
2924 * PARAMETER(S):
2925 * [I] infoPtr : valid pointer to the listview structure
2927 * RETURN:
2928 * The desired item width.
2930 static INT LISTVIEW_CalculateItemWidth(const LISTVIEW_INFO *infoPtr)
2932 INT nItemWidth = 0;
2934 TRACE("view %ld\n", infoPtr->uView);
2936 if (infoPtr->uView == LV_VIEW_ICON)
2937 nItemWidth = infoPtr->iconSpacing.cx;
2938 else if (infoPtr->uView == LV_VIEW_DETAILS)
2940 if (DPA_GetPtrCount(infoPtr->hdpaColumns) > 0)
2942 RECT rcHeader;
2943 INT index;
2945 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX,
2946 DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, 0);
2948 LISTVIEW_GetHeaderRect(infoPtr, index, &rcHeader);
2949 nItemWidth = rcHeader.right;
2952 else /* LV_VIEW_SMALLICON, or LV_VIEW_LIST */
2954 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
2955 LVITEMW lvItem;
2956 INT i;
2958 lvItem.mask = LVIF_TEXT;
2959 lvItem.iSubItem = 0;
2961 for (i = 0; i < infoPtr->nItemCount; i++)
2963 lvItem.iItem = i;
2964 lvItem.pszText = szDispText;
2965 lvItem.cchTextMax = DISP_TEXT_SIZE;
2966 if (LISTVIEW_GetItemW(infoPtr, &lvItem))
2967 nItemWidth = max(LISTVIEW_GetStringWidthT(infoPtr, lvItem.pszText, TRUE),
2968 nItemWidth);
2971 if (infoPtr->himlSmall) nItemWidth += infoPtr->iconSize.cx;
2972 if (infoPtr->himlState) nItemWidth += infoPtr->iconStateSize.cx;
2974 nItemWidth = max(DEFAULT_COLUMN_WIDTH, nItemWidth + WIDTH_PADDING);
2977 return nItemWidth;
2980 /***
2981 * DESCRIPTION:
2982 * Calculates the desired item height.
2984 * PARAMETER(S):
2985 * [I] infoPtr : valid pointer to the listview structure
2987 * RETURN:
2988 * The desired item height.
2990 static INT LISTVIEW_CalculateItemHeight(const LISTVIEW_INFO *infoPtr)
2992 INT nItemHeight;
2994 TRACE("view %ld\n", infoPtr->uView);
2996 if (infoPtr->uView == LV_VIEW_ICON)
2997 nItemHeight = infoPtr->iconSpacing.cy;
2998 else
3000 nItemHeight = infoPtr->ntmHeight;
3001 if (infoPtr->himlState)
3002 nItemHeight = max(nItemHeight, infoPtr->iconStateSize.cy);
3003 if (infoPtr->himlSmall)
3004 nItemHeight = max(nItemHeight, infoPtr->iconSize.cy);
3005 nItemHeight += HEIGHT_PADDING;
3006 if (infoPtr->nMeasureItemHeight > 0)
3007 nItemHeight = infoPtr->nMeasureItemHeight;
3010 return max(nItemHeight, 1);
3013 /***
3014 * DESCRIPTION:
3015 * Updates the width, and height of an item.
3017 * PARAMETER(S):
3018 * [I] infoPtr : valid pointer to the listview structure
3020 * RETURN:
3021 * None.
3023 static inline void LISTVIEW_UpdateItemSize(LISTVIEW_INFO *infoPtr)
3025 infoPtr->nItemWidth = LISTVIEW_CalculateItemWidth(infoPtr);
3026 infoPtr->nItemHeight = LISTVIEW_CalculateItemHeight(infoPtr);
3030 /***
3031 * DESCRIPTION:
3032 * Retrieves and saves important text metrics info for the current
3033 * Listview font.
3035 * PARAMETER(S):
3036 * [I] infoPtr : valid pointer to the listview structure
3039 static void LISTVIEW_SaveTextMetrics(LISTVIEW_INFO *infoPtr)
3041 HDC hdc = GetDC(infoPtr->hwndSelf);
3042 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
3043 HFONT hOldFont = SelectObject(hdc, hFont);
3044 TEXTMETRICW tm;
3045 SIZE sz;
3047 if (GetTextMetricsW(hdc, &tm))
3049 infoPtr->ntmHeight = tm.tmHeight;
3050 infoPtr->ntmMaxCharWidth = tm.tmMaxCharWidth;
3053 if (GetTextExtentPoint32A(hdc, "...", 3, &sz))
3054 infoPtr->nEllipsisWidth = sz.cx;
3056 SelectObject(hdc, hOldFont);
3057 ReleaseDC(infoPtr->hwndSelf, hdc);
3059 TRACE("tmHeight=%d\n", infoPtr->ntmHeight);
3062 /***
3063 * DESCRIPTION:
3064 * A compare function for ranges
3066 * PARAMETER(S)
3067 * [I] range1 : pointer to range 1;
3068 * [I] range2 : pointer to range 2;
3069 * [I] flags : flags
3071 * RETURNS:
3072 * > 0 : if range 1 > range 2
3073 * < 0 : if range 2 > range 1
3074 * = 0 : if range intersects range 2
3076 static INT CALLBACK ranges_cmp(LPVOID range1, LPVOID range2, LPARAM flags)
3078 INT cmp;
3080 if (((RANGE*)range1)->upper <= ((RANGE*)range2)->lower)
3081 cmp = -1;
3082 else if (((RANGE*)range2)->upper <= ((RANGE*)range1)->lower)
3083 cmp = 1;
3084 else
3085 cmp = 0;
3087 TRACE("range1=%s, range2=%s, cmp=%d\n", debugrange(range1), debugrange(range2), cmp);
3089 return cmp;
3092 #define ranges_check(ranges, desc) if (TRACE_ON(listview)) ranges_assert(ranges, desc, __FILE__, __LINE__)
3094 static void ranges_assert(RANGES ranges, LPCSTR desc, const char *file, int line)
3096 INT i;
3097 RANGE *prev, *curr;
3099 TRACE("*** Checking %s:%d:%s ***\n", file, line, desc);
3100 assert (ranges);
3101 assert (DPA_GetPtrCount(ranges->hdpa) >= 0);
3102 ranges_dump(ranges);
3103 if (DPA_GetPtrCount(ranges->hdpa) > 0)
3105 prev = DPA_GetPtr(ranges->hdpa, 0);
3106 assert (prev->lower >= 0 && prev->lower < prev->upper);
3107 for (i = 1; i < DPA_GetPtrCount(ranges->hdpa); i++)
3109 curr = DPA_GetPtr(ranges->hdpa, i);
3110 assert (prev->upper <= curr->lower);
3111 assert (curr->lower < curr->upper);
3112 prev = curr;
3115 TRACE("--- Done checking---\n");
3118 static RANGES ranges_create(int count)
3120 RANGES ranges = Alloc(sizeof(struct tagRANGES));
3121 if (!ranges) return NULL;
3122 ranges->hdpa = DPA_Create(count);
3123 if (ranges->hdpa) return ranges;
3124 Free(ranges);
3125 return NULL;
3128 static void ranges_clear(RANGES ranges)
3130 INT i;
3132 for(i = 0; i < DPA_GetPtrCount(ranges->hdpa); i++)
3133 Free(DPA_GetPtr(ranges->hdpa, i));
3134 DPA_DeleteAllPtrs(ranges->hdpa);
3138 static void ranges_destroy(RANGES ranges)
3140 if (!ranges) return;
3141 ranges_clear(ranges);
3142 DPA_Destroy(ranges->hdpa);
3143 Free(ranges);
3146 static RANGES ranges_clone(RANGES ranges)
3148 RANGES clone;
3149 INT i;
3151 if (!(clone = ranges_create(DPA_GetPtrCount(ranges->hdpa)))) goto fail;
3153 for (i = 0; i < DPA_GetPtrCount(ranges->hdpa); i++)
3155 RANGE *newrng = Alloc(sizeof(RANGE));
3156 if (!newrng) goto fail;
3157 *newrng = *((RANGE*)DPA_GetPtr(ranges->hdpa, i));
3158 if (!DPA_SetPtr(clone->hdpa, i, newrng))
3160 Free(newrng);
3161 goto fail;
3164 return clone;
3166 fail:
3167 TRACE ("clone failed\n");
3168 ranges_destroy(clone);
3169 return NULL;
3172 static RANGES ranges_diff(RANGES ranges, RANGES sub)
3174 INT i;
3176 for (i = 0; i < DPA_GetPtrCount(sub->hdpa); i++)
3177 ranges_del(ranges, *((RANGE *)DPA_GetPtr(sub->hdpa, i)));
3179 return ranges;
3182 static void ranges_dump(RANGES ranges)
3184 INT i;
3186 for (i = 0; i < DPA_GetPtrCount(ranges->hdpa); i++)
3187 TRACE(" %s\n", debugrange(DPA_GetPtr(ranges->hdpa, i)));
3190 static inline BOOL ranges_contain(RANGES ranges, INT nItem)
3192 RANGE srchrng = { nItem, nItem + 1 };
3194 TRACE("(nItem=%d)\n", nItem);
3195 ranges_check(ranges, "before contain");
3196 return DPA_Search(ranges->hdpa, &srchrng, 0, ranges_cmp, 0, DPAS_SORTED) != -1;
3199 static INT ranges_itemcount(RANGES ranges)
3201 INT i, count = 0;
3203 for (i = 0; i < DPA_GetPtrCount(ranges->hdpa); i++)
3205 RANGE *sel = DPA_GetPtr(ranges->hdpa, i);
3206 count += sel->upper - sel->lower;
3209 return count;
3212 static BOOL ranges_shift(RANGES ranges, INT nItem, INT delta, INT nUpper)
3214 RANGE srchrng = { nItem, nItem + 1 }, *chkrng;
3215 INT index;
3217 index = DPA_Search(ranges->hdpa, &srchrng, 0, ranges_cmp, 0, DPAS_SORTED | DPAS_INSERTAFTER);
3218 if (index == -1) return TRUE;
3220 for (; index < DPA_GetPtrCount(ranges->hdpa); index++)
3222 chkrng = DPA_GetPtr(ranges->hdpa, index);
3223 if (chkrng->lower >= nItem)
3224 chkrng->lower = max(min(chkrng->lower + delta, nUpper - 1), 0);
3225 if (chkrng->upper > nItem)
3226 chkrng->upper = max(min(chkrng->upper + delta, nUpper), 0);
3228 return TRUE;
3231 static BOOL ranges_add(RANGES ranges, RANGE range)
3233 RANGE srchrgn;
3234 INT index;
3236 TRACE("(%s)\n", debugrange(&range));
3237 ranges_check(ranges, "before add");
3239 /* try find overlapping regions first */
3240 srchrgn.lower = range.lower - 1;
3241 srchrgn.upper = range.upper + 1;
3242 index = DPA_Search(ranges->hdpa, &srchrgn, 0, ranges_cmp, 0, DPAS_SORTED);
3244 if (index == -1)
3246 RANGE *newrgn;
3248 TRACE("Adding new range\n");
3250 /* create the brand new range to insert */
3251 newrgn = Alloc(sizeof(RANGE));
3252 if(!newrgn) goto fail;
3253 *newrgn = range;
3255 /* figure out where to insert it */
3256 index = DPA_Search(ranges->hdpa, newrgn, 0, ranges_cmp, 0, DPAS_SORTED | DPAS_INSERTAFTER);
3257 TRACE("index=%d\n", index);
3258 if (index == -1) index = 0;
3260 /* and get it over with */
3261 if (DPA_InsertPtr(ranges->hdpa, index, newrgn) == -1)
3263 Free(newrgn);
3264 goto fail;
3267 else
3269 RANGE *chkrgn, *mrgrgn;
3270 INT fromindex, mergeindex;
3272 chkrgn = DPA_GetPtr(ranges->hdpa, index);
3273 TRACE("Merge with %s @%d\n", debugrange(chkrgn), index);
3275 chkrgn->lower = min(range.lower, chkrgn->lower);
3276 chkrgn->upper = max(range.upper, chkrgn->upper);
3278 TRACE("New range %s @%d\n", debugrange(chkrgn), index);
3280 /* merge now common ranges */
3281 fromindex = 0;
3282 srchrgn.lower = chkrgn->lower - 1;
3283 srchrgn.upper = chkrgn->upper + 1;
3287 mergeindex = DPA_Search(ranges->hdpa, &srchrgn, fromindex, ranges_cmp, 0, 0);
3288 if (mergeindex == -1) break;
3289 if (mergeindex == index)
3291 fromindex = index + 1;
3292 continue;
3295 TRACE("Merge with index %i\n", mergeindex);
3297 mrgrgn = DPA_GetPtr(ranges->hdpa, mergeindex);
3298 chkrgn->lower = min(chkrgn->lower, mrgrgn->lower);
3299 chkrgn->upper = max(chkrgn->upper, mrgrgn->upper);
3300 Free(mrgrgn);
3301 DPA_DeletePtr(ranges->hdpa, mergeindex);
3302 if (mergeindex < index) index --;
3303 } while(1);
3306 ranges_check(ranges, "after add");
3307 return TRUE;
3309 fail:
3310 ranges_check(ranges, "failed add");
3311 return FALSE;
3314 static BOOL ranges_del(RANGES ranges, RANGE range)
3316 RANGE *chkrgn;
3317 INT index;
3319 TRACE("(%s)\n", debugrange(&range));
3320 ranges_check(ranges, "before del");
3322 /* we don't use DPAS_SORTED here, since we need *
3323 * to find the first overlapping range */
3324 index = DPA_Search(ranges->hdpa, &range, 0, ranges_cmp, 0, 0);
3325 while(index != -1)
3327 chkrgn = DPA_GetPtr(ranges->hdpa, index);
3329 TRACE("Matches range %s @%d\n", debugrange(chkrgn), index);
3331 /* case 1: Same range */
3332 if ( (chkrgn->upper == range.upper) &&
3333 (chkrgn->lower == range.lower) )
3335 DPA_DeletePtr(ranges->hdpa, index);
3336 Free(chkrgn);
3337 break;
3339 /* case 2: engulf */
3340 else if ( (chkrgn->upper <= range.upper) &&
3341 (chkrgn->lower >= range.lower) )
3343 DPA_DeletePtr(ranges->hdpa, index);
3344 Free(chkrgn);
3346 /* case 3: overlap upper */
3347 else if ( (chkrgn->upper <= range.upper) &&
3348 (chkrgn->lower < range.lower) )
3350 chkrgn->upper = range.lower;
3352 /* case 4: overlap lower */
3353 else if ( (chkrgn->upper > range.upper) &&
3354 (chkrgn->lower >= range.lower) )
3356 chkrgn->lower = range.upper;
3357 break;
3359 /* case 5: fully internal */
3360 else
3362 RANGE *newrgn;
3364 if (!(newrgn = Alloc(sizeof(RANGE)))) goto fail;
3365 newrgn->lower = chkrgn->lower;
3366 newrgn->upper = range.lower;
3367 chkrgn->lower = range.upper;
3368 if (DPA_InsertPtr(ranges->hdpa, index, newrgn) == -1)
3370 Free(newrgn);
3371 goto fail;
3373 break;
3376 index = DPA_Search(ranges->hdpa, &range, index, ranges_cmp, 0, 0);
3379 ranges_check(ranges, "after del");
3380 return TRUE;
3382 fail:
3383 ranges_check(ranges, "failed del");
3384 return FALSE;
3387 /***
3388 * DESCRIPTION:
3389 * Removes all selection ranges
3391 * Parameters(s):
3392 * [I] infoPtr : valid pointer to the listview structure
3393 * [I] toSkip : item range to skip removing the selection
3395 * RETURNS:
3396 * SUCCESS : TRUE
3397 * FAILURE : FALSE
3399 static BOOL LISTVIEW_DeselectAllSkipItems(LISTVIEW_INFO *infoPtr, RANGES toSkip)
3401 LVITEMW lvItem;
3402 ITERATOR i;
3403 RANGES clone;
3405 TRACE("()\n");
3407 lvItem.state = 0;
3408 lvItem.stateMask = LVIS_SELECTED;
3410 /* need to clone the DPA because callbacks can change it */
3411 if (!(clone = ranges_clone(infoPtr->selectionRanges))) return FALSE;
3412 iterator_rangesitems(&i, ranges_diff(clone, toSkip));
3413 while(iterator_next(&i))
3414 LISTVIEW_SetItemState(infoPtr, i.nItem, &lvItem);
3415 /* note that the iterator destructor will free the cloned range */
3416 iterator_destroy(&i);
3418 return TRUE;
3421 static inline BOOL LISTVIEW_DeselectAllSkipItem(LISTVIEW_INFO *infoPtr, INT nItem)
3423 RANGES toSkip;
3425 if (!(toSkip = ranges_create(1))) return FALSE;
3426 if (nItem != -1) ranges_additem(toSkip, nItem);
3427 LISTVIEW_DeselectAllSkipItems(infoPtr, toSkip);
3428 ranges_destroy(toSkip);
3429 return TRUE;
3432 static inline BOOL LISTVIEW_DeselectAll(LISTVIEW_INFO *infoPtr)
3434 return LISTVIEW_DeselectAllSkipItem(infoPtr, -1);
3437 /***
3438 * DESCRIPTION:
3439 * Retrieves the number of items that are marked as selected.
3441 * PARAMETER(S):
3442 * [I] infoPtr : valid pointer to the listview structure
3444 * RETURN:
3445 * Number of items selected.
3447 static INT LISTVIEW_GetSelectedCount(const LISTVIEW_INFO *infoPtr)
3449 INT nSelectedCount = 0;
3451 if (infoPtr->uCallbackMask & LVIS_SELECTED)
3453 INT i;
3454 for (i = 0; i < infoPtr->nItemCount; i++)
3456 if (LISTVIEW_GetItemState(infoPtr, i, LVIS_SELECTED))
3457 nSelectedCount++;
3460 else
3461 nSelectedCount = ranges_itemcount(infoPtr->selectionRanges);
3463 TRACE("nSelectedCount=%d\n", nSelectedCount);
3464 return nSelectedCount;
3467 /***
3468 * DESCRIPTION:
3469 * Manages the item focus.
3471 * PARAMETER(S):
3472 * [I] infoPtr : valid pointer to the listview structure
3473 * [I] nItem : item index
3475 * RETURN:
3476 * TRUE : focused item changed
3477 * FALSE : focused item has NOT changed
3479 static inline BOOL LISTVIEW_SetItemFocus(LISTVIEW_INFO *infoPtr, INT nItem)
3481 INT oldFocus = infoPtr->nFocusedItem;
3482 LVITEMW lvItem;
3484 if (nItem == infoPtr->nFocusedItem) return FALSE;
3486 lvItem.state = nItem == -1 ? 0 : LVIS_FOCUSED;
3487 lvItem.stateMask = LVIS_FOCUSED;
3488 LISTVIEW_SetItemState(infoPtr, nItem == -1 ? infoPtr->nFocusedItem : nItem, &lvItem);
3490 return oldFocus != infoPtr->nFocusedItem;
3493 static INT shift_item(const LISTVIEW_INFO *infoPtr, INT nShiftItem, INT nItem, INT direction)
3495 if (nShiftItem < nItem) return nShiftItem;
3497 if (nShiftItem > nItem) return nShiftItem + direction;
3499 if (direction > 0) return nShiftItem + direction;
3501 return min(nShiftItem, infoPtr->nItemCount - 1);
3504 /* This function updates focus index.
3506 Parameters:
3507 focus : current focus index
3508 item : index of item to be added/removed
3509 direction : add/remove flag
3511 static void LISTVIEW_ShiftFocus(LISTVIEW_INFO *infoPtr, INT focus, INT item, INT direction)
3513 DWORD old_mask = infoPtr->notify_mask & NOTIFY_MASK_ITEM_CHANGE;
3515 infoPtr->notify_mask &= ~NOTIFY_MASK_ITEM_CHANGE;
3516 focus = shift_item(infoPtr, focus, item, direction);
3517 if (focus != infoPtr->nFocusedItem)
3518 LISTVIEW_SetItemFocus(infoPtr, focus);
3519 infoPtr->notify_mask |= old_mask;
3523 * DESCRIPTION:
3524 * Updates the various indices after an item has been inserted or deleted.
3526 * PARAMETER(S):
3527 * [I] infoPtr : valid pointer to the listview structure
3528 * [I] nItem : item index
3529 * [I] direction : Direction of shift, +1 or -1.
3531 * RETURN:
3532 * None
3534 static void LISTVIEW_ShiftIndices(LISTVIEW_INFO *infoPtr, INT nItem, INT direction)
3536 TRACE("Shifting %i, %i steps\n", nItem, direction);
3538 ranges_shift(infoPtr->selectionRanges, nItem, direction, infoPtr->nItemCount);
3539 assert(abs(direction) == 1);
3540 infoPtr->nSelectionMark = shift_item(infoPtr, infoPtr->nSelectionMark, nItem, direction);
3542 /* But we are not supposed to modify nHotItem! */
3546 * DESCRIPTION:
3547 * Adds a block of selections.
3549 * PARAMETER(S):
3550 * [I] infoPtr : valid pointer to the listview structure
3551 * [I] nItem : item index
3553 * RETURN:
3554 * Whether the window is still valid.
3556 static BOOL LISTVIEW_AddGroupSelection(LISTVIEW_INFO *infoPtr, INT nItem)
3558 INT nFirst = min(infoPtr->nSelectionMark, nItem);
3559 INT nLast = max(infoPtr->nSelectionMark, nItem);
3560 HWND hwndSelf = infoPtr->hwndSelf;
3561 NMLVODSTATECHANGE nmlv;
3562 DWORD old_mask;
3563 LVITEMW item;
3564 INT i;
3566 /* Temporarily disable change notification
3567 * If the control is LVS_OWNERDATA, we need to send
3568 * only one LVN_ODSTATECHANGED notification.
3569 * See MSDN documentation for LVN_ITEMCHANGED.
3571 old_mask = infoPtr->notify_mask & NOTIFY_MASK_ITEM_CHANGE;
3572 if (infoPtr->dwStyle & LVS_OWNERDATA)
3573 infoPtr->notify_mask &= ~NOTIFY_MASK_ITEM_CHANGE;
3575 if (nFirst == -1) nFirst = nItem;
3577 item.state = LVIS_SELECTED;
3578 item.stateMask = LVIS_SELECTED;
3580 for (i = nFirst; i <= nLast; i++)
3581 LISTVIEW_SetItemState(infoPtr,i,&item);
3583 ZeroMemory(&nmlv, sizeof(nmlv));
3584 nmlv.iFrom = nFirst;
3585 nmlv.iTo = nLast;
3586 nmlv.uOldState = 0;
3587 nmlv.uNewState = item.state;
3589 notify_hdr(infoPtr, LVN_ODSTATECHANGED, (LPNMHDR)&nmlv);
3590 if (!IsWindow(hwndSelf))
3591 return FALSE;
3592 infoPtr->notify_mask |= old_mask;
3593 return TRUE;
3597 /***
3598 * DESCRIPTION:
3599 * Sets a single group selection.
3601 * PARAMETER(S):
3602 * [I] infoPtr : valid pointer to the listview structure
3603 * [I] nItem : item index
3605 * RETURN:
3606 * None
3608 static void LISTVIEW_SetGroupSelection(LISTVIEW_INFO *infoPtr, INT nItem)
3610 RANGES selection;
3611 DWORD old_mask;
3612 LVITEMW item;
3613 ITERATOR i;
3615 if (!(selection = ranges_create(100))) return;
3617 item.state = LVIS_SELECTED;
3618 item.stateMask = LVIS_SELECTED;
3620 if ((infoPtr->uView == LV_VIEW_LIST) || (infoPtr->uView == LV_VIEW_DETAILS))
3622 if (infoPtr->nSelectionMark == -1)
3624 infoPtr->nSelectionMark = nItem;
3625 ranges_additem(selection, nItem);
3627 else
3629 RANGE sel;
3631 sel.lower = min(infoPtr->nSelectionMark, nItem);
3632 sel.upper = max(infoPtr->nSelectionMark, nItem) + 1;
3633 ranges_add(selection, sel);
3636 else
3638 RECT rcItem, rcSel, rcSelMark;
3639 POINT ptItem;
3641 rcItem.left = LVIR_BOUNDS;
3642 if (!LISTVIEW_GetItemRect(infoPtr, nItem, &rcItem)) {
3643 ranges_destroy (selection);
3644 return;
3646 rcSelMark.left = LVIR_BOUNDS;
3647 if (!LISTVIEW_GetItemRect(infoPtr, infoPtr->nSelectionMark, &rcSelMark)) {
3648 ranges_destroy (selection);
3649 return;
3651 UnionRect(&rcSel, &rcItem, &rcSelMark);
3652 iterator_frameditems(&i, infoPtr, &rcSel);
3653 while(iterator_next(&i))
3655 LISTVIEW_GetItemPosition(infoPtr, i.nItem, &ptItem);
3656 if (PtInRect(&rcSel, ptItem)) ranges_additem(selection, i.nItem);
3658 iterator_destroy(&i);
3661 /* disable per item notifications on LVS_OWNERDATA style
3662 FIXME: single LVN_ODSTATECHANGED should be used */
3663 old_mask = infoPtr->notify_mask & NOTIFY_MASK_ITEM_CHANGE;
3664 if (infoPtr->dwStyle & LVS_OWNERDATA)
3665 infoPtr->notify_mask &= ~NOTIFY_MASK_ITEM_CHANGE;
3667 LISTVIEW_DeselectAllSkipItems(infoPtr, selection);
3670 iterator_rangesitems(&i, selection);
3671 while(iterator_next(&i))
3672 LISTVIEW_SetItemState(infoPtr, i.nItem, &item);
3673 /* this will also destroy the selection */
3674 iterator_destroy(&i);
3676 infoPtr->notify_mask |= old_mask;
3677 LISTVIEW_SetItemFocus(infoPtr, nItem);
3680 /***
3681 * DESCRIPTION:
3682 * Sets a single selection.
3684 * PARAMETER(S):
3685 * [I] infoPtr : valid pointer to the listview structure
3686 * [I] nItem : item index
3688 * RETURN:
3689 * None
3691 static void LISTVIEW_SetSelection(LISTVIEW_INFO *infoPtr, INT nItem)
3693 LVITEMW lvItem;
3695 TRACE("nItem=%d\n", nItem);
3697 LISTVIEW_DeselectAllSkipItem(infoPtr, nItem);
3699 lvItem.state = LVIS_FOCUSED | LVIS_SELECTED;
3700 lvItem.stateMask = LVIS_FOCUSED | LVIS_SELECTED;
3701 LISTVIEW_SetItemState(infoPtr, nItem, &lvItem);
3703 infoPtr->nSelectionMark = nItem;
3706 /***
3707 * DESCRIPTION:
3708 * Set selection(s) with keyboard.
3710 * PARAMETER(S):
3711 * [I] infoPtr : valid pointer to the listview structure
3712 * [I] nItem : item index
3713 * [I] space : VK_SPACE code sent
3715 * RETURN:
3716 * SUCCESS : TRUE (needs to be repainted)
3717 * FAILURE : FALSE (nothing has changed)
3719 static BOOL LISTVIEW_KeySelection(LISTVIEW_INFO *infoPtr, INT nItem, BOOL space)
3721 /* FIXME: pass in the state */
3722 WORD wShift = GetKeyState(VK_SHIFT) & 0x8000;
3723 WORD wCtrl = GetKeyState(VK_CONTROL) & 0x8000;
3724 BOOL bResult = FALSE;
3726 TRACE("nItem=%d, wShift=%d, wCtrl=%d\n", nItem, wShift, wCtrl);
3727 if ((nItem >= 0) && (nItem < infoPtr->nItemCount))
3729 bResult = TRUE;
3731 if (infoPtr->dwStyle & LVS_SINGLESEL || (wShift == 0 && wCtrl == 0))
3732 LISTVIEW_SetSelection(infoPtr, nItem);
3733 else
3735 if (wShift)
3736 LISTVIEW_SetGroupSelection(infoPtr, nItem);
3737 else if (wCtrl)
3739 LVITEMW lvItem;
3740 lvItem.state = ~LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED);
3741 lvItem.stateMask = LVIS_SELECTED;
3742 if (space)
3744 LISTVIEW_SetItemState(infoPtr, nItem, &lvItem);
3745 if (lvItem.state & LVIS_SELECTED)
3746 infoPtr->nSelectionMark = nItem;
3748 bResult = LISTVIEW_SetItemFocus(infoPtr, nItem);
3751 LISTVIEW_EnsureVisible(infoPtr, nItem, FALSE);
3754 UpdateWindow(infoPtr->hwndSelf); /* update client area */
3755 return bResult;
3758 static BOOL LISTVIEW_GetItemAtPt(const LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem, POINT pt)
3760 LVHITTESTINFO lvHitTestInfo;
3762 ZeroMemory(&lvHitTestInfo, sizeof(lvHitTestInfo));
3763 lvHitTestInfo.pt.x = pt.x;
3764 lvHitTestInfo.pt.y = pt.y;
3766 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
3768 lpLVItem->mask = LVIF_PARAM;
3769 lpLVItem->iItem = lvHitTestInfo.iItem;
3770 lpLVItem->iSubItem = 0;
3772 return LISTVIEW_GetItemT(infoPtr, lpLVItem, TRUE);
3775 static inline BOOL LISTVIEW_IsHotTracking(const LISTVIEW_INFO *infoPtr)
3777 return ((infoPtr->dwLvExStyle & LVS_EX_TRACKSELECT) ||
3778 (infoPtr->dwLvExStyle & LVS_EX_ONECLICKACTIVATE) ||
3779 (infoPtr->dwLvExStyle & LVS_EX_TWOCLICKACTIVATE));
3782 /***
3783 * DESCRIPTION:
3784 * Called when the mouse is being actively tracked and has hovered for a specified
3785 * amount of time
3787 * PARAMETER(S):
3788 * [I] infoPtr : valid pointer to the listview structure
3789 * [I] fwKeys : key indicator
3790 * [I] x,y : mouse position
3792 * RETURN:
3793 * 0 if the message was processed, non-zero if there was an error
3795 * INFO:
3796 * LVS_EX_TRACKSELECT: An item is automatically selected when the cursor remains
3797 * over the item for a certain period of time.
3800 static LRESULT LISTVIEW_MouseHover(LISTVIEW_INFO *infoPtr, INT x, INT y)
3802 NMHDR hdr;
3804 if (notify_hdr(infoPtr, NM_HOVER, &hdr)) return 0;
3806 if (LISTVIEW_IsHotTracking(infoPtr))
3808 LVITEMW item;
3809 POINT pt;
3811 pt.x = x;
3812 pt.y = y;
3814 if (LISTVIEW_GetItemAtPt(infoPtr, &item, pt))
3815 LISTVIEW_SetSelection(infoPtr, item.iItem);
3817 SetFocus(infoPtr->hwndSelf);
3820 return 0;
3823 #define SCROLL_LEFT 0x1
3824 #define SCROLL_RIGHT 0x2
3825 #define SCROLL_UP 0x4
3826 #define SCROLL_DOWN 0x8
3828 /***
3829 * DESCRIPTION:
3830 * Utility routine to draw and highlight items within a marquee selection rectangle.
3832 * PARAMETER(S):
3833 * [I] infoPtr : valid pointer to the listview structure
3834 * [I] coords_orig : original co-ordinates of the cursor
3835 * [I] coords_offs : offsetted coordinates of the cursor
3836 * [I] offset : offset amount
3837 * [I] scroll : Bitmask of which directions we should scroll, if at all
3839 * RETURN:
3840 * None.
3842 static void LISTVIEW_MarqueeHighlight(LISTVIEW_INFO *infoPtr, const POINT *coords_orig,
3843 INT scroll)
3845 BOOL controlDown = FALSE;
3846 LVITEMW item;
3847 ITERATOR old_elems, new_elems;
3848 RECT rect;
3849 POINT coords_offs, offset;
3851 /* Ensure coordinates are within client bounds */
3852 coords_offs.x = max(min(coords_orig->x, infoPtr->rcList.right), 0);
3853 coords_offs.y = max(min(coords_orig->y, infoPtr->rcList.bottom), 0);
3855 /* Get offset */
3856 LISTVIEW_GetOrigin(infoPtr, &offset);
3858 /* Offset coordinates by the appropriate amount */
3859 coords_offs.x -= offset.x;
3860 coords_offs.y -= offset.y;
3862 if (coords_offs.x > infoPtr->marqueeOrigin.x)
3864 rect.left = infoPtr->marqueeOrigin.x;
3865 rect.right = coords_offs.x;
3867 else
3869 rect.left = coords_offs.x;
3870 rect.right = infoPtr->marqueeOrigin.x;
3873 if (coords_offs.y > infoPtr->marqueeOrigin.y)
3875 rect.top = infoPtr->marqueeOrigin.y;
3876 rect.bottom = coords_offs.y;
3878 else
3880 rect.top = coords_offs.y;
3881 rect.bottom = infoPtr->marqueeOrigin.y;
3884 /* Cancel out the old marquee rectangle and draw the new one */
3885 LISTVIEW_InvalidateRect(infoPtr, &infoPtr->marqueeDrawRect);
3887 /* Scroll by the appropriate distance if applicable - speed up scrolling as
3888 the cursor is further away */
3890 if ((scroll & SCROLL_LEFT) && (coords_orig->x <= 0))
3891 LISTVIEW_Scroll(infoPtr, coords_orig->x, 0);
3893 if ((scroll & SCROLL_RIGHT) && (coords_orig->x >= infoPtr->rcList.right))
3894 LISTVIEW_Scroll(infoPtr, (coords_orig->x - infoPtr->rcList.right), 0);
3896 if ((scroll & SCROLL_UP) && (coords_orig->y <= 0))
3897 LISTVIEW_Scroll(infoPtr, 0, coords_orig->y);
3899 if ((scroll & SCROLL_DOWN) && (coords_orig->y >= infoPtr->rcList.bottom))
3900 LISTVIEW_Scroll(infoPtr, 0, (coords_orig->y - infoPtr->rcList.bottom));
3902 iterator_frameditems_absolute(&old_elems, infoPtr, &infoPtr->marqueeRect);
3904 infoPtr->marqueeRect = rect;
3905 infoPtr->marqueeDrawRect = rect;
3906 OffsetRect(&infoPtr->marqueeDrawRect, offset.x, offset.y);
3908 iterator_frameditems_absolute(&new_elems, infoPtr, &infoPtr->marqueeRect);
3909 iterator_remove_common_items(&old_elems, &new_elems);
3911 /* Iterate over no longer selected items */
3912 while (iterator_next(&old_elems))
3914 if (old_elems.nItem > -1)
3916 if (LISTVIEW_GetItemState(infoPtr, old_elems.nItem, LVIS_SELECTED) == LVIS_SELECTED)
3917 item.state = 0;
3918 else
3919 item.state = LVIS_SELECTED;
3921 item.stateMask = LVIS_SELECTED;
3923 LISTVIEW_SetItemState(infoPtr, old_elems.nItem, &item);
3926 iterator_destroy(&old_elems);
3929 /* Iterate over newly selected items */
3930 if (GetKeyState(VK_CONTROL) & 0x8000)
3931 controlDown = TRUE;
3933 while (iterator_next(&new_elems))
3935 if (new_elems.nItem > -1)
3937 /* If CTRL is pressed, invert. If not, always select the item. */
3938 if ((controlDown) && (LISTVIEW_GetItemState(infoPtr, new_elems.nItem, LVIS_SELECTED)))
3939 item.state = 0;
3940 else
3941 item.state = LVIS_SELECTED;
3943 item.stateMask = LVIS_SELECTED;
3945 LISTVIEW_SetItemState(infoPtr, new_elems.nItem, &item);
3948 iterator_destroy(&new_elems);
3950 LISTVIEW_InvalidateRect(infoPtr, &infoPtr->marqueeDrawRect);
3953 /***
3954 * DESCRIPTION:
3955 * Called when we are in a marquee selection that involves scrolling the listview (ie,
3956 * the cursor is outside the bounds of the client area). This is a TIMERPROC.
3958 * PARAMETER(S):
3959 * [I] hwnd : Handle to the listview
3960 * [I] uMsg : WM_TIMER (ignored)
3961 * [I] idEvent : The timer ID interpreted as a pointer to a LISTVIEW_INFO struct
3962 * [I] dwTimer : The elapsed time (ignored)
3964 * RETURN:
3965 * None.
3967 static VOID CALLBACK LISTVIEW_ScrollTimer(HWND hWnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
3969 LISTVIEW_INFO *infoPtr;
3970 SCROLLINFO scrollInfo;
3971 POINT coords;
3972 INT scroll = 0;
3974 infoPtr = (LISTVIEW_INFO *) idEvent;
3976 if (!infoPtr)
3977 return;
3979 /* Get the current cursor position and convert to client coordinates */
3980 GetCursorPos(&coords);
3981 ScreenToClient(hWnd, &coords);
3983 scrollInfo.cbSize = sizeof(SCROLLINFO);
3984 scrollInfo.fMask = SIF_ALL;
3986 /* Work out in which directions we can scroll */
3987 if (GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
3989 if (scrollInfo.nPos != scrollInfo.nMin)
3990 scroll |= SCROLL_UP;
3992 if (((scrollInfo.nPage + scrollInfo.nPos) - 1) != scrollInfo.nMax)
3993 scroll |= SCROLL_DOWN;
3996 if (GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo))
3998 if (scrollInfo.nPos != scrollInfo.nMin)
3999 scroll |= SCROLL_LEFT;
4001 if (((scrollInfo.nPage + scrollInfo.nPos) - 1) != scrollInfo.nMax)
4002 scroll |= SCROLL_RIGHT;
4005 if (((coords.x <= 0) && (scroll & SCROLL_LEFT)) ||
4006 ((coords.y <= 0) && (scroll & SCROLL_UP)) ||
4007 ((coords.x >= infoPtr->rcList.right) && (scroll & SCROLL_RIGHT)) ||
4008 ((coords.y >= infoPtr->rcList.bottom) && (scroll & SCROLL_DOWN)))
4010 LISTVIEW_MarqueeHighlight(infoPtr, &coords, scroll);
4014 /***
4015 * DESCRIPTION:
4016 * Called whenever WM_MOUSEMOVE is received.
4018 * PARAMETER(S):
4019 * [I] infoPtr : valid pointer to the listview structure
4020 * [I] fwKeys : key indicator
4021 * [I] x,y : mouse position
4023 * RETURN:
4024 * 0 if the message is processed, non-zero if there was an error
4026 static LRESULT LISTVIEW_MouseMove(LISTVIEW_INFO *infoPtr, WORD fwKeys, INT x, INT y)
4028 LVHITTESTINFO ht;
4029 RECT rect;
4030 POINT pt;
4032 pt.x = x;
4033 pt.y = y;
4035 if (!(fwKeys & MK_LBUTTON))
4036 infoPtr->bLButtonDown = FALSE;
4038 if (infoPtr->bLButtonDown)
4040 rect.left = rect.right = infoPtr->ptClickPos.x;
4041 rect.top = rect.bottom = infoPtr->ptClickPos.y;
4043 InflateRect(&rect, GetSystemMetrics(SM_CXDRAG), GetSystemMetrics(SM_CYDRAG));
4045 if (infoPtr->bMarqueeSelect)
4047 /* Enable the timer if we're going outside our bounds, in case the user doesn't
4048 move the mouse again */
4050 if ((x <= 0) || (y <= 0) || (x >= infoPtr->rcList.right) ||
4051 (y >= infoPtr->rcList.bottom))
4053 if (!infoPtr->bScrolling)
4055 infoPtr->bScrolling = TRUE;
4056 SetTimer(infoPtr->hwndSelf, (UINT_PTR) infoPtr, 1, LISTVIEW_ScrollTimer);
4059 else
4061 infoPtr->bScrolling = FALSE;
4062 KillTimer(infoPtr->hwndSelf, (UINT_PTR) infoPtr);
4065 LISTVIEW_MarqueeHighlight(infoPtr, &pt, 0);
4066 return 0;
4069 ht.pt = pt;
4070 LISTVIEW_HitTest(infoPtr, &ht, TRUE, TRUE);
4072 /* reset item marker */
4073 if (infoPtr->nLButtonDownItem != ht.iItem)
4074 infoPtr->nLButtonDownItem = -1;
4076 if (!PtInRect(&rect, pt))
4078 /* this path covers the following:
4079 1. WM_LBUTTONDOWN over selected item (sets focus on it)
4080 2. change focus with keys
4081 3. move mouse over item from step 1 selects it and moves focus on it */
4082 if (infoPtr->nLButtonDownItem != -1 &&
4083 !LISTVIEW_GetItemState(infoPtr, infoPtr->nLButtonDownItem, LVIS_SELECTED))
4085 LVITEMW lvItem;
4087 lvItem.state = LVIS_FOCUSED | LVIS_SELECTED;
4088 lvItem.stateMask = LVIS_FOCUSED | LVIS_SELECTED;
4090 LISTVIEW_SetItemState(infoPtr, infoPtr->nLButtonDownItem, &lvItem);
4091 infoPtr->nLButtonDownItem = -1;
4094 if (!infoPtr->bDragging)
4096 ht.pt = infoPtr->ptClickPos;
4097 LISTVIEW_HitTest(infoPtr, &ht, TRUE, TRUE);
4099 /* If the click is outside the range of an item, begin a
4100 highlight. If not, begin an item drag. */
4101 if (ht.iItem == -1)
4103 NMHDR hdr;
4105 /* If we're allowing multiple selections, send notification.
4106 If return value is non-zero, cancel. */
4107 if (!(infoPtr->dwStyle & LVS_SINGLESEL) && (notify_hdr(infoPtr, LVN_MARQUEEBEGIN, &hdr) == 0))
4109 /* Store the absolute coordinates of the click */
4110 POINT offset;
4111 LISTVIEW_GetOrigin(infoPtr, &offset);
4113 infoPtr->marqueeOrigin.x = infoPtr->ptClickPos.x - offset.x;
4114 infoPtr->marqueeOrigin.y = infoPtr->ptClickPos.y - offset.y;
4116 /* Begin selection and capture mouse */
4117 infoPtr->bMarqueeSelect = TRUE;
4118 infoPtr->marqueeRect = rect;
4119 SetCapture(infoPtr->hwndSelf);
4122 else
4124 NMLISTVIEW nmlv;
4126 ZeroMemory(&nmlv, sizeof(nmlv));
4127 nmlv.iItem = ht.iItem;
4128 nmlv.ptAction = infoPtr->ptClickPos;
4130 notify_listview(infoPtr, LVN_BEGINDRAG, &nmlv);
4131 infoPtr->bDragging = TRUE;
4135 return 0;
4139 /* see if we are supposed to be tracking mouse hovering */
4140 if (LISTVIEW_IsHotTracking(infoPtr)) {
4141 TRACKMOUSEEVENT trackinfo;
4142 DWORD flags;
4144 trackinfo.cbSize = sizeof(TRACKMOUSEEVENT);
4145 trackinfo.dwFlags = TME_QUERY;
4147 /* see if we are already tracking this hwnd */
4148 _TrackMouseEvent(&trackinfo);
4150 flags = TME_LEAVE;
4151 if(infoPtr->dwLvExStyle & LVS_EX_TRACKSELECT)
4152 flags |= TME_HOVER;
4154 if((trackinfo.dwFlags & flags) != flags || trackinfo.hwndTrack != infoPtr->hwndSelf) {
4155 trackinfo.dwFlags = flags;
4156 trackinfo.dwHoverTime = infoPtr->dwHoverTime;
4157 trackinfo.hwndTrack = infoPtr->hwndSelf;
4159 /* call TRACKMOUSEEVENT so we receive WM_MOUSEHOVER messages */
4160 _TrackMouseEvent(&trackinfo);
4164 return 0;
4168 /***
4169 * Tests whether the item is assignable to a list with style lStyle
4171 static inline BOOL is_assignable_item(const LVITEMW *lpLVItem, LONG lStyle)
4173 if ( (lpLVItem->mask & LVIF_TEXT) &&
4174 (lpLVItem->pszText == LPSTR_TEXTCALLBACKW) &&
4175 (lStyle & (LVS_SORTASCENDING | LVS_SORTDESCENDING)) ) return FALSE;
4177 return TRUE;
4181 /***
4182 * DESCRIPTION:
4183 * Helper for LISTVIEW_SetItemT and LISTVIEW_InsertItemT: sets item attributes.
4185 * PARAMETER(S):
4186 * [I] infoPtr : valid pointer to the listview structure
4187 * [I] lpLVItem : valid pointer to new item attributes
4188 * [I] isNew : the item being set is being inserted
4189 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
4190 * [O] bChanged : will be set to TRUE if the item really changed
4192 * RETURN:
4193 * SUCCESS : TRUE
4194 * FAILURE : FALSE
4196 static BOOL set_main_item(LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem, BOOL isNew, BOOL isW, BOOL *bChanged)
4198 ITEM_INFO *lpItem;
4199 NMLISTVIEW nmlv;
4200 UINT uChanged = 0;
4201 LVITEMW item;
4202 /* stateMask is ignored for LVM_INSERTITEM */
4203 UINT stateMask = isNew ? ~0 : lpLVItem->stateMask;
4204 BOOL send_change_notification;
4206 TRACE("()\n");
4208 assert(lpLVItem->iItem >= 0 && lpLVItem->iItem < infoPtr->nItemCount);
4210 if (lpLVItem->mask == 0) return TRUE;
4212 if (infoPtr->dwStyle & LVS_OWNERDATA)
4214 /* a virtual listview only stores selection and focus */
4215 if (lpLVItem->mask & ~LVIF_STATE)
4216 return FALSE;
4217 lpItem = NULL;
4219 else
4221 HDPA hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, lpLVItem->iItem);
4222 lpItem = DPA_GetPtr(hdpaSubItems, 0);
4223 assert (lpItem);
4226 /* we need to get the lParam and state of the item */
4227 item.iItem = lpLVItem->iItem;
4228 item.iSubItem = lpLVItem->iSubItem;
4229 item.mask = LVIF_STATE | LVIF_PARAM;
4230 item.stateMask = (infoPtr->dwStyle & LVS_OWNERDATA) ? LVIS_FOCUSED | LVIS_SELECTED : ~0;
4232 item.state = 0;
4233 item.lParam = 0;
4234 if (!isNew && !LISTVIEW_GetItemW(infoPtr, &item)) return FALSE;
4236 TRACE("oldState=%x, newState=%x\n", item.state, lpLVItem->state);
4237 /* determine what fields will change */
4238 if ((lpLVItem->mask & LVIF_STATE) && ((item.state ^ lpLVItem->state) & stateMask & ~infoPtr->uCallbackMask))
4239 uChanged |= LVIF_STATE;
4241 if ((lpLVItem->mask & LVIF_IMAGE) && (lpItem->hdr.iImage != lpLVItem->iImage))
4242 uChanged |= LVIF_IMAGE;
4244 if ((lpLVItem->mask & LVIF_PARAM) && (lpItem->lParam != lpLVItem->lParam))
4245 uChanged |= LVIF_PARAM;
4247 if ((lpLVItem->mask & LVIF_INDENT) && (lpItem->iIndent != lpLVItem->iIndent))
4248 uChanged |= LVIF_INDENT;
4250 if ((lpLVItem->mask & LVIF_TEXT) && textcmpWT(lpItem->hdr.pszText, lpLVItem->pszText, isW))
4251 uChanged |= LVIF_TEXT;
4253 TRACE("change mask=0x%x\n", uChanged);
4255 memset(&nmlv, 0, sizeof(NMLISTVIEW));
4256 nmlv.iItem = lpLVItem->iItem;
4257 if (lpLVItem->mask & LVIF_STATE)
4259 nmlv.uNewState = (item.state & ~stateMask) | (lpLVItem->state & stateMask);
4260 nmlv.uOldState = item.state;
4262 nmlv.uChanged = isNew ? LVIF_STATE : (uChanged ? uChanged : lpLVItem->mask);
4263 nmlv.lParam = item.lParam;
4265 /* Send change notification if the item is not being inserted, or inserted (selected|focused),
4266 and we are _NOT_ virtual (LVS_OWNERDATA), and change notifications
4267 are enabled. Even if nothing really changed we still need to send this,
4268 in this case uChanged mask is just set to passed item mask. */
4270 send_change_notification = !isNew;
4271 send_change_notification |= (uChanged & LVIF_STATE) && (lpLVItem->state & (LVIS_FOCUSED | LVIS_SELECTED));
4272 send_change_notification &= !!(infoPtr->notify_mask & NOTIFY_MASK_ITEM_CHANGE);
4274 if (lpItem && send_change_notification)
4276 HWND hwndSelf = infoPtr->hwndSelf;
4278 if (notify_listview(infoPtr, LVN_ITEMCHANGING, &nmlv))
4279 return FALSE;
4280 if (!IsWindow(hwndSelf))
4281 return FALSE;
4284 /* When item is inserted we need to shift existing focus index if new item has lower index. */
4285 if (isNew && (stateMask & ~infoPtr->uCallbackMask & LVIS_FOCUSED) &&
4286 /* this means we won't hit a focus change path later */
4287 ((uChanged & LVIF_STATE) == 0 || (!(lpLVItem->state & LVIS_FOCUSED) && (infoPtr->nFocusedItem != lpLVItem->iItem))))
4289 if (infoPtr->nFocusedItem != -1 && (lpLVItem->iItem <= infoPtr->nFocusedItem))
4290 infoPtr->nFocusedItem++;
4293 if (!uChanged) return TRUE;
4294 *bChanged = TRUE;
4296 /* copy information */
4297 if (lpLVItem->mask & LVIF_TEXT)
4298 textsetptrT(&lpItem->hdr.pszText, lpLVItem->pszText, isW);
4300 if (lpLVItem->mask & LVIF_IMAGE)
4301 lpItem->hdr.iImage = lpLVItem->iImage;
4303 if (lpLVItem->mask & LVIF_PARAM)
4304 lpItem->lParam = lpLVItem->lParam;
4306 if (lpLVItem->mask & LVIF_INDENT)
4307 lpItem->iIndent = lpLVItem->iIndent;
4309 if (uChanged & LVIF_STATE)
4311 if (lpItem && (stateMask & ~infoPtr->uCallbackMask))
4313 lpItem->state &= ~stateMask;
4314 lpItem->state |= (lpLVItem->state & stateMask);
4316 if (lpLVItem->state & stateMask & ~infoPtr->uCallbackMask & LVIS_SELECTED)
4318 if (infoPtr->dwStyle & LVS_SINGLESEL) LISTVIEW_DeselectAllSkipItem(infoPtr, lpLVItem->iItem);
4319 ranges_additem(infoPtr->selectionRanges, lpLVItem->iItem);
4321 else if (stateMask & LVIS_SELECTED)
4323 ranges_delitem(infoPtr->selectionRanges, lpLVItem->iItem);
4325 /* If we are asked to change focus, and we manage it, do it.
4326 It's important to have all new item data stored at this point,
4327 because changing existing focus could result in a redrawing operation,
4328 which in turn could ask for disp data, application should see all data
4329 for inserted item when processing LVN_GETDISPINFO.
4331 The way this works application will see nested item change notifications -
4332 changed item notifications interrupted by ones from item losing focus. */
4333 if (stateMask & ~infoPtr->uCallbackMask & LVIS_FOCUSED)
4335 if (lpLVItem->state & LVIS_FOCUSED)
4337 /* update selection mark */
4338 if (infoPtr->nFocusedItem == -1 && infoPtr->nSelectionMark == -1)
4339 infoPtr->nSelectionMark = lpLVItem->iItem;
4341 if (infoPtr->nFocusedItem != -1)
4343 /* remove current focus */
4344 item.mask = LVIF_STATE;
4345 item.state = 0;
4346 item.stateMask = LVIS_FOCUSED;
4348 /* recurse with redrawing an item */
4349 LISTVIEW_SetItemState(infoPtr, infoPtr->nFocusedItem, &item);
4352 infoPtr->nFocusedItem = lpLVItem->iItem;
4353 LISTVIEW_EnsureVisible(infoPtr, lpLVItem->iItem, infoPtr->uView == LV_VIEW_LIST);
4355 else if (infoPtr->nFocusedItem == lpLVItem->iItem)
4357 infoPtr->nFocusedItem = -1;
4362 if (send_change_notification)
4364 if (lpLVItem->mask & LVIF_PARAM) nmlv.lParam = lpLVItem->lParam;
4365 notify_listview(infoPtr, LVN_ITEMCHANGED, &nmlv);
4368 return TRUE;
4371 /***
4372 * DESCRIPTION:
4373 * Helper for LISTVIEW_{Set,Insert}ItemT *only*: sets subitem attributes.
4375 * PARAMETER(S):
4376 * [I] infoPtr : valid pointer to the listview structure
4377 * [I] lpLVItem : valid pointer to new subitem attributes
4378 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
4379 * [O] bChanged : will be set to TRUE if the item really changed
4381 * RETURN:
4382 * SUCCESS : TRUE
4383 * FAILURE : FALSE
4385 static BOOL set_sub_item(const LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem, BOOL isW, BOOL *bChanged)
4387 HDPA hdpaSubItems;
4388 SUBITEM_INFO *lpSubItem;
4390 /* we do not support subitems for virtual listviews */
4391 if (infoPtr->dwStyle & LVS_OWNERDATA) return FALSE;
4393 /* set subitem only if column is present */
4394 if (lpLVItem->iSubItem >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
4396 /* First do some sanity checks */
4397 /* The LVIF_STATE flag is valid for subitems, but does not appear to be
4398 particularly useful. We currently do not actually do anything with
4399 the flag on subitems.
4401 if (lpLVItem->mask & ~(LVIF_TEXT | LVIF_IMAGE | LVIF_STATE | LVIF_DI_SETITEM)) return FALSE;
4402 if (!(lpLVItem->mask & (LVIF_TEXT | LVIF_IMAGE | LVIF_STATE))) return TRUE;
4404 /* get the subitem structure, and create it if not there */
4405 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, lpLVItem->iItem);
4406 assert (hdpaSubItems);
4408 lpSubItem = LISTVIEW_GetSubItemPtr(hdpaSubItems, lpLVItem->iSubItem);
4409 if (!lpSubItem)
4411 SUBITEM_INFO *tmpSubItem;
4412 INT i;
4414 lpSubItem = Alloc(sizeof(SUBITEM_INFO));
4415 if (!lpSubItem) return FALSE;
4416 /* we could binary search here, if need be...*/
4417 for (i = 1; i < DPA_GetPtrCount(hdpaSubItems); i++)
4419 tmpSubItem = DPA_GetPtr(hdpaSubItems, i);
4420 if (tmpSubItem->iSubItem > lpLVItem->iSubItem) break;
4422 if (DPA_InsertPtr(hdpaSubItems, i, lpSubItem) == -1)
4424 Free(lpSubItem);
4425 return FALSE;
4427 lpSubItem->iSubItem = lpLVItem->iSubItem;
4428 lpSubItem->hdr.iImage = I_IMAGECALLBACK;
4429 *bChanged = TRUE;
4432 if ((lpLVItem->mask & LVIF_IMAGE) && (lpSubItem->hdr.iImage != lpLVItem->iImage))
4434 lpSubItem->hdr.iImage = lpLVItem->iImage;
4435 *bChanged = TRUE;
4438 if ((lpLVItem->mask & LVIF_TEXT) && textcmpWT(lpSubItem->hdr.pszText, lpLVItem->pszText, isW))
4440 textsetptrT(&lpSubItem->hdr.pszText, lpLVItem->pszText, isW);
4441 *bChanged = TRUE;
4444 return TRUE;
4447 /***
4448 * DESCRIPTION:
4449 * Sets item attributes.
4451 * PARAMETER(S):
4452 * [I] infoPtr : valid pointer to the listview structure
4453 * [I] lpLVItem : new item attributes
4454 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
4456 * RETURN:
4457 * SUCCESS : TRUE
4458 * FAILURE : FALSE
4460 static BOOL LISTVIEW_SetItemT(LISTVIEW_INFO *infoPtr, LVITEMW *lpLVItem, BOOL isW)
4462 HWND hwndSelf = infoPtr->hwndSelf;
4463 LPWSTR pszText = NULL;
4464 BOOL bResult, bChanged = FALSE;
4465 RECT oldItemArea;
4467 TRACE("(lpLVItem=%s, isW=%d)\n", debuglvitem_t(lpLVItem, isW), isW);
4469 if (!lpLVItem || lpLVItem->iItem < 0 || lpLVItem->iItem >= infoPtr->nItemCount)
4470 return FALSE;
4472 /* Store old item area */
4473 LISTVIEW_GetItemBox(infoPtr, lpLVItem->iItem, &oldItemArea);
4475 /* For efficiency, we transform the lpLVItem->pszText to Unicode here */
4476 if ((lpLVItem->mask & LVIF_TEXT) && is_text(lpLVItem->pszText))
4478 pszText = lpLVItem->pszText;
4479 lpLVItem->pszText = textdupTtoW(lpLVItem->pszText, isW);
4482 /* actually set the fields */
4483 if (!is_assignable_item(lpLVItem, infoPtr->dwStyle)) return FALSE;
4485 if (lpLVItem->iSubItem)
4486 bResult = set_sub_item(infoPtr, lpLVItem, TRUE, &bChanged);
4487 else
4488 bResult = set_main_item(infoPtr, lpLVItem, FALSE, TRUE, &bChanged);
4489 if (!IsWindow(hwndSelf))
4490 return FALSE;
4492 /* redraw item, if necessary */
4493 if (bChanged && !infoPtr->bIsDrawing)
4495 /* this little optimization eliminates some nasty flicker */
4496 if ( infoPtr->uView == LV_VIEW_DETAILS && !(infoPtr->dwStyle & LVS_OWNERDRAWFIXED) &&
4497 !(infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) &&
4498 lpLVItem->iSubItem > 0 && lpLVItem->iSubItem <= DPA_GetPtrCount(infoPtr->hdpaColumns) )
4499 LISTVIEW_InvalidateSubItem(infoPtr, lpLVItem->iItem, lpLVItem->iSubItem);
4500 else
4502 LISTVIEW_InvalidateRect(infoPtr, &oldItemArea);
4503 LISTVIEW_InvalidateItem(infoPtr, lpLVItem->iItem);
4506 /* restore text */
4507 if (pszText)
4509 textfreeT(lpLVItem->pszText, isW);
4510 lpLVItem->pszText = pszText;
4513 return bResult;
4516 /***
4517 * DESCRIPTION:
4518 * Retrieves the index of the item at coordinate (0, 0) of the client area.
4520 * PARAMETER(S):
4521 * [I] infoPtr : valid pointer to the listview structure
4523 * RETURN:
4524 * item index
4526 static INT LISTVIEW_GetTopIndex(const LISTVIEW_INFO *infoPtr)
4528 INT nItem = 0;
4529 SCROLLINFO scrollInfo;
4531 scrollInfo.cbSize = sizeof(SCROLLINFO);
4532 scrollInfo.fMask = SIF_POS;
4534 if (infoPtr->uView == LV_VIEW_LIST)
4536 if (GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo))
4537 nItem = scrollInfo.nPos * LISTVIEW_GetCountPerColumn(infoPtr);
4539 else if (infoPtr->uView == LV_VIEW_DETAILS)
4541 if (GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
4542 nItem = scrollInfo.nPos;
4544 else
4546 if (GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
4547 nItem = LISTVIEW_GetCountPerRow(infoPtr) * (scrollInfo.nPos / infoPtr->nItemHeight);
4550 TRACE("nItem=%d\n", nItem);
4552 return nItem;
4555 static void LISTVIEW_DrawBackgroundBitmap(const LISTVIEW_INFO *infoPtr, HDC hdc, const RECT *lprcBox)
4557 HDC mem_hdc;
4559 if (!infoPtr->hBkBitmap)
4560 return;
4562 TRACE("(hdc=%p, lprcBox=%s, hBkBitmap=%p)\n", hdc, wine_dbgstr_rect(lprcBox), infoPtr->hBkBitmap);
4564 mem_hdc = CreateCompatibleDC(hdc);
4565 SelectObject(mem_hdc, infoPtr->hBkBitmap);
4566 BitBlt(hdc, lprcBox->left, lprcBox->top, lprcBox->right - lprcBox->left,
4567 lprcBox->bottom - lprcBox->top, mem_hdc, lprcBox->left, lprcBox->top, SRCCOPY);
4568 DeleteDC(mem_hdc);
4571 /***
4572 * DESCRIPTION:
4573 * Erases the background of the given rectangle
4575 * PARAMETER(S):
4576 * [I] infoPtr : valid pointer to the listview structure
4577 * [I] hdc : device context handle
4578 * [I] lprcBox : clipping rectangle
4580 * RETURN:
4581 * Success: TRUE
4582 * Failure: FALSE
4584 static BOOL LISTVIEW_FillBkgnd(const LISTVIEW_INFO *infoPtr, HDC hdc, const RECT *lprcBox)
4586 if (infoPtr->hBkBrush)
4588 TRACE("(hdc=%p, lprcBox=%s, hBkBrush=%p)\n", hdc, wine_dbgstr_rect(lprcBox), infoPtr->hBkBrush);
4590 FillRect(hdc, lprcBox, infoPtr->hBkBrush);
4593 LISTVIEW_DrawBackgroundBitmap(infoPtr, hdc, lprcBox);
4594 return TRUE;
4597 /* Draw main item or subitem */
4598 static void LISTVIEW_DrawItemPart(LISTVIEW_INFO *infoPtr, LVITEMW *item, const NMLVCUSTOMDRAW *nmlvcd, const POINT *pos)
4600 RECT rcSelect, rcLabel, rcBox, rcStateIcon, rcIcon;
4601 const RECT *background;
4602 HIMAGELIST himl;
4603 UINT format;
4604 RECT *focus;
4606 /* now check if we need to update the focus rectangle */
4607 focus = infoPtr->bFocus && (item->state & LVIS_FOCUSED) ? &infoPtr->rcFocus : 0;
4608 if (!focus) item->state &= ~LVIS_FOCUSED;
4610 LISTVIEW_GetItemMetrics(infoPtr, item, &rcBox, &rcSelect, &rcIcon, &rcStateIcon, &rcLabel);
4611 OffsetRect(&rcBox, pos->x, pos->y);
4612 OffsetRect(&rcSelect, pos->x, pos->y);
4613 OffsetRect(&rcIcon, pos->x, pos->y);
4614 OffsetRect(&rcStateIcon, pos->x, pos->y);
4615 OffsetRect(&rcLabel, pos->x, pos->y);
4616 TRACE("%d: box=%s, select=%s, icon=%s. label=%s\n", item->iSubItem,
4617 wine_dbgstr_rect(&rcBox), wine_dbgstr_rect(&rcSelect),
4618 wine_dbgstr_rect(&rcIcon), wine_dbgstr_rect(&rcLabel));
4620 /* FIXME: temporary hack */
4621 rcSelect.left = rcLabel.left;
4623 if (infoPtr->uView == LV_VIEW_DETAILS && item->iSubItem == 0)
4625 if (!(infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT))
4626 OffsetRect(&rcSelect, LISTVIEW_GetColumnInfo(infoPtr, 0)->rcHeader.left, 0);
4627 OffsetRect(&rcIcon, LISTVIEW_GetColumnInfo(infoPtr, 0)->rcHeader.left, 0);
4628 OffsetRect(&rcStateIcon, LISTVIEW_GetColumnInfo(infoPtr, 0)->rcHeader.left, 0);
4629 OffsetRect(&rcLabel, LISTVIEW_GetColumnInfo(infoPtr, 0)->rcHeader.left, 0);
4632 /* in icon mode, the label rect is really what we want to draw the
4633 * background for */
4634 /* in detail mode, we want to paint background for label rect when
4635 * item is not selected or listview has full row select; otherwise paint
4636 * background for text only */
4637 if ( infoPtr->uView == LV_VIEW_ICON ||
4638 (infoPtr->uView == LV_VIEW_DETAILS && (!(item->state & LVIS_SELECTED) ||
4639 (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT))))
4640 background = &rcLabel;
4641 else
4642 background = &rcSelect;
4644 if (nmlvcd->clrTextBk != CLR_NONE)
4645 ExtTextOutW(nmlvcd->nmcd.hdc, background->left, background->top, ETO_OPAQUE, background, NULL, 0, NULL);
4647 if (item->state & LVIS_FOCUSED)
4649 if (infoPtr->uView == LV_VIEW_DETAILS)
4651 if (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT)
4653 /* we have to update left focus bound too if item isn't in leftmost column
4654 and reduce right box bound */
4655 if (DPA_GetPtrCount(infoPtr->hdpaColumns) > 0)
4657 INT leftmost;
4659 if ((leftmost = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, 0, 0)))
4661 INT Originx = pos->x - LISTVIEW_GetColumnInfo(infoPtr, leftmost)->rcHeader.left;
4662 INT rightmost = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX,
4663 DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, 0);
4665 rcBox.right = LISTVIEW_GetColumnInfo(infoPtr, rightmost)->rcHeader.right + Originx;
4666 rcSelect.left = LISTVIEW_GetColumnInfo(infoPtr, leftmost)->rcHeader.left + Originx;
4669 rcSelect.right = rcBox.right;
4671 infoPtr->rcFocus = rcSelect;
4673 else
4674 infoPtr->rcFocus = rcLabel;
4677 /* state icons */
4678 if (infoPtr->himlState && STATEIMAGEINDEX(item->state) && (item->iSubItem == 0))
4680 UINT stateimage = STATEIMAGEINDEX(item->state);
4681 if (stateimage)
4683 TRACE("stateimage=%d\n", stateimage);
4684 ImageList_Draw(infoPtr->himlState, stateimage-1, nmlvcd->nmcd.hdc, rcStateIcon.left, rcStateIcon.top, ILD_NORMAL);
4688 /* item icons */
4689 himl = (infoPtr->uView == LV_VIEW_ICON ? infoPtr->himlNormal : infoPtr->himlSmall);
4690 if (himl && item->iImage >= 0 && !IsRectEmpty(&rcIcon))
4692 UINT style;
4694 TRACE("iImage=%d\n", item->iImage);
4696 if (item->state & (LVIS_SELECTED | LVIS_CUT) && infoPtr->bFocus)
4697 style = ILD_SELECTED;
4698 else
4699 style = ILD_NORMAL;
4701 ImageList_DrawEx(himl, item->iImage, nmlvcd->nmcd.hdc, rcIcon.left, rcIcon.top,
4702 rcIcon.right - rcIcon.left, rcIcon.bottom - rcIcon.top, infoPtr->clrBk,
4703 item->state & LVIS_CUT ? RGB(255, 255, 255) : CLR_DEFAULT,
4704 style | (item->state & LVIS_OVERLAYMASK));
4707 /* Don't bother painting item being edited */
4708 if (infoPtr->hwndEdit && item->iItem == infoPtr->nEditLabelItem && item->iSubItem == 0) return;
4710 /* figure out the text drawing flags */
4711 format = (infoPtr->uView == LV_VIEW_ICON ? (focus ? LV_FL_DT_FLAGS : LV_ML_DT_FLAGS) : LV_SL_DT_FLAGS);
4712 if (infoPtr->uView == LV_VIEW_ICON)
4713 format = (focus ? LV_FL_DT_FLAGS : LV_ML_DT_FLAGS);
4714 else if (item->iSubItem)
4716 switch (LISTVIEW_GetColumnInfo(infoPtr, item->iSubItem)->fmt & LVCFMT_JUSTIFYMASK)
4718 case LVCFMT_RIGHT: format |= DT_RIGHT; break;
4719 case LVCFMT_CENTER: format |= DT_CENTER; break;
4720 default: format |= DT_LEFT;
4723 if (!(format & (DT_RIGHT | DT_CENTER)))
4725 if (himl && item->iImage >= 0 && !IsRectEmpty(&rcIcon)) rcLabel.left += IMAGE_PADDING;
4726 else rcLabel.left += LABEL_HOR_PADDING;
4728 else if (format & DT_RIGHT) rcLabel.right -= LABEL_HOR_PADDING;
4730 /* for GRIDLINES reduce the bottom so the text formats correctly */
4731 if (infoPtr->uView == LV_VIEW_DETAILS && infoPtr->dwLvExStyle & LVS_EX_GRIDLINES)
4732 rcLabel.bottom--;
4734 DrawTextW(nmlvcd->nmcd.hdc, item->pszText, -1, &rcLabel, format);
4737 /***
4738 * DESCRIPTION:
4739 * Draws an item.
4741 * PARAMETER(S):
4742 * [I] infoPtr : valid pointer to the listview structure
4743 * [I] hdc : device context handle
4744 * [I] nItem : item index
4745 * [I] nSubItem : subitem index
4746 * [I] pos : item position in client coordinates
4747 * [I] cdmode : custom draw mode
4749 * RETURN:
4750 * Success: TRUE
4751 * Failure: FALSE
4753 static BOOL LISTVIEW_DrawItem(LISTVIEW_INFO *infoPtr, HDC hdc, INT nItem, ITERATOR *subitems, POINT pos, DWORD cdmode)
4755 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
4756 static WCHAR callbackW[] = L"(callback)";
4757 DWORD cdsubitemmode = CDRF_DODEFAULT;
4758 RECT *focus, rcBox;
4759 NMLVCUSTOMDRAW nmlvcd;
4760 LVITEMW lvItem;
4762 TRACE("(hdc=%p, nItem=%d, subitems=%p, pos=%s)\n", hdc, nItem, subitems, wine_dbgstr_point(&pos));
4764 /* get information needed for drawing the item */
4765 lvItem.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_PARAM | LVIF_STATE;
4766 if (infoPtr->uView == LV_VIEW_DETAILS) lvItem.mask |= LVIF_INDENT;
4767 lvItem.stateMask = LVIS_SELECTED | LVIS_FOCUSED | LVIS_STATEIMAGEMASK | LVIS_CUT | LVIS_OVERLAYMASK;
4768 lvItem.iItem = nItem;
4769 lvItem.iSubItem = 0;
4770 lvItem.state = 0;
4771 lvItem.lParam = 0;
4772 lvItem.cchTextMax = DISP_TEXT_SIZE;
4773 lvItem.pszText = szDispText;
4774 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return FALSE;
4775 if (lvItem.pszText == LPSTR_TEXTCALLBACKW) lvItem.pszText = callbackW;
4776 TRACE(" lvItem=%s\n", debuglvitem_t(&lvItem, TRUE));
4778 /* now check if we need to update the focus rectangle */
4779 focus = infoPtr->bFocus && (lvItem.state & LVIS_FOCUSED) ? &infoPtr->rcFocus : 0;
4780 if (!focus) lvItem.state &= ~LVIS_FOCUSED;
4782 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, &rcBox, NULL, NULL, NULL, NULL);
4783 OffsetRect(&rcBox, pos.x, pos.y);
4785 /* Full custom draw stage sequence looks like this:
4787 LV_VIEW_DETAILS:
4789 - CDDS_ITEMPREPAINT
4790 - CDDS_ITEMPREPAINT|CDDS_SUBITEM | => sent n times, where n is number of subitems,
4791 CDDS_ITEMPOSTPAINT|CDDS_SUBITEM | including item itself
4792 - CDDS_ITEMPOSTPAINT
4794 other styles:
4796 - CDDS_ITEMPREPAINT
4797 - CDDS_ITEMPOSTPAINT
4800 /* fill in the custom draw structure */
4801 customdraw_fill(&nmlvcd, infoPtr, hdc, &rcBox, &lvItem);
4802 if (cdmode & CDRF_NOTIFYITEMDRAW)
4803 cdsubitemmode = notify_customdraw(infoPtr, CDDS_ITEMPREPAINT, &nmlvcd);
4804 if (cdsubitemmode & CDRF_SKIPDEFAULT) goto postpaint;
4806 if (subitems)
4808 while (iterator_next(subitems))
4810 DWORD subitemstage = CDRF_DODEFAULT;
4812 /* We need to query for each subitem, item's data (subitem == 0) is already here at this point */
4813 if (subitems->nItem)
4815 lvItem.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_PARAM | LVIF_INDENT;
4816 lvItem.stateMask = LVIS_SELECTED | LVIS_FOCUSED | LVIS_STATEIMAGEMASK | LVIS_CUT | LVIS_OVERLAYMASK;
4817 lvItem.iItem = nItem;
4818 lvItem.iSubItem = subitems->nItem;
4819 lvItem.state = 0;
4820 lvItem.lParam = 0;
4821 lvItem.cchTextMax = DISP_TEXT_SIZE;
4822 lvItem.pszText = szDispText;
4823 szDispText[0] = 0;
4824 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return FALSE;
4825 if (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT)
4826 lvItem.state = LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED);
4827 if (lvItem.pszText == LPSTR_TEXTCALLBACKW) lvItem.pszText = callbackW;
4828 TRACE(" lvItem=%s\n", debuglvitem_t(&lvItem, TRUE));
4830 /* update custom draw data */
4831 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, &nmlvcd.nmcd.rc, NULL, NULL, NULL, NULL);
4832 OffsetRect(&nmlvcd.nmcd.rc, pos.x, pos.y);
4833 nmlvcd.iSubItem = subitems->nItem;
4836 if (cdsubitemmode & CDRF_NOTIFYSUBITEMDRAW)
4837 subitemstage = notify_customdraw(infoPtr, CDDS_SUBITEM | CDDS_ITEMPREPAINT, &nmlvcd);
4839 if (subitems->nItem == 0 || (cdmode & CDRF_NOTIFYITEMDRAW))
4840 prepaint_setup(infoPtr, hdc, &nmlvcd, FALSE);
4841 else if (!(infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT))
4842 prepaint_setup(infoPtr, hdc, &nmlvcd, TRUE);
4844 if (!(subitemstage & CDRF_SKIPDEFAULT))
4845 LISTVIEW_DrawItemPart(infoPtr, &lvItem, &nmlvcd, &pos);
4847 if (subitemstage & CDRF_NOTIFYPOSTPAINT)
4848 subitemstage = notify_customdraw(infoPtr, CDDS_SUBITEM | CDDS_ITEMPOSTPAINT, &nmlvcd);
4851 else
4853 prepaint_setup(infoPtr, hdc, &nmlvcd, FALSE);
4854 LISTVIEW_DrawItemPart(infoPtr, &lvItem, &nmlvcd, &pos);
4857 postpaint:
4858 if (cdsubitemmode & CDRF_NOTIFYPOSTPAINT)
4860 nmlvcd.iSubItem = 0;
4861 notify_customdraw(infoPtr, CDDS_ITEMPOSTPAINT, &nmlvcd);
4864 return TRUE;
4867 /***
4868 * DESCRIPTION:
4869 * Draws listview items when in owner draw mode.
4871 * PARAMETER(S):
4872 * [I] infoPtr : valid pointer to the listview structure
4873 * [I] hdc : device context handle
4875 * RETURN:
4876 * None
4878 static void LISTVIEW_RefreshOwnerDraw(const LISTVIEW_INFO *infoPtr, ITERATOR *i, HDC hdc, DWORD cdmode)
4880 UINT uID = (UINT)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
4881 DWORD cditemmode = CDRF_DODEFAULT;
4882 NMLVCUSTOMDRAW nmlvcd;
4883 POINT Origin, Position;
4884 DRAWITEMSTRUCT dis;
4885 LVITEMW item;
4887 TRACE("()\n");
4889 ZeroMemory(&dis, sizeof(dis));
4891 /* Get scroll info once before loop */
4892 LISTVIEW_GetOrigin(infoPtr, &Origin);
4894 /* iterate through the invalidated rows */
4895 while(iterator_next(i))
4897 item.iItem = i->nItem;
4898 item.iSubItem = 0;
4899 item.mask = LVIF_PARAM | LVIF_STATE;
4900 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
4901 if (!LISTVIEW_GetItemW(infoPtr, &item)) continue;
4903 dis.CtlType = ODT_LISTVIEW;
4904 dis.CtlID = uID;
4905 dis.itemID = item.iItem;
4906 dis.itemAction = ODA_DRAWENTIRE;
4907 dis.itemState = 0;
4908 if (item.state & LVIS_SELECTED) dis.itemState |= ODS_SELECTED;
4909 if (infoPtr->bFocus && (item.state & LVIS_FOCUSED)) dis.itemState |= ODS_FOCUS;
4910 dis.hwndItem = infoPtr->hwndSelf;
4911 dis.hDC = hdc;
4912 LISTVIEW_GetItemOrigin(infoPtr, dis.itemID, &Position);
4913 dis.rcItem.left = Position.x + Origin.x;
4914 dis.rcItem.right = dis.rcItem.left + infoPtr->nItemWidth;
4915 dis.rcItem.top = Position.y + Origin.y;
4916 dis.rcItem.bottom = dis.rcItem.top + infoPtr->nItemHeight;
4917 dis.itemData = item.lParam;
4919 TRACE("item=%s, rcItem=%s\n", debuglvitem_t(&item, TRUE), wine_dbgstr_rect(&dis.rcItem));
4922 * Even if we do not send the CDRF_NOTIFYITEMDRAW we need to fill the nmlvcd
4923 * structure for the rest. of the paint cycle
4925 customdraw_fill(&nmlvcd, infoPtr, hdc, &dis.rcItem, &item);
4926 if (cdmode & CDRF_NOTIFYITEMDRAW)
4927 cditemmode = notify_customdraw(infoPtr, CDDS_PREPAINT, &nmlvcd);
4929 if (!(cditemmode & CDRF_SKIPDEFAULT))
4931 prepaint_setup (infoPtr, hdc, &nmlvcd, FALSE);
4932 SendMessageW(infoPtr->hwndNotify, WM_DRAWITEM, dis.CtlID, (LPARAM)&dis);
4935 if (cditemmode & CDRF_NOTIFYPOSTPAINT)
4936 notify_postpaint(infoPtr, &nmlvcd);
4940 /***
4941 * DESCRIPTION:
4942 * Draws listview items when in report display mode.
4944 * PARAMETER(S):
4945 * [I] infoPtr : valid pointer to the listview structure
4946 * [I] hdc : device context handle
4947 * [I] cdmode : custom draw mode
4949 * RETURN:
4950 * None
4952 static void LISTVIEW_RefreshReport(LISTVIEW_INFO *infoPtr, ITERATOR *i, HDC hdc, DWORD cdmode)
4954 INT rgntype;
4955 RECT rcClip, rcItem;
4956 POINT Origin;
4957 RANGES colRanges;
4958 INT col;
4959 ITERATOR j;
4961 TRACE("()\n");
4963 /* figure out what to draw */
4964 rgntype = GetClipBox(hdc, &rcClip);
4965 if (rgntype == NULLREGION) return;
4967 /* Get scroll info once before loop */
4968 LISTVIEW_GetOrigin(infoPtr, &Origin);
4970 colRanges = ranges_create(DPA_GetPtrCount(infoPtr->hdpaColumns));
4972 /* narrow down the columns we need to paint */
4973 for(col = 0; col < DPA_GetPtrCount(infoPtr->hdpaColumns); col++)
4975 INT index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, col, 0);
4977 LISTVIEW_GetHeaderRect(infoPtr, index, &rcItem);
4978 if ((rcItem.right + Origin.x >= rcClip.left) && (rcItem.left + Origin.x < rcClip.right))
4979 ranges_additem(colRanges, index);
4981 iterator_rangesitems(&j, colRanges);
4983 /* in full row select, we _have_ to draw the main item */
4984 if (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT)
4985 j.nSpecial = 0;
4987 /* iterate through the invalidated rows */
4988 while(iterator_next(i))
4990 RANGES subitems;
4991 POINT Position;
4992 ITERATOR k;
4994 SelectObject(hdc, infoPtr->hFont);
4995 LISTVIEW_GetItemOrigin(infoPtr, i->nItem, &Position);
4996 Position.x = Origin.x;
4997 Position.y += Origin.y;
4999 subitems = ranges_create(DPA_GetPtrCount(infoPtr->hdpaColumns));
5001 /* iterate through the invalidated columns */
5002 while(iterator_next(&j))
5004 LISTVIEW_GetHeaderRect(infoPtr, j.nItem, &rcItem);
5006 if (rgntype == COMPLEXREGION && !((infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) && j.nItem == 0))
5008 rcItem.top = 0;
5009 rcItem.bottom = infoPtr->nItemHeight;
5010 OffsetRect(&rcItem, Origin.x, Position.y);
5011 if (!RectVisible(hdc, &rcItem)) continue;
5014 ranges_additem(subitems, j.nItem);
5017 iterator_rangesitems(&k, subitems);
5018 LISTVIEW_DrawItem(infoPtr, hdc, i->nItem, &k, Position, cdmode);
5019 iterator_destroy(&k);
5021 iterator_destroy(&j);
5024 /***
5025 * DESCRIPTION:
5026 * Draws the gridlines if necessary when in report display mode.
5028 * PARAMETER(S):
5029 * [I] infoPtr : valid pointer to the listview structure
5030 * [I] hdc : device context handle
5032 * RETURN:
5033 * None
5035 static void LISTVIEW_RefreshReportGrid(LISTVIEW_INFO *infoPtr, HDC hdc)
5037 INT rgntype;
5038 INT y, itemheight;
5039 INT col, index;
5040 HPEN hPen, hOldPen;
5041 RECT rcClip, rcItem = {0};
5042 POINT Origin;
5043 RANGES colRanges;
5044 ITERATOR j;
5045 BOOL rmost = FALSE;
5047 TRACE("()\n");
5049 /* figure out what to draw */
5050 rgntype = GetClipBox(hdc, &rcClip);
5051 if (rgntype == NULLREGION) return;
5053 /* Get scroll info once before loop */
5054 LISTVIEW_GetOrigin(infoPtr, &Origin);
5056 colRanges = ranges_create(DPA_GetPtrCount(infoPtr->hdpaColumns));
5058 /* narrow down the columns we need to paint */
5059 for(col = 0; col < DPA_GetPtrCount(infoPtr->hdpaColumns); col++)
5061 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, col, 0);
5063 LISTVIEW_GetHeaderRect(infoPtr, index, &rcItem);
5064 if ((rcItem.right + Origin.x >= rcClip.left) && (rcItem.left + Origin.x < rcClip.right))
5065 ranges_additem(colRanges, index);
5068 /* is right most vertical line visible? */
5069 if (DPA_GetPtrCount(infoPtr->hdpaColumns) > 0)
5071 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, 0);
5072 LISTVIEW_GetHeaderRect(infoPtr, index, &rcItem);
5073 rmost = (rcItem.right + Origin.x < rcClip.right);
5076 if ((hPen = CreatePen( PS_SOLID, 1, comctl32_color.clr3dFace )))
5078 hOldPen = SelectObject ( hdc, hPen );
5080 /* draw the vertical lines for the columns */
5081 iterator_rangesitems(&j, colRanges);
5082 while(iterator_next(&j))
5084 LISTVIEW_GetHeaderRect(infoPtr, j.nItem, &rcItem);
5085 if (rcItem.left == 0) continue; /* skip leftmost column */
5086 rcItem.left += Origin.x;
5087 rcItem.right += Origin.x;
5088 rcItem.top = infoPtr->rcList.top;
5089 rcItem.bottom = infoPtr->rcList.bottom;
5090 TRACE("vert col=%d, rcItem=%s\n", j.nItem, wine_dbgstr_rect(&rcItem));
5091 MoveToEx (hdc, rcItem.left, rcItem.top, NULL);
5092 LineTo (hdc, rcItem.left, rcItem.bottom);
5094 iterator_destroy(&j);
5095 /* draw rightmost grid line if visible */
5096 if (rmost)
5098 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX,
5099 DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, 0);
5100 LISTVIEW_GetHeaderRect(infoPtr, index, &rcItem);
5102 rcItem.right += Origin.x;
5104 MoveToEx (hdc, rcItem.right, infoPtr->rcList.top, NULL);
5105 LineTo (hdc, rcItem.right, infoPtr->rcList.bottom);
5108 /* draw the horizontal lines for the rows */
5109 itemheight = LISTVIEW_CalculateItemHeight(infoPtr);
5110 rcItem.left = infoPtr->rcList.left;
5111 rcItem.right = infoPtr->rcList.right;
5112 for(y = Origin.y > 1 ? Origin.y - 1 : itemheight - 1 + Origin.y % itemheight; y<=infoPtr->rcList.bottom; y+=itemheight)
5114 rcItem.bottom = rcItem.top = y;
5115 TRACE("horz rcItem=%s\n", wine_dbgstr_rect(&rcItem));
5116 MoveToEx (hdc, rcItem.left, rcItem.top, NULL);
5117 LineTo (hdc, rcItem.right, rcItem.top);
5120 SelectObject( hdc, hOldPen );
5121 DeleteObject( hPen );
5123 else
5124 ranges_destroy(colRanges);
5127 /***
5128 * DESCRIPTION:
5129 * Draws listview items when in list display mode.
5131 * PARAMETER(S):
5132 * [I] infoPtr : valid pointer to the listview structure
5133 * [I] hdc : device context handle
5134 * [I] cdmode : custom draw mode
5136 * RETURN:
5137 * None
5139 static void LISTVIEW_RefreshList(LISTVIEW_INFO *infoPtr, ITERATOR *i, HDC hdc, DWORD cdmode)
5141 POINT Origin, Position;
5143 /* Get scroll info once before loop */
5144 LISTVIEW_GetOrigin(infoPtr, &Origin);
5146 while(iterator_prev(i))
5148 SelectObject(hdc, infoPtr->hFont);
5149 LISTVIEW_GetItemOrigin(infoPtr, i->nItem, &Position);
5150 Position.x += Origin.x;
5151 Position.y += Origin.y;
5153 LISTVIEW_DrawItem(infoPtr, hdc, i->nItem, NULL, Position, cdmode);
5158 /***
5159 * DESCRIPTION:
5160 * Draws listview items.
5162 * PARAMETER(S):
5163 * [I] infoPtr : valid pointer to the listview structure
5164 * [I] hdc : device context handle
5165 * [I] prcErase : rect to be erased before refresh (may be NULL)
5167 * RETURN:
5168 * NoneX
5170 static void LISTVIEW_Refresh(LISTVIEW_INFO *infoPtr, HDC hdc, const RECT *prcErase)
5172 COLORREF oldTextColor = 0, oldBkColor = 0;
5173 NMLVCUSTOMDRAW nmlvcd;
5174 HFONT hOldFont = 0;
5175 DWORD cdmode;
5176 INT oldBkMode = 0;
5177 RECT rcClient;
5178 ITERATOR i;
5179 HDC hdcOrig = hdc;
5180 HBITMAP hbmp = NULL;
5181 RANGE range;
5183 LISTVIEW_DUMP(infoPtr);
5185 if (infoPtr->dwLvExStyle & LVS_EX_DOUBLEBUFFER) {
5186 TRACE("double buffering\n");
5188 hdc = CreateCompatibleDC(hdcOrig);
5189 if (!hdc) {
5190 ERR("Failed to create DC for backbuffer\n");
5191 return;
5193 hbmp = CreateCompatibleBitmap(hdcOrig, infoPtr->rcList.right,
5194 infoPtr->rcList.bottom);
5195 if (!hbmp) {
5196 ERR("Failed to create bitmap for backbuffer\n");
5197 DeleteDC(hdc);
5198 return;
5201 SelectObject(hdc, hbmp);
5202 SelectObject(hdc, infoPtr->hFont);
5204 if(GetClipBox(hdcOrig, &rcClient))
5205 IntersectClipRect(hdc, rcClient.left, rcClient.top, rcClient.right, rcClient.bottom);
5206 } else {
5207 /* Save dc values we're gonna trash while drawing
5208 * FIXME: Should be done in LISTVIEW_DrawItem() */
5209 hOldFont = SelectObject(hdc, infoPtr->hFont);
5210 oldBkMode = GetBkMode(hdc);
5211 oldBkColor = GetBkColor(hdc);
5212 oldTextColor = GetTextColor(hdc);
5215 infoPtr->bIsDrawing = TRUE;
5217 if (prcErase) {
5218 LISTVIEW_FillBkgnd(infoPtr, hdc, prcErase);
5219 } else if (infoPtr->dwLvExStyle & LVS_EX_DOUBLEBUFFER) {
5220 /* If no erasing was done (usually because RedrawWindow was called
5221 * with RDW_INVALIDATE only) we need to copy the old contents into
5222 * the backbuffer before continuing. */
5223 BitBlt(hdc, infoPtr->rcList.left, infoPtr->rcList.top,
5224 infoPtr->rcList.right - infoPtr->rcList.left,
5225 infoPtr->rcList.bottom - infoPtr->rcList.top,
5226 hdcOrig, infoPtr->rcList.left, infoPtr->rcList.top, SRCCOPY);
5229 GetClientRect(infoPtr->hwndSelf, &rcClient);
5230 customdraw_fill(&nmlvcd, infoPtr, hdc, &rcClient, 0);
5231 cdmode = notify_customdraw(infoPtr, CDDS_PREPAINT, &nmlvcd);
5232 if (cdmode & CDRF_SKIPDEFAULT) goto enddraw;
5234 /* nothing to draw */
5235 if(infoPtr->nItemCount == 0) goto enddraw;
5237 /* figure out what we need to draw */
5238 iterator_visibleitems(&i, infoPtr, hdc);
5239 range = iterator_range(&i);
5241 /* send cache hint notification */
5242 if (infoPtr->dwStyle & LVS_OWNERDATA)
5244 NMLVCACHEHINT nmlv;
5246 ZeroMemory(&nmlv, sizeof(NMLVCACHEHINT));
5247 nmlv.iFrom = range.lower;
5248 nmlv.iTo = range.upper - 1;
5249 notify_hdr(infoPtr, LVN_ODCACHEHINT, &nmlv.hdr);
5252 if ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (infoPtr->uView == LV_VIEW_DETAILS))
5253 LISTVIEW_RefreshOwnerDraw(infoPtr, &i, hdc, cdmode);
5254 else
5256 if (infoPtr->uView == LV_VIEW_DETAILS)
5257 LISTVIEW_RefreshReport(infoPtr, &i, hdc, cdmode);
5258 else /* LV_VIEW_LIST, LV_VIEW_ICON or LV_VIEW_SMALLICON */
5259 LISTVIEW_RefreshList(infoPtr, &i, hdc, cdmode);
5261 /* if we have a focus rect and it's visible, draw it */
5262 if (infoPtr->bFocus && range.lower <= infoPtr->nFocusedItem &&
5263 (range.upper - 1) >= infoPtr->nFocusedItem)
5264 LISTVIEW_DrawFocusRect(infoPtr, hdc);
5266 iterator_destroy(&i);
5268 enddraw:
5269 /* For LVS_EX_GRIDLINES go and draw lines */
5270 /* This includes the case where there were *no* items */
5271 if ((infoPtr->uView == LV_VIEW_DETAILS) && infoPtr->dwLvExStyle & LVS_EX_GRIDLINES)
5272 LISTVIEW_RefreshReportGrid(infoPtr, hdc);
5274 /* Draw marquee rectangle if appropriate */
5275 if (infoPtr->bMarqueeSelect)
5276 DrawFocusRect(hdc, &infoPtr->marqueeDrawRect);
5278 if (cdmode & CDRF_NOTIFYPOSTPAINT)
5279 notify_postpaint(infoPtr, &nmlvcd);
5281 if(hbmp) {
5282 BitBlt(hdcOrig, infoPtr->rcList.left, infoPtr->rcList.top,
5283 infoPtr->rcList.right - infoPtr->rcList.left,
5284 infoPtr->rcList.bottom - infoPtr->rcList.top,
5285 hdc, infoPtr->rcList.left, infoPtr->rcList.top, SRCCOPY);
5287 DeleteObject(hbmp);
5288 DeleteDC(hdc);
5289 } else {
5290 SelectObject(hdc, hOldFont);
5291 SetBkMode(hdc, oldBkMode);
5292 SetBkColor(hdc, oldBkColor);
5293 SetTextColor(hdc, oldTextColor);
5296 infoPtr->bIsDrawing = FALSE;
5300 /***
5301 * DESCRIPTION:
5302 * Calculates the approximate width and height of a given number of items.
5304 * PARAMETER(S):
5305 * [I] infoPtr : valid pointer to the listview structure
5306 * [I] nItemCount : number of items
5307 * [I] wWidth : width
5308 * [I] wHeight : height
5310 * RETURN:
5311 * Returns a DWORD. The width in the low word and the height in high word.
5313 static DWORD LISTVIEW_ApproximateViewRect(const LISTVIEW_INFO *infoPtr, INT nItemCount,
5314 WORD wWidth, WORD wHeight)
5316 DWORD dwViewRect = 0;
5318 if (nItemCount == -1)
5319 nItemCount = infoPtr->nItemCount;
5321 if (infoPtr->uView == LV_VIEW_LIST)
5323 INT nItemCountPerColumn = 1;
5324 INT nColumnCount = 0;
5326 if (wHeight == 0xFFFF)
5328 /* use current height */
5329 wHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
5332 if (wHeight < infoPtr->nItemHeight)
5333 wHeight = infoPtr->nItemHeight;
5335 if (nItemCount > 0)
5337 if (infoPtr->nItemHeight > 0)
5339 nItemCountPerColumn = wHeight / infoPtr->nItemHeight;
5340 if (nItemCountPerColumn == 0)
5341 nItemCountPerColumn = 1;
5343 if (nItemCount % nItemCountPerColumn != 0)
5344 nColumnCount = nItemCount / nItemCountPerColumn;
5345 else
5346 nColumnCount = nItemCount / nItemCountPerColumn + 1;
5350 /* Microsoft padding magic */
5351 wHeight = nItemCountPerColumn * infoPtr->nItemHeight + 2;
5352 wWidth = nColumnCount * infoPtr->nItemWidth + 2;
5354 dwViewRect = MAKELONG(wWidth, wHeight);
5356 else if (infoPtr->uView == LV_VIEW_DETAILS)
5358 RECT rcBox;
5360 if (infoPtr->nItemCount > 0)
5362 LISTVIEW_GetItemBox(infoPtr, 0, &rcBox);
5363 wWidth = rcBox.right - rcBox.left;
5364 wHeight = (rcBox.bottom - rcBox.top) * nItemCount;
5366 else
5368 /* use current height and width */
5369 if (wHeight == 0xffff)
5370 wHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
5371 if (wWidth == 0xffff)
5372 wWidth = infoPtr->rcList.right - infoPtr->rcList.left;
5375 dwViewRect = MAKELONG(wWidth, wHeight);
5377 else if (infoPtr->uView == LV_VIEW_ICON)
5379 UINT rows,cols;
5380 UINT nItemWidth;
5381 UINT nItemHeight;
5383 nItemWidth = infoPtr->iconSpacing.cx;
5384 nItemHeight = infoPtr->iconSpacing.cy;
5386 if (wWidth == 0xffff)
5387 wWidth = infoPtr->rcList.right - infoPtr->rcList.left;
5389 if (wWidth < nItemWidth)
5390 wWidth = nItemWidth;
5392 cols = wWidth / nItemWidth;
5393 if (cols > nItemCount)
5394 cols = nItemCount;
5395 if (cols < 1)
5396 cols = 1;
5398 if (nItemCount)
5400 rows = nItemCount / cols;
5401 if (nItemCount % cols)
5402 rows++;
5404 else
5405 rows = 0;
5407 wHeight = (nItemHeight * rows)+2;
5408 wWidth = (nItemWidth * cols)+2;
5410 dwViewRect = MAKELONG(wWidth, wHeight);
5412 else if (infoPtr->uView == LV_VIEW_SMALLICON)
5413 FIXME("uView == LV_VIEW_SMALLICON: not implemented\n");
5415 return dwViewRect;
5418 /***
5419 * DESCRIPTION:
5420 * Cancel edit label with saving item text.
5422 * PARAMETER(S):
5423 * [I] infoPtr : valid pointer to the listview structure
5425 * RETURN:
5426 * Always returns TRUE.
5428 static LRESULT LISTVIEW_CancelEditLabel(LISTVIEW_INFO *infoPtr)
5430 if (infoPtr->hwndEdit)
5432 /* handle value will be lost after LISTVIEW_EndEditLabelT */
5433 HWND edit = infoPtr->hwndEdit;
5435 LISTVIEW_EndEditLabelT(infoPtr, TRUE, IsWindowUnicode(infoPtr->hwndEdit));
5436 SendMessageW(edit, WM_CLOSE, 0, 0);
5439 return TRUE;
5442 /***
5443 * DESCRIPTION:
5444 * Create a drag image list for the specified item.
5446 * PARAMETER(S):
5447 * [I] infoPtr : valid pointer to the listview structure
5448 * [I] iItem : index of item
5449 * [O] lppt : Upper-left corner of the image
5451 * RETURN:
5452 * Returns a handle to the image list if successful, NULL otherwise.
5454 static HIMAGELIST LISTVIEW_CreateDragImage(LISTVIEW_INFO *infoPtr, INT iItem, LPPOINT lppt)
5456 RECT rcItem;
5457 SIZE size;
5458 POINT pos;
5459 HDC hdc, hdcOrig;
5460 HBITMAP hbmp, hOldbmp;
5461 HFONT hOldFont;
5462 HIMAGELIST dragList = 0;
5463 TRACE("iItem=%d Count=%d\n", iItem, infoPtr->nItemCount);
5465 if (iItem < 0 || iItem >= infoPtr->nItemCount || !lppt)
5466 return 0;
5468 rcItem.left = LVIR_BOUNDS;
5469 if (!LISTVIEW_GetItemRect(infoPtr, iItem, &rcItem))
5470 return 0;
5472 lppt->x = rcItem.left;
5473 lppt->y = rcItem.top;
5475 size.cx = rcItem.right - rcItem.left;
5476 size.cy = rcItem.bottom - rcItem.top;
5478 hdcOrig = GetDC(infoPtr->hwndSelf);
5479 hdc = CreateCompatibleDC(hdcOrig);
5480 hbmp = CreateCompatibleBitmap(hdcOrig, size.cx, size.cy);
5481 hOldbmp = SelectObject(hdc, hbmp);
5482 hOldFont = SelectObject(hdc, infoPtr->hFont);
5484 SetRect(&rcItem, 0, 0, size.cx, size.cy);
5485 FillRect(hdc, &rcItem, infoPtr->hBkBrush);
5487 pos.x = pos.y = 0;
5488 if (LISTVIEW_DrawItem(infoPtr, hdc, iItem, NULL, pos, CDRF_DODEFAULT))
5490 dragList = ImageList_Create(size.cx, size.cy, ILC_COLOR, 10, 10);
5491 SelectObject(hdc, hOldbmp);
5492 ImageList_Add(dragList, hbmp, 0);
5494 else
5495 SelectObject(hdc, hOldbmp);
5497 SelectObject(hdc, hOldFont);
5498 DeleteObject(hbmp);
5499 DeleteDC(hdc);
5500 ReleaseDC(infoPtr->hwndSelf, hdcOrig);
5502 TRACE("ret=%p\n", dragList);
5504 return dragList;
5508 /***
5509 * DESCRIPTION:
5510 * Removes all listview items and subitems.
5512 * PARAMETER(S):
5513 * [I] infoPtr : valid pointer to the listview structure
5515 * RETURN:
5516 * SUCCESS : TRUE
5517 * FAILURE : FALSE
5519 static BOOL LISTVIEW_DeleteAllItems(LISTVIEW_INFO *infoPtr, BOOL destroy)
5521 HDPA hdpaSubItems = NULL;
5522 BOOL suppress = FALSE;
5523 ITEMHDR *hdrItem;
5524 ITEM_INFO *lpItem;
5525 ITEM_ID *lpID;
5526 INT i, j;
5528 TRACE("()\n");
5530 /* we do it directly, to avoid notifications */
5531 ranges_clear(infoPtr->selectionRanges);
5532 infoPtr->nSelectionMark = -1;
5533 infoPtr->nFocusedItem = -1;
5534 SetRectEmpty(&infoPtr->rcFocus);
5535 /* But we are supposed to leave nHotItem as is! */
5537 /* send LVN_DELETEALLITEMS notification */
5538 if (!(infoPtr->dwStyle & LVS_OWNERDATA) || !destroy)
5540 NMLISTVIEW nmlv;
5542 memset(&nmlv, 0, sizeof(NMLISTVIEW));
5543 nmlv.iItem = -1;
5544 suppress = notify_listview(infoPtr, LVN_DELETEALLITEMS, &nmlv);
5547 for (i = infoPtr->nItemCount - 1; i >= 0; i--)
5549 if (!(infoPtr->dwStyle & LVS_OWNERDATA))
5551 /* send LVN_DELETEITEM notification, if not suppressed
5552 and if it is not a virtual listview */
5553 if (!suppress) notify_deleteitem(infoPtr, i);
5554 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, i);
5555 lpItem = DPA_GetPtr(hdpaSubItems, 0);
5556 /* free id struct */
5557 j = DPA_GetPtrIndex(infoPtr->hdpaItemIds, lpItem->id);
5558 lpID = DPA_GetPtr(infoPtr->hdpaItemIds, j);
5559 DPA_DeletePtr(infoPtr->hdpaItemIds, j);
5560 Free(lpID);
5561 /* both item and subitem start with ITEMHDR header */
5562 for (j = 0; j < DPA_GetPtrCount(hdpaSubItems); j++)
5564 hdrItem = DPA_GetPtr(hdpaSubItems, j);
5565 if (is_text(hdrItem->pszText)) Free(hdrItem->pszText);
5566 Free(hdrItem);
5568 DPA_Destroy(hdpaSubItems);
5569 DPA_DeletePtr(infoPtr->hdpaItems, i);
5571 DPA_DeletePtr(infoPtr->hdpaPosX, i);
5572 DPA_DeletePtr(infoPtr->hdpaPosY, i);
5573 infoPtr->nItemCount --;
5576 if (!destroy)
5578 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
5579 LISTVIEW_UpdateScroll(infoPtr);
5581 LISTVIEW_InvalidateList(infoPtr);
5583 return TRUE;
5586 /***
5587 * DESCRIPTION:
5588 * Scrolls, and updates the columns, when a column is changing width.
5590 * PARAMETER(S):
5591 * [I] infoPtr : valid pointer to the listview structure
5592 * [I] nColumn : column to scroll
5593 * [I] dx : amount of scroll, in pixels
5595 * RETURN:
5596 * None.
5598 static void LISTVIEW_ScrollColumns(LISTVIEW_INFO *infoPtr, INT nColumn, INT dx)
5600 COLUMN_INFO *lpColumnInfo;
5601 RECT rcOld, rcCol;
5602 POINT ptOrigin;
5603 INT nCol;
5604 HDITEMW hdi;
5606 if (nColumn < 0 || DPA_GetPtrCount(infoPtr->hdpaColumns) < 1) return;
5607 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, min(nColumn, DPA_GetPtrCount(infoPtr->hdpaColumns) - 1));
5608 rcCol = lpColumnInfo->rcHeader;
5609 if (nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns))
5610 rcCol.left = rcCol.right;
5612 /* adjust the other columns */
5613 hdi.mask = HDI_ORDER;
5614 if (SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nColumn, (LPARAM)&hdi))
5616 INT nOrder = hdi.iOrder;
5617 for (nCol = 0; nCol < DPA_GetPtrCount(infoPtr->hdpaColumns); nCol++)
5619 hdi.mask = HDI_ORDER;
5620 SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nCol, (LPARAM)&hdi);
5621 if (hdi.iOrder >= nOrder) {
5622 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, nCol);
5623 lpColumnInfo->rcHeader.left += dx;
5624 lpColumnInfo->rcHeader.right += dx;
5629 /* do not update screen if not in report mode */
5630 if (!is_redrawing(infoPtr) || infoPtr->uView != LV_VIEW_DETAILS) return;
5632 /* Need to reset the item width when inserting a new column */
5633 infoPtr->nItemWidth += dx;
5635 LISTVIEW_UpdateScroll(infoPtr);
5636 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
5638 /* scroll to cover the deleted column, and invalidate for redraw */
5639 rcOld = infoPtr->rcList;
5640 rcOld.left = ptOrigin.x + rcCol.left + dx;
5641 ScrollWindowEx(infoPtr->hwndSelf, dx, 0, &rcOld, &rcOld, 0, 0, SW_ERASE | SW_INVALIDATE);
5644 /***
5645 * DESCRIPTION:
5646 * Removes a column from the listview control.
5648 * PARAMETER(S):
5649 * [I] infoPtr : valid pointer to the listview structure
5650 * [I] nColumn : column index
5652 * RETURN:
5653 * SUCCESS : TRUE
5654 * FAILURE : FALSE
5656 static BOOL LISTVIEW_DeleteColumn(LISTVIEW_INFO *infoPtr, INT nColumn)
5658 RECT rcCol;
5660 TRACE("nColumn=%d\n", nColumn);
5662 if (nColumn < 0 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns))
5663 return FALSE;
5665 /* While the MSDN specifically says that column zero should not be deleted,
5666 what actually happens is that the column itself is deleted but no items or subitems
5667 are removed.
5670 LISTVIEW_GetHeaderRect(infoPtr, nColumn, &rcCol);
5672 if (!SendMessageW(infoPtr->hwndHeader, HDM_DELETEITEM, nColumn, 0))
5673 return FALSE;
5675 Free(DPA_GetPtr(infoPtr->hdpaColumns, nColumn));
5676 DPA_DeletePtr(infoPtr->hdpaColumns, nColumn);
5678 if (!(infoPtr->dwStyle & LVS_OWNERDATA) && nColumn)
5680 SUBITEM_INFO *lpSubItem, *lpDelItem;
5681 HDPA hdpaSubItems;
5682 INT nItem, nSubItem, i;
5684 for (nItem = 0; nItem < infoPtr->nItemCount; nItem++)
5686 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, nItem);
5687 nSubItem = 0;
5688 lpDelItem = 0;
5689 for (i = 1; i < DPA_GetPtrCount(hdpaSubItems); i++)
5691 lpSubItem = DPA_GetPtr(hdpaSubItems, i);
5692 if (lpSubItem->iSubItem == nColumn)
5694 nSubItem = i;
5695 lpDelItem = lpSubItem;
5697 else if (lpSubItem->iSubItem > nColumn)
5699 lpSubItem->iSubItem--;
5703 /* if we found our subitem, zap it */
5704 if (nSubItem > 0)
5706 /* free string */
5707 if (is_text(lpDelItem->hdr.pszText))
5708 Free(lpDelItem->hdr.pszText);
5710 /* free item */
5711 Free(lpDelItem);
5713 /* free dpa memory */
5714 DPA_DeletePtr(hdpaSubItems, nSubItem);
5719 /* update the other column info */
5720 if(DPA_GetPtrCount(infoPtr->hdpaColumns) == 0)
5721 LISTVIEW_InvalidateList(infoPtr);
5722 else
5723 LISTVIEW_ScrollColumns(infoPtr, nColumn, -(rcCol.right - rcCol.left));
5724 LISTVIEW_UpdateItemSize(infoPtr);
5726 return TRUE;
5729 /***
5730 * DESCRIPTION:
5731 * Invalidates the listview after an item's insertion or deletion.
5733 * PARAMETER(S):
5734 * [I] infoPtr : valid pointer to the listview structure
5735 * [I] nItem : item index
5736 * [I] dir : -1 if deleting, 1 if inserting
5738 * RETURN:
5739 * None
5741 static void LISTVIEW_ScrollOnInsert(LISTVIEW_INFO *infoPtr, INT nItem, INT dir)
5743 INT nPerCol, nItemCol, nItemRow;
5744 RECT rcScroll;
5745 POINT Origin;
5747 /* if we don't refresh, what's the point of scrolling? */
5748 if (!is_redrawing(infoPtr)) return;
5750 assert (abs(dir) == 1);
5752 /* arrange icons if autoarrange is on */
5753 if (is_autoarrange(infoPtr))
5755 BOOL arrange = TRUE;
5756 if (dir < 0 && nItem >= infoPtr->nItemCount) arrange = FALSE;
5757 if (dir > 0 && nItem == infoPtr->nItemCount - 1) arrange = FALSE;
5758 if (arrange) LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
5761 /* scrollbars need updating */
5762 LISTVIEW_UpdateScroll(infoPtr);
5764 /* figure out the item's position */
5765 if (infoPtr->uView == LV_VIEW_DETAILS)
5766 nPerCol = infoPtr->nItemCount + 1;
5767 else if (infoPtr->uView == LV_VIEW_LIST)
5768 nPerCol = LISTVIEW_GetCountPerColumn(infoPtr);
5769 else /* LV_VIEW_ICON, or LV_VIEW_SMALLICON */
5770 return;
5772 nItemCol = nItem / nPerCol;
5773 nItemRow = nItem % nPerCol;
5774 LISTVIEW_GetOrigin(infoPtr, &Origin);
5776 /* move the items below up a slot */
5777 rcScroll.left = nItemCol * infoPtr->nItemWidth;
5778 rcScroll.top = nItemRow * infoPtr->nItemHeight;
5779 rcScroll.right = rcScroll.left + infoPtr->nItemWidth;
5780 rcScroll.bottom = nPerCol * infoPtr->nItemHeight;
5781 OffsetRect(&rcScroll, Origin.x, Origin.y);
5782 TRACE("rcScroll=%s, dx=%d\n", wine_dbgstr_rect(&rcScroll), dir * infoPtr->nItemHeight);
5783 if (IntersectRect(&rcScroll, &rcScroll, &infoPtr->rcList))
5785 TRACE("Invalidating rcScroll=%s, rcList=%s\n", wine_dbgstr_rect(&rcScroll), wine_dbgstr_rect(&infoPtr->rcList));
5786 InvalidateRect(infoPtr->hwndSelf, &rcScroll, TRUE);
5789 /* report has only that column, so we're done */
5790 if (infoPtr->uView == LV_VIEW_DETAILS) return;
5792 /* now for LISTs, we have to deal with the columns to the right */
5793 SetRect(&rcScroll, (nItemCol + 1) * infoPtr->nItemWidth, 0,
5794 (infoPtr->nItemCount / nPerCol + 1) * infoPtr->nItemWidth,
5795 nPerCol * infoPtr->nItemHeight);
5796 OffsetRect(&rcScroll, Origin.x, Origin.y);
5797 if (IntersectRect(&rcScroll, &rcScroll, &infoPtr->rcList))
5798 InvalidateRect(infoPtr->hwndSelf, &rcScroll, TRUE);
5801 /***
5802 * DESCRIPTION:
5803 * Removes an item from the listview control.
5805 * PARAMETER(S):
5806 * [I] infoPtr : valid pointer to the listview structure
5807 * [I] nItem : item index
5809 * RETURN:
5810 * SUCCESS : TRUE
5811 * FAILURE : FALSE
5813 static BOOL LISTVIEW_DeleteItem(LISTVIEW_INFO *infoPtr, INT nItem)
5815 LVITEMW item;
5816 const BOOL is_icon = (infoPtr->uView == LV_VIEW_SMALLICON || infoPtr->uView == LV_VIEW_ICON);
5817 INT focus = infoPtr->nFocusedItem;
5819 TRACE("(nItem=%d)\n", nItem);
5821 if (nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
5823 /* remove selection, and focus */
5824 item.state = 0;
5825 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
5826 LISTVIEW_SetItemState(infoPtr, nItem, &item);
5828 /* send LVN_DELETEITEM notification. */
5829 if (!notify_deleteitem(infoPtr, nItem)) return FALSE;
5831 /* we need to do this here, because we'll be deleting stuff */
5832 if (is_icon)
5833 LISTVIEW_InvalidateItem(infoPtr, nItem);
5835 if (!(infoPtr->dwStyle & LVS_OWNERDATA))
5837 HDPA hdpaSubItems;
5838 ITEMHDR *hdrItem;
5839 ITEM_INFO *lpItem;
5840 ITEM_ID *lpID;
5841 INT i;
5843 hdpaSubItems = DPA_DeletePtr(infoPtr->hdpaItems, nItem);
5844 lpItem = DPA_GetPtr(hdpaSubItems, 0);
5846 /* free id struct */
5847 i = DPA_GetPtrIndex(infoPtr->hdpaItemIds, lpItem->id);
5848 lpID = DPA_GetPtr(infoPtr->hdpaItemIds, i);
5849 DPA_DeletePtr(infoPtr->hdpaItemIds, i);
5850 Free(lpID);
5851 for (i = 0; i < DPA_GetPtrCount(hdpaSubItems); i++)
5853 hdrItem = DPA_GetPtr(hdpaSubItems, i);
5854 if (is_text(hdrItem->pszText)) Free(hdrItem->pszText);
5855 Free(hdrItem);
5857 DPA_Destroy(hdpaSubItems);
5860 if (is_icon)
5862 DPA_DeletePtr(infoPtr->hdpaPosX, nItem);
5863 DPA_DeletePtr(infoPtr->hdpaPosY, nItem);
5866 infoPtr->nItemCount--;
5867 LISTVIEW_ShiftIndices(infoPtr, nItem, -1);
5868 LISTVIEW_ShiftFocus(infoPtr, focus, nItem, -1);
5870 /* now is the invalidation fun */
5871 if (!is_icon)
5872 LISTVIEW_ScrollOnInsert(infoPtr, nItem, -1);
5873 return TRUE;
5877 /***
5878 * DESCRIPTION:
5879 * Callback implementation for editlabel control
5881 * PARAMETER(S):
5882 * [I] infoPtr : valid pointer to the listview structure
5883 * [I] storeText : store edit box text as item text
5884 * [I] isW : TRUE if psxText is Unicode, FALSE if it's ANSI
5886 * RETURN:
5887 * SUCCESS : TRUE
5888 * FAILURE : FALSE
5890 static BOOL LISTVIEW_EndEditLabelT(LISTVIEW_INFO *infoPtr, BOOL storeText, BOOL isW)
5892 HWND hwndSelf = infoPtr->hwndSelf;
5893 WCHAR szDispText[DISP_TEXT_SIZE] = { 0 };
5894 NMLVDISPINFOW dispInfo;
5895 INT editedItem = infoPtr->nEditLabelItem;
5896 BOOL same;
5897 WCHAR *pszText = NULL;
5898 BOOL res;
5900 if (storeText)
5902 DWORD len = isW ? GetWindowTextLengthW(infoPtr->hwndEdit) : GetWindowTextLengthA(infoPtr->hwndEdit);
5904 if (len++)
5906 if (!(pszText = Alloc(len * (isW ? sizeof(WCHAR) : sizeof(CHAR)))))
5907 return FALSE;
5909 if (isW)
5910 GetWindowTextW(infoPtr->hwndEdit, pszText, len);
5911 else
5912 GetWindowTextA(infoPtr->hwndEdit, (CHAR*)pszText, len);
5916 TRACE("(pszText=%s, isW=%d)\n", debugtext_t(pszText, isW), isW);
5918 ZeroMemory(&dispInfo, sizeof(dispInfo));
5919 dispInfo.item.mask = LVIF_PARAM | LVIF_STATE | LVIF_TEXT;
5920 dispInfo.item.iItem = editedItem;
5921 dispInfo.item.iSubItem = 0;
5922 dispInfo.item.stateMask = ~0;
5923 dispInfo.item.pszText = szDispText;
5924 dispInfo.item.cchTextMax = DISP_TEXT_SIZE;
5925 if (!LISTVIEW_GetItemT(infoPtr, &dispInfo.item, isW))
5927 res = FALSE;
5928 goto cleanup;
5931 if (isW)
5932 same = (lstrcmpW(dispInfo.item.pszText, pszText) == 0);
5933 else
5935 LPWSTR tmp = textdupTtoW(pszText, FALSE);
5936 same = (lstrcmpW(dispInfo.item.pszText, tmp) == 0);
5937 textfreeT(tmp, FALSE);
5940 /* add the text from the edit in */
5941 dispInfo.item.mask |= LVIF_TEXT;
5942 dispInfo.item.pszText = same ? NULL : pszText;
5943 dispInfo.item.cchTextMax = textlenT(dispInfo.item.pszText, isW);
5945 infoPtr->notify_mask &= ~NOTIFY_MASK_END_LABEL_EDIT;
5947 /* Do we need to update the Item Text */
5948 res = notify_dispinfoT(infoPtr, LVN_ENDLABELEDITW, &dispInfo, isW);
5950 infoPtr->notify_mask |= NOTIFY_MASK_END_LABEL_EDIT;
5952 infoPtr->nEditLabelItem = -1;
5953 infoPtr->hwndEdit = 0;
5955 if (!res) goto cleanup;
5957 if (!IsWindow(hwndSelf))
5959 res = FALSE;
5960 goto cleanup;
5962 if (!pszText) return TRUE;
5963 if (same)
5965 res = TRUE;
5966 goto cleanup;
5969 if (!(infoPtr->dwStyle & LVS_OWNERDATA))
5971 HDPA hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, editedItem);
5972 ITEM_INFO* lpItem = DPA_GetPtr(hdpaSubItems, 0);
5973 if (lpItem && lpItem->hdr.pszText == LPSTR_TEXTCALLBACKW)
5975 LISTVIEW_InvalidateItem(infoPtr, editedItem);
5976 res = TRUE;
5977 goto cleanup;
5981 ZeroMemory(&dispInfo, sizeof(dispInfo));
5982 dispInfo.item.mask = LVIF_TEXT;
5983 dispInfo.item.iItem = editedItem;
5984 dispInfo.item.iSubItem = 0;
5985 dispInfo.item.pszText = pszText;
5986 dispInfo.item.cchTextMax = textlenT(pszText, isW);
5987 res = LISTVIEW_SetItemT(infoPtr, &dispInfo.item, isW);
5989 cleanup:
5990 Free(pszText);
5992 return res;
5995 static LRESULT EditLblWndProcT(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL isW)
5997 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongPtrW(GetParent(hwnd), 0);
5998 BOOL save = TRUE;
6000 TRACE("hwnd %p, uMsg %x, wParam %Ix, lParam %Ix, isW %d\n", hwnd, uMsg, wParam, lParam, isW);
6002 switch (uMsg)
6004 case WM_GETDLGCODE:
6005 return DLGC_WANTARROWS | DLGC_WANTALLKEYS;
6007 case WM_DESTROY:
6009 WNDPROC editProc = infoPtr->EditWndProc;
6010 infoPtr->EditWndProc = 0;
6011 SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (DWORD_PTR)editProc);
6012 return CallWindowProcT(editProc, hwnd, uMsg, wParam, lParam, isW);
6015 case WM_KEYDOWN:
6016 if (VK_ESCAPE == (INT)wParam)
6018 save = FALSE;
6019 break;
6021 else if (VK_RETURN == (INT)wParam)
6022 break;
6024 default:
6025 return CallWindowProcT(infoPtr->EditWndProc, hwnd, uMsg, wParam, lParam, isW);
6028 /* kill the edit */
6029 if (infoPtr->hwndEdit)
6030 LISTVIEW_EndEditLabelT(infoPtr, save, isW);
6032 SendMessageW(hwnd, WM_CLOSE, 0, 0);
6033 return 0;
6036 static LRESULT CALLBACK EditLblWndProcW(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
6038 return EditLblWndProcT(hwnd, uMsg, wParam, lParam, TRUE);
6041 static LRESULT CALLBACK EditLblWndProcA(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
6043 return EditLblWndProcT(hwnd, uMsg, wParam, lParam, FALSE);
6046 static HWND CreateEditLabelT(LISTVIEW_INFO *infoPtr, LPCWSTR text, BOOL isW)
6048 static const DWORD style = WS_CHILDWINDOW|WS_CLIPSIBLINGS|ES_LEFT|ES_AUTOHSCROLL|WS_BORDER|WS_VISIBLE;
6049 HINSTANCE hinst = (HINSTANCE)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_HINSTANCE);
6050 HWND hedit;
6052 TRACE("(%p, text=%s, isW=%d)\n", infoPtr, debugtext_t(text, isW), isW);
6054 /* window will be resized and positioned after LVN_BEGINLABELEDIT */
6055 if (isW)
6056 hedit = CreateWindowW(WC_EDITW, text, style, 0, 0, 0, 0, infoPtr->hwndSelf, 0, hinst, 0);
6057 else
6058 hedit = CreateWindowA(WC_EDITA, (LPCSTR)text, style, 0, 0, 0, 0, infoPtr->hwndSelf, 0, hinst, 0);
6060 if (!hedit) return 0;
6062 infoPtr->EditWndProc = (WNDPROC)
6063 (isW ? SetWindowLongPtrW(hedit, GWLP_WNDPROC, (DWORD_PTR)EditLblWndProcW) :
6064 SetWindowLongPtrA(hedit, GWLP_WNDPROC, (DWORD_PTR)EditLblWndProcA) );
6066 SendMessageW(hedit, WM_SETFONT, (WPARAM)infoPtr->hFont, FALSE);
6067 SendMessageW(hedit, EM_SETLIMITTEXT, DISP_TEXT_SIZE-1, 0);
6069 return hedit;
6072 static HWND LISTVIEW_EditLabelT(LISTVIEW_INFO *infoPtr, INT nItem, BOOL isW)
6074 WCHAR disptextW[DISP_TEXT_SIZE] = { 0 };
6075 HWND hwndSelf = infoPtr->hwndSelf;
6076 NMLVDISPINFOW dispInfo;
6077 HFONT hOldFont = NULL;
6078 TEXTMETRICW tm;
6079 RECT rect;
6080 SIZE sz;
6081 HDC hdc;
6083 TRACE("(nItem=%d, isW=%d)\n", nItem, isW);
6085 if (~infoPtr->dwStyle & LVS_EDITLABELS) return 0;
6087 /* remove existing edit box */
6088 if (infoPtr->hwndEdit)
6090 SetFocus(infoPtr->hwndSelf);
6091 infoPtr->hwndEdit = 0;
6094 if (nItem < 0 || nItem >= infoPtr->nItemCount) return 0;
6096 infoPtr->nEditLabelItem = nItem;
6098 LISTVIEW_SetSelection(infoPtr, nItem);
6099 LISTVIEW_SetItemFocus(infoPtr, nItem);
6100 LISTVIEW_InvalidateItem(infoPtr, nItem);
6102 rect.left = LVIR_LABEL;
6103 if (!LISTVIEW_GetItemRect(infoPtr, nItem, &rect)) return 0;
6105 ZeroMemory(&dispInfo, sizeof(dispInfo));
6106 dispInfo.item.mask = LVIF_PARAM | LVIF_STATE | LVIF_TEXT;
6107 dispInfo.item.iItem = nItem;
6108 dispInfo.item.iSubItem = 0;
6109 dispInfo.item.stateMask = ~0;
6110 dispInfo.item.pszText = disptextW;
6111 dispInfo.item.cchTextMax = DISP_TEXT_SIZE;
6112 if (!LISTVIEW_GetItemT(infoPtr, &dispInfo.item, isW)) return 0;
6114 infoPtr->hwndEdit = CreateEditLabelT(infoPtr, dispInfo.item.pszText, isW);
6115 if (!infoPtr->hwndEdit) return 0;
6117 if (notify_dispinfoT(infoPtr, LVN_BEGINLABELEDITW, &dispInfo, isW))
6119 if (!IsWindow(hwndSelf))
6120 return 0;
6121 SendMessageW(infoPtr->hwndEdit, WM_CLOSE, 0, 0);
6122 infoPtr->hwndEdit = 0;
6123 return 0;
6126 TRACE("disp text=%s\n", debugtext_t(dispInfo.item.pszText, isW));
6128 /* position and display edit box */
6129 hdc = GetDC(infoPtr->hwndSelf);
6131 /* select the font to get appropriate metric dimensions */
6132 if (infoPtr->hFont)
6133 hOldFont = SelectObject(hdc, infoPtr->hFont);
6135 /* use real edit box content, it could be altered during LVN_BEGINLABELEDIT notification */
6136 GetWindowTextW(infoPtr->hwndEdit, disptextW, DISP_TEXT_SIZE);
6137 TRACE("edit box text=%s\n", debugstr_w(disptextW));
6139 /* get string length in pixels */
6140 GetTextExtentPoint32W(hdc, disptextW, lstrlenW(disptextW), &sz);
6142 /* add extra spacing for the next character */
6143 GetTextMetricsW(hdc, &tm);
6144 sz.cx += tm.tmMaxCharWidth * 2;
6146 if (infoPtr->hFont)
6147 SelectObject(hdc, hOldFont);
6149 ReleaseDC(infoPtr->hwndSelf, hdc);
6151 sz.cy = rect.bottom - rect.top + 2;
6152 rect.left -= 2;
6153 rect.top -= 1;
6154 TRACE("moving edit (%ld,%ld)-(%ld,%ld)\n", rect.left, rect.top, sz.cx, sz.cy);
6155 MoveWindow(infoPtr->hwndEdit, rect.left, rect.top, sz.cx, sz.cy, FALSE);
6156 ShowWindow(infoPtr->hwndEdit, SW_NORMAL);
6157 SetFocus(infoPtr->hwndEdit);
6158 SendMessageW(infoPtr->hwndEdit, EM_SETSEL, 0, -1);
6159 return infoPtr->hwndEdit;
6163 /***
6164 * DESCRIPTION:
6165 * Ensures the specified item is visible, scrolling into view if necessary.
6167 * PARAMETER(S):
6168 * [I] infoPtr : valid pointer to the listview structure
6169 * [I] nItem : item index
6170 * [I] bPartial : partially or entirely visible
6172 * RETURN:
6173 * SUCCESS : TRUE
6174 * FAILURE : FALSE
6176 static BOOL LISTVIEW_EnsureVisible(LISTVIEW_INFO *infoPtr, INT nItem, BOOL bPartial)
6178 INT nScrollPosHeight = 0;
6179 INT nScrollPosWidth = 0;
6180 INT nHorzAdjust = 0;
6181 INT nVertAdjust = 0;
6182 INT nHorzDiff = 0;
6183 INT nVertDiff = 0;
6184 RECT rcItem, rcTemp;
6186 rcItem.left = LVIR_BOUNDS;
6187 if (!LISTVIEW_GetItemRect(infoPtr, nItem, &rcItem)) return FALSE;
6189 if (bPartial && IntersectRect(&rcTemp, &infoPtr->rcList, &rcItem)) return TRUE;
6191 if (rcItem.left < infoPtr->rcList.left || rcItem.right > infoPtr->rcList.right)
6193 /* scroll left/right, but in LV_VIEW_DETAILS mode */
6194 if (infoPtr->uView == LV_VIEW_LIST)
6195 nScrollPosWidth = infoPtr->nItemWidth;
6196 else if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
6197 nScrollPosWidth = 1;
6199 if (rcItem.left < infoPtr->rcList.left)
6201 nHorzAdjust = -1;
6202 if (infoPtr->uView != LV_VIEW_DETAILS) nHorzDiff = rcItem.left - infoPtr->rcList.left;
6204 else
6206 nHorzAdjust = 1;
6207 if (infoPtr->uView != LV_VIEW_DETAILS) nHorzDiff = rcItem.right - infoPtr->rcList.right;
6211 if (rcItem.top < infoPtr->rcList.top || rcItem.bottom > infoPtr->rcList.bottom)
6213 /* scroll up/down, but not in LVS_LIST mode */
6214 if (infoPtr->uView == LV_VIEW_DETAILS)
6215 nScrollPosHeight = infoPtr->nItemHeight;
6216 else if ((infoPtr->uView == LV_VIEW_ICON) || (infoPtr->uView == LV_VIEW_SMALLICON))
6217 nScrollPosHeight = 1;
6219 if (rcItem.top < infoPtr->rcList.top)
6221 nVertAdjust = -1;
6222 if (infoPtr->uView != LV_VIEW_LIST) nVertDiff = rcItem.top - infoPtr->rcList.top;
6224 else
6226 nVertAdjust = 1;
6227 if (infoPtr->uView != LV_VIEW_LIST) nVertDiff = rcItem.bottom - infoPtr->rcList.bottom;
6231 if (!nScrollPosWidth && !nScrollPosHeight) return TRUE;
6233 if (nScrollPosWidth)
6235 INT diff = nHorzDiff / nScrollPosWidth;
6236 if (nHorzDiff % nScrollPosWidth) diff += nHorzAdjust;
6237 LISTVIEW_HScroll(infoPtr, SB_INTERNAL, diff);
6240 if (nScrollPosHeight)
6242 INT diff = nVertDiff / nScrollPosHeight;
6243 if (nVertDiff % nScrollPosHeight) diff += nVertAdjust;
6244 LISTVIEW_VScroll(infoPtr, SB_INTERNAL, diff);
6247 return TRUE;
6250 /***
6251 * DESCRIPTION:
6252 * Searches for an item with specific characteristics.
6254 * PARAMETER(S):
6255 * [I] hwnd : window handle
6256 * [I] nStart : base item index
6257 * [I] lpFindInfo : item information to look for
6259 * RETURN:
6260 * SUCCESS : index of item
6261 * FAILURE : -1
6263 static INT LISTVIEW_FindItemW(const LISTVIEW_INFO *infoPtr, INT nStart,
6264 const LVFINDINFOW *lpFindInfo)
6266 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
6267 BOOL bWrap = FALSE, bNearest = FALSE;
6268 INT nItem = nStart + 1, nLast = infoPtr->nItemCount, nNearestItem = -1;
6269 ULONG xdist, ydist, dist, mindist = 0x7fffffff;
6270 POINT Position, Destination;
6271 int search_len = 0;
6272 LVITEMW lvItem;
6274 /* Search in virtual listviews should be done by application, not by
6275 listview control, so we just send LVN_ODFINDITEMW and return the result */
6276 if (infoPtr->dwStyle & LVS_OWNERDATA)
6278 NMLVFINDITEMW nmlv;
6280 nmlv.iStart = nStart;
6281 nmlv.lvfi = *lpFindInfo;
6282 return notify_hdr(infoPtr, LVN_ODFINDITEMW, (LPNMHDR)&nmlv.hdr);
6285 if (!lpFindInfo || nItem < 0) return -1;
6287 lvItem.mask = 0;
6288 if (lpFindInfo->flags & (LVFI_STRING | LVFI_PARTIAL | LVFI_SUBSTRING))
6290 lvItem.mask |= LVIF_TEXT;
6291 lvItem.pszText = szDispText;
6292 lvItem.cchTextMax = DISP_TEXT_SIZE;
6295 if (lpFindInfo->flags & LVFI_WRAP)
6296 bWrap = TRUE;
6298 if ((lpFindInfo->flags & LVFI_NEARESTXY) &&
6299 (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON))
6301 POINT Origin;
6302 RECT rcArea;
6304 LISTVIEW_GetOrigin(infoPtr, &Origin);
6305 Destination.x = lpFindInfo->pt.x - Origin.x;
6306 Destination.y = lpFindInfo->pt.y - Origin.y;
6307 switch(lpFindInfo->vkDirection)
6309 case VK_DOWN: Destination.y += infoPtr->nItemHeight; break;
6310 case VK_UP: Destination.y -= infoPtr->nItemHeight; break;
6311 case VK_RIGHT: Destination.x += infoPtr->nItemWidth; break;
6312 case VK_LEFT: Destination.x -= infoPtr->nItemWidth; break;
6313 case VK_HOME: Destination.x = Destination.y = 0; break;
6314 case VK_NEXT: Destination.y += infoPtr->rcList.bottom - infoPtr->rcList.top; break;
6315 case VK_PRIOR: Destination.y -= infoPtr->rcList.bottom - infoPtr->rcList.top; break;
6316 case VK_END:
6317 LISTVIEW_GetAreaRect(infoPtr, &rcArea);
6318 Destination.x = rcArea.right;
6319 Destination.y = rcArea.bottom;
6320 break;
6321 default: ERR("Unknown vkDirection=%d\n", lpFindInfo->vkDirection);
6323 bNearest = TRUE;
6325 else Destination.x = Destination.y = 0;
6327 /* if LVFI_PARAM is specified, all other flags are ignored */
6328 if (lpFindInfo->flags & LVFI_PARAM)
6330 lvItem.mask |= LVIF_PARAM;
6331 bNearest = FALSE;
6332 lvItem.mask &= ~LVIF_TEXT;
6335 nItem = bNearest ? -1 : nStart + 1;
6337 if (lpFindInfo->flags & (LVFI_PARTIAL | LVFI_SUBSTRING))
6338 search_len = lstrlenW(lpFindInfo->psz);
6340 again:
6341 for (; nItem < nLast; nItem++)
6343 lvItem.iItem = nItem;
6344 lvItem.iSubItem = 0;
6345 lvItem.pszText = szDispText;
6346 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) continue;
6348 if (lvItem.mask & LVIF_PARAM)
6350 if (lpFindInfo->lParam == lvItem.lParam)
6351 return nItem;
6352 else
6353 continue;
6356 if (lvItem.mask & LVIF_TEXT)
6358 if (lpFindInfo->flags & (LVFI_PARTIAL | LVFI_SUBSTRING))
6360 if (StrCmpNIW(lvItem.pszText, lpFindInfo->psz, search_len)) continue;
6362 else
6364 if (StrCmpIW(lvItem.pszText, lpFindInfo->psz)) continue;
6368 if (!bNearest) return nItem;
6370 /* This is very inefficient. To do a good job here,
6371 * we need a sorted array of (x,y) item positions */
6372 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
6374 /* compute the distance^2 to the destination */
6375 xdist = Destination.x - Position.x;
6376 ydist = Destination.y - Position.y;
6377 dist = xdist * xdist + ydist * ydist;
6379 /* remember the distance, and item if it's closer */
6380 if (dist < mindist)
6382 mindist = dist;
6383 nNearestItem = nItem;
6387 if (bWrap)
6389 nItem = 0;
6390 nLast = min(nStart + 1, infoPtr->nItemCount);
6391 bWrap = FALSE;
6392 goto again;
6395 return nNearestItem;
6398 /***
6399 * DESCRIPTION:
6400 * Searches for an item with specific characteristics.
6402 * PARAMETER(S):
6403 * [I] hwnd : window handle
6404 * [I] nStart : base item index
6405 * [I] lpFindInfo : item information to look for
6407 * RETURN:
6408 * SUCCESS : index of item
6409 * FAILURE : -1
6411 static INT LISTVIEW_FindItemA(const LISTVIEW_INFO *infoPtr, INT nStart,
6412 const LVFINDINFOA *lpFindInfo)
6414 LVFINDINFOW fiw;
6415 INT res;
6416 LPWSTR strW = NULL;
6418 memcpy(&fiw, lpFindInfo, sizeof(fiw));
6419 if (lpFindInfo->flags & (LVFI_STRING | LVFI_PARTIAL | LVFI_SUBSTRING))
6420 fiw.psz = strW = textdupTtoW((LPCWSTR)lpFindInfo->psz, FALSE);
6421 res = LISTVIEW_FindItemW(infoPtr, nStart, &fiw);
6422 textfreeT(strW, FALSE);
6423 return res;
6426 /***
6427 * DESCRIPTION:
6428 * Retrieves column attributes.
6430 * PARAMETER(S):
6431 * [I] infoPtr : valid pointer to the listview structure
6432 * [I] nColumn : column index
6433 * [IO] lpColumn : column information
6434 * [I] isW : if TRUE, then lpColumn is a LPLVCOLUMNW
6435 * otherwise it is in fact a LPLVCOLUMNA
6437 * RETURN:
6438 * SUCCESS : TRUE
6439 * FAILURE : FALSE
6441 static BOOL LISTVIEW_GetColumnT(const LISTVIEW_INFO *infoPtr, INT nColumn, LPLVCOLUMNW lpColumn, BOOL isW)
6443 COLUMN_INFO *lpColumnInfo;
6444 HDITEMW hdi;
6446 if (!lpColumn || nColumn < 0 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
6447 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, nColumn);
6449 /* initialize memory */
6450 ZeroMemory(&hdi, sizeof(hdi));
6452 if (lpColumn->mask & LVCF_TEXT)
6454 hdi.mask |= HDI_TEXT;
6455 hdi.pszText = lpColumn->pszText;
6456 hdi.cchTextMax = lpColumn->cchTextMax;
6459 if (lpColumn->mask & LVCF_IMAGE)
6460 hdi.mask |= HDI_IMAGE;
6462 if (lpColumn->mask & LVCF_ORDER)
6463 hdi.mask |= HDI_ORDER;
6465 if (lpColumn->mask & LVCF_SUBITEM)
6466 hdi.mask |= HDI_LPARAM;
6468 if (!SendMessageW(infoPtr->hwndHeader, isW ? HDM_GETITEMW : HDM_GETITEMA, nColumn, (LPARAM)&hdi)) return FALSE;
6470 if (lpColumn->mask & LVCF_FMT)
6471 lpColumn->fmt = lpColumnInfo->fmt;
6473 if (lpColumn->mask & LVCF_WIDTH)
6474 lpColumn->cx = lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left;
6476 if (lpColumn->mask & LVCF_IMAGE)
6477 lpColumn->iImage = hdi.iImage;
6479 if (lpColumn->mask & LVCF_ORDER)
6480 lpColumn->iOrder = hdi.iOrder;
6482 if (lpColumn->mask & LVCF_SUBITEM)
6483 lpColumn->iSubItem = hdi.lParam;
6485 if (lpColumn->mask & LVCF_MINWIDTH)
6486 lpColumn->cxMin = lpColumnInfo->cxMin;
6488 return TRUE;
6491 static inline BOOL LISTVIEW_GetColumnOrderArray(const LISTVIEW_INFO *infoPtr, INT iCount, LPINT lpiArray)
6493 if (!infoPtr->hwndHeader) return FALSE;
6494 return SendMessageW(infoPtr->hwndHeader, HDM_GETORDERARRAY, iCount, (LPARAM)lpiArray);
6497 /***
6498 * DESCRIPTION:
6499 * Retrieves the column width.
6501 * PARAMETER(S):
6502 * [I] infoPtr : valid pointer to the listview structure
6503 * [I] int : column index
6505 * RETURN:
6506 * SUCCESS : column width
6507 * FAILURE : zero
6509 static INT LISTVIEW_GetColumnWidth(const LISTVIEW_INFO *infoPtr, INT nColumn)
6511 INT nColumnWidth = 0;
6512 HDITEMW hdItem;
6514 TRACE("nColumn=%d\n", nColumn);
6516 /* we have a 'column' in LIST and REPORT mode only */
6517 switch(infoPtr->uView)
6519 case LV_VIEW_LIST:
6520 nColumnWidth = infoPtr->nItemWidth;
6521 break;
6522 case LV_VIEW_DETAILS:
6523 /* We are not using LISTVIEW_GetHeaderRect as this data is updated only after a HDN_ITEMCHANGED.
6524 * There is an application that subclasses the listview, calls LVM_GETCOLUMNWIDTH in the
6525 * HDN_ITEMCHANGED handler and goes into infinite recursion if it receives old data.
6527 hdItem.mask = HDI_WIDTH;
6528 if (!SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nColumn, (LPARAM)&hdItem))
6530 WARN("(%p): HDM_GETITEMW failed for item %d\n", infoPtr->hwndSelf, nColumn);
6531 return 0;
6533 nColumnWidth = hdItem.cxy;
6534 break;
6537 TRACE("nColumnWidth=%d\n", nColumnWidth);
6538 return nColumnWidth;
6541 /***
6542 * DESCRIPTION:
6543 * In list or report display mode, retrieves the number of items that can fit
6544 * vertically in the visible area. In icon or small icon display mode,
6545 * retrieves the total number of visible items.
6547 * PARAMETER(S):
6548 * [I] infoPtr : valid pointer to the listview structure
6550 * RETURN:
6551 * Number of fully visible items.
6553 static INT LISTVIEW_GetCountPerPage(const LISTVIEW_INFO *infoPtr)
6555 switch (infoPtr->uView)
6557 case LV_VIEW_ICON:
6558 case LV_VIEW_SMALLICON:
6559 return infoPtr->nItemCount;
6560 case LV_VIEW_DETAILS:
6561 return LISTVIEW_GetCountPerColumn(infoPtr);
6562 case LV_VIEW_LIST:
6563 return LISTVIEW_GetCountPerRow(infoPtr) * LISTVIEW_GetCountPerColumn(infoPtr);
6565 assert(FALSE);
6566 return 0;
6569 /***
6570 * DESCRIPTION:
6571 * Retrieves an image list handle.
6573 * PARAMETER(S):
6574 * [I] infoPtr : valid pointer to the listview structure
6575 * [I] nImageList : image list identifier
6577 * RETURN:
6578 * SUCCESS : image list handle
6579 * FAILURE : NULL
6581 static HIMAGELIST LISTVIEW_GetImageList(const LISTVIEW_INFO *infoPtr, INT nImageList)
6583 switch (nImageList)
6585 case LVSIL_NORMAL: return infoPtr->himlNormal;
6586 case LVSIL_SMALL: return infoPtr->himlSmall;
6587 case LVSIL_STATE: return infoPtr->himlState;
6588 case LVSIL_GROUPHEADER:
6589 FIXME("LVSIL_GROUPHEADER not supported\n");
6590 break;
6591 default:
6592 WARN("got unknown imagelist index - %d\n", nImageList);
6594 return NULL;
6597 /* LISTVIEW_GetISearchString */
6599 /***
6600 * DESCRIPTION:
6601 * Retrieves item attributes.
6603 * PARAMETER(S):
6604 * [I] hwnd : window handle
6605 * [IO] lpLVItem : item info
6606 * [I] isW : if TRUE, then lpLVItem is a LPLVITEMW,
6607 * if FALSE, then lpLVItem is a LPLVITEMA.
6609 * NOTE:
6610 * This is the internal 'GetItem' interface -- it tries to
6611 * be smart and avoid text copies, if possible, by modifying
6612 * lpLVItem->pszText to point to the text string. Please note
6613 * that this is not always possible (e.g. OWNERDATA), so on
6614 * entry you *must* supply valid values for pszText, and cchTextMax.
6615 * The only difference to the documented interface is that upon
6616 * return, you should use *only* the lpLVItem->pszText, rather than
6617 * the buffer pointer you provided on input. Most code already does
6618 * that, so it's not a problem.
6619 * For the two cases when the text must be copied (that is,
6620 * for LVM_GETITEM, and LVM_GETITEMTEXT), use LISTVIEW_GetItemExtT.
6622 * RETURN:
6623 * SUCCESS : TRUE
6624 * FAILURE : FALSE
6626 static BOOL LISTVIEW_GetItemT(const LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem, BOOL isW)
6628 ITEMHDR callbackHdr = { LPSTR_TEXTCALLBACKW, I_IMAGECALLBACK };
6629 BOOL is_subitem_invalid = FALSE;
6630 NMLVDISPINFOW dispInfo;
6631 ITEM_INFO *lpItem;
6632 ITEMHDR* pItemHdr;
6633 HDPA hdpaSubItems;
6635 TRACE("(item=%s, isW=%d)\n", debuglvitem_t(lpLVItem, isW), isW);
6637 if (!lpLVItem || lpLVItem->iItem < 0 || lpLVItem->iItem >= infoPtr->nItemCount)
6638 return FALSE;
6640 if (lpLVItem->mask == 0) return TRUE;
6641 TRACE("mask=%x\n", lpLVItem->mask);
6643 if (lpLVItem->iSubItem && (lpLVItem->mask & LVIF_STATE))
6644 lpLVItem->state = 0;
6646 /* a quick optimization if all we're asked is the focus state
6647 * these queries are worth optimising since they are common,
6648 * and can be answered in constant time, without the heavy accesses */
6649 if ( (lpLVItem->mask == LVIF_STATE) && (lpLVItem->stateMask == LVIS_FOCUSED) &&
6650 !(infoPtr->uCallbackMask & LVIS_FOCUSED) )
6652 lpLVItem->state = 0;
6653 if (infoPtr->nFocusedItem == lpLVItem->iItem && !lpLVItem->iSubItem)
6654 lpLVItem->state |= LVIS_FOCUSED;
6655 return TRUE;
6658 ZeroMemory(&dispInfo, sizeof(dispInfo));
6660 /* if the app stores all the data, handle it separately */
6661 if (infoPtr->dwStyle & LVS_OWNERDATA)
6663 dispInfo.item.state = 0;
6665 /* apparently, we should not callback for lParam in LVS_OWNERDATA */
6666 if ((lpLVItem->mask & ~(LVIF_STATE | LVIF_PARAM)) ||
6667 ((lpLVItem->mask & LVIF_STATE) && (infoPtr->uCallbackMask & lpLVItem->stateMask)))
6669 UINT mask = lpLVItem->mask;
6671 /* NOTE: copy only fields which we _know_ are initialized, some apps
6672 * depend on the uninitialized fields being 0 */
6673 dispInfo.item.mask = lpLVItem->mask & ~LVIF_PARAM;
6674 dispInfo.item.iItem = lpLVItem->iItem;
6675 dispInfo.item.iSubItem = lpLVItem->iSubItem;
6676 if (lpLVItem->mask & LVIF_TEXT)
6678 if (lpLVItem->mask & LVIF_NORECOMPUTE)
6679 /* reset mask */
6680 dispInfo.item.mask &= ~(LVIF_TEXT | LVIF_NORECOMPUTE);
6681 else
6683 dispInfo.item.pszText = lpLVItem->pszText;
6684 dispInfo.item.cchTextMax = lpLVItem->cchTextMax;
6687 if (lpLVItem->mask & LVIF_STATE)
6688 dispInfo.item.stateMask = lpLVItem->stateMask & infoPtr->uCallbackMask;
6689 /* could be zeroed on LVIF_NORECOMPUTE case */
6690 if (dispInfo.item.mask)
6692 notify_dispinfoT(infoPtr, LVN_GETDISPINFOW, &dispInfo, isW);
6693 dispInfo.item.stateMask = lpLVItem->stateMask;
6694 if (lpLVItem->mask & (LVIF_GROUPID|LVIF_COLUMNS))
6696 /* full size structure expected - _WIN32IE >= 0x560 */
6697 *lpLVItem = dispInfo.item;
6699 else if (lpLVItem->mask & LVIF_INDENT)
6701 /* indent member expected - _WIN32IE >= 0x300 */
6702 memcpy(lpLVItem, &dispInfo.item, offsetof( LVITEMW, iGroupId ));
6704 else
6706 /* minimal structure expected */
6707 memcpy(lpLVItem, &dispInfo.item, offsetof( LVITEMW, iIndent ));
6709 lpLVItem->mask = mask;
6710 TRACE(" getdispinfo(1):lpLVItem=%s\n", debuglvitem_t(lpLVItem, isW));
6714 /* make sure lParam is zeroed out */
6715 if (lpLVItem->mask & LVIF_PARAM) lpLVItem->lParam = 0;
6717 /* callback marked pointer required here */
6718 if ((lpLVItem->mask & LVIF_TEXT) && (lpLVItem->mask & LVIF_NORECOMPUTE))
6719 lpLVItem->pszText = LPSTR_TEXTCALLBACKW;
6721 /* we store only a little state, so if we're not asked, we're done */
6722 if (!(lpLVItem->mask & LVIF_STATE) || lpLVItem->iSubItem) return TRUE;
6724 /* if focus is handled by us, report it */
6725 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_FOCUSED )
6727 lpLVItem->state &= ~LVIS_FOCUSED;
6728 if (infoPtr->nFocusedItem == lpLVItem->iItem)
6729 lpLVItem->state |= LVIS_FOCUSED;
6732 /* and do the same for selection, if we handle it */
6733 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_SELECTED )
6735 lpLVItem->state &= ~LVIS_SELECTED;
6736 if (ranges_contain(infoPtr->selectionRanges, lpLVItem->iItem))
6737 lpLVItem->state |= LVIS_SELECTED;
6740 return TRUE;
6743 /* find the item and subitem structures before we proceed */
6744 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, lpLVItem->iItem);
6745 lpItem = DPA_GetPtr(hdpaSubItems, 0);
6746 assert (lpItem);
6748 if (lpLVItem->iSubItem)
6750 SUBITEM_INFO *lpSubItem = LISTVIEW_GetSubItemPtr(hdpaSubItems, lpLVItem->iSubItem);
6751 if (lpSubItem)
6752 pItemHdr = &lpSubItem->hdr;
6753 else
6755 pItemHdr = &callbackHdr;
6756 is_subitem_invalid = TRUE;
6759 else
6760 pItemHdr = &lpItem->hdr;
6762 /* Do we need to query the state from the app? */
6763 if ((lpLVItem->mask & LVIF_STATE) && infoPtr->uCallbackMask && (!lpLVItem->iSubItem || is_subitem_invalid))
6765 dispInfo.item.mask |= LVIF_STATE;
6766 dispInfo.item.stateMask = infoPtr->uCallbackMask;
6769 /* Do we need to enquire about the image? */
6770 if ((lpLVItem->mask & LVIF_IMAGE) && pItemHdr->iImage == I_IMAGECALLBACK &&
6771 (!lpLVItem->iSubItem || (infoPtr->dwLvExStyle & LVS_EX_SUBITEMIMAGES)))
6773 dispInfo.item.mask |= LVIF_IMAGE;
6774 dispInfo.item.iImage = I_IMAGECALLBACK;
6777 /* Only items support indentation */
6778 if ((lpLVItem->mask & LVIF_INDENT) && lpItem->iIndent == I_INDENTCALLBACK && !lpLVItem->iSubItem)
6780 dispInfo.item.mask |= LVIF_INDENT;
6781 dispInfo.item.iIndent = I_INDENTCALLBACK;
6784 /* Apps depend on calling back for text if it is NULL or LPSTR_TEXTCALLBACKW */
6785 if ((lpLVItem->mask & LVIF_TEXT) && !(lpLVItem->mask & LVIF_NORECOMPUTE) &&
6786 !is_text(pItemHdr->pszText))
6788 dispInfo.item.mask |= LVIF_TEXT;
6789 dispInfo.item.pszText = lpLVItem->pszText;
6790 dispInfo.item.cchTextMax = lpLVItem->cchTextMax;
6791 if (dispInfo.item.pszText && dispInfo.item.cchTextMax > 0)
6792 *dispInfo.item.pszText = '\0';
6795 /* If we don't have all the requested info, query the application */
6796 if (dispInfo.item.mask)
6798 dispInfo.item.iItem = lpLVItem->iItem;
6799 dispInfo.item.iSubItem = lpLVItem->iSubItem;
6800 dispInfo.item.lParam = lpItem->lParam;
6801 notify_dispinfoT(infoPtr, LVN_GETDISPINFOW, &dispInfo, isW);
6802 TRACE(" getdispinfo(2):item=%s\n", debuglvitem_t(&dispInfo.item, isW));
6805 /* we should not store values for subitems */
6806 if (lpLVItem->iSubItem) dispInfo.item.mask &= ~LVIF_DI_SETITEM;
6808 /* Now, handle the iImage field */
6809 if (dispInfo.item.mask & LVIF_IMAGE)
6811 lpLVItem->iImage = dispInfo.item.iImage;
6812 if ((dispInfo.item.mask & LVIF_DI_SETITEM) && pItemHdr->iImage == I_IMAGECALLBACK)
6813 pItemHdr->iImage = dispInfo.item.iImage;
6815 else if (lpLVItem->mask & LVIF_IMAGE)
6817 if (!lpLVItem->iSubItem || (infoPtr->dwLvExStyle & LVS_EX_SUBITEMIMAGES))
6818 lpLVItem->iImage = pItemHdr->iImage;
6819 else
6820 lpLVItem->iImage = 0;
6823 /* The pszText field */
6824 if (dispInfo.item.mask & LVIF_TEXT)
6826 if ((dispInfo.item.mask & LVIF_DI_SETITEM) && pItemHdr->pszText)
6827 textsetptrT(&pItemHdr->pszText, dispInfo.item.pszText, isW);
6829 lpLVItem->pszText = dispInfo.item.pszText;
6831 else if (lpLVItem->mask & LVIF_TEXT)
6833 /* if LVN_GETDISPINFO's disabled with LVIF_NORECOMPUTE return callback placeholder */
6834 if (isW || !is_text(pItemHdr->pszText)) lpLVItem->pszText = pItemHdr->pszText;
6835 else textcpynT(lpLVItem->pszText, isW, pItemHdr->pszText, TRUE, lpLVItem->cchTextMax);
6838 /* Next is the lParam field */
6839 if (dispInfo.item.mask & LVIF_PARAM)
6841 lpLVItem->lParam = dispInfo.item.lParam;
6842 if ((dispInfo.item.mask & LVIF_DI_SETITEM))
6843 lpItem->lParam = dispInfo.item.lParam;
6845 else if (lpLVItem->mask & LVIF_PARAM)
6846 lpLVItem->lParam = lpItem->lParam;
6848 /* if this is a subitem, we're done */
6849 if (lpLVItem->iSubItem) return TRUE;
6851 /* ... the state field (this one is different due to uCallbackmask) */
6852 if (lpLVItem->mask & LVIF_STATE)
6854 lpLVItem->state = lpItem->state & lpLVItem->stateMask;
6855 if (dispInfo.item.mask & LVIF_STATE)
6857 lpLVItem->state &= ~dispInfo.item.stateMask;
6858 lpLVItem->state |= (dispInfo.item.state & dispInfo.item.stateMask);
6860 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_FOCUSED )
6862 lpLVItem->state &= ~LVIS_FOCUSED;
6863 if (infoPtr->nFocusedItem == lpLVItem->iItem)
6864 lpLVItem->state |= LVIS_FOCUSED;
6866 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_SELECTED )
6868 lpLVItem->state &= ~LVIS_SELECTED;
6869 if (ranges_contain(infoPtr->selectionRanges, lpLVItem->iItem))
6870 lpLVItem->state |= LVIS_SELECTED;
6874 /* and last, but not least, the indent field */
6875 if (dispInfo.item.mask & LVIF_INDENT)
6877 lpLVItem->iIndent = dispInfo.item.iIndent;
6878 if ((dispInfo.item.mask & LVIF_DI_SETITEM) && lpItem->iIndent == I_INDENTCALLBACK)
6879 lpItem->iIndent = dispInfo.item.iIndent;
6881 else if (lpLVItem->mask & LVIF_INDENT)
6883 lpLVItem->iIndent = lpItem->iIndent;
6886 return TRUE;
6889 /***
6890 * DESCRIPTION:
6891 * Retrieves item attributes.
6893 * PARAMETER(S):
6894 * [I] hwnd : window handle
6895 * [IO] lpLVItem : item info
6896 * [I] isW : if TRUE, then lpLVItem is a LPLVITEMW,
6897 * if FALSE, then lpLVItem is a LPLVITEMA.
6899 * NOTE:
6900 * This is the external 'GetItem' interface -- it properly copies
6901 * the text in the provided buffer.
6903 * RETURN:
6904 * SUCCESS : TRUE
6905 * FAILURE : FALSE
6907 static BOOL LISTVIEW_GetItemExtT(const LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem, BOOL isW)
6909 LPWSTR pszText;
6910 BOOL bResult;
6912 if (!lpLVItem || lpLVItem->iItem < 0 || lpLVItem->iItem >= infoPtr->nItemCount)
6913 return FALSE;
6915 pszText = lpLVItem->pszText;
6916 bResult = LISTVIEW_GetItemT(infoPtr, lpLVItem, isW);
6917 if (bResult && (lpLVItem->mask & LVIF_TEXT) && lpLVItem->pszText != pszText)
6919 if (lpLVItem->pszText != LPSTR_TEXTCALLBACKW)
6920 textcpynT(pszText, isW, lpLVItem->pszText, isW, lpLVItem->cchTextMax);
6921 else
6922 pszText = LPSTR_TEXTCALLBACKW;
6924 lpLVItem->pszText = pszText;
6926 return bResult;
6930 /***
6931 * DESCRIPTION:
6932 * Retrieves the position (upper-left) of the listview control item.
6933 * Note that for LVS_ICON style, the upper-left is that of the icon
6934 * and not the bounding box.
6936 * PARAMETER(S):
6937 * [I] infoPtr : valid pointer to the listview structure
6938 * [I] nItem : item index
6939 * [O] lpptPosition : coordinate information
6941 * RETURN:
6942 * SUCCESS : TRUE
6943 * FAILURE : FALSE
6945 static BOOL LISTVIEW_GetItemPosition(const LISTVIEW_INFO *infoPtr, INT nItem, LPPOINT lpptPosition)
6947 POINT Origin;
6949 TRACE("(nItem=%d, lpptPosition=%p)\n", nItem, lpptPosition);
6951 if (!lpptPosition || nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
6953 LISTVIEW_GetOrigin(infoPtr, &Origin);
6954 LISTVIEW_GetItemOrigin(infoPtr, nItem, lpptPosition);
6956 if (infoPtr->uView == LV_VIEW_ICON)
6958 lpptPosition->x += (infoPtr->nItemWidth - infoPtr->iconSize.cx) / 2;
6959 lpptPosition->y += ICON_TOP_PADDING;
6961 lpptPosition->x += Origin.x;
6962 lpptPosition->y += Origin.y;
6964 TRACE (" lpptPosition=%s\n", wine_dbgstr_point(lpptPosition));
6965 return TRUE;
6969 /***
6970 * DESCRIPTION:
6971 * Retrieves the bounding rectangle for a listview control item.
6973 * PARAMETER(S):
6974 * [I] infoPtr : valid pointer to the listview structure
6975 * [I] nItem : item index
6976 * [IO] lprc : bounding rectangle coordinates
6977 * lprc->left specifies the portion of the item for which the bounding
6978 * rectangle will be retrieved.
6980 * LVIR_BOUNDS Returns the bounding rectangle of the entire item,
6981 * including the icon and label.
6983 * * For LVS_ICON
6984 * * Experiment shows that native control returns:
6985 * * width = min (48, length of text line)
6986 * * .left = position.x - (width - iconsize.cx)/2
6987 * * .right = .left + width
6988 * * height = #lines of text * ntmHeight + icon height + 8
6989 * * .top = position.y - 2
6990 * * .bottom = .top + height
6991 * * separation between items .y = itemSpacing.cy - height
6992 * * .x = itemSpacing.cx - width
6993 * LVIR_ICON Returns the bounding rectangle of the icon or small icon.
6995 * * For LVS_ICON
6996 * * Experiment shows that native control returns:
6997 * * width = iconSize.cx + 16
6998 * * .left = position.x - (width - iconsize.cx)/2
6999 * * .right = .left + width
7000 * * height = iconSize.cy + 4
7001 * * .top = position.y - 2
7002 * * .bottom = .top + height
7003 * * separation between items .y = itemSpacing.cy - height
7004 * * .x = itemSpacing.cx - width
7005 * LVIR_LABEL Returns the bounding rectangle of the item text.
7007 * * For LVS_ICON
7008 * * Experiment shows that native control returns:
7009 * * width = text length
7010 * * .left = position.x - width/2
7011 * * .right = .left + width
7012 * * height = ntmH * linecount + 2
7013 * * .top = position.y + iconSize.cy + 6
7014 * * .bottom = .top + height
7015 * * separation between items .y = itemSpacing.cy - height
7016 * * .x = itemSpacing.cx - width
7017 * LVIR_SELECTBOUNDS Returns the union of the LVIR_ICON and LVIR_LABEL
7018 * rectangles, but excludes columns in report view.
7020 * RETURN:
7021 * SUCCESS : TRUE
7022 * FAILURE : FALSE
7024 * NOTES
7025 * Note that the bounding rectangle of the label in the LVS_ICON view depends
7026 * upon whether the window has the focus currently and on whether the item
7027 * is the one with the focus. Ensure that the control's record of which
7028 * item has the focus agrees with the items' records.
7030 static BOOL LISTVIEW_GetItemRect(const LISTVIEW_INFO *infoPtr, INT nItem, LPRECT lprc)
7032 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
7033 BOOL doLabel = TRUE, oversizedBox = FALSE;
7034 POINT Position, Origin;
7035 LVITEMW lvItem;
7036 LONG mode;
7038 TRACE("(hwnd=%p, nItem=%d, lprc=%p)\n", infoPtr->hwndSelf, nItem, lprc);
7040 if (!lprc || nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
7042 LISTVIEW_GetOrigin(infoPtr, &Origin);
7043 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
7045 /* Be smart and try to figure out the minimum we have to do */
7046 if (lprc->left == LVIR_ICON) doLabel = FALSE;
7047 if (infoPtr->uView == LV_VIEW_DETAILS && lprc->left == LVIR_BOUNDS) doLabel = FALSE;
7048 if (infoPtr->uView == LV_VIEW_ICON && lprc->left != LVIR_ICON &&
7049 infoPtr->bFocus && LISTVIEW_GetItemState(infoPtr, nItem, LVIS_FOCUSED))
7050 oversizedBox = TRUE;
7052 /* get what we need from the item before hand, so we make
7053 * only one request. This can speed up things, if data
7054 * is stored on the app side */
7055 lvItem.mask = 0;
7056 if (infoPtr->uView == LV_VIEW_DETAILS) lvItem.mask |= LVIF_INDENT;
7057 if (doLabel) lvItem.mask |= LVIF_TEXT;
7058 lvItem.iItem = nItem;
7059 lvItem.iSubItem = 0;
7060 lvItem.pszText = szDispText;
7061 lvItem.cchTextMax = DISP_TEXT_SIZE;
7062 if (lvItem.mask && !LISTVIEW_GetItemW(infoPtr, &lvItem)) return FALSE;
7063 /* we got the state already up, simulate it here, to avoid a reget */
7064 if (infoPtr->uView == LV_VIEW_ICON && (lprc->left != LVIR_ICON))
7066 lvItem.mask |= LVIF_STATE;
7067 lvItem.stateMask = LVIS_FOCUSED;
7068 lvItem.state = (oversizedBox ? LVIS_FOCUSED : 0);
7071 if (infoPtr->uView == LV_VIEW_DETAILS && (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) && lprc->left == LVIR_SELECTBOUNDS)
7072 lprc->left = LVIR_BOUNDS;
7074 mode = lprc->left;
7075 switch(lprc->left)
7077 case LVIR_ICON:
7078 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, NULL, lprc, NULL, NULL);
7079 break;
7081 case LVIR_LABEL:
7082 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, NULL, NULL, NULL, lprc);
7083 break;
7085 case LVIR_BOUNDS:
7086 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, lprc, NULL, NULL, NULL, NULL);
7087 break;
7089 case LVIR_SELECTBOUNDS:
7090 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, lprc, NULL, NULL, NULL);
7091 break;
7093 default:
7094 WARN("Unknown value: %ld\n", lprc->left);
7095 return FALSE;
7098 if (infoPtr->uView == LV_VIEW_DETAILS)
7100 if (mode != LVIR_BOUNDS)
7101 OffsetRect(lprc, Origin.x + LISTVIEW_GetColumnInfo(infoPtr, 0)->rcHeader.left,
7102 Position.y + Origin.y);
7103 else
7104 OffsetRect(lprc, Origin.x, Position.y + Origin.y);
7106 else
7107 OffsetRect(lprc, Position.x + Origin.x, Position.y + Origin.y);
7109 TRACE(" rect=%s\n", wine_dbgstr_rect(lprc));
7111 return TRUE;
7114 /***
7115 * DESCRIPTION:
7116 * Retrieves the spacing between listview control items.
7118 * PARAMETER(S):
7119 * [I] infoPtr : valid pointer to the listview structure
7120 * [IO] lprc : rectangle to receive the output
7121 * on input, lprc->top = nSubItem
7122 * lprc->left = LVIR_ICON | LVIR_BOUNDS | LVIR_LABEL
7124 * NOTE: for subItem = 0, we should return the bounds of the _entire_ item,
7125 * not only those of the first column.
7127 * RETURN:
7128 * TRUE: success
7129 * FALSE: failure
7131 static BOOL LISTVIEW_GetSubItemRect(const LISTVIEW_INFO *infoPtr, INT item, LPRECT lprc)
7133 RECT rect = { 0, 0, 0, 0 };
7134 POINT origin;
7135 INT y;
7137 if (!lprc) return FALSE;
7139 TRACE("item %d, subitem %ld, type %ld\n", item, lprc->top, lprc->left);
7141 /* Subitem of '0' means item itself, and this works for all control view modes */
7142 if (lprc->top == 0)
7143 return LISTVIEW_GetItemRect(infoPtr, item, lprc);
7145 if (infoPtr->uView != LV_VIEW_DETAILS) return FALSE;
7147 LISTVIEW_GetOrigin(infoPtr, &origin);
7148 /* this works for any item index, no matter if it exists or not */
7149 y = item * infoPtr->nItemHeight + origin.y;
7151 if (infoPtr->hwndHeader && SendMessageW(infoPtr->hwndHeader, HDM_GETITEMRECT, lprc->top, (LPARAM)&rect))
7153 rect.top = 0;
7154 rect.bottom = infoPtr->nItemHeight;
7156 else
7158 /* Native implementation is broken for this case and garbage is left for left and right fields,
7159 we zero them to get predictable output */
7160 lprc->left = lprc->right = lprc->top = 0;
7161 lprc->bottom = infoPtr->nItemHeight;
7162 OffsetRect(lprc, origin.x, y);
7163 TRACE("return rect %s\n", wine_dbgstr_rect(lprc));
7164 return TRUE;
7167 switch (lprc->left)
7169 case LVIR_ICON:
7171 /* it doesn't matter if main item actually has an icon, if imagelist is set icon width is returned */
7172 if (infoPtr->himlSmall)
7173 rect.right = rect.left + infoPtr->iconSize.cx;
7174 else
7175 rect.right = rect.left;
7177 rect.bottom = rect.top + infoPtr->iconSize.cy;
7178 break;
7180 case LVIR_LABEL:
7181 case LVIR_BOUNDS:
7182 break;
7184 default:
7185 ERR("Unknown bounds %ld\n", lprc->left);
7186 return FALSE;
7189 OffsetRect(&rect, origin.x, y);
7190 *lprc = rect;
7191 TRACE("return rect %s\n", wine_dbgstr_rect(lprc));
7193 return TRUE;
7196 /***
7197 * DESCRIPTION:
7198 * Retrieves the spacing between listview control items.
7200 * PARAMETER(S):
7201 * [I] infoPtr : valid pointer to the listview structure
7202 * [I] bSmall : flag for small or large icon
7204 * RETURN:
7205 * Horizontal + vertical spacing
7207 static LONG LISTVIEW_GetItemSpacing(const LISTVIEW_INFO *infoPtr, BOOL bSmall)
7209 LONG lResult;
7211 if (!bSmall)
7213 lResult = MAKELONG(infoPtr->iconSpacing.cx, infoPtr->iconSpacing.cy);
7215 else
7217 if (infoPtr->uView == LV_VIEW_ICON)
7218 lResult = MAKELONG(DEFAULT_COLUMN_WIDTH, GetSystemMetrics(SM_CXSMICON)+HEIGHT_PADDING);
7219 else
7220 lResult = MAKELONG(infoPtr->nItemWidth, infoPtr->nItemHeight);
7222 return lResult;
7225 /***
7226 * DESCRIPTION:
7227 * Retrieves the state of a listview control item.
7229 * PARAMETER(S):
7230 * [I] infoPtr : valid pointer to the listview structure
7231 * [I] nItem : item index
7232 * [I] uMask : state mask
7234 * RETURN:
7235 * State specified by the mask.
7237 static UINT LISTVIEW_GetItemState(const LISTVIEW_INFO *infoPtr, INT nItem, UINT uMask)
7239 LVITEMW lvItem;
7241 if (nItem < 0 || nItem >= infoPtr->nItemCount) return 0;
7243 lvItem.iItem = nItem;
7244 lvItem.iSubItem = 0;
7245 lvItem.mask = LVIF_STATE;
7246 lvItem.stateMask = uMask;
7247 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return 0;
7249 return lvItem.state & uMask;
7252 /***
7253 * DESCRIPTION:
7254 * Retrieves the text of a listview control item or subitem.
7256 * PARAMETER(S):
7257 * [I] hwnd : window handle
7258 * [I] nItem : item index
7259 * [IO] lpLVItem : item information
7260 * [I] isW : TRUE if lpLVItem is Unicode
7262 * RETURN:
7263 * SUCCESS : string length
7264 * FAILURE : 0
7266 static INT LISTVIEW_GetItemTextT(const LISTVIEW_INFO *infoPtr, INT nItem, LPLVITEMW lpLVItem, BOOL isW)
7268 if (!lpLVItem || nItem < 0 || nItem >= infoPtr->nItemCount) return 0;
7270 lpLVItem->mask = LVIF_TEXT;
7271 lpLVItem->iItem = nItem;
7272 if (!LISTVIEW_GetItemExtT(infoPtr, lpLVItem, isW)) return 0;
7274 return textlenT(lpLVItem->pszText, isW);
7277 /***
7278 * DESCRIPTION:
7279 * Searches for an item based on properties + relationships.
7281 * PARAMETER(S):
7282 * [I] infoPtr : valid pointer to the listview structure
7283 * [I] nItem : item index
7284 * [I] uFlags : relationship flag
7286 * RETURN:
7287 * SUCCESS : item index
7288 * FAILURE : -1
7290 static INT LISTVIEW_GetNextItem(const LISTVIEW_INFO *infoPtr, INT nItem, UINT uFlags)
7292 UINT uMask = 0;
7293 LVFINDINFOW lvFindInfo;
7294 INT nCountPerColumn;
7295 INT nCountPerRow;
7296 INT i;
7298 TRACE("nItem=%d, uFlags=%x, nItemCount=%d\n", nItem, uFlags, infoPtr->nItemCount);
7299 if (nItem < -1 || nItem >= infoPtr->nItemCount) return -1;
7301 ZeroMemory(&lvFindInfo, sizeof(lvFindInfo));
7303 if (uFlags & LVNI_CUT)
7304 uMask |= LVIS_CUT;
7306 if (uFlags & LVNI_DROPHILITED)
7307 uMask |= LVIS_DROPHILITED;
7309 if (uFlags & LVNI_FOCUSED)
7310 uMask |= LVIS_FOCUSED;
7312 if (uFlags & LVNI_SELECTED)
7313 uMask |= LVIS_SELECTED;
7315 /* if we're asked for the focused item, that's only one,
7316 * so it's worth optimizing */
7317 if (uFlags & LVNI_FOCUSED)
7319 if ((LISTVIEW_GetItemState(infoPtr, infoPtr->nFocusedItem, uMask) & uMask) != uMask) return -1;
7320 return (infoPtr->nFocusedItem == nItem) ? -1 : infoPtr->nFocusedItem;
7323 if (uFlags & LVNI_ABOVE)
7325 if ((infoPtr->uView == LV_VIEW_LIST) || (infoPtr->uView == LV_VIEW_DETAILS))
7327 while (nItem >= 0)
7329 nItem--;
7330 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7331 return nItem;
7334 else
7336 /* Special case for autoarrange - move 'til the top of a list */
7337 if (is_autoarrange(infoPtr))
7339 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
7340 while (nItem - nCountPerRow >= 0)
7342 nItem -= nCountPerRow;
7343 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7344 return nItem;
7346 return -1;
7348 lvFindInfo.flags = LVFI_NEARESTXY;
7349 lvFindInfo.vkDirection = VK_UP;
7350 LISTVIEW_GetItemPosition(infoPtr, nItem, &lvFindInfo.pt);
7351 while ((nItem = LISTVIEW_FindItemW(infoPtr, nItem, &lvFindInfo)) != -1)
7353 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7354 return nItem;
7358 else if (uFlags & LVNI_BELOW)
7360 if ((infoPtr->uView == LV_VIEW_LIST) || (infoPtr->uView == LV_VIEW_DETAILS))
7362 while (nItem < infoPtr->nItemCount)
7364 nItem++;
7365 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7366 return nItem;
7369 else
7371 /* Special case for autoarrange - move 'til the bottom of a list */
7372 if (is_autoarrange(infoPtr))
7374 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
7375 while (nItem + nCountPerRow < infoPtr->nItemCount )
7377 nItem += nCountPerRow;
7378 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7379 return nItem;
7381 return -1;
7383 lvFindInfo.flags = LVFI_NEARESTXY;
7384 lvFindInfo.vkDirection = VK_DOWN;
7385 LISTVIEW_GetItemPosition(infoPtr, nItem, &lvFindInfo.pt);
7386 while ((nItem = LISTVIEW_FindItemW(infoPtr, nItem, &lvFindInfo)) != -1)
7388 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7389 return nItem;
7393 else if (uFlags & LVNI_TOLEFT)
7395 if (infoPtr->uView == LV_VIEW_LIST)
7397 nCountPerColumn = LISTVIEW_GetCountPerColumn(infoPtr);
7398 while (nItem - nCountPerColumn >= 0)
7400 nItem -= nCountPerColumn;
7401 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7402 return nItem;
7405 else if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
7407 /* Special case for autoarrange - move 'til the beginning of a row */
7408 if (is_autoarrange(infoPtr))
7410 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
7411 while (nItem % nCountPerRow > 0)
7413 nItem --;
7414 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7415 return nItem;
7417 return -1;
7419 lvFindInfo.flags = LVFI_NEARESTXY;
7420 lvFindInfo.vkDirection = VK_LEFT;
7421 LISTVIEW_GetItemPosition(infoPtr, nItem, &lvFindInfo.pt);
7422 while ((nItem = LISTVIEW_FindItemW(infoPtr, nItem, &lvFindInfo)) != -1)
7424 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7425 return nItem;
7429 else if (uFlags & LVNI_TORIGHT)
7431 if (infoPtr->uView == LV_VIEW_LIST)
7433 nCountPerColumn = LISTVIEW_GetCountPerColumn(infoPtr);
7434 while (nItem + nCountPerColumn < infoPtr->nItemCount)
7436 nItem += nCountPerColumn;
7437 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7438 return nItem;
7441 else if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
7443 /* Special case for autoarrange - move 'til the end of a row */
7444 if (is_autoarrange(infoPtr))
7446 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
7447 while (nItem % nCountPerRow < nCountPerRow - 1 )
7449 nItem ++;
7450 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7451 return nItem;
7453 return -1;
7455 lvFindInfo.flags = LVFI_NEARESTXY;
7456 lvFindInfo.vkDirection = VK_RIGHT;
7457 LISTVIEW_GetItemPosition(infoPtr, nItem, &lvFindInfo.pt);
7458 while ((nItem = LISTVIEW_FindItemW(infoPtr, nItem, &lvFindInfo)) != -1)
7460 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7461 return nItem;
7465 else
7467 nItem++;
7469 /* search by index */
7470 for (i = nItem; i < infoPtr->nItemCount; i++)
7472 if ((LISTVIEW_GetItemState(infoPtr, i, uMask) & uMask) == uMask)
7473 return i;
7477 return -1;
7480 static BOOL LISTVIEW_GetNextItemIndex(const LISTVIEW_INFO *infoPtr, LVITEMINDEX *index, UINT flags)
7482 /* FIXME: specified item group is ignored */
7484 if (!index)
7485 return FALSE;
7487 index->iItem = LISTVIEW_GetNextItem(infoPtr, index->iItem, flags);
7488 return index->iItem != -1;
7491 /* LISTVIEW_GetNumberOfWorkAreas */
7493 /***
7494 * DESCRIPTION:
7495 * Retrieves the origin coordinates when in icon or small icon display mode.
7497 * PARAMETER(S):
7498 * [I] infoPtr : valid pointer to the listview structure
7499 * [O] lpptOrigin : coordinate information
7501 * RETURN:
7502 * None.
7504 static void LISTVIEW_GetOrigin(const LISTVIEW_INFO *infoPtr, LPPOINT lpptOrigin)
7506 INT nHorzPos = 0, nVertPos = 0;
7507 SCROLLINFO scrollInfo;
7509 scrollInfo.cbSize = sizeof(SCROLLINFO);
7510 scrollInfo.fMask = SIF_POS;
7512 if (GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo))
7513 nHorzPos = scrollInfo.nPos;
7514 if (GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
7515 nVertPos = scrollInfo.nPos;
7517 TRACE("nHorzPos=%d, nVertPos=%d\n", nHorzPos, nVertPos);
7519 lpptOrigin->x = infoPtr->rcList.left;
7520 lpptOrigin->y = infoPtr->rcList.top;
7521 if (infoPtr->uView == LV_VIEW_LIST)
7522 nHorzPos *= infoPtr->nItemWidth;
7523 else if (infoPtr->uView == LV_VIEW_DETAILS)
7524 nVertPos *= infoPtr->nItemHeight;
7526 lpptOrigin->x -= nHorzPos;
7527 lpptOrigin->y -= nVertPos;
7529 TRACE(" origin=%s\n", wine_dbgstr_point(lpptOrigin));
7532 /***
7533 * DESCRIPTION:
7534 * Retrieves the width of a string.
7536 * PARAMETER(S):
7537 * [I] hwnd : window handle
7538 * [I] lpszText : text string to process
7539 * [I] isW : TRUE if lpszText is Unicode, FALSE otherwise
7541 * RETURN:
7542 * SUCCESS : string width (in pixels)
7543 * FAILURE : zero
7545 static INT LISTVIEW_GetStringWidthT(const LISTVIEW_INFO *infoPtr, LPCWSTR lpszText, BOOL isW)
7547 SIZE stringSize;
7549 stringSize.cx = 0;
7550 if (is_text(lpszText))
7552 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
7553 HDC hdc = GetDC(infoPtr->hwndSelf);
7554 HFONT hOldFont = SelectObject(hdc, hFont);
7556 if (isW)
7557 GetTextExtentPointW(hdc, lpszText, lstrlenW(lpszText), &stringSize);
7558 else
7559 GetTextExtentPointA(hdc, (LPCSTR)lpszText, lstrlenA((LPCSTR)lpszText), &stringSize);
7560 SelectObject(hdc, hOldFont);
7561 ReleaseDC(infoPtr->hwndSelf, hdc);
7563 return stringSize.cx;
7566 /***
7567 * DESCRIPTION:
7568 * Determines which listview item is located at the specified position.
7570 * PARAMETER(S):
7571 * [I] infoPtr : valid pointer to the listview structure
7572 * [IO] lpht : hit test information
7573 * [I] subitem : fill out iSubItem.
7574 * [I] select : return the index only if the hit selects the item
7576 * NOTE:
7577 * (mm 20001022): We must not allow iSubItem to be touched, for
7578 * an app might pass only a structure with space up to iItem!
7579 * (MS Office 97 does that for instance in the file open dialog)
7581 * RETURN:
7582 * SUCCESS : item index
7583 * FAILURE : -1
7585 static INT LISTVIEW_HitTest(const LISTVIEW_INFO *infoPtr, LPLVHITTESTINFO lpht, BOOL subitem, BOOL select)
7587 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
7588 RECT rcBox, rcBounds, rcState, rcIcon, rcLabel, rcSearch;
7589 POINT Origin, Position, opt;
7590 BOOL is_fullrow;
7591 LVITEMW lvItem;
7592 ITERATOR i;
7593 INT iItem;
7595 TRACE("(pt=%s, subitem=%d, select=%d)\n", wine_dbgstr_point(&lpht->pt), subitem, select);
7597 lpht->flags = 0;
7598 lpht->iItem = -1;
7599 if (subitem) lpht->iSubItem = 0;
7601 LISTVIEW_GetOrigin(infoPtr, &Origin);
7603 /* set whole list relation flags */
7604 if (subitem && infoPtr->uView == LV_VIEW_DETAILS)
7606 /* LVM_SUBITEMHITTEST checks left bound of possible client area */
7607 if (infoPtr->rcList.left > lpht->pt.x && Origin.x < lpht->pt.x)
7608 lpht->flags |= LVHT_TOLEFT;
7610 if (lpht->pt.y < infoPtr->rcList.top && lpht->pt.y >= 0)
7611 opt.y = lpht->pt.y + infoPtr->rcList.top;
7612 else
7613 opt.y = lpht->pt.y;
7615 if (infoPtr->rcList.bottom < opt.y)
7616 lpht->flags |= LVHT_BELOW;
7618 else
7620 if (infoPtr->rcList.left > lpht->pt.x)
7621 lpht->flags |= LVHT_TOLEFT;
7622 else if (infoPtr->rcList.right < lpht->pt.x)
7623 lpht->flags |= LVHT_TORIGHT;
7625 if (infoPtr->rcList.top > lpht->pt.y)
7626 lpht->flags |= LVHT_ABOVE;
7627 else if (infoPtr->rcList.bottom < lpht->pt.y)
7628 lpht->flags |= LVHT_BELOW;
7631 /* even if item is invalid try to find subitem */
7632 if (infoPtr->uView == LV_VIEW_DETAILS && subitem)
7634 RECT *pRect;
7635 INT j;
7637 opt.x = lpht->pt.x - Origin.x;
7639 lpht->iSubItem = -1;
7640 for (j = 0; j < DPA_GetPtrCount(infoPtr->hdpaColumns); j++)
7642 pRect = &LISTVIEW_GetColumnInfo(infoPtr, j)->rcHeader;
7644 if ((opt.x >= pRect->left) && (opt.x < pRect->right))
7646 lpht->iSubItem = j;
7647 break;
7650 TRACE("lpht->iSubItem=%d\n", lpht->iSubItem);
7652 /* if we're outside horizontal columns bounds there's nothing to test further */
7653 if (lpht->iSubItem == -1)
7655 lpht->iItem = -1;
7656 lpht->flags = LVHT_NOWHERE;
7657 return -1;
7661 TRACE("lpht->flags=0x%x\n", lpht->flags);
7662 if (lpht->flags) return -1;
7664 lpht->flags |= LVHT_NOWHERE;
7666 /* first deal with the large items */
7667 rcSearch.left = lpht->pt.x;
7668 rcSearch.top = lpht->pt.y;
7669 rcSearch.right = rcSearch.left + 1;
7670 rcSearch.bottom = rcSearch.top + 1;
7672 iterator_frameditems(&i, infoPtr, &rcSearch);
7673 iterator_next(&i); /* go to first item in the sequence */
7674 iItem = i.nItem;
7675 iterator_destroy(&i);
7677 TRACE("lpht->iItem=%d\n", iItem);
7678 if (iItem == -1) return -1;
7680 lvItem.mask = LVIF_STATE | LVIF_TEXT;
7681 if (infoPtr->uView == LV_VIEW_DETAILS) lvItem.mask |= LVIF_INDENT;
7682 lvItem.stateMask = LVIS_STATEIMAGEMASK;
7683 if (infoPtr->uView == LV_VIEW_ICON) lvItem.stateMask |= LVIS_FOCUSED;
7684 lvItem.iItem = iItem;
7685 lvItem.iSubItem = subitem ? lpht->iSubItem : 0;
7686 lvItem.pszText = szDispText;
7687 lvItem.cchTextMax = DISP_TEXT_SIZE;
7688 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return -1;
7689 if (!infoPtr->bFocus) lvItem.state &= ~LVIS_FOCUSED;
7691 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, &rcBox, NULL, &rcIcon, &rcState, &rcLabel);
7692 LISTVIEW_GetItemOrigin(infoPtr, iItem, &Position);
7693 opt.x = lpht->pt.x - Position.x - Origin.x;
7695 if (lpht->pt.y < infoPtr->rcList.top && lpht->pt.y >= 0)
7696 opt.y = lpht->pt.y - Position.y - Origin.y + infoPtr->rcList.top;
7697 else
7698 opt.y = lpht->pt.y - Position.y - Origin.y;
7700 if (infoPtr->uView == LV_VIEW_DETAILS)
7702 rcBounds = rcBox;
7703 if (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT)
7704 opt.x = lpht->pt.x - Origin.x;
7706 else
7708 UnionRect(&rcBounds, &rcIcon, &rcLabel);
7709 UnionRect(&rcBounds, &rcBounds, &rcState);
7711 TRACE("rcBounds=%s\n", wine_dbgstr_rect(&rcBounds));
7712 if (!PtInRect(&rcBounds, opt)) return -1;
7714 /* That's a special case - row rectangle is used as item rectangle and
7715 returned flags contain all item parts. */
7716 is_fullrow = (infoPtr->uView == LV_VIEW_DETAILS) && ((infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) || (infoPtr->dwStyle & LVS_OWNERDRAWFIXED));
7718 if (PtInRect(&rcIcon, opt))
7719 lpht->flags |= LVHT_ONITEMICON;
7720 else if (PtInRect(&rcLabel, opt))
7721 lpht->flags |= LVHT_ONITEMLABEL;
7722 else if (infoPtr->himlState && PtInRect(&rcState, opt))
7723 lpht->flags |= LVHT_ONITEMSTATEICON;
7724 if (is_fullrow && !(lpht->flags & LVHT_ONITEM))
7726 lpht->flags = LVHT_ONITEM | LVHT_ABOVE;
7728 if (lpht->flags & LVHT_ONITEM)
7729 lpht->flags &= ~LVHT_NOWHERE;
7730 TRACE("lpht->flags=0x%x\n", lpht->flags);
7732 if (select && !is_fullrow)
7734 if (infoPtr->uView == LV_VIEW_DETAILS)
7736 /* get main item bounds */
7737 lvItem.iSubItem = 0;
7738 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, &rcBox, NULL, &rcIcon, &rcState, &rcLabel);
7739 UnionRect(&rcBounds, &rcIcon, &rcLabel);
7740 UnionRect(&rcBounds, &rcBounds, &rcState);
7742 if (!PtInRect(&rcBounds, opt)) iItem = -1;
7744 return lpht->iItem = iItem;
7747 /***
7748 * DESCRIPTION:
7749 * Inserts a new item in the listview control.
7751 * PARAMETER(S):
7752 * [I] infoPtr : valid pointer to the listview structure
7753 * [I] lpLVItem : item information
7754 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
7756 * RETURN:
7757 * SUCCESS : new item index
7758 * FAILURE : -1
7760 static INT LISTVIEW_InsertItemT(LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem, BOOL isW)
7762 INT nItem;
7763 HDPA hdpaSubItems;
7764 NMLISTVIEW nmlv;
7765 ITEM_INFO *lpItem;
7766 ITEM_ID *lpID;
7767 BOOL is_sorted, has_changed;
7768 LVITEMW item;
7769 HWND hwndSelf = infoPtr->hwndSelf;
7771 TRACE("(item=%s, isW=%d)\n", debuglvitem_t(lpLVItem, isW), isW);
7773 if (infoPtr->dwStyle & LVS_OWNERDATA) return infoPtr->nItemCount++;
7775 /* make sure it's an item, and not a subitem; cannot insert a subitem */
7776 if (!lpLVItem || lpLVItem->iSubItem) return -1;
7778 if (!is_assignable_item(lpLVItem, infoPtr->dwStyle)) return -1;
7780 if (!(lpItem = Alloc(sizeof(ITEM_INFO)))) return -1;
7782 /* insert item in listview control data structure */
7783 if ( !(hdpaSubItems = DPA_Create(8)) ) goto fail;
7784 if ( !DPA_SetPtr(hdpaSubItems, 0, lpItem) ) assert (FALSE);
7786 /* link with id struct */
7787 if (!(lpID = Alloc(sizeof(ITEM_ID)))) goto fail;
7788 lpItem->id = lpID;
7789 lpID->item = hdpaSubItems;
7790 lpID->id = get_next_itemid(infoPtr);
7791 if ( DPA_InsertPtr(infoPtr->hdpaItemIds, infoPtr->nItemCount, lpID) == -1) goto fail;
7793 is_sorted = (infoPtr->dwStyle & (LVS_SORTASCENDING | LVS_SORTDESCENDING)) &&
7794 !(infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (LPSTR_TEXTCALLBACKW != lpLVItem->pszText);
7796 if (lpLVItem->iItem < 0 && !is_sorted) return -1;
7798 /* calculate new item index */
7799 if (is_sorted)
7801 HDPA hItem;
7802 ITEM_INFO *item_s;
7803 INT i = 0, cmpv;
7804 WCHAR *textW;
7806 textW = textdupTtoW(lpLVItem->pszText, isW);
7808 while (i < infoPtr->nItemCount)
7810 hItem = DPA_GetPtr( infoPtr->hdpaItems, i);
7811 item_s = DPA_GetPtr(hItem, 0);
7813 cmpv = textcmpWT(item_s->hdr.pszText, textW, TRUE);
7814 if (infoPtr->dwStyle & LVS_SORTDESCENDING) cmpv *= -1;
7816 if (cmpv >= 0) break;
7817 i++;
7820 textfreeT(textW, isW);
7822 nItem = i;
7824 else
7825 nItem = min(lpLVItem->iItem, infoPtr->nItemCount);
7827 TRACE("inserting at %d, sorted=%d, count=%d, iItem=%d\n", nItem, is_sorted, infoPtr->nItemCount, lpLVItem->iItem);
7828 nItem = DPA_InsertPtr( infoPtr->hdpaItems, nItem, hdpaSubItems );
7829 if (nItem == -1) goto fail;
7830 infoPtr->nItemCount++;
7832 /* shift indices first so they don't get tangled */
7833 LISTVIEW_ShiftIndices(infoPtr, nItem, 1);
7835 /* set the item attributes */
7836 if (lpLVItem->mask & (LVIF_GROUPID|LVIF_COLUMNS))
7838 /* full size structure expected - _WIN32IE >= 0x560 */
7839 item = *lpLVItem;
7841 else if (lpLVItem->mask & LVIF_INDENT)
7843 /* indent member expected - _WIN32IE >= 0x300 */
7844 memcpy(&item, lpLVItem, offsetof( LVITEMW, iGroupId ));
7846 else
7848 /* minimal structure expected */
7849 memcpy(&item, lpLVItem, offsetof( LVITEMW, iIndent ));
7851 item.iItem = nItem;
7852 if (infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES)
7854 if (item.mask & LVIF_STATE)
7856 item.stateMask |= LVIS_STATEIMAGEMASK;
7857 item.state &= ~LVIS_STATEIMAGEMASK;
7858 item.state |= INDEXTOSTATEIMAGEMASK(1);
7860 else
7862 item.mask |= LVIF_STATE;
7863 item.stateMask = LVIS_STATEIMAGEMASK;
7864 item.state = INDEXTOSTATEIMAGEMASK(1);
7868 if (!set_main_item(infoPtr, &item, TRUE, isW, &has_changed)) goto undo;
7870 /* make room for the position, if we are in the right mode */
7871 if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
7873 if (DPA_InsertPtr(infoPtr->hdpaPosX, nItem, 0) == -1)
7874 goto undo;
7875 if (DPA_InsertPtr(infoPtr->hdpaPosY, nItem, 0) == -1)
7877 DPA_DeletePtr(infoPtr->hdpaPosX, nItem);
7878 goto undo;
7882 /* send LVN_INSERTITEM notification */
7883 memset(&nmlv, 0, sizeof(NMLISTVIEW));
7884 nmlv.iItem = nItem;
7885 nmlv.lParam = lpItem->lParam;
7886 notify_listview(infoPtr, LVN_INSERTITEM, &nmlv);
7887 if (!IsWindow(hwndSelf))
7888 return -1;
7890 /* align items (set position of each item) */
7891 if (infoPtr->uView == LV_VIEW_SMALLICON || infoPtr->uView == LV_VIEW_ICON)
7893 POINT pt;
7895 if (infoPtr->dwStyle & LVS_ALIGNLEFT)
7896 LISTVIEW_NextIconPosLeft(infoPtr, &pt);
7897 else
7898 LISTVIEW_NextIconPosTop(infoPtr, &pt);
7900 LISTVIEW_MoveIconTo(infoPtr, nItem, &pt, TRUE);
7903 /* now is the invalidation fun */
7904 LISTVIEW_ScrollOnInsert(infoPtr, nItem, 1);
7905 return nItem;
7907 undo:
7908 LISTVIEW_ShiftIndices(infoPtr, nItem, -1);
7909 LISTVIEW_ShiftFocus(infoPtr, infoPtr->nFocusedItem, nItem, -1);
7910 DPA_DeletePtr(infoPtr->hdpaItems, nItem);
7911 infoPtr->nItemCount--;
7912 fail:
7913 DPA_DeletePtr(hdpaSubItems, 0);
7914 DPA_Destroy (hdpaSubItems);
7915 Free (lpItem);
7916 return -1;
7919 /***
7920 * DESCRIPTION:
7921 * Checks item visibility.
7923 * PARAMETER(S):
7924 * [I] infoPtr : valid pointer to the listview structure
7925 * [I] nFirst : item index to check for
7927 * RETURN:
7928 * Item visible : TRUE
7929 * Item invisible or failure : FALSE
7931 static BOOL LISTVIEW_IsItemVisible(const LISTVIEW_INFO *infoPtr, INT nItem)
7933 POINT Origin, Position;
7934 RECT rcItem;
7935 HDC hdc;
7936 BOOL ret;
7938 TRACE("nItem=%d\n", nItem);
7940 if (nItem < 0 || nItem >= DPA_GetPtrCount(infoPtr->hdpaItems)) return FALSE;
7942 LISTVIEW_GetOrigin(infoPtr, &Origin);
7943 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
7944 rcItem.left = Position.x + Origin.x;
7945 rcItem.top = Position.y + Origin.y;
7946 rcItem.right = rcItem.left + infoPtr->nItemWidth;
7947 rcItem.bottom = rcItem.top + infoPtr->nItemHeight;
7949 hdc = GetDC(infoPtr->hwndSelf);
7950 if (!hdc) return FALSE;
7951 ret = RectVisible(hdc, &rcItem);
7952 ReleaseDC(infoPtr->hwndSelf, hdc);
7954 return ret;
7957 /***
7958 * DESCRIPTION:
7959 * Redraws a range of items.
7961 * PARAMETER(S):
7962 * [I] infoPtr : valid pointer to the listview structure
7963 * [I] nFirst : first item
7964 * [I] nLast : last item
7966 * RETURN:
7967 * SUCCESS : TRUE
7968 * FAILURE : FALSE
7970 static BOOL LISTVIEW_RedrawItems(const LISTVIEW_INFO *infoPtr, INT nFirst, INT nLast)
7972 INT i;
7974 for (i = max(nFirst, 0); i <= min(nLast, infoPtr->nItemCount - 1); i++)
7975 LISTVIEW_InvalidateItem(infoPtr, i);
7977 return TRUE;
7980 /***
7981 * DESCRIPTION:
7982 * Scroll the content of a listview.
7984 * PARAMETER(S):
7985 * [I] infoPtr : valid pointer to the listview structure
7986 * [I] dx : horizontal scroll amount in pixels
7987 * [I] dy : vertical scroll amount in pixels
7989 * RETURN:
7990 * SUCCESS : TRUE
7991 * FAILURE : FALSE
7993 * COMMENTS:
7994 * If the control is in report view (LV_VIEW_DETAILS) the control can
7995 * be scrolled only in line increments. "dy" will be rounded to the
7996 * nearest number of pixels that are a whole line. Ex: if line height
7997 * is 16 and an 8 is passed, the list will be scrolled by 16. If a 7
7998 * is passed, then the scroll will be 0. (per MSDN 7/2002)
8000 static BOOL LISTVIEW_Scroll(LISTVIEW_INFO *infoPtr, INT dx, INT dy)
8002 switch(infoPtr->uView) {
8003 case LV_VIEW_DETAILS:
8004 dy += (dy < 0 ? -1 : 1) * infoPtr->nItemHeight/2;
8005 dy /= infoPtr->nItemHeight;
8006 break;
8007 case LV_VIEW_LIST:
8008 if (dy != 0) return FALSE;
8009 break;
8010 default: /* icon */
8011 break;
8014 if (dx != 0) LISTVIEW_HScroll(infoPtr, SB_INTERNAL, dx);
8015 if (dy != 0) LISTVIEW_VScroll(infoPtr, SB_INTERNAL, dy);
8017 return TRUE;
8020 /***
8021 * DESCRIPTION:
8022 * Sets the background color.
8024 * PARAMETER(S):
8025 * [I] infoPtr : valid pointer to the listview structure
8026 * [I] color : background color
8028 * RETURN:
8029 * SUCCESS : TRUE
8030 * FAILURE : FALSE
8032 static BOOL LISTVIEW_SetBkColor(LISTVIEW_INFO *infoPtr, COLORREF color)
8034 TRACE("color %lx\n", color);
8036 if(infoPtr->clrBk != color) {
8037 if (infoPtr->clrBk != CLR_NONE) DeleteObject(infoPtr->hBkBrush);
8038 infoPtr->clrBk = color;
8039 if (color == CLR_NONE)
8040 infoPtr->hBkBrush = (HBRUSH)GetClassLongPtrW(infoPtr->hwndSelf, GCLP_HBRBACKGROUND);
8041 else
8043 infoPtr->hBkBrush = CreateSolidBrush(color);
8044 infoPtr->dwLvExStyle &= ~LVS_EX_TRANSPARENTBKGND;
8048 return TRUE;
8051 static BOOL LISTVIEW_SetBkImage(LISTVIEW_INFO *infoPtr, const LVBKIMAGEW *image, BOOL isW)
8053 TRACE("%08lx, %p, %p, %u, %d, %d\n", image->ulFlags, image->hbm, image->pszImage,
8054 image->cchImageMax, image->xOffsetPercent, image->yOffsetPercent);
8056 if (image->ulFlags & ~LVBKIF_SOURCE_MASK)
8057 FIXME("unsupported flags %08lx\n", image->ulFlags & ~LVBKIF_SOURCE_MASK);
8059 if (image->xOffsetPercent || image->yOffsetPercent)
8060 FIXME("unsupported offset %d,%d\n", image->xOffsetPercent, image->yOffsetPercent);
8062 switch (image->ulFlags & LVBKIF_SOURCE_MASK)
8064 case LVBKIF_SOURCE_NONE:
8065 if (infoPtr->hBkBitmap)
8067 DeleteObject(infoPtr->hBkBitmap);
8068 infoPtr->hBkBitmap = NULL;
8070 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
8071 break;
8073 case LVBKIF_SOURCE_HBITMAP:
8075 BITMAP bm;
8077 if (infoPtr->hBkBitmap)
8079 DeleteObject(infoPtr->hBkBitmap);
8080 infoPtr->hBkBitmap = NULL;
8082 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
8083 if (GetObjectW(image->hbm, sizeof(bm), &bm) == sizeof(bm))
8085 infoPtr->hBkBitmap = image->hbm;
8086 return TRUE;
8088 break;
8091 case LVBKIF_SOURCE_URL:
8092 FIXME("LVBKIF_SOURCE_URL: %s\n", isW ? debugstr_w(image->pszImage) : debugstr_a((LPCSTR)image->pszImage));
8093 break;
8096 return FALSE;
8099 /*** Helper for {Insert,Set}ColumnT *only* */
8100 static void column_fill_hditem(const LISTVIEW_INFO *infoPtr, HDITEMW *lphdi, INT nColumn,
8101 const LVCOLUMNW *lpColumn, BOOL isW)
8103 if (lpColumn->mask & LVCF_FMT)
8105 /* format member is valid */
8106 lphdi->mask |= HDI_FORMAT;
8108 /* set text alignment (leftmost column must be left-aligned) */
8109 if (nColumn == 0 || (lpColumn->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_LEFT)
8110 lphdi->fmt |= HDF_LEFT;
8111 else if ((lpColumn->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_RIGHT)
8112 lphdi->fmt |= HDF_RIGHT;
8113 else if ((lpColumn->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_CENTER)
8114 lphdi->fmt |= HDF_CENTER;
8116 if (lpColumn->fmt & LVCFMT_BITMAP_ON_RIGHT)
8117 lphdi->fmt |= HDF_BITMAP_ON_RIGHT;
8119 if (lpColumn->fmt & LVCFMT_COL_HAS_IMAGES)
8121 lphdi->fmt |= HDF_IMAGE;
8122 lphdi->iImage = I_IMAGECALLBACK;
8125 if (lpColumn->fmt & LVCFMT_FIXED_WIDTH)
8126 lphdi->fmt |= HDF_FIXEDWIDTH;
8129 if (lpColumn->mask & LVCF_WIDTH)
8131 lphdi->mask |= HDI_WIDTH;
8132 if(lpColumn->cx == LVSCW_AUTOSIZE_USEHEADER)
8134 /* make it fill the remainder of the controls width */
8135 RECT rcHeader;
8136 INT item_index;
8138 for(item_index = 0; item_index < (nColumn - 1); item_index++)
8140 LISTVIEW_GetHeaderRect(infoPtr, item_index, &rcHeader);
8141 lphdi->cxy += rcHeader.right - rcHeader.left;
8144 /* retrieve the layout of the header */
8145 GetClientRect(infoPtr->hwndSelf, &rcHeader);
8146 TRACE("start cxy=%d rcHeader=%s\n", lphdi->cxy, wine_dbgstr_rect(&rcHeader));
8148 lphdi->cxy = (rcHeader.right - rcHeader.left) - lphdi->cxy;
8150 else
8151 lphdi->cxy = lpColumn->cx;
8154 if (lpColumn->mask & LVCF_TEXT)
8156 lphdi->mask |= HDI_TEXT | HDI_FORMAT;
8157 lphdi->fmt |= HDF_STRING;
8158 lphdi->pszText = lpColumn->pszText;
8159 lphdi->cchTextMax = textlenT(lpColumn->pszText, isW);
8162 if (lpColumn->mask & LVCF_IMAGE)
8164 lphdi->mask |= HDI_IMAGE;
8165 lphdi->iImage = lpColumn->iImage;
8168 if (lpColumn->mask & LVCF_ORDER)
8170 lphdi->mask |= HDI_ORDER;
8171 lphdi->iOrder = lpColumn->iOrder;
8176 /***
8177 * DESCRIPTION:
8178 * Inserts a new column.
8180 * PARAMETER(S):
8181 * [I] infoPtr : valid pointer to the listview structure
8182 * [I] nColumn : column index
8183 * [I] lpColumn : column information
8184 * [I] isW : TRUE if lpColumn is Unicode, FALSE otherwise
8186 * RETURN:
8187 * SUCCESS : new column index
8188 * FAILURE : -1
8190 static INT LISTVIEW_InsertColumnT(LISTVIEW_INFO *infoPtr, INT nColumn,
8191 const LVCOLUMNW *lpColumn, BOOL isW)
8193 COLUMN_INFO *lpColumnInfo;
8194 INT nNewColumn;
8195 HDITEMW hdi;
8197 TRACE("(nColumn=%d, lpColumn=%s, isW=%d)\n", nColumn, debuglvcolumn_t(lpColumn, isW), isW);
8199 if (!lpColumn || nColumn < 0) return -1;
8200 nColumn = min(nColumn, DPA_GetPtrCount(infoPtr->hdpaColumns));
8202 ZeroMemory(&hdi, sizeof(HDITEMW));
8203 column_fill_hditem(infoPtr, &hdi, nColumn, lpColumn, isW);
8206 * A mask not including LVCF_WIDTH turns into a mask of width, width 10
8207 * (can be seen in SPY) otherwise column never gets added.
8209 if (!(lpColumn->mask & LVCF_WIDTH)) {
8210 hdi.mask |= HDI_WIDTH;
8211 hdi.cxy = 10;
8215 * when the iSubItem is available Windows copies it to the header lParam. It seems
8216 * to happen only in LVM_INSERTCOLUMN - not in LVM_SETCOLUMN
8218 if (lpColumn->mask & LVCF_SUBITEM)
8220 hdi.mask |= HDI_LPARAM;
8221 hdi.lParam = lpColumn->iSubItem;
8224 /* create header if not present */
8225 LISTVIEW_CreateHeader(infoPtr);
8226 if (!(LVS_NOCOLUMNHEADER & infoPtr->dwStyle) &&
8227 (infoPtr->uView == LV_VIEW_DETAILS) && (WS_VISIBLE & infoPtr->dwStyle))
8229 ShowWindow(infoPtr->hwndHeader, SW_SHOWNORMAL);
8232 /* insert item in header control */
8233 nNewColumn = SendMessageW(infoPtr->hwndHeader,
8234 isW ? HDM_INSERTITEMW : HDM_INSERTITEMA,
8235 nColumn, (LPARAM)&hdi);
8236 if (nNewColumn == -1) return -1;
8237 if (nNewColumn != nColumn) ERR("nColumn=%d, nNewColumn=%d\n", nColumn, nNewColumn);
8239 /* create our own column info */
8240 if (!(lpColumnInfo = Alloc(sizeof(COLUMN_INFO)))) goto fail;
8241 if (DPA_InsertPtr(infoPtr->hdpaColumns, nNewColumn, lpColumnInfo) == -1) goto fail;
8243 if (lpColumn->mask & LVCF_FMT) lpColumnInfo->fmt = lpColumn->fmt;
8244 if (lpColumn->mask & LVCF_MINWIDTH) lpColumnInfo->cxMin = lpColumn->cxMin;
8245 if (!SendMessageW(infoPtr->hwndHeader, HDM_GETITEMRECT, nNewColumn, (LPARAM)&lpColumnInfo->rcHeader))
8246 goto fail;
8248 /* now we have to actually adjust the data */
8249 if (!(infoPtr->dwStyle & LVS_OWNERDATA) && infoPtr->nItemCount > 0)
8251 SUBITEM_INFO *lpSubItem;
8252 HDPA hdpaSubItems;
8253 INT nItem, i;
8254 LVITEMW item;
8255 BOOL changed;
8257 item.iSubItem = nNewColumn;
8258 item.mask = LVIF_TEXT | LVIF_IMAGE;
8259 item.iImage = I_IMAGECALLBACK;
8260 item.pszText = LPSTR_TEXTCALLBACKW;
8262 for (nItem = 0; nItem < infoPtr->nItemCount; nItem++)
8264 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, nItem);
8265 for (i = 1; i < DPA_GetPtrCount(hdpaSubItems); i++)
8267 lpSubItem = DPA_GetPtr(hdpaSubItems, i);
8268 if (lpSubItem->iSubItem >= nNewColumn)
8269 lpSubItem->iSubItem++;
8272 /* add new subitem for each item */
8273 item.iItem = nItem;
8274 set_sub_item(infoPtr, &item, isW, &changed);
8278 /* make space for the new column */
8279 LISTVIEW_ScrollColumns(infoPtr, nNewColumn + 1, lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left);
8280 LISTVIEW_UpdateItemSize(infoPtr);
8282 return nNewColumn;
8284 fail:
8285 if (nNewColumn != -1) SendMessageW(infoPtr->hwndHeader, HDM_DELETEITEM, nNewColumn, 0);
8286 if (lpColumnInfo)
8288 DPA_DeletePtr(infoPtr->hdpaColumns, nNewColumn);
8289 Free(lpColumnInfo);
8291 return -1;
8294 /***
8295 * DESCRIPTION:
8296 * Sets the attributes of a header item.
8298 * PARAMETER(S):
8299 * [I] infoPtr : valid pointer to the listview structure
8300 * [I] nColumn : column index
8301 * [I] lpColumn : column attributes
8302 * [I] isW: if TRUE, then lpColumn is a LPLVCOLUMNW, else it is a LPLVCOLUMNA
8304 * RETURN:
8305 * SUCCESS : TRUE
8306 * FAILURE : FALSE
8308 static BOOL LISTVIEW_SetColumnT(const LISTVIEW_INFO *infoPtr, INT nColumn,
8309 const LVCOLUMNW *lpColumn, BOOL isW)
8311 HDITEMW hdi, hdiget;
8312 BOOL bResult;
8314 TRACE("(nColumn=%d, lpColumn=%s, isW=%d)\n", nColumn, debuglvcolumn_t(lpColumn, isW), isW);
8316 if (!lpColumn || nColumn < 0 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
8318 ZeroMemory(&hdi, sizeof(HDITEMW));
8319 if (lpColumn->mask & LVCF_FMT)
8321 hdi.mask |= HDI_FORMAT;
8322 hdiget.mask = HDI_FORMAT;
8323 if (SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nColumn, (LPARAM)&hdiget))
8324 hdi.fmt = hdiget.fmt & HDF_STRING;
8326 column_fill_hditem(infoPtr, &hdi, nColumn, lpColumn, isW);
8328 /* set header item attributes */
8329 bResult = SendMessageW(infoPtr->hwndHeader, isW ? HDM_SETITEMW : HDM_SETITEMA, nColumn, (LPARAM)&hdi);
8330 if (!bResult) return FALSE;
8332 if (lpColumn->mask & LVCF_FMT)
8334 COLUMN_INFO *lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, nColumn);
8335 INT oldFmt = lpColumnInfo->fmt;
8337 lpColumnInfo->fmt = lpColumn->fmt;
8338 if ((oldFmt ^ lpColumn->fmt) & (LVCFMT_JUSTIFYMASK | LVCFMT_IMAGE))
8340 if (infoPtr->uView == LV_VIEW_DETAILS) LISTVIEW_InvalidateColumn(infoPtr, nColumn);
8344 if (lpColumn->mask & LVCF_MINWIDTH)
8345 LISTVIEW_GetColumnInfo(infoPtr, nColumn)->cxMin = lpColumn->cxMin;
8347 return TRUE;
8350 /***
8351 * DESCRIPTION:
8352 * Sets the column order array
8354 * PARAMETERS:
8355 * [I] infoPtr : valid pointer to the listview structure
8356 * [I] iCount : number of elements in column order array
8357 * [I] lpiArray : pointer to column order array
8359 * RETURN:
8360 * SUCCESS : TRUE
8361 * FAILURE : FALSE
8363 static BOOL LISTVIEW_SetColumnOrderArray(LISTVIEW_INFO *infoPtr, INT iCount, const INT *lpiArray)
8365 if (!infoPtr->hwndHeader) return FALSE;
8366 infoPtr->colRectsDirty = TRUE;
8367 return SendMessageW(infoPtr->hwndHeader, HDM_SETORDERARRAY, iCount, (LPARAM)lpiArray);
8370 /***
8371 * DESCRIPTION:
8372 * Sets the width of a column
8374 * PARAMETERS:
8375 * [I] infoPtr : valid pointer to the listview structure
8376 * [I] nColumn : column index
8377 * [I] cx : column width
8379 * RETURN:
8380 * SUCCESS : TRUE
8381 * FAILURE : FALSE
8383 static BOOL LISTVIEW_SetColumnWidth(LISTVIEW_INFO *infoPtr, INT nColumn, INT cx)
8385 WCHAR szDispText[DISP_TEXT_SIZE] = { 0 };
8386 INT max_cx = 0;
8387 HDITEMW hdi;
8389 TRACE("(nColumn=%d, cx=%d)\n", nColumn, cx);
8391 /* set column width only if in report or list mode */
8392 if (infoPtr->uView != LV_VIEW_DETAILS && infoPtr->uView != LV_VIEW_LIST) return FALSE;
8394 /* take care of invalid cx values - LVSCW_AUTOSIZE_* values are negative,
8395 with _USEHEADER being the lowest */
8396 if (infoPtr->uView == LV_VIEW_DETAILS && cx < LVSCW_AUTOSIZE_USEHEADER) cx = LVSCW_AUTOSIZE;
8397 else if (infoPtr->uView == LV_VIEW_LIST && cx <= 0) return FALSE;
8399 /* resize all columns if in LV_VIEW_LIST mode */
8400 if(infoPtr->uView == LV_VIEW_LIST)
8402 infoPtr->nItemWidth = cx;
8403 LISTVIEW_InvalidateList(infoPtr);
8404 return TRUE;
8407 if (nColumn < 0 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
8409 if (cx == LVSCW_AUTOSIZE || (cx == LVSCW_AUTOSIZE_USEHEADER && nColumn < DPA_GetPtrCount(infoPtr->hdpaColumns) -1))
8411 INT nLabelWidth;
8412 LVITEMW lvItem;
8414 lvItem.mask = LVIF_TEXT;
8415 lvItem.iItem = 0;
8416 lvItem.iSubItem = nColumn;
8417 lvItem.cchTextMax = DISP_TEXT_SIZE;
8418 for (; lvItem.iItem < infoPtr->nItemCount; lvItem.iItem++)
8420 lvItem.pszText = szDispText;
8421 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) continue;
8422 nLabelWidth = LISTVIEW_GetStringWidthT(infoPtr, lvItem.pszText, TRUE);
8423 if (max_cx < nLabelWidth) max_cx = nLabelWidth;
8425 if (infoPtr->himlSmall && (nColumn == 0 || (LISTVIEW_GetColumnInfo(infoPtr, nColumn)->fmt & LVCFMT_IMAGE)))
8426 max_cx += infoPtr->iconSize.cx;
8427 max_cx += TRAILING_LABEL_PADDING;
8428 if (nColumn == 0 && (infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES))
8429 max_cx += GetSystemMetrics(SM_CXSMICON);
8432 /* autosize based on listview items width */
8433 if(cx == LVSCW_AUTOSIZE)
8434 cx = max_cx;
8435 else if(cx == LVSCW_AUTOSIZE_USEHEADER)
8437 /* if iCol is the last column make it fill the remainder of the controls width */
8438 if(nColumn == DPA_GetPtrCount(infoPtr->hdpaColumns) - 1)
8440 RECT rcHeader;
8441 POINT Origin;
8443 LISTVIEW_GetOrigin(infoPtr, &Origin);
8444 LISTVIEW_GetHeaderRect(infoPtr, nColumn, &rcHeader);
8446 cx = infoPtr->rcList.right - Origin.x - rcHeader.left;
8448 else
8450 /* Despite what the MS docs say, if this is not the last
8451 column, then MS resizes the column to the width of the
8452 largest text string in the column, including headers
8453 and items. This is different from LVSCW_AUTOSIZE in that
8454 LVSCW_AUTOSIZE ignores the header string length. */
8455 cx = 0;
8457 /* retrieve header text */
8458 hdi.mask = HDI_TEXT|HDI_FORMAT|HDI_IMAGE|HDI_BITMAP;
8459 hdi.cchTextMax = DISP_TEXT_SIZE;
8460 hdi.pszText = szDispText;
8461 if (SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nColumn, (LPARAM)&hdi))
8463 HDC hdc = GetDC(infoPtr->hwndSelf);
8464 HFONT old_font = SelectObject(hdc, (HFONT)SendMessageW(infoPtr->hwndHeader, WM_GETFONT, 0, 0));
8465 HIMAGELIST himl = (HIMAGELIST)SendMessageW(infoPtr->hwndHeader, HDM_GETIMAGELIST, 0, 0);
8466 INT bitmap_margin = 0;
8467 SIZE size;
8469 if (GetTextExtentPoint32W(hdc, hdi.pszText, lstrlenW(hdi.pszText), &size))
8470 cx = size.cx + TRAILING_HEADER_PADDING;
8472 if (hdi.fmt & (HDF_IMAGE|HDF_BITMAP))
8473 bitmap_margin = SendMessageW(infoPtr->hwndHeader, HDM_GETBITMAPMARGIN, 0, 0);
8475 if ((hdi.fmt & HDF_IMAGE) && himl)
8477 INT icon_cx, icon_cy;
8479 if (!ImageList_GetIconSize(himl, &icon_cx, &icon_cy))
8480 cx += icon_cx + 2*bitmap_margin;
8482 else if (hdi.fmt & HDF_BITMAP)
8484 BITMAP bmp;
8486 GetObjectW(hdi.hbm, sizeof(BITMAP), &bmp);
8487 cx += bmp.bmWidth + 2*bitmap_margin;
8490 SelectObject(hdc, old_font);
8491 ReleaseDC(infoPtr->hwndSelf, hdc);
8493 cx = max (cx, max_cx);
8497 if (cx < 0) return FALSE;
8499 /* call header to update the column change */
8500 hdi.mask = HDI_WIDTH;
8501 hdi.cxy = max(cx, LISTVIEW_GetColumnInfo(infoPtr, nColumn)->cxMin);
8502 TRACE("hdi.cxy=%d\n", hdi.cxy);
8503 return SendMessageW(infoPtr->hwndHeader, HDM_SETITEMW, nColumn, (LPARAM)&hdi);
8506 /***
8507 * Creates the checkbox imagelist. Helper for LISTVIEW_SetExtendedListViewStyle
8510 static HIMAGELIST LISTVIEW_CreateThemedCheckBoxImageList(const LISTVIEW_INFO *info)
8512 HBITMAP bitmap, old_bitmap;
8513 HIMAGELIST image_list;
8514 HDC hdc, mem_hdc;
8515 HTHEME theme;
8516 RECT rect;
8517 SIZE size;
8519 if (!GetWindowTheme(info->hwndSelf))
8520 return NULL;
8522 theme = OpenThemeDataForDpi(NULL, L"Button", GetDpiForWindow(info->hwndSelf));
8523 if (!theme)
8524 return NULL;
8526 hdc = GetDC(info->hwndSelf);
8527 GetThemePartSize(theme, hdc, BP_CHECKBOX, 0, NULL, TS_DRAW, &size);
8528 SetRect(&rect, 0, 0, size.cx, size.cy);
8529 image_list = ImageList_Create(size.cx, size.cy, ILC_COLOR32, 2, 2);
8530 mem_hdc = CreateCompatibleDC(hdc);
8531 bitmap = CreateCompatibleBitmap(hdc, size.cx, size.cy);
8532 old_bitmap = SelectObject(mem_hdc, bitmap);
8533 ReleaseDC(info->hwndSelf, hdc);
8535 if (IsThemeBackgroundPartiallyTransparent(theme, BP_CHECKBOX, CBS_UNCHECKEDNORMAL))
8536 FillRect(mem_hdc, &rect, (HBRUSH)(COLOR_WINDOW + 1));
8537 DrawThemeBackground(theme, mem_hdc, BP_CHECKBOX, CBS_UNCHECKEDNORMAL, &rect, NULL);
8538 ImageList_Add(image_list, bitmap, NULL);
8540 if (IsThemeBackgroundPartiallyTransparent(theme, BP_CHECKBOX, CBS_CHECKEDNORMAL))
8541 FillRect(mem_hdc, &rect, (HBRUSH)(COLOR_WINDOW + 1));
8542 DrawThemeBackground(theme, mem_hdc, BP_CHECKBOX, CBS_CHECKEDNORMAL, &rect, NULL);
8543 ImageList_Add(image_list, bitmap, NULL);
8545 SelectObject(mem_hdc, old_bitmap);
8546 DeleteObject(bitmap);
8547 DeleteDC(mem_hdc);
8548 CloseThemeData(theme);
8549 return image_list;
8552 static HIMAGELIST LISTVIEW_CreateCheckBoxIL(const LISTVIEW_INFO *infoPtr)
8554 HDC hdc_wnd, hdc;
8555 HBITMAP hbm_im, hbm_mask, hbm_orig;
8556 RECT rc;
8557 HBRUSH hbr_white, hbr_black;
8558 HIMAGELIST himl;
8560 himl = LISTVIEW_CreateThemedCheckBoxImageList(infoPtr);
8561 if (himl)
8562 return himl;
8564 hbr_white = GetStockObject(WHITE_BRUSH);
8565 hbr_black = GetStockObject(BLACK_BRUSH);
8566 himl = ImageList_Create(GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON),
8567 ILC_COLOR | ILC_MASK, 2, 2);
8568 hdc_wnd = GetDC(infoPtr->hwndSelf);
8569 hdc = CreateCompatibleDC(hdc_wnd);
8570 hbm_im = CreateCompatibleBitmap(hdc_wnd, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON));
8571 hbm_mask = CreateBitmap(GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), 1, 1, NULL);
8572 ReleaseDC(infoPtr->hwndSelf, hdc_wnd);
8574 SetRect(&rc, 0, 0, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON));
8575 hbm_orig = SelectObject(hdc, hbm_mask);
8576 FillRect(hdc, &rc, hbr_white);
8577 InflateRect(&rc, -2, -2);
8578 FillRect(hdc, &rc, hbr_black);
8580 SelectObject(hdc, hbm_im);
8581 DrawFrameControl(hdc, &rc, DFC_BUTTON, DFCS_BUTTONCHECK | DFCS_MONO);
8582 SelectObject(hdc, hbm_orig);
8583 ImageList_Add(himl, hbm_im, hbm_mask);
8585 SelectObject(hdc, hbm_im);
8586 DrawFrameControl(hdc, &rc, DFC_BUTTON, DFCS_BUTTONCHECK | DFCS_MONO | DFCS_CHECKED);
8587 SelectObject(hdc, hbm_orig);
8588 ImageList_Add(himl, hbm_im, hbm_mask);
8590 DeleteObject(hbm_mask);
8591 DeleteObject(hbm_im);
8592 DeleteDC(hdc);
8594 return himl;
8597 /***
8598 * DESCRIPTION:
8599 * Sets the extended listview style.
8601 * PARAMETERS:
8602 * [I] infoPtr : valid pointer to the listview structure
8603 * [I] dwMask : mask
8604 * [I] dwStyle : style
8606 * RETURN:
8607 * SUCCESS : previous style
8608 * FAILURE : 0
8610 static DWORD LISTVIEW_SetExtendedListViewStyle(LISTVIEW_INFO *infoPtr, DWORD mask, DWORD ex_style)
8612 DWORD old_ex_style = infoPtr->dwLvExStyle;
8614 TRACE("mask %#lx, ex_style %#lx\n", mask, ex_style);
8616 /* set new style */
8617 if (mask)
8618 infoPtr->dwLvExStyle = (old_ex_style & ~mask) | (ex_style & mask);
8619 else
8620 infoPtr->dwLvExStyle = ex_style;
8622 if((infoPtr->dwLvExStyle ^ old_ex_style) & LVS_EX_CHECKBOXES)
8624 HIMAGELIST himl = 0;
8625 if(infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES)
8627 LVITEMW item;
8628 item.mask = LVIF_STATE;
8629 item.stateMask = LVIS_STATEIMAGEMASK;
8630 item.state = INDEXTOSTATEIMAGEMASK(1);
8631 LISTVIEW_SetItemState(infoPtr, -1, &item);
8633 himl = LISTVIEW_CreateCheckBoxIL(infoPtr);
8634 if(!(infoPtr->dwStyle & LVS_SHAREIMAGELISTS))
8635 ImageList_Destroy(infoPtr->himlState);
8637 himl = LISTVIEW_SetImageList(infoPtr, LVSIL_STATE, himl);
8638 /* checkbox list replaces previous custom list or... */
8639 if(((infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES) &&
8640 !(infoPtr->dwStyle & LVS_SHAREIMAGELISTS)) ||
8641 /* ...previous was checkbox list */
8642 (old_ex_style & LVS_EX_CHECKBOXES))
8643 ImageList_Destroy(himl);
8646 if((infoPtr->dwLvExStyle ^ old_ex_style) & LVS_EX_HEADERDRAGDROP)
8648 DWORD style;
8650 /* if not already created */
8651 LISTVIEW_CreateHeader(infoPtr);
8653 style = GetWindowLongW(infoPtr->hwndHeader, GWL_STYLE);
8654 if (infoPtr->dwLvExStyle & LVS_EX_HEADERDRAGDROP)
8655 style |= HDS_DRAGDROP;
8656 else
8657 style &= ~HDS_DRAGDROP;
8658 SetWindowLongW(infoPtr->hwndHeader, GWL_STYLE, style);
8661 /* GRIDLINES adds decoration at top so changes sizes */
8662 if((infoPtr->dwLvExStyle ^ old_ex_style) & LVS_EX_GRIDLINES)
8664 LISTVIEW_CreateHeader(infoPtr);
8665 LISTVIEW_UpdateSize(infoPtr);
8668 if((infoPtr->dwLvExStyle ^ old_ex_style) & LVS_EX_FULLROWSELECT)
8670 LISTVIEW_CreateHeader(infoPtr);
8673 if((infoPtr->dwLvExStyle ^ old_ex_style) & LVS_EX_TRANSPARENTBKGND)
8675 if (infoPtr->dwLvExStyle & LVS_EX_TRANSPARENTBKGND)
8676 LISTVIEW_SetBkColor(infoPtr, CLR_NONE);
8679 if((infoPtr->dwLvExStyle ^ old_ex_style) & LVS_EX_HEADERINALLVIEWS)
8681 if (infoPtr->dwLvExStyle & LVS_EX_HEADERINALLVIEWS)
8682 LISTVIEW_CreateHeader(infoPtr);
8683 else
8684 ShowWindow(infoPtr->hwndHeader, SW_HIDE);
8685 LISTVIEW_UpdateSize(infoPtr);
8686 LISTVIEW_UpdateScroll(infoPtr);
8689 LISTVIEW_InvalidateList(infoPtr);
8690 return old_ex_style;
8693 /***
8694 * DESCRIPTION:
8695 * Sets the new hot cursor used during hot tracking and hover selection.
8697 * PARAMETER(S):
8698 * [I] infoPtr : valid pointer to the listview structure
8699 * [I] hCursor : the new hot cursor handle
8701 * RETURN:
8702 * Returns the previous hot cursor
8704 static HCURSOR LISTVIEW_SetHotCursor(LISTVIEW_INFO *infoPtr, HCURSOR hCursor)
8706 HCURSOR oldCursor = infoPtr->hHotCursor;
8708 infoPtr->hHotCursor = hCursor;
8710 return oldCursor;
8714 /***
8715 * DESCRIPTION:
8716 * Sets the hot item index.
8718 * PARAMETERS:
8719 * [I] infoPtr : valid pointer to the listview structure
8720 * [I] iIndex : index
8722 * RETURN:
8723 * SUCCESS : previous hot item index
8724 * FAILURE : -1 (no hot item)
8726 static INT LISTVIEW_SetHotItem(LISTVIEW_INFO *infoPtr, INT iIndex)
8728 INT iOldIndex = infoPtr->nHotItem;
8730 infoPtr->nHotItem = iIndex;
8732 return iOldIndex;
8736 /***
8737 * DESCRIPTION:
8738 * Sets the amount of time the cursor must hover over an item before it is selected.
8740 * PARAMETER(S):
8741 * [I] infoPtr : valid pointer to the listview structure
8742 * [I] dwHoverTime : hover time, if -1 the hover time is set to the default
8744 * RETURN:
8745 * Returns the previous hover time
8747 static DWORD LISTVIEW_SetHoverTime(LISTVIEW_INFO *infoPtr, DWORD dwHoverTime)
8749 DWORD oldHoverTime = infoPtr->dwHoverTime;
8751 infoPtr->dwHoverTime = dwHoverTime;
8753 return oldHoverTime;
8756 /***
8757 * DESCRIPTION:
8758 * Sets spacing for icons of LVS_ICON style.
8760 * PARAMETER(S):
8761 * [I] infoPtr : valid pointer to the listview structure
8762 * [I] cx : horizontal spacing (-1 = system spacing, 0 = autosize)
8763 * [I] cy : vertical spacing (-1 = system spacing, 0 = autosize)
8765 * RETURN:
8766 * MAKELONG(oldcx, oldcy)
8768 static DWORD LISTVIEW_SetIconSpacing(LISTVIEW_INFO *infoPtr, INT cx, INT cy)
8770 INT iconWidth = 0, iconHeight = 0;
8771 DWORD oldspacing = MAKELONG(infoPtr->iconSpacing.cx, infoPtr->iconSpacing.cy);
8773 TRACE("requested=(%d,%d)\n", cx, cy);
8775 /* set to defaults, if instructed to */
8776 if (cx == -1 && cy == -1)
8778 infoPtr->autoSpacing = TRUE;
8779 if (infoPtr->himlNormal)
8780 ImageList_GetIconSize(infoPtr->himlNormal, &iconWidth, &iconHeight);
8781 cx = GetSystemMetrics(SM_CXICONSPACING) - GetSystemMetrics(SM_CXICON) + iconWidth;
8782 cy = GetSystemMetrics(SM_CYICONSPACING) - GetSystemMetrics(SM_CYICON) + iconHeight;
8784 else
8785 infoPtr->autoSpacing = FALSE;
8787 /* if 0 then keep width */
8788 if (cx != 0)
8789 infoPtr->iconSpacing.cx = cx;
8791 /* if 0 then keep height */
8792 if (cy != 0)
8793 infoPtr->iconSpacing.cy = cy;
8795 TRACE("old=(%d,%d), new=(%ld,%ld), iconSize=(%ld,%ld), ntmH=%d\n",
8796 LOWORD(oldspacing), HIWORD(oldspacing), infoPtr->iconSpacing.cx, infoPtr->iconSpacing.cy,
8797 infoPtr->iconSize.cx, infoPtr->iconSize.cy,
8798 infoPtr->ntmHeight);
8800 /* these depend on the iconSpacing */
8801 LISTVIEW_UpdateItemSize(infoPtr);
8803 return oldspacing;
8806 static inline void set_icon_size(SIZE *size, HIMAGELIST himl, BOOL is_small)
8808 INT cx, cy;
8810 if (himl && ImageList_GetIconSize(himl, &cx, &cy))
8812 size->cx = cx;
8813 size->cy = cy;
8815 else
8817 size->cx = GetSystemMetrics(is_small ? SM_CXSMICON : SM_CXICON);
8818 size->cy = GetSystemMetrics(is_small ? SM_CYSMICON : SM_CYICON);
8822 /***
8823 * DESCRIPTION:
8824 * Sets image lists.
8826 * PARAMETER(S):
8827 * [I] infoPtr : valid pointer to the listview structure
8828 * [I] nType : image list type
8829 * [I] himl : image list handle
8831 * RETURN:
8832 * SUCCESS : old image list
8833 * FAILURE : NULL
8835 static HIMAGELIST LISTVIEW_SetImageList(LISTVIEW_INFO *infoPtr, INT nType, HIMAGELIST himl)
8837 INT oldHeight = infoPtr->nItemHeight;
8838 HIMAGELIST himlOld = 0;
8840 TRACE("(nType=%d, himl=%p)\n", nType, himl);
8842 switch (nType)
8844 case LVSIL_NORMAL:
8845 himlOld = infoPtr->himlNormal;
8846 infoPtr->himlNormal = himl;
8847 if (infoPtr->uView == LV_VIEW_ICON) set_icon_size(&infoPtr->iconSize, himl, FALSE);
8848 if (infoPtr->autoSpacing)
8849 LISTVIEW_SetIconSpacing(infoPtr, -1, -1);
8850 break;
8852 case LVSIL_SMALL:
8853 himlOld = infoPtr->himlSmall;
8854 infoPtr->himlSmall = himl;
8855 if (infoPtr->uView != LV_VIEW_ICON) set_icon_size(&infoPtr->iconSize, himl, TRUE);
8856 if (infoPtr->hwndHeader)
8857 SendMessageW(infoPtr->hwndHeader, HDM_SETIMAGELIST, 0, (LPARAM)himl);
8858 break;
8860 case LVSIL_STATE:
8861 himlOld = infoPtr->himlState;
8862 infoPtr->himlState = himl;
8863 set_icon_size(&infoPtr->iconStateSize, himl, TRUE);
8864 ImageList_SetBkColor(infoPtr->himlState, CLR_NONE);
8865 break;
8867 default:
8868 ERR("Unknown icon type=%d\n", nType);
8869 return NULL;
8872 infoPtr->nItemHeight = LISTVIEW_CalculateItemHeight(infoPtr);
8873 if (infoPtr->nItemHeight != oldHeight)
8874 LISTVIEW_UpdateScroll(infoPtr);
8876 return himlOld;
8879 /***
8880 * DESCRIPTION:
8881 * Preallocates memory (does *not* set the actual count of items !)
8883 * PARAMETER(S):
8884 * [I] infoPtr : valid pointer to the listview structure
8885 * [I] nItems : item count (projected number of items to allocate)
8886 * [I] dwFlags : update flags
8888 * RETURN:
8889 * SUCCESS : TRUE
8890 * FAILURE : FALSE
8892 static BOOL LISTVIEW_SetItemCount(LISTVIEW_INFO *infoPtr, INT nItems, DWORD dwFlags)
8894 TRACE("nItems %d, flags %#lx\n", nItems, dwFlags);
8896 if (infoPtr->dwStyle & LVS_OWNERDATA)
8898 INT nOldCount = infoPtr->nItemCount;
8899 infoPtr->nItemCount = nItems;
8901 if (nItems < nOldCount)
8903 RANGE range = { nItems, nOldCount };
8904 ranges_del(infoPtr->selectionRanges, range);
8905 if (infoPtr->nFocusedItem >= nItems)
8907 LISTVIEW_SetItemFocus(infoPtr, -1);
8908 infoPtr->nFocusedItem = -1;
8909 SetRectEmpty(&infoPtr->rcFocus);
8913 LISTVIEW_UpdateScroll(infoPtr);
8915 /* the flags are valid only in ownerdata report and list modes */
8916 if (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON) dwFlags = 0;
8918 if (!(dwFlags & LVSICF_NOSCROLL) && infoPtr->nFocusedItem != -1)
8919 LISTVIEW_EnsureVisible(infoPtr, infoPtr->nFocusedItem, FALSE);
8921 if (!(dwFlags & LVSICF_NOINVALIDATEALL))
8922 LISTVIEW_InvalidateList(infoPtr);
8923 else
8925 INT nFrom, nTo;
8926 POINT Origin;
8927 RECT rcErase;
8929 LISTVIEW_GetOrigin(infoPtr, &Origin);
8930 nFrom = min(nOldCount, nItems);
8931 nTo = max(nOldCount, nItems);
8933 if (infoPtr->uView == LV_VIEW_DETAILS)
8935 SetRect(&rcErase, 0, nFrom * infoPtr->nItemHeight, infoPtr->nItemWidth,
8936 nTo * infoPtr->nItemHeight);
8937 OffsetRect(&rcErase, Origin.x, Origin.y);
8938 if (IntersectRect(&rcErase, &rcErase, &infoPtr->rcList))
8939 LISTVIEW_InvalidateRect(infoPtr, &rcErase);
8941 else /* LV_VIEW_LIST */
8943 INT nPerCol = LISTVIEW_GetCountPerColumn(infoPtr);
8945 rcErase.left = (nFrom / nPerCol) * infoPtr->nItemWidth;
8946 rcErase.top = (nFrom % nPerCol) * infoPtr->nItemHeight;
8947 rcErase.right = rcErase.left + infoPtr->nItemWidth;
8948 rcErase.bottom = nPerCol * infoPtr->nItemHeight;
8949 OffsetRect(&rcErase, Origin.x, Origin.y);
8950 if (IntersectRect(&rcErase, &rcErase, &infoPtr->rcList))
8951 LISTVIEW_InvalidateRect(infoPtr, &rcErase);
8953 rcErase.left = (nFrom / nPerCol + 1) * infoPtr->nItemWidth;
8954 rcErase.top = 0;
8955 rcErase.right = (nTo / nPerCol + 1) * infoPtr->nItemWidth;
8956 rcErase.bottom = nPerCol * infoPtr->nItemHeight;
8957 OffsetRect(&rcErase, Origin.x, Origin.y);
8958 if (IntersectRect(&rcErase, &rcErase, &infoPtr->rcList))
8959 LISTVIEW_InvalidateRect(infoPtr, &rcErase);
8963 else
8965 /* According to MSDN for non-LVS_OWNERDATA this is just
8966 * a performance issue. The control allocates its internal
8967 * data structures for the number of items specified. It
8968 * cuts down on the number of memory allocations. Therefore
8969 * we will just issue a WARN here
8971 WARN("for non-ownerdata performance option not implemented.\n");
8974 return TRUE;
8977 /***
8978 * DESCRIPTION:
8979 * Sets the position of an item.
8981 * PARAMETER(S):
8982 * [I] infoPtr : valid pointer to the listview structure
8983 * [I] nItem : item index
8984 * [I] pt : coordinate
8986 * RETURN:
8987 * SUCCESS : TRUE
8988 * FAILURE : FALSE
8990 static BOOL LISTVIEW_SetItemPosition(LISTVIEW_INFO *infoPtr, INT nItem, const POINT *pt)
8992 POINT Origin, Pt;
8994 TRACE("(nItem=%d, pt=%s)\n", nItem, wine_dbgstr_point(pt));
8996 if (!pt || nItem < 0 || nItem >= infoPtr->nItemCount ||
8997 !(infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON)) return FALSE;
8999 Pt = *pt;
9000 LISTVIEW_GetOrigin(infoPtr, &Origin);
9002 /* This point value seems to be an undocumented feature.
9003 * The best guess is that it means either at the origin,
9004 * or at true beginning of the list. I will assume the origin. */
9005 if ((Pt.x == -1) && (Pt.y == -1))
9006 Pt = Origin;
9008 if (infoPtr->uView == LV_VIEW_ICON)
9010 Pt.x -= (infoPtr->nItemWidth - infoPtr->iconSize.cx) / 2;
9011 Pt.y -= ICON_TOP_PADDING;
9013 Pt.x -= Origin.x;
9014 Pt.y -= Origin.y;
9016 return LISTVIEW_MoveIconTo(infoPtr, nItem, &Pt, FALSE);
9019 /***
9020 * DESCRIPTION:
9021 * Sets the state of one or many items.
9023 * PARAMETER(S):
9024 * [I] infoPtr : valid pointer to the listview structure
9025 * [I] nItem : item index
9026 * [I] item : item or subitem info
9028 * RETURN:
9029 * SUCCESS : TRUE
9030 * FAILURE : FALSE
9032 static BOOL LISTVIEW_SetItemState(LISTVIEW_INFO *infoPtr, INT nItem, const LVITEMW *item)
9034 BOOL ret = TRUE;
9035 LVITEMW lvItem;
9037 if (!item) return FALSE;
9039 lvItem.iItem = nItem;
9040 lvItem.iSubItem = 0;
9041 lvItem.mask = LVIF_STATE;
9042 lvItem.state = item->state;
9043 lvItem.stateMask = item->stateMask;
9044 TRACE("item=%s\n", debuglvitem_t(&lvItem, TRUE));
9046 if (nItem == -1)
9048 UINT oldstate = 0;
9049 DWORD old_mask;
9051 /* special case optimization for recurring attempt to deselect all */
9052 if (lvItem.state == 0 && lvItem.stateMask == LVIS_SELECTED && !LISTVIEW_GetSelectedCount(infoPtr))
9053 return TRUE;
9055 /* select all isn't allowed in LVS_SINGLESEL */
9056 if ((lvItem.state & lvItem.stateMask & LVIS_SELECTED) && (infoPtr->dwStyle & LVS_SINGLESEL))
9057 return FALSE;
9059 /* focus all isn't allowed */
9060 if (lvItem.state & lvItem.stateMask & LVIS_FOCUSED) return FALSE;
9062 old_mask = infoPtr->notify_mask & NOTIFY_MASK_ITEM_CHANGE;
9063 if (infoPtr->dwStyle & LVS_OWNERDATA)
9065 infoPtr->notify_mask &= ~NOTIFY_MASK_ITEM_CHANGE;
9066 if (!(lvItem.state & LVIS_SELECTED) && LISTVIEW_GetSelectedCount(infoPtr))
9067 oldstate |= LVIS_SELECTED;
9068 if (infoPtr->nFocusedItem != -1) oldstate |= LVIS_FOCUSED;
9071 /* apply to all items */
9072 for (lvItem.iItem = 0; lvItem.iItem < infoPtr->nItemCount; lvItem.iItem++)
9073 if (!LISTVIEW_SetItemT(infoPtr, &lvItem, TRUE)) ret = FALSE;
9075 if (infoPtr->dwStyle & LVS_OWNERDATA)
9077 NMLISTVIEW nmlv;
9079 infoPtr->notify_mask |= old_mask;
9081 nmlv.iItem = -1;
9082 nmlv.iSubItem = 0;
9083 nmlv.uNewState = lvItem.state & lvItem.stateMask;
9084 nmlv.uOldState = oldstate & lvItem.stateMask;
9085 nmlv.uChanged = LVIF_STATE;
9086 nmlv.ptAction.x = nmlv.ptAction.y = 0;
9087 nmlv.lParam = 0;
9089 notify_listview(infoPtr, LVN_ITEMCHANGED, &nmlv);
9092 else
9093 ret = LISTVIEW_SetItemT(infoPtr, &lvItem, TRUE);
9095 return ret;
9098 /***
9099 * DESCRIPTION:
9100 * Sets the text of an item or subitem.
9102 * PARAMETER(S):
9103 * [I] hwnd : window handle
9104 * [I] nItem : item index
9105 * [I] lpLVItem : item or subitem info
9106 * [I] isW : TRUE if input is Unicode
9108 * RETURN:
9109 * SUCCESS : TRUE
9110 * FAILURE : FALSE
9112 static BOOL LISTVIEW_SetItemTextT(LISTVIEW_INFO *infoPtr, INT nItem, const LVITEMW *lpLVItem, BOOL isW)
9114 LVITEMW lvItem;
9116 if (!lpLVItem || nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
9117 if (infoPtr->dwStyle & LVS_OWNERDATA) return FALSE;
9119 lvItem.iItem = nItem;
9120 lvItem.iSubItem = lpLVItem->iSubItem;
9121 lvItem.mask = LVIF_TEXT;
9122 lvItem.pszText = lpLVItem->pszText;
9123 lvItem.cchTextMax = lpLVItem->cchTextMax;
9125 TRACE("(nItem=%d, lpLVItem=%s, isW=%d)\n", nItem, debuglvitem_t(&lvItem, isW), isW);
9127 return LISTVIEW_SetItemT(infoPtr, &lvItem, isW);
9130 /***
9131 * DESCRIPTION:
9132 * Set item index that marks the start of a multiple selection.
9134 * PARAMETER(S):
9135 * [I] infoPtr : valid pointer to the listview structure
9136 * [I] nIndex : index
9138 * RETURN:
9139 * Index number or -1 if there is no selection mark.
9141 static INT LISTVIEW_SetSelectionMark(LISTVIEW_INFO *infoPtr, INT nIndex)
9143 INT nOldIndex = infoPtr->nSelectionMark;
9145 TRACE("(nIndex=%d)\n", nIndex);
9147 if (nIndex >= -1 && nIndex < infoPtr->nItemCount)
9148 infoPtr->nSelectionMark = nIndex;
9150 return nOldIndex;
9153 /***
9154 * DESCRIPTION:
9155 * Sets the text background color.
9157 * PARAMETER(S):
9158 * [I] infoPtr : valid pointer to the listview structure
9159 * [I] color : text background color
9161 * RETURN:
9162 * SUCCESS : TRUE
9163 * FAILURE : FALSE
9165 static BOOL LISTVIEW_SetTextBkColor(LISTVIEW_INFO *infoPtr, COLORREF color)
9167 TRACE("color %#lx\n", color);
9169 infoPtr->clrTextBk = color;
9170 return TRUE;
9173 /***
9174 * DESCRIPTION:
9175 * Sets the text foreground color.
9177 * PARAMETER(S):
9178 * [I] infoPtr : valid pointer to the listview structure
9179 * [I] color : text color
9181 * RETURN:
9182 * SUCCESS : TRUE
9183 * FAILURE : FALSE
9185 static BOOL LISTVIEW_SetTextColor (LISTVIEW_INFO *infoPtr, COLORREF color)
9187 TRACE("color %#lx\n", color);
9189 infoPtr->clrText = color;
9190 return TRUE;
9193 /***
9194 * DESCRIPTION:
9195 * Sets new ToolTip window to ListView control.
9197 * PARAMETER(S):
9198 * [I] infoPtr : valid pointer to the listview structure
9199 * [I] hwndNewToolTip : handle to new ToolTip
9201 * RETURN:
9202 * old tool tip
9204 static HWND LISTVIEW_SetToolTips( LISTVIEW_INFO *infoPtr, HWND hwndNewToolTip)
9206 HWND hwndOldToolTip = infoPtr->hwndToolTip;
9207 infoPtr->hwndToolTip = hwndNewToolTip;
9208 return hwndOldToolTip;
9212 * DESCRIPTION:
9213 * sets the Unicode character format flag for the control
9214 * PARAMETER(S):
9215 * [I] infoPtr :valid pointer to the listview structure
9216 * [I] fUnicode :true to switch to UNICODE false to switch to ANSI
9218 * RETURN:
9219 * Old Unicode Format
9221 static BOOL LISTVIEW_SetUnicodeFormat( LISTVIEW_INFO *infoPtr, BOOL unicode)
9223 SHORT rc = infoPtr->notifyFormat;
9224 infoPtr->notifyFormat = (unicode) ? NFR_UNICODE : NFR_ANSI;
9225 return rc == NFR_UNICODE;
9229 * DESCRIPTION:
9230 * sets the control view mode
9231 * PARAMETER(S):
9232 * [I] infoPtr :valid pointer to the listview structure
9233 * [I] nView :new view mode value
9235 * RETURN:
9236 * SUCCESS: 1
9237 * FAILURE: -1
9239 static INT LISTVIEW_SetView(LISTVIEW_INFO *infoPtr, DWORD nView)
9241 HIMAGELIST himl;
9243 if (infoPtr->uView == nView) return 1;
9245 if ((INT)nView < 0 || nView > LV_VIEW_MAX) return -1;
9246 if (nView == LV_VIEW_TILE)
9248 FIXME("View LV_VIEW_TILE unimplemented\n");
9249 return -1;
9252 infoPtr->uView = nView;
9254 SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
9255 ShowWindow(infoPtr->hwndHeader, SW_HIDE);
9257 ShowScrollBar(infoPtr->hwndSelf, SB_BOTH, FALSE);
9258 SetRectEmpty(&infoPtr->rcFocus);
9260 himl = (nView == LV_VIEW_ICON ? infoPtr->himlNormal : infoPtr->himlSmall);
9261 set_icon_size(&infoPtr->iconSize, himl, nView != LV_VIEW_ICON);
9263 switch (nView)
9265 case LV_VIEW_ICON:
9266 case LV_VIEW_SMALLICON:
9267 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
9268 break;
9269 case LV_VIEW_DETAILS:
9271 HDLAYOUT hl;
9272 WINDOWPOS wp;
9274 LISTVIEW_CreateHeader( infoPtr );
9276 hl.prc = &infoPtr->rcList;
9277 hl.pwpos = &wp;
9278 SendMessageW(infoPtr->hwndHeader, HDM_LAYOUT, 0, (LPARAM)&hl);
9279 SetWindowPos(infoPtr->hwndHeader, infoPtr->hwndSelf, wp.x, wp.y, wp.cx, wp.cy,
9280 wp.flags | ((infoPtr->dwStyle & LVS_NOCOLUMNHEADER) ? SWP_HIDEWINDOW : SWP_SHOWWINDOW));
9281 break;
9283 case LV_VIEW_LIST:
9284 break;
9287 LISTVIEW_UpdateItemSize(infoPtr);
9288 LISTVIEW_UpdateSize(infoPtr);
9289 LISTVIEW_UpdateScroll(infoPtr);
9290 LISTVIEW_InvalidateList(infoPtr);
9292 TRACE("nView %ld\n", nView);
9294 return 1;
9297 /* LISTVIEW_SetWorkAreas */
9299 struct sorting_context
9301 HDPA items;
9302 PFNLVCOMPARE compare_func;
9303 LPARAM lParam;
9306 /* DPA_Sort() callback used for LVM_SORTITEMS */
9307 static INT WINAPI LISTVIEW_CallBackCompare(LPVOID first, LPVOID second, LPARAM lParam)
9309 struct sorting_context *context = (struct sorting_context *)lParam;
9310 ITEM_INFO* lv_first = DPA_GetPtr( first, 0 );
9311 ITEM_INFO* lv_second = DPA_GetPtr( second, 0 );
9313 return context->compare_func(lv_first->lParam, lv_second->lParam, context->lParam);
9316 /* DPA_Sort() callback used for LVM_SORTITEMSEX */
9317 static INT WINAPI LISTVIEW_CallBackCompareEx(LPVOID first, LPVOID second, LPARAM lParam)
9319 struct sorting_context *context = (struct sorting_context *)lParam;
9320 INT first_idx = DPA_GetPtrIndex( context->items, first );
9321 INT second_idx = DPA_GetPtrIndex( context->items, second );
9323 return context->compare_func(first_idx, second_idx, context->lParam);
9326 /***
9327 * DESCRIPTION:
9328 * Sorts the listview items.
9330 * PARAMETER(S):
9331 * [I] infoPtr : valid pointer to the listview structure
9332 * [I] pfnCompare : application-defined value
9333 * [I] lParamSort : pointer to comparison callback
9334 * [I] IsEx : TRUE when LVM_SORTITEMSEX used
9336 * RETURN:
9337 * SUCCESS : TRUE
9338 * FAILURE : FALSE
9340 static BOOL LISTVIEW_SortItems(LISTVIEW_INFO *infoPtr, PFNLVCOMPARE pfnCompare,
9341 LPARAM lParamSort, BOOL IsEx)
9343 HDPA hdpaSubItems, hdpaItems;
9344 ITEM_INFO *lpItem;
9345 LPVOID selectionMarkItem = NULL;
9346 LPVOID focusedItem = NULL;
9347 struct sorting_context context;
9348 int i;
9350 TRACE("pfnCompare %p, lParamSort %Ix\n", pfnCompare, lParamSort);
9352 if (infoPtr->dwStyle & LVS_OWNERDATA) return FALSE;
9354 if (!pfnCompare) return FALSE;
9355 if (!infoPtr->hdpaItems) return FALSE;
9357 /* if there are 0 or 1 items, there is no need to sort */
9358 if (infoPtr->nItemCount < 2) return TRUE;
9359 if (!(hdpaItems = DPA_Clone(infoPtr->hdpaItems, NULL))) return FALSE;
9361 /* clear selection */
9362 ranges_clear(infoPtr->selectionRanges);
9364 /* save selection mark and focused item */
9365 if (infoPtr->nSelectionMark >= 0)
9366 selectionMarkItem = DPA_GetPtr(infoPtr->hdpaItems, infoPtr->nSelectionMark);
9367 if (infoPtr->nFocusedItem >= 0)
9368 focusedItem = DPA_GetPtr(infoPtr->hdpaItems, infoPtr->nFocusedItem);
9370 context.items = hdpaItems;
9371 context.compare_func = pfnCompare;
9372 context.lParam = lParamSort;
9373 if (IsEx)
9374 DPA_Sort(hdpaItems, LISTVIEW_CallBackCompareEx, (LPARAM)&context);
9375 else
9376 DPA_Sort(hdpaItems, LISTVIEW_CallBackCompare, (LPARAM)&context);
9377 DPA_Destroy(infoPtr->hdpaItems);
9378 infoPtr->hdpaItems = hdpaItems;
9380 /* restore selection ranges */
9381 for (i=0; i < infoPtr->nItemCount; i++)
9383 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, i);
9384 lpItem = DPA_GetPtr(hdpaSubItems, 0);
9386 if (lpItem->state & LVIS_SELECTED)
9387 ranges_additem(infoPtr->selectionRanges, i);
9389 /* restore selection mark and focused item */
9390 infoPtr->nSelectionMark = DPA_GetPtrIndex(infoPtr->hdpaItems, selectionMarkItem);
9391 infoPtr->nFocusedItem = DPA_GetPtrIndex(infoPtr->hdpaItems, focusedItem);
9393 /* I believe nHotItem should be left alone, see LISTVIEW_ShiftIndices */
9395 /* refresh the display */
9396 LISTVIEW_InvalidateList(infoPtr);
9397 return TRUE;
9400 /***
9401 * DESCRIPTION:
9402 * Update theme handle after a theme change.
9404 * PARAMETER(S):
9405 * [I] infoPtr : valid pointer to the listview structure
9407 * RETURN:
9408 * SUCCESS : 0
9409 * FAILURE : something else
9411 static LRESULT LISTVIEW_ThemeChanged(const LISTVIEW_INFO *infoPtr)
9413 HTHEME theme = GetWindowTheme(infoPtr->hwndSelf);
9414 CloseThemeData(theme);
9415 OpenThemeData(infoPtr->hwndSelf, themeClass);
9416 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
9417 return 0;
9420 /***
9421 * DESCRIPTION:
9422 * Updates an items or rearranges the listview control.
9424 * PARAMETER(S):
9425 * [I] infoPtr : valid pointer to the listview structure
9426 * [I] nItem : item index
9428 * RETURN:
9429 * SUCCESS : TRUE
9430 * FAILURE : FALSE
9432 static BOOL LISTVIEW_Update(LISTVIEW_INFO *infoPtr, INT nItem)
9434 TRACE("(nItem=%d)\n", nItem);
9436 if (nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
9438 /* rearrange with default alignment style */
9439 if (is_autoarrange(infoPtr))
9440 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
9441 else
9442 LISTVIEW_InvalidateItem(infoPtr, nItem);
9444 return TRUE;
9447 /***
9448 * DESCRIPTION:
9449 * Draw the track line at the place defined in the infoPtr structure.
9450 * The line is drawn with a XOR pen so drawing the line for the second time
9451 * in the same place erases the line.
9453 * PARAMETER(S):
9454 * [I] infoPtr : valid pointer to the listview structure
9456 * RETURN:
9457 * SUCCESS : TRUE
9458 * FAILURE : FALSE
9460 static BOOL LISTVIEW_DrawTrackLine(const LISTVIEW_INFO *infoPtr)
9462 HDC hdc;
9464 if (infoPtr->xTrackLine == -1)
9465 return FALSE;
9467 if (!(hdc = GetDC(infoPtr->hwndSelf)))
9468 return FALSE;
9469 PatBlt( hdc, infoPtr->xTrackLine, infoPtr->rcList.top,
9470 1, infoPtr->rcList.bottom - infoPtr->rcList.top, DSTINVERT );
9471 ReleaseDC(infoPtr->hwndSelf, hdc);
9472 return TRUE;
9475 /***
9476 * DESCRIPTION:
9477 * Called when an edit control should be displayed. This function is called after
9478 * we are sure that there was a single click - not a double click (this is a TIMERPROC).
9480 * PARAMETER(S):
9481 * [I] hwnd : Handle to the listview
9482 * [I] uMsg : WM_TIMER (ignored)
9483 * [I] idEvent : The timer ID interpreted as a pointer to a DELAYED_EDIT_ITEM struct
9484 * [I] dwTimer : The elapsed time (ignored)
9486 * RETURN:
9487 * None.
9489 static VOID CALLBACK LISTVIEW_DelayedEditItem(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
9491 DELAYED_ITEM_EDIT *editItem = (DELAYED_ITEM_EDIT *)idEvent;
9492 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongPtrW(hwnd, 0);
9494 KillTimer(hwnd, idEvent);
9495 editItem->fEnabled = FALSE;
9496 /* check if the item is still selected */
9497 if (infoPtr->bFocus && LISTVIEW_GetItemState(infoPtr, editItem->iItem, LVIS_SELECTED))
9498 LISTVIEW_EditLabelT(infoPtr, editItem->iItem, TRUE);
9501 /***
9502 * DESCRIPTION:
9503 * Creates the listview control - the WM_NCCREATE phase.
9505 * PARAMETER(S):
9506 * [I] hwnd : window handle
9507 * [I] lpcs : the create parameters
9509 * RETURN:
9510 * Success: TRUE
9511 * Failure: FALSE
9513 static LRESULT LISTVIEW_NCCreate(HWND hwnd, WPARAM wParam, const CREATESTRUCTW *lpcs)
9515 LISTVIEW_INFO *infoPtr;
9516 LOGFONTW logFont;
9518 TRACE("(lpcs=%p)\n", lpcs);
9520 /* initialize info pointer */
9521 infoPtr = Alloc(sizeof(LISTVIEW_INFO));
9522 if (!infoPtr) return FALSE;
9524 SetWindowLongPtrW(hwnd, 0, (DWORD_PTR)infoPtr);
9526 infoPtr->hwndSelf = hwnd;
9527 infoPtr->dwStyle = lpcs->style; /* Note: may be changed in WM_CREATE */
9528 map_style_view(infoPtr);
9529 /* determine the type of structures to use */
9530 infoPtr->hwndNotify = lpcs->hwndParent;
9531 /* infoPtr->notifyFormat will be filled in WM_CREATE */
9533 /* initialize color information */
9534 infoPtr->clrBk = CLR_NONE;
9535 infoPtr->clrText = CLR_DEFAULT;
9536 infoPtr->clrTextBk = CLR_DEFAULT;
9537 LISTVIEW_SetBkColor(infoPtr, comctl32_color.clrWindow);
9539 /* set default values */
9540 infoPtr->nFocusedItem = -1;
9541 infoPtr->nSelectionMark = -1;
9542 infoPtr->nHotItem = -1;
9543 infoPtr->redraw = TRUE;
9544 infoPtr->bNoItemMetrics = TRUE;
9545 infoPtr->notify_mask = NOTIFY_MASK_UNMASK_ALL;
9546 infoPtr->autoSpacing = TRUE;
9547 infoPtr->iconSpacing.cx = GetSystemMetrics(SM_CXICONSPACING) - GetSystemMetrics(SM_CXICON);
9548 infoPtr->iconSpacing.cy = GetSystemMetrics(SM_CYICONSPACING) - GetSystemMetrics(SM_CYICON);
9549 infoPtr->nEditLabelItem = -1;
9550 infoPtr->nLButtonDownItem = -1;
9551 infoPtr->dwHoverTime = HOVER_DEFAULT; /* default system hover time */
9552 infoPtr->cWheelRemainder = 0;
9553 infoPtr->nMeasureItemHeight = 0;
9554 infoPtr->xTrackLine = -1; /* no track line */
9555 infoPtr->itemEdit.fEnabled = FALSE;
9556 infoPtr->iVersion = COMCTL32_VERSION;
9557 infoPtr->colRectsDirty = FALSE;
9558 infoPtr->selected_column = -1;
9560 /* get default font (icon title) */
9561 SystemParametersInfoW(SPI_GETICONTITLELOGFONT, 0, &logFont, 0);
9562 infoPtr->hDefaultFont = CreateFontIndirectW(&logFont);
9563 infoPtr->hFont = infoPtr->hDefaultFont;
9564 LISTVIEW_SaveTextMetrics(infoPtr);
9566 /* allocate memory for the data structure */
9567 if (!(infoPtr->selectionRanges = ranges_create(10))) goto fail;
9568 if (!(infoPtr->hdpaItems = DPA_Create(10))) goto fail;
9569 if (!(infoPtr->hdpaItemIds = DPA_Create(10))) goto fail;
9570 if (!(infoPtr->hdpaPosX = DPA_Create(10))) goto fail;
9571 if (!(infoPtr->hdpaPosY = DPA_Create(10))) goto fail;
9572 if (!(infoPtr->hdpaColumns = DPA_Create(10))) goto fail;
9574 return DefWindowProcW(hwnd, WM_NCCREATE, wParam, (LPARAM)lpcs);
9576 fail:
9577 DestroyWindow(infoPtr->hwndHeader);
9578 ranges_destroy(infoPtr->selectionRanges);
9579 DPA_Destroy(infoPtr->hdpaItems);
9580 DPA_Destroy(infoPtr->hdpaItemIds);
9581 DPA_Destroy(infoPtr->hdpaPosX);
9582 DPA_Destroy(infoPtr->hdpaPosY);
9583 DPA_Destroy(infoPtr->hdpaColumns);
9584 Free(infoPtr);
9585 return FALSE;
9588 /***
9589 * DESCRIPTION:
9590 * Creates the listview control - the WM_CREATE phase. Most of the data is
9591 * already set up in LISTVIEW_NCCreate
9593 * PARAMETER(S):
9594 * [I] hwnd : window handle
9595 * [I] lpcs : the create parameters
9597 * RETURN:
9598 * Success: 0
9599 * Failure: -1
9601 static LRESULT LISTVIEW_Create(HWND hwnd, const CREATESTRUCTW *lpcs)
9603 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongPtrW(hwnd, 0);
9605 TRACE("lpcs %p, style %#lx\n", lpcs, lpcs->style);
9607 infoPtr->dwStyle = lpcs->style;
9608 map_style_view(infoPtr);
9610 infoPtr->notifyFormat = SendMessageW(infoPtr->hwndNotify, WM_NOTIFYFORMAT,
9611 (WPARAM)infoPtr->hwndSelf, NF_QUERY);
9612 /* on error defaulting to ANSI notifications */
9613 if (infoPtr->notifyFormat == 0) infoPtr->notifyFormat = NFR_ANSI;
9614 TRACE("notify format=%d\n", infoPtr->notifyFormat);
9616 if ((infoPtr->uView == LV_VIEW_DETAILS) && (lpcs->style & WS_VISIBLE))
9618 if (LISTVIEW_CreateHeader(infoPtr) < 0) return -1;
9620 else
9621 infoPtr->hwndHeader = 0;
9623 /* init item size to avoid division by 0 */
9624 LISTVIEW_UpdateItemSize (infoPtr);
9625 LISTVIEW_UpdateSize (infoPtr);
9627 if (infoPtr->uView == LV_VIEW_DETAILS)
9629 if (!(LVS_NOCOLUMNHEADER & lpcs->style) && (WS_VISIBLE & lpcs->style))
9631 ShowWindow(infoPtr->hwndHeader, SW_SHOWNORMAL);
9633 LISTVIEW_UpdateScroll(infoPtr);
9634 /* send WM_MEASUREITEM notification */
9635 if (infoPtr->dwStyle & LVS_OWNERDRAWFIXED) notify_measureitem(infoPtr);
9638 OpenThemeData(hwnd, themeClass);
9640 /* initialize the icon sizes */
9641 set_icon_size(&infoPtr->iconSize, infoPtr->himlNormal, infoPtr->uView != LV_VIEW_ICON);
9642 set_icon_size(&infoPtr->iconStateSize, infoPtr->himlState, TRUE);
9643 return 0;
9646 /***
9647 * DESCRIPTION:
9648 * Destroys the listview control.
9650 * PARAMETER(S):
9651 * [I] infoPtr : valid pointer to the listview structure
9653 * RETURN:
9654 * Success: 0
9655 * Failure: -1
9657 static LRESULT LISTVIEW_Destroy(LISTVIEW_INFO *infoPtr)
9659 HTHEME theme = GetWindowTheme(infoPtr->hwndSelf);
9660 CloseThemeData(theme);
9662 /* delete all items */
9663 LISTVIEW_DeleteAllItems(infoPtr, TRUE);
9665 return 0;
9668 /***
9669 * DESCRIPTION:
9670 * Enables the listview control.
9672 * PARAMETER(S):
9673 * [I] infoPtr : valid pointer to the listview structure
9674 * [I] bEnable : specifies whether to enable or disable the window
9676 * RETURN:
9677 * SUCCESS : TRUE
9678 * FAILURE : FALSE
9680 static BOOL LISTVIEW_Enable(const LISTVIEW_INFO *infoPtr)
9682 if (infoPtr->dwStyle & LVS_OWNERDRAWFIXED)
9683 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
9684 return TRUE;
9687 /***
9688 * DESCRIPTION:
9689 * Erases the background of the listview control.
9691 * PARAMETER(S):
9692 * [I] infoPtr : valid pointer to the listview structure
9693 * [I] hdc : device context handle
9695 * RETURN:
9696 * SUCCESS : TRUE
9697 * FAILURE : FALSE
9699 static inline BOOL LISTVIEW_EraseBkgnd(const LISTVIEW_INFO *infoPtr, HDC hdc)
9701 RECT rc;
9703 TRACE("(hdc=%p)\n", hdc);
9705 if (!GetClipBox(hdc, &rc)) return FALSE;
9707 if (infoPtr->clrBk == CLR_NONE)
9709 if (infoPtr->dwLvExStyle & LVS_EX_TRANSPARENTBKGND)
9710 SendMessageW(infoPtr->hwndNotify, WM_PRINTCLIENT, (WPARAM)hdc, PRF_ERASEBKGND);
9711 else
9712 SendMessageW(infoPtr->hwndNotify, WM_ERASEBKGND, (WPARAM)hdc, 0);
9713 LISTVIEW_DrawBackgroundBitmap(infoPtr, hdc, &rc);
9714 return TRUE;
9717 /* for double buffered controls we need to do this during refresh */
9718 if (infoPtr->dwLvExStyle & LVS_EX_DOUBLEBUFFER) return FALSE;
9720 return LISTVIEW_FillBkgnd(infoPtr, hdc, &rc);
9724 /***
9725 * DESCRIPTION:
9726 * Helper function for LISTVIEW_[HV]Scroll *only*.
9727 * Performs vertical/horizontal scrolling by a give amount.
9729 * PARAMETER(S):
9730 * [I] infoPtr : valid pointer to the listview structure
9731 * [I] dx : amount of horizontal scroll
9732 * [I] dy : amount of vertical scroll
9734 static void scroll_list(LISTVIEW_INFO *infoPtr, INT dx, INT dy)
9736 /* now we can scroll the list */
9737 ScrollWindowEx(infoPtr->hwndSelf, dx, dy, &infoPtr->rcList,
9738 &infoPtr->rcList, 0, 0, SW_ERASE | SW_INVALIDATE);
9739 /* if we have focus, adjust rect */
9740 OffsetRect(&infoPtr->rcFocus, dx, dy);
9741 UpdateWindow(infoPtr->hwndSelf);
9744 /***
9745 * DESCRIPTION:
9746 * Performs vertical scrolling.
9748 * PARAMETER(S):
9749 * [I] infoPtr : valid pointer to the listview structure
9750 * [I] nScrollCode : scroll code
9751 * [I] nScrollDiff : units to scroll in SB_INTERNAL mode, 0 otherwise
9752 * [I] hScrollWnd : scrollbar control window handle
9754 * RETURN:
9755 * Zero
9757 * NOTES:
9758 * SB_LINEUP/SB_LINEDOWN:
9759 * for LVS_ICON, LVS_SMALLICON is 37 by experiment
9760 * for LVS_REPORT is 1 line
9761 * for LVS_LIST cannot occur
9764 static LRESULT LISTVIEW_VScroll(LISTVIEW_INFO *infoPtr, INT nScrollCode,
9765 INT nScrollDiff)
9767 INT nOldScrollPos, nNewScrollPos;
9768 SCROLLINFO scrollInfo;
9769 BOOL is_an_icon;
9771 TRACE("(nScrollCode=%d(%s), nScrollDiff=%d)\n", nScrollCode,
9772 debugscrollcode(nScrollCode), nScrollDiff);
9774 if (infoPtr->hwndEdit) SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
9776 scrollInfo.cbSize = sizeof(SCROLLINFO);
9777 scrollInfo.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_TRACKPOS;
9779 is_an_icon = ((infoPtr->uView == LV_VIEW_ICON) || (infoPtr->uView == LV_VIEW_SMALLICON));
9781 if (!GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo)) return 1;
9783 nOldScrollPos = scrollInfo.nPos;
9784 switch (nScrollCode)
9786 case SB_INTERNAL:
9787 break;
9789 case SB_LINEUP:
9790 nScrollDiff = (is_an_icon) ? -LISTVIEW_SCROLL_ICON_LINE_SIZE : -1;
9791 break;
9793 case SB_LINEDOWN:
9794 nScrollDiff = (is_an_icon) ? LISTVIEW_SCROLL_ICON_LINE_SIZE : 1;
9795 break;
9797 case SB_PAGEUP:
9798 nScrollDiff = -scrollInfo.nPage;
9799 break;
9801 case SB_PAGEDOWN:
9802 nScrollDiff = scrollInfo.nPage;
9803 break;
9805 case SB_THUMBPOSITION:
9806 case SB_THUMBTRACK:
9807 nScrollDiff = scrollInfo.nTrackPos - scrollInfo.nPos;
9808 break;
9810 default:
9811 nScrollDiff = 0;
9814 /* quit right away if pos isn't changing */
9815 if (nScrollDiff == 0) return 0;
9817 /* calculate new position, and handle overflows */
9818 nNewScrollPos = scrollInfo.nPos + nScrollDiff;
9819 if (nScrollDiff > 0) {
9820 if (nNewScrollPos < nOldScrollPos ||
9821 nNewScrollPos > scrollInfo.nMax)
9822 nNewScrollPos = scrollInfo.nMax;
9823 } else {
9824 if (nNewScrollPos > nOldScrollPos ||
9825 nNewScrollPos < scrollInfo.nMin)
9826 nNewScrollPos = scrollInfo.nMin;
9829 /* set the new position, and reread in case it changed */
9830 scrollInfo.fMask = SIF_POS;
9831 scrollInfo.nPos = nNewScrollPos;
9832 nNewScrollPos = SetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo, TRUE);
9834 /* carry on only if it really changed */
9835 if (nNewScrollPos == nOldScrollPos) return 0;
9837 /* now adjust to client coordinates */
9838 nScrollDiff = nOldScrollPos - nNewScrollPos;
9839 if (infoPtr->uView == LV_VIEW_DETAILS) nScrollDiff *= infoPtr->nItemHeight;
9841 /* and scroll the window */
9842 scroll_list(infoPtr, 0, nScrollDiff);
9844 return 0;
9847 /***
9848 * DESCRIPTION:
9849 * Performs horizontal scrolling.
9851 * PARAMETER(S):
9852 * [I] infoPtr : valid pointer to the listview structure
9853 * [I] nScrollCode : scroll code
9854 * [I] nScrollDiff : units to scroll in SB_INTERNAL mode, 0 otherwise
9855 * [I] hScrollWnd : scrollbar control window handle
9857 * RETURN:
9858 * Zero
9860 * NOTES:
9861 * SB_LINELEFT/SB_LINERIGHT:
9862 * for LVS_ICON, LVS_SMALLICON 1 pixel
9863 * for LVS_REPORT is 1 pixel
9864 * for LVS_LIST is 1 column --> which is a 1 because the
9865 * scroll is based on columns not pixels
9868 static LRESULT LISTVIEW_HScroll(LISTVIEW_INFO *infoPtr, INT nScrollCode,
9869 INT nScrollDiff)
9871 INT nOldScrollPos, nNewScrollPos;
9872 SCROLLINFO scrollInfo;
9873 BOOL is_an_icon;
9875 TRACE("(nScrollCode=%d(%s), nScrollDiff=%d)\n", nScrollCode,
9876 debugscrollcode(nScrollCode), nScrollDiff);
9878 if (infoPtr->hwndEdit) SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
9880 scrollInfo.cbSize = sizeof(SCROLLINFO);
9881 scrollInfo.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_TRACKPOS;
9883 is_an_icon = ((infoPtr->uView == LV_VIEW_ICON) || (infoPtr->uView == LV_VIEW_SMALLICON));
9885 if (!GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo)) return 1;
9887 nOldScrollPos = scrollInfo.nPos;
9889 switch (nScrollCode)
9891 case SB_INTERNAL:
9892 break;
9894 case SB_LINELEFT:
9895 nScrollDiff = (is_an_icon) ? -LISTVIEW_SCROLL_ICON_LINE_SIZE : -1;
9896 break;
9898 case SB_LINERIGHT:
9899 nScrollDiff = (is_an_icon) ? LISTVIEW_SCROLL_ICON_LINE_SIZE : 1;
9900 break;
9902 case SB_PAGELEFT:
9903 nScrollDiff = -scrollInfo.nPage;
9904 break;
9906 case SB_PAGERIGHT:
9907 nScrollDiff = scrollInfo.nPage;
9908 break;
9910 case SB_THUMBPOSITION:
9911 case SB_THUMBTRACK:
9912 nScrollDiff = scrollInfo.nTrackPos - scrollInfo.nPos;
9913 break;
9915 default:
9916 nScrollDiff = 0;
9919 /* quit right away if pos isn't changing */
9920 if (nScrollDiff == 0) return 0;
9922 /* calculate new position, and handle overflows */
9923 nNewScrollPos = scrollInfo.nPos + nScrollDiff;
9924 if (nScrollDiff > 0) {
9925 if (nNewScrollPos < nOldScrollPos ||
9926 nNewScrollPos > scrollInfo.nMax)
9927 nNewScrollPos = scrollInfo.nMax;
9928 } else {
9929 if (nNewScrollPos > nOldScrollPos ||
9930 nNewScrollPos < scrollInfo.nMin)
9931 nNewScrollPos = scrollInfo.nMin;
9934 /* set the new position, and reread in case it changed */
9935 scrollInfo.fMask = SIF_POS;
9936 scrollInfo.nPos = nNewScrollPos;
9937 nNewScrollPos = SetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo, TRUE);
9939 /* carry on only if it really changed */
9940 if (nNewScrollPos == nOldScrollPos) return 0;
9942 LISTVIEW_UpdateHeaderSize(infoPtr, nNewScrollPos);
9944 /* now adjust to client coordinates */
9945 nScrollDiff = nOldScrollPos - nNewScrollPos;
9946 if (infoPtr->uView == LV_VIEW_LIST) nScrollDiff *= infoPtr->nItemWidth;
9948 /* and scroll the window */
9949 scroll_list(infoPtr, nScrollDiff, 0);
9951 return 0;
9954 static LRESULT LISTVIEW_MouseWheel(LISTVIEW_INFO *infoPtr, INT wheelDelta)
9956 INT pulScrollLines = 3;
9958 TRACE("(wheelDelta=%d)\n", wheelDelta);
9960 switch(infoPtr->uView)
9962 case LV_VIEW_ICON:
9963 case LV_VIEW_SMALLICON:
9965 * listview should be scrolled by a multiple of 37 dependently on its dimension or its visible item number
9966 * should be fixed in the future.
9968 LISTVIEW_VScroll(infoPtr, SB_INTERNAL, (wheelDelta > 0) ?
9969 -LISTVIEW_SCROLL_ICON_LINE_SIZE : LISTVIEW_SCROLL_ICON_LINE_SIZE);
9970 break;
9972 case LV_VIEW_DETAILS:
9973 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
9975 /* if scrolling changes direction, ignore left overs */
9976 if ((wheelDelta < 0 && infoPtr->cWheelRemainder < 0) ||
9977 (wheelDelta > 0 && infoPtr->cWheelRemainder > 0))
9978 infoPtr->cWheelRemainder += wheelDelta;
9979 else
9980 infoPtr->cWheelRemainder = wheelDelta;
9981 if (infoPtr->cWheelRemainder && pulScrollLines)
9983 int cLineScroll;
9984 pulScrollLines = min((UINT)LISTVIEW_GetCountPerColumn(infoPtr), pulScrollLines);
9985 cLineScroll = pulScrollLines * infoPtr->cWheelRemainder / WHEEL_DELTA;
9986 infoPtr->cWheelRemainder -= WHEEL_DELTA * cLineScroll / pulScrollLines;
9987 LISTVIEW_VScroll(infoPtr, SB_INTERNAL, -cLineScroll);
9989 break;
9991 case LV_VIEW_LIST:
9992 LISTVIEW_HScroll(infoPtr, (wheelDelta > 0) ? SB_LINELEFT : SB_LINERIGHT, 0);
9993 break;
9995 return 0;
9998 /***
9999 * DESCRIPTION:
10000 * ???
10002 * PARAMETER(S):
10003 * [I] infoPtr : valid pointer to the listview structure
10004 * [I] nVirtualKey : virtual key
10005 * [I] lKeyData : key data
10007 * RETURN:
10008 * Zero
10010 static LRESULT LISTVIEW_KeyDown(LISTVIEW_INFO *infoPtr, INT nVirtualKey, LONG lKeyData)
10012 HWND hwndSelf = infoPtr->hwndSelf;
10013 INT nItem = -1;
10014 NMLVKEYDOWN nmKeyDown;
10016 TRACE("(nVirtualKey=%d, lKeyData=%ld)\n", nVirtualKey, lKeyData);
10018 /* send LVN_KEYDOWN notification */
10019 nmKeyDown.wVKey = nVirtualKey;
10020 nmKeyDown.flags = 0;
10021 notify_hdr(infoPtr, LVN_KEYDOWN, &nmKeyDown.hdr);
10022 if (!IsWindow(hwndSelf))
10023 return 0;
10025 switch (nVirtualKey)
10027 case VK_SPACE:
10028 nItem = infoPtr->nFocusedItem;
10029 if (infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES)
10030 toggle_checkbox_state(infoPtr, infoPtr->nFocusedItem);
10031 break;
10033 case VK_RETURN:
10034 if ((infoPtr->nItemCount > 0) && (infoPtr->nFocusedItem != -1))
10036 if (!notify(infoPtr, NM_RETURN)) return 0;
10037 if (!notify(infoPtr, LVN_ITEMACTIVATE)) return 0;
10039 break;
10041 case VK_HOME:
10042 if (infoPtr->nItemCount > 0)
10043 nItem = 0;
10044 break;
10046 case VK_END:
10047 if (infoPtr->nItemCount > 0)
10048 nItem = infoPtr->nItemCount - 1;
10049 break;
10051 case VK_LEFT:
10052 nItem = LISTVIEW_GetNextItem(infoPtr, infoPtr->nFocusedItem, LVNI_TOLEFT);
10053 break;
10055 case VK_UP:
10056 nItem = LISTVIEW_GetNextItem(infoPtr, infoPtr->nFocusedItem, LVNI_ABOVE);
10057 break;
10059 case VK_RIGHT:
10060 nItem = LISTVIEW_GetNextItem(infoPtr, infoPtr->nFocusedItem, LVNI_TORIGHT);
10061 break;
10063 case VK_DOWN:
10064 nItem = LISTVIEW_GetNextItem(infoPtr, infoPtr->nFocusedItem, LVNI_BELOW);
10065 break;
10067 case VK_PRIOR:
10068 if (infoPtr->uView == LV_VIEW_DETAILS)
10070 INT topidx = LISTVIEW_GetTopIndex(infoPtr);
10071 if (infoPtr->nFocusedItem == topidx)
10072 nItem = topidx - LISTVIEW_GetCountPerColumn(infoPtr) + 1;
10073 else
10074 nItem = topidx;
10076 else
10077 nItem = infoPtr->nFocusedItem - LISTVIEW_GetCountPerColumn(infoPtr)
10078 * LISTVIEW_GetCountPerRow(infoPtr);
10079 if(nItem < 0) nItem = 0;
10080 break;
10082 case VK_NEXT:
10083 if (infoPtr->uView == LV_VIEW_DETAILS)
10085 INT topidx = LISTVIEW_GetTopIndex(infoPtr);
10086 INT cnt = LISTVIEW_GetCountPerColumn(infoPtr);
10087 if (infoPtr->nFocusedItem == topidx + cnt - 1)
10088 nItem = infoPtr->nFocusedItem + cnt - 1;
10089 else
10090 nItem = topidx + cnt - 1;
10092 else
10093 nItem = infoPtr->nFocusedItem + LISTVIEW_GetCountPerColumn(infoPtr)
10094 * LISTVIEW_GetCountPerRow(infoPtr);
10095 if(nItem >= infoPtr->nItemCount) nItem = infoPtr->nItemCount - 1;
10096 break;
10099 if ((nItem != -1) && (nItem != infoPtr->nFocusedItem || nVirtualKey == VK_SPACE))
10100 LISTVIEW_KeySelection(infoPtr, nItem, nVirtualKey == VK_SPACE);
10102 return 0;
10105 /***
10106 * DESCRIPTION:
10107 * Kills the focus.
10109 * PARAMETER(S):
10110 * [I] infoPtr : valid pointer to the listview structure
10112 * RETURN:
10113 * Zero
10115 static LRESULT LISTVIEW_KillFocus(LISTVIEW_INFO *infoPtr)
10117 TRACE("()\n");
10119 /* drop any left over scroll amount */
10120 infoPtr->cWheelRemainder = 0;
10122 /* if we did not have the focus, there's nothing more to do */
10123 if (!infoPtr->bFocus) return 0;
10125 /* send NM_KILLFOCUS notification */
10126 if (!notify(infoPtr, NM_KILLFOCUS)) return 0;
10128 /* if we have a focus rectangle, get rid of it */
10129 LISTVIEW_ShowFocusRect(infoPtr, FALSE);
10131 /* if have a marquee selection, stop it */
10132 if (infoPtr->bMarqueeSelect)
10134 /* Remove the marquee rectangle and release our mouse capture */
10135 LISTVIEW_InvalidateRect(infoPtr, &infoPtr->marqueeRect);
10136 ReleaseCapture();
10138 SetRectEmpty(&infoPtr->marqueeRect);
10140 infoPtr->bMarqueeSelect = FALSE;
10141 infoPtr->bScrolling = FALSE;
10142 KillTimer(infoPtr->hwndSelf, (UINT_PTR) infoPtr);
10145 /* set window focus flag */
10146 infoPtr->bFocus = FALSE;
10148 /* invalidate the selected items before resetting focus flag */
10149 LISTVIEW_InvalidateSelectedItems(infoPtr);
10151 return 0;
10154 /***
10155 * DESCRIPTION:
10156 * Processes double click messages (left mouse button).
10158 * PARAMETER(S):
10159 * [I] infoPtr : valid pointer to the listview structure
10160 * [I] wKey : key flag
10161 * [I] x,y : mouse coordinate
10163 * RETURN:
10164 * Zero
10166 static LRESULT LISTVIEW_LButtonDblClk(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
10168 LVHITTESTINFO htInfo;
10170 TRACE("(key=%hu, X=%u, Y=%u)\n", wKey, x, y);
10172 /* Cancel the item edition if any */
10173 if (infoPtr->itemEdit.fEnabled)
10175 KillTimer(infoPtr->hwndSelf, (UINT_PTR)&infoPtr->itemEdit);
10176 infoPtr->itemEdit.fEnabled = FALSE;
10179 /* send NM_RELEASEDCAPTURE notification */
10180 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
10182 htInfo.pt.x = x;
10183 htInfo.pt.y = y;
10185 /* send NM_DBLCLK notification */
10186 LISTVIEW_HitTest(infoPtr, &htInfo, TRUE, FALSE);
10187 if (!notify_click(infoPtr, NM_DBLCLK, &htInfo)) return 0;
10189 /* To send the LVN_ITEMACTIVATE, it must be on an Item */
10190 if(htInfo.iItem != -1) notify_itemactivate(infoPtr,&htInfo);
10192 return 0;
10195 static LRESULT LISTVIEW_TrackMouse(const LISTVIEW_INFO *infoPtr, POINT pt)
10197 MSG msg;
10198 RECT r;
10200 r.top = r.bottom = pt.y;
10201 r.left = r.right = pt.x;
10203 InflateRect(&r, GetSystemMetrics(SM_CXDRAG), GetSystemMetrics(SM_CYDRAG));
10205 SetCapture(infoPtr->hwndSelf);
10207 while (1)
10209 if (PeekMessageW(&msg, 0, 0, 0, PM_REMOVE | PM_NOYIELD))
10211 if (msg.message == WM_MOUSEMOVE)
10213 pt.x = (short)LOWORD(msg.lParam);
10214 pt.y = (short)HIWORD(msg.lParam);
10215 if (PtInRect(&r, pt))
10216 continue;
10217 else
10219 ReleaseCapture();
10220 return 1;
10223 else if (msg.message >= WM_LBUTTONDOWN &&
10224 msg.message <= WM_RBUTTONDBLCLK)
10226 break;
10229 DispatchMessageW(&msg);
10232 if (GetCapture() != infoPtr->hwndSelf)
10233 return 0;
10236 ReleaseCapture();
10237 return 0;
10241 /***
10242 * DESCRIPTION:
10243 * Processes mouse down messages (left mouse button).
10245 * PARAMETERS:
10246 * infoPtr [I ] valid pointer to the listview structure
10247 * wKey [I ] key flag
10248 * x,y [I ] mouse coordinate
10250 * RETURN:
10251 * Zero
10253 static LRESULT LISTVIEW_LButtonDown(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
10255 LVHITTESTINFO lvHitTestInfo;
10256 static BOOL bGroupSelect = TRUE;
10257 POINT pt = { x, y };
10258 INT nItem;
10260 TRACE("(key=%hu, X=%u, Y=%u)\n", wKey, x, y);
10262 /* send NM_RELEASEDCAPTURE notification */
10263 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
10265 /* set left button down flag and record the click position */
10266 infoPtr->bLButtonDown = TRUE;
10267 infoPtr->ptClickPos = pt;
10268 infoPtr->bDragging = FALSE;
10269 infoPtr->bMarqueeSelect = FALSE;
10270 infoPtr->bScrolling = FALSE;
10272 lvHitTestInfo.pt.x = x;
10273 lvHitTestInfo.pt.y = y;
10275 nItem = LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, TRUE);
10276 TRACE("at %s, nItem=%d\n", wine_dbgstr_point(&pt), nItem);
10277 if ((nItem >= 0) && (nItem < infoPtr->nItemCount))
10279 if ((infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES) && (lvHitTestInfo.flags & LVHT_ONITEMSTATEICON))
10281 notify_click(infoPtr, NM_CLICK, &lvHitTestInfo);
10282 toggle_checkbox_state(infoPtr, nItem);
10283 infoPtr->bLButtonDown = FALSE;
10284 return 0;
10287 if (infoPtr->dwStyle & LVS_SINGLESEL)
10289 if (LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED))
10290 infoPtr->nEditLabelItem = nItem;
10291 else
10292 LISTVIEW_SetSelection(infoPtr, nItem);
10294 else
10296 if ((wKey & MK_CONTROL) && (wKey & MK_SHIFT))
10298 if (bGroupSelect)
10300 if (!LISTVIEW_AddGroupSelection(infoPtr, nItem)) return 0;
10301 LISTVIEW_SetItemFocus(infoPtr, nItem);
10302 infoPtr->nSelectionMark = nItem;
10304 else
10306 LVITEMW item;
10308 item.state = LVIS_SELECTED | LVIS_FOCUSED;
10309 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
10311 LISTVIEW_SetItemState(infoPtr,nItem,&item);
10312 infoPtr->nSelectionMark = nItem;
10315 else if (wKey & MK_CONTROL)
10317 LVITEMW item;
10319 bGroupSelect = (LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED) == 0);
10321 item.state = (bGroupSelect ? LVIS_SELECTED : 0) | LVIS_FOCUSED;
10322 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
10323 LISTVIEW_SetItemState(infoPtr, nItem, &item);
10324 infoPtr->nSelectionMark = nItem;
10326 else if (wKey & MK_SHIFT)
10328 LISTVIEW_SetGroupSelection(infoPtr, nItem);
10330 else
10332 if (LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED))
10334 infoPtr->nEditLabelItem = nItem;
10335 infoPtr->nLButtonDownItem = nItem;
10337 LISTVIEW_SetItemFocus(infoPtr, nItem);
10339 else
10340 /* set selection (clears other pre-existing selections) */
10341 LISTVIEW_SetSelection(infoPtr, nItem);
10345 if (!infoPtr->bFocus)
10346 SetFocus(infoPtr->hwndSelf);
10348 if (infoPtr->dwLvExStyle & LVS_EX_ONECLICKACTIVATE)
10349 if(lvHitTestInfo.iItem != -1) notify_itemactivate(infoPtr,&lvHitTestInfo);
10351 else
10353 if (!infoPtr->bFocus)
10354 SetFocus(infoPtr->hwndSelf);
10356 /* remove all selections */
10357 if (!(wKey & MK_CONTROL) && !(wKey & MK_SHIFT))
10358 LISTVIEW_DeselectAll(infoPtr);
10359 ReleaseCapture();
10362 return 0;
10365 /***
10366 * DESCRIPTION:
10367 * Processes mouse up messages (left mouse button).
10369 * PARAMETERS:
10370 * infoPtr [I ] valid pointer to the listview structure
10371 * wKey [I ] key flag
10372 * x,y [I ] mouse coordinate
10374 * RETURN:
10375 * Zero
10377 static LRESULT LISTVIEW_LButtonUp(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
10379 LVHITTESTINFO lvHitTestInfo;
10381 TRACE("(key=%hu, X=%u, Y=%u)\n", wKey, x, y);
10383 if (!infoPtr->bLButtonDown) return 0;
10385 lvHitTestInfo.pt.x = x;
10386 lvHitTestInfo.pt.y = y;
10388 /* send NM_CLICK notification */
10389 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
10390 if (!notify_click(infoPtr, NM_CLICK, &lvHitTestInfo)) return 0;
10392 /* set left button flag */
10393 infoPtr->bLButtonDown = FALSE;
10395 /* set a single selection, reset others */
10396 if(lvHitTestInfo.iItem == infoPtr->nLButtonDownItem && lvHitTestInfo.iItem != -1)
10397 LISTVIEW_SetSelection(infoPtr, infoPtr->nLButtonDownItem);
10398 infoPtr->nLButtonDownItem = -1;
10400 if (infoPtr->bDragging || infoPtr->bMarqueeSelect)
10402 /* Remove the marquee rectangle and release our mouse capture */
10403 if (infoPtr->bMarqueeSelect)
10405 LISTVIEW_InvalidateRect(infoPtr, &infoPtr->marqueeDrawRect);
10406 ReleaseCapture();
10409 SetRectEmpty(&infoPtr->marqueeRect);
10410 SetRectEmpty(&infoPtr->marqueeDrawRect);
10412 infoPtr->bDragging = FALSE;
10413 infoPtr->bMarqueeSelect = FALSE;
10414 infoPtr->bScrolling = FALSE;
10416 KillTimer(infoPtr->hwndSelf, (UINT_PTR) infoPtr);
10417 return 0;
10420 /* if we clicked on a selected item, edit the label */
10421 if(lvHitTestInfo.iItem == infoPtr->nEditLabelItem && (lvHitTestInfo.flags & LVHT_ONITEMLABEL))
10423 /* we want to make sure the user doesn't want to do a double click. So we will
10424 * delay the edit. WM_LBUTTONDBLCLICK will cancel the timer
10426 infoPtr->itemEdit.fEnabled = TRUE;
10427 infoPtr->itemEdit.iItem = lvHitTestInfo.iItem;
10428 SetTimer(infoPtr->hwndSelf,
10429 (UINT_PTR)&infoPtr->itemEdit,
10430 GetDoubleClickTime(),
10431 LISTVIEW_DelayedEditItem);
10434 return 0;
10437 /***
10438 * DESCRIPTION:
10439 * Destroys the listview control (called after WM_DESTROY).
10441 * PARAMETER(S):
10442 * [I] infoPtr : valid pointer to the listview structure
10444 * RETURN:
10445 * Zero
10447 static LRESULT LISTVIEW_NCDestroy(LISTVIEW_INFO *infoPtr)
10449 INT i;
10451 TRACE("()\n");
10453 /* destroy data structure */
10454 DPA_Destroy(infoPtr->hdpaItems);
10455 DPA_Destroy(infoPtr->hdpaItemIds);
10456 DPA_Destroy(infoPtr->hdpaPosX);
10457 DPA_Destroy(infoPtr->hdpaPosY);
10458 /* columns */
10459 for (i = 0; i < DPA_GetPtrCount(infoPtr->hdpaColumns); i++)
10460 Free(DPA_GetPtr(infoPtr->hdpaColumns, i));
10461 DPA_Destroy(infoPtr->hdpaColumns);
10462 ranges_destroy(infoPtr->selectionRanges);
10464 /* destroy image lists */
10465 if (!(infoPtr->dwStyle & LVS_SHAREIMAGELISTS))
10467 ImageList_Destroy(infoPtr->himlNormal);
10468 ImageList_Destroy(infoPtr->himlSmall);
10469 ImageList_Destroy(infoPtr->himlState);
10472 /* destroy font, bkgnd brush */
10473 infoPtr->hFont = 0;
10474 if (infoPtr->hDefaultFont) DeleteObject(infoPtr->hDefaultFont);
10475 if (infoPtr->clrBk != CLR_NONE) DeleteObject(infoPtr->hBkBrush);
10476 if (infoPtr->hBkBitmap) DeleteObject(infoPtr->hBkBitmap);
10478 SetWindowLongPtrW(infoPtr->hwndSelf, 0, 0);
10480 /* free listview info pointer*/
10481 Free(infoPtr);
10483 return 0;
10486 /***
10487 * DESCRIPTION:
10488 * Handles notifications.
10490 * PARAMETER(S):
10491 * [I] infoPtr : valid pointer to the listview structure
10492 * [I] lpnmhdr : notification information
10494 * RETURN:
10495 * Zero
10497 static LRESULT LISTVIEW_Notify(LISTVIEW_INFO *infoPtr, NMHDR *lpnmhdr)
10499 NMHEADERW *lpnmh;
10501 TRACE("(lpnmhdr=%p)\n", lpnmhdr);
10503 if (!lpnmhdr || lpnmhdr->hwndFrom != infoPtr->hwndHeader) return 0;
10505 /* remember: HDN_LAST < HDN_FIRST */
10506 if (lpnmhdr->code > HDN_FIRST || lpnmhdr->code < HDN_LAST) return 0;
10507 lpnmh = (NMHEADERW *)lpnmhdr;
10509 if (lpnmh->iItem < 0 || lpnmh->iItem >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return 0;
10511 switch (lpnmhdr->code)
10513 case HDN_TRACKW:
10514 case HDN_TRACKA:
10516 COLUMN_INFO *lpColumnInfo;
10517 POINT ptOrigin;
10518 INT x;
10520 if (!lpnmh->pitem || !(lpnmh->pitem->mask & HDI_WIDTH))
10521 break;
10523 /* remove the old line (if any) */
10524 LISTVIEW_DrawTrackLine(infoPtr);
10526 /* compute & draw the new line */
10527 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, lpnmh->iItem);
10528 x = lpColumnInfo->rcHeader.left + lpnmh->pitem->cxy;
10529 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
10530 infoPtr->xTrackLine = x + ptOrigin.x;
10531 LISTVIEW_DrawTrackLine(infoPtr);
10532 return notify_forward_header(infoPtr, lpnmh);
10535 case HDN_ENDTRACKA:
10536 case HDN_ENDTRACKW:
10537 /* remove the track line (if any) */
10538 LISTVIEW_DrawTrackLine(infoPtr);
10539 infoPtr->xTrackLine = -1;
10540 return notify_forward_header(infoPtr, lpnmh);
10542 case HDN_BEGINDRAG:
10543 if ((infoPtr->dwLvExStyle & LVS_EX_HEADERDRAGDROP) == 0) return 1;
10544 return notify_forward_header(infoPtr, lpnmh);
10546 case HDN_ENDDRAG:
10547 infoPtr->colRectsDirty = TRUE;
10548 LISTVIEW_InvalidateList(infoPtr);
10549 return notify_forward_header(infoPtr, lpnmh);
10551 case HDN_ITEMCHANGEDW:
10552 case HDN_ITEMCHANGEDA:
10554 COLUMN_INFO *lpColumnInfo;
10555 HDITEMW hdi;
10556 INT dx, cxy;
10558 if (!lpnmh->pitem || !(lpnmh->pitem->mask & HDI_WIDTH))
10560 hdi.mask = HDI_WIDTH;
10561 if (!SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, lpnmh->iItem, (LPARAM)&hdi)) return 0;
10562 cxy = hdi.cxy;
10564 else
10565 cxy = lpnmh->pitem->cxy;
10567 /* determine how much we change since the last know position */
10568 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, lpnmh->iItem);
10569 dx = cxy - (lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left);
10570 if (dx != 0)
10572 lpColumnInfo->rcHeader.right += dx;
10574 hdi.mask = HDI_ORDER;
10575 SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, lpnmh->iItem, (LPARAM)&hdi);
10577 /* not the rightmost one */
10578 if (hdi.iOrder + 1 < DPA_GetPtrCount(infoPtr->hdpaColumns))
10580 INT nIndex = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX,
10581 hdi.iOrder + 1, 0);
10582 LISTVIEW_ScrollColumns(infoPtr, nIndex, dx);
10584 else
10586 /* only needs to update the scrolls */
10587 infoPtr->nItemWidth += dx;
10588 LISTVIEW_UpdateScroll(infoPtr);
10590 LISTVIEW_UpdateItemSize(infoPtr);
10591 if (infoPtr->uView == LV_VIEW_DETAILS && is_redrawing(infoPtr))
10593 POINT ptOrigin;
10594 RECT rcCol = lpColumnInfo->rcHeader;
10596 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
10597 OffsetRect(&rcCol, ptOrigin.x, 0);
10599 rcCol.top = infoPtr->rcList.top;
10600 rcCol.bottom = infoPtr->rcList.bottom;
10602 /* resizing left-aligned columns leaves most of the left side untouched */
10603 if ((lpColumnInfo->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_LEFT)
10605 INT nMaxDirty = infoPtr->nEllipsisWidth + infoPtr->ntmMaxCharWidth;
10606 if (dx > 0)
10607 nMaxDirty += dx;
10608 rcCol.left = max (rcCol.left, rcCol.right - nMaxDirty);
10611 /* when shrinking the last column clear the now unused field */
10612 if (hdi.iOrder == DPA_GetPtrCount(infoPtr->hdpaColumns) - 1)
10614 RECT right;
10616 rcCol.right -= dx;
10618 /* deal with right from rightmost column area */
10619 right.left = rcCol.right;
10620 right.top = rcCol.top;
10621 right.bottom = rcCol.bottom;
10622 right.right = infoPtr->rcList.right;
10624 LISTVIEW_InvalidateRect(infoPtr, &right);
10627 LISTVIEW_InvalidateRect(infoPtr, &rcCol);
10630 break;
10633 case HDN_ITEMCLICKW:
10634 case HDN_ITEMCLICKA:
10636 /* Handle sorting by Header Column */
10637 NMLISTVIEW nmlv;
10639 ZeroMemory(&nmlv, sizeof(NMLISTVIEW));
10640 nmlv.iItem = -1;
10641 nmlv.iSubItem = lpnmh->iItem;
10642 notify_listview(infoPtr, LVN_COLUMNCLICK, &nmlv);
10643 return notify_forward_header(infoPtr, lpnmh);
10646 case HDN_DIVIDERDBLCLICKW:
10647 case HDN_DIVIDERDBLCLICKA:
10648 /* FIXME: for LVS_EX_HEADERINALLVIEWS and not LV_VIEW_DETAILS
10649 we should use LVSCW_AUTOSIZE_USEHEADER, helper rework or
10650 split needed for that */
10651 LISTVIEW_SetColumnWidth(infoPtr, lpnmh->iItem, LVSCW_AUTOSIZE);
10652 return notify_forward_header(infoPtr, lpnmh);
10654 return 0;
10657 /***
10658 * DESCRIPTION:
10659 * Paint non-client area of control.
10661 * PARAMETER(S):
10662 * [I] infoPtr : valid pointer to the listview structureof the sender
10663 * [I] region : update region
10665 * RETURN:
10666 * 0 - frame was painted
10668 static LRESULT LISTVIEW_NCPaint(const LISTVIEW_INFO *infoPtr, HRGN region)
10670 LONG exstyle = GetWindowLongW (infoPtr->hwndSelf, GWL_EXSTYLE);
10671 HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
10672 RECT r, window_rect;
10673 HDC dc;
10674 HRGN cliprgn;
10675 int cxEdge = GetSystemMetrics (SM_CXEDGE),
10676 cyEdge = GetSystemMetrics (SM_CYEDGE);
10678 if (!theme || !(exstyle & WS_EX_CLIENTEDGE))
10679 return DefWindowProcW (infoPtr->hwndSelf, WM_NCPAINT, (WPARAM)region, 0);
10681 GetWindowRect(infoPtr->hwndSelf, &r);
10683 cliprgn = CreateRectRgn (r.left + cxEdge, r.top + cyEdge,
10684 r.right - cxEdge, r.bottom - cyEdge);
10685 if (region != (HRGN)1)
10686 CombineRgn (cliprgn, cliprgn, region, RGN_AND);
10688 OffsetRect(&r, -r.left, -r.top);
10689 if (infoPtr->hwndHeader && LISTVIEW_IsHeaderEnabled(infoPtr))
10691 GetWindowRect(infoPtr->hwndHeader, &window_rect);
10692 r.top = min(r.bottom, r.top + window_rect.bottom - window_rect.top);
10695 dc = GetDCEx(infoPtr->hwndSelf, region, DCX_WINDOW|DCX_INTERSECTRGN);
10697 if (IsThemeBackgroundPartiallyTransparent (theme, 0, 0))
10698 DrawThemeParentBackground(infoPtr->hwndSelf, dc, &r);
10699 DrawThemeBackground (theme, dc, 0, 0, &r, 0);
10700 ReleaseDC(infoPtr->hwndSelf, dc);
10702 /* Call default proc to get the scrollbars etc. painted */
10703 DefWindowProcW (infoPtr->hwndSelf, WM_NCPAINT, (WPARAM)cliprgn, 0);
10704 DeleteObject(cliprgn);
10706 return 0;
10709 /***
10710 * DESCRIPTION:
10711 * Determines the type of structure to use.
10713 * PARAMETER(S):
10714 * [I] infoPtr : valid pointer to the listview structureof the sender
10715 * [I] hwndFrom : listview window handle
10716 * [I] nCommand : command specifying the nature of the WM_NOTIFYFORMAT
10718 * RETURN:
10719 * Zero
10721 static LRESULT LISTVIEW_NotifyFormat(LISTVIEW_INFO *infoPtr, HWND hwndFrom, INT nCommand)
10723 TRACE("(hwndFrom=%p, nCommand=%d)\n", hwndFrom, nCommand);
10725 if (nCommand == NF_REQUERY)
10726 infoPtr->notifyFormat = SendMessageW(infoPtr->hwndNotify, WM_NOTIFYFORMAT, (WPARAM)infoPtr->hwndSelf, NF_QUERY);
10728 return infoPtr->notifyFormat;
10731 /***
10732 * DESCRIPTION:
10733 * Paints/Repaints the listview control. Internal use.
10735 * PARAMETER(S):
10736 * [I] infoPtr : valid pointer to the listview structure
10737 * [I] hdc : device context handle
10739 * RETURN:
10740 * Zero
10742 static LRESULT LISTVIEW_Paint(LISTVIEW_INFO *infoPtr, HDC hdc)
10744 TRACE("(hdc=%p)\n", hdc);
10746 if (infoPtr->bNoItemMetrics && infoPtr->nItemCount)
10748 infoPtr->bNoItemMetrics = FALSE;
10749 LISTVIEW_UpdateItemSize(infoPtr);
10750 if (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON)
10751 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
10752 LISTVIEW_UpdateScroll(infoPtr);
10755 if (infoPtr->hwndHeader) UpdateWindow(infoPtr->hwndHeader);
10757 if (hdc)
10758 LISTVIEW_Refresh(infoPtr, hdc, NULL);
10759 else
10761 PAINTSTRUCT ps;
10763 hdc = BeginPaint(infoPtr->hwndSelf, &ps);
10764 if (!hdc) return 1;
10765 LISTVIEW_Refresh(infoPtr, hdc, ps.fErase ? &ps.rcPaint : NULL);
10766 EndPaint(infoPtr->hwndSelf, &ps);
10769 return 0;
10772 /***
10773 * DESCRIPTION:
10774 * Paints/Repaints the listview control, WM_PAINT handler.
10776 * PARAMETER(S):
10777 * [I] infoPtr : valid pointer to the listview structure
10778 * [I] hdc : device context handle
10780 * RETURN:
10781 * Zero
10783 static inline LRESULT LISTVIEW_WMPaint(LISTVIEW_INFO *infoPtr, HDC hdc)
10785 TRACE("(hdc=%p)\n", hdc);
10787 if (!is_redrawing(infoPtr))
10788 return DefWindowProcW (infoPtr->hwndSelf, WM_PAINT, (WPARAM)hdc, 0);
10790 return LISTVIEW_Paint(infoPtr, hdc);
10793 /***
10794 * DESCRIPTION:
10795 * Paints/Repaints the listview control.
10797 * PARAMETER(S):
10798 * [I] infoPtr : valid pointer to the listview structure
10799 * [I] hdc : device context handle
10800 * [I] options : drawing options
10802 * RETURN:
10803 * Zero
10805 static LRESULT LISTVIEW_PrintClient(LISTVIEW_INFO *infoPtr, HDC hdc, DWORD options)
10807 if ((options & PRF_CHECKVISIBLE) && !IsWindowVisible(infoPtr->hwndSelf))
10808 return 0;
10810 if (options & ~(PRF_ERASEBKGND|PRF_CLIENT))
10811 FIXME("(hdc=%p options %#lx) partial stub\n", hdc, options);
10813 if (options & PRF_ERASEBKGND)
10814 LISTVIEW_EraseBkgnd(infoPtr, hdc);
10816 if (options & PRF_CLIENT)
10817 LISTVIEW_Paint(infoPtr, hdc);
10819 return 0;
10823 /***
10824 * DESCRIPTION:
10825 * Processes double click messages (right mouse button).
10827 * PARAMETER(S):
10828 * [I] infoPtr : valid pointer to the listview structure
10829 * [I] wKey : key flag
10830 * [I] x,y : mouse coordinate
10832 * RETURN:
10833 * Zero
10835 static LRESULT LISTVIEW_RButtonDblClk(const LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
10837 LVHITTESTINFO lvHitTestInfo;
10839 TRACE("(key=%hu,X=%u,Y=%u)\n", wKey, x, y);
10841 /* send NM_RELEASEDCAPTURE notification */
10842 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
10844 /* send NM_RDBLCLK notification */
10845 lvHitTestInfo.pt.x = x;
10846 lvHitTestInfo.pt.y = y;
10847 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
10848 notify_click(infoPtr, NM_RDBLCLK, &lvHitTestInfo);
10850 return 0;
10853 /***
10854 * DESCRIPTION:
10855 * Processes WM_RBUTTONDOWN message and corresponding drag operation.
10857 * PARAMETER(S):
10858 * [I] infoPtr : valid pointer to the listview structure
10859 * [I] wKey : key flag
10860 * [I] x, y : mouse coordinate
10862 * RETURN:
10863 * Zero
10865 static LRESULT LISTVIEW_RButtonDown(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
10867 LVHITTESTINFO ht;
10868 INT item;
10870 TRACE("(key=%hu, x=%d, y=%d)\n", wKey, x, y);
10872 /* send NM_RELEASEDCAPTURE notification */
10873 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
10875 /* determine the index of the selected item */
10876 ht.pt.x = x;
10877 ht.pt.y = y;
10878 item = LISTVIEW_HitTest(infoPtr, &ht, TRUE, TRUE);
10880 /* make sure the listview control window has the focus */
10881 if (!infoPtr->bFocus) SetFocus(infoPtr->hwndSelf);
10883 if ((item >= 0) && (item < infoPtr->nItemCount))
10885 LISTVIEW_SetItemFocus(infoPtr, item);
10886 if (!((wKey & MK_SHIFT) || (wKey & MK_CONTROL)) &&
10887 !LISTVIEW_GetItemState(infoPtr, item, LVIS_SELECTED))
10888 LISTVIEW_SetSelection(infoPtr, item);
10890 else
10891 LISTVIEW_DeselectAll(infoPtr);
10893 if (LISTVIEW_TrackMouse(infoPtr, ht.pt))
10895 if (ht.iItem != -1)
10897 NMLISTVIEW nmlv;
10899 memset(&nmlv, 0, sizeof(nmlv));
10900 nmlv.iItem = ht.iItem;
10901 nmlv.ptAction = ht.pt;
10903 notify_listview(infoPtr, LVN_BEGINRDRAG, &nmlv);
10906 else
10908 SetFocus(infoPtr->hwndSelf);
10910 ht.pt.x = x;
10911 ht.pt.y = y;
10912 LISTVIEW_HitTest(infoPtr, &ht, TRUE, FALSE);
10914 if (notify_click(infoPtr, NM_RCLICK, &ht))
10916 /* Send a WM_CONTEXTMENU message in response to the WM_RBUTTONUP */
10917 SendMessageW(infoPtr->hwndSelf, WM_CONTEXTMENU,
10918 (WPARAM)infoPtr->hwndSelf, (LPARAM)GetMessagePos());
10922 return 0;
10925 /***
10926 * DESCRIPTION:
10927 * Sets the cursor.
10929 * PARAMETER(S):
10930 * [I] infoPtr : valid pointer to the listview structure
10931 * [I] hwnd : window handle of window containing the cursor
10932 * [I] nHittest : hit-test code
10933 * [I] wMouseMsg : ideintifier of the mouse message
10935 * RETURN:
10936 * TRUE if cursor is set
10937 * FALSE otherwise
10939 static BOOL LISTVIEW_SetCursor(const LISTVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
10941 LVHITTESTINFO lvHitTestInfo;
10943 if (!LISTVIEW_IsHotTracking(infoPtr)) goto forward;
10945 if (!infoPtr->hHotCursor) goto forward;
10947 GetCursorPos(&lvHitTestInfo.pt);
10948 if (LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, FALSE, FALSE) < 0) goto forward;
10950 SetCursor(infoPtr->hHotCursor);
10952 return TRUE;
10954 forward:
10956 return DefWindowProcW(infoPtr->hwndSelf, WM_SETCURSOR, wParam, lParam);
10959 /***
10960 * DESCRIPTION:
10961 * Sets the focus.
10963 * PARAMETER(S):
10964 * [I] infoPtr : valid pointer to the listview structure
10965 * [I] hwndLoseFocus : handle of previously focused window
10967 * RETURN:
10968 * Zero
10970 static LRESULT LISTVIEW_SetFocus(LISTVIEW_INFO *infoPtr, HWND hwndLoseFocus)
10972 TRACE("(hwndLoseFocus=%p)\n", hwndLoseFocus);
10974 /* if we have the focus already, there's nothing to do */
10975 if (infoPtr->bFocus) return 0;
10977 /* send NM_SETFOCUS notification */
10978 if (!notify(infoPtr, NM_SETFOCUS)) return 0;
10980 /* set window focus flag */
10981 infoPtr->bFocus = TRUE;
10983 /* put the focus rect back on */
10984 LISTVIEW_ShowFocusRect(infoPtr, TRUE);
10986 /* redraw all visible selected items */
10987 LISTVIEW_InvalidateSelectedItems(infoPtr);
10989 return 0;
10992 /***
10993 * DESCRIPTION:
10994 * Sets the font.
10996 * PARAMETER(S):
10997 * [I] infoPtr : valid pointer to the listview structure
10998 * [I] fRedraw : font handle
10999 * [I] fRedraw : redraw flag
11001 * RETURN:
11002 * Zero
11004 static LRESULT LISTVIEW_SetFont(LISTVIEW_INFO *infoPtr, HFONT hFont, WORD fRedraw)
11006 HFONT oldFont = infoPtr->hFont;
11007 INT oldHeight = infoPtr->nItemHeight;
11009 TRACE("(hfont=%p,redraw=%hu)\n", hFont, fRedraw);
11011 infoPtr->hFont = hFont ? hFont : infoPtr->hDefaultFont;
11012 if (infoPtr->hFont == oldFont) return 0;
11014 LISTVIEW_SaveTextMetrics(infoPtr);
11016 infoPtr->nItemHeight = LISTVIEW_CalculateItemHeight(infoPtr);
11018 if (infoPtr->uView == LV_VIEW_DETAILS)
11020 SendMessageW(infoPtr->hwndHeader, WM_SETFONT, (WPARAM)hFont, MAKELPARAM(fRedraw, 0));
11021 LISTVIEW_UpdateSize(infoPtr);
11022 LISTVIEW_UpdateScroll(infoPtr);
11024 else if (infoPtr->nItemHeight != oldHeight)
11025 LISTVIEW_UpdateScroll(infoPtr);
11027 if (fRedraw) LISTVIEW_InvalidateList(infoPtr);
11029 return 0;
11032 /***
11033 * DESCRIPTION:
11034 * Message handling for WM_SETREDRAW.
11035 * For the Listview, it invalidates the entire window (the doc specifies otherwise)
11037 * PARAMETER(S):
11038 * [I] infoPtr : valid pointer to the listview structure
11039 * [I] redraw: state of redraw flag
11041 * RETURN:
11042 * Zero.
11044 static LRESULT LISTVIEW_SetRedraw(LISTVIEW_INFO *infoPtr, BOOL redraw)
11046 TRACE("old=%d, new=%d\n", infoPtr->redraw, redraw);
11048 if (infoPtr->redraw == !!redraw)
11049 return 0;
11051 if (!(infoPtr->redraw = !!redraw))
11052 return 0;
11054 if (is_autoarrange(infoPtr))
11055 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
11056 LISTVIEW_UpdateScroll(infoPtr);
11058 /* despite what the WM_SETREDRAW docs says, apps expect us
11059 * to invalidate the listview here... stupid! */
11060 LISTVIEW_InvalidateList(infoPtr);
11062 return 0;
11065 /***
11066 * DESCRIPTION:
11067 * Resizes the listview control. This function processes WM_SIZE
11068 * messages. At this time, the width and height are not used.
11070 * PARAMETER(S):
11071 * [I] infoPtr : valid pointer to the listview structure
11072 * [I] Width : new width
11073 * [I] Height : new height
11075 * RETURN:
11076 * Zero
11078 static LRESULT LISTVIEW_Size(LISTVIEW_INFO *infoPtr, int Width, int Height)
11080 RECT rcOld = infoPtr->rcList;
11082 TRACE("(width=%d, height=%d)\n", Width, Height);
11084 LISTVIEW_UpdateSize(infoPtr);
11085 if (EqualRect(&rcOld, &infoPtr->rcList)) return 0;
11087 /* do not bother with display related stuff if we're not redrawing */
11088 if (!is_redrawing(infoPtr)) return 0;
11090 if (is_autoarrange(infoPtr))
11091 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
11093 LISTVIEW_UpdateScroll(infoPtr);
11095 /* refresh all only for lists whose height changed significantly */
11096 if ((infoPtr->uView == LV_VIEW_LIST) &&
11097 (rcOld.bottom - rcOld.top) / infoPtr->nItemHeight !=
11098 (infoPtr->rcList.bottom - infoPtr->rcList.top) / infoPtr->nItemHeight)
11099 LISTVIEW_InvalidateList(infoPtr);
11101 return 0;
11104 /***
11105 * DESCRIPTION:
11106 * Sets the size information.
11108 * PARAMETER(S):
11109 * [I] infoPtr : valid pointer to the listview structure
11111 * RETURN:
11112 * None
11114 static void LISTVIEW_UpdateSize(LISTVIEW_INFO *infoPtr)
11116 TRACE("uView %ld, rcList(old)=%s\n", infoPtr->uView, wine_dbgstr_rect(&infoPtr->rcList));
11118 GetClientRect(infoPtr->hwndSelf, &infoPtr->rcList);
11120 if (infoPtr->uView == LV_VIEW_LIST)
11122 /* Apparently the "LIST" style is supposed to have the same
11123 * number of items in a column even if there is no scroll bar.
11124 * Since if a scroll bar already exists then the bottom is already
11125 * reduced, only reduce if the scroll bar does not currently exist.
11126 * The "2" is there to mimic the native control. I think it may be
11127 * related to either padding or edges. (GLA 7/2002)
11129 if (!(GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE) & WS_HSCROLL))
11130 infoPtr->rcList.bottom -= GetSystemMetrics(SM_CYHSCROLL);
11131 infoPtr->rcList.bottom = max (infoPtr->rcList.bottom - 2, 0);
11134 /* When ListView control is created invisible, header isn't created right away. */
11135 if (infoPtr->hwndHeader)
11137 POINT origin;
11138 WINDOWPOS wp;
11139 HDLAYOUT hl;
11140 RECT rect;
11142 LISTVIEW_GetOrigin(infoPtr, &origin);
11144 rect = infoPtr->rcList;
11145 rect.left += origin.x;
11147 hl.prc = &rect;
11148 hl.pwpos = &wp;
11149 SendMessageW( infoPtr->hwndHeader, HDM_LAYOUT, 0, (LPARAM)&hl );
11150 TRACE(" wp.flags=0x%08x, wp=%d,%d (%dx%d)\n", wp.flags, wp.x, wp.y, wp.cx, wp.cy);
11152 if (LISTVIEW_IsHeaderEnabled(infoPtr))
11153 wp.flags |= SWP_SHOWWINDOW;
11154 else
11156 wp.flags |= SWP_HIDEWINDOW;
11157 wp.cy = 0;
11160 SetWindowPos(wp.hwnd, wp.hwndInsertAfter, wp.x, wp.y, wp.cx, wp.cy, wp.flags);
11161 TRACE(" after SWP wp=%d,%d (%dx%d)\n", wp.x, wp.y, wp.cx, wp.cy);
11163 infoPtr->rcList.top = max(wp.cy, 0);
11165 /* extra padding for grid */
11166 if (infoPtr->uView == LV_VIEW_DETAILS && infoPtr->dwLvExStyle & LVS_EX_GRIDLINES)
11167 infoPtr->rcList.top += 2;
11169 TRACE(" rcList=%s\n", wine_dbgstr_rect(&infoPtr->rcList));
11172 /***
11173 * DESCRIPTION:
11174 * Processes WM_STYLECHANGED messages.
11176 * PARAMETER(S):
11177 * [I] infoPtr : valid pointer to the listview structure
11178 * [I] wStyleType : window style type (normal or extended)
11179 * [I] lpss : window style information
11181 * RETURN:
11182 * Zero
11184 static INT LISTVIEW_StyleChanged(LISTVIEW_INFO *infoPtr, WPARAM wStyleType,
11185 const STYLESTRUCT *lpss)
11187 UINT uNewView, uOldView;
11188 UINT style;
11190 TRACE("styletype %Ix, styleOld %#lx, styleNew %#lx\n",
11191 wStyleType, lpss->styleOld, lpss->styleNew);
11193 if (wStyleType != GWL_STYLE || lpss->styleNew == infoPtr->dwStyle) return 0;
11195 infoPtr->dwStyle = lpss->styleNew;
11197 if (((lpss->styleOld & WS_HSCROLL) != 0)&&
11198 ((lpss->styleNew & WS_HSCROLL) == 0))
11199 ShowScrollBar(infoPtr->hwndSelf, SB_HORZ, FALSE);
11201 if (((lpss->styleOld & WS_VSCROLL) != 0)&&
11202 ((lpss->styleNew & WS_VSCROLL) == 0))
11203 ShowScrollBar(infoPtr->hwndSelf, SB_VERT, FALSE);
11205 uNewView = lpss->styleNew & LVS_TYPEMASK;
11206 uOldView = lpss->styleOld & LVS_TYPEMASK;
11208 if (uNewView != uOldView)
11210 HIMAGELIST himl;
11212 /* LVM_SETVIEW doesn't change window style bits within LVS_TYPEMASK,
11213 changing style updates current view only when view bits change. */
11214 map_style_view(infoPtr);
11215 SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
11216 ShowWindow(infoPtr->hwndHeader, SW_HIDE);
11218 ShowScrollBar(infoPtr->hwndSelf, SB_BOTH, FALSE);
11219 SetRectEmpty(&infoPtr->rcFocus);
11221 himl = (uNewView == LVS_ICON ? infoPtr->himlNormal : infoPtr->himlSmall);
11222 set_icon_size(&infoPtr->iconSize, himl, uNewView != LVS_ICON);
11224 if (uNewView == LVS_REPORT)
11226 HDLAYOUT hl;
11227 WINDOWPOS wp;
11229 LISTVIEW_CreateHeader( infoPtr );
11231 hl.prc = &infoPtr->rcList;
11232 hl.pwpos = &wp;
11233 SendMessageW( infoPtr->hwndHeader, HDM_LAYOUT, 0, (LPARAM)&hl );
11234 SetWindowPos(infoPtr->hwndHeader, infoPtr->hwndSelf, wp.x, wp.y, wp.cx, wp.cy,
11235 wp.flags | ((infoPtr->dwStyle & LVS_NOCOLUMNHEADER)
11236 ? SWP_HIDEWINDOW : SWP_SHOWWINDOW));
11239 LISTVIEW_UpdateItemSize(infoPtr);
11242 if (uNewView == LVS_REPORT || infoPtr->dwLvExStyle & LVS_EX_HEADERINALLVIEWS)
11244 if ((lpss->styleOld ^ lpss->styleNew) & LVS_NOCOLUMNHEADER)
11246 if (lpss->styleNew & LVS_NOCOLUMNHEADER)
11248 /* Turn off the header control */
11249 style = GetWindowLongW(infoPtr->hwndHeader, GWL_STYLE);
11250 TRACE("Hide header control, was 0x%08x\n", style);
11251 SetWindowLongW(infoPtr->hwndHeader, GWL_STYLE, style | HDS_HIDDEN);
11252 } else {
11253 /* Turn on the header control */
11254 if ((style = GetWindowLongW(infoPtr->hwndHeader, GWL_STYLE)) & HDS_HIDDEN)
11256 TRACE("Show header control, was 0x%08x\n", style);
11257 SetWindowLongW(infoPtr->hwndHeader, GWL_STYLE, (style & ~HDS_HIDDEN) | WS_VISIBLE);
11263 if ( (uNewView == LVS_ICON || uNewView == LVS_SMALLICON) &&
11264 (uNewView != uOldView || ((lpss->styleNew ^ lpss->styleOld) & LVS_ALIGNMASK)) )
11265 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
11267 /* update the size of the client area */
11268 LISTVIEW_UpdateSize(infoPtr);
11270 /* add scrollbars if needed */
11271 LISTVIEW_UpdateScroll(infoPtr);
11273 /* invalidate client area + erase background */
11274 LISTVIEW_InvalidateList(infoPtr);
11276 return 0;
11279 /***
11280 * DESCRIPTION:
11281 * Processes WM_STYLECHANGING messages.
11283 * PARAMETER(S):
11284 * [I] wStyleType : window style type (normal or extended)
11285 * [I0] lpss : window style information
11287 * RETURN:
11288 * Zero
11290 static INT LISTVIEW_StyleChanging(WPARAM wStyleType,
11291 STYLESTRUCT *lpss)
11293 TRACE("styletype %Ix, styleOld %#lx, styleNew %#lx\n",
11294 wStyleType, lpss->styleOld, lpss->styleNew);
11296 /* don't forward LVS_OWNERDATA only if not already set to */
11297 if ((lpss->styleNew ^ lpss->styleOld) & LVS_OWNERDATA)
11299 if (lpss->styleOld & LVS_OWNERDATA)
11300 lpss->styleNew |= LVS_OWNERDATA;
11301 else
11302 lpss->styleNew &= ~LVS_OWNERDATA;
11305 return 0;
11308 /***
11309 * DESCRIPTION:
11310 * Processes WM_SHOWWINDOW messages.
11312 * PARAMETER(S):
11313 * [I] infoPtr : valid pointer to the listview structure
11314 * [I] bShown : window is being shown (FALSE when hidden)
11315 * [I] iStatus : window show status
11317 * RETURN:
11318 * Zero
11320 static LRESULT LISTVIEW_ShowWindow(LISTVIEW_INFO *infoPtr, WPARAM bShown, LPARAM iStatus)
11322 /* header delayed creation */
11323 if ((infoPtr->uView == LV_VIEW_DETAILS) && bShown)
11325 LISTVIEW_CreateHeader(infoPtr);
11327 if (!(LVS_NOCOLUMNHEADER & infoPtr->dwStyle))
11328 ShowWindow(infoPtr->hwndHeader, SW_SHOWNORMAL);
11331 return DefWindowProcW(infoPtr->hwndSelf, WM_SHOWWINDOW, bShown, iStatus);
11334 /***
11335 * DESCRIPTION:
11336 * Processes CCM_GETVERSION messages.
11338 * PARAMETER(S):
11339 * [I] infoPtr : valid pointer to the listview structure
11341 * RETURN:
11342 * Current version
11344 static inline LRESULT LISTVIEW_GetVersion(const LISTVIEW_INFO *infoPtr)
11346 return infoPtr->iVersion;
11349 /***
11350 * DESCRIPTION:
11351 * Processes CCM_SETVERSION messages.
11353 * PARAMETER(S):
11354 * [I] infoPtr : valid pointer to the listview structure
11355 * [I] iVersion : version to be set
11357 * RETURN:
11358 * -1 when requested version is greater than DLL version;
11359 * previous version otherwise
11361 static LRESULT LISTVIEW_SetVersion(LISTVIEW_INFO *infoPtr, DWORD iVersion)
11363 INT iOldVersion = infoPtr->iVersion;
11365 if (iVersion > COMCTL32_VERSION)
11366 return -1;
11368 infoPtr->iVersion = iVersion;
11370 TRACE("new version %ld\n", iVersion);
11372 return iOldVersion;
11375 /***
11376 * DESCRIPTION:
11377 * Window procedure of the listview control.
11380 static LRESULT WINAPI
11381 LISTVIEW_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
11383 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongPtrW(hwnd, 0);
11385 TRACE("hwnd %p, uMsg %x, wParam %Ix, lParam %Ix\n", hwnd, uMsg, wParam, lParam);
11387 if (!infoPtr && (uMsg != WM_NCCREATE))
11388 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
11390 switch (uMsg)
11392 case LVM_APPROXIMATEVIEWRECT:
11393 return LISTVIEW_ApproximateViewRect(infoPtr, (INT)wParam,
11394 LOWORD(lParam), HIWORD(lParam));
11395 case LVM_ARRANGE:
11396 return LISTVIEW_Arrange(infoPtr, (INT)wParam);
11398 case LVM_CANCELEDITLABEL:
11399 return LISTVIEW_CancelEditLabel(infoPtr);
11401 case LVM_CREATEDRAGIMAGE:
11402 return (LRESULT)LISTVIEW_CreateDragImage(infoPtr, (INT)wParam, (LPPOINT)lParam);
11404 case LVM_DELETEALLITEMS:
11405 return LISTVIEW_DeleteAllItems(infoPtr, FALSE);
11407 case LVM_DELETECOLUMN:
11408 return LISTVIEW_DeleteColumn(infoPtr, (INT)wParam);
11410 case LVM_DELETEITEM:
11411 return LISTVIEW_DeleteItem(infoPtr, (INT)wParam);
11413 case LVM_EDITLABELA:
11414 case LVM_EDITLABELW:
11415 return (LRESULT)LISTVIEW_EditLabelT(infoPtr, (INT)wParam,
11416 uMsg == LVM_EDITLABELW);
11417 /* case LVM_ENABLEGROUPVIEW: */
11419 case LVM_ENSUREVISIBLE:
11420 return LISTVIEW_EnsureVisible(infoPtr, (INT)wParam, (BOOL)lParam);
11422 case LVM_FINDITEMW:
11423 return LISTVIEW_FindItemW(infoPtr, (INT)wParam, (LPLVFINDINFOW)lParam);
11425 case LVM_FINDITEMA:
11426 return LISTVIEW_FindItemA(infoPtr, (INT)wParam, (LPLVFINDINFOA)lParam);
11428 case LVM_GETBKCOLOR:
11429 return infoPtr->clrBk;
11431 /* case LVM_GETBKIMAGE: */
11433 case LVM_GETCALLBACKMASK:
11434 return infoPtr->uCallbackMask;
11436 case LVM_GETCOLUMNA:
11437 case LVM_GETCOLUMNW:
11438 return LISTVIEW_GetColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam,
11439 uMsg == LVM_GETCOLUMNW);
11441 case LVM_GETCOLUMNORDERARRAY:
11442 return LISTVIEW_GetColumnOrderArray(infoPtr, (INT)wParam, (LPINT)lParam);
11444 case LVM_GETCOLUMNWIDTH:
11445 return LISTVIEW_GetColumnWidth(infoPtr, (INT)wParam);
11447 case LVM_GETCOUNTPERPAGE:
11448 return LISTVIEW_GetCountPerPage(infoPtr);
11450 case LVM_GETEDITCONTROL:
11451 return (LRESULT)infoPtr->hwndEdit;
11453 case LVM_GETEXTENDEDLISTVIEWSTYLE:
11454 return infoPtr->dwLvExStyle;
11456 /* case LVM_GETGROUPINFO: */
11458 /* case LVM_GETGROUPMETRICS: */
11460 case LVM_GETHEADER:
11461 return (LRESULT)infoPtr->hwndHeader;
11463 case LVM_GETHOTCURSOR:
11464 return (LRESULT)infoPtr->hHotCursor;
11466 case LVM_GETHOTITEM:
11467 return infoPtr->nHotItem;
11469 case LVM_GETHOVERTIME:
11470 return infoPtr->dwHoverTime;
11472 case LVM_GETIMAGELIST:
11473 return (LRESULT)LISTVIEW_GetImageList(infoPtr, (INT)wParam);
11475 /* case LVM_GETINSERTMARK: */
11477 /* case LVM_GETINSERTMARKCOLOR: */
11479 /* case LVM_GETINSERTMARKRECT: */
11481 case LVM_GETISEARCHSTRINGA:
11482 case LVM_GETISEARCHSTRINGW:
11483 FIXME("LVM_GETISEARCHSTRING: unimplemented\n");
11484 return FALSE;
11486 case LVM_GETITEMA:
11487 case LVM_GETITEMW:
11488 return LISTVIEW_GetItemExtT(infoPtr, (LPLVITEMW)lParam, uMsg == LVM_GETITEMW);
11490 case LVM_GETITEMCOUNT:
11491 return infoPtr->nItemCount;
11493 case LVM_GETITEMPOSITION:
11494 return LISTVIEW_GetItemPosition(infoPtr, (INT)wParam, (LPPOINT)lParam);
11496 case LVM_GETITEMRECT:
11497 return LISTVIEW_GetItemRect(infoPtr, (INT)wParam, (LPRECT)lParam);
11499 case LVM_GETITEMSPACING:
11500 return LISTVIEW_GetItemSpacing(infoPtr, (BOOL)wParam);
11502 case LVM_GETITEMSTATE:
11503 return LISTVIEW_GetItemState(infoPtr, (INT)wParam, (UINT)lParam);
11505 case LVM_GETITEMTEXTA:
11506 case LVM_GETITEMTEXTW:
11507 return LISTVIEW_GetItemTextT(infoPtr, (INT)wParam, (LPLVITEMW)lParam,
11508 uMsg == LVM_GETITEMTEXTW);
11510 case LVM_GETNEXTITEM:
11511 return LISTVIEW_GetNextItem(infoPtr, (INT)wParam, LOWORD(lParam));
11513 case LVM_GETNEXTITEMINDEX:
11514 return LISTVIEW_GetNextItemIndex(infoPtr, (LVITEMINDEX *)wParam, lParam);
11516 case LVM_GETNUMBEROFWORKAREAS:
11517 FIXME("LVM_GETNUMBEROFWORKAREAS: unimplemented\n");
11518 return 1;
11520 case LVM_GETORIGIN:
11521 if (!lParam) return FALSE;
11522 if (infoPtr->uView == LV_VIEW_DETAILS ||
11523 infoPtr->uView == LV_VIEW_LIST) return FALSE;
11524 LISTVIEW_GetOrigin(infoPtr, (LPPOINT)lParam);
11525 return TRUE;
11527 /* case LVM_GETOUTLINECOLOR: */
11529 case LVM_GETSELECTEDCOLUMN:
11530 return infoPtr->selected_column;
11532 case LVM_GETSELECTEDCOUNT:
11533 return LISTVIEW_GetSelectedCount(infoPtr);
11535 case LVM_GETSELECTIONMARK:
11536 return infoPtr->nSelectionMark;
11538 case LVM_GETSTRINGWIDTHA:
11539 case LVM_GETSTRINGWIDTHW:
11540 return LISTVIEW_GetStringWidthT(infoPtr, (LPCWSTR)lParam,
11541 uMsg == LVM_GETSTRINGWIDTHW);
11543 case LVM_GETSUBITEMRECT:
11544 return LISTVIEW_GetSubItemRect(infoPtr, (UINT)wParam, (LPRECT)lParam);
11546 case LVM_GETTEXTBKCOLOR:
11547 return infoPtr->clrTextBk;
11549 case LVM_GETTEXTCOLOR:
11550 return infoPtr->clrText;
11552 /* case LVM_GETTILEINFO: */
11554 /* case LVM_GETTILEVIEWINFO: */
11556 case LVM_GETTOOLTIPS:
11557 if( !infoPtr->hwndToolTip )
11558 infoPtr->hwndToolTip = COMCTL32_CreateToolTip( hwnd );
11559 return (LRESULT)infoPtr->hwndToolTip;
11561 case LVM_GETTOPINDEX:
11562 return LISTVIEW_GetTopIndex(infoPtr);
11564 case LVM_GETUNICODEFORMAT:
11565 return (infoPtr->notifyFormat == NFR_UNICODE);
11567 case LVM_GETVIEW:
11568 return infoPtr->uView;
11570 case LVM_GETVIEWRECT:
11571 return LISTVIEW_GetViewRect(infoPtr, (LPRECT)lParam);
11573 case LVM_GETWORKAREAS:
11574 FIXME("LVM_GETWORKAREAS: unimplemented\n");
11575 return FALSE;
11577 /* case LVM_HASGROUP: */
11579 case LVM_HITTEST:
11580 return LISTVIEW_HitTest(infoPtr, (LPLVHITTESTINFO)lParam, FALSE, TRUE);
11582 case LVM_INSERTCOLUMNA:
11583 case LVM_INSERTCOLUMNW:
11584 return LISTVIEW_InsertColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam,
11585 uMsg == LVM_INSERTCOLUMNW);
11587 /* case LVM_INSERTGROUP: */
11589 /* case LVM_INSERTGROUPSORTED: */
11591 case LVM_INSERTITEMA:
11592 case LVM_INSERTITEMW:
11593 return LISTVIEW_InsertItemT(infoPtr, (LPLVITEMW)lParam, uMsg == LVM_INSERTITEMW);
11595 /* case LVM_INSERTMARKHITTEST: */
11597 /* case LVM_ISGROUPVIEWENABLED: */
11599 case LVM_ISITEMVISIBLE:
11600 return LISTVIEW_IsItemVisible(infoPtr, (INT)wParam);
11602 case LVM_MAPIDTOINDEX:
11603 return LISTVIEW_MapIdToIndex(infoPtr, (UINT)wParam);
11605 case LVM_MAPINDEXTOID:
11606 return LISTVIEW_MapIndexToId(infoPtr, (INT)wParam);
11608 /* case LVM_MOVEGROUP: */
11610 /* case LVM_MOVEITEMTOGROUP: */
11612 case LVM_REDRAWITEMS:
11613 return LISTVIEW_RedrawItems(infoPtr, (INT)wParam, (INT)lParam);
11615 /* case LVM_REMOVEALLGROUPS: */
11617 /* case LVM_REMOVEGROUP: */
11619 case LVM_SCROLL:
11620 return LISTVIEW_Scroll(infoPtr, (INT)wParam, (INT)lParam);
11622 case LVM_SETBKCOLOR:
11623 return LISTVIEW_SetBkColor(infoPtr, (COLORREF)lParam);
11625 case LVM_SETBKIMAGEA:
11626 case LVM_SETBKIMAGEW:
11627 return LISTVIEW_SetBkImage(infoPtr, (LVBKIMAGEW *)lParam, uMsg == LVM_SETBKIMAGEW);
11629 case LVM_SETCALLBACKMASK:
11630 infoPtr->uCallbackMask = (UINT)wParam;
11631 return TRUE;
11633 case LVM_SETCOLUMNA:
11634 case LVM_SETCOLUMNW:
11635 return LISTVIEW_SetColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam,
11636 uMsg == LVM_SETCOLUMNW);
11638 case LVM_SETCOLUMNORDERARRAY:
11639 return LISTVIEW_SetColumnOrderArray(infoPtr, (INT)wParam, (LPINT)lParam);
11641 case LVM_SETCOLUMNWIDTH:
11642 return LISTVIEW_SetColumnWidth(infoPtr, (INT)wParam, (short)LOWORD(lParam));
11644 case LVM_SETEXTENDEDLISTVIEWSTYLE:
11645 return LISTVIEW_SetExtendedListViewStyle(infoPtr, (DWORD)wParam, (DWORD)lParam);
11647 /* case LVM_SETGROUPINFO: */
11649 /* case LVM_SETGROUPMETRICS: */
11651 case LVM_SETHOTCURSOR:
11652 return (LRESULT)LISTVIEW_SetHotCursor(infoPtr, (HCURSOR)lParam);
11654 case LVM_SETHOTITEM:
11655 return LISTVIEW_SetHotItem(infoPtr, (INT)wParam);
11657 case LVM_SETHOVERTIME:
11658 return LISTVIEW_SetHoverTime(infoPtr, (DWORD)lParam);
11660 case LVM_SETICONSPACING:
11661 if(lParam == -1)
11662 return LISTVIEW_SetIconSpacing(infoPtr, -1, -1);
11663 return LISTVIEW_SetIconSpacing(infoPtr, LOWORD(lParam), HIWORD(lParam));
11665 case LVM_SETIMAGELIST:
11666 return (LRESULT)LISTVIEW_SetImageList(infoPtr, (INT)wParam, (HIMAGELIST)lParam);
11668 /* case LVM_SETINFOTIP: */
11670 /* case LVM_SETINSERTMARK: */
11672 /* case LVM_SETINSERTMARKCOLOR: */
11674 case LVM_SETITEMA:
11675 case LVM_SETITEMW:
11677 if (infoPtr->dwStyle & LVS_OWNERDATA) return FALSE;
11678 return LISTVIEW_SetItemT(infoPtr, (LPLVITEMW)lParam, (uMsg == LVM_SETITEMW));
11681 case LVM_SETITEMCOUNT:
11682 return LISTVIEW_SetItemCount(infoPtr, (INT)wParam, (DWORD)lParam);
11684 case LVM_SETITEMPOSITION:
11686 POINT pt;
11687 pt.x = (short)LOWORD(lParam);
11688 pt.y = (short)HIWORD(lParam);
11689 return LISTVIEW_SetItemPosition(infoPtr, (INT)wParam, &pt);
11692 case LVM_SETITEMPOSITION32:
11693 return LISTVIEW_SetItemPosition(infoPtr, (INT)wParam, (POINT*)lParam);
11695 case LVM_SETITEMSTATE:
11696 return LISTVIEW_SetItemState(infoPtr, (INT)wParam, (LPLVITEMW)lParam);
11698 case LVM_SETITEMTEXTA:
11699 case LVM_SETITEMTEXTW:
11700 return LISTVIEW_SetItemTextT(infoPtr, (INT)wParam, (LPLVITEMW)lParam,
11701 uMsg == LVM_SETITEMTEXTW);
11703 /* case LVM_SETOUTLINECOLOR: */
11705 case LVM_SETSELECTEDCOLUMN:
11706 infoPtr->selected_column = (INT)wParam;
11707 return TRUE;
11709 case LVM_SETSELECTIONMARK:
11710 return LISTVIEW_SetSelectionMark(infoPtr, (INT)lParam);
11712 case LVM_SETTEXTBKCOLOR:
11713 return LISTVIEW_SetTextBkColor(infoPtr, (COLORREF)lParam);
11715 case LVM_SETTEXTCOLOR:
11716 return LISTVIEW_SetTextColor(infoPtr, (COLORREF)lParam);
11718 /* case LVM_SETTILEINFO: */
11720 /* case LVM_SETTILEVIEWINFO: */
11722 /* case LVM_SETTILEWIDTH: */
11724 case LVM_SETTOOLTIPS:
11725 return (LRESULT)LISTVIEW_SetToolTips(infoPtr, (HWND)lParam);
11727 case LVM_SETUNICODEFORMAT:
11728 return LISTVIEW_SetUnicodeFormat(infoPtr, wParam);
11730 case LVM_SETVIEW:
11731 return LISTVIEW_SetView(infoPtr, wParam);
11733 /* case LVM_SETWORKAREAS: */
11735 /* case LVM_SORTGROUPS: */
11737 case LVM_SORTITEMS:
11738 case LVM_SORTITEMSEX:
11739 return LISTVIEW_SortItems(infoPtr, (PFNLVCOMPARE)lParam, wParam,
11740 uMsg == LVM_SORTITEMSEX);
11741 case LVM_SUBITEMHITTEST:
11742 return LISTVIEW_HitTest(infoPtr, (LPLVHITTESTINFO)lParam, TRUE, FALSE);
11744 case LVM_UPDATE:
11745 return LISTVIEW_Update(infoPtr, (INT)wParam);
11747 case CCM_GETVERSION:
11748 return LISTVIEW_GetVersion(infoPtr);
11750 case CCM_SETVERSION:
11751 return LISTVIEW_SetVersion(infoPtr, wParam);
11753 case WM_CHAR:
11754 return LISTVIEW_ProcessLetterKeys( infoPtr, wParam, lParam );
11756 case WM_COMMAND:
11757 return LISTVIEW_Command(infoPtr, wParam, lParam);
11759 case WM_NCCREATE:
11760 return LISTVIEW_NCCreate(hwnd, wParam, (LPCREATESTRUCTW)lParam);
11762 case WM_CREATE:
11763 return LISTVIEW_Create(hwnd, (LPCREATESTRUCTW)lParam);
11765 case WM_DESTROY:
11766 return LISTVIEW_Destroy(infoPtr);
11768 case WM_ENABLE:
11769 return LISTVIEW_Enable(infoPtr);
11771 case WM_ERASEBKGND:
11772 return LISTVIEW_EraseBkgnd(infoPtr, (HDC)wParam);
11774 case WM_GETDLGCODE:
11775 return DLGC_WANTCHARS | DLGC_WANTARROWS;
11777 case WM_GETFONT:
11778 return (LRESULT)infoPtr->hFont;
11780 case WM_HSCROLL:
11781 return LISTVIEW_HScroll(infoPtr, (INT)LOWORD(wParam), 0);
11783 case WM_KEYDOWN:
11784 return LISTVIEW_KeyDown(infoPtr, (INT)wParam, (LONG)lParam);
11786 case WM_KILLFOCUS:
11787 return LISTVIEW_KillFocus(infoPtr);
11789 case WM_LBUTTONDBLCLK:
11790 return LISTVIEW_LButtonDblClk(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11792 case WM_LBUTTONDOWN:
11793 return LISTVIEW_LButtonDown(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11795 case WM_LBUTTONUP:
11796 return LISTVIEW_LButtonUp(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11798 case WM_MOUSEMOVE:
11799 return LISTVIEW_MouseMove (infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11801 case WM_MOUSEHOVER:
11802 return LISTVIEW_MouseHover(infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11804 case WM_NCDESTROY:
11805 return LISTVIEW_NCDestroy(infoPtr);
11807 case WM_NCPAINT:
11808 return LISTVIEW_NCPaint(infoPtr, (HRGN)wParam);
11810 case WM_NOTIFY:
11811 return LISTVIEW_Notify(infoPtr, (LPNMHDR)lParam);
11813 case WM_NOTIFYFORMAT:
11814 return LISTVIEW_NotifyFormat(infoPtr, (HWND)wParam, (INT)lParam);
11816 case WM_PRINTCLIENT:
11817 return LISTVIEW_PrintClient(infoPtr, (HDC)wParam, (DWORD)lParam);
11819 case WM_PAINT:
11820 return LISTVIEW_WMPaint(infoPtr, (HDC)wParam);
11822 case WM_RBUTTONDBLCLK:
11823 return LISTVIEW_RButtonDblClk(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11825 case WM_RBUTTONDOWN:
11826 return LISTVIEW_RButtonDown(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11828 case WM_SETCURSOR:
11829 return LISTVIEW_SetCursor(infoPtr, wParam, lParam);
11831 case WM_SETFOCUS:
11832 return LISTVIEW_SetFocus(infoPtr, (HWND)wParam);
11834 case WM_SETFONT:
11835 return LISTVIEW_SetFont(infoPtr, (HFONT)wParam, (WORD)lParam);
11837 case WM_SETREDRAW:
11838 return LISTVIEW_SetRedraw(infoPtr, (BOOL)wParam);
11840 case WM_SHOWWINDOW:
11841 return LISTVIEW_ShowWindow(infoPtr, wParam, lParam);
11843 case WM_STYLECHANGED:
11844 return LISTVIEW_StyleChanged(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
11846 case WM_STYLECHANGING:
11847 return LISTVIEW_StyleChanging(wParam, (LPSTYLESTRUCT)lParam);
11849 case WM_SYSCOLORCHANGE:
11850 COMCTL32_RefreshSysColors();
11851 return 0;
11853 /* case WM_TIMER: */
11854 case WM_THEMECHANGED:
11855 return LISTVIEW_ThemeChanged(infoPtr);
11857 case WM_VSCROLL:
11858 return LISTVIEW_VScroll(infoPtr, (INT)LOWORD(wParam), 0);
11860 case WM_MOUSEWHEEL:
11861 if (wParam & (MK_SHIFT | MK_CONTROL))
11862 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
11863 return LISTVIEW_MouseWheel(infoPtr, (short int)HIWORD(wParam));
11865 case WM_WINDOWPOSCHANGED:
11866 if (!(((WINDOWPOS *)lParam)->flags & SWP_NOSIZE))
11868 SetWindowPos(infoPtr->hwndSelf, 0, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOACTIVATE |
11869 SWP_NOZORDER | SWP_NOMOVE | SWP_NOSIZE);
11871 if ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (infoPtr->uView == LV_VIEW_DETAILS))
11873 if (notify_measureitem(infoPtr)) LISTVIEW_InvalidateList(infoPtr);
11875 LISTVIEW_Size(infoPtr, ((WINDOWPOS *)lParam)->cx, ((WINDOWPOS *)lParam)->cy);
11877 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
11879 /* case WM_WININICHANGE: */
11881 default:
11882 if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
11883 ERR("unknown msg %04x, wp %Ix, lp %Ix\n", uMsg, wParam, lParam);
11885 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
11890 /***
11891 * DESCRIPTION:
11892 * Registers the window class.
11894 * PARAMETER(S):
11895 * None
11897 * RETURN:
11898 * None
11900 void LISTVIEW_Register(void)
11902 WNDCLASSW wndClass;
11904 ZeroMemory(&wndClass, sizeof(WNDCLASSW));
11905 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS;
11906 wndClass.lpfnWndProc = LISTVIEW_WindowProc;
11907 wndClass.cbClsExtra = 0;
11908 wndClass.cbWndExtra = sizeof(LISTVIEW_INFO *);
11909 wndClass.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
11910 wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
11911 wndClass.lpszClassName = WC_LISTVIEWW;
11912 RegisterClassW(&wndClass);
11915 /***
11916 * DESCRIPTION:
11917 * Unregisters the window class.
11919 * PARAMETER(S):
11920 * None
11922 * RETURN:
11923 * None
11925 void LISTVIEW_Unregister(void)
11927 UnregisterClassW(WC_LISTVIEWW, NULL);
11930 /***
11931 * DESCRIPTION:
11932 * Handle any WM_COMMAND messages
11934 * PARAMETER(S):
11935 * [I] infoPtr : valid pointer to the listview structure
11936 * [I] wParam : the first message parameter
11937 * [I] lParam : the second message parameter
11939 * RETURN:
11940 * Zero.
11942 static LRESULT LISTVIEW_Command(LISTVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
11945 TRACE("%p, %x, %x, %Ix\n", infoPtr, HIWORD(wParam), LOWORD(wParam), lParam);
11947 if (!infoPtr->hwndEdit) return 0;
11949 switch (HIWORD(wParam))
11951 case EN_UPDATE:
11954 * Adjust the edit window size
11956 WCHAR buffer[1024];
11957 HDC hdc = GetDC(infoPtr->hwndEdit);
11958 HFONT hFont, hOldFont = 0;
11959 RECT rect;
11960 SIZE sz;
11962 if (!infoPtr->hwndEdit || !hdc) return 0;
11963 GetWindowTextW(infoPtr->hwndEdit, buffer, ARRAY_SIZE(buffer));
11964 GetWindowRect(infoPtr->hwndEdit, &rect);
11966 /* Select font to get the right dimension of the string */
11967 hFont = (HFONT)SendMessageW(infoPtr->hwndEdit, WM_GETFONT, 0, 0);
11968 if (hFont)
11970 hOldFont = SelectObject(hdc, hFont);
11973 if (GetTextExtentPoint32W(hdc, buffer, lstrlenW(buffer), &sz))
11975 TEXTMETRICW textMetric;
11977 /* Add Extra spacing for the next character */
11978 GetTextMetricsW(hdc, &textMetric);
11979 sz.cx += (textMetric.tmMaxCharWidth * 2);
11981 SetWindowPos(infoPtr->hwndEdit, NULL, 0, 0, sz.cx,
11982 rect.bottom - rect.top, SWP_DRAWFRAME | SWP_NOMOVE | SWP_NOZORDER);
11984 if (hFont)
11985 SelectObject(hdc, hOldFont);
11987 ReleaseDC(infoPtr->hwndEdit, hdc);
11989 break;
11991 case EN_KILLFOCUS:
11993 if (infoPtr->notify_mask & NOTIFY_MASK_END_LABEL_EDIT)
11994 LISTVIEW_CancelEditLabel(infoPtr);
11995 break;
11998 default:
11999 return SendMessageW (infoPtr->hwndNotify, WM_COMMAND, wParam, lParam);
12002 return 0;