TESTING -- override pthreads to fix gstreamer v5
[wine/multimedia.git] / dlls / comctl32 / listview.c
blob68a5515f1a8ca107ef1a0254fa26ca2f68320a79
1 /*
2 * Listview control
4 * Copyright 1998, 1999 Eric Kohl
5 * Copyright 1999 Luc Tourangeau
6 * Copyright 2000 Jason Mawdsley
7 * Copyright 2001 CodeWeavers Inc.
8 * Copyright 2002 Dimitrie O. Paun
9 * Copyright 2009-2015 Nikolay Sivov
10 * Copyright 2009 Owen Rudge for CodeWeavers
11 * Copyright 2012-2013 Daniel Jelinski
13 * This library is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU Lesser General Public
15 * License as published by the Free Software Foundation; either
16 * version 2.1 of the License, or (at your option) any later version.
18 * This library is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * Lesser General Public License for more details.
23 * You should have received a copy of the GNU Lesser General Public
24 * License along with this library; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27 * NOTES
29 * This code was audited for completeness against the documented features
30 * of Comctl32.dll version 6.0 on May. 20, 2005, by James Hawkins.
32 * Unless otherwise noted, we believe this code to be complete, as per
33 * the specification mentioned above.
34 * If you discover missing features, or bugs, please note them below.
36 * TODO:
38 * Default Message Processing
39 * -- WM_CREATE: create the icon and small icon image lists at this point only if
40 * the LVS_SHAREIMAGELISTS style is not specified.
41 * -- WM_WINDOWPOSCHANGED: arrange the list items if the current view is icon
42 * or small icon and the LVS_AUTOARRANGE style is specified.
43 * -- WM_TIMER
44 * -- WM_WININICHANGE
46 * Features
47 * -- Hot item handling, mouse hovering
48 * -- Workareas support
49 * -- Tilemode support
50 * -- Groups support
52 * Bugs
53 * -- Expand large item in ICON mode when the cursor is flying over the icon or text.
54 * -- Support CustomDraw options for _WIN32_IE >= 0x560 (see NMLVCUSTOMDRAW docs).
55 * -- LVA_SNAPTOGRID not implemented
56 * -- LISTVIEW_ApproximateViewRect partially implemented
57 * -- LISTVIEW_StyleChanged doesn't handle some changes too well
59 * Speedups
60 * -- LISTVIEW_GetNextItem needs to be rewritten. It is currently
61 * linear in the number of items in the list, and this is
62 * unacceptable for large lists.
63 * -- if list is sorted by item text LISTVIEW_InsertItemT could use
64 * binary search to calculate item index (e.g. DPA_Search()).
65 * This requires sorted state to be reliably tracked in item modifiers.
66 * -- we should keep an ordered array of coordinates in iconic mode.
67 * This would allow framing items (iterator_frameditems),
68 * and finding the nearest item (LVFI_NEARESTXY) a lot more efficiently.
70 * Flags
71 * -- LVIF_COLUMNS
72 * -- LVIF_GROUPID
74 * States
75 * -- LVIS_ACTIVATING (not currently supported by comctl32.dll version 6.0)
76 * -- LVIS_DROPHILITED
78 * Styles
79 * -- LVS_NOLABELWRAP
80 * -- LVS_NOSCROLL (see Q137520)
81 * -- LVS_ALIGNTOP
83 * Extended Styles
84 * -- LVS_EX_BORDERSELECT
85 * -- LVS_EX_FLATSB
86 * -- LVS_EX_INFOTIP
87 * -- LVS_EX_LABELTIP
88 * -- LVS_EX_MULTIWORKAREAS
89 * -- LVS_EX_REGIONAL
90 * -- LVS_EX_SIMPLESELECT
91 * -- LVS_EX_TWOCLICKACTIVATE
92 * -- LVS_EX_UNDERLINECOLD
93 * -- LVS_EX_UNDERLINEHOT
95 * Notifications:
96 * -- LVN_BEGINSCROLL, LVN_ENDSCROLL
97 * -- LVN_GETINFOTIP
98 * -- LVN_HOTTRACK
99 * -- LVN_SETDISPINFO
101 * Messages:
102 * -- LVM_ENABLEGROUPVIEW
103 * -- LVM_GETBKIMAGE, LVM_SETBKIMAGE
104 * -- LVM_GETGROUPINFO, LVM_SETGROUPINFO
105 * -- LVM_GETGROUPMETRICS, LVM_SETGROUPMETRICS
106 * -- LVM_GETINSERTMARK, LVM_SETINSERTMARK
107 * -- LVM_GETINSERTMARKCOLOR, LVM_SETINSERTMARKCOLOR
108 * -- LVM_GETINSERTMARKRECT
109 * -- LVM_GETNUMBEROFWORKAREAS
110 * -- LVM_GETOUTLINECOLOR, LVM_SETOUTLINECOLOR
111 * -- LVM_GETSELECTEDCOLUMN, LVM_SETSELECTEDCOLUMN
112 * -- LVM_GETISEARCHSTRINGW, LVM_GETISEARCHSTRINGA
113 * -- LVM_GETTILEINFO, LVM_SETTILEINFO
114 * -- LVM_GETTILEVIEWINFO, LVM_SETTILEVIEWINFO
115 * -- LVM_GETWORKAREAS, LVM_SETWORKAREAS
116 * -- LVM_HASGROUP, LVM_INSERTGROUP, LVM_REMOVEGROUP, LVM_REMOVEALLGROUPS
117 * -- LVM_INSERTGROUPSORTED
118 * -- LVM_INSERTMARKHITTEST
119 * -- LVM_ISGROUPVIEWENABLED
120 * -- LVM_MOVEGROUP
121 * -- LVM_MOVEITEMTOGROUP
122 * -- LVM_SETINFOTIP
123 * -- LVM_SETTILEWIDTH
124 * -- LVM_SORTGROUPS
126 * Macros:
127 * -- ListView_GetHoverTime, ListView_SetHoverTime
128 * -- ListView_GetISearchString
129 * -- ListView_GetNumberOfWorkAreas
130 * -- ListView_GetWorkAreas, ListView_SetWorkAreas
132 * Functions:
133 * -- LVGroupComparE
136 #include "config.h"
137 #include "wine/port.h"
139 #include <assert.h>
140 #include <ctype.h>
141 #include <string.h>
142 #include <stdlib.h>
143 #include <stdarg.h>
144 #include <stdio.h>
146 #include "windef.h"
147 #include "winbase.h"
148 #include "winnt.h"
149 #include "wingdi.h"
150 #include "winuser.h"
151 #include "winnls.h"
152 #include "commctrl.h"
153 #include "comctl32.h"
154 #include "uxtheme.h"
156 #include "wine/debug.h"
157 #include "wine/unicode.h"
159 WINE_DEFAULT_DEBUG_CHANNEL(listview);
161 typedef struct tagCOLUMN_INFO
163 RECT rcHeader; /* tracks the header's rectangle */
164 INT fmt; /* same as LVCOLUMN.fmt */
165 INT cxMin;
166 } COLUMN_INFO;
168 typedef struct tagITEMHDR
170 LPWSTR pszText;
171 INT iImage;
172 } ITEMHDR, *LPITEMHDR;
174 typedef struct tagSUBITEM_INFO
176 ITEMHDR hdr;
177 INT iSubItem;
178 } SUBITEM_INFO;
180 typedef struct tagITEM_ID ITEM_ID;
182 typedef struct tagITEM_INFO
184 ITEMHDR hdr;
185 UINT state;
186 LPARAM lParam;
187 INT iIndent;
188 ITEM_ID *id;
189 } ITEM_INFO;
191 struct tagITEM_ID
193 UINT id; /* item id */
194 HDPA item; /* link to item data */
197 typedef struct tagRANGE
199 INT lower;
200 INT upper;
201 } RANGE;
203 typedef struct tagRANGES
205 HDPA hdpa;
206 } *RANGES;
208 typedef struct tagITERATOR
210 INT nItem;
211 INT nSpecial;
212 RANGE range;
213 RANGES ranges;
214 INT index;
215 } ITERATOR;
217 typedef struct tagDELAYED_ITEM_EDIT
219 BOOL fEnabled;
220 INT iItem;
221 } DELAYED_ITEM_EDIT;
223 typedef struct tagLISTVIEW_INFO
225 /* control window */
226 HWND hwndSelf;
227 RECT rcList; /* This rectangle is really the window
228 * client rectangle possibly reduced by the
229 * horizontal scroll bar and/or header - see
230 * LISTVIEW_UpdateSize. This rectangle offset
231 * by the LISTVIEW_GetOrigin value is in
232 * client coordinates */
234 /* notification window */
235 SHORT notifyFormat;
236 HWND hwndNotify;
237 BOOL bDoChangeNotify; /* send change notification messages? */
238 UINT uCallbackMask;
240 /* tooltips */
241 HWND hwndToolTip;
243 /* items */
244 INT nItemCount; /* the number of items in the list */
245 HDPA hdpaItems; /* array ITEM_INFO pointers */
246 HDPA hdpaItemIds; /* array of ITEM_ID pointers */
247 HDPA hdpaPosX; /* maintains the (X, Y) coordinates of the */
248 HDPA hdpaPosY; /* items in LVS_ICON, and LVS_SMALLICON modes */
249 RANGES selectionRanges;
250 INT nSelectionMark; /* item to start next multiselection from */
251 INT nHotItem;
252 BOOL bAutoarrange; /* Autoarrange flag when NOT in LVS_AUTOARRANGE */
254 /* columns */
255 HDPA hdpaColumns; /* array of COLUMN_INFO pointers */
256 BOOL colRectsDirty; /* trigger column rectangles requery from header */
258 /* item metrics */
259 BOOL bNoItemMetrics; /* flags if item metrics are not yet computed */
260 INT nItemHeight;
261 INT nItemWidth;
263 /* sorting */
264 PFNLVCOMPARE pfnCompare; /* sorting callback pointer */
265 LPARAM lParamSort;
267 /* style */
268 DWORD dwStyle; /* the cached window GWL_STYLE */
269 DWORD dwLvExStyle; /* extended listview style */
270 DWORD uView; /* current view available through LVM_[G,S]ETVIEW */
272 /* edit item */
273 HWND hwndEdit;
274 WNDPROC EditWndProc;
275 INT nEditLabelItem;
276 DELAYED_ITEM_EDIT itemEdit; /* Pointer to this structure will be the timer ID */
278 /* icons */
279 HIMAGELIST himlNormal;
280 HIMAGELIST himlSmall;
281 HIMAGELIST himlState;
282 SIZE iconSize;
283 BOOL autoSpacing;
284 SIZE iconSpacing;
285 SIZE iconStateSize;
286 POINT currIconPos; /* this is the position next icon will be placed */
288 /* header */
289 HWND hwndHeader;
290 INT xTrackLine; /* The x coefficient of the track line or -1 if none */
292 /* marquee selection */
293 BOOL bMarqueeSelect; /* marquee selection/highlight underway */
294 BOOL bScrolling;
295 RECT marqueeRect; /* absolute coordinates of marquee selection */
296 RECT marqueeDrawRect; /* relative coordinates for drawing marquee */
297 POINT marqueeOrigin; /* absolute coordinates of marquee click origin */
299 /* focus drawing */
300 BOOL bFocus; /* control has focus */
301 INT nFocusedItem;
302 RECT rcFocus; /* focus bounds */
304 /* colors */
305 HBRUSH hBkBrush;
306 COLORREF clrBk;
307 COLORREF clrText;
308 COLORREF clrTextBk;
310 /* font */
311 HFONT hDefaultFont;
312 HFONT hFont;
313 INT ntmHeight; /* Some cached metrics of the font used */
314 INT ntmMaxCharWidth; /* by the listview to draw items */
315 INT nEllipsisWidth;
317 /* mouse operation */
318 BOOL bLButtonDown;
319 BOOL bDragging;
320 POINT ptClickPos; /* point where the user clicked */
321 INT nLButtonDownItem; /* tracks item to reset multiselection on WM_LBUTTONUP */
322 DWORD dwHoverTime;
323 HCURSOR hHotCursor;
324 INT cWheelRemainder;
326 /* keyboard operation */
327 DWORD lastKeyPressTimestamp;
328 WPARAM charCode;
329 INT nSearchParamLength;
330 WCHAR szSearchParam[ MAX_PATH ];
332 /* painting */
333 BOOL bIsDrawing; /* Drawing in progress */
334 INT nMeasureItemHeight; /* WM_MEASUREITEM result */
335 BOOL bRedraw; /* WM_SETREDRAW switch */
337 /* misc */
338 DWORD iVersion; /* CCM_[G,S]ETVERSION */
339 } LISTVIEW_INFO;
342 * constants
344 /* How many we debug buffer to allocate */
345 #define DEBUG_BUFFERS 20
346 /* The size of a single debug buffer */
347 #define DEBUG_BUFFER_SIZE 256
349 /* Internal interface to LISTVIEW_HScroll and LISTVIEW_VScroll */
350 #define SB_INTERNAL -1
352 /* maximum size of a label */
353 #define DISP_TEXT_SIZE 260
355 /* padding for items in list and small icon display modes */
356 #define WIDTH_PADDING 12
358 /* padding for items in list, report and small icon display modes */
359 #define HEIGHT_PADDING 1
361 /* offset of items in report display mode */
362 #define REPORT_MARGINX 2
364 /* padding for icon in large icon display mode
365 * ICON_TOP_PADDING_NOTHITABLE - space between top of box and area
366 * that HITTEST will see.
367 * ICON_TOP_PADDING_HITABLE - spacing between above and icon.
368 * ICON_TOP_PADDING - sum of the two above.
369 * ICON_BOTTOM_PADDING - between bottom of icon and top of text
370 * LABEL_HOR_PADDING - between text and sides of box
371 * LABEL_VERT_PADDING - between bottom of text and end of box
373 * ICON_LR_PADDING - additional width above icon size.
374 * ICON_LR_HALF - half of the above value
376 #define ICON_TOP_PADDING_NOTHITABLE 2
377 #define ICON_TOP_PADDING_HITABLE 2
378 #define ICON_TOP_PADDING (ICON_TOP_PADDING_NOTHITABLE + ICON_TOP_PADDING_HITABLE)
379 #define ICON_BOTTOM_PADDING 4
380 #define LABEL_HOR_PADDING 5
381 #define LABEL_VERT_PADDING 7
382 #define ICON_LR_PADDING 16
383 #define ICON_LR_HALF (ICON_LR_PADDING/2)
385 /* default label width for items in list and small icon display modes */
386 #define DEFAULT_LABEL_WIDTH 40
387 /* maximum select rectangle width for empty text item in LV_VIEW_DETAILS */
388 #define MAX_EMPTYTEXT_SELECT_WIDTH 80
390 /* default column width for items in list display mode */
391 #define DEFAULT_COLUMN_WIDTH 128
393 /* Size of "line" scroll for V & H scrolls */
394 #define LISTVIEW_SCROLL_ICON_LINE_SIZE 37
396 /* Padding between image and label */
397 #define IMAGE_PADDING 2
399 /* Padding behind the label */
400 #define TRAILING_LABEL_PADDING 12
401 #define TRAILING_HEADER_PADDING 11
403 /* Border for the icon caption */
404 #define CAPTION_BORDER 2
406 /* Standard DrawText flags */
407 #define LV_ML_DT_FLAGS (DT_TOP | DT_NOPREFIX | DT_EDITCONTROL | DT_CENTER | DT_WORDBREAK | DT_WORD_ELLIPSIS | DT_END_ELLIPSIS)
408 #define LV_FL_DT_FLAGS (DT_TOP | DT_NOPREFIX | DT_EDITCONTROL | DT_CENTER | DT_WORDBREAK | DT_NOCLIP)
409 #define LV_SL_DT_FLAGS (DT_VCENTER | DT_NOPREFIX | DT_EDITCONTROL | DT_SINGLELINE | DT_WORD_ELLIPSIS | DT_END_ELLIPSIS)
411 /* Image index from state */
412 #define STATEIMAGEINDEX(x) (((x) & LVIS_STATEIMAGEMASK) >> 12)
414 /* The time in milliseconds to reset the search in the list */
415 #define KEY_DELAY 450
417 /* Dump the LISTVIEW_INFO structure to the debug channel */
418 #define LISTVIEW_DUMP(iP) do { \
419 TRACE("hwndSelf=%p, clrBk=0x%06x, clrText=0x%06x, clrTextBk=0x%06x, ItemHeight=%d, ItemWidth=%d, Style=0x%08x\n", \
420 iP->hwndSelf, iP->clrBk, iP->clrText, iP->clrTextBk, \
421 iP->nItemHeight, iP->nItemWidth, iP->dwStyle); \
422 TRACE("hwndSelf=%p, himlNor=%p, himlSml=%p, himlState=%p, Focused=%d, Hot=%d, exStyle=0x%08x, Focus=%d\n", \
423 iP->hwndSelf, iP->himlNormal, iP->himlSmall, iP->himlState, \
424 iP->nFocusedItem, iP->nHotItem, iP->dwLvExStyle, iP->bFocus ); \
425 TRACE("hwndSelf=%p, ntmH=%d, icSz.cx=%d, icSz.cy=%d, icSp.cx=%d, icSp.cy=%d, notifyFmt=%d\n", \
426 iP->hwndSelf, iP->ntmHeight, iP->iconSize.cx, iP->iconSize.cy, \
427 iP->iconSpacing.cx, iP->iconSpacing.cy, iP->notifyFormat); \
428 TRACE("hwndSelf=%p, rcList=%s\n", iP->hwndSelf, wine_dbgstr_rect(&iP->rcList)); \
429 } while(0)
431 static const WCHAR themeClass[] = {'L','i','s','t','V','i','e','w',0};
434 * forward declarations
436 static BOOL LISTVIEW_GetItemT(const LISTVIEW_INFO *, LPLVITEMW, BOOL);
437 static void LISTVIEW_GetItemBox(const LISTVIEW_INFO *, INT, LPRECT);
438 static void LISTVIEW_GetItemOrigin(const LISTVIEW_INFO *, INT, LPPOINT);
439 static BOOL LISTVIEW_GetItemPosition(const LISTVIEW_INFO *, INT, LPPOINT);
440 static BOOL LISTVIEW_GetItemRect(const LISTVIEW_INFO *, INT, LPRECT);
441 static void LISTVIEW_GetOrigin(const LISTVIEW_INFO *, LPPOINT);
442 static BOOL LISTVIEW_GetViewRect(const LISTVIEW_INFO *, LPRECT);
443 static void LISTVIEW_UpdateSize(LISTVIEW_INFO *);
444 static LRESULT LISTVIEW_Command(LISTVIEW_INFO *, WPARAM, LPARAM);
445 static INT LISTVIEW_GetStringWidthT(const LISTVIEW_INFO *, LPCWSTR, BOOL);
446 static BOOL LISTVIEW_KeySelection(LISTVIEW_INFO *, INT, BOOL);
447 static UINT LISTVIEW_GetItemState(const LISTVIEW_INFO *, INT, UINT);
448 static BOOL LISTVIEW_SetItemState(LISTVIEW_INFO *, INT, const LVITEMW *);
449 static LRESULT LISTVIEW_VScroll(LISTVIEW_INFO *, INT, INT);
450 static LRESULT LISTVIEW_HScroll(LISTVIEW_INFO *, INT, INT);
451 static BOOL LISTVIEW_EnsureVisible(LISTVIEW_INFO *, INT, BOOL);
452 static HIMAGELIST LISTVIEW_SetImageList(LISTVIEW_INFO *, INT, HIMAGELIST);
453 static INT LISTVIEW_HitTest(const LISTVIEW_INFO *, LPLVHITTESTINFO, BOOL, BOOL);
454 static BOOL LISTVIEW_EndEditLabelT(LISTVIEW_INFO *, BOOL, BOOL);
455 static BOOL LISTVIEW_Scroll(LISTVIEW_INFO *, INT, INT);
457 /******** Text handling functions *************************************/
459 /* A text pointer is either NULL, LPSTR_TEXTCALLBACK, or points to a
460 * text string. The string may be ANSI or Unicode, in which case
461 * the boolean isW tells us the type of the string.
463 * The name of the function tell what type of strings it expects:
464 * W: Unicode, T: ANSI/Unicode - function of isW
467 static inline BOOL is_text(LPCWSTR text)
469 return text != NULL && text != LPSTR_TEXTCALLBACKW;
472 static inline int textlenT(LPCWSTR text, BOOL isW)
474 return !is_text(text) ? 0 :
475 isW ? lstrlenW(text) : lstrlenA((LPCSTR)text);
478 static inline void textcpynT(LPWSTR dest, BOOL isDestW, LPCWSTR src, BOOL isSrcW, INT max)
480 if (isDestW)
481 if (isSrcW) lstrcpynW(dest, src, max);
482 else MultiByteToWideChar(CP_ACP, 0, (LPCSTR)src, -1, dest, max);
483 else
484 if (isSrcW) WideCharToMultiByte(CP_ACP, 0, src, -1, (LPSTR)dest, max, NULL, NULL);
485 else lstrcpynA((LPSTR)dest, (LPCSTR)src, max);
488 static inline LPWSTR textdupTtoW(LPCWSTR text, BOOL isW)
490 LPWSTR wstr = (LPWSTR)text;
492 if (!isW && is_text(text))
494 INT len = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)text, -1, NULL, 0);
495 wstr = Alloc(len * sizeof(WCHAR));
496 if (wstr) MultiByteToWideChar(CP_ACP, 0, (LPCSTR)text, -1, wstr, len);
498 TRACE(" wstr=%s\n", text == LPSTR_TEXTCALLBACKW ? "(callback)" : debugstr_w(wstr));
499 return wstr;
502 static inline void textfreeT(LPWSTR wstr, BOOL isW)
504 if (!isW && is_text(wstr)) Free (wstr);
508 * dest is a pointer to a Unicode string
509 * src is a pointer to a string (Unicode if isW, ANSI if !isW)
511 static BOOL textsetptrT(LPWSTR *dest, LPCWSTR src, BOOL isW)
513 BOOL bResult = TRUE;
515 if (src == LPSTR_TEXTCALLBACKW)
517 if (is_text(*dest)) Free(*dest);
518 *dest = LPSTR_TEXTCALLBACKW;
520 else
522 LPWSTR pszText = textdupTtoW(src, isW);
523 if (*dest == LPSTR_TEXTCALLBACKW) *dest = NULL;
524 bResult = Str_SetPtrW(dest, pszText);
525 textfreeT(pszText, isW);
527 return bResult;
531 * compares a Unicode to a Unicode/ANSI text string
533 static inline int textcmpWT(LPCWSTR aw, LPCWSTR bt, BOOL isW)
535 if (!aw) return bt ? -1 : 0;
536 if (!bt) return 1;
537 if (aw == LPSTR_TEXTCALLBACKW)
538 return bt == LPSTR_TEXTCALLBACKW ? 1 : -1;
539 if (bt != LPSTR_TEXTCALLBACKW)
541 LPWSTR bw = textdupTtoW(bt, isW);
542 int r = bw ? lstrcmpW(aw, bw) : 1;
543 textfreeT(bw, isW);
544 return r;
547 return 1;
550 static inline int lstrncmpiW(LPCWSTR s1, LPCWSTR s2, int n)
552 n = min(min(n, lstrlenW(s1)), lstrlenW(s2));
553 return CompareStringW(LOCALE_USER_DEFAULT, NORM_IGNORECASE, s1, n, s2, n) - CSTR_EQUAL;
556 /******** Debugging functions *****************************************/
558 static inline LPCSTR debugtext_t(LPCWSTR text, BOOL isW)
560 if (text == LPSTR_TEXTCALLBACKW) return "(callback)";
561 return isW ? debugstr_w(text) : debugstr_a((LPCSTR)text);
564 static inline LPCSTR debugtext_tn(LPCWSTR text, BOOL isW, INT n)
566 if (text == LPSTR_TEXTCALLBACKW) return "(callback)";
567 n = min(textlenT(text, isW), n);
568 return isW ? debugstr_wn(text, n) : debugstr_an((LPCSTR)text, n);
571 static char* debug_getbuf(void)
573 static int index = 0;
574 static char buffers[DEBUG_BUFFERS][DEBUG_BUFFER_SIZE];
575 return buffers[index++ % DEBUG_BUFFERS];
578 static inline const char* debugrange(const RANGE *lprng)
580 if (!lprng) return "(null)";
581 return wine_dbg_sprintf("[%d, %d]", lprng->lower, lprng->upper);
584 static const char* debugscrollinfo(const SCROLLINFO *pScrollInfo)
586 char* buf = debug_getbuf(), *text = buf;
587 int len, size = DEBUG_BUFFER_SIZE;
589 if (pScrollInfo == NULL) return "(null)";
590 len = snprintf(buf, size, "{cbSize=%u, ", pScrollInfo->cbSize);
591 if (len == -1) goto end; buf += len; size -= len;
592 if (pScrollInfo->fMask & SIF_RANGE)
593 len = snprintf(buf, size, "nMin=%d, nMax=%d, ", pScrollInfo->nMin, pScrollInfo->nMax);
594 else len = 0;
595 if (len == -1) goto end; buf += len; size -= len;
596 if (pScrollInfo->fMask & SIF_PAGE)
597 len = snprintf(buf, size, "nPage=%u, ", pScrollInfo->nPage);
598 else len = 0;
599 if (len == -1) goto end; buf += len; size -= len;
600 if (pScrollInfo->fMask & SIF_POS)
601 len = snprintf(buf, size, "nPos=%d, ", pScrollInfo->nPos);
602 else len = 0;
603 if (len == -1) goto end; buf += len; size -= len;
604 if (pScrollInfo->fMask & SIF_TRACKPOS)
605 len = snprintf(buf, size, "nTrackPos=%d, ", pScrollInfo->nTrackPos);
606 else len = 0;
607 if (len == -1) goto end; buf += len;
608 goto undo;
609 end:
610 buf = text + strlen(text);
611 undo:
612 if (buf - text > 2) { buf[-2] = '}'; buf[-1] = 0; }
613 return text;
616 static const char* debugnmlistview(const NMLISTVIEW *plvnm)
618 if (!plvnm) return "(null)";
619 return wine_dbg_sprintf("iItem=%d, iSubItem=%d, uNewState=0x%x,"
620 " uOldState=0x%x, uChanged=0x%x, ptAction=%s, lParam=%ld",
621 plvnm->iItem, plvnm->iSubItem, plvnm->uNewState, plvnm->uOldState,
622 plvnm->uChanged, wine_dbgstr_point(&plvnm->ptAction), plvnm->lParam);
625 static const char* debuglvitem_t(const LVITEMW *lpLVItem, BOOL isW)
627 char* buf = debug_getbuf(), *text = buf;
628 int len, size = DEBUG_BUFFER_SIZE;
630 if (lpLVItem == NULL) return "(null)";
631 len = snprintf(buf, size, "{iItem=%d, iSubItem=%d, ", lpLVItem->iItem, lpLVItem->iSubItem);
632 if (len == -1) goto end; buf += len; size -= len;
633 if (lpLVItem->mask & LVIF_STATE)
634 len = snprintf(buf, size, "state=%x, stateMask=%x, ", lpLVItem->state, lpLVItem->stateMask);
635 else len = 0;
636 if (len == -1) goto end; buf += len; size -= len;
637 if (lpLVItem->mask & LVIF_TEXT)
638 len = snprintf(buf, size, "pszText=%s, cchTextMax=%d, ", debugtext_tn(lpLVItem->pszText, isW, 80), lpLVItem->cchTextMax);
639 else len = 0;
640 if (len == -1) goto end; buf += len; size -= len;
641 if (lpLVItem->mask & LVIF_IMAGE)
642 len = snprintf(buf, size, "iImage=%d, ", lpLVItem->iImage);
643 else len = 0;
644 if (len == -1) goto end; buf += len; size -= len;
645 if (lpLVItem->mask & LVIF_PARAM)
646 len = snprintf(buf, size, "lParam=%lx, ", lpLVItem->lParam);
647 else len = 0;
648 if (len == -1) goto end; buf += len; size -= len;
649 if (lpLVItem->mask & LVIF_INDENT)
650 len = snprintf(buf, size, "iIndent=%d, ", lpLVItem->iIndent);
651 else len = 0;
652 if (len == -1) goto end; buf += len;
653 goto undo;
654 end:
655 buf = text + strlen(text);
656 undo:
657 if (buf - text > 2) { buf[-2] = '}'; buf[-1] = 0; }
658 return text;
661 static const char* debuglvcolumn_t(const LVCOLUMNW *lpColumn, BOOL isW)
663 char* buf = debug_getbuf(), *text = buf;
664 int len, size = DEBUG_BUFFER_SIZE;
666 if (lpColumn == NULL) return "(null)";
667 len = snprintf(buf, size, "{");
668 if (len == -1) goto end; buf += len; size -= len;
669 if (lpColumn->mask & LVCF_SUBITEM)
670 len = snprintf(buf, size, "iSubItem=%d, ", lpColumn->iSubItem);
671 else len = 0;
672 if (len == -1) goto end; buf += len; size -= len;
673 if (lpColumn->mask & LVCF_FMT)
674 len = snprintf(buf, size, "fmt=%x, ", lpColumn->fmt);
675 else len = 0;
676 if (len == -1) goto end; buf += len; size -= len;
677 if (lpColumn->mask & LVCF_WIDTH)
678 len = snprintf(buf, size, "cx=%d, ", lpColumn->cx);
679 else len = 0;
680 if (len == -1) goto end; buf += len; size -= len;
681 if (lpColumn->mask & LVCF_TEXT)
682 len = snprintf(buf, size, "pszText=%s, cchTextMax=%d, ", debugtext_tn(lpColumn->pszText, isW, 80), lpColumn->cchTextMax);
683 else len = 0;
684 if (len == -1) goto end; buf += len; size -= len;
685 if (lpColumn->mask & LVCF_IMAGE)
686 len = snprintf(buf, size, "iImage=%d, ", lpColumn->iImage);
687 else len = 0;
688 if (len == -1) goto end; buf += len; size -= len;
689 if (lpColumn->mask & LVCF_ORDER)
690 len = snprintf(buf, size, "iOrder=%d, ", lpColumn->iOrder);
691 else len = 0;
692 if (len == -1) goto end; buf += len;
693 goto undo;
694 end:
695 buf = text + strlen(text);
696 undo:
697 if (buf - text > 2) { buf[-2] = '}'; buf[-1] = 0; }
698 return text;
701 static const char* debuglvhittestinfo(const LVHITTESTINFO *lpht)
703 if (!lpht) return "(null)";
705 return wine_dbg_sprintf("{pt=%s, flags=0x%x, iItem=%d, iSubItem=%d}",
706 wine_dbgstr_point(&lpht->pt), lpht->flags, lpht->iItem, lpht->iSubItem);
709 /* Return the corresponding text for a given scroll value */
710 static inline LPCSTR debugscrollcode(int nScrollCode)
712 switch(nScrollCode)
714 case SB_LINELEFT: return "SB_LINELEFT";
715 case SB_LINERIGHT: return "SB_LINERIGHT";
716 case SB_PAGELEFT: return "SB_PAGELEFT";
717 case SB_PAGERIGHT: return "SB_PAGERIGHT";
718 case SB_THUMBPOSITION: return "SB_THUMBPOSITION";
719 case SB_THUMBTRACK: return "SB_THUMBTRACK";
720 case SB_ENDSCROLL: return "SB_ENDSCROLL";
721 case SB_INTERNAL: return "SB_INTERNAL";
722 default: return "unknown";
727 /******** Notification functions ************************************/
729 static int get_ansi_notification(UINT unicodeNotificationCode)
731 switch (unicodeNotificationCode)
733 case LVN_BEGINLABELEDITA:
734 case LVN_BEGINLABELEDITW: return LVN_BEGINLABELEDITA;
735 case LVN_ENDLABELEDITA:
736 case LVN_ENDLABELEDITW: return LVN_ENDLABELEDITA;
737 case LVN_GETDISPINFOA:
738 case LVN_GETDISPINFOW: return LVN_GETDISPINFOA;
739 case LVN_SETDISPINFOA:
740 case LVN_SETDISPINFOW: return LVN_SETDISPINFOA;
741 case LVN_ODFINDITEMA:
742 case LVN_ODFINDITEMW: return LVN_ODFINDITEMA;
743 case LVN_GETINFOTIPA:
744 case LVN_GETINFOTIPW: return LVN_GETINFOTIPA;
745 /* header forwards */
746 case HDN_TRACKA:
747 case HDN_TRACKW: return HDN_TRACKA;
748 case HDN_ENDTRACKA:
749 case HDN_ENDTRACKW: return HDN_ENDTRACKA;
750 case HDN_BEGINDRAG: return HDN_BEGINDRAG;
751 case HDN_ENDDRAG: return HDN_ENDDRAG;
752 case HDN_ITEMCHANGINGA:
753 case HDN_ITEMCHANGINGW: return HDN_ITEMCHANGINGA;
754 case HDN_ITEMCHANGEDA:
755 case HDN_ITEMCHANGEDW: return HDN_ITEMCHANGEDA;
756 case HDN_ITEMCLICKA:
757 case HDN_ITEMCLICKW: return HDN_ITEMCLICKA;
758 case HDN_DIVIDERDBLCLICKA:
759 case HDN_DIVIDERDBLCLICKW: return HDN_DIVIDERDBLCLICKA;
760 default: break;
762 FIXME("unknown notification %x\n", unicodeNotificationCode);
763 return unicodeNotificationCode;
766 /* forwards header notifications to listview parent */
767 static LRESULT notify_forward_header(const LISTVIEW_INFO *infoPtr, NMHEADERW *lpnmhW)
769 LPCWSTR text = NULL, filter = NULL;
770 LRESULT ret;
771 NMHEADERA *lpnmh = (NMHEADERA*) lpnmhW;
773 /* on unicode format exit earlier */
774 if (infoPtr->notifyFormat == NFR_UNICODE)
775 return SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, lpnmh->hdr.idFrom,
776 (LPARAM)lpnmh);
778 /* header always supplies unicode notifications,
779 all we have to do is to convert strings to ANSI */
780 if (lpnmh->pitem)
782 /* convert item text */
783 if (lpnmh->pitem->mask & HDI_TEXT)
785 text = (LPCWSTR)lpnmh->pitem->pszText;
786 lpnmh->pitem->pszText = NULL;
787 Str_SetPtrWtoA(&lpnmh->pitem->pszText, text);
789 /* convert filter text */
790 if ((lpnmh->pitem->mask & HDI_FILTER) && (lpnmh->pitem->type == HDFT_ISSTRING) &&
791 lpnmh->pitem->pvFilter)
793 filter = (LPCWSTR)((HD_TEXTFILTERA*)lpnmh->pitem->pvFilter)->pszText;
794 ((HD_TEXTFILTERA*)lpnmh->pitem->pvFilter)->pszText = NULL;
795 Str_SetPtrWtoA(&((HD_TEXTFILTERA*)lpnmh->pitem->pvFilter)->pszText, filter);
798 lpnmh->hdr.code = get_ansi_notification(lpnmh->hdr.code);
800 ret = SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, lpnmh->hdr.idFrom,
801 (LPARAM)lpnmh);
803 /* cleanup */
804 if(text)
806 Free(lpnmh->pitem->pszText);
807 lpnmh->pitem->pszText = (LPSTR)text;
809 if(filter)
811 Free(((HD_TEXTFILTERA*)lpnmh->pitem->pvFilter)->pszText);
812 ((HD_TEXTFILTERA*)lpnmh->pitem->pvFilter)->pszText = (LPSTR)filter;
815 return ret;
818 static LRESULT notify_hdr(const LISTVIEW_INFO *infoPtr, INT code, LPNMHDR pnmh)
820 LRESULT result;
822 TRACE("(code=%d)\n", code);
824 pnmh->hwndFrom = infoPtr->hwndSelf;
825 pnmh->idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
826 pnmh->code = code;
827 result = SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, pnmh->idFrom, (LPARAM)pnmh);
829 TRACE(" <= %ld\n", result);
831 return result;
834 static inline BOOL notify(const LISTVIEW_INFO *infoPtr, INT code)
836 NMHDR nmh;
837 HWND hwnd = infoPtr->hwndSelf;
838 notify_hdr(infoPtr, code, &nmh);
839 return IsWindow(hwnd);
842 static inline void notify_itemactivate(const LISTVIEW_INFO *infoPtr, const LVHITTESTINFO *htInfo)
844 NMITEMACTIVATE nmia;
845 LVITEMW item;
847 if (htInfo) {
848 nmia.uNewState = 0;
849 nmia.uOldState = 0;
850 nmia.uChanged = 0;
851 nmia.uKeyFlags = 0;
853 item.mask = LVIF_PARAM|LVIF_STATE;
854 item.iItem = htInfo->iItem;
855 item.iSubItem = 0;
856 item.stateMask = (UINT)-1;
857 if (LISTVIEW_GetItemT(infoPtr, &item, TRUE)) {
858 nmia.lParam = item.lParam;
859 nmia.uOldState = item.state;
860 nmia.uNewState = item.state | LVIS_ACTIVATING;
861 nmia.uChanged = LVIF_STATE;
864 nmia.iItem = htInfo->iItem;
865 nmia.iSubItem = htInfo->iSubItem;
866 nmia.ptAction = htInfo->pt;
868 if (GetKeyState(VK_SHIFT) & 0x8000) nmia.uKeyFlags |= LVKF_SHIFT;
869 if (GetKeyState(VK_CONTROL) & 0x8000) nmia.uKeyFlags |= LVKF_CONTROL;
870 if (GetKeyState(VK_MENU) & 0x8000) nmia.uKeyFlags |= LVKF_ALT;
872 notify_hdr(infoPtr, LVN_ITEMACTIVATE, (LPNMHDR)&nmia);
875 static inline LRESULT notify_listview(const LISTVIEW_INFO *infoPtr, INT code, LPNMLISTVIEW plvnm)
877 TRACE("(code=%d, plvnm=%s)\n", code, debugnmlistview(plvnm));
878 return notify_hdr(infoPtr, code, (LPNMHDR)plvnm);
881 /* Handles NM_DBLCLK, NM_CLICK, NM_RDBLCLK, NM_RCLICK. Only NM_RCLICK return value is used. */
882 static BOOL notify_click(const LISTVIEW_INFO *infoPtr, INT code, const LVHITTESTINFO *lvht)
884 NMITEMACTIVATE nmia;
885 LVITEMW item;
886 HWND hwnd = infoPtr->hwndSelf;
887 LRESULT ret;
889 TRACE("code=%d, lvht=%s\n", code, debuglvhittestinfo(lvht));
890 ZeroMemory(&nmia, sizeof(nmia));
891 nmia.iItem = lvht->iItem;
892 nmia.iSubItem = lvht->iSubItem;
893 nmia.ptAction = lvht->pt;
894 item.mask = LVIF_PARAM;
895 item.iItem = lvht->iItem;
896 item.iSubItem = 0;
897 if (LISTVIEW_GetItemT(infoPtr, &item, TRUE)) nmia.lParam = item.lParam;
898 ret = notify_hdr(infoPtr, code, (NMHDR*)&nmia);
899 return IsWindow(hwnd) && (code == NM_RCLICK ? !ret : TRUE);
902 static BOOL notify_deleteitem(const LISTVIEW_INFO *infoPtr, INT nItem)
904 NMLISTVIEW nmlv;
905 LVITEMW item;
906 HWND hwnd = infoPtr->hwndSelf;
908 ZeroMemory(&nmlv, sizeof (NMLISTVIEW));
909 nmlv.iItem = nItem;
910 item.mask = LVIF_PARAM;
911 item.iItem = nItem;
912 item.iSubItem = 0;
913 if (LISTVIEW_GetItemT(infoPtr, &item, TRUE)) nmlv.lParam = item.lParam;
914 notify_listview(infoPtr, LVN_DELETEITEM, &nmlv);
915 return IsWindow(hwnd);
919 Send notification. depends on dispinfoW having same
920 structure as dispinfoA.
921 infoPtr : listview struct
922 code : *Unicode* notification code
923 pdi : dispinfo structure (can be unicode or ansi)
924 isW : TRUE if dispinfo is Unicode
926 static BOOL notify_dispinfoT(const LISTVIEW_INFO *infoPtr, UINT code, LPNMLVDISPINFOW pdi, BOOL isW)
928 INT length = 0, ret_length;
929 LPWSTR buffer = NULL, ret_text;
930 BOOL return_ansi = FALSE;
931 BOOL return_unicode = FALSE;
932 BOOL ret;
934 if ((pdi->item.mask & LVIF_TEXT) && is_text(pdi->item.pszText))
936 return_unicode = ( isW && infoPtr->notifyFormat == NFR_ANSI);
937 return_ansi = (!isW && infoPtr->notifyFormat == NFR_UNICODE);
940 ret_length = pdi->item.cchTextMax;
941 ret_text = pdi->item.pszText;
943 if (return_unicode || return_ansi)
945 if (code != LVN_GETDISPINFOW)
947 length = return_ansi ?
948 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pdi->item.pszText, -1, NULL, 0):
949 WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, NULL, 0, NULL, NULL);
951 else
953 length = pdi->item.cchTextMax;
954 *pdi->item.pszText = 0; /* make sure we don't process garbage */
957 buffer = Alloc( (return_ansi ? sizeof(WCHAR) : sizeof(CHAR)) * length);
958 if (!buffer) return FALSE;
960 if (return_ansi)
961 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pdi->item.pszText, -1,
962 buffer, length);
963 else
964 WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, (LPSTR) buffer,
965 length, NULL, NULL);
967 pdi->item.pszText = buffer;
968 pdi->item.cchTextMax = length;
971 if (infoPtr->notifyFormat == NFR_ANSI)
972 code = get_ansi_notification(code);
974 TRACE(" pdi->item=%s\n", debuglvitem_t(&pdi->item, infoPtr->notifyFormat != NFR_ANSI));
975 ret = notify_hdr(infoPtr, code, &pdi->hdr);
976 TRACE(" resulting code=%d\n", pdi->hdr.code);
978 if (return_ansi || return_unicode)
980 if (return_ansi && (pdi->hdr.code == LVN_GETDISPINFOA))
982 strcpy((char*)ret_text, (char*)pdi->item.pszText);
984 else if (return_unicode && (pdi->hdr.code == LVN_GETDISPINFOW))
986 strcpyW(ret_text, pdi->item.pszText);
988 else if (return_ansi) /* note : pointer can be changed by app ! */
990 WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, (LPSTR) ret_text,
991 ret_length, NULL, NULL);
993 else
994 MultiByteToWideChar(CP_ACP, 0, (LPSTR) pdi->item.pszText, -1,
995 ret_text, ret_length);
997 pdi->item.pszText = ret_text; /* restores our buffer */
998 pdi->item.cchTextMax = ret_length;
1000 Free(buffer);
1001 return ret;
1004 /* if dipsinfo holder changed notification code then convert */
1005 if (!isW && (pdi->hdr.code == LVN_GETDISPINFOW) && (pdi->item.mask & LVIF_TEXT))
1007 length = WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, NULL, 0, NULL, NULL);
1009 buffer = Alloc(length * sizeof(CHAR));
1010 if (!buffer) return FALSE;
1012 WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, (LPSTR) buffer,
1013 ret_length, NULL, NULL);
1015 strcpy((LPSTR)pdi->item.pszText, (LPSTR)buffer);
1016 Free(buffer);
1019 return ret;
1022 static void customdraw_fill(NMLVCUSTOMDRAW *lpnmlvcd, const LISTVIEW_INFO *infoPtr, HDC hdc,
1023 const RECT *rcBounds, const LVITEMW *lplvItem)
1025 ZeroMemory(lpnmlvcd, sizeof(NMLVCUSTOMDRAW));
1026 lpnmlvcd->nmcd.hdc = hdc;
1027 lpnmlvcd->nmcd.rc = *rcBounds;
1028 lpnmlvcd->clrTextBk = infoPtr->clrTextBk;
1029 lpnmlvcd->clrText = infoPtr->clrText;
1030 if (!lplvItem) return;
1031 lpnmlvcd->nmcd.dwItemSpec = lplvItem->iItem + 1;
1032 lpnmlvcd->iSubItem = lplvItem->iSubItem;
1033 if (lplvItem->state & LVIS_SELECTED) lpnmlvcd->nmcd.uItemState |= CDIS_SELECTED;
1034 if (lplvItem->state & LVIS_FOCUSED) lpnmlvcd->nmcd.uItemState |= CDIS_FOCUS;
1035 if (lplvItem->iItem == infoPtr->nHotItem) lpnmlvcd->nmcd.uItemState |= CDIS_HOT;
1036 lpnmlvcd->nmcd.lItemlParam = lplvItem->lParam;
1039 static inline DWORD notify_customdraw (const LISTVIEW_INFO *infoPtr, DWORD dwDrawStage, NMLVCUSTOMDRAW *lpnmlvcd)
1041 BOOL isForItem = (lpnmlvcd->nmcd.dwItemSpec != 0);
1042 DWORD result;
1044 lpnmlvcd->nmcd.dwDrawStage = dwDrawStage;
1045 if (isForItem) lpnmlvcd->nmcd.dwDrawStage |= CDDS_ITEM;
1046 if (lpnmlvcd->iSubItem) lpnmlvcd->nmcd.dwDrawStage |= CDDS_SUBITEM;
1047 if (isForItem) lpnmlvcd->nmcd.dwItemSpec--;
1048 result = notify_hdr(infoPtr, NM_CUSTOMDRAW, &lpnmlvcd->nmcd.hdr);
1049 if (isForItem) lpnmlvcd->nmcd.dwItemSpec++;
1050 return result;
1053 static void prepaint_setup (const LISTVIEW_INFO *infoPtr, HDC hdc, NMLVCUSTOMDRAW *lpnmlvcd, BOOL SubItem)
1055 COLORREF backcolor, textcolor;
1057 /* apparently, for selected items, we have to override the returned values */
1058 if (!SubItem)
1060 if (lpnmlvcd->nmcd.uItemState & CDIS_SELECTED)
1062 if (infoPtr->bFocus)
1064 lpnmlvcd->clrTextBk = comctl32_color.clrHighlight;
1065 lpnmlvcd->clrText = comctl32_color.clrHighlightText;
1067 else if (infoPtr->dwStyle & LVS_SHOWSELALWAYS)
1069 lpnmlvcd->clrTextBk = comctl32_color.clr3dFace;
1070 lpnmlvcd->clrText = comctl32_color.clrBtnText;
1075 backcolor = lpnmlvcd->clrTextBk;
1076 textcolor = lpnmlvcd->clrText;
1078 if (backcolor == CLR_DEFAULT)
1079 backcolor = comctl32_color.clrWindow;
1080 if (textcolor == CLR_DEFAULT)
1081 textcolor = comctl32_color.clrWindowText;
1083 /* Set the text attributes */
1084 if (backcolor != CLR_NONE)
1086 SetBkMode(hdc, OPAQUE);
1087 SetBkColor(hdc, backcolor);
1089 else
1090 SetBkMode(hdc, TRANSPARENT);
1091 SetTextColor(hdc, textcolor);
1094 static inline DWORD notify_postpaint (const LISTVIEW_INFO *infoPtr, NMLVCUSTOMDRAW *lpnmlvcd)
1096 return notify_customdraw(infoPtr, CDDS_POSTPAINT, lpnmlvcd);
1099 /* returns TRUE when repaint needed, FALSE otherwise */
1100 static BOOL notify_measureitem(LISTVIEW_INFO *infoPtr)
1102 MEASUREITEMSTRUCT mis;
1103 mis.CtlType = ODT_LISTVIEW;
1104 mis.CtlID = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1105 mis.itemID = -1;
1106 mis.itemWidth = 0;
1107 mis.itemData = 0;
1108 mis.itemHeight= infoPtr->nItemHeight;
1109 SendMessageW(infoPtr->hwndNotify, WM_MEASUREITEM, mis.CtlID, (LPARAM)&mis);
1110 if (infoPtr->nItemHeight != max(mis.itemHeight, 1))
1112 infoPtr->nMeasureItemHeight = infoPtr->nItemHeight = max(mis.itemHeight, 1);
1113 return TRUE;
1115 return FALSE;
1118 /******** Item iterator functions **********************************/
1120 static RANGES ranges_create(int count);
1121 static void ranges_destroy(RANGES ranges);
1122 static BOOL ranges_add(RANGES ranges, RANGE range);
1123 static BOOL ranges_del(RANGES ranges, RANGE range);
1124 static void ranges_dump(RANGES ranges);
1126 static inline BOOL ranges_additem(RANGES ranges, INT nItem)
1128 RANGE range = { nItem, nItem + 1 };
1130 return ranges_add(ranges, range);
1133 static inline BOOL ranges_delitem(RANGES ranges, INT nItem)
1135 RANGE range = { nItem, nItem + 1 };
1137 return ranges_del(ranges, range);
1140 /***
1141 * ITERATOR DOCUMENTATION
1143 * The iterator functions allow for easy, and convenient iteration
1144 * over items of interest in the list. Typically, you create an
1145 * iterator, use it, and destroy it, as such:
1146 * ITERATOR i;
1148 * iterator_xxxitems(&i, ...);
1149 * while (iterator_{prev,next}(&i)
1151 * //code which uses i.nItem
1153 * iterator_destroy(&i);
1155 * where xxx is either: framed, or visible.
1156 * Note that it is important that the code destroys the iterator
1157 * after it's done with it, as the creation of the iterator may
1158 * allocate memory, which thus needs to be freed.
1160 * You can iterate both forwards, and backwards through the list,
1161 * by using iterator_next or iterator_prev respectively.
1163 * Lower numbered items are draw on top of higher number items in
1164 * LVS_ICON, and LVS_SMALLICON (which are the only modes where
1165 * items may overlap). So, to test items, you should use
1166 * iterator_next
1167 * which lists the items top to bottom (in Z-order).
1168 * For drawing items, you should use
1169 * iterator_prev
1170 * which lists the items bottom to top (in Z-order).
1171 * If you keep iterating over the items after the end-of-items
1172 * marker (-1) is returned, the iterator will start from the
1173 * beginning. Typically, you don't need to test for -1,
1174 * because iterator_{next,prev} will return TRUE if more items
1175 * are to be iterated over, or FALSE otherwise.
1177 * Note: the iterator is defined to be bidirectional. That is,
1178 * any number of prev followed by any number of next, or
1179 * five versa, should leave the iterator at the same item:
1180 * prev * n, next * n = next * n, prev * n
1182 * The iterator has a notion of an out-of-order, special item,
1183 * which sits at the start of the list. This is used in
1184 * LVS_ICON, and LVS_SMALLICON mode to handle the focused item,
1185 * which needs to be first, as it may overlap other items.
1187 * The code is a bit messy because we have:
1188 * - a special item to deal with
1189 * - simple range, or composite range
1190 * - empty range.
1191 * If you find bugs, or want to add features, please make sure you
1192 * always check/modify *both* iterator_prev, and iterator_next.
1195 /****
1196 * This function iterates through the items in increasing order,
1197 * but prefixed by the special item, then -1. That is:
1198 * special, 1, 2, 3, ..., n, -1.
1199 * Each item is listed only once.
1201 static inline BOOL iterator_next(ITERATOR* i)
1203 if (i->nItem == -1)
1205 i->nItem = i->nSpecial;
1206 if (i->nItem != -1) return TRUE;
1208 if (i->nItem == i->nSpecial)
1210 if (i->ranges) i->index = 0;
1211 goto pickarange;
1214 i->nItem++;
1215 testitem:
1216 if (i->nItem == i->nSpecial) i->nItem++;
1217 if (i->nItem < i->range.upper) return TRUE;
1219 pickarange:
1220 if (i->ranges)
1222 if (i->index < DPA_GetPtrCount(i->ranges->hdpa))
1223 i->range = *(RANGE*)DPA_GetPtr(i->ranges->hdpa, i->index++);
1224 else goto end;
1226 else if (i->nItem >= i->range.upper) goto end;
1228 i->nItem = i->range.lower;
1229 if (i->nItem >= 0) goto testitem;
1230 end:
1231 i->nItem = -1;
1232 return FALSE;
1235 /****
1236 * This function iterates through the items in decreasing order,
1237 * followed by the special item, then -1. That is:
1238 * n, n-1, ..., 3, 2, 1, special, -1.
1239 * Each item is listed only once.
1241 static inline BOOL iterator_prev(ITERATOR* i)
1243 BOOL start = FALSE;
1245 if (i->nItem == -1)
1247 start = TRUE;
1248 if (i->ranges) i->index = DPA_GetPtrCount(i->ranges->hdpa);
1249 goto pickarange;
1251 if (i->nItem == i->nSpecial)
1253 i->nItem = -1;
1254 return FALSE;
1257 testitem:
1258 i->nItem--;
1259 if (i->nItem == i->nSpecial) i->nItem--;
1260 if (i->nItem >= i->range.lower) return TRUE;
1262 pickarange:
1263 if (i->ranges)
1265 if (i->index > 0)
1266 i->range = *(RANGE*)DPA_GetPtr(i->ranges->hdpa, --i->index);
1267 else goto end;
1269 else if (!start && i->nItem < i->range.lower) goto end;
1271 i->nItem = i->range.upper;
1272 if (i->nItem > 0) goto testitem;
1273 end:
1274 return (i->nItem = i->nSpecial) != -1;
1277 static RANGE iterator_range(const ITERATOR *i)
1279 RANGE range;
1281 if (!i->ranges) return i->range;
1283 if (DPA_GetPtrCount(i->ranges->hdpa) > 0)
1285 range.lower = (*(RANGE*)DPA_GetPtr(i->ranges->hdpa, 0)).lower;
1286 range.upper = (*(RANGE*)DPA_GetPtr(i->ranges->hdpa, DPA_GetPtrCount(i->ranges->hdpa) - 1)).upper;
1288 else range.lower = range.upper = 0;
1290 return range;
1293 /***
1294 * Releases resources associated with this iterator.
1296 static inline void iterator_destroy(const ITERATOR *i)
1298 ranges_destroy(i->ranges);
1301 /***
1302 * Create an empty iterator.
1304 static inline BOOL iterator_empty(ITERATOR* i)
1306 ZeroMemory(i, sizeof(*i));
1307 i->nItem = i->nSpecial = i->range.lower = i->range.upper = -1;
1308 return TRUE;
1311 /***
1312 * Create an iterator over a range.
1314 static inline BOOL iterator_rangeitems(ITERATOR* i, RANGE range)
1316 iterator_empty(i);
1317 i->range = range;
1318 return TRUE;
1321 /***
1322 * Create an iterator over a bunch of ranges.
1323 * Please note that the iterator will take ownership of the ranges,
1324 * and will free them upon destruction.
1326 static inline BOOL iterator_rangesitems(ITERATOR* i, RANGES ranges)
1328 iterator_empty(i);
1329 i->ranges = ranges;
1330 return TRUE;
1333 /***
1334 * Creates an iterator over the items which intersect frame.
1335 * Uses absolute coordinates rather than compensating for the current offset.
1337 static BOOL iterator_frameditems_absolute(ITERATOR* i, const LISTVIEW_INFO* infoPtr, const RECT *frame)
1339 RECT rcItem, rcTemp;
1341 /* in case we fail, we want to return an empty iterator */
1342 if (!iterator_empty(i)) return FALSE;
1344 TRACE("(frame=%s)\n", wine_dbgstr_rect(frame));
1346 if (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON)
1348 INT nItem;
1350 if (infoPtr->uView == LV_VIEW_ICON && infoPtr->nFocusedItem != -1)
1352 LISTVIEW_GetItemBox(infoPtr, infoPtr->nFocusedItem, &rcItem);
1353 if (IntersectRect(&rcTemp, &rcItem, frame))
1354 i->nSpecial = infoPtr->nFocusedItem;
1356 if (!(iterator_rangesitems(i, ranges_create(50)))) return FALSE;
1357 /* to do better here, we need to have PosX, and PosY sorted */
1358 TRACE("building icon ranges:\n");
1359 for (nItem = 0; nItem < infoPtr->nItemCount; nItem++)
1361 rcItem.left = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosX, nItem);
1362 rcItem.top = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosY, nItem);
1363 rcItem.right = rcItem.left + infoPtr->nItemWidth;
1364 rcItem.bottom = rcItem.top + infoPtr->nItemHeight;
1365 if (IntersectRect(&rcTemp, &rcItem, frame))
1366 ranges_additem(i->ranges, nItem);
1368 return TRUE;
1370 else if (infoPtr->uView == LV_VIEW_DETAILS)
1372 RANGE range;
1374 if (frame->left >= infoPtr->nItemWidth) return TRUE;
1375 if (frame->top >= infoPtr->nItemHeight * infoPtr->nItemCount) return TRUE;
1377 range.lower = max(frame->top / infoPtr->nItemHeight, 0);
1378 range.upper = min((frame->bottom - 1) / infoPtr->nItemHeight, infoPtr->nItemCount - 1) + 1;
1379 if (range.upper <= range.lower) return TRUE;
1380 if (!iterator_rangeitems(i, range)) return FALSE;
1381 TRACE(" report=%s\n", debugrange(&i->range));
1383 else
1385 INT nPerCol = max((infoPtr->rcList.bottom - infoPtr->rcList.top) / infoPtr->nItemHeight, 1);
1386 INT nFirstRow = max(frame->top / infoPtr->nItemHeight, 0);
1387 INT nLastRow = min((frame->bottom - 1) / infoPtr->nItemHeight, nPerCol - 1);
1388 INT nFirstCol;
1389 INT nLastCol;
1390 INT lower;
1391 RANGE item_range;
1392 INT nCol;
1394 if (infoPtr->nItemWidth)
1396 nFirstCol = max(frame->left / infoPtr->nItemWidth, 0);
1397 nLastCol = min((frame->right - 1) / infoPtr->nItemWidth, (infoPtr->nItemCount + nPerCol - 1) / nPerCol);
1399 else
1401 nFirstCol = max(frame->left, 0);
1402 nLastCol = min(frame->right - 1, (infoPtr->nItemCount + nPerCol - 1) / nPerCol);
1405 lower = nFirstCol * nPerCol + nFirstRow;
1407 TRACE("nPerCol=%d, nFirstRow=%d, nLastRow=%d, nFirstCol=%d, nLastCol=%d, lower=%d\n",
1408 nPerCol, nFirstRow, nLastRow, nFirstCol, nLastCol, lower);
1410 if (nLastCol < nFirstCol || nLastRow < nFirstRow) return TRUE;
1412 if (!(iterator_rangesitems(i, ranges_create(nLastCol - nFirstCol + 1)))) return FALSE;
1413 TRACE("building list ranges:\n");
1414 for (nCol = nFirstCol; nCol <= nLastCol; nCol++)
1416 item_range.lower = nCol * nPerCol + nFirstRow;
1417 if(item_range.lower >= infoPtr->nItemCount) break;
1418 item_range.upper = min(nCol * nPerCol + nLastRow + 1, infoPtr->nItemCount);
1419 TRACE(" list=%s\n", debugrange(&item_range));
1420 ranges_add(i->ranges, item_range);
1424 return TRUE;
1427 /***
1428 * Creates an iterator over the items which intersect lprc.
1430 static BOOL iterator_frameditems(ITERATOR* i, const LISTVIEW_INFO* infoPtr, const RECT *lprc)
1432 RECT frame = *lprc;
1433 POINT Origin;
1435 TRACE("(lprc=%s)\n", wine_dbgstr_rect(lprc));
1437 LISTVIEW_GetOrigin(infoPtr, &Origin);
1438 OffsetRect(&frame, -Origin.x, -Origin.y);
1440 return iterator_frameditems_absolute(i, infoPtr, &frame);
1443 /***
1444 * Creates an iterator over the items which intersect the visible region of hdc.
1446 static BOOL iterator_visibleitems(ITERATOR *i, const LISTVIEW_INFO *infoPtr, HDC hdc)
1448 POINT Origin, Position;
1449 RECT rcItem, rcClip;
1450 INT rgntype;
1452 rgntype = GetClipBox(hdc, &rcClip);
1453 if (rgntype == NULLREGION) return iterator_empty(i);
1454 if (!iterator_frameditems(i, infoPtr, &rcClip)) return FALSE;
1455 if (rgntype == SIMPLEREGION) return TRUE;
1457 /* first deal with the special item */
1458 if (i->nSpecial != -1)
1460 LISTVIEW_GetItemBox(infoPtr, i->nSpecial, &rcItem);
1461 if (!RectVisible(hdc, &rcItem)) i->nSpecial = -1;
1464 /* if we can't deal with the region, we'll just go with the simple range */
1465 LISTVIEW_GetOrigin(infoPtr, &Origin);
1466 TRACE("building visible range:\n");
1467 if (!i->ranges && i->range.lower < i->range.upper)
1469 if (!(i->ranges = ranges_create(50))) return TRUE;
1470 if (!ranges_add(i->ranges, i->range))
1472 ranges_destroy(i->ranges);
1473 i->ranges = 0;
1474 return TRUE;
1478 /* now delete the invisible items from the list */
1479 while(iterator_next(i))
1481 LISTVIEW_GetItemOrigin(infoPtr, i->nItem, &Position);
1482 rcItem.left = (infoPtr->uView == LV_VIEW_DETAILS) ? Origin.x : Position.x + Origin.x;
1483 rcItem.top = Position.y + Origin.y;
1484 rcItem.right = rcItem.left + infoPtr->nItemWidth;
1485 rcItem.bottom = rcItem.top + infoPtr->nItemHeight;
1486 if (!RectVisible(hdc, &rcItem))
1487 ranges_delitem(i->ranges, i->nItem);
1489 /* the iterator should restart on the next iterator_next */
1490 TRACE("done\n");
1492 return TRUE;
1495 /* Remove common elements from two iterators */
1496 /* Passed iterators have to point on the first elements */
1497 static BOOL iterator_remove_common_items(ITERATOR *iter1, ITERATOR *iter2)
1499 if(!iter1->ranges || !iter2->ranges) {
1500 int lower, upper;
1502 if(iter1->ranges || iter2->ranges ||
1503 (iter1->range.lower<iter2->range.lower && iter1->range.upper>iter2->range.upper) ||
1504 (iter1->range.lower>iter2->range.lower && iter1->range.upper<iter2->range.upper)) {
1505 ERR("result is not a one range iterator\n");
1506 return FALSE;
1509 if(iter1->range.lower==-1 || iter2->range.lower==-1)
1510 return TRUE;
1512 lower = iter1->range.lower;
1513 upper = iter1->range.upper;
1515 if(lower < iter2->range.lower)
1516 iter1->range.upper = iter2->range.lower;
1517 else if(upper > iter2->range.upper)
1518 iter1->range.lower = iter2->range.upper;
1519 else
1520 iter1->range.lower = iter1->range.upper = -1;
1522 if(iter2->range.lower < lower)
1523 iter2->range.upper = lower;
1524 else if(iter2->range.upper > upper)
1525 iter2->range.lower = upper;
1526 else
1527 iter2->range.lower = iter2->range.upper = -1;
1529 return TRUE;
1532 iterator_next(iter1);
1533 iterator_next(iter2);
1535 while(1) {
1536 if(iter1->nItem==-1 || iter2->nItem==-1)
1537 break;
1539 if(iter1->nItem == iter2->nItem) {
1540 int delete = iter1->nItem;
1542 iterator_prev(iter1);
1543 iterator_prev(iter2);
1544 ranges_delitem(iter1->ranges, delete);
1545 ranges_delitem(iter2->ranges, delete);
1546 iterator_next(iter1);
1547 iterator_next(iter2);
1548 } else if(iter1->nItem > iter2->nItem)
1549 iterator_next(iter2);
1550 else
1551 iterator_next(iter1);
1554 iter1->nItem = iter1->range.lower = iter1->range.upper = -1;
1555 iter2->nItem = iter2->range.lower = iter2->range.upper = -1;
1556 return TRUE;
1559 /******** Misc helper functions ************************************/
1561 static inline LRESULT CallWindowProcT(WNDPROC proc, HWND hwnd, UINT uMsg,
1562 WPARAM wParam, LPARAM lParam, BOOL isW)
1564 if (isW) return CallWindowProcW(proc, hwnd, uMsg, wParam, lParam);
1565 else return CallWindowProcA(proc, hwnd, uMsg, wParam, lParam);
1568 static inline BOOL is_autoarrange(const LISTVIEW_INFO *infoPtr)
1570 return ((infoPtr->dwStyle & LVS_AUTOARRANGE) || infoPtr->bAutoarrange) &&
1571 (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON);
1574 static void toggle_checkbox_state(LISTVIEW_INFO *infoPtr, INT nItem)
1576 DWORD state = STATEIMAGEINDEX(LISTVIEW_GetItemState(infoPtr, nItem, LVIS_STATEIMAGEMASK));
1577 if(state == 1 || state == 2)
1579 LVITEMW lvitem;
1580 state ^= 3;
1581 lvitem.state = INDEXTOSTATEIMAGEMASK(state);
1582 lvitem.stateMask = LVIS_STATEIMAGEMASK;
1583 LISTVIEW_SetItemState(infoPtr, nItem, &lvitem);
1587 /* this should be called after window style got updated,
1588 it used to reset view state to match current window style */
1589 static inline void map_style_view(LISTVIEW_INFO *infoPtr)
1591 switch (infoPtr->dwStyle & LVS_TYPEMASK)
1593 case LVS_ICON:
1594 infoPtr->uView = LV_VIEW_ICON;
1595 break;
1596 case LVS_REPORT:
1597 infoPtr->uView = LV_VIEW_DETAILS;
1598 break;
1599 case LVS_SMALLICON:
1600 infoPtr->uView = LV_VIEW_SMALLICON;
1601 break;
1602 case LVS_LIST:
1603 infoPtr->uView = LV_VIEW_LIST;
1607 /* computes next item id value */
1608 static DWORD get_next_itemid(const LISTVIEW_INFO *infoPtr)
1610 INT count = DPA_GetPtrCount(infoPtr->hdpaItemIds);
1612 if (count > 0)
1614 ITEM_ID *lpID = DPA_GetPtr(infoPtr->hdpaItemIds, count - 1);
1615 return lpID->id + 1;
1617 return 0;
1620 /******** Internal API functions ************************************/
1622 static inline COLUMN_INFO * LISTVIEW_GetColumnInfo(const LISTVIEW_INFO *infoPtr, INT nSubItem)
1624 static COLUMN_INFO mainItem;
1626 if (nSubItem == 0 && DPA_GetPtrCount(infoPtr->hdpaColumns) == 0) return &mainItem;
1627 assert (nSubItem >= 0 && nSubItem < DPA_GetPtrCount(infoPtr->hdpaColumns));
1629 /* update cached column rectangles */
1630 if (infoPtr->colRectsDirty)
1632 COLUMN_INFO *info;
1633 LISTVIEW_INFO *Ptr = (LISTVIEW_INFO*)infoPtr;
1634 INT i;
1636 for (i = 0; i < DPA_GetPtrCount(infoPtr->hdpaColumns); i++) {
1637 info = DPA_GetPtr(infoPtr->hdpaColumns, i);
1638 SendMessageW(infoPtr->hwndHeader, HDM_GETITEMRECT, i, (LPARAM)&info->rcHeader);
1640 Ptr->colRectsDirty = FALSE;
1643 return DPA_GetPtr(infoPtr->hdpaColumns, nSubItem);
1646 static INT LISTVIEW_CreateHeader(LISTVIEW_INFO *infoPtr)
1648 DWORD dFlags = WS_CHILD | HDS_HORZ | HDS_FULLDRAG | HDS_DRAGDROP;
1649 HINSTANCE hInst;
1651 if (infoPtr->hwndHeader) return 0;
1653 TRACE("Creating header for list %p\n", infoPtr->hwndSelf);
1655 /* setup creation flags */
1656 dFlags |= (LVS_NOSORTHEADER & infoPtr->dwStyle) ? 0 : HDS_BUTTONS;
1657 dFlags |= (LVS_NOCOLUMNHEADER & infoPtr->dwStyle) ? HDS_HIDDEN : 0;
1659 hInst = (HINSTANCE)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_HINSTANCE);
1661 /* create header */
1662 infoPtr->hwndHeader = CreateWindowW(WC_HEADERW, NULL, dFlags,
1663 0, 0, 0, 0, infoPtr->hwndSelf, NULL, hInst, NULL);
1664 if (!infoPtr->hwndHeader) return -1;
1666 /* set header unicode format */
1667 SendMessageW(infoPtr->hwndHeader, HDM_SETUNICODEFORMAT, TRUE, 0);
1669 /* set header font */
1670 SendMessageW(infoPtr->hwndHeader, WM_SETFONT, (WPARAM)infoPtr->hFont, TRUE);
1672 /* set header image list */
1673 if (infoPtr->himlSmall)
1674 SendMessageW(infoPtr->hwndHeader, HDM_SETIMAGELIST, 0, (LPARAM)infoPtr->himlSmall);
1676 LISTVIEW_UpdateSize(infoPtr);
1678 return 0;
1681 static inline void LISTVIEW_GetHeaderRect(const LISTVIEW_INFO *infoPtr, INT nSubItem, LPRECT lprc)
1683 *lprc = LISTVIEW_GetColumnInfo(infoPtr, nSubItem)->rcHeader;
1686 static inline BOOL LISTVIEW_IsHeaderEnabled(const LISTVIEW_INFO *infoPtr)
1688 return (infoPtr->uView == LV_VIEW_DETAILS ||
1689 infoPtr->dwLvExStyle & LVS_EX_HEADERINALLVIEWS) &&
1690 !(infoPtr->dwStyle & LVS_NOCOLUMNHEADER);
1693 static inline BOOL LISTVIEW_GetItemW(const LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem)
1695 return LISTVIEW_GetItemT(infoPtr, lpLVItem, TRUE);
1698 /* used to handle collapse main item column case */
1699 static inline BOOL LISTVIEW_DrawFocusRect(const LISTVIEW_INFO *infoPtr, HDC hdc)
1701 return (infoPtr->rcFocus.left < infoPtr->rcFocus.right) ?
1702 DrawFocusRect(hdc, &infoPtr->rcFocus) : FALSE;
1705 /* Listview invalidation functions: use _only_ these functions to invalidate */
1707 static inline BOOL is_redrawing(const LISTVIEW_INFO *infoPtr)
1709 return infoPtr->bRedraw;
1712 static inline void LISTVIEW_InvalidateRect(const LISTVIEW_INFO *infoPtr, const RECT* rect)
1714 if(!is_redrawing(infoPtr)) return;
1715 TRACE(" invalidating rect=%s\n", wine_dbgstr_rect(rect));
1716 InvalidateRect(infoPtr->hwndSelf, rect, TRUE);
1719 static inline void LISTVIEW_InvalidateItem(const LISTVIEW_INFO *infoPtr, INT nItem)
1721 RECT rcBox;
1723 if(!is_redrawing(infoPtr)) return;
1724 LISTVIEW_GetItemBox(infoPtr, nItem, &rcBox);
1725 LISTVIEW_InvalidateRect(infoPtr, &rcBox);
1728 static inline void LISTVIEW_InvalidateSubItem(const LISTVIEW_INFO *infoPtr, INT nItem, INT nSubItem)
1730 POINT Origin, Position;
1731 RECT rcBox;
1733 if(!is_redrawing(infoPtr)) return;
1734 assert (infoPtr->uView == LV_VIEW_DETAILS);
1735 LISTVIEW_GetOrigin(infoPtr, &Origin);
1736 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
1737 LISTVIEW_GetHeaderRect(infoPtr, nSubItem, &rcBox);
1738 rcBox.top = 0;
1739 rcBox.bottom = infoPtr->nItemHeight;
1740 OffsetRect(&rcBox, Origin.x + Position.x, Origin.y + Position.y);
1741 LISTVIEW_InvalidateRect(infoPtr, &rcBox);
1744 static inline void LISTVIEW_InvalidateList(const LISTVIEW_INFO *infoPtr)
1746 LISTVIEW_InvalidateRect(infoPtr, NULL);
1749 static inline void LISTVIEW_InvalidateColumn(const LISTVIEW_INFO *infoPtr, INT nColumn)
1751 RECT rcCol;
1753 if(!is_redrawing(infoPtr)) return;
1754 LISTVIEW_GetHeaderRect(infoPtr, nColumn, &rcCol);
1755 rcCol.top = infoPtr->rcList.top;
1756 rcCol.bottom = infoPtr->rcList.bottom;
1757 LISTVIEW_InvalidateRect(infoPtr, &rcCol);
1760 /***
1761 * DESCRIPTION:
1762 * Retrieves the number of items that can fit vertically in the client area.
1764 * PARAMETER(S):
1765 * [I] infoPtr : valid pointer to the listview structure
1767 * RETURN:
1768 * Number of items per row.
1770 static inline INT LISTVIEW_GetCountPerRow(const LISTVIEW_INFO *infoPtr)
1772 INT nListWidth = infoPtr->rcList.right - infoPtr->rcList.left;
1774 return max(nListWidth/(infoPtr->nItemWidth ? infoPtr->nItemWidth : 1), 1);
1777 /***
1778 * DESCRIPTION:
1779 * Retrieves the number of items that can fit horizontally in the client
1780 * area.
1782 * PARAMETER(S):
1783 * [I] infoPtr : valid pointer to the listview structure
1785 * RETURN:
1786 * Number of items per column.
1788 static inline INT LISTVIEW_GetCountPerColumn(const LISTVIEW_INFO *infoPtr)
1790 INT nListHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
1792 return max(nListHeight / infoPtr->nItemHeight, 1);
1796 /*************************************************************************
1797 * LISTVIEW_ProcessLetterKeys
1799 * Processes keyboard messages generated by pressing the letter keys
1800 * on the keyboard.
1801 * What this does is perform a case insensitive search from the
1802 * current position with the following quirks:
1803 * - If two chars or more are pressed in quick succession we search
1804 * for the corresponding string (e.g. 'abc').
1805 * - If there is a delay we wipe away the current search string and
1806 * restart with just that char.
1807 * - If the user keeps pressing the same character, whether slowly or
1808 * fast, so that the search string is entirely composed of this
1809 * character ('aaaaa' for instance), then we search for first item
1810 * that starting with that character.
1811 * - If the user types the above character in quick succession, then
1812 * we must also search for the corresponding string ('aaaaa'), and
1813 * go to that string if there is a match.
1815 * PARAMETERS
1816 * [I] hwnd : handle to the window
1817 * [I] charCode : the character code, the actual character
1818 * [I] keyData : key data
1820 * RETURNS
1822 * Zero.
1824 * BUGS
1826 * - The current implementation has a list of characters it will
1827 * accept and it ignores everything else. In particular it will
1828 * ignore accentuated characters which seems to match what
1829 * Windows does. But I'm not sure it makes sense to follow
1830 * Windows there.
1831 * - We don't sound a beep when the search fails.
1833 * SEE ALSO
1835 * TREEVIEW_ProcessLetterKeys
1837 static INT LISTVIEW_ProcessLetterKeys(LISTVIEW_INFO *infoPtr, WPARAM charCode, LPARAM keyData)
1839 WCHAR buffer[MAX_PATH];
1840 DWORD prevTime;
1841 LVITEMW item;
1842 int startidx;
1843 INT nItem;
1844 INT diff;
1846 /* simple parameter checking */
1847 if (!charCode || !keyData || infoPtr->nItemCount == 0) return 0;
1849 /* only allow the valid WM_CHARs through */
1850 if (!isalnumW(charCode) &&
1851 charCode != '.' && charCode != '`' && charCode != '!' &&
1852 charCode != '@' && charCode != '#' && charCode != '$' &&
1853 charCode != '%' && charCode != '^' && charCode != '&' &&
1854 charCode != '*' && charCode != '(' && charCode != ')' &&
1855 charCode != '-' && charCode != '_' && charCode != '+' &&
1856 charCode != '=' && charCode != '\\'&& charCode != ']' &&
1857 charCode != '}' && charCode != '[' && charCode != '{' &&
1858 charCode != '/' && charCode != '?' && charCode != '>' &&
1859 charCode != '<' && charCode != ',' && charCode != '~')
1860 return 0;
1862 /* update the search parameters */
1863 prevTime = infoPtr->lastKeyPressTimestamp;
1864 infoPtr->lastKeyPressTimestamp = GetTickCount();
1865 diff = infoPtr->lastKeyPressTimestamp - prevTime;
1867 if (diff >= 0 && diff < KEY_DELAY)
1869 if (infoPtr->nSearchParamLength < MAX_PATH - 1)
1870 infoPtr->szSearchParam[infoPtr->nSearchParamLength++] = charCode;
1872 if (infoPtr->charCode != charCode)
1873 infoPtr->charCode = charCode = 0;
1875 else
1877 infoPtr->charCode = charCode;
1878 infoPtr->szSearchParam[0] = charCode;
1879 infoPtr->nSearchParamLength = 1;
1882 /* should start from next after focused item, so next item that matches
1883 will be selected, if there isn't any and focused matches it will be selected
1884 on second search stage from beginning of the list */
1885 if (infoPtr->nFocusedItem >= 0 && infoPtr->nItemCount > 1)
1887 /* with some accumulated search data available start with current focus, otherwise
1888 it's excluded from search */
1889 startidx = infoPtr->nSearchParamLength > 1 ? infoPtr->nFocusedItem : infoPtr->nFocusedItem + 1;
1890 if (startidx == infoPtr->nItemCount) startidx = 0;
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, endidx;
1913 /* and search from the current position */
1914 nItem = -1;
1915 endidx = infoPtr->nItemCount;
1917 /* first search in [startidx, endidx), on failure continue in [0, startidx) */
1918 while (1)
1920 /* start from first item if not found with >= startidx */
1921 if (i == infoPtr->nItemCount && startidx > 0)
1923 endidx = startidx;
1924 startidx = 0;
1927 for (i = startidx; i < endidx; i++)
1929 /* retrieve text */
1930 item.mask = LVIF_TEXT;
1931 item.iItem = i;
1932 item.iSubItem = 0;
1933 item.pszText = buffer;
1934 item.cchTextMax = MAX_PATH;
1935 if (!LISTVIEW_GetItemW(infoPtr, &item)) return 0;
1937 if (!lstrncmpiW(item.pszText, infoPtr->szSearchParam, infoPtr->nSearchParamLength))
1939 nItem = i;
1940 break;
1942 /* this is used to find first char match when search string is not available yet,
1943 otherwise every WM_CHAR will search to next item by first char, ignoring that we're
1944 already waiting for user to complete a string */
1945 else if (nItem == -1 && infoPtr->nSearchParamLength == 1 && !lstrncmpiW(item.pszText, infoPtr->szSearchParam, 1))
1947 /* this would work but we must keep looking for a longer match */
1948 nItem = i;
1952 if ( nItem != -1 || /* found something */
1953 endidx != infoPtr->nItemCount || /* second search done */
1954 (startidx == 0 && endidx == infoPtr->nItemCount) /* full range for first search */ )
1955 break;
1959 if (nItem != -1)
1960 LISTVIEW_KeySelection(infoPtr, nItem, FALSE);
1962 return 0;
1965 /*************************************************************************
1966 * LISTVIEW_UpdateHeaderSize [Internal]
1968 * Function to resize the header control
1970 * PARAMS
1971 * [I] hwnd : handle to a window
1972 * [I] nNewScrollPos : scroll pos to set
1974 * RETURNS
1975 * None.
1977 static void LISTVIEW_UpdateHeaderSize(const LISTVIEW_INFO *infoPtr, INT nNewScrollPos)
1979 RECT winRect;
1980 POINT point[2];
1982 TRACE("nNewScrollPos=%d\n", nNewScrollPos);
1984 if (!infoPtr->hwndHeader) return;
1986 GetWindowRect(infoPtr->hwndHeader, &winRect);
1987 point[0].x = winRect.left;
1988 point[0].y = winRect.top;
1989 point[1].x = winRect.right;
1990 point[1].y = winRect.bottom;
1992 MapWindowPoints(HWND_DESKTOP, infoPtr->hwndSelf, point, 2);
1993 point[0].x = -nNewScrollPos;
1994 point[1].x += nNewScrollPos;
1996 SetWindowPos(infoPtr->hwndHeader,0,
1997 point[0].x,point[0].y,point[1].x,point[1].y,
1998 (infoPtr->dwStyle & LVS_NOCOLUMNHEADER) ? SWP_HIDEWINDOW : SWP_SHOWWINDOW |
1999 SWP_NOZORDER | SWP_NOACTIVATE);
2002 /***
2003 * DESCRIPTION:
2004 * Update the scrollbars. This functions should be called whenever
2005 * the content, size or view changes.
2007 * PARAMETER(S):
2008 * [I] infoPtr : valid pointer to the listview structure
2010 * RETURN:
2011 * None
2013 static void LISTVIEW_UpdateScroll(const LISTVIEW_INFO *infoPtr)
2015 SCROLLINFO horzInfo, vertInfo;
2016 INT dx, dy;
2018 if ((infoPtr->dwStyle & LVS_NOSCROLL) || !is_redrawing(infoPtr)) return;
2020 ZeroMemory(&horzInfo, sizeof(SCROLLINFO));
2021 horzInfo.cbSize = sizeof(SCROLLINFO);
2022 horzInfo.nPage = infoPtr->rcList.right - infoPtr->rcList.left;
2024 /* for now, we'll set info.nMax to the _count_, and adjust it later */
2025 if (infoPtr->uView == LV_VIEW_LIST)
2027 INT nPerCol = LISTVIEW_GetCountPerColumn(infoPtr);
2028 horzInfo.nMax = (infoPtr->nItemCount + nPerCol - 1) / nPerCol;
2030 /* scroll by at least one column per page */
2031 if(horzInfo.nPage < infoPtr->nItemWidth)
2032 horzInfo.nPage = infoPtr->nItemWidth;
2034 if (infoPtr->nItemWidth)
2035 horzInfo.nPage /= infoPtr->nItemWidth;
2037 else if (infoPtr->uView == LV_VIEW_DETAILS)
2039 horzInfo.nMax = infoPtr->nItemWidth;
2041 else /* LV_VIEW_ICON, or LV_VIEW_SMALLICON */
2043 RECT rcView;
2045 if (LISTVIEW_GetViewRect(infoPtr, &rcView)) horzInfo.nMax = rcView.right - rcView.left;
2048 if (LISTVIEW_IsHeaderEnabled(infoPtr))
2050 if (DPA_GetPtrCount(infoPtr->hdpaColumns))
2052 RECT rcHeader;
2053 INT index;
2055 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX,
2056 DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, 0);
2058 LISTVIEW_GetHeaderRect(infoPtr, index, &rcHeader);
2059 horzInfo.nMax = rcHeader.right;
2060 TRACE("horzInfo.nMax=%d\n", horzInfo.nMax);
2064 horzInfo.fMask = SIF_RANGE | SIF_PAGE;
2065 horzInfo.nMax = max(horzInfo.nMax - 1, 0);
2066 dx = GetScrollPos(infoPtr->hwndSelf, SB_HORZ);
2067 dx -= SetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &horzInfo, TRUE);
2068 TRACE("horzInfo=%s\n", debugscrollinfo(&horzInfo));
2070 /* Setting the horizontal scroll can change the listview size
2071 * (and potentially everything else) so we need to recompute
2072 * everything again for the vertical scroll
2075 ZeroMemory(&vertInfo, sizeof(SCROLLINFO));
2076 vertInfo.cbSize = sizeof(SCROLLINFO);
2077 vertInfo.nPage = infoPtr->rcList.bottom - infoPtr->rcList.top;
2079 if (infoPtr->uView == LV_VIEW_DETAILS)
2081 vertInfo.nMax = infoPtr->nItemCount;
2083 /* scroll by at least one page */
2084 if(vertInfo.nPage < infoPtr->nItemHeight)
2085 vertInfo.nPage = infoPtr->nItemHeight;
2087 if (infoPtr->nItemHeight > 0)
2088 vertInfo.nPage /= infoPtr->nItemHeight;
2090 else if (infoPtr->uView != LV_VIEW_LIST) /* LV_VIEW_ICON, or LV_VIEW_SMALLICON */
2092 RECT rcView;
2094 if (LISTVIEW_GetViewRect(infoPtr, &rcView)) vertInfo.nMax = rcView.bottom - rcView.top;
2097 vertInfo.fMask = SIF_RANGE | SIF_PAGE;
2098 vertInfo.nMax = max(vertInfo.nMax - 1, 0);
2099 dy = GetScrollPos(infoPtr->hwndSelf, SB_VERT);
2100 dy -= SetScrollInfo(infoPtr->hwndSelf, SB_VERT, &vertInfo, TRUE);
2101 TRACE("vertInfo=%s\n", debugscrollinfo(&vertInfo));
2103 /* Change of the range may have changed the scroll pos. If so move the content */
2104 if (dx != 0 || dy != 0)
2106 RECT listRect;
2107 listRect = infoPtr->rcList;
2108 ScrollWindowEx(infoPtr->hwndSelf, dx, dy, &listRect, &listRect, 0, 0,
2109 SW_ERASE | SW_INVALIDATE);
2112 /* Update the Header Control */
2113 if (infoPtr->hwndHeader)
2115 horzInfo.fMask = SIF_POS;
2116 GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &horzInfo);
2117 LISTVIEW_UpdateHeaderSize(infoPtr, horzInfo.nPos);
2122 /***
2123 * DESCRIPTION:
2124 * Shows/hides the focus rectangle.
2126 * PARAMETER(S):
2127 * [I] infoPtr : valid pointer to the listview structure
2128 * [I] fShow : TRUE to show the focus, FALSE to hide it.
2130 * RETURN:
2131 * None
2133 static void LISTVIEW_ShowFocusRect(const LISTVIEW_INFO *infoPtr, BOOL fShow)
2135 HDC hdc;
2137 TRACE("fShow=%d, nItem=%d\n", fShow, infoPtr->nFocusedItem);
2139 if (infoPtr->nFocusedItem < 0) return;
2141 /* we need some gymnastics in ICON mode to handle large items */
2142 if (infoPtr->uView == LV_VIEW_ICON)
2144 RECT rcBox;
2146 LISTVIEW_GetItemBox(infoPtr, infoPtr->nFocusedItem, &rcBox);
2147 if ((rcBox.bottom - rcBox.top) > infoPtr->nItemHeight)
2149 LISTVIEW_InvalidateRect(infoPtr, &rcBox);
2150 return;
2154 if (!(hdc = GetDC(infoPtr->hwndSelf))) return;
2156 /* for some reason, owner draw should work only in report mode */
2157 if ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (infoPtr->uView == LV_VIEW_DETAILS))
2159 DRAWITEMSTRUCT dis;
2160 LVITEMW item;
2162 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
2163 HFONT hOldFont = SelectObject(hdc, hFont);
2165 item.iItem = infoPtr->nFocusedItem;
2166 item.iSubItem = 0;
2167 item.mask = LVIF_PARAM;
2168 if (!LISTVIEW_GetItemW(infoPtr, &item)) goto done;
2170 ZeroMemory(&dis, sizeof(dis));
2171 dis.CtlType = ODT_LISTVIEW;
2172 dis.CtlID = (UINT)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
2173 dis.itemID = item.iItem;
2174 dis.itemAction = ODA_FOCUS;
2175 if (fShow) dis.itemState |= ODS_FOCUS;
2176 dis.hwndItem = infoPtr->hwndSelf;
2177 dis.hDC = hdc;
2178 LISTVIEW_GetItemBox(infoPtr, dis.itemID, &dis.rcItem);
2179 dis.itemData = item.lParam;
2181 SendMessageW(infoPtr->hwndNotify, WM_DRAWITEM, dis.CtlID, (LPARAM)&dis);
2183 SelectObject(hdc, hOldFont);
2185 else
2187 LISTVIEW_DrawFocusRect(infoPtr, hdc);
2189 done:
2190 ReleaseDC(infoPtr->hwndSelf, hdc);
2193 /***
2194 * Invalidates all visible selected items.
2196 static void LISTVIEW_InvalidateSelectedItems(const LISTVIEW_INFO *infoPtr)
2198 ITERATOR i;
2200 iterator_frameditems(&i, infoPtr, &infoPtr->rcList);
2201 while(iterator_next(&i))
2203 if (LISTVIEW_GetItemState(infoPtr, i.nItem, LVIS_SELECTED))
2204 LISTVIEW_InvalidateItem(infoPtr, i.nItem);
2206 iterator_destroy(&i);
2210 /***
2211 * DESCRIPTION: [INTERNAL]
2212 * Computes an item's (left,top) corner, relative to rcView.
2213 * That is, the position has NOT been made relative to the Origin.
2214 * This is deliberate, to avoid computing the Origin over, and
2215 * over again, when this function is called in a loop. Instead,
2216 * one can factor the computation of the Origin before the loop,
2217 * and offset the value returned by this function, on every iteration.
2219 * PARAMETER(S):
2220 * [I] infoPtr : valid pointer to the listview structure
2221 * [I] nItem : item number
2222 * [O] lpptOrig : item top, left corner
2224 * RETURN:
2225 * None.
2227 static void LISTVIEW_GetItemOrigin(const LISTVIEW_INFO *infoPtr, INT nItem, LPPOINT lpptPosition)
2229 assert(nItem >= 0 && nItem < infoPtr->nItemCount);
2231 if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
2233 lpptPosition->x = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosX, nItem);
2234 lpptPosition->y = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosY, nItem);
2236 else if (infoPtr->uView == LV_VIEW_LIST)
2238 INT nCountPerColumn = LISTVIEW_GetCountPerColumn(infoPtr);
2239 lpptPosition->x = nItem / nCountPerColumn * infoPtr->nItemWidth;
2240 lpptPosition->y = nItem % nCountPerColumn * infoPtr->nItemHeight;
2242 else /* LV_VIEW_DETAILS */
2244 lpptPosition->x = REPORT_MARGINX;
2245 /* item is always at zero indexed column */
2246 if (DPA_GetPtrCount(infoPtr->hdpaColumns) > 0)
2247 lpptPosition->x += LISTVIEW_GetColumnInfo(infoPtr, 0)->rcHeader.left;
2248 lpptPosition->y = nItem * infoPtr->nItemHeight;
2252 /***
2253 * DESCRIPTION: [INTERNAL]
2254 * Compute the rectangles of an item. This is to localize all
2255 * the computations in one place. If you are not interested in some
2256 * of these values, simply pass in a NULL -- the function is smart
2257 * enough to compute only what's necessary. The function computes
2258 * the standard rectangles (BOUNDS, ICON, LABEL) plus a non-standard
2259 * one, the BOX rectangle. This rectangle is very cheap to compute,
2260 * and is guaranteed to contain all the other rectangles. Computing
2261 * the ICON rect is also cheap, but all the others are potentially
2262 * expensive. This gives an easy and effective optimization when
2263 * searching (like point inclusion, or rectangle intersection):
2264 * first test against the BOX, and if TRUE, test against the desired
2265 * rectangle.
2266 * If the function does not have all the necessary information
2267 * to computed the requested rectangles, will crash with a
2268 * failed assertion. This is done so we catch all programming
2269 * errors, given that the function is called only from our code.
2271 * We have the following 'special' meanings for a few fields:
2272 * * If LVIS_FOCUSED is set, we assume the item has the focus
2273 * This is important in ICON mode, where it might get a larger
2274 * then usual rectangle
2276 * Please note that subitem support works only in REPORT mode.
2278 * PARAMETER(S):
2279 * [I] infoPtr : valid pointer to the listview structure
2280 * [I] lpLVItem : item to compute the measures for
2281 * [O] lprcBox : ptr to Box rectangle
2282 * Same as LVM_GETITEMRECT with LVIR_BOUNDS
2283 * [0] lprcSelectBox : ptr to select box rectangle
2284 * Same as LVM_GETITEMRECT with LVIR_SELECTEDBOUNDS
2285 * [O] lprcIcon : ptr to Icon rectangle
2286 * Same as LVM_GETITEMRECT with LVIR_ICON
2287 * [O] lprcStateIcon: ptr to State Icon rectangle
2288 * [O] lprcLabel : ptr to Label rectangle
2289 * Same as LVM_GETITEMRECT with LVIR_LABEL
2291 * RETURN:
2292 * None.
2294 static void LISTVIEW_GetItemMetrics(const LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem,
2295 LPRECT lprcBox, LPRECT lprcSelectBox,
2296 LPRECT lprcIcon, LPRECT lprcStateIcon, LPRECT lprcLabel)
2298 BOOL doSelectBox = FALSE, doIcon = FALSE, doLabel = FALSE, oversizedBox = FALSE;
2299 RECT Box, SelectBox, Icon, Label;
2300 COLUMN_INFO *lpColumnInfo = NULL;
2301 SIZE labelSize = { 0, 0 };
2303 TRACE("(lpLVItem=%s)\n", debuglvitem_t(lpLVItem, TRUE));
2305 /* Be smart and try to figure out the minimum we have to do */
2306 if (lpLVItem->iSubItem) assert(infoPtr->uView == LV_VIEW_DETAILS);
2307 if (infoPtr->uView == LV_VIEW_ICON && (lprcBox || lprcLabel))
2309 assert((lpLVItem->mask & LVIF_STATE) && (lpLVItem->stateMask & LVIS_FOCUSED));
2310 if (lpLVItem->state & LVIS_FOCUSED) oversizedBox = doLabel = TRUE;
2312 if (lprcSelectBox) doSelectBox = TRUE;
2313 if (lprcLabel) doLabel = TRUE;
2314 if (doLabel || lprcIcon || lprcStateIcon) doIcon = TRUE;
2315 if (doSelectBox)
2317 doIcon = TRUE;
2318 doLabel = TRUE;
2321 /************************************************************/
2322 /* compute the box rectangle (it should be cheap to do) */
2323 /************************************************************/
2324 if (lpLVItem->iSubItem || infoPtr->uView == LV_VIEW_DETAILS)
2325 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, lpLVItem->iSubItem);
2327 if (lpLVItem->iSubItem)
2329 Box = lpColumnInfo->rcHeader;
2331 else
2333 Box.left = 0;
2334 Box.right = infoPtr->nItemWidth;
2336 Box.top = 0;
2337 Box.bottom = infoPtr->nItemHeight;
2339 /******************************************************************/
2340 /* compute ICON bounding box (ala LVM_GETITEMRECT) and STATEICON */
2341 /******************************************************************/
2342 if (doIcon)
2344 LONG state_width = 0;
2346 if (infoPtr->himlState && lpLVItem->iSubItem == 0)
2347 state_width = infoPtr->iconStateSize.cx;
2349 if (infoPtr->uView == LV_VIEW_ICON)
2351 Icon.left = Box.left + state_width;
2352 if (infoPtr->himlNormal)
2353 Icon.left += (infoPtr->nItemWidth - infoPtr->iconSize.cx - state_width) / 2;
2354 Icon.top = Box.top + ICON_TOP_PADDING;
2355 Icon.right = Icon.left;
2356 Icon.bottom = Icon.top;
2357 if (infoPtr->himlNormal)
2359 Icon.right += infoPtr->iconSize.cx;
2360 Icon.bottom += infoPtr->iconSize.cy;
2363 else /* LV_VIEW_SMALLICON, LV_VIEW_LIST or LV_VIEW_DETAILS */
2365 Icon.left = Box.left + state_width;
2367 if (infoPtr->uView == LV_VIEW_DETAILS && lpLVItem->iSubItem == 0)
2369 /* we need the indent in report mode */
2370 assert(lpLVItem->mask & LVIF_INDENT);
2371 Icon.left += infoPtr->iconSize.cx * lpLVItem->iIndent + REPORT_MARGINX;
2374 Icon.top = Box.top;
2375 Icon.right = Icon.left;
2376 if (infoPtr->himlSmall &&
2377 (!lpColumnInfo || lpLVItem->iSubItem == 0 ||
2378 ((infoPtr->dwLvExStyle & LVS_EX_SUBITEMIMAGES) && lpLVItem->iImage != I_IMAGECALLBACK)))
2379 Icon.right += infoPtr->iconSize.cx;
2380 Icon.bottom = Icon.top + infoPtr->iconSize.cy;
2382 if(lprcIcon) *lprcIcon = Icon;
2383 TRACE(" - icon=%s\n", wine_dbgstr_rect(&Icon));
2385 /* TODO: is this correct? */
2386 if (lprcStateIcon)
2388 lprcStateIcon->left = Icon.left - state_width;
2389 lprcStateIcon->right = Icon.left;
2390 lprcStateIcon->top = Icon.top;
2391 lprcStateIcon->bottom = lprcStateIcon->top + infoPtr->iconSize.cy;
2392 TRACE(" - state icon=%s\n", wine_dbgstr_rect(lprcStateIcon));
2395 else Icon.right = 0;
2397 /************************************************************/
2398 /* compute LABEL bounding box (ala LVM_GETITEMRECT) */
2399 /************************************************************/
2400 if (doLabel)
2402 /* calculate how far to the right can the label stretch */
2403 Label.right = Box.right;
2404 if (infoPtr->uView == LV_VIEW_DETAILS)
2406 if (lpLVItem->iSubItem == 0)
2408 /* we need a zero based rect here */
2409 Label = lpColumnInfo->rcHeader;
2410 OffsetRect(&Label, -Label.left, 0);
2414 if (lpLVItem->iSubItem || ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && infoPtr->uView == LV_VIEW_DETAILS))
2416 labelSize.cx = infoPtr->nItemWidth;
2417 labelSize.cy = infoPtr->nItemHeight;
2418 goto calc_label;
2421 /* we need the text in non owner draw mode */
2422 assert(lpLVItem->mask & LVIF_TEXT);
2423 if (is_text(lpLVItem->pszText))
2425 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
2426 HDC hdc = GetDC(infoPtr->hwndSelf);
2427 HFONT hOldFont = SelectObject(hdc, hFont);
2428 UINT uFormat;
2429 RECT rcText;
2431 /* compute rough rectangle where the label will go */
2432 SetRectEmpty(&rcText);
2433 rcText.right = infoPtr->nItemWidth - TRAILING_LABEL_PADDING;
2434 rcText.bottom = infoPtr->nItemHeight;
2435 if (infoPtr->uView == LV_VIEW_ICON)
2436 rcText.bottom -= ICON_TOP_PADDING + infoPtr->iconSize.cy + ICON_BOTTOM_PADDING;
2438 /* now figure out the flags */
2439 if (infoPtr->uView == LV_VIEW_ICON)
2440 uFormat = oversizedBox ? LV_FL_DT_FLAGS : LV_ML_DT_FLAGS;
2441 else
2442 uFormat = LV_SL_DT_FLAGS;
2444 DrawTextW (hdc, lpLVItem->pszText, -1, &rcText, uFormat | DT_CALCRECT);
2446 if (rcText.right != rcText.left)
2447 labelSize.cx = min(rcText.right - rcText.left + TRAILING_LABEL_PADDING, infoPtr->nItemWidth);
2449 labelSize.cy = rcText.bottom - rcText.top;
2451 SelectObject(hdc, hOldFont);
2452 ReleaseDC(infoPtr->hwndSelf, hdc);
2455 calc_label:
2456 if (infoPtr->uView == LV_VIEW_ICON)
2458 Label.left = Box.left + (infoPtr->nItemWidth - labelSize.cx) / 2;
2459 Label.top = Box.top + ICON_TOP_PADDING_HITABLE +
2460 infoPtr->iconSize.cy + ICON_BOTTOM_PADDING;
2461 Label.right = Label.left + labelSize.cx;
2462 Label.bottom = Label.top + infoPtr->nItemHeight;
2463 if (!oversizedBox && labelSize.cy > infoPtr->ntmHeight)
2465 labelSize.cy = min(Box.bottom - Label.top, labelSize.cy);
2466 labelSize.cy /= infoPtr->ntmHeight;
2467 labelSize.cy = max(labelSize.cy, 1);
2468 labelSize.cy *= infoPtr->ntmHeight;
2470 Label.bottom = Label.top + labelSize.cy + HEIGHT_PADDING;
2472 else if (infoPtr->uView == LV_VIEW_DETAILS)
2474 Label.left = Icon.right;
2475 Label.top = Box.top;
2476 Label.right = lpLVItem->iSubItem ? lpColumnInfo->rcHeader.right :
2477 lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left;
2478 Label.bottom = Label.top + infoPtr->nItemHeight;
2480 else /* LV_VIEW_SMALLICON or LV_VIEW_LIST */
2482 Label.left = Icon.right;
2483 Label.top = Box.top;
2484 Label.right = min(Label.left + labelSize.cx, Label.right);
2485 Label.bottom = Label.top + infoPtr->nItemHeight;
2488 if (lprcLabel) *lprcLabel = Label;
2489 TRACE(" - label=%s\n", wine_dbgstr_rect(&Label));
2492 /************************************************************/
2493 /* compute SELECT bounding box */
2494 /************************************************************/
2495 if (doSelectBox)
2497 if (infoPtr->uView == LV_VIEW_DETAILS)
2499 SelectBox.left = Icon.left;
2500 SelectBox.top = Box.top;
2501 SelectBox.bottom = Box.bottom;
2503 if (labelSize.cx)
2504 SelectBox.right = min(Label.left + labelSize.cx, Label.right);
2505 else
2506 SelectBox.right = min(Label.left + MAX_EMPTYTEXT_SELECT_WIDTH, Label.right);
2508 else
2510 UnionRect(&SelectBox, &Icon, &Label);
2512 if (lprcSelectBox) *lprcSelectBox = SelectBox;
2513 TRACE(" - select box=%s\n", wine_dbgstr_rect(&SelectBox));
2516 /* Fix the Box if necessary */
2517 if (lprcBox)
2519 if (oversizedBox) UnionRect(lprcBox, &Box, &Label);
2520 else *lprcBox = Box;
2522 TRACE(" - box=%s\n", wine_dbgstr_rect(&Box));
2525 /***
2526 * DESCRIPTION: [INTERNAL]
2528 * PARAMETER(S):
2529 * [I] infoPtr : valid pointer to the listview structure
2530 * [I] nItem : item number
2531 * [O] lprcBox : ptr to Box rectangle
2533 * RETURN:
2534 * None.
2536 static void LISTVIEW_GetItemBox(const LISTVIEW_INFO *infoPtr, INT nItem, LPRECT lprcBox)
2538 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
2539 POINT Position, Origin;
2540 LVITEMW lvItem;
2542 LISTVIEW_GetOrigin(infoPtr, &Origin);
2543 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
2545 /* Be smart and try to figure out the minimum we have to do */
2546 lvItem.mask = 0;
2547 if (infoPtr->uView == LV_VIEW_ICON && infoPtr->bFocus && LISTVIEW_GetItemState(infoPtr, nItem, LVIS_FOCUSED))
2548 lvItem.mask |= LVIF_TEXT;
2549 lvItem.iItem = nItem;
2550 lvItem.iSubItem = 0;
2551 lvItem.pszText = szDispText;
2552 lvItem.cchTextMax = DISP_TEXT_SIZE;
2553 if (lvItem.mask) LISTVIEW_GetItemW(infoPtr, &lvItem);
2554 if (infoPtr->uView == LV_VIEW_ICON)
2556 lvItem.mask |= LVIF_STATE;
2557 lvItem.stateMask = LVIS_FOCUSED;
2558 lvItem.state = (lvItem.mask & LVIF_TEXT ? LVIS_FOCUSED : 0);
2560 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, lprcBox, 0, 0, 0, 0);
2562 if (infoPtr->uView == LV_VIEW_DETAILS && infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT &&
2563 SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, 0, 0))
2565 OffsetRect(lprcBox, Origin.x, Position.y + Origin.y);
2567 else
2568 OffsetRect(lprcBox, Position.x + Origin.x, Position.y + Origin.y);
2571 /* LISTVIEW_MapIdToIndex helper */
2572 static INT CALLBACK MapIdSearchCompare(LPVOID p1, LPVOID p2, LPARAM lParam)
2574 ITEM_ID *id1 = (ITEM_ID*)p1;
2575 ITEM_ID *id2 = (ITEM_ID*)p2;
2577 if (id1->id == id2->id) return 0;
2579 return (id1->id < id2->id) ? -1 : 1;
2582 /***
2583 * DESCRIPTION:
2584 * Returns the item index for id specified.
2586 * PARAMETER(S):
2587 * [I] infoPtr : valid pointer to the listview structure
2588 * [I] iID : item id to get index for
2590 * RETURN:
2591 * Item index, or -1 on failure.
2593 static INT LISTVIEW_MapIdToIndex(const LISTVIEW_INFO *infoPtr, UINT iID)
2595 ITEM_ID ID;
2596 INT index;
2598 TRACE("iID=%d\n", iID);
2600 if (infoPtr->dwStyle & LVS_OWNERDATA) return -1;
2601 if (infoPtr->nItemCount == 0) return -1;
2603 ID.id = iID;
2604 index = DPA_Search(infoPtr->hdpaItemIds, &ID, -1, MapIdSearchCompare, 0, DPAS_SORTED);
2606 if (index != -1)
2608 ITEM_ID *lpID = DPA_GetPtr(infoPtr->hdpaItemIds, index);
2609 return DPA_GetPtrIndex(infoPtr->hdpaItems, lpID->item);
2612 return -1;
2615 /***
2616 * DESCRIPTION:
2617 * Returns the item id for index given.
2619 * PARAMETER(S):
2620 * [I] infoPtr : valid pointer to the listview structure
2621 * [I] iItem : item index to get id for
2623 * RETURN:
2624 * Item id.
2626 static DWORD LISTVIEW_MapIndexToId(const LISTVIEW_INFO *infoPtr, INT iItem)
2628 ITEM_INFO *lpItem;
2629 HDPA hdpaSubItems;
2631 TRACE("iItem=%d\n", iItem);
2633 if (infoPtr->dwStyle & LVS_OWNERDATA) return -1;
2634 if (iItem < 0 || iItem >= infoPtr->nItemCount) return -1;
2636 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, iItem);
2637 lpItem = DPA_GetPtr(hdpaSubItems, 0);
2639 return lpItem->id->id;
2642 /***
2643 * DESCRIPTION:
2644 * Returns the current icon position, and advances it along the top.
2645 * The returned position is not offset by Origin.
2647 * PARAMETER(S):
2648 * [I] infoPtr : valid pointer to the listview structure
2649 * [O] lpPos : will get the current icon position
2651 * RETURN:
2652 * None
2654 static void LISTVIEW_NextIconPosTop(LISTVIEW_INFO *infoPtr, LPPOINT lpPos)
2656 INT nListWidth = infoPtr->rcList.right - infoPtr->rcList.left;
2658 *lpPos = infoPtr->currIconPos;
2660 infoPtr->currIconPos.x += infoPtr->nItemWidth;
2661 if (infoPtr->currIconPos.x + infoPtr->nItemWidth <= nListWidth) return;
2663 infoPtr->currIconPos.x = 0;
2664 infoPtr->currIconPos.y += infoPtr->nItemHeight;
2668 /***
2669 * DESCRIPTION:
2670 * Returns the current icon position, and advances it down the left edge.
2671 * The returned position is not offset by Origin.
2673 * PARAMETER(S):
2674 * [I] infoPtr : valid pointer to the listview structure
2675 * [O] lpPos : will get the current icon position
2677 * RETURN:
2678 * None
2680 static void LISTVIEW_NextIconPosLeft(LISTVIEW_INFO *infoPtr, LPPOINT lpPos)
2682 INT nListHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
2684 *lpPos = infoPtr->currIconPos;
2686 infoPtr->currIconPos.y += infoPtr->nItemHeight;
2687 if (infoPtr->currIconPos.y + infoPtr->nItemHeight <= nListHeight) return;
2689 infoPtr->currIconPos.x += infoPtr->nItemWidth;
2690 infoPtr->currIconPos.y = 0;
2694 /***
2695 * DESCRIPTION:
2696 * Moves an icon to the specified position.
2697 * It takes care of invalidating the item, etc.
2699 * PARAMETER(S):
2700 * [I] infoPtr : valid pointer to the listview structure
2701 * [I] nItem : the item to move
2702 * [I] lpPos : the new icon position
2703 * [I] isNew : flags the item as being new
2705 * RETURN:
2706 * Success: TRUE
2707 * Failure: FALSE
2709 static BOOL LISTVIEW_MoveIconTo(const LISTVIEW_INFO *infoPtr, INT nItem, const POINT *lppt, BOOL isNew)
2711 POINT old;
2713 if (!isNew)
2715 old.x = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosX, nItem);
2716 old.y = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosY, nItem);
2718 if (lppt->x == old.x && lppt->y == old.y) return TRUE;
2719 LISTVIEW_InvalidateItem(infoPtr, nItem);
2722 /* Allocating a POINTER for every item is too resource intensive,
2723 * so we'll keep the (x,y) in different arrays */
2724 if (!DPA_SetPtr(infoPtr->hdpaPosX, nItem, (void *)(LONG_PTR)lppt->x)) return FALSE;
2725 if (!DPA_SetPtr(infoPtr->hdpaPosY, nItem, (void *)(LONG_PTR)lppt->y)) return FALSE;
2727 LISTVIEW_InvalidateItem(infoPtr, nItem);
2729 return TRUE;
2732 /***
2733 * DESCRIPTION:
2734 * Arranges listview items in icon display mode.
2736 * PARAMETER(S):
2737 * [I] infoPtr : valid pointer to the listview structure
2738 * [I] nAlignCode : alignment code
2740 * RETURN:
2741 * SUCCESS : TRUE
2742 * FAILURE : FALSE
2744 static BOOL LISTVIEW_Arrange(LISTVIEW_INFO *infoPtr, INT nAlignCode)
2746 void (*next_pos)(LISTVIEW_INFO *, LPPOINT);
2747 POINT pos;
2748 INT i;
2750 if (infoPtr->uView != LV_VIEW_ICON && infoPtr->uView != LV_VIEW_SMALLICON) return FALSE;
2752 TRACE("nAlignCode=%d\n", nAlignCode);
2754 if (nAlignCode == LVA_DEFAULT)
2756 if (infoPtr->dwStyle & LVS_ALIGNLEFT) nAlignCode = LVA_ALIGNLEFT;
2757 else nAlignCode = LVA_ALIGNTOP;
2760 switch (nAlignCode)
2762 case LVA_ALIGNLEFT: next_pos = LISTVIEW_NextIconPosLeft; break;
2763 case LVA_ALIGNTOP: next_pos = LISTVIEW_NextIconPosTop; break;
2764 case LVA_SNAPTOGRID: next_pos = LISTVIEW_NextIconPosTop; break; /* FIXME */
2765 default: return FALSE;
2768 infoPtr->bAutoarrange = TRUE;
2769 infoPtr->currIconPos.x = infoPtr->currIconPos.y = 0;
2770 for (i = 0; i < infoPtr->nItemCount; i++)
2772 next_pos(infoPtr, &pos);
2773 LISTVIEW_MoveIconTo(infoPtr, i, &pos, FALSE);
2776 return TRUE;
2779 /***
2780 * DESCRIPTION:
2781 * Retrieves the bounding rectangle of all the items, not offset by Origin.
2782 * For LVS_REPORT always returns empty rectangle.
2784 * PARAMETER(S):
2785 * [I] infoPtr : valid pointer to the listview structure
2786 * [O] lprcView : bounding rectangle
2788 * RETURN:
2789 * SUCCESS : TRUE
2790 * FAILURE : FALSE
2792 static void LISTVIEW_GetAreaRect(const LISTVIEW_INFO *infoPtr, LPRECT lprcView)
2794 INT i, x, y;
2796 SetRectEmpty(lprcView);
2798 switch (infoPtr->uView)
2800 case LV_VIEW_ICON:
2801 case LV_VIEW_SMALLICON:
2802 for (i = 0; i < infoPtr->nItemCount; i++)
2804 x = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosX, i);
2805 y = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosY, i);
2806 lprcView->right = max(lprcView->right, x);
2807 lprcView->bottom = max(lprcView->bottom, y);
2809 if (infoPtr->nItemCount > 0)
2811 lprcView->right += infoPtr->nItemWidth;
2812 lprcView->bottom += infoPtr->nItemHeight;
2814 break;
2816 case LV_VIEW_LIST:
2817 y = LISTVIEW_GetCountPerColumn(infoPtr);
2818 x = infoPtr->nItemCount / y;
2819 if (infoPtr->nItemCount % y) x++;
2820 lprcView->right = x * infoPtr->nItemWidth;
2821 lprcView->bottom = y * infoPtr->nItemHeight;
2822 break;
2826 /***
2827 * DESCRIPTION:
2828 * Retrieves the bounding rectangle of all the items.
2830 * PARAMETER(S):
2831 * [I] infoPtr : valid pointer to the listview structure
2832 * [O] lprcView : bounding rectangle
2834 * RETURN:
2835 * SUCCESS : TRUE
2836 * FAILURE : FALSE
2838 static BOOL LISTVIEW_GetViewRect(const LISTVIEW_INFO *infoPtr, LPRECT lprcView)
2840 POINT ptOrigin;
2842 TRACE("(lprcView=%p)\n", lprcView);
2844 if (!lprcView) return FALSE;
2846 LISTVIEW_GetAreaRect(infoPtr, lprcView);
2848 if (infoPtr->uView != LV_VIEW_DETAILS)
2850 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
2851 OffsetRect(lprcView, ptOrigin.x, ptOrigin.y);
2854 TRACE("lprcView=%s\n", wine_dbgstr_rect(lprcView));
2856 return TRUE;
2859 /***
2860 * DESCRIPTION:
2861 * Retrieves the subitem pointer associated with the subitem index.
2863 * PARAMETER(S):
2864 * [I] hdpaSubItems : DPA handle for a specific item
2865 * [I] nSubItem : index of subitem
2867 * RETURN:
2868 * SUCCESS : subitem pointer
2869 * FAILURE : NULL
2871 static SUBITEM_INFO* LISTVIEW_GetSubItemPtr(HDPA hdpaSubItems, INT nSubItem)
2873 SUBITEM_INFO *lpSubItem;
2874 INT i;
2876 /* we should binary search here if need be */
2877 for (i = 1; i < DPA_GetPtrCount(hdpaSubItems); i++)
2879 lpSubItem = DPA_GetPtr(hdpaSubItems, i);
2880 if (lpSubItem->iSubItem == nSubItem)
2881 return lpSubItem;
2884 return NULL;
2888 /***
2889 * DESCRIPTION:
2890 * Calculates the desired item width.
2892 * PARAMETER(S):
2893 * [I] infoPtr : valid pointer to the listview structure
2895 * RETURN:
2896 * The desired item width.
2898 static INT LISTVIEW_CalculateItemWidth(const LISTVIEW_INFO *infoPtr)
2900 INT nItemWidth = 0;
2902 TRACE("uView=%d\n", infoPtr->uView);
2904 if (infoPtr->uView == LV_VIEW_ICON)
2905 nItemWidth = infoPtr->iconSpacing.cx;
2906 else if (infoPtr->uView == LV_VIEW_DETAILS)
2908 if (DPA_GetPtrCount(infoPtr->hdpaColumns) > 0)
2910 RECT rcHeader;
2911 INT index;
2913 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX,
2914 DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, 0);
2916 LISTVIEW_GetHeaderRect(infoPtr, index, &rcHeader);
2917 nItemWidth = rcHeader.right;
2920 else /* LV_VIEW_SMALLICON, or LV_VIEW_LIST */
2922 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
2923 LVITEMW lvItem;
2924 INT i;
2926 lvItem.mask = LVIF_TEXT;
2927 lvItem.iSubItem = 0;
2929 for (i = 0; i < infoPtr->nItemCount; i++)
2931 lvItem.iItem = i;
2932 lvItem.pszText = szDispText;
2933 lvItem.cchTextMax = DISP_TEXT_SIZE;
2934 if (LISTVIEW_GetItemW(infoPtr, &lvItem))
2935 nItemWidth = max(LISTVIEW_GetStringWidthT(infoPtr, lvItem.pszText, TRUE),
2936 nItemWidth);
2939 if (infoPtr->himlSmall) nItemWidth += infoPtr->iconSize.cx;
2940 if (infoPtr->himlState) nItemWidth += infoPtr->iconStateSize.cx;
2942 nItemWidth = max(DEFAULT_COLUMN_WIDTH, nItemWidth + WIDTH_PADDING);
2945 return nItemWidth;
2948 /***
2949 * DESCRIPTION:
2950 * Calculates the desired item height.
2952 * PARAMETER(S):
2953 * [I] infoPtr : valid pointer to the listview structure
2955 * RETURN:
2956 * The desired item height.
2958 static INT LISTVIEW_CalculateItemHeight(const LISTVIEW_INFO *infoPtr)
2960 INT nItemHeight;
2962 TRACE("uView=%d\n", infoPtr->uView);
2964 if (infoPtr->uView == LV_VIEW_ICON)
2965 nItemHeight = infoPtr->iconSpacing.cy;
2966 else
2968 nItemHeight = infoPtr->ntmHeight;
2969 if (infoPtr->himlState)
2970 nItemHeight = max(nItemHeight, infoPtr->iconStateSize.cy);
2971 if (infoPtr->himlSmall)
2972 nItemHeight = max(nItemHeight, infoPtr->iconSize.cy);
2973 nItemHeight += HEIGHT_PADDING;
2974 if (infoPtr->nMeasureItemHeight > 0)
2975 nItemHeight = infoPtr->nMeasureItemHeight;
2978 return max(nItemHeight, 1);
2981 /***
2982 * DESCRIPTION:
2983 * Updates the width, and height of an item.
2985 * PARAMETER(S):
2986 * [I] infoPtr : valid pointer to the listview structure
2988 * RETURN:
2989 * None.
2991 static inline void LISTVIEW_UpdateItemSize(LISTVIEW_INFO *infoPtr)
2993 infoPtr->nItemWidth = LISTVIEW_CalculateItemWidth(infoPtr);
2994 infoPtr->nItemHeight = LISTVIEW_CalculateItemHeight(infoPtr);
2998 /***
2999 * DESCRIPTION:
3000 * Retrieves and saves important text metrics info for the current
3001 * Listview font.
3003 * PARAMETER(S):
3004 * [I] infoPtr : valid pointer to the listview structure
3007 static void LISTVIEW_SaveTextMetrics(LISTVIEW_INFO *infoPtr)
3009 HDC hdc = GetDC(infoPtr->hwndSelf);
3010 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
3011 HFONT hOldFont = SelectObject(hdc, hFont);
3012 TEXTMETRICW tm;
3013 SIZE sz;
3015 if (GetTextMetricsW(hdc, &tm))
3017 infoPtr->ntmHeight = tm.tmHeight;
3018 infoPtr->ntmMaxCharWidth = tm.tmMaxCharWidth;
3021 if (GetTextExtentPoint32A(hdc, "...", 3, &sz))
3022 infoPtr->nEllipsisWidth = sz.cx;
3024 SelectObject(hdc, hOldFont);
3025 ReleaseDC(infoPtr->hwndSelf, hdc);
3027 TRACE("tmHeight=%d\n", infoPtr->ntmHeight);
3030 /***
3031 * DESCRIPTION:
3032 * A compare function for ranges
3034 * PARAMETER(S)
3035 * [I] range1 : pointer to range 1;
3036 * [I] range2 : pointer to range 2;
3037 * [I] flags : flags
3039 * RETURNS:
3040 * > 0 : if range 1 > range 2
3041 * < 0 : if range 2 > range 1
3042 * = 0 : if range intersects range 2
3044 static INT CALLBACK ranges_cmp(LPVOID range1, LPVOID range2, LPARAM flags)
3046 INT cmp;
3048 if (((RANGE*)range1)->upper <= ((RANGE*)range2)->lower)
3049 cmp = -1;
3050 else if (((RANGE*)range2)->upper <= ((RANGE*)range1)->lower)
3051 cmp = 1;
3052 else
3053 cmp = 0;
3055 TRACE("range1=%s, range2=%s, cmp=%d\n", debugrange(range1), debugrange(range2), cmp);
3057 return cmp;
3060 #define ranges_check(ranges, desc) if (TRACE_ON(listview)) ranges_assert(ranges, desc, __FILE__, __LINE__)
3062 static void ranges_assert(RANGES ranges, LPCSTR desc, const char *file, int line)
3064 INT i;
3065 RANGE *prev, *curr;
3067 TRACE("*** Checking %s:%d:%s ***\n", file, line, desc);
3068 assert (ranges);
3069 assert (DPA_GetPtrCount(ranges->hdpa) >= 0);
3070 ranges_dump(ranges);
3071 if (DPA_GetPtrCount(ranges->hdpa) > 0)
3073 prev = DPA_GetPtr(ranges->hdpa, 0);
3074 assert (prev->lower >= 0 && prev->lower < prev->upper);
3075 for (i = 1; i < DPA_GetPtrCount(ranges->hdpa); i++)
3077 curr = DPA_GetPtr(ranges->hdpa, i);
3078 assert (prev->upper <= curr->lower);
3079 assert (curr->lower < curr->upper);
3080 prev = curr;
3083 TRACE("--- Done checking---\n");
3086 static RANGES ranges_create(int count)
3088 RANGES ranges = Alloc(sizeof(struct tagRANGES));
3089 if (!ranges) return NULL;
3090 ranges->hdpa = DPA_Create(count);
3091 if (ranges->hdpa) return ranges;
3092 Free(ranges);
3093 return NULL;
3096 static void ranges_clear(RANGES ranges)
3098 INT i;
3100 for(i = 0; i < DPA_GetPtrCount(ranges->hdpa); i++)
3101 Free(DPA_GetPtr(ranges->hdpa, i));
3102 DPA_DeleteAllPtrs(ranges->hdpa);
3106 static void ranges_destroy(RANGES ranges)
3108 if (!ranges) return;
3109 ranges_clear(ranges);
3110 DPA_Destroy(ranges->hdpa);
3111 Free(ranges);
3114 static RANGES ranges_clone(RANGES ranges)
3116 RANGES clone;
3117 INT i;
3119 if (!(clone = ranges_create(DPA_GetPtrCount(ranges->hdpa)))) goto fail;
3121 for (i = 0; i < DPA_GetPtrCount(ranges->hdpa); i++)
3123 RANGE *newrng = Alloc(sizeof(RANGE));
3124 if (!newrng) goto fail;
3125 *newrng = *((RANGE*)DPA_GetPtr(ranges->hdpa, i));
3126 DPA_SetPtr(clone->hdpa, i, newrng);
3128 return clone;
3130 fail:
3131 TRACE ("clone failed\n");
3132 ranges_destroy(clone);
3133 return NULL;
3136 static RANGES ranges_diff(RANGES ranges, RANGES sub)
3138 INT i;
3140 for (i = 0; i < DPA_GetPtrCount(sub->hdpa); i++)
3141 ranges_del(ranges, *((RANGE *)DPA_GetPtr(sub->hdpa, i)));
3143 return ranges;
3146 static void ranges_dump(RANGES ranges)
3148 INT i;
3150 for (i = 0; i < DPA_GetPtrCount(ranges->hdpa); i++)
3151 TRACE(" %s\n", debugrange(DPA_GetPtr(ranges->hdpa, i)));
3154 static inline BOOL ranges_contain(RANGES ranges, INT nItem)
3156 RANGE srchrng = { nItem, nItem + 1 };
3158 TRACE("(nItem=%d)\n", nItem);
3159 ranges_check(ranges, "before contain");
3160 return DPA_Search(ranges->hdpa, &srchrng, 0, ranges_cmp, 0, DPAS_SORTED) != -1;
3163 static INT ranges_itemcount(RANGES ranges)
3165 INT i, count = 0;
3167 for (i = 0; i < DPA_GetPtrCount(ranges->hdpa); i++)
3169 RANGE *sel = DPA_GetPtr(ranges->hdpa, i);
3170 count += sel->upper - sel->lower;
3173 return count;
3176 static BOOL ranges_shift(RANGES ranges, INT nItem, INT delta, INT nUpper)
3178 RANGE srchrng = { nItem, nItem + 1 }, *chkrng;
3179 INT index;
3181 index = DPA_Search(ranges->hdpa, &srchrng, 0, ranges_cmp, 0, DPAS_SORTED | DPAS_INSERTAFTER);
3182 if (index == -1) return TRUE;
3184 for (; index < DPA_GetPtrCount(ranges->hdpa); index++)
3186 chkrng = DPA_GetPtr(ranges->hdpa, index);
3187 if (chkrng->lower >= nItem)
3188 chkrng->lower = max(min(chkrng->lower + delta, nUpper - 1), 0);
3189 if (chkrng->upper > nItem)
3190 chkrng->upper = max(min(chkrng->upper + delta, nUpper), 0);
3192 return TRUE;
3195 static BOOL ranges_add(RANGES ranges, RANGE range)
3197 RANGE srchrgn;
3198 INT index;
3200 TRACE("(%s)\n", debugrange(&range));
3201 ranges_check(ranges, "before add");
3203 /* try find overlapping regions first */
3204 srchrgn.lower = range.lower - 1;
3205 srchrgn.upper = range.upper + 1;
3206 index = DPA_Search(ranges->hdpa, &srchrgn, 0, ranges_cmp, 0, DPAS_SORTED);
3208 if (index == -1)
3210 RANGE *newrgn;
3212 TRACE("Adding new range\n");
3214 /* create the brand new range to insert */
3215 newrgn = Alloc(sizeof(RANGE));
3216 if(!newrgn) goto fail;
3217 *newrgn = range;
3219 /* figure out where to insert it */
3220 index = DPA_Search(ranges->hdpa, newrgn, 0, ranges_cmp, 0, DPAS_SORTED | DPAS_INSERTAFTER);
3221 TRACE("index=%d\n", index);
3222 if (index == -1) index = 0;
3224 /* and get it over with */
3225 if (DPA_InsertPtr(ranges->hdpa, index, newrgn) == -1)
3227 Free(newrgn);
3228 goto fail;
3231 else
3233 RANGE *chkrgn, *mrgrgn;
3234 INT fromindex, mergeindex;
3236 chkrgn = DPA_GetPtr(ranges->hdpa, index);
3237 TRACE("Merge with %s @%d\n", debugrange(chkrgn), index);
3239 chkrgn->lower = min(range.lower, chkrgn->lower);
3240 chkrgn->upper = max(range.upper, chkrgn->upper);
3242 TRACE("New range %s @%d\n", debugrange(chkrgn), index);
3244 /* merge now common ranges */
3245 fromindex = 0;
3246 srchrgn.lower = chkrgn->lower - 1;
3247 srchrgn.upper = chkrgn->upper + 1;
3251 mergeindex = DPA_Search(ranges->hdpa, &srchrgn, fromindex, ranges_cmp, 0, 0);
3252 if (mergeindex == -1) break;
3253 if (mergeindex == index)
3255 fromindex = index + 1;
3256 continue;
3259 TRACE("Merge with index %i\n", mergeindex);
3261 mrgrgn = DPA_GetPtr(ranges->hdpa, mergeindex);
3262 chkrgn->lower = min(chkrgn->lower, mrgrgn->lower);
3263 chkrgn->upper = max(chkrgn->upper, mrgrgn->upper);
3264 Free(mrgrgn);
3265 DPA_DeletePtr(ranges->hdpa, mergeindex);
3266 if (mergeindex < index) index --;
3267 } while(1);
3270 ranges_check(ranges, "after add");
3271 return TRUE;
3273 fail:
3274 ranges_check(ranges, "failed add");
3275 return FALSE;
3278 static BOOL ranges_del(RANGES ranges, RANGE range)
3280 RANGE *chkrgn;
3281 INT index;
3283 TRACE("(%s)\n", debugrange(&range));
3284 ranges_check(ranges, "before del");
3286 /* we don't use DPAS_SORTED here, since we need *
3287 * to find the first overlapping range */
3288 index = DPA_Search(ranges->hdpa, &range, 0, ranges_cmp, 0, 0);
3289 while(index != -1)
3291 chkrgn = DPA_GetPtr(ranges->hdpa, index);
3293 TRACE("Matches range %s @%d\n", debugrange(chkrgn), index);
3295 /* case 1: Same range */
3296 if ( (chkrgn->upper == range.upper) &&
3297 (chkrgn->lower == range.lower) )
3299 DPA_DeletePtr(ranges->hdpa, index);
3300 Free(chkrgn);
3301 break;
3303 /* case 2: engulf */
3304 else if ( (chkrgn->upper <= range.upper) &&
3305 (chkrgn->lower >= range.lower) )
3307 DPA_DeletePtr(ranges->hdpa, index);
3308 Free(chkrgn);
3310 /* case 3: overlap upper */
3311 else if ( (chkrgn->upper <= range.upper) &&
3312 (chkrgn->lower < range.lower) )
3314 chkrgn->upper = range.lower;
3316 /* case 4: overlap lower */
3317 else if ( (chkrgn->upper > range.upper) &&
3318 (chkrgn->lower >= range.lower) )
3320 chkrgn->lower = range.upper;
3321 break;
3323 /* case 5: fully internal */
3324 else
3326 RANGE *newrgn;
3328 if (!(newrgn = Alloc(sizeof(RANGE)))) goto fail;
3329 newrgn->lower = chkrgn->lower;
3330 newrgn->upper = range.lower;
3331 chkrgn->lower = range.upper;
3332 if (DPA_InsertPtr(ranges->hdpa, index, newrgn) == -1)
3334 Free(newrgn);
3335 goto fail;
3337 break;
3340 index = DPA_Search(ranges->hdpa, &range, index, ranges_cmp, 0, 0);
3343 ranges_check(ranges, "after del");
3344 return TRUE;
3346 fail:
3347 ranges_check(ranges, "failed del");
3348 return FALSE;
3351 /***
3352 * DESCRIPTION:
3353 * Removes all selection ranges
3355 * Parameters(s):
3356 * [I] infoPtr : valid pointer to the listview structure
3357 * [I] toSkip : item range to skip removing the selection
3359 * RETURNS:
3360 * SUCCESS : TRUE
3361 * FAILURE : FALSE
3363 static BOOL LISTVIEW_DeselectAllSkipItems(LISTVIEW_INFO *infoPtr, RANGES toSkip)
3365 LVITEMW lvItem;
3366 ITERATOR i;
3367 RANGES clone;
3369 TRACE("()\n");
3371 lvItem.state = 0;
3372 lvItem.stateMask = LVIS_SELECTED;
3374 /* need to clone the DPA because callbacks can change it */
3375 if (!(clone = ranges_clone(infoPtr->selectionRanges))) return FALSE;
3376 iterator_rangesitems(&i, ranges_diff(clone, toSkip));
3377 while(iterator_next(&i))
3378 LISTVIEW_SetItemState(infoPtr, i.nItem, &lvItem);
3379 /* note that the iterator destructor will free the cloned range */
3380 iterator_destroy(&i);
3382 return TRUE;
3385 static inline BOOL LISTVIEW_DeselectAllSkipItem(LISTVIEW_INFO *infoPtr, INT nItem)
3387 RANGES toSkip;
3389 if (!(toSkip = ranges_create(1))) return FALSE;
3390 if (nItem != -1) ranges_additem(toSkip, nItem);
3391 LISTVIEW_DeselectAllSkipItems(infoPtr, toSkip);
3392 ranges_destroy(toSkip);
3393 return TRUE;
3396 static inline BOOL LISTVIEW_DeselectAll(LISTVIEW_INFO *infoPtr)
3398 return LISTVIEW_DeselectAllSkipItem(infoPtr, -1);
3401 /***
3402 * DESCRIPTION:
3403 * Retrieves the number of items that are marked as selected.
3405 * PARAMETER(S):
3406 * [I] infoPtr : valid pointer to the listview structure
3408 * RETURN:
3409 * Number of items selected.
3411 static INT LISTVIEW_GetSelectedCount(const LISTVIEW_INFO *infoPtr)
3413 INT nSelectedCount = 0;
3415 if (infoPtr->uCallbackMask & LVIS_SELECTED)
3417 INT i;
3418 for (i = 0; i < infoPtr->nItemCount; i++)
3420 if (LISTVIEW_GetItemState(infoPtr, i, LVIS_SELECTED))
3421 nSelectedCount++;
3424 else
3425 nSelectedCount = ranges_itemcount(infoPtr->selectionRanges);
3427 TRACE("nSelectedCount=%d\n", nSelectedCount);
3428 return nSelectedCount;
3431 /***
3432 * DESCRIPTION:
3433 * Manages the item focus.
3435 * PARAMETER(S):
3436 * [I] infoPtr : valid pointer to the listview structure
3437 * [I] nItem : item index
3439 * RETURN:
3440 * TRUE : focused item changed
3441 * FALSE : focused item has NOT changed
3443 static inline BOOL LISTVIEW_SetItemFocus(LISTVIEW_INFO *infoPtr, INT nItem)
3445 INT oldFocus = infoPtr->nFocusedItem;
3446 LVITEMW lvItem;
3448 if (nItem == infoPtr->nFocusedItem) return FALSE;
3450 lvItem.state = nItem == -1 ? 0 : LVIS_FOCUSED;
3451 lvItem.stateMask = LVIS_FOCUSED;
3452 LISTVIEW_SetItemState(infoPtr, nItem == -1 ? infoPtr->nFocusedItem : nItem, &lvItem);
3454 return oldFocus != infoPtr->nFocusedItem;
3457 static INT shift_item(const LISTVIEW_INFO *infoPtr, INT nShiftItem, INT nItem, INT direction)
3459 if (nShiftItem < nItem) return nShiftItem;
3461 if (nShiftItem > nItem) return nShiftItem + direction;
3463 if (direction > 0) return nShiftItem + direction;
3465 return min(nShiftItem, infoPtr->nItemCount - 1);
3468 /* This function updates focus index.
3470 Parameters:
3471 focus : current focus index
3472 item : index of item to be added/removed
3473 direction : add/remove flag
3475 static void LISTVIEW_ShiftFocus(LISTVIEW_INFO *infoPtr, INT focus, INT item, INT direction)
3477 BOOL old_change = infoPtr->bDoChangeNotify;
3479 infoPtr->bDoChangeNotify = FALSE;
3480 focus = shift_item(infoPtr, focus, item, direction);
3481 if (focus != infoPtr->nFocusedItem)
3482 LISTVIEW_SetItemFocus(infoPtr, focus);
3483 infoPtr->bDoChangeNotify = old_change;
3487 * DESCRIPTION:
3488 * Updates the various indices after an item has been inserted or deleted.
3490 * PARAMETER(S):
3491 * [I] infoPtr : valid pointer to the listview structure
3492 * [I] nItem : item index
3493 * [I] direction : Direction of shift, +1 or -1.
3495 * RETURN:
3496 * None
3498 static void LISTVIEW_ShiftIndices(LISTVIEW_INFO *infoPtr, INT nItem, INT direction)
3500 TRACE("Shifting %i, %i steps\n", nItem, direction);
3502 ranges_shift(infoPtr->selectionRanges, nItem, direction, infoPtr->nItemCount);
3503 assert(abs(direction) == 1);
3504 infoPtr->nSelectionMark = shift_item(infoPtr, infoPtr->nSelectionMark, nItem, direction);
3506 /* But we are not supposed to modify nHotItem! */
3510 * DESCRIPTION:
3511 * Adds a block of selections.
3513 * PARAMETER(S):
3514 * [I] infoPtr : valid pointer to the listview structure
3515 * [I] nItem : item index
3517 * RETURN:
3518 * Whether the window is still valid.
3520 static BOOL LISTVIEW_AddGroupSelection(LISTVIEW_INFO *infoPtr, INT nItem)
3522 INT nFirst = min(infoPtr->nSelectionMark, nItem);
3523 INT nLast = max(infoPtr->nSelectionMark, nItem);
3524 HWND hwndSelf = infoPtr->hwndSelf;
3525 NMLVODSTATECHANGE nmlv;
3526 LVITEMW item;
3527 BOOL bOldChange;
3528 INT i;
3530 /* Temporarily disable change notification
3531 * If the control is LVS_OWNERDATA, we need to send
3532 * only one LVN_ODSTATECHANGED notification.
3533 * See MSDN documentation for LVN_ITEMCHANGED.
3535 bOldChange = infoPtr->bDoChangeNotify;
3536 if (infoPtr->dwStyle & LVS_OWNERDATA) infoPtr->bDoChangeNotify = FALSE;
3538 if (nFirst == -1) nFirst = nItem;
3540 item.state = LVIS_SELECTED;
3541 item.stateMask = LVIS_SELECTED;
3543 for (i = nFirst; i <= nLast; i++)
3544 LISTVIEW_SetItemState(infoPtr,i,&item);
3546 ZeroMemory(&nmlv, sizeof(nmlv));
3547 nmlv.iFrom = nFirst;
3548 nmlv.iTo = nLast;
3549 nmlv.uOldState = 0;
3550 nmlv.uNewState = item.state;
3552 notify_hdr(infoPtr, LVN_ODSTATECHANGED, (LPNMHDR)&nmlv);
3553 if (!IsWindow(hwndSelf))
3554 return FALSE;
3555 infoPtr->bDoChangeNotify = bOldChange;
3556 return TRUE;
3560 /***
3561 * DESCRIPTION:
3562 * Sets a single group selection.
3564 * PARAMETER(S):
3565 * [I] infoPtr : valid pointer to the listview structure
3566 * [I] nItem : item index
3568 * RETURN:
3569 * None
3571 static void LISTVIEW_SetGroupSelection(LISTVIEW_INFO *infoPtr, INT nItem)
3573 RANGES selection;
3574 LVITEMW item;
3575 ITERATOR i;
3576 BOOL bOldChange;
3578 if (!(selection = ranges_create(100))) return;
3580 item.state = LVIS_SELECTED;
3581 item.stateMask = LVIS_SELECTED;
3583 if ((infoPtr->uView == LV_VIEW_LIST) || (infoPtr->uView == LV_VIEW_DETAILS))
3585 if (infoPtr->nSelectionMark == -1)
3587 infoPtr->nSelectionMark = nItem;
3588 ranges_additem(selection, nItem);
3590 else
3592 RANGE sel;
3594 sel.lower = min(infoPtr->nSelectionMark, nItem);
3595 sel.upper = max(infoPtr->nSelectionMark, nItem) + 1;
3596 ranges_add(selection, sel);
3599 else
3601 RECT rcItem, rcSel, rcSelMark;
3602 POINT ptItem;
3604 rcItem.left = LVIR_BOUNDS;
3605 if (!LISTVIEW_GetItemRect(infoPtr, nItem, &rcItem)) {
3606 ranges_destroy (selection);
3607 return;
3609 rcSelMark.left = LVIR_BOUNDS;
3610 if (!LISTVIEW_GetItemRect(infoPtr, infoPtr->nSelectionMark, &rcSelMark)) {
3611 ranges_destroy (selection);
3612 return;
3614 UnionRect(&rcSel, &rcItem, &rcSelMark);
3615 iterator_frameditems(&i, infoPtr, &rcSel);
3616 while(iterator_next(&i))
3618 LISTVIEW_GetItemPosition(infoPtr, i.nItem, &ptItem);
3619 if (PtInRect(&rcSel, ptItem)) ranges_additem(selection, i.nItem);
3621 iterator_destroy(&i);
3624 /* disable per item notifications on LVS_OWNERDATA style
3625 FIXME: single LVN_ODSTATECHANGED should be used */
3626 bOldChange = infoPtr->bDoChangeNotify;
3627 if (infoPtr->dwStyle & LVS_OWNERDATA) infoPtr->bDoChangeNotify = FALSE;
3629 LISTVIEW_DeselectAllSkipItems(infoPtr, selection);
3632 iterator_rangesitems(&i, selection);
3633 while(iterator_next(&i))
3634 LISTVIEW_SetItemState(infoPtr, i.nItem, &item);
3635 /* this will also destroy the selection */
3636 iterator_destroy(&i);
3638 infoPtr->bDoChangeNotify = bOldChange;
3640 LISTVIEW_SetItemFocus(infoPtr, nItem);
3643 /***
3644 * DESCRIPTION:
3645 * Sets a single selection.
3647 * PARAMETER(S):
3648 * [I] infoPtr : valid pointer to the listview structure
3649 * [I] nItem : item index
3651 * RETURN:
3652 * None
3654 static void LISTVIEW_SetSelection(LISTVIEW_INFO *infoPtr, INT nItem)
3656 LVITEMW lvItem;
3658 TRACE("nItem=%d\n", nItem);
3660 LISTVIEW_DeselectAllSkipItem(infoPtr, nItem);
3662 lvItem.state = LVIS_FOCUSED | LVIS_SELECTED;
3663 lvItem.stateMask = LVIS_FOCUSED | LVIS_SELECTED;
3664 LISTVIEW_SetItemState(infoPtr, nItem, &lvItem);
3666 infoPtr->nSelectionMark = nItem;
3669 /***
3670 * DESCRIPTION:
3671 * Set selection(s) with keyboard.
3673 * PARAMETER(S):
3674 * [I] infoPtr : valid pointer to the listview structure
3675 * [I] nItem : item index
3676 * [I] space : VK_SPACE code sent
3678 * RETURN:
3679 * SUCCESS : TRUE (needs to be repainted)
3680 * FAILURE : FALSE (nothing has changed)
3682 static BOOL LISTVIEW_KeySelection(LISTVIEW_INFO *infoPtr, INT nItem, BOOL space)
3684 /* FIXME: pass in the state */
3685 WORD wShift = GetKeyState(VK_SHIFT) & 0x8000;
3686 WORD wCtrl = GetKeyState(VK_CONTROL) & 0x8000;
3687 BOOL bResult = FALSE;
3689 TRACE("nItem=%d, wShift=%d, wCtrl=%d\n", nItem, wShift, wCtrl);
3690 if ((nItem >= 0) && (nItem < infoPtr->nItemCount))
3692 bResult = TRUE;
3694 if (infoPtr->dwStyle & LVS_SINGLESEL || (wShift == 0 && wCtrl == 0))
3695 LISTVIEW_SetSelection(infoPtr, nItem);
3696 else
3698 if (wShift)
3699 LISTVIEW_SetGroupSelection(infoPtr, nItem);
3700 else if (wCtrl)
3702 LVITEMW lvItem;
3703 lvItem.state = ~LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED);
3704 lvItem.stateMask = LVIS_SELECTED;
3705 if (space)
3707 LISTVIEW_SetItemState(infoPtr, nItem, &lvItem);
3708 if (lvItem.state & LVIS_SELECTED)
3709 infoPtr->nSelectionMark = nItem;
3711 bResult = LISTVIEW_SetItemFocus(infoPtr, nItem);
3714 LISTVIEW_EnsureVisible(infoPtr, nItem, FALSE);
3717 UpdateWindow(infoPtr->hwndSelf); /* update client area */
3718 return bResult;
3721 static BOOL LISTVIEW_GetItemAtPt(const LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem, POINT pt)
3723 LVHITTESTINFO lvHitTestInfo;
3725 ZeroMemory(&lvHitTestInfo, sizeof(lvHitTestInfo));
3726 lvHitTestInfo.pt.x = pt.x;
3727 lvHitTestInfo.pt.y = pt.y;
3729 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
3731 lpLVItem->mask = LVIF_PARAM;
3732 lpLVItem->iItem = lvHitTestInfo.iItem;
3733 lpLVItem->iSubItem = 0;
3735 return LISTVIEW_GetItemT(infoPtr, lpLVItem, TRUE);
3738 static inline BOOL LISTVIEW_IsHotTracking(const LISTVIEW_INFO *infoPtr)
3740 return ((infoPtr->dwLvExStyle & LVS_EX_TRACKSELECT) ||
3741 (infoPtr->dwLvExStyle & LVS_EX_ONECLICKACTIVATE) ||
3742 (infoPtr->dwLvExStyle & LVS_EX_TWOCLICKACTIVATE));
3745 /***
3746 * DESCRIPTION:
3747 * Called when the mouse is being actively tracked and has hovered for a specified
3748 * amount of time
3750 * PARAMETER(S):
3751 * [I] infoPtr : valid pointer to the listview structure
3752 * [I] fwKeys : key indicator
3753 * [I] x,y : mouse position
3755 * RETURN:
3756 * 0 if the message was processed, non-zero if there was an error
3758 * INFO:
3759 * LVS_EX_TRACKSELECT: An item is automatically selected when the cursor remains
3760 * over the item for a certain period of time.
3763 static LRESULT LISTVIEW_MouseHover(LISTVIEW_INFO *infoPtr, INT x, INT y)
3765 NMHDR hdr;
3767 if (notify_hdr(infoPtr, NM_HOVER, &hdr)) return 0;
3769 if (LISTVIEW_IsHotTracking(infoPtr))
3771 LVITEMW item;
3772 POINT pt;
3774 pt.x = x;
3775 pt.y = y;
3777 if (LISTVIEW_GetItemAtPt(infoPtr, &item, pt))
3778 LISTVIEW_SetSelection(infoPtr, item.iItem);
3780 SetFocus(infoPtr->hwndSelf);
3783 return 0;
3786 #define SCROLL_LEFT 0x1
3787 #define SCROLL_RIGHT 0x2
3788 #define SCROLL_UP 0x4
3789 #define SCROLL_DOWN 0x8
3791 /***
3792 * DESCRIPTION:
3793 * Utility routine to draw and highlight items within a marquee selection rectangle.
3795 * PARAMETER(S):
3796 * [I] infoPtr : valid pointer to the listview structure
3797 * [I] coords_orig : original co-ordinates of the cursor
3798 * [I] coords_offs : offsetted coordinates of the cursor
3799 * [I] offset : offset amount
3800 * [I] scroll : Bitmask of which directions we should scroll, if at all
3802 * RETURN:
3803 * None.
3805 static void LISTVIEW_MarqueeHighlight(LISTVIEW_INFO *infoPtr, const POINT *coords_orig,
3806 const POINT *coords_offs, const POINT *offset,
3807 INT scroll)
3809 BOOL controlDown = FALSE;
3810 LVITEMW item;
3811 ITERATOR old_elems, new_elems;
3812 RECT rect;
3814 if (coords_offs->x > infoPtr->marqueeOrigin.x)
3816 rect.left = infoPtr->marqueeOrigin.x;
3817 rect.right = coords_offs->x;
3819 else
3821 rect.left = coords_offs->x;
3822 rect.right = infoPtr->marqueeOrigin.x;
3825 if (coords_offs->y > infoPtr->marqueeOrigin.y)
3827 rect.top = infoPtr->marqueeOrigin.y;
3828 rect.bottom = coords_offs->y;
3830 else
3832 rect.top = coords_offs->y;
3833 rect.bottom = infoPtr->marqueeOrigin.y;
3836 /* Cancel out the old marquee rectangle and draw the new one */
3837 LISTVIEW_InvalidateRect(infoPtr, &infoPtr->marqueeDrawRect);
3839 /* Scroll by the appropriate distance if applicable - speed up scrolling as
3840 the cursor is further away */
3842 if ((scroll & SCROLL_LEFT) && (coords_orig->x <= 0))
3843 LISTVIEW_Scroll(infoPtr, coords_orig->x, 0);
3845 if ((scroll & SCROLL_RIGHT) && (coords_orig->x >= infoPtr->rcList.right))
3846 LISTVIEW_Scroll(infoPtr, (coords_orig->x - infoPtr->rcList.right), 0);
3848 if ((scroll & SCROLL_UP) && (coords_orig->y <= 0))
3849 LISTVIEW_Scroll(infoPtr, 0, coords_orig->y);
3851 if ((scroll & SCROLL_DOWN) && (coords_orig->y >= infoPtr->rcList.bottom))
3852 LISTVIEW_Scroll(infoPtr, 0, (coords_orig->y - infoPtr->rcList.bottom));
3854 iterator_frameditems_absolute(&old_elems, infoPtr, &infoPtr->marqueeRect);
3856 CopyRect(&infoPtr->marqueeRect, &rect);
3858 CopyRect(&infoPtr->marqueeDrawRect, &rect);
3859 OffsetRect(&infoPtr->marqueeDrawRect, offset->x, offset->y);
3861 iterator_frameditems_absolute(&new_elems, infoPtr, &infoPtr->marqueeRect);
3862 iterator_remove_common_items(&old_elems, &new_elems);
3864 /* Iterate over no longer selected items */
3865 while (iterator_next(&old_elems))
3867 if (old_elems.nItem > -1)
3869 if (LISTVIEW_GetItemState(infoPtr, old_elems.nItem, LVIS_SELECTED) == LVIS_SELECTED)
3870 item.state = 0;
3871 else
3872 item.state = LVIS_SELECTED;
3874 item.stateMask = LVIS_SELECTED;
3876 LISTVIEW_SetItemState(infoPtr, old_elems.nItem, &item);
3879 iterator_destroy(&old_elems);
3882 /* Iterate over newly selected items */
3883 if (GetKeyState(VK_CONTROL) & 0x8000)
3884 controlDown = TRUE;
3886 while (iterator_next(&new_elems))
3888 if (new_elems.nItem > -1)
3890 /* If CTRL is pressed, invert. If not, always select the item. */
3891 if ((controlDown) && (LISTVIEW_GetItemState(infoPtr, new_elems.nItem, LVIS_SELECTED)))
3892 item.state = 0;
3893 else
3894 item.state = LVIS_SELECTED;
3896 item.stateMask = LVIS_SELECTED;
3898 LISTVIEW_SetItemState(infoPtr, new_elems.nItem, &item);
3901 iterator_destroy(&new_elems);
3903 LISTVIEW_InvalidateRect(infoPtr, &infoPtr->marqueeDrawRect);
3906 /***
3907 * DESCRIPTION:
3908 * Called when we are in a marquee selection that involves scrolling the listview (ie,
3909 * the cursor is outside the bounds of the client area). This is a TIMERPROC.
3911 * PARAMETER(S):
3912 * [I] hwnd : Handle to the listview
3913 * [I] uMsg : WM_TIMER (ignored)
3914 * [I] idEvent : The timer ID interpreted as a pointer to a LISTVIEW_INFO struct
3915 * [I] dwTimer : The elapsed time (ignored)
3917 * RETURN:
3918 * None.
3920 static VOID CALLBACK LISTVIEW_ScrollTimer(HWND hWnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
3922 LISTVIEW_INFO *infoPtr;
3923 SCROLLINFO scrollInfo;
3924 POINT coords_orig;
3925 POINT coords_offs;
3926 POINT offset;
3927 INT scroll = 0;
3929 infoPtr = (LISTVIEW_INFO *) idEvent;
3931 if (!infoPtr)
3932 return;
3934 /* Get the current cursor position and convert to client coordinates */
3935 GetCursorPos(&coords_orig);
3936 ScreenToClient(hWnd, &coords_orig);
3938 /* Ensure coordinates are within client bounds */
3939 coords_offs.x = max(min(coords_orig.x, infoPtr->rcList.right), 0);
3940 coords_offs.y = max(min(coords_orig.y, infoPtr->rcList.bottom), 0);
3942 /* Get offset */
3943 LISTVIEW_GetOrigin(infoPtr, &offset);
3945 /* Offset coordinates by the appropriate amount */
3946 coords_offs.x -= offset.x;
3947 coords_offs.y -= offset.y;
3949 scrollInfo.cbSize = sizeof(SCROLLINFO);
3950 scrollInfo.fMask = SIF_ALL;
3952 /* Work out in which directions we can scroll */
3953 if (GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
3955 if (scrollInfo.nPos != scrollInfo.nMin)
3956 scroll |= SCROLL_UP;
3958 if (((scrollInfo.nPage + scrollInfo.nPos) - 1) != scrollInfo.nMax)
3959 scroll |= SCROLL_DOWN;
3962 if (GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo))
3964 if (scrollInfo.nPos != scrollInfo.nMin)
3965 scroll |= SCROLL_LEFT;
3967 if (((scrollInfo.nPage + scrollInfo.nPos) - 1) != scrollInfo.nMax)
3968 scroll |= SCROLL_RIGHT;
3971 if (((coords_orig.x <= 0) && (scroll & SCROLL_LEFT)) ||
3972 ((coords_orig.y <= 0) && (scroll & SCROLL_UP)) ||
3973 ((coords_orig.x >= infoPtr->rcList.right) && (scroll & SCROLL_RIGHT)) ||
3974 ((coords_orig.y >= infoPtr->rcList.bottom) && (scroll & SCROLL_DOWN)))
3976 LISTVIEW_MarqueeHighlight(infoPtr, &coords_orig, &coords_offs, &offset, scroll);
3980 /***
3981 * DESCRIPTION:
3982 * Called whenever WM_MOUSEMOVE is received.
3984 * PARAMETER(S):
3985 * [I] infoPtr : valid pointer to the listview structure
3986 * [I] fwKeys : key indicator
3987 * [I] x,y : mouse position
3989 * RETURN:
3990 * 0 if the message is processed, non-zero if there was an error
3992 static LRESULT LISTVIEW_MouseMove(LISTVIEW_INFO *infoPtr, WORD fwKeys, INT x, INT y)
3994 LVHITTESTINFO ht;
3995 RECT rect;
3996 POINT pt;
3998 if (!(fwKeys & MK_LBUTTON))
3999 infoPtr->bLButtonDown = FALSE;
4001 if (infoPtr->bLButtonDown)
4003 rect.left = rect.right = infoPtr->ptClickPos.x;
4004 rect.top = rect.bottom = infoPtr->ptClickPos.y;
4006 InflateRect(&rect, GetSystemMetrics(SM_CXDRAG), GetSystemMetrics(SM_CYDRAG));
4008 if (infoPtr->bMarqueeSelect)
4010 POINT coords_orig;
4011 POINT coords_offs;
4012 POINT offset;
4014 coords_orig.x = x;
4015 coords_orig.y = y;
4017 /* Get offset */
4018 LISTVIEW_GetOrigin(infoPtr, &offset);
4020 /* Ensure coordinates are within client bounds */
4021 coords_offs.x = max(min(x, infoPtr->rcList.right), 0);
4022 coords_offs.y = max(min(y, infoPtr->rcList.bottom), 0);
4024 /* Offset coordinates by the appropriate amount */
4025 coords_offs.x -= offset.x;
4026 coords_offs.y -= offset.y;
4028 /* Enable the timer if we're going outside our bounds, in case the user doesn't
4029 move the mouse again */
4031 if ((x <= 0) || (y <= 0) || (x >= infoPtr->rcList.right) ||
4032 (y >= infoPtr->rcList.bottom))
4034 if (!infoPtr->bScrolling)
4036 infoPtr->bScrolling = TRUE;
4037 SetTimer(infoPtr->hwndSelf, (UINT_PTR) infoPtr, 1, LISTVIEW_ScrollTimer);
4040 else
4042 infoPtr->bScrolling = FALSE;
4043 KillTimer(infoPtr->hwndSelf, (UINT_PTR) infoPtr);
4046 LISTVIEW_MarqueeHighlight(infoPtr, &coords_orig, &coords_offs, &offset, 0);
4047 return 0;
4050 pt.x = x;
4051 pt.y = y;
4053 ht.pt = pt;
4054 LISTVIEW_HitTest(infoPtr, &ht, TRUE, TRUE);
4056 /* reset item marker */
4057 if (infoPtr->nLButtonDownItem != ht.iItem)
4058 infoPtr->nLButtonDownItem = -1;
4060 if (!PtInRect(&rect, pt))
4062 /* this path covers the following:
4063 1. WM_LBUTTONDOWN over selected item (sets focus on it)
4064 2. change focus with keys
4065 3. move mouse over item from step 1 selects it and moves focus on it */
4066 if (infoPtr->nLButtonDownItem != -1 &&
4067 !LISTVIEW_GetItemState(infoPtr, infoPtr->nLButtonDownItem, LVIS_SELECTED))
4069 LVITEMW lvItem;
4071 lvItem.state = LVIS_FOCUSED | LVIS_SELECTED;
4072 lvItem.stateMask = LVIS_FOCUSED | LVIS_SELECTED;
4074 LISTVIEW_SetItemState(infoPtr, infoPtr->nLButtonDownItem, &lvItem);
4075 infoPtr->nLButtonDownItem = -1;
4078 if (!infoPtr->bDragging)
4080 ht.pt = infoPtr->ptClickPos;
4081 LISTVIEW_HitTest(infoPtr, &ht, TRUE, TRUE);
4083 /* If the click is outside the range of an item, begin a
4084 highlight. If not, begin an item drag. */
4085 if (ht.iItem == -1)
4087 NMHDR hdr;
4089 /* If we're allowing multiple selections, send notification.
4090 If return value is non-zero, cancel. */
4091 if (!(infoPtr->dwStyle & LVS_SINGLESEL) && (notify_hdr(infoPtr, LVN_MARQUEEBEGIN, &hdr) == 0))
4093 /* Store the absolute coordinates of the click */
4094 POINT offset;
4095 LISTVIEW_GetOrigin(infoPtr, &offset);
4097 infoPtr->marqueeOrigin.x = infoPtr->ptClickPos.x - offset.x;
4098 infoPtr->marqueeOrigin.y = infoPtr->ptClickPos.y - offset.y;
4100 /* Begin selection and capture mouse */
4101 infoPtr->bMarqueeSelect = TRUE;
4102 SetCapture(infoPtr->hwndSelf);
4105 else
4107 NMLISTVIEW nmlv;
4109 ZeroMemory(&nmlv, sizeof(nmlv));
4110 nmlv.iItem = ht.iItem;
4111 nmlv.ptAction = infoPtr->ptClickPos;
4113 notify_listview(infoPtr, LVN_BEGINDRAG, &nmlv);
4114 infoPtr->bDragging = TRUE;
4118 return 0;
4122 /* see if we are supposed to be tracking mouse hovering */
4123 if (LISTVIEW_IsHotTracking(infoPtr)) {
4124 TRACKMOUSEEVENT trackinfo;
4125 DWORD flags;
4127 trackinfo.cbSize = sizeof(TRACKMOUSEEVENT);
4128 trackinfo.dwFlags = TME_QUERY;
4130 /* see if we are already tracking this hwnd */
4131 _TrackMouseEvent(&trackinfo);
4133 flags = TME_LEAVE;
4134 if(infoPtr->dwLvExStyle & LVS_EX_TRACKSELECT)
4135 flags |= TME_HOVER;
4137 if((trackinfo.dwFlags & flags) != flags || trackinfo.hwndTrack != infoPtr->hwndSelf) {
4138 trackinfo.dwFlags = flags;
4139 trackinfo.dwHoverTime = infoPtr->dwHoverTime;
4140 trackinfo.hwndTrack = infoPtr->hwndSelf;
4142 /* call TRACKMOUSEEVENT so we receive WM_MOUSEHOVER messages */
4143 _TrackMouseEvent(&trackinfo);
4147 return 0;
4151 /***
4152 * Tests whether the item is assignable to a list with style lStyle
4154 static inline BOOL is_assignable_item(const LVITEMW *lpLVItem, LONG lStyle)
4156 if ( (lpLVItem->mask & LVIF_TEXT) &&
4157 (lpLVItem->pszText == LPSTR_TEXTCALLBACKW) &&
4158 (lStyle & (LVS_SORTASCENDING | LVS_SORTDESCENDING)) ) return FALSE;
4160 return TRUE;
4164 /***
4165 * DESCRIPTION:
4166 * Helper for LISTVIEW_SetItemT and LISTVIEW_InsertItemT: sets item attributes.
4168 * PARAMETER(S):
4169 * [I] infoPtr : valid pointer to the listview structure
4170 * [I] lpLVItem : valid pointer to new item attributes
4171 * [I] isNew : the item being set is being inserted
4172 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
4173 * [O] bChanged : will be set to TRUE if the item really changed
4175 * RETURN:
4176 * SUCCESS : TRUE
4177 * FAILURE : FALSE
4179 static BOOL set_main_item(LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem, BOOL isNew, BOOL isW, BOOL *bChanged)
4181 ITEM_INFO *lpItem;
4182 NMLISTVIEW nmlv;
4183 UINT uChanged = 0;
4184 LVITEMW item;
4185 /* stateMask is ignored for LVM_INSERTITEM */
4186 UINT stateMask = isNew ? ~0 : lpLVItem->stateMask;
4188 TRACE("()\n");
4190 assert(lpLVItem->iItem >= 0 && lpLVItem->iItem < infoPtr->nItemCount);
4192 if (lpLVItem->mask == 0) return TRUE;
4194 if (infoPtr->dwStyle & LVS_OWNERDATA)
4196 /* a virtual listview only stores selection and focus */
4197 if (lpLVItem->mask & ~LVIF_STATE)
4198 return FALSE;
4199 lpItem = NULL;
4201 else
4203 HDPA hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, lpLVItem->iItem);
4204 lpItem = DPA_GetPtr(hdpaSubItems, 0);
4205 assert (lpItem);
4208 /* we need to get the lParam and state of the item */
4209 item.iItem = lpLVItem->iItem;
4210 item.iSubItem = lpLVItem->iSubItem;
4211 item.mask = LVIF_STATE | LVIF_PARAM;
4212 item.stateMask = (infoPtr->dwStyle & LVS_OWNERDATA) ? LVIS_FOCUSED | LVIS_SELECTED : ~0;
4214 item.state = 0;
4215 item.lParam = 0;
4216 if (!isNew && !LISTVIEW_GetItemW(infoPtr, &item)) return FALSE;
4218 TRACE("oldState=%x, newState=%x\n", item.state, lpLVItem->state);
4219 /* determine what fields will change */
4220 if ((lpLVItem->mask & LVIF_STATE) && ((item.state ^ lpLVItem->state) & stateMask & ~infoPtr->uCallbackMask))
4221 uChanged |= LVIF_STATE;
4223 if ((lpLVItem->mask & LVIF_IMAGE) && (lpItem->hdr.iImage != lpLVItem->iImage))
4224 uChanged |= LVIF_IMAGE;
4226 if ((lpLVItem->mask & LVIF_PARAM) && (lpItem->lParam != lpLVItem->lParam))
4227 uChanged |= LVIF_PARAM;
4229 if ((lpLVItem->mask & LVIF_INDENT) && (lpItem->iIndent != lpLVItem->iIndent))
4230 uChanged |= LVIF_INDENT;
4232 if ((lpLVItem->mask & LVIF_TEXT) && textcmpWT(lpItem->hdr.pszText, lpLVItem->pszText, isW))
4233 uChanged |= LVIF_TEXT;
4235 TRACE("change mask=0x%x\n", uChanged);
4237 memset(&nmlv, 0, sizeof(NMLISTVIEW));
4238 nmlv.iItem = lpLVItem->iItem;
4239 nmlv.uNewState = (item.state & ~stateMask) | (lpLVItem->state & stateMask);
4240 nmlv.uOldState = item.state;
4241 nmlv.uChanged = uChanged ? uChanged : lpLVItem->mask;
4242 nmlv.lParam = item.lParam;
4244 /* Send LVN_ITEMCHANGING notification, if the item is not being inserted
4245 and we are _NOT_ virtual (LVS_OWNERDATA), and change notifications
4246 are enabled. Even nothing really changed we still need to send this,
4247 in this case uChanged mask is just set to passed item mask. */
4248 if(lpItem && !isNew && infoPtr->bDoChangeNotify)
4250 HWND hwndSelf = infoPtr->hwndSelf;
4252 if (notify_listview(infoPtr, LVN_ITEMCHANGING, &nmlv))
4253 return FALSE;
4254 if (!IsWindow(hwndSelf))
4255 return FALSE;
4258 /* When item is inserted we need to shift existing focus index if new item has lower index. */
4259 if (isNew && (stateMask & ~infoPtr->uCallbackMask & LVIS_FOCUSED) &&
4260 /* this means we won't hit a focus change path later */
4261 ((uChanged & LVIF_STATE) == 0 || (!(lpLVItem->state & LVIS_FOCUSED) && (infoPtr->nFocusedItem != lpLVItem->iItem))))
4263 if (infoPtr->nFocusedItem != -1 && (lpLVItem->iItem <= infoPtr->nFocusedItem))
4264 infoPtr->nFocusedItem++;
4267 if (!uChanged) return TRUE;
4268 *bChanged = TRUE;
4270 /* copy information */
4271 if (lpLVItem->mask & LVIF_TEXT)
4272 textsetptrT(&lpItem->hdr.pszText, lpLVItem->pszText, isW);
4274 if (lpLVItem->mask & LVIF_IMAGE)
4275 lpItem->hdr.iImage = lpLVItem->iImage;
4277 if (lpLVItem->mask & LVIF_PARAM)
4278 lpItem->lParam = lpLVItem->lParam;
4280 if (lpLVItem->mask & LVIF_INDENT)
4281 lpItem->iIndent = lpLVItem->iIndent;
4283 if (uChanged & LVIF_STATE)
4285 if (lpItem && (stateMask & ~infoPtr->uCallbackMask))
4287 lpItem->state &= ~stateMask;
4288 lpItem->state |= (lpLVItem->state & stateMask);
4290 if (lpLVItem->state & stateMask & ~infoPtr->uCallbackMask & LVIS_SELECTED)
4292 if (infoPtr->dwStyle & LVS_SINGLESEL) LISTVIEW_DeselectAllSkipItem(infoPtr, lpLVItem->iItem);
4293 ranges_additem(infoPtr->selectionRanges, lpLVItem->iItem);
4295 else if (stateMask & LVIS_SELECTED)
4297 ranges_delitem(infoPtr->selectionRanges, lpLVItem->iItem);
4299 /* If we are asked to change focus, and we manage it, do it.
4300 It's important to have all new item data stored at this point,
4301 because changing existing focus could result in a redrawing operation,
4302 which in turn could ask for disp data, application should see all data
4303 for inserted item when processing LVN_GETDISPINFO.
4305 The way this works application will see nested item change notifications -
4306 changed item notifications interrupted by ones from item losing focus. */
4307 if (stateMask & ~infoPtr->uCallbackMask & LVIS_FOCUSED)
4309 if (lpLVItem->state & LVIS_FOCUSED)
4311 /* update selection mark */
4312 if (infoPtr->nFocusedItem == -1 && infoPtr->nSelectionMark == -1)
4313 infoPtr->nSelectionMark = lpLVItem->iItem;
4315 if (infoPtr->nFocusedItem != -1)
4317 /* remove current focus */
4318 item.mask = LVIF_STATE;
4319 item.state = 0;
4320 item.stateMask = LVIS_FOCUSED;
4322 /* recurse with redrawing an item */
4323 LISTVIEW_SetItemState(infoPtr, infoPtr->nFocusedItem, &item);
4326 infoPtr->nFocusedItem = lpLVItem->iItem;
4327 LISTVIEW_EnsureVisible(infoPtr, lpLVItem->iItem, infoPtr->uView == LV_VIEW_LIST);
4329 else if (infoPtr->nFocusedItem == lpLVItem->iItem)
4331 infoPtr->nFocusedItem = -1;
4336 /* if we're inserting the item, we're done */
4337 if (isNew) return TRUE;
4339 /* send LVN_ITEMCHANGED notification */
4340 if (lpLVItem->mask & LVIF_PARAM) nmlv.lParam = lpLVItem->lParam;
4341 if (infoPtr->bDoChangeNotify) notify_listview(infoPtr, LVN_ITEMCHANGED, &nmlv);
4343 return TRUE;
4346 /***
4347 * DESCRIPTION:
4348 * Helper for LISTVIEW_{Set,Insert}ItemT *only*: sets subitem attributes.
4350 * PARAMETER(S):
4351 * [I] infoPtr : valid pointer to the listview structure
4352 * [I] lpLVItem : valid pointer to new subitem attributes
4353 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
4354 * [O] bChanged : will be set to TRUE if the item really changed
4356 * RETURN:
4357 * SUCCESS : TRUE
4358 * FAILURE : FALSE
4360 static BOOL set_sub_item(const LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem, BOOL isW, BOOL *bChanged)
4362 HDPA hdpaSubItems;
4363 SUBITEM_INFO *lpSubItem;
4365 /* we do not support subitems for virtual listviews */
4366 if (infoPtr->dwStyle & LVS_OWNERDATA) return FALSE;
4368 /* set subitem only if column is present */
4369 if (lpLVItem->iSubItem >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
4371 /* First do some sanity checks */
4372 /* The LVIF_STATE flag is valid for subitems, but does not appear to be
4373 particularly useful. We currently do not actually do anything with
4374 the flag on subitems.
4376 if (lpLVItem->mask & ~(LVIF_TEXT | LVIF_IMAGE | LVIF_STATE | LVIF_DI_SETITEM)) return FALSE;
4377 if (!(lpLVItem->mask & (LVIF_TEXT | LVIF_IMAGE | LVIF_STATE))) return TRUE;
4379 /* get the subitem structure, and create it if not there */
4380 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, lpLVItem->iItem);
4381 assert (hdpaSubItems);
4383 lpSubItem = LISTVIEW_GetSubItemPtr(hdpaSubItems, lpLVItem->iSubItem);
4384 if (!lpSubItem)
4386 SUBITEM_INFO *tmpSubItem;
4387 INT i;
4389 lpSubItem = Alloc(sizeof(SUBITEM_INFO));
4390 if (!lpSubItem) return FALSE;
4391 /* we could binary search here, if need be...*/
4392 for (i = 1; i < DPA_GetPtrCount(hdpaSubItems); i++)
4394 tmpSubItem = DPA_GetPtr(hdpaSubItems, i);
4395 if (tmpSubItem->iSubItem > lpLVItem->iSubItem) break;
4397 if (DPA_InsertPtr(hdpaSubItems, i, lpSubItem) == -1)
4399 Free(lpSubItem);
4400 return FALSE;
4402 lpSubItem->iSubItem = lpLVItem->iSubItem;
4403 lpSubItem->hdr.iImage = I_IMAGECALLBACK;
4404 *bChanged = TRUE;
4407 if ((lpLVItem->mask & LVIF_IMAGE) && (lpSubItem->hdr.iImage != lpLVItem->iImage))
4409 lpSubItem->hdr.iImage = lpLVItem->iImage;
4410 *bChanged = TRUE;
4413 if ((lpLVItem->mask & LVIF_TEXT) && textcmpWT(lpSubItem->hdr.pszText, lpLVItem->pszText, isW))
4415 textsetptrT(&lpSubItem->hdr.pszText, lpLVItem->pszText, isW);
4416 *bChanged = TRUE;
4419 return TRUE;
4422 /***
4423 * DESCRIPTION:
4424 * Sets item attributes.
4426 * PARAMETER(S):
4427 * [I] infoPtr : valid pointer to the listview structure
4428 * [I] lpLVItem : new item attributes
4429 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
4431 * RETURN:
4432 * SUCCESS : TRUE
4433 * FAILURE : FALSE
4435 static BOOL LISTVIEW_SetItemT(LISTVIEW_INFO *infoPtr, LVITEMW *lpLVItem, BOOL isW)
4437 HWND hwndSelf = infoPtr->hwndSelf;
4438 LPWSTR pszText = NULL;
4439 BOOL bResult, bChanged = FALSE;
4440 RECT oldItemArea;
4442 TRACE("(lpLVItem=%s, isW=%d)\n", debuglvitem_t(lpLVItem, isW), isW);
4444 if (!lpLVItem || lpLVItem->iItem < 0 || lpLVItem->iItem >= infoPtr->nItemCount)
4445 return FALSE;
4447 /* Store old item area */
4448 LISTVIEW_GetItemBox(infoPtr, lpLVItem->iItem, &oldItemArea);
4450 /* For efficiency, we transform the lpLVItem->pszText to Unicode here */
4451 if ((lpLVItem->mask & LVIF_TEXT) && is_text(lpLVItem->pszText))
4453 pszText = lpLVItem->pszText;
4454 lpLVItem->pszText = textdupTtoW(lpLVItem->pszText, isW);
4457 /* actually set the fields */
4458 if (!is_assignable_item(lpLVItem, infoPtr->dwStyle)) return FALSE;
4460 if (lpLVItem->iSubItem)
4461 bResult = set_sub_item(infoPtr, lpLVItem, TRUE, &bChanged);
4462 else
4463 bResult = set_main_item(infoPtr, lpLVItem, FALSE, TRUE, &bChanged);
4464 if (!IsWindow(hwndSelf))
4465 return FALSE;
4467 /* redraw item, if necessary */
4468 if (bChanged && !infoPtr->bIsDrawing)
4470 /* this little optimization eliminates some nasty flicker */
4471 if ( infoPtr->uView == LV_VIEW_DETAILS && !(infoPtr->dwStyle & LVS_OWNERDRAWFIXED) &&
4472 !(infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) &&
4473 lpLVItem->iSubItem > 0 && lpLVItem->iSubItem <= DPA_GetPtrCount(infoPtr->hdpaColumns) )
4474 LISTVIEW_InvalidateSubItem(infoPtr, lpLVItem->iItem, lpLVItem->iSubItem);
4475 else
4477 LISTVIEW_InvalidateRect(infoPtr, &oldItemArea);
4478 LISTVIEW_InvalidateItem(infoPtr, lpLVItem->iItem);
4481 /* restore text */
4482 if (pszText)
4484 textfreeT(lpLVItem->pszText, isW);
4485 lpLVItem->pszText = pszText;
4488 return bResult;
4491 /***
4492 * DESCRIPTION:
4493 * Retrieves the index of the item at coordinate (0, 0) of the client area.
4495 * PARAMETER(S):
4496 * [I] infoPtr : valid pointer to the listview structure
4498 * RETURN:
4499 * item index
4501 static INT LISTVIEW_GetTopIndex(const LISTVIEW_INFO *infoPtr)
4503 INT nItem = 0;
4504 SCROLLINFO scrollInfo;
4506 scrollInfo.cbSize = sizeof(SCROLLINFO);
4507 scrollInfo.fMask = SIF_POS;
4509 if (infoPtr->uView == LV_VIEW_LIST)
4511 if (GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo))
4512 nItem = scrollInfo.nPos * LISTVIEW_GetCountPerColumn(infoPtr);
4514 else if (infoPtr->uView == LV_VIEW_DETAILS)
4516 if (GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
4517 nItem = scrollInfo.nPos;
4519 else
4521 if (GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
4522 nItem = LISTVIEW_GetCountPerRow(infoPtr) * (scrollInfo.nPos / infoPtr->nItemHeight);
4525 TRACE("nItem=%d\n", nItem);
4527 return nItem;
4531 /***
4532 * DESCRIPTION:
4533 * Erases the background of the given rectangle
4535 * PARAMETER(S):
4536 * [I] infoPtr : valid pointer to the listview structure
4537 * [I] hdc : device context handle
4538 * [I] lprcBox : clipping rectangle
4540 * RETURN:
4541 * Success: TRUE
4542 * Failure: FALSE
4544 static inline BOOL LISTVIEW_FillBkgnd(const LISTVIEW_INFO *infoPtr, HDC hdc, const RECT *lprcBox)
4546 if (!infoPtr->hBkBrush) return FALSE;
4548 TRACE("(hdc=%p, lprcBox=%s, hBkBrush=%p)\n", hdc, wine_dbgstr_rect(lprcBox), infoPtr->hBkBrush);
4550 return FillRect(hdc, lprcBox, infoPtr->hBkBrush);
4553 /* Draw main item or subitem */
4554 static void LISTVIEW_DrawItemPart(LISTVIEW_INFO *infoPtr, LVITEMW *item, const NMLVCUSTOMDRAW *nmlvcd, const POINT *pos)
4556 RECT rcSelect, rcLabel, rcBox, rcStateIcon, rcIcon;
4557 const RECT *background;
4558 HIMAGELIST himl;
4559 UINT format;
4560 RECT *focus;
4562 /* now check if we need to update the focus rectangle */
4563 focus = infoPtr->bFocus && (item->state & LVIS_FOCUSED) ? &infoPtr->rcFocus : 0;
4564 if (!focus) item->state &= ~LVIS_FOCUSED;
4566 LISTVIEW_GetItemMetrics(infoPtr, item, &rcBox, &rcSelect, &rcIcon, &rcStateIcon, &rcLabel);
4567 OffsetRect(&rcBox, pos->x, pos->y);
4568 OffsetRect(&rcSelect, pos->x, pos->y);
4569 OffsetRect(&rcIcon, pos->x, pos->y);
4570 OffsetRect(&rcStateIcon, pos->x, pos->y);
4571 OffsetRect(&rcLabel, pos->x, pos->y);
4572 TRACE("%d: box=%s, select=%s, icon=%s. label=%s\n", item->iSubItem,
4573 wine_dbgstr_rect(&rcBox), wine_dbgstr_rect(&rcSelect),
4574 wine_dbgstr_rect(&rcIcon), wine_dbgstr_rect(&rcLabel));
4576 /* FIXME: temporary hack */
4577 rcSelect.left = rcLabel.left;
4579 if (infoPtr->uView == LV_VIEW_DETAILS && item->iSubItem == 0)
4581 if (!(infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT))
4582 OffsetRect(&rcSelect, LISTVIEW_GetColumnInfo(infoPtr, 0)->rcHeader.left, 0);
4583 OffsetRect(&rcIcon, LISTVIEW_GetColumnInfo(infoPtr, 0)->rcHeader.left, 0);
4584 OffsetRect(&rcStateIcon, LISTVIEW_GetColumnInfo(infoPtr, 0)->rcHeader.left, 0);
4585 OffsetRect(&rcLabel, LISTVIEW_GetColumnInfo(infoPtr, 0)->rcHeader.left, 0);
4588 /* in icon mode, the label rect is really what we want to draw the
4589 * background for */
4590 /* in detail mode, we want to paint background for label rect when
4591 * item is not selected or listview has full row select; otherwise paint
4592 * background for text only */
4593 if ( infoPtr->uView == LV_VIEW_ICON ||
4594 (infoPtr->uView == LV_VIEW_DETAILS && (!(item->state & LVIS_SELECTED) ||
4595 (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT))))
4596 background = &rcLabel;
4597 else
4598 background = &rcSelect;
4600 if (nmlvcd->clrTextBk != CLR_NONE)
4601 ExtTextOutW(nmlvcd->nmcd.hdc, background->left, background->top, ETO_OPAQUE, background, NULL, 0, NULL);
4603 if (item->state & LVIS_FOCUSED)
4605 if (infoPtr->uView == LV_VIEW_DETAILS)
4607 if (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT)
4609 /* we have to update left focus bound too if item isn't in leftmost column
4610 and reduce right box bound */
4611 if (DPA_GetPtrCount(infoPtr->hdpaColumns) > 0)
4613 INT leftmost;
4615 if ((leftmost = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, 0, 0)))
4617 INT Originx = pos->x - LISTVIEW_GetColumnInfo(infoPtr, leftmost)->rcHeader.left;
4618 INT rightmost = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX,
4619 DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, 0);
4621 rcBox.right = LISTVIEW_GetColumnInfo(infoPtr, rightmost)->rcHeader.right + Originx;
4622 rcSelect.left = LISTVIEW_GetColumnInfo(infoPtr, leftmost)->rcHeader.left + Originx;
4625 rcSelect.right = rcBox.right;
4627 infoPtr->rcFocus = rcSelect;
4629 else
4630 infoPtr->rcFocus = rcLabel;
4633 /* state icons */
4634 if (infoPtr->himlState && STATEIMAGEINDEX(item->state) && (item->iSubItem == 0))
4636 UINT stateimage = STATEIMAGEINDEX(item->state);
4637 if (stateimage)
4639 TRACE("stateimage=%d\n", stateimage);
4640 ImageList_Draw(infoPtr->himlState, stateimage-1, nmlvcd->nmcd.hdc, rcStateIcon.left, rcStateIcon.top, ILD_NORMAL);
4644 /* item icons */
4645 himl = (infoPtr->uView == LV_VIEW_ICON ? infoPtr->himlNormal : infoPtr->himlSmall);
4646 if (himl && item->iImage >= 0 && !IsRectEmpty(&rcIcon))
4648 UINT style;
4650 TRACE("iImage=%d\n", item->iImage);
4652 if (item->state & (LVIS_SELECTED | LVIS_CUT) && infoPtr->bFocus)
4653 style = ILD_SELECTED;
4654 else
4655 style = ILD_NORMAL;
4657 ImageList_DrawEx(himl, item->iImage, nmlvcd->nmcd.hdc, rcIcon.left, rcIcon.top,
4658 rcIcon.right - rcIcon.left, rcIcon.bottom - rcIcon.top, infoPtr->clrBk,
4659 item->state & LVIS_CUT ? RGB(255, 255, 255) : CLR_DEFAULT,
4660 style | (item->state & LVIS_OVERLAYMASK));
4663 /* Don't bother painting item being edited */
4664 if (infoPtr->hwndEdit && item->iItem == infoPtr->nEditLabelItem && item->iSubItem == 0) return;
4666 /* figure out the text drawing flags */
4667 format = (infoPtr->uView == LV_VIEW_ICON ? (focus ? LV_FL_DT_FLAGS : LV_ML_DT_FLAGS) : LV_SL_DT_FLAGS);
4668 if (infoPtr->uView == LV_VIEW_ICON)
4669 format = (focus ? LV_FL_DT_FLAGS : LV_ML_DT_FLAGS);
4670 else if (item->iSubItem)
4672 switch (LISTVIEW_GetColumnInfo(infoPtr, item->iSubItem)->fmt & LVCFMT_JUSTIFYMASK)
4674 case LVCFMT_RIGHT: format |= DT_RIGHT; break;
4675 case LVCFMT_CENTER: format |= DT_CENTER; break;
4676 default: format |= DT_LEFT;
4679 if (!(format & (DT_RIGHT | DT_CENTER)))
4681 if (himl && item->iImage >= 0 && !IsRectEmpty(&rcIcon)) rcLabel.left += IMAGE_PADDING;
4682 else rcLabel.left += LABEL_HOR_PADDING;
4684 else if (format & DT_RIGHT) rcLabel.right -= LABEL_HOR_PADDING;
4686 /* for GRIDLINES reduce the bottom so the text formats correctly */
4687 if (infoPtr->uView == LV_VIEW_DETAILS && infoPtr->dwLvExStyle & LVS_EX_GRIDLINES)
4688 rcLabel.bottom--;
4690 DrawTextW(nmlvcd->nmcd.hdc, item->pszText, -1, &rcLabel, format);
4693 /***
4694 * DESCRIPTION:
4695 * Draws an item.
4697 * PARAMETER(S):
4698 * [I] infoPtr : valid pointer to the listview structure
4699 * [I] hdc : device context handle
4700 * [I] nItem : item index
4701 * [I] nSubItem : subitem index
4702 * [I] pos : item position in client coordinates
4703 * [I] cdmode : custom draw mode
4705 * RETURN:
4706 * Success: TRUE
4707 * Failure: FALSE
4709 static BOOL LISTVIEW_DrawItem(LISTVIEW_INFO *infoPtr, HDC hdc, INT nItem, ITERATOR *subitems, POINT pos, DWORD cdmode)
4711 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
4712 static WCHAR callbackW[] = { '(', 'c', 'a', 'l', 'l', 'b', 'a', 'c', 'k', ')', 0 };
4713 DWORD cdsubitemmode = CDRF_DODEFAULT;
4714 RECT *focus, rcBox;
4715 NMLVCUSTOMDRAW nmlvcd;
4716 LVITEMW lvItem;
4718 TRACE("(hdc=%p, nItem=%d, subitems=%p, pos=%s)\n", hdc, nItem, subitems, wine_dbgstr_point(&pos));
4720 /* get information needed for drawing the item */
4721 lvItem.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_PARAM | LVIF_STATE;
4722 if (infoPtr->uView == LV_VIEW_DETAILS) lvItem.mask |= LVIF_INDENT;
4723 lvItem.stateMask = LVIS_SELECTED | LVIS_FOCUSED | LVIS_STATEIMAGEMASK | LVIS_CUT | LVIS_OVERLAYMASK;
4724 lvItem.iItem = nItem;
4725 lvItem.iSubItem = 0;
4726 lvItem.state = 0;
4727 lvItem.lParam = 0;
4728 lvItem.cchTextMax = DISP_TEXT_SIZE;
4729 lvItem.pszText = szDispText;
4730 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return FALSE;
4731 if (lvItem.pszText == LPSTR_TEXTCALLBACKW) lvItem.pszText = callbackW;
4732 TRACE(" lvItem=%s\n", debuglvitem_t(&lvItem, TRUE));
4734 /* now check if we need to update the focus rectangle */
4735 focus = infoPtr->bFocus && (lvItem.state & LVIS_FOCUSED) ? &infoPtr->rcFocus : 0;
4736 if (!focus) lvItem.state &= ~LVIS_FOCUSED;
4738 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, &rcBox, NULL, NULL, NULL, NULL);
4739 OffsetRect(&rcBox, pos.x, pos.y);
4741 /* Full custom draw stage sequence looks like this:
4743 LV_VIEW_DETAILS:
4745 - CDDS_ITEMPREPAINT
4746 - CDDS_ITEMPREPAINT|CDDS_SUBITEM | => sent n times, where n is number of subitems,
4747 CDDS_ITEMPOSTPAINT|CDDS_SUBITEM | including item iself
4748 - CDDS_ITEMPOSTPAINT
4750 other styles:
4752 - CDDS_ITEMPREPAINT
4753 - CDDS_ITEMPOSTPAINT
4756 /* fill in the custom draw structure */
4757 customdraw_fill(&nmlvcd, infoPtr, hdc, &rcBox, &lvItem);
4758 if (cdmode & CDRF_NOTIFYITEMDRAW)
4759 cdsubitemmode = notify_customdraw(infoPtr, CDDS_ITEMPREPAINT, &nmlvcd);
4760 if (cdsubitemmode & CDRF_SKIPDEFAULT) goto postpaint;
4762 if (subitems)
4764 while (iterator_next(subitems))
4766 DWORD subitemstage = CDRF_DODEFAULT;
4768 /* We need to query for each subitem, item's data (subitem == 0) is already here at this point */
4769 if (subitems->nItem)
4771 lvItem.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_PARAM | LVIF_INDENT;
4772 lvItem.stateMask = LVIS_SELECTED | LVIS_FOCUSED | LVIS_STATEIMAGEMASK | LVIS_CUT | LVIS_OVERLAYMASK;
4773 lvItem.iItem = nItem;
4774 lvItem.iSubItem = subitems->nItem;
4775 lvItem.state = 0;
4776 lvItem.lParam = 0;
4777 lvItem.cchTextMax = DISP_TEXT_SIZE;
4778 lvItem.pszText = szDispText;
4779 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return FALSE;
4780 if (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT)
4781 lvItem.state = LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED);
4782 if (lvItem.pszText == LPSTR_TEXTCALLBACKW) lvItem.pszText = callbackW;
4783 TRACE(" lvItem=%s\n", debuglvitem_t(&lvItem, TRUE));
4785 /* update custom draw data */
4786 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, &nmlvcd.nmcd.rc, NULL, NULL, NULL, NULL);
4787 OffsetRect(&nmlvcd.nmcd.rc, pos.x, pos.y);
4788 nmlvcd.iSubItem = subitems->nItem;
4791 if (cdsubitemmode & CDRF_NOTIFYSUBITEMDRAW)
4792 subitemstage = notify_customdraw(infoPtr, CDDS_SUBITEM | CDDS_ITEMPREPAINT, &nmlvcd);
4793 else
4795 nmlvcd.clrTextBk = infoPtr->clrTextBk;
4796 nmlvcd.clrText = infoPtr->clrText;
4799 if (subitems->nItem == 0 || (cdmode & CDRF_NOTIFYITEMDRAW))
4800 prepaint_setup(infoPtr, hdc, &nmlvcd, FALSE);
4801 else if (!(infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT))
4802 prepaint_setup(infoPtr, hdc, &nmlvcd, TRUE);
4804 if (!(subitemstage & CDRF_SKIPDEFAULT))
4805 LISTVIEW_DrawItemPart(infoPtr, &lvItem, &nmlvcd, &pos);
4807 if (subitemstage & CDRF_NOTIFYPOSTPAINT)
4808 subitemstage = notify_customdraw(infoPtr, CDDS_SUBITEM | CDDS_ITEMPOSTPAINT, &nmlvcd);
4811 else
4813 prepaint_setup(infoPtr, hdc, &nmlvcd, FALSE);
4814 LISTVIEW_DrawItemPart(infoPtr, &lvItem, &nmlvcd, &pos);
4817 postpaint:
4818 if (cdsubitemmode & CDRF_NOTIFYPOSTPAINT)
4820 nmlvcd.iSubItem = 0;
4821 notify_customdraw(infoPtr, CDDS_ITEMPOSTPAINT, &nmlvcd);
4824 return TRUE;
4827 /***
4828 * DESCRIPTION:
4829 * Draws listview items when in owner draw mode.
4831 * PARAMETER(S):
4832 * [I] infoPtr : valid pointer to the listview structure
4833 * [I] hdc : device context handle
4835 * RETURN:
4836 * None
4838 static void LISTVIEW_RefreshOwnerDraw(const LISTVIEW_INFO *infoPtr, ITERATOR *i, HDC hdc, DWORD cdmode)
4840 UINT uID = (UINT)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
4841 DWORD cditemmode = CDRF_DODEFAULT;
4842 NMLVCUSTOMDRAW nmlvcd;
4843 POINT Origin, Position;
4844 DRAWITEMSTRUCT dis;
4845 LVITEMW item;
4847 TRACE("()\n");
4849 ZeroMemory(&dis, sizeof(dis));
4851 /* Get scroll info once before loop */
4852 LISTVIEW_GetOrigin(infoPtr, &Origin);
4854 /* iterate through the invalidated rows */
4855 while(iterator_next(i))
4857 item.iItem = i->nItem;
4858 item.iSubItem = 0;
4859 item.mask = LVIF_PARAM | LVIF_STATE;
4860 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
4861 if (!LISTVIEW_GetItemW(infoPtr, &item)) continue;
4863 dis.CtlType = ODT_LISTVIEW;
4864 dis.CtlID = uID;
4865 dis.itemID = item.iItem;
4866 dis.itemAction = ODA_DRAWENTIRE;
4867 dis.itemState = 0;
4868 if (item.state & LVIS_SELECTED) dis.itemState |= ODS_SELECTED;
4869 if (infoPtr->bFocus && (item.state & LVIS_FOCUSED)) dis.itemState |= ODS_FOCUS;
4870 dis.hwndItem = infoPtr->hwndSelf;
4871 dis.hDC = hdc;
4872 LISTVIEW_GetItemOrigin(infoPtr, dis.itemID, &Position);
4873 dis.rcItem.left = Position.x + Origin.x;
4874 dis.rcItem.right = dis.rcItem.left + infoPtr->nItemWidth;
4875 dis.rcItem.top = Position.y + Origin.y;
4876 dis.rcItem.bottom = dis.rcItem.top + infoPtr->nItemHeight;
4877 dis.itemData = item.lParam;
4879 TRACE("item=%s, rcItem=%s\n", debuglvitem_t(&item, TRUE), wine_dbgstr_rect(&dis.rcItem));
4882 * Even if we do not send the CDRF_NOTIFYITEMDRAW we need to fill the nmlvcd
4883 * structure for the rest. of the paint cycle
4885 customdraw_fill(&nmlvcd, infoPtr, hdc, &dis.rcItem, &item);
4886 if (cdmode & CDRF_NOTIFYITEMDRAW)
4887 cditemmode = notify_customdraw(infoPtr, CDDS_PREPAINT, &nmlvcd);
4889 if (!(cditemmode & CDRF_SKIPDEFAULT))
4891 prepaint_setup (infoPtr, hdc, &nmlvcd, FALSE);
4892 SendMessageW(infoPtr->hwndNotify, WM_DRAWITEM, dis.CtlID, (LPARAM)&dis);
4895 if (cditemmode & CDRF_NOTIFYPOSTPAINT)
4896 notify_postpaint(infoPtr, &nmlvcd);
4900 /***
4901 * DESCRIPTION:
4902 * Draws listview items when in report display mode.
4904 * PARAMETER(S):
4905 * [I] infoPtr : valid pointer to the listview structure
4906 * [I] hdc : device context handle
4907 * [I] cdmode : custom draw mode
4909 * RETURN:
4910 * None
4912 static void LISTVIEW_RefreshReport(LISTVIEW_INFO *infoPtr, ITERATOR *i, HDC hdc, DWORD cdmode)
4914 INT rgntype;
4915 RECT rcClip, rcItem;
4916 POINT Origin;
4917 RANGES colRanges;
4918 INT col;
4919 ITERATOR j;
4921 TRACE("()\n");
4923 /* figure out what to draw */
4924 rgntype = GetClipBox(hdc, &rcClip);
4925 if (rgntype == NULLREGION) return;
4927 /* Get scroll info once before loop */
4928 LISTVIEW_GetOrigin(infoPtr, &Origin);
4930 colRanges = ranges_create(DPA_GetPtrCount(infoPtr->hdpaColumns));
4932 /* narrow down the columns we need to paint */
4933 for(col = 0; col < DPA_GetPtrCount(infoPtr->hdpaColumns); col++)
4935 INT index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, col, 0);
4937 LISTVIEW_GetHeaderRect(infoPtr, index, &rcItem);
4938 if ((rcItem.right + Origin.x >= rcClip.left) && (rcItem.left + Origin.x < rcClip.right))
4939 ranges_additem(colRanges, index);
4941 iterator_rangesitems(&j, colRanges);
4943 /* in full row select, we _have_ to draw the main item */
4944 if (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT)
4945 j.nSpecial = 0;
4947 /* iterate through the invalidated rows */
4948 while(iterator_next(i))
4950 RANGES subitems;
4951 POINT Position;
4952 ITERATOR k;
4954 SelectObject(hdc, infoPtr->hFont);
4955 LISTVIEW_GetItemOrigin(infoPtr, i->nItem, &Position);
4956 Position.x = Origin.x;
4957 Position.y += Origin.y;
4959 subitems = ranges_create(DPA_GetPtrCount(infoPtr->hdpaColumns));
4961 /* iterate through the invalidated columns */
4962 while(iterator_next(&j))
4964 LISTVIEW_GetHeaderRect(infoPtr, j.nItem, &rcItem);
4966 if (rgntype == COMPLEXREGION && !((infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) && j.nItem == 0))
4968 rcItem.top = 0;
4969 rcItem.bottom = infoPtr->nItemHeight;
4970 OffsetRect(&rcItem, Origin.x, Position.y);
4971 if (!RectVisible(hdc, &rcItem)) continue;
4974 ranges_additem(subitems, j.nItem);
4977 iterator_rangesitems(&k, subitems);
4978 LISTVIEW_DrawItem(infoPtr, hdc, i->nItem, &k, Position, cdmode);
4979 iterator_destroy(&k);
4981 iterator_destroy(&j);
4984 /***
4985 * DESCRIPTION:
4986 * Draws the gridlines if necessary when in report display mode.
4988 * PARAMETER(S):
4989 * [I] infoPtr : valid pointer to the listview structure
4990 * [I] hdc : device context handle
4992 * RETURN:
4993 * None
4995 static void LISTVIEW_RefreshReportGrid(LISTVIEW_INFO *infoPtr, HDC hdc)
4997 INT rgntype;
4998 INT y, itemheight;
4999 INT col, index;
5000 HPEN hPen, hOldPen;
5001 RECT rcClip, rcItem = {0};
5002 POINT Origin;
5003 RANGES colRanges;
5004 ITERATOR j;
5005 BOOL rmost = FALSE;
5007 TRACE("()\n");
5009 /* figure out what to draw */
5010 rgntype = GetClipBox(hdc, &rcClip);
5011 if (rgntype == NULLREGION) return;
5013 /* Get scroll info once before loop */
5014 LISTVIEW_GetOrigin(infoPtr, &Origin);
5016 colRanges = ranges_create(DPA_GetPtrCount(infoPtr->hdpaColumns));
5018 /* narrow down the columns we need to paint */
5019 for(col = 0; col < DPA_GetPtrCount(infoPtr->hdpaColumns); col++)
5021 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, col, 0);
5023 LISTVIEW_GetHeaderRect(infoPtr, index, &rcItem);
5024 if ((rcItem.right + Origin.x >= rcClip.left) && (rcItem.left + Origin.x < rcClip.right))
5025 ranges_additem(colRanges, index);
5028 /* is right most vertical line visible? */
5029 if (DPA_GetPtrCount(infoPtr->hdpaColumns) > 0)
5031 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, 0);
5032 LISTVIEW_GetHeaderRect(infoPtr, index, &rcItem);
5033 rmost = (rcItem.right + Origin.x < rcClip.right);
5036 if ((hPen = CreatePen( PS_SOLID, 1, comctl32_color.clr3dFace )))
5038 hOldPen = SelectObject ( hdc, hPen );
5040 /* draw the vertical lines for the columns */
5041 iterator_rangesitems(&j, colRanges);
5042 while(iterator_next(&j))
5044 LISTVIEW_GetHeaderRect(infoPtr, j.nItem, &rcItem);
5045 if (rcItem.left == 0) continue; /* skip leftmost column */
5046 rcItem.left += Origin.x;
5047 rcItem.right += Origin.x;
5048 rcItem.top = infoPtr->rcList.top;
5049 rcItem.bottom = infoPtr->rcList.bottom;
5050 TRACE("vert col=%d, rcItem=%s\n", j.nItem, wine_dbgstr_rect(&rcItem));
5051 MoveToEx (hdc, rcItem.left, rcItem.top, NULL);
5052 LineTo (hdc, rcItem.left, rcItem.bottom);
5054 iterator_destroy(&j);
5055 /* draw rightmost grid line if visible */
5056 if (rmost)
5058 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX,
5059 DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, 0);
5060 LISTVIEW_GetHeaderRect(infoPtr, index, &rcItem);
5062 rcItem.right += Origin.x;
5064 MoveToEx (hdc, rcItem.right, infoPtr->rcList.top, NULL);
5065 LineTo (hdc, rcItem.right, infoPtr->rcList.bottom);
5068 /* draw the horizontal lines for the rows */
5069 itemheight = LISTVIEW_CalculateItemHeight(infoPtr);
5070 rcItem.left = infoPtr->rcList.left;
5071 rcItem.right = infoPtr->rcList.right;
5072 for(y = Origin.y > 1 ? Origin.y - 1 : itemheight - 1 + Origin.y % itemheight; y<=infoPtr->rcList.bottom; y+=itemheight)
5074 rcItem.bottom = rcItem.top = y;
5075 TRACE("horz rcItem=%s\n", wine_dbgstr_rect(&rcItem));
5076 MoveToEx (hdc, rcItem.left, rcItem.top, NULL);
5077 LineTo (hdc, rcItem.right, rcItem.top);
5080 SelectObject( hdc, hOldPen );
5081 DeleteObject( hPen );
5083 else
5084 ranges_destroy(colRanges);
5087 /***
5088 * DESCRIPTION:
5089 * Draws listview items when in list display mode.
5091 * PARAMETER(S):
5092 * [I] infoPtr : valid pointer to the listview structure
5093 * [I] hdc : device context handle
5094 * [I] cdmode : custom draw mode
5096 * RETURN:
5097 * None
5099 static void LISTVIEW_RefreshList(LISTVIEW_INFO *infoPtr, ITERATOR *i, HDC hdc, DWORD cdmode)
5101 POINT Origin, Position;
5103 /* Get scroll info once before loop */
5104 LISTVIEW_GetOrigin(infoPtr, &Origin);
5106 while(iterator_prev(i))
5108 SelectObject(hdc, infoPtr->hFont);
5109 LISTVIEW_GetItemOrigin(infoPtr, i->nItem, &Position);
5110 Position.x += Origin.x;
5111 Position.y += Origin.y;
5113 LISTVIEW_DrawItem(infoPtr, hdc, i->nItem, NULL, Position, cdmode);
5118 /***
5119 * DESCRIPTION:
5120 * Draws listview items.
5122 * PARAMETER(S):
5123 * [I] infoPtr : valid pointer to the listview structure
5124 * [I] hdc : device context handle
5125 * [I] prcErase : rect to be erased before refresh (may be NULL)
5127 * RETURN:
5128 * NoneX
5130 static void LISTVIEW_Refresh(LISTVIEW_INFO *infoPtr, HDC hdc, const RECT *prcErase)
5132 COLORREF oldTextColor = 0, oldBkColor = 0;
5133 NMLVCUSTOMDRAW nmlvcd;
5134 HFONT hOldFont = 0;
5135 DWORD cdmode;
5136 INT oldBkMode = 0;
5137 RECT rcClient;
5138 ITERATOR i;
5139 HDC hdcOrig = hdc;
5140 HBITMAP hbmp = NULL;
5141 RANGE range;
5143 LISTVIEW_DUMP(infoPtr);
5145 if (infoPtr->dwLvExStyle & LVS_EX_DOUBLEBUFFER) {
5146 TRACE("double buffering\n");
5148 hdc = CreateCompatibleDC(hdcOrig);
5149 if (!hdc) {
5150 ERR("Failed to create DC for backbuffer\n");
5151 return;
5153 hbmp = CreateCompatibleBitmap(hdcOrig, infoPtr->rcList.right,
5154 infoPtr->rcList.bottom);
5155 if (!hbmp) {
5156 ERR("Failed to create bitmap for backbuffer\n");
5157 DeleteDC(hdc);
5158 return;
5161 SelectObject(hdc, hbmp);
5162 SelectObject(hdc, infoPtr->hFont);
5164 if(GetClipBox(hdcOrig, &rcClient))
5165 IntersectClipRect(hdc, rcClient.left, rcClient.top, rcClient.right, rcClient.bottom);
5166 } else {
5167 /* Save dc values we're gonna trash while drawing
5168 * FIXME: Should be done in LISTVIEW_DrawItem() */
5169 hOldFont = SelectObject(hdc, infoPtr->hFont);
5170 oldBkMode = GetBkMode(hdc);
5171 oldBkColor = GetBkColor(hdc);
5172 oldTextColor = GetTextColor(hdc);
5175 infoPtr->bIsDrawing = TRUE;
5177 if (prcErase) {
5178 LISTVIEW_FillBkgnd(infoPtr, hdc, prcErase);
5179 } else if (infoPtr->dwLvExStyle & LVS_EX_DOUBLEBUFFER) {
5180 /* If no erasing was done (usually because RedrawWindow was called
5181 * with RDW_INVALIDATE only) we need to copy the old contents into
5182 * the backbuffer before continuing. */
5183 BitBlt(hdc, infoPtr->rcList.left, infoPtr->rcList.top,
5184 infoPtr->rcList.right - infoPtr->rcList.left,
5185 infoPtr->rcList.bottom - infoPtr->rcList.top,
5186 hdcOrig, infoPtr->rcList.left, infoPtr->rcList.top, SRCCOPY);
5189 GetClientRect(infoPtr->hwndSelf, &rcClient);
5190 customdraw_fill(&nmlvcd, infoPtr, hdc, &rcClient, 0);
5191 cdmode = notify_customdraw(infoPtr, CDDS_PREPAINT, &nmlvcd);
5192 if (cdmode & CDRF_SKIPDEFAULT) goto enddraw;
5194 /* nothing to draw */
5195 if(infoPtr->nItemCount == 0) goto enddraw;
5197 /* figure out what we need to draw */
5198 iterator_visibleitems(&i, infoPtr, hdc);
5199 range = iterator_range(&i);
5201 /* send cache hint notification */
5202 if (infoPtr->dwStyle & LVS_OWNERDATA)
5204 NMLVCACHEHINT nmlv;
5206 ZeroMemory(&nmlv, sizeof(NMLVCACHEHINT));
5207 nmlv.iFrom = range.lower;
5208 nmlv.iTo = range.upper - 1;
5209 notify_hdr(infoPtr, LVN_ODCACHEHINT, &nmlv.hdr);
5212 if ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (infoPtr->uView == LV_VIEW_DETAILS))
5213 LISTVIEW_RefreshOwnerDraw(infoPtr, &i, hdc, cdmode);
5214 else
5216 if (infoPtr->uView == LV_VIEW_DETAILS)
5217 LISTVIEW_RefreshReport(infoPtr, &i, hdc, cdmode);
5218 else /* LV_VIEW_LIST, LV_VIEW_ICON or LV_VIEW_SMALLICON */
5219 LISTVIEW_RefreshList(infoPtr, &i, hdc, cdmode);
5221 /* if we have a focus rect and it's visible, draw it */
5222 if (infoPtr->bFocus && range.lower <= infoPtr->nFocusedItem &&
5223 (range.upper - 1) >= infoPtr->nFocusedItem)
5224 LISTVIEW_DrawFocusRect(infoPtr, hdc);
5226 iterator_destroy(&i);
5228 enddraw:
5229 /* For LVS_EX_GRIDLINES go and draw lines */
5230 /* This includes the case where there were *no* items */
5231 if ((infoPtr->uView == LV_VIEW_DETAILS) && infoPtr->dwLvExStyle & LVS_EX_GRIDLINES)
5232 LISTVIEW_RefreshReportGrid(infoPtr, hdc);
5234 /* Draw marquee rectangle if appropriate */
5235 if (infoPtr->bMarqueeSelect)
5236 DrawFocusRect(hdc, &infoPtr->marqueeDrawRect);
5238 if (cdmode & CDRF_NOTIFYPOSTPAINT)
5239 notify_postpaint(infoPtr, &nmlvcd);
5241 if(hbmp) {
5242 BitBlt(hdcOrig, infoPtr->rcList.left, infoPtr->rcList.top,
5243 infoPtr->rcList.right - infoPtr->rcList.left,
5244 infoPtr->rcList.bottom - infoPtr->rcList.top,
5245 hdc, infoPtr->rcList.left, infoPtr->rcList.top, SRCCOPY);
5247 DeleteObject(hbmp);
5248 DeleteDC(hdc);
5249 } else {
5250 SelectObject(hdc, hOldFont);
5251 SetBkMode(hdc, oldBkMode);
5252 SetBkColor(hdc, oldBkColor);
5253 SetTextColor(hdc, oldTextColor);
5256 infoPtr->bIsDrawing = FALSE;
5260 /***
5261 * DESCRIPTION:
5262 * Calculates the approximate width and height of a given number of items.
5264 * PARAMETER(S):
5265 * [I] infoPtr : valid pointer to the listview structure
5266 * [I] nItemCount : number of items
5267 * [I] wWidth : width
5268 * [I] wHeight : height
5270 * RETURN:
5271 * Returns a DWORD. The width in the low word and the height in high word.
5273 static DWORD LISTVIEW_ApproximateViewRect(const LISTVIEW_INFO *infoPtr, INT nItemCount,
5274 WORD wWidth, WORD wHeight)
5276 DWORD dwViewRect = 0;
5278 if (nItemCount == -1)
5279 nItemCount = infoPtr->nItemCount;
5281 if (infoPtr->uView == LV_VIEW_LIST)
5283 INT nItemCountPerColumn = 1;
5284 INT nColumnCount = 0;
5286 if (wHeight == 0xFFFF)
5288 /* use current height */
5289 wHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
5292 if (wHeight < infoPtr->nItemHeight)
5293 wHeight = infoPtr->nItemHeight;
5295 if (nItemCount > 0)
5297 if (infoPtr->nItemHeight > 0)
5299 nItemCountPerColumn = wHeight / infoPtr->nItemHeight;
5300 if (nItemCountPerColumn == 0)
5301 nItemCountPerColumn = 1;
5303 if (nItemCount % nItemCountPerColumn != 0)
5304 nColumnCount = nItemCount / nItemCountPerColumn;
5305 else
5306 nColumnCount = nItemCount / nItemCountPerColumn + 1;
5310 /* Microsoft padding magic */
5311 wHeight = nItemCountPerColumn * infoPtr->nItemHeight + 2;
5312 wWidth = nColumnCount * infoPtr->nItemWidth + 2;
5314 dwViewRect = MAKELONG(wWidth, wHeight);
5316 else if (infoPtr->uView == LV_VIEW_DETAILS)
5318 RECT rcBox;
5320 if (infoPtr->nItemCount > 0)
5322 LISTVIEW_GetItemBox(infoPtr, 0, &rcBox);
5323 wWidth = rcBox.right - rcBox.left;
5324 wHeight = (rcBox.bottom - rcBox.top) * nItemCount;
5326 else
5328 /* use current height and width */
5329 if (wHeight == 0xffff)
5330 wHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
5331 if (wWidth == 0xffff)
5332 wWidth = infoPtr->rcList.right - infoPtr->rcList.left;
5335 dwViewRect = MAKELONG(wWidth, wHeight);
5337 else if (infoPtr->uView == LV_VIEW_ICON)
5339 UINT rows,cols;
5340 UINT nItemWidth;
5341 UINT nItemHeight;
5343 nItemWidth = infoPtr->iconSpacing.cx;
5344 nItemHeight = infoPtr->iconSpacing.cy;
5346 if (wWidth == 0xffff)
5347 wWidth = infoPtr->rcList.right - infoPtr->rcList.left;
5349 if (wWidth < nItemWidth)
5350 wWidth = nItemWidth;
5352 cols = wWidth / nItemWidth;
5353 if (cols > nItemCount)
5354 cols = nItemCount;
5355 if (cols < 1)
5356 cols = 1;
5358 if (nItemCount)
5360 rows = nItemCount / cols;
5361 if (nItemCount % cols)
5362 rows++;
5364 else
5365 rows = 0;
5367 wHeight = (nItemHeight * rows)+2;
5368 wWidth = (nItemWidth * cols)+2;
5370 dwViewRect = MAKELONG(wWidth, wHeight);
5372 else if (infoPtr->uView == LV_VIEW_SMALLICON)
5373 FIXME("uView == LV_VIEW_SMALLICON: not implemented\n");
5375 return dwViewRect;
5378 /***
5379 * DESCRIPTION:
5380 * Cancel edit label with saving item text.
5382 * PARAMETER(S):
5383 * [I] infoPtr : valid pointer to the listview structure
5385 * RETURN:
5386 * Always returns TRUE.
5388 static LRESULT LISTVIEW_CancelEditLabel(LISTVIEW_INFO *infoPtr)
5390 if (infoPtr->hwndEdit)
5392 /* handle value will be lost after LISTVIEW_EndEditLabelT */
5393 HWND edit = infoPtr->hwndEdit;
5395 LISTVIEW_EndEditLabelT(infoPtr, TRUE, IsWindowUnicode(infoPtr->hwndEdit));
5396 SendMessageW(edit, WM_CLOSE, 0, 0);
5399 return TRUE;
5402 /***
5403 * DESCRIPTION:
5404 * Create a drag image list for the specified item.
5406 * PARAMETER(S):
5407 * [I] infoPtr : valid pointer to the listview structure
5408 * [I] iItem : index of item
5409 * [O] lppt : Upper-left corner of the image
5411 * RETURN:
5412 * Returns a handle to the image list if successful, NULL otherwise.
5414 static HIMAGELIST LISTVIEW_CreateDragImage(LISTVIEW_INFO *infoPtr, INT iItem, LPPOINT lppt)
5416 RECT rcItem;
5417 SIZE size;
5418 POINT pos;
5419 HDC hdc, hdcOrig;
5420 HBITMAP hbmp, hOldbmp;
5421 HFONT hOldFont;
5422 HIMAGELIST dragList = 0;
5423 TRACE("iItem=%d Count=%d\n", iItem, infoPtr->nItemCount);
5425 if (iItem < 0 || iItem >= infoPtr->nItemCount || !lppt)
5426 return 0;
5428 rcItem.left = LVIR_BOUNDS;
5429 if (!LISTVIEW_GetItemRect(infoPtr, iItem, &rcItem))
5430 return 0;
5432 lppt->x = rcItem.left;
5433 lppt->y = rcItem.top;
5435 size.cx = rcItem.right - rcItem.left;
5436 size.cy = rcItem.bottom - rcItem.top;
5438 hdcOrig = GetDC(infoPtr->hwndSelf);
5439 hdc = CreateCompatibleDC(hdcOrig);
5440 hbmp = CreateCompatibleBitmap(hdcOrig, size.cx, size.cy);
5441 hOldbmp = SelectObject(hdc, hbmp);
5442 hOldFont = SelectObject(hdc, infoPtr->hFont);
5444 rcItem.left = rcItem.top = 0;
5445 rcItem.right = size.cx;
5446 rcItem.bottom = size.cy;
5447 FillRect(hdc, &rcItem, infoPtr->hBkBrush);
5449 pos.x = pos.y = 0;
5450 if (LISTVIEW_DrawItem(infoPtr, hdc, iItem, NULL, pos, CDRF_DODEFAULT))
5452 dragList = ImageList_Create(size.cx, size.cy, ILC_COLOR, 10, 10);
5453 SelectObject(hdc, hOldbmp);
5454 ImageList_Add(dragList, hbmp, 0);
5456 else
5457 SelectObject(hdc, hOldbmp);
5459 SelectObject(hdc, hOldFont);
5460 DeleteObject(hbmp);
5461 DeleteDC(hdc);
5462 ReleaseDC(infoPtr->hwndSelf, hdcOrig);
5464 TRACE("ret=%p\n", dragList);
5466 return dragList;
5470 /***
5471 * DESCRIPTION:
5472 * Removes all listview items and subitems.
5474 * PARAMETER(S):
5475 * [I] infoPtr : valid pointer to the listview structure
5477 * RETURN:
5478 * SUCCESS : TRUE
5479 * FAILURE : FALSE
5481 static BOOL LISTVIEW_DeleteAllItems(LISTVIEW_INFO *infoPtr, BOOL destroy)
5483 HDPA hdpaSubItems = NULL;
5484 BOOL suppress = FALSE;
5485 ITEMHDR *hdrItem;
5486 ITEM_INFO *lpItem;
5487 ITEM_ID *lpID;
5488 INT i, j;
5490 TRACE("()\n");
5492 /* we do it directly, to avoid notifications */
5493 ranges_clear(infoPtr->selectionRanges);
5494 infoPtr->nSelectionMark = -1;
5495 infoPtr->nFocusedItem = -1;
5496 SetRectEmpty(&infoPtr->rcFocus);
5497 /* But we are supposed to leave nHotItem as is! */
5499 /* send LVN_DELETEALLITEMS notification */
5500 if (!(infoPtr->dwStyle & LVS_OWNERDATA) || !destroy)
5502 NMLISTVIEW nmlv;
5504 memset(&nmlv, 0, sizeof(NMLISTVIEW));
5505 nmlv.iItem = -1;
5506 suppress = notify_listview(infoPtr, LVN_DELETEALLITEMS, &nmlv);
5509 for (i = infoPtr->nItemCount - 1; i >= 0; i--)
5511 if (!(infoPtr->dwStyle & LVS_OWNERDATA))
5513 /* send LVN_DELETEITEM notification, if not suppressed
5514 and if it is not a virtual listview */
5515 if (!suppress) notify_deleteitem(infoPtr, i);
5516 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, i);
5517 lpItem = DPA_GetPtr(hdpaSubItems, 0);
5518 /* free id struct */
5519 j = DPA_GetPtrIndex(infoPtr->hdpaItemIds, lpItem->id);
5520 lpID = DPA_GetPtr(infoPtr->hdpaItemIds, j);
5521 DPA_DeletePtr(infoPtr->hdpaItemIds, j);
5522 Free(lpID);
5523 /* both item and subitem start with ITEMHDR header */
5524 for (j = 0; j < DPA_GetPtrCount(hdpaSubItems); j++)
5526 hdrItem = DPA_GetPtr(hdpaSubItems, j);
5527 if (is_text(hdrItem->pszText)) Free(hdrItem->pszText);
5528 Free(hdrItem);
5530 DPA_Destroy(hdpaSubItems);
5531 DPA_DeletePtr(infoPtr->hdpaItems, i);
5533 DPA_DeletePtr(infoPtr->hdpaPosX, i);
5534 DPA_DeletePtr(infoPtr->hdpaPosY, i);
5535 infoPtr->nItemCount --;
5538 if (!destroy)
5540 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
5541 LISTVIEW_UpdateScroll(infoPtr);
5543 LISTVIEW_InvalidateList(infoPtr);
5545 return TRUE;
5548 /***
5549 * DESCRIPTION:
5550 * Scrolls, and updates the columns, when a column is changing width.
5552 * PARAMETER(S):
5553 * [I] infoPtr : valid pointer to the listview structure
5554 * [I] nColumn : column to scroll
5555 * [I] dx : amount of scroll, in pixels
5557 * RETURN:
5558 * None.
5560 static void LISTVIEW_ScrollColumns(LISTVIEW_INFO *infoPtr, INT nColumn, INT dx)
5562 COLUMN_INFO *lpColumnInfo;
5563 RECT rcOld, rcCol;
5564 POINT ptOrigin;
5565 INT nCol;
5566 HDITEMW hdi;
5568 if (nColumn < 0 || DPA_GetPtrCount(infoPtr->hdpaColumns) < 1) return;
5569 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, min(nColumn, DPA_GetPtrCount(infoPtr->hdpaColumns) - 1));
5570 rcCol = lpColumnInfo->rcHeader;
5571 if (nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns))
5572 rcCol.left = rcCol.right;
5574 /* adjust the other columns */
5575 hdi.mask = HDI_ORDER;
5576 if (SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nColumn, (LPARAM)&hdi))
5578 INT nOrder = hdi.iOrder;
5579 for (nCol = 0; nCol < DPA_GetPtrCount(infoPtr->hdpaColumns); nCol++)
5581 hdi.mask = HDI_ORDER;
5582 SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nCol, (LPARAM)&hdi);
5583 if (hdi.iOrder >= nOrder) {
5584 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, nCol);
5585 lpColumnInfo->rcHeader.left += dx;
5586 lpColumnInfo->rcHeader.right += dx;
5591 /* do not update screen if not in report mode */
5592 if (!is_redrawing(infoPtr) || infoPtr->uView != LV_VIEW_DETAILS) return;
5594 /* Need to reset the item width when inserting a new column */
5595 infoPtr->nItemWidth += dx;
5597 LISTVIEW_UpdateScroll(infoPtr);
5598 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
5600 /* scroll to cover the deleted column, and invalidate for redraw */
5601 rcOld = infoPtr->rcList;
5602 rcOld.left = ptOrigin.x + rcCol.left + dx;
5603 ScrollWindowEx(infoPtr->hwndSelf, dx, 0, &rcOld, &rcOld, 0, 0, SW_ERASE | SW_INVALIDATE);
5606 /***
5607 * DESCRIPTION:
5608 * Removes a column from the listview control.
5610 * PARAMETER(S):
5611 * [I] infoPtr : valid pointer to the listview structure
5612 * [I] nColumn : column index
5614 * RETURN:
5615 * SUCCESS : TRUE
5616 * FAILURE : FALSE
5618 static BOOL LISTVIEW_DeleteColumn(LISTVIEW_INFO *infoPtr, INT nColumn)
5620 RECT rcCol;
5622 TRACE("nColumn=%d\n", nColumn);
5624 if (nColumn < 0 || DPA_GetPtrCount(infoPtr->hdpaColumns) == 0
5625 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
5627 /* While the MSDN specifically says that column zero should not be deleted,
5628 what actually happens is that the column itself is deleted but no items or subitems
5629 are removed.
5632 LISTVIEW_GetHeaderRect(infoPtr, nColumn, &rcCol);
5634 if (!SendMessageW(infoPtr->hwndHeader, HDM_DELETEITEM, nColumn, 0))
5635 return FALSE;
5637 Free(DPA_GetPtr(infoPtr->hdpaColumns, nColumn));
5638 DPA_DeletePtr(infoPtr->hdpaColumns, nColumn);
5640 if (!(infoPtr->dwStyle & LVS_OWNERDATA) && nColumn)
5642 SUBITEM_INFO *lpSubItem, *lpDelItem;
5643 HDPA hdpaSubItems;
5644 INT nItem, nSubItem, i;
5646 for (nItem = 0; nItem < infoPtr->nItemCount; nItem++)
5648 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, nItem);
5649 nSubItem = 0;
5650 lpDelItem = 0;
5651 for (i = 1; i < DPA_GetPtrCount(hdpaSubItems); i++)
5653 lpSubItem = DPA_GetPtr(hdpaSubItems, i);
5654 if (lpSubItem->iSubItem == nColumn)
5656 nSubItem = i;
5657 lpDelItem = lpSubItem;
5659 else if (lpSubItem->iSubItem > nColumn)
5661 lpSubItem->iSubItem--;
5665 /* if we found our subitem, zap it */
5666 if (nSubItem > 0)
5668 /* free string */
5669 if (is_text(lpDelItem->hdr.pszText))
5670 Free(lpDelItem->hdr.pszText);
5672 /* free item */
5673 Free(lpDelItem);
5675 /* free dpa memory */
5676 DPA_DeletePtr(hdpaSubItems, nSubItem);
5681 /* update the other column info */
5682 if(DPA_GetPtrCount(infoPtr->hdpaColumns) == 0)
5683 LISTVIEW_InvalidateList(infoPtr);
5684 else
5685 LISTVIEW_ScrollColumns(infoPtr, nColumn, -(rcCol.right - rcCol.left));
5686 LISTVIEW_UpdateItemSize(infoPtr);
5688 return TRUE;
5691 /***
5692 * DESCRIPTION:
5693 * Invalidates the listview after an item's insertion or deletion.
5695 * PARAMETER(S):
5696 * [I] infoPtr : valid pointer to the listview structure
5697 * [I] nItem : item index
5698 * [I] dir : -1 if deleting, 1 if inserting
5700 * RETURN:
5701 * None
5703 static void LISTVIEW_ScrollOnInsert(LISTVIEW_INFO *infoPtr, INT nItem, INT dir)
5705 INT nPerCol, nItemCol, nItemRow;
5706 RECT rcScroll;
5707 POINT Origin;
5709 /* if we don't refresh, what's the point of scrolling? */
5710 if (!is_redrawing(infoPtr)) return;
5712 assert (abs(dir) == 1);
5714 /* arrange icons if autoarrange is on */
5715 if (is_autoarrange(infoPtr))
5717 BOOL arrange = TRUE;
5718 if (dir < 0 && nItem >= infoPtr->nItemCount) arrange = FALSE;
5719 if (dir > 0 && nItem == infoPtr->nItemCount - 1) arrange = FALSE;
5720 if (arrange) LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
5723 /* scrollbars need updating */
5724 LISTVIEW_UpdateScroll(infoPtr);
5726 /* figure out the item's position */
5727 if (infoPtr->uView == LV_VIEW_DETAILS)
5728 nPerCol = infoPtr->nItemCount + 1;
5729 else if (infoPtr->uView == LV_VIEW_LIST)
5730 nPerCol = LISTVIEW_GetCountPerColumn(infoPtr);
5731 else /* LV_VIEW_ICON, or LV_VIEW_SMALLICON */
5732 return;
5734 nItemCol = nItem / nPerCol;
5735 nItemRow = nItem % nPerCol;
5736 LISTVIEW_GetOrigin(infoPtr, &Origin);
5738 /* move the items below up a slot */
5739 rcScroll.left = nItemCol * infoPtr->nItemWidth;
5740 rcScroll.top = nItemRow * infoPtr->nItemHeight;
5741 rcScroll.right = rcScroll.left + infoPtr->nItemWidth;
5742 rcScroll.bottom = nPerCol * infoPtr->nItemHeight;
5743 OffsetRect(&rcScroll, Origin.x, Origin.y);
5744 TRACE("rcScroll=%s, dx=%d\n", wine_dbgstr_rect(&rcScroll), dir * infoPtr->nItemHeight);
5745 if (IntersectRect(&rcScroll, &rcScroll, &infoPtr->rcList))
5747 TRACE("Invalidating rcScroll=%s, rcList=%s\n", wine_dbgstr_rect(&rcScroll), wine_dbgstr_rect(&infoPtr->rcList));
5748 InvalidateRect(infoPtr->hwndSelf, &rcScroll, TRUE);
5751 /* report has only that column, so we're done */
5752 if (infoPtr->uView == LV_VIEW_DETAILS) return;
5754 /* now for LISTs, we have to deal with the columns to the right */
5755 rcScroll.left = (nItemCol + 1) * infoPtr->nItemWidth;
5756 rcScroll.top = 0;
5757 rcScroll.right = (infoPtr->nItemCount / nPerCol + 1) * infoPtr->nItemWidth;
5758 rcScroll.bottom = nPerCol * infoPtr->nItemHeight;
5759 OffsetRect(&rcScroll, Origin.x, Origin.y);
5760 if (IntersectRect(&rcScroll, &rcScroll, &infoPtr->rcList))
5761 InvalidateRect(infoPtr->hwndSelf, &rcScroll, TRUE);
5764 /***
5765 * DESCRIPTION:
5766 * Removes an item from the listview control.
5768 * PARAMETER(S):
5769 * [I] infoPtr : valid pointer to the listview structure
5770 * [I] nItem : item index
5772 * RETURN:
5773 * SUCCESS : TRUE
5774 * FAILURE : FALSE
5776 static BOOL LISTVIEW_DeleteItem(LISTVIEW_INFO *infoPtr, INT nItem)
5778 LVITEMW item;
5779 const BOOL is_icon = (infoPtr->uView == LV_VIEW_SMALLICON || infoPtr->uView == LV_VIEW_ICON);
5780 INT focus = infoPtr->nFocusedItem;
5782 TRACE("(nItem=%d)\n", nItem);
5784 if (nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
5786 /* remove selection, and focus */
5787 item.state = 0;
5788 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
5789 LISTVIEW_SetItemState(infoPtr, nItem, &item);
5791 /* send LVN_DELETEITEM notification. */
5792 if (!notify_deleteitem(infoPtr, nItem)) return FALSE;
5794 /* we need to do this here, because we'll be deleting stuff */
5795 if (is_icon)
5796 LISTVIEW_InvalidateItem(infoPtr, nItem);
5798 if (!(infoPtr->dwStyle & LVS_OWNERDATA))
5800 HDPA hdpaSubItems;
5801 ITEMHDR *hdrItem;
5802 ITEM_INFO *lpItem;
5803 ITEM_ID *lpID;
5804 INT i;
5806 hdpaSubItems = DPA_DeletePtr(infoPtr->hdpaItems, nItem);
5807 lpItem = DPA_GetPtr(hdpaSubItems, 0);
5809 /* free id struct */
5810 i = DPA_GetPtrIndex(infoPtr->hdpaItemIds, lpItem->id);
5811 lpID = DPA_GetPtr(infoPtr->hdpaItemIds, i);
5812 DPA_DeletePtr(infoPtr->hdpaItemIds, i);
5813 Free(lpID);
5814 for (i = 0; i < DPA_GetPtrCount(hdpaSubItems); i++)
5816 hdrItem = DPA_GetPtr(hdpaSubItems, i);
5817 if (is_text(hdrItem->pszText)) Free(hdrItem->pszText);
5818 Free(hdrItem);
5820 DPA_Destroy(hdpaSubItems);
5823 if (is_icon)
5825 DPA_DeletePtr(infoPtr->hdpaPosX, nItem);
5826 DPA_DeletePtr(infoPtr->hdpaPosY, nItem);
5829 infoPtr->nItemCount--;
5830 LISTVIEW_ShiftIndices(infoPtr, nItem, -1);
5831 LISTVIEW_ShiftFocus(infoPtr, focus, nItem, -1);
5833 /* now is the invalidation fun */
5834 if (!is_icon)
5835 LISTVIEW_ScrollOnInsert(infoPtr, nItem, -1);
5836 return TRUE;
5840 /***
5841 * DESCRIPTION:
5842 * Callback implementation for editlabel control
5844 * PARAMETER(S):
5845 * [I] infoPtr : valid pointer to the listview structure
5846 * [I] storeText : store edit box text as item text
5847 * [I] isW : TRUE if psxText is Unicode, FALSE if it's ANSI
5849 * RETURN:
5850 * SUCCESS : TRUE
5851 * FAILURE : FALSE
5853 static BOOL LISTVIEW_EndEditLabelT(LISTVIEW_INFO *infoPtr, BOOL storeText, BOOL isW)
5855 HWND hwndSelf = infoPtr->hwndSelf;
5856 WCHAR szDispText[DISP_TEXT_SIZE] = { 0 };
5857 NMLVDISPINFOW dispInfo;
5858 INT editedItem = infoPtr->nEditLabelItem;
5859 BOOL same;
5860 WCHAR *pszText = NULL;
5861 BOOL res;
5863 if (storeText)
5865 DWORD len = isW ? GetWindowTextLengthW(infoPtr->hwndEdit) : GetWindowTextLengthA(infoPtr->hwndEdit);
5867 if (len)
5869 if ((pszText = Alloc((len+1) * (isW ? sizeof(WCHAR) : sizeof(CHAR)))))
5871 if (isW) GetWindowTextW(infoPtr->hwndEdit, pszText, len+1);
5872 else GetWindowTextA(infoPtr->hwndEdit, (CHAR*)pszText, len+1);
5877 TRACE("(pszText=%s, isW=%d)\n", debugtext_t(pszText, isW), isW);
5879 ZeroMemory(&dispInfo, sizeof(dispInfo));
5880 dispInfo.item.mask = LVIF_PARAM | LVIF_STATE | LVIF_TEXT;
5881 dispInfo.item.iItem = editedItem;
5882 dispInfo.item.iSubItem = 0;
5883 dispInfo.item.stateMask = ~0;
5884 dispInfo.item.pszText = szDispText;
5885 dispInfo.item.cchTextMax = DISP_TEXT_SIZE;
5886 if (!LISTVIEW_GetItemT(infoPtr, &dispInfo.item, isW))
5888 res = FALSE;
5889 goto cleanup;
5892 if (isW)
5893 same = (lstrcmpW(dispInfo.item.pszText, pszText) == 0);
5894 else
5896 LPWSTR tmp = textdupTtoW(pszText, FALSE);
5897 same = (lstrcmpW(dispInfo.item.pszText, tmp) == 0);
5898 textfreeT(tmp, FALSE);
5901 /* add the text from the edit in */
5902 dispInfo.item.mask |= LVIF_TEXT;
5903 dispInfo.item.pszText = same ? NULL : pszText;
5904 dispInfo.item.cchTextMax = textlenT(dispInfo.item.pszText, isW);
5906 /* Do we need to update the Item Text */
5907 res = notify_dispinfoT(infoPtr, LVN_ENDLABELEDITW, &dispInfo, isW);
5909 infoPtr->nEditLabelItem = -1;
5910 infoPtr->hwndEdit = 0;
5912 if (!res) goto cleanup;
5914 if (!IsWindow(hwndSelf))
5916 res = FALSE;
5917 goto cleanup;
5919 if (!pszText) return TRUE;
5920 if (same)
5922 res = TRUE;
5923 goto cleanup;
5926 if (!(infoPtr->dwStyle & LVS_OWNERDATA))
5928 HDPA hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, editedItem);
5929 ITEM_INFO* lpItem = DPA_GetPtr(hdpaSubItems, 0);
5930 if (lpItem && lpItem->hdr.pszText == LPSTR_TEXTCALLBACKW)
5932 LISTVIEW_InvalidateItem(infoPtr, editedItem);
5933 res = TRUE;
5934 goto cleanup;
5938 ZeroMemory(&dispInfo, sizeof(dispInfo));
5939 dispInfo.item.mask = LVIF_TEXT;
5940 dispInfo.item.iItem = editedItem;
5941 dispInfo.item.iSubItem = 0;
5942 dispInfo.item.pszText = pszText;
5943 dispInfo.item.cchTextMax = textlenT(pszText, isW);
5944 res = LISTVIEW_SetItemT(infoPtr, &dispInfo.item, isW);
5946 cleanup:
5947 Free(pszText);
5949 return res;
5952 /***
5953 * DESCRIPTION:
5954 * Subclassed edit control windproc function
5956 * PARAMETER(S):
5957 * [I] hwnd : the edit window handle
5958 * [I] uMsg : the message that is to be processed
5959 * [I] wParam : first message parameter
5960 * [I] lParam : second message parameter
5961 * [I] isW : TRUE if input is Unicode
5963 * RETURN:
5964 * Zero.
5966 static LRESULT EditLblWndProcT(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL isW)
5968 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongPtrW(GetParent(hwnd), 0);
5969 BOOL save = TRUE;
5971 TRACE("(hwnd=%p, uMsg=%x, wParam=%lx, lParam=%lx, isW=%d)\n",
5972 hwnd, uMsg, wParam, lParam, isW);
5974 switch (uMsg)
5976 case WM_GETDLGCODE:
5977 return DLGC_WANTARROWS | DLGC_WANTALLKEYS;
5979 case WM_DESTROY:
5981 WNDPROC editProc = infoPtr->EditWndProc;
5982 infoPtr->EditWndProc = 0;
5983 SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (DWORD_PTR)editProc);
5984 return CallWindowProcT(editProc, hwnd, uMsg, wParam, lParam, isW);
5987 case WM_KEYDOWN:
5988 if (VK_ESCAPE == (INT)wParam)
5990 save = FALSE;
5991 break;
5993 else if (VK_RETURN == (INT)wParam)
5994 break;
5996 default:
5997 return CallWindowProcT(infoPtr->EditWndProc, hwnd, uMsg, wParam, lParam, isW);
6000 /* kill the edit */
6001 if (infoPtr->hwndEdit)
6002 LISTVIEW_EndEditLabelT(infoPtr, save, isW);
6004 SendMessageW(hwnd, WM_CLOSE, 0, 0);
6005 return 0;
6008 /***
6009 * DESCRIPTION:
6010 * Subclassed edit control Unicode windproc function
6012 * PARAMETER(S):
6013 * [I] hwnd : the edit window handle
6014 * [I] uMsg : the message that is to be processed
6015 * [I] wParam : first message parameter
6016 * [I] lParam : second message parameter
6018 * RETURN:
6020 static LRESULT CALLBACK EditLblWndProcW(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
6022 return EditLblWndProcT(hwnd, uMsg, wParam, lParam, TRUE);
6025 /***
6026 * DESCRIPTION:
6027 * Subclassed edit control ANSI windproc function
6029 * PARAMETER(S):
6030 * [I] hwnd : the edit window handle
6031 * [I] uMsg : the message that is to be processed
6032 * [I] wParam : first message parameter
6033 * [I] lParam : second message parameter
6035 * RETURN:
6037 static LRESULT CALLBACK EditLblWndProcA(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
6039 return EditLblWndProcT(hwnd, uMsg, wParam, lParam, FALSE);
6042 /***
6043 * DESCRIPTION:
6044 * Creates a subclassed edit control
6046 * PARAMETER(S):
6047 * [I] infoPtr : valid pointer to the listview structure
6048 * [I] text : initial text for the edit
6049 * [I] style : the window style
6050 * [I] isW : TRUE if input is Unicode
6052 * RETURN:
6054 static HWND CreateEditLabelT(LISTVIEW_INFO *infoPtr, LPCWSTR text, BOOL isW)
6056 static const DWORD style = WS_CHILDWINDOW|WS_CLIPSIBLINGS|ES_LEFT|ES_AUTOHSCROLL|WS_BORDER|WS_VISIBLE;
6057 HINSTANCE hinst = (HINSTANCE)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_HINSTANCE);
6058 HWND hedit;
6060 TRACE("(%p, text=%s, isW=%d)\n", infoPtr, debugtext_t(text, isW), isW);
6062 /* window will be resized and positioned after LVN_BEGINLABELEDIT */
6063 if (isW)
6064 hedit = CreateWindowW(WC_EDITW, text, style, 0, 0, 0, 0, infoPtr->hwndSelf, 0, hinst, 0);
6065 else
6066 hedit = CreateWindowA(WC_EDITA, (LPCSTR)text, style, 0, 0, 0, 0, infoPtr->hwndSelf, 0, hinst, 0);
6068 if (!hedit) return 0;
6070 infoPtr->EditWndProc = (WNDPROC)
6071 (isW ? SetWindowLongPtrW(hedit, GWLP_WNDPROC, (DWORD_PTR)EditLblWndProcW) :
6072 SetWindowLongPtrA(hedit, GWLP_WNDPROC, (DWORD_PTR)EditLblWndProcA) );
6074 SendMessageW(hedit, WM_SETFONT, (WPARAM)infoPtr->hFont, FALSE);
6075 SendMessageW(hedit, EM_SETLIMITTEXT, DISP_TEXT_SIZE-1, 0);
6077 return hedit;
6080 /***
6081 * DESCRIPTION:
6082 * Begin in place editing of specified list view item
6084 * PARAMETER(S):
6085 * [I] infoPtr : valid pointer to the listview structure
6086 * [I] nItem : item index
6087 * [I] isW : TRUE if it's a Unicode req, FALSE if ASCII
6089 * RETURN:
6090 * SUCCESS : TRUE
6091 * FAILURE : FALSE
6093 static HWND LISTVIEW_EditLabelT(LISTVIEW_INFO *infoPtr, INT nItem, BOOL isW)
6095 WCHAR disptextW[DISP_TEXT_SIZE] = { 0 };
6096 HWND hwndSelf = infoPtr->hwndSelf;
6097 NMLVDISPINFOW dispInfo;
6098 HFONT hOldFont = NULL;
6099 TEXTMETRICW tm;
6100 RECT rect;
6101 SIZE sz;
6102 HDC hdc;
6104 TRACE("(nItem=%d, isW=%d)\n", nItem, isW);
6106 if (~infoPtr->dwStyle & LVS_EDITLABELS) return 0;
6108 /* remove existing edit box */
6109 if (infoPtr->hwndEdit)
6111 SetFocus(infoPtr->hwndSelf);
6112 infoPtr->hwndEdit = 0;
6115 if (nItem < 0 || nItem >= infoPtr->nItemCount) return 0;
6117 infoPtr->nEditLabelItem = nItem;
6119 LISTVIEW_SetSelection(infoPtr, nItem);
6120 LISTVIEW_SetItemFocus(infoPtr, nItem);
6121 LISTVIEW_InvalidateItem(infoPtr, nItem);
6123 rect.left = LVIR_LABEL;
6124 if (!LISTVIEW_GetItemRect(infoPtr, nItem, &rect)) return 0;
6126 ZeroMemory(&dispInfo, sizeof(dispInfo));
6127 dispInfo.item.mask = LVIF_PARAM | LVIF_STATE | LVIF_TEXT;
6128 dispInfo.item.iItem = nItem;
6129 dispInfo.item.iSubItem = 0;
6130 dispInfo.item.stateMask = ~0;
6131 dispInfo.item.pszText = disptextW;
6132 dispInfo.item.cchTextMax = DISP_TEXT_SIZE;
6133 if (!LISTVIEW_GetItemT(infoPtr, &dispInfo.item, isW)) return 0;
6135 infoPtr->hwndEdit = CreateEditLabelT(infoPtr, dispInfo.item.pszText, isW);
6136 if (!infoPtr->hwndEdit) return 0;
6138 if (notify_dispinfoT(infoPtr, LVN_BEGINLABELEDITW, &dispInfo, isW))
6140 if (!IsWindow(hwndSelf))
6141 return 0;
6142 SendMessageW(infoPtr->hwndEdit, WM_CLOSE, 0, 0);
6143 infoPtr->hwndEdit = 0;
6144 return 0;
6147 TRACE("disp text=%s\n", debugtext_t(dispInfo.item.pszText, isW));
6149 /* position and display edit box */
6150 hdc = GetDC(infoPtr->hwndSelf);
6152 /* select the font to get appropriate metric dimensions */
6153 if (infoPtr->hFont)
6154 hOldFont = SelectObject(hdc, infoPtr->hFont);
6156 /* use real edit box content, it could be altered during LVN_BEGINLABELEDIT notification */
6157 GetWindowTextW(infoPtr->hwndEdit, disptextW, DISP_TEXT_SIZE);
6158 TRACE("edit box text=%s\n", debugstr_w(disptextW));
6160 /* get string length in pixels */
6161 GetTextExtentPoint32W(hdc, disptextW, lstrlenW(disptextW), &sz);
6163 /* add extra spacing for the next character */
6164 GetTextMetricsW(hdc, &tm);
6165 sz.cx += tm.tmMaxCharWidth * 2;
6167 if (infoPtr->hFont)
6168 SelectObject(hdc, hOldFont);
6170 ReleaseDC(infoPtr->hwndSelf, hdc);
6172 sz.cy = rect.bottom - rect.top + 2;
6173 rect.left -= 2;
6174 rect.top -= 1;
6175 TRACE("moving edit=(%d,%d)-(%d,%d)\n", rect.left, rect.top, sz.cx, sz.cy);
6176 MoveWindow(infoPtr->hwndEdit, rect.left, rect.top, sz.cx, sz.cy, FALSE);
6177 ShowWindow(infoPtr->hwndEdit, SW_NORMAL);
6178 SetFocus(infoPtr->hwndEdit);
6179 SendMessageW(infoPtr->hwndEdit, EM_SETSEL, 0, -1);
6180 return infoPtr->hwndEdit;
6184 /***
6185 * DESCRIPTION:
6186 * Ensures the specified item is visible, scrolling into view if necessary.
6188 * PARAMETER(S):
6189 * [I] infoPtr : valid pointer to the listview structure
6190 * [I] nItem : item index
6191 * [I] bPartial : partially or entirely visible
6193 * RETURN:
6194 * SUCCESS : TRUE
6195 * FAILURE : FALSE
6197 static BOOL LISTVIEW_EnsureVisible(LISTVIEW_INFO *infoPtr, INT nItem, BOOL bPartial)
6199 INT nScrollPosHeight = 0;
6200 INT nScrollPosWidth = 0;
6201 INT nHorzAdjust = 0;
6202 INT nVertAdjust = 0;
6203 INT nHorzDiff = 0;
6204 INT nVertDiff = 0;
6205 RECT rcItem, rcTemp;
6207 rcItem.left = LVIR_BOUNDS;
6208 if (!LISTVIEW_GetItemRect(infoPtr, nItem, &rcItem)) return FALSE;
6210 if (bPartial && IntersectRect(&rcTemp, &infoPtr->rcList, &rcItem)) return TRUE;
6212 if (rcItem.left < infoPtr->rcList.left || rcItem.right > infoPtr->rcList.right)
6214 /* scroll left/right, but in LV_VIEW_DETAILS mode */
6215 if (infoPtr->uView == LV_VIEW_LIST)
6216 nScrollPosWidth = infoPtr->nItemWidth;
6217 else if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
6218 nScrollPosWidth = 1;
6220 if (rcItem.left < infoPtr->rcList.left)
6222 nHorzAdjust = -1;
6223 if (infoPtr->uView != LV_VIEW_DETAILS) nHorzDiff = rcItem.left - infoPtr->rcList.left;
6225 else
6227 nHorzAdjust = 1;
6228 if (infoPtr->uView != LV_VIEW_DETAILS) nHorzDiff = rcItem.right - infoPtr->rcList.right;
6232 if (rcItem.top < infoPtr->rcList.top || rcItem.bottom > infoPtr->rcList.bottom)
6234 /* scroll up/down, but not in LVS_LIST mode */
6235 if (infoPtr->uView == LV_VIEW_DETAILS)
6236 nScrollPosHeight = infoPtr->nItemHeight;
6237 else if ((infoPtr->uView == LV_VIEW_ICON) || (infoPtr->uView == LV_VIEW_SMALLICON))
6238 nScrollPosHeight = 1;
6240 if (rcItem.top < infoPtr->rcList.top)
6242 nVertAdjust = -1;
6243 if (infoPtr->uView != LV_VIEW_LIST) nVertDiff = rcItem.top - infoPtr->rcList.top;
6245 else
6247 nVertAdjust = 1;
6248 if (infoPtr->uView != LV_VIEW_LIST) nVertDiff = rcItem.bottom - infoPtr->rcList.bottom;
6252 if (!nScrollPosWidth && !nScrollPosHeight) return TRUE;
6254 if (nScrollPosWidth)
6256 INT diff = nHorzDiff / nScrollPosWidth;
6257 if (nHorzDiff % nScrollPosWidth) diff += nHorzAdjust;
6258 LISTVIEW_HScroll(infoPtr, SB_INTERNAL, diff);
6261 if (nScrollPosHeight)
6263 INT diff = nVertDiff / nScrollPosHeight;
6264 if (nVertDiff % nScrollPosHeight) diff += nVertAdjust;
6265 LISTVIEW_VScroll(infoPtr, SB_INTERNAL, diff);
6268 return TRUE;
6271 /***
6272 * DESCRIPTION:
6273 * Searches for an item with specific characteristics.
6275 * PARAMETER(S):
6276 * [I] hwnd : window handle
6277 * [I] nStart : base item index
6278 * [I] lpFindInfo : item information to look for
6280 * RETURN:
6281 * SUCCESS : index of item
6282 * FAILURE : -1
6284 static INT LISTVIEW_FindItemW(const LISTVIEW_INFO *infoPtr, INT nStart,
6285 const LVFINDINFOW *lpFindInfo)
6287 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
6288 BOOL bWrap = FALSE, bNearest = FALSE;
6289 INT nItem = nStart + 1, nLast = infoPtr->nItemCount, nNearestItem = -1;
6290 ULONG xdist, ydist, dist, mindist = 0x7fffffff;
6291 POINT Position, Destination;
6292 LVITEMW lvItem;
6294 /* Search in virtual listviews should be done by application, not by
6295 listview control, so we just send LVN_ODFINDITEMW and return the result */
6296 if (infoPtr->dwStyle & LVS_OWNERDATA)
6298 NMLVFINDITEMW nmlv;
6300 nmlv.iStart = nStart;
6301 nmlv.lvfi = *lpFindInfo;
6302 return notify_hdr(infoPtr, LVN_ODFINDITEMW, (LPNMHDR)&nmlv.hdr);
6305 if (!lpFindInfo || nItem < 0) return -1;
6307 lvItem.mask = 0;
6308 if (lpFindInfo->flags & (LVFI_STRING | LVFI_PARTIAL) ||
6309 lpFindInfo->flags & LVFI_SUBSTRING)
6311 lvItem.mask |= LVIF_TEXT;
6312 lvItem.pszText = szDispText;
6313 lvItem.cchTextMax = DISP_TEXT_SIZE;
6316 if (lpFindInfo->flags & LVFI_WRAP)
6317 bWrap = TRUE;
6319 if ((lpFindInfo->flags & LVFI_NEARESTXY) &&
6320 (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON))
6322 POINT Origin;
6323 RECT rcArea;
6325 LISTVIEW_GetOrigin(infoPtr, &Origin);
6326 Destination.x = lpFindInfo->pt.x - Origin.x;
6327 Destination.y = lpFindInfo->pt.y - Origin.y;
6328 switch(lpFindInfo->vkDirection)
6330 case VK_DOWN: Destination.y += infoPtr->nItemHeight; break;
6331 case VK_UP: Destination.y -= infoPtr->nItemHeight; break;
6332 case VK_RIGHT: Destination.x += infoPtr->nItemWidth; break;
6333 case VK_LEFT: Destination.x -= infoPtr->nItemWidth; break;
6334 case VK_HOME: Destination.x = Destination.y = 0; break;
6335 case VK_NEXT: Destination.y += infoPtr->rcList.bottom - infoPtr->rcList.top; break;
6336 case VK_PRIOR: Destination.y -= infoPtr->rcList.bottom - infoPtr->rcList.top; break;
6337 case VK_END:
6338 LISTVIEW_GetAreaRect(infoPtr, &rcArea);
6339 Destination.x = rcArea.right;
6340 Destination.y = rcArea.bottom;
6341 break;
6342 default: ERR("Unknown vkDirection=%d\n", lpFindInfo->vkDirection);
6344 bNearest = TRUE;
6346 else Destination.x = Destination.y = 0;
6348 /* if LVFI_PARAM is specified, all other flags are ignored */
6349 if (lpFindInfo->flags & LVFI_PARAM)
6351 lvItem.mask |= LVIF_PARAM;
6352 bNearest = FALSE;
6353 lvItem.mask &= ~LVIF_TEXT;
6356 again:
6357 for (; nItem < nLast; nItem++)
6359 lvItem.iItem = nItem;
6360 lvItem.iSubItem = 0;
6361 lvItem.pszText = szDispText;
6362 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) continue;
6364 if (lvItem.mask & LVIF_PARAM)
6366 if (lpFindInfo->lParam == lvItem.lParam)
6367 return nItem;
6368 else
6369 continue;
6372 if (lvItem.mask & LVIF_TEXT)
6374 if (lpFindInfo->flags & (LVFI_PARTIAL | LVFI_SUBSTRING))
6376 WCHAR *p = strstrW(lvItem.pszText, lpFindInfo->psz);
6377 if (!p || p != lvItem.pszText) continue;
6379 else
6381 if (lstrcmpW(lvItem.pszText, lpFindInfo->psz) != 0) continue;
6385 if (!bNearest) return nItem;
6387 /* This is very inefficient. To do a good job here,
6388 * we need a sorted array of (x,y) item positions */
6389 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
6391 /* compute the distance^2 to the destination */
6392 xdist = Destination.x - Position.x;
6393 ydist = Destination.y - Position.y;
6394 dist = xdist * xdist + ydist * ydist;
6396 /* remember the distance, and item if it's closer */
6397 if (dist < mindist)
6399 mindist = dist;
6400 nNearestItem = nItem;
6404 if (bWrap)
6406 nItem = 0;
6407 nLast = min(nStart + 1, infoPtr->nItemCount);
6408 bWrap = FALSE;
6409 goto again;
6412 return nNearestItem;
6415 /***
6416 * DESCRIPTION:
6417 * Searches for an item with specific characteristics.
6419 * PARAMETER(S):
6420 * [I] hwnd : window handle
6421 * [I] nStart : base item index
6422 * [I] lpFindInfo : item information to look for
6424 * RETURN:
6425 * SUCCESS : index of item
6426 * FAILURE : -1
6428 static INT LISTVIEW_FindItemA(const LISTVIEW_INFO *infoPtr, INT nStart,
6429 const LVFINDINFOA *lpFindInfo)
6431 BOOL hasText = lpFindInfo->flags & (LVFI_STRING | LVFI_PARTIAL) ||
6432 lpFindInfo->flags & LVFI_SUBSTRING;
6433 LVFINDINFOW fiw;
6434 INT res;
6435 LPWSTR strW = NULL;
6437 memcpy(&fiw, lpFindInfo, sizeof(fiw));
6438 if (hasText) fiw.psz = strW = textdupTtoW((LPCWSTR)lpFindInfo->psz, FALSE);
6439 res = LISTVIEW_FindItemW(infoPtr, nStart, &fiw);
6440 textfreeT(strW, FALSE);
6441 return res;
6444 /***
6445 * DESCRIPTION:
6446 * Retrieves column attributes.
6448 * PARAMETER(S):
6449 * [I] infoPtr : valid pointer to the listview structure
6450 * [I] nColumn : column index
6451 * [IO] lpColumn : column information
6452 * [I] isW : if TRUE, then lpColumn is a LPLVCOLUMNW
6453 * otherwise it is in fact a LPLVCOLUMNA
6455 * RETURN:
6456 * SUCCESS : TRUE
6457 * FAILURE : FALSE
6459 static BOOL LISTVIEW_GetColumnT(const LISTVIEW_INFO *infoPtr, INT nColumn, LPLVCOLUMNW lpColumn, BOOL isW)
6461 COLUMN_INFO *lpColumnInfo;
6462 HDITEMW hdi;
6464 if (!lpColumn || nColumn < 0 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
6465 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, nColumn);
6467 /* initialize memory */
6468 ZeroMemory(&hdi, sizeof(hdi));
6470 if (lpColumn->mask & LVCF_TEXT)
6472 hdi.mask |= HDI_TEXT;
6473 hdi.pszText = lpColumn->pszText;
6474 hdi.cchTextMax = lpColumn->cchTextMax;
6477 if (lpColumn->mask & LVCF_IMAGE)
6478 hdi.mask |= HDI_IMAGE;
6480 if (lpColumn->mask & LVCF_ORDER)
6481 hdi.mask |= HDI_ORDER;
6483 if (lpColumn->mask & LVCF_SUBITEM)
6484 hdi.mask |= HDI_LPARAM;
6486 if (!SendMessageW(infoPtr->hwndHeader, isW ? HDM_GETITEMW : HDM_GETITEMA, nColumn, (LPARAM)&hdi)) return FALSE;
6488 if (lpColumn->mask & LVCF_FMT)
6489 lpColumn->fmt = lpColumnInfo->fmt;
6491 if (lpColumn->mask & LVCF_WIDTH)
6492 lpColumn->cx = lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left;
6494 if (lpColumn->mask & LVCF_IMAGE)
6495 lpColumn->iImage = hdi.iImage;
6497 if (lpColumn->mask & LVCF_ORDER)
6498 lpColumn->iOrder = hdi.iOrder;
6500 if (lpColumn->mask & LVCF_SUBITEM)
6501 lpColumn->iSubItem = hdi.lParam;
6503 if (lpColumn->mask & LVCF_MINWIDTH)
6504 lpColumn->cxMin = lpColumnInfo->cxMin;
6506 return TRUE;
6509 static inline BOOL LISTVIEW_GetColumnOrderArray(const LISTVIEW_INFO *infoPtr, INT iCount, LPINT lpiArray)
6511 if (!infoPtr->hwndHeader) return FALSE;
6512 return SendMessageW(infoPtr->hwndHeader, HDM_GETORDERARRAY, iCount, (LPARAM)lpiArray);
6515 /***
6516 * DESCRIPTION:
6517 * Retrieves the column width.
6519 * PARAMETER(S):
6520 * [I] infoPtr : valid pointer to the listview structure
6521 * [I] int : column index
6523 * RETURN:
6524 * SUCCESS : column width
6525 * FAILURE : zero
6527 static INT LISTVIEW_GetColumnWidth(const LISTVIEW_INFO *infoPtr, INT nColumn)
6529 INT nColumnWidth = 0;
6530 HDITEMW hdItem;
6532 TRACE("nColumn=%d\n", nColumn);
6534 /* we have a 'column' in LIST and REPORT mode only */
6535 switch(infoPtr->uView)
6537 case LV_VIEW_LIST:
6538 nColumnWidth = infoPtr->nItemWidth;
6539 break;
6540 case LV_VIEW_DETAILS:
6541 /* We are not using LISTVIEW_GetHeaderRect as this data is updated only after a HDN_ITEMCHANGED.
6542 * There is an application that subclasses the listview, calls LVM_GETCOLUMNWIDTH in the
6543 * HDN_ITEMCHANGED handler and goes into infinite recursion if it receives old data.
6545 hdItem.mask = HDI_WIDTH;
6546 if (!SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nColumn, (LPARAM)&hdItem))
6548 WARN("(%p): HDM_GETITEMW failed for item %d\n", infoPtr->hwndSelf, nColumn);
6549 return 0;
6551 nColumnWidth = hdItem.cxy;
6552 break;
6555 TRACE("nColumnWidth=%d\n", nColumnWidth);
6556 return nColumnWidth;
6559 /***
6560 * DESCRIPTION:
6561 * In list or report display mode, retrieves the number of items that can fit
6562 * vertically in the visible area. In icon or small icon display mode,
6563 * retrieves the total number of visible items.
6565 * PARAMETER(S):
6566 * [I] infoPtr : valid pointer to the listview structure
6568 * RETURN:
6569 * Number of fully visible items.
6571 static INT LISTVIEW_GetCountPerPage(const LISTVIEW_INFO *infoPtr)
6573 switch (infoPtr->uView)
6575 case LV_VIEW_ICON:
6576 case LV_VIEW_SMALLICON:
6577 return infoPtr->nItemCount;
6578 case LV_VIEW_DETAILS:
6579 return LISTVIEW_GetCountPerColumn(infoPtr);
6580 case LV_VIEW_LIST:
6581 return LISTVIEW_GetCountPerRow(infoPtr) * LISTVIEW_GetCountPerColumn(infoPtr);
6583 assert(FALSE);
6584 return 0;
6587 /***
6588 * DESCRIPTION:
6589 * Retrieves an image list handle.
6591 * PARAMETER(S):
6592 * [I] infoPtr : valid pointer to the listview structure
6593 * [I] nImageList : image list identifier
6595 * RETURN:
6596 * SUCCESS : image list handle
6597 * FAILURE : NULL
6599 static HIMAGELIST LISTVIEW_GetImageList(const LISTVIEW_INFO *infoPtr, INT nImageList)
6601 switch (nImageList)
6603 case LVSIL_NORMAL: return infoPtr->himlNormal;
6604 case LVSIL_SMALL: return infoPtr->himlSmall;
6605 case LVSIL_STATE: return infoPtr->himlState;
6606 case LVSIL_GROUPHEADER:
6607 FIXME("LVSIL_GROUPHEADER not supported\n");
6608 break;
6609 default:
6610 WARN("got unknown imagelist index - %d\n", nImageList);
6612 return NULL;
6615 /* LISTVIEW_GetISearchString */
6617 /***
6618 * DESCRIPTION:
6619 * Retrieves item attributes.
6621 * PARAMETER(S):
6622 * [I] hwnd : window handle
6623 * [IO] lpLVItem : item info
6624 * [I] isW : if TRUE, then lpLVItem is a LPLVITEMW,
6625 * if FALSE, then lpLVItem is a LPLVITEMA.
6627 * NOTE:
6628 * This is the internal 'GetItem' interface -- it tries to
6629 * be smart and avoid text copies, if possible, by modifying
6630 * lpLVItem->pszText to point to the text string. Please note
6631 * that this is not always possible (e.g. OWNERDATA), so on
6632 * entry you *must* supply valid values for pszText, and cchTextMax.
6633 * The only difference to the documented interface is that upon
6634 * return, you should use *only* the lpLVItem->pszText, rather than
6635 * the buffer pointer you provided on input. Most code already does
6636 * that, so it's not a problem.
6637 * For the two cases when the text must be copied (that is,
6638 * for LVM_GETITEM, and LVM_GETITEMTEXT), use LISTVIEW_GetItemExtT.
6640 * RETURN:
6641 * SUCCESS : TRUE
6642 * FAILURE : FALSE
6644 static BOOL LISTVIEW_GetItemT(const LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem, BOOL isW)
6646 ITEMHDR callbackHdr = { LPSTR_TEXTCALLBACKW, I_IMAGECALLBACK };
6647 NMLVDISPINFOW dispInfo;
6648 ITEM_INFO *lpItem;
6649 ITEMHDR* pItemHdr;
6650 HDPA hdpaSubItems;
6651 INT isubitem;
6653 TRACE("(item=%s, isW=%d)\n", debuglvitem_t(lpLVItem, isW), isW);
6655 if (!lpLVItem || lpLVItem->iItem < 0 || lpLVItem->iItem >= infoPtr->nItemCount)
6656 return FALSE;
6658 if (lpLVItem->mask == 0) return TRUE;
6659 TRACE("mask=%x\n", lpLVItem->mask);
6661 /* make a local copy */
6662 isubitem = lpLVItem->iSubItem;
6664 /* a quick optimization if all we're asked is the focus state
6665 * these queries are worth optimising since they are common,
6666 * and can be answered in constant time, without the heavy accesses */
6667 if ( (lpLVItem->mask == LVIF_STATE) && (lpLVItem->stateMask == LVIS_FOCUSED) &&
6668 !(infoPtr->uCallbackMask & LVIS_FOCUSED) )
6670 lpLVItem->state = 0;
6671 if (infoPtr->nFocusedItem == lpLVItem->iItem)
6672 lpLVItem->state |= LVIS_FOCUSED;
6673 return TRUE;
6676 ZeroMemory(&dispInfo, sizeof(dispInfo));
6678 /* if the app stores all the data, handle it separately */
6679 if (infoPtr->dwStyle & LVS_OWNERDATA)
6681 dispInfo.item.state = 0;
6683 /* apparently, we should not callback for lParam in LVS_OWNERDATA */
6684 if ((lpLVItem->mask & ~(LVIF_STATE | LVIF_PARAM)) ||
6685 ((lpLVItem->mask & LVIF_STATE) && (infoPtr->uCallbackMask & lpLVItem->stateMask)))
6687 UINT mask = lpLVItem->mask;
6689 /* NOTE: copy only fields which we _know_ are initialized, some apps
6690 * depend on the uninitialized fields being 0 */
6691 dispInfo.item.mask = lpLVItem->mask & ~LVIF_PARAM;
6692 dispInfo.item.iItem = lpLVItem->iItem;
6693 dispInfo.item.iSubItem = isubitem;
6694 if (lpLVItem->mask & LVIF_TEXT)
6696 if (lpLVItem->mask & LVIF_NORECOMPUTE)
6697 /* reset mask */
6698 dispInfo.item.mask &= ~(LVIF_TEXT | LVIF_NORECOMPUTE);
6699 else
6701 dispInfo.item.pszText = lpLVItem->pszText;
6702 dispInfo.item.cchTextMax = lpLVItem->cchTextMax;
6705 if (lpLVItem->mask & LVIF_STATE)
6706 dispInfo.item.stateMask = lpLVItem->stateMask & infoPtr->uCallbackMask;
6707 /* could be zeroed on LVIF_NORECOMPUTE case */
6708 if (dispInfo.item.mask)
6710 notify_dispinfoT(infoPtr, LVN_GETDISPINFOW, &dispInfo, isW);
6711 dispInfo.item.stateMask = lpLVItem->stateMask;
6712 if (lpLVItem->mask & (LVIF_GROUPID|LVIF_COLUMNS))
6714 /* full size structure expected - _WIN32IE >= 0x560 */
6715 *lpLVItem = dispInfo.item;
6717 else if (lpLVItem->mask & LVIF_INDENT)
6719 /* indent member expected - _WIN32IE >= 0x300 */
6720 memcpy(lpLVItem, &dispInfo.item, offsetof( LVITEMW, iGroupId ));
6722 else
6724 /* minimal structure expected */
6725 memcpy(lpLVItem, &dispInfo.item, offsetof( LVITEMW, iIndent ));
6727 lpLVItem->mask = mask;
6728 TRACE(" getdispinfo(1):lpLVItem=%s\n", debuglvitem_t(lpLVItem, isW));
6732 /* make sure lParam is zeroed out */
6733 if (lpLVItem->mask & LVIF_PARAM) lpLVItem->lParam = 0;
6735 /* callback marked pointer required here */
6736 if ((lpLVItem->mask & LVIF_TEXT) && (lpLVItem->mask & LVIF_NORECOMPUTE))
6737 lpLVItem->pszText = LPSTR_TEXTCALLBACKW;
6739 /* we store only a little state, so if we're not asked, we're done */
6740 if (!(lpLVItem->mask & LVIF_STATE) || isubitem) return TRUE;
6742 /* if focus is handled by us, report it */
6743 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_FOCUSED )
6745 lpLVItem->state &= ~LVIS_FOCUSED;
6746 if (infoPtr->nFocusedItem == lpLVItem->iItem)
6747 lpLVItem->state |= LVIS_FOCUSED;
6750 /* and do the same for selection, if we handle it */
6751 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_SELECTED )
6753 lpLVItem->state &= ~LVIS_SELECTED;
6754 if (ranges_contain(infoPtr->selectionRanges, lpLVItem->iItem))
6755 lpLVItem->state |= LVIS_SELECTED;
6758 return TRUE;
6761 /* find the item and subitem structures before we proceed */
6762 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, lpLVItem->iItem);
6763 lpItem = DPA_GetPtr(hdpaSubItems, 0);
6764 assert (lpItem);
6766 if (isubitem)
6768 SUBITEM_INFO *lpSubItem = LISTVIEW_GetSubItemPtr(hdpaSubItems, isubitem);
6769 pItemHdr = lpSubItem ? &lpSubItem->hdr : &callbackHdr;
6770 if (!lpSubItem)
6772 WARN(" iSubItem invalid (%08x), ignored.\n", isubitem);
6773 isubitem = 0;
6776 else
6777 pItemHdr = &lpItem->hdr;
6779 /* Do we need to query the state from the app? */
6780 if ((lpLVItem->mask & LVIF_STATE) && infoPtr->uCallbackMask && isubitem == 0)
6782 dispInfo.item.mask |= LVIF_STATE;
6783 dispInfo.item.stateMask = infoPtr->uCallbackMask;
6786 /* Do we need to enquire about the image? */
6787 if ((lpLVItem->mask & LVIF_IMAGE) && pItemHdr->iImage == I_IMAGECALLBACK &&
6788 (isubitem == 0 || (infoPtr->dwLvExStyle & LVS_EX_SUBITEMIMAGES)))
6790 dispInfo.item.mask |= LVIF_IMAGE;
6791 dispInfo.item.iImage = I_IMAGECALLBACK;
6794 /* Only items support indentation */
6795 if ((lpLVItem->mask & LVIF_INDENT) && lpItem->iIndent == I_INDENTCALLBACK &&
6796 (isubitem == 0))
6798 dispInfo.item.mask |= LVIF_INDENT;
6799 dispInfo.item.iIndent = I_INDENTCALLBACK;
6802 /* Apps depend on calling back for text if it is NULL or LPSTR_TEXTCALLBACKW */
6803 if ((lpLVItem->mask & LVIF_TEXT) && !(lpLVItem->mask & LVIF_NORECOMPUTE) &&
6804 !is_text(pItemHdr->pszText))
6806 dispInfo.item.mask |= LVIF_TEXT;
6807 dispInfo.item.pszText = lpLVItem->pszText;
6808 dispInfo.item.cchTextMax = lpLVItem->cchTextMax;
6809 if (dispInfo.item.pszText && dispInfo.item.cchTextMax > 0)
6810 *dispInfo.item.pszText = '\0';
6813 /* If we don't have all the requested info, query the application */
6814 if (dispInfo.item.mask)
6816 dispInfo.item.iItem = lpLVItem->iItem;
6817 dispInfo.item.iSubItem = lpLVItem->iSubItem; /* yes: the original subitem */
6818 dispInfo.item.lParam = lpItem->lParam;
6819 notify_dispinfoT(infoPtr, LVN_GETDISPINFOW, &dispInfo, isW);
6820 TRACE(" getdispinfo(2):item=%s\n", debuglvitem_t(&dispInfo.item, isW));
6823 /* we should not store values for subitems */
6824 if (isubitem) dispInfo.item.mask &= ~LVIF_DI_SETITEM;
6826 /* Now, handle the iImage field */
6827 if (dispInfo.item.mask & LVIF_IMAGE)
6829 lpLVItem->iImage = dispInfo.item.iImage;
6830 if ((dispInfo.item.mask & LVIF_DI_SETITEM) && pItemHdr->iImage == I_IMAGECALLBACK)
6831 pItemHdr->iImage = dispInfo.item.iImage;
6833 else if (lpLVItem->mask & LVIF_IMAGE)
6835 if(isubitem == 0 || (infoPtr->dwLvExStyle & LVS_EX_SUBITEMIMAGES))
6836 lpLVItem->iImage = pItemHdr->iImage;
6837 else
6838 lpLVItem->iImage = 0;
6841 /* The pszText field */
6842 if (dispInfo.item.mask & LVIF_TEXT)
6844 if ((dispInfo.item.mask & LVIF_DI_SETITEM) && pItemHdr->pszText)
6845 textsetptrT(&pItemHdr->pszText, dispInfo.item.pszText, isW);
6847 lpLVItem->pszText = dispInfo.item.pszText;
6849 else if (lpLVItem->mask & LVIF_TEXT)
6851 /* if LVN_GETDISPINFO's disabled with LVIF_NORECOMPUTE return callback placeholder */
6852 if (isW || !is_text(pItemHdr->pszText)) lpLVItem->pszText = pItemHdr->pszText;
6853 else textcpynT(lpLVItem->pszText, isW, pItemHdr->pszText, TRUE, lpLVItem->cchTextMax);
6856 /* Next is the lParam field */
6857 if (dispInfo.item.mask & LVIF_PARAM)
6859 lpLVItem->lParam = dispInfo.item.lParam;
6860 if ((dispInfo.item.mask & LVIF_DI_SETITEM))
6861 lpItem->lParam = dispInfo.item.lParam;
6863 else if (lpLVItem->mask & LVIF_PARAM)
6864 lpLVItem->lParam = lpItem->lParam;
6866 /* if this is a subitem, we're done */
6867 if (isubitem) return TRUE;
6869 /* ... the state field (this one is different due to uCallbackmask) */
6870 if (lpLVItem->mask & LVIF_STATE)
6872 lpLVItem->state = lpItem->state & lpLVItem->stateMask;
6873 if (dispInfo.item.mask & LVIF_STATE)
6875 lpLVItem->state &= ~dispInfo.item.stateMask;
6876 lpLVItem->state |= (dispInfo.item.state & dispInfo.item.stateMask);
6878 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_FOCUSED )
6880 lpLVItem->state &= ~LVIS_FOCUSED;
6881 if (infoPtr->nFocusedItem == lpLVItem->iItem)
6882 lpLVItem->state |= LVIS_FOCUSED;
6884 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_SELECTED )
6886 lpLVItem->state &= ~LVIS_SELECTED;
6887 if (ranges_contain(infoPtr->selectionRanges, lpLVItem->iItem))
6888 lpLVItem->state |= LVIS_SELECTED;
6892 /* and last, but not least, the indent field */
6893 if (dispInfo.item.mask & LVIF_INDENT)
6895 lpLVItem->iIndent = dispInfo.item.iIndent;
6896 if ((dispInfo.item.mask & LVIF_DI_SETITEM) && lpItem->iIndent == I_INDENTCALLBACK)
6897 lpItem->iIndent = dispInfo.item.iIndent;
6899 else if (lpLVItem->mask & LVIF_INDENT)
6901 lpLVItem->iIndent = lpItem->iIndent;
6904 return TRUE;
6907 /***
6908 * DESCRIPTION:
6909 * Retrieves item attributes.
6911 * PARAMETER(S):
6912 * [I] hwnd : window handle
6913 * [IO] lpLVItem : item info
6914 * [I] isW : if TRUE, then lpLVItem is a LPLVITEMW,
6915 * if FALSE, then lpLVItem is a LPLVITEMA.
6917 * NOTE:
6918 * This is the external 'GetItem' interface -- it properly copies
6919 * the text in the provided buffer.
6921 * RETURN:
6922 * SUCCESS : TRUE
6923 * FAILURE : FALSE
6925 static BOOL LISTVIEW_GetItemExtT(const LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem, BOOL isW)
6927 LPWSTR pszText;
6928 BOOL bResult;
6930 if (!lpLVItem || lpLVItem->iItem < 0 || lpLVItem->iItem >= infoPtr->nItemCount)
6931 return FALSE;
6933 pszText = lpLVItem->pszText;
6934 bResult = LISTVIEW_GetItemT(infoPtr, lpLVItem, isW);
6935 if (bResult && (lpLVItem->mask & LVIF_TEXT) && lpLVItem->pszText != pszText)
6937 if (lpLVItem->pszText != LPSTR_TEXTCALLBACKW)
6938 textcpynT(pszText, isW, lpLVItem->pszText, isW, lpLVItem->cchTextMax);
6939 else
6940 pszText = LPSTR_TEXTCALLBACKW;
6942 lpLVItem->pszText = pszText;
6944 return bResult;
6948 /***
6949 * DESCRIPTION:
6950 * Retrieves the position (upper-left) of the listview control item.
6951 * Note that for LVS_ICON style, the upper-left is that of the icon
6952 * and not the bounding box.
6954 * PARAMETER(S):
6955 * [I] infoPtr : valid pointer to the listview structure
6956 * [I] nItem : item index
6957 * [O] lpptPosition : coordinate information
6959 * RETURN:
6960 * SUCCESS : TRUE
6961 * FAILURE : FALSE
6963 static BOOL LISTVIEW_GetItemPosition(const LISTVIEW_INFO *infoPtr, INT nItem, LPPOINT lpptPosition)
6965 POINT Origin;
6967 TRACE("(nItem=%d, lpptPosition=%p)\n", nItem, lpptPosition);
6969 if (!lpptPosition || nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
6971 LISTVIEW_GetOrigin(infoPtr, &Origin);
6972 LISTVIEW_GetItemOrigin(infoPtr, nItem, lpptPosition);
6974 if (infoPtr->uView == LV_VIEW_ICON)
6976 lpptPosition->x += (infoPtr->nItemWidth - infoPtr->iconSize.cx) / 2;
6977 lpptPosition->y += ICON_TOP_PADDING;
6979 lpptPosition->x += Origin.x;
6980 lpptPosition->y += Origin.y;
6982 TRACE (" lpptPosition=%s\n", wine_dbgstr_point(lpptPosition));
6983 return TRUE;
6987 /***
6988 * DESCRIPTION:
6989 * Retrieves the bounding rectangle for a listview control item.
6991 * PARAMETER(S):
6992 * [I] infoPtr : valid pointer to the listview structure
6993 * [I] nItem : item index
6994 * [IO] lprc : bounding rectangle coordinates
6995 * lprc->left specifies the portion of the item for which the bounding
6996 * rectangle will be retrieved.
6998 * LVIR_BOUNDS Returns the bounding rectangle of the entire item,
6999 * including the icon and label.
7001 * * For LVS_ICON
7002 * * Experiment shows that native control returns:
7003 * * width = min (48, length of text line)
7004 * * .left = position.x - (width - iconsize.cx)/2
7005 * * .right = .left + width
7006 * * height = #lines of text * ntmHeight + icon height + 8
7007 * * .top = position.y - 2
7008 * * .bottom = .top + height
7009 * * separation between items .y = itemSpacing.cy - height
7010 * * .x = itemSpacing.cx - width
7011 * LVIR_ICON Returns the bounding rectangle of the icon or small icon.
7013 * * For LVS_ICON
7014 * * Experiment shows that native control returns:
7015 * * width = iconSize.cx + 16
7016 * * .left = position.x - (width - iconsize.cx)/2
7017 * * .right = .left + width
7018 * * height = iconSize.cy + 4
7019 * * .top = position.y - 2
7020 * * .bottom = .top + height
7021 * * separation between items .y = itemSpacing.cy - height
7022 * * .x = itemSpacing.cx - width
7023 * LVIR_LABEL Returns the bounding rectangle of the item text.
7025 * * For LVS_ICON
7026 * * Experiment shows that native control returns:
7027 * * width = text length
7028 * * .left = position.x - width/2
7029 * * .right = .left + width
7030 * * height = ntmH * linecount + 2
7031 * * .top = position.y + iconSize.cy + 6
7032 * * .bottom = .top + height
7033 * * separation between items .y = itemSpacing.cy - height
7034 * * .x = itemSpacing.cx - width
7035 * LVIR_SELECTBOUNDS Returns the union of the LVIR_ICON and LVIR_LABEL
7036 * rectangles, but excludes columns in report view.
7038 * RETURN:
7039 * SUCCESS : TRUE
7040 * FAILURE : FALSE
7042 * NOTES
7043 * Note that the bounding rectangle of the label in the LVS_ICON view depends
7044 * upon whether the window has the focus currently and on whether the item
7045 * is the one with the focus. Ensure that the control's record of which
7046 * item has the focus agrees with the items' records.
7048 static BOOL LISTVIEW_GetItemRect(const LISTVIEW_INFO *infoPtr, INT nItem, LPRECT lprc)
7050 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
7051 BOOL doLabel = TRUE, oversizedBox = FALSE;
7052 POINT Position, Origin;
7053 LVITEMW lvItem;
7054 LONG mode;
7056 TRACE("(hwnd=%p, nItem=%d, lprc=%p)\n", infoPtr->hwndSelf, nItem, lprc);
7058 if (!lprc || nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
7060 LISTVIEW_GetOrigin(infoPtr, &Origin);
7061 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
7063 /* Be smart and try to figure out the minimum we have to do */
7064 if (lprc->left == LVIR_ICON) doLabel = FALSE;
7065 if (infoPtr->uView == LV_VIEW_DETAILS && lprc->left == LVIR_BOUNDS) doLabel = FALSE;
7066 if (infoPtr->uView == LV_VIEW_ICON && lprc->left != LVIR_ICON &&
7067 infoPtr->bFocus && LISTVIEW_GetItemState(infoPtr, nItem, LVIS_FOCUSED))
7068 oversizedBox = TRUE;
7070 /* get what we need from the item before hand, so we make
7071 * only one request. This can speed up things, if data
7072 * is stored on the app side */
7073 lvItem.mask = 0;
7074 if (infoPtr->uView == LV_VIEW_DETAILS) lvItem.mask |= LVIF_INDENT;
7075 if (doLabel) lvItem.mask |= LVIF_TEXT;
7076 lvItem.iItem = nItem;
7077 lvItem.iSubItem = 0;
7078 lvItem.pszText = szDispText;
7079 lvItem.cchTextMax = DISP_TEXT_SIZE;
7080 if (lvItem.mask && !LISTVIEW_GetItemW(infoPtr, &lvItem)) return FALSE;
7081 /* we got the state already up, simulate it here, to avoid a reget */
7082 if (infoPtr->uView == LV_VIEW_ICON && (lprc->left != LVIR_ICON))
7084 lvItem.mask |= LVIF_STATE;
7085 lvItem.stateMask = LVIS_FOCUSED;
7086 lvItem.state = (oversizedBox ? LVIS_FOCUSED : 0);
7089 if (infoPtr->uView == LV_VIEW_DETAILS && (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) && lprc->left == LVIR_SELECTBOUNDS)
7090 lprc->left = LVIR_BOUNDS;
7092 mode = lprc->left;
7093 switch(lprc->left)
7095 case LVIR_ICON:
7096 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, NULL, lprc, NULL, NULL);
7097 break;
7099 case LVIR_LABEL:
7100 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, NULL, NULL, NULL, lprc);
7101 break;
7103 case LVIR_BOUNDS:
7104 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, lprc, NULL, NULL, NULL, NULL);
7105 break;
7107 case LVIR_SELECTBOUNDS:
7108 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, lprc, NULL, NULL, NULL);
7109 break;
7111 default:
7112 WARN("Unknown value: %d\n", lprc->left);
7113 return FALSE;
7116 if (infoPtr->uView == LV_VIEW_DETAILS)
7118 if (mode != LVIR_BOUNDS)
7119 OffsetRect(lprc, Origin.x + LISTVIEW_GetColumnInfo(infoPtr, 0)->rcHeader.left,
7120 Position.y + Origin.y);
7121 else
7122 OffsetRect(lprc, Origin.x, Position.y + Origin.y);
7124 else
7125 OffsetRect(lprc, Position.x + Origin.x, Position.y + Origin.y);
7127 TRACE(" rect=%s\n", wine_dbgstr_rect(lprc));
7129 return TRUE;
7132 /***
7133 * DESCRIPTION:
7134 * Retrieves the spacing between listview control items.
7136 * PARAMETER(S):
7137 * [I] infoPtr : valid pointer to the listview structure
7138 * [IO] lprc : rectangle to receive the output
7139 * on input, lprc->top = nSubItem
7140 * lprc->left = LVIR_ICON | LVIR_BOUNDS | LVIR_LABEL
7142 * NOTE: for subItem = 0, we should return the bounds of the _entire_ item,
7143 * not only those of the first column.
7145 * RETURN:
7146 * TRUE: success
7147 * FALSE: failure
7149 static BOOL LISTVIEW_GetSubItemRect(const LISTVIEW_INFO *infoPtr, INT item, LPRECT lprc)
7151 RECT rect = { 0, 0, 0, 0 };
7152 POINT origin;
7153 INT y;
7155 if (!lprc) return FALSE;
7157 TRACE("(item=%d, subitem=%d, type=%d)\n", item, lprc->top, lprc->left);
7158 /* Subitem of '0' means item itself, and this works for all control view modes */
7159 if (lprc->top == 0)
7160 return LISTVIEW_GetItemRect(infoPtr, item, lprc);
7162 if (infoPtr->uView != LV_VIEW_DETAILS) return FALSE;
7164 LISTVIEW_GetOrigin(infoPtr, &origin);
7165 /* this works for any item index, no matter if it exists or not */
7166 y = item * infoPtr->nItemHeight + origin.y;
7168 if (infoPtr->hwndHeader && SendMessageW(infoPtr->hwndHeader, HDM_GETITEMRECT, lprc->top, (LPARAM)&rect))
7170 rect.top = 0;
7171 rect.bottom = infoPtr->nItemHeight;
7173 else
7175 /* Native implementation is broken for this case and garbage is left for left and right fields,
7176 we zero them to get predictable output */
7177 lprc->left = lprc->right = lprc->top = 0;
7178 lprc->bottom = infoPtr->nItemHeight;
7179 OffsetRect(lprc, origin.x, y);
7180 TRACE("return rect %s\n", wine_dbgstr_rect(lprc));
7181 return TRUE;
7184 switch (lprc->left)
7186 case LVIR_ICON:
7188 /* it doesn't matter if main item actually has an icon, if imagelist is set icon width is returned */
7189 if (infoPtr->himlSmall)
7190 rect.right = rect.left + infoPtr->iconSize.cx;
7191 else
7192 rect.right = rect.left;
7194 rect.bottom = rect.top + infoPtr->iconSize.cy;
7195 break;
7197 case LVIR_LABEL:
7198 case LVIR_BOUNDS:
7199 break;
7201 default:
7202 ERR("Unknown bounds=%d\n", lprc->left);
7203 return FALSE;
7206 OffsetRect(&rect, origin.x, y);
7207 *lprc = rect;
7208 TRACE("return rect %s\n", wine_dbgstr_rect(lprc));
7210 return TRUE;
7213 /***
7214 * DESCRIPTION:
7215 * Retrieves the spacing between listview control items.
7217 * PARAMETER(S):
7218 * [I] infoPtr : valid pointer to the listview structure
7219 * [I] bSmall : flag for small or large icon
7221 * RETURN:
7222 * Horizontal + vertical spacing
7224 static LONG LISTVIEW_GetItemSpacing(const LISTVIEW_INFO *infoPtr, BOOL bSmall)
7226 LONG lResult;
7228 if (!bSmall)
7230 lResult = MAKELONG(infoPtr->iconSpacing.cx, infoPtr->iconSpacing.cy);
7232 else
7234 if (infoPtr->uView == LV_VIEW_ICON)
7235 lResult = MAKELONG(DEFAULT_COLUMN_WIDTH, GetSystemMetrics(SM_CXSMICON)+HEIGHT_PADDING);
7236 else
7237 lResult = MAKELONG(infoPtr->nItemWidth, infoPtr->nItemHeight);
7239 return lResult;
7242 /***
7243 * DESCRIPTION:
7244 * Retrieves the state of a listview control item.
7246 * PARAMETER(S):
7247 * [I] infoPtr : valid pointer to the listview structure
7248 * [I] nItem : item index
7249 * [I] uMask : state mask
7251 * RETURN:
7252 * State specified by the mask.
7254 static UINT LISTVIEW_GetItemState(const LISTVIEW_INFO *infoPtr, INT nItem, UINT uMask)
7256 LVITEMW lvItem;
7258 if (nItem < 0 || nItem >= infoPtr->nItemCount) return 0;
7260 lvItem.iItem = nItem;
7261 lvItem.iSubItem = 0;
7262 lvItem.mask = LVIF_STATE;
7263 lvItem.stateMask = uMask;
7264 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return 0;
7266 return lvItem.state & uMask;
7269 /***
7270 * DESCRIPTION:
7271 * Retrieves the text of a listview control item or subitem.
7273 * PARAMETER(S):
7274 * [I] hwnd : window handle
7275 * [I] nItem : item index
7276 * [IO] lpLVItem : item information
7277 * [I] isW : TRUE if lpLVItem is Unicode
7279 * RETURN:
7280 * SUCCESS : string length
7281 * FAILURE : 0
7283 static INT LISTVIEW_GetItemTextT(const LISTVIEW_INFO *infoPtr, INT nItem, LPLVITEMW lpLVItem, BOOL isW)
7285 if (!lpLVItem || nItem < 0 || nItem >= infoPtr->nItemCount) return 0;
7287 lpLVItem->mask = LVIF_TEXT;
7288 lpLVItem->iItem = nItem;
7289 if (!LISTVIEW_GetItemExtT(infoPtr, lpLVItem, isW)) return 0;
7291 return textlenT(lpLVItem->pszText, isW);
7294 /***
7295 * DESCRIPTION:
7296 * Searches for an item based on properties + relationships.
7298 * PARAMETER(S):
7299 * [I] infoPtr : valid pointer to the listview structure
7300 * [I] nItem : item index
7301 * [I] uFlags : relationship flag
7303 * RETURN:
7304 * SUCCESS : item index
7305 * FAILURE : -1
7307 static INT LISTVIEW_GetNextItem(const LISTVIEW_INFO *infoPtr, INT nItem, UINT uFlags)
7309 UINT uMask = 0;
7310 LVFINDINFOW lvFindInfo;
7311 INT nCountPerColumn;
7312 INT nCountPerRow;
7313 INT i;
7315 TRACE("nItem=%d, uFlags=%x, nItemCount=%d\n", nItem, uFlags, infoPtr->nItemCount);
7316 if (nItem < -1 || nItem >= infoPtr->nItemCount) return -1;
7318 ZeroMemory(&lvFindInfo, sizeof(lvFindInfo));
7320 if (uFlags & LVNI_CUT)
7321 uMask |= LVIS_CUT;
7323 if (uFlags & LVNI_DROPHILITED)
7324 uMask |= LVIS_DROPHILITED;
7326 if (uFlags & LVNI_FOCUSED)
7327 uMask |= LVIS_FOCUSED;
7329 if (uFlags & LVNI_SELECTED)
7330 uMask |= LVIS_SELECTED;
7332 /* if we're asked for the focused item, that's only one,
7333 * so it's worth optimizing */
7334 if (uFlags & LVNI_FOCUSED)
7336 if ((LISTVIEW_GetItemState(infoPtr, infoPtr->nFocusedItem, uMask) & uMask) != uMask) return -1;
7337 return (infoPtr->nFocusedItem == nItem) ? -1 : infoPtr->nFocusedItem;
7340 if (uFlags & LVNI_ABOVE)
7342 if ((infoPtr->uView == LV_VIEW_LIST) || (infoPtr->uView == LV_VIEW_DETAILS))
7344 while (nItem >= 0)
7346 nItem--;
7347 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7348 return nItem;
7351 else
7353 /* Special case for autoarrange - move 'til the top of a list */
7354 if (is_autoarrange(infoPtr))
7356 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
7357 while (nItem - nCountPerRow >= 0)
7359 nItem -= nCountPerRow;
7360 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7361 return nItem;
7363 return -1;
7365 lvFindInfo.flags = LVFI_NEARESTXY;
7366 lvFindInfo.vkDirection = VK_UP;
7367 LISTVIEW_GetItemPosition(infoPtr, nItem, &lvFindInfo.pt);
7368 while ((nItem = LISTVIEW_FindItemW(infoPtr, nItem, &lvFindInfo)) != -1)
7370 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7371 return nItem;
7375 else if (uFlags & LVNI_BELOW)
7377 if ((infoPtr->uView == LV_VIEW_LIST) || (infoPtr->uView == LV_VIEW_DETAILS))
7379 while (nItem < infoPtr->nItemCount)
7381 nItem++;
7382 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7383 return nItem;
7386 else
7388 /* Special case for autoarrange - move 'til the bottom of a list */
7389 if (is_autoarrange(infoPtr))
7391 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
7392 while (nItem + nCountPerRow < infoPtr->nItemCount )
7394 nItem += nCountPerRow;
7395 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7396 return nItem;
7398 return -1;
7400 lvFindInfo.flags = LVFI_NEARESTXY;
7401 lvFindInfo.vkDirection = VK_DOWN;
7402 LISTVIEW_GetItemPosition(infoPtr, nItem, &lvFindInfo.pt);
7403 while ((nItem = LISTVIEW_FindItemW(infoPtr, nItem, &lvFindInfo)) != -1)
7405 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7406 return nItem;
7410 else if (uFlags & LVNI_TOLEFT)
7412 if (infoPtr->uView == LV_VIEW_LIST)
7414 nCountPerColumn = LISTVIEW_GetCountPerColumn(infoPtr);
7415 while (nItem - nCountPerColumn >= 0)
7417 nItem -= nCountPerColumn;
7418 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7419 return nItem;
7422 else if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
7424 /* Special case for autoarrange - move 'til the beginning of a row */
7425 if (is_autoarrange(infoPtr))
7427 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
7428 while (nItem % nCountPerRow > 0)
7430 nItem --;
7431 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7432 return nItem;
7434 return -1;
7436 lvFindInfo.flags = LVFI_NEARESTXY;
7437 lvFindInfo.vkDirection = VK_LEFT;
7438 LISTVIEW_GetItemPosition(infoPtr, nItem, &lvFindInfo.pt);
7439 while ((nItem = LISTVIEW_FindItemW(infoPtr, nItem, &lvFindInfo)) != -1)
7441 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7442 return nItem;
7446 else if (uFlags & LVNI_TORIGHT)
7448 if (infoPtr->uView == LV_VIEW_LIST)
7450 nCountPerColumn = LISTVIEW_GetCountPerColumn(infoPtr);
7451 while (nItem + nCountPerColumn < infoPtr->nItemCount)
7453 nItem += nCountPerColumn;
7454 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7455 return nItem;
7458 else if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
7460 /* Special case for autoarrange - move 'til the end of a row */
7461 if (is_autoarrange(infoPtr))
7463 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
7464 while (nItem % nCountPerRow < nCountPerRow - 1 )
7466 nItem ++;
7467 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7468 return nItem;
7470 return -1;
7472 lvFindInfo.flags = LVFI_NEARESTXY;
7473 lvFindInfo.vkDirection = VK_RIGHT;
7474 LISTVIEW_GetItemPosition(infoPtr, nItem, &lvFindInfo.pt);
7475 while ((nItem = LISTVIEW_FindItemW(infoPtr, nItem, &lvFindInfo)) != -1)
7477 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7478 return nItem;
7482 else
7484 nItem++;
7486 /* search by index */
7487 for (i = nItem; i < infoPtr->nItemCount; i++)
7489 if ((LISTVIEW_GetItemState(infoPtr, i, uMask) & uMask) == uMask)
7490 return i;
7494 return -1;
7497 /* LISTVIEW_GetNumberOfWorkAreas */
7499 /***
7500 * DESCRIPTION:
7501 * Retrieves the origin coordinates when in icon or small icon display mode.
7503 * PARAMETER(S):
7504 * [I] infoPtr : valid pointer to the listview structure
7505 * [O] lpptOrigin : coordinate information
7507 * RETURN:
7508 * None.
7510 static void LISTVIEW_GetOrigin(const LISTVIEW_INFO *infoPtr, LPPOINT lpptOrigin)
7512 INT nHorzPos = 0, nVertPos = 0;
7513 SCROLLINFO scrollInfo;
7515 scrollInfo.cbSize = sizeof(SCROLLINFO);
7516 scrollInfo.fMask = SIF_POS;
7518 if (GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo))
7519 nHorzPos = scrollInfo.nPos;
7520 if (GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
7521 nVertPos = scrollInfo.nPos;
7523 TRACE("nHorzPos=%d, nVertPos=%d\n", nHorzPos, nVertPos);
7525 lpptOrigin->x = infoPtr->rcList.left;
7526 lpptOrigin->y = infoPtr->rcList.top;
7527 if (infoPtr->uView == LV_VIEW_LIST)
7528 nHorzPos *= infoPtr->nItemWidth;
7529 else if (infoPtr->uView == LV_VIEW_DETAILS)
7530 nVertPos *= infoPtr->nItemHeight;
7532 lpptOrigin->x -= nHorzPos;
7533 lpptOrigin->y -= nVertPos;
7535 TRACE(" origin=%s\n", wine_dbgstr_point(lpptOrigin));
7538 /***
7539 * DESCRIPTION:
7540 * Retrieves the width of a string.
7542 * PARAMETER(S):
7543 * [I] hwnd : window handle
7544 * [I] lpszText : text string to process
7545 * [I] isW : TRUE if lpszText is Unicode, FALSE otherwise
7547 * RETURN:
7548 * SUCCESS : string width (in pixels)
7549 * FAILURE : zero
7551 static INT LISTVIEW_GetStringWidthT(const LISTVIEW_INFO *infoPtr, LPCWSTR lpszText, BOOL isW)
7553 SIZE stringSize;
7555 stringSize.cx = 0;
7556 if (is_text(lpszText))
7558 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
7559 HDC hdc = GetDC(infoPtr->hwndSelf);
7560 HFONT hOldFont = SelectObject(hdc, hFont);
7562 if (isW)
7563 GetTextExtentPointW(hdc, lpszText, lstrlenW(lpszText), &stringSize);
7564 else
7565 GetTextExtentPointA(hdc, (LPCSTR)lpszText, lstrlenA((LPCSTR)lpszText), &stringSize);
7566 SelectObject(hdc, hOldFont);
7567 ReleaseDC(infoPtr->hwndSelf, hdc);
7569 return stringSize.cx;
7572 /***
7573 * DESCRIPTION:
7574 * Determines which listview item is located at the specified position.
7576 * PARAMETER(S):
7577 * [I] infoPtr : valid pointer to the listview structure
7578 * [IO] lpht : hit test information
7579 * [I] subitem : fill out iSubItem.
7580 * [I] select : return the index only if the hit selects the item
7582 * NOTE:
7583 * (mm 20001022): We must not allow iSubItem to be touched, for
7584 * an app might pass only a structure with space up to iItem!
7585 * (MS Office 97 does that for instance in the file open dialog)
7587 * RETURN:
7588 * SUCCESS : item index
7589 * FAILURE : -1
7591 static INT LISTVIEW_HitTest(const LISTVIEW_INFO *infoPtr, LPLVHITTESTINFO lpht, BOOL subitem, BOOL select)
7593 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
7594 RECT rcBox, rcBounds, rcState, rcIcon, rcLabel, rcSearch;
7595 POINT Origin, Position, opt;
7596 BOOL is_fullrow;
7597 LVITEMW lvItem;
7598 ITERATOR i;
7599 INT iItem;
7601 TRACE("(pt=%s, subitem=%d, select=%d)\n", wine_dbgstr_point(&lpht->pt), subitem, select);
7603 lpht->flags = 0;
7604 lpht->iItem = -1;
7605 if (subitem) lpht->iSubItem = 0;
7607 LISTVIEW_GetOrigin(infoPtr, &Origin);
7609 /* set whole list relation flags */
7610 if (subitem && infoPtr->uView == LV_VIEW_DETAILS)
7612 /* LVM_SUBITEMHITTEST checks left bound of possible client area */
7613 if (infoPtr->rcList.left > lpht->pt.x && Origin.x < lpht->pt.x)
7614 lpht->flags |= LVHT_TOLEFT;
7616 if (lpht->pt.y < infoPtr->rcList.top && lpht->pt.y >= 0)
7617 opt.y = lpht->pt.y + infoPtr->rcList.top;
7618 else
7619 opt.y = lpht->pt.y;
7621 if (infoPtr->rcList.bottom < opt.y)
7622 lpht->flags |= LVHT_BELOW;
7624 else
7626 if (infoPtr->rcList.left > lpht->pt.x)
7627 lpht->flags |= LVHT_TOLEFT;
7628 else if (infoPtr->rcList.right < lpht->pt.x)
7629 lpht->flags |= LVHT_TORIGHT;
7631 if (infoPtr->rcList.top > lpht->pt.y)
7632 lpht->flags |= LVHT_ABOVE;
7633 else if (infoPtr->rcList.bottom < lpht->pt.y)
7634 lpht->flags |= LVHT_BELOW;
7637 /* even if item is invalid try to find subitem */
7638 if (infoPtr->uView == LV_VIEW_DETAILS && subitem)
7640 RECT *pRect;
7641 INT j;
7643 opt.x = lpht->pt.x - Origin.x;
7645 lpht->iSubItem = -1;
7646 for (j = 0; j < DPA_GetPtrCount(infoPtr->hdpaColumns); j++)
7648 pRect = &LISTVIEW_GetColumnInfo(infoPtr, j)->rcHeader;
7650 if ((opt.x >= pRect->left) && (opt.x < pRect->right))
7652 lpht->iSubItem = j;
7653 break;
7656 TRACE("lpht->iSubItem=%d\n", lpht->iSubItem);
7658 /* if we're outside horizontal columns bounds there's nothing to test further */
7659 if (lpht->iSubItem == -1)
7661 lpht->iItem = -1;
7662 lpht->flags = LVHT_NOWHERE;
7663 return -1;
7667 TRACE("lpht->flags=0x%x\n", lpht->flags);
7668 if (lpht->flags) return -1;
7670 lpht->flags |= LVHT_NOWHERE;
7672 /* first deal with the large items */
7673 rcSearch.left = lpht->pt.x;
7674 rcSearch.top = lpht->pt.y;
7675 rcSearch.right = rcSearch.left + 1;
7676 rcSearch.bottom = rcSearch.top + 1;
7678 iterator_frameditems(&i, infoPtr, &rcSearch);
7679 iterator_next(&i); /* go to first item in the sequence */
7680 iItem = i.nItem;
7681 iterator_destroy(&i);
7683 TRACE("lpht->iItem=%d\n", iItem);
7684 if (iItem == -1) return -1;
7686 lvItem.mask = LVIF_STATE | LVIF_TEXT;
7687 if (infoPtr->uView == LV_VIEW_DETAILS) lvItem.mask |= LVIF_INDENT;
7688 lvItem.stateMask = LVIS_STATEIMAGEMASK;
7689 if (infoPtr->uView == LV_VIEW_ICON) lvItem.stateMask |= LVIS_FOCUSED;
7690 lvItem.iItem = iItem;
7691 lvItem.iSubItem = subitem ? lpht->iSubItem : 0;
7692 lvItem.pszText = szDispText;
7693 lvItem.cchTextMax = DISP_TEXT_SIZE;
7694 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return -1;
7695 if (!infoPtr->bFocus) lvItem.state &= ~LVIS_FOCUSED;
7697 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, &rcBox, NULL, &rcIcon, &rcState, &rcLabel);
7698 LISTVIEW_GetItemOrigin(infoPtr, iItem, &Position);
7699 opt.x = lpht->pt.x - Position.x - Origin.x;
7701 if (lpht->pt.y < infoPtr->rcList.top && lpht->pt.y >= 0)
7702 opt.y = lpht->pt.y - Position.y - Origin.y + infoPtr->rcList.top;
7703 else
7704 opt.y = lpht->pt.y - Position.y - Origin.y;
7706 if (infoPtr->uView == LV_VIEW_DETAILS)
7708 rcBounds = rcBox;
7709 if (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT)
7710 opt.x = lpht->pt.x - Origin.x;
7712 else
7714 UnionRect(&rcBounds, &rcIcon, &rcLabel);
7715 UnionRect(&rcBounds, &rcBounds, &rcState);
7717 TRACE("rcBounds=%s\n", wine_dbgstr_rect(&rcBounds));
7718 if (!PtInRect(&rcBounds, opt)) return -1;
7720 /* That's a special case - row rectangle is used as item rectangle and
7721 returned flags contain all item parts. */
7722 is_fullrow = (infoPtr->uView == LV_VIEW_DETAILS) && ((infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) || (infoPtr->dwStyle & LVS_OWNERDRAWFIXED));
7724 if (PtInRect(&rcIcon, opt))
7725 lpht->flags |= LVHT_ONITEMICON;
7726 else if (PtInRect(&rcLabel, opt))
7727 lpht->flags |= LVHT_ONITEMLABEL;
7728 else if (infoPtr->himlState && PtInRect(&rcState, opt))
7729 lpht->flags |= LVHT_ONITEMSTATEICON;
7730 if (is_fullrow && !(lpht->flags & LVHT_ONITEM))
7732 lpht->flags = LVHT_ONITEM | LVHT_ABOVE;
7734 if (lpht->flags & LVHT_ONITEM)
7735 lpht->flags &= ~LVHT_NOWHERE;
7736 TRACE("lpht->flags=0x%x\n", lpht->flags);
7738 if (select && !is_fullrow)
7740 if (infoPtr->uView == LV_VIEW_DETAILS)
7742 /* get main item bounds */
7743 lvItem.iSubItem = 0;
7744 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, &rcBox, NULL, &rcIcon, &rcState, &rcLabel);
7745 UnionRect(&rcBounds, &rcIcon, &rcLabel);
7746 UnionRect(&rcBounds, &rcBounds, &rcState);
7748 if (!PtInRect(&rcBounds, opt)) iItem = -1;
7750 return lpht->iItem = iItem;
7753 /***
7754 * DESCRIPTION:
7755 * Inserts a new item in the listview control.
7757 * PARAMETER(S):
7758 * [I] infoPtr : valid pointer to the listview structure
7759 * [I] lpLVItem : item information
7760 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
7762 * RETURN:
7763 * SUCCESS : new item index
7764 * FAILURE : -1
7766 static INT LISTVIEW_InsertItemT(LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem, BOOL isW)
7768 INT nItem;
7769 HDPA hdpaSubItems;
7770 NMLISTVIEW nmlv;
7771 ITEM_INFO *lpItem;
7772 ITEM_ID *lpID;
7773 BOOL is_sorted, has_changed;
7774 LVITEMW item;
7775 HWND hwndSelf = infoPtr->hwndSelf;
7777 TRACE("(item=%s, isW=%d)\n", debuglvitem_t(lpLVItem, isW), isW);
7779 if (infoPtr->dwStyle & LVS_OWNERDATA) return infoPtr->nItemCount++;
7781 /* make sure it's an item, and not a subitem; cannot insert a subitem */
7782 if (!lpLVItem || lpLVItem->iSubItem) return -1;
7784 if (!is_assignable_item(lpLVItem, infoPtr->dwStyle)) return -1;
7786 if (!(lpItem = Alloc(sizeof(ITEM_INFO)))) return -1;
7788 /* insert item in listview control data structure */
7789 if ( !(hdpaSubItems = DPA_Create(8)) ) goto fail;
7790 if ( !DPA_SetPtr(hdpaSubItems, 0, lpItem) ) assert (FALSE);
7792 /* link with id struct */
7793 if (!(lpID = Alloc(sizeof(ITEM_ID)))) goto fail;
7794 lpItem->id = lpID;
7795 lpID->item = hdpaSubItems;
7796 lpID->id = get_next_itemid(infoPtr);
7797 if ( DPA_InsertPtr(infoPtr->hdpaItemIds, infoPtr->nItemCount, lpID) == -1) goto fail;
7799 is_sorted = (infoPtr->dwStyle & (LVS_SORTASCENDING | LVS_SORTDESCENDING)) &&
7800 !(infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (LPSTR_TEXTCALLBACKW != lpLVItem->pszText);
7802 if (lpLVItem->iItem < 0 && !is_sorted) return -1;
7804 /* calculate new item index */
7805 if (is_sorted)
7807 HDPA hItem;
7808 ITEM_INFO *item_s;
7809 INT i = 0, cmpv;
7811 while (i < infoPtr->nItemCount)
7813 hItem = DPA_GetPtr( infoPtr->hdpaItems, i);
7814 item_s = DPA_GetPtr(hItem, 0);
7816 cmpv = textcmpWT(item_s->hdr.pszText, lpLVItem->pszText, isW);
7817 if (infoPtr->dwStyle & LVS_SORTDESCENDING) cmpv *= -1;
7819 if (cmpv >= 0) break;
7820 i++;
7822 nItem = i;
7824 else
7825 nItem = min(lpLVItem->iItem, infoPtr->nItemCount);
7827 TRACE("inserting at %d, sorted=%d, count=%d, iItem=%d\n", nItem, is_sorted, infoPtr->nItemCount, lpLVItem->iItem);
7828 nItem = DPA_InsertPtr( infoPtr->hdpaItems, nItem, hdpaSubItems );
7829 if (nItem == -1) goto fail;
7830 infoPtr->nItemCount++;
7832 /* shift indices first so they don't get tangled */
7833 LISTVIEW_ShiftIndices(infoPtr, nItem, 1);
7835 /* set the item attributes */
7836 if (lpLVItem->mask & (LVIF_GROUPID|LVIF_COLUMNS))
7838 /* full size structure expected - _WIN32IE >= 0x560 */
7839 item = *lpLVItem;
7841 else if (lpLVItem->mask & LVIF_INDENT)
7843 /* indent member expected - _WIN32IE >= 0x300 */
7844 memcpy(&item, lpLVItem, offsetof( LVITEMW, iGroupId ));
7846 else
7848 /* minimal structure expected */
7849 memcpy(&item, lpLVItem, offsetof( LVITEMW, iIndent ));
7851 item.iItem = nItem;
7852 if (infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES)
7854 if (item.mask & LVIF_STATE)
7856 item.stateMask |= LVIS_STATEIMAGEMASK;
7857 item.state &= ~LVIS_STATEIMAGEMASK;
7858 item.state |= INDEXTOSTATEIMAGEMASK(1);
7860 else
7862 item.mask |= LVIF_STATE;
7863 item.stateMask = LVIS_STATEIMAGEMASK;
7864 item.state = INDEXTOSTATEIMAGEMASK(1);
7868 if (!set_main_item(infoPtr, &item, TRUE, isW, &has_changed)) goto undo;
7870 /* make room for the position, if we are in the right mode */
7871 if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
7873 if (DPA_InsertPtr(infoPtr->hdpaPosX, nItem, 0) == -1)
7874 goto undo;
7875 if (DPA_InsertPtr(infoPtr->hdpaPosY, nItem, 0) == -1)
7877 DPA_DeletePtr(infoPtr->hdpaPosX, nItem);
7878 goto undo;
7882 /* send LVN_INSERTITEM notification */
7883 memset(&nmlv, 0, sizeof(NMLISTVIEW));
7884 nmlv.iItem = nItem;
7885 nmlv.lParam = lpItem->lParam;
7886 notify_listview(infoPtr, LVN_INSERTITEM, &nmlv);
7887 if (!IsWindow(hwndSelf))
7888 return -1;
7890 /* align items (set position of each item) */
7891 if (infoPtr->uView == LV_VIEW_SMALLICON || infoPtr->uView == LV_VIEW_ICON)
7893 POINT pt;
7895 if (infoPtr->dwStyle & LVS_ALIGNLEFT)
7896 LISTVIEW_NextIconPosLeft(infoPtr, &pt);
7897 else
7898 LISTVIEW_NextIconPosTop(infoPtr, &pt);
7900 LISTVIEW_MoveIconTo(infoPtr, nItem, &pt, TRUE);
7903 /* now is the invalidation fun */
7904 LISTVIEW_ScrollOnInsert(infoPtr, nItem, 1);
7905 return nItem;
7907 undo:
7908 LISTVIEW_ShiftIndices(infoPtr, nItem, -1);
7909 LISTVIEW_ShiftFocus(infoPtr, infoPtr->nFocusedItem, nItem, -1);
7910 DPA_DeletePtr(infoPtr->hdpaItems, nItem);
7911 infoPtr->nItemCount--;
7912 fail:
7913 DPA_DeletePtr(hdpaSubItems, 0);
7914 DPA_Destroy (hdpaSubItems);
7915 Free (lpItem);
7916 return -1;
7919 /***
7920 * DESCRIPTION:
7921 * Checks item visibility.
7923 * PARAMETER(S):
7924 * [I] infoPtr : valid pointer to the listview structure
7925 * [I] nFirst : item index to check for
7927 * RETURN:
7928 * Item visible : TRUE
7929 * Item invisible or failure : FALSE
7931 static BOOL LISTVIEW_IsItemVisible(const LISTVIEW_INFO *infoPtr, INT nItem)
7933 POINT Origin, Position;
7934 RECT rcItem;
7935 HDC hdc;
7936 BOOL ret;
7938 TRACE("nItem=%d\n", nItem);
7940 if (nItem < 0 || nItem >= DPA_GetPtrCount(infoPtr->hdpaItems)) return FALSE;
7942 LISTVIEW_GetOrigin(infoPtr, &Origin);
7943 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
7944 rcItem.left = Position.x + Origin.x;
7945 rcItem.top = Position.y + Origin.y;
7946 rcItem.right = rcItem.left + infoPtr->nItemWidth;
7947 rcItem.bottom = rcItem.top + infoPtr->nItemHeight;
7949 hdc = GetDC(infoPtr->hwndSelf);
7950 if (!hdc) return FALSE;
7951 ret = RectVisible(hdc, &rcItem);
7952 ReleaseDC(infoPtr->hwndSelf, hdc);
7954 return ret;
7957 /***
7958 * DESCRIPTION:
7959 * Redraws a range of items.
7961 * PARAMETER(S):
7962 * [I] infoPtr : valid pointer to the listview structure
7963 * [I] nFirst : first item
7964 * [I] nLast : last item
7966 * RETURN:
7967 * SUCCESS : TRUE
7968 * FAILURE : FALSE
7970 static BOOL LISTVIEW_RedrawItems(const LISTVIEW_INFO *infoPtr, INT nFirst, INT nLast)
7972 INT i;
7974 if (nLast < nFirst || min(nFirst, nLast) < 0 ||
7975 max(nFirst, nLast) >= infoPtr->nItemCount)
7976 return FALSE;
7978 for (i = nFirst; i <= nLast; i++)
7979 LISTVIEW_InvalidateItem(infoPtr, i);
7981 return TRUE;
7984 /***
7985 * DESCRIPTION:
7986 * Scroll the content of a listview.
7988 * PARAMETER(S):
7989 * [I] infoPtr : valid pointer to the listview structure
7990 * [I] dx : horizontal scroll amount in pixels
7991 * [I] dy : vertical scroll amount in pixels
7993 * RETURN:
7994 * SUCCESS : TRUE
7995 * FAILURE : FALSE
7997 * COMMENTS:
7998 * If the control is in report view (LV_VIEW_DETAILS) the control can
7999 * be scrolled only in line increments. "dy" will be rounded to the
8000 * nearest number of pixels that are a whole line. Ex: if line height
8001 * is 16 and an 8 is passed, the list will be scrolled by 16. If a 7
8002 * is passed, then the scroll will be 0. (per MSDN 7/2002)
8004 static BOOL LISTVIEW_Scroll(LISTVIEW_INFO *infoPtr, INT dx, INT dy)
8006 switch(infoPtr->uView) {
8007 case LV_VIEW_DETAILS:
8008 dy += (dy < 0 ? -1 : 1) * infoPtr->nItemHeight/2;
8009 dy /= infoPtr->nItemHeight;
8010 break;
8011 case LV_VIEW_LIST:
8012 if (dy != 0) return FALSE;
8013 break;
8014 default: /* icon */
8015 break;
8018 if (dx != 0) LISTVIEW_HScroll(infoPtr, SB_INTERNAL, dx);
8019 if (dy != 0) LISTVIEW_VScroll(infoPtr, SB_INTERNAL, dy);
8021 return TRUE;
8024 /***
8025 * DESCRIPTION:
8026 * Sets the background color.
8028 * PARAMETER(S):
8029 * [I] infoPtr : valid pointer to the listview structure
8030 * [I] color : background color
8032 * RETURN:
8033 * SUCCESS : TRUE
8034 * FAILURE : FALSE
8036 static BOOL LISTVIEW_SetBkColor(LISTVIEW_INFO *infoPtr, COLORREF color)
8038 TRACE("(color=%x)\n", color);
8040 if(infoPtr->clrBk != color) {
8041 if (infoPtr->clrBk != CLR_NONE) DeleteObject(infoPtr->hBkBrush);
8042 infoPtr->clrBk = color;
8043 if (color == CLR_NONE)
8044 infoPtr->hBkBrush = (HBRUSH)GetClassLongPtrW(infoPtr->hwndSelf, GCLP_HBRBACKGROUND);
8045 else
8047 infoPtr->hBkBrush = CreateSolidBrush(color);
8048 infoPtr->dwLvExStyle &= ~LVS_EX_TRANSPARENTBKGND;
8052 return TRUE;
8055 /* LISTVIEW_SetBkImage */
8057 /*** Helper for {Insert,Set}ColumnT *only* */
8058 static void column_fill_hditem(const LISTVIEW_INFO *infoPtr, HDITEMW *lphdi, INT nColumn,
8059 const LVCOLUMNW *lpColumn, BOOL isW)
8061 if (lpColumn->mask & LVCF_FMT)
8063 /* format member is valid */
8064 lphdi->mask |= HDI_FORMAT;
8066 /* set text alignment (leftmost column must be left-aligned) */
8067 if (nColumn == 0 || (lpColumn->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_LEFT)
8068 lphdi->fmt |= HDF_LEFT;
8069 else if ((lpColumn->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_RIGHT)
8070 lphdi->fmt |= HDF_RIGHT;
8071 else if ((lpColumn->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_CENTER)
8072 lphdi->fmt |= HDF_CENTER;
8074 if (lpColumn->fmt & LVCFMT_BITMAP_ON_RIGHT)
8075 lphdi->fmt |= HDF_BITMAP_ON_RIGHT;
8077 if (lpColumn->fmt & LVCFMT_COL_HAS_IMAGES)
8079 lphdi->fmt |= HDF_IMAGE;
8080 lphdi->iImage = I_IMAGECALLBACK;
8083 if (lpColumn->fmt & LVCFMT_FIXED_WIDTH)
8084 lphdi->fmt |= HDF_FIXEDWIDTH;
8087 if (lpColumn->mask & LVCF_WIDTH)
8089 lphdi->mask |= HDI_WIDTH;
8090 if(lpColumn->cx == LVSCW_AUTOSIZE_USEHEADER)
8092 /* make it fill the remainder of the controls width */
8093 RECT rcHeader;
8094 INT item_index;
8096 for(item_index = 0; item_index < (nColumn - 1); item_index++)
8098 LISTVIEW_GetHeaderRect(infoPtr, item_index, &rcHeader);
8099 lphdi->cxy += rcHeader.right - rcHeader.left;
8102 /* retrieve the layout of the header */
8103 GetClientRect(infoPtr->hwndSelf, &rcHeader);
8104 TRACE("start cxy=%d rcHeader=%s\n", lphdi->cxy, wine_dbgstr_rect(&rcHeader));
8106 lphdi->cxy = (rcHeader.right - rcHeader.left) - lphdi->cxy;
8108 else
8109 lphdi->cxy = lpColumn->cx;
8112 if (lpColumn->mask & LVCF_TEXT)
8114 lphdi->mask |= HDI_TEXT | HDI_FORMAT;
8115 lphdi->fmt |= HDF_STRING;
8116 lphdi->pszText = lpColumn->pszText;
8117 lphdi->cchTextMax = textlenT(lpColumn->pszText, isW);
8120 if (lpColumn->mask & LVCF_IMAGE)
8122 lphdi->mask |= HDI_IMAGE;
8123 lphdi->iImage = lpColumn->iImage;
8126 if (lpColumn->mask & LVCF_ORDER)
8128 lphdi->mask |= HDI_ORDER;
8129 lphdi->iOrder = lpColumn->iOrder;
8134 /***
8135 * DESCRIPTION:
8136 * Inserts a new column.
8138 * PARAMETER(S):
8139 * [I] infoPtr : valid pointer to the listview structure
8140 * [I] nColumn : column index
8141 * [I] lpColumn : column information
8142 * [I] isW : TRUE if lpColumn is Unicode, FALSE otherwise
8144 * RETURN:
8145 * SUCCESS : new column index
8146 * FAILURE : -1
8148 static INT LISTVIEW_InsertColumnT(LISTVIEW_INFO *infoPtr, INT nColumn,
8149 const LVCOLUMNW *lpColumn, BOOL isW)
8151 COLUMN_INFO *lpColumnInfo;
8152 INT nNewColumn;
8153 HDITEMW hdi;
8155 TRACE("(nColumn=%d, lpColumn=%s, isW=%d)\n", nColumn, debuglvcolumn_t(lpColumn, isW), isW);
8157 if (!lpColumn || nColumn < 0) return -1;
8158 nColumn = min(nColumn, DPA_GetPtrCount(infoPtr->hdpaColumns));
8160 ZeroMemory(&hdi, sizeof(HDITEMW));
8161 column_fill_hditem(infoPtr, &hdi, nColumn, lpColumn, isW);
8164 * A mask not including LVCF_WIDTH turns into a mask of width, width 10
8165 * (can be seen in SPY) otherwise column never gets added.
8167 if (!(lpColumn->mask & LVCF_WIDTH)) {
8168 hdi.mask |= HDI_WIDTH;
8169 hdi.cxy = 10;
8173 * when the iSubItem is available Windows copies it to the header lParam. It seems
8174 * to happen only in LVM_INSERTCOLUMN - not in LVM_SETCOLUMN
8176 if (lpColumn->mask & LVCF_SUBITEM)
8178 hdi.mask |= HDI_LPARAM;
8179 hdi.lParam = lpColumn->iSubItem;
8182 /* create header if not present */
8183 LISTVIEW_CreateHeader(infoPtr);
8184 if (!(LVS_NOCOLUMNHEADER & infoPtr->dwStyle) &&
8185 (infoPtr->uView == LV_VIEW_DETAILS) && (WS_VISIBLE & infoPtr->dwStyle))
8187 ShowWindow(infoPtr->hwndHeader, SW_SHOWNORMAL);
8190 /* insert item in header control */
8191 nNewColumn = SendMessageW(infoPtr->hwndHeader,
8192 isW ? HDM_INSERTITEMW : HDM_INSERTITEMA,
8193 nColumn, (LPARAM)&hdi);
8194 if (nNewColumn == -1) return -1;
8195 if (nNewColumn != nColumn) ERR("nColumn=%d, nNewColumn=%d\n", nColumn, nNewColumn);
8197 /* create our own column info */
8198 if (!(lpColumnInfo = Alloc(sizeof(COLUMN_INFO)))) goto fail;
8199 if (DPA_InsertPtr(infoPtr->hdpaColumns, nNewColumn, lpColumnInfo) == -1) goto fail;
8201 if (lpColumn->mask & LVCF_FMT) lpColumnInfo->fmt = lpColumn->fmt;
8202 if (lpColumn->mask & LVCF_MINWIDTH) lpColumnInfo->cxMin = lpColumn->cxMin;
8203 if (!SendMessageW(infoPtr->hwndHeader, HDM_GETITEMRECT, nNewColumn, (LPARAM)&lpColumnInfo->rcHeader))
8204 goto fail;
8206 /* now we have to actually adjust the data */
8207 if (!(infoPtr->dwStyle & LVS_OWNERDATA) && infoPtr->nItemCount > 0)
8209 SUBITEM_INFO *lpSubItem;
8210 HDPA hdpaSubItems;
8211 INT nItem, i;
8212 LVITEMW item;
8213 BOOL changed;
8215 item.iSubItem = nNewColumn;
8216 item.mask = LVIF_TEXT | LVIF_IMAGE;
8217 item.iImage = I_IMAGECALLBACK;
8218 item.pszText = LPSTR_TEXTCALLBACKW;
8220 for (nItem = 0; nItem < infoPtr->nItemCount; nItem++)
8222 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, nItem);
8223 for (i = 1; i < DPA_GetPtrCount(hdpaSubItems); i++)
8225 lpSubItem = DPA_GetPtr(hdpaSubItems, i);
8226 if (lpSubItem->iSubItem >= nNewColumn)
8227 lpSubItem->iSubItem++;
8230 /* add new subitem for each item */
8231 item.iItem = nItem;
8232 set_sub_item(infoPtr, &item, isW, &changed);
8236 /* make space for the new column */
8237 LISTVIEW_ScrollColumns(infoPtr, nNewColumn + 1, lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left);
8238 LISTVIEW_UpdateItemSize(infoPtr);
8240 return nNewColumn;
8242 fail:
8243 if (nNewColumn != -1) SendMessageW(infoPtr->hwndHeader, HDM_DELETEITEM, nNewColumn, 0);
8244 if (lpColumnInfo)
8246 DPA_DeletePtr(infoPtr->hdpaColumns, nNewColumn);
8247 Free(lpColumnInfo);
8249 return -1;
8252 /***
8253 * DESCRIPTION:
8254 * Sets the attributes of a header item.
8256 * PARAMETER(S):
8257 * [I] infoPtr : valid pointer to the listview structure
8258 * [I] nColumn : column index
8259 * [I] lpColumn : column attributes
8260 * [I] isW: if TRUE, then lpColumn is a LPLVCOLUMNW, else it is a LPLVCOLUMNA
8262 * RETURN:
8263 * SUCCESS : TRUE
8264 * FAILURE : FALSE
8266 static BOOL LISTVIEW_SetColumnT(const LISTVIEW_INFO *infoPtr, INT nColumn,
8267 const LVCOLUMNW *lpColumn, BOOL isW)
8269 HDITEMW hdi, hdiget;
8270 BOOL bResult;
8272 TRACE("(nColumn=%d, lpColumn=%s, isW=%d)\n", nColumn, debuglvcolumn_t(lpColumn, isW), isW);
8274 if (!lpColumn || nColumn < 0 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
8276 ZeroMemory(&hdi, sizeof(HDITEMW));
8277 if (lpColumn->mask & LVCF_FMT)
8279 hdi.mask |= HDI_FORMAT;
8280 hdiget.mask = HDI_FORMAT;
8281 if (SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nColumn, (LPARAM)&hdiget))
8282 hdi.fmt = hdiget.fmt & HDF_STRING;
8284 column_fill_hditem(infoPtr, &hdi, nColumn, lpColumn, isW);
8286 /* set header item attributes */
8287 bResult = SendMessageW(infoPtr->hwndHeader, isW ? HDM_SETITEMW : HDM_SETITEMA, nColumn, (LPARAM)&hdi);
8288 if (!bResult) return FALSE;
8290 if (lpColumn->mask & LVCF_FMT)
8292 COLUMN_INFO *lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, nColumn);
8293 INT oldFmt = lpColumnInfo->fmt;
8295 lpColumnInfo->fmt = lpColumn->fmt;
8296 if ((oldFmt ^ lpColumn->fmt) & (LVCFMT_JUSTIFYMASK | LVCFMT_IMAGE))
8298 if (infoPtr->uView == LV_VIEW_DETAILS) LISTVIEW_InvalidateColumn(infoPtr, nColumn);
8302 if (lpColumn->mask & LVCF_MINWIDTH)
8303 LISTVIEW_GetColumnInfo(infoPtr, nColumn)->cxMin = lpColumn->cxMin;
8305 return TRUE;
8308 /***
8309 * DESCRIPTION:
8310 * Sets the column order array
8312 * PARAMETERS:
8313 * [I] infoPtr : valid pointer to the listview structure
8314 * [I] iCount : number of elements in column order array
8315 * [I] lpiArray : pointer to column order array
8317 * RETURN:
8318 * SUCCESS : TRUE
8319 * FAILURE : FALSE
8321 static BOOL LISTVIEW_SetColumnOrderArray(LISTVIEW_INFO *infoPtr, INT iCount, const INT *lpiArray)
8323 if (!infoPtr->hwndHeader) return FALSE;
8324 infoPtr->colRectsDirty = TRUE;
8325 return SendMessageW(infoPtr->hwndHeader, HDM_SETORDERARRAY, iCount, (LPARAM)lpiArray);
8328 /***
8329 * DESCRIPTION:
8330 * Sets the width of a column
8332 * PARAMETERS:
8333 * [I] infoPtr : valid pointer to the listview structure
8334 * [I] nColumn : column index
8335 * [I] cx : column width
8337 * RETURN:
8338 * SUCCESS : TRUE
8339 * FAILURE : FALSE
8341 static BOOL LISTVIEW_SetColumnWidth(LISTVIEW_INFO *infoPtr, INT nColumn, INT cx)
8343 WCHAR szDispText[DISP_TEXT_SIZE] = { 0 };
8344 INT max_cx = 0;
8345 HDITEMW hdi;
8347 TRACE("(nColumn=%d, cx=%d)\n", nColumn, cx);
8349 /* set column width only if in report or list mode */
8350 if (infoPtr->uView != LV_VIEW_DETAILS && infoPtr->uView != LV_VIEW_LIST) return FALSE;
8352 /* take care of invalid cx values - LVSCW_AUTOSIZE_* values are negative,
8353 with _USEHEADER being the lowest */
8354 if (infoPtr->uView == LV_VIEW_DETAILS && cx < LVSCW_AUTOSIZE_USEHEADER) cx = LVSCW_AUTOSIZE;
8355 else if (infoPtr->uView == LV_VIEW_LIST && cx <= 0) return FALSE;
8357 /* resize all columns if in LV_VIEW_LIST mode */
8358 if(infoPtr->uView == LV_VIEW_LIST)
8360 infoPtr->nItemWidth = cx;
8361 LISTVIEW_InvalidateList(infoPtr);
8362 return TRUE;
8365 if (nColumn < 0 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
8367 if (cx == LVSCW_AUTOSIZE || (cx == LVSCW_AUTOSIZE_USEHEADER && nColumn < DPA_GetPtrCount(infoPtr->hdpaColumns) -1))
8369 INT nLabelWidth;
8370 LVITEMW lvItem;
8372 lvItem.mask = LVIF_TEXT;
8373 lvItem.iItem = 0;
8374 lvItem.iSubItem = nColumn;
8375 lvItem.cchTextMax = DISP_TEXT_SIZE;
8376 for (; lvItem.iItem < infoPtr->nItemCount; lvItem.iItem++)
8378 lvItem.pszText = szDispText;
8379 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) continue;
8380 nLabelWidth = LISTVIEW_GetStringWidthT(infoPtr, lvItem.pszText, TRUE);
8381 if (max_cx < nLabelWidth) max_cx = nLabelWidth;
8383 if (infoPtr->himlSmall && (nColumn == 0 || (LISTVIEW_GetColumnInfo(infoPtr, nColumn)->fmt & LVCFMT_IMAGE)))
8384 max_cx += infoPtr->iconSize.cx;
8385 max_cx += TRAILING_LABEL_PADDING;
8388 /* autosize based on listview items width */
8389 if(cx == LVSCW_AUTOSIZE)
8390 cx = max_cx;
8391 else if(cx == LVSCW_AUTOSIZE_USEHEADER)
8393 /* if iCol is the last column make it fill the remainder of the controls width */
8394 if(nColumn == DPA_GetPtrCount(infoPtr->hdpaColumns) - 1)
8396 RECT rcHeader;
8397 POINT Origin;
8399 LISTVIEW_GetOrigin(infoPtr, &Origin);
8400 LISTVIEW_GetHeaderRect(infoPtr, nColumn, &rcHeader);
8402 cx = infoPtr->rcList.right - Origin.x - rcHeader.left;
8404 else
8406 /* Despite what the MS docs say, if this is not the last
8407 column, then MS resizes the column to the width of the
8408 largest text string in the column, including headers
8409 and items. This is different from LVSCW_AUTOSIZE in that
8410 LVSCW_AUTOSIZE ignores the header string length. */
8411 cx = 0;
8413 /* retrieve header text */
8414 hdi.mask = HDI_TEXT|HDI_FORMAT|HDI_IMAGE|HDI_BITMAP;
8415 hdi.cchTextMax = DISP_TEXT_SIZE;
8416 hdi.pszText = szDispText;
8417 if (SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nColumn, (LPARAM)&hdi))
8419 HDC hdc = GetDC(infoPtr->hwndSelf);
8420 HFONT old_font = SelectObject(hdc, (HFONT)SendMessageW(infoPtr->hwndHeader, WM_GETFONT, 0, 0));
8421 HIMAGELIST himl = (HIMAGELIST)SendMessageW(infoPtr->hwndHeader, HDM_GETIMAGELIST, 0, 0);
8422 INT bitmap_margin = 0;
8423 SIZE size;
8425 if (GetTextExtentPoint32W(hdc, hdi.pszText, lstrlenW(hdi.pszText), &size))
8426 cx = size.cx + TRAILING_HEADER_PADDING;
8428 if (hdi.fmt & (HDF_IMAGE|HDF_BITMAP))
8429 bitmap_margin = SendMessageW(infoPtr->hwndHeader, HDM_GETBITMAPMARGIN, 0, 0);
8431 if ((hdi.fmt & HDF_IMAGE) && himl)
8433 INT icon_cx, icon_cy;
8435 if (!ImageList_GetIconSize(himl, &icon_cx, &icon_cy))
8436 cx += icon_cx + 2*bitmap_margin;
8438 else if (hdi.fmt & HDF_BITMAP)
8440 BITMAP bmp;
8442 GetObjectW(hdi.hbm, sizeof(BITMAP), &bmp);
8443 cx += bmp.bmWidth + 2*bitmap_margin;
8446 SelectObject(hdc, old_font);
8447 ReleaseDC(infoPtr->hwndSelf, hdc);
8449 cx = max (cx, max_cx);
8453 if (cx < 0) return FALSE;
8455 /* call header to update the column change */
8456 hdi.mask = HDI_WIDTH;
8457 hdi.cxy = max(cx, LISTVIEW_GetColumnInfo(infoPtr, nColumn)->cxMin);
8458 TRACE("hdi.cxy=%d\n", hdi.cxy);
8459 return SendMessageW(infoPtr->hwndHeader, HDM_SETITEMW, nColumn, (LPARAM)&hdi);
8462 /***
8463 * Creates the checkbox imagelist. Helper for LISTVIEW_SetExtendedListViewStyle
8466 static HIMAGELIST LISTVIEW_CreateCheckBoxIL(const LISTVIEW_INFO *infoPtr)
8468 HDC hdc_wnd, hdc;
8469 HBITMAP hbm_im, hbm_mask, hbm_orig;
8470 RECT rc;
8471 HBRUSH hbr_white = GetStockObject(WHITE_BRUSH);
8472 HBRUSH hbr_black = GetStockObject(BLACK_BRUSH);
8473 HIMAGELIST himl;
8475 himl = ImageList_Create(GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON),
8476 ILC_COLOR | ILC_MASK, 2, 2);
8477 hdc_wnd = GetDC(infoPtr->hwndSelf);
8478 hdc = CreateCompatibleDC(hdc_wnd);
8479 hbm_im = CreateCompatibleBitmap(hdc_wnd, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON));
8480 hbm_mask = CreateBitmap(GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), 1, 1, NULL);
8481 ReleaseDC(infoPtr->hwndSelf, hdc_wnd);
8483 rc.left = rc.top = 0;
8484 rc.right = GetSystemMetrics(SM_CXSMICON);
8485 rc.bottom = GetSystemMetrics(SM_CYSMICON);
8487 hbm_orig = SelectObject(hdc, hbm_mask);
8488 FillRect(hdc, &rc, hbr_white);
8489 InflateRect(&rc, -2, -2);
8490 FillRect(hdc, &rc, hbr_black);
8492 SelectObject(hdc, hbm_im);
8493 DrawFrameControl(hdc, &rc, DFC_BUTTON, DFCS_BUTTONCHECK | DFCS_MONO);
8494 SelectObject(hdc, hbm_orig);
8495 ImageList_Add(himl, hbm_im, hbm_mask);
8497 SelectObject(hdc, hbm_im);
8498 DrawFrameControl(hdc, &rc, DFC_BUTTON, DFCS_BUTTONCHECK | DFCS_MONO | DFCS_CHECKED);
8499 SelectObject(hdc, hbm_orig);
8500 ImageList_Add(himl, hbm_im, hbm_mask);
8502 DeleteObject(hbm_mask);
8503 DeleteObject(hbm_im);
8504 DeleteDC(hdc);
8506 return himl;
8509 /***
8510 * DESCRIPTION:
8511 * Sets the extended listview style.
8513 * PARAMETERS:
8514 * [I] infoPtr : valid pointer to the listview structure
8515 * [I] dwMask : mask
8516 * [I] dwStyle : style
8518 * RETURN:
8519 * SUCCESS : previous style
8520 * FAILURE : 0
8522 static DWORD LISTVIEW_SetExtendedListViewStyle(LISTVIEW_INFO *infoPtr, DWORD mask, DWORD ex_style)
8524 DWORD old_ex_style = infoPtr->dwLvExStyle;
8526 TRACE("mask=0x%08x, ex_style=0x%08x\n", mask, ex_style);
8528 /* set new style */
8529 if (mask)
8530 infoPtr->dwLvExStyle = (old_ex_style & ~mask) | (ex_style & mask);
8531 else
8532 infoPtr->dwLvExStyle = ex_style;
8534 if((infoPtr->dwLvExStyle ^ old_ex_style) & LVS_EX_CHECKBOXES)
8536 HIMAGELIST himl = 0;
8537 if(infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES)
8539 LVITEMW item;
8540 item.mask = LVIF_STATE;
8541 item.stateMask = LVIS_STATEIMAGEMASK;
8542 item.state = INDEXTOSTATEIMAGEMASK(1);
8543 LISTVIEW_SetItemState(infoPtr, -1, &item);
8545 himl = LISTVIEW_CreateCheckBoxIL(infoPtr);
8546 if(!(infoPtr->dwStyle & LVS_SHAREIMAGELISTS))
8547 ImageList_Destroy(infoPtr->himlState);
8549 himl = LISTVIEW_SetImageList(infoPtr, LVSIL_STATE, himl);
8550 /* checkbox list replaces previous custom list or... */
8551 if(((infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES) &&
8552 !(infoPtr->dwStyle & LVS_SHAREIMAGELISTS)) ||
8553 /* ...previous was checkbox list */
8554 (old_ex_style & LVS_EX_CHECKBOXES))
8555 ImageList_Destroy(himl);
8558 if((infoPtr->dwLvExStyle ^ old_ex_style) & LVS_EX_HEADERDRAGDROP)
8560 DWORD style;
8562 /* if not already created */
8563 LISTVIEW_CreateHeader(infoPtr);
8565 style = GetWindowLongW(infoPtr->hwndHeader, GWL_STYLE);
8566 if (infoPtr->dwLvExStyle & LVS_EX_HEADERDRAGDROP)
8567 style |= HDS_DRAGDROP;
8568 else
8569 style &= ~HDS_DRAGDROP;
8570 SetWindowLongW(infoPtr->hwndHeader, GWL_STYLE, style);
8573 /* GRIDLINES adds decoration at top so changes sizes */
8574 if((infoPtr->dwLvExStyle ^ old_ex_style) & LVS_EX_GRIDLINES)
8576 LISTVIEW_CreateHeader(infoPtr);
8577 LISTVIEW_UpdateSize(infoPtr);
8580 if((infoPtr->dwLvExStyle ^ old_ex_style) & LVS_EX_FULLROWSELECT)
8582 LISTVIEW_CreateHeader(infoPtr);
8585 if((infoPtr->dwLvExStyle ^ old_ex_style) & LVS_EX_TRANSPARENTBKGND)
8587 if (infoPtr->dwLvExStyle & LVS_EX_TRANSPARENTBKGND)
8588 LISTVIEW_SetBkColor(infoPtr, CLR_NONE);
8591 if((infoPtr->dwLvExStyle ^ old_ex_style) & LVS_EX_HEADERINALLVIEWS)
8593 if (infoPtr->dwLvExStyle & LVS_EX_HEADERINALLVIEWS)
8594 LISTVIEW_CreateHeader(infoPtr);
8595 else
8596 ShowWindow(infoPtr->hwndHeader, SW_HIDE);
8597 LISTVIEW_UpdateSize(infoPtr);
8598 LISTVIEW_UpdateScroll(infoPtr);
8601 LISTVIEW_InvalidateList(infoPtr);
8602 return old_ex_style;
8605 /***
8606 * DESCRIPTION:
8607 * Sets the new hot cursor used during hot tracking and hover selection.
8609 * PARAMETER(S):
8610 * [I] infoPtr : valid pointer to the listview structure
8611 * [I] hCursor : the new hot cursor handle
8613 * RETURN:
8614 * Returns the previous hot cursor
8616 static HCURSOR LISTVIEW_SetHotCursor(LISTVIEW_INFO *infoPtr, HCURSOR hCursor)
8618 HCURSOR oldCursor = infoPtr->hHotCursor;
8620 infoPtr->hHotCursor = hCursor;
8622 return oldCursor;
8626 /***
8627 * DESCRIPTION:
8628 * Sets the hot item index.
8630 * PARAMETERS:
8631 * [I] infoPtr : valid pointer to the listview structure
8632 * [I] iIndex : index
8634 * RETURN:
8635 * SUCCESS : previous hot item index
8636 * FAILURE : -1 (no hot item)
8638 static INT LISTVIEW_SetHotItem(LISTVIEW_INFO *infoPtr, INT iIndex)
8640 INT iOldIndex = infoPtr->nHotItem;
8642 infoPtr->nHotItem = iIndex;
8644 return iOldIndex;
8648 /***
8649 * DESCRIPTION:
8650 * Sets the amount of time the cursor must hover over an item before it is selected.
8652 * PARAMETER(S):
8653 * [I] infoPtr : valid pointer to the listview structure
8654 * [I] dwHoverTime : hover time, if -1 the hover time is set to the default
8656 * RETURN:
8657 * Returns the previous hover time
8659 static DWORD LISTVIEW_SetHoverTime(LISTVIEW_INFO *infoPtr, DWORD dwHoverTime)
8661 DWORD oldHoverTime = infoPtr->dwHoverTime;
8663 infoPtr->dwHoverTime = dwHoverTime;
8665 return oldHoverTime;
8668 /***
8669 * DESCRIPTION:
8670 * Sets spacing for icons of LVS_ICON style.
8672 * PARAMETER(S):
8673 * [I] infoPtr : valid pointer to the listview structure
8674 * [I] cx : horizontal spacing (-1 = system spacing, 0 = autosize)
8675 * [I] cy : vertical spacing (-1 = system spacing, 0 = autosize)
8677 * RETURN:
8678 * MAKELONG(oldcx, oldcy)
8680 static DWORD LISTVIEW_SetIconSpacing(LISTVIEW_INFO *infoPtr, INT cx, INT cy)
8682 INT iconWidth = 0, iconHeight = 0;
8683 DWORD oldspacing = MAKELONG(infoPtr->iconSpacing.cx, infoPtr->iconSpacing.cy);
8685 TRACE("requested=(%d,%d)\n", cx, cy);
8687 /* set to defaults, if instructed to */
8688 if (cx == -1 && cy == -1)
8690 infoPtr->autoSpacing = TRUE;
8691 if (infoPtr->himlNormal)
8692 ImageList_GetIconSize(infoPtr->himlNormal, &iconWidth, &iconHeight);
8693 cx = GetSystemMetrics(SM_CXICONSPACING) - GetSystemMetrics(SM_CXICON) + iconWidth;
8694 cy = GetSystemMetrics(SM_CYICONSPACING) - GetSystemMetrics(SM_CYICON) + iconHeight;
8696 else
8697 infoPtr->autoSpacing = FALSE;
8699 /* if 0 then keep width */
8700 if (cx != 0)
8701 infoPtr->iconSpacing.cx = cx;
8703 /* if 0 then keep height */
8704 if (cy != 0)
8705 infoPtr->iconSpacing.cy = cy;
8707 TRACE("old=(%d,%d), new=(%d,%d), iconSize=(%d,%d), ntmH=%d\n",
8708 LOWORD(oldspacing), HIWORD(oldspacing), infoPtr->iconSpacing.cx, infoPtr->iconSpacing.cy,
8709 infoPtr->iconSize.cx, infoPtr->iconSize.cy,
8710 infoPtr->ntmHeight);
8712 /* these depend on the iconSpacing */
8713 LISTVIEW_UpdateItemSize(infoPtr);
8715 return oldspacing;
8718 static inline void set_icon_size(SIZE *size, HIMAGELIST himl, BOOL small)
8720 INT cx, cy;
8722 if (himl && ImageList_GetIconSize(himl, &cx, &cy))
8724 size->cx = cx;
8725 size->cy = cy;
8727 else
8729 size->cx = GetSystemMetrics(small ? SM_CXSMICON : SM_CXICON);
8730 size->cy = GetSystemMetrics(small ? SM_CYSMICON : SM_CYICON);
8734 /***
8735 * DESCRIPTION:
8736 * Sets image lists.
8738 * PARAMETER(S):
8739 * [I] infoPtr : valid pointer to the listview structure
8740 * [I] nType : image list type
8741 * [I] himl : image list handle
8743 * RETURN:
8744 * SUCCESS : old image list
8745 * FAILURE : NULL
8747 static HIMAGELIST LISTVIEW_SetImageList(LISTVIEW_INFO *infoPtr, INT nType, HIMAGELIST himl)
8749 INT oldHeight = infoPtr->nItemHeight;
8750 HIMAGELIST himlOld = 0;
8752 TRACE("(nType=%d, himl=%p)\n", nType, himl);
8754 switch (nType)
8756 case LVSIL_NORMAL:
8757 himlOld = infoPtr->himlNormal;
8758 infoPtr->himlNormal = himl;
8759 if (infoPtr->uView == LV_VIEW_ICON) set_icon_size(&infoPtr->iconSize, himl, FALSE);
8760 if (infoPtr->autoSpacing)
8761 LISTVIEW_SetIconSpacing(infoPtr, -1, -1);
8762 break;
8764 case LVSIL_SMALL:
8765 himlOld = infoPtr->himlSmall;
8766 infoPtr->himlSmall = himl;
8767 if (infoPtr->uView != LV_VIEW_ICON) set_icon_size(&infoPtr->iconSize, himl, TRUE);
8768 if (infoPtr->hwndHeader)
8769 SendMessageW(infoPtr->hwndHeader, HDM_SETIMAGELIST, 0, (LPARAM)himl);
8770 break;
8772 case LVSIL_STATE:
8773 himlOld = infoPtr->himlState;
8774 infoPtr->himlState = himl;
8775 set_icon_size(&infoPtr->iconStateSize, himl, TRUE);
8776 ImageList_SetBkColor(infoPtr->himlState, CLR_NONE);
8777 break;
8779 default:
8780 ERR("Unknown icon type=%d\n", nType);
8781 return NULL;
8784 infoPtr->nItemHeight = LISTVIEW_CalculateItemHeight(infoPtr);
8785 if (infoPtr->nItemHeight != oldHeight)
8786 LISTVIEW_UpdateScroll(infoPtr);
8788 return himlOld;
8791 /***
8792 * DESCRIPTION:
8793 * Preallocates memory (does *not* set the actual count of items !)
8795 * PARAMETER(S):
8796 * [I] infoPtr : valid pointer to the listview structure
8797 * [I] nItems : item count (projected number of items to allocate)
8798 * [I] dwFlags : update flags
8800 * RETURN:
8801 * SUCCESS : TRUE
8802 * FAILURE : FALSE
8804 static BOOL LISTVIEW_SetItemCount(LISTVIEW_INFO *infoPtr, INT nItems, DWORD dwFlags)
8806 TRACE("(nItems=%d, dwFlags=%x)\n", nItems, dwFlags);
8808 if (infoPtr->dwStyle & LVS_OWNERDATA)
8810 INT nOldCount = infoPtr->nItemCount;
8812 if (nItems < nOldCount)
8814 RANGE range = { nItems, nOldCount };
8815 ranges_del(infoPtr->selectionRanges, range);
8816 if (infoPtr->nFocusedItem >= nItems)
8818 LISTVIEW_SetItemFocus(infoPtr, -1);
8819 SetRectEmpty(&infoPtr->rcFocus);
8823 infoPtr->nItemCount = nItems;
8824 LISTVIEW_UpdateScroll(infoPtr);
8826 /* the flags are valid only in ownerdata report and list modes */
8827 if (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON) dwFlags = 0;
8829 if (!(dwFlags & LVSICF_NOSCROLL) && infoPtr->nFocusedItem != -1)
8830 LISTVIEW_EnsureVisible(infoPtr, infoPtr->nFocusedItem, FALSE);
8832 if (!(dwFlags & LVSICF_NOINVALIDATEALL))
8833 LISTVIEW_InvalidateList(infoPtr);
8834 else
8836 INT nFrom, nTo;
8837 POINT Origin;
8838 RECT rcErase;
8840 LISTVIEW_GetOrigin(infoPtr, &Origin);
8841 nFrom = min(nOldCount, nItems);
8842 nTo = max(nOldCount, nItems);
8844 if (infoPtr->uView == LV_VIEW_DETAILS)
8846 rcErase.left = 0;
8847 rcErase.top = nFrom * infoPtr->nItemHeight;
8848 rcErase.right = infoPtr->nItemWidth;
8849 rcErase.bottom = nTo * infoPtr->nItemHeight;
8850 OffsetRect(&rcErase, Origin.x, Origin.y);
8851 if (IntersectRect(&rcErase, &rcErase, &infoPtr->rcList))
8852 LISTVIEW_InvalidateRect(infoPtr, &rcErase);
8854 else /* LV_VIEW_LIST */
8856 INT nPerCol = LISTVIEW_GetCountPerColumn(infoPtr);
8858 rcErase.left = (nFrom / nPerCol) * infoPtr->nItemWidth;
8859 rcErase.top = (nFrom % nPerCol) * infoPtr->nItemHeight;
8860 rcErase.right = rcErase.left + infoPtr->nItemWidth;
8861 rcErase.bottom = nPerCol * infoPtr->nItemHeight;
8862 OffsetRect(&rcErase, Origin.x, Origin.y);
8863 if (IntersectRect(&rcErase, &rcErase, &infoPtr->rcList))
8864 LISTVIEW_InvalidateRect(infoPtr, &rcErase);
8866 rcErase.left = (nFrom / nPerCol + 1) * infoPtr->nItemWidth;
8867 rcErase.top = 0;
8868 rcErase.right = (nTo / nPerCol + 1) * infoPtr->nItemWidth;
8869 rcErase.bottom = nPerCol * infoPtr->nItemHeight;
8870 OffsetRect(&rcErase, Origin.x, Origin.y);
8871 if (IntersectRect(&rcErase, &rcErase, &infoPtr->rcList))
8872 LISTVIEW_InvalidateRect(infoPtr, &rcErase);
8876 else
8878 /* According to MSDN for non-LVS_OWNERDATA this is just
8879 * a performance issue. The control allocates its internal
8880 * data structures for the number of items specified. It
8881 * cuts down on the number of memory allocations. Therefore
8882 * we will just issue a WARN here
8884 WARN("for non-ownerdata performance option not implemented.\n");
8887 return TRUE;
8890 /***
8891 * DESCRIPTION:
8892 * Sets the position of an item.
8894 * PARAMETER(S):
8895 * [I] infoPtr : valid pointer to the listview structure
8896 * [I] nItem : item index
8897 * [I] pt : coordinate
8899 * RETURN:
8900 * SUCCESS : TRUE
8901 * FAILURE : FALSE
8903 static BOOL LISTVIEW_SetItemPosition(LISTVIEW_INFO *infoPtr, INT nItem, const POINT *pt)
8905 POINT Origin, Pt;
8907 TRACE("(nItem=%d, pt=%s)\n", nItem, wine_dbgstr_point(pt));
8909 if (!pt || nItem < 0 || nItem >= infoPtr->nItemCount ||
8910 !(infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON)) return FALSE;
8912 Pt = *pt;
8913 LISTVIEW_GetOrigin(infoPtr, &Origin);
8915 /* This point value seems to be an undocumented feature.
8916 * The best guess is that it means either at the origin,
8917 * or at true beginning of the list. I will assume the origin. */
8918 if ((Pt.x == -1) && (Pt.y == -1))
8919 Pt = Origin;
8921 if (infoPtr->uView == LV_VIEW_ICON)
8923 Pt.x -= (infoPtr->nItemWidth - infoPtr->iconSize.cx) / 2;
8924 Pt.y -= ICON_TOP_PADDING;
8926 Pt.x -= Origin.x;
8927 Pt.y -= Origin.y;
8929 infoPtr->bAutoarrange = FALSE;
8931 return LISTVIEW_MoveIconTo(infoPtr, nItem, &Pt, FALSE);
8934 /***
8935 * DESCRIPTION:
8936 * Sets the state of one or many items.
8938 * PARAMETER(S):
8939 * [I] infoPtr : valid pointer to the listview structure
8940 * [I] nItem : item index
8941 * [I] item : item or subitem info
8943 * RETURN:
8944 * SUCCESS : TRUE
8945 * FAILURE : FALSE
8947 static BOOL LISTVIEW_SetItemState(LISTVIEW_INFO *infoPtr, INT nItem, const LVITEMW *item)
8949 BOOL ret = TRUE;
8950 LVITEMW lvItem;
8952 if (!item) return FALSE;
8954 lvItem.iItem = nItem;
8955 lvItem.iSubItem = 0;
8956 lvItem.mask = LVIF_STATE;
8957 lvItem.state = item->state;
8958 lvItem.stateMask = item->stateMask;
8959 TRACE("item=%s\n", debuglvitem_t(&lvItem, TRUE));
8961 if (nItem == -1)
8963 UINT oldstate = 0;
8964 BOOL notify;
8966 /* special case optimization for recurring attempt to deselect all */
8967 if (lvItem.state == 0 && lvItem.stateMask == LVIS_SELECTED && !LISTVIEW_GetSelectedCount(infoPtr))
8968 return TRUE;
8970 /* select all isn't allowed in LVS_SINGLESEL */
8971 if ((lvItem.state & lvItem.stateMask & LVIS_SELECTED) && (infoPtr->dwStyle & LVS_SINGLESEL))
8972 return FALSE;
8974 /* focus all isn't allowed */
8975 if (lvItem.state & lvItem.stateMask & LVIS_FOCUSED) return FALSE;
8977 notify = infoPtr->bDoChangeNotify;
8978 if (infoPtr->dwStyle & LVS_OWNERDATA)
8980 infoPtr->bDoChangeNotify = FALSE;
8981 if (!(lvItem.state & LVIS_SELECTED) && LISTVIEW_GetSelectedCount(infoPtr))
8982 oldstate |= LVIS_SELECTED;
8983 if (infoPtr->nFocusedItem != -1) oldstate |= LVIS_FOCUSED;
8986 /* apply to all items */
8987 for (lvItem.iItem = 0; lvItem.iItem < infoPtr->nItemCount; lvItem.iItem++)
8988 if (!LISTVIEW_SetItemT(infoPtr, &lvItem, TRUE)) ret = FALSE;
8990 if (infoPtr->dwStyle & LVS_OWNERDATA)
8992 NMLISTVIEW nmlv;
8994 infoPtr->bDoChangeNotify = notify;
8996 nmlv.iItem = -1;
8997 nmlv.iSubItem = 0;
8998 nmlv.uNewState = lvItem.state & lvItem.stateMask;
8999 nmlv.uOldState = oldstate & lvItem.stateMask;
9000 nmlv.uChanged = LVIF_STATE;
9001 nmlv.ptAction.x = nmlv.ptAction.y = 0;
9002 nmlv.lParam = 0;
9004 notify_listview(infoPtr, LVN_ITEMCHANGED, &nmlv);
9007 else
9008 ret = LISTVIEW_SetItemT(infoPtr, &lvItem, TRUE);
9010 return ret;
9013 /***
9014 * DESCRIPTION:
9015 * Sets the text of an item or subitem.
9017 * PARAMETER(S):
9018 * [I] hwnd : window handle
9019 * [I] nItem : item index
9020 * [I] lpLVItem : item or subitem info
9021 * [I] isW : TRUE if input is Unicode
9023 * RETURN:
9024 * SUCCESS : TRUE
9025 * FAILURE : FALSE
9027 static BOOL LISTVIEW_SetItemTextT(LISTVIEW_INFO *infoPtr, INT nItem, const LVITEMW *lpLVItem, BOOL isW)
9029 LVITEMW lvItem;
9031 if (!lpLVItem || nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
9032 if (infoPtr->dwStyle & LVS_OWNERDATA) return FALSE;
9034 lvItem.iItem = nItem;
9035 lvItem.iSubItem = lpLVItem->iSubItem;
9036 lvItem.mask = LVIF_TEXT;
9037 lvItem.pszText = lpLVItem->pszText;
9038 lvItem.cchTextMax = lpLVItem->cchTextMax;
9040 TRACE("(nItem=%d, lpLVItem=%s, isW=%d)\n", nItem, debuglvitem_t(&lvItem, isW), isW);
9042 return LISTVIEW_SetItemT(infoPtr, &lvItem, isW);
9045 /***
9046 * DESCRIPTION:
9047 * Set item index that marks the start of a multiple selection.
9049 * PARAMETER(S):
9050 * [I] infoPtr : valid pointer to the listview structure
9051 * [I] nIndex : index
9053 * RETURN:
9054 * Index number or -1 if there is no selection mark.
9056 static INT LISTVIEW_SetSelectionMark(LISTVIEW_INFO *infoPtr, INT nIndex)
9058 INT nOldIndex = infoPtr->nSelectionMark;
9060 TRACE("(nIndex=%d)\n", nIndex);
9062 infoPtr->nSelectionMark = nIndex;
9064 return nOldIndex;
9067 /***
9068 * DESCRIPTION:
9069 * Sets the text background color.
9071 * PARAMETER(S):
9072 * [I] infoPtr : valid pointer to the listview structure
9073 * [I] color : text background color
9075 * RETURN:
9076 * SUCCESS : TRUE
9077 * FAILURE : FALSE
9079 static BOOL LISTVIEW_SetTextBkColor(LISTVIEW_INFO *infoPtr, COLORREF color)
9081 TRACE("(color=%x)\n", color);
9083 infoPtr->clrTextBk = color;
9084 return TRUE;
9087 /***
9088 * DESCRIPTION:
9089 * Sets the text foreground color.
9091 * PARAMETER(S):
9092 * [I] infoPtr : valid pointer to the listview structure
9093 * [I] color : text color
9095 * RETURN:
9096 * SUCCESS : TRUE
9097 * FAILURE : FALSE
9099 static BOOL LISTVIEW_SetTextColor (LISTVIEW_INFO *infoPtr, COLORREF color)
9101 TRACE("(color=%x)\n", color);
9103 infoPtr->clrText = color;
9104 return TRUE;
9107 /***
9108 * DESCRIPTION:
9109 * Sets new ToolTip window to ListView control.
9111 * PARAMETER(S):
9112 * [I] infoPtr : valid pointer to the listview structure
9113 * [I] hwndNewToolTip : handle to new ToolTip
9115 * RETURN:
9116 * old tool tip
9118 static HWND LISTVIEW_SetToolTips( LISTVIEW_INFO *infoPtr, HWND hwndNewToolTip)
9120 HWND hwndOldToolTip = infoPtr->hwndToolTip;
9121 infoPtr->hwndToolTip = hwndNewToolTip;
9122 return hwndOldToolTip;
9126 * DESCRIPTION:
9127 * sets the Unicode character format flag for the control
9128 * PARAMETER(S):
9129 * [I] infoPtr :valid pointer to the listview structure
9130 * [I] fUnicode :true to switch to UNICODE false to switch to ANSI
9132 * RETURN:
9133 * Old Unicode Format
9135 static BOOL LISTVIEW_SetUnicodeFormat( LISTVIEW_INFO *infoPtr, BOOL unicode)
9137 SHORT rc = infoPtr->notifyFormat;
9138 infoPtr->notifyFormat = (unicode) ? NFR_UNICODE : NFR_ANSI;
9139 return rc == NFR_UNICODE;
9143 * DESCRIPTION:
9144 * sets the control view mode
9145 * PARAMETER(S):
9146 * [I] infoPtr :valid pointer to the listview structure
9147 * [I] nView :new view mode value
9149 * RETURN:
9150 * SUCCESS: 1
9151 * FAILURE: -1
9153 static INT LISTVIEW_SetView(LISTVIEW_INFO *infoPtr, DWORD nView)
9155 HIMAGELIST himl;
9157 if (infoPtr->uView == nView) return 1;
9159 if ((INT)nView < 0 || nView > LV_VIEW_MAX) return -1;
9160 if (nView == LV_VIEW_TILE)
9162 FIXME("View LV_VIEW_TILE unimplemented\n");
9163 return -1;
9166 infoPtr->uView = nView;
9168 SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
9169 ShowWindow(infoPtr->hwndHeader, SW_HIDE);
9171 ShowScrollBar(infoPtr->hwndSelf, SB_BOTH, FALSE);
9172 SetRectEmpty(&infoPtr->rcFocus);
9174 himl = (nView == LV_VIEW_ICON ? infoPtr->himlNormal : infoPtr->himlSmall);
9175 set_icon_size(&infoPtr->iconSize, himl, nView != LV_VIEW_ICON);
9177 switch (nView)
9179 case LV_VIEW_ICON:
9180 case LV_VIEW_SMALLICON:
9181 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
9182 break;
9183 case LV_VIEW_DETAILS:
9185 HDLAYOUT hl;
9186 WINDOWPOS wp;
9188 LISTVIEW_CreateHeader( infoPtr );
9190 hl.prc = &infoPtr->rcList;
9191 hl.pwpos = &wp;
9192 SendMessageW(infoPtr->hwndHeader, HDM_LAYOUT, 0, (LPARAM)&hl);
9193 SetWindowPos(infoPtr->hwndHeader, infoPtr->hwndSelf, wp.x, wp.y, wp.cx, wp.cy,
9194 wp.flags | ((infoPtr->dwStyle & LVS_NOCOLUMNHEADER) ? SWP_HIDEWINDOW : SWP_SHOWWINDOW));
9195 break;
9197 case LV_VIEW_LIST:
9198 break;
9201 LISTVIEW_UpdateItemSize(infoPtr);
9202 LISTVIEW_UpdateSize(infoPtr);
9203 LISTVIEW_UpdateScroll(infoPtr);
9204 LISTVIEW_InvalidateList(infoPtr);
9206 TRACE("nView=%d\n", nView);
9208 return 1;
9211 /* LISTVIEW_SetWorkAreas */
9213 /***
9214 * DESCRIPTION:
9215 * Callback internally used by LISTVIEW_SortItems() in response of LVM_SORTITEMS
9217 * PARAMETER(S):
9218 * [I] first : pointer to first ITEM_INFO to compare
9219 * [I] second : pointer to second ITEM_INFO to compare
9220 * [I] lParam : HWND of control
9222 * RETURN:
9223 * if first comes before second : negative
9224 * if first comes after second : positive
9225 * if first and second are equivalent : zero
9227 static INT WINAPI LISTVIEW_CallBackCompare(LPVOID first, LPVOID second, LPARAM lParam)
9229 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)lParam;
9230 ITEM_INFO* lv_first = DPA_GetPtr( first, 0 );
9231 ITEM_INFO* lv_second = DPA_GetPtr( second, 0 );
9233 /* Forward the call to the client defined callback */
9234 return (infoPtr->pfnCompare)( lv_first->lParam , lv_second->lParam, infoPtr->lParamSort );
9237 /***
9238 * DESCRIPTION:
9239 * Callback internally used by LISTVIEW_SortItems() in response of LVM_SORTITEMSEX
9241 * PARAMETER(S):
9242 * [I] first : pointer to first ITEM_INFO to compare
9243 * [I] second : pointer to second ITEM_INFO to compare
9244 * [I] lParam : HWND of control
9246 * RETURN:
9247 * if first comes before second : negative
9248 * if first comes after second : positive
9249 * if first and second are equivalent : zero
9251 static INT WINAPI LISTVIEW_CallBackCompareEx(LPVOID first, LPVOID second, LPARAM lParam)
9253 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)lParam;
9254 INT first_idx = DPA_GetPtrIndex( infoPtr->hdpaItems, first );
9255 INT second_idx = DPA_GetPtrIndex( infoPtr->hdpaItems, second );
9257 /* Forward the call to the client defined callback */
9258 return (infoPtr->pfnCompare)( first_idx, second_idx, infoPtr->lParamSort );
9261 /***
9262 * DESCRIPTION:
9263 * Sorts the listview items.
9265 * PARAMETER(S):
9266 * [I] infoPtr : valid pointer to the listview structure
9267 * [I] pfnCompare : application-defined value
9268 * [I] lParamSort : pointer to comparison callback
9269 * [I] IsEx : TRUE when LVM_SORTITEMSEX used
9271 * RETURN:
9272 * SUCCESS : TRUE
9273 * FAILURE : FALSE
9275 static BOOL LISTVIEW_SortItems(LISTVIEW_INFO *infoPtr, PFNLVCOMPARE pfnCompare,
9276 LPARAM lParamSort, BOOL IsEx)
9278 HDPA hdpaSubItems;
9279 ITEM_INFO *lpItem;
9280 LPVOID selectionMarkItem = NULL;
9281 LPVOID focusedItem = NULL;
9282 int i;
9284 TRACE("(pfnCompare=%p, lParamSort=%lx)\n", pfnCompare, lParamSort);
9286 if (infoPtr->dwStyle & LVS_OWNERDATA) return FALSE;
9288 if (!pfnCompare) return FALSE;
9289 if (!infoPtr->hdpaItems) return FALSE;
9291 /* if there are 0 or 1 items, there is no need to sort */
9292 if (infoPtr->nItemCount < 2) return TRUE;
9294 /* clear selection */
9295 ranges_clear(infoPtr->selectionRanges);
9297 /* save selection mark and focused item */
9298 if (infoPtr->nSelectionMark >= 0)
9299 selectionMarkItem = DPA_GetPtr(infoPtr->hdpaItems, infoPtr->nSelectionMark);
9300 if (infoPtr->nFocusedItem >= 0)
9301 focusedItem = DPA_GetPtr(infoPtr->hdpaItems, infoPtr->nFocusedItem);
9303 infoPtr->pfnCompare = pfnCompare;
9304 infoPtr->lParamSort = lParamSort;
9305 if (IsEx)
9306 DPA_Sort(infoPtr->hdpaItems, LISTVIEW_CallBackCompareEx, (LPARAM)infoPtr);
9307 else
9308 DPA_Sort(infoPtr->hdpaItems, LISTVIEW_CallBackCompare, (LPARAM)infoPtr);
9310 /* restore selection ranges */
9311 for (i=0; i < infoPtr->nItemCount; i++)
9313 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, i);
9314 lpItem = DPA_GetPtr(hdpaSubItems, 0);
9316 if (lpItem->state & LVIS_SELECTED)
9317 ranges_additem(infoPtr->selectionRanges, i);
9319 /* restore selection mark and focused item */
9320 infoPtr->nSelectionMark = DPA_GetPtrIndex(infoPtr->hdpaItems, selectionMarkItem);
9321 infoPtr->nFocusedItem = DPA_GetPtrIndex(infoPtr->hdpaItems, focusedItem);
9323 /* I believe nHotItem should be left alone, see LISTVIEW_ShiftIndices */
9325 /* refresh the display */
9326 LISTVIEW_InvalidateList(infoPtr);
9327 return TRUE;
9330 /***
9331 * DESCRIPTION:
9332 * Update theme handle after a theme change.
9334 * PARAMETER(S):
9335 * [I] infoPtr : valid pointer to the listview structure
9337 * RETURN:
9338 * SUCCESS : 0
9339 * FAILURE : something else
9341 static LRESULT LISTVIEW_ThemeChanged(const LISTVIEW_INFO *infoPtr)
9343 HTHEME theme = GetWindowTheme(infoPtr->hwndSelf);
9344 CloseThemeData(theme);
9345 OpenThemeData(infoPtr->hwndSelf, themeClass);
9346 return 0;
9349 /***
9350 * DESCRIPTION:
9351 * Updates an items or rearranges the listview control.
9353 * PARAMETER(S):
9354 * [I] infoPtr : valid pointer to the listview structure
9355 * [I] nItem : item index
9357 * RETURN:
9358 * SUCCESS : TRUE
9359 * FAILURE : FALSE
9361 static BOOL LISTVIEW_Update(LISTVIEW_INFO *infoPtr, INT nItem)
9363 TRACE("(nItem=%d)\n", nItem);
9365 if (nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
9367 /* rearrange with default alignment style */
9368 if (is_autoarrange(infoPtr))
9369 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
9370 else
9371 LISTVIEW_InvalidateItem(infoPtr, nItem);
9373 return TRUE;
9376 /***
9377 * DESCRIPTION:
9378 * Draw the track line at the place defined in the infoPtr structure.
9379 * The line is drawn with a XOR pen so drawing the line for the second time
9380 * in the same place erases the line.
9382 * PARAMETER(S):
9383 * [I] infoPtr : valid pointer to the listview structure
9385 * RETURN:
9386 * SUCCESS : TRUE
9387 * FAILURE : FALSE
9389 static BOOL LISTVIEW_DrawTrackLine(const LISTVIEW_INFO *infoPtr)
9391 HDC hdc;
9393 if (infoPtr->xTrackLine == -1)
9394 return FALSE;
9396 if (!(hdc = GetDC(infoPtr->hwndSelf)))
9397 return FALSE;
9398 PatBlt( hdc, infoPtr->xTrackLine, infoPtr->rcList.top,
9399 1, infoPtr->rcList.bottom - infoPtr->rcList.top, DSTINVERT );
9400 ReleaseDC(infoPtr->hwndSelf, hdc);
9401 return TRUE;
9404 /***
9405 * DESCRIPTION:
9406 * Called when an edit control should be displayed. This function is called after
9407 * we are sure that there was a single click - not a double click (this is a TIMERPROC).
9409 * PARAMETER(S):
9410 * [I] hwnd : Handle to the listview
9411 * [I] uMsg : WM_TIMER (ignored)
9412 * [I] idEvent : The timer ID interpreted as a pointer to a DELAYED_EDIT_ITEM struct
9413 * [I] dwTimer : The elapsed time (ignored)
9415 * RETURN:
9416 * None.
9418 static VOID CALLBACK LISTVIEW_DelayedEditItem(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
9420 DELAYED_ITEM_EDIT *editItem = (DELAYED_ITEM_EDIT *)idEvent;
9421 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongPtrW(hwnd, 0);
9423 KillTimer(hwnd, idEvent);
9424 editItem->fEnabled = FALSE;
9425 /* check if the item is still selected */
9426 if (infoPtr->bFocus && LISTVIEW_GetItemState(infoPtr, editItem->iItem, LVIS_SELECTED))
9427 LISTVIEW_EditLabelT(infoPtr, editItem->iItem, TRUE);
9430 /***
9431 * DESCRIPTION:
9432 * Creates the listview control - the WM_NCCREATE phase.
9434 * PARAMETER(S):
9435 * [I] hwnd : window handle
9436 * [I] lpcs : the create parameters
9438 * RETURN:
9439 * Success: TRUE
9440 * Failure: FALSE
9442 static LRESULT LISTVIEW_NCCreate(HWND hwnd, const CREATESTRUCTW *lpcs)
9444 LISTVIEW_INFO *infoPtr;
9445 LOGFONTW logFont;
9447 TRACE("(lpcs=%p)\n", lpcs);
9449 /* initialize info pointer */
9450 infoPtr = Alloc(sizeof(LISTVIEW_INFO));
9451 if (!infoPtr) return FALSE;
9453 SetWindowLongPtrW(hwnd, 0, (DWORD_PTR)infoPtr);
9455 infoPtr->hwndSelf = hwnd;
9456 infoPtr->dwStyle = lpcs->style; /* Note: may be changed in WM_CREATE */
9457 map_style_view(infoPtr);
9458 /* determine the type of structures to use */
9459 infoPtr->hwndNotify = lpcs->hwndParent;
9460 /* infoPtr->notifyFormat will be filled in WM_CREATE */
9462 /* initialize color information */
9463 infoPtr->clrBk = CLR_NONE;
9464 infoPtr->clrText = CLR_DEFAULT;
9465 infoPtr->clrTextBk = CLR_DEFAULT;
9466 LISTVIEW_SetBkColor(infoPtr, comctl32_color.clrWindow);
9468 /* set default values */
9469 infoPtr->nFocusedItem = -1;
9470 infoPtr->nSelectionMark = -1;
9471 infoPtr->nHotItem = -1;
9472 infoPtr->bRedraw = TRUE;
9473 infoPtr->bNoItemMetrics = TRUE;
9474 infoPtr->bDoChangeNotify = TRUE;
9475 infoPtr->autoSpacing = TRUE;
9476 infoPtr->iconSpacing.cx = GetSystemMetrics(SM_CXICONSPACING) - GetSystemMetrics(SM_CXICON);
9477 infoPtr->iconSpacing.cy = GetSystemMetrics(SM_CYICONSPACING) - GetSystemMetrics(SM_CYICON);
9478 infoPtr->nEditLabelItem = -1;
9479 infoPtr->nLButtonDownItem = -1;
9480 infoPtr->dwHoverTime = HOVER_DEFAULT; /* default system hover time */
9481 infoPtr->cWheelRemainder = 0;
9482 infoPtr->nMeasureItemHeight = 0;
9483 infoPtr->xTrackLine = -1; /* no track line */
9484 infoPtr->itemEdit.fEnabled = FALSE;
9485 infoPtr->iVersion = COMCTL32_VERSION;
9486 infoPtr->colRectsDirty = FALSE;
9488 /* get default font (icon title) */
9489 SystemParametersInfoW(SPI_GETICONTITLELOGFONT, 0, &logFont, 0);
9490 infoPtr->hDefaultFont = CreateFontIndirectW(&logFont);
9491 infoPtr->hFont = infoPtr->hDefaultFont;
9492 LISTVIEW_SaveTextMetrics(infoPtr);
9494 /* allocate memory for the data structure */
9495 if (!(infoPtr->selectionRanges = ranges_create(10))) goto fail;
9496 if (!(infoPtr->hdpaItems = DPA_Create(10))) goto fail;
9497 if (!(infoPtr->hdpaItemIds = DPA_Create(10))) goto fail;
9498 if (!(infoPtr->hdpaPosX = DPA_Create(10))) goto fail;
9499 if (!(infoPtr->hdpaPosY = DPA_Create(10))) goto fail;
9500 if (!(infoPtr->hdpaColumns = DPA_Create(10))) goto fail;
9501 return TRUE;
9503 fail:
9504 DestroyWindow(infoPtr->hwndHeader);
9505 ranges_destroy(infoPtr->selectionRanges);
9506 DPA_Destroy(infoPtr->hdpaItems);
9507 DPA_Destroy(infoPtr->hdpaItemIds);
9508 DPA_Destroy(infoPtr->hdpaPosX);
9509 DPA_Destroy(infoPtr->hdpaPosY);
9510 DPA_Destroy(infoPtr->hdpaColumns);
9511 Free(infoPtr);
9512 return FALSE;
9515 /***
9516 * DESCRIPTION:
9517 * Creates the listview control - the WM_CREATE phase. Most of the data is
9518 * already set up in LISTVIEW_NCCreate
9520 * PARAMETER(S):
9521 * [I] hwnd : window handle
9522 * [I] lpcs : the create parameters
9524 * RETURN:
9525 * Success: 0
9526 * Failure: -1
9528 static LRESULT LISTVIEW_Create(HWND hwnd, const CREATESTRUCTW *lpcs)
9530 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongPtrW(hwnd, 0);
9532 TRACE("(lpcs=%p, style=0x%08x)\n", lpcs, lpcs->style);
9534 infoPtr->dwStyle = lpcs->style;
9535 map_style_view(infoPtr);
9537 infoPtr->notifyFormat = SendMessageW(infoPtr->hwndNotify, WM_NOTIFYFORMAT,
9538 (WPARAM)infoPtr->hwndSelf, NF_QUERY);
9539 /* on error defaulting to ANSI notifications */
9540 if (infoPtr->notifyFormat == 0) infoPtr->notifyFormat = NFR_ANSI;
9541 TRACE("notify format=%d\n", infoPtr->notifyFormat);
9543 if ((infoPtr->uView == LV_VIEW_DETAILS) && (lpcs->style & WS_VISIBLE))
9545 if (LISTVIEW_CreateHeader(infoPtr) < 0) return -1;
9547 else
9548 infoPtr->hwndHeader = 0;
9550 /* init item size to avoid division by 0 */
9551 LISTVIEW_UpdateItemSize (infoPtr);
9552 LISTVIEW_UpdateSize (infoPtr);
9554 if (infoPtr->uView == LV_VIEW_DETAILS)
9556 if (!(LVS_NOCOLUMNHEADER & lpcs->style) && (WS_VISIBLE & lpcs->style))
9558 ShowWindow(infoPtr->hwndHeader, SW_SHOWNORMAL);
9560 LISTVIEW_UpdateScroll(infoPtr);
9561 /* send WM_MEASUREITEM notification */
9562 if (infoPtr->dwStyle & LVS_OWNERDRAWFIXED) notify_measureitem(infoPtr);
9565 OpenThemeData(hwnd, themeClass);
9567 /* initialize the icon sizes */
9568 set_icon_size(&infoPtr->iconSize, infoPtr->himlNormal, infoPtr->uView != LV_VIEW_ICON);
9569 set_icon_size(&infoPtr->iconStateSize, infoPtr->himlState, TRUE);
9570 return 0;
9573 /***
9574 * DESCRIPTION:
9575 * Destroys the listview control.
9577 * PARAMETER(S):
9578 * [I] infoPtr : valid pointer to the listview structure
9580 * RETURN:
9581 * Success: 0
9582 * Failure: -1
9584 static LRESULT LISTVIEW_Destroy(LISTVIEW_INFO *infoPtr)
9586 HTHEME theme = GetWindowTheme(infoPtr->hwndSelf);
9587 CloseThemeData(theme);
9589 /* delete all items */
9590 LISTVIEW_DeleteAllItems(infoPtr, TRUE);
9592 return 0;
9595 /***
9596 * DESCRIPTION:
9597 * Enables the listview control.
9599 * PARAMETER(S):
9600 * [I] infoPtr : valid pointer to the listview structure
9601 * [I] bEnable : specifies whether to enable or disable the window
9603 * RETURN:
9604 * SUCCESS : TRUE
9605 * FAILURE : FALSE
9607 static BOOL LISTVIEW_Enable(const LISTVIEW_INFO *infoPtr)
9609 if (infoPtr->dwStyle & LVS_OWNERDRAWFIXED)
9610 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
9611 return TRUE;
9614 /***
9615 * DESCRIPTION:
9616 * Erases the background of the listview control.
9618 * PARAMETER(S):
9619 * [I] infoPtr : valid pointer to the listview structure
9620 * [I] hdc : device context handle
9622 * RETURN:
9623 * SUCCESS : TRUE
9624 * FAILURE : FALSE
9626 static inline BOOL LISTVIEW_EraseBkgnd(const LISTVIEW_INFO *infoPtr, HDC hdc)
9628 RECT rc;
9630 TRACE("(hdc=%p)\n", hdc);
9632 if (!GetClipBox(hdc, &rc)) return FALSE;
9634 if (infoPtr->clrBk == CLR_NONE)
9636 if (infoPtr->dwLvExStyle & LVS_EX_TRANSPARENTBKGND)
9637 return SendMessageW(infoPtr->hwndNotify, WM_PRINTCLIENT,
9638 (WPARAM)hdc, PRF_ERASEBKGND);
9639 else
9640 return SendMessageW(infoPtr->hwndNotify, WM_ERASEBKGND, (WPARAM)hdc, 0);
9643 /* for double buffered controls we need to do this during refresh */
9644 if (infoPtr->dwLvExStyle & LVS_EX_DOUBLEBUFFER) return FALSE;
9646 return LISTVIEW_FillBkgnd(infoPtr, hdc, &rc);
9650 /***
9651 * DESCRIPTION:
9652 * Helper function for LISTVIEW_[HV]Scroll *only*.
9653 * Performs vertical/horizontal scrolling by a give amount.
9655 * PARAMETER(S):
9656 * [I] infoPtr : valid pointer to the listview structure
9657 * [I] dx : amount of horizontal scroll
9658 * [I] dy : amount of vertical scroll
9660 static void scroll_list(LISTVIEW_INFO *infoPtr, INT dx, INT dy)
9662 /* now we can scroll the list */
9663 ScrollWindowEx(infoPtr->hwndSelf, dx, dy, &infoPtr->rcList,
9664 &infoPtr->rcList, 0, 0, SW_ERASE | SW_INVALIDATE);
9665 /* if we have focus, adjust rect */
9666 OffsetRect(&infoPtr->rcFocus, dx, dy);
9667 UpdateWindow(infoPtr->hwndSelf);
9670 /***
9671 * DESCRIPTION:
9672 * Performs vertical scrolling.
9674 * PARAMETER(S):
9675 * [I] infoPtr : valid pointer to the listview structure
9676 * [I] nScrollCode : scroll code
9677 * [I] nScrollDiff : units to scroll in SB_INTERNAL mode, 0 otherwise
9678 * [I] hScrollWnd : scrollbar control window handle
9680 * RETURN:
9681 * Zero
9683 * NOTES:
9684 * SB_LINEUP/SB_LINEDOWN:
9685 * for LVS_ICON, LVS_SMALLICON is 37 by experiment
9686 * for LVS_REPORT is 1 line
9687 * for LVS_LIST cannot occur
9690 static LRESULT LISTVIEW_VScroll(LISTVIEW_INFO *infoPtr, INT nScrollCode,
9691 INT nScrollDiff)
9693 INT nOldScrollPos, nNewScrollPos;
9694 SCROLLINFO scrollInfo;
9695 BOOL is_an_icon;
9697 TRACE("(nScrollCode=%d(%s), nScrollDiff=%d)\n", nScrollCode,
9698 debugscrollcode(nScrollCode), nScrollDiff);
9700 if (infoPtr->hwndEdit) SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
9702 scrollInfo.cbSize = sizeof(SCROLLINFO);
9703 scrollInfo.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_TRACKPOS;
9705 is_an_icon = ((infoPtr->uView == LV_VIEW_ICON) || (infoPtr->uView == LV_VIEW_SMALLICON));
9707 if (!GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo)) return 1;
9709 nOldScrollPos = scrollInfo.nPos;
9710 switch (nScrollCode)
9712 case SB_INTERNAL:
9713 break;
9715 case SB_LINEUP:
9716 nScrollDiff = (is_an_icon) ? -LISTVIEW_SCROLL_ICON_LINE_SIZE : -1;
9717 break;
9719 case SB_LINEDOWN:
9720 nScrollDiff = (is_an_icon) ? LISTVIEW_SCROLL_ICON_LINE_SIZE : 1;
9721 break;
9723 case SB_PAGEUP:
9724 nScrollDiff = -scrollInfo.nPage;
9725 break;
9727 case SB_PAGEDOWN:
9728 nScrollDiff = scrollInfo.nPage;
9729 break;
9731 case SB_THUMBPOSITION:
9732 case SB_THUMBTRACK:
9733 nScrollDiff = scrollInfo.nTrackPos - scrollInfo.nPos;
9734 break;
9736 default:
9737 nScrollDiff = 0;
9740 /* quit right away if pos isn't changing */
9741 if (nScrollDiff == 0) return 0;
9743 /* calculate new position, and handle overflows */
9744 nNewScrollPos = scrollInfo.nPos + nScrollDiff;
9745 if (nScrollDiff > 0) {
9746 if (nNewScrollPos < nOldScrollPos ||
9747 nNewScrollPos > scrollInfo.nMax)
9748 nNewScrollPos = scrollInfo.nMax;
9749 } else {
9750 if (nNewScrollPos > nOldScrollPos ||
9751 nNewScrollPos < scrollInfo.nMin)
9752 nNewScrollPos = scrollInfo.nMin;
9755 /* set the new position, and reread in case it changed */
9756 scrollInfo.fMask = SIF_POS;
9757 scrollInfo.nPos = nNewScrollPos;
9758 nNewScrollPos = SetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo, TRUE);
9760 /* carry on only if it really changed */
9761 if (nNewScrollPos == nOldScrollPos) return 0;
9763 /* now adjust to client coordinates */
9764 nScrollDiff = nOldScrollPos - nNewScrollPos;
9765 if (infoPtr->uView == LV_VIEW_DETAILS) nScrollDiff *= infoPtr->nItemHeight;
9767 /* and scroll the window */
9768 scroll_list(infoPtr, 0, nScrollDiff);
9770 return 0;
9773 /***
9774 * DESCRIPTION:
9775 * Performs horizontal scrolling.
9777 * PARAMETER(S):
9778 * [I] infoPtr : valid pointer to the listview structure
9779 * [I] nScrollCode : scroll code
9780 * [I] nScrollDiff : units to scroll in SB_INTERNAL mode, 0 otherwise
9781 * [I] hScrollWnd : scrollbar control window handle
9783 * RETURN:
9784 * Zero
9786 * NOTES:
9787 * SB_LINELEFT/SB_LINERIGHT:
9788 * for LVS_ICON, LVS_SMALLICON 1 pixel
9789 * for LVS_REPORT is 1 pixel
9790 * for LVS_LIST is 1 column --> which is a 1 because the
9791 * scroll is based on columns not pixels
9794 static LRESULT LISTVIEW_HScroll(LISTVIEW_INFO *infoPtr, INT nScrollCode,
9795 INT nScrollDiff)
9797 INT nOldScrollPos, nNewScrollPos;
9798 SCROLLINFO scrollInfo;
9799 BOOL is_an_icon;
9801 TRACE("(nScrollCode=%d(%s), nScrollDiff=%d)\n", nScrollCode,
9802 debugscrollcode(nScrollCode), nScrollDiff);
9804 if (infoPtr->hwndEdit) SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
9806 scrollInfo.cbSize = sizeof(SCROLLINFO);
9807 scrollInfo.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_TRACKPOS;
9809 is_an_icon = ((infoPtr->uView == LV_VIEW_ICON) || (infoPtr->uView == LV_VIEW_SMALLICON));
9811 if (!GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo)) return 1;
9813 nOldScrollPos = scrollInfo.nPos;
9815 switch (nScrollCode)
9817 case SB_INTERNAL:
9818 break;
9820 case SB_LINELEFT:
9821 nScrollDiff = (is_an_icon) ? -LISTVIEW_SCROLL_ICON_LINE_SIZE : -1;
9822 break;
9824 case SB_LINERIGHT:
9825 nScrollDiff = (is_an_icon) ? LISTVIEW_SCROLL_ICON_LINE_SIZE : 1;
9826 break;
9828 case SB_PAGELEFT:
9829 nScrollDiff = -scrollInfo.nPage;
9830 break;
9832 case SB_PAGERIGHT:
9833 nScrollDiff = scrollInfo.nPage;
9834 break;
9836 case SB_THUMBPOSITION:
9837 case SB_THUMBTRACK:
9838 nScrollDiff = scrollInfo.nTrackPos - scrollInfo.nPos;
9839 break;
9841 default:
9842 nScrollDiff = 0;
9845 /* quit right away if pos isn't changing */
9846 if (nScrollDiff == 0) return 0;
9848 /* calculate new position, and handle overflows */
9849 nNewScrollPos = scrollInfo.nPos + nScrollDiff;
9850 if (nScrollDiff > 0) {
9851 if (nNewScrollPos < nOldScrollPos ||
9852 nNewScrollPos > scrollInfo.nMax)
9853 nNewScrollPos = scrollInfo.nMax;
9854 } else {
9855 if (nNewScrollPos > nOldScrollPos ||
9856 nNewScrollPos < scrollInfo.nMin)
9857 nNewScrollPos = scrollInfo.nMin;
9860 /* set the new position, and reread in case it changed */
9861 scrollInfo.fMask = SIF_POS;
9862 scrollInfo.nPos = nNewScrollPos;
9863 nNewScrollPos = SetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo, TRUE);
9865 /* carry on only if it really changed */
9866 if (nNewScrollPos == nOldScrollPos) return 0;
9868 if (infoPtr->hwndHeader) LISTVIEW_UpdateHeaderSize(infoPtr, nNewScrollPos);
9870 /* now adjust to client coordinates */
9871 nScrollDiff = nOldScrollPos - nNewScrollPos;
9872 if (infoPtr->uView == LV_VIEW_LIST) nScrollDiff *= infoPtr->nItemWidth;
9874 /* and scroll the window */
9875 scroll_list(infoPtr, nScrollDiff, 0);
9877 return 0;
9880 static LRESULT LISTVIEW_MouseWheel(LISTVIEW_INFO *infoPtr, INT wheelDelta)
9882 UINT pulScrollLines = 3;
9884 TRACE("(wheelDelta=%d)\n", wheelDelta);
9886 switch(infoPtr->uView)
9888 case LV_VIEW_ICON:
9889 case LV_VIEW_SMALLICON:
9891 * listview should be scrolled by a multiple of 37 dependently on its dimension or its visible item number
9892 * should be fixed in the future.
9894 LISTVIEW_VScroll(infoPtr, SB_INTERNAL, (wheelDelta > 0) ?
9895 -LISTVIEW_SCROLL_ICON_LINE_SIZE : LISTVIEW_SCROLL_ICON_LINE_SIZE);
9896 break;
9898 case LV_VIEW_DETAILS:
9899 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
9901 /* if scrolling changes direction, ignore left overs */
9902 if ((wheelDelta < 0 && infoPtr->cWheelRemainder < 0) ||
9903 (wheelDelta > 0 && infoPtr->cWheelRemainder > 0))
9904 infoPtr->cWheelRemainder += wheelDelta;
9905 else
9906 infoPtr->cWheelRemainder = wheelDelta;
9907 if (infoPtr->cWheelRemainder && pulScrollLines)
9909 int cLineScroll;
9910 pulScrollLines = min((UINT)LISTVIEW_GetCountPerColumn(infoPtr), pulScrollLines);
9911 cLineScroll = pulScrollLines * (float)infoPtr->cWheelRemainder / WHEEL_DELTA;
9912 infoPtr->cWheelRemainder -= WHEEL_DELTA * cLineScroll / (int)pulScrollLines;
9913 LISTVIEW_VScroll(infoPtr, SB_INTERNAL, -cLineScroll);
9915 break;
9917 case LV_VIEW_LIST:
9918 LISTVIEW_HScroll(infoPtr, (wheelDelta > 0) ? SB_LINELEFT : SB_LINERIGHT, 0);
9919 break;
9921 return 0;
9924 /***
9925 * DESCRIPTION:
9926 * ???
9928 * PARAMETER(S):
9929 * [I] infoPtr : valid pointer to the listview structure
9930 * [I] nVirtualKey : virtual key
9931 * [I] lKeyData : key data
9933 * RETURN:
9934 * Zero
9936 static LRESULT LISTVIEW_KeyDown(LISTVIEW_INFO *infoPtr, INT nVirtualKey, LONG lKeyData)
9938 HWND hwndSelf = infoPtr->hwndSelf;
9939 INT nItem = -1;
9940 NMLVKEYDOWN nmKeyDown;
9942 TRACE("(nVirtualKey=%d, lKeyData=%d)\n", nVirtualKey, lKeyData);
9944 /* send LVN_KEYDOWN notification */
9945 nmKeyDown.wVKey = nVirtualKey;
9946 nmKeyDown.flags = 0;
9947 notify_hdr(infoPtr, LVN_KEYDOWN, &nmKeyDown.hdr);
9948 if (!IsWindow(hwndSelf))
9949 return 0;
9951 switch (nVirtualKey)
9953 case VK_SPACE:
9954 nItem = infoPtr->nFocusedItem;
9955 if (infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES)
9956 toggle_checkbox_state(infoPtr, infoPtr->nFocusedItem);
9957 break;
9959 case VK_RETURN:
9960 if ((infoPtr->nItemCount > 0) && (infoPtr->nFocusedItem != -1))
9962 if (!notify(infoPtr, NM_RETURN)) return 0;
9963 if (!notify(infoPtr, LVN_ITEMACTIVATE)) return 0;
9965 break;
9967 case VK_HOME:
9968 if (infoPtr->nItemCount > 0)
9969 nItem = 0;
9970 break;
9972 case VK_END:
9973 if (infoPtr->nItemCount > 0)
9974 nItem = infoPtr->nItemCount - 1;
9975 break;
9977 case VK_LEFT:
9978 nItem = LISTVIEW_GetNextItem(infoPtr, infoPtr->nFocusedItem, LVNI_TOLEFT);
9979 break;
9981 case VK_UP:
9982 nItem = LISTVIEW_GetNextItem(infoPtr, infoPtr->nFocusedItem, LVNI_ABOVE);
9983 break;
9985 case VK_RIGHT:
9986 nItem = LISTVIEW_GetNextItem(infoPtr, infoPtr->nFocusedItem, LVNI_TORIGHT);
9987 break;
9989 case VK_DOWN:
9990 nItem = LISTVIEW_GetNextItem(infoPtr, infoPtr->nFocusedItem, LVNI_BELOW);
9991 break;
9993 case VK_PRIOR:
9994 if (infoPtr->uView == LV_VIEW_DETAILS)
9996 INT topidx = LISTVIEW_GetTopIndex(infoPtr);
9997 if (infoPtr->nFocusedItem == topidx)
9998 nItem = topidx - LISTVIEW_GetCountPerColumn(infoPtr) + 1;
9999 else
10000 nItem = topidx;
10002 else
10003 nItem = infoPtr->nFocusedItem - LISTVIEW_GetCountPerColumn(infoPtr)
10004 * LISTVIEW_GetCountPerRow(infoPtr);
10005 if(nItem < 0) nItem = 0;
10006 break;
10008 case VK_NEXT:
10009 if (infoPtr->uView == LV_VIEW_DETAILS)
10011 INT topidx = LISTVIEW_GetTopIndex(infoPtr);
10012 INT cnt = LISTVIEW_GetCountPerColumn(infoPtr);
10013 if (infoPtr->nFocusedItem == topidx + cnt - 1)
10014 nItem = infoPtr->nFocusedItem + cnt - 1;
10015 else
10016 nItem = topidx + cnt - 1;
10018 else
10019 nItem = infoPtr->nFocusedItem + LISTVIEW_GetCountPerColumn(infoPtr)
10020 * LISTVIEW_GetCountPerRow(infoPtr);
10021 if(nItem >= infoPtr->nItemCount) nItem = infoPtr->nItemCount - 1;
10022 break;
10025 if ((nItem != -1) && (nItem != infoPtr->nFocusedItem || nVirtualKey == VK_SPACE))
10026 LISTVIEW_KeySelection(infoPtr, nItem, nVirtualKey == VK_SPACE);
10028 return 0;
10031 /***
10032 * DESCRIPTION:
10033 * Kills the focus.
10035 * PARAMETER(S):
10036 * [I] infoPtr : valid pointer to the listview structure
10038 * RETURN:
10039 * Zero
10041 static LRESULT LISTVIEW_KillFocus(LISTVIEW_INFO *infoPtr)
10043 TRACE("()\n");
10045 /* drop any left over scroll amount */
10046 infoPtr->cWheelRemainder = 0;
10048 /* if we did not have the focus, there's nothing more to do */
10049 if (!infoPtr->bFocus) return 0;
10051 /* send NM_KILLFOCUS notification */
10052 if (!notify(infoPtr, NM_KILLFOCUS)) return 0;
10054 /* if we have a focus rectangle, get rid of it */
10055 LISTVIEW_ShowFocusRect(infoPtr, FALSE);
10057 /* if have a marquee selection, stop it */
10058 if (infoPtr->bMarqueeSelect)
10060 /* Remove the marquee rectangle and release our mouse capture */
10061 LISTVIEW_InvalidateRect(infoPtr, &infoPtr->marqueeRect);
10062 ReleaseCapture();
10064 SetRect(&infoPtr->marqueeRect, 0, 0, 0, 0);
10066 infoPtr->bMarqueeSelect = FALSE;
10067 infoPtr->bScrolling = FALSE;
10068 KillTimer(infoPtr->hwndSelf, (UINT_PTR) infoPtr);
10071 /* set window focus flag */
10072 infoPtr->bFocus = FALSE;
10074 /* invalidate the selected items before resetting focus flag */
10075 LISTVIEW_InvalidateSelectedItems(infoPtr);
10077 return 0;
10080 /***
10081 * DESCRIPTION:
10082 * Processes double click messages (left mouse button).
10084 * PARAMETER(S):
10085 * [I] infoPtr : valid pointer to the listview structure
10086 * [I] wKey : key flag
10087 * [I] x,y : mouse coordinate
10089 * RETURN:
10090 * Zero
10092 static LRESULT LISTVIEW_LButtonDblClk(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
10094 LVHITTESTINFO htInfo;
10096 TRACE("(key=%hu, X=%u, Y=%u)\n", wKey, x, y);
10098 /* Cancel the item edition if any */
10099 if (infoPtr->itemEdit.fEnabled)
10101 KillTimer(infoPtr->hwndSelf, (UINT_PTR)&infoPtr->itemEdit);
10102 infoPtr->itemEdit.fEnabled = FALSE;
10105 /* send NM_RELEASEDCAPTURE notification */
10106 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
10108 htInfo.pt.x = x;
10109 htInfo.pt.y = y;
10111 /* send NM_DBLCLK notification */
10112 LISTVIEW_HitTest(infoPtr, &htInfo, TRUE, FALSE);
10113 if (!notify_click(infoPtr, NM_DBLCLK, &htInfo)) return 0;
10115 /* To send the LVN_ITEMACTIVATE, it must be on an Item */
10116 if(htInfo.iItem != -1) notify_itemactivate(infoPtr,&htInfo);
10118 return 0;
10121 static LRESULT LISTVIEW_TrackMouse(const LISTVIEW_INFO *infoPtr, POINT pt)
10123 MSG msg;
10124 RECT r;
10126 r.top = r.bottom = pt.y;
10127 r.left = r.right = pt.x;
10129 InflateRect(&r, GetSystemMetrics(SM_CXDRAG), GetSystemMetrics(SM_CYDRAG));
10131 SetCapture(infoPtr->hwndSelf);
10133 while (1)
10135 if (PeekMessageW(&msg, 0, 0, 0, PM_REMOVE | PM_NOYIELD))
10137 if (msg.message == WM_MOUSEMOVE)
10139 pt.x = (short)LOWORD(msg.lParam);
10140 pt.y = (short)HIWORD(msg.lParam);
10141 if (PtInRect(&r, pt))
10142 continue;
10143 else
10145 ReleaseCapture();
10146 return 1;
10149 else if (msg.message >= WM_LBUTTONDOWN &&
10150 msg.message <= WM_RBUTTONDBLCLK)
10152 break;
10155 DispatchMessageW(&msg);
10158 if (GetCapture() != infoPtr->hwndSelf)
10159 return 0;
10162 ReleaseCapture();
10163 return 0;
10167 /***
10168 * DESCRIPTION:
10169 * Processes mouse down messages (left mouse button).
10171 * PARAMETERS:
10172 * infoPtr [I ] valid pointer to the listview structure
10173 * wKey [I ] key flag
10174 * x,y [I ] mouse coordinate
10176 * RETURN:
10177 * Zero
10179 static LRESULT LISTVIEW_LButtonDown(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
10181 LVHITTESTINFO lvHitTestInfo;
10182 static BOOL bGroupSelect = TRUE;
10183 POINT pt = { x, y };
10184 INT nItem;
10186 TRACE("(key=%hu, X=%u, Y=%u)\n", wKey, x, y);
10188 /* send NM_RELEASEDCAPTURE notification */
10189 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
10191 /* set left button down flag and record the click position */
10192 infoPtr->bLButtonDown = TRUE;
10193 infoPtr->ptClickPos = pt;
10194 infoPtr->bDragging = FALSE;
10195 infoPtr->bMarqueeSelect = FALSE;
10196 infoPtr->bScrolling = FALSE;
10198 lvHitTestInfo.pt.x = x;
10199 lvHitTestInfo.pt.y = y;
10201 nItem = LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, TRUE);
10202 TRACE("at %s, nItem=%d\n", wine_dbgstr_point(&pt), nItem);
10203 if ((nItem >= 0) && (nItem < infoPtr->nItemCount))
10205 if ((infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES) && (lvHitTestInfo.flags & LVHT_ONITEMSTATEICON))
10207 toggle_checkbox_state(infoPtr, nItem);
10208 return 0;
10211 if (infoPtr->dwStyle & LVS_SINGLESEL)
10213 if (LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED))
10214 infoPtr->nEditLabelItem = nItem;
10215 else
10216 LISTVIEW_SetSelection(infoPtr, nItem);
10218 else
10220 if ((wKey & MK_CONTROL) && (wKey & MK_SHIFT))
10222 if (bGroupSelect)
10224 if (!LISTVIEW_AddGroupSelection(infoPtr, nItem)) return 0;
10225 LISTVIEW_SetItemFocus(infoPtr, nItem);
10226 infoPtr->nSelectionMark = nItem;
10228 else
10230 LVITEMW item;
10232 item.state = LVIS_SELECTED | LVIS_FOCUSED;
10233 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
10235 LISTVIEW_SetItemState(infoPtr,nItem,&item);
10236 infoPtr->nSelectionMark = nItem;
10239 else if (wKey & MK_CONTROL)
10241 LVITEMW item;
10243 bGroupSelect = (LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED) == 0);
10245 item.state = (bGroupSelect ? LVIS_SELECTED : 0) | LVIS_FOCUSED;
10246 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
10247 LISTVIEW_SetItemState(infoPtr, nItem, &item);
10248 infoPtr->nSelectionMark = nItem;
10250 else if (wKey & MK_SHIFT)
10252 LISTVIEW_SetGroupSelection(infoPtr, nItem);
10254 else
10256 if (LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED))
10258 infoPtr->nEditLabelItem = nItem;
10259 infoPtr->nLButtonDownItem = nItem;
10261 LISTVIEW_SetItemFocus(infoPtr, nItem);
10263 else
10264 /* set selection (clears other pre-existing selections) */
10265 LISTVIEW_SetSelection(infoPtr, nItem);
10269 if (!infoPtr->bFocus)
10270 SetFocus(infoPtr->hwndSelf);
10272 if (infoPtr->dwLvExStyle & LVS_EX_ONECLICKACTIVATE)
10273 if(lvHitTestInfo.iItem != -1) notify_itemactivate(infoPtr,&lvHitTestInfo);
10275 else
10277 if (!infoPtr->bFocus)
10278 SetFocus(infoPtr->hwndSelf);
10280 /* remove all selections */
10281 if (!(wKey & MK_CONTROL) && !(wKey & MK_SHIFT))
10282 LISTVIEW_DeselectAll(infoPtr);
10283 ReleaseCapture();
10286 return 0;
10289 /***
10290 * DESCRIPTION:
10291 * Processes mouse up messages (left mouse button).
10293 * PARAMETERS:
10294 * infoPtr [I ] valid pointer to the listview structure
10295 * wKey [I ] key flag
10296 * x,y [I ] mouse coordinate
10298 * RETURN:
10299 * Zero
10301 static LRESULT LISTVIEW_LButtonUp(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
10303 LVHITTESTINFO lvHitTestInfo;
10305 TRACE("(key=%hu, X=%u, Y=%u)\n", wKey, x, y);
10307 if (!infoPtr->bLButtonDown) return 0;
10309 lvHitTestInfo.pt.x = x;
10310 lvHitTestInfo.pt.y = y;
10312 /* send NM_CLICK notification */
10313 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
10314 if (!notify_click(infoPtr, NM_CLICK, &lvHitTestInfo)) return 0;
10316 /* set left button flag */
10317 infoPtr->bLButtonDown = FALSE;
10319 /* set a single selection, reset others */
10320 if(lvHitTestInfo.iItem == infoPtr->nLButtonDownItem && lvHitTestInfo.iItem != -1)
10321 LISTVIEW_SetSelection(infoPtr, infoPtr->nLButtonDownItem);
10322 infoPtr->nLButtonDownItem = -1;
10324 if (infoPtr->bDragging || infoPtr->bMarqueeSelect)
10326 /* Remove the marquee rectangle and release our mouse capture */
10327 if (infoPtr->bMarqueeSelect)
10329 LISTVIEW_InvalidateRect(infoPtr, &infoPtr->marqueeDrawRect);
10330 ReleaseCapture();
10333 SetRect(&infoPtr->marqueeRect, 0, 0, 0, 0);
10334 SetRect(&infoPtr->marqueeDrawRect, 0, 0, 0, 0);
10336 infoPtr->bDragging = FALSE;
10337 infoPtr->bMarqueeSelect = FALSE;
10338 infoPtr->bScrolling = FALSE;
10340 KillTimer(infoPtr->hwndSelf, (UINT_PTR) infoPtr);
10341 return 0;
10344 /* if we clicked on a selected item, edit the label */
10345 if(lvHitTestInfo.iItem == infoPtr->nEditLabelItem && (lvHitTestInfo.flags & LVHT_ONITEMLABEL))
10347 /* we want to make sure the user doesn't want to do a double click. So we will
10348 * delay the edit. WM_LBUTTONDBLCLICK will cancel the timer
10350 infoPtr->itemEdit.fEnabled = TRUE;
10351 infoPtr->itemEdit.iItem = lvHitTestInfo.iItem;
10352 SetTimer(infoPtr->hwndSelf,
10353 (UINT_PTR)&infoPtr->itemEdit,
10354 GetDoubleClickTime(),
10355 LISTVIEW_DelayedEditItem);
10358 return 0;
10361 /***
10362 * DESCRIPTION:
10363 * Destroys the listview control (called after WM_DESTROY).
10365 * PARAMETER(S):
10366 * [I] infoPtr : valid pointer to the listview structure
10368 * RETURN:
10369 * Zero
10371 static LRESULT LISTVIEW_NCDestroy(LISTVIEW_INFO *infoPtr)
10373 INT i;
10375 TRACE("()\n");
10377 /* destroy data structure */
10378 DPA_Destroy(infoPtr->hdpaItems);
10379 DPA_Destroy(infoPtr->hdpaItemIds);
10380 DPA_Destroy(infoPtr->hdpaPosX);
10381 DPA_Destroy(infoPtr->hdpaPosY);
10382 /* columns */
10383 for (i = 0; i < DPA_GetPtrCount(infoPtr->hdpaColumns); i++)
10384 Free(DPA_GetPtr(infoPtr->hdpaColumns, i));
10385 DPA_Destroy(infoPtr->hdpaColumns);
10386 ranges_destroy(infoPtr->selectionRanges);
10388 /* destroy image lists */
10389 if (!(infoPtr->dwStyle & LVS_SHAREIMAGELISTS))
10391 ImageList_Destroy(infoPtr->himlNormal);
10392 ImageList_Destroy(infoPtr->himlSmall);
10393 ImageList_Destroy(infoPtr->himlState);
10396 /* destroy font, bkgnd brush */
10397 infoPtr->hFont = 0;
10398 if (infoPtr->hDefaultFont) DeleteObject(infoPtr->hDefaultFont);
10399 if (infoPtr->clrBk != CLR_NONE) DeleteObject(infoPtr->hBkBrush);
10401 SetWindowLongPtrW(infoPtr->hwndSelf, 0, 0);
10403 /* free listview info pointer*/
10404 Free(infoPtr);
10406 return 0;
10409 /***
10410 * DESCRIPTION:
10411 * Handles notifications.
10413 * PARAMETER(S):
10414 * [I] infoPtr : valid pointer to the listview structure
10415 * [I] lpnmhdr : notification information
10417 * RETURN:
10418 * Zero
10420 static LRESULT LISTVIEW_Notify(LISTVIEW_INFO *infoPtr, NMHDR *lpnmhdr)
10422 NMHEADERW *lpnmh;
10424 TRACE("(lpnmhdr=%p)\n", lpnmhdr);
10426 if (!lpnmhdr || lpnmhdr->hwndFrom != infoPtr->hwndHeader) return 0;
10428 /* remember: HDN_LAST < HDN_FIRST */
10429 if (lpnmhdr->code > HDN_FIRST || lpnmhdr->code < HDN_LAST) return 0;
10430 lpnmh = (NMHEADERW *)lpnmhdr;
10432 if (lpnmh->iItem < 0 || lpnmh->iItem >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return 0;
10434 switch (lpnmhdr->code)
10436 case HDN_TRACKW:
10437 case HDN_TRACKA:
10439 COLUMN_INFO *lpColumnInfo;
10440 POINT ptOrigin;
10441 INT x;
10443 if (!lpnmh->pitem || !(lpnmh->pitem->mask & HDI_WIDTH))
10444 break;
10446 /* remove the old line (if any) */
10447 LISTVIEW_DrawTrackLine(infoPtr);
10449 /* compute & draw the new line */
10450 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, lpnmh->iItem);
10451 x = lpColumnInfo->rcHeader.left + lpnmh->pitem->cxy;
10452 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
10453 infoPtr->xTrackLine = x + ptOrigin.x;
10454 LISTVIEW_DrawTrackLine(infoPtr);
10455 return notify_forward_header(infoPtr, lpnmh);
10458 case HDN_ENDTRACKA:
10459 case HDN_ENDTRACKW:
10460 /* remove the track line (if any) */
10461 LISTVIEW_DrawTrackLine(infoPtr);
10462 infoPtr->xTrackLine = -1;
10463 return notify_forward_header(infoPtr, lpnmh);
10465 case HDN_BEGINDRAG:
10466 if ((infoPtr->dwLvExStyle & LVS_EX_HEADERDRAGDROP) == 0) return 1;
10467 return notify_forward_header(infoPtr, lpnmh);
10469 case HDN_ENDDRAG:
10470 infoPtr->colRectsDirty = TRUE;
10471 LISTVIEW_InvalidateList(infoPtr);
10472 return notify_forward_header(infoPtr, lpnmh);
10474 case HDN_ITEMCHANGEDW:
10475 case HDN_ITEMCHANGEDA:
10477 COLUMN_INFO *lpColumnInfo;
10478 HDITEMW hdi;
10479 INT dx, cxy;
10481 if (!lpnmh->pitem || !(lpnmh->pitem->mask & HDI_WIDTH))
10483 hdi.mask = HDI_WIDTH;
10484 if (!SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, lpnmh->iItem, (LPARAM)&hdi)) return 0;
10485 cxy = hdi.cxy;
10487 else
10488 cxy = lpnmh->pitem->cxy;
10490 /* determine how much we change since the last know position */
10491 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, lpnmh->iItem);
10492 dx = cxy - (lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left);
10493 if (dx != 0)
10495 lpColumnInfo->rcHeader.right += dx;
10497 hdi.mask = HDI_ORDER;
10498 SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, lpnmh->iItem, (LPARAM)&hdi);
10500 /* not the rightmost one */
10501 if (hdi.iOrder + 1 < DPA_GetPtrCount(infoPtr->hdpaColumns))
10503 INT nIndex = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX,
10504 hdi.iOrder + 1, 0);
10505 LISTVIEW_ScrollColumns(infoPtr, nIndex, dx);
10507 else
10509 /* only needs to update the scrolls */
10510 infoPtr->nItemWidth += dx;
10511 LISTVIEW_UpdateScroll(infoPtr);
10513 LISTVIEW_UpdateItemSize(infoPtr);
10514 if (infoPtr->uView == LV_VIEW_DETAILS && is_redrawing(infoPtr))
10516 POINT ptOrigin;
10517 RECT rcCol = lpColumnInfo->rcHeader;
10519 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
10520 OffsetRect(&rcCol, ptOrigin.x, 0);
10522 rcCol.top = infoPtr->rcList.top;
10523 rcCol.bottom = infoPtr->rcList.bottom;
10525 /* resizing left-aligned columns leaves most of the left side untouched */
10526 if ((lpColumnInfo->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_LEFT)
10528 INT nMaxDirty = infoPtr->nEllipsisWidth + infoPtr->ntmMaxCharWidth;
10529 if (dx > 0)
10530 nMaxDirty += dx;
10531 rcCol.left = max (rcCol.left, rcCol.right - nMaxDirty);
10534 /* when shrinking the last column clear the now unused field */
10535 if (hdi.iOrder == DPA_GetPtrCount(infoPtr->hdpaColumns) - 1)
10537 RECT right;
10539 rcCol.right -= dx;
10541 /* deal with right from rightmost column area */
10542 right.left = rcCol.right;
10543 right.top = rcCol.top;
10544 right.bottom = rcCol.bottom;
10545 right.right = infoPtr->rcList.right;
10547 LISTVIEW_InvalidateRect(infoPtr, &right);
10550 LISTVIEW_InvalidateRect(infoPtr, &rcCol);
10553 break;
10556 case HDN_ITEMCLICKW:
10557 case HDN_ITEMCLICKA:
10559 /* Handle sorting by Header Column */
10560 NMLISTVIEW nmlv;
10562 ZeroMemory(&nmlv, sizeof(NMLISTVIEW));
10563 nmlv.iItem = -1;
10564 nmlv.iSubItem = lpnmh->iItem;
10565 notify_listview(infoPtr, LVN_COLUMNCLICK, &nmlv);
10566 return notify_forward_header(infoPtr, lpnmh);
10569 case HDN_DIVIDERDBLCLICKW:
10570 case HDN_DIVIDERDBLCLICKA:
10571 /* FIXME: for LVS_EX_HEADERINALLVIEWS and not LV_VIEW_DETAILS
10572 we should use LVSCW_AUTOSIZE_USEHEADER, helper rework or
10573 split needed for that */
10574 LISTVIEW_SetColumnWidth(infoPtr, lpnmh->iItem, LVSCW_AUTOSIZE);
10575 return notify_forward_header(infoPtr, lpnmh);
10577 return 0;
10580 /***
10581 * DESCRIPTION:
10582 * Paint non-client area of control.
10584 * PARAMETER(S):
10585 * [I] infoPtr : valid pointer to the listview structureof the sender
10586 * [I] region : update region
10588 * RETURN:
10589 * TRUE - frame was painted
10590 * FALSE - call default window proc
10592 static BOOL LISTVIEW_NCPaint(const LISTVIEW_INFO *infoPtr, HRGN region)
10594 HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
10595 HDC dc;
10596 RECT r;
10597 HRGN cliprgn;
10598 int cxEdge = GetSystemMetrics (SM_CXEDGE),
10599 cyEdge = GetSystemMetrics (SM_CYEDGE);
10601 if (!theme)
10602 return DefWindowProcW (infoPtr->hwndSelf, WM_NCPAINT, (WPARAM)region, 0);
10604 GetWindowRect(infoPtr->hwndSelf, &r);
10606 cliprgn = CreateRectRgn (r.left + cxEdge, r.top + cyEdge,
10607 r.right - cxEdge, r.bottom - cyEdge);
10608 if (region != (HRGN)1)
10609 CombineRgn (cliprgn, cliprgn, region, RGN_AND);
10610 OffsetRect(&r, -r.left, -r.top);
10612 dc = GetDCEx(infoPtr->hwndSelf, region, DCX_WINDOW|DCX_INTERSECTRGN);
10613 OffsetRect(&r, -r.left, -r.top);
10615 if (IsThemeBackgroundPartiallyTransparent (theme, 0, 0))
10616 DrawThemeParentBackground(infoPtr->hwndSelf, dc, &r);
10617 DrawThemeBackground (theme, dc, 0, 0, &r, 0);
10618 ReleaseDC(infoPtr->hwndSelf, dc);
10620 /* Call default proc to get the scrollbars etc. painted */
10621 DefWindowProcW (infoPtr->hwndSelf, WM_NCPAINT, (WPARAM)cliprgn, 0);
10623 return FALSE;
10626 /***
10627 * DESCRIPTION:
10628 * Determines the type of structure to use.
10630 * PARAMETER(S):
10631 * [I] infoPtr : valid pointer to the listview structureof the sender
10632 * [I] hwndFrom : listview window handle
10633 * [I] nCommand : command specifying the nature of the WM_NOTIFYFORMAT
10635 * RETURN:
10636 * Zero
10638 static LRESULT LISTVIEW_NotifyFormat(LISTVIEW_INFO *infoPtr, HWND hwndFrom, INT nCommand)
10640 TRACE("(hwndFrom=%p, nCommand=%d)\n", hwndFrom, nCommand);
10642 if (nCommand == NF_REQUERY)
10643 infoPtr->notifyFormat = SendMessageW(infoPtr->hwndNotify, WM_NOTIFYFORMAT, (WPARAM)infoPtr->hwndSelf, NF_QUERY);
10645 return infoPtr->notifyFormat;
10648 /***
10649 * DESCRIPTION:
10650 * Paints/Repaints the listview control. Internal use.
10652 * PARAMETER(S):
10653 * [I] infoPtr : valid pointer to the listview structure
10654 * [I] hdc : device context handle
10656 * RETURN:
10657 * Zero
10659 static LRESULT LISTVIEW_Paint(LISTVIEW_INFO *infoPtr, HDC hdc)
10661 TRACE("(hdc=%p)\n", hdc);
10663 if (infoPtr->bNoItemMetrics && infoPtr->nItemCount)
10665 infoPtr->bNoItemMetrics = FALSE;
10666 LISTVIEW_UpdateItemSize(infoPtr);
10667 if (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON)
10668 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
10669 LISTVIEW_UpdateScroll(infoPtr);
10672 if (infoPtr->hwndHeader) UpdateWindow(infoPtr->hwndHeader);
10674 if (hdc)
10675 LISTVIEW_Refresh(infoPtr, hdc, NULL);
10676 else
10678 PAINTSTRUCT ps;
10680 hdc = BeginPaint(infoPtr->hwndSelf, &ps);
10681 if (!hdc) return 1;
10682 LISTVIEW_Refresh(infoPtr, hdc, ps.fErase ? &ps.rcPaint : NULL);
10683 EndPaint(infoPtr->hwndSelf, &ps);
10686 return 0;
10689 /***
10690 * DESCRIPTION:
10691 * Paints/Repaints the listview control, WM_PAINT handler.
10693 * PARAMETER(S):
10694 * [I] infoPtr : valid pointer to the listview structure
10695 * [I] hdc : device context handle
10697 * RETURN:
10698 * Zero
10700 static inline LRESULT LISTVIEW_WMPaint(LISTVIEW_INFO *infoPtr, HDC hdc)
10702 TRACE("(hdc=%p)\n", hdc);
10704 if (!is_redrawing(infoPtr))
10705 return DefWindowProcW (infoPtr->hwndSelf, WM_PAINT, (WPARAM)hdc, 0);
10707 return LISTVIEW_Paint(infoPtr, hdc);
10710 /***
10711 * DESCRIPTION:
10712 * Paints/Repaints the listview control.
10714 * PARAMETER(S):
10715 * [I] infoPtr : valid pointer to the listview structure
10716 * [I] hdc : device context handle
10717 * [I] options : drawing options
10719 * RETURN:
10720 * Zero
10722 static LRESULT LISTVIEW_PrintClient(LISTVIEW_INFO *infoPtr, HDC hdc, DWORD options)
10724 FIXME("Partial Stub: (hdc=%p options=0x%08x)\n", hdc, options);
10726 if ((options & PRF_CHECKVISIBLE) && !IsWindowVisible(infoPtr->hwndSelf))
10727 return 0;
10729 if (options & PRF_ERASEBKGND)
10730 LISTVIEW_EraseBkgnd(infoPtr, hdc);
10732 if (options & PRF_CLIENT)
10733 LISTVIEW_Paint(infoPtr, hdc);
10735 return 0;
10739 /***
10740 * DESCRIPTION:
10741 * Processes double click messages (right mouse button).
10743 * PARAMETER(S):
10744 * [I] infoPtr : valid pointer to the listview structure
10745 * [I] wKey : key flag
10746 * [I] x,y : mouse coordinate
10748 * RETURN:
10749 * Zero
10751 static LRESULT LISTVIEW_RButtonDblClk(const LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
10753 LVHITTESTINFO lvHitTestInfo;
10755 TRACE("(key=%hu,X=%u,Y=%u)\n", wKey, x, y);
10757 /* send NM_RELEASEDCAPTURE notification */
10758 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
10760 /* send NM_RDBLCLK notification */
10761 lvHitTestInfo.pt.x = x;
10762 lvHitTestInfo.pt.y = y;
10763 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
10764 notify_click(infoPtr, NM_RDBLCLK, &lvHitTestInfo);
10766 return 0;
10769 /***
10770 * DESCRIPTION:
10771 * Processes WM_RBUTTONDOWN message and corresponding drag operation.
10773 * PARAMETER(S):
10774 * [I] infoPtr : valid pointer to the listview structure
10775 * [I] wKey : key flag
10776 * [I] x, y : mouse coordinate
10778 * RETURN:
10779 * Zero
10781 static LRESULT LISTVIEW_RButtonDown(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
10783 LVHITTESTINFO ht;
10784 INT item;
10786 TRACE("(key=%hu, x=%d, y=%d)\n", wKey, x, y);
10788 /* send NM_RELEASEDCAPTURE notification */
10789 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
10791 /* determine the index of the selected item */
10792 ht.pt.x = x;
10793 ht.pt.y = y;
10794 item = LISTVIEW_HitTest(infoPtr, &ht, TRUE, TRUE);
10796 /* make sure the listview control window has the focus */
10797 if (!infoPtr->bFocus) SetFocus(infoPtr->hwndSelf);
10799 if ((item >= 0) && (item < infoPtr->nItemCount))
10801 LISTVIEW_SetItemFocus(infoPtr, item);
10802 if (!((wKey & MK_SHIFT) || (wKey & MK_CONTROL)) &&
10803 !LISTVIEW_GetItemState(infoPtr, item, LVIS_SELECTED))
10804 LISTVIEW_SetSelection(infoPtr, item);
10806 else
10807 LISTVIEW_DeselectAll(infoPtr);
10809 if (LISTVIEW_TrackMouse(infoPtr, ht.pt))
10811 if (ht.iItem != -1)
10813 NMLISTVIEW nmlv;
10815 memset(&nmlv, 0, sizeof(nmlv));
10816 nmlv.iItem = ht.iItem;
10817 nmlv.ptAction = ht.pt;
10819 notify_listview(infoPtr, LVN_BEGINRDRAG, &nmlv);
10822 else
10824 SetFocus(infoPtr->hwndSelf);
10826 ht.pt.x = x;
10827 ht.pt.y = y;
10828 LISTVIEW_HitTest(infoPtr, &ht, TRUE, FALSE);
10830 if (notify_click(infoPtr, NM_RCLICK, &ht))
10832 /* Send a WM_CONTEXTMENU message in response to the WM_RBUTTONUP */
10833 SendMessageW(infoPtr->hwndSelf, WM_CONTEXTMENU,
10834 (WPARAM)infoPtr->hwndSelf, (LPARAM)GetMessagePos());
10838 return 0;
10841 /***
10842 * DESCRIPTION:
10843 * Sets the cursor.
10845 * PARAMETER(S):
10846 * [I] infoPtr : valid pointer to the listview structure
10847 * [I] hwnd : window handle of window containing the cursor
10848 * [I] nHittest : hit-test code
10849 * [I] wMouseMsg : ideintifier of the mouse message
10851 * RETURN:
10852 * TRUE if cursor is set
10853 * FALSE otherwise
10855 static BOOL LISTVIEW_SetCursor(const LISTVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
10857 LVHITTESTINFO lvHitTestInfo;
10859 if (!LISTVIEW_IsHotTracking(infoPtr)) goto forward;
10861 if (!infoPtr->hHotCursor) goto forward;
10863 GetCursorPos(&lvHitTestInfo.pt);
10864 if (LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, FALSE, FALSE) < 0) goto forward;
10866 SetCursor(infoPtr->hHotCursor);
10868 return TRUE;
10870 forward:
10872 return DefWindowProcW(infoPtr->hwndSelf, WM_SETCURSOR, wParam, lParam);
10875 /***
10876 * DESCRIPTION:
10877 * Sets the focus.
10879 * PARAMETER(S):
10880 * [I] infoPtr : valid pointer to the listview structure
10881 * [I] hwndLoseFocus : handle of previously focused window
10883 * RETURN:
10884 * Zero
10886 static LRESULT LISTVIEW_SetFocus(LISTVIEW_INFO *infoPtr, HWND hwndLoseFocus)
10888 TRACE("(hwndLoseFocus=%p)\n", hwndLoseFocus);
10890 /* if we have the focus already, there's nothing to do */
10891 if (infoPtr->bFocus) return 0;
10893 /* send NM_SETFOCUS notification */
10894 if (!notify(infoPtr, NM_SETFOCUS)) return 0;
10896 /* set window focus flag */
10897 infoPtr->bFocus = TRUE;
10899 /* put the focus rect back on */
10900 LISTVIEW_ShowFocusRect(infoPtr, TRUE);
10902 /* redraw all visible selected items */
10903 LISTVIEW_InvalidateSelectedItems(infoPtr);
10905 return 0;
10908 /***
10909 * DESCRIPTION:
10910 * Sets the font.
10912 * PARAMETER(S):
10913 * [I] infoPtr : valid pointer to the listview structure
10914 * [I] fRedraw : font handle
10915 * [I] fRedraw : redraw flag
10917 * RETURN:
10918 * Zero
10920 static LRESULT LISTVIEW_SetFont(LISTVIEW_INFO *infoPtr, HFONT hFont, WORD fRedraw)
10922 HFONT oldFont = infoPtr->hFont;
10923 INT oldHeight = infoPtr->nItemHeight;
10925 TRACE("(hfont=%p,redraw=%hu)\n", hFont, fRedraw);
10927 infoPtr->hFont = hFont ? hFont : infoPtr->hDefaultFont;
10928 if (infoPtr->hFont == oldFont) return 0;
10930 LISTVIEW_SaveTextMetrics(infoPtr);
10932 infoPtr->nItemHeight = LISTVIEW_CalculateItemHeight(infoPtr);
10934 if (infoPtr->uView == LV_VIEW_DETAILS)
10936 SendMessageW(infoPtr->hwndHeader, WM_SETFONT, (WPARAM)hFont, MAKELPARAM(fRedraw, 0));
10937 LISTVIEW_UpdateSize(infoPtr);
10938 LISTVIEW_UpdateScroll(infoPtr);
10940 else if (infoPtr->nItemHeight != oldHeight)
10941 LISTVIEW_UpdateScroll(infoPtr);
10943 if (fRedraw) LISTVIEW_InvalidateList(infoPtr);
10945 return 0;
10948 /***
10949 * DESCRIPTION:
10950 * Message handling for WM_SETREDRAW.
10951 * For the Listview, it invalidates the entire window (the doc specifies otherwise)
10953 * PARAMETER(S):
10954 * [I] infoPtr : valid pointer to the listview structure
10955 * [I] bRedraw: state of redraw flag
10957 * RETURN:
10958 * DefWinProc return value
10960 static LRESULT LISTVIEW_SetRedraw(LISTVIEW_INFO *infoPtr, BOOL bRedraw)
10962 TRACE("infoPtr->bRedraw=%d, bRedraw=%d\n", infoPtr->bRedraw, bRedraw);
10964 /* we cannot use straight equality here because _any_ non-zero value is TRUE */
10965 if ((infoPtr->bRedraw && bRedraw) || (!infoPtr->bRedraw && !bRedraw)) return 0;
10967 infoPtr->bRedraw = bRedraw;
10969 if(!bRedraw) return 0;
10971 if (is_autoarrange(infoPtr))
10972 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
10973 LISTVIEW_UpdateScroll(infoPtr);
10975 /* despite what the WM_SETREDRAW docs says, apps expect us
10976 * to invalidate the listview here... stupid! */
10977 LISTVIEW_InvalidateList(infoPtr);
10979 return 0;
10982 /***
10983 * DESCRIPTION:
10984 * Resizes the listview control. This function processes WM_SIZE
10985 * messages. At this time, the width and height are not used.
10987 * PARAMETER(S):
10988 * [I] infoPtr : valid pointer to the listview structure
10989 * [I] Width : new width
10990 * [I] Height : new height
10992 * RETURN:
10993 * Zero
10995 static LRESULT LISTVIEW_Size(LISTVIEW_INFO *infoPtr, int Width, int Height)
10997 RECT rcOld = infoPtr->rcList;
10999 TRACE("(width=%d, height=%d)\n", Width, Height);
11001 LISTVIEW_UpdateSize(infoPtr);
11002 if (EqualRect(&rcOld, &infoPtr->rcList)) return 0;
11004 /* do not bother with display related stuff if we're not redrawing */
11005 if (!is_redrawing(infoPtr)) return 0;
11007 if (is_autoarrange(infoPtr))
11008 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
11010 LISTVIEW_UpdateScroll(infoPtr);
11012 /* refresh all only for lists whose height changed significantly */
11013 if ((infoPtr->uView == LV_VIEW_LIST) &&
11014 (rcOld.bottom - rcOld.top) / infoPtr->nItemHeight !=
11015 (infoPtr->rcList.bottom - infoPtr->rcList.top) / infoPtr->nItemHeight)
11016 LISTVIEW_InvalidateList(infoPtr);
11018 return 0;
11021 /***
11022 * DESCRIPTION:
11023 * Sets the size information.
11025 * PARAMETER(S):
11026 * [I] infoPtr : valid pointer to the listview structure
11028 * RETURN:
11029 * None
11031 static void LISTVIEW_UpdateSize(LISTVIEW_INFO *infoPtr)
11033 TRACE("uView=%d, rcList(old)=%s\n", infoPtr->uView, wine_dbgstr_rect(&infoPtr->rcList));
11035 GetClientRect(infoPtr->hwndSelf, &infoPtr->rcList);
11037 if (infoPtr->uView == LV_VIEW_LIST)
11039 /* Apparently the "LIST" style is supposed to have the same
11040 * number of items in a column even if there is no scroll bar.
11041 * Since if a scroll bar already exists then the bottom is already
11042 * reduced, only reduce if the scroll bar does not currently exist.
11043 * The "2" is there to mimic the native control. I think it may be
11044 * related to either padding or edges. (GLA 7/2002)
11046 if (!(GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE) & WS_HSCROLL))
11047 infoPtr->rcList.bottom -= GetSystemMetrics(SM_CYHSCROLL);
11048 infoPtr->rcList.bottom = max (infoPtr->rcList.bottom - 2, 0);
11051 /* if control created invisible header isn't created */
11052 if (infoPtr->hwndHeader)
11054 HDLAYOUT hl;
11055 WINDOWPOS wp;
11057 hl.prc = &infoPtr->rcList;
11058 hl.pwpos = &wp;
11059 SendMessageW( infoPtr->hwndHeader, HDM_LAYOUT, 0, (LPARAM)&hl );
11060 TRACE(" wp.flags=0x%08x, wp=%d,%d (%dx%d)\n", wp.flags, wp.x, wp.y, wp.cx, wp.cy);
11062 if (LISTVIEW_IsHeaderEnabled(infoPtr))
11063 wp.flags |= SWP_SHOWWINDOW;
11064 else
11066 wp.flags |= SWP_HIDEWINDOW;
11067 wp.cy = 0;
11070 SetWindowPos(wp.hwnd, wp.hwndInsertAfter, wp.x, wp.y, wp.cx, wp.cy, wp.flags);
11071 TRACE(" after SWP wp=%d,%d (%dx%d)\n", wp.x, wp.y, wp.cx, wp.cy);
11073 infoPtr->rcList.top = max(wp.cy, 0);
11075 /* extra padding for grid */
11076 if (infoPtr->uView == LV_VIEW_DETAILS && infoPtr->dwLvExStyle & LVS_EX_GRIDLINES)
11077 infoPtr->rcList.top += 2;
11079 TRACE(" rcList=%s\n", wine_dbgstr_rect(&infoPtr->rcList));
11082 /***
11083 * DESCRIPTION:
11084 * Processes WM_STYLECHANGED messages.
11086 * PARAMETER(S):
11087 * [I] infoPtr : valid pointer to the listview structure
11088 * [I] wStyleType : window style type (normal or extended)
11089 * [I] lpss : window style information
11091 * RETURN:
11092 * Zero
11094 static INT LISTVIEW_StyleChanged(LISTVIEW_INFO *infoPtr, WPARAM wStyleType,
11095 const STYLESTRUCT *lpss)
11097 UINT uNewView = lpss->styleNew & LVS_TYPEMASK;
11098 UINT uOldView = lpss->styleOld & LVS_TYPEMASK;
11099 UINT style;
11101 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
11102 wStyleType, lpss->styleOld, lpss->styleNew);
11104 if (wStyleType != GWL_STYLE) return 0;
11106 infoPtr->dwStyle = lpss->styleNew;
11108 if (((lpss->styleOld & WS_HSCROLL) != 0)&&
11109 ((lpss->styleNew & WS_HSCROLL) == 0))
11110 ShowScrollBar(infoPtr->hwndSelf, SB_HORZ, FALSE);
11112 if (((lpss->styleOld & WS_VSCROLL) != 0)&&
11113 ((lpss->styleNew & WS_VSCROLL) == 0))
11114 ShowScrollBar(infoPtr->hwndSelf, SB_VERT, FALSE);
11116 if (uNewView != uOldView)
11118 HIMAGELIST himl;
11120 /* LVM_SETVIEW doesn't change window style bits within LVS_TYPEMASK,
11121 changing style updates current view only when view bits change. */
11122 map_style_view(infoPtr);
11123 SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
11124 ShowWindow(infoPtr->hwndHeader, SW_HIDE);
11126 ShowScrollBar(infoPtr->hwndSelf, SB_BOTH, FALSE);
11127 SetRectEmpty(&infoPtr->rcFocus);
11129 himl = (uNewView == LVS_ICON ? infoPtr->himlNormal : infoPtr->himlSmall);
11130 set_icon_size(&infoPtr->iconSize, himl, uNewView != LVS_ICON);
11132 if (uNewView == LVS_REPORT)
11134 HDLAYOUT hl;
11135 WINDOWPOS wp;
11137 LISTVIEW_CreateHeader( infoPtr );
11139 hl.prc = &infoPtr->rcList;
11140 hl.pwpos = &wp;
11141 SendMessageW( infoPtr->hwndHeader, HDM_LAYOUT, 0, (LPARAM)&hl );
11142 SetWindowPos(infoPtr->hwndHeader, infoPtr->hwndSelf, wp.x, wp.y, wp.cx, wp.cy,
11143 wp.flags | ((infoPtr->dwStyle & LVS_NOCOLUMNHEADER)
11144 ? SWP_HIDEWINDOW : SWP_SHOWWINDOW));
11147 LISTVIEW_UpdateItemSize(infoPtr);
11150 if (uNewView == LVS_REPORT || infoPtr->dwLvExStyle & LVS_EX_HEADERINALLVIEWS)
11152 if ((lpss->styleOld ^ lpss->styleNew) & LVS_NOCOLUMNHEADER)
11154 if (lpss->styleNew & LVS_NOCOLUMNHEADER)
11156 /* Turn off the header control */
11157 style = GetWindowLongW(infoPtr->hwndHeader, GWL_STYLE);
11158 TRACE("Hide header control, was 0x%08x\n", style);
11159 SetWindowLongW(infoPtr->hwndHeader, GWL_STYLE, style | HDS_HIDDEN);
11160 } else {
11161 /* Turn on the header control */
11162 if ((style = GetWindowLongW(infoPtr->hwndHeader, GWL_STYLE)) & HDS_HIDDEN)
11164 TRACE("Show header control, was 0x%08x\n", style);
11165 SetWindowLongW(infoPtr->hwndHeader, GWL_STYLE, (style & ~HDS_HIDDEN) | WS_VISIBLE);
11171 if ( (uNewView == LVS_ICON || uNewView == LVS_SMALLICON) &&
11172 (uNewView != uOldView || ((lpss->styleNew ^ lpss->styleOld) & LVS_ALIGNMASK)) )
11173 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
11175 /* update the size of the client area */
11176 LISTVIEW_UpdateSize(infoPtr);
11178 /* add scrollbars if needed */
11179 LISTVIEW_UpdateScroll(infoPtr);
11181 /* invalidate client area + erase background */
11182 LISTVIEW_InvalidateList(infoPtr);
11184 return 0;
11187 /***
11188 * DESCRIPTION:
11189 * Processes WM_STYLECHANGING messages.
11191 * PARAMETER(S):
11192 * [I] wStyleType : window style type (normal or extended)
11193 * [I0] lpss : window style information
11195 * RETURN:
11196 * Zero
11198 static INT LISTVIEW_StyleChanging(WPARAM wStyleType,
11199 STYLESTRUCT *lpss)
11201 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
11202 wStyleType, lpss->styleOld, lpss->styleNew);
11204 /* don't forward LVS_OWNERDATA only if not already set to */
11205 if ((lpss->styleNew ^ lpss->styleOld) & LVS_OWNERDATA)
11207 if (lpss->styleOld & LVS_OWNERDATA)
11208 lpss->styleNew |= LVS_OWNERDATA;
11209 else
11210 lpss->styleNew &= ~LVS_OWNERDATA;
11213 return 0;
11216 /***
11217 * DESCRIPTION:
11218 * Processes WM_SHOWWINDOW messages.
11220 * PARAMETER(S):
11221 * [I] infoPtr : valid pointer to the listview structure
11222 * [I] bShown : window is being shown (FALSE when hidden)
11223 * [I] iStatus : window show status
11225 * RETURN:
11226 * Zero
11228 static LRESULT LISTVIEW_ShowWindow(LISTVIEW_INFO *infoPtr, WPARAM bShown, LPARAM iStatus)
11230 /* header delayed creation */
11231 if ((infoPtr->uView == LV_VIEW_DETAILS) && bShown)
11233 LISTVIEW_CreateHeader(infoPtr);
11235 if (!(LVS_NOCOLUMNHEADER & infoPtr->dwStyle))
11236 ShowWindow(infoPtr->hwndHeader, SW_SHOWNORMAL);
11239 return DefWindowProcW(infoPtr->hwndSelf, WM_SHOWWINDOW, bShown, iStatus);
11242 /***
11243 * DESCRIPTION:
11244 * Processes CCM_GETVERSION messages.
11246 * PARAMETER(S):
11247 * [I] infoPtr : valid pointer to the listview structure
11249 * RETURN:
11250 * Current version
11252 static inline LRESULT LISTVIEW_GetVersion(const LISTVIEW_INFO *infoPtr)
11254 return infoPtr->iVersion;
11257 /***
11258 * DESCRIPTION:
11259 * Processes CCM_SETVERSION messages.
11261 * PARAMETER(S):
11262 * [I] infoPtr : valid pointer to the listview structure
11263 * [I] iVersion : version to be set
11265 * RETURN:
11266 * -1 when requested version is greater than DLL version;
11267 * previous version otherwise
11269 static LRESULT LISTVIEW_SetVersion(LISTVIEW_INFO *infoPtr, DWORD iVersion)
11271 INT iOldVersion = infoPtr->iVersion;
11273 if (iVersion > COMCTL32_VERSION)
11274 return -1;
11276 infoPtr->iVersion = iVersion;
11278 TRACE("new version %d\n", iVersion);
11280 return iOldVersion;
11283 /***
11284 * DESCRIPTION:
11285 * Window procedure of the listview control.
11288 static LRESULT WINAPI
11289 LISTVIEW_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
11291 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongPtrW(hwnd, 0);
11293 TRACE("(hwnd=%p uMsg=%x wParam=%lx lParam=%lx)\n", hwnd, uMsg, wParam, lParam);
11295 if (!infoPtr && (uMsg != WM_NCCREATE))
11296 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
11298 switch (uMsg)
11300 case LVM_APPROXIMATEVIEWRECT:
11301 return LISTVIEW_ApproximateViewRect(infoPtr, (INT)wParam,
11302 LOWORD(lParam), HIWORD(lParam));
11303 case LVM_ARRANGE:
11304 return LISTVIEW_Arrange(infoPtr, (INT)wParam);
11306 case LVM_CANCELEDITLABEL:
11307 return LISTVIEW_CancelEditLabel(infoPtr);
11309 case LVM_CREATEDRAGIMAGE:
11310 return (LRESULT)LISTVIEW_CreateDragImage(infoPtr, (INT)wParam, (LPPOINT)lParam);
11312 case LVM_DELETEALLITEMS:
11313 return LISTVIEW_DeleteAllItems(infoPtr, FALSE);
11315 case LVM_DELETECOLUMN:
11316 return LISTVIEW_DeleteColumn(infoPtr, (INT)wParam);
11318 case LVM_DELETEITEM:
11319 return LISTVIEW_DeleteItem(infoPtr, (INT)wParam);
11321 case LVM_EDITLABELA:
11322 case LVM_EDITLABELW:
11323 return (LRESULT)LISTVIEW_EditLabelT(infoPtr, (INT)wParam,
11324 uMsg == LVM_EDITLABELW);
11325 /* case LVM_ENABLEGROUPVIEW: */
11327 case LVM_ENSUREVISIBLE:
11328 return LISTVIEW_EnsureVisible(infoPtr, (INT)wParam, (BOOL)lParam);
11330 case LVM_FINDITEMW:
11331 return LISTVIEW_FindItemW(infoPtr, (INT)wParam, (LPLVFINDINFOW)lParam);
11333 case LVM_FINDITEMA:
11334 return LISTVIEW_FindItemA(infoPtr, (INT)wParam, (LPLVFINDINFOA)lParam);
11336 case LVM_GETBKCOLOR:
11337 return infoPtr->clrBk;
11339 /* case LVM_GETBKIMAGE: */
11341 case LVM_GETCALLBACKMASK:
11342 return infoPtr->uCallbackMask;
11344 case LVM_GETCOLUMNA:
11345 case LVM_GETCOLUMNW:
11346 return LISTVIEW_GetColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam,
11347 uMsg == LVM_GETCOLUMNW);
11349 case LVM_GETCOLUMNORDERARRAY:
11350 return LISTVIEW_GetColumnOrderArray(infoPtr, (INT)wParam, (LPINT)lParam);
11352 case LVM_GETCOLUMNWIDTH:
11353 return LISTVIEW_GetColumnWidth(infoPtr, (INT)wParam);
11355 case LVM_GETCOUNTPERPAGE:
11356 return LISTVIEW_GetCountPerPage(infoPtr);
11358 case LVM_GETEDITCONTROL:
11359 return (LRESULT)infoPtr->hwndEdit;
11361 case LVM_GETEXTENDEDLISTVIEWSTYLE:
11362 return infoPtr->dwLvExStyle;
11364 /* case LVM_GETGROUPINFO: */
11366 /* case LVM_GETGROUPMETRICS: */
11368 case LVM_GETHEADER:
11369 return (LRESULT)infoPtr->hwndHeader;
11371 case LVM_GETHOTCURSOR:
11372 return (LRESULT)infoPtr->hHotCursor;
11374 case LVM_GETHOTITEM:
11375 return infoPtr->nHotItem;
11377 case LVM_GETHOVERTIME:
11378 return infoPtr->dwHoverTime;
11380 case LVM_GETIMAGELIST:
11381 return (LRESULT)LISTVIEW_GetImageList(infoPtr, (INT)wParam);
11383 /* case LVM_GETINSERTMARK: */
11385 /* case LVM_GETINSERTMARKCOLOR: */
11387 /* case LVM_GETINSERTMARKRECT: */
11389 case LVM_GETISEARCHSTRINGA:
11390 case LVM_GETISEARCHSTRINGW:
11391 FIXME("LVM_GETISEARCHSTRING: unimplemented\n");
11392 return FALSE;
11394 case LVM_GETITEMA:
11395 case LVM_GETITEMW:
11396 return LISTVIEW_GetItemExtT(infoPtr, (LPLVITEMW)lParam, uMsg == LVM_GETITEMW);
11398 case LVM_GETITEMCOUNT:
11399 return infoPtr->nItemCount;
11401 case LVM_GETITEMPOSITION:
11402 return LISTVIEW_GetItemPosition(infoPtr, (INT)wParam, (LPPOINT)lParam);
11404 case LVM_GETITEMRECT:
11405 return LISTVIEW_GetItemRect(infoPtr, (INT)wParam, (LPRECT)lParam);
11407 case LVM_GETITEMSPACING:
11408 return LISTVIEW_GetItemSpacing(infoPtr, (BOOL)wParam);
11410 case LVM_GETITEMSTATE:
11411 return LISTVIEW_GetItemState(infoPtr, (INT)wParam, (UINT)lParam);
11413 case LVM_GETITEMTEXTA:
11414 case LVM_GETITEMTEXTW:
11415 return LISTVIEW_GetItemTextT(infoPtr, (INT)wParam, (LPLVITEMW)lParam,
11416 uMsg == LVM_GETITEMTEXTW);
11418 case LVM_GETNEXTITEM:
11419 return LISTVIEW_GetNextItem(infoPtr, (INT)wParam, LOWORD(lParam));
11421 case LVM_GETNUMBEROFWORKAREAS:
11422 FIXME("LVM_GETNUMBEROFWORKAREAS: unimplemented\n");
11423 return 1;
11425 case LVM_GETORIGIN:
11426 if (!lParam) return FALSE;
11427 if (infoPtr->uView == LV_VIEW_DETAILS ||
11428 infoPtr->uView == LV_VIEW_LIST) return FALSE;
11429 LISTVIEW_GetOrigin(infoPtr, (LPPOINT)lParam);
11430 return TRUE;
11432 /* case LVM_GETOUTLINECOLOR: */
11434 /* case LVM_GETSELECTEDCOLUMN: */
11436 case LVM_GETSELECTEDCOUNT:
11437 return LISTVIEW_GetSelectedCount(infoPtr);
11439 case LVM_GETSELECTIONMARK:
11440 return infoPtr->nSelectionMark;
11442 case LVM_GETSTRINGWIDTHA:
11443 case LVM_GETSTRINGWIDTHW:
11444 return LISTVIEW_GetStringWidthT(infoPtr, (LPCWSTR)lParam,
11445 uMsg == LVM_GETSTRINGWIDTHW);
11447 case LVM_GETSUBITEMRECT:
11448 return LISTVIEW_GetSubItemRect(infoPtr, (UINT)wParam, (LPRECT)lParam);
11450 case LVM_GETTEXTBKCOLOR:
11451 return infoPtr->clrTextBk;
11453 case LVM_GETTEXTCOLOR:
11454 return infoPtr->clrText;
11456 /* case LVM_GETTILEINFO: */
11458 /* case LVM_GETTILEVIEWINFO: */
11460 case LVM_GETTOOLTIPS:
11461 if( !infoPtr->hwndToolTip )
11462 infoPtr->hwndToolTip = COMCTL32_CreateToolTip( hwnd );
11463 return (LRESULT)infoPtr->hwndToolTip;
11465 case LVM_GETTOPINDEX:
11466 return LISTVIEW_GetTopIndex(infoPtr);
11468 case LVM_GETUNICODEFORMAT:
11469 return (infoPtr->notifyFormat == NFR_UNICODE);
11471 case LVM_GETVIEW:
11472 return infoPtr->uView;
11474 case LVM_GETVIEWRECT:
11475 return LISTVIEW_GetViewRect(infoPtr, (LPRECT)lParam);
11477 case LVM_GETWORKAREAS:
11478 FIXME("LVM_GETWORKAREAS: unimplemented\n");
11479 return FALSE;
11481 /* case LVM_HASGROUP: */
11483 case LVM_HITTEST:
11484 return LISTVIEW_HitTest(infoPtr, (LPLVHITTESTINFO)lParam, FALSE, TRUE);
11486 case LVM_INSERTCOLUMNA:
11487 case LVM_INSERTCOLUMNW:
11488 return LISTVIEW_InsertColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam,
11489 uMsg == LVM_INSERTCOLUMNW);
11491 /* case LVM_INSERTGROUP: */
11493 /* case LVM_INSERTGROUPSORTED: */
11495 case LVM_INSERTITEMA:
11496 case LVM_INSERTITEMW:
11497 return LISTVIEW_InsertItemT(infoPtr, (LPLVITEMW)lParam, uMsg == LVM_INSERTITEMW);
11499 /* case LVM_INSERTMARKHITTEST: */
11501 /* case LVM_ISGROUPVIEWENABLED: */
11503 case LVM_ISITEMVISIBLE:
11504 return LISTVIEW_IsItemVisible(infoPtr, (INT)wParam);
11506 case LVM_MAPIDTOINDEX:
11507 return LISTVIEW_MapIdToIndex(infoPtr, (UINT)wParam);
11509 case LVM_MAPINDEXTOID:
11510 return LISTVIEW_MapIndexToId(infoPtr, (INT)wParam);
11512 /* case LVM_MOVEGROUP: */
11514 /* case LVM_MOVEITEMTOGROUP: */
11516 case LVM_REDRAWITEMS:
11517 return LISTVIEW_RedrawItems(infoPtr, (INT)wParam, (INT)lParam);
11519 /* case LVM_REMOVEALLGROUPS: */
11521 /* case LVM_REMOVEGROUP: */
11523 case LVM_SCROLL:
11524 return LISTVIEW_Scroll(infoPtr, (INT)wParam, (INT)lParam);
11526 case LVM_SETBKCOLOR:
11527 return LISTVIEW_SetBkColor(infoPtr, (COLORREF)lParam);
11529 /* case LVM_SETBKIMAGE: */
11531 case LVM_SETCALLBACKMASK:
11532 infoPtr->uCallbackMask = (UINT)wParam;
11533 return TRUE;
11535 case LVM_SETCOLUMNA:
11536 case LVM_SETCOLUMNW:
11537 return LISTVIEW_SetColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam,
11538 uMsg == LVM_SETCOLUMNW);
11540 case LVM_SETCOLUMNORDERARRAY:
11541 return LISTVIEW_SetColumnOrderArray(infoPtr, (INT)wParam, (LPINT)lParam);
11543 case LVM_SETCOLUMNWIDTH:
11544 return LISTVIEW_SetColumnWidth(infoPtr, (INT)wParam, (short)LOWORD(lParam));
11546 case LVM_SETEXTENDEDLISTVIEWSTYLE:
11547 return LISTVIEW_SetExtendedListViewStyle(infoPtr, (DWORD)wParam, (DWORD)lParam);
11549 /* case LVM_SETGROUPINFO: */
11551 /* case LVM_SETGROUPMETRICS: */
11553 case LVM_SETHOTCURSOR:
11554 return (LRESULT)LISTVIEW_SetHotCursor(infoPtr, (HCURSOR)lParam);
11556 case LVM_SETHOTITEM:
11557 return LISTVIEW_SetHotItem(infoPtr, (INT)wParam);
11559 case LVM_SETHOVERTIME:
11560 return LISTVIEW_SetHoverTime(infoPtr, (DWORD)lParam);
11562 case LVM_SETICONSPACING:
11563 if(lParam == -1)
11564 return LISTVIEW_SetIconSpacing(infoPtr, -1, -1);
11565 return LISTVIEW_SetIconSpacing(infoPtr, LOWORD(lParam), HIWORD(lParam));
11567 case LVM_SETIMAGELIST:
11568 return (LRESULT)LISTVIEW_SetImageList(infoPtr, (INT)wParam, (HIMAGELIST)lParam);
11570 /* case LVM_SETINFOTIP: */
11572 /* case LVM_SETINSERTMARK: */
11574 /* case LVM_SETINSERTMARKCOLOR: */
11576 case LVM_SETITEMA:
11577 case LVM_SETITEMW:
11579 if (infoPtr->dwStyle & LVS_OWNERDATA) return FALSE;
11580 return LISTVIEW_SetItemT(infoPtr, (LPLVITEMW)lParam, (uMsg == LVM_SETITEMW));
11583 case LVM_SETITEMCOUNT:
11584 return LISTVIEW_SetItemCount(infoPtr, (INT)wParam, (DWORD)lParam);
11586 case LVM_SETITEMPOSITION:
11588 POINT pt;
11589 pt.x = (short)LOWORD(lParam);
11590 pt.y = (short)HIWORD(lParam);
11591 return LISTVIEW_SetItemPosition(infoPtr, (INT)wParam, &pt);
11594 case LVM_SETITEMPOSITION32:
11595 return LISTVIEW_SetItemPosition(infoPtr, (INT)wParam, (POINT*)lParam);
11597 case LVM_SETITEMSTATE:
11598 return LISTVIEW_SetItemState(infoPtr, (INT)wParam, (LPLVITEMW)lParam);
11600 case LVM_SETITEMTEXTA:
11601 case LVM_SETITEMTEXTW:
11602 return LISTVIEW_SetItemTextT(infoPtr, (INT)wParam, (LPLVITEMW)lParam,
11603 uMsg == LVM_SETITEMTEXTW);
11605 /* case LVM_SETOUTLINECOLOR: */
11607 /* case LVM_SETSELECTEDCOLUMN: */
11609 case LVM_SETSELECTIONMARK:
11610 return LISTVIEW_SetSelectionMark(infoPtr, (INT)lParam);
11612 case LVM_SETTEXTBKCOLOR:
11613 return LISTVIEW_SetTextBkColor(infoPtr, (COLORREF)lParam);
11615 case LVM_SETTEXTCOLOR:
11616 return LISTVIEW_SetTextColor(infoPtr, (COLORREF)lParam);
11618 /* case LVM_SETTILEINFO: */
11620 /* case LVM_SETTILEVIEWINFO: */
11622 /* case LVM_SETTILEWIDTH: */
11624 case LVM_SETTOOLTIPS:
11625 return (LRESULT)LISTVIEW_SetToolTips(infoPtr, (HWND)lParam);
11627 case LVM_SETUNICODEFORMAT:
11628 return LISTVIEW_SetUnicodeFormat(infoPtr, wParam);
11630 case LVM_SETVIEW:
11631 return LISTVIEW_SetView(infoPtr, wParam);
11633 /* case LVM_SETWORKAREAS: */
11635 /* case LVM_SORTGROUPS: */
11637 case LVM_SORTITEMS:
11638 case LVM_SORTITEMSEX:
11639 return LISTVIEW_SortItems(infoPtr, (PFNLVCOMPARE)lParam, wParam,
11640 uMsg == LVM_SORTITEMSEX);
11641 case LVM_SUBITEMHITTEST:
11642 return LISTVIEW_HitTest(infoPtr, (LPLVHITTESTINFO)lParam, TRUE, FALSE);
11644 case LVM_UPDATE:
11645 return LISTVIEW_Update(infoPtr, (INT)wParam);
11647 case CCM_GETVERSION:
11648 return LISTVIEW_GetVersion(infoPtr);
11650 case CCM_SETVERSION:
11651 return LISTVIEW_SetVersion(infoPtr, wParam);
11653 case WM_CHAR:
11654 return LISTVIEW_ProcessLetterKeys( infoPtr, wParam, lParam );
11656 case WM_COMMAND:
11657 return LISTVIEW_Command(infoPtr, wParam, lParam);
11659 case WM_NCCREATE:
11660 return LISTVIEW_NCCreate(hwnd, (LPCREATESTRUCTW)lParam);
11662 case WM_CREATE:
11663 return LISTVIEW_Create(hwnd, (LPCREATESTRUCTW)lParam);
11665 case WM_DESTROY:
11666 return LISTVIEW_Destroy(infoPtr);
11668 case WM_ENABLE:
11669 return LISTVIEW_Enable(infoPtr);
11671 case WM_ERASEBKGND:
11672 return LISTVIEW_EraseBkgnd(infoPtr, (HDC)wParam);
11674 case WM_GETDLGCODE:
11675 return DLGC_WANTCHARS | DLGC_WANTARROWS;
11677 case WM_GETFONT:
11678 return (LRESULT)infoPtr->hFont;
11680 case WM_HSCROLL:
11681 return LISTVIEW_HScroll(infoPtr, (INT)LOWORD(wParam), 0);
11683 case WM_KEYDOWN:
11684 return LISTVIEW_KeyDown(infoPtr, (INT)wParam, (LONG)lParam);
11686 case WM_KILLFOCUS:
11687 return LISTVIEW_KillFocus(infoPtr);
11689 case WM_LBUTTONDBLCLK:
11690 return LISTVIEW_LButtonDblClk(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11692 case WM_LBUTTONDOWN:
11693 return LISTVIEW_LButtonDown(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11695 case WM_LBUTTONUP:
11696 return LISTVIEW_LButtonUp(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11698 case WM_MOUSEMOVE:
11699 return LISTVIEW_MouseMove (infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11701 case WM_MOUSEHOVER:
11702 return LISTVIEW_MouseHover(infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11704 case WM_NCDESTROY:
11705 return LISTVIEW_NCDestroy(infoPtr);
11707 case WM_NCPAINT:
11708 return LISTVIEW_NCPaint(infoPtr, (HRGN)wParam);
11710 case WM_NOTIFY:
11711 return LISTVIEW_Notify(infoPtr, (LPNMHDR)lParam);
11713 case WM_NOTIFYFORMAT:
11714 return LISTVIEW_NotifyFormat(infoPtr, (HWND)wParam, (INT)lParam);
11716 case WM_PRINTCLIENT:
11717 return LISTVIEW_PrintClient(infoPtr, (HDC)wParam, (DWORD)lParam);
11719 case WM_PAINT:
11720 return LISTVIEW_WMPaint(infoPtr, (HDC)wParam);
11722 case WM_RBUTTONDBLCLK:
11723 return LISTVIEW_RButtonDblClk(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11725 case WM_RBUTTONDOWN:
11726 return LISTVIEW_RButtonDown(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11728 case WM_SETCURSOR:
11729 return LISTVIEW_SetCursor(infoPtr, wParam, lParam);
11731 case WM_SETFOCUS:
11732 return LISTVIEW_SetFocus(infoPtr, (HWND)wParam);
11734 case WM_SETFONT:
11735 return LISTVIEW_SetFont(infoPtr, (HFONT)wParam, (WORD)lParam);
11737 case WM_SETREDRAW:
11738 return LISTVIEW_SetRedraw(infoPtr, (BOOL)wParam);
11740 case WM_SHOWWINDOW:
11741 return LISTVIEW_ShowWindow(infoPtr, wParam, lParam);
11743 case WM_STYLECHANGED:
11744 return LISTVIEW_StyleChanged(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
11746 case WM_STYLECHANGING:
11747 return LISTVIEW_StyleChanging(wParam, (LPSTYLESTRUCT)lParam);
11749 case WM_SYSCOLORCHANGE:
11750 COMCTL32_RefreshSysColors();
11751 return 0;
11753 /* case WM_TIMER: */
11754 case WM_THEMECHANGED:
11755 return LISTVIEW_ThemeChanged(infoPtr);
11757 case WM_VSCROLL:
11758 return LISTVIEW_VScroll(infoPtr, (INT)LOWORD(wParam), 0);
11760 case WM_MOUSEWHEEL:
11761 if (wParam & (MK_SHIFT | MK_CONTROL))
11762 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
11763 return LISTVIEW_MouseWheel(infoPtr, (short int)HIWORD(wParam));
11765 case WM_WINDOWPOSCHANGED:
11766 if (!(((WINDOWPOS *)lParam)->flags & SWP_NOSIZE))
11768 SetWindowPos(infoPtr->hwndSelf, 0, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOACTIVATE |
11769 SWP_NOZORDER | SWP_NOMOVE | SWP_NOSIZE);
11771 if ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (infoPtr->uView == LV_VIEW_DETAILS))
11773 if (notify_measureitem(infoPtr)) LISTVIEW_InvalidateList(infoPtr);
11775 LISTVIEW_Size(infoPtr, ((WINDOWPOS *)lParam)->cx, ((WINDOWPOS *)lParam)->cy);
11777 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
11779 /* case WM_WININICHANGE: */
11781 default:
11782 if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
11783 ERR("unknown msg %04x wp=%08lx lp=%08lx\n", uMsg, wParam, lParam);
11785 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
11790 /***
11791 * DESCRIPTION:
11792 * Registers the window class.
11794 * PARAMETER(S):
11795 * None
11797 * RETURN:
11798 * None
11800 void LISTVIEW_Register(void)
11802 WNDCLASSW wndClass;
11804 ZeroMemory(&wndClass, sizeof(WNDCLASSW));
11805 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS;
11806 wndClass.lpfnWndProc = LISTVIEW_WindowProc;
11807 wndClass.cbClsExtra = 0;
11808 wndClass.cbWndExtra = sizeof(LISTVIEW_INFO *);
11809 wndClass.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
11810 wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
11811 wndClass.lpszClassName = WC_LISTVIEWW;
11812 RegisterClassW(&wndClass);
11815 /***
11816 * DESCRIPTION:
11817 * Unregisters the window class.
11819 * PARAMETER(S):
11820 * None
11822 * RETURN:
11823 * None
11825 void LISTVIEW_Unregister(void)
11827 UnregisterClassW(WC_LISTVIEWW, NULL);
11830 /***
11831 * DESCRIPTION:
11832 * Handle any WM_COMMAND messages
11834 * PARAMETER(S):
11835 * [I] infoPtr : valid pointer to the listview structure
11836 * [I] wParam : the first message parameter
11837 * [I] lParam : the second message parameter
11839 * RETURN:
11840 * Zero.
11842 static LRESULT LISTVIEW_Command(LISTVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
11845 TRACE("(%p %x %x %lx)\n", infoPtr, HIWORD(wParam), LOWORD(wParam), lParam);
11847 if (!infoPtr->hwndEdit) return 0;
11849 switch (HIWORD(wParam))
11851 case EN_UPDATE:
11854 * Adjust the edit window size
11856 WCHAR buffer[1024];
11857 HDC hdc = GetDC(infoPtr->hwndEdit);
11858 HFONT hFont, hOldFont = 0;
11859 RECT rect;
11860 SIZE sz;
11862 if (!infoPtr->hwndEdit || !hdc) return 0;
11863 GetWindowTextW(infoPtr->hwndEdit, buffer, sizeof(buffer)/sizeof(buffer[0]));
11864 GetWindowRect(infoPtr->hwndEdit, &rect);
11866 /* Select font to get the right dimension of the string */
11867 hFont = (HFONT)SendMessageW(infoPtr->hwndEdit, WM_GETFONT, 0, 0);
11868 if (hFont)
11870 hOldFont = SelectObject(hdc, hFont);
11873 if (GetTextExtentPoint32W(hdc, buffer, lstrlenW(buffer), &sz))
11875 TEXTMETRICW textMetric;
11877 /* Add Extra spacing for the next character */
11878 GetTextMetricsW(hdc, &textMetric);
11879 sz.cx += (textMetric.tmMaxCharWidth * 2);
11881 SetWindowPos(infoPtr->hwndEdit, NULL, 0, 0, sz.cx,
11882 rect.bottom - rect.top, SWP_DRAWFRAME | SWP_NOMOVE | SWP_NOZORDER);
11884 if (hFont)
11885 SelectObject(hdc, hOldFont);
11887 ReleaseDC(infoPtr->hwndEdit, hdc);
11889 break;
11891 case EN_KILLFOCUS:
11893 LISTVIEW_CancelEditLabel(infoPtr);
11894 break;
11897 default:
11898 return SendMessageW (infoPtr->hwndNotify, WM_COMMAND, wParam, lParam);
11901 return 0;