include/mscvpdb.h: Use flexible array members for the rest of structures.
[wine.git] / dlls / comctl32 / listview.c
blobc82473b6205f6251e4ef15f1283b8748378ddf4a
1 /*
2 * Listview control
4 * Copyright 1998, 1999 Eric Kohl
5 * Copyright 1999 Luc Tourangeau
6 * Copyright 2000 Jason Mawdsley
7 * Copyright 2001 CodeWeavers Inc.
8 * Copyright 2002 Dimitrie O. Paun
9 * Copyright 2009-2015 Nikolay Sivov
10 * Copyright 2009 Owen Rudge for CodeWeavers
11 * Copyright 2012-2013 Daniel Jelinski
13 * This library is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU Lesser General Public
15 * License as published by the Free Software Foundation; either
16 * version 2.1 of the License, or (at your option) any later version.
18 * This library is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * Lesser General Public License for more details.
23 * You should have received a copy of the GNU Lesser General Public
24 * License along with this library; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27 * TODO:
29 * Default Message Processing
30 * -- WM_CREATE: create the icon and small icon image lists at this point only if
31 * the LVS_SHAREIMAGELISTS style is not specified.
32 * -- WM_WINDOWPOSCHANGED: arrange the list items if the current view is icon
33 * or small icon and the LVS_AUTOARRANGE style is specified.
34 * -- WM_TIMER
35 * -- WM_WININICHANGE
37 * Features
38 * -- Hot item handling, mouse hovering
39 * -- Workareas support
40 * -- Tilemode support
41 * -- Groups support
43 * Bugs
44 * -- Expand large item in ICON mode when the cursor is flying over the icon or text.
45 * -- Support CustomDraw options for _WIN32_IE >= 0x560 (see NMLVCUSTOMDRAW docs).
46 * -- LVA_SNAPTOGRID not implemented
47 * -- LISTVIEW_ApproximateViewRect partially implemented
48 * -- LISTVIEW_StyleChanged doesn't handle some changes too well
50 * Speedups
51 * -- LISTVIEW_GetNextItem needs to be rewritten. It is currently
52 * linear in the number of items in the list, and this is
53 * unacceptable for large lists.
54 * -- if list is sorted by item text LISTVIEW_InsertItemT could use
55 * binary search to calculate item index (e.g. DPA_Search()).
56 * This requires sorted state to be reliably tracked in item modifiers.
57 * -- we should keep an ordered array of coordinates in iconic mode.
58 * This would allow framing items (iterator_frameditems),
59 * and finding the nearest item (LVFI_NEARESTXY) a lot more efficiently.
61 * Flags
62 * -- LVIF_COLUMNS
63 * -- LVIF_GROUPID
65 * States
66 * -- LVIS_ACTIVATING (not currently supported by comctl32.dll version 6.0)
67 * -- LVIS_DROPHILITED
69 * Styles
70 * -- LVS_NOLABELWRAP
71 * -- LVS_NOSCROLL (see Q137520)
72 * -- LVS_ALIGNTOP
74 * Extended Styles
75 * -- LVS_EX_BORDERSELECT
76 * -- LVS_EX_FLATSB
77 * -- LVS_EX_INFOTIP
78 * -- LVS_EX_LABELTIP
79 * -- LVS_EX_MULTIWORKAREAS
80 * -- LVS_EX_REGIONAL
81 * -- LVS_EX_SIMPLESELECT
82 * -- LVS_EX_TWOCLICKACTIVATE
83 * -- LVS_EX_UNDERLINECOLD
84 * -- LVS_EX_UNDERLINEHOT
86 * Notifications:
87 * -- LVN_BEGINSCROLL, LVN_ENDSCROLL
88 * -- LVN_GETINFOTIP
89 * -- LVN_HOTTRACK
90 * -- LVN_SETDISPINFO
92 * Messages:
93 * -- LVM_ENABLEGROUPVIEW
94 * -- LVM_GETBKIMAGE
95 * -- LVM_GETGROUPINFO, LVM_SETGROUPINFO
96 * -- LVM_GETGROUPMETRICS, LVM_SETGROUPMETRICS
97 * -- LVM_GETINSERTMARK, LVM_SETINSERTMARK
98 * -- LVM_GETINSERTMARKCOLOR, LVM_SETINSERTMARKCOLOR
99 * -- LVM_GETINSERTMARKRECT
100 * -- LVM_GETNUMBEROFWORKAREAS
101 * -- LVM_GETOUTLINECOLOR, LVM_SETOUTLINECOLOR
102 * -- LVM_GETISEARCHSTRINGW, LVM_GETISEARCHSTRINGA
103 * -- LVM_GETTILEINFO, LVM_SETTILEINFO
104 * -- LVM_GETTILEVIEWINFO, LVM_SETTILEVIEWINFO
105 * -- LVM_GETWORKAREAS, LVM_SETWORKAREAS
106 * -- LVM_HASGROUP, LVM_INSERTGROUP, LVM_REMOVEGROUP, LVM_REMOVEALLGROUPS
107 * -- LVM_INSERTGROUPSORTED
108 * -- LVM_INSERTMARKHITTEST
109 * -- LVM_ISGROUPVIEWENABLED
110 * -- LVM_MOVEGROUP
111 * -- LVM_MOVEITEMTOGROUP
112 * -- LVM_SETINFOTIP
113 * -- LVM_SETTILEWIDTH
114 * -- LVM_SORTGROUPS
116 * Macros:
117 * -- ListView_GetHoverTime, ListView_SetHoverTime
118 * -- ListView_GetISearchString
119 * -- ListView_GetNumberOfWorkAreas
120 * -- ListView_GetWorkAreas, ListView_SetWorkAreas
122 * Functions:
123 * -- LVGroupComparE
126 #include <assert.h>
127 #include <ctype.h>
128 #include <string.h>
129 #include <stdlib.h>
130 #include <stdarg.h>
131 #include <stdio.h>
133 #include "windef.h"
134 #include "winbase.h"
135 #include "winnt.h"
136 #include "wingdi.h"
137 #include "winuser.h"
138 #include "winnls.h"
139 #include "commctrl.h"
140 #include "comctl32.h"
141 #include "uxtheme.h"
142 #include "vsstyle.h"
143 #include "shlwapi.h"
145 #include "wine/debug.h"
147 WINE_DEFAULT_DEBUG_CHANNEL(listview);
149 typedef struct tagCOLUMN_INFO
151 RECT rcHeader; /* tracks the header's rectangle */
152 INT fmt; /* same as LVCOLUMN.fmt */
153 INT cxMin;
154 } COLUMN_INFO;
156 typedef struct tagITEMHDR
158 LPWSTR pszText;
159 INT iImage;
160 } ITEMHDR, *LPITEMHDR;
162 typedef struct tagSUBITEM_INFO
164 ITEMHDR hdr;
165 INT iSubItem;
166 } SUBITEM_INFO;
168 typedef struct tagITEM_ID ITEM_ID;
170 typedef struct tagITEM_INFO
172 ITEMHDR hdr;
173 UINT state;
174 LPARAM lParam;
175 INT iIndent;
176 ITEM_ID *id;
177 } ITEM_INFO;
179 struct tagITEM_ID
181 UINT id; /* item id */
182 HDPA item; /* link to item data */
185 typedef struct tagRANGE
187 INT lower;
188 INT upper;
189 } RANGE;
191 typedef struct tagRANGES
193 HDPA hdpa;
194 } *RANGES;
196 typedef struct tagITERATOR
198 INT nItem;
199 INT nSpecial;
200 RANGE range;
201 RANGES ranges;
202 INT index;
203 } ITERATOR;
205 typedef struct tagDELAYED_ITEM_EDIT
207 BOOL fEnabled;
208 INT iItem;
209 } DELAYED_ITEM_EDIT;
211 enum notification_mask
213 NOTIFY_MASK_ITEM_CHANGE = 0x1,
214 NOTIFY_MASK_END_LABEL_EDIT = 0x2,
215 NOTIFY_MASK_UNMASK_ALL = 0xffffffff
218 typedef struct tagLISTVIEW_INFO
220 /* control window */
221 HWND hwndSelf;
222 RECT rcList; /* This rectangle is really the window
223 * client rectangle possibly reduced by the
224 * horizontal scroll bar and/or header - see
225 * LISTVIEW_UpdateSize. This rectangle offset
226 * by the LISTVIEW_GetOrigin value is in
227 * client coordinates */
229 /* notification window */
230 SHORT notifyFormat;
231 HWND hwndNotify;
232 DWORD notify_mask;
233 UINT uCallbackMask;
235 /* tooltips */
236 HWND hwndToolTip;
238 /* items */
239 INT nItemCount; /* the number of items in the list */
240 HDPA hdpaItems; /* array ITEM_INFO pointers */
241 HDPA hdpaItemIds; /* array of ITEM_ID pointers */
242 HDPA hdpaPosX; /* maintains the (X, Y) coordinates of the */
243 HDPA hdpaPosY; /* items in LVS_ICON, and LVS_SMALLICON modes */
244 RANGES selectionRanges;
245 INT nSelectionMark; /* item to start next multiselection from */
246 INT nHotItem;
248 /* columns */
249 HDPA hdpaColumns; /* array of COLUMN_INFO pointers */
250 BOOL colRectsDirty; /* trigger column rectangles requery from header */
251 INT selected_column; /* index for LVM_SETSELECTEDCOLUMN/LVM_GETSELECTEDCOLUMN */
253 /* item metrics */
254 BOOL bNoItemMetrics; /* flags if item metrics are not yet computed */
255 INT nItemHeight;
256 INT nItemWidth;
258 /* style */
259 DWORD dwStyle; /* the cached window GWL_STYLE */
260 DWORD dwLvExStyle; /* extended listview style */
261 DWORD uView; /* current view available through LVM_[G,S]ETVIEW */
263 /* edit item */
264 HWND hwndEdit;
265 WNDPROC EditWndProc;
266 INT nEditLabelItem;
267 DELAYED_ITEM_EDIT itemEdit; /* Pointer to this structure will be the timer ID */
269 /* icons */
270 HIMAGELIST himlNormal;
271 HIMAGELIST himlSmall;
272 HIMAGELIST himlState;
273 SIZE iconSize;
274 BOOL autoSpacing;
275 SIZE iconSpacing;
276 SIZE iconStateSize;
277 POINT currIconPos; /* this is the position next icon will be placed */
279 /* header */
280 HWND hwndHeader;
281 INT xTrackLine; /* The x coefficient of the track line or -1 if none */
283 /* marquee selection */
284 BOOL bMarqueeSelect; /* marquee selection/highlight underway */
285 BOOL bScrolling;
286 RECT marqueeRect; /* absolute coordinates of marquee selection */
287 RECT marqueeDrawRect; /* relative coordinates for drawing marquee */
288 POINT marqueeOrigin; /* absolute coordinates of marquee click origin */
290 /* focus drawing */
291 BOOL bFocus; /* control has focus */
292 INT nFocusedItem;
293 RECT rcFocus; /* focus bounds */
295 /* colors */
296 HBRUSH hBkBrush;
297 COLORREF clrBk;
298 COLORREF clrText;
299 COLORREF clrTextBk;
300 HBITMAP hBkBitmap;
302 /* font */
303 HFONT hDefaultFont;
304 HFONT hFont;
305 INT ntmHeight; /* Some cached metrics of the font used */
306 INT ntmMaxCharWidth; /* by the listview to draw items */
307 INT nEllipsisWidth;
309 /* mouse operation */
310 BOOL bLButtonDown;
311 BOOL bDragging;
312 POINT ptClickPos; /* point where the user clicked */
313 INT nLButtonDownItem; /* tracks item to reset multiselection on WM_LBUTTONUP */
314 DWORD dwHoverTime;
315 HCURSOR hHotCursor;
316 INT cWheelRemainder;
318 /* keyboard operation */
319 DWORD lastKeyPressTimestamp;
320 WPARAM charCode;
321 INT nSearchParamLength;
322 WCHAR szSearchParam[ MAX_PATH ];
324 /* painting */
325 BOOL bIsDrawing; /* Drawing in progress */
326 INT nMeasureItemHeight; /* WM_MEASUREITEM result */
327 BOOL redraw; /* WM_SETREDRAW switch */
329 /* misc */
330 DWORD iVersion; /* CCM_[G,S]ETVERSION */
331 } LISTVIEW_INFO;
334 * constants
336 /* How many we debug buffer to allocate */
337 #define DEBUG_BUFFERS 20
338 /* The size of a single debug buffer */
339 #define DEBUG_BUFFER_SIZE 256
341 /* Internal interface to LISTVIEW_HScroll and LISTVIEW_VScroll */
342 #define SB_INTERNAL -1
344 /* maximum size of a label */
345 #define DISP_TEXT_SIZE 260
347 /* padding for items in list and small icon display modes */
348 #define WIDTH_PADDING 12
350 /* padding for items in list, report and small icon display modes */
351 #define HEIGHT_PADDING 1
353 /* offset of items in report display mode */
354 #define REPORT_MARGINX 2
356 /* padding for icon in large icon display mode
357 * ICON_TOP_PADDING_NOTHITABLE - space between top of box and area
358 * that HITTEST will see.
359 * ICON_TOP_PADDING_HITABLE - spacing between above and icon.
360 * ICON_TOP_PADDING - sum of the two above.
361 * ICON_BOTTOM_PADDING - between bottom of icon and top of text
362 * LABEL_HOR_PADDING - between text and sides of box
363 * LABEL_VERT_PADDING - between bottom of text and end of box
365 * ICON_LR_PADDING - additional width above icon size.
366 * ICON_LR_HALF - half of the above value
368 #define ICON_TOP_PADDING_NOTHITABLE 2
369 #define ICON_TOP_PADDING_HITABLE 2
370 #define ICON_TOP_PADDING (ICON_TOP_PADDING_NOTHITABLE + ICON_TOP_PADDING_HITABLE)
371 #define ICON_BOTTOM_PADDING 4
372 #define LABEL_HOR_PADDING 5
373 #define LABEL_VERT_PADDING 7
374 #define ICON_LR_PADDING 16
375 #define ICON_LR_HALF (ICON_LR_PADDING/2)
377 /* default label width for items in list and small icon display modes */
378 #define DEFAULT_LABEL_WIDTH 40
379 /* maximum select rectangle width for empty text item in LV_VIEW_DETAILS */
380 #define MAX_EMPTYTEXT_SELECT_WIDTH 80
382 /* default column width for items in list display mode */
383 #define DEFAULT_COLUMN_WIDTH 128
385 /* Size of "line" scroll for V & H scrolls */
386 #define LISTVIEW_SCROLL_ICON_LINE_SIZE 37
388 /* Padding between image and label */
389 #define IMAGE_PADDING 2
391 /* Padding behind the label */
392 #define TRAILING_LABEL_PADDING 12
393 #define TRAILING_HEADER_PADDING 11
395 /* Border for the icon caption */
396 #define CAPTION_BORDER 2
398 /* Standard DrawText flags */
399 #define LV_ML_DT_FLAGS (DT_TOP | DT_NOPREFIX | DT_EDITCONTROL | DT_CENTER | DT_WORDBREAK | DT_WORD_ELLIPSIS | DT_END_ELLIPSIS)
400 #define LV_FL_DT_FLAGS (DT_TOP | DT_NOPREFIX | DT_EDITCONTROL | DT_CENTER | DT_WORDBREAK | DT_NOCLIP)
401 #define LV_SL_DT_FLAGS (DT_VCENTER | DT_NOPREFIX | DT_EDITCONTROL | DT_SINGLELINE | DT_WORD_ELLIPSIS | DT_END_ELLIPSIS)
403 /* Image index from state */
404 #define STATEIMAGEINDEX(x) (((x) & LVIS_STATEIMAGEMASK) >> 12)
406 /* The time in milliseconds to reset the search in the list */
407 #define KEY_DELAY 450
409 /* Dump the LISTVIEW_INFO structure to the debug channel */
410 #define LISTVIEW_DUMP(iP) do { \
411 TRACE("hwndSelf=%p, clrBk=%#lx, clrText=%#lx, clrTextBk=%#lx, ItemHeight=%d, ItemWidth=%d, Style=%#lx\n", \
412 iP->hwndSelf, iP->clrBk, iP->clrText, iP->clrTextBk, \
413 iP->nItemHeight, iP->nItemWidth, iP->dwStyle); \
414 TRACE("hwndSelf=%p, himlNor=%p, himlSml=%p, himlState=%p, Focused=%d, Hot=%d, exStyle=%#lx, Focus=%d\n", \
415 iP->hwndSelf, iP->himlNormal, iP->himlSmall, iP->himlState, \
416 iP->nFocusedItem, iP->nHotItem, iP->dwLvExStyle, iP->bFocus ); \
417 TRACE("hwndSelf=%p, ntmH=%d, icSz.cx=%ld, icSz.cy=%ld, icSp.cx=%ld, icSp.cy=%ld, notifyFmt=%d\n", \
418 iP->hwndSelf, iP->ntmHeight, iP->iconSize.cx, iP->iconSize.cy, \
419 iP->iconSpacing.cx, iP->iconSpacing.cy, iP->notifyFormat); \
420 TRACE("hwndSelf=%p, rcList=%s\n", iP->hwndSelf, wine_dbgstr_rect(&iP->rcList)); \
421 } while(0)
423 static const WCHAR themeClass[] = L"ListView";
426 * forward declarations
428 static BOOL LISTVIEW_GetItemT(const LISTVIEW_INFO *, LPLVITEMW, BOOL);
429 static void LISTVIEW_GetItemBox(const LISTVIEW_INFO *, INT, LPRECT);
430 static void LISTVIEW_GetItemOrigin(const LISTVIEW_INFO *, INT, LPPOINT);
431 static BOOL LISTVIEW_GetItemPosition(const LISTVIEW_INFO *, INT, LPPOINT);
432 static BOOL LISTVIEW_GetItemRect(const LISTVIEW_INFO *, INT, LPRECT);
433 static void LISTVIEW_GetOrigin(const LISTVIEW_INFO *, LPPOINT);
434 static BOOL LISTVIEW_GetViewRect(const LISTVIEW_INFO *, LPRECT);
435 static void LISTVIEW_UpdateSize(LISTVIEW_INFO *);
436 static LRESULT LISTVIEW_Command(LISTVIEW_INFO *, WPARAM, LPARAM);
437 static INT LISTVIEW_GetStringWidthT(const LISTVIEW_INFO *, LPCWSTR, BOOL);
438 static BOOL LISTVIEW_KeySelection(LISTVIEW_INFO *, INT, BOOL);
439 static UINT LISTVIEW_GetItemState(const LISTVIEW_INFO *, INT, UINT);
440 static BOOL LISTVIEW_SetItemState(LISTVIEW_INFO *, INT, const LVITEMW *);
441 static VOID LISTVIEW_SetOwnerDataState(LISTVIEW_INFO *, INT, INT, const LVITEMW *);
442 static LRESULT LISTVIEW_VScroll(LISTVIEW_INFO *, INT, INT);
443 static LRESULT LISTVIEW_HScroll(LISTVIEW_INFO *, INT, INT);
444 static BOOL LISTVIEW_EnsureVisible(LISTVIEW_INFO *, INT, BOOL);
445 static HIMAGELIST LISTVIEW_SetImageList(LISTVIEW_INFO *, INT, HIMAGELIST);
446 static INT LISTVIEW_HitTest(const LISTVIEW_INFO *, LPLVHITTESTINFO, BOOL, BOOL);
447 static BOOL LISTVIEW_EndEditLabelT(LISTVIEW_INFO *, BOOL, BOOL);
448 static BOOL LISTVIEW_Scroll(LISTVIEW_INFO *, INT, INT);
450 /******** Text handling functions *************************************/
452 /* A text pointer is either NULL, LPSTR_TEXTCALLBACK, or points to a
453 * text string. The string may be ANSI or Unicode, in which case
454 * the boolean isW tells us the type of the string.
456 * The name of the function tell what type of strings it expects:
457 * W: Unicode, T: ANSI/Unicode - function of isW
460 static inline BOOL is_text(LPCWSTR text)
462 return text != NULL && text != LPSTR_TEXTCALLBACKW;
465 static inline int textlenT(LPCWSTR text, BOOL isW)
467 return !is_text(text) ? 0 :
468 isW ? lstrlenW(text) : lstrlenA((LPCSTR)text);
471 static inline void textcpynT(LPWSTR dest, BOOL isDestW, LPCWSTR src, BOOL isSrcW, INT max)
473 if (isDestW)
474 if (isSrcW) lstrcpynW(dest, src, max);
475 else MultiByteToWideChar(CP_ACP, 0, (LPCSTR)src, -1, dest, max);
476 else
477 if (isSrcW) WideCharToMultiByte(CP_ACP, 0, src, -1, (LPSTR)dest, max, NULL, NULL);
478 else lstrcpynA((LPSTR)dest, (LPCSTR)src, max);
481 static inline LPWSTR textdupTtoW(LPCWSTR text, BOOL isW)
483 LPWSTR wstr = (LPWSTR)text;
485 if (!isW && is_text(text))
487 INT len = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)text, -1, NULL, 0);
488 wstr = Alloc(len * sizeof(WCHAR));
489 if (wstr) MultiByteToWideChar(CP_ACP, 0, (LPCSTR)text, -1, wstr, len);
491 TRACE(" wstr=%s\n", text == LPSTR_TEXTCALLBACKW ? "(callback)" : debugstr_w(wstr));
492 return wstr;
495 static inline void textfreeT(LPWSTR wstr, BOOL isW)
497 if (!isW && is_text(wstr)) Free (wstr);
501 * dest is a pointer to a Unicode string
502 * src is a pointer to a string (Unicode if isW, ANSI if !isW)
504 static BOOL textsetptrT(LPWSTR *dest, LPCWSTR src, BOOL isW)
506 BOOL bResult = TRUE;
508 if (src == LPSTR_TEXTCALLBACKW)
510 if (is_text(*dest)) Free(*dest);
511 *dest = LPSTR_TEXTCALLBACKW;
513 else
515 LPWSTR pszText = textdupTtoW(src, isW);
516 if (*dest == LPSTR_TEXTCALLBACKW) *dest = NULL;
517 bResult = Str_SetPtrW(dest, pszText);
518 textfreeT(pszText, isW);
520 return bResult;
524 * compares a Unicode to a Unicode/ANSI text string
526 static inline int textcmpWT(LPCWSTR aw, LPCWSTR bt, BOOL isW)
528 if (!aw) return bt ? -1 : 0;
529 if (!bt) return 1;
530 if (aw == LPSTR_TEXTCALLBACKW)
531 return bt == LPSTR_TEXTCALLBACKW ? 1 : -1;
532 if (bt != LPSTR_TEXTCALLBACKW)
534 LPWSTR bw = textdupTtoW(bt, isW);
535 int r = bw ? lstrcmpW(aw, bw) : 1;
536 textfreeT(bw, isW);
537 return r;
540 return 1;
543 /******** Debugging functions *****************************************/
545 static inline LPCSTR debugtext_t(LPCWSTR text, BOOL isW)
547 if (text == LPSTR_TEXTCALLBACKW) return "(callback)";
548 return isW ? debugstr_w(text) : debugstr_a((LPCSTR)text);
551 static inline LPCSTR debugtext_tn(LPCWSTR text, BOOL isW, INT n)
553 if (text == LPSTR_TEXTCALLBACKW) return "(callback)";
554 n = min(textlenT(text, isW), n);
555 return isW ? debugstr_wn(text, n) : debugstr_an((LPCSTR)text, n);
558 static char* debug_getbuf(void)
560 static int index = 0;
561 static char buffers[DEBUG_BUFFERS][DEBUG_BUFFER_SIZE];
562 return buffers[index++ % DEBUG_BUFFERS];
565 static inline const char* debugrange(const RANGE *lprng)
567 if (!lprng) return "(null)";
568 return wine_dbg_sprintf("[%d, %d]", lprng->lower, lprng->upper);
571 static const char* debugscrollinfo(const SCROLLINFO *pScrollInfo)
573 char* buf = debug_getbuf(), *text = buf;
574 int len, size = DEBUG_BUFFER_SIZE;
576 if (pScrollInfo == NULL) return "(null)";
577 len = snprintf(buf, size, "{cbSize=%u, ", pScrollInfo->cbSize);
578 if (len == -1) goto end;
579 buf += len; size -= len;
580 if (pScrollInfo->fMask & SIF_RANGE)
581 len = snprintf(buf, size, "nMin=%d, nMax=%d, ", pScrollInfo->nMin, pScrollInfo->nMax);
582 else len = 0;
583 if (len == -1) goto end;
584 buf += len; size -= len;
585 if (pScrollInfo->fMask & SIF_PAGE)
586 len = snprintf(buf, size, "nPage=%u, ", pScrollInfo->nPage);
587 else len = 0;
588 if (len == -1) goto end;
589 buf += len; size -= len;
590 if (pScrollInfo->fMask & SIF_POS)
591 len = snprintf(buf, size, "nPos=%d, ", pScrollInfo->nPos);
592 else len = 0;
593 if (len == -1) goto end;
594 buf += len; size -= len;
595 if (pScrollInfo->fMask & SIF_TRACKPOS)
596 len = snprintf(buf, size, "nTrackPos=%d, ", pScrollInfo->nTrackPos);
597 else len = 0;
598 if (len == -1) goto end;
599 buf += len;
600 goto undo;
601 end:
602 buf = text + strlen(text);
603 undo:
604 if (buf - text > 2) { buf[-2] = '}'; buf[-1] = 0; }
605 return text;
608 static const char* debugnmlistview(const NMLISTVIEW *plvnm)
610 if (!plvnm) return "(null)";
611 return wine_dbg_sprintf("iItem=%d, iSubItem=%d, uNewState=0x%x,"
612 " uOldState=0x%x, uChanged=0x%x, ptAction=%s, lParam=%Id",
613 plvnm->iItem, plvnm->iSubItem, plvnm->uNewState, plvnm->uOldState,
614 plvnm->uChanged, wine_dbgstr_point(&plvnm->ptAction), plvnm->lParam);
617 static const char* debuglvitem_t(const LVITEMW *lpLVItem, BOOL isW)
619 char* buf = debug_getbuf(), *text = buf;
620 int len, size = DEBUG_BUFFER_SIZE;
622 if (lpLVItem == NULL) return "(null)";
623 len = snprintf(buf, size, "{iItem=%d, iSubItem=%d, ", lpLVItem->iItem, lpLVItem->iSubItem);
624 if (len == -1) goto end;
625 buf += len; size -= len;
626 if (lpLVItem->mask & LVIF_STATE)
627 len = snprintf(buf, size, "state=%x, stateMask=%x, ", lpLVItem->state, lpLVItem->stateMask);
628 else len = 0;
629 if (len == -1) goto end;
630 buf += len; size -= len;
631 if (lpLVItem->mask & LVIF_TEXT)
632 len = snprintf(buf, size, "pszText=%s, cchTextMax=%d, ", debugtext_tn(lpLVItem->pszText, isW, 80), lpLVItem->cchTextMax);
633 else len = 0;
634 if (len == -1) goto end;
635 buf += len; size -= len;
636 if (lpLVItem->mask & LVIF_IMAGE)
637 len = snprintf(buf, size, "iImage=%d, ", lpLVItem->iImage);
638 else len = 0;
639 if (len == -1) goto end;
640 buf += len; size -= len;
641 if (lpLVItem->mask & LVIF_PARAM)
642 len = snprintf(buf, size, "lParam=%Ix, ", lpLVItem->lParam);
643 else len = 0;
644 if (len == -1) goto end;
645 buf += len; size -= len;
646 if (lpLVItem->mask & LVIF_INDENT)
647 len = snprintf(buf, size, "iIndent=%d, ", lpLVItem->iIndent);
648 else len = 0;
649 if (len == -1) goto end;
650 buf += len;
651 goto undo;
652 end:
653 buf = text + strlen(text);
654 undo:
655 if (buf - text > 2) { buf[-2] = '}'; buf[-1] = 0; }
656 return text;
659 static const char* debuglvcolumn_t(const LVCOLUMNW *lpColumn, BOOL isW)
661 char* buf = debug_getbuf(), *text = buf;
662 int len, size = DEBUG_BUFFER_SIZE;
664 if (lpColumn == NULL) return "(null)";
665 len = snprintf(buf, size, "{");
666 if (len == -1) goto end;
667 buf += len; size -= len;
668 if (lpColumn->mask & LVCF_SUBITEM)
669 len = snprintf(buf, size, "iSubItem=%d, ", lpColumn->iSubItem);
670 else len = 0;
671 if (len == -1) goto end;
672 buf += len; size -= len;
673 if (lpColumn->mask & LVCF_FMT)
674 len = snprintf(buf, size, "fmt=%x, ", lpColumn->fmt);
675 else len = 0;
676 if (len == -1) goto end;
677 buf += len; size -= len;
678 if (lpColumn->mask & LVCF_WIDTH)
679 len = snprintf(buf, size, "cx=%d, ", lpColumn->cx);
680 else len = 0;
681 if (len == -1) goto end;
682 buf += len; size -= len;
683 if (lpColumn->mask & LVCF_TEXT)
684 len = snprintf(buf, size, "pszText=%s, cchTextMax=%d, ", debugtext_tn(lpColumn->pszText, isW, 80), lpColumn->cchTextMax);
685 else len = 0;
686 if (len == -1) goto end;
687 buf += len; size -= len;
688 if (lpColumn->mask & LVCF_IMAGE)
689 len = snprintf(buf, size, "iImage=%d, ", lpColumn->iImage);
690 else len = 0;
691 if (len == -1) goto end;
692 buf += len; size -= len;
693 if (lpColumn->mask & LVCF_ORDER)
694 len = snprintf(buf, size, "iOrder=%d, ", lpColumn->iOrder);
695 else len = 0;
696 if (len == -1) goto end;
697 buf += len;
698 goto undo;
699 end:
700 buf = text + strlen(text);
701 undo:
702 if (buf - text > 2) { buf[-2] = '}'; buf[-1] = 0; }
703 return text;
706 static const char* debuglvhittestinfo(const LVHITTESTINFO *lpht)
708 if (!lpht) return "(null)";
710 return wine_dbg_sprintf("{pt=%s, flags=0x%x, iItem=%d, iSubItem=%d}",
711 wine_dbgstr_point(&lpht->pt), lpht->flags, lpht->iItem, lpht->iSubItem);
714 /* Return the corresponding text for a given scroll value */
715 static inline LPCSTR debugscrollcode(int nScrollCode)
717 switch(nScrollCode)
719 case SB_LINELEFT: return "SB_LINELEFT";
720 case SB_LINERIGHT: return "SB_LINERIGHT";
721 case SB_PAGELEFT: return "SB_PAGELEFT";
722 case SB_PAGERIGHT: return "SB_PAGERIGHT";
723 case SB_THUMBPOSITION: return "SB_THUMBPOSITION";
724 case SB_THUMBTRACK: return "SB_THUMBTRACK";
725 case SB_ENDSCROLL: return "SB_ENDSCROLL";
726 case SB_INTERNAL: return "SB_INTERNAL";
727 default: return "unknown";
732 /******** Notification functions ************************************/
734 static int get_ansi_notification(UINT unicodeNotificationCode)
736 switch (unicodeNotificationCode)
738 case LVN_BEGINLABELEDITA:
739 case LVN_BEGINLABELEDITW: return LVN_BEGINLABELEDITA;
740 case LVN_ENDLABELEDITA:
741 case LVN_ENDLABELEDITW: return LVN_ENDLABELEDITA;
742 case LVN_GETDISPINFOA:
743 case LVN_GETDISPINFOW: return LVN_GETDISPINFOA;
744 case LVN_SETDISPINFOA:
745 case LVN_SETDISPINFOW: return LVN_SETDISPINFOA;
746 case LVN_ODFINDITEMA:
747 case LVN_ODFINDITEMW: return LVN_ODFINDITEMA;
748 case LVN_GETINFOTIPA:
749 case LVN_GETINFOTIPW: return LVN_GETINFOTIPA;
750 /* header forwards */
751 case HDN_TRACKA:
752 case HDN_TRACKW: return HDN_TRACKA;
753 case HDN_ENDTRACKA:
754 case HDN_ENDTRACKW: return HDN_ENDTRACKA;
755 case HDN_BEGINDRAG: return HDN_BEGINDRAG;
756 case HDN_ENDDRAG: return HDN_ENDDRAG;
757 case HDN_ITEMCHANGINGA:
758 case HDN_ITEMCHANGINGW: return HDN_ITEMCHANGINGA;
759 case HDN_ITEMCHANGEDA:
760 case HDN_ITEMCHANGEDW: return HDN_ITEMCHANGEDA;
761 case HDN_ITEMCLICKA:
762 case HDN_ITEMCLICKW: return HDN_ITEMCLICKA;
763 case HDN_DIVIDERDBLCLICKA:
764 case HDN_DIVIDERDBLCLICKW: return HDN_DIVIDERDBLCLICKA;
765 default: break;
767 FIXME("unknown notification %x\n", unicodeNotificationCode);
768 return unicodeNotificationCode;
771 /* forwards header notifications to listview parent */
772 static LRESULT notify_forward_header(const LISTVIEW_INFO *infoPtr, NMHEADERW *lpnmhW)
774 LPCWSTR text = NULL, filter = NULL;
775 LRESULT ret;
776 NMHEADERA *lpnmh = (NMHEADERA*) lpnmhW;
778 /* on unicode format exit earlier */
779 if (infoPtr->notifyFormat == NFR_UNICODE)
780 return SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, lpnmh->hdr.idFrom,
781 (LPARAM)lpnmh);
783 /* header always supplies unicode notifications,
784 all we have to do is to convert strings to ANSI */
785 if (lpnmh->pitem)
787 /* convert item text */
788 if (lpnmh->pitem->mask & HDI_TEXT)
790 text = (LPCWSTR)lpnmh->pitem->pszText;
791 lpnmh->pitem->pszText = NULL;
792 Str_SetPtrWtoA(&lpnmh->pitem->pszText, text);
794 /* convert filter text */
795 if ((lpnmh->pitem->mask & HDI_FILTER) && (lpnmh->pitem->type == HDFT_ISSTRING) &&
796 lpnmh->pitem->pvFilter)
798 filter = (LPCWSTR)((HD_TEXTFILTERA*)lpnmh->pitem->pvFilter)->pszText;
799 ((HD_TEXTFILTERA*)lpnmh->pitem->pvFilter)->pszText = NULL;
800 Str_SetPtrWtoA(&((HD_TEXTFILTERA*)lpnmh->pitem->pvFilter)->pszText, filter);
803 lpnmh->hdr.code = get_ansi_notification(lpnmh->hdr.code);
805 ret = SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, lpnmh->hdr.idFrom,
806 (LPARAM)lpnmh);
808 /* cleanup */
809 if(text)
811 Free(lpnmh->pitem->pszText);
812 lpnmh->pitem->pszText = (LPSTR)text;
814 if(filter)
816 Free(((HD_TEXTFILTERA*)lpnmh->pitem->pvFilter)->pszText);
817 ((HD_TEXTFILTERA*)lpnmh->pitem->pvFilter)->pszText = (LPSTR)filter;
820 return ret;
823 static LRESULT notify_hdr(const LISTVIEW_INFO *infoPtr, INT code, LPNMHDR pnmh)
825 LRESULT result;
827 TRACE("(code=%d)\n", code);
829 pnmh->hwndFrom = infoPtr->hwndSelf;
830 pnmh->idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
831 pnmh->code = code;
832 result = SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, pnmh->idFrom, (LPARAM)pnmh);
834 TRACE(" <= %Id\n", result);
836 return result;
839 static inline BOOL notify(const LISTVIEW_INFO *infoPtr, INT code)
841 NMHDR nmh;
842 HWND hwnd = infoPtr->hwndSelf;
843 notify_hdr(infoPtr, code, &nmh);
844 return IsWindow(hwnd);
847 static inline void notify_itemactivate(const LISTVIEW_INFO *infoPtr, const LVHITTESTINFO *htInfo)
849 NMITEMACTIVATE nmia;
850 LVITEMW item;
852 nmia.uNewState = 0;
853 nmia.uOldState = 0;
854 nmia.uChanged = 0;
855 nmia.uKeyFlags = 0;
857 item.mask = LVIF_PARAM|LVIF_STATE;
858 item.iItem = htInfo->iItem;
859 item.iSubItem = 0;
860 item.stateMask = (UINT)-1;
861 if (LISTVIEW_GetItemT(infoPtr, &item, TRUE)) {
862 nmia.lParam = item.lParam;
863 nmia.uOldState = item.state;
864 nmia.uNewState = item.state | LVIS_ACTIVATING;
865 nmia.uChanged = LVIF_STATE;
868 nmia.iItem = htInfo->iItem;
869 nmia.iSubItem = htInfo->iSubItem;
870 nmia.ptAction = htInfo->pt;
872 if (GetKeyState(VK_SHIFT) & 0x8000) nmia.uKeyFlags |= LVKF_SHIFT;
873 if (GetKeyState(VK_CONTROL) & 0x8000) nmia.uKeyFlags |= LVKF_CONTROL;
874 if (GetKeyState(VK_MENU) & 0x8000) nmia.uKeyFlags |= LVKF_ALT;
876 notify_hdr(infoPtr, LVN_ITEMACTIVATE, (LPNMHDR)&nmia);
879 static inline LRESULT notify_listview(const LISTVIEW_INFO *infoPtr, INT code, LPNMLISTVIEW plvnm)
881 TRACE("(code=%d, plvnm=%s)\n", code, debugnmlistview(plvnm));
882 return notify_hdr(infoPtr, code, (LPNMHDR)plvnm);
885 /* Handles NM_DBLCLK, NM_CLICK, NM_RDBLCLK, NM_RCLICK. Only NM_RCLICK return value is used. */
886 static BOOL notify_click(const LISTVIEW_INFO *infoPtr, INT code, const LVHITTESTINFO *lvht)
888 NMITEMACTIVATE nmia;
889 LVITEMW item;
890 HWND hwnd = infoPtr->hwndSelf;
891 LRESULT ret;
893 TRACE("code=%d, lvht=%s\n", code, debuglvhittestinfo(lvht));
894 ZeroMemory(&nmia, sizeof(nmia));
895 nmia.iItem = lvht->iItem;
896 nmia.iSubItem = lvht->iSubItem;
897 nmia.ptAction = lvht->pt;
898 item.mask = LVIF_PARAM;
899 item.iItem = lvht->iItem;
900 item.iSubItem = 0;
901 if (LISTVIEW_GetItemT(infoPtr, &item, TRUE)) nmia.lParam = item.lParam;
902 ret = notify_hdr(infoPtr, code, (NMHDR*)&nmia);
903 return IsWindow(hwnd) && (code == NM_RCLICK ? !ret : TRUE);
906 static BOOL notify_deleteitem(const LISTVIEW_INFO *infoPtr, INT nItem)
908 NMLISTVIEW nmlv;
909 LVITEMW item;
910 HWND hwnd = infoPtr->hwndSelf;
912 ZeroMemory(&nmlv, sizeof (NMLISTVIEW));
913 nmlv.iItem = nItem;
914 item.mask = LVIF_PARAM;
915 item.iItem = nItem;
916 item.iSubItem = 0;
917 if (LISTVIEW_GetItemT(infoPtr, &item, TRUE)) nmlv.lParam = item.lParam;
918 notify_listview(infoPtr, LVN_DELETEITEM, &nmlv);
919 return IsWindow(hwnd);
923 Send notification. depends on dispinfoW having same
924 structure as dispinfoA.
925 infoPtr : listview struct
926 code : *Unicode* notification code
927 pdi : dispinfo structure (can be unicode or ansi)
928 isW : TRUE if dispinfo is Unicode
930 static BOOL notify_dispinfoT(const LISTVIEW_INFO *infoPtr, UINT code, LPNMLVDISPINFOW pdi, BOOL isW)
932 INT length = 0, ret_length;
933 LPWSTR buffer = NULL, ret_text;
934 BOOL return_ansi = FALSE;
935 BOOL return_unicode = FALSE;
936 BOOL ret;
938 if ((pdi->item.mask & LVIF_TEXT) && is_text(pdi->item.pszText))
940 return_unicode = ( isW && infoPtr->notifyFormat == NFR_ANSI);
941 return_ansi = (!isW && infoPtr->notifyFormat == NFR_UNICODE);
944 ret_length = pdi->item.cchTextMax;
945 ret_text = pdi->item.pszText;
947 if (return_unicode || return_ansi)
949 if (code != LVN_GETDISPINFOW)
951 length = return_ansi ?
952 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pdi->item.pszText, -1, NULL, 0):
953 WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, NULL, 0, NULL, NULL);
955 else
957 length = pdi->item.cchTextMax;
958 *pdi->item.pszText = 0; /* make sure we don't process garbage */
961 buffer = Alloc( length * (return_ansi ? sizeof(WCHAR) : sizeof(CHAR)) );
962 if (!buffer) return FALSE;
964 if (return_ansi)
965 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pdi->item.pszText, -1,
966 buffer, length);
967 else
968 WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, (LPSTR) buffer,
969 length, NULL, NULL);
971 pdi->item.pszText = buffer;
972 pdi->item.cchTextMax = length;
975 if (infoPtr->notifyFormat == NFR_ANSI)
976 code = get_ansi_notification(code);
978 TRACE(" pdi->item=%s\n", debuglvitem_t(&pdi->item, infoPtr->notifyFormat != NFR_ANSI));
979 ret = notify_hdr(infoPtr, code, &pdi->hdr);
980 TRACE(" resulting code=%d\n", pdi->hdr.code);
982 if (return_ansi || return_unicode)
984 if (return_ansi && (pdi->hdr.code == LVN_GETDISPINFOA))
986 strcpy((char*)ret_text, (char*)pdi->item.pszText);
988 else if (return_unicode && (pdi->hdr.code == LVN_GETDISPINFOW))
990 lstrcpyW(ret_text, pdi->item.pszText);
992 else if (return_ansi) /* note : pointer can be changed by app ! */
994 WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, (LPSTR) ret_text,
995 ret_length, NULL, NULL);
997 else
998 MultiByteToWideChar(CP_ACP, 0, (LPSTR) pdi->item.pszText, -1,
999 ret_text, ret_length);
1001 pdi->item.pszText = ret_text; /* restores our buffer */
1002 pdi->item.cchTextMax = ret_length;
1004 Free(buffer);
1005 return ret;
1008 /* if dispinfo holder changed notification code then convert */
1009 if (!isW && (pdi->hdr.code == LVN_GETDISPINFOW) && (pdi->item.mask & LVIF_TEXT))
1011 length = WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, NULL, 0, NULL, NULL);
1013 buffer = Alloc(length * sizeof(CHAR));
1014 if (!buffer) return FALSE;
1016 WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, (LPSTR) buffer,
1017 ret_length, NULL, NULL);
1019 strcpy((LPSTR)pdi->item.pszText, (LPSTR)buffer);
1020 Free(buffer);
1023 return ret;
1026 static void customdraw_fill(NMLVCUSTOMDRAW *lpnmlvcd, const LISTVIEW_INFO *infoPtr, HDC hdc,
1027 const RECT *rcBounds, const LVITEMW *lplvItem)
1029 ZeroMemory(lpnmlvcd, sizeof(NMLVCUSTOMDRAW));
1030 lpnmlvcd->nmcd.hdc = hdc;
1031 lpnmlvcd->nmcd.rc = *rcBounds;
1032 lpnmlvcd->clrTextBk = infoPtr->clrTextBk;
1033 lpnmlvcd->clrText = infoPtr->clrText;
1034 if (!lplvItem) return;
1035 lpnmlvcd->nmcd.dwItemSpec = lplvItem->iItem + 1;
1036 lpnmlvcd->iSubItem = lplvItem->iSubItem;
1037 if (lplvItem->state & LVIS_SELECTED) lpnmlvcd->nmcd.uItemState |= CDIS_SELECTED;
1038 if (lplvItem->state & LVIS_FOCUSED) lpnmlvcd->nmcd.uItemState |= CDIS_FOCUS;
1039 if (lplvItem->iItem == infoPtr->nHotItem) lpnmlvcd->nmcd.uItemState |= CDIS_HOT;
1040 lpnmlvcd->nmcd.lItemlParam = lplvItem->lParam;
1043 static inline DWORD notify_customdraw (const LISTVIEW_INFO *infoPtr, DWORD dwDrawStage, NMLVCUSTOMDRAW *lpnmlvcd)
1045 BOOL isForItem = (lpnmlvcd->nmcd.dwItemSpec != 0);
1046 DWORD result;
1048 lpnmlvcd->nmcd.dwDrawStage = dwDrawStage;
1049 if (isForItem) lpnmlvcd->nmcd.dwDrawStage |= CDDS_ITEM;
1050 if (lpnmlvcd->iSubItem) lpnmlvcd->nmcd.dwDrawStage |= CDDS_SUBITEM;
1051 if (isForItem) lpnmlvcd->nmcd.dwItemSpec--;
1052 result = notify_hdr(infoPtr, NM_CUSTOMDRAW, &lpnmlvcd->nmcd.hdr);
1053 if (isForItem) lpnmlvcd->nmcd.dwItemSpec++;
1054 return result;
1057 static void prepaint_setup (const LISTVIEW_INFO *infoPtr, HDC hdc, const NMLVCUSTOMDRAW *cd, BOOL SubItem)
1059 COLORREF backcolor, textcolor;
1061 backcolor = cd->clrTextBk;
1062 textcolor = cd->clrText;
1064 /* apparently, for selected items, we have to override the returned values */
1065 if (!SubItem)
1067 if (cd->nmcd.uItemState & CDIS_SELECTED)
1069 if (infoPtr->bFocus)
1071 backcolor = comctl32_color.clrHighlight;
1072 textcolor = comctl32_color.clrHighlightText;
1074 else if (infoPtr->dwStyle & LVS_SHOWSELALWAYS)
1076 backcolor = comctl32_color.clr3dFace;
1077 textcolor = comctl32_color.clrBtnText;
1082 if (backcolor == CLR_DEFAULT)
1083 backcolor = comctl32_color.clrWindow;
1084 if (textcolor == CLR_DEFAULT)
1085 textcolor = comctl32_color.clrWindowText;
1087 /* Set the text attributes */
1088 if (backcolor != CLR_NONE)
1090 SetBkMode(hdc, OPAQUE);
1091 SetBkColor(hdc, backcolor);
1093 else
1094 SetBkMode(hdc, TRANSPARENT);
1095 SetTextColor(hdc, textcolor);
1098 static inline DWORD notify_postpaint (const LISTVIEW_INFO *infoPtr, NMLVCUSTOMDRAW *lpnmlvcd)
1100 return notify_customdraw(infoPtr, CDDS_POSTPAINT, lpnmlvcd);
1103 /* returns TRUE when repaint needed, FALSE otherwise */
1104 static BOOL notify_measureitem(LISTVIEW_INFO *infoPtr)
1106 MEASUREITEMSTRUCT mis;
1107 mis.CtlType = ODT_LISTVIEW;
1108 mis.CtlID = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1109 mis.itemID = -1;
1110 mis.itemWidth = 0;
1111 mis.itemData = 0;
1112 mis.itemHeight= infoPtr->nItemHeight;
1113 SendMessageW(infoPtr->hwndNotify, WM_MEASUREITEM, mis.CtlID, (LPARAM)&mis);
1114 if (infoPtr->nItemHeight != max(mis.itemHeight, 1))
1116 infoPtr->nMeasureItemHeight = infoPtr->nItemHeight = max(mis.itemHeight, 1);
1117 return TRUE;
1119 return FALSE;
1122 /******** Item iterator functions **********************************/
1124 static RANGES ranges_create(int count);
1125 static void ranges_destroy(RANGES ranges);
1126 static BOOL ranges_add(RANGES ranges, RANGE range);
1127 static BOOL ranges_del(RANGES ranges, RANGE range);
1128 static void ranges_dump(RANGES ranges);
1130 static inline BOOL ranges_additem(RANGES ranges, INT nItem)
1132 RANGE range = { nItem, nItem + 1 };
1134 return ranges_add(ranges, range);
1137 static inline BOOL ranges_delitem(RANGES ranges, INT nItem)
1139 RANGE range = { nItem, nItem + 1 };
1141 return ranges_del(ranges, range);
1144 /***
1145 * ITERATOR DOCUMENTATION
1147 * The iterator functions allow for easy, and convenient iteration
1148 * over items of interest in the list. Typically, you create an
1149 * iterator, use it, and destroy it, as such:
1150 * ITERATOR i;
1152 * iterator_xxxitems(&i, ...);
1153 * while (iterator_{prev,next}(&i)
1155 * //code which uses i.nItem
1157 * iterator_destroy(&i);
1159 * where xxx is either: framed, or visible.
1160 * Note that it is important that the code destroys the iterator
1161 * after it's done with it, as the creation of the iterator may
1162 * allocate memory, which thus needs to be freed.
1164 * You can iterate both forwards, and backwards through the list,
1165 * by using iterator_next or iterator_prev respectively.
1167 * Lower numbered items are draw on top of higher number items in
1168 * LVS_ICON, and LVS_SMALLICON (which are the only modes where
1169 * items may overlap). So, to test items, you should use
1170 * iterator_next
1171 * which lists the items top to bottom (in Z-order).
1172 * For drawing items, you should use
1173 * iterator_prev
1174 * which lists the items bottom to top (in Z-order).
1175 * If you keep iterating over the items after the end-of-items
1176 * marker (-1) is returned, the iterator will start from the
1177 * beginning. Typically, you don't need to test for -1,
1178 * because iterator_{next,prev} will return TRUE if more items
1179 * are to be iterated over, or FALSE otherwise.
1181 * Note: the iterator is defined to be bidirectional. That is,
1182 * any number of prev followed by any number of next, or
1183 * five versa, should leave the iterator at the same item:
1184 * prev * n, next * n = next * n, prev * n
1186 * The iterator has a notion of an out-of-order, special item,
1187 * which sits at the start of the list. This is used in
1188 * LVS_ICON, and LVS_SMALLICON mode to handle the focused item,
1189 * which needs to be first, as it may overlap other items.
1191 * The code is a bit messy because we have:
1192 * - a special item to deal with
1193 * - simple range, or composite range
1194 * - empty range.
1195 * If you find bugs, or want to add features, please make sure you
1196 * always check/modify *both* iterator_prev, and iterator_next.
1199 /****
1200 * This function iterates through the items in increasing order,
1201 * but prefixed by the special item, then -1. That is:
1202 * special, 1, 2, 3, ..., n, -1.
1203 * Each item is listed only once.
1205 static inline BOOL iterator_next(ITERATOR* i)
1207 if (i->nItem == -1)
1209 i->nItem = i->nSpecial;
1210 if (i->nItem != -1) return TRUE;
1212 if (i->nItem == i->nSpecial)
1214 if (i->ranges) i->index = 0;
1215 goto pickarange;
1218 i->nItem++;
1219 testitem:
1220 if (i->nItem == i->nSpecial) i->nItem++;
1221 if (i->nItem < i->range.upper) return TRUE;
1223 pickarange:
1224 if (i->ranges)
1226 if (i->index < DPA_GetPtrCount(i->ranges->hdpa))
1227 i->range = *(RANGE*)DPA_GetPtr(i->ranges->hdpa, i->index++);
1228 else goto end;
1230 else if (i->nItem >= i->range.upper) goto end;
1232 i->nItem = i->range.lower;
1233 if (i->nItem >= 0) goto testitem;
1234 end:
1235 i->nItem = -1;
1236 return FALSE;
1239 /****
1240 * This function iterates through the items in decreasing order,
1241 * followed by the special item, then -1. That is:
1242 * n, n-1, ..., 3, 2, 1, special, -1.
1243 * Each item is listed only once.
1245 static inline BOOL iterator_prev(ITERATOR* i)
1247 BOOL start = FALSE;
1249 if (i->nItem == -1)
1251 start = TRUE;
1252 if (i->ranges) i->index = DPA_GetPtrCount(i->ranges->hdpa);
1253 goto pickarange;
1255 if (i->nItem == i->nSpecial)
1257 i->nItem = -1;
1258 return FALSE;
1261 testitem:
1262 i->nItem--;
1263 if (i->nItem == i->nSpecial) i->nItem--;
1264 if (i->nItem >= i->range.lower) return TRUE;
1266 pickarange:
1267 if (i->ranges)
1269 if (i->index > 0)
1270 i->range = *(RANGE*)DPA_GetPtr(i->ranges->hdpa, --i->index);
1271 else goto end;
1273 else if (!start && i->nItem < i->range.lower) goto end;
1275 i->nItem = i->range.upper;
1276 if (i->nItem > 0) goto testitem;
1277 end:
1278 return (i->nItem = i->nSpecial) != -1;
1281 static RANGE iterator_range(const ITERATOR *i)
1283 RANGE range;
1285 if (!i->ranges) return i->range;
1287 if (DPA_GetPtrCount(i->ranges->hdpa) > 0)
1289 range.lower = (*(RANGE*)DPA_GetPtr(i->ranges->hdpa, 0)).lower;
1290 range.upper = (*(RANGE*)DPA_GetPtr(i->ranges->hdpa, DPA_GetPtrCount(i->ranges->hdpa) - 1)).upper;
1292 else range.lower = range.upper = 0;
1294 return range;
1297 /***
1298 * Releases resources associated with this iterator.
1300 static inline void iterator_destroy(const ITERATOR *i)
1302 ranges_destroy(i->ranges);
1305 /***
1306 * Create an empty iterator.
1308 static inline void iterator_empty(ITERATOR* i)
1310 ZeroMemory(i, sizeof(*i));
1311 i->nItem = i->nSpecial = i->range.lower = i->range.upper = -1;
1314 /***
1315 * Create an iterator over a range.
1317 static inline void iterator_rangeitems(ITERATOR* i, RANGE range)
1319 iterator_empty(i);
1320 i->range = range;
1323 /***
1324 * Create an iterator over a bunch of ranges.
1325 * Please note that the iterator will take ownership of the ranges,
1326 * and will free them upon destruction.
1328 static inline void iterator_rangesitems(ITERATOR* i, RANGES ranges)
1330 iterator_empty(i);
1331 i->ranges = ranges;
1334 /***
1335 * Creates an iterator over the items which intersect frame.
1336 * Uses absolute coordinates rather than compensating for the current offset.
1338 static BOOL iterator_frameditems_absolute(ITERATOR* i, const LISTVIEW_INFO* infoPtr, const RECT *frame)
1340 RECT rcItem, rcTemp;
1341 RANGES ranges;
1343 TRACE("(frame=%s)\n", wine_dbgstr_rect(frame));
1345 /* in case we fail, we want to return an empty iterator */
1346 iterator_empty(i);
1348 if (infoPtr->nItemCount == 0)
1349 return TRUE;
1351 if (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON)
1353 INT nItem;
1355 if (infoPtr->uView == LV_VIEW_ICON && infoPtr->nFocusedItem != -1)
1357 LISTVIEW_GetItemBox(infoPtr, infoPtr->nFocusedItem, &rcItem);
1358 if (IntersectRect(&rcTemp, &rcItem, frame))
1359 i->nSpecial = infoPtr->nFocusedItem;
1361 if (!(ranges = ranges_create(50))) return FALSE;
1362 iterator_rangesitems(i, ranges);
1363 /* to do better here, we need to have PosX, and PosY sorted */
1364 TRACE("building icon ranges:\n");
1365 for (nItem = 0; nItem < infoPtr->nItemCount; nItem++)
1367 rcItem.left = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosX, nItem);
1368 rcItem.top = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosY, nItem);
1369 rcItem.right = rcItem.left + infoPtr->nItemWidth;
1370 rcItem.bottom = rcItem.top + infoPtr->nItemHeight;
1371 if (IntersectRect(&rcTemp, &rcItem, frame))
1372 ranges_additem(i->ranges, nItem);
1374 return TRUE;
1376 else if (infoPtr->uView == LV_VIEW_DETAILS)
1378 RANGE range;
1380 if (frame->left >= infoPtr->nItemWidth) return TRUE;
1381 if (frame->top >= infoPtr->nItemHeight * infoPtr->nItemCount) return TRUE;
1383 range.lower = max(frame->top / infoPtr->nItemHeight, 0);
1384 range.upper = min((frame->bottom - 1) / infoPtr->nItemHeight, infoPtr->nItemCount - 1) + 1;
1385 if (range.upper <= range.lower) return TRUE;
1386 iterator_rangeitems(i, range);
1387 TRACE(" report=%s\n", debugrange(&i->range));
1389 else
1391 INT nPerCol = max((infoPtr->rcList.bottom - infoPtr->rcList.top) / infoPtr->nItemHeight, 1);
1392 INT nFirstRow = max(frame->top / infoPtr->nItemHeight, 0);
1393 INT nLastRow = min((frame->bottom - 1) / infoPtr->nItemHeight, nPerCol - 1);
1394 INT nFirstCol;
1395 INT nLastCol;
1396 INT lower;
1397 RANGE item_range;
1398 INT nCol;
1400 if (infoPtr->nItemWidth)
1402 nFirstCol = max(frame->left / infoPtr->nItemWidth, 0);
1403 nLastCol = min((frame->right - 1) / infoPtr->nItemWidth, (infoPtr->nItemCount + nPerCol - 1) / nPerCol);
1405 else
1407 nFirstCol = max(frame->left, 0);
1408 nLastCol = min(frame->right - 1, (infoPtr->nItemCount + nPerCol - 1) / nPerCol);
1411 lower = nFirstCol * nPerCol + nFirstRow;
1413 TRACE("nPerCol=%d, nFirstRow=%d, nLastRow=%d, nFirstCol=%d, nLastCol=%d, lower=%d\n",
1414 nPerCol, nFirstRow, nLastRow, nFirstCol, nLastCol, lower);
1416 if (nLastCol < nFirstCol || nLastRow < nFirstRow) return TRUE;
1418 if (!(ranges = ranges_create(nLastCol - nFirstCol + 1))) return FALSE;
1419 iterator_rangesitems(i, ranges);
1420 TRACE("building list ranges:\n");
1421 for (nCol = nFirstCol; nCol <= nLastCol; nCol++)
1423 item_range.lower = nCol * nPerCol + nFirstRow;
1424 if(item_range.lower >= infoPtr->nItemCount) break;
1425 item_range.upper = min(nCol * nPerCol + nLastRow + 1, infoPtr->nItemCount);
1426 TRACE(" list=%s\n", debugrange(&item_range));
1427 ranges_add(i->ranges, item_range);
1431 return TRUE;
1434 /***
1435 * Creates an iterator over the items which intersect lprc.
1437 static BOOL iterator_frameditems(ITERATOR* i, const LISTVIEW_INFO* infoPtr, const RECT *lprc)
1439 RECT frame = *lprc;
1440 POINT Origin;
1442 TRACE("(lprc=%s)\n", wine_dbgstr_rect(lprc));
1444 LISTVIEW_GetOrigin(infoPtr, &Origin);
1445 OffsetRect(&frame, -Origin.x, -Origin.y);
1447 return iterator_frameditems_absolute(i, infoPtr, &frame);
1450 /***
1451 * Creates an iterator over the items which intersect the visible region of hdc.
1453 static BOOL iterator_visibleitems(ITERATOR *i, const LISTVIEW_INFO *infoPtr, HDC hdc)
1455 POINT Origin, Position;
1456 RECT rcItem, rcClip;
1457 INT rgntype;
1459 rgntype = GetClipBox(hdc, &rcClip);
1460 if (rgntype == NULLREGION)
1462 iterator_empty(i);
1463 return TRUE;
1465 if (!iterator_frameditems(i, infoPtr, &rcClip)) return FALSE;
1466 if (rgntype == SIMPLEREGION) return TRUE;
1468 /* first deal with the special item */
1469 if (i->nSpecial != -1)
1471 LISTVIEW_GetItemBox(infoPtr, i->nSpecial, &rcItem);
1472 if (!RectVisible(hdc, &rcItem)) i->nSpecial = -1;
1475 /* if we can't deal with the region, we'll just go with the simple range */
1476 LISTVIEW_GetOrigin(infoPtr, &Origin);
1477 TRACE("building visible range:\n");
1478 if (!i->ranges && i->range.lower < i->range.upper)
1480 if (!(i->ranges = ranges_create(50))) return TRUE;
1481 if (!ranges_add(i->ranges, i->range))
1483 ranges_destroy(i->ranges);
1484 i->ranges = 0;
1485 return TRUE;
1489 /* now delete the invisible items from the list */
1490 while(iterator_next(i))
1492 LISTVIEW_GetItemOrigin(infoPtr, i->nItem, &Position);
1493 rcItem.left = (infoPtr->uView == LV_VIEW_DETAILS) ? Origin.x : Position.x + Origin.x;
1494 rcItem.top = Position.y + Origin.y;
1495 rcItem.right = rcItem.left + infoPtr->nItemWidth;
1496 rcItem.bottom = rcItem.top + infoPtr->nItemHeight;
1497 if (!RectVisible(hdc, &rcItem))
1498 ranges_delitem(i->ranges, i->nItem);
1500 /* the iterator should restart on the next iterator_next */
1501 TRACE("done\n");
1503 return TRUE;
1506 /* Remove common elements from two iterators */
1507 /* Passed iterators have to point on the first elements */
1508 static BOOL iterator_remove_common_items(ITERATOR *iter1, ITERATOR *iter2)
1510 if(!iter1->ranges || !iter2->ranges) {
1511 int lower, upper;
1513 if(iter1->ranges || iter2->ranges ||
1514 (iter1->range.lower<iter2->range.lower && iter1->range.upper>iter2->range.upper) ||
1515 (iter1->range.lower>iter2->range.lower && iter1->range.upper<iter2->range.upper)) {
1516 ERR("result is not a one range iterator\n");
1517 return FALSE;
1520 if(iter1->range.lower==-1 || iter2->range.lower==-1)
1521 return TRUE;
1523 lower = iter1->range.lower;
1524 upper = iter1->range.upper;
1526 if(lower < iter2->range.lower)
1527 iter1->range.upper = iter2->range.lower;
1528 else if(upper > iter2->range.upper)
1529 iter1->range.lower = iter2->range.upper;
1530 else
1531 iter1->range.lower = iter1->range.upper = -1;
1533 if(iter2->range.lower < lower)
1534 iter2->range.upper = lower;
1535 else if(iter2->range.upper > upper)
1536 iter2->range.lower = upper;
1537 else
1538 iter2->range.lower = iter2->range.upper = -1;
1540 return TRUE;
1543 iterator_next(iter1);
1544 iterator_next(iter2);
1546 while(1) {
1547 if(iter1->nItem==-1 || iter2->nItem==-1)
1548 break;
1550 if(iter1->nItem == iter2->nItem) {
1551 int delete = iter1->nItem;
1553 iterator_prev(iter1);
1554 iterator_prev(iter2);
1555 ranges_delitem(iter1->ranges, delete);
1556 ranges_delitem(iter2->ranges, delete);
1557 iterator_next(iter1);
1558 iterator_next(iter2);
1559 } else if(iter1->nItem > iter2->nItem)
1560 iterator_next(iter2);
1561 else
1562 iterator_next(iter1);
1565 iter1->nItem = iter1->range.lower = iter1->range.upper = -1;
1566 iter2->nItem = iter2->range.lower = iter2->range.upper = -1;
1567 return TRUE;
1570 /******** Misc helper functions ************************************/
1572 static inline LRESULT CallWindowProcT(WNDPROC proc, HWND hwnd, UINT uMsg,
1573 WPARAM wParam, LPARAM lParam, BOOL isW)
1575 if (isW) return CallWindowProcW(proc, hwnd, uMsg, wParam, lParam);
1576 else return CallWindowProcA(proc, hwnd, uMsg, wParam, lParam);
1579 static inline BOOL is_autoarrange(const LISTVIEW_INFO *infoPtr)
1581 return (infoPtr->dwStyle & LVS_AUTOARRANGE) &&
1582 (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON);
1585 static void toggle_checkbox_state(LISTVIEW_INFO *infoPtr, INT nItem)
1587 DWORD state = STATEIMAGEINDEX(LISTVIEW_GetItemState(infoPtr, nItem, LVIS_STATEIMAGEMASK));
1588 if(state == 1 || state == 2)
1590 LVITEMW lvitem;
1591 state ^= 3;
1592 lvitem.state = INDEXTOSTATEIMAGEMASK(state);
1593 lvitem.stateMask = LVIS_STATEIMAGEMASK;
1594 LISTVIEW_SetItemState(infoPtr, nItem, &lvitem);
1598 /* this should be called after window style got updated,
1599 it used to reset view state to match current window style */
1600 static inline void map_style_view(LISTVIEW_INFO *infoPtr)
1602 switch (infoPtr->dwStyle & LVS_TYPEMASK)
1604 case LVS_ICON:
1605 infoPtr->uView = LV_VIEW_ICON;
1606 break;
1607 case LVS_REPORT:
1608 infoPtr->uView = LV_VIEW_DETAILS;
1609 break;
1610 case LVS_SMALLICON:
1611 infoPtr->uView = LV_VIEW_SMALLICON;
1612 break;
1613 case LVS_LIST:
1614 infoPtr->uView = LV_VIEW_LIST;
1618 /* computes next item id value */
1619 static DWORD get_next_itemid(const LISTVIEW_INFO *infoPtr)
1621 INT count = DPA_GetPtrCount(infoPtr->hdpaItemIds);
1623 if (count > 0)
1625 ITEM_ID *lpID = DPA_GetPtr(infoPtr->hdpaItemIds, count - 1);
1626 return lpID->id + 1;
1628 return 0;
1631 /******** Internal API functions ************************************/
1633 static inline COLUMN_INFO * LISTVIEW_GetColumnInfo(const LISTVIEW_INFO *infoPtr, INT nSubItem)
1635 static COLUMN_INFO mainItem;
1637 if (nSubItem == 0 && DPA_GetPtrCount(infoPtr->hdpaColumns) == 0) return &mainItem;
1638 assert (nSubItem >= 0 && nSubItem < DPA_GetPtrCount(infoPtr->hdpaColumns));
1640 /* update cached column rectangles */
1641 if (infoPtr->colRectsDirty)
1643 COLUMN_INFO *info;
1644 LISTVIEW_INFO *Ptr = (LISTVIEW_INFO*)infoPtr;
1645 INT i;
1647 for (i = 0; i < DPA_GetPtrCount(infoPtr->hdpaColumns); i++) {
1648 info = DPA_GetPtr(infoPtr->hdpaColumns, i);
1649 SendMessageW(infoPtr->hwndHeader, HDM_GETITEMRECT, i, (LPARAM)&info->rcHeader);
1651 Ptr->colRectsDirty = FALSE;
1654 return DPA_GetPtr(infoPtr->hdpaColumns, nSubItem);
1657 static INT LISTVIEW_CreateHeader(LISTVIEW_INFO *infoPtr)
1659 DWORD dFlags = WS_CHILD | HDS_HORZ | HDS_FULLDRAG | HDS_DRAGDROP;
1660 HINSTANCE hInst;
1662 if (infoPtr->hwndHeader) return 0;
1664 TRACE("Creating header for list %p\n", infoPtr->hwndSelf);
1666 /* setup creation flags */
1667 dFlags |= (LVS_NOSORTHEADER & infoPtr->dwStyle) ? 0 : HDS_BUTTONS;
1668 dFlags |= (LVS_NOCOLUMNHEADER & infoPtr->dwStyle) ? HDS_HIDDEN : 0;
1670 hInst = (HINSTANCE)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_HINSTANCE);
1672 /* create header */
1673 infoPtr->hwndHeader = CreateWindowW(WC_HEADERW, NULL, dFlags,
1674 0, 0, 0, 0, infoPtr->hwndSelf, NULL, hInst, NULL);
1675 if (!infoPtr->hwndHeader) return -1;
1677 /* set header unicode format */
1678 SendMessageW(infoPtr->hwndHeader, HDM_SETUNICODEFORMAT, TRUE, 0);
1680 /* set header font */
1681 SendMessageW(infoPtr->hwndHeader, WM_SETFONT, (WPARAM)infoPtr->hFont, TRUE);
1683 /* set header image list */
1684 if (infoPtr->himlSmall)
1685 SendMessageW(infoPtr->hwndHeader, HDM_SETIMAGELIST, 0, (LPARAM)infoPtr->himlSmall);
1687 LISTVIEW_UpdateSize(infoPtr);
1689 return 0;
1692 static inline void LISTVIEW_GetHeaderRect(const LISTVIEW_INFO *infoPtr, INT nSubItem, LPRECT lprc)
1694 *lprc = LISTVIEW_GetColumnInfo(infoPtr, nSubItem)->rcHeader;
1697 static inline BOOL LISTVIEW_IsHeaderEnabled(const LISTVIEW_INFO *infoPtr)
1699 return (infoPtr->uView == LV_VIEW_DETAILS ||
1700 infoPtr->dwLvExStyle & LVS_EX_HEADERINALLVIEWS) &&
1701 !(infoPtr->dwStyle & LVS_NOCOLUMNHEADER);
1704 static inline BOOL LISTVIEW_GetItemW(const LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem)
1706 return LISTVIEW_GetItemT(infoPtr, lpLVItem, TRUE);
1709 /* used to handle collapse main item column case */
1710 static inline BOOL LISTVIEW_DrawFocusRect(const LISTVIEW_INFO *infoPtr, HDC hdc)
1712 return (infoPtr->rcFocus.left < infoPtr->rcFocus.right) ?
1713 DrawFocusRect(hdc, &infoPtr->rcFocus) : FALSE;
1716 /* Listview invalidation functions: use _only_ these functions to invalidate */
1718 static inline BOOL is_redrawing(const LISTVIEW_INFO *infoPtr)
1720 return infoPtr->redraw;
1723 static inline void LISTVIEW_InvalidateRect(const LISTVIEW_INFO *infoPtr, const RECT* rect)
1725 if(!is_redrawing(infoPtr)) return;
1726 TRACE(" invalidating rect=%s\n", wine_dbgstr_rect(rect));
1727 InvalidateRect(infoPtr->hwndSelf, rect, TRUE);
1730 static inline void LISTVIEW_InvalidateItem(const LISTVIEW_INFO *infoPtr, INT nItem)
1732 RECT rcBox;
1734 if (!is_redrawing(infoPtr) || nItem < 0 || nItem >= infoPtr->nItemCount)
1735 return;
1737 LISTVIEW_GetItemBox(infoPtr, nItem, &rcBox);
1738 LISTVIEW_InvalidateRect(infoPtr, &rcBox);
1741 static inline void LISTVIEW_InvalidateSubItem(const LISTVIEW_INFO *infoPtr, INT nItem, INT nSubItem)
1743 POINT Origin, Position;
1744 RECT rcBox;
1746 if(!is_redrawing(infoPtr)) return;
1747 assert (infoPtr->uView == LV_VIEW_DETAILS);
1748 LISTVIEW_GetOrigin(infoPtr, &Origin);
1749 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
1750 LISTVIEW_GetHeaderRect(infoPtr, nSubItem, &rcBox);
1751 rcBox.top = 0;
1752 rcBox.bottom = infoPtr->nItemHeight;
1753 OffsetRect(&rcBox, Origin.x, Origin.y + Position.y);
1754 LISTVIEW_InvalidateRect(infoPtr, &rcBox);
1757 static inline void LISTVIEW_InvalidateList(const LISTVIEW_INFO *infoPtr)
1759 LISTVIEW_InvalidateRect(infoPtr, NULL);
1762 static inline void LISTVIEW_InvalidateColumn(const LISTVIEW_INFO *infoPtr, INT nColumn)
1764 RECT rcCol;
1766 if(!is_redrawing(infoPtr)) return;
1767 LISTVIEW_GetHeaderRect(infoPtr, nColumn, &rcCol);
1768 rcCol.top = infoPtr->rcList.top;
1769 rcCol.bottom = infoPtr->rcList.bottom;
1770 LISTVIEW_InvalidateRect(infoPtr, &rcCol);
1773 /***
1774 * DESCRIPTION:
1775 * Retrieves the number of items that can fit vertically in the client area.
1777 * PARAMETER(S):
1778 * [I] infoPtr : valid pointer to the listview structure
1780 * RETURN:
1781 * Number of items per row.
1783 static inline INT LISTVIEW_GetCountPerRow(const LISTVIEW_INFO *infoPtr)
1785 INT nListWidth = infoPtr->rcList.right - infoPtr->rcList.left;
1787 return max(nListWidth/(infoPtr->nItemWidth ? infoPtr->nItemWidth : 1), 1);
1790 /***
1791 * DESCRIPTION:
1792 * Retrieves the number of items that can fit horizontally in the client
1793 * area.
1795 * PARAMETER(S):
1796 * [I] infoPtr : valid pointer to the listview structure
1798 * RETURN:
1799 * Number of items per column.
1801 static inline INT LISTVIEW_GetCountPerColumn(const LISTVIEW_INFO *infoPtr)
1803 INT nListHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
1805 return infoPtr->nItemHeight ? max(nListHeight / infoPtr->nItemHeight, 1) : 0;
1809 /*************************************************************************
1810 * LISTVIEW_ProcessLetterKeys
1812 * Processes keyboard messages generated by pressing the letter keys
1813 * on the keyboard.
1814 * What this does is perform a case insensitive search from the
1815 * current position with the following quirks:
1816 * - If two chars or more are pressed in quick succession we search
1817 * for the corresponding string (e.g. 'abc').
1818 * - If there is a delay we wipe away the current search string and
1819 * restart with just that char.
1820 * - If the user keeps pressing the same character, whether slowly or
1821 * fast, so that the search string is entirely composed of this
1822 * character ('aaaaa' for instance), then we search for first item
1823 * that starting with that character.
1824 * - If the user types the above character in quick succession, then
1825 * we must also search for the corresponding string ('aaaaa'), and
1826 * go to that string if there is a match.
1828 * PARAMETERS
1829 * [I] hwnd : handle to the window
1830 * [I] charCode : the character code, the actual character
1831 * [I] keyData : key data
1833 * RETURNS
1835 * Zero.
1837 * BUGS
1839 * - The current implementation has a list of characters it will
1840 * accept and it ignores everything else. In particular it will
1841 * ignore accentuated characters which seems to match what
1842 * Windows does. But I'm not sure it makes sense to follow
1843 * Windows there.
1844 * - We don't sound a beep when the search fails.
1846 * SEE ALSO
1848 * TREEVIEW_ProcessLetterKeys
1850 static INT LISTVIEW_ProcessLetterKeys(LISTVIEW_INFO *infoPtr, WPARAM charCode, LPARAM keyData)
1852 WCHAR buffer[MAX_PATH];
1853 DWORD prevTime;
1854 LVITEMW item;
1855 int startidx;
1856 INT nItem;
1857 INT diff;
1859 /* simple parameter checking */
1860 if (!charCode || !keyData || infoPtr->nItemCount == 0) return 0;
1862 /* only allow the valid WM_CHARs through */
1863 if (!iswalnum(charCode) &&
1864 charCode != '.' && charCode != '`' && charCode != '!' &&
1865 charCode != '@' && charCode != '#' && charCode != '$' &&
1866 charCode != '%' && charCode != '^' && charCode != '&' &&
1867 charCode != '*' && charCode != '(' && charCode != ')' &&
1868 charCode != '-' && charCode != '_' && charCode != '+' &&
1869 charCode != '=' && charCode != '\\'&& charCode != ']' &&
1870 charCode != '}' && charCode != '[' && charCode != '{' &&
1871 charCode != '/' && charCode != '?' && charCode != '>' &&
1872 charCode != '<' && charCode != ',' && charCode != '~')
1873 return 0;
1875 /* update the search parameters */
1876 prevTime = infoPtr->lastKeyPressTimestamp;
1877 infoPtr->lastKeyPressTimestamp = GetTickCount();
1878 diff = infoPtr->lastKeyPressTimestamp - prevTime;
1880 if (diff >= 0 && diff < KEY_DELAY)
1882 if (infoPtr->nSearchParamLength < MAX_PATH - 1)
1883 infoPtr->szSearchParam[infoPtr->nSearchParamLength++] = charCode;
1885 if (infoPtr->charCode != charCode)
1886 infoPtr->charCode = charCode = 0;
1888 else
1890 infoPtr->charCode = charCode;
1891 infoPtr->szSearchParam[0] = charCode;
1892 infoPtr->nSearchParamLength = 1;
1895 /* should start from next after focused item, so next item that matches
1896 will be selected, if there isn't any and focused matches it will be selected
1897 on second search stage from beginning of the list */
1898 if (infoPtr->nFocusedItem >= 0 && infoPtr->nItemCount > 1)
1900 /* with some accumulated search data available start with current focus, otherwise
1901 it's excluded from search */
1902 startidx = infoPtr->nSearchParamLength > 1 ? infoPtr->nFocusedItem : infoPtr->nFocusedItem + 1;
1903 if (startidx == infoPtr->nItemCount) startidx = 0;
1905 else
1906 startidx = 0;
1908 /* let application handle this for virtual listview */
1909 if (infoPtr->dwStyle & LVS_OWNERDATA)
1911 NMLVFINDITEMW nmlv;
1913 memset(&nmlv.lvfi, 0, sizeof(nmlv.lvfi));
1914 nmlv.lvfi.flags = (LVFI_WRAP | LVFI_PARTIAL);
1915 nmlv.lvfi.psz = infoPtr->szSearchParam;
1916 nmlv.iStart = startidx;
1918 infoPtr->szSearchParam[infoPtr->nSearchParamLength] = 0;
1920 nItem = notify_hdr(infoPtr, LVN_ODFINDITEMW, (LPNMHDR)&nmlv.hdr);
1922 else
1924 int i = startidx, endidx;
1926 /* and search from the current position */
1927 nItem = -1;
1928 endidx = infoPtr->nItemCount;
1930 /* first search in [startidx, endidx), on failure continue in [0, startidx) */
1931 while (1)
1933 /* start from first item if not found with >= startidx */
1934 if (i == infoPtr->nItemCount && startidx > 0)
1936 endidx = startidx;
1937 startidx = 0;
1940 for (i = startidx; i < endidx; i++)
1942 /* retrieve text */
1943 item.mask = LVIF_TEXT;
1944 item.iItem = i;
1945 item.iSubItem = 0;
1946 item.pszText = buffer;
1947 item.cchTextMax = MAX_PATH;
1948 if (!LISTVIEW_GetItemW(infoPtr, &item)) return 0;
1950 if (!wcsnicmp(item.pszText, infoPtr->szSearchParam, infoPtr->nSearchParamLength))
1952 nItem = i;
1953 break;
1955 /* this is used to find first char match when search string is not available yet,
1956 otherwise every WM_CHAR will search to next item by first char, ignoring that we're
1957 already waiting for user to complete a string */
1958 else if (nItem == -1 && infoPtr->nSearchParamLength == 1 && !wcsnicmp(item.pszText, infoPtr->szSearchParam, 1))
1960 /* this would work but we must keep looking for a longer match */
1961 nItem = i;
1965 if ( nItem != -1 || /* found something */
1966 endidx != infoPtr->nItemCount || /* second search done */
1967 (startidx == 0 && endidx == infoPtr->nItemCount) /* full range for first search */ )
1968 break;
1972 if (nItem != -1)
1973 LISTVIEW_KeySelection(infoPtr, nItem, FALSE);
1975 return 0;
1978 /*************************************************************************
1979 * LISTVIEW_UpdateHeaderSize [Internal]
1981 * Function to resize the header control
1983 * PARAMS
1984 * [I] hwnd : handle to a window
1985 * [I] nNewScrollPos : scroll pos to set
1987 * RETURNS
1988 * None.
1990 static void LISTVIEW_UpdateHeaderSize(const LISTVIEW_INFO *infoPtr, INT nNewScrollPos)
1992 RECT winRect;
1993 POINT point[2];
1995 TRACE("nNewScrollPos=%d\n", nNewScrollPos);
1997 if (!infoPtr->hwndHeader) return;
1999 GetWindowRect(infoPtr->hwndHeader, &winRect);
2000 point[0].x = winRect.left;
2001 point[0].y = winRect.top;
2002 point[1].x = winRect.right;
2003 point[1].y = winRect.bottom;
2005 MapWindowPoints(HWND_DESKTOP, infoPtr->hwndSelf, point, 2);
2006 point[0].x = -nNewScrollPos;
2007 point[1].x += nNewScrollPos;
2009 SetWindowPos(infoPtr->hwndHeader,0,
2010 point[0].x,point[0].y,point[1].x,point[1].y,
2011 (infoPtr->dwStyle & LVS_NOCOLUMNHEADER) ? SWP_HIDEWINDOW : SWP_SHOWWINDOW |
2012 SWP_NOZORDER | SWP_NOACTIVATE);
2015 static INT LISTVIEW_UpdateHScroll(LISTVIEW_INFO *infoPtr)
2017 SCROLLINFO horzInfo;
2018 INT dx;
2020 ZeroMemory(&horzInfo, sizeof(SCROLLINFO));
2021 horzInfo.cbSize = sizeof(SCROLLINFO);
2022 horzInfo.nPage = infoPtr->rcList.right - infoPtr->rcList.left;
2024 /* for now, we'll set info.nMax to the _count_, and adjust it later */
2025 if (infoPtr->uView == LV_VIEW_LIST)
2027 INT nPerCol = LISTVIEW_GetCountPerColumn(infoPtr);
2028 horzInfo.nMax = (infoPtr->nItemCount + nPerCol - 1) / nPerCol;
2030 /* scroll by at least one column per page */
2031 if(horzInfo.nPage < infoPtr->nItemWidth)
2032 horzInfo.nPage = infoPtr->nItemWidth;
2034 if (infoPtr->nItemWidth)
2035 horzInfo.nPage /= infoPtr->nItemWidth;
2037 else if (infoPtr->uView == LV_VIEW_DETAILS)
2039 horzInfo.nMax = infoPtr->nItemWidth;
2041 else /* LV_VIEW_ICON, or LV_VIEW_SMALLICON */
2043 RECT rcView;
2045 if (LISTVIEW_GetViewRect(infoPtr, &rcView)) horzInfo.nMax = rcView.right - rcView.left;
2048 if (LISTVIEW_IsHeaderEnabled(infoPtr))
2050 if (DPA_GetPtrCount(infoPtr->hdpaColumns))
2052 RECT rcHeader;
2053 INT index;
2055 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX,
2056 DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, 0);
2058 LISTVIEW_GetHeaderRect(infoPtr, index, &rcHeader);
2059 horzInfo.nMax = rcHeader.right;
2060 TRACE("horzInfo.nMax=%d\n", horzInfo.nMax);
2064 horzInfo.fMask = SIF_RANGE | SIF_PAGE;
2065 horzInfo.nMax = max(horzInfo.nMax - 1, 0);
2066 dx = GetScrollPos(infoPtr->hwndSelf, SB_HORZ);
2067 dx -= SetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &horzInfo, TRUE);
2068 TRACE("horzInfo=%s\n", debugscrollinfo(&horzInfo));
2070 /* Update the Header Control */
2071 if (infoPtr->hwndHeader)
2073 horzInfo.fMask = SIF_POS;
2074 GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &horzInfo);
2075 LISTVIEW_UpdateHeaderSize(infoPtr, horzInfo.nPos);
2078 LISTVIEW_UpdateSize(infoPtr);
2079 return dx;
2082 static INT LISTVIEW_UpdateVScroll(LISTVIEW_INFO *infoPtr)
2084 SCROLLINFO vertInfo;
2085 INT dy;
2087 ZeroMemory(&vertInfo, sizeof(SCROLLINFO));
2088 vertInfo.cbSize = sizeof(SCROLLINFO);
2089 vertInfo.nPage = infoPtr->rcList.bottom - infoPtr->rcList.top;
2091 if (infoPtr->uView == LV_VIEW_DETAILS)
2093 vertInfo.nMax = infoPtr->nItemCount;
2095 /* scroll by at least one page */
2096 if(vertInfo.nPage < infoPtr->nItemHeight)
2097 vertInfo.nPage = infoPtr->nItemHeight;
2099 if (infoPtr->nItemHeight > 0)
2100 vertInfo.nPage /= infoPtr->nItemHeight;
2102 else if (infoPtr->uView != LV_VIEW_LIST) /* LV_VIEW_ICON, or LV_VIEW_SMALLICON */
2104 RECT rcView;
2106 if (LISTVIEW_GetViewRect(infoPtr, &rcView)) vertInfo.nMax = rcView.bottom - rcView.top;
2109 vertInfo.fMask = SIF_RANGE | SIF_PAGE;
2110 vertInfo.nMax = max(vertInfo.nMax - 1, 0);
2111 dy = GetScrollPos(infoPtr->hwndSelf, SB_VERT);
2112 dy -= SetScrollInfo(infoPtr->hwndSelf, SB_VERT, &vertInfo, TRUE);
2113 TRACE("vertInfo=%s\n", debugscrollinfo(&vertInfo));
2115 LISTVIEW_UpdateSize(infoPtr);
2116 return dy;
2119 /***
2120 * DESCRIPTION:
2121 * Update the scrollbars. This function should be called whenever
2122 * the content, size or view changes.
2124 * PARAMETER(S):
2125 * [I] infoPtr : valid pointer to the listview structure
2127 * RETURN:
2128 * None
2130 static void LISTVIEW_UpdateScroll(LISTVIEW_INFO *infoPtr)
2132 INT dx, dy, pass;
2134 if ((infoPtr->dwStyle & LVS_NOSCROLL) || !is_redrawing(infoPtr)) return;
2136 /* Setting the horizontal scroll can change the listview size
2137 * (and potentially everything else) so we need to recompute
2138 * everything again for the vertical scroll and vice-versa
2140 for (dx = 0, dy = 0, pass = 0; pass <= 1; pass++)
2142 dx += LISTVIEW_UpdateHScroll(infoPtr);
2143 dy += LISTVIEW_UpdateVScroll(infoPtr);
2146 /* Change of the range may have changed the scroll pos. If so move the content */
2147 if (dx != 0 || dy != 0)
2149 RECT listRect;
2150 listRect = infoPtr->rcList;
2151 ScrollWindowEx(infoPtr->hwndSelf, dx, dy, &listRect, &listRect, 0, 0,
2152 SW_ERASE | SW_INVALIDATE);
2157 /***
2158 * DESCRIPTION:
2159 * Shows/hides the focus rectangle.
2161 * PARAMETER(S):
2162 * [I] infoPtr : valid pointer to the listview structure
2163 * [I] fShow : TRUE to show the focus, FALSE to hide it.
2165 * RETURN:
2166 * None
2168 static void LISTVIEW_ShowFocusRect(const LISTVIEW_INFO *infoPtr, BOOL fShow)
2170 HDC hdc;
2172 TRACE("fShow=%d, nItem=%d\n", fShow, infoPtr->nFocusedItem);
2174 if (infoPtr->nFocusedItem < 0) return;
2176 /* we need some gymnastics in ICON mode to handle large items */
2177 if (infoPtr->uView == LV_VIEW_ICON)
2179 RECT rcBox;
2181 LISTVIEW_GetItemBox(infoPtr, infoPtr->nFocusedItem, &rcBox);
2182 if ((rcBox.bottom - rcBox.top) > infoPtr->nItemHeight)
2184 LISTVIEW_InvalidateRect(infoPtr, &rcBox);
2185 return;
2189 if (!(hdc = GetDC(infoPtr->hwndSelf))) return;
2191 /* for some reason, owner draw should work only in report mode */
2192 if ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (infoPtr->uView == LV_VIEW_DETAILS))
2194 DRAWITEMSTRUCT dis;
2195 LVITEMW item;
2197 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
2198 HFONT hOldFont = SelectObject(hdc, hFont);
2200 item.iItem = infoPtr->nFocusedItem;
2201 item.iSubItem = 0;
2202 item.mask = LVIF_PARAM;
2203 if (!LISTVIEW_GetItemW(infoPtr, &item)) goto done;
2205 ZeroMemory(&dis, sizeof(dis));
2206 dis.CtlType = ODT_LISTVIEW;
2207 dis.CtlID = (UINT)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
2208 dis.itemID = item.iItem;
2209 dis.itemAction = ODA_FOCUS;
2210 if (fShow) dis.itemState |= ODS_FOCUS;
2211 dis.hwndItem = infoPtr->hwndSelf;
2212 dis.hDC = hdc;
2213 LISTVIEW_GetItemBox(infoPtr, dis.itemID, &dis.rcItem);
2214 dis.itemData = item.lParam;
2216 SendMessageW(infoPtr->hwndNotify, WM_DRAWITEM, dis.CtlID, (LPARAM)&dis);
2218 SelectObject(hdc, hOldFont);
2220 else
2221 LISTVIEW_InvalidateItem(infoPtr, infoPtr->nFocusedItem);
2223 done:
2224 ReleaseDC(infoPtr->hwndSelf, hdc);
2227 /***
2228 * Invalidates all visible selected items.
2230 static void LISTVIEW_InvalidateSelectedItems(const LISTVIEW_INFO *infoPtr)
2232 ITERATOR i;
2234 iterator_frameditems(&i, infoPtr, &infoPtr->rcList);
2235 while(iterator_next(&i))
2237 if (LISTVIEW_GetItemState(infoPtr, i.nItem, LVIS_SELECTED))
2238 LISTVIEW_InvalidateItem(infoPtr, i.nItem);
2240 iterator_destroy(&i);
2244 /***
2245 * DESCRIPTION: [INTERNAL]
2246 * Computes an item's (left,top) corner, relative to rcView.
2247 * That is, the position has NOT been made relative to the Origin.
2248 * This is deliberate, to avoid computing the Origin over, and
2249 * over again, when this function is called in a loop. Instead,
2250 * one can factor the computation of the Origin before the loop,
2251 * and offset the value returned by this function, on every iteration.
2253 * PARAMETER(S):
2254 * [I] infoPtr : valid pointer to the listview structure
2255 * [I] nItem : item number
2256 * [O] lpptOrig : item top, left corner
2258 * RETURN:
2259 * None.
2261 static void LISTVIEW_GetItemOrigin(const LISTVIEW_INFO *infoPtr, INT nItem, LPPOINT lpptPosition)
2263 assert(nItem >= 0 && nItem < infoPtr->nItemCount);
2265 if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
2267 lpptPosition->x = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosX, nItem);
2268 lpptPosition->y = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosY, nItem);
2270 else if (infoPtr->uView == LV_VIEW_LIST)
2272 INT nCountPerColumn = LISTVIEW_GetCountPerColumn(infoPtr);
2273 lpptPosition->x = nItem / nCountPerColumn * infoPtr->nItemWidth;
2274 lpptPosition->y = nItem % nCountPerColumn * infoPtr->nItemHeight;
2276 else /* LV_VIEW_DETAILS */
2278 lpptPosition->x = REPORT_MARGINX;
2279 /* item is always at zero indexed column */
2280 if (DPA_GetPtrCount(infoPtr->hdpaColumns) > 0)
2281 lpptPosition->x += LISTVIEW_GetColumnInfo(infoPtr, 0)->rcHeader.left;
2282 lpptPosition->y = nItem * infoPtr->nItemHeight;
2286 /***
2287 * DESCRIPTION: [INTERNAL]
2288 * Compute the rectangles of an item. This is to localize all
2289 * the computations in one place. If you are not interested in some
2290 * of these values, simply pass in a NULL -- the function is smart
2291 * enough to compute only what's necessary. The function computes
2292 * the standard rectangles (BOUNDS, ICON, LABEL) plus a non-standard
2293 * one, the BOX rectangle. This rectangle is very cheap to compute,
2294 * and is guaranteed to contain all the other rectangles. Computing
2295 * the ICON rect is also cheap, but all the others are potentially
2296 * expensive. This gives an easy and effective optimization when
2297 * searching (like point inclusion, or rectangle intersection):
2298 * first test against the BOX, and if TRUE, test against the desired
2299 * rectangle.
2300 * If the function does not have all the necessary information
2301 * to computed the requested rectangles, will crash with a
2302 * failed assertion. This is done so we catch all programming
2303 * errors, given that the function is called only from our code.
2305 * We have the following 'special' meanings for a few fields:
2306 * * If LVIS_FOCUSED is set, we assume the item has the focus
2307 * This is important in ICON mode, where it might get a larger
2308 * then usual rectangle
2310 * Please note that subitem support works only in REPORT mode.
2312 * PARAMETER(S):
2313 * [I] infoPtr : valid pointer to the listview structure
2314 * [I] lpLVItem : item to compute the measures for
2315 * [O] lprcBox : ptr to Box rectangle
2316 * Same as LVM_GETITEMRECT with LVIR_BOUNDS
2317 * [0] lprcSelectBox : ptr to select box rectangle
2318 * Same as LVM_GETITEMRECT with LVIR_SELECTEDBOUNDS
2319 * [O] lprcIcon : ptr to Icon rectangle
2320 * Same as LVM_GETITEMRECT with LVIR_ICON
2321 * [O] lprcStateIcon: ptr to State Icon rectangle
2322 * [O] lprcLabel : ptr to Label rectangle
2323 * Same as LVM_GETITEMRECT with LVIR_LABEL
2325 * RETURN:
2326 * None.
2328 static void LISTVIEW_GetItemMetrics(const LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem,
2329 LPRECT lprcBox, LPRECT lprcSelectBox,
2330 LPRECT lprcIcon, LPRECT lprcStateIcon, LPRECT lprcLabel)
2332 BOOL doSelectBox = FALSE, doIcon = FALSE, doLabel = FALSE, oversizedBox = FALSE;
2333 RECT Box, SelectBox, Icon, Label;
2334 COLUMN_INFO *lpColumnInfo = NULL;
2335 SIZE labelSize = { 0, 0 };
2337 TRACE("(lpLVItem=%s)\n", debuglvitem_t(lpLVItem, TRUE));
2339 /* Be smart and try to figure out the minimum we have to do */
2340 if (lpLVItem->iSubItem) assert(infoPtr->uView == LV_VIEW_DETAILS);
2341 if (infoPtr->uView == LV_VIEW_ICON && (lprcBox || lprcLabel))
2343 assert((lpLVItem->mask & LVIF_STATE) && (lpLVItem->stateMask & LVIS_FOCUSED));
2344 if (lpLVItem->state & LVIS_FOCUSED) oversizedBox = doLabel = TRUE;
2346 if (lprcSelectBox) doSelectBox = TRUE;
2347 if (lprcLabel) doLabel = TRUE;
2348 if (doLabel || lprcIcon || lprcStateIcon) doIcon = TRUE;
2349 if (doSelectBox)
2351 doIcon = TRUE;
2352 doLabel = TRUE;
2355 /************************************************************/
2356 /* compute the box rectangle (it should be cheap to do) */
2357 /************************************************************/
2358 if (lpLVItem->iSubItem || infoPtr->uView == LV_VIEW_DETAILS)
2359 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, lpLVItem->iSubItem);
2361 if (lpLVItem->iSubItem)
2363 Box = lpColumnInfo->rcHeader;
2365 else
2367 Box.left = 0;
2368 Box.right = infoPtr->nItemWidth;
2370 Box.top = 0;
2371 Box.bottom = infoPtr->nItemHeight;
2373 /******************************************************************/
2374 /* compute ICON bounding box (ala LVM_GETITEMRECT) and STATEICON */
2375 /******************************************************************/
2376 if (doIcon)
2378 LONG state_width = 0;
2380 if (infoPtr->himlState && lpLVItem->iSubItem == 0)
2381 state_width = infoPtr->iconStateSize.cx;
2383 if (infoPtr->uView == LV_VIEW_ICON)
2385 Icon.left = Box.left + state_width;
2386 if (infoPtr->himlNormal)
2387 Icon.left += (infoPtr->nItemWidth - infoPtr->iconSize.cx - state_width) / 2;
2388 Icon.top = Box.top + ICON_TOP_PADDING;
2389 Icon.right = Icon.left;
2390 Icon.bottom = Icon.top;
2391 if (infoPtr->himlNormal)
2393 Icon.right += infoPtr->iconSize.cx;
2394 Icon.bottom += infoPtr->iconSize.cy;
2397 else /* LV_VIEW_SMALLICON, LV_VIEW_LIST or LV_VIEW_DETAILS */
2399 Icon.left = Box.left + state_width;
2401 if (infoPtr->uView == LV_VIEW_DETAILS && lpLVItem->iSubItem == 0)
2403 /* we need the indent in report mode */
2404 assert(lpLVItem->mask & LVIF_INDENT);
2405 Icon.left += infoPtr->iconSize.cx * lpLVItem->iIndent + REPORT_MARGINX;
2408 Icon.top = Box.top;
2409 Icon.right = Icon.left;
2410 if (infoPtr->himlSmall &&
2411 (!lpColumnInfo || lpLVItem->iSubItem == 0 ||
2412 ((infoPtr->dwLvExStyle & LVS_EX_SUBITEMIMAGES) && lpLVItem->iImage != I_IMAGECALLBACK)))
2413 Icon.right += infoPtr->iconSize.cx;
2414 Icon.bottom = Icon.top + infoPtr->iconSize.cy;
2416 if(lprcIcon) *lprcIcon = Icon;
2417 TRACE(" - icon=%s\n", wine_dbgstr_rect(&Icon));
2419 /* TODO: is this correct? */
2420 if (lprcStateIcon)
2422 lprcStateIcon->left = Icon.left - state_width;
2423 lprcStateIcon->right = Icon.left;
2424 lprcStateIcon->top = Icon.top;
2425 lprcStateIcon->bottom = lprcStateIcon->top + infoPtr->iconSize.cy;
2426 TRACE(" - state icon=%s\n", wine_dbgstr_rect(lprcStateIcon));
2429 else Icon.right = 0;
2431 /************************************************************/
2432 /* compute LABEL bounding box (ala LVM_GETITEMRECT) */
2433 /************************************************************/
2434 if (doLabel)
2436 /* calculate how far to the right can the label stretch */
2437 Label.right = Box.right;
2438 if (infoPtr->uView == LV_VIEW_DETAILS)
2440 if (lpLVItem->iSubItem == 0)
2442 /* we need a zero based rect here */
2443 Label = lpColumnInfo->rcHeader;
2444 OffsetRect(&Label, -Label.left, 0);
2448 if (lpLVItem->iSubItem || ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && infoPtr->uView == LV_VIEW_DETAILS))
2450 labelSize.cx = infoPtr->nItemWidth;
2451 labelSize.cy = infoPtr->nItemHeight;
2452 goto calc_label;
2455 /* we need the text in non owner draw mode */
2456 assert(lpLVItem->mask & LVIF_TEXT);
2457 if (is_text(lpLVItem->pszText))
2459 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
2460 HDC hdc = GetDC(infoPtr->hwndSelf);
2461 HFONT hOldFont = SelectObject(hdc, hFont);
2462 UINT uFormat;
2463 RECT rcText;
2465 /* compute rough rectangle where the label will go */
2466 SetRectEmpty(&rcText);
2467 rcText.right = infoPtr->nItemWidth - TRAILING_LABEL_PADDING;
2468 rcText.bottom = infoPtr->nItemHeight;
2469 if (infoPtr->uView == LV_VIEW_ICON)
2470 rcText.bottom -= ICON_TOP_PADDING + infoPtr->iconSize.cy + ICON_BOTTOM_PADDING;
2472 /* now figure out the flags */
2473 if (infoPtr->uView == LV_VIEW_ICON)
2474 uFormat = oversizedBox ? LV_FL_DT_FLAGS : LV_ML_DT_FLAGS;
2475 else
2476 uFormat = LV_SL_DT_FLAGS;
2478 DrawTextW (hdc, lpLVItem->pszText, -1, &rcText, uFormat | DT_CALCRECT);
2480 if (rcText.right != rcText.left)
2481 labelSize.cx = min(rcText.right - rcText.left + TRAILING_LABEL_PADDING, infoPtr->nItemWidth);
2483 labelSize.cy = rcText.bottom - rcText.top;
2485 SelectObject(hdc, hOldFont);
2486 ReleaseDC(infoPtr->hwndSelf, hdc);
2489 calc_label:
2490 if (infoPtr->uView == LV_VIEW_ICON)
2492 Label.left = Box.left + (infoPtr->nItemWidth - labelSize.cx) / 2;
2493 Label.top = Box.top + ICON_TOP_PADDING_HITABLE +
2494 infoPtr->iconSize.cy + ICON_BOTTOM_PADDING;
2495 Label.right = Label.left + labelSize.cx;
2496 Label.bottom = Label.top + infoPtr->nItemHeight;
2497 if (!oversizedBox && labelSize.cy > infoPtr->ntmHeight)
2499 labelSize.cy = min(Box.bottom - Label.top, labelSize.cy);
2500 labelSize.cy /= infoPtr->ntmHeight;
2501 labelSize.cy = max(labelSize.cy, 1);
2502 labelSize.cy *= infoPtr->ntmHeight;
2504 Label.bottom = Label.top + labelSize.cy + HEIGHT_PADDING;
2506 else if (infoPtr->uView == LV_VIEW_DETAILS)
2508 Label.left = Icon.right;
2509 Label.top = Box.top;
2510 Label.right = lpLVItem->iSubItem ? lpColumnInfo->rcHeader.right :
2511 lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left;
2512 Label.bottom = Label.top + infoPtr->nItemHeight;
2514 else /* LV_VIEW_SMALLICON or LV_VIEW_LIST */
2516 Label.left = Icon.right;
2517 Label.top = Box.top;
2518 Label.right = min(Label.left + labelSize.cx, Label.right);
2519 Label.bottom = Label.top + infoPtr->nItemHeight;
2522 if (lprcLabel) *lprcLabel = Label;
2523 TRACE(" - label=%s\n", wine_dbgstr_rect(&Label));
2526 /************************************************************/
2527 /* compute SELECT bounding box */
2528 /************************************************************/
2529 if (doSelectBox)
2531 if (infoPtr->uView == LV_VIEW_DETAILS)
2533 SelectBox.left = Icon.left;
2534 SelectBox.top = Box.top;
2535 SelectBox.bottom = Box.bottom;
2537 if (labelSize.cx)
2538 SelectBox.right = min(Label.left + labelSize.cx, Label.right);
2539 else
2540 SelectBox.right = min(Label.left + MAX_EMPTYTEXT_SELECT_WIDTH, Label.right);
2542 else
2544 UnionRect(&SelectBox, &Icon, &Label);
2546 if (lprcSelectBox) *lprcSelectBox = SelectBox;
2547 TRACE(" - select box=%s\n", wine_dbgstr_rect(&SelectBox));
2550 /* Fix the Box if necessary */
2551 if (lprcBox)
2553 if (oversizedBox) UnionRect(lprcBox, &Box, &Label);
2554 else *lprcBox = Box;
2556 TRACE(" - box=%s\n", wine_dbgstr_rect(&Box));
2559 /***
2560 * DESCRIPTION: [INTERNAL]
2562 * PARAMETER(S):
2563 * [I] infoPtr : valid pointer to the listview structure
2564 * [I] nItem : item number
2565 * [O] lprcBox : ptr to Box rectangle
2567 * RETURN:
2568 * None.
2570 static void LISTVIEW_GetItemBox(const LISTVIEW_INFO *infoPtr, INT nItem, LPRECT lprcBox)
2572 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
2573 POINT Position, Origin;
2574 LVITEMW lvItem;
2576 LISTVIEW_GetOrigin(infoPtr, &Origin);
2577 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
2579 /* Be smart and try to figure out the minimum we have to do */
2580 lvItem.mask = 0;
2581 if (infoPtr->uView == LV_VIEW_ICON && infoPtr->bFocus && LISTVIEW_GetItemState(infoPtr, nItem, LVIS_FOCUSED))
2582 lvItem.mask |= LVIF_TEXT;
2583 lvItem.iItem = nItem;
2584 lvItem.iSubItem = 0;
2585 lvItem.pszText = szDispText;
2586 lvItem.cchTextMax = DISP_TEXT_SIZE;
2587 if (lvItem.mask) LISTVIEW_GetItemW(infoPtr, &lvItem);
2588 if (infoPtr->uView == LV_VIEW_ICON)
2590 lvItem.mask |= LVIF_STATE;
2591 lvItem.stateMask = LVIS_FOCUSED;
2592 lvItem.state = (lvItem.mask & LVIF_TEXT ? LVIS_FOCUSED : 0);
2594 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, lprcBox, 0, 0, 0, 0);
2596 if (infoPtr->uView == LV_VIEW_DETAILS && infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT &&
2597 SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, 0, 0))
2599 OffsetRect(lprcBox, Origin.x, Position.y + Origin.y);
2601 else
2602 OffsetRect(lprcBox, Position.x + Origin.x, Position.y + Origin.y);
2605 /* LISTVIEW_MapIdToIndex helper */
2606 static INT CALLBACK MapIdSearchCompare(LPVOID p1, LPVOID p2, LPARAM lParam)
2608 ITEM_ID *id1 = (ITEM_ID*)p1;
2609 ITEM_ID *id2 = (ITEM_ID*)p2;
2611 if (id1->id == id2->id) return 0;
2613 return (id1->id < id2->id) ? -1 : 1;
2616 /***
2617 * DESCRIPTION:
2618 * Returns the item index for id specified.
2620 * PARAMETER(S):
2621 * [I] infoPtr : valid pointer to the listview structure
2622 * [I] iID : item id to get index for
2624 * RETURN:
2625 * Item index, or -1 on failure.
2627 static INT LISTVIEW_MapIdToIndex(const LISTVIEW_INFO *infoPtr, UINT iID)
2629 ITEM_ID ID;
2630 INT index;
2632 TRACE("iID=%d\n", iID);
2634 if (infoPtr->dwStyle & LVS_OWNERDATA) return -1;
2635 if (infoPtr->nItemCount == 0) return -1;
2637 ID.id = iID;
2638 index = DPA_Search(infoPtr->hdpaItemIds, &ID, -1, MapIdSearchCompare, 0, DPAS_SORTED);
2640 if (index != -1)
2642 ITEM_ID *lpID = DPA_GetPtr(infoPtr->hdpaItemIds, index);
2643 return DPA_GetPtrIndex(infoPtr->hdpaItems, lpID->item);
2646 return -1;
2649 /***
2650 * DESCRIPTION:
2651 * Returns the item id for index given.
2653 * PARAMETER(S):
2654 * [I] infoPtr : valid pointer to the listview structure
2655 * [I] iItem : item index to get id for
2657 * RETURN:
2658 * Item id.
2660 static DWORD LISTVIEW_MapIndexToId(const LISTVIEW_INFO *infoPtr, INT iItem)
2662 ITEM_INFO *lpItem;
2663 HDPA hdpaSubItems;
2665 TRACE("iItem=%d\n", iItem);
2667 if (infoPtr->dwStyle & LVS_OWNERDATA) return -1;
2668 if (iItem < 0 || iItem >= infoPtr->nItemCount) return -1;
2670 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, iItem);
2671 lpItem = DPA_GetPtr(hdpaSubItems, 0);
2673 return lpItem->id->id;
2676 /***
2677 * DESCRIPTION:
2678 * Returns the current icon position, and advances it along the top.
2679 * The returned position is not offset by Origin.
2681 * PARAMETER(S):
2682 * [I] infoPtr : valid pointer to the listview structure
2683 * [O] lpPos : will get the current icon position
2685 * RETURN:
2686 * None
2688 static void LISTVIEW_NextIconPosTop(LISTVIEW_INFO *infoPtr, LPPOINT lpPos)
2690 INT nListWidth = infoPtr->rcList.right - infoPtr->rcList.left;
2692 *lpPos = infoPtr->currIconPos;
2694 infoPtr->currIconPos.x += infoPtr->nItemWidth;
2695 if (infoPtr->currIconPos.x + infoPtr->nItemWidth <= nListWidth) return;
2697 infoPtr->currIconPos.x = 0;
2698 infoPtr->currIconPos.y += infoPtr->nItemHeight;
2702 /***
2703 * DESCRIPTION:
2704 * Returns the current icon position, and advances it down the left edge.
2705 * The returned position is not offset by Origin.
2707 * PARAMETER(S):
2708 * [I] infoPtr : valid pointer to the listview structure
2709 * [O] lpPos : will get the current icon position
2711 * RETURN:
2712 * None
2714 static void LISTVIEW_NextIconPosLeft(LISTVIEW_INFO *infoPtr, LPPOINT lpPos)
2716 INT nListHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
2718 *lpPos = infoPtr->currIconPos;
2720 infoPtr->currIconPos.y += infoPtr->nItemHeight;
2721 if (infoPtr->currIconPos.y + infoPtr->nItemHeight <= nListHeight) return;
2723 infoPtr->currIconPos.x += infoPtr->nItemWidth;
2724 infoPtr->currIconPos.y = 0;
2728 /***
2729 * DESCRIPTION:
2730 * Moves an icon to the specified position.
2731 * It takes care of invalidating the item, etc.
2733 * PARAMETER(S):
2734 * [I] infoPtr : valid pointer to the listview structure
2735 * [I] nItem : the item to move
2736 * [I] lpPos : the new icon position
2737 * [I] isNew : flags the item as being new
2739 * RETURN:
2740 * Success: TRUE
2741 * Failure: FALSE
2743 static BOOL LISTVIEW_MoveIconTo(const LISTVIEW_INFO *infoPtr, INT nItem, const POINT *lppt, BOOL isNew)
2745 POINT old;
2747 if (!isNew)
2749 old.x = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosX, nItem);
2750 old.y = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosY, nItem);
2752 if (lppt->x == old.x && lppt->y == old.y) return TRUE;
2753 LISTVIEW_InvalidateItem(infoPtr, nItem);
2756 /* Allocating a POINTER for every item is too resource intensive,
2757 * so we'll keep the (x,y) in different arrays */
2758 if (!DPA_SetPtr(infoPtr->hdpaPosX, nItem, (void *)(LONG_PTR)lppt->x)) return FALSE;
2759 if (!DPA_SetPtr(infoPtr->hdpaPosY, nItem, (void *)(LONG_PTR)lppt->y)) return FALSE;
2761 LISTVIEW_InvalidateItem(infoPtr, nItem);
2763 return TRUE;
2766 /***
2767 * DESCRIPTION:
2768 * Arranges listview items in icon display mode.
2770 * PARAMETER(S):
2771 * [I] infoPtr : valid pointer to the listview structure
2772 * [I] nAlignCode : alignment code
2774 * RETURN:
2775 * SUCCESS : TRUE
2776 * FAILURE : FALSE
2778 static BOOL LISTVIEW_Arrange(LISTVIEW_INFO *infoPtr, INT nAlignCode)
2780 void (*next_pos)(LISTVIEW_INFO *, LPPOINT);
2781 POINT pos;
2782 INT i;
2784 if (infoPtr->uView != LV_VIEW_ICON && infoPtr->uView != LV_VIEW_SMALLICON) return FALSE;
2786 TRACE("nAlignCode=%d\n", nAlignCode);
2788 if (nAlignCode == LVA_DEFAULT)
2790 if (infoPtr->dwStyle & LVS_ALIGNLEFT) nAlignCode = LVA_ALIGNLEFT;
2791 else nAlignCode = LVA_ALIGNTOP;
2794 switch (nAlignCode)
2796 case LVA_ALIGNLEFT: next_pos = LISTVIEW_NextIconPosLeft; break;
2797 case LVA_ALIGNTOP: next_pos = LISTVIEW_NextIconPosTop; break;
2798 case LVA_SNAPTOGRID: next_pos = LISTVIEW_NextIconPosTop; break; /* FIXME */
2799 default: return FALSE;
2802 infoPtr->currIconPos.x = infoPtr->currIconPos.y = 0;
2803 for (i = 0; i < infoPtr->nItemCount; i++)
2805 next_pos(infoPtr, &pos);
2806 LISTVIEW_MoveIconTo(infoPtr, i, &pos, FALSE);
2809 return TRUE;
2812 /***
2813 * DESCRIPTION:
2814 * Retrieves the bounding rectangle of all the items, not offset by Origin.
2815 * For LVS_REPORT always returns empty rectangle.
2817 * PARAMETER(S):
2818 * [I] infoPtr : valid pointer to the listview structure
2819 * [O] lprcView : bounding rectangle
2821 * RETURN:
2822 * SUCCESS : TRUE
2823 * FAILURE : FALSE
2825 static void LISTVIEW_GetAreaRect(const LISTVIEW_INFO *infoPtr, LPRECT lprcView)
2827 INT i, x, y;
2829 SetRectEmpty(lprcView);
2831 switch (infoPtr->uView)
2833 case LV_VIEW_ICON:
2834 case LV_VIEW_SMALLICON:
2835 for (i = 0; i < infoPtr->nItemCount; i++)
2837 x = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosX, i);
2838 y = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosY, i);
2839 lprcView->right = max(lprcView->right, x);
2840 lprcView->bottom = max(lprcView->bottom, y);
2842 if (infoPtr->nItemCount > 0)
2844 lprcView->right += infoPtr->nItemWidth;
2845 lprcView->bottom += infoPtr->nItemHeight;
2847 break;
2849 case LV_VIEW_LIST:
2850 y = LISTVIEW_GetCountPerColumn(infoPtr);
2851 x = infoPtr->nItemCount / y;
2852 if (infoPtr->nItemCount % y) x++;
2853 lprcView->right = x * infoPtr->nItemWidth;
2854 lprcView->bottom = y * infoPtr->nItemHeight;
2855 break;
2859 /***
2860 * DESCRIPTION:
2861 * Retrieves the bounding rectangle of all the items.
2863 * PARAMETER(S):
2864 * [I] infoPtr : valid pointer to the listview structure
2865 * [O] lprcView : bounding rectangle
2867 * RETURN:
2868 * SUCCESS : TRUE
2869 * FAILURE : FALSE
2871 static BOOL LISTVIEW_GetViewRect(const LISTVIEW_INFO *infoPtr, LPRECT lprcView)
2873 POINT ptOrigin;
2875 TRACE("(lprcView=%p)\n", lprcView);
2877 if (!lprcView) return FALSE;
2879 LISTVIEW_GetAreaRect(infoPtr, lprcView);
2881 if (infoPtr->uView != LV_VIEW_DETAILS)
2883 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
2884 OffsetRect(lprcView, ptOrigin.x, ptOrigin.y);
2887 TRACE("lprcView=%s\n", wine_dbgstr_rect(lprcView));
2889 return TRUE;
2892 /***
2893 * DESCRIPTION:
2894 * Retrieves the subitem pointer associated with the subitem index.
2896 * PARAMETER(S):
2897 * [I] hdpaSubItems : DPA handle for a specific item
2898 * [I] nSubItem : index of subitem
2900 * RETURN:
2901 * SUCCESS : subitem pointer
2902 * FAILURE : NULL
2904 static SUBITEM_INFO* LISTVIEW_GetSubItemPtr(HDPA hdpaSubItems, INT nSubItem)
2906 SUBITEM_INFO *lpSubItem;
2907 INT i;
2909 /* we should binary search here if need be */
2910 for (i = 1; i < DPA_GetPtrCount(hdpaSubItems); i++)
2912 lpSubItem = DPA_GetPtr(hdpaSubItems, i);
2913 if (lpSubItem->iSubItem == nSubItem)
2914 return lpSubItem;
2917 return NULL;
2921 /***
2922 * DESCRIPTION:
2923 * Calculates the desired item width.
2925 * PARAMETER(S):
2926 * [I] infoPtr : valid pointer to the listview structure
2928 * RETURN:
2929 * The desired item width.
2931 static INT LISTVIEW_CalculateItemWidth(const LISTVIEW_INFO *infoPtr)
2933 INT nItemWidth = 0;
2935 TRACE("view %ld\n", infoPtr->uView);
2937 if (infoPtr->uView == LV_VIEW_ICON)
2938 nItemWidth = infoPtr->iconSpacing.cx;
2939 else if (infoPtr->uView == LV_VIEW_DETAILS)
2941 if (DPA_GetPtrCount(infoPtr->hdpaColumns) > 0)
2943 RECT rcHeader;
2944 INT index;
2946 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX,
2947 DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, 0);
2949 LISTVIEW_GetHeaderRect(infoPtr, index, &rcHeader);
2950 nItemWidth = rcHeader.right;
2953 else /* LV_VIEW_SMALLICON, or LV_VIEW_LIST */
2955 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
2956 LVITEMW lvItem;
2957 INT i;
2959 lvItem.mask = LVIF_TEXT;
2960 lvItem.iSubItem = 0;
2962 for (i = 0; i < infoPtr->nItemCount; i++)
2964 lvItem.iItem = i;
2965 lvItem.pszText = szDispText;
2966 lvItem.cchTextMax = DISP_TEXT_SIZE;
2967 if (LISTVIEW_GetItemW(infoPtr, &lvItem))
2968 nItemWidth = max(LISTVIEW_GetStringWidthT(infoPtr, lvItem.pszText, TRUE),
2969 nItemWidth);
2972 if (infoPtr->himlSmall) nItemWidth += infoPtr->iconSize.cx;
2973 if (infoPtr->himlState) nItemWidth += infoPtr->iconStateSize.cx;
2975 nItemWidth = max(DEFAULT_COLUMN_WIDTH, nItemWidth + WIDTH_PADDING);
2978 return nItemWidth;
2981 /***
2982 * DESCRIPTION:
2983 * Calculates the desired item height.
2985 * PARAMETER(S):
2986 * [I] infoPtr : valid pointer to the listview structure
2988 * RETURN:
2989 * The desired item height.
2991 static INT LISTVIEW_CalculateItemHeight(const LISTVIEW_INFO *infoPtr)
2993 INT nItemHeight;
2995 TRACE("view %ld\n", infoPtr->uView);
2997 if (infoPtr->uView == LV_VIEW_ICON)
2998 nItemHeight = infoPtr->iconSpacing.cy;
2999 else
3001 nItemHeight = infoPtr->ntmHeight;
3002 if (infoPtr->himlState)
3003 nItemHeight = max(nItemHeight, infoPtr->iconStateSize.cy);
3004 if (infoPtr->himlSmall)
3005 nItemHeight = max(nItemHeight, infoPtr->iconSize.cy);
3006 nItemHeight += HEIGHT_PADDING;
3007 if (infoPtr->nMeasureItemHeight > 0)
3008 nItemHeight = infoPtr->nMeasureItemHeight;
3011 return max(nItemHeight, 1);
3014 /***
3015 * DESCRIPTION:
3016 * Updates the width, and height of an item.
3018 * PARAMETER(S):
3019 * [I] infoPtr : valid pointer to the listview structure
3021 * RETURN:
3022 * None.
3024 static inline void LISTVIEW_UpdateItemSize(LISTVIEW_INFO *infoPtr)
3026 infoPtr->nItemWidth = LISTVIEW_CalculateItemWidth(infoPtr);
3027 infoPtr->nItemHeight = LISTVIEW_CalculateItemHeight(infoPtr);
3031 /***
3032 * DESCRIPTION:
3033 * Retrieves and saves important text metrics info for the current
3034 * Listview font.
3036 * PARAMETER(S):
3037 * [I] infoPtr : valid pointer to the listview structure
3040 static void LISTVIEW_SaveTextMetrics(LISTVIEW_INFO *infoPtr)
3042 HDC hdc = GetDC(infoPtr->hwndSelf);
3043 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
3044 HFONT hOldFont = SelectObject(hdc, hFont);
3045 TEXTMETRICW tm;
3046 SIZE sz;
3048 if (GetTextMetricsW(hdc, &tm))
3050 infoPtr->ntmHeight = tm.tmHeight;
3051 infoPtr->ntmMaxCharWidth = tm.tmMaxCharWidth;
3054 if (GetTextExtentPoint32A(hdc, "...", 3, &sz))
3055 infoPtr->nEllipsisWidth = sz.cx;
3057 SelectObject(hdc, hOldFont);
3058 ReleaseDC(infoPtr->hwndSelf, hdc);
3060 TRACE("tmHeight=%d\n", infoPtr->ntmHeight);
3063 /***
3064 * DESCRIPTION:
3065 * A compare function for ranges
3067 * PARAMETER(S)
3068 * [I] range1 : pointer to range 1;
3069 * [I] range2 : pointer to range 2;
3070 * [I] flags : flags
3072 * RETURNS:
3073 * > 0 : if range 1 > range 2
3074 * < 0 : if range 2 > range 1
3075 * = 0 : if range intersects range 2
3077 static INT CALLBACK ranges_cmp(LPVOID range1, LPVOID range2, LPARAM flags)
3079 INT cmp;
3081 if (((RANGE*)range1)->upper <= ((RANGE*)range2)->lower)
3082 cmp = -1;
3083 else if (((RANGE*)range2)->upper <= ((RANGE*)range1)->lower)
3084 cmp = 1;
3085 else
3086 cmp = 0;
3088 TRACE("range1=%s, range2=%s, cmp=%d\n", debugrange(range1), debugrange(range2), cmp);
3090 return cmp;
3093 #define ranges_check(ranges, desc) if (TRACE_ON(listview)) ranges_assert(ranges, desc, __FILE__, __LINE__)
3095 static void ranges_assert(RANGES ranges, LPCSTR desc, const char *file, int line)
3097 INT i;
3098 RANGE *prev, *curr;
3100 TRACE("*** Checking %s:%d:%s ***\n", file, line, desc);
3101 assert (ranges);
3102 assert (DPA_GetPtrCount(ranges->hdpa) >= 0);
3103 ranges_dump(ranges);
3104 if (DPA_GetPtrCount(ranges->hdpa) > 0)
3106 prev = DPA_GetPtr(ranges->hdpa, 0);
3107 assert (prev->lower >= 0 && prev->lower < prev->upper);
3108 for (i = 1; i < DPA_GetPtrCount(ranges->hdpa); i++)
3110 curr = DPA_GetPtr(ranges->hdpa, i);
3111 assert (prev->upper <= curr->lower);
3112 assert (curr->lower < curr->upper);
3113 prev = curr;
3116 TRACE("--- Done checking---\n");
3119 static RANGES ranges_create(int count)
3121 RANGES ranges = Alloc(sizeof(*ranges));
3122 if (!ranges) return NULL;
3123 ranges->hdpa = DPA_Create(count);
3124 if (ranges->hdpa) return ranges;
3125 Free(ranges);
3126 return NULL;
3129 static void ranges_clear(RANGES ranges)
3131 INT i;
3133 for(i = 0; i < DPA_GetPtrCount(ranges->hdpa); i++)
3134 Free(DPA_GetPtr(ranges->hdpa, i));
3135 DPA_DeleteAllPtrs(ranges->hdpa);
3139 static void ranges_destroy(RANGES ranges)
3141 if (!ranges) return;
3142 ranges_clear(ranges);
3143 DPA_Destroy(ranges->hdpa);
3144 Free(ranges);
3147 static RANGES ranges_clone(RANGES ranges)
3149 RANGES clone;
3150 INT i;
3152 if (!(clone = ranges_create(DPA_GetPtrCount(ranges->hdpa)))) goto fail;
3154 for (i = 0; i < DPA_GetPtrCount(ranges->hdpa); i++)
3156 RANGE *newrng = Alloc(sizeof(*newrng));
3157 if (!newrng) goto fail;
3158 *newrng = *((RANGE*)DPA_GetPtr(ranges->hdpa, i));
3159 if (!DPA_SetPtr(clone->hdpa, i, newrng))
3161 Free(newrng);
3162 goto fail;
3165 return clone;
3167 fail:
3168 TRACE ("clone failed\n");
3169 ranges_destroy(clone);
3170 return NULL;
3173 static RANGES ranges_diff(RANGES ranges, RANGES sub)
3175 INT i;
3177 for (i = 0; i < DPA_GetPtrCount(sub->hdpa); i++)
3178 ranges_del(ranges, *((RANGE *)DPA_GetPtr(sub->hdpa, i)));
3180 return ranges;
3183 static void ranges_dump(RANGES ranges)
3185 INT i;
3187 for (i = 0; i < DPA_GetPtrCount(ranges->hdpa); i++)
3188 TRACE(" %s\n", debugrange(DPA_GetPtr(ranges->hdpa, i)));
3191 static inline BOOL ranges_contain(RANGES ranges, INT nItem)
3193 RANGE srchrng = { nItem, nItem + 1 };
3195 TRACE("(nItem=%d)\n", nItem);
3196 ranges_check(ranges, "before contain");
3197 return DPA_Search(ranges->hdpa, &srchrng, 0, ranges_cmp, 0, DPAS_SORTED) != -1;
3200 static INT ranges_itemcount(RANGES ranges)
3202 INT i, count = 0;
3204 for (i = 0; i < DPA_GetPtrCount(ranges->hdpa); i++)
3206 RANGE *sel = DPA_GetPtr(ranges->hdpa, i);
3207 count += sel->upper - sel->lower;
3210 return count;
3213 static BOOL ranges_shift(RANGES ranges, INT nItem, INT delta, INT nUpper)
3215 RANGE srchrng = { nItem, nItem + 1 }, *chkrng;
3216 INT index;
3218 index = DPA_Search(ranges->hdpa, &srchrng, 0, ranges_cmp, 0, DPAS_SORTED | DPAS_INSERTAFTER);
3219 if (index == -1) return TRUE;
3221 for (; index < DPA_GetPtrCount(ranges->hdpa); index++)
3223 chkrng = DPA_GetPtr(ranges->hdpa, index);
3224 if (chkrng->lower >= nItem)
3225 chkrng->lower = max(min(chkrng->lower + delta, nUpper - 1), 0);
3226 if (chkrng->upper > nItem)
3227 chkrng->upper = max(min(chkrng->upper + delta, nUpper), 0);
3229 return TRUE;
3232 static BOOL ranges_add(RANGES ranges, RANGE range)
3234 RANGE srchrgn;
3235 INT index;
3237 TRACE("(%s)\n", debugrange(&range));
3238 ranges_check(ranges, "before add");
3240 /* try find overlapping regions first */
3241 srchrgn.lower = range.lower - 1;
3242 srchrgn.upper = range.upper + 1;
3243 index = DPA_Search(ranges->hdpa, &srchrgn, 0, ranges_cmp, 0, DPAS_SORTED);
3245 if (index == -1)
3247 RANGE *newrgn;
3249 TRACE("Adding new range\n");
3251 /* create the brand new range to insert */
3252 newrgn = Alloc(sizeof(*newrgn));
3253 if(!newrgn) goto fail;
3254 *newrgn = range;
3256 /* figure out where to insert it */
3257 index = DPA_Search(ranges->hdpa, newrgn, 0, ranges_cmp, 0, DPAS_SORTED | DPAS_INSERTAFTER);
3258 TRACE("index=%d\n", index);
3259 if (index == -1) index = 0;
3261 /* and get it over with */
3262 if (DPA_InsertPtr(ranges->hdpa, index, newrgn) == -1)
3264 Free(newrgn);
3265 goto fail;
3268 else
3270 RANGE *chkrgn, *mrgrgn;
3271 INT fromindex, mergeindex;
3273 chkrgn = DPA_GetPtr(ranges->hdpa, index);
3274 TRACE("Merge with %s @%d\n", debugrange(chkrgn), index);
3276 chkrgn->lower = min(range.lower, chkrgn->lower);
3277 chkrgn->upper = max(range.upper, chkrgn->upper);
3279 TRACE("New range %s @%d\n", debugrange(chkrgn), index);
3281 /* merge now common ranges */
3282 fromindex = 0;
3283 srchrgn.lower = chkrgn->lower - 1;
3284 srchrgn.upper = chkrgn->upper + 1;
3288 mergeindex = DPA_Search(ranges->hdpa, &srchrgn, fromindex, ranges_cmp, 0, 0);
3289 if (mergeindex == -1) break;
3290 if (mergeindex == index)
3292 fromindex = index + 1;
3293 continue;
3296 TRACE("Merge with index %i\n", mergeindex);
3298 mrgrgn = DPA_GetPtr(ranges->hdpa, mergeindex);
3299 chkrgn->lower = min(chkrgn->lower, mrgrgn->lower);
3300 chkrgn->upper = max(chkrgn->upper, mrgrgn->upper);
3301 Free(mrgrgn);
3302 DPA_DeletePtr(ranges->hdpa, mergeindex);
3303 if (mergeindex < index) index --;
3304 } while(1);
3307 ranges_check(ranges, "after add");
3308 return TRUE;
3310 fail:
3311 ranges_check(ranges, "failed add");
3312 return FALSE;
3315 static BOOL ranges_del(RANGES ranges, RANGE range)
3317 RANGE *chkrgn;
3318 INT index;
3320 TRACE("(%s)\n", debugrange(&range));
3321 ranges_check(ranges, "before del");
3323 /* we don't use DPAS_SORTED here, since we need *
3324 * to find the first overlapping range */
3325 index = DPA_Search(ranges->hdpa, &range, 0, ranges_cmp, 0, 0);
3326 while(index != -1)
3328 chkrgn = DPA_GetPtr(ranges->hdpa, index);
3330 TRACE("Matches range %s @%d\n", debugrange(chkrgn), index);
3332 /* case 1: Same range */
3333 if ( (chkrgn->upper == range.upper) &&
3334 (chkrgn->lower == range.lower) )
3336 DPA_DeletePtr(ranges->hdpa, index);
3337 Free(chkrgn);
3338 break;
3340 /* case 2: engulf */
3341 else if ( (chkrgn->upper <= range.upper) &&
3342 (chkrgn->lower >= range.lower) )
3344 DPA_DeletePtr(ranges->hdpa, index);
3345 Free(chkrgn);
3347 /* case 3: overlap upper */
3348 else if ( (chkrgn->upper <= range.upper) &&
3349 (chkrgn->lower < range.lower) )
3351 chkrgn->upper = range.lower;
3353 /* case 4: overlap lower */
3354 else if ( (chkrgn->upper > range.upper) &&
3355 (chkrgn->lower >= range.lower) )
3357 chkrgn->lower = range.upper;
3358 break;
3360 /* case 5: fully internal */
3361 else
3363 RANGE *newrgn;
3365 if (!(newrgn = Alloc(sizeof(*newrgn)))) goto fail;
3366 newrgn->lower = chkrgn->lower;
3367 newrgn->upper = range.lower;
3368 chkrgn->lower = range.upper;
3369 if (DPA_InsertPtr(ranges->hdpa, index, newrgn) == -1)
3371 Free(newrgn);
3372 goto fail;
3374 break;
3377 index = DPA_Search(ranges->hdpa, &range, index, ranges_cmp, 0, 0);
3380 ranges_check(ranges, "after del");
3381 return TRUE;
3383 fail:
3384 ranges_check(ranges, "failed del");
3385 return FALSE;
3388 /***
3389 * DESCRIPTION:
3390 * Removes all selection ranges
3392 * Parameters(s):
3393 * [I] infoPtr : valid pointer to the listview structure
3394 * [I] toSkip : item range to skip removing the selection
3396 * RETURNS:
3397 * SUCCESS : TRUE
3398 * FAILURE : FALSE
3400 static BOOL LISTVIEW_DeselectAllSkipItems(LISTVIEW_INFO *infoPtr, RANGES toSkip)
3402 LVITEMW lvItem;
3403 ITERATOR i;
3404 RANGES clone;
3406 TRACE("()\n");
3408 lvItem.state = 0;
3409 lvItem.stateMask = LVIS_SELECTED;
3411 /* Only send one deselect all (-1) notification for LVS_OWNERDATA style */
3412 if (infoPtr->dwStyle & LVS_OWNERDATA)
3414 LISTVIEW_SetItemState(infoPtr, -1, &lvItem);
3415 return TRUE;
3418 /* need to clone the DPA because callbacks can change it */
3419 if (!(clone = ranges_clone(infoPtr->selectionRanges))) return FALSE;
3420 iterator_rangesitems(&i, ranges_diff(clone, toSkip));
3421 while(iterator_next(&i))
3422 LISTVIEW_SetItemState(infoPtr, i.nItem, &lvItem);
3423 /* note that the iterator destructor will free the cloned range */
3424 iterator_destroy(&i);
3426 return TRUE;
3429 static inline BOOL LISTVIEW_DeselectAllSkipItem(LISTVIEW_INFO *infoPtr, INT nItem)
3431 RANGES toSkip;
3433 if (!(toSkip = ranges_create(1))) return FALSE;
3434 if (nItem != -1) ranges_additem(toSkip, nItem);
3435 LISTVIEW_DeselectAllSkipItems(infoPtr, toSkip);
3436 ranges_destroy(toSkip);
3437 return TRUE;
3440 static inline BOOL LISTVIEW_DeselectAll(LISTVIEW_INFO *infoPtr)
3442 return LISTVIEW_DeselectAllSkipItem(infoPtr, -1);
3445 /***
3446 * DESCRIPTION:
3447 * Retrieves the number of items that are marked as selected.
3449 * PARAMETER(S):
3450 * [I] infoPtr : valid pointer to the listview structure
3452 * RETURN:
3453 * Number of items selected.
3455 static INT LISTVIEW_GetSelectedCount(const LISTVIEW_INFO *infoPtr)
3457 INT nSelectedCount = 0;
3459 if (infoPtr->uCallbackMask & LVIS_SELECTED)
3461 INT i;
3462 for (i = 0; i < infoPtr->nItemCount; i++)
3464 if (LISTVIEW_GetItemState(infoPtr, i, LVIS_SELECTED))
3465 nSelectedCount++;
3468 else
3469 nSelectedCount = ranges_itemcount(infoPtr->selectionRanges);
3471 TRACE("nSelectedCount=%d\n", nSelectedCount);
3472 return nSelectedCount;
3475 /***
3476 * DESCRIPTION:
3477 * Manages the item focus.
3479 * PARAMETER(S):
3480 * [I] infoPtr : valid pointer to the listview structure
3481 * [I] nItem : item index
3483 * RETURN:
3484 * TRUE : focused item changed
3485 * FALSE : focused item has NOT changed
3487 static inline BOOL LISTVIEW_SetItemFocus(LISTVIEW_INFO *infoPtr, INT nItem)
3489 INT oldFocus = infoPtr->nFocusedItem;
3490 LVITEMW lvItem;
3492 if (nItem == infoPtr->nFocusedItem) return FALSE;
3494 lvItem.state = nItem == -1 ? 0 : LVIS_FOCUSED;
3495 lvItem.stateMask = LVIS_FOCUSED;
3496 LISTVIEW_SetItemState(infoPtr, nItem == -1 ? infoPtr->nFocusedItem : nItem, &lvItem);
3498 return oldFocus != infoPtr->nFocusedItem;
3501 static INT shift_item(const LISTVIEW_INFO *infoPtr, INT nShiftItem, INT nItem, INT direction)
3503 if (nShiftItem < nItem) return nShiftItem;
3505 if (nShiftItem > nItem) return nShiftItem + direction;
3507 if (direction > 0) return nShiftItem + direction;
3509 return min(nShiftItem, infoPtr->nItemCount - 1);
3512 /* This function updates focus index.
3514 Parameters:
3515 focus : current focus index
3516 item : index of item to be added/removed
3517 direction : add/remove flag
3519 static void LISTVIEW_ShiftFocus(LISTVIEW_INFO *infoPtr, INT focus, INT item, INT direction)
3521 DWORD old_mask = infoPtr->notify_mask & NOTIFY_MASK_ITEM_CHANGE;
3523 infoPtr->notify_mask &= ~NOTIFY_MASK_ITEM_CHANGE;
3524 focus = shift_item(infoPtr, focus, item, direction);
3525 if (focus != infoPtr->nFocusedItem)
3526 LISTVIEW_SetItemFocus(infoPtr, focus);
3527 infoPtr->notify_mask |= old_mask;
3531 * DESCRIPTION:
3532 * Updates the various indices after an item has been inserted or deleted.
3534 * PARAMETER(S):
3535 * [I] infoPtr : valid pointer to the listview structure
3536 * [I] nItem : item index
3537 * [I] direction : Direction of shift, +1 or -1.
3539 * RETURN:
3540 * None
3542 static void LISTVIEW_ShiftIndices(LISTVIEW_INFO *infoPtr, INT nItem, INT direction)
3544 TRACE("Shifting %i, %i steps\n", nItem, direction);
3546 ranges_shift(infoPtr->selectionRanges, nItem, direction, infoPtr->nItemCount);
3547 assert(abs(direction) == 1);
3548 infoPtr->nSelectionMark = shift_item(infoPtr, infoPtr->nSelectionMark, nItem, direction);
3550 /* But we are not supposed to modify nHotItem! */
3554 * DESCRIPTION:
3555 * Adds a block of selections.
3557 * PARAMETER(S):
3558 * [I] infoPtr : valid pointer to the listview structure
3559 * [I] nItem : item index
3561 * RETURN:
3562 * Whether the window is still valid.
3564 static BOOL LISTVIEW_AddGroupSelection(LISTVIEW_INFO *infoPtr, INT nItem)
3566 INT nFirst = min(infoPtr->nSelectionMark, nItem);
3567 INT nLast = max(infoPtr->nSelectionMark, nItem);
3568 HWND hwndSelf = infoPtr->hwndSelf;
3569 DWORD old_mask;
3570 LVITEMW item;
3571 INT i;
3573 /* Temporarily disable change notification
3574 * If the control is LVS_OWNERDATA, we need to send
3575 * only one LVN_ODSTATECHANGED notification.
3576 * See MSDN documentation for LVN_ITEMCHANGED.
3578 old_mask = infoPtr->notify_mask & NOTIFY_MASK_ITEM_CHANGE;
3579 if (infoPtr->dwStyle & LVS_OWNERDATA)
3580 infoPtr->notify_mask &= ~NOTIFY_MASK_ITEM_CHANGE;
3582 if (nFirst == -1) nFirst = nItem;
3584 item.state = LVIS_SELECTED;
3585 item.stateMask = LVIS_SELECTED;
3587 for (i = nFirst; i <= nLast; i++)
3588 LISTVIEW_SetItemState(infoPtr,i,&item);
3590 if (infoPtr->dwStyle & LVS_OWNERDATA)
3591 LISTVIEW_SetOwnerDataState(infoPtr, nFirst, nLast, &item);
3593 if (!IsWindow(hwndSelf))
3594 return FALSE;
3595 infoPtr->notify_mask |= old_mask;
3596 return TRUE;
3600 /***
3601 * DESCRIPTION:
3602 * Sets a single group selection.
3604 * PARAMETER(S):
3605 * [I] infoPtr : valid pointer to the listview structure
3606 * [I] nItem : item index
3608 * RETURN:
3609 * None
3611 static void LISTVIEW_SetGroupSelection(LISTVIEW_INFO *infoPtr, INT nItem)
3613 INT nFirst = -1, nLast = -1;
3614 RANGES selection;
3615 DWORD old_mask;
3616 LVITEMW item;
3617 ITERATOR i;
3619 if (!(selection = ranges_create(100))) return;
3621 item.state = LVIS_SELECTED;
3622 item.stateMask = LVIS_SELECTED;
3624 if ((infoPtr->uView == LV_VIEW_LIST) || (infoPtr->uView == LV_VIEW_DETAILS))
3626 if (infoPtr->nSelectionMark == -1)
3628 infoPtr->nSelectionMark = nItem;
3629 ranges_additem(selection, nItem);
3631 else
3633 RANGE sel;
3635 sel.lower = min(infoPtr->nSelectionMark, nItem);
3636 sel.upper = max(infoPtr->nSelectionMark, nItem) + 1;
3637 ranges_add(selection, sel);
3640 else
3642 RECT rcItem, rcSel, rcSelMark;
3643 POINT ptItem;
3645 rcItem.left = LVIR_BOUNDS;
3646 if (!LISTVIEW_GetItemRect(infoPtr, nItem, &rcItem)) {
3647 ranges_destroy (selection);
3648 return;
3650 rcSelMark.left = LVIR_BOUNDS;
3651 if (!LISTVIEW_GetItemRect(infoPtr, infoPtr->nSelectionMark, &rcSelMark)) {
3652 ranges_destroy (selection);
3653 return;
3655 UnionRect(&rcSel, &rcItem, &rcSelMark);
3656 iterator_frameditems(&i, infoPtr, &rcSel);
3657 while(iterator_next(&i))
3659 LISTVIEW_GetItemPosition(infoPtr, i.nItem, &ptItem);
3660 if (PtInRect(&rcSel, ptItem)) ranges_additem(selection, i.nItem);
3662 iterator_destroy(&i);
3665 /* Disable per item notifications on LVS_OWNERDATA style */
3666 old_mask = infoPtr->notify_mask & NOTIFY_MASK_ITEM_CHANGE;
3667 if (infoPtr->dwStyle & LVS_OWNERDATA)
3668 infoPtr->notify_mask &= ~NOTIFY_MASK_ITEM_CHANGE;
3670 LISTVIEW_DeselectAllSkipItems(infoPtr, selection);
3672 iterator_rangesitems(&i, selection);
3673 while(iterator_next(&i))
3675 /* Find the range for LVN_ODSTATECHANGED */
3676 if (nFirst == -1)
3677 nFirst = i.nItem;
3678 nLast = i.nItem;
3679 LISTVIEW_SetItemState(infoPtr, i.nItem, &item);
3681 /* this will also destroy the selection */
3682 iterator_destroy(&i);
3684 if (infoPtr->dwStyle & LVS_OWNERDATA)
3685 LISTVIEW_SetOwnerDataState(infoPtr, nFirst, nLast, &item);
3687 infoPtr->notify_mask |= old_mask;
3688 LISTVIEW_SetItemFocus(infoPtr, nItem);
3691 /***
3692 * DESCRIPTION:
3693 * Sets a single selection.
3695 * PARAMETER(S):
3696 * [I] infoPtr : valid pointer to the listview structure
3697 * [I] nItem : item index
3699 * RETURN:
3700 * None
3702 static void LISTVIEW_SetSelection(LISTVIEW_INFO *infoPtr, INT nItem)
3704 LVITEMW lvItem;
3706 TRACE("nItem=%d\n", nItem);
3708 LISTVIEW_DeselectAllSkipItem(infoPtr, nItem);
3710 lvItem.state = LVIS_FOCUSED | LVIS_SELECTED;
3711 lvItem.stateMask = LVIS_FOCUSED | LVIS_SELECTED;
3712 LISTVIEW_SetItemState(infoPtr, nItem, &lvItem);
3714 infoPtr->nSelectionMark = nItem;
3717 /***
3718 * DESCRIPTION:
3719 * Set selection(s) with keyboard.
3721 * PARAMETER(S):
3722 * [I] infoPtr : valid pointer to the listview structure
3723 * [I] nItem : item index
3724 * [I] space : VK_SPACE code sent
3726 * RETURN:
3727 * SUCCESS : TRUE (needs to be repainted)
3728 * FAILURE : FALSE (nothing has changed)
3730 static BOOL LISTVIEW_KeySelection(LISTVIEW_INFO *infoPtr, INT nItem, BOOL space)
3732 /* FIXME: pass in the state */
3733 WORD wShift = GetKeyState(VK_SHIFT) & 0x8000;
3734 WORD wCtrl = GetKeyState(VK_CONTROL) & 0x8000;
3735 BOOL bResult = FALSE;
3737 TRACE("nItem=%d, wShift=%d, wCtrl=%d\n", nItem, wShift, wCtrl);
3738 if ((nItem >= 0) && (nItem < infoPtr->nItemCount))
3740 bResult = TRUE;
3742 if (infoPtr->dwStyle & LVS_SINGLESEL || (wShift == 0 && wCtrl == 0))
3743 LISTVIEW_SetSelection(infoPtr, nItem);
3744 else
3746 if (wShift)
3747 LISTVIEW_SetGroupSelection(infoPtr, nItem);
3748 else if (wCtrl)
3750 LVITEMW lvItem;
3751 lvItem.state = ~LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED);
3752 lvItem.stateMask = LVIS_SELECTED;
3753 if (space)
3755 LISTVIEW_SetItemState(infoPtr, nItem, &lvItem);
3756 if (lvItem.state & LVIS_SELECTED)
3757 infoPtr->nSelectionMark = nItem;
3759 bResult = LISTVIEW_SetItemFocus(infoPtr, nItem);
3762 LISTVIEW_EnsureVisible(infoPtr, nItem, FALSE);
3765 UpdateWindow(infoPtr->hwndSelf); /* update client area */
3766 return bResult;
3769 static BOOL LISTVIEW_GetItemAtPt(const LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem, POINT pt)
3771 LVHITTESTINFO lvHitTestInfo;
3773 ZeroMemory(&lvHitTestInfo, sizeof(lvHitTestInfo));
3774 lvHitTestInfo.pt.x = pt.x;
3775 lvHitTestInfo.pt.y = pt.y;
3777 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
3779 lpLVItem->mask = LVIF_PARAM;
3780 lpLVItem->iItem = lvHitTestInfo.iItem;
3781 lpLVItem->iSubItem = 0;
3783 return LISTVIEW_GetItemT(infoPtr, lpLVItem, TRUE);
3786 static inline BOOL LISTVIEW_IsHotTracking(const LISTVIEW_INFO *infoPtr)
3788 return ((infoPtr->dwLvExStyle & LVS_EX_TRACKSELECT) ||
3789 (infoPtr->dwLvExStyle & LVS_EX_ONECLICKACTIVATE) ||
3790 (infoPtr->dwLvExStyle & LVS_EX_TWOCLICKACTIVATE));
3793 /***
3794 * DESCRIPTION:
3795 * Called when the mouse is being actively tracked and has hovered for a specified
3796 * amount of time
3798 * PARAMETER(S):
3799 * [I] infoPtr : valid pointer to the listview structure
3800 * [I] fwKeys : key indicator
3801 * [I] x,y : mouse position
3803 * RETURN:
3804 * 0 if the message was processed, non-zero if there was an error
3806 * INFO:
3807 * LVS_EX_TRACKSELECT: An item is automatically selected when the cursor remains
3808 * over the item for a certain period of time.
3811 static LRESULT LISTVIEW_MouseHover(LISTVIEW_INFO *infoPtr, INT x, INT y)
3813 NMHDR hdr;
3815 if (notify_hdr(infoPtr, NM_HOVER, &hdr)) return 0;
3817 if (LISTVIEW_IsHotTracking(infoPtr))
3819 LVITEMW item;
3820 POINT pt;
3822 pt.x = x;
3823 pt.y = y;
3825 if (LISTVIEW_GetItemAtPt(infoPtr, &item, pt))
3826 LISTVIEW_SetSelection(infoPtr, item.iItem);
3828 SetFocus(infoPtr->hwndSelf);
3831 return 0;
3834 #define SCROLL_LEFT 0x1
3835 #define SCROLL_RIGHT 0x2
3836 #define SCROLL_UP 0x4
3837 #define SCROLL_DOWN 0x8
3839 /***
3840 * DESCRIPTION:
3841 * Utility routine to draw and highlight items within a marquee selection rectangle.
3843 * PARAMETER(S):
3844 * [I] infoPtr : valid pointer to the listview structure
3845 * [I] coords_orig : original co-ordinates of the cursor
3846 * [I] coords_offs : offsetted coordinates of the cursor
3847 * [I] offset : offset amount
3848 * [I] scroll : Bitmask of which directions we should scroll, if at all
3850 * RETURN:
3851 * None.
3853 static void LISTVIEW_MarqueeHighlight(LISTVIEW_INFO *infoPtr, const POINT *coords_orig,
3854 INT scroll)
3856 BOOL controlDown = FALSE;
3857 LVITEMW item;
3858 ITERATOR old_elems, new_elems;
3859 RECT rect;
3860 POINT coords_offs, offset;
3862 /* Ensure coordinates are within client bounds */
3863 coords_offs.x = max(min(coords_orig->x, infoPtr->rcList.right), 0);
3864 coords_offs.y = max(min(coords_orig->y, infoPtr->rcList.bottom), 0);
3866 /* Get offset */
3867 LISTVIEW_GetOrigin(infoPtr, &offset);
3869 /* Offset coordinates by the appropriate amount */
3870 coords_offs.x -= offset.x;
3871 coords_offs.y -= offset.y;
3873 if (coords_offs.x > infoPtr->marqueeOrigin.x)
3875 rect.left = infoPtr->marqueeOrigin.x;
3876 rect.right = coords_offs.x;
3878 else
3880 rect.left = coords_offs.x;
3881 rect.right = infoPtr->marqueeOrigin.x;
3884 if (coords_offs.y > infoPtr->marqueeOrigin.y)
3886 rect.top = infoPtr->marqueeOrigin.y;
3887 rect.bottom = coords_offs.y;
3889 else
3891 rect.top = coords_offs.y;
3892 rect.bottom = infoPtr->marqueeOrigin.y;
3895 /* Cancel out the old marquee rectangle and draw the new one */
3896 LISTVIEW_InvalidateRect(infoPtr, &infoPtr->marqueeDrawRect);
3898 /* Scroll by the appropriate distance if applicable - speed up scrolling as
3899 the cursor is further away */
3901 if ((scroll & SCROLL_LEFT) && (coords_orig->x <= 0))
3902 LISTVIEW_Scroll(infoPtr, coords_orig->x, 0);
3904 if ((scroll & SCROLL_RIGHT) && (coords_orig->x >= infoPtr->rcList.right))
3905 LISTVIEW_Scroll(infoPtr, (coords_orig->x - infoPtr->rcList.right), 0);
3907 if ((scroll & SCROLL_UP) && (coords_orig->y <= 0))
3908 LISTVIEW_Scroll(infoPtr, 0, coords_orig->y);
3910 if ((scroll & SCROLL_DOWN) && (coords_orig->y >= infoPtr->rcList.bottom))
3911 LISTVIEW_Scroll(infoPtr, 0, (coords_orig->y - infoPtr->rcList.bottom));
3913 iterator_frameditems_absolute(&old_elems, infoPtr, &infoPtr->marqueeRect);
3915 infoPtr->marqueeRect = rect;
3916 infoPtr->marqueeDrawRect = rect;
3917 OffsetRect(&infoPtr->marqueeDrawRect, offset.x, offset.y);
3919 iterator_frameditems_absolute(&new_elems, infoPtr, &infoPtr->marqueeRect);
3920 iterator_remove_common_items(&old_elems, &new_elems);
3922 /* Iterate over no longer selected items */
3923 while (iterator_next(&old_elems))
3925 if (old_elems.nItem > -1)
3927 if (LISTVIEW_GetItemState(infoPtr, old_elems.nItem, LVIS_SELECTED) == LVIS_SELECTED)
3928 item.state = 0;
3929 else
3930 item.state = LVIS_SELECTED;
3932 item.stateMask = LVIS_SELECTED;
3934 LISTVIEW_SetItemState(infoPtr, old_elems.nItem, &item);
3937 iterator_destroy(&old_elems);
3940 /* Iterate over newly selected items */
3941 if (GetKeyState(VK_CONTROL) & 0x8000)
3942 controlDown = TRUE;
3944 while (iterator_next(&new_elems))
3946 if (new_elems.nItem > -1)
3948 /* If CTRL is pressed, invert. If not, always select the item. */
3949 if ((controlDown) && (LISTVIEW_GetItemState(infoPtr, new_elems.nItem, LVIS_SELECTED)))
3950 item.state = 0;
3951 else
3952 item.state = LVIS_SELECTED;
3954 item.stateMask = LVIS_SELECTED;
3956 LISTVIEW_SetItemState(infoPtr, new_elems.nItem, &item);
3959 iterator_destroy(&new_elems);
3961 LISTVIEW_InvalidateRect(infoPtr, &infoPtr->marqueeDrawRect);
3964 /***
3965 * DESCRIPTION:
3966 * Called when we are in a marquee selection that involves scrolling the listview (ie,
3967 * the cursor is outside the bounds of the client area). This is a TIMERPROC.
3969 * PARAMETER(S):
3970 * [I] hwnd : Handle to the listview
3971 * [I] uMsg : WM_TIMER (ignored)
3972 * [I] idEvent : The timer ID interpreted as a pointer to a LISTVIEW_INFO struct
3973 * [I] dwTimer : The elapsed time (ignored)
3975 * RETURN:
3976 * None.
3978 static VOID CALLBACK LISTVIEW_ScrollTimer(HWND hWnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
3980 LISTVIEW_INFO *infoPtr;
3981 SCROLLINFO scrollInfo;
3982 POINT coords;
3983 INT scroll = 0;
3985 infoPtr = (LISTVIEW_INFO *) idEvent;
3987 if (!infoPtr)
3988 return;
3990 /* Get the current cursor position and convert to client coordinates */
3991 GetCursorPos(&coords);
3992 ScreenToClient(hWnd, &coords);
3994 scrollInfo.cbSize = sizeof(SCROLLINFO);
3995 scrollInfo.fMask = SIF_ALL;
3997 /* Work out in which directions we can scroll */
3998 if (GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
4000 if (scrollInfo.nPos != scrollInfo.nMin)
4001 scroll |= SCROLL_UP;
4003 if (((scrollInfo.nPage + scrollInfo.nPos) - 1) != scrollInfo.nMax)
4004 scroll |= SCROLL_DOWN;
4007 if (GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo))
4009 if (scrollInfo.nPos != scrollInfo.nMin)
4010 scroll |= SCROLL_LEFT;
4012 if (((scrollInfo.nPage + scrollInfo.nPos) - 1) != scrollInfo.nMax)
4013 scroll |= SCROLL_RIGHT;
4016 if (((coords.x <= 0) && (scroll & SCROLL_LEFT)) ||
4017 ((coords.y <= 0) && (scroll & SCROLL_UP)) ||
4018 ((coords.x >= infoPtr->rcList.right) && (scroll & SCROLL_RIGHT)) ||
4019 ((coords.y >= infoPtr->rcList.bottom) && (scroll & SCROLL_DOWN)))
4021 LISTVIEW_MarqueeHighlight(infoPtr, &coords, scroll);
4025 /***
4026 * DESCRIPTION:
4027 * Called whenever WM_MOUSEMOVE is received.
4029 * PARAMETER(S):
4030 * [I] infoPtr : valid pointer to the listview structure
4031 * [I] fwKeys : key indicator
4032 * [I] x,y : mouse position
4034 * RETURN:
4035 * 0 if the message is processed, non-zero if there was an error
4037 static LRESULT LISTVIEW_MouseMove(LISTVIEW_INFO *infoPtr, WORD fwKeys, INT x, INT y)
4039 LVHITTESTINFO ht;
4040 RECT rect;
4041 POINT pt;
4043 pt.x = x;
4044 pt.y = y;
4046 if (!(fwKeys & MK_LBUTTON))
4047 infoPtr->bLButtonDown = FALSE;
4049 if (infoPtr->bLButtonDown)
4051 rect.left = rect.right = infoPtr->ptClickPos.x;
4052 rect.top = rect.bottom = infoPtr->ptClickPos.y;
4054 InflateRect(&rect, GetSystemMetrics(SM_CXDRAG), GetSystemMetrics(SM_CYDRAG));
4056 if (infoPtr->bMarqueeSelect)
4058 /* Enable the timer if we're going outside our bounds, in case the user doesn't
4059 move the mouse again */
4061 if ((x <= 0) || (y <= 0) || (x >= infoPtr->rcList.right) ||
4062 (y >= infoPtr->rcList.bottom))
4064 if (!infoPtr->bScrolling)
4066 infoPtr->bScrolling = TRUE;
4067 SetTimer(infoPtr->hwndSelf, (UINT_PTR) infoPtr, 1, LISTVIEW_ScrollTimer);
4070 else
4072 infoPtr->bScrolling = FALSE;
4073 KillTimer(infoPtr->hwndSelf, (UINT_PTR) infoPtr);
4076 LISTVIEW_MarqueeHighlight(infoPtr, &pt, 0);
4077 return 0;
4080 ht.pt = pt;
4081 LISTVIEW_HitTest(infoPtr, &ht, TRUE, TRUE);
4083 /* reset item marker */
4084 if (infoPtr->nLButtonDownItem != ht.iItem)
4085 infoPtr->nLButtonDownItem = -1;
4087 if (!PtInRect(&rect, pt))
4089 /* this path covers the following:
4090 1. WM_LBUTTONDOWN over selected item (sets focus on it)
4091 2. change focus with keys
4092 3. move mouse over item from step 1 selects it and moves focus on it */
4093 if (infoPtr->nLButtonDownItem != -1 &&
4094 !LISTVIEW_GetItemState(infoPtr, infoPtr->nLButtonDownItem, LVIS_SELECTED))
4096 LVITEMW lvItem;
4098 lvItem.state = LVIS_FOCUSED | LVIS_SELECTED;
4099 lvItem.stateMask = LVIS_FOCUSED | LVIS_SELECTED;
4101 LISTVIEW_SetItemState(infoPtr, infoPtr->nLButtonDownItem, &lvItem);
4102 infoPtr->nLButtonDownItem = -1;
4105 if (!infoPtr->bDragging)
4107 ht.pt = infoPtr->ptClickPos;
4108 LISTVIEW_HitTest(infoPtr, &ht, TRUE, TRUE);
4110 /* If the click is outside the range of an item, begin a
4111 highlight. If not, begin an item drag. */
4112 if (ht.iItem == -1)
4114 NMHDR hdr;
4116 /* If we're allowing multiple selections, send notification.
4117 If return value is non-zero, cancel. */
4118 if (!(infoPtr->dwStyle & LVS_SINGLESEL) && (notify_hdr(infoPtr, LVN_MARQUEEBEGIN, &hdr) == 0))
4120 /* Store the absolute coordinates of the click */
4121 POINT offset;
4122 LISTVIEW_GetOrigin(infoPtr, &offset);
4124 infoPtr->marqueeOrigin.x = infoPtr->ptClickPos.x - offset.x;
4125 infoPtr->marqueeOrigin.y = infoPtr->ptClickPos.y - offset.y;
4127 /* Begin selection and capture mouse */
4128 infoPtr->bMarqueeSelect = TRUE;
4129 infoPtr->marqueeRect = rect;
4130 SetCapture(infoPtr->hwndSelf);
4133 else
4135 NMLISTVIEW nmlv;
4137 ZeroMemory(&nmlv, sizeof(nmlv));
4138 nmlv.iItem = ht.iItem;
4139 nmlv.ptAction = infoPtr->ptClickPos;
4141 notify_listview(infoPtr, LVN_BEGINDRAG, &nmlv);
4142 infoPtr->bDragging = TRUE;
4146 return 0;
4150 /* see if we are supposed to be tracking mouse hovering */
4151 if (LISTVIEW_IsHotTracking(infoPtr)) {
4152 TRACKMOUSEEVENT trackinfo;
4153 DWORD flags;
4155 trackinfo.cbSize = sizeof(TRACKMOUSEEVENT);
4156 trackinfo.dwFlags = TME_QUERY;
4158 /* see if we are already tracking this hwnd */
4159 _TrackMouseEvent(&trackinfo);
4161 flags = TME_LEAVE;
4162 if(infoPtr->dwLvExStyle & LVS_EX_TRACKSELECT)
4163 flags |= TME_HOVER;
4165 if((trackinfo.dwFlags & flags) != flags || trackinfo.hwndTrack != infoPtr->hwndSelf) {
4166 trackinfo.dwFlags = flags;
4167 trackinfo.dwHoverTime = infoPtr->dwHoverTime;
4168 trackinfo.hwndTrack = infoPtr->hwndSelf;
4170 /* call TRACKMOUSEEVENT so we receive WM_MOUSEHOVER messages */
4171 _TrackMouseEvent(&trackinfo);
4175 return 0;
4179 /***
4180 * Tests whether the item is assignable to a list with style lStyle
4182 static inline BOOL is_assignable_item(const LVITEMW *lpLVItem, LONG lStyle)
4184 if ( (lpLVItem->mask & LVIF_TEXT) &&
4185 (lpLVItem->pszText == LPSTR_TEXTCALLBACKW) &&
4186 (lStyle & (LVS_SORTASCENDING | LVS_SORTDESCENDING)) ) return FALSE;
4188 return TRUE;
4192 /***
4193 * DESCRIPTION:
4194 * Helper for LISTVIEW_SetItemT and LISTVIEW_InsertItemT: sets item attributes.
4196 * PARAMETER(S):
4197 * [I] infoPtr : valid pointer to the listview structure
4198 * [I] lpLVItem : valid pointer to new item attributes
4199 * [I] isNew : the item being set is being inserted
4200 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
4201 * [O] bChanged : will be set to TRUE if the item really changed
4203 * RETURN:
4204 * SUCCESS : TRUE
4205 * FAILURE : FALSE
4207 static BOOL set_main_item(LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem, BOOL isNew, BOOL isW, BOOL *bChanged)
4209 ITEM_INFO *lpItem;
4210 NMLISTVIEW nmlv;
4211 UINT uChanged = 0;
4212 LVITEMW item;
4213 /* stateMask is ignored for LVM_INSERTITEM */
4214 UINT stateMask = isNew ? ~0 : lpLVItem->stateMask;
4215 BOOL send_change_notification;
4217 TRACE("()\n");
4219 assert(lpLVItem->iItem >= 0 && lpLVItem->iItem < infoPtr->nItemCount);
4221 if (lpLVItem->mask == 0) return TRUE;
4223 if (infoPtr->dwStyle & LVS_OWNERDATA)
4225 /* a virtual listview only stores selection and focus */
4226 if (lpLVItem->mask & ~LVIF_STATE)
4227 return FALSE;
4228 lpItem = NULL;
4230 else
4232 HDPA hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, lpLVItem->iItem);
4233 lpItem = DPA_GetPtr(hdpaSubItems, 0);
4234 assert (lpItem);
4237 /* we need to get the lParam and state of the item */
4238 item.iItem = lpLVItem->iItem;
4239 item.iSubItem = lpLVItem->iSubItem;
4240 item.mask = LVIF_STATE | LVIF_PARAM;
4241 item.stateMask = (infoPtr->dwStyle & LVS_OWNERDATA) ? LVIS_FOCUSED | LVIS_SELECTED : ~0;
4243 item.state = 0;
4244 item.lParam = 0;
4245 if (!isNew && !LISTVIEW_GetItemW(infoPtr, &item)) return FALSE;
4247 TRACE("oldState=%x, newState=%x\n", item.state, lpLVItem->state);
4248 /* determine what fields will change */
4249 if ((lpLVItem->mask & LVIF_STATE) && ((item.state ^ lpLVItem->state) & stateMask & ~infoPtr->uCallbackMask))
4250 uChanged |= LVIF_STATE;
4252 if ((lpLVItem->mask & LVIF_IMAGE) && (lpItem->hdr.iImage != lpLVItem->iImage))
4253 uChanged |= LVIF_IMAGE;
4255 if ((lpLVItem->mask & LVIF_PARAM) && (lpItem->lParam != lpLVItem->lParam))
4256 uChanged |= LVIF_PARAM;
4258 if ((lpLVItem->mask & LVIF_INDENT) && (lpItem->iIndent != lpLVItem->iIndent))
4259 uChanged |= LVIF_INDENT;
4261 if ((lpLVItem->mask & LVIF_TEXT) && textcmpWT(lpItem->hdr.pszText, lpLVItem->pszText, isW))
4262 uChanged |= LVIF_TEXT;
4264 TRACE("change mask=0x%x\n", uChanged);
4266 memset(&nmlv, 0, sizeof(NMLISTVIEW));
4267 nmlv.iItem = lpLVItem->iItem;
4268 if (lpLVItem->mask & LVIF_STATE)
4270 nmlv.uNewState = (item.state & ~stateMask) | (lpLVItem->state & stateMask);
4271 nmlv.uOldState = item.state;
4273 nmlv.uChanged = isNew ? LVIF_STATE : (uChanged ? uChanged : lpLVItem->mask);
4274 nmlv.lParam = item.lParam;
4276 /* Send change notification if the item is not being inserted, or inserted (selected|focused),
4277 and we are _NOT_ virtual (LVS_OWNERDATA), and change notifications
4278 are enabled. Even if nothing really changed we still need to send this,
4279 in this case uChanged mask is just set to passed item mask. */
4281 send_change_notification = !isNew;
4282 send_change_notification |= (uChanged & LVIF_STATE) && (lpLVItem->state & (LVIS_FOCUSED | LVIS_SELECTED));
4283 send_change_notification &= !!(infoPtr->notify_mask & NOTIFY_MASK_ITEM_CHANGE);
4285 if (lpItem && send_change_notification)
4287 HWND hwndSelf = infoPtr->hwndSelf;
4289 if (notify_listview(infoPtr, LVN_ITEMCHANGING, &nmlv))
4290 return FALSE;
4291 if (!IsWindow(hwndSelf))
4292 return FALSE;
4295 /* When item is inserted we need to shift existing focus index if new item has lower index. */
4296 if (isNew && (stateMask & ~infoPtr->uCallbackMask & LVIS_FOCUSED) &&
4297 /* this means we won't hit a focus change path later */
4298 ((uChanged & LVIF_STATE) == 0 || (!(lpLVItem->state & LVIS_FOCUSED) && (infoPtr->nFocusedItem != lpLVItem->iItem))))
4300 if (infoPtr->nFocusedItem != -1 && (lpLVItem->iItem <= infoPtr->nFocusedItem))
4301 infoPtr->nFocusedItem++;
4304 if (!uChanged) return TRUE;
4305 *bChanged = TRUE;
4307 /* copy information */
4308 if (lpLVItem->mask & LVIF_TEXT)
4309 textsetptrT(&lpItem->hdr.pszText, lpLVItem->pszText, isW);
4311 if (lpLVItem->mask & LVIF_IMAGE)
4312 lpItem->hdr.iImage = lpLVItem->iImage;
4314 if (lpLVItem->mask & LVIF_PARAM)
4315 lpItem->lParam = lpLVItem->lParam;
4317 if (lpLVItem->mask & LVIF_INDENT)
4318 lpItem->iIndent = lpLVItem->iIndent;
4320 if (uChanged & LVIF_STATE)
4322 if (lpItem && (stateMask & ~infoPtr->uCallbackMask))
4324 lpItem->state &= ~stateMask;
4325 lpItem->state |= (lpLVItem->state & stateMask);
4327 if (lpLVItem->state & stateMask & ~infoPtr->uCallbackMask & LVIS_SELECTED)
4329 if (infoPtr->dwStyle & LVS_SINGLESEL) LISTVIEW_DeselectAllSkipItem(infoPtr, lpLVItem->iItem);
4330 ranges_additem(infoPtr->selectionRanges, lpLVItem->iItem);
4332 else if (stateMask & LVIS_SELECTED)
4334 ranges_delitem(infoPtr->selectionRanges, lpLVItem->iItem);
4336 /* If we are asked to change focus, and we manage it, do it.
4337 It's important to have all new item data stored at this point,
4338 because changing existing focus could result in a redrawing operation,
4339 which in turn could ask for disp data, application should see all data
4340 for inserted item when processing LVN_GETDISPINFO.
4342 The way this works application will see nested item change notifications -
4343 changed item notifications interrupted by ones from item losing focus. */
4344 if (stateMask & ~infoPtr->uCallbackMask & LVIS_FOCUSED)
4346 if (lpLVItem->state & LVIS_FOCUSED)
4348 /* update selection mark */
4349 if (infoPtr->nFocusedItem == -1 && infoPtr->nSelectionMark == -1)
4350 infoPtr->nSelectionMark = lpLVItem->iItem;
4352 if (infoPtr->nFocusedItem != -1)
4354 /* remove current focus */
4355 item.mask = LVIF_STATE;
4356 item.state = 0;
4357 item.stateMask = LVIS_FOCUSED;
4359 /* recurse with redrawing an item */
4360 LISTVIEW_SetItemState(infoPtr, infoPtr->nFocusedItem, &item);
4363 infoPtr->nFocusedItem = lpLVItem->iItem;
4364 LISTVIEW_EnsureVisible(infoPtr, lpLVItem->iItem, infoPtr->uView == LV_VIEW_LIST);
4366 else if (infoPtr->nFocusedItem == lpLVItem->iItem)
4368 infoPtr->nFocusedItem = -1;
4373 if (send_change_notification)
4375 if (lpLVItem->mask & LVIF_PARAM) nmlv.lParam = lpLVItem->lParam;
4376 notify_listview(infoPtr, LVN_ITEMCHANGED, &nmlv);
4379 return TRUE;
4382 /***
4383 * DESCRIPTION:
4384 * Helper for LISTVIEW_{Set,Insert}ItemT *only*: sets subitem attributes.
4386 * PARAMETER(S):
4387 * [I] infoPtr : valid pointer to the listview structure
4388 * [I] lpLVItem : valid pointer to new subitem attributes
4389 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
4390 * [O] bChanged : will be set to TRUE if the item really changed
4392 * RETURN:
4393 * SUCCESS : TRUE
4394 * FAILURE : FALSE
4396 static BOOL set_sub_item(const LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem, BOOL isW, BOOL *bChanged)
4398 HDPA hdpaSubItems;
4399 SUBITEM_INFO *lpSubItem;
4401 /* we do not support subitems for virtual listviews */
4402 if (infoPtr->dwStyle & LVS_OWNERDATA) return FALSE;
4404 /* set subitem only if column is present */
4405 if (lpLVItem->iSubItem >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
4407 /* First do some sanity checks */
4408 /* The LVIF_STATE flag is valid for subitems, but does not appear to be
4409 particularly useful. We currently do not actually do anything with
4410 the flag on subitems.
4412 if (lpLVItem->mask & ~(LVIF_TEXT | LVIF_IMAGE | LVIF_STATE | LVIF_DI_SETITEM)) return FALSE;
4413 if (!(lpLVItem->mask & (LVIF_TEXT | LVIF_IMAGE | LVIF_STATE))) return TRUE;
4415 /* get the subitem structure, and create it if not there */
4416 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, lpLVItem->iItem);
4417 assert (hdpaSubItems);
4419 lpSubItem = LISTVIEW_GetSubItemPtr(hdpaSubItems, lpLVItem->iSubItem);
4420 if (!lpSubItem)
4422 SUBITEM_INFO *tmpSubItem;
4423 INT i;
4425 lpSubItem = Alloc(sizeof(*lpSubItem));
4426 if (!lpSubItem) return FALSE;
4427 /* we could binary search here, if need be...*/
4428 for (i = 1; i < DPA_GetPtrCount(hdpaSubItems); i++)
4430 tmpSubItem = DPA_GetPtr(hdpaSubItems, i);
4431 if (tmpSubItem->iSubItem > lpLVItem->iSubItem) break;
4433 if (DPA_InsertPtr(hdpaSubItems, i, lpSubItem) == -1)
4435 Free(lpSubItem);
4436 return FALSE;
4438 lpSubItem->iSubItem = lpLVItem->iSubItem;
4439 lpSubItem->hdr.iImage = I_IMAGECALLBACK;
4440 *bChanged = TRUE;
4443 if ((lpLVItem->mask & LVIF_IMAGE) && (lpSubItem->hdr.iImage != lpLVItem->iImage))
4445 lpSubItem->hdr.iImage = lpLVItem->iImage;
4446 *bChanged = TRUE;
4449 if ((lpLVItem->mask & LVIF_TEXT) && textcmpWT(lpSubItem->hdr.pszText, lpLVItem->pszText, isW))
4451 textsetptrT(&lpSubItem->hdr.pszText, lpLVItem->pszText, isW);
4452 *bChanged = TRUE;
4455 return TRUE;
4458 /***
4459 * DESCRIPTION:
4460 * Sets item attributes.
4462 * PARAMETER(S):
4463 * [I] infoPtr : valid pointer to the listview structure
4464 * [I] lpLVItem : new item attributes
4465 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
4467 * RETURN:
4468 * SUCCESS : TRUE
4469 * FAILURE : FALSE
4471 static BOOL LISTVIEW_SetItemT(LISTVIEW_INFO *infoPtr, LVITEMW *lpLVItem, BOOL isW)
4473 HWND hwndSelf = infoPtr->hwndSelf;
4474 LPWSTR pszText = NULL;
4475 BOOL bResult, bChanged = FALSE;
4476 RECT oldItemArea;
4478 TRACE("(lpLVItem=%s, isW=%d)\n", debuglvitem_t(lpLVItem, isW), isW);
4480 if (!lpLVItem || lpLVItem->iItem < 0 || lpLVItem->iItem >= infoPtr->nItemCount)
4481 return FALSE;
4483 /* Store old item area */
4484 LISTVIEW_GetItemBox(infoPtr, lpLVItem->iItem, &oldItemArea);
4486 /* For efficiency, we transform the lpLVItem->pszText to Unicode here */
4487 if ((lpLVItem->mask & LVIF_TEXT) && is_text(lpLVItem->pszText))
4489 pszText = lpLVItem->pszText;
4490 lpLVItem->pszText = textdupTtoW(lpLVItem->pszText, isW);
4493 /* actually set the fields */
4494 if (!is_assignable_item(lpLVItem, infoPtr->dwStyle)) return FALSE;
4496 if (lpLVItem->iSubItem)
4497 bResult = set_sub_item(infoPtr, lpLVItem, TRUE, &bChanged);
4498 else
4499 bResult = set_main_item(infoPtr, lpLVItem, FALSE, TRUE, &bChanged);
4500 if (!IsWindow(hwndSelf))
4501 return FALSE;
4503 /* redraw item, if necessary */
4504 if (bChanged && !infoPtr->bIsDrawing)
4506 /* this little optimization eliminates some nasty flicker */
4507 if ( infoPtr->uView == LV_VIEW_DETAILS && !(infoPtr->dwStyle & LVS_OWNERDRAWFIXED) &&
4508 !(infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) &&
4509 lpLVItem->iSubItem > 0 && lpLVItem->iSubItem <= DPA_GetPtrCount(infoPtr->hdpaColumns) )
4510 LISTVIEW_InvalidateSubItem(infoPtr, lpLVItem->iItem, lpLVItem->iSubItem);
4511 else
4513 LISTVIEW_InvalidateRect(infoPtr, &oldItemArea);
4514 LISTVIEW_InvalidateItem(infoPtr, lpLVItem->iItem);
4517 /* restore text */
4518 if (pszText)
4520 textfreeT(lpLVItem->pszText, isW);
4521 lpLVItem->pszText = pszText;
4524 return bResult;
4527 /***
4528 * DESCRIPTION:
4529 * Retrieves the index of the item at coordinate (0, 0) of the client area.
4531 * PARAMETER(S):
4532 * [I] infoPtr : valid pointer to the listview structure
4534 * RETURN:
4535 * item index
4537 static INT LISTVIEW_GetTopIndex(const LISTVIEW_INFO *infoPtr)
4539 INT nItem = 0;
4540 SCROLLINFO scrollInfo;
4542 scrollInfo.cbSize = sizeof(SCROLLINFO);
4543 scrollInfo.fMask = SIF_POS;
4545 if (infoPtr->uView == LV_VIEW_LIST)
4547 if (GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo))
4548 nItem = scrollInfo.nPos * LISTVIEW_GetCountPerColumn(infoPtr);
4550 else if (infoPtr->uView == LV_VIEW_DETAILS)
4552 if (GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
4553 nItem = scrollInfo.nPos;
4555 else
4557 if (GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
4558 nItem = LISTVIEW_GetCountPerRow(infoPtr) * (scrollInfo.nPos / infoPtr->nItemHeight);
4561 TRACE("nItem=%d\n", nItem);
4563 return nItem;
4566 static void LISTVIEW_DrawBackgroundBitmap(const LISTVIEW_INFO *infoPtr, HDC hdc, const RECT *lprcBox)
4568 HDC mem_hdc;
4570 if (!infoPtr->hBkBitmap)
4571 return;
4573 TRACE("(hdc=%p, lprcBox=%s, hBkBitmap=%p)\n", hdc, wine_dbgstr_rect(lprcBox), infoPtr->hBkBitmap);
4575 mem_hdc = CreateCompatibleDC(hdc);
4576 SelectObject(mem_hdc, infoPtr->hBkBitmap);
4577 BitBlt(hdc, lprcBox->left, lprcBox->top, lprcBox->right - lprcBox->left,
4578 lprcBox->bottom - lprcBox->top, mem_hdc, lprcBox->left, lprcBox->top, SRCCOPY);
4579 DeleteDC(mem_hdc);
4582 /***
4583 * DESCRIPTION:
4584 * Erases the background of the given rectangle
4586 * PARAMETER(S):
4587 * [I] infoPtr : valid pointer to the listview structure
4588 * [I] hdc : device context handle
4589 * [I] lprcBox : clipping rectangle
4591 * RETURN:
4592 * Success: TRUE
4593 * Failure: FALSE
4595 static BOOL LISTVIEW_FillBkgnd(const LISTVIEW_INFO *infoPtr, HDC hdc, const RECT *lprcBox)
4597 if (infoPtr->hBkBrush)
4599 TRACE("(hdc=%p, lprcBox=%s, hBkBrush=%p)\n", hdc, wine_dbgstr_rect(lprcBox), infoPtr->hBkBrush);
4601 FillRect(hdc, lprcBox, infoPtr->hBkBrush);
4604 LISTVIEW_DrawBackgroundBitmap(infoPtr, hdc, lprcBox);
4605 return TRUE;
4608 /* Draw main item or subitem */
4609 static void LISTVIEW_DrawItemPart(LISTVIEW_INFO *infoPtr, LVITEMW *item, const NMLVCUSTOMDRAW *nmlvcd, const POINT *pos)
4611 RECT rcSelect, rcLabel, rcBox, rcStateIcon, rcIcon;
4612 const RECT *background;
4613 HIMAGELIST himl;
4614 UINT format;
4615 RECT *focus;
4617 /* now check if we need to update the focus rectangle */
4618 focus = infoPtr->bFocus && (item->state & LVIS_FOCUSED) ? &infoPtr->rcFocus : 0;
4619 if (!focus) item->state &= ~LVIS_FOCUSED;
4621 LISTVIEW_GetItemMetrics(infoPtr, item, &rcBox, &rcSelect, &rcIcon, &rcStateIcon, &rcLabel);
4622 OffsetRect(&rcBox, pos->x, pos->y);
4623 OffsetRect(&rcSelect, pos->x, pos->y);
4624 OffsetRect(&rcIcon, pos->x, pos->y);
4625 OffsetRect(&rcStateIcon, pos->x, pos->y);
4626 OffsetRect(&rcLabel, pos->x, pos->y);
4627 TRACE("%d: box=%s, select=%s, icon=%s. label=%s\n", item->iSubItem,
4628 wine_dbgstr_rect(&rcBox), wine_dbgstr_rect(&rcSelect),
4629 wine_dbgstr_rect(&rcIcon), wine_dbgstr_rect(&rcLabel));
4631 /* FIXME: temporary hack */
4632 rcSelect.left = rcLabel.left;
4634 if (infoPtr->uView == LV_VIEW_DETAILS && item->iSubItem == 0)
4636 if (!(infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT))
4637 OffsetRect(&rcSelect, LISTVIEW_GetColumnInfo(infoPtr, 0)->rcHeader.left, 0);
4638 OffsetRect(&rcIcon, LISTVIEW_GetColumnInfo(infoPtr, 0)->rcHeader.left, 0);
4639 OffsetRect(&rcStateIcon, LISTVIEW_GetColumnInfo(infoPtr, 0)->rcHeader.left, 0);
4640 OffsetRect(&rcLabel, LISTVIEW_GetColumnInfo(infoPtr, 0)->rcHeader.left, 0);
4643 /* in icon mode, the label rect is really what we want to draw the
4644 * background for */
4645 /* in detail mode, we want to paint background for label rect when
4646 * item is not selected or listview has full row select; otherwise paint
4647 * background for text only */
4648 if ( infoPtr->uView == LV_VIEW_ICON ||
4649 (infoPtr->uView == LV_VIEW_DETAILS && (!(item->state & LVIS_SELECTED) ||
4650 (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT))))
4651 background = &rcLabel;
4652 else
4653 background = &rcSelect;
4655 if (nmlvcd->clrTextBk != CLR_NONE)
4656 ExtTextOutW(nmlvcd->nmcd.hdc, background->left, background->top, ETO_OPAQUE, background, NULL, 0, NULL);
4658 if (item->state & LVIS_FOCUSED)
4660 if (infoPtr->uView == LV_VIEW_DETAILS)
4662 if (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT)
4664 /* we have to update left focus bound too if item isn't in leftmost column
4665 and reduce right box bound */
4666 if (DPA_GetPtrCount(infoPtr->hdpaColumns) > 0)
4668 INT leftmost;
4670 if ((leftmost = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, 0, 0)))
4672 INT Originx = pos->x - LISTVIEW_GetColumnInfo(infoPtr, leftmost)->rcHeader.left;
4673 INT rightmost = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX,
4674 DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, 0);
4676 rcBox.right = LISTVIEW_GetColumnInfo(infoPtr, rightmost)->rcHeader.right + Originx;
4677 rcSelect.left = LISTVIEW_GetColumnInfo(infoPtr, leftmost)->rcHeader.left + Originx;
4680 rcSelect.right = rcBox.right;
4682 infoPtr->rcFocus = rcSelect;
4684 else
4685 infoPtr->rcFocus = rcLabel;
4688 /* state icons */
4689 if (infoPtr->himlState && STATEIMAGEINDEX(item->state) && (item->iSubItem == 0))
4691 UINT stateimage = STATEIMAGEINDEX(item->state);
4692 if (stateimage)
4694 TRACE("stateimage=%d\n", stateimage);
4695 ImageList_Draw(infoPtr->himlState, stateimage-1, nmlvcd->nmcd.hdc, rcStateIcon.left, rcStateIcon.top, ILD_NORMAL);
4699 /* item icons */
4700 himl = (infoPtr->uView == LV_VIEW_ICON ? infoPtr->himlNormal : infoPtr->himlSmall);
4701 if (himl && item->iImage >= 0 && !IsRectEmpty(&rcIcon))
4703 UINT style;
4705 TRACE("iImage=%d\n", item->iImage);
4707 if (item->state & (LVIS_SELECTED | LVIS_CUT) && infoPtr->bFocus)
4708 style = ILD_SELECTED;
4709 else
4710 style = ILD_NORMAL;
4712 ImageList_DrawEx(himl, item->iImage, nmlvcd->nmcd.hdc, rcIcon.left, rcIcon.top,
4713 rcIcon.right - rcIcon.left, rcIcon.bottom - rcIcon.top, infoPtr->clrBk,
4714 item->state & LVIS_CUT ? RGB(255, 255, 255) : CLR_DEFAULT,
4715 style | (item->state & LVIS_OVERLAYMASK));
4718 /* Don't bother painting item being edited */
4719 if (infoPtr->hwndEdit && item->iItem == infoPtr->nEditLabelItem && item->iSubItem == 0) return;
4721 /* figure out the text drawing flags */
4722 format = (infoPtr->uView == LV_VIEW_ICON ? (focus ? LV_FL_DT_FLAGS : LV_ML_DT_FLAGS) : LV_SL_DT_FLAGS);
4723 if (infoPtr->uView == LV_VIEW_ICON)
4724 format = (focus ? LV_FL_DT_FLAGS : LV_ML_DT_FLAGS);
4725 else if (item->iSubItem)
4727 switch (LISTVIEW_GetColumnInfo(infoPtr, item->iSubItem)->fmt & LVCFMT_JUSTIFYMASK)
4729 case LVCFMT_RIGHT: format |= DT_RIGHT; break;
4730 case LVCFMT_CENTER: format |= DT_CENTER; break;
4731 default: format |= DT_LEFT;
4734 if (!(format & (DT_RIGHT | DT_CENTER)))
4736 if (himl && item->iImage >= 0 && !IsRectEmpty(&rcIcon)) rcLabel.left += IMAGE_PADDING;
4737 else rcLabel.left += LABEL_HOR_PADDING;
4739 else if (format & DT_RIGHT) rcLabel.right -= LABEL_HOR_PADDING;
4741 /* for GRIDLINES reduce the bottom so the text formats correctly */
4742 if (infoPtr->uView == LV_VIEW_DETAILS && infoPtr->dwLvExStyle & LVS_EX_GRIDLINES)
4743 rcLabel.bottom--;
4745 DrawTextW(nmlvcd->nmcd.hdc, item->pszText, -1, &rcLabel, format);
4748 /***
4749 * DESCRIPTION:
4750 * Draws an item.
4752 * PARAMETER(S):
4753 * [I] infoPtr : valid pointer to the listview structure
4754 * [I] hdc : device context handle
4755 * [I] nItem : item index
4756 * [I] nSubItem : subitem index
4757 * [I] pos : item position in client coordinates
4758 * [I] cdmode : custom draw mode
4760 * RETURN:
4761 * Success: TRUE
4762 * Failure: FALSE
4764 static BOOL LISTVIEW_DrawItem(LISTVIEW_INFO *infoPtr, HDC hdc, INT nItem, ITERATOR *subitems, POINT pos, DWORD cdmode)
4766 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
4767 static WCHAR callbackW[] = L"(callback)";
4768 DWORD cdsubitemmode = CDRF_DODEFAULT;
4769 RECT *focus, rcBox;
4770 NMLVCUSTOMDRAW nmlvcd;
4771 LVITEMW lvItem;
4773 TRACE("(hdc=%p, nItem=%d, subitems=%p, pos=%s)\n", hdc, nItem, subitems, wine_dbgstr_point(&pos));
4775 /* get information needed for drawing the item */
4776 lvItem.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_PARAM | LVIF_STATE;
4777 if (infoPtr->uView == LV_VIEW_DETAILS) lvItem.mask |= LVIF_INDENT;
4778 lvItem.stateMask = LVIS_SELECTED | LVIS_FOCUSED | LVIS_STATEIMAGEMASK | LVIS_CUT | LVIS_OVERLAYMASK;
4779 lvItem.iItem = nItem;
4780 lvItem.iSubItem = 0;
4781 lvItem.state = 0;
4782 lvItem.lParam = 0;
4783 lvItem.cchTextMax = DISP_TEXT_SIZE;
4784 lvItem.pszText = szDispText;
4785 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return FALSE;
4786 if (lvItem.pszText == LPSTR_TEXTCALLBACKW) lvItem.pszText = callbackW;
4787 TRACE(" lvItem=%s\n", debuglvitem_t(&lvItem, TRUE));
4789 /* now check if we need to update the focus rectangle */
4790 focus = infoPtr->bFocus && (lvItem.state & LVIS_FOCUSED) ? &infoPtr->rcFocus : 0;
4791 if (!focus) lvItem.state &= ~LVIS_FOCUSED;
4793 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, &rcBox, NULL, NULL, NULL, NULL);
4794 OffsetRect(&rcBox, pos.x, pos.y);
4796 /* Full custom draw stage sequence looks like this:
4798 LV_VIEW_DETAILS:
4800 - CDDS_ITEMPREPAINT
4801 - CDDS_ITEMPREPAINT|CDDS_SUBITEM | => sent n times, where n is number of subitems,
4802 CDDS_ITEMPOSTPAINT|CDDS_SUBITEM | including item itself
4803 - CDDS_ITEMPOSTPAINT
4805 other styles:
4807 - CDDS_ITEMPREPAINT
4808 - CDDS_ITEMPOSTPAINT
4811 /* fill in the custom draw structure */
4812 customdraw_fill(&nmlvcd, infoPtr, hdc, &rcBox, &lvItem);
4813 if (cdmode & CDRF_NOTIFYITEMDRAW)
4814 cdsubitemmode = notify_customdraw(infoPtr, CDDS_ITEMPREPAINT, &nmlvcd);
4815 if (cdsubitemmode & CDRF_SKIPDEFAULT) goto postpaint;
4817 if (subitems)
4819 while (iterator_next(subitems))
4821 DWORD subitemstage = CDRF_DODEFAULT;
4823 /* We need to query for each subitem, item's data (subitem == 0) is already here at this point */
4824 if (subitems->nItem)
4826 lvItem.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_PARAM | LVIF_INDENT;
4827 lvItem.stateMask = LVIS_SELECTED | LVIS_FOCUSED | LVIS_STATEIMAGEMASK | LVIS_CUT | LVIS_OVERLAYMASK;
4828 lvItem.iItem = nItem;
4829 lvItem.iSubItem = subitems->nItem;
4830 lvItem.state = 0;
4831 lvItem.lParam = 0;
4832 lvItem.cchTextMax = DISP_TEXT_SIZE;
4833 lvItem.pszText = szDispText;
4834 szDispText[0] = 0;
4835 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return FALSE;
4836 if (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT)
4837 lvItem.state = LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED);
4838 if (lvItem.pszText == LPSTR_TEXTCALLBACKW) lvItem.pszText = callbackW;
4839 TRACE(" lvItem=%s\n", debuglvitem_t(&lvItem, TRUE));
4841 /* update custom draw data */
4842 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, &nmlvcd.nmcd.rc, NULL, NULL, NULL, NULL);
4843 OffsetRect(&nmlvcd.nmcd.rc, pos.x, pos.y);
4844 nmlvcd.iSubItem = subitems->nItem;
4847 if (cdsubitemmode & CDRF_NOTIFYSUBITEMDRAW)
4848 subitemstage = notify_customdraw(infoPtr, CDDS_SUBITEM | CDDS_ITEMPREPAINT, &nmlvcd);
4850 if (subitems->nItem == 0 || (cdmode & CDRF_NOTIFYITEMDRAW))
4851 prepaint_setup(infoPtr, hdc, &nmlvcd, FALSE);
4852 else if (!(infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT))
4853 prepaint_setup(infoPtr, hdc, &nmlvcd, TRUE);
4855 if (!(subitemstage & CDRF_SKIPDEFAULT))
4856 LISTVIEW_DrawItemPart(infoPtr, &lvItem, &nmlvcd, &pos);
4858 if (subitemstage & CDRF_NOTIFYPOSTPAINT)
4859 subitemstage = notify_customdraw(infoPtr, CDDS_SUBITEM | CDDS_ITEMPOSTPAINT, &nmlvcd);
4862 else
4864 prepaint_setup(infoPtr, hdc, &nmlvcd, FALSE);
4865 LISTVIEW_DrawItemPart(infoPtr, &lvItem, &nmlvcd, &pos);
4868 postpaint:
4869 if (cdsubitemmode & CDRF_NOTIFYPOSTPAINT)
4871 nmlvcd.iSubItem = 0;
4872 notify_customdraw(infoPtr, CDDS_ITEMPOSTPAINT, &nmlvcd);
4875 return TRUE;
4878 /***
4879 * DESCRIPTION:
4880 * Draws listview items when in owner draw mode.
4882 * PARAMETER(S):
4883 * [I] infoPtr : valid pointer to the listview structure
4884 * [I] hdc : device context handle
4886 * RETURN:
4887 * None
4889 static void LISTVIEW_RefreshOwnerDraw(const LISTVIEW_INFO *infoPtr, ITERATOR *i, HDC hdc, DWORD cdmode)
4891 UINT uID = (UINT)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
4892 DWORD cditemmode = CDRF_DODEFAULT;
4893 NMLVCUSTOMDRAW nmlvcd;
4894 POINT Origin, Position;
4895 DRAWITEMSTRUCT dis;
4896 LVITEMW item;
4898 TRACE("()\n");
4900 ZeroMemory(&dis, sizeof(dis));
4902 /* Get scroll info once before loop */
4903 LISTVIEW_GetOrigin(infoPtr, &Origin);
4905 /* iterate through the invalidated rows */
4906 while(iterator_next(i))
4908 item.iItem = i->nItem;
4909 item.iSubItem = 0;
4910 item.mask = LVIF_PARAM | LVIF_STATE;
4911 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
4912 if (!LISTVIEW_GetItemW(infoPtr, &item)) continue;
4914 dis.CtlType = ODT_LISTVIEW;
4915 dis.CtlID = uID;
4916 dis.itemID = item.iItem;
4917 dis.itemAction = ODA_DRAWENTIRE;
4918 dis.itemState = 0;
4919 if (item.state & LVIS_SELECTED) dis.itemState |= ODS_SELECTED;
4920 if (infoPtr->bFocus && (item.state & LVIS_FOCUSED)) dis.itemState |= ODS_FOCUS;
4921 dis.hwndItem = infoPtr->hwndSelf;
4922 dis.hDC = hdc;
4923 LISTVIEW_GetItemOrigin(infoPtr, dis.itemID, &Position);
4924 dis.rcItem.left = Position.x + Origin.x;
4925 dis.rcItem.right = dis.rcItem.left + infoPtr->nItemWidth;
4926 dis.rcItem.top = Position.y + Origin.y;
4927 dis.rcItem.bottom = dis.rcItem.top + infoPtr->nItemHeight;
4928 dis.itemData = item.lParam;
4930 TRACE("item=%s, rcItem=%s\n", debuglvitem_t(&item, TRUE), wine_dbgstr_rect(&dis.rcItem));
4933 * Even if we do not send the CDRF_NOTIFYITEMDRAW we need to fill the nmlvcd
4934 * structure for the rest. of the paint cycle
4936 customdraw_fill(&nmlvcd, infoPtr, hdc, &dis.rcItem, &item);
4937 if (cdmode & CDRF_NOTIFYITEMDRAW)
4938 cditemmode = notify_customdraw(infoPtr, CDDS_PREPAINT, &nmlvcd);
4940 if (!(cditemmode & CDRF_SKIPDEFAULT))
4942 prepaint_setup (infoPtr, hdc, &nmlvcd, FALSE);
4943 SendMessageW(infoPtr->hwndNotify, WM_DRAWITEM, dis.CtlID, (LPARAM)&dis);
4946 if (cditemmode & CDRF_NOTIFYPOSTPAINT)
4947 notify_postpaint(infoPtr, &nmlvcd);
4951 /***
4952 * DESCRIPTION:
4953 * Draws listview items when in report display mode.
4955 * PARAMETER(S):
4956 * [I] infoPtr : valid pointer to the listview structure
4957 * [I] hdc : device context handle
4958 * [I] cdmode : custom draw mode
4960 * RETURN:
4961 * None
4963 static void LISTVIEW_RefreshReport(LISTVIEW_INFO *infoPtr, ITERATOR *i, HDC hdc, DWORD cdmode)
4965 INT rgntype;
4966 RECT rcClip, rcItem;
4967 POINT Origin;
4968 RANGES colRanges;
4969 INT col;
4970 ITERATOR j;
4972 TRACE("()\n");
4974 /* figure out what to draw */
4975 rgntype = GetClipBox(hdc, &rcClip);
4976 if (rgntype == NULLREGION) return;
4978 /* Get scroll info once before loop */
4979 LISTVIEW_GetOrigin(infoPtr, &Origin);
4981 colRanges = ranges_create(DPA_GetPtrCount(infoPtr->hdpaColumns));
4983 /* narrow down the columns we need to paint */
4984 for(col = 0; col < DPA_GetPtrCount(infoPtr->hdpaColumns); col++)
4986 INT index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, col, 0);
4988 LISTVIEW_GetHeaderRect(infoPtr, index, &rcItem);
4989 if ((rcItem.right + Origin.x >= rcClip.left) && (rcItem.left + Origin.x < rcClip.right))
4990 ranges_additem(colRanges, index);
4992 iterator_rangesitems(&j, colRanges);
4994 /* in full row select, we _have_ to draw the main item */
4995 if (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT)
4996 j.nSpecial = 0;
4998 /* iterate through the invalidated rows */
4999 while(iterator_next(i))
5001 RANGES subitems;
5002 POINT Position;
5003 ITERATOR k;
5005 SelectObject(hdc, infoPtr->hFont);
5006 LISTVIEW_GetItemOrigin(infoPtr, i->nItem, &Position);
5007 Position.x = Origin.x;
5008 Position.y += Origin.y;
5010 subitems = ranges_create(DPA_GetPtrCount(infoPtr->hdpaColumns));
5012 /* iterate through the invalidated columns */
5013 while(iterator_next(&j))
5015 LISTVIEW_GetHeaderRect(infoPtr, j.nItem, &rcItem);
5017 if (rgntype == COMPLEXREGION && !((infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) && j.nItem == 0))
5019 rcItem.top = 0;
5020 rcItem.bottom = infoPtr->nItemHeight;
5021 OffsetRect(&rcItem, Origin.x, Position.y);
5022 if (!RectVisible(hdc, &rcItem)) continue;
5025 ranges_additem(subitems, j.nItem);
5028 iterator_rangesitems(&k, subitems);
5029 LISTVIEW_DrawItem(infoPtr, hdc, i->nItem, &k, Position, cdmode);
5030 iterator_destroy(&k);
5032 iterator_destroy(&j);
5035 /***
5036 * DESCRIPTION:
5037 * Draws the gridlines if necessary when in report display mode.
5039 * PARAMETER(S):
5040 * [I] infoPtr : valid pointer to the listview structure
5041 * [I] hdc : device context handle
5043 * RETURN:
5044 * None
5046 static void LISTVIEW_RefreshReportGrid(LISTVIEW_INFO *infoPtr, HDC hdc)
5048 INT rgntype;
5049 INT y, itemheight;
5050 INT col, index;
5051 HPEN hPen, hOldPen;
5052 RECT rcClip, rcItem = {0};
5053 POINT Origin;
5054 RANGES colRanges;
5055 ITERATOR j;
5056 BOOL rmost = FALSE;
5058 TRACE("()\n");
5060 /* figure out what to draw */
5061 rgntype = GetClipBox(hdc, &rcClip);
5062 if (rgntype == NULLREGION) return;
5064 /* Get scroll info once before loop */
5065 LISTVIEW_GetOrigin(infoPtr, &Origin);
5067 colRanges = ranges_create(DPA_GetPtrCount(infoPtr->hdpaColumns));
5069 /* narrow down the columns we need to paint */
5070 for(col = 0; col < DPA_GetPtrCount(infoPtr->hdpaColumns); col++)
5072 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, col, 0);
5074 LISTVIEW_GetHeaderRect(infoPtr, index, &rcItem);
5075 if ((rcItem.right + Origin.x >= rcClip.left) && (rcItem.left + Origin.x < rcClip.right))
5076 ranges_additem(colRanges, index);
5079 /* is right most vertical line visible? */
5080 if (DPA_GetPtrCount(infoPtr->hdpaColumns) > 0)
5082 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, 0);
5083 LISTVIEW_GetHeaderRect(infoPtr, index, &rcItem);
5084 rmost = (rcItem.right + Origin.x < rcClip.right);
5087 if ((hPen = CreatePen( PS_SOLID, 1, comctl32_color.clr3dFace )))
5089 hOldPen = SelectObject ( hdc, hPen );
5091 /* draw the vertical lines for the columns */
5092 iterator_rangesitems(&j, colRanges);
5093 while(iterator_next(&j))
5095 LISTVIEW_GetHeaderRect(infoPtr, j.nItem, &rcItem);
5096 if (rcItem.left == 0) continue; /* skip leftmost column */
5097 rcItem.left += Origin.x;
5098 rcItem.right += Origin.x;
5099 rcItem.top = infoPtr->rcList.top;
5100 rcItem.bottom = infoPtr->rcList.bottom;
5101 TRACE("vert col=%d, rcItem=%s\n", j.nItem, wine_dbgstr_rect(&rcItem));
5102 MoveToEx (hdc, rcItem.left, rcItem.top, NULL);
5103 LineTo (hdc, rcItem.left, rcItem.bottom);
5105 iterator_destroy(&j);
5106 /* draw rightmost grid line if visible */
5107 if (rmost)
5109 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX,
5110 DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, 0);
5111 LISTVIEW_GetHeaderRect(infoPtr, index, &rcItem);
5113 rcItem.right += Origin.x;
5115 MoveToEx (hdc, rcItem.right, infoPtr->rcList.top, NULL);
5116 LineTo (hdc, rcItem.right, infoPtr->rcList.bottom);
5119 /* draw the horizontal lines for the rows */
5120 itemheight = LISTVIEW_CalculateItemHeight(infoPtr);
5121 rcItem.left = infoPtr->rcList.left;
5122 rcItem.right = infoPtr->rcList.right;
5123 for(y = Origin.y > 1 ? Origin.y - 1 : itemheight - 1 + Origin.y % itemheight; y<=infoPtr->rcList.bottom; y+=itemheight)
5125 rcItem.bottom = rcItem.top = y;
5126 TRACE("horz rcItem=%s\n", wine_dbgstr_rect(&rcItem));
5127 MoveToEx (hdc, rcItem.left, rcItem.top, NULL);
5128 LineTo (hdc, rcItem.right, rcItem.top);
5131 SelectObject( hdc, hOldPen );
5132 DeleteObject( hPen );
5134 else
5135 ranges_destroy(colRanges);
5138 /***
5139 * DESCRIPTION:
5140 * Draws listview items when in list display mode.
5142 * PARAMETER(S):
5143 * [I] infoPtr : valid pointer to the listview structure
5144 * [I] hdc : device context handle
5145 * [I] cdmode : custom draw mode
5147 * RETURN:
5148 * None
5150 static void LISTVIEW_RefreshList(LISTVIEW_INFO *infoPtr, ITERATOR *i, HDC hdc, DWORD cdmode)
5152 POINT Origin, Position;
5154 /* Get scroll info once before loop */
5155 LISTVIEW_GetOrigin(infoPtr, &Origin);
5157 while(iterator_prev(i))
5159 SelectObject(hdc, infoPtr->hFont);
5160 LISTVIEW_GetItemOrigin(infoPtr, i->nItem, &Position);
5161 Position.x += Origin.x;
5162 Position.y += Origin.y;
5164 LISTVIEW_DrawItem(infoPtr, hdc, i->nItem, NULL, Position, cdmode);
5169 /***
5170 * DESCRIPTION:
5171 * Draws listview items.
5173 * PARAMETER(S):
5174 * [I] infoPtr : valid pointer to the listview structure
5175 * [I] hdc : device context handle
5176 * [I] prcErase : rect to be erased before refresh (may be NULL)
5178 * RETURN:
5179 * NoneX
5181 static void LISTVIEW_Refresh(LISTVIEW_INFO *infoPtr, HDC hdc, const RECT *prcErase)
5183 COLORREF oldTextColor = 0, oldBkColor = 0;
5184 NMLVCUSTOMDRAW nmlvcd;
5185 HFONT hOldFont = 0;
5186 DWORD cdmode;
5187 INT oldBkMode = 0;
5188 RECT rcClient;
5189 ITERATOR i;
5190 HDC hdcOrig = hdc;
5191 HBITMAP hbmp = NULL;
5192 RANGE range;
5194 LISTVIEW_DUMP(infoPtr);
5196 if (infoPtr->dwLvExStyle & LVS_EX_DOUBLEBUFFER) {
5197 TRACE("double buffering\n");
5199 hdc = CreateCompatibleDC(hdcOrig);
5200 if (!hdc) {
5201 ERR("Failed to create DC for backbuffer\n");
5202 return;
5204 hbmp = CreateCompatibleBitmap(hdcOrig, infoPtr->rcList.right,
5205 infoPtr->rcList.bottom);
5206 if (!hbmp) {
5207 ERR("Failed to create bitmap for backbuffer\n");
5208 DeleteDC(hdc);
5209 return;
5212 SelectObject(hdc, hbmp);
5213 SelectObject(hdc, infoPtr->hFont);
5215 if(GetClipBox(hdcOrig, &rcClient))
5216 IntersectClipRect(hdc, rcClient.left, rcClient.top, rcClient.right, rcClient.bottom);
5217 } else {
5218 /* Save dc values we're gonna trash while drawing
5219 * FIXME: Should be done in LISTVIEW_DrawItem() */
5220 hOldFont = SelectObject(hdc, infoPtr->hFont);
5221 oldBkMode = GetBkMode(hdc);
5222 oldBkColor = GetBkColor(hdc);
5223 oldTextColor = GetTextColor(hdc);
5226 infoPtr->bIsDrawing = TRUE;
5228 if (prcErase) {
5229 LISTVIEW_FillBkgnd(infoPtr, hdc, prcErase);
5230 } else if (infoPtr->dwLvExStyle & LVS_EX_DOUBLEBUFFER) {
5231 /* If no erasing was done (usually because RedrawWindow was called
5232 * with RDW_INVALIDATE only) we need to copy the old contents into
5233 * the backbuffer before continuing. */
5234 BitBlt(hdc, infoPtr->rcList.left, infoPtr->rcList.top,
5235 infoPtr->rcList.right - infoPtr->rcList.left,
5236 infoPtr->rcList.bottom - infoPtr->rcList.top,
5237 hdcOrig, infoPtr->rcList.left, infoPtr->rcList.top, SRCCOPY);
5240 GetClientRect(infoPtr->hwndSelf, &rcClient);
5241 customdraw_fill(&nmlvcd, infoPtr, hdc, &rcClient, 0);
5242 cdmode = notify_customdraw(infoPtr, CDDS_PREPAINT, &nmlvcd);
5243 if (cdmode & CDRF_SKIPDEFAULT) goto enddraw;
5245 /* nothing to draw */
5246 if(infoPtr->nItemCount == 0) goto enddraw;
5248 /* figure out what we need to draw */
5249 iterator_visibleitems(&i, infoPtr, hdc);
5250 range = iterator_range(&i);
5252 /* send cache hint notification */
5253 if (infoPtr->dwStyle & LVS_OWNERDATA)
5255 NMLVCACHEHINT nmlv;
5257 ZeroMemory(&nmlv, sizeof(NMLVCACHEHINT));
5258 nmlv.iFrom = range.lower;
5259 nmlv.iTo = range.upper - 1;
5260 notify_hdr(infoPtr, LVN_ODCACHEHINT, &nmlv.hdr);
5263 if ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (infoPtr->uView == LV_VIEW_DETAILS))
5264 LISTVIEW_RefreshOwnerDraw(infoPtr, &i, hdc, cdmode);
5265 else
5267 if (infoPtr->uView == LV_VIEW_DETAILS)
5268 LISTVIEW_RefreshReport(infoPtr, &i, hdc, cdmode);
5269 else /* LV_VIEW_LIST, LV_VIEW_ICON or LV_VIEW_SMALLICON */
5270 LISTVIEW_RefreshList(infoPtr, &i, hdc, cdmode);
5272 /* if we have a focus rect and it's visible, draw it */
5273 if (infoPtr->bFocus && range.lower <= infoPtr->nFocusedItem &&
5274 (range.upper - 1) >= infoPtr->nFocusedItem)
5275 LISTVIEW_DrawFocusRect(infoPtr, hdc);
5277 iterator_destroy(&i);
5279 enddraw:
5280 /* For LVS_EX_GRIDLINES go and draw lines */
5281 /* This includes the case where there were *no* items */
5282 if ((infoPtr->uView == LV_VIEW_DETAILS) && infoPtr->dwLvExStyle & LVS_EX_GRIDLINES)
5283 LISTVIEW_RefreshReportGrid(infoPtr, hdc);
5285 /* Draw marquee rectangle if appropriate */
5286 if (infoPtr->bMarqueeSelect)
5287 DrawFocusRect(hdc, &infoPtr->marqueeDrawRect);
5289 if (cdmode & CDRF_NOTIFYPOSTPAINT)
5290 notify_postpaint(infoPtr, &nmlvcd);
5292 if(hbmp) {
5293 BitBlt(hdcOrig, infoPtr->rcList.left, infoPtr->rcList.top,
5294 infoPtr->rcList.right - infoPtr->rcList.left,
5295 infoPtr->rcList.bottom - infoPtr->rcList.top,
5296 hdc, infoPtr->rcList.left, infoPtr->rcList.top, SRCCOPY);
5298 DeleteObject(hbmp);
5299 DeleteDC(hdc);
5300 } else {
5301 SelectObject(hdc, hOldFont);
5302 SetBkMode(hdc, oldBkMode);
5303 SetBkColor(hdc, oldBkColor);
5304 SetTextColor(hdc, oldTextColor);
5307 infoPtr->bIsDrawing = FALSE;
5311 /***
5312 * DESCRIPTION:
5313 * Calculates the approximate width and height of a given number of items.
5315 * PARAMETER(S):
5316 * [I] infoPtr : valid pointer to the listview structure
5317 * [I] nItemCount : number of items
5318 * [I] wWidth : width
5319 * [I] wHeight : height
5321 * RETURN:
5322 * Returns a DWORD. The width in the low word and the height in high word.
5324 static DWORD LISTVIEW_ApproximateViewRect(const LISTVIEW_INFO *infoPtr, INT nItemCount,
5325 WORD wWidth, WORD wHeight)
5327 DWORD dwViewRect = 0;
5329 if (nItemCount == -1)
5330 nItemCount = infoPtr->nItemCount;
5332 if (infoPtr->uView == LV_VIEW_LIST)
5334 INT nItemCountPerColumn = 1;
5335 INT nColumnCount = 0;
5337 if (wHeight == 0xFFFF)
5339 /* use current height */
5340 wHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
5343 if (wHeight < infoPtr->nItemHeight)
5344 wHeight = infoPtr->nItemHeight;
5346 if (nItemCount > 0)
5348 if (infoPtr->nItemHeight > 0)
5350 nItemCountPerColumn = wHeight / infoPtr->nItemHeight;
5351 if (nItemCountPerColumn == 0)
5352 nItemCountPerColumn = 1;
5354 if (nItemCount % nItemCountPerColumn != 0)
5355 nColumnCount = nItemCount / nItemCountPerColumn;
5356 else
5357 nColumnCount = nItemCount / nItemCountPerColumn + 1;
5361 /* Microsoft padding magic */
5362 wHeight = nItemCountPerColumn * infoPtr->nItemHeight + 2;
5363 wWidth = nColumnCount * infoPtr->nItemWidth + 2;
5365 dwViewRect = MAKELONG(wWidth, wHeight);
5367 else if (infoPtr->uView == LV_VIEW_DETAILS)
5369 RECT rcBox;
5371 if (infoPtr->nItemCount > 0)
5373 LISTVIEW_GetItemBox(infoPtr, 0, &rcBox);
5374 wWidth = rcBox.right - rcBox.left;
5375 wHeight = (rcBox.bottom - rcBox.top) * nItemCount;
5377 else
5379 /* use current height and width */
5380 if (wHeight == 0xffff)
5381 wHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
5382 if (wWidth == 0xffff)
5383 wWidth = infoPtr->rcList.right - infoPtr->rcList.left;
5386 dwViewRect = MAKELONG(wWidth, wHeight);
5388 else if (infoPtr->uView == LV_VIEW_ICON)
5390 UINT rows,cols;
5391 UINT nItemWidth;
5392 UINT nItemHeight;
5394 nItemWidth = infoPtr->iconSpacing.cx;
5395 nItemHeight = infoPtr->iconSpacing.cy;
5397 if (wWidth == 0xffff)
5398 wWidth = infoPtr->rcList.right - infoPtr->rcList.left;
5400 if (wWidth < nItemWidth)
5401 wWidth = nItemWidth;
5403 cols = wWidth / nItemWidth;
5404 if (cols > nItemCount)
5405 cols = nItemCount;
5406 if (cols < 1)
5407 cols = 1;
5409 if (nItemCount)
5411 rows = nItemCount / cols;
5412 if (nItemCount % cols)
5413 rows++;
5415 else
5416 rows = 0;
5418 wHeight = (nItemHeight * rows)+2;
5419 wWidth = (nItemWidth * cols)+2;
5421 dwViewRect = MAKELONG(wWidth, wHeight);
5423 else if (infoPtr->uView == LV_VIEW_SMALLICON)
5424 FIXME("uView == LV_VIEW_SMALLICON: not implemented\n");
5426 return dwViewRect;
5429 /***
5430 * DESCRIPTION:
5431 * Cancel edit label with saving item text.
5433 * PARAMETER(S):
5434 * [I] infoPtr : valid pointer to the listview structure
5436 * RETURN:
5437 * Always returns TRUE.
5439 static LRESULT LISTVIEW_CancelEditLabel(LISTVIEW_INFO *infoPtr)
5441 if (infoPtr->hwndEdit)
5443 /* handle value will be lost after LISTVIEW_EndEditLabelT */
5444 HWND edit = infoPtr->hwndEdit;
5446 LISTVIEW_EndEditLabelT(infoPtr, TRUE, IsWindowUnicode(infoPtr->hwndEdit));
5447 SendMessageW(edit, WM_CLOSE, 0, 0);
5450 return TRUE;
5453 /***
5454 * DESCRIPTION:
5455 * Create a drag image list for the specified item.
5457 * PARAMETER(S):
5458 * [I] infoPtr : valid pointer to the listview structure
5459 * [I] iItem : index of item
5460 * [O] lppt : Upper-left corner of the image
5462 * RETURN:
5463 * Returns a handle to the image list if successful, NULL otherwise.
5465 static HIMAGELIST LISTVIEW_CreateDragImage(LISTVIEW_INFO *infoPtr, INT iItem, LPPOINT lppt)
5467 RECT rcItem;
5468 SIZE size;
5469 POINT pos;
5470 HDC hdc, hdcOrig;
5471 HBITMAP hbmp, hOldbmp;
5472 HFONT hOldFont;
5473 HIMAGELIST dragList = 0;
5474 TRACE("iItem=%d Count=%d\n", iItem, infoPtr->nItemCount);
5476 if (iItem < 0 || iItem >= infoPtr->nItemCount || !lppt)
5477 return 0;
5479 rcItem.left = LVIR_BOUNDS;
5480 if (!LISTVIEW_GetItemRect(infoPtr, iItem, &rcItem))
5481 return 0;
5483 lppt->x = rcItem.left;
5484 lppt->y = rcItem.top;
5486 size.cx = rcItem.right - rcItem.left;
5487 size.cy = rcItem.bottom - rcItem.top;
5489 hdcOrig = GetDC(infoPtr->hwndSelf);
5490 hdc = CreateCompatibleDC(hdcOrig);
5491 hbmp = CreateCompatibleBitmap(hdcOrig, size.cx, size.cy);
5492 hOldbmp = SelectObject(hdc, hbmp);
5493 hOldFont = SelectObject(hdc, infoPtr->hFont);
5495 SetRect(&rcItem, 0, 0, size.cx, size.cy);
5496 FillRect(hdc, &rcItem, infoPtr->hBkBrush);
5498 pos.x = pos.y = 0;
5499 if (LISTVIEW_DrawItem(infoPtr, hdc, iItem, NULL, pos, CDRF_DODEFAULT))
5501 dragList = ImageList_Create(size.cx, size.cy, ILC_COLOR, 10, 10);
5502 SelectObject(hdc, hOldbmp);
5503 ImageList_Add(dragList, hbmp, 0);
5505 else
5506 SelectObject(hdc, hOldbmp);
5508 SelectObject(hdc, hOldFont);
5509 DeleteObject(hbmp);
5510 DeleteDC(hdc);
5511 ReleaseDC(infoPtr->hwndSelf, hdcOrig);
5513 TRACE("ret=%p\n", dragList);
5515 return dragList;
5519 /***
5520 * DESCRIPTION:
5521 * Removes all listview items and subitems.
5523 * PARAMETER(S):
5524 * [I] infoPtr : valid pointer to the listview structure
5526 * RETURN:
5527 * SUCCESS : TRUE
5528 * FAILURE : FALSE
5530 static BOOL LISTVIEW_DeleteAllItems(LISTVIEW_INFO *infoPtr, BOOL destroy)
5532 HDPA hdpaSubItems = NULL;
5533 BOOL suppress = FALSE;
5534 ITEMHDR *hdrItem;
5535 ITEM_INFO *lpItem;
5536 ITEM_ID *lpID;
5537 INT i, j;
5539 TRACE("()\n");
5541 /* we do it directly, to avoid notifications */
5542 ranges_clear(infoPtr->selectionRanges);
5543 infoPtr->nSelectionMark = -1;
5544 infoPtr->nFocusedItem = -1;
5545 SetRectEmpty(&infoPtr->rcFocus);
5546 /* But we are supposed to leave nHotItem as is! */
5548 /* send LVN_DELETEALLITEMS notification */
5549 if (!(infoPtr->dwStyle & LVS_OWNERDATA) || !destroy)
5551 NMLISTVIEW nmlv;
5553 memset(&nmlv, 0, sizeof(NMLISTVIEW));
5554 nmlv.iItem = -1;
5555 suppress = notify_listview(infoPtr, LVN_DELETEALLITEMS, &nmlv);
5558 for (i = infoPtr->nItemCount - 1; i >= 0; i--)
5560 if (!(infoPtr->dwStyle & LVS_OWNERDATA))
5562 /* send LVN_DELETEITEM notification, if not suppressed
5563 and if it is not a virtual listview */
5564 if (!suppress) notify_deleteitem(infoPtr, i);
5565 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, i);
5566 lpItem = DPA_GetPtr(hdpaSubItems, 0);
5567 /* free id struct */
5568 j = DPA_GetPtrIndex(infoPtr->hdpaItemIds, lpItem->id);
5569 lpID = DPA_GetPtr(infoPtr->hdpaItemIds, j);
5570 DPA_DeletePtr(infoPtr->hdpaItemIds, j);
5571 Free(lpID);
5572 /* both item and subitem start with ITEMHDR header */
5573 for (j = 0; j < DPA_GetPtrCount(hdpaSubItems); j++)
5575 hdrItem = DPA_GetPtr(hdpaSubItems, j);
5576 if (is_text(hdrItem->pszText)) Free(hdrItem->pszText);
5577 Free(hdrItem);
5579 DPA_Destroy(hdpaSubItems);
5580 DPA_DeletePtr(infoPtr->hdpaItems, i);
5582 DPA_DeletePtr(infoPtr->hdpaPosX, i);
5583 DPA_DeletePtr(infoPtr->hdpaPosY, i);
5584 infoPtr->nItemCount --;
5587 if (!destroy)
5589 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
5590 LISTVIEW_UpdateScroll(infoPtr);
5592 LISTVIEW_InvalidateList(infoPtr);
5593 infoPtr->bNoItemMetrics = TRUE;
5595 return TRUE;
5598 /***
5599 * DESCRIPTION:
5600 * Scrolls, and updates the columns, when a column is changing width.
5602 * PARAMETER(S):
5603 * [I] infoPtr : valid pointer to the listview structure
5604 * [I] nColumn : column to scroll
5605 * [I] dx : amount of scroll, in pixels
5607 * RETURN:
5608 * None.
5610 static void LISTVIEW_ScrollColumns(LISTVIEW_INFO *infoPtr, INT nColumn, INT dx)
5612 COLUMN_INFO *lpColumnInfo;
5613 RECT rcOld, rcCol;
5614 POINT ptOrigin;
5615 INT nCol;
5616 HDITEMW hdi;
5618 if (nColumn < 0 || DPA_GetPtrCount(infoPtr->hdpaColumns) < 1) return;
5619 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, min(nColumn, DPA_GetPtrCount(infoPtr->hdpaColumns) - 1));
5620 rcCol = lpColumnInfo->rcHeader;
5621 if (nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns))
5622 rcCol.left = rcCol.right;
5624 /* adjust the other columns */
5625 hdi.mask = HDI_ORDER;
5626 if (SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nColumn, (LPARAM)&hdi))
5628 INT nOrder = hdi.iOrder;
5629 for (nCol = 0; nCol < DPA_GetPtrCount(infoPtr->hdpaColumns); nCol++)
5631 hdi.mask = HDI_ORDER;
5632 SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nCol, (LPARAM)&hdi);
5633 if (hdi.iOrder >= nOrder) {
5634 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, nCol);
5635 lpColumnInfo->rcHeader.left += dx;
5636 lpColumnInfo->rcHeader.right += dx;
5641 /* do not update screen if not in report mode */
5642 if (!is_redrawing(infoPtr) || infoPtr->uView != LV_VIEW_DETAILS) return;
5644 /* Need to reset the item width when inserting a new column */
5645 infoPtr->nItemWidth += dx;
5647 LISTVIEW_UpdateScroll(infoPtr);
5648 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
5650 /* scroll to cover the deleted column, and invalidate for redraw */
5651 rcOld = infoPtr->rcList;
5652 rcOld.left = ptOrigin.x + rcCol.left + dx;
5653 ScrollWindowEx(infoPtr->hwndSelf, dx, 0, &rcOld, &rcOld, 0, 0, SW_ERASE | SW_INVALIDATE);
5656 /***
5657 * DESCRIPTION:
5658 * Removes a column from the listview control.
5660 * PARAMETER(S):
5661 * [I] infoPtr : valid pointer to the listview structure
5662 * [I] nColumn : column index
5664 * RETURN:
5665 * SUCCESS : TRUE
5666 * FAILURE : FALSE
5668 static BOOL LISTVIEW_DeleteColumn(LISTVIEW_INFO *infoPtr, INT nColumn)
5670 RECT rcCol;
5672 TRACE("nColumn=%d\n", nColumn);
5674 if (nColumn < 0 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns))
5675 return FALSE;
5677 /* While the MSDN specifically says that column zero should not be deleted,
5678 what actually happens is that the column itself is deleted but no items or subitems
5679 are removed.
5682 LISTVIEW_GetHeaderRect(infoPtr, nColumn, &rcCol);
5684 if (!SendMessageW(infoPtr->hwndHeader, HDM_DELETEITEM, nColumn, 0))
5685 return FALSE;
5687 Free(DPA_GetPtr(infoPtr->hdpaColumns, nColumn));
5688 DPA_DeletePtr(infoPtr->hdpaColumns, nColumn);
5690 if (!(infoPtr->dwStyle & LVS_OWNERDATA) && nColumn)
5692 SUBITEM_INFO *lpSubItem, *lpDelItem;
5693 HDPA hdpaSubItems;
5694 INT nItem, nSubItem, i;
5696 for (nItem = 0; nItem < infoPtr->nItemCount; nItem++)
5698 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, nItem);
5699 nSubItem = 0;
5700 lpDelItem = 0;
5701 for (i = 1; i < DPA_GetPtrCount(hdpaSubItems); i++)
5703 lpSubItem = DPA_GetPtr(hdpaSubItems, i);
5704 if (lpSubItem->iSubItem == nColumn)
5706 nSubItem = i;
5707 lpDelItem = lpSubItem;
5709 else if (lpSubItem->iSubItem > nColumn)
5711 lpSubItem->iSubItem--;
5715 /* if we found our subitem, zap it */
5716 if (nSubItem > 0)
5718 /* free string */
5719 if (is_text(lpDelItem->hdr.pszText))
5720 Free(lpDelItem->hdr.pszText);
5722 /* free item */
5723 Free(lpDelItem);
5725 /* free dpa memory */
5726 DPA_DeletePtr(hdpaSubItems, nSubItem);
5731 /* update the other column info */
5732 if(DPA_GetPtrCount(infoPtr->hdpaColumns) == 0)
5733 LISTVIEW_InvalidateList(infoPtr);
5734 else
5735 LISTVIEW_ScrollColumns(infoPtr, nColumn, -(rcCol.right - rcCol.left));
5736 LISTVIEW_UpdateItemSize(infoPtr);
5738 return TRUE;
5741 /***
5742 * DESCRIPTION:
5743 * Invalidates the listview after an item's insertion or deletion.
5745 * PARAMETER(S):
5746 * [I] infoPtr : valid pointer to the listview structure
5747 * [I] nItem : item index
5748 * [I] dir : -1 if deleting, 1 if inserting
5750 * RETURN:
5751 * None
5753 static void LISTVIEW_ScrollOnInsert(LISTVIEW_INFO *infoPtr, INT nItem, INT dir)
5755 INT nPerCol, nItemCol, nItemRow;
5756 RECT rcScroll;
5757 POINT Origin;
5759 /* if we don't refresh, what's the point of scrolling? */
5760 if (!is_redrawing(infoPtr)) return;
5762 assert (abs(dir) == 1);
5764 /* arrange icons if autoarrange is on */
5765 if (is_autoarrange(infoPtr))
5767 BOOL arrange = TRUE;
5768 if (dir < 0 && nItem >= infoPtr->nItemCount) arrange = FALSE;
5769 if (dir > 0 && nItem == infoPtr->nItemCount - 1) arrange = FALSE;
5770 if (arrange) LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
5773 /* scrollbars need updating */
5774 LISTVIEW_UpdateScroll(infoPtr);
5776 /* figure out the item's position */
5777 if (infoPtr->uView == LV_VIEW_DETAILS)
5778 nPerCol = infoPtr->nItemCount + 1;
5779 else if (infoPtr->uView == LV_VIEW_LIST)
5780 nPerCol = LISTVIEW_GetCountPerColumn(infoPtr);
5781 else /* LV_VIEW_ICON, or LV_VIEW_SMALLICON */
5782 return;
5784 nItemCol = nItem / nPerCol;
5785 nItemRow = nItem % nPerCol;
5786 LISTVIEW_GetOrigin(infoPtr, &Origin);
5788 /* move the items below up a slot */
5789 rcScroll.left = nItemCol * infoPtr->nItemWidth;
5790 rcScroll.top = nItemRow * infoPtr->nItemHeight;
5791 rcScroll.right = rcScroll.left + infoPtr->nItemWidth;
5792 rcScroll.bottom = nPerCol * infoPtr->nItemHeight;
5793 OffsetRect(&rcScroll, Origin.x, Origin.y);
5794 TRACE("rcScroll=%s, dx=%d\n", wine_dbgstr_rect(&rcScroll), dir * infoPtr->nItemHeight);
5795 if (IntersectRect(&rcScroll, &rcScroll, &infoPtr->rcList))
5797 TRACE("Invalidating rcScroll=%s, rcList=%s\n", wine_dbgstr_rect(&rcScroll), wine_dbgstr_rect(&infoPtr->rcList));
5798 InvalidateRect(infoPtr->hwndSelf, &rcScroll, TRUE);
5801 /* report has only that column, so we're done */
5802 if (infoPtr->uView == LV_VIEW_DETAILS) return;
5804 /* now for LISTs, we have to deal with the columns to the right */
5805 SetRect(&rcScroll, (nItemCol + 1) * infoPtr->nItemWidth, 0,
5806 (infoPtr->nItemCount / nPerCol + 1) * infoPtr->nItemWidth,
5807 nPerCol * infoPtr->nItemHeight);
5808 OffsetRect(&rcScroll, Origin.x, Origin.y);
5809 if (IntersectRect(&rcScroll, &rcScroll, &infoPtr->rcList))
5810 InvalidateRect(infoPtr->hwndSelf, &rcScroll, TRUE);
5813 /***
5814 * DESCRIPTION:
5815 * Removes an item from the listview control.
5817 * PARAMETER(S):
5818 * [I] infoPtr : valid pointer to the listview structure
5819 * [I] nItem : item index
5821 * RETURN:
5822 * SUCCESS : TRUE
5823 * FAILURE : FALSE
5825 static BOOL LISTVIEW_DeleteItem(LISTVIEW_INFO *infoPtr, INT nItem)
5827 LVITEMW item;
5828 const BOOL is_icon = (infoPtr->uView == LV_VIEW_SMALLICON || infoPtr->uView == LV_VIEW_ICON);
5829 INT focus = infoPtr->nFocusedItem;
5831 TRACE("(nItem=%d)\n", nItem);
5833 if (nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
5835 /* remove selection, and focus */
5836 item.state = 0;
5837 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
5838 LISTVIEW_SetItemState(infoPtr, nItem, &item);
5840 /* send LVN_DELETEITEM notification. */
5841 if (!notify_deleteitem(infoPtr, nItem)) return FALSE;
5843 /* we need to do this here, because we'll be deleting stuff */
5844 if (is_icon)
5845 LISTVIEW_InvalidateItem(infoPtr, nItem);
5847 if (!(infoPtr->dwStyle & LVS_OWNERDATA))
5849 HDPA hdpaSubItems;
5850 ITEMHDR *hdrItem;
5851 ITEM_INFO *lpItem;
5852 ITEM_ID *lpID;
5853 INT i;
5855 hdpaSubItems = DPA_DeletePtr(infoPtr->hdpaItems, nItem);
5856 lpItem = DPA_GetPtr(hdpaSubItems, 0);
5858 /* free id struct */
5859 i = DPA_GetPtrIndex(infoPtr->hdpaItemIds, lpItem->id);
5860 lpID = DPA_GetPtr(infoPtr->hdpaItemIds, i);
5861 DPA_DeletePtr(infoPtr->hdpaItemIds, i);
5862 Free(lpID);
5863 for (i = 0; i < DPA_GetPtrCount(hdpaSubItems); i++)
5865 hdrItem = DPA_GetPtr(hdpaSubItems, i);
5866 if (is_text(hdrItem->pszText)) Free(hdrItem->pszText);
5867 Free(hdrItem);
5869 DPA_Destroy(hdpaSubItems);
5872 if (is_icon)
5874 DPA_DeletePtr(infoPtr->hdpaPosX, nItem);
5875 DPA_DeletePtr(infoPtr->hdpaPosY, nItem);
5878 infoPtr->nItemCount--;
5879 LISTVIEW_ShiftIndices(infoPtr, nItem, -1);
5880 LISTVIEW_ShiftFocus(infoPtr, focus, nItem, -1);
5882 /* now is the invalidation fun */
5883 if (!is_icon)
5884 LISTVIEW_ScrollOnInsert(infoPtr, nItem, -1);
5885 return TRUE;
5889 /***
5890 * DESCRIPTION:
5891 * Callback implementation for editlabel control
5893 * PARAMETER(S):
5894 * [I] infoPtr : valid pointer to the listview structure
5895 * [I] storeText : store edit box text as item text
5896 * [I] isW : TRUE if psxText is Unicode, FALSE if it's ANSI
5898 * RETURN:
5899 * SUCCESS : TRUE
5900 * FAILURE : FALSE
5902 static BOOL LISTVIEW_EndEditLabelT(LISTVIEW_INFO *infoPtr, BOOL storeText, BOOL isW)
5904 HWND hwndSelf = infoPtr->hwndSelf;
5905 WCHAR szDispText[DISP_TEXT_SIZE] = { 0 };
5906 NMLVDISPINFOW dispInfo;
5907 INT editedItem = infoPtr->nEditLabelItem;
5908 BOOL same;
5909 WCHAR *pszText = NULL;
5910 BOOL res;
5912 if (storeText)
5914 DWORD len = isW ? GetWindowTextLengthW(infoPtr->hwndEdit) : GetWindowTextLengthA(infoPtr->hwndEdit);
5916 if (len++)
5918 if (!(pszText = Alloc(len * (isW ? sizeof(WCHAR) : sizeof(CHAR)))))
5919 return FALSE;
5921 if (isW)
5922 GetWindowTextW(infoPtr->hwndEdit, pszText, len);
5923 else
5924 GetWindowTextA(infoPtr->hwndEdit, (CHAR*)pszText, len);
5928 TRACE("(pszText=%s, isW=%d)\n", debugtext_t(pszText, isW), isW);
5930 ZeroMemory(&dispInfo, sizeof(dispInfo));
5931 dispInfo.item.mask = LVIF_PARAM | LVIF_STATE | LVIF_TEXT;
5932 dispInfo.item.iItem = editedItem;
5933 dispInfo.item.iSubItem = 0;
5934 dispInfo.item.stateMask = ~0;
5935 dispInfo.item.pszText = szDispText;
5936 dispInfo.item.cchTextMax = DISP_TEXT_SIZE;
5937 if (!LISTVIEW_GetItemT(infoPtr, &dispInfo.item, isW))
5939 res = FALSE;
5940 goto cleanup;
5943 if (isW)
5944 same = (lstrcmpW(dispInfo.item.pszText, pszText) == 0);
5945 else
5947 LPWSTR tmp = textdupTtoW(pszText, FALSE);
5948 same = (lstrcmpW(dispInfo.item.pszText, tmp) == 0);
5949 textfreeT(tmp, FALSE);
5952 /* add the text from the edit in */
5953 dispInfo.item.mask |= LVIF_TEXT;
5954 dispInfo.item.pszText = same ? NULL : pszText;
5955 dispInfo.item.cchTextMax = textlenT(dispInfo.item.pszText, isW);
5957 infoPtr->notify_mask &= ~NOTIFY_MASK_END_LABEL_EDIT;
5959 /* Do we need to update the Item Text */
5960 res = notify_dispinfoT(infoPtr, LVN_ENDLABELEDITW, &dispInfo, isW);
5962 infoPtr->notify_mask |= NOTIFY_MASK_END_LABEL_EDIT;
5964 infoPtr->nEditLabelItem = -1;
5965 infoPtr->hwndEdit = 0;
5967 if (!res) goto cleanup;
5969 if (!IsWindow(hwndSelf))
5971 res = FALSE;
5972 goto cleanup;
5974 if (!pszText) return TRUE;
5975 if (same)
5977 res = TRUE;
5978 goto cleanup;
5981 if (!(infoPtr->dwStyle & LVS_OWNERDATA))
5983 HDPA hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, editedItem);
5984 ITEM_INFO* lpItem = DPA_GetPtr(hdpaSubItems, 0);
5985 if (lpItem && lpItem->hdr.pszText == LPSTR_TEXTCALLBACKW)
5987 LISTVIEW_InvalidateItem(infoPtr, editedItem);
5988 res = TRUE;
5989 goto cleanup;
5993 ZeroMemory(&dispInfo, sizeof(dispInfo));
5994 dispInfo.item.mask = LVIF_TEXT;
5995 dispInfo.item.iItem = editedItem;
5996 dispInfo.item.iSubItem = 0;
5997 dispInfo.item.pszText = pszText;
5998 dispInfo.item.cchTextMax = textlenT(pszText, isW);
5999 res = LISTVIEW_SetItemT(infoPtr, &dispInfo.item, isW);
6001 cleanup:
6002 Free(pszText);
6004 return res;
6007 static LRESULT EditLblWndProcT(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL isW)
6009 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongPtrW(GetParent(hwnd), 0);
6010 BOOL save = TRUE;
6012 TRACE("hwnd %p, uMsg %x, wParam %Ix, lParam %Ix, isW %d\n", hwnd, uMsg, wParam, lParam, isW);
6014 switch (uMsg)
6016 case WM_GETDLGCODE:
6017 return DLGC_WANTARROWS | DLGC_WANTALLKEYS;
6019 case WM_DESTROY:
6021 WNDPROC editProc = infoPtr->EditWndProc;
6022 infoPtr->EditWndProc = 0;
6023 SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (DWORD_PTR)editProc);
6024 return CallWindowProcT(editProc, hwnd, uMsg, wParam, lParam, isW);
6027 case WM_KEYDOWN:
6028 if (VK_ESCAPE == (INT)wParam)
6030 save = FALSE;
6031 break;
6033 else if (VK_RETURN == (INT)wParam)
6034 break;
6036 default:
6037 return CallWindowProcT(infoPtr->EditWndProc, hwnd, uMsg, wParam, lParam, isW);
6040 /* kill the edit */
6041 if (infoPtr->hwndEdit)
6042 LISTVIEW_EndEditLabelT(infoPtr, save, isW);
6044 SendMessageW(hwnd, WM_CLOSE, 0, 0);
6045 return 0;
6048 static LRESULT CALLBACK EditLblWndProcW(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
6050 return EditLblWndProcT(hwnd, uMsg, wParam, lParam, TRUE);
6053 static LRESULT CALLBACK EditLblWndProcA(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
6055 return EditLblWndProcT(hwnd, uMsg, wParam, lParam, FALSE);
6058 static HWND CreateEditLabelT(LISTVIEW_INFO *infoPtr, LPCWSTR text, BOOL isW)
6060 static const DWORD style = WS_CHILDWINDOW|WS_CLIPSIBLINGS|ES_LEFT|ES_AUTOHSCROLL|WS_BORDER|WS_VISIBLE;
6061 HINSTANCE hinst = (HINSTANCE)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_HINSTANCE);
6062 HWND hedit;
6064 TRACE("(%p, text=%s, isW=%d)\n", infoPtr, debugtext_t(text, isW), isW);
6066 /* window will be resized and positioned after LVN_BEGINLABELEDIT */
6067 if (isW)
6068 hedit = CreateWindowW(WC_EDITW, text, style, 0, 0, 0, 0, infoPtr->hwndSelf, 0, hinst, 0);
6069 else
6070 hedit = CreateWindowA(WC_EDITA, (LPCSTR)text, style, 0, 0, 0, 0, infoPtr->hwndSelf, 0, hinst, 0);
6072 if (!hedit) return 0;
6074 infoPtr->EditWndProc = (WNDPROC)
6075 (isW ? SetWindowLongPtrW(hedit, GWLP_WNDPROC, (DWORD_PTR)EditLblWndProcW) :
6076 SetWindowLongPtrA(hedit, GWLP_WNDPROC, (DWORD_PTR)EditLblWndProcA) );
6078 SendMessageW(hedit, WM_SETFONT, (WPARAM)infoPtr->hFont, FALSE);
6079 SendMessageW(hedit, EM_SETLIMITTEXT, DISP_TEXT_SIZE-1, 0);
6081 return hedit;
6084 static HWND LISTVIEW_EditLabelT(LISTVIEW_INFO *infoPtr, INT nItem, BOOL isW)
6086 WCHAR disptextW[DISP_TEXT_SIZE] = { 0 };
6087 HWND hwndSelf = infoPtr->hwndSelf;
6088 NMLVDISPINFOW dispInfo;
6089 HFONT hOldFont = NULL;
6090 TEXTMETRICW tm;
6091 RECT rect;
6092 SIZE sz;
6093 HDC hdc;
6095 TRACE("(nItem=%d, isW=%d)\n", nItem, isW);
6097 if (~infoPtr->dwStyle & LVS_EDITLABELS) return 0;
6099 /* remove existing edit box */
6100 if (infoPtr->hwndEdit)
6102 SetFocus(infoPtr->hwndSelf);
6103 infoPtr->hwndEdit = 0;
6106 if (nItem < 0 || nItem >= infoPtr->nItemCount) return 0;
6108 infoPtr->nEditLabelItem = nItem;
6110 LISTVIEW_SetSelection(infoPtr, nItem);
6111 LISTVIEW_SetItemFocus(infoPtr, nItem);
6112 LISTVIEW_InvalidateItem(infoPtr, nItem);
6114 rect.left = LVIR_LABEL;
6115 if (!LISTVIEW_GetItemRect(infoPtr, nItem, &rect)) return 0;
6117 ZeroMemory(&dispInfo, sizeof(dispInfo));
6118 dispInfo.item.mask = LVIF_PARAM | LVIF_STATE | LVIF_TEXT;
6119 dispInfo.item.iItem = nItem;
6120 dispInfo.item.iSubItem = 0;
6121 dispInfo.item.stateMask = ~0;
6122 dispInfo.item.pszText = disptextW;
6123 dispInfo.item.cchTextMax = DISP_TEXT_SIZE;
6124 if (!LISTVIEW_GetItemT(infoPtr, &dispInfo.item, isW)) return 0;
6126 infoPtr->hwndEdit = CreateEditLabelT(infoPtr, dispInfo.item.pszText, isW);
6127 if (!infoPtr->hwndEdit) return 0;
6129 if (notify_dispinfoT(infoPtr, LVN_BEGINLABELEDITW, &dispInfo, isW))
6131 if (!IsWindow(hwndSelf))
6132 return 0;
6133 SendMessageW(infoPtr->hwndEdit, WM_CLOSE, 0, 0);
6134 infoPtr->hwndEdit = 0;
6135 return 0;
6138 TRACE("disp text=%s\n", debugtext_t(dispInfo.item.pszText, isW));
6140 /* position and display edit box */
6141 hdc = GetDC(infoPtr->hwndSelf);
6143 /* select the font to get appropriate metric dimensions */
6144 if (infoPtr->hFont)
6145 hOldFont = SelectObject(hdc, infoPtr->hFont);
6147 /* use real edit box content, it could be altered during LVN_BEGINLABELEDIT notification */
6148 GetWindowTextW(infoPtr->hwndEdit, disptextW, DISP_TEXT_SIZE);
6149 TRACE("edit box text=%s\n", debugstr_w(disptextW));
6151 /* get string length in pixels */
6152 GetTextExtentPoint32W(hdc, disptextW, lstrlenW(disptextW), &sz);
6154 /* add extra spacing for the next character */
6155 GetTextMetricsW(hdc, &tm);
6156 sz.cx += tm.tmMaxCharWidth * 2;
6158 if (infoPtr->hFont)
6159 SelectObject(hdc, hOldFont);
6161 ReleaseDC(infoPtr->hwndSelf, hdc);
6163 sz.cy = rect.bottom - rect.top + 2;
6164 rect.left -= 2;
6165 rect.top -= 1;
6166 TRACE("moving edit (%ld,%ld)-(%ld,%ld)\n", rect.left, rect.top, sz.cx, sz.cy);
6167 MoveWindow(infoPtr->hwndEdit, rect.left, rect.top, sz.cx, sz.cy, FALSE);
6168 ShowWindow(infoPtr->hwndEdit, SW_NORMAL);
6169 SetFocus(infoPtr->hwndEdit);
6170 SendMessageW(infoPtr->hwndEdit, EM_SETSEL, 0, -1);
6171 return infoPtr->hwndEdit;
6175 /***
6176 * DESCRIPTION:
6177 * Ensures the specified item is visible, scrolling into view if necessary.
6179 * PARAMETER(S):
6180 * [I] infoPtr : valid pointer to the listview structure
6181 * [I] nItem : item index
6182 * [I] bPartial : partially or entirely visible
6184 * RETURN:
6185 * SUCCESS : TRUE
6186 * FAILURE : FALSE
6188 static BOOL LISTVIEW_EnsureVisible(LISTVIEW_INFO *infoPtr, INT nItem, BOOL bPartial)
6190 INT nScrollPosHeight = 0;
6191 INT nScrollPosWidth = 0;
6192 INT nHorzAdjust = 0;
6193 INT nVertAdjust = 0;
6194 INT nHorzDiff = 0;
6195 INT nVertDiff = 0;
6196 RECT rcItem, rcTemp;
6198 rcItem.left = LVIR_BOUNDS;
6199 if (!LISTVIEW_GetItemRect(infoPtr, nItem, &rcItem)) return FALSE;
6201 if (bPartial && IntersectRect(&rcTemp, &infoPtr->rcList, &rcItem)) return TRUE;
6203 if (rcItem.left < infoPtr->rcList.left || rcItem.right > infoPtr->rcList.right)
6205 /* scroll left/right, but in LV_VIEW_DETAILS mode */
6206 if (infoPtr->uView == LV_VIEW_LIST)
6207 nScrollPosWidth = infoPtr->nItemWidth;
6208 else if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
6209 nScrollPosWidth = 1;
6211 if (rcItem.left < infoPtr->rcList.left)
6213 nHorzAdjust = -1;
6214 if (infoPtr->uView != LV_VIEW_DETAILS) nHorzDiff = rcItem.left - infoPtr->rcList.left;
6216 else
6218 nHorzAdjust = 1;
6219 if (infoPtr->uView != LV_VIEW_DETAILS) nHorzDiff = rcItem.right - infoPtr->rcList.right;
6223 if (rcItem.top < infoPtr->rcList.top || rcItem.bottom > infoPtr->rcList.bottom)
6225 /* scroll up/down, but not in LVS_LIST mode */
6226 if (infoPtr->uView == LV_VIEW_DETAILS)
6227 nScrollPosHeight = infoPtr->nItemHeight;
6228 else if ((infoPtr->uView == LV_VIEW_ICON) || (infoPtr->uView == LV_VIEW_SMALLICON))
6229 nScrollPosHeight = 1;
6231 if (rcItem.top < infoPtr->rcList.top)
6233 nVertAdjust = -1;
6234 if (infoPtr->uView != LV_VIEW_LIST) nVertDiff = rcItem.top - infoPtr->rcList.top;
6236 else
6238 nVertAdjust = 1;
6239 if (infoPtr->uView != LV_VIEW_LIST) nVertDiff = rcItem.bottom - infoPtr->rcList.bottom;
6243 if (!nScrollPosWidth && !nScrollPosHeight) return TRUE;
6245 if (nScrollPosWidth)
6247 INT diff = nHorzDiff / nScrollPosWidth;
6248 if (nHorzDiff % nScrollPosWidth) diff += nHorzAdjust;
6249 LISTVIEW_HScroll(infoPtr, SB_INTERNAL, diff);
6252 if (nScrollPosHeight)
6254 INT diff = nVertDiff / nScrollPosHeight;
6255 if (nVertDiff % nScrollPosHeight) diff += nVertAdjust;
6256 LISTVIEW_VScroll(infoPtr, SB_INTERNAL, diff);
6259 return TRUE;
6262 /***
6263 * DESCRIPTION:
6264 * Searches for an item with specific characteristics.
6266 * PARAMETER(S):
6267 * [I] hwnd : window handle
6268 * [I] nStart : base item index
6269 * [I] lpFindInfo : item information to look for
6271 * RETURN:
6272 * SUCCESS : index of item
6273 * FAILURE : -1
6275 static INT LISTVIEW_FindItemW(const LISTVIEW_INFO *infoPtr, INT nStart,
6276 const LVFINDINFOW *lpFindInfo)
6278 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
6279 BOOL bWrap = FALSE, bNearest = FALSE;
6280 INT nItem = nStart + 1, nLast = infoPtr->nItemCount, nNearestItem = -1;
6281 ULONG xdist, ydist, dist, mindist = 0x7fffffff;
6282 POINT Position, Destination;
6283 int search_len = 0;
6284 LVITEMW lvItem;
6286 /* Search in virtual listviews should be done by application, not by
6287 listview control, so we just send LVN_ODFINDITEMW and return the result */
6288 if (infoPtr->dwStyle & LVS_OWNERDATA)
6290 NMLVFINDITEMW nmlv;
6292 nmlv.iStart = nStart;
6293 nmlv.lvfi = *lpFindInfo;
6294 return notify_hdr(infoPtr, LVN_ODFINDITEMW, (LPNMHDR)&nmlv.hdr);
6297 if (!lpFindInfo || nItem < 0) return -1;
6299 lvItem.mask = 0;
6300 if (lpFindInfo->flags & (LVFI_STRING | LVFI_PARTIAL | LVFI_SUBSTRING))
6302 lvItem.mask |= LVIF_TEXT;
6303 lvItem.pszText = szDispText;
6304 lvItem.cchTextMax = DISP_TEXT_SIZE;
6307 if (lpFindInfo->flags & LVFI_WRAP)
6308 bWrap = TRUE;
6310 if ((lpFindInfo->flags & LVFI_NEARESTXY) &&
6311 (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON))
6313 POINT Origin;
6314 RECT rcArea;
6316 LISTVIEW_GetOrigin(infoPtr, &Origin);
6317 Destination.x = lpFindInfo->pt.x - Origin.x;
6318 Destination.y = lpFindInfo->pt.y - Origin.y;
6319 switch(lpFindInfo->vkDirection)
6321 case VK_DOWN: Destination.y += infoPtr->nItemHeight; break;
6322 case VK_UP: Destination.y -= infoPtr->nItemHeight; break;
6323 case VK_RIGHT: Destination.x += infoPtr->nItemWidth; break;
6324 case VK_LEFT: Destination.x -= infoPtr->nItemWidth; break;
6325 case VK_HOME: Destination.x = Destination.y = 0; break;
6326 case VK_NEXT: Destination.y += infoPtr->rcList.bottom - infoPtr->rcList.top; break;
6327 case VK_PRIOR: Destination.y -= infoPtr->rcList.bottom - infoPtr->rcList.top; break;
6328 case VK_END:
6329 LISTVIEW_GetAreaRect(infoPtr, &rcArea);
6330 Destination.x = rcArea.right;
6331 Destination.y = rcArea.bottom;
6332 break;
6333 default: ERR("Unknown vkDirection=%d\n", lpFindInfo->vkDirection);
6335 bNearest = TRUE;
6337 else Destination.x = Destination.y = 0;
6339 /* if LVFI_PARAM is specified, all other flags are ignored */
6340 if (lpFindInfo->flags & LVFI_PARAM)
6342 lvItem.mask |= LVIF_PARAM;
6343 bNearest = FALSE;
6344 lvItem.mask &= ~LVIF_TEXT;
6347 nItem = bNearest ? -1 : nStart + 1;
6349 if (lpFindInfo->flags & (LVFI_PARTIAL | LVFI_SUBSTRING))
6350 search_len = lstrlenW(lpFindInfo->psz);
6352 again:
6353 for (; nItem < nLast; nItem++)
6355 lvItem.iItem = nItem;
6356 lvItem.iSubItem = 0;
6357 lvItem.pszText = szDispText;
6358 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) continue;
6360 if (lvItem.mask & LVIF_PARAM)
6362 if (lpFindInfo->lParam == lvItem.lParam)
6363 return nItem;
6364 else
6365 continue;
6368 if (lvItem.mask & LVIF_TEXT)
6370 if (lpFindInfo->flags & (LVFI_PARTIAL | LVFI_SUBSTRING))
6372 if (StrCmpNIW(lvItem.pszText, lpFindInfo->psz, search_len)) continue;
6374 else
6376 if (StrCmpIW(lvItem.pszText, lpFindInfo->psz)) continue;
6380 if (!bNearest) return nItem;
6382 /* This is very inefficient. To do a good job here,
6383 * we need a sorted array of (x,y) item positions */
6384 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
6386 /* compute the distance^2 to the destination */
6387 xdist = Destination.x - Position.x;
6388 ydist = Destination.y - Position.y;
6389 dist = xdist * xdist + ydist * ydist;
6391 /* remember the distance, and item if it's closer */
6392 if (dist < mindist)
6394 mindist = dist;
6395 nNearestItem = nItem;
6399 if (bWrap)
6401 nItem = 0;
6402 nLast = min(nStart + 1, infoPtr->nItemCount);
6403 bWrap = FALSE;
6404 goto again;
6407 return nNearestItem;
6410 /***
6411 * DESCRIPTION:
6412 * Searches for an item with specific characteristics.
6414 * PARAMETER(S):
6415 * [I] hwnd : window handle
6416 * [I] nStart : base item index
6417 * [I] lpFindInfo : item information to look for
6419 * RETURN:
6420 * SUCCESS : index of item
6421 * FAILURE : -1
6423 static INT LISTVIEW_FindItemA(const LISTVIEW_INFO *infoPtr, INT nStart,
6424 const LVFINDINFOA *lpFindInfo)
6426 LVFINDINFOW fiw;
6427 INT res;
6428 LPWSTR strW = NULL;
6430 memcpy(&fiw, lpFindInfo, sizeof(fiw));
6431 if (lpFindInfo->flags & (LVFI_STRING | LVFI_PARTIAL | LVFI_SUBSTRING))
6432 fiw.psz = strW = textdupTtoW((LPCWSTR)lpFindInfo->psz, FALSE);
6433 res = LISTVIEW_FindItemW(infoPtr, nStart, &fiw);
6434 textfreeT(strW, FALSE);
6435 return res;
6438 /***
6439 * DESCRIPTION:
6440 * Retrieves column attributes.
6442 * PARAMETER(S):
6443 * [I] infoPtr : valid pointer to the listview structure
6444 * [I] nColumn : column index
6445 * [IO] lpColumn : column information
6446 * [I] isW : if TRUE, then lpColumn is a LPLVCOLUMNW
6447 * otherwise it is in fact a LPLVCOLUMNA
6449 * RETURN:
6450 * SUCCESS : TRUE
6451 * FAILURE : FALSE
6453 static BOOL LISTVIEW_GetColumnT(const LISTVIEW_INFO *infoPtr, INT nColumn, LPLVCOLUMNW lpColumn, BOOL isW)
6455 COLUMN_INFO *lpColumnInfo;
6456 HDITEMW hdi;
6458 if (!lpColumn || nColumn < 0 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
6459 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, nColumn);
6461 /* initialize memory */
6462 ZeroMemory(&hdi, sizeof(hdi));
6464 if (lpColumn->mask & LVCF_TEXT)
6466 hdi.mask |= HDI_TEXT;
6467 hdi.pszText = lpColumn->pszText;
6468 hdi.cchTextMax = lpColumn->cchTextMax;
6471 if (lpColumn->mask & LVCF_IMAGE)
6472 hdi.mask |= HDI_IMAGE;
6474 if (lpColumn->mask & LVCF_ORDER)
6475 hdi.mask |= HDI_ORDER;
6477 if (lpColumn->mask & LVCF_SUBITEM)
6478 hdi.mask |= HDI_LPARAM;
6480 if (!SendMessageW(infoPtr->hwndHeader, isW ? HDM_GETITEMW : HDM_GETITEMA, nColumn, (LPARAM)&hdi)) return FALSE;
6482 if (lpColumn->mask & LVCF_FMT)
6483 lpColumn->fmt = lpColumnInfo->fmt;
6485 if (lpColumn->mask & LVCF_WIDTH)
6486 lpColumn->cx = lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left;
6488 if (lpColumn->mask & LVCF_IMAGE)
6489 lpColumn->iImage = hdi.iImage;
6491 if (lpColumn->mask & LVCF_ORDER)
6492 lpColumn->iOrder = hdi.iOrder;
6494 if (lpColumn->mask & LVCF_SUBITEM)
6495 lpColumn->iSubItem = hdi.lParam;
6497 if (lpColumn->mask & LVCF_MINWIDTH)
6498 lpColumn->cxMin = lpColumnInfo->cxMin;
6500 return TRUE;
6503 static inline BOOL LISTVIEW_GetColumnOrderArray(const LISTVIEW_INFO *infoPtr, INT iCount, LPINT lpiArray)
6505 if (!infoPtr->hwndHeader) return FALSE;
6506 return SendMessageW(infoPtr->hwndHeader, HDM_GETORDERARRAY, iCount, (LPARAM)lpiArray);
6509 /***
6510 * DESCRIPTION:
6511 * Retrieves the column width.
6513 * PARAMETER(S):
6514 * [I] infoPtr : valid pointer to the listview structure
6515 * [I] int : column index
6517 * RETURN:
6518 * SUCCESS : column width
6519 * FAILURE : zero
6521 static INT LISTVIEW_GetColumnWidth(const LISTVIEW_INFO *infoPtr, INT nColumn)
6523 INT nColumnWidth = 0;
6524 HDITEMW hdItem;
6526 TRACE("nColumn=%d\n", nColumn);
6528 /* we have a 'column' in LIST and REPORT mode only */
6529 switch(infoPtr->uView)
6531 case LV_VIEW_LIST:
6532 nColumnWidth = infoPtr->nItemWidth;
6533 break;
6534 case LV_VIEW_DETAILS:
6535 /* We are not using LISTVIEW_GetHeaderRect as this data is updated only after a HDN_ITEMCHANGED.
6536 * There is an application that subclasses the listview, calls LVM_GETCOLUMNWIDTH in the
6537 * HDN_ITEMCHANGED handler and goes into infinite recursion if it receives old data.
6539 hdItem.mask = HDI_WIDTH;
6540 if (!SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nColumn, (LPARAM)&hdItem))
6542 WARN("(%p): HDM_GETITEMW failed for item %d\n", infoPtr->hwndSelf, nColumn);
6543 return 0;
6545 nColumnWidth = hdItem.cxy;
6546 break;
6549 TRACE("nColumnWidth=%d\n", nColumnWidth);
6550 return nColumnWidth;
6553 /***
6554 * DESCRIPTION:
6555 * In list or report display mode, retrieves the number of items that can fit
6556 * vertically in the visible area. In icon or small icon display mode,
6557 * retrieves the total number of visible items.
6559 * PARAMETER(S):
6560 * [I] infoPtr : valid pointer to the listview structure
6562 * RETURN:
6563 * Number of fully visible items.
6565 static INT LISTVIEW_GetCountPerPage(const LISTVIEW_INFO *infoPtr)
6567 switch (infoPtr->uView)
6569 case LV_VIEW_ICON:
6570 case LV_VIEW_SMALLICON:
6571 return infoPtr->nItemCount;
6572 case LV_VIEW_DETAILS:
6573 return LISTVIEW_GetCountPerColumn(infoPtr);
6574 case LV_VIEW_LIST:
6575 return LISTVIEW_GetCountPerRow(infoPtr) * LISTVIEW_GetCountPerColumn(infoPtr);
6577 assert(FALSE);
6578 return 0;
6581 /***
6582 * DESCRIPTION:
6583 * Retrieves an image list handle.
6585 * PARAMETER(S):
6586 * [I] infoPtr : valid pointer to the listview structure
6587 * [I] nImageList : image list identifier
6589 * RETURN:
6590 * SUCCESS : image list handle
6591 * FAILURE : NULL
6593 static HIMAGELIST LISTVIEW_GetImageList(const LISTVIEW_INFO *infoPtr, INT nImageList)
6595 switch (nImageList)
6597 case LVSIL_NORMAL: return infoPtr->himlNormal;
6598 case LVSIL_SMALL: return infoPtr->himlSmall;
6599 case LVSIL_STATE: return infoPtr->himlState;
6600 case LVSIL_GROUPHEADER:
6601 FIXME("LVSIL_GROUPHEADER not supported\n");
6602 break;
6603 default:
6604 WARN("got unknown imagelist index - %d\n", nImageList);
6606 return NULL;
6609 /* LISTVIEW_GetISearchString */
6611 /***
6612 * DESCRIPTION:
6613 * Retrieves item attributes.
6615 * PARAMETER(S):
6616 * [I] hwnd : window handle
6617 * [IO] lpLVItem : item info
6618 * [I] isW : if TRUE, then lpLVItem is a LPLVITEMW,
6619 * if FALSE, then lpLVItem is a LPLVITEMA.
6621 * NOTE:
6622 * This is the internal 'GetItem' interface -- it tries to
6623 * be smart and avoid text copies, if possible, by modifying
6624 * lpLVItem->pszText to point to the text string. Please note
6625 * that this is not always possible (e.g. OWNERDATA), so on
6626 * entry you *must* supply valid values for pszText, and cchTextMax.
6627 * The only difference to the documented interface is that upon
6628 * return, you should use *only* the lpLVItem->pszText, rather than
6629 * the buffer pointer you provided on input. Most code already does
6630 * that, so it's not a problem.
6631 * For the two cases when the text must be copied (that is,
6632 * for LVM_GETITEM, and LVM_GETITEMTEXT), use LISTVIEW_GetItemExtT.
6634 * RETURN:
6635 * SUCCESS : TRUE
6636 * FAILURE : FALSE
6638 static BOOL LISTVIEW_GetItemT(const LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem, BOOL isW)
6640 ITEMHDR callbackHdr = { LPSTR_TEXTCALLBACKW, I_IMAGECALLBACK };
6641 BOOL is_subitem_invalid = FALSE;
6642 NMLVDISPINFOW dispInfo;
6643 ITEM_INFO *lpItem;
6644 ITEMHDR* pItemHdr;
6645 HDPA hdpaSubItems;
6647 TRACE("(item=%s, isW=%d)\n", debuglvitem_t(lpLVItem, isW), isW);
6649 if (!lpLVItem || lpLVItem->iItem < 0 || lpLVItem->iItem >= infoPtr->nItemCount)
6650 return FALSE;
6652 if (lpLVItem->mask == 0) return TRUE;
6653 TRACE("mask=%x\n", lpLVItem->mask);
6655 if (lpLVItem->iSubItem && (lpLVItem->mask & LVIF_STATE))
6656 lpLVItem->state = 0;
6658 /* a quick optimization if all we're asked is the focus state
6659 * these queries are worth optimising since they are common,
6660 * and can be answered in constant time, without the heavy accesses */
6661 if ( (lpLVItem->mask == LVIF_STATE) && (lpLVItem->stateMask == LVIS_FOCUSED) &&
6662 !(infoPtr->uCallbackMask & LVIS_FOCUSED) )
6664 lpLVItem->state = 0;
6665 if (infoPtr->nFocusedItem == lpLVItem->iItem && !lpLVItem->iSubItem)
6666 lpLVItem->state |= LVIS_FOCUSED;
6667 return TRUE;
6670 ZeroMemory(&dispInfo, sizeof(dispInfo));
6672 /* if the app stores all the data, handle it separately */
6673 if (infoPtr->dwStyle & LVS_OWNERDATA)
6675 dispInfo.item.state = 0;
6677 /* apparently, we should not callback for lParam in LVS_OWNERDATA */
6678 if ((lpLVItem->mask & ~(LVIF_STATE | LVIF_PARAM)) ||
6679 ((lpLVItem->mask & LVIF_STATE) && (infoPtr->uCallbackMask & lpLVItem->stateMask)))
6681 UINT mask = lpLVItem->mask;
6683 /* NOTE: copy only fields which we _know_ are initialized, some apps
6684 * depend on the uninitialized fields being 0 */
6685 dispInfo.item.mask = lpLVItem->mask & ~LVIF_PARAM;
6686 dispInfo.item.iItem = lpLVItem->iItem;
6687 dispInfo.item.iSubItem = lpLVItem->iSubItem;
6688 if (lpLVItem->mask & LVIF_TEXT)
6690 if (lpLVItem->mask & LVIF_NORECOMPUTE)
6691 /* reset mask */
6692 dispInfo.item.mask &= ~(LVIF_TEXT | LVIF_NORECOMPUTE);
6693 else
6695 dispInfo.item.pszText = lpLVItem->pszText;
6696 dispInfo.item.cchTextMax = lpLVItem->cchTextMax;
6699 if (lpLVItem->mask & LVIF_STATE)
6700 dispInfo.item.stateMask = lpLVItem->stateMask & infoPtr->uCallbackMask;
6701 /* could be zeroed on LVIF_NORECOMPUTE case */
6702 if (dispInfo.item.mask)
6704 notify_dispinfoT(infoPtr, LVN_GETDISPINFOW, &dispInfo, isW);
6705 dispInfo.item.stateMask = lpLVItem->stateMask;
6706 if (lpLVItem->mask & (LVIF_GROUPID|LVIF_COLUMNS))
6708 /* full size structure expected - _WIN32IE >= 0x560 */
6709 *lpLVItem = dispInfo.item;
6711 else if (lpLVItem->mask & LVIF_INDENT)
6713 /* indent member expected - _WIN32IE >= 0x300 */
6714 memcpy(lpLVItem, &dispInfo.item, offsetof( LVITEMW, iGroupId ));
6716 else
6718 /* minimal structure expected */
6719 memcpy(lpLVItem, &dispInfo.item, offsetof( LVITEMW, iIndent ));
6721 lpLVItem->mask = mask;
6722 TRACE(" getdispinfo(1):lpLVItem=%s\n", debuglvitem_t(lpLVItem, isW));
6726 /* make sure lParam is zeroed out */
6727 if (lpLVItem->mask & LVIF_PARAM) lpLVItem->lParam = 0;
6729 /* callback marked pointer required here */
6730 if ((lpLVItem->mask & LVIF_TEXT) && (lpLVItem->mask & LVIF_NORECOMPUTE))
6731 lpLVItem->pszText = LPSTR_TEXTCALLBACKW;
6733 /* we store only a little state, so if we're not asked, we're done */
6734 if (!(lpLVItem->mask & LVIF_STATE) || lpLVItem->iSubItem) return TRUE;
6736 /* if focus is handled by us, report it */
6737 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_FOCUSED )
6739 lpLVItem->state &= ~LVIS_FOCUSED;
6740 if (infoPtr->nFocusedItem == lpLVItem->iItem)
6741 lpLVItem->state |= LVIS_FOCUSED;
6744 /* and do the same for selection, if we handle it */
6745 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_SELECTED )
6747 lpLVItem->state &= ~LVIS_SELECTED;
6748 if (ranges_contain(infoPtr->selectionRanges, lpLVItem->iItem))
6749 lpLVItem->state |= LVIS_SELECTED;
6752 return TRUE;
6755 /* find the item and subitem structures before we proceed */
6756 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, lpLVItem->iItem);
6757 lpItem = DPA_GetPtr(hdpaSubItems, 0);
6758 assert (lpItem);
6760 if (lpLVItem->iSubItem)
6762 SUBITEM_INFO *lpSubItem = LISTVIEW_GetSubItemPtr(hdpaSubItems, lpLVItem->iSubItem);
6763 if (lpSubItem)
6764 pItemHdr = &lpSubItem->hdr;
6765 else
6767 pItemHdr = &callbackHdr;
6768 is_subitem_invalid = TRUE;
6771 else
6772 pItemHdr = &lpItem->hdr;
6774 /* Do we need to query the state from the app? */
6775 if ((lpLVItem->mask & LVIF_STATE) && infoPtr->uCallbackMask && (!lpLVItem->iSubItem || is_subitem_invalid))
6777 dispInfo.item.mask |= LVIF_STATE;
6778 dispInfo.item.stateMask = infoPtr->uCallbackMask;
6781 /* Do we need to enquire about the image? */
6782 if ((lpLVItem->mask & LVIF_IMAGE) && pItemHdr->iImage == I_IMAGECALLBACK &&
6783 (!lpLVItem->iSubItem || (infoPtr->dwLvExStyle & LVS_EX_SUBITEMIMAGES)))
6785 dispInfo.item.mask |= LVIF_IMAGE;
6786 dispInfo.item.iImage = I_IMAGECALLBACK;
6789 /* Only items support indentation */
6790 if ((lpLVItem->mask & LVIF_INDENT) && lpItem->iIndent == I_INDENTCALLBACK && !lpLVItem->iSubItem)
6792 dispInfo.item.mask |= LVIF_INDENT;
6793 dispInfo.item.iIndent = I_INDENTCALLBACK;
6796 /* Apps depend on calling back for text if it is NULL or LPSTR_TEXTCALLBACKW */
6797 if ((lpLVItem->mask & LVIF_TEXT) && !(lpLVItem->mask & LVIF_NORECOMPUTE) &&
6798 !is_text(pItemHdr->pszText))
6800 dispInfo.item.mask |= LVIF_TEXT;
6801 dispInfo.item.pszText = lpLVItem->pszText;
6802 dispInfo.item.cchTextMax = lpLVItem->cchTextMax;
6803 if (dispInfo.item.pszText && dispInfo.item.cchTextMax > 0)
6804 *dispInfo.item.pszText = '\0';
6807 /* If we don't have all the requested info, query the application */
6808 if (dispInfo.item.mask)
6810 dispInfo.item.iItem = lpLVItem->iItem;
6811 dispInfo.item.iSubItem = lpLVItem->iSubItem;
6812 dispInfo.item.lParam = lpItem->lParam;
6813 notify_dispinfoT(infoPtr, LVN_GETDISPINFOW, &dispInfo, isW);
6814 TRACE(" getdispinfo(2):item=%s\n", debuglvitem_t(&dispInfo.item, isW));
6817 /* we should not store values for subitems */
6818 if (lpLVItem->iSubItem) dispInfo.item.mask &= ~LVIF_DI_SETITEM;
6820 /* Now, handle the iImage field */
6821 if (dispInfo.item.mask & LVIF_IMAGE)
6823 lpLVItem->iImage = dispInfo.item.iImage;
6824 if ((dispInfo.item.mask & LVIF_DI_SETITEM) && pItemHdr->iImage == I_IMAGECALLBACK)
6825 pItemHdr->iImage = dispInfo.item.iImage;
6827 else if (lpLVItem->mask & LVIF_IMAGE)
6829 if (!lpLVItem->iSubItem || (infoPtr->dwLvExStyle & LVS_EX_SUBITEMIMAGES))
6830 lpLVItem->iImage = pItemHdr->iImage;
6831 else
6832 lpLVItem->iImage = 0;
6835 /* The pszText field */
6836 if (dispInfo.item.mask & LVIF_TEXT)
6838 if ((dispInfo.item.mask & LVIF_DI_SETITEM) && pItemHdr->pszText)
6839 textsetptrT(&pItemHdr->pszText, dispInfo.item.pszText, isW);
6841 lpLVItem->pszText = dispInfo.item.pszText;
6843 else if (lpLVItem->mask & LVIF_TEXT)
6845 /* if LVN_GETDISPINFO's disabled with LVIF_NORECOMPUTE return callback placeholder */
6846 if (isW || !is_text(pItemHdr->pszText)) lpLVItem->pszText = pItemHdr->pszText;
6847 else textcpynT(lpLVItem->pszText, isW, pItemHdr->pszText, TRUE, lpLVItem->cchTextMax);
6850 /* Next is the lParam field */
6851 if (dispInfo.item.mask & LVIF_PARAM)
6853 lpLVItem->lParam = dispInfo.item.lParam;
6854 if ((dispInfo.item.mask & LVIF_DI_SETITEM))
6855 lpItem->lParam = dispInfo.item.lParam;
6857 else if (lpLVItem->mask & LVIF_PARAM)
6858 lpLVItem->lParam = lpItem->lParam;
6860 /* if this is a subitem, we're done */
6861 if (lpLVItem->iSubItem) return TRUE;
6863 /* ... the state field (this one is different due to uCallbackmask) */
6864 if (lpLVItem->mask & LVIF_STATE)
6866 lpLVItem->state = lpItem->state & lpLVItem->stateMask;
6867 if (dispInfo.item.mask & LVIF_STATE)
6869 lpLVItem->state &= ~dispInfo.item.stateMask;
6870 lpLVItem->state |= (dispInfo.item.state & dispInfo.item.stateMask);
6872 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_FOCUSED )
6874 lpLVItem->state &= ~LVIS_FOCUSED;
6875 if (infoPtr->nFocusedItem == lpLVItem->iItem)
6876 lpLVItem->state |= LVIS_FOCUSED;
6878 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_SELECTED )
6880 lpLVItem->state &= ~LVIS_SELECTED;
6881 if (ranges_contain(infoPtr->selectionRanges, lpLVItem->iItem))
6882 lpLVItem->state |= LVIS_SELECTED;
6886 /* and last, but not least, the indent field */
6887 if (dispInfo.item.mask & LVIF_INDENT)
6889 lpLVItem->iIndent = dispInfo.item.iIndent;
6890 if ((dispInfo.item.mask & LVIF_DI_SETITEM) && lpItem->iIndent == I_INDENTCALLBACK)
6891 lpItem->iIndent = dispInfo.item.iIndent;
6893 else if (lpLVItem->mask & LVIF_INDENT)
6895 lpLVItem->iIndent = lpItem->iIndent;
6898 return TRUE;
6901 /***
6902 * DESCRIPTION:
6903 * Retrieves item attributes.
6905 * PARAMETER(S):
6906 * [I] hwnd : window handle
6907 * [IO] lpLVItem : item info
6908 * [I] isW : if TRUE, then lpLVItem is a LPLVITEMW,
6909 * if FALSE, then lpLVItem is a LPLVITEMA.
6911 * NOTE:
6912 * This is the external 'GetItem' interface -- it properly copies
6913 * the text in the provided buffer.
6915 * RETURN:
6916 * SUCCESS : TRUE
6917 * FAILURE : FALSE
6919 static BOOL LISTVIEW_GetItemExtT(const LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem, BOOL isW)
6921 LPWSTR pszText;
6922 BOOL bResult;
6924 if (!lpLVItem || lpLVItem->iItem < 0 || lpLVItem->iItem >= infoPtr->nItemCount)
6925 return FALSE;
6927 pszText = lpLVItem->pszText;
6928 bResult = LISTVIEW_GetItemT(infoPtr, lpLVItem, isW);
6929 if (bResult && (lpLVItem->mask & LVIF_TEXT) && lpLVItem->pszText != pszText)
6931 if (lpLVItem->pszText != LPSTR_TEXTCALLBACKW)
6932 textcpynT(pszText, isW, lpLVItem->pszText, isW, lpLVItem->cchTextMax);
6933 else
6934 pszText = LPSTR_TEXTCALLBACKW;
6936 lpLVItem->pszText = pszText;
6938 return bResult;
6942 /***
6943 * DESCRIPTION:
6944 * Retrieves the position (upper-left) of the listview control item.
6945 * Note that for LVS_ICON style, the upper-left is that of the icon
6946 * and not the bounding box.
6948 * PARAMETER(S):
6949 * [I] infoPtr : valid pointer to the listview structure
6950 * [I] nItem : item index
6951 * [O] lpptPosition : coordinate information
6953 * RETURN:
6954 * SUCCESS : TRUE
6955 * FAILURE : FALSE
6957 static BOOL LISTVIEW_GetItemPosition(const LISTVIEW_INFO *infoPtr, INT nItem, LPPOINT lpptPosition)
6959 POINT Origin;
6961 TRACE("(nItem=%d, lpptPosition=%p)\n", nItem, lpptPosition);
6963 if (!lpptPosition || nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
6965 LISTVIEW_GetOrigin(infoPtr, &Origin);
6966 LISTVIEW_GetItemOrigin(infoPtr, nItem, lpptPosition);
6968 if (infoPtr->uView == LV_VIEW_ICON)
6970 lpptPosition->x += (infoPtr->nItemWidth - infoPtr->iconSize.cx) / 2;
6971 lpptPosition->y += ICON_TOP_PADDING;
6973 lpptPosition->x += Origin.x;
6974 lpptPosition->y += Origin.y;
6976 TRACE (" lpptPosition=%s\n", wine_dbgstr_point(lpptPosition));
6977 return TRUE;
6981 /***
6982 * DESCRIPTION:
6983 * Retrieves the bounding rectangle for a listview control item.
6985 * PARAMETER(S):
6986 * [I] infoPtr : valid pointer to the listview structure
6987 * [I] nItem : item index
6988 * [IO] lprc : bounding rectangle coordinates
6989 * lprc->left specifies the portion of the item for which the bounding
6990 * rectangle will be retrieved.
6992 * LVIR_BOUNDS Returns the bounding rectangle of the entire item,
6993 * including the icon and label.
6995 * * For LVS_ICON
6996 * * Experiment shows that native control returns:
6997 * * width = min (48, length of text line)
6998 * * .left = position.x - (width - iconsize.cx)/2
6999 * * .right = .left + width
7000 * * height = #lines of text * ntmHeight + icon height + 8
7001 * * .top = position.y - 2
7002 * * .bottom = .top + height
7003 * * separation between items .y = itemSpacing.cy - height
7004 * * .x = itemSpacing.cx - width
7005 * LVIR_ICON Returns the bounding rectangle of the icon or small icon.
7007 * * For LVS_ICON
7008 * * Experiment shows that native control returns:
7009 * * width = iconSize.cx + 16
7010 * * .left = position.x - (width - iconsize.cx)/2
7011 * * .right = .left + width
7012 * * height = iconSize.cy + 4
7013 * * .top = position.y - 2
7014 * * .bottom = .top + height
7015 * * separation between items .y = itemSpacing.cy - height
7016 * * .x = itemSpacing.cx - width
7017 * LVIR_LABEL Returns the bounding rectangle of the item text.
7019 * * For LVS_ICON
7020 * * Experiment shows that native control returns:
7021 * * width = text length
7022 * * .left = position.x - width/2
7023 * * .right = .left + width
7024 * * height = ntmH * linecount + 2
7025 * * .top = position.y + iconSize.cy + 6
7026 * * .bottom = .top + height
7027 * * separation between items .y = itemSpacing.cy - height
7028 * * .x = itemSpacing.cx - width
7029 * LVIR_SELECTBOUNDS Returns the union of the LVIR_ICON and LVIR_LABEL
7030 * rectangles, but excludes columns in report view.
7032 * RETURN:
7033 * SUCCESS : TRUE
7034 * FAILURE : FALSE
7036 * NOTES
7037 * Note that the bounding rectangle of the label in the LVS_ICON view depends
7038 * upon whether the window has the focus currently and on whether the item
7039 * is the one with the focus. Ensure that the control's record of which
7040 * item has the focus agrees with the items' records.
7042 static BOOL LISTVIEW_GetItemRect(const LISTVIEW_INFO *infoPtr, INT nItem, LPRECT lprc)
7044 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
7045 BOOL doLabel = TRUE, oversizedBox = FALSE;
7046 POINT Position, Origin;
7047 LVITEMW lvItem;
7048 LONG mode;
7050 TRACE("(hwnd=%p, nItem=%d, lprc=%p)\n", infoPtr->hwndSelf, nItem, lprc);
7052 if (!lprc || nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
7054 LISTVIEW_GetOrigin(infoPtr, &Origin);
7055 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
7057 /* Be smart and try to figure out the minimum we have to do */
7058 if (lprc->left == LVIR_ICON) doLabel = FALSE;
7059 if (infoPtr->uView == LV_VIEW_DETAILS && lprc->left == LVIR_BOUNDS) doLabel = FALSE;
7060 if (infoPtr->uView == LV_VIEW_ICON && lprc->left != LVIR_ICON &&
7061 infoPtr->bFocus && LISTVIEW_GetItemState(infoPtr, nItem, LVIS_FOCUSED))
7062 oversizedBox = TRUE;
7064 /* get what we need from the item before hand, so we make
7065 * only one request. This can speed up things, if data
7066 * is stored on the app side */
7067 lvItem.mask = 0;
7068 if (infoPtr->uView == LV_VIEW_DETAILS) lvItem.mask |= LVIF_INDENT;
7069 if (doLabel) lvItem.mask |= LVIF_TEXT;
7070 lvItem.iItem = nItem;
7071 lvItem.iSubItem = 0;
7072 lvItem.pszText = szDispText;
7073 lvItem.cchTextMax = DISP_TEXT_SIZE;
7074 if (lvItem.mask && !LISTVIEW_GetItemW(infoPtr, &lvItem)) return FALSE;
7075 /* we got the state already up, simulate it here, to avoid a reget */
7076 if (infoPtr->uView == LV_VIEW_ICON && (lprc->left != LVIR_ICON))
7078 lvItem.mask |= LVIF_STATE;
7079 lvItem.stateMask = LVIS_FOCUSED;
7080 lvItem.state = (oversizedBox ? LVIS_FOCUSED : 0);
7083 if (infoPtr->uView == LV_VIEW_DETAILS && (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) && lprc->left == LVIR_SELECTBOUNDS)
7084 lprc->left = LVIR_BOUNDS;
7086 mode = lprc->left;
7087 switch(lprc->left)
7089 case LVIR_ICON:
7090 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, NULL, lprc, NULL, NULL);
7091 break;
7093 case LVIR_LABEL:
7094 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, NULL, NULL, NULL, lprc);
7095 break;
7097 case LVIR_BOUNDS:
7098 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, lprc, NULL, NULL, NULL, NULL);
7099 break;
7101 case LVIR_SELECTBOUNDS:
7102 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, lprc, NULL, NULL, NULL);
7103 break;
7105 default:
7106 WARN("Unknown value: %ld\n", lprc->left);
7107 return FALSE;
7110 if (infoPtr->uView == LV_VIEW_DETAILS)
7112 if (mode != LVIR_BOUNDS)
7113 OffsetRect(lprc, Origin.x + LISTVIEW_GetColumnInfo(infoPtr, 0)->rcHeader.left,
7114 Position.y + Origin.y);
7115 else
7116 OffsetRect(lprc, Origin.x, Position.y + Origin.y);
7118 else
7119 OffsetRect(lprc, Position.x + Origin.x, Position.y + Origin.y);
7121 TRACE(" rect=%s\n", wine_dbgstr_rect(lprc));
7123 return TRUE;
7126 /***
7127 * DESCRIPTION:
7128 * Retrieves the spacing between listview control items.
7130 * PARAMETER(S):
7131 * [I] infoPtr : valid pointer to the listview structure
7132 * [IO] lprc : rectangle to receive the output
7133 * on input, lprc->top = nSubItem
7134 * lprc->left = LVIR_ICON | LVIR_BOUNDS | LVIR_LABEL
7136 * NOTE: for subItem = 0, we should return the bounds of the _entire_ item,
7137 * not only those of the first column.
7139 * RETURN:
7140 * TRUE: success
7141 * FALSE: failure
7143 static BOOL LISTVIEW_GetSubItemRect(const LISTVIEW_INFO *infoPtr, INT item, LPRECT lprc)
7145 RECT rect = { 0, 0, 0, 0 };
7146 POINT origin;
7147 INT y;
7149 if (!lprc) return FALSE;
7151 TRACE("item %d, subitem %ld, type %ld\n", item, lprc->top, lprc->left);
7153 /* Subitem of '0' means item itself, and this works for all control view modes */
7154 if (lprc->top == 0)
7155 return LISTVIEW_GetItemRect(infoPtr, item, lprc);
7157 if (infoPtr->uView != LV_VIEW_DETAILS) return FALSE;
7159 LISTVIEW_GetOrigin(infoPtr, &origin);
7160 /* this works for any item index, no matter if it exists or not */
7161 y = item * infoPtr->nItemHeight + origin.y;
7163 if (infoPtr->hwndHeader && SendMessageW(infoPtr->hwndHeader, HDM_GETITEMRECT, lprc->top, (LPARAM)&rect))
7165 rect.top = 0;
7166 rect.bottom = infoPtr->nItemHeight;
7168 else
7170 /* Native implementation is broken for this case and garbage is left for left and right fields,
7171 we zero them to get predictable output */
7172 lprc->left = lprc->right = lprc->top = 0;
7173 lprc->bottom = infoPtr->nItemHeight;
7174 OffsetRect(lprc, origin.x, y);
7175 TRACE("return rect %s\n", wine_dbgstr_rect(lprc));
7176 return TRUE;
7179 switch (lprc->left)
7181 case LVIR_ICON:
7183 /* it doesn't matter if main item actually has an icon, if imagelist is set icon width is returned */
7184 if (infoPtr->himlSmall)
7185 rect.right = rect.left + infoPtr->iconSize.cx;
7186 else
7187 rect.right = rect.left;
7189 rect.bottom = rect.top + infoPtr->iconSize.cy;
7190 break;
7192 case LVIR_LABEL:
7193 case LVIR_BOUNDS:
7194 break;
7196 default:
7197 ERR("Unknown bounds %ld\n", lprc->left);
7198 return FALSE;
7201 OffsetRect(&rect, origin.x, y);
7202 *lprc = rect;
7203 TRACE("return rect %s\n", wine_dbgstr_rect(lprc));
7205 return TRUE;
7208 /***
7209 * DESCRIPTION:
7210 * Retrieves the spacing between listview control items.
7212 * PARAMETER(S):
7213 * [I] infoPtr : valid pointer to the listview structure
7214 * [I] bSmall : flag for small or large icon
7216 * RETURN:
7217 * Horizontal + vertical spacing
7219 static LONG LISTVIEW_GetItemSpacing(const LISTVIEW_INFO *infoPtr, BOOL bSmall)
7221 LONG lResult;
7223 if (!bSmall)
7225 lResult = MAKELONG(infoPtr->iconSpacing.cx, infoPtr->iconSpacing.cy);
7227 else
7229 if (infoPtr->uView == LV_VIEW_ICON)
7230 lResult = MAKELONG(DEFAULT_COLUMN_WIDTH, GetSystemMetrics(SM_CXSMICON)+HEIGHT_PADDING);
7231 else
7232 lResult = MAKELONG(infoPtr->nItemWidth, infoPtr->nItemHeight);
7234 return lResult;
7237 /***
7238 * DESCRIPTION:
7239 * Retrieves the state of a listview control item.
7241 * PARAMETER(S):
7242 * [I] infoPtr : valid pointer to the listview structure
7243 * [I] nItem : item index
7244 * [I] uMask : state mask
7246 * RETURN:
7247 * State specified by the mask.
7249 static UINT LISTVIEW_GetItemState(const LISTVIEW_INFO *infoPtr, INT nItem, UINT uMask)
7251 LVITEMW lvItem;
7253 if (nItem < 0 || nItem >= infoPtr->nItemCount) return 0;
7255 lvItem.iItem = nItem;
7256 lvItem.iSubItem = 0;
7257 lvItem.mask = LVIF_STATE;
7258 lvItem.stateMask = uMask;
7259 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return 0;
7261 return lvItem.state & uMask;
7264 /***
7265 * DESCRIPTION:
7266 * Retrieves the text of a listview control item or subitem.
7268 * PARAMETER(S):
7269 * [I] hwnd : window handle
7270 * [I] nItem : item index
7271 * [IO] lpLVItem : item information
7272 * [I] isW : TRUE if lpLVItem is Unicode
7274 * RETURN:
7275 * SUCCESS : string length
7276 * FAILURE : 0
7278 static INT LISTVIEW_GetItemTextT(const LISTVIEW_INFO *infoPtr, INT nItem, LPLVITEMW lpLVItem, BOOL isW)
7280 if (!lpLVItem || nItem < 0 || nItem >= infoPtr->nItemCount) return 0;
7282 lpLVItem->mask = LVIF_TEXT;
7283 lpLVItem->iItem = nItem;
7284 if (!LISTVIEW_GetItemExtT(infoPtr, lpLVItem, isW)) return 0;
7286 return textlenT(lpLVItem->pszText, isW);
7289 /***
7290 * DESCRIPTION:
7291 * Searches for an item based on properties + relationships.
7293 * PARAMETER(S):
7294 * [I] infoPtr : valid pointer to the listview structure
7295 * [I] nItem : item index
7296 * [I] uFlags : relationship flag
7298 * RETURN:
7299 * SUCCESS : item index
7300 * FAILURE : -1
7302 static INT LISTVIEW_GetNextItem(const LISTVIEW_INFO *infoPtr, INT nItem, UINT uFlags)
7304 UINT uMask = 0;
7305 LVFINDINFOW lvFindInfo;
7306 INT nCountPerColumn;
7307 INT nCountPerRow;
7308 INT i;
7310 TRACE("nItem=%d, uFlags=%x, nItemCount=%d\n", nItem, uFlags, infoPtr->nItemCount);
7311 if (nItem < -1 || nItem >= infoPtr->nItemCount) return -1;
7313 ZeroMemory(&lvFindInfo, sizeof(lvFindInfo));
7315 if (uFlags & LVNI_CUT)
7316 uMask |= LVIS_CUT;
7318 if (uFlags & LVNI_DROPHILITED)
7319 uMask |= LVIS_DROPHILITED;
7321 if (uFlags & LVNI_FOCUSED)
7322 uMask |= LVIS_FOCUSED;
7324 if (uFlags & LVNI_SELECTED)
7325 uMask |= LVIS_SELECTED;
7327 /* if we're asked for the focused item, that's only one,
7328 * so it's worth optimizing */
7329 if (uFlags & LVNI_FOCUSED)
7331 if ((LISTVIEW_GetItemState(infoPtr, infoPtr->nFocusedItem, uMask) & uMask) != uMask) return -1;
7332 return (infoPtr->nFocusedItem == nItem) ? -1 : infoPtr->nFocusedItem;
7335 if (uFlags & LVNI_ABOVE)
7337 if ((infoPtr->uView == LV_VIEW_LIST) || (infoPtr->uView == LV_VIEW_DETAILS))
7339 while (nItem >= 0)
7341 nItem--;
7342 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7343 return nItem;
7346 else
7348 /* Special case for autoarrange - move 'til the top of a list */
7349 if (is_autoarrange(infoPtr))
7351 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
7352 while (nItem - nCountPerRow >= 0)
7354 nItem -= nCountPerRow;
7355 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7356 return nItem;
7358 return -1;
7360 lvFindInfo.flags = LVFI_NEARESTXY;
7361 lvFindInfo.vkDirection = VK_UP;
7362 LISTVIEW_GetItemPosition(infoPtr, nItem, &lvFindInfo.pt);
7363 while ((nItem = LISTVIEW_FindItemW(infoPtr, nItem, &lvFindInfo)) != -1)
7365 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7366 return nItem;
7370 else if (uFlags & LVNI_BELOW)
7372 if ((infoPtr->uView == LV_VIEW_LIST) || (infoPtr->uView == LV_VIEW_DETAILS))
7374 while (nItem < infoPtr->nItemCount)
7376 nItem++;
7377 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7378 return nItem;
7381 else
7383 /* Special case for autoarrange - move 'til the bottom of a list */
7384 if (is_autoarrange(infoPtr))
7386 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
7387 while (nItem + nCountPerRow < infoPtr->nItemCount )
7389 nItem += nCountPerRow;
7390 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7391 return nItem;
7393 return -1;
7395 lvFindInfo.flags = LVFI_NEARESTXY;
7396 lvFindInfo.vkDirection = VK_DOWN;
7397 LISTVIEW_GetItemPosition(infoPtr, nItem, &lvFindInfo.pt);
7398 while ((nItem = LISTVIEW_FindItemW(infoPtr, nItem, &lvFindInfo)) != -1)
7400 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7401 return nItem;
7405 else if (uFlags & LVNI_TOLEFT)
7407 if (infoPtr->uView == LV_VIEW_LIST)
7409 nCountPerColumn = LISTVIEW_GetCountPerColumn(infoPtr);
7410 while (nItem - nCountPerColumn >= 0)
7412 nItem -= nCountPerColumn;
7413 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7414 return nItem;
7417 else if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
7419 /* Special case for autoarrange - move 'til the beginning of a row */
7420 if (is_autoarrange(infoPtr))
7422 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
7423 while (nItem % nCountPerRow > 0)
7425 nItem --;
7426 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7427 return nItem;
7429 return -1;
7431 lvFindInfo.flags = LVFI_NEARESTXY;
7432 lvFindInfo.vkDirection = VK_LEFT;
7433 LISTVIEW_GetItemPosition(infoPtr, nItem, &lvFindInfo.pt);
7434 while ((nItem = LISTVIEW_FindItemW(infoPtr, nItem, &lvFindInfo)) != -1)
7436 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7437 return nItem;
7441 else if (uFlags & LVNI_TORIGHT)
7443 if (infoPtr->uView == LV_VIEW_LIST)
7445 nCountPerColumn = LISTVIEW_GetCountPerColumn(infoPtr);
7446 while (nItem + nCountPerColumn < infoPtr->nItemCount)
7448 nItem += nCountPerColumn;
7449 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7450 return nItem;
7453 else if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
7455 /* Special case for autoarrange - move 'til the end of a row */
7456 if (is_autoarrange(infoPtr))
7458 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
7459 while (nItem % nCountPerRow < nCountPerRow - 1 )
7461 nItem ++;
7462 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7463 return nItem;
7465 return -1;
7467 lvFindInfo.flags = LVFI_NEARESTXY;
7468 lvFindInfo.vkDirection = VK_RIGHT;
7469 LISTVIEW_GetItemPosition(infoPtr, nItem, &lvFindInfo.pt);
7470 while ((nItem = LISTVIEW_FindItemW(infoPtr, nItem, &lvFindInfo)) != -1)
7472 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7473 return nItem;
7477 else
7479 nItem++;
7481 /* search by index */
7482 for (i = nItem; i < infoPtr->nItemCount; i++)
7484 if ((LISTVIEW_GetItemState(infoPtr, i, uMask) & uMask) == uMask)
7485 return i;
7489 return -1;
7492 static BOOL LISTVIEW_GetNextItemIndex(const LISTVIEW_INFO *infoPtr, LVITEMINDEX *index, UINT flags)
7494 /* FIXME: specified item group is ignored */
7496 if (!index)
7497 return FALSE;
7499 index->iItem = LISTVIEW_GetNextItem(infoPtr, index->iItem, flags);
7500 return index->iItem != -1;
7503 /* LISTVIEW_GetNumberOfWorkAreas */
7505 /***
7506 * DESCRIPTION:
7507 * Retrieves the origin coordinates when in icon or small icon display mode.
7509 * PARAMETER(S):
7510 * [I] infoPtr : valid pointer to the listview structure
7511 * [O] lpptOrigin : coordinate information
7513 * RETURN:
7514 * None.
7516 static void LISTVIEW_GetOrigin(const LISTVIEW_INFO *infoPtr, LPPOINT lpptOrigin)
7518 INT nHorzPos = 0, nVertPos = 0;
7519 SCROLLINFO scrollInfo;
7521 scrollInfo.cbSize = sizeof(SCROLLINFO);
7522 scrollInfo.fMask = SIF_POS;
7524 if (GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo))
7525 nHorzPos = scrollInfo.nPos;
7526 if (GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
7527 nVertPos = scrollInfo.nPos;
7529 TRACE("nHorzPos=%d, nVertPos=%d\n", nHorzPos, nVertPos);
7531 lpptOrigin->x = infoPtr->rcList.left;
7532 lpptOrigin->y = infoPtr->rcList.top;
7533 if (infoPtr->uView == LV_VIEW_LIST)
7534 nHorzPos *= infoPtr->nItemWidth;
7535 else if (infoPtr->uView == LV_VIEW_DETAILS)
7536 nVertPos *= infoPtr->nItemHeight;
7538 lpptOrigin->x -= nHorzPos;
7539 lpptOrigin->y -= nVertPos;
7541 TRACE(" origin=%s\n", wine_dbgstr_point(lpptOrigin));
7544 /***
7545 * DESCRIPTION:
7546 * Retrieves the width of a string.
7548 * PARAMETER(S):
7549 * [I] hwnd : window handle
7550 * [I] lpszText : text string to process
7551 * [I] isW : TRUE if lpszText is Unicode, FALSE otherwise
7553 * RETURN:
7554 * SUCCESS : string width (in pixels)
7555 * FAILURE : zero
7557 static INT LISTVIEW_GetStringWidthT(const LISTVIEW_INFO *infoPtr, LPCWSTR lpszText, BOOL isW)
7559 SIZE stringSize;
7561 stringSize.cx = 0;
7562 if (is_text(lpszText))
7564 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
7565 HDC hdc = GetDC(infoPtr->hwndSelf);
7566 HFONT hOldFont = SelectObject(hdc, hFont);
7568 if (isW)
7569 GetTextExtentPointW(hdc, lpszText, lstrlenW(lpszText), &stringSize);
7570 else
7571 GetTextExtentPointA(hdc, (LPCSTR)lpszText, lstrlenA((LPCSTR)lpszText), &stringSize);
7572 SelectObject(hdc, hOldFont);
7573 ReleaseDC(infoPtr->hwndSelf, hdc);
7575 return stringSize.cx;
7578 /***
7579 * DESCRIPTION:
7580 * Determines which listview item is located at the specified position.
7582 * PARAMETER(S):
7583 * [I] infoPtr : valid pointer to the listview structure
7584 * [IO] lpht : hit test information
7585 * [I] subitem : fill out iSubItem.
7586 * [I] select : return the index only if the hit selects the item
7588 * NOTE:
7589 * (mm 20001022): We must not allow iSubItem to be touched, for
7590 * an app might pass only a structure with space up to iItem!
7591 * (MS Office 97 does that for instance in the file open dialog)
7593 * RETURN:
7594 * SUCCESS : item index
7595 * FAILURE : -1
7597 static INT LISTVIEW_HitTest(const LISTVIEW_INFO *infoPtr, LPLVHITTESTINFO lpht, BOOL subitem, BOOL select)
7599 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
7600 RECT rcBox, rcBounds, rcState, rcIcon, rcLabel, rcSearch;
7601 POINT Origin, Position, opt;
7602 BOOL is_fullrow;
7603 LVITEMW lvItem;
7604 ITERATOR i;
7605 INT iItem;
7607 TRACE("(pt=%s, subitem=%d, select=%d)\n", wine_dbgstr_point(&lpht->pt), subitem, select);
7609 lpht->flags = 0;
7610 lpht->iItem = -1;
7611 if (subitem) lpht->iSubItem = 0;
7613 LISTVIEW_GetOrigin(infoPtr, &Origin);
7615 /* set whole list relation flags */
7616 if (subitem && infoPtr->uView == LV_VIEW_DETAILS)
7618 /* LVM_SUBITEMHITTEST checks left bound of possible client area */
7619 if (infoPtr->rcList.left > lpht->pt.x && Origin.x < lpht->pt.x)
7620 lpht->flags |= LVHT_TOLEFT;
7622 if (lpht->pt.y < infoPtr->rcList.top && lpht->pt.y >= 0)
7623 opt.y = lpht->pt.y + infoPtr->rcList.top;
7624 else
7625 opt.y = lpht->pt.y;
7627 if (infoPtr->rcList.bottom < opt.y)
7628 lpht->flags |= LVHT_BELOW;
7630 else
7632 if (infoPtr->rcList.left > lpht->pt.x)
7633 lpht->flags |= LVHT_TOLEFT;
7634 else if (infoPtr->rcList.right < lpht->pt.x)
7635 lpht->flags |= LVHT_TORIGHT;
7637 if (infoPtr->rcList.top > lpht->pt.y)
7638 lpht->flags |= LVHT_ABOVE;
7639 else if (infoPtr->rcList.bottom < lpht->pt.y)
7640 lpht->flags |= LVHT_BELOW;
7643 /* even if item is invalid try to find subitem */
7644 if (infoPtr->uView == LV_VIEW_DETAILS && subitem)
7646 RECT *pRect;
7647 INT j;
7649 opt.x = lpht->pt.x - Origin.x;
7651 lpht->iSubItem = -1;
7652 for (j = 0; j < DPA_GetPtrCount(infoPtr->hdpaColumns); j++)
7654 pRect = &LISTVIEW_GetColumnInfo(infoPtr, j)->rcHeader;
7656 if ((opt.x >= pRect->left) && (opt.x < pRect->right))
7658 lpht->iSubItem = j;
7659 break;
7662 TRACE("lpht->iSubItem=%d\n", lpht->iSubItem);
7664 /* if we're outside horizontal columns bounds there's nothing to test further */
7665 if (lpht->iSubItem == -1)
7667 lpht->iItem = -1;
7668 lpht->flags = LVHT_NOWHERE;
7669 return -1;
7673 TRACE("lpht->flags=0x%x\n", lpht->flags);
7674 if (lpht->flags) return -1;
7676 lpht->flags |= LVHT_NOWHERE;
7678 /* first deal with the large items */
7679 rcSearch.left = lpht->pt.x;
7680 rcSearch.top = lpht->pt.y;
7681 rcSearch.right = rcSearch.left + 1;
7682 rcSearch.bottom = rcSearch.top + 1;
7684 iterator_frameditems(&i, infoPtr, &rcSearch);
7685 iterator_next(&i); /* go to first item in the sequence */
7686 iItem = i.nItem;
7687 iterator_destroy(&i);
7689 TRACE("lpht->iItem=%d\n", iItem);
7690 if (iItem == -1) return -1;
7692 lvItem.mask = LVIF_STATE | LVIF_TEXT;
7693 if (infoPtr->uView == LV_VIEW_DETAILS) lvItem.mask |= LVIF_INDENT;
7694 lvItem.stateMask = LVIS_STATEIMAGEMASK;
7695 if (infoPtr->uView == LV_VIEW_ICON) lvItem.stateMask |= LVIS_FOCUSED;
7696 lvItem.iItem = iItem;
7697 lvItem.iSubItem = subitem ? lpht->iSubItem : 0;
7698 lvItem.pszText = szDispText;
7699 lvItem.cchTextMax = DISP_TEXT_SIZE;
7700 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return -1;
7701 if (!infoPtr->bFocus) lvItem.state &= ~LVIS_FOCUSED;
7703 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, &rcBox, NULL, &rcIcon, &rcState, &rcLabel);
7704 LISTVIEW_GetItemOrigin(infoPtr, iItem, &Position);
7705 opt.x = lpht->pt.x - Position.x - Origin.x;
7707 if (lpht->pt.y < infoPtr->rcList.top && lpht->pt.y >= 0)
7708 opt.y = lpht->pt.y - Position.y - Origin.y + infoPtr->rcList.top;
7709 else
7710 opt.y = lpht->pt.y - Position.y - Origin.y;
7712 if (infoPtr->uView == LV_VIEW_DETAILS)
7714 rcBounds = rcBox;
7715 if (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT)
7716 opt.x = lpht->pt.x - Origin.x;
7718 else
7720 UnionRect(&rcBounds, &rcIcon, &rcLabel);
7721 UnionRect(&rcBounds, &rcBounds, &rcState);
7723 TRACE("rcBounds=%s\n", wine_dbgstr_rect(&rcBounds));
7724 if (!PtInRect(&rcBounds, opt)) return -1;
7726 /* That's a special case - row rectangle is used as item rectangle and
7727 returned flags contain all item parts. */
7728 is_fullrow = (infoPtr->uView == LV_VIEW_DETAILS) && ((infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) || (infoPtr->dwStyle & LVS_OWNERDRAWFIXED));
7730 if (PtInRect(&rcIcon, opt))
7731 lpht->flags |= LVHT_ONITEMICON;
7732 else if (PtInRect(&rcLabel, opt))
7733 lpht->flags |= LVHT_ONITEMLABEL;
7734 else if (infoPtr->himlState && PtInRect(&rcState, opt))
7735 lpht->flags |= LVHT_ONITEMSTATEICON;
7736 if (is_fullrow && !(lpht->flags & LVHT_ONITEM))
7738 lpht->flags = LVHT_ONITEM | LVHT_ABOVE;
7740 if (lpht->flags & LVHT_ONITEM)
7741 lpht->flags &= ~LVHT_NOWHERE;
7742 TRACE("lpht->flags=0x%x\n", lpht->flags);
7744 if (select && !is_fullrow)
7746 if (infoPtr->uView == LV_VIEW_DETAILS)
7748 /* get main item bounds */
7749 lvItem.iSubItem = 0;
7750 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, &rcBox, NULL, &rcIcon, &rcState, &rcLabel);
7751 UnionRect(&rcBounds, &rcIcon, &rcLabel);
7752 UnionRect(&rcBounds, &rcBounds, &rcState);
7754 if (!PtInRect(&rcBounds, opt)) iItem = -1;
7756 return lpht->iItem = iItem;
7759 /***
7760 * DESCRIPTION:
7761 * Inserts a new item in the listview control.
7763 * PARAMETER(S):
7764 * [I] infoPtr : valid pointer to the listview structure
7765 * [I] lpLVItem : item information
7766 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
7768 * RETURN:
7769 * SUCCESS : new item index
7770 * FAILURE : -1
7772 static INT LISTVIEW_InsertItemT(LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem, BOOL isW)
7774 INT nItem;
7775 HDPA hdpaSubItems;
7776 NMLISTVIEW nmlv;
7777 ITEM_INFO *lpItem;
7778 ITEM_ID *lpID;
7779 BOOL is_sorted, has_changed;
7780 LVITEMW item;
7781 HWND hwndSelf = infoPtr->hwndSelf;
7783 TRACE("(item=%s, isW=%d)\n", debuglvitem_t(lpLVItem, isW), isW);
7785 if (infoPtr->dwStyle & LVS_OWNERDATA) return infoPtr->nItemCount++;
7787 /* make sure it's an item, and not a subitem; cannot insert a subitem */
7788 if (!lpLVItem || lpLVItem->iSubItem) return -1;
7790 if (!is_assignable_item(lpLVItem, infoPtr->dwStyle)) return -1;
7792 if (!(lpItem = Alloc(sizeof(*lpItem)))) return -1;
7794 /* insert item in listview control data structure */
7795 if ( !(hdpaSubItems = DPA_Create(8)) ) goto fail;
7796 if ( !DPA_SetPtr(hdpaSubItems, 0, lpItem) ) assert (FALSE);
7798 /* link with id struct */
7799 if (!(lpID = Alloc(sizeof(*lpID)))) goto fail;
7800 lpItem->id = lpID;
7801 lpID->item = hdpaSubItems;
7802 lpID->id = get_next_itemid(infoPtr);
7803 if ( DPA_InsertPtr(infoPtr->hdpaItemIds, infoPtr->nItemCount, lpID) == -1) goto fail;
7805 is_sorted = (infoPtr->dwStyle & (LVS_SORTASCENDING | LVS_SORTDESCENDING)) &&
7806 !(infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (LPSTR_TEXTCALLBACKW != lpLVItem->pszText);
7808 if (lpLVItem->iItem < 0 && !is_sorted) return -1;
7810 /* calculate new item index */
7811 if (is_sorted)
7813 HDPA hItem;
7814 ITEM_INFO *item_s;
7815 INT i = 0, cmpv;
7816 WCHAR *textW;
7818 textW = textdupTtoW(lpLVItem->pszText, isW);
7820 while (i < infoPtr->nItemCount)
7822 hItem = DPA_GetPtr( infoPtr->hdpaItems, i);
7823 item_s = DPA_GetPtr(hItem, 0);
7825 cmpv = textcmpWT(item_s->hdr.pszText, textW, TRUE);
7826 if (infoPtr->dwStyle & LVS_SORTDESCENDING) cmpv *= -1;
7828 if (cmpv >= 0) break;
7829 i++;
7832 textfreeT(textW, isW);
7834 nItem = i;
7836 else
7837 nItem = min(lpLVItem->iItem, infoPtr->nItemCount);
7839 TRACE("inserting at %d, sorted=%d, count=%d, iItem=%d\n", nItem, is_sorted, infoPtr->nItemCount, lpLVItem->iItem);
7840 nItem = DPA_InsertPtr( infoPtr->hdpaItems, nItem, hdpaSubItems );
7841 if (nItem == -1) goto fail;
7842 infoPtr->nItemCount++;
7844 /* shift indices first so they don't get tangled */
7845 LISTVIEW_ShiftIndices(infoPtr, nItem, 1);
7847 /* set the item attributes */
7848 if (lpLVItem->mask & (LVIF_GROUPID|LVIF_COLUMNS))
7850 /* full size structure expected - _WIN32IE >= 0x560 */
7851 item = *lpLVItem;
7853 else if (lpLVItem->mask & LVIF_INDENT)
7855 /* indent member expected - _WIN32IE >= 0x300 */
7856 memcpy(&item, lpLVItem, offsetof( LVITEMW, iGroupId ));
7858 else
7860 /* minimal structure expected */
7861 memcpy(&item, lpLVItem, offsetof( LVITEMW, iIndent ));
7863 item.iItem = nItem;
7864 if (infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES)
7866 if (item.mask & LVIF_STATE)
7868 item.stateMask |= LVIS_STATEIMAGEMASK;
7869 item.state &= ~LVIS_STATEIMAGEMASK;
7870 item.state |= INDEXTOSTATEIMAGEMASK(1);
7872 else
7874 item.mask |= LVIF_STATE;
7875 item.stateMask = LVIS_STATEIMAGEMASK;
7876 item.state = INDEXTOSTATEIMAGEMASK(1);
7880 if (!set_main_item(infoPtr, &item, TRUE, isW, &has_changed)) goto undo;
7882 /* make room for the position, if we are in the right mode */
7883 if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
7885 if (DPA_InsertPtr(infoPtr->hdpaPosX, nItem, 0) == -1)
7886 goto undo;
7887 if (DPA_InsertPtr(infoPtr->hdpaPosY, nItem, 0) == -1)
7889 DPA_DeletePtr(infoPtr->hdpaPosX, nItem);
7890 goto undo;
7894 /* send LVN_INSERTITEM notification */
7895 memset(&nmlv, 0, sizeof(NMLISTVIEW));
7896 nmlv.iItem = nItem;
7897 nmlv.lParam = lpItem->lParam;
7898 notify_listview(infoPtr, LVN_INSERTITEM, &nmlv);
7899 if (!IsWindow(hwndSelf))
7900 return -1;
7902 /* align items (set position of each item) */
7903 if (infoPtr->uView == LV_VIEW_SMALLICON || infoPtr->uView == LV_VIEW_ICON)
7905 POINT pt;
7907 if (infoPtr->dwStyle & LVS_ALIGNLEFT)
7908 LISTVIEW_NextIconPosLeft(infoPtr, &pt);
7909 else
7910 LISTVIEW_NextIconPosTop(infoPtr, &pt);
7912 LISTVIEW_MoveIconTo(infoPtr, nItem, &pt, TRUE);
7915 /* now is the invalidation fun */
7916 LISTVIEW_ScrollOnInsert(infoPtr, nItem, 1);
7917 return nItem;
7919 undo:
7920 LISTVIEW_ShiftIndices(infoPtr, nItem, -1);
7921 LISTVIEW_ShiftFocus(infoPtr, infoPtr->nFocusedItem, nItem, -1);
7922 DPA_DeletePtr(infoPtr->hdpaItems, nItem);
7923 infoPtr->nItemCount--;
7924 fail:
7925 DPA_DeletePtr(hdpaSubItems, 0);
7926 DPA_Destroy (hdpaSubItems);
7927 Free (lpItem);
7928 return -1;
7931 /***
7932 * DESCRIPTION:
7933 * Checks item visibility.
7935 * PARAMETER(S):
7936 * [I] infoPtr : valid pointer to the listview structure
7937 * [I] nFirst : item index to check for
7939 * RETURN:
7940 * Item visible : TRUE
7941 * Item invisible or failure : FALSE
7943 static BOOL LISTVIEW_IsItemVisible(const LISTVIEW_INFO *infoPtr, INT nItem)
7945 POINT Origin, Position;
7946 RECT rcItem;
7947 HDC hdc;
7948 BOOL ret;
7950 TRACE("nItem=%d\n", nItem);
7952 if (nItem < 0 || nItem >= DPA_GetPtrCount(infoPtr->hdpaItems)) return FALSE;
7954 LISTVIEW_GetOrigin(infoPtr, &Origin);
7955 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
7956 rcItem.left = Position.x + Origin.x;
7957 rcItem.top = Position.y + Origin.y;
7958 rcItem.right = rcItem.left + infoPtr->nItemWidth;
7959 rcItem.bottom = rcItem.top + infoPtr->nItemHeight;
7961 hdc = GetDC(infoPtr->hwndSelf);
7962 if (!hdc) return FALSE;
7963 ret = RectVisible(hdc, &rcItem);
7964 ReleaseDC(infoPtr->hwndSelf, hdc);
7966 return ret;
7969 /***
7970 * DESCRIPTION:
7971 * Redraws a range of items.
7973 * PARAMETER(S):
7974 * [I] infoPtr : valid pointer to the listview structure
7975 * [I] nFirst : first item
7976 * [I] nLast : last item
7978 * RETURN:
7979 * SUCCESS : TRUE
7980 * FAILURE : FALSE
7982 static BOOL LISTVIEW_RedrawItems(const LISTVIEW_INFO *infoPtr, INT nFirst, INT nLast)
7984 INT i;
7986 for (i = max(nFirst, 0); i <= min(nLast, infoPtr->nItemCount - 1); i++)
7987 LISTVIEW_InvalidateItem(infoPtr, i);
7989 return TRUE;
7992 /***
7993 * DESCRIPTION:
7994 * Scroll the content of a listview.
7996 * PARAMETER(S):
7997 * [I] infoPtr : valid pointer to the listview structure
7998 * [I] dx : horizontal scroll amount in pixels
7999 * [I] dy : vertical scroll amount in pixels
8001 * RETURN:
8002 * SUCCESS : TRUE
8003 * FAILURE : FALSE
8005 * COMMENTS:
8006 * If the control is in report view (LV_VIEW_DETAILS) the control can
8007 * be scrolled only in line increments. "dy" will be rounded to the
8008 * nearest number of pixels that are a whole line. Ex: if line height
8009 * is 16 and an 8 is passed, the list will be scrolled by 16. If a 7
8010 * is passed, then the scroll will be 0. (per MSDN 7/2002)
8012 static BOOL LISTVIEW_Scroll(LISTVIEW_INFO *infoPtr, INT dx, INT dy)
8014 switch(infoPtr->uView) {
8015 case LV_VIEW_DETAILS:
8016 dy += (dy < 0 ? -1 : 1) * infoPtr->nItemHeight/2;
8017 dy /= infoPtr->nItemHeight;
8018 break;
8019 case LV_VIEW_LIST:
8020 if (dy != 0) return FALSE;
8021 break;
8022 default: /* icon */
8023 break;
8026 if (dx != 0) LISTVIEW_HScroll(infoPtr, SB_INTERNAL, dx);
8027 if (dy != 0) LISTVIEW_VScroll(infoPtr, SB_INTERNAL, dy);
8029 return TRUE;
8032 /***
8033 * DESCRIPTION:
8034 * Sets the background color.
8036 * PARAMETER(S):
8037 * [I] infoPtr : valid pointer to the listview structure
8038 * [I] color : background color
8040 * RETURN:
8041 * SUCCESS : TRUE
8042 * FAILURE : FALSE
8044 static BOOL LISTVIEW_SetBkColor(LISTVIEW_INFO *infoPtr, COLORREF color)
8046 TRACE("color %lx\n", color);
8048 if(infoPtr->clrBk != color) {
8049 if (infoPtr->clrBk != CLR_NONE) DeleteObject(infoPtr->hBkBrush);
8050 infoPtr->clrBk = color;
8051 if (color == CLR_NONE)
8052 infoPtr->hBkBrush = (HBRUSH)GetClassLongPtrW(infoPtr->hwndSelf, GCLP_HBRBACKGROUND);
8053 else
8055 infoPtr->hBkBrush = CreateSolidBrush(color);
8056 infoPtr->dwLvExStyle &= ~LVS_EX_TRANSPARENTBKGND;
8060 return TRUE;
8063 static BOOL LISTVIEW_SetBkImage(LISTVIEW_INFO *infoPtr, const LVBKIMAGEW *image, BOOL isW)
8065 TRACE("%08lx, %p, %p, %u, %d, %d\n", image->ulFlags, image->hbm, image->pszImage,
8066 image->cchImageMax, image->xOffsetPercent, image->yOffsetPercent);
8068 if (image->ulFlags & ~LVBKIF_SOURCE_MASK)
8069 FIXME("unsupported flags %08lx\n", image->ulFlags & ~LVBKIF_SOURCE_MASK);
8071 if (image->xOffsetPercent || image->yOffsetPercent)
8072 FIXME("unsupported offset %d,%d\n", image->xOffsetPercent, image->yOffsetPercent);
8074 switch (image->ulFlags & LVBKIF_SOURCE_MASK)
8076 case LVBKIF_SOURCE_NONE:
8077 if (infoPtr->hBkBitmap)
8079 DeleteObject(infoPtr->hBkBitmap);
8080 infoPtr->hBkBitmap = NULL;
8082 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
8083 break;
8085 case LVBKIF_SOURCE_HBITMAP:
8087 BITMAP bm;
8089 if (infoPtr->hBkBitmap)
8091 DeleteObject(infoPtr->hBkBitmap);
8092 infoPtr->hBkBitmap = NULL;
8094 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
8095 if (GetObjectW(image->hbm, sizeof(bm), &bm) == sizeof(bm))
8097 infoPtr->hBkBitmap = image->hbm;
8098 return TRUE;
8100 break;
8103 case LVBKIF_SOURCE_URL:
8104 FIXME("LVBKIF_SOURCE_URL: %s\n", isW ? debugstr_w(image->pszImage) : debugstr_a((LPCSTR)image->pszImage));
8105 break;
8108 return FALSE;
8111 /*** Helper for {Insert,Set}ColumnT *only* */
8112 static void column_fill_hditem(const LISTVIEW_INFO *infoPtr, HDITEMW *lphdi, INT nColumn,
8113 const LVCOLUMNW *lpColumn, BOOL isW)
8115 if (lpColumn->mask & LVCF_FMT)
8117 /* format member is valid */
8118 lphdi->mask |= HDI_FORMAT;
8120 /* set text alignment (leftmost column must be left-aligned) */
8121 if (nColumn == 0 || (lpColumn->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_LEFT)
8122 lphdi->fmt |= HDF_LEFT;
8123 else if ((lpColumn->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_RIGHT)
8124 lphdi->fmt |= HDF_RIGHT;
8125 else if ((lpColumn->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_CENTER)
8126 lphdi->fmt |= HDF_CENTER;
8128 if (lpColumn->fmt & LVCFMT_BITMAP_ON_RIGHT)
8129 lphdi->fmt |= HDF_BITMAP_ON_RIGHT;
8131 if (lpColumn->fmt & LVCFMT_COL_HAS_IMAGES)
8133 lphdi->fmt |= HDF_IMAGE;
8134 lphdi->iImage = I_IMAGECALLBACK;
8137 if (lpColumn->fmt & LVCFMT_FIXED_WIDTH)
8138 lphdi->fmt |= HDF_FIXEDWIDTH;
8141 if (lpColumn->mask & LVCF_WIDTH)
8143 lphdi->mask |= HDI_WIDTH;
8144 if(lpColumn->cx == LVSCW_AUTOSIZE_USEHEADER)
8146 /* make it fill the remainder of the controls width */
8147 RECT rcHeader;
8148 INT item_index;
8150 for(item_index = 0; item_index < (nColumn - 1); item_index++)
8152 LISTVIEW_GetHeaderRect(infoPtr, item_index, &rcHeader);
8153 lphdi->cxy += rcHeader.right - rcHeader.left;
8156 /* retrieve the layout of the header */
8157 GetClientRect(infoPtr->hwndSelf, &rcHeader);
8158 TRACE("start cxy=%d rcHeader=%s\n", lphdi->cxy, wine_dbgstr_rect(&rcHeader));
8160 lphdi->cxy = (rcHeader.right - rcHeader.left) - lphdi->cxy;
8162 else
8163 lphdi->cxy = lpColumn->cx;
8166 if (lpColumn->mask & LVCF_TEXT)
8168 lphdi->mask |= HDI_TEXT | HDI_FORMAT;
8169 lphdi->fmt |= HDF_STRING;
8170 lphdi->pszText = lpColumn->pszText;
8171 lphdi->cchTextMax = textlenT(lpColumn->pszText, isW);
8174 if (lpColumn->mask & LVCF_IMAGE)
8176 lphdi->mask |= HDI_IMAGE;
8177 lphdi->iImage = lpColumn->iImage;
8180 if (lpColumn->mask & LVCF_ORDER)
8182 lphdi->mask |= HDI_ORDER;
8183 lphdi->iOrder = lpColumn->iOrder;
8188 /***
8189 * DESCRIPTION:
8190 * Inserts a new column.
8192 * PARAMETER(S):
8193 * [I] infoPtr : valid pointer to the listview structure
8194 * [I] nColumn : column index
8195 * [I] lpColumn : column information
8196 * [I] isW : TRUE if lpColumn is Unicode, FALSE otherwise
8198 * RETURN:
8199 * SUCCESS : new column index
8200 * FAILURE : -1
8202 static INT LISTVIEW_InsertColumnT(LISTVIEW_INFO *infoPtr, INT nColumn,
8203 const LVCOLUMNW *lpColumn, BOOL isW)
8205 COLUMN_INFO *lpColumnInfo;
8206 INT nNewColumn;
8207 HDITEMW hdi;
8209 TRACE("(nColumn=%d, lpColumn=%s, isW=%d)\n", nColumn, debuglvcolumn_t(lpColumn, isW), isW);
8211 if (!lpColumn || nColumn < 0) return -1;
8212 nColumn = min(nColumn, DPA_GetPtrCount(infoPtr->hdpaColumns));
8214 ZeroMemory(&hdi, sizeof(HDITEMW));
8215 column_fill_hditem(infoPtr, &hdi, nColumn, lpColumn, isW);
8218 * A mask not including LVCF_WIDTH turns into a mask of width, width 10
8219 * (can be seen in SPY) otherwise column never gets added.
8221 if (!(lpColumn->mask & LVCF_WIDTH)) {
8222 hdi.mask |= HDI_WIDTH;
8223 hdi.cxy = 10;
8227 * when the iSubItem is available Windows copies it to the header lParam. It seems
8228 * to happen only in LVM_INSERTCOLUMN - not in LVM_SETCOLUMN
8230 if (lpColumn->mask & LVCF_SUBITEM)
8232 hdi.mask |= HDI_LPARAM;
8233 hdi.lParam = lpColumn->iSubItem;
8236 /* create header if not present */
8237 LISTVIEW_CreateHeader(infoPtr);
8238 if (!(LVS_NOCOLUMNHEADER & infoPtr->dwStyle) &&
8239 (infoPtr->uView == LV_VIEW_DETAILS) && (WS_VISIBLE & infoPtr->dwStyle))
8241 ShowWindow(infoPtr->hwndHeader, SW_SHOWNORMAL);
8244 /* insert item in header control */
8245 nNewColumn = SendMessageW(infoPtr->hwndHeader,
8246 isW ? HDM_INSERTITEMW : HDM_INSERTITEMA,
8247 nColumn, (LPARAM)&hdi);
8248 if (nNewColumn == -1) return -1;
8249 if (nNewColumn != nColumn) ERR("nColumn=%d, nNewColumn=%d\n", nColumn, nNewColumn);
8251 /* create our own column info */
8252 if (!(lpColumnInfo = Alloc(sizeof(*lpColumnInfo)))) goto fail;
8253 if (DPA_InsertPtr(infoPtr->hdpaColumns, nNewColumn, lpColumnInfo) == -1) goto fail;
8255 if (lpColumn->mask & LVCF_FMT) lpColumnInfo->fmt = lpColumn->fmt;
8256 if (lpColumn->mask & LVCF_MINWIDTH) lpColumnInfo->cxMin = lpColumn->cxMin;
8257 if (!SendMessageW(infoPtr->hwndHeader, HDM_GETITEMRECT, nNewColumn, (LPARAM)&lpColumnInfo->rcHeader))
8258 goto fail;
8260 /* now we have to actually adjust the data */
8261 if (!(infoPtr->dwStyle & LVS_OWNERDATA) && infoPtr->nItemCount > 0)
8263 SUBITEM_INFO *lpSubItem;
8264 HDPA hdpaSubItems;
8265 INT nItem, i;
8266 LVITEMW item;
8267 BOOL changed;
8269 item.iSubItem = nNewColumn;
8270 item.mask = LVIF_TEXT | LVIF_IMAGE;
8271 item.iImage = I_IMAGECALLBACK;
8272 item.pszText = LPSTR_TEXTCALLBACKW;
8274 for (nItem = 0; nItem < infoPtr->nItemCount; nItem++)
8276 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, nItem);
8277 for (i = 1; i < DPA_GetPtrCount(hdpaSubItems); i++)
8279 lpSubItem = DPA_GetPtr(hdpaSubItems, i);
8280 if (lpSubItem->iSubItem >= nNewColumn)
8281 lpSubItem->iSubItem++;
8284 /* add new subitem for each item */
8285 item.iItem = nItem;
8286 set_sub_item(infoPtr, &item, isW, &changed);
8290 /* make space for the new column */
8291 LISTVIEW_ScrollColumns(infoPtr, nNewColumn + 1, lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left);
8292 LISTVIEW_UpdateItemSize(infoPtr);
8294 return nNewColumn;
8296 fail:
8297 if (nNewColumn != -1) SendMessageW(infoPtr->hwndHeader, HDM_DELETEITEM, nNewColumn, 0);
8298 if (lpColumnInfo)
8300 DPA_DeletePtr(infoPtr->hdpaColumns, nNewColumn);
8301 Free(lpColumnInfo);
8303 return -1;
8306 /***
8307 * DESCRIPTION:
8308 * Sets the attributes of a header item.
8310 * PARAMETER(S):
8311 * [I] infoPtr : valid pointer to the listview structure
8312 * [I] nColumn : column index
8313 * [I] lpColumn : column attributes
8314 * [I] isW: if TRUE, then lpColumn is a LPLVCOLUMNW, else it is a LPLVCOLUMNA
8316 * RETURN:
8317 * SUCCESS : TRUE
8318 * FAILURE : FALSE
8320 static BOOL LISTVIEW_SetColumnT(const LISTVIEW_INFO *infoPtr, INT nColumn,
8321 const LVCOLUMNW *lpColumn, BOOL isW)
8323 HDITEMW hdi, hdiget;
8324 BOOL bResult;
8326 TRACE("(nColumn=%d, lpColumn=%s, isW=%d)\n", nColumn, debuglvcolumn_t(lpColumn, isW), isW);
8328 if (!lpColumn || nColumn < 0 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
8330 ZeroMemory(&hdi, sizeof(HDITEMW));
8331 if (lpColumn->mask & LVCF_FMT)
8333 hdi.mask |= HDI_FORMAT;
8334 hdiget.mask = HDI_FORMAT;
8335 if (SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nColumn, (LPARAM)&hdiget))
8336 hdi.fmt = hdiget.fmt & HDF_STRING;
8338 column_fill_hditem(infoPtr, &hdi, nColumn, lpColumn, isW);
8340 /* set header item attributes */
8341 bResult = SendMessageW(infoPtr->hwndHeader, isW ? HDM_SETITEMW : HDM_SETITEMA, nColumn, (LPARAM)&hdi);
8342 if (!bResult) return FALSE;
8344 if (lpColumn->mask & LVCF_FMT)
8346 COLUMN_INFO *lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, nColumn);
8347 INT oldFmt = lpColumnInfo->fmt;
8349 lpColumnInfo->fmt = lpColumn->fmt;
8350 if ((oldFmt ^ lpColumn->fmt) & (LVCFMT_JUSTIFYMASK | LVCFMT_IMAGE))
8352 if (infoPtr->uView == LV_VIEW_DETAILS) LISTVIEW_InvalidateColumn(infoPtr, nColumn);
8356 if (lpColumn->mask & LVCF_MINWIDTH)
8357 LISTVIEW_GetColumnInfo(infoPtr, nColumn)->cxMin = lpColumn->cxMin;
8359 return TRUE;
8362 /***
8363 * DESCRIPTION:
8364 * Sets the column order array
8366 * PARAMETERS:
8367 * [I] infoPtr : valid pointer to the listview structure
8368 * [I] iCount : number of elements in column order array
8369 * [I] lpiArray : pointer to column order array
8371 * RETURN:
8372 * SUCCESS : TRUE
8373 * FAILURE : FALSE
8375 static BOOL LISTVIEW_SetColumnOrderArray(LISTVIEW_INFO *infoPtr, INT iCount, const INT *lpiArray)
8377 if (!infoPtr->hwndHeader) return FALSE;
8378 infoPtr->colRectsDirty = TRUE;
8379 return SendMessageW(infoPtr->hwndHeader, HDM_SETORDERARRAY, iCount, (LPARAM)lpiArray);
8382 /***
8383 * DESCRIPTION:
8384 * Sets the width of a column
8386 * PARAMETERS:
8387 * [I] infoPtr : valid pointer to the listview structure
8388 * [I] nColumn : column index
8389 * [I] cx : column width
8391 * RETURN:
8392 * SUCCESS : TRUE
8393 * FAILURE : FALSE
8395 static BOOL LISTVIEW_SetColumnWidth(LISTVIEW_INFO *infoPtr, INT nColumn, INT cx)
8397 WCHAR szDispText[DISP_TEXT_SIZE] = { 0 };
8398 INT max_cx = 0;
8399 HDITEMW hdi;
8401 TRACE("(nColumn=%d, cx=%d)\n", nColumn, cx);
8403 /* set column width only if in report or list mode */
8404 if (infoPtr->uView != LV_VIEW_DETAILS && infoPtr->uView != LV_VIEW_LIST) return FALSE;
8406 /* take care of invalid cx values - LVSCW_AUTOSIZE_* values are negative,
8407 with _USEHEADER being the lowest */
8408 if (infoPtr->uView == LV_VIEW_DETAILS && cx < LVSCW_AUTOSIZE_USEHEADER) cx = LVSCW_AUTOSIZE;
8409 else if (infoPtr->uView == LV_VIEW_LIST && cx <= 0) return FALSE;
8411 /* resize all columns if in LV_VIEW_LIST mode */
8412 if(infoPtr->uView == LV_VIEW_LIST)
8414 infoPtr->nItemWidth = cx;
8415 LISTVIEW_InvalidateList(infoPtr);
8416 return TRUE;
8419 if (nColumn < 0 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
8421 if (cx == LVSCW_AUTOSIZE || (cx == LVSCW_AUTOSIZE_USEHEADER && nColumn < DPA_GetPtrCount(infoPtr->hdpaColumns) -1))
8423 INT nLabelWidth;
8424 LVITEMW lvItem;
8426 lvItem.mask = LVIF_TEXT;
8427 lvItem.iItem = 0;
8428 lvItem.iSubItem = nColumn;
8429 lvItem.cchTextMax = DISP_TEXT_SIZE;
8430 for (; lvItem.iItem < infoPtr->nItemCount; lvItem.iItem++)
8432 lvItem.pszText = szDispText;
8433 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) continue;
8434 nLabelWidth = LISTVIEW_GetStringWidthT(infoPtr, lvItem.pszText, TRUE);
8435 if (max_cx < nLabelWidth) max_cx = nLabelWidth;
8437 if (infoPtr->himlSmall && (nColumn == 0 || (LISTVIEW_GetColumnInfo(infoPtr, nColumn)->fmt & LVCFMT_IMAGE)))
8438 max_cx += infoPtr->iconSize.cx;
8439 max_cx += TRAILING_LABEL_PADDING;
8440 if (nColumn == 0 && (infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES))
8441 max_cx += GetSystemMetrics(SM_CXSMICON);
8444 /* autosize based on listview items width */
8445 if(cx == LVSCW_AUTOSIZE)
8446 cx = max_cx;
8447 else if(cx == LVSCW_AUTOSIZE_USEHEADER)
8449 /* if iCol is the last column make it fill the remainder of the controls width */
8450 if(nColumn == DPA_GetPtrCount(infoPtr->hdpaColumns) - 1)
8452 RECT rcHeader;
8453 POINT Origin;
8455 LISTVIEW_GetOrigin(infoPtr, &Origin);
8456 LISTVIEW_GetHeaderRect(infoPtr, nColumn, &rcHeader);
8458 cx = infoPtr->rcList.right - Origin.x - rcHeader.left;
8460 else
8462 /* Despite what the MS docs say, if this is not the last
8463 column, then MS resizes the column to the width of the
8464 largest text string in the column, including headers
8465 and items. This is different from LVSCW_AUTOSIZE in that
8466 LVSCW_AUTOSIZE ignores the header string length. */
8467 cx = 0;
8469 /* retrieve header text */
8470 hdi.mask = HDI_TEXT|HDI_FORMAT|HDI_IMAGE|HDI_BITMAP;
8471 hdi.cchTextMax = DISP_TEXT_SIZE;
8472 hdi.pszText = szDispText;
8473 if (SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nColumn, (LPARAM)&hdi))
8475 HDC hdc = GetDC(infoPtr->hwndSelf);
8476 HFONT old_font = SelectObject(hdc, (HFONT)SendMessageW(infoPtr->hwndHeader, WM_GETFONT, 0, 0));
8477 HIMAGELIST himl = (HIMAGELIST)SendMessageW(infoPtr->hwndHeader, HDM_GETIMAGELIST, 0, 0);
8478 INT bitmap_margin = 0;
8479 SIZE size;
8481 if (GetTextExtentPoint32W(hdc, hdi.pszText, lstrlenW(hdi.pszText), &size))
8482 cx = size.cx + TRAILING_HEADER_PADDING;
8484 if (hdi.fmt & (HDF_IMAGE|HDF_BITMAP))
8485 bitmap_margin = SendMessageW(infoPtr->hwndHeader, HDM_GETBITMAPMARGIN, 0, 0);
8487 if ((hdi.fmt & HDF_IMAGE) && himl)
8489 INT icon_cx, icon_cy;
8491 if (!ImageList_GetIconSize(himl, &icon_cx, &icon_cy))
8492 cx += icon_cx + 2*bitmap_margin;
8494 else if (hdi.fmt & HDF_BITMAP)
8496 BITMAP bmp;
8498 GetObjectW(hdi.hbm, sizeof(BITMAP), &bmp);
8499 cx += bmp.bmWidth + 2*bitmap_margin;
8502 SelectObject(hdc, old_font);
8503 ReleaseDC(infoPtr->hwndSelf, hdc);
8505 cx = max (cx, max_cx);
8509 if (cx < 0) return FALSE;
8511 /* call header to update the column change */
8512 hdi.mask = HDI_WIDTH;
8513 hdi.cxy = max(cx, LISTVIEW_GetColumnInfo(infoPtr, nColumn)->cxMin);
8514 TRACE("hdi.cxy=%d\n", hdi.cxy);
8515 return SendMessageW(infoPtr->hwndHeader, HDM_SETITEMW, nColumn, (LPARAM)&hdi);
8518 /***
8519 * Creates the checkbox imagelist. Helper for LISTVIEW_SetExtendedListViewStyle
8522 static HIMAGELIST LISTVIEW_CreateThemedCheckBoxImageList(const LISTVIEW_INFO *info)
8524 HBITMAP bitmap, old_bitmap;
8525 HIMAGELIST image_list;
8526 HDC hdc, mem_hdc;
8527 HTHEME theme;
8528 RECT rect;
8529 SIZE size;
8531 if (!GetWindowTheme(info->hwndSelf))
8532 return NULL;
8534 theme = OpenThemeDataForDpi(NULL, L"Button", GetDpiForWindow(info->hwndSelf));
8535 if (!theme)
8536 return NULL;
8538 hdc = GetDC(info->hwndSelf);
8539 GetThemePartSize(theme, hdc, BP_CHECKBOX, 0, NULL, TS_DRAW, &size);
8540 SetRect(&rect, 0, 0, size.cx, size.cy);
8541 image_list = ImageList_Create(size.cx, size.cy, ILC_COLOR32, 2, 2);
8542 mem_hdc = CreateCompatibleDC(hdc);
8543 bitmap = CreateCompatibleBitmap(hdc, size.cx, size.cy);
8544 old_bitmap = SelectObject(mem_hdc, bitmap);
8545 ReleaseDC(info->hwndSelf, hdc);
8547 if (IsThemeBackgroundPartiallyTransparent(theme, BP_CHECKBOX, CBS_UNCHECKEDNORMAL))
8548 FillRect(mem_hdc, &rect, (HBRUSH)(COLOR_WINDOW + 1));
8549 DrawThemeBackground(theme, mem_hdc, BP_CHECKBOX, CBS_UNCHECKEDNORMAL, &rect, NULL);
8550 ImageList_Add(image_list, bitmap, NULL);
8552 if (IsThemeBackgroundPartiallyTransparent(theme, BP_CHECKBOX, CBS_CHECKEDNORMAL))
8553 FillRect(mem_hdc, &rect, (HBRUSH)(COLOR_WINDOW + 1));
8554 DrawThemeBackground(theme, mem_hdc, BP_CHECKBOX, CBS_CHECKEDNORMAL, &rect, NULL);
8555 ImageList_Add(image_list, bitmap, NULL);
8557 SelectObject(mem_hdc, old_bitmap);
8558 DeleteObject(bitmap);
8559 DeleteDC(mem_hdc);
8560 CloseThemeData(theme);
8561 return image_list;
8564 static HIMAGELIST LISTVIEW_CreateCheckBoxIL(const LISTVIEW_INFO *infoPtr)
8566 HDC hdc_wnd, hdc;
8567 HBITMAP hbm_im, hbm_mask, hbm_orig;
8568 RECT rc;
8569 HBRUSH hbr_white, hbr_black;
8570 HIMAGELIST himl;
8572 himl = LISTVIEW_CreateThemedCheckBoxImageList(infoPtr);
8573 if (himl)
8574 return himl;
8576 hbr_white = GetStockObject(WHITE_BRUSH);
8577 hbr_black = GetStockObject(BLACK_BRUSH);
8578 himl = ImageList_Create(GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON),
8579 ILC_COLOR | ILC_MASK, 2, 2);
8580 hdc_wnd = GetDC(infoPtr->hwndSelf);
8581 hdc = CreateCompatibleDC(hdc_wnd);
8582 hbm_im = CreateCompatibleBitmap(hdc_wnd, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON));
8583 hbm_mask = CreateBitmap(GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), 1, 1, NULL);
8584 ReleaseDC(infoPtr->hwndSelf, hdc_wnd);
8586 SetRect(&rc, 0, 0, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON));
8587 hbm_orig = SelectObject(hdc, hbm_mask);
8588 FillRect(hdc, &rc, hbr_white);
8589 InflateRect(&rc, -2, -2);
8590 FillRect(hdc, &rc, hbr_black);
8592 SelectObject(hdc, hbm_im);
8593 DrawFrameControl(hdc, &rc, DFC_BUTTON, DFCS_BUTTONCHECK | DFCS_MONO);
8594 SelectObject(hdc, hbm_orig);
8595 ImageList_Add(himl, hbm_im, hbm_mask);
8597 SelectObject(hdc, hbm_im);
8598 DrawFrameControl(hdc, &rc, DFC_BUTTON, DFCS_BUTTONCHECK | DFCS_MONO | DFCS_CHECKED);
8599 SelectObject(hdc, hbm_orig);
8600 ImageList_Add(himl, hbm_im, hbm_mask);
8602 DeleteObject(hbm_mask);
8603 DeleteObject(hbm_im);
8604 DeleteDC(hdc);
8606 return himl;
8609 /***
8610 * DESCRIPTION:
8611 * Sets the extended listview style.
8613 * PARAMETERS:
8614 * [I] infoPtr : valid pointer to the listview structure
8615 * [I] dwMask : mask
8616 * [I] dwStyle : style
8618 * RETURN:
8619 * SUCCESS : previous style
8620 * FAILURE : 0
8622 static DWORD LISTVIEW_SetExtendedListViewStyle(LISTVIEW_INFO *infoPtr, DWORD mask, DWORD ex_style)
8624 DWORD old_ex_style = infoPtr->dwLvExStyle;
8626 TRACE("mask %#lx, ex_style %#lx\n", mask, ex_style);
8628 /* set new style */
8629 if (mask)
8630 infoPtr->dwLvExStyle = (old_ex_style & ~mask) | (ex_style & mask);
8631 else
8632 infoPtr->dwLvExStyle = ex_style;
8634 if((infoPtr->dwLvExStyle ^ old_ex_style) & LVS_EX_CHECKBOXES)
8636 HIMAGELIST himl = 0;
8637 if(infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES)
8639 LVITEMW item;
8640 item.mask = LVIF_STATE;
8641 item.stateMask = LVIS_STATEIMAGEMASK;
8642 item.state = INDEXTOSTATEIMAGEMASK(1);
8643 LISTVIEW_SetItemState(infoPtr, -1, &item);
8645 himl = LISTVIEW_CreateCheckBoxIL(infoPtr);
8646 if(!(infoPtr->dwStyle & LVS_SHAREIMAGELISTS))
8647 ImageList_Destroy(infoPtr->himlState);
8649 himl = LISTVIEW_SetImageList(infoPtr, LVSIL_STATE, himl);
8650 /* checkbox list replaces previous custom list or... */
8651 if(((infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES) &&
8652 !(infoPtr->dwStyle & LVS_SHAREIMAGELISTS)) ||
8653 /* ...previous was checkbox list */
8654 (old_ex_style & LVS_EX_CHECKBOXES))
8655 ImageList_Destroy(himl);
8658 if((infoPtr->dwLvExStyle ^ old_ex_style) & LVS_EX_HEADERDRAGDROP)
8660 DWORD style;
8662 /* if not already created */
8663 LISTVIEW_CreateHeader(infoPtr);
8665 style = GetWindowLongW(infoPtr->hwndHeader, GWL_STYLE);
8666 if (infoPtr->dwLvExStyle & LVS_EX_HEADERDRAGDROP)
8667 style |= HDS_DRAGDROP;
8668 else
8669 style &= ~HDS_DRAGDROP;
8670 SetWindowLongW(infoPtr->hwndHeader, GWL_STYLE, style);
8673 /* GRIDLINES adds decoration at top so changes sizes */
8674 if((infoPtr->dwLvExStyle ^ old_ex_style) & LVS_EX_GRIDLINES)
8676 LISTVIEW_CreateHeader(infoPtr);
8677 LISTVIEW_UpdateSize(infoPtr);
8680 if((infoPtr->dwLvExStyle ^ old_ex_style) & LVS_EX_FULLROWSELECT)
8682 LISTVIEW_CreateHeader(infoPtr);
8685 if((infoPtr->dwLvExStyle ^ old_ex_style) & LVS_EX_TRANSPARENTBKGND)
8687 if (infoPtr->dwLvExStyle & LVS_EX_TRANSPARENTBKGND)
8688 LISTVIEW_SetBkColor(infoPtr, CLR_NONE);
8691 if((infoPtr->dwLvExStyle ^ old_ex_style) & LVS_EX_HEADERINALLVIEWS)
8693 if (infoPtr->dwLvExStyle & LVS_EX_HEADERINALLVIEWS)
8694 LISTVIEW_CreateHeader(infoPtr);
8695 else
8696 ShowWindow(infoPtr->hwndHeader, SW_HIDE);
8697 LISTVIEW_UpdateSize(infoPtr);
8698 LISTVIEW_UpdateScroll(infoPtr);
8701 LISTVIEW_InvalidateList(infoPtr);
8702 return old_ex_style;
8705 /***
8706 * DESCRIPTION:
8707 * Sets the new hot cursor used during hot tracking and hover selection.
8709 * PARAMETER(S):
8710 * [I] infoPtr : valid pointer to the listview structure
8711 * [I] hCursor : the new hot cursor handle
8713 * RETURN:
8714 * Returns the previous hot cursor
8716 static HCURSOR LISTVIEW_SetHotCursor(LISTVIEW_INFO *infoPtr, HCURSOR hCursor)
8718 HCURSOR oldCursor = infoPtr->hHotCursor;
8720 infoPtr->hHotCursor = hCursor;
8722 return oldCursor;
8726 /***
8727 * DESCRIPTION:
8728 * Sets the hot item index.
8730 * PARAMETERS:
8731 * [I] infoPtr : valid pointer to the listview structure
8732 * [I] iIndex : index
8734 * RETURN:
8735 * SUCCESS : previous hot item index
8736 * FAILURE : -1 (no hot item)
8738 static INT LISTVIEW_SetHotItem(LISTVIEW_INFO *infoPtr, INT iIndex)
8740 INT iOldIndex = infoPtr->nHotItem;
8742 infoPtr->nHotItem = iIndex;
8744 return iOldIndex;
8748 /***
8749 * DESCRIPTION:
8750 * Sets the amount of time the cursor must hover over an item before it is selected.
8752 * PARAMETER(S):
8753 * [I] infoPtr : valid pointer to the listview structure
8754 * [I] dwHoverTime : hover time, if -1 the hover time is set to the default
8756 * RETURN:
8757 * Returns the previous hover time
8759 static DWORD LISTVIEW_SetHoverTime(LISTVIEW_INFO *infoPtr, DWORD dwHoverTime)
8761 DWORD oldHoverTime = infoPtr->dwHoverTime;
8763 infoPtr->dwHoverTime = dwHoverTime;
8765 return oldHoverTime;
8768 /***
8769 * DESCRIPTION:
8770 * Sets spacing for icons of LVS_ICON style.
8772 * PARAMETER(S):
8773 * [I] infoPtr : valid pointer to the listview structure
8774 * [I] cx : horizontal spacing (-1 = system spacing, 0 = autosize)
8775 * [I] cy : vertical spacing (-1 = system spacing, 0 = autosize)
8777 * RETURN:
8778 * MAKELONG(oldcx, oldcy)
8780 static DWORD LISTVIEW_SetIconSpacing(LISTVIEW_INFO *infoPtr, INT cx, INT cy)
8782 INT iconWidth = 0, iconHeight = 0;
8783 DWORD oldspacing = MAKELONG(infoPtr->iconSpacing.cx, infoPtr->iconSpacing.cy);
8785 TRACE("requested=(%d,%d)\n", cx, cy);
8787 /* set to defaults, if instructed to */
8788 if (cx == -1 && cy == -1)
8790 infoPtr->autoSpacing = TRUE;
8791 if (infoPtr->himlNormal)
8792 ImageList_GetIconSize(infoPtr->himlNormal, &iconWidth, &iconHeight);
8793 cx = GetSystemMetrics(SM_CXICONSPACING) - GetSystemMetrics(SM_CXICON) + iconWidth;
8794 cy = GetSystemMetrics(SM_CYICONSPACING) - GetSystemMetrics(SM_CYICON) + iconHeight;
8796 else
8797 infoPtr->autoSpacing = FALSE;
8799 /* if 0 then keep width */
8800 if (cx != 0)
8801 infoPtr->iconSpacing.cx = cx;
8803 /* if 0 then keep height */
8804 if (cy != 0)
8805 infoPtr->iconSpacing.cy = cy;
8807 TRACE("old=(%d,%d), new=(%ld,%ld), iconSize=(%ld,%ld), ntmH=%d\n",
8808 LOWORD(oldspacing), HIWORD(oldspacing), infoPtr->iconSpacing.cx, infoPtr->iconSpacing.cy,
8809 infoPtr->iconSize.cx, infoPtr->iconSize.cy,
8810 infoPtr->ntmHeight);
8812 /* these depend on the iconSpacing */
8813 LISTVIEW_UpdateItemSize(infoPtr);
8815 return oldspacing;
8818 static inline void set_icon_size(SIZE *size, HIMAGELIST himl, BOOL is_small)
8820 INT cx, cy;
8822 if (himl && ImageList_GetIconSize(himl, &cx, &cy))
8824 size->cx = cx;
8825 size->cy = cy;
8827 else
8829 size->cx = GetSystemMetrics(is_small ? SM_CXSMICON : SM_CXICON);
8830 size->cy = GetSystemMetrics(is_small ? SM_CYSMICON : SM_CYICON);
8834 /***
8835 * DESCRIPTION:
8836 * Sets image lists.
8838 * PARAMETER(S):
8839 * [I] infoPtr : valid pointer to the listview structure
8840 * [I] nType : image list type
8841 * [I] himl : image list handle
8843 * RETURN:
8844 * SUCCESS : old image list
8845 * FAILURE : NULL
8847 static HIMAGELIST LISTVIEW_SetImageList(LISTVIEW_INFO *infoPtr, INT nType, HIMAGELIST himl)
8849 INT oldHeight = infoPtr->nItemHeight;
8850 HIMAGELIST himlOld = 0;
8852 TRACE("(nType=%d, himl=%p)\n", nType, himl);
8854 switch (nType)
8856 case LVSIL_NORMAL:
8857 himlOld = infoPtr->himlNormal;
8858 infoPtr->himlNormal = himl;
8859 if (infoPtr->uView == LV_VIEW_ICON) set_icon_size(&infoPtr->iconSize, himl, FALSE);
8860 if (infoPtr->autoSpacing)
8861 LISTVIEW_SetIconSpacing(infoPtr, -1, -1);
8862 break;
8864 case LVSIL_SMALL:
8865 himlOld = infoPtr->himlSmall;
8866 infoPtr->himlSmall = himl;
8867 if (infoPtr->uView != LV_VIEW_ICON) set_icon_size(&infoPtr->iconSize, himl, TRUE);
8868 if (infoPtr->hwndHeader)
8869 SendMessageW(infoPtr->hwndHeader, HDM_SETIMAGELIST, 0, (LPARAM)himl);
8870 break;
8872 case LVSIL_STATE:
8873 himlOld = infoPtr->himlState;
8874 infoPtr->himlState = himl;
8875 set_icon_size(&infoPtr->iconStateSize, himl, TRUE);
8876 ImageList_SetBkColor(infoPtr->himlState, CLR_NONE);
8877 break;
8879 default:
8880 ERR("Unknown icon type=%d\n", nType);
8881 return NULL;
8884 infoPtr->nItemHeight = LISTVIEW_CalculateItemHeight(infoPtr);
8885 if (infoPtr->nItemHeight != oldHeight)
8886 LISTVIEW_UpdateScroll(infoPtr);
8888 return himlOld;
8891 /***
8892 * DESCRIPTION:
8893 * Preallocates memory (does *not* set the actual count of items !)
8895 * PARAMETER(S):
8896 * [I] infoPtr : valid pointer to the listview structure
8897 * [I] nItems : item count (projected number of items to allocate)
8898 * [I] dwFlags : update flags
8900 * RETURN:
8901 * SUCCESS : TRUE
8902 * FAILURE : FALSE
8904 static BOOL LISTVIEW_SetItemCount(LISTVIEW_INFO *infoPtr, INT nItems, DWORD dwFlags)
8906 TRACE("nItems %d, flags %#lx\n", nItems, dwFlags);
8908 if (infoPtr->dwStyle & LVS_OWNERDATA)
8910 INT nOldCount = infoPtr->nItemCount;
8911 infoPtr->nItemCount = nItems;
8913 if (nItems < nOldCount)
8915 RANGE range = { nItems, nOldCount };
8916 ranges_del(infoPtr->selectionRanges, range);
8917 if (infoPtr->nFocusedItem >= nItems)
8919 LISTVIEW_SetItemFocus(infoPtr, -1);
8920 infoPtr->nFocusedItem = -1;
8921 SetRectEmpty(&infoPtr->rcFocus);
8925 LISTVIEW_UpdateScroll(infoPtr);
8927 /* the flags are valid only in ownerdata report and list modes */
8928 if (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON) dwFlags = 0;
8930 if (!(dwFlags & LVSICF_NOSCROLL) && infoPtr->nFocusedItem != -1)
8931 LISTVIEW_EnsureVisible(infoPtr, infoPtr->nFocusedItem, FALSE);
8933 if (!(dwFlags & LVSICF_NOINVALIDATEALL))
8934 LISTVIEW_InvalidateList(infoPtr);
8935 else
8937 INT nFrom, nTo;
8938 POINT Origin;
8939 RECT rcErase;
8941 LISTVIEW_GetOrigin(infoPtr, &Origin);
8942 nFrom = min(nOldCount, nItems);
8943 nTo = max(nOldCount, nItems);
8945 if (infoPtr->uView == LV_VIEW_DETAILS)
8947 SetRect(&rcErase, 0, nFrom * infoPtr->nItemHeight, infoPtr->nItemWidth,
8948 nTo * infoPtr->nItemHeight);
8949 OffsetRect(&rcErase, Origin.x, Origin.y);
8950 if (IntersectRect(&rcErase, &rcErase, &infoPtr->rcList))
8951 LISTVIEW_InvalidateRect(infoPtr, &rcErase);
8953 else /* LV_VIEW_LIST */
8955 INT nPerCol = LISTVIEW_GetCountPerColumn(infoPtr);
8957 rcErase.left = (nFrom / nPerCol) * infoPtr->nItemWidth;
8958 rcErase.top = (nFrom % nPerCol) * infoPtr->nItemHeight;
8959 rcErase.right = rcErase.left + infoPtr->nItemWidth;
8960 rcErase.bottom = nPerCol * infoPtr->nItemHeight;
8961 OffsetRect(&rcErase, Origin.x, Origin.y);
8962 if (IntersectRect(&rcErase, &rcErase, &infoPtr->rcList))
8963 LISTVIEW_InvalidateRect(infoPtr, &rcErase);
8965 rcErase.left = (nFrom / nPerCol + 1) * infoPtr->nItemWidth;
8966 rcErase.top = 0;
8967 rcErase.right = (nTo / nPerCol + 1) * infoPtr->nItemWidth;
8968 rcErase.bottom = nPerCol * infoPtr->nItemHeight;
8969 OffsetRect(&rcErase, Origin.x, Origin.y);
8970 if (IntersectRect(&rcErase, &rcErase, &infoPtr->rcList))
8971 LISTVIEW_InvalidateRect(infoPtr, &rcErase);
8975 else
8977 /* According to MSDN for non-LVS_OWNERDATA this is just
8978 * a performance issue. The control allocates its internal
8979 * data structures for the number of items specified. It
8980 * cuts down on the number of memory allocations. Therefore
8981 * we will just issue a WARN here
8983 WARN("for non-ownerdata performance option not implemented.\n");
8986 return TRUE;
8989 /***
8990 * DESCRIPTION:
8991 * Sets the position of an item.
8993 * PARAMETER(S):
8994 * [I] infoPtr : valid pointer to the listview structure
8995 * [I] nItem : item index
8996 * [I] pt : coordinate
8998 * RETURN:
8999 * SUCCESS : TRUE
9000 * FAILURE : FALSE
9002 static BOOL LISTVIEW_SetItemPosition(LISTVIEW_INFO *infoPtr, INT nItem, const POINT *pt)
9004 POINT Origin, Pt;
9006 TRACE("(nItem=%d, pt=%s)\n", nItem, wine_dbgstr_point(pt));
9008 if (!pt || nItem < 0 || nItem >= infoPtr->nItemCount ||
9009 !(infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON)) return FALSE;
9011 Pt = *pt;
9012 LISTVIEW_GetOrigin(infoPtr, &Origin);
9014 /* This point value seems to be an undocumented feature.
9015 * The best guess is that it means either at the origin,
9016 * or at true beginning of the list. I will assume the origin. */
9017 if ((Pt.x == -1) && (Pt.y == -1))
9018 Pt = Origin;
9020 if (infoPtr->uView == LV_VIEW_ICON)
9022 Pt.x -= (infoPtr->nItemWidth - infoPtr->iconSize.cx) / 2;
9023 Pt.y -= ICON_TOP_PADDING;
9025 Pt.x -= Origin.x;
9026 Pt.y -= Origin.y;
9028 return LISTVIEW_MoveIconTo(infoPtr, nItem, &Pt, FALSE);
9031 /* Make sure to also disable per item notifications via the notification mask. */
9032 static VOID LISTVIEW_SetOwnerDataState(LISTVIEW_INFO *infoPtr, INT nFirst, INT nLast, const LVITEMW *item)
9034 NMLVODSTATECHANGE nmlv;
9036 if (nFirst == nLast) return;
9037 if (!item) return;
9039 ZeroMemory(&nmlv, sizeof(nmlv));
9040 nmlv.iFrom = nFirst;
9041 nmlv.iTo = nLast;
9042 nmlv.uOldState = 0;
9043 nmlv.uNewState = item->state;
9045 notify_hdr(infoPtr, LVN_ODSTATECHANGED, (LPNMHDR)&nmlv);
9048 /***
9049 * DESCRIPTION:
9050 * Sets the state of one or many items.
9052 * PARAMETER(S):
9053 * [I] infoPtr : valid pointer to the listview structure
9054 * [I] nItem : item index
9055 * [I] item : item or subitem info
9057 * RETURN:
9058 * SUCCESS : TRUE
9059 * FAILURE : FALSE
9061 static BOOL LISTVIEW_SetItemState(LISTVIEW_INFO *infoPtr, INT nItem, const LVITEMW *item)
9063 BOOL ret = TRUE;
9064 LVITEMW lvItem;
9066 if (!item) return FALSE;
9068 lvItem.iItem = nItem;
9069 lvItem.iSubItem = 0;
9070 lvItem.mask = LVIF_STATE;
9071 lvItem.state = item->state;
9072 lvItem.stateMask = item->stateMask;
9073 TRACE("item=%s\n", debuglvitem_t(&lvItem, TRUE));
9075 if (nItem == -1)
9077 UINT oldstate = 0;
9078 DWORD old_mask;
9080 /* special case optimization for recurring attempt to deselect all */
9081 if (lvItem.state == 0 && lvItem.stateMask == LVIS_SELECTED && !LISTVIEW_GetSelectedCount(infoPtr))
9082 return TRUE;
9084 /* select all isn't allowed in LVS_SINGLESEL */
9085 if ((lvItem.state & lvItem.stateMask & LVIS_SELECTED) && (infoPtr->dwStyle & LVS_SINGLESEL))
9086 return FALSE;
9088 /* focus all isn't allowed */
9089 if (lvItem.state & lvItem.stateMask & LVIS_FOCUSED) return FALSE;
9091 old_mask = infoPtr->notify_mask & NOTIFY_MASK_ITEM_CHANGE;
9092 if (infoPtr->dwStyle & LVS_OWNERDATA)
9094 infoPtr->notify_mask &= ~NOTIFY_MASK_ITEM_CHANGE;
9095 if (!(lvItem.state & LVIS_SELECTED) && LISTVIEW_GetSelectedCount(infoPtr))
9096 oldstate |= LVIS_SELECTED;
9097 if (infoPtr->nFocusedItem != -1) oldstate |= LVIS_FOCUSED;
9100 /* apply to all items */
9101 for (lvItem.iItem = 0; lvItem.iItem < infoPtr->nItemCount; lvItem.iItem++)
9102 if (!LISTVIEW_SetItemT(infoPtr, &lvItem, TRUE)) ret = FALSE;
9104 if (infoPtr->dwStyle & LVS_OWNERDATA)
9106 NMLISTVIEW nmlv;
9108 infoPtr->notify_mask |= old_mask;
9110 nmlv.iItem = -1;
9111 nmlv.iSubItem = 0;
9112 nmlv.uNewState = lvItem.state & lvItem.stateMask;
9113 nmlv.uOldState = oldstate & lvItem.stateMask;
9114 nmlv.uChanged = LVIF_STATE;
9115 nmlv.ptAction.x = nmlv.ptAction.y = 0;
9116 nmlv.lParam = 0;
9118 notify_listview(infoPtr, LVN_ITEMCHANGED, &nmlv);
9121 else
9122 ret = LISTVIEW_SetItemT(infoPtr, &lvItem, TRUE);
9124 return ret;
9127 /***
9128 * DESCRIPTION:
9129 * Sets the text of an item or subitem.
9131 * PARAMETER(S):
9132 * [I] hwnd : window handle
9133 * [I] nItem : item index
9134 * [I] lpLVItem : item or subitem info
9135 * [I] isW : TRUE if input is Unicode
9137 * RETURN:
9138 * SUCCESS : TRUE
9139 * FAILURE : FALSE
9141 static BOOL LISTVIEW_SetItemTextT(LISTVIEW_INFO *infoPtr, INT nItem, const LVITEMW *lpLVItem, BOOL isW)
9143 LVITEMW lvItem;
9145 if (!lpLVItem || nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
9146 if (infoPtr->dwStyle & LVS_OWNERDATA) return FALSE;
9148 lvItem.iItem = nItem;
9149 lvItem.iSubItem = lpLVItem->iSubItem;
9150 lvItem.mask = LVIF_TEXT;
9151 lvItem.pszText = lpLVItem->pszText;
9152 lvItem.cchTextMax = lpLVItem->cchTextMax;
9154 TRACE("(nItem=%d, lpLVItem=%s, isW=%d)\n", nItem, debuglvitem_t(&lvItem, isW), isW);
9156 return LISTVIEW_SetItemT(infoPtr, &lvItem, isW);
9159 /***
9160 * DESCRIPTION:
9161 * Set item index that marks the start of a multiple selection.
9163 * PARAMETER(S):
9164 * [I] infoPtr : valid pointer to the listview structure
9165 * [I] nIndex : index
9167 * RETURN:
9168 * Index number or -1 if there is no selection mark.
9170 static INT LISTVIEW_SetSelectionMark(LISTVIEW_INFO *infoPtr, INT nIndex)
9172 INT nOldIndex = infoPtr->nSelectionMark;
9174 TRACE("(nIndex=%d)\n", nIndex);
9176 if (nIndex >= -1 && nIndex < infoPtr->nItemCount)
9177 infoPtr->nSelectionMark = nIndex;
9179 return nOldIndex;
9182 /***
9183 * DESCRIPTION:
9184 * Sets the text background color.
9186 * PARAMETER(S):
9187 * [I] infoPtr : valid pointer to the listview structure
9188 * [I] color : text background color
9190 * RETURN:
9191 * SUCCESS : TRUE
9192 * FAILURE : FALSE
9194 static BOOL LISTVIEW_SetTextBkColor(LISTVIEW_INFO *infoPtr, COLORREF color)
9196 TRACE("color %#lx\n", color);
9198 infoPtr->clrTextBk = color;
9199 return TRUE;
9202 /***
9203 * DESCRIPTION:
9204 * Sets the text foreground color.
9206 * PARAMETER(S):
9207 * [I] infoPtr : valid pointer to the listview structure
9208 * [I] color : text color
9210 * RETURN:
9211 * SUCCESS : TRUE
9212 * FAILURE : FALSE
9214 static BOOL LISTVIEW_SetTextColor (LISTVIEW_INFO *infoPtr, COLORREF color)
9216 TRACE("color %#lx\n", color);
9218 infoPtr->clrText = color;
9219 return TRUE;
9222 /***
9223 * DESCRIPTION:
9224 * Sets new ToolTip window to ListView control.
9226 * PARAMETER(S):
9227 * [I] infoPtr : valid pointer to the listview structure
9228 * [I] hwndNewToolTip : handle to new ToolTip
9230 * RETURN:
9231 * old tool tip
9233 static HWND LISTVIEW_SetToolTips( LISTVIEW_INFO *infoPtr, HWND hwndNewToolTip)
9235 HWND hwndOldToolTip = infoPtr->hwndToolTip;
9236 infoPtr->hwndToolTip = hwndNewToolTip;
9237 return hwndOldToolTip;
9241 * DESCRIPTION:
9242 * sets the Unicode character format flag for the control
9243 * PARAMETER(S):
9244 * [I] infoPtr :valid pointer to the listview structure
9245 * [I] fUnicode :true to switch to UNICODE false to switch to ANSI
9247 * RETURN:
9248 * Old Unicode Format
9250 static BOOL LISTVIEW_SetUnicodeFormat( LISTVIEW_INFO *infoPtr, BOOL unicode)
9252 SHORT rc = infoPtr->notifyFormat;
9253 infoPtr->notifyFormat = (unicode) ? NFR_UNICODE : NFR_ANSI;
9254 return rc == NFR_UNICODE;
9258 * DESCRIPTION:
9259 * sets the control view mode
9260 * PARAMETER(S):
9261 * [I] infoPtr :valid pointer to the listview structure
9262 * [I] nView :new view mode value
9264 * RETURN:
9265 * SUCCESS: 1
9266 * FAILURE: -1
9268 static INT LISTVIEW_SetView(LISTVIEW_INFO *infoPtr, DWORD nView)
9270 HIMAGELIST himl;
9272 if (infoPtr->uView == nView) return 1;
9274 if ((INT)nView < 0 || nView > LV_VIEW_MAX) return -1;
9275 if (nView == LV_VIEW_TILE)
9277 FIXME("View LV_VIEW_TILE unimplemented\n");
9278 return -1;
9281 infoPtr->uView = nView;
9283 SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
9284 ShowWindow(infoPtr->hwndHeader, SW_HIDE);
9286 ShowScrollBar(infoPtr->hwndSelf, SB_BOTH, FALSE);
9287 SetRectEmpty(&infoPtr->rcFocus);
9289 himl = (nView == LV_VIEW_ICON ? infoPtr->himlNormal : infoPtr->himlSmall);
9290 set_icon_size(&infoPtr->iconSize, himl, nView != LV_VIEW_ICON);
9292 switch (nView)
9294 case LV_VIEW_ICON:
9295 case LV_VIEW_SMALLICON:
9296 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
9297 break;
9298 case LV_VIEW_DETAILS:
9300 HDLAYOUT hl;
9301 WINDOWPOS wp;
9303 LISTVIEW_CreateHeader( infoPtr );
9305 hl.prc = &infoPtr->rcList;
9306 hl.pwpos = &wp;
9307 SendMessageW(infoPtr->hwndHeader, HDM_LAYOUT, 0, (LPARAM)&hl);
9308 SetWindowPos(infoPtr->hwndHeader, infoPtr->hwndSelf, wp.x, wp.y, wp.cx, wp.cy,
9309 wp.flags | ((infoPtr->dwStyle & LVS_NOCOLUMNHEADER) ? SWP_HIDEWINDOW : SWP_SHOWWINDOW));
9310 break;
9312 case LV_VIEW_LIST:
9313 break;
9316 LISTVIEW_UpdateItemSize(infoPtr);
9317 LISTVIEW_UpdateSize(infoPtr);
9318 LISTVIEW_UpdateScroll(infoPtr);
9319 LISTVIEW_InvalidateList(infoPtr);
9321 TRACE("nView %ld\n", nView);
9323 return 1;
9326 /* LISTVIEW_SetWorkAreas */
9328 struct sorting_context
9330 HDPA items;
9331 PFNLVCOMPARE compare_func;
9332 LPARAM lParam;
9335 /* DPA_Sort() callback used for LVM_SORTITEMS */
9336 static INT WINAPI LISTVIEW_CallBackCompare(LPVOID first, LPVOID second, LPARAM lParam)
9338 struct sorting_context *context = (struct sorting_context *)lParam;
9339 ITEM_INFO* lv_first = DPA_GetPtr( first, 0 );
9340 ITEM_INFO* lv_second = DPA_GetPtr( second, 0 );
9342 return context->compare_func(lv_first->lParam, lv_second->lParam, context->lParam);
9345 /* DPA_Sort() callback used for LVM_SORTITEMSEX */
9346 static INT WINAPI LISTVIEW_CallBackCompareEx(LPVOID first, LPVOID second, LPARAM lParam)
9348 struct sorting_context *context = (struct sorting_context *)lParam;
9349 INT first_idx = DPA_GetPtrIndex( context->items, first );
9350 INT second_idx = DPA_GetPtrIndex( context->items, second );
9352 return context->compare_func(first_idx, second_idx, context->lParam);
9355 /***
9356 * DESCRIPTION:
9357 * Sorts the listview items.
9359 * PARAMETER(S):
9360 * [I] infoPtr : valid pointer to the listview structure
9361 * [I] pfnCompare : application-defined value
9362 * [I] lParamSort : pointer to comparison callback
9363 * [I] IsEx : TRUE when LVM_SORTITEMSEX used
9365 * RETURN:
9366 * SUCCESS : TRUE
9367 * FAILURE : FALSE
9369 static BOOL LISTVIEW_SortItems(LISTVIEW_INFO *infoPtr, PFNLVCOMPARE pfnCompare,
9370 LPARAM lParamSort, BOOL IsEx)
9372 HDPA hdpaSubItems, hdpaItems;
9373 ITEM_INFO *lpItem;
9374 LPVOID selectionMarkItem = NULL;
9375 LPVOID focusedItem = NULL;
9376 struct sorting_context context;
9377 int i;
9379 TRACE("pfnCompare %p, lParamSort %Ix\n", pfnCompare, lParamSort);
9381 if (infoPtr->dwStyle & LVS_OWNERDATA) return FALSE;
9383 if (!pfnCompare) return FALSE;
9384 if (!infoPtr->hdpaItems) return FALSE;
9386 /* if there are 0 or 1 items, there is no need to sort */
9387 if (infoPtr->nItemCount < 2) return TRUE;
9388 if (!(hdpaItems = DPA_Clone(infoPtr->hdpaItems, NULL))) return FALSE;
9390 /* clear selection */
9391 ranges_clear(infoPtr->selectionRanges);
9393 /* save selection mark and focused item */
9394 if (infoPtr->nSelectionMark >= 0)
9395 selectionMarkItem = DPA_GetPtr(infoPtr->hdpaItems, infoPtr->nSelectionMark);
9396 if (infoPtr->nFocusedItem >= 0)
9397 focusedItem = DPA_GetPtr(infoPtr->hdpaItems, infoPtr->nFocusedItem);
9399 context.items = hdpaItems;
9400 context.compare_func = pfnCompare;
9401 context.lParam = lParamSort;
9402 if (IsEx)
9403 DPA_Sort(hdpaItems, LISTVIEW_CallBackCompareEx, (LPARAM)&context);
9404 else
9405 DPA_Sort(hdpaItems, LISTVIEW_CallBackCompare, (LPARAM)&context);
9406 DPA_Destroy(infoPtr->hdpaItems);
9407 infoPtr->hdpaItems = hdpaItems;
9409 /* restore selection ranges */
9410 for (i=0; i < infoPtr->nItemCount; i++)
9412 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, i);
9413 lpItem = DPA_GetPtr(hdpaSubItems, 0);
9415 if (lpItem->state & LVIS_SELECTED)
9416 ranges_additem(infoPtr->selectionRanges, i);
9418 /* restore selection mark and focused item */
9419 infoPtr->nSelectionMark = DPA_GetPtrIndex(infoPtr->hdpaItems, selectionMarkItem);
9420 infoPtr->nFocusedItem = DPA_GetPtrIndex(infoPtr->hdpaItems, focusedItem);
9422 /* I believe nHotItem should be left alone, see LISTVIEW_ShiftIndices */
9424 /* refresh the display */
9425 LISTVIEW_InvalidateList(infoPtr);
9426 return TRUE;
9429 /***
9430 * DESCRIPTION:
9431 * Update theme handle after a theme change.
9433 * PARAMETER(S):
9434 * [I] infoPtr : valid pointer to the listview structure
9436 * RETURN:
9437 * SUCCESS : 0
9438 * FAILURE : something else
9440 static LRESULT LISTVIEW_ThemeChanged(const LISTVIEW_INFO *infoPtr)
9442 HTHEME theme = GetWindowTheme(infoPtr->hwndSelf);
9443 CloseThemeData(theme);
9444 OpenThemeData(infoPtr->hwndSelf, themeClass);
9445 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
9446 return 0;
9449 /***
9450 * DESCRIPTION:
9451 * Updates an items or rearranges the listview control.
9453 * PARAMETER(S):
9454 * [I] infoPtr : valid pointer to the listview structure
9455 * [I] nItem : item index
9457 * RETURN:
9458 * SUCCESS : TRUE
9459 * FAILURE : FALSE
9461 static BOOL LISTVIEW_Update(LISTVIEW_INFO *infoPtr, INT nItem)
9463 TRACE("(nItem=%d)\n", nItem);
9465 if (nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
9467 /* rearrange with default alignment style */
9468 if (is_autoarrange(infoPtr))
9469 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
9470 else
9471 LISTVIEW_InvalidateItem(infoPtr, nItem);
9473 return TRUE;
9476 /***
9477 * DESCRIPTION:
9478 * Draw the track line at the place defined in the infoPtr structure.
9479 * The line is drawn with a XOR pen so drawing the line for the second time
9480 * in the same place erases the line.
9482 * PARAMETER(S):
9483 * [I] infoPtr : valid pointer to the listview structure
9485 * RETURN:
9486 * SUCCESS : TRUE
9487 * FAILURE : FALSE
9489 static BOOL LISTVIEW_DrawTrackLine(const LISTVIEW_INFO *infoPtr)
9491 HDC hdc;
9493 if (infoPtr->xTrackLine == -1)
9494 return FALSE;
9496 if (!(hdc = GetDC(infoPtr->hwndSelf)))
9497 return FALSE;
9498 PatBlt( hdc, infoPtr->xTrackLine, infoPtr->rcList.top,
9499 1, infoPtr->rcList.bottom - infoPtr->rcList.top, DSTINVERT );
9500 ReleaseDC(infoPtr->hwndSelf, hdc);
9501 return TRUE;
9504 /***
9505 * DESCRIPTION:
9506 * Called when an edit control should be displayed. This function is called after
9507 * we are sure that there was a single click - not a double click (this is a TIMERPROC).
9509 * PARAMETER(S):
9510 * [I] hwnd : Handle to the listview
9511 * [I] uMsg : WM_TIMER (ignored)
9512 * [I] idEvent : The timer ID interpreted as a pointer to a DELAYED_EDIT_ITEM struct
9513 * [I] dwTimer : The elapsed time (ignored)
9515 * RETURN:
9516 * None.
9518 static VOID CALLBACK LISTVIEW_DelayedEditItem(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
9520 DELAYED_ITEM_EDIT *editItem = (DELAYED_ITEM_EDIT *)idEvent;
9521 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongPtrW(hwnd, 0);
9523 KillTimer(hwnd, idEvent);
9524 editItem->fEnabled = FALSE;
9525 /* check if the item is still selected */
9526 if (infoPtr->bFocus && LISTVIEW_GetItemState(infoPtr, editItem->iItem, LVIS_SELECTED))
9527 LISTVIEW_EditLabelT(infoPtr, editItem->iItem, TRUE);
9530 /***
9531 * DESCRIPTION:
9532 * Creates the listview control - the WM_NCCREATE phase.
9534 * PARAMETER(S):
9535 * [I] hwnd : window handle
9536 * [I] lpcs : the create parameters
9538 * RETURN:
9539 * Success: TRUE
9540 * Failure: FALSE
9542 static LRESULT LISTVIEW_NCCreate(HWND hwnd, WPARAM wParam, const CREATESTRUCTW *lpcs)
9544 LISTVIEW_INFO *infoPtr;
9545 LOGFONTW logFont;
9547 TRACE("(lpcs=%p)\n", lpcs);
9549 /* initialize info pointer */
9550 infoPtr = Alloc(sizeof(*infoPtr));
9551 if (!infoPtr) return FALSE;
9553 SetWindowLongPtrW(hwnd, 0, (DWORD_PTR)infoPtr);
9555 infoPtr->hwndSelf = hwnd;
9556 infoPtr->dwStyle = lpcs->style; /* Note: may be changed in WM_CREATE */
9557 map_style_view(infoPtr);
9558 /* determine the type of structures to use */
9559 infoPtr->hwndNotify = lpcs->hwndParent;
9560 /* infoPtr->notifyFormat will be filled in WM_CREATE */
9562 /* initialize color information */
9563 infoPtr->clrBk = CLR_NONE;
9564 infoPtr->clrText = CLR_DEFAULT;
9565 infoPtr->clrTextBk = CLR_DEFAULT;
9566 LISTVIEW_SetBkColor(infoPtr, comctl32_color.clrWindow);
9568 /* set default values */
9569 infoPtr->nFocusedItem = -1;
9570 infoPtr->nSelectionMark = -1;
9571 infoPtr->nHotItem = -1;
9572 infoPtr->redraw = TRUE;
9573 infoPtr->bNoItemMetrics = TRUE;
9574 infoPtr->notify_mask = NOTIFY_MASK_UNMASK_ALL;
9575 infoPtr->autoSpacing = TRUE;
9576 infoPtr->iconSpacing.cx = GetSystemMetrics(SM_CXICONSPACING) - GetSystemMetrics(SM_CXICON);
9577 infoPtr->iconSpacing.cy = GetSystemMetrics(SM_CYICONSPACING) - GetSystemMetrics(SM_CYICON);
9578 infoPtr->nEditLabelItem = -1;
9579 infoPtr->nLButtonDownItem = -1;
9580 infoPtr->dwHoverTime = HOVER_DEFAULT; /* default system hover time */
9581 infoPtr->cWheelRemainder = 0;
9582 infoPtr->nMeasureItemHeight = 0;
9583 infoPtr->xTrackLine = -1; /* no track line */
9584 infoPtr->itemEdit.fEnabled = FALSE;
9585 infoPtr->iVersion = COMCTL32_VERSION;
9586 infoPtr->colRectsDirty = FALSE;
9587 infoPtr->selected_column = -1;
9589 /* get default font (icon title) */
9590 SystemParametersInfoW(SPI_GETICONTITLELOGFONT, 0, &logFont, 0);
9591 infoPtr->hDefaultFont = CreateFontIndirectW(&logFont);
9592 infoPtr->hFont = infoPtr->hDefaultFont;
9593 LISTVIEW_SaveTextMetrics(infoPtr);
9595 /* allocate memory for the data structure */
9596 if (!(infoPtr->selectionRanges = ranges_create(10))) goto fail;
9597 if (!(infoPtr->hdpaItems = DPA_Create(10))) goto fail;
9598 if (!(infoPtr->hdpaItemIds = DPA_Create(10))) goto fail;
9599 if (!(infoPtr->hdpaPosX = DPA_Create(10))) goto fail;
9600 if (!(infoPtr->hdpaPosY = DPA_Create(10))) goto fail;
9601 if (!(infoPtr->hdpaColumns = DPA_Create(10))) goto fail;
9603 return DefWindowProcW(hwnd, WM_NCCREATE, wParam, (LPARAM)lpcs);
9605 fail:
9606 DestroyWindow(infoPtr->hwndHeader);
9607 ranges_destroy(infoPtr->selectionRanges);
9608 DPA_Destroy(infoPtr->hdpaItems);
9609 DPA_Destroy(infoPtr->hdpaItemIds);
9610 DPA_Destroy(infoPtr->hdpaPosX);
9611 DPA_Destroy(infoPtr->hdpaPosY);
9612 DPA_Destroy(infoPtr->hdpaColumns);
9613 Free(infoPtr);
9614 return FALSE;
9617 /***
9618 * DESCRIPTION:
9619 * Creates the listview control - the WM_CREATE phase. Most of the data is
9620 * already set up in LISTVIEW_NCCreate
9622 * PARAMETER(S):
9623 * [I] hwnd : window handle
9624 * [I] lpcs : the create parameters
9626 * RETURN:
9627 * Success: 0
9628 * Failure: -1
9630 static LRESULT LISTVIEW_Create(HWND hwnd, const CREATESTRUCTW *lpcs)
9632 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongPtrW(hwnd, 0);
9634 TRACE("lpcs %p, style %#lx\n", lpcs, lpcs->style);
9636 infoPtr->dwStyle = lpcs->style;
9637 map_style_view(infoPtr);
9639 infoPtr->notifyFormat = SendMessageW(infoPtr->hwndNotify, WM_NOTIFYFORMAT,
9640 (WPARAM)infoPtr->hwndSelf, NF_QUERY);
9641 /* on error defaulting to ANSI notifications */
9642 if (infoPtr->notifyFormat == 0) infoPtr->notifyFormat = NFR_ANSI;
9643 TRACE("notify format=%d\n", infoPtr->notifyFormat);
9645 if ((infoPtr->uView == LV_VIEW_DETAILS) && (lpcs->style & WS_VISIBLE))
9647 if (LISTVIEW_CreateHeader(infoPtr) < 0) return -1;
9649 else
9650 infoPtr->hwndHeader = 0;
9652 /* init item size to avoid division by 0 */
9653 LISTVIEW_UpdateItemSize (infoPtr);
9654 LISTVIEW_UpdateSize (infoPtr);
9656 if (infoPtr->uView == LV_VIEW_DETAILS)
9658 if (!(LVS_NOCOLUMNHEADER & lpcs->style) && (WS_VISIBLE & lpcs->style))
9660 ShowWindow(infoPtr->hwndHeader, SW_SHOWNORMAL);
9662 LISTVIEW_UpdateScroll(infoPtr);
9663 /* send WM_MEASUREITEM notification */
9664 if (infoPtr->dwStyle & LVS_OWNERDRAWFIXED) notify_measureitem(infoPtr);
9667 OpenThemeData(hwnd, themeClass);
9669 /* initialize the icon sizes */
9670 set_icon_size(&infoPtr->iconSize, infoPtr->himlNormal, infoPtr->uView != LV_VIEW_ICON);
9671 set_icon_size(&infoPtr->iconStateSize, infoPtr->himlState, TRUE);
9672 return 0;
9675 /***
9676 * DESCRIPTION:
9677 * Destroys the listview control.
9679 * PARAMETER(S):
9680 * [I] infoPtr : valid pointer to the listview structure
9682 * RETURN:
9683 * Success: 0
9684 * Failure: -1
9686 static LRESULT LISTVIEW_Destroy(LISTVIEW_INFO *infoPtr)
9688 HTHEME theme = GetWindowTheme(infoPtr->hwndSelf);
9689 CloseThemeData(theme);
9691 /* delete all items */
9692 LISTVIEW_DeleteAllItems(infoPtr, TRUE);
9694 return 0;
9697 /***
9698 * DESCRIPTION:
9699 * Enables the listview control.
9701 * PARAMETER(S):
9702 * [I] infoPtr : valid pointer to the listview structure
9703 * [I] bEnable : specifies whether to enable or disable the window
9705 * RETURN:
9706 * SUCCESS : TRUE
9707 * FAILURE : FALSE
9709 static BOOL LISTVIEW_Enable(const LISTVIEW_INFO *infoPtr)
9711 if (infoPtr->dwStyle & LVS_OWNERDRAWFIXED)
9712 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
9713 return TRUE;
9716 /***
9717 * DESCRIPTION:
9718 * Erases the background of the listview control.
9720 * PARAMETER(S):
9721 * [I] infoPtr : valid pointer to the listview structure
9722 * [I] hdc : device context handle
9724 * RETURN:
9725 * SUCCESS : TRUE
9726 * FAILURE : FALSE
9728 static inline BOOL LISTVIEW_EraseBkgnd(const LISTVIEW_INFO *infoPtr, HDC hdc)
9730 RECT rc;
9732 TRACE("(hdc=%p)\n", hdc);
9734 if (!GetClipBox(hdc, &rc)) return FALSE;
9736 if (infoPtr->clrBk == CLR_NONE)
9738 if (infoPtr->dwLvExStyle & LVS_EX_TRANSPARENTBKGND)
9739 SendMessageW(infoPtr->hwndNotify, WM_PRINTCLIENT, (WPARAM)hdc, PRF_ERASEBKGND);
9740 else
9741 SendMessageW(infoPtr->hwndNotify, WM_ERASEBKGND, (WPARAM)hdc, 0);
9742 LISTVIEW_DrawBackgroundBitmap(infoPtr, hdc, &rc);
9743 return TRUE;
9746 /* for double buffered controls we need to do this during refresh */
9747 if (infoPtr->dwLvExStyle & LVS_EX_DOUBLEBUFFER) return FALSE;
9749 return LISTVIEW_FillBkgnd(infoPtr, hdc, &rc);
9753 /***
9754 * DESCRIPTION:
9755 * Helper function for LISTVIEW_[HV]Scroll *only*.
9756 * Performs vertical/horizontal scrolling by a give amount.
9758 * PARAMETER(S):
9759 * [I] infoPtr : valid pointer to the listview structure
9760 * [I] dx : amount of horizontal scroll
9761 * [I] dy : amount of vertical scroll
9763 static void scroll_list(LISTVIEW_INFO *infoPtr, INT dx, INT dy)
9765 /* now we can scroll the list */
9766 ScrollWindowEx(infoPtr->hwndSelf, dx, dy, &infoPtr->rcList,
9767 &infoPtr->rcList, 0, 0, SW_ERASE | SW_INVALIDATE);
9768 /* if we have focus, adjust rect */
9769 OffsetRect(&infoPtr->rcFocus, dx, dy);
9770 UpdateWindow(infoPtr->hwndSelf);
9773 /***
9774 * DESCRIPTION:
9775 * Performs vertical scrolling.
9777 * PARAMETER(S):
9778 * [I] infoPtr : valid pointer to the listview structure
9779 * [I] nScrollCode : scroll code
9780 * [I] nScrollDiff : units to scroll in SB_INTERNAL mode, 0 otherwise
9781 * [I] hScrollWnd : scrollbar control window handle
9783 * RETURN:
9784 * Zero
9786 * NOTES:
9787 * SB_LINEUP/SB_LINEDOWN:
9788 * for LVS_ICON, LVS_SMALLICON is 37 by experiment
9789 * for LVS_REPORT is 1 line
9790 * for LVS_LIST cannot occur
9793 static LRESULT LISTVIEW_VScroll(LISTVIEW_INFO *infoPtr, INT nScrollCode,
9794 INT nScrollDiff)
9796 INT nOldScrollPos, nNewScrollPos;
9797 SCROLLINFO scrollInfo;
9798 BOOL is_an_icon;
9800 TRACE("(nScrollCode=%d(%s), nScrollDiff=%d)\n", nScrollCode,
9801 debugscrollcode(nScrollCode), nScrollDiff);
9803 if (infoPtr->hwndEdit) SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
9805 scrollInfo.cbSize = sizeof(SCROLLINFO);
9806 scrollInfo.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_TRACKPOS;
9808 is_an_icon = ((infoPtr->uView == LV_VIEW_ICON) || (infoPtr->uView == LV_VIEW_SMALLICON));
9810 if (!GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo)) return 1;
9812 nOldScrollPos = scrollInfo.nPos;
9813 switch (nScrollCode)
9815 case SB_INTERNAL:
9816 break;
9818 case SB_LINEUP:
9819 nScrollDiff = (is_an_icon) ? -LISTVIEW_SCROLL_ICON_LINE_SIZE : -1;
9820 break;
9822 case SB_LINEDOWN:
9823 nScrollDiff = (is_an_icon) ? LISTVIEW_SCROLL_ICON_LINE_SIZE : 1;
9824 break;
9826 case SB_PAGEUP:
9827 nScrollDiff = -scrollInfo.nPage;
9828 break;
9830 case SB_PAGEDOWN:
9831 nScrollDiff = scrollInfo.nPage;
9832 break;
9834 case SB_THUMBPOSITION:
9835 case SB_THUMBTRACK:
9836 nScrollDiff = scrollInfo.nTrackPos - scrollInfo.nPos;
9837 break;
9839 default:
9840 nScrollDiff = 0;
9843 /* quit right away if pos isn't changing */
9844 if (nScrollDiff == 0) return 0;
9846 /* calculate new position, and handle overflows */
9847 nNewScrollPos = scrollInfo.nPos + nScrollDiff;
9848 if (nScrollDiff > 0) {
9849 if (nNewScrollPos < nOldScrollPos ||
9850 nNewScrollPos > scrollInfo.nMax)
9851 nNewScrollPos = scrollInfo.nMax;
9852 } else {
9853 if (nNewScrollPos > nOldScrollPos ||
9854 nNewScrollPos < scrollInfo.nMin)
9855 nNewScrollPos = scrollInfo.nMin;
9858 /* set the new position, and reread in case it changed */
9859 scrollInfo.fMask = SIF_POS;
9860 scrollInfo.nPos = nNewScrollPos;
9861 nNewScrollPos = SetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo, TRUE);
9863 /* carry on only if it really changed */
9864 if (nNewScrollPos == nOldScrollPos) return 0;
9866 /* now adjust to client coordinates */
9867 nScrollDiff = nOldScrollPos - nNewScrollPos;
9868 if (infoPtr->uView == LV_VIEW_DETAILS) nScrollDiff *= infoPtr->nItemHeight;
9870 /* and scroll the window */
9871 scroll_list(infoPtr, 0, nScrollDiff);
9873 return 0;
9876 /***
9877 * DESCRIPTION:
9878 * Performs horizontal scrolling.
9880 * PARAMETER(S):
9881 * [I] infoPtr : valid pointer to the listview structure
9882 * [I] nScrollCode : scroll code
9883 * [I] nScrollDiff : units to scroll in SB_INTERNAL mode, 0 otherwise
9884 * [I] hScrollWnd : scrollbar control window handle
9886 * RETURN:
9887 * Zero
9889 * NOTES:
9890 * SB_LINELEFT/SB_LINERIGHT:
9891 * for LVS_ICON, LVS_SMALLICON 1 pixel
9892 * for LVS_REPORT is 1 pixel
9893 * for LVS_LIST is 1 column --> which is a 1 because the
9894 * scroll is based on columns not pixels
9897 static LRESULT LISTVIEW_HScroll(LISTVIEW_INFO *infoPtr, INT nScrollCode,
9898 INT nScrollDiff)
9900 INT nOldScrollPos, nNewScrollPos;
9901 SCROLLINFO scrollInfo;
9902 BOOL is_an_icon;
9904 TRACE("(nScrollCode=%d(%s), nScrollDiff=%d)\n", nScrollCode,
9905 debugscrollcode(nScrollCode), nScrollDiff);
9907 if (infoPtr->hwndEdit) SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
9909 scrollInfo.cbSize = sizeof(SCROLLINFO);
9910 scrollInfo.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_TRACKPOS;
9912 is_an_icon = ((infoPtr->uView == LV_VIEW_ICON) || (infoPtr->uView == LV_VIEW_SMALLICON));
9914 if (!GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo)) return 1;
9916 nOldScrollPos = scrollInfo.nPos;
9918 switch (nScrollCode)
9920 case SB_INTERNAL:
9921 break;
9923 case SB_LINELEFT:
9924 nScrollDiff = (is_an_icon) ? -LISTVIEW_SCROLL_ICON_LINE_SIZE : -1;
9925 break;
9927 case SB_LINERIGHT:
9928 nScrollDiff = (is_an_icon) ? LISTVIEW_SCROLL_ICON_LINE_SIZE : 1;
9929 break;
9931 case SB_PAGELEFT:
9932 nScrollDiff = -scrollInfo.nPage;
9933 break;
9935 case SB_PAGERIGHT:
9936 nScrollDiff = scrollInfo.nPage;
9937 break;
9939 case SB_THUMBPOSITION:
9940 case SB_THUMBTRACK:
9941 nScrollDiff = scrollInfo.nTrackPos - scrollInfo.nPos;
9942 break;
9944 default:
9945 nScrollDiff = 0;
9948 /* quit right away if pos isn't changing */
9949 if (nScrollDiff == 0) return 0;
9951 /* calculate new position, and handle overflows */
9952 nNewScrollPos = scrollInfo.nPos + nScrollDiff;
9953 if (nScrollDiff > 0) {
9954 if (nNewScrollPos < nOldScrollPos ||
9955 nNewScrollPos > scrollInfo.nMax)
9956 nNewScrollPos = scrollInfo.nMax;
9957 } else {
9958 if (nNewScrollPos > nOldScrollPos ||
9959 nNewScrollPos < scrollInfo.nMin)
9960 nNewScrollPos = scrollInfo.nMin;
9963 /* set the new position, and reread in case it changed */
9964 scrollInfo.fMask = SIF_POS;
9965 scrollInfo.nPos = nNewScrollPos;
9966 nNewScrollPos = SetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo, TRUE);
9968 /* carry on only if it really changed */
9969 if (nNewScrollPos == nOldScrollPos) return 0;
9971 LISTVIEW_UpdateHeaderSize(infoPtr, nNewScrollPos);
9973 /* now adjust to client coordinates */
9974 nScrollDiff = nOldScrollPos - nNewScrollPos;
9975 if (infoPtr->uView == LV_VIEW_LIST) nScrollDiff *= infoPtr->nItemWidth;
9977 /* and scroll the window */
9978 scroll_list(infoPtr, nScrollDiff, 0);
9980 return 0;
9983 static LRESULT LISTVIEW_MouseWheel(LISTVIEW_INFO *infoPtr, INT wheelDelta)
9985 INT pulScrollLines = 3;
9987 TRACE("(wheelDelta=%d)\n", wheelDelta);
9989 switch(infoPtr->uView)
9991 case LV_VIEW_ICON:
9992 case LV_VIEW_SMALLICON:
9994 * listview should be scrolled by a multiple of 37 dependently on its dimension or its visible item number
9995 * should be fixed in the future.
9997 LISTVIEW_VScroll(infoPtr, SB_INTERNAL, (wheelDelta > 0) ?
9998 -LISTVIEW_SCROLL_ICON_LINE_SIZE : LISTVIEW_SCROLL_ICON_LINE_SIZE);
9999 break;
10001 case LV_VIEW_DETAILS:
10002 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
10004 /* if scrolling changes direction, ignore left overs */
10005 if ((wheelDelta < 0 && infoPtr->cWheelRemainder < 0) ||
10006 (wheelDelta > 0 && infoPtr->cWheelRemainder > 0))
10007 infoPtr->cWheelRemainder += wheelDelta;
10008 else
10009 infoPtr->cWheelRemainder = wheelDelta;
10010 if (infoPtr->cWheelRemainder && pulScrollLines)
10012 int cLineScroll;
10013 pulScrollLines = min((UINT)LISTVIEW_GetCountPerColumn(infoPtr), pulScrollLines);
10014 cLineScroll = pulScrollLines * infoPtr->cWheelRemainder / WHEEL_DELTA;
10015 infoPtr->cWheelRemainder -= WHEEL_DELTA * cLineScroll / pulScrollLines;
10016 LISTVIEW_VScroll(infoPtr, SB_INTERNAL, -cLineScroll);
10018 break;
10020 case LV_VIEW_LIST:
10021 LISTVIEW_HScroll(infoPtr, (wheelDelta > 0) ? SB_LINELEFT : SB_LINERIGHT, 0);
10022 break;
10024 return 0;
10027 /***
10028 * DESCRIPTION:
10029 * ???
10031 * PARAMETER(S):
10032 * [I] infoPtr : valid pointer to the listview structure
10033 * [I] nVirtualKey : virtual key
10034 * [I] lKeyData : key data
10036 * RETURN:
10037 * Zero
10039 static LRESULT LISTVIEW_KeyDown(LISTVIEW_INFO *infoPtr, INT nVirtualKey, LONG lKeyData)
10041 HWND hwndSelf = infoPtr->hwndSelf;
10042 INT nItem = -1;
10043 NMLVKEYDOWN nmKeyDown;
10045 TRACE("(nVirtualKey=%d, lKeyData=%ld)\n", nVirtualKey, lKeyData);
10047 /* send LVN_KEYDOWN notification */
10048 nmKeyDown.wVKey = nVirtualKey;
10049 nmKeyDown.flags = 0;
10050 notify_hdr(infoPtr, LVN_KEYDOWN, &nmKeyDown.hdr);
10051 if (!IsWindow(hwndSelf))
10052 return 0;
10054 switch (nVirtualKey)
10056 case VK_SPACE:
10057 nItem = infoPtr->nFocusedItem;
10058 if (infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES)
10059 toggle_checkbox_state(infoPtr, infoPtr->nFocusedItem);
10060 break;
10062 case VK_RETURN:
10063 if ((infoPtr->nItemCount > 0) && (infoPtr->nFocusedItem != -1))
10065 if (!notify(infoPtr, NM_RETURN)) return 0;
10066 if (!notify(infoPtr, LVN_ITEMACTIVATE)) return 0;
10068 break;
10070 case VK_HOME:
10071 if (infoPtr->nItemCount > 0)
10072 nItem = 0;
10073 break;
10075 case VK_END:
10076 if (infoPtr->nItemCount > 0)
10077 nItem = infoPtr->nItemCount - 1;
10078 break;
10080 case VK_LEFT:
10081 nItem = LISTVIEW_GetNextItem(infoPtr, infoPtr->nFocusedItem, LVNI_TOLEFT);
10082 break;
10084 case VK_UP:
10085 nItem = LISTVIEW_GetNextItem(infoPtr, infoPtr->nFocusedItem, LVNI_ABOVE);
10086 break;
10088 case VK_RIGHT:
10089 nItem = LISTVIEW_GetNextItem(infoPtr, infoPtr->nFocusedItem, LVNI_TORIGHT);
10090 break;
10092 case VK_DOWN:
10093 nItem = LISTVIEW_GetNextItem(infoPtr, infoPtr->nFocusedItem, LVNI_BELOW);
10094 break;
10096 case VK_PRIOR:
10097 if (infoPtr->uView == LV_VIEW_DETAILS)
10099 INT topidx = LISTVIEW_GetTopIndex(infoPtr);
10100 if (infoPtr->nFocusedItem == topidx)
10101 nItem = topidx - LISTVIEW_GetCountPerColumn(infoPtr) + 1;
10102 else
10103 nItem = topidx;
10105 else
10106 nItem = infoPtr->nFocusedItem - LISTVIEW_GetCountPerColumn(infoPtr)
10107 * LISTVIEW_GetCountPerRow(infoPtr);
10108 if(nItem < 0) nItem = 0;
10109 break;
10111 case VK_NEXT:
10112 if (infoPtr->uView == LV_VIEW_DETAILS)
10114 INT topidx = LISTVIEW_GetTopIndex(infoPtr);
10115 INT cnt = LISTVIEW_GetCountPerColumn(infoPtr);
10116 if (infoPtr->nFocusedItem == topidx + cnt - 1)
10117 nItem = infoPtr->nFocusedItem + cnt - 1;
10118 else
10119 nItem = topidx + cnt - 1;
10121 else
10122 nItem = infoPtr->nFocusedItem + LISTVIEW_GetCountPerColumn(infoPtr)
10123 * LISTVIEW_GetCountPerRow(infoPtr);
10124 if(nItem >= infoPtr->nItemCount) nItem = infoPtr->nItemCount - 1;
10125 break;
10128 if ((nItem != -1) && (nItem != infoPtr->nFocusedItem || nVirtualKey == VK_SPACE))
10129 LISTVIEW_KeySelection(infoPtr, nItem, nVirtualKey == VK_SPACE);
10131 return 0;
10134 /***
10135 * DESCRIPTION:
10136 * Kills the focus.
10138 * PARAMETER(S):
10139 * [I] infoPtr : valid pointer to the listview structure
10141 * RETURN:
10142 * Zero
10144 static LRESULT LISTVIEW_KillFocus(LISTVIEW_INFO *infoPtr)
10146 TRACE("()\n");
10148 /* drop any left over scroll amount */
10149 infoPtr->cWheelRemainder = 0;
10151 /* if we did not have the focus, there's nothing more to do */
10152 if (!infoPtr->bFocus) return 0;
10154 /* send NM_KILLFOCUS notification */
10155 if (!notify(infoPtr, NM_KILLFOCUS)) return 0;
10157 /* if we have a focus rectangle, get rid of it */
10158 LISTVIEW_ShowFocusRect(infoPtr, FALSE);
10160 /* if have a marquee selection, stop it */
10161 if (infoPtr->bMarqueeSelect)
10163 /* Remove the marquee rectangle and release our mouse capture */
10164 LISTVIEW_InvalidateRect(infoPtr, &infoPtr->marqueeRect);
10165 ReleaseCapture();
10167 SetRectEmpty(&infoPtr->marqueeRect);
10169 infoPtr->bMarqueeSelect = FALSE;
10170 infoPtr->bScrolling = FALSE;
10171 KillTimer(infoPtr->hwndSelf, (UINT_PTR) infoPtr);
10174 /* set window focus flag */
10175 infoPtr->bFocus = FALSE;
10177 /* invalidate the selected items before resetting focus flag */
10178 LISTVIEW_InvalidateSelectedItems(infoPtr);
10180 return 0;
10183 /***
10184 * DESCRIPTION:
10185 * Processes double click messages (left mouse button).
10187 * PARAMETER(S):
10188 * [I] infoPtr : valid pointer to the listview structure
10189 * [I] wKey : key flag
10190 * [I] x,y : mouse coordinate
10192 * RETURN:
10193 * Zero
10195 static LRESULT LISTVIEW_LButtonDblClk(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
10197 LVHITTESTINFO htInfo;
10199 TRACE("(key=%hu, X=%u, Y=%u)\n", wKey, x, y);
10201 /* Cancel the item edition if any */
10202 if (infoPtr->itemEdit.fEnabled)
10204 KillTimer(infoPtr->hwndSelf, (UINT_PTR)&infoPtr->itemEdit);
10205 infoPtr->itemEdit.fEnabled = FALSE;
10208 /* send NM_RELEASEDCAPTURE notification */
10209 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
10211 htInfo.pt.x = x;
10212 htInfo.pt.y = y;
10214 /* send NM_DBLCLK notification */
10215 LISTVIEW_HitTest(infoPtr, &htInfo, TRUE, FALSE);
10216 if (!notify_click(infoPtr, NM_DBLCLK, &htInfo)) return 0;
10218 /* To send the LVN_ITEMACTIVATE, it must be on an Item */
10219 if(htInfo.iItem != -1) notify_itemactivate(infoPtr,&htInfo);
10221 return 0;
10224 static LRESULT LISTVIEW_TrackMouse(const LISTVIEW_INFO *infoPtr, POINT pt)
10226 MSG msg;
10227 RECT r;
10229 r.top = r.bottom = pt.y;
10230 r.left = r.right = pt.x;
10232 InflateRect(&r, GetSystemMetrics(SM_CXDRAG), GetSystemMetrics(SM_CYDRAG));
10234 SetCapture(infoPtr->hwndSelf);
10236 while (1)
10238 if (PeekMessageW(&msg, 0, 0, 0, PM_REMOVE | PM_NOYIELD))
10240 if (msg.message == WM_MOUSEMOVE)
10242 pt.x = (short)LOWORD(msg.lParam);
10243 pt.y = (short)HIWORD(msg.lParam);
10244 if (PtInRect(&r, pt))
10245 continue;
10246 else
10248 ReleaseCapture();
10249 return 1;
10252 else if (msg.message >= WM_LBUTTONDOWN &&
10253 msg.message <= WM_RBUTTONDBLCLK)
10255 break;
10258 DispatchMessageW(&msg);
10261 if (GetCapture() != infoPtr->hwndSelf)
10262 return 0;
10265 ReleaseCapture();
10266 return 0;
10270 /***
10271 * DESCRIPTION:
10272 * Processes mouse down messages (left mouse button).
10274 * PARAMETERS:
10275 * infoPtr [I ] valid pointer to the listview structure
10276 * wKey [I ] key flag
10277 * x,y [I ] mouse coordinate
10279 * RETURN:
10280 * Zero
10282 static LRESULT LISTVIEW_LButtonDown(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
10284 LVHITTESTINFO lvHitTestInfo;
10285 static BOOL bGroupSelect = TRUE;
10286 POINT pt = { x, y };
10287 INT nItem;
10289 TRACE("(key=%hu, X=%u, Y=%u)\n", wKey, x, y);
10291 /* send NM_RELEASEDCAPTURE notification */
10292 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
10294 /* set left button down flag and record the click position */
10295 infoPtr->bLButtonDown = TRUE;
10296 infoPtr->ptClickPos = pt;
10297 infoPtr->bDragging = FALSE;
10298 infoPtr->bMarqueeSelect = FALSE;
10299 infoPtr->bScrolling = FALSE;
10301 lvHitTestInfo.pt.x = x;
10302 lvHitTestInfo.pt.y = y;
10304 nItem = LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, TRUE);
10305 TRACE("at %s, nItem=%d\n", wine_dbgstr_point(&pt), nItem);
10306 if ((nItem >= 0) && (nItem < infoPtr->nItemCount))
10308 if ((infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES) && (lvHitTestInfo.flags & LVHT_ONITEMSTATEICON))
10310 notify_click(infoPtr, NM_CLICK, &lvHitTestInfo);
10311 toggle_checkbox_state(infoPtr, nItem);
10312 infoPtr->bLButtonDown = FALSE;
10313 return 0;
10316 if (infoPtr->dwStyle & LVS_SINGLESEL)
10318 if (LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED))
10319 infoPtr->nEditLabelItem = nItem;
10320 else
10321 LISTVIEW_SetSelection(infoPtr, nItem);
10323 else
10325 if ((wKey & MK_CONTROL) && (wKey & MK_SHIFT))
10327 if (bGroupSelect)
10329 if (!LISTVIEW_AddGroupSelection(infoPtr, nItem)) return 0;
10330 LISTVIEW_SetItemFocus(infoPtr, nItem);
10331 infoPtr->nSelectionMark = nItem;
10333 else
10335 LVITEMW item;
10337 item.state = LVIS_SELECTED | LVIS_FOCUSED;
10338 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
10340 LISTVIEW_SetItemState(infoPtr,nItem,&item);
10341 infoPtr->nSelectionMark = nItem;
10344 else if (wKey & MK_CONTROL)
10346 LVITEMW item;
10348 bGroupSelect = (LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED) == 0);
10350 item.state = (bGroupSelect ? LVIS_SELECTED : 0) | LVIS_FOCUSED;
10351 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
10352 LISTVIEW_SetItemState(infoPtr, nItem, &item);
10353 infoPtr->nSelectionMark = nItem;
10355 else if (wKey & MK_SHIFT)
10357 LISTVIEW_SetGroupSelection(infoPtr, nItem);
10359 else
10361 if (LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED))
10363 infoPtr->nEditLabelItem = nItem;
10364 infoPtr->nLButtonDownItem = nItem;
10366 LISTVIEW_SetItemFocus(infoPtr, nItem);
10368 else
10369 /* set selection (clears other pre-existing selections) */
10370 LISTVIEW_SetSelection(infoPtr, nItem);
10374 if (!infoPtr->bFocus)
10375 SetFocus(infoPtr->hwndSelf);
10377 if (infoPtr->dwLvExStyle & LVS_EX_ONECLICKACTIVATE)
10378 if(lvHitTestInfo.iItem != -1) notify_itemactivate(infoPtr,&lvHitTestInfo);
10380 else
10382 if (!infoPtr->bFocus)
10383 SetFocus(infoPtr->hwndSelf);
10385 /* remove all selections */
10386 if (!(wKey & MK_CONTROL) && !(wKey & MK_SHIFT))
10387 LISTVIEW_DeselectAll(infoPtr);
10388 ReleaseCapture();
10391 return 0;
10394 /***
10395 * DESCRIPTION:
10396 * Processes mouse up messages (left mouse button).
10398 * PARAMETERS:
10399 * infoPtr [I ] valid pointer to the listview structure
10400 * wKey [I ] key flag
10401 * x,y [I ] mouse coordinate
10403 * RETURN:
10404 * Zero
10406 static LRESULT LISTVIEW_LButtonUp(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
10408 LVHITTESTINFO lvHitTestInfo;
10410 TRACE("(key=%hu, X=%u, Y=%u)\n", wKey, x, y);
10412 if (!infoPtr->bLButtonDown) return 0;
10414 lvHitTestInfo.pt.x = x;
10415 lvHitTestInfo.pt.y = y;
10417 /* send NM_CLICK notification */
10418 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
10419 if (!notify_click(infoPtr, NM_CLICK, &lvHitTestInfo)) return 0;
10421 /* set left button flag */
10422 infoPtr->bLButtonDown = FALSE;
10424 /* set a single selection, reset others */
10425 if(lvHitTestInfo.iItem == infoPtr->nLButtonDownItem && lvHitTestInfo.iItem != -1)
10426 LISTVIEW_SetSelection(infoPtr, infoPtr->nLButtonDownItem);
10427 infoPtr->nLButtonDownItem = -1;
10429 if (infoPtr->bDragging || infoPtr->bMarqueeSelect)
10431 /* Remove the marquee rectangle and release our mouse capture */
10432 if (infoPtr->bMarqueeSelect)
10434 LISTVIEW_InvalidateRect(infoPtr, &infoPtr->marqueeDrawRect);
10435 ReleaseCapture();
10438 SetRectEmpty(&infoPtr->marqueeRect);
10439 SetRectEmpty(&infoPtr->marqueeDrawRect);
10441 infoPtr->bDragging = FALSE;
10442 infoPtr->bMarqueeSelect = FALSE;
10443 infoPtr->bScrolling = FALSE;
10445 KillTimer(infoPtr->hwndSelf, (UINT_PTR) infoPtr);
10446 return 0;
10449 /* if we clicked on a selected item, edit the label */
10450 if(lvHitTestInfo.iItem == infoPtr->nEditLabelItem && (lvHitTestInfo.flags & LVHT_ONITEMLABEL))
10452 /* we want to make sure the user doesn't want to do a double click. So we will
10453 * delay the edit. WM_LBUTTONDBLCLICK will cancel the timer
10455 infoPtr->itemEdit.fEnabled = TRUE;
10456 infoPtr->itemEdit.iItem = lvHitTestInfo.iItem;
10457 SetTimer(infoPtr->hwndSelf,
10458 (UINT_PTR)&infoPtr->itemEdit,
10459 GetDoubleClickTime(),
10460 LISTVIEW_DelayedEditItem);
10463 return 0;
10466 /***
10467 * DESCRIPTION:
10468 * Destroys the listview control (called after WM_DESTROY).
10470 * PARAMETER(S):
10471 * [I] infoPtr : valid pointer to the listview structure
10473 * RETURN:
10474 * Zero
10476 static LRESULT LISTVIEW_NCDestroy(LISTVIEW_INFO *infoPtr)
10478 INT i;
10480 TRACE("()\n");
10482 /* destroy data structure */
10483 DPA_Destroy(infoPtr->hdpaItems);
10484 DPA_Destroy(infoPtr->hdpaItemIds);
10485 DPA_Destroy(infoPtr->hdpaPosX);
10486 DPA_Destroy(infoPtr->hdpaPosY);
10487 /* columns */
10488 for (i = 0; i < DPA_GetPtrCount(infoPtr->hdpaColumns); i++)
10489 Free(DPA_GetPtr(infoPtr->hdpaColumns, i));
10490 DPA_Destroy(infoPtr->hdpaColumns);
10491 ranges_destroy(infoPtr->selectionRanges);
10493 /* destroy image lists */
10494 if (!(infoPtr->dwStyle & LVS_SHAREIMAGELISTS))
10496 ImageList_Destroy(infoPtr->himlNormal);
10497 ImageList_Destroy(infoPtr->himlSmall);
10498 ImageList_Destroy(infoPtr->himlState);
10501 /* destroy font, bkgnd brush */
10502 infoPtr->hFont = 0;
10503 if (infoPtr->hDefaultFont) DeleteObject(infoPtr->hDefaultFont);
10504 if (infoPtr->clrBk != CLR_NONE) DeleteObject(infoPtr->hBkBrush);
10505 if (infoPtr->hBkBitmap) DeleteObject(infoPtr->hBkBitmap);
10507 SetWindowLongPtrW(infoPtr->hwndSelf, 0, 0);
10509 Free(infoPtr);
10511 return 0;
10514 /***
10515 * DESCRIPTION:
10516 * Handles notifications.
10518 * PARAMETER(S):
10519 * [I] infoPtr : valid pointer to the listview structure
10520 * [I] lpnmhdr : notification information
10522 * RETURN:
10523 * Zero
10525 static LRESULT LISTVIEW_Notify(LISTVIEW_INFO *infoPtr, NMHDR *lpnmhdr)
10527 NMHEADERW *lpnmh;
10529 TRACE("(lpnmhdr=%p)\n", lpnmhdr);
10531 if (!lpnmhdr || lpnmhdr->hwndFrom != infoPtr->hwndHeader) return 0;
10533 /* remember: HDN_LAST < HDN_FIRST */
10534 if (lpnmhdr->code > HDN_FIRST || lpnmhdr->code < HDN_LAST) return 0;
10535 lpnmh = (NMHEADERW *)lpnmhdr;
10537 if (lpnmh->iItem < 0 || lpnmh->iItem >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return 0;
10539 switch (lpnmhdr->code)
10541 case HDN_TRACKW:
10542 case HDN_TRACKA:
10544 COLUMN_INFO *lpColumnInfo;
10545 POINT ptOrigin;
10546 INT x;
10548 if (!lpnmh->pitem || !(lpnmh->pitem->mask & HDI_WIDTH))
10549 break;
10551 /* remove the old line (if any) */
10552 LISTVIEW_DrawTrackLine(infoPtr);
10554 /* compute & draw the new line */
10555 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, lpnmh->iItem);
10556 x = lpColumnInfo->rcHeader.left + lpnmh->pitem->cxy;
10557 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
10558 infoPtr->xTrackLine = x + ptOrigin.x;
10559 LISTVIEW_DrawTrackLine(infoPtr);
10560 return notify_forward_header(infoPtr, lpnmh);
10563 case HDN_ENDTRACKA:
10564 case HDN_ENDTRACKW:
10565 /* remove the track line (if any) */
10566 LISTVIEW_DrawTrackLine(infoPtr);
10567 infoPtr->xTrackLine = -1;
10568 return notify_forward_header(infoPtr, lpnmh);
10570 case HDN_BEGINDRAG:
10571 if ((infoPtr->dwLvExStyle & LVS_EX_HEADERDRAGDROP) == 0) return 1;
10572 return notify_forward_header(infoPtr, lpnmh);
10574 case HDN_ENDDRAG:
10575 infoPtr->colRectsDirty = TRUE;
10576 LISTVIEW_InvalidateList(infoPtr);
10577 return notify_forward_header(infoPtr, lpnmh);
10579 case HDN_ITEMCHANGEDW:
10580 case HDN_ITEMCHANGEDA:
10582 COLUMN_INFO *lpColumnInfo;
10583 HDITEMW hdi;
10584 INT dx, cxy;
10586 if (!lpnmh->pitem || !(lpnmh->pitem->mask & HDI_WIDTH))
10588 hdi.mask = HDI_WIDTH;
10589 if (!SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, lpnmh->iItem, (LPARAM)&hdi)) return 0;
10590 cxy = hdi.cxy;
10592 else
10593 cxy = lpnmh->pitem->cxy;
10595 /* determine how much we change since the last know position */
10596 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, lpnmh->iItem);
10597 dx = cxy - (lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left);
10598 if (dx != 0)
10600 lpColumnInfo->rcHeader.right += dx;
10602 hdi.mask = HDI_ORDER;
10603 SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, lpnmh->iItem, (LPARAM)&hdi);
10605 /* not the rightmost one */
10606 if (hdi.iOrder + 1 < DPA_GetPtrCount(infoPtr->hdpaColumns))
10608 INT nIndex = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX,
10609 hdi.iOrder + 1, 0);
10610 LISTVIEW_ScrollColumns(infoPtr, nIndex, dx);
10612 else
10614 /* only needs to update the scrolls */
10615 infoPtr->nItemWidth += dx;
10616 LISTVIEW_UpdateScroll(infoPtr);
10618 LISTVIEW_UpdateItemSize(infoPtr);
10619 if (infoPtr->uView == LV_VIEW_DETAILS && is_redrawing(infoPtr))
10621 POINT ptOrigin;
10622 RECT rcCol = lpColumnInfo->rcHeader;
10624 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
10625 OffsetRect(&rcCol, ptOrigin.x, 0);
10627 rcCol.top = infoPtr->rcList.top;
10628 rcCol.bottom = infoPtr->rcList.bottom;
10630 /* resizing left-aligned columns leaves most of the left side untouched */
10631 if ((lpColumnInfo->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_LEFT)
10633 INT nMaxDirty = infoPtr->nEllipsisWidth + infoPtr->ntmMaxCharWidth;
10634 if (dx > 0)
10635 nMaxDirty += dx;
10636 rcCol.left = max (rcCol.left, rcCol.right - nMaxDirty);
10639 /* when shrinking the last column clear the now unused field */
10640 if (hdi.iOrder == DPA_GetPtrCount(infoPtr->hdpaColumns) - 1)
10642 RECT right;
10644 rcCol.right -= dx;
10646 /* deal with right from rightmost column area */
10647 right.left = rcCol.right;
10648 right.top = rcCol.top;
10649 right.bottom = rcCol.bottom;
10650 right.right = infoPtr->rcList.right;
10652 LISTVIEW_InvalidateRect(infoPtr, &right);
10655 LISTVIEW_InvalidateRect(infoPtr, &rcCol);
10658 break;
10661 case HDN_ITEMCLICKW:
10662 case HDN_ITEMCLICKA:
10664 /* Handle sorting by Header Column */
10665 NMLISTVIEW nmlv;
10667 ZeroMemory(&nmlv, sizeof(NMLISTVIEW));
10668 nmlv.iItem = -1;
10669 nmlv.iSubItem = lpnmh->iItem;
10670 notify_listview(infoPtr, LVN_COLUMNCLICK, &nmlv);
10671 return notify_forward_header(infoPtr, lpnmh);
10674 case HDN_DIVIDERDBLCLICKW:
10675 case HDN_DIVIDERDBLCLICKA:
10676 /* FIXME: for LVS_EX_HEADERINALLVIEWS and not LV_VIEW_DETAILS
10677 we should use LVSCW_AUTOSIZE_USEHEADER, helper rework or
10678 split needed for that */
10679 LISTVIEW_SetColumnWidth(infoPtr, lpnmh->iItem, LVSCW_AUTOSIZE);
10680 return notify_forward_header(infoPtr, lpnmh);
10682 return 0;
10685 /***
10686 * DESCRIPTION:
10687 * Paint non-client area of control.
10689 * PARAMETER(S):
10690 * [I] infoPtr : valid pointer to the listview structureof the sender
10691 * [I] region : update region
10693 * RETURN:
10694 * 0 - frame was painted
10696 static LRESULT LISTVIEW_NCPaint(const LISTVIEW_INFO *infoPtr, HRGN region)
10698 LONG exstyle = GetWindowLongW (infoPtr->hwndSelf, GWL_EXSTYLE);
10699 HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
10700 RECT r, window_rect;
10701 HDC dc;
10702 HRGN cliprgn;
10703 int cxEdge = GetSystemMetrics (SM_CXEDGE),
10704 cyEdge = GetSystemMetrics (SM_CYEDGE);
10706 if (!theme || !(exstyle & WS_EX_CLIENTEDGE))
10707 return DefWindowProcW (infoPtr->hwndSelf, WM_NCPAINT, (WPARAM)region, 0);
10709 GetWindowRect(infoPtr->hwndSelf, &r);
10711 cliprgn = CreateRectRgn (r.left + cxEdge, r.top + cyEdge,
10712 r.right - cxEdge, r.bottom - cyEdge);
10713 if (region != (HRGN)1)
10714 CombineRgn (cliprgn, cliprgn, region, RGN_AND);
10716 dc = GetDCEx(infoPtr->hwndSelf, region, DCX_WINDOW | DCX_INTERSECTRGN);
10717 if (infoPtr->hwndHeader && LISTVIEW_IsHeaderEnabled(infoPtr))
10719 GetWindowRect(infoPtr->hwndHeader, &window_rect);
10720 OffsetRect(&window_rect, -r.left, -r.top);
10721 ExcludeClipRect(dc, window_rect.left, window_rect.top, window_rect.right, window_rect.bottom);
10724 OffsetRect(&r, -r.left, -r.top);
10725 if (IsThemeBackgroundPartiallyTransparent (theme, 0, 0))
10726 DrawThemeParentBackground(infoPtr->hwndSelf, dc, &r);
10727 DrawThemeBackground (theme, dc, 0, 0, &r, 0);
10728 ReleaseDC(infoPtr->hwndSelf, dc);
10730 /* Call default proc to get the scrollbars etc. painted */
10731 DefWindowProcW (infoPtr->hwndSelf, WM_NCPAINT, (WPARAM)cliprgn, 0);
10732 DeleteObject(cliprgn);
10734 return 0;
10737 /***
10738 * DESCRIPTION:
10739 * Determines the type of structure to use.
10741 * PARAMETER(S):
10742 * [I] infoPtr : valid pointer to the listview structureof the sender
10743 * [I] hwndFrom : listview window handle
10744 * [I] nCommand : command specifying the nature of the WM_NOTIFYFORMAT
10746 * RETURN:
10747 * Zero
10749 static LRESULT LISTVIEW_NotifyFormat(LISTVIEW_INFO *infoPtr, HWND hwndFrom, INT nCommand)
10751 TRACE("(hwndFrom=%p, nCommand=%d)\n", hwndFrom, nCommand);
10753 if (nCommand == NF_REQUERY)
10754 infoPtr->notifyFormat = SendMessageW(infoPtr->hwndNotify, WM_NOTIFYFORMAT, (WPARAM)infoPtr->hwndSelf, NF_QUERY);
10756 return infoPtr->notifyFormat;
10759 /***
10760 * DESCRIPTION:
10761 * Paints/Repaints the listview control. Internal use.
10763 * PARAMETER(S):
10764 * [I] infoPtr : valid pointer to the listview structure
10765 * [I] hdc : device context handle
10767 * RETURN:
10768 * Zero
10770 static LRESULT LISTVIEW_Paint(LISTVIEW_INFO *infoPtr, HDC hdc)
10772 TRACE("(hdc=%p)\n", hdc);
10774 if (infoPtr->bNoItemMetrics && infoPtr->nItemCount)
10776 infoPtr->bNoItemMetrics = FALSE;
10777 LISTVIEW_UpdateItemSize(infoPtr);
10778 if (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON)
10779 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
10780 LISTVIEW_UpdateScroll(infoPtr);
10783 if (infoPtr->hwndHeader) UpdateWindow(infoPtr->hwndHeader);
10785 if (hdc)
10786 LISTVIEW_Refresh(infoPtr, hdc, NULL);
10787 else
10789 PAINTSTRUCT ps;
10791 hdc = BeginPaint(infoPtr->hwndSelf, &ps);
10792 if (!hdc) return 1;
10793 LISTVIEW_Refresh(infoPtr, hdc, ps.fErase ? &ps.rcPaint : NULL);
10794 EndPaint(infoPtr->hwndSelf, &ps);
10797 return 0;
10800 /***
10801 * DESCRIPTION:
10802 * Paints/Repaints the listview control, WM_PAINT handler.
10804 * PARAMETER(S):
10805 * [I] infoPtr : valid pointer to the listview structure
10806 * [I] hdc : device context handle
10808 * RETURN:
10809 * Zero
10811 static inline LRESULT LISTVIEW_WMPaint(LISTVIEW_INFO *infoPtr, HDC hdc)
10813 TRACE("(hdc=%p)\n", hdc);
10815 if (!is_redrawing(infoPtr))
10816 return DefWindowProcW (infoPtr->hwndSelf, WM_PAINT, (WPARAM)hdc, 0);
10818 return LISTVIEW_Paint(infoPtr, hdc);
10821 /***
10822 * DESCRIPTION:
10823 * Paints/Repaints the listview control.
10825 * PARAMETER(S):
10826 * [I] infoPtr : valid pointer to the listview structure
10827 * [I] hdc : device context handle
10828 * [I] options : drawing options
10830 * RETURN:
10831 * Zero
10833 static LRESULT LISTVIEW_PrintClient(LISTVIEW_INFO *infoPtr, HDC hdc, DWORD options)
10835 FIXME("(hdc=%p options=%#lx) partial stub\n", hdc, options);
10837 LISTVIEW_Paint(infoPtr, hdc);
10839 return 0;
10843 /***
10844 * DESCRIPTION:
10845 * Processes double click messages (right mouse button).
10847 * PARAMETER(S):
10848 * [I] infoPtr : valid pointer to the listview structure
10849 * [I] wKey : key flag
10850 * [I] x,y : mouse coordinate
10852 * RETURN:
10853 * Zero
10855 static LRESULT LISTVIEW_RButtonDblClk(const LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
10857 LVHITTESTINFO lvHitTestInfo;
10859 TRACE("(key=%hu,X=%u,Y=%u)\n", wKey, x, y);
10861 /* send NM_RELEASEDCAPTURE notification */
10862 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
10864 /* send NM_RDBLCLK notification */
10865 lvHitTestInfo.pt.x = x;
10866 lvHitTestInfo.pt.y = y;
10867 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
10868 notify_click(infoPtr, NM_RDBLCLK, &lvHitTestInfo);
10870 return 0;
10873 /***
10874 * DESCRIPTION:
10875 * Processes WM_RBUTTONDOWN message and corresponding drag operation.
10877 * PARAMETER(S):
10878 * [I] infoPtr : valid pointer to the listview structure
10879 * [I] wKey : key flag
10880 * [I] x, y : mouse coordinate
10882 * RETURN:
10883 * Zero
10885 static LRESULT LISTVIEW_RButtonDown(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
10887 LVHITTESTINFO ht;
10888 INT item;
10890 TRACE("(key=%hu, x=%d, y=%d)\n", wKey, x, y);
10892 /* send NM_RELEASEDCAPTURE notification */
10893 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
10895 /* determine the index of the selected item */
10896 ht.pt.x = x;
10897 ht.pt.y = y;
10898 item = LISTVIEW_HitTest(infoPtr, &ht, TRUE, TRUE);
10900 /* make sure the listview control window has the focus */
10901 if (!infoPtr->bFocus) SetFocus(infoPtr->hwndSelf);
10903 if ((item >= 0) && (item < infoPtr->nItemCount))
10905 LISTVIEW_SetItemFocus(infoPtr, item);
10906 if (!((wKey & MK_SHIFT) || (wKey & MK_CONTROL)) &&
10907 !LISTVIEW_GetItemState(infoPtr, item, LVIS_SELECTED))
10908 LISTVIEW_SetSelection(infoPtr, item);
10910 else
10911 LISTVIEW_DeselectAll(infoPtr);
10913 if (LISTVIEW_TrackMouse(infoPtr, ht.pt))
10915 if (ht.iItem != -1)
10917 NMLISTVIEW nmlv;
10919 memset(&nmlv, 0, sizeof(nmlv));
10920 nmlv.iItem = ht.iItem;
10921 nmlv.ptAction = ht.pt;
10923 notify_listview(infoPtr, LVN_BEGINRDRAG, &nmlv);
10926 else
10928 SetFocus(infoPtr->hwndSelf);
10930 ht.pt.x = x;
10931 ht.pt.y = y;
10932 LISTVIEW_HitTest(infoPtr, &ht, TRUE, FALSE);
10934 if (notify_click(infoPtr, NM_RCLICK, &ht))
10936 /* Send a WM_CONTEXTMENU message in response to the WM_RBUTTONUP */
10937 SendMessageW(infoPtr->hwndSelf, WM_CONTEXTMENU,
10938 (WPARAM)infoPtr->hwndSelf, (LPARAM)GetMessagePos());
10942 return 0;
10945 /***
10946 * DESCRIPTION:
10947 * Sets the cursor.
10949 * PARAMETER(S):
10950 * [I] infoPtr : valid pointer to the listview structure
10951 * [I] hwnd : window handle of window containing the cursor
10952 * [I] nHittest : hit-test code
10953 * [I] wMouseMsg : ideintifier of the mouse message
10955 * RETURN:
10956 * TRUE if cursor is set
10957 * FALSE otherwise
10959 static BOOL LISTVIEW_SetCursor(const LISTVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
10961 LVHITTESTINFO lvHitTestInfo;
10963 if (!LISTVIEW_IsHotTracking(infoPtr)) goto forward;
10965 if (!infoPtr->hHotCursor) goto forward;
10967 GetCursorPos(&lvHitTestInfo.pt);
10968 if (LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, FALSE, FALSE) < 0) goto forward;
10970 SetCursor(infoPtr->hHotCursor);
10972 return TRUE;
10974 forward:
10976 return DefWindowProcW(infoPtr->hwndSelf, WM_SETCURSOR, wParam, lParam);
10979 /***
10980 * DESCRIPTION:
10981 * Sets the focus.
10983 * PARAMETER(S):
10984 * [I] infoPtr : valid pointer to the listview structure
10985 * [I] hwndLoseFocus : handle of previously focused window
10987 * RETURN:
10988 * Zero
10990 static LRESULT LISTVIEW_SetFocus(LISTVIEW_INFO *infoPtr, HWND hwndLoseFocus)
10992 TRACE("(hwndLoseFocus=%p)\n", hwndLoseFocus);
10994 /* if we have the focus already, there's nothing to do */
10995 if (infoPtr->bFocus) return 0;
10997 /* send NM_SETFOCUS notification */
10998 if (!notify(infoPtr, NM_SETFOCUS)) return 0;
11000 /* set window focus flag */
11001 infoPtr->bFocus = TRUE;
11003 /* put the focus rect back on */
11004 LISTVIEW_ShowFocusRect(infoPtr, TRUE);
11006 /* redraw all visible selected items */
11007 LISTVIEW_InvalidateSelectedItems(infoPtr);
11009 return 0;
11012 /***
11013 * DESCRIPTION:
11014 * Sets the font.
11016 * PARAMETER(S):
11017 * [I] infoPtr : valid pointer to the listview structure
11018 * [I] fRedraw : font handle
11019 * [I] fRedraw : redraw flag
11021 * RETURN:
11022 * Zero
11024 static LRESULT LISTVIEW_SetFont(LISTVIEW_INFO *infoPtr, HFONT hFont, WORD fRedraw)
11026 HFONT oldFont = infoPtr->hFont;
11027 INT oldHeight = infoPtr->nItemHeight;
11029 TRACE("(hfont=%p,redraw=%hu)\n", hFont, fRedraw);
11031 infoPtr->hFont = hFont ? hFont : infoPtr->hDefaultFont;
11032 if (infoPtr->hFont == oldFont) return 0;
11034 LISTVIEW_SaveTextMetrics(infoPtr);
11036 infoPtr->nItemHeight = LISTVIEW_CalculateItemHeight(infoPtr);
11038 if (infoPtr->uView == LV_VIEW_DETAILS)
11040 SendMessageW(infoPtr->hwndHeader, WM_SETFONT, (WPARAM)hFont, MAKELPARAM(fRedraw, 0));
11041 LISTVIEW_UpdateSize(infoPtr);
11042 LISTVIEW_UpdateScroll(infoPtr);
11044 else if (infoPtr->nItemHeight != oldHeight)
11045 LISTVIEW_UpdateScroll(infoPtr);
11047 if (fRedraw) LISTVIEW_InvalidateList(infoPtr);
11049 return 0;
11052 /***
11053 * DESCRIPTION:
11054 * Message handling for WM_SETREDRAW.
11055 * For the Listview, it invalidates the entire window (the doc specifies otherwise)
11057 * PARAMETER(S):
11058 * [I] infoPtr : valid pointer to the listview structure
11059 * [I] redraw: state of redraw flag
11061 * RETURN:
11062 * Zero.
11064 static LRESULT LISTVIEW_SetRedraw(LISTVIEW_INFO *infoPtr, BOOL redraw)
11066 TRACE("old=%d, new=%d\n", infoPtr->redraw, redraw);
11068 if (infoPtr->redraw == !!redraw)
11069 return 0;
11071 if (!(infoPtr->redraw = !!redraw))
11072 return 0;
11074 if (is_autoarrange(infoPtr))
11075 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
11076 LISTVIEW_UpdateScroll(infoPtr);
11078 /* despite what the WM_SETREDRAW docs says, apps expect us
11079 * to invalidate the listview here... stupid! */
11080 LISTVIEW_InvalidateList(infoPtr);
11082 return 0;
11085 /***
11086 * DESCRIPTION:
11087 * Resizes the listview control. This function processes WM_SIZE
11088 * messages. At this time, the width and height are not used.
11090 * PARAMETER(S):
11091 * [I] infoPtr : valid pointer to the listview structure
11092 * [I] Width : new width
11093 * [I] Height : new height
11095 * RETURN:
11096 * Zero
11098 static LRESULT LISTVIEW_Size(LISTVIEW_INFO *infoPtr, int Width, int Height)
11100 RECT rcOld = infoPtr->rcList;
11102 TRACE("(width=%d, height=%d)\n", Width, Height);
11104 LISTVIEW_UpdateSize(infoPtr);
11105 if (EqualRect(&rcOld, &infoPtr->rcList)) return 0;
11107 /* do not bother with display related stuff if we're not redrawing */
11108 if (!is_redrawing(infoPtr)) return 0;
11110 if (is_autoarrange(infoPtr))
11111 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
11113 LISTVIEW_UpdateScroll(infoPtr);
11115 /* refresh all only for lists whose height changed significantly */
11116 if ((infoPtr->uView == LV_VIEW_LIST) &&
11117 (rcOld.bottom - rcOld.top) / infoPtr->nItemHeight !=
11118 (infoPtr->rcList.bottom - infoPtr->rcList.top) / infoPtr->nItemHeight)
11119 LISTVIEW_InvalidateList(infoPtr);
11121 return 0;
11124 /***
11125 * DESCRIPTION:
11126 * Sets the size information.
11128 * PARAMETER(S):
11129 * [I] infoPtr : valid pointer to the listview structure
11131 * RETURN:
11132 * None
11134 static void LISTVIEW_UpdateSize(LISTVIEW_INFO *infoPtr)
11136 TRACE("uView %ld, rcList(old)=%s\n", infoPtr->uView, wine_dbgstr_rect(&infoPtr->rcList));
11138 GetClientRect(infoPtr->hwndSelf, &infoPtr->rcList);
11140 if (infoPtr->uView == LV_VIEW_LIST)
11142 /* Apparently the "LIST" style is supposed to have the same
11143 * number of items in a column even if there is no scroll bar.
11144 * Since if a scroll bar already exists then the bottom is already
11145 * reduced, only reduce if the scroll bar does not currently exist.
11146 * The "2" is there to mimic the native control. I think it may be
11147 * related to either padding or edges. (GLA 7/2002)
11149 if (!(GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE) & WS_HSCROLL))
11150 infoPtr->rcList.bottom -= GetSystemMetrics(SM_CYHSCROLL);
11151 infoPtr->rcList.bottom = max (infoPtr->rcList.bottom - 2, 0);
11154 /* When ListView control is created invisible, header isn't created right away. */
11155 if (infoPtr->hwndHeader)
11157 POINT origin;
11158 WINDOWPOS wp;
11159 HDLAYOUT hl;
11160 RECT rect;
11162 LISTVIEW_GetOrigin(infoPtr, &origin);
11164 rect = infoPtr->rcList;
11165 rect.left += origin.x;
11167 hl.prc = &rect;
11168 hl.pwpos = &wp;
11169 SendMessageW( infoPtr->hwndHeader, HDM_LAYOUT, 0, (LPARAM)&hl );
11170 TRACE(" wp.flags=0x%08x, wp=%d,%d (%dx%d)\n", wp.flags, wp.x, wp.y, wp.cx, wp.cy);
11172 if (LISTVIEW_IsHeaderEnabled(infoPtr))
11173 wp.flags |= SWP_SHOWWINDOW;
11174 else
11176 wp.flags |= SWP_HIDEWINDOW;
11177 wp.cy = 0;
11180 SetWindowPos(infoPtr->hwndHeader, wp.hwndInsertAfter, wp.x, wp.y, wp.cx, wp.cy, wp.flags);
11181 TRACE(" after SWP wp=%d,%d (%dx%d)\n", wp.x, wp.y, wp.cx, wp.cy);
11183 infoPtr->rcList.top = max(wp.cy, 0);
11185 /* extra padding for grid */
11186 if (infoPtr->uView == LV_VIEW_DETAILS && infoPtr->dwLvExStyle & LVS_EX_GRIDLINES)
11187 infoPtr->rcList.top += 2;
11189 TRACE(" rcList=%s\n", wine_dbgstr_rect(&infoPtr->rcList));
11192 /***
11193 * DESCRIPTION:
11194 * Processes WM_STYLECHANGED messages.
11196 * PARAMETER(S):
11197 * [I] infoPtr : valid pointer to the listview structure
11198 * [I] wStyleType : window style type (normal or extended)
11199 * [I] lpss : window style information
11201 * RETURN:
11202 * Zero
11204 static INT LISTVIEW_StyleChanged(LISTVIEW_INFO *infoPtr, WPARAM wStyleType,
11205 const STYLESTRUCT *lpss)
11207 UINT uNewView, uOldView;
11208 BOOL repaint = FALSE;
11209 UINT style;
11211 TRACE("styletype %Ix, styleOld %#lx, styleNew %#lx\n",
11212 wStyleType, lpss->styleOld, lpss->styleNew);
11214 if (wStyleType != GWL_STYLE || lpss->styleNew == infoPtr->dwStyle) return 0;
11216 infoPtr->dwStyle = lpss->styleNew;
11218 if (((lpss->styleOld & WS_HSCROLL) != 0)&&
11219 ((lpss->styleNew & WS_HSCROLL) == 0))
11220 ShowScrollBar(infoPtr->hwndSelf, SB_HORZ, FALSE);
11222 if (((lpss->styleOld & WS_VSCROLL) != 0)&&
11223 ((lpss->styleNew & WS_VSCROLL) == 0))
11224 ShowScrollBar(infoPtr->hwndSelf, SB_VERT, FALSE);
11226 uNewView = lpss->styleNew & LVS_TYPEMASK;
11227 uOldView = lpss->styleOld & LVS_TYPEMASK;
11229 if (uNewView != uOldView)
11231 HIMAGELIST himl;
11233 repaint = TRUE;
11235 /* LVM_SETVIEW doesn't change window style bits within LVS_TYPEMASK,
11236 changing style updates current view only when view bits change. */
11237 map_style_view(infoPtr);
11238 SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
11239 ShowWindow(infoPtr->hwndHeader, SW_HIDE);
11241 ShowScrollBar(infoPtr->hwndSelf, SB_BOTH, FALSE);
11242 SetRectEmpty(&infoPtr->rcFocus);
11244 himl = (uNewView == LVS_ICON ? infoPtr->himlNormal : infoPtr->himlSmall);
11245 set_icon_size(&infoPtr->iconSize, himl, uNewView != LVS_ICON);
11247 if (uNewView == LVS_REPORT)
11249 HDLAYOUT hl;
11250 WINDOWPOS wp;
11252 LISTVIEW_CreateHeader( infoPtr );
11254 hl.prc = &infoPtr->rcList;
11255 hl.pwpos = &wp;
11256 SendMessageW( infoPtr->hwndHeader, HDM_LAYOUT, 0, (LPARAM)&hl );
11257 SetWindowPos(infoPtr->hwndHeader, infoPtr->hwndSelf, wp.x, wp.y, wp.cx, wp.cy,
11258 wp.flags | ((infoPtr->dwStyle & LVS_NOCOLUMNHEADER)
11259 ? SWP_HIDEWINDOW : SWP_SHOWWINDOW));
11262 LISTVIEW_UpdateItemSize(infoPtr);
11265 if (uNewView == LVS_REPORT || infoPtr->dwLvExStyle & LVS_EX_HEADERINALLVIEWS)
11267 if ((lpss->styleOld ^ lpss->styleNew) & LVS_NOCOLUMNHEADER)
11269 if (lpss->styleNew & LVS_NOCOLUMNHEADER)
11271 /* Turn off the header control */
11272 style = GetWindowLongW(infoPtr->hwndHeader, GWL_STYLE);
11273 TRACE("Hide header control, was 0x%08x\n", style);
11274 SetWindowLongW(infoPtr->hwndHeader, GWL_STYLE, style | HDS_HIDDEN);
11275 } else {
11276 /* Turn on the header control */
11277 if ((style = GetWindowLongW(infoPtr->hwndHeader, GWL_STYLE)) & HDS_HIDDEN)
11279 TRACE("Show header control, was 0x%08x\n", style);
11280 SetWindowLongW(infoPtr->hwndHeader, GWL_STYLE, (style & ~HDS_HIDDEN) | WS_VISIBLE);
11286 if ( (uNewView == LVS_ICON || uNewView == LVS_SMALLICON) &&
11287 (uNewView != uOldView || ((lpss->styleNew ^ lpss->styleOld) & LVS_ALIGNMASK)) )
11288 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
11290 /* update the size of the client area */
11291 LISTVIEW_UpdateSize(infoPtr);
11293 /* add scrollbars if needed */
11294 LISTVIEW_UpdateScroll(infoPtr);
11296 if (repaint)
11297 LISTVIEW_InvalidateList(infoPtr);
11299 return 0;
11302 /***
11303 * DESCRIPTION:
11304 * Processes WM_STYLECHANGING messages.
11306 * PARAMETER(S):
11307 * [I] wStyleType : window style type (normal or extended)
11308 * [I0] lpss : window style information
11310 * RETURN:
11311 * Zero
11313 static INT LISTVIEW_StyleChanging(WPARAM wStyleType,
11314 STYLESTRUCT *lpss)
11316 TRACE("styletype %Ix, styleOld %#lx, styleNew %#lx\n",
11317 wStyleType, lpss->styleOld, lpss->styleNew);
11319 /* don't forward LVS_OWNERDATA only if not already set to */
11320 if ((lpss->styleNew ^ lpss->styleOld) & LVS_OWNERDATA)
11322 if (lpss->styleOld & LVS_OWNERDATA)
11323 lpss->styleNew |= LVS_OWNERDATA;
11324 else
11325 lpss->styleNew &= ~LVS_OWNERDATA;
11328 return 0;
11331 /***
11332 * DESCRIPTION:
11333 * Processes WM_SHOWWINDOW messages.
11335 * PARAMETER(S):
11336 * [I] infoPtr : valid pointer to the listview structure
11337 * [I] bShown : window is being shown (FALSE when hidden)
11338 * [I] iStatus : window show status
11340 * RETURN:
11341 * Zero
11343 static LRESULT LISTVIEW_ShowWindow(LISTVIEW_INFO *infoPtr, WPARAM bShown, LPARAM iStatus)
11345 /* header delayed creation */
11346 if ((infoPtr->uView == LV_VIEW_DETAILS) && bShown)
11348 LISTVIEW_CreateHeader(infoPtr);
11350 if (!(LVS_NOCOLUMNHEADER & infoPtr->dwStyle))
11351 ShowWindow(infoPtr->hwndHeader, SW_SHOWNORMAL);
11354 return DefWindowProcW(infoPtr->hwndSelf, WM_SHOWWINDOW, bShown, iStatus);
11357 /***
11358 * DESCRIPTION:
11359 * Processes CCM_GETVERSION messages.
11361 * PARAMETER(S):
11362 * [I] infoPtr : valid pointer to the listview structure
11364 * RETURN:
11365 * Current version
11367 static inline LRESULT LISTVIEW_GetVersion(const LISTVIEW_INFO *infoPtr)
11369 return infoPtr->iVersion;
11372 /***
11373 * DESCRIPTION:
11374 * Processes CCM_SETVERSION messages.
11376 * PARAMETER(S):
11377 * [I] infoPtr : valid pointer to the listview structure
11378 * [I] iVersion : version to be set
11380 * RETURN:
11381 * -1 when requested version is greater than DLL version;
11382 * previous version otherwise
11384 static LRESULT LISTVIEW_SetVersion(LISTVIEW_INFO *infoPtr, DWORD iVersion)
11386 INT iOldVersion = infoPtr->iVersion;
11388 if (iVersion > COMCTL32_VERSION)
11389 return -1;
11391 infoPtr->iVersion = iVersion;
11393 TRACE("new version %ld\n", iVersion);
11395 return iOldVersion;
11398 /***
11399 * DESCRIPTION:
11400 * Window procedure of the listview control.
11403 static LRESULT WINAPI
11404 LISTVIEW_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
11406 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongPtrW(hwnd, 0);
11408 TRACE("hwnd %p, uMsg %x, wParam %Ix, lParam %Ix\n", hwnd, uMsg, wParam, lParam);
11410 if (!infoPtr && (uMsg != WM_NCCREATE))
11411 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
11413 switch (uMsg)
11415 case LVM_APPROXIMATEVIEWRECT:
11416 return LISTVIEW_ApproximateViewRect(infoPtr, (INT)wParam,
11417 LOWORD(lParam), HIWORD(lParam));
11418 case LVM_ARRANGE:
11419 return LISTVIEW_Arrange(infoPtr, (INT)wParam);
11421 case LVM_CANCELEDITLABEL:
11422 return LISTVIEW_CancelEditLabel(infoPtr);
11424 case LVM_CREATEDRAGIMAGE:
11425 return (LRESULT)LISTVIEW_CreateDragImage(infoPtr, (INT)wParam, (LPPOINT)lParam);
11427 case LVM_DELETEALLITEMS:
11428 return LISTVIEW_DeleteAllItems(infoPtr, FALSE);
11430 case LVM_DELETECOLUMN:
11431 return LISTVIEW_DeleteColumn(infoPtr, (INT)wParam);
11433 case LVM_DELETEITEM:
11434 return LISTVIEW_DeleteItem(infoPtr, (INT)wParam);
11436 case LVM_EDITLABELA:
11437 case LVM_EDITLABELW:
11438 return (LRESULT)LISTVIEW_EditLabelT(infoPtr, (INT)wParam,
11439 uMsg == LVM_EDITLABELW);
11440 /* case LVM_ENABLEGROUPVIEW: */
11442 case LVM_ENSUREVISIBLE:
11443 return LISTVIEW_EnsureVisible(infoPtr, (INT)wParam, (BOOL)lParam);
11445 case LVM_FINDITEMW:
11446 return LISTVIEW_FindItemW(infoPtr, (INT)wParam, (LPLVFINDINFOW)lParam);
11448 case LVM_FINDITEMA:
11449 return LISTVIEW_FindItemA(infoPtr, (INT)wParam, (LPLVFINDINFOA)lParam);
11451 case LVM_GETBKCOLOR:
11452 return infoPtr->clrBk;
11454 /* case LVM_GETBKIMAGE: */
11456 case LVM_GETCALLBACKMASK:
11457 return infoPtr->uCallbackMask;
11459 case LVM_GETCOLUMNA:
11460 case LVM_GETCOLUMNW:
11461 return LISTVIEW_GetColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam,
11462 uMsg == LVM_GETCOLUMNW);
11464 case LVM_GETCOLUMNORDERARRAY:
11465 return LISTVIEW_GetColumnOrderArray(infoPtr, (INT)wParam, (LPINT)lParam);
11467 case LVM_GETCOLUMNWIDTH:
11468 return LISTVIEW_GetColumnWidth(infoPtr, (INT)wParam);
11470 case LVM_GETCOUNTPERPAGE:
11471 return LISTVIEW_GetCountPerPage(infoPtr);
11473 case LVM_GETEDITCONTROL:
11474 return (LRESULT)infoPtr->hwndEdit;
11476 case LVM_GETEXTENDEDLISTVIEWSTYLE:
11477 return infoPtr->dwLvExStyle;
11479 /* case LVM_GETGROUPINFO: */
11481 /* case LVM_GETGROUPMETRICS: */
11483 case LVM_GETHEADER:
11484 return (LRESULT)infoPtr->hwndHeader;
11486 case LVM_GETHOTCURSOR:
11487 return (LRESULT)infoPtr->hHotCursor;
11489 case LVM_GETHOTITEM:
11490 return infoPtr->nHotItem;
11492 case LVM_GETHOVERTIME:
11493 return infoPtr->dwHoverTime;
11495 case LVM_GETIMAGELIST:
11496 return (LRESULT)LISTVIEW_GetImageList(infoPtr, (INT)wParam);
11498 /* case LVM_GETINSERTMARK: */
11500 /* case LVM_GETINSERTMARKCOLOR: */
11502 /* case LVM_GETINSERTMARKRECT: */
11504 case LVM_GETISEARCHSTRINGA:
11505 case LVM_GETISEARCHSTRINGW:
11506 FIXME("LVM_GETISEARCHSTRING: unimplemented\n");
11507 return FALSE;
11509 case LVM_GETITEMA:
11510 case LVM_GETITEMW:
11511 return LISTVIEW_GetItemExtT(infoPtr, (LPLVITEMW)lParam, uMsg == LVM_GETITEMW);
11513 case LVM_GETITEMCOUNT:
11514 return infoPtr->nItemCount;
11516 case LVM_GETITEMPOSITION:
11517 return LISTVIEW_GetItemPosition(infoPtr, (INT)wParam, (LPPOINT)lParam);
11519 case LVM_GETITEMRECT:
11520 return LISTVIEW_GetItemRect(infoPtr, (INT)wParam, (LPRECT)lParam);
11522 case LVM_GETITEMSPACING:
11523 return LISTVIEW_GetItemSpacing(infoPtr, (BOOL)wParam);
11525 case LVM_GETITEMSTATE:
11526 return LISTVIEW_GetItemState(infoPtr, (INT)wParam, (UINT)lParam);
11528 case LVM_GETITEMTEXTA:
11529 case LVM_GETITEMTEXTW:
11530 return LISTVIEW_GetItemTextT(infoPtr, (INT)wParam, (LPLVITEMW)lParam,
11531 uMsg == LVM_GETITEMTEXTW);
11533 case LVM_GETNEXTITEM:
11534 return LISTVIEW_GetNextItem(infoPtr, (INT)wParam, LOWORD(lParam));
11536 case LVM_GETNEXTITEMINDEX:
11537 return LISTVIEW_GetNextItemIndex(infoPtr, (LVITEMINDEX *)wParam, lParam);
11539 case LVM_GETNUMBEROFWORKAREAS:
11540 FIXME("LVM_GETNUMBEROFWORKAREAS: unimplemented\n");
11541 return 1;
11543 case LVM_GETORIGIN:
11544 if (!lParam) return FALSE;
11545 if (infoPtr->uView == LV_VIEW_DETAILS ||
11546 infoPtr->uView == LV_VIEW_LIST) return FALSE;
11547 LISTVIEW_GetOrigin(infoPtr, (LPPOINT)lParam);
11548 return TRUE;
11550 /* case LVM_GETOUTLINECOLOR: */
11552 case LVM_GETSELECTEDCOLUMN:
11553 return infoPtr->selected_column;
11555 case LVM_GETSELECTEDCOUNT:
11556 return LISTVIEW_GetSelectedCount(infoPtr);
11558 case LVM_GETSELECTIONMARK:
11559 return infoPtr->nSelectionMark;
11561 case LVM_GETSTRINGWIDTHA:
11562 case LVM_GETSTRINGWIDTHW:
11563 return LISTVIEW_GetStringWidthT(infoPtr, (LPCWSTR)lParam,
11564 uMsg == LVM_GETSTRINGWIDTHW);
11566 case LVM_GETSUBITEMRECT:
11567 return LISTVIEW_GetSubItemRect(infoPtr, (UINT)wParam, (LPRECT)lParam);
11569 case LVM_GETTEXTBKCOLOR:
11570 return infoPtr->clrTextBk;
11572 case LVM_GETTEXTCOLOR:
11573 return infoPtr->clrText;
11575 /* case LVM_GETTILEINFO: */
11577 /* case LVM_GETTILEVIEWINFO: */
11579 case LVM_GETTOOLTIPS:
11580 if( !infoPtr->hwndToolTip )
11581 infoPtr->hwndToolTip = COMCTL32_CreateToolTip( hwnd );
11582 return (LRESULT)infoPtr->hwndToolTip;
11584 case LVM_GETTOPINDEX:
11585 return LISTVIEW_GetTopIndex(infoPtr);
11587 case LVM_GETUNICODEFORMAT:
11588 return (infoPtr->notifyFormat == NFR_UNICODE);
11590 case LVM_GETVIEW:
11591 return infoPtr->uView;
11593 case LVM_GETVIEWRECT:
11594 return LISTVIEW_GetViewRect(infoPtr, (LPRECT)lParam);
11596 case LVM_GETWORKAREAS:
11597 FIXME("LVM_GETWORKAREAS: unimplemented\n");
11598 return FALSE;
11600 /* case LVM_HASGROUP: */
11602 case LVM_HITTEST:
11603 return LISTVIEW_HitTest(infoPtr, (LPLVHITTESTINFO)lParam, FALSE, TRUE);
11605 case LVM_INSERTCOLUMNA:
11606 case LVM_INSERTCOLUMNW:
11607 return LISTVIEW_InsertColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam,
11608 uMsg == LVM_INSERTCOLUMNW);
11610 /* case LVM_INSERTGROUP: */
11612 /* case LVM_INSERTGROUPSORTED: */
11614 case LVM_INSERTITEMA:
11615 case LVM_INSERTITEMW:
11616 return LISTVIEW_InsertItemT(infoPtr, (LPLVITEMW)lParam, uMsg == LVM_INSERTITEMW);
11618 /* case LVM_INSERTMARKHITTEST: */
11620 /* case LVM_ISGROUPVIEWENABLED: */
11622 case LVM_ISITEMVISIBLE:
11623 return LISTVIEW_IsItemVisible(infoPtr, (INT)wParam);
11625 case LVM_MAPIDTOINDEX:
11626 return LISTVIEW_MapIdToIndex(infoPtr, (UINT)wParam);
11628 case LVM_MAPINDEXTOID:
11629 return LISTVIEW_MapIndexToId(infoPtr, (INT)wParam);
11631 /* case LVM_MOVEGROUP: */
11633 /* case LVM_MOVEITEMTOGROUP: */
11635 case LVM_REDRAWITEMS:
11636 return LISTVIEW_RedrawItems(infoPtr, (INT)wParam, (INT)lParam);
11638 /* case LVM_REMOVEALLGROUPS: */
11640 /* case LVM_REMOVEGROUP: */
11642 case LVM_SCROLL:
11643 return LISTVIEW_Scroll(infoPtr, (INT)wParam, (INT)lParam);
11645 case LVM_SETBKCOLOR:
11646 return LISTVIEW_SetBkColor(infoPtr, (COLORREF)lParam);
11648 case LVM_SETBKIMAGEA:
11649 case LVM_SETBKIMAGEW:
11650 return LISTVIEW_SetBkImage(infoPtr, (LVBKIMAGEW *)lParam, uMsg == LVM_SETBKIMAGEW);
11652 case LVM_SETCALLBACKMASK:
11653 infoPtr->uCallbackMask = (UINT)wParam;
11654 return TRUE;
11656 case LVM_SETCOLUMNA:
11657 case LVM_SETCOLUMNW:
11658 return LISTVIEW_SetColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam,
11659 uMsg == LVM_SETCOLUMNW);
11661 case LVM_SETCOLUMNORDERARRAY:
11662 return LISTVIEW_SetColumnOrderArray(infoPtr, (INT)wParam, (LPINT)lParam);
11664 case LVM_SETCOLUMNWIDTH:
11665 return LISTVIEW_SetColumnWidth(infoPtr, (INT)wParam, (short)LOWORD(lParam));
11667 case LVM_SETEXTENDEDLISTVIEWSTYLE:
11668 return LISTVIEW_SetExtendedListViewStyle(infoPtr, (DWORD)wParam, (DWORD)lParam);
11670 /* case LVM_SETGROUPINFO: */
11672 /* case LVM_SETGROUPMETRICS: */
11674 case LVM_SETHOTCURSOR:
11675 return (LRESULT)LISTVIEW_SetHotCursor(infoPtr, (HCURSOR)lParam);
11677 case LVM_SETHOTITEM:
11678 return LISTVIEW_SetHotItem(infoPtr, (INT)wParam);
11680 case LVM_SETHOVERTIME:
11681 return LISTVIEW_SetHoverTime(infoPtr, (DWORD)lParam);
11683 case LVM_SETICONSPACING:
11684 if(lParam == -1)
11685 return LISTVIEW_SetIconSpacing(infoPtr, -1, -1);
11686 return LISTVIEW_SetIconSpacing(infoPtr, LOWORD(lParam), HIWORD(lParam));
11688 case LVM_SETIMAGELIST:
11689 return (LRESULT)LISTVIEW_SetImageList(infoPtr, (INT)wParam, (HIMAGELIST)lParam);
11691 /* case LVM_SETINFOTIP: */
11693 /* case LVM_SETINSERTMARK: */
11695 /* case LVM_SETINSERTMARKCOLOR: */
11697 case LVM_SETITEMA:
11698 case LVM_SETITEMW:
11700 if (infoPtr->dwStyle & LVS_OWNERDATA) return FALSE;
11701 return LISTVIEW_SetItemT(infoPtr, (LPLVITEMW)lParam, (uMsg == LVM_SETITEMW));
11704 case LVM_SETITEMCOUNT:
11705 return LISTVIEW_SetItemCount(infoPtr, (INT)wParam, (DWORD)lParam);
11707 case LVM_SETITEMPOSITION:
11709 POINT pt;
11710 pt.x = (short)LOWORD(lParam);
11711 pt.y = (short)HIWORD(lParam);
11712 return LISTVIEW_SetItemPosition(infoPtr, (INT)wParam, &pt);
11715 case LVM_SETITEMPOSITION32:
11716 return LISTVIEW_SetItemPosition(infoPtr, (INT)wParam, (POINT*)lParam);
11718 case LVM_SETITEMSTATE:
11719 return LISTVIEW_SetItemState(infoPtr, (INT)wParam, (LPLVITEMW)lParam);
11721 case LVM_SETITEMTEXTA:
11722 case LVM_SETITEMTEXTW:
11723 return LISTVIEW_SetItemTextT(infoPtr, (INT)wParam, (LPLVITEMW)lParam,
11724 uMsg == LVM_SETITEMTEXTW);
11726 /* case LVM_SETOUTLINECOLOR: */
11728 case LVM_SETSELECTEDCOLUMN:
11729 infoPtr->selected_column = (INT)wParam;
11730 return TRUE;
11732 case LVM_SETSELECTIONMARK:
11733 return LISTVIEW_SetSelectionMark(infoPtr, (INT)lParam);
11735 case LVM_SETTEXTBKCOLOR:
11736 return LISTVIEW_SetTextBkColor(infoPtr, (COLORREF)lParam);
11738 case LVM_SETTEXTCOLOR:
11739 return LISTVIEW_SetTextColor(infoPtr, (COLORREF)lParam);
11741 /* case LVM_SETTILEINFO: */
11743 /* case LVM_SETTILEVIEWINFO: */
11745 /* case LVM_SETTILEWIDTH: */
11747 case LVM_SETTOOLTIPS:
11748 return (LRESULT)LISTVIEW_SetToolTips(infoPtr, (HWND)lParam);
11750 case LVM_SETUNICODEFORMAT:
11751 return LISTVIEW_SetUnicodeFormat(infoPtr, wParam);
11753 case LVM_SETVIEW:
11754 return LISTVIEW_SetView(infoPtr, wParam);
11756 /* case LVM_SETWORKAREAS: */
11758 /* case LVM_SORTGROUPS: */
11760 case LVM_SORTITEMS:
11761 case LVM_SORTITEMSEX:
11762 return LISTVIEW_SortItems(infoPtr, (PFNLVCOMPARE)lParam, wParam,
11763 uMsg == LVM_SORTITEMSEX);
11764 case LVM_SUBITEMHITTEST:
11765 return LISTVIEW_HitTest(infoPtr, (LPLVHITTESTINFO)lParam, TRUE, FALSE);
11767 case LVM_UPDATE:
11768 return LISTVIEW_Update(infoPtr, (INT)wParam);
11770 case CCM_GETVERSION:
11771 return LISTVIEW_GetVersion(infoPtr);
11773 case CCM_SETVERSION:
11774 return LISTVIEW_SetVersion(infoPtr, wParam);
11776 case WM_CHAR:
11777 return LISTVIEW_ProcessLetterKeys( infoPtr, wParam, lParam );
11779 case WM_COMMAND:
11780 return LISTVIEW_Command(infoPtr, wParam, lParam);
11782 case WM_NCCREATE:
11783 return LISTVIEW_NCCreate(hwnd, wParam, (LPCREATESTRUCTW)lParam);
11785 case WM_CREATE:
11786 return LISTVIEW_Create(hwnd, (LPCREATESTRUCTW)lParam);
11788 case WM_DESTROY:
11789 return LISTVIEW_Destroy(infoPtr);
11791 case WM_ENABLE:
11792 return LISTVIEW_Enable(infoPtr);
11794 case WM_ERASEBKGND:
11795 return LISTVIEW_EraseBkgnd(infoPtr, (HDC)wParam);
11797 case WM_GETDLGCODE:
11798 return DLGC_WANTCHARS | DLGC_WANTARROWS;
11800 case WM_GETFONT:
11801 return (LRESULT)infoPtr->hFont;
11803 case WM_HSCROLL:
11804 return LISTVIEW_HScroll(infoPtr, (INT)LOWORD(wParam), 0);
11806 case WM_KEYDOWN:
11807 return LISTVIEW_KeyDown(infoPtr, (INT)wParam, (LONG)lParam);
11809 case WM_KILLFOCUS:
11810 return LISTVIEW_KillFocus(infoPtr);
11812 case WM_LBUTTONDBLCLK:
11813 return LISTVIEW_LButtonDblClk(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11815 case WM_LBUTTONDOWN:
11816 return LISTVIEW_LButtonDown(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11818 case WM_LBUTTONUP:
11819 return LISTVIEW_LButtonUp(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11821 case WM_MOUSEMOVE:
11822 return LISTVIEW_MouseMove (infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11824 case WM_MOUSEHOVER:
11825 return LISTVIEW_MouseHover(infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11827 case WM_NCDESTROY:
11828 return LISTVIEW_NCDestroy(infoPtr);
11830 case WM_NCPAINT:
11831 return LISTVIEW_NCPaint(infoPtr, (HRGN)wParam);
11833 case WM_NOTIFY:
11834 return LISTVIEW_Notify(infoPtr, (LPNMHDR)lParam);
11836 case WM_NOTIFYFORMAT:
11837 return LISTVIEW_NotifyFormat(infoPtr, (HWND)wParam, (INT)lParam);
11839 case WM_PRINTCLIENT:
11840 return LISTVIEW_PrintClient(infoPtr, (HDC)wParam, (DWORD)lParam);
11842 case WM_PAINT:
11843 return LISTVIEW_WMPaint(infoPtr, (HDC)wParam);
11845 case WM_RBUTTONDBLCLK:
11846 return LISTVIEW_RButtonDblClk(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11848 case WM_RBUTTONDOWN:
11849 return LISTVIEW_RButtonDown(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11851 case WM_SETCURSOR:
11852 return LISTVIEW_SetCursor(infoPtr, wParam, lParam);
11854 case WM_SETFOCUS:
11855 return LISTVIEW_SetFocus(infoPtr, (HWND)wParam);
11857 case WM_SETFONT:
11858 return LISTVIEW_SetFont(infoPtr, (HFONT)wParam, (WORD)lParam);
11860 case WM_SETREDRAW:
11861 return LISTVIEW_SetRedraw(infoPtr, (BOOL)wParam);
11863 case WM_SHOWWINDOW:
11864 return LISTVIEW_ShowWindow(infoPtr, wParam, lParam);
11866 case WM_STYLECHANGED:
11867 return LISTVIEW_StyleChanged(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
11869 case WM_STYLECHANGING:
11870 return LISTVIEW_StyleChanging(wParam, (LPSTYLESTRUCT)lParam);
11872 case WM_SYSCOLORCHANGE:
11873 COMCTL32_RefreshSysColors();
11874 return 0;
11876 /* case WM_TIMER: */
11877 case WM_THEMECHANGED:
11878 return LISTVIEW_ThemeChanged(infoPtr);
11880 case WM_VSCROLL:
11881 return LISTVIEW_VScroll(infoPtr, (INT)LOWORD(wParam), 0);
11883 case WM_MOUSEWHEEL:
11884 if (wParam & (MK_SHIFT | MK_CONTROL))
11885 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
11886 return LISTVIEW_MouseWheel(infoPtr, (short int)HIWORD(wParam));
11888 case WM_WINDOWPOSCHANGED:
11889 if (!(((WINDOWPOS *)lParam)->flags & SWP_NOSIZE))
11891 SetWindowPos(infoPtr->hwndSelf, 0, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOACTIVATE |
11892 SWP_NOZORDER | SWP_NOMOVE | SWP_NOSIZE);
11894 if ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (infoPtr->uView == LV_VIEW_DETAILS))
11896 if (notify_measureitem(infoPtr)) LISTVIEW_InvalidateList(infoPtr);
11898 LISTVIEW_Size(infoPtr, ((WINDOWPOS *)lParam)->cx, ((WINDOWPOS *)lParam)->cy);
11900 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
11902 /* case WM_WININICHANGE: */
11904 default:
11905 if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
11906 ERR("unknown msg %04x, wp %Ix, lp %Ix\n", uMsg, wParam, lParam);
11908 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
11913 /***
11914 * DESCRIPTION:
11915 * Registers the window class.
11917 * PARAMETER(S):
11918 * None
11920 * RETURN:
11921 * None
11923 void LISTVIEW_Register(void)
11925 WNDCLASSW wndClass;
11927 ZeroMemory(&wndClass, sizeof(WNDCLASSW));
11928 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS;
11929 wndClass.lpfnWndProc = LISTVIEW_WindowProc;
11930 wndClass.cbClsExtra = 0;
11931 wndClass.cbWndExtra = sizeof(LISTVIEW_INFO *);
11932 wndClass.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
11933 wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
11934 wndClass.lpszClassName = WC_LISTVIEWW;
11935 RegisterClassW(&wndClass);
11938 /***
11939 * DESCRIPTION:
11940 * Unregisters the window class.
11942 * PARAMETER(S):
11943 * None
11945 * RETURN:
11946 * None
11948 void LISTVIEW_Unregister(void)
11950 UnregisterClassW(WC_LISTVIEWW, NULL);
11953 /***
11954 * DESCRIPTION:
11955 * Handle any WM_COMMAND messages
11957 * PARAMETER(S):
11958 * [I] infoPtr : valid pointer to the listview structure
11959 * [I] wParam : the first message parameter
11960 * [I] lParam : the second message parameter
11962 * RETURN:
11963 * Zero.
11965 static LRESULT LISTVIEW_Command(LISTVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
11968 TRACE("%p, %x, %x, %Ix\n", infoPtr, HIWORD(wParam), LOWORD(wParam), lParam);
11970 if (!infoPtr->hwndEdit) return 0;
11972 switch (HIWORD(wParam))
11974 case EN_UPDATE:
11977 * Adjust the edit window size
11979 WCHAR buffer[1024];
11980 HDC hdc = GetDC(infoPtr->hwndEdit);
11981 HFONT hFont, hOldFont = 0;
11982 RECT rect;
11983 SIZE sz;
11985 if (!infoPtr->hwndEdit || !hdc) return 0;
11986 GetWindowTextW(infoPtr->hwndEdit, buffer, ARRAY_SIZE(buffer));
11987 GetWindowRect(infoPtr->hwndEdit, &rect);
11989 /* Select font to get the right dimension of the string */
11990 hFont = (HFONT)SendMessageW(infoPtr->hwndEdit, WM_GETFONT, 0, 0);
11991 if (hFont)
11993 hOldFont = SelectObject(hdc, hFont);
11996 if (GetTextExtentPoint32W(hdc, buffer, lstrlenW(buffer), &sz))
11998 TEXTMETRICW textMetric;
12000 /* Add Extra spacing for the next character */
12001 GetTextMetricsW(hdc, &textMetric);
12002 sz.cx += (textMetric.tmMaxCharWidth * 2);
12004 SetWindowPos(infoPtr->hwndEdit, NULL, 0, 0, sz.cx,
12005 rect.bottom - rect.top, SWP_DRAWFRAME | SWP_NOMOVE | SWP_NOZORDER);
12007 if (hFont)
12008 SelectObject(hdc, hOldFont);
12010 ReleaseDC(infoPtr->hwndEdit, hdc);
12012 break;
12014 case EN_KILLFOCUS:
12016 if (infoPtr->notify_mask & NOTIFY_MASK_END_LABEL_EDIT)
12017 LISTVIEW_CancelEditLabel(infoPtr);
12018 break;
12021 default:
12022 return SendMessageW (infoPtr->hwndNotify, WM_COMMAND, wParam, lParam);
12025 return 0;