gdi32: Pass the source/dest visible rectangles to the AlphaBlend driver entry point.
[wine/testsucceed.git] / dlls / comctl32 / listview.c
blob033472cc8da0beb08ea09d7987c27df7a998fffe
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-2011 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 int res;
564 n = min(min(n, lstrlenW(s1)), lstrlenW(s2));
565 res = CompareStringW(LOCALE_USER_DEFAULT, NORM_IGNORECASE, s1, n, s2, n);
566 return res ? res - sizeof(WCHAR) : res;
569 /******** Debugging functions *****************************************/
571 static inline LPCSTR debugtext_t(LPCWSTR text, BOOL isW)
573 if (text == LPSTR_TEXTCALLBACKW) return "(callback)";
574 return isW ? debugstr_w(text) : debugstr_a((LPCSTR)text);
577 static inline LPCSTR debugtext_tn(LPCWSTR text, BOOL isW, INT n)
579 if (text == LPSTR_TEXTCALLBACKW) return "(callback)";
580 n = min(textlenT(text, isW), n);
581 return isW ? debugstr_wn(text, n) : debugstr_an((LPCSTR)text, n);
584 static char* debug_getbuf(void)
586 static int index = 0;
587 static char buffers[DEBUG_BUFFERS][DEBUG_BUFFER_SIZE];
588 return buffers[index++ % DEBUG_BUFFERS];
591 static inline const char* debugrange(const RANGE *lprng)
593 if (!lprng) return "(null)";
594 return wine_dbg_sprintf("[%d, %d]", lprng->lower, lprng->upper);
597 static const char* debugscrollinfo(const SCROLLINFO *pScrollInfo)
599 char* buf = debug_getbuf(), *text = buf;
600 int len, size = DEBUG_BUFFER_SIZE;
602 if (pScrollInfo == NULL) return "(null)";
603 len = snprintf(buf, size, "{cbSize=%d, ", pScrollInfo->cbSize);
604 if (len == -1) goto end; buf += len; size -= len;
605 if (pScrollInfo->fMask & SIF_RANGE)
606 len = snprintf(buf, size, "nMin=%d, nMax=%d, ", pScrollInfo->nMin, pScrollInfo->nMax);
607 else len = 0;
608 if (len == -1) goto end; buf += len; size -= len;
609 if (pScrollInfo->fMask & SIF_PAGE)
610 len = snprintf(buf, size, "nPage=%u, ", pScrollInfo->nPage);
611 else len = 0;
612 if (len == -1) goto end; buf += len; size -= len;
613 if (pScrollInfo->fMask & SIF_POS)
614 len = snprintf(buf, size, "nPos=%d, ", pScrollInfo->nPos);
615 else len = 0;
616 if (len == -1) goto end; buf += len; size -= len;
617 if (pScrollInfo->fMask & SIF_TRACKPOS)
618 len = snprintf(buf, size, "nTrackPos=%d, ", pScrollInfo->nTrackPos);
619 else len = 0;
620 if (len == -1) goto end; buf += len;
621 goto undo;
622 end:
623 buf = text + strlen(text);
624 undo:
625 if (buf - text > 2) { buf[-2] = '}'; buf[-1] = 0; }
626 return text;
629 static const char* debugnmlistview(const NMLISTVIEW *plvnm)
631 if (!plvnm) return "(null)";
632 return wine_dbg_sprintf("iItem=%d, iSubItem=%d, uNewState=0x%x,"
633 " uOldState=0x%x, uChanged=0x%x, ptAction=%s, lParam=%ld",
634 plvnm->iItem, plvnm->iSubItem, plvnm->uNewState, plvnm->uOldState,
635 plvnm->uChanged, wine_dbgstr_point(&plvnm->ptAction), plvnm->lParam);
638 static const char* debuglvitem_t(const LVITEMW *lpLVItem, BOOL isW)
640 char* buf = debug_getbuf(), *text = buf;
641 int len, size = DEBUG_BUFFER_SIZE;
643 if (lpLVItem == NULL) return "(null)";
644 len = snprintf(buf, size, "{iItem=%d, iSubItem=%d, ", lpLVItem->iItem, lpLVItem->iSubItem);
645 if (len == -1) goto end; buf += len; size -= len;
646 if (lpLVItem->mask & LVIF_STATE)
647 len = snprintf(buf, size, "state=%x, stateMask=%x, ", lpLVItem->state, lpLVItem->stateMask);
648 else len = 0;
649 if (len == -1) goto end; buf += len; size -= len;
650 if (lpLVItem->mask & LVIF_TEXT)
651 len = snprintf(buf, size, "pszText=%s, cchTextMax=%d, ", debugtext_tn(lpLVItem->pszText, isW, 80), lpLVItem->cchTextMax);
652 else len = 0;
653 if (len == -1) goto end; buf += len; size -= len;
654 if (lpLVItem->mask & LVIF_IMAGE)
655 len = snprintf(buf, size, "iImage=%d, ", lpLVItem->iImage);
656 else len = 0;
657 if (len == -1) goto end; buf += len; size -= len;
658 if (lpLVItem->mask & LVIF_PARAM)
659 len = snprintf(buf, size, "lParam=%lx, ", lpLVItem->lParam);
660 else len = 0;
661 if (len == -1) goto end; buf += len; size -= len;
662 if (lpLVItem->mask & LVIF_INDENT)
663 len = snprintf(buf, size, "iIndent=%d, ", lpLVItem->iIndent);
664 else len = 0;
665 if (len == -1) goto end; buf += len;
666 goto undo;
667 end:
668 buf = text + strlen(text);
669 undo:
670 if (buf - text > 2) { buf[-2] = '}'; buf[-1] = 0; }
671 return text;
674 static const char* debuglvcolumn_t(const LVCOLUMNW *lpColumn, BOOL isW)
676 char* buf = debug_getbuf(), *text = buf;
677 int len, size = DEBUG_BUFFER_SIZE;
679 if (lpColumn == NULL) return "(null)";
680 len = snprintf(buf, size, "{");
681 if (len == -1) goto end; buf += len; size -= len;
682 if (lpColumn->mask & LVCF_SUBITEM)
683 len = snprintf(buf, size, "iSubItem=%d, ", lpColumn->iSubItem);
684 else len = 0;
685 if (len == -1) goto end; buf += len; size -= len;
686 if (lpColumn->mask & LVCF_FMT)
687 len = snprintf(buf, size, "fmt=%x, ", lpColumn->fmt);
688 else len = 0;
689 if (len == -1) goto end; buf += len; size -= len;
690 if (lpColumn->mask & LVCF_WIDTH)
691 len = snprintf(buf, size, "cx=%d, ", lpColumn->cx);
692 else len = 0;
693 if (len == -1) goto end; buf += len; size -= len;
694 if (lpColumn->mask & LVCF_TEXT)
695 len = snprintf(buf, size, "pszText=%s, cchTextMax=%d, ", debugtext_tn(lpColumn->pszText, isW, 80), lpColumn->cchTextMax);
696 else len = 0;
697 if (len == -1) goto end; buf += len; size -= len;
698 if (lpColumn->mask & LVCF_IMAGE)
699 len = snprintf(buf, size, "iImage=%d, ", lpColumn->iImage);
700 else len = 0;
701 if (len == -1) goto end; buf += len; size -= len;
702 if (lpColumn->mask & LVCF_ORDER)
703 len = snprintf(buf, size, "iOrder=%d, ", lpColumn->iOrder);
704 else len = 0;
705 if (len == -1) goto end; buf += len;
706 goto undo;
707 end:
708 buf = text + strlen(text);
709 undo:
710 if (buf - text > 2) { buf[-2] = '}'; buf[-1] = 0; }
711 return text;
714 static const char* debuglvhittestinfo(const LVHITTESTINFO *lpht)
716 if (!lpht) return "(null)";
718 return wine_dbg_sprintf("{pt=%s, flags=0x%x, iItem=%d, iSubItem=%d}",
719 wine_dbgstr_point(&lpht->pt), lpht->flags, lpht->iItem, lpht->iSubItem);
722 /* Return the corresponding text for a given scroll value */
723 static inline LPCSTR debugscrollcode(int nScrollCode)
725 switch(nScrollCode)
727 case SB_LINELEFT: return "SB_LINELEFT";
728 case SB_LINERIGHT: return "SB_LINERIGHT";
729 case SB_PAGELEFT: return "SB_PAGELEFT";
730 case SB_PAGERIGHT: return "SB_PAGERIGHT";
731 case SB_THUMBPOSITION: return "SB_THUMBPOSITION";
732 case SB_THUMBTRACK: return "SB_THUMBTRACK";
733 case SB_ENDSCROLL: return "SB_ENDSCROLL";
734 case SB_INTERNAL: return "SB_INTERNAL";
735 default: return "unknown";
740 /******** Notification functions ************************************/
742 static int get_ansi_notification(UINT unicodeNotificationCode)
744 switch (unicodeNotificationCode)
746 case LVN_BEGINLABELEDITA:
747 case LVN_BEGINLABELEDITW: return LVN_BEGINLABELEDITA;
748 case LVN_ENDLABELEDITA:
749 case LVN_ENDLABELEDITW: return LVN_ENDLABELEDITA;
750 case LVN_GETDISPINFOA:
751 case LVN_GETDISPINFOW: return LVN_GETDISPINFOA;
752 case LVN_SETDISPINFOA:
753 case LVN_SETDISPINFOW: return LVN_SETDISPINFOA;
754 case LVN_ODFINDITEMA:
755 case LVN_ODFINDITEMW: return LVN_ODFINDITEMA;
756 case LVN_GETINFOTIPA:
757 case LVN_GETINFOTIPW: return LVN_GETINFOTIPA;
758 /* header forwards */
759 case HDN_TRACKA:
760 case HDN_TRACKW: return HDN_TRACKA;
761 case HDN_ENDTRACKA:
762 case HDN_ENDTRACKW: return HDN_ENDTRACKA;
763 case HDN_BEGINDRAG: return HDN_BEGINDRAG;
764 case HDN_ENDDRAG: return HDN_ENDDRAG;
765 case HDN_ITEMCHANGINGA:
766 case HDN_ITEMCHANGINGW: return HDN_ITEMCHANGINGA;
767 case HDN_ITEMCHANGEDA:
768 case HDN_ITEMCHANGEDW: return HDN_ITEMCHANGEDA;
769 case HDN_ITEMCLICKA:
770 case HDN_ITEMCLICKW: return HDN_ITEMCLICKA;
771 case HDN_DIVIDERDBLCLICKA:
772 case HDN_DIVIDERDBLCLICKW: return HDN_DIVIDERDBLCLICKA;
773 default: break;
775 FIXME("unknown notification %x\n", unicodeNotificationCode);
776 return unicodeNotificationCode;
779 /* forwards header notifications to listview parent */
780 static LRESULT notify_forward_header(const LISTVIEW_INFO *infoPtr, const NMHEADERW *lpnmh)
782 NMHEADERA nmhA;
783 HDITEMA hditema;
784 HD_TEXTFILTERA textfilter;
785 LPSTR text = NULL, filter = NULL;
786 LRESULT ret;
788 /* on unicode format exit earlier */
789 if (infoPtr->notifyFormat == NFR_UNICODE)
790 return SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, lpnmh->hdr.idFrom,
791 (LPARAM)lpnmh);
793 /* header always supplies unicode notifications,
794 all we have to do is to convert strings to ANSI */
795 nmhA = *(const NMHEADERA*)lpnmh;
796 if (lpnmh->pitem)
798 hditema = *(HDITEMA*)lpnmh->pitem;
799 nmhA.pitem = &hditema;
800 /* convert item text */
801 if (lpnmh->pitem->mask & HDI_TEXT)
803 hditema.pszText = NULL;
804 Str_SetPtrWtoA(&hditema.pszText, lpnmh->pitem->pszText);
805 text = hditema.pszText;
807 /* convert filter text */
808 if ((lpnmh->pitem->mask & HDI_FILTER) && (lpnmh->pitem->type == HDFT_ISSTRING) &&
809 lpnmh->pitem->pvFilter)
811 hditema.pvFilter = &textfilter;
812 textfilter = *(HD_TEXTFILTERA*)(lpnmh->pitem->pvFilter);
813 textfilter.pszText = NULL;
814 Str_SetPtrWtoA(&textfilter.pszText, ((HD_TEXTFILTERW*)lpnmh->pitem->pvFilter)->pszText);
815 filter = textfilter.pszText;
818 nmhA.hdr.code = get_ansi_notification(lpnmh->hdr.code);
820 ret = SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmhA.hdr.idFrom,
821 (LPARAM)&nmhA);
823 /* cleanup */
824 Free(text);
825 Free(filter);
827 return ret;
830 static LRESULT notify_hdr(const LISTVIEW_INFO *infoPtr, INT code, LPNMHDR pnmh)
832 LRESULT result;
834 TRACE("(code=%d)\n", code);
836 pnmh->hwndFrom = infoPtr->hwndSelf;
837 pnmh->idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
838 pnmh->code = code;
839 result = SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, pnmh->idFrom, (LPARAM)pnmh);
841 TRACE(" <= %ld\n", result);
843 return result;
846 static inline BOOL notify(const LISTVIEW_INFO *infoPtr, INT code)
848 NMHDR nmh;
849 HWND hwnd = infoPtr->hwndSelf;
850 notify_hdr(infoPtr, code, &nmh);
851 return IsWindow(hwnd);
854 static inline void notify_itemactivate(const LISTVIEW_INFO *infoPtr, const LVHITTESTINFO *htInfo)
856 NMITEMACTIVATE nmia;
857 LVITEMW item;
859 if (htInfo) {
860 nmia.uNewState = 0;
861 nmia.uOldState = 0;
862 nmia.uChanged = 0;
863 nmia.uKeyFlags = 0;
865 item.mask = LVIF_PARAM|LVIF_STATE;
866 item.iItem = htInfo->iItem;
867 item.iSubItem = 0;
868 item.stateMask = (UINT)-1;
869 if (LISTVIEW_GetItemT(infoPtr, &item, TRUE)) {
870 nmia.lParam = item.lParam;
871 nmia.uOldState = item.state;
872 nmia.uNewState = item.state | LVIS_ACTIVATING;
873 nmia.uChanged = LVIF_STATE;
876 nmia.iItem = htInfo->iItem;
877 nmia.iSubItem = htInfo->iSubItem;
878 nmia.ptAction = htInfo->pt;
880 if (GetKeyState(VK_SHIFT) & 0x8000) nmia.uKeyFlags |= LVKF_SHIFT;
881 if (GetKeyState(VK_CONTROL) & 0x8000) nmia.uKeyFlags |= LVKF_CONTROL;
882 if (GetKeyState(VK_MENU) & 0x8000) nmia.uKeyFlags |= LVKF_ALT;
884 notify_hdr(infoPtr, LVN_ITEMACTIVATE, (LPNMHDR)&nmia);
887 static inline LRESULT notify_listview(const LISTVIEW_INFO *infoPtr, INT code, LPNMLISTVIEW plvnm)
889 TRACE("(code=%d, plvnm=%s)\n", code, debugnmlistview(plvnm));
890 return notify_hdr(infoPtr, code, (LPNMHDR)plvnm);
893 static BOOL notify_click(const LISTVIEW_INFO *infoPtr, INT code, const LVHITTESTINFO *lvht)
895 NMITEMACTIVATE nmia;
896 LVITEMW item;
897 HWND hwnd = infoPtr->hwndSelf;
899 TRACE("code=%d, lvht=%s\n", code, debuglvhittestinfo(lvht));
900 ZeroMemory(&nmia, sizeof(nmia));
901 nmia.iItem = lvht->iItem;
902 nmia.iSubItem = lvht->iSubItem;
903 nmia.ptAction = lvht->pt;
904 item.mask = LVIF_PARAM;
905 item.iItem = lvht->iItem;
906 item.iSubItem = 0;
907 if (LISTVIEW_GetItemT(infoPtr, &item, TRUE)) nmia.lParam = item.lParam;
908 notify_hdr(infoPtr, code, (LPNMHDR)&nmia);
909 return IsWindow(hwnd);
912 static BOOL notify_deleteitem(const LISTVIEW_INFO *infoPtr, INT nItem)
914 NMLISTVIEW nmlv;
915 LVITEMW item;
916 HWND hwnd = infoPtr->hwndSelf;
918 ZeroMemory(&nmlv, sizeof (NMLISTVIEW));
919 nmlv.iItem = nItem;
920 item.mask = LVIF_PARAM;
921 item.iItem = nItem;
922 item.iSubItem = 0;
923 if (LISTVIEW_GetItemT(infoPtr, &item, TRUE)) nmlv.lParam = item.lParam;
924 notify_listview(infoPtr, LVN_DELETEITEM, &nmlv);
925 return IsWindow(hwnd);
929 Send notification. depends on dispinfoW having same
930 structure as dispinfoA.
931 infoPtr : listview struct
932 code : *Unicode* notification code
933 pdi : dispinfo structure (can be unicode or ansi)
934 isW : TRUE if dispinfo is Unicode
936 static BOOL notify_dispinfoT(const LISTVIEW_INFO *infoPtr, UINT code, LPNMLVDISPINFOW pdi, BOOL isW)
938 INT length = 0, ret_length;
939 LPWSTR buffer = NULL, ret_text;
940 BOOL return_ansi = FALSE;
941 BOOL return_unicode = FALSE;
942 BOOL ret;
944 if ((pdi->item.mask & LVIF_TEXT) && is_text(pdi->item.pszText))
946 return_unicode = ( isW && infoPtr->notifyFormat == NFR_ANSI);
947 return_ansi = (!isW && infoPtr->notifyFormat == NFR_UNICODE);
950 ret_length = pdi->item.cchTextMax;
951 ret_text = pdi->item.pszText;
953 if (return_unicode || return_ansi)
955 if (code != LVN_GETDISPINFOW)
957 length = return_ansi ?
958 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pdi->item.pszText, -1, NULL, 0):
959 WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, NULL, 0, NULL, NULL);
961 else
963 length = pdi->item.cchTextMax;
964 *pdi->item.pszText = 0; /* make sure we don't process garbage */
967 buffer = Alloc( (return_ansi ? sizeof(WCHAR) : sizeof(CHAR)) * length);
968 if (!buffer) return FALSE;
970 if (return_ansi)
971 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pdi->item.pszText, -1,
972 buffer, length);
973 else
974 WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, (LPSTR) buffer,
975 length, NULL, NULL);
977 pdi->item.pszText = buffer;
978 pdi->item.cchTextMax = length;
981 if (infoPtr->notifyFormat == NFR_ANSI)
982 code = get_ansi_notification(code);
984 TRACE(" pdi->item=%s\n", debuglvitem_t(&pdi->item, infoPtr->notifyFormat != NFR_ANSI));
985 ret = notify_hdr(infoPtr, code, &pdi->hdr);
986 TRACE(" resulting code=%d\n", pdi->hdr.code);
988 if (return_ansi || return_unicode)
990 if (return_ansi && (pdi->hdr.code == LVN_GETDISPINFOA))
992 strcpy((char*)ret_text, (char*)pdi->item.pszText);
994 else if (return_unicode && (pdi->hdr.code == LVN_GETDISPINFOW))
996 strcpyW(ret_text, pdi->item.pszText);
998 else if (return_ansi) /* note : pointer can be changed by app ! */
1000 WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, (LPSTR) ret_text,
1001 ret_length, NULL, NULL);
1003 else
1004 MultiByteToWideChar(CP_ACP, 0, (LPSTR) pdi->item.pszText, -1,
1005 ret_text, ret_length);
1007 pdi->item.pszText = ret_text; /* restores our buffer */
1008 pdi->item.cchTextMax = ret_length;
1010 Free(buffer);
1011 return ret;
1014 /* if dipsinfo holder changed notification code then convert */
1015 if (!isW && (pdi->hdr.code == LVN_GETDISPINFOW) && (pdi->item.mask & LVIF_TEXT))
1017 length = WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, NULL, 0, NULL, NULL);
1019 buffer = Alloc(length * sizeof(CHAR));
1020 if (!buffer) return FALSE;
1022 WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, (LPSTR) buffer,
1023 ret_length, NULL, NULL);
1025 strcpy((LPSTR)pdi->item.pszText, (LPSTR)buffer);
1026 Free(buffer);
1029 return ret;
1032 static void customdraw_fill(NMLVCUSTOMDRAW *lpnmlvcd, const LISTVIEW_INFO *infoPtr, HDC hdc,
1033 const RECT *rcBounds, const LVITEMW *lplvItem)
1035 ZeroMemory(lpnmlvcd, sizeof(NMLVCUSTOMDRAW));
1036 lpnmlvcd->nmcd.hdc = hdc;
1037 lpnmlvcd->nmcd.rc = *rcBounds;
1038 lpnmlvcd->clrTextBk = infoPtr->clrTextBk;
1039 lpnmlvcd->clrText = infoPtr->clrText;
1040 if (!lplvItem) return;
1041 lpnmlvcd->nmcd.dwItemSpec = lplvItem->iItem + 1;
1042 lpnmlvcd->iSubItem = lplvItem->iSubItem;
1043 if (lplvItem->state & LVIS_SELECTED) lpnmlvcd->nmcd.uItemState |= CDIS_SELECTED;
1044 if (lplvItem->state & LVIS_FOCUSED) lpnmlvcd->nmcd.uItemState |= CDIS_FOCUS;
1045 if (lplvItem->iItem == infoPtr->nHotItem) lpnmlvcd->nmcd.uItemState |= CDIS_HOT;
1046 lpnmlvcd->nmcd.lItemlParam = lplvItem->lParam;
1049 static inline DWORD notify_customdraw (const LISTVIEW_INFO *infoPtr, DWORD dwDrawStage, NMLVCUSTOMDRAW *lpnmlvcd)
1051 BOOL isForItem = (lpnmlvcd->nmcd.dwItemSpec != 0);
1052 DWORD result;
1054 lpnmlvcd->nmcd.dwDrawStage = dwDrawStage;
1055 if (isForItem) lpnmlvcd->nmcd.dwDrawStage |= CDDS_ITEM;
1056 if (lpnmlvcd->iSubItem) lpnmlvcd->nmcd.dwDrawStage |= CDDS_SUBITEM;
1057 if (isForItem) lpnmlvcd->nmcd.dwItemSpec--;
1058 result = notify_hdr(infoPtr, NM_CUSTOMDRAW, &lpnmlvcd->nmcd.hdr);
1059 if (isForItem) lpnmlvcd->nmcd.dwItemSpec++;
1060 return result;
1063 static void prepaint_setup (const LISTVIEW_INFO *infoPtr, HDC hdc, NMLVCUSTOMDRAW *lpnmlvcd, BOOL SubItem)
1065 if (lpnmlvcd->clrTextBk == CLR_DEFAULT)
1066 lpnmlvcd->clrTextBk = comctl32_color.clrWindow;
1067 if (lpnmlvcd->clrText == CLR_DEFAULT)
1068 lpnmlvcd->clrText = comctl32_color.clrWindowText;
1070 /* apparently, for selected items, we have to override the returned values */
1071 if (!SubItem)
1073 if (lpnmlvcd->nmcd.uItemState & CDIS_SELECTED)
1075 if (infoPtr->bFocus)
1077 lpnmlvcd->clrTextBk = comctl32_color.clrHighlight;
1078 lpnmlvcd->clrText = comctl32_color.clrHighlightText;
1080 else if (infoPtr->dwStyle & LVS_SHOWSELALWAYS)
1082 lpnmlvcd->clrTextBk = comctl32_color.clr3dFace;
1083 lpnmlvcd->clrText = comctl32_color.clrBtnText;
1088 /* Set the text attributes */
1089 if (lpnmlvcd->clrTextBk != CLR_NONE)
1091 SetBkMode(hdc, OPAQUE);
1092 SetBkColor(hdc,lpnmlvcd->clrTextBk);
1094 else
1095 SetBkMode(hdc, TRANSPARENT);
1096 SetTextColor(hdc, lpnmlvcd->clrText);
1099 static inline DWORD notify_postpaint (const LISTVIEW_INFO *infoPtr, NMLVCUSTOMDRAW *lpnmlvcd)
1101 return notify_customdraw(infoPtr, CDDS_POSTPAINT, lpnmlvcd);
1104 /* returns TRUE when repaint needed, FALSE otherwise */
1105 static BOOL notify_measureitem(LISTVIEW_INFO *infoPtr)
1107 MEASUREITEMSTRUCT mis;
1108 mis.CtlType = ODT_LISTVIEW;
1109 mis.CtlID = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1110 mis.itemID = -1;
1111 mis.itemWidth = 0;
1112 mis.itemData = 0;
1113 mis.itemHeight= infoPtr->nItemHeight;
1114 SendMessageW(infoPtr->hwndNotify, WM_MEASUREITEM, mis.CtlID, (LPARAM)&mis);
1115 if (infoPtr->nItemHeight != max(mis.itemHeight, 1))
1117 infoPtr->nMeasureItemHeight = infoPtr->nItemHeight = max(mis.itemHeight, 1);
1118 return TRUE;
1120 return FALSE;
1123 /******** Item iterator functions **********************************/
1125 static RANGES ranges_create(int count);
1126 static void ranges_destroy(RANGES ranges);
1127 static BOOL ranges_add(RANGES ranges, RANGE range);
1128 static BOOL ranges_del(RANGES ranges, RANGE range);
1129 static void ranges_dump(RANGES ranges);
1131 static inline BOOL ranges_additem(RANGES ranges, INT nItem)
1133 RANGE range = { nItem, nItem + 1 };
1135 return ranges_add(ranges, range);
1138 static inline BOOL ranges_delitem(RANGES ranges, INT nItem)
1140 RANGE range = { nItem, nItem + 1 };
1142 return ranges_del(ranges, range);
1145 /***
1146 * ITERATOR DOCUMENTATION
1148 * The iterator functions allow for easy, and convenient iteration
1149 * over items of interest in the list. Typically, you create a
1150 * iterator, use it, and destroy it, as such:
1151 * ITERATOR i;
1153 * iterator_xxxitems(&i, ...);
1154 * while (iterator_{prev,next}(&i)
1156 * //code which uses i.nItem
1158 * iterator_destroy(&i);
1160 * where xxx is either: framed, or visible.
1161 * Note that it is important that the code destroys the iterator
1162 * after it's done with it, as the creation of the iterator may
1163 * allocate memory, which thus needs to be freed.
1165 * You can iterate both forwards, and backwards through the list,
1166 * by using iterator_next or iterator_prev respectively.
1168 * Lower numbered items are draw on top of higher number items in
1169 * LVS_ICON, and LVS_SMALLICON (which are the only modes where
1170 * items may overlap). So, to test items, you should use
1171 * iterator_next
1172 * which lists the items top to bottom (in Z-order).
1173 * For drawing items, you should use
1174 * iterator_prev
1175 * which lists the items bottom to top (in Z-order).
1176 * If you keep iterating over the items after the end-of-items
1177 * marker (-1) is returned, the iterator will start from the
1178 * beginning. Typically, you don't need to test for -1,
1179 * because iterator_{next,prev} will return TRUE if more items
1180 * are to be iterated over, or FALSE otherwise.
1182 * Note: the iterator is defined to be bidirectional. That is,
1183 * any number of prev followed by any number of next, or
1184 * five versa, should leave the iterator at the same item:
1185 * prev * n, next * n = next * n, prev * n
1187 * The iterator has a notion of an out-of-order, special item,
1188 * which sits at the start of the list. This is used in
1189 * LVS_ICON, and LVS_SMALLICON mode to handle the focused item,
1190 * which needs to be first, as it may overlap other items.
1192 * The code is a bit messy because we have:
1193 * - a special item to deal with
1194 * - simple range, or composite range
1195 * - empty range.
1196 * If you find bugs, or want to add features, please make sure you
1197 * always check/modify *both* iterator_prev, and iterator_next.
1200 /****
1201 * This function iterates through the items in increasing order,
1202 * but prefixed by the special item, then -1. That is:
1203 * special, 1, 2, 3, ..., n, -1.
1204 * Each item is listed only once.
1206 static inline BOOL iterator_next(ITERATOR* i)
1208 if (i->nItem == -1)
1210 i->nItem = i->nSpecial;
1211 if (i->nItem != -1) return TRUE;
1213 if (i->nItem == i->nSpecial)
1215 if (i->ranges) i->index = 0;
1216 goto pickarange;
1219 i->nItem++;
1220 testitem:
1221 if (i->nItem == i->nSpecial) i->nItem++;
1222 if (i->nItem < i->range.upper) return TRUE;
1224 pickarange:
1225 if (i->ranges)
1227 if (i->index < DPA_GetPtrCount(i->ranges->hdpa))
1228 i->range = *(RANGE*)DPA_GetPtr(i->ranges->hdpa, i->index++);
1229 else goto end;
1231 else if (i->nItem >= i->range.upper) goto end;
1233 i->nItem = i->range.lower;
1234 if (i->nItem >= 0) goto testitem;
1235 end:
1236 i->nItem = -1;
1237 return FALSE;
1240 /****
1241 * This function iterates through the items in decreasing order,
1242 * followed by the special item, then -1. That is:
1243 * n, n-1, ..., 3, 2, 1, special, -1.
1244 * Each item is listed only once.
1246 static inline BOOL iterator_prev(ITERATOR* i)
1248 BOOL start = FALSE;
1250 if (i->nItem == -1)
1252 start = TRUE;
1253 if (i->ranges) i->index = DPA_GetPtrCount(i->ranges->hdpa);
1254 goto pickarange;
1256 if (i->nItem == i->nSpecial)
1258 i->nItem = -1;
1259 return FALSE;
1262 testitem:
1263 i->nItem--;
1264 if (i->nItem == i->nSpecial) i->nItem--;
1265 if (i->nItem >= i->range.lower) return TRUE;
1267 pickarange:
1268 if (i->ranges)
1270 if (i->index > 0)
1271 i->range = *(RANGE*)DPA_GetPtr(i->ranges->hdpa, --i->index);
1272 else goto end;
1274 else if (!start && i->nItem < i->range.lower) goto end;
1276 i->nItem = i->range.upper;
1277 if (i->nItem > 0) goto testitem;
1278 end:
1279 return (i->nItem = i->nSpecial) != -1;
1282 static RANGE iterator_range(const ITERATOR *i)
1284 RANGE range;
1286 if (!i->ranges) return i->range;
1288 if (DPA_GetPtrCount(i->ranges->hdpa) > 0)
1290 range.lower = (*(RANGE*)DPA_GetPtr(i->ranges->hdpa, 0)).lower;
1291 range.upper = (*(RANGE*)DPA_GetPtr(i->ranges->hdpa, DPA_GetPtrCount(i->ranges->hdpa) - 1)).upper;
1293 else range.lower = range.upper = 0;
1295 return range;
1298 /***
1299 * Releases resources associated with this ierator.
1301 static inline void iterator_destroy(const ITERATOR *i)
1303 ranges_destroy(i->ranges);
1306 /***
1307 * Create an empty iterator.
1309 static inline BOOL iterator_empty(ITERATOR* i)
1311 ZeroMemory(i, sizeof(*i));
1312 i->nItem = i->nSpecial = i->range.lower = i->range.upper = -1;
1313 return TRUE;
1316 /***
1317 * Create an iterator over a range.
1319 static inline BOOL iterator_rangeitems(ITERATOR* i, RANGE range)
1321 iterator_empty(i);
1322 i->range = range;
1323 return TRUE;
1326 /***
1327 * Create an iterator over a bunch of ranges.
1328 * Please note that the iterator will take ownership of the ranges,
1329 * and will free them upon destruction.
1331 static inline BOOL iterator_rangesitems(ITERATOR* i, RANGES ranges)
1333 iterator_empty(i);
1334 i->ranges = ranges;
1335 return TRUE;
1338 /***
1339 * Creates an iterator over the items which intersect frame.
1340 * Uses absolute coordinates rather than compensating for the current offset.
1342 static BOOL iterator_frameditems_absolute(ITERATOR* i, const LISTVIEW_INFO* infoPtr, const RECT *frame)
1344 RECT rcItem, rcTemp;
1346 /* in case we fail, we want to return an empty iterator */
1347 if (!iterator_empty(i)) return FALSE;
1349 TRACE("(frame=%s)\n", wine_dbgstr_rect(frame));
1351 if (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON)
1353 INT nItem;
1355 if (infoPtr->uView == LV_VIEW_ICON && infoPtr->nFocusedItem != -1)
1357 LISTVIEW_GetItemBox(infoPtr, infoPtr->nFocusedItem, &rcItem);
1358 if (IntersectRect(&rcTemp, &rcItem, frame))
1359 i->nSpecial = infoPtr->nFocusedItem;
1361 if (!(iterator_rangesitems(i, ranges_create(50)))) return FALSE;
1362 /* to do better here, we need to have PosX, and PosY sorted */
1363 TRACE("building icon ranges:\n");
1364 for (nItem = 0; nItem < infoPtr->nItemCount; nItem++)
1366 rcItem.left = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosX, nItem);
1367 rcItem.top = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosY, nItem);
1368 rcItem.right = rcItem.left + infoPtr->nItemWidth;
1369 rcItem.bottom = rcItem.top + infoPtr->nItemHeight;
1370 if (IntersectRect(&rcTemp, &rcItem, frame))
1371 ranges_additem(i->ranges, nItem);
1373 return TRUE;
1375 else if (infoPtr->uView == LV_VIEW_DETAILS)
1377 RANGE range;
1379 if (frame->left >= infoPtr->nItemWidth) return TRUE;
1380 if (frame->top >= infoPtr->nItemHeight * infoPtr->nItemCount) return TRUE;
1382 range.lower = max(frame->top / infoPtr->nItemHeight, 0);
1383 range.upper = min((frame->bottom - 1) / infoPtr->nItemHeight, infoPtr->nItemCount - 1) + 1;
1384 if (range.upper <= range.lower) return TRUE;
1385 if (!iterator_rangeitems(i, range)) return FALSE;
1386 TRACE(" report=%s\n", debugrange(&i->range));
1388 else
1390 INT nPerCol = max((infoPtr->rcList.bottom - infoPtr->rcList.top) / infoPtr->nItemHeight, 1);
1391 INT nFirstRow = max(frame->top / infoPtr->nItemHeight, 0);
1392 INT nLastRow = min((frame->bottom - 1) / infoPtr->nItemHeight, nPerCol - 1);
1393 INT nFirstCol;
1394 INT nLastCol;
1395 INT lower;
1396 RANGE item_range;
1397 INT nCol;
1399 if (infoPtr->nItemWidth)
1401 nFirstCol = max(frame->left / infoPtr->nItemWidth, 0);
1402 nLastCol = min((frame->right - 1) / infoPtr->nItemWidth, (infoPtr->nItemCount + nPerCol - 1) / nPerCol);
1404 else
1406 nFirstCol = max(frame->left, 0);
1407 nLastCol = min(frame->right - 1, (infoPtr->nItemCount + nPerCol - 1) / nPerCol);
1410 lower = nFirstCol * nPerCol + nFirstRow;
1412 TRACE("nPerCol=%d, nFirstRow=%d, nLastRow=%d, nFirstCol=%d, nLastCol=%d, lower=%d\n",
1413 nPerCol, nFirstRow, nLastRow, nFirstCol, nLastCol, lower);
1415 if (nLastCol < nFirstCol || nLastRow < nFirstRow) return TRUE;
1417 if (!(iterator_rangesitems(i, ranges_create(nLastCol - nFirstCol + 1)))) return FALSE;
1418 TRACE("building list ranges:\n");
1419 for (nCol = nFirstCol; nCol <= nLastCol; nCol++)
1421 item_range.lower = nCol * nPerCol + nFirstRow;
1422 if(item_range.lower >= infoPtr->nItemCount) break;
1423 item_range.upper = min(nCol * nPerCol + nLastRow + 1, infoPtr->nItemCount);
1424 TRACE(" list=%s\n", debugrange(&item_range));
1425 ranges_add(i->ranges, item_range);
1429 return TRUE;
1432 /***
1433 * Creates an iterator over the items which intersect lprc.
1435 static BOOL iterator_frameditems(ITERATOR* i, const LISTVIEW_INFO* infoPtr, const RECT *lprc)
1437 RECT frame = *lprc;
1438 POINT Origin;
1440 TRACE("(lprc=%s)\n", wine_dbgstr_rect(lprc));
1442 LISTVIEW_GetOrigin(infoPtr, &Origin);
1443 OffsetRect(&frame, -Origin.x, -Origin.y);
1445 return iterator_frameditems_absolute(i, infoPtr, &frame);
1448 /***
1449 * Creates an iterator over the items which intersect the visible region of hdc.
1451 static BOOL iterator_visibleitems(ITERATOR *i, const LISTVIEW_INFO *infoPtr, HDC hdc)
1453 POINT Origin, Position;
1454 RECT rcItem, rcClip;
1455 INT rgntype;
1457 rgntype = GetClipBox(hdc, &rcClip);
1458 if (rgntype == NULLREGION) return iterator_empty(i);
1459 if (!iterator_frameditems(i, infoPtr, &rcClip)) return FALSE;
1460 if (rgntype == SIMPLEREGION) return TRUE;
1462 /* first deal with the special item */
1463 if (i->nSpecial != -1)
1465 LISTVIEW_GetItemBox(infoPtr, i->nSpecial, &rcItem);
1466 if (!RectVisible(hdc, &rcItem)) i->nSpecial = -1;
1469 /* if we can't deal with the region, we'll just go with the simple range */
1470 LISTVIEW_GetOrigin(infoPtr, &Origin);
1471 TRACE("building visible range:\n");
1472 if (!i->ranges && i->range.lower < i->range.upper)
1474 if (!(i->ranges = ranges_create(50))) return TRUE;
1475 if (!ranges_add(i->ranges, i->range))
1477 ranges_destroy(i->ranges);
1478 i->ranges = 0;
1479 return TRUE;
1483 /* now delete the invisible items from the list */
1484 while(iterator_next(i))
1486 LISTVIEW_GetItemOrigin(infoPtr, i->nItem, &Position);
1487 rcItem.left = (infoPtr->uView == LV_VIEW_DETAILS) ? Origin.x : Position.x + Origin.x;
1488 rcItem.top = Position.y + Origin.y;
1489 rcItem.right = rcItem.left + infoPtr->nItemWidth;
1490 rcItem.bottom = rcItem.top + infoPtr->nItemHeight;
1491 if (!RectVisible(hdc, &rcItem))
1492 ranges_delitem(i->ranges, i->nItem);
1494 /* the iterator should restart on the next iterator_next */
1495 TRACE("done\n");
1497 return TRUE;
1500 /* Remove common elements from two iterators */
1501 /* Passed iterators have to point on the first elements */
1502 static BOOL iterator_remove_common_items(ITERATOR *iter1, ITERATOR *iter2)
1504 if(!iter1->ranges || !iter2->ranges) {
1505 int lower, upper;
1507 if(iter1->ranges || iter2->ranges ||
1508 (iter1->range.lower<iter2->range.lower && iter1->range.upper>iter2->range.upper) ||
1509 (iter1->range.lower>iter2->range.lower && iter1->range.upper<iter2->range.upper)) {
1510 ERR("result is not a one range iterator\n");
1511 return FALSE;
1514 if(iter1->range.lower==-1 || iter2->range.lower==-1)
1515 return TRUE;
1517 lower = iter1->range.lower;
1518 upper = iter1->range.upper;
1520 if(lower < iter2->range.lower)
1521 iter1->range.upper = iter2->range.lower;
1522 else if(upper > iter2->range.upper)
1523 iter1->range.lower = iter2->range.upper;
1524 else
1525 iter1->range.lower = iter1->range.upper = -1;
1527 if(iter2->range.lower < lower)
1528 iter2->range.upper = lower;
1529 else if(iter2->range.upper > upper)
1530 iter2->range.lower = upper;
1531 else
1532 iter2->range.lower = iter2->range.upper = -1;
1534 return TRUE;
1537 iterator_next(iter1);
1538 iterator_next(iter2);
1540 while(1) {
1541 if(iter1->nItem==-1 || iter2->nItem==-1)
1542 break;
1544 if(iter1->nItem == iter2->nItem) {
1545 int delete = iter1->nItem;
1547 iterator_prev(iter1);
1548 iterator_prev(iter2);
1549 ranges_delitem(iter1->ranges, delete);
1550 ranges_delitem(iter2->ranges, delete);
1551 iterator_next(iter1);
1552 iterator_next(iter2);
1553 } else if(iter1->nItem > iter2->nItem)
1554 iterator_next(iter2);
1555 else
1556 iterator_next(iter1);
1559 iter1->nItem = iter1->range.lower = iter1->range.upper = -1;
1560 iter2->nItem = iter2->range.lower = iter2->range.upper = -1;
1561 return TRUE;
1564 /******** Misc helper functions ************************************/
1566 static inline LRESULT CallWindowProcT(WNDPROC proc, HWND hwnd, UINT uMsg,
1567 WPARAM wParam, LPARAM lParam, BOOL isW)
1569 if (isW) return CallWindowProcW(proc, hwnd, uMsg, wParam, lParam);
1570 else return CallWindowProcA(proc, hwnd, uMsg, wParam, lParam);
1573 static inline BOOL is_autoarrange(const LISTVIEW_INFO *infoPtr)
1575 return ((infoPtr->dwStyle & LVS_AUTOARRANGE) || infoPtr->bAutoarrange) &&
1576 (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON);
1579 static void toggle_checkbox_state(LISTVIEW_INFO *infoPtr, INT nItem)
1581 DWORD state = STATEIMAGEINDEX(LISTVIEW_GetItemState(infoPtr, nItem, LVIS_STATEIMAGEMASK));
1582 if(state == 1 || state == 2)
1584 LVITEMW lvitem;
1585 state ^= 3;
1586 lvitem.state = INDEXTOSTATEIMAGEMASK(state);
1587 lvitem.stateMask = LVIS_STATEIMAGEMASK;
1588 LISTVIEW_SetItemState(infoPtr, nItem, &lvitem);
1592 /* this should be called after window style got updated,
1593 it used to reset view state to match current window style */
1594 static inline void map_style_view(LISTVIEW_INFO *infoPtr)
1596 switch (infoPtr->dwStyle & LVS_TYPEMASK)
1598 case LVS_ICON:
1599 infoPtr->uView = LV_VIEW_ICON;
1600 break;
1601 case LVS_REPORT:
1602 infoPtr->uView = LV_VIEW_DETAILS;
1603 break;
1604 case LVS_SMALLICON:
1605 infoPtr->uView = LV_VIEW_SMALLICON;
1606 break;
1607 case LVS_LIST:
1608 infoPtr->uView = LV_VIEW_LIST;
1612 /* computes next item id value */
1613 static DWORD get_next_itemid(const LISTVIEW_INFO *infoPtr)
1615 INT count = DPA_GetPtrCount(infoPtr->hdpaItemIds);
1617 if (count > 0)
1619 ITEM_ID *lpID = DPA_GetPtr(infoPtr->hdpaItemIds, count - 1);
1620 return lpID->id + 1;
1622 return 0;
1625 /******** Internal API functions ************************************/
1627 static inline COLUMN_INFO * LISTVIEW_GetColumnInfo(const LISTVIEW_INFO *infoPtr, INT nSubItem)
1629 static COLUMN_INFO mainItem;
1631 if (nSubItem == 0 && DPA_GetPtrCount(infoPtr->hdpaColumns) == 0) return &mainItem;
1632 assert (nSubItem >= 0 && nSubItem < DPA_GetPtrCount(infoPtr->hdpaColumns));
1634 /* update cached column rectangles */
1635 if (infoPtr->colRectsDirty)
1637 COLUMN_INFO *info;
1638 LISTVIEW_INFO *Ptr = (LISTVIEW_INFO*)infoPtr;
1639 INT i;
1641 for (i = 0; i < DPA_GetPtrCount(infoPtr->hdpaColumns); i++) {
1642 info = DPA_GetPtr(infoPtr->hdpaColumns, i);
1643 SendMessageW(infoPtr->hwndHeader, HDM_GETITEMRECT, i, (LPARAM)&info->rcHeader);
1645 Ptr->colRectsDirty = FALSE;
1648 return DPA_GetPtr(infoPtr->hdpaColumns, nSubItem);
1651 static INT LISTVIEW_CreateHeader(LISTVIEW_INFO *infoPtr)
1653 DWORD dFlags = WS_CHILD | HDS_HORZ | HDS_FULLDRAG | HDS_DRAGDROP;
1654 HINSTANCE hInst;
1656 if (infoPtr->hwndHeader) return 0;
1658 TRACE("Creating header for list %p\n", infoPtr->hwndSelf);
1660 /* setup creation flags */
1661 dFlags |= (LVS_NOSORTHEADER & infoPtr->dwStyle) ? 0 : HDS_BUTTONS;
1662 dFlags |= (LVS_NOCOLUMNHEADER & infoPtr->dwStyle) ? HDS_HIDDEN : 0;
1664 hInst = (HINSTANCE)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_HINSTANCE);
1666 /* create header */
1667 infoPtr->hwndHeader = CreateWindowW(WC_HEADERW, NULL, dFlags,
1668 0, 0, 0, 0, infoPtr->hwndSelf, NULL, hInst, NULL);
1669 if (!infoPtr->hwndHeader) return -1;
1671 /* set header unicode format */
1672 SendMessageW(infoPtr->hwndHeader, HDM_SETUNICODEFORMAT, TRUE, 0);
1674 /* set header font */
1675 SendMessageW(infoPtr->hwndHeader, WM_SETFONT, (WPARAM)infoPtr->hFont, TRUE);
1677 LISTVIEW_UpdateSize(infoPtr);
1679 return 0;
1682 static inline void LISTVIEW_GetHeaderRect(const LISTVIEW_INFO *infoPtr, INT nSubItem, LPRECT lprc)
1684 *lprc = LISTVIEW_GetColumnInfo(infoPtr, nSubItem)->rcHeader;
1687 static inline BOOL LISTVIEW_IsHeaderEnabled(const LISTVIEW_INFO *infoPtr)
1689 return (infoPtr->uView == LV_VIEW_DETAILS ||
1690 infoPtr->dwLvExStyle & LVS_EX_HEADERINALLVIEWS) &&
1691 !(infoPtr->dwStyle & LVS_NOCOLUMNHEADER);
1694 static inline BOOL LISTVIEW_GetItemW(const LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem)
1696 return LISTVIEW_GetItemT(infoPtr, lpLVItem, TRUE);
1699 /* used to handle collapse main item column case */
1700 static inline BOOL LISTVIEW_DrawFocusRect(const LISTVIEW_INFO *infoPtr, HDC hdc)
1702 return (infoPtr->rcFocus.left < infoPtr->rcFocus.right) ?
1703 DrawFocusRect(hdc, &infoPtr->rcFocus) : FALSE;
1706 /* Listview invalidation functions: use _only_ these functions to invalidate */
1708 static inline BOOL is_redrawing(const LISTVIEW_INFO *infoPtr)
1710 return infoPtr->bRedraw;
1713 static inline void LISTVIEW_InvalidateRect(const LISTVIEW_INFO *infoPtr, const RECT* rect)
1715 if(!is_redrawing(infoPtr)) return;
1716 TRACE(" invalidating rect=%s\n", wine_dbgstr_rect(rect));
1717 InvalidateRect(infoPtr->hwndSelf, rect, TRUE);
1720 static inline void LISTVIEW_InvalidateItem(const LISTVIEW_INFO *infoPtr, INT nItem)
1722 RECT rcBox;
1724 if(!is_redrawing(infoPtr)) return;
1725 LISTVIEW_GetItemBox(infoPtr, nItem, &rcBox);
1726 LISTVIEW_InvalidateRect(infoPtr, &rcBox);
1729 static inline void LISTVIEW_InvalidateSubItem(const LISTVIEW_INFO *infoPtr, INT nItem, INT nSubItem)
1731 POINT Origin, Position;
1732 RECT rcBox;
1734 if(!is_redrawing(infoPtr)) return;
1735 assert (infoPtr->uView == LV_VIEW_DETAILS);
1736 LISTVIEW_GetOrigin(infoPtr, &Origin);
1737 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
1738 LISTVIEW_GetHeaderRect(infoPtr, nSubItem, &rcBox);
1739 rcBox.top = 0;
1740 rcBox.bottom = infoPtr->nItemHeight;
1741 OffsetRect(&rcBox, Origin.x + Position.x, Origin.y + Position.y);
1742 LISTVIEW_InvalidateRect(infoPtr, &rcBox);
1745 static inline void LISTVIEW_InvalidateList(const LISTVIEW_INFO *infoPtr)
1747 LISTVIEW_InvalidateRect(infoPtr, NULL);
1750 static inline void LISTVIEW_InvalidateColumn(const LISTVIEW_INFO *infoPtr, INT nColumn)
1752 RECT rcCol;
1754 if(!is_redrawing(infoPtr)) return;
1755 LISTVIEW_GetHeaderRect(infoPtr, nColumn, &rcCol);
1756 rcCol.top = infoPtr->rcList.top;
1757 rcCol.bottom = infoPtr->rcList.bottom;
1758 LISTVIEW_InvalidateRect(infoPtr, &rcCol);
1761 /***
1762 * DESCRIPTION:
1763 * Retrieves the number of items that can fit vertically in the client area.
1765 * PARAMETER(S):
1766 * [I] infoPtr : valid pointer to the listview structure
1768 * RETURN:
1769 * Number of items per row.
1771 static inline INT LISTVIEW_GetCountPerRow(const LISTVIEW_INFO *infoPtr)
1773 INT nListWidth = infoPtr->rcList.right - infoPtr->rcList.left;
1775 return max(nListWidth/(infoPtr->nItemWidth ? infoPtr->nItemWidth : 1), 1);
1778 /***
1779 * DESCRIPTION:
1780 * Retrieves the number of items that can fit horizontally in the client
1781 * area.
1783 * PARAMETER(S):
1784 * [I] infoPtr : valid pointer to the listview structure
1786 * RETURN:
1787 * Number of items per column.
1789 static inline INT LISTVIEW_GetCountPerColumn(const LISTVIEW_INFO *infoPtr)
1791 INT nListHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
1793 return max(nListHeight / infoPtr->nItemHeight, 1);
1797 /*************************************************************************
1798 * LISTVIEW_ProcessLetterKeys
1800 * Processes keyboard messages generated by pressing the letter keys
1801 * on the keyboard.
1802 * What this does is perform a case insensitive search from the
1803 * current position with the following quirks:
1804 * - If two chars or more are pressed in quick succession we search
1805 * for the corresponding string (e.g. 'abc').
1806 * - If there is a delay we wipe away the current search string and
1807 * restart with just that char.
1808 * - If the user keeps pressing the same character, whether slowly or
1809 * fast, so that the search string is entirely composed of this
1810 * character ('aaaaa' for instance), then we search for first item
1811 * that starting with that character.
1812 * - If the user types the above character in quick succession, then
1813 * we must also search for the corresponding string ('aaaaa'), and
1814 * go to that string if there is a match.
1816 * PARAMETERS
1817 * [I] hwnd : handle to the window
1818 * [I] charCode : the character code, the actual character
1819 * [I] keyData : key data
1821 * RETURNS
1823 * Zero.
1825 * BUGS
1827 * - The current implementation has a list of characters it will
1828 * accept and it ignores everything else. In particular it will
1829 * ignore accentuated characters which seems to match what
1830 * Windows does. But I'm not sure it makes sense to follow
1831 * Windows there.
1832 * - We don't sound a beep when the search fails.
1834 * SEE ALSO
1836 * TREEVIEW_ProcessLetterKeys
1838 static INT LISTVIEW_ProcessLetterKeys(LISTVIEW_INFO *infoPtr, WPARAM charCode, LPARAM keyData)
1840 WCHAR buffer[MAX_PATH];
1841 INT endidx, startidx;
1842 DWORD prevTime;
1843 LVITEMW item;
1844 INT nItem;
1845 INT diff;
1847 /* simple parameter checking */
1848 if (!charCode || !keyData || infoPtr->nItemCount == 0) return 0;
1850 /* only allow the valid WM_CHARs through */
1851 if (!isalnumW(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 charCode != '}' && charCode != '[' && charCode != '{' &&
1859 charCode != '/' && charCode != '?' && charCode != '>' &&
1860 charCode != '<' && charCode != ',' && charCode != '~')
1861 return 0;
1863 /* update the search parameters */
1864 prevTime = infoPtr->lastKeyPressTimestamp;
1865 infoPtr->lastKeyPressTimestamp = GetTickCount();
1866 diff = infoPtr->lastKeyPressTimestamp - prevTime;
1868 if (diff >= 0 && diff < KEY_DELAY)
1870 if (infoPtr->nSearchParamLength < MAX_PATH - 1)
1871 infoPtr->szSearchParam[infoPtr->nSearchParamLength++] = charCode;
1873 if (infoPtr->charCode != charCode)
1874 infoPtr->charCode = charCode = 0;
1876 else
1878 infoPtr->charCode = charCode;
1879 infoPtr->szSearchParam[0] = charCode;
1880 infoPtr->nSearchParamLength = 1;
1883 /* and search from the current position */
1884 nItem = -1;
1885 endidx = infoPtr->nItemCount;
1887 /* should start from next after focused item, so next item that matches
1888 will be selected, if there isn't any and focused matches it will be selected
1889 on second search stage from beginning of the list */
1890 if (infoPtr->nFocusedItem >= 0 && infoPtr->nItemCount > 1)
1891 startidx = infoPtr->nFocusedItem + 1;
1892 else
1893 startidx = 0;
1895 /* let application handle this for virtual listview */
1896 if (infoPtr->dwStyle & LVS_OWNERDATA)
1898 NMLVFINDITEMW nmlv;
1900 memset(&nmlv.lvfi, 0, sizeof(nmlv.lvfi));
1901 nmlv.lvfi.flags = (LVFI_WRAP | LVFI_PARTIAL);
1902 nmlv.lvfi.psz = infoPtr->szSearchParam;
1903 nmlv.iStart = startidx;
1905 infoPtr->szSearchParam[infoPtr->nSearchParamLength] = 0;
1907 nItem = notify_hdr(infoPtr, LVN_ODFINDITEMW, (LPNMHDR)&nmlv.hdr);
1909 else
1911 INT i = startidx;
1913 /* first search in [startidx, endidx), on failure continue in [0, startidx) */
1914 while (1)
1916 /* start from first item if not found with >= startidx */
1917 if (i == infoPtr->nItemCount && startidx > 0)
1919 endidx = startidx;
1920 startidx = 0;
1923 for (i = startidx; i < endidx; i++)
1925 /* retrieve text */
1926 item.mask = LVIF_TEXT;
1927 item.iItem = i;
1928 item.iSubItem = 0;
1929 item.pszText = buffer;
1930 item.cchTextMax = MAX_PATH;
1931 if (!LISTVIEW_GetItemW(infoPtr, &item)) return 0;
1933 if (lstrncmpiW(item.pszText, infoPtr->szSearchParam, infoPtr->nSearchParamLength) == 0)
1935 nItem = i;
1936 break;
1938 else if (nItem == -1 && lstrncmpiW(item.pszText, infoPtr->szSearchParam, 1) == 0)
1940 /* this would work but we must keep looking for a longer match */
1941 nItem = i;
1945 if ( nItem != -1 || /* found something */
1946 endidx != infoPtr->nItemCount || /* second search done */
1947 (startidx == 0 && endidx == infoPtr->nItemCount) /* full range for first search */ )
1948 break;
1952 if (nItem != -1)
1953 LISTVIEW_KeySelection(infoPtr, nItem, FALSE);
1955 return 0;
1958 /*************************************************************************
1959 * LISTVIEW_UpdateHeaderSize [Internal]
1961 * Function to resize the header control
1963 * PARAMS
1964 * [I] hwnd : handle to a window
1965 * [I] nNewScrollPos : scroll pos to set
1967 * RETURNS
1968 * None.
1970 static void LISTVIEW_UpdateHeaderSize(const LISTVIEW_INFO *infoPtr, INT nNewScrollPos)
1972 RECT winRect;
1973 POINT point[2];
1975 TRACE("nNewScrollPos=%d\n", nNewScrollPos);
1977 if (!infoPtr->hwndHeader) return;
1979 GetWindowRect(infoPtr->hwndHeader, &winRect);
1980 point[0].x = winRect.left;
1981 point[0].y = winRect.top;
1982 point[1].x = winRect.right;
1983 point[1].y = winRect.bottom;
1985 MapWindowPoints(HWND_DESKTOP, infoPtr->hwndSelf, point, 2);
1986 point[0].x = -nNewScrollPos;
1987 point[1].x += nNewScrollPos;
1989 SetWindowPos(infoPtr->hwndHeader,0,
1990 point[0].x,point[0].y,point[1].x,point[1].y,
1991 (infoPtr->dwStyle & LVS_NOCOLUMNHEADER) ? SWP_HIDEWINDOW : SWP_SHOWWINDOW |
1992 SWP_NOZORDER | SWP_NOACTIVATE);
1995 /***
1996 * DESCRIPTION:
1997 * Update the scrollbars. This functions should be called whenever
1998 * the content, size or view changes.
2000 * PARAMETER(S):
2001 * [I] infoPtr : valid pointer to the listview structure
2003 * RETURN:
2004 * None
2006 static void LISTVIEW_UpdateScroll(const LISTVIEW_INFO *infoPtr)
2008 SCROLLINFO horzInfo, vertInfo;
2009 INT dx, dy;
2011 if ((infoPtr->dwStyle & LVS_NOSCROLL) || !is_redrawing(infoPtr)) return;
2013 ZeroMemory(&horzInfo, sizeof(SCROLLINFO));
2014 horzInfo.cbSize = sizeof(SCROLLINFO);
2015 horzInfo.nPage = infoPtr->rcList.right - infoPtr->rcList.left;
2017 /* for now, we'll set info.nMax to the _count_, and adjust it later */
2018 if (infoPtr->uView == LV_VIEW_LIST)
2020 INT nPerCol = LISTVIEW_GetCountPerColumn(infoPtr);
2021 horzInfo.nMax = (infoPtr->nItemCount + nPerCol - 1) / nPerCol;
2023 /* scroll by at least one column per page */
2024 if(horzInfo.nPage < infoPtr->nItemWidth)
2025 horzInfo.nPage = infoPtr->nItemWidth;
2027 if (infoPtr->nItemWidth)
2028 horzInfo.nPage /= infoPtr->nItemWidth;
2030 else if (infoPtr->uView == LV_VIEW_DETAILS)
2032 horzInfo.nMax = infoPtr->nItemWidth;
2034 else /* LV_VIEW_ICON, or LV_VIEW_SMALLICON */
2036 RECT rcView;
2038 if (LISTVIEW_GetViewRect(infoPtr, &rcView)) horzInfo.nMax = rcView.right - rcView.left;
2041 if (LISTVIEW_IsHeaderEnabled(infoPtr))
2043 if (DPA_GetPtrCount(infoPtr->hdpaColumns))
2045 RECT rcHeader;
2046 INT index;
2048 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX,
2049 DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, 0);
2051 LISTVIEW_GetHeaderRect(infoPtr, index, &rcHeader);
2052 horzInfo.nMax = rcHeader.right;
2053 TRACE("horzInfo.nMax=%d\n", horzInfo.nMax);
2057 horzInfo.fMask = SIF_RANGE | SIF_PAGE;
2058 horzInfo.nMax = max(horzInfo.nMax - 1, 0);
2059 dx = GetScrollPos(infoPtr->hwndSelf, SB_HORZ);
2060 dx -= SetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &horzInfo, TRUE);
2061 TRACE("horzInfo=%s\n", debugscrollinfo(&horzInfo));
2063 /* Setting the horizontal scroll can change the listview size
2064 * (and potentially everything else) so we need to recompute
2065 * everything again for the vertical scroll
2068 ZeroMemory(&vertInfo, sizeof(SCROLLINFO));
2069 vertInfo.cbSize = sizeof(SCROLLINFO);
2070 vertInfo.nPage = infoPtr->rcList.bottom - infoPtr->rcList.top;
2072 if (infoPtr->uView == LV_VIEW_DETAILS)
2074 vertInfo.nMax = infoPtr->nItemCount;
2076 /* scroll by at least one page */
2077 if(vertInfo.nPage < infoPtr->nItemHeight)
2078 vertInfo.nPage = infoPtr->nItemHeight;
2080 if (infoPtr->nItemHeight > 0)
2081 vertInfo.nPage /= infoPtr->nItemHeight;
2083 else if (infoPtr->uView != LV_VIEW_LIST) /* LV_VIEW_ICON, or LV_VIEW_SMALLICON */
2085 RECT rcView;
2087 if (LISTVIEW_GetViewRect(infoPtr, &rcView)) vertInfo.nMax = rcView.bottom - rcView.top;
2090 vertInfo.fMask = SIF_RANGE | SIF_PAGE;
2091 vertInfo.nMax = max(vertInfo.nMax - 1, 0);
2092 dy = GetScrollPos(infoPtr->hwndSelf, SB_VERT);
2093 dy -= SetScrollInfo(infoPtr->hwndSelf, SB_VERT, &vertInfo, TRUE);
2094 TRACE("vertInfo=%s\n", debugscrollinfo(&vertInfo));
2096 /* Change of the range may have changed the scroll pos. If so move the content */
2097 if (dx != 0 || dy != 0)
2099 RECT listRect;
2100 listRect = infoPtr->rcList;
2101 ScrollWindowEx(infoPtr->hwndSelf, dx, dy, &listRect, &listRect, 0, 0,
2102 SW_ERASE | SW_INVALIDATE);
2105 /* Update the Header Control */
2106 if (infoPtr->hwndHeader)
2108 horzInfo.fMask = SIF_POS;
2109 GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &horzInfo);
2110 LISTVIEW_UpdateHeaderSize(infoPtr, horzInfo.nPos);
2115 /***
2116 * DESCRIPTION:
2117 * Shows/hides the focus rectangle.
2119 * PARAMETER(S):
2120 * [I] infoPtr : valid pointer to the listview structure
2121 * [I] fShow : TRUE to show the focus, FALSE to hide it.
2123 * RETURN:
2124 * None
2126 static void LISTVIEW_ShowFocusRect(const LISTVIEW_INFO *infoPtr, BOOL fShow)
2128 HDC hdc;
2130 TRACE("fShow=%d, nItem=%d\n", fShow, infoPtr->nFocusedItem);
2132 if (infoPtr->nFocusedItem < 0) return;
2134 /* we need some gymnastics in ICON mode to handle large items */
2135 if (infoPtr->uView == LV_VIEW_ICON)
2137 RECT rcBox;
2139 LISTVIEW_GetItemBox(infoPtr, infoPtr->nFocusedItem, &rcBox);
2140 if ((rcBox.bottom - rcBox.top) > infoPtr->nItemHeight)
2142 LISTVIEW_InvalidateRect(infoPtr, &rcBox);
2143 return;
2147 if (!(hdc = GetDC(infoPtr->hwndSelf))) return;
2149 /* for some reason, owner draw should work only in report mode */
2150 if ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (infoPtr->uView == LV_VIEW_DETAILS))
2152 DRAWITEMSTRUCT dis;
2153 LVITEMW item;
2155 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
2156 HFONT hOldFont = SelectObject(hdc, hFont);
2158 item.iItem = infoPtr->nFocusedItem;
2159 item.iSubItem = 0;
2160 item.mask = LVIF_PARAM;
2161 if (!LISTVIEW_GetItemW(infoPtr, &item)) goto done;
2163 ZeroMemory(&dis, sizeof(dis));
2164 dis.CtlType = ODT_LISTVIEW;
2165 dis.CtlID = (UINT)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
2166 dis.itemID = item.iItem;
2167 dis.itemAction = ODA_FOCUS;
2168 if (fShow) dis.itemState |= ODS_FOCUS;
2169 dis.hwndItem = infoPtr->hwndSelf;
2170 dis.hDC = hdc;
2171 LISTVIEW_GetItemBox(infoPtr, dis.itemID, &dis.rcItem);
2172 dis.itemData = item.lParam;
2174 SendMessageW(infoPtr->hwndNotify, WM_DRAWITEM, dis.CtlID, (LPARAM)&dis);
2176 SelectObject(hdc, hOldFont);
2178 else
2180 LISTVIEW_DrawFocusRect(infoPtr, hdc);
2182 done:
2183 ReleaseDC(infoPtr->hwndSelf, hdc);
2186 /***
2187 * Invalidates all visible selected items.
2189 static void LISTVIEW_InvalidateSelectedItems(const LISTVIEW_INFO *infoPtr)
2191 ITERATOR i;
2193 iterator_frameditems(&i, infoPtr, &infoPtr->rcList);
2194 while(iterator_next(&i))
2196 if (LISTVIEW_GetItemState(infoPtr, i.nItem, LVIS_SELECTED))
2197 LISTVIEW_InvalidateItem(infoPtr, i.nItem);
2199 iterator_destroy(&i);
2203 /***
2204 * DESCRIPTION: [INTERNAL]
2205 * Computes an item's (left,top) corner, relative to rcView.
2206 * That is, the position has NOT been made relative to the Origin.
2207 * This is deliberate, to avoid computing the Origin over, and
2208 * over again, when this function is called in a loop. Instead,
2209 * one can factor the computation of the Origin before the loop,
2210 * and offset the value returned by this function, on every iteration.
2212 * PARAMETER(S):
2213 * [I] infoPtr : valid pointer to the listview structure
2214 * [I] nItem : item number
2215 * [O] lpptOrig : item top, left corner
2217 * RETURN:
2218 * None.
2220 static void LISTVIEW_GetItemOrigin(const LISTVIEW_INFO *infoPtr, INT nItem, LPPOINT lpptPosition)
2222 assert(nItem >= 0 && nItem < infoPtr->nItemCount);
2224 if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
2226 lpptPosition->x = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosX, nItem);
2227 lpptPosition->y = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosY, nItem);
2229 else if (infoPtr->uView == LV_VIEW_LIST)
2231 INT nCountPerColumn = LISTVIEW_GetCountPerColumn(infoPtr);
2232 lpptPosition->x = nItem / nCountPerColumn * infoPtr->nItemWidth;
2233 lpptPosition->y = nItem % nCountPerColumn * infoPtr->nItemHeight;
2235 else /* LV_VIEW_DETAILS */
2237 lpptPosition->x = REPORT_MARGINX;
2238 /* item is always at zero indexed column */
2239 if (DPA_GetPtrCount(infoPtr->hdpaColumns) > 0)
2240 lpptPosition->x += LISTVIEW_GetColumnInfo(infoPtr, 0)->rcHeader.left;
2241 lpptPosition->y = nItem * infoPtr->nItemHeight;
2245 /***
2246 * DESCRIPTION: [INTERNAL]
2247 * Compute the rectangles of an item. This is to localize all
2248 * the computations in one place. If you are not interested in some
2249 * of these values, simply pass in a NULL -- the function is smart
2250 * enough to compute only what's necessary. The function computes
2251 * the standard rectangles (BOUNDS, ICON, LABEL) plus a non-standard
2252 * one, the BOX rectangle. This rectangle is very cheap to compute,
2253 * and is guaranteed to contain all the other rectangles. Computing
2254 * the ICON rect is also cheap, but all the others are potentially
2255 * expensive. This gives an easy and effective optimization when
2256 * searching (like point inclusion, or rectangle intersection):
2257 * first test against the BOX, and if TRUE, test against the desired
2258 * rectangle.
2259 * If the function does not have all the necessary information
2260 * to computed the requested rectangles, will crash with a
2261 * failed assertion. This is done so we catch all programming
2262 * errors, given that the function is called only from our code.
2264 * We have the following 'special' meanings for a few fields:
2265 * * If LVIS_FOCUSED is set, we assume the item has the focus
2266 * This is important in ICON mode, where it might get a larger
2267 * then usual rectangle
2269 * Please note that subitem support works only in REPORT mode.
2271 * PARAMETER(S):
2272 * [I] infoPtr : valid pointer to the listview structure
2273 * [I] lpLVItem : item to compute the measures for
2274 * [O] lprcBox : ptr to Box rectangle
2275 * Same as LVM_GETITEMRECT with LVIR_BOUNDS
2276 * [0] lprcSelectBox : ptr to select box rectangle
2277 * Same as LVM_GETITEMRECT with LVIR_SELECTEDBOUNDS
2278 * [O] lprcIcon : ptr to Icon rectangle
2279 * Same as LVM_GETITEMRECT with LVIR_ICON
2280 * [O] lprcStateIcon: ptr to State Icon rectangle
2281 * [O] lprcLabel : ptr to Label rectangle
2282 * Same as LVM_GETITEMRECT with LVIR_LABEL
2284 * RETURN:
2285 * None.
2287 static void LISTVIEW_GetItemMetrics(const LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem,
2288 LPRECT lprcBox, LPRECT lprcSelectBox,
2289 LPRECT lprcIcon, LPRECT lprcStateIcon, LPRECT lprcLabel)
2291 BOOL doSelectBox = FALSE, doIcon = FALSE, doLabel = FALSE, oversizedBox = FALSE;
2292 RECT Box, SelectBox, Icon, Label;
2293 COLUMN_INFO *lpColumnInfo = NULL;
2294 SIZE labelSize = { 0, 0 };
2296 TRACE("(lpLVItem=%s)\n", debuglvitem_t(lpLVItem, TRUE));
2298 /* Be smart and try to figure out the minimum we have to do */
2299 if (lpLVItem->iSubItem) assert(infoPtr->uView == LV_VIEW_DETAILS);
2300 if (infoPtr->uView == LV_VIEW_ICON && (lprcBox || lprcLabel))
2302 assert((lpLVItem->mask & LVIF_STATE) && (lpLVItem->stateMask & LVIS_FOCUSED));
2303 if (lpLVItem->state & LVIS_FOCUSED) oversizedBox = doLabel = TRUE;
2305 if (lprcSelectBox) doSelectBox = TRUE;
2306 if (lprcLabel) doLabel = TRUE;
2307 if (doLabel || lprcIcon || lprcStateIcon) doIcon = TRUE;
2308 if (doSelectBox)
2310 doIcon = TRUE;
2311 doLabel = TRUE;
2314 /************************************************************/
2315 /* compute the box rectangle (it should be cheap to do) */
2316 /************************************************************/
2317 if (lpLVItem->iSubItem || infoPtr->uView == LV_VIEW_DETAILS)
2318 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, lpLVItem->iSubItem);
2320 if (lpLVItem->iSubItem)
2322 Box = lpColumnInfo->rcHeader;
2324 else
2326 Box.left = 0;
2327 Box.right = infoPtr->nItemWidth;
2329 Box.top = 0;
2330 Box.bottom = infoPtr->nItemHeight;
2332 /******************************************************************/
2333 /* compute ICON bounding box (ala LVM_GETITEMRECT) and STATEICON */
2334 /******************************************************************/
2335 if (doIcon)
2337 LONG state_width = 0;
2339 if (infoPtr->himlState && lpLVItem->iSubItem == 0)
2340 state_width = infoPtr->iconStateSize.cx;
2342 if (infoPtr->uView == LV_VIEW_ICON)
2344 Icon.left = Box.left + state_width;
2345 if (infoPtr->himlNormal)
2346 Icon.left += (infoPtr->nItemWidth - infoPtr->iconSize.cx - state_width) / 2;
2347 Icon.top = Box.top + ICON_TOP_PADDING;
2348 Icon.right = Icon.left;
2349 Icon.bottom = Icon.top;
2350 if (infoPtr->himlNormal)
2352 Icon.right += infoPtr->iconSize.cx;
2353 Icon.bottom += infoPtr->iconSize.cy;
2356 else /* LV_VIEW_SMALLICON, LV_VIEW_LIST or LV_VIEW_DETAILS */
2358 Icon.left = Box.left + state_width;
2360 if (infoPtr->uView == LV_VIEW_DETAILS && lpLVItem->iSubItem == 0)
2362 /* we need the indent in report mode */
2363 assert(lpLVItem->mask & LVIF_INDENT);
2364 Icon.left += infoPtr->iconSize.cx * lpLVItem->iIndent + REPORT_MARGINX;
2367 Icon.top = Box.top;
2368 Icon.right = Icon.left;
2369 if (infoPtr->himlSmall &&
2370 (!lpColumnInfo || lpLVItem->iSubItem == 0 || (lpColumnInfo->fmt & LVCFMT_IMAGE) ||
2371 ((infoPtr->dwLvExStyle & LVS_EX_SUBITEMIMAGES) && lpLVItem->iImage != I_IMAGECALLBACK)))
2372 Icon.right += infoPtr->iconSize.cx;
2373 Icon.bottom = Icon.top + infoPtr->iconSize.cy;
2375 if(lprcIcon) *lprcIcon = Icon;
2376 TRACE(" - icon=%s\n", wine_dbgstr_rect(&Icon));
2378 /* TODO: is this correct? */
2379 if (lprcStateIcon)
2381 lprcStateIcon->left = Icon.left - state_width;
2382 lprcStateIcon->right = Icon.left;
2383 lprcStateIcon->top = Icon.top;
2384 lprcStateIcon->bottom = lprcStateIcon->top + infoPtr->iconSize.cy;
2385 TRACE(" - state icon=%s\n", wine_dbgstr_rect(lprcStateIcon));
2388 else Icon.right = 0;
2390 /************************************************************/
2391 /* compute LABEL bounding box (ala LVM_GETITEMRECT) */
2392 /************************************************************/
2393 if (doLabel)
2395 /* calculate how far to the right can the label stretch */
2396 Label.right = Box.right;
2397 if (infoPtr->uView == LV_VIEW_DETAILS)
2399 if (lpLVItem->iSubItem == 0)
2401 /* we need a zero based rect here */
2402 Label = lpColumnInfo->rcHeader;
2403 OffsetRect(&Label, -Label.left, 0);
2407 if (lpLVItem->iSubItem || ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && infoPtr->uView == LV_VIEW_DETAILS))
2409 labelSize.cx = infoPtr->nItemWidth;
2410 labelSize.cy = infoPtr->nItemHeight;
2411 goto calc_label;
2414 /* we need the text in non owner draw mode */
2415 assert(lpLVItem->mask & LVIF_TEXT);
2416 if (is_text(lpLVItem->pszText))
2418 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
2419 HDC hdc = GetDC(infoPtr->hwndSelf);
2420 HFONT hOldFont = SelectObject(hdc, hFont);
2421 UINT uFormat;
2422 RECT rcText;
2424 /* compute rough rectangle where the label will go */
2425 SetRectEmpty(&rcText);
2426 rcText.right = infoPtr->nItemWidth - TRAILING_LABEL_PADDING;
2427 rcText.bottom = infoPtr->nItemHeight;
2428 if (infoPtr->uView == LV_VIEW_ICON)
2429 rcText.bottom -= ICON_TOP_PADDING + infoPtr->iconSize.cy + ICON_BOTTOM_PADDING;
2431 /* now figure out the flags */
2432 if (infoPtr->uView == LV_VIEW_ICON)
2433 uFormat = oversizedBox ? LV_FL_DT_FLAGS : LV_ML_DT_FLAGS;
2434 else
2435 uFormat = LV_SL_DT_FLAGS;
2437 DrawTextW (hdc, lpLVItem->pszText, -1, &rcText, uFormat | DT_CALCRECT);
2439 if (rcText.right != rcText.left)
2440 labelSize.cx = min(rcText.right - rcText.left + TRAILING_LABEL_PADDING, infoPtr->nItemWidth);
2442 labelSize.cy = rcText.bottom - rcText.top;
2444 SelectObject(hdc, hOldFont);
2445 ReleaseDC(infoPtr->hwndSelf, hdc);
2448 calc_label:
2449 if (infoPtr->uView == LV_VIEW_ICON)
2451 Label.left = Box.left + (infoPtr->nItemWidth - labelSize.cx) / 2;
2452 Label.top = Box.top + ICON_TOP_PADDING_HITABLE +
2453 infoPtr->iconSize.cy + ICON_BOTTOM_PADDING;
2454 Label.right = Label.left + labelSize.cx;
2455 Label.bottom = Label.top + infoPtr->nItemHeight;
2456 if (!oversizedBox && labelSize.cy > infoPtr->ntmHeight)
2458 labelSize.cy = min(Box.bottom - Label.top, labelSize.cy);
2459 labelSize.cy /= infoPtr->ntmHeight;
2460 labelSize.cy = max(labelSize.cy, 1);
2461 labelSize.cy *= infoPtr->ntmHeight;
2463 Label.bottom = Label.top + labelSize.cy + HEIGHT_PADDING;
2465 else if (infoPtr->uView == LV_VIEW_DETAILS)
2467 Label.left = Icon.right;
2468 Label.top = Box.top;
2469 Label.right = lpLVItem->iSubItem ? lpColumnInfo->rcHeader.right :
2470 lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left;
2471 Label.bottom = Label.top + infoPtr->nItemHeight;
2473 else /* LV_VIEW_SMALLICON or LV_VIEW_LIST */
2475 Label.left = Icon.right;
2476 Label.top = Box.top;
2477 Label.right = min(Label.left + labelSize.cx, Label.right);
2478 Label.bottom = Label.top + infoPtr->nItemHeight;
2481 if (lprcLabel) *lprcLabel = Label;
2482 TRACE(" - label=%s\n", wine_dbgstr_rect(&Label));
2485 /************************************************************/
2486 /* compute SELECT bounding box */
2487 /************************************************************/
2488 if (doSelectBox)
2490 if (infoPtr->uView == LV_VIEW_DETAILS)
2492 SelectBox.left = Icon.left;
2493 SelectBox.top = Box.top;
2494 SelectBox.bottom = Box.bottom;
2496 if (labelSize.cx)
2497 SelectBox.right = min(Label.left + labelSize.cx, Label.right);
2498 else
2499 SelectBox.right = min(Label.left + MAX_EMPTYTEXT_SELECT_WIDTH, Label.right);
2501 else
2503 UnionRect(&SelectBox, &Icon, &Label);
2505 if (lprcSelectBox) *lprcSelectBox = SelectBox;
2506 TRACE(" - select box=%s\n", wine_dbgstr_rect(&SelectBox));
2509 /* Fix the Box if necessary */
2510 if (lprcBox)
2512 if (oversizedBox) UnionRect(lprcBox, &Box, &Label);
2513 else *lprcBox = Box;
2515 TRACE(" - box=%s\n", wine_dbgstr_rect(&Box));
2518 /***
2519 * DESCRIPTION: [INTERNAL]
2521 * PARAMETER(S):
2522 * [I] infoPtr : valid pointer to the listview structure
2523 * [I] nItem : item number
2524 * [O] lprcBox : ptr to Box rectangle
2526 * RETURN:
2527 * None.
2529 static void LISTVIEW_GetItemBox(const LISTVIEW_INFO *infoPtr, INT nItem, LPRECT lprcBox)
2531 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
2532 POINT Position, Origin;
2533 LVITEMW lvItem;
2535 LISTVIEW_GetOrigin(infoPtr, &Origin);
2536 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
2538 /* Be smart and try to figure out the minimum we have to do */
2539 lvItem.mask = 0;
2540 if (infoPtr->uView == LV_VIEW_ICON && infoPtr->bFocus && LISTVIEW_GetItemState(infoPtr, nItem, LVIS_FOCUSED))
2541 lvItem.mask |= LVIF_TEXT;
2542 lvItem.iItem = nItem;
2543 lvItem.iSubItem = 0;
2544 lvItem.pszText = szDispText;
2545 lvItem.cchTextMax = DISP_TEXT_SIZE;
2546 if (lvItem.mask) LISTVIEW_GetItemW(infoPtr, &lvItem);
2547 if (infoPtr->uView == LV_VIEW_ICON)
2549 lvItem.mask |= LVIF_STATE;
2550 lvItem.stateMask = LVIS_FOCUSED;
2551 lvItem.state = (lvItem.mask & LVIF_TEXT ? LVIS_FOCUSED : 0);
2553 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, lprcBox, 0, 0, 0, 0);
2555 if (infoPtr->uView == LV_VIEW_DETAILS && infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT &&
2556 SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, 0, 0))
2558 OffsetRect(lprcBox, Origin.x, Position.y + Origin.y);
2560 else
2561 OffsetRect(lprcBox, Position.x + Origin.x, Position.y + Origin.y);
2564 /* LISTVIEW_MapIdToIndex helper */
2565 static INT CALLBACK MapIdSearchCompare(LPVOID p1, LPVOID p2, LPARAM lParam)
2567 ITEM_ID *id1 = (ITEM_ID*)p1;
2568 ITEM_ID *id2 = (ITEM_ID*)p2;
2570 if (id1->id == id2->id) return 0;
2572 return (id1->id < id2->id) ? -1 : 1;
2575 /***
2576 * DESCRIPTION:
2577 * Returns the item index for id specified.
2579 * PARAMETER(S):
2580 * [I] infoPtr : valid pointer to the listview structure
2581 * [I] iID : item id to get index for
2583 * RETURN:
2584 * Item index, or -1 on failure.
2586 static INT LISTVIEW_MapIdToIndex(const LISTVIEW_INFO *infoPtr, UINT iID)
2588 ITEM_ID ID;
2589 INT index;
2591 TRACE("iID=%d\n", iID);
2593 if (infoPtr->dwStyle & LVS_OWNERDATA) return -1;
2594 if (infoPtr->nItemCount == 0) return -1;
2596 ID.id = iID;
2597 index = DPA_Search(infoPtr->hdpaItemIds, &ID, -1, &MapIdSearchCompare, 0, DPAS_SORTED);
2599 if (index != -1)
2601 ITEM_ID *lpID = DPA_GetPtr(infoPtr->hdpaItemIds, index);
2602 return DPA_GetPtrIndex(infoPtr->hdpaItems, lpID->item);
2605 return -1;
2608 /***
2609 * DESCRIPTION:
2610 * Returns the item id for index given.
2612 * PARAMETER(S):
2613 * [I] infoPtr : valid pointer to the listview structure
2614 * [I] iItem : item index to get id for
2616 * RETURN:
2617 * Item id.
2619 static DWORD LISTVIEW_MapIndexToId(const LISTVIEW_INFO *infoPtr, INT iItem)
2621 ITEM_INFO *lpItem;
2622 HDPA hdpaSubItems;
2624 TRACE("iItem=%d\n", iItem);
2626 if (infoPtr->dwStyle & LVS_OWNERDATA) return -1;
2627 if (iItem < 0 || iItem >= infoPtr->nItemCount) return -1;
2629 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, iItem);
2630 lpItem = DPA_GetPtr(hdpaSubItems, 0);
2632 return lpItem->id->id;
2635 /***
2636 * DESCRIPTION:
2637 * Returns the current icon position, and advances it along the top.
2638 * The returned position is not offset by Origin.
2640 * PARAMETER(S):
2641 * [I] infoPtr : valid pointer to the listview structure
2642 * [O] lpPos : will get the current icon position
2644 * RETURN:
2645 * None
2647 static void LISTVIEW_NextIconPosTop(LISTVIEW_INFO *infoPtr, LPPOINT lpPos)
2649 INT nListWidth = infoPtr->rcList.right - infoPtr->rcList.left;
2651 *lpPos = infoPtr->currIconPos;
2653 infoPtr->currIconPos.x += infoPtr->nItemWidth;
2654 if (infoPtr->currIconPos.x + infoPtr->nItemWidth <= nListWidth) return;
2656 infoPtr->currIconPos.x = 0;
2657 infoPtr->currIconPos.y += infoPtr->nItemHeight;
2661 /***
2662 * DESCRIPTION:
2663 * Returns the current icon position, and advances it down the left edge.
2664 * The returned position is not offset by Origin.
2666 * PARAMETER(S):
2667 * [I] infoPtr : valid pointer to the listview structure
2668 * [O] lpPos : will get the current icon position
2670 * RETURN:
2671 * None
2673 static void LISTVIEW_NextIconPosLeft(LISTVIEW_INFO *infoPtr, LPPOINT lpPos)
2675 INT nListHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
2677 *lpPos = infoPtr->currIconPos;
2679 infoPtr->currIconPos.y += infoPtr->nItemHeight;
2680 if (infoPtr->currIconPos.y + infoPtr->nItemHeight <= nListHeight) return;
2682 infoPtr->currIconPos.x += infoPtr->nItemWidth;
2683 infoPtr->currIconPos.y = 0;
2687 /***
2688 * DESCRIPTION:
2689 * Moves an icon to the specified position.
2690 * It takes care of invalidating the item, etc.
2692 * PARAMETER(S):
2693 * [I] infoPtr : valid pointer to the listview structure
2694 * [I] nItem : the item to move
2695 * [I] lpPos : the new icon position
2696 * [I] isNew : flags the item as being new
2698 * RETURN:
2699 * Success: TRUE
2700 * Failure: FALSE
2702 static BOOL LISTVIEW_MoveIconTo(const LISTVIEW_INFO *infoPtr, INT nItem, const POINT *lppt, BOOL isNew)
2704 POINT old;
2706 if (!isNew)
2708 old.x = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosX, nItem);
2709 old.y = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosY, nItem);
2711 if (lppt->x == old.x && lppt->y == old.y) return TRUE;
2712 LISTVIEW_InvalidateItem(infoPtr, nItem);
2715 /* Allocating a POINTER for every item is too resource intensive,
2716 * so we'll keep the (x,y) in different arrays */
2717 if (!DPA_SetPtr(infoPtr->hdpaPosX, nItem, (void *)(LONG_PTR)lppt->x)) return FALSE;
2718 if (!DPA_SetPtr(infoPtr->hdpaPosY, nItem, (void *)(LONG_PTR)lppt->y)) return FALSE;
2720 LISTVIEW_InvalidateItem(infoPtr, nItem);
2722 return TRUE;
2725 /***
2726 * DESCRIPTION:
2727 * Arranges listview items in icon display mode.
2729 * PARAMETER(S):
2730 * [I] infoPtr : valid pointer to the listview structure
2731 * [I] nAlignCode : alignment code
2733 * RETURN:
2734 * SUCCESS : TRUE
2735 * FAILURE : FALSE
2737 static BOOL LISTVIEW_Arrange(LISTVIEW_INFO *infoPtr, INT nAlignCode)
2739 void (*next_pos)(LISTVIEW_INFO *, LPPOINT);
2740 POINT pos;
2741 INT i;
2743 if (infoPtr->uView != LV_VIEW_ICON && infoPtr->uView != LV_VIEW_SMALLICON) return FALSE;
2745 TRACE("nAlignCode=%d\n", nAlignCode);
2747 if (nAlignCode == LVA_DEFAULT)
2749 if (infoPtr->dwStyle & LVS_ALIGNLEFT) nAlignCode = LVA_ALIGNLEFT;
2750 else nAlignCode = LVA_ALIGNTOP;
2753 switch (nAlignCode)
2755 case LVA_ALIGNLEFT: next_pos = LISTVIEW_NextIconPosLeft; break;
2756 case LVA_ALIGNTOP: next_pos = LISTVIEW_NextIconPosTop; break;
2757 case LVA_SNAPTOGRID: next_pos = LISTVIEW_NextIconPosTop; break; /* FIXME */
2758 default: return FALSE;
2761 infoPtr->bAutoarrange = TRUE;
2762 infoPtr->currIconPos.x = infoPtr->currIconPos.y = 0;
2763 for (i = 0; i < infoPtr->nItemCount; i++)
2765 next_pos(infoPtr, &pos);
2766 LISTVIEW_MoveIconTo(infoPtr, i, &pos, FALSE);
2769 return TRUE;
2772 /***
2773 * DESCRIPTION:
2774 * Retrieves the bounding rectangle of all the items, not offset by Origin.
2775 * For LVS_REPORT always returns empty rectangle.
2777 * PARAMETER(S):
2778 * [I] infoPtr : valid pointer to the listview structure
2779 * [O] lprcView : bounding rectangle
2781 * RETURN:
2782 * SUCCESS : TRUE
2783 * FAILURE : FALSE
2785 static void LISTVIEW_GetAreaRect(const LISTVIEW_INFO *infoPtr, LPRECT lprcView)
2787 INT i, x, y;
2789 SetRectEmpty(lprcView);
2791 switch (infoPtr->uView)
2793 case LV_VIEW_ICON:
2794 case LV_VIEW_SMALLICON:
2795 for (i = 0; i < infoPtr->nItemCount; i++)
2797 x = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosX, i);
2798 y = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosY, i);
2799 lprcView->right = max(lprcView->right, x);
2800 lprcView->bottom = max(lprcView->bottom, y);
2802 if (infoPtr->nItemCount > 0)
2804 lprcView->right += infoPtr->nItemWidth;
2805 lprcView->bottom += infoPtr->nItemHeight;
2807 break;
2809 case LV_VIEW_LIST:
2810 y = LISTVIEW_GetCountPerColumn(infoPtr);
2811 x = infoPtr->nItemCount / y;
2812 if (infoPtr->nItemCount % y) x++;
2813 lprcView->right = x * infoPtr->nItemWidth;
2814 lprcView->bottom = y * infoPtr->nItemHeight;
2815 break;
2819 /***
2820 * DESCRIPTION:
2821 * Retrieves the bounding rectangle of all the items.
2823 * PARAMETER(S):
2824 * [I] infoPtr : valid pointer to the listview structure
2825 * [O] lprcView : bounding rectangle
2827 * RETURN:
2828 * SUCCESS : TRUE
2829 * FAILURE : FALSE
2831 static BOOL LISTVIEW_GetViewRect(const LISTVIEW_INFO *infoPtr, LPRECT lprcView)
2833 POINT ptOrigin;
2835 TRACE("(lprcView=%p)\n", lprcView);
2837 if (!lprcView) return FALSE;
2839 LISTVIEW_GetAreaRect(infoPtr, lprcView);
2841 if (infoPtr->uView != LV_VIEW_DETAILS)
2843 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
2844 OffsetRect(lprcView, ptOrigin.x, ptOrigin.y);
2847 TRACE("lprcView=%s\n", wine_dbgstr_rect(lprcView));
2849 return TRUE;
2852 /***
2853 * DESCRIPTION:
2854 * Retrieves the subitem pointer associated with the subitem index.
2856 * PARAMETER(S):
2857 * [I] hdpaSubItems : DPA handle for a specific item
2858 * [I] nSubItem : index of subitem
2860 * RETURN:
2861 * SUCCESS : subitem pointer
2862 * FAILURE : NULL
2864 static SUBITEM_INFO* LISTVIEW_GetSubItemPtr(HDPA hdpaSubItems, INT nSubItem)
2866 SUBITEM_INFO *lpSubItem;
2867 INT i;
2869 /* we should binary search here if need be */
2870 for (i = 1; i < DPA_GetPtrCount(hdpaSubItems); i++)
2872 lpSubItem = DPA_GetPtr(hdpaSubItems, i);
2873 if (lpSubItem->iSubItem == nSubItem)
2874 return lpSubItem;
2877 return NULL;
2881 /***
2882 * DESCRIPTION:
2883 * Calculates the desired item width.
2885 * PARAMETER(S):
2886 * [I] infoPtr : valid pointer to the listview structure
2888 * RETURN:
2889 * The desired item width.
2891 static INT LISTVIEW_CalculateItemWidth(const LISTVIEW_INFO *infoPtr)
2893 INT nItemWidth = 0;
2895 TRACE("uView=%d\n", infoPtr->uView);
2897 if (infoPtr->uView == LV_VIEW_ICON)
2898 nItemWidth = infoPtr->iconSpacing.cx;
2899 else if (infoPtr->uView == LV_VIEW_DETAILS)
2901 if (DPA_GetPtrCount(infoPtr->hdpaColumns) > 0)
2903 RECT rcHeader;
2904 INT index;
2906 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX,
2907 DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, 0);
2909 LISTVIEW_GetHeaderRect(infoPtr, index, &rcHeader);
2910 nItemWidth = rcHeader.right;
2913 else /* LV_VIEW_SMALLICON, or LV_VIEW_LIST */
2915 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
2916 LVITEMW lvItem;
2917 INT i;
2919 lvItem.mask = LVIF_TEXT;
2920 lvItem.iSubItem = 0;
2922 for (i = 0; i < infoPtr->nItemCount; i++)
2924 lvItem.iItem = i;
2925 lvItem.pszText = szDispText;
2926 lvItem.cchTextMax = DISP_TEXT_SIZE;
2927 if (LISTVIEW_GetItemW(infoPtr, &lvItem))
2928 nItemWidth = max(LISTVIEW_GetStringWidthT(infoPtr, lvItem.pszText, TRUE),
2929 nItemWidth);
2932 if (infoPtr->himlSmall) nItemWidth += infoPtr->iconSize.cx;
2933 if (infoPtr->himlState) nItemWidth += infoPtr->iconStateSize.cx;
2935 nItemWidth = max(DEFAULT_COLUMN_WIDTH, nItemWidth + WIDTH_PADDING);
2938 return nItemWidth;
2941 /***
2942 * DESCRIPTION:
2943 * Calculates the desired item height.
2945 * PARAMETER(S):
2946 * [I] infoPtr : valid pointer to the listview structure
2948 * RETURN:
2949 * The desired item height.
2951 static INT LISTVIEW_CalculateItemHeight(const LISTVIEW_INFO *infoPtr)
2953 INT nItemHeight;
2955 TRACE("uView=%d\n", infoPtr->uView);
2957 if (infoPtr->uView == LV_VIEW_ICON)
2958 nItemHeight = infoPtr->iconSpacing.cy;
2959 else
2961 nItemHeight = infoPtr->ntmHeight;
2962 if (infoPtr->uView == LV_VIEW_DETAILS && infoPtr->dwLvExStyle & LVS_EX_GRIDLINES)
2963 nItemHeight++;
2964 if (infoPtr->himlState)
2965 nItemHeight = max(nItemHeight, infoPtr->iconStateSize.cy);
2966 if (infoPtr->himlSmall)
2967 nItemHeight = max(nItemHeight, infoPtr->iconSize.cy);
2968 if (infoPtr->himlState || infoPtr->himlSmall)
2969 nItemHeight += HEIGHT_PADDING;
2970 if (infoPtr->nMeasureItemHeight > 0)
2971 nItemHeight = infoPtr->nMeasureItemHeight;
2974 return max(nItemHeight, 1);
2977 /***
2978 * DESCRIPTION:
2979 * Updates the width, and height of an item.
2981 * PARAMETER(S):
2982 * [I] infoPtr : valid pointer to the listview structure
2984 * RETURN:
2985 * None.
2987 static inline void LISTVIEW_UpdateItemSize(LISTVIEW_INFO *infoPtr)
2989 infoPtr->nItemWidth = LISTVIEW_CalculateItemWidth(infoPtr);
2990 infoPtr->nItemHeight = LISTVIEW_CalculateItemHeight(infoPtr);
2994 /***
2995 * DESCRIPTION:
2996 * Retrieves and saves important text metrics info for the current
2997 * Listview font.
2999 * PARAMETER(S):
3000 * [I] infoPtr : valid pointer to the listview structure
3003 static void LISTVIEW_SaveTextMetrics(LISTVIEW_INFO *infoPtr)
3005 HDC hdc = GetDC(infoPtr->hwndSelf);
3006 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
3007 HFONT hOldFont = SelectObject(hdc, hFont);
3008 TEXTMETRICW tm;
3009 SIZE sz;
3011 if (GetTextMetricsW(hdc, &tm))
3013 infoPtr->ntmHeight = tm.tmHeight;
3014 infoPtr->ntmMaxCharWidth = tm.tmMaxCharWidth;
3017 if (GetTextExtentPoint32A(hdc, "...", 3, &sz))
3018 infoPtr->nEllipsisWidth = sz.cx;
3020 SelectObject(hdc, hOldFont);
3021 ReleaseDC(infoPtr->hwndSelf, hdc);
3023 TRACE("tmHeight=%d\n", infoPtr->ntmHeight);
3026 /***
3027 * DESCRIPTION:
3028 * A compare function for ranges
3030 * PARAMETER(S)
3031 * [I] range1 : pointer to range 1;
3032 * [I] range2 : pointer to range 2;
3033 * [I] flags : flags
3035 * RETURNS:
3036 * > 0 : if range 1 > range 2
3037 * < 0 : if range 2 > range 1
3038 * = 0 : if range intersects range 2
3040 static INT CALLBACK ranges_cmp(LPVOID range1, LPVOID range2, LPARAM flags)
3042 INT cmp;
3044 if (((RANGE*)range1)->upper <= ((RANGE*)range2)->lower)
3045 cmp = -1;
3046 else if (((RANGE*)range2)->upper <= ((RANGE*)range1)->lower)
3047 cmp = 1;
3048 else
3049 cmp = 0;
3051 TRACE("range1=%s, range2=%s, cmp=%d\n", debugrange(range1), debugrange(range2), cmp);
3053 return cmp;
3056 #define ranges_check(ranges, desc) if (TRACE_ON(listview)) ranges_assert(ranges, desc, __FUNCTION__, __LINE__)
3058 static void ranges_assert(RANGES ranges, LPCSTR desc, const char *func, int line)
3060 INT i;
3061 RANGE *prev, *curr;
3063 TRACE("*** Checking %s:%d:%s ***\n", func, line, desc);
3064 assert (ranges);
3065 assert (DPA_GetPtrCount(ranges->hdpa) >= 0);
3066 ranges_dump(ranges);
3067 if (DPA_GetPtrCount(ranges->hdpa) > 0)
3069 prev = DPA_GetPtr(ranges->hdpa, 0);
3070 assert (prev->lower >= 0 && prev->lower < prev->upper);
3071 for (i = 1; i < DPA_GetPtrCount(ranges->hdpa); i++)
3073 curr = DPA_GetPtr(ranges->hdpa, i);
3074 assert (prev->upper <= curr->lower);
3075 assert (curr->lower < curr->upper);
3076 prev = curr;
3079 TRACE("--- Done checking---\n");
3082 static RANGES ranges_create(int count)
3084 RANGES ranges = Alloc(sizeof(struct tagRANGES));
3085 if (!ranges) return NULL;
3086 ranges->hdpa = DPA_Create(count);
3087 if (ranges->hdpa) return ranges;
3088 Free(ranges);
3089 return NULL;
3092 static void ranges_clear(RANGES ranges)
3094 INT i;
3096 for(i = 0; i < DPA_GetPtrCount(ranges->hdpa); i++)
3097 Free(DPA_GetPtr(ranges->hdpa, i));
3098 DPA_DeleteAllPtrs(ranges->hdpa);
3102 static void ranges_destroy(RANGES ranges)
3104 if (!ranges) return;
3105 ranges_clear(ranges);
3106 DPA_Destroy(ranges->hdpa);
3107 Free(ranges);
3110 static RANGES ranges_clone(RANGES ranges)
3112 RANGES clone;
3113 INT i;
3115 if (!(clone = ranges_create(DPA_GetPtrCount(ranges->hdpa)))) goto fail;
3117 for (i = 0; i < DPA_GetPtrCount(ranges->hdpa); i++)
3119 RANGE *newrng = Alloc(sizeof(RANGE));
3120 if (!newrng) goto fail;
3121 *newrng = *((RANGE*)DPA_GetPtr(ranges->hdpa, i));
3122 DPA_SetPtr(clone->hdpa, i, newrng);
3124 return clone;
3126 fail:
3127 TRACE ("clone failed\n");
3128 ranges_destroy(clone);
3129 return NULL;
3132 static RANGES ranges_diff(RANGES ranges, RANGES sub)
3134 INT i;
3136 for (i = 0; i < DPA_GetPtrCount(sub->hdpa); i++)
3137 ranges_del(ranges, *((RANGE *)DPA_GetPtr(sub->hdpa, i)));
3139 return ranges;
3142 static void ranges_dump(RANGES ranges)
3144 INT i;
3146 for (i = 0; i < DPA_GetPtrCount(ranges->hdpa); i++)
3147 TRACE(" %s\n", debugrange(DPA_GetPtr(ranges->hdpa, i)));
3150 static inline BOOL ranges_contain(RANGES ranges, INT nItem)
3152 RANGE srchrng = { nItem, nItem + 1 };
3154 TRACE("(nItem=%d)\n", nItem);
3155 ranges_check(ranges, "before contain");
3156 return DPA_Search(ranges->hdpa, &srchrng, 0, ranges_cmp, 0, DPAS_SORTED) != -1;
3159 static INT ranges_itemcount(RANGES ranges)
3161 INT i, count = 0;
3163 for (i = 0; i < DPA_GetPtrCount(ranges->hdpa); i++)
3165 RANGE *sel = DPA_GetPtr(ranges->hdpa, i);
3166 count += sel->upper - sel->lower;
3169 return count;
3172 static BOOL ranges_shift(RANGES ranges, INT nItem, INT delta, INT nUpper)
3174 RANGE srchrng = { nItem, nItem + 1 }, *chkrng;
3175 INT index;
3177 index = DPA_Search(ranges->hdpa, &srchrng, 0, ranges_cmp, 0, DPAS_SORTED | DPAS_INSERTAFTER);
3178 if (index == -1) return TRUE;
3180 for (; index < DPA_GetPtrCount(ranges->hdpa); index++)
3182 chkrng = DPA_GetPtr(ranges->hdpa, index);
3183 if (chkrng->lower >= nItem)
3184 chkrng->lower = max(min(chkrng->lower + delta, nUpper - 1), 0);
3185 if (chkrng->upper > nItem)
3186 chkrng->upper = max(min(chkrng->upper + delta, nUpper), 0);
3188 return TRUE;
3191 static BOOL ranges_add(RANGES ranges, RANGE range)
3193 RANGE srchrgn;
3194 INT index;
3196 TRACE("(%s)\n", debugrange(&range));
3197 ranges_check(ranges, "before add");
3199 /* try find overlapping regions first */
3200 srchrgn.lower = range.lower - 1;
3201 srchrgn.upper = range.upper + 1;
3202 index = DPA_Search(ranges->hdpa, &srchrgn, 0, ranges_cmp, 0, DPAS_SORTED);
3204 if (index == -1)
3206 RANGE *newrgn;
3208 TRACE("Adding new range\n");
3210 /* create the brand new range to insert */
3211 newrgn = Alloc(sizeof(RANGE));
3212 if(!newrgn) goto fail;
3213 *newrgn = range;
3215 /* figure out where to insert it */
3216 index = DPA_Search(ranges->hdpa, newrgn, 0, ranges_cmp, 0, DPAS_SORTED | DPAS_INSERTAFTER);
3217 TRACE("index=%d\n", index);
3218 if (index == -1) index = 0;
3220 /* and get it over with */
3221 if (DPA_InsertPtr(ranges->hdpa, index, newrgn) == -1)
3223 Free(newrgn);
3224 goto fail;
3227 else
3229 RANGE *chkrgn, *mrgrgn;
3230 INT fromindex, mergeindex;
3232 chkrgn = DPA_GetPtr(ranges->hdpa, index);
3233 TRACE("Merge with %s @%d\n", debugrange(chkrgn), index);
3235 chkrgn->lower = min(range.lower, chkrgn->lower);
3236 chkrgn->upper = max(range.upper, chkrgn->upper);
3238 TRACE("New range %s @%d\n", debugrange(chkrgn), index);
3240 /* merge now common ranges */
3241 fromindex = 0;
3242 srchrgn.lower = chkrgn->lower - 1;
3243 srchrgn.upper = chkrgn->upper + 1;
3247 mergeindex = DPA_Search(ranges->hdpa, &srchrgn, fromindex, ranges_cmp, 0, 0);
3248 if (mergeindex == -1) break;
3249 if (mergeindex == index)
3251 fromindex = index + 1;
3252 continue;
3255 TRACE("Merge with index %i\n", mergeindex);
3257 mrgrgn = DPA_GetPtr(ranges->hdpa, mergeindex);
3258 chkrgn->lower = min(chkrgn->lower, mrgrgn->lower);
3259 chkrgn->upper = max(chkrgn->upper, mrgrgn->upper);
3260 Free(mrgrgn);
3261 DPA_DeletePtr(ranges->hdpa, mergeindex);
3262 if (mergeindex < index) index --;
3263 } while(1);
3266 ranges_check(ranges, "after add");
3267 return TRUE;
3269 fail:
3270 ranges_check(ranges, "failed add");
3271 return FALSE;
3274 static BOOL ranges_del(RANGES ranges, RANGE range)
3276 RANGE *chkrgn;
3277 INT index;
3279 TRACE("(%s)\n", debugrange(&range));
3280 ranges_check(ranges, "before del");
3282 /* we don't use DPAS_SORTED here, since we need *
3283 * to find the first overlapping range */
3284 index = DPA_Search(ranges->hdpa, &range, 0, ranges_cmp, 0, 0);
3285 while(index != -1)
3287 chkrgn = DPA_GetPtr(ranges->hdpa, index);
3289 TRACE("Matches range %s @%d\n", debugrange(chkrgn), index);
3291 /* case 1: Same range */
3292 if ( (chkrgn->upper == range.upper) &&
3293 (chkrgn->lower == range.lower) )
3295 DPA_DeletePtr(ranges->hdpa, index);
3296 Free(chkrgn);
3297 break;
3299 /* case 2: engulf */
3300 else if ( (chkrgn->upper <= range.upper) &&
3301 (chkrgn->lower >= range.lower) )
3303 DPA_DeletePtr(ranges->hdpa, index);
3304 Free(chkrgn);
3306 /* case 3: overlap upper */
3307 else if ( (chkrgn->upper <= range.upper) &&
3308 (chkrgn->lower < range.lower) )
3310 chkrgn->upper = range.lower;
3312 /* case 4: overlap lower */
3313 else if ( (chkrgn->upper > range.upper) &&
3314 (chkrgn->lower >= range.lower) )
3316 chkrgn->lower = range.upper;
3317 break;
3319 /* case 5: fully internal */
3320 else
3322 RANGE *newrgn;
3324 if (!(newrgn = Alloc(sizeof(RANGE)))) goto fail;
3325 newrgn->lower = chkrgn->lower;
3326 newrgn->upper = range.lower;
3327 chkrgn->lower = range.upper;
3328 if (DPA_InsertPtr(ranges->hdpa, index, newrgn) == -1)
3330 Free(newrgn);
3331 goto fail;
3333 break;
3336 index = DPA_Search(ranges->hdpa, &range, index, ranges_cmp, 0, 0);
3339 ranges_check(ranges, "after del");
3340 return TRUE;
3342 fail:
3343 ranges_check(ranges, "failed del");
3344 return FALSE;
3347 /***
3348 * DESCRIPTION:
3349 * Removes all selection ranges
3351 * Parameters(s):
3352 * [I] infoPtr : valid pointer to the listview structure
3353 * [I] toSkip : item range to skip removing the selection
3355 * RETURNS:
3356 * SUCCESS : TRUE
3357 * FAILURE : FALSE
3359 static BOOL LISTVIEW_DeselectAllSkipItems(LISTVIEW_INFO *infoPtr, RANGES toSkip)
3361 LVITEMW lvItem;
3362 ITERATOR i;
3363 RANGES clone;
3365 TRACE("()\n");
3367 lvItem.state = 0;
3368 lvItem.stateMask = LVIS_SELECTED;
3370 /* need to clone the DPA because callbacks can change it */
3371 if (!(clone = ranges_clone(infoPtr->selectionRanges))) return FALSE;
3372 iterator_rangesitems(&i, ranges_diff(clone, toSkip));
3373 while(iterator_next(&i))
3374 LISTVIEW_SetItemState(infoPtr, i.nItem, &lvItem);
3375 /* note that the iterator destructor will free the cloned range */
3376 iterator_destroy(&i);
3378 return TRUE;
3381 static inline BOOL LISTVIEW_DeselectAllSkipItem(LISTVIEW_INFO *infoPtr, INT nItem)
3383 RANGES toSkip;
3385 if (!(toSkip = ranges_create(1))) return FALSE;
3386 if (nItem != -1) ranges_additem(toSkip, nItem);
3387 LISTVIEW_DeselectAllSkipItems(infoPtr, toSkip);
3388 ranges_destroy(toSkip);
3389 return TRUE;
3392 static inline BOOL LISTVIEW_DeselectAll(LISTVIEW_INFO *infoPtr)
3394 return LISTVIEW_DeselectAllSkipItem(infoPtr, -1);
3397 /***
3398 * DESCRIPTION:
3399 * Retrieves the number of items that are marked as selected.
3401 * PARAMETER(S):
3402 * [I] infoPtr : valid pointer to the listview structure
3404 * RETURN:
3405 * Number of items selected.
3407 static INT LISTVIEW_GetSelectedCount(const LISTVIEW_INFO *infoPtr)
3409 INT nSelectedCount = 0;
3411 if (infoPtr->uCallbackMask & LVIS_SELECTED)
3413 INT i;
3414 for (i = 0; i < infoPtr->nItemCount; i++)
3416 if (LISTVIEW_GetItemState(infoPtr, i, LVIS_SELECTED))
3417 nSelectedCount++;
3420 else
3421 nSelectedCount = ranges_itemcount(infoPtr->selectionRanges);
3423 TRACE("nSelectedCount=%d\n", nSelectedCount);
3424 return nSelectedCount;
3427 /***
3428 * DESCRIPTION:
3429 * Manages the item focus.
3431 * PARAMETER(S):
3432 * [I] infoPtr : valid pointer to the listview structure
3433 * [I] nItem : item index
3435 * RETURN:
3436 * TRUE : focused item changed
3437 * FALSE : focused item has NOT changed
3439 static inline BOOL LISTVIEW_SetItemFocus(LISTVIEW_INFO *infoPtr, INT nItem)
3441 INT oldFocus = infoPtr->nFocusedItem;
3442 LVITEMW lvItem;
3444 if (nItem == infoPtr->nFocusedItem) return FALSE;
3446 lvItem.state = nItem == -1 ? 0 : LVIS_FOCUSED;
3447 lvItem.stateMask = LVIS_FOCUSED;
3448 LISTVIEW_SetItemState(infoPtr, nItem == -1 ? infoPtr->nFocusedItem : nItem, &lvItem);
3450 return oldFocus != infoPtr->nFocusedItem;
3453 /* Helper function for LISTVIEW_ShiftIndices *only* */
3454 static INT shift_item(const LISTVIEW_INFO *infoPtr, INT nShiftItem, INT nItem, INT direction)
3456 if (nShiftItem < nItem) return nShiftItem;
3458 if (nShiftItem > nItem) return nShiftItem + direction;
3460 if (direction > 0) return nShiftItem + direction;
3462 return min(nShiftItem, infoPtr->nItemCount - 1);
3466 * DESCRIPTION:
3467 * Updates the various indices after an item has been inserted or deleted.
3469 * PARAMETER(S):
3470 * [I] infoPtr : valid pointer to the listview structure
3471 * [I] nItem : item index
3472 * [I] direction : Direction of shift, +1 or -1.
3474 * RETURN:
3475 * None
3477 static void LISTVIEW_ShiftIndices(LISTVIEW_INFO *infoPtr, INT nItem, INT direction)
3479 INT nNewFocus;
3480 BOOL bOldChange;
3482 /* temporarily disable change notification while shifting items */
3483 bOldChange = infoPtr->bDoChangeNotify;
3484 infoPtr->bDoChangeNotify = FALSE;
3486 TRACE("Shifting %iu, %i steps\n", nItem, direction);
3488 ranges_shift(infoPtr->selectionRanges, nItem, direction, infoPtr->nItemCount);
3490 assert(abs(direction) == 1);
3492 infoPtr->nSelectionMark = shift_item(infoPtr, infoPtr->nSelectionMark, nItem, direction);
3494 nNewFocus = shift_item(infoPtr, infoPtr->nFocusedItem, nItem, direction);
3495 if (nNewFocus != infoPtr->nFocusedItem)
3496 LISTVIEW_SetItemFocus(infoPtr, nNewFocus);
3498 /* But we are not supposed to modify nHotItem! */
3500 infoPtr->bDoChangeNotify = bOldChange;
3505 * DESCRIPTION:
3506 * Adds a block of selections.
3508 * PARAMETER(S):
3509 * [I] infoPtr : valid pointer to the listview structure
3510 * [I] nItem : item index
3512 * RETURN:
3513 * Whether the window is still valid.
3515 static BOOL LISTVIEW_AddGroupSelection(LISTVIEW_INFO *infoPtr, INT nItem)
3517 INT nFirst = min(infoPtr->nSelectionMark, nItem);
3518 INT nLast = max(infoPtr->nSelectionMark, nItem);
3519 HWND hwndSelf = infoPtr->hwndSelf;
3520 NMLVODSTATECHANGE nmlv;
3521 LVITEMW item;
3522 BOOL bOldChange;
3523 INT i;
3525 /* Temporarily disable change notification
3526 * If the control is LVS_OWNERDATA, we need to send
3527 * only one LVN_ODSTATECHANGED notification.
3528 * See MSDN documentation for LVN_ITEMCHANGED.
3530 bOldChange = infoPtr->bDoChangeNotify;
3531 if (infoPtr->dwStyle & LVS_OWNERDATA) infoPtr->bDoChangeNotify = FALSE;
3533 if (nFirst == -1) nFirst = nItem;
3535 item.state = LVIS_SELECTED;
3536 item.stateMask = LVIS_SELECTED;
3538 for (i = nFirst; i <= nLast; i++)
3539 LISTVIEW_SetItemState(infoPtr,i,&item);
3541 ZeroMemory(&nmlv, sizeof(nmlv));
3542 nmlv.iFrom = nFirst;
3543 nmlv.iTo = nLast;
3544 nmlv.uNewState = 0;
3545 nmlv.uOldState = item.state;
3547 notify_hdr(infoPtr, LVN_ODSTATECHANGED, (LPNMHDR)&nmlv);
3548 if (!IsWindow(hwndSelf))
3549 return FALSE;
3550 infoPtr->bDoChangeNotify = bOldChange;
3551 return TRUE;
3555 /***
3556 * DESCRIPTION:
3557 * Sets a single group selection.
3559 * PARAMETER(S):
3560 * [I] infoPtr : valid pointer to the listview structure
3561 * [I] nItem : item index
3563 * RETURN:
3564 * None
3566 static void LISTVIEW_SetGroupSelection(LISTVIEW_INFO *infoPtr, INT nItem)
3568 RANGES selection;
3569 LVITEMW item;
3570 ITERATOR i;
3571 BOOL bOldChange;
3573 if (!(selection = ranges_create(100))) return;
3575 item.state = LVIS_SELECTED;
3576 item.stateMask = LVIS_SELECTED;
3578 if ((infoPtr->uView == LV_VIEW_LIST) || (infoPtr->uView == LV_VIEW_DETAILS))
3580 if (infoPtr->nSelectionMark == -1)
3582 infoPtr->nSelectionMark = nItem;
3583 ranges_additem(selection, nItem);
3585 else
3587 RANGE sel;
3589 sel.lower = min(infoPtr->nSelectionMark, nItem);
3590 sel.upper = max(infoPtr->nSelectionMark, nItem) + 1;
3591 ranges_add(selection, sel);
3594 else
3596 RECT rcItem, rcSel, rcSelMark;
3597 POINT ptItem;
3599 rcItem.left = LVIR_BOUNDS;
3600 if (!LISTVIEW_GetItemRect(infoPtr, nItem, &rcItem)) return;
3601 rcSelMark.left = LVIR_BOUNDS;
3602 if (!LISTVIEW_GetItemRect(infoPtr, infoPtr->nSelectionMark, &rcSelMark)) return;
3603 UnionRect(&rcSel, &rcItem, &rcSelMark);
3604 iterator_frameditems(&i, infoPtr, &rcSel);
3605 while(iterator_next(&i))
3607 LISTVIEW_GetItemPosition(infoPtr, i.nItem, &ptItem);
3608 if (PtInRect(&rcSel, ptItem)) ranges_additem(selection, i.nItem);
3610 iterator_destroy(&i);
3613 /* disable per item notifications on LVS_OWNERDATA style
3614 FIXME: single LVN_ODSTATECHANGED should be used */
3615 bOldChange = infoPtr->bDoChangeNotify;
3616 if (infoPtr->dwStyle & LVS_OWNERDATA) infoPtr->bDoChangeNotify = FALSE;
3618 LISTVIEW_DeselectAllSkipItems(infoPtr, selection);
3621 iterator_rangesitems(&i, selection);
3622 while(iterator_next(&i))
3623 LISTVIEW_SetItemState(infoPtr, i.nItem, &item);
3624 /* this will also destroy the selection */
3625 iterator_destroy(&i);
3627 infoPtr->bDoChangeNotify = bOldChange;
3629 LISTVIEW_SetItemFocus(infoPtr, nItem);
3632 /***
3633 * DESCRIPTION:
3634 * Sets a single selection.
3636 * PARAMETER(S):
3637 * [I] infoPtr : valid pointer to the listview structure
3638 * [I] nItem : item index
3640 * RETURN:
3641 * None
3643 static void LISTVIEW_SetSelection(LISTVIEW_INFO *infoPtr, INT nItem)
3645 LVITEMW lvItem;
3647 TRACE("nItem=%d\n", nItem);
3649 LISTVIEW_DeselectAllSkipItem(infoPtr, nItem);
3651 lvItem.state = LVIS_FOCUSED | LVIS_SELECTED;
3652 lvItem.stateMask = LVIS_FOCUSED | LVIS_SELECTED;
3653 LISTVIEW_SetItemState(infoPtr, nItem, &lvItem);
3655 infoPtr->nSelectionMark = nItem;
3658 /***
3659 * DESCRIPTION:
3660 * Set selection(s) with keyboard.
3662 * PARAMETER(S):
3663 * [I] infoPtr : valid pointer to the listview structure
3664 * [I] nItem : item index
3665 * [I] space : VK_SPACE code sent
3667 * RETURN:
3668 * SUCCESS : TRUE (needs to be repainted)
3669 * FAILURE : FALSE (nothing has changed)
3671 static BOOL LISTVIEW_KeySelection(LISTVIEW_INFO *infoPtr, INT nItem, BOOL space)
3673 /* FIXME: pass in the state */
3674 WORD wShift = HIWORD(GetKeyState(VK_SHIFT));
3675 WORD wCtrl = HIWORD(GetKeyState(VK_CONTROL));
3676 BOOL bResult = FALSE;
3678 TRACE("nItem=%d, wShift=%d, wCtrl=%d\n", nItem, wShift, wCtrl);
3679 if ((nItem >= 0) && (nItem < infoPtr->nItemCount))
3681 bResult = TRUE;
3683 if (infoPtr->dwStyle & LVS_SINGLESEL || (wShift == 0 && wCtrl == 0))
3684 LISTVIEW_SetSelection(infoPtr, nItem);
3685 else
3687 if (wShift)
3688 LISTVIEW_SetGroupSelection(infoPtr, nItem);
3689 else if (wCtrl)
3691 LVITEMW lvItem;
3692 lvItem.state = ~LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED);
3693 lvItem.stateMask = LVIS_SELECTED;
3694 if (space)
3696 LISTVIEW_SetItemState(infoPtr, nItem, &lvItem);
3697 if (lvItem.state & LVIS_SELECTED)
3698 infoPtr->nSelectionMark = nItem;
3700 bResult = LISTVIEW_SetItemFocus(infoPtr, nItem);
3703 LISTVIEW_EnsureVisible(infoPtr, nItem, FALSE);
3706 UpdateWindow(infoPtr->hwndSelf); /* update client area */
3707 return bResult;
3710 static BOOL LISTVIEW_GetItemAtPt(const LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem, POINT pt)
3712 LVHITTESTINFO lvHitTestInfo;
3714 ZeroMemory(&lvHitTestInfo, sizeof(lvHitTestInfo));
3715 lvHitTestInfo.pt.x = pt.x;
3716 lvHitTestInfo.pt.y = pt.y;
3718 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
3720 lpLVItem->mask = LVIF_PARAM;
3721 lpLVItem->iItem = lvHitTestInfo.iItem;
3722 lpLVItem->iSubItem = 0;
3724 return LISTVIEW_GetItemT(infoPtr, lpLVItem, TRUE);
3727 static inline BOOL LISTVIEW_IsHotTracking(const LISTVIEW_INFO *infoPtr)
3729 return ((infoPtr->dwLvExStyle & LVS_EX_TRACKSELECT) ||
3730 (infoPtr->dwLvExStyle & LVS_EX_ONECLICKACTIVATE) ||
3731 (infoPtr->dwLvExStyle & LVS_EX_TWOCLICKACTIVATE));
3734 /***
3735 * DESCRIPTION:
3736 * Called when the mouse is being actively tracked and has hovered for a specified
3737 * amount of time
3739 * PARAMETER(S):
3740 * [I] infoPtr : valid pointer to the listview structure
3741 * [I] fwKeys : key indicator
3742 * [I] x,y : mouse position
3744 * RETURN:
3745 * 0 if the message was processed, non-zero if there was an error
3747 * INFO:
3748 * LVS_EX_TRACKSELECT: An item is automatically selected when the cursor remains
3749 * over the item for a certain period of time.
3752 static LRESULT LISTVIEW_MouseHover(LISTVIEW_INFO *infoPtr, INT x, INT y)
3754 NMHDR hdr;
3756 if (notify_hdr(infoPtr, NM_HOVER, &hdr)) return 0;
3758 if (LISTVIEW_IsHotTracking(infoPtr))
3760 LVITEMW item;
3761 POINT pt;
3763 pt.x = x;
3764 pt.y = y;
3766 if (LISTVIEW_GetItemAtPt(infoPtr, &item, pt))
3767 LISTVIEW_SetSelection(infoPtr, item.iItem);
3769 SetFocus(infoPtr->hwndSelf);
3772 return 0;
3775 #define SCROLL_LEFT 0x1
3776 #define SCROLL_RIGHT 0x2
3777 #define SCROLL_UP 0x4
3778 #define SCROLL_DOWN 0x8
3780 /***
3781 * DESCRIPTION:
3782 * Utility routine to draw and highlight items within a marquee selection rectangle.
3784 * PARAMETER(S):
3785 * [I] infoPtr : valid pointer to the listview structure
3786 * [I] coords_orig : original co-ordinates of the cursor
3787 * [I] coords_offs : offsetted coordinates of the cursor
3788 * [I] offset : offset amount
3789 * [I] scroll : Bitmask of which directions we should scroll, if at all
3791 * RETURN:
3792 * None.
3794 static void LISTVIEW_MarqueeHighlight(LISTVIEW_INFO *infoPtr, const POINT *coords_orig,
3795 const POINT *coords_offs, const POINT *offset,
3796 INT scroll)
3798 BOOL controlDown = FALSE;
3799 LVITEMW item;
3800 ITERATOR old_elems, new_elems;
3801 RECT rect;
3803 if (coords_offs->x > infoPtr->marqueeOrigin.x)
3805 rect.left = infoPtr->marqueeOrigin.x;
3806 rect.right = coords_offs->x;
3808 else
3810 rect.left = coords_offs->x;
3811 rect.right = infoPtr->marqueeOrigin.x;
3814 if (coords_offs->y > infoPtr->marqueeOrigin.y)
3816 rect.top = infoPtr->marqueeOrigin.y;
3817 rect.bottom = coords_offs->y;
3819 else
3821 rect.top = coords_offs->y;
3822 rect.bottom = infoPtr->marqueeOrigin.y;
3825 /* Cancel out the old marquee rectangle and draw the new one */
3826 LISTVIEW_InvalidateRect(infoPtr, &infoPtr->marqueeDrawRect);
3828 /* Scroll by the appropriate distance if applicable - speed up scrolling as
3829 the cursor is further away */
3831 if ((scroll & SCROLL_LEFT) && (coords_orig->x <= 0))
3832 LISTVIEW_Scroll(infoPtr, coords_orig->x, 0);
3834 if ((scroll & SCROLL_RIGHT) && (coords_orig->x >= infoPtr->rcList.right))
3835 LISTVIEW_Scroll(infoPtr, (coords_orig->x - infoPtr->rcList.right), 0);
3837 if ((scroll & SCROLL_UP) && (coords_orig->y <= 0))
3838 LISTVIEW_Scroll(infoPtr, 0, coords_orig->y);
3840 if ((scroll & SCROLL_DOWN) && (coords_orig->y >= infoPtr->rcList.bottom))
3841 LISTVIEW_Scroll(infoPtr, 0, (coords_orig->y - infoPtr->rcList.bottom));
3843 iterator_frameditems_absolute(&old_elems, infoPtr, &infoPtr->marqueeRect);
3845 CopyRect(&infoPtr->marqueeRect, &rect);
3847 CopyRect(&infoPtr->marqueeDrawRect, &rect);
3848 OffsetRect(&infoPtr->marqueeDrawRect, offset->x, offset->y);
3850 iterator_frameditems_absolute(&new_elems, infoPtr, &infoPtr->marqueeRect);
3851 iterator_remove_common_items(&old_elems, &new_elems);
3853 /* Iterate over no longer selected items */
3854 while (iterator_next(&old_elems))
3856 if (old_elems.nItem > -1)
3858 if (LISTVIEW_GetItemState(infoPtr, old_elems.nItem, LVIS_SELECTED) == LVIS_SELECTED)
3859 item.state = 0;
3860 else
3861 item.state = LVIS_SELECTED;
3863 item.stateMask = LVIS_SELECTED;
3865 LISTVIEW_SetItemState(infoPtr, old_elems.nItem, &item);
3868 iterator_destroy(&old_elems);
3871 /* Iterate over newly selected items */
3872 if (GetKeyState(VK_CONTROL) & 0x8000)
3873 controlDown = TRUE;
3875 while (iterator_next(&new_elems))
3877 if (new_elems.nItem > -1)
3879 /* If CTRL is pressed, invert. If not, always select the item. */
3880 if ((controlDown) && (LISTVIEW_GetItemState(infoPtr, new_elems.nItem, LVIS_SELECTED)))
3881 item.state = 0;
3882 else
3883 item.state = LVIS_SELECTED;
3885 item.stateMask = LVIS_SELECTED;
3887 LISTVIEW_SetItemState(infoPtr, new_elems.nItem, &item);
3890 iterator_destroy(&new_elems);
3892 LISTVIEW_InvalidateRect(infoPtr, &rect);
3895 /***
3896 * DESCRIPTION:
3897 * Called when we are in a marquee selection that involves scrolling the listview (ie,
3898 * the cursor is outside the bounds of the client area). This is a TIMERPROC.
3900 * PARAMETER(S):
3901 * [I] hwnd : Handle to the listview
3902 * [I] uMsg : WM_TIMER (ignored)
3903 * [I] idEvent : The timer ID interpreted as a pointer to a LISTVIEW_INFO struct
3904 * [I] dwTimer : The elapsed time (ignored)
3906 * RETURN:
3907 * None.
3909 static VOID CALLBACK LISTVIEW_ScrollTimer(HWND hWnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
3911 LISTVIEW_INFO *infoPtr;
3912 SCROLLINFO scrollInfo;
3913 POINT coords_orig;
3914 POINT coords_offs;
3915 POINT offset;
3916 INT scroll = 0;
3918 infoPtr = (LISTVIEW_INFO *) idEvent;
3920 if (!infoPtr)
3921 return;
3923 /* Get the current cursor position and convert to client coordinates */
3924 GetCursorPos(&coords_orig);
3925 ScreenToClient(hWnd, &coords_orig);
3927 /* Ensure coordinates are within client bounds */
3928 coords_offs.x = max(min(coords_orig.x, infoPtr->rcList.right), 0);
3929 coords_offs.y = max(min(coords_orig.y, infoPtr->rcList.bottom), 0);
3931 /* Get offset */
3932 LISTVIEW_GetOrigin(infoPtr, &offset);
3934 /* Offset coordinates by the appropriate amount */
3935 coords_offs.x -= offset.x;
3936 coords_offs.y -= offset.y;
3938 scrollInfo.cbSize = sizeof(SCROLLINFO);
3939 scrollInfo.fMask = SIF_ALL;
3941 /* Work out in which directions we can scroll */
3942 if (GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
3944 if (scrollInfo.nPos != scrollInfo.nMin)
3945 scroll |= SCROLL_UP;
3947 if (((scrollInfo.nPage + scrollInfo.nPos) - 1) != scrollInfo.nMax)
3948 scroll |= SCROLL_DOWN;
3951 if (GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo))
3953 if (scrollInfo.nPos != scrollInfo.nMin)
3954 scroll |= SCROLL_LEFT;
3956 if (((scrollInfo.nPage + scrollInfo.nPos) - 1) != scrollInfo.nMax)
3957 scroll |= SCROLL_RIGHT;
3960 if (((coords_orig.x <= 0) && (scroll & SCROLL_LEFT)) ||
3961 ((coords_orig.y <= 0) && (scroll & SCROLL_UP)) ||
3962 ((coords_orig.x >= infoPtr->rcList.right) && (scroll & SCROLL_RIGHT)) ||
3963 ((coords_orig.y >= infoPtr->rcList.bottom) && (scroll & SCROLL_DOWN)))
3965 LISTVIEW_MarqueeHighlight(infoPtr, &coords_orig, &coords_offs, &offset, scroll);
3969 /***
3970 * DESCRIPTION:
3971 * Called whenever WM_MOUSEMOVE is received.
3973 * PARAMETER(S):
3974 * [I] infoPtr : valid pointer to the listview structure
3975 * [I] fwKeys : key indicator
3976 * [I] x,y : mouse position
3978 * RETURN:
3979 * 0 if the message is processed, non-zero if there was an error
3981 static LRESULT LISTVIEW_MouseMove(LISTVIEW_INFO *infoPtr, WORD fwKeys, INT x, INT y)
3983 if (!(fwKeys & MK_LBUTTON))
3984 infoPtr->bLButtonDown = FALSE;
3986 if (infoPtr->bLButtonDown)
3988 POINT tmp;
3989 RECT rect;
3990 LVHITTESTINFO lvHitTestInfo;
3991 WORD wDragWidth = GetSystemMetrics(SM_CXDRAG);
3992 WORD wDragHeight= GetSystemMetrics(SM_CYDRAG);
3994 if (infoPtr->bMarqueeSelect)
3996 POINT coords_orig;
3997 POINT coords_offs;
3998 POINT offset;
4000 coords_orig.x = x;
4001 coords_orig.y = y;
4003 /* Get offset */
4004 LISTVIEW_GetOrigin(infoPtr, &offset);
4006 /* Ensure coordinates are within client bounds */
4007 coords_offs.x = max(min(x, infoPtr->rcList.right), 0);
4008 coords_offs.y = max(min(y, infoPtr->rcList.bottom), 0);
4010 /* Offset coordinates by the appropriate amount */
4011 coords_offs.x -= offset.x;
4012 coords_offs.y -= offset.y;
4014 /* Enable the timer if we're going outside our bounds, in case the user doesn't
4015 move the mouse again */
4017 if ((x <= 0) || (y <= 0) || (x >= infoPtr->rcList.right) ||
4018 (y >= infoPtr->rcList.bottom))
4020 if (!infoPtr->bScrolling)
4022 infoPtr->bScrolling = TRUE;
4023 SetTimer(infoPtr->hwndSelf, (UINT_PTR) infoPtr, 1, LISTVIEW_ScrollTimer);
4026 else
4028 infoPtr->bScrolling = FALSE;
4029 KillTimer(infoPtr->hwndSelf, (UINT_PTR) infoPtr);
4032 LISTVIEW_MarqueeHighlight(infoPtr, &coords_orig, &coords_offs, &offset, 0);
4033 return 0;
4036 rect.left = infoPtr->ptClickPos.x - wDragWidth;
4037 rect.right = infoPtr->ptClickPos.x + wDragWidth;
4038 rect.top = infoPtr->ptClickPos.y - wDragHeight;
4039 rect.bottom = infoPtr->ptClickPos.y + wDragHeight;
4041 tmp.x = x;
4042 tmp.y = y;
4044 lvHitTestInfo.pt = tmp;
4045 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, TRUE);
4047 /* reset item marker */
4048 if (infoPtr->nLButtonDownItem != lvHitTestInfo.iItem)
4049 infoPtr->nLButtonDownItem = -1;
4051 if (!PtInRect(&rect, tmp))
4053 /* this path covers the following:
4054 1. WM_LBUTTONDOWN over selected item (sets focus on it)
4055 2. change focus with keys
4056 3. move mouse over item from step 1 selects it and moves focus on it */
4057 if (infoPtr->nLButtonDownItem != -1 &&
4058 !LISTVIEW_GetItemState(infoPtr, infoPtr->nLButtonDownItem, LVIS_SELECTED))
4060 LVITEMW lvItem;
4062 lvItem.state = LVIS_FOCUSED | LVIS_SELECTED;
4063 lvItem.stateMask = LVIS_FOCUSED | LVIS_SELECTED;
4065 LISTVIEW_SetItemState(infoPtr, infoPtr->nLButtonDownItem, &lvItem);
4066 infoPtr->nLButtonDownItem = -1;
4069 if (!infoPtr->bDragging)
4071 lvHitTestInfo.pt = infoPtr->ptClickPos;
4072 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, TRUE);
4074 /* If the click is outside the range of an item, begin a
4075 highlight. If not, begin an item drag. */
4076 if (lvHitTestInfo.iItem == -1)
4078 NMHDR hdr;
4080 /* If we're allowing multiple selections, send notification.
4081 If return value is non-zero, cancel. */
4082 if (!(infoPtr->dwStyle & LVS_SINGLESEL) && (notify_hdr(infoPtr, LVN_MARQUEEBEGIN, &hdr) == 0))
4084 /* Store the absolute coordinates of the click */
4085 POINT offset;
4086 LISTVIEW_GetOrigin(infoPtr, &offset);
4088 infoPtr->marqueeOrigin.x = infoPtr->ptClickPos.x - offset.x;
4089 infoPtr->marqueeOrigin.y = infoPtr->ptClickPos.y - offset.y;
4091 /* Begin selection and capture mouse */
4092 infoPtr->bMarqueeSelect = TRUE;
4093 SetCapture(infoPtr->hwndSelf);
4096 else
4098 NMLISTVIEW nmlv;
4100 ZeroMemory(&nmlv, sizeof(nmlv));
4101 nmlv.iItem = lvHitTestInfo.iItem;
4102 nmlv.ptAction = infoPtr->ptClickPos;
4104 notify_listview(infoPtr, LVN_BEGINDRAG, &nmlv);
4105 infoPtr->bDragging = TRUE;
4109 return 0;
4113 /* see if we are supposed to be tracking mouse hovering */
4114 if (LISTVIEW_IsHotTracking(infoPtr)) {
4115 TRACKMOUSEEVENT trackinfo;
4117 trackinfo.cbSize = sizeof(TRACKMOUSEEVENT);
4118 trackinfo.dwFlags = TME_QUERY;
4120 /* see if we are already tracking this hwnd */
4121 _TrackMouseEvent(&trackinfo);
4123 if(!(trackinfo.dwFlags & TME_HOVER) || trackinfo.hwndTrack != infoPtr->hwndSelf) {
4124 trackinfo.dwFlags = TME_HOVER;
4125 trackinfo.dwHoverTime = infoPtr->dwHoverTime;
4126 trackinfo.hwndTrack = infoPtr->hwndSelf;
4128 /* call TRACKMOUSEEVENT so we receive WM_MOUSEHOVER messages */
4129 _TrackMouseEvent(&trackinfo);
4133 return 0;
4137 /***
4138 * Tests whether the item is assignable to a list with style lStyle
4140 static inline BOOL is_assignable_item(const LVITEMW *lpLVItem, LONG lStyle)
4142 if ( (lpLVItem->mask & LVIF_TEXT) &&
4143 (lpLVItem->pszText == LPSTR_TEXTCALLBACKW) &&
4144 (lStyle & (LVS_SORTASCENDING | LVS_SORTDESCENDING)) ) return FALSE;
4146 return TRUE;
4150 /***
4151 * DESCRIPTION:
4152 * Helper for LISTVIEW_SetItemT *only*: sets item attributes.
4154 * PARAMETER(S):
4155 * [I] infoPtr : valid pointer to the listview structure
4156 * [I] lpLVItem : valid pointer to new item attributes
4157 * [I] isNew : the item being set is being inserted
4158 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
4159 * [O] bChanged : will be set to TRUE if the item really changed
4161 * RETURN:
4162 * SUCCESS : TRUE
4163 * FAILURE : FALSE
4165 static BOOL set_main_item(LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem, BOOL isNew, BOOL isW, BOOL *bChanged)
4167 ITEM_INFO *lpItem;
4168 NMLISTVIEW nmlv;
4169 UINT uChanged = 0;
4170 LVITEMW item;
4171 /* stateMask is ignored for LVM_INSERTITEM */
4172 UINT stateMask = isNew ? ~0 : lpLVItem->stateMask;
4174 TRACE("()\n");
4176 assert(lpLVItem->iItem >= 0 && lpLVItem->iItem < infoPtr->nItemCount);
4178 if (lpLVItem->mask == 0) return TRUE;
4180 if (infoPtr->dwStyle & LVS_OWNERDATA)
4182 /* a virtual listview only stores selection and focus */
4183 if (lpLVItem->mask & ~LVIF_STATE)
4184 return FALSE;
4185 lpItem = NULL;
4187 else
4189 HDPA hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, lpLVItem->iItem);
4190 lpItem = DPA_GetPtr(hdpaSubItems, 0);
4191 assert (lpItem);
4194 /* we need to get the lParam and state of the item */
4195 item.iItem = lpLVItem->iItem;
4196 item.iSubItem = lpLVItem->iSubItem;
4197 item.mask = LVIF_STATE | LVIF_PARAM;
4198 item.stateMask = (infoPtr->dwStyle & LVS_OWNERDATA) ? LVIS_FOCUSED | LVIS_SELECTED : ~0;
4200 item.state = 0;
4201 item.lParam = 0;
4202 if (!isNew && !LISTVIEW_GetItemW(infoPtr, &item)) return FALSE;
4204 TRACE("oldState=%x, newState=%x\n", item.state, lpLVItem->state);
4205 /* determine what fields will change */
4206 if ((lpLVItem->mask & LVIF_STATE) && ((item.state ^ lpLVItem->state) & stateMask & ~infoPtr->uCallbackMask))
4207 uChanged |= LVIF_STATE;
4209 if ((lpLVItem->mask & LVIF_IMAGE) && (lpItem->hdr.iImage != lpLVItem->iImage))
4210 uChanged |= LVIF_IMAGE;
4212 if ((lpLVItem->mask & LVIF_PARAM) && (lpItem->lParam != lpLVItem->lParam))
4213 uChanged |= LVIF_PARAM;
4215 if ((lpLVItem->mask & LVIF_INDENT) && (lpItem->iIndent != lpLVItem->iIndent))
4216 uChanged |= LVIF_INDENT;
4218 if ((lpLVItem->mask & LVIF_TEXT) && textcmpWT(lpItem->hdr.pszText, lpLVItem->pszText, isW))
4219 uChanged |= LVIF_TEXT;
4221 TRACE("uChanged=0x%x\n", uChanged);
4222 if (!uChanged) return TRUE;
4223 *bChanged = TRUE;
4225 ZeroMemory(&nmlv, sizeof(NMLISTVIEW));
4226 nmlv.iItem = lpLVItem->iItem;
4227 nmlv.uNewState = (item.state & ~stateMask) | (lpLVItem->state & stateMask);
4228 nmlv.uOldState = item.state;
4229 nmlv.uChanged = uChanged;
4230 nmlv.lParam = item.lParam;
4232 /* send LVN_ITEMCHANGING notification, if the item is not being inserted */
4233 /* and we are _NOT_ virtual (LVS_OWNERDATA), and change notifications */
4234 /* are enabled */
4235 if(lpItem && !isNew && infoPtr->bDoChangeNotify)
4237 HWND hwndSelf = infoPtr->hwndSelf;
4239 if (notify_listview(infoPtr, LVN_ITEMCHANGING, &nmlv))
4240 return FALSE;
4241 if (!IsWindow(hwndSelf))
4242 return FALSE;
4245 /* copy information */
4246 if (lpLVItem->mask & LVIF_TEXT)
4247 textsetptrT(&lpItem->hdr.pszText, lpLVItem->pszText, isW);
4249 if (lpLVItem->mask & LVIF_IMAGE)
4250 lpItem->hdr.iImage = lpLVItem->iImage;
4252 if (lpLVItem->mask & LVIF_PARAM)
4253 lpItem->lParam = lpLVItem->lParam;
4255 if (lpLVItem->mask & LVIF_INDENT)
4256 lpItem->iIndent = lpLVItem->iIndent;
4258 if (uChanged & LVIF_STATE)
4260 if (lpItem && (stateMask & ~infoPtr->uCallbackMask))
4262 lpItem->state &= ~stateMask;
4263 lpItem->state |= (lpLVItem->state & stateMask);
4265 if (lpLVItem->state & stateMask & ~infoPtr->uCallbackMask & LVIS_SELECTED)
4267 if (infoPtr->dwStyle & LVS_SINGLESEL) LISTVIEW_DeselectAllSkipItem(infoPtr, lpLVItem->iItem);
4268 ranges_additem(infoPtr->selectionRanges, lpLVItem->iItem);
4270 else if (stateMask & LVIS_SELECTED)
4272 ranges_delitem(infoPtr->selectionRanges, lpLVItem->iItem);
4274 /* if we are asked to change focus, and we manage it, do it */
4275 if (stateMask & ~infoPtr->uCallbackMask & LVIS_FOCUSED)
4277 if (lpLVItem->state & LVIS_FOCUSED)
4279 if (infoPtr->nFocusedItem != -1)
4281 /* remove current focus */
4282 item.mask = LVIF_STATE;
4283 item.state = 0;
4284 item.stateMask = LVIS_FOCUSED;
4286 /* recurse with redrawing an item */
4287 LISTVIEW_SetItemState(infoPtr, infoPtr->nFocusedItem, &item);
4290 infoPtr->nFocusedItem = lpLVItem->iItem;
4291 LISTVIEW_EnsureVisible(infoPtr, lpLVItem->iItem, infoPtr->uView == LV_VIEW_LIST);
4293 else if (infoPtr->nFocusedItem == lpLVItem->iItem)
4295 infoPtr->nFocusedItem = -1;
4300 /* if we're inserting the item, we're done */
4301 if (isNew) return TRUE;
4303 /* send LVN_ITEMCHANGED notification */
4304 if (lpLVItem->mask & LVIF_PARAM) nmlv.lParam = lpLVItem->lParam;
4305 if (infoPtr->bDoChangeNotify) notify_listview(infoPtr, LVN_ITEMCHANGED, &nmlv);
4307 return TRUE;
4310 /***
4311 * DESCRIPTION:
4312 * Helper for LISTVIEW_{Set,Insert}ItemT *only*: sets subitem attributes.
4314 * PARAMETER(S):
4315 * [I] infoPtr : valid pointer to the listview structure
4316 * [I] lpLVItem : valid pointer to new subitem attributes
4317 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
4318 * [O] bChanged : will be set to TRUE if the item really changed
4320 * RETURN:
4321 * SUCCESS : TRUE
4322 * FAILURE : FALSE
4324 static BOOL set_sub_item(const LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem, BOOL isW, BOOL *bChanged)
4326 HDPA hdpaSubItems;
4327 SUBITEM_INFO *lpSubItem;
4329 /* we do not support subitems for virtual listviews */
4330 if (infoPtr->dwStyle & LVS_OWNERDATA) return FALSE;
4332 /* set subitem only if column is present */
4333 if (lpLVItem->iSubItem >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
4335 /* First do some sanity checks */
4336 /* The LVIF_STATE flag is valid for subitems, but does not appear to be
4337 particularly useful. We currently do not actually do anything with
4338 the flag on subitems.
4340 if (lpLVItem->mask & ~(LVIF_TEXT | LVIF_IMAGE | LVIF_STATE | LVIF_DI_SETITEM)) return FALSE;
4341 if (!(lpLVItem->mask & (LVIF_TEXT | LVIF_IMAGE | LVIF_STATE))) return TRUE;
4343 /* get the subitem structure, and create it if not there */
4344 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, lpLVItem->iItem);
4345 assert (hdpaSubItems);
4347 lpSubItem = LISTVIEW_GetSubItemPtr(hdpaSubItems, lpLVItem->iSubItem);
4348 if (!lpSubItem)
4350 SUBITEM_INFO *tmpSubItem;
4351 INT i;
4353 lpSubItem = Alloc(sizeof(SUBITEM_INFO));
4354 if (!lpSubItem) return FALSE;
4355 /* we could binary search here, if need be...*/
4356 for (i = 1; i < DPA_GetPtrCount(hdpaSubItems); i++)
4358 tmpSubItem = DPA_GetPtr(hdpaSubItems, i);
4359 if (tmpSubItem->iSubItem > lpLVItem->iSubItem) break;
4361 if (DPA_InsertPtr(hdpaSubItems, i, lpSubItem) == -1)
4363 Free(lpSubItem);
4364 return FALSE;
4366 lpSubItem->iSubItem = lpLVItem->iSubItem;
4367 lpSubItem->hdr.iImage = I_IMAGECALLBACK;
4368 *bChanged = TRUE;
4371 if ((lpLVItem->mask & LVIF_IMAGE) && (lpSubItem->hdr.iImage != lpLVItem->iImage))
4373 lpSubItem->hdr.iImage = lpLVItem->iImage;
4374 *bChanged = TRUE;
4377 if ((lpLVItem->mask & LVIF_TEXT) && textcmpWT(lpSubItem->hdr.pszText, lpLVItem->pszText, isW))
4379 textsetptrT(&lpSubItem->hdr.pszText, lpLVItem->pszText, isW);
4380 *bChanged = TRUE;
4383 return TRUE;
4386 /***
4387 * DESCRIPTION:
4388 * Sets item attributes.
4390 * PARAMETER(S):
4391 * [I] infoPtr : valid pointer to the listview structure
4392 * [I] lpLVItem : new item attributes
4393 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
4395 * RETURN:
4396 * SUCCESS : TRUE
4397 * FAILURE : FALSE
4399 static BOOL LISTVIEW_SetItemT(LISTVIEW_INFO *infoPtr, LVITEMW *lpLVItem, BOOL isW)
4401 HWND hwndSelf = infoPtr->hwndSelf;
4402 LPWSTR pszText = NULL;
4403 BOOL bResult, bChanged = FALSE;
4405 TRACE("(lpLVItem=%s, isW=%d)\n", debuglvitem_t(lpLVItem, isW), isW);
4407 if (!lpLVItem || lpLVItem->iItem < 0 || lpLVItem->iItem >= infoPtr->nItemCount)
4408 return FALSE;
4410 /* Invalidate old item area */
4411 LISTVIEW_InvalidateItem(infoPtr, lpLVItem->iItem);
4413 /* For efficiency, we transform the lpLVItem->pszText to Unicode here */
4414 if ((lpLVItem->mask & LVIF_TEXT) && is_text(lpLVItem->pszText))
4416 pszText = lpLVItem->pszText;
4417 lpLVItem->pszText = textdupTtoW(lpLVItem->pszText, isW);
4420 /* actually set the fields */
4421 if (!is_assignable_item(lpLVItem, infoPtr->dwStyle)) return FALSE;
4423 if (lpLVItem->iSubItem)
4424 bResult = set_sub_item(infoPtr, lpLVItem, TRUE, &bChanged);
4425 else
4426 bResult = set_main_item(infoPtr, lpLVItem, FALSE, TRUE, &bChanged);
4427 if (!IsWindow(hwndSelf))
4428 return FALSE;
4430 /* redraw item, if necessary */
4431 if (bChanged && !infoPtr->bIsDrawing)
4433 /* this little optimization eliminates some nasty flicker */
4434 if ( infoPtr->uView == LV_VIEW_DETAILS && !(infoPtr->dwStyle & LVS_OWNERDRAWFIXED) &&
4435 !(infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) &&
4436 lpLVItem->iSubItem > 0 && lpLVItem->iSubItem <= DPA_GetPtrCount(infoPtr->hdpaColumns) )
4437 LISTVIEW_InvalidateSubItem(infoPtr, lpLVItem->iItem, lpLVItem->iSubItem);
4438 else
4439 LISTVIEW_InvalidateItem(infoPtr, lpLVItem->iItem);
4441 /* restore text */
4442 if (pszText)
4444 textfreeT(lpLVItem->pszText, isW);
4445 lpLVItem->pszText = pszText;
4448 return bResult;
4451 /***
4452 * DESCRIPTION:
4453 * Retrieves the index of the item at coordinate (0, 0) of the client area.
4455 * PARAMETER(S):
4456 * [I] infoPtr : valid pointer to the listview structure
4458 * RETURN:
4459 * item index
4461 static INT LISTVIEW_GetTopIndex(const LISTVIEW_INFO *infoPtr)
4463 INT nItem = 0;
4464 SCROLLINFO scrollInfo;
4466 scrollInfo.cbSize = sizeof(SCROLLINFO);
4467 scrollInfo.fMask = SIF_POS;
4469 if (infoPtr->uView == LV_VIEW_LIST)
4471 if (GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo))
4472 nItem = scrollInfo.nPos * LISTVIEW_GetCountPerColumn(infoPtr);
4474 else if (infoPtr->uView == LV_VIEW_DETAILS)
4476 if (GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
4477 nItem = scrollInfo.nPos;
4479 else
4481 if (GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
4482 nItem = LISTVIEW_GetCountPerRow(infoPtr) * (scrollInfo.nPos / infoPtr->nItemHeight);
4485 TRACE("nItem=%d\n", nItem);
4487 return nItem;
4491 /***
4492 * DESCRIPTION:
4493 * Erases the background of the given rectangle
4495 * PARAMETER(S):
4496 * [I] infoPtr : valid pointer to the listview structure
4497 * [I] hdc : device context handle
4498 * [I] lprcBox : clipping rectangle
4500 * RETURN:
4501 * Success: TRUE
4502 * Failure: FALSE
4504 static inline BOOL LISTVIEW_FillBkgnd(const LISTVIEW_INFO *infoPtr, HDC hdc, const RECT *lprcBox)
4506 if (!infoPtr->hBkBrush) return FALSE;
4508 TRACE("(hdc=%p, lprcBox=%s, hBkBrush=%p)\n", hdc, wine_dbgstr_rect(lprcBox), infoPtr->hBkBrush);
4510 return FillRect(hdc, lprcBox, infoPtr->hBkBrush);
4513 /***
4514 * DESCRIPTION:
4515 * Draws an item.
4517 * PARAMETER(S):
4518 * [I] infoPtr : valid pointer to the listview structure
4519 * [I] hdc : device context handle
4520 * [I] nItem : item index
4521 * [I] nSubItem : subitem index
4522 * [I] pos : item position in client coordinates
4523 * [I] cdmode : custom draw mode
4525 * RETURN:
4526 * Success: TRUE
4527 * Failure: FALSE
4529 static BOOL LISTVIEW_DrawItem(LISTVIEW_INFO *infoPtr, HDC hdc, INT nItem, INT nSubItem, POINT pos, DWORD cdmode)
4531 UINT uFormat;
4532 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
4533 static WCHAR szCallback[] = { '(', 'c', 'a', 'l', 'l', 'b', 'a', 'c', 'k', ')', 0 };
4534 DWORD cdsubitemmode = CDRF_DODEFAULT;
4535 LPRECT lprcFocus;
4536 RECT rcSelect, rcBox, rcIcon, rcLabel, rcStateIcon;
4537 NMLVCUSTOMDRAW nmlvcd;
4538 HIMAGELIST himl;
4539 LVITEMW lvItem;
4540 HFONT hOldFont;
4542 TRACE("(hdc=%p, nItem=%d, nSubItem=%d, pos=%s)\n", hdc, nItem, nSubItem, wine_dbgstr_point(&pos));
4544 /* get information needed for drawing the item */
4545 lvItem.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_PARAM;
4546 if (nSubItem == 0) lvItem.mask |= LVIF_STATE;
4547 if (infoPtr->uView == LV_VIEW_DETAILS) lvItem.mask |= LVIF_INDENT;
4548 lvItem.stateMask = LVIS_SELECTED | LVIS_FOCUSED | LVIS_STATEIMAGEMASK | LVIS_CUT;
4549 lvItem.iItem = nItem;
4550 lvItem.iSubItem = nSubItem;
4551 lvItem.state = 0;
4552 lvItem.lParam = 0;
4553 lvItem.cchTextMax = DISP_TEXT_SIZE;
4554 lvItem.pszText = szDispText;
4555 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return FALSE;
4556 if (nSubItem > 0 && (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT))
4557 lvItem.state = LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED);
4558 if (lvItem.pszText == LPSTR_TEXTCALLBACKW) lvItem.pszText = szCallback;
4559 TRACE(" lvItem=%s\n", debuglvitem_t(&lvItem, TRUE));
4561 /* now check if we need to update the focus rectangle */
4562 lprcFocus = infoPtr->bFocus && (lvItem.state & LVIS_FOCUSED) ? &infoPtr->rcFocus : 0;
4564 if (!lprcFocus) lvItem.state &= ~LVIS_FOCUSED;
4565 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, &rcBox, &rcSelect, &rcIcon, &rcStateIcon, &rcLabel);
4566 OffsetRect(&rcBox, pos.x, pos.y);
4567 OffsetRect(&rcSelect, pos.x, pos.y);
4568 OffsetRect(&rcIcon, pos.x, pos.y);
4569 OffsetRect(&rcStateIcon, pos.x, pos.y);
4570 OffsetRect(&rcLabel, pos.x, pos.y);
4571 TRACE(" rcBox=%s, rcSelect=%s, rcIcon=%s. rcLabel=%s\n",
4572 wine_dbgstr_rect(&rcBox), wine_dbgstr_rect(&rcSelect),
4573 wine_dbgstr_rect(&rcIcon), wine_dbgstr_rect(&rcLabel));
4575 /* fill in the custom draw structure */
4576 customdraw_fill(&nmlvcd, infoPtr, hdc, &rcBox, &lvItem);
4578 hOldFont = GetCurrentObject(hdc, OBJ_FONT);
4579 if (nSubItem > 0) cdmode = infoPtr->cditemmode;
4580 if (cdmode & CDRF_SKIPDEFAULT) goto postpaint;
4581 if (cdmode & CDRF_NOTIFYITEMDRAW)
4582 cdsubitemmode = notify_customdraw(infoPtr, CDDS_PREPAINT, &nmlvcd);
4583 if (nSubItem == 0) infoPtr->cditemmode = cdsubitemmode;
4584 if (cdsubitemmode & CDRF_SKIPDEFAULT) goto postpaint;
4585 /* we have to send a CDDS_SUBITEM customdraw explicitly for subitem 0 */
4586 if (nSubItem == 0 && cdsubitemmode == CDRF_NOTIFYITEMDRAW)
4588 cdsubitemmode = notify_customdraw(infoPtr, CDDS_SUBITEM | CDDS_ITEMPREPAINT, &nmlvcd);
4589 if (cdsubitemmode & CDRF_SKIPDEFAULT) goto postpaint;
4591 if (nSubItem == 0 || (cdmode & CDRF_NOTIFYITEMDRAW))
4592 prepaint_setup(infoPtr, hdc, &nmlvcd, FALSE);
4593 else if ((infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) == FALSE)
4594 prepaint_setup(infoPtr, hdc, &nmlvcd, TRUE);
4596 /* in full row select, subitems, will just use main item's colors */
4597 if (nSubItem && infoPtr->uView == LV_VIEW_DETAILS && (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT))
4598 nmlvcd.clrTextBk = CLR_NONE;
4600 /* FIXME: temporary hack */
4601 rcSelect.left = rcLabel.left;
4603 /* draw the selection background, if we're drawing the main item */
4604 if (nSubItem == 0)
4606 /* in icon mode, the label rect is really what we want to draw the
4607 * background for */
4608 if (infoPtr->uView == LV_VIEW_ICON)
4609 rcSelect = rcLabel;
4611 if (infoPtr->uView == LV_VIEW_DETAILS && (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT))
4613 /* we have to update left focus bound too if item isn't in leftmost column
4614 and reduce right box bound */
4615 if (DPA_GetPtrCount(infoPtr->hdpaColumns) > 0)
4617 INT leftmost;
4619 if ((leftmost = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, 0, 0)))
4621 INT Originx = pos.x - LISTVIEW_GetColumnInfo(infoPtr, 0)->rcHeader.left;
4622 INT index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX,
4623 DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, 0);
4625 rcBox.right = LISTVIEW_GetColumnInfo(infoPtr, index)->rcHeader.right + Originx;
4626 rcSelect.left = LISTVIEW_GetColumnInfo(infoPtr, leftmost)->rcHeader.left + Originx;
4630 rcSelect.right = rcBox.right;
4633 if (nmlvcd.clrTextBk != CLR_NONE)
4634 ExtTextOutW(hdc, rcSelect.left, rcSelect.top, ETO_OPAQUE, &rcSelect, NULL, 0, NULL);
4635 /* store new focus rectangle */
4636 if (infoPtr->nFocusedItem == nItem) infoPtr->rcFocus = rcSelect;
4639 /* state icons */
4640 if (infoPtr->himlState && STATEIMAGEINDEX(lvItem.state) && (nSubItem == 0))
4642 UINT uStateImage = STATEIMAGEINDEX(lvItem.state);
4643 if (uStateImage)
4645 TRACE("uStateImage=%d\n", uStateImage);
4646 ImageList_Draw(infoPtr->himlState, uStateImage - 1, hdc,
4647 rcStateIcon.left, rcStateIcon.top, ILD_NORMAL);
4651 /* item icons */
4652 himl = (infoPtr->uView == LV_VIEW_ICON ? infoPtr->himlNormal : infoPtr->himlSmall);
4653 if (himl && lvItem.iImage >= 0 && !IsRectEmpty(&rcIcon))
4655 UINT style;
4657 TRACE("iImage=%d\n", lvItem.iImage);
4659 if (lvItem.state & (LVIS_SELECTED | LVIS_CUT) && infoPtr->bFocus)
4660 style = ILD_SELECTED;
4661 else
4662 style = ILD_NORMAL;
4664 ImageList_DrawEx(himl, lvItem.iImage, hdc, rcIcon.left, rcIcon.top,
4665 rcIcon.right - rcIcon.left, rcIcon.bottom - rcIcon.top, infoPtr->clrBk,
4666 lvItem.state & LVIS_CUT ? RGB(255, 255, 255) : CLR_DEFAULT,
4667 style);
4670 /* Don't bother painting item being edited */
4671 if (infoPtr->hwndEdit && nItem == infoPtr->nEditLabelItem && nSubItem == 0) goto postpaint;
4673 /* figure out the text drawing flags */
4674 uFormat = (infoPtr->uView == LV_VIEW_ICON ? (lprcFocus ? LV_FL_DT_FLAGS : LV_ML_DT_FLAGS) : LV_SL_DT_FLAGS);
4675 if (infoPtr->uView == LV_VIEW_ICON)
4676 uFormat = (lprcFocus ? LV_FL_DT_FLAGS : LV_ML_DT_FLAGS);
4677 else if (nSubItem)
4679 switch (LISTVIEW_GetColumnInfo(infoPtr, nSubItem)->fmt & LVCFMT_JUSTIFYMASK)
4681 case LVCFMT_RIGHT: uFormat |= DT_RIGHT; break;
4682 case LVCFMT_CENTER: uFormat |= DT_CENTER; break;
4683 default: uFormat |= DT_LEFT;
4686 if (!(uFormat & (DT_RIGHT | DT_CENTER)))
4688 if (himl && lvItem.iImage >= 0 && !IsRectEmpty(&rcIcon)) rcLabel.left += IMAGE_PADDING;
4689 else rcLabel.left += LABEL_HOR_PADDING;
4691 else if (uFormat & DT_RIGHT) rcLabel.right -= LABEL_HOR_PADDING;
4693 /* for GRIDLINES reduce the bottom so the text formats correctly */
4694 if (infoPtr->uView == LV_VIEW_DETAILS && infoPtr->dwLvExStyle & LVS_EX_GRIDLINES)
4695 rcLabel.bottom--;
4697 DrawTextW(hdc, lvItem.pszText, -1, &rcLabel, uFormat);
4699 postpaint:
4700 if (cdsubitemmode & CDRF_NOTIFYPOSTPAINT)
4701 notify_postpaint(infoPtr, &nmlvcd);
4702 if (cdsubitemmode & CDRF_NEWFONT)
4703 SelectObject(hdc, hOldFont);
4704 return TRUE;
4707 /***
4708 * DESCRIPTION:
4709 * Draws listview items when in owner draw mode.
4711 * PARAMETER(S):
4712 * [I] infoPtr : valid pointer to the listview structure
4713 * [I] hdc : device context handle
4715 * RETURN:
4716 * None
4718 static void LISTVIEW_RefreshOwnerDraw(const LISTVIEW_INFO *infoPtr, ITERATOR *i, HDC hdc, DWORD cdmode)
4720 UINT uID = (UINT)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
4721 DWORD cditemmode = CDRF_DODEFAULT;
4722 NMLVCUSTOMDRAW nmlvcd;
4723 POINT Origin, Position;
4724 DRAWITEMSTRUCT dis;
4725 LVITEMW item;
4727 TRACE("()\n");
4729 ZeroMemory(&dis, sizeof(dis));
4731 /* Get scroll info once before loop */
4732 LISTVIEW_GetOrigin(infoPtr, &Origin);
4734 /* iterate through the invalidated rows */
4735 while(iterator_next(i))
4737 item.iItem = i->nItem;
4738 item.iSubItem = 0;
4739 item.mask = LVIF_PARAM | LVIF_STATE;
4740 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
4741 if (!LISTVIEW_GetItemW(infoPtr, &item)) continue;
4743 dis.CtlType = ODT_LISTVIEW;
4744 dis.CtlID = uID;
4745 dis.itemID = item.iItem;
4746 dis.itemAction = ODA_DRAWENTIRE;
4747 dis.itemState = 0;
4748 if (item.state & LVIS_SELECTED) dis.itemState |= ODS_SELECTED;
4749 if (infoPtr->bFocus && (item.state & LVIS_FOCUSED)) dis.itemState |= ODS_FOCUS;
4750 dis.hwndItem = infoPtr->hwndSelf;
4751 dis.hDC = hdc;
4752 LISTVIEW_GetItemOrigin(infoPtr, dis.itemID, &Position);
4753 dis.rcItem.left = Position.x + Origin.x;
4754 dis.rcItem.right = dis.rcItem.left + infoPtr->nItemWidth;
4755 dis.rcItem.top = Position.y + Origin.y;
4756 dis.rcItem.bottom = dis.rcItem.top + infoPtr->nItemHeight;
4757 dis.itemData = item.lParam;
4759 TRACE("item=%s, rcItem=%s\n", debuglvitem_t(&item, TRUE), wine_dbgstr_rect(&dis.rcItem));
4762 * Even if we do not send the CDRF_NOTIFYITEMDRAW we need to fill the nmlvcd
4763 * structure for the rest. of the paint cycle
4765 customdraw_fill(&nmlvcd, infoPtr, hdc, &dis.rcItem, &item);
4766 if (cdmode & CDRF_NOTIFYITEMDRAW)
4767 cditemmode = notify_customdraw(infoPtr, CDDS_PREPAINT, &nmlvcd);
4769 if (!(cditemmode & CDRF_SKIPDEFAULT))
4771 prepaint_setup (infoPtr, hdc, &nmlvcd, FALSE);
4772 SendMessageW(infoPtr->hwndNotify, WM_DRAWITEM, dis.CtlID, (LPARAM)&dis);
4775 if (cditemmode & CDRF_NOTIFYPOSTPAINT)
4776 notify_postpaint(infoPtr, &nmlvcd);
4780 /***
4781 * DESCRIPTION:
4782 * Draws listview items when in report display mode.
4784 * PARAMETER(S):
4785 * [I] infoPtr : valid pointer to the listview structure
4786 * [I] hdc : device context handle
4787 * [I] cdmode : custom draw mode
4789 * RETURN:
4790 * None
4792 static void LISTVIEW_RefreshReport(LISTVIEW_INFO *infoPtr, ITERATOR *i, HDC hdc, DWORD cdmode)
4794 INT rgntype;
4795 RECT rcClip, rcItem;
4796 POINT Origin, Position;
4797 RANGES colRanges;
4798 INT col, index;
4799 ITERATOR j;
4801 TRACE("()\n");
4803 /* figure out what to draw */
4804 rgntype = GetClipBox(hdc, &rcClip);
4805 if (rgntype == NULLREGION) return;
4807 /* Get scroll info once before loop */
4808 LISTVIEW_GetOrigin(infoPtr, &Origin);
4810 colRanges = ranges_create(DPA_GetPtrCount(infoPtr->hdpaColumns));
4812 /* narrow down the columns we need to paint */
4813 for(col = 0; col < DPA_GetPtrCount(infoPtr->hdpaColumns); col++)
4815 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, col, 0);
4817 LISTVIEW_GetHeaderRect(infoPtr, index, &rcItem);
4818 if ((rcItem.right + Origin.x >= rcClip.left) && (rcItem.left + Origin.x < rcClip.right))
4819 ranges_additem(colRanges, index);
4821 iterator_rangesitems(&j, colRanges);
4823 /* in full row select, we _have_ to draw the main item */
4824 if (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT)
4825 j.nSpecial = 0;
4827 /* iterate through the invalidated rows */
4828 while(iterator_next(i))
4830 LISTVIEW_GetItemOrigin(infoPtr, i->nItem, &Position);
4831 Position.y += Origin.y;
4833 /* iterate through the invalidated columns */
4834 while(iterator_next(&j))
4836 LISTVIEW_GetHeaderRect(infoPtr, j.nItem, &rcItem);
4837 Position.x = (j.nItem == 0) ? rcItem.left + Origin.x : Origin.x;
4839 if (rgntype == COMPLEXREGION && !((infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) && j.nItem == 0))
4841 rcItem.top = 0;
4842 rcItem.bottom = infoPtr->nItemHeight;
4843 OffsetRect(&rcItem, Origin.x, Position.y);
4844 if (!RectVisible(hdc, &rcItem)) continue;
4847 LISTVIEW_DrawItem(infoPtr, hdc, i->nItem, j.nItem, Position, cdmode);
4850 iterator_destroy(&j);
4853 /***
4854 * DESCRIPTION:
4855 * Draws the gridlines if necessary when in report display mode.
4857 * PARAMETER(S):
4858 * [I] infoPtr : valid pointer to the listview structure
4859 * [I] hdc : device context handle
4861 * RETURN:
4862 * None
4864 static void LISTVIEW_RefreshReportGrid(LISTVIEW_INFO *infoPtr, HDC hdc)
4866 INT rgntype;
4867 INT y, itemheight;
4868 INT col, index;
4869 HPEN hPen, hOldPen;
4870 RECT rcClip, rcItem = {0};
4871 POINT Origin;
4872 RANGES colRanges;
4873 ITERATOR j;
4874 BOOL rmost = FALSE;
4876 TRACE("()\n");
4878 /* figure out what to draw */
4879 rgntype = GetClipBox(hdc, &rcClip);
4880 if (rgntype == NULLREGION) return;
4882 /* Get scroll info once before loop */
4883 LISTVIEW_GetOrigin(infoPtr, &Origin);
4885 colRanges = ranges_create(DPA_GetPtrCount(infoPtr->hdpaColumns));
4887 /* narrow down the columns we need to paint */
4888 for(col = 0; col < DPA_GetPtrCount(infoPtr->hdpaColumns); col++)
4890 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, col, 0);
4892 LISTVIEW_GetHeaderRect(infoPtr, index, &rcItem);
4893 if ((rcItem.right + Origin.x >= rcClip.left) && (rcItem.left + Origin.x < rcClip.right))
4894 ranges_additem(colRanges, index);
4897 /* is right most vertical line visible? */
4898 if (DPA_GetPtrCount(infoPtr->hdpaColumns) > 0)
4900 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, 0);
4901 LISTVIEW_GetHeaderRect(infoPtr, index, &rcItem);
4902 rmost = (rcItem.right + Origin.x < rcClip.right);
4905 if ((hPen = CreatePen( PS_SOLID, 1, comctl32_color.clr3dFace )))
4907 hOldPen = SelectObject ( hdc, hPen );
4909 /* draw the vertical lines for the columns */
4910 iterator_rangesitems(&j, colRanges);
4911 while(iterator_next(&j))
4913 LISTVIEW_GetHeaderRect(infoPtr, j.nItem, &rcItem);
4914 if (rcItem.left == 0) continue; /* skip leftmost column */
4915 rcItem.left += Origin.x;
4916 rcItem.right += Origin.x;
4917 rcItem.top = infoPtr->rcList.top;
4918 rcItem.bottom = infoPtr->rcList.bottom;
4919 TRACE("vert col=%d, rcItem=%s\n", j.nItem, wine_dbgstr_rect(&rcItem));
4920 MoveToEx (hdc, rcItem.left, rcItem.top, NULL);
4921 LineTo (hdc, rcItem.left, rcItem.bottom);
4923 iterator_destroy(&j);
4924 /* draw rightmost grid line if visible */
4925 if (rmost)
4927 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX,
4928 DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, 0);
4929 LISTVIEW_GetHeaderRect(infoPtr, index, &rcItem);
4931 rcItem.right += Origin.x;
4933 MoveToEx (hdc, rcItem.right, infoPtr->rcList.top, NULL);
4934 LineTo (hdc, rcItem.right, infoPtr->rcList.bottom);
4937 /* draw the horizontial lines for the rows */
4938 itemheight = LISTVIEW_CalculateItemHeight(infoPtr);
4939 rcItem.left = infoPtr->rcList.left;
4940 rcItem.right = infoPtr->rcList.right;
4941 rcItem.bottom = rcItem.top = Origin.y - 1;
4942 MoveToEx(hdc, rcItem.left, rcItem.top, NULL);
4943 LineTo(hdc, rcItem.right, rcItem.top);
4944 for(y=itemheight-1+Origin.y; y<=infoPtr->rcList.bottom; y+=itemheight)
4946 rcItem.bottom = rcItem.top = y;
4947 TRACE("horz rcItem=%s\n", wine_dbgstr_rect(&rcItem));
4948 MoveToEx (hdc, rcItem.left, rcItem.top, NULL);
4949 LineTo (hdc, rcItem.right, rcItem.top);
4952 SelectObject( hdc, hOldPen );
4953 DeleteObject( hPen );
4957 /***
4958 * DESCRIPTION:
4959 * Draws listview items when in list display mode.
4961 * PARAMETER(S):
4962 * [I] infoPtr : valid pointer to the listview structure
4963 * [I] hdc : device context handle
4964 * [I] cdmode : custom draw mode
4966 * RETURN:
4967 * None
4969 static void LISTVIEW_RefreshList(LISTVIEW_INFO *infoPtr, ITERATOR *i, HDC hdc, DWORD cdmode)
4971 POINT Origin, Position;
4973 /* Get scroll info once before loop */
4974 LISTVIEW_GetOrigin(infoPtr, &Origin);
4976 while(iterator_prev(i))
4978 LISTVIEW_GetItemOrigin(infoPtr, i->nItem, &Position);
4979 Position.x += Origin.x;
4980 Position.y += Origin.y;
4982 LISTVIEW_DrawItem(infoPtr, hdc, i->nItem, 0, Position, cdmode);
4987 /***
4988 * DESCRIPTION:
4989 * Draws listview items.
4991 * PARAMETER(S):
4992 * [I] infoPtr : valid pointer to the listview structure
4993 * [I] hdc : device context handle
4994 * [I] prcErase : rect to be erased before refresh (may be NULL)
4996 * RETURN:
4997 * NoneX
4999 static void LISTVIEW_Refresh(LISTVIEW_INFO *infoPtr, HDC hdc, const RECT *prcErase)
5001 COLORREF oldTextColor = 0, oldBkColor = 0, oldClrTextBk, oldClrText;
5002 NMLVCUSTOMDRAW nmlvcd;
5003 HFONT hOldFont = 0;
5004 DWORD cdmode;
5005 INT oldBkMode = 0;
5006 RECT rcClient;
5007 ITERATOR i;
5008 HDC hdcOrig = hdc;
5009 HBITMAP hbmp = NULL;
5010 RANGE range;
5012 LISTVIEW_DUMP(infoPtr);
5014 if (infoPtr->dwLvExStyle & LVS_EX_DOUBLEBUFFER) {
5015 TRACE("double buffering\n");
5017 hdc = CreateCompatibleDC(hdcOrig);
5018 if (!hdc) {
5019 ERR("Failed to create DC for backbuffer\n");
5020 return;
5022 hbmp = CreateCompatibleBitmap(hdcOrig, infoPtr->rcList.right,
5023 infoPtr->rcList.bottom);
5024 if (!hbmp) {
5025 ERR("Failed to create bitmap for backbuffer\n");
5026 DeleteDC(hdc);
5027 return;
5030 SelectObject(hdc, hbmp);
5031 SelectObject(hdc, infoPtr->hFont);
5033 if(GetClipBox(hdcOrig, &rcClient))
5034 IntersectClipRect(hdc, rcClient.left, rcClient.top, rcClient.right, rcClient.bottom);
5035 } else {
5036 /* Save dc values we're gonna trash while drawing
5037 * FIXME: Should be done in LISTVIEW_DrawItem() */
5038 hOldFont = SelectObject(hdc, infoPtr->hFont);
5039 oldBkMode = GetBkMode(hdc);
5040 oldBkColor = GetBkColor(hdc);
5041 oldTextColor = GetTextColor(hdc);
5044 infoPtr->bIsDrawing = TRUE;
5046 if (prcErase) {
5047 LISTVIEW_FillBkgnd(infoPtr, hdc, prcErase);
5048 } else if (infoPtr->dwLvExStyle & LVS_EX_DOUBLEBUFFER) {
5049 /* If no erasing was done (usually because RedrawWindow was called
5050 * with RDW_INVALIDATE only) we need to copy the old contents into
5051 * the backbuffer before continuing. */
5052 BitBlt(hdc, infoPtr->rcList.left, infoPtr->rcList.top,
5053 infoPtr->rcList.right - infoPtr->rcList.left,
5054 infoPtr->rcList.bottom - infoPtr->rcList.top,
5055 hdcOrig, infoPtr->rcList.left, infoPtr->rcList.top, SRCCOPY);
5058 /* FIXME: Shouldn't need to do this */
5059 oldClrTextBk = infoPtr->clrTextBk;
5060 oldClrText = infoPtr->clrText;
5062 infoPtr->cditemmode = CDRF_DODEFAULT;
5064 GetClientRect(infoPtr->hwndSelf, &rcClient);
5065 customdraw_fill(&nmlvcd, infoPtr, hdc, &rcClient, 0);
5066 cdmode = notify_customdraw(infoPtr, CDDS_PREPAINT, &nmlvcd);
5067 if (cdmode & CDRF_SKIPDEFAULT) goto enddraw;
5068 prepaint_setup(infoPtr, hdc, &nmlvcd, FALSE);
5070 /* Use these colors to draw the items */
5071 infoPtr->clrTextBk = nmlvcd.clrTextBk;
5072 infoPtr->clrText = nmlvcd.clrText;
5074 /* nothing to draw */
5075 if(infoPtr->nItemCount == 0) goto enddraw;
5077 /* figure out what we need to draw */
5078 iterator_visibleitems(&i, infoPtr, hdc);
5079 range = iterator_range(&i);
5081 /* send cache hint notification */
5082 if (infoPtr->dwStyle & LVS_OWNERDATA)
5084 NMLVCACHEHINT nmlv;
5086 ZeroMemory(&nmlv, sizeof(NMLVCACHEHINT));
5087 nmlv.iFrom = range.lower;
5088 nmlv.iTo = range.upper - 1;
5089 notify_hdr(infoPtr, LVN_ODCACHEHINT, &nmlv.hdr);
5092 if ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (infoPtr->uView == LV_VIEW_DETAILS))
5093 LISTVIEW_RefreshOwnerDraw(infoPtr, &i, hdc, cdmode);
5094 else
5096 if (infoPtr->uView == LV_VIEW_DETAILS)
5097 LISTVIEW_RefreshReport(infoPtr, &i, hdc, cdmode);
5098 else /* LV_VIEW_LIST, LV_VIEW_ICON or LV_VIEW_SMALLICON */
5099 LISTVIEW_RefreshList(infoPtr, &i, hdc, cdmode);
5101 /* if we have a focus rect and it's visible, draw it */
5102 if (infoPtr->bFocus && range.lower <= infoPtr->nFocusedItem &&
5103 (range.upper - 1) >= infoPtr->nFocusedItem)
5104 LISTVIEW_DrawFocusRect(infoPtr, hdc);
5106 iterator_destroy(&i);
5108 enddraw:
5109 /* For LVS_EX_GRIDLINES go and draw lines */
5110 /* This includes the case where there were *no* items */
5111 if ((infoPtr->uView == LV_VIEW_DETAILS) && infoPtr->dwLvExStyle & LVS_EX_GRIDLINES)
5112 LISTVIEW_RefreshReportGrid(infoPtr, hdc);
5114 /* Draw marquee rectangle if appropriate */
5115 if (infoPtr->bMarqueeSelect)
5116 DrawFocusRect(hdc, &infoPtr->marqueeDrawRect);
5118 if (cdmode & CDRF_NOTIFYPOSTPAINT)
5119 notify_postpaint(infoPtr, &nmlvcd);
5121 infoPtr->clrTextBk = oldClrTextBk;
5122 infoPtr->clrText = oldClrText;
5124 if(hbmp) {
5125 BitBlt(hdcOrig, infoPtr->rcList.left, infoPtr->rcList.top,
5126 infoPtr->rcList.right - infoPtr->rcList.left,
5127 infoPtr->rcList.bottom - infoPtr->rcList.top,
5128 hdc, infoPtr->rcList.left, infoPtr->rcList.top, SRCCOPY);
5130 DeleteObject(hbmp);
5131 DeleteDC(hdc);
5132 } else {
5133 SelectObject(hdc, hOldFont);
5134 SetBkMode(hdc, oldBkMode);
5135 SetBkColor(hdc, oldBkColor);
5136 SetTextColor(hdc, oldTextColor);
5139 infoPtr->bIsDrawing = FALSE;
5143 /***
5144 * DESCRIPTION:
5145 * Calculates the approximate width and height of a given number of items.
5147 * PARAMETER(S):
5148 * [I] infoPtr : valid pointer to the listview structure
5149 * [I] nItemCount : number of items
5150 * [I] wWidth : width
5151 * [I] wHeight : height
5153 * RETURN:
5154 * Returns a DWORD. The width in the low word and the height in high word.
5156 static DWORD LISTVIEW_ApproximateViewRect(const LISTVIEW_INFO *infoPtr, INT nItemCount,
5157 WORD wWidth, WORD wHeight)
5159 DWORD dwViewRect = 0;
5161 if (nItemCount == -1)
5162 nItemCount = infoPtr->nItemCount;
5164 if (infoPtr->uView == LV_VIEW_LIST)
5166 INT nItemCountPerColumn = 1;
5167 INT nColumnCount = 0;
5169 if (wHeight == 0xFFFF)
5171 /* use current height */
5172 wHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
5175 if (wHeight < infoPtr->nItemHeight)
5176 wHeight = infoPtr->nItemHeight;
5178 if (nItemCount > 0)
5180 if (infoPtr->nItemHeight > 0)
5182 nItemCountPerColumn = wHeight / infoPtr->nItemHeight;
5183 if (nItemCountPerColumn == 0)
5184 nItemCountPerColumn = 1;
5186 if (nItemCount % nItemCountPerColumn != 0)
5187 nColumnCount = nItemCount / nItemCountPerColumn;
5188 else
5189 nColumnCount = nItemCount / nItemCountPerColumn + 1;
5193 /* Microsoft padding magic */
5194 wHeight = nItemCountPerColumn * infoPtr->nItemHeight + 2;
5195 wWidth = nColumnCount * infoPtr->nItemWidth + 2;
5197 dwViewRect = MAKELONG(wWidth, wHeight);
5199 else if (infoPtr->uView == LV_VIEW_DETAILS)
5201 RECT rcBox;
5203 if (infoPtr->nItemCount > 0)
5205 LISTVIEW_GetItemBox(infoPtr, 0, &rcBox);
5206 wWidth = rcBox.right - rcBox.left;
5207 wHeight = (rcBox.bottom - rcBox.top) * nItemCount;
5209 else
5211 /* use current height and width */
5212 if (wHeight == 0xffff)
5213 wHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
5214 if (wWidth == 0xffff)
5215 wWidth = infoPtr->rcList.right - infoPtr->rcList.left;
5218 dwViewRect = MAKELONG(wWidth, wHeight);
5220 else if (infoPtr->uView == LV_VIEW_ICON)
5222 UINT rows,cols;
5223 UINT nItemWidth;
5224 UINT nItemHeight;
5226 nItemWidth = infoPtr->iconSpacing.cx;
5227 nItemHeight = infoPtr->iconSpacing.cy;
5229 if (wWidth == 0xffff)
5230 wWidth = infoPtr->rcList.right - infoPtr->rcList.left;
5232 if (wWidth < nItemWidth)
5233 wWidth = nItemWidth;
5235 cols = wWidth / nItemWidth;
5236 if (cols > nItemCount)
5237 cols = nItemCount;
5238 if (cols < 1)
5239 cols = 1;
5241 if (nItemCount)
5243 rows = nItemCount / cols;
5244 if (nItemCount % cols)
5245 rows++;
5247 else
5248 rows = 0;
5250 wHeight = (nItemHeight * rows)+2;
5251 wWidth = (nItemWidth * cols)+2;
5253 dwViewRect = MAKELONG(wWidth, wHeight);
5255 else if (infoPtr->uView == LV_VIEW_SMALLICON)
5256 FIXME("uView == LV_VIEW_SMALLICON: not implemented\n");
5258 return dwViewRect;
5261 /***
5262 * DESCRIPTION:
5263 * Cancel edit label with saving item text.
5265 * PARAMETER(S):
5266 * [I] infoPtr : valid pointer to the listview structure
5268 * RETURN:
5269 * Always returns TRUE.
5271 static LRESULT LISTVIEW_CancelEditLabel(LISTVIEW_INFO *infoPtr)
5273 if (infoPtr->hwndEdit)
5275 /* handle value will be lost after LISTVIEW_EndEditLabelT */
5276 HWND edit = infoPtr->hwndEdit;
5278 LISTVIEW_EndEditLabelT(infoPtr, TRUE, IsWindowUnicode(infoPtr->hwndEdit));
5279 SendMessageW(edit, WM_CLOSE, 0, 0);
5282 return TRUE;
5285 /***
5286 * DESCRIPTION:
5287 * Create a drag image list for the specified item.
5289 * PARAMETER(S):
5290 * [I] infoPtr : valid pointer to the listview structure
5291 * [I] iItem : index of item
5292 * [O] lppt : Upper-left corner of the image
5294 * RETURN:
5295 * Returns a handle to the image list if successful, NULL otherwise.
5297 static HIMAGELIST LISTVIEW_CreateDragImage(LISTVIEW_INFO *infoPtr, INT iItem, LPPOINT lppt)
5299 RECT rcItem;
5300 SIZE size;
5301 POINT pos;
5302 HDC hdc, hdcOrig;
5303 HBITMAP hbmp, hOldbmp;
5304 HIMAGELIST dragList = 0;
5305 TRACE("iItem=%d Count=%d\n", iItem, infoPtr->nItemCount);
5307 if (iItem < 0 || iItem >= infoPtr->nItemCount || !lppt)
5308 return 0;
5310 rcItem.left = LVIR_BOUNDS;
5311 if (!LISTVIEW_GetItemRect(infoPtr, iItem, &rcItem))
5312 return 0;
5314 lppt->x = rcItem.left;
5315 lppt->y = rcItem.top;
5317 size.cx = rcItem.right - rcItem.left;
5318 size.cy = rcItem.bottom - rcItem.top;
5320 hdcOrig = GetDC(infoPtr->hwndSelf);
5321 hdc = CreateCompatibleDC(hdcOrig);
5322 hbmp = CreateCompatibleBitmap(hdcOrig, size.cx, size.cy);
5323 hOldbmp = SelectObject(hdc, hbmp);
5325 rcItem.left = rcItem.top = 0;
5326 rcItem.right = size.cx;
5327 rcItem.bottom = size.cy;
5328 FillRect(hdc, &rcItem, infoPtr->hBkBrush);
5330 pos.x = pos.y = 0;
5331 if (LISTVIEW_DrawItem(infoPtr, hdc, iItem, 0, pos, infoPtr->cditemmode))
5333 dragList = ImageList_Create(size.cx, size.cy, ILC_COLOR, 10, 10);
5334 SelectObject(hdc, hOldbmp);
5335 ImageList_Add(dragList, hbmp, 0);
5337 else
5338 SelectObject(hdc, hOldbmp);
5340 DeleteObject(hbmp);
5341 DeleteDC(hdc);
5342 ReleaseDC(infoPtr->hwndSelf, hdcOrig);
5344 TRACE("ret=%p\n", dragList);
5346 return dragList;
5350 /***
5351 * DESCRIPTION:
5352 * Removes all listview items and subitems.
5354 * PARAMETER(S):
5355 * [I] infoPtr : valid pointer to the listview structure
5357 * RETURN:
5358 * SUCCESS : TRUE
5359 * FAILURE : FALSE
5361 static BOOL LISTVIEW_DeleteAllItems(LISTVIEW_INFO *infoPtr, BOOL destroy)
5363 NMLISTVIEW nmlv;
5364 HDPA hdpaSubItems = NULL;
5365 BOOL bSuppress;
5366 ITEMHDR *hdrItem;
5367 ITEM_INFO *lpItem;
5368 ITEM_ID *lpID;
5369 INT i, j;
5371 TRACE("()\n");
5373 /* we do it directly, to avoid notifications */
5374 ranges_clear(infoPtr->selectionRanges);
5375 infoPtr->nSelectionMark = -1;
5376 infoPtr->nFocusedItem = -1;
5377 SetRectEmpty(&infoPtr->rcFocus);
5378 /* But we are supposed to leave nHotItem as is! */
5381 /* send LVN_DELETEALLITEMS notification */
5382 ZeroMemory(&nmlv, sizeof(NMLISTVIEW));
5383 nmlv.iItem = -1;
5384 bSuppress = notify_listview(infoPtr, LVN_DELETEALLITEMS, &nmlv);
5386 for (i = infoPtr->nItemCount - 1; i >= 0; i--)
5388 if (!(infoPtr->dwStyle & LVS_OWNERDATA))
5390 /* send LVN_DELETEITEM notification, if not suppressed
5391 and if it is not a virtual listview */
5392 if (!bSuppress) notify_deleteitem(infoPtr, i);
5393 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, i);
5394 lpItem = DPA_GetPtr(hdpaSubItems, 0);
5395 /* free id struct */
5396 j = DPA_GetPtrIndex(infoPtr->hdpaItemIds, lpItem->id);
5397 lpID = DPA_GetPtr(infoPtr->hdpaItemIds, j);
5398 DPA_DeletePtr(infoPtr->hdpaItemIds, j);
5399 Free(lpID);
5400 /* both item and subitem start with ITEMHDR header */
5401 for (j = 0; j < DPA_GetPtrCount(hdpaSubItems); j++)
5403 hdrItem = DPA_GetPtr(hdpaSubItems, j);
5404 if (is_text(hdrItem->pszText)) Free(hdrItem->pszText);
5405 Free(hdrItem);
5407 DPA_Destroy(hdpaSubItems);
5408 DPA_DeletePtr(infoPtr->hdpaItems, i);
5410 DPA_DeletePtr(infoPtr->hdpaPosX, i);
5411 DPA_DeletePtr(infoPtr->hdpaPosY, i);
5412 infoPtr->nItemCount --;
5415 if (!destroy)
5417 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
5418 LISTVIEW_UpdateScroll(infoPtr);
5420 LISTVIEW_InvalidateList(infoPtr);
5422 return TRUE;
5425 /***
5426 * DESCRIPTION:
5427 * Scrolls, and updates the columns, when a column is changing width.
5429 * PARAMETER(S):
5430 * [I] infoPtr : valid pointer to the listview structure
5431 * [I] nColumn : column to scroll
5432 * [I] dx : amount of scroll, in pixels
5434 * RETURN:
5435 * None.
5437 static void LISTVIEW_ScrollColumns(LISTVIEW_INFO *infoPtr, INT nColumn, INT dx)
5439 COLUMN_INFO *lpColumnInfo;
5440 RECT rcOld, rcCol;
5441 POINT ptOrigin;
5442 INT nCol;
5443 HDITEMW hdi;
5445 if (nColumn < 0 || DPA_GetPtrCount(infoPtr->hdpaColumns) < 1) return;
5446 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, min(nColumn, DPA_GetPtrCount(infoPtr->hdpaColumns) - 1));
5447 rcCol = lpColumnInfo->rcHeader;
5448 if (nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns))
5449 rcCol.left = rcCol.right;
5451 /* adjust the other columns */
5452 hdi.mask = HDI_ORDER;
5453 if (SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nColumn, (LPARAM)&hdi))
5455 INT nOrder = hdi.iOrder;
5456 for (nCol = 0; nCol < DPA_GetPtrCount(infoPtr->hdpaColumns); nCol++)
5458 hdi.mask = HDI_ORDER;
5459 SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nCol, (LPARAM)&hdi);
5460 if (hdi.iOrder >= nOrder) {
5461 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, nCol);
5462 lpColumnInfo->rcHeader.left += dx;
5463 lpColumnInfo->rcHeader.right += dx;
5468 /* do not update screen if not in report mode */
5469 if (!is_redrawing(infoPtr) || infoPtr->uView != LV_VIEW_DETAILS) return;
5471 /* Need to reset the item width when inserting a new column */
5472 infoPtr->nItemWidth += dx;
5474 LISTVIEW_UpdateScroll(infoPtr);
5475 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
5477 /* scroll to cover the deleted column, and invalidate for redraw */
5478 rcOld = infoPtr->rcList;
5479 rcOld.left = ptOrigin.x + rcCol.left + dx;
5480 ScrollWindowEx(infoPtr->hwndSelf, dx, 0, &rcOld, &rcOld, 0, 0, SW_ERASE | SW_INVALIDATE);
5483 /***
5484 * DESCRIPTION:
5485 * Removes a column from the listview control.
5487 * PARAMETER(S):
5488 * [I] infoPtr : valid pointer to the listview structure
5489 * [I] nColumn : column index
5491 * RETURN:
5492 * SUCCESS : TRUE
5493 * FAILURE : FALSE
5495 static BOOL LISTVIEW_DeleteColumn(LISTVIEW_INFO *infoPtr, INT nColumn)
5497 RECT rcCol;
5499 TRACE("nColumn=%d\n", nColumn);
5501 if (nColumn < 0 || DPA_GetPtrCount(infoPtr->hdpaColumns) == 0
5502 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
5504 /* While the MSDN specifically says that column zero should not be deleted,
5505 what actually happens is that the column itself is deleted but no items or subitems
5506 are removed.
5509 LISTVIEW_GetHeaderRect(infoPtr, nColumn, &rcCol);
5511 if (!SendMessageW(infoPtr->hwndHeader, HDM_DELETEITEM, nColumn, 0))
5512 return FALSE;
5514 Free(DPA_GetPtr(infoPtr->hdpaColumns, nColumn));
5515 DPA_DeletePtr(infoPtr->hdpaColumns, nColumn);
5517 if (!(infoPtr->dwStyle & LVS_OWNERDATA) && nColumn)
5519 SUBITEM_INFO *lpSubItem, *lpDelItem;
5520 HDPA hdpaSubItems;
5521 INT nItem, nSubItem, i;
5523 for (nItem = 0; nItem < infoPtr->nItemCount; nItem++)
5525 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, nItem);
5526 nSubItem = 0;
5527 lpDelItem = 0;
5528 for (i = 1; i < DPA_GetPtrCount(hdpaSubItems); i++)
5530 lpSubItem = DPA_GetPtr(hdpaSubItems, i);
5531 if (lpSubItem->iSubItem == nColumn)
5533 nSubItem = i;
5534 lpDelItem = lpSubItem;
5536 else if (lpSubItem->iSubItem > nColumn)
5538 lpSubItem->iSubItem--;
5542 /* if we found our subitem, zapp it */
5543 if (nSubItem > 0)
5545 /* free string */
5546 if (is_text(lpDelItem->hdr.pszText))
5547 Free(lpDelItem->hdr.pszText);
5549 /* free item */
5550 Free(lpDelItem);
5552 /* free dpa memory */
5553 DPA_DeletePtr(hdpaSubItems, nSubItem);
5558 /* update the other column info */
5559 LISTVIEW_UpdateItemSize(infoPtr);
5560 if(DPA_GetPtrCount(infoPtr->hdpaColumns) == 0)
5561 LISTVIEW_InvalidateList(infoPtr);
5562 else
5563 LISTVIEW_ScrollColumns(infoPtr, nColumn, -(rcCol.right - rcCol.left));
5565 return TRUE;
5568 /***
5569 * DESCRIPTION:
5570 * Invalidates the listview after an item's insertion or deletion.
5572 * PARAMETER(S):
5573 * [I] infoPtr : valid pointer to the listview structure
5574 * [I] nItem : item index
5575 * [I] dir : -1 if deleting, 1 if inserting
5577 * RETURN:
5578 * None
5580 static void LISTVIEW_ScrollOnInsert(LISTVIEW_INFO *infoPtr, INT nItem, INT dir)
5582 INT nPerCol, nItemCol, nItemRow;
5583 RECT rcScroll;
5584 POINT Origin;
5586 /* if we don't refresh, what's the point of scrolling? */
5587 if (!is_redrawing(infoPtr)) return;
5589 assert (abs(dir) == 1);
5591 /* arrange icons if autoarrange is on */
5592 if (is_autoarrange(infoPtr))
5594 BOOL arrange = TRUE;
5595 if (dir < 0 && nItem >= infoPtr->nItemCount) arrange = FALSE;
5596 if (dir > 0 && nItem == infoPtr->nItemCount - 1) arrange = FALSE;
5597 if (arrange) LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
5600 /* scrollbars need updating */
5601 LISTVIEW_UpdateScroll(infoPtr);
5603 /* figure out the item's position */
5604 if (infoPtr->uView == LV_VIEW_DETAILS)
5605 nPerCol = infoPtr->nItemCount + 1;
5606 else if (infoPtr->uView == LV_VIEW_LIST)
5607 nPerCol = LISTVIEW_GetCountPerColumn(infoPtr);
5608 else /* LV_VIEW_ICON, or LV_VIEW_SMALLICON */
5609 return;
5611 nItemCol = nItem / nPerCol;
5612 nItemRow = nItem % nPerCol;
5613 LISTVIEW_GetOrigin(infoPtr, &Origin);
5615 /* move the items below up a slot */
5616 rcScroll.left = nItemCol * infoPtr->nItemWidth;
5617 rcScroll.top = nItemRow * infoPtr->nItemHeight;
5618 rcScroll.right = rcScroll.left + infoPtr->nItemWidth;
5619 rcScroll.bottom = nPerCol * infoPtr->nItemHeight;
5620 OffsetRect(&rcScroll, Origin.x, Origin.y);
5621 TRACE("rcScroll=%s, dx=%d\n", wine_dbgstr_rect(&rcScroll), dir * infoPtr->nItemHeight);
5622 if (IntersectRect(&rcScroll, &rcScroll, &infoPtr->rcList))
5624 TRACE("Scrolling rcScroll=%s, rcList=%s\n", wine_dbgstr_rect(&rcScroll), wine_dbgstr_rect(&infoPtr->rcList));
5625 ScrollWindowEx(infoPtr->hwndSelf, 0, dir * infoPtr->nItemHeight,
5626 &rcScroll, &rcScroll, 0, 0, SW_ERASE | SW_INVALIDATE);
5629 /* report has only that column, so we're done */
5630 if (infoPtr->uView == LV_VIEW_DETAILS) return;
5632 /* now for LISTs, we have to deal with the columns to the right */
5633 rcScroll.left = (nItemCol + 1) * infoPtr->nItemWidth;
5634 rcScroll.top = 0;
5635 rcScroll.right = (infoPtr->nItemCount / nPerCol + 1) * infoPtr->nItemWidth;
5636 rcScroll.bottom = nPerCol * infoPtr->nItemHeight;
5637 OffsetRect(&rcScroll, Origin.x, Origin.y);
5638 if (IntersectRect(&rcScroll, &rcScroll, &infoPtr->rcList))
5639 ScrollWindowEx(infoPtr->hwndSelf, 0, dir * infoPtr->nItemHeight,
5640 &rcScroll, &rcScroll, 0, 0, SW_ERASE | SW_INVALIDATE);
5643 /***
5644 * DESCRIPTION:
5645 * Removes an item from the listview control.
5647 * PARAMETER(S):
5648 * [I] infoPtr : valid pointer to the listview structure
5649 * [I] nItem : item index
5651 * RETURN:
5652 * SUCCESS : TRUE
5653 * FAILURE : FALSE
5655 static BOOL LISTVIEW_DeleteItem(LISTVIEW_INFO *infoPtr, INT nItem)
5657 LVITEMW item;
5658 const BOOL is_icon = (infoPtr->uView == LV_VIEW_SMALLICON || infoPtr->uView == LV_VIEW_ICON);
5660 TRACE("(nItem=%d)\n", nItem);
5662 if (nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
5664 /* remove selection, and focus */
5665 item.state = 0;
5666 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
5667 LISTVIEW_SetItemState(infoPtr, nItem, &item);
5669 /* send LVN_DELETEITEM notification. */
5670 if (!notify_deleteitem(infoPtr, nItem)) return FALSE;
5672 /* we need to do this here, because we'll be deleting stuff */
5673 if (is_icon)
5674 LISTVIEW_InvalidateItem(infoPtr, nItem);
5676 if (!(infoPtr->dwStyle & LVS_OWNERDATA))
5678 HDPA hdpaSubItems;
5679 ITEMHDR *hdrItem;
5680 ITEM_INFO *lpItem;
5681 ITEM_ID *lpID;
5682 INT i;
5684 hdpaSubItems = DPA_DeletePtr(infoPtr->hdpaItems, nItem);
5685 lpItem = DPA_GetPtr(hdpaSubItems, 0);
5687 /* free id struct */
5688 i = DPA_GetPtrIndex(infoPtr->hdpaItemIds, lpItem->id);
5689 lpID = DPA_GetPtr(infoPtr->hdpaItemIds, i);
5690 DPA_DeletePtr(infoPtr->hdpaItemIds, i);
5691 Free(lpID);
5692 for (i = 0; i < DPA_GetPtrCount(hdpaSubItems); i++)
5694 hdrItem = DPA_GetPtr(hdpaSubItems, i);
5695 if (is_text(hdrItem->pszText)) Free(hdrItem->pszText);
5696 Free(hdrItem);
5698 DPA_Destroy(hdpaSubItems);
5701 if (is_icon)
5703 DPA_DeletePtr(infoPtr->hdpaPosX, nItem);
5704 DPA_DeletePtr(infoPtr->hdpaPosY, nItem);
5707 infoPtr->nItemCount--;
5708 LISTVIEW_ShiftIndices(infoPtr, nItem, -1);
5710 /* now is the invalidation fun */
5711 if (!is_icon)
5712 LISTVIEW_ScrollOnInsert(infoPtr, nItem, -1);
5713 return TRUE;
5717 /***
5718 * DESCRIPTION:
5719 * Callback implementation for editlabel control
5721 * PARAMETER(S):
5722 * [I] infoPtr : valid pointer to the listview structure
5723 * [I] storeText : store edit box text as item text
5724 * [I] isW : TRUE if psxText is Unicode, FALSE if it's ANSI
5726 * RETURN:
5727 * SUCCESS : TRUE
5728 * FAILURE : FALSE
5730 static BOOL LISTVIEW_EndEditLabelT(LISTVIEW_INFO *infoPtr, BOOL storeText, BOOL isW)
5732 HWND hwndSelf = infoPtr->hwndSelf;
5733 WCHAR szDispText[DISP_TEXT_SIZE] = { 0 };
5734 NMLVDISPINFOW dispInfo;
5735 INT editedItem = infoPtr->nEditLabelItem;
5736 BOOL same;
5737 WCHAR *pszText = NULL;
5738 BOOL res;
5740 if (storeText)
5742 DWORD len = isW ? GetWindowTextLengthW(infoPtr->hwndEdit) : GetWindowTextLengthA(infoPtr->hwndEdit);
5744 if (len)
5746 if ((pszText = Alloc((len+1) * (isW ? sizeof(WCHAR) : sizeof(CHAR)))))
5748 if (isW) GetWindowTextW(infoPtr->hwndEdit, pszText, len+1);
5749 else GetWindowTextA(infoPtr->hwndEdit, (CHAR*)pszText, len+1);
5754 TRACE("(pszText=%s, isW=%d)\n", debugtext_t(pszText, isW), isW);
5756 ZeroMemory(&dispInfo, sizeof(dispInfo));
5757 dispInfo.item.mask = LVIF_PARAM | LVIF_STATE | LVIF_TEXT;
5758 dispInfo.item.iItem = editedItem;
5759 dispInfo.item.iSubItem = 0;
5760 dispInfo.item.stateMask = ~0;
5761 dispInfo.item.pszText = szDispText;
5762 dispInfo.item.cchTextMax = DISP_TEXT_SIZE;
5763 if (!LISTVIEW_GetItemT(infoPtr, &dispInfo.item, isW))
5765 res = FALSE;
5766 goto cleanup;
5769 if (isW)
5770 same = (lstrcmpW(dispInfo.item.pszText, pszText) == 0);
5771 else
5773 LPWSTR tmp = textdupTtoW(pszText, FALSE);
5774 same = (lstrcmpW(dispInfo.item.pszText, tmp) == 0);
5775 textfreeT(tmp, FALSE);
5778 /* add the text from the edit in */
5779 dispInfo.item.mask |= LVIF_TEXT;
5780 dispInfo.item.pszText = same ? NULL : pszText;
5781 dispInfo.item.cchTextMax = textlenT(dispInfo.item.pszText, isW);
5783 /* Do we need to update the Item Text */
5784 res = notify_dispinfoT(infoPtr, LVN_ENDLABELEDITW, &dispInfo, isW);
5786 infoPtr->nEditLabelItem = -1;
5787 infoPtr->hwndEdit = 0;
5789 if (!res) goto cleanup;
5791 if (!IsWindow(hwndSelf))
5793 res = FALSE;
5794 goto cleanup;
5796 if (!pszText) return TRUE;
5797 if (same)
5799 res = TRUE;
5800 goto cleanup;
5803 if (!(infoPtr->dwStyle & LVS_OWNERDATA))
5805 HDPA hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, editedItem);
5806 ITEM_INFO* lpItem = DPA_GetPtr(hdpaSubItems, 0);
5807 if (lpItem && lpItem->hdr.pszText == LPSTR_TEXTCALLBACKW)
5809 LISTVIEW_InvalidateItem(infoPtr, editedItem);
5810 res = TRUE;
5811 goto cleanup;
5815 ZeroMemory(&dispInfo, sizeof(dispInfo));
5816 dispInfo.item.mask = LVIF_TEXT;
5817 dispInfo.item.iItem = editedItem;
5818 dispInfo.item.iSubItem = 0;
5819 dispInfo.item.pszText = pszText;
5820 dispInfo.item.cchTextMax = textlenT(pszText, isW);
5821 res = LISTVIEW_SetItemT(infoPtr, &dispInfo.item, isW);
5823 cleanup:
5824 Free(pszText);
5826 return res;
5829 /***
5830 * DESCRIPTION:
5831 * Subclassed edit control windproc function
5833 * PARAMETER(S):
5834 * [I] hwnd : the edit window handle
5835 * [I] uMsg : the message that is to be processed
5836 * [I] wParam : first message parameter
5837 * [I] lParam : second message parameter
5838 * [I] isW : TRUE if input is Unicode
5840 * RETURN:
5841 * Zero.
5843 static LRESULT EditLblWndProcT(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL isW)
5845 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongPtrW(GetParent(hwnd), 0);
5846 BOOL save = TRUE;
5848 TRACE("(hwnd=%p, uMsg=%x, wParam=%lx, lParam=%lx, isW=%d)\n",
5849 hwnd, uMsg, wParam, lParam, isW);
5851 switch (uMsg)
5853 case WM_GETDLGCODE:
5854 return DLGC_WANTARROWS | DLGC_WANTALLKEYS;
5856 case WM_DESTROY:
5858 WNDPROC editProc = infoPtr->EditWndProc;
5859 infoPtr->EditWndProc = 0;
5860 SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (DWORD_PTR)editProc);
5861 return CallWindowProcT(editProc, hwnd, uMsg, wParam, lParam, isW);
5864 case WM_KEYDOWN:
5865 if (VK_ESCAPE == (INT)wParam)
5867 save = FALSE;
5868 break;
5870 else if (VK_RETURN == (INT)wParam)
5871 break;
5873 default:
5874 return CallWindowProcT(infoPtr->EditWndProc, hwnd, uMsg, wParam, lParam, isW);
5877 /* kill the edit */
5878 if (infoPtr->hwndEdit)
5879 LISTVIEW_EndEditLabelT(infoPtr, save, isW);
5881 SendMessageW(hwnd, WM_CLOSE, 0, 0);
5882 return 0;
5885 /***
5886 * DESCRIPTION:
5887 * Subclassed edit control Unicode windproc function
5889 * PARAMETER(S):
5890 * [I] hwnd : the edit window handle
5891 * [I] uMsg : the message that is to be processed
5892 * [I] wParam : first message parameter
5893 * [I] lParam : second message parameter
5895 * RETURN:
5897 static LRESULT CALLBACK EditLblWndProcW(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
5899 return EditLblWndProcT(hwnd, uMsg, wParam, lParam, TRUE);
5902 /***
5903 * DESCRIPTION:
5904 * Subclassed edit control ANSI windproc function
5906 * PARAMETER(S):
5907 * [I] hwnd : the edit window handle
5908 * [I] uMsg : the message that is to be processed
5909 * [I] wParam : first message parameter
5910 * [I] lParam : second message parameter
5912 * RETURN:
5914 static LRESULT CALLBACK EditLblWndProcA(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
5916 return EditLblWndProcT(hwnd, uMsg, wParam, lParam, FALSE);
5919 /***
5920 * DESCRIPTION:
5921 * Creates a subclassed edit control
5923 * PARAMETER(S):
5924 * [I] infoPtr : valid pointer to the listview structure
5925 * [I] text : initial text for the edit
5926 * [I] style : the window style
5927 * [I] isW : TRUE if input is Unicode
5929 * RETURN:
5931 static HWND CreateEditLabelT(LISTVIEW_INFO *infoPtr, LPCWSTR text, BOOL isW)
5933 static const DWORD style = WS_CHILDWINDOW|WS_CLIPSIBLINGS|ES_LEFT|ES_AUTOHSCROLL|WS_BORDER|WS_VISIBLE;
5934 HINSTANCE hinst = (HINSTANCE)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_HINSTANCE);
5935 HWND hedit;
5937 TRACE("(%p, text=%s, isW=%d)\n", infoPtr, debugtext_t(text, isW), isW);
5939 /* window will be resized and positioned after LVN_BEGINLABELEDIT */
5940 if (isW)
5941 hedit = CreateWindowW(WC_EDITW, text, style, 0, 0, 0, 0, infoPtr->hwndSelf, 0, hinst, 0);
5942 else
5943 hedit = CreateWindowA(WC_EDITA, (LPCSTR)text, style, 0, 0, 0, 0, infoPtr->hwndSelf, 0, hinst, 0);
5945 if (!hedit) return 0;
5947 infoPtr->EditWndProc = (WNDPROC)
5948 (isW ? SetWindowLongPtrW(hedit, GWLP_WNDPROC, (DWORD_PTR)EditLblWndProcW) :
5949 SetWindowLongPtrA(hedit, GWLP_WNDPROC, (DWORD_PTR)EditLblWndProcA) );
5951 SendMessageW(hedit, WM_SETFONT, (WPARAM)infoPtr->hFont, FALSE);
5952 SendMessageW(hedit, EM_SETLIMITTEXT, DISP_TEXT_SIZE-1, 0);
5954 return hedit;
5957 /***
5958 * DESCRIPTION:
5959 * Begin in place editing of specified list view item
5961 * PARAMETER(S):
5962 * [I] infoPtr : valid pointer to the listview structure
5963 * [I] nItem : item index
5964 * [I] isW : TRUE if it's a Unicode req, FALSE if ASCII
5966 * RETURN:
5967 * SUCCESS : TRUE
5968 * FAILURE : FALSE
5970 static HWND LISTVIEW_EditLabelT(LISTVIEW_INFO *infoPtr, INT nItem, BOOL isW)
5972 WCHAR disptextW[DISP_TEXT_SIZE] = { 0 };
5973 HWND hwndSelf = infoPtr->hwndSelf;
5974 NMLVDISPINFOW dispInfo;
5975 HFONT hOldFont = NULL;
5976 TEXTMETRICW tm;
5977 RECT rect;
5978 SIZE sz;
5979 HDC hdc;
5981 TRACE("(nItem=%d, isW=%d)\n", nItem, isW);
5983 if (~infoPtr->dwStyle & LVS_EDITLABELS) return 0;
5985 /* remove existing edit box */
5986 if (infoPtr->hwndEdit)
5988 SetFocus(infoPtr->hwndSelf);
5989 infoPtr->hwndEdit = 0;
5992 if (nItem < 0 || nItem >= infoPtr->nItemCount) return 0;
5994 infoPtr->nEditLabelItem = nItem;
5996 LISTVIEW_SetSelection(infoPtr, nItem);
5997 LISTVIEW_SetItemFocus(infoPtr, nItem);
5998 LISTVIEW_InvalidateItem(infoPtr, nItem);
6000 rect.left = LVIR_LABEL;
6001 if (!LISTVIEW_GetItemRect(infoPtr, nItem, &rect)) return 0;
6003 ZeroMemory(&dispInfo, sizeof(dispInfo));
6004 dispInfo.item.mask = LVIF_PARAM | LVIF_STATE | LVIF_TEXT;
6005 dispInfo.item.iItem = nItem;
6006 dispInfo.item.iSubItem = 0;
6007 dispInfo.item.stateMask = ~0;
6008 dispInfo.item.pszText = disptextW;
6009 dispInfo.item.cchTextMax = DISP_TEXT_SIZE;
6010 if (!LISTVIEW_GetItemT(infoPtr, &dispInfo.item, isW)) return 0;
6012 infoPtr->hwndEdit = CreateEditLabelT(infoPtr, dispInfo.item.pszText, isW);
6013 if (!infoPtr->hwndEdit) return 0;
6015 if (notify_dispinfoT(infoPtr, LVN_BEGINLABELEDITW, &dispInfo, isW))
6017 if (!IsWindow(hwndSelf))
6018 return 0;
6019 SendMessageW(infoPtr->hwndEdit, WM_CLOSE, 0, 0);
6020 infoPtr->hwndEdit = 0;
6021 return 0;
6024 TRACE("disp text=%s\n", debugtext_t(dispInfo.item.pszText, isW));
6026 /* position and display edit box */
6027 hdc = GetDC(infoPtr->hwndSelf);
6029 /* select the font to get appropriate metric dimensions */
6030 if (infoPtr->hFont)
6031 hOldFont = SelectObject(hdc, infoPtr->hFont);
6033 /* use real edit box content, it could be altered during LVN_BEGINLABELEDIT notification */
6034 GetWindowTextW(infoPtr->hwndEdit, disptextW, DISP_TEXT_SIZE);
6035 TRACE("edit box text=%s\n", debugstr_w(disptextW));
6037 /* get string length in pixels */
6038 GetTextExtentPoint32W(hdc, disptextW, lstrlenW(disptextW), &sz);
6040 /* add extra spacing for the next character */
6041 GetTextMetricsW(hdc, &tm);
6042 sz.cx += tm.tmMaxCharWidth * 2;
6044 if (infoPtr->hFont)
6045 SelectObject(hdc, hOldFont);
6047 ReleaseDC(infoPtr->hwndSelf, hdc);
6049 sz.cy = rect.bottom - rect.top + 2;
6050 rect.left -= 2;
6051 rect.top -= 1;
6052 TRACE("moving edit=(%d,%d)-(%d,%d)\n", rect.left, rect.top, sz.cx, sz.cy);
6053 MoveWindow(infoPtr->hwndEdit, rect.left, rect.top, sz.cx, sz.cy, FALSE);
6054 ShowWindow(infoPtr->hwndEdit, SW_NORMAL);
6055 SetFocus(infoPtr->hwndEdit);
6056 SendMessageW(infoPtr->hwndEdit, EM_SETSEL, 0, -1);
6057 return infoPtr->hwndEdit;
6061 /***
6062 * DESCRIPTION:
6063 * Ensures the specified item is visible, scrolling into view if necessary.
6065 * PARAMETER(S):
6066 * [I] infoPtr : valid pointer to the listview structure
6067 * [I] nItem : item index
6068 * [I] bPartial : partially or entirely visible
6070 * RETURN:
6071 * SUCCESS : TRUE
6072 * FAILURE : FALSE
6074 static BOOL LISTVIEW_EnsureVisible(LISTVIEW_INFO *infoPtr, INT nItem, BOOL bPartial)
6076 INT nScrollPosHeight = 0;
6077 INT nScrollPosWidth = 0;
6078 INT nHorzAdjust = 0;
6079 INT nVertAdjust = 0;
6080 INT nHorzDiff = 0;
6081 INT nVertDiff = 0;
6082 RECT rcItem, rcTemp;
6084 rcItem.left = LVIR_BOUNDS;
6085 if (!LISTVIEW_GetItemRect(infoPtr, nItem, &rcItem)) return FALSE;
6087 if (bPartial && IntersectRect(&rcTemp, &infoPtr->rcList, &rcItem)) return TRUE;
6089 if (rcItem.left < infoPtr->rcList.left || rcItem.right > infoPtr->rcList.right)
6091 /* scroll left/right, but in LV_VIEW_DETAILS mode */
6092 if (infoPtr->uView == LV_VIEW_LIST)
6093 nScrollPosWidth = infoPtr->nItemWidth;
6094 else if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
6095 nScrollPosWidth = 1;
6097 if (rcItem.left < infoPtr->rcList.left)
6099 nHorzAdjust = -1;
6100 if (infoPtr->uView != LV_VIEW_DETAILS) nHorzDiff = rcItem.left - infoPtr->rcList.left;
6102 else
6104 nHorzAdjust = 1;
6105 if (infoPtr->uView != LV_VIEW_DETAILS) nHorzDiff = rcItem.right - infoPtr->rcList.right;
6109 if (rcItem.top < infoPtr->rcList.top || rcItem.bottom > infoPtr->rcList.bottom)
6111 /* scroll up/down, but not in LVS_LIST mode */
6112 if (infoPtr->uView == LV_VIEW_DETAILS)
6113 nScrollPosHeight = infoPtr->nItemHeight;
6114 else if ((infoPtr->uView == LV_VIEW_ICON) || (infoPtr->uView == LV_VIEW_SMALLICON))
6115 nScrollPosHeight = 1;
6117 if (rcItem.top < infoPtr->rcList.top)
6119 nVertAdjust = -1;
6120 if (infoPtr->uView != LV_VIEW_LIST) nVertDiff = rcItem.top - infoPtr->rcList.top;
6122 else
6124 nVertAdjust = 1;
6125 if (infoPtr->uView != LV_VIEW_LIST) nVertDiff = rcItem.bottom - infoPtr->rcList.bottom;
6129 if (!nScrollPosWidth && !nScrollPosHeight) return TRUE;
6131 if (nScrollPosWidth)
6133 INT diff = nHorzDiff / nScrollPosWidth;
6134 if (nHorzDiff % nScrollPosWidth) diff += nHorzAdjust;
6135 LISTVIEW_HScroll(infoPtr, SB_INTERNAL, diff);
6138 if (nScrollPosHeight)
6140 INT diff = nVertDiff / nScrollPosHeight;
6141 if (nVertDiff % nScrollPosHeight) diff += nVertAdjust;
6142 LISTVIEW_VScroll(infoPtr, SB_INTERNAL, diff);
6145 return TRUE;
6148 /***
6149 * DESCRIPTION:
6150 * Searches for an item with specific characteristics.
6152 * PARAMETER(S):
6153 * [I] hwnd : window handle
6154 * [I] nStart : base item index
6155 * [I] lpFindInfo : item information to look for
6157 * RETURN:
6158 * SUCCESS : index of item
6159 * FAILURE : -1
6161 static INT LISTVIEW_FindItemW(const LISTVIEW_INFO *infoPtr, INT nStart,
6162 const LVFINDINFOW *lpFindInfo)
6164 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
6165 BOOL bWrap = FALSE, bNearest = FALSE;
6166 INT nItem = nStart + 1, nLast = infoPtr->nItemCount, nNearestItem = -1;
6167 ULONG xdist, ydist, dist, mindist = 0x7fffffff;
6168 POINT Position, Destination;
6169 LVITEMW lvItem;
6171 /* Search in virtual listviews should be done by application, not by
6172 listview control, so we just send LVN_ODFINDITEMW and return the result */
6173 if (infoPtr->dwStyle & LVS_OWNERDATA)
6175 NMLVFINDITEMW nmlv;
6177 nmlv.iStart = nStart;
6178 nmlv.lvfi = *lpFindInfo;
6179 return notify_hdr(infoPtr, LVN_ODFINDITEMW, (LPNMHDR)&nmlv.hdr);
6182 if (!lpFindInfo || nItem < 0) return -1;
6184 lvItem.mask = 0;
6185 if (lpFindInfo->flags & (LVFI_STRING | LVFI_PARTIAL) ||
6186 lpFindInfo->flags & LVFI_SUBSTRING)
6188 lvItem.mask |= LVIF_TEXT;
6189 lvItem.pszText = szDispText;
6190 lvItem.cchTextMax = DISP_TEXT_SIZE;
6193 if (lpFindInfo->flags & LVFI_WRAP)
6194 bWrap = TRUE;
6196 if ((lpFindInfo->flags & LVFI_NEARESTXY) &&
6197 (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON))
6199 POINT Origin;
6200 RECT rcArea;
6202 LISTVIEW_GetOrigin(infoPtr, &Origin);
6203 Destination.x = lpFindInfo->pt.x - Origin.x;
6204 Destination.y = lpFindInfo->pt.y - Origin.y;
6205 switch(lpFindInfo->vkDirection)
6207 case VK_DOWN: Destination.y += infoPtr->nItemHeight; break;
6208 case VK_UP: Destination.y -= infoPtr->nItemHeight; break;
6209 case VK_RIGHT: Destination.x += infoPtr->nItemWidth; break;
6210 case VK_LEFT: Destination.x -= infoPtr->nItemWidth; break;
6211 case VK_HOME: Destination.x = Destination.y = 0; break;
6212 case VK_NEXT: Destination.y += infoPtr->rcList.bottom - infoPtr->rcList.top; break;
6213 case VK_PRIOR: Destination.y -= infoPtr->rcList.bottom - infoPtr->rcList.top; break;
6214 case VK_END:
6215 LISTVIEW_GetAreaRect(infoPtr, &rcArea);
6216 Destination.x = rcArea.right;
6217 Destination.y = rcArea.bottom;
6218 break;
6219 default: ERR("Unknown vkDirection=%d\n", lpFindInfo->vkDirection);
6221 bNearest = TRUE;
6223 else Destination.x = Destination.y = 0;
6225 /* if LVFI_PARAM is specified, all other flags are ignored */
6226 if (lpFindInfo->flags & LVFI_PARAM)
6228 lvItem.mask |= LVIF_PARAM;
6229 bNearest = FALSE;
6230 lvItem.mask &= ~LVIF_TEXT;
6233 again:
6234 for (; nItem < nLast; nItem++)
6236 lvItem.iItem = nItem;
6237 lvItem.iSubItem = 0;
6238 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) continue;
6240 if (lvItem.mask & LVIF_PARAM)
6242 if (lpFindInfo->lParam == lvItem.lParam)
6243 return nItem;
6244 else
6245 continue;
6248 if (lvItem.mask & LVIF_TEXT)
6250 if (lpFindInfo->flags & (LVFI_PARTIAL | LVFI_SUBSTRING))
6252 WCHAR *p = strstrW(lvItem.pszText, lpFindInfo->psz);
6253 if (!p || p != lvItem.pszText) continue;
6255 else
6257 if (lstrcmpW(lvItem.pszText, lpFindInfo->psz) != 0) continue;
6261 if (!bNearest) return nItem;
6263 /* This is very inefficient. To do a good job here,
6264 * we need a sorted array of (x,y) item positions */
6265 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
6267 /* compute the distance^2 to the destination */
6268 xdist = Destination.x - Position.x;
6269 ydist = Destination.y - Position.y;
6270 dist = xdist * xdist + ydist * ydist;
6272 /* remember the distance, and item if it's closer */
6273 if (dist < mindist)
6275 mindist = dist;
6276 nNearestItem = nItem;
6280 if (bWrap)
6282 nItem = 0;
6283 nLast = min(nStart + 1, infoPtr->nItemCount);
6284 bWrap = FALSE;
6285 goto again;
6288 return nNearestItem;
6291 /***
6292 * DESCRIPTION:
6293 * Searches for an item with specific characteristics.
6295 * PARAMETER(S):
6296 * [I] hwnd : window handle
6297 * [I] nStart : base item index
6298 * [I] lpFindInfo : item information to look for
6300 * RETURN:
6301 * SUCCESS : index of item
6302 * FAILURE : -1
6304 static INT LISTVIEW_FindItemA(const LISTVIEW_INFO *infoPtr, INT nStart,
6305 const LVFINDINFOA *lpFindInfo)
6307 BOOL hasText = lpFindInfo->flags & (LVFI_STRING | LVFI_PARTIAL) ||
6308 lpFindInfo->flags & LVFI_SUBSTRING;
6309 LVFINDINFOW fiw;
6310 INT res;
6311 LPWSTR strW = NULL;
6313 memcpy(&fiw, lpFindInfo, sizeof(fiw));
6314 if (hasText) fiw.psz = strW = textdupTtoW((LPCWSTR)lpFindInfo->psz, FALSE);
6315 res = LISTVIEW_FindItemW(infoPtr, nStart, &fiw);
6316 textfreeT(strW, FALSE);
6317 return res;
6320 /***
6321 * DESCRIPTION:
6322 * Retrieves column attributes.
6324 * PARAMETER(S):
6325 * [I] infoPtr : valid pointer to the listview structure
6326 * [I] nColumn : column index
6327 * [IO] lpColumn : column information
6328 * [I] isW : if TRUE, then lpColumn is a LPLVCOLUMNW
6329 * otherwise it is in fact a LPLVCOLUMNA
6331 * RETURN:
6332 * SUCCESS : TRUE
6333 * FAILURE : FALSE
6335 static BOOL LISTVIEW_GetColumnT(const LISTVIEW_INFO *infoPtr, INT nColumn, LPLVCOLUMNW lpColumn, BOOL isW)
6337 COLUMN_INFO *lpColumnInfo;
6338 HDITEMW hdi;
6340 if (!lpColumn || nColumn < 0 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
6341 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, nColumn);
6343 /* initialize memory */
6344 ZeroMemory(&hdi, sizeof(hdi));
6346 if (lpColumn->mask & LVCF_TEXT)
6348 hdi.mask |= HDI_TEXT;
6349 hdi.pszText = lpColumn->pszText;
6350 hdi.cchTextMax = lpColumn->cchTextMax;
6353 if (lpColumn->mask & LVCF_IMAGE)
6354 hdi.mask |= HDI_IMAGE;
6356 if (lpColumn->mask & LVCF_ORDER)
6357 hdi.mask |= HDI_ORDER;
6359 if (lpColumn->mask & LVCF_SUBITEM)
6360 hdi.mask |= HDI_LPARAM;
6362 if (!SendMessageW(infoPtr->hwndHeader, isW ? HDM_GETITEMW : HDM_GETITEMA, nColumn, (LPARAM)&hdi)) return FALSE;
6364 if (lpColumn->mask & LVCF_FMT)
6365 lpColumn->fmt = lpColumnInfo->fmt;
6367 if (lpColumn->mask & LVCF_WIDTH)
6368 lpColumn->cx = lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left;
6370 if (lpColumn->mask & LVCF_IMAGE)
6371 lpColumn->iImage = hdi.iImage;
6373 if (lpColumn->mask & LVCF_ORDER)
6374 lpColumn->iOrder = hdi.iOrder;
6376 if (lpColumn->mask & LVCF_SUBITEM)
6377 lpColumn->iSubItem = hdi.lParam;
6379 if (lpColumn->mask & LVCF_MINWIDTH)
6380 lpColumn->cxMin = lpColumnInfo->cxMin;
6382 return TRUE;
6386 static BOOL LISTVIEW_GetColumnOrderArray(const LISTVIEW_INFO *infoPtr, INT iCount, LPINT lpiArray)
6388 TRACE("iCount=%d, lpiArray=%p\n", iCount, lpiArray);
6390 if (!lpiArray)
6391 return FALSE;
6393 return SendMessageW(infoPtr->hwndHeader, HDM_GETORDERARRAY, iCount, (LPARAM)lpiArray);
6396 /***
6397 * DESCRIPTION:
6398 * Retrieves the column width.
6400 * PARAMETER(S):
6401 * [I] infoPtr : valid pointer to the listview structure
6402 * [I] int : column index
6404 * RETURN:
6405 * SUCCESS : column width
6406 * FAILURE : zero
6408 static INT LISTVIEW_GetColumnWidth(const LISTVIEW_INFO *infoPtr, INT nColumn)
6410 INT nColumnWidth = 0;
6411 HDITEMW hdItem;
6413 TRACE("nColumn=%d\n", nColumn);
6415 /* we have a 'column' in LIST and REPORT mode only */
6416 switch(infoPtr->uView)
6418 case LV_VIEW_LIST:
6419 nColumnWidth = infoPtr->nItemWidth;
6420 break;
6421 case LV_VIEW_DETAILS:
6422 /* We are not using LISTVIEW_GetHeaderRect as this data is updated only after a HDN_ITEMCHANGED.
6423 * There is an application that subclasses the listview, calls LVM_GETCOLUMNWIDTH in the
6424 * HDN_ITEMCHANGED handler and goes into infinite recursion if it receives old data.
6426 * TODO: should we do the same in LVM_GETCOLUMN?
6428 hdItem.mask = HDI_WIDTH;
6429 if (!SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nColumn, (LPARAM)&hdItem))
6431 WARN("(%p): HDM_GETITEMW failed for item %d\n", infoPtr->hwndSelf, nColumn);
6432 return 0;
6434 nColumnWidth = hdItem.cxy;
6435 break;
6438 TRACE("nColumnWidth=%d\n", nColumnWidth);
6439 return nColumnWidth;
6442 /***
6443 * DESCRIPTION:
6444 * In list or report display mode, retrieves the number of items that can fit
6445 * vertically in the visible area. In icon or small icon display mode,
6446 * retrieves the total number of visible items.
6448 * PARAMETER(S):
6449 * [I] infoPtr : valid pointer to the listview structure
6451 * RETURN:
6452 * Number of fully visible items.
6454 static INT LISTVIEW_GetCountPerPage(const LISTVIEW_INFO *infoPtr)
6456 switch (infoPtr->uView)
6458 case LV_VIEW_ICON:
6459 case LV_VIEW_SMALLICON:
6460 return infoPtr->nItemCount;
6461 case LV_VIEW_DETAILS:
6462 return LISTVIEW_GetCountPerColumn(infoPtr);
6463 case LV_VIEW_LIST:
6464 return LISTVIEW_GetCountPerRow(infoPtr) * LISTVIEW_GetCountPerColumn(infoPtr);
6466 assert(FALSE);
6467 return 0;
6470 /***
6471 * DESCRIPTION:
6472 * Retrieves an image list handle.
6474 * PARAMETER(S):
6475 * [I] infoPtr : valid pointer to the listview structure
6476 * [I] nImageList : image list identifier
6478 * RETURN:
6479 * SUCCESS : image list handle
6480 * FAILURE : NULL
6482 static HIMAGELIST LISTVIEW_GetImageList(const LISTVIEW_INFO *infoPtr, INT nImageList)
6484 switch (nImageList)
6486 case LVSIL_NORMAL: return infoPtr->himlNormal;
6487 case LVSIL_SMALL: return infoPtr->himlSmall;
6488 case LVSIL_STATE: return infoPtr->himlState;
6489 case LVSIL_GROUPHEADER:
6490 FIXME("LVSIL_GROUPHEADER not supported\n");
6491 break;
6492 default:
6493 WARN("got unknown imagelist index - %d\n", nImageList);
6495 return NULL;
6498 /* LISTVIEW_GetISearchString */
6500 /***
6501 * DESCRIPTION:
6502 * Retrieves item attributes.
6504 * PARAMETER(S):
6505 * [I] hwnd : window handle
6506 * [IO] lpLVItem : item info
6507 * [I] isW : if TRUE, then lpLVItem is a LPLVITEMW,
6508 * if FALSE, then lpLVItem is a LPLVITEMA.
6510 * NOTE:
6511 * This is the internal 'GetItem' interface -- it tries to
6512 * be smart and avoid text copies, if possible, by modifying
6513 * lpLVItem->pszText to point to the text string. Please note
6514 * that this is not always possible (e.g. OWNERDATA), so on
6515 * entry you *must* supply valid values for pszText, and cchTextMax.
6516 * The only difference to the documented interface is that upon
6517 * return, you should use *only* the lpLVItem->pszText, rather than
6518 * the buffer pointer you provided on input. Most code already does
6519 * that, so it's not a problem.
6520 * For the two cases when the text must be copied (that is,
6521 * for LVM_GETITEM, and LVM_GETITEMTEXT), use LISTVIEW_GetItemExtT.
6523 * RETURN:
6524 * SUCCESS : TRUE
6525 * FAILURE : FALSE
6527 static BOOL LISTVIEW_GetItemT(const LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem, BOOL isW)
6529 ITEMHDR callbackHdr = { LPSTR_TEXTCALLBACKW, I_IMAGECALLBACK };
6530 NMLVDISPINFOW dispInfo;
6531 ITEM_INFO *lpItem;
6532 ITEMHDR* pItemHdr;
6533 HDPA hdpaSubItems;
6534 INT isubitem;
6536 TRACE("(lpLVItem=%s, isW=%d)\n", debuglvitem_t(lpLVItem, isW), isW);
6538 if (!lpLVItem || lpLVItem->iItem < 0 || lpLVItem->iItem >= infoPtr->nItemCount)
6539 return FALSE;
6541 if (lpLVItem->mask == 0) return TRUE;
6542 TRACE("mask=%x\n", lpLVItem->mask);
6544 /* make a local copy */
6545 isubitem = lpLVItem->iSubItem;
6547 /* a quick optimization if all we're asked is the focus state
6548 * these queries are worth optimising since they are common,
6549 * and can be answered in constant time, without the heavy accesses */
6550 if ( (lpLVItem->mask == LVIF_STATE) && (lpLVItem->stateMask == LVIS_FOCUSED) &&
6551 !(infoPtr->uCallbackMask & LVIS_FOCUSED) )
6553 lpLVItem->state = 0;
6554 if (infoPtr->nFocusedItem == lpLVItem->iItem)
6555 lpLVItem->state |= LVIS_FOCUSED;
6556 return TRUE;
6559 ZeroMemory(&dispInfo, sizeof(dispInfo));
6561 /* if the app stores all the data, handle it separately */
6562 if (infoPtr->dwStyle & LVS_OWNERDATA)
6564 dispInfo.item.state = 0;
6566 /* apparently, we should not callback for lParam in LVS_OWNERDATA */
6567 if ((lpLVItem->mask & ~(LVIF_STATE | LVIF_PARAM)) ||
6568 ((lpLVItem->mask & LVIF_STATE) && (infoPtr->uCallbackMask & lpLVItem->stateMask)))
6570 UINT mask = lpLVItem->mask;
6572 /* NOTE: copy only fields which we _know_ are initialized, some apps
6573 * depend on the uninitialized fields being 0 */
6574 dispInfo.item.mask = lpLVItem->mask & ~LVIF_PARAM;
6575 dispInfo.item.iItem = lpLVItem->iItem;
6576 dispInfo.item.iSubItem = isubitem;
6577 if (lpLVItem->mask & LVIF_TEXT)
6579 if (lpLVItem->mask & LVIF_NORECOMPUTE)
6580 /* reset mask */
6581 dispInfo.item.mask &= ~(LVIF_TEXT | LVIF_NORECOMPUTE);
6582 else
6584 dispInfo.item.pszText = lpLVItem->pszText;
6585 dispInfo.item.cchTextMax = lpLVItem->cchTextMax;
6588 if (lpLVItem->mask & LVIF_STATE)
6589 dispInfo.item.stateMask = lpLVItem->stateMask & infoPtr->uCallbackMask;
6590 /* could be zeroed on LVIF_NORECOMPUTE case */
6591 if (dispInfo.item.mask)
6593 notify_dispinfoT(infoPtr, LVN_GETDISPINFOW, &dispInfo, isW);
6594 dispInfo.item.stateMask = lpLVItem->stateMask;
6595 if (lpLVItem->mask & (LVIF_GROUPID|LVIF_COLUMNS))
6597 /* full size structure expected - _WIN32IE >= 0x560 */
6598 *lpLVItem = dispInfo.item;
6600 else if (lpLVItem->mask & LVIF_INDENT)
6602 /* indent member expected - _WIN32IE >= 0x300 */
6603 memcpy(lpLVItem, &dispInfo.item, offsetof( LVITEMW, iGroupId ));
6605 else
6607 /* minimal structure expected */
6608 memcpy(lpLVItem, &dispInfo.item, offsetof( LVITEMW, iIndent ));
6610 lpLVItem->mask = mask;
6611 TRACE(" getdispinfo(1):lpLVItem=%s\n", debuglvitem_t(lpLVItem, isW));
6615 /* make sure lParam is zeroed out */
6616 if (lpLVItem->mask & LVIF_PARAM) lpLVItem->lParam = 0;
6618 /* callback marked pointer required here */
6619 if ((lpLVItem->mask & LVIF_TEXT) && (lpLVItem->mask & LVIF_NORECOMPUTE))
6620 lpLVItem->pszText = LPSTR_TEXTCALLBACKW;
6622 /* we store only a little state, so if we're not asked, we're done */
6623 if (!(lpLVItem->mask & LVIF_STATE) || isubitem) return TRUE;
6625 /* if focus is handled by us, report it */
6626 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_FOCUSED )
6628 lpLVItem->state &= ~LVIS_FOCUSED;
6629 if (infoPtr->nFocusedItem == lpLVItem->iItem)
6630 lpLVItem->state |= LVIS_FOCUSED;
6633 /* and do the same for selection, if we handle it */
6634 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_SELECTED )
6636 lpLVItem->state &= ~LVIS_SELECTED;
6637 if (ranges_contain(infoPtr->selectionRanges, lpLVItem->iItem))
6638 lpLVItem->state |= LVIS_SELECTED;
6641 return TRUE;
6644 /* find the item and subitem structures before we proceed */
6645 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, lpLVItem->iItem);
6646 lpItem = DPA_GetPtr(hdpaSubItems, 0);
6647 assert (lpItem);
6649 if (isubitem)
6651 SUBITEM_INFO *lpSubItem = LISTVIEW_GetSubItemPtr(hdpaSubItems, isubitem);
6652 pItemHdr = lpSubItem ? &lpSubItem->hdr : &callbackHdr;
6653 if (!lpSubItem)
6655 WARN(" iSubItem invalid (%08x), ignored.\n", isubitem);
6656 isubitem = 0;
6659 else
6660 pItemHdr = &lpItem->hdr;
6662 /* Do we need to query the state from the app? */
6663 if ((lpLVItem->mask & LVIF_STATE) && infoPtr->uCallbackMask && isubitem == 0)
6665 dispInfo.item.mask |= LVIF_STATE;
6666 dispInfo.item.stateMask = infoPtr->uCallbackMask;
6669 /* Do we need to enquire about the image? */
6670 if ((lpLVItem->mask & LVIF_IMAGE) && pItemHdr->iImage == I_IMAGECALLBACK &&
6671 (isubitem == 0 || (infoPtr->dwLvExStyle & LVS_EX_SUBITEMIMAGES)))
6673 dispInfo.item.mask |= LVIF_IMAGE;
6674 dispInfo.item.iImage = I_IMAGECALLBACK;
6677 /* Only items support indentation */
6678 if ((lpLVItem->mask & LVIF_INDENT) && lpItem->iIndent == I_INDENTCALLBACK &&
6679 (isubitem == 0))
6681 dispInfo.item.mask |= LVIF_INDENT;
6682 dispInfo.item.iIndent = I_INDENTCALLBACK;
6685 /* Apps depend on calling back for text if it is NULL or LPSTR_TEXTCALLBACKW */
6686 if ((lpLVItem->mask & LVIF_TEXT) && !(lpLVItem->mask & LVIF_NORECOMPUTE) &&
6687 !is_text(pItemHdr->pszText))
6689 dispInfo.item.mask |= LVIF_TEXT;
6690 dispInfo.item.pszText = lpLVItem->pszText;
6691 dispInfo.item.cchTextMax = lpLVItem->cchTextMax;
6692 if (dispInfo.item.pszText && dispInfo.item.cchTextMax > 0)
6693 *dispInfo.item.pszText = '\0';
6696 /* If we don't have all the requested info, query the application */
6697 if (dispInfo.item.mask)
6699 dispInfo.item.iItem = lpLVItem->iItem;
6700 dispInfo.item.iSubItem = lpLVItem->iSubItem; /* yes: the original subitem */
6701 dispInfo.item.lParam = lpItem->lParam;
6702 notify_dispinfoT(infoPtr, LVN_GETDISPINFOW, &dispInfo, isW);
6703 TRACE(" getdispinfo(2):item=%s\n", debuglvitem_t(&dispInfo.item, isW));
6706 /* we should not store values for subitems */
6707 if (isubitem) dispInfo.item.mask &= ~LVIF_DI_SETITEM;
6709 /* Now, handle the iImage field */
6710 if (dispInfo.item.mask & LVIF_IMAGE)
6712 lpLVItem->iImage = dispInfo.item.iImage;
6713 if ((dispInfo.item.mask & LVIF_DI_SETITEM) && pItemHdr->iImage == I_IMAGECALLBACK)
6714 pItemHdr->iImage = dispInfo.item.iImage;
6716 else if (lpLVItem->mask & LVIF_IMAGE)
6718 if(isubitem == 0 || (infoPtr->dwLvExStyle & LVS_EX_SUBITEMIMAGES))
6719 lpLVItem->iImage = pItemHdr->iImage;
6720 else
6721 lpLVItem->iImage = 0;
6724 /* The pszText field */
6725 if (dispInfo.item.mask & LVIF_TEXT)
6727 if ((dispInfo.item.mask & LVIF_DI_SETITEM) && pItemHdr->pszText)
6728 textsetptrT(&pItemHdr->pszText, dispInfo.item.pszText, isW);
6730 lpLVItem->pszText = dispInfo.item.pszText;
6732 else if (lpLVItem->mask & LVIF_TEXT)
6734 /* if LVN_GETDISPINFO's disabled with LVIF_NORECOMPUTE return callback placeholder */
6735 if (isW || !is_text(pItemHdr->pszText)) lpLVItem->pszText = pItemHdr->pszText;
6736 else textcpynT(lpLVItem->pszText, isW, pItemHdr->pszText, TRUE, lpLVItem->cchTextMax);
6739 /* Next is the lParam field */
6740 if (dispInfo.item.mask & LVIF_PARAM)
6742 lpLVItem->lParam = dispInfo.item.lParam;
6743 if ((dispInfo.item.mask & LVIF_DI_SETITEM))
6744 lpItem->lParam = dispInfo.item.lParam;
6746 else if (lpLVItem->mask & LVIF_PARAM)
6747 lpLVItem->lParam = lpItem->lParam;
6749 /* if this is a subitem, we're done */
6750 if (isubitem) return TRUE;
6752 /* ... the state field (this one is different due to uCallbackmask) */
6753 if (lpLVItem->mask & LVIF_STATE)
6755 lpLVItem->state = lpItem->state & lpLVItem->stateMask;
6756 if (dispInfo.item.mask & LVIF_STATE)
6758 lpLVItem->state &= ~dispInfo.item.stateMask;
6759 lpLVItem->state |= (dispInfo.item.state & dispInfo.item.stateMask);
6761 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_FOCUSED )
6763 lpLVItem->state &= ~LVIS_FOCUSED;
6764 if (infoPtr->nFocusedItem == lpLVItem->iItem)
6765 lpLVItem->state |= LVIS_FOCUSED;
6767 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_SELECTED )
6769 lpLVItem->state &= ~LVIS_SELECTED;
6770 if (ranges_contain(infoPtr->selectionRanges, lpLVItem->iItem))
6771 lpLVItem->state |= LVIS_SELECTED;
6775 /* and last, but not least, the indent field */
6776 if (dispInfo.item.mask & LVIF_INDENT)
6778 lpLVItem->iIndent = dispInfo.item.iIndent;
6779 if ((dispInfo.item.mask & LVIF_DI_SETITEM) && lpItem->iIndent == I_INDENTCALLBACK)
6780 lpItem->iIndent = dispInfo.item.iIndent;
6782 else if (lpLVItem->mask & LVIF_INDENT)
6784 lpLVItem->iIndent = lpItem->iIndent;
6787 return TRUE;
6790 /***
6791 * DESCRIPTION:
6792 * Retrieves item attributes.
6794 * PARAMETER(S):
6795 * [I] hwnd : window handle
6796 * [IO] lpLVItem : item info
6797 * [I] isW : if TRUE, then lpLVItem is a LPLVITEMW,
6798 * if FALSE, then lpLVItem is a LPLVITEMA.
6800 * NOTE:
6801 * This is the external 'GetItem' interface -- it properly copies
6802 * the text in the provided buffer.
6804 * RETURN:
6805 * SUCCESS : TRUE
6806 * FAILURE : FALSE
6808 static BOOL LISTVIEW_GetItemExtT(const LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem, BOOL isW)
6810 LPWSTR pszText;
6811 BOOL bResult;
6813 if (!lpLVItem || lpLVItem->iItem < 0 || lpLVItem->iItem >= infoPtr->nItemCount)
6814 return FALSE;
6816 pszText = lpLVItem->pszText;
6817 bResult = LISTVIEW_GetItemT(infoPtr, lpLVItem, isW);
6818 if (bResult && (lpLVItem->mask & LVIF_TEXT) && lpLVItem->pszText != pszText)
6820 if (lpLVItem->pszText != LPSTR_TEXTCALLBACKW)
6821 textcpynT(pszText, isW, lpLVItem->pszText, isW, lpLVItem->cchTextMax);
6822 else
6823 pszText = LPSTR_TEXTCALLBACKW;
6825 lpLVItem->pszText = pszText;
6827 return bResult;
6831 /***
6832 * DESCRIPTION:
6833 * Retrieves the position (upper-left) of the listview control item.
6834 * Note that for LVS_ICON style, the upper-left is that of the icon
6835 * and not the bounding box.
6837 * PARAMETER(S):
6838 * [I] infoPtr : valid pointer to the listview structure
6839 * [I] nItem : item index
6840 * [O] lpptPosition : coordinate information
6842 * RETURN:
6843 * SUCCESS : TRUE
6844 * FAILURE : FALSE
6846 static BOOL LISTVIEW_GetItemPosition(const LISTVIEW_INFO *infoPtr, INT nItem, LPPOINT lpptPosition)
6848 POINT Origin;
6850 TRACE("(nItem=%d, lpptPosition=%p)\n", nItem, lpptPosition);
6852 if (!lpptPosition || nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
6854 LISTVIEW_GetOrigin(infoPtr, &Origin);
6855 LISTVIEW_GetItemOrigin(infoPtr, nItem, lpptPosition);
6857 if (infoPtr->uView == LV_VIEW_ICON)
6859 lpptPosition->x += (infoPtr->nItemWidth - infoPtr->iconSize.cx) / 2;
6860 lpptPosition->y += ICON_TOP_PADDING;
6862 lpptPosition->x += Origin.x;
6863 lpptPosition->y += Origin.y;
6865 TRACE (" lpptPosition=%s\n", wine_dbgstr_point(lpptPosition));
6866 return TRUE;
6870 /***
6871 * DESCRIPTION:
6872 * Retrieves the bounding rectangle for a listview control item.
6874 * PARAMETER(S):
6875 * [I] infoPtr : valid pointer to the listview structure
6876 * [I] nItem : item index
6877 * [IO] lprc : bounding rectangle coordinates
6878 * lprc->left specifies the portion of the item for which the bounding
6879 * rectangle will be retrieved.
6881 * LVIR_BOUNDS Returns the bounding rectangle of the entire item,
6882 * including the icon and label.
6884 * * For LVS_ICON
6885 * * Experiment shows that native control returns:
6886 * * width = min (48, length of text line)
6887 * * .left = position.x - (width - iconsize.cx)/2
6888 * * .right = .left + width
6889 * * height = #lines of text * ntmHeight + icon height + 8
6890 * * .top = position.y - 2
6891 * * .bottom = .top + height
6892 * * separation between items .y = itemSpacing.cy - height
6893 * * .x = itemSpacing.cx - width
6894 * LVIR_ICON Returns the bounding rectangle of the icon or small icon.
6896 * * For LVS_ICON
6897 * * Experiment shows that native control returns:
6898 * * width = iconSize.cx + 16
6899 * * .left = position.x - (width - iconsize.cx)/2
6900 * * .right = .left + width
6901 * * height = iconSize.cy + 4
6902 * * .top = position.y - 2
6903 * * .bottom = .top + height
6904 * * separation between items .y = itemSpacing.cy - height
6905 * * .x = itemSpacing.cx - width
6906 * LVIR_LABEL Returns the bounding rectangle of the item text.
6908 * * For LVS_ICON
6909 * * Experiment shows that native control returns:
6910 * * width = text length
6911 * * .left = position.x - width/2
6912 * * .right = .left + width
6913 * * height = ntmH * linecount + 2
6914 * * .top = position.y + iconSize.cy + 6
6915 * * .bottom = .top + height
6916 * * separation between items .y = itemSpacing.cy - height
6917 * * .x = itemSpacing.cx - width
6918 * LVIR_SELECTBOUNDS Returns the union of the LVIR_ICON and LVIR_LABEL
6919 * rectangles, but excludes columns in report view.
6921 * RETURN:
6922 * SUCCESS : TRUE
6923 * FAILURE : FALSE
6925 * NOTES
6926 * Note that the bounding rectangle of the label in the LVS_ICON view depends
6927 * upon whether the window has the focus currently and on whether the item
6928 * is the one with the focus. Ensure that the control's record of which
6929 * item has the focus agrees with the items' records.
6931 static BOOL LISTVIEW_GetItemRect(const LISTVIEW_INFO *infoPtr, INT nItem, LPRECT lprc)
6933 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
6934 BOOL doLabel = TRUE, oversizedBox = FALSE;
6935 POINT Position, Origin;
6936 LVITEMW lvItem;
6937 LONG mode;
6939 TRACE("(hwnd=%p, nItem=%d, lprc=%p)\n", infoPtr->hwndSelf, nItem, lprc);
6941 if (!lprc || nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
6943 LISTVIEW_GetOrigin(infoPtr, &Origin);
6944 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
6946 /* Be smart and try to figure out the minimum we have to do */
6947 if (lprc->left == LVIR_ICON) doLabel = FALSE;
6948 if (infoPtr->uView == LV_VIEW_DETAILS && lprc->left == LVIR_BOUNDS) doLabel = FALSE;
6949 if (infoPtr->uView == LV_VIEW_ICON && lprc->left != LVIR_ICON &&
6950 infoPtr->bFocus && LISTVIEW_GetItemState(infoPtr, nItem, LVIS_FOCUSED))
6951 oversizedBox = TRUE;
6953 /* get what we need from the item before hand, so we make
6954 * only one request. This can speed up things, if data
6955 * is stored on the app side */
6956 lvItem.mask = 0;
6957 if (infoPtr->uView == LV_VIEW_DETAILS) lvItem.mask |= LVIF_INDENT;
6958 if (doLabel) lvItem.mask |= LVIF_TEXT;
6959 lvItem.iItem = nItem;
6960 lvItem.iSubItem = 0;
6961 lvItem.pszText = szDispText;
6962 lvItem.cchTextMax = DISP_TEXT_SIZE;
6963 if (lvItem.mask && !LISTVIEW_GetItemW(infoPtr, &lvItem)) return FALSE;
6964 /* we got the state already up, simulate it here, to avoid a reget */
6965 if (infoPtr->uView == LV_VIEW_ICON && (lprc->left != LVIR_ICON))
6967 lvItem.mask |= LVIF_STATE;
6968 lvItem.stateMask = LVIS_FOCUSED;
6969 lvItem.state = (oversizedBox ? LVIS_FOCUSED : 0);
6972 if (infoPtr->uView == LV_VIEW_DETAILS && (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) && lprc->left == LVIR_SELECTBOUNDS)
6973 lprc->left = LVIR_BOUNDS;
6975 mode = lprc->left;
6976 switch(lprc->left)
6978 case LVIR_ICON:
6979 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, NULL, lprc, NULL, NULL);
6980 break;
6982 case LVIR_LABEL:
6983 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, NULL, NULL, NULL, lprc);
6984 break;
6986 case LVIR_BOUNDS:
6987 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, lprc, NULL, NULL, NULL, NULL);
6988 break;
6990 case LVIR_SELECTBOUNDS:
6991 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, lprc, NULL, NULL, NULL);
6992 break;
6994 default:
6995 WARN("Unknown value: %d\n", lprc->left);
6996 return FALSE;
6999 if (infoPtr->uView == LV_VIEW_DETAILS)
7001 if (mode != LVIR_BOUNDS)
7002 OffsetRect(lprc, Origin.x + LISTVIEW_GetColumnInfo(infoPtr, 0)->rcHeader.left,
7003 Position.y + Origin.y);
7004 else
7005 OffsetRect(lprc, Origin.x, Position.y + Origin.y);
7007 else
7008 OffsetRect(lprc, Position.x + Origin.x, Position.y + Origin.y);
7010 TRACE(" rect=%s\n", wine_dbgstr_rect(lprc));
7012 return TRUE;
7015 /***
7016 * DESCRIPTION:
7017 * Retrieves the spacing between listview control items.
7019 * PARAMETER(S):
7020 * [I] infoPtr : valid pointer to the listview structure
7021 * [IO] lprc : rectangle to receive the output
7022 * on input, lprc->top = nSubItem
7023 * lprc->left = LVIR_ICON | LVIR_BOUNDS | LVIR_LABEL
7025 * NOTE: for subItem = 0, we should return the bounds of the _entire_ item,
7026 * not only those of the first column.
7027 * Fortunately, LISTVIEW_GetItemMetrics does the right thing.
7029 * RETURN:
7030 * TRUE: success
7031 * FALSE: failure
7033 static BOOL LISTVIEW_GetSubItemRect(const LISTVIEW_INFO *infoPtr, INT nItem, LPRECT lprc)
7035 POINT Position, Origin;
7036 LVITEMW lvItem;
7037 INT nColumn;
7039 if (!lprc) return FALSE;
7041 nColumn = lprc->top;
7043 TRACE("(nItem=%d, nSubItem=%d, type=%d)\n", nItem, lprc->top, lprc->left);
7044 /* On WinNT, a subitem of '0' calls LISTVIEW_GetItemRect */
7045 if (lprc->top == 0)
7046 return LISTVIEW_GetItemRect(infoPtr, nItem, lprc);
7048 if (infoPtr->uView != LV_VIEW_DETAILS) return FALSE;
7050 /* special case for header items */
7051 if (nItem == -1)
7053 if (lprc->left != LVIR_BOUNDS)
7055 FIXME("Only LVIR_BOUNDS is implemented for header, got %d\n", lprc->left);
7056 return FALSE;
7059 if (infoPtr->hwndHeader)
7060 return SendMessageW(infoPtr->hwndHeader, HDM_GETITEMRECT, lprc->top, (LPARAM)lprc);
7061 else
7063 memset(lprc, 0, sizeof(RECT));
7064 return TRUE;
7068 if (!LISTVIEW_GetItemPosition(infoPtr, nItem, &Position)) return FALSE;
7069 LISTVIEW_GetOrigin(infoPtr, &Origin);
7071 if (nColumn < 0 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
7073 lvItem.mask = 0;
7074 lvItem.iItem = nItem;
7075 lvItem.iSubItem = nColumn;
7077 switch(lprc->left)
7079 case LVIR_ICON:
7080 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, NULL, lprc, NULL, NULL);
7081 break;
7083 case LVIR_LABEL:
7084 case LVIR_BOUNDS:
7085 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, lprc, NULL, NULL, NULL, NULL);
7086 break;
7088 default:
7089 ERR("Unknown bounds=%d\n", lprc->left);
7090 return FALSE;
7093 OffsetRect(lprc, Origin.x, Position.y);
7094 TRACE("return rect %s\n", wine_dbgstr_rect(lprc));
7096 return TRUE;
7099 /***
7100 * DESCRIPTION:
7101 * Retrieves the spacing between listview control items.
7103 * PARAMETER(S):
7104 * [I] infoPtr : valid pointer to the listview structure
7105 * [I] bSmall : flag for small or large icon
7107 * RETURN:
7108 * Horizontal + vertical spacing
7110 static LONG LISTVIEW_GetItemSpacing(const LISTVIEW_INFO *infoPtr, BOOL bSmall)
7112 LONG lResult;
7114 if (!bSmall)
7116 lResult = MAKELONG(infoPtr->iconSpacing.cx, infoPtr->iconSpacing.cy);
7118 else
7120 if (infoPtr->uView == LV_VIEW_ICON)
7121 lResult = MAKELONG(DEFAULT_COLUMN_WIDTH, GetSystemMetrics(SM_CXSMICON)+HEIGHT_PADDING);
7122 else
7123 lResult = MAKELONG(infoPtr->nItemWidth, infoPtr->nItemHeight);
7125 return lResult;
7128 /***
7129 * DESCRIPTION:
7130 * Retrieves the state of a listview control item.
7132 * PARAMETER(S):
7133 * [I] infoPtr : valid pointer to the listview structure
7134 * [I] nItem : item index
7135 * [I] uMask : state mask
7137 * RETURN:
7138 * State specified by the mask.
7140 static UINT LISTVIEW_GetItemState(const LISTVIEW_INFO *infoPtr, INT nItem, UINT uMask)
7142 LVITEMW lvItem;
7144 if (nItem < 0 || nItem >= infoPtr->nItemCount) return 0;
7146 lvItem.iItem = nItem;
7147 lvItem.iSubItem = 0;
7148 lvItem.mask = LVIF_STATE;
7149 lvItem.stateMask = uMask;
7150 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return 0;
7152 return lvItem.state & uMask;
7155 /***
7156 * DESCRIPTION:
7157 * Retrieves the text of a listview control item or subitem.
7159 * PARAMETER(S):
7160 * [I] hwnd : window handle
7161 * [I] nItem : item index
7162 * [IO] lpLVItem : item information
7163 * [I] isW : TRUE if lpLVItem is Unicode
7165 * RETURN:
7166 * SUCCESS : string length
7167 * FAILURE : 0
7169 static INT LISTVIEW_GetItemTextT(const LISTVIEW_INFO *infoPtr, INT nItem, LPLVITEMW lpLVItem, BOOL isW)
7171 if (!lpLVItem || nItem < 0 || nItem >= infoPtr->nItemCount) return 0;
7173 lpLVItem->mask = LVIF_TEXT;
7174 lpLVItem->iItem = nItem;
7175 if (!LISTVIEW_GetItemExtT(infoPtr, lpLVItem, isW)) return 0;
7177 return textlenT(lpLVItem->pszText, isW);
7180 /***
7181 * DESCRIPTION:
7182 * Searches for an item based on properties + relationships.
7184 * PARAMETER(S):
7185 * [I] infoPtr : valid pointer to the listview structure
7186 * [I] nItem : item index
7187 * [I] uFlags : relationship flag
7189 * RETURN:
7190 * SUCCESS : item index
7191 * FAILURE : -1
7193 static INT LISTVIEW_GetNextItem(const LISTVIEW_INFO *infoPtr, INT nItem, UINT uFlags)
7195 UINT uMask = 0;
7196 LVFINDINFOW lvFindInfo;
7197 INT nCountPerColumn;
7198 INT nCountPerRow;
7199 INT i;
7201 TRACE("nItem=%d, uFlags=%x, nItemCount=%d\n", nItem, uFlags, infoPtr->nItemCount);
7202 if (nItem < -1 || nItem >= infoPtr->nItemCount) return -1;
7204 ZeroMemory(&lvFindInfo, sizeof(lvFindInfo));
7206 if (uFlags & LVNI_CUT)
7207 uMask |= LVIS_CUT;
7209 if (uFlags & LVNI_DROPHILITED)
7210 uMask |= LVIS_DROPHILITED;
7212 if (uFlags & LVNI_FOCUSED)
7213 uMask |= LVIS_FOCUSED;
7215 if (uFlags & LVNI_SELECTED)
7216 uMask |= LVIS_SELECTED;
7218 /* if we're asked for the focused item, that's only one,
7219 * so it's worth optimizing */
7220 if (uFlags & LVNI_FOCUSED)
7222 if ((LISTVIEW_GetItemState(infoPtr, infoPtr->nFocusedItem, uMask) & uMask) != uMask) return -1;
7223 return (infoPtr->nFocusedItem == nItem) ? -1 : infoPtr->nFocusedItem;
7226 if (uFlags & LVNI_ABOVE)
7228 if ((infoPtr->uView == LV_VIEW_LIST) || (infoPtr->uView == LV_VIEW_DETAILS))
7230 while (nItem >= 0)
7232 nItem--;
7233 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7234 return nItem;
7237 else
7239 /* Special case for autoarrange - move 'til the top of a list */
7240 if (is_autoarrange(infoPtr))
7242 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
7243 while (nItem - nCountPerRow >= 0)
7245 nItem -= nCountPerRow;
7246 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7247 return nItem;
7249 return -1;
7251 lvFindInfo.flags = LVFI_NEARESTXY;
7252 lvFindInfo.vkDirection = VK_UP;
7253 LISTVIEW_GetItemPosition(infoPtr, nItem, &lvFindInfo.pt);
7254 while ((nItem = LISTVIEW_FindItemW(infoPtr, nItem, &lvFindInfo)) != -1)
7256 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7257 return nItem;
7261 else if (uFlags & LVNI_BELOW)
7263 if ((infoPtr->uView == LV_VIEW_LIST) || (infoPtr->uView == LV_VIEW_DETAILS))
7265 while (nItem < infoPtr->nItemCount)
7267 nItem++;
7268 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7269 return nItem;
7272 else
7274 /* Special case for autoarrange - move 'til the bottom of a list */
7275 if (is_autoarrange(infoPtr))
7277 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
7278 while (nItem + nCountPerRow < infoPtr->nItemCount )
7280 nItem += nCountPerRow;
7281 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7282 return nItem;
7284 return -1;
7286 lvFindInfo.flags = LVFI_NEARESTXY;
7287 lvFindInfo.vkDirection = VK_DOWN;
7288 LISTVIEW_GetItemPosition(infoPtr, nItem, &lvFindInfo.pt);
7289 while ((nItem = LISTVIEW_FindItemW(infoPtr, nItem, &lvFindInfo)) != -1)
7291 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7292 return nItem;
7296 else if (uFlags & LVNI_TOLEFT)
7298 if (infoPtr->uView == LV_VIEW_LIST)
7300 nCountPerColumn = LISTVIEW_GetCountPerColumn(infoPtr);
7301 while (nItem - nCountPerColumn >= 0)
7303 nItem -= nCountPerColumn;
7304 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7305 return nItem;
7308 else if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
7310 /* Special case for autoarrange - move 'til the beginning of a row */
7311 if (is_autoarrange(infoPtr))
7313 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
7314 while (nItem % nCountPerRow > 0)
7316 nItem --;
7317 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7318 return nItem;
7320 return -1;
7322 lvFindInfo.flags = LVFI_NEARESTXY;
7323 lvFindInfo.vkDirection = VK_LEFT;
7324 LISTVIEW_GetItemPosition(infoPtr, nItem, &lvFindInfo.pt);
7325 while ((nItem = LISTVIEW_FindItemW(infoPtr, nItem, &lvFindInfo)) != -1)
7327 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7328 return nItem;
7332 else if (uFlags & LVNI_TORIGHT)
7334 if (infoPtr->uView == LV_VIEW_LIST)
7336 nCountPerColumn = LISTVIEW_GetCountPerColumn(infoPtr);
7337 while (nItem + nCountPerColumn < infoPtr->nItemCount)
7339 nItem += nCountPerColumn;
7340 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7341 return nItem;
7344 else if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
7346 /* Special case for autoarrange - move 'til the end of a row */
7347 if (is_autoarrange(infoPtr))
7349 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
7350 while (nItem % nCountPerRow < nCountPerRow - 1 )
7352 nItem ++;
7353 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7354 return nItem;
7356 return -1;
7358 lvFindInfo.flags = LVFI_NEARESTXY;
7359 lvFindInfo.vkDirection = VK_RIGHT;
7360 LISTVIEW_GetItemPosition(infoPtr, nItem, &lvFindInfo.pt);
7361 while ((nItem = LISTVIEW_FindItemW(infoPtr, nItem, &lvFindInfo)) != -1)
7363 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7364 return nItem;
7368 else
7370 nItem++;
7372 /* search by index */
7373 for (i = nItem; i < infoPtr->nItemCount; i++)
7375 if ((LISTVIEW_GetItemState(infoPtr, i, uMask) & uMask) == uMask)
7376 return i;
7380 return -1;
7383 /* LISTVIEW_GetNumberOfWorkAreas */
7385 /***
7386 * DESCRIPTION:
7387 * Retrieves the origin coordinates when in icon or small icon display mode.
7389 * PARAMETER(S):
7390 * [I] infoPtr : valid pointer to the listview structure
7391 * [O] lpptOrigin : coordinate information
7393 * RETURN:
7394 * None.
7396 static void LISTVIEW_GetOrigin(const LISTVIEW_INFO *infoPtr, LPPOINT lpptOrigin)
7398 INT nHorzPos = 0, nVertPos = 0;
7399 SCROLLINFO scrollInfo;
7401 scrollInfo.cbSize = sizeof(SCROLLINFO);
7402 scrollInfo.fMask = SIF_POS;
7404 if (GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo))
7405 nHorzPos = scrollInfo.nPos;
7406 if (GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
7407 nVertPos = scrollInfo.nPos;
7409 TRACE("nHorzPos=%d, nVertPos=%d\n", nHorzPos, nVertPos);
7411 lpptOrigin->x = infoPtr->rcList.left;
7412 lpptOrigin->y = infoPtr->rcList.top;
7413 if (infoPtr->uView == LV_VIEW_LIST)
7414 nHorzPos *= infoPtr->nItemWidth;
7415 else if (infoPtr->uView == LV_VIEW_DETAILS)
7416 nVertPos *= infoPtr->nItemHeight;
7418 lpptOrigin->x -= nHorzPos;
7419 lpptOrigin->y -= nVertPos;
7421 TRACE(" origin=%s\n", wine_dbgstr_point(lpptOrigin));
7424 /***
7425 * DESCRIPTION:
7426 * Retrieves the width of a string.
7428 * PARAMETER(S):
7429 * [I] hwnd : window handle
7430 * [I] lpszText : text string to process
7431 * [I] isW : TRUE if lpszText is Unicode, FALSE otherwise
7433 * RETURN:
7434 * SUCCESS : string width (in pixels)
7435 * FAILURE : zero
7437 static INT LISTVIEW_GetStringWidthT(const LISTVIEW_INFO *infoPtr, LPCWSTR lpszText, BOOL isW)
7439 SIZE stringSize;
7441 stringSize.cx = 0;
7442 if (is_text(lpszText))
7444 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
7445 HDC hdc = GetDC(infoPtr->hwndSelf);
7446 HFONT hOldFont = SelectObject(hdc, hFont);
7448 if (isW)
7449 GetTextExtentPointW(hdc, lpszText, lstrlenW(lpszText), &stringSize);
7450 else
7451 GetTextExtentPointA(hdc, (LPCSTR)lpszText, lstrlenA((LPCSTR)lpszText), &stringSize);
7452 SelectObject(hdc, hOldFont);
7453 ReleaseDC(infoPtr->hwndSelf, hdc);
7455 return stringSize.cx;
7458 /***
7459 * DESCRIPTION:
7460 * Determines which listview item is located at the specified position.
7462 * PARAMETER(S):
7463 * [I] infoPtr : valid pointer to the listview structure
7464 * [IO] lpht : hit test information
7465 * [I] subitem : fill out iSubItem.
7466 * [I] select : return the index only if the hit selects the item
7468 * NOTE:
7469 * (mm 20001022): We must not allow iSubItem to be touched, for
7470 * an app might pass only a structure with space up to iItem!
7471 * (MS Office 97 does that for instance in the file open dialog)
7473 * RETURN:
7474 * SUCCESS : item index
7475 * FAILURE : -1
7477 static INT LISTVIEW_HitTest(const LISTVIEW_INFO *infoPtr, LPLVHITTESTINFO lpht, BOOL subitem, BOOL select)
7479 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
7480 RECT rcBox, rcBounds, rcState, rcIcon, rcLabel, rcSearch;
7481 POINT Origin, Position, opt;
7482 LVITEMW lvItem;
7483 ITERATOR i;
7484 INT iItem;
7486 TRACE("(pt=%s, subitem=%d, select=%d)\n", wine_dbgstr_point(&lpht->pt), subitem, select);
7488 lpht->flags = 0;
7489 lpht->iItem = -1;
7490 if (subitem) lpht->iSubItem = 0;
7492 LISTVIEW_GetOrigin(infoPtr, &Origin);
7494 /* set whole list relation flags */
7495 if (subitem && infoPtr->uView == LV_VIEW_DETAILS)
7497 /* LVM_SUBITEMHITTEST checks left bound of possible client area */
7498 if (infoPtr->rcList.left > lpht->pt.x && Origin.x < lpht->pt.x)
7499 lpht->flags |= LVHT_TOLEFT;
7501 if (lpht->pt.y < infoPtr->rcList.top && lpht->pt.y >= 0)
7502 opt.y = lpht->pt.y + infoPtr->rcList.top;
7503 else
7504 opt.y = lpht->pt.y;
7506 if (infoPtr->rcList.bottom < opt.y)
7507 lpht->flags |= LVHT_BELOW;
7509 else
7511 if (infoPtr->rcList.left > lpht->pt.x)
7512 lpht->flags |= LVHT_TOLEFT;
7513 else if (infoPtr->rcList.right < lpht->pt.x)
7514 lpht->flags |= LVHT_TORIGHT;
7516 if (infoPtr->rcList.top > lpht->pt.y)
7517 lpht->flags |= LVHT_ABOVE;
7518 else if (infoPtr->rcList.bottom < lpht->pt.y)
7519 lpht->flags |= LVHT_BELOW;
7522 /* even if item is invalid try to find subitem */
7523 if (infoPtr->uView == LV_VIEW_DETAILS && subitem)
7525 RECT *pRect;
7526 INT j;
7528 opt.x = lpht->pt.x - Origin.x;
7530 lpht->iSubItem = -1;
7531 for (j = 0; j < DPA_GetPtrCount(infoPtr->hdpaColumns); j++)
7533 pRect = &LISTVIEW_GetColumnInfo(infoPtr, j)->rcHeader;
7535 if ((opt.x >= pRect->left) && (opt.x < pRect->right))
7537 lpht->iSubItem = j;
7538 break;
7541 TRACE("lpht->iSubItem=%d\n", lpht->iSubItem);
7543 /* if we're outside horizontal columns bounds there's nothing to test further */
7544 if (lpht->iSubItem == -1)
7546 lpht->iItem = -1;
7547 lpht->flags = LVHT_NOWHERE;
7548 return -1;
7552 TRACE("lpht->flags=0x%x\n", lpht->flags);
7553 if (lpht->flags) return -1;
7555 lpht->flags |= LVHT_NOWHERE;
7557 /* first deal with the large items */
7558 rcSearch.left = lpht->pt.x;
7559 rcSearch.top = lpht->pt.y;
7560 rcSearch.right = rcSearch.left + 1;
7561 rcSearch.bottom = rcSearch.top + 1;
7563 iterator_frameditems(&i, infoPtr, &rcSearch);
7564 iterator_next(&i); /* go to first item in the sequence */
7565 iItem = i.nItem;
7566 iterator_destroy(&i);
7568 TRACE("lpht->iItem=%d\n", iItem);
7569 if (iItem == -1) return -1;
7571 lvItem.mask = LVIF_STATE | LVIF_TEXT;
7572 if (infoPtr->uView == LV_VIEW_DETAILS) lvItem.mask |= LVIF_INDENT;
7573 lvItem.stateMask = LVIS_STATEIMAGEMASK;
7574 if (infoPtr->uView == LV_VIEW_ICON) lvItem.stateMask |= LVIS_FOCUSED;
7575 lvItem.iItem = iItem;
7576 lvItem.iSubItem = subitem ? lpht->iSubItem : 0;
7577 lvItem.pszText = szDispText;
7578 lvItem.cchTextMax = DISP_TEXT_SIZE;
7579 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return -1;
7580 if (!infoPtr->bFocus) lvItem.state &= ~LVIS_FOCUSED;
7582 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, &rcBox, NULL, &rcIcon, &rcState, &rcLabel);
7583 LISTVIEW_GetItemOrigin(infoPtr, iItem, &Position);
7584 opt.x = lpht->pt.x - Position.x - Origin.x;
7586 if (lpht->pt.y < infoPtr->rcList.top && lpht->pt.y >= 0)
7587 opt.y = lpht->pt.y - Position.y - Origin.y + infoPtr->rcList.top;
7588 else
7589 opt.y = lpht->pt.y - Position.y - Origin.y;
7591 if (infoPtr->uView == LV_VIEW_DETAILS)
7593 rcBounds = rcBox;
7594 if (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT)
7595 opt.x = lpht->pt.x - Origin.x;
7597 else
7599 UnionRect(&rcBounds, &rcIcon, &rcLabel);
7600 UnionRect(&rcBounds, &rcBounds, &rcState);
7602 TRACE("rcBounds=%s\n", wine_dbgstr_rect(&rcBounds));
7603 if (!PtInRect(&rcBounds, opt)) return -1;
7605 if (PtInRect(&rcIcon, opt))
7606 lpht->flags |= LVHT_ONITEMICON;
7607 else if (PtInRect(&rcLabel, opt))
7608 lpht->flags |= LVHT_ONITEMLABEL;
7609 else if (infoPtr->himlState && PtInRect(&rcState, opt))
7610 lpht->flags |= LVHT_ONITEMSTATEICON;
7611 /* special case for LVS_EX_FULLROWSELECT */
7612 if (infoPtr->uView == LV_VIEW_DETAILS && infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT &&
7613 !(lpht->flags & LVHT_ONITEM))
7615 lpht->flags = LVHT_ONITEM | LVHT_ABOVE;
7617 if (lpht->flags & LVHT_ONITEM)
7618 lpht->flags &= ~LVHT_NOWHERE;
7619 TRACE("lpht->flags=0x%x\n", lpht->flags);
7621 if (select && !(infoPtr->uView == LV_VIEW_DETAILS &&
7622 ((infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) ||
7623 (infoPtr->dwStyle & LVS_OWNERDRAWFIXED))))
7625 if (infoPtr->uView == LV_VIEW_DETAILS)
7627 /* get main item bounds */
7628 lvItem.iSubItem = 0;
7629 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, &rcBox, NULL, &rcIcon, &rcState, &rcLabel);
7630 UnionRect(&rcBounds, &rcIcon, &rcLabel);
7631 UnionRect(&rcBounds, &rcBounds, &rcState);
7633 if (!PtInRect(&rcBounds, opt)) iItem = -1;
7635 return lpht->iItem = iItem;
7638 /***
7639 * DESCRIPTION:
7640 * Inserts a new item in the listview control.
7642 * PARAMETER(S):
7643 * [I] infoPtr : valid pointer to the listview structure
7644 * [I] lpLVItem : item information
7645 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
7647 * RETURN:
7648 * SUCCESS : new item index
7649 * FAILURE : -1
7651 static INT LISTVIEW_InsertItemT(LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem, BOOL isW)
7653 INT nItem;
7654 HDPA hdpaSubItems;
7655 NMLISTVIEW nmlv;
7656 ITEM_INFO *lpItem;
7657 ITEM_ID *lpID;
7658 BOOL is_sorted, has_changed;
7659 LVITEMW item;
7660 HWND hwndSelf = infoPtr->hwndSelf;
7662 TRACE("(lpLVItem=%s, isW=%d)\n", debuglvitem_t(lpLVItem, isW), isW);
7664 if (infoPtr->dwStyle & LVS_OWNERDATA) return infoPtr->nItemCount++;
7666 /* make sure it's an item, and not a subitem; cannot insert a subitem */
7667 if (!lpLVItem || lpLVItem->iSubItem) return -1;
7669 if (!is_assignable_item(lpLVItem, infoPtr->dwStyle)) return -1;
7671 if (!(lpItem = Alloc(sizeof(ITEM_INFO)))) return -1;
7673 /* insert item in listview control data structure */
7674 if ( !(hdpaSubItems = DPA_Create(8)) ) goto fail;
7675 if ( !DPA_SetPtr(hdpaSubItems, 0, lpItem) ) assert (FALSE);
7677 /* link with id struct */
7678 if (!(lpID = Alloc(sizeof(ITEM_ID)))) goto fail;
7679 lpItem->id = lpID;
7680 lpID->item = hdpaSubItems;
7681 lpID->id = get_next_itemid(infoPtr);
7682 if ( DPA_InsertPtr(infoPtr->hdpaItemIds, infoPtr->nItemCount, lpID) == -1) goto fail;
7684 is_sorted = (infoPtr->dwStyle & (LVS_SORTASCENDING | LVS_SORTDESCENDING)) &&
7685 !(infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (LPSTR_TEXTCALLBACKW != lpLVItem->pszText);
7687 if (lpLVItem->iItem < 0 && !is_sorted) return -1;
7689 /* calculate new item index */
7690 if (is_sorted)
7692 HDPA hItem;
7693 ITEM_INFO *item_s;
7694 INT i = 0, cmpv;
7696 while (i < infoPtr->nItemCount)
7698 hItem = DPA_GetPtr( infoPtr->hdpaItems, i);
7699 item_s = DPA_GetPtr(hItem, 0);
7701 cmpv = textcmpWT(item_s->hdr.pszText, lpLVItem->pszText, isW);
7702 if (infoPtr->dwStyle & LVS_SORTDESCENDING) cmpv *= -1;
7704 if (cmpv >= 0) break;
7705 i++;
7707 nItem = i;
7709 else
7710 nItem = min(lpLVItem->iItem, infoPtr->nItemCount);
7712 TRACE(" inserting at %d, sorted=%d, count=%d, iItem=%d\n", nItem, is_sorted, infoPtr->nItemCount, lpLVItem->iItem);
7713 nItem = DPA_InsertPtr( infoPtr->hdpaItems, nItem, hdpaSubItems );
7714 if (nItem == -1) goto fail;
7715 infoPtr->nItemCount++;
7717 /* shift indices first so they don't get tangled */
7718 LISTVIEW_ShiftIndices(infoPtr, nItem, 1);
7720 /* set the item attributes */
7721 if (lpLVItem->mask & (LVIF_GROUPID|LVIF_COLUMNS))
7723 /* full size structure expected - _WIN32IE >= 0x560 */
7724 item = *lpLVItem;
7726 else if (lpLVItem->mask & LVIF_INDENT)
7728 /* indent member expected - _WIN32IE >= 0x300 */
7729 memcpy(&item, lpLVItem, offsetof( LVITEMW, iGroupId ));
7731 else
7733 /* minimal structure expected */
7734 memcpy(&item, lpLVItem, offsetof( LVITEMW, iIndent ));
7736 item.iItem = nItem;
7737 if (infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES)
7739 item.mask |= LVIF_STATE;
7740 item.stateMask |= LVIS_STATEIMAGEMASK;
7741 item.state &= ~LVIS_STATEIMAGEMASK;
7742 item.state |= INDEXTOSTATEIMAGEMASK(1);
7744 if (!set_main_item(infoPtr, &item, TRUE, isW, &has_changed)) goto undo;
7746 /* make room for the position, if we are in the right mode */
7747 if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
7749 if (DPA_InsertPtr(infoPtr->hdpaPosX, nItem, 0) == -1)
7750 goto undo;
7751 if (DPA_InsertPtr(infoPtr->hdpaPosY, nItem, 0) == -1)
7753 DPA_DeletePtr(infoPtr->hdpaPosX, nItem);
7754 goto undo;
7758 /* send LVN_INSERTITEM notification */
7759 ZeroMemory(&nmlv, sizeof(NMLISTVIEW));
7760 nmlv.iItem = nItem;
7761 nmlv.lParam = lpItem->lParam;
7762 notify_listview(infoPtr, LVN_INSERTITEM, &nmlv);
7763 if (!IsWindow(hwndSelf))
7764 return -1;
7766 /* align items (set position of each item) */
7767 if (infoPtr->uView == LV_VIEW_SMALLICON || infoPtr->uView == LV_VIEW_ICON)
7769 POINT pt;
7771 if (infoPtr->dwStyle & LVS_ALIGNLEFT)
7772 LISTVIEW_NextIconPosLeft(infoPtr, &pt);
7773 else
7774 LISTVIEW_NextIconPosTop(infoPtr, &pt);
7776 LISTVIEW_MoveIconTo(infoPtr, nItem, &pt, TRUE);
7779 /* now is the invalidation fun */
7780 LISTVIEW_ScrollOnInsert(infoPtr, nItem, 1);
7781 return nItem;
7783 undo:
7784 LISTVIEW_ShiftIndices(infoPtr, nItem, -1);
7785 DPA_DeletePtr(infoPtr->hdpaItems, nItem);
7786 infoPtr->nItemCount--;
7787 fail:
7788 DPA_DeletePtr(hdpaSubItems, 0);
7789 DPA_Destroy (hdpaSubItems);
7790 Free (lpItem);
7791 return -1;
7794 /***
7795 * DESCRIPTION:
7796 * Checks item visibility.
7798 * PARAMETER(S):
7799 * [I] infoPtr : valid pointer to the listview structure
7800 * [I] nFirst : item index to check for
7802 * RETURN:
7803 * Item visible : TRUE
7804 * Item invisible or failure : FALSE
7806 static BOOL LISTVIEW_IsItemVisible(const LISTVIEW_INFO *infoPtr, INT nItem)
7808 POINT Origin, Position;
7809 RECT rcItem;
7810 HDC hdc;
7811 BOOL ret;
7813 TRACE("nItem=%d\n", nItem);
7815 if (nItem < 0 || nItem >= DPA_GetPtrCount(infoPtr->hdpaItems)) return FALSE;
7817 LISTVIEW_GetOrigin(infoPtr, &Origin);
7818 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
7819 rcItem.left = Position.x + Origin.x;
7820 rcItem.top = Position.y + Origin.y;
7821 rcItem.right = rcItem.left + infoPtr->nItemWidth;
7822 rcItem.bottom = rcItem.top + infoPtr->nItemHeight;
7824 hdc = GetDC(infoPtr->hwndSelf);
7825 if (!hdc) return FALSE;
7826 ret = RectVisible(hdc, &rcItem);
7827 ReleaseDC(infoPtr->hwndSelf, hdc);
7829 return ret;
7832 /***
7833 * DESCRIPTION:
7834 * Redraws a range of items.
7836 * PARAMETER(S):
7837 * [I] infoPtr : valid pointer to the listview structure
7838 * [I] nFirst : first item
7839 * [I] nLast : last item
7841 * RETURN:
7842 * SUCCESS : TRUE
7843 * FAILURE : FALSE
7845 static BOOL LISTVIEW_RedrawItems(const LISTVIEW_INFO *infoPtr, INT nFirst, INT nLast)
7847 INT i;
7849 if (nLast < nFirst || min(nFirst, nLast) < 0 ||
7850 max(nFirst, nLast) >= infoPtr->nItemCount)
7851 return FALSE;
7853 for (i = nFirst; i <= nLast; i++)
7854 LISTVIEW_InvalidateItem(infoPtr, i);
7856 return TRUE;
7859 /***
7860 * DESCRIPTION:
7861 * Scroll the content of a listview.
7863 * PARAMETER(S):
7864 * [I] infoPtr : valid pointer to the listview structure
7865 * [I] dx : horizontal scroll amount in pixels
7866 * [I] dy : vertical scroll amount in pixels
7868 * RETURN:
7869 * SUCCESS : TRUE
7870 * FAILURE : FALSE
7872 * COMMENTS:
7873 * If the control is in report view (LV_VIEW_DETAILS) the control can
7874 * be scrolled only in line increments. "dy" will be rounded to the
7875 * nearest number of pixels that are a whole line. Ex: if line height
7876 * is 16 and an 8 is passed, the list will be scrolled by 16. If a 7
7877 * is passed, then the scroll will be 0. (per MSDN 7/2002)
7879 * For: (per experimentation with native control and CSpy ListView)
7880 * LV_VIEW_ICON scrolling in any direction is allowed
7881 * LV_VIEW_SMALLICON scrolling in any direction is allowed
7882 * LV_VIEW_LIST dx=1 = 1 column (horizontal only)
7883 * but will only scroll 1 column per message
7884 * no matter what the value.
7885 * dy must be 0 or FALSE returned.
7886 * LV_VIEW_DETAILS dx=1 = 1 pixel
7887 * dy= see above
7890 static BOOL LISTVIEW_Scroll(LISTVIEW_INFO *infoPtr, INT dx, INT dy)
7892 switch(infoPtr->uView) {
7893 case LV_VIEW_DETAILS:
7894 dy += (dy < 0 ? -1 : 1) * infoPtr->nItemHeight/2;
7895 dy /= infoPtr->nItemHeight;
7896 break;
7897 case LV_VIEW_LIST:
7898 if (dy != 0) return FALSE;
7899 break;
7900 default: /* icon */
7901 break;
7904 if (dx != 0) LISTVIEW_HScroll(infoPtr, SB_INTERNAL, dx);
7905 if (dy != 0) LISTVIEW_VScroll(infoPtr, SB_INTERNAL, dy);
7907 return TRUE;
7910 /***
7911 * DESCRIPTION:
7912 * Sets the background color.
7914 * PARAMETER(S):
7915 * [I] infoPtr : valid pointer to the listview structure
7916 * [I] clrBk : background color
7918 * RETURN:
7919 * SUCCESS : TRUE
7920 * FAILURE : FALSE
7922 static BOOL LISTVIEW_SetBkColor(LISTVIEW_INFO *infoPtr, COLORREF clrBk)
7924 TRACE("(clrBk=%x)\n", clrBk);
7926 if(infoPtr->clrBk != clrBk) {
7927 if (infoPtr->clrBk != CLR_NONE) DeleteObject(infoPtr->hBkBrush);
7928 infoPtr->clrBk = clrBk;
7929 if (clrBk == CLR_NONE)
7930 infoPtr->hBkBrush = (HBRUSH)GetClassLongPtrW(infoPtr->hwndSelf, GCLP_HBRBACKGROUND);
7931 else
7933 infoPtr->hBkBrush = CreateSolidBrush(clrBk);
7934 infoPtr->dwLvExStyle &= ~LVS_EX_TRANSPARENTBKGND;
7936 LISTVIEW_InvalidateList(infoPtr);
7939 return TRUE;
7942 /* LISTVIEW_SetBkImage */
7944 /*** Helper for {Insert,Set}ColumnT *only* */
7945 static void column_fill_hditem(const LISTVIEW_INFO *infoPtr, HDITEMW *lphdi, INT nColumn,
7946 const LVCOLUMNW *lpColumn, BOOL isW)
7948 if (lpColumn->mask & LVCF_FMT)
7950 /* format member is valid */
7951 lphdi->mask |= HDI_FORMAT;
7953 /* set text alignment (leftmost column must be left-aligned) */
7954 if (nColumn == 0 || (lpColumn->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_LEFT)
7955 lphdi->fmt |= HDF_LEFT;
7956 else if ((lpColumn->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_RIGHT)
7957 lphdi->fmt |= HDF_RIGHT;
7958 else if ((lpColumn->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_CENTER)
7959 lphdi->fmt |= HDF_CENTER;
7961 if (lpColumn->fmt & LVCFMT_BITMAP_ON_RIGHT)
7962 lphdi->fmt |= HDF_BITMAP_ON_RIGHT;
7964 if (lpColumn->fmt & LVCFMT_COL_HAS_IMAGES)
7966 lphdi->fmt |= HDF_IMAGE;
7967 lphdi->iImage = I_IMAGECALLBACK;
7970 if (lpColumn->fmt & LVCFMT_FIXED_WIDTH)
7971 lphdi->fmt |= HDF_FIXEDWIDTH;
7974 if (lpColumn->mask & LVCF_WIDTH)
7976 lphdi->mask |= HDI_WIDTH;
7977 if(lpColumn->cx == LVSCW_AUTOSIZE_USEHEADER)
7979 /* make it fill the remainder of the controls width */
7980 RECT rcHeader;
7981 INT item_index;
7983 for(item_index = 0; item_index < (nColumn - 1); item_index++)
7985 LISTVIEW_GetHeaderRect(infoPtr, item_index, &rcHeader);
7986 lphdi->cxy += rcHeader.right - rcHeader.left;
7989 /* retrieve the layout of the header */
7990 GetClientRect(infoPtr->hwndSelf, &rcHeader);
7991 TRACE("start cxy=%d rcHeader=%s\n", lphdi->cxy, wine_dbgstr_rect(&rcHeader));
7993 lphdi->cxy = (rcHeader.right - rcHeader.left) - lphdi->cxy;
7995 else
7996 lphdi->cxy = lpColumn->cx;
7999 if (lpColumn->mask & LVCF_TEXT)
8001 lphdi->mask |= HDI_TEXT | HDI_FORMAT;
8002 lphdi->fmt |= HDF_STRING;
8003 lphdi->pszText = lpColumn->pszText;
8004 lphdi->cchTextMax = textlenT(lpColumn->pszText, isW);
8007 if (lpColumn->mask & LVCF_IMAGE)
8009 lphdi->mask |= HDI_IMAGE;
8010 lphdi->iImage = lpColumn->iImage;
8013 if (lpColumn->mask & LVCF_ORDER)
8015 lphdi->mask |= HDI_ORDER;
8016 lphdi->iOrder = lpColumn->iOrder;
8021 /***
8022 * DESCRIPTION:
8023 * Inserts a new column.
8025 * PARAMETER(S):
8026 * [I] infoPtr : valid pointer to the listview structure
8027 * [I] nColumn : column index
8028 * [I] lpColumn : column information
8029 * [I] isW : TRUE if lpColumn is Unicode, FALSE otherwise
8031 * RETURN:
8032 * SUCCESS : new column index
8033 * FAILURE : -1
8035 static INT LISTVIEW_InsertColumnT(LISTVIEW_INFO *infoPtr, INT nColumn,
8036 const LVCOLUMNW *lpColumn, BOOL isW)
8038 COLUMN_INFO *lpColumnInfo;
8039 INT nNewColumn;
8040 HDITEMW hdi;
8042 TRACE("(nColumn=%d, lpColumn=%s, isW=%d)\n", nColumn, debuglvcolumn_t(lpColumn, isW), isW);
8044 if (!lpColumn || nColumn < 0) return -1;
8045 nColumn = min(nColumn, DPA_GetPtrCount(infoPtr->hdpaColumns));
8047 ZeroMemory(&hdi, sizeof(HDITEMW));
8048 column_fill_hditem(infoPtr, &hdi, nColumn, lpColumn, isW);
8051 * A mask not including LVCF_WIDTH turns into a mask of width, width 10
8052 * (can be seen in SPY) otherwise column never gets added.
8054 if (!(lpColumn->mask & LVCF_WIDTH)) {
8055 hdi.mask |= HDI_WIDTH;
8056 hdi.cxy = 10;
8060 * when the iSubItem is available Windows copies it to the header lParam. It seems
8061 * to happen only in LVM_INSERTCOLUMN - not in LVM_SETCOLUMN
8063 if (lpColumn->mask & LVCF_SUBITEM)
8065 hdi.mask |= HDI_LPARAM;
8066 hdi.lParam = lpColumn->iSubItem;
8069 /* create header if not present */
8070 LISTVIEW_CreateHeader(infoPtr);
8071 if (!(LVS_NOCOLUMNHEADER & infoPtr->dwStyle) &&
8072 (infoPtr->uView == LV_VIEW_DETAILS) && (WS_VISIBLE & infoPtr->dwStyle))
8074 ShowWindow(infoPtr->hwndHeader, SW_SHOWNORMAL);
8077 /* insert item in header control */
8078 nNewColumn = SendMessageW(infoPtr->hwndHeader,
8079 isW ? HDM_INSERTITEMW : HDM_INSERTITEMA,
8080 nColumn, (LPARAM)&hdi);
8081 if (nNewColumn == -1) return -1;
8082 if (nNewColumn != nColumn) ERR("nColumn=%d, nNewColumn=%d\n", nColumn, nNewColumn);
8084 /* create our own column info */
8085 if (!(lpColumnInfo = Alloc(sizeof(COLUMN_INFO)))) goto fail;
8086 if (DPA_InsertPtr(infoPtr->hdpaColumns, nNewColumn, lpColumnInfo) == -1) goto fail;
8088 if (lpColumn->mask & LVCF_FMT) lpColumnInfo->fmt = lpColumn->fmt;
8089 if (lpColumn->mask & LVCF_MINWIDTH) lpColumnInfo->cxMin = lpColumn->cxMin;
8090 if (!SendMessageW(infoPtr->hwndHeader, HDM_GETITEMRECT, nNewColumn, (LPARAM)&lpColumnInfo->rcHeader))
8091 goto fail;
8093 /* now we have to actually adjust the data */
8094 if (!(infoPtr->dwStyle & LVS_OWNERDATA) && infoPtr->nItemCount > 0)
8096 SUBITEM_INFO *lpSubItem;
8097 HDPA hdpaSubItems;
8098 INT nItem, i;
8099 LVITEMW item;
8100 BOOL changed;
8102 item.iSubItem = nNewColumn;
8103 item.mask = LVIF_TEXT | LVIF_IMAGE;
8104 item.iImage = I_IMAGECALLBACK;
8105 item.pszText = LPSTR_TEXTCALLBACKW;
8107 for (nItem = 0; nItem < infoPtr->nItemCount; nItem++)
8109 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, nItem);
8110 for (i = 1; i < DPA_GetPtrCount(hdpaSubItems); i++)
8112 lpSubItem = DPA_GetPtr(hdpaSubItems, i);
8113 if (lpSubItem->iSubItem >= nNewColumn)
8114 lpSubItem->iSubItem++;
8117 /* add new subitem for each item */
8118 item.iItem = nItem;
8119 set_sub_item(infoPtr, &item, isW, &changed);
8123 /* make space for the new column */
8124 LISTVIEW_ScrollColumns(infoPtr, nNewColumn + 1, lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left);
8125 LISTVIEW_UpdateItemSize(infoPtr);
8127 return nNewColumn;
8129 fail:
8130 if (nNewColumn != -1) SendMessageW(infoPtr->hwndHeader, HDM_DELETEITEM, nNewColumn, 0);
8131 if (lpColumnInfo)
8133 DPA_DeletePtr(infoPtr->hdpaColumns, nNewColumn);
8134 Free(lpColumnInfo);
8136 return -1;
8139 /***
8140 * DESCRIPTION:
8141 * Sets the attributes of a header item.
8143 * PARAMETER(S):
8144 * [I] infoPtr : valid pointer to the listview structure
8145 * [I] nColumn : column index
8146 * [I] lpColumn : column attributes
8147 * [I] isW: if TRUE, then lpColumn is a LPLVCOLUMNW, else it is a LPLVCOLUMNA
8149 * RETURN:
8150 * SUCCESS : TRUE
8151 * FAILURE : FALSE
8153 static BOOL LISTVIEW_SetColumnT(const LISTVIEW_INFO *infoPtr, INT nColumn,
8154 const LVCOLUMNW *lpColumn, BOOL isW)
8156 HDITEMW hdi, hdiget;
8157 BOOL bResult;
8159 TRACE("(nColumn=%d, lpColumn=%s, isW=%d)\n", nColumn, debuglvcolumn_t(lpColumn, isW), isW);
8161 if (!lpColumn || nColumn < 0 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
8163 ZeroMemory(&hdi, sizeof(HDITEMW));
8164 if (lpColumn->mask & LVCF_FMT)
8166 hdi.mask |= HDI_FORMAT;
8167 hdiget.mask = HDI_FORMAT;
8168 if (SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nColumn, (LPARAM)&hdiget))
8169 hdi.fmt = hdiget.fmt & HDF_STRING;
8171 column_fill_hditem(infoPtr, &hdi, nColumn, lpColumn, isW);
8173 /* set header item attributes */
8174 bResult = SendMessageW(infoPtr->hwndHeader, isW ? HDM_SETITEMW : HDM_SETITEMA, nColumn, (LPARAM)&hdi);
8175 if (!bResult) return FALSE;
8177 if (lpColumn->mask & LVCF_FMT)
8179 COLUMN_INFO *lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, nColumn);
8180 INT oldFmt = lpColumnInfo->fmt;
8182 lpColumnInfo->fmt = lpColumn->fmt;
8183 if ((oldFmt ^ lpColumn->fmt) & (LVCFMT_JUSTIFYMASK | LVCFMT_IMAGE))
8185 if (infoPtr->uView == LV_VIEW_DETAILS) LISTVIEW_InvalidateColumn(infoPtr, nColumn);
8189 if (lpColumn->mask & LVCF_MINWIDTH)
8190 LISTVIEW_GetColumnInfo(infoPtr, nColumn)->cxMin = lpColumn->cxMin;
8192 return TRUE;
8195 /***
8196 * DESCRIPTION:
8197 * Sets the column order array
8199 * PARAMETERS:
8200 * [I] infoPtr : valid pointer to the listview structure
8201 * [I] iCount : number of elements in column order array
8202 * [I] lpiArray : pointer to column order array
8204 * RETURN:
8205 * SUCCESS : TRUE
8206 * FAILURE : FALSE
8208 static BOOL LISTVIEW_SetColumnOrderArray(LISTVIEW_INFO *infoPtr, INT iCount, const INT *lpiArray)
8210 TRACE("iCount %d lpiArray %p\n", iCount, lpiArray);
8212 if (!lpiArray || !IsWindow(infoPtr->hwndHeader)) return FALSE;
8214 infoPtr->colRectsDirty = TRUE;
8216 return SendMessageW(infoPtr->hwndHeader, HDM_SETORDERARRAY, iCount, (LPARAM)lpiArray);
8219 /***
8220 * DESCRIPTION:
8221 * Sets the width of a column
8223 * PARAMETERS:
8224 * [I] infoPtr : valid pointer to the listview structure
8225 * [I] nColumn : column index
8226 * [I] cx : column width
8228 * RETURN:
8229 * SUCCESS : TRUE
8230 * FAILURE : FALSE
8232 static BOOL LISTVIEW_SetColumnWidth(LISTVIEW_INFO *infoPtr, INT nColumn, INT cx)
8234 WCHAR szDispText[DISP_TEXT_SIZE] = { 0 };
8235 INT max_cx = 0;
8236 HDITEMW hdi;
8238 TRACE("(nColumn=%d, cx=%d\n", nColumn, cx);
8240 /* set column width only if in report or list mode */
8241 if (infoPtr->uView != LV_VIEW_DETAILS && infoPtr->uView != LV_VIEW_LIST) return FALSE;
8243 /* take care of invalid cx values */
8244 if(infoPtr->uView == LV_VIEW_DETAILS && cx < -2) cx = LVSCW_AUTOSIZE;
8245 else if (infoPtr->uView == LV_VIEW_LIST && cx < 1) return FALSE;
8247 /* resize all columns if in LV_VIEW_LIST mode */
8248 if(infoPtr->uView == LV_VIEW_LIST)
8250 infoPtr->nItemWidth = cx;
8251 LISTVIEW_InvalidateList(infoPtr);
8252 return TRUE;
8255 if (nColumn < 0 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
8257 if (cx == LVSCW_AUTOSIZE || (cx == LVSCW_AUTOSIZE_USEHEADER && nColumn < DPA_GetPtrCount(infoPtr->hdpaColumns) -1))
8259 INT nLabelWidth;
8260 LVITEMW lvItem;
8262 lvItem.mask = LVIF_TEXT;
8263 lvItem.iItem = 0;
8264 lvItem.iSubItem = nColumn;
8265 lvItem.pszText = szDispText;
8266 lvItem.cchTextMax = DISP_TEXT_SIZE;
8267 for (; lvItem.iItem < infoPtr->nItemCount; lvItem.iItem++)
8269 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) continue;
8270 nLabelWidth = LISTVIEW_GetStringWidthT(infoPtr, lvItem.pszText, TRUE);
8271 if (max_cx < nLabelWidth) max_cx = nLabelWidth;
8273 if (infoPtr->himlSmall && (nColumn == 0 || (LISTVIEW_GetColumnInfo(infoPtr, nColumn)->fmt & LVCFMT_IMAGE)))
8274 max_cx += infoPtr->iconSize.cx;
8275 max_cx += TRAILING_LABEL_PADDING;
8278 /* autosize based on listview items width */
8279 if(cx == LVSCW_AUTOSIZE)
8280 cx = max_cx;
8281 else if(cx == LVSCW_AUTOSIZE_USEHEADER)
8283 /* if iCol is the last column make it fill the remainder of the controls width */
8284 if(nColumn == DPA_GetPtrCount(infoPtr->hdpaColumns) - 1)
8286 RECT rcHeader;
8287 POINT Origin;
8289 LISTVIEW_GetOrigin(infoPtr, &Origin);
8290 LISTVIEW_GetHeaderRect(infoPtr, nColumn, &rcHeader);
8292 cx = infoPtr->rcList.right - Origin.x - rcHeader.left;
8294 else
8296 /* Despite what the MS docs say, if this is not the last
8297 column, then MS resizes the column to the width of the
8298 largest text string in the column, including headers
8299 and items. This is different from LVSCW_AUTOSIZE in that
8300 LVSCW_AUTOSIZE ignores the header string length. */
8301 cx = 0;
8303 /* retrieve header text */
8304 hdi.mask = HDI_TEXT;
8305 hdi.cchTextMax = DISP_TEXT_SIZE;
8306 hdi.pszText = szDispText;
8307 if (SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nColumn, (LPARAM)&hdi))
8309 HDC hdc = GetDC(infoPtr->hwndSelf);
8310 HFONT old_font = SelectObject(hdc, (HFONT)SendMessageW(infoPtr->hwndHeader, WM_GETFONT, 0, 0));
8311 SIZE size;
8313 if (GetTextExtentPoint32W(hdc, hdi.pszText, lstrlenW(hdi.pszText), &size))
8314 cx = size.cx + TRAILING_HEADER_PADDING;
8315 /* FIXME: Take into account the header image, if one is present */
8316 SelectObject(hdc, old_font);
8317 ReleaseDC(infoPtr->hwndSelf, hdc);
8319 cx = max (cx, max_cx);
8323 if (cx < 0) return FALSE;
8325 /* call header to update the column change */
8326 hdi.mask = HDI_WIDTH;
8327 hdi.cxy = max(cx, LISTVIEW_GetColumnInfo(infoPtr, nColumn)->cxMin);
8328 TRACE("hdi.cxy=%d\n", hdi.cxy);
8329 return SendMessageW(infoPtr->hwndHeader, HDM_SETITEMW, nColumn, (LPARAM)&hdi);
8332 /***
8333 * Creates the checkbox imagelist. Helper for LISTVIEW_SetExtendedListViewStyle
8336 static HIMAGELIST LISTVIEW_CreateCheckBoxIL(const LISTVIEW_INFO *infoPtr)
8338 HDC hdc_wnd, hdc;
8339 HBITMAP hbm_im, hbm_mask, hbm_orig;
8340 RECT rc;
8341 HBRUSH hbr_white = GetStockObject(WHITE_BRUSH);
8342 HBRUSH hbr_black = GetStockObject(BLACK_BRUSH);
8343 HIMAGELIST himl;
8345 himl = ImageList_Create(GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON),
8346 ILC_COLOR | ILC_MASK, 2, 2);
8347 hdc_wnd = GetDC(infoPtr->hwndSelf);
8348 hdc = CreateCompatibleDC(hdc_wnd);
8349 hbm_im = CreateCompatibleBitmap(hdc_wnd, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON));
8350 hbm_mask = CreateBitmap(GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), 1, 1, NULL);
8351 ReleaseDC(infoPtr->hwndSelf, hdc_wnd);
8353 rc.left = rc.top = 0;
8354 rc.right = GetSystemMetrics(SM_CXSMICON);
8355 rc.bottom = GetSystemMetrics(SM_CYSMICON);
8357 hbm_orig = SelectObject(hdc, hbm_mask);
8358 FillRect(hdc, &rc, hbr_white);
8359 InflateRect(&rc, -2, -2);
8360 FillRect(hdc, &rc, hbr_black);
8362 SelectObject(hdc, hbm_im);
8363 DrawFrameControl(hdc, &rc, DFC_BUTTON, DFCS_BUTTONCHECK | DFCS_MONO);
8364 SelectObject(hdc, hbm_orig);
8365 ImageList_Add(himl, hbm_im, hbm_mask);
8367 SelectObject(hdc, hbm_im);
8368 DrawFrameControl(hdc, &rc, DFC_BUTTON, DFCS_BUTTONCHECK | DFCS_MONO | DFCS_CHECKED);
8369 SelectObject(hdc, hbm_orig);
8370 ImageList_Add(himl, hbm_im, hbm_mask);
8372 DeleteObject(hbm_mask);
8373 DeleteObject(hbm_im);
8374 DeleteDC(hdc);
8376 return himl;
8379 /***
8380 * DESCRIPTION:
8381 * Sets the extended listview style.
8383 * PARAMETERS:
8384 * [I] infoPtr : valid pointer to the listview structure
8385 * [I] dwMask : mask
8386 * [I] dwStyle : style
8388 * RETURN:
8389 * SUCCESS : previous style
8390 * FAILURE : 0
8392 static DWORD LISTVIEW_SetExtendedListViewStyle(LISTVIEW_INFO *infoPtr, DWORD dwMask, DWORD dwExStyle)
8394 DWORD dwOldExStyle = infoPtr->dwLvExStyle;
8396 /* set new style */
8397 if (dwMask)
8398 infoPtr->dwLvExStyle = (dwOldExStyle & ~dwMask) | (dwExStyle & dwMask);
8399 else
8400 infoPtr->dwLvExStyle = dwExStyle;
8402 if((infoPtr->dwLvExStyle ^ dwOldExStyle) & LVS_EX_CHECKBOXES)
8404 HIMAGELIST himl = 0;
8405 if(infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES)
8407 LVITEMW item;
8408 item.mask = LVIF_STATE;
8409 item.stateMask = LVIS_STATEIMAGEMASK;
8410 item.state = INDEXTOSTATEIMAGEMASK(1);
8411 LISTVIEW_SetItemState(infoPtr, -1, &item);
8413 himl = LISTVIEW_CreateCheckBoxIL(infoPtr);
8414 if(!(infoPtr->dwStyle & LVS_SHAREIMAGELISTS))
8415 ImageList_Destroy(infoPtr->himlState);
8417 himl = LISTVIEW_SetImageList(infoPtr, LVSIL_STATE, himl);
8418 /* checkbox list replaces prevous custom list or... */
8419 if(((infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES) &&
8420 !(infoPtr->dwStyle & LVS_SHAREIMAGELISTS)) ||
8421 /* ...previous was checkbox list */
8422 (dwOldExStyle & LVS_EX_CHECKBOXES))
8423 ImageList_Destroy(himl);
8426 if((infoPtr->dwLvExStyle ^ dwOldExStyle) & LVS_EX_HEADERDRAGDROP)
8428 DWORD dwStyle;
8430 /* if not already created */
8431 LISTVIEW_CreateHeader(infoPtr);
8433 dwStyle = GetWindowLongW(infoPtr->hwndHeader, GWL_STYLE);
8434 if (infoPtr->dwLvExStyle & LVS_EX_HEADERDRAGDROP)
8435 dwStyle |= HDS_DRAGDROP;
8436 else
8437 dwStyle &= ~HDS_DRAGDROP;
8438 SetWindowLongW(infoPtr->hwndHeader, GWL_STYLE, dwStyle);
8441 /* GRIDLINES adds decoration at top so changes sizes */
8442 if((infoPtr->dwLvExStyle ^ dwOldExStyle) & LVS_EX_GRIDLINES)
8444 LISTVIEW_UpdateSize(infoPtr);
8447 if((infoPtr->dwLvExStyle ^ dwOldExStyle) & LVS_EX_TRANSPARENTBKGND)
8449 if (infoPtr->dwLvExStyle & LVS_EX_TRANSPARENTBKGND)
8450 LISTVIEW_SetBkColor(infoPtr, CLR_NONE);
8453 if((infoPtr->dwLvExStyle ^ dwOldExStyle) & LVS_EX_HEADERINALLVIEWS)
8455 if (infoPtr->dwLvExStyle & LVS_EX_HEADERINALLVIEWS)
8456 LISTVIEW_CreateHeader(infoPtr);
8457 else
8458 ShowWindow(infoPtr->hwndHeader, SW_HIDE);
8459 LISTVIEW_UpdateSize(infoPtr);
8460 LISTVIEW_UpdateScroll(infoPtr);
8463 LISTVIEW_InvalidateList(infoPtr);
8464 return dwOldExStyle;
8467 /***
8468 * DESCRIPTION:
8469 * Sets the new hot cursor used during hot tracking and hover selection.
8471 * PARAMETER(S):
8472 * [I] infoPtr : valid pointer to the listview structure
8473 * [I] hCursor : the new hot cursor handle
8475 * RETURN:
8476 * Returns the previous hot cursor
8478 static HCURSOR LISTVIEW_SetHotCursor(LISTVIEW_INFO *infoPtr, HCURSOR hCursor)
8480 HCURSOR oldCursor = infoPtr->hHotCursor;
8482 infoPtr->hHotCursor = hCursor;
8484 return oldCursor;
8488 /***
8489 * DESCRIPTION:
8490 * Sets the hot item index.
8492 * PARAMETERS:
8493 * [I] infoPtr : valid pointer to the listview structure
8494 * [I] iIndex : index
8496 * RETURN:
8497 * SUCCESS : previous hot item index
8498 * FAILURE : -1 (no hot item)
8500 static INT LISTVIEW_SetHotItem(LISTVIEW_INFO *infoPtr, INT iIndex)
8502 INT iOldIndex = infoPtr->nHotItem;
8504 infoPtr->nHotItem = iIndex;
8506 return iOldIndex;
8510 /***
8511 * DESCRIPTION:
8512 * Sets the amount of time the cursor must hover over an item before it is selected.
8514 * PARAMETER(S):
8515 * [I] infoPtr : valid pointer to the listview structure
8516 * [I] dwHoverTime : hover time, if -1 the hover time is set to the default
8518 * RETURN:
8519 * Returns the previous hover time
8521 static DWORD LISTVIEW_SetHoverTime(LISTVIEW_INFO *infoPtr, DWORD dwHoverTime)
8523 DWORD oldHoverTime = infoPtr->dwHoverTime;
8525 infoPtr->dwHoverTime = dwHoverTime;
8527 return oldHoverTime;
8530 /***
8531 * DESCRIPTION:
8532 * Sets spacing for icons of LVS_ICON style.
8534 * PARAMETER(S):
8535 * [I] infoPtr : valid pointer to the listview structure
8536 * [I] cx : horizontal spacing (-1 = system spacing, 0 = autosize)
8537 * [I] cy : vertical spacing (-1 = system spacing, 0 = autosize)
8539 * RETURN:
8540 * MAKELONG(oldcx, oldcy)
8542 static DWORD LISTVIEW_SetIconSpacing(LISTVIEW_INFO *infoPtr, INT cx, INT cy)
8544 DWORD oldspacing = MAKELONG(infoPtr->iconSpacing.cx, infoPtr->iconSpacing.cy);
8546 TRACE("requested=(%d,%d)\n", cx, cy);
8548 /* this is supported only for LVS_ICON style */
8549 if (infoPtr->uView != LV_VIEW_ICON) return oldspacing;
8551 /* set to defaults, if instructed to */
8552 if (cx == -1) cx = GetSystemMetrics(SM_CXICONSPACING);
8553 if (cy == -1) cy = GetSystemMetrics(SM_CYICONSPACING);
8555 /* if 0 then compute width
8556 * FIXME: Should scan each item and determine max width of
8557 * icon or label, then make that the width */
8558 if (cx == 0)
8559 cx = infoPtr->iconSpacing.cx;
8561 /* if 0 then compute height */
8562 if (cy == 0)
8563 cy = infoPtr->iconSize.cy + 2 * infoPtr->ntmHeight +
8564 ICON_BOTTOM_PADDING + ICON_TOP_PADDING + LABEL_VERT_PADDING;
8567 infoPtr->iconSpacing.cx = cx;
8568 infoPtr->iconSpacing.cy = cy;
8570 TRACE("old=(%d,%d), new=(%d,%d), iconSize=(%d,%d), ntmH=%d\n",
8571 LOWORD(oldspacing), HIWORD(oldspacing), cx, cy,
8572 infoPtr->iconSize.cx, infoPtr->iconSize.cy,
8573 infoPtr->ntmHeight);
8575 /* these depend on the iconSpacing */
8576 LISTVIEW_UpdateItemSize(infoPtr);
8578 return oldspacing;
8581 static inline void set_icon_size(SIZE *size, HIMAGELIST himl, BOOL small)
8583 INT cx, cy;
8585 if (himl && ImageList_GetIconSize(himl, &cx, &cy))
8587 size->cx = cx;
8588 size->cy = cy;
8590 else
8592 size->cx = GetSystemMetrics(small ? SM_CXSMICON : SM_CXICON);
8593 size->cy = GetSystemMetrics(small ? SM_CYSMICON : SM_CYICON);
8597 /***
8598 * DESCRIPTION:
8599 * Sets image lists.
8601 * PARAMETER(S):
8602 * [I] infoPtr : valid pointer to the listview structure
8603 * [I] nType : image list type
8604 * [I] himl : image list handle
8606 * RETURN:
8607 * SUCCESS : old image list
8608 * FAILURE : NULL
8610 static HIMAGELIST LISTVIEW_SetImageList(LISTVIEW_INFO *infoPtr, INT nType, HIMAGELIST himl)
8612 INT oldHeight = infoPtr->nItemHeight;
8613 HIMAGELIST himlOld = 0;
8615 TRACE("(nType=%d, himl=%p\n", nType, himl);
8617 switch (nType)
8619 case LVSIL_NORMAL:
8620 himlOld = infoPtr->himlNormal;
8621 infoPtr->himlNormal = himl;
8622 if (infoPtr->uView == LV_VIEW_ICON) set_icon_size(&infoPtr->iconSize, himl, FALSE);
8623 LISTVIEW_SetIconSpacing(infoPtr, 0, 0);
8624 break;
8626 case LVSIL_SMALL:
8627 himlOld = infoPtr->himlSmall;
8628 infoPtr->himlSmall = himl;
8629 if (infoPtr->uView != LV_VIEW_ICON) set_icon_size(&infoPtr->iconSize, himl, TRUE);
8630 break;
8632 case LVSIL_STATE:
8633 himlOld = infoPtr->himlState;
8634 infoPtr->himlState = himl;
8635 set_icon_size(&infoPtr->iconStateSize, himl, TRUE);
8636 ImageList_SetBkColor(infoPtr->himlState, CLR_NONE);
8637 break;
8639 default:
8640 ERR("Unknown icon type=%d\n", nType);
8641 return NULL;
8644 infoPtr->nItemHeight = LISTVIEW_CalculateItemHeight(infoPtr);
8645 if (infoPtr->nItemHeight != oldHeight)
8646 LISTVIEW_UpdateScroll(infoPtr);
8648 return himlOld;
8651 /***
8652 * DESCRIPTION:
8653 * Preallocates memory (does *not* set the actual count of items !)
8655 * PARAMETER(S):
8656 * [I] infoPtr : valid pointer to the listview structure
8657 * [I] nItems : item count (projected number of items to allocate)
8658 * [I] dwFlags : update flags
8660 * RETURN:
8661 * SUCCESS : TRUE
8662 * FAILURE : FALSE
8664 static BOOL LISTVIEW_SetItemCount(LISTVIEW_INFO *infoPtr, INT nItems, DWORD dwFlags)
8666 TRACE("(nItems=%d, dwFlags=%x)\n", nItems, dwFlags);
8668 if (infoPtr->dwStyle & LVS_OWNERDATA)
8670 INT nOldCount = infoPtr->nItemCount;
8672 if (nItems < nOldCount)
8674 RANGE range = { nItems, nOldCount };
8675 ranges_del(infoPtr->selectionRanges, range);
8676 if (infoPtr->nFocusedItem >= nItems)
8678 LISTVIEW_SetItemFocus(infoPtr, -1);
8679 SetRectEmpty(&infoPtr->rcFocus);
8683 infoPtr->nItemCount = nItems;
8684 LISTVIEW_UpdateScroll(infoPtr);
8686 /* the flags are valid only in ownerdata report and list modes */
8687 if (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON) dwFlags = 0;
8689 if (!(dwFlags & LVSICF_NOSCROLL) && infoPtr->nFocusedItem != -1)
8690 LISTVIEW_EnsureVisible(infoPtr, infoPtr->nFocusedItem, FALSE);
8692 if (!(dwFlags & LVSICF_NOINVALIDATEALL))
8693 LISTVIEW_InvalidateList(infoPtr);
8694 else
8696 INT nFrom, nTo;
8697 POINT Origin;
8698 RECT rcErase;
8700 LISTVIEW_GetOrigin(infoPtr, &Origin);
8701 nFrom = min(nOldCount, nItems);
8702 nTo = max(nOldCount, nItems);
8704 if (infoPtr->uView == LV_VIEW_DETAILS)
8706 rcErase.left = 0;
8707 rcErase.top = nFrom * infoPtr->nItemHeight;
8708 rcErase.right = infoPtr->nItemWidth;
8709 rcErase.bottom = nTo * infoPtr->nItemHeight;
8710 OffsetRect(&rcErase, Origin.x, Origin.y);
8711 if (IntersectRect(&rcErase, &rcErase, &infoPtr->rcList))
8712 LISTVIEW_InvalidateRect(infoPtr, &rcErase);
8714 else /* LV_VIEW_LIST */
8716 INT nPerCol = LISTVIEW_GetCountPerColumn(infoPtr);
8718 rcErase.left = (nFrom / nPerCol) * infoPtr->nItemWidth;
8719 rcErase.top = (nFrom % nPerCol) * infoPtr->nItemHeight;
8720 rcErase.right = rcErase.left + infoPtr->nItemWidth;
8721 rcErase.bottom = nPerCol * infoPtr->nItemHeight;
8722 OffsetRect(&rcErase, Origin.x, Origin.y);
8723 if (IntersectRect(&rcErase, &rcErase, &infoPtr->rcList))
8724 LISTVIEW_InvalidateRect(infoPtr, &rcErase);
8726 rcErase.left = (nFrom / nPerCol + 1) * infoPtr->nItemWidth;
8727 rcErase.top = 0;
8728 rcErase.right = (nTo / nPerCol + 1) * infoPtr->nItemWidth;
8729 rcErase.bottom = nPerCol * infoPtr->nItemHeight;
8730 OffsetRect(&rcErase, Origin.x, Origin.y);
8731 if (IntersectRect(&rcErase, &rcErase, &infoPtr->rcList))
8732 LISTVIEW_InvalidateRect(infoPtr, &rcErase);
8736 else
8738 /* According to MSDN for non-LVS_OWNERDATA this is just
8739 * a performance issue. The control allocates its internal
8740 * data structures for the number of items specified. It
8741 * cuts down on the number of memory allocations. Therefore
8742 * we will just issue a WARN here
8744 WARN("for non-ownerdata performance option not implemented.\n");
8747 return TRUE;
8750 /***
8751 * DESCRIPTION:
8752 * Sets the position of an item.
8754 * PARAMETER(S):
8755 * [I] infoPtr : valid pointer to the listview structure
8756 * [I] nItem : item index
8757 * [I] pt : coordinate
8759 * RETURN:
8760 * SUCCESS : TRUE
8761 * FAILURE : FALSE
8763 static BOOL LISTVIEW_SetItemPosition(LISTVIEW_INFO *infoPtr, INT nItem, const POINT *pt)
8765 POINT Origin, Pt;
8767 TRACE("(nItem=%d, pt=%s\n", nItem, wine_dbgstr_point(pt));
8769 if (!pt || nItem < 0 || nItem >= infoPtr->nItemCount ||
8770 !(infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON)) return FALSE;
8772 Pt = *pt;
8773 LISTVIEW_GetOrigin(infoPtr, &Origin);
8775 /* This point value seems to be an undocumented feature.
8776 * The best guess is that it means either at the origin,
8777 * or at true beginning of the list. I will assume the origin. */
8778 if ((Pt.x == -1) && (Pt.y == -1))
8779 Pt = Origin;
8781 if (infoPtr->uView == LV_VIEW_ICON)
8783 Pt.x -= (infoPtr->nItemWidth - infoPtr->iconSize.cx) / 2;
8784 Pt.y -= ICON_TOP_PADDING;
8786 Pt.x -= Origin.x;
8787 Pt.y -= Origin.y;
8789 infoPtr->bAutoarrange = FALSE;
8791 return LISTVIEW_MoveIconTo(infoPtr, nItem, &Pt, FALSE);
8794 /***
8795 * DESCRIPTION:
8796 * Sets the state of one or many items.
8798 * PARAMETER(S):
8799 * [I] infoPtr : valid pointer to the listview structure
8800 * [I] nItem : item index
8801 * [I] lpLVItem : item or subitem info
8803 * RETURN:
8804 * SUCCESS : TRUE
8805 * FAILURE : FALSE
8807 static BOOL LISTVIEW_SetItemState(LISTVIEW_INFO *infoPtr, INT nItem, const LVITEMW *lpLVItem)
8809 BOOL bResult = TRUE;
8810 LVITEMW lvItem;
8812 lvItem.iItem = nItem;
8813 lvItem.iSubItem = 0;
8814 lvItem.mask = LVIF_STATE;
8815 lvItem.state = lpLVItem->state;
8816 lvItem.stateMask = lpLVItem->stateMask;
8817 TRACE("lvItem=%s\n", debuglvitem_t(&lvItem, TRUE));
8819 if (nItem == -1)
8821 /* select all isn't allowed in LVS_SINGLESEL */
8822 if ((lvItem.state & lvItem.stateMask & LVIS_SELECTED) && (infoPtr->dwStyle & LVS_SINGLESEL))
8823 return FALSE;
8825 /* focus all isn't allowed */
8826 if (lvItem.state & lvItem.stateMask & LVIS_FOCUSED) return FALSE;
8828 /* apply to all items */
8829 for (lvItem.iItem = 0; lvItem.iItem < infoPtr->nItemCount; lvItem.iItem++)
8830 if (!LISTVIEW_SetItemT(infoPtr, &lvItem, TRUE)) bResult = FALSE;
8832 else
8833 bResult = LISTVIEW_SetItemT(infoPtr, &lvItem, TRUE);
8835 return bResult;
8838 /***
8839 * DESCRIPTION:
8840 * Sets the text of an item or subitem.
8842 * PARAMETER(S):
8843 * [I] hwnd : window handle
8844 * [I] nItem : item index
8845 * [I] lpLVItem : item or subitem info
8846 * [I] isW : TRUE if input is Unicode
8848 * RETURN:
8849 * SUCCESS : TRUE
8850 * FAILURE : FALSE
8852 static BOOL LISTVIEW_SetItemTextT(LISTVIEW_INFO *infoPtr, INT nItem, const LVITEMW *lpLVItem, BOOL isW)
8854 LVITEMW lvItem;
8856 if (!lpLVItem || nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
8858 lvItem.iItem = nItem;
8859 lvItem.iSubItem = lpLVItem->iSubItem;
8860 lvItem.mask = LVIF_TEXT;
8861 lvItem.pszText = lpLVItem->pszText;
8862 lvItem.cchTextMax = lpLVItem->cchTextMax;
8864 TRACE("(nItem=%d, lpLVItem=%s, isW=%d)\n", nItem, debuglvitem_t(&lvItem, isW), isW);
8866 return LISTVIEW_SetItemT(infoPtr, &lvItem, isW);
8869 /***
8870 * DESCRIPTION:
8871 * Set item index that marks the start of a multiple selection.
8873 * PARAMETER(S):
8874 * [I] infoPtr : valid pointer to the listview structure
8875 * [I] nIndex : index
8877 * RETURN:
8878 * Index number or -1 if there is no selection mark.
8880 static INT LISTVIEW_SetSelectionMark(LISTVIEW_INFO *infoPtr, INT nIndex)
8882 INT nOldIndex = infoPtr->nSelectionMark;
8884 TRACE("(nIndex=%d)\n", nIndex);
8886 infoPtr->nSelectionMark = nIndex;
8888 return nOldIndex;
8891 /***
8892 * DESCRIPTION:
8893 * Sets the text background color.
8895 * PARAMETER(S):
8896 * [I] infoPtr : valid pointer to the listview structure
8897 * [I] clrTextBk : text background color
8899 * RETURN:
8900 * SUCCESS : TRUE
8901 * FAILURE : FALSE
8903 static BOOL LISTVIEW_SetTextBkColor(LISTVIEW_INFO *infoPtr, COLORREF clrTextBk)
8905 TRACE("(clrTextBk=%x)\n", clrTextBk);
8907 if (infoPtr->clrTextBk != clrTextBk)
8909 infoPtr->clrTextBk = clrTextBk;
8910 LISTVIEW_InvalidateList(infoPtr);
8913 return TRUE;
8916 /***
8917 * DESCRIPTION:
8918 * Sets the text foreground color.
8920 * PARAMETER(S):
8921 * [I] infoPtr : valid pointer to the listview structure
8922 * [I] clrText : text color
8924 * RETURN:
8925 * SUCCESS : TRUE
8926 * FAILURE : FALSE
8928 static BOOL LISTVIEW_SetTextColor (LISTVIEW_INFO *infoPtr, COLORREF clrText)
8930 TRACE("(clrText=%x)\n", clrText);
8932 if (infoPtr->clrText != clrText)
8934 infoPtr->clrText = clrText;
8935 LISTVIEW_InvalidateList(infoPtr);
8938 return TRUE;
8941 /***
8942 * DESCRIPTION:
8943 * Sets new ToolTip window to ListView control.
8945 * PARAMETER(S):
8946 * [I] infoPtr : valid pointer to the listview structure
8947 * [I] hwndNewToolTip : handle to new ToolTip
8949 * RETURN:
8950 * old tool tip
8952 static HWND LISTVIEW_SetToolTips( LISTVIEW_INFO *infoPtr, HWND hwndNewToolTip)
8954 HWND hwndOldToolTip = infoPtr->hwndToolTip;
8955 infoPtr->hwndToolTip = hwndNewToolTip;
8956 return hwndOldToolTip;
8960 * DESCRIPTION:
8961 * sets the Unicode character format flag for the control
8962 * PARAMETER(S):
8963 * [I] infoPtr :valid pointer to the listview structure
8964 * [I] fUnicode :true to switch to UNICODE false to switch to ANSI
8966 * RETURN:
8967 * Old Unicode Format
8969 static BOOL LISTVIEW_SetUnicodeFormat( LISTVIEW_INFO *infoPtr, BOOL unicode)
8971 SHORT rc = infoPtr->notifyFormat;
8972 infoPtr->notifyFormat = (unicode) ? NFR_UNICODE : NFR_ANSI;
8973 return rc == NFR_UNICODE;
8977 * DESCRIPTION:
8978 * sets the control view mode
8979 * PARAMETER(S):
8980 * [I] infoPtr :valid pointer to the listview structure
8981 * [I] nView :new view mode value
8983 * RETURN:
8984 * SUCCESS: 1
8985 * FAILURE: -1
8987 static INT LISTVIEW_SetView(LISTVIEW_INFO *infoPtr, DWORD nView)
8989 SIZE oldIconSize = infoPtr->iconSize;
8990 HIMAGELIST himl;
8992 if (infoPtr->uView == nView) return 1;
8994 if ((INT)nView < 0 || nView > LV_VIEW_MAX) return -1;
8995 if (nView == LV_VIEW_TILE)
8997 FIXME("View LV_VIEW_TILE unimplemented\n");
8998 return -1;
9001 infoPtr->uView = nView;
9003 SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
9004 ShowWindow(infoPtr->hwndHeader, SW_HIDE);
9006 ShowScrollBar(infoPtr->hwndSelf, SB_BOTH, FALSE);
9007 SetRectEmpty(&infoPtr->rcFocus);
9009 himl = (nView == LV_VIEW_ICON ? infoPtr->himlNormal : infoPtr->himlSmall);
9010 set_icon_size(&infoPtr->iconSize, himl, nView != LV_VIEW_ICON);
9012 switch (nView)
9014 case LV_VIEW_ICON:
9015 if ((infoPtr->iconSize.cx != oldIconSize.cx) || (infoPtr->iconSize.cy != oldIconSize.cy))
9017 TRACE("icon old size=(%d,%d), new size=(%d,%d)\n",
9018 oldIconSize.cx, oldIconSize.cy, infoPtr->iconSize.cx, infoPtr->iconSize.cy);
9019 LISTVIEW_SetIconSpacing(infoPtr, 0, 0);
9021 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
9022 break;
9023 case LV_VIEW_SMALLICON:
9024 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
9025 break;
9026 case LV_VIEW_DETAILS:
9028 HDLAYOUT hl;
9029 WINDOWPOS wp;
9031 LISTVIEW_CreateHeader( infoPtr );
9033 hl.prc = &infoPtr->rcList;
9034 hl.pwpos = &wp;
9035 SendMessageW(infoPtr->hwndHeader, HDM_LAYOUT, 0, (LPARAM)&hl);
9036 SetWindowPos(infoPtr->hwndHeader, infoPtr->hwndSelf, wp.x, wp.y, wp.cx, wp.cy,
9037 wp.flags | ((infoPtr->dwStyle & LVS_NOCOLUMNHEADER) ? SWP_HIDEWINDOW : SWP_SHOWWINDOW));
9038 break;
9040 case LV_VIEW_LIST:
9041 break;
9044 LISTVIEW_UpdateItemSize(infoPtr);
9045 LISTVIEW_UpdateSize(infoPtr);
9046 LISTVIEW_UpdateScroll(infoPtr);
9047 LISTVIEW_InvalidateList(infoPtr);
9049 TRACE("nView=%d\n", nView);
9051 return 1;
9054 /* LISTVIEW_SetWorkAreas */
9056 /***
9057 * DESCRIPTION:
9058 * Callback internally used by LISTVIEW_SortItems() in response of LVM_SORTITEMS
9060 * PARAMETER(S):
9061 * [I] first : pointer to first ITEM_INFO to compare
9062 * [I] second : pointer to second ITEM_INFO to compare
9063 * [I] lParam : HWND of control
9065 * RETURN:
9066 * if first comes before second : negative
9067 * if first comes after second : positive
9068 * if first and second are equivalent : zero
9070 static INT WINAPI LISTVIEW_CallBackCompare(LPVOID first, LPVOID second, LPARAM lParam)
9072 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)lParam;
9073 ITEM_INFO* lv_first = DPA_GetPtr( first, 0 );
9074 ITEM_INFO* lv_second = DPA_GetPtr( second, 0 );
9076 /* Forward the call to the client defined callback */
9077 return (infoPtr->pfnCompare)( lv_first->lParam , lv_second->lParam, infoPtr->lParamSort );
9080 /***
9081 * DESCRIPTION:
9082 * Callback internally used by LISTVIEW_SortItems() in response of LVM_SORTITEMSEX
9084 * PARAMETER(S):
9085 * [I] first : pointer to first ITEM_INFO to compare
9086 * [I] second : pointer to second ITEM_INFO to compare
9087 * [I] lParam : HWND of control
9089 * RETURN:
9090 * if first comes before second : negative
9091 * if first comes after second : positive
9092 * if first and second are equivalent : zero
9094 static INT WINAPI LISTVIEW_CallBackCompareEx(LPVOID first, LPVOID second, LPARAM lParam)
9096 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)lParam;
9097 INT first_idx = DPA_GetPtrIndex( infoPtr->hdpaItems, first );
9098 INT second_idx = DPA_GetPtrIndex( infoPtr->hdpaItems, second );
9100 /* Forward the call to the client defined callback */
9101 return (infoPtr->pfnCompare)( first_idx, second_idx, infoPtr->lParamSort );
9104 /***
9105 * DESCRIPTION:
9106 * Sorts the listview items.
9108 * PARAMETER(S):
9109 * [I] infoPtr : valid pointer to the listview structure
9110 * [I] pfnCompare : application-defined value
9111 * [I] lParamSort : pointer to comparison callback
9112 * [I] IsEx : TRUE when LVM_SORTITEMSEX used
9114 * RETURN:
9115 * SUCCESS : TRUE
9116 * FAILURE : FALSE
9118 static BOOL LISTVIEW_SortItems(LISTVIEW_INFO *infoPtr, PFNLVCOMPARE pfnCompare,
9119 LPARAM lParamSort, BOOL IsEx)
9121 HDPA hdpaSubItems;
9122 ITEM_INFO *lpItem;
9123 LPVOID selectionMarkItem = NULL;
9124 LPVOID focusedItem = NULL;
9125 int i;
9127 TRACE("(pfnCompare=%p, lParamSort=%lx)\n", pfnCompare, lParamSort);
9129 if (infoPtr->dwStyle & LVS_OWNERDATA) return FALSE;
9131 if (!pfnCompare) return FALSE;
9132 if (!infoPtr->hdpaItems) return FALSE;
9134 /* if there are 0 or 1 items, there is no need to sort */
9135 if (infoPtr->nItemCount < 2) return TRUE;
9137 /* clear selection */
9138 ranges_clear(infoPtr->selectionRanges);
9140 /* save selection mark and focused item */
9141 if (infoPtr->nSelectionMark >= 0)
9142 selectionMarkItem = DPA_GetPtr(infoPtr->hdpaItems, infoPtr->nSelectionMark);
9143 if (infoPtr->nFocusedItem >= 0)
9144 focusedItem = DPA_GetPtr(infoPtr->hdpaItems, infoPtr->nFocusedItem);
9146 infoPtr->pfnCompare = pfnCompare;
9147 infoPtr->lParamSort = lParamSort;
9148 if (IsEx)
9149 DPA_Sort(infoPtr->hdpaItems, LISTVIEW_CallBackCompareEx, (LPARAM)infoPtr);
9150 else
9151 DPA_Sort(infoPtr->hdpaItems, LISTVIEW_CallBackCompare, (LPARAM)infoPtr);
9153 /* restore selection ranges */
9154 for (i=0; i < infoPtr->nItemCount; i++)
9156 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, i);
9157 lpItem = DPA_GetPtr(hdpaSubItems, 0);
9159 if (lpItem->state & LVIS_SELECTED)
9160 ranges_additem(infoPtr->selectionRanges, i);
9162 /* restore selection mark and focused item */
9163 infoPtr->nSelectionMark = DPA_GetPtrIndex(infoPtr->hdpaItems, selectionMarkItem);
9164 infoPtr->nFocusedItem = DPA_GetPtrIndex(infoPtr->hdpaItems, focusedItem);
9166 /* I believe nHotItem should be left alone, see LISTVIEW_ShiftIndices */
9168 /* refresh the display */
9169 LISTVIEW_InvalidateList(infoPtr);
9170 return TRUE;
9173 /***
9174 * DESCRIPTION:
9175 * Update theme handle after a theme change.
9177 * PARAMETER(S):
9178 * [I] infoPtr : valid pointer to the listview structure
9180 * RETURN:
9181 * SUCCESS : 0
9182 * FAILURE : something else
9184 static LRESULT LISTVIEW_ThemeChanged(const LISTVIEW_INFO *infoPtr)
9186 HTHEME theme = GetWindowTheme(infoPtr->hwndSelf);
9187 CloseThemeData(theme);
9188 OpenThemeData(infoPtr->hwndSelf, themeClass);
9189 return 0;
9192 /***
9193 * DESCRIPTION:
9194 * Updates an items or rearranges the listview control.
9196 * PARAMETER(S):
9197 * [I] infoPtr : valid pointer to the listview structure
9198 * [I] nItem : item index
9200 * RETURN:
9201 * SUCCESS : TRUE
9202 * FAILURE : FALSE
9204 static BOOL LISTVIEW_Update(LISTVIEW_INFO *infoPtr, INT nItem)
9206 TRACE("(nItem=%d)\n", nItem);
9208 if (nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
9210 /* rearrange with default alignment style */
9211 if (is_autoarrange(infoPtr))
9212 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
9213 else
9214 LISTVIEW_InvalidateItem(infoPtr, nItem);
9216 return TRUE;
9219 /***
9220 * DESCRIPTION:
9221 * Draw the track line at the place defined in the infoPtr structure.
9222 * The line is drawn with a XOR pen so drawing the line for the second time
9223 * in the same place erases the line.
9225 * PARAMETER(S):
9226 * [I] infoPtr : valid pointer to the listview structure
9228 * RETURN:
9229 * SUCCESS : TRUE
9230 * FAILURE : FALSE
9232 static BOOL LISTVIEW_DrawTrackLine(const LISTVIEW_INFO *infoPtr)
9234 HPEN hOldPen;
9235 HDC hdc;
9236 INT oldROP;
9238 if (infoPtr->xTrackLine == -1)
9239 return FALSE;
9241 if (!(hdc = GetDC(infoPtr->hwndSelf)))
9242 return FALSE;
9243 hOldPen = SelectObject(hdc, GetStockObject(BLACK_PEN));
9244 oldROP = SetROP2(hdc, R2_XORPEN);
9245 MoveToEx(hdc, infoPtr->xTrackLine, infoPtr->rcList.top, NULL);
9246 LineTo(hdc, infoPtr->xTrackLine, infoPtr->rcList.bottom);
9247 SetROP2(hdc, oldROP);
9248 SelectObject(hdc, hOldPen);
9249 ReleaseDC(infoPtr->hwndSelf, hdc);
9250 return TRUE;
9253 /***
9254 * DESCRIPTION:
9255 * Called when an edit control should be displayed. This function is called after
9256 * we are sure that there was a single click - not a double click (this is a TIMERPROC).
9258 * PARAMETER(S):
9259 * [I] hwnd : Handle to the listview
9260 * [I] uMsg : WM_TIMER (ignored)
9261 * [I] idEvent : The timer ID interpreted as a pointer to a DELAYED_EDIT_ITEM struct
9262 * [I] dwTimer : The elapsed time (ignored)
9264 * RETURN:
9265 * None.
9267 static VOID CALLBACK LISTVIEW_DelayedEditItem(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
9269 DELAYED_ITEM_EDIT *editItem = (DELAYED_ITEM_EDIT *)idEvent;
9270 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongPtrW(hwnd, 0);
9272 KillTimer(hwnd, idEvent);
9273 editItem->fEnabled = FALSE;
9274 /* check if the item is still selected */
9275 if (infoPtr->bFocus && LISTVIEW_GetItemState(infoPtr, editItem->iItem, LVIS_SELECTED))
9276 LISTVIEW_EditLabelT(infoPtr, editItem->iItem, TRUE);
9279 /***
9280 * DESCRIPTION:
9281 * Creates the listview control - the WM_NCCREATE phase.
9283 * PARAMETER(S):
9284 * [I] hwnd : window handle
9285 * [I] lpcs : the create parameters
9287 * RETURN:
9288 * Success: TRUE
9289 * Failure: FALSE
9291 static LRESULT LISTVIEW_NCCreate(HWND hwnd, const CREATESTRUCTW *lpcs)
9293 LISTVIEW_INFO *infoPtr;
9294 LOGFONTW logFont;
9296 TRACE("(lpcs=%p)\n", lpcs);
9298 /* initialize info pointer */
9299 infoPtr = Alloc(sizeof(LISTVIEW_INFO));
9300 if (!infoPtr) return FALSE;
9302 SetWindowLongPtrW(hwnd, 0, (DWORD_PTR)infoPtr);
9304 infoPtr->hwndSelf = hwnd;
9305 infoPtr->dwStyle = lpcs->style; /* Note: may be changed in WM_CREATE */
9306 map_style_view(infoPtr);
9307 /* determine the type of structures to use */
9308 infoPtr->hwndNotify = lpcs->hwndParent;
9309 /* infoPtr->notifyFormat will be filled in WM_CREATE */
9311 /* initialize color information */
9312 infoPtr->clrBk = CLR_NONE;
9313 infoPtr->clrText = CLR_DEFAULT;
9314 infoPtr->clrTextBk = CLR_DEFAULT;
9315 LISTVIEW_SetBkColor(infoPtr, comctl32_color.clrWindow);
9317 /* set default values */
9318 infoPtr->nFocusedItem = -1;
9319 infoPtr->nSelectionMark = -1;
9320 infoPtr->nHotItem = -1;
9321 infoPtr->bRedraw = TRUE;
9322 infoPtr->bNoItemMetrics = TRUE;
9323 infoPtr->bDoChangeNotify = TRUE;
9324 infoPtr->iconSpacing.cx = GetSystemMetrics(SM_CXICONSPACING);
9325 infoPtr->iconSpacing.cy = GetSystemMetrics(SM_CYICONSPACING);
9326 infoPtr->nEditLabelItem = -1;
9327 infoPtr->nLButtonDownItem = -1;
9328 infoPtr->dwHoverTime = HOVER_DEFAULT; /* default system hover time */
9329 infoPtr->nMeasureItemHeight = 0;
9330 infoPtr->xTrackLine = -1; /* no track line */
9331 infoPtr->itemEdit.fEnabled = FALSE;
9332 infoPtr->iVersion = COMCTL32_VERSION;
9333 infoPtr->colRectsDirty = FALSE;
9335 /* get default font (icon title) */
9336 SystemParametersInfoW(SPI_GETICONTITLELOGFONT, 0, &logFont, 0);
9337 infoPtr->hDefaultFont = CreateFontIndirectW(&logFont);
9338 infoPtr->hFont = infoPtr->hDefaultFont;
9339 LISTVIEW_SaveTextMetrics(infoPtr);
9341 /* allocate memory for the data structure */
9342 if (!(infoPtr->selectionRanges = ranges_create(10))) goto fail;
9343 if (!(infoPtr->hdpaItems = DPA_Create(10))) goto fail;
9344 if (!(infoPtr->hdpaItemIds = DPA_Create(10))) goto fail;
9345 if (!(infoPtr->hdpaPosX = DPA_Create(10))) goto fail;
9346 if (!(infoPtr->hdpaPosY = DPA_Create(10))) goto fail;
9347 if (!(infoPtr->hdpaColumns = DPA_Create(10))) goto fail;
9348 return TRUE;
9350 fail:
9351 DestroyWindow(infoPtr->hwndHeader);
9352 ranges_destroy(infoPtr->selectionRanges);
9353 DPA_Destroy(infoPtr->hdpaItems);
9354 DPA_Destroy(infoPtr->hdpaItemIds);
9355 DPA_Destroy(infoPtr->hdpaPosX);
9356 DPA_Destroy(infoPtr->hdpaPosY);
9357 DPA_Destroy(infoPtr->hdpaColumns);
9358 Free(infoPtr);
9359 return FALSE;
9362 /***
9363 * DESCRIPTION:
9364 * Creates the listview control - the WM_CREATE phase. Most of the data is
9365 * already set up in LISTVIEW_NCCreate
9367 * PARAMETER(S):
9368 * [I] hwnd : window handle
9369 * [I] lpcs : the create parameters
9371 * RETURN:
9372 * Success: 0
9373 * Failure: -1
9375 static LRESULT LISTVIEW_Create(HWND hwnd, const CREATESTRUCTW *lpcs)
9377 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongPtrW(hwnd, 0);
9379 TRACE("(lpcs=%p)\n", lpcs);
9381 infoPtr->dwStyle = lpcs->style;
9382 map_style_view(infoPtr);
9384 infoPtr->notifyFormat = SendMessageW(infoPtr->hwndNotify, WM_NOTIFYFORMAT,
9385 (WPARAM)infoPtr->hwndSelf, NF_QUERY);
9386 /* on error defaulting to ANSI notifications */
9387 if (infoPtr->notifyFormat == 0) infoPtr->notifyFormat = NFR_ANSI;
9388 TRACE("notify format=%d\n", infoPtr->notifyFormat);
9390 if ((infoPtr->uView == LV_VIEW_DETAILS) && (lpcs->style & WS_VISIBLE))
9392 if (LISTVIEW_CreateHeader(infoPtr) < 0) return -1;
9394 else
9395 infoPtr->hwndHeader = 0;
9397 /* init item size to avoid division by 0 */
9398 LISTVIEW_UpdateItemSize (infoPtr);
9400 if (infoPtr->uView == LV_VIEW_DETAILS)
9402 if (!(LVS_NOCOLUMNHEADER & lpcs->style) && (WS_VISIBLE & lpcs->style))
9404 ShowWindow(infoPtr->hwndHeader, SW_SHOWNORMAL);
9406 LISTVIEW_UpdateScroll(infoPtr);
9407 /* send WM_MEASUREITEM notification */
9408 if (infoPtr->dwStyle & LVS_OWNERDRAWFIXED) notify_measureitem(infoPtr);
9411 OpenThemeData(hwnd, themeClass);
9413 /* initialize the icon sizes */
9414 set_icon_size(&infoPtr->iconSize, infoPtr->himlNormal, infoPtr->uView != LV_VIEW_ICON);
9415 set_icon_size(&infoPtr->iconStateSize, infoPtr->himlState, TRUE);
9416 return 0;
9419 /***
9420 * DESCRIPTION:
9421 * Destroys the listview control.
9423 * PARAMETER(S):
9424 * [I] infoPtr : valid pointer to the listview structure
9426 * RETURN:
9427 * Success: 0
9428 * Failure: -1
9430 static LRESULT LISTVIEW_Destroy(LISTVIEW_INFO *infoPtr)
9432 HTHEME theme = GetWindowTheme(infoPtr->hwndSelf);
9433 CloseThemeData(theme);
9435 /* delete all items */
9436 LISTVIEW_DeleteAllItems(infoPtr, TRUE);
9438 return 0;
9441 /***
9442 * DESCRIPTION:
9443 * Enables the listview control.
9445 * PARAMETER(S):
9446 * [I] infoPtr : valid pointer to the listview structure
9447 * [I] bEnable : specifies whether to enable or disable the window
9449 * RETURN:
9450 * SUCCESS : TRUE
9451 * FAILURE : FALSE
9453 static BOOL LISTVIEW_Enable(const LISTVIEW_INFO *infoPtr)
9455 if (infoPtr->dwStyle & LVS_OWNERDRAWFIXED)
9456 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
9457 return TRUE;
9460 /***
9461 * DESCRIPTION:
9462 * Erases the background of the listview control.
9464 * PARAMETER(S):
9465 * [I] infoPtr : valid pointer to the listview structure
9466 * [I] hdc : device context handle
9468 * RETURN:
9469 * SUCCESS : TRUE
9470 * FAILURE : FALSE
9472 static inline BOOL LISTVIEW_EraseBkgnd(const LISTVIEW_INFO *infoPtr, HDC hdc)
9474 RECT rc;
9476 TRACE("(hdc=%p)\n", hdc);
9478 if (!GetClipBox(hdc, &rc)) return FALSE;
9480 if (infoPtr->clrBk == CLR_NONE)
9482 if (infoPtr->dwLvExStyle & LVS_EX_TRANSPARENTBKGND)
9483 return SendMessageW(infoPtr->hwndNotify, WM_PRINTCLIENT,
9484 (WPARAM)hdc, PRF_ERASEBKGND);
9485 else
9486 return SendMessageW(infoPtr->hwndNotify, WM_ERASEBKGND, (WPARAM)hdc, 0);
9489 /* for double buffered controls we need to do this during refresh */
9490 if (infoPtr->dwLvExStyle & LVS_EX_DOUBLEBUFFER) return FALSE;
9492 return LISTVIEW_FillBkgnd(infoPtr, hdc, &rc);
9496 /***
9497 * DESCRIPTION:
9498 * Helper function for LISTVIEW_[HV]Scroll *only*.
9499 * Performs vertical/horizontal scrolling by a give amount.
9501 * PARAMETER(S):
9502 * [I] infoPtr : valid pointer to the listview structure
9503 * [I] dx : amount of horizontal scroll
9504 * [I] dy : amount of vertical scroll
9506 static void scroll_list(LISTVIEW_INFO *infoPtr, INT dx, INT dy)
9508 /* now we can scroll the list */
9509 ScrollWindowEx(infoPtr->hwndSelf, dx, dy, &infoPtr->rcList,
9510 &infoPtr->rcList, 0, 0, SW_ERASE | SW_INVALIDATE);
9511 /* if we have focus, adjust rect */
9512 OffsetRect(&infoPtr->rcFocus, dx, dy);
9513 UpdateWindow(infoPtr->hwndSelf);
9516 /***
9517 * DESCRIPTION:
9518 * Performs vertical scrolling.
9520 * PARAMETER(S):
9521 * [I] infoPtr : valid pointer to the listview structure
9522 * [I] nScrollCode : scroll code
9523 * [I] nScrollDiff : units to scroll in SB_INTERNAL mode, 0 otherwise
9524 * [I] hScrollWnd : scrollbar control window handle
9526 * RETURN:
9527 * Zero
9529 * NOTES:
9530 * SB_LINEUP/SB_LINEDOWN:
9531 * for LVS_ICON, LVS_SMALLICON is 37 by experiment
9532 * for LVS_REPORT is 1 line
9533 * for LVS_LIST cannot occur
9536 static LRESULT LISTVIEW_VScroll(LISTVIEW_INFO *infoPtr, INT nScrollCode,
9537 INT nScrollDiff)
9539 INT nOldScrollPos, nNewScrollPos;
9540 SCROLLINFO scrollInfo;
9541 BOOL is_an_icon;
9543 TRACE("(nScrollCode=%d(%s), nScrollDiff=%d)\n", nScrollCode,
9544 debugscrollcode(nScrollCode), nScrollDiff);
9546 if (infoPtr->hwndEdit) SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
9548 scrollInfo.cbSize = sizeof(SCROLLINFO);
9549 scrollInfo.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_TRACKPOS;
9551 is_an_icon = ((infoPtr->uView == LV_VIEW_ICON) || (infoPtr->uView == LV_VIEW_SMALLICON));
9553 if (!GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo)) return 1;
9555 nOldScrollPos = scrollInfo.nPos;
9556 switch (nScrollCode)
9558 case SB_INTERNAL:
9559 break;
9561 case SB_LINEUP:
9562 nScrollDiff = (is_an_icon) ? -LISTVIEW_SCROLL_ICON_LINE_SIZE : -1;
9563 break;
9565 case SB_LINEDOWN:
9566 nScrollDiff = (is_an_icon) ? LISTVIEW_SCROLL_ICON_LINE_SIZE : 1;
9567 break;
9569 case SB_PAGEUP:
9570 nScrollDiff = -scrollInfo.nPage;
9571 break;
9573 case SB_PAGEDOWN:
9574 nScrollDiff = scrollInfo.nPage;
9575 break;
9577 case SB_THUMBPOSITION:
9578 case SB_THUMBTRACK:
9579 nScrollDiff = scrollInfo.nTrackPos - scrollInfo.nPos;
9580 break;
9582 default:
9583 nScrollDiff = 0;
9586 /* quit right away if pos isn't changing */
9587 if (nScrollDiff == 0) return 0;
9589 /* calculate new position, and handle overflows */
9590 nNewScrollPos = scrollInfo.nPos + nScrollDiff;
9591 if (nScrollDiff > 0) {
9592 if (nNewScrollPos < nOldScrollPos ||
9593 nNewScrollPos > scrollInfo.nMax)
9594 nNewScrollPos = scrollInfo.nMax;
9595 } else {
9596 if (nNewScrollPos > nOldScrollPos ||
9597 nNewScrollPos < scrollInfo.nMin)
9598 nNewScrollPos = scrollInfo.nMin;
9601 /* set the new position, and reread in case it changed */
9602 scrollInfo.fMask = SIF_POS;
9603 scrollInfo.nPos = nNewScrollPos;
9604 nNewScrollPos = SetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo, TRUE);
9606 /* carry on only if it really changed */
9607 if (nNewScrollPos == nOldScrollPos) return 0;
9609 /* now adjust to client coordinates */
9610 nScrollDiff = nOldScrollPos - nNewScrollPos;
9611 if (infoPtr->uView == LV_VIEW_DETAILS) nScrollDiff *= infoPtr->nItemHeight;
9613 /* and scroll the window */
9614 scroll_list(infoPtr, 0, nScrollDiff);
9616 return 0;
9619 /***
9620 * DESCRIPTION:
9621 * Performs horizontal scrolling.
9623 * PARAMETER(S):
9624 * [I] infoPtr : valid pointer to the listview structure
9625 * [I] nScrollCode : scroll code
9626 * [I] nScrollDiff : units to scroll in SB_INTERNAL mode, 0 otherwise
9627 * [I] hScrollWnd : scrollbar control window handle
9629 * RETURN:
9630 * Zero
9632 * NOTES:
9633 * SB_LINELEFT/SB_LINERIGHT:
9634 * for LVS_ICON, LVS_SMALLICON 1 pixel
9635 * for LVS_REPORT is 1 pixel
9636 * for LVS_LIST is 1 column --> which is a 1 because the
9637 * scroll is based on columns not pixels
9640 static LRESULT LISTVIEW_HScroll(LISTVIEW_INFO *infoPtr, INT nScrollCode,
9641 INT nScrollDiff)
9643 INT nOldScrollPos, nNewScrollPos;
9644 SCROLLINFO scrollInfo;
9645 BOOL is_an_icon;
9647 TRACE("(nScrollCode=%d(%s), nScrollDiff=%d)\n", nScrollCode,
9648 debugscrollcode(nScrollCode), nScrollDiff);
9650 if (infoPtr->hwndEdit) SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
9652 scrollInfo.cbSize = sizeof(SCROLLINFO);
9653 scrollInfo.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_TRACKPOS;
9655 is_an_icon = ((infoPtr->uView == LV_VIEW_ICON) || (infoPtr->uView == LV_VIEW_SMALLICON));
9657 if (!GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo)) return 1;
9659 nOldScrollPos = scrollInfo.nPos;
9661 switch (nScrollCode)
9663 case SB_INTERNAL:
9664 break;
9666 case SB_LINELEFT:
9667 nScrollDiff = (is_an_icon) ? -LISTVIEW_SCROLL_ICON_LINE_SIZE : -1;
9668 break;
9670 case SB_LINERIGHT:
9671 nScrollDiff = (is_an_icon) ? LISTVIEW_SCROLL_ICON_LINE_SIZE : 1;
9672 break;
9674 case SB_PAGELEFT:
9675 nScrollDiff = -scrollInfo.nPage;
9676 break;
9678 case SB_PAGERIGHT:
9679 nScrollDiff = scrollInfo.nPage;
9680 break;
9682 case SB_THUMBPOSITION:
9683 case SB_THUMBTRACK:
9684 nScrollDiff = scrollInfo.nTrackPos - scrollInfo.nPos;
9685 break;
9687 default:
9688 nScrollDiff = 0;
9691 /* quit right away if pos isn't changing */
9692 if (nScrollDiff == 0) return 0;
9694 /* calculate new position, and handle overflows */
9695 nNewScrollPos = scrollInfo.nPos + nScrollDiff;
9696 if (nScrollDiff > 0) {
9697 if (nNewScrollPos < nOldScrollPos ||
9698 nNewScrollPos > scrollInfo.nMax)
9699 nNewScrollPos = scrollInfo.nMax;
9700 } else {
9701 if (nNewScrollPos > nOldScrollPos ||
9702 nNewScrollPos < scrollInfo.nMin)
9703 nNewScrollPos = scrollInfo.nMin;
9706 /* set the new position, and reread in case it changed */
9707 scrollInfo.fMask = SIF_POS;
9708 scrollInfo.nPos = nNewScrollPos;
9709 nNewScrollPos = SetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo, TRUE);
9711 /* carry on only if it really changed */
9712 if (nNewScrollPos == nOldScrollPos) return 0;
9714 if (infoPtr->hwndHeader) LISTVIEW_UpdateHeaderSize(infoPtr, nNewScrollPos);
9716 /* now adjust to client coordinates */
9717 nScrollDiff = nOldScrollPos - nNewScrollPos;
9718 if (infoPtr->uView == LV_VIEW_LIST) nScrollDiff *= infoPtr->nItemWidth;
9720 /* and scroll the window */
9721 scroll_list(infoPtr, nScrollDiff, 0);
9723 return 0;
9726 static LRESULT LISTVIEW_MouseWheel(LISTVIEW_INFO *infoPtr, INT wheelDelta)
9728 INT gcWheelDelta = 0;
9729 INT pulScrollLines = 3;
9731 TRACE("(wheelDelta=%d)\n", wheelDelta);
9733 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
9734 gcWheelDelta -= wheelDelta;
9736 switch(infoPtr->uView)
9738 case LV_VIEW_ICON:
9739 case LV_VIEW_SMALLICON:
9741 * listview should be scrolled by a multiple of 37 dependently on its dimension or its visible item number
9742 * should be fixed in the future.
9744 LISTVIEW_VScroll(infoPtr, SB_INTERNAL, (gcWheelDelta < 0) ?
9745 -LISTVIEW_SCROLL_ICON_LINE_SIZE : LISTVIEW_SCROLL_ICON_LINE_SIZE);
9746 break;
9748 case LV_VIEW_DETAILS:
9749 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
9751 int cLineScroll = min(LISTVIEW_GetCountPerColumn(infoPtr), pulScrollLines);
9752 cLineScroll *= (gcWheelDelta / WHEEL_DELTA);
9753 LISTVIEW_VScroll(infoPtr, SB_INTERNAL, cLineScroll);
9755 break;
9757 case LV_VIEW_LIST:
9758 LISTVIEW_HScroll(infoPtr, (gcWheelDelta < 0) ? SB_LINELEFT : SB_LINERIGHT, 0);
9759 break;
9761 return 0;
9764 /***
9765 * DESCRIPTION:
9766 * ???
9768 * PARAMETER(S):
9769 * [I] infoPtr : valid pointer to the listview structure
9770 * [I] nVirtualKey : virtual key
9771 * [I] lKeyData : key data
9773 * RETURN:
9774 * Zero
9776 static LRESULT LISTVIEW_KeyDown(LISTVIEW_INFO *infoPtr, INT nVirtualKey, LONG lKeyData)
9778 HWND hwndSelf = infoPtr->hwndSelf;
9779 INT nItem = -1;
9780 NMLVKEYDOWN nmKeyDown;
9782 TRACE("(nVirtualKey=%d, lKeyData=%d)\n", nVirtualKey, lKeyData);
9784 /* send LVN_KEYDOWN notification */
9785 nmKeyDown.wVKey = nVirtualKey;
9786 nmKeyDown.flags = 0;
9787 notify_hdr(infoPtr, LVN_KEYDOWN, &nmKeyDown.hdr);
9788 if (!IsWindow(hwndSelf))
9789 return 0;
9791 switch (nVirtualKey)
9793 case VK_SPACE:
9794 nItem = infoPtr->nFocusedItem;
9795 if (infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES)
9796 toggle_checkbox_state(infoPtr, infoPtr->nFocusedItem);
9797 break;
9799 case VK_RETURN:
9800 if ((infoPtr->nItemCount > 0) && (infoPtr->nFocusedItem != -1))
9802 if (!notify(infoPtr, NM_RETURN)) return 0;
9803 if (!notify(infoPtr, LVN_ITEMACTIVATE)) return 0;
9805 break;
9807 case VK_HOME:
9808 if (infoPtr->nItemCount > 0)
9809 nItem = 0;
9810 break;
9812 case VK_END:
9813 if (infoPtr->nItemCount > 0)
9814 nItem = infoPtr->nItemCount - 1;
9815 break;
9817 case VK_LEFT:
9818 nItem = LISTVIEW_GetNextItem(infoPtr, infoPtr->nFocusedItem, LVNI_TOLEFT);
9819 break;
9821 case VK_UP:
9822 nItem = LISTVIEW_GetNextItem(infoPtr, infoPtr->nFocusedItem, LVNI_ABOVE);
9823 break;
9825 case VK_RIGHT:
9826 nItem = LISTVIEW_GetNextItem(infoPtr, infoPtr->nFocusedItem, LVNI_TORIGHT);
9827 break;
9829 case VK_DOWN:
9830 nItem = LISTVIEW_GetNextItem(infoPtr, infoPtr->nFocusedItem, LVNI_BELOW);
9831 break;
9833 case VK_PRIOR:
9834 if (infoPtr->uView == LV_VIEW_DETAILS)
9836 INT topidx = LISTVIEW_GetTopIndex(infoPtr);
9837 if (infoPtr->nFocusedItem == topidx)
9838 nItem = topidx - LISTVIEW_GetCountPerColumn(infoPtr) + 1;
9839 else
9840 nItem = topidx;
9842 else
9843 nItem = infoPtr->nFocusedItem - LISTVIEW_GetCountPerColumn(infoPtr)
9844 * LISTVIEW_GetCountPerRow(infoPtr);
9845 if(nItem < 0) nItem = 0;
9846 break;
9848 case VK_NEXT:
9849 if (infoPtr->uView == LV_VIEW_DETAILS)
9851 INT topidx = LISTVIEW_GetTopIndex(infoPtr);
9852 INT cnt = LISTVIEW_GetCountPerColumn(infoPtr);
9853 if (infoPtr->nFocusedItem == topidx + cnt - 1)
9854 nItem = infoPtr->nFocusedItem + cnt - 1;
9855 else
9856 nItem = topidx + cnt - 1;
9858 else
9859 nItem = infoPtr->nFocusedItem + LISTVIEW_GetCountPerColumn(infoPtr)
9860 * LISTVIEW_GetCountPerRow(infoPtr);
9861 if(nItem >= infoPtr->nItemCount) nItem = infoPtr->nItemCount - 1;
9862 break;
9865 if ((nItem != -1) && (nItem != infoPtr->nFocusedItem || nVirtualKey == VK_SPACE))
9866 LISTVIEW_KeySelection(infoPtr, nItem, nVirtualKey == VK_SPACE);
9868 return 0;
9871 /***
9872 * DESCRIPTION:
9873 * Kills the focus.
9875 * PARAMETER(S):
9876 * [I] infoPtr : valid pointer to the listview structure
9878 * RETURN:
9879 * Zero
9881 static LRESULT LISTVIEW_KillFocus(LISTVIEW_INFO *infoPtr)
9883 TRACE("()\n");
9885 /* if we did not have the focus, there's nothing to do */
9886 if (!infoPtr->bFocus) return 0;
9888 /* send NM_KILLFOCUS notification */
9889 if (!notify(infoPtr, NM_KILLFOCUS)) return 0;
9891 /* if we have a focus rectangle, get rid of it */
9892 LISTVIEW_ShowFocusRect(infoPtr, FALSE);
9894 /* if have a marquee selection, stop it */
9895 if (infoPtr->bMarqueeSelect)
9897 /* Remove the marquee rectangle and release our mouse capture */
9898 LISTVIEW_InvalidateRect(infoPtr, &infoPtr->marqueeRect);
9899 ReleaseCapture();
9901 SetRect(&infoPtr->marqueeRect, 0, 0, 0, 0);
9903 infoPtr->bMarqueeSelect = FALSE;
9904 infoPtr->bScrolling = FALSE;
9905 KillTimer(infoPtr->hwndSelf, (UINT_PTR) infoPtr);
9908 /* set window focus flag */
9909 infoPtr->bFocus = FALSE;
9911 /* invalidate the selected items before resetting focus flag */
9912 LISTVIEW_InvalidateSelectedItems(infoPtr);
9914 return 0;
9917 /***
9918 * DESCRIPTION:
9919 * Processes double click messages (left mouse button).
9921 * PARAMETER(S):
9922 * [I] infoPtr : valid pointer to the listview structure
9923 * [I] wKey : key flag
9924 * [I] x,y : mouse coordinate
9926 * RETURN:
9927 * Zero
9929 static LRESULT LISTVIEW_LButtonDblClk(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
9931 LVHITTESTINFO htInfo;
9933 TRACE("(key=%hu, X=%u, Y=%u)\n", wKey, x, y);
9935 /* Cancel the item edition if any */
9936 if (infoPtr->itemEdit.fEnabled)
9938 KillTimer(infoPtr->hwndSelf, (UINT_PTR)&infoPtr->itemEdit);
9939 infoPtr->itemEdit.fEnabled = FALSE;
9942 /* send NM_RELEASEDCAPTURE notification */
9943 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
9945 htInfo.pt.x = x;
9946 htInfo.pt.y = y;
9948 /* send NM_DBLCLK notification */
9949 LISTVIEW_HitTest(infoPtr, &htInfo, TRUE, FALSE);
9950 if (!notify_click(infoPtr, NM_DBLCLK, &htInfo)) return 0;
9952 /* To send the LVN_ITEMACTIVATE, it must be on an Item */
9953 if(htInfo.iItem != -1) notify_itemactivate(infoPtr,&htInfo);
9955 return 0;
9958 /***
9959 * DESCRIPTION:
9960 * Processes mouse down messages (left mouse button).
9962 * PARAMETERS:
9963 * infoPtr [I ] valid pointer to the listview structure
9964 * wKey [I ] key flag
9965 * x,y [I ] mouse coordinate
9967 * RETURN:
9968 * Zero
9970 static LRESULT LISTVIEW_LButtonDown(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
9972 LVHITTESTINFO lvHitTestInfo;
9973 static BOOL bGroupSelect = TRUE;
9974 POINT pt = { x, y };
9975 INT nItem;
9977 TRACE("(key=%hu, X=%u, Y=%u)\n", wKey, x, y);
9979 /* send NM_RELEASEDCAPTURE notification */
9980 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
9982 /* set left button down flag and record the click position */
9983 infoPtr->bLButtonDown = TRUE;
9984 infoPtr->ptClickPos = pt;
9985 infoPtr->bDragging = FALSE;
9986 infoPtr->bMarqueeSelect = FALSE;
9987 infoPtr->bScrolling = FALSE;
9989 lvHitTestInfo.pt.x = x;
9990 lvHitTestInfo.pt.y = y;
9992 nItem = LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, TRUE);
9993 TRACE("at %s, nItem=%d\n", wine_dbgstr_point(&pt), nItem);
9994 if ((nItem >= 0) && (nItem < infoPtr->nItemCount))
9996 if ((infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES) && (lvHitTestInfo.flags & LVHT_ONITEMSTATEICON))
9998 toggle_checkbox_state(infoPtr, nItem);
9999 return 0;
10002 if (infoPtr->dwStyle & LVS_SINGLESEL)
10004 if (LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED))
10005 infoPtr->nEditLabelItem = nItem;
10006 else
10007 LISTVIEW_SetSelection(infoPtr, nItem);
10009 else
10011 if ((wKey & MK_CONTROL) && (wKey & MK_SHIFT))
10013 if (bGroupSelect)
10015 if (!LISTVIEW_AddGroupSelection(infoPtr, nItem)) return 0;
10016 LISTVIEW_SetItemFocus(infoPtr, nItem);
10017 infoPtr->nSelectionMark = nItem;
10019 else
10021 LVITEMW item;
10023 item.state = LVIS_SELECTED | LVIS_FOCUSED;
10024 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
10026 LISTVIEW_SetItemState(infoPtr,nItem,&item);
10027 infoPtr->nSelectionMark = nItem;
10030 else if (wKey & MK_CONTROL)
10032 LVITEMW item;
10034 bGroupSelect = (LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED) == 0);
10036 item.state = (bGroupSelect ? LVIS_SELECTED : 0) | LVIS_FOCUSED;
10037 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
10038 LISTVIEW_SetItemState(infoPtr, nItem, &item);
10039 infoPtr->nSelectionMark = nItem;
10041 else if (wKey & MK_SHIFT)
10043 LISTVIEW_SetGroupSelection(infoPtr, nItem);
10045 else
10047 if (LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED))
10049 infoPtr->nEditLabelItem = nItem;
10050 infoPtr->nLButtonDownItem = nItem;
10052 LISTVIEW_SetItemFocus(infoPtr, nItem);
10054 else
10055 /* set selection (clears other pre-existing selections) */
10056 LISTVIEW_SetSelection(infoPtr, nItem);
10060 if (infoPtr->dwLvExStyle & LVS_EX_ONECLICKACTIVATE)
10061 if(lvHitTestInfo.iItem != -1) notify_itemactivate(infoPtr,&lvHitTestInfo);
10063 else
10065 if (!infoPtr->bFocus)
10066 SetFocus(infoPtr->hwndSelf);
10068 /* remove all selections */
10069 if (!(wKey & MK_CONTROL) && !(wKey & MK_SHIFT))
10070 LISTVIEW_DeselectAll(infoPtr);
10071 ReleaseCapture();
10074 return 0;
10077 /***
10078 * DESCRIPTION:
10079 * Processes mouse up messages (left mouse button).
10081 * PARAMETERS:
10082 * infoPtr [I ] valid pointer to the listview structure
10083 * wKey [I ] key flag
10084 * x,y [I ] mouse coordinate
10086 * RETURN:
10087 * Zero
10089 static LRESULT LISTVIEW_LButtonUp(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
10091 LVHITTESTINFO lvHitTestInfo;
10093 TRACE("(key=%hu, X=%u, Y=%u)\n", wKey, x, y);
10095 if (!infoPtr->bLButtonDown) return 0;
10097 lvHitTestInfo.pt.x = x;
10098 lvHitTestInfo.pt.y = y;
10100 /* send NM_CLICK notification */
10101 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
10102 if (!notify_click(infoPtr, NM_CLICK, &lvHitTestInfo)) return 0;
10104 /* set left button flag */
10105 infoPtr->bLButtonDown = FALSE;
10107 /* set a single selection, reset others */
10108 if(lvHitTestInfo.iItem == infoPtr->nLButtonDownItem && lvHitTestInfo.iItem != -1)
10109 LISTVIEW_SetSelection(infoPtr, infoPtr->nLButtonDownItem);
10110 infoPtr->nLButtonDownItem = -1;
10112 if (infoPtr->bDragging || infoPtr->bMarqueeSelect)
10114 /* Remove the marquee rectangle and release our mouse capture */
10115 if (infoPtr->bMarqueeSelect)
10117 LISTVIEW_InvalidateRect(infoPtr, &infoPtr->marqueeDrawRect);
10118 ReleaseCapture();
10121 SetRect(&infoPtr->marqueeRect, 0, 0, 0, 0);
10122 SetRect(&infoPtr->marqueeDrawRect, 0, 0, 0, 0);
10124 infoPtr->bDragging = FALSE;
10125 infoPtr->bMarqueeSelect = FALSE;
10126 infoPtr->bScrolling = FALSE;
10128 KillTimer(infoPtr->hwndSelf, (UINT_PTR) infoPtr);
10129 return 0;
10132 /* if we clicked on a selected item, edit the label */
10133 if(lvHitTestInfo.iItem == infoPtr->nEditLabelItem && (lvHitTestInfo.flags & LVHT_ONITEMLABEL))
10135 /* we want to make sure the user doesn't want to do a double click. So we will
10136 * delay the edit. WM_LBUTTONDBLCLICK will cancel the timer
10138 infoPtr->itemEdit.fEnabled = TRUE;
10139 infoPtr->itemEdit.iItem = lvHitTestInfo.iItem;
10140 SetTimer(infoPtr->hwndSelf,
10141 (UINT_PTR)&infoPtr->itemEdit,
10142 GetDoubleClickTime(),
10143 LISTVIEW_DelayedEditItem);
10146 if (!infoPtr->bFocus)
10147 SetFocus(infoPtr->hwndSelf);
10149 return 0;
10152 /***
10153 * DESCRIPTION:
10154 * Destroys the listview control (called after WM_DESTROY).
10156 * PARAMETER(S):
10157 * [I] infoPtr : valid pointer to the listview structure
10159 * RETURN:
10160 * Zero
10162 static LRESULT LISTVIEW_NCDestroy(LISTVIEW_INFO *infoPtr)
10164 INT i;
10166 TRACE("()\n");
10168 /* destroy data structure */
10169 DPA_Destroy(infoPtr->hdpaItems);
10170 DPA_Destroy(infoPtr->hdpaItemIds);
10171 DPA_Destroy(infoPtr->hdpaPosX);
10172 DPA_Destroy(infoPtr->hdpaPosY);
10173 /* columns */
10174 for (i = 0; i < DPA_GetPtrCount(infoPtr->hdpaColumns); i++)
10175 Free(DPA_GetPtr(infoPtr->hdpaColumns, i));
10176 DPA_Destroy(infoPtr->hdpaColumns);
10177 ranges_destroy(infoPtr->selectionRanges);
10179 /* destroy image lists */
10180 if (!(infoPtr->dwStyle & LVS_SHAREIMAGELISTS))
10182 ImageList_Destroy(infoPtr->himlNormal);
10183 ImageList_Destroy(infoPtr->himlSmall);
10184 ImageList_Destroy(infoPtr->himlState);
10187 /* destroy font, bkgnd brush */
10188 infoPtr->hFont = 0;
10189 if (infoPtr->hDefaultFont) DeleteObject(infoPtr->hDefaultFont);
10190 if (infoPtr->clrBk != CLR_NONE) DeleteObject(infoPtr->hBkBrush);
10192 SetWindowLongPtrW(infoPtr->hwndSelf, 0, 0);
10194 /* free listview info pointer*/
10195 Free(infoPtr);
10197 return 0;
10200 /***
10201 * DESCRIPTION:
10202 * Handles notifications.
10204 * PARAMETER(S):
10205 * [I] infoPtr : valid pointer to the listview structure
10206 * [I] lpnmhdr : notification information
10208 * RETURN:
10209 * Zero
10211 static LRESULT LISTVIEW_Notify(LISTVIEW_INFO *infoPtr, const NMHDR *lpnmhdr)
10213 const NMHEADERW *lpnmh;
10215 TRACE("(lpnmhdr=%p)\n", lpnmhdr);
10217 if (!lpnmhdr || lpnmhdr->hwndFrom != infoPtr->hwndHeader) return 0;
10219 /* remember: HDN_LAST < HDN_FIRST */
10220 if (lpnmhdr->code > HDN_FIRST || lpnmhdr->code < HDN_LAST) return 0;
10221 lpnmh = (const NMHEADERW *)lpnmhdr;
10223 if (lpnmh->iItem < 0 || lpnmh->iItem >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return 0;
10225 switch (lpnmhdr->code)
10227 case HDN_TRACKW:
10228 case HDN_TRACKA:
10230 COLUMN_INFO *lpColumnInfo;
10231 POINT ptOrigin;
10232 INT x;
10234 if (!lpnmh->pitem || !(lpnmh->pitem->mask & HDI_WIDTH))
10235 break;
10237 /* remove the old line (if any) */
10238 LISTVIEW_DrawTrackLine(infoPtr);
10240 /* compute & draw the new line */
10241 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, lpnmh->iItem);
10242 x = lpColumnInfo->rcHeader.left + lpnmh->pitem->cxy;
10243 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
10244 infoPtr->xTrackLine = x + ptOrigin.x;
10245 LISTVIEW_DrawTrackLine(infoPtr);
10246 break;
10249 case HDN_ENDTRACKA:
10250 case HDN_ENDTRACKW:
10251 /* remove the track line (if any) */
10252 LISTVIEW_DrawTrackLine(infoPtr);
10253 infoPtr->xTrackLine = -1;
10254 break;
10256 case HDN_BEGINDRAG:
10257 notify_forward_header(infoPtr, lpnmh);
10258 return (infoPtr->dwLvExStyle & LVS_EX_HEADERDRAGDROP) == 0;
10260 case HDN_ENDDRAG:
10261 infoPtr->colRectsDirty = TRUE;
10262 LISTVIEW_InvalidateList(infoPtr);
10263 notify_forward_header(infoPtr, lpnmh);
10264 return FALSE;
10266 case HDN_ITEMCHANGEDW:
10267 case HDN_ITEMCHANGEDA:
10269 COLUMN_INFO *lpColumnInfo;
10270 HDITEMW hdi;
10271 INT dx, cxy;
10273 if (!lpnmh->pitem || !(lpnmh->pitem->mask & HDI_WIDTH))
10275 hdi.mask = HDI_WIDTH;
10276 if (!SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, lpnmh->iItem, (LPARAM)&hdi)) return 0;
10277 cxy = hdi.cxy;
10279 else
10280 cxy = lpnmh->pitem->cxy;
10282 /* determine how much we change since the last know position */
10283 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, lpnmh->iItem);
10284 dx = cxy - (lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left);
10285 if (dx != 0)
10287 lpColumnInfo->rcHeader.right += dx;
10289 hdi.mask = HDI_ORDER;
10290 SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, lpnmh->iItem, (LPARAM)&hdi);
10292 /* not the rightmost one */
10293 if (hdi.iOrder + 1 < DPA_GetPtrCount(infoPtr->hdpaColumns))
10295 INT nIndex = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX,
10296 hdi.iOrder + 1, 0);
10297 LISTVIEW_ScrollColumns(infoPtr, nIndex, dx);
10299 else
10301 /* only needs to update the scrolls */
10302 infoPtr->nItemWidth += dx;
10303 LISTVIEW_UpdateScroll(infoPtr);
10305 LISTVIEW_UpdateItemSize(infoPtr);
10306 if (infoPtr->uView == LV_VIEW_DETAILS && is_redrawing(infoPtr))
10308 POINT ptOrigin;
10309 RECT rcCol = lpColumnInfo->rcHeader;
10311 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
10312 OffsetRect(&rcCol, ptOrigin.x, 0);
10314 rcCol.top = infoPtr->rcList.top;
10315 rcCol.bottom = infoPtr->rcList.bottom;
10317 /* resizing left-aligned columns leaves most of the left side untouched */
10318 if ((lpColumnInfo->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_LEFT)
10320 INT nMaxDirty = infoPtr->nEllipsisWidth + infoPtr->ntmMaxCharWidth;
10321 if (dx > 0)
10322 nMaxDirty += dx;
10323 rcCol.left = max (rcCol.left, rcCol.right - nMaxDirty);
10326 /* when shrinking the last column clear the now unused field */
10327 if (hdi.iOrder == DPA_GetPtrCount(infoPtr->hdpaColumns) - 1)
10329 RECT right;
10331 rcCol.right -= dx;
10333 /* deal with right from rightmost column area */
10334 right.left = rcCol.right;
10335 right.top = rcCol.top;
10336 right.bottom = rcCol.bottom;
10337 right.right = infoPtr->rcList.right;
10339 LISTVIEW_InvalidateRect(infoPtr, &right);
10342 LISTVIEW_InvalidateRect(infoPtr, &rcCol);
10346 break;
10348 case HDN_ITEMCLICKW:
10349 case HDN_ITEMCLICKA:
10351 /* Handle sorting by Header Column */
10352 NMLISTVIEW nmlv;
10354 ZeroMemory(&nmlv, sizeof(NMLISTVIEW));
10355 nmlv.iItem = -1;
10356 nmlv.iSubItem = lpnmh->iItem;
10357 notify_listview(infoPtr, LVN_COLUMNCLICK, &nmlv);
10358 notify_forward_header(infoPtr, lpnmh);
10360 break;
10362 case HDN_DIVIDERDBLCLICKW:
10363 case HDN_DIVIDERDBLCLICKA:
10364 /* FIXME: for LVS_EX_HEADERINALLVIEWS and not LV_VIEW_DETAILS
10365 we should use LVSCW_AUTOSIZE_USEHEADER, helper rework or
10366 split needed for that */
10367 LISTVIEW_SetColumnWidth(infoPtr, lpnmh->iItem, LVSCW_AUTOSIZE);
10368 notify_forward_header(infoPtr, lpnmh);
10369 break;
10372 return 0;
10375 /***
10376 * DESCRIPTION:
10377 * Paint non-client area of control.
10379 * PARAMETER(S):
10380 * [I] infoPtr : valid pointer to the listview structureof the sender
10381 * [I] region : update region
10383 * RETURN:
10384 * TRUE - frame was painted
10385 * FALSE - call default window proc
10387 static BOOL LISTVIEW_NCPaint(const LISTVIEW_INFO *infoPtr, HRGN region)
10389 HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
10390 HDC dc;
10391 RECT r;
10392 HRGN cliprgn;
10393 int cxEdge = GetSystemMetrics (SM_CXEDGE),
10394 cyEdge = GetSystemMetrics (SM_CYEDGE);
10396 if (!theme)
10397 return DefWindowProcW (infoPtr->hwndSelf, WM_NCPAINT, (WPARAM)region, 0);
10399 GetWindowRect(infoPtr->hwndSelf, &r);
10401 cliprgn = CreateRectRgn (r.left + cxEdge, r.top + cyEdge,
10402 r.right - cxEdge, r.bottom - cyEdge);
10403 if (region != (HRGN)1)
10404 CombineRgn (cliprgn, cliprgn, region, RGN_AND);
10405 OffsetRect(&r, -r.left, -r.top);
10407 dc = GetDCEx(infoPtr->hwndSelf, region, DCX_WINDOW|DCX_INTERSECTRGN);
10408 OffsetRect(&r, -r.left, -r.top);
10410 if (IsThemeBackgroundPartiallyTransparent (theme, 0, 0))
10411 DrawThemeParentBackground(infoPtr->hwndSelf, dc, &r);
10412 DrawThemeBackground (theme, dc, 0, 0, &r, 0);
10413 ReleaseDC(infoPtr->hwndSelf, dc);
10415 /* Call default proc to get the scrollbars etc. painted */
10416 DefWindowProcW (infoPtr->hwndSelf, WM_NCPAINT, (WPARAM)cliprgn, 0);
10418 return FALSE;
10421 /***
10422 * DESCRIPTION:
10423 * Determines the type of structure to use.
10425 * PARAMETER(S):
10426 * [I] infoPtr : valid pointer to the listview structureof the sender
10427 * [I] hwndFrom : listview window handle
10428 * [I] nCommand : command specifying the nature of the WM_NOTIFYFORMAT
10430 * RETURN:
10431 * Zero
10433 static LRESULT LISTVIEW_NotifyFormat(LISTVIEW_INFO *infoPtr, HWND hwndFrom, INT nCommand)
10435 TRACE("(hwndFrom=%p, nCommand=%d)\n", hwndFrom, nCommand);
10437 if (nCommand == NF_REQUERY)
10438 infoPtr->notifyFormat = SendMessageW(infoPtr->hwndNotify, WM_NOTIFYFORMAT, (WPARAM)infoPtr->hwndSelf, NF_QUERY);
10440 return infoPtr->notifyFormat;
10443 /***
10444 * DESCRIPTION:
10445 * Paints/Repaints the listview control. Internal use.
10447 * PARAMETER(S):
10448 * [I] infoPtr : valid pointer to the listview structure
10449 * [I] hdc : device context handle
10451 * RETURN:
10452 * Zero
10454 static LRESULT LISTVIEW_Paint(LISTVIEW_INFO *infoPtr, HDC hdc)
10456 TRACE("(hdc=%p)\n", hdc);
10458 if (infoPtr->bNoItemMetrics && infoPtr->nItemCount)
10460 infoPtr->bNoItemMetrics = FALSE;
10461 LISTVIEW_UpdateItemSize(infoPtr);
10462 if (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON)
10463 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
10464 LISTVIEW_UpdateScroll(infoPtr);
10467 if (infoPtr->hwndHeader) UpdateWindow(infoPtr->hwndHeader);
10469 if (hdc)
10470 LISTVIEW_Refresh(infoPtr, hdc, NULL);
10471 else
10473 PAINTSTRUCT ps;
10475 hdc = BeginPaint(infoPtr->hwndSelf, &ps);
10476 if (!hdc) return 1;
10477 LISTVIEW_Refresh(infoPtr, hdc, ps.fErase ? &ps.rcPaint : NULL);
10478 EndPaint(infoPtr->hwndSelf, &ps);
10481 return 0;
10484 /***
10485 * DESCRIPTION:
10486 * Paints/Repaints the listview control, WM_PAINT handler.
10488 * PARAMETER(S):
10489 * [I] infoPtr : valid pointer to the listview structure
10490 * [I] hdc : device context handle
10492 * RETURN:
10493 * Zero
10495 static inline LRESULT LISTVIEW_WMPaint(LISTVIEW_INFO *infoPtr, HDC hdc)
10497 TRACE("(hdc=%p)\n", hdc);
10499 if (!is_redrawing(infoPtr))
10500 return DefWindowProcW (infoPtr->hwndSelf, WM_PAINT, (WPARAM)hdc, 0);
10502 return LISTVIEW_Paint(infoPtr, hdc);
10505 /***
10506 * DESCRIPTION:
10507 * Paints/Repaints the listview control.
10509 * PARAMETER(S):
10510 * [I] infoPtr : valid pointer to the listview structure
10511 * [I] hdc : device context handle
10512 * [I] options : drawing options
10514 * RETURN:
10515 * Zero
10517 static LRESULT LISTVIEW_PrintClient(LISTVIEW_INFO *infoPtr, HDC hdc, DWORD options)
10519 FIXME("Partial Stub: (hdc=%p options=0x%08x)\n", hdc, options);
10521 if ((options & PRF_CHECKVISIBLE) && !IsWindowVisible(infoPtr->hwndSelf))
10522 return 0;
10524 if (options & PRF_ERASEBKGND)
10525 LISTVIEW_EraseBkgnd(infoPtr, hdc);
10527 if (options & PRF_CLIENT)
10528 LISTVIEW_Paint(infoPtr, hdc);
10530 return 0;
10534 /***
10535 * DESCRIPTION:
10536 * Processes double click messages (right mouse button).
10538 * PARAMETER(S):
10539 * [I] infoPtr : valid pointer to the listview structure
10540 * [I] wKey : key flag
10541 * [I] x,y : mouse coordinate
10543 * RETURN:
10544 * Zero
10546 static LRESULT LISTVIEW_RButtonDblClk(const LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
10548 LVHITTESTINFO lvHitTestInfo;
10550 TRACE("(key=%hu,X=%u,Y=%u)\n", wKey, x, y);
10552 /* send NM_RELEASEDCAPTURE notification */
10553 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
10555 /* send NM_RDBLCLK notification */
10556 lvHitTestInfo.pt.x = x;
10557 lvHitTestInfo.pt.y = y;
10558 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
10559 notify_click(infoPtr, NM_RDBLCLK, &lvHitTestInfo);
10561 return 0;
10564 /***
10565 * DESCRIPTION:
10566 * Processes mouse down messages (right mouse button).
10568 * PARAMETER(S):
10569 * [I] infoPtr : valid pointer to the listview structure
10570 * [I] wKey : key flag
10571 * [I] x,y : mouse coordinate
10573 * RETURN:
10574 * Zero
10576 static LRESULT LISTVIEW_RButtonDown(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
10578 LVHITTESTINFO lvHitTestInfo;
10579 INT nItem;
10581 TRACE("(key=%hu,X=%u,Y=%u)\n", wKey, x, y);
10583 /* send NM_RELEASEDCAPTURE notification */
10584 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
10586 /* make sure the listview control window has the focus */
10587 if (!infoPtr->bFocus) SetFocus(infoPtr->hwndSelf);
10589 /* set right button down flag */
10590 infoPtr->bRButtonDown = TRUE;
10592 /* determine the index of the selected item */
10593 lvHitTestInfo.pt.x = x;
10594 lvHitTestInfo.pt.y = y;
10595 nItem = LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, TRUE);
10597 if ((nItem >= 0) && (nItem < infoPtr->nItemCount))
10599 LISTVIEW_SetItemFocus(infoPtr, nItem);
10600 if (!((wKey & MK_SHIFT) || (wKey & MK_CONTROL)) &&
10601 !LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED))
10602 LISTVIEW_SetSelection(infoPtr, nItem);
10604 else
10606 LISTVIEW_DeselectAll(infoPtr);
10609 return 0;
10612 /***
10613 * DESCRIPTION:
10614 * Processes mouse up messages (right mouse button).
10616 * PARAMETER(S):
10617 * [I] infoPtr : valid pointer to the listview structure
10618 * [I] wKey : key flag
10619 * [I] x,y : mouse coordinate
10621 * RETURN:
10622 * Zero
10624 static LRESULT LISTVIEW_RButtonUp(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
10626 LVHITTESTINFO lvHitTestInfo;
10627 POINT pt;
10629 TRACE("(key=%hu,X=%u,Y=%u)\n", wKey, x, y);
10631 if (!infoPtr->bRButtonDown) return 0;
10633 /* set button flag */
10634 infoPtr->bRButtonDown = FALSE;
10636 /* Send NM_RCLICK notification */
10637 lvHitTestInfo.pt.x = x;
10638 lvHitTestInfo.pt.y = y;
10639 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
10640 if (!notify_click(infoPtr, NM_RCLICK, &lvHitTestInfo)) return 0;
10642 /* Change to screen coordinate for WM_CONTEXTMENU */
10643 pt = lvHitTestInfo.pt;
10644 ClientToScreen(infoPtr->hwndSelf, &pt);
10646 /* Send a WM_CONTEXTMENU message in response to the RBUTTONUP */
10647 SendMessageW(infoPtr->hwndSelf, WM_CONTEXTMENU,
10648 (WPARAM)infoPtr->hwndSelf, MAKELPARAM(pt.x, pt.y));
10650 return 0;
10654 /***
10655 * DESCRIPTION:
10656 * Sets the cursor.
10658 * PARAMETER(S):
10659 * [I] infoPtr : valid pointer to the listview structure
10660 * [I] hwnd : window handle of window containing the cursor
10661 * [I] nHittest : hit-test code
10662 * [I] wMouseMsg : ideintifier of the mouse message
10664 * RETURN:
10665 * TRUE if cursor is set
10666 * FALSE otherwise
10668 static BOOL LISTVIEW_SetCursor(const LISTVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
10670 LVHITTESTINFO lvHitTestInfo;
10672 if (!LISTVIEW_IsHotTracking(infoPtr)) goto forward;
10674 if (!infoPtr->hHotCursor) goto forward;
10676 GetCursorPos(&lvHitTestInfo.pt);
10677 if (LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, FALSE, FALSE) < 0) goto forward;
10679 SetCursor(infoPtr->hHotCursor);
10681 return TRUE;
10683 forward:
10685 return DefWindowProcW(infoPtr->hwndSelf, WM_SETCURSOR, wParam, lParam);
10688 /***
10689 * DESCRIPTION:
10690 * Sets the focus.
10692 * PARAMETER(S):
10693 * [I] infoPtr : valid pointer to the listview structure
10694 * [I] hwndLoseFocus : handle of previously focused window
10696 * RETURN:
10697 * Zero
10699 static LRESULT LISTVIEW_SetFocus(LISTVIEW_INFO *infoPtr, HWND hwndLoseFocus)
10701 TRACE("(hwndLoseFocus=%p)\n", hwndLoseFocus);
10703 /* if we have the focus already, there's nothing to do */
10704 if (infoPtr->bFocus) return 0;
10706 /* send NM_SETFOCUS notification */
10707 if (!notify(infoPtr, NM_SETFOCUS)) return 0;
10709 /* set window focus flag */
10710 infoPtr->bFocus = TRUE;
10712 /* put the focus rect back on */
10713 LISTVIEW_ShowFocusRect(infoPtr, TRUE);
10715 /* redraw all visible selected items */
10716 LISTVIEW_InvalidateSelectedItems(infoPtr);
10718 return 0;
10721 /***
10722 * DESCRIPTION:
10723 * Sets the font.
10725 * PARAMETER(S):
10726 * [I] infoPtr : valid pointer to the listview structure
10727 * [I] fRedraw : font handle
10728 * [I] fRedraw : redraw flag
10730 * RETURN:
10731 * Zero
10733 static LRESULT LISTVIEW_SetFont(LISTVIEW_INFO *infoPtr, HFONT hFont, WORD fRedraw)
10735 HFONT oldFont = infoPtr->hFont;
10737 TRACE("(hfont=%p,redraw=%hu)\n", hFont, fRedraw);
10739 infoPtr->hFont = hFont ? hFont : infoPtr->hDefaultFont;
10740 if (infoPtr->hFont == oldFont) return 0;
10742 LISTVIEW_SaveTextMetrics(infoPtr);
10744 if (infoPtr->uView == LV_VIEW_DETAILS)
10746 SendMessageW(infoPtr->hwndHeader, WM_SETFONT, (WPARAM)hFont, MAKELPARAM(fRedraw, 0));
10747 LISTVIEW_UpdateSize(infoPtr);
10748 LISTVIEW_UpdateScroll(infoPtr);
10751 if (fRedraw) LISTVIEW_InvalidateList(infoPtr);
10753 return 0;
10756 /***
10757 * DESCRIPTION:
10758 * Message handling for WM_SETREDRAW.
10759 * For the Listview, it invalidates the entire window (the doc specifies otherwise)
10761 * PARAMETER(S):
10762 * [I] infoPtr : valid pointer to the listview structure
10763 * [I] bRedraw: state of redraw flag
10765 * RETURN:
10766 * DefWinProc return value
10768 static LRESULT LISTVIEW_SetRedraw(LISTVIEW_INFO *infoPtr, BOOL bRedraw)
10770 TRACE("infoPtr->bRedraw=%d, bRedraw=%d\n", infoPtr->bRedraw, bRedraw);
10772 /* we cannot use straight equality here because _any_ non-zero value is TRUE */
10773 if ((infoPtr->bRedraw && bRedraw) || (!infoPtr->bRedraw && !bRedraw)) return 0;
10775 infoPtr->bRedraw = bRedraw;
10777 if(!bRedraw) return 0;
10779 if (is_autoarrange(infoPtr))
10780 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
10781 LISTVIEW_UpdateScroll(infoPtr);
10783 /* despite what the WM_SETREDRAW docs says, apps expect us
10784 * to invalidate the listview here... stupid! */
10785 LISTVIEW_InvalidateList(infoPtr);
10787 return 0;
10790 /***
10791 * DESCRIPTION:
10792 * Resizes the listview control. This function processes WM_SIZE
10793 * messages. At this time, the width and height are not used.
10795 * PARAMETER(S):
10796 * [I] infoPtr : valid pointer to the listview structure
10797 * [I] Width : new width
10798 * [I] Height : new height
10800 * RETURN:
10801 * Zero
10803 static LRESULT LISTVIEW_Size(LISTVIEW_INFO *infoPtr, int Width, int Height)
10805 RECT rcOld = infoPtr->rcList;
10807 TRACE("(width=%d, height=%d)\n", Width, Height);
10809 LISTVIEW_UpdateSize(infoPtr);
10810 if (EqualRect(&rcOld, &infoPtr->rcList)) return 0;
10812 /* do not bother with display related stuff if we're not redrawing */
10813 if (!is_redrawing(infoPtr)) return 0;
10815 if (is_autoarrange(infoPtr))
10816 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
10818 LISTVIEW_UpdateScroll(infoPtr);
10820 /* refresh all only for lists whose height changed significantly */
10821 if ((infoPtr->uView == LV_VIEW_LIST) &&
10822 (rcOld.bottom - rcOld.top) / infoPtr->nItemHeight !=
10823 (infoPtr->rcList.bottom - infoPtr->rcList.top) / infoPtr->nItemHeight)
10824 LISTVIEW_InvalidateList(infoPtr);
10826 return 0;
10829 /***
10830 * DESCRIPTION:
10831 * Sets the size information.
10833 * PARAMETER(S):
10834 * [I] infoPtr : valid pointer to the listview structure
10836 * RETURN:
10837 * None
10839 static void LISTVIEW_UpdateSize(LISTVIEW_INFO *infoPtr)
10841 TRACE("uView=%d, rcList(old)=%s\n", infoPtr->uView, wine_dbgstr_rect(&infoPtr->rcList));
10843 GetClientRect(infoPtr->hwndSelf, &infoPtr->rcList);
10845 if (infoPtr->uView == LV_VIEW_LIST)
10847 /* Apparently the "LIST" style is supposed to have the same
10848 * number of items in a column even if there is no scroll bar.
10849 * Since if a scroll bar already exists then the bottom is already
10850 * reduced, only reduce if the scroll bar does not currently exist.
10851 * The "2" is there to mimic the native control. I think it may be
10852 * related to either padding or edges. (GLA 7/2002)
10854 if (!(infoPtr->dwStyle & WS_HSCROLL))
10855 infoPtr->rcList.bottom -= GetSystemMetrics(SM_CYHSCROLL);
10856 infoPtr->rcList.bottom = max (infoPtr->rcList.bottom - 2, 0);
10859 /* if control created invisible header isn't created */
10860 if (infoPtr->hwndHeader)
10862 HDLAYOUT hl;
10863 WINDOWPOS wp;
10865 hl.prc = &infoPtr->rcList;
10866 hl.pwpos = &wp;
10867 SendMessageW( infoPtr->hwndHeader, HDM_LAYOUT, 0, (LPARAM)&hl );
10868 TRACE(" wp.flags=0x%08x, wp=%d,%d (%dx%d)\n", wp.flags, wp.x, wp.y, wp.cx, wp.cy);
10870 if (LISTVIEW_IsHeaderEnabled(infoPtr))
10871 wp.flags |= SWP_SHOWWINDOW;
10872 else
10874 wp.flags |= SWP_HIDEWINDOW;
10875 wp.cy = 0;
10878 SetWindowPos(wp.hwnd, wp.hwndInsertAfter, wp.x, wp.y, wp.cx, wp.cy, wp.flags);
10879 TRACE(" after SWP wp=%d,%d (%dx%d)\n", wp.x, wp.y, wp.cx, wp.cy);
10881 infoPtr->rcList.top = max(wp.cy, 0);
10883 /* extra padding for grid */
10884 if (infoPtr->uView == LV_VIEW_DETAILS && infoPtr->dwLvExStyle & LVS_EX_GRIDLINES)
10885 infoPtr->rcList.top += 2;
10887 TRACE(" rcList=%s\n", wine_dbgstr_rect(&infoPtr->rcList));
10890 /***
10891 * DESCRIPTION:
10892 * Processes WM_STYLECHANGED messages.
10894 * PARAMETER(S):
10895 * [I] infoPtr : valid pointer to the listview structure
10896 * [I] wStyleType : window style type (normal or extended)
10897 * [I] lpss : window style information
10899 * RETURN:
10900 * Zero
10902 static INT LISTVIEW_StyleChanged(LISTVIEW_INFO *infoPtr, WPARAM wStyleType,
10903 const STYLESTRUCT *lpss)
10905 UINT uNewView = lpss->styleNew & LVS_TYPEMASK;
10906 UINT uOldView = lpss->styleOld & LVS_TYPEMASK;
10907 UINT style;
10909 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
10910 wStyleType, lpss->styleOld, lpss->styleNew);
10912 if (wStyleType != GWL_STYLE) return 0;
10914 infoPtr->dwStyle = lpss->styleNew;
10915 map_style_view(infoPtr);
10917 if (((lpss->styleOld & WS_HSCROLL) != 0)&&
10918 ((lpss->styleNew & WS_HSCROLL) == 0))
10919 ShowScrollBar(infoPtr->hwndSelf, SB_HORZ, FALSE);
10921 if (((lpss->styleOld & WS_VSCROLL) != 0)&&
10922 ((lpss->styleNew & WS_VSCROLL) == 0))
10923 ShowScrollBar(infoPtr->hwndSelf, SB_VERT, FALSE);
10925 if (uNewView != uOldView)
10927 SIZE oldIconSize = infoPtr->iconSize;
10928 HIMAGELIST himl;
10930 SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
10931 ShowWindow(infoPtr->hwndHeader, SW_HIDE);
10933 ShowScrollBar(infoPtr->hwndSelf, SB_BOTH, FALSE);
10934 SetRectEmpty(&infoPtr->rcFocus);
10936 himl = (uNewView == LVS_ICON ? infoPtr->himlNormal : infoPtr->himlSmall);
10937 set_icon_size(&infoPtr->iconSize, himl, uNewView != LVS_ICON);
10939 if (uNewView == LVS_ICON)
10941 if ((infoPtr->iconSize.cx != oldIconSize.cx) || (infoPtr->iconSize.cy != oldIconSize.cy))
10943 TRACE("icon old size=(%d,%d), new size=(%d,%d)\n",
10944 oldIconSize.cx, oldIconSize.cy, infoPtr->iconSize.cx, infoPtr->iconSize.cy);
10945 LISTVIEW_SetIconSpacing(infoPtr, 0, 0);
10948 else if (uNewView == LVS_REPORT)
10950 HDLAYOUT hl;
10951 WINDOWPOS wp;
10953 LISTVIEW_CreateHeader( infoPtr );
10955 hl.prc = &infoPtr->rcList;
10956 hl.pwpos = &wp;
10957 SendMessageW( infoPtr->hwndHeader, HDM_LAYOUT, 0, (LPARAM)&hl );
10958 SetWindowPos(infoPtr->hwndHeader, infoPtr->hwndSelf, wp.x, wp.y, wp.cx, wp.cy,
10959 wp.flags | ((infoPtr->dwStyle & LVS_NOCOLUMNHEADER)
10960 ? SWP_HIDEWINDOW : SWP_SHOWWINDOW));
10963 LISTVIEW_UpdateItemSize(infoPtr);
10966 if (uNewView == LVS_REPORT || infoPtr->dwLvExStyle & LVS_EX_HEADERINALLVIEWS)
10968 if ((lpss->styleOld ^ lpss->styleNew) & LVS_NOCOLUMNHEADER)
10970 if (lpss->styleNew & LVS_NOCOLUMNHEADER)
10972 /* Turn off the header control */
10973 style = GetWindowLongW(infoPtr->hwndHeader, GWL_STYLE);
10974 TRACE("Hide header control, was 0x%08x\n", style);
10975 SetWindowLongW(infoPtr->hwndHeader, GWL_STYLE, style | HDS_HIDDEN);
10976 } else {
10977 /* Turn on the header control */
10978 if ((style = GetWindowLongW(infoPtr->hwndHeader, GWL_STYLE)) & HDS_HIDDEN)
10980 TRACE("Show header control, was 0x%08x\n", style);
10981 SetWindowLongW(infoPtr->hwndHeader, GWL_STYLE, (style & ~HDS_HIDDEN) | WS_VISIBLE);
10987 if ( (uNewView == LVS_ICON || uNewView == LVS_SMALLICON) &&
10988 (uNewView != uOldView || ((lpss->styleNew ^ lpss->styleOld) & LVS_ALIGNMASK)) )
10989 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
10991 /* update the size of the client area */
10992 LISTVIEW_UpdateSize(infoPtr);
10994 /* add scrollbars if needed */
10995 LISTVIEW_UpdateScroll(infoPtr);
10997 /* invalidate client area + erase background */
10998 LISTVIEW_InvalidateList(infoPtr);
11000 return 0;
11003 /***
11004 * DESCRIPTION:
11005 * Processes WM_STYLECHANGING messages.
11007 * PARAMETER(S):
11008 * [I] wStyleType : window style type (normal or extended)
11009 * [I0] lpss : window style information
11011 * RETURN:
11012 * Zero
11014 static INT LISTVIEW_StyleChanging(WPARAM wStyleType,
11015 STYLESTRUCT *lpss)
11017 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
11018 wStyleType, lpss->styleOld, lpss->styleNew);
11020 /* don't forward LVS_OWNERDATA only if not already set to */
11021 if ((lpss->styleNew ^ lpss->styleOld) & LVS_OWNERDATA)
11023 if (lpss->styleOld & LVS_OWNERDATA)
11024 lpss->styleNew |= LVS_OWNERDATA;
11025 else
11026 lpss->styleNew &= ~LVS_OWNERDATA;
11029 return 0;
11032 /***
11033 * DESCRIPTION:
11034 * Processes WM_SHOWWINDOW messages.
11036 * PARAMETER(S):
11037 * [I] infoPtr : valid pointer to the listview structure
11038 * [I] bShown : window is being shown (FALSE when hidden)
11039 * [I] iStatus : window show status
11041 * RETURN:
11042 * Zero
11044 static LRESULT LISTVIEW_ShowWindow(LISTVIEW_INFO *infoPtr, WPARAM bShown, LPARAM iStatus)
11046 /* header delayed creation */
11047 if ((infoPtr->uView == LV_VIEW_DETAILS) && bShown)
11049 LISTVIEW_CreateHeader(infoPtr);
11051 if (!(LVS_NOCOLUMNHEADER & infoPtr->dwStyle))
11052 ShowWindow(infoPtr->hwndHeader, SW_SHOWNORMAL);
11055 return DefWindowProcW(infoPtr->hwndSelf, WM_SHOWWINDOW, bShown, iStatus);
11058 /***
11059 * DESCRIPTION:
11060 * Processes CCM_GETVERSION messages.
11062 * PARAMETER(S):
11063 * [I] infoPtr : valid pointer to the listview structure
11065 * RETURN:
11066 * Current version
11068 static inline LRESULT LISTVIEW_GetVersion(const LISTVIEW_INFO *infoPtr)
11070 return infoPtr->iVersion;
11073 /***
11074 * DESCRIPTION:
11075 * Processes CCM_SETVERSION messages.
11077 * PARAMETER(S):
11078 * [I] infoPtr : valid pointer to the listview structure
11079 * [I] iVersion : version to be set
11081 * RETURN:
11082 * -1 when requested version is greater than DLL version;
11083 * previous version otherwise
11085 static LRESULT LISTVIEW_SetVersion(LISTVIEW_INFO *infoPtr, DWORD iVersion)
11087 INT iOldVersion = infoPtr->iVersion;
11089 if (iVersion > COMCTL32_VERSION)
11090 return -1;
11092 infoPtr->iVersion = iVersion;
11094 TRACE("new version %d\n", iVersion);
11096 return iOldVersion;
11099 /***
11100 * DESCRIPTION:
11101 * Window procedure of the listview control.
11104 static LRESULT WINAPI
11105 LISTVIEW_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
11107 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongPtrW(hwnd, 0);
11109 TRACE("(hwnd=%p uMsg=%x wParam=%lx lParam=%lx)\n", hwnd, uMsg, wParam, lParam);
11111 if (!infoPtr && (uMsg != WM_NCCREATE))
11112 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
11114 switch (uMsg)
11116 case LVM_APPROXIMATEVIEWRECT:
11117 return LISTVIEW_ApproximateViewRect(infoPtr, (INT)wParam,
11118 LOWORD(lParam), HIWORD(lParam));
11119 case LVM_ARRANGE:
11120 return LISTVIEW_Arrange(infoPtr, (INT)wParam);
11122 case LVM_CANCELEDITLABEL:
11123 return LISTVIEW_CancelEditLabel(infoPtr);
11125 case LVM_CREATEDRAGIMAGE:
11126 return (LRESULT)LISTVIEW_CreateDragImage(infoPtr, (INT)wParam, (LPPOINT)lParam);
11128 case LVM_DELETEALLITEMS:
11129 return LISTVIEW_DeleteAllItems(infoPtr, FALSE);
11131 case LVM_DELETECOLUMN:
11132 return LISTVIEW_DeleteColumn(infoPtr, (INT)wParam);
11134 case LVM_DELETEITEM:
11135 return LISTVIEW_DeleteItem(infoPtr, (INT)wParam);
11137 case LVM_EDITLABELA:
11138 case LVM_EDITLABELW:
11139 return (LRESULT)LISTVIEW_EditLabelT(infoPtr, (INT)wParam,
11140 uMsg == LVM_EDITLABELW);
11141 /* case LVM_ENABLEGROUPVIEW: */
11143 case LVM_ENSUREVISIBLE:
11144 return LISTVIEW_EnsureVisible(infoPtr, (INT)wParam, (BOOL)lParam);
11146 case LVM_FINDITEMW:
11147 return LISTVIEW_FindItemW(infoPtr, (INT)wParam, (LPLVFINDINFOW)lParam);
11149 case LVM_FINDITEMA:
11150 return LISTVIEW_FindItemA(infoPtr, (INT)wParam, (LPLVFINDINFOA)lParam);
11152 case LVM_GETBKCOLOR:
11153 return infoPtr->clrBk;
11155 /* case LVM_GETBKIMAGE: */
11157 case LVM_GETCALLBACKMASK:
11158 return infoPtr->uCallbackMask;
11160 case LVM_GETCOLUMNA:
11161 case LVM_GETCOLUMNW:
11162 return LISTVIEW_GetColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam,
11163 uMsg == LVM_GETCOLUMNW);
11165 case LVM_GETCOLUMNORDERARRAY:
11166 return LISTVIEW_GetColumnOrderArray(infoPtr, (INT)wParam, (LPINT)lParam);
11168 case LVM_GETCOLUMNWIDTH:
11169 return LISTVIEW_GetColumnWidth(infoPtr, (INT)wParam);
11171 case LVM_GETCOUNTPERPAGE:
11172 return LISTVIEW_GetCountPerPage(infoPtr);
11174 case LVM_GETEDITCONTROL:
11175 return (LRESULT)infoPtr->hwndEdit;
11177 case LVM_GETEXTENDEDLISTVIEWSTYLE:
11178 return infoPtr->dwLvExStyle;
11180 /* case LVM_GETGROUPINFO: */
11182 /* case LVM_GETGROUPMETRICS: */
11184 case LVM_GETHEADER:
11185 return (LRESULT)infoPtr->hwndHeader;
11187 case LVM_GETHOTCURSOR:
11188 return (LRESULT)infoPtr->hHotCursor;
11190 case LVM_GETHOTITEM:
11191 return infoPtr->nHotItem;
11193 case LVM_GETHOVERTIME:
11194 return infoPtr->dwHoverTime;
11196 case LVM_GETIMAGELIST:
11197 return (LRESULT)LISTVIEW_GetImageList(infoPtr, (INT)wParam);
11199 /* case LVM_GETINSERTMARK: */
11201 /* case LVM_GETINSERTMARKCOLOR: */
11203 /* case LVM_GETINSERTMARKRECT: */
11205 case LVM_GETISEARCHSTRINGA:
11206 case LVM_GETISEARCHSTRINGW:
11207 FIXME("LVM_GETISEARCHSTRING: unimplemented\n");
11208 return FALSE;
11210 case LVM_GETITEMA:
11211 case LVM_GETITEMW:
11212 return LISTVIEW_GetItemExtT(infoPtr, (LPLVITEMW)lParam, uMsg == LVM_GETITEMW);
11214 case LVM_GETITEMCOUNT:
11215 return infoPtr->nItemCount;
11217 case LVM_GETITEMPOSITION:
11218 return LISTVIEW_GetItemPosition(infoPtr, (INT)wParam, (LPPOINT)lParam);
11220 case LVM_GETITEMRECT:
11221 return LISTVIEW_GetItemRect(infoPtr, (INT)wParam, (LPRECT)lParam);
11223 case LVM_GETITEMSPACING:
11224 return LISTVIEW_GetItemSpacing(infoPtr, (BOOL)wParam);
11226 case LVM_GETITEMSTATE:
11227 return LISTVIEW_GetItemState(infoPtr, (INT)wParam, (UINT)lParam);
11229 case LVM_GETITEMTEXTA:
11230 case LVM_GETITEMTEXTW:
11231 return LISTVIEW_GetItemTextT(infoPtr, (INT)wParam, (LPLVITEMW)lParam,
11232 uMsg == LVM_GETITEMTEXTW);
11234 case LVM_GETNEXTITEM:
11235 return LISTVIEW_GetNextItem(infoPtr, (INT)wParam, LOWORD(lParam));
11237 case LVM_GETNUMBEROFWORKAREAS:
11238 FIXME("LVM_GETNUMBEROFWORKAREAS: unimplemented\n");
11239 return 1;
11241 case LVM_GETORIGIN:
11242 if (!lParam) return FALSE;
11243 if (infoPtr->uView == LV_VIEW_DETAILS ||
11244 infoPtr->uView == LV_VIEW_LIST) return FALSE;
11245 LISTVIEW_GetOrigin(infoPtr, (LPPOINT)lParam);
11246 return TRUE;
11248 /* case LVM_GETOUTLINECOLOR: */
11250 /* case LVM_GETSELECTEDCOLUMN: */
11252 case LVM_GETSELECTEDCOUNT:
11253 return LISTVIEW_GetSelectedCount(infoPtr);
11255 case LVM_GETSELECTIONMARK:
11256 return infoPtr->nSelectionMark;
11258 case LVM_GETSTRINGWIDTHA:
11259 case LVM_GETSTRINGWIDTHW:
11260 return LISTVIEW_GetStringWidthT(infoPtr, (LPCWSTR)lParam,
11261 uMsg == LVM_GETSTRINGWIDTHW);
11263 case LVM_GETSUBITEMRECT:
11264 return LISTVIEW_GetSubItemRect(infoPtr, (UINT)wParam, (LPRECT)lParam);
11266 case LVM_GETTEXTBKCOLOR:
11267 return infoPtr->clrTextBk;
11269 case LVM_GETTEXTCOLOR:
11270 return infoPtr->clrText;
11272 /* case LVM_GETTILEINFO: */
11274 /* case LVM_GETTILEVIEWINFO: */
11276 case LVM_GETTOOLTIPS:
11277 if( !infoPtr->hwndToolTip )
11278 infoPtr->hwndToolTip = COMCTL32_CreateToolTip( hwnd );
11279 return (LRESULT)infoPtr->hwndToolTip;
11281 case LVM_GETTOPINDEX:
11282 return LISTVIEW_GetTopIndex(infoPtr);
11284 case LVM_GETUNICODEFORMAT:
11285 return (infoPtr->notifyFormat == NFR_UNICODE);
11287 case LVM_GETVIEW:
11288 return infoPtr->uView;
11290 case LVM_GETVIEWRECT:
11291 return LISTVIEW_GetViewRect(infoPtr, (LPRECT)lParam);
11293 case LVM_GETWORKAREAS:
11294 FIXME("LVM_GETWORKAREAS: unimplemented\n");
11295 return FALSE;
11297 /* case LVM_HASGROUP: */
11299 case LVM_HITTEST:
11300 return LISTVIEW_HitTest(infoPtr, (LPLVHITTESTINFO)lParam, FALSE, TRUE);
11302 case LVM_INSERTCOLUMNA:
11303 case LVM_INSERTCOLUMNW:
11304 return LISTVIEW_InsertColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam,
11305 uMsg == LVM_INSERTCOLUMNW);
11307 /* case LVM_INSERTGROUP: */
11309 /* case LVM_INSERTGROUPSORTED: */
11311 case LVM_INSERTITEMA:
11312 case LVM_INSERTITEMW:
11313 return LISTVIEW_InsertItemT(infoPtr, (LPLVITEMW)lParam, uMsg == LVM_INSERTITEMW);
11315 /* case LVM_INSERTMARKHITTEST: */
11317 /* case LVM_ISGROUPVIEWENABLED: */
11319 case LVM_ISITEMVISIBLE:
11320 return LISTVIEW_IsItemVisible(infoPtr, (INT)wParam);
11322 case LVM_MAPIDTOINDEX:
11323 return LISTVIEW_MapIdToIndex(infoPtr, (UINT)wParam);
11325 case LVM_MAPINDEXTOID:
11326 return LISTVIEW_MapIndexToId(infoPtr, (INT)wParam);
11328 /* case LVM_MOVEGROUP: */
11330 /* case LVM_MOVEITEMTOGROUP: */
11332 case LVM_REDRAWITEMS:
11333 return LISTVIEW_RedrawItems(infoPtr, (INT)wParam, (INT)lParam);
11335 /* case LVM_REMOVEALLGROUPS: */
11337 /* case LVM_REMOVEGROUP: */
11339 case LVM_SCROLL:
11340 return LISTVIEW_Scroll(infoPtr, (INT)wParam, (INT)lParam);
11342 case LVM_SETBKCOLOR:
11343 return LISTVIEW_SetBkColor(infoPtr, (COLORREF)lParam);
11345 /* case LVM_SETBKIMAGE: */
11347 case LVM_SETCALLBACKMASK:
11348 infoPtr->uCallbackMask = (UINT)wParam;
11349 return TRUE;
11351 case LVM_SETCOLUMNA:
11352 case LVM_SETCOLUMNW:
11353 return LISTVIEW_SetColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam,
11354 uMsg == LVM_SETCOLUMNW);
11356 case LVM_SETCOLUMNORDERARRAY:
11357 return LISTVIEW_SetColumnOrderArray(infoPtr, (INT)wParam, (LPINT)lParam);
11359 case LVM_SETCOLUMNWIDTH:
11360 return LISTVIEW_SetColumnWidth(infoPtr, (INT)wParam, (short)LOWORD(lParam));
11362 case LVM_SETEXTENDEDLISTVIEWSTYLE:
11363 return LISTVIEW_SetExtendedListViewStyle(infoPtr, (DWORD)wParam, (DWORD)lParam);
11365 /* case LVM_SETGROUPINFO: */
11367 /* case LVM_SETGROUPMETRICS: */
11369 case LVM_SETHOTCURSOR:
11370 return (LRESULT)LISTVIEW_SetHotCursor(infoPtr, (HCURSOR)lParam);
11372 case LVM_SETHOTITEM:
11373 return LISTVIEW_SetHotItem(infoPtr, (INT)wParam);
11375 case LVM_SETHOVERTIME:
11376 return LISTVIEW_SetHoverTime(infoPtr, (DWORD)lParam);
11378 case LVM_SETICONSPACING:
11379 return LISTVIEW_SetIconSpacing(infoPtr, (short)LOWORD(lParam), (short)HIWORD(lParam));
11381 case LVM_SETIMAGELIST:
11382 return (LRESULT)LISTVIEW_SetImageList(infoPtr, (INT)wParam, (HIMAGELIST)lParam);
11384 /* case LVM_SETINFOTIP: */
11386 /* case LVM_SETINSERTMARK: */
11388 /* case LVM_SETINSERTMARKCOLOR: */
11390 case LVM_SETITEMA:
11391 case LVM_SETITEMW:
11393 if (infoPtr->dwStyle & LVS_OWNERDATA) return FALSE;
11394 return LISTVIEW_SetItemT(infoPtr, (LPLVITEMW)lParam, (uMsg == LVM_SETITEMW));
11397 case LVM_SETITEMCOUNT:
11398 return LISTVIEW_SetItemCount(infoPtr, (INT)wParam, (DWORD)lParam);
11400 case LVM_SETITEMPOSITION:
11402 POINT pt;
11403 pt.x = (short)LOWORD(lParam);
11404 pt.y = (short)HIWORD(lParam);
11405 return LISTVIEW_SetItemPosition(infoPtr, (INT)wParam, &pt);
11408 case LVM_SETITEMPOSITION32:
11409 return LISTVIEW_SetItemPosition(infoPtr, (INT)wParam, (POINT*)lParam);
11411 case LVM_SETITEMSTATE:
11412 if (lParam == 0) return FALSE;
11413 return LISTVIEW_SetItemState(infoPtr, (INT)wParam, (LPLVITEMW)lParam);
11415 case LVM_SETITEMTEXTA:
11416 case LVM_SETITEMTEXTW:
11417 return LISTVIEW_SetItemTextT(infoPtr, (INT)wParam, (LPLVITEMW)lParam,
11418 uMsg == LVM_SETITEMTEXTW);
11420 /* case LVM_SETOUTLINECOLOR: */
11422 /* case LVM_SETSELECTEDCOLUMN: */
11424 case LVM_SETSELECTIONMARK:
11425 return LISTVIEW_SetSelectionMark(infoPtr, (INT)lParam);
11427 case LVM_SETTEXTBKCOLOR:
11428 return LISTVIEW_SetTextBkColor(infoPtr, (COLORREF)lParam);
11430 case LVM_SETTEXTCOLOR:
11431 return LISTVIEW_SetTextColor(infoPtr, (COLORREF)lParam);
11433 /* case LVM_SETTILEINFO: */
11435 /* case LVM_SETTILEVIEWINFO: */
11437 /* case LVM_SETTILEWIDTH: */
11439 case LVM_SETTOOLTIPS:
11440 return (LRESULT)LISTVIEW_SetToolTips(infoPtr, (HWND)lParam);
11442 case LVM_SETUNICODEFORMAT:
11443 return LISTVIEW_SetUnicodeFormat(infoPtr, wParam);
11445 case LVM_SETVIEW:
11446 return LISTVIEW_SetView(infoPtr, wParam);
11448 /* case LVM_SETWORKAREAS: */
11450 /* case LVM_SORTGROUPS: */
11452 case LVM_SORTITEMS:
11453 case LVM_SORTITEMSEX:
11454 return LISTVIEW_SortItems(infoPtr, (PFNLVCOMPARE)lParam, wParam,
11455 uMsg == LVM_SORTITEMSEX);
11456 case LVM_SUBITEMHITTEST:
11457 return LISTVIEW_HitTest(infoPtr, (LPLVHITTESTINFO)lParam, TRUE, FALSE);
11459 case LVM_UPDATE:
11460 return LISTVIEW_Update(infoPtr, (INT)wParam);
11462 case CCM_GETVERSION:
11463 return LISTVIEW_GetVersion(infoPtr);
11465 case CCM_SETVERSION:
11466 return LISTVIEW_SetVersion(infoPtr, wParam);
11468 case WM_CHAR:
11469 return LISTVIEW_ProcessLetterKeys( infoPtr, wParam, lParam );
11471 case WM_COMMAND:
11472 return LISTVIEW_Command(infoPtr, wParam, lParam);
11474 case WM_NCCREATE:
11475 return LISTVIEW_NCCreate(hwnd, (LPCREATESTRUCTW)lParam);
11477 case WM_CREATE:
11478 return LISTVIEW_Create(hwnd, (LPCREATESTRUCTW)lParam);
11480 case WM_DESTROY:
11481 return LISTVIEW_Destroy(infoPtr);
11483 case WM_ENABLE:
11484 return LISTVIEW_Enable(infoPtr);
11486 case WM_ERASEBKGND:
11487 return LISTVIEW_EraseBkgnd(infoPtr, (HDC)wParam);
11489 case WM_GETDLGCODE:
11490 return DLGC_WANTCHARS | DLGC_WANTARROWS;
11492 case WM_GETFONT:
11493 return (LRESULT)infoPtr->hFont;
11495 case WM_HSCROLL:
11496 return LISTVIEW_HScroll(infoPtr, (INT)LOWORD(wParam), 0);
11498 case WM_KEYDOWN:
11499 return LISTVIEW_KeyDown(infoPtr, (INT)wParam, (LONG)lParam);
11501 case WM_KILLFOCUS:
11502 return LISTVIEW_KillFocus(infoPtr);
11504 case WM_LBUTTONDBLCLK:
11505 return LISTVIEW_LButtonDblClk(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11507 case WM_LBUTTONDOWN:
11508 return LISTVIEW_LButtonDown(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11510 case WM_LBUTTONUP:
11511 return LISTVIEW_LButtonUp(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11513 case WM_MOUSEMOVE:
11514 return LISTVIEW_MouseMove (infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11516 case WM_MOUSEHOVER:
11517 return LISTVIEW_MouseHover(infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11519 case WM_NCDESTROY:
11520 return LISTVIEW_NCDestroy(infoPtr);
11522 case WM_NCPAINT:
11523 return LISTVIEW_NCPaint(infoPtr, (HRGN)wParam);
11525 case WM_NOTIFY:
11526 return LISTVIEW_Notify(infoPtr, (LPNMHDR)lParam);
11528 case WM_NOTIFYFORMAT:
11529 return LISTVIEW_NotifyFormat(infoPtr, (HWND)wParam, (INT)lParam);
11531 case WM_PRINTCLIENT:
11532 return LISTVIEW_PrintClient(infoPtr, (HDC)wParam, (DWORD)lParam);
11534 case WM_PAINT:
11535 return LISTVIEW_WMPaint(infoPtr, (HDC)wParam);
11537 case WM_RBUTTONDBLCLK:
11538 return LISTVIEW_RButtonDblClk(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11540 case WM_RBUTTONDOWN:
11541 return LISTVIEW_RButtonDown(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11543 case WM_RBUTTONUP:
11544 return LISTVIEW_RButtonUp(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11546 case WM_SETCURSOR:
11547 return LISTVIEW_SetCursor(infoPtr, wParam, lParam);
11549 case WM_SETFOCUS:
11550 return LISTVIEW_SetFocus(infoPtr, (HWND)wParam);
11552 case WM_SETFONT:
11553 return LISTVIEW_SetFont(infoPtr, (HFONT)wParam, (WORD)lParam);
11555 case WM_SETREDRAW:
11556 return LISTVIEW_SetRedraw(infoPtr, (BOOL)wParam);
11558 case WM_SHOWWINDOW:
11559 return LISTVIEW_ShowWindow(infoPtr, wParam, lParam);
11561 case WM_SIZE:
11562 return LISTVIEW_Size(infoPtr, (short)LOWORD(lParam), (short)HIWORD(lParam));
11564 case WM_STYLECHANGED:
11565 return LISTVIEW_StyleChanged(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
11567 case WM_STYLECHANGING:
11568 return LISTVIEW_StyleChanging(wParam, (LPSTYLESTRUCT)lParam);
11570 case WM_SYSCOLORCHANGE:
11571 COMCTL32_RefreshSysColors();
11572 return 0;
11574 /* case WM_TIMER: */
11575 case WM_THEMECHANGED:
11576 return LISTVIEW_ThemeChanged(infoPtr);
11578 case WM_VSCROLL:
11579 return LISTVIEW_VScroll(infoPtr, (INT)LOWORD(wParam), 0);
11581 case WM_MOUSEWHEEL:
11582 if (wParam & (MK_SHIFT | MK_CONTROL))
11583 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
11584 return LISTVIEW_MouseWheel(infoPtr, (short int)HIWORD(wParam));
11586 case WM_WINDOWPOSCHANGED:
11587 if (!(((WINDOWPOS *)lParam)->flags & SWP_NOSIZE))
11589 SetWindowPos(infoPtr->hwndSelf, 0, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOACTIVATE |
11590 SWP_NOZORDER | SWP_NOMOVE | SWP_NOSIZE);
11592 if ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (infoPtr->uView == LV_VIEW_DETAILS))
11594 if (notify_measureitem(infoPtr)) LISTVIEW_InvalidateList(infoPtr);
11597 LISTVIEW_UpdateSize(infoPtr);
11598 LISTVIEW_UpdateScroll(infoPtr);
11600 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
11602 /* case WM_WININICHANGE: */
11604 default:
11605 if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
11606 ERR("unknown msg %04x wp=%08lx lp=%08lx\n", uMsg, wParam, lParam);
11608 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
11613 /***
11614 * DESCRIPTION:
11615 * Registers the window class.
11617 * PARAMETER(S):
11618 * None
11620 * RETURN:
11621 * None
11623 void LISTVIEW_Register(void)
11625 WNDCLASSW wndClass;
11627 ZeroMemory(&wndClass, sizeof(WNDCLASSW));
11628 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS;
11629 wndClass.lpfnWndProc = LISTVIEW_WindowProc;
11630 wndClass.cbClsExtra = 0;
11631 wndClass.cbWndExtra = sizeof(LISTVIEW_INFO *);
11632 wndClass.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
11633 wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
11634 wndClass.lpszClassName = WC_LISTVIEWW;
11635 RegisterClassW(&wndClass);
11638 /***
11639 * DESCRIPTION:
11640 * Unregisters the window class.
11642 * PARAMETER(S):
11643 * None
11645 * RETURN:
11646 * None
11648 void LISTVIEW_Unregister(void)
11650 UnregisterClassW(WC_LISTVIEWW, NULL);
11653 /***
11654 * DESCRIPTION:
11655 * Handle any WM_COMMAND messages
11657 * PARAMETER(S):
11658 * [I] infoPtr : valid pointer to the listview structure
11659 * [I] wParam : the first message parameter
11660 * [I] lParam : the second message parameter
11662 * RETURN:
11663 * Zero.
11665 static LRESULT LISTVIEW_Command(LISTVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
11668 TRACE("(%p %x %x %lx)\n", infoPtr, HIWORD(wParam), LOWORD(wParam), lParam);
11670 if (!infoPtr->hwndEdit) return 0;
11672 switch (HIWORD(wParam))
11674 case EN_UPDATE:
11677 * Adjust the edit window size
11679 WCHAR buffer[1024];
11680 HDC hdc = GetDC(infoPtr->hwndEdit);
11681 HFONT hFont, hOldFont = 0;
11682 RECT rect;
11683 SIZE sz;
11685 if (!infoPtr->hwndEdit || !hdc) return 0;
11686 GetWindowTextW(infoPtr->hwndEdit, buffer, sizeof(buffer)/sizeof(buffer[0]));
11687 GetWindowRect(infoPtr->hwndEdit, &rect);
11689 /* Select font to get the right dimension of the string */
11690 hFont = (HFONT)SendMessageW(infoPtr->hwndEdit, WM_GETFONT, 0, 0);
11691 if (hFont)
11693 hOldFont = SelectObject(hdc, hFont);
11696 if (GetTextExtentPoint32W(hdc, buffer, lstrlenW(buffer), &sz))
11698 TEXTMETRICW textMetric;
11700 /* Add Extra spacing for the next character */
11701 GetTextMetricsW(hdc, &textMetric);
11702 sz.cx += (textMetric.tmMaxCharWidth * 2);
11704 SetWindowPos(infoPtr->hwndEdit, NULL, 0, 0, sz.cx,
11705 rect.bottom - rect.top, SWP_DRAWFRAME | SWP_NOMOVE | SWP_NOZORDER);
11707 if (hFont)
11708 SelectObject(hdc, hOldFont);
11710 ReleaseDC(infoPtr->hwndEdit, hdc);
11712 break;
11714 case EN_KILLFOCUS:
11716 LISTVIEW_CancelEditLabel(infoPtr);
11717 break;
11720 default:
11721 return SendMessageW (infoPtr->hwndNotify, WM_COMMAND, wParam, lParam);
11724 return 0;