comctl32: Use SetRect() instead of open coding it.
[wine.git] / dlls / comctl32 / listview.c
bloba4336e8c6c0ac8c446fc7d7c527793627fe3682f
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 if (!DPA_SetPtr(clone->hdpa, i, newrng))
3128 Free(newrng);
3129 goto fail;
3132 return clone;
3134 fail:
3135 TRACE ("clone failed\n");
3136 ranges_destroy(clone);
3137 return NULL;
3140 static RANGES ranges_diff(RANGES ranges, RANGES sub)
3142 INT i;
3144 for (i = 0; i < DPA_GetPtrCount(sub->hdpa); i++)
3145 ranges_del(ranges, *((RANGE *)DPA_GetPtr(sub->hdpa, i)));
3147 return ranges;
3150 static void ranges_dump(RANGES ranges)
3152 INT i;
3154 for (i = 0; i < DPA_GetPtrCount(ranges->hdpa); i++)
3155 TRACE(" %s\n", debugrange(DPA_GetPtr(ranges->hdpa, i)));
3158 static inline BOOL ranges_contain(RANGES ranges, INT nItem)
3160 RANGE srchrng = { nItem, nItem + 1 };
3162 TRACE("(nItem=%d)\n", nItem);
3163 ranges_check(ranges, "before contain");
3164 return DPA_Search(ranges->hdpa, &srchrng, 0, ranges_cmp, 0, DPAS_SORTED) != -1;
3167 static INT ranges_itemcount(RANGES ranges)
3169 INT i, count = 0;
3171 for (i = 0; i < DPA_GetPtrCount(ranges->hdpa); i++)
3173 RANGE *sel = DPA_GetPtr(ranges->hdpa, i);
3174 count += sel->upper - sel->lower;
3177 return count;
3180 static BOOL ranges_shift(RANGES ranges, INT nItem, INT delta, INT nUpper)
3182 RANGE srchrng = { nItem, nItem + 1 }, *chkrng;
3183 INT index;
3185 index = DPA_Search(ranges->hdpa, &srchrng, 0, ranges_cmp, 0, DPAS_SORTED | DPAS_INSERTAFTER);
3186 if (index == -1) return TRUE;
3188 for (; index < DPA_GetPtrCount(ranges->hdpa); index++)
3190 chkrng = DPA_GetPtr(ranges->hdpa, index);
3191 if (chkrng->lower >= nItem)
3192 chkrng->lower = max(min(chkrng->lower + delta, nUpper - 1), 0);
3193 if (chkrng->upper > nItem)
3194 chkrng->upper = max(min(chkrng->upper + delta, nUpper), 0);
3196 return TRUE;
3199 static BOOL ranges_add(RANGES ranges, RANGE range)
3201 RANGE srchrgn;
3202 INT index;
3204 TRACE("(%s)\n", debugrange(&range));
3205 ranges_check(ranges, "before add");
3207 /* try find overlapping regions first */
3208 srchrgn.lower = range.lower - 1;
3209 srchrgn.upper = range.upper + 1;
3210 index = DPA_Search(ranges->hdpa, &srchrgn, 0, ranges_cmp, 0, DPAS_SORTED);
3212 if (index == -1)
3214 RANGE *newrgn;
3216 TRACE("Adding new range\n");
3218 /* create the brand new range to insert */
3219 newrgn = Alloc(sizeof(RANGE));
3220 if(!newrgn) goto fail;
3221 *newrgn = range;
3223 /* figure out where to insert it */
3224 index = DPA_Search(ranges->hdpa, newrgn, 0, ranges_cmp, 0, DPAS_SORTED | DPAS_INSERTAFTER);
3225 TRACE("index=%d\n", index);
3226 if (index == -1) index = 0;
3228 /* and get it over with */
3229 if (DPA_InsertPtr(ranges->hdpa, index, newrgn) == -1)
3231 Free(newrgn);
3232 goto fail;
3235 else
3237 RANGE *chkrgn, *mrgrgn;
3238 INT fromindex, mergeindex;
3240 chkrgn = DPA_GetPtr(ranges->hdpa, index);
3241 TRACE("Merge with %s @%d\n", debugrange(chkrgn), index);
3243 chkrgn->lower = min(range.lower, chkrgn->lower);
3244 chkrgn->upper = max(range.upper, chkrgn->upper);
3246 TRACE("New range %s @%d\n", debugrange(chkrgn), index);
3248 /* merge now common ranges */
3249 fromindex = 0;
3250 srchrgn.lower = chkrgn->lower - 1;
3251 srchrgn.upper = chkrgn->upper + 1;
3255 mergeindex = DPA_Search(ranges->hdpa, &srchrgn, fromindex, ranges_cmp, 0, 0);
3256 if (mergeindex == -1) break;
3257 if (mergeindex == index)
3259 fromindex = index + 1;
3260 continue;
3263 TRACE("Merge with index %i\n", mergeindex);
3265 mrgrgn = DPA_GetPtr(ranges->hdpa, mergeindex);
3266 chkrgn->lower = min(chkrgn->lower, mrgrgn->lower);
3267 chkrgn->upper = max(chkrgn->upper, mrgrgn->upper);
3268 Free(mrgrgn);
3269 DPA_DeletePtr(ranges->hdpa, mergeindex);
3270 if (mergeindex < index) index --;
3271 } while(1);
3274 ranges_check(ranges, "after add");
3275 return TRUE;
3277 fail:
3278 ranges_check(ranges, "failed add");
3279 return FALSE;
3282 static BOOL ranges_del(RANGES ranges, RANGE range)
3284 RANGE *chkrgn;
3285 INT index;
3287 TRACE("(%s)\n", debugrange(&range));
3288 ranges_check(ranges, "before del");
3290 /* we don't use DPAS_SORTED here, since we need *
3291 * to find the first overlapping range */
3292 index = DPA_Search(ranges->hdpa, &range, 0, ranges_cmp, 0, 0);
3293 while(index != -1)
3295 chkrgn = DPA_GetPtr(ranges->hdpa, index);
3297 TRACE("Matches range %s @%d\n", debugrange(chkrgn), index);
3299 /* case 1: Same range */
3300 if ( (chkrgn->upper == range.upper) &&
3301 (chkrgn->lower == range.lower) )
3303 DPA_DeletePtr(ranges->hdpa, index);
3304 Free(chkrgn);
3305 break;
3307 /* case 2: engulf */
3308 else if ( (chkrgn->upper <= range.upper) &&
3309 (chkrgn->lower >= range.lower) )
3311 DPA_DeletePtr(ranges->hdpa, index);
3312 Free(chkrgn);
3314 /* case 3: overlap upper */
3315 else if ( (chkrgn->upper <= range.upper) &&
3316 (chkrgn->lower < range.lower) )
3318 chkrgn->upper = range.lower;
3320 /* case 4: overlap lower */
3321 else if ( (chkrgn->upper > range.upper) &&
3322 (chkrgn->lower >= range.lower) )
3324 chkrgn->lower = range.upper;
3325 break;
3327 /* case 5: fully internal */
3328 else
3330 RANGE *newrgn;
3332 if (!(newrgn = Alloc(sizeof(RANGE)))) goto fail;
3333 newrgn->lower = chkrgn->lower;
3334 newrgn->upper = range.lower;
3335 chkrgn->lower = range.upper;
3336 if (DPA_InsertPtr(ranges->hdpa, index, newrgn) == -1)
3338 Free(newrgn);
3339 goto fail;
3341 break;
3344 index = DPA_Search(ranges->hdpa, &range, index, ranges_cmp, 0, 0);
3347 ranges_check(ranges, "after del");
3348 return TRUE;
3350 fail:
3351 ranges_check(ranges, "failed del");
3352 return FALSE;
3355 /***
3356 * DESCRIPTION:
3357 * Removes all selection ranges
3359 * Parameters(s):
3360 * [I] infoPtr : valid pointer to the listview structure
3361 * [I] toSkip : item range to skip removing the selection
3363 * RETURNS:
3364 * SUCCESS : TRUE
3365 * FAILURE : FALSE
3367 static BOOL LISTVIEW_DeselectAllSkipItems(LISTVIEW_INFO *infoPtr, RANGES toSkip)
3369 LVITEMW lvItem;
3370 ITERATOR i;
3371 RANGES clone;
3373 TRACE("()\n");
3375 lvItem.state = 0;
3376 lvItem.stateMask = LVIS_SELECTED;
3378 /* need to clone the DPA because callbacks can change it */
3379 if (!(clone = ranges_clone(infoPtr->selectionRanges))) return FALSE;
3380 iterator_rangesitems(&i, ranges_diff(clone, toSkip));
3381 while(iterator_next(&i))
3382 LISTVIEW_SetItemState(infoPtr, i.nItem, &lvItem);
3383 /* note that the iterator destructor will free the cloned range */
3384 iterator_destroy(&i);
3386 return TRUE;
3389 static inline BOOL LISTVIEW_DeselectAllSkipItem(LISTVIEW_INFO *infoPtr, INT nItem)
3391 RANGES toSkip;
3393 if (!(toSkip = ranges_create(1))) return FALSE;
3394 if (nItem != -1) ranges_additem(toSkip, nItem);
3395 LISTVIEW_DeselectAllSkipItems(infoPtr, toSkip);
3396 ranges_destroy(toSkip);
3397 return TRUE;
3400 static inline BOOL LISTVIEW_DeselectAll(LISTVIEW_INFO *infoPtr)
3402 return LISTVIEW_DeselectAllSkipItem(infoPtr, -1);
3405 /***
3406 * DESCRIPTION:
3407 * Retrieves the number of items that are marked as selected.
3409 * PARAMETER(S):
3410 * [I] infoPtr : valid pointer to the listview structure
3412 * RETURN:
3413 * Number of items selected.
3415 static INT LISTVIEW_GetSelectedCount(const LISTVIEW_INFO *infoPtr)
3417 INT nSelectedCount = 0;
3419 if (infoPtr->uCallbackMask & LVIS_SELECTED)
3421 INT i;
3422 for (i = 0; i < infoPtr->nItemCount; i++)
3424 if (LISTVIEW_GetItemState(infoPtr, i, LVIS_SELECTED))
3425 nSelectedCount++;
3428 else
3429 nSelectedCount = ranges_itemcount(infoPtr->selectionRanges);
3431 TRACE("nSelectedCount=%d\n", nSelectedCount);
3432 return nSelectedCount;
3435 /***
3436 * DESCRIPTION:
3437 * Manages the item focus.
3439 * PARAMETER(S):
3440 * [I] infoPtr : valid pointer to the listview structure
3441 * [I] nItem : item index
3443 * RETURN:
3444 * TRUE : focused item changed
3445 * FALSE : focused item has NOT changed
3447 static inline BOOL LISTVIEW_SetItemFocus(LISTVIEW_INFO *infoPtr, INT nItem)
3449 INT oldFocus = infoPtr->nFocusedItem;
3450 LVITEMW lvItem;
3452 if (nItem == infoPtr->nFocusedItem) return FALSE;
3454 lvItem.state = nItem == -1 ? 0 : LVIS_FOCUSED;
3455 lvItem.stateMask = LVIS_FOCUSED;
3456 LISTVIEW_SetItemState(infoPtr, nItem == -1 ? infoPtr->nFocusedItem : nItem, &lvItem);
3458 return oldFocus != infoPtr->nFocusedItem;
3461 static INT shift_item(const LISTVIEW_INFO *infoPtr, INT nShiftItem, INT nItem, INT direction)
3463 if (nShiftItem < nItem) return nShiftItem;
3465 if (nShiftItem > nItem) return nShiftItem + direction;
3467 if (direction > 0) return nShiftItem + direction;
3469 return min(nShiftItem, infoPtr->nItemCount - 1);
3472 /* This function updates focus index.
3474 Parameters:
3475 focus : current focus index
3476 item : index of item to be added/removed
3477 direction : add/remove flag
3479 static void LISTVIEW_ShiftFocus(LISTVIEW_INFO *infoPtr, INT focus, INT item, INT direction)
3481 BOOL old_change = infoPtr->bDoChangeNotify;
3483 infoPtr->bDoChangeNotify = FALSE;
3484 focus = shift_item(infoPtr, focus, item, direction);
3485 if (focus != infoPtr->nFocusedItem)
3486 LISTVIEW_SetItemFocus(infoPtr, focus);
3487 infoPtr->bDoChangeNotify = old_change;
3491 * DESCRIPTION:
3492 * Updates the various indices after an item has been inserted or deleted.
3494 * PARAMETER(S):
3495 * [I] infoPtr : valid pointer to the listview structure
3496 * [I] nItem : item index
3497 * [I] direction : Direction of shift, +1 or -1.
3499 * RETURN:
3500 * None
3502 static void LISTVIEW_ShiftIndices(LISTVIEW_INFO *infoPtr, INT nItem, INT direction)
3504 TRACE("Shifting %i, %i steps\n", nItem, direction);
3506 ranges_shift(infoPtr->selectionRanges, nItem, direction, infoPtr->nItemCount);
3507 assert(abs(direction) == 1);
3508 infoPtr->nSelectionMark = shift_item(infoPtr, infoPtr->nSelectionMark, nItem, direction);
3510 /* But we are not supposed to modify nHotItem! */
3514 * DESCRIPTION:
3515 * Adds a block of selections.
3517 * PARAMETER(S):
3518 * [I] infoPtr : valid pointer to the listview structure
3519 * [I] nItem : item index
3521 * RETURN:
3522 * Whether the window is still valid.
3524 static BOOL LISTVIEW_AddGroupSelection(LISTVIEW_INFO *infoPtr, INT nItem)
3526 INT nFirst = min(infoPtr->nSelectionMark, nItem);
3527 INT nLast = max(infoPtr->nSelectionMark, nItem);
3528 HWND hwndSelf = infoPtr->hwndSelf;
3529 NMLVODSTATECHANGE nmlv;
3530 LVITEMW item;
3531 BOOL bOldChange;
3532 INT i;
3534 /* Temporarily disable change notification
3535 * If the control is LVS_OWNERDATA, we need to send
3536 * only one LVN_ODSTATECHANGED notification.
3537 * See MSDN documentation for LVN_ITEMCHANGED.
3539 bOldChange = infoPtr->bDoChangeNotify;
3540 if (infoPtr->dwStyle & LVS_OWNERDATA) infoPtr->bDoChangeNotify = FALSE;
3542 if (nFirst == -1) nFirst = nItem;
3544 item.state = LVIS_SELECTED;
3545 item.stateMask = LVIS_SELECTED;
3547 for (i = nFirst; i <= nLast; i++)
3548 LISTVIEW_SetItemState(infoPtr,i,&item);
3550 ZeroMemory(&nmlv, sizeof(nmlv));
3551 nmlv.iFrom = nFirst;
3552 nmlv.iTo = nLast;
3553 nmlv.uOldState = 0;
3554 nmlv.uNewState = item.state;
3556 notify_hdr(infoPtr, LVN_ODSTATECHANGED, (LPNMHDR)&nmlv);
3557 if (!IsWindow(hwndSelf))
3558 return FALSE;
3559 infoPtr->bDoChangeNotify = bOldChange;
3560 return TRUE;
3564 /***
3565 * DESCRIPTION:
3566 * Sets a single group selection.
3568 * PARAMETER(S):
3569 * [I] infoPtr : valid pointer to the listview structure
3570 * [I] nItem : item index
3572 * RETURN:
3573 * None
3575 static void LISTVIEW_SetGroupSelection(LISTVIEW_INFO *infoPtr, INT nItem)
3577 RANGES selection;
3578 LVITEMW item;
3579 ITERATOR i;
3580 BOOL bOldChange;
3582 if (!(selection = ranges_create(100))) return;
3584 item.state = LVIS_SELECTED;
3585 item.stateMask = LVIS_SELECTED;
3587 if ((infoPtr->uView == LV_VIEW_LIST) || (infoPtr->uView == LV_VIEW_DETAILS))
3589 if (infoPtr->nSelectionMark == -1)
3591 infoPtr->nSelectionMark = nItem;
3592 ranges_additem(selection, nItem);
3594 else
3596 RANGE sel;
3598 sel.lower = min(infoPtr->nSelectionMark, nItem);
3599 sel.upper = max(infoPtr->nSelectionMark, nItem) + 1;
3600 ranges_add(selection, sel);
3603 else
3605 RECT rcItem, rcSel, rcSelMark;
3606 POINT ptItem;
3608 rcItem.left = LVIR_BOUNDS;
3609 if (!LISTVIEW_GetItemRect(infoPtr, nItem, &rcItem)) {
3610 ranges_destroy (selection);
3611 return;
3613 rcSelMark.left = LVIR_BOUNDS;
3614 if (!LISTVIEW_GetItemRect(infoPtr, infoPtr->nSelectionMark, &rcSelMark)) {
3615 ranges_destroy (selection);
3616 return;
3618 UnionRect(&rcSel, &rcItem, &rcSelMark);
3619 iterator_frameditems(&i, infoPtr, &rcSel);
3620 while(iterator_next(&i))
3622 LISTVIEW_GetItemPosition(infoPtr, i.nItem, &ptItem);
3623 if (PtInRect(&rcSel, ptItem)) ranges_additem(selection, i.nItem);
3625 iterator_destroy(&i);
3628 /* disable per item notifications on LVS_OWNERDATA style
3629 FIXME: single LVN_ODSTATECHANGED should be used */
3630 bOldChange = infoPtr->bDoChangeNotify;
3631 if (infoPtr->dwStyle & LVS_OWNERDATA) infoPtr->bDoChangeNotify = FALSE;
3633 LISTVIEW_DeselectAllSkipItems(infoPtr, selection);
3636 iterator_rangesitems(&i, selection);
3637 while(iterator_next(&i))
3638 LISTVIEW_SetItemState(infoPtr, i.nItem, &item);
3639 /* this will also destroy the selection */
3640 iterator_destroy(&i);
3642 infoPtr->bDoChangeNotify = bOldChange;
3644 LISTVIEW_SetItemFocus(infoPtr, nItem);
3647 /***
3648 * DESCRIPTION:
3649 * Sets a single selection.
3651 * PARAMETER(S):
3652 * [I] infoPtr : valid pointer to the listview structure
3653 * [I] nItem : item index
3655 * RETURN:
3656 * None
3658 static void LISTVIEW_SetSelection(LISTVIEW_INFO *infoPtr, INT nItem)
3660 LVITEMW lvItem;
3662 TRACE("nItem=%d\n", nItem);
3664 LISTVIEW_DeselectAllSkipItem(infoPtr, nItem);
3666 lvItem.state = LVIS_FOCUSED | LVIS_SELECTED;
3667 lvItem.stateMask = LVIS_FOCUSED | LVIS_SELECTED;
3668 LISTVIEW_SetItemState(infoPtr, nItem, &lvItem);
3670 infoPtr->nSelectionMark = nItem;
3673 /***
3674 * DESCRIPTION:
3675 * Set selection(s) with keyboard.
3677 * PARAMETER(S):
3678 * [I] infoPtr : valid pointer to the listview structure
3679 * [I] nItem : item index
3680 * [I] space : VK_SPACE code sent
3682 * RETURN:
3683 * SUCCESS : TRUE (needs to be repainted)
3684 * FAILURE : FALSE (nothing has changed)
3686 static BOOL LISTVIEW_KeySelection(LISTVIEW_INFO *infoPtr, INT nItem, BOOL space)
3688 /* FIXME: pass in the state */
3689 WORD wShift = GetKeyState(VK_SHIFT) & 0x8000;
3690 WORD wCtrl = GetKeyState(VK_CONTROL) & 0x8000;
3691 BOOL bResult = FALSE;
3693 TRACE("nItem=%d, wShift=%d, wCtrl=%d\n", nItem, wShift, wCtrl);
3694 if ((nItem >= 0) && (nItem < infoPtr->nItemCount))
3696 bResult = TRUE;
3698 if (infoPtr->dwStyle & LVS_SINGLESEL || (wShift == 0 && wCtrl == 0))
3699 LISTVIEW_SetSelection(infoPtr, nItem);
3700 else
3702 if (wShift)
3703 LISTVIEW_SetGroupSelection(infoPtr, nItem);
3704 else if (wCtrl)
3706 LVITEMW lvItem;
3707 lvItem.state = ~LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED);
3708 lvItem.stateMask = LVIS_SELECTED;
3709 if (space)
3711 LISTVIEW_SetItemState(infoPtr, nItem, &lvItem);
3712 if (lvItem.state & LVIS_SELECTED)
3713 infoPtr->nSelectionMark = nItem;
3715 bResult = LISTVIEW_SetItemFocus(infoPtr, nItem);
3718 LISTVIEW_EnsureVisible(infoPtr, nItem, FALSE);
3721 UpdateWindow(infoPtr->hwndSelf); /* update client area */
3722 return bResult;
3725 static BOOL LISTVIEW_GetItemAtPt(const LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem, POINT pt)
3727 LVHITTESTINFO lvHitTestInfo;
3729 ZeroMemory(&lvHitTestInfo, sizeof(lvHitTestInfo));
3730 lvHitTestInfo.pt.x = pt.x;
3731 lvHitTestInfo.pt.y = pt.y;
3733 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
3735 lpLVItem->mask = LVIF_PARAM;
3736 lpLVItem->iItem = lvHitTestInfo.iItem;
3737 lpLVItem->iSubItem = 0;
3739 return LISTVIEW_GetItemT(infoPtr, lpLVItem, TRUE);
3742 static inline BOOL LISTVIEW_IsHotTracking(const LISTVIEW_INFO *infoPtr)
3744 return ((infoPtr->dwLvExStyle & LVS_EX_TRACKSELECT) ||
3745 (infoPtr->dwLvExStyle & LVS_EX_ONECLICKACTIVATE) ||
3746 (infoPtr->dwLvExStyle & LVS_EX_TWOCLICKACTIVATE));
3749 /***
3750 * DESCRIPTION:
3751 * Called when the mouse is being actively tracked and has hovered for a specified
3752 * amount of time
3754 * PARAMETER(S):
3755 * [I] infoPtr : valid pointer to the listview structure
3756 * [I] fwKeys : key indicator
3757 * [I] x,y : mouse position
3759 * RETURN:
3760 * 0 if the message was processed, non-zero if there was an error
3762 * INFO:
3763 * LVS_EX_TRACKSELECT: An item is automatically selected when the cursor remains
3764 * over the item for a certain period of time.
3767 static LRESULT LISTVIEW_MouseHover(LISTVIEW_INFO *infoPtr, INT x, INT y)
3769 NMHDR hdr;
3771 if (notify_hdr(infoPtr, NM_HOVER, &hdr)) return 0;
3773 if (LISTVIEW_IsHotTracking(infoPtr))
3775 LVITEMW item;
3776 POINT pt;
3778 pt.x = x;
3779 pt.y = y;
3781 if (LISTVIEW_GetItemAtPt(infoPtr, &item, pt))
3782 LISTVIEW_SetSelection(infoPtr, item.iItem);
3784 SetFocus(infoPtr->hwndSelf);
3787 return 0;
3790 #define SCROLL_LEFT 0x1
3791 #define SCROLL_RIGHT 0x2
3792 #define SCROLL_UP 0x4
3793 #define SCROLL_DOWN 0x8
3795 /***
3796 * DESCRIPTION:
3797 * Utility routine to draw and highlight items within a marquee selection rectangle.
3799 * PARAMETER(S):
3800 * [I] infoPtr : valid pointer to the listview structure
3801 * [I] coords_orig : original co-ordinates of the cursor
3802 * [I] coords_offs : offsetted coordinates of the cursor
3803 * [I] offset : offset amount
3804 * [I] scroll : Bitmask of which directions we should scroll, if at all
3806 * RETURN:
3807 * None.
3809 static void LISTVIEW_MarqueeHighlight(LISTVIEW_INFO *infoPtr, const POINT *coords_orig,
3810 const POINT *coords_offs, const POINT *offset,
3811 INT scroll)
3813 BOOL controlDown = FALSE;
3814 LVITEMW item;
3815 ITERATOR old_elems, new_elems;
3816 RECT rect;
3818 if (coords_offs->x > infoPtr->marqueeOrigin.x)
3820 rect.left = infoPtr->marqueeOrigin.x;
3821 rect.right = coords_offs->x;
3823 else
3825 rect.left = coords_offs->x;
3826 rect.right = infoPtr->marqueeOrigin.x;
3829 if (coords_offs->y > infoPtr->marqueeOrigin.y)
3831 rect.top = infoPtr->marqueeOrigin.y;
3832 rect.bottom = coords_offs->y;
3834 else
3836 rect.top = coords_offs->y;
3837 rect.bottom = infoPtr->marqueeOrigin.y;
3840 /* Cancel out the old marquee rectangle and draw the new one */
3841 LISTVIEW_InvalidateRect(infoPtr, &infoPtr->marqueeDrawRect);
3843 /* Scroll by the appropriate distance if applicable - speed up scrolling as
3844 the cursor is further away */
3846 if ((scroll & SCROLL_LEFT) && (coords_orig->x <= 0))
3847 LISTVIEW_Scroll(infoPtr, coords_orig->x, 0);
3849 if ((scroll & SCROLL_RIGHT) && (coords_orig->x >= infoPtr->rcList.right))
3850 LISTVIEW_Scroll(infoPtr, (coords_orig->x - infoPtr->rcList.right), 0);
3852 if ((scroll & SCROLL_UP) && (coords_orig->y <= 0))
3853 LISTVIEW_Scroll(infoPtr, 0, coords_orig->y);
3855 if ((scroll & SCROLL_DOWN) && (coords_orig->y >= infoPtr->rcList.bottom))
3856 LISTVIEW_Scroll(infoPtr, 0, (coords_orig->y - infoPtr->rcList.bottom));
3858 iterator_frameditems_absolute(&old_elems, infoPtr, &infoPtr->marqueeRect);
3860 CopyRect(&infoPtr->marqueeRect, &rect);
3862 CopyRect(&infoPtr->marqueeDrawRect, &rect);
3863 OffsetRect(&infoPtr->marqueeDrawRect, offset->x, offset->y);
3865 iterator_frameditems_absolute(&new_elems, infoPtr, &infoPtr->marqueeRect);
3866 iterator_remove_common_items(&old_elems, &new_elems);
3868 /* Iterate over no longer selected items */
3869 while (iterator_next(&old_elems))
3871 if (old_elems.nItem > -1)
3873 if (LISTVIEW_GetItemState(infoPtr, old_elems.nItem, LVIS_SELECTED) == LVIS_SELECTED)
3874 item.state = 0;
3875 else
3876 item.state = LVIS_SELECTED;
3878 item.stateMask = LVIS_SELECTED;
3880 LISTVIEW_SetItemState(infoPtr, old_elems.nItem, &item);
3883 iterator_destroy(&old_elems);
3886 /* Iterate over newly selected items */
3887 if (GetKeyState(VK_CONTROL) & 0x8000)
3888 controlDown = TRUE;
3890 while (iterator_next(&new_elems))
3892 if (new_elems.nItem > -1)
3894 /* If CTRL is pressed, invert. If not, always select the item. */
3895 if ((controlDown) && (LISTVIEW_GetItemState(infoPtr, new_elems.nItem, LVIS_SELECTED)))
3896 item.state = 0;
3897 else
3898 item.state = LVIS_SELECTED;
3900 item.stateMask = LVIS_SELECTED;
3902 LISTVIEW_SetItemState(infoPtr, new_elems.nItem, &item);
3905 iterator_destroy(&new_elems);
3907 LISTVIEW_InvalidateRect(infoPtr, &infoPtr->marqueeDrawRect);
3910 /***
3911 * DESCRIPTION:
3912 * Called when we are in a marquee selection that involves scrolling the listview (ie,
3913 * the cursor is outside the bounds of the client area). This is a TIMERPROC.
3915 * PARAMETER(S):
3916 * [I] hwnd : Handle to the listview
3917 * [I] uMsg : WM_TIMER (ignored)
3918 * [I] idEvent : The timer ID interpreted as a pointer to a LISTVIEW_INFO struct
3919 * [I] dwTimer : The elapsed time (ignored)
3921 * RETURN:
3922 * None.
3924 static VOID CALLBACK LISTVIEW_ScrollTimer(HWND hWnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
3926 LISTVIEW_INFO *infoPtr;
3927 SCROLLINFO scrollInfo;
3928 POINT coords_orig;
3929 POINT coords_offs;
3930 POINT offset;
3931 INT scroll = 0;
3933 infoPtr = (LISTVIEW_INFO *) idEvent;
3935 if (!infoPtr)
3936 return;
3938 /* Get the current cursor position and convert to client coordinates */
3939 GetCursorPos(&coords_orig);
3940 ScreenToClient(hWnd, &coords_orig);
3942 /* Ensure coordinates are within client bounds */
3943 coords_offs.x = max(min(coords_orig.x, infoPtr->rcList.right), 0);
3944 coords_offs.y = max(min(coords_orig.y, infoPtr->rcList.bottom), 0);
3946 /* Get offset */
3947 LISTVIEW_GetOrigin(infoPtr, &offset);
3949 /* Offset coordinates by the appropriate amount */
3950 coords_offs.x -= offset.x;
3951 coords_offs.y -= offset.y;
3953 scrollInfo.cbSize = sizeof(SCROLLINFO);
3954 scrollInfo.fMask = SIF_ALL;
3956 /* Work out in which directions we can scroll */
3957 if (GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
3959 if (scrollInfo.nPos != scrollInfo.nMin)
3960 scroll |= SCROLL_UP;
3962 if (((scrollInfo.nPage + scrollInfo.nPos) - 1) != scrollInfo.nMax)
3963 scroll |= SCROLL_DOWN;
3966 if (GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo))
3968 if (scrollInfo.nPos != scrollInfo.nMin)
3969 scroll |= SCROLL_LEFT;
3971 if (((scrollInfo.nPage + scrollInfo.nPos) - 1) != scrollInfo.nMax)
3972 scroll |= SCROLL_RIGHT;
3975 if (((coords_orig.x <= 0) && (scroll & SCROLL_LEFT)) ||
3976 ((coords_orig.y <= 0) && (scroll & SCROLL_UP)) ||
3977 ((coords_orig.x >= infoPtr->rcList.right) && (scroll & SCROLL_RIGHT)) ||
3978 ((coords_orig.y >= infoPtr->rcList.bottom) && (scroll & SCROLL_DOWN)))
3980 LISTVIEW_MarqueeHighlight(infoPtr, &coords_orig, &coords_offs, &offset, scroll);
3984 /***
3985 * DESCRIPTION:
3986 * Called whenever WM_MOUSEMOVE is received.
3988 * PARAMETER(S):
3989 * [I] infoPtr : valid pointer to the listview structure
3990 * [I] fwKeys : key indicator
3991 * [I] x,y : mouse position
3993 * RETURN:
3994 * 0 if the message is processed, non-zero if there was an error
3996 static LRESULT LISTVIEW_MouseMove(LISTVIEW_INFO *infoPtr, WORD fwKeys, INT x, INT y)
3998 LVHITTESTINFO ht;
3999 RECT rect;
4000 POINT pt;
4002 if (!(fwKeys & MK_LBUTTON))
4003 infoPtr->bLButtonDown = FALSE;
4005 if (infoPtr->bLButtonDown)
4007 rect.left = rect.right = infoPtr->ptClickPos.x;
4008 rect.top = rect.bottom = infoPtr->ptClickPos.y;
4010 InflateRect(&rect, GetSystemMetrics(SM_CXDRAG), GetSystemMetrics(SM_CYDRAG));
4012 if (infoPtr->bMarqueeSelect)
4014 POINT coords_orig;
4015 POINT coords_offs;
4016 POINT offset;
4018 coords_orig.x = x;
4019 coords_orig.y = y;
4021 /* Get offset */
4022 LISTVIEW_GetOrigin(infoPtr, &offset);
4024 /* Ensure coordinates are within client bounds */
4025 coords_offs.x = max(min(x, infoPtr->rcList.right), 0);
4026 coords_offs.y = max(min(y, infoPtr->rcList.bottom), 0);
4028 /* Offset coordinates by the appropriate amount */
4029 coords_offs.x -= offset.x;
4030 coords_offs.y -= offset.y;
4032 /* Enable the timer if we're going outside our bounds, in case the user doesn't
4033 move the mouse again */
4035 if ((x <= 0) || (y <= 0) || (x >= infoPtr->rcList.right) ||
4036 (y >= infoPtr->rcList.bottom))
4038 if (!infoPtr->bScrolling)
4040 infoPtr->bScrolling = TRUE;
4041 SetTimer(infoPtr->hwndSelf, (UINT_PTR) infoPtr, 1, LISTVIEW_ScrollTimer);
4044 else
4046 infoPtr->bScrolling = FALSE;
4047 KillTimer(infoPtr->hwndSelf, (UINT_PTR) infoPtr);
4050 LISTVIEW_MarqueeHighlight(infoPtr, &coords_orig, &coords_offs, &offset, 0);
4051 return 0;
4054 pt.x = x;
4055 pt.y = y;
4057 ht.pt = pt;
4058 LISTVIEW_HitTest(infoPtr, &ht, TRUE, TRUE);
4060 /* reset item marker */
4061 if (infoPtr->nLButtonDownItem != ht.iItem)
4062 infoPtr->nLButtonDownItem = -1;
4064 if (!PtInRect(&rect, pt))
4066 /* this path covers the following:
4067 1. WM_LBUTTONDOWN over selected item (sets focus on it)
4068 2. change focus with keys
4069 3. move mouse over item from step 1 selects it and moves focus on it */
4070 if (infoPtr->nLButtonDownItem != -1 &&
4071 !LISTVIEW_GetItemState(infoPtr, infoPtr->nLButtonDownItem, LVIS_SELECTED))
4073 LVITEMW lvItem;
4075 lvItem.state = LVIS_FOCUSED | LVIS_SELECTED;
4076 lvItem.stateMask = LVIS_FOCUSED | LVIS_SELECTED;
4078 LISTVIEW_SetItemState(infoPtr, infoPtr->nLButtonDownItem, &lvItem);
4079 infoPtr->nLButtonDownItem = -1;
4082 if (!infoPtr->bDragging)
4084 ht.pt = infoPtr->ptClickPos;
4085 LISTVIEW_HitTest(infoPtr, &ht, TRUE, TRUE);
4087 /* If the click is outside the range of an item, begin a
4088 highlight. If not, begin an item drag. */
4089 if (ht.iItem == -1)
4091 NMHDR hdr;
4093 /* If we're allowing multiple selections, send notification.
4094 If return value is non-zero, cancel. */
4095 if (!(infoPtr->dwStyle & LVS_SINGLESEL) && (notify_hdr(infoPtr, LVN_MARQUEEBEGIN, &hdr) == 0))
4097 /* Store the absolute coordinates of the click */
4098 POINT offset;
4099 LISTVIEW_GetOrigin(infoPtr, &offset);
4101 infoPtr->marqueeOrigin.x = infoPtr->ptClickPos.x - offset.x;
4102 infoPtr->marqueeOrigin.y = infoPtr->ptClickPos.y - offset.y;
4104 /* Begin selection and capture mouse */
4105 infoPtr->bMarqueeSelect = TRUE;
4106 SetCapture(infoPtr->hwndSelf);
4109 else
4111 NMLISTVIEW nmlv;
4113 ZeroMemory(&nmlv, sizeof(nmlv));
4114 nmlv.iItem = ht.iItem;
4115 nmlv.ptAction = infoPtr->ptClickPos;
4117 notify_listview(infoPtr, LVN_BEGINDRAG, &nmlv);
4118 infoPtr->bDragging = TRUE;
4122 return 0;
4126 /* see if we are supposed to be tracking mouse hovering */
4127 if (LISTVIEW_IsHotTracking(infoPtr)) {
4128 TRACKMOUSEEVENT trackinfo;
4129 DWORD flags;
4131 trackinfo.cbSize = sizeof(TRACKMOUSEEVENT);
4132 trackinfo.dwFlags = TME_QUERY;
4134 /* see if we are already tracking this hwnd */
4135 _TrackMouseEvent(&trackinfo);
4137 flags = TME_LEAVE;
4138 if(infoPtr->dwLvExStyle & LVS_EX_TRACKSELECT)
4139 flags |= TME_HOVER;
4141 if((trackinfo.dwFlags & flags) != flags || trackinfo.hwndTrack != infoPtr->hwndSelf) {
4142 trackinfo.dwFlags = flags;
4143 trackinfo.dwHoverTime = infoPtr->dwHoverTime;
4144 trackinfo.hwndTrack = infoPtr->hwndSelf;
4146 /* call TRACKMOUSEEVENT so we receive WM_MOUSEHOVER messages */
4147 _TrackMouseEvent(&trackinfo);
4151 return 0;
4155 /***
4156 * Tests whether the item is assignable to a list with style lStyle
4158 static inline BOOL is_assignable_item(const LVITEMW *lpLVItem, LONG lStyle)
4160 if ( (lpLVItem->mask & LVIF_TEXT) &&
4161 (lpLVItem->pszText == LPSTR_TEXTCALLBACKW) &&
4162 (lStyle & (LVS_SORTASCENDING | LVS_SORTDESCENDING)) ) return FALSE;
4164 return TRUE;
4168 /***
4169 * DESCRIPTION:
4170 * Helper for LISTVIEW_SetItemT and LISTVIEW_InsertItemT: sets item attributes.
4172 * PARAMETER(S):
4173 * [I] infoPtr : valid pointer to the listview structure
4174 * [I] lpLVItem : valid pointer to new item attributes
4175 * [I] isNew : the item being set is being inserted
4176 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
4177 * [O] bChanged : will be set to TRUE if the item really changed
4179 * RETURN:
4180 * SUCCESS : TRUE
4181 * FAILURE : FALSE
4183 static BOOL set_main_item(LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem, BOOL isNew, BOOL isW, BOOL *bChanged)
4185 ITEM_INFO *lpItem;
4186 NMLISTVIEW nmlv;
4187 UINT uChanged = 0;
4188 LVITEMW item;
4189 /* stateMask is ignored for LVM_INSERTITEM */
4190 UINT stateMask = isNew ? ~0 : lpLVItem->stateMask;
4192 TRACE("()\n");
4194 assert(lpLVItem->iItem >= 0 && lpLVItem->iItem < infoPtr->nItemCount);
4196 if (lpLVItem->mask == 0) return TRUE;
4198 if (infoPtr->dwStyle & LVS_OWNERDATA)
4200 /* a virtual listview only stores selection and focus */
4201 if (lpLVItem->mask & ~LVIF_STATE)
4202 return FALSE;
4203 lpItem = NULL;
4205 else
4207 HDPA hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, lpLVItem->iItem);
4208 lpItem = DPA_GetPtr(hdpaSubItems, 0);
4209 assert (lpItem);
4212 /* we need to get the lParam and state of the item */
4213 item.iItem = lpLVItem->iItem;
4214 item.iSubItem = lpLVItem->iSubItem;
4215 item.mask = LVIF_STATE | LVIF_PARAM;
4216 item.stateMask = (infoPtr->dwStyle & LVS_OWNERDATA) ? LVIS_FOCUSED | LVIS_SELECTED : ~0;
4218 item.state = 0;
4219 item.lParam = 0;
4220 if (!isNew && !LISTVIEW_GetItemW(infoPtr, &item)) return FALSE;
4222 TRACE("oldState=%x, newState=%x\n", item.state, lpLVItem->state);
4223 /* determine what fields will change */
4224 if ((lpLVItem->mask & LVIF_STATE) && ((item.state ^ lpLVItem->state) & stateMask & ~infoPtr->uCallbackMask))
4225 uChanged |= LVIF_STATE;
4227 if ((lpLVItem->mask & LVIF_IMAGE) && (lpItem->hdr.iImage != lpLVItem->iImage))
4228 uChanged |= LVIF_IMAGE;
4230 if ((lpLVItem->mask & LVIF_PARAM) && (lpItem->lParam != lpLVItem->lParam))
4231 uChanged |= LVIF_PARAM;
4233 if ((lpLVItem->mask & LVIF_INDENT) && (lpItem->iIndent != lpLVItem->iIndent))
4234 uChanged |= LVIF_INDENT;
4236 if ((lpLVItem->mask & LVIF_TEXT) && textcmpWT(lpItem->hdr.pszText, lpLVItem->pszText, isW))
4237 uChanged |= LVIF_TEXT;
4239 TRACE("change mask=0x%x\n", uChanged);
4241 memset(&nmlv, 0, sizeof(NMLISTVIEW));
4242 nmlv.iItem = lpLVItem->iItem;
4243 nmlv.uNewState = (item.state & ~stateMask) | (lpLVItem->state & stateMask);
4244 nmlv.uOldState = item.state;
4245 nmlv.uChanged = uChanged ? uChanged : lpLVItem->mask;
4246 nmlv.lParam = item.lParam;
4248 /* Send LVN_ITEMCHANGING notification, if the item is not being inserted
4249 and we are _NOT_ virtual (LVS_OWNERDATA), and change notifications
4250 are enabled. Even nothing really changed we still need to send this,
4251 in this case uChanged mask is just set to passed item mask. */
4252 if(lpItem && !isNew && infoPtr->bDoChangeNotify)
4254 HWND hwndSelf = infoPtr->hwndSelf;
4256 if (notify_listview(infoPtr, LVN_ITEMCHANGING, &nmlv))
4257 return FALSE;
4258 if (!IsWindow(hwndSelf))
4259 return FALSE;
4262 /* When item is inserted we need to shift existing focus index if new item has lower index. */
4263 if (isNew && (stateMask & ~infoPtr->uCallbackMask & LVIS_FOCUSED) &&
4264 /* this means we won't hit a focus change path later */
4265 ((uChanged & LVIF_STATE) == 0 || (!(lpLVItem->state & LVIS_FOCUSED) && (infoPtr->nFocusedItem != lpLVItem->iItem))))
4267 if (infoPtr->nFocusedItem != -1 && (lpLVItem->iItem <= infoPtr->nFocusedItem))
4268 infoPtr->nFocusedItem++;
4271 if (!uChanged) return TRUE;
4272 *bChanged = TRUE;
4274 /* copy information */
4275 if (lpLVItem->mask & LVIF_TEXT)
4276 textsetptrT(&lpItem->hdr.pszText, lpLVItem->pszText, isW);
4278 if (lpLVItem->mask & LVIF_IMAGE)
4279 lpItem->hdr.iImage = lpLVItem->iImage;
4281 if (lpLVItem->mask & LVIF_PARAM)
4282 lpItem->lParam = lpLVItem->lParam;
4284 if (lpLVItem->mask & LVIF_INDENT)
4285 lpItem->iIndent = lpLVItem->iIndent;
4287 if (uChanged & LVIF_STATE)
4289 if (lpItem && (stateMask & ~infoPtr->uCallbackMask))
4291 lpItem->state &= ~stateMask;
4292 lpItem->state |= (lpLVItem->state & stateMask);
4294 if (lpLVItem->state & stateMask & ~infoPtr->uCallbackMask & LVIS_SELECTED)
4296 if (infoPtr->dwStyle & LVS_SINGLESEL) LISTVIEW_DeselectAllSkipItem(infoPtr, lpLVItem->iItem);
4297 ranges_additem(infoPtr->selectionRanges, lpLVItem->iItem);
4299 else if (stateMask & LVIS_SELECTED)
4301 ranges_delitem(infoPtr->selectionRanges, lpLVItem->iItem);
4303 /* If we are asked to change focus, and we manage it, do it.
4304 It's important to have all new item data stored at this point,
4305 because changing existing focus could result in a redrawing operation,
4306 which in turn could ask for disp data, application should see all data
4307 for inserted item when processing LVN_GETDISPINFO.
4309 The way this works application will see nested item change notifications -
4310 changed item notifications interrupted by ones from item losing focus. */
4311 if (stateMask & ~infoPtr->uCallbackMask & LVIS_FOCUSED)
4313 if (lpLVItem->state & LVIS_FOCUSED)
4315 /* update selection mark */
4316 if (infoPtr->nFocusedItem == -1 && infoPtr->nSelectionMark == -1)
4317 infoPtr->nSelectionMark = lpLVItem->iItem;
4319 if (infoPtr->nFocusedItem != -1)
4321 /* remove current focus */
4322 item.mask = LVIF_STATE;
4323 item.state = 0;
4324 item.stateMask = LVIS_FOCUSED;
4326 /* recurse with redrawing an item */
4327 LISTVIEW_SetItemState(infoPtr, infoPtr->nFocusedItem, &item);
4330 infoPtr->nFocusedItem = lpLVItem->iItem;
4331 LISTVIEW_EnsureVisible(infoPtr, lpLVItem->iItem, infoPtr->uView == LV_VIEW_LIST);
4333 else if (infoPtr->nFocusedItem == lpLVItem->iItem)
4335 infoPtr->nFocusedItem = -1;
4340 /* if we're inserting the item, we're done */
4341 if (isNew) return TRUE;
4343 /* send LVN_ITEMCHANGED notification */
4344 if (lpLVItem->mask & LVIF_PARAM) nmlv.lParam = lpLVItem->lParam;
4345 if (infoPtr->bDoChangeNotify) notify_listview(infoPtr, LVN_ITEMCHANGED, &nmlv);
4347 return TRUE;
4350 /***
4351 * DESCRIPTION:
4352 * Helper for LISTVIEW_{Set,Insert}ItemT *only*: sets subitem attributes.
4354 * PARAMETER(S):
4355 * [I] infoPtr : valid pointer to the listview structure
4356 * [I] lpLVItem : valid pointer to new subitem attributes
4357 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
4358 * [O] bChanged : will be set to TRUE if the item really changed
4360 * RETURN:
4361 * SUCCESS : TRUE
4362 * FAILURE : FALSE
4364 static BOOL set_sub_item(const LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem, BOOL isW, BOOL *bChanged)
4366 HDPA hdpaSubItems;
4367 SUBITEM_INFO *lpSubItem;
4369 /* we do not support subitems for virtual listviews */
4370 if (infoPtr->dwStyle & LVS_OWNERDATA) return FALSE;
4372 /* set subitem only if column is present */
4373 if (lpLVItem->iSubItem >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
4375 /* First do some sanity checks */
4376 /* The LVIF_STATE flag is valid for subitems, but does not appear to be
4377 particularly useful. We currently do not actually do anything with
4378 the flag on subitems.
4380 if (lpLVItem->mask & ~(LVIF_TEXT | LVIF_IMAGE | LVIF_STATE | LVIF_DI_SETITEM)) return FALSE;
4381 if (!(lpLVItem->mask & (LVIF_TEXT | LVIF_IMAGE | LVIF_STATE))) return TRUE;
4383 /* get the subitem structure, and create it if not there */
4384 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, lpLVItem->iItem);
4385 assert (hdpaSubItems);
4387 lpSubItem = LISTVIEW_GetSubItemPtr(hdpaSubItems, lpLVItem->iSubItem);
4388 if (!lpSubItem)
4390 SUBITEM_INFO *tmpSubItem;
4391 INT i;
4393 lpSubItem = Alloc(sizeof(SUBITEM_INFO));
4394 if (!lpSubItem) return FALSE;
4395 /* we could binary search here, if need be...*/
4396 for (i = 1; i < DPA_GetPtrCount(hdpaSubItems); i++)
4398 tmpSubItem = DPA_GetPtr(hdpaSubItems, i);
4399 if (tmpSubItem->iSubItem > lpLVItem->iSubItem) break;
4401 if (DPA_InsertPtr(hdpaSubItems, i, lpSubItem) == -1)
4403 Free(lpSubItem);
4404 return FALSE;
4406 lpSubItem->iSubItem = lpLVItem->iSubItem;
4407 lpSubItem->hdr.iImage = I_IMAGECALLBACK;
4408 *bChanged = TRUE;
4411 if ((lpLVItem->mask & LVIF_IMAGE) && (lpSubItem->hdr.iImage != lpLVItem->iImage))
4413 lpSubItem->hdr.iImage = lpLVItem->iImage;
4414 *bChanged = TRUE;
4417 if ((lpLVItem->mask & LVIF_TEXT) && textcmpWT(lpSubItem->hdr.pszText, lpLVItem->pszText, isW))
4419 textsetptrT(&lpSubItem->hdr.pszText, lpLVItem->pszText, isW);
4420 *bChanged = TRUE;
4423 return TRUE;
4426 /***
4427 * DESCRIPTION:
4428 * Sets item attributes.
4430 * PARAMETER(S):
4431 * [I] infoPtr : valid pointer to the listview structure
4432 * [I] lpLVItem : new item attributes
4433 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
4435 * RETURN:
4436 * SUCCESS : TRUE
4437 * FAILURE : FALSE
4439 static BOOL LISTVIEW_SetItemT(LISTVIEW_INFO *infoPtr, LVITEMW *lpLVItem, BOOL isW)
4441 HWND hwndSelf = infoPtr->hwndSelf;
4442 LPWSTR pszText = NULL;
4443 BOOL bResult, bChanged = FALSE;
4444 RECT oldItemArea;
4446 TRACE("(lpLVItem=%s, isW=%d)\n", debuglvitem_t(lpLVItem, isW), isW);
4448 if (!lpLVItem || lpLVItem->iItem < 0 || lpLVItem->iItem >= infoPtr->nItemCount)
4449 return FALSE;
4451 /* Store old item area */
4452 LISTVIEW_GetItemBox(infoPtr, lpLVItem->iItem, &oldItemArea);
4454 /* For efficiency, we transform the lpLVItem->pszText to Unicode here */
4455 if ((lpLVItem->mask & LVIF_TEXT) && is_text(lpLVItem->pszText))
4457 pszText = lpLVItem->pszText;
4458 lpLVItem->pszText = textdupTtoW(lpLVItem->pszText, isW);
4461 /* actually set the fields */
4462 if (!is_assignable_item(lpLVItem, infoPtr->dwStyle)) return FALSE;
4464 if (lpLVItem->iSubItem)
4465 bResult = set_sub_item(infoPtr, lpLVItem, TRUE, &bChanged);
4466 else
4467 bResult = set_main_item(infoPtr, lpLVItem, FALSE, TRUE, &bChanged);
4468 if (!IsWindow(hwndSelf))
4469 return FALSE;
4471 /* redraw item, if necessary */
4472 if (bChanged && !infoPtr->bIsDrawing)
4474 /* this little optimization eliminates some nasty flicker */
4475 if ( infoPtr->uView == LV_VIEW_DETAILS && !(infoPtr->dwStyle & LVS_OWNERDRAWFIXED) &&
4476 !(infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) &&
4477 lpLVItem->iSubItem > 0 && lpLVItem->iSubItem <= DPA_GetPtrCount(infoPtr->hdpaColumns) )
4478 LISTVIEW_InvalidateSubItem(infoPtr, lpLVItem->iItem, lpLVItem->iSubItem);
4479 else
4481 LISTVIEW_InvalidateRect(infoPtr, &oldItemArea);
4482 LISTVIEW_InvalidateItem(infoPtr, lpLVItem->iItem);
4485 /* restore text */
4486 if (pszText)
4488 textfreeT(lpLVItem->pszText, isW);
4489 lpLVItem->pszText = pszText;
4492 return bResult;
4495 /***
4496 * DESCRIPTION:
4497 * Retrieves the index of the item at coordinate (0, 0) of the client area.
4499 * PARAMETER(S):
4500 * [I] infoPtr : valid pointer to the listview structure
4502 * RETURN:
4503 * item index
4505 static INT LISTVIEW_GetTopIndex(const LISTVIEW_INFO *infoPtr)
4507 INT nItem = 0;
4508 SCROLLINFO scrollInfo;
4510 scrollInfo.cbSize = sizeof(SCROLLINFO);
4511 scrollInfo.fMask = SIF_POS;
4513 if (infoPtr->uView == LV_VIEW_LIST)
4515 if (GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo))
4516 nItem = scrollInfo.nPos * LISTVIEW_GetCountPerColumn(infoPtr);
4518 else if (infoPtr->uView == LV_VIEW_DETAILS)
4520 if (GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
4521 nItem = scrollInfo.nPos;
4523 else
4525 if (GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
4526 nItem = LISTVIEW_GetCountPerRow(infoPtr) * (scrollInfo.nPos / infoPtr->nItemHeight);
4529 TRACE("nItem=%d\n", nItem);
4531 return nItem;
4535 /***
4536 * DESCRIPTION:
4537 * Erases the background of the given rectangle
4539 * PARAMETER(S):
4540 * [I] infoPtr : valid pointer to the listview structure
4541 * [I] hdc : device context handle
4542 * [I] lprcBox : clipping rectangle
4544 * RETURN:
4545 * Success: TRUE
4546 * Failure: FALSE
4548 static inline BOOL LISTVIEW_FillBkgnd(const LISTVIEW_INFO *infoPtr, HDC hdc, const RECT *lprcBox)
4550 if (!infoPtr->hBkBrush) return FALSE;
4552 TRACE("(hdc=%p, lprcBox=%s, hBkBrush=%p)\n", hdc, wine_dbgstr_rect(lprcBox), infoPtr->hBkBrush);
4554 return FillRect(hdc, lprcBox, infoPtr->hBkBrush);
4557 /* Draw main item or subitem */
4558 static void LISTVIEW_DrawItemPart(LISTVIEW_INFO *infoPtr, LVITEMW *item, const NMLVCUSTOMDRAW *nmlvcd, const POINT *pos)
4560 RECT rcSelect, rcLabel, rcBox, rcStateIcon, rcIcon;
4561 const RECT *background;
4562 HIMAGELIST himl;
4563 UINT format;
4564 RECT *focus;
4566 /* now check if we need to update the focus rectangle */
4567 focus = infoPtr->bFocus && (item->state & LVIS_FOCUSED) ? &infoPtr->rcFocus : 0;
4568 if (!focus) item->state &= ~LVIS_FOCUSED;
4570 LISTVIEW_GetItemMetrics(infoPtr, item, &rcBox, &rcSelect, &rcIcon, &rcStateIcon, &rcLabel);
4571 OffsetRect(&rcBox, pos->x, pos->y);
4572 OffsetRect(&rcSelect, pos->x, pos->y);
4573 OffsetRect(&rcIcon, pos->x, pos->y);
4574 OffsetRect(&rcStateIcon, pos->x, pos->y);
4575 OffsetRect(&rcLabel, pos->x, pos->y);
4576 TRACE("%d: box=%s, select=%s, icon=%s. label=%s\n", item->iSubItem,
4577 wine_dbgstr_rect(&rcBox), wine_dbgstr_rect(&rcSelect),
4578 wine_dbgstr_rect(&rcIcon), wine_dbgstr_rect(&rcLabel));
4580 /* FIXME: temporary hack */
4581 rcSelect.left = rcLabel.left;
4583 if (infoPtr->uView == LV_VIEW_DETAILS && item->iSubItem == 0)
4585 if (!(infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT))
4586 OffsetRect(&rcSelect, LISTVIEW_GetColumnInfo(infoPtr, 0)->rcHeader.left, 0);
4587 OffsetRect(&rcIcon, LISTVIEW_GetColumnInfo(infoPtr, 0)->rcHeader.left, 0);
4588 OffsetRect(&rcStateIcon, LISTVIEW_GetColumnInfo(infoPtr, 0)->rcHeader.left, 0);
4589 OffsetRect(&rcLabel, LISTVIEW_GetColumnInfo(infoPtr, 0)->rcHeader.left, 0);
4592 /* in icon mode, the label rect is really what we want to draw the
4593 * background for */
4594 /* in detail mode, we want to paint background for label rect when
4595 * item is not selected or listview has full row select; otherwise paint
4596 * background for text only */
4597 if ( infoPtr->uView == LV_VIEW_ICON ||
4598 (infoPtr->uView == LV_VIEW_DETAILS && (!(item->state & LVIS_SELECTED) ||
4599 (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT))))
4600 background = &rcLabel;
4601 else
4602 background = &rcSelect;
4604 if (nmlvcd->clrTextBk != CLR_NONE)
4605 ExtTextOutW(nmlvcd->nmcd.hdc, background->left, background->top, ETO_OPAQUE, background, NULL, 0, NULL);
4607 if (item->state & LVIS_FOCUSED)
4609 if (infoPtr->uView == LV_VIEW_DETAILS)
4611 if (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT)
4613 /* we have to update left focus bound too if item isn't in leftmost column
4614 and reduce right box bound */
4615 if (DPA_GetPtrCount(infoPtr->hdpaColumns) > 0)
4617 INT leftmost;
4619 if ((leftmost = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, 0, 0)))
4621 INT Originx = pos->x - LISTVIEW_GetColumnInfo(infoPtr, leftmost)->rcHeader.left;
4622 INT rightmost = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX,
4623 DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, 0);
4625 rcBox.right = LISTVIEW_GetColumnInfo(infoPtr, rightmost)->rcHeader.right + Originx;
4626 rcSelect.left = LISTVIEW_GetColumnInfo(infoPtr, leftmost)->rcHeader.left + Originx;
4629 rcSelect.right = rcBox.right;
4631 infoPtr->rcFocus = rcSelect;
4633 else
4634 infoPtr->rcFocus = rcLabel;
4637 /* state icons */
4638 if (infoPtr->himlState && STATEIMAGEINDEX(item->state) && (item->iSubItem == 0))
4640 UINT stateimage = STATEIMAGEINDEX(item->state);
4641 if (stateimage)
4643 TRACE("stateimage=%d\n", stateimage);
4644 ImageList_Draw(infoPtr->himlState, stateimage-1, nmlvcd->nmcd.hdc, rcStateIcon.left, rcStateIcon.top, ILD_NORMAL);
4648 /* item icons */
4649 himl = (infoPtr->uView == LV_VIEW_ICON ? infoPtr->himlNormal : infoPtr->himlSmall);
4650 if (himl && item->iImage >= 0 && !IsRectEmpty(&rcIcon))
4652 UINT style;
4654 TRACE("iImage=%d\n", item->iImage);
4656 if (item->state & (LVIS_SELECTED | LVIS_CUT) && infoPtr->bFocus)
4657 style = ILD_SELECTED;
4658 else
4659 style = ILD_NORMAL;
4661 ImageList_DrawEx(himl, item->iImage, nmlvcd->nmcd.hdc, rcIcon.left, rcIcon.top,
4662 rcIcon.right - rcIcon.left, rcIcon.bottom - rcIcon.top, infoPtr->clrBk,
4663 item->state & LVIS_CUT ? RGB(255, 255, 255) : CLR_DEFAULT,
4664 style | (item->state & LVIS_OVERLAYMASK));
4667 /* Don't bother painting item being edited */
4668 if (infoPtr->hwndEdit && item->iItem == infoPtr->nEditLabelItem && item->iSubItem == 0) return;
4670 /* figure out the text drawing flags */
4671 format = (infoPtr->uView == LV_VIEW_ICON ? (focus ? LV_FL_DT_FLAGS : LV_ML_DT_FLAGS) : LV_SL_DT_FLAGS);
4672 if (infoPtr->uView == LV_VIEW_ICON)
4673 format = (focus ? LV_FL_DT_FLAGS : LV_ML_DT_FLAGS);
4674 else if (item->iSubItem)
4676 switch (LISTVIEW_GetColumnInfo(infoPtr, item->iSubItem)->fmt & LVCFMT_JUSTIFYMASK)
4678 case LVCFMT_RIGHT: format |= DT_RIGHT; break;
4679 case LVCFMT_CENTER: format |= DT_CENTER; break;
4680 default: format |= DT_LEFT;
4683 if (!(format & (DT_RIGHT | DT_CENTER)))
4685 if (himl && item->iImage >= 0 && !IsRectEmpty(&rcIcon)) rcLabel.left += IMAGE_PADDING;
4686 else rcLabel.left += LABEL_HOR_PADDING;
4688 else if (format & DT_RIGHT) rcLabel.right -= LABEL_HOR_PADDING;
4690 /* for GRIDLINES reduce the bottom so the text formats correctly */
4691 if (infoPtr->uView == LV_VIEW_DETAILS && infoPtr->dwLvExStyle & LVS_EX_GRIDLINES)
4692 rcLabel.bottom--;
4694 DrawTextW(nmlvcd->nmcd.hdc, item->pszText, -1, &rcLabel, format);
4697 /***
4698 * DESCRIPTION:
4699 * Draws an item.
4701 * PARAMETER(S):
4702 * [I] infoPtr : valid pointer to the listview structure
4703 * [I] hdc : device context handle
4704 * [I] nItem : item index
4705 * [I] nSubItem : subitem index
4706 * [I] pos : item position in client coordinates
4707 * [I] cdmode : custom draw mode
4709 * RETURN:
4710 * Success: TRUE
4711 * Failure: FALSE
4713 static BOOL LISTVIEW_DrawItem(LISTVIEW_INFO *infoPtr, HDC hdc, INT nItem, ITERATOR *subitems, POINT pos, DWORD cdmode)
4715 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
4716 static WCHAR callbackW[] = { '(', 'c', 'a', 'l', 'l', 'b', 'a', 'c', 'k', ')', 0 };
4717 DWORD cdsubitemmode = CDRF_DODEFAULT;
4718 RECT *focus, rcBox;
4719 NMLVCUSTOMDRAW nmlvcd;
4720 LVITEMW lvItem;
4722 TRACE("(hdc=%p, nItem=%d, subitems=%p, pos=%s)\n", hdc, nItem, subitems, wine_dbgstr_point(&pos));
4724 /* get information needed for drawing the item */
4725 lvItem.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_PARAM | LVIF_STATE;
4726 if (infoPtr->uView == LV_VIEW_DETAILS) lvItem.mask |= LVIF_INDENT;
4727 lvItem.stateMask = LVIS_SELECTED | LVIS_FOCUSED | LVIS_STATEIMAGEMASK | LVIS_CUT | LVIS_OVERLAYMASK;
4728 lvItem.iItem = nItem;
4729 lvItem.iSubItem = 0;
4730 lvItem.state = 0;
4731 lvItem.lParam = 0;
4732 lvItem.cchTextMax = DISP_TEXT_SIZE;
4733 lvItem.pszText = szDispText;
4734 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return FALSE;
4735 if (lvItem.pszText == LPSTR_TEXTCALLBACKW) lvItem.pszText = callbackW;
4736 TRACE(" lvItem=%s\n", debuglvitem_t(&lvItem, TRUE));
4738 /* now check if we need to update the focus rectangle */
4739 focus = infoPtr->bFocus && (lvItem.state & LVIS_FOCUSED) ? &infoPtr->rcFocus : 0;
4740 if (!focus) lvItem.state &= ~LVIS_FOCUSED;
4742 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, &rcBox, NULL, NULL, NULL, NULL);
4743 OffsetRect(&rcBox, pos.x, pos.y);
4745 /* Full custom draw stage sequence looks like this:
4747 LV_VIEW_DETAILS:
4749 - CDDS_ITEMPREPAINT
4750 - CDDS_ITEMPREPAINT|CDDS_SUBITEM | => sent n times, where n is number of subitems,
4751 CDDS_ITEMPOSTPAINT|CDDS_SUBITEM | including item iself
4752 - CDDS_ITEMPOSTPAINT
4754 other styles:
4756 - CDDS_ITEMPREPAINT
4757 - CDDS_ITEMPOSTPAINT
4760 /* fill in the custom draw structure */
4761 customdraw_fill(&nmlvcd, infoPtr, hdc, &rcBox, &lvItem);
4762 if (cdmode & CDRF_NOTIFYITEMDRAW)
4763 cdsubitemmode = notify_customdraw(infoPtr, CDDS_ITEMPREPAINT, &nmlvcd);
4764 if (cdsubitemmode & CDRF_SKIPDEFAULT) goto postpaint;
4766 if (subitems)
4768 while (iterator_next(subitems))
4770 DWORD subitemstage = CDRF_DODEFAULT;
4772 /* We need to query for each subitem, item's data (subitem == 0) is already here at this point */
4773 if (subitems->nItem)
4775 lvItem.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_PARAM | LVIF_INDENT;
4776 lvItem.stateMask = LVIS_SELECTED | LVIS_FOCUSED | LVIS_STATEIMAGEMASK | LVIS_CUT | LVIS_OVERLAYMASK;
4777 lvItem.iItem = nItem;
4778 lvItem.iSubItem = subitems->nItem;
4779 lvItem.state = 0;
4780 lvItem.lParam = 0;
4781 lvItem.cchTextMax = DISP_TEXT_SIZE;
4782 lvItem.pszText = szDispText;
4783 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return FALSE;
4784 if (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT)
4785 lvItem.state = LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED);
4786 if (lvItem.pszText == LPSTR_TEXTCALLBACKW) lvItem.pszText = callbackW;
4787 TRACE(" lvItem=%s\n", debuglvitem_t(&lvItem, TRUE));
4789 /* update custom draw data */
4790 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, &nmlvcd.nmcd.rc, NULL, NULL, NULL, NULL);
4791 OffsetRect(&nmlvcd.nmcd.rc, pos.x, pos.y);
4792 nmlvcd.iSubItem = subitems->nItem;
4795 if (cdsubitemmode & CDRF_NOTIFYSUBITEMDRAW)
4796 subitemstage = notify_customdraw(infoPtr, CDDS_SUBITEM | CDDS_ITEMPREPAINT, &nmlvcd);
4797 else
4799 nmlvcd.clrTextBk = infoPtr->clrTextBk;
4800 nmlvcd.clrText = infoPtr->clrText;
4803 if (subitems->nItem == 0 || (cdmode & CDRF_NOTIFYITEMDRAW))
4804 prepaint_setup(infoPtr, hdc, &nmlvcd, FALSE);
4805 else if (!(infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT))
4806 prepaint_setup(infoPtr, hdc, &nmlvcd, TRUE);
4808 if (!(subitemstage & CDRF_SKIPDEFAULT))
4809 LISTVIEW_DrawItemPart(infoPtr, &lvItem, &nmlvcd, &pos);
4811 if (subitemstage & CDRF_NOTIFYPOSTPAINT)
4812 subitemstage = notify_customdraw(infoPtr, CDDS_SUBITEM | CDDS_ITEMPOSTPAINT, &nmlvcd);
4815 else
4817 prepaint_setup(infoPtr, hdc, &nmlvcd, FALSE);
4818 LISTVIEW_DrawItemPart(infoPtr, &lvItem, &nmlvcd, &pos);
4821 postpaint:
4822 if (cdsubitemmode & CDRF_NOTIFYPOSTPAINT)
4824 nmlvcd.iSubItem = 0;
4825 notify_customdraw(infoPtr, CDDS_ITEMPOSTPAINT, &nmlvcd);
4828 return TRUE;
4831 /***
4832 * DESCRIPTION:
4833 * Draws listview items when in owner draw mode.
4835 * PARAMETER(S):
4836 * [I] infoPtr : valid pointer to the listview structure
4837 * [I] hdc : device context handle
4839 * RETURN:
4840 * None
4842 static void LISTVIEW_RefreshOwnerDraw(const LISTVIEW_INFO *infoPtr, ITERATOR *i, HDC hdc, DWORD cdmode)
4844 UINT uID = (UINT)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
4845 DWORD cditemmode = CDRF_DODEFAULT;
4846 NMLVCUSTOMDRAW nmlvcd;
4847 POINT Origin, Position;
4848 DRAWITEMSTRUCT dis;
4849 LVITEMW item;
4851 TRACE("()\n");
4853 ZeroMemory(&dis, sizeof(dis));
4855 /* Get scroll info once before loop */
4856 LISTVIEW_GetOrigin(infoPtr, &Origin);
4858 /* iterate through the invalidated rows */
4859 while(iterator_next(i))
4861 item.iItem = i->nItem;
4862 item.iSubItem = 0;
4863 item.mask = LVIF_PARAM | LVIF_STATE;
4864 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
4865 if (!LISTVIEW_GetItemW(infoPtr, &item)) continue;
4867 dis.CtlType = ODT_LISTVIEW;
4868 dis.CtlID = uID;
4869 dis.itemID = item.iItem;
4870 dis.itemAction = ODA_DRAWENTIRE;
4871 dis.itemState = 0;
4872 if (item.state & LVIS_SELECTED) dis.itemState |= ODS_SELECTED;
4873 if (infoPtr->bFocus && (item.state & LVIS_FOCUSED)) dis.itemState |= ODS_FOCUS;
4874 dis.hwndItem = infoPtr->hwndSelf;
4875 dis.hDC = hdc;
4876 LISTVIEW_GetItemOrigin(infoPtr, dis.itemID, &Position);
4877 dis.rcItem.left = Position.x + Origin.x;
4878 dis.rcItem.right = dis.rcItem.left + infoPtr->nItemWidth;
4879 dis.rcItem.top = Position.y + Origin.y;
4880 dis.rcItem.bottom = dis.rcItem.top + infoPtr->nItemHeight;
4881 dis.itemData = item.lParam;
4883 TRACE("item=%s, rcItem=%s\n", debuglvitem_t(&item, TRUE), wine_dbgstr_rect(&dis.rcItem));
4886 * Even if we do not send the CDRF_NOTIFYITEMDRAW we need to fill the nmlvcd
4887 * structure for the rest. of the paint cycle
4889 customdraw_fill(&nmlvcd, infoPtr, hdc, &dis.rcItem, &item);
4890 if (cdmode & CDRF_NOTIFYITEMDRAW)
4891 cditemmode = notify_customdraw(infoPtr, CDDS_PREPAINT, &nmlvcd);
4893 if (!(cditemmode & CDRF_SKIPDEFAULT))
4895 prepaint_setup (infoPtr, hdc, &nmlvcd, FALSE);
4896 SendMessageW(infoPtr->hwndNotify, WM_DRAWITEM, dis.CtlID, (LPARAM)&dis);
4899 if (cditemmode & CDRF_NOTIFYPOSTPAINT)
4900 notify_postpaint(infoPtr, &nmlvcd);
4904 /***
4905 * DESCRIPTION:
4906 * Draws listview items when in report display mode.
4908 * PARAMETER(S):
4909 * [I] infoPtr : valid pointer to the listview structure
4910 * [I] hdc : device context handle
4911 * [I] cdmode : custom draw mode
4913 * RETURN:
4914 * None
4916 static void LISTVIEW_RefreshReport(LISTVIEW_INFO *infoPtr, ITERATOR *i, HDC hdc, DWORD cdmode)
4918 INT rgntype;
4919 RECT rcClip, rcItem;
4920 POINT Origin;
4921 RANGES colRanges;
4922 INT col;
4923 ITERATOR j;
4925 TRACE("()\n");
4927 /* figure out what to draw */
4928 rgntype = GetClipBox(hdc, &rcClip);
4929 if (rgntype == NULLREGION) return;
4931 /* Get scroll info once before loop */
4932 LISTVIEW_GetOrigin(infoPtr, &Origin);
4934 colRanges = ranges_create(DPA_GetPtrCount(infoPtr->hdpaColumns));
4936 /* narrow down the columns we need to paint */
4937 for(col = 0; col < DPA_GetPtrCount(infoPtr->hdpaColumns); col++)
4939 INT index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, col, 0);
4941 LISTVIEW_GetHeaderRect(infoPtr, index, &rcItem);
4942 if ((rcItem.right + Origin.x >= rcClip.left) && (rcItem.left + Origin.x < rcClip.right))
4943 ranges_additem(colRanges, index);
4945 iterator_rangesitems(&j, colRanges);
4947 /* in full row select, we _have_ to draw the main item */
4948 if (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT)
4949 j.nSpecial = 0;
4951 /* iterate through the invalidated rows */
4952 while(iterator_next(i))
4954 RANGES subitems;
4955 POINT Position;
4956 ITERATOR k;
4958 SelectObject(hdc, infoPtr->hFont);
4959 LISTVIEW_GetItemOrigin(infoPtr, i->nItem, &Position);
4960 Position.x = Origin.x;
4961 Position.y += Origin.y;
4963 subitems = ranges_create(DPA_GetPtrCount(infoPtr->hdpaColumns));
4965 /* iterate through the invalidated columns */
4966 while(iterator_next(&j))
4968 LISTVIEW_GetHeaderRect(infoPtr, j.nItem, &rcItem);
4970 if (rgntype == COMPLEXREGION && !((infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) && j.nItem == 0))
4972 rcItem.top = 0;
4973 rcItem.bottom = infoPtr->nItemHeight;
4974 OffsetRect(&rcItem, Origin.x, Position.y);
4975 if (!RectVisible(hdc, &rcItem)) continue;
4978 ranges_additem(subitems, j.nItem);
4981 iterator_rangesitems(&k, subitems);
4982 LISTVIEW_DrawItem(infoPtr, hdc, i->nItem, &k, Position, cdmode);
4983 iterator_destroy(&k);
4985 iterator_destroy(&j);
4988 /***
4989 * DESCRIPTION:
4990 * Draws the gridlines if necessary when in report display mode.
4992 * PARAMETER(S):
4993 * [I] infoPtr : valid pointer to the listview structure
4994 * [I] hdc : device context handle
4996 * RETURN:
4997 * None
4999 static void LISTVIEW_RefreshReportGrid(LISTVIEW_INFO *infoPtr, HDC hdc)
5001 INT rgntype;
5002 INT y, itemheight;
5003 INT col, index;
5004 HPEN hPen, hOldPen;
5005 RECT rcClip, rcItem = {0};
5006 POINT Origin;
5007 RANGES colRanges;
5008 ITERATOR j;
5009 BOOL rmost = FALSE;
5011 TRACE("()\n");
5013 /* figure out what to draw */
5014 rgntype = GetClipBox(hdc, &rcClip);
5015 if (rgntype == NULLREGION) return;
5017 /* Get scroll info once before loop */
5018 LISTVIEW_GetOrigin(infoPtr, &Origin);
5020 colRanges = ranges_create(DPA_GetPtrCount(infoPtr->hdpaColumns));
5022 /* narrow down the columns we need to paint */
5023 for(col = 0; col < DPA_GetPtrCount(infoPtr->hdpaColumns); col++)
5025 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, col, 0);
5027 LISTVIEW_GetHeaderRect(infoPtr, index, &rcItem);
5028 if ((rcItem.right + Origin.x >= rcClip.left) && (rcItem.left + Origin.x < rcClip.right))
5029 ranges_additem(colRanges, index);
5032 /* is right most vertical line visible? */
5033 if (DPA_GetPtrCount(infoPtr->hdpaColumns) > 0)
5035 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, 0);
5036 LISTVIEW_GetHeaderRect(infoPtr, index, &rcItem);
5037 rmost = (rcItem.right + Origin.x < rcClip.right);
5040 if ((hPen = CreatePen( PS_SOLID, 1, comctl32_color.clr3dFace )))
5042 hOldPen = SelectObject ( hdc, hPen );
5044 /* draw the vertical lines for the columns */
5045 iterator_rangesitems(&j, colRanges);
5046 while(iterator_next(&j))
5048 LISTVIEW_GetHeaderRect(infoPtr, j.nItem, &rcItem);
5049 if (rcItem.left == 0) continue; /* skip leftmost column */
5050 rcItem.left += Origin.x;
5051 rcItem.right += Origin.x;
5052 rcItem.top = infoPtr->rcList.top;
5053 rcItem.bottom = infoPtr->rcList.bottom;
5054 TRACE("vert col=%d, rcItem=%s\n", j.nItem, wine_dbgstr_rect(&rcItem));
5055 MoveToEx (hdc, rcItem.left, rcItem.top, NULL);
5056 LineTo (hdc, rcItem.left, rcItem.bottom);
5058 iterator_destroy(&j);
5059 /* draw rightmost grid line if visible */
5060 if (rmost)
5062 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX,
5063 DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, 0);
5064 LISTVIEW_GetHeaderRect(infoPtr, index, &rcItem);
5066 rcItem.right += Origin.x;
5068 MoveToEx (hdc, rcItem.right, infoPtr->rcList.top, NULL);
5069 LineTo (hdc, rcItem.right, infoPtr->rcList.bottom);
5072 /* draw the horizontal lines for the rows */
5073 itemheight = LISTVIEW_CalculateItemHeight(infoPtr);
5074 rcItem.left = infoPtr->rcList.left;
5075 rcItem.right = infoPtr->rcList.right;
5076 for(y = Origin.y > 1 ? Origin.y - 1 : itemheight - 1 + Origin.y % itemheight; y<=infoPtr->rcList.bottom; y+=itemheight)
5078 rcItem.bottom = rcItem.top = y;
5079 TRACE("horz rcItem=%s\n", wine_dbgstr_rect(&rcItem));
5080 MoveToEx (hdc, rcItem.left, rcItem.top, NULL);
5081 LineTo (hdc, rcItem.right, rcItem.top);
5084 SelectObject( hdc, hOldPen );
5085 DeleteObject( hPen );
5087 else
5088 ranges_destroy(colRanges);
5091 /***
5092 * DESCRIPTION:
5093 * Draws listview items when in list display mode.
5095 * PARAMETER(S):
5096 * [I] infoPtr : valid pointer to the listview structure
5097 * [I] hdc : device context handle
5098 * [I] cdmode : custom draw mode
5100 * RETURN:
5101 * None
5103 static void LISTVIEW_RefreshList(LISTVIEW_INFO *infoPtr, ITERATOR *i, HDC hdc, DWORD cdmode)
5105 POINT Origin, Position;
5107 /* Get scroll info once before loop */
5108 LISTVIEW_GetOrigin(infoPtr, &Origin);
5110 while(iterator_prev(i))
5112 SelectObject(hdc, infoPtr->hFont);
5113 LISTVIEW_GetItemOrigin(infoPtr, i->nItem, &Position);
5114 Position.x += Origin.x;
5115 Position.y += Origin.y;
5117 LISTVIEW_DrawItem(infoPtr, hdc, i->nItem, NULL, Position, cdmode);
5122 /***
5123 * DESCRIPTION:
5124 * Draws listview items.
5126 * PARAMETER(S):
5127 * [I] infoPtr : valid pointer to the listview structure
5128 * [I] hdc : device context handle
5129 * [I] prcErase : rect to be erased before refresh (may be NULL)
5131 * RETURN:
5132 * NoneX
5134 static void LISTVIEW_Refresh(LISTVIEW_INFO *infoPtr, HDC hdc, const RECT *prcErase)
5136 COLORREF oldTextColor = 0, oldBkColor = 0;
5137 NMLVCUSTOMDRAW nmlvcd;
5138 HFONT hOldFont = 0;
5139 DWORD cdmode;
5140 INT oldBkMode = 0;
5141 RECT rcClient;
5142 ITERATOR i;
5143 HDC hdcOrig = hdc;
5144 HBITMAP hbmp = NULL;
5145 RANGE range;
5147 LISTVIEW_DUMP(infoPtr);
5149 if (infoPtr->dwLvExStyle & LVS_EX_DOUBLEBUFFER) {
5150 TRACE("double buffering\n");
5152 hdc = CreateCompatibleDC(hdcOrig);
5153 if (!hdc) {
5154 ERR("Failed to create DC for backbuffer\n");
5155 return;
5157 hbmp = CreateCompatibleBitmap(hdcOrig, infoPtr->rcList.right,
5158 infoPtr->rcList.bottom);
5159 if (!hbmp) {
5160 ERR("Failed to create bitmap for backbuffer\n");
5161 DeleteDC(hdc);
5162 return;
5165 SelectObject(hdc, hbmp);
5166 SelectObject(hdc, infoPtr->hFont);
5168 if(GetClipBox(hdcOrig, &rcClient))
5169 IntersectClipRect(hdc, rcClient.left, rcClient.top, rcClient.right, rcClient.bottom);
5170 } else {
5171 /* Save dc values we're gonna trash while drawing
5172 * FIXME: Should be done in LISTVIEW_DrawItem() */
5173 hOldFont = SelectObject(hdc, infoPtr->hFont);
5174 oldBkMode = GetBkMode(hdc);
5175 oldBkColor = GetBkColor(hdc);
5176 oldTextColor = GetTextColor(hdc);
5179 infoPtr->bIsDrawing = TRUE;
5181 if (prcErase) {
5182 LISTVIEW_FillBkgnd(infoPtr, hdc, prcErase);
5183 } else if (infoPtr->dwLvExStyle & LVS_EX_DOUBLEBUFFER) {
5184 /* If no erasing was done (usually because RedrawWindow was called
5185 * with RDW_INVALIDATE only) we need to copy the old contents into
5186 * the backbuffer before continuing. */
5187 BitBlt(hdc, infoPtr->rcList.left, infoPtr->rcList.top,
5188 infoPtr->rcList.right - infoPtr->rcList.left,
5189 infoPtr->rcList.bottom - infoPtr->rcList.top,
5190 hdcOrig, infoPtr->rcList.left, infoPtr->rcList.top, SRCCOPY);
5193 GetClientRect(infoPtr->hwndSelf, &rcClient);
5194 customdraw_fill(&nmlvcd, infoPtr, hdc, &rcClient, 0);
5195 cdmode = notify_customdraw(infoPtr, CDDS_PREPAINT, &nmlvcd);
5196 if (cdmode & CDRF_SKIPDEFAULT) goto enddraw;
5198 /* nothing to draw */
5199 if(infoPtr->nItemCount == 0) goto enddraw;
5201 /* figure out what we need to draw */
5202 iterator_visibleitems(&i, infoPtr, hdc);
5203 range = iterator_range(&i);
5205 /* send cache hint notification */
5206 if (infoPtr->dwStyle & LVS_OWNERDATA)
5208 NMLVCACHEHINT nmlv;
5210 ZeroMemory(&nmlv, sizeof(NMLVCACHEHINT));
5211 nmlv.iFrom = range.lower;
5212 nmlv.iTo = range.upper - 1;
5213 notify_hdr(infoPtr, LVN_ODCACHEHINT, &nmlv.hdr);
5216 if ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (infoPtr->uView == LV_VIEW_DETAILS))
5217 LISTVIEW_RefreshOwnerDraw(infoPtr, &i, hdc, cdmode);
5218 else
5220 if (infoPtr->uView == LV_VIEW_DETAILS)
5221 LISTVIEW_RefreshReport(infoPtr, &i, hdc, cdmode);
5222 else /* LV_VIEW_LIST, LV_VIEW_ICON or LV_VIEW_SMALLICON */
5223 LISTVIEW_RefreshList(infoPtr, &i, hdc, cdmode);
5225 /* if we have a focus rect and it's visible, draw it */
5226 if (infoPtr->bFocus && range.lower <= infoPtr->nFocusedItem &&
5227 (range.upper - 1) >= infoPtr->nFocusedItem)
5228 LISTVIEW_DrawFocusRect(infoPtr, hdc);
5230 iterator_destroy(&i);
5232 enddraw:
5233 /* For LVS_EX_GRIDLINES go and draw lines */
5234 /* This includes the case where there were *no* items */
5235 if ((infoPtr->uView == LV_VIEW_DETAILS) && infoPtr->dwLvExStyle & LVS_EX_GRIDLINES)
5236 LISTVIEW_RefreshReportGrid(infoPtr, hdc);
5238 /* Draw marquee rectangle if appropriate */
5239 if (infoPtr->bMarqueeSelect)
5240 DrawFocusRect(hdc, &infoPtr->marqueeDrawRect);
5242 if (cdmode & CDRF_NOTIFYPOSTPAINT)
5243 notify_postpaint(infoPtr, &nmlvcd);
5245 if(hbmp) {
5246 BitBlt(hdcOrig, infoPtr->rcList.left, infoPtr->rcList.top,
5247 infoPtr->rcList.right - infoPtr->rcList.left,
5248 infoPtr->rcList.bottom - infoPtr->rcList.top,
5249 hdc, infoPtr->rcList.left, infoPtr->rcList.top, SRCCOPY);
5251 DeleteObject(hbmp);
5252 DeleteDC(hdc);
5253 } else {
5254 SelectObject(hdc, hOldFont);
5255 SetBkMode(hdc, oldBkMode);
5256 SetBkColor(hdc, oldBkColor);
5257 SetTextColor(hdc, oldTextColor);
5260 infoPtr->bIsDrawing = FALSE;
5264 /***
5265 * DESCRIPTION:
5266 * Calculates the approximate width and height of a given number of items.
5268 * PARAMETER(S):
5269 * [I] infoPtr : valid pointer to the listview structure
5270 * [I] nItemCount : number of items
5271 * [I] wWidth : width
5272 * [I] wHeight : height
5274 * RETURN:
5275 * Returns a DWORD. The width in the low word and the height in high word.
5277 static DWORD LISTVIEW_ApproximateViewRect(const LISTVIEW_INFO *infoPtr, INT nItemCount,
5278 WORD wWidth, WORD wHeight)
5280 DWORD dwViewRect = 0;
5282 if (nItemCount == -1)
5283 nItemCount = infoPtr->nItemCount;
5285 if (infoPtr->uView == LV_VIEW_LIST)
5287 INT nItemCountPerColumn = 1;
5288 INT nColumnCount = 0;
5290 if (wHeight == 0xFFFF)
5292 /* use current height */
5293 wHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
5296 if (wHeight < infoPtr->nItemHeight)
5297 wHeight = infoPtr->nItemHeight;
5299 if (nItemCount > 0)
5301 if (infoPtr->nItemHeight > 0)
5303 nItemCountPerColumn = wHeight / infoPtr->nItemHeight;
5304 if (nItemCountPerColumn == 0)
5305 nItemCountPerColumn = 1;
5307 if (nItemCount % nItemCountPerColumn != 0)
5308 nColumnCount = nItemCount / nItemCountPerColumn;
5309 else
5310 nColumnCount = nItemCount / nItemCountPerColumn + 1;
5314 /* Microsoft padding magic */
5315 wHeight = nItemCountPerColumn * infoPtr->nItemHeight + 2;
5316 wWidth = nColumnCount * infoPtr->nItemWidth + 2;
5318 dwViewRect = MAKELONG(wWidth, wHeight);
5320 else if (infoPtr->uView == LV_VIEW_DETAILS)
5322 RECT rcBox;
5324 if (infoPtr->nItemCount > 0)
5326 LISTVIEW_GetItemBox(infoPtr, 0, &rcBox);
5327 wWidth = rcBox.right - rcBox.left;
5328 wHeight = (rcBox.bottom - rcBox.top) * nItemCount;
5330 else
5332 /* use current height and width */
5333 if (wHeight == 0xffff)
5334 wHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
5335 if (wWidth == 0xffff)
5336 wWidth = infoPtr->rcList.right - infoPtr->rcList.left;
5339 dwViewRect = MAKELONG(wWidth, wHeight);
5341 else if (infoPtr->uView == LV_VIEW_ICON)
5343 UINT rows,cols;
5344 UINT nItemWidth;
5345 UINT nItemHeight;
5347 nItemWidth = infoPtr->iconSpacing.cx;
5348 nItemHeight = infoPtr->iconSpacing.cy;
5350 if (wWidth == 0xffff)
5351 wWidth = infoPtr->rcList.right - infoPtr->rcList.left;
5353 if (wWidth < nItemWidth)
5354 wWidth = nItemWidth;
5356 cols = wWidth / nItemWidth;
5357 if (cols > nItemCount)
5358 cols = nItemCount;
5359 if (cols < 1)
5360 cols = 1;
5362 if (nItemCount)
5364 rows = nItemCount / cols;
5365 if (nItemCount % cols)
5366 rows++;
5368 else
5369 rows = 0;
5371 wHeight = (nItemHeight * rows)+2;
5372 wWidth = (nItemWidth * cols)+2;
5374 dwViewRect = MAKELONG(wWidth, wHeight);
5376 else if (infoPtr->uView == LV_VIEW_SMALLICON)
5377 FIXME("uView == LV_VIEW_SMALLICON: not implemented\n");
5379 return dwViewRect;
5382 /***
5383 * DESCRIPTION:
5384 * Cancel edit label with saving item text.
5386 * PARAMETER(S):
5387 * [I] infoPtr : valid pointer to the listview structure
5389 * RETURN:
5390 * Always returns TRUE.
5392 static LRESULT LISTVIEW_CancelEditLabel(LISTVIEW_INFO *infoPtr)
5394 if (infoPtr->hwndEdit)
5396 /* handle value will be lost after LISTVIEW_EndEditLabelT */
5397 HWND edit = infoPtr->hwndEdit;
5399 LISTVIEW_EndEditLabelT(infoPtr, TRUE, IsWindowUnicode(infoPtr->hwndEdit));
5400 SendMessageW(edit, WM_CLOSE, 0, 0);
5403 return TRUE;
5406 /***
5407 * DESCRIPTION:
5408 * Create a drag image list for the specified item.
5410 * PARAMETER(S):
5411 * [I] infoPtr : valid pointer to the listview structure
5412 * [I] iItem : index of item
5413 * [O] lppt : Upper-left corner of the image
5415 * RETURN:
5416 * Returns a handle to the image list if successful, NULL otherwise.
5418 static HIMAGELIST LISTVIEW_CreateDragImage(LISTVIEW_INFO *infoPtr, INT iItem, LPPOINT lppt)
5420 RECT rcItem;
5421 SIZE size;
5422 POINT pos;
5423 HDC hdc, hdcOrig;
5424 HBITMAP hbmp, hOldbmp;
5425 HFONT hOldFont;
5426 HIMAGELIST dragList = 0;
5427 TRACE("iItem=%d Count=%d\n", iItem, infoPtr->nItemCount);
5429 if (iItem < 0 || iItem >= infoPtr->nItemCount || !lppt)
5430 return 0;
5432 rcItem.left = LVIR_BOUNDS;
5433 if (!LISTVIEW_GetItemRect(infoPtr, iItem, &rcItem))
5434 return 0;
5436 lppt->x = rcItem.left;
5437 lppt->y = rcItem.top;
5439 size.cx = rcItem.right - rcItem.left;
5440 size.cy = rcItem.bottom - rcItem.top;
5442 hdcOrig = GetDC(infoPtr->hwndSelf);
5443 hdc = CreateCompatibleDC(hdcOrig);
5444 hbmp = CreateCompatibleBitmap(hdcOrig, size.cx, size.cy);
5445 hOldbmp = SelectObject(hdc, hbmp);
5446 hOldFont = SelectObject(hdc, infoPtr->hFont);
5448 SetRect(&rcItem, 0, 0, size.cx, size.cy);
5449 FillRect(hdc, &rcItem, infoPtr->hBkBrush);
5451 pos.x = pos.y = 0;
5452 if (LISTVIEW_DrawItem(infoPtr, hdc, iItem, NULL, pos, CDRF_DODEFAULT))
5454 dragList = ImageList_Create(size.cx, size.cy, ILC_COLOR, 10, 10);
5455 SelectObject(hdc, hOldbmp);
5456 ImageList_Add(dragList, hbmp, 0);
5458 else
5459 SelectObject(hdc, hOldbmp);
5461 SelectObject(hdc, hOldFont);
5462 DeleteObject(hbmp);
5463 DeleteDC(hdc);
5464 ReleaseDC(infoPtr->hwndSelf, hdcOrig);
5466 TRACE("ret=%p\n", dragList);
5468 return dragList;
5472 /***
5473 * DESCRIPTION:
5474 * Removes all listview items and subitems.
5476 * PARAMETER(S):
5477 * [I] infoPtr : valid pointer to the listview structure
5479 * RETURN:
5480 * SUCCESS : TRUE
5481 * FAILURE : FALSE
5483 static BOOL LISTVIEW_DeleteAllItems(LISTVIEW_INFO *infoPtr, BOOL destroy)
5485 HDPA hdpaSubItems = NULL;
5486 BOOL suppress = FALSE;
5487 ITEMHDR *hdrItem;
5488 ITEM_INFO *lpItem;
5489 ITEM_ID *lpID;
5490 INT i, j;
5492 TRACE("()\n");
5494 /* we do it directly, to avoid notifications */
5495 ranges_clear(infoPtr->selectionRanges);
5496 infoPtr->nSelectionMark = -1;
5497 infoPtr->nFocusedItem = -1;
5498 SetRectEmpty(&infoPtr->rcFocus);
5499 /* But we are supposed to leave nHotItem as is! */
5501 /* send LVN_DELETEALLITEMS notification */
5502 if (!(infoPtr->dwStyle & LVS_OWNERDATA) || !destroy)
5504 NMLISTVIEW nmlv;
5506 memset(&nmlv, 0, sizeof(NMLISTVIEW));
5507 nmlv.iItem = -1;
5508 suppress = notify_listview(infoPtr, LVN_DELETEALLITEMS, &nmlv);
5511 for (i = infoPtr->nItemCount - 1; i >= 0; i--)
5513 if (!(infoPtr->dwStyle & LVS_OWNERDATA))
5515 /* send LVN_DELETEITEM notification, if not suppressed
5516 and if it is not a virtual listview */
5517 if (!suppress) notify_deleteitem(infoPtr, i);
5518 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, i);
5519 lpItem = DPA_GetPtr(hdpaSubItems, 0);
5520 /* free id struct */
5521 j = DPA_GetPtrIndex(infoPtr->hdpaItemIds, lpItem->id);
5522 lpID = DPA_GetPtr(infoPtr->hdpaItemIds, j);
5523 DPA_DeletePtr(infoPtr->hdpaItemIds, j);
5524 Free(lpID);
5525 /* both item and subitem start with ITEMHDR header */
5526 for (j = 0; j < DPA_GetPtrCount(hdpaSubItems); j++)
5528 hdrItem = DPA_GetPtr(hdpaSubItems, j);
5529 if (is_text(hdrItem->pszText)) Free(hdrItem->pszText);
5530 Free(hdrItem);
5532 DPA_Destroy(hdpaSubItems);
5533 DPA_DeletePtr(infoPtr->hdpaItems, i);
5535 DPA_DeletePtr(infoPtr->hdpaPosX, i);
5536 DPA_DeletePtr(infoPtr->hdpaPosY, i);
5537 infoPtr->nItemCount --;
5540 if (!destroy)
5542 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
5543 LISTVIEW_UpdateScroll(infoPtr);
5545 LISTVIEW_InvalidateList(infoPtr);
5547 return TRUE;
5550 /***
5551 * DESCRIPTION:
5552 * Scrolls, and updates the columns, when a column is changing width.
5554 * PARAMETER(S):
5555 * [I] infoPtr : valid pointer to the listview structure
5556 * [I] nColumn : column to scroll
5557 * [I] dx : amount of scroll, in pixels
5559 * RETURN:
5560 * None.
5562 static void LISTVIEW_ScrollColumns(LISTVIEW_INFO *infoPtr, INT nColumn, INT dx)
5564 COLUMN_INFO *lpColumnInfo;
5565 RECT rcOld, rcCol;
5566 POINT ptOrigin;
5567 INT nCol;
5568 HDITEMW hdi;
5570 if (nColumn < 0 || DPA_GetPtrCount(infoPtr->hdpaColumns) < 1) return;
5571 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, min(nColumn, DPA_GetPtrCount(infoPtr->hdpaColumns) - 1));
5572 rcCol = lpColumnInfo->rcHeader;
5573 if (nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns))
5574 rcCol.left = rcCol.right;
5576 /* adjust the other columns */
5577 hdi.mask = HDI_ORDER;
5578 if (SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nColumn, (LPARAM)&hdi))
5580 INT nOrder = hdi.iOrder;
5581 for (nCol = 0; nCol < DPA_GetPtrCount(infoPtr->hdpaColumns); nCol++)
5583 hdi.mask = HDI_ORDER;
5584 SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nCol, (LPARAM)&hdi);
5585 if (hdi.iOrder >= nOrder) {
5586 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, nCol);
5587 lpColumnInfo->rcHeader.left += dx;
5588 lpColumnInfo->rcHeader.right += dx;
5593 /* do not update screen if not in report mode */
5594 if (!is_redrawing(infoPtr) || infoPtr->uView != LV_VIEW_DETAILS) return;
5596 /* Need to reset the item width when inserting a new column */
5597 infoPtr->nItemWidth += dx;
5599 LISTVIEW_UpdateScroll(infoPtr);
5600 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
5602 /* scroll to cover the deleted column, and invalidate for redraw */
5603 rcOld = infoPtr->rcList;
5604 rcOld.left = ptOrigin.x + rcCol.left + dx;
5605 ScrollWindowEx(infoPtr->hwndSelf, dx, 0, &rcOld, &rcOld, 0, 0, SW_ERASE | SW_INVALIDATE);
5608 /***
5609 * DESCRIPTION:
5610 * Removes a column from the listview control.
5612 * PARAMETER(S):
5613 * [I] infoPtr : valid pointer to the listview structure
5614 * [I] nColumn : column index
5616 * RETURN:
5617 * SUCCESS : TRUE
5618 * FAILURE : FALSE
5620 static BOOL LISTVIEW_DeleteColumn(LISTVIEW_INFO *infoPtr, INT nColumn)
5622 RECT rcCol;
5624 TRACE("nColumn=%d\n", nColumn);
5626 if (nColumn < 0 || DPA_GetPtrCount(infoPtr->hdpaColumns) == 0
5627 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
5629 /* While the MSDN specifically says that column zero should not be deleted,
5630 what actually happens is that the column itself is deleted but no items or subitems
5631 are removed.
5634 LISTVIEW_GetHeaderRect(infoPtr, nColumn, &rcCol);
5636 if (!SendMessageW(infoPtr->hwndHeader, HDM_DELETEITEM, nColumn, 0))
5637 return FALSE;
5639 Free(DPA_GetPtr(infoPtr->hdpaColumns, nColumn));
5640 DPA_DeletePtr(infoPtr->hdpaColumns, nColumn);
5642 if (!(infoPtr->dwStyle & LVS_OWNERDATA) && nColumn)
5644 SUBITEM_INFO *lpSubItem, *lpDelItem;
5645 HDPA hdpaSubItems;
5646 INT nItem, nSubItem, i;
5648 for (nItem = 0; nItem < infoPtr->nItemCount; nItem++)
5650 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, nItem);
5651 nSubItem = 0;
5652 lpDelItem = 0;
5653 for (i = 1; i < DPA_GetPtrCount(hdpaSubItems); i++)
5655 lpSubItem = DPA_GetPtr(hdpaSubItems, i);
5656 if (lpSubItem->iSubItem == nColumn)
5658 nSubItem = i;
5659 lpDelItem = lpSubItem;
5661 else if (lpSubItem->iSubItem > nColumn)
5663 lpSubItem->iSubItem--;
5667 /* if we found our subitem, zap it */
5668 if (nSubItem > 0)
5670 /* free string */
5671 if (is_text(lpDelItem->hdr.pszText))
5672 Free(lpDelItem->hdr.pszText);
5674 /* free item */
5675 Free(lpDelItem);
5677 /* free dpa memory */
5678 DPA_DeletePtr(hdpaSubItems, nSubItem);
5683 /* update the other column info */
5684 if(DPA_GetPtrCount(infoPtr->hdpaColumns) == 0)
5685 LISTVIEW_InvalidateList(infoPtr);
5686 else
5687 LISTVIEW_ScrollColumns(infoPtr, nColumn, -(rcCol.right - rcCol.left));
5688 LISTVIEW_UpdateItemSize(infoPtr);
5690 return TRUE;
5693 /***
5694 * DESCRIPTION:
5695 * Invalidates the listview after an item's insertion or deletion.
5697 * PARAMETER(S):
5698 * [I] infoPtr : valid pointer to the listview structure
5699 * [I] nItem : item index
5700 * [I] dir : -1 if deleting, 1 if inserting
5702 * RETURN:
5703 * None
5705 static void LISTVIEW_ScrollOnInsert(LISTVIEW_INFO *infoPtr, INT nItem, INT dir)
5707 INT nPerCol, nItemCol, nItemRow;
5708 RECT rcScroll;
5709 POINT Origin;
5711 /* if we don't refresh, what's the point of scrolling? */
5712 if (!is_redrawing(infoPtr)) return;
5714 assert (abs(dir) == 1);
5716 /* arrange icons if autoarrange is on */
5717 if (is_autoarrange(infoPtr))
5719 BOOL arrange = TRUE;
5720 if (dir < 0 && nItem >= infoPtr->nItemCount) arrange = FALSE;
5721 if (dir > 0 && nItem == infoPtr->nItemCount - 1) arrange = FALSE;
5722 if (arrange) LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
5725 /* scrollbars need updating */
5726 LISTVIEW_UpdateScroll(infoPtr);
5728 /* figure out the item's position */
5729 if (infoPtr->uView == LV_VIEW_DETAILS)
5730 nPerCol = infoPtr->nItemCount + 1;
5731 else if (infoPtr->uView == LV_VIEW_LIST)
5732 nPerCol = LISTVIEW_GetCountPerColumn(infoPtr);
5733 else /* LV_VIEW_ICON, or LV_VIEW_SMALLICON */
5734 return;
5736 nItemCol = nItem / nPerCol;
5737 nItemRow = nItem % nPerCol;
5738 LISTVIEW_GetOrigin(infoPtr, &Origin);
5740 /* move the items below up a slot */
5741 rcScroll.left = nItemCol * infoPtr->nItemWidth;
5742 rcScroll.top = nItemRow * infoPtr->nItemHeight;
5743 rcScroll.right = rcScroll.left + infoPtr->nItemWidth;
5744 rcScroll.bottom = nPerCol * infoPtr->nItemHeight;
5745 OffsetRect(&rcScroll, Origin.x, Origin.y);
5746 TRACE("rcScroll=%s, dx=%d\n", wine_dbgstr_rect(&rcScroll), dir * infoPtr->nItemHeight);
5747 if (IntersectRect(&rcScroll, &rcScroll, &infoPtr->rcList))
5749 TRACE("Invalidating rcScroll=%s, rcList=%s\n", wine_dbgstr_rect(&rcScroll), wine_dbgstr_rect(&infoPtr->rcList));
5750 InvalidateRect(infoPtr->hwndSelf, &rcScroll, TRUE);
5753 /* report has only that column, so we're done */
5754 if (infoPtr->uView == LV_VIEW_DETAILS) return;
5756 /* now for LISTs, we have to deal with the columns to the right */
5757 SetRect(&rcScroll, (nItemCol + 1) * infoPtr->nItemWidth, 0,
5758 (infoPtr->nItemCount / nPerCol + 1) * infoPtr->nItemWidth,
5759 nPerCol * infoPtr->nItemHeight);
5760 OffsetRect(&rcScroll, Origin.x, Origin.y);
5761 if (IntersectRect(&rcScroll, &rcScroll, &infoPtr->rcList))
5762 InvalidateRect(infoPtr->hwndSelf, &rcScroll, TRUE);
5765 /***
5766 * DESCRIPTION:
5767 * Removes an item from the listview control.
5769 * PARAMETER(S):
5770 * [I] infoPtr : valid pointer to the listview structure
5771 * [I] nItem : item index
5773 * RETURN:
5774 * SUCCESS : TRUE
5775 * FAILURE : FALSE
5777 static BOOL LISTVIEW_DeleteItem(LISTVIEW_INFO *infoPtr, INT nItem)
5779 LVITEMW item;
5780 const BOOL is_icon = (infoPtr->uView == LV_VIEW_SMALLICON || infoPtr->uView == LV_VIEW_ICON);
5781 INT focus = infoPtr->nFocusedItem;
5783 TRACE("(nItem=%d)\n", nItem);
5785 if (nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
5787 /* remove selection, and focus */
5788 item.state = 0;
5789 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
5790 LISTVIEW_SetItemState(infoPtr, nItem, &item);
5792 /* send LVN_DELETEITEM notification. */
5793 if (!notify_deleteitem(infoPtr, nItem)) return FALSE;
5795 /* we need to do this here, because we'll be deleting stuff */
5796 if (is_icon)
5797 LISTVIEW_InvalidateItem(infoPtr, nItem);
5799 if (!(infoPtr->dwStyle & LVS_OWNERDATA))
5801 HDPA hdpaSubItems;
5802 ITEMHDR *hdrItem;
5803 ITEM_INFO *lpItem;
5804 ITEM_ID *lpID;
5805 INT i;
5807 hdpaSubItems = DPA_DeletePtr(infoPtr->hdpaItems, nItem);
5808 lpItem = DPA_GetPtr(hdpaSubItems, 0);
5810 /* free id struct */
5811 i = DPA_GetPtrIndex(infoPtr->hdpaItemIds, lpItem->id);
5812 lpID = DPA_GetPtr(infoPtr->hdpaItemIds, i);
5813 DPA_DeletePtr(infoPtr->hdpaItemIds, i);
5814 Free(lpID);
5815 for (i = 0; i < DPA_GetPtrCount(hdpaSubItems); i++)
5817 hdrItem = DPA_GetPtr(hdpaSubItems, i);
5818 if (is_text(hdrItem->pszText)) Free(hdrItem->pszText);
5819 Free(hdrItem);
5821 DPA_Destroy(hdpaSubItems);
5824 if (is_icon)
5826 DPA_DeletePtr(infoPtr->hdpaPosX, nItem);
5827 DPA_DeletePtr(infoPtr->hdpaPosY, nItem);
5830 infoPtr->nItemCount--;
5831 LISTVIEW_ShiftIndices(infoPtr, nItem, -1);
5832 LISTVIEW_ShiftFocus(infoPtr, focus, nItem, -1);
5834 /* now is the invalidation fun */
5835 if (!is_icon)
5836 LISTVIEW_ScrollOnInsert(infoPtr, nItem, -1);
5837 return TRUE;
5841 /***
5842 * DESCRIPTION:
5843 * Callback implementation for editlabel control
5845 * PARAMETER(S):
5846 * [I] infoPtr : valid pointer to the listview structure
5847 * [I] storeText : store edit box text as item text
5848 * [I] isW : TRUE if psxText is Unicode, FALSE if it's ANSI
5850 * RETURN:
5851 * SUCCESS : TRUE
5852 * FAILURE : FALSE
5854 static BOOL LISTVIEW_EndEditLabelT(LISTVIEW_INFO *infoPtr, BOOL storeText, BOOL isW)
5856 HWND hwndSelf = infoPtr->hwndSelf;
5857 WCHAR szDispText[DISP_TEXT_SIZE] = { 0 };
5858 NMLVDISPINFOW dispInfo;
5859 INT editedItem = infoPtr->nEditLabelItem;
5860 BOOL same;
5861 WCHAR *pszText = NULL;
5862 BOOL res;
5864 if (storeText)
5866 DWORD len = isW ? GetWindowTextLengthW(infoPtr->hwndEdit) : GetWindowTextLengthA(infoPtr->hwndEdit);
5868 if (len)
5870 if ((pszText = Alloc((len+1) * (isW ? sizeof(WCHAR) : sizeof(CHAR)))))
5872 if (isW) GetWindowTextW(infoPtr->hwndEdit, pszText, len+1);
5873 else GetWindowTextA(infoPtr->hwndEdit, (CHAR*)pszText, len+1);
5878 TRACE("(pszText=%s, isW=%d)\n", debugtext_t(pszText, isW), isW);
5880 ZeroMemory(&dispInfo, sizeof(dispInfo));
5881 dispInfo.item.mask = LVIF_PARAM | LVIF_STATE | LVIF_TEXT;
5882 dispInfo.item.iItem = editedItem;
5883 dispInfo.item.iSubItem = 0;
5884 dispInfo.item.stateMask = ~0;
5885 dispInfo.item.pszText = szDispText;
5886 dispInfo.item.cchTextMax = DISP_TEXT_SIZE;
5887 if (!LISTVIEW_GetItemT(infoPtr, &dispInfo.item, isW))
5889 res = FALSE;
5890 goto cleanup;
5893 if (isW)
5894 same = (lstrcmpW(dispInfo.item.pszText, pszText) == 0);
5895 else
5897 LPWSTR tmp = textdupTtoW(pszText, FALSE);
5898 same = (lstrcmpW(dispInfo.item.pszText, tmp) == 0);
5899 textfreeT(tmp, FALSE);
5902 /* add the text from the edit in */
5903 dispInfo.item.mask |= LVIF_TEXT;
5904 dispInfo.item.pszText = same ? NULL : pszText;
5905 dispInfo.item.cchTextMax = textlenT(dispInfo.item.pszText, isW);
5907 /* Do we need to update the Item Text */
5908 res = notify_dispinfoT(infoPtr, LVN_ENDLABELEDITW, &dispInfo, isW);
5910 infoPtr->nEditLabelItem = -1;
5911 infoPtr->hwndEdit = 0;
5913 if (!res) goto cleanup;
5915 if (!IsWindow(hwndSelf))
5917 res = FALSE;
5918 goto cleanup;
5920 if (!pszText) return TRUE;
5921 if (same)
5923 res = TRUE;
5924 goto cleanup;
5927 if (!(infoPtr->dwStyle & LVS_OWNERDATA))
5929 HDPA hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, editedItem);
5930 ITEM_INFO* lpItem = DPA_GetPtr(hdpaSubItems, 0);
5931 if (lpItem && lpItem->hdr.pszText == LPSTR_TEXTCALLBACKW)
5933 LISTVIEW_InvalidateItem(infoPtr, editedItem);
5934 res = TRUE;
5935 goto cleanup;
5939 ZeroMemory(&dispInfo, sizeof(dispInfo));
5940 dispInfo.item.mask = LVIF_TEXT;
5941 dispInfo.item.iItem = editedItem;
5942 dispInfo.item.iSubItem = 0;
5943 dispInfo.item.pszText = pszText;
5944 dispInfo.item.cchTextMax = textlenT(pszText, isW);
5945 res = LISTVIEW_SetItemT(infoPtr, &dispInfo.item, isW);
5947 cleanup:
5948 Free(pszText);
5950 return res;
5953 /***
5954 * DESCRIPTION:
5955 * Subclassed edit control windproc function
5957 * PARAMETER(S):
5958 * [I] hwnd : the edit window handle
5959 * [I] uMsg : the message that is to be processed
5960 * [I] wParam : first message parameter
5961 * [I] lParam : second message parameter
5962 * [I] isW : TRUE if input is Unicode
5964 * RETURN:
5965 * Zero.
5967 static LRESULT EditLblWndProcT(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL isW)
5969 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongPtrW(GetParent(hwnd), 0);
5970 BOOL save = TRUE;
5972 TRACE("(hwnd=%p, uMsg=%x, wParam=%lx, lParam=%lx, isW=%d)\n",
5973 hwnd, uMsg, wParam, lParam, isW);
5975 switch (uMsg)
5977 case WM_GETDLGCODE:
5978 return DLGC_WANTARROWS | DLGC_WANTALLKEYS;
5980 case WM_DESTROY:
5982 WNDPROC editProc = infoPtr->EditWndProc;
5983 infoPtr->EditWndProc = 0;
5984 SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (DWORD_PTR)editProc);
5985 return CallWindowProcT(editProc, hwnd, uMsg, wParam, lParam, isW);
5988 case WM_KEYDOWN:
5989 if (VK_ESCAPE == (INT)wParam)
5991 save = FALSE;
5992 break;
5994 else if (VK_RETURN == (INT)wParam)
5995 break;
5997 default:
5998 return CallWindowProcT(infoPtr->EditWndProc, hwnd, uMsg, wParam, lParam, isW);
6001 /* kill the edit */
6002 if (infoPtr->hwndEdit)
6003 LISTVIEW_EndEditLabelT(infoPtr, save, isW);
6005 SendMessageW(hwnd, WM_CLOSE, 0, 0);
6006 return 0;
6009 /***
6010 * DESCRIPTION:
6011 * Subclassed edit control Unicode windproc function
6013 * PARAMETER(S):
6014 * [I] hwnd : the edit window handle
6015 * [I] uMsg : the message that is to be processed
6016 * [I] wParam : first message parameter
6017 * [I] lParam : second message parameter
6019 * RETURN:
6021 static LRESULT CALLBACK EditLblWndProcW(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
6023 return EditLblWndProcT(hwnd, uMsg, wParam, lParam, TRUE);
6026 /***
6027 * DESCRIPTION:
6028 * Subclassed edit control ANSI windproc function
6030 * PARAMETER(S):
6031 * [I] hwnd : the edit window handle
6032 * [I] uMsg : the message that is to be processed
6033 * [I] wParam : first message parameter
6034 * [I] lParam : second message parameter
6036 * RETURN:
6038 static LRESULT CALLBACK EditLblWndProcA(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
6040 return EditLblWndProcT(hwnd, uMsg, wParam, lParam, FALSE);
6043 /***
6044 * DESCRIPTION:
6045 * Creates a subclassed edit control
6047 * PARAMETER(S):
6048 * [I] infoPtr : valid pointer to the listview structure
6049 * [I] text : initial text for the edit
6050 * [I] style : the window style
6051 * [I] isW : TRUE if input is Unicode
6053 * RETURN:
6055 static HWND CreateEditLabelT(LISTVIEW_INFO *infoPtr, LPCWSTR text, BOOL isW)
6057 static const DWORD style = WS_CHILDWINDOW|WS_CLIPSIBLINGS|ES_LEFT|ES_AUTOHSCROLL|WS_BORDER|WS_VISIBLE;
6058 HINSTANCE hinst = (HINSTANCE)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_HINSTANCE);
6059 HWND hedit;
6061 TRACE("(%p, text=%s, isW=%d)\n", infoPtr, debugtext_t(text, isW), isW);
6063 /* window will be resized and positioned after LVN_BEGINLABELEDIT */
6064 if (isW)
6065 hedit = CreateWindowW(WC_EDITW, text, style, 0, 0, 0, 0, infoPtr->hwndSelf, 0, hinst, 0);
6066 else
6067 hedit = CreateWindowA(WC_EDITA, (LPCSTR)text, style, 0, 0, 0, 0, infoPtr->hwndSelf, 0, hinst, 0);
6069 if (!hedit) return 0;
6071 infoPtr->EditWndProc = (WNDPROC)
6072 (isW ? SetWindowLongPtrW(hedit, GWLP_WNDPROC, (DWORD_PTR)EditLblWndProcW) :
6073 SetWindowLongPtrA(hedit, GWLP_WNDPROC, (DWORD_PTR)EditLblWndProcA) );
6075 SendMessageW(hedit, WM_SETFONT, (WPARAM)infoPtr->hFont, FALSE);
6076 SendMessageW(hedit, EM_SETLIMITTEXT, DISP_TEXT_SIZE-1, 0);
6078 return hedit;
6081 /***
6082 * DESCRIPTION:
6083 * Begin in place editing of specified list view item
6085 * PARAMETER(S):
6086 * [I] infoPtr : valid pointer to the listview structure
6087 * [I] nItem : item index
6088 * [I] isW : TRUE if it's a Unicode req, FALSE if ASCII
6090 * RETURN:
6091 * SUCCESS : TRUE
6092 * FAILURE : FALSE
6094 static HWND LISTVIEW_EditLabelT(LISTVIEW_INFO *infoPtr, INT nItem, BOOL isW)
6096 WCHAR disptextW[DISP_TEXT_SIZE] = { 0 };
6097 HWND hwndSelf = infoPtr->hwndSelf;
6098 NMLVDISPINFOW dispInfo;
6099 HFONT hOldFont = NULL;
6100 TEXTMETRICW tm;
6101 RECT rect;
6102 SIZE sz;
6103 HDC hdc;
6105 TRACE("(nItem=%d, isW=%d)\n", nItem, isW);
6107 if (~infoPtr->dwStyle & LVS_EDITLABELS) return 0;
6109 /* remove existing edit box */
6110 if (infoPtr->hwndEdit)
6112 SetFocus(infoPtr->hwndSelf);
6113 infoPtr->hwndEdit = 0;
6116 if (nItem < 0 || nItem >= infoPtr->nItemCount) return 0;
6118 infoPtr->nEditLabelItem = nItem;
6120 LISTVIEW_SetSelection(infoPtr, nItem);
6121 LISTVIEW_SetItemFocus(infoPtr, nItem);
6122 LISTVIEW_InvalidateItem(infoPtr, nItem);
6124 rect.left = LVIR_LABEL;
6125 if (!LISTVIEW_GetItemRect(infoPtr, nItem, &rect)) return 0;
6127 ZeroMemory(&dispInfo, sizeof(dispInfo));
6128 dispInfo.item.mask = LVIF_PARAM | LVIF_STATE | LVIF_TEXT;
6129 dispInfo.item.iItem = nItem;
6130 dispInfo.item.iSubItem = 0;
6131 dispInfo.item.stateMask = ~0;
6132 dispInfo.item.pszText = disptextW;
6133 dispInfo.item.cchTextMax = DISP_TEXT_SIZE;
6134 if (!LISTVIEW_GetItemT(infoPtr, &dispInfo.item, isW)) return 0;
6136 infoPtr->hwndEdit = CreateEditLabelT(infoPtr, dispInfo.item.pszText, isW);
6137 if (!infoPtr->hwndEdit) return 0;
6139 if (notify_dispinfoT(infoPtr, LVN_BEGINLABELEDITW, &dispInfo, isW))
6141 if (!IsWindow(hwndSelf))
6142 return 0;
6143 SendMessageW(infoPtr->hwndEdit, WM_CLOSE, 0, 0);
6144 infoPtr->hwndEdit = 0;
6145 return 0;
6148 TRACE("disp text=%s\n", debugtext_t(dispInfo.item.pszText, isW));
6150 /* position and display edit box */
6151 hdc = GetDC(infoPtr->hwndSelf);
6153 /* select the font to get appropriate metric dimensions */
6154 if (infoPtr->hFont)
6155 hOldFont = SelectObject(hdc, infoPtr->hFont);
6157 /* use real edit box content, it could be altered during LVN_BEGINLABELEDIT notification */
6158 GetWindowTextW(infoPtr->hwndEdit, disptextW, DISP_TEXT_SIZE);
6159 TRACE("edit box text=%s\n", debugstr_w(disptextW));
6161 /* get string length in pixels */
6162 GetTextExtentPoint32W(hdc, disptextW, lstrlenW(disptextW), &sz);
6164 /* add extra spacing for the next character */
6165 GetTextMetricsW(hdc, &tm);
6166 sz.cx += tm.tmMaxCharWidth * 2;
6168 if (infoPtr->hFont)
6169 SelectObject(hdc, hOldFont);
6171 ReleaseDC(infoPtr->hwndSelf, hdc);
6173 sz.cy = rect.bottom - rect.top + 2;
6174 rect.left -= 2;
6175 rect.top -= 1;
6176 TRACE("moving edit=(%d,%d)-(%d,%d)\n", rect.left, rect.top, sz.cx, sz.cy);
6177 MoveWindow(infoPtr->hwndEdit, rect.left, rect.top, sz.cx, sz.cy, FALSE);
6178 ShowWindow(infoPtr->hwndEdit, SW_NORMAL);
6179 SetFocus(infoPtr->hwndEdit);
6180 SendMessageW(infoPtr->hwndEdit, EM_SETSEL, 0, -1);
6181 return infoPtr->hwndEdit;
6185 /***
6186 * DESCRIPTION:
6187 * Ensures the specified item is visible, scrolling into view if necessary.
6189 * PARAMETER(S):
6190 * [I] infoPtr : valid pointer to the listview structure
6191 * [I] nItem : item index
6192 * [I] bPartial : partially or entirely visible
6194 * RETURN:
6195 * SUCCESS : TRUE
6196 * FAILURE : FALSE
6198 static BOOL LISTVIEW_EnsureVisible(LISTVIEW_INFO *infoPtr, INT nItem, BOOL bPartial)
6200 INT nScrollPosHeight = 0;
6201 INT nScrollPosWidth = 0;
6202 INT nHorzAdjust = 0;
6203 INT nVertAdjust = 0;
6204 INT nHorzDiff = 0;
6205 INT nVertDiff = 0;
6206 RECT rcItem, rcTemp;
6208 rcItem.left = LVIR_BOUNDS;
6209 if (!LISTVIEW_GetItemRect(infoPtr, nItem, &rcItem)) return FALSE;
6211 if (bPartial && IntersectRect(&rcTemp, &infoPtr->rcList, &rcItem)) return TRUE;
6213 if (rcItem.left < infoPtr->rcList.left || rcItem.right > infoPtr->rcList.right)
6215 /* scroll left/right, but in LV_VIEW_DETAILS mode */
6216 if (infoPtr->uView == LV_VIEW_LIST)
6217 nScrollPosWidth = infoPtr->nItemWidth;
6218 else if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
6219 nScrollPosWidth = 1;
6221 if (rcItem.left < infoPtr->rcList.left)
6223 nHorzAdjust = -1;
6224 if (infoPtr->uView != LV_VIEW_DETAILS) nHorzDiff = rcItem.left - infoPtr->rcList.left;
6226 else
6228 nHorzAdjust = 1;
6229 if (infoPtr->uView != LV_VIEW_DETAILS) nHorzDiff = rcItem.right - infoPtr->rcList.right;
6233 if (rcItem.top < infoPtr->rcList.top || rcItem.bottom > infoPtr->rcList.bottom)
6235 /* scroll up/down, but not in LVS_LIST mode */
6236 if (infoPtr->uView == LV_VIEW_DETAILS)
6237 nScrollPosHeight = infoPtr->nItemHeight;
6238 else if ((infoPtr->uView == LV_VIEW_ICON) || (infoPtr->uView == LV_VIEW_SMALLICON))
6239 nScrollPosHeight = 1;
6241 if (rcItem.top < infoPtr->rcList.top)
6243 nVertAdjust = -1;
6244 if (infoPtr->uView != LV_VIEW_LIST) nVertDiff = rcItem.top - infoPtr->rcList.top;
6246 else
6248 nVertAdjust = 1;
6249 if (infoPtr->uView != LV_VIEW_LIST) nVertDiff = rcItem.bottom - infoPtr->rcList.bottom;
6253 if (!nScrollPosWidth && !nScrollPosHeight) return TRUE;
6255 if (nScrollPosWidth)
6257 INT diff = nHorzDiff / nScrollPosWidth;
6258 if (nHorzDiff % nScrollPosWidth) diff += nHorzAdjust;
6259 LISTVIEW_HScroll(infoPtr, SB_INTERNAL, diff);
6262 if (nScrollPosHeight)
6264 INT diff = nVertDiff / nScrollPosHeight;
6265 if (nVertDiff % nScrollPosHeight) diff += nVertAdjust;
6266 LISTVIEW_VScroll(infoPtr, SB_INTERNAL, diff);
6269 return TRUE;
6272 /***
6273 * DESCRIPTION:
6274 * Searches for an item with specific characteristics.
6276 * PARAMETER(S):
6277 * [I] hwnd : window handle
6278 * [I] nStart : base item index
6279 * [I] lpFindInfo : item information to look for
6281 * RETURN:
6282 * SUCCESS : index of item
6283 * FAILURE : -1
6285 static INT LISTVIEW_FindItemW(const LISTVIEW_INFO *infoPtr, INT nStart,
6286 const LVFINDINFOW *lpFindInfo)
6288 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
6289 BOOL bWrap = FALSE, bNearest = FALSE;
6290 INT nItem = nStart + 1, nLast = infoPtr->nItemCount, nNearestItem = -1;
6291 ULONG xdist, ydist, dist, mindist = 0x7fffffff;
6292 POINT Position, Destination;
6293 LVITEMW lvItem;
6295 /* Search in virtual listviews should be done by application, not by
6296 listview control, so we just send LVN_ODFINDITEMW and return the result */
6297 if (infoPtr->dwStyle & LVS_OWNERDATA)
6299 NMLVFINDITEMW nmlv;
6301 nmlv.iStart = nStart;
6302 nmlv.lvfi = *lpFindInfo;
6303 return notify_hdr(infoPtr, LVN_ODFINDITEMW, (LPNMHDR)&nmlv.hdr);
6306 if (!lpFindInfo || nItem < 0) return -1;
6308 lvItem.mask = 0;
6309 if (lpFindInfo->flags & (LVFI_STRING | LVFI_PARTIAL) ||
6310 lpFindInfo->flags & LVFI_SUBSTRING)
6312 lvItem.mask |= LVIF_TEXT;
6313 lvItem.pszText = szDispText;
6314 lvItem.cchTextMax = DISP_TEXT_SIZE;
6317 if (lpFindInfo->flags & LVFI_WRAP)
6318 bWrap = TRUE;
6320 if ((lpFindInfo->flags & LVFI_NEARESTXY) &&
6321 (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON))
6323 POINT Origin;
6324 RECT rcArea;
6326 LISTVIEW_GetOrigin(infoPtr, &Origin);
6327 Destination.x = lpFindInfo->pt.x - Origin.x;
6328 Destination.y = lpFindInfo->pt.y - Origin.y;
6329 switch(lpFindInfo->vkDirection)
6331 case VK_DOWN: Destination.y += infoPtr->nItemHeight; break;
6332 case VK_UP: Destination.y -= infoPtr->nItemHeight; break;
6333 case VK_RIGHT: Destination.x += infoPtr->nItemWidth; break;
6334 case VK_LEFT: Destination.x -= infoPtr->nItemWidth; break;
6335 case VK_HOME: Destination.x = Destination.y = 0; break;
6336 case VK_NEXT: Destination.y += infoPtr->rcList.bottom - infoPtr->rcList.top; break;
6337 case VK_PRIOR: Destination.y -= infoPtr->rcList.bottom - infoPtr->rcList.top; break;
6338 case VK_END:
6339 LISTVIEW_GetAreaRect(infoPtr, &rcArea);
6340 Destination.x = rcArea.right;
6341 Destination.y = rcArea.bottom;
6342 break;
6343 default: ERR("Unknown vkDirection=%d\n", lpFindInfo->vkDirection);
6345 bNearest = TRUE;
6347 else Destination.x = Destination.y = 0;
6349 /* if LVFI_PARAM is specified, all other flags are ignored */
6350 if (lpFindInfo->flags & LVFI_PARAM)
6352 lvItem.mask |= LVIF_PARAM;
6353 bNearest = FALSE;
6354 lvItem.mask &= ~LVIF_TEXT;
6357 again:
6358 for (; nItem < nLast; nItem++)
6360 lvItem.iItem = nItem;
6361 lvItem.iSubItem = 0;
6362 lvItem.pszText = szDispText;
6363 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) continue;
6365 if (lvItem.mask & LVIF_PARAM)
6367 if (lpFindInfo->lParam == lvItem.lParam)
6368 return nItem;
6369 else
6370 continue;
6373 if (lvItem.mask & LVIF_TEXT)
6375 if (lpFindInfo->flags & (LVFI_PARTIAL | LVFI_SUBSTRING))
6377 WCHAR *p = strstrW(lvItem.pszText, lpFindInfo->psz);
6378 if (!p || p != lvItem.pszText) continue;
6380 else
6382 if (lstrcmpW(lvItem.pszText, lpFindInfo->psz) != 0) continue;
6386 if (!bNearest) return nItem;
6388 /* This is very inefficient. To do a good job here,
6389 * we need a sorted array of (x,y) item positions */
6390 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
6392 /* compute the distance^2 to the destination */
6393 xdist = Destination.x - Position.x;
6394 ydist = Destination.y - Position.y;
6395 dist = xdist * xdist + ydist * ydist;
6397 /* remember the distance, and item if it's closer */
6398 if (dist < mindist)
6400 mindist = dist;
6401 nNearestItem = nItem;
6405 if (bWrap)
6407 nItem = 0;
6408 nLast = min(nStart + 1, infoPtr->nItemCount);
6409 bWrap = FALSE;
6410 goto again;
6413 return nNearestItem;
6416 /***
6417 * DESCRIPTION:
6418 * Searches for an item with specific characteristics.
6420 * PARAMETER(S):
6421 * [I] hwnd : window handle
6422 * [I] nStart : base item index
6423 * [I] lpFindInfo : item information to look for
6425 * RETURN:
6426 * SUCCESS : index of item
6427 * FAILURE : -1
6429 static INT LISTVIEW_FindItemA(const LISTVIEW_INFO *infoPtr, INT nStart,
6430 const LVFINDINFOA *lpFindInfo)
6432 BOOL hasText = lpFindInfo->flags & (LVFI_STRING | LVFI_PARTIAL) ||
6433 lpFindInfo->flags & LVFI_SUBSTRING;
6434 LVFINDINFOW fiw;
6435 INT res;
6436 LPWSTR strW = NULL;
6438 memcpy(&fiw, lpFindInfo, sizeof(fiw));
6439 if (hasText) fiw.psz = strW = textdupTtoW((LPCWSTR)lpFindInfo->psz, FALSE);
6440 res = LISTVIEW_FindItemW(infoPtr, nStart, &fiw);
6441 textfreeT(strW, FALSE);
6442 return res;
6445 /***
6446 * DESCRIPTION:
6447 * Retrieves column attributes.
6449 * PARAMETER(S):
6450 * [I] infoPtr : valid pointer to the listview structure
6451 * [I] nColumn : column index
6452 * [IO] lpColumn : column information
6453 * [I] isW : if TRUE, then lpColumn is a LPLVCOLUMNW
6454 * otherwise it is in fact a LPLVCOLUMNA
6456 * RETURN:
6457 * SUCCESS : TRUE
6458 * FAILURE : FALSE
6460 static BOOL LISTVIEW_GetColumnT(const LISTVIEW_INFO *infoPtr, INT nColumn, LPLVCOLUMNW lpColumn, BOOL isW)
6462 COLUMN_INFO *lpColumnInfo;
6463 HDITEMW hdi;
6465 if (!lpColumn || nColumn < 0 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
6466 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, nColumn);
6468 /* initialize memory */
6469 ZeroMemory(&hdi, sizeof(hdi));
6471 if (lpColumn->mask & LVCF_TEXT)
6473 hdi.mask |= HDI_TEXT;
6474 hdi.pszText = lpColumn->pszText;
6475 hdi.cchTextMax = lpColumn->cchTextMax;
6478 if (lpColumn->mask & LVCF_IMAGE)
6479 hdi.mask |= HDI_IMAGE;
6481 if (lpColumn->mask & LVCF_ORDER)
6482 hdi.mask |= HDI_ORDER;
6484 if (lpColumn->mask & LVCF_SUBITEM)
6485 hdi.mask |= HDI_LPARAM;
6487 if (!SendMessageW(infoPtr->hwndHeader, isW ? HDM_GETITEMW : HDM_GETITEMA, nColumn, (LPARAM)&hdi)) return FALSE;
6489 if (lpColumn->mask & LVCF_FMT)
6490 lpColumn->fmt = lpColumnInfo->fmt;
6492 if (lpColumn->mask & LVCF_WIDTH)
6493 lpColumn->cx = lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left;
6495 if (lpColumn->mask & LVCF_IMAGE)
6496 lpColumn->iImage = hdi.iImage;
6498 if (lpColumn->mask & LVCF_ORDER)
6499 lpColumn->iOrder = hdi.iOrder;
6501 if (lpColumn->mask & LVCF_SUBITEM)
6502 lpColumn->iSubItem = hdi.lParam;
6504 if (lpColumn->mask & LVCF_MINWIDTH)
6505 lpColumn->cxMin = lpColumnInfo->cxMin;
6507 return TRUE;
6510 static inline BOOL LISTVIEW_GetColumnOrderArray(const LISTVIEW_INFO *infoPtr, INT iCount, LPINT lpiArray)
6512 if (!infoPtr->hwndHeader) return FALSE;
6513 return SendMessageW(infoPtr->hwndHeader, HDM_GETORDERARRAY, iCount, (LPARAM)lpiArray);
6516 /***
6517 * DESCRIPTION:
6518 * Retrieves the column width.
6520 * PARAMETER(S):
6521 * [I] infoPtr : valid pointer to the listview structure
6522 * [I] int : column index
6524 * RETURN:
6525 * SUCCESS : column width
6526 * FAILURE : zero
6528 static INT LISTVIEW_GetColumnWidth(const LISTVIEW_INFO *infoPtr, INT nColumn)
6530 INT nColumnWidth = 0;
6531 HDITEMW hdItem;
6533 TRACE("nColumn=%d\n", nColumn);
6535 /* we have a 'column' in LIST and REPORT mode only */
6536 switch(infoPtr->uView)
6538 case LV_VIEW_LIST:
6539 nColumnWidth = infoPtr->nItemWidth;
6540 break;
6541 case LV_VIEW_DETAILS:
6542 /* We are not using LISTVIEW_GetHeaderRect as this data is updated only after a HDN_ITEMCHANGED.
6543 * There is an application that subclasses the listview, calls LVM_GETCOLUMNWIDTH in the
6544 * HDN_ITEMCHANGED handler and goes into infinite recursion if it receives old data.
6546 hdItem.mask = HDI_WIDTH;
6547 if (!SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nColumn, (LPARAM)&hdItem))
6549 WARN("(%p): HDM_GETITEMW failed for item %d\n", infoPtr->hwndSelf, nColumn);
6550 return 0;
6552 nColumnWidth = hdItem.cxy;
6553 break;
6556 TRACE("nColumnWidth=%d\n", nColumnWidth);
6557 return nColumnWidth;
6560 /***
6561 * DESCRIPTION:
6562 * In list or report display mode, retrieves the number of items that can fit
6563 * vertically in the visible area. In icon or small icon display mode,
6564 * retrieves the total number of visible items.
6566 * PARAMETER(S):
6567 * [I] infoPtr : valid pointer to the listview structure
6569 * RETURN:
6570 * Number of fully visible items.
6572 static INT LISTVIEW_GetCountPerPage(const LISTVIEW_INFO *infoPtr)
6574 switch (infoPtr->uView)
6576 case LV_VIEW_ICON:
6577 case LV_VIEW_SMALLICON:
6578 return infoPtr->nItemCount;
6579 case LV_VIEW_DETAILS:
6580 return LISTVIEW_GetCountPerColumn(infoPtr);
6581 case LV_VIEW_LIST:
6582 return LISTVIEW_GetCountPerRow(infoPtr) * LISTVIEW_GetCountPerColumn(infoPtr);
6584 assert(FALSE);
6585 return 0;
6588 /***
6589 * DESCRIPTION:
6590 * Retrieves an image list handle.
6592 * PARAMETER(S):
6593 * [I] infoPtr : valid pointer to the listview structure
6594 * [I] nImageList : image list identifier
6596 * RETURN:
6597 * SUCCESS : image list handle
6598 * FAILURE : NULL
6600 static HIMAGELIST LISTVIEW_GetImageList(const LISTVIEW_INFO *infoPtr, INT nImageList)
6602 switch (nImageList)
6604 case LVSIL_NORMAL: return infoPtr->himlNormal;
6605 case LVSIL_SMALL: return infoPtr->himlSmall;
6606 case LVSIL_STATE: return infoPtr->himlState;
6607 case LVSIL_GROUPHEADER:
6608 FIXME("LVSIL_GROUPHEADER not supported\n");
6609 break;
6610 default:
6611 WARN("got unknown imagelist index - %d\n", nImageList);
6613 return NULL;
6616 /* LISTVIEW_GetISearchString */
6618 /***
6619 * DESCRIPTION:
6620 * Retrieves item attributes.
6622 * PARAMETER(S):
6623 * [I] hwnd : window handle
6624 * [IO] lpLVItem : item info
6625 * [I] isW : if TRUE, then lpLVItem is a LPLVITEMW,
6626 * if FALSE, then lpLVItem is a LPLVITEMA.
6628 * NOTE:
6629 * This is the internal 'GetItem' interface -- it tries to
6630 * be smart and avoid text copies, if possible, by modifying
6631 * lpLVItem->pszText to point to the text string. Please note
6632 * that this is not always possible (e.g. OWNERDATA), so on
6633 * entry you *must* supply valid values for pszText, and cchTextMax.
6634 * The only difference to the documented interface is that upon
6635 * return, you should use *only* the lpLVItem->pszText, rather than
6636 * the buffer pointer you provided on input. Most code already does
6637 * that, so it's not a problem.
6638 * For the two cases when the text must be copied (that is,
6639 * for LVM_GETITEM, and LVM_GETITEMTEXT), use LISTVIEW_GetItemExtT.
6641 * RETURN:
6642 * SUCCESS : TRUE
6643 * FAILURE : FALSE
6645 static BOOL LISTVIEW_GetItemT(const LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem, BOOL isW)
6647 ITEMHDR callbackHdr = { LPSTR_TEXTCALLBACKW, I_IMAGECALLBACK };
6648 NMLVDISPINFOW dispInfo;
6649 ITEM_INFO *lpItem;
6650 ITEMHDR* pItemHdr;
6651 HDPA hdpaSubItems;
6652 INT isubitem;
6654 TRACE("(item=%s, isW=%d)\n", debuglvitem_t(lpLVItem, isW), isW);
6656 if (!lpLVItem || lpLVItem->iItem < 0 || lpLVItem->iItem >= infoPtr->nItemCount)
6657 return FALSE;
6659 if (lpLVItem->mask == 0) return TRUE;
6660 TRACE("mask=%x\n", lpLVItem->mask);
6662 /* make a local copy */
6663 isubitem = lpLVItem->iSubItem;
6665 /* a quick optimization if all we're asked is the focus state
6666 * these queries are worth optimising since they are common,
6667 * and can be answered in constant time, without the heavy accesses */
6668 if ( (lpLVItem->mask == LVIF_STATE) && (lpLVItem->stateMask == LVIS_FOCUSED) &&
6669 !(infoPtr->uCallbackMask & LVIS_FOCUSED) )
6671 lpLVItem->state = 0;
6672 if (infoPtr->nFocusedItem == lpLVItem->iItem)
6673 lpLVItem->state |= LVIS_FOCUSED;
6674 return TRUE;
6677 ZeroMemory(&dispInfo, sizeof(dispInfo));
6679 /* if the app stores all the data, handle it separately */
6680 if (infoPtr->dwStyle & LVS_OWNERDATA)
6682 dispInfo.item.state = 0;
6684 /* apparently, we should not callback for lParam in LVS_OWNERDATA */
6685 if ((lpLVItem->mask & ~(LVIF_STATE | LVIF_PARAM)) ||
6686 ((lpLVItem->mask & LVIF_STATE) && (infoPtr->uCallbackMask & lpLVItem->stateMask)))
6688 UINT mask = lpLVItem->mask;
6690 /* NOTE: copy only fields which we _know_ are initialized, some apps
6691 * depend on the uninitialized fields being 0 */
6692 dispInfo.item.mask = lpLVItem->mask & ~LVIF_PARAM;
6693 dispInfo.item.iItem = lpLVItem->iItem;
6694 dispInfo.item.iSubItem = isubitem;
6695 if (lpLVItem->mask & LVIF_TEXT)
6697 if (lpLVItem->mask & LVIF_NORECOMPUTE)
6698 /* reset mask */
6699 dispInfo.item.mask &= ~(LVIF_TEXT | LVIF_NORECOMPUTE);
6700 else
6702 dispInfo.item.pszText = lpLVItem->pszText;
6703 dispInfo.item.cchTextMax = lpLVItem->cchTextMax;
6706 if (lpLVItem->mask & LVIF_STATE)
6707 dispInfo.item.stateMask = lpLVItem->stateMask & infoPtr->uCallbackMask;
6708 /* could be zeroed on LVIF_NORECOMPUTE case */
6709 if (dispInfo.item.mask)
6711 notify_dispinfoT(infoPtr, LVN_GETDISPINFOW, &dispInfo, isW);
6712 dispInfo.item.stateMask = lpLVItem->stateMask;
6713 if (lpLVItem->mask & (LVIF_GROUPID|LVIF_COLUMNS))
6715 /* full size structure expected - _WIN32IE >= 0x560 */
6716 *lpLVItem = dispInfo.item;
6718 else if (lpLVItem->mask & LVIF_INDENT)
6720 /* indent member expected - _WIN32IE >= 0x300 */
6721 memcpy(lpLVItem, &dispInfo.item, offsetof( LVITEMW, iGroupId ));
6723 else
6725 /* minimal structure expected */
6726 memcpy(lpLVItem, &dispInfo.item, offsetof( LVITEMW, iIndent ));
6728 lpLVItem->mask = mask;
6729 TRACE(" getdispinfo(1):lpLVItem=%s\n", debuglvitem_t(lpLVItem, isW));
6733 /* make sure lParam is zeroed out */
6734 if (lpLVItem->mask & LVIF_PARAM) lpLVItem->lParam = 0;
6736 /* callback marked pointer required here */
6737 if ((lpLVItem->mask & LVIF_TEXT) && (lpLVItem->mask & LVIF_NORECOMPUTE))
6738 lpLVItem->pszText = LPSTR_TEXTCALLBACKW;
6740 /* we store only a little state, so if we're not asked, we're done */
6741 if (!(lpLVItem->mask & LVIF_STATE) || isubitem) return TRUE;
6743 /* if focus is handled by us, report it */
6744 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_FOCUSED )
6746 lpLVItem->state &= ~LVIS_FOCUSED;
6747 if (infoPtr->nFocusedItem == lpLVItem->iItem)
6748 lpLVItem->state |= LVIS_FOCUSED;
6751 /* and do the same for selection, if we handle it */
6752 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_SELECTED )
6754 lpLVItem->state &= ~LVIS_SELECTED;
6755 if (ranges_contain(infoPtr->selectionRanges, lpLVItem->iItem))
6756 lpLVItem->state |= LVIS_SELECTED;
6759 return TRUE;
6762 /* find the item and subitem structures before we proceed */
6763 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, lpLVItem->iItem);
6764 lpItem = DPA_GetPtr(hdpaSubItems, 0);
6765 assert (lpItem);
6767 if (isubitem)
6769 SUBITEM_INFO *lpSubItem = LISTVIEW_GetSubItemPtr(hdpaSubItems, isubitem);
6770 pItemHdr = lpSubItem ? &lpSubItem->hdr : &callbackHdr;
6771 if (!lpSubItem)
6773 WARN(" iSubItem invalid (%08x), ignored.\n", isubitem);
6774 isubitem = 0;
6777 else
6778 pItemHdr = &lpItem->hdr;
6780 /* Do we need to query the state from the app? */
6781 if ((lpLVItem->mask & LVIF_STATE) && infoPtr->uCallbackMask && isubitem == 0)
6783 dispInfo.item.mask |= LVIF_STATE;
6784 dispInfo.item.stateMask = infoPtr->uCallbackMask;
6787 /* Do we need to enquire about the image? */
6788 if ((lpLVItem->mask & LVIF_IMAGE) && pItemHdr->iImage == I_IMAGECALLBACK &&
6789 (isubitem == 0 || (infoPtr->dwLvExStyle & LVS_EX_SUBITEMIMAGES)))
6791 dispInfo.item.mask |= LVIF_IMAGE;
6792 dispInfo.item.iImage = I_IMAGECALLBACK;
6795 /* Only items support indentation */
6796 if ((lpLVItem->mask & LVIF_INDENT) && lpItem->iIndent == I_INDENTCALLBACK &&
6797 (isubitem == 0))
6799 dispInfo.item.mask |= LVIF_INDENT;
6800 dispInfo.item.iIndent = I_INDENTCALLBACK;
6803 /* Apps depend on calling back for text if it is NULL or LPSTR_TEXTCALLBACKW */
6804 if ((lpLVItem->mask & LVIF_TEXT) && !(lpLVItem->mask & LVIF_NORECOMPUTE) &&
6805 !is_text(pItemHdr->pszText))
6807 dispInfo.item.mask |= LVIF_TEXT;
6808 dispInfo.item.pszText = lpLVItem->pszText;
6809 dispInfo.item.cchTextMax = lpLVItem->cchTextMax;
6810 if (dispInfo.item.pszText && dispInfo.item.cchTextMax > 0)
6811 *dispInfo.item.pszText = '\0';
6814 /* If we don't have all the requested info, query the application */
6815 if (dispInfo.item.mask)
6817 dispInfo.item.iItem = lpLVItem->iItem;
6818 dispInfo.item.iSubItem = lpLVItem->iSubItem; /* yes: the original subitem */
6819 dispInfo.item.lParam = lpItem->lParam;
6820 notify_dispinfoT(infoPtr, LVN_GETDISPINFOW, &dispInfo, isW);
6821 TRACE(" getdispinfo(2):item=%s\n", debuglvitem_t(&dispInfo.item, isW));
6824 /* we should not store values for subitems */
6825 if (isubitem) dispInfo.item.mask &= ~LVIF_DI_SETITEM;
6827 /* Now, handle the iImage field */
6828 if (dispInfo.item.mask & LVIF_IMAGE)
6830 lpLVItem->iImage = dispInfo.item.iImage;
6831 if ((dispInfo.item.mask & LVIF_DI_SETITEM) && pItemHdr->iImage == I_IMAGECALLBACK)
6832 pItemHdr->iImage = dispInfo.item.iImage;
6834 else if (lpLVItem->mask & LVIF_IMAGE)
6836 if(isubitem == 0 || (infoPtr->dwLvExStyle & LVS_EX_SUBITEMIMAGES))
6837 lpLVItem->iImage = pItemHdr->iImage;
6838 else
6839 lpLVItem->iImage = 0;
6842 /* The pszText field */
6843 if (dispInfo.item.mask & LVIF_TEXT)
6845 if ((dispInfo.item.mask & LVIF_DI_SETITEM) && pItemHdr->pszText)
6846 textsetptrT(&pItemHdr->pszText, dispInfo.item.pszText, isW);
6848 lpLVItem->pszText = dispInfo.item.pszText;
6850 else if (lpLVItem->mask & LVIF_TEXT)
6852 /* if LVN_GETDISPINFO's disabled with LVIF_NORECOMPUTE return callback placeholder */
6853 if (isW || !is_text(pItemHdr->pszText)) lpLVItem->pszText = pItemHdr->pszText;
6854 else textcpynT(lpLVItem->pszText, isW, pItemHdr->pszText, TRUE, lpLVItem->cchTextMax);
6857 /* Next is the lParam field */
6858 if (dispInfo.item.mask & LVIF_PARAM)
6860 lpLVItem->lParam = dispInfo.item.lParam;
6861 if ((dispInfo.item.mask & LVIF_DI_SETITEM))
6862 lpItem->lParam = dispInfo.item.lParam;
6864 else if (lpLVItem->mask & LVIF_PARAM)
6865 lpLVItem->lParam = lpItem->lParam;
6867 /* if this is a subitem, we're done */
6868 if (isubitem) return TRUE;
6870 /* ... the state field (this one is different due to uCallbackmask) */
6871 if (lpLVItem->mask & LVIF_STATE)
6873 lpLVItem->state = lpItem->state & lpLVItem->stateMask;
6874 if (dispInfo.item.mask & LVIF_STATE)
6876 lpLVItem->state &= ~dispInfo.item.stateMask;
6877 lpLVItem->state |= (dispInfo.item.state & dispInfo.item.stateMask);
6879 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_FOCUSED )
6881 lpLVItem->state &= ~LVIS_FOCUSED;
6882 if (infoPtr->nFocusedItem == lpLVItem->iItem)
6883 lpLVItem->state |= LVIS_FOCUSED;
6885 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_SELECTED )
6887 lpLVItem->state &= ~LVIS_SELECTED;
6888 if (ranges_contain(infoPtr->selectionRanges, lpLVItem->iItem))
6889 lpLVItem->state |= LVIS_SELECTED;
6893 /* and last, but not least, the indent field */
6894 if (dispInfo.item.mask & LVIF_INDENT)
6896 lpLVItem->iIndent = dispInfo.item.iIndent;
6897 if ((dispInfo.item.mask & LVIF_DI_SETITEM) && lpItem->iIndent == I_INDENTCALLBACK)
6898 lpItem->iIndent = dispInfo.item.iIndent;
6900 else if (lpLVItem->mask & LVIF_INDENT)
6902 lpLVItem->iIndent = lpItem->iIndent;
6905 return TRUE;
6908 /***
6909 * DESCRIPTION:
6910 * Retrieves item attributes.
6912 * PARAMETER(S):
6913 * [I] hwnd : window handle
6914 * [IO] lpLVItem : item info
6915 * [I] isW : if TRUE, then lpLVItem is a LPLVITEMW,
6916 * if FALSE, then lpLVItem is a LPLVITEMA.
6918 * NOTE:
6919 * This is the external 'GetItem' interface -- it properly copies
6920 * the text in the provided buffer.
6922 * RETURN:
6923 * SUCCESS : TRUE
6924 * FAILURE : FALSE
6926 static BOOL LISTVIEW_GetItemExtT(const LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem, BOOL isW)
6928 LPWSTR pszText;
6929 BOOL bResult;
6931 if (!lpLVItem || lpLVItem->iItem < 0 || lpLVItem->iItem >= infoPtr->nItemCount)
6932 return FALSE;
6934 pszText = lpLVItem->pszText;
6935 bResult = LISTVIEW_GetItemT(infoPtr, lpLVItem, isW);
6936 if (bResult && (lpLVItem->mask & LVIF_TEXT) && lpLVItem->pszText != pszText)
6938 if (lpLVItem->pszText != LPSTR_TEXTCALLBACKW)
6939 textcpynT(pszText, isW, lpLVItem->pszText, isW, lpLVItem->cchTextMax);
6940 else
6941 pszText = LPSTR_TEXTCALLBACKW;
6943 lpLVItem->pszText = pszText;
6945 return bResult;
6949 /***
6950 * DESCRIPTION:
6951 * Retrieves the position (upper-left) of the listview control item.
6952 * Note that for LVS_ICON style, the upper-left is that of the icon
6953 * and not the bounding box.
6955 * PARAMETER(S):
6956 * [I] infoPtr : valid pointer to the listview structure
6957 * [I] nItem : item index
6958 * [O] lpptPosition : coordinate information
6960 * RETURN:
6961 * SUCCESS : TRUE
6962 * FAILURE : FALSE
6964 static BOOL LISTVIEW_GetItemPosition(const LISTVIEW_INFO *infoPtr, INT nItem, LPPOINT lpptPosition)
6966 POINT Origin;
6968 TRACE("(nItem=%d, lpptPosition=%p)\n", nItem, lpptPosition);
6970 if (!lpptPosition || nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
6972 LISTVIEW_GetOrigin(infoPtr, &Origin);
6973 LISTVIEW_GetItemOrigin(infoPtr, nItem, lpptPosition);
6975 if (infoPtr->uView == LV_VIEW_ICON)
6977 lpptPosition->x += (infoPtr->nItemWidth - infoPtr->iconSize.cx) / 2;
6978 lpptPosition->y += ICON_TOP_PADDING;
6980 lpptPosition->x += Origin.x;
6981 lpptPosition->y += Origin.y;
6983 TRACE (" lpptPosition=%s\n", wine_dbgstr_point(lpptPosition));
6984 return TRUE;
6988 /***
6989 * DESCRIPTION:
6990 * Retrieves the bounding rectangle for a listview control item.
6992 * PARAMETER(S):
6993 * [I] infoPtr : valid pointer to the listview structure
6994 * [I] nItem : item index
6995 * [IO] lprc : bounding rectangle coordinates
6996 * lprc->left specifies the portion of the item for which the bounding
6997 * rectangle will be retrieved.
6999 * LVIR_BOUNDS Returns the bounding rectangle of the entire item,
7000 * including the icon and label.
7002 * * For LVS_ICON
7003 * * Experiment shows that native control returns:
7004 * * width = min (48, length of text line)
7005 * * .left = position.x - (width - iconsize.cx)/2
7006 * * .right = .left + width
7007 * * height = #lines of text * ntmHeight + icon height + 8
7008 * * .top = position.y - 2
7009 * * .bottom = .top + height
7010 * * separation between items .y = itemSpacing.cy - height
7011 * * .x = itemSpacing.cx - width
7012 * LVIR_ICON Returns the bounding rectangle of the icon or small icon.
7014 * * For LVS_ICON
7015 * * Experiment shows that native control returns:
7016 * * width = iconSize.cx + 16
7017 * * .left = position.x - (width - iconsize.cx)/2
7018 * * .right = .left + width
7019 * * height = iconSize.cy + 4
7020 * * .top = position.y - 2
7021 * * .bottom = .top + height
7022 * * separation between items .y = itemSpacing.cy - height
7023 * * .x = itemSpacing.cx - width
7024 * LVIR_LABEL Returns the bounding rectangle of the item text.
7026 * * For LVS_ICON
7027 * * Experiment shows that native control returns:
7028 * * width = text length
7029 * * .left = position.x - width/2
7030 * * .right = .left + width
7031 * * height = ntmH * linecount + 2
7032 * * .top = position.y + iconSize.cy + 6
7033 * * .bottom = .top + height
7034 * * separation between items .y = itemSpacing.cy - height
7035 * * .x = itemSpacing.cx - width
7036 * LVIR_SELECTBOUNDS Returns the union of the LVIR_ICON and LVIR_LABEL
7037 * rectangles, but excludes columns in report view.
7039 * RETURN:
7040 * SUCCESS : TRUE
7041 * FAILURE : FALSE
7043 * NOTES
7044 * Note that the bounding rectangle of the label in the LVS_ICON view depends
7045 * upon whether the window has the focus currently and on whether the item
7046 * is the one with the focus. Ensure that the control's record of which
7047 * item has the focus agrees with the items' records.
7049 static BOOL LISTVIEW_GetItemRect(const LISTVIEW_INFO *infoPtr, INT nItem, LPRECT lprc)
7051 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
7052 BOOL doLabel = TRUE, oversizedBox = FALSE;
7053 POINT Position, Origin;
7054 LVITEMW lvItem;
7055 LONG mode;
7057 TRACE("(hwnd=%p, nItem=%d, lprc=%p)\n", infoPtr->hwndSelf, nItem, lprc);
7059 if (!lprc || nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
7061 LISTVIEW_GetOrigin(infoPtr, &Origin);
7062 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
7064 /* Be smart and try to figure out the minimum we have to do */
7065 if (lprc->left == LVIR_ICON) doLabel = FALSE;
7066 if (infoPtr->uView == LV_VIEW_DETAILS && lprc->left == LVIR_BOUNDS) doLabel = FALSE;
7067 if (infoPtr->uView == LV_VIEW_ICON && lprc->left != LVIR_ICON &&
7068 infoPtr->bFocus && LISTVIEW_GetItemState(infoPtr, nItem, LVIS_FOCUSED))
7069 oversizedBox = TRUE;
7071 /* get what we need from the item before hand, so we make
7072 * only one request. This can speed up things, if data
7073 * is stored on the app side */
7074 lvItem.mask = 0;
7075 if (infoPtr->uView == LV_VIEW_DETAILS) lvItem.mask |= LVIF_INDENT;
7076 if (doLabel) lvItem.mask |= LVIF_TEXT;
7077 lvItem.iItem = nItem;
7078 lvItem.iSubItem = 0;
7079 lvItem.pszText = szDispText;
7080 lvItem.cchTextMax = DISP_TEXT_SIZE;
7081 if (lvItem.mask && !LISTVIEW_GetItemW(infoPtr, &lvItem)) return FALSE;
7082 /* we got the state already up, simulate it here, to avoid a reget */
7083 if (infoPtr->uView == LV_VIEW_ICON && (lprc->left != LVIR_ICON))
7085 lvItem.mask |= LVIF_STATE;
7086 lvItem.stateMask = LVIS_FOCUSED;
7087 lvItem.state = (oversizedBox ? LVIS_FOCUSED : 0);
7090 if (infoPtr->uView == LV_VIEW_DETAILS && (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) && lprc->left == LVIR_SELECTBOUNDS)
7091 lprc->left = LVIR_BOUNDS;
7093 mode = lprc->left;
7094 switch(lprc->left)
7096 case LVIR_ICON:
7097 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, NULL, lprc, NULL, NULL);
7098 break;
7100 case LVIR_LABEL:
7101 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, NULL, NULL, NULL, lprc);
7102 break;
7104 case LVIR_BOUNDS:
7105 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, lprc, NULL, NULL, NULL, NULL);
7106 break;
7108 case LVIR_SELECTBOUNDS:
7109 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, lprc, NULL, NULL, NULL);
7110 break;
7112 default:
7113 WARN("Unknown value: %d\n", lprc->left);
7114 return FALSE;
7117 if (infoPtr->uView == LV_VIEW_DETAILS)
7119 if (mode != LVIR_BOUNDS)
7120 OffsetRect(lprc, Origin.x + LISTVIEW_GetColumnInfo(infoPtr, 0)->rcHeader.left,
7121 Position.y + Origin.y);
7122 else
7123 OffsetRect(lprc, Origin.x, Position.y + Origin.y);
7125 else
7126 OffsetRect(lprc, Position.x + Origin.x, Position.y + Origin.y);
7128 TRACE(" rect=%s\n", wine_dbgstr_rect(lprc));
7130 return TRUE;
7133 /***
7134 * DESCRIPTION:
7135 * Retrieves the spacing between listview control items.
7137 * PARAMETER(S):
7138 * [I] infoPtr : valid pointer to the listview structure
7139 * [IO] lprc : rectangle to receive the output
7140 * on input, lprc->top = nSubItem
7141 * lprc->left = LVIR_ICON | LVIR_BOUNDS | LVIR_LABEL
7143 * NOTE: for subItem = 0, we should return the bounds of the _entire_ item,
7144 * not only those of the first column.
7146 * RETURN:
7147 * TRUE: success
7148 * FALSE: failure
7150 static BOOL LISTVIEW_GetSubItemRect(const LISTVIEW_INFO *infoPtr, INT item, LPRECT lprc)
7152 RECT rect = { 0, 0, 0, 0 };
7153 POINT origin;
7154 INT y;
7156 if (!lprc) return FALSE;
7158 TRACE("(item=%d, subitem=%d, type=%d)\n", item, lprc->top, lprc->left);
7159 /* Subitem of '0' means item itself, and this works for all control view modes */
7160 if (lprc->top == 0)
7161 return LISTVIEW_GetItemRect(infoPtr, item, lprc);
7163 if (infoPtr->uView != LV_VIEW_DETAILS) return FALSE;
7165 LISTVIEW_GetOrigin(infoPtr, &origin);
7166 /* this works for any item index, no matter if it exists or not */
7167 y = item * infoPtr->nItemHeight + origin.y;
7169 if (infoPtr->hwndHeader && SendMessageW(infoPtr->hwndHeader, HDM_GETITEMRECT, lprc->top, (LPARAM)&rect))
7171 rect.top = 0;
7172 rect.bottom = infoPtr->nItemHeight;
7174 else
7176 /* Native implementation is broken for this case and garbage is left for left and right fields,
7177 we zero them to get predictable output */
7178 lprc->left = lprc->right = lprc->top = 0;
7179 lprc->bottom = infoPtr->nItemHeight;
7180 OffsetRect(lprc, origin.x, y);
7181 TRACE("return rect %s\n", wine_dbgstr_rect(lprc));
7182 return TRUE;
7185 switch (lprc->left)
7187 case LVIR_ICON:
7189 /* it doesn't matter if main item actually has an icon, if imagelist is set icon width is returned */
7190 if (infoPtr->himlSmall)
7191 rect.right = rect.left + infoPtr->iconSize.cx;
7192 else
7193 rect.right = rect.left;
7195 rect.bottom = rect.top + infoPtr->iconSize.cy;
7196 break;
7198 case LVIR_LABEL:
7199 case LVIR_BOUNDS:
7200 break;
7202 default:
7203 ERR("Unknown bounds=%d\n", lprc->left);
7204 return FALSE;
7207 OffsetRect(&rect, origin.x, y);
7208 *lprc = rect;
7209 TRACE("return rect %s\n", wine_dbgstr_rect(lprc));
7211 return TRUE;
7214 /***
7215 * DESCRIPTION:
7216 * Retrieves the spacing between listview control items.
7218 * PARAMETER(S):
7219 * [I] infoPtr : valid pointer to the listview structure
7220 * [I] bSmall : flag for small or large icon
7222 * RETURN:
7223 * Horizontal + vertical spacing
7225 static LONG LISTVIEW_GetItemSpacing(const LISTVIEW_INFO *infoPtr, BOOL bSmall)
7227 LONG lResult;
7229 if (!bSmall)
7231 lResult = MAKELONG(infoPtr->iconSpacing.cx, infoPtr->iconSpacing.cy);
7233 else
7235 if (infoPtr->uView == LV_VIEW_ICON)
7236 lResult = MAKELONG(DEFAULT_COLUMN_WIDTH, GetSystemMetrics(SM_CXSMICON)+HEIGHT_PADDING);
7237 else
7238 lResult = MAKELONG(infoPtr->nItemWidth, infoPtr->nItemHeight);
7240 return lResult;
7243 /***
7244 * DESCRIPTION:
7245 * Retrieves the state of a listview control item.
7247 * PARAMETER(S):
7248 * [I] infoPtr : valid pointer to the listview structure
7249 * [I] nItem : item index
7250 * [I] uMask : state mask
7252 * RETURN:
7253 * State specified by the mask.
7255 static UINT LISTVIEW_GetItemState(const LISTVIEW_INFO *infoPtr, INT nItem, UINT uMask)
7257 LVITEMW lvItem;
7259 if (nItem < 0 || nItem >= infoPtr->nItemCount) return 0;
7261 lvItem.iItem = nItem;
7262 lvItem.iSubItem = 0;
7263 lvItem.mask = LVIF_STATE;
7264 lvItem.stateMask = uMask;
7265 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return 0;
7267 return lvItem.state & uMask;
7270 /***
7271 * DESCRIPTION:
7272 * Retrieves the text of a listview control item or subitem.
7274 * PARAMETER(S):
7275 * [I] hwnd : window handle
7276 * [I] nItem : item index
7277 * [IO] lpLVItem : item information
7278 * [I] isW : TRUE if lpLVItem is Unicode
7280 * RETURN:
7281 * SUCCESS : string length
7282 * FAILURE : 0
7284 static INT LISTVIEW_GetItemTextT(const LISTVIEW_INFO *infoPtr, INT nItem, LPLVITEMW lpLVItem, BOOL isW)
7286 if (!lpLVItem || nItem < 0 || nItem >= infoPtr->nItemCount) return 0;
7288 lpLVItem->mask = LVIF_TEXT;
7289 lpLVItem->iItem = nItem;
7290 if (!LISTVIEW_GetItemExtT(infoPtr, lpLVItem, isW)) return 0;
7292 return textlenT(lpLVItem->pszText, isW);
7295 /***
7296 * DESCRIPTION:
7297 * Searches for an item based on properties + relationships.
7299 * PARAMETER(S):
7300 * [I] infoPtr : valid pointer to the listview structure
7301 * [I] nItem : item index
7302 * [I] uFlags : relationship flag
7304 * RETURN:
7305 * SUCCESS : item index
7306 * FAILURE : -1
7308 static INT LISTVIEW_GetNextItem(const LISTVIEW_INFO *infoPtr, INT nItem, UINT uFlags)
7310 UINT uMask = 0;
7311 LVFINDINFOW lvFindInfo;
7312 INT nCountPerColumn;
7313 INT nCountPerRow;
7314 INT i;
7316 TRACE("nItem=%d, uFlags=%x, nItemCount=%d\n", nItem, uFlags, infoPtr->nItemCount);
7317 if (nItem < -1 || nItem >= infoPtr->nItemCount) return -1;
7319 ZeroMemory(&lvFindInfo, sizeof(lvFindInfo));
7321 if (uFlags & LVNI_CUT)
7322 uMask |= LVIS_CUT;
7324 if (uFlags & LVNI_DROPHILITED)
7325 uMask |= LVIS_DROPHILITED;
7327 if (uFlags & LVNI_FOCUSED)
7328 uMask |= LVIS_FOCUSED;
7330 if (uFlags & LVNI_SELECTED)
7331 uMask |= LVIS_SELECTED;
7333 /* if we're asked for the focused item, that's only one,
7334 * so it's worth optimizing */
7335 if (uFlags & LVNI_FOCUSED)
7337 if ((LISTVIEW_GetItemState(infoPtr, infoPtr->nFocusedItem, uMask) & uMask) != uMask) return -1;
7338 return (infoPtr->nFocusedItem == nItem) ? -1 : infoPtr->nFocusedItem;
7341 if (uFlags & LVNI_ABOVE)
7343 if ((infoPtr->uView == LV_VIEW_LIST) || (infoPtr->uView == LV_VIEW_DETAILS))
7345 while (nItem >= 0)
7347 nItem--;
7348 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7349 return nItem;
7352 else
7354 /* Special case for autoarrange - move 'til the top of a list */
7355 if (is_autoarrange(infoPtr))
7357 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
7358 while (nItem - nCountPerRow >= 0)
7360 nItem -= nCountPerRow;
7361 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7362 return nItem;
7364 return -1;
7366 lvFindInfo.flags = LVFI_NEARESTXY;
7367 lvFindInfo.vkDirection = VK_UP;
7368 LISTVIEW_GetItemPosition(infoPtr, nItem, &lvFindInfo.pt);
7369 while ((nItem = LISTVIEW_FindItemW(infoPtr, nItem, &lvFindInfo)) != -1)
7371 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7372 return nItem;
7376 else if (uFlags & LVNI_BELOW)
7378 if ((infoPtr->uView == LV_VIEW_LIST) || (infoPtr->uView == LV_VIEW_DETAILS))
7380 while (nItem < infoPtr->nItemCount)
7382 nItem++;
7383 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7384 return nItem;
7387 else
7389 /* Special case for autoarrange - move 'til the bottom of a list */
7390 if (is_autoarrange(infoPtr))
7392 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
7393 while (nItem + nCountPerRow < infoPtr->nItemCount )
7395 nItem += nCountPerRow;
7396 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7397 return nItem;
7399 return -1;
7401 lvFindInfo.flags = LVFI_NEARESTXY;
7402 lvFindInfo.vkDirection = VK_DOWN;
7403 LISTVIEW_GetItemPosition(infoPtr, nItem, &lvFindInfo.pt);
7404 while ((nItem = LISTVIEW_FindItemW(infoPtr, nItem, &lvFindInfo)) != -1)
7406 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7407 return nItem;
7411 else if (uFlags & LVNI_TOLEFT)
7413 if (infoPtr->uView == LV_VIEW_LIST)
7415 nCountPerColumn = LISTVIEW_GetCountPerColumn(infoPtr);
7416 while (nItem - nCountPerColumn >= 0)
7418 nItem -= nCountPerColumn;
7419 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7420 return nItem;
7423 else if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
7425 /* Special case for autoarrange - move 'til the beginning of a row */
7426 if (is_autoarrange(infoPtr))
7428 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
7429 while (nItem % nCountPerRow > 0)
7431 nItem --;
7432 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7433 return nItem;
7435 return -1;
7437 lvFindInfo.flags = LVFI_NEARESTXY;
7438 lvFindInfo.vkDirection = VK_LEFT;
7439 LISTVIEW_GetItemPosition(infoPtr, nItem, &lvFindInfo.pt);
7440 while ((nItem = LISTVIEW_FindItemW(infoPtr, nItem, &lvFindInfo)) != -1)
7442 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7443 return nItem;
7447 else if (uFlags & LVNI_TORIGHT)
7449 if (infoPtr->uView == LV_VIEW_LIST)
7451 nCountPerColumn = LISTVIEW_GetCountPerColumn(infoPtr);
7452 while (nItem + nCountPerColumn < infoPtr->nItemCount)
7454 nItem += nCountPerColumn;
7455 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7456 return nItem;
7459 else if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
7461 /* Special case for autoarrange - move 'til the end of a row */
7462 if (is_autoarrange(infoPtr))
7464 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
7465 while (nItem % nCountPerRow < nCountPerRow - 1 )
7467 nItem ++;
7468 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7469 return nItem;
7471 return -1;
7473 lvFindInfo.flags = LVFI_NEARESTXY;
7474 lvFindInfo.vkDirection = VK_RIGHT;
7475 LISTVIEW_GetItemPosition(infoPtr, nItem, &lvFindInfo.pt);
7476 while ((nItem = LISTVIEW_FindItemW(infoPtr, nItem, &lvFindInfo)) != -1)
7478 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7479 return nItem;
7483 else
7485 nItem++;
7487 /* search by index */
7488 for (i = nItem; i < infoPtr->nItemCount; i++)
7490 if ((LISTVIEW_GetItemState(infoPtr, i, uMask) & uMask) == uMask)
7491 return i;
7495 return -1;
7498 /* LISTVIEW_GetNumberOfWorkAreas */
7500 /***
7501 * DESCRIPTION:
7502 * Retrieves the origin coordinates when in icon or small icon display mode.
7504 * PARAMETER(S):
7505 * [I] infoPtr : valid pointer to the listview structure
7506 * [O] lpptOrigin : coordinate information
7508 * RETURN:
7509 * None.
7511 static void LISTVIEW_GetOrigin(const LISTVIEW_INFO *infoPtr, LPPOINT lpptOrigin)
7513 INT nHorzPos = 0, nVertPos = 0;
7514 SCROLLINFO scrollInfo;
7516 scrollInfo.cbSize = sizeof(SCROLLINFO);
7517 scrollInfo.fMask = SIF_POS;
7519 if (GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo))
7520 nHorzPos = scrollInfo.nPos;
7521 if (GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
7522 nVertPos = scrollInfo.nPos;
7524 TRACE("nHorzPos=%d, nVertPos=%d\n", nHorzPos, nVertPos);
7526 lpptOrigin->x = infoPtr->rcList.left;
7527 lpptOrigin->y = infoPtr->rcList.top;
7528 if (infoPtr->uView == LV_VIEW_LIST)
7529 nHorzPos *= infoPtr->nItemWidth;
7530 else if (infoPtr->uView == LV_VIEW_DETAILS)
7531 nVertPos *= infoPtr->nItemHeight;
7533 lpptOrigin->x -= nHorzPos;
7534 lpptOrigin->y -= nVertPos;
7536 TRACE(" origin=%s\n", wine_dbgstr_point(lpptOrigin));
7539 /***
7540 * DESCRIPTION:
7541 * Retrieves the width of a string.
7543 * PARAMETER(S):
7544 * [I] hwnd : window handle
7545 * [I] lpszText : text string to process
7546 * [I] isW : TRUE if lpszText is Unicode, FALSE otherwise
7548 * RETURN:
7549 * SUCCESS : string width (in pixels)
7550 * FAILURE : zero
7552 static INT LISTVIEW_GetStringWidthT(const LISTVIEW_INFO *infoPtr, LPCWSTR lpszText, BOOL isW)
7554 SIZE stringSize;
7556 stringSize.cx = 0;
7557 if (is_text(lpszText))
7559 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
7560 HDC hdc = GetDC(infoPtr->hwndSelf);
7561 HFONT hOldFont = SelectObject(hdc, hFont);
7563 if (isW)
7564 GetTextExtentPointW(hdc, lpszText, lstrlenW(lpszText), &stringSize);
7565 else
7566 GetTextExtentPointA(hdc, (LPCSTR)lpszText, lstrlenA((LPCSTR)lpszText), &stringSize);
7567 SelectObject(hdc, hOldFont);
7568 ReleaseDC(infoPtr->hwndSelf, hdc);
7570 return stringSize.cx;
7573 /***
7574 * DESCRIPTION:
7575 * Determines which listview item is located at the specified position.
7577 * PARAMETER(S):
7578 * [I] infoPtr : valid pointer to the listview structure
7579 * [IO] lpht : hit test information
7580 * [I] subitem : fill out iSubItem.
7581 * [I] select : return the index only if the hit selects the item
7583 * NOTE:
7584 * (mm 20001022): We must not allow iSubItem to be touched, for
7585 * an app might pass only a structure with space up to iItem!
7586 * (MS Office 97 does that for instance in the file open dialog)
7588 * RETURN:
7589 * SUCCESS : item index
7590 * FAILURE : -1
7592 static INT LISTVIEW_HitTest(const LISTVIEW_INFO *infoPtr, LPLVHITTESTINFO lpht, BOOL subitem, BOOL select)
7594 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
7595 RECT rcBox, rcBounds, rcState, rcIcon, rcLabel, rcSearch;
7596 POINT Origin, Position, opt;
7597 BOOL is_fullrow;
7598 LVITEMW lvItem;
7599 ITERATOR i;
7600 INT iItem;
7602 TRACE("(pt=%s, subitem=%d, select=%d)\n", wine_dbgstr_point(&lpht->pt), subitem, select);
7604 lpht->flags = 0;
7605 lpht->iItem = -1;
7606 if (subitem) lpht->iSubItem = 0;
7608 LISTVIEW_GetOrigin(infoPtr, &Origin);
7610 /* set whole list relation flags */
7611 if (subitem && infoPtr->uView == LV_VIEW_DETAILS)
7613 /* LVM_SUBITEMHITTEST checks left bound of possible client area */
7614 if (infoPtr->rcList.left > lpht->pt.x && Origin.x < lpht->pt.x)
7615 lpht->flags |= LVHT_TOLEFT;
7617 if (lpht->pt.y < infoPtr->rcList.top && lpht->pt.y >= 0)
7618 opt.y = lpht->pt.y + infoPtr->rcList.top;
7619 else
7620 opt.y = lpht->pt.y;
7622 if (infoPtr->rcList.bottom < opt.y)
7623 lpht->flags |= LVHT_BELOW;
7625 else
7627 if (infoPtr->rcList.left > lpht->pt.x)
7628 lpht->flags |= LVHT_TOLEFT;
7629 else if (infoPtr->rcList.right < lpht->pt.x)
7630 lpht->flags |= LVHT_TORIGHT;
7632 if (infoPtr->rcList.top > lpht->pt.y)
7633 lpht->flags |= LVHT_ABOVE;
7634 else if (infoPtr->rcList.bottom < lpht->pt.y)
7635 lpht->flags |= LVHT_BELOW;
7638 /* even if item is invalid try to find subitem */
7639 if (infoPtr->uView == LV_VIEW_DETAILS && subitem)
7641 RECT *pRect;
7642 INT j;
7644 opt.x = lpht->pt.x - Origin.x;
7646 lpht->iSubItem = -1;
7647 for (j = 0; j < DPA_GetPtrCount(infoPtr->hdpaColumns); j++)
7649 pRect = &LISTVIEW_GetColumnInfo(infoPtr, j)->rcHeader;
7651 if ((opt.x >= pRect->left) && (opt.x < pRect->right))
7653 lpht->iSubItem = j;
7654 break;
7657 TRACE("lpht->iSubItem=%d\n", lpht->iSubItem);
7659 /* if we're outside horizontal columns bounds there's nothing to test further */
7660 if (lpht->iSubItem == -1)
7662 lpht->iItem = -1;
7663 lpht->flags = LVHT_NOWHERE;
7664 return -1;
7668 TRACE("lpht->flags=0x%x\n", lpht->flags);
7669 if (lpht->flags) return -1;
7671 lpht->flags |= LVHT_NOWHERE;
7673 /* first deal with the large items */
7674 rcSearch.left = lpht->pt.x;
7675 rcSearch.top = lpht->pt.y;
7676 rcSearch.right = rcSearch.left + 1;
7677 rcSearch.bottom = rcSearch.top + 1;
7679 iterator_frameditems(&i, infoPtr, &rcSearch);
7680 iterator_next(&i); /* go to first item in the sequence */
7681 iItem = i.nItem;
7682 iterator_destroy(&i);
7684 TRACE("lpht->iItem=%d\n", iItem);
7685 if (iItem == -1) return -1;
7687 lvItem.mask = LVIF_STATE | LVIF_TEXT;
7688 if (infoPtr->uView == LV_VIEW_DETAILS) lvItem.mask |= LVIF_INDENT;
7689 lvItem.stateMask = LVIS_STATEIMAGEMASK;
7690 if (infoPtr->uView == LV_VIEW_ICON) lvItem.stateMask |= LVIS_FOCUSED;
7691 lvItem.iItem = iItem;
7692 lvItem.iSubItem = subitem ? lpht->iSubItem : 0;
7693 lvItem.pszText = szDispText;
7694 lvItem.cchTextMax = DISP_TEXT_SIZE;
7695 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return -1;
7696 if (!infoPtr->bFocus) lvItem.state &= ~LVIS_FOCUSED;
7698 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, &rcBox, NULL, &rcIcon, &rcState, &rcLabel);
7699 LISTVIEW_GetItemOrigin(infoPtr, iItem, &Position);
7700 opt.x = lpht->pt.x - Position.x - Origin.x;
7702 if (lpht->pt.y < infoPtr->rcList.top && lpht->pt.y >= 0)
7703 opt.y = lpht->pt.y - Position.y - Origin.y + infoPtr->rcList.top;
7704 else
7705 opt.y = lpht->pt.y - Position.y - Origin.y;
7707 if (infoPtr->uView == LV_VIEW_DETAILS)
7709 rcBounds = rcBox;
7710 if (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT)
7711 opt.x = lpht->pt.x - Origin.x;
7713 else
7715 UnionRect(&rcBounds, &rcIcon, &rcLabel);
7716 UnionRect(&rcBounds, &rcBounds, &rcState);
7718 TRACE("rcBounds=%s\n", wine_dbgstr_rect(&rcBounds));
7719 if (!PtInRect(&rcBounds, opt)) return -1;
7721 /* That's a special case - row rectangle is used as item rectangle and
7722 returned flags contain all item parts. */
7723 is_fullrow = (infoPtr->uView == LV_VIEW_DETAILS) && ((infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) || (infoPtr->dwStyle & LVS_OWNERDRAWFIXED));
7725 if (PtInRect(&rcIcon, opt))
7726 lpht->flags |= LVHT_ONITEMICON;
7727 else if (PtInRect(&rcLabel, opt))
7728 lpht->flags |= LVHT_ONITEMLABEL;
7729 else if (infoPtr->himlState && PtInRect(&rcState, opt))
7730 lpht->flags |= LVHT_ONITEMSTATEICON;
7731 if (is_fullrow && !(lpht->flags & LVHT_ONITEM))
7733 lpht->flags = LVHT_ONITEM | LVHT_ABOVE;
7735 if (lpht->flags & LVHT_ONITEM)
7736 lpht->flags &= ~LVHT_NOWHERE;
7737 TRACE("lpht->flags=0x%x\n", lpht->flags);
7739 if (select && !is_fullrow)
7741 if (infoPtr->uView == LV_VIEW_DETAILS)
7743 /* get main item bounds */
7744 lvItem.iSubItem = 0;
7745 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, &rcBox, NULL, &rcIcon, &rcState, &rcLabel);
7746 UnionRect(&rcBounds, &rcIcon, &rcLabel);
7747 UnionRect(&rcBounds, &rcBounds, &rcState);
7749 if (!PtInRect(&rcBounds, opt)) iItem = -1;
7751 return lpht->iItem = iItem;
7754 /***
7755 * DESCRIPTION:
7756 * Inserts a new item in the listview control.
7758 * PARAMETER(S):
7759 * [I] infoPtr : valid pointer to the listview structure
7760 * [I] lpLVItem : item information
7761 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
7763 * RETURN:
7764 * SUCCESS : new item index
7765 * FAILURE : -1
7767 static INT LISTVIEW_InsertItemT(LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem, BOOL isW)
7769 INT nItem;
7770 HDPA hdpaSubItems;
7771 NMLISTVIEW nmlv;
7772 ITEM_INFO *lpItem;
7773 ITEM_ID *lpID;
7774 BOOL is_sorted, has_changed;
7775 LVITEMW item;
7776 HWND hwndSelf = infoPtr->hwndSelf;
7778 TRACE("(item=%s, isW=%d)\n", debuglvitem_t(lpLVItem, isW), isW);
7780 if (infoPtr->dwStyle & LVS_OWNERDATA) return infoPtr->nItemCount++;
7782 /* make sure it's an item, and not a subitem; cannot insert a subitem */
7783 if (!lpLVItem || lpLVItem->iSubItem) return -1;
7785 if (!is_assignable_item(lpLVItem, infoPtr->dwStyle)) return -1;
7787 if (!(lpItem = Alloc(sizeof(ITEM_INFO)))) return -1;
7789 /* insert item in listview control data structure */
7790 if ( !(hdpaSubItems = DPA_Create(8)) ) goto fail;
7791 if ( !DPA_SetPtr(hdpaSubItems, 0, lpItem) ) assert (FALSE);
7793 /* link with id struct */
7794 if (!(lpID = Alloc(sizeof(ITEM_ID)))) goto fail;
7795 lpItem->id = lpID;
7796 lpID->item = hdpaSubItems;
7797 lpID->id = get_next_itemid(infoPtr);
7798 if ( DPA_InsertPtr(infoPtr->hdpaItemIds, infoPtr->nItemCount, lpID) == -1) goto fail;
7800 is_sorted = (infoPtr->dwStyle & (LVS_SORTASCENDING | LVS_SORTDESCENDING)) &&
7801 !(infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (LPSTR_TEXTCALLBACKW != lpLVItem->pszText);
7803 if (lpLVItem->iItem < 0 && !is_sorted) return -1;
7805 /* calculate new item index */
7806 if (is_sorted)
7808 HDPA hItem;
7809 ITEM_INFO *item_s;
7810 INT i = 0, cmpv;
7812 while (i < infoPtr->nItemCount)
7814 hItem = DPA_GetPtr( infoPtr->hdpaItems, i);
7815 item_s = DPA_GetPtr(hItem, 0);
7817 cmpv = textcmpWT(item_s->hdr.pszText, lpLVItem->pszText, isW);
7818 if (infoPtr->dwStyle & LVS_SORTDESCENDING) cmpv *= -1;
7820 if (cmpv >= 0) break;
7821 i++;
7823 nItem = i;
7825 else
7826 nItem = min(lpLVItem->iItem, infoPtr->nItemCount);
7828 TRACE("inserting at %d, sorted=%d, count=%d, iItem=%d\n", nItem, is_sorted, infoPtr->nItemCount, lpLVItem->iItem);
7829 nItem = DPA_InsertPtr( infoPtr->hdpaItems, nItem, hdpaSubItems );
7830 if (nItem == -1) goto fail;
7831 infoPtr->nItemCount++;
7833 /* shift indices first so they don't get tangled */
7834 LISTVIEW_ShiftIndices(infoPtr, nItem, 1);
7836 /* set the item attributes */
7837 if (lpLVItem->mask & (LVIF_GROUPID|LVIF_COLUMNS))
7839 /* full size structure expected - _WIN32IE >= 0x560 */
7840 item = *lpLVItem;
7842 else if (lpLVItem->mask & LVIF_INDENT)
7844 /* indent member expected - _WIN32IE >= 0x300 */
7845 memcpy(&item, lpLVItem, offsetof( LVITEMW, iGroupId ));
7847 else
7849 /* minimal structure expected */
7850 memcpy(&item, lpLVItem, offsetof( LVITEMW, iIndent ));
7852 item.iItem = nItem;
7853 if (infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES)
7855 if (item.mask & LVIF_STATE)
7857 item.stateMask |= LVIS_STATEIMAGEMASK;
7858 item.state &= ~LVIS_STATEIMAGEMASK;
7859 item.state |= INDEXTOSTATEIMAGEMASK(1);
7861 else
7863 item.mask |= LVIF_STATE;
7864 item.stateMask = LVIS_STATEIMAGEMASK;
7865 item.state = INDEXTOSTATEIMAGEMASK(1);
7869 if (!set_main_item(infoPtr, &item, TRUE, isW, &has_changed)) goto undo;
7871 /* make room for the position, if we are in the right mode */
7872 if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
7874 if (DPA_InsertPtr(infoPtr->hdpaPosX, nItem, 0) == -1)
7875 goto undo;
7876 if (DPA_InsertPtr(infoPtr->hdpaPosY, nItem, 0) == -1)
7878 DPA_DeletePtr(infoPtr->hdpaPosX, nItem);
7879 goto undo;
7883 /* send LVN_INSERTITEM notification */
7884 memset(&nmlv, 0, sizeof(NMLISTVIEW));
7885 nmlv.iItem = nItem;
7886 nmlv.lParam = lpItem->lParam;
7887 notify_listview(infoPtr, LVN_INSERTITEM, &nmlv);
7888 if (!IsWindow(hwndSelf))
7889 return -1;
7891 /* align items (set position of each item) */
7892 if (infoPtr->uView == LV_VIEW_SMALLICON || infoPtr->uView == LV_VIEW_ICON)
7894 POINT pt;
7896 if (infoPtr->dwStyle & LVS_ALIGNLEFT)
7897 LISTVIEW_NextIconPosLeft(infoPtr, &pt);
7898 else
7899 LISTVIEW_NextIconPosTop(infoPtr, &pt);
7901 LISTVIEW_MoveIconTo(infoPtr, nItem, &pt, TRUE);
7904 /* now is the invalidation fun */
7905 LISTVIEW_ScrollOnInsert(infoPtr, nItem, 1);
7906 return nItem;
7908 undo:
7909 LISTVIEW_ShiftIndices(infoPtr, nItem, -1);
7910 LISTVIEW_ShiftFocus(infoPtr, infoPtr->nFocusedItem, nItem, -1);
7911 DPA_DeletePtr(infoPtr->hdpaItems, nItem);
7912 infoPtr->nItemCount--;
7913 fail:
7914 DPA_DeletePtr(hdpaSubItems, 0);
7915 DPA_Destroy (hdpaSubItems);
7916 Free (lpItem);
7917 return -1;
7920 /***
7921 * DESCRIPTION:
7922 * Checks item visibility.
7924 * PARAMETER(S):
7925 * [I] infoPtr : valid pointer to the listview structure
7926 * [I] nFirst : item index to check for
7928 * RETURN:
7929 * Item visible : TRUE
7930 * Item invisible or failure : FALSE
7932 static BOOL LISTVIEW_IsItemVisible(const LISTVIEW_INFO *infoPtr, INT nItem)
7934 POINT Origin, Position;
7935 RECT rcItem;
7936 HDC hdc;
7937 BOOL ret;
7939 TRACE("nItem=%d\n", nItem);
7941 if (nItem < 0 || nItem >= DPA_GetPtrCount(infoPtr->hdpaItems)) return FALSE;
7943 LISTVIEW_GetOrigin(infoPtr, &Origin);
7944 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
7945 rcItem.left = Position.x + Origin.x;
7946 rcItem.top = Position.y + Origin.y;
7947 rcItem.right = rcItem.left + infoPtr->nItemWidth;
7948 rcItem.bottom = rcItem.top + infoPtr->nItemHeight;
7950 hdc = GetDC(infoPtr->hwndSelf);
7951 if (!hdc) return FALSE;
7952 ret = RectVisible(hdc, &rcItem);
7953 ReleaseDC(infoPtr->hwndSelf, hdc);
7955 return ret;
7958 /***
7959 * DESCRIPTION:
7960 * Redraws a range of items.
7962 * PARAMETER(S):
7963 * [I] infoPtr : valid pointer to the listview structure
7964 * [I] nFirst : first item
7965 * [I] nLast : last item
7967 * RETURN:
7968 * SUCCESS : TRUE
7969 * FAILURE : FALSE
7971 static BOOL LISTVIEW_RedrawItems(const LISTVIEW_INFO *infoPtr, INT nFirst, INT nLast)
7973 INT i;
7975 if (nLast < nFirst || min(nFirst, nLast) < 0 ||
7976 max(nFirst, nLast) >= infoPtr->nItemCount)
7977 return FALSE;
7979 for (i = nFirst; i <= nLast; i++)
7980 LISTVIEW_InvalidateItem(infoPtr, i);
7982 return TRUE;
7985 /***
7986 * DESCRIPTION:
7987 * Scroll the content of a listview.
7989 * PARAMETER(S):
7990 * [I] infoPtr : valid pointer to the listview structure
7991 * [I] dx : horizontal scroll amount in pixels
7992 * [I] dy : vertical scroll amount in pixels
7994 * RETURN:
7995 * SUCCESS : TRUE
7996 * FAILURE : FALSE
7998 * COMMENTS:
7999 * If the control is in report view (LV_VIEW_DETAILS) the control can
8000 * be scrolled only in line increments. "dy" will be rounded to the
8001 * nearest number of pixels that are a whole line. Ex: if line height
8002 * is 16 and an 8 is passed, the list will be scrolled by 16. If a 7
8003 * is passed, then the scroll will be 0. (per MSDN 7/2002)
8005 static BOOL LISTVIEW_Scroll(LISTVIEW_INFO *infoPtr, INT dx, INT dy)
8007 switch(infoPtr->uView) {
8008 case LV_VIEW_DETAILS:
8009 dy += (dy < 0 ? -1 : 1) * infoPtr->nItemHeight/2;
8010 dy /= infoPtr->nItemHeight;
8011 break;
8012 case LV_VIEW_LIST:
8013 if (dy != 0) return FALSE;
8014 break;
8015 default: /* icon */
8016 break;
8019 if (dx != 0) LISTVIEW_HScroll(infoPtr, SB_INTERNAL, dx);
8020 if (dy != 0) LISTVIEW_VScroll(infoPtr, SB_INTERNAL, dy);
8022 return TRUE;
8025 /***
8026 * DESCRIPTION:
8027 * Sets the background color.
8029 * PARAMETER(S):
8030 * [I] infoPtr : valid pointer to the listview structure
8031 * [I] color : background color
8033 * RETURN:
8034 * SUCCESS : TRUE
8035 * FAILURE : FALSE
8037 static BOOL LISTVIEW_SetBkColor(LISTVIEW_INFO *infoPtr, COLORREF color)
8039 TRACE("(color=%x)\n", color);
8041 if(infoPtr->clrBk != color) {
8042 if (infoPtr->clrBk != CLR_NONE) DeleteObject(infoPtr->hBkBrush);
8043 infoPtr->clrBk = color;
8044 if (color == CLR_NONE)
8045 infoPtr->hBkBrush = (HBRUSH)GetClassLongPtrW(infoPtr->hwndSelf, GCLP_HBRBACKGROUND);
8046 else
8048 infoPtr->hBkBrush = CreateSolidBrush(color);
8049 infoPtr->dwLvExStyle &= ~LVS_EX_TRANSPARENTBKGND;
8053 return TRUE;
8056 /* LISTVIEW_SetBkImage */
8058 /*** Helper for {Insert,Set}ColumnT *only* */
8059 static void column_fill_hditem(const LISTVIEW_INFO *infoPtr, HDITEMW *lphdi, INT nColumn,
8060 const LVCOLUMNW *lpColumn, BOOL isW)
8062 if (lpColumn->mask & LVCF_FMT)
8064 /* format member is valid */
8065 lphdi->mask |= HDI_FORMAT;
8067 /* set text alignment (leftmost column must be left-aligned) */
8068 if (nColumn == 0 || (lpColumn->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_LEFT)
8069 lphdi->fmt |= HDF_LEFT;
8070 else if ((lpColumn->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_RIGHT)
8071 lphdi->fmt |= HDF_RIGHT;
8072 else if ((lpColumn->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_CENTER)
8073 lphdi->fmt |= HDF_CENTER;
8075 if (lpColumn->fmt & LVCFMT_BITMAP_ON_RIGHT)
8076 lphdi->fmt |= HDF_BITMAP_ON_RIGHT;
8078 if (lpColumn->fmt & LVCFMT_COL_HAS_IMAGES)
8080 lphdi->fmt |= HDF_IMAGE;
8081 lphdi->iImage = I_IMAGECALLBACK;
8084 if (lpColumn->fmt & LVCFMT_FIXED_WIDTH)
8085 lphdi->fmt |= HDF_FIXEDWIDTH;
8088 if (lpColumn->mask & LVCF_WIDTH)
8090 lphdi->mask |= HDI_WIDTH;
8091 if(lpColumn->cx == LVSCW_AUTOSIZE_USEHEADER)
8093 /* make it fill the remainder of the controls width */
8094 RECT rcHeader;
8095 INT item_index;
8097 for(item_index = 0; item_index < (nColumn - 1); item_index++)
8099 LISTVIEW_GetHeaderRect(infoPtr, item_index, &rcHeader);
8100 lphdi->cxy += rcHeader.right - rcHeader.left;
8103 /* retrieve the layout of the header */
8104 GetClientRect(infoPtr->hwndSelf, &rcHeader);
8105 TRACE("start cxy=%d rcHeader=%s\n", lphdi->cxy, wine_dbgstr_rect(&rcHeader));
8107 lphdi->cxy = (rcHeader.right - rcHeader.left) - lphdi->cxy;
8109 else
8110 lphdi->cxy = lpColumn->cx;
8113 if (lpColumn->mask & LVCF_TEXT)
8115 lphdi->mask |= HDI_TEXT | HDI_FORMAT;
8116 lphdi->fmt |= HDF_STRING;
8117 lphdi->pszText = lpColumn->pszText;
8118 lphdi->cchTextMax = textlenT(lpColumn->pszText, isW);
8121 if (lpColumn->mask & LVCF_IMAGE)
8123 lphdi->mask |= HDI_IMAGE;
8124 lphdi->iImage = lpColumn->iImage;
8127 if (lpColumn->mask & LVCF_ORDER)
8129 lphdi->mask |= HDI_ORDER;
8130 lphdi->iOrder = lpColumn->iOrder;
8135 /***
8136 * DESCRIPTION:
8137 * Inserts a new column.
8139 * PARAMETER(S):
8140 * [I] infoPtr : valid pointer to the listview structure
8141 * [I] nColumn : column index
8142 * [I] lpColumn : column information
8143 * [I] isW : TRUE if lpColumn is Unicode, FALSE otherwise
8145 * RETURN:
8146 * SUCCESS : new column index
8147 * FAILURE : -1
8149 static INT LISTVIEW_InsertColumnT(LISTVIEW_INFO *infoPtr, INT nColumn,
8150 const LVCOLUMNW *lpColumn, BOOL isW)
8152 COLUMN_INFO *lpColumnInfo;
8153 INT nNewColumn;
8154 HDITEMW hdi;
8156 TRACE("(nColumn=%d, lpColumn=%s, isW=%d)\n", nColumn, debuglvcolumn_t(lpColumn, isW), isW);
8158 if (!lpColumn || nColumn < 0) return -1;
8159 nColumn = min(nColumn, DPA_GetPtrCount(infoPtr->hdpaColumns));
8161 ZeroMemory(&hdi, sizeof(HDITEMW));
8162 column_fill_hditem(infoPtr, &hdi, nColumn, lpColumn, isW);
8165 * A mask not including LVCF_WIDTH turns into a mask of width, width 10
8166 * (can be seen in SPY) otherwise column never gets added.
8168 if (!(lpColumn->mask & LVCF_WIDTH)) {
8169 hdi.mask |= HDI_WIDTH;
8170 hdi.cxy = 10;
8174 * when the iSubItem is available Windows copies it to the header lParam. It seems
8175 * to happen only in LVM_INSERTCOLUMN - not in LVM_SETCOLUMN
8177 if (lpColumn->mask & LVCF_SUBITEM)
8179 hdi.mask |= HDI_LPARAM;
8180 hdi.lParam = lpColumn->iSubItem;
8183 /* create header if not present */
8184 LISTVIEW_CreateHeader(infoPtr);
8185 if (!(LVS_NOCOLUMNHEADER & infoPtr->dwStyle) &&
8186 (infoPtr->uView == LV_VIEW_DETAILS) && (WS_VISIBLE & infoPtr->dwStyle))
8188 ShowWindow(infoPtr->hwndHeader, SW_SHOWNORMAL);
8191 /* insert item in header control */
8192 nNewColumn = SendMessageW(infoPtr->hwndHeader,
8193 isW ? HDM_INSERTITEMW : HDM_INSERTITEMA,
8194 nColumn, (LPARAM)&hdi);
8195 if (nNewColumn == -1) return -1;
8196 if (nNewColumn != nColumn) ERR("nColumn=%d, nNewColumn=%d\n", nColumn, nNewColumn);
8198 /* create our own column info */
8199 if (!(lpColumnInfo = Alloc(sizeof(COLUMN_INFO)))) goto fail;
8200 if (DPA_InsertPtr(infoPtr->hdpaColumns, nNewColumn, lpColumnInfo) == -1) goto fail;
8202 if (lpColumn->mask & LVCF_FMT) lpColumnInfo->fmt = lpColumn->fmt;
8203 if (lpColumn->mask & LVCF_MINWIDTH) lpColumnInfo->cxMin = lpColumn->cxMin;
8204 if (!SendMessageW(infoPtr->hwndHeader, HDM_GETITEMRECT, nNewColumn, (LPARAM)&lpColumnInfo->rcHeader))
8205 goto fail;
8207 /* now we have to actually adjust the data */
8208 if (!(infoPtr->dwStyle & LVS_OWNERDATA) && infoPtr->nItemCount > 0)
8210 SUBITEM_INFO *lpSubItem;
8211 HDPA hdpaSubItems;
8212 INT nItem, i;
8213 LVITEMW item;
8214 BOOL changed;
8216 item.iSubItem = nNewColumn;
8217 item.mask = LVIF_TEXT | LVIF_IMAGE;
8218 item.iImage = I_IMAGECALLBACK;
8219 item.pszText = LPSTR_TEXTCALLBACKW;
8221 for (nItem = 0; nItem < infoPtr->nItemCount; nItem++)
8223 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, nItem);
8224 for (i = 1; i < DPA_GetPtrCount(hdpaSubItems); i++)
8226 lpSubItem = DPA_GetPtr(hdpaSubItems, i);
8227 if (lpSubItem->iSubItem >= nNewColumn)
8228 lpSubItem->iSubItem++;
8231 /* add new subitem for each item */
8232 item.iItem = nItem;
8233 set_sub_item(infoPtr, &item, isW, &changed);
8237 /* make space for the new column */
8238 LISTVIEW_ScrollColumns(infoPtr, nNewColumn + 1, lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left);
8239 LISTVIEW_UpdateItemSize(infoPtr);
8241 return nNewColumn;
8243 fail:
8244 if (nNewColumn != -1) SendMessageW(infoPtr->hwndHeader, HDM_DELETEITEM, nNewColumn, 0);
8245 if (lpColumnInfo)
8247 DPA_DeletePtr(infoPtr->hdpaColumns, nNewColumn);
8248 Free(lpColumnInfo);
8250 return -1;
8253 /***
8254 * DESCRIPTION:
8255 * Sets the attributes of a header item.
8257 * PARAMETER(S):
8258 * [I] infoPtr : valid pointer to the listview structure
8259 * [I] nColumn : column index
8260 * [I] lpColumn : column attributes
8261 * [I] isW: if TRUE, then lpColumn is a LPLVCOLUMNW, else it is a LPLVCOLUMNA
8263 * RETURN:
8264 * SUCCESS : TRUE
8265 * FAILURE : FALSE
8267 static BOOL LISTVIEW_SetColumnT(const LISTVIEW_INFO *infoPtr, INT nColumn,
8268 const LVCOLUMNW *lpColumn, BOOL isW)
8270 HDITEMW hdi, hdiget;
8271 BOOL bResult;
8273 TRACE("(nColumn=%d, lpColumn=%s, isW=%d)\n", nColumn, debuglvcolumn_t(lpColumn, isW), isW);
8275 if (!lpColumn || nColumn < 0 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
8277 ZeroMemory(&hdi, sizeof(HDITEMW));
8278 if (lpColumn->mask & LVCF_FMT)
8280 hdi.mask |= HDI_FORMAT;
8281 hdiget.mask = HDI_FORMAT;
8282 if (SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nColumn, (LPARAM)&hdiget))
8283 hdi.fmt = hdiget.fmt & HDF_STRING;
8285 column_fill_hditem(infoPtr, &hdi, nColumn, lpColumn, isW);
8287 /* set header item attributes */
8288 bResult = SendMessageW(infoPtr->hwndHeader, isW ? HDM_SETITEMW : HDM_SETITEMA, nColumn, (LPARAM)&hdi);
8289 if (!bResult) return FALSE;
8291 if (lpColumn->mask & LVCF_FMT)
8293 COLUMN_INFO *lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, nColumn);
8294 INT oldFmt = lpColumnInfo->fmt;
8296 lpColumnInfo->fmt = lpColumn->fmt;
8297 if ((oldFmt ^ lpColumn->fmt) & (LVCFMT_JUSTIFYMASK | LVCFMT_IMAGE))
8299 if (infoPtr->uView == LV_VIEW_DETAILS) LISTVIEW_InvalidateColumn(infoPtr, nColumn);
8303 if (lpColumn->mask & LVCF_MINWIDTH)
8304 LISTVIEW_GetColumnInfo(infoPtr, nColumn)->cxMin = lpColumn->cxMin;
8306 return TRUE;
8309 /***
8310 * DESCRIPTION:
8311 * Sets the column order array
8313 * PARAMETERS:
8314 * [I] infoPtr : valid pointer to the listview structure
8315 * [I] iCount : number of elements in column order array
8316 * [I] lpiArray : pointer to column order array
8318 * RETURN:
8319 * SUCCESS : TRUE
8320 * FAILURE : FALSE
8322 static BOOL LISTVIEW_SetColumnOrderArray(LISTVIEW_INFO *infoPtr, INT iCount, const INT *lpiArray)
8324 if (!infoPtr->hwndHeader) return FALSE;
8325 infoPtr->colRectsDirty = TRUE;
8326 return SendMessageW(infoPtr->hwndHeader, HDM_SETORDERARRAY, iCount, (LPARAM)lpiArray);
8329 /***
8330 * DESCRIPTION:
8331 * Sets the width of a column
8333 * PARAMETERS:
8334 * [I] infoPtr : valid pointer to the listview structure
8335 * [I] nColumn : column index
8336 * [I] cx : column width
8338 * RETURN:
8339 * SUCCESS : TRUE
8340 * FAILURE : FALSE
8342 static BOOL LISTVIEW_SetColumnWidth(LISTVIEW_INFO *infoPtr, INT nColumn, INT cx)
8344 WCHAR szDispText[DISP_TEXT_SIZE] = { 0 };
8345 INT max_cx = 0;
8346 HDITEMW hdi;
8348 TRACE("(nColumn=%d, cx=%d)\n", nColumn, cx);
8350 /* set column width only if in report or list mode */
8351 if (infoPtr->uView != LV_VIEW_DETAILS && infoPtr->uView != LV_VIEW_LIST) return FALSE;
8353 /* take care of invalid cx values - LVSCW_AUTOSIZE_* values are negative,
8354 with _USEHEADER being the lowest */
8355 if (infoPtr->uView == LV_VIEW_DETAILS && cx < LVSCW_AUTOSIZE_USEHEADER) cx = LVSCW_AUTOSIZE;
8356 else if (infoPtr->uView == LV_VIEW_LIST && cx <= 0) return FALSE;
8358 /* resize all columns if in LV_VIEW_LIST mode */
8359 if(infoPtr->uView == LV_VIEW_LIST)
8361 infoPtr->nItemWidth = cx;
8362 LISTVIEW_InvalidateList(infoPtr);
8363 return TRUE;
8366 if (nColumn < 0 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
8368 if (cx == LVSCW_AUTOSIZE || (cx == LVSCW_AUTOSIZE_USEHEADER && nColumn < DPA_GetPtrCount(infoPtr->hdpaColumns) -1))
8370 INT nLabelWidth;
8371 LVITEMW lvItem;
8373 lvItem.mask = LVIF_TEXT;
8374 lvItem.iItem = 0;
8375 lvItem.iSubItem = nColumn;
8376 lvItem.cchTextMax = DISP_TEXT_SIZE;
8377 for (; lvItem.iItem < infoPtr->nItemCount; lvItem.iItem++)
8379 lvItem.pszText = szDispText;
8380 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) continue;
8381 nLabelWidth = LISTVIEW_GetStringWidthT(infoPtr, lvItem.pszText, TRUE);
8382 if (max_cx < nLabelWidth) max_cx = nLabelWidth;
8384 if (infoPtr->himlSmall && (nColumn == 0 || (LISTVIEW_GetColumnInfo(infoPtr, nColumn)->fmt & LVCFMT_IMAGE)))
8385 max_cx += infoPtr->iconSize.cx;
8386 max_cx += TRAILING_LABEL_PADDING;
8389 /* autosize based on listview items width */
8390 if(cx == LVSCW_AUTOSIZE)
8391 cx = max_cx;
8392 else if(cx == LVSCW_AUTOSIZE_USEHEADER)
8394 /* if iCol is the last column make it fill the remainder of the controls width */
8395 if(nColumn == DPA_GetPtrCount(infoPtr->hdpaColumns) - 1)
8397 RECT rcHeader;
8398 POINT Origin;
8400 LISTVIEW_GetOrigin(infoPtr, &Origin);
8401 LISTVIEW_GetHeaderRect(infoPtr, nColumn, &rcHeader);
8403 cx = infoPtr->rcList.right - Origin.x - rcHeader.left;
8405 else
8407 /* Despite what the MS docs say, if this is not the last
8408 column, then MS resizes the column to the width of the
8409 largest text string in the column, including headers
8410 and items. This is different from LVSCW_AUTOSIZE in that
8411 LVSCW_AUTOSIZE ignores the header string length. */
8412 cx = 0;
8414 /* retrieve header text */
8415 hdi.mask = HDI_TEXT|HDI_FORMAT|HDI_IMAGE|HDI_BITMAP;
8416 hdi.cchTextMax = DISP_TEXT_SIZE;
8417 hdi.pszText = szDispText;
8418 if (SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nColumn, (LPARAM)&hdi))
8420 HDC hdc = GetDC(infoPtr->hwndSelf);
8421 HFONT old_font = SelectObject(hdc, (HFONT)SendMessageW(infoPtr->hwndHeader, WM_GETFONT, 0, 0));
8422 HIMAGELIST himl = (HIMAGELIST)SendMessageW(infoPtr->hwndHeader, HDM_GETIMAGELIST, 0, 0);
8423 INT bitmap_margin = 0;
8424 SIZE size;
8426 if (GetTextExtentPoint32W(hdc, hdi.pszText, lstrlenW(hdi.pszText), &size))
8427 cx = size.cx + TRAILING_HEADER_PADDING;
8429 if (hdi.fmt & (HDF_IMAGE|HDF_BITMAP))
8430 bitmap_margin = SendMessageW(infoPtr->hwndHeader, HDM_GETBITMAPMARGIN, 0, 0);
8432 if ((hdi.fmt & HDF_IMAGE) && himl)
8434 INT icon_cx, icon_cy;
8436 if (!ImageList_GetIconSize(himl, &icon_cx, &icon_cy))
8437 cx += icon_cx + 2*bitmap_margin;
8439 else if (hdi.fmt & HDF_BITMAP)
8441 BITMAP bmp;
8443 GetObjectW(hdi.hbm, sizeof(BITMAP), &bmp);
8444 cx += bmp.bmWidth + 2*bitmap_margin;
8447 SelectObject(hdc, old_font);
8448 ReleaseDC(infoPtr->hwndSelf, hdc);
8450 cx = max (cx, max_cx);
8454 if (cx < 0) return FALSE;
8456 /* call header to update the column change */
8457 hdi.mask = HDI_WIDTH;
8458 hdi.cxy = max(cx, LISTVIEW_GetColumnInfo(infoPtr, nColumn)->cxMin);
8459 TRACE("hdi.cxy=%d\n", hdi.cxy);
8460 return SendMessageW(infoPtr->hwndHeader, HDM_SETITEMW, nColumn, (LPARAM)&hdi);
8463 /***
8464 * Creates the checkbox imagelist. Helper for LISTVIEW_SetExtendedListViewStyle
8467 static HIMAGELIST LISTVIEW_CreateCheckBoxIL(const LISTVIEW_INFO *infoPtr)
8469 HDC hdc_wnd, hdc;
8470 HBITMAP hbm_im, hbm_mask, hbm_orig;
8471 RECT rc;
8472 HBRUSH hbr_white = GetStockObject(WHITE_BRUSH);
8473 HBRUSH hbr_black = GetStockObject(BLACK_BRUSH);
8474 HIMAGELIST himl;
8476 himl = ImageList_Create(GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON),
8477 ILC_COLOR | ILC_MASK, 2, 2);
8478 hdc_wnd = GetDC(infoPtr->hwndSelf);
8479 hdc = CreateCompatibleDC(hdc_wnd);
8480 hbm_im = CreateCompatibleBitmap(hdc_wnd, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON));
8481 hbm_mask = CreateBitmap(GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), 1, 1, NULL);
8482 ReleaseDC(infoPtr->hwndSelf, hdc_wnd);
8484 SetRect(&rc, 0, 0, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON));
8485 hbm_orig = SelectObject(hdc, hbm_mask);
8486 FillRect(hdc, &rc, hbr_white);
8487 InflateRect(&rc, -2, -2);
8488 FillRect(hdc, &rc, hbr_black);
8490 SelectObject(hdc, hbm_im);
8491 DrawFrameControl(hdc, &rc, DFC_BUTTON, DFCS_BUTTONCHECK | DFCS_MONO);
8492 SelectObject(hdc, hbm_orig);
8493 ImageList_Add(himl, hbm_im, hbm_mask);
8495 SelectObject(hdc, hbm_im);
8496 DrawFrameControl(hdc, &rc, DFC_BUTTON, DFCS_BUTTONCHECK | DFCS_MONO | DFCS_CHECKED);
8497 SelectObject(hdc, hbm_orig);
8498 ImageList_Add(himl, hbm_im, hbm_mask);
8500 DeleteObject(hbm_mask);
8501 DeleteObject(hbm_im);
8502 DeleteDC(hdc);
8504 return himl;
8507 /***
8508 * DESCRIPTION:
8509 * Sets the extended listview style.
8511 * PARAMETERS:
8512 * [I] infoPtr : valid pointer to the listview structure
8513 * [I] dwMask : mask
8514 * [I] dwStyle : style
8516 * RETURN:
8517 * SUCCESS : previous style
8518 * FAILURE : 0
8520 static DWORD LISTVIEW_SetExtendedListViewStyle(LISTVIEW_INFO *infoPtr, DWORD mask, DWORD ex_style)
8522 DWORD old_ex_style = infoPtr->dwLvExStyle;
8524 TRACE("mask=0x%08x, ex_style=0x%08x\n", mask, ex_style);
8526 /* set new style */
8527 if (mask)
8528 infoPtr->dwLvExStyle = (old_ex_style & ~mask) | (ex_style & mask);
8529 else
8530 infoPtr->dwLvExStyle = ex_style;
8532 if((infoPtr->dwLvExStyle ^ old_ex_style) & LVS_EX_CHECKBOXES)
8534 HIMAGELIST himl = 0;
8535 if(infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES)
8537 LVITEMW item;
8538 item.mask = LVIF_STATE;
8539 item.stateMask = LVIS_STATEIMAGEMASK;
8540 item.state = INDEXTOSTATEIMAGEMASK(1);
8541 LISTVIEW_SetItemState(infoPtr, -1, &item);
8543 himl = LISTVIEW_CreateCheckBoxIL(infoPtr);
8544 if(!(infoPtr->dwStyle & LVS_SHAREIMAGELISTS))
8545 ImageList_Destroy(infoPtr->himlState);
8547 himl = LISTVIEW_SetImageList(infoPtr, LVSIL_STATE, himl);
8548 /* checkbox list replaces previous custom list or... */
8549 if(((infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES) &&
8550 !(infoPtr->dwStyle & LVS_SHAREIMAGELISTS)) ||
8551 /* ...previous was checkbox list */
8552 (old_ex_style & LVS_EX_CHECKBOXES))
8553 ImageList_Destroy(himl);
8556 if((infoPtr->dwLvExStyle ^ old_ex_style) & LVS_EX_HEADERDRAGDROP)
8558 DWORD style;
8560 /* if not already created */
8561 LISTVIEW_CreateHeader(infoPtr);
8563 style = GetWindowLongW(infoPtr->hwndHeader, GWL_STYLE);
8564 if (infoPtr->dwLvExStyle & LVS_EX_HEADERDRAGDROP)
8565 style |= HDS_DRAGDROP;
8566 else
8567 style &= ~HDS_DRAGDROP;
8568 SetWindowLongW(infoPtr->hwndHeader, GWL_STYLE, style);
8571 /* GRIDLINES adds decoration at top so changes sizes */
8572 if((infoPtr->dwLvExStyle ^ old_ex_style) & LVS_EX_GRIDLINES)
8574 LISTVIEW_CreateHeader(infoPtr);
8575 LISTVIEW_UpdateSize(infoPtr);
8578 if((infoPtr->dwLvExStyle ^ old_ex_style) & LVS_EX_FULLROWSELECT)
8580 LISTVIEW_CreateHeader(infoPtr);
8583 if((infoPtr->dwLvExStyle ^ old_ex_style) & LVS_EX_TRANSPARENTBKGND)
8585 if (infoPtr->dwLvExStyle & LVS_EX_TRANSPARENTBKGND)
8586 LISTVIEW_SetBkColor(infoPtr, CLR_NONE);
8589 if((infoPtr->dwLvExStyle ^ old_ex_style) & LVS_EX_HEADERINALLVIEWS)
8591 if (infoPtr->dwLvExStyle & LVS_EX_HEADERINALLVIEWS)
8592 LISTVIEW_CreateHeader(infoPtr);
8593 else
8594 ShowWindow(infoPtr->hwndHeader, SW_HIDE);
8595 LISTVIEW_UpdateSize(infoPtr);
8596 LISTVIEW_UpdateScroll(infoPtr);
8599 LISTVIEW_InvalidateList(infoPtr);
8600 return old_ex_style;
8603 /***
8604 * DESCRIPTION:
8605 * Sets the new hot cursor used during hot tracking and hover selection.
8607 * PARAMETER(S):
8608 * [I] infoPtr : valid pointer to the listview structure
8609 * [I] hCursor : the new hot cursor handle
8611 * RETURN:
8612 * Returns the previous hot cursor
8614 static HCURSOR LISTVIEW_SetHotCursor(LISTVIEW_INFO *infoPtr, HCURSOR hCursor)
8616 HCURSOR oldCursor = infoPtr->hHotCursor;
8618 infoPtr->hHotCursor = hCursor;
8620 return oldCursor;
8624 /***
8625 * DESCRIPTION:
8626 * Sets the hot item index.
8628 * PARAMETERS:
8629 * [I] infoPtr : valid pointer to the listview structure
8630 * [I] iIndex : index
8632 * RETURN:
8633 * SUCCESS : previous hot item index
8634 * FAILURE : -1 (no hot item)
8636 static INT LISTVIEW_SetHotItem(LISTVIEW_INFO *infoPtr, INT iIndex)
8638 INT iOldIndex = infoPtr->nHotItem;
8640 infoPtr->nHotItem = iIndex;
8642 return iOldIndex;
8646 /***
8647 * DESCRIPTION:
8648 * Sets the amount of time the cursor must hover over an item before it is selected.
8650 * PARAMETER(S):
8651 * [I] infoPtr : valid pointer to the listview structure
8652 * [I] dwHoverTime : hover time, if -1 the hover time is set to the default
8654 * RETURN:
8655 * Returns the previous hover time
8657 static DWORD LISTVIEW_SetHoverTime(LISTVIEW_INFO *infoPtr, DWORD dwHoverTime)
8659 DWORD oldHoverTime = infoPtr->dwHoverTime;
8661 infoPtr->dwHoverTime = dwHoverTime;
8663 return oldHoverTime;
8666 /***
8667 * DESCRIPTION:
8668 * Sets spacing for icons of LVS_ICON style.
8670 * PARAMETER(S):
8671 * [I] infoPtr : valid pointer to the listview structure
8672 * [I] cx : horizontal spacing (-1 = system spacing, 0 = autosize)
8673 * [I] cy : vertical spacing (-1 = system spacing, 0 = autosize)
8675 * RETURN:
8676 * MAKELONG(oldcx, oldcy)
8678 static DWORD LISTVIEW_SetIconSpacing(LISTVIEW_INFO *infoPtr, INT cx, INT cy)
8680 INT iconWidth = 0, iconHeight = 0;
8681 DWORD oldspacing = MAKELONG(infoPtr->iconSpacing.cx, infoPtr->iconSpacing.cy);
8683 TRACE("requested=(%d,%d)\n", cx, cy);
8685 /* set to defaults, if instructed to */
8686 if (cx == -1 && cy == -1)
8688 infoPtr->autoSpacing = TRUE;
8689 if (infoPtr->himlNormal)
8690 ImageList_GetIconSize(infoPtr->himlNormal, &iconWidth, &iconHeight);
8691 cx = GetSystemMetrics(SM_CXICONSPACING) - GetSystemMetrics(SM_CXICON) + iconWidth;
8692 cy = GetSystemMetrics(SM_CYICONSPACING) - GetSystemMetrics(SM_CYICON) + iconHeight;
8694 else
8695 infoPtr->autoSpacing = FALSE;
8697 /* if 0 then keep width */
8698 if (cx != 0)
8699 infoPtr->iconSpacing.cx = cx;
8701 /* if 0 then keep height */
8702 if (cy != 0)
8703 infoPtr->iconSpacing.cy = cy;
8705 TRACE("old=(%d,%d), new=(%d,%d), iconSize=(%d,%d), ntmH=%d\n",
8706 LOWORD(oldspacing), HIWORD(oldspacing), infoPtr->iconSpacing.cx, infoPtr->iconSpacing.cy,
8707 infoPtr->iconSize.cx, infoPtr->iconSize.cy,
8708 infoPtr->ntmHeight);
8710 /* these depend on the iconSpacing */
8711 LISTVIEW_UpdateItemSize(infoPtr);
8713 return oldspacing;
8716 static inline void set_icon_size(SIZE *size, HIMAGELIST himl, BOOL small)
8718 INT cx, cy;
8720 if (himl && ImageList_GetIconSize(himl, &cx, &cy))
8722 size->cx = cx;
8723 size->cy = cy;
8725 else
8727 size->cx = GetSystemMetrics(small ? SM_CXSMICON : SM_CXICON);
8728 size->cy = GetSystemMetrics(small ? SM_CYSMICON : SM_CYICON);
8732 /***
8733 * DESCRIPTION:
8734 * Sets image lists.
8736 * PARAMETER(S):
8737 * [I] infoPtr : valid pointer to the listview structure
8738 * [I] nType : image list type
8739 * [I] himl : image list handle
8741 * RETURN:
8742 * SUCCESS : old image list
8743 * FAILURE : NULL
8745 static HIMAGELIST LISTVIEW_SetImageList(LISTVIEW_INFO *infoPtr, INT nType, HIMAGELIST himl)
8747 INT oldHeight = infoPtr->nItemHeight;
8748 HIMAGELIST himlOld = 0;
8750 TRACE("(nType=%d, himl=%p)\n", nType, himl);
8752 switch (nType)
8754 case LVSIL_NORMAL:
8755 himlOld = infoPtr->himlNormal;
8756 infoPtr->himlNormal = himl;
8757 if (infoPtr->uView == LV_VIEW_ICON) set_icon_size(&infoPtr->iconSize, himl, FALSE);
8758 if (infoPtr->autoSpacing)
8759 LISTVIEW_SetIconSpacing(infoPtr, -1, -1);
8760 break;
8762 case LVSIL_SMALL:
8763 himlOld = infoPtr->himlSmall;
8764 infoPtr->himlSmall = himl;
8765 if (infoPtr->uView != LV_VIEW_ICON) set_icon_size(&infoPtr->iconSize, himl, TRUE);
8766 if (infoPtr->hwndHeader)
8767 SendMessageW(infoPtr->hwndHeader, HDM_SETIMAGELIST, 0, (LPARAM)himl);
8768 break;
8770 case LVSIL_STATE:
8771 himlOld = infoPtr->himlState;
8772 infoPtr->himlState = himl;
8773 set_icon_size(&infoPtr->iconStateSize, himl, TRUE);
8774 ImageList_SetBkColor(infoPtr->himlState, CLR_NONE);
8775 break;
8777 default:
8778 ERR("Unknown icon type=%d\n", nType);
8779 return NULL;
8782 infoPtr->nItemHeight = LISTVIEW_CalculateItemHeight(infoPtr);
8783 if (infoPtr->nItemHeight != oldHeight)
8784 LISTVIEW_UpdateScroll(infoPtr);
8786 return himlOld;
8789 /***
8790 * DESCRIPTION:
8791 * Preallocates memory (does *not* set the actual count of items !)
8793 * PARAMETER(S):
8794 * [I] infoPtr : valid pointer to the listview structure
8795 * [I] nItems : item count (projected number of items to allocate)
8796 * [I] dwFlags : update flags
8798 * RETURN:
8799 * SUCCESS : TRUE
8800 * FAILURE : FALSE
8802 static BOOL LISTVIEW_SetItemCount(LISTVIEW_INFO *infoPtr, INT nItems, DWORD dwFlags)
8804 TRACE("(nItems=%d, dwFlags=%x)\n", nItems, dwFlags);
8806 if (infoPtr->dwStyle & LVS_OWNERDATA)
8808 INT nOldCount = infoPtr->nItemCount;
8809 infoPtr->nItemCount = nItems;
8811 if (nItems < nOldCount)
8813 RANGE range = { nItems, nOldCount };
8814 ranges_del(infoPtr->selectionRanges, range);
8815 if (infoPtr->nFocusedItem >= nItems)
8817 LISTVIEW_SetItemFocus(infoPtr, -1);
8818 SetRectEmpty(&infoPtr->rcFocus);
8822 LISTVIEW_UpdateScroll(infoPtr);
8824 /* the flags are valid only in ownerdata report and list modes */
8825 if (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON) dwFlags = 0;
8827 if (!(dwFlags & LVSICF_NOSCROLL) && infoPtr->nFocusedItem != -1)
8828 LISTVIEW_EnsureVisible(infoPtr, infoPtr->nFocusedItem, FALSE);
8830 if (!(dwFlags & LVSICF_NOINVALIDATEALL))
8831 LISTVIEW_InvalidateList(infoPtr);
8832 else
8834 INT nFrom, nTo;
8835 POINT Origin;
8836 RECT rcErase;
8838 LISTVIEW_GetOrigin(infoPtr, &Origin);
8839 nFrom = min(nOldCount, nItems);
8840 nTo = max(nOldCount, nItems);
8842 if (infoPtr->uView == LV_VIEW_DETAILS)
8844 SetRect(&rcErase, 0, nFrom * infoPtr->nItemHeight, infoPtr->nItemWidth,
8845 nTo * infoPtr->nItemHeight);
8846 OffsetRect(&rcErase, Origin.x, Origin.y);
8847 if (IntersectRect(&rcErase, &rcErase, &infoPtr->rcList))
8848 LISTVIEW_InvalidateRect(infoPtr, &rcErase);
8850 else /* LV_VIEW_LIST */
8852 INT nPerCol = LISTVIEW_GetCountPerColumn(infoPtr);
8854 rcErase.left = (nFrom / nPerCol) * infoPtr->nItemWidth;
8855 rcErase.top = (nFrom % nPerCol) * infoPtr->nItemHeight;
8856 rcErase.right = rcErase.left + infoPtr->nItemWidth;
8857 rcErase.bottom = nPerCol * infoPtr->nItemHeight;
8858 OffsetRect(&rcErase, Origin.x, Origin.y);
8859 if (IntersectRect(&rcErase, &rcErase, &infoPtr->rcList))
8860 LISTVIEW_InvalidateRect(infoPtr, &rcErase);
8862 rcErase.left = (nFrom / nPerCol + 1) * infoPtr->nItemWidth;
8863 rcErase.top = 0;
8864 rcErase.right = (nTo / nPerCol + 1) * infoPtr->nItemWidth;
8865 rcErase.bottom = nPerCol * infoPtr->nItemHeight;
8866 OffsetRect(&rcErase, Origin.x, Origin.y);
8867 if (IntersectRect(&rcErase, &rcErase, &infoPtr->rcList))
8868 LISTVIEW_InvalidateRect(infoPtr, &rcErase);
8872 else
8874 /* According to MSDN for non-LVS_OWNERDATA this is just
8875 * a performance issue. The control allocates its internal
8876 * data structures for the number of items specified. It
8877 * cuts down on the number of memory allocations. Therefore
8878 * we will just issue a WARN here
8880 WARN("for non-ownerdata performance option not implemented.\n");
8883 return TRUE;
8886 /***
8887 * DESCRIPTION:
8888 * Sets the position of an item.
8890 * PARAMETER(S):
8891 * [I] infoPtr : valid pointer to the listview structure
8892 * [I] nItem : item index
8893 * [I] pt : coordinate
8895 * RETURN:
8896 * SUCCESS : TRUE
8897 * FAILURE : FALSE
8899 static BOOL LISTVIEW_SetItemPosition(LISTVIEW_INFO *infoPtr, INT nItem, const POINT *pt)
8901 POINT Origin, Pt;
8903 TRACE("(nItem=%d, pt=%s)\n", nItem, wine_dbgstr_point(pt));
8905 if (!pt || nItem < 0 || nItem >= infoPtr->nItemCount ||
8906 !(infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON)) return FALSE;
8908 Pt = *pt;
8909 LISTVIEW_GetOrigin(infoPtr, &Origin);
8911 /* This point value seems to be an undocumented feature.
8912 * The best guess is that it means either at the origin,
8913 * or at true beginning of the list. I will assume the origin. */
8914 if ((Pt.x == -1) && (Pt.y == -1))
8915 Pt = Origin;
8917 if (infoPtr->uView == LV_VIEW_ICON)
8919 Pt.x -= (infoPtr->nItemWidth - infoPtr->iconSize.cx) / 2;
8920 Pt.y -= ICON_TOP_PADDING;
8922 Pt.x -= Origin.x;
8923 Pt.y -= Origin.y;
8925 infoPtr->bAutoarrange = FALSE;
8927 return LISTVIEW_MoveIconTo(infoPtr, nItem, &Pt, FALSE);
8930 /***
8931 * DESCRIPTION:
8932 * Sets the state of one or many items.
8934 * PARAMETER(S):
8935 * [I] infoPtr : valid pointer to the listview structure
8936 * [I] nItem : item index
8937 * [I] item : item or subitem info
8939 * RETURN:
8940 * SUCCESS : TRUE
8941 * FAILURE : FALSE
8943 static BOOL LISTVIEW_SetItemState(LISTVIEW_INFO *infoPtr, INT nItem, const LVITEMW *item)
8945 BOOL ret = TRUE;
8946 LVITEMW lvItem;
8948 if (!item) return FALSE;
8950 lvItem.iItem = nItem;
8951 lvItem.iSubItem = 0;
8952 lvItem.mask = LVIF_STATE;
8953 lvItem.state = item->state;
8954 lvItem.stateMask = item->stateMask;
8955 TRACE("item=%s\n", debuglvitem_t(&lvItem, TRUE));
8957 if (nItem == -1)
8959 UINT oldstate = 0;
8960 BOOL notify;
8962 /* special case optimization for recurring attempt to deselect all */
8963 if (lvItem.state == 0 && lvItem.stateMask == LVIS_SELECTED && !LISTVIEW_GetSelectedCount(infoPtr))
8964 return TRUE;
8966 /* select all isn't allowed in LVS_SINGLESEL */
8967 if ((lvItem.state & lvItem.stateMask & LVIS_SELECTED) && (infoPtr->dwStyle & LVS_SINGLESEL))
8968 return FALSE;
8970 /* focus all isn't allowed */
8971 if (lvItem.state & lvItem.stateMask & LVIS_FOCUSED) return FALSE;
8973 notify = infoPtr->bDoChangeNotify;
8974 if (infoPtr->dwStyle & LVS_OWNERDATA)
8976 infoPtr->bDoChangeNotify = FALSE;
8977 if (!(lvItem.state & LVIS_SELECTED) && LISTVIEW_GetSelectedCount(infoPtr))
8978 oldstate |= LVIS_SELECTED;
8979 if (infoPtr->nFocusedItem != -1) oldstate |= LVIS_FOCUSED;
8982 /* apply to all items */
8983 for (lvItem.iItem = 0; lvItem.iItem < infoPtr->nItemCount; lvItem.iItem++)
8984 if (!LISTVIEW_SetItemT(infoPtr, &lvItem, TRUE)) ret = FALSE;
8986 if (infoPtr->dwStyle & LVS_OWNERDATA)
8988 NMLISTVIEW nmlv;
8990 infoPtr->bDoChangeNotify = notify;
8992 nmlv.iItem = -1;
8993 nmlv.iSubItem = 0;
8994 nmlv.uNewState = lvItem.state & lvItem.stateMask;
8995 nmlv.uOldState = oldstate & lvItem.stateMask;
8996 nmlv.uChanged = LVIF_STATE;
8997 nmlv.ptAction.x = nmlv.ptAction.y = 0;
8998 nmlv.lParam = 0;
9000 notify_listview(infoPtr, LVN_ITEMCHANGED, &nmlv);
9003 else
9004 ret = LISTVIEW_SetItemT(infoPtr, &lvItem, TRUE);
9006 return ret;
9009 /***
9010 * DESCRIPTION:
9011 * Sets the text of an item or subitem.
9013 * PARAMETER(S):
9014 * [I] hwnd : window handle
9015 * [I] nItem : item index
9016 * [I] lpLVItem : item or subitem info
9017 * [I] isW : TRUE if input is Unicode
9019 * RETURN:
9020 * SUCCESS : TRUE
9021 * FAILURE : FALSE
9023 static BOOL LISTVIEW_SetItemTextT(LISTVIEW_INFO *infoPtr, INT nItem, const LVITEMW *lpLVItem, BOOL isW)
9025 LVITEMW lvItem;
9027 if (!lpLVItem || nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
9028 if (infoPtr->dwStyle & LVS_OWNERDATA) return FALSE;
9030 lvItem.iItem = nItem;
9031 lvItem.iSubItem = lpLVItem->iSubItem;
9032 lvItem.mask = LVIF_TEXT;
9033 lvItem.pszText = lpLVItem->pszText;
9034 lvItem.cchTextMax = lpLVItem->cchTextMax;
9036 TRACE("(nItem=%d, lpLVItem=%s, isW=%d)\n", nItem, debuglvitem_t(&lvItem, isW), isW);
9038 return LISTVIEW_SetItemT(infoPtr, &lvItem, isW);
9041 /***
9042 * DESCRIPTION:
9043 * Set item index that marks the start of a multiple selection.
9045 * PARAMETER(S):
9046 * [I] infoPtr : valid pointer to the listview structure
9047 * [I] nIndex : index
9049 * RETURN:
9050 * Index number or -1 if there is no selection mark.
9052 static INT LISTVIEW_SetSelectionMark(LISTVIEW_INFO *infoPtr, INT nIndex)
9054 INT nOldIndex = infoPtr->nSelectionMark;
9056 TRACE("(nIndex=%d)\n", nIndex);
9058 infoPtr->nSelectionMark = nIndex;
9060 return nOldIndex;
9063 /***
9064 * DESCRIPTION:
9065 * Sets the text background color.
9067 * PARAMETER(S):
9068 * [I] infoPtr : valid pointer to the listview structure
9069 * [I] color : text background color
9071 * RETURN:
9072 * SUCCESS : TRUE
9073 * FAILURE : FALSE
9075 static BOOL LISTVIEW_SetTextBkColor(LISTVIEW_INFO *infoPtr, COLORREF color)
9077 TRACE("(color=%x)\n", color);
9079 infoPtr->clrTextBk = color;
9080 return TRUE;
9083 /***
9084 * DESCRIPTION:
9085 * Sets the text foreground color.
9087 * PARAMETER(S):
9088 * [I] infoPtr : valid pointer to the listview structure
9089 * [I] color : text color
9091 * RETURN:
9092 * SUCCESS : TRUE
9093 * FAILURE : FALSE
9095 static BOOL LISTVIEW_SetTextColor (LISTVIEW_INFO *infoPtr, COLORREF color)
9097 TRACE("(color=%x)\n", color);
9099 infoPtr->clrText = color;
9100 return TRUE;
9103 /***
9104 * DESCRIPTION:
9105 * Sets new ToolTip window to ListView control.
9107 * PARAMETER(S):
9108 * [I] infoPtr : valid pointer to the listview structure
9109 * [I] hwndNewToolTip : handle to new ToolTip
9111 * RETURN:
9112 * old tool tip
9114 static HWND LISTVIEW_SetToolTips( LISTVIEW_INFO *infoPtr, HWND hwndNewToolTip)
9116 HWND hwndOldToolTip = infoPtr->hwndToolTip;
9117 infoPtr->hwndToolTip = hwndNewToolTip;
9118 return hwndOldToolTip;
9122 * DESCRIPTION:
9123 * sets the Unicode character format flag for the control
9124 * PARAMETER(S):
9125 * [I] infoPtr :valid pointer to the listview structure
9126 * [I] fUnicode :true to switch to UNICODE false to switch to ANSI
9128 * RETURN:
9129 * Old Unicode Format
9131 static BOOL LISTVIEW_SetUnicodeFormat( LISTVIEW_INFO *infoPtr, BOOL unicode)
9133 SHORT rc = infoPtr->notifyFormat;
9134 infoPtr->notifyFormat = (unicode) ? NFR_UNICODE : NFR_ANSI;
9135 return rc == NFR_UNICODE;
9139 * DESCRIPTION:
9140 * sets the control view mode
9141 * PARAMETER(S):
9142 * [I] infoPtr :valid pointer to the listview structure
9143 * [I] nView :new view mode value
9145 * RETURN:
9146 * SUCCESS: 1
9147 * FAILURE: -1
9149 static INT LISTVIEW_SetView(LISTVIEW_INFO *infoPtr, DWORD nView)
9151 HIMAGELIST himl;
9153 if (infoPtr->uView == nView) return 1;
9155 if ((INT)nView < 0 || nView > LV_VIEW_MAX) return -1;
9156 if (nView == LV_VIEW_TILE)
9158 FIXME("View LV_VIEW_TILE unimplemented\n");
9159 return -1;
9162 infoPtr->uView = nView;
9164 SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
9165 ShowWindow(infoPtr->hwndHeader, SW_HIDE);
9167 ShowScrollBar(infoPtr->hwndSelf, SB_BOTH, FALSE);
9168 SetRectEmpty(&infoPtr->rcFocus);
9170 himl = (nView == LV_VIEW_ICON ? infoPtr->himlNormal : infoPtr->himlSmall);
9171 set_icon_size(&infoPtr->iconSize, himl, nView != LV_VIEW_ICON);
9173 switch (nView)
9175 case LV_VIEW_ICON:
9176 case LV_VIEW_SMALLICON:
9177 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
9178 break;
9179 case LV_VIEW_DETAILS:
9181 HDLAYOUT hl;
9182 WINDOWPOS wp;
9184 LISTVIEW_CreateHeader( infoPtr );
9186 hl.prc = &infoPtr->rcList;
9187 hl.pwpos = &wp;
9188 SendMessageW(infoPtr->hwndHeader, HDM_LAYOUT, 0, (LPARAM)&hl);
9189 SetWindowPos(infoPtr->hwndHeader, infoPtr->hwndSelf, wp.x, wp.y, wp.cx, wp.cy,
9190 wp.flags | ((infoPtr->dwStyle & LVS_NOCOLUMNHEADER) ? SWP_HIDEWINDOW : SWP_SHOWWINDOW));
9191 break;
9193 case LV_VIEW_LIST:
9194 break;
9197 LISTVIEW_UpdateItemSize(infoPtr);
9198 LISTVIEW_UpdateSize(infoPtr);
9199 LISTVIEW_UpdateScroll(infoPtr);
9200 LISTVIEW_InvalidateList(infoPtr);
9202 TRACE("nView=%d\n", nView);
9204 return 1;
9207 /* LISTVIEW_SetWorkAreas */
9209 /***
9210 * DESCRIPTION:
9211 * Callback internally used by LISTVIEW_SortItems() in response of LVM_SORTITEMS
9213 * PARAMETER(S):
9214 * [I] first : pointer to first ITEM_INFO to compare
9215 * [I] second : pointer to second ITEM_INFO to compare
9216 * [I] lParam : HWND of control
9218 * RETURN:
9219 * if first comes before second : negative
9220 * if first comes after second : positive
9221 * if first and second are equivalent : zero
9223 static INT WINAPI LISTVIEW_CallBackCompare(LPVOID first, LPVOID second, LPARAM lParam)
9225 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)lParam;
9226 ITEM_INFO* lv_first = DPA_GetPtr( first, 0 );
9227 ITEM_INFO* lv_second = DPA_GetPtr( second, 0 );
9229 /* Forward the call to the client defined callback */
9230 return (infoPtr->pfnCompare)( lv_first->lParam , lv_second->lParam, infoPtr->lParamSort );
9233 /***
9234 * DESCRIPTION:
9235 * Callback internally used by LISTVIEW_SortItems() in response of LVM_SORTITEMSEX
9237 * PARAMETER(S):
9238 * [I] first : pointer to first ITEM_INFO to compare
9239 * [I] second : pointer to second ITEM_INFO to compare
9240 * [I] lParam : HWND of control
9242 * RETURN:
9243 * if first comes before second : negative
9244 * if first comes after second : positive
9245 * if first and second are equivalent : zero
9247 static INT WINAPI LISTVIEW_CallBackCompareEx(LPVOID first, LPVOID second, LPARAM lParam)
9249 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)lParam;
9250 INT first_idx = DPA_GetPtrIndex( infoPtr->hdpaItems, first );
9251 INT second_idx = DPA_GetPtrIndex( infoPtr->hdpaItems, second );
9253 /* Forward the call to the client defined callback */
9254 return (infoPtr->pfnCompare)( first_idx, second_idx, infoPtr->lParamSort );
9257 /***
9258 * DESCRIPTION:
9259 * Sorts the listview items.
9261 * PARAMETER(S):
9262 * [I] infoPtr : valid pointer to the listview structure
9263 * [I] pfnCompare : application-defined value
9264 * [I] lParamSort : pointer to comparison callback
9265 * [I] IsEx : TRUE when LVM_SORTITEMSEX used
9267 * RETURN:
9268 * SUCCESS : TRUE
9269 * FAILURE : FALSE
9271 static BOOL LISTVIEW_SortItems(LISTVIEW_INFO *infoPtr, PFNLVCOMPARE pfnCompare,
9272 LPARAM lParamSort, BOOL IsEx)
9274 HDPA hdpaSubItems;
9275 ITEM_INFO *lpItem;
9276 LPVOID selectionMarkItem = NULL;
9277 LPVOID focusedItem = NULL;
9278 int i;
9280 TRACE("(pfnCompare=%p, lParamSort=%lx)\n", pfnCompare, lParamSort);
9282 if (infoPtr->dwStyle & LVS_OWNERDATA) return FALSE;
9284 if (!pfnCompare) return FALSE;
9285 if (!infoPtr->hdpaItems) return FALSE;
9287 /* if there are 0 or 1 items, there is no need to sort */
9288 if (infoPtr->nItemCount < 2) return TRUE;
9290 /* clear selection */
9291 ranges_clear(infoPtr->selectionRanges);
9293 /* save selection mark and focused item */
9294 if (infoPtr->nSelectionMark >= 0)
9295 selectionMarkItem = DPA_GetPtr(infoPtr->hdpaItems, infoPtr->nSelectionMark);
9296 if (infoPtr->nFocusedItem >= 0)
9297 focusedItem = DPA_GetPtr(infoPtr->hdpaItems, infoPtr->nFocusedItem);
9299 infoPtr->pfnCompare = pfnCompare;
9300 infoPtr->lParamSort = lParamSort;
9301 if (IsEx)
9302 DPA_Sort(infoPtr->hdpaItems, LISTVIEW_CallBackCompareEx, (LPARAM)infoPtr);
9303 else
9304 DPA_Sort(infoPtr->hdpaItems, LISTVIEW_CallBackCompare, (LPARAM)infoPtr);
9306 /* restore selection ranges */
9307 for (i=0; i < infoPtr->nItemCount; i++)
9309 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, i);
9310 lpItem = DPA_GetPtr(hdpaSubItems, 0);
9312 if (lpItem->state & LVIS_SELECTED)
9313 ranges_additem(infoPtr->selectionRanges, i);
9315 /* restore selection mark and focused item */
9316 infoPtr->nSelectionMark = DPA_GetPtrIndex(infoPtr->hdpaItems, selectionMarkItem);
9317 infoPtr->nFocusedItem = DPA_GetPtrIndex(infoPtr->hdpaItems, focusedItem);
9319 /* I believe nHotItem should be left alone, see LISTVIEW_ShiftIndices */
9321 /* refresh the display */
9322 LISTVIEW_InvalidateList(infoPtr);
9323 return TRUE;
9326 /***
9327 * DESCRIPTION:
9328 * Update theme handle after a theme change.
9330 * PARAMETER(S):
9331 * [I] infoPtr : valid pointer to the listview structure
9333 * RETURN:
9334 * SUCCESS : 0
9335 * FAILURE : something else
9337 static LRESULT LISTVIEW_ThemeChanged(const LISTVIEW_INFO *infoPtr)
9339 HTHEME theme = GetWindowTheme(infoPtr->hwndSelf);
9340 CloseThemeData(theme);
9341 OpenThemeData(infoPtr->hwndSelf, themeClass);
9342 return 0;
9345 /***
9346 * DESCRIPTION:
9347 * Updates an items or rearranges the listview control.
9349 * PARAMETER(S):
9350 * [I] infoPtr : valid pointer to the listview structure
9351 * [I] nItem : item index
9353 * RETURN:
9354 * SUCCESS : TRUE
9355 * FAILURE : FALSE
9357 static BOOL LISTVIEW_Update(LISTVIEW_INFO *infoPtr, INT nItem)
9359 TRACE("(nItem=%d)\n", nItem);
9361 if (nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
9363 /* rearrange with default alignment style */
9364 if (is_autoarrange(infoPtr))
9365 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
9366 else
9367 LISTVIEW_InvalidateItem(infoPtr, nItem);
9369 return TRUE;
9372 /***
9373 * DESCRIPTION:
9374 * Draw the track line at the place defined in the infoPtr structure.
9375 * The line is drawn with a XOR pen so drawing the line for the second time
9376 * in the same place erases the line.
9378 * PARAMETER(S):
9379 * [I] infoPtr : valid pointer to the listview structure
9381 * RETURN:
9382 * SUCCESS : TRUE
9383 * FAILURE : FALSE
9385 static BOOL LISTVIEW_DrawTrackLine(const LISTVIEW_INFO *infoPtr)
9387 HDC hdc;
9389 if (infoPtr->xTrackLine == -1)
9390 return FALSE;
9392 if (!(hdc = GetDC(infoPtr->hwndSelf)))
9393 return FALSE;
9394 PatBlt( hdc, infoPtr->xTrackLine, infoPtr->rcList.top,
9395 1, infoPtr->rcList.bottom - infoPtr->rcList.top, DSTINVERT );
9396 ReleaseDC(infoPtr->hwndSelf, hdc);
9397 return TRUE;
9400 /***
9401 * DESCRIPTION:
9402 * Called when an edit control should be displayed. This function is called after
9403 * we are sure that there was a single click - not a double click (this is a TIMERPROC).
9405 * PARAMETER(S):
9406 * [I] hwnd : Handle to the listview
9407 * [I] uMsg : WM_TIMER (ignored)
9408 * [I] idEvent : The timer ID interpreted as a pointer to a DELAYED_EDIT_ITEM struct
9409 * [I] dwTimer : The elapsed time (ignored)
9411 * RETURN:
9412 * None.
9414 static VOID CALLBACK LISTVIEW_DelayedEditItem(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
9416 DELAYED_ITEM_EDIT *editItem = (DELAYED_ITEM_EDIT *)idEvent;
9417 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongPtrW(hwnd, 0);
9419 KillTimer(hwnd, idEvent);
9420 editItem->fEnabled = FALSE;
9421 /* check if the item is still selected */
9422 if (infoPtr->bFocus && LISTVIEW_GetItemState(infoPtr, editItem->iItem, LVIS_SELECTED))
9423 LISTVIEW_EditLabelT(infoPtr, editItem->iItem, TRUE);
9426 /***
9427 * DESCRIPTION:
9428 * Creates the listview control - the WM_NCCREATE phase.
9430 * PARAMETER(S):
9431 * [I] hwnd : window handle
9432 * [I] lpcs : the create parameters
9434 * RETURN:
9435 * Success: TRUE
9436 * Failure: FALSE
9438 static LRESULT LISTVIEW_NCCreate(HWND hwnd, const CREATESTRUCTW *lpcs)
9440 LISTVIEW_INFO *infoPtr;
9441 LOGFONTW logFont;
9443 TRACE("(lpcs=%p)\n", lpcs);
9445 /* initialize info pointer */
9446 infoPtr = Alloc(sizeof(LISTVIEW_INFO));
9447 if (!infoPtr) return FALSE;
9449 SetWindowLongPtrW(hwnd, 0, (DWORD_PTR)infoPtr);
9451 infoPtr->hwndSelf = hwnd;
9452 infoPtr->dwStyle = lpcs->style; /* Note: may be changed in WM_CREATE */
9453 map_style_view(infoPtr);
9454 /* determine the type of structures to use */
9455 infoPtr->hwndNotify = lpcs->hwndParent;
9456 /* infoPtr->notifyFormat will be filled in WM_CREATE */
9458 /* initialize color information */
9459 infoPtr->clrBk = CLR_NONE;
9460 infoPtr->clrText = CLR_DEFAULT;
9461 infoPtr->clrTextBk = CLR_DEFAULT;
9462 LISTVIEW_SetBkColor(infoPtr, comctl32_color.clrWindow);
9464 /* set default values */
9465 infoPtr->nFocusedItem = -1;
9466 infoPtr->nSelectionMark = -1;
9467 infoPtr->nHotItem = -1;
9468 infoPtr->bRedraw = TRUE;
9469 infoPtr->bNoItemMetrics = TRUE;
9470 infoPtr->bDoChangeNotify = TRUE;
9471 infoPtr->autoSpacing = TRUE;
9472 infoPtr->iconSpacing.cx = GetSystemMetrics(SM_CXICONSPACING) - GetSystemMetrics(SM_CXICON);
9473 infoPtr->iconSpacing.cy = GetSystemMetrics(SM_CYICONSPACING) - GetSystemMetrics(SM_CYICON);
9474 infoPtr->nEditLabelItem = -1;
9475 infoPtr->nLButtonDownItem = -1;
9476 infoPtr->dwHoverTime = HOVER_DEFAULT; /* default system hover time */
9477 infoPtr->cWheelRemainder = 0;
9478 infoPtr->nMeasureItemHeight = 0;
9479 infoPtr->xTrackLine = -1; /* no track line */
9480 infoPtr->itemEdit.fEnabled = FALSE;
9481 infoPtr->iVersion = COMCTL32_VERSION;
9482 infoPtr->colRectsDirty = FALSE;
9484 /* get default font (icon title) */
9485 SystemParametersInfoW(SPI_GETICONTITLELOGFONT, 0, &logFont, 0);
9486 infoPtr->hDefaultFont = CreateFontIndirectW(&logFont);
9487 infoPtr->hFont = infoPtr->hDefaultFont;
9488 LISTVIEW_SaveTextMetrics(infoPtr);
9490 /* allocate memory for the data structure */
9491 if (!(infoPtr->selectionRanges = ranges_create(10))) goto fail;
9492 if (!(infoPtr->hdpaItems = DPA_Create(10))) goto fail;
9493 if (!(infoPtr->hdpaItemIds = DPA_Create(10))) goto fail;
9494 if (!(infoPtr->hdpaPosX = DPA_Create(10))) goto fail;
9495 if (!(infoPtr->hdpaPosY = DPA_Create(10))) goto fail;
9496 if (!(infoPtr->hdpaColumns = DPA_Create(10))) goto fail;
9497 return TRUE;
9499 fail:
9500 DestroyWindow(infoPtr->hwndHeader);
9501 ranges_destroy(infoPtr->selectionRanges);
9502 DPA_Destroy(infoPtr->hdpaItems);
9503 DPA_Destroy(infoPtr->hdpaItemIds);
9504 DPA_Destroy(infoPtr->hdpaPosX);
9505 DPA_Destroy(infoPtr->hdpaPosY);
9506 DPA_Destroy(infoPtr->hdpaColumns);
9507 Free(infoPtr);
9508 return FALSE;
9511 /***
9512 * DESCRIPTION:
9513 * Creates the listview control - the WM_CREATE phase. Most of the data is
9514 * already set up in LISTVIEW_NCCreate
9516 * PARAMETER(S):
9517 * [I] hwnd : window handle
9518 * [I] lpcs : the create parameters
9520 * RETURN:
9521 * Success: 0
9522 * Failure: -1
9524 static LRESULT LISTVIEW_Create(HWND hwnd, const CREATESTRUCTW *lpcs)
9526 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongPtrW(hwnd, 0);
9528 TRACE("(lpcs=%p, style=0x%08x)\n", lpcs, lpcs->style);
9530 infoPtr->dwStyle = lpcs->style;
9531 map_style_view(infoPtr);
9533 infoPtr->notifyFormat = SendMessageW(infoPtr->hwndNotify, WM_NOTIFYFORMAT,
9534 (WPARAM)infoPtr->hwndSelf, NF_QUERY);
9535 /* on error defaulting to ANSI notifications */
9536 if (infoPtr->notifyFormat == 0) infoPtr->notifyFormat = NFR_ANSI;
9537 TRACE("notify format=%d\n", infoPtr->notifyFormat);
9539 if ((infoPtr->uView == LV_VIEW_DETAILS) && (lpcs->style & WS_VISIBLE))
9541 if (LISTVIEW_CreateHeader(infoPtr) < 0) return -1;
9543 else
9544 infoPtr->hwndHeader = 0;
9546 /* init item size to avoid division by 0 */
9547 LISTVIEW_UpdateItemSize (infoPtr);
9548 LISTVIEW_UpdateSize (infoPtr);
9550 if (infoPtr->uView == LV_VIEW_DETAILS)
9552 if (!(LVS_NOCOLUMNHEADER & lpcs->style) && (WS_VISIBLE & lpcs->style))
9554 ShowWindow(infoPtr->hwndHeader, SW_SHOWNORMAL);
9556 LISTVIEW_UpdateScroll(infoPtr);
9557 /* send WM_MEASUREITEM notification */
9558 if (infoPtr->dwStyle & LVS_OWNERDRAWFIXED) notify_measureitem(infoPtr);
9561 OpenThemeData(hwnd, themeClass);
9563 /* initialize the icon sizes */
9564 set_icon_size(&infoPtr->iconSize, infoPtr->himlNormal, infoPtr->uView != LV_VIEW_ICON);
9565 set_icon_size(&infoPtr->iconStateSize, infoPtr->himlState, TRUE);
9566 return 0;
9569 /***
9570 * DESCRIPTION:
9571 * Destroys the listview control.
9573 * PARAMETER(S):
9574 * [I] infoPtr : valid pointer to the listview structure
9576 * RETURN:
9577 * Success: 0
9578 * Failure: -1
9580 static LRESULT LISTVIEW_Destroy(LISTVIEW_INFO *infoPtr)
9582 HTHEME theme = GetWindowTheme(infoPtr->hwndSelf);
9583 CloseThemeData(theme);
9585 /* delete all items */
9586 LISTVIEW_DeleteAllItems(infoPtr, TRUE);
9588 return 0;
9591 /***
9592 * DESCRIPTION:
9593 * Enables the listview control.
9595 * PARAMETER(S):
9596 * [I] infoPtr : valid pointer to the listview structure
9597 * [I] bEnable : specifies whether to enable or disable the window
9599 * RETURN:
9600 * SUCCESS : TRUE
9601 * FAILURE : FALSE
9603 static BOOL LISTVIEW_Enable(const LISTVIEW_INFO *infoPtr)
9605 if (infoPtr->dwStyle & LVS_OWNERDRAWFIXED)
9606 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
9607 return TRUE;
9610 /***
9611 * DESCRIPTION:
9612 * Erases the background of the listview control.
9614 * PARAMETER(S):
9615 * [I] infoPtr : valid pointer to the listview structure
9616 * [I] hdc : device context handle
9618 * RETURN:
9619 * SUCCESS : TRUE
9620 * FAILURE : FALSE
9622 static inline BOOL LISTVIEW_EraseBkgnd(const LISTVIEW_INFO *infoPtr, HDC hdc)
9624 RECT rc;
9626 TRACE("(hdc=%p)\n", hdc);
9628 if (!GetClipBox(hdc, &rc)) return FALSE;
9630 if (infoPtr->clrBk == CLR_NONE)
9632 if (infoPtr->dwLvExStyle & LVS_EX_TRANSPARENTBKGND)
9633 return SendMessageW(infoPtr->hwndNotify, WM_PRINTCLIENT,
9634 (WPARAM)hdc, PRF_ERASEBKGND);
9635 else
9636 return SendMessageW(infoPtr->hwndNotify, WM_ERASEBKGND, (WPARAM)hdc, 0);
9639 /* for double buffered controls we need to do this during refresh */
9640 if (infoPtr->dwLvExStyle & LVS_EX_DOUBLEBUFFER) return FALSE;
9642 return LISTVIEW_FillBkgnd(infoPtr, hdc, &rc);
9646 /***
9647 * DESCRIPTION:
9648 * Helper function for LISTVIEW_[HV]Scroll *only*.
9649 * Performs vertical/horizontal scrolling by a give amount.
9651 * PARAMETER(S):
9652 * [I] infoPtr : valid pointer to the listview structure
9653 * [I] dx : amount of horizontal scroll
9654 * [I] dy : amount of vertical scroll
9656 static void scroll_list(LISTVIEW_INFO *infoPtr, INT dx, INT dy)
9658 /* now we can scroll the list */
9659 ScrollWindowEx(infoPtr->hwndSelf, dx, dy, &infoPtr->rcList,
9660 &infoPtr->rcList, 0, 0, SW_ERASE | SW_INVALIDATE);
9661 /* if we have focus, adjust rect */
9662 OffsetRect(&infoPtr->rcFocus, dx, dy);
9663 UpdateWindow(infoPtr->hwndSelf);
9666 /***
9667 * DESCRIPTION:
9668 * Performs vertical scrolling.
9670 * PARAMETER(S):
9671 * [I] infoPtr : valid pointer to the listview structure
9672 * [I] nScrollCode : scroll code
9673 * [I] nScrollDiff : units to scroll in SB_INTERNAL mode, 0 otherwise
9674 * [I] hScrollWnd : scrollbar control window handle
9676 * RETURN:
9677 * Zero
9679 * NOTES:
9680 * SB_LINEUP/SB_LINEDOWN:
9681 * for LVS_ICON, LVS_SMALLICON is 37 by experiment
9682 * for LVS_REPORT is 1 line
9683 * for LVS_LIST cannot occur
9686 static LRESULT LISTVIEW_VScroll(LISTVIEW_INFO *infoPtr, INT nScrollCode,
9687 INT nScrollDiff)
9689 INT nOldScrollPos, nNewScrollPos;
9690 SCROLLINFO scrollInfo;
9691 BOOL is_an_icon;
9693 TRACE("(nScrollCode=%d(%s), nScrollDiff=%d)\n", nScrollCode,
9694 debugscrollcode(nScrollCode), nScrollDiff);
9696 if (infoPtr->hwndEdit) SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
9698 scrollInfo.cbSize = sizeof(SCROLLINFO);
9699 scrollInfo.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_TRACKPOS;
9701 is_an_icon = ((infoPtr->uView == LV_VIEW_ICON) || (infoPtr->uView == LV_VIEW_SMALLICON));
9703 if (!GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo)) return 1;
9705 nOldScrollPos = scrollInfo.nPos;
9706 switch (nScrollCode)
9708 case SB_INTERNAL:
9709 break;
9711 case SB_LINEUP:
9712 nScrollDiff = (is_an_icon) ? -LISTVIEW_SCROLL_ICON_LINE_SIZE : -1;
9713 break;
9715 case SB_LINEDOWN:
9716 nScrollDiff = (is_an_icon) ? LISTVIEW_SCROLL_ICON_LINE_SIZE : 1;
9717 break;
9719 case SB_PAGEUP:
9720 nScrollDiff = -scrollInfo.nPage;
9721 break;
9723 case SB_PAGEDOWN:
9724 nScrollDiff = scrollInfo.nPage;
9725 break;
9727 case SB_THUMBPOSITION:
9728 case SB_THUMBTRACK:
9729 nScrollDiff = scrollInfo.nTrackPos - scrollInfo.nPos;
9730 break;
9732 default:
9733 nScrollDiff = 0;
9736 /* quit right away if pos isn't changing */
9737 if (nScrollDiff == 0) return 0;
9739 /* calculate new position, and handle overflows */
9740 nNewScrollPos = scrollInfo.nPos + nScrollDiff;
9741 if (nScrollDiff > 0) {
9742 if (nNewScrollPos < nOldScrollPos ||
9743 nNewScrollPos > scrollInfo.nMax)
9744 nNewScrollPos = scrollInfo.nMax;
9745 } else {
9746 if (nNewScrollPos > nOldScrollPos ||
9747 nNewScrollPos < scrollInfo.nMin)
9748 nNewScrollPos = scrollInfo.nMin;
9751 /* set the new position, and reread in case it changed */
9752 scrollInfo.fMask = SIF_POS;
9753 scrollInfo.nPos = nNewScrollPos;
9754 nNewScrollPos = SetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo, TRUE);
9756 /* carry on only if it really changed */
9757 if (nNewScrollPos == nOldScrollPos) return 0;
9759 /* now adjust to client coordinates */
9760 nScrollDiff = nOldScrollPos - nNewScrollPos;
9761 if (infoPtr->uView == LV_VIEW_DETAILS) nScrollDiff *= infoPtr->nItemHeight;
9763 /* and scroll the window */
9764 scroll_list(infoPtr, 0, nScrollDiff);
9766 return 0;
9769 /***
9770 * DESCRIPTION:
9771 * Performs horizontal scrolling.
9773 * PARAMETER(S):
9774 * [I] infoPtr : valid pointer to the listview structure
9775 * [I] nScrollCode : scroll code
9776 * [I] nScrollDiff : units to scroll in SB_INTERNAL mode, 0 otherwise
9777 * [I] hScrollWnd : scrollbar control window handle
9779 * RETURN:
9780 * Zero
9782 * NOTES:
9783 * SB_LINELEFT/SB_LINERIGHT:
9784 * for LVS_ICON, LVS_SMALLICON 1 pixel
9785 * for LVS_REPORT is 1 pixel
9786 * for LVS_LIST is 1 column --> which is a 1 because the
9787 * scroll is based on columns not pixels
9790 static LRESULT LISTVIEW_HScroll(LISTVIEW_INFO *infoPtr, INT nScrollCode,
9791 INT nScrollDiff)
9793 INT nOldScrollPos, nNewScrollPos;
9794 SCROLLINFO scrollInfo;
9795 BOOL is_an_icon;
9797 TRACE("(nScrollCode=%d(%s), nScrollDiff=%d)\n", nScrollCode,
9798 debugscrollcode(nScrollCode), nScrollDiff);
9800 if (infoPtr->hwndEdit) SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
9802 scrollInfo.cbSize = sizeof(SCROLLINFO);
9803 scrollInfo.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_TRACKPOS;
9805 is_an_icon = ((infoPtr->uView == LV_VIEW_ICON) || (infoPtr->uView == LV_VIEW_SMALLICON));
9807 if (!GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo)) return 1;
9809 nOldScrollPos = scrollInfo.nPos;
9811 switch (nScrollCode)
9813 case SB_INTERNAL:
9814 break;
9816 case SB_LINELEFT:
9817 nScrollDiff = (is_an_icon) ? -LISTVIEW_SCROLL_ICON_LINE_SIZE : -1;
9818 break;
9820 case SB_LINERIGHT:
9821 nScrollDiff = (is_an_icon) ? LISTVIEW_SCROLL_ICON_LINE_SIZE : 1;
9822 break;
9824 case SB_PAGELEFT:
9825 nScrollDiff = -scrollInfo.nPage;
9826 break;
9828 case SB_PAGERIGHT:
9829 nScrollDiff = scrollInfo.nPage;
9830 break;
9832 case SB_THUMBPOSITION:
9833 case SB_THUMBTRACK:
9834 nScrollDiff = scrollInfo.nTrackPos - scrollInfo.nPos;
9835 break;
9837 default:
9838 nScrollDiff = 0;
9841 /* quit right away if pos isn't changing */
9842 if (nScrollDiff == 0) return 0;
9844 /* calculate new position, and handle overflows */
9845 nNewScrollPos = scrollInfo.nPos + nScrollDiff;
9846 if (nScrollDiff > 0) {
9847 if (nNewScrollPos < nOldScrollPos ||
9848 nNewScrollPos > scrollInfo.nMax)
9849 nNewScrollPos = scrollInfo.nMax;
9850 } else {
9851 if (nNewScrollPos > nOldScrollPos ||
9852 nNewScrollPos < scrollInfo.nMin)
9853 nNewScrollPos = scrollInfo.nMin;
9856 /* set the new position, and reread in case it changed */
9857 scrollInfo.fMask = SIF_POS;
9858 scrollInfo.nPos = nNewScrollPos;
9859 nNewScrollPos = SetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo, TRUE);
9861 /* carry on only if it really changed */
9862 if (nNewScrollPos == nOldScrollPos) return 0;
9864 if (infoPtr->hwndHeader) LISTVIEW_UpdateHeaderSize(infoPtr, nNewScrollPos);
9866 /* now adjust to client coordinates */
9867 nScrollDiff = nOldScrollPos - nNewScrollPos;
9868 if (infoPtr->uView == LV_VIEW_LIST) nScrollDiff *= infoPtr->nItemWidth;
9870 /* and scroll the window */
9871 scroll_list(infoPtr, nScrollDiff, 0);
9873 return 0;
9876 static LRESULT LISTVIEW_MouseWheel(LISTVIEW_INFO *infoPtr, INT wheelDelta)
9878 UINT pulScrollLines = 3;
9880 TRACE("(wheelDelta=%d)\n", wheelDelta);
9882 switch(infoPtr->uView)
9884 case LV_VIEW_ICON:
9885 case LV_VIEW_SMALLICON:
9887 * listview should be scrolled by a multiple of 37 dependently on its dimension or its visible item number
9888 * should be fixed in the future.
9890 LISTVIEW_VScroll(infoPtr, SB_INTERNAL, (wheelDelta > 0) ?
9891 -LISTVIEW_SCROLL_ICON_LINE_SIZE : LISTVIEW_SCROLL_ICON_LINE_SIZE);
9892 break;
9894 case LV_VIEW_DETAILS:
9895 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
9897 /* if scrolling changes direction, ignore left overs */
9898 if ((wheelDelta < 0 && infoPtr->cWheelRemainder < 0) ||
9899 (wheelDelta > 0 && infoPtr->cWheelRemainder > 0))
9900 infoPtr->cWheelRemainder += wheelDelta;
9901 else
9902 infoPtr->cWheelRemainder = wheelDelta;
9903 if (infoPtr->cWheelRemainder && pulScrollLines)
9905 int cLineScroll;
9906 pulScrollLines = min((UINT)LISTVIEW_GetCountPerColumn(infoPtr), pulScrollLines);
9907 cLineScroll = pulScrollLines * (float)infoPtr->cWheelRemainder / WHEEL_DELTA;
9908 infoPtr->cWheelRemainder -= WHEEL_DELTA * cLineScroll / (int)pulScrollLines;
9909 LISTVIEW_VScroll(infoPtr, SB_INTERNAL, -cLineScroll);
9911 break;
9913 case LV_VIEW_LIST:
9914 LISTVIEW_HScroll(infoPtr, (wheelDelta > 0) ? SB_LINELEFT : SB_LINERIGHT, 0);
9915 break;
9917 return 0;
9920 /***
9921 * DESCRIPTION:
9922 * ???
9924 * PARAMETER(S):
9925 * [I] infoPtr : valid pointer to the listview structure
9926 * [I] nVirtualKey : virtual key
9927 * [I] lKeyData : key data
9929 * RETURN:
9930 * Zero
9932 static LRESULT LISTVIEW_KeyDown(LISTVIEW_INFO *infoPtr, INT nVirtualKey, LONG lKeyData)
9934 HWND hwndSelf = infoPtr->hwndSelf;
9935 INT nItem = -1;
9936 NMLVKEYDOWN nmKeyDown;
9938 TRACE("(nVirtualKey=%d, lKeyData=%d)\n", nVirtualKey, lKeyData);
9940 /* send LVN_KEYDOWN notification */
9941 nmKeyDown.wVKey = nVirtualKey;
9942 nmKeyDown.flags = 0;
9943 notify_hdr(infoPtr, LVN_KEYDOWN, &nmKeyDown.hdr);
9944 if (!IsWindow(hwndSelf))
9945 return 0;
9947 switch (nVirtualKey)
9949 case VK_SPACE:
9950 nItem = infoPtr->nFocusedItem;
9951 if (infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES)
9952 toggle_checkbox_state(infoPtr, infoPtr->nFocusedItem);
9953 break;
9955 case VK_RETURN:
9956 if ((infoPtr->nItemCount > 0) && (infoPtr->nFocusedItem != -1))
9958 if (!notify(infoPtr, NM_RETURN)) return 0;
9959 if (!notify(infoPtr, LVN_ITEMACTIVATE)) return 0;
9961 break;
9963 case VK_HOME:
9964 if (infoPtr->nItemCount > 0)
9965 nItem = 0;
9966 break;
9968 case VK_END:
9969 if (infoPtr->nItemCount > 0)
9970 nItem = infoPtr->nItemCount - 1;
9971 break;
9973 case VK_LEFT:
9974 nItem = LISTVIEW_GetNextItem(infoPtr, infoPtr->nFocusedItem, LVNI_TOLEFT);
9975 break;
9977 case VK_UP:
9978 nItem = LISTVIEW_GetNextItem(infoPtr, infoPtr->nFocusedItem, LVNI_ABOVE);
9979 break;
9981 case VK_RIGHT:
9982 nItem = LISTVIEW_GetNextItem(infoPtr, infoPtr->nFocusedItem, LVNI_TORIGHT);
9983 break;
9985 case VK_DOWN:
9986 nItem = LISTVIEW_GetNextItem(infoPtr, infoPtr->nFocusedItem, LVNI_BELOW);
9987 break;
9989 case VK_PRIOR:
9990 if (infoPtr->uView == LV_VIEW_DETAILS)
9992 INT topidx = LISTVIEW_GetTopIndex(infoPtr);
9993 if (infoPtr->nFocusedItem == topidx)
9994 nItem = topidx - LISTVIEW_GetCountPerColumn(infoPtr) + 1;
9995 else
9996 nItem = topidx;
9998 else
9999 nItem = infoPtr->nFocusedItem - LISTVIEW_GetCountPerColumn(infoPtr)
10000 * LISTVIEW_GetCountPerRow(infoPtr);
10001 if(nItem < 0) nItem = 0;
10002 break;
10004 case VK_NEXT:
10005 if (infoPtr->uView == LV_VIEW_DETAILS)
10007 INT topidx = LISTVIEW_GetTopIndex(infoPtr);
10008 INT cnt = LISTVIEW_GetCountPerColumn(infoPtr);
10009 if (infoPtr->nFocusedItem == topidx + cnt - 1)
10010 nItem = infoPtr->nFocusedItem + cnt - 1;
10011 else
10012 nItem = topidx + cnt - 1;
10014 else
10015 nItem = infoPtr->nFocusedItem + LISTVIEW_GetCountPerColumn(infoPtr)
10016 * LISTVIEW_GetCountPerRow(infoPtr);
10017 if(nItem >= infoPtr->nItemCount) nItem = infoPtr->nItemCount - 1;
10018 break;
10021 if ((nItem != -1) && (nItem != infoPtr->nFocusedItem || nVirtualKey == VK_SPACE))
10022 LISTVIEW_KeySelection(infoPtr, nItem, nVirtualKey == VK_SPACE);
10024 return 0;
10027 /***
10028 * DESCRIPTION:
10029 * Kills the focus.
10031 * PARAMETER(S):
10032 * [I] infoPtr : valid pointer to the listview structure
10034 * RETURN:
10035 * Zero
10037 static LRESULT LISTVIEW_KillFocus(LISTVIEW_INFO *infoPtr)
10039 TRACE("()\n");
10041 /* drop any left over scroll amount */
10042 infoPtr->cWheelRemainder = 0;
10044 /* if we did not have the focus, there's nothing more to do */
10045 if (!infoPtr->bFocus) return 0;
10047 /* send NM_KILLFOCUS notification */
10048 if (!notify(infoPtr, NM_KILLFOCUS)) return 0;
10050 /* if we have a focus rectangle, get rid of it */
10051 LISTVIEW_ShowFocusRect(infoPtr, FALSE);
10053 /* if have a marquee selection, stop it */
10054 if (infoPtr->bMarqueeSelect)
10056 /* Remove the marquee rectangle and release our mouse capture */
10057 LISTVIEW_InvalidateRect(infoPtr, &infoPtr->marqueeRect);
10058 ReleaseCapture();
10060 SetRectEmpty(&infoPtr->marqueeRect);
10062 infoPtr->bMarqueeSelect = FALSE;
10063 infoPtr->bScrolling = FALSE;
10064 KillTimer(infoPtr->hwndSelf, (UINT_PTR) infoPtr);
10067 /* set window focus flag */
10068 infoPtr->bFocus = FALSE;
10070 /* invalidate the selected items before resetting focus flag */
10071 LISTVIEW_InvalidateSelectedItems(infoPtr);
10073 return 0;
10076 /***
10077 * DESCRIPTION:
10078 * Processes double click messages (left mouse button).
10080 * PARAMETER(S):
10081 * [I] infoPtr : valid pointer to the listview structure
10082 * [I] wKey : key flag
10083 * [I] x,y : mouse coordinate
10085 * RETURN:
10086 * Zero
10088 static LRESULT LISTVIEW_LButtonDblClk(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
10090 LVHITTESTINFO htInfo;
10092 TRACE("(key=%hu, X=%u, Y=%u)\n", wKey, x, y);
10094 /* Cancel the item edition if any */
10095 if (infoPtr->itemEdit.fEnabled)
10097 KillTimer(infoPtr->hwndSelf, (UINT_PTR)&infoPtr->itemEdit);
10098 infoPtr->itemEdit.fEnabled = FALSE;
10101 /* send NM_RELEASEDCAPTURE notification */
10102 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
10104 htInfo.pt.x = x;
10105 htInfo.pt.y = y;
10107 /* send NM_DBLCLK notification */
10108 LISTVIEW_HitTest(infoPtr, &htInfo, TRUE, FALSE);
10109 if (!notify_click(infoPtr, NM_DBLCLK, &htInfo)) return 0;
10111 /* To send the LVN_ITEMACTIVATE, it must be on an Item */
10112 if(htInfo.iItem != -1) notify_itemactivate(infoPtr,&htInfo);
10114 return 0;
10117 static LRESULT LISTVIEW_TrackMouse(const LISTVIEW_INFO *infoPtr, POINT pt)
10119 MSG msg;
10120 RECT r;
10122 r.top = r.bottom = pt.y;
10123 r.left = r.right = pt.x;
10125 InflateRect(&r, GetSystemMetrics(SM_CXDRAG), GetSystemMetrics(SM_CYDRAG));
10127 SetCapture(infoPtr->hwndSelf);
10129 while (1)
10131 if (PeekMessageW(&msg, 0, 0, 0, PM_REMOVE | PM_NOYIELD))
10133 if (msg.message == WM_MOUSEMOVE)
10135 pt.x = (short)LOWORD(msg.lParam);
10136 pt.y = (short)HIWORD(msg.lParam);
10137 if (PtInRect(&r, pt))
10138 continue;
10139 else
10141 ReleaseCapture();
10142 return 1;
10145 else if (msg.message >= WM_LBUTTONDOWN &&
10146 msg.message <= WM_RBUTTONDBLCLK)
10148 break;
10151 DispatchMessageW(&msg);
10154 if (GetCapture() != infoPtr->hwndSelf)
10155 return 0;
10158 ReleaseCapture();
10159 return 0;
10163 /***
10164 * DESCRIPTION:
10165 * Processes mouse down messages (left mouse button).
10167 * PARAMETERS:
10168 * infoPtr [I ] valid pointer to the listview structure
10169 * wKey [I ] key flag
10170 * x,y [I ] mouse coordinate
10172 * RETURN:
10173 * Zero
10175 static LRESULT LISTVIEW_LButtonDown(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
10177 LVHITTESTINFO lvHitTestInfo;
10178 static BOOL bGroupSelect = TRUE;
10179 POINT pt = { x, y };
10180 INT nItem;
10182 TRACE("(key=%hu, X=%u, Y=%u)\n", wKey, x, y);
10184 /* send NM_RELEASEDCAPTURE notification */
10185 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
10187 /* set left button down flag and record the click position */
10188 infoPtr->bLButtonDown = TRUE;
10189 infoPtr->ptClickPos = pt;
10190 infoPtr->bDragging = FALSE;
10191 infoPtr->bMarqueeSelect = FALSE;
10192 infoPtr->bScrolling = FALSE;
10194 lvHitTestInfo.pt.x = x;
10195 lvHitTestInfo.pt.y = y;
10197 nItem = LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, TRUE);
10198 TRACE("at %s, nItem=%d\n", wine_dbgstr_point(&pt), nItem);
10199 if ((nItem >= 0) && (nItem < infoPtr->nItemCount))
10201 if ((infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES) && (lvHitTestInfo.flags & LVHT_ONITEMSTATEICON))
10203 toggle_checkbox_state(infoPtr, nItem);
10204 return 0;
10207 if (infoPtr->dwStyle & LVS_SINGLESEL)
10209 if (LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED))
10210 infoPtr->nEditLabelItem = nItem;
10211 else
10212 LISTVIEW_SetSelection(infoPtr, nItem);
10214 else
10216 if ((wKey & MK_CONTROL) && (wKey & MK_SHIFT))
10218 if (bGroupSelect)
10220 if (!LISTVIEW_AddGroupSelection(infoPtr, nItem)) return 0;
10221 LISTVIEW_SetItemFocus(infoPtr, nItem);
10222 infoPtr->nSelectionMark = nItem;
10224 else
10226 LVITEMW item;
10228 item.state = LVIS_SELECTED | LVIS_FOCUSED;
10229 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
10231 LISTVIEW_SetItemState(infoPtr,nItem,&item);
10232 infoPtr->nSelectionMark = nItem;
10235 else if (wKey & MK_CONTROL)
10237 LVITEMW item;
10239 bGroupSelect = (LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED) == 0);
10241 item.state = (bGroupSelect ? LVIS_SELECTED : 0) | LVIS_FOCUSED;
10242 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
10243 LISTVIEW_SetItemState(infoPtr, nItem, &item);
10244 infoPtr->nSelectionMark = nItem;
10246 else if (wKey & MK_SHIFT)
10248 LISTVIEW_SetGroupSelection(infoPtr, nItem);
10250 else
10252 if (LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED))
10254 infoPtr->nEditLabelItem = nItem;
10255 infoPtr->nLButtonDownItem = nItem;
10257 LISTVIEW_SetItemFocus(infoPtr, nItem);
10259 else
10260 /* set selection (clears other pre-existing selections) */
10261 LISTVIEW_SetSelection(infoPtr, nItem);
10265 if (!infoPtr->bFocus)
10266 SetFocus(infoPtr->hwndSelf);
10268 if (infoPtr->dwLvExStyle & LVS_EX_ONECLICKACTIVATE)
10269 if(lvHitTestInfo.iItem != -1) notify_itemactivate(infoPtr,&lvHitTestInfo);
10271 else
10273 if (!infoPtr->bFocus)
10274 SetFocus(infoPtr->hwndSelf);
10276 /* remove all selections */
10277 if (!(wKey & MK_CONTROL) && !(wKey & MK_SHIFT))
10278 LISTVIEW_DeselectAll(infoPtr);
10279 ReleaseCapture();
10282 return 0;
10285 /***
10286 * DESCRIPTION:
10287 * Processes mouse up messages (left mouse button).
10289 * PARAMETERS:
10290 * infoPtr [I ] valid pointer to the listview structure
10291 * wKey [I ] key flag
10292 * x,y [I ] mouse coordinate
10294 * RETURN:
10295 * Zero
10297 static LRESULT LISTVIEW_LButtonUp(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
10299 LVHITTESTINFO lvHitTestInfo;
10301 TRACE("(key=%hu, X=%u, Y=%u)\n", wKey, x, y);
10303 if (!infoPtr->bLButtonDown) return 0;
10305 lvHitTestInfo.pt.x = x;
10306 lvHitTestInfo.pt.y = y;
10308 /* send NM_CLICK notification */
10309 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
10310 if (!notify_click(infoPtr, NM_CLICK, &lvHitTestInfo)) return 0;
10312 /* set left button flag */
10313 infoPtr->bLButtonDown = FALSE;
10315 /* set a single selection, reset others */
10316 if(lvHitTestInfo.iItem == infoPtr->nLButtonDownItem && lvHitTestInfo.iItem != -1)
10317 LISTVIEW_SetSelection(infoPtr, infoPtr->nLButtonDownItem);
10318 infoPtr->nLButtonDownItem = -1;
10320 if (infoPtr->bDragging || infoPtr->bMarqueeSelect)
10322 /* Remove the marquee rectangle and release our mouse capture */
10323 if (infoPtr->bMarqueeSelect)
10325 LISTVIEW_InvalidateRect(infoPtr, &infoPtr->marqueeDrawRect);
10326 ReleaseCapture();
10329 SetRectEmpty(&infoPtr->marqueeRect);
10330 SetRectEmpty(&infoPtr->marqueeDrawRect);
10332 infoPtr->bDragging = FALSE;
10333 infoPtr->bMarqueeSelect = FALSE;
10334 infoPtr->bScrolling = FALSE;
10336 KillTimer(infoPtr->hwndSelf, (UINT_PTR) infoPtr);
10337 return 0;
10340 /* if we clicked on a selected item, edit the label */
10341 if(lvHitTestInfo.iItem == infoPtr->nEditLabelItem && (lvHitTestInfo.flags & LVHT_ONITEMLABEL))
10343 /* we want to make sure the user doesn't want to do a double click. So we will
10344 * delay the edit. WM_LBUTTONDBLCLICK will cancel the timer
10346 infoPtr->itemEdit.fEnabled = TRUE;
10347 infoPtr->itemEdit.iItem = lvHitTestInfo.iItem;
10348 SetTimer(infoPtr->hwndSelf,
10349 (UINT_PTR)&infoPtr->itemEdit,
10350 GetDoubleClickTime(),
10351 LISTVIEW_DelayedEditItem);
10354 return 0;
10357 /***
10358 * DESCRIPTION:
10359 * Destroys the listview control (called after WM_DESTROY).
10361 * PARAMETER(S):
10362 * [I] infoPtr : valid pointer to the listview structure
10364 * RETURN:
10365 * Zero
10367 static LRESULT LISTVIEW_NCDestroy(LISTVIEW_INFO *infoPtr)
10369 INT i;
10371 TRACE("()\n");
10373 /* destroy data structure */
10374 DPA_Destroy(infoPtr->hdpaItems);
10375 DPA_Destroy(infoPtr->hdpaItemIds);
10376 DPA_Destroy(infoPtr->hdpaPosX);
10377 DPA_Destroy(infoPtr->hdpaPosY);
10378 /* columns */
10379 for (i = 0; i < DPA_GetPtrCount(infoPtr->hdpaColumns); i++)
10380 Free(DPA_GetPtr(infoPtr->hdpaColumns, i));
10381 DPA_Destroy(infoPtr->hdpaColumns);
10382 ranges_destroy(infoPtr->selectionRanges);
10384 /* destroy image lists */
10385 if (!(infoPtr->dwStyle & LVS_SHAREIMAGELISTS))
10387 ImageList_Destroy(infoPtr->himlNormal);
10388 ImageList_Destroy(infoPtr->himlSmall);
10389 ImageList_Destroy(infoPtr->himlState);
10392 /* destroy font, bkgnd brush */
10393 infoPtr->hFont = 0;
10394 if (infoPtr->hDefaultFont) DeleteObject(infoPtr->hDefaultFont);
10395 if (infoPtr->clrBk != CLR_NONE) DeleteObject(infoPtr->hBkBrush);
10397 SetWindowLongPtrW(infoPtr->hwndSelf, 0, 0);
10399 /* free listview info pointer*/
10400 Free(infoPtr);
10402 return 0;
10405 /***
10406 * DESCRIPTION:
10407 * Handles notifications.
10409 * PARAMETER(S):
10410 * [I] infoPtr : valid pointer to the listview structure
10411 * [I] lpnmhdr : notification information
10413 * RETURN:
10414 * Zero
10416 static LRESULT LISTVIEW_Notify(LISTVIEW_INFO *infoPtr, NMHDR *lpnmhdr)
10418 NMHEADERW *lpnmh;
10420 TRACE("(lpnmhdr=%p)\n", lpnmhdr);
10422 if (!lpnmhdr || lpnmhdr->hwndFrom != infoPtr->hwndHeader) return 0;
10424 /* remember: HDN_LAST < HDN_FIRST */
10425 if (lpnmhdr->code > HDN_FIRST || lpnmhdr->code < HDN_LAST) return 0;
10426 lpnmh = (NMHEADERW *)lpnmhdr;
10428 if (lpnmh->iItem < 0 || lpnmh->iItem >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return 0;
10430 switch (lpnmhdr->code)
10432 case HDN_TRACKW:
10433 case HDN_TRACKA:
10435 COLUMN_INFO *lpColumnInfo;
10436 POINT ptOrigin;
10437 INT x;
10439 if (!lpnmh->pitem || !(lpnmh->pitem->mask & HDI_WIDTH))
10440 break;
10442 /* remove the old line (if any) */
10443 LISTVIEW_DrawTrackLine(infoPtr);
10445 /* compute & draw the new line */
10446 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, lpnmh->iItem);
10447 x = lpColumnInfo->rcHeader.left + lpnmh->pitem->cxy;
10448 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
10449 infoPtr->xTrackLine = x + ptOrigin.x;
10450 LISTVIEW_DrawTrackLine(infoPtr);
10451 return notify_forward_header(infoPtr, lpnmh);
10454 case HDN_ENDTRACKA:
10455 case HDN_ENDTRACKW:
10456 /* remove the track line (if any) */
10457 LISTVIEW_DrawTrackLine(infoPtr);
10458 infoPtr->xTrackLine = -1;
10459 return notify_forward_header(infoPtr, lpnmh);
10461 case HDN_BEGINDRAG:
10462 if ((infoPtr->dwLvExStyle & LVS_EX_HEADERDRAGDROP) == 0) return 1;
10463 return notify_forward_header(infoPtr, lpnmh);
10465 case HDN_ENDDRAG:
10466 infoPtr->colRectsDirty = TRUE;
10467 LISTVIEW_InvalidateList(infoPtr);
10468 return notify_forward_header(infoPtr, lpnmh);
10470 case HDN_ITEMCHANGEDW:
10471 case HDN_ITEMCHANGEDA:
10473 COLUMN_INFO *lpColumnInfo;
10474 HDITEMW hdi;
10475 INT dx, cxy;
10477 if (!lpnmh->pitem || !(lpnmh->pitem->mask & HDI_WIDTH))
10479 hdi.mask = HDI_WIDTH;
10480 if (!SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, lpnmh->iItem, (LPARAM)&hdi)) return 0;
10481 cxy = hdi.cxy;
10483 else
10484 cxy = lpnmh->pitem->cxy;
10486 /* determine how much we change since the last know position */
10487 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, lpnmh->iItem);
10488 dx = cxy - (lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left);
10489 if (dx != 0)
10491 lpColumnInfo->rcHeader.right += dx;
10493 hdi.mask = HDI_ORDER;
10494 SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, lpnmh->iItem, (LPARAM)&hdi);
10496 /* not the rightmost one */
10497 if (hdi.iOrder + 1 < DPA_GetPtrCount(infoPtr->hdpaColumns))
10499 INT nIndex = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX,
10500 hdi.iOrder + 1, 0);
10501 LISTVIEW_ScrollColumns(infoPtr, nIndex, dx);
10503 else
10505 /* only needs to update the scrolls */
10506 infoPtr->nItemWidth += dx;
10507 LISTVIEW_UpdateScroll(infoPtr);
10509 LISTVIEW_UpdateItemSize(infoPtr);
10510 if (infoPtr->uView == LV_VIEW_DETAILS && is_redrawing(infoPtr))
10512 POINT ptOrigin;
10513 RECT rcCol = lpColumnInfo->rcHeader;
10515 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
10516 OffsetRect(&rcCol, ptOrigin.x, 0);
10518 rcCol.top = infoPtr->rcList.top;
10519 rcCol.bottom = infoPtr->rcList.bottom;
10521 /* resizing left-aligned columns leaves most of the left side untouched */
10522 if ((lpColumnInfo->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_LEFT)
10524 INT nMaxDirty = infoPtr->nEllipsisWidth + infoPtr->ntmMaxCharWidth;
10525 if (dx > 0)
10526 nMaxDirty += dx;
10527 rcCol.left = max (rcCol.left, rcCol.right - nMaxDirty);
10530 /* when shrinking the last column clear the now unused field */
10531 if (hdi.iOrder == DPA_GetPtrCount(infoPtr->hdpaColumns) - 1)
10533 RECT right;
10535 rcCol.right -= dx;
10537 /* deal with right from rightmost column area */
10538 right.left = rcCol.right;
10539 right.top = rcCol.top;
10540 right.bottom = rcCol.bottom;
10541 right.right = infoPtr->rcList.right;
10543 LISTVIEW_InvalidateRect(infoPtr, &right);
10546 LISTVIEW_InvalidateRect(infoPtr, &rcCol);
10549 break;
10552 case HDN_ITEMCLICKW:
10553 case HDN_ITEMCLICKA:
10555 /* Handle sorting by Header Column */
10556 NMLISTVIEW nmlv;
10558 ZeroMemory(&nmlv, sizeof(NMLISTVIEW));
10559 nmlv.iItem = -1;
10560 nmlv.iSubItem = lpnmh->iItem;
10561 notify_listview(infoPtr, LVN_COLUMNCLICK, &nmlv);
10562 return notify_forward_header(infoPtr, lpnmh);
10565 case HDN_DIVIDERDBLCLICKW:
10566 case HDN_DIVIDERDBLCLICKA:
10567 /* FIXME: for LVS_EX_HEADERINALLVIEWS and not LV_VIEW_DETAILS
10568 we should use LVSCW_AUTOSIZE_USEHEADER, helper rework or
10569 split needed for that */
10570 LISTVIEW_SetColumnWidth(infoPtr, lpnmh->iItem, LVSCW_AUTOSIZE);
10571 return notify_forward_header(infoPtr, lpnmh);
10573 return 0;
10576 /***
10577 * DESCRIPTION:
10578 * Paint non-client area of control.
10580 * PARAMETER(S):
10581 * [I] infoPtr : valid pointer to the listview structureof the sender
10582 * [I] region : update region
10584 * RETURN:
10585 * TRUE - frame was painted
10586 * FALSE - call default window proc
10588 static BOOL LISTVIEW_NCPaint(const LISTVIEW_INFO *infoPtr, HRGN region)
10590 HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
10591 HDC dc;
10592 RECT r;
10593 HRGN cliprgn;
10594 int cxEdge = GetSystemMetrics (SM_CXEDGE),
10595 cyEdge = GetSystemMetrics (SM_CYEDGE);
10597 if (!theme)
10598 return DefWindowProcW (infoPtr->hwndSelf, WM_NCPAINT, (WPARAM)region, 0);
10600 GetWindowRect(infoPtr->hwndSelf, &r);
10602 cliprgn = CreateRectRgn (r.left + cxEdge, r.top + cyEdge,
10603 r.right - cxEdge, r.bottom - cyEdge);
10604 if (region != (HRGN)1)
10605 CombineRgn (cliprgn, cliprgn, region, RGN_AND);
10606 OffsetRect(&r, -r.left, -r.top);
10608 dc = GetDCEx(infoPtr->hwndSelf, region, DCX_WINDOW|DCX_INTERSECTRGN);
10609 OffsetRect(&r, -r.left, -r.top);
10611 if (IsThemeBackgroundPartiallyTransparent (theme, 0, 0))
10612 DrawThemeParentBackground(infoPtr->hwndSelf, dc, &r);
10613 DrawThemeBackground (theme, dc, 0, 0, &r, 0);
10614 ReleaseDC(infoPtr->hwndSelf, dc);
10616 /* Call default proc to get the scrollbars etc. painted */
10617 DefWindowProcW (infoPtr->hwndSelf, WM_NCPAINT, (WPARAM)cliprgn, 0);
10619 return FALSE;
10622 /***
10623 * DESCRIPTION:
10624 * Determines the type of structure to use.
10626 * PARAMETER(S):
10627 * [I] infoPtr : valid pointer to the listview structureof the sender
10628 * [I] hwndFrom : listview window handle
10629 * [I] nCommand : command specifying the nature of the WM_NOTIFYFORMAT
10631 * RETURN:
10632 * Zero
10634 static LRESULT LISTVIEW_NotifyFormat(LISTVIEW_INFO *infoPtr, HWND hwndFrom, INT nCommand)
10636 TRACE("(hwndFrom=%p, nCommand=%d)\n", hwndFrom, nCommand);
10638 if (nCommand == NF_REQUERY)
10639 infoPtr->notifyFormat = SendMessageW(infoPtr->hwndNotify, WM_NOTIFYFORMAT, (WPARAM)infoPtr->hwndSelf, NF_QUERY);
10641 return infoPtr->notifyFormat;
10644 /***
10645 * DESCRIPTION:
10646 * Paints/Repaints the listview control. Internal use.
10648 * PARAMETER(S):
10649 * [I] infoPtr : valid pointer to the listview structure
10650 * [I] hdc : device context handle
10652 * RETURN:
10653 * Zero
10655 static LRESULT LISTVIEW_Paint(LISTVIEW_INFO *infoPtr, HDC hdc)
10657 TRACE("(hdc=%p)\n", hdc);
10659 if (infoPtr->bNoItemMetrics && infoPtr->nItemCount)
10661 infoPtr->bNoItemMetrics = FALSE;
10662 LISTVIEW_UpdateItemSize(infoPtr);
10663 if (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON)
10664 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
10665 LISTVIEW_UpdateScroll(infoPtr);
10668 if (infoPtr->hwndHeader) UpdateWindow(infoPtr->hwndHeader);
10670 if (hdc)
10671 LISTVIEW_Refresh(infoPtr, hdc, NULL);
10672 else
10674 PAINTSTRUCT ps;
10676 hdc = BeginPaint(infoPtr->hwndSelf, &ps);
10677 if (!hdc) return 1;
10678 LISTVIEW_Refresh(infoPtr, hdc, ps.fErase ? &ps.rcPaint : NULL);
10679 EndPaint(infoPtr->hwndSelf, &ps);
10682 return 0;
10685 /***
10686 * DESCRIPTION:
10687 * Paints/Repaints the listview control, WM_PAINT handler.
10689 * PARAMETER(S):
10690 * [I] infoPtr : valid pointer to the listview structure
10691 * [I] hdc : device context handle
10693 * RETURN:
10694 * Zero
10696 static inline LRESULT LISTVIEW_WMPaint(LISTVIEW_INFO *infoPtr, HDC hdc)
10698 TRACE("(hdc=%p)\n", hdc);
10700 if (!is_redrawing(infoPtr))
10701 return DefWindowProcW (infoPtr->hwndSelf, WM_PAINT, (WPARAM)hdc, 0);
10703 return LISTVIEW_Paint(infoPtr, hdc);
10706 /***
10707 * DESCRIPTION:
10708 * Paints/Repaints the listview control.
10710 * PARAMETER(S):
10711 * [I] infoPtr : valid pointer to the listview structure
10712 * [I] hdc : device context handle
10713 * [I] options : drawing options
10715 * RETURN:
10716 * Zero
10718 static LRESULT LISTVIEW_PrintClient(LISTVIEW_INFO *infoPtr, HDC hdc, DWORD options)
10720 FIXME("Partial Stub: (hdc=%p options=0x%08x)\n", hdc, options);
10722 if ((options & PRF_CHECKVISIBLE) && !IsWindowVisible(infoPtr->hwndSelf))
10723 return 0;
10725 if (options & PRF_ERASEBKGND)
10726 LISTVIEW_EraseBkgnd(infoPtr, hdc);
10728 if (options & PRF_CLIENT)
10729 LISTVIEW_Paint(infoPtr, hdc);
10731 return 0;
10735 /***
10736 * DESCRIPTION:
10737 * Processes double click messages (right mouse button).
10739 * PARAMETER(S):
10740 * [I] infoPtr : valid pointer to the listview structure
10741 * [I] wKey : key flag
10742 * [I] x,y : mouse coordinate
10744 * RETURN:
10745 * Zero
10747 static LRESULT LISTVIEW_RButtonDblClk(const LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
10749 LVHITTESTINFO lvHitTestInfo;
10751 TRACE("(key=%hu,X=%u,Y=%u)\n", wKey, x, y);
10753 /* send NM_RELEASEDCAPTURE notification */
10754 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
10756 /* send NM_RDBLCLK notification */
10757 lvHitTestInfo.pt.x = x;
10758 lvHitTestInfo.pt.y = y;
10759 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
10760 notify_click(infoPtr, NM_RDBLCLK, &lvHitTestInfo);
10762 return 0;
10765 /***
10766 * DESCRIPTION:
10767 * Processes WM_RBUTTONDOWN message and corresponding drag operation.
10769 * PARAMETER(S):
10770 * [I] infoPtr : valid pointer to the listview structure
10771 * [I] wKey : key flag
10772 * [I] x, y : mouse coordinate
10774 * RETURN:
10775 * Zero
10777 static LRESULT LISTVIEW_RButtonDown(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
10779 LVHITTESTINFO ht;
10780 INT item;
10782 TRACE("(key=%hu, x=%d, y=%d)\n", wKey, x, y);
10784 /* send NM_RELEASEDCAPTURE notification */
10785 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
10787 /* determine the index of the selected item */
10788 ht.pt.x = x;
10789 ht.pt.y = y;
10790 item = LISTVIEW_HitTest(infoPtr, &ht, TRUE, TRUE);
10792 /* make sure the listview control window has the focus */
10793 if (!infoPtr->bFocus) SetFocus(infoPtr->hwndSelf);
10795 if ((item >= 0) && (item < infoPtr->nItemCount))
10797 LISTVIEW_SetItemFocus(infoPtr, item);
10798 if (!((wKey & MK_SHIFT) || (wKey & MK_CONTROL)) &&
10799 !LISTVIEW_GetItemState(infoPtr, item, LVIS_SELECTED))
10800 LISTVIEW_SetSelection(infoPtr, item);
10802 else
10803 LISTVIEW_DeselectAll(infoPtr);
10805 if (LISTVIEW_TrackMouse(infoPtr, ht.pt))
10807 if (ht.iItem != -1)
10809 NMLISTVIEW nmlv;
10811 memset(&nmlv, 0, sizeof(nmlv));
10812 nmlv.iItem = ht.iItem;
10813 nmlv.ptAction = ht.pt;
10815 notify_listview(infoPtr, LVN_BEGINRDRAG, &nmlv);
10818 else
10820 SetFocus(infoPtr->hwndSelf);
10822 ht.pt.x = x;
10823 ht.pt.y = y;
10824 LISTVIEW_HitTest(infoPtr, &ht, TRUE, FALSE);
10826 if (notify_click(infoPtr, NM_RCLICK, &ht))
10828 /* Send a WM_CONTEXTMENU message in response to the WM_RBUTTONUP */
10829 SendMessageW(infoPtr->hwndSelf, WM_CONTEXTMENU,
10830 (WPARAM)infoPtr->hwndSelf, (LPARAM)GetMessagePos());
10834 return 0;
10837 /***
10838 * DESCRIPTION:
10839 * Sets the cursor.
10841 * PARAMETER(S):
10842 * [I] infoPtr : valid pointer to the listview structure
10843 * [I] hwnd : window handle of window containing the cursor
10844 * [I] nHittest : hit-test code
10845 * [I] wMouseMsg : ideintifier of the mouse message
10847 * RETURN:
10848 * TRUE if cursor is set
10849 * FALSE otherwise
10851 static BOOL LISTVIEW_SetCursor(const LISTVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
10853 LVHITTESTINFO lvHitTestInfo;
10855 if (!LISTVIEW_IsHotTracking(infoPtr)) goto forward;
10857 if (!infoPtr->hHotCursor) goto forward;
10859 GetCursorPos(&lvHitTestInfo.pt);
10860 if (LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, FALSE, FALSE) < 0) goto forward;
10862 SetCursor(infoPtr->hHotCursor);
10864 return TRUE;
10866 forward:
10868 return DefWindowProcW(infoPtr->hwndSelf, WM_SETCURSOR, wParam, lParam);
10871 /***
10872 * DESCRIPTION:
10873 * Sets the focus.
10875 * PARAMETER(S):
10876 * [I] infoPtr : valid pointer to the listview structure
10877 * [I] hwndLoseFocus : handle of previously focused window
10879 * RETURN:
10880 * Zero
10882 static LRESULT LISTVIEW_SetFocus(LISTVIEW_INFO *infoPtr, HWND hwndLoseFocus)
10884 TRACE("(hwndLoseFocus=%p)\n", hwndLoseFocus);
10886 /* if we have the focus already, there's nothing to do */
10887 if (infoPtr->bFocus) return 0;
10889 /* send NM_SETFOCUS notification */
10890 if (!notify(infoPtr, NM_SETFOCUS)) return 0;
10892 /* set window focus flag */
10893 infoPtr->bFocus = TRUE;
10895 /* put the focus rect back on */
10896 LISTVIEW_ShowFocusRect(infoPtr, TRUE);
10898 /* redraw all visible selected items */
10899 LISTVIEW_InvalidateSelectedItems(infoPtr);
10901 return 0;
10904 /***
10905 * DESCRIPTION:
10906 * Sets the font.
10908 * PARAMETER(S):
10909 * [I] infoPtr : valid pointer to the listview structure
10910 * [I] fRedraw : font handle
10911 * [I] fRedraw : redraw flag
10913 * RETURN:
10914 * Zero
10916 static LRESULT LISTVIEW_SetFont(LISTVIEW_INFO *infoPtr, HFONT hFont, WORD fRedraw)
10918 HFONT oldFont = infoPtr->hFont;
10919 INT oldHeight = infoPtr->nItemHeight;
10921 TRACE("(hfont=%p,redraw=%hu)\n", hFont, fRedraw);
10923 infoPtr->hFont = hFont ? hFont : infoPtr->hDefaultFont;
10924 if (infoPtr->hFont == oldFont) return 0;
10926 LISTVIEW_SaveTextMetrics(infoPtr);
10928 infoPtr->nItemHeight = LISTVIEW_CalculateItemHeight(infoPtr);
10930 if (infoPtr->uView == LV_VIEW_DETAILS)
10932 SendMessageW(infoPtr->hwndHeader, WM_SETFONT, (WPARAM)hFont, MAKELPARAM(fRedraw, 0));
10933 LISTVIEW_UpdateSize(infoPtr);
10934 LISTVIEW_UpdateScroll(infoPtr);
10936 else if (infoPtr->nItemHeight != oldHeight)
10937 LISTVIEW_UpdateScroll(infoPtr);
10939 if (fRedraw) LISTVIEW_InvalidateList(infoPtr);
10941 return 0;
10944 /***
10945 * DESCRIPTION:
10946 * Message handling for WM_SETREDRAW.
10947 * For the Listview, it invalidates the entire window (the doc specifies otherwise)
10949 * PARAMETER(S):
10950 * [I] infoPtr : valid pointer to the listview structure
10951 * [I] bRedraw: state of redraw flag
10953 * RETURN:
10954 * DefWinProc return value
10956 static LRESULT LISTVIEW_SetRedraw(LISTVIEW_INFO *infoPtr, BOOL bRedraw)
10958 TRACE("infoPtr->bRedraw=%d, bRedraw=%d\n", infoPtr->bRedraw, bRedraw);
10960 /* we cannot use straight equality here because _any_ non-zero value is TRUE */
10961 if ((infoPtr->bRedraw && bRedraw) || (!infoPtr->bRedraw && !bRedraw)) return 0;
10963 infoPtr->bRedraw = bRedraw;
10965 if(!bRedraw) return 0;
10967 if (is_autoarrange(infoPtr))
10968 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
10969 LISTVIEW_UpdateScroll(infoPtr);
10971 /* despite what the WM_SETREDRAW docs says, apps expect us
10972 * to invalidate the listview here... stupid! */
10973 LISTVIEW_InvalidateList(infoPtr);
10975 return 0;
10978 /***
10979 * DESCRIPTION:
10980 * Resizes the listview control. This function processes WM_SIZE
10981 * messages. At this time, the width and height are not used.
10983 * PARAMETER(S):
10984 * [I] infoPtr : valid pointer to the listview structure
10985 * [I] Width : new width
10986 * [I] Height : new height
10988 * RETURN:
10989 * Zero
10991 static LRESULT LISTVIEW_Size(LISTVIEW_INFO *infoPtr, int Width, int Height)
10993 RECT rcOld = infoPtr->rcList;
10995 TRACE("(width=%d, height=%d)\n", Width, Height);
10997 LISTVIEW_UpdateSize(infoPtr);
10998 if (EqualRect(&rcOld, &infoPtr->rcList)) return 0;
11000 /* do not bother with display related stuff if we're not redrawing */
11001 if (!is_redrawing(infoPtr)) return 0;
11003 if (is_autoarrange(infoPtr))
11004 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
11006 LISTVIEW_UpdateScroll(infoPtr);
11008 /* refresh all only for lists whose height changed significantly */
11009 if ((infoPtr->uView == LV_VIEW_LIST) &&
11010 (rcOld.bottom - rcOld.top) / infoPtr->nItemHeight !=
11011 (infoPtr->rcList.bottom - infoPtr->rcList.top) / infoPtr->nItemHeight)
11012 LISTVIEW_InvalidateList(infoPtr);
11014 return 0;
11017 /***
11018 * DESCRIPTION:
11019 * Sets the size information.
11021 * PARAMETER(S):
11022 * [I] infoPtr : valid pointer to the listview structure
11024 * RETURN:
11025 * None
11027 static void LISTVIEW_UpdateSize(LISTVIEW_INFO *infoPtr)
11029 TRACE("uView=%d, rcList(old)=%s\n", infoPtr->uView, wine_dbgstr_rect(&infoPtr->rcList));
11031 GetClientRect(infoPtr->hwndSelf, &infoPtr->rcList);
11033 if (infoPtr->uView == LV_VIEW_LIST)
11035 /* Apparently the "LIST" style is supposed to have the same
11036 * number of items in a column even if there is no scroll bar.
11037 * Since if a scroll bar already exists then the bottom is already
11038 * reduced, only reduce if the scroll bar does not currently exist.
11039 * The "2" is there to mimic the native control. I think it may be
11040 * related to either padding or edges. (GLA 7/2002)
11042 if (!(GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE) & WS_HSCROLL))
11043 infoPtr->rcList.bottom -= GetSystemMetrics(SM_CYHSCROLL);
11044 infoPtr->rcList.bottom = max (infoPtr->rcList.bottom - 2, 0);
11047 /* if control created invisible header isn't created */
11048 if (infoPtr->hwndHeader)
11050 HDLAYOUT hl;
11051 WINDOWPOS wp;
11053 hl.prc = &infoPtr->rcList;
11054 hl.pwpos = &wp;
11055 SendMessageW( infoPtr->hwndHeader, HDM_LAYOUT, 0, (LPARAM)&hl );
11056 TRACE(" wp.flags=0x%08x, wp=%d,%d (%dx%d)\n", wp.flags, wp.x, wp.y, wp.cx, wp.cy);
11058 if (LISTVIEW_IsHeaderEnabled(infoPtr))
11059 wp.flags |= SWP_SHOWWINDOW;
11060 else
11062 wp.flags |= SWP_HIDEWINDOW;
11063 wp.cy = 0;
11066 SetWindowPos(wp.hwnd, wp.hwndInsertAfter, wp.x, wp.y, wp.cx, wp.cy, wp.flags);
11067 TRACE(" after SWP wp=%d,%d (%dx%d)\n", wp.x, wp.y, wp.cx, wp.cy);
11069 infoPtr->rcList.top = max(wp.cy, 0);
11071 /* extra padding for grid */
11072 if (infoPtr->uView == LV_VIEW_DETAILS && infoPtr->dwLvExStyle & LVS_EX_GRIDLINES)
11073 infoPtr->rcList.top += 2;
11075 TRACE(" rcList=%s\n", wine_dbgstr_rect(&infoPtr->rcList));
11078 /***
11079 * DESCRIPTION:
11080 * Processes WM_STYLECHANGED messages.
11082 * PARAMETER(S):
11083 * [I] infoPtr : valid pointer to the listview structure
11084 * [I] wStyleType : window style type (normal or extended)
11085 * [I] lpss : window style information
11087 * RETURN:
11088 * Zero
11090 static INT LISTVIEW_StyleChanged(LISTVIEW_INFO *infoPtr, WPARAM wStyleType,
11091 const STYLESTRUCT *lpss)
11093 UINT uNewView = lpss->styleNew & LVS_TYPEMASK;
11094 UINT uOldView = lpss->styleOld & LVS_TYPEMASK;
11095 UINT style;
11097 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
11098 wStyleType, lpss->styleOld, lpss->styleNew);
11100 if (wStyleType != GWL_STYLE) return 0;
11102 infoPtr->dwStyle = lpss->styleNew;
11104 if (((lpss->styleOld & WS_HSCROLL) != 0)&&
11105 ((lpss->styleNew & WS_HSCROLL) == 0))
11106 ShowScrollBar(infoPtr->hwndSelf, SB_HORZ, FALSE);
11108 if (((lpss->styleOld & WS_VSCROLL) != 0)&&
11109 ((lpss->styleNew & WS_VSCROLL) == 0))
11110 ShowScrollBar(infoPtr->hwndSelf, SB_VERT, FALSE);
11112 if (uNewView != uOldView)
11114 HIMAGELIST himl;
11116 /* LVM_SETVIEW doesn't change window style bits within LVS_TYPEMASK,
11117 changing style updates current view only when view bits change. */
11118 map_style_view(infoPtr);
11119 SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
11120 ShowWindow(infoPtr->hwndHeader, SW_HIDE);
11122 ShowScrollBar(infoPtr->hwndSelf, SB_BOTH, FALSE);
11123 SetRectEmpty(&infoPtr->rcFocus);
11125 himl = (uNewView == LVS_ICON ? infoPtr->himlNormal : infoPtr->himlSmall);
11126 set_icon_size(&infoPtr->iconSize, himl, uNewView != LVS_ICON);
11128 if (uNewView == LVS_REPORT)
11130 HDLAYOUT hl;
11131 WINDOWPOS wp;
11133 LISTVIEW_CreateHeader( infoPtr );
11135 hl.prc = &infoPtr->rcList;
11136 hl.pwpos = &wp;
11137 SendMessageW( infoPtr->hwndHeader, HDM_LAYOUT, 0, (LPARAM)&hl );
11138 SetWindowPos(infoPtr->hwndHeader, infoPtr->hwndSelf, wp.x, wp.y, wp.cx, wp.cy,
11139 wp.flags | ((infoPtr->dwStyle & LVS_NOCOLUMNHEADER)
11140 ? SWP_HIDEWINDOW : SWP_SHOWWINDOW));
11143 LISTVIEW_UpdateItemSize(infoPtr);
11146 if (uNewView == LVS_REPORT || infoPtr->dwLvExStyle & LVS_EX_HEADERINALLVIEWS)
11148 if ((lpss->styleOld ^ lpss->styleNew) & LVS_NOCOLUMNHEADER)
11150 if (lpss->styleNew & LVS_NOCOLUMNHEADER)
11152 /* Turn off the header control */
11153 style = GetWindowLongW(infoPtr->hwndHeader, GWL_STYLE);
11154 TRACE("Hide header control, was 0x%08x\n", style);
11155 SetWindowLongW(infoPtr->hwndHeader, GWL_STYLE, style | HDS_HIDDEN);
11156 } else {
11157 /* Turn on the header control */
11158 if ((style = GetWindowLongW(infoPtr->hwndHeader, GWL_STYLE)) & HDS_HIDDEN)
11160 TRACE("Show header control, was 0x%08x\n", style);
11161 SetWindowLongW(infoPtr->hwndHeader, GWL_STYLE, (style & ~HDS_HIDDEN) | WS_VISIBLE);
11167 if ( (uNewView == LVS_ICON || uNewView == LVS_SMALLICON) &&
11168 (uNewView != uOldView || ((lpss->styleNew ^ lpss->styleOld) & LVS_ALIGNMASK)) )
11169 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
11171 /* update the size of the client area */
11172 LISTVIEW_UpdateSize(infoPtr);
11174 /* add scrollbars if needed */
11175 LISTVIEW_UpdateScroll(infoPtr);
11177 /* invalidate client area + erase background */
11178 LISTVIEW_InvalidateList(infoPtr);
11180 return 0;
11183 /***
11184 * DESCRIPTION:
11185 * Processes WM_STYLECHANGING messages.
11187 * PARAMETER(S):
11188 * [I] wStyleType : window style type (normal or extended)
11189 * [I0] lpss : window style information
11191 * RETURN:
11192 * Zero
11194 static INT LISTVIEW_StyleChanging(WPARAM wStyleType,
11195 STYLESTRUCT *lpss)
11197 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
11198 wStyleType, lpss->styleOld, lpss->styleNew);
11200 /* don't forward LVS_OWNERDATA only if not already set to */
11201 if ((lpss->styleNew ^ lpss->styleOld) & LVS_OWNERDATA)
11203 if (lpss->styleOld & LVS_OWNERDATA)
11204 lpss->styleNew |= LVS_OWNERDATA;
11205 else
11206 lpss->styleNew &= ~LVS_OWNERDATA;
11209 return 0;
11212 /***
11213 * DESCRIPTION:
11214 * Processes WM_SHOWWINDOW messages.
11216 * PARAMETER(S):
11217 * [I] infoPtr : valid pointer to the listview structure
11218 * [I] bShown : window is being shown (FALSE when hidden)
11219 * [I] iStatus : window show status
11221 * RETURN:
11222 * Zero
11224 static LRESULT LISTVIEW_ShowWindow(LISTVIEW_INFO *infoPtr, WPARAM bShown, LPARAM iStatus)
11226 /* header delayed creation */
11227 if ((infoPtr->uView == LV_VIEW_DETAILS) && bShown)
11229 LISTVIEW_CreateHeader(infoPtr);
11231 if (!(LVS_NOCOLUMNHEADER & infoPtr->dwStyle))
11232 ShowWindow(infoPtr->hwndHeader, SW_SHOWNORMAL);
11235 return DefWindowProcW(infoPtr->hwndSelf, WM_SHOWWINDOW, bShown, iStatus);
11238 /***
11239 * DESCRIPTION:
11240 * Processes CCM_GETVERSION messages.
11242 * PARAMETER(S):
11243 * [I] infoPtr : valid pointer to the listview structure
11245 * RETURN:
11246 * Current version
11248 static inline LRESULT LISTVIEW_GetVersion(const LISTVIEW_INFO *infoPtr)
11250 return infoPtr->iVersion;
11253 /***
11254 * DESCRIPTION:
11255 * Processes CCM_SETVERSION messages.
11257 * PARAMETER(S):
11258 * [I] infoPtr : valid pointer to the listview structure
11259 * [I] iVersion : version to be set
11261 * RETURN:
11262 * -1 when requested version is greater than DLL version;
11263 * previous version otherwise
11265 static LRESULT LISTVIEW_SetVersion(LISTVIEW_INFO *infoPtr, DWORD iVersion)
11267 INT iOldVersion = infoPtr->iVersion;
11269 if (iVersion > COMCTL32_VERSION)
11270 return -1;
11272 infoPtr->iVersion = iVersion;
11274 TRACE("new version %d\n", iVersion);
11276 return iOldVersion;
11279 /***
11280 * DESCRIPTION:
11281 * Window procedure of the listview control.
11284 static LRESULT WINAPI
11285 LISTVIEW_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
11287 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongPtrW(hwnd, 0);
11289 TRACE("(hwnd=%p uMsg=%x wParam=%lx lParam=%lx)\n", hwnd, uMsg, wParam, lParam);
11291 if (!infoPtr && (uMsg != WM_NCCREATE))
11292 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
11294 switch (uMsg)
11296 case LVM_APPROXIMATEVIEWRECT:
11297 return LISTVIEW_ApproximateViewRect(infoPtr, (INT)wParam,
11298 LOWORD(lParam), HIWORD(lParam));
11299 case LVM_ARRANGE:
11300 return LISTVIEW_Arrange(infoPtr, (INT)wParam);
11302 case LVM_CANCELEDITLABEL:
11303 return LISTVIEW_CancelEditLabel(infoPtr);
11305 case LVM_CREATEDRAGIMAGE:
11306 return (LRESULT)LISTVIEW_CreateDragImage(infoPtr, (INT)wParam, (LPPOINT)lParam);
11308 case LVM_DELETEALLITEMS:
11309 return LISTVIEW_DeleteAllItems(infoPtr, FALSE);
11311 case LVM_DELETECOLUMN:
11312 return LISTVIEW_DeleteColumn(infoPtr, (INT)wParam);
11314 case LVM_DELETEITEM:
11315 return LISTVIEW_DeleteItem(infoPtr, (INT)wParam);
11317 case LVM_EDITLABELA:
11318 case LVM_EDITLABELW:
11319 return (LRESULT)LISTVIEW_EditLabelT(infoPtr, (INT)wParam,
11320 uMsg == LVM_EDITLABELW);
11321 /* case LVM_ENABLEGROUPVIEW: */
11323 case LVM_ENSUREVISIBLE:
11324 return LISTVIEW_EnsureVisible(infoPtr, (INT)wParam, (BOOL)lParam);
11326 case LVM_FINDITEMW:
11327 return LISTVIEW_FindItemW(infoPtr, (INT)wParam, (LPLVFINDINFOW)lParam);
11329 case LVM_FINDITEMA:
11330 return LISTVIEW_FindItemA(infoPtr, (INT)wParam, (LPLVFINDINFOA)lParam);
11332 case LVM_GETBKCOLOR:
11333 return infoPtr->clrBk;
11335 /* case LVM_GETBKIMAGE: */
11337 case LVM_GETCALLBACKMASK:
11338 return infoPtr->uCallbackMask;
11340 case LVM_GETCOLUMNA:
11341 case LVM_GETCOLUMNW:
11342 return LISTVIEW_GetColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam,
11343 uMsg == LVM_GETCOLUMNW);
11345 case LVM_GETCOLUMNORDERARRAY:
11346 return LISTVIEW_GetColumnOrderArray(infoPtr, (INT)wParam, (LPINT)lParam);
11348 case LVM_GETCOLUMNWIDTH:
11349 return LISTVIEW_GetColumnWidth(infoPtr, (INT)wParam);
11351 case LVM_GETCOUNTPERPAGE:
11352 return LISTVIEW_GetCountPerPage(infoPtr);
11354 case LVM_GETEDITCONTROL:
11355 return (LRESULT)infoPtr->hwndEdit;
11357 case LVM_GETEXTENDEDLISTVIEWSTYLE:
11358 return infoPtr->dwLvExStyle;
11360 /* case LVM_GETGROUPINFO: */
11362 /* case LVM_GETGROUPMETRICS: */
11364 case LVM_GETHEADER:
11365 return (LRESULT)infoPtr->hwndHeader;
11367 case LVM_GETHOTCURSOR:
11368 return (LRESULT)infoPtr->hHotCursor;
11370 case LVM_GETHOTITEM:
11371 return infoPtr->nHotItem;
11373 case LVM_GETHOVERTIME:
11374 return infoPtr->dwHoverTime;
11376 case LVM_GETIMAGELIST:
11377 return (LRESULT)LISTVIEW_GetImageList(infoPtr, (INT)wParam);
11379 /* case LVM_GETINSERTMARK: */
11381 /* case LVM_GETINSERTMARKCOLOR: */
11383 /* case LVM_GETINSERTMARKRECT: */
11385 case LVM_GETISEARCHSTRINGA:
11386 case LVM_GETISEARCHSTRINGW:
11387 FIXME("LVM_GETISEARCHSTRING: unimplemented\n");
11388 return FALSE;
11390 case LVM_GETITEMA:
11391 case LVM_GETITEMW:
11392 return LISTVIEW_GetItemExtT(infoPtr, (LPLVITEMW)lParam, uMsg == LVM_GETITEMW);
11394 case LVM_GETITEMCOUNT:
11395 return infoPtr->nItemCount;
11397 case LVM_GETITEMPOSITION:
11398 return LISTVIEW_GetItemPosition(infoPtr, (INT)wParam, (LPPOINT)lParam);
11400 case LVM_GETITEMRECT:
11401 return LISTVIEW_GetItemRect(infoPtr, (INT)wParam, (LPRECT)lParam);
11403 case LVM_GETITEMSPACING:
11404 return LISTVIEW_GetItemSpacing(infoPtr, (BOOL)wParam);
11406 case LVM_GETITEMSTATE:
11407 return LISTVIEW_GetItemState(infoPtr, (INT)wParam, (UINT)lParam);
11409 case LVM_GETITEMTEXTA:
11410 case LVM_GETITEMTEXTW:
11411 return LISTVIEW_GetItemTextT(infoPtr, (INT)wParam, (LPLVITEMW)lParam,
11412 uMsg == LVM_GETITEMTEXTW);
11414 case LVM_GETNEXTITEM:
11415 return LISTVIEW_GetNextItem(infoPtr, (INT)wParam, LOWORD(lParam));
11417 case LVM_GETNUMBEROFWORKAREAS:
11418 FIXME("LVM_GETNUMBEROFWORKAREAS: unimplemented\n");
11419 return 1;
11421 case LVM_GETORIGIN:
11422 if (!lParam) return FALSE;
11423 if (infoPtr->uView == LV_VIEW_DETAILS ||
11424 infoPtr->uView == LV_VIEW_LIST) return FALSE;
11425 LISTVIEW_GetOrigin(infoPtr, (LPPOINT)lParam);
11426 return TRUE;
11428 /* case LVM_GETOUTLINECOLOR: */
11430 /* case LVM_GETSELECTEDCOLUMN: */
11432 case LVM_GETSELECTEDCOUNT:
11433 return LISTVIEW_GetSelectedCount(infoPtr);
11435 case LVM_GETSELECTIONMARK:
11436 return infoPtr->nSelectionMark;
11438 case LVM_GETSTRINGWIDTHA:
11439 case LVM_GETSTRINGWIDTHW:
11440 return LISTVIEW_GetStringWidthT(infoPtr, (LPCWSTR)lParam,
11441 uMsg == LVM_GETSTRINGWIDTHW);
11443 case LVM_GETSUBITEMRECT:
11444 return LISTVIEW_GetSubItemRect(infoPtr, (UINT)wParam, (LPRECT)lParam);
11446 case LVM_GETTEXTBKCOLOR:
11447 return infoPtr->clrTextBk;
11449 case LVM_GETTEXTCOLOR:
11450 return infoPtr->clrText;
11452 /* case LVM_GETTILEINFO: */
11454 /* case LVM_GETTILEVIEWINFO: */
11456 case LVM_GETTOOLTIPS:
11457 if( !infoPtr->hwndToolTip )
11458 infoPtr->hwndToolTip = COMCTL32_CreateToolTip( hwnd );
11459 return (LRESULT)infoPtr->hwndToolTip;
11461 case LVM_GETTOPINDEX:
11462 return LISTVIEW_GetTopIndex(infoPtr);
11464 case LVM_GETUNICODEFORMAT:
11465 return (infoPtr->notifyFormat == NFR_UNICODE);
11467 case LVM_GETVIEW:
11468 return infoPtr->uView;
11470 case LVM_GETVIEWRECT:
11471 return LISTVIEW_GetViewRect(infoPtr, (LPRECT)lParam);
11473 case LVM_GETWORKAREAS:
11474 FIXME("LVM_GETWORKAREAS: unimplemented\n");
11475 return FALSE;
11477 /* case LVM_HASGROUP: */
11479 case LVM_HITTEST:
11480 return LISTVIEW_HitTest(infoPtr, (LPLVHITTESTINFO)lParam, FALSE, TRUE);
11482 case LVM_INSERTCOLUMNA:
11483 case LVM_INSERTCOLUMNW:
11484 return LISTVIEW_InsertColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam,
11485 uMsg == LVM_INSERTCOLUMNW);
11487 /* case LVM_INSERTGROUP: */
11489 /* case LVM_INSERTGROUPSORTED: */
11491 case LVM_INSERTITEMA:
11492 case LVM_INSERTITEMW:
11493 return LISTVIEW_InsertItemT(infoPtr, (LPLVITEMW)lParam, uMsg == LVM_INSERTITEMW);
11495 /* case LVM_INSERTMARKHITTEST: */
11497 /* case LVM_ISGROUPVIEWENABLED: */
11499 case LVM_ISITEMVISIBLE:
11500 return LISTVIEW_IsItemVisible(infoPtr, (INT)wParam);
11502 case LVM_MAPIDTOINDEX:
11503 return LISTVIEW_MapIdToIndex(infoPtr, (UINT)wParam);
11505 case LVM_MAPINDEXTOID:
11506 return LISTVIEW_MapIndexToId(infoPtr, (INT)wParam);
11508 /* case LVM_MOVEGROUP: */
11510 /* case LVM_MOVEITEMTOGROUP: */
11512 case LVM_REDRAWITEMS:
11513 return LISTVIEW_RedrawItems(infoPtr, (INT)wParam, (INT)lParam);
11515 /* case LVM_REMOVEALLGROUPS: */
11517 /* case LVM_REMOVEGROUP: */
11519 case LVM_SCROLL:
11520 return LISTVIEW_Scroll(infoPtr, (INT)wParam, (INT)lParam);
11522 case LVM_SETBKCOLOR:
11523 return LISTVIEW_SetBkColor(infoPtr, (COLORREF)lParam);
11525 /* case LVM_SETBKIMAGE: */
11527 case LVM_SETCALLBACKMASK:
11528 infoPtr->uCallbackMask = (UINT)wParam;
11529 return TRUE;
11531 case LVM_SETCOLUMNA:
11532 case LVM_SETCOLUMNW:
11533 return LISTVIEW_SetColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam,
11534 uMsg == LVM_SETCOLUMNW);
11536 case LVM_SETCOLUMNORDERARRAY:
11537 return LISTVIEW_SetColumnOrderArray(infoPtr, (INT)wParam, (LPINT)lParam);
11539 case LVM_SETCOLUMNWIDTH:
11540 return LISTVIEW_SetColumnWidth(infoPtr, (INT)wParam, (short)LOWORD(lParam));
11542 case LVM_SETEXTENDEDLISTVIEWSTYLE:
11543 return LISTVIEW_SetExtendedListViewStyle(infoPtr, (DWORD)wParam, (DWORD)lParam);
11545 /* case LVM_SETGROUPINFO: */
11547 /* case LVM_SETGROUPMETRICS: */
11549 case LVM_SETHOTCURSOR:
11550 return (LRESULT)LISTVIEW_SetHotCursor(infoPtr, (HCURSOR)lParam);
11552 case LVM_SETHOTITEM:
11553 return LISTVIEW_SetHotItem(infoPtr, (INT)wParam);
11555 case LVM_SETHOVERTIME:
11556 return LISTVIEW_SetHoverTime(infoPtr, (DWORD)lParam);
11558 case LVM_SETICONSPACING:
11559 if(lParam == -1)
11560 return LISTVIEW_SetIconSpacing(infoPtr, -1, -1);
11561 return LISTVIEW_SetIconSpacing(infoPtr, LOWORD(lParam), HIWORD(lParam));
11563 case LVM_SETIMAGELIST:
11564 return (LRESULT)LISTVIEW_SetImageList(infoPtr, (INT)wParam, (HIMAGELIST)lParam);
11566 /* case LVM_SETINFOTIP: */
11568 /* case LVM_SETINSERTMARK: */
11570 /* case LVM_SETINSERTMARKCOLOR: */
11572 case LVM_SETITEMA:
11573 case LVM_SETITEMW:
11575 if (infoPtr->dwStyle & LVS_OWNERDATA) return FALSE;
11576 return LISTVIEW_SetItemT(infoPtr, (LPLVITEMW)lParam, (uMsg == LVM_SETITEMW));
11579 case LVM_SETITEMCOUNT:
11580 return LISTVIEW_SetItemCount(infoPtr, (INT)wParam, (DWORD)lParam);
11582 case LVM_SETITEMPOSITION:
11584 POINT pt;
11585 pt.x = (short)LOWORD(lParam);
11586 pt.y = (short)HIWORD(lParam);
11587 return LISTVIEW_SetItemPosition(infoPtr, (INT)wParam, &pt);
11590 case LVM_SETITEMPOSITION32:
11591 return LISTVIEW_SetItemPosition(infoPtr, (INT)wParam, (POINT*)lParam);
11593 case LVM_SETITEMSTATE:
11594 return LISTVIEW_SetItemState(infoPtr, (INT)wParam, (LPLVITEMW)lParam);
11596 case LVM_SETITEMTEXTA:
11597 case LVM_SETITEMTEXTW:
11598 return LISTVIEW_SetItemTextT(infoPtr, (INT)wParam, (LPLVITEMW)lParam,
11599 uMsg == LVM_SETITEMTEXTW);
11601 /* case LVM_SETOUTLINECOLOR: */
11603 /* case LVM_SETSELECTEDCOLUMN: */
11605 case LVM_SETSELECTIONMARK:
11606 return LISTVIEW_SetSelectionMark(infoPtr, (INT)lParam);
11608 case LVM_SETTEXTBKCOLOR:
11609 return LISTVIEW_SetTextBkColor(infoPtr, (COLORREF)lParam);
11611 case LVM_SETTEXTCOLOR:
11612 return LISTVIEW_SetTextColor(infoPtr, (COLORREF)lParam);
11614 /* case LVM_SETTILEINFO: */
11616 /* case LVM_SETTILEVIEWINFO: */
11618 /* case LVM_SETTILEWIDTH: */
11620 case LVM_SETTOOLTIPS:
11621 return (LRESULT)LISTVIEW_SetToolTips(infoPtr, (HWND)lParam);
11623 case LVM_SETUNICODEFORMAT:
11624 return LISTVIEW_SetUnicodeFormat(infoPtr, wParam);
11626 case LVM_SETVIEW:
11627 return LISTVIEW_SetView(infoPtr, wParam);
11629 /* case LVM_SETWORKAREAS: */
11631 /* case LVM_SORTGROUPS: */
11633 case LVM_SORTITEMS:
11634 case LVM_SORTITEMSEX:
11635 return LISTVIEW_SortItems(infoPtr, (PFNLVCOMPARE)lParam, wParam,
11636 uMsg == LVM_SORTITEMSEX);
11637 case LVM_SUBITEMHITTEST:
11638 return LISTVIEW_HitTest(infoPtr, (LPLVHITTESTINFO)lParam, TRUE, FALSE);
11640 case LVM_UPDATE:
11641 return LISTVIEW_Update(infoPtr, (INT)wParam);
11643 case CCM_GETVERSION:
11644 return LISTVIEW_GetVersion(infoPtr);
11646 case CCM_SETVERSION:
11647 return LISTVIEW_SetVersion(infoPtr, wParam);
11649 case WM_CHAR:
11650 return LISTVIEW_ProcessLetterKeys( infoPtr, wParam, lParam );
11652 case WM_COMMAND:
11653 return LISTVIEW_Command(infoPtr, wParam, lParam);
11655 case WM_NCCREATE:
11656 return LISTVIEW_NCCreate(hwnd, (LPCREATESTRUCTW)lParam);
11658 case WM_CREATE:
11659 return LISTVIEW_Create(hwnd, (LPCREATESTRUCTW)lParam);
11661 case WM_DESTROY:
11662 return LISTVIEW_Destroy(infoPtr);
11664 case WM_ENABLE:
11665 return LISTVIEW_Enable(infoPtr);
11667 case WM_ERASEBKGND:
11668 return LISTVIEW_EraseBkgnd(infoPtr, (HDC)wParam);
11670 case WM_GETDLGCODE:
11671 return DLGC_WANTCHARS | DLGC_WANTARROWS;
11673 case WM_GETFONT:
11674 return (LRESULT)infoPtr->hFont;
11676 case WM_HSCROLL:
11677 return LISTVIEW_HScroll(infoPtr, (INT)LOWORD(wParam), 0);
11679 case WM_KEYDOWN:
11680 return LISTVIEW_KeyDown(infoPtr, (INT)wParam, (LONG)lParam);
11682 case WM_KILLFOCUS:
11683 return LISTVIEW_KillFocus(infoPtr);
11685 case WM_LBUTTONDBLCLK:
11686 return LISTVIEW_LButtonDblClk(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11688 case WM_LBUTTONDOWN:
11689 return LISTVIEW_LButtonDown(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11691 case WM_LBUTTONUP:
11692 return LISTVIEW_LButtonUp(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11694 case WM_MOUSEMOVE:
11695 return LISTVIEW_MouseMove (infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11697 case WM_MOUSEHOVER:
11698 return LISTVIEW_MouseHover(infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11700 case WM_NCDESTROY:
11701 return LISTVIEW_NCDestroy(infoPtr);
11703 case WM_NCPAINT:
11704 return LISTVIEW_NCPaint(infoPtr, (HRGN)wParam);
11706 case WM_NOTIFY:
11707 return LISTVIEW_Notify(infoPtr, (LPNMHDR)lParam);
11709 case WM_NOTIFYFORMAT:
11710 return LISTVIEW_NotifyFormat(infoPtr, (HWND)wParam, (INT)lParam);
11712 case WM_PRINTCLIENT:
11713 return LISTVIEW_PrintClient(infoPtr, (HDC)wParam, (DWORD)lParam);
11715 case WM_PAINT:
11716 return LISTVIEW_WMPaint(infoPtr, (HDC)wParam);
11718 case WM_RBUTTONDBLCLK:
11719 return LISTVIEW_RButtonDblClk(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11721 case WM_RBUTTONDOWN:
11722 return LISTVIEW_RButtonDown(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11724 case WM_SETCURSOR:
11725 return LISTVIEW_SetCursor(infoPtr, wParam, lParam);
11727 case WM_SETFOCUS:
11728 return LISTVIEW_SetFocus(infoPtr, (HWND)wParam);
11730 case WM_SETFONT:
11731 return LISTVIEW_SetFont(infoPtr, (HFONT)wParam, (WORD)lParam);
11733 case WM_SETREDRAW:
11734 return LISTVIEW_SetRedraw(infoPtr, (BOOL)wParam);
11736 case WM_SHOWWINDOW:
11737 return LISTVIEW_ShowWindow(infoPtr, wParam, lParam);
11739 case WM_STYLECHANGED:
11740 return LISTVIEW_StyleChanged(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
11742 case WM_STYLECHANGING:
11743 return LISTVIEW_StyleChanging(wParam, (LPSTYLESTRUCT)lParam);
11745 case WM_SYSCOLORCHANGE:
11746 COMCTL32_RefreshSysColors();
11747 return 0;
11749 /* case WM_TIMER: */
11750 case WM_THEMECHANGED:
11751 return LISTVIEW_ThemeChanged(infoPtr);
11753 case WM_VSCROLL:
11754 return LISTVIEW_VScroll(infoPtr, (INT)LOWORD(wParam), 0);
11756 case WM_MOUSEWHEEL:
11757 if (wParam & (MK_SHIFT | MK_CONTROL))
11758 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
11759 return LISTVIEW_MouseWheel(infoPtr, (short int)HIWORD(wParam));
11761 case WM_WINDOWPOSCHANGED:
11762 if (!(((WINDOWPOS *)lParam)->flags & SWP_NOSIZE))
11764 SetWindowPos(infoPtr->hwndSelf, 0, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOACTIVATE |
11765 SWP_NOZORDER | SWP_NOMOVE | SWP_NOSIZE);
11767 if ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (infoPtr->uView == LV_VIEW_DETAILS))
11769 if (notify_measureitem(infoPtr)) LISTVIEW_InvalidateList(infoPtr);
11771 LISTVIEW_Size(infoPtr, ((WINDOWPOS *)lParam)->cx, ((WINDOWPOS *)lParam)->cy);
11773 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
11775 /* case WM_WININICHANGE: */
11777 default:
11778 if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
11779 ERR("unknown msg %04x wp=%08lx lp=%08lx\n", uMsg, wParam, lParam);
11781 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
11786 /***
11787 * DESCRIPTION:
11788 * Registers the window class.
11790 * PARAMETER(S):
11791 * None
11793 * RETURN:
11794 * None
11796 void LISTVIEW_Register(void)
11798 WNDCLASSW wndClass;
11800 ZeroMemory(&wndClass, sizeof(WNDCLASSW));
11801 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS;
11802 wndClass.lpfnWndProc = LISTVIEW_WindowProc;
11803 wndClass.cbClsExtra = 0;
11804 wndClass.cbWndExtra = sizeof(LISTVIEW_INFO *);
11805 wndClass.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
11806 wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
11807 wndClass.lpszClassName = WC_LISTVIEWW;
11808 RegisterClassW(&wndClass);
11811 /***
11812 * DESCRIPTION:
11813 * Unregisters the window class.
11815 * PARAMETER(S):
11816 * None
11818 * RETURN:
11819 * None
11821 void LISTVIEW_Unregister(void)
11823 UnregisterClassW(WC_LISTVIEWW, NULL);
11826 /***
11827 * DESCRIPTION:
11828 * Handle any WM_COMMAND messages
11830 * PARAMETER(S):
11831 * [I] infoPtr : valid pointer to the listview structure
11832 * [I] wParam : the first message parameter
11833 * [I] lParam : the second message parameter
11835 * RETURN:
11836 * Zero.
11838 static LRESULT LISTVIEW_Command(LISTVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
11841 TRACE("(%p %x %x %lx)\n", infoPtr, HIWORD(wParam), LOWORD(wParam), lParam);
11843 if (!infoPtr->hwndEdit) return 0;
11845 switch (HIWORD(wParam))
11847 case EN_UPDATE:
11850 * Adjust the edit window size
11852 WCHAR buffer[1024];
11853 HDC hdc = GetDC(infoPtr->hwndEdit);
11854 HFONT hFont, hOldFont = 0;
11855 RECT rect;
11856 SIZE sz;
11858 if (!infoPtr->hwndEdit || !hdc) return 0;
11859 GetWindowTextW(infoPtr->hwndEdit, buffer, sizeof(buffer)/sizeof(buffer[0]));
11860 GetWindowRect(infoPtr->hwndEdit, &rect);
11862 /* Select font to get the right dimension of the string */
11863 hFont = (HFONT)SendMessageW(infoPtr->hwndEdit, WM_GETFONT, 0, 0);
11864 if (hFont)
11866 hOldFont = SelectObject(hdc, hFont);
11869 if (GetTextExtentPoint32W(hdc, buffer, lstrlenW(buffer), &sz))
11871 TEXTMETRICW textMetric;
11873 /* Add Extra spacing for the next character */
11874 GetTextMetricsW(hdc, &textMetric);
11875 sz.cx += (textMetric.tmMaxCharWidth * 2);
11877 SetWindowPos(infoPtr->hwndEdit, NULL, 0, 0, sz.cx,
11878 rect.bottom - rect.top, SWP_DRAWFRAME | SWP_NOMOVE | SWP_NOZORDER);
11880 if (hFont)
11881 SelectObject(hdc, hOldFont);
11883 ReleaseDC(infoPtr->hwndEdit, hdc);
11885 break;
11887 case EN_KILLFOCUS:
11889 LISTVIEW_CancelEditLabel(infoPtr);
11890 break;
11893 default:
11894 return SendMessageW (infoPtr->hwndNotify, WM_COMMAND, wParam, lParam);
11897 return 0;