vbscript: Implemented Tan.
[wine.git] / dlls / comctl32 / listview.c
blobbc748c61e5bc3014ab83281a5838ed3c8ee362a5
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-2014 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_SetColumnWidth ignores header images & bitmap
58 * -- LISTVIEW_StyleChanged doesn't handle some changes too well
60 * Speedups
61 * -- LISTVIEW_GetNextItem needs to be rewritten. It is currently
62 * linear in the number of items in the list, and this is
63 * unacceptable for large lists.
64 * -- if list is sorted by item text LISTVIEW_InsertItemT could use
65 * binary search to calculate item index (e.g. DPA_Search()).
66 * This requires sorted state to be reliably tracked in item modifiers.
67 * -- we should keep an ordered array of coordinates in iconic mode
68 * this would allow to frame items (iterator_frameditems),
69 * and find nearest item (LVFI_NEARESTXY) a lot more efficiently
71 * Flags
72 * -- LVIF_COLUMNS
73 * -- LVIF_GROUPID
75 * States
76 * -- LVIS_ACTIVATING (not currently supported by comctl32.dll version 6.0)
77 * -- LVIS_DROPHILITED
79 * Styles
80 * -- LVS_NOLABELWRAP
81 * -- LVS_NOSCROLL (see Q137520)
82 * -- LVS_ALIGNTOP
84 * Extended Styles
85 * -- LVS_EX_BORDERSELECT
86 * -- LVS_EX_FLATSB
87 * -- LVS_EX_INFOTIP
88 * -- LVS_EX_LABELTIP
89 * -- LVS_EX_MULTIWORKAREAS
90 * -- LVS_EX_REGIONAL
91 * -- LVS_EX_SIMPLESELECT
92 * -- LVS_EX_TWOCLICKACTIVATE
93 * -- LVS_EX_UNDERLINECOLD
94 * -- LVS_EX_UNDERLINEHOT
96 * Notifications:
97 * -- LVN_BEGINSCROLL, LVN_ENDSCROLL
98 * -- LVN_GETINFOTIP
99 * -- LVN_HOTTRACK
100 * -- LVN_SETDISPINFO
102 * Messages:
103 * -- LVM_ENABLEGROUPVIEW
104 * -- LVM_GETBKIMAGE, LVM_SETBKIMAGE
105 * -- LVM_GETGROUPINFO, LVM_SETGROUPINFO
106 * -- LVM_GETGROUPMETRICS, LVM_SETGROUPMETRICS
107 * -- LVM_GETINSERTMARK, LVM_SETINSERTMARK
108 * -- LVM_GETINSERTMARKCOLOR, LVM_SETINSERTMARKCOLOR
109 * -- LVM_GETINSERTMARKRECT
110 * -- LVM_GETNUMBEROFWORKAREAS
111 * -- LVM_GETOUTLINECOLOR, LVM_SETOUTLINECOLOR
112 * -- LVM_GETSELECTEDCOLUMN, LVM_SETSELECTEDCOLUMN
113 * -- LVM_GETISEARCHSTRINGW, LVM_GETISEARCHSTRINGA
114 * -- LVM_GETTILEINFO, LVM_SETTILEINFO
115 * -- LVM_GETTILEVIEWINFO, LVM_SETTILEVIEWINFO
116 * -- LVM_GETWORKAREAS, LVM_SETWORKAREAS
117 * -- LVM_HASGROUP, LVM_INSERTGROUP, LVM_REMOVEGROUP, LVM_REMOVEALLGROUPS
118 * -- LVM_INSERTGROUPSORTED
119 * -- LVM_INSERTMARKHITTEST
120 * -- LVM_ISGROUPVIEWENABLED
121 * -- LVM_MOVEGROUP
122 * -- LVM_MOVEITEMTOGROUP
123 * -- LVM_SETINFOTIP
124 * -- LVM_SETTILEWIDTH
125 * -- LVM_SORTGROUPS
127 * Macros:
128 * -- ListView_GetHoverTime, ListView_SetHoverTime
129 * -- ListView_GetISearchString
130 * -- ListView_GetNumberOfWorkAreas
131 * -- ListView_GetWorkAreas, ListView_SetWorkAreas
133 * Functions:
134 * -- LVGroupComparE
137 #include "config.h"
138 #include "wine/port.h"
140 #include <assert.h>
141 #include <ctype.h>
142 #include <string.h>
143 #include <stdlib.h>
144 #include <stdarg.h>
145 #include <stdio.h>
147 #include "windef.h"
148 #include "winbase.h"
149 #include "winnt.h"
150 #include "wingdi.h"
151 #include "winuser.h"
152 #include "winnls.h"
153 #include "commctrl.h"
154 #include "comctl32.h"
155 #include "uxtheme.h"
157 #include "wine/debug.h"
158 #include "wine/unicode.h"
160 WINE_DEFAULT_DEBUG_CHANNEL(listview);
162 typedef struct tagCOLUMN_INFO
164 RECT rcHeader; /* tracks the header's rectangle */
165 INT fmt; /* same as LVCOLUMN.fmt */
166 INT cxMin;
167 } COLUMN_INFO;
169 typedef struct tagITEMHDR
171 LPWSTR pszText;
172 INT iImage;
173 } ITEMHDR, *LPITEMHDR;
175 typedef struct tagSUBITEM_INFO
177 ITEMHDR hdr;
178 INT iSubItem;
179 } SUBITEM_INFO;
181 typedef struct tagITEM_ID ITEM_ID;
183 typedef struct tagITEM_INFO
185 ITEMHDR hdr;
186 UINT state;
187 LPARAM lParam;
188 INT iIndent;
189 ITEM_ID *id;
190 } ITEM_INFO;
192 struct tagITEM_ID
194 UINT id; /* item id */
195 HDPA item; /* link to item data */
198 typedef struct tagRANGE
200 INT lower;
201 INT upper;
202 } RANGE;
204 typedef struct tagRANGES
206 HDPA hdpa;
207 } *RANGES;
209 typedef struct tagITERATOR
211 INT nItem;
212 INT nSpecial;
213 RANGE range;
214 RANGES ranges;
215 INT index;
216 } ITERATOR;
218 typedef struct tagDELAYED_ITEM_EDIT
220 BOOL fEnabled;
221 INT iItem;
222 } DELAYED_ITEM_EDIT;
224 typedef struct tagLISTVIEW_INFO
226 /* control window */
227 HWND hwndSelf;
228 RECT rcList; /* This rectangle is really the window
229 * client rectangle possibly reduced by the
230 * horizontal scroll bar and/or header - see
231 * LISTVIEW_UpdateSize. This rectangle offset
232 * by the LISTVIEW_GetOrigin value is in
233 * client coordinates */
235 /* notification window */
236 SHORT notifyFormat;
237 HWND hwndNotify;
238 BOOL bDoChangeNotify; /* send change notification messages? */
239 UINT uCallbackMask;
241 /* tooltips */
242 HWND hwndToolTip;
244 /* items */
245 INT nItemCount; /* the number of items in the list */
246 HDPA hdpaItems; /* array ITEM_INFO pointers */
247 HDPA hdpaItemIds; /* array of ITEM_ID pointers */
248 HDPA hdpaPosX; /* maintains the (X, Y) coordinates of the */
249 HDPA hdpaPosY; /* items in LVS_ICON, and LVS_SMALLICON modes */
250 RANGES selectionRanges;
251 INT nSelectionMark; /* item to start next multiselection from */
252 INT nHotItem;
253 BOOL bAutoarrange; /* Autoarrange flag when NOT in LVS_AUTOARRANGE */
255 /* columns */
256 HDPA hdpaColumns; /* array of COLUMN_INFO pointers */
257 BOOL colRectsDirty; /* trigger column rectangles requery from header */
259 /* item metrics */
260 BOOL bNoItemMetrics; /* flags if item metrics are not yet computed */
261 INT nItemHeight;
262 INT nItemWidth;
264 /* sorting */
265 PFNLVCOMPARE pfnCompare; /* sorting callback pointer */
266 LPARAM lParamSort;
268 /* style */
269 DWORD dwStyle; /* the cached window GWL_STYLE */
270 DWORD dwLvExStyle; /* extended listview style */
271 DWORD uView; /* current view available through LVM_[G,S]ETVIEW */
273 /* edit item */
274 HWND hwndEdit;
275 WNDPROC EditWndProc;
276 INT nEditLabelItem;
277 DELAYED_ITEM_EDIT itemEdit; /* Pointer to this structure will be the timer ID */
279 /* icons */
280 HIMAGELIST himlNormal;
281 HIMAGELIST himlSmall;
282 HIMAGELIST himlState;
283 SIZE iconSize;
284 BOOL autoSpacing;
285 SIZE iconSpacing;
286 SIZE iconStateSize;
287 POINT currIconPos; /* this is the position next icon will be placed */
289 /* header */
290 HWND hwndHeader;
291 INT xTrackLine; /* The x coefficient of the track line or -1 if none */
293 /* marquee selection */
294 BOOL bMarqueeSelect; /* marquee selection/highlight underway */
295 BOOL bScrolling;
296 RECT marqueeRect; /* absolute coordinates of marquee selection */
297 RECT marqueeDrawRect; /* relative coordinates for drawing marquee */
298 POINT marqueeOrigin; /* absolute coordinates of marquee click origin */
300 /* focus drawing */
301 BOOL bFocus; /* control has focus */
302 INT nFocusedItem;
303 RECT rcFocus; /* focus bounds */
305 /* colors */
306 HBRUSH hBkBrush;
307 COLORREF clrBk;
308 COLORREF clrText;
309 COLORREF clrTextBk;
311 /* font */
312 HFONT hDefaultFont;
313 HFONT hFont;
314 INT ntmHeight; /* Some cached metrics of the font used */
315 INT ntmMaxCharWidth; /* by the listview to draw items */
316 INT nEllipsisWidth;
318 /* mouse operation */
319 BOOL bLButtonDown;
320 BOOL bDragging;
321 POINT ptClickPos; /* point where the user clicked */
322 INT nLButtonDownItem; /* tracks item to reset multiselection on WM_LBUTTONUP */
323 DWORD dwHoverTime;
324 HCURSOR hHotCursor;
325 INT cWheelRemainder;
327 /* keyboard operation */
328 DWORD lastKeyPressTimestamp;
329 WPARAM charCode;
330 INT nSearchParamLength;
331 WCHAR szSearchParam[ MAX_PATH ];
333 /* painting */
334 BOOL bIsDrawing; /* Drawing in progress */
335 INT nMeasureItemHeight; /* WM_MEASUREITEM result */
336 BOOL bRedraw; /* WM_SETREDRAW switch */
338 /* misc */
339 DWORD iVersion; /* CCM_[G,S]ETVERSION */
340 } LISTVIEW_INFO;
343 * constants
345 /* How many we debug buffer to allocate */
346 #define DEBUG_BUFFERS 20
347 /* The size of a single debug buffer */
348 #define DEBUG_BUFFER_SIZE 256
350 /* Internal interface to LISTVIEW_HScroll and LISTVIEW_VScroll */
351 #define SB_INTERNAL -1
353 /* maximum size of a label */
354 #define DISP_TEXT_SIZE 260
356 /* padding for items in list and small icon display modes */
357 #define WIDTH_PADDING 12
359 /* padding for items in list, report and small icon display modes */
360 #define HEIGHT_PADDING 1
362 /* offset of items in report display mode */
363 #define REPORT_MARGINX 2
365 /* padding for icon in large icon display mode
366 * ICON_TOP_PADDING_NOTHITABLE - space between top of box and area
367 * that HITTEST will see.
368 * ICON_TOP_PADDING_HITABLE - spacing between above and icon.
369 * ICON_TOP_PADDING - sum of the two above.
370 * ICON_BOTTOM_PADDING - between bottom of icon and top of text
371 * LABEL_HOR_PADDING - between text and sides of box
372 * LABEL_VERT_PADDING - between bottom of text and end of box
374 * ICON_LR_PADDING - additional width above icon size.
375 * ICON_LR_HALF - half of the above value
377 #define ICON_TOP_PADDING_NOTHITABLE 2
378 #define ICON_TOP_PADDING_HITABLE 2
379 #define ICON_TOP_PADDING (ICON_TOP_PADDING_NOTHITABLE + ICON_TOP_PADDING_HITABLE)
380 #define ICON_BOTTOM_PADDING 4
381 #define LABEL_HOR_PADDING 5
382 #define LABEL_VERT_PADDING 7
383 #define ICON_LR_PADDING 16
384 #define ICON_LR_HALF (ICON_LR_PADDING/2)
386 /* default label width for items in list and small icon display modes */
387 #define DEFAULT_LABEL_WIDTH 40
388 /* maximum select rectangle width for empty text item in LV_VIEW_DETAILS */
389 #define MAX_EMPTYTEXT_SELECT_WIDTH 80
391 /* default column width for items in list display mode */
392 #define DEFAULT_COLUMN_WIDTH 128
394 /* Size of "line" scroll for V & H scrolls */
395 #define LISTVIEW_SCROLL_ICON_LINE_SIZE 37
397 /* Padding between image and label */
398 #define IMAGE_PADDING 2
400 /* Padding behind the label */
401 #define TRAILING_LABEL_PADDING 12
402 #define TRAILING_HEADER_PADDING 11
404 /* Border for the icon caption */
405 #define CAPTION_BORDER 2
407 /* Standard DrawText flags */
408 #define LV_ML_DT_FLAGS (DT_TOP | DT_NOPREFIX | DT_EDITCONTROL | DT_CENTER | DT_WORDBREAK | DT_WORD_ELLIPSIS | DT_END_ELLIPSIS)
409 #define LV_FL_DT_FLAGS (DT_TOP | DT_NOPREFIX | DT_EDITCONTROL | DT_CENTER | DT_WORDBREAK | DT_NOCLIP)
410 #define LV_SL_DT_FLAGS (DT_VCENTER | DT_NOPREFIX | DT_EDITCONTROL | DT_SINGLELINE | DT_WORD_ELLIPSIS | DT_END_ELLIPSIS)
412 /* Image index from state */
413 #define STATEIMAGEINDEX(x) (((x) & LVIS_STATEIMAGEMASK) >> 12)
415 /* The time in milliseconds to reset the search in the list */
416 #define KEY_DELAY 450
418 /* Dump the LISTVIEW_INFO structure to the debug channel */
419 #define LISTVIEW_DUMP(iP) do { \
420 TRACE("hwndSelf=%p, clrBk=0x%06x, clrText=0x%06x, clrTextBk=0x%06x, ItemHeight=%d, ItemWidth=%d, Style=0x%08x\n", \
421 iP->hwndSelf, iP->clrBk, iP->clrText, iP->clrTextBk, \
422 iP->nItemHeight, iP->nItemWidth, iP->dwStyle); \
423 TRACE("hwndSelf=%p, himlNor=%p, himlSml=%p, himlState=%p, Focused=%d, Hot=%d, exStyle=0x%08x, Focus=%d\n", \
424 iP->hwndSelf, iP->himlNormal, iP->himlSmall, iP->himlState, \
425 iP->nFocusedItem, iP->nHotItem, iP->dwLvExStyle, iP->bFocus ); \
426 TRACE("hwndSelf=%p, ntmH=%d, icSz.cx=%d, icSz.cy=%d, icSp.cx=%d, icSp.cy=%d, notifyFmt=%d\n", \
427 iP->hwndSelf, iP->ntmHeight, iP->iconSize.cx, iP->iconSize.cy, \
428 iP->iconSpacing.cx, iP->iconSpacing.cy, iP->notifyFormat); \
429 TRACE("hwndSelf=%p, rcList=%s\n", iP->hwndSelf, wine_dbgstr_rect(&iP->rcList)); \
430 } while(0)
432 static const WCHAR themeClass[] = {'L','i','s','t','V','i','e','w',0};
435 * forward declarations
437 static BOOL LISTVIEW_GetItemT(const LISTVIEW_INFO *, LPLVITEMW, BOOL);
438 static void LISTVIEW_GetItemBox(const LISTVIEW_INFO *, INT, LPRECT);
439 static void LISTVIEW_GetItemOrigin(const LISTVIEW_INFO *, INT, LPPOINT);
440 static BOOL LISTVIEW_GetItemPosition(const LISTVIEW_INFO *, INT, LPPOINT);
441 static BOOL LISTVIEW_GetItemRect(const LISTVIEW_INFO *, INT, LPRECT);
442 static void LISTVIEW_GetOrigin(const LISTVIEW_INFO *, LPPOINT);
443 static BOOL LISTVIEW_GetViewRect(const LISTVIEW_INFO *, LPRECT);
444 static void LISTVIEW_UpdateSize(LISTVIEW_INFO *);
445 static LRESULT LISTVIEW_Command(LISTVIEW_INFO *, WPARAM, LPARAM);
446 static INT LISTVIEW_GetStringWidthT(const LISTVIEW_INFO *, LPCWSTR, BOOL);
447 static BOOL LISTVIEW_KeySelection(LISTVIEW_INFO *, INT, BOOL);
448 static UINT LISTVIEW_GetItemState(const LISTVIEW_INFO *, INT, UINT);
449 static BOOL LISTVIEW_SetItemState(LISTVIEW_INFO *, INT, const LVITEMW *);
450 static LRESULT LISTVIEW_VScroll(LISTVIEW_INFO *, INT, INT);
451 static LRESULT LISTVIEW_HScroll(LISTVIEW_INFO *, INT, INT);
452 static BOOL LISTVIEW_EnsureVisible(LISTVIEW_INFO *, INT, BOOL);
453 static HIMAGELIST LISTVIEW_SetImageList(LISTVIEW_INFO *, INT, HIMAGELIST);
454 static INT LISTVIEW_HitTest(const LISTVIEW_INFO *, LPLVHITTESTINFO, BOOL, BOOL);
455 static BOOL LISTVIEW_EndEditLabelT(LISTVIEW_INFO *, BOOL, BOOL);
456 static BOOL LISTVIEW_Scroll(LISTVIEW_INFO *, INT, INT);
458 /******** Text handling functions *************************************/
460 /* A text pointer is either NULL, LPSTR_TEXTCALLBACK, or points to a
461 * text string. The string may be ANSI or Unicode, in which case
462 * the boolean isW tells us the type of the string.
464 * The name of the function tell what type of strings it expects:
465 * W: Unicode, T: ANSI/Unicode - function of isW
468 static inline BOOL is_text(LPCWSTR text)
470 return text != NULL && text != LPSTR_TEXTCALLBACKW;
473 static inline int textlenT(LPCWSTR text, BOOL isW)
475 return !is_text(text) ? 0 :
476 isW ? lstrlenW(text) : lstrlenA((LPCSTR)text);
479 static inline void textcpynT(LPWSTR dest, BOOL isDestW, LPCWSTR src, BOOL isSrcW, INT max)
481 if (isDestW)
482 if (isSrcW) lstrcpynW(dest, src, max);
483 else MultiByteToWideChar(CP_ACP, 0, (LPCSTR)src, -1, dest, max);
484 else
485 if (isSrcW) WideCharToMultiByte(CP_ACP, 0, src, -1, (LPSTR)dest, max, NULL, NULL);
486 else lstrcpynA((LPSTR)dest, (LPCSTR)src, max);
489 static inline LPWSTR textdupTtoW(LPCWSTR text, BOOL isW)
491 LPWSTR wstr = (LPWSTR)text;
493 if (!isW && is_text(text))
495 INT len = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)text, -1, NULL, 0);
496 wstr = Alloc(len * sizeof(WCHAR));
497 if (wstr) MultiByteToWideChar(CP_ACP, 0, (LPCSTR)text, -1, wstr, len);
499 TRACE(" wstr=%s\n", text == LPSTR_TEXTCALLBACKW ? "(callback)" : debugstr_w(wstr));
500 return wstr;
503 static inline void textfreeT(LPWSTR wstr, BOOL isW)
505 if (!isW && is_text(wstr)) Free (wstr);
509 * dest is a pointer to a Unicode string
510 * src is a pointer to a string (Unicode if isW, ANSI if !isW)
512 static BOOL textsetptrT(LPWSTR *dest, LPCWSTR src, BOOL isW)
514 BOOL bResult = TRUE;
516 if (src == LPSTR_TEXTCALLBACKW)
518 if (is_text(*dest)) Free(*dest);
519 *dest = LPSTR_TEXTCALLBACKW;
521 else
523 LPWSTR pszText = textdupTtoW(src, isW);
524 if (*dest == LPSTR_TEXTCALLBACKW) *dest = NULL;
525 bResult = Str_SetPtrW(dest, pszText);
526 textfreeT(pszText, isW);
528 return bResult;
532 * compares a Unicode to a Unicode/ANSI text string
534 static inline int textcmpWT(LPCWSTR aw, LPCWSTR bt, BOOL isW)
536 if (!aw) return bt ? -1 : 0;
537 if (!bt) return 1;
538 if (aw == LPSTR_TEXTCALLBACKW)
539 return bt == LPSTR_TEXTCALLBACKW ? 1 : -1;
540 if (bt != LPSTR_TEXTCALLBACKW)
542 LPWSTR bw = textdupTtoW(bt, isW);
543 int r = bw ? lstrcmpW(aw, bw) : 1;
544 textfreeT(bw, isW);
545 return r;
548 return 1;
551 static inline int lstrncmpiW(LPCWSTR s1, LPCWSTR s2, int n)
553 n = min(min(n, lstrlenW(s1)), lstrlenW(s2));
554 return CompareStringW(LOCALE_USER_DEFAULT, NORM_IGNORECASE, s1, n, s2, n) - CSTR_EQUAL;
557 /******** Debugging functions *****************************************/
559 static inline LPCSTR debugtext_t(LPCWSTR text, BOOL isW)
561 if (text == LPSTR_TEXTCALLBACKW) return "(callback)";
562 return isW ? debugstr_w(text) : debugstr_a((LPCSTR)text);
565 static inline LPCSTR debugtext_tn(LPCWSTR text, BOOL isW, INT n)
567 if (text == LPSTR_TEXTCALLBACKW) return "(callback)";
568 n = min(textlenT(text, isW), n);
569 return isW ? debugstr_wn(text, n) : debugstr_an((LPCSTR)text, n);
572 static char* debug_getbuf(void)
574 static int index = 0;
575 static char buffers[DEBUG_BUFFERS][DEBUG_BUFFER_SIZE];
576 return buffers[index++ % DEBUG_BUFFERS];
579 static inline const char* debugrange(const RANGE *lprng)
581 if (!lprng) return "(null)";
582 return wine_dbg_sprintf("[%d, %d]", lprng->lower, lprng->upper);
585 static const char* debugscrollinfo(const SCROLLINFO *pScrollInfo)
587 char* buf = debug_getbuf(), *text = buf;
588 int len, size = DEBUG_BUFFER_SIZE;
590 if (pScrollInfo == NULL) return "(null)";
591 len = snprintf(buf, size, "{cbSize=%d, ", pScrollInfo->cbSize);
592 if (len == -1) goto end; buf += len; size -= len;
593 if (pScrollInfo->fMask & SIF_RANGE)
594 len = snprintf(buf, size, "nMin=%d, nMax=%d, ", pScrollInfo->nMin, pScrollInfo->nMax);
595 else len = 0;
596 if (len == -1) goto end; buf += len; size -= len;
597 if (pScrollInfo->fMask & SIF_PAGE)
598 len = snprintf(buf, size, "nPage=%u, ", pScrollInfo->nPage);
599 else len = 0;
600 if (len == -1) goto end; buf += len; size -= len;
601 if (pScrollInfo->fMask & SIF_POS)
602 len = snprintf(buf, size, "nPos=%d, ", pScrollInfo->nPos);
603 else len = 0;
604 if (len == -1) goto end; buf += len; size -= len;
605 if (pScrollInfo->fMask & SIF_TRACKPOS)
606 len = snprintf(buf, size, "nTrackPos=%d, ", pScrollInfo->nTrackPos);
607 else len = 0;
608 if (len == -1) goto end; buf += len;
609 goto undo;
610 end:
611 buf = text + strlen(text);
612 undo:
613 if (buf - text > 2) { buf[-2] = '}'; buf[-1] = 0; }
614 return text;
617 static const char* debugnmlistview(const NMLISTVIEW *plvnm)
619 if (!plvnm) return "(null)";
620 return wine_dbg_sprintf("iItem=%d, iSubItem=%d, uNewState=0x%x,"
621 " uOldState=0x%x, uChanged=0x%x, ptAction=%s, lParam=%ld",
622 plvnm->iItem, plvnm->iSubItem, plvnm->uNewState, plvnm->uOldState,
623 plvnm->uChanged, wine_dbgstr_point(&plvnm->ptAction), plvnm->lParam);
626 static const char* debuglvitem_t(const LVITEMW *lpLVItem, BOOL isW)
628 char* buf = debug_getbuf(), *text = buf;
629 int len, size = DEBUG_BUFFER_SIZE;
631 if (lpLVItem == NULL) return "(null)";
632 len = snprintf(buf, size, "{iItem=%d, iSubItem=%d, ", lpLVItem->iItem, lpLVItem->iSubItem);
633 if (len == -1) goto end; buf += len; size -= len;
634 if (lpLVItem->mask & LVIF_STATE)
635 len = snprintf(buf, size, "state=%x, stateMask=%x, ", lpLVItem->state, lpLVItem->stateMask);
636 else len = 0;
637 if (len == -1) goto end; buf += len; size -= len;
638 if (lpLVItem->mask & LVIF_TEXT)
639 len = snprintf(buf, size, "pszText=%s, cchTextMax=%d, ", debugtext_tn(lpLVItem->pszText, isW, 80), lpLVItem->cchTextMax);
640 else len = 0;
641 if (len == -1) goto end; buf += len; size -= len;
642 if (lpLVItem->mask & LVIF_IMAGE)
643 len = snprintf(buf, size, "iImage=%d, ", lpLVItem->iImage);
644 else len = 0;
645 if (len == -1) goto end; buf += len; size -= len;
646 if (lpLVItem->mask & LVIF_PARAM)
647 len = snprintf(buf, size, "lParam=%lx, ", lpLVItem->lParam);
648 else len = 0;
649 if (len == -1) goto end; buf += len; size -= len;
650 if (lpLVItem->mask & LVIF_INDENT)
651 len = snprintf(buf, size, "iIndent=%d, ", lpLVItem->iIndent);
652 else len = 0;
653 if (len == -1) goto end; buf += len;
654 goto undo;
655 end:
656 buf = text + strlen(text);
657 undo:
658 if (buf - text > 2) { buf[-2] = '}'; buf[-1] = 0; }
659 return text;
662 static const char* debuglvcolumn_t(const LVCOLUMNW *lpColumn, BOOL isW)
664 char* buf = debug_getbuf(), *text = buf;
665 int len, size = DEBUG_BUFFER_SIZE;
667 if (lpColumn == NULL) return "(null)";
668 len = snprintf(buf, size, "{");
669 if (len == -1) goto end; buf += len; size -= len;
670 if (lpColumn->mask & LVCF_SUBITEM)
671 len = snprintf(buf, size, "iSubItem=%d, ", lpColumn->iSubItem);
672 else len = 0;
673 if (len == -1) goto end; buf += len; size -= len;
674 if (lpColumn->mask & LVCF_FMT)
675 len = snprintf(buf, size, "fmt=%x, ", lpColumn->fmt);
676 else len = 0;
677 if (len == -1) goto end; buf += len; size -= len;
678 if (lpColumn->mask & LVCF_WIDTH)
679 len = snprintf(buf, size, "cx=%d, ", lpColumn->cx);
680 else len = 0;
681 if (len == -1) goto end; buf += len; size -= len;
682 if (lpColumn->mask & LVCF_TEXT)
683 len = snprintf(buf, size, "pszText=%s, cchTextMax=%d, ", debugtext_tn(lpColumn->pszText, isW, 80), lpColumn->cchTextMax);
684 else len = 0;
685 if (len == -1) goto end; buf += len; size -= len;
686 if (lpColumn->mask & LVCF_IMAGE)
687 len = snprintf(buf, size, "iImage=%d, ", lpColumn->iImage);
688 else len = 0;
689 if (len == -1) goto end; buf += len; size -= len;
690 if (lpColumn->mask & LVCF_ORDER)
691 len = snprintf(buf, size, "iOrder=%d, ", lpColumn->iOrder);
692 else len = 0;
693 if (len == -1) goto end; buf += len;
694 goto undo;
695 end:
696 buf = text + strlen(text);
697 undo:
698 if (buf - text > 2) { buf[-2] = '}'; buf[-1] = 0; }
699 return text;
702 static const char* debuglvhittestinfo(const LVHITTESTINFO *lpht)
704 if (!lpht) return "(null)";
706 return wine_dbg_sprintf("{pt=%s, flags=0x%x, iItem=%d, iSubItem=%d}",
707 wine_dbgstr_point(&lpht->pt), lpht->flags, lpht->iItem, lpht->iSubItem);
710 /* Return the corresponding text for a given scroll value */
711 static inline LPCSTR debugscrollcode(int nScrollCode)
713 switch(nScrollCode)
715 case SB_LINELEFT: return "SB_LINELEFT";
716 case SB_LINERIGHT: return "SB_LINERIGHT";
717 case SB_PAGELEFT: return "SB_PAGELEFT";
718 case SB_PAGERIGHT: return "SB_PAGERIGHT";
719 case SB_THUMBPOSITION: return "SB_THUMBPOSITION";
720 case SB_THUMBTRACK: return "SB_THUMBTRACK";
721 case SB_ENDSCROLL: return "SB_ENDSCROLL";
722 case SB_INTERNAL: return "SB_INTERNAL";
723 default: return "unknown";
728 /******** Notification functions ************************************/
730 static int get_ansi_notification(UINT unicodeNotificationCode)
732 switch (unicodeNotificationCode)
734 case LVN_BEGINLABELEDITA:
735 case LVN_BEGINLABELEDITW: return LVN_BEGINLABELEDITA;
736 case LVN_ENDLABELEDITA:
737 case LVN_ENDLABELEDITW: return LVN_ENDLABELEDITA;
738 case LVN_GETDISPINFOA:
739 case LVN_GETDISPINFOW: return LVN_GETDISPINFOA;
740 case LVN_SETDISPINFOA:
741 case LVN_SETDISPINFOW: return LVN_SETDISPINFOA;
742 case LVN_ODFINDITEMA:
743 case LVN_ODFINDITEMW: return LVN_ODFINDITEMA;
744 case LVN_GETINFOTIPA:
745 case LVN_GETINFOTIPW: return LVN_GETINFOTIPA;
746 /* header forwards */
747 case HDN_TRACKA:
748 case HDN_TRACKW: return HDN_TRACKA;
749 case HDN_ENDTRACKA:
750 case HDN_ENDTRACKW: return HDN_ENDTRACKA;
751 case HDN_BEGINDRAG: return HDN_BEGINDRAG;
752 case HDN_ENDDRAG: return HDN_ENDDRAG;
753 case HDN_ITEMCHANGINGA:
754 case HDN_ITEMCHANGINGW: return HDN_ITEMCHANGINGA;
755 case HDN_ITEMCHANGEDA:
756 case HDN_ITEMCHANGEDW: return HDN_ITEMCHANGEDA;
757 case HDN_ITEMCLICKA:
758 case HDN_ITEMCLICKW: return HDN_ITEMCLICKA;
759 case HDN_DIVIDERDBLCLICKA:
760 case HDN_DIVIDERDBLCLICKW: return HDN_DIVIDERDBLCLICKA;
761 default: break;
763 FIXME("unknown notification %x\n", unicodeNotificationCode);
764 return unicodeNotificationCode;
767 /* forwards header notifications to listview parent */
768 static LRESULT notify_forward_header(const LISTVIEW_INFO *infoPtr, NMHEADERW *lpnmhW)
770 LPCWSTR text = NULL, filter = NULL;
771 LRESULT ret;
772 NMHEADERA *lpnmh = (NMHEADERA*) lpnmhW;
774 /* on unicode format exit earlier */
775 if (infoPtr->notifyFormat == NFR_UNICODE)
776 return SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, lpnmh->hdr.idFrom,
777 (LPARAM)lpnmh);
779 /* header always supplies unicode notifications,
780 all we have to do is to convert strings to ANSI */
781 if (lpnmh->pitem)
783 /* convert item text */
784 if (lpnmh->pitem->mask & HDI_TEXT)
786 text = (LPCWSTR)lpnmh->pitem->pszText;
787 lpnmh->pitem->pszText = NULL;
788 Str_SetPtrWtoA(&lpnmh->pitem->pszText, text);
790 /* convert filter text */
791 if ((lpnmh->pitem->mask & HDI_FILTER) && (lpnmh->pitem->type == HDFT_ISSTRING) &&
792 lpnmh->pitem->pvFilter)
794 filter = (LPCWSTR)((HD_TEXTFILTERA*)lpnmh->pitem->pvFilter)->pszText;
795 ((HD_TEXTFILTERA*)lpnmh->pitem->pvFilter)->pszText = NULL;
796 Str_SetPtrWtoA(&((HD_TEXTFILTERA*)lpnmh->pitem->pvFilter)->pszText, filter);
799 lpnmh->hdr.code = get_ansi_notification(lpnmh->hdr.code);
801 ret = SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, lpnmh->hdr.idFrom,
802 (LPARAM)lpnmh);
804 /* cleanup */
805 if(text)
807 Free(lpnmh->pitem->pszText);
808 lpnmh->pitem->pszText = (LPSTR)text;
810 if(filter)
812 Free(((HD_TEXTFILTERA*)lpnmh->pitem->pvFilter)->pszText);
813 ((HD_TEXTFILTERA*)lpnmh->pitem->pvFilter)->pszText = (LPSTR)filter;
816 return ret;
819 static LRESULT notify_hdr(const LISTVIEW_INFO *infoPtr, INT code, LPNMHDR pnmh)
821 LRESULT result;
823 TRACE("(code=%d)\n", code);
825 pnmh->hwndFrom = infoPtr->hwndSelf;
826 pnmh->idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
827 pnmh->code = code;
828 result = SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, pnmh->idFrom, (LPARAM)pnmh);
830 TRACE(" <= %ld\n", result);
832 return result;
835 static inline BOOL notify(const LISTVIEW_INFO *infoPtr, INT code)
837 NMHDR nmh;
838 HWND hwnd = infoPtr->hwndSelf;
839 notify_hdr(infoPtr, code, &nmh);
840 return IsWindow(hwnd);
843 static inline void notify_itemactivate(const LISTVIEW_INFO *infoPtr, const LVHITTESTINFO *htInfo)
845 NMITEMACTIVATE nmia;
846 LVITEMW item;
848 if (htInfo) {
849 nmia.uNewState = 0;
850 nmia.uOldState = 0;
851 nmia.uChanged = 0;
852 nmia.uKeyFlags = 0;
854 item.mask = LVIF_PARAM|LVIF_STATE;
855 item.iItem = htInfo->iItem;
856 item.iSubItem = 0;
857 item.stateMask = (UINT)-1;
858 if (LISTVIEW_GetItemT(infoPtr, &item, TRUE)) {
859 nmia.lParam = item.lParam;
860 nmia.uOldState = item.state;
861 nmia.uNewState = item.state | LVIS_ACTIVATING;
862 nmia.uChanged = LVIF_STATE;
865 nmia.iItem = htInfo->iItem;
866 nmia.iSubItem = htInfo->iSubItem;
867 nmia.ptAction = htInfo->pt;
869 if (GetKeyState(VK_SHIFT) & 0x8000) nmia.uKeyFlags |= LVKF_SHIFT;
870 if (GetKeyState(VK_CONTROL) & 0x8000) nmia.uKeyFlags |= LVKF_CONTROL;
871 if (GetKeyState(VK_MENU) & 0x8000) nmia.uKeyFlags |= LVKF_ALT;
873 notify_hdr(infoPtr, LVN_ITEMACTIVATE, (LPNMHDR)&nmia);
876 static inline LRESULT notify_listview(const LISTVIEW_INFO *infoPtr, INT code, LPNMLISTVIEW plvnm)
878 TRACE("(code=%d, plvnm=%s)\n", code, debugnmlistview(plvnm));
879 return notify_hdr(infoPtr, code, (LPNMHDR)plvnm);
882 /* Handles NM_DBLCLK, NM_CLICK, NM_RDBLCLK, NM_RCLICK. Only NM_RCLICK return value is used. */
883 static BOOL notify_click(const LISTVIEW_INFO *infoPtr, INT code, const LVHITTESTINFO *lvht)
885 NMITEMACTIVATE nmia;
886 LVITEMW item;
887 HWND hwnd = infoPtr->hwndSelf;
888 LRESULT ret;
890 TRACE("code=%d, lvht=%s\n", code, debuglvhittestinfo(lvht));
891 ZeroMemory(&nmia, sizeof(nmia));
892 nmia.iItem = lvht->iItem;
893 nmia.iSubItem = lvht->iSubItem;
894 nmia.ptAction = lvht->pt;
895 item.mask = LVIF_PARAM;
896 item.iItem = lvht->iItem;
897 item.iSubItem = 0;
898 if (LISTVIEW_GetItemT(infoPtr, &item, TRUE)) nmia.lParam = item.lParam;
899 ret = notify_hdr(infoPtr, code, (NMHDR*)&nmia);
900 return IsWindow(hwnd) && (code == NM_RCLICK ? !ret : TRUE);
903 static BOOL notify_deleteitem(const LISTVIEW_INFO *infoPtr, INT nItem)
905 NMLISTVIEW nmlv;
906 LVITEMW item;
907 HWND hwnd = infoPtr->hwndSelf;
909 ZeroMemory(&nmlv, sizeof (NMLISTVIEW));
910 nmlv.iItem = nItem;
911 item.mask = LVIF_PARAM;
912 item.iItem = nItem;
913 item.iSubItem = 0;
914 if (LISTVIEW_GetItemT(infoPtr, &item, TRUE)) nmlv.lParam = item.lParam;
915 notify_listview(infoPtr, LVN_DELETEITEM, &nmlv);
916 return IsWindow(hwnd);
920 Send notification. depends on dispinfoW having same
921 structure as dispinfoA.
922 infoPtr : listview struct
923 code : *Unicode* notification code
924 pdi : dispinfo structure (can be unicode or ansi)
925 isW : TRUE if dispinfo is Unicode
927 static BOOL notify_dispinfoT(const LISTVIEW_INFO *infoPtr, UINT code, LPNMLVDISPINFOW pdi, BOOL isW)
929 INT length = 0, ret_length;
930 LPWSTR buffer = NULL, ret_text;
931 BOOL return_ansi = FALSE;
932 BOOL return_unicode = FALSE;
933 BOOL ret;
935 if ((pdi->item.mask & LVIF_TEXT) && is_text(pdi->item.pszText))
937 return_unicode = ( isW && infoPtr->notifyFormat == NFR_ANSI);
938 return_ansi = (!isW && infoPtr->notifyFormat == NFR_UNICODE);
941 ret_length = pdi->item.cchTextMax;
942 ret_text = pdi->item.pszText;
944 if (return_unicode || return_ansi)
946 if (code != LVN_GETDISPINFOW)
948 length = return_ansi ?
949 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pdi->item.pszText, -1, NULL, 0):
950 WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, NULL, 0, NULL, NULL);
952 else
954 length = pdi->item.cchTextMax;
955 *pdi->item.pszText = 0; /* make sure we don't process garbage */
958 buffer = Alloc( (return_ansi ? sizeof(WCHAR) : sizeof(CHAR)) * length);
959 if (!buffer) return FALSE;
961 if (return_ansi)
962 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pdi->item.pszText, -1,
963 buffer, length);
964 else
965 WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, (LPSTR) buffer,
966 length, NULL, NULL);
968 pdi->item.pszText = buffer;
969 pdi->item.cchTextMax = length;
972 if (infoPtr->notifyFormat == NFR_ANSI)
973 code = get_ansi_notification(code);
975 TRACE(" pdi->item=%s\n", debuglvitem_t(&pdi->item, infoPtr->notifyFormat != NFR_ANSI));
976 ret = notify_hdr(infoPtr, code, &pdi->hdr);
977 TRACE(" resulting code=%d\n", pdi->hdr.code);
979 if (return_ansi || return_unicode)
981 if (return_ansi && (pdi->hdr.code == LVN_GETDISPINFOA))
983 strcpy((char*)ret_text, (char*)pdi->item.pszText);
985 else if (return_unicode && (pdi->hdr.code == LVN_GETDISPINFOW))
987 strcpyW(ret_text, pdi->item.pszText);
989 else if (return_ansi) /* note : pointer can be changed by app ! */
991 WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, (LPSTR) ret_text,
992 ret_length, NULL, NULL);
994 else
995 MultiByteToWideChar(CP_ACP, 0, (LPSTR) pdi->item.pszText, -1,
996 ret_text, ret_length);
998 pdi->item.pszText = ret_text; /* restores our buffer */
999 pdi->item.cchTextMax = ret_length;
1001 Free(buffer);
1002 return ret;
1005 /* if dipsinfo holder changed notification code then convert */
1006 if (!isW && (pdi->hdr.code == LVN_GETDISPINFOW) && (pdi->item.mask & LVIF_TEXT))
1008 length = WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, NULL, 0, NULL, NULL);
1010 buffer = Alloc(length * sizeof(CHAR));
1011 if (!buffer) return FALSE;
1013 WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, (LPSTR) buffer,
1014 ret_length, NULL, NULL);
1016 strcpy((LPSTR)pdi->item.pszText, (LPSTR)buffer);
1017 Free(buffer);
1020 return ret;
1023 static void customdraw_fill(NMLVCUSTOMDRAW *lpnmlvcd, const LISTVIEW_INFO *infoPtr, HDC hdc,
1024 const RECT *rcBounds, const LVITEMW *lplvItem)
1026 ZeroMemory(lpnmlvcd, sizeof(NMLVCUSTOMDRAW));
1027 lpnmlvcd->nmcd.hdc = hdc;
1028 lpnmlvcd->nmcd.rc = *rcBounds;
1029 lpnmlvcd->clrTextBk = infoPtr->clrTextBk;
1030 lpnmlvcd->clrText = infoPtr->clrText;
1031 if (!lplvItem) return;
1032 lpnmlvcd->nmcd.dwItemSpec = lplvItem->iItem + 1;
1033 lpnmlvcd->iSubItem = lplvItem->iSubItem;
1034 if (lplvItem->state & LVIS_SELECTED) lpnmlvcd->nmcd.uItemState |= CDIS_SELECTED;
1035 if (lplvItem->state & LVIS_FOCUSED) lpnmlvcd->nmcd.uItemState |= CDIS_FOCUS;
1036 if (lplvItem->iItem == infoPtr->nHotItem) lpnmlvcd->nmcd.uItemState |= CDIS_HOT;
1037 lpnmlvcd->nmcd.lItemlParam = lplvItem->lParam;
1040 static inline DWORD notify_customdraw (const LISTVIEW_INFO *infoPtr, DWORD dwDrawStage, NMLVCUSTOMDRAW *lpnmlvcd)
1042 BOOL isForItem = (lpnmlvcd->nmcd.dwItemSpec != 0);
1043 DWORD result;
1045 lpnmlvcd->nmcd.dwDrawStage = dwDrawStage;
1046 if (isForItem) lpnmlvcd->nmcd.dwDrawStage |= CDDS_ITEM;
1047 if (lpnmlvcd->iSubItem) lpnmlvcd->nmcd.dwDrawStage |= CDDS_SUBITEM;
1048 if (isForItem) lpnmlvcd->nmcd.dwItemSpec--;
1049 result = notify_hdr(infoPtr, NM_CUSTOMDRAW, &lpnmlvcd->nmcd.hdr);
1050 if (isForItem) lpnmlvcd->nmcd.dwItemSpec++;
1051 return result;
1054 static void prepaint_setup (const LISTVIEW_INFO *infoPtr, HDC hdc, NMLVCUSTOMDRAW *lpnmlvcd, BOOL SubItem)
1056 COLORREF backcolor, textcolor;
1058 /* apparently, for selected items, we have to override the returned values */
1059 if (!SubItem)
1061 if (lpnmlvcd->nmcd.uItemState & CDIS_SELECTED)
1063 if (infoPtr->bFocus)
1065 lpnmlvcd->clrTextBk = comctl32_color.clrHighlight;
1066 lpnmlvcd->clrText = comctl32_color.clrHighlightText;
1068 else if (infoPtr->dwStyle & LVS_SHOWSELALWAYS)
1070 lpnmlvcd->clrTextBk = comctl32_color.clr3dFace;
1071 lpnmlvcd->clrText = comctl32_color.clrBtnText;
1076 backcolor = lpnmlvcd->clrTextBk;
1077 textcolor = lpnmlvcd->clrText;
1079 if (backcolor == CLR_DEFAULT)
1080 backcolor = comctl32_color.clrWindow;
1081 if (textcolor == CLR_DEFAULT)
1082 textcolor = comctl32_color.clrWindowText;
1084 /* Set the text attributes */
1085 if (backcolor != CLR_NONE)
1087 SetBkMode(hdc, OPAQUE);
1088 SetBkColor(hdc, backcolor);
1090 else
1091 SetBkMode(hdc, TRANSPARENT);
1092 SetTextColor(hdc, textcolor);
1095 static inline DWORD notify_postpaint (const LISTVIEW_INFO *infoPtr, NMLVCUSTOMDRAW *lpnmlvcd)
1097 return notify_customdraw(infoPtr, CDDS_POSTPAINT, lpnmlvcd);
1100 /* returns TRUE when repaint needed, FALSE otherwise */
1101 static BOOL notify_measureitem(LISTVIEW_INFO *infoPtr)
1103 MEASUREITEMSTRUCT mis;
1104 mis.CtlType = ODT_LISTVIEW;
1105 mis.CtlID = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1106 mis.itemID = -1;
1107 mis.itemWidth = 0;
1108 mis.itemData = 0;
1109 mis.itemHeight= infoPtr->nItemHeight;
1110 SendMessageW(infoPtr->hwndNotify, WM_MEASUREITEM, mis.CtlID, (LPARAM)&mis);
1111 if (infoPtr->nItemHeight != max(mis.itemHeight, 1))
1113 infoPtr->nMeasureItemHeight = infoPtr->nItemHeight = max(mis.itemHeight, 1);
1114 return TRUE;
1116 return FALSE;
1119 /******** Item iterator functions **********************************/
1121 static RANGES ranges_create(int count);
1122 static void ranges_destroy(RANGES ranges);
1123 static BOOL ranges_add(RANGES ranges, RANGE range);
1124 static BOOL ranges_del(RANGES ranges, RANGE range);
1125 static void ranges_dump(RANGES ranges);
1127 static inline BOOL ranges_additem(RANGES ranges, INT nItem)
1129 RANGE range = { nItem, nItem + 1 };
1131 return ranges_add(ranges, range);
1134 static inline BOOL ranges_delitem(RANGES ranges, INT nItem)
1136 RANGE range = { nItem, nItem + 1 };
1138 return ranges_del(ranges, range);
1141 /***
1142 * ITERATOR DOCUMENTATION
1144 * The iterator functions allow for easy, and convenient iteration
1145 * over items of interest in the list. Typically, you create an
1146 * iterator, use it, and destroy it, as such:
1147 * ITERATOR i;
1149 * iterator_xxxitems(&i, ...);
1150 * while (iterator_{prev,next}(&i)
1152 * //code which uses i.nItem
1154 * iterator_destroy(&i);
1156 * where xxx is either: framed, or visible.
1157 * Note that it is important that the code destroys the iterator
1158 * after it's done with it, as the creation of the iterator may
1159 * allocate memory, which thus needs to be freed.
1161 * You can iterate both forwards, and backwards through the list,
1162 * by using iterator_next or iterator_prev respectively.
1164 * Lower numbered items are draw on top of higher number items in
1165 * LVS_ICON, and LVS_SMALLICON (which are the only modes where
1166 * items may overlap). So, to test items, you should use
1167 * iterator_next
1168 * which lists the items top to bottom (in Z-order).
1169 * For drawing items, you should use
1170 * iterator_prev
1171 * which lists the items bottom to top (in Z-order).
1172 * If you keep iterating over the items after the end-of-items
1173 * marker (-1) is returned, the iterator will start from the
1174 * beginning. Typically, you don't need to test for -1,
1175 * because iterator_{next,prev} will return TRUE if more items
1176 * are to be iterated over, or FALSE otherwise.
1178 * Note: the iterator is defined to be bidirectional. That is,
1179 * any number of prev followed by any number of next, or
1180 * five versa, should leave the iterator at the same item:
1181 * prev * n, next * n = next * n, prev * n
1183 * The iterator has a notion of an out-of-order, special item,
1184 * which sits at the start of the list. This is used in
1185 * LVS_ICON, and LVS_SMALLICON mode to handle the focused item,
1186 * which needs to be first, as it may overlap other items.
1188 * The code is a bit messy because we have:
1189 * - a special item to deal with
1190 * - simple range, or composite range
1191 * - empty range.
1192 * If you find bugs, or want to add features, please make sure you
1193 * always check/modify *both* iterator_prev, and iterator_next.
1196 /****
1197 * This function iterates through the items in increasing order,
1198 * but prefixed by the special item, then -1. That is:
1199 * special, 1, 2, 3, ..., n, -1.
1200 * Each item is listed only once.
1202 static inline BOOL iterator_next(ITERATOR* i)
1204 if (i->nItem == -1)
1206 i->nItem = i->nSpecial;
1207 if (i->nItem != -1) return TRUE;
1209 if (i->nItem == i->nSpecial)
1211 if (i->ranges) i->index = 0;
1212 goto pickarange;
1215 i->nItem++;
1216 testitem:
1217 if (i->nItem == i->nSpecial) i->nItem++;
1218 if (i->nItem < i->range.upper) return TRUE;
1220 pickarange:
1221 if (i->ranges)
1223 if (i->index < DPA_GetPtrCount(i->ranges->hdpa))
1224 i->range = *(RANGE*)DPA_GetPtr(i->ranges->hdpa, i->index++);
1225 else goto end;
1227 else if (i->nItem >= i->range.upper) goto end;
1229 i->nItem = i->range.lower;
1230 if (i->nItem >= 0) goto testitem;
1231 end:
1232 i->nItem = -1;
1233 return FALSE;
1236 /****
1237 * This function iterates through the items in decreasing order,
1238 * followed by the special item, then -1. That is:
1239 * n, n-1, ..., 3, 2, 1, special, -1.
1240 * Each item is listed only once.
1242 static inline BOOL iterator_prev(ITERATOR* i)
1244 BOOL start = FALSE;
1246 if (i->nItem == -1)
1248 start = TRUE;
1249 if (i->ranges) i->index = DPA_GetPtrCount(i->ranges->hdpa);
1250 goto pickarange;
1252 if (i->nItem == i->nSpecial)
1254 i->nItem = -1;
1255 return FALSE;
1258 testitem:
1259 i->nItem--;
1260 if (i->nItem == i->nSpecial) i->nItem--;
1261 if (i->nItem >= i->range.lower) return TRUE;
1263 pickarange:
1264 if (i->ranges)
1266 if (i->index > 0)
1267 i->range = *(RANGE*)DPA_GetPtr(i->ranges->hdpa, --i->index);
1268 else goto end;
1270 else if (!start && i->nItem < i->range.lower) goto end;
1272 i->nItem = i->range.upper;
1273 if (i->nItem > 0) goto testitem;
1274 end:
1275 return (i->nItem = i->nSpecial) != -1;
1278 static RANGE iterator_range(const ITERATOR *i)
1280 RANGE range;
1282 if (!i->ranges) return i->range;
1284 if (DPA_GetPtrCount(i->ranges->hdpa) > 0)
1286 range.lower = (*(RANGE*)DPA_GetPtr(i->ranges->hdpa, 0)).lower;
1287 range.upper = (*(RANGE*)DPA_GetPtr(i->ranges->hdpa, DPA_GetPtrCount(i->ranges->hdpa) - 1)).upper;
1289 else range.lower = range.upper = 0;
1291 return range;
1294 /***
1295 * Releases resources associated with this iterator.
1297 static inline void iterator_destroy(const ITERATOR *i)
1299 ranges_destroy(i->ranges);
1302 /***
1303 * Create an empty iterator.
1305 static inline BOOL iterator_empty(ITERATOR* i)
1307 ZeroMemory(i, sizeof(*i));
1308 i->nItem = i->nSpecial = i->range.lower = i->range.upper = -1;
1309 return TRUE;
1312 /***
1313 * Create an iterator over a range.
1315 static inline BOOL iterator_rangeitems(ITERATOR* i, RANGE range)
1317 iterator_empty(i);
1318 i->range = range;
1319 return TRUE;
1322 /***
1323 * Create an iterator over a bunch of ranges.
1324 * Please note that the iterator will take ownership of the ranges,
1325 * and will free them upon destruction.
1327 static inline BOOL iterator_rangesitems(ITERATOR* i, RANGES ranges)
1329 iterator_empty(i);
1330 i->ranges = ranges;
1331 return TRUE;
1334 /***
1335 * Creates an iterator over the items which intersect frame.
1336 * Uses absolute coordinates rather than compensating for the current offset.
1338 static BOOL iterator_frameditems_absolute(ITERATOR* i, const LISTVIEW_INFO* infoPtr, const RECT *frame)
1340 RECT rcItem, rcTemp;
1342 /* in case we fail, we want to return an empty iterator */
1343 if (!iterator_empty(i)) return FALSE;
1345 TRACE("(frame=%s)\n", wine_dbgstr_rect(frame));
1347 if (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON)
1349 INT nItem;
1351 if (infoPtr->uView == LV_VIEW_ICON && infoPtr->nFocusedItem != -1)
1353 LISTVIEW_GetItemBox(infoPtr, infoPtr->nFocusedItem, &rcItem);
1354 if (IntersectRect(&rcTemp, &rcItem, frame))
1355 i->nSpecial = infoPtr->nFocusedItem;
1357 if (!(iterator_rangesitems(i, ranges_create(50)))) return FALSE;
1358 /* to do better here, we need to have PosX, and PosY sorted */
1359 TRACE("building icon ranges:\n");
1360 for (nItem = 0; nItem < infoPtr->nItemCount; nItem++)
1362 rcItem.left = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosX, nItem);
1363 rcItem.top = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosY, nItem);
1364 rcItem.right = rcItem.left + infoPtr->nItemWidth;
1365 rcItem.bottom = rcItem.top + infoPtr->nItemHeight;
1366 if (IntersectRect(&rcTemp, &rcItem, frame))
1367 ranges_additem(i->ranges, nItem);
1369 return TRUE;
1371 else if (infoPtr->uView == LV_VIEW_DETAILS)
1373 RANGE range;
1375 if (frame->left >= infoPtr->nItemWidth) return TRUE;
1376 if (frame->top >= infoPtr->nItemHeight * infoPtr->nItemCount) return TRUE;
1378 range.lower = max(frame->top / infoPtr->nItemHeight, 0);
1379 range.upper = min((frame->bottom - 1) / infoPtr->nItemHeight, infoPtr->nItemCount - 1) + 1;
1380 if (range.upper <= range.lower) return TRUE;
1381 if (!iterator_rangeitems(i, range)) return FALSE;
1382 TRACE(" report=%s\n", debugrange(&i->range));
1384 else
1386 INT nPerCol = max((infoPtr->rcList.bottom - infoPtr->rcList.top) / infoPtr->nItemHeight, 1);
1387 INT nFirstRow = max(frame->top / infoPtr->nItemHeight, 0);
1388 INT nLastRow = min((frame->bottom - 1) / infoPtr->nItemHeight, nPerCol - 1);
1389 INT nFirstCol;
1390 INT nLastCol;
1391 INT lower;
1392 RANGE item_range;
1393 INT nCol;
1395 if (infoPtr->nItemWidth)
1397 nFirstCol = max(frame->left / infoPtr->nItemWidth, 0);
1398 nLastCol = min((frame->right - 1) / infoPtr->nItemWidth, (infoPtr->nItemCount + nPerCol - 1) / nPerCol);
1400 else
1402 nFirstCol = max(frame->left, 0);
1403 nLastCol = min(frame->right - 1, (infoPtr->nItemCount + nPerCol - 1) / nPerCol);
1406 lower = nFirstCol * nPerCol + nFirstRow;
1408 TRACE("nPerCol=%d, nFirstRow=%d, nLastRow=%d, nFirstCol=%d, nLastCol=%d, lower=%d\n",
1409 nPerCol, nFirstRow, nLastRow, nFirstCol, nLastCol, lower);
1411 if (nLastCol < nFirstCol || nLastRow < nFirstRow) return TRUE;
1413 if (!(iterator_rangesitems(i, ranges_create(nLastCol - nFirstCol + 1)))) return FALSE;
1414 TRACE("building list ranges:\n");
1415 for (nCol = nFirstCol; nCol <= nLastCol; nCol++)
1417 item_range.lower = nCol * nPerCol + nFirstRow;
1418 if(item_range.lower >= infoPtr->nItemCount) break;
1419 item_range.upper = min(nCol * nPerCol + nLastRow + 1, infoPtr->nItemCount);
1420 TRACE(" list=%s\n", debugrange(&item_range));
1421 ranges_add(i->ranges, item_range);
1425 return TRUE;
1428 /***
1429 * Creates an iterator over the items which intersect lprc.
1431 static BOOL iterator_frameditems(ITERATOR* i, const LISTVIEW_INFO* infoPtr, const RECT *lprc)
1433 RECT frame = *lprc;
1434 POINT Origin;
1436 TRACE("(lprc=%s)\n", wine_dbgstr_rect(lprc));
1438 LISTVIEW_GetOrigin(infoPtr, &Origin);
1439 OffsetRect(&frame, -Origin.x, -Origin.y);
1441 return iterator_frameditems_absolute(i, infoPtr, &frame);
1444 /***
1445 * Creates an iterator over the items which intersect the visible region of hdc.
1447 static BOOL iterator_visibleitems(ITERATOR *i, const LISTVIEW_INFO *infoPtr, HDC hdc)
1449 POINT Origin, Position;
1450 RECT rcItem, rcClip;
1451 INT rgntype;
1453 rgntype = GetClipBox(hdc, &rcClip);
1454 if (rgntype == NULLREGION) return iterator_empty(i);
1455 if (!iterator_frameditems(i, infoPtr, &rcClip)) return FALSE;
1456 if (rgntype == SIMPLEREGION) return TRUE;
1458 /* first deal with the special item */
1459 if (i->nSpecial != -1)
1461 LISTVIEW_GetItemBox(infoPtr, i->nSpecial, &rcItem);
1462 if (!RectVisible(hdc, &rcItem)) i->nSpecial = -1;
1465 /* if we can't deal with the region, we'll just go with the simple range */
1466 LISTVIEW_GetOrigin(infoPtr, &Origin);
1467 TRACE("building visible range:\n");
1468 if (!i->ranges && i->range.lower < i->range.upper)
1470 if (!(i->ranges = ranges_create(50))) return TRUE;
1471 if (!ranges_add(i->ranges, i->range))
1473 ranges_destroy(i->ranges);
1474 i->ranges = 0;
1475 return TRUE;
1479 /* now delete the invisible items from the list */
1480 while(iterator_next(i))
1482 LISTVIEW_GetItemOrigin(infoPtr, i->nItem, &Position);
1483 rcItem.left = (infoPtr->uView == LV_VIEW_DETAILS) ? Origin.x : Position.x + Origin.x;
1484 rcItem.top = Position.y + Origin.y;
1485 rcItem.right = rcItem.left + infoPtr->nItemWidth;
1486 rcItem.bottom = rcItem.top + infoPtr->nItemHeight;
1487 if (!RectVisible(hdc, &rcItem))
1488 ranges_delitem(i->ranges, i->nItem);
1490 /* the iterator should restart on the next iterator_next */
1491 TRACE("done\n");
1493 return TRUE;
1496 /* Remove common elements from two iterators */
1497 /* Passed iterators have to point on the first elements */
1498 static BOOL iterator_remove_common_items(ITERATOR *iter1, ITERATOR *iter2)
1500 if(!iter1->ranges || !iter2->ranges) {
1501 int lower, upper;
1503 if(iter1->ranges || iter2->ranges ||
1504 (iter1->range.lower<iter2->range.lower && iter1->range.upper>iter2->range.upper) ||
1505 (iter1->range.lower>iter2->range.lower && iter1->range.upper<iter2->range.upper)) {
1506 ERR("result is not a one range iterator\n");
1507 return FALSE;
1510 if(iter1->range.lower==-1 || iter2->range.lower==-1)
1511 return TRUE;
1513 lower = iter1->range.lower;
1514 upper = iter1->range.upper;
1516 if(lower < iter2->range.lower)
1517 iter1->range.upper = iter2->range.lower;
1518 else if(upper > iter2->range.upper)
1519 iter1->range.lower = iter2->range.upper;
1520 else
1521 iter1->range.lower = iter1->range.upper = -1;
1523 if(iter2->range.lower < lower)
1524 iter2->range.upper = lower;
1525 else if(iter2->range.upper > upper)
1526 iter2->range.lower = upper;
1527 else
1528 iter2->range.lower = iter2->range.upper = -1;
1530 return TRUE;
1533 iterator_next(iter1);
1534 iterator_next(iter2);
1536 while(1) {
1537 if(iter1->nItem==-1 || iter2->nItem==-1)
1538 break;
1540 if(iter1->nItem == iter2->nItem) {
1541 int delete = iter1->nItem;
1543 iterator_prev(iter1);
1544 iterator_prev(iter2);
1545 ranges_delitem(iter1->ranges, delete);
1546 ranges_delitem(iter2->ranges, delete);
1547 iterator_next(iter1);
1548 iterator_next(iter2);
1549 } else if(iter1->nItem > iter2->nItem)
1550 iterator_next(iter2);
1551 else
1552 iterator_next(iter1);
1555 iter1->nItem = iter1->range.lower = iter1->range.upper = -1;
1556 iter2->nItem = iter2->range.lower = iter2->range.upper = -1;
1557 return TRUE;
1560 /******** Misc helper functions ************************************/
1562 static inline LRESULT CallWindowProcT(WNDPROC proc, HWND hwnd, UINT uMsg,
1563 WPARAM wParam, LPARAM lParam, BOOL isW)
1565 if (isW) return CallWindowProcW(proc, hwnd, uMsg, wParam, lParam);
1566 else return CallWindowProcA(proc, hwnd, uMsg, wParam, lParam);
1569 static inline BOOL is_autoarrange(const LISTVIEW_INFO *infoPtr)
1571 return ((infoPtr->dwStyle & LVS_AUTOARRANGE) || infoPtr->bAutoarrange) &&
1572 (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON);
1575 static void toggle_checkbox_state(LISTVIEW_INFO *infoPtr, INT nItem)
1577 DWORD state = STATEIMAGEINDEX(LISTVIEW_GetItemState(infoPtr, nItem, LVIS_STATEIMAGEMASK));
1578 if(state == 1 || state == 2)
1580 LVITEMW lvitem;
1581 state ^= 3;
1582 lvitem.state = INDEXTOSTATEIMAGEMASK(state);
1583 lvitem.stateMask = LVIS_STATEIMAGEMASK;
1584 LISTVIEW_SetItemState(infoPtr, nItem, &lvitem);
1588 /* this should be called after window style got updated,
1589 it used to reset view state to match current window style */
1590 static inline void map_style_view(LISTVIEW_INFO *infoPtr)
1592 switch (infoPtr->dwStyle & LVS_TYPEMASK)
1594 case LVS_ICON:
1595 infoPtr->uView = LV_VIEW_ICON;
1596 break;
1597 case LVS_REPORT:
1598 infoPtr->uView = LV_VIEW_DETAILS;
1599 break;
1600 case LVS_SMALLICON:
1601 infoPtr->uView = LV_VIEW_SMALLICON;
1602 break;
1603 case LVS_LIST:
1604 infoPtr->uView = LV_VIEW_LIST;
1608 /* computes next item id value */
1609 static DWORD get_next_itemid(const LISTVIEW_INFO *infoPtr)
1611 INT count = DPA_GetPtrCount(infoPtr->hdpaItemIds);
1613 if (count > 0)
1615 ITEM_ID *lpID = DPA_GetPtr(infoPtr->hdpaItemIds, count - 1);
1616 return lpID->id + 1;
1618 return 0;
1621 /******** Internal API functions ************************************/
1623 static inline COLUMN_INFO * LISTVIEW_GetColumnInfo(const LISTVIEW_INFO *infoPtr, INT nSubItem)
1625 static COLUMN_INFO mainItem;
1627 if (nSubItem == 0 && DPA_GetPtrCount(infoPtr->hdpaColumns) == 0) return &mainItem;
1628 assert (nSubItem >= 0 && nSubItem < DPA_GetPtrCount(infoPtr->hdpaColumns));
1630 /* update cached column rectangles */
1631 if (infoPtr->colRectsDirty)
1633 COLUMN_INFO *info;
1634 LISTVIEW_INFO *Ptr = (LISTVIEW_INFO*)infoPtr;
1635 INT i;
1637 for (i = 0; i < DPA_GetPtrCount(infoPtr->hdpaColumns); i++) {
1638 info = DPA_GetPtr(infoPtr->hdpaColumns, i);
1639 SendMessageW(infoPtr->hwndHeader, HDM_GETITEMRECT, i, (LPARAM)&info->rcHeader);
1641 Ptr->colRectsDirty = FALSE;
1644 return DPA_GetPtr(infoPtr->hdpaColumns, nSubItem);
1647 static INT LISTVIEW_CreateHeader(LISTVIEW_INFO *infoPtr)
1649 DWORD dFlags = WS_CHILD | HDS_HORZ | HDS_FULLDRAG | HDS_DRAGDROP;
1650 HINSTANCE hInst;
1652 if (infoPtr->hwndHeader) return 0;
1654 TRACE("Creating header for list %p\n", infoPtr->hwndSelf);
1656 /* setup creation flags */
1657 dFlags |= (LVS_NOSORTHEADER & infoPtr->dwStyle) ? 0 : HDS_BUTTONS;
1658 dFlags |= (LVS_NOCOLUMNHEADER & infoPtr->dwStyle) ? HDS_HIDDEN : 0;
1660 hInst = (HINSTANCE)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_HINSTANCE);
1662 /* create header */
1663 infoPtr->hwndHeader = CreateWindowW(WC_HEADERW, NULL, dFlags,
1664 0, 0, 0, 0, infoPtr->hwndSelf, NULL, hInst, NULL);
1665 if (!infoPtr->hwndHeader) return -1;
1667 /* set header unicode format */
1668 SendMessageW(infoPtr->hwndHeader, HDM_SETUNICODEFORMAT, TRUE, 0);
1670 /* set header font */
1671 SendMessageW(infoPtr->hwndHeader, WM_SETFONT, (WPARAM)infoPtr->hFont, TRUE);
1673 /* set header image list */
1674 if (infoPtr->himlSmall)
1675 SendMessageW(infoPtr->hwndHeader, HDM_SETIMAGELIST, 0, (LPARAM)infoPtr->himlSmall);
1677 LISTVIEW_UpdateSize(infoPtr);
1679 return 0;
1682 static inline void LISTVIEW_GetHeaderRect(const LISTVIEW_INFO *infoPtr, INT nSubItem, LPRECT lprc)
1684 *lprc = LISTVIEW_GetColumnInfo(infoPtr, nSubItem)->rcHeader;
1687 static inline BOOL LISTVIEW_IsHeaderEnabled(const LISTVIEW_INFO *infoPtr)
1689 return (infoPtr->uView == LV_VIEW_DETAILS ||
1690 infoPtr->dwLvExStyle & LVS_EX_HEADERINALLVIEWS) &&
1691 !(infoPtr->dwStyle & LVS_NOCOLUMNHEADER);
1694 static inline BOOL LISTVIEW_GetItemW(const LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem)
1696 return LISTVIEW_GetItemT(infoPtr, lpLVItem, TRUE);
1699 /* used to handle collapse main item column case */
1700 static inline BOOL LISTVIEW_DrawFocusRect(const LISTVIEW_INFO *infoPtr, HDC hdc)
1702 return (infoPtr->rcFocus.left < infoPtr->rcFocus.right) ?
1703 DrawFocusRect(hdc, &infoPtr->rcFocus) : FALSE;
1706 /* Listview invalidation functions: use _only_ these functions to invalidate */
1708 static inline BOOL is_redrawing(const LISTVIEW_INFO *infoPtr)
1710 return infoPtr->bRedraw;
1713 static inline void LISTVIEW_InvalidateRect(const LISTVIEW_INFO *infoPtr, const RECT* rect)
1715 if(!is_redrawing(infoPtr)) return;
1716 TRACE(" invalidating rect=%s\n", wine_dbgstr_rect(rect));
1717 InvalidateRect(infoPtr->hwndSelf, rect, TRUE);
1720 static inline void LISTVIEW_InvalidateItem(const LISTVIEW_INFO *infoPtr, INT nItem)
1722 RECT rcBox;
1724 if(!is_redrawing(infoPtr)) return;
1725 LISTVIEW_GetItemBox(infoPtr, nItem, &rcBox);
1726 LISTVIEW_InvalidateRect(infoPtr, &rcBox);
1729 static inline void LISTVIEW_InvalidateSubItem(const LISTVIEW_INFO *infoPtr, INT nItem, INT nSubItem)
1731 POINT Origin, Position;
1732 RECT rcBox;
1734 if(!is_redrawing(infoPtr)) return;
1735 assert (infoPtr->uView == LV_VIEW_DETAILS);
1736 LISTVIEW_GetOrigin(infoPtr, &Origin);
1737 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
1738 LISTVIEW_GetHeaderRect(infoPtr, nSubItem, &rcBox);
1739 rcBox.top = 0;
1740 rcBox.bottom = infoPtr->nItemHeight;
1741 OffsetRect(&rcBox, Origin.x + Position.x, Origin.y + Position.y);
1742 LISTVIEW_InvalidateRect(infoPtr, &rcBox);
1745 static inline void LISTVIEW_InvalidateList(const LISTVIEW_INFO *infoPtr)
1747 LISTVIEW_InvalidateRect(infoPtr, NULL);
1750 static inline void LISTVIEW_InvalidateColumn(const LISTVIEW_INFO *infoPtr, INT nColumn)
1752 RECT rcCol;
1754 if(!is_redrawing(infoPtr)) return;
1755 LISTVIEW_GetHeaderRect(infoPtr, nColumn, &rcCol);
1756 rcCol.top = infoPtr->rcList.top;
1757 rcCol.bottom = infoPtr->rcList.bottom;
1758 LISTVIEW_InvalidateRect(infoPtr, &rcCol);
1761 /***
1762 * DESCRIPTION:
1763 * Retrieves the number of items that can fit vertically in the client area.
1765 * PARAMETER(S):
1766 * [I] infoPtr : valid pointer to the listview structure
1768 * RETURN:
1769 * Number of items per row.
1771 static inline INT LISTVIEW_GetCountPerRow(const LISTVIEW_INFO *infoPtr)
1773 INT nListWidth = infoPtr->rcList.right - infoPtr->rcList.left;
1775 return max(nListWidth/(infoPtr->nItemWidth ? infoPtr->nItemWidth : 1), 1);
1778 /***
1779 * DESCRIPTION:
1780 * Retrieves the number of items that can fit horizontally in the client
1781 * area.
1783 * PARAMETER(S):
1784 * [I] infoPtr : valid pointer to the listview structure
1786 * RETURN:
1787 * Number of items per column.
1789 static inline INT LISTVIEW_GetCountPerColumn(const LISTVIEW_INFO *infoPtr)
1791 INT nListHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
1793 return max(nListHeight / infoPtr->nItemHeight, 1);
1797 /*************************************************************************
1798 * LISTVIEW_ProcessLetterKeys
1800 * Processes keyboard messages generated by pressing the letter keys
1801 * on the keyboard.
1802 * What this does is perform a case insensitive search from the
1803 * current position with the following quirks:
1804 * - If two chars or more are pressed in quick succession we search
1805 * for the corresponding string (e.g. 'abc').
1806 * - If there is a delay we wipe away the current search string and
1807 * restart with just that char.
1808 * - If the user keeps pressing the same character, whether slowly or
1809 * fast, so that the search string is entirely composed of this
1810 * character ('aaaaa' for instance), then we search for first item
1811 * that starting with that character.
1812 * - If the user types the above character in quick succession, then
1813 * we must also search for the corresponding string ('aaaaa'), and
1814 * go to that string if there is a match.
1816 * PARAMETERS
1817 * [I] hwnd : handle to the window
1818 * [I] charCode : the character code, the actual character
1819 * [I] keyData : key data
1821 * RETURNS
1823 * Zero.
1825 * BUGS
1827 * - The current implementation has a list of characters it will
1828 * accept and it ignores everything else. In particular it will
1829 * ignore accentuated characters which seems to match what
1830 * Windows does. But I'm not sure it makes sense to follow
1831 * Windows there.
1832 * - We don't sound a beep when the search fails.
1834 * SEE ALSO
1836 * TREEVIEW_ProcessLetterKeys
1838 static INT LISTVIEW_ProcessLetterKeys(LISTVIEW_INFO *infoPtr, WPARAM charCode, LPARAM keyData)
1840 WCHAR buffer[MAX_PATH];
1841 DWORD prevTime;
1842 LVITEMW item;
1843 int startidx;
1844 INT nItem;
1845 INT diff;
1847 /* simple parameter checking */
1848 if (!charCode || !keyData || infoPtr->nItemCount == 0) return 0;
1850 /* only allow the valid WM_CHARs through */
1851 if (!isalnumW(charCode) &&
1852 charCode != '.' && charCode != '`' && charCode != '!' &&
1853 charCode != '@' && charCode != '#' && charCode != '$' &&
1854 charCode != '%' && charCode != '^' && charCode != '&' &&
1855 charCode != '*' && charCode != '(' && charCode != ')' &&
1856 charCode != '-' && charCode != '_' && charCode != '+' &&
1857 charCode != '=' && charCode != '\\'&& charCode != ']' &&
1858 charCode != '}' && charCode != '[' && charCode != '{' &&
1859 charCode != '/' && charCode != '?' && charCode != '>' &&
1860 charCode != '<' && charCode != ',' && charCode != '~')
1861 return 0;
1863 /* update the search parameters */
1864 prevTime = infoPtr->lastKeyPressTimestamp;
1865 infoPtr->lastKeyPressTimestamp = GetTickCount();
1866 diff = infoPtr->lastKeyPressTimestamp - prevTime;
1868 if (diff >= 0 && diff < KEY_DELAY)
1870 if (infoPtr->nSearchParamLength < MAX_PATH - 1)
1871 infoPtr->szSearchParam[infoPtr->nSearchParamLength++] = charCode;
1873 if (infoPtr->charCode != charCode)
1874 infoPtr->charCode = charCode = 0;
1876 else
1878 infoPtr->charCode = charCode;
1879 infoPtr->szSearchParam[0] = charCode;
1880 infoPtr->nSearchParamLength = 1;
1883 /* should start from next after focused item, so next item that matches
1884 will be selected, if there isn't any and focused matches it will be selected
1885 on second search stage from beginning of the list */
1886 if (infoPtr->nFocusedItem >= 0 && infoPtr->nItemCount > 1)
1888 /* with some accumulated search data available start with current focus, otherwise
1889 it's excluded from search */
1890 startidx = infoPtr->nSearchParamLength > 1 ? infoPtr->nFocusedItem : infoPtr->nFocusedItem + 1;
1891 if (startidx == infoPtr->nItemCount) startidx = 0;
1893 else
1894 startidx = 0;
1896 /* let application handle this for virtual listview */
1897 if (infoPtr->dwStyle & LVS_OWNERDATA)
1899 NMLVFINDITEMW nmlv;
1901 memset(&nmlv.lvfi, 0, sizeof(nmlv.lvfi));
1902 nmlv.lvfi.flags = (LVFI_WRAP | LVFI_PARTIAL);
1903 nmlv.lvfi.psz = infoPtr->szSearchParam;
1904 nmlv.iStart = startidx;
1906 infoPtr->szSearchParam[infoPtr->nSearchParamLength] = 0;
1908 nItem = notify_hdr(infoPtr, LVN_ODFINDITEMW, (LPNMHDR)&nmlv.hdr);
1910 else
1912 int i = startidx, endidx;
1914 /* and search from the current position */
1915 nItem = -1;
1916 endidx = infoPtr->nItemCount;
1918 /* first search in [startidx, endidx), on failure continue in [0, startidx) */
1919 while (1)
1921 /* start from first item if not found with >= startidx */
1922 if (i == infoPtr->nItemCount && startidx > 0)
1924 endidx = startidx;
1925 startidx = 0;
1928 for (i = startidx; i < endidx; i++)
1930 /* retrieve text */
1931 item.mask = LVIF_TEXT;
1932 item.iItem = i;
1933 item.iSubItem = 0;
1934 item.pszText = buffer;
1935 item.cchTextMax = MAX_PATH;
1936 if (!LISTVIEW_GetItemW(infoPtr, &item)) return 0;
1938 if (!lstrncmpiW(item.pszText, infoPtr->szSearchParam, infoPtr->nSearchParamLength))
1940 nItem = i;
1941 break;
1943 /* this is used to find first char match when search string is not available yet,
1944 otherwise every WM_CHAR will search to next item by first char, ignoring that we're
1945 already waiting for user to complete a string */
1946 else if (nItem == -1 && infoPtr->nSearchParamLength == 1 && !lstrncmpiW(item.pszText, infoPtr->szSearchParam, 1))
1948 /* this would work but we must keep looking for a longer match */
1949 nItem = i;
1953 if ( nItem != -1 || /* found something */
1954 endidx != infoPtr->nItemCount || /* second search done */
1955 (startidx == 0 && endidx == infoPtr->nItemCount) /* full range for first search */ )
1956 break;
1960 if (nItem != -1)
1961 LISTVIEW_KeySelection(infoPtr, nItem, FALSE);
1963 return 0;
1966 /*************************************************************************
1967 * LISTVIEW_UpdateHeaderSize [Internal]
1969 * Function to resize the header control
1971 * PARAMS
1972 * [I] hwnd : handle to a window
1973 * [I] nNewScrollPos : scroll pos to set
1975 * RETURNS
1976 * None.
1978 static void LISTVIEW_UpdateHeaderSize(const LISTVIEW_INFO *infoPtr, INT nNewScrollPos)
1980 RECT winRect;
1981 POINT point[2];
1983 TRACE("nNewScrollPos=%d\n", nNewScrollPos);
1985 if (!infoPtr->hwndHeader) return;
1987 GetWindowRect(infoPtr->hwndHeader, &winRect);
1988 point[0].x = winRect.left;
1989 point[0].y = winRect.top;
1990 point[1].x = winRect.right;
1991 point[1].y = winRect.bottom;
1993 MapWindowPoints(HWND_DESKTOP, infoPtr->hwndSelf, point, 2);
1994 point[0].x = -nNewScrollPos;
1995 point[1].x += nNewScrollPos;
1997 SetWindowPos(infoPtr->hwndHeader,0,
1998 point[0].x,point[0].y,point[1].x,point[1].y,
1999 (infoPtr->dwStyle & LVS_NOCOLUMNHEADER) ? SWP_HIDEWINDOW : SWP_SHOWWINDOW |
2000 SWP_NOZORDER | SWP_NOACTIVATE);
2003 /***
2004 * DESCRIPTION:
2005 * Update the scrollbars. This functions should be called whenever
2006 * the content, size or view changes.
2008 * PARAMETER(S):
2009 * [I] infoPtr : valid pointer to the listview structure
2011 * RETURN:
2012 * None
2014 static void LISTVIEW_UpdateScroll(const LISTVIEW_INFO *infoPtr)
2016 SCROLLINFO horzInfo, vertInfo;
2017 INT dx, dy;
2019 if ((infoPtr->dwStyle & LVS_NOSCROLL) || !is_redrawing(infoPtr)) return;
2021 ZeroMemory(&horzInfo, sizeof(SCROLLINFO));
2022 horzInfo.cbSize = sizeof(SCROLLINFO);
2023 horzInfo.nPage = infoPtr->rcList.right - infoPtr->rcList.left;
2025 /* for now, we'll set info.nMax to the _count_, and adjust it later */
2026 if (infoPtr->uView == LV_VIEW_LIST)
2028 INT nPerCol = LISTVIEW_GetCountPerColumn(infoPtr);
2029 horzInfo.nMax = (infoPtr->nItemCount + nPerCol - 1) / nPerCol;
2031 /* scroll by at least one column per page */
2032 if(horzInfo.nPage < infoPtr->nItemWidth)
2033 horzInfo.nPage = infoPtr->nItemWidth;
2035 if (infoPtr->nItemWidth)
2036 horzInfo.nPage /= infoPtr->nItemWidth;
2038 else if (infoPtr->uView == LV_VIEW_DETAILS)
2040 horzInfo.nMax = infoPtr->nItemWidth;
2042 else /* LV_VIEW_ICON, or LV_VIEW_SMALLICON */
2044 RECT rcView;
2046 if (LISTVIEW_GetViewRect(infoPtr, &rcView)) horzInfo.nMax = rcView.right - rcView.left;
2049 if (LISTVIEW_IsHeaderEnabled(infoPtr))
2051 if (DPA_GetPtrCount(infoPtr->hdpaColumns))
2053 RECT rcHeader;
2054 INT index;
2056 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX,
2057 DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, 0);
2059 LISTVIEW_GetHeaderRect(infoPtr, index, &rcHeader);
2060 horzInfo.nMax = rcHeader.right;
2061 TRACE("horzInfo.nMax=%d\n", horzInfo.nMax);
2065 horzInfo.fMask = SIF_RANGE | SIF_PAGE;
2066 horzInfo.nMax = max(horzInfo.nMax - 1, 0);
2067 dx = GetScrollPos(infoPtr->hwndSelf, SB_HORZ);
2068 dx -= SetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &horzInfo, TRUE);
2069 TRACE("horzInfo=%s\n", debugscrollinfo(&horzInfo));
2071 /* Setting the horizontal scroll can change the listview size
2072 * (and potentially everything else) so we need to recompute
2073 * everything again for the vertical scroll
2076 ZeroMemory(&vertInfo, sizeof(SCROLLINFO));
2077 vertInfo.cbSize = sizeof(SCROLLINFO);
2078 vertInfo.nPage = infoPtr->rcList.bottom - infoPtr->rcList.top;
2080 if (infoPtr->uView == LV_VIEW_DETAILS)
2082 vertInfo.nMax = infoPtr->nItemCount;
2084 /* scroll by at least one page */
2085 if(vertInfo.nPage < infoPtr->nItemHeight)
2086 vertInfo.nPage = infoPtr->nItemHeight;
2088 if (infoPtr->nItemHeight > 0)
2089 vertInfo.nPage /= infoPtr->nItemHeight;
2091 else if (infoPtr->uView != LV_VIEW_LIST) /* LV_VIEW_ICON, or LV_VIEW_SMALLICON */
2093 RECT rcView;
2095 if (LISTVIEW_GetViewRect(infoPtr, &rcView)) vertInfo.nMax = rcView.bottom - rcView.top;
2098 vertInfo.fMask = SIF_RANGE | SIF_PAGE;
2099 vertInfo.nMax = max(vertInfo.nMax - 1, 0);
2100 dy = GetScrollPos(infoPtr->hwndSelf, SB_VERT);
2101 dy -= SetScrollInfo(infoPtr->hwndSelf, SB_VERT, &vertInfo, TRUE);
2102 TRACE("vertInfo=%s\n", debugscrollinfo(&vertInfo));
2104 /* Change of the range may have changed the scroll pos. If so move the content */
2105 if (dx != 0 || dy != 0)
2107 RECT listRect;
2108 listRect = infoPtr->rcList;
2109 ScrollWindowEx(infoPtr->hwndSelf, dx, dy, &listRect, &listRect, 0, 0,
2110 SW_ERASE | SW_INVALIDATE);
2113 /* Update the Header Control */
2114 if (infoPtr->hwndHeader)
2116 horzInfo.fMask = SIF_POS;
2117 GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &horzInfo);
2118 LISTVIEW_UpdateHeaderSize(infoPtr, horzInfo.nPos);
2123 /***
2124 * DESCRIPTION:
2125 * Shows/hides the focus rectangle.
2127 * PARAMETER(S):
2128 * [I] infoPtr : valid pointer to the listview structure
2129 * [I] fShow : TRUE to show the focus, FALSE to hide it.
2131 * RETURN:
2132 * None
2134 static void LISTVIEW_ShowFocusRect(const LISTVIEW_INFO *infoPtr, BOOL fShow)
2136 HDC hdc;
2138 TRACE("fShow=%d, nItem=%d\n", fShow, infoPtr->nFocusedItem);
2140 if (infoPtr->nFocusedItem < 0) return;
2142 /* we need some gymnastics in ICON mode to handle large items */
2143 if (infoPtr->uView == LV_VIEW_ICON)
2145 RECT rcBox;
2147 LISTVIEW_GetItemBox(infoPtr, infoPtr->nFocusedItem, &rcBox);
2148 if ((rcBox.bottom - rcBox.top) > infoPtr->nItemHeight)
2150 LISTVIEW_InvalidateRect(infoPtr, &rcBox);
2151 return;
2155 if (!(hdc = GetDC(infoPtr->hwndSelf))) return;
2157 /* for some reason, owner draw should work only in report mode */
2158 if ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (infoPtr->uView == LV_VIEW_DETAILS))
2160 DRAWITEMSTRUCT dis;
2161 LVITEMW item;
2163 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
2164 HFONT hOldFont = SelectObject(hdc, hFont);
2166 item.iItem = infoPtr->nFocusedItem;
2167 item.iSubItem = 0;
2168 item.mask = LVIF_PARAM;
2169 if (!LISTVIEW_GetItemW(infoPtr, &item)) goto done;
2171 ZeroMemory(&dis, sizeof(dis));
2172 dis.CtlType = ODT_LISTVIEW;
2173 dis.CtlID = (UINT)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
2174 dis.itemID = item.iItem;
2175 dis.itemAction = ODA_FOCUS;
2176 if (fShow) dis.itemState |= ODS_FOCUS;
2177 dis.hwndItem = infoPtr->hwndSelf;
2178 dis.hDC = hdc;
2179 LISTVIEW_GetItemBox(infoPtr, dis.itemID, &dis.rcItem);
2180 dis.itemData = item.lParam;
2182 SendMessageW(infoPtr->hwndNotify, WM_DRAWITEM, dis.CtlID, (LPARAM)&dis);
2184 SelectObject(hdc, hOldFont);
2186 else
2188 LISTVIEW_DrawFocusRect(infoPtr, hdc);
2190 done:
2191 ReleaseDC(infoPtr->hwndSelf, hdc);
2194 /***
2195 * Invalidates all visible selected items.
2197 static void LISTVIEW_InvalidateSelectedItems(const LISTVIEW_INFO *infoPtr)
2199 ITERATOR i;
2201 iterator_frameditems(&i, infoPtr, &infoPtr->rcList);
2202 while(iterator_next(&i))
2204 if (LISTVIEW_GetItemState(infoPtr, i.nItem, LVIS_SELECTED))
2205 LISTVIEW_InvalidateItem(infoPtr, i.nItem);
2207 iterator_destroy(&i);
2211 /***
2212 * DESCRIPTION: [INTERNAL]
2213 * Computes an item's (left,top) corner, relative to rcView.
2214 * That is, the position has NOT been made relative to the Origin.
2215 * This is deliberate, to avoid computing the Origin over, and
2216 * over again, when this function is called in a loop. Instead,
2217 * one can factor the computation of the Origin before the loop,
2218 * and offset the value returned by this function, on every iteration.
2220 * PARAMETER(S):
2221 * [I] infoPtr : valid pointer to the listview structure
2222 * [I] nItem : item number
2223 * [O] lpptOrig : item top, left corner
2225 * RETURN:
2226 * None.
2228 static void LISTVIEW_GetItemOrigin(const LISTVIEW_INFO *infoPtr, INT nItem, LPPOINT lpptPosition)
2230 assert(nItem >= 0 && nItem < infoPtr->nItemCount);
2232 if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
2234 lpptPosition->x = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosX, nItem);
2235 lpptPosition->y = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosY, nItem);
2237 else if (infoPtr->uView == LV_VIEW_LIST)
2239 INT nCountPerColumn = LISTVIEW_GetCountPerColumn(infoPtr);
2240 lpptPosition->x = nItem / nCountPerColumn * infoPtr->nItemWidth;
2241 lpptPosition->y = nItem % nCountPerColumn * infoPtr->nItemHeight;
2243 else /* LV_VIEW_DETAILS */
2245 lpptPosition->x = REPORT_MARGINX;
2246 /* item is always at zero indexed column */
2247 if (DPA_GetPtrCount(infoPtr->hdpaColumns) > 0)
2248 lpptPosition->x += LISTVIEW_GetColumnInfo(infoPtr, 0)->rcHeader.left;
2249 lpptPosition->y = nItem * infoPtr->nItemHeight;
2253 /***
2254 * DESCRIPTION: [INTERNAL]
2255 * Compute the rectangles of an item. This is to localize all
2256 * the computations in one place. If you are not interested in some
2257 * of these values, simply pass in a NULL -- the function is smart
2258 * enough to compute only what's necessary. The function computes
2259 * the standard rectangles (BOUNDS, ICON, LABEL) plus a non-standard
2260 * one, the BOX rectangle. This rectangle is very cheap to compute,
2261 * and is guaranteed to contain all the other rectangles. Computing
2262 * the ICON rect is also cheap, but all the others are potentially
2263 * expensive. This gives an easy and effective optimization when
2264 * searching (like point inclusion, or rectangle intersection):
2265 * first test against the BOX, and if TRUE, test against the desired
2266 * rectangle.
2267 * If the function does not have all the necessary information
2268 * to computed the requested rectangles, will crash with a
2269 * failed assertion. This is done so we catch all programming
2270 * errors, given that the function is called only from our code.
2272 * We have the following 'special' meanings for a few fields:
2273 * * If LVIS_FOCUSED is set, we assume the item has the focus
2274 * This is important in ICON mode, where it might get a larger
2275 * then usual rectangle
2277 * Please note that subitem support works only in REPORT mode.
2279 * PARAMETER(S):
2280 * [I] infoPtr : valid pointer to the listview structure
2281 * [I] lpLVItem : item to compute the measures for
2282 * [O] lprcBox : ptr to Box rectangle
2283 * Same as LVM_GETITEMRECT with LVIR_BOUNDS
2284 * [0] lprcSelectBox : ptr to select box rectangle
2285 * Same as LVM_GETITEMRECT with LVIR_SELECTEDBOUNDS
2286 * [O] lprcIcon : ptr to Icon rectangle
2287 * Same as LVM_GETITEMRECT with LVIR_ICON
2288 * [O] lprcStateIcon: ptr to State Icon rectangle
2289 * [O] lprcLabel : ptr to Label rectangle
2290 * Same as LVM_GETITEMRECT with LVIR_LABEL
2292 * RETURN:
2293 * None.
2295 static void LISTVIEW_GetItemMetrics(const LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem,
2296 LPRECT lprcBox, LPRECT lprcSelectBox,
2297 LPRECT lprcIcon, LPRECT lprcStateIcon, LPRECT lprcLabel)
2299 BOOL doSelectBox = FALSE, doIcon = FALSE, doLabel = FALSE, oversizedBox = FALSE;
2300 RECT Box, SelectBox, Icon, Label;
2301 COLUMN_INFO *lpColumnInfo = NULL;
2302 SIZE labelSize = { 0, 0 };
2304 TRACE("(lpLVItem=%s)\n", debuglvitem_t(lpLVItem, TRUE));
2306 /* Be smart and try to figure out the minimum we have to do */
2307 if (lpLVItem->iSubItem) assert(infoPtr->uView == LV_VIEW_DETAILS);
2308 if (infoPtr->uView == LV_VIEW_ICON && (lprcBox || lprcLabel))
2310 assert((lpLVItem->mask & LVIF_STATE) && (lpLVItem->stateMask & LVIS_FOCUSED));
2311 if (lpLVItem->state & LVIS_FOCUSED) oversizedBox = doLabel = TRUE;
2313 if (lprcSelectBox) doSelectBox = TRUE;
2314 if (lprcLabel) doLabel = TRUE;
2315 if (doLabel || lprcIcon || lprcStateIcon) doIcon = TRUE;
2316 if (doSelectBox)
2318 doIcon = TRUE;
2319 doLabel = TRUE;
2322 /************************************************************/
2323 /* compute the box rectangle (it should be cheap to do) */
2324 /************************************************************/
2325 if (lpLVItem->iSubItem || infoPtr->uView == LV_VIEW_DETAILS)
2326 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, lpLVItem->iSubItem);
2328 if (lpLVItem->iSubItem)
2330 Box = lpColumnInfo->rcHeader;
2332 else
2334 Box.left = 0;
2335 Box.right = infoPtr->nItemWidth;
2337 Box.top = 0;
2338 Box.bottom = infoPtr->nItemHeight;
2340 /******************************************************************/
2341 /* compute ICON bounding box (ala LVM_GETITEMRECT) and STATEICON */
2342 /******************************************************************/
2343 if (doIcon)
2345 LONG state_width = 0;
2347 if (infoPtr->himlState && lpLVItem->iSubItem == 0)
2348 state_width = infoPtr->iconStateSize.cx;
2350 if (infoPtr->uView == LV_VIEW_ICON)
2352 Icon.left = Box.left + state_width;
2353 if (infoPtr->himlNormal)
2354 Icon.left += (infoPtr->nItemWidth - infoPtr->iconSize.cx - state_width) / 2;
2355 Icon.top = Box.top + ICON_TOP_PADDING;
2356 Icon.right = Icon.left;
2357 Icon.bottom = Icon.top;
2358 if (infoPtr->himlNormal)
2360 Icon.right += infoPtr->iconSize.cx;
2361 Icon.bottom += infoPtr->iconSize.cy;
2364 else /* LV_VIEW_SMALLICON, LV_VIEW_LIST or LV_VIEW_DETAILS */
2366 Icon.left = Box.left + state_width;
2368 if (infoPtr->uView == LV_VIEW_DETAILS && lpLVItem->iSubItem == 0)
2370 /* we need the indent in report mode */
2371 assert(lpLVItem->mask & LVIF_INDENT);
2372 Icon.left += infoPtr->iconSize.cx * lpLVItem->iIndent + REPORT_MARGINX;
2375 Icon.top = Box.top;
2376 Icon.right = Icon.left;
2377 if (infoPtr->himlSmall &&
2378 (!lpColumnInfo || lpLVItem->iSubItem == 0 ||
2379 ((infoPtr->dwLvExStyle & LVS_EX_SUBITEMIMAGES) && lpLVItem->iImage != I_IMAGECALLBACK)))
2380 Icon.right += infoPtr->iconSize.cx;
2381 Icon.bottom = Icon.top + infoPtr->iconSize.cy;
2383 if(lprcIcon) *lprcIcon = Icon;
2384 TRACE(" - icon=%s\n", wine_dbgstr_rect(&Icon));
2386 /* TODO: is this correct? */
2387 if (lprcStateIcon)
2389 lprcStateIcon->left = Icon.left - state_width;
2390 lprcStateIcon->right = Icon.left;
2391 lprcStateIcon->top = Icon.top;
2392 lprcStateIcon->bottom = lprcStateIcon->top + infoPtr->iconSize.cy;
2393 TRACE(" - state icon=%s\n", wine_dbgstr_rect(lprcStateIcon));
2396 else Icon.right = 0;
2398 /************************************************************/
2399 /* compute LABEL bounding box (ala LVM_GETITEMRECT) */
2400 /************************************************************/
2401 if (doLabel)
2403 /* calculate how far to the right can the label stretch */
2404 Label.right = Box.right;
2405 if (infoPtr->uView == LV_VIEW_DETAILS)
2407 if (lpLVItem->iSubItem == 0)
2409 /* we need a zero based rect here */
2410 Label = lpColumnInfo->rcHeader;
2411 OffsetRect(&Label, -Label.left, 0);
2415 if (lpLVItem->iSubItem || ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && infoPtr->uView == LV_VIEW_DETAILS))
2417 labelSize.cx = infoPtr->nItemWidth;
2418 labelSize.cy = infoPtr->nItemHeight;
2419 goto calc_label;
2422 /* we need the text in non owner draw mode */
2423 assert(lpLVItem->mask & LVIF_TEXT);
2424 if (is_text(lpLVItem->pszText))
2426 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
2427 HDC hdc = GetDC(infoPtr->hwndSelf);
2428 HFONT hOldFont = SelectObject(hdc, hFont);
2429 UINT uFormat;
2430 RECT rcText;
2432 /* compute rough rectangle where the label will go */
2433 SetRectEmpty(&rcText);
2434 rcText.right = infoPtr->nItemWidth - TRAILING_LABEL_PADDING;
2435 rcText.bottom = infoPtr->nItemHeight;
2436 if (infoPtr->uView == LV_VIEW_ICON)
2437 rcText.bottom -= ICON_TOP_PADDING + infoPtr->iconSize.cy + ICON_BOTTOM_PADDING;
2439 /* now figure out the flags */
2440 if (infoPtr->uView == LV_VIEW_ICON)
2441 uFormat = oversizedBox ? LV_FL_DT_FLAGS : LV_ML_DT_FLAGS;
2442 else
2443 uFormat = LV_SL_DT_FLAGS;
2445 DrawTextW (hdc, lpLVItem->pszText, -1, &rcText, uFormat | DT_CALCRECT);
2447 if (rcText.right != rcText.left)
2448 labelSize.cx = min(rcText.right - rcText.left + TRAILING_LABEL_PADDING, infoPtr->nItemWidth);
2450 labelSize.cy = rcText.bottom - rcText.top;
2452 SelectObject(hdc, hOldFont);
2453 ReleaseDC(infoPtr->hwndSelf, hdc);
2456 calc_label:
2457 if (infoPtr->uView == LV_VIEW_ICON)
2459 Label.left = Box.left + (infoPtr->nItemWidth - labelSize.cx) / 2;
2460 Label.top = Box.top + ICON_TOP_PADDING_HITABLE +
2461 infoPtr->iconSize.cy + ICON_BOTTOM_PADDING;
2462 Label.right = Label.left + labelSize.cx;
2463 Label.bottom = Label.top + infoPtr->nItemHeight;
2464 if (!oversizedBox && labelSize.cy > infoPtr->ntmHeight)
2466 labelSize.cy = min(Box.bottom - Label.top, labelSize.cy);
2467 labelSize.cy /= infoPtr->ntmHeight;
2468 labelSize.cy = max(labelSize.cy, 1);
2469 labelSize.cy *= infoPtr->ntmHeight;
2471 Label.bottom = Label.top + labelSize.cy + HEIGHT_PADDING;
2473 else if (infoPtr->uView == LV_VIEW_DETAILS)
2475 Label.left = Icon.right;
2476 Label.top = Box.top;
2477 Label.right = lpLVItem->iSubItem ? lpColumnInfo->rcHeader.right :
2478 lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left;
2479 Label.bottom = Label.top + infoPtr->nItemHeight;
2481 else /* LV_VIEW_SMALLICON or LV_VIEW_LIST */
2483 Label.left = Icon.right;
2484 Label.top = Box.top;
2485 Label.right = min(Label.left + labelSize.cx, Label.right);
2486 Label.bottom = Label.top + infoPtr->nItemHeight;
2489 if (lprcLabel) *lprcLabel = Label;
2490 TRACE(" - label=%s\n", wine_dbgstr_rect(&Label));
2493 /************************************************************/
2494 /* compute SELECT bounding box */
2495 /************************************************************/
2496 if (doSelectBox)
2498 if (infoPtr->uView == LV_VIEW_DETAILS)
2500 SelectBox.left = Icon.left;
2501 SelectBox.top = Box.top;
2502 SelectBox.bottom = Box.bottom;
2504 if (labelSize.cx)
2505 SelectBox.right = min(Label.left + labelSize.cx, Label.right);
2506 else
2507 SelectBox.right = min(Label.left + MAX_EMPTYTEXT_SELECT_WIDTH, Label.right);
2509 else
2511 UnionRect(&SelectBox, &Icon, &Label);
2513 if (lprcSelectBox) *lprcSelectBox = SelectBox;
2514 TRACE(" - select box=%s\n", wine_dbgstr_rect(&SelectBox));
2517 /* Fix the Box if necessary */
2518 if (lprcBox)
2520 if (oversizedBox) UnionRect(lprcBox, &Box, &Label);
2521 else *lprcBox = Box;
2523 TRACE(" - box=%s\n", wine_dbgstr_rect(&Box));
2526 /***
2527 * DESCRIPTION: [INTERNAL]
2529 * PARAMETER(S):
2530 * [I] infoPtr : valid pointer to the listview structure
2531 * [I] nItem : item number
2532 * [O] lprcBox : ptr to Box rectangle
2534 * RETURN:
2535 * None.
2537 static void LISTVIEW_GetItemBox(const LISTVIEW_INFO *infoPtr, INT nItem, LPRECT lprcBox)
2539 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
2540 POINT Position, Origin;
2541 LVITEMW lvItem;
2543 LISTVIEW_GetOrigin(infoPtr, &Origin);
2544 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
2546 /* Be smart and try to figure out the minimum we have to do */
2547 lvItem.mask = 0;
2548 if (infoPtr->uView == LV_VIEW_ICON && infoPtr->bFocus && LISTVIEW_GetItemState(infoPtr, nItem, LVIS_FOCUSED))
2549 lvItem.mask |= LVIF_TEXT;
2550 lvItem.iItem = nItem;
2551 lvItem.iSubItem = 0;
2552 lvItem.pszText = szDispText;
2553 lvItem.cchTextMax = DISP_TEXT_SIZE;
2554 if (lvItem.mask) LISTVIEW_GetItemW(infoPtr, &lvItem);
2555 if (infoPtr->uView == LV_VIEW_ICON)
2557 lvItem.mask |= LVIF_STATE;
2558 lvItem.stateMask = LVIS_FOCUSED;
2559 lvItem.state = (lvItem.mask & LVIF_TEXT ? LVIS_FOCUSED : 0);
2561 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, lprcBox, 0, 0, 0, 0);
2563 if (infoPtr->uView == LV_VIEW_DETAILS && infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT &&
2564 SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, 0, 0))
2566 OffsetRect(lprcBox, Origin.x, Position.y + Origin.y);
2568 else
2569 OffsetRect(lprcBox, Position.x + Origin.x, Position.y + Origin.y);
2572 /* LISTVIEW_MapIdToIndex helper */
2573 static INT CALLBACK MapIdSearchCompare(LPVOID p1, LPVOID p2, LPARAM lParam)
2575 ITEM_ID *id1 = (ITEM_ID*)p1;
2576 ITEM_ID *id2 = (ITEM_ID*)p2;
2578 if (id1->id == id2->id) return 0;
2580 return (id1->id < id2->id) ? -1 : 1;
2583 /***
2584 * DESCRIPTION:
2585 * Returns the item index for id specified.
2587 * PARAMETER(S):
2588 * [I] infoPtr : valid pointer to the listview structure
2589 * [I] iID : item id to get index for
2591 * RETURN:
2592 * Item index, or -1 on failure.
2594 static INT LISTVIEW_MapIdToIndex(const LISTVIEW_INFO *infoPtr, UINT iID)
2596 ITEM_ID ID;
2597 INT index;
2599 TRACE("iID=%d\n", iID);
2601 if (infoPtr->dwStyle & LVS_OWNERDATA) return -1;
2602 if (infoPtr->nItemCount == 0) return -1;
2604 ID.id = iID;
2605 index = DPA_Search(infoPtr->hdpaItemIds, &ID, -1, MapIdSearchCompare, 0, DPAS_SORTED);
2607 if (index != -1)
2609 ITEM_ID *lpID = DPA_GetPtr(infoPtr->hdpaItemIds, index);
2610 return DPA_GetPtrIndex(infoPtr->hdpaItems, lpID->item);
2613 return -1;
2616 /***
2617 * DESCRIPTION:
2618 * Returns the item id for index given.
2620 * PARAMETER(S):
2621 * [I] infoPtr : valid pointer to the listview structure
2622 * [I] iItem : item index to get id for
2624 * RETURN:
2625 * Item id.
2627 static DWORD LISTVIEW_MapIndexToId(const LISTVIEW_INFO *infoPtr, INT iItem)
2629 ITEM_INFO *lpItem;
2630 HDPA hdpaSubItems;
2632 TRACE("iItem=%d\n", iItem);
2634 if (infoPtr->dwStyle & LVS_OWNERDATA) return -1;
2635 if (iItem < 0 || iItem >= infoPtr->nItemCount) return -1;
2637 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, iItem);
2638 lpItem = DPA_GetPtr(hdpaSubItems, 0);
2640 return lpItem->id->id;
2643 /***
2644 * DESCRIPTION:
2645 * Returns the current icon position, and advances it along the top.
2646 * The returned position is not offset by Origin.
2648 * PARAMETER(S):
2649 * [I] infoPtr : valid pointer to the listview structure
2650 * [O] lpPos : will get the current icon position
2652 * RETURN:
2653 * None
2655 static void LISTVIEW_NextIconPosTop(LISTVIEW_INFO *infoPtr, LPPOINT lpPos)
2657 INT nListWidth = infoPtr->rcList.right - infoPtr->rcList.left;
2659 *lpPos = infoPtr->currIconPos;
2661 infoPtr->currIconPos.x += infoPtr->nItemWidth;
2662 if (infoPtr->currIconPos.x + infoPtr->nItemWidth <= nListWidth) return;
2664 infoPtr->currIconPos.x = 0;
2665 infoPtr->currIconPos.y += infoPtr->nItemHeight;
2669 /***
2670 * DESCRIPTION:
2671 * Returns the current icon position, and advances it down the left edge.
2672 * The returned position is not offset by Origin.
2674 * PARAMETER(S):
2675 * [I] infoPtr : valid pointer to the listview structure
2676 * [O] lpPos : will get the current icon position
2678 * RETURN:
2679 * None
2681 static void LISTVIEW_NextIconPosLeft(LISTVIEW_INFO *infoPtr, LPPOINT lpPos)
2683 INT nListHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
2685 *lpPos = infoPtr->currIconPos;
2687 infoPtr->currIconPos.y += infoPtr->nItemHeight;
2688 if (infoPtr->currIconPos.y + infoPtr->nItemHeight <= nListHeight) return;
2690 infoPtr->currIconPos.x += infoPtr->nItemWidth;
2691 infoPtr->currIconPos.y = 0;
2695 /***
2696 * DESCRIPTION:
2697 * Moves an icon to the specified position.
2698 * It takes care of invalidating the item, etc.
2700 * PARAMETER(S):
2701 * [I] infoPtr : valid pointer to the listview structure
2702 * [I] nItem : the item to move
2703 * [I] lpPos : the new icon position
2704 * [I] isNew : flags the item as being new
2706 * RETURN:
2707 * Success: TRUE
2708 * Failure: FALSE
2710 static BOOL LISTVIEW_MoveIconTo(const LISTVIEW_INFO *infoPtr, INT nItem, const POINT *lppt, BOOL isNew)
2712 POINT old;
2714 if (!isNew)
2716 old.x = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosX, nItem);
2717 old.y = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosY, nItem);
2719 if (lppt->x == old.x && lppt->y == old.y) return TRUE;
2720 LISTVIEW_InvalidateItem(infoPtr, nItem);
2723 /* Allocating a POINTER for every item is too resource intensive,
2724 * so we'll keep the (x,y) in different arrays */
2725 if (!DPA_SetPtr(infoPtr->hdpaPosX, nItem, (void *)(LONG_PTR)lppt->x)) return FALSE;
2726 if (!DPA_SetPtr(infoPtr->hdpaPosY, nItem, (void *)(LONG_PTR)lppt->y)) return FALSE;
2728 LISTVIEW_InvalidateItem(infoPtr, nItem);
2730 return TRUE;
2733 /***
2734 * DESCRIPTION:
2735 * Arranges listview items in icon display mode.
2737 * PARAMETER(S):
2738 * [I] infoPtr : valid pointer to the listview structure
2739 * [I] nAlignCode : alignment code
2741 * RETURN:
2742 * SUCCESS : TRUE
2743 * FAILURE : FALSE
2745 static BOOL LISTVIEW_Arrange(LISTVIEW_INFO *infoPtr, INT nAlignCode)
2747 void (*next_pos)(LISTVIEW_INFO *, LPPOINT);
2748 POINT pos;
2749 INT i;
2751 if (infoPtr->uView != LV_VIEW_ICON && infoPtr->uView != LV_VIEW_SMALLICON) return FALSE;
2753 TRACE("nAlignCode=%d\n", nAlignCode);
2755 if (nAlignCode == LVA_DEFAULT)
2757 if (infoPtr->dwStyle & LVS_ALIGNLEFT) nAlignCode = LVA_ALIGNLEFT;
2758 else nAlignCode = LVA_ALIGNTOP;
2761 switch (nAlignCode)
2763 case LVA_ALIGNLEFT: next_pos = LISTVIEW_NextIconPosLeft; break;
2764 case LVA_ALIGNTOP: next_pos = LISTVIEW_NextIconPosTop; break;
2765 case LVA_SNAPTOGRID: next_pos = LISTVIEW_NextIconPosTop; break; /* FIXME */
2766 default: return FALSE;
2769 infoPtr->bAutoarrange = TRUE;
2770 infoPtr->currIconPos.x = infoPtr->currIconPos.y = 0;
2771 for (i = 0; i < infoPtr->nItemCount; i++)
2773 next_pos(infoPtr, &pos);
2774 LISTVIEW_MoveIconTo(infoPtr, i, &pos, FALSE);
2777 return TRUE;
2780 /***
2781 * DESCRIPTION:
2782 * Retrieves the bounding rectangle of all the items, not offset by Origin.
2783 * For LVS_REPORT always returns empty rectangle.
2785 * PARAMETER(S):
2786 * [I] infoPtr : valid pointer to the listview structure
2787 * [O] lprcView : bounding rectangle
2789 * RETURN:
2790 * SUCCESS : TRUE
2791 * FAILURE : FALSE
2793 static void LISTVIEW_GetAreaRect(const LISTVIEW_INFO *infoPtr, LPRECT lprcView)
2795 INT i, x, y;
2797 SetRectEmpty(lprcView);
2799 switch (infoPtr->uView)
2801 case LV_VIEW_ICON:
2802 case LV_VIEW_SMALLICON:
2803 for (i = 0; i < infoPtr->nItemCount; i++)
2805 x = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosX, i);
2806 y = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosY, i);
2807 lprcView->right = max(lprcView->right, x);
2808 lprcView->bottom = max(lprcView->bottom, y);
2810 if (infoPtr->nItemCount > 0)
2812 lprcView->right += infoPtr->nItemWidth;
2813 lprcView->bottom += infoPtr->nItemHeight;
2815 break;
2817 case LV_VIEW_LIST:
2818 y = LISTVIEW_GetCountPerColumn(infoPtr);
2819 x = infoPtr->nItemCount / y;
2820 if (infoPtr->nItemCount % y) x++;
2821 lprcView->right = x * infoPtr->nItemWidth;
2822 lprcView->bottom = y * infoPtr->nItemHeight;
2823 break;
2827 /***
2828 * DESCRIPTION:
2829 * Retrieves the bounding rectangle of all the items.
2831 * PARAMETER(S):
2832 * [I] infoPtr : valid pointer to the listview structure
2833 * [O] lprcView : bounding rectangle
2835 * RETURN:
2836 * SUCCESS : TRUE
2837 * FAILURE : FALSE
2839 static BOOL LISTVIEW_GetViewRect(const LISTVIEW_INFO *infoPtr, LPRECT lprcView)
2841 POINT ptOrigin;
2843 TRACE("(lprcView=%p)\n", lprcView);
2845 if (!lprcView) return FALSE;
2847 LISTVIEW_GetAreaRect(infoPtr, lprcView);
2849 if (infoPtr->uView != LV_VIEW_DETAILS)
2851 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
2852 OffsetRect(lprcView, ptOrigin.x, ptOrigin.y);
2855 TRACE("lprcView=%s\n", wine_dbgstr_rect(lprcView));
2857 return TRUE;
2860 /***
2861 * DESCRIPTION:
2862 * Retrieves the subitem pointer associated with the subitem index.
2864 * PARAMETER(S):
2865 * [I] hdpaSubItems : DPA handle for a specific item
2866 * [I] nSubItem : index of subitem
2868 * RETURN:
2869 * SUCCESS : subitem pointer
2870 * FAILURE : NULL
2872 static SUBITEM_INFO* LISTVIEW_GetSubItemPtr(HDPA hdpaSubItems, INT nSubItem)
2874 SUBITEM_INFO *lpSubItem;
2875 INT i;
2877 /* we should binary search here if need be */
2878 for (i = 1; i < DPA_GetPtrCount(hdpaSubItems); i++)
2880 lpSubItem = DPA_GetPtr(hdpaSubItems, i);
2881 if (lpSubItem->iSubItem == nSubItem)
2882 return lpSubItem;
2885 return NULL;
2889 /***
2890 * DESCRIPTION:
2891 * Calculates the desired item width.
2893 * PARAMETER(S):
2894 * [I] infoPtr : valid pointer to the listview structure
2896 * RETURN:
2897 * The desired item width.
2899 static INT LISTVIEW_CalculateItemWidth(const LISTVIEW_INFO *infoPtr)
2901 INT nItemWidth = 0;
2903 TRACE("uView=%d\n", infoPtr->uView);
2905 if (infoPtr->uView == LV_VIEW_ICON)
2906 nItemWidth = infoPtr->iconSpacing.cx;
2907 else if (infoPtr->uView == LV_VIEW_DETAILS)
2909 if (DPA_GetPtrCount(infoPtr->hdpaColumns) > 0)
2911 RECT rcHeader;
2912 INT index;
2914 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX,
2915 DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, 0);
2917 LISTVIEW_GetHeaderRect(infoPtr, index, &rcHeader);
2918 nItemWidth = rcHeader.right;
2921 else /* LV_VIEW_SMALLICON, or LV_VIEW_LIST */
2923 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
2924 LVITEMW lvItem;
2925 INT i;
2927 lvItem.mask = LVIF_TEXT;
2928 lvItem.iSubItem = 0;
2930 for (i = 0; i < infoPtr->nItemCount; i++)
2932 lvItem.iItem = i;
2933 lvItem.pszText = szDispText;
2934 lvItem.cchTextMax = DISP_TEXT_SIZE;
2935 if (LISTVIEW_GetItemW(infoPtr, &lvItem))
2936 nItemWidth = max(LISTVIEW_GetStringWidthT(infoPtr, lvItem.pszText, TRUE),
2937 nItemWidth);
2940 if (infoPtr->himlSmall) nItemWidth += infoPtr->iconSize.cx;
2941 if (infoPtr->himlState) nItemWidth += infoPtr->iconStateSize.cx;
2943 nItemWidth = max(DEFAULT_COLUMN_WIDTH, nItemWidth + WIDTH_PADDING);
2946 return nItemWidth;
2949 /***
2950 * DESCRIPTION:
2951 * Calculates the desired item height.
2953 * PARAMETER(S):
2954 * [I] infoPtr : valid pointer to the listview structure
2956 * RETURN:
2957 * The desired item height.
2959 static INT LISTVIEW_CalculateItemHeight(const LISTVIEW_INFO *infoPtr)
2961 INT nItemHeight;
2963 TRACE("uView=%d\n", infoPtr->uView);
2965 if (infoPtr->uView == LV_VIEW_ICON)
2966 nItemHeight = infoPtr->iconSpacing.cy;
2967 else
2969 nItemHeight = infoPtr->ntmHeight;
2970 if (infoPtr->himlState)
2971 nItemHeight = max(nItemHeight, infoPtr->iconStateSize.cy);
2972 if (infoPtr->himlSmall)
2973 nItemHeight = max(nItemHeight, infoPtr->iconSize.cy);
2974 nItemHeight += HEIGHT_PADDING;
2975 if (infoPtr->nMeasureItemHeight > 0)
2976 nItemHeight = infoPtr->nMeasureItemHeight;
2979 return max(nItemHeight, 1);
2982 /***
2983 * DESCRIPTION:
2984 * Updates the width, and height of an item.
2986 * PARAMETER(S):
2987 * [I] infoPtr : valid pointer to the listview structure
2989 * RETURN:
2990 * None.
2992 static inline void LISTVIEW_UpdateItemSize(LISTVIEW_INFO *infoPtr)
2994 infoPtr->nItemWidth = LISTVIEW_CalculateItemWidth(infoPtr);
2995 infoPtr->nItemHeight = LISTVIEW_CalculateItemHeight(infoPtr);
2999 /***
3000 * DESCRIPTION:
3001 * Retrieves and saves important text metrics info for the current
3002 * Listview font.
3004 * PARAMETER(S):
3005 * [I] infoPtr : valid pointer to the listview structure
3008 static void LISTVIEW_SaveTextMetrics(LISTVIEW_INFO *infoPtr)
3010 HDC hdc = GetDC(infoPtr->hwndSelf);
3011 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
3012 HFONT hOldFont = SelectObject(hdc, hFont);
3013 TEXTMETRICW tm;
3014 SIZE sz;
3016 if (GetTextMetricsW(hdc, &tm))
3018 infoPtr->ntmHeight = tm.tmHeight;
3019 infoPtr->ntmMaxCharWidth = tm.tmMaxCharWidth;
3022 if (GetTextExtentPoint32A(hdc, "...", 3, &sz))
3023 infoPtr->nEllipsisWidth = sz.cx;
3025 SelectObject(hdc, hOldFont);
3026 ReleaseDC(infoPtr->hwndSelf, hdc);
3028 TRACE("tmHeight=%d\n", infoPtr->ntmHeight);
3031 /***
3032 * DESCRIPTION:
3033 * A compare function for ranges
3035 * PARAMETER(S)
3036 * [I] range1 : pointer to range 1;
3037 * [I] range2 : pointer to range 2;
3038 * [I] flags : flags
3040 * RETURNS:
3041 * > 0 : if range 1 > range 2
3042 * < 0 : if range 2 > range 1
3043 * = 0 : if range intersects range 2
3045 static INT CALLBACK ranges_cmp(LPVOID range1, LPVOID range2, LPARAM flags)
3047 INT cmp;
3049 if (((RANGE*)range1)->upper <= ((RANGE*)range2)->lower)
3050 cmp = -1;
3051 else if (((RANGE*)range2)->upper <= ((RANGE*)range1)->lower)
3052 cmp = 1;
3053 else
3054 cmp = 0;
3056 TRACE("range1=%s, range2=%s, cmp=%d\n", debugrange(range1), debugrange(range2), cmp);
3058 return cmp;
3061 #define ranges_check(ranges, desc) if (TRACE_ON(listview)) ranges_assert(ranges, desc, __FILE__, __LINE__)
3063 static void ranges_assert(RANGES ranges, LPCSTR desc, const char *file, int line)
3065 INT i;
3066 RANGE *prev, *curr;
3068 TRACE("*** Checking %s:%d:%s ***\n", file, line, desc);
3069 assert (ranges);
3070 assert (DPA_GetPtrCount(ranges->hdpa) >= 0);
3071 ranges_dump(ranges);
3072 if (DPA_GetPtrCount(ranges->hdpa) > 0)
3074 prev = DPA_GetPtr(ranges->hdpa, 0);
3075 assert (prev->lower >= 0 && prev->lower < prev->upper);
3076 for (i = 1; i < DPA_GetPtrCount(ranges->hdpa); i++)
3078 curr = DPA_GetPtr(ranges->hdpa, i);
3079 assert (prev->upper <= curr->lower);
3080 assert (curr->lower < curr->upper);
3081 prev = curr;
3084 TRACE("--- Done checking---\n");
3087 static RANGES ranges_create(int count)
3089 RANGES ranges = Alloc(sizeof(struct tagRANGES));
3090 if (!ranges) return NULL;
3091 ranges->hdpa = DPA_Create(count);
3092 if (ranges->hdpa) return ranges;
3093 Free(ranges);
3094 return NULL;
3097 static void ranges_clear(RANGES ranges)
3099 INT i;
3101 for(i = 0; i < DPA_GetPtrCount(ranges->hdpa); i++)
3102 Free(DPA_GetPtr(ranges->hdpa, i));
3103 DPA_DeleteAllPtrs(ranges->hdpa);
3107 static void ranges_destroy(RANGES ranges)
3109 if (!ranges) return;
3110 ranges_clear(ranges);
3111 DPA_Destroy(ranges->hdpa);
3112 Free(ranges);
3115 static RANGES ranges_clone(RANGES ranges)
3117 RANGES clone;
3118 INT i;
3120 if (!(clone = ranges_create(DPA_GetPtrCount(ranges->hdpa)))) goto fail;
3122 for (i = 0; i < DPA_GetPtrCount(ranges->hdpa); i++)
3124 RANGE *newrng = Alloc(sizeof(RANGE));
3125 if (!newrng) goto fail;
3126 *newrng = *((RANGE*)DPA_GetPtr(ranges->hdpa, i));
3127 DPA_SetPtr(clone->hdpa, i, newrng);
3129 return clone;
3131 fail:
3132 TRACE ("clone failed\n");
3133 ranges_destroy(clone);
3134 return NULL;
3137 static RANGES ranges_diff(RANGES ranges, RANGES sub)
3139 INT i;
3141 for (i = 0; i < DPA_GetPtrCount(sub->hdpa); i++)
3142 ranges_del(ranges, *((RANGE *)DPA_GetPtr(sub->hdpa, i)));
3144 return ranges;
3147 static void ranges_dump(RANGES ranges)
3149 INT i;
3151 for (i = 0; i < DPA_GetPtrCount(ranges->hdpa); i++)
3152 TRACE(" %s\n", debugrange(DPA_GetPtr(ranges->hdpa, i)));
3155 static inline BOOL ranges_contain(RANGES ranges, INT nItem)
3157 RANGE srchrng = { nItem, nItem + 1 };
3159 TRACE("(nItem=%d)\n", nItem);
3160 ranges_check(ranges, "before contain");
3161 return DPA_Search(ranges->hdpa, &srchrng, 0, ranges_cmp, 0, DPAS_SORTED) != -1;
3164 static INT ranges_itemcount(RANGES ranges)
3166 INT i, count = 0;
3168 for (i = 0; i < DPA_GetPtrCount(ranges->hdpa); i++)
3170 RANGE *sel = DPA_GetPtr(ranges->hdpa, i);
3171 count += sel->upper - sel->lower;
3174 return count;
3177 static BOOL ranges_shift(RANGES ranges, INT nItem, INT delta, INT nUpper)
3179 RANGE srchrng = { nItem, nItem + 1 }, *chkrng;
3180 INT index;
3182 index = DPA_Search(ranges->hdpa, &srchrng, 0, ranges_cmp, 0, DPAS_SORTED | DPAS_INSERTAFTER);
3183 if (index == -1) return TRUE;
3185 for (; index < DPA_GetPtrCount(ranges->hdpa); index++)
3187 chkrng = DPA_GetPtr(ranges->hdpa, index);
3188 if (chkrng->lower >= nItem)
3189 chkrng->lower = max(min(chkrng->lower + delta, nUpper - 1), 0);
3190 if (chkrng->upper > nItem)
3191 chkrng->upper = max(min(chkrng->upper + delta, nUpper), 0);
3193 return TRUE;
3196 static BOOL ranges_add(RANGES ranges, RANGE range)
3198 RANGE srchrgn;
3199 INT index;
3201 TRACE("(%s)\n", debugrange(&range));
3202 ranges_check(ranges, "before add");
3204 /* try find overlapping regions first */
3205 srchrgn.lower = range.lower - 1;
3206 srchrgn.upper = range.upper + 1;
3207 index = DPA_Search(ranges->hdpa, &srchrgn, 0, ranges_cmp, 0, DPAS_SORTED);
3209 if (index == -1)
3211 RANGE *newrgn;
3213 TRACE("Adding new range\n");
3215 /* create the brand new range to insert */
3216 newrgn = Alloc(sizeof(RANGE));
3217 if(!newrgn) goto fail;
3218 *newrgn = range;
3220 /* figure out where to insert it */
3221 index = DPA_Search(ranges->hdpa, newrgn, 0, ranges_cmp, 0, DPAS_SORTED | DPAS_INSERTAFTER);
3222 TRACE("index=%d\n", index);
3223 if (index == -1) index = 0;
3225 /* and get it over with */
3226 if (DPA_InsertPtr(ranges->hdpa, index, newrgn) == -1)
3228 Free(newrgn);
3229 goto fail;
3232 else
3234 RANGE *chkrgn, *mrgrgn;
3235 INT fromindex, mergeindex;
3237 chkrgn = DPA_GetPtr(ranges->hdpa, index);
3238 TRACE("Merge with %s @%d\n", debugrange(chkrgn), index);
3240 chkrgn->lower = min(range.lower, chkrgn->lower);
3241 chkrgn->upper = max(range.upper, chkrgn->upper);
3243 TRACE("New range %s @%d\n", debugrange(chkrgn), index);
3245 /* merge now common ranges */
3246 fromindex = 0;
3247 srchrgn.lower = chkrgn->lower - 1;
3248 srchrgn.upper = chkrgn->upper + 1;
3252 mergeindex = DPA_Search(ranges->hdpa, &srchrgn, fromindex, ranges_cmp, 0, 0);
3253 if (mergeindex == -1) break;
3254 if (mergeindex == index)
3256 fromindex = index + 1;
3257 continue;
3260 TRACE("Merge with index %i\n", mergeindex);
3262 mrgrgn = DPA_GetPtr(ranges->hdpa, mergeindex);
3263 chkrgn->lower = min(chkrgn->lower, mrgrgn->lower);
3264 chkrgn->upper = max(chkrgn->upper, mrgrgn->upper);
3265 Free(mrgrgn);
3266 DPA_DeletePtr(ranges->hdpa, mergeindex);
3267 if (mergeindex < index) index --;
3268 } while(1);
3271 ranges_check(ranges, "after add");
3272 return TRUE;
3274 fail:
3275 ranges_check(ranges, "failed add");
3276 return FALSE;
3279 static BOOL ranges_del(RANGES ranges, RANGE range)
3281 RANGE *chkrgn;
3282 INT index;
3284 TRACE("(%s)\n", debugrange(&range));
3285 ranges_check(ranges, "before del");
3287 /* we don't use DPAS_SORTED here, since we need *
3288 * to find the first overlapping range */
3289 index = DPA_Search(ranges->hdpa, &range, 0, ranges_cmp, 0, 0);
3290 while(index != -1)
3292 chkrgn = DPA_GetPtr(ranges->hdpa, index);
3294 TRACE("Matches range %s @%d\n", debugrange(chkrgn), index);
3296 /* case 1: Same range */
3297 if ( (chkrgn->upper == range.upper) &&
3298 (chkrgn->lower == range.lower) )
3300 DPA_DeletePtr(ranges->hdpa, index);
3301 Free(chkrgn);
3302 break;
3304 /* case 2: engulf */
3305 else if ( (chkrgn->upper <= range.upper) &&
3306 (chkrgn->lower >= range.lower) )
3308 DPA_DeletePtr(ranges->hdpa, index);
3309 Free(chkrgn);
3311 /* case 3: overlap upper */
3312 else if ( (chkrgn->upper <= range.upper) &&
3313 (chkrgn->lower < range.lower) )
3315 chkrgn->upper = range.lower;
3317 /* case 4: overlap lower */
3318 else if ( (chkrgn->upper > range.upper) &&
3319 (chkrgn->lower >= range.lower) )
3321 chkrgn->lower = range.upper;
3322 break;
3324 /* case 5: fully internal */
3325 else
3327 RANGE *newrgn;
3329 if (!(newrgn = Alloc(sizeof(RANGE)))) goto fail;
3330 newrgn->lower = chkrgn->lower;
3331 newrgn->upper = range.lower;
3332 chkrgn->lower = range.upper;
3333 if (DPA_InsertPtr(ranges->hdpa, index, newrgn) == -1)
3335 Free(newrgn);
3336 goto fail;
3338 break;
3341 index = DPA_Search(ranges->hdpa, &range, index, ranges_cmp, 0, 0);
3344 ranges_check(ranges, "after del");
3345 return TRUE;
3347 fail:
3348 ranges_check(ranges, "failed del");
3349 return FALSE;
3352 /***
3353 * DESCRIPTION:
3354 * Removes all selection ranges
3356 * Parameters(s):
3357 * [I] infoPtr : valid pointer to the listview structure
3358 * [I] toSkip : item range to skip removing the selection
3360 * RETURNS:
3361 * SUCCESS : TRUE
3362 * FAILURE : FALSE
3364 static BOOL LISTVIEW_DeselectAllSkipItems(LISTVIEW_INFO *infoPtr, RANGES toSkip)
3366 LVITEMW lvItem;
3367 ITERATOR i;
3368 RANGES clone;
3370 TRACE("()\n");
3372 lvItem.state = 0;
3373 lvItem.stateMask = LVIS_SELECTED;
3375 /* need to clone the DPA because callbacks can change it */
3376 if (!(clone = ranges_clone(infoPtr->selectionRanges))) return FALSE;
3377 iterator_rangesitems(&i, ranges_diff(clone, toSkip));
3378 while(iterator_next(&i))
3379 LISTVIEW_SetItemState(infoPtr, i.nItem, &lvItem);
3380 /* note that the iterator destructor will free the cloned range */
3381 iterator_destroy(&i);
3383 return TRUE;
3386 static inline BOOL LISTVIEW_DeselectAllSkipItem(LISTVIEW_INFO *infoPtr, INT nItem)
3388 RANGES toSkip;
3390 if (!(toSkip = ranges_create(1))) return FALSE;
3391 if (nItem != -1) ranges_additem(toSkip, nItem);
3392 LISTVIEW_DeselectAllSkipItems(infoPtr, toSkip);
3393 ranges_destroy(toSkip);
3394 return TRUE;
3397 static inline BOOL LISTVIEW_DeselectAll(LISTVIEW_INFO *infoPtr)
3399 return LISTVIEW_DeselectAllSkipItem(infoPtr, -1);
3402 /***
3403 * DESCRIPTION:
3404 * Retrieves the number of items that are marked as selected.
3406 * PARAMETER(S):
3407 * [I] infoPtr : valid pointer to the listview structure
3409 * RETURN:
3410 * Number of items selected.
3412 static INT LISTVIEW_GetSelectedCount(const LISTVIEW_INFO *infoPtr)
3414 INT nSelectedCount = 0;
3416 if (infoPtr->uCallbackMask & LVIS_SELECTED)
3418 INT i;
3419 for (i = 0; i < infoPtr->nItemCount; i++)
3421 if (LISTVIEW_GetItemState(infoPtr, i, LVIS_SELECTED))
3422 nSelectedCount++;
3425 else
3426 nSelectedCount = ranges_itemcount(infoPtr->selectionRanges);
3428 TRACE("nSelectedCount=%d\n", nSelectedCount);
3429 return nSelectedCount;
3432 /***
3433 * DESCRIPTION:
3434 * Manages the item focus.
3436 * PARAMETER(S):
3437 * [I] infoPtr : valid pointer to the listview structure
3438 * [I] nItem : item index
3440 * RETURN:
3441 * TRUE : focused item changed
3442 * FALSE : focused item has NOT changed
3444 static inline BOOL LISTVIEW_SetItemFocus(LISTVIEW_INFO *infoPtr, INT nItem)
3446 INT oldFocus = infoPtr->nFocusedItem;
3447 LVITEMW lvItem;
3449 if (nItem == infoPtr->nFocusedItem) return FALSE;
3451 lvItem.state = nItem == -1 ? 0 : LVIS_FOCUSED;
3452 lvItem.stateMask = LVIS_FOCUSED;
3453 LISTVIEW_SetItemState(infoPtr, nItem == -1 ? infoPtr->nFocusedItem : nItem, &lvItem);
3455 return oldFocus != infoPtr->nFocusedItem;
3458 static INT shift_item(const LISTVIEW_INFO *infoPtr, INT nShiftItem, INT nItem, INT direction)
3460 if (nShiftItem < nItem) return nShiftItem;
3462 if (nShiftItem > nItem) return nShiftItem + direction;
3464 if (direction > 0) return nShiftItem + direction;
3466 return min(nShiftItem, infoPtr->nItemCount - 1);
3469 /* This function updates focus index.
3471 Parameters:
3472 focus : current focus index
3473 item : index of item to be added/removed
3474 direction : add/remove flag
3476 static void LISTVIEW_ShiftFocus(LISTVIEW_INFO *infoPtr, INT focus, INT item, INT direction)
3478 BOOL old_change = infoPtr->bDoChangeNotify;
3480 infoPtr->bDoChangeNotify = FALSE;
3481 focus = shift_item(infoPtr, focus, item, direction);
3482 if (focus != infoPtr->nFocusedItem)
3483 LISTVIEW_SetItemFocus(infoPtr, focus);
3484 infoPtr->bDoChangeNotify = old_change;
3488 * DESCRIPTION:
3489 * Updates the various indices after an item has been inserted or deleted.
3491 * PARAMETER(S):
3492 * [I] infoPtr : valid pointer to the listview structure
3493 * [I] nItem : item index
3494 * [I] direction : Direction of shift, +1 or -1.
3496 * RETURN:
3497 * None
3499 static void LISTVIEW_ShiftIndices(LISTVIEW_INFO *infoPtr, INT nItem, INT direction)
3501 TRACE("Shifting %i, %i steps\n", nItem, direction);
3503 ranges_shift(infoPtr->selectionRanges, nItem, direction, infoPtr->nItemCount);
3504 assert(abs(direction) == 1);
3505 infoPtr->nSelectionMark = shift_item(infoPtr, infoPtr->nSelectionMark, nItem, direction);
3507 /* But we are not supposed to modify nHotItem! */
3511 * DESCRIPTION:
3512 * Adds a block of selections.
3514 * PARAMETER(S):
3515 * [I] infoPtr : valid pointer to the listview structure
3516 * [I] nItem : item index
3518 * RETURN:
3519 * Whether the window is still valid.
3521 static BOOL LISTVIEW_AddGroupSelection(LISTVIEW_INFO *infoPtr, INT nItem)
3523 INT nFirst = min(infoPtr->nSelectionMark, nItem);
3524 INT nLast = max(infoPtr->nSelectionMark, nItem);
3525 HWND hwndSelf = infoPtr->hwndSelf;
3526 NMLVODSTATECHANGE nmlv;
3527 LVITEMW item;
3528 BOOL bOldChange;
3529 INT i;
3531 /* Temporarily disable change notification
3532 * If the control is LVS_OWNERDATA, we need to send
3533 * only one LVN_ODSTATECHANGED notification.
3534 * See MSDN documentation for LVN_ITEMCHANGED.
3536 bOldChange = infoPtr->bDoChangeNotify;
3537 if (infoPtr->dwStyle & LVS_OWNERDATA) infoPtr->bDoChangeNotify = FALSE;
3539 if (nFirst == -1) nFirst = nItem;
3541 item.state = LVIS_SELECTED;
3542 item.stateMask = LVIS_SELECTED;
3544 for (i = nFirst; i <= nLast; i++)
3545 LISTVIEW_SetItemState(infoPtr,i,&item);
3547 ZeroMemory(&nmlv, sizeof(nmlv));
3548 nmlv.iFrom = nFirst;
3549 nmlv.iTo = nLast;
3550 nmlv.uOldState = 0;
3551 nmlv.uNewState = item.state;
3553 notify_hdr(infoPtr, LVN_ODSTATECHANGED, (LPNMHDR)&nmlv);
3554 if (!IsWindow(hwndSelf))
3555 return FALSE;
3556 infoPtr->bDoChangeNotify = bOldChange;
3557 return TRUE;
3561 /***
3562 * DESCRIPTION:
3563 * Sets a single group selection.
3565 * PARAMETER(S):
3566 * [I] infoPtr : valid pointer to the listview structure
3567 * [I] nItem : item index
3569 * RETURN:
3570 * None
3572 static void LISTVIEW_SetGroupSelection(LISTVIEW_INFO *infoPtr, INT nItem)
3574 RANGES selection;
3575 LVITEMW item;
3576 ITERATOR i;
3577 BOOL bOldChange;
3579 if (!(selection = ranges_create(100))) return;
3581 item.state = LVIS_SELECTED;
3582 item.stateMask = LVIS_SELECTED;
3584 if ((infoPtr->uView == LV_VIEW_LIST) || (infoPtr->uView == LV_VIEW_DETAILS))
3586 if (infoPtr->nSelectionMark == -1)
3588 infoPtr->nSelectionMark = nItem;
3589 ranges_additem(selection, nItem);
3591 else
3593 RANGE sel;
3595 sel.lower = min(infoPtr->nSelectionMark, nItem);
3596 sel.upper = max(infoPtr->nSelectionMark, nItem) + 1;
3597 ranges_add(selection, sel);
3600 else
3602 RECT rcItem, rcSel, rcSelMark;
3603 POINT ptItem;
3605 rcItem.left = LVIR_BOUNDS;
3606 if (!LISTVIEW_GetItemRect(infoPtr, nItem, &rcItem)) {
3607 ranges_destroy (selection);
3608 return;
3610 rcSelMark.left = LVIR_BOUNDS;
3611 if (!LISTVIEW_GetItemRect(infoPtr, infoPtr->nSelectionMark, &rcSelMark)) {
3612 ranges_destroy (selection);
3613 return;
3615 UnionRect(&rcSel, &rcItem, &rcSelMark);
3616 iterator_frameditems(&i, infoPtr, &rcSel);
3617 while(iterator_next(&i))
3619 LISTVIEW_GetItemPosition(infoPtr, i.nItem, &ptItem);
3620 if (PtInRect(&rcSel, ptItem)) ranges_additem(selection, i.nItem);
3622 iterator_destroy(&i);
3625 /* disable per item notifications on LVS_OWNERDATA style
3626 FIXME: single LVN_ODSTATECHANGED should be used */
3627 bOldChange = infoPtr->bDoChangeNotify;
3628 if (infoPtr->dwStyle & LVS_OWNERDATA) infoPtr->bDoChangeNotify = FALSE;
3630 LISTVIEW_DeselectAllSkipItems(infoPtr, selection);
3633 iterator_rangesitems(&i, selection);
3634 while(iterator_next(&i))
3635 LISTVIEW_SetItemState(infoPtr, i.nItem, &item);
3636 /* this will also destroy the selection */
3637 iterator_destroy(&i);
3639 infoPtr->bDoChangeNotify = bOldChange;
3641 LISTVIEW_SetItemFocus(infoPtr, nItem);
3644 /***
3645 * DESCRIPTION:
3646 * Sets a single selection.
3648 * PARAMETER(S):
3649 * [I] infoPtr : valid pointer to the listview structure
3650 * [I] nItem : item index
3652 * RETURN:
3653 * None
3655 static void LISTVIEW_SetSelection(LISTVIEW_INFO *infoPtr, INT nItem)
3657 LVITEMW lvItem;
3659 TRACE("nItem=%d\n", nItem);
3661 LISTVIEW_DeselectAllSkipItem(infoPtr, nItem);
3663 lvItem.state = LVIS_FOCUSED | LVIS_SELECTED;
3664 lvItem.stateMask = LVIS_FOCUSED | LVIS_SELECTED;
3665 LISTVIEW_SetItemState(infoPtr, nItem, &lvItem);
3667 infoPtr->nSelectionMark = nItem;
3670 /***
3671 * DESCRIPTION:
3672 * Set selection(s) with keyboard.
3674 * PARAMETER(S):
3675 * [I] infoPtr : valid pointer to the listview structure
3676 * [I] nItem : item index
3677 * [I] space : VK_SPACE code sent
3679 * RETURN:
3680 * SUCCESS : TRUE (needs to be repainted)
3681 * FAILURE : FALSE (nothing has changed)
3683 static BOOL LISTVIEW_KeySelection(LISTVIEW_INFO *infoPtr, INT nItem, BOOL space)
3685 /* FIXME: pass in the state */
3686 WORD wShift = GetKeyState(VK_SHIFT) & 0x8000;
3687 WORD wCtrl = GetKeyState(VK_CONTROL) & 0x8000;
3688 BOOL bResult = FALSE;
3690 TRACE("nItem=%d, wShift=%d, wCtrl=%d\n", nItem, wShift, wCtrl);
3691 if ((nItem >= 0) && (nItem < infoPtr->nItemCount))
3693 bResult = TRUE;
3695 if (infoPtr->dwStyle & LVS_SINGLESEL || (wShift == 0 && wCtrl == 0))
3696 LISTVIEW_SetSelection(infoPtr, nItem);
3697 else
3699 if (wShift)
3700 LISTVIEW_SetGroupSelection(infoPtr, nItem);
3701 else if (wCtrl)
3703 LVITEMW lvItem;
3704 lvItem.state = ~LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED);
3705 lvItem.stateMask = LVIS_SELECTED;
3706 if (space)
3708 LISTVIEW_SetItemState(infoPtr, nItem, &lvItem);
3709 if (lvItem.state & LVIS_SELECTED)
3710 infoPtr->nSelectionMark = nItem;
3712 bResult = LISTVIEW_SetItemFocus(infoPtr, nItem);
3715 LISTVIEW_EnsureVisible(infoPtr, nItem, FALSE);
3718 UpdateWindow(infoPtr->hwndSelf); /* update client area */
3719 return bResult;
3722 static BOOL LISTVIEW_GetItemAtPt(const LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem, POINT pt)
3724 LVHITTESTINFO lvHitTestInfo;
3726 ZeroMemory(&lvHitTestInfo, sizeof(lvHitTestInfo));
3727 lvHitTestInfo.pt.x = pt.x;
3728 lvHitTestInfo.pt.y = pt.y;
3730 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
3732 lpLVItem->mask = LVIF_PARAM;
3733 lpLVItem->iItem = lvHitTestInfo.iItem;
3734 lpLVItem->iSubItem = 0;
3736 return LISTVIEW_GetItemT(infoPtr, lpLVItem, TRUE);
3739 static inline BOOL LISTVIEW_IsHotTracking(const LISTVIEW_INFO *infoPtr)
3741 return ((infoPtr->dwLvExStyle & LVS_EX_TRACKSELECT) ||
3742 (infoPtr->dwLvExStyle & LVS_EX_ONECLICKACTIVATE) ||
3743 (infoPtr->dwLvExStyle & LVS_EX_TWOCLICKACTIVATE));
3746 /***
3747 * DESCRIPTION:
3748 * Called when the mouse is being actively tracked and has hovered for a specified
3749 * amount of time
3751 * PARAMETER(S):
3752 * [I] infoPtr : valid pointer to the listview structure
3753 * [I] fwKeys : key indicator
3754 * [I] x,y : mouse position
3756 * RETURN:
3757 * 0 if the message was processed, non-zero if there was an error
3759 * INFO:
3760 * LVS_EX_TRACKSELECT: An item is automatically selected when the cursor remains
3761 * over the item for a certain period of time.
3764 static LRESULT LISTVIEW_MouseHover(LISTVIEW_INFO *infoPtr, INT x, INT y)
3766 NMHDR hdr;
3768 if (notify_hdr(infoPtr, NM_HOVER, &hdr)) return 0;
3770 if (LISTVIEW_IsHotTracking(infoPtr))
3772 LVITEMW item;
3773 POINT pt;
3775 pt.x = x;
3776 pt.y = y;
3778 if (LISTVIEW_GetItemAtPt(infoPtr, &item, pt))
3779 LISTVIEW_SetSelection(infoPtr, item.iItem);
3781 SetFocus(infoPtr->hwndSelf);
3784 return 0;
3787 #define SCROLL_LEFT 0x1
3788 #define SCROLL_RIGHT 0x2
3789 #define SCROLL_UP 0x4
3790 #define SCROLL_DOWN 0x8
3792 /***
3793 * DESCRIPTION:
3794 * Utility routine to draw and highlight items within a marquee selection rectangle.
3796 * PARAMETER(S):
3797 * [I] infoPtr : valid pointer to the listview structure
3798 * [I] coords_orig : original co-ordinates of the cursor
3799 * [I] coords_offs : offsetted coordinates of the cursor
3800 * [I] offset : offset amount
3801 * [I] scroll : Bitmask of which directions we should scroll, if at all
3803 * RETURN:
3804 * None.
3806 static void LISTVIEW_MarqueeHighlight(LISTVIEW_INFO *infoPtr, const POINT *coords_orig,
3807 const POINT *coords_offs, const POINT *offset,
3808 INT scroll)
3810 BOOL controlDown = FALSE;
3811 LVITEMW item;
3812 ITERATOR old_elems, new_elems;
3813 RECT rect;
3815 if (coords_offs->x > infoPtr->marqueeOrigin.x)
3817 rect.left = infoPtr->marqueeOrigin.x;
3818 rect.right = coords_offs->x;
3820 else
3822 rect.left = coords_offs->x;
3823 rect.right = infoPtr->marqueeOrigin.x;
3826 if (coords_offs->y > infoPtr->marqueeOrigin.y)
3828 rect.top = infoPtr->marqueeOrigin.y;
3829 rect.bottom = coords_offs->y;
3831 else
3833 rect.top = coords_offs->y;
3834 rect.bottom = infoPtr->marqueeOrigin.y;
3837 /* Cancel out the old marquee rectangle and draw the new one */
3838 LISTVIEW_InvalidateRect(infoPtr, &infoPtr->marqueeDrawRect);
3840 /* Scroll by the appropriate distance if applicable - speed up scrolling as
3841 the cursor is further away */
3843 if ((scroll & SCROLL_LEFT) && (coords_orig->x <= 0))
3844 LISTVIEW_Scroll(infoPtr, coords_orig->x, 0);
3846 if ((scroll & SCROLL_RIGHT) && (coords_orig->x >= infoPtr->rcList.right))
3847 LISTVIEW_Scroll(infoPtr, (coords_orig->x - infoPtr->rcList.right), 0);
3849 if ((scroll & SCROLL_UP) && (coords_orig->y <= 0))
3850 LISTVIEW_Scroll(infoPtr, 0, coords_orig->y);
3852 if ((scroll & SCROLL_DOWN) && (coords_orig->y >= infoPtr->rcList.bottom))
3853 LISTVIEW_Scroll(infoPtr, 0, (coords_orig->y - infoPtr->rcList.bottom));
3855 iterator_frameditems_absolute(&old_elems, infoPtr, &infoPtr->marqueeRect);
3857 CopyRect(&infoPtr->marqueeRect, &rect);
3859 CopyRect(&infoPtr->marqueeDrawRect, &rect);
3860 OffsetRect(&infoPtr->marqueeDrawRect, offset->x, offset->y);
3862 iterator_frameditems_absolute(&new_elems, infoPtr, &infoPtr->marqueeRect);
3863 iterator_remove_common_items(&old_elems, &new_elems);
3865 /* Iterate over no longer selected items */
3866 while (iterator_next(&old_elems))
3868 if (old_elems.nItem > -1)
3870 if (LISTVIEW_GetItemState(infoPtr, old_elems.nItem, LVIS_SELECTED) == LVIS_SELECTED)
3871 item.state = 0;
3872 else
3873 item.state = LVIS_SELECTED;
3875 item.stateMask = LVIS_SELECTED;
3877 LISTVIEW_SetItemState(infoPtr, old_elems.nItem, &item);
3880 iterator_destroy(&old_elems);
3883 /* Iterate over newly selected items */
3884 if (GetKeyState(VK_CONTROL) & 0x8000)
3885 controlDown = TRUE;
3887 while (iterator_next(&new_elems))
3889 if (new_elems.nItem > -1)
3891 /* If CTRL is pressed, invert. If not, always select the item. */
3892 if ((controlDown) && (LISTVIEW_GetItemState(infoPtr, new_elems.nItem, LVIS_SELECTED)))
3893 item.state = 0;
3894 else
3895 item.state = LVIS_SELECTED;
3897 item.stateMask = LVIS_SELECTED;
3899 LISTVIEW_SetItemState(infoPtr, new_elems.nItem, &item);
3902 iterator_destroy(&new_elems);
3904 LISTVIEW_InvalidateRect(infoPtr, &infoPtr->marqueeDrawRect);
3907 /***
3908 * DESCRIPTION:
3909 * Called when we are in a marquee selection that involves scrolling the listview (ie,
3910 * the cursor is outside the bounds of the client area). This is a TIMERPROC.
3912 * PARAMETER(S):
3913 * [I] hwnd : Handle to the listview
3914 * [I] uMsg : WM_TIMER (ignored)
3915 * [I] idEvent : The timer ID interpreted as a pointer to a LISTVIEW_INFO struct
3916 * [I] dwTimer : The elapsed time (ignored)
3918 * RETURN:
3919 * None.
3921 static VOID CALLBACK LISTVIEW_ScrollTimer(HWND hWnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
3923 LISTVIEW_INFO *infoPtr;
3924 SCROLLINFO scrollInfo;
3925 POINT coords_orig;
3926 POINT coords_offs;
3927 POINT offset;
3928 INT scroll = 0;
3930 infoPtr = (LISTVIEW_INFO *) idEvent;
3932 if (!infoPtr)
3933 return;
3935 /* Get the current cursor position and convert to client coordinates */
3936 GetCursorPos(&coords_orig);
3937 ScreenToClient(hWnd, &coords_orig);
3939 /* Ensure coordinates are within client bounds */
3940 coords_offs.x = max(min(coords_orig.x, infoPtr->rcList.right), 0);
3941 coords_offs.y = max(min(coords_orig.y, infoPtr->rcList.bottom), 0);
3943 /* Get offset */
3944 LISTVIEW_GetOrigin(infoPtr, &offset);
3946 /* Offset coordinates by the appropriate amount */
3947 coords_offs.x -= offset.x;
3948 coords_offs.y -= offset.y;
3950 scrollInfo.cbSize = sizeof(SCROLLINFO);
3951 scrollInfo.fMask = SIF_ALL;
3953 /* Work out in which directions we can scroll */
3954 if (GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
3956 if (scrollInfo.nPos != scrollInfo.nMin)
3957 scroll |= SCROLL_UP;
3959 if (((scrollInfo.nPage + scrollInfo.nPos) - 1) != scrollInfo.nMax)
3960 scroll |= SCROLL_DOWN;
3963 if (GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo))
3965 if (scrollInfo.nPos != scrollInfo.nMin)
3966 scroll |= SCROLL_LEFT;
3968 if (((scrollInfo.nPage + scrollInfo.nPos) - 1) != scrollInfo.nMax)
3969 scroll |= SCROLL_RIGHT;
3972 if (((coords_orig.x <= 0) && (scroll & SCROLL_LEFT)) ||
3973 ((coords_orig.y <= 0) && (scroll & SCROLL_UP)) ||
3974 ((coords_orig.x >= infoPtr->rcList.right) && (scroll & SCROLL_RIGHT)) ||
3975 ((coords_orig.y >= infoPtr->rcList.bottom) && (scroll & SCROLL_DOWN)))
3977 LISTVIEW_MarqueeHighlight(infoPtr, &coords_orig, &coords_offs, &offset, scroll);
3981 /***
3982 * DESCRIPTION:
3983 * Called whenever WM_MOUSEMOVE is received.
3985 * PARAMETER(S):
3986 * [I] infoPtr : valid pointer to the listview structure
3987 * [I] fwKeys : key indicator
3988 * [I] x,y : mouse position
3990 * RETURN:
3991 * 0 if the message is processed, non-zero if there was an error
3993 static LRESULT LISTVIEW_MouseMove(LISTVIEW_INFO *infoPtr, WORD fwKeys, INT x, INT y)
3995 LVHITTESTINFO ht;
3996 RECT rect;
3997 POINT pt;
3999 if (!(fwKeys & MK_LBUTTON))
4000 infoPtr->bLButtonDown = FALSE;
4002 if (infoPtr->bLButtonDown)
4004 rect.left = rect.right = infoPtr->ptClickPos.x;
4005 rect.top = rect.bottom = infoPtr->ptClickPos.y;
4007 InflateRect(&rect, GetSystemMetrics(SM_CXDRAG), GetSystemMetrics(SM_CYDRAG));
4010 if (infoPtr->bLButtonDown)
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;
4130 trackinfo.cbSize = sizeof(TRACKMOUSEEVENT);
4131 trackinfo.dwFlags = TME_QUERY;
4133 /* see if we are already tracking this hwnd */
4134 _TrackMouseEvent(&trackinfo);
4136 if(!(trackinfo.dwFlags & TME_HOVER) || trackinfo.hwndTrack != infoPtr->hwndSelf) {
4137 trackinfo.dwFlags = TME_HOVER;
4138 trackinfo.dwHoverTime = infoPtr->dwHoverTime;
4139 trackinfo.hwndTrack = infoPtr->hwndSelf;
4141 /* call TRACKMOUSEEVENT so we receive WM_MOUSEHOVER messages */
4142 _TrackMouseEvent(&trackinfo);
4146 return 0;
4150 /***
4151 * Tests whether the item is assignable to a list with style lStyle
4153 static inline BOOL is_assignable_item(const LVITEMW *lpLVItem, LONG lStyle)
4155 if ( (lpLVItem->mask & LVIF_TEXT) &&
4156 (lpLVItem->pszText == LPSTR_TEXTCALLBACKW) &&
4157 (lStyle & (LVS_SORTASCENDING | LVS_SORTDESCENDING)) ) return FALSE;
4159 return TRUE;
4163 /***
4164 * DESCRIPTION:
4165 * Helper for LISTVIEW_SetItemT and LISTVIEW_InsertItemT: sets item attributes.
4167 * PARAMETER(S):
4168 * [I] infoPtr : valid pointer to the listview structure
4169 * [I] lpLVItem : valid pointer to new item attributes
4170 * [I] isNew : the item being set is being inserted
4171 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
4172 * [O] bChanged : will be set to TRUE if the item really changed
4174 * RETURN:
4175 * SUCCESS : TRUE
4176 * FAILURE : FALSE
4178 static BOOL set_main_item(LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem, BOOL isNew, BOOL isW, BOOL *bChanged)
4180 ITEM_INFO *lpItem;
4181 NMLISTVIEW nmlv;
4182 UINT uChanged = 0;
4183 LVITEMW item;
4184 /* stateMask is ignored for LVM_INSERTITEM */
4185 UINT stateMask = isNew ? ~0 : lpLVItem->stateMask;
4187 TRACE("()\n");
4189 assert(lpLVItem->iItem >= 0 && lpLVItem->iItem < infoPtr->nItemCount);
4191 if (lpLVItem->mask == 0) return TRUE;
4193 if (infoPtr->dwStyle & LVS_OWNERDATA)
4195 /* a virtual listview only stores selection and focus */
4196 if (lpLVItem->mask & ~LVIF_STATE)
4197 return FALSE;
4198 lpItem = NULL;
4200 else
4202 HDPA hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, lpLVItem->iItem);
4203 lpItem = DPA_GetPtr(hdpaSubItems, 0);
4204 assert (lpItem);
4207 /* we need to get the lParam and state of the item */
4208 item.iItem = lpLVItem->iItem;
4209 item.iSubItem = lpLVItem->iSubItem;
4210 item.mask = LVIF_STATE | LVIF_PARAM;
4211 item.stateMask = (infoPtr->dwStyle & LVS_OWNERDATA) ? LVIS_FOCUSED | LVIS_SELECTED : ~0;
4213 item.state = 0;
4214 item.lParam = 0;
4215 if (!isNew && !LISTVIEW_GetItemW(infoPtr, &item)) return FALSE;
4217 TRACE("oldState=%x, newState=%x\n", item.state, lpLVItem->state);
4218 /* determine what fields will change */
4219 if ((lpLVItem->mask & LVIF_STATE) && ((item.state ^ lpLVItem->state) & stateMask & ~infoPtr->uCallbackMask))
4220 uChanged |= LVIF_STATE;
4222 if ((lpLVItem->mask & LVIF_IMAGE) && (lpItem->hdr.iImage != lpLVItem->iImage))
4223 uChanged |= LVIF_IMAGE;
4225 if ((lpLVItem->mask & LVIF_PARAM) && (lpItem->lParam != lpLVItem->lParam))
4226 uChanged |= LVIF_PARAM;
4228 if ((lpLVItem->mask & LVIF_INDENT) && (lpItem->iIndent != lpLVItem->iIndent))
4229 uChanged |= LVIF_INDENT;
4231 if ((lpLVItem->mask & LVIF_TEXT) && textcmpWT(lpItem->hdr.pszText, lpLVItem->pszText, isW))
4232 uChanged |= LVIF_TEXT;
4234 TRACE("change mask=0x%x\n", uChanged);
4236 memset(&nmlv, 0, sizeof(NMLISTVIEW));
4237 nmlv.iItem = lpLVItem->iItem;
4238 nmlv.uNewState = (item.state & ~stateMask) | (lpLVItem->state & stateMask);
4239 nmlv.uOldState = item.state;
4240 nmlv.uChanged = uChanged ? uChanged : lpLVItem->mask;
4241 nmlv.lParam = item.lParam;
4243 /* Send LVN_ITEMCHANGING notification, if the item is not being inserted
4244 and we are _NOT_ virtual (LVS_OWNERDATA), and change notifications
4245 are enabled. Even nothing really changed we still need to send this,
4246 in this case uChanged mask is just set to passed item mask. */
4247 if(lpItem && !isNew && infoPtr->bDoChangeNotify)
4249 HWND hwndSelf = infoPtr->hwndSelf;
4251 if (notify_listview(infoPtr, LVN_ITEMCHANGING, &nmlv))
4252 return FALSE;
4253 if (!IsWindow(hwndSelf))
4254 return FALSE;
4257 /* When item is inserted we need to shift existing focus index if new item has lower index. */
4258 if (isNew && (stateMask & ~infoPtr->uCallbackMask & LVIS_FOCUSED) &&
4259 /* this means we won't hit a focus change path later */
4260 ((uChanged & LVIF_STATE) == 0 || (!(lpLVItem->state & LVIS_FOCUSED) && (infoPtr->nFocusedItem != lpLVItem->iItem))))
4262 if (infoPtr->nFocusedItem != -1 && (lpLVItem->iItem <= infoPtr->nFocusedItem))
4263 infoPtr->nFocusedItem++;
4266 if (!uChanged) return TRUE;
4267 *bChanged = TRUE;
4269 /* copy information */
4270 if (lpLVItem->mask & LVIF_TEXT)
4271 textsetptrT(&lpItem->hdr.pszText, lpLVItem->pszText, isW);
4273 if (lpLVItem->mask & LVIF_IMAGE)
4274 lpItem->hdr.iImage = lpLVItem->iImage;
4276 if (lpLVItem->mask & LVIF_PARAM)
4277 lpItem->lParam = lpLVItem->lParam;
4279 if (lpLVItem->mask & LVIF_INDENT)
4280 lpItem->iIndent = lpLVItem->iIndent;
4282 if (uChanged & LVIF_STATE)
4284 if (lpItem && (stateMask & ~infoPtr->uCallbackMask))
4286 lpItem->state &= ~stateMask;
4287 lpItem->state |= (lpLVItem->state & stateMask);
4289 if (lpLVItem->state & stateMask & ~infoPtr->uCallbackMask & LVIS_SELECTED)
4291 if (infoPtr->dwStyle & LVS_SINGLESEL) LISTVIEW_DeselectAllSkipItem(infoPtr, lpLVItem->iItem);
4292 ranges_additem(infoPtr->selectionRanges, lpLVItem->iItem);
4294 else if (stateMask & LVIS_SELECTED)
4296 ranges_delitem(infoPtr->selectionRanges, lpLVItem->iItem);
4298 /* If we are asked to change focus, and we manage it, do it.
4299 It's important to have all new item data stored at this point,
4300 because changing existing focus could result in a redrawing operation,
4301 which in turn could ask for disp data, application should see all data
4302 for inserted item when processing LVN_GETDISPINFO.
4304 The way this works application will see nested item change notifications -
4305 changed item notifications interrupted by ones from item losing focus. */
4306 if (stateMask & ~infoPtr->uCallbackMask & LVIS_FOCUSED)
4308 if (lpLVItem->state & LVIS_FOCUSED)
4310 /* update selection mark */
4311 if (infoPtr->nFocusedItem == -1 && infoPtr->nSelectionMark == -1)
4312 infoPtr->nSelectionMark = lpLVItem->iItem;
4314 if (infoPtr->nFocusedItem != -1)
4316 /* remove current focus */
4317 item.mask = LVIF_STATE;
4318 item.state = 0;
4319 item.stateMask = LVIS_FOCUSED;
4321 /* recurse with redrawing an item */
4322 LISTVIEW_SetItemState(infoPtr, infoPtr->nFocusedItem, &item);
4325 infoPtr->nFocusedItem = lpLVItem->iItem;
4326 LISTVIEW_EnsureVisible(infoPtr, lpLVItem->iItem, infoPtr->uView == LV_VIEW_LIST);
4328 else if (infoPtr->nFocusedItem == lpLVItem->iItem)
4330 infoPtr->nFocusedItem = -1;
4335 /* if we're inserting the item, we're done */
4336 if (isNew) return TRUE;
4338 /* send LVN_ITEMCHANGED notification */
4339 if (lpLVItem->mask & LVIF_PARAM) nmlv.lParam = lpLVItem->lParam;
4340 if (infoPtr->bDoChangeNotify) notify_listview(infoPtr, LVN_ITEMCHANGED, &nmlv);
4342 return TRUE;
4345 /***
4346 * DESCRIPTION:
4347 * Helper for LISTVIEW_{Set,Insert}ItemT *only*: sets subitem attributes.
4349 * PARAMETER(S):
4350 * [I] infoPtr : valid pointer to the listview structure
4351 * [I] lpLVItem : valid pointer to new subitem attributes
4352 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
4353 * [O] bChanged : will be set to TRUE if the item really changed
4355 * RETURN:
4356 * SUCCESS : TRUE
4357 * FAILURE : FALSE
4359 static BOOL set_sub_item(const LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem, BOOL isW, BOOL *bChanged)
4361 HDPA hdpaSubItems;
4362 SUBITEM_INFO *lpSubItem;
4364 /* we do not support subitems for virtual listviews */
4365 if (infoPtr->dwStyle & LVS_OWNERDATA) return FALSE;
4367 /* set subitem only if column is present */
4368 if (lpLVItem->iSubItem >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
4370 /* First do some sanity checks */
4371 /* The LVIF_STATE flag is valid for subitems, but does not appear to be
4372 particularly useful. We currently do not actually do anything with
4373 the flag on subitems.
4375 if (lpLVItem->mask & ~(LVIF_TEXT | LVIF_IMAGE | LVIF_STATE | LVIF_DI_SETITEM)) return FALSE;
4376 if (!(lpLVItem->mask & (LVIF_TEXT | LVIF_IMAGE | LVIF_STATE))) return TRUE;
4378 /* get the subitem structure, and create it if not there */
4379 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, lpLVItem->iItem);
4380 assert (hdpaSubItems);
4382 lpSubItem = LISTVIEW_GetSubItemPtr(hdpaSubItems, lpLVItem->iSubItem);
4383 if (!lpSubItem)
4385 SUBITEM_INFO *tmpSubItem;
4386 INT i;
4388 lpSubItem = Alloc(sizeof(SUBITEM_INFO));
4389 if (!lpSubItem) return FALSE;
4390 /* we could binary search here, if need be...*/
4391 for (i = 1; i < DPA_GetPtrCount(hdpaSubItems); i++)
4393 tmpSubItem = DPA_GetPtr(hdpaSubItems, i);
4394 if (tmpSubItem->iSubItem > lpLVItem->iSubItem) break;
4396 if (DPA_InsertPtr(hdpaSubItems, i, lpSubItem) == -1)
4398 Free(lpSubItem);
4399 return FALSE;
4401 lpSubItem->iSubItem = lpLVItem->iSubItem;
4402 lpSubItem->hdr.iImage = I_IMAGECALLBACK;
4403 *bChanged = TRUE;
4406 if ((lpLVItem->mask & LVIF_IMAGE) && (lpSubItem->hdr.iImage != lpLVItem->iImage))
4408 lpSubItem->hdr.iImage = lpLVItem->iImage;
4409 *bChanged = TRUE;
4412 if ((lpLVItem->mask & LVIF_TEXT) && textcmpWT(lpSubItem->hdr.pszText, lpLVItem->pszText, isW))
4414 textsetptrT(&lpSubItem->hdr.pszText, lpLVItem->pszText, isW);
4415 *bChanged = TRUE;
4418 return TRUE;
4421 /***
4422 * DESCRIPTION:
4423 * Sets item attributes.
4425 * PARAMETER(S):
4426 * [I] infoPtr : valid pointer to the listview structure
4427 * [I] lpLVItem : new item attributes
4428 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
4430 * RETURN:
4431 * SUCCESS : TRUE
4432 * FAILURE : FALSE
4434 static BOOL LISTVIEW_SetItemT(LISTVIEW_INFO *infoPtr, LVITEMW *lpLVItem, BOOL isW)
4436 HWND hwndSelf = infoPtr->hwndSelf;
4437 LPWSTR pszText = NULL;
4438 BOOL bResult, bChanged = FALSE;
4439 RECT oldItemArea;
4441 TRACE("(lpLVItem=%s, isW=%d)\n", debuglvitem_t(lpLVItem, isW), isW);
4443 if (!lpLVItem || lpLVItem->iItem < 0 || lpLVItem->iItem >= infoPtr->nItemCount)
4444 return FALSE;
4446 /* Store old item area */
4447 LISTVIEW_GetItemBox(infoPtr, lpLVItem->iItem, &oldItemArea);
4449 /* For efficiency, we transform the lpLVItem->pszText to Unicode here */
4450 if ((lpLVItem->mask & LVIF_TEXT) && is_text(lpLVItem->pszText))
4452 pszText = lpLVItem->pszText;
4453 lpLVItem->pszText = textdupTtoW(lpLVItem->pszText, isW);
4456 /* actually set the fields */
4457 if (!is_assignable_item(lpLVItem, infoPtr->dwStyle)) return FALSE;
4459 if (lpLVItem->iSubItem)
4460 bResult = set_sub_item(infoPtr, lpLVItem, TRUE, &bChanged);
4461 else
4462 bResult = set_main_item(infoPtr, lpLVItem, FALSE, TRUE, &bChanged);
4463 if (!IsWindow(hwndSelf))
4464 return FALSE;
4466 /* redraw item, if necessary */
4467 if (bChanged && !infoPtr->bIsDrawing)
4469 /* this little optimization eliminates some nasty flicker */
4470 if ( infoPtr->uView == LV_VIEW_DETAILS && !(infoPtr->dwStyle & LVS_OWNERDRAWFIXED) &&
4471 !(infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) &&
4472 lpLVItem->iSubItem > 0 && lpLVItem->iSubItem <= DPA_GetPtrCount(infoPtr->hdpaColumns) )
4473 LISTVIEW_InvalidateSubItem(infoPtr, lpLVItem->iItem, lpLVItem->iSubItem);
4474 else
4476 LISTVIEW_InvalidateRect(infoPtr, &oldItemArea);
4477 LISTVIEW_InvalidateItem(infoPtr, lpLVItem->iItem);
4480 /* restore text */
4481 if (pszText)
4483 textfreeT(lpLVItem->pszText, isW);
4484 lpLVItem->pszText = pszText;
4487 return bResult;
4490 /***
4491 * DESCRIPTION:
4492 * Retrieves the index of the item at coordinate (0, 0) of the client area.
4494 * PARAMETER(S):
4495 * [I] infoPtr : valid pointer to the listview structure
4497 * RETURN:
4498 * item index
4500 static INT LISTVIEW_GetTopIndex(const LISTVIEW_INFO *infoPtr)
4502 INT nItem = 0;
4503 SCROLLINFO scrollInfo;
4505 scrollInfo.cbSize = sizeof(SCROLLINFO);
4506 scrollInfo.fMask = SIF_POS;
4508 if (infoPtr->uView == LV_VIEW_LIST)
4510 if (GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo))
4511 nItem = scrollInfo.nPos * LISTVIEW_GetCountPerColumn(infoPtr);
4513 else if (infoPtr->uView == LV_VIEW_DETAILS)
4515 if (GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
4516 nItem = scrollInfo.nPos;
4518 else
4520 if (GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
4521 nItem = LISTVIEW_GetCountPerRow(infoPtr) * (scrollInfo.nPos / infoPtr->nItemHeight);
4524 TRACE("nItem=%d\n", nItem);
4526 return nItem;
4530 /***
4531 * DESCRIPTION:
4532 * Erases the background of the given rectangle
4534 * PARAMETER(S):
4535 * [I] infoPtr : valid pointer to the listview structure
4536 * [I] hdc : device context handle
4537 * [I] lprcBox : clipping rectangle
4539 * RETURN:
4540 * Success: TRUE
4541 * Failure: FALSE
4543 static inline BOOL LISTVIEW_FillBkgnd(const LISTVIEW_INFO *infoPtr, HDC hdc, const RECT *lprcBox)
4545 if (!infoPtr->hBkBrush) return FALSE;
4547 TRACE("(hdc=%p, lprcBox=%s, hBkBrush=%p)\n", hdc, wine_dbgstr_rect(lprcBox), infoPtr->hBkBrush);
4549 return FillRect(hdc, lprcBox, infoPtr->hBkBrush);
4552 /* Draw main item or subitem */
4553 static void LISTVIEW_DrawItemPart(LISTVIEW_INFO *infoPtr, LVITEMW *item, const NMLVCUSTOMDRAW *nmlvcd, const POINT *pos)
4555 RECT rcSelect, rcLabel, rcBox, rcStateIcon, rcIcon;
4556 HIMAGELIST himl;
4557 UINT format;
4558 RECT *focus;
4560 /* now check if we need to update the focus rectangle */
4561 focus = infoPtr->bFocus && (item->state & LVIS_FOCUSED) ? &infoPtr->rcFocus : 0;
4562 if (!focus) item->state &= ~LVIS_FOCUSED;
4564 LISTVIEW_GetItemMetrics(infoPtr, item, &rcBox, &rcSelect, &rcIcon, &rcStateIcon, &rcLabel);
4565 OffsetRect(&rcBox, pos->x, pos->y);
4566 OffsetRect(&rcSelect, pos->x, pos->y);
4567 OffsetRect(&rcIcon, pos->x, pos->y);
4568 OffsetRect(&rcStateIcon, pos->x, pos->y);
4569 OffsetRect(&rcLabel, pos->x, pos->y);
4570 TRACE(" rcBox=%s, rcSelect=%s, rcIcon=%s. rcLabel=%s\n",
4571 wine_dbgstr_rect(&rcBox), wine_dbgstr_rect(&rcSelect),
4572 wine_dbgstr_rect(&rcIcon), wine_dbgstr_rect(&rcLabel));
4574 /* FIXME: temporary hack */
4575 rcSelect.left = rcLabel.left;
4577 /* in icon mode, the label rect is really what we want to draw the
4578 * background for */
4579 /* in detail mode, we want to paint background for label rect when
4580 * item is not selected or listview has full row select; otherwise paint
4581 * background for text only */
4582 if ( infoPtr->uView == LV_VIEW_ICON ||
4583 (infoPtr->uView == LV_VIEW_DETAILS && (!(item->state & LVIS_SELECTED) ||
4584 (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT))))
4585 rcSelect = rcLabel;
4587 if (nmlvcd->clrTextBk != CLR_NONE)
4588 ExtTextOutW(nmlvcd->nmcd.hdc, rcSelect.left, rcSelect.top, ETO_OPAQUE, &rcSelect, NULL, 0, NULL);
4590 if (item->state & LVIS_FOCUSED)
4592 if (infoPtr->uView == LV_VIEW_DETAILS && (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT))
4594 /* we have to update left focus bound too if item isn't in leftmost column
4595 and reduce right box bound */
4596 if (DPA_GetPtrCount(infoPtr->hdpaColumns) > 0)
4598 INT leftmost;
4600 if ((leftmost = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, 0, 0)))
4602 INT Originx = pos->x - LISTVIEW_GetColumnInfo(infoPtr, 0)->rcHeader.left;
4603 INT index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX,
4604 DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, 0);
4606 rcBox.right = LISTVIEW_GetColumnInfo(infoPtr, index)->rcHeader.right + Originx;
4607 rcSelect.left = LISTVIEW_GetColumnInfo(infoPtr, leftmost)->rcHeader.left + Originx;
4611 rcSelect.right = rcBox.right;
4614 /* store new focus rectangle */
4615 infoPtr->rcFocus = rcSelect;
4618 /* state icons */
4619 if (infoPtr->himlState && STATEIMAGEINDEX(item->state) && (item->iSubItem == 0))
4621 UINT stateimage = STATEIMAGEINDEX(item->state);
4622 if (stateimage)
4624 TRACE("stateimage=%d\n", stateimage);
4625 ImageList_Draw(infoPtr->himlState, stateimage-1, nmlvcd->nmcd.hdc, rcStateIcon.left, rcStateIcon.top, ILD_NORMAL);
4629 /* item icons */
4630 himl = (infoPtr->uView == LV_VIEW_ICON ? infoPtr->himlNormal : infoPtr->himlSmall);
4631 if (himl && item->iImage >= 0 && !IsRectEmpty(&rcIcon))
4633 UINT style;
4635 TRACE("iImage=%d\n", item->iImage);
4637 if (item->state & (LVIS_SELECTED | LVIS_CUT) && infoPtr->bFocus)
4638 style = ILD_SELECTED;
4639 else
4640 style = ILD_NORMAL;
4642 ImageList_DrawEx(himl, item->iImage, nmlvcd->nmcd.hdc, rcIcon.left, rcIcon.top,
4643 rcIcon.right - rcIcon.left, rcIcon.bottom - rcIcon.top, infoPtr->clrBk,
4644 item->state & LVIS_CUT ? RGB(255, 255, 255) : CLR_DEFAULT,
4645 style | (item->state & LVIS_OVERLAYMASK));
4648 /* Don't bother painting item being edited */
4649 if (infoPtr->hwndEdit && item->iItem == infoPtr->nEditLabelItem && item->iSubItem == 0) return;
4651 /* figure out the text drawing flags */
4652 format = (infoPtr->uView == LV_VIEW_ICON ? (focus ? LV_FL_DT_FLAGS : LV_ML_DT_FLAGS) : LV_SL_DT_FLAGS);
4653 if (infoPtr->uView == LV_VIEW_ICON)
4654 format = (focus ? LV_FL_DT_FLAGS : LV_ML_DT_FLAGS);
4655 else if (item->iSubItem)
4657 switch (LISTVIEW_GetColumnInfo(infoPtr, item->iSubItem)->fmt & LVCFMT_JUSTIFYMASK)
4659 case LVCFMT_RIGHT: format |= DT_RIGHT; break;
4660 case LVCFMT_CENTER: format |= DT_CENTER; break;
4661 default: format |= DT_LEFT;
4664 if (!(format & (DT_RIGHT | DT_CENTER)))
4666 if (himl && item->iImage >= 0 && !IsRectEmpty(&rcIcon)) rcLabel.left += IMAGE_PADDING;
4667 else rcLabel.left += LABEL_HOR_PADDING;
4669 else if (format & DT_RIGHT) rcLabel.right -= LABEL_HOR_PADDING;
4671 /* for GRIDLINES reduce the bottom so the text formats correctly */
4672 if (infoPtr->uView == LV_VIEW_DETAILS && infoPtr->dwLvExStyle & LVS_EX_GRIDLINES)
4673 rcLabel.bottom--;
4675 DrawTextW(nmlvcd->nmcd.hdc, item->pszText, -1, &rcLabel, format);
4678 /***
4679 * DESCRIPTION:
4680 * Draws an item.
4682 * PARAMETER(S):
4683 * [I] infoPtr : valid pointer to the listview structure
4684 * [I] hdc : device context handle
4685 * [I] nItem : item index
4686 * [I] nSubItem : subitem index
4687 * [I] pos : item position in client coordinates
4688 * [I] cdmode : custom draw mode
4690 * RETURN:
4691 * Success: TRUE
4692 * Failure: FALSE
4694 static BOOL LISTVIEW_DrawItem(LISTVIEW_INFO *infoPtr, HDC hdc, INT nItem, ITERATOR *subitems, POINT pos, DWORD cdmode)
4696 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
4697 static WCHAR callbackW[] = { '(', 'c', 'a', 'l', 'l', 'b', 'a', 'c', 'k', ')', 0 };
4698 DWORD cdsubitemmode = CDRF_DODEFAULT;
4699 RECT *focus, rcBox;
4700 NMLVCUSTOMDRAW nmlvcd;
4701 LVITEMW lvItem;
4703 TRACE("(hdc=%p, nItem=%d, subitems=%p, pos=%s)\n", hdc, nItem, subitems, wine_dbgstr_point(&pos));
4705 /* get information needed for drawing the item */
4706 lvItem.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_PARAM | LVIF_STATE;
4707 if (infoPtr->uView == LV_VIEW_DETAILS) lvItem.mask |= LVIF_INDENT;
4708 lvItem.stateMask = LVIS_SELECTED | LVIS_FOCUSED | LVIS_STATEIMAGEMASK | LVIS_CUT | LVIS_OVERLAYMASK;
4709 lvItem.iItem = nItem;
4710 lvItem.iSubItem = 0;
4711 lvItem.state = 0;
4712 lvItem.lParam = 0;
4713 lvItem.cchTextMax = DISP_TEXT_SIZE;
4714 lvItem.pszText = szDispText;
4715 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return FALSE;
4716 if (lvItem.pszText == LPSTR_TEXTCALLBACKW) lvItem.pszText = callbackW;
4717 TRACE(" lvItem=%s\n", debuglvitem_t(&lvItem, TRUE));
4719 /* now check if we need to update the focus rectangle */
4720 focus = infoPtr->bFocus && (lvItem.state & LVIS_FOCUSED) ? &infoPtr->rcFocus : 0;
4721 if (!focus) lvItem.state &= ~LVIS_FOCUSED;
4723 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, &rcBox, NULL, NULL, NULL, NULL);
4724 OffsetRect(&rcBox, pos.x, pos.y);
4726 /* Full custom draw stage sequence looks like this:
4728 LV_VIEW_DETAILS:
4730 - CDDS_ITEMPREPAINT
4731 - CDDS_ITEMPREPAINT|CDDS_SUBITEM | => sent n times, where n is number of subitems,
4732 CDDS_ITEMPOSTPAINT|CDDS_SUBITEM | including item iself
4733 - CDDS_ITEMPOSTPAINT
4735 other styles:
4737 - CDDS_ITEMPREPAINT
4738 - CDDS_ITEMPOSTPAINT
4741 /* fill in the custom draw structure */
4742 customdraw_fill(&nmlvcd, infoPtr, hdc, &rcBox, &lvItem);
4743 if (cdmode & CDRF_NOTIFYITEMDRAW)
4744 cdsubitemmode = notify_customdraw(infoPtr, CDDS_ITEMPREPAINT, &nmlvcd);
4745 if (cdsubitemmode & CDRF_SKIPDEFAULT) goto postpaint;
4747 if (subitems)
4749 while (iterator_next(subitems))
4751 DWORD subitemstage = CDRF_DODEFAULT;
4753 /* We need to query for each subitem, item's data (subitem == 0) is already here at this point */
4754 if (subitems->nItem)
4756 lvItem.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_PARAM | LVIF_INDENT;
4757 lvItem.stateMask = LVIS_SELECTED | LVIS_FOCUSED | LVIS_STATEIMAGEMASK | LVIS_CUT | LVIS_OVERLAYMASK;
4758 lvItem.iItem = nItem;
4759 lvItem.iSubItem = subitems->nItem;
4760 lvItem.state = 0;
4761 lvItem.lParam = 0;
4762 lvItem.cchTextMax = DISP_TEXT_SIZE;
4763 lvItem.pszText = szDispText;
4764 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return FALSE;
4765 if (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT)
4766 lvItem.state = LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED);
4767 if (lvItem.pszText == LPSTR_TEXTCALLBACKW) lvItem.pszText = callbackW;
4768 TRACE(" lvItem=%s\n", debuglvitem_t(&lvItem, TRUE));
4770 /* update custom draw data */
4771 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, &nmlvcd.nmcd.rc, NULL, NULL, NULL, NULL);
4772 OffsetRect(&nmlvcd.nmcd.rc, pos.x, pos.y);
4773 nmlvcd.iSubItem = subitems->nItem;
4776 if (cdsubitemmode & CDRF_NOTIFYSUBITEMDRAW)
4777 subitemstage = notify_customdraw(infoPtr, CDDS_SUBITEM | CDDS_ITEMPREPAINT, &nmlvcd);
4778 else
4780 nmlvcd.clrTextBk = infoPtr->clrTextBk;
4781 nmlvcd.clrText = infoPtr->clrText;
4784 if (subitems->nItem == 0 || (cdmode & CDRF_NOTIFYITEMDRAW))
4785 prepaint_setup(infoPtr, hdc, &nmlvcd, FALSE);
4786 else if (!(infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT))
4787 prepaint_setup(infoPtr, hdc, &nmlvcd, TRUE);
4789 if (!(subitemstage & CDRF_SKIPDEFAULT))
4790 LISTVIEW_DrawItemPart(infoPtr, &lvItem, &nmlvcd, &pos);
4792 if (subitemstage & CDRF_NOTIFYPOSTPAINT)
4793 subitemstage = notify_customdraw(infoPtr, CDDS_SUBITEM | CDDS_ITEMPOSTPAINT, &nmlvcd);
4796 else
4798 prepaint_setup(infoPtr, hdc, &nmlvcd, FALSE);
4799 LISTVIEW_DrawItemPart(infoPtr, &lvItem, &nmlvcd, &pos);
4802 postpaint:
4803 if (cdsubitemmode & CDRF_NOTIFYPOSTPAINT)
4805 nmlvcd.iSubItem = 0;
4806 notify_customdraw(infoPtr, CDDS_ITEMPOSTPAINT, &nmlvcd);
4809 return TRUE;
4812 /***
4813 * DESCRIPTION:
4814 * Draws listview items when in owner draw mode.
4816 * PARAMETER(S):
4817 * [I] infoPtr : valid pointer to the listview structure
4818 * [I] hdc : device context handle
4820 * RETURN:
4821 * None
4823 static void LISTVIEW_RefreshOwnerDraw(const LISTVIEW_INFO *infoPtr, ITERATOR *i, HDC hdc, DWORD cdmode)
4825 UINT uID = (UINT)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
4826 DWORD cditemmode = CDRF_DODEFAULT;
4827 NMLVCUSTOMDRAW nmlvcd;
4828 POINT Origin, Position;
4829 DRAWITEMSTRUCT dis;
4830 LVITEMW item;
4832 TRACE("()\n");
4834 ZeroMemory(&dis, sizeof(dis));
4836 /* Get scroll info once before loop */
4837 LISTVIEW_GetOrigin(infoPtr, &Origin);
4839 /* iterate through the invalidated rows */
4840 while(iterator_next(i))
4842 item.iItem = i->nItem;
4843 item.iSubItem = 0;
4844 item.mask = LVIF_PARAM | LVIF_STATE;
4845 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
4846 if (!LISTVIEW_GetItemW(infoPtr, &item)) continue;
4848 dis.CtlType = ODT_LISTVIEW;
4849 dis.CtlID = uID;
4850 dis.itemID = item.iItem;
4851 dis.itemAction = ODA_DRAWENTIRE;
4852 dis.itemState = 0;
4853 if (item.state & LVIS_SELECTED) dis.itemState |= ODS_SELECTED;
4854 if (infoPtr->bFocus && (item.state & LVIS_FOCUSED)) dis.itemState |= ODS_FOCUS;
4855 dis.hwndItem = infoPtr->hwndSelf;
4856 dis.hDC = hdc;
4857 LISTVIEW_GetItemOrigin(infoPtr, dis.itemID, &Position);
4858 dis.rcItem.left = Position.x + Origin.x;
4859 dis.rcItem.right = dis.rcItem.left + infoPtr->nItemWidth;
4860 dis.rcItem.top = Position.y + Origin.y;
4861 dis.rcItem.bottom = dis.rcItem.top + infoPtr->nItemHeight;
4862 dis.itemData = item.lParam;
4864 TRACE("item=%s, rcItem=%s\n", debuglvitem_t(&item, TRUE), wine_dbgstr_rect(&dis.rcItem));
4867 * Even if we do not send the CDRF_NOTIFYITEMDRAW we need to fill the nmlvcd
4868 * structure for the rest. of the paint cycle
4870 customdraw_fill(&nmlvcd, infoPtr, hdc, &dis.rcItem, &item);
4871 if (cdmode & CDRF_NOTIFYITEMDRAW)
4872 cditemmode = notify_customdraw(infoPtr, CDDS_PREPAINT, &nmlvcd);
4874 if (!(cditemmode & CDRF_SKIPDEFAULT))
4876 prepaint_setup (infoPtr, hdc, &nmlvcd, FALSE);
4877 SendMessageW(infoPtr->hwndNotify, WM_DRAWITEM, dis.CtlID, (LPARAM)&dis);
4880 if (cditemmode & CDRF_NOTIFYPOSTPAINT)
4881 notify_postpaint(infoPtr, &nmlvcd);
4885 /***
4886 * DESCRIPTION:
4887 * Draws listview items when in report display mode.
4889 * PARAMETER(S):
4890 * [I] infoPtr : valid pointer to the listview structure
4891 * [I] hdc : device context handle
4892 * [I] cdmode : custom draw mode
4894 * RETURN:
4895 * None
4897 static void LISTVIEW_RefreshReport(LISTVIEW_INFO *infoPtr, ITERATOR *i, HDC hdc, DWORD cdmode)
4899 INT rgntype;
4900 RECT rcClip, rcItem;
4901 POINT Origin, Position;
4902 RANGES colRanges;
4903 INT col, index;
4904 ITERATOR j;
4906 TRACE("()\n");
4908 /* figure out what to draw */
4909 rgntype = GetClipBox(hdc, &rcClip);
4910 if (rgntype == NULLREGION) return;
4912 /* Get scroll info once before loop */
4913 LISTVIEW_GetOrigin(infoPtr, &Origin);
4915 colRanges = ranges_create(DPA_GetPtrCount(infoPtr->hdpaColumns));
4917 /* narrow down the columns we need to paint */
4918 for(col = 0; col < DPA_GetPtrCount(infoPtr->hdpaColumns); col++)
4920 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, col, 0);
4922 LISTVIEW_GetHeaderRect(infoPtr, index, &rcItem);
4923 if ((rcItem.right + Origin.x >= rcClip.left) && (rcItem.left + Origin.x < rcClip.right))
4924 ranges_additem(colRanges, index);
4926 iterator_rangesitems(&j, colRanges);
4928 /* in full row select, we _have_ to draw the main item */
4929 if (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT)
4930 j.nSpecial = 0;
4932 /* iterate through the invalidated rows */
4933 while(iterator_next(i))
4935 RANGES subitems;
4936 ITERATOR k;
4938 SelectObject(hdc, infoPtr->hFont);
4939 LISTVIEW_GetItemOrigin(infoPtr, i->nItem, &Position);
4940 Position.y += Origin.y;
4942 subitems = ranges_create(DPA_GetPtrCount(infoPtr->hdpaColumns));
4944 /* iterate through the invalidated columns */
4945 while(iterator_next(&j))
4947 LISTVIEW_GetHeaderRect(infoPtr, j.nItem, &rcItem);
4948 Position.x = (j.nItem == 0) ? rcItem.left + Origin.x : Origin.x;
4950 if (rgntype == COMPLEXREGION && !((infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) && j.nItem == 0))
4952 rcItem.top = 0;
4953 rcItem.bottom = infoPtr->nItemHeight;
4954 OffsetRect(&rcItem, Origin.x, Position.y);
4955 if (!RectVisible(hdc, &rcItem)) continue;
4958 ranges_additem(subitems, j.nItem);
4961 iterator_rangesitems(&k, subitems);
4962 LISTVIEW_DrawItem(infoPtr, hdc, i->nItem, &k, Position, cdmode);
4963 iterator_destroy(&k);
4965 iterator_destroy(&j);
4968 /***
4969 * DESCRIPTION:
4970 * Draws the gridlines if necessary when in report display mode.
4972 * PARAMETER(S):
4973 * [I] infoPtr : valid pointer to the listview structure
4974 * [I] hdc : device context handle
4976 * RETURN:
4977 * None
4979 static void LISTVIEW_RefreshReportGrid(LISTVIEW_INFO *infoPtr, HDC hdc)
4981 INT rgntype;
4982 INT y, itemheight;
4983 INT col, index;
4984 HPEN hPen, hOldPen;
4985 RECT rcClip, rcItem = {0};
4986 POINT Origin;
4987 RANGES colRanges;
4988 ITERATOR j;
4989 BOOL rmost = FALSE;
4991 TRACE("()\n");
4993 /* figure out what to draw */
4994 rgntype = GetClipBox(hdc, &rcClip);
4995 if (rgntype == NULLREGION) return;
4997 /* Get scroll info once before loop */
4998 LISTVIEW_GetOrigin(infoPtr, &Origin);
5000 colRanges = ranges_create(DPA_GetPtrCount(infoPtr->hdpaColumns));
5002 /* narrow down the columns we need to paint */
5003 for(col = 0; col < DPA_GetPtrCount(infoPtr->hdpaColumns); col++)
5005 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, col, 0);
5007 LISTVIEW_GetHeaderRect(infoPtr, index, &rcItem);
5008 if ((rcItem.right + Origin.x >= rcClip.left) && (rcItem.left + Origin.x < rcClip.right))
5009 ranges_additem(colRanges, index);
5012 /* is right most vertical line visible? */
5013 if (DPA_GetPtrCount(infoPtr->hdpaColumns) > 0)
5015 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, 0);
5016 LISTVIEW_GetHeaderRect(infoPtr, index, &rcItem);
5017 rmost = (rcItem.right + Origin.x < rcClip.right);
5020 if ((hPen = CreatePen( PS_SOLID, 1, comctl32_color.clr3dFace )))
5022 hOldPen = SelectObject ( hdc, hPen );
5024 /* draw the vertical lines for the columns */
5025 iterator_rangesitems(&j, colRanges);
5026 while(iterator_next(&j))
5028 LISTVIEW_GetHeaderRect(infoPtr, j.nItem, &rcItem);
5029 if (rcItem.left == 0) continue; /* skip leftmost column */
5030 rcItem.left += Origin.x;
5031 rcItem.right += Origin.x;
5032 rcItem.top = infoPtr->rcList.top;
5033 rcItem.bottom = infoPtr->rcList.bottom;
5034 TRACE("vert col=%d, rcItem=%s\n", j.nItem, wine_dbgstr_rect(&rcItem));
5035 MoveToEx (hdc, rcItem.left, rcItem.top, NULL);
5036 LineTo (hdc, rcItem.left, rcItem.bottom);
5038 iterator_destroy(&j);
5039 /* draw rightmost grid line if visible */
5040 if (rmost)
5042 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX,
5043 DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, 0);
5044 LISTVIEW_GetHeaderRect(infoPtr, index, &rcItem);
5046 rcItem.right += Origin.x;
5048 MoveToEx (hdc, rcItem.right, infoPtr->rcList.top, NULL);
5049 LineTo (hdc, rcItem.right, infoPtr->rcList.bottom);
5052 /* draw the horizontal lines for the rows */
5053 itemheight = LISTVIEW_CalculateItemHeight(infoPtr);
5054 rcItem.left = infoPtr->rcList.left;
5055 rcItem.right = infoPtr->rcList.right;
5056 for(y = Origin.y > 1 ? Origin.y - 1 : itemheight - 1 + Origin.y % itemheight; y<=infoPtr->rcList.bottom; y+=itemheight)
5058 rcItem.bottom = rcItem.top = y;
5059 TRACE("horz rcItem=%s\n", wine_dbgstr_rect(&rcItem));
5060 MoveToEx (hdc, rcItem.left, rcItem.top, NULL);
5061 LineTo (hdc, rcItem.right, rcItem.top);
5064 SelectObject( hdc, hOldPen );
5065 DeleteObject( hPen );
5067 else
5068 ranges_destroy(colRanges);
5071 /***
5072 * DESCRIPTION:
5073 * Draws listview items when in list display mode.
5075 * PARAMETER(S):
5076 * [I] infoPtr : valid pointer to the listview structure
5077 * [I] hdc : device context handle
5078 * [I] cdmode : custom draw mode
5080 * RETURN:
5081 * None
5083 static void LISTVIEW_RefreshList(LISTVIEW_INFO *infoPtr, ITERATOR *i, HDC hdc, DWORD cdmode)
5085 POINT Origin, Position;
5087 /* Get scroll info once before loop */
5088 LISTVIEW_GetOrigin(infoPtr, &Origin);
5090 while(iterator_prev(i))
5092 SelectObject(hdc, infoPtr->hFont);
5093 LISTVIEW_GetItemOrigin(infoPtr, i->nItem, &Position);
5094 Position.x += Origin.x;
5095 Position.y += Origin.y;
5097 LISTVIEW_DrawItem(infoPtr, hdc, i->nItem, NULL, Position, cdmode);
5102 /***
5103 * DESCRIPTION:
5104 * Draws listview items.
5106 * PARAMETER(S):
5107 * [I] infoPtr : valid pointer to the listview structure
5108 * [I] hdc : device context handle
5109 * [I] prcErase : rect to be erased before refresh (may be NULL)
5111 * RETURN:
5112 * NoneX
5114 static void LISTVIEW_Refresh(LISTVIEW_INFO *infoPtr, HDC hdc, const RECT *prcErase)
5116 COLORREF oldTextColor = 0, oldBkColor = 0;
5117 NMLVCUSTOMDRAW nmlvcd;
5118 HFONT hOldFont = 0;
5119 DWORD cdmode;
5120 INT oldBkMode = 0;
5121 RECT rcClient;
5122 ITERATOR i;
5123 HDC hdcOrig = hdc;
5124 HBITMAP hbmp = NULL;
5125 RANGE range;
5127 LISTVIEW_DUMP(infoPtr);
5129 if (infoPtr->dwLvExStyle & LVS_EX_DOUBLEBUFFER) {
5130 TRACE("double buffering\n");
5132 hdc = CreateCompatibleDC(hdcOrig);
5133 if (!hdc) {
5134 ERR("Failed to create DC for backbuffer\n");
5135 return;
5137 hbmp = CreateCompatibleBitmap(hdcOrig, infoPtr->rcList.right,
5138 infoPtr->rcList.bottom);
5139 if (!hbmp) {
5140 ERR("Failed to create bitmap for backbuffer\n");
5141 DeleteDC(hdc);
5142 return;
5145 SelectObject(hdc, hbmp);
5146 SelectObject(hdc, infoPtr->hFont);
5148 if(GetClipBox(hdcOrig, &rcClient))
5149 IntersectClipRect(hdc, rcClient.left, rcClient.top, rcClient.right, rcClient.bottom);
5150 } else {
5151 /* Save dc values we're gonna trash while drawing
5152 * FIXME: Should be done in LISTVIEW_DrawItem() */
5153 hOldFont = SelectObject(hdc, infoPtr->hFont);
5154 oldBkMode = GetBkMode(hdc);
5155 oldBkColor = GetBkColor(hdc);
5156 oldTextColor = GetTextColor(hdc);
5159 infoPtr->bIsDrawing = TRUE;
5161 if (prcErase) {
5162 LISTVIEW_FillBkgnd(infoPtr, hdc, prcErase);
5163 } else if (infoPtr->dwLvExStyle & LVS_EX_DOUBLEBUFFER) {
5164 /* If no erasing was done (usually because RedrawWindow was called
5165 * with RDW_INVALIDATE only) we need to copy the old contents into
5166 * the backbuffer before continuing. */
5167 BitBlt(hdc, infoPtr->rcList.left, infoPtr->rcList.top,
5168 infoPtr->rcList.right - infoPtr->rcList.left,
5169 infoPtr->rcList.bottom - infoPtr->rcList.top,
5170 hdcOrig, infoPtr->rcList.left, infoPtr->rcList.top, SRCCOPY);
5173 GetClientRect(infoPtr->hwndSelf, &rcClient);
5174 customdraw_fill(&nmlvcd, infoPtr, hdc, &rcClient, 0);
5175 cdmode = notify_customdraw(infoPtr, CDDS_PREPAINT, &nmlvcd);
5176 if (cdmode & CDRF_SKIPDEFAULT) goto enddraw;
5178 /* nothing to draw */
5179 if(infoPtr->nItemCount == 0) goto enddraw;
5181 /* figure out what we need to draw */
5182 iterator_visibleitems(&i, infoPtr, hdc);
5183 range = iterator_range(&i);
5185 /* send cache hint notification */
5186 if (infoPtr->dwStyle & LVS_OWNERDATA)
5188 NMLVCACHEHINT nmlv;
5190 ZeroMemory(&nmlv, sizeof(NMLVCACHEHINT));
5191 nmlv.iFrom = range.lower;
5192 nmlv.iTo = range.upper - 1;
5193 notify_hdr(infoPtr, LVN_ODCACHEHINT, &nmlv.hdr);
5196 if ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (infoPtr->uView == LV_VIEW_DETAILS))
5197 LISTVIEW_RefreshOwnerDraw(infoPtr, &i, hdc, cdmode);
5198 else
5200 if (infoPtr->uView == LV_VIEW_DETAILS)
5201 LISTVIEW_RefreshReport(infoPtr, &i, hdc, cdmode);
5202 else /* LV_VIEW_LIST, LV_VIEW_ICON or LV_VIEW_SMALLICON */
5203 LISTVIEW_RefreshList(infoPtr, &i, hdc, cdmode);
5205 /* if we have a focus rect and it's visible, draw it */
5206 if (infoPtr->bFocus && range.lower <= infoPtr->nFocusedItem &&
5207 (range.upper - 1) >= infoPtr->nFocusedItem)
5208 LISTVIEW_DrawFocusRect(infoPtr, hdc);
5210 iterator_destroy(&i);
5212 enddraw:
5213 /* For LVS_EX_GRIDLINES go and draw lines */
5214 /* This includes the case where there were *no* items */
5215 if ((infoPtr->uView == LV_VIEW_DETAILS) && infoPtr->dwLvExStyle & LVS_EX_GRIDLINES)
5216 LISTVIEW_RefreshReportGrid(infoPtr, hdc);
5218 /* Draw marquee rectangle if appropriate */
5219 if (infoPtr->bMarqueeSelect)
5220 DrawFocusRect(hdc, &infoPtr->marqueeDrawRect);
5222 if (cdmode & CDRF_NOTIFYPOSTPAINT)
5223 notify_postpaint(infoPtr, &nmlvcd);
5225 if(hbmp) {
5226 BitBlt(hdcOrig, infoPtr->rcList.left, infoPtr->rcList.top,
5227 infoPtr->rcList.right - infoPtr->rcList.left,
5228 infoPtr->rcList.bottom - infoPtr->rcList.top,
5229 hdc, infoPtr->rcList.left, infoPtr->rcList.top, SRCCOPY);
5231 DeleteObject(hbmp);
5232 DeleteDC(hdc);
5233 } else {
5234 SelectObject(hdc, hOldFont);
5235 SetBkMode(hdc, oldBkMode);
5236 SetBkColor(hdc, oldBkColor);
5237 SetTextColor(hdc, oldTextColor);
5240 infoPtr->bIsDrawing = FALSE;
5244 /***
5245 * DESCRIPTION:
5246 * Calculates the approximate width and height of a given number of items.
5248 * PARAMETER(S):
5249 * [I] infoPtr : valid pointer to the listview structure
5250 * [I] nItemCount : number of items
5251 * [I] wWidth : width
5252 * [I] wHeight : height
5254 * RETURN:
5255 * Returns a DWORD. The width in the low word and the height in high word.
5257 static DWORD LISTVIEW_ApproximateViewRect(const LISTVIEW_INFO *infoPtr, INT nItemCount,
5258 WORD wWidth, WORD wHeight)
5260 DWORD dwViewRect = 0;
5262 if (nItemCount == -1)
5263 nItemCount = infoPtr->nItemCount;
5265 if (infoPtr->uView == LV_VIEW_LIST)
5267 INT nItemCountPerColumn = 1;
5268 INT nColumnCount = 0;
5270 if (wHeight == 0xFFFF)
5272 /* use current height */
5273 wHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
5276 if (wHeight < infoPtr->nItemHeight)
5277 wHeight = infoPtr->nItemHeight;
5279 if (nItemCount > 0)
5281 if (infoPtr->nItemHeight > 0)
5283 nItemCountPerColumn = wHeight / infoPtr->nItemHeight;
5284 if (nItemCountPerColumn == 0)
5285 nItemCountPerColumn = 1;
5287 if (nItemCount % nItemCountPerColumn != 0)
5288 nColumnCount = nItemCount / nItemCountPerColumn;
5289 else
5290 nColumnCount = nItemCount / nItemCountPerColumn + 1;
5294 /* Microsoft padding magic */
5295 wHeight = nItemCountPerColumn * infoPtr->nItemHeight + 2;
5296 wWidth = nColumnCount * infoPtr->nItemWidth + 2;
5298 dwViewRect = MAKELONG(wWidth, wHeight);
5300 else if (infoPtr->uView == LV_VIEW_DETAILS)
5302 RECT rcBox;
5304 if (infoPtr->nItemCount > 0)
5306 LISTVIEW_GetItemBox(infoPtr, 0, &rcBox);
5307 wWidth = rcBox.right - rcBox.left;
5308 wHeight = (rcBox.bottom - rcBox.top) * nItemCount;
5310 else
5312 /* use current height and width */
5313 if (wHeight == 0xffff)
5314 wHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
5315 if (wWidth == 0xffff)
5316 wWidth = infoPtr->rcList.right - infoPtr->rcList.left;
5319 dwViewRect = MAKELONG(wWidth, wHeight);
5321 else if (infoPtr->uView == LV_VIEW_ICON)
5323 UINT rows,cols;
5324 UINT nItemWidth;
5325 UINT nItemHeight;
5327 nItemWidth = infoPtr->iconSpacing.cx;
5328 nItemHeight = infoPtr->iconSpacing.cy;
5330 if (wWidth == 0xffff)
5331 wWidth = infoPtr->rcList.right - infoPtr->rcList.left;
5333 if (wWidth < nItemWidth)
5334 wWidth = nItemWidth;
5336 cols = wWidth / nItemWidth;
5337 if (cols > nItemCount)
5338 cols = nItemCount;
5339 if (cols < 1)
5340 cols = 1;
5342 if (nItemCount)
5344 rows = nItemCount / cols;
5345 if (nItemCount % cols)
5346 rows++;
5348 else
5349 rows = 0;
5351 wHeight = (nItemHeight * rows)+2;
5352 wWidth = (nItemWidth * cols)+2;
5354 dwViewRect = MAKELONG(wWidth, wHeight);
5356 else if (infoPtr->uView == LV_VIEW_SMALLICON)
5357 FIXME("uView == LV_VIEW_SMALLICON: not implemented\n");
5359 return dwViewRect;
5362 /***
5363 * DESCRIPTION:
5364 * Cancel edit label with saving item text.
5366 * PARAMETER(S):
5367 * [I] infoPtr : valid pointer to the listview structure
5369 * RETURN:
5370 * Always returns TRUE.
5372 static LRESULT LISTVIEW_CancelEditLabel(LISTVIEW_INFO *infoPtr)
5374 if (infoPtr->hwndEdit)
5376 /* handle value will be lost after LISTVIEW_EndEditLabelT */
5377 HWND edit = infoPtr->hwndEdit;
5379 LISTVIEW_EndEditLabelT(infoPtr, TRUE, IsWindowUnicode(infoPtr->hwndEdit));
5380 SendMessageW(edit, WM_CLOSE, 0, 0);
5383 return TRUE;
5386 /***
5387 * DESCRIPTION:
5388 * Create a drag image list for the specified item.
5390 * PARAMETER(S):
5391 * [I] infoPtr : valid pointer to the listview structure
5392 * [I] iItem : index of item
5393 * [O] lppt : Upper-left corner of the image
5395 * RETURN:
5396 * Returns a handle to the image list if successful, NULL otherwise.
5398 static HIMAGELIST LISTVIEW_CreateDragImage(LISTVIEW_INFO *infoPtr, INT iItem, LPPOINT lppt)
5400 RECT rcItem;
5401 SIZE size;
5402 POINT pos;
5403 HDC hdc, hdcOrig;
5404 HBITMAP hbmp, hOldbmp;
5405 HFONT hOldFont;
5406 HIMAGELIST dragList = 0;
5407 TRACE("iItem=%d Count=%d\n", iItem, infoPtr->nItemCount);
5409 if (iItem < 0 || iItem >= infoPtr->nItemCount || !lppt)
5410 return 0;
5412 rcItem.left = LVIR_BOUNDS;
5413 if (!LISTVIEW_GetItemRect(infoPtr, iItem, &rcItem))
5414 return 0;
5416 lppt->x = rcItem.left;
5417 lppt->y = rcItem.top;
5419 size.cx = rcItem.right - rcItem.left;
5420 size.cy = rcItem.bottom - rcItem.top;
5422 hdcOrig = GetDC(infoPtr->hwndSelf);
5423 hdc = CreateCompatibleDC(hdcOrig);
5424 hbmp = CreateCompatibleBitmap(hdcOrig, size.cx, size.cy);
5425 hOldbmp = SelectObject(hdc, hbmp);
5426 hOldFont = SelectObject(hdc, infoPtr->hFont);
5428 rcItem.left = rcItem.top = 0;
5429 rcItem.right = size.cx;
5430 rcItem.bottom = size.cy;
5431 FillRect(hdc, &rcItem, infoPtr->hBkBrush);
5433 pos.x = pos.y = 0;
5434 if (LISTVIEW_DrawItem(infoPtr, hdc, iItem, NULL, pos, CDRF_DODEFAULT))
5436 dragList = ImageList_Create(size.cx, size.cy, ILC_COLOR, 10, 10);
5437 SelectObject(hdc, hOldbmp);
5438 ImageList_Add(dragList, hbmp, 0);
5440 else
5441 SelectObject(hdc, hOldbmp);
5443 SelectObject(hdc, hOldFont);
5444 DeleteObject(hbmp);
5445 DeleteDC(hdc);
5446 ReleaseDC(infoPtr->hwndSelf, hdcOrig);
5448 TRACE("ret=%p\n", dragList);
5450 return dragList;
5454 /***
5455 * DESCRIPTION:
5456 * Removes all listview items and subitems.
5458 * PARAMETER(S):
5459 * [I] infoPtr : valid pointer to the listview structure
5461 * RETURN:
5462 * SUCCESS : TRUE
5463 * FAILURE : FALSE
5465 static BOOL LISTVIEW_DeleteAllItems(LISTVIEW_INFO *infoPtr, BOOL destroy)
5467 HDPA hdpaSubItems = NULL;
5468 BOOL suppress = FALSE;
5469 ITEMHDR *hdrItem;
5470 ITEM_INFO *lpItem;
5471 ITEM_ID *lpID;
5472 INT i, j;
5474 TRACE("()\n");
5476 /* we do it directly, to avoid notifications */
5477 ranges_clear(infoPtr->selectionRanges);
5478 infoPtr->nSelectionMark = -1;
5479 infoPtr->nFocusedItem = -1;
5480 SetRectEmpty(&infoPtr->rcFocus);
5481 /* But we are supposed to leave nHotItem as is! */
5483 /* send LVN_DELETEALLITEMS notification */
5484 if (!(infoPtr->dwStyle & LVS_OWNERDATA) || !destroy)
5486 NMLISTVIEW nmlv;
5488 memset(&nmlv, 0, sizeof(NMLISTVIEW));
5489 nmlv.iItem = -1;
5490 suppress = notify_listview(infoPtr, LVN_DELETEALLITEMS, &nmlv);
5493 for (i = infoPtr->nItemCount - 1; i >= 0; i--)
5495 if (!(infoPtr->dwStyle & LVS_OWNERDATA))
5497 /* send LVN_DELETEITEM notification, if not suppressed
5498 and if it is not a virtual listview */
5499 if (!suppress) notify_deleteitem(infoPtr, i);
5500 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, i);
5501 lpItem = DPA_GetPtr(hdpaSubItems, 0);
5502 /* free id struct */
5503 j = DPA_GetPtrIndex(infoPtr->hdpaItemIds, lpItem->id);
5504 lpID = DPA_GetPtr(infoPtr->hdpaItemIds, j);
5505 DPA_DeletePtr(infoPtr->hdpaItemIds, j);
5506 Free(lpID);
5507 /* both item and subitem start with ITEMHDR header */
5508 for (j = 0; j < DPA_GetPtrCount(hdpaSubItems); j++)
5510 hdrItem = DPA_GetPtr(hdpaSubItems, j);
5511 if (is_text(hdrItem->pszText)) Free(hdrItem->pszText);
5512 Free(hdrItem);
5514 DPA_Destroy(hdpaSubItems);
5515 DPA_DeletePtr(infoPtr->hdpaItems, i);
5517 DPA_DeletePtr(infoPtr->hdpaPosX, i);
5518 DPA_DeletePtr(infoPtr->hdpaPosY, i);
5519 infoPtr->nItemCount --;
5522 if (!destroy)
5524 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
5525 LISTVIEW_UpdateScroll(infoPtr);
5527 LISTVIEW_InvalidateList(infoPtr);
5529 return TRUE;
5532 /***
5533 * DESCRIPTION:
5534 * Scrolls, and updates the columns, when a column is changing width.
5536 * PARAMETER(S):
5537 * [I] infoPtr : valid pointer to the listview structure
5538 * [I] nColumn : column to scroll
5539 * [I] dx : amount of scroll, in pixels
5541 * RETURN:
5542 * None.
5544 static void LISTVIEW_ScrollColumns(LISTVIEW_INFO *infoPtr, INT nColumn, INT dx)
5546 COLUMN_INFO *lpColumnInfo;
5547 RECT rcOld, rcCol;
5548 POINT ptOrigin;
5549 INT nCol;
5550 HDITEMW hdi;
5552 if (nColumn < 0 || DPA_GetPtrCount(infoPtr->hdpaColumns) < 1) return;
5553 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, min(nColumn, DPA_GetPtrCount(infoPtr->hdpaColumns) - 1));
5554 rcCol = lpColumnInfo->rcHeader;
5555 if (nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns))
5556 rcCol.left = rcCol.right;
5558 /* adjust the other columns */
5559 hdi.mask = HDI_ORDER;
5560 if (SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nColumn, (LPARAM)&hdi))
5562 INT nOrder = hdi.iOrder;
5563 for (nCol = 0; nCol < DPA_GetPtrCount(infoPtr->hdpaColumns); nCol++)
5565 hdi.mask = HDI_ORDER;
5566 SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nCol, (LPARAM)&hdi);
5567 if (hdi.iOrder >= nOrder) {
5568 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, nCol);
5569 lpColumnInfo->rcHeader.left += dx;
5570 lpColumnInfo->rcHeader.right += dx;
5575 /* do not update screen if not in report mode */
5576 if (!is_redrawing(infoPtr) || infoPtr->uView != LV_VIEW_DETAILS) return;
5578 /* Need to reset the item width when inserting a new column */
5579 infoPtr->nItemWidth += dx;
5581 LISTVIEW_UpdateScroll(infoPtr);
5582 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
5584 /* scroll to cover the deleted column, and invalidate for redraw */
5585 rcOld = infoPtr->rcList;
5586 rcOld.left = ptOrigin.x + rcCol.left + dx;
5587 ScrollWindowEx(infoPtr->hwndSelf, dx, 0, &rcOld, &rcOld, 0, 0, SW_ERASE | SW_INVALIDATE);
5590 /***
5591 * DESCRIPTION:
5592 * Removes a column from the listview control.
5594 * PARAMETER(S):
5595 * [I] infoPtr : valid pointer to the listview structure
5596 * [I] nColumn : column index
5598 * RETURN:
5599 * SUCCESS : TRUE
5600 * FAILURE : FALSE
5602 static BOOL LISTVIEW_DeleteColumn(LISTVIEW_INFO *infoPtr, INT nColumn)
5604 RECT rcCol;
5606 TRACE("nColumn=%d\n", nColumn);
5608 if (nColumn < 0 || DPA_GetPtrCount(infoPtr->hdpaColumns) == 0
5609 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
5611 /* While the MSDN specifically says that column zero should not be deleted,
5612 what actually happens is that the column itself is deleted but no items or subitems
5613 are removed.
5616 LISTVIEW_GetHeaderRect(infoPtr, nColumn, &rcCol);
5618 if (!SendMessageW(infoPtr->hwndHeader, HDM_DELETEITEM, nColumn, 0))
5619 return FALSE;
5621 Free(DPA_GetPtr(infoPtr->hdpaColumns, nColumn));
5622 DPA_DeletePtr(infoPtr->hdpaColumns, nColumn);
5624 if (!(infoPtr->dwStyle & LVS_OWNERDATA) && nColumn)
5626 SUBITEM_INFO *lpSubItem, *lpDelItem;
5627 HDPA hdpaSubItems;
5628 INT nItem, nSubItem, i;
5630 for (nItem = 0; nItem < infoPtr->nItemCount; nItem++)
5632 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, nItem);
5633 nSubItem = 0;
5634 lpDelItem = 0;
5635 for (i = 1; i < DPA_GetPtrCount(hdpaSubItems); i++)
5637 lpSubItem = DPA_GetPtr(hdpaSubItems, i);
5638 if (lpSubItem->iSubItem == nColumn)
5640 nSubItem = i;
5641 lpDelItem = lpSubItem;
5643 else if (lpSubItem->iSubItem > nColumn)
5645 lpSubItem->iSubItem--;
5649 /* if we found our subitem, zap it */
5650 if (nSubItem > 0)
5652 /* free string */
5653 if (is_text(lpDelItem->hdr.pszText))
5654 Free(lpDelItem->hdr.pszText);
5656 /* free item */
5657 Free(lpDelItem);
5659 /* free dpa memory */
5660 DPA_DeletePtr(hdpaSubItems, nSubItem);
5665 /* update the other column info */
5666 LISTVIEW_UpdateItemSize(infoPtr);
5667 if(DPA_GetPtrCount(infoPtr->hdpaColumns) == 0)
5668 LISTVIEW_InvalidateList(infoPtr);
5669 else
5670 LISTVIEW_ScrollColumns(infoPtr, nColumn, -(rcCol.right - rcCol.left));
5672 return TRUE;
5675 /***
5676 * DESCRIPTION:
5677 * Invalidates the listview after an item's insertion or deletion.
5679 * PARAMETER(S):
5680 * [I] infoPtr : valid pointer to the listview structure
5681 * [I] nItem : item index
5682 * [I] dir : -1 if deleting, 1 if inserting
5684 * RETURN:
5685 * None
5687 static void LISTVIEW_ScrollOnInsert(LISTVIEW_INFO *infoPtr, INT nItem, INT dir)
5689 INT nPerCol, nItemCol, nItemRow;
5690 RECT rcScroll;
5691 POINT Origin;
5693 /* if we don't refresh, what's the point of scrolling? */
5694 if (!is_redrawing(infoPtr)) return;
5696 assert (abs(dir) == 1);
5698 /* arrange icons if autoarrange is on */
5699 if (is_autoarrange(infoPtr))
5701 BOOL arrange = TRUE;
5702 if (dir < 0 && nItem >= infoPtr->nItemCount) arrange = FALSE;
5703 if (dir > 0 && nItem == infoPtr->nItemCount - 1) arrange = FALSE;
5704 if (arrange) LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
5707 /* scrollbars need updating */
5708 LISTVIEW_UpdateScroll(infoPtr);
5710 /* figure out the item's position */
5711 if (infoPtr->uView == LV_VIEW_DETAILS)
5712 nPerCol = infoPtr->nItemCount + 1;
5713 else if (infoPtr->uView == LV_VIEW_LIST)
5714 nPerCol = LISTVIEW_GetCountPerColumn(infoPtr);
5715 else /* LV_VIEW_ICON, or LV_VIEW_SMALLICON */
5716 return;
5718 nItemCol = nItem / nPerCol;
5719 nItemRow = nItem % nPerCol;
5720 LISTVIEW_GetOrigin(infoPtr, &Origin);
5722 /* move the items below up a slot */
5723 rcScroll.left = nItemCol * infoPtr->nItemWidth;
5724 rcScroll.top = nItemRow * infoPtr->nItemHeight;
5725 rcScroll.right = rcScroll.left + infoPtr->nItemWidth;
5726 rcScroll.bottom = nPerCol * infoPtr->nItemHeight;
5727 OffsetRect(&rcScroll, Origin.x, Origin.y);
5728 TRACE("rcScroll=%s, dx=%d\n", wine_dbgstr_rect(&rcScroll), dir * infoPtr->nItemHeight);
5729 if (IntersectRect(&rcScroll, &rcScroll, &infoPtr->rcList))
5731 TRACE("Invalidating rcScroll=%s, rcList=%s\n", wine_dbgstr_rect(&rcScroll), wine_dbgstr_rect(&infoPtr->rcList));
5732 InvalidateRect(infoPtr->hwndSelf, &rcScroll, TRUE);
5735 /* report has only that column, so we're done */
5736 if (infoPtr->uView == LV_VIEW_DETAILS) return;
5738 /* now for LISTs, we have to deal with the columns to the right */
5739 rcScroll.left = (nItemCol + 1) * infoPtr->nItemWidth;
5740 rcScroll.top = 0;
5741 rcScroll.right = (infoPtr->nItemCount / nPerCol + 1) * infoPtr->nItemWidth;
5742 rcScroll.bottom = nPerCol * infoPtr->nItemHeight;
5743 OffsetRect(&rcScroll, Origin.x, Origin.y);
5744 if (IntersectRect(&rcScroll, &rcScroll, &infoPtr->rcList))
5745 InvalidateRect(infoPtr->hwndSelf, &rcScroll, TRUE);
5748 /***
5749 * DESCRIPTION:
5750 * Removes an item from the listview control.
5752 * PARAMETER(S):
5753 * [I] infoPtr : valid pointer to the listview structure
5754 * [I] nItem : item index
5756 * RETURN:
5757 * SUCCESS : TRUE
5758 * FAILURE : FALSE
5760 static BOOL LISTVIEW_DeleteItem(LISTVIEW_INFO *infoPtr, INT nItem)
5762 LVITEMW item;
5763 const BOOL is_icon = (infoPtr->uView == LV_VIEW_SMALLICON || infoPtr->uView == LV_VIEW_ICON);
5764 INT focus = infoPtr->nFocusedItem;
5766 TRACE("(nItem=%d)\n", nItem);
5768 if (nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
5770 /* remove selection, and focus */
5771 item.state = 0;
5772 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
5773 LISTVIEW_SetItemState(infoPtr, nItem, &item);
5775 /* send LVN_DELETEITEM notification. */
5776 if (!notify_deleteitem(infoPtr, nItem)) return FALSE;
5778 /* we need to do this here, because we'll be deleting stuff */
5779 if (is_icon)
5780 LISTVIEW_InvalidateItem(infoPtr, nItem);
5782 if (!(infoPtr->dwStyle & LVS_OWNERDATA))
5784 HDPA hdpaSubItems;
5785 ITEMHDR *hdrItem;
5786 ITEM_INFO *lpItem;
5787 ITEM_ID *lpID;
5788 INT i;
5790 hdpaSubItems = DPA_DeletePtr(infoPtr->hdpaItems, nItem);
5791 lpItem = DPA_GetPtr(hdpaSubItems, 0);
5793 /* free id struct */
5794 i = DPA_GetPtrIndex(infoPtr->hdpaItemIds, lpItem->id);
5795 lpID = DPA_GetPtr(infoPtr->hdpaItemIds, i);
5796 DPA_DeletePtr(infoPtr->hdpaItemIds, i);
5797 Free(lpID);
5798 for (i = 0; i < DPA_GetPtrCount(hdpaSubItems); i++)
5800 hdrItem = DPA_GetPtr(hdpaSubItems, i);
5801 if (is_text(hdrItem->pszText)) Free(hdrItem->pszText);
5802 Free(hdrItem);
5804 DPA_Destroy(hdpaSubItems);
5807 if (is_icon)
5809 DPA_DeletePtr(infoPtr->hdpaPosX, nItem);
5810 DPA_DeletePtr(infoPtr->hdpaPosY, nItem);
5813 infoPtr->nItemCount--;
5814 LISTVIEW_ShiftIndices(infoPtr, nItem, -1);
5815 LISTVIEW_ShiftFocus(infoPtr, focus, nItem, -1);
5817 /* now is the invalidation fun */
5818 if (!is_icon)
5819 LISTVIEW_ScrollOnInsert(infoPtr, nItem, -1);
5820 return TRUE;
5824 /***
5825 * DESCRIPTION:
5826 * Callback implementation for editlabel control
5828 * PARAMETER(S):
5829 * [I] infoPtr : valid pointer to the listview structure
5830 * [I] storeText : store edit box text as item text
5831 * [I] isW : TRUE if psxText is Unicode, FALSE if it's ANSI
5833 * RETURN:
5834 * SUCCESS : TRUE
5835 * FAILURE : FALSE
5837 static BOOL LISTVIEW_EndEditLabelT(LISTVIEW_INFO *infoPtr, BOOL storeText, BOOL isW)
5839 HWND hwndSelf = infoPtr->hwndSelf;
5840 WCHAR szDispText[DISP_TEXT_SIZE] = { 0 };
5841 NMLVDISPINFOW dispInfo;
5842 INT editedItem = infoPtr->nEditLabelItem;
5843 BOOL same;
5844 WCHAR *pszText = NULL;
5845 BOOL res;
5847 if (storeText)
5849 DWORD len = isW ? GetWindowTextLengthW(infoPtr->hwndEdit) : GetWindowTextLengthA(infoPtr->hwndEdit);
5851 if (len)
5853 if ((pszText = Alloc((len+1) * (isW ? sizeof(WCHAR) : sizeof(CHAR)))))
5855 if (isW) GetWindowTextW(infoPtr->hwndEdit, pszText, len+1);
5856 else GetWindowTextA(infoPtr->hwndEdit, (CHAR*)pszText, len+1);
5861 TRACE("(pszText=%s, isW=%d)\n", debugtext_t(pszText, isW), isW);
5863 ZeroMemory(&dispInfo, sizeof(dispInfo));
5864 dispInfo.item.mask = LVIF_PARAM | LVIF_STATE | LVIF_TEXT;
5865 dispInfo.item.iItem = editedItem;
5866 dispInfo.item.iSubItem = 0;
5867 dispInfo.item.stateMask = ~0;
5868 dispInfo.item.pszText = szDispText;
5869 dispInfo.item.cchTextMax = DISP_TEXT_SIZE;
5870 if (!LISTVIEW_GetItemT(infoPtr, &dispInfo.item, isW))
5872 res = FALSE;
5873 goto cleanup;
5876 if (isW)
5877 same = (lstrcmpW(dispInfo.item.pszText, pszText) == 0);
5878 else
5880 LPWSTR tmp = textdupTtoW(pszText, FALSE);
5881 same = (lstrcmpW(dispInfo.item.pszText, tmp) == 0);
5882 textfreeT(tmp, FALSE);
5885 /* add the text from the edit in */
5886 dispInfo.item.mask |= LVIF_TEXT;
5887 dispInfo.item.pszText = same ? NULL : pszText;
5888 dispInfo.item.cchTextMax = textlenT(dispInfo.item.pszText, isW);
5890 /* Do we need to update the Item Text */
5891 res = notify_dispinfoT(infoPtr, LVN_ENDLABELEDITW, &dispInfo, isW);
5893 infoPtr->nEditLabelItem = -1;
5894 infoPtr->hwndEdit = 0;
5896 if (!res) goto cleanup;
5898 if (!IsWindow(hwndSelf))
5900 res = FALSE;
5901 goto cleanup;
5903 if (!pszText) return TRUE;
5904 if (same)
5906 res = TRUE;
5907 goto cleanup;
5910 if (!(infoPtr->dwStyle & LVS_OWNERDATA))
5912 HDPA hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, editedItem);
5913 ITEM_INFO* lpItem = DPA_GetPtr(hdpaSubItems, 0);
5914 if (lpItem && lpItem->hdr.pszText == LPSTR_TEXTCALLBACKW)
5916 LISTVIEW_InvalidateItem(infoPtr, editedItem);
5917 res = TRUE;
5918 goto cleanup;
5922 ZeroMemory(&dispInfo, sizeof(dispInfo));
5923 dispInfo.item.mask = LVIF_TEXT;
5924 dispInfo.item.iItem = editedItem;
5925 dispInfo.item.iSubItem = 0;
5926 dispInfo.item.pszText = pszText;
5927 dispInfo.item.cchTextMax = textlenT(pszText, isW);
5928 res = LISTVIEW_SetItemT(infoPtr, &dispInfo.item, isW);
5930 cleanup:
5931 Free(pszText);
5933 return res;
5936 /***
5937 * DESCRIPTION:
5938 * Subclassed edit control windproc function
5940 * PARAMETER(S):
5941 * [I] hwnd : the edit window handle
5942 * [I] uMsg : the message that is to be processed
5943 * [I] wParam : first message parameter
5944 * [I] lParam : second message parameter
5945 * [I] isW : TRUE if input is Unicode
5947 * RETURN:
5948 * Zero.
5950 static LRESULT EditLblWndProcT(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL isW)
5952 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongPtrW(GetParent(hwnd), 0);
5953 BOOL save = TRUE;
5955 TRACE("(hwnd=%p, uMsg=%x, wParam=%lx, lParam=%lx, isW=%d)\n",
5956 hwnd, uMsg, wParam, lParam, isW);
5958 switch (uMsg)
5960 case WM_GETDLGCODE:
5961 return DLGC_WANTARROWS | DLGC_WANTALLKEYS;
5963 case WM_DESTROY:
5965 WNDPROC editProc = infoPtr->EditWndProc;
5966 infoPtr->EditWndProc = 0;
5967 SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (DWORD_PTR)editProc);
5968 return CallWindowProcT(editProc, hwnd, uMsg, wParam, lParam, isW);
5971 case WM_KEYDOWN:
5972 if (VK_ESCAPE == (INT)wParam)
5974 save = FALSE;
5975 break;
5977 else if (VK_RETURN == (INT)wParam)
5978 break;
5980 default:
5981 return CallWindowProcT(infoPtr->EditWndProc, hwnd, uMsg, wParam, lParam, isW);
5984 /* kill the edit */
5985 if (infoPtr->hwndEdit)
5986 LISTVIEW_EndEditLabelT(infoPtr, save, isW);
5988 SendMessageW(hwnd, WM_CLOSE, 0, 0);
5989 return 0;
5992 /***
5993 * DESCRIPTION:
5994 * Subclassed edit control Unicode windproc function
5996 * PARAMETER(S):
5997 * [I] hwnd : the edit window handle
5998 * [I] uMsg : the message that is to be processed
5999 * [I] wParam : first message parameter
6000 * [I] lParam : second message parameter
6002 * RETURN:
6004 static LRESULT CALLBACK EditLblWndProcW(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
6006 return EditLblWndProcT(hwnd, uMsg, wParam, lParam, TRUE);
6009 /***
6010 * DESCRIPTION:
6011 * Subclassed edit control ANSI 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 EditLblWndProcA(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
6023 return EditLblWndProcT(hwnd, uMsg, wParam, lParam, FALSE);
6026 /***
6027 * DESCRIPTION:
6028 * Creates a subclassed edit control
6030 * PARAMETER(S):
6031 * [I] infoPtr : valid pointer to the listview structure
6032 * [I] text : initial text for the edit
6033 * [I] style : the window style
6034 * [I] isW : TRUE if input is Unicode
6036 * RETURN:
6038 static HWND CreateEditLabelT(LISTVIEW_INFO *infoPtr, LPCWSTR text, BOOL isW)
6040 static const DWORD style = WS_CHILDWINDOW|WS_CLIPSIBLINGS|ES_LEFT|ES_AUTOHSCROLL|WS_BORDER|WS_VISIBLE;
6041 HINSTANCE hinst = (HINSTANCE)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_HINSTANCE);
6042 HWND hedit;
6044 TRACE("(%p, text=%s, isW=%d)\n", infoPtr, debugtext_t(text, isW), isW);
6046 /* window will be resized and positioned after LVN_BEGINLABELEDIT */
6047 if (isW)
6048 hedit = CreateWindowW(WC_EDITW, text, style, 0, 0, 0, 0, infoPtr->hwndSelf, 0, hinst, 0);
6049 else
6050 hedit = CreateWindowA(WC_EDITA, (LPCSTR)text, style, 0, 0, 0, 0, infoPtr->hwndSelf, 0, hinst, 0);
6052 if (!hedit) return 0;
6054 infoPtr->EditWndProc = (WNDPROC)
6055 (isW ? SetWindowLongPtrW(hedit, GWLP_WNDPROC, (DWORD_PTR)EditLblWndProcW) :
6056 SetWindowLongPtrA(hedit, GWLP_WNDPROC, (DWORD_PTR)EditLblWndProcA) );
6058 SendMessageW(hedit, WM_SETFONT, (WPARAM)infoPtr->hFont, FALSE);
6059 SendMessageW(hedit, EM_SETLIMITTEXT, DISP_TEXT_SIZE-1, 0);
6061 return hedit;
6064 /***
6065 * DESCRIPTION:
6066 * Begin in place editing of specified list view item
6068 * PARAMETER(S):
6069 * [I] infoPtr : valid pointer to the listview structure
6070 * [I] nItem : item index
6071 * [I] isW : TRUE if it's a Unicode req, FALSE if ASCII
6073 * RETURN:
6074 * SUCCESS : TRUE
6075 * FAILURE : FALSE
6077 static HWND LISTVIEW_EditLabelT(LISTVIEW_INFO *infoPtr, INT nItem, BOOL isW)
6079 WCHAR disptextW[DISP_TEXT_SIZE] = { 0 };
6080 HWND hwndSelf = infoPtr->hwndSelf;
6081 NMLVDISPINFOW dispInfo;
6082 HFONT hOldFont = NULL;
6083 TEXTMETRICW tm;
6084 RECT rect;
6085 SIZE sz;
6086 HDC hdc;
6088 TRACE("(nItem=%d, isW=%d)\n", nItem, isW);
6090 if (~infoPtr->dwStyle & LVS_EDITLABELS) return 0;
6092 /* remove existing edit box */
6093 if (infoPtr->hwndEdit)
6095 SetFocus(infoPtr->hwndSelf);
6096 infoPtr->hwndEdit = 0;
6099 if (nItem < 0 || nItem >= infoPtr->nItemCount) return 0;
6101 infoPtr->nEditLabelItem = nItem;
6103 LISTVIEW_SetSelection(infoPtr, nItem);
6104 LISTVIEW_SetItemFocus(infoPtr, nItem);
6105 LISTVIEW_InvalidateItem(infoPtr, nItem);
6107 rect.left = LVIR_LABEL;
6108 if (!LISTVIEW_GetItemRect(infoPtr, nItem, &rect)) return 0;
6110 ZeroMemory(&dispInfo, sizeof(dispInfo));
6111 dispInfo.item.mask = LVIF_PARAM | LVIF_STATE | LVIF_TEXT;
6112 dispInfo.item.iItem = nItem;
6113 dispInfo.item.iSubItem = 0;
6114 dispInfo.item.stateMask = ~0;
6115 dispInfo.item.pszText = disptextW;
6116 dispInfo.item.cchTextMax = DISP_TEXT_SIZE;
6117 if (!LISTVIEW_GetItemT(infoPtr, &dispInfo.item, isW)) return 0;
6119 infoPtr->hwndEdit = CreateEditLabelT(infoPtr, dispInfo.item.pszText, isW);
6120 if (!infoPtr->hwndEdit) return 0;
6122 if (notify_dispinfoT(infoPtr, LVN_BEGINLABELEDITW, &dispInfo, isW))
6124 if (!IsWindow(hwndSelf))
6125 return 0;
6126 SendMessageW(infoPtr->hwndEdit, WM_CLOSE, 0, 0);
6127 infoPtr->hwndEdit = 0;
6128 return 0;
6131 TRACE("disp text=%s\n", debugtext_t(dispInfo.item.pszText, isW));
6133 /* position and display edit box */
6134 hdc = GetDC(infoPtr->hwndSelf);
6136 /* select the font to get appropriate metric dimensions */
6137 if (infoPtr->hFont)
6138 hOldFont = SelectObject(hdc, infoPtr->hFont);
6140 /* use real edit box content, it could be altered during LVN_BEGINLABELEDIT notification */
6141 GetWindowTextW(infoPtr->hwndEdit, disptextW, DISP_TEXT_SIZE);
6142 TRACE("edit box text=%s\n", debugstr_w(disptextW));
6144 /* get string length in pixels */
6145 GetTextExtentPoint32W(hdc, disptextW, lstrlenW(disptextW), &sz);
6147 /* add extra spacing for the next character */
6148 GetTextMetricsW(hdc, &tm);
6149 sz.cx += tm.tmMaxCharWidth * 2;
6151 if (infoPtr->hFont)
6152 SelectObject(hdc, hOldFont);
6154 ReleaseDC(infoPtr->hwndSelf, hdc);
6156 sz.cy = rect.bottom - rect.top + 2;
6157 rect.left -= 2;
6158 rect.top -= 1;
6159 TRACE("moving edit=(%d,%d)-(%d,%d)\n", rect.left, rect.top, sz.cx, sz.cy);
6160 MoveWindow(infoPtr->hwndEdit, rect.left, rect.top, sz.cx, sz.cy, FALSE);
6161 ShowWindow(infoPtr->hwndEdit, SW_NORMAL);
6162 SetFocus(infoPtr->hwndEdit);
6163 SendMessageW(infoPtr->hwndEdit, EM_SETSEL, 0, -1);
6164 return infoPtr->hwndEdit;
6168 /***
6169 * DESCRIPTION:
6170 * Ensures the specified item is visible, scrolling into view if necessary.
6172 * PARAMETER(S):
6173 * [I] infoPtr : valid pointer to the listview structure
6174 * [I] nItem : item index
6175 * [I] bPartial : partially or entirely visible
6177 * RETURN:
6178 * SUCCESS : TRUE
6179 * FAILURE : FALSE
6181 static BOOL LISTVIEW_EnsureVisible(LISTVIEW_INFO *infoPtr, INT nItem, BOOL bPartial)
6183 INT nScrollPosHeight = 0;
6184 INT nScrollPosWidth = 0;
6185 INT nHorzAdjust = 0;
6186 INT nVertAdjust = 0;
6187 INT nHorzDiff = 0;
6188 INT nVertDiff = 0;
6189 RECT rcItem, rcTemp;
6191 rcItem.left = LVIR_BOUNDS;
6192 if (!LISTVIEW_GetItemRect(infoPtr, nItem, &rcItem)) return FALSE;
6194 if (bPartial && IntersectRect(&rcTemp, &infoPtr->rcList, &rcItem)) return TRUE;
6196 if (rcItem.left < infoPtr->rcList.left || rcItem.right > infoPtr->rcList.right)
6198 /* scroll left/right, but in LV_VIEW_DETAILS mode */
6199 if (infoPtr->uView == LV_VIEW_LIST)
6200 nScrollPosWidth = infoPtr->nItemWidth;
6201 else if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
6202 nScrollPosWidth = 1;
6204 if (rcItem.left < infoPtr->rcList.left)
6206 nHorzAdjust = -1;
6207 if (infoPtr->uView != LV_VIEW_DETAILS) nHorzDiff = rcItem.left - infoPtr->rcList.left;
6209 else
6211 nHorzAdjust = 1;
6212 if (infoPtr->uView != LV_VIEW_DETAILS) nHorzDiff = rcItem.right - infoPtr->rcList.right;
6216 if (rcItem.top < infoPtr->rcList.top || rcItem.bottom > infoPtr->rcList.bottom)
6218 /* scroll up/down, but not in LVS_LIST mode */
6219 if (infoPtr->uView == LV_VIEW_DETAILS)
6220 nScrollPosHeight = infoPtr->nItemHeight;
6221 else if ((infoPtr->uView == LV_VIEW_ICON) || (infoPtr->uView == LV_VIEW_SMALLICON))
6222 nScrollPosHeight = 1;
6224 if (rcItem.top < infoPtr->rcList.top)
6226 nVertAdjust = -1;
6227 if (infoPtr->uView != LV_VIEW_LIST) nVertDiff = rcItem.top - infoPtr->rcList.top;
6229 else
6231 nVertAdjust = 1;
6232 if (infoPtr->uView != LV_VIEW_LIST) nVertDiff = rcItem.bottom - infoPtr->rcList.bottom;
6236 if (!nScrollPosWidth && !nScrollPosHeight) return TRUE;
6238 if (nScrollPosWidth)
6240 INT diff = nHorzDiff / nScrollPosWidth;
6241 if (nHorzDiff % nScrollPosWidth) diff += nHorzAdjust;
6242 LISTVIEW_HScroll(infoPtr, SB_INTERNAL, diff);
6245 if (nScrollPosHeight)
6247 INT diff = nVertDiff / nScrollPosHeight;
6248 if (nVertDiff % nScrollPosHeight) diff += nVertAdjust;
6249 LISTVIEW_VScroll(infoPtr, SB_INTERNAL, diff);
6252 return TRUE;
6255 /***
6256 * DESCRIPTION:
6257 * Searches for an item with specific characteristics.
6259 * PARAMETER(S):
6260 * [I] hwnd : window handle
6261 * [I] nStart : base item index
6262 * [I] lpFindInfo : item information to look for
6264 * RETURN:
6265 * SUCCESS : index of item
6266 * FAILURE : -1
6268 static INT LISTVIEW_FindItemW(const LISTVIEW_INFO *infoPtr, INT nStart,
6269 const LVFINDINFOW *lpFindInfo)
6271 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
6272 BOOL bWrap = FALSE, bNearest = FALSE;
6273 INT nItem = nStart + 1, nLast = infoPtr->nItemCount, nNearestItem = -1;
6274 ULONG xdist, ydist, dist, mindist = 0x7fffffff;
6275 POINT Position, Destination;
6276 LVITEMW lvItem;
6278 /* Search in virtual listviews should be done by application, not by
6279 listview control, so we just send LVN_ODFINDITEMW and return the result */
6280 if (infoPtr->dwStyle & LVS_OWNERDATA)
6282 NMLVFINDITEMW nmlv;
6284 nmlv.iStart = nStart;
6285 nmlv.lvfi = *lpFindInfo;
6286 return notify_hdr(infoPtr, LVN_ODFINDITEMW, (LPNMHDR)&nmlv.hdr);
6289 if (!lpFindInfo || nItem < 0) return -1;
6291 lvItem.mask = 0;
6292 if (lpFindInfo->flags & (LVFI_STRING | LVFI_PARTIAL) ||
6293 lpFindInfo->flags & LVFI_SUBSTRING)
6295 lvItem.mask |= LVIF_TEXT;
6296 lvItem.pszText = szDispText;
6297 lvItem.cchTextMax = DISP_TEXT_SIZE;
6300 if (lpFindInfo->flags & LVFI_WRAP)
6301 bWrap = TRUE;
6303 if ((lpFindInfo->flags & LVFI_NEARESTXY) &&
6304 (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON))
6306 POINT Origin;
6307 RECT rcArea;
6309 LISTVIEW_GetOrigin(infoPtr, &Origin);
6310 Destination.x = lpFindInfo->pt.x - Origin.x;
6311 Destination.y = lpFindInfo->pt.y - Origin.y;
6312 switch(lpFindInfo->vkDirection)
6314 case VK_DOWN: Destination.y += infoPtr->nItemHeight; break;
6315 case VK_UP: Destination.y -= infoPtr->nItemHeight; break;
6316 case VK_RIGHT: Destination.x += infoPtr->nItemWidth; break;
6317 case VK_LEFT: Destination.x -= infoPtr->nItemWidth; break;
6318 case VK_HOME: Destination.x = Destination.y = 0; break;
6319 case VK_NEXT: Destination.y += infoPtr->rcList.bottom - infoPtr->rcList.top; break;
6320 case VK_PRIOR: Destination.y -= infoPtr->rcList.bottom - infoPtr->rcList.top; break;
6321 case VK_END:
6322 LISTVIEW_GetAreaRect(infoPtr, &rcArea);
6323 Destination.x = rcArea.right;
6324 Destination.y = rcArea.bottom;
6325 break;
6326 default: ERR("Unknown vkDirection=%d\n", lpFindInfo->vkDirection);
6328 bNearest = TRUE;
6330 else Destination.x = Destination.y = 0;
6332 /* if LVFI_PARAM is specified, all other flags are ignored */
6333 if (lpFindInfo->flags & LVFI_PARAM)
6335 lvItem.mask |= LVIF_PARAM;
6336 bNearest = FALSE;
6337 lvItem.mask &= ~LVIF_TEXT;
6340 again:
6341 for (; nItem < nLast; nItem++)
6343 lvItem.iItem = nItem;
6344 lvItem.iSubItem = 0;
6345 lvItem.pszText = szDispText;
6346 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) continue;
6348 if (lvItem.mask & LVIF_PARAM)
6350 if (lpFindInfo->lParam == lvItem.lParam)
6351 return nItem;
6352 else
6353 continue;
6356 if (lvItem.mask & LVIF_TEXT)
6358 if (lpFindInfo->flags & (LVFI_PARTIAL | LVFI_SUBSTRING))
6360 WCHAR *p = strstrW(lvItem.pszText, lpFindInfo->psz);
6361 if (!p || p != lvItem.pszText) continue;
6363 else
6365 if (lstrcmpW(lvItem.pszText, lpFindInfo->psz) != 0) continue;
6369 if (!bNearest) return nItem;
6371 /* This is very inefficient. To do a good job here,
6372 * we need a sorted array of (x,y) item positions */
6373 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
6375 /* compute the distance^2 to the destination */
6376 xdist = Destination.x - Position.x;
6377 ydist = Destination.y - Position.y;
6378 dist = xdist * xdist + ydist * ydist;
6380 /* remember the distance, and item if it's closer */
6381 if (dist < mindist)
6383 mindist = dist;
6384 nNearestItem = nItem;
6388 if (bWrap)
6390 nItem = 0;
6391 nLast = min(nStart + 1, infoPtr->nItemCount);
6392 bWrap = FALSE;
6393 goto again;
6396 return nNearestItem;
6399 /***
6400 * DESCRIPTION:
6401 * Searches for an item with specific characteristics.
6403 * PARAMETER(S):
6404 * [I] hwnd : window handle
6405 * [I] nStart : base item index
6406 * [I] lpFindInfo : item information to look for
6408 * RETURN:
6409 * SUCCESS : index of item
6410 * FAILURE : -1
6412 static INT LISTVIEW_FindItemA(const LISTVIEW_INFO *infoPtr, INT nStart,
6413 const LVFINDINFOA *lpFindInfo)
6415 BOOL hasText = lpFindInfo->flags & (LVFI_STRING | LVFI_PARTIAL) ||
6416 lpFindInfo->flags & LVFI_SUBSTRING;
6417 LVFINDINFOW fiw;
6418 INT res;
6419 LPWSTR strW = NULL;
6421 memcpy(&fiw, lpFindInfo, sizeof(fiw));
6422 if (hasText) fiw.psz = strW = textdupTtoW((LPCWSTR)lpFindInfo->psz, FALSE);
6423 res = LISTVIEW_FindItemW(infoPtr, nStart, &fiw);
6424 textfreeT(strW, FALSE);
6425 return res;
6428 /***
6429 * DESCRIPTION:
6430 * Retrieves column attributes.
6432 * PARAMETER(S):
6433 * [I] infoPtr : valid pointer to the listview structure
6434 * [I] nColumn : column index
6435 * [IO] lpColumn : column information
6436 * [I] isW : if TRUE, then lpColumn is a LPLVCOLUMNW
6437 * otherwise it is in fact a LPLVCOLUMNA
6439 * RETURN:
6440 * SUCCESS : TRUE
6441 * FAILURE : FALSE
6443 static BOOL LISTVIEW_GetColumnT(const LISTVIEW_INFO *infoPtr, INT nColumn, LPLVCOLUMNW lpColumn, BOOL isW)
6445 COLUMN_INFO *lpColumnInfo;
6446 HDITEMW hdi;
6448 if (!lpColumn || nColumn < 0 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
6449 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, nColumn);
6451 /* initialize memory */
6452 ZeroMemory(&hdi, sizeof(hdi));
6454 if (lpColumn->mask & LVCF_TEXT)
6456 hdi.mask |= HDI_TEXT;
6457 hdi.pszText = lpColumn->pszText;
6458 hdi.cchTextMax = lpColumn->cchTextMax;
6461 if (lpColumn->mask & LVCF_IMAGE)
6462 hdi.mask |= HDI_IMAGE;
6464 if (lpColumn->mask & LVCF_ORDER)
6465 hdi.mask |= HDI_ORDER;
6467 if (lpColumn->mask & LVCF_SUBITEM)
6468 hdi.mask |= HDI_LPARAM;
6470 if (!SendMessageW(infoPtr->hwndHeader, isW ? HDM_GETITEMW : HDM_GETITEMA, nColumn, (LPARAM)&hdi)) return FALSE;
6472 if (lpColumn->mask & LVCF_FMT)
6473 lpColumn->fmt = lpColumnInfo->fmt;
6475 if (lpColumn->mask & LVCF_WIDTH)
6476 lpColumn->cx = lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left;
6478 if (lpColumn->mask & LVCF_IMAGE)
6479 lpColumn->iImage = hdi.iImage;
6481 if (lpColumn->mask & LVCF_ORDER)
6482 lpColumn->iOrder = hdi.iOrder;
6484 if (lpColumn->mask & LVCF_SUBITEM)
6485 lpColumn->iSubItem = hdi.lParam;
6487 if (lpColumn->mask & LVCF_MINWIDTH)
6488 lpColumn->cxMin = lpColumnInfo->cxMin;
6490 return TRUE;
6494 static BOOL LISTVIEW_GetColumnOrderArray(const LISTVIEW_INFO *infoPtr, INT iCount, LPINT lpiArray)
6496 TRACE("iCount=%d, lpiArray=%p\n", iCount, lpiArray);
6498 if (!lpiArray)
6499 return FALSE;
6501 return SendMessageW(infoPtr->hwndHeader, HDM_GETORDERARRAY, iCount, (LPARAM)lpiArray);
6504 /***
6505 * DESCRIPTION:
6506 * Retrieves the column width.
6508 * PARAMETER(S):
6509 * [I] infoPtr : valid pointer to the listview structure
6510 * [I] int : column index
6512 * RETURN:
6513 * SUCCESS : column width
6514 * FAILURE : zero
6516 static INT LISTVIEW_GetColumnWidth(const LISTVIEW_INFO *infoPtr, INT nColumn)
6518 INT nColumnWidth = 0;
6519 HDITEMW hdItem;
6521 TRACE("nColumn=%d\n", nColumn);
6523 /* we have a 'column' in LIST and REPORT mode only */
6524 switch(infoPtr->uView)
6526 case LV_VIEW_LIST:
6527 nColumnWidth = infoPtr->nItemWidth;
6528 break;
6529 case LV_VIEW_DETAILS:
6530 /* We are not using LISTVIEW_GetHeaderRect as this data is updated only after a HDN_ITEMCHANGED.
6531 * There is an application that subclasses the listview, calls LVM_GETCOLUMNWIDTH in the
6532 * HDN_ITEMCHANGED handler and goes into infinite recursion if it receives old data.
6534 * TODO: should we do the same in LVM_GETCOLUMN?
6536 hdItem.mask = HDI_WIDTH;
6537 if (!SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nColumn, (LPARAM)&hdItem))
6539 WARN("(%p): HDM_GETITEMW failed for item %d\n", infoPtr->hwndSelf, nColumn);
6540 return 0;
6542 nColumnWidth = hdItem.cxy;
6543 break;
6546 TRACE("nColumnWidth=%d\n", nColumnWidth);
6547 return nColumnWidth;
6550 /***
6551 * DESCRIPTION:
6552 * In list or report display mode, retrieves the number of items that can fit
6553 * vertically in the visible area. In icon or small icon display mode,
6554 * retrieves the total number of visible items.
6556 * PARAMETER(S):
6557 * [I] infoPtr : valid pointer to the listview structure
6559 * RETURN:
6560 * Number of fully visible items.
6562 static INT LISTVIEW_GetCountPerPage(const LISTVIEW_INFO *infoPtr)
6564 switch (infoPtr->uView)
6566 case LV_VIEW_ICON:
6567 case LV_VIEW_SMALLICON:
6568 return infoPtr->nItemCount;
6569 case LV_VIEW_DETAILS:
6570 return LISTVIEW_GetCountPerColumn(infoPtr);
6571 case LV_VIEW_LIST:
6572 return LISTVIEW_GetCountPerRow(infoPtr) * LISTVIEW_GetCountPerColumn(infoPtr);
6574 assert(FALSE);
6575 return 0;
6578 /***
6579 * DESCRIPTION:
6580 * Retrieves an image list handle.
6582 * PARAMETER(S):
6583 * [I] infoPtr : valid pointer to the listview structure
6584 * [I] nImageList : image list identifier
6586 * RETURN:
6587 * SUCCESS : image list handle
6588 * FAILURE : NULL
6590 static HIMAGELIST LISTVIEW_GetImageList(const LISTVIEW_INFO *infoPtr, INT nImageList)
6592 switch (nImageList)
6594 case LVSIL_NORMAL: return infoPtr->himlNormal;
6595 case LVSIL_SMALL: return infoPtr->himlSmall;
6596 case LVSIL_STATE: return infoPtr->himlState;
6597 case LVSIL_GROUPHEADER:
6598 FIXME("LVSIL_GROUPHEADER not supported\n");
6599 break;
6600 default:
6601 WARN("got unknown imagelist index - %d\n", nImageList);
6603 return NULL;
6606 /* LISTVIEW_GetISearchString */
6608 /***
6609 * DESCRIPTION:
6610 * Retrieves item attributes.
6612 * PARAMETER(S):
6613 * [I] hwnd : window handle
6614 * [IO] lpLVItem : item info
6615 * [I] isW : if TRUE, then lpLVItem is a LPLVITEMW,
6616 * if FALSE, then lpLVItem is a LPLVITEMA.
6618 * NOTE:
6619 * This is the internal 'GetItem' interface -- it tries to
6620 * be smart and avoid text copies, if possible, by modifying
6621 * lpLVItem->pszText to point to the text string. Please note
6622 * that this is not always possible (e.g. OWNERDATA), so on
6623 * entry you *must* supply valid values for pszText, and cchTextMax.
6624 * The only difference to the documented interface is that upon
6625 * return, you should use *only* the lpLVItem->pszText, rather than
6626 * the buffer pointer you provided on input. Most code already does
6627 * that, so it's not a problem.
6628 * For the two cases when the text must be copied (that is,
6629 * for LVM_GETITEM, and LVM_GETITEMTEXT), use LISTVIEW_GetItemExtT.
6631 * RETURN:
6632 * SUCCESS : TRUE
6633 * FAILURE : FALSE
6635 static BOOL LISTVIEW_GetItemT(const LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem, BOOL isW)
6637 ITEMHDR callbackHdr = { LPSTR_TEXTCALLBACKW, I_IMAGECALLBACK };
6638 NMLVDISPINFOW dispInfo;
6639 ITEM_INFO *lpItem;
6640 ITEMHDR* pItemHdr;
6641 HDPA hdpaSubItems;
6642 INT isubitem;
6644 TRACE("(item=%s, isW=%d)\n", debuglvitem_t(lpLVItem, isW), isW);
6646 if (!lpLVItem || lpLVItem->iItem < 0 || lpLVItem->iItem >= infoPtr->nItemCount)
6647 return FALSE;
6649 if (lpLVItem->mask == 0) return TRUE;
6650 TRACE("mask=%x\n", lpLVItem->mask);
6652 /* make a local copy */
6653 isubitem = lpLVItem->iSubItem;
6655 /* a quick optimization if all we're asked is the focus state
6656 * these queries are worth optimising since they are common,
6657 * and can be answered in constant time, without the heavy accesses */
6658 if ( (lpLVItem->mask == LVIF_STATE) && (lpLVItem->stateMask == LVIS_FOCUSED) &&
6659 !(infoPtr->uCallbackMask & LVIS_FOCUSED) )
6661 lpLVItem->state = 0;
6662 if (infoPtr->nFocusedItem == lpLVItem->iItem)
6663 lpLVItem->state |= LVIS_FOCUSED;
6664 return TRUE;
6667 ZeroMemory(&dispInfo, sizeof(dispInfo));
6669 /* if the app stores all the data, handle it separately */
6670 if (infoPtr->dwStyle & LVS_OWNERDATA)
6672 dispInfo.item.state = 0;
6674 /* apparently, we should not callback for lParam in LVS_OWNERDATA */
6675 if ((lpLVItem->mask & ~(LVIF_STATE | LVIF_PARAM)) ||
6676 ((lpLVItem->mask & LVIF_STATE) && (infoPtr->uCallbackMask & lpLVItem->stateMask)))
6678 UINT mask = lpLVItem->mask;
6680 /* NOTE: copy only fields which we _know_ are initialized, some apps
6681 * depend on the uninitialized fields being 0 */
6682 dispInfo.item.mask = lpLVItem->mask & ~LVIF_PARAM;
6683 dispInfo.item.iItem = lpLVItem->iItem;
6684 dispInfo.item.iSubItem = isubitem;
6685 if (lpLVItem->mask & LVIF_TEXT)
6687 if (lpLVItem->mask & LVIF_NORECOMPUTE)
6688 /* reset mask */
6689 dispInfo.item.mask &= ~(LVIF_TEXT | LVIF_NORECOMPUTE);
6690 else
6692 dispInfo.item.pszText = lpLVItem->pszText;
6693 dispInfo.item.cchTextMax = lpLVItem->cchTextMax;
6696 if (lpLVItem->mask & LVIF_STATE)
6697 dispInfo.item.stateMask = lpLVItem->stateMask & infoPtr->uCallbackMask;
6698 /* could be zeroed on LVIF_NORECOMPUTE case */
6699 if (dispInfo.item.mask)
6701 notify_dispinfoT(infoPtr, LVN_GETDISPINFOW, &dispInfo, isW);
6702 dispInfo.item.stateMask = lpLVItem->stateMask;
6703 if (lpLVItem->mask & (LVIF_GROUPID|LVIF_COLUMNS))
6705 /* full size structure expected - _WIN32IE >= 0x560 */
6706 *lpLVItem = dispInfo.item;
6708 else if (lpLVItem->mask & LVIF_INDENT)
6710 /* indent member expected - _WIN32IE >= 0x300 */
6711 memcpy(lpLVItem, &dispInfo.item, offsetof( LVITEMW, iGroupId ));
6713 else
6715 /* minimal structure expected */
6716 memcpy(lpLVItem, &dispInfo.item, offsetof( LVITEMW, iIndent ));
6718 lpLVItem->mask = mask;
6719 TRACE(" getdispinfo(1):lpLVItem=%s\n", debuglvitem_t(lpLVItem, isW));
6723 /* make sure lParam is zeroed out */
6724 if (lpLVItem->mask & LVIF_PARAM) lpLVItem->lParam = 0;
6726 /* callback marked pointer required here */
6727 if ((lpLVItem->mask & LVIF_TEXT) && (lpLVItem->mask & LVIF_NORECOMPUTE))
6728 lpLVItem->pszText = LPSTR_TEXTCALLBACKW;
6730 /* we store only a little state, so if we're not asked, we're done */
6731 if (!(lpLVItem->mask & LVIF_STATE) || isubitem) return TRUE;
6733 /* if focus is handled by us, report it */
6734 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_FOCUSED )
6736 lpLVItem->state &= ~LVIS_FOCUSED;
6737 if (infoPtr->nFocusedItem == lpLVItem->iItem)
6738 lpLVItem->state |= LVIS_FOCUSED;
6741 /* and do the same for selection, if we handle it */
6742 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_SELECTED )
6744 lpLVItem->state &= ~LVIS_SELECTED;
6745 if (ranges_contain(infoPtr->selectionRanges, lpLVItem->iItem))
6746 lpLVItem->state |= LVIS_SELECTED;
6749 return TRUE;
6752 /* find the item and subitem structures before we proceed */
6753 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, lpLVItem->iItem);
6754 lpItem = DPA_GetPtr(hdpaSubItems, 0);
6755 assert (lpItem);
6757 if (isubitem)
6759 SUBITEM_INFO *lpSubItem = LISTVIEW_GetSubItemPtr(hdpaSubItems, isubitem);
6760 pItemHdr = lpSubItem ? &lpSubItem->hdr : &callbackHdr;
6761 if (!lpSubItem)
6763 WARN(" iSubItem invalid (%08x), ignored.\n", isubitem);
6764 isubitem = 0;
6767 else
6768 pItemHdr = &lpItem->hdr;
6770 /* Do we need to query the state from the app? */
6771 if ((lpLVItem->mask & LVIF_STATE) && infoPtr->uCallbackMask && isubitem == 0)
6773 dispInfo.item.mask |= LVIF_STATE;
6774 dispInfo.item.stateMask = infoPtr->uCallbackMask;
6777 /* Do we need to enquire about the image? */
6778 if ((lpLVItem->mask & LVIF_IMAGE) && pItemHdr->iImage == I_IMAGECALLBACK &&
6779 (isubitem == 0 || (infoPtr->dwLvExStyle & LVS_EX_SUBITEMIMAGES)))
6781 dispInfo.item.mask |= LVIF_IMAGE;
6782 dispInfo.item.iImage = I_IMAGECALLBACK;
6785 /* Only items support indentation */
6786 if ((lpLVItem->mask & LVIF_INDENT) && lpItem->iIndent == I_INDENTCALLBACK &&
6787 (isubitem == 0))
6789 dispInfo.item.mask |= LVIF_INDENT;
6790 dispInfo.item.iIndent = I_INDENTCALLBACK;
6793 /* Apps depend on calling back for text if it is NULL or LPSTR_TEXTCALLBACKW */
6794 if ((lpLVItem->mask & LVIF_TEXT) && !(lpLVItem->mask & LVIF_NORECOMPUTE) &&
6795 !is_text(pItemHdr->pszText))
6797 dispInfo.item.mask |= LVIF_TEXT;
6798 dispInfo.item.pszText = lpLVItem->pszText;
6799 dispInfo.item.cchTextMax = lpLVItem->cchTextMax;
6800 if (dispInfo.item.pszText && dispInfo.item.cchTextMax > 0)
6801 *dispInfo.item.pszText = '\0';
6804 /* If we don't have all the requested info, query the application */
6805 if (dispInfo.item.mask)
6807 dispInfo.item.iItem = lpLVItem->iItem;
6808 dispInfo.item.iSubItem = lpLVItem->iSubItem; /* yes: the original subitem */
6809 dispInfo.item.lParam = lpItem->lParam;
6810 notify_dispinfoT(infoPtr, LVN_GETDISPINFOW, &dispInfo, isW);
6811 TRACE(" getdispinfo(2):item=%s\n", debuglvitem_t(&dispInfo.item, isW));
6814 /* we should not store values for subitems */
6815 if (isubitem) dispInfo.item.mask &= ~LVIF_DI_SETITEM;
6817 /* Now, handle the iImage field */
6818 if (dispInfo.item.mask & LVIF_IMAGE)
6820 lpLVItem->iImage = dispInfo.item.iImage;
6821 if ((dispInfo.item.mask & LVIF_DI_SETITEM) && pItemHdr->iImage == I_IMAGECALLBACK)
6822 pItemHdr->iImage = dispInfo.item.iImage;
6824 else if (lpLVItem->mask & LVIF_IMAGE)
6826 if(isubitem == 0 || (infoPtr->dwLvExStyle & LVS_EX_SUBITEMIMAGES))
6827 lpLVItem->iImage = pItemHdr->iImage;
6828 else
6829 lpLVItem->iImage = 0;
6832 /* The pszText field */
6833 if (dispInfo.item.mask & LVIF_TEXT)
6835 if ((dispInfo.item.mask & LVIF_DI_SETITEM) && pItemHdr->pszText)
6836 textsetptrT(&pItemHdr->pszText, dispInfo.item.pszText, isW);
6838 lpLVItem->pszText = dispInfo.item.pszText;
6840 else if (lpLVItem->mask & LVIF_TEXT)
6842 /* if LVN_GETDISPINFO's disabled with LVIF_NORECOMPUTE return callback placeholder */
6843 if (isW || !is_text(pItemHdr->pszText)) lpLVItem->pszText = pItemHdr->pszText;
6844 else textcpynT(lpLVItem->pszText, isW, pItemHdr->pszText, TRUE, lpLVItem->cchTextMax);
6847 /* Next is the lParam field */
6848 if (dispInfo.item.mask & LVIF_PARAM)
6850 lpLVItem->lParam = dispInfo.item.lParam;
6851 if ((dispInfo.item.mask & LVIF_DI_SETITEM))
6852 lpItem->lParam = dispInfo.item.lParam;
6854 else if (lpLVItem->mask & LVIF_PARAM)
6855 lpLVItem->lParam = lpItem->lParam;
6857 /* if this is a subitem, we're done */
6858 if (isubitem) return TRUE;
6860 /* ... the state field (this one is different due to uCallbackmask) */
6861 if (lpLVItem->mask & LVIF_STATE)
6863 lpLVItem->state = lpItem->state & lpLVItem->stateMask;
6864 if (dispInfo.item.mask & LVIF_STATE)
6866 lpLVItem->state &= ~dispInfo.item.stateMask;
6867 lpLVItem->state |= (dispInfo.item.state & dispInfo.item.stateMask);
6869 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_FOCUSED )
6871 lpLVItem->state &= ~LVIS_FOCUSED;
6872 if (infoPtr->nFocusedItem == lpLVItem->iItem)
6873 lpLVItem->state |= LVIS_FOCUSED;
6875 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_SELECTED )
6877 lpLVItem->state &= ~LVIS_SELECTED;
6878 if (ranges_contain(infoPtr->selectionRanges, lpLVItem->iItem))
6879 lpLVItem->state |= LVIS_SELECTED;
6883 /* and last, but not least, the indent field */
6884 if (dispInfo.item.mask & LVIF_INDENT)
6886 lpLVItem->iIndent = dispInfo.item.iIndent;
6887 if ((dispInfo.item.mask & LVIF_DI_SETITEM) && lpItem->iIndent == I_INDENTCALLBACK)
6888 lpItem->iIndent = dispInfo.item.iIndent;
6890 else if (lpLVItem->mask & LVIF_INDENT)
6892 lpLVItem->iIndent = lpItem->iIndent;
6895 return TRUE;
6898 /***
6899 * DESCRIPTION:
6900 * Retrieves item attributes.
6902 * PARAMETER(S):
6903 * [I] hwnd : window handle
6904 * [IO] lpLVItem : item info
6905 * [I] isW : if TRUE, then lpLVItem is a LPLVITEMW,
6906 * if FALSE, then lpLVItem is a LPLVITEMA.
6908 * NOTE:
6909 * This is the external 'GetItem' interface -- it properly copies
6910 * the text in the provided buffer.
6912 * RETURN:
6913 * SUCCESS : TRUE
6914 * FAILURE : FALSE
6916 static BOOL LISTVIEW_GetItemExtT(const LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem, BOOL isW)
6918 LPWSTR pszText;
6919 BOOL bResult;
6921 if (!lpLVItem || lpLVItem->iItem < 0 || lpLVItem->iItem >= infoPtr->nItemCount)
6922 return FALSE;
6924 pszText = lpLVItem->pszText;
6925 bResult = LISTVIEW_GetItemT(infoPtr, lpLVItem, isW);
6926 if (bResult && (lpLVItem->mask & LVIF_TEXT) && lpLVItem->pszText != pszText)
6928 if (lpLVItem->pszText != LPSTR_TEXTCALLBACKW)
6929 textcpynT(pszText, isW, lpLVItem->pszText, isW, lpLVItem->cchTextMax);
6930 else
6931 pszText = LPSTR_TEXTCALLBACKW;
6933 lpLVItem->pszText = pszText;
6935 return bResult;
6939 /***
6940 * DESCRIPTION:
6941 * Retrieves the position (upper-left) of the listview control item.
6942 * Note that for LVS_ICON style, the upper-left is that of the icon
6943 * and not the bounding box.
6945 * PARAMETER(S):
6946 * [I] infoPtr : valid pointer to the listview structure
6947 * [I] nItem : item index
6948 * [O] lpptPosition : coordinate information
6950 * RETURN:
6951 * SUCCESS : TRUE
6952 * FAILURE : FALSE
6954 static BOOL LISTVIEW_GetItemPosition(const LISTVIEW_INFO *infoPtr, INT nItem, LPPOINT lpptPosition)
6956 POINT Origin;
6958 TRACE("(nItem=%d, lpptPosition=%p)\n", nItem, lpptPosition);
6960 if (!lpptPosition || nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
6962 LISTVIEW_GetOrigin(infoPtr, &Origin);
6963 LISTVIEW_GetItemOrigin(infoPtr, nItem, lpptPosition);
6965 if (infoPtr->uView == LV_VIEW_ICON)
6967 lpptPosition->x += (infoPtr->nItemWidth - infoPtr->iconSize.cx) / 2;
6968 lpptPosition->y += ICON_TOP_PADDING;
6970 lpptPosition->x += Origin.x;
6971 lpptPosition->y += Origin.y;
6973 TRACE (" lpptPosition=%s\n", wine_dbgstr_point(lpptPosition));
6974 return TRUE;
6978 /***
6979 * DESCRIPTION:
6980 * Retrieves the bounding rectangle for a listview control item.
6982 * PARAMETER(S):
6983 * [I] infoPtr : valid pointer to the listview structure
6984 * [I] nItem : item index
6985 * [IO] lprc : bounding rectangle coordinates
6986 * lprc->left specifies the portion of the item for which the bounding
6987 * rectangle will be retrieved.
6989 * LVIR_BOUNDS Returns the bounding rectangle of the entire item,
6990 * including the icon and label.
6992 * * For LVS_ICON
6993 * * Experiment shows that native control returns:
6994 * * width = min (48, length of text line)
6995 * * .left = position.x - (width - iconsize.cx)/2
6996 * * .right = .left + width
6997 * * height = #lines of text * ntmHeight + icon height + 8
6998 * * .top = position.y - 2
6999 * * .bottom = .top + height
7000 * * separation between items .y = itemSpacing.cy - height
7001 * * .x = itemSpacing.cx - width
7002 * LVIR_ICON Returns the bounding rectangle of the icon or small icon.
7004 * * For LVS_ICON
7005 * * Experiment shows that native control returns:
7006 * * width = iconSize.cx + 16
7007 * * .left = position.x - (width - iconsize.cx)/2
7008 * * .right = .left + width
7009 * * height = iconSize.cy + 4
7010 * * .top = position.y - 2
7011 * * .bottom = .top + height
7012 * * separation between items .y = itemSpacing.cy - height
7013 * * .x = itemSpacing.cx - width
7014 * LVIR_LABEL Returns the bounding rectangle of the item text.
7016 * * For LVS_ICON
7017 * * Experiment shows that native control returns:
7018 * * width = text length
7019 * * .left = position.x - width/2
7020 * * .right = .left + width
7021 * * height = ntmH * linecount + 2
7022 * * .top = position.y + iconSize.cy + 6
7023 * * .bottom = .top + height
7024 * * separation between items .y = itemSpacing.cy - height
7025 * * .x = itemSpacing.cx - width
7026 * LVIR_SELECTBOUNDS Returns the union of the LVIR_ICON and LVIR_LABEL
7027 * rectangles, but excludes columns in report view.
7029 * RETURN:
7030 * SUCCESS : TRUE
7031 * FAILURE : FALSE
7033 * NOTES
7034 * Note that the bounding rectangle of the label in the LVS_ICON view depends
7035 * upon whether the window has the focus currently and on whether the item
7036 * is the one with the focus. Ensure that the control's record of which
7037 * item has the focus agrees with the items' records.
7039 static BOOL LISTVIEW_GetItemRect(const LISTVIEW_INFO *infoPtr, INT nItem, LPRECT lprc)
7041 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
7042 BOOL doLabel = TRUE, oversizedBox = FALSE;
7043 POINT Position, Origin;
7044 LVITEMW lvItem;
7045 LONG mode;
7047 TRACE("(hwnd=%p, nItem=%d, lprc=%p)\n", infoPtr->hwndSelf, nItem, lprc);
7049 if (!lprc || nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
7051 LISTVIEW_GetOrigin(infoPtr, &Origin);
7052 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
7054 /* Be smart and try to figure out the minimum we have to do */
7055 if (lprc->left == LVIR_ICON) doLabel = FALSE;
7056 if (infoPtr->uView == LV_VIEW_DETAILS && lprc->left == LVIR_BOUNDS) doLabel = FALSE;
7057 if (infoPtr->uView == LV_VIEW_ICON && lprc->left != LVIR_ICON &&
7058 infoPtr->bFocus && LISTVIEW_GetItemState(infoPtr, nItem, LVIS_FOCUSED))
7059 oversizedBox = TRUE;
7061 /* get what we need from the item before hand, so we make
7062 * only one request. This can speed up things, if data
7063 * is stored on the app side */
7064 lvItem.mask = 0;
7065 if (infoPtr->uView == LV_VIEW_DETAILS) lvItem.mask |= LVIF_INDENT;
7066 if (doLabel) lvItem.mask |= LVIF_TEXT;
7067 lvItem.iItem = nItem;
7068 lvItem.iSubItem = 0;
7069 lvItem.pszText = szDispText;
7070 lvItem.cchTextMax = DISP_TEXT_SIZE;
7071 if (lvItem.mask && !LISTVIEW_GetItemW(infoPtr, &lvItem)) return FALSE;
7072 /* we got the state already up, simulate it here, to avoid a reget */
7073 if (infoPtr->uView == LV_VIEW_ICON && (lprc->left != LVIR_ICON))
7075 lvItem.mask |= LVIF_STATE;
7076 lvItem.stateMask = LVIS_FOCUSED;
7077 lvItem.state = (oversizedBox ? LVIS_FOCUSED : 0);
7080 if (infoPtr->uView == LV_VIEW_DETAILS && (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) && lprc->left == LVIR_SELECTBOUNDS)
7081 lprc->left = LVIR_BOUNDS;
7083 mode = lprc->left;
7084 switch(lprc->left)
7086 case LVIR_ICON:
7087 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, NULL, lprc, NULL, NULL);
7088 break;
7090 case LVIR_LABEL:
7091 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, NULL, NULL, NULL, lprc);
7092 break;
7094 case LVIR_BOUNDS:
7095 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, lprc, NULL, NULL, NULL, NULL);
7096 break;
7098 case LVIR_SELECTBOUNDS:
7099 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, lprc, NULL, NULL, NULL);
7100 break;
7102 default:
7103 WARN("Unknown value: %d\n", lprc->left);
7104 return FALSE;
7107 if (infoPtr->uView == LV_VIEW_DETAILS)
7109 if (mode != LVIR_BOUNDS)
7110 OffsetRect(lprc, Origin.x + LISTVIEW_GetColumnInfo(infoPtr, 0)->rcHeader.left,
7111 Position.y + Origin.y);
7112 else
7113 OffsetRect(lprc, Origin.x, Position.y + Origin.y);
7115 else
7116 OffsetRect(lprc, Position.x + Origin.x, Position.y + Origin.y);
7118 TRACE(" rect=%s\n", wine_dbgstr_rect(lprc));
7120 return TRUE;
7123 /***
7124 * DESCRIPTION:
7125 * Retrieves the spacing between listview control items.
7127 * PARAMETER(S):
7128 * [I] infoPtr : valid pointer to the listview structure
7129 * [IO] lprc : rectangle to receive the output
7130 * on input, lprc->top = nSubItem
7131 * lprc->left = LVIR_ICON | LVIR_BOUNDS | LVIR_LABEL
7133 * NOTE: for subItem = 0, we should return the bounds of the _entire_ item,
7134 * not only those of the first column.
7136 * RETURN:
7137 * TRUE: success
7138 * FALSE: failure
7140 static BOOL LISTVIEW_GetSubItemRect(const LISTVIEW_INFO *infoPtr, INT item, LPRECT lprc)
7142 RECT rect = { 0, 0, 0, 0 };
7143 POINT origin;
7144 INT y;
7146 if (!lprc) return FALSE;
7148 TRACE("(item=%d, subitem=%d, type=%d)\n", item, lprc->top, lprc->left);
7149 /* Subitem of '0' means item itself, and this works for all control view modes */
7150 if (lprc->top == 0)
7151 return LISTVIEW_GetItemRect(infoPtr, item, lprc);
7153 if (infoPtr->uView != LV_VIEW_DETAILS) return FALSE;
7155 LISTVIEW_GetOrigin(infoPtr, &origin);
7156 /* this works for any item index, no matter if it exists or not */
7157 y = item * infoPtr->nItemHeight + origin.y;
7159 if (infoPtr->hwndHeader && SendMessageW(infoPtr->hwndHeader, HDM_GETITEMRECT, lprc->top, (LPARAM)&rect))
7161 rect.top = 0;
7162 rect.bottom = infoPtr->nItemHeight;
7164 else
7166 /* Native implementation is broken for this case and garbage is left for left and right fields,
7167 we zero them to get predictable output */
7168 lprc->left = lprc->right = lprc->top = 0;
7169 lprc->bottom = infoPtr->nItemHeight;
7170 OffsetRect(lprc, origin.x, y);
7171 TRACE("return rect %s\n", wine_dbgstr_rect(lprc));
7172 return TRUE;
7175 switch (lprc->left)
7177 case LVIR_ICON:
7179 /* it doesn't matter if main item actually has an icon, if imagelist is set icon width is returned */
7180 if (infoPtr->himlSmall)
7181 rect.right = rect.left + infoPtr->iconSize.cx;
7182 else
7183 rect.right = rect.left;
7185 rect.bottom = rect.top + infoPtr->iconSize.cy;
7186 break;
7188 case LVIR_LABEL:
7189 case LVIR_BOUNDS:
7190 break;
7192 default:
7193 ERR("Unknown bounds=%d\n", lprc->left);
7194 return FALSE;
7197 OffsetRect(&rect, origin.x, y);
7198 *lprc = rect;
7199 TRACE("return rect %s\n", wine_dbgstr_rect(lprc));
7201 return TRUE;
7204 /***
7205 * DESCRIPTION:
7206 * Retrieves the spacing between listview control items.
7208 * PARAMETER(S):
7209 * [I] infoPtr : valid pointer to the listview structure
7210 * [I] bSmall : flag for small or large icon
7212 * RETURN:
7213 * Horizontal + vertical spacing
7215 static LONG LISTVIEW_GetItemSpacing(const LISTVIEW_INFO *infoPtr, BOOL bSmall)
7217 LONG lResult;
7219 if (!bSmall)
7221 lResult = MAKELONG(infoPtr->iconSpacing.cx, infoPtr->iconSpacing.cy);
7223 else
7225 if (infoPtr->uView == LV_VIEW_ICON)
7226 lResult = MAKELONG(DEFAULT_COLUMN_WIDTH, GetSystemMetrics(SM_CXSMICON)+HEIGHT_PADDING);
7227 else
7228 lResult = MAKELONG(infoPtr->nItemWidth, infoPtr->nItemHeight);
7230 return lResult;
7233 /***
7234 * DESCRIPTION:
7235 * Retrieves the state of a listview control item.
7237 * PARAMETER(S):
7238 * [I] infoPtr : valid pointer to the listview structure
7239 * [I] nItem : item index
7240 * [I] uMask : state mask
7242 * RETURN:
7243 * State specified by the mask.
7245 static UINT LISTVIEW_GetItemState(const LISTVIEW_INFO *infoPtr, INT nItem, UINT uMask)
7247 LVITEMW lvItem;
7249 if (nItem < 0 || nItem >= infoPtr->nItemCount) return 0;
7251 lvItem.iItem = nItem;
7252 lvItem.iSubItem = 0;
7253 lvItem.mask = LVIF_STATE;
7254 lvItem.stateMask = uMask;
7255 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return 0;
7257 return lvItem.state & uMask;
7260 /***
7261 * DESCRIPTION:
7262 * Retrieves the text of a listview control item or subitem.
7264 * PARAMETER(S):
7265 * [I] hwnd : window handle
7266 * [I] nItem : item index
7267 * [IO] lpLVItem : item information
7268 * [I] isW : TRUE if lpLVItem is Unicode
7270 * RETURN:
7271 * SUCCESS : string length
7272 * FAILURE : 0
7274 static INT LISTVIEW_GetItemTextT(const LISTVIEW_INFO *infoPtr, INT nItem, LPLVITEMW lpLVItem, BOOL isW)
7276 if (!lpLVItem || nItem < 0 || nItem >= infoPtr->nItemCount) return 0;
7278 lpLVItem->mask = LVIF_TEXT;
7279 lpLVItem->iItem = nItem;
7280 if (!LISTVIEW_GetItemExtT(infoPtr, lpLVItem, isW)) return 0;
7282 return textlenT(lpLVItem->pszText, isW);
7285 /***
7286 * DESCRIPTION:
7287 * Searches for an item based on properties + relationships.
7289 * PARAMETER(S):
7290 * [I] infoPtr : valid pointer to the listview structure
7291 * [I] nItem : item index
7292 * [I] uFlags : relationship flag
7294 * RETURN:
7295 * SUCCESS : item index
7296 * FAILURE : -1
7298 static INT LISTVIEW_GetNextItem(const LISTVIEW_INFO *infoPtr, INT nItem, UINT uFlags)
7300 UINT uMask = 0;
7301 LVFINDINFOW lvFindInfo;
7302 INT nCountPerColumn;
7303 INT nCountPerRow;
7304 INT i;
7306 TRACE("nItem=%d, uFlags=%x, nItemCount=%d\n", nItem, uFlags, infoPtr->nItemCount);
7307 if (nItem < -1 || nItem >= infoPtr->nItemCount) return -1;
7309 ZeroMemory(&lvFindInfo, sizeof(lvFindInfo));
7311 if (uFlags & LVNI_CUT)
7312 uMask |= LVIS_CUT;
7314 if (uFlags & LVNI_DROPHILITED)
7315 uMask |= LVIS_DROPHILITED;
7317 if (uFlags & LVNI_FOCUSED)
7318 uMask |= LVIS_FOCUSED;
7320 if (uFlags & LVNI_SELECTED)
7321 uMask |= LVIS_SELECTED;
7323 /* if we're asked for the focused item, that's only one,
7324 * so it's worth optimizing */
7325 if (uFlags & LVNI_FOCUSED)
7327 if ((LISTVIEW_GetItemState(infoPtr, infoPtr->nFocusedItem, uMask) & uMask) != uMask) return -1;
7328 return (infoPtr->nFocusedItem == nItem) ? -1 : infoPtr->nFocusedItem;
7331 if (uFlags & LVNI_ABOVE)
7333 if ((infoPtr->uView == LV_VIEW_LIST) || (infoPtr->uView == LV_VIEW_DETAILS))
7335 while (nItem >= 0)
7337 nItem--;
7338 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7339 return nItem;
7342 else
7344 /* Special case for autoarrange - move 'til the top of a list */
7345 if (is_autoarrange(infoPtr))
7347 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
7348 while (nItem - nCountPerRow >= 0)
7350 nItem -= nCountPerRow;
7351 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7352 return nItem;
7354 return -1;
7356 lvFindInfo.flags = LVFI_NEARESTXY;
7357 lvFindInfo.vkDirection = VK_UP;
7358 LISTVIEW_GetItemPosition(infoPtr, nItem, &lvFindInfo.pt);
7359 while ((nItem = LISTVIEW_FindItemW(infoPtr, nItem, &lvFindInfo)) != -1)
7361 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7362 return nItem;
7366 else if (uFlags & LVNI_BELOW)
7368 if ((infoPtr->uView == LV_VIEW_LIST) || (infoPtr->uView == LV_VIEW_DETAILS))
7370 while (nItem < infoPtr->nItemCount)
7372 nItem++;
7373 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7374 return nItem;
7377 else
7379 /* Special case for autoarrange - move 'til the bottom of a list */
7380 if (is_autoarrange(infoPtr))
7382 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
7383 while (nItem + nCountPerRow < infoPtr->nItemCount )
7385 nItem += nCountPerRow;
7386 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7387 return nItem;
7389 return -1;
7391 lvFindInfo.flags = LVFI_NEARESTXY;
7392 lvFindInfo.vkDirection = VK_DOWN;
7393 LISTVIEW_GetItemPosition(infoPtr, nItem, &lvFindInfo.pt);
7394 while ((nItem = LISTVIEW_FindItemW(infoPtr, nItem, &lvFindInfo)) != -1)
7396 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7397 return nItem;
7401 else if (uFlags & LVNI_TOLEFT)
7403 if (infoPtr->uView == LV_VIEW_LIST)
7405 nCountPerColumn = LISTVIEW_GetCountPerColumn(infoPtr);
7406 while (nItem - nCountPerColumn >= 0)
7408 nItem -= nCountPerColumn;
7409 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7410 return nItem;
7413 else if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
7415 /* Special case for autoarrange - move 'til the beginning of a row */
7416 if (is_autoarrange(infoPtr))
7418 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
7419 while (nItem % nCountPerRow > 0)
7421 nItem --;
7422 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7423 return nItem;
7425 return -1;
7427 lvFindInfo.flags = LVFI_NEARESTXY;
7428 lvFindInfo.vkDirection = VK_LEFT;
7429 LISTVIEW_GetItemPosition(infoPtr, nItem, &lvFindInfo.pt);
7430 while ((nItem = LISTVIEW_FindItemW(infoPtr, nItem, &lvFindInfo)) != -1)
7432 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7433 return nItem;
7437 else if (uFlags & LVNI_TORIGHT)
7439 if (infoPtr->uView == LV_VIEW_LIST)
7441 nCountPerColumn = LISTVIEW_GetCountPerColumn(infoPtr);
7442 while (nItem + nCountPerColumn < infoPtr->nItemCount)
7444 nItem += nCountPerColumn;
7445 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7446 return nItem;
7449 else if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
7451 /* Special case for autoarrange - move 'til the end of a row */
7452 if (is_autoarrange(infoPtr))
7454 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
7455 while (nItem % nCountPerRow < nCountPerRow - 1 )
7457 nItem ++;
7458 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7459 return nItem;
7461 return -1;
7463 lvFindInfo.flags = LVFI_NEARESTXY;
7464 lvFindInfo.vkDirection = VK_RIGHT;
7465 LISTVIEW_GetItemPosition(infoPtr, nItem, &lvFindInfo.pt);
7466 while ((nItem = LISTVIEW_FindItemW(infoPtr, nItem, &lvFindInfo)) != -1)
7468 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7469 return nItem;
7473 else
7475 nItem++;
7477 /* search by index */
7478 for (i = nItem; i < infoPtr->nItemCount; i++)
7480 if ((LISTVIEW_GetItemState(infoPtr, i, uMask) & uMask) == uMask)
7481 return i;
7485 return -1;
7488 /* LISTVIEW_GetNumberOfWorkAreas */
7490 /***
7491 * DESCRIPTION:
7492 * Retrieves the origin coordinates when in icon or small icon display mode.
7494 * PARAMETER(S):
7495 * [I] infoPtr : valid pointer to the listview structure
7496 * [O] lpptOrigin : coordinate information
7498 * RETURN:
7499 * None.
7501 static void LISTVIEW_GetOrigin(const LISTVIEW_INFO *infoPtr, LPPOINT lpptOrigin)
7503 INT nHorzPos = 0, nVertPos = 0;
7504 SCROLLINFO scrollInfo;
7506 scrollInfo.cbSize = sizeof(SCROLLINFO);
7507 scrollInfo.fMask = SIF_POS;
7509 if (GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo))
7510 nHorzPos = scrollInfo.nPos;
7511 if (GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
7512 nVertPos = scrollInfo.nPos;
7514 TRACE("nHorzPos=%d, nVertPos=%d\n", nHorzPos, nVertPos);
7516 lpptOrigin->x = infoPtr->rcList.left;
7517 lpptOrigin->y = infoPtr->rcList.top;
7518 if (infoPtr->uView == LV_VIEW_LIST)
7519 nHorzPos *= infoPtr->nItemWidth;
7520 else if (infoPtr->uView == LV_VIEW_DETAILS)
7521 nVertPos *= infoPtr->nItemHeight;
7523 lpptOrigin->x -= nHorzPos;
7524 lpptOrigin->y -= nVertPos;
7526 TRACE(" origin=%s\n", wine_dbgstr_point(lpptOrigin));
7529 /***
7530 * DESCRIPTION:
7531 * Retrieves the width of a string.
7533 * PARAMETER(S):
7534 * [I] hwnd : window handle
7535 * [I] lpszText : text string to process
7536 * [I] isW : TRUE if lpszText is Unicode, FALSE otherwise
7538 * RETURN:
7539 * SUCCESS : string width (in pixels)
7540 * FAILURE : zero
7542 static INT LISTVIEW_GetStringWidthT(const LISTVIEW_INFO *infoPtr, LPCWSTR lpszText, BOOL isW)
7544 SIZE stringSize;
7546 stringSize.cx = 0;
7547 if (is_text(lpszText))
7549 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
7550 HDC hdc = GetDC(infoPtr->hwndSelf);
7551 HFONT hOldFont = SelectObject(hdc, hFont);
7553 if (isW)
7554 GetTextExtentPointW(hdc, lpszText, lstrlenW(lpszText), &stringSize);
7555 else
7556 GetTextExtentPointA(hdc, (LPCSTR)lpszText, lstrlenA((LPCSTR)lpszText), &stringSize);
7557 SelectObject(hdc, hOldFont);
7558 ReleaseDC(infoPtr->hwndSelf, hdc);
7560 return stringSize.cx;
7563 /***
7564 * DESCRIPTION:
7565 * Determines which listview item is located at the specified position.
7567 * PARAMETER(S):
7568 * [I] infoPtr : valid pointer to the listview structure
7569 * [IO] lpht : hit test information
7570 * [I] subitem : fill out iSubItem.
7571 * [I] select : return the index only if the hit selects the item
7573 * NOTE:
7574 * (mm 20001022): We must not allow iSubItem to be touched, for
7575 * an app might pass only a structure with space up to iItem!
7576 * (MS Office 97 does that for instance in the file open dialog)
7578 * RETURN:
7579 * SUCCESS : item index
7580 * FAILURE : -1
7582 static INT LISTVIEW_HitTest(const LISTVIEW_INFO *infoPtr, LPLVHITTESTINFO lpht, BOOL subitem, BOOL select)
7584 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
7585 RECT rcBox, rcBounds, rcState, rcIcon, rcLabel, rcSearch;
7586 POINT Origin, Position, opt;
7587 BOOL is_fullrow;
7588 LVITEMW lvItem;
7589 ITERATOR i;
7590 INT iItem;
7592 TRACE("(pt=%s, subitem=%d, select=%d)\n", wine_dbgstr_point(&lpht->pt), subitem, select);
7594 lpht->flags = 0;
7595 lpht->iItem = -1;
7596 if (subitem) lpht->iSubItem = 0;
7598 LISTVIEW_GetOrigin(infoPtr, &Origin);
7600 /* set whole list relation flags */
7601 if (subitem && infoPtr->uView == LV_VIEW_DETAILS)
7603 /* LVM_SUBITEMHITTEST checks left bound of possible client area */
7604 if (infoPtr->rcList.left > lpht->pt.x && Origin.x < lpht->pt.x)
7605 lpht->flags |= LVHT_TOLEFT;
7607 if (lpht->pt.y < infoPtr->rcList.top && lpht->pt.y >= 0)
7608 opt.y = lpht->pt.y + infoPtr->rcList.top;
7609 else
7610 opt.y = lpht->pt.y;
7612 if (infoPtr->rcList.bottom < opt.y)
7613 lpht->flags |= LVHT_BELOW;
7615 else
7617 if (infoPtr->rcList.left > lpht->pt.x)
7618 lpht->flags |= LVHT_TOLEFT;
7619 else if (infoPtr->rcList.right < lpht->pt.x)
7620 lpht->flags |= LVHT_TORIGHT;
7622 if (infoPtr->rcList.top > lpht->pt.y)
7623 lpht->flags |= LVHT_ABOVE;
7624 else if (infoPtr->rcList.bottom < lpht->pt.y)
7625 lpht->flags |= LVHT_BELOW;
7628 /* even if item is invalid try to find subitem */
7629 if (infoPtr->uView == LV_VIEW_DETAILS && subitem)
7631 RECT *pRect;
7632 INT j;
7634 opt.x = lpht->pt.x - Origin.x;
7636 lpht->iSubItem = -1;
7637 for (j = 0; j < DPA_GetPtrCount(infoPtr->hdpaColumns); j++)
7639 pRect = &LISTVIEW_GetColumnInfo(infoPtr, j)->rcHeader;
7641 if ((opt.x >= pRect->left) && (opt.x < pRect->right))
7643 lpht->iSubItem = j;
7644 break;
7647 TRACE("lpht->iSubItem=%d\n", lpht->iSubItem);
7649 /* if we're outside horizontal columns bounds there's nothing to test further */
7650 if (lpht->iSubItem == -1)
7652 lpht->iItem = -1;
7653 lpht->flags = LVHT_NOWHERE;
7654 return -1;
7658 TRACE("lpht->flags=0x%x\n", lpht->flags);
7659 if (lpht->flags) return -1;
7661 lpht->flags |= LVHT_NOWHERE;
7663 /* first deal with the large items */
7664 rcSearch.left = lpht->pt.x;
7665 rcSearch.top = lpht->pt.y;
7666 rcSearch.right = rcSearch.left + 1;
7667 rcSearch.bottom = rcSearch.top + 1;
7669 iterator_frameditems(&i, infoPtr, &rcSearch);
7670 iterator_next(&i); /* go to first item in the sequence */
7671 iItem = i.nItem;
7672 iterator_destroy(&i);
7674 TRACE("lpht->iItem=%d\n", iItem);
7675 if (iItem == -1) return -1;
7677 lvItem.mask = LVIF_STATE | LVIF_TEXT;
7678 if (infoPtr->uView == LV_VIEW_DETAILS) lvItem.mask |= LVIF_INDENT;
7679 lvItem.stateMask = LVIS_STATEIMAGEMASK;
7680 if (infoPtr->uView == LV_VIEW_ICON) lvItem.stateMask |= LVIS_FOCUSED;
7681 lvItem.iItem = iItem;
7682 lvItem.iSubItem = subitem ? lpht->iSubItem : 0;
7683 lvItem.pszText = szDispText;
7684 lvItem.cchTextMax = DISP_TEXT_SIZE;
7685 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return -1;
7686 if (!infoPtr->bFocus) lvItem.state &= ~LVIS_FOCUSED;
7688 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, &rcBox, NULL, &rcIcon, &rcState, &rcLabel);
7689 LISTVIEW_GetItemOrigin(infoPtr, iItem, &Position);
7690 opt.x = lpht->pt.x - Position.x - Origin.x;
7692 if (lpht->pt.y < infoPtr->rcList.top && lpht->pt.y >= 0)
7693 opt.y = lpht->pt.y - Position.y - Origin.y + infoPtr->rcList.top;
7694 else
7695 opt.y = lpht->pt.y - Position.y - Origin.y;
7697 if (infoPtr->uView == LV_VIEW_DETAILS)
7699 rcBounds = rcBox;
7700 if (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT)
7701 opt.x = lpht->pt.x - Origin.x;
7703 else
7705 UnionRect(&rcBounds, &rcIcon, &rcLabel);
7706 UnionRect(&rcBounds, &rcBounds, &rcState);
7708 TRACE("rcBounds=%s\n", wine_dbgstr_rect(&rcBounds));
7709 if (!PtInRect(&rcBounds, opt)) return -1;
7711 /* That's a special case - row rectangle is used as item rectangle and
7712 returned flags contain all item parts. */
7713 is_fullrow = (infoPtr->uView == LV_VIEW_DETAILS) && ((infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) || (infoPtr->dwStyle & LVS_OWNERDRAWFIXED));
7715 if (PtInRect(&rcIcon, opt))
7716 lpht->flags |= LVHT_ONITEMICON;
7717 else if (PtInRect(&rcLabel, opt))
7718 lpht->flags |= LVHT_ONITEMLABEL;
7719 else if (infoPtr->himlState && PtInRect(&rcState, opt))
7720 lpht->flags |= LVHT_ONITEMSTATEICON;
7721 if (is_fullrow && !(lpht->flags & LVHT_ONITEM))
7723 lpht->flags = LVHT_ONITEM | LVHT_ABOVE;
7725 if (lpht->flags & LVHT_ONITEM)
7726 lpht->flags &= ~LVHT_NOWHERE;
7727 TRACE("lpht->flags=0x%x\n", lpht->flags);
7729 if (select && !is_fullrow)
7731 if (infoPtr->uView == LV_VIEW_DETAILS)
7733 /* get main item bounds */
7734 lvItem.iSubItem = 0;
7735 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, &rcBox, NULL, &rcIcon, &rcState, &rcLabel);
7736 UnionRect(&rcBounds, &rcIcon, &rcLabel);
7737 UnionRect(&rcBounds, &rcBounds, &rcState);
7739 if (!PtInRect(&rcBounds, opt)) iItem = -1;
7741 return lpht->iItem = iItem;
7744 /***
7745 * DESCRIPTION:
7746 * Inserts a new item in the listview control.
7748 * PARAMETER(S):
7749 * [I] infoPtr : valid pointer to the listview structure
7750 * [I] lpLVItem : item information
7751 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
7753 * RETURN:
7754 * SUCCESS : new item index
7755 * FAILURE : -1
7757 static INT LISTVIEW_InsertItemT(LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem, BOOL isW)
7759 INT nItem;
7760 HDPA hdpaSubItems;
7761 NMLISTVIEW nmlv;
7762 ITEM_INFO *lpItem;
7763 ITEM_ID *lpID;
7764 BOOL is_sorted, has_changed;
7765 LVITEMW item;
7766 HWND hwndSelf = infoPtr->hwndSelf;
7768 TRACE("(item=%s, isW=%d)\n", debuglvitem_t(lpLVItem, isW), isW);
7770 if (infoPtr->dwStyle & LVS_OWNERDATA) return infoPtr->nItemCount++;
7772 /* make sure it's an item, and not a subitem; cannot insert a subitem */
7773 if (!lpLVItem || lpLVItem->iSubItem) return -1;
7775 if (!is_assignable_item(lpLVItem, infoPtr->dwStyle)) return -1;
7777 if (!(lpItem = Alloc(sizeof(ITEM_INFO)))) return -1;
7779 /* insert item in listview control data structure */
7780 if ( !(hdpaSubItems = DPA_Create(8)) ) goto fail;
7781 if ( !DPA_SetPtr(hdpaSubItems, 0, lpItem) ) assert (FALSE);
7783 /* link with id struct */
7784 if (!(lpID = Alloc(sizeof(ITEM_ID)))) goto fail;
7785 lpItem->id = lpID;
7786 lpID->item = hdpaSubItems;
7787 lpID->id = get_next_itemid(infoPtr);
7788 if ( DPA_InsertPtr(infoPtr->hdpaItemIds, infoPtr->nItemCount, lpID) == -1) goto fail;
7790 is_sorted = (infoPtr->dwStyle & (LVS_SORTASCENDING | LVS_SORTDESCENDING)) &&
7791 !(infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (LPSTR_TEXTCALLBACKW != lpLVItem->pszText);
7793 if (lpLVItem->iItem < 0 && !is_sorted) return -1;
7795 /* calculate new item index */
7796 if (is_sorted)
7798 HDPA hItem;
7799 ITEM_INFO *item_s;
7800 INT i = 0, cmpv;
7802 while (i < infoPtr->nItemCount)
7804 hItem = DPA_GetPtr( infoPtr->hdpaItems, i);
7805 item_s = DPA_GetPtr(hItem, 0);
7807 cmpv = textcmpWT(item_s->hdr.pszText, lpLVItem->pszText, isW);
7808 if (infoPtr->dwStyle & LVS_SORTDESCENDING) cmpv *= -1;
7810 if (cmpv >= 0) break;
7811 i++;
7813 nItem = i;
7815 else
7816 nItem = min(lpLVItem->iItem, infoPtr->nItemCount);
7818 TRACE("inserting at %d, sorted=%d, count=%d, iItem=%d\n", nItem, is_sorted, infoPtr->nItemCount, lpLVItem->iItem);
7819 nItem = DPA_InsertPtr( infoPtr->hdpaItems, nItem, hdpaSubItems );
7820 if (nItem == -1) goto fail;
7821 infoPtr->nItemCount++;
7823 /* shift indices first so they don't get tangled */
7824 LISTVIEW_ShiftIndices(infoPtr, nItem, 1);
7826 /* set the item attributes */
7827 if (lpLVItem->mask & (LVIF_GROUPID|LVIF_COLUMNS))
7829 /* full size structure expected - _WIN32IE >= 0x560 */
7830 item = *lpLVItem;
7832 else if (lpLVItem->mask & LVIF_INDENT)
7834 /* indent member expected - _WIN32IE >= 0x300 */
7835 memcpy(&item, lpLVItem, offsetof( LVITEMW, iGroupId ));
7837 else
7839 /* minimal structure expected */
7840 memcpy(&item, lpLVItem, offsetof( LVITEMW, iIndent ));
7842 item.iItem = nItem;
7843 if (infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES)
7845 item.mask |= LVIF_STATE;
7846 item.stateMask |= LVIS_STATEIMAGEMASK;
7847 item.state &= ~LVIS_STATEIMAGEMASK;
7848 item.state |= INDEXTOSTATEIMAGEMASK(1);
7851 if (!set_main_item(infoPtr, &item, TRUE, isW, &has_changed)) goto undo;
7853 /* make room for the position, if we are in the right mode */
7854 if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
7856 if (DPA_InsertPtr(infoPtr->hdpaPosX, nItem, 0) == -1)
7857 goto undo;
7858 if (DPA_InsertPtr(infoPtr->hdpaPosY, nItem, 0) == -1)
7860 DPA_DeletePtr(infoPtr->hdpaPosX, nItem);
7861 goto undo;
7865 /* send LVN_INSERTITEM notification */
7866 memset(&nmlv, 0, sizeof(NMLISTVIEW));
7867 nmlv.iItem = nItem;
7868 nmlv.lParam = lpItem->lParam;
7869 notify_listview(infoPtr, LVN_INSERTITEM, &nmlv);
7870 if (!IsWindow(hwndSelf))
7871 return -1;
7873 /* align items (set position of each item) */
7874 if (infoPtr->uView == LV_VIEW_SMALLICON || infoPtr->uView == LV_VIEW_ICON)
7876 POINT pt;
7878 if (infoPtr->dwStyle & LVS_ALIGNLEFT)
7879 LISTVIEW_NextIconPosLeft(infoPtr, &pt);
7880 else
7881 LISTVIEW_NextIconPosTop(infoPtr, &pt);
7883 LISTVIEW_MoveIconTo(infoPtr, nItem, &pt, TRUE);
7886 /* now is the invalidation fun */
7887 LISTVIEW_ScrollOnInsert(infoPtr, nItem, 1);
7888 return nItem;
7890 undo:
7891 LISTVIEW_ShiftIndices(infoPtr, nItem, -1);
7892 LISTVIEW_ShiftFocus(infoPtr, infoPtr->nFocusedItem, nItem, -1);
7893 DPA_DeletePtr(infoPtr->hdpaItems, nItem);
7894 infoPtr->nItemCount--;
7895 fail:
7896 DPA_DeletePtr(hdpaSubItems, 0);
7897 DPA_Destroy (hdpaSubItems);
7898 Free (lpItem);
7899 return -1;
7902 /***
7903 * DESCRIPTION:
7904 * Checks item visibility.
7906 * PARAMETER(S):
7907 * [I] infoPtr : valid pointer to the listview structure
7908 * [I] nFirst : item index to check for
7910 * RETURN:
7911 * Item visible : TRUE
7912 * Item invisible or failure : FALSE
7914 static BOOL LISTVIEW_IsItemVisible(const LISTVIEW_INFO *infoPtr, INT nItem)
7916 POINT Origin, Position;
7917 RECT rcItem;
7918 HDC hdc;
7919 BOOL ret;
7921 TRACE("nItem=%d\n", nItem);
7923 if (nItem < 0 || nItem >= DPA_GetPtrCount(infoPtr->hdpaItems)) return FALSE;
7925 LISTVIEW_GetOrigin(infoPtr, &Origin);
7926 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
7927 rcItem.left = Position.x + Origin.x;
7928 rcItem.top = Position.y + Origin.y;
7929 rcItem.right = rcItem.left + infoPtr->nItemWidth;
7930 rcItem.bottom = rcItem.top + infoPtr->nItemHeight;
7932 hdc = GetDC(infoPtr->hwndSelf);
7933 if (!hdc) return FALSE;
7934 ret = RectVisible(hdc, &rcItem);
7935 ReleaseDC(infoPtr->hwndSelf, hdc);
7937 return ret;
7940 /***
7941 * DESCRIPTION:
7942 * Redraws a range of items.
7944 * PARAMETER(S):
7945 * [I] infoPtr : valid pointer to the listview structure
7946 * [I] nFirst : first item
7947 * [I] nLast : last item
7949 * RETURN:
7950 * SUCCESS : TRUE
7951 * FAILURE : FALSE
7953 static BOOL LISTVIEW_RedrawItems(const LISTVIEW_INFO *infoPtr, INT nFirst, INT nLast)
7955 INT i;
7957 if (nLast < nFirst || min(nFirst, nLast) < 0 ||
7958 max(nFirst, nLast) >= infoPtr->nItemCount)
7959 return FALSE;
7961 for (i = nFirst; i <= nLast; i++)
7962 LISTVIEW_InvalidateItem(infoPtr, i);
7964 return TRUE;
7967 /***
7968 * DESCRIPTION:
7969 * Scroll the content of a listview.
7971 * PARAMETER(S):
7972 * [I] infoPtr : valid pointer to the listview structure
7973 * [I] dx : horizontal scroll amount in pixels
7974 * [I] dy : vertical scroll amount in pixels
7976 * RETURN:
7977 * SUCCESS : TRUE
7978 * FAILURE : FALSE
7980 * COMMENTS:
7981 * If the control is in report view (LV_VIEW_DETAILS) the control can
7982 * be scrolled only in line increments. "dy" will be rounded to the
7983 * nearest number of pixels that are a whole line. Ex: if line height
7984 * is 16 and an 8 is passed, the list will be scrolled by 16. If a 7
7985 * is passed, then the scroll will be 0. (per MSDN 7/2002)
7987 static BOOL LISTVIEW_Scroll(LISTVIEW_INFO *infoPtr, INT dx, INT dy)
7989 switch(infoPtr->uView) {
7990 case LV_VIEW_DETAILS:
7991 dy += (dy < 0 ? -1 : 1) * infoPtr->nItemHeight/2;
7992 dy /= infoPtr->nItemHeight;
7993 break;
7994 case LV_VIEW_LIST:
7995 if (dy != 0) return FALSE;
7996 break;
7997 default: /* icon */
7998 break;
8001 if (dx != 0) LISTVIEW_HScroll(infoPtr, SB_INTERNAL, dx);
8002 if (dy != 0) LISTVIEW_VScroll(infoPtr, SB_INTERNAL, dy);
8004 return TRUE;
8007 /***
8008 * DESCRIPTION:
8009 * Sets the background color.
8011 * PARAMETER(S):
8012 * [I] infoPtr : valid pointer to the listview structure
8013 * [I] color : background color
8015 * RETURN:
8016 * SUCCESS : TRUE
8017 * FAILURE : FALSE
8019 static BOOL LISTVIEW_SetBkColor(LISTVIEW_INFO *infoPtr, COLORREF color)
8021 TRACE("(color=%x)\n", color);
8023 if(infoPtr->clrBk != color) {
8024 if (infoPtr->clrBk != CLR_NONE) DeleteObject(infoPtr->hBkBrush);
8025 infoPtr->clrBk = color;
8026 if (color == CLR_NONE)
8027 infoPtr->hBkBrush = (HBRUSH)GetClassLongPtrW(infoPtr->hwndSelf, GCLP_HBRBACKGROUND);
8028 else
8030 infoPtr->hBkBrush = CreateSolidBrush(color);
8031 infoPtr->dwLvExStyle &= ~LVS_EX_TRANSPARENTBKGND;
8035 return TRUE;
8038 /* LISTVIEW_SetBkImage */
8040 /*** Helper for {Insert,Set}ColumnT *only* */
8041 static void column_fill_hditem(const LISTVIEW_INFO *infoPtr, HDITEMW *lphdi, INT nColumn,
8042 const LVCOLUMNW *lpColumn, BOOL isW)
8044 if (lpColumn->mask & LVCF_FMT)
8046 /* format member is valid */
8047 lphdi->mask |= HDI_FORMAT;
8049 /* set text alignment (leftmost column must be left-aligned) */
8050 if (nColumn == 0 || (lpColumn->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_LEFT)
8051 lphdi->fmt |= HDF_LEFT;
8052 else if ((lpColumn->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_RIGHT)
8053 lphdi->fmt |= HDF_RIGHT;
8054 else if ((lpColumn->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_CENTER)
8055 lphdi->fmt |= HDF_CENTER;
8057 if (lpColumn->fmt & LVCFMT_BITMAP_ON_RIGHT)
8058 lphdi->fmt |= HDF_BITMAP_ON_RIGHT;
8060 if (lpColumn->fmt & LVCFMT_COL_HAS_IMAGES)
8062 lphdi->fmt |= HDF_IMAGE;
8063 lphdi->iImage = I_IMAGECALLBACK;
8066 if (lpColumn->fmt & LVCFMT_FIXED_WIDTH)
8067 lphdi->fmt |= HDF_FIXEDWIDTH;
8070 if (lpColumn->mask & LVCF_WIDTH)
8072 lphdi->mask |= HDI_WIDTH;
8073 if(lpColumn->cx == LVSCW_AUTOSIZE_USEHEADER)
8075 /* make it fill the remainder of the controls width */
8076 RECT rcHeader;
8077 INT item_index;
8079 for(item_index = 0; item_index < (nColumn - 1); item_index++)
8081 LISTVIEW_GetHeaderRect(infoPtr, item_index, &rcHeader);
8082 lphdi->cxy += rcHeader.right - rcHeader.left;
8085 /* retrieve the layout of the header */
8086 GetClientRect(infoPtr->hwndSelf, &rcHeader);
8087 TRACE("start cxy=%d rcHeader=%s\n", lphdi->cxy, wine_dbgstr_rect(&rcHeader));
8089 lphdi->cxy = (rcHeader.right - rcHeader.left) - lphdi->cxy;
8091 else
8092 lphdi->cxy = lpColumn->cx;
8095 if (lpColumn->mask & LVCF_TEXT)
8097 lphdi->mask |= HDI_TEXT | HDI_FORMAT;
8098 lphdi->fmt |= HDF_STRING;
8099 lphdi->pszText = lpColumn->pszText;
8100 lphdi->cchTextMax = textlenT(lpColumn->pszText, isW);
8103 if (lpColumn->mask & LVCF_IMAGE)
8105 lphdi->mask |= HDI_IMAGE;
8106 lphdi->iImage = lpColumn->iImage;
8109 if (lpColumn->mask & LVCF_ORDER)
8111 lphdi->mask |= HDI_ORDER;
8112 lphdi->iOrder = lpColumn->iOrder;
8117 /***
8118 * DESCRIPTION:
8119 * Inserts a new column.
8121 * PARAMETER(S):
8122 * [I] infoPtr : valid pointer to the listview structure
8123 * [I] nColumn : column index
8124 * [I] lpColumn : column information
8125 * [I] isW : TRUE if lpColumn is Unicode, FALSE otherwise
8127 * RETURN:
8128 * SUCCESS : new column index
8129 * FAILURE : -1
8131 static INT LISTVIEW_InsertColumnT(LISTVIEW_INFO *infoPtr, INT nColumn,
8132 const LVCOLUMNW *lpColumn, BOOL isW)
8134 COLUMN_INFO *lpColumnInfo;
8135 INT nNewColumn;
8136 HDITEMW hdi;
8138 TRACE("(nColumn=%d, lpColumn=%s, isW=%d)\n", nColumn, debuglvcolumn_t(lpColumn, isW), isW);
8140 if (!lpColumn || nColumn < 0) return -1;
8141 nColumn = min(nColumn, DPA_GetPtrCount(infoPtr->hdpaColumns));
8143 ZeroMemory(&hdi, sizeof(HDITEMW));
8144 column_fill_hditem(infoPtr, &hdi, nColumn, lpColumn, isW);
8147 * A mask not including LVCF_WIDTH turns into a mask of width, width 10
8148 * (can be seen in SPY) otherwise column never gets added.
8150 if (!(lpColumn->mask & LVCF_WIDTH)) {
8151 hdi.mask |= HDI_WIDTH;
8152 hdi.cxy = 10;
8156 * when the iSubItem is available Windows copies it to the header lParam. It seems
8157 * to happen only in LVM_INSERTCOLUMN - not in LVM_SETCOLUMN
8159 if (lpColumn->mask & LVCF_SUBITEM)
8161 hdi.mask |= HDI_LPARAM;
8162 hdi.lParam = lpColumn->iSubItem;
8165 /* create header if not present */
8166 LISTVIEW_CreateHeader(infoPtr);
8167 if (!(LVS_NOCOLUMNHEADER & infoPtr->dwStyle) &&
8168 (infoPtr->uView == LV_VIEW_DETAILS) && (WS_VISIBLE & infoPtr->dwStyle))
8170 ShowWindow(infoPtr->hwndHeader, SW_SHOWNORMAL);
8173 /* insert item in header control */
8174 nNewColumn = SendMessageW(infoPtr->hwndHeader,
8175 isW ? HDM_INSERTITEMW : HDM_INSERTITEMA,
8176 nColumn, (LPARAM)&hdi);
8177 if (nNewColumn == -1) return -1;
8178 if (nNewColumn != nColumn) ERR("nColumn=%d, nNewColumn=%d\n", nColumn, nNewColumn);
8180 /* create our own column info */
8181 if (!(lpColumnInfo = Alloc(sizeof(COLUMN_INFO)))) goto fail;
8182 if (DPA_InsertPtr(infoPtr->hdpaColumns, nNewColumn, lpColumnInfo) == -1) goto fail;
8184 if (lpColumn->mask & LVCF_FMT) lpColumnInfo->fmt = lpColumn->fmt;
8185 if (lpColumn->mask & LVCF_MINWIDTH) lpColumnInfo->cxMin = lpColumn->cxMin;
8186 if (!SendMessageW(infoPtr->hwndHeader, HDM_GETITEMRECT, nNewColumn, (LPARAM)&lpColumnInfo->rcHeader))
8187 goto fail;
8189 /* now we have to actually adjust the data */
8190 if (!(infoPtr->dwStyle & LVS_OWNERDATA) && infoPtr->nItemCount > 0)
8192 SUBITEM_INFO *lpSubItem;
8193 HDPA hdpaSubItems;
8194 INT nItem, i;
8195 LVITEMW item;
8196 BOOL changed;
8198 item.iSubItem = nNewColumn;
8199 item.mask = LVIF_TEXT | LVIF_IMAGE;
8200 item.iImage = I_IMAGECALLBACK;
8201 item.pszText = LPSTR_TEXTCALLBACKW;
8203 for (nItem = 0; nItem < infoPtr->nItemCount; nItem++)
8205 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, nItem);
8206 for (i = 1; i < DPA_GetPtrCount(hdpaSubItems); i++)
8208 lpSubItem = DPA_GetPtr(hdpaSubItems, i);
8209 if (lpSubItem->iSubItem >= nNewColumn)
8210 lpSubItem->iSubItem++;
8213 /* add new subitem for each item */
8214 item.iItem = nItem;
8215 set_sub_item(infoPtr, &item, isW, &changed);
8219 /* make space for the new column */
8220 LISTVIEW_ScrollColumns(infoPtr, nNewColumn + 1, lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left);
8221 LISTVIEW_UpdateItemSize(infoPtr);
8223 return nNewColumn;
8225 fail:
8226 if (nNewColumn != -1) SendMessageW(infoPtr->hwndHeader, HDM_DELETEITEM, nNewColumn, 0);
8227 if (lpColumnInfo)
8229 DPA_DeletePtr(infoPtr->hdpaColumns, nNewColumn);
8230 Free(lpColumnInfo);
8232 return -1;
8235 /***
8236 * DESCRIPTION:
8237 * Sets the attributes of a header item.
8239 * PARAMETER(S):
8240 * [I] infoPtr : valid pointer to the listview structure
8241 * [I] nColumn : column index
8242 * [I] lpColumn : column attributes
8243 * [I] isW: if TRUE, then lpColumn is a LPLVCOLUMNW, else it is a LPLVCOLUMNA
8245 * RETURN:
8246 * SUCCESS : TRUE
8247 * FAILURE : FALSE
8249 static BOOL LISTVIEW_SetColumnT(const LISTVIEW_INFO *infoPtr, INT nColumn,
8250 const LVCOLUMNW *lpColumn, BOOL isW)
8252 HDITEMW hdi, hdiget;
8253 BOOL bResult;
8255 TRACE("(nColumn=%d, lpColumn=%s, isW=%d)\n", nColumn, debuglvcolumn_t(lpColumn, isW), isW);
8257 if (!lpColumn || nColumn < 0 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
8259 ZeroMemory(&hdi, sizeof(HDITEMW));
8260 if (lpColumn->mask & LVCF_FMT)
8262 hdi.mask |= HDI_FORMAT;
8263 hdiget.mask = HDI_FORMAT;
8264 if (SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nColumn, (LPARAM)&hdiget))
8265 hdi.fmt = hdiget.fmt & HDF_STRING;
8267 column_fill_hditem(infoPtr, &hdi, nColumn, lpColumn, isW);
8269 /* set header item attributes */
8270 bResult = SendMessageW(infoPtr->hwndHeader, isW ? HDM_SETITEMW : HDM_SETITEMA, nColumn, (LPARAM)&hdi);
8271 if (!bResult) return FALSE;
8273 if (lpColumn->mask & LVCF_FMT)
8275 COLUMN_INFO *lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, nColumn);
8276 INT oldFmt = lpColumnInfo->fmt;
8278 lpColumnInfo->fmt = lpColumn->fmt;
8279 if ((oldFmt ^ lpColumn->fmt) & (LVCFMT_JUSTIFYMASK | LVCFMT_IMAGE))
8281 if (infoPtr->uView == LV_VIEW_DETAILS) LISTVIEW_InvalidateColumn(infoPtr, nColumn);
8285 if (lpColumn->mask & LVCF_MINWIDTH)
8286 LISTVIEW_GetColumnInfo(infoPtr, nColumn)->cxMin = lpColumn->cxMin;
8288 return TRUE;
8291 /***
8292 * DESCRIPTION:
8293 * Sets the column order array
8295 * PARAMETERS:
8296 * [I] infoPtr : valid pointer to the listview structure
8297 * [I] iCount : number of elements in column order array
8298 * [I] lpiArray : pointer to column order array
8300 * RETURN:
8301 * SUCCESS : TRUE
8302 * FAILURE : FALSE
8304 static BOOL LISTVIEW_SetColumnOrderArray(LISTVIEW_INFO *infoPtr, INT iCount, const INT *lpiArray)
8306 TRACE("iCount %d lpiArray %p\n", iCount, lpiArray);
8308 if (!lpiArray || !IsWindow(infoPtr->hwndHeader)) return FALSE;
8310 infoPtr->colRectsDirty = TRUE;
8312 return SendMessageW(infoPtr->hwndHeader, HDM_SETORDERARRAY, iCount, (LPARAM)lpiArray);
8315 /***
8316 * DESCRIPTION:
8317 * Sets the width of a column
8319 * PARAMETERS:
8320 * [I] infoPtr : valid pointer to the listview structure
8321 * [I] nColumn : column index
8322 * [I] cx : column width
8324 * RETURN:
8325 * SUCCESS : TRUE
8326 * FAILURE : FALSE
8328 static BOOL LISTVIEW_SetColumnWidth(LISTVIEW_INFO *infoPtr, INT nColumn, INT cx)
8330 WCHAR szDispText[DISP_TEXT_SIZE] = { 0 };
8331 INT max_cx = 0;
8332 HDITEMW hdi;
8334 TRACE("(nColumn=%d, cx=%d)\n", nColumn, cx);
8336 /* set column width only if in report or list mode */
8337 if (infoPtr->uView != LV_VIEW_DETAILS && infoPtr->uView != LV_VIEW_LIST) return FALSE;
8339 /* take care of invalid cx values */
8340 if(infoPtr->uView == LV_VIEW_DETAILS && cx < -2) cx = LVSCW_AUTOSIZE;
8341 else if (infoPtr->uView == LV_VIEW_LIST && cx < 1) return FALSE;
8343 /* resize all columns if in LV_VIEW_LIST mode */
8344 if(infoPtr->uView == LV_VIEW_LIST)
8346 infoPtr->nItemWidth = cx;
8347 LISTVIEW_InvalidateList(infoPtr);
8348 return TRUE;
8351 if (nColumn < 0 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
8353 if (cx == LVSCW_AUTOSIZE || (cx == LVSCW_AUTOSIZE_USEHEADER && nColumn < DPA_GetPtrCount(infoPtr->hdpaColumns) -1))
8355 INT nLabelWidth;
8356 LVITEMW lvItem;
8358 lvItem.mask = LVIF_TEXT;
8359 lvItem.iItem = 0;
8360 lvItem.iSubItem = nColumn;
8361 lvItem.cchTextMax = DISP_TEXT_SIZE;
8362 for (; lvItem.iItem < infoPtr->nItemCount; lvItem.iItem++)
8364 lvItem.pszText = szDispText;
8365 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) continue;
8366 nLabelWidth = LISTVIEW_GetStringWidthT(infoPtr, lvItem.pszText, TRUE);
8367 if (max_cx < nLabelWidth) max_cx = nLabelWidth;
8369 if (infoPtr->himlSmall && (nColumn == 0 || (LISTVIEW_GetColumnInfo(infoPtr, nColumn)->fmt & LVCFMT_IMAGE)))
8370 max_cx += infoPtr->iconSize.cx;
8371 max_cx += TRAILING_LABEL_PADDING;
8374 /* autosize based on listview items width */
8375 if(cx == LVSCW_AUTOSIZE)
8376 cx = max_cx;
8377 else if(cx == LVSCW_AUTOSIZE_USEHEADER)
8379 /* if iCol is the last column make it fill the remainder of the controls width */
8380 if(nColumn == DPA_GetPtrCount(infoPtr->hdpaColumns) - 1)
8382 RECT rcHeader;
8383 POINT Origin;
8385 LISTVIEW_GetOrigin(infoPtr, &Origin);
8386 LISTVIEW_GetHeaderRect(infoPtr, nColumn, &rcHeader);
8388 cx = infoPtr->rcList.right - Origin.x - rcHeader.left;
8390 else
8392 /* Despite what the MS docs say, if this is not the last
8393 column, then MS resizes the column to the width of the
8394 largest text string in the column, including headers
8395 and items. This is different from LVSCW_AUTOSIZE in that
8396 LVSCW_AUTOSIZE ignores the header string length. */
8397 cx = 0;
8399 /* retrieve header text */
8400 hdi.mask = HDI_TEXT;
8401 hdi.cchTextMax = DISP_TEXT_SIZE;
8402 hdi.pszText = szDispText;
8403 if (SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nColumn, (LPARAM)&hdi))
8405 HDC hdc = GetDC(infoPtr->hwndSelf);
8406 HFONT old_font = SelectObject(hdc, (HFONT)SendMessageW(infoPtr->hwndHeader, WM_GETFONT, 0, 0));
8407 SIZE size;
8409 if (GetTextExtentPoint32W(hdc, hdi.pszText, lstrlenW(hdi.pszText), &size))
8410 cx = size.cx + TRAILING_HEADER_PADDING;
8411 /* FIXME: Take into account the header image, if one is present */
8412 SelectObject(hdc, old_font);
8413 ReleaseDC(infoPtr->hwndSelf, hdc);
8415 cx = max (cx, max_cx);
8419 if (cx < 0) return FALSE;
8421 /* call header to update the column change */
8422 hdi.mask = HDI_WIDTH;
8423 hdi.cxy = max(cx, LISTVIEW_GetColumnInfo(infoPtr, nColumn)->cxMin);
8424 TRACE("hdi.cxy=%d\n", hdi.cxy);
8425 return SendMessageW(infoPtr->hwndHeader, HDM_SETITEMW, nColumn, (LPARAM)&hdi);
8428 /***
8429 * Creates the checkbox imagelist. Helper for LISTVIEW_SetExtendedListViewStyle
8432 static HIMAGELIST LISTVIEW_CreateCheckBoxIL(const LISTVIEW_INFO *infoPtr)
8434 HDC hdc_wnd, hdc;
8435 HBITMAP hbm_im, hbm_mask, hbm_orig;
8436 RECT rc;
8437 HBRUSH hbr_white = GetStockObject(WHITE_BRUSH);
8438 HBRUSH hbr_black = GetStockObject(BLACK_BRUSH);
8439 HIMAGELIST himl;
8441 himl = ImageList_Create(GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON),
8442 ILC_COLOR | ILC_MASK, 2, 2);
8443 hdc_wnd = GetDC(infoPtr->hwndSelf);
8444 hdc = CreateCompatibleDC(hdc_wnd);
8445 hbm_im = CreateCompatibleBitmap(hdc_wnd, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON));
8446 hbm_mask = CreateBitmap(GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), 1, 1, NULL);
8447 ReleaseDC(infoPtr->hwndSelf, hdc_wnd);
8449 rc.left = rc.top = 0;
8450 rc.right = GetSystemMetrics(SM_CXSMICON);
8451 rc.bottom = GetSystemMetrics(SM_CYSMICON);
8453 hbm_orig = SelectObject(hdc, hbm_mask);
8454 FillRect(hdc, &rc, hbr_white);
8455 InflateRect(&rc, -2, -2);
8456 FillRect(hdc, &rc, hbr_black);
8458 SelectObject(hdc, hbm_im);
8459 DrawFrameControl(hdc, &rc, DFC_BUTTON, DFCS_BUTTONCHECK | DFCS_MONO);
8460 SelectObject(hdc, hbm_orig);
8461 ImageList_Add(himl, hbm_im, hbm_mask);
8463 SelectObject(hdc, hbm_im);
8464 DrawFrameControl(hdc, &rc, DFC_BUTTON, DFCS_BUTTONCHECK | DFCS_MONO | DFCS_CHECKED);
8465 SelectObject(hdc, hbm_orig);
8466 ImageList_Add(himl, hbm_im, hbm_mask);
8468 DeleteObject(hbm_mask);
8469 DeleteObject(hbm_im);
8470 DeleteDC(hdc);
8472 return himl;
8475 /***
8476 * DESCRIPTION:
8477 * Sets the extended listview style.
8479 * PARAMETERS:
8480 * [I] infoPtr : valid pointer to the listview structure
8481 * [I] dwMask : mask
8482 * [I] dwStyle : style
8484 * RETURN:
8485 * SUCCESS : previous style
8486 * FAILURE : 0
8488 static DWORD LISTVIEW_SetExtendedListViewStyle(LISTVIEW_INFO *infoPtr, DWORD mask, DWORD ex_style)
8490 DWORD old_ex_style = infoPtr->dwLvExStyle;
8492 TRACE("mask=0x%08x, ex_style=0x%08x\n", mask, ex_style);
8494 /* set new style */
8495 if (mask)
8496 infoPtr->dwLvExStyle = (old_ex_style & ~mask) | (ex_style & mask);
8497 else
8498 infoPtr->dwLvExStyle = ex_style;
8500 if((infoPtr->dwLvExStyle ^ old_ex_style) & LVS_EX_CHECKBOXES)
8502 HIMAGELIST himl = 0;
8503 if(infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES)
8505 LVITEMW item;
8506 item.mask = LVIF_STATE;
8507 item.stateMask = LVIS_STATEIMAGEMASK;
8508 item.state = INDEXTOSTATEIMAGEMASK(1);
8509 LISTVIEW_SetItemState(infoPtr, -1, &item);
8511 himl = LISTVIEW_CreateCheckBoxIL(infoPtr);
8512 if(!(infoPtr->dwStyle & LVS_SHAREIMAGELISTS))
8513 ImageList_Destroy(infoPtr->himlState);
8515 himl = LISTVIEW_SetImageList(infoPtr, LVSIL_STATE, himl);
8516 /* checkbox list replaces previous custom list or... */
8517 if(((infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES) &&
8518 !(infoPtr->dwStyle & LVS_SHAREIMAGELISTS)) ||
8519 /* ...previous was checkbox list */
8520 (old_ex_style & LVS_EX_CHECKBOXES))
8521 ImageList_Destroy(himl);
8524 if((infoPtr->dwLvExStyle ^ old_ex_style) & LVS_EX_HEADERDRAGDROP)
8526 DWORD style;
8528 /* if not already created */
8529 LISTVIEW_CreateHeader(infoPtr);
8531 style = GetWindowLongW(infoPtr->hwndHeader, GWL_STYLE);
8532 if (infoPtr->dwLvExStyle & LVS_EX_HEADERDRAGDROP)
8533 style |= HDS_DRAGDROP;
8534 else
8535 style &= ~HDS_DRAGDROP;
8536 SetWindowLongW(infoPtr->hwndHeader, GWL_STYLE, style);
8539 /* GRIDLINES adds decoration at top so changes sizes */
8540 if((infoPtr->dwLvExStyle ^ old_ex_style) & LVS_EX_GRIDLINES)
8542 LISTVIEW_CreateHeader(infoPtr);
8543 LISTVIEW_UpdateSize(infoPtr);
8546 if((infoPtr->dwLvExStyle ^ old_ex_style) & LVS_EX_FULLROWSELECT)
8548 LISTVIEW_CreateHeader(infoPtr);
8551 if((infoPtr->dwLvExStyle ^ old_ex_style) & LVS_EX_TRANSPARENTBKGND)
8553 if (infoPtr->dwLvExStyle & LVS_EX_TRANSPARENTBKGND)
8554 LISTVIEW_SetBkColor(infoPtr, CLR_NONE);
8557 if((infoPtr->dwLvExStyle ^ old_ex_style) & LVS_EX_HEADERINALLVIEWS)
8559 if (infoPtr->dwLvExStyle & LVS_EX_HEADERINALLVIEWS)
8560 LISTVIEW_CreateHeader(infoPtr);
8561 else
8562 ShowWindow(infoPtr->hwndHeader, SW_HIDE);
8563 LISTVIEW_UpdateSize(infoPtr);
8564 LISTVIEW_UpdateScroll(infoPtr);
8567 LISTVIEW_InvalidateList(infoPtr);
8568 return old_ex_style;
8571 /***
8572 * DESCRIPTION:
8573 * Sets the new hot cursor used during hot tracking and hover selection.
8575 * PARAMETER(S):
8576 * [I] infoPtr : valid pointer to the listview structure
8577 * [I] hCursor : the new hot cursor handle
8579 * RETURN:
8580 * Returns the previous hot cursor
8582 static HCURSOR LISTVIEW_SetHotCursor(LISTVIEW_INFO *infoPtr, HCURSOR hCursor)
8584 HCURSOR oldCursor = infoPtr->hHotCursor;
8586 infoPtr->hHotCursor = hCursor;
8588 return oldCursor;
8592 /***
8593 * DESCRIPTION:
8594 * Sets the hot item index.
8596 * PARAMETERS:
8597 * [I] infoPtr : valid pointer to the listview structure
8598 * [I] iIndex : index
8600 * RETURN:
8601 * SUCCESS : previous hot item index
8602 * FAILURE : -1 (no hot item)
8604 static INT LISTVIEW_SetHotItem(LISTVIEW_INFO *infoPtr, INT iIndex)
8606 INT iOldIndex = infoPtr->nHotItem;
8608 infoPtr->nHotItem = iIndex;
8610 return iOldIndex;
8614 /***
8615 * DESCRIPTION:
8616 * Sets the amount of time the cursor must hover over an item before it is selected.
8618 * PARAMETER(S):
8619 * [I] infoPtr : valid pointer to the listview structure
8620 * [I] dwHoverTime : hover time, if -1 the hover time is set to the default
8622 * RETURN:
8623 * Returns the previous hover time
8625 static DWORD LISTVIEW_SetHoverTime(LISTVIEW_INFO *infoPtr, DWORD dwHoverTime)
8627 DWORD oldHoverTime = infoPtr->dwHoverTime;
8629 infoPtr->dwHoverTime = dwHoverTime;
8631 return oldHoverTime;
8634 /***
8635 * DESCRIPTION:
8636 * Sets spacing for icons of LVS_ICON style.
8638 * PARAMETER(S):
8639 * [I] infoPtr : valid pointer to the listview structure
8640 * [I] cx : horizontal spacing (-1 = system spacing, 0 = autosize)
8641 * [I] cy : vertical spacing (-1 = system spacing, 0 = autosize)
8643 * RETURN:
8644 * MAKELONG(oldcx, oldcy)
8646 static DWORD LISTVIEW_SetIconSpacing(LISTVIEW_INFO *infoPtr, INT cx, INT cy)
8648 INT iconWidth = 0, iconHeight = 0;
8649 DWORD oldspacing = MAKELONG(infoPtr->iconSpacing.cx, infoPtr->iconSpacing.cy);
8651 TRACE("requested=(%d,%d)\n", cx, cy);
8653 /* set to defaults, if instructed to */
8654 if (cx == -1 && cy == -1)
8656 infoPtr->autoSpacing = TRUE;
8657 if (infoPtr->himlNormal)
8658 ImageList_GetIconSize(infoPtr->himlNormal, &iconWidth, &iconHeight);
8659 cx = GetSystemMetrics(SM_CXICONSPACING) - GetSystemMetrics(SM_CXICON) + iconWidth;
8660 cy = GetSystemMetrics(SM_CYICONSPACING) - GetSystemMetrics(SM_CYICON) + iconHeight;
8662 else
8663 infoPtr->autoSpacing = FALSE;
8665 /* if 0 then keep width */
8666 if (cx != 0)
8667 infoPtr->iconSpacing.cx = cx;
8669 /* if 0 then keep height */
8670 if (cy != 0)
8671 infoPtr->iconSpacing.cy = cy;
8673 TRACE("old=(%d,%d), new=(%d,%d), iconSize=(%d,%d), ntmH=%d\n",
8674 LOWORD(oldspacing), HIWORD(oldspacing), infoPtr->iconSpacing.cx, infoPtr->iconSpacing.cy,
8675 infoPtr->iconSize.cx, infoPtr->iconSize.cy,
8676 infoPtr->ntmHeight);
8678 /* these depend on the iconSpacing */
8679 LISTVIEW_UpdateItemSize(infoPtr);
8681 return oldspacing;
8684 static inline void set_icon_size(SIZE *size, HIMAGELIST himl, BOOL small)
8686 INT cx, cy;
8688 if (himl && ImageList_GetIconSize(himl, &cx, &cy))
8690 size->cx = cx;
8691 size->cy = cy;
8693 else
8695 size->cx = GetSystemMetrics(small ? SM_CXSMICON : SM_CXICON);
8696 size->cy = GetSystemMetrics(small ? SM_CYSMICON : SM_CYICON);
8700 /***
8701 * DESCRIPTION:
8702 * Sets image lists.
8704 * PARAMETER(S):
8705 * [I] infoPtr : valid pointer to the listview structure
8706 * [I] nType : image list type
8707 * [I] himl : image list handle
8709 * RETURN:
8710 * SUCCESS : old image list
8711 * FAILURE : NULL
8713 static HIMAGELIST LISTVIEW_SetImageList(LISTVIEW_INFO *infoPtr, INT nType, HIMAGELIST himl)
8715 INT oldHeight = infoPtr->nItemHeight;
8716 HIMAGELIST himlOld = 0;
8718 TRACE("(nType=%d, himl=%p)\n", nType, himl);
8720 switch (nType)
8722 case LVSIL_NORMAL:
8723 himlOld = infoPtr->himlNormal;
8724 infoPtr->himlNormal = himl;
8725 if (infoPtr->uView == LV_VIEW_ICON) set_icon_size(&infoPtr->iconSize, himl, FALSE);
8726 if (infoPtr->autoSpacing)
8727 LISTVIEW_SetIconSpacing(infoPtr, -1, -1);
8728 break;
8730 case LVSIL_SMALL:
8731 himlOld = infoPtr->himlSmall;
8732 infoPtr->himlSmall = himl;
8733 if (infoPtr->uView != LV_VIEW_ICON) set_icon_size(&infoPtr->iconSize, himl, TRUE);
8734 if (infoPtr->hwndHeader)
8735 SendMessageW(infoPtr->hwndHeader, HDM_SETIMAGELIST, 0, (LPARAM)himl);
8736 break;
8738 case LVSIL_STATE:
8739 himlOld = infoPtr->himlState;
8740 infoPtr->himlState = himl;
8741 set_icon_size(&infoPtr->iconStateSize, himl, TRUE);
8742 ImageList_SetBkColor(infoPtr->himlState, CLR_NONE);
8743 break;
8745 default:
8746 ERR("Unknown icon type=%d\n", nType);
8747 return NULL;
8750 infoPtr->nItemHeight = LISTVIEW_CalculateItemHeight(infoPtr);
8751 if (infoPtr->nItemHeight != oldHeight)
8752 LISTVIEW_UpdateScroll(infoPtr);
8754 return himlOld;
8757 /***
8758 * DESCRIPTION:
8759 * Preallocates memory (does *not* set the actual count of items !)
8761 * PARAMETER(S):
8762 * [I] infoPtr : valid pointer to the listview structure
8763 * [I] nItems : item count (projected number of items to allocate)
8764 * [I] dwFlags : update flags
8766 * RETURN:
8767 * SUCCESS : TRUE
8768 * FAILURE : FALSE
8770 static BOOL LISTVIEW_SetItemCount(LISTVIEW_INFO *infoPtr, INT nItems, DWORD dwFlags)
8772 TRACE("(nItems=%d, dwFlags=%x)\n", nItems, dwFlags);
8774 if (infoPtr->dwStyle & LVS_OWNERDATA)
8776 INT nOldCount = infoPtr->nItemCount;
8778 if (nItems < nOldCount)
8780 RANGE range = { nItems, nOldCount };
8781 ranges_del(infoPtr->selectionRanges, range);
8782 if (infoPtr->nFocusedItem >= nItems)
8784 LISTVIEW_SetItemFocus(infoPtr, -1);
8785 SetRectEmpty(&infoPtr->rcFocus);
8789 infoPtr->nItemCount = nItems;
8790 LISTVIEW_UpdateScroll(infoPtr);
8792 /* the flags are valid only in ownerdata report and list modes */
8793 if (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON) dwFlags = 0;
8795 if (!(dwFlags & LVSICF_NOSCROLL) && infoPtr->nFocusedItem != -1)
8796 LISTVIEW_EnsureVisible(infoPtr, infoPtr->nFocusedItem, FALSE);
8798 if (!(dwFlags & LVSICF_NOINVALIDATEALL))
8799 LISTVIEW_InvalidateList(infoPtr);
8800 else
8802 INT nFrom, nTo;
8803 POINT Origin;
8804 RECT rcErase;
8806 LISTVIEW_GetOrigin(infoPtr, &Origin);
8807 nFrom = min(nOldCount, nItems);
8808 nTo = max(nOldCount, nItems);
8810 if (infoPtr->uView == LV_VIEW_DETAILS)
8812 rcErase.left = 0;
8813 rcErase.top = nFrom * infoPtr->nItemHeight;
8814 rcErase.right = infoPtr->nItemWidth;
8815 rcErase.bottom = nTo * infoPtr->nItemHeight;
8816 OffsetRect(&rcErase, Origin.x, Origin.y);
8817 if (IntersectRect(&rcErase, &rcErase, &infoPtr->rcList))
8818 LISTVIEW_InvalidateRect(infoPtr, &rcErase);
8820 else /* LV_VIEW_LIST */
8822 INT nPerCol = LISTVIEW_GetCountPerColumn(infoPtr);
8824 rcErase.left = (nFrom / nPerCol) * infoPtr->nItemWidth;
8825 rcErase.top = (nFrom % nPerCol) * infoPtr->nItemHeight;
8826 rcErase.right = rcErase.left + infoPtr->nItemWidth;
8827 rcErase.bottom = nPerCol * infoPtr->nItemHeight;
8828 OffsetRect(&rcErase, Origin.x, Origin.y);
8829 if (IntersectRect(&rcErase, &rcErase, &infoPtr->rcList))
8830 LISTVIEW_InvalidateRect(infoPtr, &rcErase);
8832 rcErase.left = (nFrom / nPerCol + 1) * infoPtr->nItemWidth;
8833 rcErase.top = 0;
8834 rcErase.right = (nTo / nPerCol + 1) * infoPtr->nItemWidth;
8835 rcErase.bottom = nPerCol * infoPtr->nItemHeight;
8836 OffsetRect(&rcErase, Origin.x, Origin.y);
8837 if (IntersectRect(&rcErase, &rcErase, &infoPtr->rcList))
8838 LISTVIEW_InvalidateRect(infoPtr, &rcErase);
8842 else
8844 /* According to MSDN for non-LVS_OWNERDATA this is just
8845 * a performance issue. The control allocates its internal
8846 * data structures for the number of items specified. It
8847 * cuts down on the number of memory allocations. Therefore
8848 * we will just issue a WARN here
8850 WARN("for non-ownerdata performance option not implemented.\n");
8853 return TRUE;
8856 /***
8857 * DESCRIPTION:
8858 * Sets the position of an item.
8860 * PARAMETER(S):
8861 * [I] infoPtr : valid pointer to the listview structure
8862 * [I] nItem : item index
8863 * [I] pt : coordinate
8865 * RETURN:
8866 * SUCCESS : TRUE
8867 * FAILURE : FALSE
8869 static BOOL LISTVIEW_SetItemPosition(LISTVIEW_INFO *infoPtr, INT nItem, const POINT *pt)
8871 POINT Origin, Pt;
8873 TRACE("(nItem=%d, pt=%s)\n", nItem, wine_dbgstr_point(pt));
8875 if (!pt || nItem < 0 || nItem >= infoPtr->nItemCount ||
8876 !(infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON)) return FALSE;
8878 Pt = *pt;
8879 LISTVIEW_GetOrigin(infoPtr, &Origin);
8881 /* This point value seems to be an undocumented feature.
8882 * The best guess is that it means either at the origin,
8883 * or at true beginning of the list. I will assume the origin. */
8884 if ((Pt.x == -1) && (Pt.y == -1))
8885 Pt = Origin;
8887 if (infoPtr->uView == LV_VIEW_ICON)
8889 Pt.x -= (infoPtr->nItemWidth - infoPtr->iconSize.cx) / 2;
8890 Pt.y -= ICON_TOP_PADDING;
8892 Pt.x -= Origin.x;
8893 Pt.y -= Origin.y;
8895 infoPtr->bAutoarrange = FALSE;
8897 return LISTVIEW_MoveIconTo(infoPtr, nItem, &Pt, FALSE);
8900 /***
8901 * DESCRIPTION:
8902 * Sets the state of one or many items.
8904 * PARAMETER(S):
8905 * [I] infoPtr : valid pointer to the listview structure
8906 * [I] nItem : item index
8907 * [I] item : item or subitem info
8909 * RETURN:
8910 * SUCCESS : TRUE
8911 * FAILURE : FALSE
8913 static BOOL LISTVIEW_SetItemState(LISTVIEW_INFO *infoPtr, INT nItem, const LVITEMW *item)
8915 BOOL ret = TRUE;
8916 LVITEMW lvItem;
8918 if (!item) return FALSE;
8920 lvItem.iItem = nItem;
8921 lvItem.iSubItem = 0;
8922 lvItem.mask = LVIF_STATE;
8923 lvItem.state = item->state;
8924 lvItem.stateMask = item->stateMask;
8925 TRACE("item=%s\n", debuglvitem_t(&lvItem, TRUE));
8927 if (nItem == -1)
8929 UINT oldstate = 0;
8930 BOOL notify;
8932 /* special case optimization for recurring attempt to deselect all */
8933 if (lvItem.state == 0 && lvItem.stateMask == LVIS_SELECTED && !LISTVIEW_GetSelectedCount(infoPtr))
8934 return TRUE;
8936 /* select all isn't allowed in LVS_SINGLESEL */
8937 if ((lvItem.state & lvItem.stateMask & LVIS_SELECTED) && (infoPtr->dwStyle & LVS_SINGLESEL))
8938 return FALSE;
8940 /* focus all isn't allowed */
8941 if (lvItem.state & lvItem.stateMask & LVIS_FOCUSED) return FALSE;
8943 notify = infoPtr->bDoChangeNotify;
8944 if (infoPtr->dwStyle & LVS_OWNERDATA)
8946 infoPtr->bDoChangeNotify = FALSE;
8947 if (!(lvItem.state & LVIS_SELECTED) && LISTVIEW_GetSelectedCount(infoPtr))
8948 oldstate |= LVIS_SELECTED;
8949 if (infoPtr->nFocusedItem != -1) oldstate |= LVIS_FOCUSED;
8952 /* apply to all items */
8953 for (lvItem.iItem = 0; lvItem.iItem < infoPtr->nItemCount; lvItem.iItem++)
8954 if (!LISTVIEW_SetItemT(infoPtr, &lvItem, TRUE)) ret = FALSE;
8956 if (infoPtr->dwStyle & LVS_OWNERDATA)
8958 NMLISTVIEW nmlv;
8960 infoPtr->bDoChangeNotify = notify;
8962 nmlv.iItem = -1;
8963 nmlv.iSubItem = 0;
8964 nmlv.uNewState = lvItem.state & lvItem.stateMask;
8965 nmlv.uOldState = oldstate & lvItem.stateMask;
8966 nmlv.uChanged = LVIF_STATE;
8967 nmlv.ptAction.x = nmlv.ptAction.y = 0;
8968 nmlv.lParam = 0;
8970 notify_listview(infoPtr, LVN_ITEMCHANGED, &nmlv);
8973 else
8974 ret = LISTVIEW_SetItemT(infoPtr, &lvItem, TRUE);
8976 return ret;
8979 /***
8980 * DESCRIPTION:
8981 * Sets the text of an item or subitem.
8983 * PARAMETER(S):
8984 * [I] hwnd : window handle
8985 * [I] nItem : item index
8986 * [I] lpLVItem : item or subitem info
8987 * [I] isW : TRUE if input is Unicode
8989 * RETURN:
8990 * SUCCESS : TRUE
8991 * FAILURE : FALSE
8993 static BOOL LISTVIEW_SetItemTextT(LISTVIEW_INFO *infoPtr, INT nItem, const LVITEMW *lpLVItem, BOOL isW)
8995 LVITEMW lvItem;
8997 if (!lpLVItem || nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
8998 if (infoPtr->dwStyle & LVS_OWNERDATA) return FALSE;
9000 lvItem.iItem = nItem;
9001 lvItem.iSubItem = lpLVItem->iSubItem;
9002 lvItem.mask = LVIF_TEXT;
9003 lvItem.pszText = lpLVItem->pszText;
9004 lvItem.cchTextMax = lpLVItem->cchTextMax;
9006 TRACE("(nItem=%d, lpLVItem=%s, isW=%d)\n", nItem, debuglvitem_t(&lvItem, isW), isW);
9008 return LISTVIEW_SetItemT(infoPtr, &lvItem, isW);
9011 /***
9012 * DESCRIPTION:
9013 * Set item index that marks the start of a multiple selection.
9015 * PARAMETER(S):
9016 * [I] infoPtr : valid pointer to the listview structure
9017 * [I] nIndex : index
9019 * RETURN:
9020 * Index number or -1 if there is no selection mark.
9022 static INT LISTVIEW_SetSelectionMark(LISTVIEW_INFO *infoPtr, INT nIndex)
9024 INT nOldIndex = infoPtr->nSelectionMark;
9026 TRACE("(nIndex=%d)\n", nIndex);
9028 infoPtr->nSelectionMark = nIndex;
9030 return nOldIndex;
9033 /***
9034 * DESCRIPTION:
9035 * Sets the text background color.
9037 * PARAMETER(S):
9038 * [I] infoPtr : valid pointer to the listview structure
9039 * [I] color : text background color
9041 * RETURN:
9042 * SUCCESS : TRUE
9043 * FAILURE : FALSE
9045 static BOOL LISTVIEW_SetTextBkColor(LISTVIEW_INFO *infoPtr, COLORREF color)
9047 TRACE("(color=%x)\n", color);
9049 infoPtr->clrTextBk = color;
9050 return TRUE;
9053 /***
9054 * DESCRIPTION:
9055 * Sets the text foreground color.
9057 * PARAMETER(S):
9058 * [I] infoPtr : valid pointer to the listview structure
9059 * [I] color : text color
9061 * RETURN:
9062 * SUCCESS : TRUE
9063 * FAILURE : FALSE
9065 static BOOL LISTVIEW_SetTextColor (LISTVIEW_INFO *infoPtr, COLORREF color)
9067 TRACE("(color=%x)\n", color);
9069 infoPtr->clrText = color;
9070 return TRUE;
9073 /***
9074 * DESCRIPTION:
9075 * Sets new ToolTip window to ListView control.
9077 * PARAMETER(S):
9078 * [I] infoPtr : valid pointer to the listview structure
9079 * [I] hwndNewToolTip : handle to new ToolTip
9081 * RETURN:
9082 * old tool tip
9084 static HWND LISTVIEW_SetToolTips( LISTVIEW_INFO *infoPtr, HWND hwndNewToolTip)
9086 HWND hwndOldToolTip = infoPtr->hwndToolTip;
9087 infoPtr->hwndToolTip = hwndNewToolTip;
9088 return hwndOldToolTip;
9092 * DESCRIPTION:
9093 * sets the Unicode character format flag for the control
9094 * PARAMETER(S):
9095 * [I] infoPtr :valid pointer to the listview structure
9096 * [I] fUnicode :true to switch to UNICODE false to switch to ANSI
9098 * RETURN:
9099 * Old Unicode Format
9101 static BOOL LISTVIEW_SetUnicodeFormat( LISTVIEW_INFO *infoPtr, BOOL unicode)
9103 SHORT rc = infoPtr->notifyFormat;
9104 infoPtr->notifyFormat = (unicode) ? NFR_UNICODE : NFR_ANSI;
9105 return rc == NFR_UNICODE;
9109 * DESCRIPTION:
9110 * sets the control view mode
9111 * PARAMETER(S):
9112 * [I] infoPtr :valid pointer to the listview structure
9113 * [I] nView :new view mode value
9115 * RETURN:
9116 * SUCCESS: 1
9117 * FAILURE: -1
9119 static INT LISTVIEW_SetView(LISTVIEW_INFO *infoPtr, DWORD nView)
9121 HIMAGELIST himl;
9123 if (infoPtr->uView == nView) return 1;
9125 if ((INT)nView < 0 || nView > LV_VIEW_MAX) return -1;
9126 if (nView == LV_VIEW_TILE)
9128 FIXME("View LV_VIEW_TILE unimplemented\n");
9129 return -1;
9132 infoPtr->uView = nView;
9134 SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
9135 ShowWindow(infoPtr->hwndHeader, SW_HIDE);
9137 ShowScrollBar(infoPtr->hwndSelf, SB_BOTH, FALSE);
9138 SetRectEmpty(&infoPtr->rcFocus);
9140 himl = (nView == LV_VIEW_ICON ? infoPtr->himlNormal : infoPtr->himlSmall);
9141 set_icon_size(&infoPtr->iconSize, himl, nView != LV_VIEW_ICON);
9143 switch (nView)
9145 case LV_VIEW_ICON:
9146 case LV_VIEW_SMALLICON:
9147 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
9148 break;
9149 case LV_VIEW_DETAILS:
9151 HDLAYOUT hl;
9152 WINDOWPOS wp;
9154 LISTVIEW_CreateHeader( infoPtr );
9156 hl.prc = &infoPtr->rcList;
9157 hl.pwpos = &wp;
9158 SendMessageW(infoPtr->hwndHeader, HDM_LAYOUT, 0, (LPARAM)&hl);
9159 SetWindowPos(infoPtr->hwndHeader, infoPtr->hwndSelf, wp.x, wp.y, wp.cx, wp.cy,
9160 wp.flags | ((infoPtr->dwStyle & LVS_NOCOLUMNHEADER) ? SWP_HIDEWINDOW : SWP_SHOWWINDOW));
9161 break;
9163 case LV_VIEW_LIST:
9164 break;
9167 LISTVIEW_UpdateItemSize(infoPtr);
9168 LISTVIEW_UpdateSize(infoPtr);
9169 LISTVIEW_UpdateScroll(infoPtr);
9170 LISTVIEW_InvalidateList(infoPtr);
9172 TRACE("nView=%d\n", nView);
9174 return 1;
9177 /* LISTVIEW_SetWorkAreas */
9179 /***
9180 * DESCRIPTION:
9181 * Callback internally used by LISTVIEW_SortItems() in response of LVM_SORTITEMS
9183 * PARAMETER(S):
9184 * [I] first : pointer to first ITEM_INFO to compare
9185 * [I] second : pointer to second ITEM_INFO to compare
9186 * [I] lParam : HWND of control
9188 * RETURN:
9189 * if first comes before second : negative
9190 * if first comes after second : positive
9191 * if first and second are equivalent : zero
9193 static INT WINAPI LISTVIEW_CallBackCompare(LPVOID first, LPVOID second, LPARAM lParam)
9195 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)lParam;
9196 ITEM_INFO* lv_first = DPA_GetPtr( first, 0 );
9197 ITEM_INFO* lv_second = DPA_GetPtr( second, 0 );
9199 /* Forward the call to the client defined callback */
9200 return (infoPtr->pfnCompare)( lv_first->lParam , lv_second->lParam, infoPtr->lParamSort );
9203 /***
9204 * DESCRIPTION:
9205 * Callback internally used by LISTVIEW_SortItems() in response of LVM_SORTITEMSEX
9207 * PARAMETER(S):
9208 * [I] first : pointer to first ITEM_INFO to compare
9209 * [I] second : pointer to second ITEM_INFO to compare
9210 * [I] lParam : HWND of control
9212 * RETURN:
9213 * if first comes before second : negative
9214 * if first comes after second : positive
9215 * if first and second are equivalent : zero
9217 static INT WINAPI LISTVIEW_CallBackCompareEx(LPVOID first, LPVOID second, LPARAM lParam)
9219 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)lParam;
9220 INT first_idx = DPA_GetPtrIndex( infoPtr->hdpaItems, first );
9221 INT second_idx = DPA_GetPtrIndex( infoPtr->hdpaItems, second );
9223 /* Forward the call to the client defined callback */
9224 return (infoPtr->pfnCompare)( first_idx, second_idx, infoPtr->lParamSort );
9227 /***
9228 * DESCRIPTION:
9229 * Sorts the listview items.
9231 * PARAMETER(S):
9232 * [I] infoPtr : valid pointer to the listview structure
9233 * [I] pfnCompare : application-defined value
9234 * [I] lParamSort : pointer to comparison callback
9235 * [I] IsEx : TRUE when LVM_SORTITEMSEX used
9237 * RETURN:
9238 * SUCCESS : TRUE
9239 * FAILURE : FALSE
9241 static BOOL LISTVIEW_SortItems(LISTVIEW_INFO *infoPtr, PFNLVCOMPARE pfnCompare,
9242 LPARAM lParamSort, BOOL IsEx)
9244 HDPA hdpaSubItems;
9245 ITEM_INFO *lpItem;
9246 LPVOID selectionMarkItem = NULL;
9247 LPVOID focusedItem = NULL;
9248 int i;
9250 TRACE("(pfnCompare=%p, lParamSort=%lx)\n", pfnCompare, lParamSort);
9252 if (infoPtr->dwStyle & LVS_OWNERDATA) return FALSE;
9254 if (!pfnCompare) return FALSE;
9255 if (!infoPtr->hdpaItems) return FALSE;
9257 /* if there are 0 or 1 items, there is no need to sort */
9258 if (infoPtr->nItemCount < 2) return TRUE;
9260 /* clear selection */
9261 ranges_clear(infoPtr->selectionRanges);
9263 /* save selection mark and focused item */
9264 if (infoPtr->nSelectionMark >= 0)
9265 selectionMarkItem = DPA_GetPtr(infoPtr->hdpaItems, infoPtr->nSelectionMark);
9266 if (infoPtr->nFocusedItem >= 0)
9267 focusedItem = DPA_GetPtr(infoPtr->hdpaItems, infoPtr->nFocusedItem);
9269 infoPtr->pfnCompare = pfnCompare;
9270 infoPtr->lParamSort = lParamSort;
9271 if (IsEx)
9272 DPA_Sort(infoPtr->hdpaItems, LISTVIEW_CallBackCompareEx, (LPARAM)infoPtr);
9273 else
9274 DPA_Sort(infoPtr->hdpaItems, LISTVIEW_CallBackCompare, (LPARAM)infoPtr);
9276 /* restore selection ranges */
9277 for (i=0; i < infoPtr->nItemCount; i++)
9279 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, i);
9280 lpItem = DPA_GetPtr(hdpaSubItems, 0);
9282 if (lpItem->state & LVIS_SELECTED)
9283 ranges_additem(infoPtr->selectionRanges, i);
9285 /* restore selection mark and focused item */
9286 infoPtr->nSelectionMark = DPA_GetPtrIndex(infoPtr->hdpaItems, selectionMarkItem);
9287 infoPtr->nFocusedItem = DPA_GetPtrIndex(infoPtr->hdpaItems, focusedItem);
9289 /* I believe nHotItem should be left alone, see LISTVIEW_ShiftIndices */
9291 /* refresh the display */
9292 LISTVIEW_InvalidateList(infoPtr);
9293 return TRUE;
9296 /***
9297 * DESCRIPTION:
9298 * Update theme handle after a theme change.
9300 * PARAMETER(S):
9301 * [I] infoPtr : valid pointer to the listview structure
9303 * RETURN:
9304 * SUCCESS : 0
9305 * FAILURE : something else
9307 static LRESULT LISTVIEW_ThemeChanged(const LISTVIEW_INFO *infoPtr)
9309 HTHEME theme = GetWindowTheme(infoPtr->hwndSelf);
9310 CloseThemeData(theme);
9311 OpenThemeData(infoPtr->hwndSelf, themeClass);
9312 return 0;
9315 /***
9316 * DESCRIPTION:
9317 * Updates an items or rearranges the listview control.
9319 * PARAMETER(S):
9320 * [I] infoPtr : valid pointer to the listview structure
9321 * [I] nItem : item index
9323 * RETURN:
9324 * SUCCESS : TRUE
9325 * FAILURE : FALSE
9327 static BOOL LISTVIEW_Update(LISTVIEW_INFO *infoPtr, INT nItem)
9329 TRACE("(nItem=%d)\n", nItem);
9331 if (nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
9333 /* rearrange with default alignment style */
9334 if (is_autoarrange(infoPtr))
9335 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
9336 else
9337 LISTVIEW_InvalidateItem(infoPtr, nItem);
9339 return TRUE;
9342 /***
9343 * DESCRIPTION:
9344 * Draw the track line at the place defined in the infoPtr structure.
9345 * The line is drawn with a XOR pen so drawing the line for the second time
9346 * in the same place erases the line.
9348 * PARAMETER(S):
9349 * [I] infoPtr : valid pointer to the listview structure
9351 * RETURN:
9352 * SUCCESS : TRUE
9353 * FAILURE : FALSE
9355 static BOOL LISTVIEW_DrawTrackLine(const LISTVIEW_INFO *infoPtr)
9357 HDC hdc;
9359 if (infoPtr->xTrackLine == -1)
9360 return FALSE;
9362 if (!(hdc = GetDC(infoPtr->hwndSelf)))
9363 return FALSE;
9364 PatBlt( hdc, infoPtr->xTrackLine, infoPtr->rcList.top,
9365 1, infoPtr->rcList.bottom - infoPtr->rcList.top, DSTINVERT );
9366 ReleaseDC(infoPtr->hwndSelf, hdc);
9367 return TRUE;
9370 /***
9371 * DESCRIPTION:
9372 * Called when an edit control should be displayed. This function is called after
9373 * we are sure that there was a single click - not a double click (this is a TIMERPROC).
9375 * PARAMETER(S):
9376 * [I] hwnd : Handle to the listview
9377 * [I] uMsg : WM_TIMER (ignored)
9378 * [I] idEvent : The timer ID interpreted as a pointer to a DELAYED_EDIT_ITEM struct
9379 * [I] dwTimer : The elapsed time (ignored)
9381 * RETURN:
9382 * None.
9384 static VOID CALLBACK LISTVIEW_DelayedEditItem(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
9386 DELAYED_ITEM_EDIT *editItem = (DELAYED_ITEM_EDIT *)idEvent;
9387 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongPtrW(hwnd, 0);
9389 KillTimer(hwnd, idEvent);
9390 editItem->fEnabled = FALSE;
9391 /* check if the item is still selected */
9392 if (infoPtr->bFocus && LISTVIEW_GetItemState(infoPtr, editItem->iItem, LVIS_SELECTED))
9393 LISTVIEW_EditLabelT(infoPtr, editItem->iItem, TRUE);
9396 /***
9397 * DESCRIPTION:
9398 * Creates the listview control - the WM_NCCREATE phase.
9400 * PARAMETER(S):
9401 * [I] hwnd : window handle
9402 * [I] lpcs : the create parameters
9404 * RETURN:
9405 * Success: TRUE
9406 * Failure: FALSE
9408 static LRESULT LISTVIEW_NCCreate(HWND hwnd, const CREATESTRUCTW *lpcs)
9410 LISTVIEW_INFO *infoPtr;
9411 LOGFONTW logFont;
9413 TRACE("(lpcs=%p)\n", lpcs);
9415 /* initialize info pointer */
9416 infoPtr = Alloc(sizeof(LISTVIEW_INFO));
9417 if (!infoPtr) return FALSE;
9419 SetWindowLongPtrW(hwnd, 0, (DWORD_PTR)infoPtr);
9421 infoPtr->hwndSelf = hwnd;
9422 infoPtr->dwStyle = lpcs->style; /* Note: may be changed in WM_CREATE */
9423 map_style_view(infoPtr);
9424 /* determine the type of structures to use */
9425 infoPtr->hwndNotify = lpcs->hwndParent;
9426 /* infoPtr->notifyFormat will be filled in WM_CREATE */
9428 /* initialize color information */
9429 infoPtr->clrBk = CLR_NONE;
9430 infoPtr->clrText = CLR_DEFAULT;
9431 infoPtr->clrTextBk = CLR_DEFAULT;
9432 LISTVIEW_SetBkColor(infoPtr, comctl32_color.clrWindow);
9434 /* set default values */
9435 infoPtr->nFocusedItem = -1;
9436 infoPtr->nSelectionMark = -1;
9437 infoPtr->nHotItem = -1;
9438 infoPtr->bRedraw = TRUE;
9439 infoPtr->bNoItemMetrics = TRUE;
9440 infoPtr->bDoChangeNotify = TRUE;
9441 infoPtr->autoSpacing = TRUE;
9442 infoPtr->iconSpacing.cx = GetSystemMetrics(SM_CXICONSPACING) - GetSystemMetrics(SM_CXICON);
9443 infoPtr->iconSpacing.cy = GetSystemMetrics(SM_CYICONSPACING) - GetSystemMetrics(SM_CYICON);
9444 infoPtr->nEditLabelItem = -1;
9445 infoPtr->nLButtonDownItem = -1;
9446 infoPtr->dwHoverTime = HOVER_DEFAULT; /* default system hover time */
9447 infoPtr->cWheelRemainder = 0;
9448 infoPtr->nMeasureItemHeight = 0;
9449 infoPtr->xTrackLine = -1; /* no track line */
9450 infoPtr->itemEdit.fEnabled = FALSE;
9451 infoPtr->iVersion = COMCTL32_VERSION;
9452 infoPtr->colRectsDirty = FALSE;
9454 /* get default font (icon title) */
9455 SystemParametersInfoW(SPI_GETICONTITLELOGFONT, 0, &logFont, 0);
9456 infoPtr->hDefaultFont = CreateFontIndirectW(&logFont);
9457 infoPtr->hFont = infoPtr->hDefaultFont;
9458 LISTVIEW_SaveTextMetrics(infoPtr);
9460 /* allocate memory for the data structure */
9461 if (!(infoPtr->selectionRanges = ranges_create(10))) goto fail;
9462 if (!(infoPtr->hdpaItems = DPA_Create(10))) goto fail;
9463 if (!(infoPtr->hdpaItemIds = DPA_Create(10))) goto fail;
9464 if (!(infoPtr->hdpaPosX = DPA_Create(10))) goto fail;
9465 if (!(infoPtr->hdpaPosY = DPA_Create(10))) goto fail;
9466 if (!(infoPtr->hdpaColumns = DPA_Create(10))) goto fail;
9467 return TRUE;
9469 fail:
9470 DestroyWindow(infoPtr->hwndHeader);
9471 ranges_destroy(infoPtr->selectionRanges);
9472 DPA_Destroy(infoPtr->hdpaItems);
9473 DPA_Destroy(infoPtr->hdpaItemIds);
9474 DPA_Destroy(infoPtr->hdpaPosX);
9475 DPA_Destroy(infoPtr->hdpaPosY);
9476 DPA_Destroy(infoPtr->hdpaColumns);
9477 Free(infoPtr);
9478 return FALSE;
9481 /***
9482 * DESCRIPTION:
9483 * Creates the listview control - the WM_CREATE phase. Most of the data is
9484 * already set up in LISTVIEW_NCCreate
9486 * PARAMETER(S):
9487 * [I] hwnd : window handle
9488 * [I] lpcs : the create parameters
9490 * RETURN:
9491 * Success: 0
9492 * Failure: -1
9494 static LRESULT LISTVIEW_Create(HWND hwnd, const CREATESTRUCTW *lpcs)
9496 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongPtrW(hwnd, 0);
9498 TRACE("(lpcs=%p, style=0x%08x)\n", lpcs, lpcs->style);
9500 infoPtr->dwStyle = lpcs->style;
9501 map_style_view(infoPtr);
9503 infoPtr->notifyFormat = SendMessageW(infoPtr->hwndNotify, WM_NOTIFYFORMAT,
9504 (WPARAM)infoPtr->hwndSelf, NF_QUERY);
9505 /* on error defaulting to ANSI notifications */
9506 if (infoPtr->notifyFormat == 0) infoPtr->notifyFormat = NFR_ANSI;
9507 TRACE("notify format=%d\n", infoPtr->notifyFormat);
9509 if ((infoPtr->uView == LV_VIEW_DETAILS) && (lpcs->style & WS_VISIBLE))
9511 if (LISTVIEW_CreateHeader(infoPtr) < 0) return -1;
9513 else
9514 infoPtr->hwndHeader = 0;
9516 /* init item size to avoid division by 0 */
9517 LISTVIEW_UpdateItemSize (infoPtr);
9518 LISTVIEW_UpdateSize (infoPtr);
9520 if (infoPtr->uView == LV_VIEW_DETAILS)
9522 if (!(LVS_NOCOLUMNHEADER & lpcs->style) && (WS_VISIBLE & lpcs->style))
9524 ShowWindow(infoPtr->hwndHeader, SW_SHOWNORMAL);
9526 LISTVIEW_UpdateScroll(infoPtr);
9527 /* send WM_MEASUREITEM notification */
9528 if (infoPtr->dwStyle & LVS_OWNERDRAWFIXED) notify_measureitem(infoPtr);
9531 OpenThemeData(hwnd, themeClass);
9533 /* initialize the icon sizes */
9534 set_icon_size(&infoPtr->iconSize, infoPtr->himlNormal, infoPtr->uView != LV_VIEW_ICON);
9535 set_icon_size(&infoPtr->iconStateSize, infoPtr->himlState, TRUE);
9536 return 0;
9539 /***
9540 * DESCRIPTION:
9541 * Destroys the listview control.
9543 * PARAMETER(S):
9544 * [I] infoPtr : valid pointer to the listview structure
9546 * RETURN:
9547 * Success: 0
9548 * Failure: -1
9550 static LRESULT LISTVIEW_Destroy(LISTVIEW_INFO *infoPtr)
9552 HTHEME theme = GetWindowTheme(infoPtr->hwndSelf);
9553 CloseThemeData(theme);
9555 /* delete all items */
9556 LISTVIEW_DeleteAllItems(infoPtr, TRUE);
9558 return 0;
9561 /***
9562 * DESCRIPTION:
9563 * Enables the listview control.
9565 * PARAMETER(S):
9566 * [I] infoPtr : valid pointer to the listview structure
9567 * [I] bEnable : specifies whether to enable or disable the window
9569 * RETURN:
9570 * SUCCESS : TRUE
9571 * FAILURE : FALSE
9573 static BOOL LISTVIEW_Enable(const LISTVIEW_INFO *infoPtr)
9575 if (infoPtr->dwStyle & LVS_OWNERDRAWFIXED)
9576 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
9577 return TRUE;
9580 /***
9581 * DESCRIPTION:
9582 * Erases the background of the listview control.
9584 * PARAMETER(S):
9585 * [I] infoPtr : valid pointer to the listview structure
9586 * [I] hdc : device context handle
9588 * RETURN:
9589 * SUCCESS : TRUE
9590 * FAILURE : FALSE
9592 static inline BOOL LISTVIEW_EraseBkgnd(const LISTVIEW_INFO *infoPtr, HDC hdc)
9594 RECT rc;
9596 TRACE("(hdc=%p)\n", hdc);
9598 if (!GetClipBox(hdc, &rc)) return FALSE;
9600 if (infoPtr->clrBk == CLR_NONE)
9602 if (infoPtr->dwLvExStyle & LVS_EX_TRANSPARENTBKGND)
9603 return SendMessageW(infoPtr->hwndNotify, WM_PRINTCLIENT,
9604 (WPARAM)hdc, PRF_ERASEBKGND);
9605 else
9606 return SendMessageW(infoPtr->hwndNotify, WM_ERASEBKGND, (WPARAM)hdc, 0);
9609 /* for double buffered controls we need to do this during refresh */
9610 if (infoPtr->dwLvExStyle & LVS_EX_DOUBLEBUFFER) return FALSE;
9612 return LISTVIEW_FillBkgnd(infoPtr, hdc, &rc);
9616 /***
9617 * DESCRIPTION:
9618 * Helper function for LISTVIEW_[HV]Scroll *only*.
9619 * Performs vertical/horizontal scrolling by a give amount.
9621 * PARAMETER(S):
9622 * [I] infoPtr : valid pointer to the listview structure
9623 * [I] dx : amount of horizontal scroll
9624 * [I] dy : amount of vertical scroll
9626 static void scroll_list(LISTVIEW_INFO *infoPtr, INT dx, INT dy)
9628 /* now we can scroll the list */
9629 ScrollWindowEx(infoPtr->hwndSelf, dx, dy, &infoPtr->rcList,
9630 &infoPtr->rcList, 0, 0, SW_ERASE | SW_INVALIDATE);
9631 /* if we have focus, adjust rect */
9632 OffsetRect(&infoPtr->rcFocus, dx, dy);
9633 UpdateWindow(infoPtr->hwndSelf);
9636 /***
9637 * DESCRIPTION:
9638 * Performs vertical scrolling.
9640 * PARAMETER(S):
9641 * [I] infoPtr : valid pointer to the listview structure
9642 * [I] nScrollCode : scroll code
9643 * [I] nScrollDiff : units to scroll in SB_INTERNAL mode, 0 otherwise
9644 * [I] hScrollWnd : scrollbar control window handle
9646 * RETURN:
9647 * Zero
9649 * NOTES:
9650 * SB_LINEUP/SB_LINEDOWN:
9651 * for LVS_ICON, LVS_SMALLICON is 37 by experiment
9652 * for LVS_REPORT is 1 line
9653 * for LVS_LIST cannot occur
9656 static LRESULT LISTVIEW_VScroll(LISTVIEW_INFO *infoPtr, INT nScrollCode,
9657 INT nScrollDiff)
9659 INT nOldScrollPos, nNewScrollPos;
9660 SCROLLINFO scrollInfo;
9661 BOOL is_an_icon;
9663 TRACE("(nScrollCode=%d(%s), nScrollDiff=%d)\n", nScrollCode,
9664 debugscrollcode(nScrollCode), nScrollDiff);
9666 if (infoPtr->hwndEdit) SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
9668 scrollInfo.cbSize = sizeof(SCROLLINFO);
9669 scrollInfo.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_TRACKPOS;
9671 is_an_icon = ((infoPtr->uView == LV_VIEW_ICON) || (infoPtr->uView == LV_VIEW_SMALLICON));
9673 if (!GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo)) return 1;
9675 nOldScrollPos = scrollInfo.nPos;
9676 switch (nScrollCode)
9678 case SB_INTERNAL:
9679 break;
9681 case SB_LINEUP:
9682 nScrollDiff = (is_an_icon) ? -LISTVIEW_SCROLL_ICON_LINE_SIZE : -1;
9683 break;
9685 case SB_LINEDOWN:
9686 nScrollDiff = (is_an_icon) ? LISTVIEW_SCROLL_ICON_LINE_SIZE : 1;
9687 break;
9689 case SB_PAGEUP:
9690 nScrollDiff = -scrollInfo.nPage;
9691 break;
9693 case SB_PAGEDOWN:
9694 nScrollDiff = scrollInfo.nPage;
9695 break;
9697 case SB_THUMBPOSITION:
9698 case SB_THUMBTRACK:
9699 nScrollDiff = scrollInfo.nTrackPos - scrollInfo.nPos;
9700 break;
9702 default:
9703 nScrollDiff = 0;
9706 /* quit right away if pos isn't changing */
9707 if (nScrollDiff == 0) return 0;
9709 /* calculate new position, and handle overflows */
9710 nNewScrollPos = scrollInfo.nPos + nScrollDiff;
9711 if (nScrollDiff > 0) {
9712 if (nNewScrollPos < nOldScrollPos ||
9713 nNewScrollPos > scrollInfo.nMax)
9714 nNewScrollPos = scrollInfo.nMax;
9715 } else {
9716 if (nNewScrollPos > nOldScrollPos ||
9717 nNewScrollPos < scrollInfo.nMin)
9718 nNewScrollPos = scrollInfo.nMin;
9721 /* set the new position, and reread in case it changed */
9722 scrollInfo.fMask = SIF_POS;
9723 scrollInfo.nPos = nNewScrollPos;
9724 nNewScrollPos = SetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo, TRUE);
9726 /* carry on only if it really changed */
9727 if (nNewScrollPos == nOldScrollPos) return 0;
9729 /* now adjust to client coordinates */
9730 nScrollDiff = nOldScrollPos - nNewScrollPos;
9731 if (infoPtr->uView == LV_VIEW_DETAILS) nScrollDiff *= infoPtr->nItemHeight;
9733 /* and scroll the window */
9734 scroll_list(infoPtr, 0, nScrollDiff);
9736 return 0;
9739 /***
9740 * DESCRIPTION:
9741 * Performs horizontal scrolling.
9743 * PARAMETER(S):
9744 * [I] infoPtr : valid pointer to the listview structure
9745 * [I] nScrollCode : scroll code
9746 * [I] nScrollDiff : units to scroll in SB_INTERNAL mode, 0 otherwise
9747 * [I] hScrollWnd : scrollbar control window handle
9749 * RETURN:
9750 * Zero
9752 * NOTES:
9753 * SB_LINELEFT/SB_LINERIGHT:
9754 * for LVS_ICON, LVS_SMALLICON 1 pixel
9755 * for LVS_REPORT is 1 pixel
9756 * for LVS_LIST is 1 column --> which is a 1 because the
9757 * scroll is based on columns not pixels
9760 static LRESULT LISTVIEW_HScroll(LISTVIEW_INFO *infoPtr, INT nScrollCode,
9761 INT nScrollDiff)
9763 INT nOldScrollPos, nNewScrollPos;
9764 SCROLLINFO scrollInfo;
9765 BOOL is_an_icon;
9767 TRACE("(nScrollCode=%d(%s), nScrollDiff=%d)\n", nScrollCode,
9768 debugscrollcode(nScrollCode), nScrollDiff);
9770 if (infoPtr->hwndEdit) SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
9772 scrollInfo.cbSize = sizeof(SCROLLINFO);
9773 scrollInfo.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_TRACKPOS;
9775 is_an_icon = ((infoPtr->uView == LV_VIEW_ICON) || (infoPtr->uView == LV_VIEW_SMALLICON));
9777 if (!GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo)) return 1;
9779 nOldScrollPos = scrollInfo.nPos;
9781 switch (nScrollCode)
9783 case SB_INTERNAL:
9784 break;
9786 case SB_LINELEFT:
9787 nScrollDiff = (is_an_icon) ? -LISTVIEW_SCROLL_ICON_LINE_SIZE : -1;
9788 break;
9790 case SB_LINERIGHT:
9791 nScrollDiff = (is_an_icon) ? LISTVIEW_SCROLL_ICON_LINE_SIZE : 1;
9792 break;
9794 case SB_PAGELEFT:
9795 nScrollDiff = -scrollInfo.nPage;
9796 break;
9798 case SB_PAGERIGHT:
9799 nScrollDiff = scrollInfo.nPage;
9800 break;
9802 case SB_THUMBPOSITION:
9803 case SB_THUMBTRACK:
9804 nScrollDiff = scrollInfo.nTrackPos - scrollInfo.nPos;
9805 break;
9807 default:
9808 nScrollDiff = 0;
9811 /* quit right away if pos isn't changing */
9812 if (nScrollDiff == 0) return 0;
9814 /* calculate new position, and handle overflows */
9815 nNewScrollPos = scrollInfo.nPos + nScrollDiff;
9816 if (nScrollDiff > 0) {
9817 if (nNewScrollPos < nOldScrollPos ||
9818 nNewScrollPos > scrollInfo.nMax)
9819 nNewScrollPos = scrollInfo.nMax;
9820 } else {
9821 if (nNewScrollPos > nOldScrollPos ||
9822 nNewScrollPos < scrollInfo.nMin)
9823 nNewScrollPos = scrollInfo.nMin;
9826 /* set the new position, and reread in case it changed */
9827 scrollInfo.fMask = SIF_POS;
9828 scrollInfo.nPos = nNewScrollPos;
9829 nNewScrollPos = SetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo, TRUE);
9831 /* carry on only if it really changed */
9832 if (nNewScrollPos == nOldScrollPos) return 0;
9834 if (infoPtr->hwndHeader) LISTVIEW_UpdateHeaderSize(infoPtr, nNewScrollPos);
9836 /* now adjust to client coordinates */
9837 nScrollDiff = nOldScrollPos - nNewScrollPos;
9838 if (infoPtr->uView == LV_VIEW_LIST) nScrollDiff *= infoPtr->nItemWidth;
9840 /* and scroll the window */
9841 scroll_list(infoPtr, nScrollDiff, 0);
9843 return 0;
9846 static LRESULT LISTVIEW_MouseWheel(LISTVIEW_INFO *infoPtr, INT wheelDelta)
9848 UINT pulScrollLines = 3;
9850 TRACE("(wheelDelta=%d)\n", wheelDelta);
9852 switch(infoPtr->uView)
9854 case LV_VIEW_ICON:
9855 case LV_VIEW_SMALLICON:
9857 * listview should be scrolled by a multiple of 37 dependently on its dimension or its visible item number
9858 * should be fixed in the future.
9860 LISTVIEW_VScroll(infoPtr, SB_INTERNAL, (wheelDelta > 0) ?
9861 -LISTVIEW_SCROLL_ICON_LINE_SIZE : LISTVIEW_SCROLL_ICON_LINE_SIZE);
9862 break;
9864 case LV_VIEW_DETAILS:
9865 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
9867 /* if scrolling changes direction, ignore left overs */
9868 if ((wheelDelta < 0 && infoPtr->cWheelRemainder < 0) ||
9869 (wheelDelta > 0 && infoPtr->cWheelRemainder > 0))
9870 infoPtr->cWheelRemainder += wheelDelta;
9871 else
9872 infoPtr->cWheelRemainder = wheelDelta;
9873 if (infoPtr->cWheelRemainder && pulScrollLines)
9875 int cLineScroll;
9876 pulScrollLines = min((UINT)LISTVIEW_GetCountPerColumn(infoPtr), pulScrollLines);
9877 cLineScroll = pulScrollLines * (float)infoPtr->cWheelRemainder / WHEEL_DELTA;
9878 infoPtr->cWheelRemainder -= WHEEL_DELTA * cLineScroll / (int)pulScrollLines;
9879 LISTVIEW_VScroll(infoPtr, SB_INTERNAL, -cLineScroll);
9881 break;
9883 case LV_VIEW_LIST:
9884 LISTVIEW_HScroll(infoPtr, (wheelDelta > 0) ? SB_LINELEFT : SB_LINERIGHT, 0);
9885 break;
9887 return 0;
9890 /***
9891 * DESCRIPTION:
9892 * ???
9894 * PARAMETER(S):
9895 * [I] infoPtr : valid pointer to the listview structure
9896 * [I] nVirtualKey : virtual key
9897 * [I] lKeyData : key data
9899 * RETURN:
9900 * Zero
9902 static LRESULT LISTVIEW_KeyDown(LISTVIEW_INFO *infoPtr, INT nVirtualKey, LONG lKeyData)
9904 HWND hwndSelf = infoPtr->hwndSelf;
9905 INT nItem = -1;
9906 NMLVKEYDOWN nmKeyDown;
9908 TRACE("(nVirtualKey=%d, lKeyData=%d)\n", nVirtualKey, lKeyData);
9910 /* send LVN_KEYDOWN notification */
9911 nmKeyDown.wVKey = nVirtualKey;
9912 nmKeyDown.flags = 0;
9913 notify_hdr(infoPtr, LVN_KEYDOWN, &nmKeyDown.hdr);
9914 if (!IsWindow(hwndSelf))
9915 return 0;
9917 switch (nVirtualKey)
9919 case VK_SPACE:
9920 nItem = infoPtr->nFocusedItem;
9921 if (infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES)
9922 toggle_checkbox_state(infoPtr, infoPtr->nFocusedItem);
9923 break;
9925 case VK_RETURN:
9926 if ((infoPtr->nItemCount > 0) && (infoPtr->nFocusedItem != -1))
9928 if (!notify(infoPtr, NM_RETURN)) return 0;
9929 if (!notify(infoPtr, LVN_ITEMACTIVATE)) return 0;
9931 break;
9933 case VK_HOME:
9934 if (infoPtr->nItemCount > 0)
9935 nItem = 0;
9936 break;
9938 case VK_END:
9939 if (infoPtr->nItemCount > 0)
9940 nItem = infoPtr->nItemCount - 1;
9941 break;
9943 case VK_LEFT:
9944 nItem = LISTVIEW_GetNextItem(infoPtr, infoPtr->nFocusedItem, LVNI_TOLEFT);
9945 break;
9947 case VK_UP:
9948 nItem = LISTVIEW_GetNextItem(infoPtr, infoPtr->nFocusedItem, LVNI_ABOVE);
9949 break;
9951 case VK_RIGHT:
9952 nItem = LISTVIEW_GetNextItem(infoPtr, infoPtr->nFocusedItem, LVNI_TORIGHT);
9953 break;
9955 case VK_DOWN:
9956 nItem = LISTVIEW_GetNextItem(infoPtr, infoPtr->nFocusedItem, LVNI_BELOW);
9957 break;
9959 case VK_PRIOR:
9960 if (infoPtr->uView == LV_VIEW_DETAILS)
9962 INT topidx = LISTVIEW_GetTopIndex(infoPtr);
9963 if (infoPtr->nFocusedItem == topidx)
9964 nItem = topidx - LISTVIEW_GetCountPerColumn(infoPtr) + 1;
9965 else
9966 nItem = topidx;
9968 else
9969 nItem = infoPtr->nFocusedItem - LISTVIEW_GetCountPerColumn(infoPtr)
9970 * LISTVIEW_GetCountPerRow(infoPtr);
9971 if(nItem < 0) nItem = 0;
9972 break;
9974 case VK_NEXT:
9975 if (infoPtr->uView == LV_VIEW_DETAILS)
9977 INT topidx = LISTVIEW_GetTopIndex(infoPtr);
9978 INT cnt = LISTVIEW_GetCountPerColumn(infoPtr);
9979 if (infoPtr->nFocusedItem == topidx + cnt - 1)
9980 nItem = infoPtr->nFocusedItem + cnt - 1;
9981 else
9982 nItem = topidx + cnt - 1;
9984 else
9985 nItem = infoPtr->nFocusedItem + LISTVIEW_GetCountPerColumn(infoPtr)
9986 * LISTVIEW_GetCountPerRow(infoPtr);
9987 if(nItem >= infoPtr->nItemCount) nItem = infoPtr->nItemCount - 1;
9988 break;
9991 if ((nItem != -1) && (nItem != infoPtr->nFocusedItem || nVirtualKey == VK_SPACE))
9992 LISTVIEW_KeySelection(infoPtr, nItem, nVirtualKey == VK_SPACE);
9994 return 0;
9997 /***
9998 * DESCRIPTION:
9999 * Kills the focus.
10001 * PARAMETER(S):
10002 * [I] infoPtr : valid pointer to the listview structure
10004 * RETURN:
10005 * Zero
10007 static LRESULT LISTVIEW_KillFocus(LISTVIEW_INFO *infoPtr)
10009 TRACE("()\n");
10011 /* drop any left over scroll amount */
10012 infoPtr->cWheelRemainder = 0;
10014 /* if we did not have the focus, there's nothing more to do */
10015 if (!infoPtr->bFocus) return 0;
10017 /* send NM_KILLFOCUS notification */
10018 if (!notify(infoPtr, NM_KILLFOCUS)) return 0;
10020 /* if we have a focus rectangle, get rid of it */
10021 LISTVIEW_ShowFocusRect(infoPtr, FALSE);
10023 /* if have a marquee selection, stop it */
10024 if (infoPtr->bMarqueeSelect)
10026 /* Remove the marquee rectangle and release our mouse capture */
10027 LISTVIEW_InvalidateRect(infoPtr, &infoPtr->marqueeRect);
10028 ReleaseCapture();
10030 SetRect(&infoPtr->marqueeRect, 0, 0, 0, 0);
10032 infoPtr->bMarqueeSelect = FALSE;
10033 infoPtr->bScrolling = FALSE;
10034 KillTimer(infoPtr->hwndSelf, (UINT_PTR) infoPtr);
10037 /* set window focus flag */
10038 infoPtr->bFocus = FALSE;
10040 /* invalidate the selected items before resetting focus flag */
10041 LISTVIEW_InvalidateSelectedItems(infoPtr);
10043 return 0;
10046 /***
10047 * DESCRIPTION:
10048 * Processes double click messages (left mouse button).
10050 * PARAMETER(S):
10051 * [I] infoPtr : valid pointer to the listview structure
10052 * [I] wKey : key flag
10053 * [I] x,y : mouse coordinate
10055 * RETURN:
10056 * Zero
10058 static LRESULT LISTVIEW_LButtonDblClk(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
10060 LVHITTESTINFO htInfo;
10062 TRACE("(key=%hu, X=%u, Y=%u)\n", wKey, x, y);
10064 /* Cancel the item edition if any */
10065 if (infoPtr->itemEdit.fEnabled)
10067 KillTimer(infoPtr->hwndSelf, (UINT_PTR)&infoPtr->itemEdit);
10068 infoPtr->itemEdit.fEnabled = FALSE;
10071 /* send NM_RELEASEDCAPTURE notification */
10072 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
10074 htInfo.pt.x = x;
10075 htInfo.pt.y = y;
10077 /* send NM_DBLCLK notification */
10078 LISTVIEW_HitTest(infoPtr, &htInfo, TRUE, FALSE);
10079 if (!notify_click(infoPtr, NM_DBLCLK, &htInfo)) return 0;
10081 /* To send the LVN_ITEMACTIVATE, it must be on an Item */
10082 if(htInfo.iItem != -1) notify_itemactivate(infoPtr,&htInfo);
10084 return 0;
10087 static LRESULT LISTVIEW_TrackMouse(const LISTVIEW_INFO *infoPtr, POINT pt)
10089 MSG msg;
10090 RECT r;
10092 r.top = r.bottom = pt.y;
10093 r.left = r.right = pt.x;
10095 InflateRect(&r, GetSystemMetrics(SM_CXDRAG), GetSystemMetrics(SM_CYDRAG));
10097 SetCapture(infoPtr->hwndSelf);
10099 while (1)
10101 if (PeekMessageW(&msg, 0, 0, 0, PM_REMOVE | PM_NOYIELD))
10103 if (msg.message == WM_MOUSEMOVE)
10105 pt.x = (short)LOWORD(msg.lParam);
10106 pt.y = (short)HIWORD(msg.lParam);
10107 if (PtInRect(&r, pt))
10108 continue;
10109 else
10111 ReleaseCapture();
10112 return 1;
10115 else if (msg.message >= WM_LBUTTONDOWN &&
10116 msg.message <= WM_RBUTTONDBLCLK)
10118 break;
10121 DispatchMessageW(&msg);
10124 if (GetCapture() != infoPtr->hwndSelf)
10125 return 0;
10128 ReleaseCapture();
10129 return 0;
10133 /***
10134 * DESCRIPTION:
10135 * Processes mouse down messages (left mouse button).
10137 * PARAMETERS:
10138 * infoPtr [I ] valid pointer to the listview structure
10139 * wKey [I ] key flag
10140 * x,y [I ] mouse coordinate
10142 * RETURN:
10143 * Zero
10145 static LRESULT LISTVIEW_LButtonDown(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
10147 LVHITTESTINFO lvHitTestInfo;
10148 static BOOL bGroupSelect = TRUE;
10149 POINT pt = { x, y };
10150 INT nItem;
10152 TRACE("(key=%hu, X=%u, Y=%u)\n", wKey, x, y);
10154 /* send NM_RELEASEDCAPTURE notification */
10155 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
10157 /* set left button down flag and record the click position */
10158 infoPtr->bLButtonDown = TRUE;
10159 infoPtr->ptClickPos = pt;
10160 infoPtr->bDragging = FALSE;
10161 infoPtr->bMarqueeSelect = FALSE;
10162 infoPtr->bScrolling = FALSE;
10164 lvHitTestInfo.pt.x = x;
10165 lvHitTestInfo.pt.y = y;
10167 nItem = LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, TRUE);
10168 TRACE("at %s, nItem=%d\n", wine_dbgstr_point(&pt), nItem);
10169 if ((nItem >= 0) && (nItem < infoPtr->nItemCount))
10171 if ((infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES) && (lvHitTestInfo.flags & LVHT_ONITEMSTATEICON))
10173 toggle_checkbox_state(infoPtr, nItem);
10174 return 0;
10177 if (infoPtr->dwStyle & LVS_SINGLESEL)
10179 if (LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED))
10180 infoPtr->nEditLabelItem = nItem;
10181 else
10182 LISTVIEW_SetSelection(infoPtr, nItem);
10184 else
10186 if ((wKey & MK_CONTROL) && (wKey & MK_SHIFT))
10188 if (bGroupSelect)
10190 if (!LISTVIEW_AddGroupSelection(infoPtr, nItem)) return 0;
10191 LISTVIEW_SetItemFocus(infoPtr, nItem);
10192 infoPtr->nSelectionMark = nItem;
10194 else
10196 LVITEMW item;
10198 item.state = LVIS_SELECTED | LVIS_FOCUSED;
10199 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
10201 LISTVIEW_SetItemState(infoPtr,nItem,&item);
10202 infoPtr->nSelectionMark = nItem;
10205 else if (wKey & MK_CONTROL)
10207 LVITEMW item;
10209 bGroupSelect = (LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED) == 0);
10211 item.state = (bGroupSelect ? LVIS_SELECTED : 0) | LVIS_FOCUSED;
10212 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
10213 LISTVIEW_SetItemState(infoPtr, nItem, &item);
10214 infoPtr->nSelectionMark = nItem;
10216 else if (wKey & MK_SHIFT)
10218 LISTVIEW_SetGroupSelection(infoPtr, nItem);
10220 else
10222 if (LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED))
10224 infoPtr->nEditLabelItem = nItem;
10225 infoPtr->nLButtonDownItem = nItem;
10227 LISTVIEW_SetItemFocus(infoPtr, nItem);
10229 else
10230 /* set selection (clears other pre-existing selections) */
10231 LISTVIEW_SetSelection(infoPtr, nItem);
10235 if (infoPtr->dwLvExStyle & LVS_EX_ONECLICKACTIVATE)
10236 if(lvHitTestInfo.iItem != -1) notify_itemactivate(infoPtr,&lvHitTestInfo);
10238 else
10240 if (!infoPtr->bFocus)
10241 SetFocus(infoPtr->hwndSelf);
10243 /* remove all selections */
10244 if (!(wKey & MK_CONTROL) && !(wKey & MK_SHIFT))
10245 LISTVIEW_DeselectAll(infoPtr);
10246 ReleaseCapture();
10249 return 0;
10252 /***
10253 * DESCRIPTION:
10254 * Processes mouse up messages (left mouse button).
10256 * PARAMETERS:
10257 * infoPtr [I ] valid pointer to the listview structure
10258 * wKey [I ] key flag
10259 * x,y [I ] mouse coordinate
10261 * RETURN:
10262 * Zero
10264 static LRESULT LISTVIEW_LButtonUp(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
10266 LVHITTESTINFO lvHitTestInfo;
10268 TRACE("(key=%hu, X=%u, Y=%u)\n", wKey, x, y);
10270 if (!infoPtr->bLButtonDown) return 0;
10272 lvHitTestInfo.pt.x = x;
10273 lvHitTestInfo.pt.y = y;
10275 /* send NM_CLICK notification */
10276 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
10277 if (!notify_click(infoPtr, NM_CLICK, &lvHitTestInfo)) return 0;
10279 /* set left button flag */
10280 infoPtr->bLButtonDown = FALSE;
10282 /* set a single selection, reset others */
10283 if(lvHitTestInfo.iItem == infoPtr->nLButtonDownItem && lvHitTestInfo.iItem != -1)
10284 LISTVIEW_SetSelection(infoPtr, infoPtr->nLButtonDownItem);
10285 infoPtr->nLButtonDownItem = -1;
10287 if (infoPtr->bDragging || infoPtr->bMarqueeSelect)
10289 /* Remove the marquee rectangle and release our mouse capture */
10290 if (infoPtr->bMarqueeSelect)
10292 LISTVIEW_InvalidateRect(infoPtr, &infoPtr->marqueeDrawRect);
10293 ReleaseCapture();
10296 SetRect(&infoPtr->marqueeRect, 0, 0, 0, 0);
10297 SetRect(&infoPtr->marqueeDrawRect, 0, 0, 0, 0);
10299 infoPtr->bDragging = FALSE;
10300 infoPtr->bMarqueeSelect = FALSE;
10301 infoPtr->bScrolling = FALSE;
10303 KillTimer(infoPtr->hwndSelf, (UINT_PTR) infoPtr);
10304 return 0;
10307 /* if we clicked on a selected item, edit the label */
10308 if(lvHitTestInfo.iItem == infoPtr->nEditLabelItem && (lvHitTestInfo.flags & LVHT_ONITEMLABEL))
10310 /* we want to make sure the user doesn't want to do a double click. So we will
10311 * delay the edit. WM_LBUTTONDBLCLICK will cancel the timer
10313 infoPtr->itemEdit.fEnabled = TRUE;
10314 infoPtr->itemEdit.iItem = lvHitTestInfo.iItem;
10315 SetTimer(infoPtr->hwndSelf,
10316 (UINT_PTR)&infoPtr->itemEdit,
10317 GetDoubleClickTime(),
10318 LISTVIEW_DelayedEditItem);
10321 if (!infoPtr->bFocus)
10322 SetFocus(infoPtr->hwndSelf);
10324 return 0;
10327 /***
10328 * DESCRIPTION:
10329 * Destroys the listview control (called after WM_DESTROY).
10331 * PARAMETER(S):
10332 * [I] infoPtr : valid pointer to the listview structure
10334 * RETURN:
10335 * Zero
10337 static LRESULT LISTVIEW_NCDestroy(LISTVIEW_INFO *infoPtr)
10339 INT i;
10341 TRACE("()\n");
10343 /* destroy data structure */
10344 DPA_Destroy(infoPtr->hdpaItems);
10345 DPA_Destroy(infoPtr->hdpaItemIds);
10346 DPA_Destroy(infoPtr->hdpaPosX);
10347 DPA_Destroy(infoPtr->hdpaPosY);
10348 /* columns */
10349 for (i = 0; i < DPA_GetPtrCount(infoPtr->hdpaColumns); i++)
10350 Free(DPA_GetPtr(infoPtr->hdpaColumns, i));
10351 DPA_Destroy(infoPtr->hdpaColumns);
10352 ranges_destroy(infoPtr->selectionRanges);
10354 /* destroy image lists */
10355 if (!(infoPtr->dwStyle & LVS_SHAREIMAGELISTS))
10357 ImageList_Destroy(infoPtr->himlNormal);
10358 ImageList_Destroy(infoPtr->himlSmall);
10359 ImageList_Destroy(infoPtr->himlState);
10362 /* destroy font, bkgnd brush */
10363 infoPtr->hFont = 0;
10364 if (infoPtr->hDefaultFont) DeleteObject(infoPtr->hDefaultFont);
10365 if (infoPtr->clrBk != CLR_NONE) DeleteObject(infoPtr->hBkBrush);
10367 SetWindowLongPtrW(infoPtr->hwndSelf, 0, 0);
10369 /* free listview info pointer*/
10370 Free(infoPtr);
10372 return 0;
10375 /***
10376 * DESCRIPTION:
10377 * Handles notifications.
10379 * PARAMETER(S):
10380 * [I] infoPtr : valid pointer to the listview structure
10381 * [I] lpnmhdr : notification information
10383 * RETURN:
10384 * Zero
10386 static LRESULT LISTVIEW_Notify(LISTVIEW_INFO *infoPtr, NMHDR *lpnmhdr)
10388 NMHEADERW *lpnmh;
10390 TRACE("(lpnmhdr=%p)\n", lpnmhdr);
10392 if (!lpnmhdr || lpnmhdr->hwndFrom != infoPtr->hwndHeader) return 0;
10394 /* remember: HDN_LAST < HDN_FIRST */
10395 if (lpnmhdr->code > HDN_FIRST || lpnmhdr->code < HDN_LAST) return 0;
10396 lpnmh = (NMHEADERW *)lpnmhdr;
10398 if (lpnmh->iItem < 0 || lpnmh->iItem >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return 0;
10400 switch (lpnmhdr->code)
10402 case HDN_TRACKW:
10403 case HDN_TRACKA:
10405 COLUMN_INFO *lpColumnInfo;
10406 POINT ptOrigin;
10407 INT x;
10409 if (!lpnmh->pitem || !(lpnmh->pitem->mask & HDI_WIDTH))
10410 break;
10412 /* remove the old line (if any) */
10413 LISTVIEW_DrawTrackLine(infoPtr);
10415 /* compute & draw the new line */
10416 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, lpnmh->iItem);
10417 x = lpColumnInfo->rcHeader.left + lpnmh->pitem->cxy;
10418 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
10419 infoPtr->xTrackLine = x + ptOrigin.x;
10420 LISTVIEW_DrawTrackLine(infoPtr);
10421 return notify_forward_header(infoPtr, lpnmh);
10424 case HDN_ENDTRACKA:
10425 case HDN_ENDTRACKW:
10426 /* remove the track line (if any) */
10427 LISTVIEW_DrawTrackLine(infoPtr);
10428 infoPtr->xTrackLine = -1;
10429 return notify_forward_header(infoPtr, lpnmh);
10431 case HDN_BEGINDRAG:
10432 if ((infoPtr->dwLvExStyle & LVS_EX_HEADERDRAGDROP) == 0) return 1;
10433 return notify_forward_header(infoPtr, lpnmh);
10435 case HDN_ENDDRAG:
10436 infoPtr->colRectsDirty = TRUE;
10437 LISTVIEW_InvalidateList(infoPtr);
10438 return notify_forward_header(infoPtr, lpnmh);
10440 case HDN_ITEMCHANGEDW:
10441 case HDN_ITEMCHANGEDA:
10443 COLUMN_INFO *lpColumnInfo;
10444 HDITEMW hdi;
10445 INT dx, cxy;
10447 if (!lpnmh->pitem || !(lpnmh->pitem->mask & HDI_WIDTH))
10449 hdi.mask = HDI_WIDTH;
10450 if (!SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, lpnmh->iItem, (LPARAM)&hdi)) return 0;
10451 cxy = hdi.cxy;
10453 else
10454 cxy = lpnmh->pitem->cxy;
10456 /* determine how much we change since the last know position */
10457 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, lpnmh->iItem);
10458 dx = cxy - (lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left);
10459 if (dx != 0)
10461 lpColumnInfo->rcHeader.right += dx;
10463 hdi.mask = HDI_ORDER;
10464 SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, lpnmh->iItem, (LPARAM)&hdi);
10466 /* not the rightmost one */
10467 if (hdi.iOrder + 1 < DPA_GetPtrCount(infoPtr->hdpaColumns))
10469 INT nIndex = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX,
10470 hdi.iOrder + 1, 0);
10471 LISTVIEW_ScrollColumns(infoPtr, nIndex, dx);
10473 else
10475 /* only needs to update the scrolls */
10476 infoPtr->nItemWidth += dx;
10477 LISTVIEW_UpdateScroll(infoPtr);
10479 LISTVIEW_UpdateItemSize(infoPtr);
10480 if (infoPtr->uView == LV_VIEW_DETAILS && is_redrawing(infoPtr))
10482 POINT ptOrigin;
10483 RECT rcCol = lpColumnInfo->rcHeader;
10485 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
10486 OffsetRect(&rcCol, ptOrigin.x, 0);
10488 rcCol.top = infoPtr->rcList.top;
10489 rcCol.bottom = infoPtr->rcList.bottom;
10491 /* resizing left-aligned columns leaves most of the left side untouched */
10492 if ((lpColumnInfo->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_LEFT)
10494 INT nMaxDirty = infoPtr->nEllipsisWidth + infoPtr->ntmMaxCharWidth;
10495 if (dx > 0)
10496 nMaxDirty += dx;
10497 rcCol.left = max (rcCol.left, rcCol.right - nMaxDirty);
10500 /* when shrinking the last column clear the now unused field */
10501 if (hdi.iOrder == DPA_GetPtrCount(infoPtr->hdpaColumns) - 1)
10503 RECT right;
10505 rcCol.right -= dx;
10507 /* deal with right from rightmost column area */
10508 right.left = rcCol.right;
10509 right.top = rcCol.top;
10510 right.bottom = rcCol.bottom;
10511 right.right = infoPtr->rcList.right;
10513 LISTVIEW_InvalidateRect(infoPtr, &right);
10516 LISTVIEW_InvalidateRect(infoPtr, &rcCol);
10519 break;
10522 case HDN_ITEMCLICKW:
10523 case HDN_ITEMCLICKA:
10525 /* Handle sorting by Header Column */
10526 NMLISTVIEW nmlv;
10528 ZeroMemory(&nmlv, sizeof(NMLISTVIEW));
10529 nmlv.iItem = -1;
10530 nmlv.iSubItem = lpnmh->iItem;
10531 notify_listview(infoPtr, LVN_COLUMNCLICK, &nmlv);
10532 return notify_forward_header(infoPtr, lpnmh);
10535 case HDN_DIVIDERDBLCLICKW:
10536 case HDN_DIVIDERDBLCLICKA:
10537 /* FIXME: for LVS_EX_HEADERINALLVIEWS and not LV_VIEW_DETAILS
10538 we should use LVSCW_AUTOSIZE_USEHEADER, helper rework or
10539 split needed for that */
10540 LISTVIEW_SetColumnWidth(infoPtr, lpnmh->iItem, LVSCW_AUTOSIZE);
10541 return notify_forward_header(infoPtr, lpnmh);
10543 return 0;
10546 /***
10547 * DESCRIPTION:
10548 * Paint non-client area of control.
10550 * PARAMETER(S):
10551 * [I] infoPtr : valid pointer to the listview structureof the sender
10552 * [I] region : update region
10554 * RETURN:
10555 * TRUE - frame was painted
10556 * FALSE - call default window proc
10558 static BOOL LISTVIEW_NCPaint(const LISTVIEW_INFO *infoPtr, HRGN region)
10560 HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
10561 HDC dc;
10562 RECT r;
10563 HRGN cliprgn;
10564 int cxEdge = GetSystemMetrics (SM_CXEDGE),
10565 cyEdge = GetSystemMetrics (SM_CYEDGE);
10567 if (!theme)
10568 return DefWindowProcW (infoPtr->hwndSelf, WM_NCPAINT, (WPARAM)region, 0);
10570 GetWindowRect(infoPtr->hwndSelf, &r);
10572 cliprgn = CreateRectRgn (r.left + cxEdge, r.top + cyEdge,
10573 r.right - cxEdge, r.bottom - cyEdge);
10574 if (region != (HRGN)1)
10575 CombineRgn (cliprgn, cliprgn, region, RGN_AND);
10576 OffsetRect(&r, -r.left, -r.top);
10578 dc = GetDCEx(infoPtr->hwndSelf, region, DCX_WINDOW|DCX_INTERSECTRGN);
10579 OffsetRect(&r, -r.left, -r.top);
10581 if (IsThemeBackgroundPartiallyTransparent (theme, 0, 0))
10582 DrawThemeParentBackground(infoPtr->hwndSelf, dc, &r);
10583 DrawThemeBackground (theme, dc, 0, 0, &r, 0);
10584 ReleaseDC(infoPtr->hwndSelf, dc);
10586 /* Call default proc to get the scrollbars etc. painted */
10587 DefWindowProcW (infoPtr->hwndSelf, WM_NCPAINT, (WPARAM)cliprgn, 0);
10589 return FALSE;
10592 /***
10593 * DESCRIPTION:
10594 * Determines the type of structure to use.
10596 * PARAMETER(S):
10597 * [I] infoPtr : valid pointer to the listview structureof the sender
10598 * [I] hwndFrom : listview window handle
10599 * [I] nCommand : command specifying the nature of the WM_NOTIFYFORMAT
10601 * RETURN:
10602 * Zero
10604 static LRESULT LISTVIEW_NotifyFormat(LISTVIEW_INFO *infoPtr, HWND hwndFrom, INT nCommand)
10606 TRACE("(hwndFrom=%p, nCommand=%d)\n", hwndFrom, nCommand);
10608 if (nCommand == NF_REQUERY)
10609 infoPtr->notifyFormat = SendMessageW(infoPtr->hwndNotify, WM_NOTIFYFORMAT, (WPARAM)infoPtr->hwndSelf, NF_QUERY);
10611 return infoPtr->notifyFormat;
10614 /***
10615 * DESCRIPTION:
10616 * Paints/Repaints the listview control. Internal use.
10618 * PARAMETER(S):
10619 * [I] infoPtr : valid pointer to the listview structure
10620 * [I] hdc : device context handle
10622 * RETURN:
10623 * Zero
10625 static LRESULT LISTVIEW_Paint(LISTVIEW_INFO *infoPtr, HDC hdc)
10627 TRACE("(hdc=%p)\n", hdc);
10629 if (infoPtr->bNoItemMetrics && infoPtr->nItemCount)
10631 infoPtr->bNoItemMetrics = FALSE;
10632 LISTVIEW_UpdateItemSize(infoPtr);
10633 if (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON)
10634 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
10635 LISTVIEW_UpdateScroll(infoPtr);
10638 if (infoPtr->hwndHeader) UpdateWindow(infoPtr->hwndHeader);
10640 if (hdc)
10641 LISTVIEW_Refresh(infoPtr, hdc, NULL);
10642 else
10644 PAINTSTRUCT ps;
10646 hdc = BeginPaint(infoPtr->hwndSelf, &ps);
10647 if (!hdc) return 1;
10648 LISTVIEW_Refresh(infoPtr, hdc, ps.fErase ? &ps.rcPaint : NULL);
10649 EndPaint(infoPtr->hwndSelf, &ps);
10652 return 0;
10655 /***
10656 * DESCRIPTION:
10657 * Paints/Repaints the listview control, WM_PAINT handler.
10659 * PARAMETER(S):
10660 * [I] infoPtr : valid pointer to the listview structure
10661 * [I] hdc : device context handle
10663 * RETURN:
10664 * Zero
10666 static inline LRESULT LISTVIEW_WMPaint(LISTVIEW_INFO *infoPtr, HDC hdc)
10668 TRACE("(hdc=%p)\n", hdc);
10670 if (!is_redrawing(infoPtr))
10671 return DefWindowProcW (infoPtr->hwndSelf, WM_PAINT, (WPARAM)hdc, 0);
10673 return LISTVIEW_Paint(infoPtr, hdc);
10676 /***
10677 * DESCRIPTION:
10678 * Paints/Repaints the listview control.
10680 * PARAMETER(S):
10681 * [I] infoPtr : valid pointer to the listview structure
10682 * [I] hdc : device context handle
10683 * [I] options : drawing options
10685 * RETURN:
10686 * Zero
10688 static LRESULT LISTVIEW_PrintClient(LISTVIEW_INFO *infoPtr, HDC hdc, DWORD options)
10690 FIXME("Partial Stub: (hdc=%p options=0x%08x)\n", hdc, options);
10692 if ((options & PRF_CHECKVISIBLE) && !IsWindowVisible(infoPtr->hwndSelf))
10693 return 0;
10695 if (options & PRF_ERASEBKGND)
10696 LISTVIEW_EraseBkgnd(infoPtr, hdc);
10698 if (options & PRF_CLIENT)
10699 LISTVIEW_Paint(infoPtr, hdc);
10701 return 0;
10705 /***
10706 * DESCRIPTION:
10707 * Processes double click messages (right mouse button).
10709 * PARAMETER(S):
10710 * [I] infoPtr : valid pointer to the listview structure
10711 * [I] wKey : key flag
10712 * [I] x,y : mouse coordinate
10714 * RETURN:
10715 * Zero
10717 static LRESULT LISTVIEW_RButtonDblClk(const LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
10719 LVHITTESTINFO lvHitTestInfo;
10721 TRACE("(key=%hu,X=%u,Y=%u)\n", wKey, x, y);
10723 /* send NM_RELEASEDCAPTURE notification */
10724 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
10726 /* send NM_RDBLCLK notification */
10727 lvHitTestInfo.pt.x = x;
10728 lvHitTestInfo.pt.y = y;
10729 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
10730 notify_click(infoPtr, NM_RDBLCLK, &lvHitTestInfo);
10732 return 0;
10735 /***
10736 * DESCRIPTION:
10737 * Processes WM_RBUTTONDOWN message and corresponding drag operation.
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_RButtonDown(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
10749 LVHITTESTINFO ht;
10750 INT item;
10752 TRACE("(key=%hu, x=%d, y=%d)\n", wKey, x, y);
10754 /* send NM_RELEASEDCAPTURE notification */
10755 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
10757 /* determine the index of the selected item */
10758 ht.pt.x = x;
10759 ht.pt.y = y;
10760 item = LISTVIEW_HitTest(infoPtr, &ht, TRUE, TRUE);
10762 /* make sure the listview control window has the focus */
10763 if (!infoPtr->bFocus) SetFocus(infoPtr->hwndSelf);
10765 if ((item >= 0) && (item < infoPtr->nItemCount))
10767 LISTVIEW_SetItemFocus(infoPtr, item);
10768 if (!((wKey & MK_SHIFT) || (wKey & MK_CONTROL)) &&
10769 !LISTVIEW_GetItemState(infoPtr, item, LVIS_SELECTED))
10770 LISTVIEW_SetSelection(infoPtr, item);
10772 else
10773 LISTVIEW_DeselectAll(infoPtr);
10775 if (LISTVIEW_TrackMouse(infoPtr, ht.pt))
10777 if (ht.iItem != -1)
10779 NMLISTVIEW nmlv;
10781 memset(&nmlv, 0, sizeof(nmlv));
10782 nmlv.iItem = ht.iItem;
10783 nmlv.ptAction = ht.pt;
10785 notify_listview(infoPtr, LVN_BEGINRDRAG, &nmlv);
10788 else
10790 SetFocus(infoPtr->hwndSelf);
10792 ht.pt.x = x;
10793 ht.pt.y = y;
10794 LISTVIEW_HitTest(infoPtr, &ht, TRUE, FALSE);
10796 if (notify_click(infoPtr, NM_RCLICK, &ht))
10798 /* Send a WM_CONTEXTMENU message in response to the WM_RBUTTONUP */
10799 SendMessageW(infoPtr->hwndSelf, WM_CONTEXTMENU,
10800 (WPARAM)infoPtr->hwndSelf, (LPARAM)GetMessagePos());
10804 return 0;
10807 /***
10808 * DESCRIPTION:
10809 * Sets the cursor.
10811 * PARAMETER(S):
10812 * [I] infoPtr : valid pointer to the listview structure
10813 * [I] hwnd : window handle of window containing the cursor
10814 * [I] nHittest : hit-test code
10815 * [I] wMouseMsg : ideintifier of the mouse message
10817 * RETURN:
10818 * TRUE if cursor is set
10819 * FALSE otherwise
10821 static BOOL LISTVIEW_SetCursor(const LISTVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
10823 LVHITTESTINFO lvHitTestInfo;
10825 if (!LISTVIEW_IsHotTracking(infoPtr)) goto forward;
10827 if (!infoPtr->hHotCursor) goto forward;
10829 GetCursorPos(&lvHitTestInfo.pt);
10830 if (LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, FALSE, FALSE) < 0) goto forward;
10832 SetCursor(infoPtr->hHotCursor);
10834 return TRUE;
10836 forward:
10838 return DefWindowProcW(infoPtr->hwndSelf, WM_SETCURSOR, wParam, lParam);
10841 /***
10842 * DESCRIPTION:
10843 * Sets the focus.
10845 * PARAMETER(S):
10846 * [I] infoPtr : valid pointer to the listview structure
10847 * [I] hwndLoseFocus : handle of previously focused window
10849 * RETURN:
10850 * Zero
10852 static LRESULT LISTVIEW_SetFocus(LISTVIEW_INFO *infoPtr, HWND hwndLoseFocus)
10854 TRACE("(hwndLoseFocus=%p)\n", hwndLoseFocus);
10856 /* if we have the focus already, there's nothing to do */
10857 if (infoPtr->bFocus) return 0;
10859 /* send NM_SETFOCUS notification */
10860 if (!notify(infoPtr, NM_SETFOCUS)) return 0;
10862 /* set window focus flag */
10863 infoPtr->bFocus = TRUE;
10865 /* put the focus rect back on */
10866 LISTVIEW_ShowFocusRect(infoPtr, TRUE);
10868 /* redraw all visible selected items */
10869 LISTVIEW_InvalidateSelectedItems(infoPtr);
10871 return 0;
10874 /***
10875 * DESCRIPTION:
10876 * Sets the font.
10878 * PARAMETER(S):
10879 * [I] infoPtr : valid pointer to the listview structure
10880 * [I] fRedraw : font handle
10881 * [I] fRedraw : redraw flag
10883 * RETURN:
10884 * Zero
10886 static LRESULT LISTVIEW_SetFont(LISTVIEW_INFO *infoPtr, HFONT hFont, WORD fRedraw)
10888 HFONT oldFont = infoPtr->hFont;
10889 INT oldHeight = infoPtr->nItemHeight;
10891 TRACE("(hfont=%p,redraw=%hu)\n", hFont, fRedraw);
10893 infoPtr->hFont = hFont ? hFont : infoPtr->hDefaultFont;
10894 if (infoPtr->hFont == oldFont) return 0;
10896 LISTVIEW_SaveTextMetrics(infoPtr);
10898 infoPtr->nItemHeight = LISTVIEW_CalculateItemHeight(infoPtr);
10900 if (infoPtr->uView == LV_VIEW_DETAILS)
10902 SendMessageW(infoPtr->hwndHeader, WM_SETFONT, (WPARAM)hFont, MAKELPARAM(fRedraw, 0));
10903 LISTVIEW_UpdateSize(infoPtr);
10904 LISTVIEW_UpdateScroll(infoPtr);
10906 else if (infoPtr->nItemHeight != oldHeight)
10907 LISTVIEW_UpdateScroll(infoPtr);
10909 if (fRedraw) LISTVIEW_InvalidateList(infoPtr);
10911 return 0;
10914 /***
10915 * DESCRIPTION:
10916 * Message handling for WM_SETREDRAW.
10917 * For the Listview, it invalidates the entire window (the doc specifies otherwise)
10919 * PARAMETER(S):
10920 * [I] infoPtr : valid pointer to the listview structure
10921 * [I] bRedraw: state of redraw flag
10923 * RETURN:
10924 * DefWinProc return value
10926 static LRESULT LISTVIEW_SetRedraw(LISTVIEW_INFO *infoPtr, BOOL bRedraw)
10928 TRACE("infoPtr->bRedraw=%d, bRedraw=%d\n", infoPtr->bRedraw, bRedraw);
10930 /* we cannot use straight equality here because _any_ non-zero value is TRUE */
10931 if ((infoPtr->bRedraw && bRedraw) || (!infoPtr->bRedraw && !bRedraw)) return 0;
10933 infoPtr->bRedraw = bRedraw;
10935 if(!bRedraw) return 0;
10937 if (is_autoarrange(infoPtr))
10938 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
10939 LISTVIEW_UpdateScroll(infoPtr);
10941 /* despite what the WM_SETREDRAW docs says, apps expect us
10942 * to invalidate the listview here... stupid! */
10943 LISTVIEW_InvalidateList(infoPtr);
10945 return 0;
10948 /***
10949 * DESCRIPTION:
10950 * Resizes the listview control. This function processes WM_SIZE
10951 * messages. At this time, the width and height are not used.
10953 * PARAMETER(S):
10954 * [I] infoPtr : valid pointer to the listview structure
10955 * [I] Width : new width
10956 * [I] Height : new height
10958 * RETURN:
10959 * Zero
10961 static LRESULT LISTVIEW_Size(LISTVIEW_INFO *infoPtr, int Width, int Height)
10963 RECT rcOld = infoPtr->rcList;
10965 TRACE("(width=%d, height=%d)\n", Width, Height);
10967 LISTVIEW_UpdateSize(infoPtr);
10968 if (EqualRect(&rcOld, &infoPtr->rcList)) return 0;
10970 /* do not bother with display related stuff if we're not redrawing */
10971 if (!is_redrawing(infoPtr)) return 0;
10973 if (is_autoarrange(infoPtr))
10974 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
10976 LISTVIEW_UpdateScroll(infoPtr);
10978 /* refresh all only for lists whose height changed significantly */
10979 if ((infoPtr->uView == LV_VIEW_LIST) &&
10980 (rcOld.bottom - rcOld.top) / infoPtr->nItemHeight !=
10981 (infoPtr->rcList.bottom - infoPtr->rcList.top) / infoPtr->nItemHeight)
10982 LISTVIEW_InvalidateList(infoPtr);
10984 return 0;
10987 /***
10988 * DESCRIPTION:
10989 * Sets the size information.
10991 * PARAMETER(S):
10992 * [I] infoPtr : valid pointer to the listview structure
10994 * RETURN:
10995 * None
10997 static void LISTVIEW_UpdateSize(LISTVIEW_INFO *infoPtr)
10999 TRACE("uView=%d, rcList(old)=%s\n", infoPtr->uView, wine_dbgstr_rect(&infoPtr->rcList));
11001 GetClientRect(infoPtr->hwndSelf, &infoPtr->rcList);
11003 if (infoPtr->uView == LV_VIEW_LIST)
11005 /* Apparently the "LIST" style is supposed to have the same
11006 * number of items in a column even if there is no scroll bar.
11007 * Since if a scroll bar already exists then the bottom is already
11008 * reduced, only reduce if the scroll bar does not currently exist.
11009 * The "2" is there to mimic the native control. I think it may be
11010 * related to either padding or edges. (GLA 7/2002)
11012 if (!(GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE) & WS_HSCROLL))
11013 infoPtr->rcList.bottom -= GetSystemMetrics(SM_CYHSCROLL);
11014 infoPtr->rcList.bottom = max (infoPtr->rcList.bottom - 2, 0);
11017 /* if control created invisible header isn't created */
11018 if (infoPtr->hwndHeader)
11020 HDLAYOUT hl;
11021 WINDOWPOS wp;
11023 hl.prc = &infoPtr->rcList;
11024 hl.pwpos = &wp;
11025 SendMessageW( infoPtr->hwndHeader, HDM_LAYOUT, 0, (LPARAM)&hl );
11026 TRACE(" wp.flags=0x%08x, wp=%d,%d (%dx%d)\n", wp.flags, wp.x, wp.y, wp.cx, wp.cy);
11028 if (LISTVIEW_IsHeaderEnabled(infoPtr))
11029 wp.flags |= SWP_SHOWWINDOW;
11030 else
11032 wp.flags |= SWP_HIDEWINDOW;
11033 wp.cy = 0;
11036 SetWindowPos(wp.hwnd, wp.hwndInsertAfter, wp.x, wp.y, wp.cx, wp.cy, wp.flags);
11037 TRACE(" after SWP wp=%d,%d (%dx%d)\n", wp.x, wp.y, wp.cx, wp.cy);
11039 infoPtr->rcList.top = max(wp.cy, 0);
11041 /* extra padding for grid */
11042 if (infoPtr->uView == LV_VIEW_DETAILS && infoPtr->dwLvExStyle & LVS_EX_GRIDLINES)
11043 infoPtr->rcList.top += 2;
11045 TRACE(" rcList=%s\n", wine_dbgstr_rect(&infoPtr->rcList));
11048 /***
11049 * DESCRIPTION:
11050 * Processes WM_STYLECHANGED messages.
11052 * PARAMETER(S):
11053 * [I] infoPtr : valid pointer to the listview structure
11054 * [I] wStyleType : window style type (normal or extended)
11055 * [I] lpss : window style information
11057 * RETURN:
11058 * Zero
11060 static INT LISTVIEW_StyleChanged(LISTVIEW_INFO *infoPtr, WPARAM wStyleType,
11061 const STYLESTRUCT *lpss)
11063 UINT uNewView = lpss->styleNew & LVS_TYPEMASK;
11064 UINT uOldView = lpss->styleOld & LVS_TYPEMASK;
11065 UINT style;
11067 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
11068 wStyleType, lpss->styleOld, lpss->styleNew);
11070 if (wStyleType != GWL_STYLE) return 0;
11072 infoPtr->dwStyle = lpss->styleNew;
11073 map_style_view(infoPtr);
11075 if (((lpss->styleOld & WS_HSCROLL) != 0)&&
11076 ((lpss->styleNew & WS_HSCROLL) == 0))
11077 ShowScrollBar(infoPtr->hwndSelf, SB_HORZ, FALSE);
11079 if (((lpss->styleOld & WS_VSCROLL) != 0)&&
11080 ((lpss->styleNew & WS_VSCROLL) == 0))
11081 ShowScrollBar(infoPtr->hwndSelf, SB_VERT, FALSE);
11083 if (uNewView != uOldView)
11085 HIMAGELIST himl;
11087 SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
11088 ShowWindow(infoPtr->hwndHeader, SW_HIDE);
11090 ShowScrollBar(infoPtr->hwndSelf, SB_BOTH, FALSE);
11091 SetRectEmpty(&infoPtr->rcFocus);
11093 himl = (uNewView == LVS_ICON ? infoPtr->himlNormal : infoPtr->himlSmall);
11094 set_icon_size(&infoPtr->iconSize, himl, uNewView != LVS_ICON);
11096 if (uNewView == LVS_REPORT)
11098 HDLAYOUT hl;
11099 WINDOWPOS wp;
11101 LISTVIEW_CreateHeader( infoPtr );
11103 hl.prc = &infoPtr->rcList;
11104 hl.pwpos = &wp;
11105 SendMessageW( infoPtr->hwndHeader, HDM_LAYOUT, 0, (LPARAM)&hl );
11106 SetWindowPos(infoPtr->hwndHeader, infoPtr->hwndSelf, wp.x, wp.y, wp.cx, wp.cy,
11107 wp.flags | ((infoPtr->dwStyle & LVS_NOCOLUMNHEADER)
11108 ? SWP_HIDEWINDOW : SWP_SHOWWINDOW));
11111 LISTVIEW_UpdateItemSize(infoPtr);
11114 if (uNewView == LVS_REPORT || infoPtr->dwLvExStyle & LVS_EX_HEADERINALLVIEWS)
11116 if ((lpss->styleOld ^ lpss->styleNew) & LVS_NOCOLUMNHEADER)
11118 if (lpss->styleNew & LVS_NOCOLUMNHEADER)
11120 /* Turn off the header control */
11121 style = GetWindowLongW(infoPtr->hwndHeader, GWL_STYLE);
11122 TRACE("Hide header control, was 0x%08x\n", style);
11123 SetWindowLongW(infoPtr->hwndHeader, GWL_STYLE, style | HDS_HIDDEN);
11124 } else {
11125 /* Turn on the header control */
11126 if ((style = GetWindowLongW(infoPtr->hwndHeader, GWL_STYLE)) & HDS_HIDDEN)
11128 TRACE("Show header control, was 0x%08x\n", style);
11129 SetWindowLongW(infoPtr->hwndHeader, GWL_STYLE, (style & ~HDS_HIDDEN) | WS_VISIBLE);
11135 if ( (uNewView == LVS_ICON || uNewView == LVS_SMALLICON) &&
11136 (uNewView != uOldView || ((lpss->styleNew ^ lpss->styleOld) & LVS_ALIGNMASK)) )
11137 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
11139 /* update the size of the client area */
11140 LISTVIEW_UpdateSize(infoPtr);
11142 /* add scrollbars if needed */
11143 LISTVIEW_UpdateScroll(infoPtr);
11145 /* invalidate client area + erase background */
11146 LISTVIEW_InvalidateList(infoPtr);
11148 return 0;
11151 /***
11152 * DESCRIPTION:
11153 * Processes WM_STYLECHANGING messages.
11155 * PARAMETER(S):
11156 * [I] wStyleType : window style type (normal or extended)
11157 * [I0] lpss : window style information
11159 * RETURN:
11160 * Zero
11162 static INT LISTVIEW_StyleChanging(WPARAM wStyleType,
11163 STYLESTRUCT *lpss)
11165 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
11166 wStyleType, lpss->styleOld, lpss->styleNew);
11168 /* don't forward LVS_OWNERDATA only if not already set to */
11169 if ((lpss->styleNew ^ lpss->styleOld) & LVS_OWNERDATA)
11171 if (lpss->styleOld & LVS_OWNERDATA)
11172 lpss->styleNew |= LVS_OWNERDATA;
11173 else
11174 lpss->styleNew &= ~LVS_OWNERDATA;
11177 return 0;
11180 /***
11181 * DESCRIPTION:
11182 * Processes WM_SHOWWINDOW messages.
11184 * PARAMETER(S):
11185 * [I] infoPtr : valid pointer to the listview structure
11186 * [I] bShown : window is being shown (FALSE when hidden)
11187 * [I] iStatus : window show status
11189 * RETURN:
11190 * Zero
11192 static LRESULT LISTVIEW_ShowWindow(LISTVIEW_INFO *infoPtr, WPARAM bShown, LPARAM iStatus)
11194 /* header delayed creation */
11195 if ((infoPtr->uView == LV_VIEW_DETAILS) && bShown)
11197 LISTVIEW_CreateHeader(infoPtr);
11199 if (!(LVS_NOCOLUMNHEADER & infoPtr->dwStyle))
11200 ShowWindow(infoPtr->hwndHeader, SW_SHOWNORMAL);
11203 return DefWindowProcW(infoPtr->hwndSelf, WM_SHOWWINDOW, bShown, iStatus);
11206 /***
11207 * DESCRIPTION:
11208 * Processes CCM_GETVERSION messages.
11210 * PARAMETER(S):
11211 * [I] infoPtr : valid pointer to the listview structure
11213 * RETURN:
11214 * Current version
11216 static inline LRESULT LISTVIEW_GetVersion(const LISTVIEW_INFO *infoPtr)
11218 return infoPtr->iVersion;
11221 /***
11222 * DESCRIPTION:
11223 * Processes CCM_SETVERSION messages.
11225 * PARAMETER(S):
11226 * [I] infoPtr : valid pointer to the listview structure
11227 * [I] iVersion : version to be set
11229 * RETURN:
11230 * -1 when requested version is greater than DLL version;
11231 * previous version otherwise
11233 static LRESULT LISTVIEW_SetVersion(LISTVIEW_INFO *infoPtr, DWORD iVersion)
11235 INT iOldVersion = infoPtr->iVersion;
11237 if (iVersion > COMCTL32_VERSION)
11238 return -1;
11240 infoPtr->iVersion = iVersion;
11242 TRACE("new version %d\n", iVersion);
11244 return iOldVersion;
11247 /***
11248 * DESCRIPTION:
11249 * Window procedure of the listview control.
11252 static LRESULT WINAPI
11253 LISTVIEW_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
11255 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongPtrW(hwnd, 0);
11257 TRACE("(hwnd=%p uMsg=%x wParam=%lx lParam=%lx)\n", hwnd, uMsg, wParam, lParam);
11259 if (!infoPtr && (uMsg != WM_NCCREATE))
11260 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
11262 switch (uMsg)
11264 case LVM_APPROXIMATEVIEWRECT:
11265 return LISTVIEW_ApproximateViewRect(infoPtr, (INT)wParam,
11266 LOWORD(lParam), HIWORD(lParam));
11267 case LVM_ARRANGE:
11268 return LISTVIEW_Arrange(infoPtr, (INT)wParam);
11270 case LVM_CANCELEDITLABEL:
11271 return LISTVIEW_CancelEditLabel(infoPtr);
11273 case LVM_CREATEDRAGIMAGE:
11274 return (LRESULT)LISTVIEW_CreateDragImage(infoPtr, (INT)wParam, (LPPOINT)lParam);
11276 case LVM_DELETEALLITEMS:
11277 return LISTVIEW_DeleteAllItems(infoPtr, FALSE);
11279 case LVM_DELETECOLUMN:
11280 return LISTVIEW_DeleteColumn(infoPtr, (INT)wParam);
11282 case LVM_DELETEITEM:
11283 return LISTVIEW_DeleteItem(infoPtr, (INT)wParam);
11285 case LVM_EDITLABELA:
11286 case LVM_EDITLABELW:
11287 return (LRESULT)LISTVIEW_EditLabelT(infoPtr, (INT)wParam,
11288 uMsg == LVM_EDITLABELW);
11289 /* case LVM_ENABLEGROUPVIEW: */
11291 case LVM_ENSUREVISIBLE:
11292 return LISTVIEW_EnsureVisible(infoPtr, (INT)wParam, (BOOL)lParam);
11294 case LVM_FINDITEMW:
11295 return LISTVIEW_FindItemW(infoPtr, (INT)wParam, (LPLVFINDINFOW)lParam);
11297 case LVM_FINDITEMA:
11298 return LISTVIEW_FindItemA(infoPtr, (INT)wParam, (LPLVFINDINFOA)lParam);
11300 case LVM_GETBKCOLOR:
11301 return infoPtr->clrBk;
11303 /* case LVM_GETBKIMAGE: */
11305 case LVM_GETCALLBACKMASK:
11306 return infoPtr->uCallbackMask;
11308 case LVM_GETCOLUMNA:
11309 case LVM_GETCOLUMNW:
11310 return LISTVIEW_GetColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam,
11311 uMsg == LVM_GETCOLUMNW);
11313 case LVM_GETCOLUMNORDERARRAY:
11314 return LISTVIEW_GetColumnOrderArray(infoPtr, (INT)wParam, (LPINT)lParam);
11316 case LVM_GETCOLUMNWIDTH:
11317 return LISTVIEW_GetColumnWidth(infoPtr, (INT)wParam);
11319 case LVM_GETCOUNTPERPAGE:
11320 return LISTVIEW_GetCountPerPage(infoPtr);
11322 case LVM_GETEDITCONTROL:
11323 return (LRESULT)infoPtr->hwndEdit;
11325 case LVM_GETEXTENDEDLISTVIEWSTYLE:
11326 return infoPtr->dwLvExStyle;
11328 /* case LVM_GETGROUPINFO: */
11330 /* case LVM_GETGROUPMETRICS: */
11332 case LVM_GETHEADER:
11333 return (LRESULT)infoPtr->hwndHeader;
11335 case LVM_GETHOTCURSOR:
11336 return (LRESULT)infoPtr->hHotCursor;
11338 case LVM_GETHOTITEM:
11339 return infoPtr->nHotItem;
11341 case LVM_GETHOVERTIME:
11342 return infoPtr->dwHoverTime;
11344 case LVM_GETIMAGELIST:
11345 return (LRESULT)LISTVIEW_GetImageList(infoPtr, (INT)wParam);
11347 /* case LVM_GETINSERTMARK: */
11349 /* case LVM_GETINSERTMARKCOLOR: */
11351 /* case LVM_GETINSERTMARKRECT: */
11353 case LVM_GETISEARCHSTRINGA:
11354 case LVM_GETISEARCHSTRINGW:
11355 FIXME("LVM_GETISEARCHSTRING: unimplemented\n");
11356 return FALSE;
11358 case LVM_GETITEMA:
11359 case LVM_GETITEMW:
11360 return LISTVIEW_GetItemExtT(infoPtr, (LPLVITEMW)lParam, uMsg == LVM_GETITEMW);
11362 case LVM_GETITEMCOUNT:
11363 return infoPtr->nItemCount;
11365 case LVM_GETITEMPOSITION:
11366 return LISTVIEW_GetItemPosition(infoPtr, (INT)wParam, (LPPOINT)lParam);
11368 case LVM_GETITEMRECT:
11369 return LISTVIEW_GetItemRect(infoPtr, (INT)wParam, (LPRECT)lParam);
11371 case LVM_GETITEMSPACING:
11372 return LISTVIEW_GetItemSpacing(infoPtr, (BOOL)wParam);
11374 case LVM_GETITEMSTATE:
11375 return LISTVIEW_GetItemState(infoPtr, (INT)wParam, (UINT)lParam);
11377 case LVM_GETITEMTEXTA:
11378 case LVM_GETITEMTEXTW:
11379 return LISTVIEW_GetItemTextT(infoPtr, (INT)wParam, (LPLVITEMW)lParam,
11380 uMsg == LVM_GETITEMTEXTW);
11382 case LVM_GETNEXTITEM:
11383 return LISTVIEW_GetNextItem(infoPtr, (INT)wParam, LOWORD(lParam));
11385 case LVM_GETNUMBEROFWORKAREAS:
11386 FIXME("LVM_GETNUMBEROFWORKAREAS: unimplemented\n");
11387 return 1;
11389 case LVM_GETORIGIN:
11390 if (!lParam) return FALSE;
11391 if (infoPtr->uView == LV_VIEW_DETAILS ||
11392 infoPtr->uView == LV_VIEW_LIST) return FALSE;
11393 LISTVIEW_GetOrigin(infoPtr, (LPPOINT)lParam);
11394 return TRUE;
11396 /* case LVM_GETOUTLINECOLOR: */
11398 /* case LVM_GETSELECTEDCOLUMN: */
11400 case LVM_GETSELECTEDCOUNT:
11401 return LISTVIEW_GetSelectedCount(infoPtr);
11403 case LVM_GETSELECTIONMARK:
11404 return infoPtr->nSelectionMark;
11406 case LVM_GETSTRINGWIDTHA:
11407 case LVM_GETSTRINGWIDTHW:
11408 return LISTVIEW_GetStringWidthT(infoPtr, (LPCWSTR)lParam,
11409 uMsg == LVM_GETSTRINGWIDTHW);
11411 case LVM_GETSUBITEMRECT:
11412 return LISTVIEW_GetSubItemRect(infoPtr, (UINT)wParam, (LPRECT)lParam);
11414 case LVM_GETTEXTBKCOLOR:
11415 return infoPtr->clrTextBk;
11417 case LVM_GETTEXTCOLOR:
11418 return infoPtr->clrText;
11420 /* case LVM_GETTILEINFO: */
11422 /* case LVM_GETTILEVIEWINFO: */
11424 case LVM_GETTOOLTIPS:
11425 if( !infoPtr->hwndToolTip )
11426 infoPtr->hwndToolTip = COMCTL32_CreateToolTip( hwnd );
11427 return (LRESULT)infoPtr->hwndToolTip;
11429 case LVM_GETTOPINDEX:
11430 return LISTVIEW_GetTopIndex(infoPtr);
11432 case LVM_GETUNICODEFORMAT:
11433 return (infoPtr->notifyFormat == NFR_UNICODE);
11435 case LVM_GETVIEW:
11436 return infoPtr->uView;
11438 case LVM_GETVIEWRECT:
11439 return LISTVIEW_GetViewRect(infoPtr, (LPRECT)lParam);
11441 case LVM_GETWORKAREAS:
11442 FIXME("LVM_GETWORKAREAS: unimplemented\n");
11443 return FALSE;
11445 /* case LVM_HASGROUP: */
11447 case LVM_HITTEST:
11448 return LISTVIEW_HitTest(infoPtr, (LPLVHITTESTINFO)lParam, FALSE, TRUE);
11450 case LVM_INSERTCOLUMNA:
11451 case LVM_INSERTCOLUMNW:
11452 return LISTVIEW_InsertColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam,
11453 uMsg == LVM_INSERTCOLUMNW);
11455 /* case LVM_INSERTGROUP: */
11457 /* case LVM_INSERTGROUPSORTED: */
11459 case LVM_INSERTITEMA:
11460 case LVM_INSERTITEMW:
11461 return LISTVIEW_InsertItemT(infoPtr, (LPLVITEMW)lParam, uMsg == LVM_INSERTITEMW);
11463 /* case LVM_INSERTMARKHITTEST: */
11465 /* case LVM_ISGROUPVIEWENABLED: */
11467 case LVM_ISITEMVISIBLE:
11468 return LISTVIEW_IsItemVisible(infoPtr, (INT)wParam);
11470 case LVM_MAPIDTOINDEX:
11471 return LISTVIEW_MapIdToIndex(infoPtr, (UINT)wParam);
11473 case LVM_MAPINDEXTOID:
11474 return LISTVIEW_MapIndexToId(infoPtr, (INT)wParam);
11476 /* case LVM_MOVEGROUP: */
11478 /* case LVM_MOVEITEMTOGROUP: */
11480 case LVM_REDRAWITEMS:
11481 return LISTVIEW_RedrawItems(infoPtr, (INT)wParam, (INT)lParam);
11483 /* case LVM_REMOVEALLGROUPS: */
11485 /* case LVM_REMOVEGROUP: */
11487 case LVM_SCROLL:
11488 return LISTVIEW_Scroll(infoPtr, (INT)wParam, (INT)lParam);
11490 case LVM_SETBKCOLOR:
11491 return LISTVIEW_SetBkColor(infoPtr, (COLORREF)lParam);
11493 /* case LVM_SETBKIMAGE: */
11495 case LVM_SETCALLBACKMASK:
11496 infoPtr->uCallbackMask = (UINT)wParam;
11497 return TRUE;
11499 case LVM_SETCOLUMNA:
11500 case LVM_SETCOLUMNW:
11501 return LISTVIEW_SetColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam,
11502 uMsg == LVM_SETCOLUMNW);
11504 case LVM_SETCOLUMNORDERARRAY:
11505 return LISTVIEW_SetColumnOrderArray(infoPtr, (INT)wParam, (LPINT)lParam);
11507 case LVM_SETCOLUMNWIDTH:
11508 return LISTVIEW_SetColumnWidth(infoPtr, (INT)wParam, (short)LOWORD(lParam));
11510 case LVM_SETEXTENDEDLISTVIEWSTYLE:
11511 return LISTVIEW_SetExtendedListViewStyle(infoPtr, (DWORD)wParam, (DWORD)lParam);
11513 /* case LVM_SETGROUPINFO: */
11515 /* case LVM_SETGROUPMETRICS: */
11517 case LVM_SETHOTCURSOR:
11518 return (LRESULT)LISTVIEW_SetHotCursor(infoPtr, (HCURSOR)lParam);
11520 case LVM_SETHOTITEM:
11521 return LISTVIEW_SetHotItem(infoPtr, (INT)wParam);
11523 case LVM_SETHOVERTIME:
11524 return LISTVIEW_SetHoverTime(infoPtr, (DWORD)lParam);
11526 case LVM_SETICONSPACING:
11527 if(lParam == -1)
11528 return LISTVIEW_SetIconSpacing(infoPtr, -1, -1);
11529 return LISTVIEW_SetIconSpacing(infoPtr, LOWORD(lParam), HIWORD(lParam));
11531 case LVM_SETIMAGELIST:
11532 return (LRESULT)LISTVIEW_SetImageList(infoPtr, (INT)wParam, (HIMAGELIST)lParam);
11534 /* case LVM_SETINFOTIP: */
11536 /* case LVM_SETINSERTMARK: */
11538 /* case LVM_SETINSERTMARKCOLOR: */
11540 case LVM_SETITEMA:
11541 case LVM_SETITEMW:
11543 if (infoPtr->dwStyle & LVS_OWNERDATA) return FALSE;
11544 return LISTVIEW_SetItemT(infoPtr, (LPLVITEMW)lParam, (uMsg == LVM_SETITEMW));
11547 case LVM_SETITEMCOUNT:
11548 return LISTVIEW_SetItemCount(infoPtr, (INT)wParam, (DWORD)lParam);
11550 case LVM_SETITEMPOSITION:
11552 POINT pt;
11553 pt.x = (short)LOWORD(lParam);
11554 pt.y = (short)HIWORD(lParam);
11555 return LISTVIEW_SetItemPosition(infoPtr, (INT)wParam, &pt);
11558 case LVM_SETITEMPOSITION32:
11559 return LISTVIEW_SetItemPosition(infoPtr, (INT)wParam, (POINT*)lParam);
11561 case LVM_SETITEMSTATE:
11562 return LISTVIEW_SetItemState(infoPtr, (INT)wParam, (LPLVITEMW)lParam);
11564 case LVM_SETITEMTEXTA:
11565 case LVM_SETITEMTEXTW:
11566 return LISTVIEW_SetItemTextT(infoPtr, (INT)wParam, (LPLVITEMW)lParam,
11567 uMsg == LVM_SETITEMTEXTW);
11569 /* case LVM_SETOUTLINECOLOR: */
11571 /* case LVM_SETSELECTEDCOLUMN: */
11573 case LVM_SETSELECTIONMARK:
11574 return LISTVIEW_SetSelectionMark(infoPtr, (INT)lParam);
11576 case LVM_SETTEXTBKCOLOR:
11577 return LISTVIEW_SetTextBkColor(infoPtr, (COLORREF)lParam);
11579 case LVM_SETTEXTCOLOR:
11580 return LISTVIEW_SetTextColor(infoPtr, (COLORREF)lParam);
11582 /* case LVM_SETTILEINFO: */
11584 /* case LVM_SETTILEVIEWINFO: */
11586 /* case LVM_SETTILEWIDTH: */
11588 case LVM_SETTOOLTIPS:
11589 return (LRESULT)LISTVIEW_SetToolTips(infoPtr, (HWND)lParam);
11591 case LVM_SETUNICODEFORMAT:
11592 return LISTVIEW_SetUnicodeFormat(infoPtr, wParam);
11594 case LVM_SETVIEW:
11595 return LISTVIEW_SetView(infoPtr, wParam);
11597 /* case LVM_SETWORKAREAS: */
11599 /* case LVM_SORTGROUPS: */
11601 case LVM_SORTITEMS:
11602 case LVM_SORTITEMSEX:
11603 return LISTVIEW_SortItems(infoPtr, (PFNLVCOMPARE)lParam, wParam,
11604 uMsg == LVM_SORTITEMSEX);
11605 case LVM_SUBITEMHITTEST:
11606 return LISTVIEW_HitTest(infoPtr, (LPLVHITTESTINFO)lParam, TRUE, FALSE);
11608 case LVM_UPDATE:
11609 return LISTVIEW_Update(infoPtr, (INT)wParam);
11611 case CCM_GETVERSION:
11612 return LISTVIEW_GetVersion(infoPtr);
11614 case CCM_SETVERSION:
11615 return LISTVIEW_SetVersion(infoPtr, wParam);
11617 case WM_CHAR:
11618 return LISTVIEW_ProcessLetterKeys( infoPtr, wParam, lParam );
11620 case WM_COMMAND:
11621 return LISTVIEW_Command(infoPtr, wParam, lParam);
11623 case WM_NCCREATE:
11624 return LISTVIEW_NCCreate(hwnd, (LPCREATESTRUCTW)lParam);
11626 case WM_CREATE:
11627 return LISTVIEW_Create(hwnd, (LPCREATESTRUCTW)lParam);
11629 case WM_DESTROY:
11630 return LISTVIEW_Destroy(infoPtr);
11632 case WM_ENABLE:
11633 return LISTVIEW_Enable(infoPtr);
11635 case WM_ERASEBKGND:
11636 return LISTVIEW_EraseBkgnd(infoPtr, (HDC)wParam);
11638 case WM_GETDLGCODE:
11639 return DLGC_WANTCHARS | DLGC_WANTARROWS;
11641 case WM_GETFONT:
11642 return (LRESULT)infoPtr->hFont;
11644 case WM_HSCROLL:
11645 return LISTVIEW_HScroll(infoPtr, (INT)LOWORD(wParam), 0);
11647 case WM_KEYDOWN:
11648 return LISTVIEW_KeyDown(infoPtr, (INT)wParam, (LONG)lParam);
11650 case WM_KILLFOCUS:
11651 return LISTVIEW_KillFocus(infoPtr);
11653 case WM_LBUTTONDBLCLK:
11654 return LISTVIEW_LButtonDblClk(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11656 case WM_LBUTTONDOWN:
11657 return LISTVIEW_LButtonDown(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11659 case WM_LBUTTONUP:
11660 return LISTVIEW_LButtonUp(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11662 case WM_MOUSEMOVE:
11663 return LISTVIEW_MouseMove (infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11665 case WM_MOUSEHOVER:
11666 return LISTVIEW_MouseHover(infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11668 case WM_NCDESTROY:
11669 return LISTVIEW_NCDestroy(infoPtr);
11671 case WM_NCPAINT:
11672 return LISTVIEW_NCPaint(infoPtr, (HRGN)wParam);
11674 case WM_NOTIFY:
11675 return LISTVIEW_Notify(infoPtr, (LPNMHDR)lParam);
11677 case WM_NOTIFYFORMAT:
11678 return LISTVIEW_NotifyFormat(infoPtr, (HWND)wParam, (INT)lParam);
11680 case WM_PRINTCLIENT:
11681 return LISTVIEW_PrintClient(infoPtr, (HDC)wParam, (DWORD)lParam);
11683 case WM_PAINT:
11684 return LISTVIEW_WMPaint(infoPtr, (HDC)wParam);
11686 case WM_RBUTTONDBLCLK:
11687 return LISTVIEW_RButtonDblClk(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11689 case WM_RBUTTONDOWN:
11690 return LISTVIEW_RButtonDown(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11692 case WM_SETCURSOR:
11693 return LISTVIEW_SetCursor(infoPtr, wParam, lParam);
11695 case WM_SETFOCUS:
11696 return LISTVIEW_SetFocus(infoPtr, (HWND)wParam);
11698 case WM_SETFONT:
11699 return LISTVIEW_SetFont(infoPtr, (HFONT)wParam, (WORD)lParam);
11701 case WM_SETREDRAW:
11702 return LISTVIEW_SetRedraw(infoPtr, (BOOL)wParam);
11704 case WM_SHOWWINDOW:
11705 return LISTVIEW_ShowWindow(infoPtr, wParam, lParam);
11707 case WM_STYLECHANGED:
11708 return LISTVIEW_StyleChanged(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
11710 case WM_STYLECHANGING:
11711 return LISTVIEW_StyleChanging(wParam, (LPSTYLESTRUCT)lParam);
11713 case WM_SYSCOLORCHANGE:
11714 COMCTL32_RefreshSysColors();
11715 return 0;
11717 /* case WM_TIMER: */
11718 case WM_THEMECHANGED:
11719 return LISTVIEW_ThemeChanged(infoPtr);
11721 case WM_VSCROLL:
11722 return LISTVIEW_VScroll(infoPtr, (INT)LOWORD(wParam), 0);
11724 case WM_MOUSEWHEEL:
11725 if (wParam & (MK_SHIFT | MK_CONTROL))
11726 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
11727 return LISTVIEW_MouseWheel(infoPtr, (short int)HIWORD(wParam));
11729 case WM_WINDOWPOSCHANGED:
11730 if (!(((WINDOWPOS *)lParam)->flags & SWP_NOSIZE))
11732 SetWindowPos(infoPtr->hwndSelf, 0, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOACTIVATE |
11733 SWP_NOZORDER | SWP_NOMOVE | SWP_NOSIZE);
11735 if ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (infoPtr->uView == LV_VIEW_DETAILS))
11737 if (notify_measureitem(infoPtr)) LISTVIEW_InvalidateList(infoPtr);
11739 LISTVIEW_Size(infoPtr, ((WINDOWPOS *)lParam)->cx, ((WINDOWPOS *)lParam)->cy);
11741 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
11743 /* case WM_WININICHANGE: */
11745 default:
11746 if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
11747 ERR("unknown msg %04x wp=%08lx lp=%08lx\n", uMsg, wParam, lParam);
11749 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
11754 /***
11755 * DESCRIPTION:
11756 * Registers the window class.
11758 * PARAMETER(S):
11759 * None
11761 * RETURN:
11762 * None
11764 void LISTVIEW_Register(void)
11766 WNDCLASSW wndClass;
11768 ZeroMemory(&wndClass, sizeof(WNDCLASSW));
11769 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS;
11770 wndClass.lpfnWndProc = LISTVIEW_WindowProc;
11771 wndClass.cbClsExtra = 0;
11772 wndClass.cbWndExtra = sizeof(LISTVIEW_INFO *);
11773 wndClass.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
11774 wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
11775 wndClass.lpszClassName = WC_LISTVIEWW;
11776 RegisterClassW(&wndClass);
11779 /***
11780 * DESCRIPTION:
11781 * Unregisters the window class.
11783 * PARAMETER(S):
11784 * None
11786 * RETURN:
11787 * None
11789 void LISTVIEW_Unregister(void)
11791 UnregisterClassW(WC_LISTVIEWW, NULL);
11794 /***
11795 * DESCRIPTION:
11796 * Handle any WM_COMMAND messages
11798 * PARAMETER(S):
11799 * [I] infoPtr : valid pointer to the listview structure
11800 * [I] wParam : the first message parameter
11801 * [I] lParam : the second message parameter
11803 * RETURN:
11804 * Zero.
11806 static LRESULT LISTVIEW_Command(LISTVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
11809 TRACE("(%p %x %x %lx)\n", infoPtr, HIWORD(wParam), LOWORD(wParam), lParam);
11811 if (!infoPtr->hwndEdit) return 0;
11813 switch (HIWORD(wParam))
11815 case EN_UPDATE:
11818 * Adjust the edit window size
11820 WCHAR buffer[1024];
11821 HDC hdc = GetDC(infoPtr->hwndEdit);
11822 HFONT hFont, hOldFont = 0;
11823 RECT rect;
11824 SIZE sz;
11826 if (!infoPtr->hwndEdit || !hdc) return 0;
11827 GetWindowTextW(infoPtr->hwndEdit, buffer, sizeof(buffer)/sizeof(buffer[0]));
11828 GetWindowRect(infoPtr->hwndEdit, &rect);
11830 /* Select font to get the right dimension of the string */
11831 hFont = (HFONT)SendMessageW(infoPtr->hwndEdit, WM_GETFONT, 0, 0);
11832 if (hFont)
11834 hOldFont = SelectObject(hdc, hFont);
11837 if (GetTextExtentPoint32W(hdc, buffer, lstrlenW(buffer), &sz))
11839 TEXTMETRICW textMetric;
11841 /* Add Extra spacing for the next character */
11842 GetTextMetricsW(hdc, &textMetric);
11843 sz.cx += (textMetric.tmMaxCharWidth * 2);
11845 SetWindowPos(infoPtr->hwndEdit, NULL, 0, 0, sz.cx,
11846 rect.bottom - rect.top, SWP_DRAWFRAME | SWP_NOMOVE | SWP_NOZORDER);
11848 if (hFont)
11849 SelectObject(hdc, hOldFont);
11851 ReleaseDC(infoPtr->hwndEdit, hdc);
11853 break;
11855 case EN_KILLFOCUS:
11857 LISTVIEW_CancelEditLabel(infoPtr);
11858 break;
11861 default:
11862 return SendMessageW (infoPtr->hwndNotify, WM_COMMAND, wParam, lParam);
11865 return 0;