winex11: Add window data structure locking to the X window creation paths.
[wine.git] / dlls / comctl32 / listview.c
blob7a11557439ad5a004082a3dd5d972e9a79834e98
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-2012 Nikolay Sivov
10 * Copyright 2009 Owen Rudge for CodeWeavers
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; either
15 * version 2.1 of the License, or (at your option) any later version.
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 * NOTES
28 * This code was audited for completeness against the documented features
29 * of Comctl32.dll version 6.0 on May. 20, 2005, by James Hawkins.
31 * Unless otherwise noted, we believe this code to be complete, as per
32 * the specification mentioned above.
33 * If you discover missing features, or bugs, please note them below.
35 * TODO:
37 * Default Message Processing
38 * -- WM_CREATE: create the icon and small icon image lists at this point only if
39 * the LVS_SHAREIMAGELISTS style is not specified.
40 * -- WM_WINDOWPOSCHANGED: arrange the list items if the current view is icon
41 * or small icon and the LVS_AUTOARRANGE style is specified.
42 * -- WM_TIMER
43 * -- WM_WININICHANGE
45 * Features
46 * -- Hot item handling, mouse hovering
47 * -- Workareas support
48 * -- Tilemode support
49 * -- Groups support
51 * Bugs
52 * -- Expand large item in ICON mode when the cursor is flying over the icon or text.
53 * -- Support CustomDraw options for _WIN32_IE >= 0x560 (see NMLVCUSTOMDRAW docs).
54 * -- LVA_SNAPTOGRID not implemented
55 * -- LISTVIEW_ApproximateViewRect partially implemented
56 * -- LISTVIEW_SetColumnWidth ignores header images & bitmap
57 * -- LISTVIEW_SetIconSpacing is incomplete
58 * -- LISTVIEW_StyleChanged doesn't handle some changes too well
60 * Speedups
61 * -- LISTVIEW_GetNextItem needs to be rewritten. It is currently
62 * linear in the number of items in the list, and this is
63 * unacceptable for large lists.
64 * -- if list is sorted by item text LISTVIEW_InsertItemT could use
65 * binary search to calculate item index (e.g. DPA_Search()).
66 * This requires sorted state to be reliably tracked in item modifiers.
67 * -- we should keep an ordered array of coordinates in iconic mode
68 * this would allow to frame items (iterator_frameditems),
69 * and find nearest item (LVFI_NEARESTXY) a lot more efficiently
71 * Flags
72 * -- LVIF_COLUMNS
73 * -- LVIF_GROUPID
75 * States
76 * -- LVIS_ACTIVATING (not currently supported by comctl32.dll version 6.0)
77 * -- LVIS_DROPHILITED
78 * -- LVIS_OVERLAYMASK
80 * Styles
81 * -- LVS_NOLABELWRAP
82 * -- LVS_NOSCROLL (see Q137520)
83 * -- LVS_ALIGNTOP
85 * Extended Styles
86 * -- LVS_EX_BORDERSELECT
87 * -- LVS_EX_FLATSB
88 * -- LVS_EX_INFOTIP
89 * -- LVS_EX_LABELTIP
90 * -- LVS_EX_MULTIWORKAREAS
91 * -- LVS_EX_REGIONAL
92 * -- LVS_EX_SIMPLESELECT
93 * -- LVS_EX_TWOCLICKACTIVATE
94 * -- LVS_EX_UNDERLINECOLD
95 * -- LVS_EX_UNDERLINEHOT
97 * Notifications:
98 * -- LVN_BEGINSCROLL, LVN_ENDSCROLL
99 * -- LVN_GETINFOTIP
100 * -- LVN_HOTTRACK
101 * -- LVN_SETDISPINFO
102 * -- LVN_BEGINRDRAG
104 * Messages:
105 * -- LVM_ENABLEGROUPVIEW
106 * -- LVM_GETBKIMAGE, LVM_SETBKIMAGE
107 * -- LVM_GETGROUPINFO, LVM_SETGROUPINFO
108 * -- LVM_GETGROUPMETRICS, LVM_SETGROUPMETRICS
109 * -- LVM_GETINSERTMARK, LVM_SETINSERTMARK
110 * -- LVM_GETINSERTMARKCOLOR, LVM_SETINSERTMARKCOLOR
111 * -- LVM_GETINSERTMARKRECT
112 * -- LVM_GETNUMBEROFWORKAREAS
113 * -- LVM_GETOUTLINECOLOR, LVM_SETOUTLINECOLOR
114 * -- LVM_GETSELECTEDCOLUMN, LVM_SETSELECTEDCOLUMN
115 * -- LVM_GETISEARCHSTRINGW, LVM_GETISEARCHSTRINGA
116 * -- LVM_GETTILEINFO, LVM_SETTILEINFO
117 * -- LVM_GETTILEVIEWINFO, LVM_SETTILEVIEWINFO
118 * -- LVM_GETWORKAREAS, LVM_SETWORKAREAS
119 * -- LVM_HASGROUP, LVM_INSERTGROUP, LVM_REMOVEGROUP, LVM_REMOVEALLGROUPS
120 * -- LVM_INSERTGROUPSORTED
121 * -- LVM_INSERTMARKHITTEST
122 * -- LVM_ISGROUPVIEWENABLED
123 * -- LVM_MOVEGROUP
124 * -- LVM_MOVEITEMTOGROUP
125 * -- LVM_SETINFOTIP
126 * -- LVM_SETTILEWIDTH
127 * -- LVM_SORTGROUPS
129 * Macros:
130 * -- ListView_GetHoverTime, ListView_SetHoverTime
131 * -- ListView_GetISearchString
132 * -- ListView_GetNumberOfWorkAreas
133 * -- ListView_GetWorkAreas, ListView_SetWorkAreas
135 * Functions:
136 * -- LVGroupComparE
138 * Known differences in message stream from native control (not known if
139 * these differences cause problems):
140 * LVM_INSERTITEM issues LVM_SETITEMSTATE and LVM_SETITEM in certain cases.
141 * LVM_SETITEM does not always issue LVN_ITEMCHANGING/LVN_ITEMCHANGED.
142 * WM_CREATE does not issue WM_QUERYUISTATE and associated registry
143 * processing for "USEDOUBLECLICKTIME".
146 #include "config.h"
147 #include "wine/port.h"
149 #include <assert.h>
150 #include <ctype.h>
151 #include <string.h>
152 #include <stdlib.h>
153 #include <stdarg.h>
154 #include <stdio.h>
156 #include "windef.h"
157 #include "winbase.h"
158 #include "winnt.h"
159 #include "wingdi.h"
160 #include "winuser.h"
161 #include "winnls.h"
162 #include "commctrl.h"
163 #include "comctl32.h"
164 #include "uxtheme.h"
166 #include "wine/debug.h"
167 #include "wine/unicode.h"
169 WINE_DEFAULT_DEBUG_CHANNEL(listview);
171 typedef struct tagCOLUMN_INFO
173 RECT rcHeader; /* tracks the header's rectangle */
174 INT fmt; /* same as LVCOLUMN.fmt */
175 INT cxMin;
176 } COLUMN_INFO;
178 typedef struct tagITEMHDR
180 LPWSTR pszText;
181 INT iImage;
182 } ITEMHDR, *LPITEMHDR;
184 typedef struct tagSUBITEM_INFO
186 ITEMHDR hdr;
187 INT iSubItem;
188 } SUBITEM_INFO;
190 typedef struct tagITEM_ID ITEM_ID;
192 typedef struct tagITEM_INFO
194 ITEMHDR hdr;
195 UINT state;
196 LPARAM lParam;
197 INT iIndent;
198 ITEM_ID *id;
199 } ITEM_INFO;
201 struct tagITEM_ID
203 UINT id; /* item id */
204 HDPA item; /* link to item data */
207 typedef struct tagRANGE
209 INT lower;
210 INT upper;
211 } RANGE;
213 typedef struct tagRANGES
215 HDPA hdpa;
216 } *RANGES;
218 typedef struct tagITERATOR
220 INT nItem;
221 INT nSpecial;
222 RANGE range;
223 RANGES ranges;
224 INT index;
225 } ITERATOR;
227 typedef struct tagDELAYED_ITEM_EDIT
229 BOOL fEnabled;
230 INT iItem;
231 } DELAYED_ITEM_EDIT;
233 typedef struct tagLISTVIEW_INFO
235 /* control window */
236 HWND hwndSelf;
237 RECT rcList; /* This rectangle is really the window
238 * client rectangle possibly reduced by the
239 * horizontal scroll bar and/or header - see
240 * LISTVIEW_UpdateSize. This rectangle offset
241 * by the LISTVIEW_GetOrigin value is in
242 * client coordinates */
244 /* notification window */
245 SHORT notifyFormat;
246 HWND hwndNotify;
247 BOOL bDoChangeNotify; /* send change notification messages? */
248 UINT uCallbackMask;
250 /* tooltips */
251 HWND hwndToolTip;
253 /* items */
254 INT nItemCount; /* the number of items in the list */
255 HDPA hdpaItems; /* array ITEM_INFO pointers */
256 HDPA hdpaItemIds; /* array of ITEM_ID pointers */
257 HDPA hdpaPosX; /* maintains the (X, Y) coordinates of the */
258 HDPA hdpaPosY; /* items in LVS_ICON, and LVS_SMALLICON modes */
259 RANGES selectionRanges;
260 INT nSelectionMark; /* item to start next multiselection from */
261 INT nHotItem;
262 BOOL bAutoarrange; /* Autoarrange flag when NOT in LVS_AUTOARRANGE */
264 /* columns */
265 HDPA hdpaColumns; /* array of COLUMN_INFO pointers */
266 BOOL colRectsDirty; /* trigger column rectangles requery from header */
268 /* item metrics */
269 BOOL bNoItemMetrics; /* flags if item metrics are not yet computed */
270 INT nItemHeight;
271 INT nItemWidth;
273 /* sorting */
274 PFNLVCOMPARE pfnCompare; /* sorting callback pointer */
275 LPARAM lParamSort;
277 /* style */
278 DWORD dwStyle; /* the cached window GWL_STYLE */
279 DWORD dwLvExStyle; /* extended listview style */
280 DWORD uView; /* current view available through LVM_[G,S]ETVIEW */
282 /* edit item */
283 HWND hwndEdit;
284 WNDPROC EditWndProc;
285 INT nEditLabelItem;
286 DELAYED_ITEM_EDIT itemEdit; /* Pointer to this structure will be the timer ID */
288 /* icons */
289 HIMAGELIST himlNormal;
290 HIMAGELIST himlSmall;
291 HIMAGELIST himlState;
292 SIZE iconSize;
293 SIZE iconSpacing;
294 SIZE iconStateSize;
295 POINT currIconPos; /* this is the position next icon will be placed */
297 /* header */
298 HWND hwndHeader;
299 INT xTrackLine; /* The x coefficient of the track line or -1 if none */
301 /* marquee selection */
302 BOOL bMarqueeSelect; /* marquee selection/highlight underway */
303 BOOL bScrolling;
304 RECT marqueeRect; /* absolute coordinates of marquee selection */
305 RECT marqueeDrawRect; /* relative coordinates for drawing marquee */
306 POINT marqueeOrigin; /* absolute coordinates of marquee click origin */
308 /* focus drawing */
309 BOOL bFocus; /* control has focus */
310 INT nFocusedItem;
311 RECT rcFocus; /* focus bounds */
313 /* colors */
314 HBRUSH hBkBrush;
315 COLORREF clrBk;
316 COLORREF clrText;
317 COLORREF clrTextBk;
319 /* font */
320 HFONT hDefaultFont;
321 HFONT hFont;
322 INT ntmHeight; /* Some cached metrics of the font used */
323 INT ntmMaxCharWidth; /* by the listview to draw items */
324 INT nEllipsisWidth;
326 /* mouse operation */
327 BOOL bLButtonDown;
328 BOOL bRButtonDown;
329 BOOL bDragging;
330 POINT ptClickPos; /* point where the user clicked */
331 INT nLButtonDownItem; /* tracks item to reset multiselection on WM_LBUTTONUP */
332 DWORD dwHoverTime;
333 HCURSOR hHotCursor;
335 /* keyboard operation */
336 DWORD lastKeyPressTimestamp;
337 WPARAM charCode;
338 INT nSearchParamLength;
339 WCHAR szSearchParam[ MAX_PATH ];
341 /* painting */
342 DWORD cditemmode; /* Keep the custom draw flags for an item/row */
343 BOOL bIsDrawing; /* Drawing in progress */
344 INT nMeasureItemHeight; /* WM_MEASUREITEM result */
345 BOOL bRedraw; /* WM_SETREDRAW switch */
347 /* misc */
348 DWORD iVersion; /* CCM_[G,S]ETVERSION */
349 } LISTVIEW_INFO;
352 * constants
354 /* How many we debug buffer to allocate */
355 #define DEBUG_BUFFERS 20
356 /* The size of a single debug buffer */
357 #define DEBUG_BUFFER_SIZE 256
359 /* Internal interface to LISTVIEW_HScroll and LISTVIEW_VScroll */
360 #define SB_INTERNAL -1
362 /* maximum size of a label */
363 #define DISP_TEXT_SIZE 260
365 /* padding for items in list and small icon display modes */
366 #define WIDTH_PADDING 12
368 /* padding for items in list, report and small icon display modes */
369 #define HEIGHT_PADDING 1
371 /* offset of items in report display mode */
372 #define REPORT_MARGINX 2
374 /* padding for icon in large icon display mode
375 * ICON_TOP_PADDING_NOTHITABLE - space between top of box and area
376 * that HITTEST will see.
377 * ICON_TOP_PADDING_HITABLE - spacing between above and icon.
378 * ICON_TOP_PADDING - sum of the two above.
379 * ICON_BOTTOM_PADDING - between bottom of icon and top of text
380 * LABEL_HOR_PADDING - between text and sides of box
381 * LABEL_VERT_PADDING - between bottom of text and end of box
383 * ICON_LR_PADDING - additional width above icon size.
384 * ICON_LR_HALF - half of the above value
386 #define ICON_TOP_PADDING_NOTHITABLE 2
387 #define ICON_TOP_PADDING_HITABLE 2
388 #define ICON_TOP_PADDING (ICON_TOP_PADDING_NOTHITABLE + ICON_TOP_PADDING_HITABLE)
389 #define ICON_BOTTOM_PADDING 4
390 #define LABEL_HOR_PADDING 5
391 #define LABEL_VERT_PADDING 7
392 #define ICON_LR_PADDING 16
393 #define ICON_LR_HALF (ICON_LR_PADDING/2)
395 /* default label width for items in list and small icon display modes */
396 #define DEFAULT_LABEL_WIDTH 40
397 /* maximum select rectangle width for empty text item in LV_VIEW_DETAILS */
398 #define MAX_EMPTYTEXT_SELECT_WIDTH 80
400 /* default column width for items in list display mode */
401 #define DEFAULT_COLUMN_WIDTH 128
403 /* Size of "line" scroll for V & H scrolls */
404 #define LISTVIEW_SCROLL_ICON_LINE_SIZE 37
406 /* Padding between image and label */
407 #define IMAGE_PADDING 2
409 /* Padding behind the label */
410 #define TRAILING_LABEL_PADDING 12
411 #define TRAILING_HEADER_PADDING 11
413 /* Border for the icon caption */
414 #define CAPTION_BORDER 2
416 /* Standard DrawText flags */
417 #define LV_ML_DT_FLAGS (DT_TOP | DT_NOPREFIX | DT_EDITCONTROL | DT_CENTER | DT_WORDBREAK | DT_WORD_ELLIPSIS | DT_END_ELLIPSIS)
418 #define LV_FL_DT_FLAGS (DT_TOP | DT_NOPREFIX | DT_EDITCONTROL | DT_CENTER | DT_WORDBREAK | DT_NOCLIP)
419 #define LV_SL_DT_FLAGS (DT_VCENTER | DT_NOPREFIX | DT_EDITCONTROL | DT_SINGLELINE | DT_WORD_ELLIPSIS | DT_END_ELLIPSIS)
421 /* Image index from state */
422 #define STATEIMAGEINDEX(x) (((x) & LVIS_STATEIMAGEMASK) >> 12)
424 /* The time in milliseconds to reset the search in the list */
425 #define KEY_DELAY 450
427 /* Dump the LISTVIEW_INFO structure to the debug channel */
428 #define LISTVIEW_DUMP(iP) do { \
429 TRACE("hwndSelf=%p, clrBk=0x%06x, clrText=0x%06x, clrTextBk=0x%06x, ItemHeight=%d, ItemWidth=%d, Style=0x%08x\n", \
430 iP->hwndSelf, iP->clrBk, iP->clrText, iP->clrTextBk, \
431 iP->nItemHeight, iP->nItemWidth, iP->dwStyle); \
432 TRACE("hwndSelf=%p, himlNor=%p, himlSml=%p, himlState=%p, Focused=%d, Hot=%d, exStyle=0x%08x, Focus=%d\n", \
433 iP->hwndSelf, iP->himlNormal, iP->himlSmall, iP->himlState, \
434 iP->nFocusedItem, iP->nHotItem, iP->dwLvExStyle, iP->bFocus ); \
435 TRACE("hwndSelf=%p, ntmH=%d, icSz.cx=%d, icSz.cy=%d, icSp.cx=%d, icSp.cy=%d, notifyFmt=%d\n", \
436 iP->hwndSelf, iP->ntmHeight, iP->iconSize.cx, iP->iconSize.cy, \
437 iP->iconSpacing.cx, iP->iconSpacing.cy, iP->notifyFormat); \
438 TRACE("hwndSelf=%p, rcList=%s\n", iP->hwndSelf, wine_dbgstr_rect(&iP->rcList)); \
439 } while(0)
441 static const WCHAR themeClass[] = {'L','i','s','t','V','i','e','w',0};
444 * forward declarations
446 static BOOL LISTVIEW_GetItemT(const LISTVIEW_INFO *, LPLVITEMW, BOOL);
447 static void LISTVIEW_GetItemBox(const LISTVIEW_INFO *, INT, LPRECT);
448 static void LISTVIEW_GetItemOrigin(const LISTVIEW_INFO *, INT, LPPOINT);
449 static BOOL LISTVIEW_GetItemPosition(const LISTVIEW_INFO *, INT, LPPOINT);
450 static BOOL LISTVIEW_GetItemRect(const LISTVIEW_INFO *, INT, LPRECT);
451 static void LISTVIEW_GetOrigin(const LISTVIEW_INFO *, LPPOINT);
452 static BOOL LISTVIEW_GetViewRect(const LISTVIEW_INFO *, LPRECT);
453 static void LISTVIEW_UpdateSize(LISTVIEW_INFO *);
454 static LRESULT LISTVIEW_Command(LISTVIEW_INFO *, WPARAM, LPARAM);
455 static INT LISTVIEW_GetStringWidthT(const LISTVIEW_INFO *, LPCWSTR, BOOL);
456 static BOOL LISTVIEW_KeySelection(LISTVIEW_INFO *, INT, BOOL);
457 static UINT LISTVIEW_GetItemState(const LISTVIEW_INFO *, INT, UINT);
458 static BOOL LISTVIEW_SetItemState(LISTVIEW_INFO *, INT, const LVITEMW *);
459 static LRESULT LISTVIEW_VScroll(LISTVIEW_INFO *, INT, INT);
460 static LRESULT LISTVIEW_HScroll(LISTVIEW_INFO *, INT, INT);
461 static BOOL LISTVIEW_EnsureVisible(LISTVIEW_INFO *, INT, BOOL);
462 static HIMAGELIST LISTVIEW_SetImageList(LISTVIEW_INFO *, INT, HIMAGELIST);
463 static INT LISTVIEW_HitTest(const LISTVIEW_INFO *, LPLVHITTESTINFO, BOOL, BOOL);
464 static BOOL LISTVIEW_EndEditLabelT(LISTVIEW_INFO *, BOOL, BOOL);
465 static BOOL LISTVIEW_Scroll(LISTVIEW_INFO *, INT, INT);
467 /******** Text handling functions *************************************/
469 /* A text pointer is either NULL, LPSTR_TEXTCALLBACK, or points to a
470 * text string. The string may be ANSI or Unicode, in which case
471 * the boolean isW tells us the type of the string.
473 * The name of the function tell what type of strings it expects:
474 * W: Unicode, T: ANSI/Unicode - function of isW
477 static inline BOOL is_text(LPCWSTR text)
479 return text != NULL && text != LPSTR_TEXTCALLBACKW;
482 static inline int textlenT(LPCWSTR text, BOOL isW)
484 return !is_text(text) ? 0 :
485 isW ? lstrlenW(text) : lstrlenA((LPCSTR)text);
488 static inline void textcpynT(LPWSTR dest, BOOL isDestW, LPCWSTR src, BOOL isSrcW, INT max)
490 if (isDestW)
491 if (isSrcW) lstrcpynW(dest, src, max);
492 else MultiByteToWideChar(CP_ACP, 0, (LPCSTR)src, -1, dest, max);
493 else
494 if (isSrcW) WideCharToMultiByte(CP_ACP, 0, src, -1, (LPSTR)dest, max, NULL, NULL);
495 else lstrcpynA((LPSTR)dest, (LPCSTR)src, max);
498 static inline LPWSTR textdupTtoW(LPCWSTR text, BOOL isW)
500 LPWSTR wstr = (LPWSTR)text;
502 if (!isW && is_text(text))
504 INT len = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)text, -1, NULL, 0);
505 wstr = Alloc(len * sizeof(WCHAR));
506 if (wstr) MultiByteToWideChar(CP_ACP, 0, (LPCSTR)text, -1, wstr, len);
508 TRACE(" wstr=%s\n", text == LPSTR_TEXTCALLBACKW ? "(callback)" : debugstr_w(wstr));
509 return wstr;
512 static inline void textfreeT(LPWSTR wstr, BOOL isW)
514 if (!isW && is_text(wstr)) Free (wstr);
518 * dest is a pointer to a Unicode string
519 * src is a pointer to a string (Unicode if isW, ANSI if !isW)
521 static BOOL textsetptrT(LPWSTR *dest, LPCWSTR src, BOOL isW)
523 BOOL bResult = TRUE;
525 if (src == LPSTR_TEXTCALLBACKW)
527 if (is_text(*dest)) Free(*dest);
528 *dest = LPSTR_TEXTCALLBACKW;
530 else
532 LPWSTR pszText = textdupTtoW(src, isW);
533 if (*dest == LPSTR_TEXTCALLBACKW) *dest = NULL;
534 bResult = Str_SetPtrW(dest, pszText);
535 textfreeT(pszText, isW);
537 return bResult;
541 * compares a Unicode to a Unicode/ANSI text string
543 static inline int textcmpWT(LPCWSTR aw, LPCWSTR bt, BOOL isW)
545 if (!aw) return bt ? -1 : 0;
546 if (!bt) return 1;
547 if (aw == LPSTR_TEXTCALLBACKW)
548 return bt == LPSTR_TEXTCALLBACKW ? 1 : -1;
549 if (bt != LPSTR_TEXTCALLBACKW)
551 LPWSTR bw = textdupTtoW(bt, isW);
552 int r = bw ? lstrcmpW(aw, bw) : 1;
553 textfreeT(bw, isW);
554 return r;
557 return 1;
560 static inline int lstrncmpiW(LPCWSTR s1, LPCWSTR s2, int n)
562 n = min(min(n, lstrlenW(s1)), lstrlenW(s2));
563 return CompareStringW(LOCALE_USER_DEFAULT, NORM_IGNORECASE, s1, n, s2, n) - CSTR_EQUAL;
566 /******** Debugging functions *****************************************/
568 static inline LPCSTR debugtext_t(LPCWSTR text, BOOL isW)
570 if (text == LPSTR_TEXTCALLBACKW) return "(callback)";
571 return isW ? debugstr_w(text) : debugstr_a((LPCSTR)text);
574 static inline LPCSTR debugtext_tn(LPCWSTR text, BOOL isW, INT n)
576 if (text == LPSTR_TEXTCALLBACKW) return "(callback)";
577 n = min(textlenT(text, isW), n);
578 return isW ? debugstr_wn(text, n) : debugstr_an((LPCSTR)text, n);
581 static char* debug_getbuf(void)
583 static int index = 0;
584 static char buffers[DEBUG_BUFFERS][DEBUG_BUFFER_SIZE];
585 return buffers[index++ % DEBUG_BUFFERS];
588 static inline const char* debugrange(const RANGE *lprng)
590 if (!lprng) return "(null)";
591 return wine_dbg_sprintf("[%d, %d]", lprng->lower, lprng->upper);
594 static const char* debugscrollinfo(const SCROLLINFO *pScrollInfo)
596 char* buf = debug_getbuf(), *text = buf;
597 int len, size = DEBUG_BUFFER_SIZE;
599 if (pScrollInfo == NULL) return "(null)";
600 len = snprintf(buf, size, "{cbSize=%d, ", pScrollInfo->cbSize);
601 if (len == -1) goto end; buf += len; size -= len;
602 if (pScrollInfo->fMask & SIF_RANGE)
603 len = snprintf(buf, size, "nMin=%d, nMax=%d, ", pScrollInfo->nMin, pScrollInfo->nMax);
604 else len = 0;
605 if (len == -1) goto end; buf += len; size -= len;
606 if (pScrollInfo->fMask & SIF_PAGE)
607 len = snprintf(buf, size, "nPage=%u, ", pScrollInfo->nPage);
608 else len = 0;
609 if (len == -1) goto end; buf += len; size -= len;
610 if (pScrollInfo->fMask & SIF_POS)
611 len = snprintf(buf, size, "nPos=%d, ", pScrollInfo->nPos);
612 else len = 0;
613 if (len == -1) goto end; buf += len; size -= len;
614 if (pScrollInfo->fMask & SIF_TRACKPOS)
615 len = snprintf(buf, size, "nTrackPos=%d, ", pScrollInfo->nTrackPos);
616 else len = 0;
617 if (len == -1) goto end; buf += len;
618 goto undo;
619 end:
620 buf = text + strlen(text);
621 undo:
622 if (buf - text > 2) { buf[-2] = '}'; buf[-1] = 0; }
623 return text;
626 static const char* debugnmlistview(const NMLISTVIEW *plvnm)
628 if (!plvnm) return "(null)";
629 return wine_dbg_sprintf("iItem=%d, iSubItem=%d, uNewState=0x%x,"
630 " uOldState=0x%x, uChanged=0x%x, ptAction=%s, lParam=%ld",
631 plvnm->iItem, plvnm->iSubItem, plvnm->uNewState, plvnm->uOldState,
632 plvnm->uChanged, wine_dbgstr_point(&plvnm->ptAction), plvnm->lParam);
635 static const char* debuglvitem_t(const LVITEMW *lpLVItem, BOOL isW)
637 char* buf = debug_getbuf(), *text = buf;
638 int len, size = DEBUG_BUFFER_SIZE;
640 if (lpLVItem == NULL) return "(null)";
641 len = snprintf(buf, size, "{iItem=%d, iSubItem=%d, ", lpLVItem->iItem, lpLVItem->iSubItem);
642 if (len == -1) goto end; buf += len; size -= len;
643 if (lpLVItem->mask & LVIF_STATE)
644 len = snprintf(buf, size, "state=%x, stateMask=%x, ", lpLVItem->state, lpLVItem->stateMask);
645 else len = 0;
646 if (len == -1) goto end; buf += len; size -= len;
647 if (lpLVItem->mask & LVIF_TEXT)
648 len = snprintf(buf, size, "pszText=%s, cchTextMax=%d, ", debugtext_tn(lpLVItem->pszText, isW, 80), lpLVItem->cchTextMax);
649 else len = 0;
650 if (len == -1) goto end; buf += len; size -= len;
651 if (lpLVItem->mask & LVIF_IMAGE)
652 len = snprintf(buf, size, "iImage=%d, ", lpLVItem->iImage);
653 else len = 0;
654 if (len == -1) goto end; buf += len; size -= len;
655 if (lpLVItem->mask & LVIF_PARAM)
656 len = snprintf(buf, size, "lParam=%lx, ", lpLVItem->lParam);
657 else len = 0;
658 if (len == -1) goto end; buf += len; size -= len;
659 if (lpLVItem->mask & LVIF_INDENT)
660 len = snprintf(buf, size, "iIndent=%d, ", lpLVItem->iIndent);
661 else len = 0;
662 if (len == -1) goto end; buf += len;
663 goto undo;
664 end:
665 buf = text + strlen(text);
666 undo:
667 if (buf - text > 2) { buf[-2] = '}'; buf[-1] = 0; }
668 return text;
671 static const char* debuglvcolumn_t(const LVCOLUMNW *lpColumn, BOOL isW)
673 char* buf = debug_getbuf(), *text = buf;
674 int len, size = DEBUG_BUFFER_SIZE;
676 if (lpColumn == NULL) return "(null)";
677 len = snprintf(buf, size, "{");
678 if (len == -1) goto end; buf += len; size -= len;
679 if (lpColumn->mask & LVCF_SUBITEM)
680 len = snprintf(buf, size, "iSubItem=%d, ", lpColumn->iSubItem);
681 else len = 0;
682 if (len == -1) goto end; buf += len; size -= len;
683 if (lpColumn->mask & LVCF_FMT)
684 len = snprintf(buf, size, "fmt=%x, ", lpColumn->fmt);
685 else len = 0;
686 if (len == -1) goto end; buf += len; size -= len;
687 if (lpColumn->mask & LVCF_WIDTH)
688 len = snprintf(buf, size, "cx=%d, ", lpColumn->cx);
689 else len = 0;
690 if (len == -1) goto end; buf += len; size -= len;
691 if (lpColumn->mask & LVCF_TEXT)
692 len = snprintf(buf, size, "pszText=%s, cchTextMax=%d, ", debugtext_tn(lpColumn->pszText, isW, 80), lpColumn->cchTextMax);
693 else len = 0;
694 if (len == -1) goto end; buf += len; size -= len;
695 if (lpColumn->mask & LVCF_IMAGE)
696 len = snprintf(buf, size, "iImage=%d, ", lpColumn->iImage);
697 else len = 0;
698 if (len == -1) goto end; buf += len; size -= len;
699 if (lpColumn->mask & LVCF_ORDER)
700 len = snprintf(buf, size, "iOrder=%d, ", lpColumn->iOrder);
701 else len = 0;
702 if (len == -1) goto end; buf += len;
703 goto undo;
704 end:
705 buf = text + strlen(text);
706 undo:
707 if (buf - text > 2) { buf[-2] = '}'; buf[-1] = 0; }
708 return text;
711 static const char* debuglvhittestinfo(const LVHITTESTINFO *lpht)
713 if (!lpht) return "(null)";
715 return wine_dbg_sprintf("{pt=%s, flags=0x%x, iItem=%d, iSubItem=%d}",
716 wine_dbgstr_point(&lpht->pt), lpht->flags, lpht->iItem, lpht->iSubItem);
719 /* Return the corresponding text for a given scroll value */
720 static inline LPCSTR debugscrollcode(int nScrollCode)
722 switch(nScrollCode)
724 case SB_LINELEFT: return "SB_LINELEFT";
725 case SB_LINERIGHT: return "SB_LINERIGHT";
726 case SB_PAGELEFT: return "SB_PAGELEFT";
727 case SB_PAGERIGHT: return "SB_PAGERIGHT";
728 case SB_THUMBPOSITION: return "SB_THUMBPOSITION";
729 case SB_THUMBTRACK: return "SB_THUMBTRACK";
730 case SB_ENDSCROLL: return "SB_ENDSCROLL";
731 case SB_INTERNAL: return "SB_INTERNAL";
732 default: return "unknown";
737 /******** Notification functions ************************************/
739 static int get_ansi_notification(UINT unicodeNotificationCode)
741 switch (unicodeNotificationCode)
743 case LVN_BEGINLABELEDITA:
744 case LVN_BEGINLABELEDITW: return LVN_BEGINLABELEDITA;
745 case LVN_ENDLABELEDITA:
746 case LVN_ENDLABELEDITW: return LVN_ENDLABELEDITA;
747 case LVN_GETDISPINFOA:
748 case LVN_GETDISPINFOW: return LVN_GETDISPINFOA;
749 case LVN_SETDISPINFOA:
750 case LVN_SETDISPINFOW: return LVN_SETDISPINFOA;
751 case LVN_ODFINDITEMA:
752 case LVN_ODFINDITEMW: return LVN_ODFINDITEMA;
753 case LVN_GETINFOTIPA:
754 case LVN_GETINFOTIPW: return LVN_GETINFOTIPA;
755 /* header forwards */
756 case HDN_TRACKA:
757 case HDN_TRACKW: return HDN_TRACKA;
758 case HDN_ENDTRACKA:
759 case HDN_ENDTRACKW: return HDN_ENDTRACKA;
760 case HDN_BEGINDRAG: return HDN_BEGINDRAG;
761 case HDN_ENDDRAG: return HDN_ENDDRAG;
762 case HDN_ITEMCHANGINGA:
763 case HDN_ITEMCHANGINGW: return HDN_ITEMCHANGINGA;
764 case HDN_ITEMCHANGEDA:
765 case HDN_ITEMCHANGEDW: return HDN_ITEMCHANGEDA;
766 case HDN_ITEMCLICKA:
767 case HDN_ITEMCLICKW: return HDN_ITEMCLICKA;
768 case HDN_DIVIDERDBLCLICKA:
769 case HDN_DIVIDERDBLCLICKW: return HDN_DIVIDERDBLCLICKA;
770 default: break;
772 FIXME("unknown notification %x\n", unicodeNotificationCode);
773 return unicodeNotificationCode;
776 /* forwards header notifications to listview parent */
777 static LRESULT notify_forward_header(const LISTVIEW_INFO *infoPtr, const NMHEADERW *lpnmh)
779 NMHEADERA nmhA;
780 HDITEMA hditema;
781 HD_TEXTFILTERA textfilter;
782 LPSTR text = NULL, filter = NULL;
783 LRESULT ret;
785 /* on unicode format exit earlier */
786 if (infoPtr->notifyFormat == NFR_UNICODE)
787 return SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, lpnmh->hdr.idFrom,
788 (LPARAM)lpnmh);
790 /* header always supplies unicode notifications,
791 all we have to do is to convert strings to ANSI */
792 nmhA = *(const NMHEADERA*)lpnmh;
793 if (lpnmh->pitem)
795 hditema = *(HDITEMA*)lpnmh->pitem;
796 nmhA.pitem = &hditema;
797 /* convert item text */
798 if (lpnmh->pitem->mask & HDI_TEXT)
800 hditema.pszText = NULL;
801 Str_SetPtrWtoA(&hditema.pszText, lpnmh->pitem->pszText);
802 text = hditema.pszText;
804 /* convert filter text */
805 if ((lpnmh->pitem->mask & HDI_FILTER) && (lpnmh->pitem->type == HDFT_ISSTRING) &&
806 lpnmh->pitem->pvFilter)
808 hditema.pvFilter = &textfilter;
809 textfilter = *(HD_TEXTFILTERA*)(lpnmh->pitem->pvFilter);
810 textfilter.pszText = NULL;
811 Str_SetPtrWtoA(&textfilter.pszText, ((HD_TEXTFILTERW*)lpnmh->pitem->pvFilter)->pszText);
812 filter = textfilter.pszText;
815 nmhA.hdr.code = get_ansi_notification(lpnmh->hdr.code);
817 ret = SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmhA.hdr.idFrom,
818 (LPARAM)&nmhA);
820 /* cleanup */
821 Free(text);
822 Free(filter);
824 return ret;
827 static LRESULT notify_hdr(const LISTVIEW_INFO *infoPtr, INT code, LPNMHDR pnmh)
829 LRESULT result;
831 TRACE("(code=%d)\n", code);
833 pnmh->hwndFrom = infoPtr->hwndSelf;
834 pnmh->idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
835 pnmh->code = code;
836 result = SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, pnmh->idFrom, (LPARAM)pnmh);
838 TRACE(" <= %ld\n", result);
840 return result;
843 static inline BOOL notify(const LISTVIEW_INFO *infoPtr, INT code)
845 NMHDR nmh;
846 HWND hwnd = infoPtr->hwndSelf;
847 notify_hdr(infoPtr, code, &nmh);
848 return IsWindow(hwnd);
851 static inline void notify_itemactivate(const LISTVIEW_INFO *infoPtr, const LVHITTESTINFO *htInfo)
853 NMITEMACTIVATE nmia;
854 LVITEMW item;
856 if (htInfo) {
857 nmia.uNewState = 0;
858 nmia.uOldState = 0;
859 nmia.uChanged = 0;
860 nmia.uKeyFlags = 0;
862 item.mask = LVIF_PARAM|LVIF_STATE;
863 item.iItem = htInfo->iItem;
864 item.iSubItem = 0;
865 item.stateMask = (UINT)-1;
866 if (LISTVIEW_GetItemT(infoPtr, &item, TRUE)) {
867 nmia.lParam = item.lParam;
868 nmia.uOldState = item.state;
869 nmia.uNewState = item.state | LVIS_ACTIVATING;
870 nmia.uChanged = LVIF_STATE;
873 nmia.iItem = htInfo->iItem;
874 nmia.iSubItem = htInfo->iSubItem;
875 nmia.ptAction = htInfo->pt;
877 if (GetKeyState(VK_SHIFT) & 0x8000) nmia.uKeyFlags |= LVKF_SHIFT;
878 if (GetKeyState(VK_CONTROL) & 0x8000) nmia.uKeyFlags |= LVKF_CONTROL;
879 if (GetKeyState(VK_MENU) & 0x8000) nmia.uKeyFlags |= LVKF_ALT;
881 notify_hdr(infoPtr, LVN_ITEMACTIVATE, (LPNMHDR)&nmia);
884 static inline LRESULT notify_listview(const LISTVIEW_INFO *infoPtr, INT code, LPNMLISTVIEW plvnm)
886 TRACE("(code=%d, plvnm=%s)\n", code, debugnmlistview(plvnm));
887 return notify_hdr(infoPtr, code, (LPNMHDR)plvnm);
890 static BOOL notify_click(const LISTVIEW_INFO *infoPtr, INT code, const LVHITTESTINFO *lvht)
892 NMITEMACTIVATE nmia;
893 LVITEMW item;
894 HWND hwnd = infoPtr->hwndSelf;
896 TRACE("code=%d, lvht=%s\n", code, debuglvhittestinfo(lvht));
897 ZeroMemory(&nmia, sizeof(nmia));
898 nmia.iItem = lvht->iItem;
899 nmia.iSubItem = lvht->iSubItem;
900 nmia.ptAction = lvht->pt;
901 item.mask = LVIF_PARAM;
902 item.iItem = lvht->iItem;
903 item.iSubItem = 0;
904 if (LISTVIEW_GetItemT(infoPtr, &item, TRUE)) nmia.lParam = item.lParam;
905 notify_hdr(infoPtr, code, (LPNMHDR)&nmia);
906 return IsWindow(hwnd);
909 static BOOL notify_deleteitem(const LISTVIEW_INFO *infoPtr, INT nItem)
911 NMLISTVIEW nmlv;
912 LVITEMW item;
913 HWND hwnd = infoPtr->hwndSelf;
915 ZeroMemory(&nmlv, sizeof (NMLISTVIEW));
916 nmlv.iItem = nItem;
917 item.mask = LVIF_PARAM;
918 item.iItem = nItem;
919 item.iSubItem = 0;
920 if (LISTVIEW_GetItemT(infoPtr, &item, TRUE)) nmlv.lParam = item.lParam;
921 notify_listview(infoPtr, LVN_DELETEITEM, &nmlv);
922 return IsWindow(hwnd);
926 Send notification. depends on dispinfoW having same
927 structure as dispinfoA.
928 infoPtr : listview struct
929 code : *Unicode* notification code
930 pdi : dispinfo structure (can be unicode or ansi)
931 isW : TRUE if dispinfo is Unicode
933 static BOOL notify_dispinfoT(const LISTVIEW_INFO *infoPtr, UINT code, LPNMLVDISPINFOW pdi, BOOL isW)
935 INT length = 0, ret_length;
936 LPWSTR buffer = NULL, ret_text;
937 BOOL return_ansi = FALSE;
938 BOOL return_unicode = FALSE;
939 BOOL ret;
941 if ((pdi->item.mask & LVIF_TEXT) && is_text(pdi->item.pszText))
943 return_unicode = ( isW && infoPtr->notifyFormat == NFR_ANSI);
944 return_ansi = (!isW && infoPtr->notifyFormat == NFR_UNICODE);
947 ret_length = pdi->item.cchTextMax;
948 ret_text = pdi->item.pszText;
950 if (return_unicode || return_ansi)
952 if (code != LVN_GETDISPINFOW)
954 length = return_ansi ?
955 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pdi->item.pszText, -1, NULL, 0):
956 WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, NULL, 0, NULL, NULL);
958 else
960 length = pdi->item.cchTextMax;
961 *pdi->item.pszText = 0; /* make sure we don't process garbage */
964 buffer = Alloc( (return_ansi ? sizeof(WCHAR) : sizeof(CHAR)) * length);
965 if (!buffer) return FALSE;
967 if (return_ansi)
968 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pdi->item.pszText, -1,
969 buffer, length);
970 else
971 WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, (LPSTR) buffer,
972 length, NULL, NULL);
974 pdi->item.pszText = buffer;
975 pdi->item.cchTextMax = length;
978 if (infoPtr->notifyFormat == NFR_ANSI)
979 code = get_ansi_notification(code);
981 TRACE(" pdi->item=%s\n", debuglvitem_t(&pdi->item, infoPtr->notifyFormat != NFR_ANSI));
982 ret = notify_hdr(infoPtr, code, &pdi->hdr);
983 TRACE(" resulting code=%d\n", pdi->hdr.code);
985 if (return_ansi || return_unicode)
987 if (return_ansi && (pdi->hdr.code == LVN_GETDISPINFOA))
989 strcpy((char*)ret_text, (char*)pdi->item.pszText);
991 else if (return_unicode && (pdi->hdr.code == LVN_GETDISPINFOW))
993 strcpyW(ret_text, pdi->item.pszText);
995 else if (return_ansi) /* note : pointer can be changed by app ! */
997 WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, (LPSTR) ret_text,
998 ret_length, NULL, NULL);
1000 else
1001 MultiByteToWideChar(CP_ACP, 0, (LPSTR) pdi->item.pszText, -1,
1002 ret_text, ret_length);
1004 pdi->item.pszText = ret_text; /* restores our buffer */
1005 pdi->item.cchTextMax = ret_length;
1007 Free(buffer);
1008 return ret;
1011 /* if dipsinfo holder changed notification code then convert */
1012 if (!isW && (pdi->hdr.code == LVN_GETDISPINFOW) && (pdi->item.mask & LVIF_TEXT))
1014 length = WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, NULL, 0, NULL, NULL);
1016 buffer = Alloc(length * sizeof(CHAR));
1017 if (!buffer) return FALSE;
1019 WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, (LPSTR) buffer,
1020 ret_length, NULL, NULL);
1022 strcpy((LPSTR)pdi->item.pszText, (LPSTR)buffer);
1023 Free(buffer);
1026 return ret;
1029 static void customdraw_fill(NMLVCUSTOMDRAW *lpnmlvcd, const LISTVIEW_INFO *infoPtr, HDC hdc,
1030 const RECT *rcBounds, const LVITEMW *lplvItem)
1032 ZeroMemory(lpnmlvcd, sizeof(NMLVCUSTOMDRAW));
1033 lpnmlvcd->nmcd.hdc = hdc;
1034 lpnmlvcd->nmcd.rc = *rcBounds;
1035 lpnmlvcd->clrTextBk = infoPtr->clrTextBk;
1036 lpnmlvcd->clrText = infoPtr->clrText;
1037 if (!lplvItem) return;
1038 lpnmlvcd->nmcd.dwItemSpec = lplvItem->iItem + 1;
1039 lpnmlvcd->iSubItem = lplvItem->iSubItem;
1040 if (lplvItem->state & LVIS_SELECTED) lpnmlvcd->nmcd.uItemState |= CDIS_SELECTED;
1041 if (lplvItem->state & LVIS_FOCUSED) lpnmlvcd->nmcd.uItemState |= CDIS_FOCUS;
1042 if (lplvItem->iItem == infoPtr->nHotItem) lpnmlvcd->nmcd.uItemState |= CDIS_HOT;
1043 lpnmlvcd->nmcd.lItemlParam = lplvItem->lParam;
1046 static inline DWORD notify_customdraw (const LISTVIEW_INFO *infoPtr, DWORD dwDrawStage, NMLVCUSTOMDRAW *lpnmlvcd)
1048 BOOL isForItem = (lpnmlvcd->nmcd.dwItemSpec != 0);
1049 DWORD result;
1051 lpnmlvcd->nmcd.dwDrawStage = dwDrawStage;
1052 if (isForItem) lpnmlvcd->nmcd.dwDrawStage |= CDDS_ITEM;
1053 if (lpnmlvcd->iSubItem) lpnmlvcd->nmcd.dwDrawStage |= CDDS_SUBITEM;
1054 if (isForItem) lpnmlvcd->nmcd.dwItemSpec--;
1055 result = notify_hdr(infoPtr, NM_CUSTOMDRAW, &lpnmlvcd->nmcd.hdr);
1056 if (isForItem) lpnmlvcd->nmcd.dwItemSpec++;
1057 return result;
1060 static void prepaint_setup (const LISTVIEW_INFO *infoPtr, HDC hdc, NMLVCUSTOMDRAW *lpnmlvcd, BOOL SubItem)
1062 if (lpnmlvcd->clrTextBk == CLR_DEFAULT)
1063 lpnmlvcd->clrTextBk = comctl32_color.clrWindow;
1064 if (lpnmlvcd->clrText == CLR_DEFAULT)
1065 lpnmlvcd->clrText = comctl32_color.clrWindowText;
1067 /* apparently, for selected items, we have to override the returned values */
1068 if (!SubItem)
1070 if (lpnmlvcd->nmcd.uItemState & CDIS_SELECTED)
1072 if (infoPtr->bFocus)
1074 lpnmlvcd->clrTextBk = comctl32_color.clrHighlight;
1075 lpnmlvcd->clrText = comctl32_color.clrHighlightText;
1077 else if (infoPtr->dwStyle & LVS_SHOWSELALWAYS)
1079 lpnmlvcd->clrTextBk = comctl32_color.clr3dFace;
1080 lpnmlvcd->clrText = comctl32_color.clrBtnText;
1085 /* Set the text attributes */
1086 if (lpnmlvcd->clrTextBk != CLR_NONE)
1088 SetBkMode(hdc, OPAQUE);
1089 SetBkColor(hdc,lpnmlvcd->clrTextBk);
1091 else
1092 SetBkMode(hdc, TRANSPARENT);
1093 SetTextColor(hdc, lpnmlvcd->clrText);
1096 static inline DWORD notify_postpaint (const LISTVIEW_INFO *infoPtr, NMLVCUSTOMDRAW *lpnmlvcd)
1098 return notify_customdraw(infoPtr, CDDS_POSTPAINT, lpnmlvcd);
1101 /* returns TRUE when repaint needed, FALSE otherwise */
1102 static BOOL notify_measureitem(LISTVIEW_INFO *infoPtr)
1104 MEASUREITEMSTRUCT mis;
1105 mis.CtlType = ODT_LISTVIEW;
1106 mis.CtlID = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1107 mis.itemID = -1;
1108 mis.itemWidth = 0;
1109 mis.itemData = 0;
1110 mis.itemHeight= infoPtr->nItemHeight;
1111 SendMessageW(infoPtr->hwndNotify, WM_MEASUREITEM, mis.CtlID, (LPARAM)&mis);
1112 if (infoPtr->nItemHeight != max(mis.itemHeight, 1))
1114 infoPtr->nMeasureItemHeight = infoPtr->nItemHeight = max(mis.itemHeight, 1);
1115 return TRUE;
1117 return FALSE;
1120 /******** Item iterator functions **********************************/
1122 static RANGES ranges_create(int count);
1123 static void ranges_destroy(RANGES ranges);
1124 static BOOL ranges_add(RANGES ranges, RANGE range);
1125 static BOOL ranges_del(RANGES ranges, RANGE range);
1126 static void ranges_dump(RANGES ranges);
1128 static inline BOOL ranges_additem(RANGES ranges, INT nItem)
1130 RANGE range = { nItem, nItem + 1 };
1132 return ranges_add(ranges, range);
1135 static inline BOOL ranges_delitem(RANGES ranges, INT nItem)
1137 RANGE range = { nItem, nItem + 1 };
1139 return ranges_del(ranges, range);
1142 /***
1143 * ITERATOR DOCUMENTATION
1145 * The iterator functions allow for easy, and convenient iteration
1146 * over items of interest in the list. Typically, you create a
1147 * iterator, use it, and destroy it, as such:
1148 * ITERATOR i;
1150 * iterator_xxxitems(&i, ...);
1151 * while (iterator_{prev,next}(&i)
1153 * //code which uses i.nItem
1155 * iterator_destroy(&i);
1157 * where xxx is either: framed, or visible.
1158 * Note that it is important that the code destroys the iterator
1159 * after it's done with it, as the creation of the iterator may
1160 * allocate memory, which thus needs to be freed.
1162 * You can iterate both forwards, and backwards through the list,
1163 * by using iterator_next or iterator_prev respectively.
1165 * Lower numbered items are draw on top of higher number items in
1166 * LVS_ICON, and LVS_SMALLICON (which are the only modes where
1167 * items may overlap). So, to test items, you should use
1168 * iterator_next
1169 * which lists the items top to bottom (in Z-order).
1170 * For drawing items, you should use
1171 * iterator_prev
1172 * which lists the items bottom to top (in Z-order).
1173 * If you keep iterating over the items after the end-of-items
1174 * marker (-1) is returned, the iterator will start from the
1175 * beginning. Typically, you don't need to test for -1,
1176 * because iterator_{next,prev} will return TRUE if more items
1177 * are to be iterated over, or FALSE otherwise.
1179 * Note: the iterator is defined to be bidirectional. That is,
1180 * any number of prev followed by any number of next, or
1181 * five versa, should leave the iterator at the same item:
1182 * prev * n, next * n = next * n, prev * n
1184 * The iterator has a notion of an out-of-order, special item,
1185 * which sits at the start of the list. This is used in
1186 * LVS_ICON, and LVS_SMALLICON mode to handle the focused item,
1187 * which needs to be first, as it may overlap other items.
1189 * The code is a bit messy because we have:
1190 * - a special item to deal with
1191 * - simple range, or composite range
1192 * - empty range.
1193 * If you find bugs, or want to add features, please make sure you
1194 * always check/modify *both* iterator_prev, and iterator_next.
1197 /****
1198 * This function iterates through the items in increasing order,
1199 * but prefixed by the special item, then -1. That is:
1200 * special, 1, 2, 3, ..., n, -1.
1201 * Each item is listed only once.
1203 static inline BOOL iterator_next(ITERATOR* i)
1205 if (i->nItem == -1)
1207 i->nItem = i->nSpecial;
1208 if (i->nItem != -1) return TRUE;
1210 if (i->nItem == i->nSpecial)
1212 if (i->ranges) i->index = 0;
1213 goto pickarange;
1216 i->nItem++;
1217 testitem:
1218 if (i->nItem == i->nSpecial) i->nItem++;
1219 if (i->nItem < i->range.upper) return TRUE;
1221 pickarange:
1222 if (i->ranges)
1224 if (i->index < DPA_GetPtrCount(i->ranges->hdpa))
1225 i->range = *(RANGE*)DPA_GetPtr(i->ranges->hdpa, i->index++);
1226 else goto end;
1228 else if (i->nItem >= i->range.upper) goto end;
1230 i->nItem = i->range.lower;
1231 if (i->nItem >= 0) goto testitem;
1232 end:
1233 i->nItem = -1;
1234 return FALSE;
1237 /****
1238 * This function iterates through the items in decreasing order,
1239 * followed by the special item, then -1. That is:
1240 * n, n-1, ..., 3, 2, 1, special, -1.
1241 * Each item is listed only once.
1243 static inline BOOL iterator_prev(ITERATOR* i)
1245 BOOL start = FALSE;
1247 if (i->nItem == -1)
1249 start = TRUE;
1250 if (i->ranges) i->index = DPA_GetPtrCount(i->ranges->hdpa);
1251 goto pickarange;
1253 if (i->nItem == i->nSpecial)
1255 i->nItem = -1;
1256 return FALSE;
1259 testitem:
1260 i->nItem--;
1261 if (i->nItem == i->nSpecial) i->nItem--;
1262 if (i->nItem >= i->range.lower) return TRUE;
1264 pickarange:
1265 if (i->ranges)
1267 if (i->index > 0)
1268 i->range = *(RANGE*)DPA_GetPtr(i->ranges->hdpa, --i->index);
1269 else goto end;
1271 else if (!start && i->nItem < i->range.lower) goto end;
1273 i->nItem = i->range.upper;
1274 if (i->nItem > 0) goto testitem;
1275 end:
1276 return (i->nItem = i->nSpecial) != -1;
1279 static RANGE iterator_range(const ITERATOR *i)
1281 RANGE range;
1283 if (!i->ranges) return i->range;
1285 if (DPA_GetPtrCount(i->ranges->hdpa) > 0)
1287 range.lower = (*(RANGE*)DPA_GetPtr(i->ranges->hdpa, 0)).lower;
1288 range.upper = (*(RANGE*)DPA_GetPtr(i->ranges->hdpa, DPA_GetPtrCount(i->ranges->hdpa) - 1)).upper;
1290 else range.lower = range.upper = 0;
1292 return range;
1295 /***
1296 * Releases resources associated with this iterator.
1298 static inline void iterator_destroy(const ITERATOR *i)
1300 ranges_destroy(i->ranges);
1303 /***
1304 * Create an empty iterator.
1306 static inline BOOL iterator_empty(ITERATOR* i)
1308 ZeroMemory(i, sizeof(*i));
1309 i->nItem = i->nSpecial = i->range.lower = i->range.upper = -1;
1310 return TRUE;
1313 /***
1314 * Create an iterator over a range.
1316 static inline BOOL iterator_rangeitems(ITERATOR* i, RANGE range)
1318 iterator_empty(i);
1319 i->range = range;
1320 return TRUE;
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 BOOL iterator_rangesitems(ITERATOR* i, RANGES ranges)
1330 iterator_empty(i);
1331 i->ranges = ranges;
1332 return TRUE;
1335 /***
1336 * Creates an iterator over the items which intersect frame.
1337 * Uses absolute coordinates rather than compensating for the current offset.
1339 static BOOL iterator_frameditems_absolute(ITERATOR* i, const LISTVIEW_INFO* infoPtr, const RECT *frame)
1341 RECT rcItem, rcTemp;
1343 /* in case we fail, we want to return an empty iterator */
1344 if (!iterator_empty(i)) return FALSE;
1346 TRACE("(frame=%s)\n", wine_dbgstr_rect(frame));
1348 if (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON)
1350 INT nItem;
1352 if (infoPtr->uView == LV_VIEW_ICON && infoPtr->nFocusedItem != -1)
1354 LISTVIEW_GetItemBox(infoPtr, infoPtr->nFocusedItem, &rcItem);
1355 if (IntersectRect(&rcTemp, &rcItem, frame))
1356 i->nSpecial = infoPtr->nFocusedItem;
1358 if (!(iterator_rangesitems(i, ranges_create(50)))) return FALSE;
1359 /* to do better here, we need to have PosX, and PosY sorted */
1360 TRACE("building icon ranges:\n");
1361 for (nItem = 0; nItem < infoPtr->nItemCount; nItem++)
1363 rcItem.left = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosX, nItem);
1364 rcItem.top = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosY, nItem);
1365 rcItem.right = rcItem.left + infoPtr->nItemWidth;
1366 rcItem.bottom = rcItem.top + infoPtr->nItemHeight;
1367 if (IntersectRect(&rcTemp, &rcItem, frame))
1368 ranges_additem(i->ranges, nItem);
1370 return TRUE;
1372 else if (infoPtr->uView == LV_VIEW_DETAILS)
1374 RANGE range;
1376 if (frame->left >= infoPtr->nItemWidth) return TRUE;
1377 if (frame->top >= infoPtr->nItemHeight * infoPtr->nItemCount) return TRUE;
1379 range.lower = max(frame->top / infoPtr->nItemHeight, 0);
1380 range.upper = min((frame->bottom - 1) / infoPtr->nItemHeight, infoPtr->nItemCount - 1) + 1;
1381 if (range.upper <= range.lower) return TRUE;
1382 if (!iterator_rangeitems(i, range)) return FALSE;
1383 TRACE(" report=%s\n", debugrange(&i->range));
1385 else
1387 INT nPerCol = max((infoPtr->rcList.bottom - infoPtr->rcList.top) / infoPtr->nItemHeight, 1);
1388 INT nFirstRow = max(frame->top / infoPtr->nItemHeight, 0);
1389 INT nLastRow = min((frame->bottom - 1) / infoPtr->nItemHeight, nPerCol - 1);
1390 INT nFirstCol;
1391 INT nLastCol;
1392 INT lower;
1393 RANGE item_range;
1394 INT nCol;
1396 if (infoPtr->nItemWidth)
1398 nFirstCol = max(frame->left / infoPtr->nItemWidth, 0);
1399 nLastCol = min((frame->right - 1) / infoPtr->nItemWidth, (infoPtr->nItemCount + nPerCol - 1) / nPerCol);
1401 else
1403 nFirstCol = max(frame->left, 0);
1404 nLastCol = min(frame->right - 1, (infoPtr->nItemCount + nPerCol - 1) / nPerCol);
1407 lower = nFirstCol * nPerCol + nFirstRow;
1409 TRACE("nPerCol=%d, nFirstRow=%d, nLastRow=%d, nFirstCol=%d, nLastCol=%d, lower=%d\n",
1410 nPerCol, nFirstRow, nLastRow, nFirstCol, nLastCol, lower);
1412 if (nLastCol < nFirstCol || nLastRow < nFirstRow) return TRUE;
1414 if (!(iterator_rangesitems(i, ranges_create(nLastCol - nFirstCol + 1)))) return FALSE;
1415 TRACE("building list ranges:\n");
1416 for (nCol = nFirstCol; nCol <= nLastCol; nCol++)
1418 item_range.lower = nCol * nPerCol + nFirstRow;
1419 if(item_range.lower >= infoPtr->nItemCount) break;
1420 item_range.upper = min(nCol * nPerCol + nLastRow + 1, infoPtr->nItemCount);
1421 TRACE(" list=%s\n", debugrange(&item_range));
1422 ranges_add(i->ranges, item_range);
1426 return TRUE;
1429 /***
1430 * Creates an iterator over the items which intersect lprc.
1432 static BOOL iterator_frameditems(ITERATOR* i, const LISTVIEW_INFO* infoPtr, const RECT *lprc)
1434 RECT frame = *lprc;
1435 POINT Origin;
1437 TRACE("(lprc=%s)\n", wine_dbgstr_rect(lprc));
1439 LISTVIEW_GetOrigin(infoPtr, &Origin);
1440 OffsetRect(&frame, -Origin.x, -Origin.y);
1442 return iterator_frameditems_absolute(i, infoPtr, &frame);
1445 /***
1446 * Creates an iterator over the items which intersect the visible region of hdc.
1448 static BOOL iterator_visibleitems(ITERATOR *i, const LISTVIEW_INFO *infoPtr, HDC hdc)
1450 POINT Origin, Position;
1451 RECT rcItem, rcClip;
1452 INT rgntype;
1454 rgntype = GetClipBox(hdc, &rcClip);
1455 if (rgntype == NULLREGION) return iterator_empty(i);
1456 if (!iterator_frameditems(i, infoPtr, &rcClip)) return FALSE;
1457 if (rgntype == SIMPLEREGION) return TRUE;
1459 /* first deal with the special item */
1460 if (i->nSpecial != -1)
1462 LISTVIEW_GetItemBox(infoPtr, i->nSpecial, &rcItem);
1463 if (!RectVisible(hdc, &rcItem)) i->nSpecial = -1;
1466 /* if we can't deal with the region, we'll just go with the simple range */
1467 LISTVIEW_GetOrigin(infoPtr, &Origin);
1468 TRACE("building visible range:\n");
1469 if (!i->ranges && i->range.lower < i->range.upper)
1471 if (!(i->ranges = ranges_create(50))) return TRUE;
1472 if (!ranges_add(i->ranges, i->range))
1474 ranges_destroy(i->ranges);
1475 i->ranges = 0;
1476 return TRUE;
1480 /* now delete the invisible items from the list */
1481 while(iterator_next(i))
1483 LISTVIEW_GetItemOrigin(infoPtr, i->nItem, &Position);
1484 rcItem.left = (infoPtr->uView == LV_VIEW_DETAILS) ? Origin.x : Position.x + Origin.x;
1485 rcItem.top = Position.y + Origin.y;
1486 rcItem.right = rcItem.left + infoPtr->nItemWidth;
1487 rcItem.bottom = rcItem.top + infoPtr->nItemHeight;
1488 if (!RectVisible(hdc, &rcItem))
1489 ranges_delitem(i->ranges, i->nItem);
1491 /* the iterator should restart on the next iterator_next */
1492 TRACE("done\n");
1494 return TRUE;
1497 /* Remove common elements from two iterators */
1498 /* Passed iterators have to point on the first elements */
1499 static BOOL iterator_remove_common_items(ITERATOR *iter1, ITERATOR *iter2)
1501 if(!iter1->ranges || !iter2->ranges) {
1502 int lower, upper;
1504 if(iter1->ranges || iter2->ranges ||
1505 (iter1->range.lower<iter2->range.lower && iter1->range.upper>iter2->range.upper) ||
1506 (iter1->range.lower>iter2->range.lower && iter1->range.upper<iter2->range.upper)) {
1507 ERR("result is not a one range iterator\n");
1508 return FALSE;
1511 if(iter1->range.lower==-1 || iter2->range.lower==-1)
1512 return TRUE;
1514 lower = iter1->range.lower;
1515 upper = iter1->range.upper;
1517 if(lower < iter2->range.lower)
1518 iter1->range.upper = iter2->range.lower;
1519 else if(upper > iter2->range.upper)
1520 iter1->range.lower = iter2->range.upper;
1521 else
1522 iter1->range.lower = iter1->range.upper = -1;
1524 if(iter2->range.lower < lower)
1525 iter2->range.upper = lower;
1526 else if(iter2->range.upper > upper)
1527 iter2->range.lower = upper;
1528 else
1529 iter2->range.lower = iter2->range.upper = -1;
1531 return TRUE;
1534 iterator_next(iter1);
1535 iterator_next(iter2);
1537 while(1) {
1538 if(iter1->nItem==-1 || iter2->nItem==-1)
1539 break;
1541 if(iter1->nItem == iter2->nItem) {
1542 int delete = iter1->nItem;
1544 iterator_prev(iter1);
1545 iterator_prev(iter2);
1546 ranges_delitem(iter1->ranges, delete);
1547 ranges_delitem(iter2->ranges, delete);
1548 iterator_next(iter1);
1549 iterator_next(iter2);
1550 } else if(iter1->nItem > iter2->nItem)
1551 iterator_next(iter2);
1552 else
1553 iterator_next(iter1);
1556 iter1->nItem = iter1->range.lower = iter1->range.upper = -1;
1557 iter2->nItem = iter2->range.lower = iter2->range.upper = -1;
1558 return TRUE;
1561 /******** Misc helper functions ************************************/
1563 static inline LRESULT CallWindowProcT(WNDPROC proc, HWND hwnd, UINT uMsg,
1564 WPARAM wParam, LPARAM lParam, BOOL isW)
1566 if (isW) return CallWindowProcW(proc, hwnd, uMsg, wParam, lParam);
1567 else return CallWindowProcA(proc, hwnd, uMsg, wParam, lParam);
1570 static inline BOOL is_autoarrange(const LISTVIEW_INFO *infoPtr)
1572 return ((infoPtr->dwStyle & LVS_AUTOARRANGE) || infoPtr->bAutoarrange) &&
1573 (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON);
1576 static void toggle_checkbox_state(LISTVIEW_INFO *infoPtr, INT nItem)
1578 DWORD state = STATEIMAGEINDEX(LISTVIEW_GetItemState(infoPtr, nItem, LVIS_STATEIMAGEMASK));
1579 if(state == 1 || state == 2)
1581 LVITEMW lvitem;
1582 state ^= 3;
1583 lvitem.state = INDEXTOSTATEIMAGEMASK(state);
1584 lvitem.stateMask = LVIS_STATEIMAGEMASK;
1585 LISTVIEW_SetItemState(infoPtr, nItem, &lvitem);
1589 /* this should be called after window style got updated,
1590 it used to reset view state to match current window style */
1591 static inline void map_style_view(LISTVIEW_INFO *infoPtr)
1593 switch (infoPtr->dwStyle & LVS_TYPEMASK)
1595 case LVS_ICON:
1596 infoPtr->uView = LV_VIEW_ICON;
1597 break;
1598 case LVS_REPORT:
1599 infoPtr->uView = LV_VIEW_DETAILS;
1600 break;
1601 case LVS_SMALLICON:
1602 infoPtr->uView = LV_VIEW_SMALLICON;
1603 break;
1604 case LVS_LIST:
1605 infoPtr->uView = LV_VIEW_LIST;
1609 /* computes next item id value */
1610 static DWORD get_next_itemid(const LISTVIEW_INFO *infoPtr)
1612 INT count = DPA_GetPtrCount(infoPtr->hdpaItemIds);
1614 if (count > 0)
1616 ITEM_ID *lpID = DPA_GetPtr(infoPtr->hdpaItemIds, count - 1);
1617 return lpID->id + 1;
1619 return 0;
1622 /******** Internal API functions ************************************/
1624 static inline COLUMN_INFO * LISTVIEW_GetColumnInfo(const LISTVIEW_INFO *infoPtr, INT nSubItem)
1626 static COLUMN_INFO mainItem;
1628 if (nSubItem == 0 && DPA_GetPtrCount(infoPtr->hdpaColumns) == 0) return &mainItem;
1629 assert (nSubItem >= 0 && nSubItem < DPA_GetPtrCount(infoPtr->hdpaColumns));
1631 /* update cached column rectangles */
1632 if (infoPtr->colRectsDirty)
1634 COLUMN_INFO *info;
1635 LISTVIEW_INFO *Ptr = (LISTVIEW_INFO*)infoPtr;
1636 INT i;
1638 for (i = 0; i < DPA_GetPtrCount(infoPtr->hdpaColumns); i++) {
1639 info = DPA_GetPtr(infoPtr->hdpaColumns, i);
1640 SendMessageW(infoPtr->hwndHeader, HDM_GETITEMRECT, i, (LPARAM)&info->rcHeader);
1642 Ptr->colRectsDirty = FALSE;
1645 return DPA_GetPtr(infoPtr->hdpaColumns, nSubItem);
1648 static INT LISTVIEW_CreateHeader(LISTVIEW_INFO *infoPtr)
1650 DWORD dFlags = WS_CHILD | HDS_HORZ | HDS_FULLDRAG | HDS_DRAGDROP;
1651 HINSTANCE hInst;
1653 if (infoPtr->hwndHeader) return 0;
1655 TRACE("Creating header for list %p\n", infoPtr->hwndSelf);
1657 /* setup creation flags */
1658 dFlags |= (LVS_NOSORTHEADER & infoPtr->dwStyle) ? 0 : HDS_BUTTONS;
1659 dFlags |= (LVS_NOCOLUMNHEADER & infoPtr->dwStyle) ? HDS_HIDDEN : 0;
1661 hInst = (HINSTANCE)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_HINSTANCE);
1663 /* create header */
1664 infoPtr->hwndHeader = CreateWindowW(WC_HEADERW, NULL, dFlags,
1665 0, 0, 0, 0, infoPtr->hwndSelf, NULL, hInst, NULL);
1666 if (!infoPtr->hwndHeader) return -1;
1668 /* set header unicode format */
1669 SendMessageW(infoPtr->hwndHeader, HDM_SETUNICODEFORMAT, TRUE, 0);
1671 /* set header font */
1672 SendMessageW(infoPtr->hwndHeader, WM_SETFONT, (WPARAM)infoPtr->hFont, TRUE);
1674 LISTVIEW_UpdateSize(infoPtr);
1676 return 0;
1679 static inline void LISTVIEW_GetHeaderRect(const LISTVIEW_INFO *infoPtr, INT nSubItem, LPRECT lprc)
1681 *lprc = LISTVIEW_GetColumnInfo(infoPtr, nSubItem)->rcHeader;
1684 static inline BOOL LISTVIEW_IsHeaderEnabled(const LISTVIEW_INFO *infoPtr)
1686 return (infoPtr->uView == LV_VIEW_DETAILS ||
1687 infoPtr->dwLvExStyle & LVS_EX_HEADERINALLVIEWS) &&
1688 !(infoPtr->dwStyle & LVS_NOCOLUMNHEADER);
1691 static inline BOOL LISTVIEW_GetItemW(const LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem)
1693 return LISTVIEW_GetItemT(infoPtr, lpLVItem, TRUE);
1696 /* used to handle collapse main item column case */
1697 static inline BOOL LISTVIEW_DrawFocusRect(const LISTVIEW_INFO *infoPtr, HDC hdc)
1699 return (infoPtr->rcFocus.left < infoPtr->rcFocus.right) ?
1700 DrawFocusRect(hdc, &infoPtr->rcFocus) : FALSE;
1703 /* Listview invalidation functions: use _only_ these functions to invalidate */
1705 static inline BOOL is_redrawing(const LISTVIEW_INFO *infoPtr)
1707 return infoPtr->bRedraw;
1710 static inline void LISTVIEW_InvalidateRect(const LISTVIEW_INFO *infoPtr, const RECT* rect)
1712 if(!is_redrawing(infoPtr)) return;
1713 TRACE(" invalidating rect=%s\n", wine_dbgstr_rect(rect));
1714 InvalidateRect(infoPtr->hwndSelf, rect, TRUE);
1717 static inline void LISTVIEW_InvalidateItem(const LISTVIEW_INFO *infoPtr, INT nItem)
1719 RECT rcBox;
1721 if(!is_redrawing(infoPtr)) return;
1722 LISTVIEW_GetItemBox(infoPtr, nItem, &rcBox);
1723 LISTVIEW_InvalidateRect(infoPtr, &rcBox);
1726 static inline void LISTVIEW_InvalidateSubItem(const LISTVIEW_INFO *infoPtr, INT nItem, INT nSubItem)
1728 POINT Origin, Position;
1729 RECT rcBox;
1731 if(!is_redrawing(infoPtr)) return;
1732 assert (infoPtr->uView == LV_VIEW_DETAILS);
1733 LISTVIEW_GetOrigin(infoPtr, &Origin);
1734 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
1735 LISTVIEW_GetHeaderRect(infoPtr, nSubItem, &rcBox);
1736 rcBox.top = 0;
1737 rcBox.bottom = infoPtr->nItemHeight;
1738 OffsetRect(&rcBox, Origin.x + Position.x, Origin.y + Position.y);
1739 LISTVIEW_InvalidateRect(infoPtr, &rcBox);
1742 static inline void LISTVIEW_InvalidateList(const LISTVIEW_INFO *infoPtr)
1744 LISTVIEW_InvalidateRect(infoPtr, NULL);
1747 static inline void LISTVIEW_InvalidateColumn(const LISTVIEW_INFO *infoPtr, INT nColumn)
1749 RECT rcCol;
1751 if(!is_redrawing(infoPtr)) return;
1752 LISTVIEW_GetHeaderRect(infoPtr, nColumn, &rcCol);
1753 rcCol.top = infoPtr->rcList.top;
1754 rcCol.bottom = infoPtr->rcList.bottom;
1755 LISTVIEW_InvalidateRect(infoPtr, &rcCol);
1758 /***
1759 * DESCRIPTION:
1760 * Retrieves the number of items that can fit vertically in the client area.
1762 * PARAMETER(S):
1763 * [I] infoPtr : valid pointer to the listview structure
1765 * RETURN:
1766 * Number of items per row.
1768 static inline INT LISTVIEW_GetCountPerRow(const LISTVIEW_INFO *infoPtr)
1770 INT nListWidth = infoPtr->rcList.right - infoPtr->rcList.left;
1772 return max(nListWidth/(infoPtr->nItemWidth ? infoPtr->nItemWidth : 1), 1);
1775 /***
1776 * DESCRIPTION:
1777 * Retrieves the number of items that can fit horizontally in the client
1778 * area.
1780 * PARAMETER(S):
1781 * [I] infoPtr : valid pointer to the listview structure
1783 * RETURN:
1784 * Number of items per column.
1786 static inline INT LISTVIEW_GetCountPerColumn(const LISTVIEW_INFO *infoPtr)
1788 INT nListHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
1790 return max(nListHeight / infoPtr->nItemHeight, 1);
1794 /*************************************************************************
1795 * LISTVIEW_ProcessLetterKeys
1797 * Processes keyboard messages generated by pressing the letter keys
1798 * on the keyboard.
1799 * What this does is perform a case insensitive search from the
1800 * current position with the following quirks:
1801 * - If two chars or more are pressed in quick succession we search
1802 * for the corresponding string (e.g. 'abc').
1803 * - If there is a delay we wipe away the current search string and
1804 * restart with just that char.
1805 * - If the user keeps pressing the same character, whether slowly or
1806 * fast, so that the search string is entirely composed of this
1807 * character ('aaaaa' for instance), then we search for first item
1808 * that starting with that character.
1809 * - If the user types the above character in quick succession, then
1810 * we must also search for the corresponding string ('aaaaa'), and
1811 * go to that string if there is a match.
1813 * PARAMETERS
1814 * [I] hwnd : handle to the window
1815 * [I] charCode : the character code, the actual character
1816 * [I] keyData : key data
1818 * RETURNS
1820 * Zero.
1822 * BUGS
1824 * - The current implementation has a list of characters it will
1825 * accept and it ignores everything else. In particular it will
1826 * ignore accentuated characters which seems to match what
1827 * Windows does. But I'm not sure it makes sense to follow
1828 * Windows there.
1829 * - We don't sound a beep when the search fails.
1831 * SEE ALSO
1833 * TREEVIEW_ProcessLetterKeys
1835 static INT LISTVIEW_ProcessLetterKeys(LISTVIEW_INFO *infoPtr, WPARAM charCode, LPARAM keyData)
1837 WCHAR buffer[MAX_PATH];
1838 INT endidx, startidx;
1839 DWORD prevTime;
1840 LVITEMW item;
1841 INT nItem;
1842 INT diff;
1844 /* simple parameter checking */
1845 if (!charCode || !keyData || infoPtr->nItemCount == 0) return 0;
1847 /* only allow the valid WM_CHARs through */
1848 if (!isalnumW(charCode) &&
1849 charCode != '.' && charCode != '`' && charCode != '!' &&
1850 charCode != '@' && charCode != '#' && charCode != '$' &&
1851 charCode != '%' && charCode != '^' && charCode != '&' &&
1852 charCode != '*' && charCode != '(' && charCode != ')' &&
1853 charCode != '-' && charCode != '_' && charCode != '+' &&
1854 charCode != '=' && charCode != '\\'&& charCode != ']' &&
1855 charCode != '}' && charCode != '[' && charCode != '{' &&
1856 charCode != '/' && charCode != '?' && charCode != '>' &&
1857 charCode != '<' && charCode != ',' && charCode != '~')
1858 return 0;
1860 /* update the search parameters */
1861 prevTime = infoPtr->lastKeyPressTimestamp;
1862 infoPtr->lastKeyPressTimestamp = GetTickCount();
1863 diff = infoPtr->lastKeyPressTimestamp - prevTime;
1865 if (diff >= 0 && diff < KEY_DELAY)
1867 if (infoPtr->nSearchParamLength < MAX_PATH - 1)
1868 infoPtr->szSearchParam[infoPtr->nSearchParamLength++] = charCode;
1870 if (infoPtr->charCode != charCode)
1871 infoPtr->charCode = charCode = 0;
1873 else
1875 infoPtr->charCode = charCode;
1876 infoPtr->szSearchParam[0] = charCode;
1877 infoPtr->nSearchParamLength = 1;
1880 /* and search from the current position */
1881 nItem = -1;
1882 endidx = infoPtr->nItemCount;
1884 /* should start from next after focused item, so next item that matches
1885 will be selected, if there isn't any and focused matches it will be selected
1886 on second search stage from beginning of the list */
1887 if (infoPtr->nFocusedItem >= 0 && infoPtr->nItemCount > 1)
1888 startidx = infoPtr->nFocusedItem + 1;
1889 else
1890 startidx = 0;
1892 /* let application handle this for virtual listview */
1893 if (infoPtr->dwStyle & LVS_OWNERDATA)
1895 NMLVFINDITEMW nmlv;
1897 memset(&nmlv.lvfi, 0, sizeof(nmlv.lvfi));
1898 nmlv.lvfi.flags = (LVFI_WRAP | LVFI_PARTIAL);
1899 nmlv.lvfi.psz = infoPtr->szSearchParam;
1900 nmlv.iStart = startidx;
1902 infoPtr->szSearchParam[infoPtr->nSearchParamLength] = 0;
1904 nItem = notify_hdr(infoPtr, LVN_ODFINDITEMW, (LPNMHDR)&nmlv.hdr);
1906 else
1908 INT i = startidx;
1910 /* first search in [startidx, endidx), on failure continue in [0, startidx) */
1911 while (1)
1913 /* start from first item if not found with >= startidx */
1914 if (i == infoPtr->nItemCount && startidx > 0)
1916 endidx = startidx;
1917 startidx = 0;
1920 for (i = startidx; i < endidx; i++)
1922 /* retrieve text */
1923 item.mask = LVIF_TEXT;
1924 item.iItem = i;
1925 item.iSubItem = 0;
1926 item.pszText = buffer;
1927 item.cchTextMax = MAX_PATH;
1928 if (!LISTVIEW_GetItemW(infoPtr, &item)) return 0;
1930 if (lstrncmpiW(item.pszText, infoPtr->szSearchParam, infoPtr->nSearchParamLength) == 0)
1932 nItem = i;
1933 break;
1935 else if (nItem == -1 && lstrncmpiW(item.pszText, infoPtr->szSearchParam, 1) == 0)
1937 /* this would work but we must keep looking for a longer match */
1938 nItem = i;
1942 if ( nItem != -1 || /* found something */
1943 endidx != infoPtr->nItemCount || /* second search done */
1944 (startidx == 0 && endidx == infoPtr->nItemCount) /* full range for first search */ )
1945 break;
1949 if (nItem != -1)
1950 LISTVIEW_KeySelection(infoPtr, nItem, FALSE);
1952 return 0;
1955 /*************************************************************************
1956 * LISTVIEW_UpdateHeaderSize [Internal]
1958 * Function to resize the header control
1960 * PARAMS
1961 * [I] hwnd : handle to a window
1962 * [I] nNewScrollPos : scroll pos to set
1964 * RETURNS
1965 * None.
1967 static void LISTVIEW_UpdateHeaderSize(const LISTVIEW_INFO *infoPtr, INT nNewScrollPos)
1969 RECT winRect;
1970 POINT point[2];
1972 TRACE("nNewScrollPos=%d\n", nNewScrollPos);
1974 if (!infoPtr->hwndHeader) return;
1976 GetWindowRect(infoPtr->hwndHeader, &winRect);
1977 point[0].x = winRect.left;
1978 point[0].y = winRect.top;
1979 point[1].x = winRect.right;
1980 point[1].y = winRect.bottom;
1982 MapWindowPoints(HWND_DESKTOP, infoPtr->hwndSelf, point, 2);
1983 point[0].x = -nNewScrollPos;
1984 point[1].x += nNewScrollPos;
1986 SetWindowPos(infoPtr->hwndHeader,0,
1987 point[0].x,point[0].y,point[1].x,point[1].y,
1988 (infoPtr->dwStyle & LVS_NOCOLUMNHEADER) ? SWP_HIDEWINDOW : SWP_SHOWWINDOW |
1989 SWP_NOZORDER | SWP_NOACTIVATE);
1992 /***
1993 * DESCRIPTION:
1994 * Update the scrollbars. This functions should be called whenever
1995 * the content, size or view changes.
1997 * PARAMETER(S):
1998 * [I] infoPtr : valid pointer to the listview structure
2000 * RETURN:
2001 * None
2003 static void LISTVIEW_UpdateScroll(const LISTVIEW_INFO *infoPtr)
2005 SCROLLINFO horzInfo, vertInfo;
2006 INT dx, dy;
2008 if ((infoPtr->dwStyle & LVS_NOSCROLL) || !is_redrawing(infoPtr)) return;
2010 ZeroMemory(&horzInfo, sizeof(SCROLLINFO));
2011 horzInfo.cbSize = sizeof(SCROLLINFO);
2012 horzInfo.nPage = infoPtr->rcList.right - infoPtr->rcList.left;
2014 /* for now, we'll set info.nMax to the _count_, and adjust it later */
2015 if (infoPtr->uView == LV_VIEW_LIST)
2017 INT nPerCol = LISTVIEW_GetCountPerColumn(infoPtr);
2018 horzInfo.nMax = (infoPtr->nItemCount + nPerCol - 1) / nPerCol;
2020 /* scroll by at least one column per page */
2021 if(horzInfo.nPage < infoPtr->nItemWidth)
2022 horzInfo.nPage = infoPtr->nItemWidth;
2024 if (infoPtr->nItemWidth)
2025 horzInfo.nPage /= infoPtr->nItemWidth;
2027 else if (infoPtr->uView == LV_VIEW_DETAILS)
2029 horzInfo.nMax = infoPtr->nItemWidth;
2031 else /* LV_VIEW_ICON, or LV_VIEW_SMALLICON */
2033 RECT rcView;
2035 if (LISTVIEW_GetViewRect(infoPtr, &rcView)) horzInfo.nMax = rcView.right - rcView.left;
2038 if (LISTVIEW_IsHeaderEnabled(infoPtr))
2040 if (DPA_GetPtrCount(infoPtr->hdpaColumns))
2042 RECT rcHeader;
2043 INT index;
2045 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX,
2046 DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, 0);
2048 LISTVIEW_GetHeaderRect(infoPtr, index, &rcHeader);
2049 horzInfo.nMax = rcHeader.right;
2050 TRACE("horzInfo.nMax=%d\n", horzInfo.nMax);
2054 horzInfo.fMask = SIF_RANGE | SIF_PAGE;
2055 horzInfo.nMax = max(horzInfo.nMax - 1, 0);
2056 dx = GetScrollPos(infoPtr->hwndSelf, SB_HORZ);
2057 dx -= SetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &horzInfo, TRUE);
2058 TRACE("horzInfo=%s\n", debugscrollinfo(&horzInfo));
2060 /* Setting the horizontal scroll can change the listview size
2061 * (and potentially everything else) so we need to recompute
2062 * everything again for the vertical scroll
2065 ZeroMemory(&vertInfo, sizeof(SCROLLINFO));
2066 vertInfo.cbSize = sizeof(SCROLLINFO);
2067 vertInfo.nPage = infoPtr->rcList.bottom - infoPtr->rcList.top;
2069 if (infoPtr->uView == LV_VIEW_DETAILS)
2071 vertInfo.nMax = infoPtr->nItemCount;
2073 /* scroll by at least one page */
2074 if(vertInfo.nPage < infoPtr->nItemHeight)
2075 vertInfo.nPage = infoPtr->nItemHeight;
2077 if (infoPtr->nItemHeight > 0)
2078 vertInfo.nPage /= infoPtr->nItemHeight;
2080 else if (infoPtr->uView != LV_VIEW_LIST) /* LV_VIEW_ICON, or LV_VIEW_SMALLICON */
2082 RECT rcView;
2084 if (LISTVIEW_GetViewRect(infoPtr, &rcView)) vertInfo.nMax = rcView.bottom - rcView.top;
2087 vertInfo.fMask = SIF_RANGE | SIF_PAGE;
2088 vertInfo.nMax = max(vertInfo.nMax - 1, 0);
2089 dy = GetScrollPos(infoPtr->hwndSelf, SB_VERT);
2090 dy -= SetScrollInfo(infoPtr->hwndSelf, SB_VERT, &vertInfo, TRUE);
2091 TRACE("vertInfo=%s\n", debugscrollinfo(&vertInfo));
2093 /* Change of the range may have changed the scroll pos. If so move the content */
2094 if (dx != 0 || dy != 0)
2096 RECT listRect;
2097 listRect = infoPtr->rcList;
2098 ScrollWindowEx(infoPtr->hwndSelf, dx, dy, &listRect, &listRect, 0, 0,
2099 SW_ERASE | SW_INVALIDATE);
2102 /* Update the Header Control */
2103 if (infoPtr->hwndHeader)
2105 horzInfo.fMask = SIF_POS;
2106 GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &horzInfo);
2107 LISTVIEW_UpdateHeaderSize(infoPtr, horzInfo.nPos);
2112 /***
2113 * DESCRIPTION:
2114 * Shows/hides the focus rectangle.
2116 * PARAMETER(S):
2117 * [I] infoPtr : valid pointer to the listview structure
2118 * [I] fShow : TRUE to show the focus, FALSE to hide it.
2120 * RETURN:
2121 * None
2123 static void LISTVIEW_ShowFocusRect(const LISTVIEW_INFO *infoPtr, BOOL fShow)
2125 HDC hdc;
2127 TRACE("fShow=%d, nItem=%d\n", fShow, infoPtr->nFocusedItem);
2129 if (infoPtr->nFocusedItem < 0) return;
2131 /* we need some gymnastics in ICON mode to handle large items */
2132 if (infoPtr->uView == LV_VIEW_ICON)
2134 RECT rcBox;
2136 LISTVIEW_GetItemBox(infoPtr, infoPtr->nFocusedItem, &rcBox);
2137 if ((rcBox.bottom - rcBox.top) > infoPtr->nItemHeight)
2139 LISTVIEW_InvalidateRect(infoPtr, &rcBox);
2140 return;
2144 if (!(hdc = GetDC(infoPtr->hwndSelf))) return;
2146 /* for some reason, owner draw should work only in report mode */
2147 if ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (infoPtr->uView == LV_VIEW_DETAILS))
2149 DRAWITEMSTRUCT dis;
2150 LVITEMW item;
2152 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
2153 HFONT hOldFont = SelectObject(hdc, hFont);
2155 item.iItem = infoPtr->nFocusedItem;
2156 item.iSubItem = 0;
2157 item.mask = LVIF_PARAM;
2158 if (!LISTVIEW_GetItemW(infoPtr, &item)) goto done;
2160 ZeroMemory(&dis, sizeof(dis));
2161 dis.CtlType = ODT_LISTVIEW;
2162 dis.CtlID = (UINT)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
2163 dis.itemID = item.iItem;
2164 dis.itemAction = ODA_FOCUS;
2165 if (fShow) dis.itemState |= ODS_FOCUS;
2166 dis.hwndItem = infoPtr->hwndSelf;
2167 dis.hDC = hdc;
2168 LISTVIEW_GetItemBox(infoPtr, dis.itemID, &dis.rcItem);
2169 dis.itemData = item.lParam;
2171 SendMessageW(infoPtr->hwndNotify, WM_DRAWITEM, dis.CtlID, (LPARAM)&dis);
2173 SelectObject(hdc, hOldFont);
2175 else
2177 LISTVIEW_DrawFocusRect(infoPtr, hdc);
2179 done:
2180 ReleaseDC(infoPtr->hwndSelf, hdc);
2183 /***
2184 * Invalidates all visible selected items.
2186 static void LISTVIEW_InvalidateSelectedItems(const LISTVIEW_INFO *infoPtr)
2188 ITERATOR i;
2190 iterator_frameditems(&i, infoPtr, &infoPtr->rcList);
2191 while(iterator_next(&i))
2193 if (LISTVIEW_GetItemState(infoPtr, i.nItem, LVIS_SELECTED))
2194 LISTVIEW_InvalidateItem(infoPtr, i.nItem);
2196 iterator_destroy(&i);
2200 /***
2201 * DESCRIPTION: [INTERNAL]
2202 * Computes an item's (left,top) corner, relative to rcView.
2203 * That is, the position has NOT been made relative to the Origin.
2204 * This is deliberate, to avoid computing the Origin over, and
2205 * over again, when this function is called in a loop. Instead,
2206 * one can factor the computation of the Origin before the loop,
2207 * and offset the value returned by this function, on every iteration.
2209 * PARAMETER(S):
2210 * [I] infoPtr : valid pointer to the listview structure
2211 * [I] nItem : item number
2212 * [O] lpptOrig : item top, left corner
2214 * RETURN:
2215 * None.
2217 static void LISTVIEW_GetItemOrigin(const LISTVIEW_INFO *infoPtr, INT nItem, LPPOINT lpptPosition)
2219 assert(nItem >= 0 && nItem < infoPtr->nItemCount);
2221 if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
2223 lpptPosition->x = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosX, nItem);
2224 lpptPosition->y = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosY, nItem);
2226 else if (infoPtr->uView == LV_VIEW_LIST)
2228 INT nCountPerColumn = LISTVIEW_GetCountPerColumn(infoPtr);
2229 lpptPosition->x = nItem / nCountPerColumn * infoPtr->nItemWidth;
2230 lpptPosition->y = nItem % nCountPerColumn * infoPtr->nItemHeight;
2232 else /* LV_VIEW_DETAILS */
2234 lpptPosition->x = REPORT_MARGINX;
2235 /* item is always at zero indexed column */
2236 if (DPA_GetPtrCount(infoPtr->hdpaColumns) > 0)
2237 lpptPosition->x += LISTVIEW_GetColumnInfo(infoPtr, 0)->rcHeader.left;
2238 lpptPosition->y = nItem * infoPtr->nItemHeight;
2242 /***
2243 * DESCRIPTION: [INTERNAL]
2244 * Compute the rectangles of an item. This is to localize all
2245 * the computations in one place. If you are not interested in some
2246 * of these values, simply pass in a NULL -- the function is smart
2247 * enough to compute only what's necessary. The function computes
2248 * the standard rectangles (BOUNDS, ICON, LABEL) plus a non-standard
2249 * one, the BOX rectangle. This rectangle is very cheap to compute,
2250 * and is guaranteed to contain all the other rectangles. Computing
2251 * the ICON rect is also cheap, but all the others are potentially
2252 * expensive. This gives an easy and effective optimization when
2253 * searching (like point inclusion, or rectangle intersection):
2254 * first test against the BOX, and if TRUE, test against the desired
2255 * rectangle.
2256 * If the function does not have all the necessary information
2257 * to computed the requested rectangles, will crash with a
2258 * failed assertion. This is done so we catch all programming
2259 * errors, given that the function is called only from our code.
2261 * We have the following 'special' meanings for a few fields:
2262 * * If LVIS_FOCUSED is set, we assume the item has the focus
2263 * This is important in ICON mode, where it might get a larger
2264 * then usual rectangle
2266 * Please note that subitem support works only in REPORT mode.
2268 * PARAMETER(S):
2269 * [I] infoPtr : valid pointer to the listview structure
2270 * [I] lpLVItem : item to compute the measures for
2271 * [O] lprcBox : ptr to Box rectangle
2272 * Same as LVM_GETITEMRECT with LVIR_BOUNDS
2273 * [0] lprcSelectBox : ptr to select box rectangle
2274 * Same as LVM_GETITEMRECT with LVIR_SELECTEDBOUNDS
2275 * [O] lprcIcon : ptr to Icon rectangle
2276 * Same as LVM_GETITEMRECT with LVIR_ICON
2277 * [O] lprcStateIcon: ptr to State Icon rectangle
2278 * [O] lprcLabel : ptr to Label rectangle
2279 * Same as LVM_GETITEMRECT with LVIR_LABEL
2281 * RETURN:
2282 * None.
2284 static void LISTVIEW_GetItemMetrics(const LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem,
2285 LPRECT lprcBox, LPRECT lprcSelectBox,
2286 LPRECT lprcIcon, LPRECT lprcStateIcon, LPRECT lprcLabel)
2288 BOOL doSelectBox = FALSE, doIcon = FALSE, doLabel = FALSE, oversizedBox = FALSE;
2289 RECT Box, SelectBox, Icon, Label;
2290 COLUMN_INFO *lpColumnInfo = NULL;
2291 SIZE labelSize = { 0, 0 };
2293 TRACE("(lpLVItem=%s)\n", debuglvitem_t(lpLVItem, TRUE));
2295 /* Be smart and try to figure out the minimum we have to do */
2296 if (lpLVItem->iSubItem) assert(infoPtr->uView == LV_VIEW_DETAILS);
2297 if (infoPtr->uView == LV_VIEW_ICON && (lprcBox || lprcLabel))
2299 assert((lpLVItem->mask & LVIF_STATE) && (lpLVItem->stateMask & LVIS_FOCUSED));
2300 if (lpLVItem->state & LVIS_FOCUSED) oversizedBox = doLabel = TRUE;
2302 if (lprcSelectBox) doSelectBox = TRUE;
2303 if (lprcLabel) doLabel = TRUE;
2304 if (doLabel || lprcIcon || lprcStateIcon) doIcon = TRUE;
2305 if (doSelectBox)
2307 doIcon = TRUE;
2308 doLabel = TRUE;
2311 /************************************************************/
2312 /* compute the box rectangle (it should be cheap to do) */
2313 /************************************************************/
2314 if (lpLVItem->iSubItem || infoPtr->uView == LV_VIEW_DETAILS)
2315 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, lpLVItem->iSubItem);
2317 if (lpLVItem->iSubItem)
2319 Box = lpColumnInfo->rcHeader;
2321 else
2323 Box.left = 0;
2324 Box.right = infoPtr->nItemWidth;
2326 Box.top = 0;
2327 Box.bottom = infoPtr->nItemHeight;
2329 /******************************************************************/
2330 /* compute ICON bounding box (ala LVM_GETITEMRECT) and STATEICON */
2331 /******************************************************************/
2332 if (doIcon)
2334 LONG state_width = 0;
2336 if (infoPtr->himlState && lpLVItem->iSubItem == 0)
2337 state_width = infoPtr->iconStateSize.cx;
2339 if (infoPtr->uView == LV_VIEW_ICON)
2341 Icon.left = Box.left + state_width;
2342 if (infoPtr->himlNormal)
2343 Icon.left += (infoPtr->nItemWidth - infoPtr->iconSize.cx - state_width) / 2;
2344 Icon.top = Box.top + ICON_TOP_PADDING;
2345 Icon.right = Icon.left;
2346 Icon.bottom = Icon.top;
2347 if (infoPtr->himlNormal)
2349 Icon.right += infoPtr->iconSize.cx;
2350 Icon.bottom += infoPtr->iconSize.cy;
2353 else /* LV_VIEW_SMALLICON, LV_VIEW_LIST or LV_VIEW_DETAILS */
2355 Icon.left = Box.left + state_width;
2357 if (infoPtr->uView == LV_VIEW_DETAILS && lpLVItem->iSubItem == 0)
2359 /* we need the indent in report mode */
2360 assert(lpLVItem->mask & LVIF_INDENT);
2361 Icon.left += infoPtr->iconSize.cx * lpLVItem->iIndent + REPORT_MARGINX;
2364 Icon.top = Box.top;
2365 Icon.right = Icon.left;
2366 if (infoPtr->himlSmall &&
2367 (!lpColumnInfo || lpLVItem->iSubItem == 0 || (lpColumnInfo->fmt & LVCFMT_IMAGE) ||
2368 ((infoPtr->dwLvExStyle & LVS_EX_SUBITEMIMAGES) && lpLVItem->iImage != I_IMAGECALLBACK)))
2369 Icon.right += infoPtr->iconSize.cx;
2370 Icon.bottom = Icon.top + infoPtr->iconSize.cy;
2372 if(lprcIcon) *lprcIcon = Icon;
2373 TRACE(" - icon=%s\n", wine_dbgstr_rect(&Icon));
2375 /* TODO: is this correct? */
2376 if (lprcStateIcon)
2378 lprcStateIcon->left = Icon.left - state_width;
2379 lprcStateIcon->right = Icon.left;
2380 lprcStateIcon->top = Icon.top;
2381 lprcStateIcon->bottom = lprcStateIcon->top + infoPtr->iconSize.cy;
2382 TRACE(" - state icon=%s\n", wine_dbgstr_rect(lprcStateIcon));
2385 else Icon.right = 0;
2387 /************************************************************/
2388 /* compute LABEL bounding box (ala LVM_GETITEMRECT) */
2389 /************************************************************/
2390 if (doLabel)
2392 /* calculate how far to the right can the label stretch */
2393 Label.right = Box.right;
2394 if (infoPtr->uView == LV_VIEW_DETAILS)
2396 if (lpLVItem->iSubItem == 0)
2398 /* we need a zero based rect here */
2399 Label = lpColumnInfo->rcHeader;
2400 OffsetRect(&Label, -Label.left, 0);
2404 if (lpLVItem->iSubItem || ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && infoPtr->uView == LV_VIEW_DETAILS))
2406 labelSize.cx = infoPtr->nItemWidth;
2407 labelSize.cy = infoPtr->nItemHeight;
2408 goto calc_label;
2411 /* we need the text in non owner draw mode */
2412 assert(lpLVItem->mask & LVIF_TEXT);
2413 if (is_text(lpLVItem->pszText))
2415 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
2416 HDC hdc = GetDC(infoPtr->hwndSelf);
2417 HFONT hOldFont = SelectObject(hdc, hFont);
2418 UINT uFormat;
2419 RECT rcText;
2421 /* compute rough rectangle where the label will go */
2422 SetRectEmpty(&rcText);
2423 rcText.right = infoPtr->nItemWidth - TRAILING_LABEL_PADDING;
2424 rcText.bottom = infoPtr->nItemHeight;
2425 if (infoPtr->uView == LV_VIEW_ICON)
2426 rcText.bottom -= ICON_TOP_PADDING + infoPtr->iconSize.cy + ICON_BOTTOM_PADDING;
2428 /* now figure out the flags */
2429 if (infoPtr->uView == LV_VIEW_ICON)
2430 uFormat = oversizedBox ? LV_FL_DT_FLAGS : LV_ML_DT_FLAGS;
2431 else
2432 uFormat = LV_SL_DT_FLAGS;
2434 DrawTextW (hdc, lpLVItem->pszText, -1, &rcText, uFormat | DT_CALCRECT);
2436 if (rcText.right != rcText.left)
2437 labelSize.cx = min(rcText.right - rcText.left + TRAILING_LABEL_PADDING, infoPtr->nItemWidth);
2439 labelSize.cy = rcText.bottom - rcText.top;
2441 SelectObject(hdc, hOldFont);
2442 ReleaseDC(infoPtr->hwndSelf, hdc);
2445 calc_label:
2446 if (infoPtr->uView == LV_VIEW_ICON)
2448 Label.left = Box.left + (infoPtr->nItemWidth - labelSize.cx) / 2;
2449 Label.top = Box.top + ICON_TOP_PADDING_HITABLE +
2450 infoPtr->iconSize.cy + ICON_BOTTOM_PADDING;
2451 Label.right = Label.left + labelSize.cx;
2452 Label.bottom = Label.top + infoPtr->nItemHeight;
2453 if (!oversizedBox && labelSize.cy > infoPtr->ntmHeight)
2455 labelSize.cy = min(Box.bottom - Label.top, labelSize.cy);
2456 labelSize.cy /= infoPtr->ntmHeight;
2457 labelSize.cy = max(labelSize.cy, 1);
2458 labelSize.cy *= infoPtr->ntmHeight;
2460 Label.bottom = Label.top + labelSize.cy + HEIGHT_PADDING;
2462 else if (infoPtr->uView == LV_VIEW_DETAILS)
2464 Label.left = Icon.right;
2465 Label.top = Box.top;
2466 Label.right = lpLVItem->iSubItem ? lpColumnInfo->rcHeader.right :
2467 lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left;
2468 Label.bottom = Label.top + infoPtr->nItemHeight;
2470 else /* LV_VIEW_SMALLICON or LV_VIEW_LIST */
2472 Label.left = Icon.right;
2473 Label.top = Box.top;
2474 Label.right = min(Label.left + labelSize.cx, Label.right);
2475 Label.bottom = Label.top + infoPtr->nItemHeight;
2478 if (lprcLabel) *lprcLabel = Label;
2479 TRACE(" - label=%s\n", wine_dbgstr_rect(&Label));
2482 /************************************************************/
2483 /* compute SELECT bounding box */
2484 /************************************************************/
2485 if (doSelectBox)
2487 if (infoPtr->uView == LV_VIEW_DETAILS)
2489 SelectBox.left = Icon.left;
2490 SelectBox.top = Box.top;
2491 SelectBox.bottom = Box.bottom;
2493 if (labelSize.cx)
2494 SelectBox.right = min(Label.left + labelSize.cx, Label.right);
2495 else
2496 SelectBox.right = min(Label.left + MAX_EMPTYTEXT_SELECT_WIDTH, Label.right);
2498 else
2500 UnionRect(&SelectBox, &Icon, &Label);
2502 if (lprcSelectBox) *lprcSelectBox = SelectBox;
2503 TRACE(" - select box=%s\n", wine_dbgstr_rect(&SelectBox));
2506 /* Fix the Box if necessary */
2507 if (lprcBox)
2509 if (oversizedBox) UnionRect(lprcBox, &Box, &Label);
2510 else *lprcBox = Box;
2512 TRACE(" - box=%s\n", wine_dbgstr_rect(&Box));
2515 /***
2516 * DESCRIPTION: [INTERNAL]
2518 * PARAMETER(S):
2519 * [I] infoPtr : valid pointer to the listview structure
2520 * [I] nItem : item number
2521 * [O] lprcBox : ptr to Box rectangle
2523 * RETURN:
2524 * None.
2526 static void LISTVIEW_GetItemBox(const LISTVIEW_INFO *infoPtr, INT nItem, LPRECT lprcBox)
2528 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
2529 POINT Position, Origin;
2530 LVITEMW lvItem;
2532 LISTVIEW_GetOrigin(infoPtr, &Origin);
2533 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
2535 /* Be smart and try to figure out the minimum we have to do */
2536 lvItem.mask = 0;
2537 if (infoPtr->uView == LV_VIEW_ICON && infoPtr->bFocus && LISTVIEW_GetItemState(infoPtr, nItem, LVIS_FOCUSED))
2538 lvItem.mask |= LVIF_TEXT;
2539 lvItem.iItem = nItem;
2540 lvItem.iSubItem = 0;
2541 lvItem.pszText = szDispText;
2542 lvItem.cchTextMax = DISP_TEXT_SIZE;
2543 if (lvItem.mask) LISTVIEW_GetItemW(infoPtr, &lvItem);
2544 if (infoPtr->uView == LV_VIEW_ICON)
2546 lvItem.mask |= LVIF_STATE;
2547 lvItem.stateMask = LVIS_FOCUSED;
2548 lvItem.state = (lvItem.mask & LVIF_TEXT ? LVIS_FOCUSED : 0);
2550 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, lprcBox, 0, 0, 0, 0);
2552 if (infoPtr->uView == LV_VIEW_DETAILS && infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT &&
2553 SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, 0, 0))
2555 OffsetRect(lprcBox, Origin.x, Position.y + Origin.y);
2557 else
2558 OffsetRect(lprcBox, Position.x + Origin.x, Position.y + Origin.y);
2561 /* LISTVIEW_MapIdToIndex helper */
2562 static INT CALLBACK MapIdSearchCompare(LPVOID p1, LPVOID p2, LPARAM lParam)
2564 ITEM_ID *id1 = (ITEM_ID*)p1;
2565 ITEM_ID *id2 = (ITEM_ID*)p2;
2567 if (id1->id == id2->id) return 0;
2569 return (id1->id < id2->id) ? -1 : 1;
2572 /***
2573 * DESCRIPTION:
2574 * Returns the item index for id specified.
2576 * PARAMETER(S):
2577 * [I] infoPtr : valid pointer to the listview structure
2578 * [I] iID : item id to get index for
2580 * RETURN:
2581 * Item index, or -1 on failure.
2583 static INT LISTVIEW_MapIdToIndex(const LISTVIEW_INFO *infoPtr, UINT iID)
2585 ITEM_ID ID;
2586 INT index;
2588 TRACE("iID=%d\n", iID);
2590 if (infoPtr->dwStyle & LVS_OWNERDATA) return -1;
2591 if (infoPtr->nItemCount == 0) return -1;
2593 ID.id = iID;
2594 index = DPA_Search(infoPtr->hdpaItemIds, &ID, -1, MapIdSearchCompare, 0, DPAS_SORTED);
2596 if (index != -1)
2598 ITEM_ID *lpID = DPA_GetPtr(infoPtr->hdpaItemIds, index);
2599 return DPA_GetPtrIndex(infoPtr->hdpaItems, lpID->item);
2602 return -1;
2605 /***
2606 * DESCRIPTION:
2607 * Returns the item id for index given.
2609 * PARAMETER(S):
2610 * [I] infoPtr : valid pointer to the listview structure
2611 * [I] iItem : item index to get id for
2613 * RETURN:
2614 * Item id.
2616 static DWORD LISTVIEW_MapIndexToId(const LISTVIEW_INFO *infoPtr, INT iItem)
2618 ITEM_INFO *lpItem;
2619 HDPA hdpaSubItems;
2621 TRACE("iItem=%d\n", iItem);
2623 if (infoPtr->dwStyle & LVS_OWNERDATA) return -1;
2624 if (iItem < 0 || iItem >= infoPtr->nItemCount) return -1;
2626 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, iItem);
2627 lpItem = DPA_GetPtr(hdpaSubItems, 0);
2629 return lpItem->id->id;
2632 /***
2633 * DESCRIPTION:
2634 * Returns the current icon position, and advances it along the top.
2635 * The returned position is not offset by Origin.
2637 * PARAMETER(S):
2638 * [I] infoPtr : valid pointer to the listview structure
2639 * [O] lpPos : will get the current icon position
2641 * RETURN:
2642 * None
2644 static void LISTVIEW_NextIconPosTop(LISTVIEW_INFO *infoPtr, LPPOINT lpPos)
2646 INT nListWidth = infoPtr->rcList.right - infoPtr->rcList.left;
2648 *lpPos = infoPtr->currIconPos;
2650 infoPtr->currIconPos.x += infoPtr->nItemWidth;
2651 if (infoPtr->currIconPos.x + infoPtr->nItemWidth <= nListWidth) return;
2653 infoPtr->currIconPos.x = 0;
2654 infoPtr->currIconPos.y += infoPtr->nItemHeight;
2658 /***
2659 * DESCRIPTION:
2660 * Returns the current icon position, and advances it down the left edge.
2661 * The returned position is not offset by Origin.
2663 * PARAMETER(S):
2664 * [I] infoPtr : valid pointer to the listview structure
2665 * [O] lpPos : will get the current icon position
2667 * RETURN:
2668 * None
2670 static void LISTVIEW_NextIconPosLeft(LISTVIEW_INFO *infoPtr, LPPOINT lpPos)
2672 INT nListHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
2674 *lpPos = infoPtr->currIconPos;
2676 infoPtr->currIconPos.y += infoPtr->nItemHeight;
2677 if (infoPtr->currIconPos.y + infoPtr->nItemHeight <= nListHeight) return;
2679 infoPtr->currIconPos.x += infoPtr->nItemWidth;
2680 infoPtr->currIconPos.y = 0;
2684 /***
2685 * DESCRIPTION:
2686 * Moves an icon to the specified position.
2687 * It takes care of invalidating the item, etc.
2689 * PARAMETER(S):
2690 * [I] infoPtr : valid pointer to the listview structure
2691 * [I] nItem : the item to move
2692 * [I] lpPos : the new icon position
2693 * [I] isNew : flags the item as being new
2695 * RETURN:
2696 * Success: TRUE
2697 * Failure: FALSE
2699 static BOOL LISTVIEW_MoveIconTo(const LISTVIEW_INFO *infoPtr, INT nItem, const POINT *lppt, BOOL isNew)
2701 POINT old;
2703 if (!isNew)
2705 old.x = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosX, nItem);
2706 old.y = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosY, nItem);
2708 if (lppt->x == old.x && lppt->y == old.y) return TRUE;
2709 LISTVIEW_InvalidateItem(infoPtr, nItem);
2712 /* Allocating a POINTER for every item is too resource intensive,
2713 * so we'll keep the (x,y) in different arrays */
2714 if (!DPA_SetPtr(infoPtr->hdpaPosX, nItem, (void *)(LONG_PTR)lppt->x)) return FALSE;
2715 if (!DPA_SetPtr(infoPtr->hdpaPosY, nItem, (void *)(LONG_PTR)lppt->y)) return FALSE;
2717 LISTVIEW_InvalidateItem(infoPtr, nItem);
2719 return TRUE;
2722 /***
2723 * DESCRIPTION:
2724 * Arranges listview items in icon display mode.
2726 * PARAMETER(S):
2727 * [I] infoPtr : valid pointer to the listview structure
2728 * [I] nAlignCode : alignment code
2730 * RETURN:
2731 * SUCCESS : TRUE
2732 * FAILURE : FALSE
2734 static BOOL LISTVIEW_Arrange(LISTVIEW_INFO *infoPtr, INT nAlignCode)
2736 void (*next_pos)(LISTVIEW_INFO *, LPPOINT);
2737 POINT pos;
2738 INT i;
2740 if (infoPtr->uView != LV_VIEW_ICON && infoPtr->uView != LV_VIEW_SMALLICON) return FALSE;
2742 TRACE("nAlignCode=%d\n", nAlignCode);
2744 if (nAlignCode == LVA_DEFAULT)
2746 if (infoPtr->dwStyle & LVS_ALIGNLEFT) nAlignCode = LVA_ALIGNLEFT;
2747 else nAlignCode = LVA_ALIGNTOP;
2750 switch (nAlignCode)
2752 case LVA_ALIGNLEFT: next_pos = LISTVIEW_NextIconPosLeft; break;
2753 case LVA_ALIGNTOP: next_pos = LISTVIEW_NextIconPosTop; break;
2754 case LVA_SNAPTOGRID: next_pos = LISTVIEW_NextIconPosTop; break; /* FIXME */
2755 default: return FALSE;
2758 infoPtr->bAutoarrange = TRUE;
2759 infoPtr->currIconPos.x = infoPtr->currIconPos.y = 0;
2760 for (i = 0; i < infoPtr->nItemCount; i++)
2762 next_pos(infoPtr, &pos);
2763 LISTVIEW_MoveIconTo(infoPtr, i, &pos, FALSE);
2766 return TRUE;
2769 /***
2770 * DESCRIPTION:
2771 * Retrieves the bounding rectangle of all the items, not offset by Origin.
2772 * For LVS_REPORT always returns empty rectangle.
2774 * PARAMETER(S):
2775 * [I] infoPtr : valid pointer to the listview structure
2776 * [O] lprcView : bounding rectangle
2778 * RETURN:
2779 * SUCCESS : TRUE
2780 * FAILURE : FALSE
2782 static void LISTVIEW_GetAreaRect(const LISTVIEW_INFO *infoPtr, LPRECT lprcView)
2784 INT i, x, y;
2786 SetRectEmpty(lprcView);
2788 switch (infoPtr->uView)
2790 case LV_VIEW_ICON:
2791 case LV_VIEW_SMALLICON:
2792 for (i = 0; i < infoPtr->nItemCount; i++)
2794 x = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosX, i);
2795 y = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosY, i);
2796 lprcView->right = max(lprcView->right, x);
2797 lprcView->bottom = max(lprcView->bottom, y);
2799 if (infoPtr->nItemCount > 0)
2801 lprcView->right += infoPtr->nItemWidth;
2802 lprcView->bottom += infoPtr->nItemHeight;
2804 break;
2806 case LV_VIEW_LIST:
2807 y = LISTVIEW_GetCountPerColumn(infoPtr);
2808 x = infoPtr->nItemCount / y;
2809 if (infoPtr->nItemCount % y) x++;
2810 lprcView->right = x * infoPtr->nItemWidth;
2811 lprcView->bottom = y * infoPtr->nItemHeight;
2812 break;
2816 /***
2817 * DESCRIPTION:
2818 * Retrieves the bounding rectangle of all the items.
2820 * PARAMETER(S):
2821 * [I] infoPtr : valid pointer to the listview structure
2822 * [O] lprcView : bounding rectangle
2824 * RETURN:
2825 * SUCCESS : TRUE
2826 * FAILURE : FALSE
2828 static BOOL LISTVIEW_GetViewRect(const LISTVIEW_INFO *infoPtr, LPRECT lprcView)
2830 POINT ptOrigin;
2832 TRACE("(lprcView=%p)\n", lprcView);
2834 if (!lprcView) return FALSE;
2836 LISTVIEW_GetAreaRect(infoPtr, lprcView);
2838 if (infoPtr->uView != LV_VIEW_DETAILS)
2840 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
2841 OffsetRect(lprcView, ptOrigin.x, ptOrigin.y);
2844 TRACE("lprcView=%s\n", wine_dbgstr_rect(lprcView));
2846 return TRUE;
2849 /***
2850 * DESCRIPTION:
2851 * Retrieves the subitem pointer associated with the subitem index.
2853 * PARAMETER(S):
2854 * [I] hdpaSubItems : DPA handle for a specific item
2855 * [I] nSubItem : index of subitem
2857 * RETURN:
2858 * SUCCESS : subitem pointer
2859 * FAILURE : NULL
2861 static SUBITEM_INFO* LISTVIEW_GetSubItemPtr(HDPA hdpaSubItems, INT nSubItem)
2863 SUBITEM_INFO *lpSubItem;
2864 INT i;
2866 /* we should binary search here if need be */
2867 for (i = 1; i < DPA_GetPtrCount(hdpaSubItems); i++)
2869 lpSubItem = DPA_GetPtr(hdpaSubItems, i);
2870 if (lpSubItem->iSubItem == nSubItem)
2871 return lpSubItem;
2874 return NULL;
2878 /***
2879 * DESCRIPTION:
2880 * Calculates the desired item width.
2882 * PARAMETER(S):
2883 * [I] infoPtr : valid pointer to the listview structure
2885 * RETURN:
2886 * The desired item width.
2888 static INT LISTVIEW_CalculateItemWidth(const LISTVIEW_INFO *infoPtr)
2890 INT nItemWidth = 0;
2892 TRACE("uView=%d\n", infoPtr->uView);
2894 if (infoPtr->uView == LV_VIEW_ICON)
2895 nItemWidth = infoPtr->iconSpacing.cx;
2896 else if (infoPtr->uView == LV_VIEW_DETAILS)
2898 if (DPA_GetPtrCount(infoPtr->hdpaColumns) > 0)
2900 RECT rcHeader;
2901 INT index;
2903 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX,
2904 DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, 0);
2906 LISTVIEW_GetHeaderRect(infoPtr, index, &rcHeader);
2907 nItemWidth = rcHeader.right;
2910 else /* LV_VIEW_SMALLICON, or LV_VIEW_LIST */
2912 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
2913 LVITEMW lvItem;
2914 INT i;
2916 lvItem.mask = LVIF_TEXT;
2917 lvItem.iSubItem = 0;
2919 for (i = 0; i < infoPtr->nItemCount; i++)
2921 lvItem.iItem = i;
2922 lvItem.pszText = szDispText;
2923 lvItem.cchTextMax = DISP_TEXT_SIZE;
2924 if (LISTVIEW_GetItemW(infoPtr, &lvItem))
2925 nItemWidth = max(LISTVIEW_GetStringWidthT(infoPtr, lvItem.pszText, TRUE),
2926 nItemWidth);
2929 if (infoPtr->himlSmall) nItemWidth += infoPtr->iconSize.cx;
2930 if (infoPtr->himlState) nItemWidth += infoPtr->iconStateSize.cx;
2932 nItemWidth = max(DEFAULT_COLUMN_WIDTH, nItemWidth + WIDTH_PADDING);
2935 return nItemWidth;
2938 /***
2939 * DESCRIPTION:
2940 * Calculates the desired item height.
2942 * PARAMETER(S):
2943 * [I] infoPtr : valid pointer to the listview structure
2945 * RETURN:
2946 * The desired item height.
2948 static INT LISTVIEW_CalculateItemHeight(const LISTVIEW_INFO *infoPtr)
2950 INT nItemHeight;
2952 TRACE("uView=%d\n", infoPtr->uView);
2954 if (infoPtr->uView == LV_VIEW_ICON)
2955 nItemHeight = infoPtr->iconSpacing.cy;
2956 else
2958 nItemHeight = infoPtr->ntmHeight;
2959 if (infoPtr->uView == LV_VIEW_DETAILS && infoPtr->dwLvExStyle & LVS_EX_GRIDLINES)
2960 nItemHeight++;
2961 if (infoPtr->himlState)
2962 nItemHeight = max(nItemHeight, infoPtr->iconStateSize.cy);
2963 if (infoPtr->himlSmall)
2964 nItemHeight = max(nItemHeight, infoPtr->iconSize.cy);
2965 if (infoPtr->himlState || infoPtr->himlSmall)
2966 nItemHeight += HEIGHT_PADDING;
2967 if (infoPtr->nMeasureItemHeight > 0)
2968 nItemHeight = infoPtr->nMeasureItemHeight;
2971 return max(nItemHeight, 1);
2974 /***
2975 * DESCRIPTION:
2976 * Updates the width, and height of an item.
2978 * PARAMETER(S):
2979 * [I] infoPtr : valid pointer to the listview structure
2981 * RETURN:
2982 * None.
2984 static inline void LISTVIEW_UpdateItemSize(LISTVIEW_INFO *infoPtr)
2986 infoPtr->nItemWidth = LISTVIEW_CalculateItemWidth(infoPtr);
2987 infoPtr->nItemHeight = LISTVIEW_CalculateItemHeight(infoPtr);
2991 /***
2992 * DESCRIPTION:
2993 * Retrieves and saves important text metrics info for the current
2994 * Listview font.
2996 * PARAMETER(S):
2997 * [I] infoPtr : valid pointer to the listview structure
3000 static void LISTVIEW_SaveTextMetrics(LISTVIEW_INFO *infoPtr)
3002 HDC hdc = GetDC(infoPtr->hwndSelf);
3003 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
3004 HFONT hOldFont = SelectObject(hdc, hFont);
3005 TEXTMETRICW tm;
3006 SIZE sz;
3008 if (GetTextMetricsW(hdc, &tm))
3010 infoPtr->ntmHeight = tm.tmHeight;
3011 infoPtr->ntmMaxCharWidth = tm.tmMaxCharWidth;
3014 if (GetTextExtentPoint32A(hdc, "...", 3, &sz))
3015 infoPtr->nEllipsisWidth = sz.cx;
3017 SelectObject(hdc, hOldFont);
3018 ReleaseDC(infoPtr->hwndSelf, hdc);
3020 TRACE("tmHeight=%d\n", infoPtr->ntmHeight);
3023 /***
3024 * DESCRIPTION:
3025 * A compare function for ranges
3027 * PARAMETER(S)
3028 * [I] range1 : pointer to range 1;
3029 * [I] range2 : pointer to range 2;
3030 * [I] flags : flags
3032 * RETURNS:
3033 * > 0 : if range 1 > range 2
3034 * < 0 : if range 2 > range 1
3035 * = 0 : if range intersects range 2
3037 static INT CALLBACK ranges_cmp(LPVOID range1, LPVOID range2, LPARAM flags)
3039 INT cmp;
3041 if (((RANGE*)range1)->upper <= ((RANGE*)range2)->lower)
3042 cmp = -1;
3043 else if (((RANGE*)range2)->upper <= ((RANGE*)range1)->lower)
3044 cmp = 1;
3045 else
3046 cmp = 0;
3048 TRACE("range1=%s, range2=%s, cmp=%d\n", debugrange(range1), debugrange(range2), cmp);
3050 return cmp;
3053 #define ranges_check(ranges, desc) if (TRACE_ON(listview)) ranges_assert(ranges, desc, __FILE__, __LINE__)
3055 static void ranges_assert(RANGES ranges, LPCSTR desc, const char *file, int line)
3057 INT i;
3058 RANGE *prev, *curr;
3060 TRACE("*** Checking %s:%d:%s ***\n", file, line, desc);
3061 assert (ranges);
3062 assert (DPA_GetPtrCount(ranges->hdpa) >= 0);
3063 ranges_dump(ranges);
3064 if (DPA_GetPtrCount(ranges->hdpa) > 0)
3066 prev = DPA_GetPtr(ranges->hdpa, 0);
3067 assert (prev->lower >= 0 && prev->lower < prev->upper);
3068 for (i = 1; i < DPA_GetPtrCount(ranges->hdpa); i++)
3070 curr = DPA_GetPtr(ranges->hdpa, i);
3071 assert (prev->upper <= curr->lower);
3072 assert (curr->lower < curr->upper);
3073 prev = curr;
3076 TRACE("--- Done checking---\n");
3079 static RANGES ranges_create(int count)
3081 RANGES ranges = Alloc(sizeof(struct tagRANGES));
3082 if (!ranges) return NULL;
3083 ranges->hdpa = DPA_Create(count);
3084 if (ranges->hdpa) return ranges;
3085 Free(ranges);
3086 return NULL;
3089 static void ranges_clear(RANGES ranges)
3091 INT i;
3093 for(i = 0; i < DPA_GetPtrCount(ranges->hdpa); i++)
3094 Free(DPA_GetPtr(ranges->hdpa, i));
3095 DPA_DeleteAllPtrs(ranges->hdpa);
3099 static void ranges_destroy(RANGES ranges)
3101 if (!ranges) return;
3102 ranges_clear(ranges);
3103 DPA_Destroy(ranges->hdpa);
3104 Free(ranges);
3107 static RANGES ranges_clone(RANGES ranges)
3109 RANGES clone;
3110 INT i;
3112 if (!(clone = ranges_create(DPA_GetPtrCount(ranges->hdpa)))) goto fail;
3114 for (i = 0; i < DPA_GetPtrCount(ranges->hdpa); i++)
3116 RANGE *newrng = Alloc(sizeof(RANGE));
3117 if (!newrng) goto fail;
3118 *newrng = *((RANGE*)DPA_GetPtr(ranges->hdpa, i));
3119 DPA_SetPtr(clone->hdpa, i, newrng);
3121 return clone;
3123 fail:
3124 TRACE ("clone failed\n");
3125 ranges_destroy(clone);
3126 return NULL;
3129 static RANGES ranges_diff(RANGES ranges, RANGES sub)
3131 INT i;
3133 for (i = 0; i < DPA_GetPtrCount(sub->hdpa); i++)
3134 ranges_del(ranges, *((RANGE *)DPA_GetPtr(sub->hdpa, i)));
3136 return ranges;
3139 static void ranges_dump(RANGES ranges)
3141 INT i;
3143 for (i = 0; i < DPA_GetPtrCount(ranges->hdpa); i++)
3144 TRACE(" %s\n", debugrange(DPA_GetPtr(ranges->hdpa, i)));
3147 static inline BOOL ranges_contain(RANGES ranges, INT nItem)
3149 RANGE srchrng = { nItem, nItem + 1 };
3151 TRACE("(nItem=%d)\n", nItem);
3152 ranges_check(ranges, "before contain");
3153 return DPA_Search(ranges->hdpa, &srchrng, 0, ranges_cmp, 0, DPAS_SORTED) != -1;
3156 static INT ranges_itemcount(RANGES ranges)
3158 INT i, count = 0;
3160 for (i = 0; i < DPA_GetPtrCount(ranges->hdpa); i++)
3162 RANGE *sel = DPA_GetPtr(ranges->hdpa, i);
3163 count += sel->upper - sel->lower;
3166 return count;
3169 static BOOL ranges_shift(RANGES ranges, INT nItem, INT delta, INT nUpper)
3171 RANGE srchrng = { nItem, nItem + 1 }, *chkrng;
3172 INT index;
3174 index = DPA_Search(ranges->hdpa, &srchrng, 0, ranges_cmp, 0, DPAS_SORTED | DPAS_INSERTAFTER);
3175 if (index == -1) return TRUE;
3177 for (; index < DPA_GetPtrCount(ranges->hdpa); index++)
3179 chkrng = DPA_GetPtr(ranges->hdpa, index);
3180 if (chkrng->lower >= nItem)
3181 chkrng->lower = max(min(chkrng->lower + delta, nUpper - 1), 0);
3182 if (chkrng->upper > nItem)
3183 chkrng->upper = max(min(chkrng->upper + delta, nUpper), 0);
3185 return TRUE;
3188 static BOOL ranges_add(RANGES ranges, RANGE range)
3190 RANGE srchrgn;
3191 INT index;
3193 TRACE("(%s)\n", debugrange(&range));
3194 ranges_check(ranges, "before add");
3196 /* try find overlapping regions first */
3197 srchrgn.lower = range.lower - 1;
3198 srchrgn.upper = range.upper + 1;
3199 index = DPA_Search(ranges->hdpa, &srchrgn, 0, ranges_cmp, 0, DPAS_SORTED);
3201 if (index == -1)
3203 RANGE *newrgn;
3205 TRACE("Adding new range\n");
3207 /* create the brand new range to insert */
3208 newrgn = Alloc(sizeof(RANGE));
3209 if(!newrgn) goto fail;
3210 *newrgn = range;
3212 /* figure out where to insert it */
3213 index = DPA_Search(ranges->hdpa, newrgn, 0, ranges_cmp, 0, DPAS_SORTED | DPAS_INSERTAFTER);
3214 TRACE("index=%d\n", index);
3215 if (index == -1) index = 0;
3217 /* and get it over with */
3218 if (DPA_InsertPtr(ranges->hdpa, index, newrgn) == -1)
3220 Free(newrgn);
3221 goto fail;
3224 else
3226 RANGE *chkrgn, *mrgrgn;
3227 INT fromindex, mergeindex;
3229 chkrgn = DPA_GetPtr(ranges->hdpa, index);
3230 TRACE("Merge with %s @%d\n", debugrange(chkrgn), index);
3232 chkrgn->lower = min(range.lower, chkrgn->lower);
3233 chkrgn->upper = max(range.upper, chkrgn->upper);
3235 TRACE("New range %s @%d\n", debugrange(chkrgn), index);
3237 /* merge now common ranges */
3238 fromindex = 0;
3239 srchrgn.lower = chkrgn->lower - 1;
3240 srchrgn.upper = chkrgn->upper + 1;
3244 mergeindex = DPA_Search(ranges->hdpa, &srchrgn, fromindex, ranges_cmp, 0, 0);
3245 if (mergeindex == -1) break;
3246 if (mergeindex == index)
3248 fromindex = index + 1;
3249 continue;
3252 TRACE("Merge with index %i\n", mergeindex);
3254 mrgrgn = DPA_GetPtr(ranges->hdpa, mergeindex);
3255 chkrgn->lower = min(chkrgn->lower, mrgrgn->lower);
3256 chkrgn->upper = max(chkrgn->upper, mrgrgn->upper);
3257 Free(mrgrgn);
3258 DPA_DeletePtr(ranges->hdpa, mergeindex);
3259 if (mergeindex < index) index --;
3260 } while(1);
3263 ranges_check(ranges, "after add");
3264 return TRUE;
3266 fail:
3267 ranges_check(ranges, "failed add");
3268 return FALSE;
3271 static BOOL ranges_del(RANGES ranges, RANGE range)
3273 RANGE *chkrgn;
3274 INT index;
3276 TRACE("(%s)\n", debugrange(&range));
3277 ranges_check(ranges, "before del");
3279 /* we don't use DPAS_SORTED here, since we need *
3280 * to find the first overlapping range */
3281 index = DPA_Search(ranges->hdpa, &range, 0, ranges_cmp, 0, 0);
3282 while(index != -1)
3284 chkrgn = DPA_GetPtr(ranges->hdpa, index);
3286 TRACE("Matches range %s @%d\n", debugrange(chkrgn), index);
3288 /* case 1: Same range */
3289 if ( (chkrgn->upper == range.upper) &&
3290 (chkrgn->lower == range.lower) )
3292 DPA_DeletePtr(ranges->hdpa, index);
3293 Free(chkrgn);
3294 break;
3296 /* case 2: engulf */
3297 else if ( (chkrgn->upper <= range.upper) &&
3298 (chkrgn->lower >= range.lower) )
3300 DPA_DeletePtr(ranges->hdpa, index);
3301 Free(chkrgn);
3303 /* case 3: overlap upper */
3304 else if ( (chkrgn->upper <= range.upper) &&
3305 (chkrgn->lower < range.lower) )
3307 chkrgn->upper = range.lower;
3309 /* case 4: overlap lower */
3310 else if ( (chkrgn->upper > range.upper) &&
3311 (chkrgn->lower >= range.lower) )
3313 chkrgn->lower = range.upper;
3314 break;
3316 /* case 5: fully internal */
3317 else
3319 RANGE *newrgn;
3321 if (!(newrgn = Alloc(sizeof(RANGE)))) goto fail;
3322 newrgn->lower = chkrgn->lower;
3323 newrgn->upper = range.lower;
3324 chkrgn->lower = range.upper;
3325 if (DPA_InsertPtr(ranges->hdpa, index, newrgn) == -1)
3327 Free(newrgn);
3328 goto fail;
3330 break;
3333 index = DPA_Search(ranges->hdpa, &range, index, ranges_cmp, 0, 0);
3336 ranges_check(ranges, "after del");
3337 return TRUE;
3339 fail:
3340 ranges_check(ranges, "failed del");
3341 return FALSE;
3344 /***
3345 * DESCRIPTION:
3346 * Removes all selection ranges
3348 * Parameters(s):
3349 * [I] infoPtr : valid pointer to the listview structure
3350 * [I] toSkip : item range to skip removing the selection
3352 * RETURNS:
3353 * SUCCESS : TRUE
3354 * FAILURE : FALSE
3356 static BOOL LISTVIEW_DeselectAllSkipItems(LISTVIEW_INFO *infoPtr, RANGES toSkip)
3358 LVITEMW lvItem;
3359 ITERATOR i;
3360 RANGES clone;
3362 TRACE("()\n");
3364 lvItem.state = 0;
3365 lvItem.stateMask = LVIS_SELECTED;
3367 /* need to clone the DPA because callbacks can change it */
3368 if (!(clone = ranges_clone(infoPtr->selectionRanges))) return FALSE;
3369 iterator_rangesitems(&i, ranges_diff(clone, toSkip));
3370 while(iterator_next(&i))
3371 LISTVIEW_SetItemState(infoPtr, i.nItem, &lvItem);
3372 /* note that the iterator destructor will free the cloned range */
3373 iterator_destroy(&i);
3375 return TRUE;
3378 static inline BOOL LISTVIEW_DeselectAllSkipItem(LISTVIEW_INFO *infoPtr, INT nItem)
3380 RANGES toSkip;
3382 if (!(toSkip = ranges_create(1))) return FALSE;
3383 if (nItem != -1) ranges_additem(toSkip, nItem);
3384 LISTVIEW_DeselectAllSkipItems(infoPtr, toSkip);
3385 ranges_destroy(toSkip);
3386 return TRUE;
3389 static inline BOOL LISTVIEW_DeselectAll(LISTVIEW_INFO *infoPtr)
3391 return LISTVIEW_DeselectAllSkipItem(infoPtr, -1);
3394 /***
3395 * DESCRIPTION:
3396 * Retrieves the number of items that are marked as selected.
3398 * PARAMETER(S):
3399 * [I] infoPtr : valid pointer to the listview structure
3401 * RETURN:
3402 * Number of items selected.
3404 static INT LISTVIEW_GetSelectedCount(const LISTVIEW_INFO *infoPtr)
3406 INT nSelectedCount = 0;
3408 if (infoPtr->uCallbackMask & LVIS_SELECTED)
3410 INT i;
3411 for (i = 0; i < infoPtr->nItemCount; i++)
3413 if (LISTVIEW_GetItemState(infoPtr, i, LVIS_SELECTED))
3414 nSelectedCount++;
3417 else
3418 nSelectedCount = ranges_itemcount(infoPtr->selectionRanges);
3420 TRACE("nSelectedCount=%d\n", nSelectedCount);
3421 return nSelectedCount;
3424 /***
3425 * DESCRIPTION:
3426 * Manages the item focus.
3428 * PARAMETER(S):
3429 * [I] infoPtr : valid pointer to the listview structure
3430 * [I] nItem : item index
3432 * RETURN:
3433 * TRUE : focused item changed
3434 * FALSE : focused item has NOT changed
3436 static inline BOOL LISTVIEW_SetItemFocus(LISTVIEW_INFO *infoPtr, INT nItem)
3438 INT oldFocus = infoPtr->nFocusedItem;
3439 LVITEMW lvItem;
3441 if (nItem == infoPtr->nFocusedItem) return FALSE;
3443 lvItem.state = nItem == -1 ? 0 : LVIS_FOCUSED;
3444 lvItem.stateMask = LVIS_FOCUSED;
3445 LISTVIEW_SetItemState(infoPtr, nItem == -1 ? infoPtr->nFocusedItem : nItem, &lvItem);
3447 return oldFocus != infoPtr->nFocusedItem;
3450 /* Helper function for LISTVIEW_ShiftIndices *only* */
3451 static INT shift_item(const LISTVIEW_INFO *infoPtr, INT nShiftItem, INT nItem, INT direction)
3453 if (nShiftItem < nItem) return nShiftItem;
3455 if (nShiftItem > nItem) return nShiftItem + direction;
3457 if (direction > 0) return nShiftItem + direction;
3459 return min(nShiftItem, infoPtr->nItemCount - 1);
3463 * DESCRIPTION:
3464 * Updates the various indices after an item has been inserted or deleted.
3466 * PARAMETER(S):
3467 * [I] infoPtr : valid pointer to the listview structure
3468 * [I] nItem : item index
3469 * [I] direction : Direction of shift, +1 or -1.
3471 * RETURN:
3472 * None
3474 static void LISTVIEW_ShiftIndices(LISTVIEW_INFO *infoPtr, INT nItem, INT direction)
3476 INT nNewFocus;
3477 BOOL bOldChange;
3479 /* temporarily disable change notification while shifting items */
3480 bOldChange = infoPtr->bDoChangeNotify;
3481 infoPtr->bDoChangeNotify = FALSE;
3483 TRACE("Shifting %iu, %i steps\n", nItem, direction);
3485 ranges_shift(infoPtr->selectionRanges, nItem, direction, infoPtr->nItemCount);
3487 assert(abs(direction) == 1);
3489 infoPtr->nSelectionMark = shift_item(infoPtr, infoPtr->nSelectionMark, nItem, direction);
3491 nNewFocus = shift_item(infoPtr, infoPtr->nFocusedItem, nItem, direction);
3492 if (nNewFocus != infoPtr->nFocusedItem)
3493 LISTVIEW_SetItemFocus(infoPtr, nNewFocus);
3495 /* But we are not supposed to modify nHotItem! */
3497 infoPtr->bDoChangeNotify = bOldChange;
3502 * DESCRIPTION:
3503 * Adds a block of selections.
3505 * PARAMETER(S):
3506 * [I] infoPtr : valid pointer to the listview structure
3507 * [I] nItem : item index
3509 * RETURN:
3510 * Whether the window is still valid.
3512 static BOOL LISTVIEW_AddGroupSelection(LISTVIEW_INFO *infoPtr, INT nItem)
3514 INT nFirst = min(infoPtr->nSelectionMark, nItem);
3515 INT nLast = max(infoPtr->nSelectionMark, nItem);
3516 HWND hwndSelf = infoPtr->hwndSelf;
3517 NMLVODSTATECHANGE nmlv;
3518 LVITEMW item;
3519 BOOL bOldChange;
3520 INT i;
3522 /* Temporarily disable change notification
3523 * If the control is LVS_OWNERDATA, we need to send
3524 * only one LVN_ODSTATECHANGED notification.
3525 * See MSDN documentation for LVN_ITEMCHANGED.
3527 bOldChange = infoPtr->bDoChangeNotify;
3528 if (infoPtr->dwStyle & LVS_OWNERDATA) infoPtr->bDoChangeNotify = FALSE;
3530 if (nFirst == -1) nFirst = nItem;
3532 item.state = LVIS_SELECTED;
3533 item.stateMask = LVIS_SELECTED;
3535 for (i = nFirst; i <= nLast; i++)
3536 LISTVIEW_SetItemState(infoPtr,i,&item);
3538 ZeroMemory(&nmlv, sizeof(nmlv));
3539 nmlv.iFrom = nFirst;
3540 nmlv.iTo = nLast;
3541 nmlv.uNewState = 0;
3542 nmlv.uOldState = item.state;
3544 notify_hdr(infoPtr, LVN_ODSTATECHANGED, (LPNMHDR)&nmlv);
3545 if (!IsWindow(hwndSelf))
3546 return FALSE;
3547 infoPtr->bDoChangeNotify = bOldChange;
3548 return TRUE;
3552 /***
3553 * DESCRIPTION:
3554 * Sets a single group selection.
3556 * PARAMETER(S):
3557 * [I] infoPtr : valid pointer to the listview structure
3558 * [I] nItem : item index
3560 * RETURN:
3561 * None
3563 static void LISTVIEW_SetGroupSelection(LISTVIEW_INFO *infoPtr, INT nItem)
3565 RANGES selection;
3566 LVITEMW item;
3567 ITERATOR i;
3568 BOOL bOldChange;
3570 if (!(selection = ranges_create(100))) return;
3572 item.state = LVIS_SELECTED;
3573 item.stateMask = LVIS_SELECTED;
3575 if ((infoPtr->uView == LV_VIEW_LIST) || (infoPtr->uView == LV_VIEW_DETAILS))
3577 if (infoPtr->nSelectionMark == -1)
3579 infoPtr->nSelectionMark = nItem;
3580 ranges_additem(selection, nItem);
3582 else
3584 RANGE sel;
3586 sel.lower = min(infoPtr->nSelectionMark, nItem);
3587 sel.upper = max(infoPtr->nSelectionMark, nItem) + 1;
3588 ranges_add(selection, sel);
3591 else
3593 RECT rcItem, rcSel, rcSelMark;
3594 POINT ptItem;
3596 rcItem.left = LVIR_BOUNDS;
3597 if (!LISTVIEW_GetItemRect(infoPtr, nItem, &rcItem)) return;
3598 rcSelMark.left = LVIR_BOUNDS;
3599 if (!LISTVIEW_GetItemRect(infoPtr, infoPtr->nSelectionMark, &rcSelMark)) return;
3600 UnionRect(&rcSel, &rcItem, &rcSelMark);
3601 iterator_frameditems(&i, infoPtr, &rcSel);
3602 while(iterator_next(&i))
3604 LISTVIEW_GetItemPosition(infoPtr, i.nItem, &ptItem);
3605 if (PtInRect(&rcSel, ptItem)) ranges_additem(selection, i.nItem);
3607 iterator_destroy(&i);
3610 /* disable per item notifications on LVS_OWNERDATA style
3611 FIXME: single LVN_ODSTATECHANGED should be used */
3612 bOldChange = infoPtr->bDoChangeNotify;
3613 if (infoPtr->dwStyle & LVS_OWNERDATA) infoPtr->bDoChangeNotify = FALSE;
3615 LISTVIEW_DeselectAllSkipItems(infoPtr, selection);
3618 iterator_rangesitems(&i, selection);
3619 while(iterator_next(&i))
3620 LISTVIEW_SetItemState(infoPtr, i.nItem, &item);
3621 /* this will also destroy the selection */
3622 iterator_destroy(&i);
3624 infoPtr->bDoChangeNotify = bOldChange;
3626 LISTVIEW_SetItemFocus(infoPtr, nItem);
3629 /***
3630 * DESCRIPTION:
3631 * Sets a single selection.
3633 * PARAMETER(S):
3634 * [I] infoPtr : valid pointer to the listview structure
3635 * [I] nItem : item index
3637 * RETURN:
3638 * None
3640 static void LISTVIEW_SetSelection(LISTVIEW_INFO *infoPtr, INT nItem)
3642 LVITEMW lvItem;
3644 TRACE("nItem=%d\n", nItem);
3646 LISTVIEW_DeselectAllSkipItem(infoPtr, nItem);
3648 lvItem.state = LVIS_FOCUSED | LVIS_SELECTED;
3649 lvItem.stateMask = LVIS_FOCUSED | LVIS_SELECTED;
3650 LISTVIEW_SetItemState(infoPtr, nItem, &lvItem);
3652 infoPtr->nSelectionMark = nItem;
3655 /***
3656 * DESCRIPTION:
3657 * Set selection(s) with keyboard.
3659 * PARAMETER(S):
3660 * [I] infoPtr : valid pointer to the listview structure
3661 * [I] nItem : item index
3662 * [I] space : VK_SPACE code sent
3664 * RETURN:
3665 * SUCCESS : TRUE (needs to be repainted)
3666 * FAILURE : FALSE (nothing has changed)
3668 static BOOL LISTVIEW_KeySelection(LISTVIEW_INFO *infoPtr, INT nItem, BOOL space)
3670 /* FIXME: pass in the state */
3671 WORD wShift = HIWORD(GetKeyState(VK_SHIFT));
3672 WORD wCtrl = HIWORD(GetKeyState(VK_CONTROL));
3673 BOOL bResult = FALSE;
3675 TRACE("nItem=%d, wShift=%d, wCtrl=%d\n", nItem, wShift, wCtrl);
3676 if ((nItem >= 0) && (nItem < infoPtr->nItemCount))
3678 bResult = TRUE;
3680 if (infoPtr->dwStyle & LVS_SINGLESEL || (wShift == 0 && wCtrl == 0))
3681 LISTVIEW_SetSelection(infoPtr, nItem);
3682 else
3684 if (wShift)
3685 LISTVIEW_SetGroupSelection(infoPtr, nItem);
3686 else if (wCtrl)
3688 LVITEMW lvItem;
3689 lvItem.state = ~LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED);
3690 lvItem.stateMask = LVIS_SELECTED;
3691 if (space)
3693 LISTVIEW_SetItemState(infoPtr, nItem, &lvItem);
3694 if (lvItem.state & LVIS_SELECTED)
3695 infoPtr->nSelectionMark = nItem;
3697 bResult = LISTVIEW_SetItemFocus(infoPtr, nItem);
3700 LISTVIEW_EnsureVisible(infoPtr, nItem, FALSE);
3703 UpdateWindow(infoPtr->hwndSelf); /* update client area */
3704 return bResult;
3707 static BOOL LISTVIEW_GetItemAtPt(const LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem, POINT pt)
3709 LVHITTESTINFO lvHitTestInfo;
3711 ZeroMemory(&lvHitTestInfo, sizeof(lvHitTestInfo));
3712 lvHitTestInfo.pt.x = pt.x;
3713 lvHitTestInfo.pt.y = pt.y;
3715 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
3717 lpLVItem->mask = LVIF_PARAM;
3718 lpLVItem->iItem = lvHitTestInfo.iItem;
3719 lpLVItem->iSubItem = 0;
3721 return LISTVIEW_GetItemT(infoPtr, lpLVItem, TRUE);
3724 static inline BOOL LISTVIEW_IsHotTracking(const LISTVIEW_INFO *infoPtr)
3726 return ((infoPtr->dwLvExStyle & LVS_EX_TRACKSELECT) ||
3727 (infoPtr->dwLvExStyle & LVS_EX_ONECLICKACTIVATE) ||
3728 (infoPtr->dwLvExStyle & LVS_EX_TWOCLICKACTIVATE));
3731 /***
3732 * DESCRIPTION:
3733 * Called when the mouse is being actively tracked and has hovered for a specified
3734 * amount of time
3736 * PARAMETER(S):
3737 * [I] infoPtr : valid pointer to the listview structure
3738 * [I] fwKeys : key indicator
3739 * [I] x,y : mouse position
3741 * RETURN:
3742 * 0 if the message was processed, non-zero if there was an error
3744 * INFO:
3745 * LVS_EX_TRACKSELECT: An item is automatically selected when the cursor remains
3746 * over the item for a certain period of time.
3749 static LRESULT LISTVIEW_MouseHover(LISTVIEW_INFO *infoPtr, INT x, INT y)
3751 NMHDR hdr;
3753 if (notify_hdr(infoPtr, NM_HOVER, &hdr)) return 0;
3755 if (LISTVIEW_IsHotTracking(infoPtr))
3757 LVITEMW item;
3758 POINT pt;
3760 pt.x = x;
3761 pt.y = y;
3763 if (LISTVIEW_GetItemAtPt(infoPtr, &item, pt))
3764 LISTVIEW_SetSelection(infoPtr, item.iItem);
3766 SetFocus(infoPtr->hwndSelf);
3769 return 0;
3772 #define SCROLL_LEFT 0x1
3773 #define SCROLL_RIGHT 0x2
3774 #define SCROLL_UP 0x4
3775 #define SCROLL_DOWN 0x8
3777 /***
3778 * DESCRIPTION:
3779 * Utility routine to draw and highlight items within a marquee selection rectangle.
3781 * PARAMETER(S):
3782 * [I] infoPtr : valid pointer to the listview structure
3783 * [I] coords_orig : original co-ordinates of the cursor
3784 * [I] coords_offs : offsetted coordinates of the cursor
3785 * [I] offset : offset amount
3786 * [I] scroll : Bitmask of which directions we should scroll, if at all
3788 * RETURN:
3789 * None.
3791 static void LISTVIEW_MarqueeHighlight(LISTVIEW_INFO *infoPtr, const POINT *coords_orig,
3792 const POINT *coords_offs, const POINT *offset,
3793 INT scroll)
3795 BOOL controlDown = FALSE;
3796 LVITEMW item;
3797 ITERATOR old_elems, new_elems;
3798 RECT rect;
3800 if (coords_offs->x > infoPtr->marqueeOrigin.x)
3802 rect.left = infoPtr->marqueeOrigin.x;
3803 rect.right = coords_offs->x;
3805 else
3807 rect.left = coords_offs->x;
3808 rect.right = infoPtr->marqueeOrigin.x;
3811 if (coords_offs->y > infoPtr->marqueeOrigin.y)
3813 rect.top = infoPtr->marqueeOrigin.y;
3814 rect.bottom = coords_offs->y;
3816 else
3818 rect.top = coords_offs->y;
3819 rect.bottom = infoPtr->marqueeOrigin.y;
3822 /* Cancel out the old marquee rectangle and draw the new one */
3823 LISTVIEW_InvalidateRect(infoPtr, &infoPtr->marqueeDrawRect);
3825 /* Scroll by the appropriate distance if applicable - speed up scrolling as
3826 the cursor is further away */
3828 if ((scroll & SCROLL_LEFT) && (coords_orig->x <= 0))
3829 LISTVIEW_Scroll(infoPtr, coords_orig->x, 0);
3831 if ((scroll & SCROLL_RIGHT) && (coords_orig->x >= infoPtr->rcList.right))
3832 LISTVIEW_Scroll(infoPtr, (coords_orig->x - infoPtr->rcList.right), 0);
3834 if ((scroll & SCROLL_UP) && (coords_orig->y <= 0))
3835 LISTVIEW_Scroll(infoPtr, 0, coords_orig->y);
3837 if ((scroll & SCROLL_DOWN) && (coords_orig->y >= infoPtr->rcList.bottom))
3838 LISTVIEW_Scroll(infoPtr, 0, (coords_orig->y - infoPtr->rcList.bottom));
3840 iterator_frameditems_absolute(&old_elems, infoPtr, &infoPtr->marqueeRect);
3842 CopyRect(&infoPtr->marqueeRect, &rect);
3844 CopyRect(&infoPtr->marqueeDrawRect, &rect);
3845 OffsetRect(&infoPtr->marqueeDrawRect, offset->x, offset->y);
3847 iterator_frameditems_absolute(&new_elems, infoPtr, &infoPtr->marqueeRect);
3848 iterator_remove_common_items(&old_elems, &new_elems);
3850 /* Iterate over no longer selected items */
3851 while (iterator_next(&old_elems))
3853 if (old_elems.nItem > -1)
3855 if (LISTVIEW_GetItemState(infoPtr, old_elems.nItem, LVIS_SELECTED) == LVIS_SELECTED)
3856 item.state = 0;
3857 else
3858 item.state = LVIS_SELECTED;
3860 item.stateMask = LVIS_SELECTED;
3862 LISTVIEW_SetItemState(infoPtr, old_elems.nItem, &item);
3865 iterator_destroy(&old_elems);
3868 /* Iterate over newly selected items */
3869 if (GetKeyState(VK_CONTROL) & 0x8000)
3870 controlDown = TRUE;
3872 while (iterator_next(&new_elems))
3874 if (new_elems.nItem > -1)
3876 /* If CTRL is pressed, invert. If not, always select the item. */
3877 if ((controlDown) && (LISTVIEW_GetItemState(infoPtr, new_elems.nItem, LVIS_SELECTED)))
3878 item.state = 0;
3879 else
3880 item.state = LVIS_SELECTED;
3882 item.stateMask = LVIS_SELECTED;
3884 LISTVIEW_SetItemState(infoPtr, new_elems.nItem, &item);
3887 iterator_destroy(&new_elems);
3889 LISTVIEW_InvalidateRect(infoPtr, &infoPtr->marqueeDrawRect);
3892 /***
3893 * DESCRIPTION:
3894 * Called when we are in a marquee selection that involves scrolling the listview (ie,
3895 * the cursor is outside the bounds of the client area). This is a TIMERPROC.
3897 * PARAMETER(S):
3898 * [I] hwnd : Handle to the listview
3899 * [I] uMsg : WM_TIMER (ignored)
3900 * [I] idEvent : The timer ID interpreted as a pointer to a LISTVIEW_INFO struct
3901 * [I] dwTimer : The elapsed time (ignored)
3903 * RETURN:
3904 * None.
3906 static VOID CALLBACK LISTVIEW_ScrollTimer(HWND hWnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
3908 LISTVIEW_INFO *infoPtr;
3909 SCROLLINFO scrollInfo;
3910 POINT coords_orig;
3911 POINT coords_offs;
3912 POINT offset;
3913 INT scroll = 0;
3915 infoPtr = (LISTVIEW_INFO *) idEvent;
3917 if (!infoPtr)
3918 return;
3920 /* Get the current cursor position and convert to client coordinates */
3921 GetCursorPos(&coords_orig);
3922 ScreenToClient(hWnd, &coords_orig);
3924 /* Ensure coordinates are within client bounds */
3925 coords_offs.x = max(min(coords_orig.x, infoPtr->rcList.right), 0);
3926 coords_offs.y = max(min(coords_orig.y, infoPtr->rcList.bottom), 0);
3928 /* Get offset */
3929 LISTVIEW_GetOrigin(infoPtr, &offset);
3931 /* Offset coordinates by the appropriate amount */
3932 coords_offs.x -= offset.x;
3933 coords_offs.y -= offset.y;
3935 scrollInfo.cbSize = sizeof(SCROLLINFO);
3936 scrollInfo.fMask = SIF_ALL;
3938 /* Work out in which directions we can scroll */
3939 if (GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
3941 if (scrollInfo.nPos != scrollInfo.nMin)
3942 scroll |= SCROLL_UP;
3944 if (((scrollInfo.nPage + scrollInfo.nPos) - 1) != scrollInfo.nMax)
3945 scroll |= SCROLL_DOWN;
3948 if (GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo))
3950 if (scrollInfo.nPos != scrollInfo.nMin)
3951 scroll |= SCROLL_LEFT;
3953 if (((scrollInfo.nPage + scrollInfo.nPos) - 1) != scrollInfo.nMax)
3954 scroll |= SCROLL_RIGHT;
3957 if (((coords_orig.x <= 0) && (scroll & SCROLL_LEFT)) ||
3958 ((coords_orig.y <= 0) && (scroll & SCROLL_UP)) ||
3959 ((coords_orig.x >= infoPtr->rcList.right) && (scroll & SCROLL_RIGHT)) ||
3960 ((coords_orig.y >= infoPtr->rcList.bottom) && (scroll & SCROLL_DOWN)))
3962 LISTVIEW_MarqueeHighlight(infoPtr, &coords_orig, &coords_offs, &offset, scroll);
3966 /***
3967 * DESCRIPTION:
3968 * Called whenever WM_MOUSEMOVE is received.
3970 * PARAMETER(S):
3971 * [I] infoPtr : valid pointer to the listview structure
3972 * [I] fwKeys : key indicator
3973 * [I] x,y : mouse position
3975 * RETURN:
3976 * 0 if the message is processed, non-zero if there was an error
3978 static LRESULT LISTVIEW_MouseMove(LISTVIEW_INFO *infoPtr, WORD fwKeys, INT x, INT y)
3980 if (!(fwKeys & MK_LBUTTON))
3981 infoPtr->bLButtonDown = FALSE;
3983 if (infoPtr->bLButtonDown)
3985 POINT tmp;
3986 RECT rect;
3987 LVHITTESTINFO lvHitTestInfo;
3988 WORD wDragWidth = GetSystemMetrics(SM_CXDRAG);
3989 WORD wDragHeight= GetSystemMetrics(SM_CYDRAG);
3991 if (infoPtr->bMarqueeSelect)
3993 POINT coords_orig;
3994 POINT coords_offs;
3995 POINT offset;
3997 coords_orig.x = x;
3998 coords_orig.y = y;
4000 /* Get offset */
4001 LISTVIEW_GetOrigin(infoPtr, &offset);
4003 /* Ensure coordinates are within client bounds */
4004 coords_offs.x = max(min(x, infoPtr->rcList.right), 0);
4005 coords_offs.y = max(min(y, infoPtr->rcList.bottom), 0);
4007 /* Offset coordinates by the appropriate amount */
4008 coords_offs.x -= offset.x;
4009 coords_offs.y -= offset.y;
4011 /* Enable the timer if we're going outside our bounds, in case the user doesn't
4012 move the mouse again */
4014 if ((x <= 0) || (y <= 0) || (x >= infoPtr->rcList.right) ||
4015 (y >= infoPtr->rcList.bottom))
4017 if (!infoPtr->bScrolling)
4019 infoPtr->bScrolling = TRUE;
4020 SetTimer(infoPtr->hwndSelf, (UINT_PTR) infoPtr, 1, LISTVIEW_ScrollTimer);
4023 else
4025 infoPtr->bScrolling = FALSE;
4026 KillTimer(infoPtr->hwndSelf, (UINT_PTR) infoPtr);
4029 LISTVIEW_MarqueeHighlight(infoPtr, &coords_orig, &coords_offs, &offset, 0);
4030 return 0;
4033 rect.left = infoPtr->ptClickPos.x - wDragWidth;
4034 rect.right = infoPtr->ptClickPos.x + wDragWidth;
4035 rect.top = infoPtr->ptClickPos.y - wDragHeight;
4036 rect.bottom = infoPtr->ptClickPos.y + wDragHeight;
4038 tmp.x = x;
4039 tmp.y = y;
4041 lvHitTestInfo.pt = tmp;
4042 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, TRUE);
4044 /* reset item marker */
4045 if (infoPtr->nLButtonDownItem != lvHitTestInfo.iItem)
4046 infoPtr->nLButtonDownItem = -1;
4048 if (!PtInRect(&rect, tmp))
4050 /* this path covers the following:
4051 1. WM_LBUTTONDOWN over selected item (sets focus on it)
4052 2. change focus with keys
4053 3. move mouse over item from step 1 selects it and moves focus on it */
4054 if (infoPtr->nLButtonDownItem != -1 &&
4055 !LISTVIEW_GetItemState(infoPtr, infoPtr->nLButtonDownItem, LVIS_SELECTED))
4057 LVITEMW lvItem;
4059 lvItem.state = LVIS_FOCUSED | LVIS_SELECTED;
4060 lvItem.stateMask = LVIS_FOCUSED | LVIS_SELECTED;
4062 LISTVIEW_SetItemState(infoPtr, infoPtr->nLButtonDownItem, &lvItem);
4063 infoPtr->nLButtonDownItem = -1;
4066 if (!infoPtr->bDragging)
4068 lvHitTestInfo.pt = infoPtr->ptClickPos;
4069 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, TRUE);
4071 /* If the click is outside the range of an item, begin a
4072 highlight. If not, begin an item drag. */
4073 if (lvHitTestInfo.iItem == -1)
4075 NMHDR hdr;
4077 /* If we're allowing multiple selections, send notification.
4078 If return value is non-zero, cancel. */
4079 if (!(infoPtr->dwStyle & LVS_SINGLESEL) && (notify_hdr(infoPtr, LVN_MARQUEEBEGIN, &hdr) == 0))
4081 /* Store the absolute coordinates of the click */
4082 POINT offset;
4083 LISTVIEW_GetOrigin(infoPtr, &offset);
4085 infoPtr->marqueeOrigin.x = infoPtr->ptClickPos.x - offset.x;
4086 infoPtr->marqueeOrigin.y = infoPtr->ptClickPos.y - offset.y;
4088 /* Begin selection and capture mouse */
4089 infoPtr->bMarqueeSelect = TRUE;
4090 SetCapture(infoPtr->hwndSelf);
4093 else
4095 NMLISTVIEW nmlv;
4097 ZeroMemory(&nmlv, sizeof(nmlv));
4098 nmlv.iItem = lvHitTestInfo.iItem;
4099 nmlv.ptAction = infoPtr->ptClickPos;
4101 notify_listview(infoPtr, LVN_BEGINDRAG, &nmlv);
4102 infoPtr->bDragging = TRUE;
4106 return 0;
4110 /* see if we are supposed to be tracking mouse hovering */
4111 if (LISTVIEW_IsHotTracking(infoPtr)) {
4112 TRACKMOUSEEVENT trackinfo;
4114 trackinfo.cbSize = sizeof(TRACKMOUSEEVENT);
4115 trackinfo.dwFlags = TME_QUERY;
4117 /* see if we are already tracking this hwnd */
4118 _TrackMouseEvent(&trackinfo);
4120 if(!(trackinfo.dwFlags & TME_HOVER) || trackinfo.hwndTrack != infoPtr->hwndSelf) {
4121 trackinfo.dwFlags = TME_HOVER;
4122 trackinfo.dwHoverTime = infoPtr->dwHoverTime;
4123 trackinfo.hwndTrack = infoPtr->hwndSelf;
4125 /* call TRACKMOUSEEVENT so we receive WM_MOUSEHOVER messages */
4126 _TrackMouseEvent(&trackinfo);
4130 return 0;
4134 /***
4135 * Tests whether the item is assignable to a list with style lStyle
4137 static inline BOOL is_assignable_item(const LVITEMW *lpLVItem, LONG lStyle)
4139 if ( (lpLVItem->mask & LVIF_TEXT) &&
4140 (lpLVItem->pszText == LPSTR_TEXTCALLBACKW) &&
4141 (lStyle & (LVS_SORTASCENDING | LVS_SORTDESCENDING)) ) return FALSE;
4143 return TRUE;
4147 /***
4148 * DESCRIPTION:
4149 * Helper for LISTVIEW_SetItemT *only*: sets item attributes.
4151 * PARAMETER(S):
4152 * [I] infoPtr : valid pointer to the listview structure
4153 * [I] lpLVItem : valid pointer to new item attributes
4154 * [I] isNew : the item being set is being inserted
4155 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
4156 * [O] bChanged : will be set to TRUE if the item really changed
4158 * RETURN:
4159 * SUCCESS : TRUE
4160 * FAILURE : FALSE
4162 static BOOL set_main_item(LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem, BOOL isNew, BOOL isW, BOOL *bChanged)
4164 ITEM_INFO *lpItem;
4165 NMLISTVIEW nmlv;
4166 UINT uChanged = 0;
4167 LVITEMW item;
4168 /* stateMask is ignored for LVM_INSERTITEM */
4169 UINT stateMask = isNew ? ~0 : lpLVItem->stateMask;
4171 TRACE("()\n");
4173 assert(lpLVItem->iItem >= 0 && lpLVItem->iItem < infoPtr->nItemCount);
4175 if (lpLVItem->mask == 0) return TRUE;
4177 if (infoPtr->dwStyle & LVS_OWNERDATA)
4179 /* a virtual listview only stores selection and focus */
4180 if (lpLVItem->mask & ~LVIF_STATE)
4181 return FALSE;
4182 lpItem = NULL;
4184 else
4186 HDPA hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, lpLVItem->iItem);
4187 lpItem = DPA_GetPtr(hdpaSubItems, 0);
4188 assert (lpItem);
4191 /* we need to get the lParam and state of the item */
4192 item.iItem = lpLVItem->iItem;
4193 item.iSubItem = lpLVItem->iSubItem;
4194 item.mask = LVIF_STATE | LVIF_PARAM;
4195 item.stateMask = (infoPtr->dwStyle & LVS_OWNERDATA) ? LVIS_FOCUSED | LVIS_SELECTED : ~0;
4197 item.state = 0;
4198 item.lParam = 0;
4199 if (!isNew && !LISTVIEW_GetItemW(infoPtr, &item)) return FALSE;
4201 TRACE("oldState=%x, newState=%x\n", item.state, lpLVItem->state);
4202 /* determine what fields will change */
4203 if ((lpLVItem->mask & LVIF_STATE) && ((item.state ^ lpLVItem->state) & stateMask & ~infoPtr->uCallbackMask))
4204 uChanged |= LVIF_STATE;
4206 if ((lpLVItem->mask & LVIF_IMAGE) && (lpItem->hdr.iImage != lpLVItem->iImage))
4207 uChanged |= LVIF_IMAGE;
4209 if ((lpLVItem->mask & LVIF_PARAM) && (lpItem->lParam != lpLVItem->lParam))
4210 uChanged |= LVIF_PARAM;
4212 if ((lpLVItem->mask & LVIF_INDENT) && (lpItem->iIndent != lpLVItem->iIndent))
4213 uChanged |= LVIF_INDENT;
4215 if ((lpLVItem->mask & LVIF_TEXT) && textcmpWT(lpItem->hdr.pszText, lpLVItem->pszText, isW))
4216 uChanged |= LVIF_TEXT;
4218 TRACE("uChanged=0x%x\n", uChanged);
4219 if (!uChanged) return TRUE;
4220 *bChanged = TRUE;
4222 ZeroMemory(&nmlv, sizeof(NMLISTVIEW));
4223 nmlv.iItem = lpLVItem->iItem;
4224 nmlv.uNewState = (item.state & ~stateMask) | (lpLVItem->state & stateMask);
4225 nmlv.uOldState = item.state;
4226 nmlv.uChanged = uChanged;
4227 nmlv.lParam = item.lParam;
4229 /* send LVN_ITEMCHANGING notification, if the item is not being inserted */
4230 /* and we are _NOT_ virtual (LVS_OWNERDATA), and change notifications */
4231 /* are enabled */
4232 if(lpItem && !isNew && infoPtr->bDoChangeNotify)
4234 HWND hwndSelf = infoPtr->hwndSelf;
4236 if (notify_listview(infoPtr, LVN_ITEMCHANGING, &nmlv))
4237 return FALSE;
4238 if (!IsWindow(hwndSelf))
4239 return FALSE;
4242 /* copy information */
4243 if (lpLVItem->mask & LVIF_TEXT)
4244 textsetptrT(&lpItem->hdr.pszText, lpLVItem->pszText, isW);
4246 if (lpLVItem->mask & LVIF_IMAGE)
4247 lpItem->hdr.iImage = lpLVItem->iImage;
4249 if (lpLVItem->mask & LVIF_PARAM)
4250 lpItem->lParam = lpLVItem->lParam;
4252 if (lpLVItem->mask & LVIF_INDENT)
4253 lpItem->iIndent = lpLVItem->iIndent;
4255 if (uChanged & LVIF_STATE)
4257 if (lpItem && (stateMask & ~infoPtr->uCallbackMask))
4259 lpItem->state &= ~stateMask;
4260 lpItem->state |= (lpLVItem->state & stateMask);
4262 if (lpLVItem->state & stateMask & ~infoPtr->uCallbackMask & LVIS_SELECTED)
4264 if (infoPtr->dwStyle & LVS_SINGLESEL) LISTVIEW_DeselectAllSkipItem(infoPtr, lpLVItem->iItem);
4265 ranges_additem(infoPtr->selectionRanges, lpLVItem->iItem);
4267 else if (stateMask & LVIS_SELECTED)
4269 ranges_delitem(infoPtr->selectionRanges, lpLVItem->iItem);
4271 /* if we are asked to change focus, and we manage it, do it */
4272 if (stateMask & ~infoPtr->uCallbackMask & LVIS_FOCUSED)
4274 if (lpLVItem->state & LVIS_FOCUSED)
4276 /* update selection mark */
4277 if (infoPtr->nFocusedItem == -1 && infoPtr->nSelectionMark == -1)
4278 infoPtr->nSelectionMark = lpLVItem->iItem;
4280 if (infoPtr->nFocusedItem != -1)
4282 /* remove current focus */
4283 item.mask = LVIF_STATE;
4284 item.state = 0;
4285 item.stateMask = LVIS_FOCUSED;
4287 /* recurse with redrawing an item */
4288 LISTVIEW_SetItemState(infoPtr, infoPtr->nFocusedItem, &item);
4291 infoPtr->nFocusedItem = lpLVItem->iItem;
4292 LISTVIEW_EnsureVisible(infoPtr, lpLVItem->iItem, infoPtr->uView == LV_VIEW_LIST);
4294 else if (infoPtr->nFocusedItem == lpLVItem->iItem)
4296 infoPtr->nFocusedItem = -1;
4301 /* if we're inserting the item, we're done */
4302 if (isNew) return TRUE;
4304 /* send LVN_ITEMCHANGED notification */
4305 if (lpLVItem->mask & LVIF_PARAM) nmlv.lParam = lpLVItem->lParam;
4306 if (infoPtr->bDoChangeNotify) notify_listview(infoPtr, LVN_ITEMCHANGED, &nmlv);
4308 return TRUE;
4311 /***
4312 * DESCRIPTION:
4313 * Helper for LISTVIEW_{Set,Insert}ItemT *only*: sets subitem attributes.
4315 * PARAMETER(S):
4316 * [I] infoPtr : valid pointer to the listview structure
4317 * [I] lpLVItem : valid pointer to new subitem attributes
4318 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
4319 * [O] bChanged : will be set to TRUE if the item really changed
4321 * RETURN:
4322 * SUCCESS : TRUE
4323 * FAILURE : FALSE
4325 static BOOL set_sub_item(const LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem, BOOL isW, BOOL *bChanged)
4327 HDPA hdpaSubItems;
4328 SUBITEM_INFO *lpSubItem;
4330 /* we do not support subitems for virtual listviews */
4331 if (infoPtr->dwStyle & LVS_OWNERDATA) return FALSE;
4333 /* set subitem only if column is present */
4334 if (lpLVItem->iSubItem >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
4336 /* First do some sanity checks */
4337 /* The LVIF_STATE flag is valid for subitems, but does not appear to be
4338 particularly useful. We currently do not actually do anything with
4339 the flag on subitems.
4341 if (lpLVItem->mask & ~(LVIF_TEXT | LVIF_IMAGE | LVIF_STATE | LVIF_DI_SETITEM)) return FALSE;
4342 if (!(lpLVItem->mask & (LVIF_TEXT | LVIF_IMAGE | LVIF_STATE))) return TRUE;
4344 /* get the subitem structure, and create it if not there */
4345 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, lpLVItem->iItem);
4346 assert (hdpaSubItems);
4348 lpSubItem = LISTVIEW_GetSubItemPtr(hdpaSubItems, lpLVItem->iSubItem);
4349 if (!lpSubItem)
4351 SUBITEM_INFO *tmpSubItem;
4352 INT i;
4354 lpSubItem = Alloc(sizeof(SUBITEM_INFO));
4355 if (!lpSubItem) return FALSE;
4356 /* we could binary search here, if need be...*/
4357 for (i = 1; i < DPA_GetPtrCount(hdpaSubItems); i++)
4359 tmpSubItem = DPA_GetPtr(hdpaSubItems, i);
4360 if (tmpSubItem->iSubItem > lpLVItem->iSubItem) break;
4362 if (DPA_InsertPtr(hdpaSubItems, i, lpSubItem) == -1)
4364 Free(lpSubItem);
4365 return FALSE;
4367 lpSubItem->iSubItem = lpLVItem->iSubItem;
4368 lpSubItem->hdr.iImage = I_IMAGECALLBACK;
4369 *bChanged = TRUE;
4372 if ((lpLVItem->mask & LVIF_IMAGE) && (lpSubItem->hdr.iImage != lpLVItem->iImage))
4374 lpSubItem->hdr.iImage = lpLVItem->iImage;
4375 *bChanged = TRUE;
4378 if ((lpLVItem->mask & LVIF_TEXT) && textcmpWT(lpSubItem->hdr.pszText, lpLVItem->pszText, isW))
4380 textsetptrT(&lpSubItem->hdr.pszText, lpLVItem->pszText, isW);
4381 *bChanged = TRUE;
4384 return TRUE;
4387 /***
4388 * DESCRIPTION:
4389 * Sets item attributes.
4391 * PARAMETER(S):
4392 * [I] infoPtr : valid pointer to the listview structure
4393 * [I] lpLVItem : new item attributes
4394 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
4396 * RETURN:
4397 * SUCCESS : TRUE
4398 * FAILURE : FALSE
4400 static BOOL LISTVIEW_SetItemT(LISTVIEW_INFO *infoPtr, LVITEMW *lpLVItem, BOOL isW)
4402 HWND hwndSelf = infoPtr->hwndSelf;
4403 LPWSTR pszText = NULL;
4404 BOOL bResult, bChanged = FALSE;
4405 RECT oldItemArea;
4407 TRACE("(lpLVItem=%s, isW=%d)\n", debuglvitem_t(lpLVItem, isW), isW);
4409 if (!lpLVItem || lpLVItem->iItem < 0 || lpLVItem->iItem >= infoPtr->nItemCount)
4410 return FALSE;
4412 /* Store old item area */
4413 LISTVIEW_GetItemBox(infoPtr, lpLVItem->iItem, &oldItemArea);
4415 /* For efficiency, we transform the lpLVItem->pszText to Unicode here */
4416 if ((lpLVItem->mask & LVIF_TEXT) && is_text(lpLVItem->pszText))
4418 pszText = lpLVItem->pszText;
4419 lpLVItem->pszText = textdupTtoW(lpLVItem->pszText, isW);
4422 /* actually set the fields */
4423 if (!is_assignable_item(lpLVItem, infoPtr->dwStyle)) return FALSE;
4425 if (lpLVItem->iSubItem)
4426 bResult = set_sub_item(infoPtr, lpLVItem, TRUE, &bChanged);
4427 else
4428 bResult = set_main_item(infoPtr, lpLVItem, FALSE, TRUE, &bChanged);
4429 if (!IsWindow(hwndSelf))
4430 return FALSE;
4432 /* redraw item, if necessary */
4433 if (bChanged && !infoPtr->bIsDrawing)
4435 /* this little optimization eliminates some nasty flicker */
4436 if ( infoPtr->uView == LV_VIEW_DETAILS && !(infoPtr->dwStyle & LVS_OWNERDRAWFIXED) &&
4437 !(infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) &&
4438 lpLVItem->iSubItem > 0 && lpLVItem->iSubItem <= DPA_GetPtrCount(infoPtr->hdpaColumns) )
4439 LISTVIEW_InvalidateSubItem(infoPtr, lpLVItem->iItem, lpLVItem->iSubItem);
4440 else
4442 LISTVIEW_InvalidateRect(infoPtr, &oldItemArea);
4443 LISTVIEW_InvalidateItem(infoPtr, lpLVItem->iItem);
4446 /* restore text */
4447 if (pszText)
4449 textfreeT(lpLVItem->pszText, isW);
4450 lpLVItem->pszText = pszText;
4453 return bResult;
4456 /***
4457 * DESCRIPTION:
4458 * Retrieves the index of the item at coordinate (0, 0) of the client area.
4460 * PARAMETER(S):
4461 * [I] infoPtr : valid pointer to the listview structure
4463 * RETURN:
4464 * item index
4466 static INT LISTVIEW_GetTopIndex(const LISTVIEW_INFO *infoPtr)
4468 INT nItem = 0;
4469 SCROLLINFO scrollInfo;
4471 scrollInfo.cbSize = sizeof(SCROLLINFO);
4472 scrollInfo.fMask = SIF_POS;
4474 if (infoPtr->uView == LV_VIEW_LIST)
4476 if (GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo))
4477 nItem = scrollInfo.nPos * LISTVIEW_GetCountPerColumn(infoPtr);
4479 else if (infoPtr->uView == LV_VIEW_DETAILS)
4481 if (GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
4482 nItem = scrollInfo.nPos;
4484 else
4486 if (GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
4487 nItem = LISTVIEW_GetCountPerRow(infoPtr) * (scrollInfo.nPos / infoPtr->nItemHeight);
4490 TRACE("nItem=%d\n", nItem);
4492 return nItem;
4496 /***
4497 * DESCRIPTION:
4498 * Erases the background of the given rectangle
4500 * PARAMETER(S):
4501 * [I] infoPtr : valid pointer to the listview structure
4502 * [I] hdc : device context handle
4503 * [I] lprcBox : clipping rectangle
4505 * RETURN:
4506 * Success: TRUE
4507 * Failure: FALSE
4509 static inline BOOL LISTVIEW_FillBkgnd(const LISTVIEW_INFO *infoPtr, HDC hdc, const RECT *lprcBox)
4511 if (!infoPtr->hBkBrush) return FALSE;
4513 TRACE("(hdc=%p, lprcBox=%s, hBkBrush=%p)\n", hdc, wine_dbgstr_rect(lprcBox), infoPtr->hBkBrush);
4515 return FillRect(hdc, lprcBox, infoPtr->hBkBrush);
4518 /***
4519 * DESCRIPTION:
4520 * Draws an item.
4522 * PARAMETER(S):
4523 * [I] infoPtr : valid pointer to the listview structure
4524 * [I] hdc : device context handle
4525 * [I] nItem : item index
4526 * [I] nSubItem : subitem index
4527 * [I] pos : item position in client coordinates
4528 * [I] cdmode : custom draw mode
4530 * RETURN:
4531 * Success: TRUE
4532 * Failure: FALSE
4534 static BOOL LISTVIEW_DrawItem(LISTVIEW_INFO *infoPtr, HDC hdc, INT nItem, INT nSubItem, POINT pos, DWORD cdmode)
4536 UINT uFormat;
4537 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
4538 static WCHAR szCallback[] = { '(', 'c', 'a', 'l', 'l', 'b', 'a', 'c', 'k', ')', 0 };
4539 DWORD cdsubitemmode = CDRF_DODEFAULT;
4540 LPRECT lprcFocus;
4541 RECT rcSelect, rcBox, rcIcon, rcLabel, rcStateIcon;
4542 NMLVCUSTOMDRAW nmlvcd;
4543 HIMAGELIST himl;
4544 LVITEMW lvItem;
4545 HFONT hOldFont;
4547 TRACE("(hdc=%p, nItem=%d, nSubItem=%d, pos=%s)\n", hdc, nItem, nSubItem, wine_dbgstr_point(&pos));
4549 /* get information needed for drawing the item */
4550 lvItem.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_PARAM;
4551 if (nSubItem == 0) lvItem.mask |= LVIF_STATE;
4552 if (infoPtr->uView == LV_VIEW_DETAILS) lvItem.mask |= LVIF_INDENT;
4553 lvItem.stateMask = LVIS_SELECTED | LVIS_FOCUSED | LVIS_STATEIMAGEMASK | LVIS_CUT;
4554 lvItem.iItem = nItem;
4555 lvItem.iSubItem = nSubItem;
4556 lvItem.state = 0;
4557 lvItem.lParam = 0;
4558 lvItem.cchTextMax = DISP_TEXT_SIZE;
4559 lvItem.pszText = szDispText;
4560 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return FALSE;
4561 if (nSubItem > 0 && (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT))
4562 lvItem.state = LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED);
4563 if (lvItem.pszText == LPSTR_TEXTCALLBACKW) lvItem.pszText = szCallback;
4564 TRACE(" lvItem=%s\n", debuglvitem_t(&lvItem, TRUE));
4566 /* now check if we need to update the focus rectangle */
4567 lprcFocus = infoPtr->bFocus && (lvItem.state & LVIS_FOCUSED) ? &infoPtr->rcFocus : 0;
4569 if (!lprcFocus) lvItem.state &= ~LVIS_FOCUSED;
4570 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, &rcBox, &rcSelect, &rcIcon, &rcStateIcon, &rcLabel);
4571 OffsetRect(&rcBox, pos.x, pos.y);
4572 OffsetRect(&rcSelect, pos.x, pos.y);
4573 OffsetRect(&rcIcon, pos.x, pos.y);
4574 OffsetRect(&rcStateIcon, pos.x, pos.y);
4575 OffsetRect(&rcLabel, pos.x, pos.y);
4576 TRACE(" rcBox=%s, rcSelect=%s, rcIcon=%s. rcLabel=%s\n",
4577 wine_dbgstr_rect(&rcBox), wine_dbgstr_rect(&rcSelect),
4578 wine_dbgstr_rect(&rcIcon), wine_dbgstr_rect(&rcLabel));
4580 /* fill in the custom draw structure */
4581 customdraw_fill(&nmlvcd, infoPtr, hdc, &rcBox, &lvItem);
4583 hOldFont = GetCurrentObject(hdc, OBJ_FONT);
4584 if (nSubItem > 0) cdmode = infoPtr->cditemmode;
4585 if (cdmode & CDRF_SKIPDEFAULT) goto postpaint;
4586 if (cdmode & CDRF_NOTIFYITEMDRAW)
4587 cdsubitemmode = notify_customdraw(infoPtr, CDDS_PREPAINT, &nmlvcd);
4588 if (nSubItem == 0) infoPtr->cditemmode = cdsubitemmode;
4589 if (cdsubitemmode & CDRF_SKIPDEFAULT) goto postpaint;
4590 /* we have to send a CDDS_SUBITEM customdraw explicitly for subitem 0 */
4591 if (nSubItem == 0 && cdsubitemmode == CDRF_NOTIFYITEMDRAW)
4593 cdsubitemmode = notify_customdraw(infoPtr, CDDS_SUBITEM | CDDS_ITEMPREPAINT, &nmlvcd);
4594 if (cdsubitemmode & CDRF_SKIPDEFAULT) goto postpaint;
4596 if (nSubItem == 0 || (cdmode & CDRF_NOTIFYITEMDRAW))
4597 prepaint_setup(infoPtr, hdc, &nmlvcd, FALSE);
4598 else if ((infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) == FALSE)
4599 prepaint_setup(infoPtr, hdc, &nmlvcd, TRUE);
4601 /* in full row select, subitems, will just use main item's colors */
4602 if (nSubItem && infoPtr->uView == LV_VIEW_DETAILS && (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT))
4603 nmlvcd.clrTextBk = CLR_NONE;
4605 /* FIXME: temporary hack */
4606 rcSelect.left = rcLabel.left;
4608 /* draw the selection background, if we're drawing the main item */
4609 if (nSubItem == 0)
4611 /* in icon mode, the label rect is really what we want to draw the
4612 * background for */
4613 if (infoPtr->uView == LV_VIEW_ICON)
4614 rcSelect = rcLabel;
4616 if (infoPtr->uView == LV_VIEW_DETAILS && (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT))
4618 /* we have to update left focus bound too if item isn't in leftmost column
4619 and reduce right box bound */
4620 if (DPA_GetPtrCount(infoPtr->hdpaColumns) > 0)
4622 INT leftmost;
4624 if ((leftmost = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, 0, 0)))
4626 INT Originx = pos.x - LISTVIEW_GetColumnInfo(infoPtr, 0)->rcHeader.left;
4627 INT index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX,
4628 DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, 0);
4630 rcBox.right = LISTVIEW_GetColumnInfo(infoPtr, index)->rcHeader.right + Originx;
4631 rcSelect.left = LISTVIEW_GetColumnInfo(infoPtr, leftmost)->rcHeader.left + Originx;
4635 rcSelect.right = rcBox.right;
4638 if (nmlvcd.clrTextBk != CLR_NONE)
4639 ExtTextOutW(hdc, rcSelect.left, rcSelect.top, ETO_OPAQUE, &rcSelect, NULL, 0, NULL);
4640 /* store new focus rectangle */
4641 if (infoPtr->nFocusedItem == nItem) infoPtr->rcFocus = rcSelect;
4644 /* state icons */
4645 if (infoPtr->himlState && STATEIMAGEINDEX(lvItem.state) && (nSubItem == 0))
4647 UINT uStateImage = STATEIMAGEINDEX(lvItem.state);
4648 if (uStateImage)
4650 TRACE("uStateImage=%d\n", uStateImage);
4651 ImageList_Draw(infoPtr->himlState, uStateImage - 1, hdc,
4652 rcStateIcon.left, rcStateIcon.top, ILD_NORMAL);
4656 /* item icons */
4657 himl = (infoPtr->uView == LV_VIEW_ICON ? infoPtr->himlNormal : infoPtr->himlSmall);
4658 if (himl && lvItem.iImage >= 0 && !IsRectEmpty(&rcIcon))
4660 UINT style;
4662 TRACE("iImage=%d\n", lvItem.iImage);
4664 if (lvItem.state & (LVIS_SELECTED | LVIS_CUT) && infoPtr->bFocus)
4665 style = ILD_SELECTED;
4666 else
4667 style = ILD_NORMAL;
4669 ImageList_DrawEx(himl, lvItem.iImage, hdc, rcIcon.left, rcIcon.top,
4670 rcIcon.right - rcIcon.left, rcIcon.bottom - rcIcon.top, infoPtr->clrBk,
4671 lvItem.state & LVIS_CUT ? RGB(255, 255, 255) : CLR_DEFAULT,
4672 style);
4675 /* Don't bother painting item being edited */
4676 if (infoPtr->hwndEdit && nItem == infoPtr->nEditLabelItem && nSubItem == 0) goto postpaint;
4678 /* figure out the text drawing flags */
4679 uFormat = (infoPtr->uView == LV_VIEW_ICON ? (lprcFocus ? LV_FL_DT_FLAGS : LV_ML_DT_FLAGS) : LV_SL_DT_FLAGS);
4680 if (infoPtr->uView == LV_VIEW_ICON)
4681 uFormat = (lprcFocus ? LV_FL_DT_FLAGS : LV_ML_DT_FLAGS);
4682 else if (nSubItem)
4684 switch (LISTVIEW_GetColumnInfo(infoPtr, nSubItem)->fmt & LVCFMT_JUSTIFYMASK)
4686 case LVCFMT_RIGHT: uFormat |= DT_RIGHT; break;
4687 case LVCFMT_CENTER: uFormat |= DT_CENTER; break;
4688 default: uFormat |= DT_LEFT;
4691 if (!(uFormat & (DT_RIGHT | DT_CENTER)))
4693 if (himl && lvItem.iImage >= 0 && !IsRectEmpty(&rcIcon)) rcLabel.left += IMAGE_PADDING;
4694 else rcLabel.left += LABEL_HOR_PADDING;
4696 else if (uFormat & DT_RIGHT) rcLabel.right -= LABEL_HOR_PADDING;
4698 /* for GRIDLINES reduce the bottom so the text formats correctly */
4699 if (infoPtr->uView == LV_VIEW_DETAILS && infoPtr->dwLvExStyle & LVS_EX_GRIDLINES)
4700 rcLabel.bottom--;
4702 DrawTextW(hdc, lvItem.pszText, -1, &rcLabel, uFormat);
4704 postpaint:
4705 if (cdsubitemmode & CDRF_NOTIFYPOSTPAINT)
4706 notify_postpaint(infoPtr, &nmlvcd);
4707 if (cdsubitemmode & CDRF_NEWFONT)
4708 SelectObject(hdc, hOldFont);
4709 return TRUE;
4712 /***
4713 * DESCRIPTION:
4714 * Draws listview items when in owner draw mode.
4716 * PARAMETER(S):
4717 * [I] infoPtr : valid pointer to the listview structure
4718 * [I] hdc : device context handle
4720 * RETURN:
4721 * None
4723 static void LISTVIEW_RefreshOwnerDraw(const LISTVIEW_INFO *infoPtr, ITERATOR *i, HDC hdc, DWORD cdmode)
4725 UINT uID = (UINT)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
4726 DWORD cditemmode = CDRF_DODEFAULT;
4727 NMLVCUSTOMDRAW nmlvcd;
4728 POINT Origin, Position;
4729 DRAWITEMSTRUCT dis;
4730 LVITEMW item;
4732 TRACE("()\n");
4734 ZeroMemory(&dis, sizeof(dis));
4736 /* Get scroll info once before loop */
4737 LISTVIEW_GetOrigin(infoPtr, &Origin);
4739 /* iterate through the invalidated rows */
4740 while(iterator_next(i))
4742 item.iItem = i->nItem;
4743 item.iSubItem = 0;
4744 item.mask = LVIF_PARAM | LVIF_STATE;
4745 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
4746 if (!LISTVIEW_GetItemW(infoPtr, &item)) continue;
4748 dis.CtlType = ODT_LISTVIEW;
4749 dis.CtlID = uID;
4750 dis.itemID = item.iItem;
4751 dis.itemAction = ODA_DRAWENTIRE;
4752 dis.itemState = 0;
4753 if (item.state & LVIS_SELECTED) dis.itemState |= ODS_SELECTED;
4754 if (infoPtr->bFocus && (item.state & LVIS_FOCUSED)) dis.itemState |= ODS_FOCUS;
4755 dis.hwndItem = infoPtr->hwndSelf;
4756 dis.hDC = hdc;
4757 LISTVIEW_GetItemOrigin(infoPtr, dis.itemID, &Position);
4758 dis.rcItem.left = Position.x + Origin.x;
4759 dis.rcItem.right = dis.rcItem.left + infoPtr->nItemWidth;
4760 dis.rcItem.top = Position.y + Origin.y;
4761 dis.rcItem.bottom = dis.rcItem.top + infoPtr->nItemHeight;
4762 dis.itemData = item.lParam;
4764 TRACE("item=%s, rcItem=%s\n", debuglvitem_t(&item, TRUE), wine_dbgstr_rect(&dis.rcItem));
4767 * Even if we do not send the CDRF_NOTIFYITEMDRAW we need to fill the nmlvcd
4768 * structure for the rest. of the paint cycle
4770 customdraw_fill(&nmlvcd, infoPtr, hdc, &dis.rcItem, &item);
4771 if (cdmode & CDRF_NOTIFYITEMDRAW)
4772 cditemmode = notify_customdraw(infoPtr, CDDS_PREPAINT, &nmlvcd);
4774 if (!(cditemmode & CDRF_SKIPDEFAULT))
4776 prepaint_setup (infoPtr, hdc, &nmlvcd, FALSE);
4777 SendMessageW(infoPtr->hwndNotify, WM_DRAWITEM, dis.CtlID, (LPARAM)&dis);
4780 if (cditemmode & CDRF_NOTIFYPOSTPAINT)
4781 notify_postpaint(infoPtr, &nmlvcd);
4785 /***
4786 * DESCRIPTION:
4787 * Draws listview items when in report display mode.
4789 * PARAMETER(S):
4790 * [I] infoPtr : valid pointer to the listview structure
4791 * [I] hdc : device context handle
4792 * [I] cdmode : custom draw mode
4794 * RETURN:
4795 * None
4797 static void LISTVIEW_RefreshReport(LISTVIEW_INFO *infoPtr, ITERATOR *i, HDC hdc, DWORD cdmode)
4799 INT rgntype;
4800 RECT rcClip, rcItem;
4801 POINT Origin, Position;
4802 RANGES colRanges;
4803 INT col, index;
4804 ITERATOR j;
4806 TRACE("()\n");
4808 /* figure out what to draw */
4809 rgntype = GetClipBox(hdc, &rcClip);
4810 if (rgntype == NULLREGION) return;
4812 /* Get scroll info once before loop */
4813 LISTVIEW_GetOrigin(infoPtr, &Origin);
4815 colRanges = ranges_create(DPA_GetPtrCount(infoPtr->hdpaColumns));
4817 /* narrow down the columns we need to paint */
4818 for(col = 0; col < DPA_GetPtrCount(infoPtr->hdpaColumns); col++)
4820 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, col, 0);
4822 LISTVIEW_GetHeaderRect(infoPtr, index, &rcItem);
4823 if ((rcItem.right + Origin.x >= rcClip.left) && (rcItem.left + Origin.x < rcClip.right))
4824 ranges_additem(colRanges, index);
4826 iterator_rangesitems(&j, colRanges);
4828 /* in full row select, we _have_ to draw the main item */
4829 if (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT)
4830 j.nSpecial = 0;
4832 /* iterate through the invalidated rows */
4833 while(iterator_next(i))
4835 LISTVIEW_GetItemOrigin(infoPtr, i->nItem, &Position);
4836 Position.y += Origin.y;
4838 /* iterate through the invalidated columns */
4839 while(iterator_next(&j))
4841 LISTVIEW_GetHeaderRect(infoPtr, j.nItem, &rcItem);
4842 Position.x = (j.nItem == 0) ? rcItem.left + Origin.x : Origin.x;
4844 if (rgntype == COMPLEXREGION && !((infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) && j.nItem == 0))
4846 rcItem.top = 0;
4847 rcItem.bottom = infoPtr->nItemHeight;
4848 OffsetRect(&rcItem, Origin.x, Position.y);
4849 if (!RectVisible(hdc, &rcItem)) continue;
4852 LISTVIEW_DrawItem(infoPtr, hdc, i->nItem, j.nItem, Position, cdmode);
4855 iterator_destroy(&j);
4858 /***
4859 * DESCRIPTION:
4860 * Draws the gridlines if necessary when in report display mode.
4862 * PARAMETER(S):
4863 * [I] infoPtr : valid pointer to the listview structure
4864 * [I] hdc : device context handle
4866 * RETURN:
4867 * None
4869 static void LISTVIEW_RefreshReportGrid(LISTVIEW_INFO *infoPtr, HDC hdc)
4871 INT rgntype;
4872 INT y, itemheight;
4873 INT col, index;
4874 HPEN hPen, hOldPen;
4875 RECT rcClip, rcItem = {0};
4876 POINT Origin;
4877 RANGES colRanges;
4878 ITERATOR j;
4879 BOOL rmost = FALSE;
4881 TRACE("()\n");
4883 /* figure out what to draw */
4884 rgntype = GetClipBox(hdc, &rcClip);
4885 if (rgntype == NULLREGION) return;
4887 /* Get scroll info once before loop */
4888 LISTVIEW_GetOrigin(infoPtr, &Origin);
4890 colRanges = ranges_create(DPA_GetPtrCount(infoPtr->hdpaColumns));
4892 /* narrow down the columns we need to paint */
4893 for(col = 0; col < DPA_GetPtrCount(infoPtr->hdpaColumns); col++)
4895 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, col, 0);
4897 LISTVIEW_GetHeaderRect(infoPtr, index, &rcItem);
4898 if ((rcItem.right + Origin.x >= rcClip.left) && (rcItem.left + Origin.x < rcClip.right))
4899 ranges_additem(colRanges, index);
4902 /* is right most vertical line visible? */
4903 if (DPA_GetPtrCount(infoPtr->hdpaColumns) > 0)
4905 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, 0);
4906 LISTVIEW_GetHeaderRect(infoPtr, index, &rcItem);
4907 rmost = (rcItem.right + Origin.x < rcClip.right);
4910 if ((hPen = CreatePen( PS_SOLID, 1, comctl32_color.clr3dFace )))
4912 hOldPen = SelectObject ( hdc, hPen );
4914 /* draw the vertical lines for the columns */
4915 iterator_rangesitems(&j, colRanges);
4916 while(iterator_next(&j))
4918 LISTVIEW_GetHeaderRect(infoPtr, j.nItem, &rcItem);
4919 if (rcItem.left == 0) continue; /* skip leftmost column */
4920 rcItem.left += Origin.x;
4921 rcItem.right += Origin.x;
4922 rcItem.top = infoPtr->rcList.top;
4923 rcItem.bottom = infoPtr->rcList.bottom;
4924 TRACE("vert col=%d, rcItem=%s\n", j.nItem, wine_dbgstr_rect(&rcItem));
4925 MoveToEx (hdc, rcItem.left, rcItem.top, NULL);
4926 LineTo (hdc, rcItem.left, rcItem.bottom);
4928 iterator_destroy(&j);
4929 /* draw rightmost grid line if visible */
4930 if (rmost)
4932 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX,
4933 DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, 0);
4934 LISTVIEW_GetHeaderRect(infoPtr, index, &rcItem);
4936 rcItem.right += Origin.x;
4938 MoveToEx (hdc, rcItem.right, infoPtr->rcList.top, NULL);
4939 LineTo (hdc, rcItem.right, infoPtr->rcList.bottom);
4942 /* draw the horizontal lines for the rows */
4943 itemheight = LISTVIEW_CalculateItemHeight(infoPtr);
4944 rcItem.left = infoPtr->rcList.left;
4945 rcItem.right = infoPtr->rcList.right;
4946 for(y = Origin.y > 1 ? Origin.y - 1 : itemheight - 1 + Origin.y % itemheight; y<=infoPtr->rcList.bottom; y+=itemheight)
4948 rcItem.bottom = rcItem.top = y;
4949 TRACE("horz rcItem=%s\n", wine_dbgstr_rect(&rcItem));
4950 MoveToEx (hdc, rcItem.left, rcItem.top, NULL);
4951 LineTo (hdc, rcItem.right, rcItem.top);
4954 SelectObject( hdc, hOldPen );
4955 DeleteObject( hPen );
4959 /***
4960 * DESCRIPTION:
4961 * Draws listview items when in list display mode.
4963 * PARAMETER(S):
4964 * [I] infoPtr : valid pointer to the listview structure
4965 * [I] hdc : device context handle
4966 * [I] cdmode : custom draw mode
4968 * RETURN:
4969 * None
4971 static void LISTVIEW_RefreshList(LISTVIEW_INFO *infoPtr, ITERATOR *i, HDC hdc, DWORD cdmode)
4973 POINT Origin, Position;
4975 /* Get scroll info once before loop */
4976 LISTVIEW_GetOrigin(infoPtr, &Origin);
4978 while(iterator_prev(i))
4980 LISTVIEW_GetItemOrigin(infoPtr, i->nItem, &Position);
4981 Position.x += Origin.x;
4982 Position.y += Origin.y;
4984 LISTVIEW_DrawItem(infoPtr, hdc, i->nItem, 0, Position, cdmode);
4989 /***
4990 * DESCRIPTION:
4991 * Draws listview items.
4993 * PARAMETER(S):
4994 * [I] infoPtr : valid pointer to the listview structure
4995 * [I] hdc : device context handle
4996 * [I] prcErase : rect to be erased before refresh (may be NULL)
4998 * RETURN:
4999 * NoneX
5001 static void LISTVIEW_Refresh(LISTVIEW_INFO *infoPtr, HDC hdc, const RECT *prcErase)
5003 COLORREF oldTextColor = 0, oldBkColor = 0, oldClrTextBk, oldClrText;
5004 NMLVCUSTOMDRAW nmlvcd;
5005 HFONT hOldFont = 0;
5006 DWORD cdmode;
5007 INT oldBkMode = 0;
5008 RECT rcClient;
5009 ITERATOR i;
5010 HDC hdcOrig = hdc;
5011 HBITMAP hbmp = NULL;
5012 RANGE range;
5014 LISTVIEW_DUMP(infoPtr);
5016 if (infoPtr->dwLvExStyle & LVS_EX_DOUBLEBUFFER) {
5017 TRACE("double buffering\n");
5019 hdc = CreateCompatibleDC(hdcOrig);
5020 if (!hdc) {
5021 ERR("Failed to create DC for backbuffer\n");
5022 return;
5024 hbmp = CreateCompatibleBitmap(hdcOrig, infoPtr->rcList.right,
5025 infoPtr->rcList.bottom);
5026 if (!hbmp) {
5027 ERR("Failed to create bitmap for backbuffer\n");
5028 DeleteDC(hdc);
5029 return;
5032 SelectObject(hdc, hbmp);
5033 SelectObject(hdc, infoPtr->hFont);
5035 if(GetClipBox(hdcOrig, &rcClient))
5036 IntersectClipRect(hdc, rcClient.left, rcClient.top, rcClient.right, rcClient.bottom);
5037 } else {
5038 /* Save dc values we're gonna trash while drawing
5039 * FIXME: Should be done in LISTVIEW_DrawItem() */
5040 hOldFont = SelectObject(hdc, infoPtr->hFont);
5041 oldBkMode = GetBkMode(hdc);
5042 oldBkColor = GetBkColor(hdc);
5043 oldTextColor = GetTextColor(hdc);
5046 infoPtr->bIsDrawing = TRUE;
5048 if (prcErase) {
5049 LISTVIEW_FillBkgnd(infoPtr, hdc, prcErase);
5050 } else if (infoPtr->dwLvExStyle & LVS_EX_DOUBLEBUFFER) {
5051 /* If no erasing was done (usually because RedrawWindow was called
5052 * with RDW_INVALIDATE only) we need to copy the old contents into
5053 * the backbuffer before continuing. */
5054 BitBlt(hdc, infoPtr->rcList.left, infoPtr->rcList.top,
5055 infoPtr->rcList.right - infoPtr->rcList.left,
5056 infoPtr->rcList.bottom - infoPtr->rcList.top,
5057 hdcOrig, infoPtr->rcList.left, infoPtr->rcList.top, SRCCOPY);
5060 /* FIXME: Shouldn't need to do this */
5061 oldClrTextBk = infoPtr->clrTextBk;
5062 oldClrText = infoPtr->clrText;
5064 infoPtr->cditemmode = CDRF_DODEFAULT;
5066 GetClientRect(infoPtr->hwndSelf, &rcClient);
5067 customdraw_fill(&nmlvcd, infoPtr, hdc, &rcClient, 0);
5068 cdmode = notify_customdraw(infoPtr, CDDS_PREPAINT, &nmlvcd);
5069 if (cdmode & CDRF_SKIPDEFAULT) goto enddraw;
5070 prepaint_setup(infoPtr, hdc, &nmlvcd, FALSE);
5072 /* Use these colors to draw the items */
5073 infoPtr->clrTextBk = nmlvcd.clrTextBk;
5074 infoPtr->clrText = nmlvcd.clrText;
5076 /* nothing to draw */
5077 if(infoPtr->nItemCount == 0) goto enddraw;
5079 /* figure out what we need to draw */
5080 iterator_visibleitems(&i, infoPtr, hdc);
5081 range = iterator_range(&i);
5083 /* send cache hint notification */
5084 if (infoPtr->dwStyle & LVS_OWNERDATA)
5086 NMLVCACHEHINT nmlv;
5088 ZeroMemory(&nmlv, sizeof(NMLVCACHEHINT));
5089 nmlv.iFrom = range.lower;
5090 nmlv.iTo = range.upper - 1;
5091 notify_hdr(infoPtr, LVN_ODCACHEHINT, &nmlv.hdr);
5094 if ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (infoPtr->uView == LV_VIEW_DETAILS))
5095 LISTVIEW_RefreshOwnerDraw(infoPtr, &i, hdc, cdmode);
5096 else
5098 if (infoPtr->uView == LV_VIEW_DETAILS)
5099 LISTVIEW_RefreshReport(infoPtr, &i, hdc, cdmode);
5100 else /* LV_VIEW_LIST, LV_VIEW_ICON or LV_VIEW_SMALLICON */
5101 LISTVIEW_RefreshList(infoPtr, &i, hdc, cdmode);
5103 /* if we have a focus rect and it's visible, draw it */
5104 if (infoPtr->bFocus && range.lower <= infoPtr->nFocusedItem &&
5105 (range.upper - 1) >= infoPtr->nFocusedItem)
5106 LISTVIEW_DrawFocusRect(infoPtr, hdc);
5108 iterator_destroy(&i);
5110 enddraw:
5111 /* For LVS_EX_GRIDLINES go and draw lines */
5112 /* This includes the case where there were *no* items */
5113 if ((infoPtr->uView == LV_VIEW_DETAILS) && infoPtr->dwLvExStyle & LVS_EX_GRIDLINES)
5114 LISTVIEW_RefreshReportGrid(infoPtr, hdc);
5116 /* Draw marquee rectangle if appropriate */
5117 if (infoPtr->bMarqueeSelect)
5118 DrawFocusRect(hdc, &infoPtr->marqueeDrawRect);
5120 if (cdmode & CDRF_NOTIFYPOSTPAINT)
5121 notify_postpaint(infoPtr, &nmlvcd);
5123 infoPtr->clrTextBk = oldClrTextBk;
5124 infoPtr->clrText = oldClrText;
5126 if(hbmp) {
5127 BitBlt(hdcOrig, infoPtr->rcList.left, infoPtr->rcList.top,
5128 infoPtr->rcList.right - infoPtr->rcList.left,
5129 infoPtr->rcList.bottom - infoPtr->rcList.top,
5130 hdc, infoPtr->rcList.left, infoPtr->rcList.top, SRCCOPY);
5132 DeleteObject(hbmp);
5133 DeleteDC(hdc);
5134 } else {
5135 SelectObject(hdc, hOldFont);
5136 SetBkMode(hdc, oldBkMode);
5137 SetBkColor(hdc, oldBkColor);
5138 SetTextColor(hdc, oldTextColor);
5141 infoPtr->bIsDrawing = FALSE;
5145 /***
5146 * DESCRIPTION:
5147 * Calculates the approximate width and height of a given number of items.
5149 * PARAMETER(S):
5150 * [I] infoPtr : valid pointer to the listview structure
5151 * [I] nItemCount : number of items
5152 * [I] wWidth : width
5153 * [I] wHeight : height
5155 * RETURN:
5156 * Returns a DWORD. The width in the low word and the height in high word.
5158 static DWORD LISTVIEW_ApproximateViewRect(const LISTVIEW_INFO *infoPtr, INT nItemCount,
5159 WORD wWidth, WORD wHeight)
5161 DWORD dwViewRect = 0;
5163 if (nItemCount == -1)
5164 nItemCount = infoPtr->nItemCount;
5166 if (infoPtr->uView == LV_VIEW_LIST)
5168 INT nItemCountPerColumn = 1;
5169 INT nColumnCount = 0;
5171 if (wHeight == 0xFFFF)
5173 /* use current height */
5174 wHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
5177 if (wHeight < infoPtr->nItemHeight)
5178 wHeight = infoPtr->nItemHeight;
5180 if (nItemCount > 0)
5182 if (infoPtr->nItemHeight > 0)
5184 nItemCountPerColumn = wHeight / infoPtr->nItemHeight;
5185 if (nItemCountPerColumn == 0)
5186 nItemCountPerColumn = 1;
5188 if (nItemCount % nItemCountPerColumn != 0)
5189 nColumnCount = nItemCount / nItemCountPerColumn;
5190 else
5191 nColumnCount = nItemCount / nItemCountPerColumn + 1;
5195 /* Microsoft padding magic */
5196 wHeight = nItemCountPerColumn * infoPtr->nItemHeight + 2;
5197 wWidth = nColumnCount * infoPtr->nItemWidth + 2;
5199 dwViewRect = MAKELONG(wWidth, wHeight);
5201 else if (infoPtr->uView == LV_VIEW_DETAILS)
5203 RECT rcBox;
5205 if (infoPtr->nItemCount > 0)
5207 LISTVIEW_GetItemBox(infoPtr, 0, &rcBox);
5208 wWidth = rcBox.right - rcBox.left;
5209 wHeight = (rcBox.bottom - rcBox.top) * nItemCount;
5211 else
5213 /* use current height and width */
5214 if (wHeight == 0xffff)
5215 wHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
5216 if (wWidth == 0xffff)
5217 wWidth = infoPtr->rcList.right - infoPtr->rcList.left;
5220 dwViewRect = MAKELONG(wWidth, wHeight);
5222 else if (infoPtr->uView == LV_VIEW_ICON)
5224 UINT rows,cols;
5225 UINT nItemWidth;
5226 UINT nItemHeight;
5228 nItemWidth = infoPtr->iconSpacing.cx;
5229 nItemHeight = infoPtr->iconSpacing.cy;
5231 if (wWidth == 0xffff)
5232 wWidth = infoPtr->rcList.right - infoPtr->rcList.left;
5234 if (wWidth < nItemWidth)
5235 wWidth = nItemWidth;
5237 cols = wWidth / nItemWidth;
5238 if (cols > nItemCount)
5239 cols = nItemCount;
5240 if (cols < 1)
5241 cols = 1;
5243 if (nItemCount)
5245 rows = nItemCount / cols;
5246 if (nItemCount % cols)
5247 rows++;
5249 else
5250 rows = 0;
5252 wHeight = (nItemHeight * rows)+2;
5253 wWidth = (nItemWidth * cols)+2;
5255 dwViewRect = MAKELONG(wWidth, wHeight);
5257 else if (infoPtr->uView == LV_VIEW_SMALLICON)
5258 FIXME("uView == LV_VIEW_SMALLICON: not implemented\n");
5260 return dwViewRect;
5263 /***
5264 * DESCRIPTION:
5265 * Cancel edit label with saving item text.
5267 * PARAMETER(S):
5268 * [I] infoPtr : valid pointer to the listview structure
5270 * RETURN:
5271 * Always returns TRUE.
5273 static LRESULT LISTVIEW_CancelEditLabel(LISTVIEW_INFO *infoPtr)
5275 if (infoPtr->hwndEdit)
5277 /* handle value will be lost after LISTVIEW_EndEditLabelT */
5278 HWND edit = infoPtr->hwndEdit;
5280 LISTVIEW_EndEditLabelT(infoPtr, TRUE, IsWindowUnicode(infoPtr->hwndEdit));
5281 SendMessageW(edit, WM_CLOSE, 0, 0);
5284 return TRUE;
5287 /***
5288 * DESCRIPTION:
5289 * Create a drag image list for the specified item.
5291 * PARAMETER(S):
5292 * [I] infoPtr : valid pointer to the listview structure
5293 * [I] iItem : index of item
5294 * [O] lppt : Upper-left corner of the image
5296 * RETURN:
5297 * Returns a handle to the image list if successful, NULL otherwise.
5299 static HIMAGELIST LISTVIEW_CreateDragImage(LISTVIEW_INFO *infoPtr, INT iItem, LPPOINT lppt)
5301 RECT rcItem;
5302 SIZE size;
5303 POINT pos;
5304 HDC hdc, hdcOrig;
5305 HBITMAP hbmp, hOldbmp;
5306 HIMAGELIST dragList = 0;
5307 TRACE("iItem=%d Count=%d\n", iItem, infoPtr->nItemCount);
5309 if (iItem < 0 || iItem >= infoPtr->nItemCount || !lppt)
5310 return 0;
5312 rcItem.left = LVIR_BOUNDS;
5313 if (!LISTVIEW_GetItemRect(infoPtr, iItem, &rcItem))
5314 return 0;
5316 lppt->x = rcItem.left;
5317 lppt->y = rcItem.top;
5319 size.cx = rcItem.right - rcItem.left;
5320 size.cy = rcItem.bottom - rcItem.top;
5322 hdcOrig = GetDC(infoPtr->hwndSelf);
5323 hdc = CreateCompatibleDC(hdcOrig);
5324 hbmp = CreateCompatibleBitmap(hdcOrig, size.cx, size.cy);
5325 hOldbmp = SelectObject(hdc, hbmp);
5327 rcItem.left = rcItem.top = 0;
5328 rcItem.right = size.cx;
5329 rcItem.bottom = size.cy;
5330 FillRect(hdc, &rcItem, infoPtr->hBkBrush);
5332 pos.x = pos.y = 0;
5333 if (LISTVIEW_DrawItem(infoPtr, hdc, iItem, 0, pos, infoPtr->cditemmode))
5335 dragList = ImageList_Create(size.cx, size.cy, ILC_COLOR, 10, 10);
5336 SelectObject(hdc, hOldbmp);
5337 ImageList_Add(dragList, hbmp, 0);
5339 else
5340 SelectObject(hdc, hOldbmp);
5342 DeleteObject(hbmp);
5343 DeleteDC(hdc);
5344 ReleaseDC(infoPtr->hwndSelf, hdcOrig);
5346 TRACE("ret=%p\n", dragList);
5348 return dragList;
5352 /***
5353 * DESCRIPTION:
5354 * Removes all listview items and subitems.
5356 * PARAMETER(S):
5357 * [I] infoPtr : valid pointer to the listview structure
5359 * RETURN:
5360 * SUCCESS : TRUE
5361 * FAILURE : FALSE
5363 static BOOL LISTVIEW_DeleteAllItems(LISTVIEW_INFO *infoPtr, BOOL destroy)
5365 NMLISTVIEW nmlv;
5366 HDPA hdpaSubItems = NULL;
5367 BOOL bSuppress;
5368 ITEMHDR *hdrItem;
5369 ITEM_INFO *lpItem;
5370 ITEM_ID *lpID;
5371 INT i, j;
5373 TRACE("()\n");
5375 /* we do it directly, to avoid notifications */
5376 ranges_clear(infoPtr->selectionRanges);
5377 infoPtr->nSelectionMark = -1;
5378 infoPtr->nFocusedItem = -1;
5379 SetRectEmpty(&infoPtr->rcFocus);
5380 /* But we are supposed to leave nHotItem as is! */
5383 /* send LVN_DELETEALLITEMS notification */
5384 ZeroMemory(&nmlv, sizeof(NMLISTVIEW));
5385 nmlv.iItem = -1;
5386 bSuppress = notify_listview(infoPtr, LVN_DELETEALLITEMS, &nmlv);
5388 for (i = infoPtr->nItemCount - 1; i >= 0; i--)
5390 if (!(infoPtr->dwStyle & LVS_OWNERDATA))
5392 /* send LVN_DELETEITEM notification, if not suppressed
5393 and if it is not a virtual listview */
5394 if (!bSuppress) notify_deleteitem(infoPtr, i);
5395 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, i);
5396 lpItem = DPA_GetPtr(hdpaSubItems, 0);
5397 /* free id struct */
5398 j = DPA_GetPtrIndex(infoPtr->hdpaItemIds, lpItem->id);
5399 lpID = DPA_GetPtr(infoPtr->hdpaItemIds, j);
5400 DPA_DeletePtr(infoPtr->hdpaItemIds, j);
5401 Free(lpID);
5402 /* both item and subitem start with ITEMHDR header */
5403 for (j = 0; j < DPA_GetPtrCount(hdpaSubItems); j++)
5405 hdrItem = DPA_GetPtr(hdpaSubItems, j);
5406 if (is_text(hdrItem->pszText)) Free(hdrItem->pszText);
5407 Free(hdrItem);
5409 DPA_Destroy(hdpaSubItems);
5410 DPA_DeletePtr(infoPtr->hdpaItems, i);
5412 DPA_DeletePtr(infoPtr->hdpaPosX, i);
5413 DPA_DeletePtr(infoPtr->hdpaPosY, i);
5414 infoPtr->nItemCount --;
5417 if (!destroy)
5419 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
5420 LISTVIEW_UpdateScroll(infoPtr);
5422 LISTVIEW_InvalidateList(infoPtr);
5424 return TRUE;
5427 /***
5428 * DESCRIPTION:
5429 * Scrolls, and updates the columns, when a column is changing width.
5431 * PARAMETER(S):
5432 * [I] infoPtr : valid pointer to the listview structure
5433 * [I] nColumn : column to scroll
5434 * [I] dx : amount of scroll, in pixels
5436 * RETURN:
5437 * None.
5439 static void LISTVIEW_ScrollColumns(LISTVIEW_INFO *infoPtr, INT nColumn, INT dx)
5441 COLUMN_INFO *lpColumnInfo;
5442 RECT rcOld, rcCol;
5443 POINT ptOrigin;
5444 INT nCol;
5445 HDITEMW hdi;
5447 if (nColumn < 0 || DPA_GetPtrCount(infoPtr->hdpaColumns) < 1) return;
5448 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, min(nColumn, DPA_GetPtrCount(infoPtr->hdpaColumns) - 1));
5449 rcCol = lpColumnInfo->rcHeader;
5450 if (nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns))
5451 rcCol.left = rcCol.right;
5453 /* adjust the other columns */
5454 hdi.mask = HDI_ORDER;
5455 if (SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nColumn, (LPARAM)&hdi))
5457 INT nOrder = hdi.iOrder;
5458 for (nCol = 0; nCol < DPA_GetPtrCount(infoPtr->hdpaColumns); nCol++)
5460 hdi.mask = HDI_ORDER;
5461 SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nCol, (LPARAM)&hdi);
5462 if (hdi.iOrder >= nOrder) {
5463 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, nCol);
5464 lpColumnInfo->rcHeader.left += dx;
5465 lpColumnInfo->rcHeader.right += dx;
5470 /* do not update screen if not in report mode */
5471 if (!is_redrawing(infoPtr) || infoPtr->uView != LV_VIEW_DETAILS) return;
5473 /* Need to reset the item width when inserting a new column */
5474 infoPtr->nItemWidth += dx;
5476 LISTVIEW_UpdateScroll(infoPtr);
5477 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
5479 /* scroll to cover the deleted column, and invalidate for redraw */
5480 rcOld = infoPtr->rcList;
5481 rcOld.left = ptOrigin.x + rcCol.left + dx;
5482 ScrollWindowEx(infoPtr->hwndSelf, dx, 0, &rcOld, &rcOld, 0, 0, SW_ERASE | SW_INVALIDATE);
5485 /***
5486 * DESCRIPTION:
5487 * Removes a column from the listview control.
5489 * PARAMETER(S):
5490 * [I] infoPtr : valid pointer to the listview structure
5491 * [I] nColumn : column index
5493 * RETURN:
5494 * SUCCESS : TRUE
5495 * FAILURE : FALSE
5497 static BOOL LISTVIEW_DeleteColumn(LISTVIEW_INFO *infoPtr, INT nColumn)
5499 RECT rcCol;
5501 TRACE("nColumn=%d\n", nColumn);
5503 if (nColumn < 0 || DPA_GetPtrCount(infoPtr->hdpaColumns) == 0
5504 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
5506 /* While the MSDN specifically says that column zero should not be deleted,
5507 what actually happens is that the column itself is deleted but no items or subitems
5508 are removed.
5511 LISTVIEW_GetHeaderRect(infoPtr, nColumn, &rcCol);
5513 if (!SendMessageW(infoPtr->hwndHeader, HDM_DELETEITEM, nColumn, 0))
5514 return FALSE;
5516 Free(DPA_GetPtr(infoPtr->hdpaColumns, nColumn));
5517 DPA_DeletePtr(infoPtr->hdpaColumns, nColumn);
5519 if (!(infoPtr->dwStyle & LVS_OWNERDATA) && nColumn)
5521 SUBITEM_INFO *lpSubItem, *lpDelItem;
5522 HDPA hdpaSubItems;
5523 INT nItem, nSubItem, i;
5525 for (nItem = 0; nItem < infoPtr->nItemCount; nItem++)
5527 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, nItem);
5528 nSubItem = 0;
5529 lpDelItem = 0;
5530 for (i = 1; i < DPA_GetPtrCount(hdpaSubItems); i++)
5532 lpSubItem = DPA_GetPtr(hdpaSubItems, i);
5533 if (lpSubItem->iSubItem == nColumn)
5535 nSubItem = i;
5536 lpDelItem = lpSubItem;
5538 else if (lpSubItem->iSubItem > nColumn)
5540 lpSubItem->iSubItem--;
5544 /* if we found our subitem, zap it */
5545 if (nSubItem > 0)
5547 /* free string */
5548 if (is_text(lpDelItem->hdr.pszText))
5549 Free(lpDelItem->hdr.pszText);
5551 /* free item */
5552 Free(lpDelItem);
5554 /* free dpa memory */
5555 DPA_DeletePtr(hdpaSubItems, nSubItem);
5560 /* update the other column info */
5561 LISTVIEW_UpdateItemSize(infoPtr);
5562 if(DPA_GetPtrCount(infoPtr->hdpaColumns) == 0)
5563 LISTVIEW_InvalidateList(infoPtr);
5564 else
5565 LISTVIEW_ScrollColumns(infoPtr, nColumn, -(rcCol.right - rcCol.left));
5567 return TRUE;
5570 /***
5571 * DESCRIPTION:
5572 * Invalidates the listview after an item's insertion or deletion.
5574 * PARAMETER(S):
5575 * [I] infoPtr : valid pointer to the listview structure
5576 * [I] nItem : item index
5577 * [I] dir : -1 if deleting, 1 if inserting
5579 * RETURN:
5580 * None
5582 static void LISTVIEW_ScrollOnInsert(LISTVIEW_INFO *infoPtr, INT nItem, INT dir)
5584 INT nPerCol, nItemCol, nItemRow;
5585 RECT rcScroll;
5586 POINT Origin;
5588 /* if we don't refresh, what's the point of scrolling? */
5589 if (!is_redrawing(infoPtr)) return;
5591 assert (abs(dir) == 1);
5593 /* arrange icons if autoarrange is on */
5594 if (is_autoarrange(infoPtr))
5596 BOOL arrange = TRUE;
5597 if (dir < 0 && nItem >= infoPtr->nItemCount) arrange = FALSE;
5598 if (dir > 0 && nItem == infoPtr->nItemCount - 1) arrange = FALSE;
5599 if (arrange) LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
5602 /* scrollbars need updating */
5603 LISTVIEW_UpdateScroll(infoPtr);
5605 /* figure out the item's position */
5606 if (infoPtr->uView == LV_VIEW_DETAILS)
5607 nPerCol = infoPtr->nItemCount + 1;
5608 else if (infoPtr->uView == LV_VIEW_LIST)
5609 nPerCol = LISTVIEW_GetCountPerColumn(infoPtr);
5610 else /* LV_VIEW_ICON, or LV_VIEW_SMALLICON */
5611 return;
5613 nItemCol = nItem / nPerCol;
5614 nItemRow = nItem % nPerCol;
5615 LISTVIEW_GetOrigin(infoPtr, &Origin);
5617 /* move the items below up a slot */
5618 rcScroll.left = nItemCol * infoPtr->nItemWidth;
5619 rcScroll.top = nItemRow * infoPtr->nItemHeight;
5620 rcScroll.right = rcScroll.left + infoPtr->nItemWidth;
5621 rcScroll.bottom = nPerCol * infoPtr->nItemHeight;
5622 OffsetRect(&rcScroll, Origin.x, Origin.y);
5623 TRACE("rcScroll=%s, dx=%d\n", wine_dbgstr_rect(&rcScroll), dir * infoPtr->nItemHeight);
5624 if (IntersectRect(&rcScroll, &rcScroll, &infoPtr->rcList))
5626 TRACE("Scrolling rcScroll=%s, rcList=%s\n", wine_dbgstr_rect(&rcScroll), wine_dbgstr_rect(&infoPtr->rcList));
5627 ScrollWindowEx(infoPtr->hwndSelf, 0, dir * infoPtr->nItemHeight,
5628 &rcScroll, &rcScroll, 0, 0, SW_ERASE | SW_INVALIDATE);
5631 /* report has only that column, so we're done */
5632 if (infoPtr->uView == LV_VIEW_DETAILS) return;
5634 /* now for LISTs, we have to deal with the columns to the right */
5635 rcScroll.left = (nItemCol + 1) * infoPtr->nItemWidth;
5636 rcScroll.top = 0;
5637 rcScroll.right = (infoPtr->nItemCount / nPerCol + 1) * infoPtr->nItemWidth;
5638 rcScroll.bottom = nPerCol * infoPtr->nItemHeight;
5639 OffsetRect(&rcScroll, Origin.x, Origin.y);
5640 if (IntersectRect(&rcScroll, &rcScroll, &infoPtr->rcList))
5641 ScrollWindowEx(infoPtr->hwndSelf, 0, dir * infoPtr->nItemHeight,
5642 &rcScroll, &rcScroll, 0, 0, SW_ERASE | SW_INVALIDATE);
5645 /***
5646 * DESCRIPTION:
5647 * Removes an item from the listview control.
5649 * PARAMETER(S):
5650 * [I] infoPtr : valid pointer to the listview structure
5651 * [I] nItem : item index
5653 * RETURN:
5654 * SUCCESS : TRUE
5655 * FAILURE : FALSE
5657 static BOOL LISTVIEW_DeleteItem(LISTVIEW_INFO *infoPtr, INT nItem)
5659 LVITEMW item;
5660 const BOOL is_icon = (infoPtr->uView == LV_VIEW_SMALLICON || infoPtr->uView == LV_VIEW_ICON);
5662 TRACE("(nItem=%d)\n", nItem);
5664 if (nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
5666 /* remove selection, and focus */
5667 item.state = 0;
5668 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
5669 LISTVIEW_SetItemState(infoPtr, nItem, &item);
5671 /* send LVN_DELETEITEM notification. */
5672 if (!notify_deleteitem(infoPtr, nItem)) return FALSE;
5674 /* we need to do this here, because we'll be deleting stuff */
5675 if (is_icon)
5676 LISTVIEW_InvalidateItem(infoPtr, nItem);
5678 if (!(infoPtr->dwStyle & LVS_OWNERDATA))
5680 HDPA hdpaSubItems;
5681 ITEMHDR *hdrItem;
5682 ITEM_INFO *lpItem;
5683 ITEM_ID *lpID;
5684 INT i;
5686 hdpaSubItems = DPA_DeletePtr(infoPtr->hdpaItems, nItem);
5687 lpItem = DPA_GetPtr(hdpaSubItems, 0);
5689 /* free id struct */
5690 i = DPA_GetPtrIndex(infoPtr->hdpaItemIds, lpItem->id);
5691 lpID = DPA_GetPtr(infoPtr->hdpaItemIds, i);
5692 DPA_DeletePtr(infoPtr->hdpaItemIds, i);
5693 Free(lpID);
5694 for (i = 0; i < DPA_GetPtrCount(hdpaSubItems); i++)
5696 hdrItem = DPA_GetPtr(hdpaSubItems, i);
5697 if (is_text(hdrItem->pszText)) Free(hdrItem->pszText);
5698 Free(hdrItem);
5700 DPA_Destroy(hdpaSubItems);
5703 if (is_icon)
5705 DPA_DeletePtr(infoPtr->hdpaPosX, nItem);
5706 DPA_DeletePtr(infoPtr->hdpaPosY, nItem);
5709 infoPtr->nItemCount--;
5710 LISTVIEW_ShiftIndices(infoPtr, nItem, -1);
5712 /* now is the invalidation fun */
5713 if (!is_icon)
5714 LISTVIEW_ScrollOnInsert(infoPtr, nItem, -1);
5715 return TRUE;
5719 /***
5720 * DESCRIPTION:
5721 * Callback implementation for editlabel control
5723 * PARAMETER(S):
5724 * [I] infoPtr : valid pointer to the listview structure
5725 * [I] storeText : store edit box text as item text
5726 * [I] isW : TRUE if psxText is Unicode, FALSE if it's ANSI
5728 * RETURN:
5729 * SUCCESS : TRUE
5730 * FAILURE : FALSE
5732 static BOOL LISTVIEW_EndEditLabelT(LISTVIEW_INFO *infoPtr, BOOL storeText, BOOL isW)
5734 HWND hwndSelf = infoPtr->hwndSelf;
5735 WCHAR szDispText[DISP_TEXT_SIZE] = { 0 };
5736 NMLVDISPINFOW dispInfo;
5737 INT editedItem = infoPtr->nEditLabelItem;
5738 BOOL same;
5739 WCHAR *pszText = NULL;
5740 BOOL res;
5742 if (storeText)
5744 DWORD len = isW ? GetWindowTextLengthW(infoPtr->hwndEdit) : GetWindowTextLengthA(infoPtr->hwndEdit);
5746 if (len)
5748 if ((pszText = Alloc((len+1) * (isW ? sizeof(WCHAR) : sizeof(CHAR)))))
5750 if (isW) GetWindowTextW(infoPtr->hwndEdit, pszText, len+1);
5751 else GetWindowTextA(infoPtr->hwndEdit, (CHAR*)pszText, len+1);
5756 TRACE("(pszText=%s, isW=%d)\n", debugtext_t(pszText, isW), isW);
5758 ZeroMemory(&dispInfo, sizeof(dispInfo));
5759 dispInfo.item.mask = LVIF_PARAM | LVIF_STATE | LVIF_TEXT;
5760 dispInfo.item.iItem = editedItem;
5761 dispInfo.item.iSubItem = 0;
5762 dispInfo.item.stateMask = ~0;
5763 dispInfo.item.pszText = szDispText;
5764 dispInfo.item.cchTextMax = DISP_TEXT_SIZE;
5765 if (!LISTVIEW_GetItemT(infoPtr, &dispInfo.item, isW))
5767 res = FALSE;
5768 goto cleanup;
5771 if (isW)
5772 same = (lstrcmpW(dispInfo.item.pszText, pszText) == 0);
5773 else
5775 LPWSTR tmp = textdupTtoW(pszText, FALSE);
5776 same = (lstrcmpW(dispInfo.item.pszText, tmp) == 0);
5777 textfreeT(tmp, FALSE);
5780 /* add the text from the edit in */
5781 dispInfo.item.mask |= LVIF_TEXT;
5782 dispInfo.item.pszText = same ? NULL : pszText;
5783 dispInfo.item.cchTextMax = textlenT(dispInfo.item.pszText, isW);
5785 /* Do we need to update the Item Text */
5786 res = notify_dispinfoT(infoPtr, LVN_ENDLABELEDITW, &dispInfo, isW);
5788 infoPtr->nEditLabelItem = -1;
5789 infoPtr->hwndEdit = 0;
5791 if (!res) goto cleanup;
5793 if (!IsWindow(hwndSelf))
5795 res = FALSE;
5796 goto cleanup;
5798 if (!pszText) return TRUE;
5799 if (same)
5801 res = TRUE;
5802 goto cleanup;
5805 if (!(infoPtr->dwStyle & LVS_OWNERDATA))
5807 HDPA hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, editedItem);
5808 ITEM_INFO* lpItem = DPA_GetPtr(hdpaSubItems, 0);
5809 if (lpItem && lpItem->hdr.pszText == LPSTR_TEXTCALLBACKW)
5811 LISTVIEW_InvalidateItem(infoPtr, editedItem);
5812 res = TRUE;
5813 goto cleanup;
5817 ZeroMemory(&dispInfo, sizeof(dispInfo));
5818 dispInfo.item.mask = LVIF_TEXT;
5819 dispInfo.item.iItem = editedItem;
5820 dispInfo.item.iSubItem = 0;
5821 dispInfo.item.pszText = pszText;
5822 dispInfo.item.cchTextMax = textlenT(pszText, isW);
5823 res = LISTVIEW_SetItemT(infoPtr, &dispInfo.item, isW);
5825 cleanup:
5826 Free(pszText);
5828 return res;
5831 /***
5832 * DESCRIPTION:
5833 * Subclassed edit control windproc function
5835 * PARAMETER(S):
5836 * [I] hwnd : the edit window handle
5837 * [I] uMsg : the message that is to be processed
5838 * [I] wParam : first message parameter
5839 * [I] lParam : second message parameter
5840 * [I] isW : TRUE if input is Unicode
5842 * RETURN:
5843 * Zero.
5845 static LRESULT EditLblWndProcT(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL isW)
5847 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongPtrW(GetParent(hwnd), 0);
5848 BOOL save = TRUE;
5850 TRACE("(hwnd=%p, uMsg=%x, wParam=%lx, lParam=%lx, isW=%d)\n",
5851 hwnd, uMsg, wParam, lParam, isW);
5853 switch (uMsg)
5855 case WM_GETDLGCODE:
5856 return DLGC_WANTARROWS | DLGC_WANTALLKEYS;
5858 case WM_DESTROY:
5860 WNDPROC editProc = infoPtr->EditWndProc;
5861 infoPtr->EditWndProc = 0;
5862 SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (DWORD_PTR)editProc);
5863 return CallWindowProcT(editProc, hwnd, uMsg, wParam, lParam, isW);
5866 case WM_KEYDOWN:
5867 if (VK_ESCAPE == (INT)wParam)
5869 save = FALSE;
5870 break;
5872 else if (VK_RETURN == (INT)wParam)
5873 break;
5875 default:
5876 return CallWindowProcT(infoPtr->EditWndProc, hwnd, uMsg, wParam, lParam, isW);
5879 /* kill the edit */
5880 if (infoPtr->hwndEdit)
5881 LISTVIEW_EndEditLabelT(infoPtr, save, isW);
5883 SendMessageW(hwnd, WM_CLOSE, 0, 0);
5884 return 0;
5887 /***
5888 * DESCRIPTION:
5889 * Subclassed edit control Unicode windproc function
5891 * PARAMETER(S):
5892 * [I] hwnd : the edit window handle
5893 * [I] uMsg : the message that is to be processed
5894 * [I] wParam : first message parameter
5895 * [I] lParam : second message parameter
5897 * RETURN:
5899 static LRESULT CALLBACK EditLblWndProcW(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
5901 return EditLblWndProcT(hwnd, uMsg, wParam, lParam, TRUE);
5904 /***
5905 * DESCRIPTION:
5906 * Subclassed edit control ANSI windproc function
5908 * PARAMETER(S):
5909 * [I] hwnd : the edit window handle
5910 * [I] uMsg : the message that is to be processed
5911 * [I] wParam : first message parameter
5912 * [I] lParam : second message parameter
5914 * RETURN:
5916 static LRESULT CALLBACK EditLblWndProcA(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
5918 return EditLblWndProcT(hwnd, uMsg, wParam, lParam, FALSE);
5921 /***
5922 * DESCRIPTION:
5923 * Creates a subclassed edit control
5925 * PARAMETER(S):
5926 * [I] infoPtr : valid pointer to the listview structure
5927 * [I] text : initial text for the edit
5928 * [I] style : the window style
5929 * [I] isW : TRUE if input is Unicode
5931 * RETURN:
5933 static HWND CreateEditLabelT(LISTVIEW_INFO *infoPtr, LPCWSTR text, BOOL isW)
5935 static const DWORD style = WS_CHILDWINDOW|WS_CLIPSIBLINGS|ES_LEFT|ES_AUTOHSCROLL|WS_BORDER|WS_VISIBLE;
5936 HINSTANCE hinst = (HINSTANCE)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_HINSTANCE);
5937 HWND hedit;
5939 TRACE("(%p, text=%s, isW=%d)\n", infoPtr, debugtext_t(text, isW), isW);
5941 /* window will be resized and positioned after LVN_BEGINLABELEDIT */
5942 if (isW)
5943 hedit = CreateWindowW(WC_EDITW, text, style, 0, 0, 0, 0, infoPtr->hwndSelf, 0, hinst, 0);
5944 else
5945 hedit = CreateWindowA(WC_EDITA, (LPCSTR)text, style, 0, 0, 0, 0, infoPtr->hwndSelf, 0, hinst, 0);
5947 if (!hedit) return 0;
5949 infoPtr->EditWndProc = (WNDPROC)
5950 (isW ? SetWindowLongPtrW(hedit, GWLP_WNDPROC, (DWORD_PTR)EditLblWndProcW) :
5951 SetWindowLongPtrA(hedit, GWLP_WNDPROC, (DWORD_PTR)EditLblWndProcA) );
5953 SendMessageW(hedit, WM_SETFONT, (WPARAM)infoPtr->hFont, FALSE);
5954 SendMessageW(hedit, EM_SETLIMITTEXT, DISP_TEXT_SIZE-1, 0);
5956 return hedit;
5959 /***
5960 * DESCRIPTION:
5961 * Begin in place editing of specified list view item
5963 * PARAMETER(S):
5964 * [I] infoPtr : valid pointer to the listview structure
5965 * [I] nItem : item index
5966 * [I] isW : TRUE if it's a Unicode req, FALSE if ASCII
5968 * RETURN:
5969 * SUCCESS : TRUE
5970 * FAILURE : FALSE
5972 static HWND LISTVIEW_EditLabelT(LISTVIEW_INFO *infoPtr, INT nItem, BOOL isW)
5974 WCHAR disptextW[DISP_TEXT_SIZE] = { 0 };
5975 HWND hwndSelf = infoPtr->hwndSelf;
5976 NMLVDISPINFOW dispInfo;
5977 HFONT hOldFont = NULL;
5978 TEXTMETRICW tm;
5979 RECT rect;
5980 SIZE sz;
5981 HDC hdc;
5983 TRACE("(nItem=%d, isW=%d)\n", nItem, isW);
5985 if (~infoPtr->dwStyle & LVS_EDITLABELS) return 0;
5987 /* remove existing edit box */
5988 if (infoPtr->hwndEdit)
5990 SetFocus(infoPtr->hwndSelf);
5991 infoPtr->hwndEdit = 0;
5994 if (nItem < 0 || nItem >= infoPtr->nItemCount) return 0;
5996 infoPtr->nEditLabelItem = nItem;
5998 LISTVIEW_SetSelection(infoPtr, nItem);
5999 LISTVIEW_SetItemFocus(infoPtr, nItem);
6000 LISTVIEW_InvalidateItem(infoPtr, nItem);
6002 rect.left = LVIR_LABEL;
6003 if (!LISTVIEW_GetItemRect(infoPtr, nItem, &rect)) return 0;
6005 ZeroMemory(&dispInfo, sizeof(dispInfo));
6006 dispInfo.item.mask = LVIF_PARAM | LVIF_STATE | LVIF_TEXT;
6007 dispInfo.item.iItem = nItem;
6008 dispInfo.item.iSubItem = 0;
6009 dispInfo.item.stateMask = ~0;
6010 dispInfo.item.pszText = disptextW;
6011 dispInfo.item.cchTextMax = DISP_TEXT_SIZE;
6012 if (!LISTVIEW_GetItemT(infoPtr, &dispInfo.item, isW)) return 0;
6014 infoPtr->hwndEdit = CreateEditLabelT(infoPtr, dispInfo.item.pszText, isW);
6015 if (!infoPtr->hwndEdit) return 0;
6017 if (notify_dispinfoT(infoPtr, LVN_BEGINLABELEDITW, &dispInfo, isW))
6019 if (!IsWindow(hwndSelf))
6020 return 0;
6021 SendMessageW(infoPtr->hwndEdit, WM_CLOSE, 0, 0);
6022 infoPtr->hwndEdit = 0;
6023 return 0;
6026 TRACE("disp text=%s\n", debugtext_t(dispInfo.item.pszText, isW));
6028 /* position and display edit box */
6029 hdc = GetDC(infoPtr->hwndSelf);
6031 /* select the font to get appropriate metric dimensions */
6032 if (infoPtr->hFont)
6033 hOldFont = SelectObject(hdc, infoPtr->hFont);
6035 /* use real edit box content, it could be altered during LVN_BEGINLABELEDIT notification */
6036 GetWindowTextW(infoPtr->hwndEdit, disptextW, DISP_TEXT_SIZE);
6037 TRACE("edit box text=%s\n", debugstr_w(disptextW));
6039 /* get string length in pixels */
6040 GetTextExtentPoint32W(hdc, disptextW, lstrlenW(disptextW), &sz);
6042 /* add extra spacing for the next character */
6043 GetTextMetricsW(hdc, &tm);
6044 sz.cx += tm.tmMaxCharWidth * 2;
6046 if (infoPtr->hFont)
6047 SelectObject(hdc, hOldFont);
6049 ReleaseDC(infoPtr->hwndSelf, hdc);
6051 sz.cy = rect.bottom - rect.top + 2;
6052 rect.left -= 2;
6053 rect.top -= 1;
6054 TRACE("moving edit=(%d,%d)-(%d,%d)\n", rect.left, rect.top, sz.cx, sz.cy);
6055 MoveWindow(infoPtr->hwndEdit, rect.left, rect.top, sz.cx, sz.cy, FALSE);
6056 ShowWindow(infoPtr->hwndEdit, SW_NORMAL);
6057 SetFocus(infoPtr->hwndEdit);
6058 SendMessageW(infoPtr->hwndEdit, EM_SETSEL, 0, -1);
6059 return infoPtr->hwndEdit;
6063 /***
6064 * DESCRIPTION:
6065 * Ensures the specified item is visible, scrolling into view if necessary.
6067 * PARAMETER(S):
6068 * [I] infoPtr : valid pointer to the listview structure
6069 * [I] nItem : item index
6070 * [I] bPartial : partially or entirely visible
6072 * RETURN:
6073 * SUCCESS : TRUE
6074 * FAILURE : FALSE
6076 static BOOL LISTVIEW_EnsureVisible(LISTVIEW_INFO *infoPtr, INT nItem, BOOL bPartial)
6078 INT nScrollPosHeight = 0;
6079 INT nScrollPosWidth = 0;
6080 INT nHorzAdjust = 0;
6081 INT nVertAdjust = 0;
6082 INT nHorzDiff = 0;
6083 INT nVertDiff = 0;
6084 RECT rcItem, rcTemp;
6086 rcItem.left = LVIR_BOUNDS;
6087 if (!LISTVIEW_GetItemRect(infoPtr, nItem, &rcItem)) return FALSE;
6089 if (bPartial && IntersectRect(&rcTemp, &infoPtr->rcList, &rcItem)) return TRUE;
6091 if (rcItem.left < infoPtr->rcList.left || rcItem.right > infoPtr->rcList.right)
6093 /* scroll left/right, but in LV_VIEW_DETAILS mode */
6094 if (infoPtr->uView == LV_VIEW_LIST)
6095 nScrollPosWidth = infoPtr->nItemWidth;
6096 else if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
6097 nScrollPosWidth = 1;
6099 if (rcItem.left < infoPtr->rcList.left)
6101 nHorzAdjust = -1;
6102 if (infoPtr->uView != LV_VIEW_DETAILS) nHorzDiff = rcItem.left - infoPtr->rcList.left;
6104 else
6106 nHorzAdjust = 1;
6107 if (infoPtr->uView != LV_VIEW_DETAILS) nHorzDiff = rcItem.right - infoPtr->rcList.right;
6111 if (rcItem.top < infoPtr->rcList.top || rcItem.bottom > infoPtr->rcList.bottom)
6113 /* scroll up/down, but not in LVS_LIST mode */
6114 if (infoPtr->uView == LV_VIEW_DETAILS)
6115 nScrollPosHeight = infoPtr->nItemHeight;
6116 else if ((infoPtr->uView == LV_VIEW_ICON) || (infoPtr->uView == LV_VIEW_SMALLICON))
6117 nScrollPosHeight = 1;
6119 if (rcItem.top < infoPtr->rcList.top)
6121 nVertAdjust = -1;
6122 if (infoPtr->uView != LV_VIEW_LIST) nVertDiff = rcItem.top - infoPtr->rcList.top;
6124 else
6126 nVertAdjust = 1;
6127 if (infoPtr->uView != LV_VIEW_LIST) nVertDiff = rcItem.bottom - infoPtr->rcList.bottom;
6131 if (!nScrollPosWidth && !nScrollPosHeight) return TRUE;
6133 if (nScrollPosWidth)
6135 INT diff = nHorzDiff / nScrollPosWidth;
6136 if (nHorzDiff % nScrollPosWidth) diff += nHorzAdjust;
6137 LISTVIEW_HScroll(infoPtr, SB_INTERNAL, diff);
6140 if (nScrollPosHeight)
6142 INT diff = nVertDiff / nScrollPosHeight;
6143 if (nVertDiff % nScrollPosHeight) diff += nVertAdjust;
6144 LISTVIEW_VScroll(infoPtr, SB_INTERNAL, diff);
6147 return TRUE;
6150 /***
6151 * DESCRIPTION:
6152 * Searches for an item with specific characteristics.
6154 * PARAMETER(S):
6155 * [I] hwnd : window handle
6156 * [I] nStart : base item index
6157 * [I] lpFindInfo : item information to look for
6159 * RETURN:
6160 * SUCCESS : index of item
6161 * FAILURE : -1
6163 static INT LISTVIEW_FindItemW(const LISTVIEW_INFO *infoPtr, INT nStart,
6164 const LVFINDINFOW *lpFindInfo)
6166 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
6167 BOOL bWrap = FALSE, bNearest = FALSE;
6168 INT nItem = nStart + 1, nLast = infoPtr->nItemCount, nNearestItem = -1;
6169 ULONG xdist, ydist, dist, mindist = 0x7fffffff;
6170 POINT Position, Destination;
6171 LVITEMW lvItem;
6173 /* Search in virtual listviews should be done by application, not by
6174 listview control, so we just send LVN_ODFINDITEMW and return the result */
6175 if (infoPtr->dwStyle & LVS_OWNERDATA)
6177 NMLVFINDITEMW nmlv;
6179 nmlv.iStart = nStart;
6180 nmlv.lvfi = *lpFindInfo;
6181 return notify_hdr(infoPtr, LVN_ODFINDITEMW, (LPNMHDR)&nmlv.hdr);
6184 if (!lpFindInfo || nItem < 0) return -1;
6186 lvItem.mask = 0;
6187 if (lpFindInfo->flags & (LVFI_STRING | LVFI_PARTIAL) ||
6188 lpFindInfo->flags & LVFI_SUBSTRING)
6190 lvItem.mask |= LVIF_TEXT;
6191 lvItem.pszText = szDispText;
6192 lvItem.cchTextMax = DISP_TEXT_SIZE;
6195 if (lpFindInfo->flags & LVFI_WRAP)
6196 bWrap = TRUE;
6198 if ((lpFindInfo->flags & LVFI_NEARESTXY) &&
6199 (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON))
6201 POINT Origin;
6202 RECT rcArea;
6204 LISTVIEW_GetOrigin(infoPtr, &Origin);
6205 Destination.x = lpFindInfo->pt.x - Origin.x;
6206 Destination.y = lpFindInfo->pt.y - Origin.y;
6207 switch(lpFindInfo->vkDirection)
6209 case VK_DOWN: Destination.y += infoPtr->nItemHeight; break;
6210 case VK_UP: Destination.y -= infoPtr->nItemHeight; break;
6211 case VK_RIGHT: Destination.x += infoPtr->nItemWidth; break;
6212 case VK_LEFT: Destination.x -= infoPtr->nItemWidth; break;
6213 case VK_HOME: Destination.x = Destination.y = 0; break;
6214 case VK_NEXT: Destination.y += infoPtr->rcList.bottom - infoPtr->rcList.top; break;
6215 case VK_PRIOR: Destination.y -= infoPtr->rcList.bottom - infoPtr->rcList.top; break;
6216 case VK_END:
6217 LISTVIEW_GetAreaRect(infoPtr, &rcArea);
6218 Destination.x = rcArea.right;
6219 Destination.y = rcArea.bottom;
6220 break;
6221 default: ERR("Unknown vkDirection=%d\n", lpFindInfo->vkDirection);
6223 bNearest = TRUE;
6225 else Destination.x = Destination.y = 0;
6227 /* if LVFI_PARAM is specified, all other flags are ignored */
6228 if (lpFindInfo->flags & LVFI_PARAM)
6230 lvItem.mask |= LVIF_PARAM;
6231 bNearest = FALSE;
6232 lvItem.mask &= ~LVIF_TEXT;
6235 again:
6236 for (; nItem < nLast; nItem++)
6238 lvItem.iItem = nItem;
6239 lvItem.iSubItem = 0;
6240 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) continue;
6242 if (lvItem.mask & LVIF_PARAM)
6244 if (lpFindInfo->lParam == lvItem.lParam)
6245 return nItem;
6246 else
6247 continue;
6250 if (lvItem.mask & LVIF_TEXT)
6252 if (lpFindInfo->flags & (LVFI_PARTIAL | LVFI_SUBSTRING))
6254 WCHAR *p = strstrW(lvItem.pszText, lpFindInfo->psz);
6255 if (!p || p != lvItem.pszText) continue;
6257 else
6259 if (lstrcmpW(lvItem.pszText, lpFindInfo->psz) != 0) continue;
6263 if (!bNearest) return nItem;
6265 /* This is very inefficient. To do a good job here,
6266 * we need a sorted array of (x,y) item positions */
6267 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
6269 /* compute the distance^2 to the destination */
6270 xdist = Destination.x - Position.x;
6271 ydist = Destination.y - Position.y;
6272 dist = xdist * xdist + ydist * ydist;
6274 /* remember the distance, and item if it's closer */
6275 if (dist < mindist)
6277 mindist = dist;
6278 nNearestItem = nItem;
6282 if (bWrap)
6284 nItem = 0;
6285 nLast = min(nStart + 1, infoPtr->nItemCount);
6286 bWrap = FALSE;
6287 goto again;
6290 return nNearestItem;
6293 /***
6294 * DESCRIPTION:
6295 * Searches for an item with specific characteristics.
6297 * PARAMETER(S):
6298 * [I] hwnd : window handle
6299 * [I] nStart : base item index
6300 * [I] lpFindInfo : item information to look for
6302 * RETURN:
6303 * SUCCESS : index of item
6304 * FAILURE : -1
6306 static INT LISTVIEW_FindItemA(const LISTVIEW_INFO *infoPtr, INT nStart,
6307 const LVFINDINFOA *lpFindInfo)
6309 BOOL hasText = lpFindInfo->flags & (LVFI_STRING | LVFI_PARTIAL) ||
6310 lpFindInfo->flags & LVFI_SUBSTRING;
6311 LVFINDINFOW fiw;
6312 INT res;
6313 LPWSTR strW = NULL;
6315 memcpy(&fiw, lpFindInfo, sizeof(fiw));
6316 if (hasText) fiw.psz = strW = textdupTtoW((LPCWSTR)lpFindInfo->psz, FALSE);
6317 res = LISTVIEW_FindItemW(infoPtr, nStart, &fiw);
6318 textfreeT(strW, FALSE);
6319 return res;
6322 /***
6323 * DESCRIPTION:
6324 * Retrieves column attributes.
6326 * PARAMETER(S):
6327 * [I] infoPtr : valid pointer to the listview structure
6328 * [I] nColumn : column index
6329 * [IO] lpColumn : column information
6330 * [I] isW : if TRUE, then lpColumn is a LPLVCOLUMNW
6331 * otherwise it is in fact a LPLVCOLUMNA
6333 * RETURN:
6334 * SUCCESS : TRUE
6335 * FAILURE : FALSE
6337 static BOOL LISTVIEW_GetColumnT(const LISTVIEW_INFO *infoPtr, INT nColumn, LPLVCOLUMNW lpColumn, BOOL isW)
6339 COLUMN_INFO *lpColumnInfo;
6340 HDITEMW hdi;
6342 if (!lpColumn || nColumn < 0 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
6343 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, nColumn);
6345 /* initialize memory */
6346 ZeroMemory(&hdi, sizeof(hdi));
6348 if (lpColumn->mask & LVCF_TEXT)
6350 hdi.mask |= HDI_TEXT;
6351 hdi.pszText = lpColumn->pszText;
6352 hdi.cchTextMax = lpColumn->cchTextMax;
6355 if (lpColumn->mask & LVCF_IMAGE)
6356 hdi.mask |= HDI_IMAGE;
6358 if (lpColumn->mask & LVCF_ORDER)
6359 hdi.mask |= HDI_ORDER;
6361 if (lpColumn->mask & LVCF_SUBITEM)
6362 hdi.mask |= HDI_LPARAM;
6364 if (!SendMessageW(infoPtr->hwndHeader, isW ? HDM_GETITEMW : HDM_GETITEMA, nColumn, (LPARAM)&hdi)) return FALSE;
6366 if (lpColumn->mask & LVCF_FMT)
6367 lpColumn->fmt = lpColumnInfo->fmt;
6369 if (lpColumn->mask & LVCF_WIDTH)
6370 lpColumn->cx = lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left;
6372 if (lpColumn->mask & LVCF_IMAGE)
6373 lpColumn->iImage = hdi.iImage;
6375 if (lpColumn->mask & LVCF_ORDER)
6376 lpColumn->iOrder = hdi.iOrder;
6378 if (lpColumn->mask & LVCF_SUBITEM)
6379 lpColumn->iSubItem = hdi.lParam;
6381 if (lpColumn->mask & LVCF_MINWIDTH)
6382 lpColumn->cxMin = lpColumnInfo->cxMin;
6384 return TRUE;
6388 static BOOL LISTVIEW_GetColumnOrderArray(const LISTVIEW_INFO *infoPtr, INT iCount, LPINT lpiArray)
6390 TRACE("iCount=%d, lpiArray=%p\n", iCount, lpiArray);
6392 if (!lpiArray)
6393 return FALSE;
6395 return SendMessageW(infoPtr->hwndHeader, HDM_GETORDERARRAY, iCount, (LPARAM)lpiArray);
6398 /***
6399 * DESCRIPTION:
6400 * Retrieves the column width.
6402 * PARAMETER(S):
6403 * [I] infoPtr : valid pointer to the listview structure
6404 * [I] int : column index
6406 * RETURN:
6407 * SUCCESS : column width
6408 * FAILURE : zero
6410 static INT LISTVIEW_GetColumnWidth(const LISTVIEW_INFO *infoPtr, INT nColumn)
6412 INT nColumnWidth = 0;
6413 HDITEMW hdItem;
6415 TRACE("nColumn=%d\n", nColumn);
6417 /* we have a 'column' in LIST and REPORT mode only */
6418 switch(infoPtr->uView)
6420 case LV_VIEW_LIST:
6421 nColumnWidth = infoPtr->nItemWidth;
6422 break;
6423 case LV_VIEW_DETAILS:
6424 /* We are not using LISTVIEW_GetHeaderRect as this data is updated only after a HDN_ITEMCHANGED.
6425 * There is an application that subclasses the listview, calls LVM_GETCOLUMNWIDTH in the
6426 * HDN_ITEMCHANGED handler and goes into infinite recursion if it receives old data.
6428 * TODO: should we do the same in LVM_GETCOLUMN?
6430 hdItem.mask = HDI_WIDTH;
6431 if (!SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nColumn, (LPARAM)&hdItem))
6433 WARN("(%p): HDM_GETITEMW failed for item %d\n", infoPtr->hwndSelf, nColumn);
6434 return 0;
6436 nColumnWidth = hdItem.cxy;
6437 break;
6440 TRACE("nColumnWidth=%d\n", nColumnWidth);
6441 return nColumnWidth;
6444 /***
6445 * DESCRIPTION:
6446 * In list or report display mode, retrieves the number of items that can fit
6447 * vertically in the visible area. In icon or small icon display mode,
6448 * retrieves the total number of visible items.
6450 * PARAMETER(S):
6451 * [I] infoPtr : valid pointer to the listview structure
6453 * RETURN:
6454 * Number of fully visible items.
6456 static INT LISTVIEW_GetCountPerPage(const LISTVIEW_INFO *infoPtr)
6458 switch (infoPtr->uView)
6460 case LV_VIEW_ICON:
6461 case LV_VIEW_SMALLICON:
6462 return infoPtr->nItemCount;
6463 case LV_VIEW_DETAILS:
6464 return LISTVIEW_GetCountPerColumn(infoPtr);
6465 case LV_VIEW_LIST:
6466 return LISTVIEW_GetCountPerRow(infoPtr) * LISTVIEW_GetCountPerColumn(infoPtr);
6468 assert(FALSE);
6469 return 0;
6472 /***
6473 * DESCRIPTION:
6474 * Retrieves an image list handle.
6476 * PARAMETER(S):
6477 * [I] infoPtr : valid pointer to the listview structure
6478 * [I] nImageList : image list identifier
6480 * RETURN:
6481 * SUCCESS : image list handle
6482 * FAILURE : NULL
6484 static HIMAGELIST LISTVIEW_GetImageList(const LISTVIEW_INFO *infoPtr, INT nImageList)
6486 switch (nImageList)
6488 case LVSIL_NORMAL: return infoPtr->himlNormal;
6489 case LVSIL_SMALL: return infoPtr->himlSmall;
6490 case LVSIL_STATE: return infoPtr->himlState;
6491 case LVSIL_GROUPHEADER:
6492 FIXME("LVSIL_GROUPHEADER not supported\n");
6493 break;
6494 default:
6495 WARN("got unknown imagelist index - %d\n", nImageList);
6497 return NULL;
6500 /* LISTVIEW_GetISearchString */
6502 /***
6503 * DESCRIPTION:
6504 * Retrieves item attributes.
6506 * PARAMETER(S):
6507 * [I] hwnd : window handle
6508 * [IO] lpLVItem : item info
6509 * [I] isW : if TRUE, then lpLVItem is a LPLVITEMW,
6510 * if FALSE, then lpLVItem is a LPLVITEMA.
6512 * NOTE:
6513 * This is the internal 'GetItem' interface -- it tries to
6514 * be smart and avoid text copies, if possible, by modifying
6515 * lpLVItem->pszText to point to the text string. Please note
6516 * that this is not always possible (e.g. OWNERDATA), so on
6517 * entry you *must* supply valid values for pszText, and cchTextMax.
6518 * The only difference to the documented interface is that upon
6519 * return, you should use *only* the lpLVItem->pszText, rather than
6520 * the buffer pointer you provided on input. Most code already does
6521 * that, so it's not a problem.
6522 * For the two cases when the text must be copied (that is,
6523 * for LVM_GETITEM, and LVM_GETITEMTEXT), use LISTVIEW_GetItemExtT.
6525 * RETURN:
6526 * SUCCESS : TRUE
6527 * FAILURE : FALSE
6529 static BOOL LISTVIEW_GetItemT(const LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem, BOOL isW)
6531 ITEMHDR callbackHdr = { LPSTR_TEXTCALLBACKW, I_IMAGECALLBACK };
6532 NMLVDISPINFOW dispInfo;
6533 ITEM_INFO *lpItem;
6534 ITEMHDR* pItemHdr;
6535 HDPA hdpaSubItems;
6536 INT isubitem;
6538 TRACE("(lpLVItem=%s, isW=%d)\n", debuglvitem_t(lpLVItem, isW), isW);
6540 if (!lpLVItem || lpLVItem->iItem < 0 || lpLVItem->iItem >= infoPtr->nItemCount)
6541 return FALSE;
6543 if (lpLVItem->mask == 0) return TRUE;
6544 TRACE("mask=%x\n", lpLVItem->mask);
6546 /* make a local copy */
6547 isubitem = lpLVItem->iSubItem;
6549 /* a quick optimization if all we're asked is the focus state
6550 * these queries are worth optimising since they are common,
6551 * and can be answered in constant time, without the heavy accesses */
6552 if ( (lpLVItem->mask == LVIF_STATE) && (lpLVItem->stateMask == LVIS_FOCUSED) &&
6553 !(infoPtr->uCallbackMask & LVIS_FOCUSED) )
6555 lpLVItem->state = 0;
6556 if (infoPtr->nFocusedItem == lpLVItem->iItem)
6557 lpLVItem->state |= LVIS_FOCUSED;
6558 return TRUE;
6561 ZeroMemory(&dispInfo, sizeof(dispInfo));
6563 /* if the app stores all the data, handle it separately */
6564 if (infoPtr->dwStyle & LVS_OWNERDATA)
6566 dispInfo.item.state = 0;
6568 /* apparently, we should not callback for lParam in LVS_OWNERDATA */
6569 if ((lpLVItem->mask & ~(LVIF_STATE | LVIF_PARAM)) ||
6570 ((lpLVItem->mask & LVIF_STATE) && (infoPtr->uCallbackMask & lpLVItem->stateMask)))
6572 UINT mask = lpLVItem->mask;
6574 /* NOTE: copy only fields which we _know_ are initialized, some apps
6575 * depend on the uninitialized fields being 0 */
6576 dispInfo.item.mask = lpLVItem->mask & ~LVIF_PARAM;
6577 dispInfo.item.iItem = lpLVItem->iItem;
6578 dispInfo.item.iSubItem = isubitem;
6579 if (lpLVItem->mask & LVIF_TEXT)
6581 if (lpLVItem->mask & LVIF_NORECOMPUTE)
6582 /* reset mask */
6583 dispInfo.item.mask &= ~(LVIF_TEXT | LVIF_NORECOMPUTE);
6584 else
6586 dispInfo.item.pszText = lpLVItem->pszText;
6587 dispInfo.item.cchTextMax = lpLVItem->cchTextMax;
6590 if (lpLVItem->mask & LVIF_STATE)
6591 dispInfo.item.stateMask = lpLVItem->stateMask & infoPtr->uCallbackMask;
6592 /* could be zeroed on LVIF_NORECOMPUTE case */
6593 if (dispInfo.item.mask)
6595 notify_dispinfoT(infoPtr, LVN_GETDISPINFOW, &dispInfo, isW);
6596 dispInfo.item.stateMask = lpLVItem->stateMask;
6597 if (lpLVItem->mask & (LVIF_GROUPID|LVIF_COLUMNS))
6599 /* full size structure expected - _WIN32IE >= 0x560 */
6600 *lpLVItem = dispInfo.item;
6602 else if (lpLVItem->mask & LVIF_INDENT)
6604 /* indent member expected - _WIN32IE >= 0x300 */
6605 memcpy(lpLVItem, &dispInfo.item, offsetof( LVITEMW, iGroupId ));
6607 else
6609 /* minimal structure expected */
6610 memcpy(lpLVItem, &dispInfo.item, offsetof( LVITEMW, iIndent ));
6612 lpLVItem->mask = mask;
6613 TRACE(" getdispinfo(1):lpLVItem=%s\n", debuglvitem_t(lpLVItem, isW));
6617 /* make sure lParam is zeroed out */
6618 if (lpLVItem->mask & LVIF_PARAM) lpLVItem->lParam = 0;
6620 /* callback marked pointer required here */
6621 if ((lpLVItem->mask & LVIF_TEXT) && (lpLVItem->mask & LVIF_NORECOMPUTE))
6622 lpLVItem->pszText = LPSTR_TEXTCALLBACKW;
6624 /* we store only a little state, so if we're not asked, we're done */
6625 if (!(lpLVItem->mask & LVIF_STATE) || isubitem) return TRUE;
6627 /* if focus is handled by us, report it */
6628 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_FOCUSED )
6630 lpLVItem->state &= ~LVIS_FOCUSED;
6631 if (infoPtr->nFocusedItem == lpLVItem->iItem)
6632 lpLVItem->state |= LVIS_FOCUSED;
6635 /* and do the same for selection, if we handle it */
6636 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_SELECTED )
6638 lpLVItem->state &= ~LVIS_SELECTED;
6639 if (ranges_contain(infoPtr->selectionRanges, lpLVItem->iItem))
6640 lpLVItem->state |= LVIS_SELECTED;
6643 return TRUE;
6646 /* find the item and subitem structures before we proceed */
6647 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, lpLVItem->iItem);
6648 lpItem = DPA_GetPtr(hdpaSubItems, 0);
6649 assert (lpItem);
6651 if (isubitem)
6653 SUBITEM_INFO *lpSubItem = LISTVIEW_GetSubItemPtr(hdpaSubItems, isubitem);
6654 pItemHdr = lpSubItem ? &lpSubItem->hdr : &callbackHdr;
6655 if (!lpSubItem)
6657 WARN(" iSubItem invalid (%08x), ignored.\n", isubitem);
6658 isubitem = 0;
6661 else
6662 pItemHdr = &lpItem->hdr;
6664 /* Do we need to query the state from the app? */
6665 if ((lpLVItem->mask & LVIF_STATE) && infoPtr->uCallbackMask && isubitem == 0)
6667 dispInfo.item.mask |= LVIF_STATE;
6668 dispInfo.item.stateMask = infoPtr->uCallbackMask;
6671 /* Do we need to enquire about the image? */
6672 if ((lpLVItem->mask & LVIF_IMAGE) && pItemHdr->iImage == I_IMAGECALLBACK &&
6673 (isubitem == 0 || (infoPtr->dwLvExStyle & LVS_EX_SUBITEMIMAGES)))
6675 dispInfo.item.mask |= LVIF_IMAGE;
6676 dispInfo.item.iImage = I_IMAGECALLBACK;
6679 /* Only items support indentation */
6680 if ((lpLVItem->mask & LVIF_INDENT) && lpItem->iIndent == I_INDENTCALLBACK &&
6681 (isubitem == 0))
6683 dispInfo.item.mask |= LVIF_INDENT;
6684 dispInfo.item.iIndent = I_INDENTCALLBACK;
6687 /* Apps depend on calling back for text if it is NULL or LPSTR_TEXTCALLBACKW */
6688 if ((lpLVItem->mask & LVIF_TEXT) && !(lpLVItem->mask & LVIF_NORECOMPUTE) &&
6689 !is_text(pItemHdr->pszText))
6691 dispInfo.item.mask |= LVIF_TEXT;
6692 dispInfo.item.pszText = lpLVItem->pszText;
6693 dispInfo.item.cchTextMax = lpLVItem->cchTextMax;
6694 if (dispInfo.item.pszText && dispInfo.item.cchTextMax > 0)
6695 *dispInfo.item.pszText = '\0';
6698 /* If we don't have all the requested info, query the application */
6699 if (dispInfo.item.mask)
6701 dispInfo.item.iItem = lpLVItem->iItem;
6702 dispInfo.item.iSubItem = lpLVItem->iSubItem; /* yes: the original subitem */
6703 dispInfo.item.lParam = lpItem->lParam;
6704 notify_dispinfoT(infoPtr, LVN_GETDISPINFOW, &dispInfo, isW);
6705 TRACE(" getdispinfo(2):item=%s\n", debuglvitem_t(&dispInfo.item, isW));
6708 /* we should not store values for subitems */
6709 if (isubitem) dispInfo.item.mask &= ~LVIF_DI_SETITEM;
6711 /* Now, handle the iImage field */
6712 if (dispInfo.item.mask & LVIF_IMAGE)
6714 lpLVItem->iImage = dispInfo.item.iImage;
6715 if ((dispInfo.item.mask & LVIF_DI_SETITEM) && pItemHdr->iImage == I_IMAGECALLBACK)
6716 pItemHdr->iImage = dispInfo.item.iImage;
6718 else if (lpLVItem->mask & LVIF_IMAGE)
6720 if(isubitem == 0 || (infoPtr->dwLvExStyle & LVS_EX_SUBITEMIMAGES))
6721 lpLVItem->iImage = pItemHdr->iImage;
6722 else
6723 lpLVItem->iImage = 0;
6726 /* The pszText field */
6727 if (dispInfo.item.mask & LVIF_TEXT)
6729 if ((dispInfo.item.mask & LVIF_DI_SETITEM) && pItemHdr->pszText)
6730 textsetptrT(&pItemHdr->pszText, dispInfo.item.pszText, isW);
6732 lpLVItem->pszText = dispInfo.item.pszText;
6734 else if (lpLVItem->mask & LVIF_TEXT)
6736 /* if LVN_GETDISPINFO's disabled with LVIF_NORECOMPUTE return callback placeholder */
6737 if (isW || !is_text(pItemHdr->pszText)) lpLVItem->pszText = pItemHdr->pszText;
6738 else textcpynT(lpLVItem->pszText, isW, pItemHdr->pszText, TRUE, lpLVItem->cchTextMax);
6741 /* Next is the lParam field */
6742 if (dispInfo.item.mask & LVIF_PARAM)
6744 lpLVItem->lParam = dispInfo.item.lParam;
6745 if ((dispInfo.item.mask & LVIF_DI_SETITEM))
6746 lpItem->lParam = dispInfo.item.lParam;
6748 else if (lpLVItem->mask & LVIF_PARAM)
6749 lpLVItem->lParam = lpItem->lParam;
6751 /* if this is a subitem, we're done */
6752 if (isubitem) return TRUE;
6754 /* ... the state field (this one is different due to uCallbackmask) */
6755 if (lpLVItem->mask & LVIF_STATE)
6757 lpLVItem->state = lpItem->state & lpLVItem->stateMask;
6758 if (dispInfo.item.mask & LVIF_STATE)
6760 lpLVItem->state &= ~dispInfo.item.stateMask;
6761 lpLVItem->state |= (dispInfo.item.state & dispInfo.item.stateMask);
6763 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_FOCUSED )
6765 lpLVItem->state &= ~LVIS_FOCUSED;
6766 if (infoPtr->nFocusedItem == lpLVItem->iItem)
6767 lpLVItem->state |= LVIS_FOCUSED;
6769 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_SELECTED )
6771 lpLVItem->state &= ~LVIS_SELECTED;
6772 if (ranges_contain(infoPtr->selectionRanges, lpLVItem->iItem))
6773 lpLVItem->state |= LVIS_SELECTED;
6777 /* and last, but not least, the indent field */
6778 if (dispInfo.item.mask & LVIF_INDENT)
6780 lpLVItem->iIndent = dispInfo.item.iIndent;
6781 if ((dispInfo.item.mask & LVIF_DI_SETITEM) && lpItem->iIndent == I_INDENTCALLBACK)
6782 lpItem->iIndent = dispInfo.item.iIndent;
6784 else if (lpLVItem->mask & LVIF_INDENT)
6786 lpLVItem->iIndent = lpItem->iIndent;
6789 return TRUE;
6792 /***
6793 * DESCRIPTION:
6794 * Retrieves item attributes.
6796 * PARAMETER(S):
6797 * [I] hwnd : window handle
6798 * [IO] lpLVItem : item info
6799 * [I] isW : if TRUE, then lpLVItem is a LPLVITEMW,
6800 * if FALSE, then lpLVItem is a LPLVITEMA.
6802 * NOTE:
6803 * This is the external 'GetItem' interface -- it properly copies
6804 * the text in the provided buffer.
6806 * RETURN:
6807 * SUCCESS : TRUE
6808 * FAILURE : FALSE
6810 static BOOL LISTVIEW_GetItemExtT(const LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem, BOOL isW)
6812 LPWSTR pszText;
6813 BOOL bResult;
6815 if (!lpLVItem || lpLVItem->iItem < 0 || lpLVItem->iItem >= infoPtr->nItemCount)
6816 return FALSE;
6818 pszText = lpLVItem->pszText;
6819 bResult = LISTVIEW_GetItemT(infoPtr, lpLVItem, isW);
6820 if (bResult && (lpLVItem->mask & LVIF_TEXT) && lpLVItem->pszText != pszText)
6822 if (lpLVItem->pszText != LPSTR_TEXTCALLBACKW)
6823 textcpynT(pszText, isW, lpLVItem->pszText, isW, lpLVItem->cchTextMax);
6824 else
6825 pszText = LPSTR_TEXTCALLBACKW;
6827 lpLVItem->pszText = pszText;
6829 return bResult;
6833 /***
6834 * DESCRIPTION:
6835 * Retrieves the position (upper-left) of the listview control item.
6836 * Note that for LVS_ICON style, the upper-left is that of the icon
6837 * and not the bounding box.
6839 * PARAMETER(S):
6840 * [I] infoPtr : valid pointer to the listview structure
6841 * [I] nItem : item index
6842 * [O] lpptPosition : coordinate information
6844 * RETURN:
6845 * SUCCESS : TRUE
6846 * FAILURE : FALSE
6848 static BOOL LISTVIEW_GetItemPosition(const LISTVIEW_INFO *infoPtr, INT nItem, LPPOINT lpptPosition)
6850 POINT Origin;
6852 TRACE("(nItem=%d, lpptPosition=%p)\n", nItem, lpptPosition);
6854 if (!lpptPosition || nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
6856 LISTVIEW_GetOrigin(infoPtr, &Origin);
6857 LISTVIEW_GetItemOrigin(infoPtr, nItem, lpptPosition);
6859 if (infoPtr->uView == LV_VIEW_ICON)
6861 lpptPosition->x += (infoPtr->nItemWidth - infoPtr->iconSize.cx) / 2;
6862 lpptPosition->y += ICON_TOP_PADDING;
6864 lpptPosition->x += Origin.x;
6865 lpptPosition->y += Origin.y;
6867 TRACE (" lpptPosition=%s\n", wine_dbgstr_point(lpptPosition));
6868 return TRUE;
6872 /***
6873 * DESCRIPTION:
6874 * Retrieves the bounding rectangle for a listview control item.
6876 * PARAMETER(S):
6877 * [I] infoPtr : valid pointer to the listview structure
6878 * [I] nItem : item index
6879 * [IO] lprc : bounding rectangle coordinates
6880 * lprc->left specifies the portion of the item for which the bounding
6881 * rectangle will be retrieved.
6883 * LVIR_BOUNDS Returns the bounding rectangle of the entire item,
6884 * including the icon and label.
6886 * * For LVS_ICON
6887 * * Experiment shows that native control returns:
6888 * * width = min (48, length of text line)
6889 * * .left = position.x - (width - iconsize.cx)/2
6890 * * .right = .left + width
6891 * * height = #lines of text * ntmHeight + icon height + 8
6892 * * .top = position.y - 2
6893 * * .bottom = .top + height
6894 * * separation between items .y = itemSpacing.cy - height
6895 * * .x = itemSpacing.cx - width
6896 * LVIR_ICON Returns the bounding rectangle of the icon or small icon.
6898 * * For LVS_ICON
6899 * * Experiment shows that native control returns:
6900 * * width = iconSize.cx + 16
6901 * * .left = position.x - (width - iconsize.cx)/2
6902 * * .right = .left + width
6903 * * height = iconSize.cy + 4
6904 * * .top = position.y - 2
6905 * * .bottom = .top + height
6906 * * separation between items .y = itemSpacing.cy - height
6907 * * .x = itemSpacing.cx - width
6908 * LVIR_LABEL Returns the bounding rectangle of the item text.
6910 * * For LVS_ICON
6911 * * Experiment shows that native control returns:
6912 * * width = text length
6913 * * .left = position.x - width/2
6914 * * .right = .left + width
6915 * * height = ntmH * linecount + 2
6916 * * .top = position.y + iconSize.cy + 6
6917 * * .bottom = .top + height
6918 * * separation between items .y = itemSpacing.cy - height
6919 * * .x = itemSpacing.cx - width
6920 * LVIR_SELECTBOUNDS Returns the union of the LVIR_ICON and LVIR_LABEL
6921 * rectangles, but excludes columns in report view.
6923 * RETURN:
6924 * SUCCESS : TRUE
6925 * FAILURE : FALSE
6927 * NOTES
6928 * Note that the bounding rectangle of the label in the LVS_ICON view depends
6929 * upon whether the window has the focus currently and on whether the item
6930 * is the one with the focus. Ensure that the control's record of which
6931 * item has the focus agrees with the items' records.
6933 static BOOL LISTVIEW_GetItemRect(const LISTVIEW_INFO *infoPtr, INT nItem, LPRECT lprc)
6935 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
6936 BOOL doLabel = TRUE, oversizedBox = FALSE;
6937 POINT Position, Origin;
6938 LVITEMW lvItem;
6939 LONG mode;
6941 TRACE("(hwnd=%p, nItem=%d, lprc=%p)\n", infoPtr->hwndSelf, nItem, lprc);
6943 if (!lprc || nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
6945 LISTVIEW_GetOrigin(infoPtr, &Origin);
6946 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
6948 /* Be smart and try to figure out the minimum we have to do */
6949 if (lprc->left == LVIR_ICON) doLabel = FALSE;
6950 if (infoPtr->uView == LV_VIEW_DETAILS && lprc->left == LVIR_BOUNDS) doLabel = FALSE;
6951 if (infoPtr->uView == LV_VIEW_ICON && lprc->left != LVIR_ICON &&
6952 infoPtr->bFocus && LISTVIEW_GetItemState(infoPtr, nItem, LVIS_FOCUSED))
6953 oversizedBox = TRUE;
6955 /* get what we need from the item before hand, so we make
6956 * only one request. This can speed up things, if data
6957 * is stored on the app side */
6958 lvItem.mask = 0;
6959 if (infoPtr->uView == LV_VIEW_DETAILS) lvItem.mask |= LVIF_INDENT;
6960 if (doLabel) lvItem.mask |= LVIF_TEXT;
6961 lvItem.iItem = nItem;
6962 lvItem.iSubItem = 0;
6963 lvItem.pszText = szDispText;
6964 lvItem.cchTextMax = DISP_TEXT_SIZE;
6965 if (lvItem.mask && !LISTVIEW_GetItemW(infoPtr, &lvItem)) return FALSE;
6966 /* we got the state already up, simulate it here, to avoid a reget */
6967 if (infoPtr->uView == LV_VIEW_ICON && (lprc->left != LVIR_ICON))
6969 lvItem.mask |= LVIF_STATE;
6970 lvItem.stateMask = LVIS_FOCUSED;
6971 lvItem.state = (oversizedBox ? LVIS_FOCUSED : 0);
6974 if (infoPtr->uView == LV_VIEW_DETAILS && (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) && lprc->left == LVIR_SELECTBOUNDS)
6975 lprc->left = LVIR_BOUNDS;
6977 mode = lprc->left;
6978 switch(lprc->left)
6980 case LVIR_ICON:
6981 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, NULL, lprc, NULL, NULL);
6982 break;
6984 case LVIR_LABEL:
6985 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, NULL, NULL, NULL, lprc);
6986 break;
6988 case LVIR_BOUNDS:
6989 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, lprc, NULL, NULL, NULL, NULL);
6990 break;
6992 case LVIR_SELECTBOUNDS:
6993 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, lprc, NULL, NULL, NULL);
6994 break;
6996 default:
6997 WARN("Unknown value: %d\n", lprc->left);
6998 return FALSE;
7001 if (infoPtr->uView == LV_VIEW_DETAILS)
7003 if (mode != LVIR_BOUNDS)
7004 OffsetRect(lprc, Origin.x + LISTVIEW_GetColumnInfo(infoPtr, 0)->rcHeader.left,
7005 Position.y + Origin.y);
7006 else
7007 OffsetRect(lprc, Origin.x, Position.y + Origin.y);
7009 else
7010 OffsetRect(lprc, Position.x + Origin.x, Position.y + Origin.y);
7012 TRACE(" rect=%s\n", wine_dbgstr_rect(lprc));
7014 return TRUE;
7017 /***
7018 * DESCRIPTION:
7019 * Retrieves the spacing between listview control items.
7021 * PARAMETER(S):
7022 * [I] infoPtr : valid pointer to the listview structure
7023 * [IO] lprc : rectangle to receive the output
7024 * on input, lprc->top = nSubItem
7025 * lprc->left = LVIR_ICON | LVIR_BOUNDS | LVIR_LABEL
7027 * NOTE: for subItem = 0, we should return the bounds of the _entire_ item,
7028 * not only those of the first column.
7029 * Fortunately, LISTVIEW_GetItemMetrics does the right thing.
7031 * RETURN:
7032 * TRUE: success
7033 * FALSE: failure
7035 static BOOL LISTVIEW_GetSubItemRect(const LISTVIEW_INFO *infoPtr, INT nItem, LPRECT lprc)
7037 POINT Position, Origin;
7038 LVITEMW lvItem;
7039 INT nColumn;
7041 if (!lprc) return FALSE;
7043 nColumn = lprc->top;
7045 TRACE("(nItem=%d, nSubItem=%d, type=%d)\n", nItem, lprc->top, lprc->left);
7046 /* On WinNT, a subitem of '0' calls LISTVIEW_GetItemRect */
7047 if (lprc->top == 0)
7048 return LISTVIEW_GetItemRect(infoPtr, nItem, lprc);
7050 if (infoPtr->uView != LV_VIEW_DETAILS) return FALSE;
7052 /* special case for header items */
7053 if (nItem == -1)
7055 if (lprc->left != LVIR_BOUNDS)
7057 FIXME("Only LVIR_BOUNDS is implemented for header, got %d\n", lprc->left);
7058 return FALSE;
7061 if (infoPtr->hwndHeader)
7062 return SendMessageW(infoPtr->hwndHeader, HDM_GETITEMRECT, lprc->top, (LPARAM)lprc);
7063 else
7065 memset(lprc, 0, sizeof(RECT));
7066 return TRUE;
7070 if (!LISTVIEW_GetItemPosition(infoPtr, nItem, &Position)) return FALSE;
7071 LISTVIEW_GetOrigin(infoPtr, &Origin);
7073 if (nColumn < 0 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
7075 lvItem.mask = 0;
7076 lvItem.iItem = nItem;
7077 lvItem.iSubItem = nColumn;
7079 switch(lprc->left)
7081 case LVIR_ICON:
7082 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, NULL, lprc, NULL, NULL);
7083 break;
7085 case LVIR_LABEL:
7086 case LVIR_BOUNDS:
7087 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, lprc, NULL, NULL, NULL, NULL);
7088 break;
7090 default:
7091 ERR("Unknown bounds=%d\n", lprc->left);
7092 return FALSE;
7095 OffsetRect(lprc, Origin.x, Position.y);
7096 TRACE("return rect %s\n", wine_dbgstr_rect(lprc));
7098 return TRUE;
7101 /***
7102 * DESCRIPTION:
7103 * Retrieves the spacing between listview control items.
7105 * PARAMETER(S):
7106 * [I] infoPtr : valid pointer to the listview structure
7107 * [I] bSmall : flag for small or large icon
7109 * RETURN:
7110 * Horizontal + vertical spacing
7112 static LONG LISTVIEW_GetItemSpacing(const LISTVIEW_INFO *infoPtr, BOOL bSmall)
7114 LONG lResult;
7116 if (!bSmall)
7118 lResult = MAKELONG(infoPtr->iconSpacing.cx, infoPtr->iconSpacing.cy);
7120 else
7122 if (infoPtr->uView == LV_VIEW_ICON)
7123 lResult = MAKELONG(DEFAULT_COLUMN_WIDTH, GetSystemMetrics(SM_CXSMICON)+HEIGHT_PADDING);
7124 else
7125 lResult = MAKELONG(infoPtr->nItemWidth, infoPtr->nItemHeight);
7127 return lResult;
7130 /***
7131 * DESCRIPTION:
7132 * Retrieves the state of a listview control item.
7134 * PARAMETER(S):
7135 * [I] infoPtr : valid pointer to the listview structure
7136 * [I] nItem : item index
7137 * [I] uMask : state mask
7139 * RETURN:
7140 * State specified by the mask.
7142 static UINT LISTVIEW_GetItemState(const LISTVIEW_INFO *infoPtr, INT nItem, UINT uMask)
7144 LVITEMW lvItem;
7146 if (nItem < 0 || nItem >= infoPtr->nItemCount) return 0;
7148 lvItem.iItem = nItem;
7149 lvItem.iSubItem = 0;
7150 lvItem.mask = LVIF_STATE;
7151 lvItem.stateMask = uMask;
7152 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return 0;
7154 return lvItem.state & uMask;
7157 /***
7158 * DESCRIPTION:
7159 * Retrieves the text of a listview control item or subitem.
7161 * PARAMETER(S):
7162 * [I] hwnd : window handle
7163 * [I] nItem : item index
7164 * [IO] lpLVItem : item information
7165 * [I] isW : TRUE if lpLVItem is Unicode
7167 * RETURN:
7168 * SUCCESS : string length
7169 * FAILURE : 0
7171 static INT LISTVIEW_GetItemTextT(const LISTVIEW_INFO *infoPtr, INT nItem, LPLVITEMW lpLVItem, BOOL isW)
7173 if (!lpLVItem || nItem < 0 || nItem >= infoPtr->nItemCount) return 0;
7175 lpLVItem->mask = LVIF_TEXT;
7176 lpLVItem->iItem = nItem;
7177 if (!LISTVIEW_GetItemExtT(infoPtr, lpLVItem, isW)) return 0;
7179 return textlenT(lpLVItem->pszText, isW);
7182 /***
7183 * DESCRIPTION:
7184 * Searches for an item based on properties + relationships.
7186 * PARAMETER(S):
7187 * [I] infoPtr : valid pointer to the listview structure
7188 * [I] nItem : item index
7189 * [I] uFlags : relationship flag
7191 * RETURN:
7192 * SUCCESS : item index
7193 * FAILURE : -1
7195 static INT LISTVIEW_GetNextItem(const LISTVIEW_INFO *infoPtr, INT nItem, UINT uFlags)
7197 UINT uMask = 0;
7198 LVFINDINFOW lvFindInfo;
7199 INT nCountPerColumn;
7200 INT nCountPerRow;
7201 INT i;
7203 TRACE("nItem=%d, uFlags=%x, nItemCount=%d\n", nItem, uFlags, infoPtr->nItemCount);
7204 if (nItem < -1 || nItem >= infoPtr->nItemCount) return -1;
7206 ZeroMemory(&lvFindInfo, sizeof(lvFindInfo));
7208 if (uFlags & LVNI_CUT)
7209 uMask |= LVIS_CUT;
7211 if (uFlags & LVNI_DROPHILITED)
7212 uMask |= LVIS_DROPHILITED;
7214 if (uFlags & LVNI_FOCUSED)
7215 uMask |= LVIS_FOCUSED;
7217 if (uFlags & LVNI_SELECTED)
7218 uMask |= LVIS_SELECTED;
7220 /* if we're asked for the focused item, that's only one,
7221 * so it's worth optimizing */
7222 if (uFlags & LVNI_FOCUSED)
7224 if ((LISTVIEW_GetItemState(infoPtr, infoPtr->nFocusedItem, uMask) & uMask) != uMask) return -1;
7225 return (infoPtr->nFocusedItem == nItem) ? -1 : infoPtr->nFocusedItem;
7228 if (uFlags & LVNI_ABOVE)
7230 if ((infoPtr->uView == LV_VIEW_LIST) || (infoPtr->uView == LV_VIEW_DETAILS))
7232 while (nItem >= 0)
7234 nItem--;
7235 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7236 return nItem;
7239 else
7241 /* Special case for autoarrange - move 'til the top of a list */
7242 if (is_autoarrange(infoPtr))
7244 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
7245 while (nItem - nCountPerRow >= 0)
7247 nItem -= nCountPerRow;
7248 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7249 return nItem;
7251 return -1;
7253 lvFindInfo.flags = LVFI_NEARESTXY;
7254 lvFindInfo.vkDirection = VK_UP;
7255 LISTVIEW_GetItemPosition(infoPtr, nItem, &lvFindInfo.pt);
7256 while ((nItem = LISTVIEW_FindItemW(infoPtr, nItem, &lvFindInfo)) != -1)
7258 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7259 return nItem;
7263 else if (uFlags & LVNI_BELOW)
7265 if ((infoPtr->uView == LV_VIEW_LIST) || (infoPtr->uView == LV_VIEW_DETAILS))
7267 while (nItem < infoPtr->nItemCount)
7269 nItem++;
7270 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7271 return nItem;
7274 else
7276 /* Special case for autoarrange - move 'til the bottom of a list */
7277 if (is_autoarrange(infoPtr))
7279 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
7280 while (nItem + nCountPerRow < infoPtr->nItemCount )
7282 nItem += nCountPerRow;
7283 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7284 return nItem;
7286 return -1;
7288 lvFindInfo.flags = LVFI_NEARESTXY;
7289 lvFindInfo.vkDirection = VK_DOWN;
7290 LISTVIEW_GetItemPosition(infoPtr, nItem, &lvFindInfo.pt);
7291 while ((nItem = LISTVIEW_FindItemW(infoPtr, nItem, &lvFindInfo)) != -1)
7293 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7294 return nItem;
7298 else if (uFlags & LVNI_TOLEFT)
7300 if (infoPtr->uView == LV_VIEW_LIST)
7302 nCountPerColumn = LISTVIEW_GetCountPerColumn(infoPtr);
7303 while (nItem - nCountPerColumn >= 0)
7305 nItem -= nCountPerColumn;
7306 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7307 return nItem;
7310 else if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
7312 /* Special case for autoarrange - move 'til the beginning of a row */
7313 if (is_autoarrange(infoPtr))
7315 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
7316 while (nItem % nCountPerRow > 0)
7318 nItem --;
7319 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7320 return nItem;
7322 return -1;
7324 lvFindInfo.flags = LVFI_NEARESTXY;
7325 lvFindInfo.vkDirection = VK_LEFT;
7326 LISTVIEW_GetItemPosition(infoPtr, nItem, &lvFindInfo.pt);
7327 while ((nItem = LISTVIEW_FindItemW(infoPtr, nItem, &lvFindInfo)) != -1)
7329 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7330 return nItem;
7334 else if (uFlags & LVNI_TORIGHT)
7336 if (infoPtr->uView == LV_VIEW_LIST)
7338 nCountPerColumn = LISTVIEW_GetCountPerColumn(infoPtr);
7339 while (nItem + nCountPerColumn < infoPtr->nItemCount)
7341 nItem += nCountPerColumn;
7342 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7343 return nItem;
7346 else if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
7348 /* Special case for autoarrange - move 'til the end of a row */
7349 if (is_autoarrange(infoPtr))
7351 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
7352 while (nItem % nCountPerRow < nCountPerRow - 1 )
7354 nItem ++;
7355 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7356 return nItem;
7358 return -1;
7360 lvFindInfo.flags = LVFI_NEARESTXY;
7361 lvFindInfo.vkDirection = VK_RIGHT;
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
7372 nItem++;
7374 /* search by index */
7375 for (i = nItem; i < infoPtr->nItemCount; i++)
7377 if ((LISTVIEW_GetItemState(infoPtr, i, uMask) & uMask) == uMask)
7378 return i;
7382 return -1;
7385 /* LISTVIEW_GetNumberOfWorkAreas */
7387 /***
7388 * DESCRIPTION:
7389 * Retrieves the origin coordinates when in icon or small icon display mode.
7391 * PARAMETER(S):
7392 * [I] infoPtr : valid pointer to the listview structure
7393 * [O] lpptOrigin : coordinate information
7395 * RETURN:
7396 * None.
7398 static void LISTVIEW_GetOrigin(const LISTVIEW_INFO *infoPtr, LPPOINT lpptOrigin)
7400 INT nHorzPos = 0, nVertPos = 0;
7401 SCROLLINFO scrollInfo;
7403 scrollInfo.cbSize = sizeof(SCROLLINFO);
7404 scrollInfo.fMask = SIF_POS;
7406 if (GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo))
7407 nHorzPos = scrollInfo.nPos;
7408 if (GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
7409 nVertPos = scrollInfo.nPos;
7411 TRACE("nHorzPos=%d, nVertPos=%d\n", nHorzPos, nVertPos);
7413 lpptOrigin->x = infoPtr->rcList.left;
7414 lpptOrigin->y = infoPtr->rcList.top;
7415 if (infoPtr->uView == LV_VIEW_LIST)
7416 nHorzPos *= infoPtr->nItemWidth;
7417 else if (infoPtr->uView == LV_VIEW_DETAILS)
7418 nVertPos *= infoPtr->nItemHeight;
7420 lpptOrigin->x -= nHorzPos;
7421 lpptOrigin->y -= nVertPos;
7423 TRACE(" origin=%s\n", wine_dbgstr_point(lpptOrigin));
7426 /***
7427 * DESCRIPTION:
7428 * Retrieves the width of a string.
7430 * PARAMETER(S):
7431 * [I] hwnd : window handle
7432 * [I] lpszText : text string to process
7433 * [I] isW : TRUE if lpszText is Unicode, FALSE otherwise
7435 * RETURN:
7436 * SUCCESS : string width (in pixels)
7437 * FAILURE : zero
7439 static INT LISTVIEW_GetStringWidthT(const LISTVIEW_INFO *infoPtr, LPCWSTR lpszText, BOOL isW)
7441 SIZE stringSize;
7443 stringSize.cx = 0;
7444 if (is_text(lpszText))
7446 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
7447 HDC hdc = GetDC(infoPtr->hwndSelf);
7448 HFONT hOldFont = SelectObject(hdc, hFont);
7450 if (isW)
7451 GetTextExtentPointW(hdc, lpszText, lstrlenW(lpszText), &stringSize);
7452 else
7453 GetTextExtentPointA(hdc, (LPCSTR)lpszText, lstrlenA((LPCSTR)lpszText), &stringSize);
7454 SelectObject(hdc, hOldFont);
7455 ReleaseDC(infoPtr->hwndSelf, hdc);
7457 return stringSize.cx;
7460 /***
7461 * DESCRIPTION:
7462 * Determines which listview item is located at the specified position.
7464 * PARAMETER(S):
7465 * [I] infoPtr : valid pointer to the listview structure
7466 * [IO] lpht : hit test information
7467 * [I] subitem : fill out iSubItem.
7468 * [I] select : return the index only if the hit selects the item
7470 * NOTE:
7471 * (mm 20001022): We must not allow iSubItem to be touched, for
7472 * an app might pass only a structure with space up to iItem!
7473 * (MS Office 97 does that for instance in the file open dialog)
7475 * RETURN:
7476 * SUCCESS : item index
7477 * FAILURE : -1
7479 static INT LISTVIEW_HitTest(const LISTVIEW_INFO *infoPtr, LPLVHITTESTINFO lpht, BOOL subitem, BOOL select)
7481 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
7482 RECT rcBox, rcBounds, rcState, rcIcon, rcLabel, rcSearch;
7483 POINT Origin, Position, opt;
7484 LVITEMW lvItem;
7485 ITERATOR i;
7486 INT iItem;
7488 TRACE("(pt=%s, subitem=%d, select=%d)\n", wine_dbgstr_point(&lpht->pt), subitem, select);
7490 lpht->flags = 0;
7491 lpht->iItem = -1;
7492 if (subitem) lpht->iSubItem = 0;
7494 LISTVIEW_GetOrigin(infoPtr, &Origin);
7496 /* set whole list relation flags */
7497 if (subitem && infoPtr->uView == LV_VIEW_DETAILS)
7499 /* LVM_SUBITEMHITTEST checks left bound of possible client area */
7500 if (infoPtr->rcList.left > lpht->pt.x && Origin.x < lpht->pt.x)
7501 lpht->flags |= LVHT_TOLEFT;
7503 if (lpht->pt.y < infoPtr->rcList.top && lpht->pt.y >= 0)
7504 opt.y = lpht->pt.y + infoPtr->rcList.top;
7505 else
7506 opt.y = lpht->pt.y;
7508 if (infoPtr->rcList.bottom < opt.y)
7509 lpht->flags |= LVHT_BELOW;
7511 else
7513 if (infoPtr->rcList.left > lpht->pt.x)
7514 lpht->flags |= LVHT_TOLEFT;
7515 else if (infoPtr->rcList.right < lpht->pt.x)
7516 lpht->flags |= LVHT_TORIGHT;
7518 if (infoPtr->rcList.top > lpht->pt.y)
7519 lpht->flags |= LVHT_ABOVE;
7520 else if (infoPtr->rcList.bottom < lpht->pt.y)
7521 lpht->flags |= LVHT_BELOW;
7524 /* even if item is invalid try to find subitem */
7525 if (infoPtr->uView == LV_VIEW_DETAILS && subitem)
7527 RECT *pRect;
7528 INT j;
7530 opt.x = lpht->pt.x - Origin.x;
7532 lpht->iSubItem = -1;
7533 for (j = 0; j < DPA_GetPtrCount(infoPtr->hdpaColumns); j++)
7535 pRect = &LISTVIEW_GetColumnInfo(infoPtr, j)->rcHeader;
7537 if ((opt.x >= pRect->left) && (opt.x < pRect->right))
7539 lpht->iSubItem = j;
7540 break;
7543 TRACE("lpht->iSubItem=%d\n", lpht->iSubItem);
7545 /* if we're outside horizontal columns bounds there's nothing to test further */
7546 if (lpht->iSubItem == -1)
7548 lpht->iItem = -1;
7549 lpht->flags = LVHT_NOWHERE;
7550 return -1;
7554 TRACE("lpht->flags=0x%x\n", lpht->flags);
7555 if (lpht->flags) return -1;
7557 lpht->flags |= LVHT_NOWHERE;
7559 /* first deal with the large items */
7560 rcSearch.left = lpht->pt.x;
7561 rcSearch.top = lpht->pt.y;
7562 rcSearch.right = rcSearch.left + 1;
7563 rcSearch.bottom = rcSearch.top + 1;
7565 iterator_frameditems(&i, infoPtr, &rcSearch);
7566 iterator_next(&i); /* go to first item in the sequence */
7567 iItem = i.nItem;
7568 iterator_destroy(&i);
7570 TRACE("lpht->iItem=%d\n", iItem);
7571 if (iItem == -1) return -1;
7573 lvItem.mask = LVIF_STATE | LVIF_TEXT;
7574 if (infoPtr->uView == LV_VIEW_DETAILS) lvItem.mask |= LVIF_INDENT;
7575 lvItem.stateMask = LVIS_STATEIMAGEMASK;
7576 if (infoPtr->uView == LV_VIEW_ICON) lvItem.stateMask |= LVIS_FOCUSED;
7577 lvItem.iItem = iItem;
7578 lvItem.iSubItem = subitem ? lpht->iSubItem : 0;
7579 lvItem.pszText = szDispText;
7580 lvItem.cchTextMax = DISP_TEXT_SIZE;
7581 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return -1;
7582 if (!infoPtr->bFocus) lvItem.state &= ~LVIS_FOCUSED;
7584 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, &rcBox, NULL, &rcIcon, &rcState, &rcLabel);
7585 LISTVIEW_GetItemOrigin(infoPtr, iItem, &Position);
7586 opt.x = lpht->pt.x - Position.x - Origin.x;
7588 if (lpht->pt.y < infoPtr->rcList.top && lpht->pt.y >= 0)
7589 opt.y = lpht->pt.y - Position.y - Origin.y + infoPtr->rcList.top;
7590 else
7591 opt.y = lpht->pt.y - Position.y - Origin.y;
7593 if (infoPtr->uView == LV_VIEW_DETAILS)
7595 rcBounds = rcBox;
7596 if (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT)
7597 opt.x = lpht->pt.x - Origin.x;
7599 else
7601 UnionRect(&rcBounds, &rcIcon, &rcLabel);
7602 UnionRect(&rcBounds, &rcBounds, &rcState);
7604 TRACE("rcBounds=%s\n", wine_dbgstr_rect(&rcBounds));
7605 if (!PtInRect(&rcBounds, opt)) return -1;
7607 if (PtInRect(&rcIcon, opt))
7608 lpht->flags |= LVHT_ONITEMICON;
7609 else if (PtInRect(&rcLabel, opt))
7610 lpht->flags |= LVHT_ONITEMLABEL;
7611 else if (infoPtr->himlState && PtInRect(&rcState, opt))
7612 lpht->flags |= LVHT_ONITEMSTATEICON;
7613 /* special case for LVS_EX_FULLROWSELECT */
7614 if (infoPtr->uView == LV_VIEW_DETAILS && infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT &&
7615 !(lpht->flags & LVHT_ONITEM))
7617 lpht->flags = LVHT_ONITEM | LVHT_ABOVE;
7619 if (lpht->flags & LVHT_ONITEM)
7620 lpht->flags &= ~LVHT_NOWHERE;
7621 TRACE("lpht->flags=0x%x\n", lpht->flags);
7623 if (select && !(infoPtr->uView == LV_VIEW_DETAILS &&
7624 ((infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) ||
7625 (infoPtr->dwStyle & LVS_OWNERDRAWFIXED))))
7627 if (infoPtr->uView == LV_VIEW_DETAILS)
7629 /* get main item bounds */
7630 lvItem.iSubItem = 0;
7631 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, &rcBox, NULL, &rcIcon, &rcState, &rcLabel);
7632 UnionRect(&rcBounds, &rcIcon, &rcLabel);
7633 UnionRect(&rcBounds, &rcBounds, &rcState);
7635 if (!PtInRect(&rcBounds, opt)) iItem = -1;
7637 return lpht->iItem = iItem;
7640 /***
7641 * DESCRIPTION:
7642 * Inserts a new item in the listview control.
7644 * PARAMETER(S):
7645 * [I] infoPtr : valid pointer to the listview structure
7646 * [I] lpLVItem : item information
7647 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
7649 * RETURN:
7650 * SUCCESS : new item index
7651 * FAILURE : -1
7653 static INT LISTVIEW_InsertItemT(LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem, BOOL isW)
7655 INT nItem;
7656 HDPA hdpaSubItems;
7657 NMLISTVIEW nmlv;
7658 ITEM_INFO *lpItem;
7659 ITEM_ID *lpID;
7660 BOOL is_sorted, has_changed;
7661 LVITEMW item;
7662 HWND hwndSelf = infoPtr->hwndSelf;
7664 TRACE("(lpLVItem=%s, isW=%d)\n", debuglvitem_t(lpLVItem, isW), isW);
7666 if (infoPtr->dwStyle & LVS_OWNERDATA) return infoPtr->nItemCount++;
7668 /* make sure it's an item, and not a subitem; cannot insert a subitem */
7669 if (!lpLVItem || lpLVItem->iSubItem) return -1;
7671 if (!is_assignable_item(lpLVItem, infoPtr->dwStyle)) return -1;
7673 if (!(lpItem = Alloc(sizeof(ITEM_INFO)))) return -1;
7675 /* insert item in listview control data structure */
7676 if ( !(hdpaSubItems = DPA_Create(8)) ) goto fail;
7677 if ( !DPA_SetPtr(hdpaSubItems, 0, lpItem) ) assert (FALSE);
7679 /* link with id struct */
7680 if (!(lpID = Alloc(sizeof(ITEM_ID)))) goto fail;
7681 lpItem->id = lpID;
7682 lpID->item = hdpaSubItems;
7683 lpID->id = get_next_itemid(infoPtr);
7684 if ( DPA_InsertPtr(infoPtr->hdpaItemIds, infoPtr->nItemCount, lpID) == -1) goto fail;
7686 is_sorted = (infoPtr->dwStyle & (LVS_SORTASCENDING | LVS_SORTDESCENDING)) &&
7687 !(infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (LPSTR_TEXTCALLBACKW != lpLVItem->pszText);
7689 if (lpLVItem->iItem < 0 && !is_sorted) return -1;
7691 /* calculate new item index */
7692 if (is_sorted)
7694 HDPA hItem;
7695 ITEM_INFO *item_s;
7696 INT i = 0, cmpv;
7698 while (i < infoPtr->nItemCount)
7700 hItem = DPA_GetPtr( infoPtr->hdpaItems, i);
7701 item_s = DPA_GetPtr(hItem, 0);
7703 cmpv = textcmpWT(item_s->hdr.pszText, lpLVItem->pszText, isW);
7704 if (infoPtr->dwStyle & LVS_SORTDESCENDING) cmpv *= -1;
7706 if (cmpv >= 0) break;
7707 i++;
7709 nItem = i;
7711 else
7712 nItem = min(lpLVItem->iItem, infoPtr->nItemCount);
7714 TRACE(" inserting at %d, sorted=%d, count=%d, iItem=%d\n", nItem, is_sorted, infoPtr->nItemCount, lpLVItem->iItem);
7715 nItem = DPA_InsertPtr( infoPtr->hdpaItems, nItem, hdpaSubItems );
7716 if (nItem == -1) goto fail;
7717 infoPtr->nItemCount++;
7719 /* shift indices first so they don't get tangled */
7720 LISTVIEW_ShiftIndices(infoPtr, nItem, 1);
7722 /* set the item attributes */
7723 if (lpLVItem->mask & (LVIF_GROUPID|LVIF_COLUMNS))
7725 /* full size structure expected - _WIN32IE >= 0x560 */
7726 item = *lpLVItem;
7728 else if (lpLVItem->mask & LVIF_INDENT)
7730 /* indent member expected - _WIN32IE >= 0x300 */
7731 memcpy(&item, lpLVItem, offsetof( LVITEMW, iGroupId ));
7733 else
7735 /* minimal structure expected */
7736 memcpy(&item, lpLVItem, offsetof( LVITEMW, iIndent ));
7738 item.iItem = nItem;
7739 if (infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES)
7741 item.mask |= LVIF_STATE;
7742 item.stateMask |= LVIS_STATEIMAGEMASK;
7743 item.state &= ~LVIS_STATEIMAGEMASK;
7744 item.state |= INDEXTOSTATEIMAGEMASK(1);
7746 if (!set_main_item(infoPtr, &item, TRUE, isW, &has_changed)) goto undo;
7748 /* make room for the position, if we are in the right mode */
7749 if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
7751 if (DPA_InsertPtr(infoPtr->hdpaPosX, nItem, 0) == -1)
7752 goto undo;
7753 if (DPA_InsertPtr(infoPtr->hdpaPosY, nItem, 0) == -1)
7755 DPA_DeletePtr(infoPtr->hdpaPosX, nItem);
7756 goto undo;
7760 /* send LVN_INSERTITEM notification */
7761 ZeroMemory(&nmlv, sizeof(NMLISTVIEW));
7762 nmlv.iItem = nItem;
7763 nmlv.lParam = lpItem->lParam;
7764 notify_listview(infoPtr, LVN_INSERTITEM, &nmlv);
7765 if (!IsWindow(hwndSelf))
7766 return -1;
7768 /* align items (set position of each item) */
7769 if (infoPtr->uView == LV_VIEW_SMALLICON || infoPtr->uView == LV_VIEW_ICON)
7771 POINT pt;
7773 if (infoPtr->dwStyle & LVS_ALIGNLEFT)
7774 LISTVIEW_NextIconPosLeft(infoPtr, &pt);
7775 else
7776 LISTVIEW_NextIconPosTop(infoPtr, &pt);
7778 LISTVIEW_MoveIconTo(infoPtr, nItem, &pt, TRUE);
7781 /* now is the invalidation fun */
7782 LISTVIEW_ScrollOnInsert(infoPtr, nItem, 1);
7783 return nItem;
7785 undo:
7786 LISTVIEW_ShiftIndices(infoPtr, nItem, -1);
7787 DPA_DeletePtr(infoPtr->hdpaItems, nItem);
7788 infoPtr->nItemCount--;
7789 fail:
7790 DPA_DeletePtr(hdpaSubItems, 0);
7791 DPA_Destroy (hdpaSubItems);
7792 Free (lpItem);
7793 return -1;
7796 /***
7797 * DESCRIPTION:
7798 * Checks item visibility.
7800 * PARAMETER(S):
7801 * [I] infoPtr : valid pointer to the listview structure
7802 * [I] nFirst : item index to check for
7804 * RETURN:
7805 * Item visible : TRUE
7806 * Item invisible or failure : FALSE
7808 static BOOL LISTVIEW_IsItemVisible(const LISTVIEW_INFO *infoPtr, INT nItem)
7810 POINT Origin, Position;
7811 RECT rcItem;
7812 HDC hdc;
7813 BOOL ret;
7815 TRACE("nItem=%d\n", nItem);
7817 if (nItem < 0 || nItem >= DPA_GetPtrCount(infoPtr->hdpaItems)) return FALSE;
7819 LISTVIEW_GetOrigin(infoPtr, &Origin);
7820 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
7821 rcItem.left = Position.x + Origin.x;
7822 rcItem.top = Position.y + Origin.y;
7823 rcItem.right = rcItem.left + infoPtr->nItemWidth;
7824 rcItem.bottom = rcItem.top + infoPtr->nItemHeight;
7826 hdc = GetDC(infoPtr->hwndSelf);
7827 if (!hdc) return FALSE;
7828 ret = RectVisible(hdc, &rcItem);
7829 ReleaseDC(infoPtr->hwndSelf, hdc);
7831 return ret;
7834 /***
7835 * DESCRIPTION:
7836 * Redraws a range of items.
7838 * PARAMETER(S):
7839 * [I] infoPtr : valid pointer to the listview structure
7840 * [I] nFirst : first item
7841 * [I] nLast : last item
7843 * RETURN:
7844 * SUCCESS : TRUE
7845 * FAILURE : FALSE
7847 static BOOL LISTVIEW_RedrawItems(const LISTVIEW_INFO *infoPtr, INT nFirst, INT nLast)
7849 INT i;
7851 if (nLast < nFirst || min(nFirst, nLast) < 0 ||
7852 max(nFirst, nLast) >= infoPtr->nItemCount)
7853 return FALSE;
7855 for (i = nFirst; i <= nLast; i++)
7856 LISTVIEW_InvalidateItem(infoPtr, i);
7858 return TRUE;
7861 /***
7862 * DESCRIPTION:
7863 * Scroll the content of a listview.
7865 * PARAMETER(S):
7866 * [I] infoPtr : valid pointer to the listview structure
7867 * [I] dx : horizontal scroll amount in pixels
7868 * [I] dy : vertical scroll amount in pixels
7870 * RETURN:
7871 * SUCCESS : TRUE
7872 * FAILURE : FALSE
7874 * COMMENTS:
7875 * If the control is in report view (LV_VIEW_DETAILS) the control can
7876 * be scrolled only in line increments. "dy" will be rounded to the
7877 * nearest number of pixels that are a whole line. Ex: if line height
7878 * is 16 and an 8 is passed, the list will be scrolled by 16. If a 7
7879 * is passed, then the scroll will be 0. (per MSDN 7/2002)
7881 * For: (per experimentation with native control and CSpy ListView)
7882 * LV_VIEW_ICON scrolling in any direction is allowed
7883 * LV_VIEW_SMALLICON scrolling in any direction is allowed
7884 * LV_VIEW_LIST dx=1 = 1 column (horizontal only)
7885 * but will only scroll 1 column per message
7886 * no matter what the value.
7887 * dy must be 0 or FALSE returned.
7888 * LV_VIEW_DETAILS dx=1 = 1 pixel
7889 * dy= see above
7892 static BOOL LISTVIEW_Scroll(LISTVIEW_INFO *infoPtr, INT dx, INT dy)
7894 switch(infoPtr->uView) {
7895 case LV_VIEW_DETAILS:
7896 dy += (dy < 0 ? -1 : 1) * infoPtr->nItemHeight/2;
7897 dy /= infoPtr->nItemHeight;
7898 break;
7899 case LV_VIEW_LIST:
7900 if (dy != 0) return FALSE;
7901 break;
7902 default: /* icon */
7903 break;
7906 if (dx != 0) LISTVIEW_HScroll(infoPtr, SB_INTERNAL, dx);
7907 if (dy != 0) LISTVIEW_VScroll(infoPtr, SB_INTERNAL, dy);
7909 return TRUE;
7912 /***
7913 * DESCRIPTION:
7914 * Sets the background color.
7916 * PARAMETER(S):
7917 * [I] infoPtr : valid pointer to the listview structure
7918 * [I] color : background color
7920 * RETURN:
7921 * SUCCESS : TRUE
7922 * FAILURE : FALSE
7924 static BOOL LISTVIEW_SetBkColor(LISTVIEW_INFO *infoPtr, COLORREF color)
7926 TRACE("(color=%x)\n", color);
7928 if(infoPtr->clrBk != color) {
7929 if (infoPtr->clrBk != CLR_NONE) DeleteObject(infoPtr->hBkBrush);
7930 infoPtr->clrBk = color;
7931 if (color == CLR_NONE)
7932 infoPtr->hBkBrush = (HBRUSH)GetClassLongPtrW(infoPtr->hwndSelf, GCLP_HBRBACKGROUND);
7933 else
7935 infoPtr->hBkBrush = CreateSolidBrush(color);
7936 infoPtr->dwLvExStyle &= ~LVS_EX_TRANSPARENTBKGND;
7940 return TRUE;
7943 /* LISTVIEW_SetBkImage */
7945 /*** Helper for {Insert,Set}ColumnT *only* */
7946 static void column_fill_hditem(const LISTVIEW_INFO *infoPtr, HDITEMW *lphdi, INT nColumn,
7947 const LVCOLUMNW *lpColumn, BOOL isW)
7949 if (lpColumn->mask & LVCF_FMT)
7951 /* format member is valid */
7952 lphdi->mask |= HDI_FORMAT;
7954 /* set text alignment (leftmost column must be left-aligned) */
7955 if (nColumn == 0 || (lpColumn->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_LEFT)
7956 lphdi->fmt |= HDF_LEFT;
7957 else if ((lpColumn->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_RIGHT)
7958 lphdi->fmt |= HDF_RIGHT;
7959 else if ((lpColumn->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_CENTER)
7960 lphdi->fmt |= HDF_CENTER;
7962 if (lpColumn->fmt & LVCFMT_BITMAP_ON_RIGHT)
7963 lphdi->fmt |= HDF_BITMAP_ON_RIGHT;
7965 if (lpColumn->fmt & LVCFMT_COL_HAS_IMAGES)
7967 lphdi->fmt |= HDF_IMAGE;
7968 lphdi->iImage = I_IMAGECALLBACK;
7971 if (lpColumn->fmt & LVCFMT_FIXED_WIDTH)
7972 lphdi->fmt |= HDF_FIXEDWIDTH;
7975 if (lpColumn->mask & LVCF_WIDTH)
7977 lphdi->mask |= HDI_WIDTH;
7978 if(lpColumn->cx == LVSCW_AUTOSIZE_USEHEADER)
7980 /* make it fill the remainder of the controls width */
7981 RECT rcHeader;
7982 INT item_index;
7984 for(item_index = 0; item_index < (nColumn - 1); item_index++)
7986 LISTVIEW_GetHeaderRect(infoPtr, item_index, &rcHeader);
7987 lphdi->cxy += rcHeader.right - rcHeader.left;
7990 /* retrieve the layout of the header */
7991 GetClientRect(infoPtr->hwndSelf, &rcHeader);
7992 TRACE("start cxy=%d rcHeader=%s\n", lphdi->cxy, wine_dbgstr_rect(&rcHeader));
7994 lphdi->cxy = (rcHeader.right - rcHeader.left) - lphdi->cxy;
7996 else
7997 lphdi->cxy = lpColumn->cx;
8000 if (lpColumn->mask & LVCF_TEXT)
8002 lphdi->mask |= HDI_TEXT | HDI_FORMAT;
8003 lphdi->fmt |= HDF_STRING;
8004 lphdi->pszText = lpColumn->pszText;
8005 lphdi->cchTextMax = textlenT(lpColumn->pszText, isW);
8008 if (lpColumn->mask & LVCF_IMAGE)
8010 lphdi->mask |= HDI_IMAGE;
8011 lphdi->iImage = lpColumn->iImage;
8014 if (lpColumn->mask & LVCF_ORDER)
8016 lphdi->mask |= HDI_ORDER;
8017 lphdi->iOrder = lpColumn->iOrder;
8022 /***
8023 * DESCRIPTION:
8024 * Inserts a new column.
8026 * PARAMETER(S):
8027 * [I] infoPtr : valid pointer to the listview structure
8028 * [I] nColumn : column index
8029 * [I] lpColumn : column information
8030 * [I] isW : TRUE if lpColumn is Unicode, FALSE otherwise
8032 * RETURN:
8033 * SUCCESS : new column index
8034 * FAILURE : -1
8036 static INT LISTVIEW_InsertColumnT(LISTVIEW_INFO *infoPtr, INT nColumn,
8037 const LVCOLUMNW *lpColumn, BOOL isW)
8039 COLUMN_INFO *lpColumnInfo;
8040 INT nNewColumn;
8041 HDITEMW hdi;
8043 TRACE("(nColumn=%d, lpColumn=%s, isW=%d)\n", nColumn, debuglvcolumn_t(lpColumn, isW), isW);
8045 if (!lpColumn || nColumn < 0) return -1;
8046 nColumn = min(nColumn, DPA_GetPtrCount(infoPtr->hdpaColumns));
8048 ZeroMemory(&hdi, sizeof(HDITEMW));
8049 column_fill_hditem(infoPtr, &hdi, nColumn, lpColumn, isW);
8052 * A mask not including LVCF_WIDTH turns into a mask of width, width 10
8053 * (can be seen in SPY) otherwise column never gets added.
8055 if (!(lpColumn->mask & LVCF_WIDTH)) {
8056 hdi.mask |= HDI_WIDTH;
8057 hdi.cxy = 10;
8061 * when the iSubItem is available Windows copies it to the header lParam. It seems
8062 * to happen only in LVM_INSERTCOLUMN - not in LVM_SETCOLUMN
8064 if (lpColumn->mask & LVCF_SUBITEM)
8066 hdi.mask |= HDI_LPARAM;
8067 hdi.lParam = lpColumn->iSubItem;
8070 /* create header if not present */
8071 LISTVIEW_CreateHeader(infoPtr);
8072 if (!(LVS_NOCOLUMNHEADER & infoPtr->dwStyle) &&
8073 (infoPtr->uView == LV_VIEW_DETAILS) && (WS_VISIBLE & infoPtr->dwStyle))
8075 ShowWindow(infoPtr->hwndHeader, SW_SHOWNORMAL);
8078 /* insert item in header control */
8079 nNewColumn = SendMessageW(infoPtr->hwndHeader,
8080 isW ? HDM_INSERTITEMW : HDM_INSERTITEMA,
8081 nColumn, (LPARAM)&hdi);
8082 if (nNewColumn == -1) return -1;
8083 if (nNewColumn != nColumn) ERR("nColumn=%d, nNewColumn=%d\n", nColumn, nNewColumn);
8085 /* create our own column info */
8086 if (!(lpColumnInfo = Alloc(sizeof(COLUMN_INFO)))) goto fail;
8087 if (DPA_InsertPtr(infoPtr->hdpaColumns, nNewColumn, lpColumnInfo) == -1) goto fail;
8089 if (lpColumn->mask & LVCF_FMT) lpColumnInfo->fmt = lpColumn->fmt;
8090 if (lpColumn->mask & LVCF_MINWIDTH) lpColumnInfo->cxMin = lpColumn->cxMin;
8091 if (!SendMessageW(infoPtr->hwndHeader, HDM_GETITEMRECT, nNewColumn, (LPARAM)&lpColumnInfo->rcHeader))
8092 goto fail;
8094 /* now we have to actually adjust the data */
8095 if (!(infoPtr->dwStyle & LVS_OWNERDATA) && infoPtr->nItemCount > 0)
8097 SUBITEM_INFO *lpSubItem;
8098 HDPA hdpaSubItems;
8099 INT nItem, i;
8100 LVITEMW item;
8101 BOOL changed;
8103 item.iSubItem = nNewColumn;
8104 item.mask = LVIF_TEXT | LVIF_IMAGE;
8105 item.iImage = I_IMAGECALLBACK;
8106 item.pszText = LPSTR_TEXTCALLBACKW;
8108 for (nItem = 0; nItem < infoPtr->nItemCount; nItem++)
8110 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, nItem);
8111 for (i = 1; i < DPA_GetPtrCount(hdpaSubItems); i++)
8113 lpSubItem = DPA_GetPtr(hdpaSubItems, i);
8114 if (lpSubItem->iSubItem >= nNewColumn)
8115 lpSubItem->iSubItem++;
8118 /* add new subitem for each item */
8119 item.iItem = nItem;
8120 set_sub_item(infoPtr, &item, isW, &changed);
8124 /* make space for the new column */
8125 LISTVIEW_ScrollColumns(infoPtr, nNewColumn + 1, lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left);
8126 LISTVIEW_UpdateItemSize(infoPtr);
8128 return nNewColumn;
8130 fail:
8131 if (nNewColumn != -1) SendMessageW(infoPtr->hwndHeader, HDM_DELETEITEM, nNewColumn, 0);
8132 if (lpColumnInfo)
8134 DPA_DeletePtr(infoPtr->hdpaColumns, nNewColumn);
8135 Free(lpColumnInfo);
8137 return -1;
8140 /***
8141 * DESCRIPTION:
8142 * Sets the attributes of a header item.
8144 * PARAMETER(S):
8145 * [I] infoPtr : valid pointer to the listview structure
8146 * [I] nColumn : column index
8147 * [I] lpColumn : column attributes
8148 * [I] isW: if TRUE, then lpColumn is a LPLVCOLUMNW, else it is a LPLVCOLUMNA
8150 * RETURN:
8151 * SUCCESS : TRUE
8152 * FAILURE : FALSE
8154 static BOOL LISTVIEW_SetColumnT(const LISTVIEW_INFO *infoPtr, INT nColumn,
8155 const LVCOLUMNW *lpColumn, BOOL isW)
8157 HDITEMW hdi, hdiget;
8158 BOOL bResult;
8160 TRACE("(nColumn=%d, lpColumn=%s, isW=%d)\n", nColumn, debuglvcolumn_t(lpColumn, isW), isW);
8162 if (!lpColumn || nColumn < 0 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
8164 ZeroMemory(&hdi, sizeof(HDITEMW));
8165 if (lpColumn->mask & LVCF_FMT)
8167 hdi.mask |= HDI_FORMAT;
8168 hdiget.mask = HDI_FORMAT;
8169 if (SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nColumn, (LPARAM)&hdiget))
8170 hdi.fmt = hdiget.fmt & HDF_STRING;
8172 column_fill_hditem(infoPtr, &hdi, nColumn, lpColumn, isW);
8174 /* set header item attributes */
8175 bResult = SendMessageW(infoPtr->hwndHeader, isW ? HDM_SETITEMW : HDM_SETITEMA, nColumn, (LPARAM)&hdi);
8176 if (!bResult) return FALSE;
8178 if (lpColumn->mask & LVCF_FMT)
8180 COLUMN_INFO *lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, nColumn);
8181 INT oldFmt = lpColumnInfo->fmt;
8183 lpColumnInfo->fmt = lpColumn->fmt;
8184 if ((oldFmt ^ lpColumn->fmt) & (LVCFMT_JUSTIFYMASK | LVCFMT_IMAGE))
8186 if (infoPtr->uView == LV_VIEW_DETAILS) LISTVIEW_InvalidateColumn(infoPtr, nColumn);
8190 if (lpColumn->mask & LVCF_MINWIDTH)
8191 LISTVIEW_GetColumnInfo(infoPtr, nColumn)->cxMin = lpColumn->cxMin;
8193 return TRUE;
8196 /***
8197 * DESCRIPTION:
8198 * Sets the column order array
8200 * PARAMETERS:
8201 * [I] infoPtr : valid pointer to the listview structure
8202 * [I] iCount : number of elements in column order array
8203 * [I] lpiArray : pointer to column order array
8205 * RETURN:
8206 * SUCCESS : TRUE
8207 * FAILURE : FALSE
8209 static BOOL LISTVIEW_SetColumnOrderArray(LISTVIEW_INFO *infoPtr, INT iCount, const INT *lpiArray)
8211 TRACE("iCount %d lpiArray %p\n", iCount, lpiArray);
8213 if (!lpiArray || !IsWindow(infoPtr->hwndHeader)) return FALSE;
8215 infoPtr->colRectsDirty = TRUE;
8217 return SendMessageW(infoPtr->hwndHeader, HDM_SETORDERARRAY, iCount, (LPARAM)lpiArray);
8220 /***
8221 * DESCRIPTION:
8222 * Sets the width of a column
8224 * PARAMETERS:
8225 * [I] infoPtr : valid pointer to the listview structure
8226 * [I] nColumn : column index
8227 * [I] cx : column width
8229 * RETURN:
8230 * SUCCESS : TRUE
8231 * FAILURE : FALSE
8233 static BOOL LISTVIEW_SetColumnWidth(LISTVIEW_INFO *infoPtr, INT nColumn, INT cx)
8235 WCHAR szDispText[DISP_TEXT_SIZE] = { 0 };
8236 INT max_cx = 0;
8237 HDITEMW hdi;
8239 TRACE("(nColumn=%d, cx=%d\n", nColumn, cx);
8241 /* set column width only if in report or list mode */
8242 if (infoPtr->uView != LV_VIEW_DETAILS && infoPtr->uView != LV_VIEW_LIST) return FALSE;
8244 /* take care of invalid cx values */
8245 if(infoPtr->uView == LV_VIEW_DETAILS && cx < -2) cx = LVSCW_AUTOSIZE;
8246 else if (infoPtr->uView == LV_VIEW_LIST && cx < 1) return FALSE;
8248 /* resize all columns if in LV_VIEW_LIST mode */
8249 if(infoPtr->uView == LV_VIEW_LIST)
8251 infoPtr->nItemWidth = cx;
8252 LISTVIEW_InvalidateList(infoPtr);
8253 return TRUE;
8256 if (nColumn < 0 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
8258 if (cx == LVSCW_AUTOSIZE || (cx == LVSCW_AUTOSIZE_USEHEADER && nColumn < DPA_GetPtrCount(infoPtr->hdpaColumns) -1))
8260 INT nLabelWidth;
8261 LVITEMW lvItem;
8263 lvItem.mask = LVIF_TEXT;
8264 lvItem.iItem = 0;
8265 lvItem.iSubItem = nColumn;
8266 lvItem.pszText = szDispText;
8267 lvItem.cchTextMax = DISP_TEXT_SIZE;
8268 for (; lvItem.iItem < infoPtr->nItemCount; lvItem.iItem++)
8270 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) continue;
8271 nLabelWidth = LISTVIEW_GetStringWidthT(infoPtr, lvItem.pszText, TRUE);
8272 if (max_cx < nLabelWidth) max_cx = nLabelWidth;
8274 if (infoPtr->himlSmall && (nColumn == 0 || (LISTVIEW_GetColumnInfo(infoPtr, nColumn)->fmt & LVCFMT_IMAGE)))
8275 max_cx += infoPtr->iconSize.cx;
8276 max_cx += TRAILING_LABEL_PADDING;
8279 /* autosize based on listview items width */
8280 if(cx == LVSCW_AUTOSIZE)
8281 cx = max_cx;
8282 else if(cx == LVSCW_AUTOSIZE_USEHEADER)
8284 /* if iCol is the last column make it fill the remainder of the controls width */
8285 if(nColumn == DPA_GetPtrCount(infoPtr->hdpaColumns) - 1)
8287 RECT rcHeader;
8288 POINT Origin;
8290 LISTVIEW_GetOrigin(infoPtr, &Origin);
8291 LISTVIEW_GetHeaderRect(infoPtr, nColumn, &rcHeader);
8293 cx = infoPtr->rcList.right - Origin.x - rcHeader.left;
8295 else
8297 /* Despite what the MS docs say, if this is not the last
8298 column, then MS resizes the column to the width of the
8299 largest text string in the column, including headers
8300 and items. This is different from LVSCW_AUTOSIZE in that
8301 LVSCW_AUTOSIZE ignores the header string length. */
8302 cx = 0;
8304 /* retrieve header text */
8305 hdi.mask = HDI_TEXT;
8306 hdi.cchTextMax = DISP_TEXT_SIZE;
8307 hdi.pszText = szDispText;
8308 if (SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nColumn, (LPARAM)&hdi))
8310 HDC hdc = GetDC(infoPtr->hwndSelf);
8311 HFONT old_font = SelectObject(hdc, (HFONT)SendMessageW(infoPtr->hwndHeader, WM_GETFONT, 0, 0));
8312 SIZE size;
8314 if (GetTextExtentPoint32W(hdc, hdi.pszText, lstrlenW(hdi.pszText), &size))
8315 cx = size.cx + TRAILING_HEADER_PADDING;
8316 /* FIXME: Take into account the header image, if one is present */
8317 SelectObject(hdc, old_font);
8318 ReleaseDC(infoPtr->hwndSelf, hdc);
8320 cx = max (cx, max_cx);
8324 if (cx < 0) return FALSE;
8326 /* call header to update the column change */
8327 hdi.mask = HDI_WIDTH;
8328 hdi.cxy = max(cx, LISTVIEW_GetColumnInfo(infoPtr, nColumn)->cxMin);
8329 TRACE("hdi.cxy=%d\n", hdi.cxy);
8330 return SendMessageW(infoPtr->hwndHeader, HDM_SETITEMW, nColumn, (LPARAM)&hdi);
8333 /***
8334 * Creates the checkbox imagelist. Helper for LISTVIEW_SetExtendedListViewStyle
8337 static HIMAGELIST LISTVIEW_CreateCheckBoxIL(const LISTVIEW_INFO *infoPtr)
8339 HDC hdc_wnd, hdc;
8340 HBITMAP hbm_im, hbm_mask, hbm_orig;
8341 RECT rc;
8342 HBRUSH hbr_white = GetStockObject(WHITE_BRUSH);
8343 HBRUSH hbr_black = GetStockObject(BLACK_BRUSH);
8344 HIMAGELIST himl;
8346 himl = ImageList_Create(GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON),
8347 ILC_COLOR | ILC_MASK, 2, 2);
8348 hdc_wnd = GetDC(infoPtr->hwndSelf);
8349 hdc = CreateCompatibleDC(hdc_wnd);
8350 hbm_im = CreateCompatibleBitmap(hdc_wnd, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON));
8351 hbm_mask = CreateBitmap(GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), 1, 1, NULL);
8352 ReleaseDC(infoPtr->hwndSelf, hdc_wnd);
8354 rc.left = rc.top = 0;
8355 rc.right = GetSystemMetrics(SM_CXSMICON);
8356 rc.bottom = GetSystemMetrics(SM_CYSMICON);
8358 hbm_orig = SelectObject(hdc, hbm_mask);
8359 FillRect(hdc, &rc, hbr_white);
8360 InflateRect(&rc, -2, -2);
8361 FillRect(hdc, &rc, hbr_black);
8363 SelectObject(hdc, hbm_im);
8364 DrawFrameControl(hdc, &rc, DFC_BUTTON, DFCS_BUTTONCHECK | DFCS_MONO);
8365 SelectObject(hdc, hbm_orig);
8366 ImageList_Add(himl, hbm_im, hbm_mask);
8368 SelectObject(hdc, hbm_im);
8369 DrawFrameControl(hdc, &rc, DFC_BUTTON, DFCS_BUTTONCHECK | DFCS_MONO | DFCS_CHECKED);
8370 SelectObject(hdc, hbm_orig);
8371 ImageList_Add(himl, hbm_im, hbm_mask);
8373 DeleteObject(hbm_mask);
8374 DeleteObject(hbm_im);
8375 DeleteDC(hdc);
8377 return himl;
8380 /***
8381 * DESCRIPTION:
8382 * Sets the extended listview style.
8384 * PARAMETERS:
8385 * [I] infoPtr : valid pointer to the listview structure
8386 * [I] dwMask : mask
8387 * [I] dwStyle : style
8389 * RETURN:
8390 * SUCCESS : previous style
8391 * FAILURE : 0
8393 static DWORD LISTVIEW_SetExtendedListViewStyle(LISTVIEW_INFO *infoPtr, DWORD mask, DWORD ex_style)
8395 DWORD old_ex_style = infoPtr->dwLvExStyle;
8397 TRACE("mask=0x%08x, ex_style=0x%08x\n", mask, ex_style);
8399 /* set new style */
8400 if (mask)
8401 infoPtr->dwLvExStyle = (old_ex_style & ~mask) | (ex_style & mask);
8402 else
8403 infoPtr->dwLvExStyle = ex_style;
8405 if((infoPtr->dwLvExStyle ^ old_ex_style) & LVS_EX_CHECKBOXES)
8407 HIMAGELIST himl = 0;
8408 if(infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES)
8410 LVITEMW item;
8411 item.mask = LVIF_STATE;
8412 item.stateMask = LVIS_STATEIMAGEMASK;
8413 item.state = INDEXTOSTATEIMAGEMASK(1);
8414 LISTVIEW_SetItemState(infoPtr, -1, &item);
8416 himl = LISTVIEW_CreateCheckBoxIL(infoPtr);
8417 if(!(infoPtr->dwStyle & LVS_SHAREIMAGELISTS))
8418 ImageList_Destroy(infoPtr->himlState);
8420 himl = LISTVIEW_SetImageList(infoPtr, LVSIL_STATE, himl);
8421 /* checkbox list replaces previous custom list or... */
8422 if(((infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES) &&
8423 !(infoPtr->dwStyle & LVS_SHAREIMAGELISTS)) ||
8424 /* ...previous was checkbox list */
8425 (old_ex_style & LVS_EX_CHECKBOXES))
8426 ImageList_Destroy(himl);
8429 if((infoPtr->dwLvExStyle ^ old_ex_style) & LVS_EX_HEADERDRAGDROP)
8431 DWORD style;
8433 /* if not already created */
8434 LISTVIEW_CreateHeader(infoPtr);
8436 style = GetWindowLongW(infoPtr->hwndHeader, GWL_STYLE);
8437 if (infoPtr->dwLvExStyle & LVS_EX_HEADERDRAGDROP)
8438 style |= HDS_DRAGDROP;
8439 else
8440 style &= ~HDS_DRAGDROP;
8441 SetWindowLongW(infoPtr->hwndHeader, GWL_STYLE, style);
8444 /* GRIDLINES adds decoration at top so changes sizes */
8445 if((infoPtr->dwLvExStyle ^ old_ex_style) & LVS_EX_GRIDLINES)
8447 LISTVIEW_CreateHeader(infoPtr);
8448 LISTVIEW_UpdateSize(infoPtr);
8451 if((infoPtr->dwLvExStyle ^ old_ex_style) & LVS_EX_FULLROWSELECT)
8453 LISTVIEW_CreateHeader(infoPtr);
8456 if((infoPtr->dwLvExStyle ^ old_ex_style) & LVS_EX_TRANSPARENTBKGND)
8458 if (infoPtr->dwLvExStyle & LVS_EX_TRANSPARENTBKGND)
8459 LISTVIEW_SetBkColor(infoPtr, CLR_NONE);
8462 if((infoPtr->dwLvExStyle ^ old_ex_style) & LVS_EX_HEADERINALLVIEWS)
8464 if (infoPtr->dwLvExStyle & LVS_EX_HEADERINALLVIEWS)
8465 LISTVIEW_CreateHeader(infoPtr);
8466 else
8467 ShowWindow(infoPtr->hwndHeader, SW_HIDE);
8468 LISTVIEW_UpdateSize(infoPtr);
8469 LISTVIEW_UpdateScroll(infoPtr);
8472 LISTVIEW_InvalidateList(infoPtr);
8473 return old_ex_style;
8476 /***
8477 * DESCRIPTION:
8478 * Sets the new hot cursor used during hot tracking and hover selection.
8480 * PARAMETER(S):
8481 * [I] infoPtr : valid pointer to the listview structure
8482 * [I] hCursor : the new hot cursor handle
8484 * RETURN:
8485 * Returns the previous hot cursor
8487 static HCURSOR LISTVIEW_SetHotCursor(LISTVIEW_INFO *infoPtr, HCURSOR hCursor)
8489 HCURSOR oldCursor = infoPtr->hHotCursor;
8491 infoPtr->hHotCursor = hCursor;
8493 return oldCursor;
8497 /***
8498 * DESCRIPTION:
8499 * Sets the hot item index.
8501 * PARAMETERS:
8502 * [I] infoPtr : valid pointer to the listview structure
8503 * [I] iIndex : index
8505 * RETURN:
8506 * SUCCESS : previous hot item index
8507 * FAILURE : -1 (no hot item)
8509 static INT LISTVIEW_SetHotItem(LISTVIEW_INFO *infoPtr, INT iIndex)
8511 INT iOldIndex = infoPtr->nHotItem;
8513 infoPtr->nHotItem = iIndex;
8515 return iOldIndex;
8519 /***
8520 * DESCRIPTION:
8521 * Sets the amount of time the cursor must hover over an item before it is selected.
8523 * PARAMETER(S):
8524 * [I] infoPtr : valid pointer to the listview structure
8525 * [I] dwHoverTime : hover time, if -1 the hover time is set to the default
8527 * RETURN:
8528 * Returns the previous hover time
8530 static DWORD LISTVIEW_SetHoverTime(LISTVIEW_INFO *infoPtr, DWORD dwHoverTime)
8532 DWORD oldHoverTime = infoPtr->dwHoverTime;
8534 infoPtr->dwHoverTime = dwHoverTime;
8536 return oldHoverTime;
8539 /***
8540 * DESCRIPTION:
8541 * Sets spacing for icons of LVS_ICON style.
8543 * PARAMETER(S):
8544 * [I] infoPtr : valid pointer to the listview structure
8545 * [I] cx : horizontal spacing (-1 = system spacing, 0 = autosize)
8546 * [I] cy : vertical spacing (-1 = system spacing, 0 = autosize)
8548 * RETURN:
8549 * MAKELONG(oldcx, oldcy)
8551 static DWORD LISTVIEW_SetIconSpacing(LISTVIEW_INFO *infoPtr, INT cx, INT cy)
8553 DWORD oldspacing = MAKELONG(infoPtr->iconSpacing.cx, infoPtr->iconSpacing.cy);
8555 TRACE("requested=(%d,%d)\n", cx, cy);
8557 /* this is supported only for LVS_ICON style */
8558 if (infoPtr->uView != LV_VIEW_ICON) return oldspacing;
8560 /* set to defaults, if instructed to */
8561 if (cx == -1) cx = GetSystemMetrics(SM_CXICONSPACING);
8562 if (cy == -1) cy = GetSystemMetrics(SM_CYICONSPACING);
8564 /* if 0 then compute width
8565 * FIXME: computed cx and cy is not matching native behaviour */
8566 if (cx == 0) {
8567 cx = GetSystemMetrics(SM_CXICONSPACING);
8568 if (infoPtr->iconSize.cx + ICON_LR_PADDING > cx)
8569 cx = infoPtr->iconSize.cx + ICON_LR_PADDING;
8572 /* if 0 then compute height */
8573 if (cy == 0)
8574 cy = infoPtr->iconSize.cy + 2 * infoPtr->ntmHeight +
8575 ICON_BOTTOM_PADDING + ICON_TOP_PADDING + LABEL_VERT_PADDING;
8578 infoPtr->iconSpacing.cx = cx;
8579 infoPtr->iconSpacing.cy = cy;
8581 TRACE("old=(%d,%d), new=(%d,%d), iconSize=(%d,%d), ntmH=%d\n",
8582 LOWORD(oldspacing), HIWORD(oldspacing), cx, cy,
8583 infoPtr->iconSize.cx, infoPtr->iconSize.cy,
8584 infoPtr->ntmHeight);
8586 /* these depend on the iconSpacing */
8587 LISTVIEW_UpdateItemSize(infoPtr);
8589 return oldspacing;
8592 static inline void set_icon_size(SIZE *size, HIMAGELIST himl, BOOL small)
8594 INT cx, cy;
8596 if (himl && ImageList_GetIconSize(himl, &cx, &cy))
8598 size->cx = cx;
8599 size->cy = cy;
8601 else
8603 size->cx = GetSystemMetrics(small ? SM_CXSMICON : SM_CXICON);
8604 size->cy = GetSystemMetrics(small ? SM_CYSMICON : SM_CYICON);
8608 /***
8609 * DESCRIPTION:
8610 * Sets image lists.
8612 * PARAMETER(S):
8613 * [I] infoPtr : valid pointer to the listview structure
8614 * [I] nType : image list type
8615 * [I] himl : image list handle
8617 * RETURN:
8618 * SUCCESS : old image list
8619 * FAILURE : NULL
8621 static HIMAGELIST LISTVIEW_SetImageList(LISTVIEW_INFO *infoPtr, INT nType, HIMAGELIST himl)
8623 INT oldHeight = infoPtr->nItemHeight;
8624 HIMAGELIST himlOld = 0;
8626 TRACE("(nType=%d, himl=%p\n", nType, himl);
8628 switch (nType)
8630 case LVSIL_NORMAL:
8631 himlOld = infoPtr->himlNormal;
8632 infoPtr->himlNormal = himl;
8633 if (infoPtr->uView == LV_VIEW_ICON) set_icon_size(&infoPtr->iconSize, himl, FALSE);
8634 LISTVIEW_SetIconSpacing(infoPtr, 0, 0);
8635 break;
8637 case LVSIL_SMALL:
8638 himlOld = infoPtr->himlSmall;
8639 infoPtr->himlSmall = himl;
8640 if (infoPtr->uView != LV_VIEW_ICON) set_icon_size(&infoPtr->iconSize, himl, TRUE);
8641 break;
8643 case LVSIL_STATE:
8644 himlOld = infoPtr->himlState;
8645 infoPtr->himlState = himl;
8646 set_icon_size(&infoPtr->iconStateSize, himl, TRUE);
8647 ImageList_SetBkColor(infoPtr->himlState, CLR_NONE);
8648 break;
8650 default:
8651 ERR("Unknown icon type=%d\n", nType);
8652 return NULL;
8655 infoPtr->nItemHeight = LISTVIEW_CalculateItemHeight(infoPtr);
8656 if (infoPtr->nItemHeight != oldHeight)
8657 LISTVIEW_UpdateScroll(infoPtr);
8659 return himlOld;
8662 /***
8663 * DESCRIPTION:
8664 * Preallocates memory (does *not* set the actual count of items !)
8666 * PARAMETER(S):
8667 * [I] infoPtr : valid pointer to the listview structure
8668 * [I] nItems : item count (projected number of items to allocate)
8669 * [I] dwFlags : update flags
8671 * RETURN:
8672 * SUCCESS : TRUE
8673 * FAILURE : FALSE
8675 static BOOL LISTVIEW_SetItemCount(LISTVIEW_INFO *infoPtr, INT nItems, DWORD dwFlags)
8677 TRACE("(nItems=%d, dwFlags=%x)\n", nItems, dwFlags);
8679 if (infoPtr->dwStyle & LVS_OWNERDATA)
8681 INT nOldCount = infoPtr->nItemCount;
8683 if (nItems < nOldCount)
8685 RANGE range = { nItems, nOldCount };
8686 ranges_del(infoPtr->selectionRanges, range);
8687 if (infoPtr->nFocusedItem >= nItems)
8689 LISTVIEW_SetItemFocus(infoPtr, -1);
8690 SetRectEmpty(&infoPtr->rcFocus);
8694 infoPtr->nItemCount = nItems;
8695 LISTVIEW_UpdateScroll(infoPtr);
8697 /* the flags are valid only in ownerdata report and list modes */
8698 if (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON) dwFlags = 0;
8700 if (!(dwFlags & LVSICF_NOSCROLL) && infoPtr->nFocusedItem != -1)
8701 LISTVIEW_EnsureVisible(infoPtr, infoPtr->nFocusedItem, FALSE);
8703 if (!(dwFlags & LVSICF_NOINVALIDATEALL))
8704 LISTVIEW_InvalidateList(infoPtr);
8705 else
8707 INT nFrom, nTo;
8708 POINT Origin;
8709 RECT rcErase;
8711 LISTVIEW_GetOrigin(infoPtr, &Origin);
8712 nFrom = min(nOldCount, nItems);
8713 nTo = max(nOldCount, nItems);
8715 if (infoPtr->uView == LV_VIEW_DETAILS)
8717 rcErase.left = 0;
8718 rcErase.top = nFrom * infoPtr->nItemHeight;
8719 rcErase.right = infoPtr->nItemWidth;
8720 rcErase.bottom = nTo * infoPtr->nItemHeight;
8721 OffsetRect(&rcErase, Origin.x, Origin.y);
8722 if (IntersectRect(&rcErase, &rcErase, &infoPtr->rcList))
8723 LISTVIEW_InvalidateRect(infoPtr, &rcErase);
8725 else /* LV_VIEW_LIST */
8727 INT nPerCol = LISTVIEW_GetCountPerColumn(infoPtr);
8729 rcErase.left = (nFrom / nPerCol) * infoPtr->nItemWidth;
8730 rcErase.top = (nFrom % nPerCol) * infoPtr->nItemHeight;
8731 rcErase.right = rcErase.left + infoPtr->nItemWidth;
8732 rcErase.bottom = nPerCol * infoPtr->nItemHeight;
8733 OffsetRect(&rcErase, Origin.x, Origin.y);
8734 if (IntersectRect(&rcErase, &rcErase, &infoPtr->rcList))
8735 LISTVIEW_InvalidateRect(infoPtr, &rcErase);
8737 rcErase.left = (nFrom / nPerCol + 1) * infoPtr->nItemWidth;
8738 rcErase.top = 0;
8739 rcErase.right = (nTo / nPerCol + 1) * infoPtr->nItemWidth;
8740 rcErase.bottom = nPerCol * infoPtr->nItemHeight;
8741 OffsetRect(&rcErase, Origin.x, Origin.y);
8742 if (IntersectRect(&rcErase, &rcErase, &infoPtr->rcList))
8743 LISTVIEW_InvalidateRect(infoPtr, &rcErase);
8747 else
8749 /* According to MSDN for non-LVS_OWNERDATA this is just
8750 * a performance issue. The control allocates its internal
8751 * data structures for the number of items specified. It
8752 * cuts down on the number of memory allocations. Therefore
8753 * we will just issue a WARN here
8755 WARN("for non-ownerdata performance option not implemented.\n");
8758 return TRUE;
8761 /***
8762 * DESCRIPTION:
8763 * Sets the position of an item.
8765 * PARAMETER(S):
8766 * [I] infoPtr : valid pointer to the listview structure
8767 * [I] nItem : item index
8768 * [I] pt : coordinate
8770 * RETURN:
8771 * SUCCESS : TRUE
8772 * FAILURE : FALSE
8774 static BOOL LISTVIEW_SetItemPosition(LISTVIEW_INFO *infoPtr, INT nItem, const POINT *pt)
8776 POINT Origin, Pt;
8778 TRACE("(nItem=%d, pt=%s\n", nItem, wine_dbgstr_point(pt));
8780 if (!pt || nItem < 0 || nItem >= infoPtr->nItemCount ||
8781 !(infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON)) return FALSE;
8783 Pt = *pt;
8784 LISTVIEW_GetOrigin(infoPtr, &Origin);
8786 /* This point value seems to be an undocumented feature.
8787 * The best guess is that it means either at the origin,
8788 * or at true beginning of the list. I will assume the origin. */
8789 if ((Pt.x == -1) && (Pt.y == -1))
8790 Pt = Origin;
8792 if (infoPtr->uView == LV_VIEW_ICON)
8794 Pt.x -= (infoPtr->nItemWidth - infoPtr->iconSize.cx) / 2;
8795 Pt.y -= ICON_TOP_PADDING;
8797 Pt.x -= Origin.x;
8798 Pt.y -= Origin.y;
8800 infoPtr->bAutoarrange = FALSE;
8802 return LISTVIEW_MoveIconTo(infoPtr, nItem, &Pt, FALSE);
8805 /***
8806 * DESCRIPTION:
8807 * Sets the state of one or many items.
8809 * PARAMETER(S):
8810 * [I] infoPtr : valid pointer to the listview structure
8811 * [I] nItem : item index
8812 * [I] item : item or subitem info
8814 * RETURN:
8815 * SUCCESS : TRUE
8816 * FAILURE : FALSE
8818 static BOOL LISTVIEW_SetItemState(LISTVIEW_INFO *infoPtr, INT nItem, const LVITEMW *item)
8820 BOOL ret = TRUE;
8821 LVITEMW lvItem;
8823 if (!item) return FALSE;
8825 lvItem.iItem = nItem;
8826 lvItem.iSubItem = 0;
8827 lvItem.mask = LVIF_STATE;
8828 lvItem.state = item->state;
8829 lvItem.stateMask = item->stateMask;
8830 TRACE("item=%s\n", debuglvitem_t(&lvItem, TRUE));
8832 if (nItem == -1)
8834 UINT oldstate = 0;
8835 BOOL notify;
8837 /* select all isn't allowed in LVS_SINGLESEL */
8838 if ((lvItem.state & lvItem.stateMask & LVIS_SELECTED) && (infoPtr->dwStyle & LVS_SINGLESEL))
8839 return FALSE;
8841 /* focus all isn't allowed */
8842 if (lvItem.state & lvItem.stateMask & LVIS_FOCUSED) return FALSE;
8844 notify = infoPtr->bDoChangeNotify;
8845 if (infoPtr->dwStyle & LVS_OWNERDATA)
8847 infoPtr->bDoChangeNotify = FALSE;
8848 if (!(lvItem.state & LVIS_SELECTED) && LISTVIEW_GetSelectedCount(infoPtr))
8849 oldstate |= LVIS_SELECTED;
8850 if (infoPtr->nFocusedItem != -1) oldstate |= LVIS_FOCUSED;
8853 /* apply to all items */
8854 for (lvItem.iItem = 0; lvItem.iItem < infoPtr->nItemCount; lvItem.iItem++)
8855 if (!LISTVIEW_SetItemT(infoPtr, &lvItem, TRUE)) ret = FALSE;
8857 if (infoPtr->dwStyle & LVS_OWNERDATA)
8859 NMLISTVIEW nmlv;
8861 infoPtr->bDoChangeNotify = notify;
8863 nmlv.iItem = -1;
8864 nmlv.iSubItem = 0;
8865 nmlv.uNewState = lvItem.state & lvItem.stateMask;
8866 nmlv.uOldState = oldstate & lvItem.stateMask;
8867 nmlv.uChanged = LVIF_STATE;
8868 nmlv.ptAction.x = nmlv.ptAction.y = 0;
8869 nmlv.lParam = 0;
8871 notify_listview(infoPtr, LVN_ITEMCHANGED, &nmlv);
8874 else
8875 ret = LISTVIEW_SetItemT(infoPtr, &lvItem, TRUE);
8877 return ret;
8880 /***
8881 * DESCRIPTION:
8882 * Sets the text of an item or subitem.
8884 * PARAMETER(S):
8885 * [I] hwnd : window handle
8886 * [I] nItem : item index
8887 * [I] lpLVItem : item or subitem info
8888 * [I] isW : TRUE if input is Unicode
8890 * RETURN:
8891 * SUCCESS : TRUE
8892 * FAILURE : FALSE
8894 static BOOL LISTVIEW_SetItemTextT(LISTVIEW_INFO *infoPtr, INT nItem, const LVITEMW *lpLVItem, BOOL isW)
8896 LVITEMW lvItem;
8898 if (!lpLVItem || nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
8899 if (infoPtr->dwStyle & LVS_OWNERDATA) return FALSE;
8901 lvItem.iItem = nItem;
8902 lvItem.iSubItem = lpLVItem->iSubItem;
8903 lvItem.mask = LVIF_TEXT;
8904 lvItem.pszText = lpLVItem->pszText;
8905 lvItem.cchTextMax = lpLVItem->cchTextMax;
8907 TRACE("(nItem=%d, lpLVItem=%s, isW=%d)\n", nItem, debuglvitem_t(&lvItem, isW), isW);
8909 return LISTVIEW_SetItemT(infoPtr, &lvItem, isW);
8912 /***
8913 * DESCRIPTION:
8914 * Set item index that marks the start of a multiple selection.
8916 * PARAMETER(S):
8917 * [I] infoPtr : valid pointer to the listview structure
8918 * [I] nIndex : index
8920 * RETURN:
8921 * Index number or -1 if there is no selection mark.
8923 static INT LISTVIEW_SetSelectionMark(LISTVIEW_INFO *infoPtr, INT nIndex)
8925 INT nOldIndex = infoPtr->nSelectionMark;
8927 TRACE("(nIndex=%d)\n", nIndex);
8929 infoPtr->nSelectionMark = nIndex;
8931 return nOldIndex;
8934 /***
8935 * DESCRIPTION:
8936 * Sets the text background color.
8938 * PARAMETER(S):
8939 * [I] infoPtr : valid pointer to the listview structure
8940 * [I] color : text background color
8942 * RETURN:
8943 * SUCCESS : TRUE
8944 * FAILURE : FALSE
8946 static BOOL LISTVIEW_SetTextBkColor(LISTVIEW_INFO *infoPtr, COLORREF color)
8948 TRACE("(color=%x)\n", color);
8950 infoPtr->clrTextBk = color;
8951 return TRUE;
8954 /***
8955 * DESCRIPTION:
8956 * Sets the text foreground color.
8958 * PARAMETER(S):
8959 * [I] infoPtr : valid pointer to the listview structure
8960 * [I] color : text color
8962 * RETURN:
8963 * SUCCESS : TRUE
8964 * FAILURE : FALSE
8966 static BOOL LISTVIEW_SetTextColor (LISTVIEW_INFO *infoPtr, COLORREF color)
8968 TRACE("(color=%x)\n", color);
8970 infoPtr->clrText = color;
8971 return TRUE;
8974 /***
8975 * DESCRIPTION:
8976 * Sets new ToolTip window to ListView control.
8978 * PARAMETER(S):
8979 * [I] infoPtr : valid pointer to the listview structure
8980 * [I] hwndNewToolTip : handle to new ToolTip
8982 * RETURN:
8983 * old tool tip
8985 static HWND LISTVIEW_SetToolTips( LISTVIEW_INFO *infoPtr, HWND hwndNewToolTip)
8987 HWND hwndOldToolTip = infoPtr->hwndToolTip;
8988 infoPtr->hwndToolTip = hwndNewToolTip;
8989 return hwndOldToolTip;
8993 * DESCRIPTION:
8994 * sets the Unicode character format flag for the control
8995 * PARAMETER(S):
8996 * [I] infoPtr :valid pointer to the listview structure
8997 * [I] fUnicode :true to switch to UNICODE false to switch to ANSI
8999 * RETURN:
9000 * Old Unicode Format
9002 static BOOL LISTVIEW_SetUnicodeFormat( LISTVIEW_INFO *infoPtr, BOOL unicode)
9004 SHORT rc = infoPtr->notifyFormat;
9005 infoPtr->notifyFormat = (unicode) ? NFR_UNICODE : NFR_ANSI;
9006 return rc == NFR_UNICODE;
9010 * DESCRIPTION:
9011 * sets the control view mode
9012 * PARAMETER(S):
9013 * [I] infoPtr :valid pointer to the listview structure
9014 * [I] nView :new view mode value
9016 * RETURN:
9017 * SUCCESS: 1
9018 * FAILURE: -1
9020 static INT LISTVIEW_SetView(LISTVIEW_INFO *infoPtr, DWORD nView)
9022 SIZE oldIconSize = infoPtr->iconSize;
9023 HIMAGELIST himl;
9025 if (infoPtr->uView == nView) return 1;
9027 if ((INT)nView < 0 || nView > LV_VIEW_MAX) return -1;
9028 if (nView == LV_VIEW_TILE)
9030 FIXME("View LV_VIEW_TILE unimplemented\n");
9031 return -1;
9034 infoPtr->uView = nView;
9036 SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
9037 ShowWindow(infoPtr->hwndHeader, SW_HIDE);
9039 ShowScrollBar(infoPtr->hwndSelf, SB_BOTH, FALSE);
9040 SetRectEmpty(&infoPtr->rcFocus);
9042 himl = (nView == LV_VIEW_ICON ? infoPtr->himlNormal : infoPtr->himlSmall);
9043 set_icon_size(&infoPtr->iconSize, himl, nView != LV_VIEW_ICON);
9045 switch (nView)
9047 case LV_VIEW_ICON:
9048 if ((infoPtr->iconSize.cx != oldIconSize.cx) || (infoPtr->iconSize.cy != oldIconSize.cy))
9050 TRACE("icon old size=(%d,%d), new size=(%d,%d)\n",
9051 oldIconSize.cx, oldIconSize.cy, infoPtr->iconSize.cx, infoPtr->iconSize.cy);
9052 LISTVIEW_SetIconSpacing(infoPtr, 0, 0);
9054 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
9055 break;
9056 case LV_VIEW_SMALLICON:
9057 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
9058 break;
9059 case LV_VIEW_DETAILS:
9061 HDLAYOUT hl;
9062 WINDOWPOS wp;
9064 LISTVIEW_CreateHeader( infoPtr );
9066 hl.prc = &infoPtr->rcList;
9067 hl.pwpos = &wp;
9068 SendMessageW(infoPtr->hwndHeader, HDM_LAYOUT, 0, (LPARAM)&hl);
9069 SetWindowPos(infoPtr->hwndHeader, infoPtr->hwndSelf, wp.x, wp.y, wp.cx, wp.cy,
9070 wp.flags | ((infoPtr->dwStyle & LVS_NOCOLUMNHEADER) ? SWP_HIDEWINDOW : SWP_SHOWWINDOW));
9071 break;
9073 case LV_VIEW_LIST:
9074 break;
9077 LISTVIEW_UpdateItemSize(infoPtr);
9078 LISTVIEW_UpdateSize(infoPtr);
9079 LISTVIEW_UpdateScroll(infoPtr);
9080 LISTVIEW_InvalidateList(infoPtr);
9082 TRACE("nView=%d\n", nView);
9084 return 1;
9087 /* LISTVIEW_SetWorkAreas */
9089 /***
9090 * DESCRIPTION:
9091 * Callback internally used by LISTVIEW_SortItems() in response of LVM_SORTITEMS
9093 * PARAMETER(S):
9094 * [I] first : pointer to first ITEM_INFO to compare
9095 * [I] second : pointer to second ITEM_INFO to compare
9096 * [I] lParam : HWND of control
9098 * RETURN:
9099 * if first comes before second : negative
9100 * if first comes after second : positive
9101 * if first and second are equivalent : zero
9103 static INT WINAPI LISTVIEW_CallBackCompare(LPVOID first, LPVOID second, LPARAM lParam)
9105 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)lParam;
9106 ITEM_INFO* lv_first = DPA_GetPtr( first, 0 );
9107 ITEM_INFO* lv_second = DPA_GetPtr( second, 0 );
9109 /* Forward the call to the client defined callback */
9110 return (infoPtr->pfnCompare)( lv_first->lParam , lv_second->lParam, infoPtr->lParamSort );
9113 /***
9114 * DESCRIPTION:
9115 * Callback internally used by LISTVIEW_SortItems() in response of LVM_SORTITEMSEX
9117 * PARAMETER(S):
9118 * [I] first : pointer to first ITEM_INFO to compare
9119 * [I] second : pointer to second ITEM_INFO to compare
9120 * [I] lParam : HWND of control
9122 * RETURN:
9123 * if first comes before second : negative
9124 * if first comes after second : positive
9125 * if first and second are equivalent : zero
9127 static INT WINAPI LISTVIEW_CallBackCompareEx(LPVOID first, LPVOID second, LPARAM lParam)
9129 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)lParam;
9130 INT first_idx = DPA_GetPtrIndex( infoPtr->hdpaItems, first );
9131 INT second_idx = DPA_GetPtrIndex( infoPtr->hdpaItems, second );
9133 /* Forward the call to the client defined callback */
9134 return (infoPtr->pfnCompare)( first_idx, second_idx, infoPtr->lParamSort );
9137 /***
9138 * DESCRIPTION:
9139 * Sorts the listview items.
9141 * PARAMETER(S):
9142 * [I] infoPtr : valid pointer to the listview structure
9143 * [I] pfnCompare : application-defined value
9144 * [I] lParamSort : pointer to comparison callback
9145 * [I] IsEx : TRUE when LVM_SORTITEMSEX used
9147 * RETURN:
9148 * SUCCESS : TRUE
9149 * FAILURE : FALSE
9151 static BOOL LISTVIEW_SortItems(LISTVIEW_INFO *infoPtr, PFNLVCOMPARE pfnCompare,
9152 LPARAM lParamSort, BOOL IsEx)
9154 HDPA hdpaSubItems;
9155 ITEM_INFO *lpItem;
9156 LPVOID selectionMarkItem = NULL;
9157 LPVOID focusedItem = NULL;
9158 int i;
9160 TRACE("(pfnCompare=%p, lParamSort=%lx)\n", pfnCompare, lParamSort);
9162 if (infoPtr->dwStyle & LVS_OWNERDATA) return FALSE;
9164 if (!pfnCompare) return FALSE;
9165 if (!infoPtr->hdpaItems) return FALSE;
9167 /* if there are 0 or 1 items, there is no need to sort */
9168 if (infoPtr->nItemCount < 2) return TRUE;
9170 /* clear selection */
9171 ranges_clear(infoPtr->selectionRanges);
9173 /* save selection mark and focused item */
9174 if (infoPtr->nSelectionMark >= 0)
9175 selectionMarkItem = DPA_GetPtr(infoPtr->hdpaItems, infoPtr->nSelectionMark);
9176 if (infoPtr->nFocusedItem >= 0)
9177 focusedItem = DPA_GetPtr(infoPtr->hdpaItems, infoPtr->nFocusedItem);
9179 infoPtr->pfnCompare = pfnCompare;
9180 infoPtr->lParamSort = lParamSort;
9181 if (IsEx)
9182 DPA_Sort(infoPtr->hdpaItems, LISTVIEW_CallBackCompareEx, (LPARAM)infoPtr);
9183 else
9184 DPA_Sort(infoPtr->hdpaItems, LISTVIEW_CallBackCompare, (LPARAM)infoPtr);
9186 /* restore selection ranges */
9187 for (i=0; i < infoPtr->nItemCount; i++)
9189 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, i);
9190 lpItem = DPA_GetPtr(hdpaSubItems, 0);
9192 if (lpItem->state & LVIS_SELECTED)
9193 ranges_additem(infoPtr->selectionRanges, i);
9195 /* restore selection mark and focused item */
9196 infoPtr->nSelectionMark = DPA_GetPtrIndex(infoPtr->hdpaItems, selectionMarkItem);
9197 infoPtr->nFocusedItem = DPA_GetPtrIndex(infoPtr->hdpaItems, focusedItem);
9199 /* I believe nHotItem should be left alone, see LISTVIEW_ShiftIndices */
9201 /* refresh the display */
9202 LISTVIEW_InvalidateList(infoPtr);
9203 return TRUE;
9206 /***
9207 * DESCRIPTION:
9208 * Update theme handle after a theme change.
9210 * PARAMETER(S):
9211 * [I] infoPtr : valid pointer to the listview structure
9213 * RETURN:
9214 * SUCCESS : 0
9215 * FAILURE : something else
9217 static LRESULT LISTVIEW_ThemeChanged(const LISTVIEW_INFO *infoPtr)
9219 HTHEME theme = GetWindowTheme(infoPtr->hwndSelf);
9220 CloseThemeData(theme);
9221 OpenThemeData(infoPtr->hwndSelf, themeClass);
9222 return 0;
9225 /***
9226 * DESCRIPTION:
9227 * Updates an items or rearranges the listview control.
9229 * PARAMETER(S):
9230 * [I] infoPtr : valid pointer to the listview structure
9231 * [I] nItem : item index
9233 * RETURN:
9234 * SUCCESS : TRUE
9235 * FAILURE : FALSE
9237 static BOOL LISTVIEW_Update(LISTVIEW_INFO *infoPtr, INT nItem)
9239 TRACE("(nItem=%d)\n", nItem);
9241 if (nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
9243 /* rearrange with default alignment style */
9244 if (is_autoarrange(infoPtr))
9245 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
9246 else
9247 LISTVIEW_InvalidateItem(infoPtr, nItem);
9249 return TRUE;
9252 /***
9253 * DESCRIPTION:
9254 * Draw the track line at the place defined in the infoPtr structure.
9255 * The line is drawn with a XOR pen so drawing the line for the second time
9256 * in the same place erases the line.
9258 * PARAMETER(S):
9259 * [I] infoPtr : valid pointer to the listview structure
9261 * RETURN:
9262 * SUCCESS : TRUE
9263 * FAILURE : FALSE
9265 static BOOL LISTVIEW_DrawTrackLine(const LISTVIEW_INFO *infoPtr)
9267 HDC hdc;
9269 if (infoPtr->xTrackLine == -1)
9270 return FALSE;
9272 if (!(hdc = GetDC(infoPtr->hwndSelf)))
9273 return FALSE;
9274 PatBlt( hdc, infoPtr->xTrackLine, infoPtr->rcList.top,
9275 1, infoPtr->rcList.bottom - infoPtr->rcList.top, DSTINVERT );
9276 ReleaseDC(infoPtr->hwndSelf, hdc);
9277 return TRUE;
9280 /***
9281 * DESCRIPTION:
9282 * Called when an edit control should be displayed. This function is called after
9283 * we are sure that there was a single click - not a double click (this is a TIMERPROC).
9285 * PARAMETER(S):
9286 * [I] hwnd : Handle to the listview
9287 * [I] uMsg : WM_TIMER (ignored)
9288 * [I] idEvent : The timer ID interpreted as a pointer to a DELAYED_EDIT_ITEM struct
9289 * [I] dwTimer : The elapsed time (ignored)
9291 * RETURN:
9292 * None.
9294 static VOID CALLBACK LISTVIEW_DelayedEditItem(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
9296 DELAYED_ITEM_EDIT *editItem = (DELAYED_ITEM_EDIT *)idEvent;
9297 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongPtrW(hwnd, 0);
9299 KillTimer(hwnd, idEvent);
9300 editItem->fEnabled = FALSE;
9301 /* check if the item is still selected */
9302 if (infoPtr->bFocus && LISTVIEW_GetItemState(infoPtr, editItem->iItem, LVIS_SELECTED))
9303 LISTVIEW_EditLabelT(infoPtr, editItem->iItem, TRUE);
9306 /***
9307 * DESCRIPTION:
9308 * Creates the listview control - the WM_NCCREATE phase.
9310 * PARAMETER(S):
9311 * [I] hwnd : window handle
9312 * [I] lpcs : the create parameters
9314 * RETURN:
9315 * Success: TRUE
9316 * Failure: FALSE
9318 static LRESULT LISTVIEW_NCCreate(HWND hwnd, const CREATESTRUCTW *lpcs)
9320 LISTVIEW_INFO *infoPtr;
9321 LOGFONTW logFont;
9323 TRACE("(lpcs=%p)\n", lpcs);
9325 /* initialize info pointer */
9326 infoPtr = Alloc(sizeof(LISTVIEW_INFO));
9327 if (!infoPtr) return FALSE;
9329 SetWindowLongPtrW(hwnd, 0, (DWORD_PTR)infoPtr);
9331 infoPtr->hwndSelf = hwnd;
9332 infoPtr->dwStyle = lpcs->style; /* Note: may be changed in WM_CREATE */
9333 map_style_view(infoPtr);
9334 /* determine the type of structures to use */
9335 infoPtr->hwndNotify = lpcs->hwndParent;
9336 /* infoPtr->notifyFormat will be filled in WM_CREATE */
9338 /* initialize color information */
9339 infoPtr->clrBk = CLR_NONE;
9340 infoPtr->clrText = CLR_DEFAULT;
9341 infoPtr->clrTextBk = CLR_DEFAULT;
9342 LISTVIEW_SetBkColor(infoPtr, comctl32_color.clrWindow);
9344 /* set default values */
9345 infoPtr->nFocusedItem = -1;
9346 infoPtr->nSelectionMark = -1;
9347 infoPtr->nHotItem = -1;
9348 infoPtr->bRedraw = TRUE;
9349 infoPtr->bNoItemMetrics = TRUE;
9350 infoPtr->bDoChangeNotify = TRUE;
9351 infoPtr->iconSpacing.cx = GetSystemMetrics(SM_CXICONSPACING);
9352 infoPtr->iconSpacing.cy = GetSystemMetrics(SM_CYICONSPACING);
9353 infoPtr->nEditLabelItem = -1;
9354 infoPtr->nLButtonDownItem = -1;
9355 infoPtr->dwHoverTime = HOVER_DEFAULT; /* default system hover time */
9356 infoPtr->nMeasureItemHeight = 0;
9357 infoPtr->xTrackLine = -1; /* no track line */
9358 infoPtr->itemEdit.fEnabled = FALSE;
9359 infoPtr->iVersion = COMCTL32_VERSION;
9360 infoPtr->colRectsDirty = FALSE;
9362 /* get default font (icon title) */
9363 SystemParametersInfoW(SPI_GETICONTITLELOGFONT, 0, &logFont, 0);
9364 infoPtr->hDefaultFont = CreateFontIndirectW(&logFont);
9365 infoPtr->hFont = infoPtr->hDefaultFont;
9366 LISTVIEW_SaveTextMetrics(infoPtr);
9368 /* allocate memory for the data structure */
9369 if (!(infoPtr->selectionRanges = ranges_create(10))) goto fail;
9370 if (!(infoPtr->hdpaItems = DPA_Create(10))) goto fail;
9371 if (!(infoPtr->hdpaItemIds = DPA_Create(10))) goto fail;
9372 if (!(infoPtr->hdpaPosX = DPA_Create(10))) goto fail;
9373 if (!(infoPtr->hdpaPosY = DPA_Create(10))) goto fail;
9374 if (!(infoPtr->hdpaColumns = DPA_Create(10))) goto fail;
9375 return TRUE;
9377 fail:
9378 DestroyWindow(infoPtr->hwndHeader);
9379 ranges_destroy(infoPtr->selectionRanges);
9380 DPA_Destroy(infoPtr->hdpaItems);
9381 DPA_Destroy(infoPtr->hdpaItemIds);
9382 DPA_Destroy(infoPtr->hdpaPosX);
9383 DPA_Destroy(infoPtr->hdpaPosY);
9384 DPA_Destroy(infoPtr->hdpaColumns);
9385 Free(infoPtr);
9386 return FALSE;
9389 /***
9390 * DESCRIPTION:
9391 * Creates the listview control - the WM_CREATE phase. Most of the data is
9392 * already set up in LISTVIEW_NCCreate
9394 * PARAMETER(S):
9395 * [I] hwnd : window handle
9396 * [I] lpcs : the create parameters
9398 * RETURN:
9399 * Success: 0
9400 * Failure: -1
9402 static LRESULT LISTVIEW_Create(HWND hwnd, const CREATESTRUCTW *lpcs)
9404 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongPtrW(hwnd, 0);
9406 TRACE("(lpcs=%p, style=0x%08x)\n", lpcs, lpcs->style);
9408 infoPtr->dwStyle = lpcs->style;
9409 map_style_view(infoPtr);
9411 infoPtr->notifyFormat = SendMessageW(infoPtr->hwndNotify, WM_NOTIFYFORMAT,
9412 (WPARAM)infoPtr->hwndSelf, NF_QUERY);
9413 /* on error defaulting to ANSI notifications */
9414 if (infoPtr->notifyFormat == 0) infoPtr->notifyFormat = NFR_ANSI;
9415 TRACE("notify format=%d\n", infoPtr->notifyFormat);
9417 if ((infoPtr->uView == LV_VIEW_DETAILS) && (lpcs->style & WS_VISIBLE))
9419 if (LISTVIEW_CreateHeader(infoPtr) < 0) return -1;
9421 else
9422 infoPtr->hwndHeader = 0;
9424 /* init item size to avoid division by 0 */
9425 LISTVIEW_UpdateItemSize (infoPtr);
9427 if (infoPtr->uView == LV_VIEW_DETAILS)
9429 if (!(LVS_NOCOLUMNHEADER & lpcs->style) && (WS_VISIBLE & lpcs->style))
9431 ShowWindow(infoPtr->hwndHeader, SW_SHOWNORMAL);
9433 LISTVIEW_UpdateScroll(infoPtr);
9434 /* send WM_MEASUREITEM notification */
9435 if (infoPtr->dwStyle & LVS_OWNERDRAWFIXED) notify_measureitem(infoPtr);
9438 OpenThemeData(hwnd, themeClass);
9440 /* initialize the icon sizes */
9441 set_icon_size(&infoPtr->iconSize, infoPtr->himlNormal, infoPtr->uView != LV_VIEW_ICON);
9442 set_icon_size(&infoPtr->iconStateSize, infoPtr->himlState, TRUE);
9443 return 0;
9446 /***
9447 * DESCRIPTION:
9448 * Destroys the listview control.
9450 * PARAMETER(S):
9451 * [I] infoPtr : valid pointer to the listview structure
9453 * RETURN:
9454 * Success: 0
9455 * Failure: -1
9457 static LRESULT LISTVIEW_Destroy(LISTVIEW_INFO *infoPtr)
9459 HTHEME theme = GetWindowTheme(infoPtr->hwndSelf);
9460 CloseThemeData(theme);
9462 /* delete all items */
9463 LISTVIEW_DeleteAllItems(infoPtr, TRUE);
9465 return 0;
9468 /***
9469 * DESCRIPTION:
9470 * Enables the listview control.
9472 * PARAMETER(S):
9473 * [I] infoPtr : valid pointer to the listview structure
9474 * [I] bEnable : specifies whether to enable or disable the window
9476 * RETURN:
9477 * SUCCESS : TRUE
9478 * FAILURE : FALSE
9480 static BOOL LISTVIEW_Enable(const LISTVIEW_INFO *infoPtr)
9482 if (infoPtr->dwStyle & LVS_OWNERDRAWFIXED)
9483 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
9484 return TRUE;
9487 /***
9488 * DESCRIPTION:
9489 * Erases the background of the listview control.
9491 * PARAMETER(S):
9492 * [I] infoPtr : valid pointer to the listview structure
9493 * [I] hdc : device context handle
9495 * RETURN:
9496 * SUCCESS : TRUE
9497 * FAILURE : FALSE
9499 static inline BOOL LISTVIEW_EraseBkgnd(const LISTVIEW_INFO *infoPtr, HDC hdc)
9501 RECT rc;
9503 TRACE("(hdc=%p)\n", hdc);
9505 if (!GetClipBox(hdc, &rc)) return FALSE;
9507 if (infoPtr->clrBk == CLR_NONE)
9509 if (infoPtr->dwLvExStyle & LVS_EX_TRANSPARENTBKGND)
9510 return SendMessageW(infoPtr->hwndNotify, WM_PRINTCLIENT,
9511 (WPARAM)hdc, PRF_ERASEBKGND);
9512 else
9513 return SendMessageW(infoPtr->hwndNotify, WM_ERASEBKGND, (WPARAM)hdc, 0);
9516 /* for double buffered controls we need to do this during refresh */
9517 if (infoPtr->dwLvExStyle & LVS_EX_DOUBLEBUFFER) return FALSE;
9519 return LISTVIEW_FillBkgnd(infoPtr, hdc, &rc);
9523 /***
9524 * DESCRIPTION:
9525 * Helper function for LISTVIEW_[HV]Scroll *only*.
9526 * Performs vertical/horizontal scrolling by a give amount.
9528 * PARAMETER(S):
9529 * [I] infoPtr : valid pointer to the listview structure
9530 * [I] dx : amount of horizontal scroll
9531 * [I] dy : amount of vertical scroll
9533 static void scroll_list(LISTVIEW_INFO *infoPtr, INT dx, INT dy)
9535 /* now we can scroll the list */
9536 ScrollWindowEx(infoPtr->hwndSelf, dx, dy, &infoPtr->rcList,
9537 &infoPtr->rcList, 0, 0, SW_ERASE | SW_INVALIDATE);
9538 /* if we have focus, adjust rect */
9539 OffsetRect(&infoPtr->rcFocus, dx, dy);
9540 UpdateWindow(infoPtr->hwndSelf);
9543 /***
9544 * DESCRIPTION:
9545 * Performs vertical scrolling.
9547 * PARAMETER(S):
9548 * [I] infoPtr : valid pointer to the listview structure
9549 * [I] nScrollCode : scroll code
9550 * [I] nScrollDiff : units to scroll in SB_INTERNAL mode, 0 otherwise
9551 * [I] hScrollWnd : scrollbar control window handle
9553 * RETURN:
9554 * Zero
9556 * NOTES:
9557 * SB_LINEUP/SB_LINEDOWN:
9558 * for LVS_ICON, LVS_SMALLICON is 37 by experiment
9559 * for LVS_REPORT is 1 line
9560 * for LVS_LIST cannot occur
9563 static LRESULT LISTVIEW_VScroll(LISTVIEW_INFO *infoPtr, INT nScrollCode,
9564 INT nScrollDiff)
9566 INT nOldScrollPos, nNewScrollPos;
9567 SCROLLINFO scrollInfo;
9568 BOOL is_an_icon;
9570 TRACE("(nScrollCode=%d(%s), nScrollDiff=%d)\n", nScrollCode,
9571 debugscrollcode(nScrollCode), nScrollDiff);
9573 if (infoPtr->hwndEdit) SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
9575 scrollInfo.cbSize = sizeof(SCROLLINFO);
9576 scrollInfo.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_TRACKPOS;
9578 is_an_icon = ((infoPtr->uView == LV_VIEW_ICON) || (infoPtr->uView == LV_VIEW_SMALLICON));
9580 if (!GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo)) return 1;
9582 nOldScrollPos = scrollInfo.nPos;
9583 switch (nScrollCode)
9585 case SB_INTERNAL:
9586 break;
9588 case SB_LINEUP:
9589 nScrollDiff = (is_an_icon) ? -LISTVIEW_SCROLL_ICON_LINE_SIZE : -1;
9590 break;
9592 case SB_LINEDOWN:
9593 nScrollDiff = (is_an_icon) ? LISTVIEW_SCROLL_ICON_LINE_SIZE : 1;
9594 break;
9596 case SB_PAGEUP:
9597 nScrollDiff = -scrollInfo.nPage;
9598 break;
9600 case SB_PAGEDOWN:
9601 nScrollDiff = scrollInfo.nPage;
9602 break;
9604 case SB_THUMBPOSITION:
9605 case SB_THUMBTRACK:
9606 nScrollDiff = scrollInfo.nTrackPos - scrollInfo.nPos;
9607 break;
9609 default:
9610 nScrollDiff = 0;
9613 /* quit right away if pos isn't changing */
9614 if (nScrollDiff == 0) return 0;
9616 /* calculate new position, and handle overflows */
9617 nNewScrollPos = scrollInfo.nPos + nScrollDiff;
9618 if (nScrollDiff > 0) {
9619 if (nNewScrollPos < nOldScrollPos ||
9620 nNewScrollPos > scrollInfo.nMax)
9621 nNewScrollPos = scrollInfo.nMax;
9622 } else {
9623 if (nNewScrollPos > nOldScrollPos ||
9624 nNewScrollPos < scrollInfo.nMin)
9625 nNewScrollPos = scrollInfo.nMin;
9628 /* set the new position, and reread in case it changed */
9629 scrollInfo.fMask = SIF_POS;
9630 scrollInfo.nPos = nNewScrollPos;
9631 nNewScrollPos = SetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo, TRUE);
9633 /* carry on only if it really changed */
9634 if (nNewScrollPos == nOldScrollPos) return 0;
9636 /* now adjust to client coordinates */
9637 nScrollDiff = nOldScrollPos - nNewScrollPos;
9638 if (infoPtr->uView == LV_VIEW_DETAILS) nScrollDiff *= infoPtr->nItemHeight;
9640 /* and scroll the window */
9641 scroll_list(infoPtr, 0, nScrollDiff);
9643 return 0;
9646 /***
9647 * DESCRIPTION:
9648 * Performs horizontal scrolling.
9650 * PARAMETER(S):
9651 * [I] infoPtr : valid pointer to the listview structure
9652 * [I] nScrollCode : scroll code
9653 * [I] nScrollDiff : units to scroll in SB_INTERNAL mode, 0 otherwise
9654 * [I] hScrollWnd : scrollbar control window handle
9656 * RETURN:
9657 * Zero
9659 * NOTES:
9660 * SB_LINELEFT/SB_LINERIGHT:
9661 * for LVS_ICON, LVS_SMALLICON 1 pixel
9662 * for LVS_REPORT is 1 pixel
9663 * for LVS_LIST is 1 column --> which is a 1 because the
9664 * scroll is based on columns not pixels
9667 static LRESULT LISTVIEW_HScroll(LISTVIEW_INFO *infoPtr, INT nScrollCode,
9668 INT nScrollDiff)
9670 INT nOldScrollPos, nNewScrollPos;
9671 SCROLLINFO scrollInfo;
9672 BOOL is_an_icon;
9674 TRACE("(nScrollCode=%d(%s), nScrollDiff=%d)\n", nScrollCode,
9675 debugscrollcode(nScrollCode), nScrollDiff);
9677 if (infoPtr->hwndEdit) SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
9679 scrollInfo.cbSize = sizeof(SCROLLINFO);
9680 scrollInfo.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_TRACKPOS;
9682 is_an_icon = ((infoPtr->uView == LV_VIEW_ICON) || (infoPtr->uView == LV_VIEW_SMALLICON));
9684 if (!GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo)) return 1;
9686 nOldScrollPos = scrollInfo.nPos;
9688 switch (nScrollCode)
9690 case SB_INTERNAL:
9691 break;
9693 case SB_LINELEFT:
9694 nScrollDiff = (is_an_icon) ? -LISTVIEW_SCROLL_ICON_LINE_SIZE : -1;
9695 break;
9697 case SB_LINERIGHT:
9698 nScrollDiff = (is_an_icon) ? LISTVIEW_SCROLL_ICON_LINE_SIZE : 1;
9699 break;
9701 case SB_PAGELEFT:
9702 nScrollDiff = -scrollInfo.nPage;
9703 break;
9705 case SB_PAGERIGHT:
9706 nScrollDiff = scrollInfo.nPage;
9707 break;
9709 case SB_THUMBPOSITION:
9710 case SB_THUMBTRACK:
9711 nScrollDiff = scrollInfo.nTrackPos - scrollInfo.nPos;
9712 break;
9714 default:
9715 nScrollDiff = 0;
9718 /* quit right away if pos isn't changing */
9719 if (nScrollDiff == 0) return 0;
9721 /* calculate new position, and handle overflows */
9722 nNewScrollPos = scrollInfo.nPos + nScrollDiff;
9723 if (nScrollDiff > 0) {
9724 if (nNewScrollPos < nOldScrollPos ||
9725 nNewScrollPos > scrollInfo.nMax)
9726 nNewScrollPos = scrollInfo.nMax;
9727 } else {
9728 if (nNewScrollPos > nOldScrollPos ||
9729 nNewScrollPos < scrollInfo.nMin)
9730 nNewScrollPos = scrollInfo.nMin;
9733 /* set the new position, and reread in case it changed */
9734 scrollInfo.fMask = SIF_POS;
9735 scrollInfo.nPos = nNewScrollPos;
9736 nNewScrollPos = SetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo, TRUE);
9738 /* carry on only if it really changed */
9739 if (nNewScrollPos == nOldScrollPos) return 0;
9741 if (infoPtr->hwndHeader) LISTVIEW_UpdateHeaderSize(infoPtr, nNewScrollPos);
9743 /* now adjust to client coordinates */
9744 nScrollDiff = nOldScrollPos - nNewScrollPos;
9745 if (infoPtr->uView == LV_VIEW_LIST) nScrollDiff *= infoPtr->nItemWidth;
9747 /* and scroll the window */
9748 scroll_list(infoPtr, nScrollDiff, 0);
9750 return 0;
9753 static LRESULT LISTVIEW_MouseWheel(LISTVIEW_INFO *infoPtr, INT wheelDelta)
9755 INT gcWheelDelta = 0;
9756 INT pulScrollLines = 3;
9758 TRACE("(wheelDelta=%d)\n", wheelDelta);
9760 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
9761 gcWheelDelta -= wheelDelta;
9763 switch(infoPtr->uView)
9765 case LV_VIEW_ICON:
9766 case LV_VIEW_SMALLICON:
9768 * listview should be scrolled by a multiple of 37 dependently on its dimension or its visible item number
9769 * should be fixed in the future.
9771 LISTVIEW_VScroll(infoPtr, SB_INTERNAL, (gcWheelDelta < 0) ?
9772 -LISTVIEW_SCROLL_ICON_LINE_SIZE : LISTVIEW_SCROLL_ICON_LINE_SIZE);
9773 break;
9775 case LV_VIEW_DETAILS:
9776 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
9778 int cLineScroll = min(LISTVIEW_GetCountPerColumn(infoPtr), pulScrollLines);
9779 cLineScroll *= (gcWheelDelta / WHEEL_DELTA);
9780 LISTVIEW_VScroll(infoPtr, SB_INTERNAL, cLineScroll);
9782 break;
9784 case LV_VIEW_LIST:
9785 LISTVIEW_HScroll(infoPtr, (gcWheelDelta < 0) ? SB_LINELEFT : SB_LINERIGHT, 0);
9786 break;
9788 return 0;
9791 /***
9792 * DESCRIPTION:
9793 * ???
9795 * PARAMETER(S):
9796 * [I] infoPtr : valid pointer to the listview structure
9797 * [I] nVirtualKey : virtual key
9798 * [I] lKeyData : key data
9800 * RETURN:
9801 * Zero
9803 static LRESULT LISTVIEW_KeyDown(LISTVIEW_INFO *infoPtr, INT nVirtualKey, LONG lKeyData)
9805 HWND hwndSelf = infoPtr->hwndSelf;
9806 INT nItem = -1;
9807 NMLVKEYDOWN nmKeyDown;
9809 TRACE("(nVirtualKey=%d, lKeyData=%d)\n", nVirtualKey, lKeyData);
9811 /* send LVN_KEYDOWN notification */
9812 nmKeyDown.wVKey = nVirtualKey;
9813 nmKeyDown.flags = 0;
9814 notify_hdr(infoPtr, LVN_KEYDOWN, &nmKeyDown.hdr);
9815 if (!IsWindow(hwndSelf))
9816 return 0;
9818 switch (nVirtualKey)
9820 case VK_SPACE:
9821 nItem = infoPtr->nFocusedItem;
9822 if (infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES)
9823 toggle_checkbox_state(infoPtr, infoPtr->nFocusedItem);
9824 break;
9826 case VK_RETURN:
9827 if ((infoPtr->nItemCount > 0) && (infoPtr->nFocusedItem != -1))
9829 if (!notify(infoPtr, NM_RETURN)) return 0;
9830 if (!notify(infoPtr, LVN_ITEMACTIVATE)) return 0;
9832 break;
9834 case VK_HOME:
9835 if (infoPtr->nItemCount > 0)
9836 nItem = 0;
9837 break;
9839 case VK_END:
9840 if (infoPtr->nItemCount > 0)
9841 nItem = infoPtr->nItemCount - 1;
9842 break;
9844 case VK_LEFT:
9845 nItem = LISTVIEW_GetNextItem(infoPtr, infoPtr->nFocusedItem, LVNI_TOLEFT);
9846 break;
9848 case VK_UP:
9849 nItem = LISTVIEW_GetNextItem(infoPtr, infoPtr->nFocusedItem, LVNI_ABOVE);
9850 break;
9852 case VK_RIGHT:
9853 nItem = LISTVIEW_GetNextItem(infoPtr, infoPtr->nFocusedItem, LVNI_TORIGHT);
9854 break;
9856 case VK_DOWN:
9857 nItem = LISTVIEW_GetNextItem(infoPtr, infoPtr->nFocusedItem, LVNI_BELOW);
9858 break;
9860 case VK_PRIOR:
9861 if (infoPtr->uView == LV_VIEW_DETAILS)
9863 INT topidx = LISTVIEW_GetTopIndex(infoPtr);
9864 if (infoPtr->nFocusedItem == topidx)
9865 nItem = topidx - LISTVIEW_GetCountPerColumn(infoPtr) + 1;
9866 else
9867 nItem = topidx;
9869 else
9870 nItem = infoPtr->nFocusedItem - LISTVIEW_GetCountPerColumn(infoPtr)
9871 * LISTVIEW_GetCountPerRow(infoPtr);
9872 if(nItem < 0) nItem = 0;
9873 break;
9875 case VK_NEXT:
9876 if (infoPtr->uView == LV_VIEW_DETAILS)
9878 INT topidx = LISTVIEW_GetTopIndex(infoPtr);
9879 INT cnt = LISTVIEW_GetCountPerColumn(infoPtr);
9880 if (infoPtr->nFocusedItem == topidx + cnt - 1)
9881 nItem = infoPtr->nFocusedItem + cnt - 1;
9882 else
9883 nItem = topidx + cnt - 1;
9885 else
9886 nItem = infoPtr->nFocusedItem + LISTVIEW_GetCountPerColumn(infoPtr)
9887 * LISTVIEW_GetCountPerRow(infoPtr);
9888 if(nItem >= infoPtr->nItemCount) nItem = infoPtr->nItemCount - 1;
9889 break;
9892 if ((nItem != -1) && (nItem != infoPtr->nFocusedItem || nVirtualKey == VK_SPACE))
9893 LISTVIEW_KeySelection(infoPtr, nItem, nVirtualKey == VK_SPACE);
9895 return 0;
9898 /***
9899 * DESCRIPTION:
9900 * Kills the focus.
9902 * PARAMETER(S):
9903 * [I] infoPtr : valid pointer to the listview structure
9905 * RETURN:
9906 * Zero
9908 static LRESULT LISTVIEW_KillFocus(LISTVIEW_INFO *infoPtr)
9910 TRACE("()\n");
9912 /* if we did not have the focus, there's nothing to do */
9913 if (!infoPtr->bFocus) return 0;
9915 /* send NM_KILLFOCUS notification */
9916 if (!notify(infoPtr, NM_KILLFOCUS)) return 0;
9918 /* if we have a focus rectangle, get rid of it */
9919 LISTVIEW_ShowFocusRect(infoPtr, FALSE);
9921 /* if have a marquee selection, stop it */
9922 if (infoPtr->bMarqueeSelect)
9924 /* Remove the marquee rectangle and release our mouse capture */
9925 LISTVIEW_InvalidateRect(infoPtr, &infoPtr->marqueeRect);
9926 ReleaseCapture();
9928 SetRect(&infoPtr->marqueeRect, 0, 0, 0, 0);
9930 infoPtr->bMarqueeSelect = FALSE;
9931 infoPtr->bScrolling = FALSE;
9932 KillTimer(infoPtr->hwndSelf, (UINT_PTR) infoPtr);
9935 /* set window focus flag */
9936 infoPtr->bFocus = FALSE;
9938 /* invalidate the selected items before resetting focus flag */
9939 LISTVIEW_InvalidateSelectedItems(infoPtr);
9941 return 0;
9944 /***
9945 * DESCRIPTION:
9946 * Processes double click messages (left mouse button).
9948 * PARAMETER(S):
9949 * [I] infoPtr : valid pointer to the listview structure
9950 * [I] wKey : key flag
9951 * [I] x,y : mouse coordinate
9953 * RETURN:
9954 * Zero
9956 static LRESULT LISTVIEW_LButtonDblClk(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
9958 LVHITTESTINFO htInfo;
9960 TRACE("(key=%hu, X=%u, Y=%u)\n", wKey, x, y);
9962 /* Cancel the item edition if any */
9963 if (infoPtr->itemEdit.fEnabled)
9965 KillTimer(infoPtr->hwndSelf, (UINT_PTR)&infoPtr->itemEdit);
9966 infoPtr->itemEdit.fEnabled = FALSE;
9969 /* send NM_RELEASEDCAPTURE notification */
9970 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
9972 htInfo.pt.x = x;
9973 htInfo.pt.y = y;
9975 /* send NM_DBLCLK notification */
9976 LISTVIEW_HitTest(infoPtr, &htInfo, TRUE, FALSE);
9977 if (!notify_click(infoPtr, NM_DBLCLK, &htInfo)) return 0;
9979 /* To send the LVN_ITEMACTIVATE, it must be on an Item */
9980 if(htInfo.iItem != -1) notify_itemactivate(infoPtr,&htInfo);
9982 return 0;
9985 /***
9986 * DESCRIPTION:
9987 * Processes mouse down messages (left mouse button).
9989 * PARAMETERS:
9990 * infoPtr [I ] valid pointer to the listview structure
9991 * wKey [I ] key flag
9992 * x,y [I ] mouse coordinate
9994 * RETURN:
9995 * Zero
9997 static LRESULT LISTVIEW_LButtonDown(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
9999 LVHITTESTINFO lvHitTestInfo;
10000 static BOOL bGroupSelect = TRUE;
10001 POINT pt = { x, y };
10002 INT nItem;
10004 TRACE("(key=%hu, X=%u, Y=%u)\n", wKey, x, y);
10006 /* send NM_RELEASEDCAPTURE notification */
10007 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
10009 /* set left button down flag and record the click position */
10010 infoPtr->bLButtonDown = TRUE;
10011 infoPtr->ptClickPos = pt;
10012 infoPtr->bDragging = FALSE;
10013 infoPtr->bMarqueeSelect = FALSE;
10014 infoPtr->bScrolling = FALSE;
10016 lvHitTestInfo.pt.x = x;
10017 lvHitTestInfo.pt.y = y;
10019 nItem = LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, TRUE);
10020 TRACE("at %s, nItem=%d\n", wine_dbgstr_point(&pt), nItem);
10021 if ((nItem >= 0) && (nItem < infoPtr->nItemCount))
10023 if ((infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES) && (lvHitTestInfo.flags & LVHT_ONITEMSTATEICON))
10025 toggle_checkbox_state(infoPtr, nItem);
10026 return 0;
10029 if (infoPtr->dwStyle & LVS_SINGLESEL)
10031 if (LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED))
10032 infoPtr->nEditLabelItem = nItem;
10033 else
10034 LISTVIEW_SetSelection(infoPtr, nItem);
10036 else
10038 if ((wKey & MK_CONTROL) && (wKey & MK_SHIFT))
10040 if (bGroupSelect)
10042 if (!LISTVIEW_AddGroupSelection(infoPtr, nItem)) return 0;
10043 LISTVIEW_SetItemFocus(infoPtr, nItem);
10044 infoPtr->nSelectionMark = nItem;
10046 else
10048 LVITEMW item;
10050 item.state = LVIS_SELECTED | LVIS_FOCUSED;
10051 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
10053 LISTVIEW_SetItemState(infoPtr,nItem,&item);
10054 infoPtr->nSelectionMark = nItem;
10057 else if (wKey & MK_CONTROL)
10059 LVITEMW item;
10061 bGroupSelect = (LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED) == 0);
10063 item.state = (bGroupSelect ? LVIS_SELECTED : 0) | LVIS_FOCUSED;
10064 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
10065 LISTVIEW_SetItemState(infoPtr, nItem, &item);
10066 infoPtr->nSelectionMark = nItem;
10068 else if (wKey & MK_SHIFT)
10070 LISTVIEW_SetGroupSelection(infoPtr, nItem);
10072 else
10074 if (LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED))
10076 infoPtr->nEditLabelItem = nItem;
10077 infoPtr->nLButtonDownItem = nItem;
10079 LISTVIEW_SetItemFocus(infoPtr, nItem);
10081 else
10082 /* set selection (clears other pre-existing selections) */
10083 LISTVIEW_SetSelection(infoPtr, nItem);
10087 if (infoPtr->dwLvExStyle & LVS_EX_ONECLICKACTIVATE)
10088 if(lvHitTestInfo.iItem != -1) notify_itemactivate(infoPtr,&lvHitTestInfo);
10090 else
10092 if (!infoPtr->bFocus)
10093 SetFocus(infoPtr->hwndSelf);
10095 /* remove all selections */
10096 if (!(wKey & MK_CONTROL) && !(wKey & MK_SHIFT))
10097 LISTVIEW_DeselectAll(infoPtr);
10098 ReleaseCapture();
10101 return 0;
10104 /***
10105 * DESCRIPTION:
10106 * Processes mouse up messages (left mouse button).
10108 * PARAMETERS:
10109 * infoPtr [I ] valid pointer to the listview structure
10110 * wKey [I ] key flag
10111 * x,y [I ] mouse coordinate
10113 * RETURN:
10114 * Zero
10116 static LRESULT LISTVIEW_LButtonUp(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
10118 LVHITTESTINFO lvHitTestInfo;
10120 TRACE("(key=%hu, X=%u, Y=%u)\n", wKey, x, y);
10122 if (!infoPtr->bLButtonDown) return 0;
10124 lvHitTestInfo.pt.x = x;
10125 lvHitTestInfo.pt.y = y;
10127 /* send NM_CLICK notification */
10128 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
10129 if (!notify_click(infoPtr, NM_CLICK, &lvHitTestInfo)) return 0;
10131 /* set left button flag */
10132 infoPtr->bLButtonDown = FALSE;
10134 /* set a single selection, reset others */
10135 if(lvHitTestInfo.iItem == infoPtr->nLButtonDownItem && lvHitTestInfo.iItem != -1)
10136 LISTVIEW_SetSelection(infoPtr, infoPtr->nLButtonDownItem);
10137 infoPtr->nLButtonDownItem = -1;
10139 if (infoPtr->bDragging || infoPtr->bMarqueeSelect)
10141 /* Remove the marquee rectangle and release our mouse capture */
10142 if (infoPtr->bMarqueeSelect)
10144 LISTVIEW_InvalidateRect(infoPtr, &infoPtr->marqueeDrawRect);
10145 ReleaseCapture();
10148 SetRect(&infoPtr->marqueeRect, 0, 0, 0, 0);
10149 SetRect(&infoPtr->marqueeDrawRect, 0, 0, 0, 0);
10151 infoPtr->bDragging = FALSE;
10152 infoPtr->bMarqueeSelect = FALSE;
10153 infoPtr->bScrolling = FALSE;
10155 KillTimer(infoPtr->hwndSelf, (UINT_PTR) infoPtr);
10156 return 0;
10159 /* if we clicked on a selected item, edit the label */
10160 if(lvHitTestInfo.iItem == infoPtr->nEditLabelItem && (lvHitTestInfo.flags & LVHT_ONITEMLABEL))
10162 /* we want to make sure the user doesn't want to do a double click. So we will
10163 * delay the edit. WM_LBUTTONDBLCLICK will cancel the timer
10165 infoPtr->itemEdit.fEnabled = TRUE;
10166 infoPtr->itemEdit.iItem = lvHitTestInfo.iItem;
10167 SetTimer(infoPtr->hwndSelf,
10168 (UINT_PTR)&infoPtr->itemEdit,
10169 GetDoubleClickTime(),
10170 LISTVIEW_DelayedEditItem);
10173 if (!infoPtr->bFocus)
10174 SetFocus(infoPtr->hwndSelf);
10176 return 0;
10179 /***
10180 * DESCRIPTION:
10181 * Destroys the listview control (called after WM_DESTROY).
10183 * PARAMETER(S):
10184 * [I] infoPtr : valid pointer to the listview structure
10186 * RETURN:
10187 * Zero
10189 static LRESULT LISTVIEW_NCDestroy(LISTVIEW_INFO *infoPtr)
10191 INT i;
10193 TRACE("()\n");
10195 /* destroy data structure */
10196 DPA_Destroy(infoPtr->hdpaItems);
10197 DPA_Destroy(infoPtr->hdpaItemIds);
10198 DPA_Destroy(infoPtr->hdpaPosX);
10199 DPA_Destroy(infoPtr->hdpaPosY);
10200 /* columns */
10201 for (i = 0; i < DPA_GetPtrCount(infoPtr->hdpaColumns); i++)
10202 Free(DPA_GetPtr(infoPtr->hdpaColumns, i));
10203 DPA_Destroy(infoPtr->hdpaColumns);
10204 ranges_destroy(infoPtr->selectionRanges);
10206 /* destroy image lists */
10207 if (!(infoPtr->dwStyle & LVS_SHAREIMAGELISTS))
10209 ImageList_Destroy(infoPtr->himlNormal);
10210 ImageList_Destroy(infoPtr->himlSmall);
10211 ImageList_Destroy(infoPtr->himlState);
10214 /* destroy font, bkgnd brush */
10215 infoPtr->hFont = 0;
10216 if (infoPtr->hDefaultFont) DeleteObject(infoPtr->hDefaultFont);
10217 if (infoPtr->clrBk != CLR_NONE) DeleteObject(infoPtr->hBkBrush);
10219 SetWindowLongPtrW(infoPtr->hwndSelf, 0, 0);
10221 /* free listview info pointer*/
10222 Free(infoPtr);
10224 return 0;
10227 /***
10228 * DESCRIPTION:
10229 * Handles notifications.
10231 * PARAMETER(S):
10232 * [I] infoPtr : valid pointer to the listview structure
10233 * [I] lpnmhdr : notification information
10235 * RETURN:
10236 * Zero
10238 static LRESULT LISTVIEW_Notify(LISTVIEW_INFO *infoPtr, const NMHDR *lpnmhdr)
10240 const NMHEADERW *lpnmh;
10242 TRACE("(lpnmhdr=%p)\n", lpnmhdr);
10244 if (!lpnmhdr || lpnmhdr->hwndFrom != infoPtr->hwndHeader) return 0;
10246 /* remember: HDN_LAST < HDN_FIRST */
10247 if (lpnmhdr->code > HDN_FIRST || lpnmhdr->code < HDN_LAST) return 0;
10248 lpnmh = (const NMHEADERW *)lpnmhdr;
10250 if (lpnmh->iItem < 0 || lpnmh->iItem >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return 0;
10252 switch (lpnmhdr->code)
10254 case HDN_TRACKW:
10255 case HDN_TRACKA:
10257 COLUMN_INFO *lpColumnInfo;
10258 POINT ptOrigin;
10259 INT x;
10261 if (!lpnmh->pitem || !(lpnmh->pitem->mask & HDI_WIDTH))
10262 break;
10264 /* remove the old line (if any) */
10265 LISTVIEW_DrawTrackLine(infoPtr);
10267 /* compute & draw the new line */
10268 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, lpnmh->iItem);
10269 x = lpColumnInfo->rcHeader.left + lpnmh->pitem->cxy;
10270 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
10271 infoPtr->xTrackLine = x + ptOrigin.x;
10272 LISTVIEW_DrawTrackLine(infoPtr);
10273 break;
10276 case HDN_ENDTRACKA:
10277 case HDN_ENDTRACKW:
10278 /* remove the track line (if any) */
10279 LISTVIEW_DrawTrackLine(infoPtr);
10280 infoPtr->xTrackLine = -1;
10281 break;
10283 case HDN_BEGINDRAG:
10284 notify_forward_header(infoPtr, lpnmh);
10285 return (infoPtr->dwLvExStyle & LVS_EX_HEADERDRAGDROP) == 0;
10287 case HDN_ENDDRAG:
10288 infoPtr->colRectsDirty = TRUE;
10289 LISTVIEW_InvalidateList(infoPtr);
10290 notify_forward_header(infoPtr, lpnmh);
10291 return FALSE;
10293 case HDN_ITEMCHANGEDW:
10294 case HDN_ITEMCHANGEDA:
10296 COLUMN_INFO *lpColumnInfo;
10297 HDITEMW hdi;
10298 INT dx, cxy;
10300 if (!lpnmh->pitem || !(lpnmh->pitem->mask & HDI_WIDTH))
10302 hdi.mask = HDI_WIDTH;
10303 if (!SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, lpnmh->iItem, (LPARAM)&hdi)) return 0;
10304 cxy = hdi.cxy;
10306 else
10307 cxy = lpnmh->pitem->cxy;
10309 /* determine how much we change since the last know position */
10310 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, lpnmh->iItem);
10311 dx = cxy - (lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left);
10312 if (dx != 0)
10314 lpColumnInfo->rcHeader.right += dx;
10316 hdi.mask = HDI_ORDER;
10317 SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, lpnmh->iItem, (LPARAM)&hdi);
10319 /* not the rightmost one */
10320 if (hdi.iOrder + 1 < DPA_GetPtrCount(infoPtr->hdpaColumns))
10322 INT nIndex = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX,
10323 hdi.iOrder + 1, 0);
10324 LISTVIEW_ScrollColumns(infoPtr, nIndex, dx);
10326 else
10328 /* only needs to update the scrolls */
10329 infoPtr->nItemWidth += dx;
10330 LISTVIEW_UpdateScroll(infoPtr);
10332 LISTVIEW_UpdateItemSize(infoPtr);
10333 if (infoPtr->uView == LV_VIEW_DETAILS && is_redrawing(infoPtr))
10335 POINT ptOrigin;
10336 RECT rcCol = lpColumnInfo->rcHeader;
10338 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
10339 OffsetRect(&rcCol, ptOrigin.x, 0);
10341 rcCol.top = infoPtr->rcList.top;
10342 rcCol.bottom = infoPtr->rcList.bottom;
10344 /* resizing left-aligned columns leaves most of the left side untouched */
10345 if ((lpColumnInfo->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_LEFT)
10347 INT nMaxDirty = infoPtr->nEllipsisWidth + infoPtr->ntmMaxCharWidth;
10348 if (dx > 0)
10349 nMaxDirty += dx;
10350 rcCol.left = max (rcCol.left, rcCol.right - nMaxDirty);
10353 /* when shrinking the last column clear the now unused field */
10354 if (hdi.iOrder == DPA_GetPtrCount(infoPtr->hdpaColumns) - 1)
10356 RECT right;
10358 rcCol.right -= dx;
10360 /* deal with right from rightmost column area */
10361 right.left = rcCol.right;
10362 right.top = rcCol.top;
10363 right.bottom = rcCol.bottom;
10364 right.right = infoPtr->rcList.right;
10366 LISTVIEW_InvalidateRect(infoPtr, &right);
10369 LISTVIEW_InvalidateRect(infoPtr, &rcCol);
10373 break;
10375 case HDN_ITEMCLICKW:
10376 case HDN_ITEMCLICKA:
10378 /* Handle sorting by Header Column */
10379 NMLISTVIEW nmlv;
10381 ZeroMemory(&nmlv, sizeof(NMLISTVIEW));
10382 nmlv.iItem = -1;
10383 nmlv.iSubItem = lpnmh->iItem;
10384 notify_listview(infoPtr, LVN_COLUMNCLICK, &nmlv);
10385 notify_forward_header(infoPtr, lpnmh);
10387 break;
10389 case HDN_DIVIDERDBLCLICKW:
10390 case HDN_DIVIDERDBLCLICKA:
10391 /* FIXME: for LVS_EX_HEADERINALLVIEWS and not LV_VIEW_DETAILS
10392 we should use LVSCW_AUTOSIZE_USEHEADER, helper rework or
10393 split needed for that */
10394 LISTVIEW_SetColumnWidth(infoPtr, lpnmh->iItem, LVSCW_AUTOSIZE);
10395 notify_forward_header(infoPtr, lpnmh);
10396 break;
10399 return 0;
10402 /***
10403 * DESCRIPTION:
10404 * Paint non-client area of control.
10406 * PARAMETER(S):
10407 * [I] infoPtr : valid pointer to the listview structureof the sender
10408 * [I] region : update region
10410 * RETURN:
10411 * TRUE - frame was painted
10412 * FALSE - call default window proc
10414 static BOOL LISTVIEW_NCPaint(const LISTVIEW_INFO *infoPtr, HRGN region)
10416 HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
10417 HDC dc;
10418 RECT r;
10419 HRGN cliprgn;
10420 int cxEdge = GetSystemMetrics (SM_CXEDGE),
10421 cyEdge = GetSystemMetrics (SM_CYEDGE);
10423 if (!theme)
10424 return DefWindowProcW (infoPtr->hwndSelf, WM_NCPAINT, (WPARAM)region, 0);
10426 GetWindowRect(infoPtr->hwndSelf, &r);
10428 cliprgn = CreateRectRgn (r.left + cxEdge, r.top + cyEdge,
10429 r.right - cxEdge, r.bottom - cyEdge);
10430 if (region != (HRGN)1)
10431 CombineRgn (cliprgn, cliprgn, region, RGN_AND);
10432 OffsetRect(&r, -r.left, -r.top);
10434 dc = GetDCEx(infoPtr->hwndSelf, region, DCX_WINDOW|DCX_INTERSECTRGN);
10435 OffsetRect(&r, -r.left, -r.top);
10437 if (IsThemeBackgroundPartiallyTransparent (theme, 0, 0))
10438 DrawThemeParentBackground(infoPtr->hwndSelf, dc, &r);
10439 DrawThemeBackground (theme, dc, 0, 0, &r, 0);
10440 ReleaseDC(infoPtr->hwndSelf, dc);
10442 /* Call default proc to get the scrollbars etc. painted */
10443 DefWindowProcW (infoPtr->hwndSelf, WM_NCPAINT, (WPARAM)cliprgn, 0);
10445 return FALSE;
10448 /***
10449 * DESCRIPTION:
10450 * Determines the type of structure to use.
10452 * PARAMETER(S):
10453 * [I] infoPtr : valid pointer to the listview structureof the sender
10454 * [I] hwndFrom : listview window handle
10455 * [I] nCommand : command specifying the nature of the WM_NOTIFYFORMAT
10457 * RETURN:
10458 * Zero
10460 static LRESULT LISTVIEW_NotifyFormat(LISTVIEW_INFO *infoPtr, HWND hwndFrom, INT nCommand)
10462 TRACE("(hwndFrom=%p, nCommand=%d)\n", hwndFrom, nCommand);
10464 if (nCommand == NF_REQUERY)
10465 infoPtr->notifyFormat = SendMessageW(infoPtr->hwndNotify, WM_NOTIFYFORMAT, (WPARAM)infoPtr->hwndSelf, NF_QUERY);
10467 return infoPtr->notifyFormat;
10470 /***
10471 * DESCRIPTION:
10472 * Paints/Repaints the listview control. Internal use.
10474 * PARAMETER(S):
10475 * [I] infoPtr : valid pointer to the listview structure
10476 * [I] hdc : device context handle
10478 * RETURN:
10479 * Zero
10481 static LRESULT LISTVIEW_Paint(LISTVIEW_INFO *infoPtr, HDC hdc)
10483 TRACE("(hdc=%p)\n", hdc);
10485 if (infoPtr->bNoItemMetrics && infoPtr->nItemCount)
10487 infoPtr->bNoItemMetrics = FALSE;
10488 LISTVIEW_UpdateItemSize(infoPtr);
10489 if (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON)
10490 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
10491 LISTVIEW_UpdateScroll(infoPtr);
10494 if (infoPtr->hwndHeader) UpdateWindow(infoPtr->hwndHeader);
10496 if (hdc)
10497 LISTVIEW_Refresh(infoPtr, hdc, NULL);
10498 else
10500 PAINTSTRUCT ps;
10502 hdc = BeginPaint(infoPtr->hwndSelf, &ps);
10503 if (!hdc) return 1;
10504 LISTVIEW_Refresh(infoPtr, hdc, ps.fErase ? &ps.rcPaint : NULL);
10505 EndPaint(infoPtr->hwndSelf, &ps);
10508 return 0;
10511 /***
10512 * DESCRIPTION:
10513 * Paints/Repaints the listview control, WM_PAINT handler.
10515 * PARAMETER(S):
10516 * [I] infoPtr : valid pointer to the listview structure
10517 * [I] hdc : device context handle
10519 * RETURN:
10520 * Zero
10522 static inline LRESULT LISTVIEW_WMPaint(LISTVIEW_INFO *infoPtr, HDC hdc)
10524 TRACE("(hdc=%p)\n", hdc);
10526 if (!is_redrawing(infoPtr))
10527 return DefWindowProcW (infoPtr->hwndSelf, WM_PAINT, (WPARAM)hdc, 0);
10529 return LISTVIEW_Paint(infoPtr, hdc);
10532 /***
10533 * DESCRIPTION:
10534 * Paints/Repaints the listview control.
10536 * PARAMETER(S):
10537 * [I] infoPtr : valid pointer to the listview structure
10538 * [I] hdc : device context handle
10539 * [I] options : drawing options
10541 * RETURN:
10542 * Zero
10544 static LRESULT LISTVIEW_PrintClient(LISTVIEW_INFO *infoPtr, HDC hdc, DWORD options)
10546 FIXME("Partial Stub: (hdc=%p options=0x%08x)\n", hdc, options);
10548 if ((options & PRF_CHECKVISIBLE) && !IsWindowVisible(infoPtr->hwndSelf))
10549 return 0;
10551 if (options & PRF_ERASEBKGND)
10552 LISTVIEW_EraseBkgnd(infoPtr, hdc);
10554 if (options & PRF_CLIENT)
10555 LISTVIEW_Paint(infoPtr, hdc);
10557 return 0;
10561 /***
10562 * DESCRIPTION:
10563 * Processes double click messages (right mouse button).
10565 * PARAMETER(S):
10566 * [I] infoPtr : valid pointer to the listview structure
10567 * [I] wKey : key flag
10568 * [I] x,y : mouse coordinate
10570 * RETURN:
10571 * Zero
10573 static LRESULT LISTVIEW_RButtonDblClk(const LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
10575 LVHITTESTINFO lvHitTestInfo;
10577 TRACE("(key=%hu,X=%u,Y=%u)\n", wKey, x, y);
10579 /* send NM_RELEASEDCAPTURE notification */
10580 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
10582 /* send NM_RDBLCLK notification */
10583 lvHitTestInfo.pt.x = x;
10584 lvHitTestInfo.pt.y = y;
10585 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
10586 notify_click(infoPtr, NM_RDBLCLK, &lvHitTestInfo);
10588 return 0;
10591 /***
10592 * DESCRIPTION:
10593 * Processes mouse down messages (right mouse button).
10595 * PARAMETER(S):
10596 * [I] infoPtr : valid pointer to the listview structure
10597 * [I] wKey : key flag
10598 * [I] x,y : mouse coordinate
10600 * RETURN:
10601 * Zero
10603 static LRESULT LISTVIEW_RButtonDown(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
10605 LVHITTESTINFO lvHitTestInfo;
10606 INT nItem;
10608 TRACE("(key=%hu,X=%u,Y=%u)\n", wKey, x, y);
10610 /* send NM_RELEASEDCAPTURE notification */
10611 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
10613 /* make sure the listview control window has the focus */
10614 if (!infoPtr->bFocus) SetFocus(infoPtr->hwndSelf);
10616 /* set right button down flag */
10617 infoPtr->bRButtonDown = TRUE;
10619 /* determine the index of the selected item */
10620 lvHitTestInfo.pt.x = x;
10621 lvHitTestInfo.pt.y = y;
10622 nItem = LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, TRUE);
10624 if ((nItem >= 0) && (nItem < infoPtr->nItemCount))
10626 LISTVIEW_SetItemFocus(infoPtr, nItem);
10627 if (!((wKey & MK_SHIFT) || (wKey & MK_CONTROL)) &&
10628 !LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED))
10629 LISTVIEW_SetSelection(infoPtr, nItem);
10631 else
10633 LISTVIEW_DeselectAll(infoPtr);
10636 return 0;
10639 /***
10640 * DESCRIPTION:
10641 * Processes mouse up messages (right mouse button).
10643 * PARAMETER(S):
10644 * [I] infoPtr : valid pointer to the listview structure
10645 * [I] wKey : key flag
10646 * [I] x,y : mouse coordinate
10648 * RETURN:
10649 * Zero
10651 static LRESULT LISTVIEW_RButtonUp(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
10653 LVHITTESTINFO lvHitTestInfo;
10654 POINT pt;
10656 TRACE("(key=%hu,X=%u,Y=%u)\n", wKey, x, y);
10658 if (!infoPtr->bRButtonDown) return 0;
10660 /* set button flag */
10661 infoPtr->bRButtonDown = FALSE;
10663 /* Send NM_RCLICK notification */
10664 lvHitTestInfo.pt.x = x;
10665 lvHitTestInfo.pt.y = y;
10666 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
10667 if (!notify_click(infoPtr, NM_RCLICK, &lvHitTestInfo)) return 0;
10669 /* Change to screen coordinate for WM_CONTEXTMENU */
10670 pt = lvHitTestInfo.pt;
10671 ClientToScreen(infoPtr->hwndSelf, &pt);
10673 /* Send a WM_CONTEXTMENU message in response to the RBUTTONUP */
10674 SendMessageW(infoPtr->hwndSelf, WM_CONTEXTMENU,
10675 (WPARAM)infoPtr->hwndSelf, MAKELPARAM(pt.x, pt.y));
10677 return 0;
10681 /***
10682 * DESCRIPTION:
10683 * Sets the cursor.
10685 * PARAMETER(S):
10686 * [I] infoPtr : valid pointer to the listview structure
10687 * [I] hwnd : window handle of window containing the cursor
10688 * [I] nHittest : hit-test code
10689 * [I] wMouseMsg : ideintifier of the mouse message
10691 * RETURN:
10692 * TRUE if cursor is set
10693 * FALSE otherwise
10695 static BOOL LISTVIEW_SetCursor(const LISTVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
10697 LVHITTESTINFO lvHitTestInfo;
10699 if (!LISTVIEW_IsHotTracking(infoPtr)) goto forward;
10701 if (!infoPtr->hHotCursor) goto forward;
10703 GetCursorPos(&lvHitTestInfo.pt);
10704 if (LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, FALSE, FALSE) < 0) goto forward;
10706 SetCursor(infoPtr->hHotCursor);
10708 return TRUE;
10710 forward:
10712 return DefWindowProcW(infoPtr->hwndSelf, WM_SETCURSOR, wParam, lParam);
10715 /***
10716 * DESCRIPTION:
10717 * Sets the focus.
10719 * PARAMETER(S):
10720 * [I] infoPtr : valid pointer to the listview structure
10721 * [I] hwndLoseFocus : handle of previously focused window
10723 * RETURN:
10724 * Zero
10726 static LRESULT LISTVIEW_SetFocus(LISTVIEW_INFO *infoPtr, HWND hwndLoseFocus)
10728 TRACE("(hwndLoseFocus=%p)\n", hwndLoseFocus);
10730 /* if we have the focus already, there's nothing to do */
10731 if (infoPtr->bFocus) return 0;
10733 /* send NM_SETFOCUS notification */
10734 if (!notify(infoPtr, NM_SETFOCUS)) return 0;
10736 /* set window focus flag */
10737 infoPtr->bFocus = TRUE;
10739 /* put the focus rect back on */
10740 LISTVIEW_ShowFocusRect(infoPtr, TRUE);
10742 /* redraw all visible selected items */
10743 LISTVIEW_InvalidateSelectedItems(infoPtr);
10745 return 0;
10748 /***
10749 * DESCRIPTION:
10750 * Sets the font.
10752 * PARAMETER(S):
10753 * [I] infoPtr : valid pointer to the listview structure
10754 * [I] fRedraw : font handle
10755 * [I] fRedraw : redraw flag
10757 * RETURN:
10758 * Zero
10760 static LRESULT LISTVIEW_SetFont(LISTVIEW_INFO *infoPtr, HFONT hFont, WORD fRedraw)
10762 HFONT oldFont = infoPtr->hFont;
10764 TRACE("(hfont=%p,redraw=%hu)\n", hFont, fRedraw);
10766 infoPtr->hFont = hFont ? hFont : infoPtr->hDefaultFont;
10767 if (infoPtr->hFont == oldFont) return 0;
10769 LISTVIEW_SaveTextMetrics(infoPtr);
10771 if (infoPtr->uView == LV_VIEW_DETAILS)
10773 SendMessageW(infoPtr->hwndHeader, WM_SETFONT, (WPARAM)hFont, MAKELPARAM(fRedraw, 0));
10774 LISTVIEW_UpdateSize(infoPtr);
10775 LISTVIEW_UpdateScroll(infoPtr);
10778 if (fRedraw) LISTVIEW_InvalidateList(infoPtr);
10780 return 0;
10783 /***
10784 * DESCRIPTION:
10785 * Message handling for WM_SETREDRAW.
10786 * For the Listview, it invalidates the entire window (the doc specifies otherwise)
10788 * PARAMETER(S):
10789 * [I] infoPtr : valid pointer to the listview structure
10790 * [I] bRedraw: state of redraw flag
10792 * RETURN:
10793 * DefWinProc return value
10795 static LRESULT LISTVIEW_SetRedraw(LISTVIEW_INFO *infoPtr, BOOL bRedraw)
10797 TRACE("infoPtr->bRedraw=%d, bRedraw=%d\n", infoPtr->bRedraw, bRedraw);
10799 /* we cannot use straight equality here because _any_ non-zero value is TRUE */
10800 if ((infoPtr->bRedraw && bRedraw) || (!infoPtr->bRedraw && !bRedraw)) return 0;
10802 infoPtr->bRedraw = bRedraw;
10804 if(!bRedraw) return 0;
10806 if (is_autoarrange(infoPtr))
10807 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
10808 LISTVIEW_UpdateScroll(infoPtr);
10810 /* despite what the WM_SETREDRAW docs says, apps expect us
10811 * to invalidate the listview here... stupid! */
10812 LISTVIEW_InvalidateList(infoPtr);
10814 return 0;
10817 /***
10818 * DESCRIPTION:
10819 * Resizes the listview control. This function processes WM_SIZE
10820 * messages. At this time, the width and height are not used.
10822 * PARAMETER(S):
10823 * [I] infoPtr : valid pointer to the listview structure
10824 * [I] Width : new width
10825 * [I] Height : new height
10827 * RETURN:
10828 * Zero
10830 static LRESULT LISTVIEW_Size(LISTVIEW_INFO *infoPtr, int Width, int Height)
10832 RECT rcOld = infoPtr->rcList;
10834 TRACE("(width=%d, height=%d)\n", Width, Height);
10836 LISTVIEW_UpdateSize(infoPtr);
10837 if (EqualRect(&rcOld, &infoPtr->rcList)) return 0;
10839 /* do not bother with display related stuff if we're not redrawing */
10840 if (!is_redrawing(infoPtr)) return 0;
10842 if (is_autoarrange(infoPtr))
10843 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
10845 LISTVIEW_UpdateScroll(infoPtr);
10847 /* refresh all only for lists whose height changed significantly */
10848 if ((infoPtr->uView == LV_VIEW_LIST) &&
10849 (rcOld.bottom - rcOld.top) / infoPtr->nItemHeight !=
10850 (infoPtr->rcList.bottom - infoPtr->rcList.top) / infoPtr->nItemHeight)
10851 LISTVIEW_InvalidateList(infoPtr);
10853 return 0;
10856 /***
10857 * DESCRIPTION:
10858 * Sets the size information.
10860 * PARAMETER(S):
10861 * [I] infoPtr : valid pointer to the listview structure
10863 * RETURN:
10864 * None
10866 static void LISTVIEW_UpdateSize(LISTVIEW_INFO *infoPtr)
10868 TRACE("uView=%d, rcList(old)=%s\n", infoPtr->uView, wine_dbgstr_rect(&infoPtr->rcList));
10870 GetClientRect(infoPtr->hwndSelf, &infoPtr->rcList);
10872 if (infoPtr->uView == LV_VIEW_LIST)
10874 /* Apparently the "LIST" style is supposed to have the same
10875 * number of items in a column even if there is no scroll bar.
10876 * Since if a scroll bar already exists then the bottom is already
10877 * reduced, only reduce if the scroll bar does not currently exist.
10878 * The "2" is there to mimic the native control. I think it may be
10879 * related to either padding or edges. (GLA 7/2002)
10881 if (!(infoPtr->dwStyle & WS_HSCROLL))
10882 infoPtr->rcList.bottom -= GetSystemMetrics(SM_CYHSCROLL);
10883 infoPtr->rcList.bottom = max (infoPtr->rcList.bottom - 2, 0);
10886 /* if control created invisible header isn't created */
10887 if (infoPtr->hwndHeader)
10889 HDLAYOUT hl;
10890 WINDOWPOS wp;
10892 hl.prc = &infoPtr->rcList;
10893 hl.pwpos = &wp;
10894 SendMessageW( infoPtr->hwndHeader, HDM_LAYOUT, 0, (LPARAM)&hl );
10895 TRACE(" wp.flags=0x%08x, wp=%d,%d (%dx%d)\n", wp.flags, wp.x, wp.y, wp.cx, wp.cy);
10897 if (LISTVIEW_IsHeaderEnabled(infoPtr))
10898 wp.flags |= SWP_SHOWWINDOW;
10899 else
10901 wp.flags |= SWP_HIDEWINDOW;
10902 wp.cy = 0;
10905 SetWindowPos(wp.hwnd, wp.hwndInsertAfter, wp.x, wp.y, wp.cx, wp.cy, wp.flags);
10906 TRACE(" after SWP wp=%d,%d (%dx%d)\n", wp.x, wp.y, wp.cx, wp.cy);
10908 infoPtr->rcList.top = max(wp.cy, 0);
10910 /* extra padding for grid */
10911 if (infoPtr->uView == LV_VIEW_DETAILS && infoPtr->dwLvExStyle & LVS_EX_GRIDLINES)
10912 infoPtr->rcList.top += 2;
10914 TRACE(" rcList=%s\n", wine_dbgstr_rect(&infoPtr->rcList));
10917 /***
10918 * DESCRIPTION:
10919 * Processes WM_STYLECHANGED messages.
10921 * PARAMETER(S):
10922 * [I] infoPtr : valid pointer to the listview structure
10923 * [I] wStyleType : window style type (normal or extended)
10924 * [I] lpss : window style information
10926 * RETURN:
10927 * Zero
10929 static INT LISTVIEW_StyleChanged(LISTVIEW_INFO *infoPtr, WPARAM wStyleType,
10930 const STYLESTRUCT *lpss)
10932 UINT uNewView = lpss->styleNew & LVS_TYPEMASK;
10933 UINT uOldView = lpss->styleOld & LVS_TYPEMASK;
10934 UINT style;
10936 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
10937 wStyleType, lpss->styleOld, lpss->styleNew);
10939 if (wStyleType != GWL_STYLE) return 0;
10941 infoPtr->dwStyle = lpss->styleNew;
10942 map_style_view(infoPtr);
10944 if (((lpss->styleOld & WS_HSCROLL) != 0)&&
10945 ((lpss->styleNew & WS_HSCROLL) == 0))
10946 ShowScrollBar(infoPtr->hwndSelf, SB_HORZ, FALSE);
10948 if (((lpss->styleOld & WS_VSCROLL) != 0)&&
10949 ((lpss->styleNew & WS_VSCROLL) == 0))
10950 ShowScrollBar(infoPtr->hwndSelf, SB_VERT, FALSE);
10952 if (uNewView != uOldView)
10954 SIZE oldIconSize = infoPtr->iconSize;
10955 HIMAGELIST himl;
10957 SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
10958 ShowWindow(infoPtr->hwndHeader, SW_HIDE);
10960 ShowScrollBar(infoPtr->hwndSelf, SB_BOTH, FALSE);
10961 SetRectEmpty(&infoPtr->rcFocus);
10963 himl = (uNewView == LVS_ICON ? infoPtr->himlNormal : infoPtr->himlSmall);
10964 set_icon_size(&infoPtr->iconSize, himl, uNewView != LVS_ICON);
10966 if (uNewView == LVS_ICON)
10968 if ((infoPtr->iconSize.cx != oldIconSize.cx) || (infoPtr->iconSize.cy != oldIconSize.cy))
10970 TRACE("icon old size=(%d,%d), new size=(%d,%d)\n",
10971 oldIconSize.cx, oldIconSize.cy, infoPtr->iconSize.cx, infoPtr->iconSize.cy);
10972 LISTVIEW_SetIconSpacing(infoPtr, 0, 0);
10975 else if (uNewView == LVS_REPORT)
10977 HDLAYOUT hl;
10978 WINDOWPOS wp;
10980 LISTVIEW_CreateHeader( infoPtr );
10982 hl.prc = &infoPtr->rcList;
10983 hl.pwpos = &wp;
10984 SendMessageW( infoPtr->hwndHeader, HDM_LAYOUT, 0, (LPARAM)&hl );
10985 SetWindowPos(infoPtr->hwndHeader, infoPtr->hwndSelf, wp.x, wp.y, wp.cx, wp.cy,
10986 wp.flags | ((infoPtr->dwStyle & LVS_NOCOLUMNHEADER)
10987 ? SWP_HIDEWINDOW : SWP_SHOWWINDOW));
10990 LISTVIEW_UpdateItemSize(infoPtr);
10993 if (uNewView == LVS_REPORT || infoPtr->dwLvExStyle & LVS_EX_HEADERINALLVIEWS)
10995 if ((lpss->styleOld ^ lpss->styleNew) & LVS_NOCOLUMNHEADER)
10997 if (lpss->styleNew & LVS_NOCOLUMNHEADER)
10999 /* Turn off the header control */
11000 style = GetWindowLongW(infoPtr->hwndHeader, GWL_STYLE);
11001 TRACE("Hide header control, was 0x%08x\n", style);
11002 SetWindowLongW(infoPtr->hwndHeader, GWL_STYLE, style | HDS_HIDDEN);
11003 } else {
11004 /* Turn on the header control */
11005 if ((style = GetWindowLongW(infoPtr->hwndHeader, GWL_STYLE)) & HDS_HIDDEN)
11007 TRACE("Show header control, was 0x%08x\n", style);
11008 SetWindowLongW(infoPtr->hwndHeader, GWL_STYLE, (style & ~HDS_HIDDEN) | WS_VISIBLE);
11014 if ( (uNewView == LVS_ICON || uNewView == LVS_SMALLICON) &&
11015 (uNewView != uOldView || ((lpss->styleNew ^ lpss->styleOld) & LVS_ALIGNMASK)) )
11016 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
11018 /* update the size of the client area */
11019 LISTVIEW_UpdateSize(infoPtr);
11021 /* add scrollbars if needed */
11022 LISTVIEW_UpdateScroll(infoPtr);
11024 /* invalidate client area + erase background */
11025 LISTVIEW_InvalidateList(infoPtr);
11027 return 0;
11030 /***
11031 * DESCRIPTION:
11032 * Processes WM_STYLECHANGING messages.
11034 * PARAMETER(S):
11035 * [I] wStyleType : window style type (normal or extended)
11036 * [I0] lpss : window style information
11038 * RETURN:
11039 * Zero
11041 static INT LISTVIEW_StyleChanging(WPARAM wStyleType,
11042 STYLESTRUCT *lpss)
11044 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
11045 wStyleType, lpss->styleOld, lpss->styleNew);
11047 /* don't forward LVS_OWNERDATA only if not already set to */
11048 if ((lpss->styleNew ^ lpss->styleOld) & LVS_OWNERDATA)
11050 if (lpss->styleOld & LVS_OWNERDATA)
11051 lpss->styleNew |= LVS_OWNERDATA;
11052 else
11053 lpss->styleNew &= ~LVS_OWNERDATA;
11056 return 0;
11059 /***
11060 * DESCRIPTION:
11061 * Processes WM_SHOWWINDOW messages.
11063 * PARAMETER(S):
11064 * [I] infoPtr : valid pointer to the listview structure
11065 * [I] bShown : window is being shown (FALSE when hidden)
11066 * [I] iStatus : window show status
11068 * RETURN:
11069 * Zero
11071 static LRESULT LISTVIEW_ShowWindow(LISTVIEW_INFO *infoPtr, WPARAM bShown, LPARAM iStatus)
11073 /* header delayed creation */
11074 if ((infoPtr->uView == LV_VIEW_DETAILS) && bShown)
11076 LISTVIEW_CreateHeader(infoPtr);
11078 if (!(LVS_NOCOLUMNHEADER & infoPtr->dwStyle))
11079 ShowWindow(infoPtr->hwndHeader, SW_SHOWNORMAL);
11082 return DefWindowProcW(infoPtr->hwndSelf, WM_SHOWWINDOW, bShown, iStatus);
11085 /***
11086 * DESCRIPTION:
11087 * Processes CCM_GETVERSION messages.
11089 * PARAMETER(S):
11090 * [I] infoPtr : valid pointer to the listview structure
11092 * RETURN:
11093 * Current version
11095 static inline LRESULT LISTVIEW_GetVersion(const LISTVIEW_INFO *infoPtr)
11097 return infoPtr->iVersion;
11100 /***
11101 * DESCRIPTION:
11102 * Processes CCM_SETVERSION messages.
11104 * PARAMETER(S):
11105 * [I] infoPtr : valid pointer to the listview structure
11106 * [I] iVersion : version to be set
11108 * RETURN:
11109 * -1 when requested version is greater than DLL version;
11110 * previous version otherwise
11112 static LRESULT LISTVIEW_SetVersion(LISTVIEW_INFO *infoPtr, DWORD iVersion)
11114 INT iOldVersion = infoPtr->iVersion;
11116 if (iVersion > COMCTL32_VERSION)
11117 return -1;
11119 infoPtr->iVersion = iVersion;
11121 TRACE("new version %d\n", iVersion);
11123 return iOldVersion;
11126 /***
11127 * DESCRIPTION:
11128 * Window procedure of the listview control.
11131 static LRESULT WINAPI
11132 LISTVIEW_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
11134 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongPtrW(hwnd, 0);
11136 TRACE("(hwnd=%p uMsg=%x wParam=%lx lParam=%lx)\n", hwnd, uMsg, wParam, lParam);
11138 if (!infoPtr && (uMsg != WM_NCCREATE))
11139 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
11141 switch (uMsg)
11143 case LVM_APPROXIMATEVIEWRECT:
11144 return LISTVIEW_ApproximateViewRect(infoPtr, (INT)wParam,
11145 LOWORD(lParam), HIWORD(lParam));
11146 case LVM_ARRANGE:
11147 return LISTVIEW_Arrange(infoPtr, (INT)wParam);
11149 case LVM_CANCELEDITLABEL:
11150 return LISTVIEW_CancelEditLabel(infoPtr);
11152 case LVM_CREATEDRAGIMAGE:
11153 return (LRESULT)LISTVIEW_CreateDragImage(infoPtr, (INT)wParam, (LPPOINT)lParam);
11155 case LVM_DELETEALLITEMS:
11156 return LISTVIEW_DeleteAllItems(infoPtr, FALSE);
11158 case LVM_DELETECOLUMN:
11159 return LISTVIEW_DeleteColumn(infoPtr, (INT)wParam);
11161 case LVM_DELETEITEM:
11162 return LISTVIEW_DeleteItem(infoPtr, (INT)wParam);
11164 case LVM_EDITLABELA:
11165 case LVM_EDITLABELW:
11166 return (LRESULT)LISTVIEW_EditLabelT(infoPtr, (INT)wParam,
11167 uMsg == LVM_EDITLABELW);
11168 /* case LVM_ENABLEGROUPVIEW: */
11170 case LVM_ENSUREVISIBLE:
11171 return LISTVIEW_EnsureVisible(infoPtr, (INT)wParam, (BOOL)lParam);
11173 case LVM_FINDITEMW:
11174 return LISTVIEW_FindItemW(infoPtr, (INT)wParam, (LPLVFINDINFOW)lParam);
11176 case LVM_FINDITEMA:
11177 return LISTVIEW_FindItemA(infoPtr, (INT)wParam, (LPLVFINDINFOA)lParam);
11179 case LVM_GETBKCOLOR:
11180 return infoPtr->clrBk;
11182 /* case LVM_GETBKIMAGE: */
11184 case LVM_GETCALLBACKMASK:
11185 return infoPtr->uCallbackMask;
11187 case LVM_GETCOLUMNA:
11188 case LVM_GETCOLUMNW:
11189 return LISTVIEW_GetColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam,
11190 uMsg == LVM_GETCOLUMNW);
11192 case LVM_GETCOLUMNORDERARRAY:
11193 return LISTVIEW_GetColumnOrderArray(infoPtr, (INT)wParam, (LPINT)lParam);
11195 case LVM_GETCOLUMNWIDTH:
11196 return LISTVIEW_GetColumnWidth(infoPtr, (INT)wParam);
11198 case LVM_GETCOUNTPERPAGE:
11199 return LISTVIEW_GetCountPerPage(infoPtr);
11201 case LVM_GETEDITCONTROL:
11202 return (LRESULT)infoPtr->hwndEdit;
11204 case LVM_GETEXTENDEDLISTVIEWSTYLE:
11205 return infoPtr->dwLvExStyle;
11207 /* case LVM_GETGROUPINFO: */
11209 /* case LVM_GETGROUPMETRICS: */
11211 case LVM_GETHEADER:
11212 return (LRESULT)infoPtr->hwndHeader;
11214 case LVM_GETHOTCURSOR:
11215 return (LRESULT)infoPtr->hHotCursor;
11217 case LVM_GETHOTITEM:
11218 return infoPtr->nHotItem;
11220 case LVM_GETHOVERTIME:
11221 return infoPtr->dwHoverTime;
11223 case LVM_GETIMAGELIST:
11224 return (LRESULT)LISTVIEW_GetImageList(infoPtr, (INT)wParam);
11226 /* case LVM_GETINSERTMARK: */
11228 /* case LVM_GETINSERTMARKCOLOR: */
11230 /* case LVM_GETINSERTMARKRECT: */
11232 case LVM_GETISEARCHSTRINGA:
11233 case LVM_GETISEARCHSTRINGW:
11234 FIXME("LVM_GETISEARCHSTRING: unimplemented\n");
11235 return FALSE;
11237 case LVM_GETITEMA:
11238 case LVM_GETITEMW:
11239 return LISTVIEW_GetItemExtT(infoPtr, (LPLVITEMW)lParam, uMsg == LVM_GETITEMW);
11241 case LVM_GETITEMCOUNT:
11242 return infoPtr->nItemCount;
11244 case LVM_GETITEMPOSITION:
11245 return LISTVIEW_GetItemPosition(infoPtr, (INT)wParam, (LPPOINT)lParam);
11247 case LVM_GETITEMRECT:
11248 return LISTVIEW_GetItemRect(infoPtr, (INT)wParam, (LPRECT)lParam);
11250 case LVM_GETITEMSPACING:
11251 return LISTVIEW_GetItemSpacing(infoPtr, (BOOL)wParam);
11253 case LVM_GETITEMSTATE:
11254 return LISTVIEW_GetItemState(infoPtr, (INT)wParam, (UINT)lParam);
11256 case LVM_GETITEMTEXTA:
11257 case LVM_GETITEMTEXTW:
11258 return LISTVIEW_GetItemTextT(infoPtr, (INT)wParam, (LPLVITEMW)lParam,
11259 uMsg == LVM_GETITEMTEXTW);
11261 case LVM_GETNEXTITEM:
11262 return LISTVIEW_GetNextItem(infoPtr, (INT)wParam, LOWORD(lParam));
11264 case LVM_GETNUMBEROFWORKAREAS:
11265 FIXME("LVM_GETNUMBEROFWORKAREAS: unimplemented\n");
11266 return 1;
11268 case LVM_GETORIGIN:
11269 if (!lParam) return FALSE;
11270 if (infoPtr->uView == LV_VIEW_DETAILS ||
11271 infoPtr->uView == LV_VIEW_LIST) return FALSE;
11272 LISTVIEW_GetOrigin(infoPtr, (LPPOINT)lParam);
11273 return TRUE;
11275 /* case LVM_GETOUTLINECOLOR: */
11277 /* case LVM_GETSELECTEDCOLUMN: */
11279 case LVM_GETSELECTEDCOUNT:
11280 return LISTVIEW_GetSelectedCount(infoPtr);
11282 case LVM_GETSELECTIONMARK:
11283 return infoPtr->nSelectionMark;
11285 case LVM_GETSTRINGWIDTHA:
11286 case LVM_GETSTRINGWIDTHW:
11287 return LISTVIEW_GetStringWidthT(infoPtr, (LPCWSTR)lParam,
11288 uMsg == LVM_GETSTRINGWIDTHW);
11290 case LVM_GETSUBITEMRECT:
11291 return LISTVIEW_GetSubItemRect(infoPtr, (UINT)wParam, (LPRECT)lParam);
11293 case LVM_GETTEXTBKCOLOR:
11294 return infoPtr->clrTextBk;
11296 case LVM_GETTEXTCOLOR:
11297 return infoPtr->clrText;
11299 /* case LVM_GETTILEINFO: */
11301 /* case LVM_GETTILEVIEWINFO: */
11303 case LVM_GETTOOLTIPS:
11304 if( !infoPtr->hwndToolTip )
11305 infoPtr->hwndToolTip = COMCTL32_CreateToolTip( hwnd );
11306 return (LRESULT)infoPtr->hwndToolTip;
11308 case LVM_GETTOPINDEX:
11309 return LISTVIEW_GetTopIndex(infoPtr);
11311 case LVM_GETUNICODEFORMAT:
11312 return (infoPtr->notifyFormat == NFR_UNICODE);
11314 case LVM_GETVIEW:
11315 return infoPtr->uView;
11317 case LVM_GETVIEWRECT:
11318 return LISTVIEW_GetViewRect(infoPtr, (LPRECT)lParam);
11320 case LVM_GETWORKAREAS:
11321 FIXME("LVM_GETWORKAREAS: unimplemented\n");
11322 return FALSE;
11324 /* case LVM_HASGROUP: */
11326 case LVM_HITTEST:
11327 return LISTVIEW_HitTest(infoPtr, (LPLVHITTESTINFO)lParam, FALSE, TRUE);
11329 case LVM_INSERTCOLUMNA:
11330 case LVM_INSERTCOLUMNW:
11331 return LISTVIEW_InsertColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam,
11332 uMsg == LVM_INSERTCOLUMNW);
11334 /* case LVM_INSERTGROUP: */
11336 /* case LVM_INSERTGROUPSORTED: */
11338 case LVM_INSERTITEMA:
11339 case LVM_INSERTITEMW:
11340 return LISTVIEW_InsertItemT(infoPtr, (LPLVITEMW)lParam, uMsg == LVM_INSERTITEMW);
11342 /* case LVM_INSERTMARKHITTEST: */
11344 /* case LVM_ISGROUPVIEWENABLED: */
11346 case LVM_ISITEMVISIBLE:
11347 return LISTVIEW_IsItemVisible(infoPtr, (INT)wParam);
11349 case LVM_MAPIDTOINDEX:
11350 return LISTVIEW_MapIdToIndex(infoPtr, (UINT)wParam);
11352 case LVM_MAPINDEXTOID:
11353 return LISTVIEW_MapIndexToId(infoPtr, (INT)wParam);
11355 /* case LVM_MOVEGROUP: */
11357 /* case LVM_MOVEITEMTOGROUP: */
11359 case LVM_REDRAWITEMS:
11360 return LISTVIEW_RedrawItems(infoPtr, (INT)wParam, (INT)lParam);
11362 /* case LVM_REMOVEALLGROUPS: */
11364 /* case LVM_REMOVEGROUP: */
11366 case LVM_SCROLL:
11367 return LISTVIEW_Scroll(infoPtr, (INT)wParam, (INT)lParam);
11369 case LVM_SETBKCOLOR:
11370 return LISTVIEW_SetBkColor(infoPtr, (COLORREF)lParam);
11372 /* case LVM_SETBKIMAGE: */
11374 case LVM_SETCALLBACKMASK:
11375 infoPtr->uCallbackMask = (UINT)wParam;
11376 return TRUE;
11378 case LVM_SETCOLUMNA:
11379 case LVM_SETCOLUMNW:
11380 return LISTVIEW_SetColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam,
11381 uMsg == LVM_SETCOLUMNW);
11383 case LVM_SETCOLUMNORDERARRAY:
11384 return LISTVIEW_SetColumnOrderArray(infoPtr, (INT)wParam, (LPINT)lParam);
11386 case LVM_SETCOLUMNWIDTH:
11387 return LISTVIEW_SetColumnWidth(infoPtr, (INT)wParam, (short)LOWORD(lParam));
11389 case LVM_SETEXTENDEDLISTVIEWSTYLE:
11390 return LISTVIEW_SetExtendedListViewStyle(infoPtr, (DWORD)wParam, (DWORD)lParam);
11392 /* case LVM_SETGROUPINFO: */
11394 /* case LVM_SETGROUPMETRICS: */
11396 case LVM_SETHOTCURSOR:
11397 return (LRESULT)LISTVIEW_SetHotCursor(infoPtr, (HCURSOR)lParam);
11399 case LVM_SETHOTITEM:
11400 return LISTVIEW_SetHotItem(infoPtr, (INT)wParam);
11402 case LVM_SETHOVERTIME:
11403 return LISTVIEW_SetHoverTime(infoPtr, (DWORD)lParam);
11405 case LVM_SETICONSPACING:
11406 return LISTVIEW_SetIconSpacing(infoPtr, (short)LOWORD(lParam), (short)HIWORD(lParam));
11408 case LVM_SETIMAGELIST:
11409 return (LRESULT)LISTVIEW_SetImageList(infoPtr, (INT)wParam, (HIMAGELIST)lParam);
11411 /* case LVM_SETINFOTIP: */
11413 /* case LVM_SETINSERTMARK: */
11415 /* case LVM_SETINSERTMARKCOLOR: */
11417 case LVM_SETITEMA:
11418 case LVM_SETITEMW:
11420 if (infoPtr->dwStyle & LVS_OWNERDATA) return FALSE;
11421 return LISTVIEW_SetItemT(infoPtr, (LPLVITEMW)lParam, (uMsg == LVM_SETITEMW));
11424 case LVM_SETITEMCOUNT:
11425 return LISTVIEW_SetItemCount(infoPtr, (INT)wParam, (DWORD)lParam);
11427 case LVM_SETITEMPOSITION:
11429 POINT pt;
11430 pt.x = (short)LOWORD(lParam);
11431 pt.y = (short)HIWORD(lParam);
11432 return LISTVIEW_SetItemPosition(infoPtr, (INT)wParam, &pt);
11435 case LVM_SETITEMPOSITION32:
11436 return LISTVIEW_SetItemPosition(infoPtr, (INT)wParam, (POINT*)lParam);
11438 case LVM_SETITEMSTATE:
11439 return LISTVIEW_SetItemState(infoPtr, (INT)wParam, (LPLVITEMW)lParam);
11441 case LVM_SETITEMTEXTA:
11442 case LVM_SETITEMTEXTW:
11443 return LISTVIEW_SetItemTextT(infoPtr, (INT)wParam, (LPLVITEMW)lParam,
11444 uMsg == LVM_SETITEMTEXTW);
11446 /* case LVM_SETOUTLINECOLOR: */
11448 /* case LVM_SETSELECTEDCOLUMN: */
11450 case LVM_SETSELECTIONMARK:
11451 return LISTVIEW_SetSelectionMark(infoPtr, (INT)lParam);
11453 case LVM_SETTEXTBKCOLOR:
11454 return LISTVIEW_SetTextBkColor(infoPtr, (COLORREF)lParam);
11456 case LVM_SETTEXTCOLOR:
11457 return LISTVIEW_SetTextColor(infoPtr, (COLORREF)lParam);
11459 /* case LVM_SETTILEINFO: */
11461 /* case LVM_SETTILEVIEWINFO: */
11463 /* case LVM_SETTILEWIDTH: */
11465 case LVM_SETTOOLTIPS:
11466 return (LRESULT)LISTVIEW_SetToolTips(infoPtr, (HWND)lParam);
11468 case LVM_SETUNICODEFORMAT:
11469 return LISTVIEW_SetUnicodeFormat(infoPtr, wParam);
11471 case LVM_SETVIEW:
11472 return LISTVIEW_SetView(infoPtr, wParam);
11474 /* case LVM_SETWORKAREAS: */
11476 /* case LVM_SORTGROUPS: */
11478 case LVM_SORTITEMS:
11479 case LVM_SORTITEMSEX:
11480 return LISTVIEW_SortItems(infoPtr, (PFNLVCOMPARE)lParam, wParam,
11481 uMsg == LVM_SORTITEMSEX);
11482 case LVM_SUBITEMHITTEST:
11483 return LISTVIEW_HitTest(infoPtr, (LPLVHITTESTINFO)lParam, TRUE, FALSE);
11485 case LVM_UPDATE:
11486 return LISTVIEW_Update(infoPtr, (INT)wParam);
11488 case CCM_GETVERSION:
11489 return LISTVIEW_GetVersion(infoPtr);
11491 case CCM_SETVERSION:
11492 return LISTVIEW_SetVersion(infoPtr, wParam);
11494 case WM_CHAR:
11495 return LISTVIEW_ProcessLetterKeys( infoPtr, wParam, lParam );
11497 case WM_COMMAND:
11498 return LISTVIEW_Command(infoPtr, wParam, lParam);
11500 case WM_NCCREATE:
11501 return LISTVIEW_NCCreate(hwnd, (LPCREATESTRUCTW)lParam);
11503 case WM_CREATE:
11504 return LISTVIEW_Create(hwnd, (LPCREATESTRUCTW)lParam);
11506 case WM_DESTROY:
11507 return LISTVIEW_Destroy(infoPtr);
11509 case WM_ENABLE:
11510 return LISTVIEW_Enable(infoPtr);
11512 case WM_ERASEBKGND:
11513 return LISTVIEW_EraseBkgnd(infoPtr, (HDC)wParam);
11515 case WM_GETDLGCODE:
11516 return DLGC_WANTCHARS | DLGC_WANTARROWS;
11518 case WM_GETFONT:
11519 return (LRESULT)infoPtr->hFont;
11521 case WM_HSCROLL:
11522 return LISTVIEW_HScroll(infoPtr, (INT)LOWORD(wParam), 0);
11524 case WM_KEYDOWN:
11525 return LISTVIEW_KeyDown(infoPtr, (INT)wParam, (LONG)lParam);
11527 case WM_KILLFOCUS:
11528 return LISTVIEW_KillFocus(infoPtr);
11530 case WM_LBUTTONDBLCLK:
11531 return LISTVIEW_LButtonDblClk(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11533 case WM_LBUTTONDOWN:
11534 return LISTVIEW_LButtonDown(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11536 case WM_LBUTTONUP:
11537 return LISTVIEW_LButtonUp(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11539 case WM_MOUSEMOVE:
11540 return LISTVIEW_MouseMove (infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11542 case WM_MOUSEHOVER:
11543 return LISTVIEW_MouseHover(infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11545 case WM_NCDESTROY:
11546 return LISTVIEW_NCDestroy(infoPtr);
11548 case WM_NCPAINT:
11549 return LISTVIEW_NCPaint(infoPtr, (HRGN)wParam);
11551 case WM_NOTIFY:
11552 return LISTVIEW_Notify(infoPtr, (LPNMHDR)lParam);
11554 case WM_NOTIFYFORMAT:
11555 return LISTVIEW_NotifyFormat(infoPtr, (HWND)wParam, (INT)lParam);
11557 case WM_PRINTCLIENT:
11558 return LISTVIEW_PrintClient(infoPtr, (HDC)wParam, (DWORD)lParam);
11560 case WM_PAINT:
11561 return LISTVIEW_WMPaint(infoPtr, (HDC)wParam);
11563 case WM_RBUTTONDBLCLK:
11564 return LISTVIEW_RButtonDblClk(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11566 case WM_RBUTTONDOWN:
11567 return LISTVIEW_RButtonDown(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11569 case WM_RBUTTONUP:
11570 return LISTVIEW_RButtonUp(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11572 case WM_SETCURSOR:
11573 return LISTVIEW_SetCursor(infoPtr, wParam, lParam);
11575 case WM_SETFOCUS:
11576 return LISTVIEW_SetFocus(infoPtr, (HWND)wParam);
11578 case WM_SETFONT:
11579 return LISTVIEW_SetFont(infoPtr, (HFONT)wParam, (WORD)lParam);
11581 case WM_SETREDRAW:
11582 return LISTVIEW_SetRedraw(infoPtr, (BOOL)wParam);
11584 case WM_SHOWWINDOW:
11585 return LISTVIEW_ShowWindow(infoPtr, wParam, lParam);
11587 case WM_SIZE:
11588 return LISTVIEW_Size(infoPtr, (short)LOWORD(lParam), (short)HIWORD(lParam));
11590 case WM_STYLECHANGED:
11591 return LISTVIEW_StyleChanged(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
11593 case WM_STYLECHANGING:
11594 return LISTVIEW_StyleChanging(wParam, (LPSTYLESTRUCT)lParam);
11596 case WM_SYSCOLORCHANGE:
11597 COMCTL32_RefreshSysColors();
11598 return 0;
11600 /* case WM_TIMER: */
11601 case WM_THEMECHANGED:
11602 return LISTVIEW_ThemeChanged(infoPtr);
11604 case WM_VSCROLL:
11605 return LISTVIEW_VScroll(infoPtr, (INT)LOWORD(wParam), 0);
11607 case WM_MOUSEWHEEL:
11608 if (wParam & (MK_SHIFT | MK_CONTROL))
11609 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
11610 return LISTVIEW_MouseWheel(infoPtr, (short int)HIWORD(wParam));
11612 case WM_WINDOWPOSCHANGED:
11613 if (!(((WINDOWPOS *)lParam)->flags & SWP_NOSIZE))
11615 SetWindowPos(infoPtr->hwndSelf, 0, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOACTIVATE |
11616 SWP_NOZORDER | SWP_NOMOVE | SWP_NOSIZE);
11618 if ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (infoPtr->uView == LV_VIEW_DETAILS))
11620 if (notify_measureitem(infoPtr)) LISTVIEW_InvalidateList(infoPtr);
11623 LISTVIEW_UpdateSize(infoPtr);
11624 LISTVIEW_UpdateScroll(infoPtr);
11626 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
11628 /* case WM_WININICHANGE: */
11630 default:
11631 if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
11632 ERR("unknown msg %04x wp=%08lx lp=%08lx\n", uMsg, wParam, lParam);
11634 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
11639 /***
11640 * DESCRIPTION:
11641 * Registers the window class.
11643 * PARAMETER(S):
11644 * None
11646 * RETURN:
11647 * None
11649 void LISTVIEW_Register(void)
11651 WNDCLASSW wndClass;
11653 ZeroMemory(&wndClass, sizeof(WNDCLASSW));
11654 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS;
11655 wndClass.lpfnWndProc = LISTVIEW_WindowProc;
11656 wndClass.cbClsExtra = 0;
11657 wndClass.cbWndExtra = sizeof(LISTVIEW_INFO *);
11658 wndClass.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
11659 wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
11660 wndClass.lpszClassName = WC_LISTVIEWW;
11661 RegisterClassW(&wndClass);
11664 /***
11665 * DESCRIPTION:
11666 * Unregisters the window class.
11668 * PARAMETER(S):
11669 * None
11671 * RETURN:
11672 * None
11674 void LISTVIEW_Unregister(void)
11676 UnregisterClassW(WC_LISTVIEWW, NULL);
11679 /***
11680 * DESCRIPTION:
11681 * Handle any WM_COMMAND messages
11683 * PARAMETER(S):
11684 * [I] infoPtr : valid pointer to the listview structure
11685 * [I] wParam : the first message parameter
11686 * [I] lParam : the second message parameter
11688 * RETURN:
11689 * Zero.
11691 static LRESULT LISTVIEW_Command(LISTVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
11694 TRACE("(%p %x %x %lx)\n", infoPtr, HIWORD(wParam), LOWORD(wParam), lParam);
11696 if (!infoPtr->hwndEdit) return 0;
11698 switch (HIWORD(wParam))
11700 case EN_UPDATE:
11703 * Adjust the edit window size
11705 WCHAR buffer[1024];
11706 HDC hdc = GetDC(infoPtr->hwndEdit);
11707 HFONT hFont, hOldFont = 0;
11708 RECT rect;
11709 SIZE sz;
11711 if (!infoPtr->hwndEdit || !hdc) return 0;
11712 GetWindowTextW(infoPtr->hwndEdit, buffer, sizeof(buffer)/sizeof(buffer[0]));
11713 GetWindowRect(infoPtr->hwndEdit, &rect);
11715 /* Select font to get the right dimension of the string */
11716 hFont = (HFONT)SendMessageW(infoPtr->hwndEdit, WM_GETFONT, 0, 0);
11717 if (hFont)
11719 hOldFont = SelectObject(hdc, hFont);
11722 if (GetTextExtentPoint32W(hdc, buffer, lstrlenW(buffer), &sz))
11724 TEXTMETRICW textMetric;
11726 /* Add Extra spacing for the next character */
11727 GetTextMetricsW(hdc, &textMetric);
11728 sz.cx += (textMetric.tmMaxCharWidth * 2);
11730 SetWindowPos(infoPtr->hwndEdit, NULL, 0, 0, sz.cx,
11731 rect.bottom - rect.top, SWP_DRAWFRAME | SWP_NOMOVE | SWP_NOZORDER);
11733 if (hFont)
11734 SelectObject(hdc, hOldFont);
11736 ReleaseDC(infoPtr->hwndEdit, hdc);
11738 break;
11740 case EN_KILLFOCUS:
11742 LISTVIEW_CancelEditLabel(infoPtr);
11743 break;
11746 default:
11747 return SendMessageW (infoPtr->hwndNotify, WM_COMMAND, wParam, lParam);
11750 return 0;