user32: Fix the calculation of system metrics icon sizes.
[wine.git] / dlls / comctl32 / listview.c
blobc01d816c4684ace720002de9c3bd33de69b49512
1 /*
2 * Listview control
4 * Copyright 1998, 1999 Eric Kohl
5 * Copyright 1999 Luc Tourangeau
6 * Copyright 2000 Jason Mawdsley
7 * Copyright 2001 CodeWeavers Inc.
8 * Copyright 2002 Dimitrie O. Paun
9 * Copyright 2009-2015 Nikolay Sivov
10 * Copyright 2009 Owen Rudge for CodeWeavers
11 * Copyright 2012-2013 Daniel Jelinski
13 * This library is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU Lesser General Public
15 * License as published by the Free Software Foundation; either
16 * version 2.1 of the License, or (at your option) any later version.
18 * This library is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * Lesser General Public License for more details.
23 * You should have received a copy of the GNU Lesser General Public
24 * License along with this library; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27 * NOTES
29 * This code was audited for completeness against the documented features
30 * of Comctl32.dll version 6.0 on May. 20, 2005, by James Hawkins.
32 * Unless otherwise noted, we believe this code to be complete, as per
33 * the specification mentioned above.
34 * If you discover missing features, or bugs, please note them below.
36 * TODO:
38 * Default Message Processing
39 * -- WM_CREATE: create the icon and small icon image lists at this point only if
40 * the LVS_SHAREIMAGELISTS style is not specified.
41 * -- WM_WINDOWPOSCHANGED: arrange the list items if the current view is icon
42 * or small icon and the LVS_AUTOARRANGE style is specified.
43 * -- WM_TIMER
44 * -- WM_WININICHANGE
46 * Features
47 * -- Hot item handling, mouse hovering
48 * -- Workareas support
49 * -- Tilemode support
50 * -- Groups support
52 * Bugs
53 * -- Expand large item in ICON mode when the cursor is flying over the icon or text.
54 * -- Support CustomDraw options for _WIN32_IE >= 0x560 (see NMLVCUSTOMDRAW docs).
55 * -- LVA_SNAPTOGRID not implemented
56 * -- LISTVIEW_ApproximateViewRect partially implemented
57 * -- LISTVIEW_StyleChanged doesn't handle some changes too well
59 * Speedups
60 * -- LISTVIEW_GetNextItem needs to be rewritten. It is currently
61 * linear in the number of items in the list, and this is
62 * unacceptable for large lists.
63 * -- if list is sorted by item text LISTVIEW_InsertItemT could use
64 * binary search to calculate item index (e.g. DPA_Search()).
65 * This requires sorted state to be reliably tracked in item modifiers.
66 * -- we should keep an ordered array of coordinates in iconic mode.
67 * This would allow framing items (iterator_frameditems),
68 * and finding the nearest item (LVFI_NEARESTXY) a lot more efficiently.
70 * Flags
71 * -- LVIF_COLUMNS
72 * -- LVIF_GROUPID
74 * States
75 * -- LVIS_ACTIVATING (not currently supported by comctl32.dll version 6.0)
76 * -- LVIS_DROPHILITED
78 * Styles
79 * -- LVS_NOLABELWRAP
80 * -- LVS_NOSCROLL (see Q137520)
81 * -- LVS_ALIGNTOP
83 * Extended Styles
84 * -- LVS_EX_BORDERSELECT
85 * -- LVS_EX_FLATSB
86 * -- LVS_EX_INFOTIP
87 * -- LVS_EX_LABELTIP
88 * -- LVS_EX_MULTIWORKAREAS
89 * -- LVS_EX_REGIONAL
90 * -- LVS_EX_SIMPLESELECT
91 * -- LVS_EX_TWOCLICKACTIVATE
92 * -- LVS_EX_UNDERLINECOLD
93 * -- LVS_EX_UNDERLINEHOT
95 * Notifications:
96 * -- LVN_BEGINSCROLL, LVN_ENDSCROLL
97 * -- LVN_GETINFOTIP
98 * -- LVN_HOTTRACK
99 * -- LVN_SETDISPINFO
101 * Messages:
102 * -- LVM_ENABLEGROUPVIEW
103 * -- LVM_GETBKIMAGE, LVM_SETBKIMAGE
104 * -- LVM_GETGROUPINFO, LVM_SETGROUPINFO
105 * -- LVM_GETGROUPMETRICS, LVM_SETGROUPMETRICS
106 * -- LVM_GETINSERTMARK, LVM_SETINSERTMARK
107 * -- LVM_GETINSERTMARKCOLOR, LVM_SETINSERTMARKCOLOR
108 * -- LVM_GETINSERTMARKRECT
109 * -- LVM_GETNUMBEROFWORKAREAS
110 * -- LVM_GETOUTLINECOLOR, LVM_SETOUTLINECOLOR
111 * -- LVM_GETSELECTEDCOLUMN, LVM_SETSELECTEDCOLUMN
112 * -- LVM_GETISEARCHSTRINGW, LVM_GETISEARCHSTRINGA
113 * -- LVM_GETTILEINFO, LVM_SETTILEINFO
114 * -- LVM_GETTILEVIEWINFO, LVM_SETTILEVIEWINFO
115 * -- LVM_GETWORKAREAS, LVM_SETWORKAREAS
116 * -- LVM_HASGROUP, LVM_INSERTGROUP, LVM_REMOVEGROUP, LVM_REMOVEALLGROUPS
117 * -- LVM_INSERTGROUPSORTED
118 * -- LVM_INSERTMARKHITTEST
119 * -- LVM_ISGROUPVIEWENABLED
120 * -- LVM_MOVEGROUP
121 * -- LVM_MOVEITEMTOGROUP
122 * -- LVM_SETINFOTIP
123 * -- LVM_SETTILEWIDTH
124 * -- LVM_SORTGROUPS
126 * Macros:
127 * -- ListView_GetHoverTime, ListView_SetHoverTime
128 * -- ListView_GetISearchString
129 * -- ListView_GetNumberOfWorkAreas
130 * -- ListView_GetWorkAreas, ListView_SetWorkAreas
132 * Functions:
133 * -- LVGroupComparE
136 #include "config.h"
137 #include "wine/port.h"
139 #include <assert.h>
140 #include <ctype.h>
141 #include <string.h>
142 #include <stdlib.h>
143 #include <stdarg.h>
144 #include <stdio.h>
146 #include "windef.h"
147 #include "winbase.h"
148 #include "winnt.h"
149 #include "wingdi.h"
150 #include "winuser.h"
151 #include "winnls.h"
152 #include "commctrl.h"
153 #include "comctl32.h"
154 #include "uxtheme.h"
156 #include "wine/debug.h"
157 #include "wine/unicode.h"
159 WINE_DEFAULT_DEBUG_CHANNEL(listview);
161 typedef struct tagCOLUMN_INFO
163 RECT rcHeader; /* tracks the header's rectangle */
164 INT fmt; /* same as LVCOLUMN.fmt */
165 INT cxMin;
166 } COLUMN_INFO;
168 typedef struct tagITEMHDR
170 LPWSTR pszText;
171 INT iImage;
172 } ITEMHDR, *LPITEMHDR;
174 typedef struct tagSUBITEM_INFO
176 ITEMHDR hdr;
177 INT iSubItem;
178 } SUBITEM_INFO;
180 typedef struct tagITEM_ID ITEM_ID;
182 typedef struct tagITEM_INFO
184 ITEMHDR hdr;
185 UINT state;
186 LPARAM lParam;
187 INT iIndent;
188 ITEM_ID *id;
189 } ITEM_INFO;
191 struct tagITEM_ID
193 UINT id; /* item id */
194 HDPA item; /* link to item data */
197 typedef struct tagRANGE
199 INT lower;
200 INT upper;
201 } RANGE;
203 typedef struct tagRANGES
205 HDPA hdpa;
206 } *RANGES;
208 typedef struct tagITERATOR
210 INT nItem;
211 INT nSpecial;
212 RANGE range;
213 RANGES ranges;
214 INT index;
215 } ITERATOR;
217 typedef struct tagDELAYED_ITEM_EDIT
219 BOOL fEnabled;
220 INT iItem;
221 } DELAYED_ITEM_EDIT;
223 typedef struct tagLISTVIEW_INFO
225 /* control window */
226 HWND hwndSelf;
227 RECT rcList; /* This rectangle is really the window
228 * client rectangle possibly reduced by the
229 * horizontal scroll bar and/or header - see
230 * LISTVIEW_UpdateSize. This rectangle offset
231 * by the LISTVIEW_GetOrigin value is in
232 * client coordinates */
234 /* notification window */
235 SHORT notifyFormat;
236 HWND hwndNotify;
237 BOOL bDoChangeNotify; /* send change notification messages? */
238 UINT uCallbackMask;
240 /* tooltips */
241 HWND hwndToolTip;
243 /* items */
244 INT nItemCount; /* the number of items in the list */
245 HDPA hdpaItems; /* array ITEM_INFO pointers */
246 HDPA hdpaItemIds; /* array of ITEM_ID pointers */
247 HDPA hdpaPosX; /* maintains the (X, Y) coordinates of the */
248 HDPA hdpaPosY; /* items in LVS_ICON, and LVS_SMALLICON modes */
249 RANGES selectionRanges;
250 INT nSelectionMark; /* item to start next multiselection from */
251 INT nHotItem;
252 BOOL bAutoarrange; /* Autoarrange flag when NOT in LVS_AUTOARRANGE */
254 /* columns */
255 HDPA hdpaColumns; /* array of COLUMN_INFO pointers */
256 BOOL colRectsDirty; /* trigger column rectangles requery from header */
258 /* item metrics */
259 BOOL bNoItemMetrics; /* flags if item metrics are not yet computed */
260 INT nItemHeight;
261 INT nItemWidth;
263 /* sorting */
264 PFNLVCOMPARE pfnCompare; /* sorting callback pointer */
265 LPARAM lParamSort;
267 /* style */
268 DWORD dwStyle; /* the cached window GWL_STYLE */
269 DWORD dwLvExStyle; /* extended listview style */
270 DWORD uView; /* current view available through LVM_[G,S]ETVIEW */
272 /* edit item */
273 HWND hwndEdit;
274 WNDPROC EditWndProc;
275 INT nEditLabelItem;
276 DELAYED_ITEM_EDIT itemEdit; /* Pointer to this structure will be the timer ID */
278 /* icons */
279 HIMAGELIST himlNormal;
280 HIMAGELIST himlSmall;
281 HIMAGELIST himlState;
282 SIZE iconSize;
283 BOOL autoSpacing;
284 SIZE iconSpacing;
285 SIZE iconStateSize;
286 POINT currIconPos; /* this is the position next icon will be placed */
288 /* header */
289 HWND hwndHeader;
290 INT xTrackLine; /* The x coefficient of the track line or -1 if none */
292 /* marquee selection */
293 BOOL bMarqueeSelect; /* marquee selection/highlight underway */
294 BOOL bScrolling;
295 RECT marqueeRect; /* absolute coordinates of marquee selection */
296 RECT marqueeDrawRect; /* relative coordinates for drawing marquee */
297 POINT marqueeOrigin; /* absolute coordinates of marquee click origin */
299 /* focus drawing */
300 BOOL bFocus; /* control has focus */
301 INT nFocusedItem;
302 RECT rcFocus; /* focus bounds */
304 /* colors */
305 HBRUSH hBkBrush;
306 COLORREF clrBk;
307 COLORREF clrText;
308 COLORREF clrTextBk;
310 /* font */
311 HFONT hDefaultFont;
312 HFONT hFont;
313 INT ntmHeight; /* Some cached metrics of the font used */
314 INT ntmMaxCharWidth; /* by the listview to draw items */
315 INT nEllipsisWidth;
317 /* mouse operation */
318 BOOL bLButtonDown;
319 BOOL bDragging;
320 POINT ptClickPos; /* point where the user clicked */
321 INT nLButtonDownItem; /* tracks item to reset multiselection on WM_LBUTTONUP */
322 DWORD dwHoverTime;
323 HCURSOR hHotCursor;
324 INT cWheelRemainder;
326 /* keyboard operation */
327 DWORD lastKeyPressTimestamp;
328 WPARAM charCode;
329 INT nSearchParamLength;
330 WCHAR szSearchParam[ MAX_PATH ];
332 /* painting */
333 BOOL bIsDrawing; /* Drawing in progress */
334 INT nMeasureItemHeight; /* WM_MEASUREITEM result */
335 BOOL redraw; /* WM_SETREDRAW switch */
337 /* misc */
338 DWORD iVersion; /* CCM_[G,S]ETVERSION */
339 } LISTVIEW_INFO;
342 * constants
344 /* How many we debug buffer to allocate */
345 #define DEBUG_BUFFERS 20
346 /* The size of a single debug buffer */
347 #define DEBUG_BUFFER_SIZE 256
349 /* Internal interface to LISTVIEW_HScroll and LISTVIEW_VScroll */
350 #define SB_INTERNAL -1
352 /* maximum size of a label */
353 #define DISP_TEXT_SIZE 260
355 /* padding for items in list and small icon display modes */
356 #define WIDTH_PADDING 12
358 /* padding for items in list, report and small icon display modes */
359 #define HEIGHT_PADDING 1
361 /* offset of items in report display mode */
362 #define REPORT_MARGINX 2
364 /* padding for icon in large icon display mode
365 * ICON_TOP_PADDING_NOTHITABLE - space between top of box and area
366 * that HITTEST will see.
367 * ICON_TOP_PADDING_HITABLE - spacing between above and icon.
368 * ICON_TOP_PADDING - sum of the two above.
369 * ICON_BOTTOM_PADDING - between bottom of icon and top of text
370 * LABEL_HOR_PADDING - between text and sides of box
371 * LABEL_VERT_PADDING - between bottom of text and end of box
373 * ICON_LR_PADDING - additional width above icon size.
374 * ICON_LR_HALF - half of the above value
376 #define ICON_TOP_PADDING_NOTHITABLE 2
377 #define ICON_TOP_PADDING_HITABLE 2
378 #define ICON_TOP_PADDING (ICON_TOP_PADDING_NOTHITABLE + ICON_TOP_PADDING_HITABLE)
379 #define ICON_BOTTOM_PADDING 4
380 #define LABEL_HOR_PADDING 5
381 #define LABEL_VERT_PADDING 7
382 #define ICON_LR_PADDING 16
383 #define ICON_LR_HALF (ICON_LR_PADDING/2)
385 /* default label width for items in list and small icon display modes */
386 #define DEFAULT_LABEL_WIDTH 40
387 /* maximum select rectangle width for empty text item in LV_VIEW_DETAILS */
388 #define MAX_EMPTYTEXT_SELECT_WIDTH 80
390 /* default column width for items in list display mode */
391 #define DEFAULT_COLUMN_WIDTH 128
393 /* Size of "line" scroll for V & H scrolls */
394 #define LISTVIEW_SCROLL_ICON_LINE_SIZE 37
396 /* Padding between image and label */
397 #define IMAGE_PADDING 2
399 /* Padding behind the label */
400 #define TRAILING_LABEL_PADDING 12
401 #define TRAILING_HEADER_PADDING 11
403 /* Border for the icon caption */
404 #define CAPTION_BORDER 2
406 /* Standard DrawText flags */
407 #define LV_ML_DT_FLAGS (DT_TOP | DT_NOPREFIX | DT_EDITCONTROL | DT_CENTER | DT_WORDBREAK | DT_WORD_ELLIPSIS | DT_END_ELLIPSIS)
408 #define LV_FL_DT_FLAGS (DT_TOP | DT_NOPREFIX | DT_EDITCONTROL | DT_CENTER | DT_WORDBREAK | DT_NOCLIP)
409 #define LV_SL_DT_FLAGS (DT_VCENTER | DT_NOPREFIX | DT_EDITCONTROL | DT_SINGLELINE | DT_WORD_ELLIPSIS | DT_END_ELLIPSIS)
411 /* Image index from state */
412 #define STATEIMAGEINDEX(x) (((x) & LVIS_STATEIMAGEMASK) >> 12)
414 /* The time in milliseconds to reset the search in the list */
415 #define KEY_DELAY 450
417 /* Dump the LISTVIEW_INFO structure to the debug channel */
418 #define LISTVIEW_DUMP(iP) do { \
419 TRACE("hwndSelf=%p, clrBk=0x%06x, clrText=0x%06x, clrTextBk=0x%06x, ItemHeight=%d, ItemWidth=%d, Style=0x%08x\n", \
420 iP->hwndSelf, iP->clrBk, iP->clrText, iP->clrTextBk, \
421 iP->nItemHeight, iP->nItemWidth, iP->dwStyle); \
422 TRACE("hwndSelf=%p, himlNor=%p, himlSml=%p, himlState=%p, Focused=%d, Hot=%d, exStyle=0x%08x, Focus=%d\n", \
423 iP->hwndSelf, iP->himlNormal, iP->himlSmall, iP->himlState, \
424 iP->nFocusedItem, iP->nHotItem, iP->dwLvExStyle, iP->bFocus ); \
425 TRACE("hwndSelf=%p, ntmH=%d, icSz.cx=%d, icSz.cy=%d, icSp.cx=%d, icSp.cy=%d, notifyFmt=%d\n", \
426 iP->hwndSelf, iP->ntmHeight, iP->iconSize.cx, iP->iconSize.cy, \
427 iP->iconSpacing.cx, iP->iconSpacing.cy, iP->notifyFormat); \
428 TRACE("hwndSelf=%p, rcList=%s\n", iP->hwndSelf, wine_dbgstr_rect(&iP->rcList)); \
429 } while(0)
431 static const WCHAR themeClass[] = {'L','i','s','t','V','i','e','w',0};
434 * forward declarations
436 static BOOL LISTVIEW_GetItemT(const LISTVIEW_INFO *, LPLVITEMW, BOOL);
437 static void LISTVIEW_GetItemBox(const LISTVIEW_INFO *, INT, LPRECT);
438 static void LISTVIEW_GetItemOrigin(const LISTVIEW_INFO *, INT, LPPOINT);
439 static BOOL LISTVIEW_GetItemPosition(const LISTVIEW_INFO *, INT, LPPOINT);
440 static BOOL LISTVIEW_GetItemRect(const LISTVIEW_INFO *, INT, LPRECT);
441 static void LISTVIEW_GetOrigin(const LISTVIEW_INFO *, LPPOINT);
442 static BOOL LISTVIEW_GetViewRect(const LISTVIEW_INFO *, LPRECT);
443 static void LISTVIEW_UpdateSize(LISTVIEW_INFO *);
444 static LRESULT LISTVIEW_Command(LISTVIEW_INFO *, WPARAM, LPARAM);
445 static INT LISTVIEW_GetStringWidthT(const LISTVIEW_INFO *, LPCWSTR, BOOL);
446 static BOOL LISTVIEW_KeySelection(LISTVIEW_INFO *, INT, BOOL);
447 static UINT LISTVIEW_GetItemState(const LISTVIEW_INFO *, INT, UINT);
448 static BOOL LISTVIEW_SetItemState(LISTVIEW_INFO *, INT, const LVITEMW *);
449 static LRESULT LISTVIEW_VScroll(LISTVIEW_INFO *, INT, INT);
450 static LRESULT LISTVIEW_HScroll(LISTVIEW_INFO *, INT, INT);
451 static BOOL LISTVIEW_EnsureVisible(LISTVIEW_INFO *, INT, BOOL);
452 static HIMAGELIST LISTVIEW_SetImageList(LISTVIEW_INFO *, INT, HIMAGELIST);
453 static INT LISTVIEW_HitTest(const LISTVIEW_INFO *, LPLVHITTESTINFO, BOOL, BOOL);
454 static BOOL LISTVIEW_EndEditLabelT(LISTVIEW_INFO *, BOOL, BOOL);
455 static BOOL LISTVIEW_Scroll(LISTVIEW_INFO *, INT, INT);
457 /******** Text handling functions *************************************/
459 /* A text pointer is either NULL, LPSTR_TEXTCALLBACK, or points to a
460 * text string. The string may be ANSI or Unicode, in which case
461 * the boolean isW tells us the type of the string.
463 * The name of the function tell what type of strings it expects:
464 * W: Unicode, T: ANSI/Unicode - function of isW
467 static inline BOOL is_text(LPCWSTR text)
469 return text != NULL && text != LPSTR_TEXTCALLBACKW;
472 static inline int textlenT(LPCWSTR text, BOOL isW)
474 return !is_text(text) ? 0 :
475 isW ? lstrlenW(text) : lstrlenA((LPCSTR)text);
478 static inline void textcpynT(LPWSTR dest, BOOL isDestW, LPCWSTR src, BOOL isSrcW, INT max)
480 if (isDestW)
481 if (isSrcW) lstrcpynW(dest, src, max);
482 else MultiByteToWideChar(CP_ACP, 0, (LPCSTR)src, -1, dest, max);
483 else
484 if (isSrcW) WideCharToMultiByte(CP_ACP, 0, src, -1, (LPSTR)dest, max, NULL, NULL);
485 else lstrcpynA((LPSTR)dest, (LPCSTR)src, max);
488 static inline LPWSTR textdupTtoW(LPCWSTR text, BOOL isW)
490 LPWSTR wstr = (LPWSTR)text;
492 if (!isW && is_text(text))
494 INT len = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)text, -1, NULL, 0);
495 wstr = Alloc(len * sizeof(WCHAR));
496 if (wstr) MultiByteToWideChar(CP_ACP, 0, (LPCSTR)text, -1, wstr, len);
498 TRACE(" wstr=%s\n", text == LPSTR_TEXTCALLBACKW ? "(callback)" : debugstr_w(wstr));
499 return wstr;
502 static inline void textfreeT(LPWSTR wstr, BOOL isW)
504 if (!isW && is_text(wstr)) Free (wstr);
508 * dest is a pointer to a Unicode string
509 * src is a pointer to a string (Unicode if isW, ANSI if !isW)
511 static BOOL textsetptrT(LPWSTR *dest, LPCWSTR src, BOOL isW)
513 BOOL bResult = TRUE;
515 if (src == LPSTR_TEXTCALLBACKW)
517 if (is_text(*dest)) Free(*dest);
518 *dest = LPSTR_TEXTCALLBACKW;
520 else
522 LPWSTR pszText = textdupTtoW(src, isW);
523 if (*dest == LPSTR_TEXTCALLBACKW) *dest = NULL;
524 bResult = Str_SetPtrW(dest, pszText);
525 textfreeT(pszText, isW);
527 return bResult;
531 * compares a Unicode to a Unicode/ANSI text string
533 static inline int textcmpWT(LPCWSTR aw, LPCWSTR bt, BOOL isW)
535 if (!aw) return bt ? -1 : 0;
536 if (!bt) return 1;
537 if (aw == LPSTR_TEXTCALLBACKW)
538 return bt == LPSTR_TEXTCALLBACKW ? 1 : -1;
539 if (bt != LPSTR_TEXTCALLBACKW)
541 LPWSTR bw = textdupTtoW(bt, isW);
542 int r = bw ? lstrcmpW(aw, bw) : 1;
543 textfreeT(bw, isW);
544 return r;
547 return 1;
550 static inline int lstrncmpiW(LPCWSTR s1, LPCWSTR s2, int n)
552 n = min(min(n, lstrlenW(s1)), lstrlenW(s2));
553 return CompareStringW(LOCALE_USER_DEFAULT, NORM_IGNORECASE, s1, n, s2, n) - CSTR_EQUAL;
556 /******** Debugging functions *****************************************/
558 static inline LPCSTR debugtext_t(LPCWSTR text, BOOL isW)
560 if (text == LPSTR_TEXTCALLBACKW) return "(callback)";
561 return isW ? debugstr_w(text) : debugstr_a((LPCSTR)text);
564 static inline LPCSTR debugtext_tn(LPCWSTR text, BOOL isW, INT n)
566 if (text == LPSTR_TEXTCALLBACKW) return "(callback)";
567 n = min(textlenT(text, isW), n);
568 return isW ? debugstr_wn(text, n) : debugstr_an((LPCSTR)text, n);
571 static char* debug_getbuf(void)
573 static int index = 0;
574 static char buffers[DEBUG_BUFFERS][DEBUG_BUFFER_SIZE];
575 return buffers[index++ % DEBUG_BUFFERS];
578 static inline const char* debugrange(const RANGE *lprng)
580 if (!lprng) return "(null)";
581 return wine_dbg_sprintf("[%d, %d]", lprng->lower, lprng->upper);
584 static const char* debugscrollinfo(const SCROLLINFO *pScrollInfo)
586 char* buf = debug_getbuf(), *text = buf;
587 int len, size = DEBUG_BUFFER_SIZE;
589 if (pScrollInfo == NULL) return "(null)";
590 len = snprintf(buf, size, "{cbSize=%u, ", pScrollInfo->cbSize);
591 if (len == -1) goto end;
592 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;
597 buf += len; size -= len;
598 if (pScrollInfo->fMask & SIF_PAGE)
599 len = snprintf(buf, size, "nPage=%u, ", pScrollInfo->nPage);
600 else len = 0;
601 if (len == -1) goto end;
602 buf += len; size -= len;
603 if (pScrollInfo->fMask & SIF_POS)
604 len = snprintf(buf, size, "nPos=%d, ", pScrollInfo->nPos);
605 else len = 0;
606 if (len == -1) goto end;
607 buf += len; size -= len;
608 if (pScrollInfo->fMask & SIF_TRACKPOS)
609 len = snprintf(buf, size, "nTrackPos=%d, ", pScrollInfo->nTrackPos);
610 else len = 0;
611 if (len == -1) goto end;
612 buf += len;
613 goto undo;
614 end:
615 buf = text + strlen(text);
616 undo:
617 if (buf - text > 2) { buf[-2] = '}'; buf[-1] = 0; }
618 return text;
621 static const char* debugnmlistview(const NMLISTVIEW *plvnm)
623 if (!plvnm) return "(null)";
624 return wine_dbg_sprintf("iItem=%d, iSubItem=%d, uNewState=0x%x,"
625 " uOldState=0x%x, uChanged=0x%x, ptAction=%s, lParam=%ld",
626 plvnm->iItem, plvnm->iSubItem, plvnm->uNewState, plvnm->uOldState,
627 plvnm->uChanged, wine_dbgstr_point(&plvnm->ptAction), plvnm->lParam);
630 static const char* debuglvitem_t(const LVITEMW *lpLVItem, BOOL isW)
632 char* buf = debug_getbuf(), *text = buf;
633 int len, size = DEBUG_BUFFER_SIZE;
635 if (lpLVItem == NULL) return "(null)";
636 len = snprintf(buf, size, "{iItem=%d, iSubItem=%d, ", lpLVItem->iItem, lpLVItem->iSubItem);
637 if (len == -1) goto end;
638 buf += len; size -= len;
639 if (lpLVItem->mask & LVIF_STATE)
640 len = snprintf(buf, size, "state=%x, stateMask=%x, ", lpLVItem->state, lpLVItem->stateMask);
641 else len = 0;
642 if (len == -1) goto end;
643 buf += len; size -= len;
644 if (lpLVItem->mask & LVIF_TEXT)
645 len = snprintf(buf, size, "pszText=%s, cchTextMax=%d, ", debugtext_tn(lpLVItem->pszText, isW, 80), lpLVItem->cchTextMax);
646 else len = 0;
647 if (len == -1) goto end;
648 buf += len; size -= len;
649 if (lpLVItem->mask & LVIF_IMAGE)
650 len = snprintf(buf, size, "iImage=%d, ", lpLVItem->iImage);
651 else len = 0;
652 if (len == -1) goto end;
653 buf += len; size -= len;
654 if (lpLVItem->mask & LVIF_PARAM)
655 len = snprintf(buf, size, "lParam=%lx, ", lpLVItem->lParam);
656 else len = 0;
657 if (len == -1) goto end;
658 buf += len; size -= len;
659 if (lpLVItem->mask & LVIF_INDENT)
660 len = snprintf(buf, size, "iIndent=%d, ", lpLVItem->iIndent);
661 else len = 0;
662 if (len == -1) goto end;
663 buf += len;
664 goto undo;
665 end:
666 buf = text + strlen(text);
667 undo:
668 if (buf - text > 2) { buf[-2] = '}'; buf[-1] = 0; }
669 return text;
672 static const char* debuglvcolumn_t(const LVCOLUMNW *lpColumn, BOOL isW)
674 char* buf = debug_getbuf(), *text = buf;
675 int len, size = DEBUG_BUFFER_SIZE;
677 if (lpColumn == NULL) return "(null)";
678 len = snprintf(buf, size, "{");
679 if (len == -1) goto end;
680 buf += len; size -= len;
681 if (lpColumn->mask & LVCF_SUBITEM)
682 len = snprintf(buf, size, "iSubItem=%d, ", lpColumn->iSubItem);
683 else len = 0;
684 if (len == -1) goto end;
685 buf += len; size -= len;
686 if (lpColumn->mask & LVCF_FMT)
687 len = snprintf(buf, size, "fmt=%x, ", lpColumn->fmt);
688 else len = 0;
689 if (len == -1) goto end;
690 buf += len; size -= len;
691 if (lpColumn->mask & LVCF_WIDTH)
692 len = snprintf(buf, size, "cx=%d, ", lpColumn->cx);
693 else len = 0;
694 if (len == -1) goto end;
695 buf += len; size -= len;
696 if (lpColumn->mask & LVCF_TEXT)
697 len = snprintf(buf, size, "pszText=%s, cchTextMax=%d, ", debugtext_tn(lpColumn->pszText, isW, 80), lpColumn->cchTextMax);
698 else len = 0;
699 if (len == -1) goto end;
700 buf += len; size -= len;
701 if (lpColumn->mask & LVCF_IMAGE)
702 len = snprintf(buf, size, "iImage=%d, ", lpColumn->iImage);
703 else len = 0;
704 if (len == -1) goto end;
705 buf += len; size -= len;
706 if (lpColumn->mask & LVCF_ORDER)
707 len = snprintf(buf, size, "iOrder=%d, ", lpColumn->iOrder);
708 else len = 0;
709 if (len == -1) goto end;
710 buf += len;
711 goto undo;
712 end:
713 buf = text + strlen(text);
714 undo:
715 if (buf - text > 2) { buf[-2] = '}'; buf[-1] = 0; }
716 return text;
719 static const char* debuglvhittestinfo(const LVHITTESTINFO *lpht)
721 if (!lpht) return "(null)";
723 return wine_dbg_sprintf("{pt=%s, flags=0x%x, iItem=%d, iSubItem=%d}",
724 wine_dbgstr_point(&lpht->pt), lpht->flags, lpht->iItem, lpht->iSubItem);
727 /* Return the corresponding text for a given scroll value */
728 static inline LPCSTR debugscrollcode(int nScrollCode)
730 switch(nScrollCode)
732 case SB_LINELEFT: return "SB_LINELEFT";
733 case SB_LINERIGHT: return "SB_LINERIGHT";
734 case SB_PAGELEFT: return "SB_PAGELEFT";
735 case SB_PAGERIGHT: return "SB_PAGERIGHT";
736 case SB_THUMBPOSITION: return "SB_THUMBPOSITION";
737 case SB_THUMBTRACK: return "SB_THUMBTRACK";
738 case SB_ENDSCROLL: return "SB_ENDSCROLL";
739 case SB_INTERNAL: return "SB_INTERNAL";
740 default: return "unknown";
745 /******** Notification functions ************************************/
747 static int get_ansi_notification(UINT unicodeNotificationCode)
749 switch (unicodeNotificationCode)
751 case LVN_BEGINLABELEDITA:
752 case LVN_BEGINLABELEDITW: return LVN_BEGINLABELEDITA;
753 case LVN_ENDLABELEDITA:
754 case LVN_ENDLABELEDITW: return LVN_ENDLABELEDITA;
755 case LVN_GETDISPINFOA:
756 case LVN_GETDISPINFOW: return LVN_GETDISPINFOA;
757 case LVN_SETDISPINFOA:
758 case LVN_SETDISPINFOW: return LVN_SETDISPINFOA;
759 case LVN_ODFINDITEMA:
760 case LVN_ODFINDITEMW: return LVN_ODFINDITEMA;
761 case LVN_GETINFOTIPA:
762 case LVN_GETINFOTIPW: return LVN_GETINFOTIPA;
763 /* header forwards */
764 case HDN_TRACKA:
765 case HDN_TRACKW: return HDN_TRACKA;
766 case HDN_ENDTRACKA:
767 case HDN_ENDTRACKW: return HDN_ENDTRACKA;
768 case HDN_BEGINDRAG: return HDN_BEGINDRAG;
769 case HDN_ENDDRAG: return HDN_ENDDRAG;
770 case HDN_ITEMCHANGINGA:
771 case HDN_ITEMCHANGINGW: return HDN_ITEMCHANGINGA;
772 case HDN_ITEMCHANGEDA:
773 case HDN_ITEMCHANGEDW: return HDN_ITEMCHANGEDA;
774 case HDN_ITEMCLICKA:
775 case HDN_ITEMCLICKW: return HDN_ITEMCLICKA;
776 case HDN_DIVIDERDBLCLICKA:
777 case HDN_DIVIDERDBLCLICKW: return HDN_DIVIDERDBLCLICKA;
778 default: break;
780 FIXME("unknown notification %x\n", unicodeNotificationCode);
781 return unicodeNotificationCode;
784 /* forwards header notifications to listview parent */
785 static LRESULT notify_forward_header(const LISTVIEW_INFO *infoPtr, NMHEADERW *lpnmhW)
787 LPCWSTR text = NULL, filter = NULL;
788 LRESULT ret;
789 NMHEADERA *lpnmh = (NMHEADERA*) lpnmhW;
791 /* on unicode format exit earlier */
792 if (infoPtr->notifyFormat == NFR_UNICODE)
793 return SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, lpnmh->hdr.idFrom,
794 (LPARAM)lpnmh);
796 /* header always supplies unicode notifications,
797 all we have to do is to convert strings to ANSI */
798 if (lpnmh->pitem)
800 /* convert item text */
801 if (lpnmh->pitem->mask & HDI_TEXT)
803 text = (LPCWSTR)lpnmh->pitem->pszText;
804 lpnmh->pitem->pszText = NULL;
805 Str_SetPtrWtoA(&lpnmh->pitem->pszText, text);
807 /* convert filter text */
808 if ((lpnmh->pitem->mask & HDI_FILTER) && (lpnmh->pitem->type == HDFT_ISSTRING) &&
809 lpnmh->pitem->pvFilter)
811 filter = (LPCWSTR)((HD_TEXTFILTERA*)lpnmh->pitem->pvFilter)->pszText;
812 ((HD_TEXTFILTERA*)lpnmh->pitem->pvFilter)->pszText = NULL;
813 Str_SetPtrWtoA(&((HD_TEXTFILTERA*)lpnmh->pitem->pvFilter)->pszText, filter);
816 lpnmh->hdr.code = get_ansi_notification(lpnmh->hdr.code);
818 ret = SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, lpnmh->hdr.idFrom,
819 (LPARAM)lpnmh);
821 /* cleanup */
822 if(text)
824 Free(lpnmh->pitem->pszText);
825 lpnmh->pitem->pszText = (LPSTR)text;
827 if(filter)
829 Free(((HD_TEXTFILTERA*)lpnmh->pitem->pvFilter)->pszText);
830 ((HD_TEXTFILTERA*)lpnmh->pitem->pvFilter)->pszText = (LPSTR)filter;
833 return ret;
836 static LRESULT notify_hdr(const LISTVIEW_INFO *infoPtr, INT code, LPNMHDR pnmh)
838 LRESULT result;
840 TRACE("(code=%d)\n", code);
842 pnmh->hwndFrom = infoPtr->hwndSelf;
843 pnmh->idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
844 pnmh->code = code;
845 result = SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, pnmh->idFrom, (LPARAM)pnmh);
847 TRACE(" <= %ld\n", result);
849 return result;
852 static inline BOOL notify(const LISTVIEW_INFO *infoPtr, INT code)
854 NMHDR nmh;
855 HWND hwnd = infoPtr->hwndSelf;
856 notify_hdr(infoPtr, code, &nmh);
857 return IsWindow(hwnd);
860 static inline void notify_itemactivate(const LISTVIEW_INFO *infoPtr, const LVHITTESTINFO *htInfo)
862 NMITEMACTIVATE nmia;
863 LVITEMW item;
865 nmia.uNewState = 0;
866 nmia.uOldState = 0;
867 nmia.uChanged = 0;
868 nmia.uKeyFlags = 0;
870 item.mask = LVIF_PARAM|LVIF_STATE;
871 item.iItem = htInfo->iItem;
872 item.iSubItem = 0;
873 item.stateMask = (UINT)-1;
874 if (LISTVIEW_GetItemT(infoPtr, &item, TRUE)) {
875 nmia.lParam = item.lParam;
876 nmia.uOldState = item.state;
877 nmia.uNewState = item.state | LVIS_ACTIVATING;
878 nmia.uChanged = LVIF_STATE;
881 nmia.iItem = htInfo->iItem;
882 nmia.iSubItem = htInfo->iSubItem;
883 nmia.ptAction = htInfo->pt;
885 if (GetKeyState(VK_SHIFT) & 0x8000) nmia.uKeyFlags |= LVKF_SHIFT;
886 if (GetKeyState(VK_CONTROL) & 0x8000) nmia.uKeyFlags |= LVKF_CONTROL;
887 if (GetKeyState(VK_MENU) & 0x8000) nmia.uKeyFlags |= LVKF_ALT;
889 notify_hdr(infoPtr, LVN_ITEMACTIVATE, (LPNMHDR)&nmia);
892 static inline LRESULT notify_listview(const LISTVIEW_INFO *infoPtr, INT code, LPNMLISTVIEW plvnm)
894 TRACE("(code=%d, plvnm=%s)\n", code, debugnmlistview(plvnm));
895 return notify_hdr(infoPtr, code, (LPNMHDR)plvnm);
898 /* Handles NM_DBLCLK, NM_CLICK, NM_RDBLCLK, NM_RCLICK. Only NM_RCLICK return value is used. */
899 static BOOL notify_click(const LISTVIEW_INFO *infoPtr, INT code, const LVHITTESTINFO *lvht)
901 NMITEMACTIVATE nmia;
902 LVITEMW item;
903 HWND hwnd = infoPtr->hwndSelf;
904 LRESULT ret;
906 TRACE("code=%d, lvht=%s\n", code, debuglvhittestinfo(lvht));
907 ZeroMemory(&nmia, sizeof(nmia));
908 nmia.iItem = lvht->iItem;
909 nmia.iSubItem = lvht->iSubItem;
910 nmia.ptAction = lvht->pt;
911 item.mask = LVIF_PARAM;
912 item.iItem = lvht->iItem;
913 item.iSubItem = 0;
914 if (LISTVIEW_GetItemT(infoPtr, &item, TRUE)) nmia.lParam = item.lParam;
915 ret = notify_hdr(infoPtr, code, (NMHDR*)&nmia);
916 return IsWindow(hwnd) && (code == NM_RCLICK ? !ret : TRUE);
919 static BOOL notify_deleteitem(const LISTVIEW_INFO *infoPtr, INT nItem)
921 NMLISTVIEW nmlv;
922 LVITEMW item;
923 HWND hwnd = infoPtr->hwndSelf;
925 ZeroMemory(&nmlv, sizeof (NMLISTVIEW));
926 nmlv.iItem = nItem;
927 item.mask = LVIF_PARAM;
928 item.iItem = nItem;
929 item.iSubItem = 0;
930 if (LISTVIEW_GetItemT(infoPtr, &item, TRUE)) nmlv.lParam = item.lParam;
931 notify_listview(infoPtr, LVN_DELETEITEM, &nmlv);
932 return IsWindow(hwnd);
936 Send notification. depends on dispinfoW having same
937 structure as dispinfoA.
938 infoPtr : listview struct
939 code : *Unicode* notification code
940 pdi : dispinfo structure (can be unicode or ansi)
941 isW : TRUE if dispinfo is Unicode
943 static BOOL notify_dispinfoT(const LISTVIEW_INFO *infoPtr, UINT code, LPNMLVDISPINFOW pdi, BOOL isW)
945 INT length = 0, ret_length;
946 LPWSTR buffer = NULL, ret_text;
947 BOOL return_ansi = FALSE;
948 BOOL return_unicode = FALSE;
949 BOOL ret;
951 if ((pdi->item.mask & LVIF_TEXT) && is_text(pdi->item.pszText))
953 return_unicode = ( isW && infoPtr->notifyFormat == NFR_ANSI);
954 return_ansi = (!isW && infoPtr->notifyFormat == NFR_UNICODE);
957 ret_length = pdi->item.cchTextMax;
958 ret_text = pdi->item.pszText;
960 if (return_unicode || return_ansi)
962 if (code != LVN_GETDISPINFOW)
964 length = return_ansi ?
965 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pdi->item.pszText, -1, NULL, 0):
966 WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, NULL, 0, NULL, NULL);
968 else
970 length = pdi->item.cchTextMax;
971 *pdi->item.pszText = 0; /* make sure we don't process garbage */
974 buffer = Alloc( (return_ansi ? sizeof(WCHAR) : sizeof(CHAR)) * length);
975 if (!buffer) return FALSE;
977 if (return_ansi)
978 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pdi->item.pszText, -1,
979 buffer, length);
980 else
981 WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, (LPSTR) buffer,
982 length, NULL, NULL);
984 pdi->item.pszText = buffer;
985 pdi->item.cchTextMax = length;
988 if (infoPtr->notifyFormat == NFR_ANSI)
989 code = get_ansi_notification(code);
991 TRACE(" pdi->item=%s\n", debuglvitem_t(&pdi->item, infoPtr->notifyFormat != NFR_ANSI));
992 ret = notify_hdr(infoPtr, code, &pdi->hdr);
993 TRACE(" resulting code=%d\n", pdi->hdr.code);
995 if (return_ansi || return_unicode)
997 if (return_ansi && (pdi->hdr.code == LVN_GETDISPINFOA))
999 strcpy((char*)ret_text, (char*)pdi->item.pszText);
1001 else if (return_unicode && (pdi->hdr.code == LVN_GETDISPINFOW))
1003 strcpyW(ret_text, pdi->item.pszText);
1005 else if (return_ansi) /* note : pointer can be changed by app ! */
1007 WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, (LPSTR) ret_text,
1008 ret_length, NULL, NULL);
1010 else
1011 MultiByteToWideChar(CP_ACP, 0, (LPSTR) pdi->item.pszText, -1,
1012 ret_text, ret_length);
1014 pdi->item.pszText = ret_text; /* restores our buffer */
1015 pdi->item.cchTextMax = ret_length;
1017 Free(buffer);
1018 return ret;
1021 /* if dipsinfo holder changed notification code then convert */
1022 if (!isW && (pdi->hdr.code == LVN_GETDISPINFOW) && (pdi->item.mask & LVIF_TEXT))
1024 length = WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, NULL, 0, NULL, NULL);
1026 buffer = Alloc(length * sizeof(CHAR));
1027 if (!buffer) return FALSE;
1029 WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, (LPSTR) buffer,
1030 ret_length, NULL, NULL);
1032 strcpy((LPSTR)pdi->item.pszText, (LPSTR)buffer);
1033 Free(buffer);
1036 return ret;
1039 static void customdraw_fill(NMLVCUSTOMDRAW *lpnmlvcd, const LISTVIEW_INFO *infoPtr, HDC hdc,
1040 const RECT *rcBounds, const LVITEMW *lplvItem)
1042 ZeroMemory(lpnmlvcd, sizeof(NMLVCUSTOMDRAW));
1043 lpnmlvcd->nmcd.hdc = hdc;
1044 lpnmlvcd->nmcd.rc = *rcBounds;
1045 lpnmlvcd->clrTextBk = infoPtr->clrTextBk;
1046 lpnmlvcd->clrText = infoPtr->clrText;
1047 if (!lplvItem) return;
1048 lpnmlvcd->nmcd.dwItemSpec = lplvItem->iItem + 1;
1049 lpnmlvcd->iSubItem = lplvItem->iSubItem;
1050 if (lplvItem->state & LVIS_SELECTED) lpnmlvcd->nmcd.uItemState |= CDIS_SELECTED;
1051 if (lplvItem->state & LVIS_FOCUSED) lpnmlvcd->nmcd.uItemState |= CDIS_FOCUS;
1052 if (lplvItem->iItem == infoPtr->nHotItem) lpnmlvcd->nmcd.uItemState |= CDIS_HOT;
1053 lpnmlvcd->nmcd.lItemlParam = lplvItem->lParam;
1056 static inline DWORD notify_customdraw (const LISTVIEW_INFO *infoPtr, DWORD dwDrawStage, NMLVCUSTOMDRAW *lpnmlvcd)
1058 BOOL isForItem = (lpnmlvcd->nmcd.dwItemSpec != 0);
1059 DWORD result;
1061 lpnmlvcd->nmcd.dwDrawStage = dwDrawStage;
1062 if (isForItem) lpnmlvcd->nmcd.dwDrawStage |= CDDS_ITEM;
1063 if (lpnmlvcd->iSubItem) lpnmlvcd->nmcd.dwDrawStage |= CDDS_SUBITEM;
1064 if (isForItem) lpnmlvcd->nmcd.dwItemSpec--;
1065 result = notify_hdr(infoPtr, NM_CUSTOMDRAW, &lpnmlvcd->nmcd.hdr);
1066 if (isForItem) lpnmlvcd->nmcd.dwItemSpec++;
1067 return result;
1070 static void prepaint_setup (const LISTVIEW_INFO *infoPtr, HDC hdc, NMLVCUSTOMDRAW *lpnmlvcd, BOOL SubItem)
1072 COLORREF backcolor, textcolor;
1074 /* apparently, for selected items, we have to override the returned values */
1075 if (!SubItem)
1077 if (lpnmlvcd->nmcd.uItemState & CDIS_SELECTED)
1079 if (infoPtr->bFocus)
1081 lpnmlvcd->clrTextBk = comctl32_color.clrHighlight;
1082 lpnmlvcd->clrText = comctl32_color.clrHighlightText;
1084 else if (infoPtr->dwStyle & LVS_SHOWSELALWAYS)
1086 lpnmlvcd->clrTextBk = comctl32_color.clr3dFace;
1087 lpnmlvcd->clrText = comctl32_color.clrBtnText;
1092 backcolor = lpnmlvcd->clrTextBk;
1093 textcolor = lpnmlvcd->clrText;
1095 if (backcolor == CLR_DEFAULT)
1096 backcolor = comctl32_color.clrWindow;
1097 if (textcolor == CLR_DEFAULT)
1098 textcolor = comctl32_color.clrWindowText;
1100 /* Set the text attributes */
1101 if (backcolor != CLR_NONE)
1103 SetBkMode(hdc, OPAQUE);
1104 SetBkColor(hdc, backcolor);
1106 else
1107 SetBkMode(hdc, TRANSPARENT);
1108 SetTextColor(hdc, textcolor);
1111 static inline DWORD notify_postpaint (const LISTVIEW_INFO *infoPtr, NMLVCUSTOMDRAW *lpnmlvcd)
1113 return notify_customdraw(infoPtr, CDDS_POSTPAINT, lpnmlvcd);
1116 /* returns TRUE when repaint needed, FALSE otherwise */
1117 static BOOL notify_measureitem(LISTVIEW_INFO *infoPtr)
1119 MEASUREITEMSTRUCT mis;
1120 mis.CtlType = ODT_LISTVIEW;
1121 mis.CtlID = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1122 mis.itemID = -1;
1123 mis.itemWidth = 0;
1124 mis.itemData = 0;
1125 mis.itemHeight= infoPtr->nItemHeight;
1126 SendMessageW(infoPtr->hwndNotify, WM_MEASUREITEM, mis.CtlID, (LPARAM)&mis);
1127 if (infoPtr->nItemHeight != max(mis.itemHeight, 1))
1129 infoPtr->nMeasureItemHeight = infoPtr->nItemHeight = max(mis.itemHeight, 1);
1130 return TRUE;
1132 return FALSE;
1135 /******** Item iterator functions **********************************/
1137 static RANGES ranges_create(int count);
1138 static void ranges_destroy(RANGES ranges);
1139 static BOOL ranges_add(RANGES ranges, RANGE range);
1140 static BOOL ranges_del(RANGES ranges, RANGE range);
1141 static void ranges_dump(RANGES ranges);
1143 static inline BOOL ranges_additem(RANGES ranges, INT nItem)
1145 RANGE range = { nItem, nItem + 1 };
1147 return ranges_add(ranges, range);
1150 static inline BOOL ranges_delitem(RANGES ranges, INT nItem)
1152 RANGE range = { nItem, nItem + 1 };
1154 return ranges_del(ranges, range);
1157 /***
1158 * ITERATOR DOCUMENTATION
1160 * The iterator functions allow for easy, and convenient iteration
1161 * over items of interest in the list. Typically, you create an
1162 * iterator, use it, and destroy it, as such:
1163 * ITERATOR i;
1165 * iterator_xxxitems(&i, ...);
1166 * while (iterator_{prev,next}(&i)
1168 * //code which uses i.nItem
1170 * iterator_destroy(&i);
1172 * where xxx is either: framed, or visible.
1173 * Note that it is important that the code destroys the iterator
1174 * after it's done with it, as the creation of the iterator may
1175 * allocate memory, which thus needs to be freed.
1177 * You can iterate both forwards, and backwards through the list,
1178 * by using iterator_next or iterator_prev respectively.
1180 * Lower numbered items are draw on top of higher number items in
1181 * LVS_ICON, and LVS_SMALLICON (which are the only modes where
1182 * items may overlap). So, to test items, you should use
1183 * iterator_next
1184 * which lists the items top to bottom (in Z-order).
1185 * For drawing items, you should use
1186 * iterator_prev
1187 * which lists the items bottom to top (in Z-order).
1188 * If you keep iterating over the items after the end-of-items
1189 * marker (-1) is returned, the iterator will start from the
1190 * beginning. Typically, you don't need to test for -1,
1191 * because iterator_{next,prev} will return TRUE if more items
1192 * are to be iterated over, or FALSE otherwise.
1194 * Note: the iterator is defined to be bidirectional. That is,
1195 * any number of prev followed by any number of next, or
1196 * five versa, should leave the iterator at the same item:
1197 * prev * n, next * n = next * n, prev * n
1199 * The iterator has a notion of an out-of-order, special item,
1200 * which sits at the start of the list. This is used in
1201 * LVS_ICON, and LVS_SMALLICON mode to handle the focused item,
1202 * which needs to be first, as it may overlap other items.
1204 * The code is a bit messy because we have:
1205 * - a special item to deal with
1206 * - simple range, or composite range
1207 * - empty range.
1208 * If you find bugs, or want to add features, please make sure you
1209 * always check/modify *both* iterator_prev, and iterator_next.
1212 /****
1213 * This function iterates through the items in increasing order,
1214 * but prefixed by the special item, then -1. That is:
1215 * special, 1, 2, 3, ..., n, -1.
1216 * Each item is listed only once.
1218 static inline BOOL iterator_next(ITERATOR* i)
1220 if (i->nItem == -1)
1222 i->nItem = i->nSpecial;
1223 if (i->nItem != -1) return TRUE;
1225 if (i->nItem == i->nSpecial)
1227 if (i->ranges) i->index = 0;
1228 goto pickarange;
1231 i->nItem++;
1232 testitem:
1233 if (i->nItem == i->nSpecial) i->nItem++;
1234 if (i->nItem < i->range.upper) return TRUE;
1236 pickarange:
1237 if (i->ranges)
1239 if (i->index < DPA_GetPtrCount(i->ranges->hdpa))
1240 i->range = *(RANGE*)DPA_GetPtr(i->ranges->hdpa, i->index++);
1241 else goto end;
1243 else if (i->nItem >= i->range.upper) goto end;
1245 i->nItem = i->range.lower;
1246 if (i->nItem >= 0) goto testitem;
1247 end:
1248 i->nItem = -1;
1249 return FALSE;
1252 /****
1253 * This function iterates through the items in decreasing order,
1254 * followed by the special item, then -1. That is:
1255 * n, n-1, ..., 3, 2, 1, special, -1.
1256 * Each item is listed only once.
1258 static inline BOOL iterator_prev(ITERATOR* i)
1260 BOOL start = FALSE;
1262 if (i->nItem == -1)
1264 start = TRUE;
1265 if (i->ranges) i->index = DPA_GetPtrCount(i->ranges->hdpa);
1266 goto pickarange;
1268 if (i->nItem == i->nSpecial)
1270 i->nItem = -1;
1271 return FALSE;
1274 testitem:
1275 i->nItem--;
1276 if (i->nItem == i->nSpecial) i->nItem--;
1277 if (i->nItem >= i->range.lower) return TRUE;
1279 pickarange:
1280 if (i->ranges)
1282 if (i->index > 0)
1283 i->range = *(RANGE*)DPA_GetPtr(i->ranges->hdpa, --i->index);
1284 else goto end;
1286 else if (!start && i->nItem < i->range.lower) goto end;
1288 i->nItem = i->range.upper;
1289 if (i->nItem > 0) goto testitem;
1290 end:
1291 return (i->nItem = i->nSpecial) != -1;
1294 static RANGE iterator_range(const ITERATOR *i)
1296 RANGE range;
1298 if (!i->ranges) return i->range;
1300 if (DPA_GetPtrCount(i->ranges->hdpa) > 0)
1302 range.lower = (*(RANGE*)DPA_GetPtr(i->ranges->hdpa, 0)).lower;
1303 range.upper = (*(RANGE*)DPA_GetPtr(i->ranges->hdpa, DPA_GetPtrCount(i->ranges->hdpa) - 1)).upper;
1305 else range.lower = range.upper = 0;
1307 return range;
1310 /***
1311 * Releases resources associated with this iterator.
1313 static inline void iterator_destroy(const ITERATOR *i)
1315 ranges_destroy(i->ranges);
1318 /***
1319 * Create an empty iterator.
1321 static inline BOOL iterator_empty(ITERATOR* i)
1323 ZeroMemory(i, sizeof(*i));
1324 i->nItem = i->nSpecial = i->range.lower = i->range.upper = -1;
1325 return TRUE;
1328 /***
1329 * Create an iterator over a range.
1331 static inline BOOL iterator_rangeitems(ITERATOR* i, RANGE range)
1333 iterator_empty(i);
1334 i->range = range;
1335 return TRUE;
1338 /***
1339 * Create an iterator over a bunch of ranges.
1340 * Please note that the iterator will take ownership of the ranges,
1341 * and will free them upon destruction.
1343 static inline BOOL iterator_rangesitems(ITERATOR* i, RANGES ranges)
1345 iterator_empty(i);
1346 i->ranges = ranges;
1347 return TRUE;
1350 /***
1351 * Creates an iterator over the items which intersect frame.
1352 * Uses absolute coordinates rather than compensating for the current offset.
1354 static BOOL iterator_frameditems_absolute(ITERATOR* i, const LISTVIEW_INFO* infoPtr, const RECT *frame)
1356 RECT rcItem, rcTemp;
1358 /* in case we fail, we want to return an empty iterator */
1359 if (!iterator_empty(i)) return FALSE;
1361 TRACE("(frame=%s)\n", wine_dbgstr_rect(frame));
1363 if (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON)
1365 INT nItem;
1367 if (infoPtr->uView == LV_VIEW_ICON && infoPtr->nFocusedItem != -1)
1369 LISTVIEW_GetItemBox(infoPtr, infoPtr->nFocusedItem, &rcItem);
1370 if (IntersectRect(&rcTemp, &rcItem, frame))
1371 i->nSpecial = infoPtr->nFocusedItem;
1373 if (!(iterator_rangesitems(i, ranges_create(50)))) return FALSE;
1374 /* to do better here, we need to have PosX, and PosY sorted */
1375 TRACE("building icon ranges:\n");
1376 for (nItem = 0; nItem < infoPtr->nItemCount; nItem++)
1378 rcItem.left = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosX, nItem);
1379 rcItem.top = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosY, nItem);
1380 rcItem.right = rcItem.left + infoPtr->nItemWidth;
1381 rcItem.bottom = rcItem.top + infoPtr->nItemHeight;
1382 if (IntersectRect(&rcTemp, &rcItem, frame))
1383 ranges_additem(i->ranges, nItem);
1385 return TRUE;
1387 else if (infoPtr->uView == LV_VIEW_DETAILS)
1389 RANGE range;
1391 if (frame->left >= infoPtr->nItemWidth) return TRUE;
1392 if (frame->top >= infoPtr->nItemHeight * infoPtr->nItemCount) return TRUE;
1394 range.lower = max(frame->top / infoPtr->nItemHeight, 0);
1395 range.upper = min((frame->bottom - 1) / infoPtr->nItemHeight, infoPtr->nItemCount - 1) + 1;
1396 if (range.upper <= range.lower) return TRUE;
1397 if (!iterator_rangeitems(i, range)) return FALSE;
1398 TRACE(" report=%s\n", debugrange(&i->range));
1400 else
1402 INT nPerCol = max((infoPtr->rcList.bottom - infoPtr->rcList.top) / infoPtr->nItemHeight, 1);
1403 INT nFirstRow = max(frame->top / infoPtr->nItemHeight, 0);
1404 INT nLastRow = min((frame->bottom - 1) / infoPtr->nItemHeight, nPerCol - 1);
1405 INT nFirstCol;
1406 INT nLastCol;
1407 INT lower;
1408 RANGE item_range;
1409 INT nCol;
1411 if (infoPtr->nItemWidth)
1413 nFirstCol = max(frame->left / infoPtr->nItemWidth, 0);
1414 nLastCol = min((frame->right - 1) / infoPtr->nItemWidth, (infoPtr->nItemCount + nPerCol - 1) / nPerCol);
1416 else
1418 nFirstCol = max(frame->left, 0);
1419 nLastCol = min(frame->right - 1, (infoPtr->nItemCount + nPerCol - 1) / nPerCol);
1422 lower = nFirstCol * nPerCol + nFirstRow;
1424 TRACE("nPerCol=%d, nFirstRow=%d, nLastRow=%d, nFirstCol=%d, nLastCol=%d, lower=%d\n",
1425 nPerCol, nFirstRow, nLastRow, nFirstCol, nLastCol, lower);
1427 if (nLastCol < nFirstCol || nLastRow < nFirstRow) return TRUE;
1429 if (!(iterator_rangesitems(i, ranges_create(nLastCol - nFirstCol + 1)))) return FALSE;
1430 TRACE("building list ranges:\n");
1431 for (nCol = nFirstCol; nCol <= nLastCol; nCol++)
1433 item_range.lower = nCol * nPerCol + nFirstRow;
1434 if(item_range.lower >= infoPtr->nItemCount) break;
1435 item_range.upper = min(nCol * nPerCol + nLastRow + 1, infoPtr->nItemCount);
1436 TRACE(" list=%s\n", debugrange(&item_range));
1437 ranges_add(i->ranges, item_range);
1441 return TRUE;
1444 /***
1445 * Creates an iterator over the items which intersect lprc.
1447 static BOOL iterator_frameditems(ITERATOR* i, const LISTVIEW_INFO* infoPtr, const RECT *lprc)
1449 RECT frame = *lprc;
1450 POINT Origin;
1452 TRACE("(lprc=%s)\n", wine_dbgstr_rect(lprc));
1454 LISTVIEW_GetOrigin(infoPtr, &Origin);
1455 OffsetRect(&frame, -Origin.x, -Origin.y);
1457 return iterator_frameditems_absolute(i, infoPtr, &frame);
1460 /***
1461 * Creates an iterator over the items which intersect the visible region of hdc.
1463 static BOOL iterator_visibleitems(ITERATOR *i, const LISTVIEW_INFO *infoPtr, HDC hdc)
1465 POINT Origin, Position;
1466 RECT rcItem, rcClip;
1467 INT rgntype;
1469 rgntype = GetClipBox(hdc, &rcClip);
1470 if (rgntype == NULLREGION) return iterator_empty(i);
1471 if (!iterator_frameditems(i, infoPtr, &rcClip)) return FALSE;
1472 if (rgntype == SIMPLEREGION) return TRUE;
1474 /* first deal with the special item */
1475 if (i->nSpecial != -1)
1477 LISTVIEW_GetItemBox(infoPtr, i->nSpecial, &rcItem);
1478 if (!RectVisible(hdc, &rcItem)) i->nSpecial = -1;
1481 /* if we can't deal with the region, we'll just go with the simple range */
1482 LISTVIEW_GetOrigin(infoPtr, &Origin);
1483 TRACE("building visible range:\n");
1484 if (!i->ranges && i->range.lower < i->range.upper)
1486 if (!(i->ranges = ranges_create(50))) return TRUE;
1487 if (!ranges_add(i->ranges, i->range))
1489 ranges_destroy(i->ranges);
1490 i->ranges = 0;
1491 return TRUE;
1495 /* now delete the invisible items from the list */
1496 while(iterator_next(i))
1498 LISTVIEW_GetItemOrigin(infoPtr, i->nItem, &Position);
1499 rcItem.left = (infoPtr->uView == LV_VIEW_DETAILS) ? Origin.x : Position.x + Origin.x;
1500 rcItem.top = Position.y + Origin.y;
1501 rcItem.right = rcItem.left + infoPtr->nItemWidth;
1502 rcItem.bottom = rcItem.top + infoPtr->nItemHeight;
1503 if (!RectVisible(hdc, &rcItem))
1504 ranges_delitem(i->ranges, i->nItem);
1506 /* the iterator should restart on the next iterator_next */
1507 TRACE("done\n");
1509 return TRUE;
1512 /* Remove common elements from two iterators */
1513 /* Passed iterators have to point on the first elements */
1514 static BOOL iterator_remove_common_items(ITERATOR *iter1, ITERATOR *iter2)
1516 if(!iter1->ranges || !iter2->ranges) {
1517 int lower, upper;
1519 if(iter1->ranges || iter2->ranges ||
1520 (iter1->range.lower<iter2->range.lower && iter1->range.upper>iter2->range.upper) ||
1521 (iter1->range.lower>iter2->range.lower && iter1->range.upper<iter2->range.upper)) {
1522 ERR("result is not a one range iterator\n");
1523 return FALSE;
1526 if(iter1->range.lower==-1 || iter2->range.lower==-1)
1527 return TRUE;
1529 lower = iter1->range.lower;
1530 upper = iter1->range.upper;
1532 if(lower < iter2->range.lower)
1533 iter1->range.upper = iter2->range.lower;
1534 else if(upper > iter2->range.upper)
1535 iter1->range.lower = iter2->range.upper;
1536 else
1537 iter1->range.lower = iter1->range.upper = -1;
1539 if(iter2->range.lower < lower)
1540 iter2->range.upper = lower;
1541 else if(iter2->range.upper > upper)
1542 iter2->range.lower = upper;
1543 else
1544 iter2->range.lower = iter2->range.upper = -1;
1546 return TRUE;
1549 iterator_next(iter1);
1550 iterator_next(iter2);
1552 while(1) {
1553 if(iter1->nItem==-1 || iter2->nItem==-1)
1554 break;
1556 if(iter1->nItem == iter2->nItem) {
1557 int delete = iter1->nItem;
1559 iterator_prev(iter1);
1560 iterator_prev(iter2);
1561 ranges_delitem(iter1->ranges, delete);
1562 ranges_delitem(iter2->ranges, delete);
1563 iterator_next(iter1);
1564 iterator_next(iter2);
1565 } else if(iter1->nItem > iter2->nItem)
1566 iterator_next(iter2);
1567 else
1568 iterator_next(iter1);
1571 iter1->nItem = iter1->range.lower = iter1->range.upper = -1;
1572 iter2->nItem = iter2->range.lower = iter2->range.upper = -1;
1573 return TRUE;
1576 /******** Misc helper functions ************************************/
1578 static inline LRESULT CallWindowProcT(WNDPROC proc, HWND hwnd, UINT uMsg,
1579 WPARAM wParam, LPARAM lParam, BOOL isW)
1581 if (isW) return CallWindowProcW(proc, hwnd, uMsg, wParam, lParam);
1582 else return CallWindowProcA(proc, hwnd, uMsg, wParam, lParam);
1585 static inline BOOL is_autoarrange(const LISTVIEW_INFO *infoPtr)
1587 return ((infoPtr->dwStyle & LVS_AUTOARRANGE) || infoPtr->bAutoarrange) &&
1588 (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON);
1591 static void toggle_checkbox_state(LISTVIEW_INFO *infoPtr, INT nItem)
1593 DWORD state = STATEIMAGEINDEX(LISTVIEW_GetItemState(infoPtr, nItem, LVIS_STATEIMAGEMASK));
1594 if(state == 1 || state == 2)
1596 LVITEMW lvitem;
1597 state ^= 3;
1598 lvitem.state = INDEXTOSTATEIMAGEMASK(state);
1599 lvitem.stateMask = LVIS_STATEIMAGEMASK;
1600 LISTVIEW_SetItemState(infoPtr, nItem, &lvitem);
1604 /* this should be called after window style got updated,
1605 it used to reset view state to match current window style */
1606 static inline void map_style_view(LISTVIEW_INFO *infoPtr)
1608 switch (infoPtr->dwStyle & LVS_TYPEMASK)
1610 case LVS_ICON:
1611 infoPtr->uView = LV_VIEW_ICON;
1612 break;
1613 case LVS_REPORT:
1614 infoPtr->uView = LV_VIEW_DETAILS;
1615 break;
1616 case LVS_SMALLICON:
1617 infoPtr->uView = LV_VIEW_SMALLICON;
1618 break;
1619 case LVS_LIST:
1620 infoPtr->uView = LV_VIEW_LIST;
1624 /* computes next item id value */
1625 static DWORD get_next_itemid(const LISTVIEW_INFO *infoPtr)
1627 INT count = DPA_GetPtrCount(infoPtr->hdpaItemIds);
1629 if (count > 0)
1631 ITEM_ID *lpID = DPA_GetPtr(infoPtr->hdpaItemIds, count - 1);
1632 return lpID->id + 1;
1634 return 0;
1637 /******** Internal API functions ************************************/
1639 static inline COLUMN_INFO * LISTVIEW_GetColumnInfo(const LISTVIEW_INFO *infoPtr, INT nSubItem)
1641 static COLUMN_INFO mainItem;
1643 if (nSubItem == 0 && DPA_GetPtrCount(infoPtr->hdpaColumns) == 0) return &mainItem;
1644 assert (nSubItem >= 0 && nSubItem < DPA_GetPtrCount(infoPtr->hdpaColumns));
1646 /* update cached column rectangles */
1647 if (infoPtr->colRectsDirty)
1649 COLUMN_INFO *info;
1650 LISTVIEW_INFO *Ptr = (LISTVIEW_INFO*)infoPtr;
1651 INT i;
1653 for (i = 0; i < DPA_GetPtrCount(infoPtr->hdpaColumns); i++) {
1654 info = DPA_GetPtr(infoPtr->hdpaColumns, i);
1655 SendMessageW(infoPtr->hwndHeader, HDM_GETITEMRECT, i, (LPARAM)&info->rcHeader);
1657 Ptr->colRectsDirty = FALSE;
1660 return DPA_GetPtr(infoPtr->hdpaColumns, nSubItem);
1663 static INT LISTVIEW_CreateHeader(LISTVIEW_INFO *infoPtr)
1665 DWORD dFlags = WS_CHILD | HDS_HORZ | HDS_FULLDRAG | HDS_DRAGDROP;
1666 HINSTANCE hInst;
1668 if (infoPtr->hwndHeader) return 0;
1670 TRACE("Creating header for list %p\n", infoPtr->hwndSelf);
1672 /* setup creation flags */
1673 dFlags |= (LVS_NOSORTHEADER & infoPtr->dwStyle) ? 0 : HDS_BUTTONS;
1674 dFlags |= (LVS_NOCOLUMNHEADER & infoPtr->dwStyle) ? HDS_HIDDEN : 0;
1676 hInst = (HINSTANCE)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_HINSTANCE);
1678 /* create header */
1679 infoPtr->hwndHeader = CreateWindowW(WC_HEADERW, NULL, dFlags,
1680 0, 0, 0, 0, infoPtr->hwndSelf, NULL, hInst, NULL);
1681 if (!infoPtr->hwndHeader) return -1;
1683 /* set header unicode format */
1684 SendMessageW(infoPtr->hwndHeader, HDM_SETUNICODEFORMAT, TRUE, 0);
1686 /* set header font */
1687 SendMessageW(infoPtr->hwndHeader, WM_SETFONT, (WPARAM)infoPtr->hFont, TRUE);
1689 /* set header image list */
1690 if (infoPtr->himlSmall)
1691 SendMessageW(infoPtr->hwndHeader, HDM_SETIMAGELIST, 0, (LPARAM)infoPtr->himlSmall);
1693 LISTVIEW_UpdateSize(infoPtr);
1695 return 0;
1698 static inline void LISTVIEW_GetHeaderRect(const LISTVIEW_INFO *infoPtr, INT nSubItem, LPRECT lprc)
1700 *lprc = LISTVIEW_GetColumnInfo(infoPtr, nSubItem)->rcHeader;
1703 static inline BOOL LISTVIEW_IsHeaderEnabled(const LISTVIEW_INFO *infoPtr)
1705 return (infoPtr->uView == LV_VIEW_DETAILS ||
1706 infoPtr->dwLvExStyle & LVS_EX_HEADERINALLVIEWS) &&
1707 !(infoPtr->dwStyle & LVS_NOCOLUMNHEADER);
1710 static inline BOOL LISTVIEW_GetItemW(const LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem)
1712 return LISTVIEW_GetItemT(infoPtr, lpLVItem, TRUE);
1715 /* used to handle collapse main item column case */
1716 static inline BOOL LISTVIEW_DrawFocusRect(const LISTVIEW_INFO *infoPtr, HDC hdc)
1718 return (infoPtr->rcFocus.left < infoPtr->rcFocus.right) ?
1719 DrawFocusRect(hdc, &infoPtr->rcFocus) : FALSE;
1722 /* Listview invalidation functions: use _only_ these functions to invalidate */
1724 static inline BOOL is_redrawing(const LISTVIEW_INFO *infoPtr)
1726 return infoPtr->redraw;
1729 static inline void LISTVIEW_InvalidateRect(const LISTVIEW_INFO *infoPtr, const RECT* rect)
1731 if(!is_redrawing(infoPtr)) return;
1732 TRACE(" invalidating rect=%s\n", wine_dbgstr_rect(rect));
1733 InvalidateRect(infoPtr->hwndSelf, rect, TRUE);
1736 static inline void LISTVIEW_InvalidateItem(const LISTVIEW_INFO *infoPtr, INT nItem)
1738 RECT rcBox;
1740 if(!is_redrawing(infoPtr)) return;
1741 LISTVIEW_GetItemBox(infoPtr, nItem, &rcBox);
1742 LISTVIEW_InvalidateRect(infoPtr, &rcBox);
1745 static inline void LISTVIEW_InvalidateSubItem(const LISTVIEW_INFO *infoPtr, INT nItem, INT nSubItem)
1747 POINT Origin, Position;
1748 RECT rcBox;
1750 if(!is_redrawing(infoPtr)) return;
1751 assert (infoPtr->uView == LV_VIEW_DETAILS);
1752 LISTVIEW_GetOrigin(infoPtr, &Origin);
1753 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
1754 LISTVIEW_GetHeaderRect(infoPtr, nSubItem, &rcBox);
1755 rcBox.top = 0;
1756 rcBox.bottom = infoPtr->nItemHeight;
1757 OffsetRect(&rcBox, Origin.x + Position.x, Origin.y + Position.y);
1758 LISTVIEW_InvalidateRect(infoPtr, &rcBox);
1761 static inline void LISTVIEW_InvalidateList(const LISTVIEW_INFO *infoPtr)
1763 LISTVIEW_InvalidateRect(infoPtr, NULL);
1766 static inline void LISTVIEW_InvalidateColumn(const LISTVIEW_INFO *infoPtr, INT nColumn)
1768 RECT rcCol;
1770 if(!is_redrawing(infoPtr)) return;
1771 LISTVIEW_GetHeaderRect(infoPtr, nColumn, &rcCol);
1772 rcCol.top = infoPtr->rcList.top;
1773 rcCol.bottom = infoPtr->rcList.bottom;
1774 LISTVIEW_InvalidateRect(infoPtr, &rcCol);
1777 /***
1778 * DESCRIPTION:
1779 * Retrieves the number of items that can fit vertically in the client area.
1781 * PARAMETER(S):
1782 * [I] infoPtr : valid pointer to the listview structure
1784 * RETURN:
1785 * Number of items per row.
1787 static inline INT LISTVIEW_GetCountPerRow(const LISTVIEW_INFO *infoPtr)
1789 INT nListWidth = infoPtr->rcList.right - infoPtr->rcList.left;
1791 return max(nListWidth/(infoPtr->nItemWidth ? infoPtr->nItemWidth : 1), 1);
1794 /***
1795 * DESCRIPTION:
1796 * Retrieves the number of items that can fit horizontally in the client
1797 * area.
1799 * PARAMETER(S):
1800 * [I] infoPtr : valid pointer to the listview structure
1802 * RETURN:
1803 * Number of items per column.
1805 static inline INT LISTVIEW_GetCountPerColumn(const LISTVIEW_INFO *infoPtr)
1807 INT nListHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
1809 return max(nListHeight / infoPtr->nItemHeight, 1);
1813 /*************************************************************************
1814 * LISTVIEW_ProcessLetterKeys
1816 * Processes keyboard messages generated by pressing the letter keys
1817 * on the keyboard.
1818 * What this does is perform a case insensitive search from the
1819 * current position with the following quirks:
1820 * - If two chars or more are pressed in quick succession we search
1821 * for the corresponding string (e.g. 'abc').
1822 * - If there is a delay we wipe away the current search string and
1823 * restart with just that char.
1824 * - If the user keeps pressing the same character, whether slowly or
1825 * fast, so that the search string is entirely composed of this
1826 * character ('aaaaa' for instance), then we search for first item
1827 * that starting with that character.
1828 * - If the user types the above character in quick succession, then
1829 * we must also search for the corresponding string ('aaaaa'), and
1830 * go to that string if there is a match.
1832 * PARAMETERS
1833 * [I] hwnd : handle to the window
1834 * [I] charCode : the character code, the actual character
1835 * [I] keyData : key data
1837 * RETURNS
1839 * Zero.
1841 * BUGS
1843 * - The current implementation has a list of characters it will
1844 * accept and it ignores everything else. In particular it will
1845 * ignore accentuated characters which seems to match what
1846 * Windows does. But I'm not sure it makes sense to follow
1847 * Windows there.
1848 * - We don't sound a beep when the search fails.
1850 * SEE ALSO
1852 * TREEVIEW_ProcessLetterKeys
1854 static INT LISTVIEW_ProcessLetterKeys(LISTVIEW_INFO *infoPtr, WPARAM charCode, LPARAM keyData)
1856 WCHAR buffer[MAX_PATH];
1857 DWORD prevTime;
1858 LVITEMW item;
1859 int startidx;
1860 INT nItem;
1861 INT diff;
1863 /* simple parameter checking */
1864 if (!charCode || !keyData || infoPtr->nItemCount == 0) return 0;
1866 /* only allow the valid WM_CHARs through */
1867 if (!isalnumW(charCode) &&
1868 charCode != '.' && charCode != '`' && charCode != '!' &&
1869 charCode != '@' && charCode != '#' && charCode != '$' &&
1870 charCode != '%' && charCode != '^' && charCode != '&' &&
1871 charCode != '*' && charCode != '(' && charCode != ')' &&
1872 charCode != '-' && charCode != '_' && charCode != '+' &&
1873 charCode != '=' && charCode != '\\'&& charCode != ']' &&
1874 charCode != '}' && charCode != '[' && charCode != '{' &&
1875 charCode != '/' && charCode != '?' && charCode != '>' &&
1876 charCode != '<' && charCode != ',' && charCode != '~')
1877 return 0;
1879 /* update the search parameters */
1880 prevTime = infoPtr->lastKeyPressTimestamp;
1881 infoPtr->lastKeyPressTimestamp = GetTickCount();
1882 diff = infoPtr->lastKeyPressTimestamp - prevTime;
1884 if (diff >= 0 && diff < KEY_DELAY)
1886 if (infoPtr->nSearchParamLength < MAX_PATH - 1)
1887 infoPtr->szSearchParam[infoPtr->nSearchParamLength++] = charCode;
1889 if (infoPtr->charCode != charCode)
1890 infoPtr->charCode = charCode = 0;
1892 else
1894 infoPtr->charCode = charCode;
1895 infoPtr->szSearchParam[0] = charCode;
1896 infoPtr->nSearchParamLength = 1;
1899 /* should start from next after focused item, so next item that matches
1900 will be selected, if there isn't any and focused matches it will be selected
1901 on second search stage from beginning of the list */
1902 if (infoPtr->nFocusedItem >= 0 && infoPtr->nItemCount > 1)
1904 /* with some accumulated search data available start with current focus, otherwise
1905 it's excluded from search */
1906 startidx = infoPtr->nSearchParamLength > 1 ? infoPtr->nFocusedItem : infoPtr->nFocusedItem + 1;
1907 if (startidx == infoPtr->nItemCount) startidx = 0;
1909 else
1910 startidx = 0;
1912 /* let application handle this for virtual listview */
1913 if (infoPtr->dwStyle & LVS_OWNERDATA)
1915 NMLVFINDITEMW nmlv;
1917 memset(&nmlv.lvfi, 0, sizeof(nmlv.lvfi));
1918 nmlv.lvfi.flags = (LVFI_WRAP | LVFI_PARTIAL);
1919 nmlv.lvfi.psz = infoPtr->szSearchParam;
1920 nmlv.iStart = startidx;
1922 infoPtr->szSearchParam[infoPtr->nSearchParamLength] = 0;
1924 nItem = notify_hdr(infoPtr, LVN_ODFINDITEMW, (LPNMHDR)&nmlv.hdr);
1926 else
1928 int i = startidx, endidx;
1930 /* and search from the current position */
1931 nItem = -1;
1932 endidx = infoPtr->nItemCount;
1934 /* first search in [startidx, endidx), on failure continue in [0, startidx) */
1935 while (1)
1937 /* start from first item if not found with >= startidx */
1938 if (i == infoPtr->nItemCount && startidx > 0)
1940 endidx = startidx;
1941 startidx = 0;
1944 for (i = startidx; i < endidx; i++)
1946 /* retrieve text */
1947 item.mask = LVIF_TEXT;
1948 item.iItem = i;
1949 item.iSubItem = 0;
1950 item.pszText = buffer;
1951 item.cchTextMax = MAX_PATH;
1952 if (!LISTVIEW_GetItemW(infoPtr, &item)) return 0;
1954 if (!lstrncmpiW(item.pszText, infoPtr->szSearchParam, infoPtr->nSearchParamLength))
1956 nItem = i;
1957 break;
1959 /* this is used to find first char match when search string is not available yet,
1960 otherwise every WM_CHAR will search to next item by first char, ignoring that we're
1961 already waiting for user to complete a string */
1962 else if (nItem == -1 && infoPtr->nSearchParamLength == 1 && !lstrncmpiW(item.pszText, infoPtr->szSearchParam, 1))
1964 /* this would work but we must keep looking for a longer match */
1965 nItem = i;
1969 if ( nItem != -1 || /* found something */
1970 endidx != infoPtr->nItemCount || /* second search done */
1971 (startidx == 0 && endidx == infoPtr->nItemCount) /* full range for first search */ )
1972 break;
1976 if (nItem != -1)
1977 LISTVIEW_KeySelection(infoPtr, nItem, FALSE);
1979 return 0;
1982 /*************************************************************************
1983 * LISTVIEW_UpdateHeaderSize [Internal]
1985 * Function to resize the header control
1987 * PARAMS
1988 * [I] hwnd : handle to a window
1989 * [I] nNewScrollPos : scroll pos to set
1991 * RETURNS
1992 * None.
1994 static void LISTVIEW_UpdateHeaderSize(const LISTVIEW_INFO *infoPtr, INT nNewScrollPos)
1996 RECT winRect;
1997 POINT point[2];
1999 TRACE("nNewScrollPos=%d\n", nNewScrollPos);
2001 if (!infoPtr->hwndHeader) return;
2003 GetWindowRect(infoPtr->hwndHeader, &winRect);
2004 point[0].x = winRect.left;
2005 point[0].y = winRect.top;
2006 point[1].x = winRect.right;
2007 point[1].y = winRect.bottom;
2009 MapWindowPoints(HWND_DESKTOP, infoPtr->hwndSelf, point, 2);
2010 point[0].x = -nNewScrollPos;
2011 point[1].x += nNewScrollPos;
2013 SetWindowPos(infoPtr->hwndHeader,0,
2014 point[0].x,point[0].y,point[1].x,point[1].y,
2015 (infoPtr->dwStyle & LVS_NOCOLUMNHEADER) ? SWP_HIDEWINDOW : SWP_SHOWWINDOW |
2016 SWP_NOZORDER | SWP_NOACTIVATE);
2019 /***
2020 * DESCRIPTION:
2021 * Update the scrollbars. This functions should be called whenever
2022 * the content, size or view changes.
2024 * PARAMETER(S):
2025 * [I] infoPtr : valid pointer to the listview structure
2027 * RETURN:
2028 * None
2030 static void LISTVIEW_UpdateScroll(const LISTVIEW_INFO *infoPtr)
2032 SCROLLINFO horzInfo, vertInfo;
2033 INT dx, dy;
2035 if ((infoPtr->dwStyle & LVS_NOSCROLL) || !is_redrawing(infoPtr)) return;
2037 ZeroMemory(&horzInfo, sizeof(SCROLLINFO));
2038 horzInfo.cbSize = sizeof(SCROLLINFO);
2039 horzInfo.nPage = infoPtr->rcList.right - infoPtr->rcList.left;
2041 /* for now, we'll set info.nMax to the _count_, and adjust it later */
2042 if (infoPtr->uView == LV_VIEW_LIST)
2044 INT nPerCol = LISTVIEW_GetCountPerColumn(infoPtr);
2045 horzInfo.nMax = (infoPtr->nItemCount + nPerCol - 1) / nPerCol;
2047 /* scroll by at least one column per page */
2048 if(horzInfo.nPage < infoPtr->nItemWidth)
2049 horzInfo.nPage = infoPtr->nItemWidth;
2051 if (infoPtr->nItemWidth)
2052 horzInfo.nPage /= infoPtr->nItemWidth;
2054 else if (infoPtr->uView == LV_VIEW_DETAILS)
2056 horzInfo.nMax = infoPtr->nItemWidth;
2058 else /* LV_VIEW_ICON, or LV_VIEW_SMALLICON */
2060 RECT rcView;
2062 if (LISTVIEW_GetViewRect(infoPtr, &rcView)) horzInfo.nMax = rcView.right - rcView.left;
2065 if (LISTVIEW_IsHeaderEnabled(infoPtr))
2067 if (DPA_GetPtrCount(infoPtr->hdpaColumns))
2069 RECT rcHeader;
2070 INT index;
2072 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX,
2073 DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, 0);
2075 LISTVIEW_GetHeaderRect(infoPtr, index, &rcHeader);
2076 horzInfo.nMax = rcHeader.right;
2077 TRACE("horzInfo.nMax=%d\n", horzInfo.nMax);
2081 horzInfo.fMask = SIF_RANGE | SIF_PAGE;
2082 horzInfo.nMax = max(horzInfo.nMax - 1, 0);
2083 dx = GetScrollPos(infoPtr->hwndSelf, SB_HORZ);
2084 dx -= SetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &horzInfo, TRUE);
2085 TRACE("horzInfo=%s\n", debugscrollinfo(&horzInfo));
2087 /* Setting the horizontal scroll can change the listview size
2088 * (and potentially everything else) so we need to recompute
2089 * everything again for the vertical scroll
2092 ZeroMemory(&vertInfo, sizeof(SCROLLINFO));
2093 vertInfo.cbSize = sizeof(SCROLLINFO);
2094 vertInfo.nPage = infoPtr->rcList.bottom - infoPtr->rcList.top;
2096 if (infoPtr->uView == LV_VIEW_DETAILS)
2098 vertInfo.nMax = infoPtr->nItemCount;
2100 /* scroll by at least one page */
2101 if(vertInfo.nPage < infoPtr->nItemHeight)
2102 vertInfo.nPage = infoPtr->nItemHeight;
2104 if (infoPtr->nItemHeight > 0)
2105 vertInfo.nPage /= infoPtr->nItemHeight;
2107 else if (infoPtr->uView != LV_VIEW_LIST) /* LV_VIEW_ICON, or LV_VIEW_SMALLICON */
2109 RECT rcView;
2111 if (LISTVIEW_GetViewRect(infoPtr, &rcView)) vertInfo.nMax = rcView.bottom - rcView.top;
2114 vertInfo.fMask = SIF_RANGE | SIF_PAGE;
2115 vertInfo.nMax = max(vertInfo.nMax - 1, 0);
2116 dy = GetScrollPos(infoPtr->hwndSelf, SB_VERT);
2117 dy -= SetScrollInfo(infoPtr->hwndSelf, SB_VERT, &vertInfo, TRUE);
2118 TRACE("vertInfo=%s\n", debugscrollinfo(&vertInfo));
2120 /* Change of the range may have changed the scroll pos. If so move the content */
2121 if (dx != 0 || dy != 0)
2123 RECT listRect;
2124 listRect = infoPtr->rcList;
2125 ScrollWindowEx(infoPtr->hwndSelf, dx, dy, &listRect, &listRect, 0, 0,
2126 SW_ERASE | SW_INVALIDATE);
2129 /* Update the Header Control */
2130 if (infoPtr->hwndHeader)
2132 horzInfo.fMask = SIF_POS;
2133 GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &horzInfo);
2134 LISTVIEW_UpdateHeaderSize(infoPtr, horzInfo.nPos);
2139 /***
2140 * DESCRIPTION:
2141 * Shows/hides the focus rectangle.
2143 * PARAMETER(S):
2144 * [I] infoPtr : valid pointer to the listview structure
2145 * [I] fShow : TRUE to show the focus, FALSE to hide it.
2147 * RETURN:
2148 * None
2150 static void LISTVIEW_ShowFocusRect(const LISTVIEW_INFO *infoPtr, BOOL fShow)
2152 HDC hdc;
2154 TRACE("fShow=%d, nItem=%d\n", fShow, infoPtr->nFocusedItem);
2156 if (infoPtr->nFocusedItem < 0) return;
2158 /* we need some gymnastics in ICON mode to handle large items */
2159 if (infoPtr->uView == LV_VIEW_ICON)
2161 RECT rcBox;
2163 LISTVIEW_GetItemBox(infoPtr, infoPtr->nFocusedItem, &rcBox);
2164 if ((rcBox.bottom - rcBox.top) > infoPtr->nItemHeight)
2166 LISTVIEW_InvalidateRect(infoPtr, &rcBox);
2167 return;
2171 if (!(hdc = GetDC(infoPtr->hwndSelf))) return;
2173 /* for some reason, owner draw should work only in report mode */
2174 if ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (infoPtr->uView == LV_VIEW_DETAILS))
2176 DRAWITEMSTRUCT dis;
2177 LVITEMW item;
2179 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
2180 HFONT hOldFont = SelectObject(hdc, hFont);
2182 item.iItem = infoPtr->nFocusedItem;
2183 item.iSubItem = 0;
2184 item.mask = LVIF_PARAM;
2185 if (!LISTVIEW_GetItemW(infoPtr, &item)) goto done;
2187 ZeroMemory(&dis, sizeof(dis));
2188 dis.CtlType = ODT_LISTVIEW;
2189 dis.CtlID = (UINT)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
2190 dis.itemID = item.iItem;
2191 dis.itemAction = ODA_FOCUS;
2192 if (fShow) dis.itemState |= ODS_FOCUS;
2193 dis.hwndItem = infoPtr->hwndSelf;
2194 dis.hDC = hdc;
2195 LISTVIEW_GetItemBox(infoPtr, dis.itemID, &dis.rcItem);
2196 dis.itemData = item.lParam;
2198 SendMessageW(infoPtr->hwndNotify, WM_DRAWITEM, dis.CtlID, (LPARAM)&dis);
2200 SelectObject(hdc, hOldFont);
2202 else
2204 LISTVIEW_DrawFocusRect(infoPtr, hdc);
2206 done:
2207 ReleaseDC(infoPtr->hwndSelf, hdc);
2210 /***
2211 * Invalidates all visible selected items.
2213 static void LISTVIEW_InvalidateSelectedItems(const LISTVIEW_INFO *infoPtr)
2215 ITERATOR i;
2217 iterator_frameditems(&i, infoPtr, &infoPtr->rcList);
2218 while(iterator_next(&i))
2220 if (LISTVIEW_GetItemState(infoPtr, i.nItem, LVIS_SELECTED))
2221 LISTVIEW_InvalidateItem(infoPtr, i.nItem);
2223 iterator_destroy(&i);
2227 /***
2228 * DESCRIPTION: [INTERNAL]
2229 * Computes an item's (left,top) corner, relative to rcView.
2230 * That is, the position has NOT been made relative to the Origin.
2231 * This is deliberate, to avoid computing the Origin over, and
2232 * over again, when this function is called in a loop. Instead,
2233 * one can factor the computation of the Origin before the loop,
2234 * and offset the value returned by this function, on every iteration.
2236 * PARAMETER(S):
2237 * [I] infoPtr : valid pointer to the listview structure
2238 * [I] nItem : item number
2239 * [O] lpptOrig : item top, left corner
2241 * RETURN:
2242 * None.
2244 static void LISTVIEW_GetItemOrigin(const LISTVIEW_INFO *infoPtr, INT nItem, LPPOINT lpptPosition)
2246 assert(nItem >= 0 && nItem < infoPtr->nItemCount);
2248 if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
2250 lpptPosition->x = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosX, nItem);
2251 lpptPosition->y = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosY, nItem);
2253 else if (infoPtr->uView == LV_VIEW_LIST)
2255 INT nCountPerColumn = LISTVIEW_GetCountPerColumn(infoPtr);
2256 lpptPosition->x = nItem / nCountPerColumn * infoPtr->nItemWidth;
2257 lpptPosition->y = nItem % nCountPerColumn * infoPtr->nItemHeight;
2259 else /* LV_VIEW_DETAILS */
2261 lpptPosition->x = REPORT_MARGINX;
2262 /* item is always at zero indexed column */
2263 if (DPA_GetPtrCount(infoPtr->hdpaColumns) > 0)
2264 lpptPosition->x += LISTVIEW_GetColumnInfo(infoPtr, 0)->rcHeader.left;
2265 lpptPosition->y = nItem * infoPtr->nItemHeight;
2269 /***
2270 * DESCRIPTION: [INTERNAL]
2271 * Compute the rectangles of an item. This is to localize all
2272 * the computations in one place. If you are not interested in some
2273 * of these values, simply pass in a NULL -- the function is smart
2274 * enough to compute only what's necessary. The function computes
2275 * the standard rectangles (BOUNDS, ICON, LABEL) plus a non-standard
2276 * one, the BOX rectangle. This rectangle is very cheap to compute,
2277 * and is guaranteed to contain all the other rectangles. Computing
2278 * the ICON rect is also cheap, but all the others are potentially
2279 * expensive. This gives an easy and effective optimization when
2280 * searching (like point inclusion, or rectangle intersection):
2281 * first test against the BOX, and if TRUE, test against the desired
2282 * rectangle.
2283 * If the function does not have all the necessary information
2284 * to computed the requested rectangles, will crash with a
2285 * failed assertion. This is done so we catch all programming
2286 * errors, given that the function is called only from our code.
2288 * We have the following 'special' meanings for a few fields:
2289 * * If LVIS_FOCUSED is set, we assume the item has the focus
2290 * This is important in ICON mode, where it might get a larger
2291 * then usual rectangle
2293 * Please note that subitem support works only in REPORT mode.
2295 * PARAMETER(S):
2296 * [I] infoPtr : valid pointer to the listview structure
2297 * [I] lpLVItem : item to compute the measures for
2298 * [O] lprcBox : ptr to Box rectangle
2299 * Same as LVM_GETITEMRECT with LVIR_BOUNDS
2300 * [0] lprcSelectBox : ptr to select box rectangle
2301 * Same as LVM_GETITEMRECT with LVIR_SELECTEDBOUNDS
2302 * [O] lprcIcon : ptr to Icon rectangle
2303 * Same as LVM_GETITEMRECT with LVIR_ICON
2304 * [O] lprcStateIcon: ptr to State Icon rectangle
2305 * [O] lprcLabel : ptr to Label rectangle
2306 * Same as LVM_GETITEMRECT with LVIR_LABEL
2308 * RETURN:
2309 * None.
2311 static void LISTVIEW_GetItemMetrics(const LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem,
2312 LPRECT lprcBox, LPRECT lprcSelectBox,
2313 LPRECT lprcIcon, LPRECT lprcStateIcon, LPRECT lprcLabel)
2315 BOOL doSelectBox = FALSE, doIcon = FALSE, doLabel = FALSE, oversizedBox = FALSE;
2316 RECT Box, SelectBox, Icon, Label;
2317 COLUMN_INFO *lpColumnInfo = NULL;
2318 SIZE labelSize = { 0, 0 };
2320 TRACE("(lpLVItem=%s)\n", debuglvitem_t(lpLVItem, TRUE));
2322 /* Be smart and try to figure out the minimum we have to do */
2323 if (lpLVItem->iSubItem) assert(infoPtr->uView == LV_VIEW_DETAILS);
2324 if (infoPtr->uView == LV_VIEW_ICON && (lprcBox || lprcLabel))
2326 assert((lpLVItem->mask & LVIF_STATE) && (lpLVItem->stateMask & LVIS_FOCUSED));
2327 if (lpLVItem->state & LVIS_FOCUSED) oversizedBox = doLabel = TRUE;
2329 if (lprcSelectBox) doSelectBox = TRUE;
2330 if (lprcLabel) doLabel = TRUE;
2331 if (doLabel || lprcIcon || lprcStateIcon) doIcon = TRUE;
2332 if (doSelectBox)
2334 doIcon = TRUE;
2335 doLabel = TRUE;
2338 /************************************************************/
2339 /* compute the box rectangle (it should be cheap to do) */
2340 /************************************************************/
2341 if (lpLVItem->iSubItem || infoPtr->uView == LV_VIEW_DETAILS)
2342 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, lpLVItem->iSubItem);
2344 if (lpLVItem->iSubItem)
2346 Box = lpColumnInfo->rcHeader;
2348 else
2350 Box.left = 0;
2351 Box.right = infoPtr->nItemWidth;
2353 Box.top = 0;
2354 Box.bottom = infoPtr->nItemHeight;
2356 /******************************************************************/
2357 /* compute ICON bounding box (ala LVM_GETITEMRECT) and STATEICON */
2358 /******************************************************************/
2359 if (doIcon)
2361 LONG state_width = 0;
2363 if (infoPtr->himlState && lpLVItem->iSubItem == 0)
2364 state_width = infoPtr->iconStateSize.cx;
2366 if (infoPtr->uView == LV_VIEW_ICON)
2368 Icon.left = Box.left + state_width;
2369 if (infoPtr->himlNormal)
2370 Icon.left += (infoPtr->nItemWidth - infoPtr->iconSize.cx - state_width) / 2;
2371 Icon.top = Box.top + ICON_TOP_PADDING;
2372 Icon.right = Icon.left;
2373 Icon.bottom = Icon.top;
2374 if (infoPtr->himlNormal)
2376 Icon.right += infoPtr->iconSize.cx;
2377 Icon.bottom += infoPtr->iconSize.cy;
2380 else /* LV_VIEW_SMALLICON, LV_VIEW_LIST or LV_VIEW_DETAILS */
2382 Icon.left = Box.left + state_width;
2384 if (infoPtr->uView == LV_VIEW_DETAILS && lpLVItem->iSubItem == 0)
2386 /* we need the indent in report mode */
2387 assert(lpLVItem->mask & LVIF_INDENT);
2388 Icon.left += infoPtr->iconSize.cx * lpLVItem->iIndent + REPORT_MARGINX;
2391 Icon.top = Box.top;
2392 Icon.right = Icon.left;
2393 if (infoPtr->himlSmall &&
2394 (!lpColumnInfo || lpLVItem->iSubItem == 0 ||
2395 ((infoPtr->dwLvExStyle & LVS_EX_SUBITEMIMAGES) && lpLVItem->iImage != I_IMAGECALLBACK)))
2396 Icon.right += infoPtr->iconSize.cx;
2397 Icon.bottom = Icon.top + infoPtr->iconSize.cy;
2399 if(lprcIcon) *lprcIcon = Icon;
2400 TRACE(" - icon=%s\n", wine_dbgstr_rect(&Icon));
2402 /* TODO: is this correct? */
2403 if (lprcStateIcon)
2405 lprcStateIcon->left = Icon.left - state_width;
2406 lprcStateIcon->right = Icon.left;
2407 lprcStateIcon->top = Icon.top;
2408 lprcStateIcon->bottom = lprcStateIcon->top + infoPtr->iconSize.cy;
2409 TRACE(" - state icon=%s\n", wine_dbgstr_rect(lprcStateIcon));
2412 else Icon.right = 0;
2414 /************************************************************/
2415 /* compute LABEL bounding box (ala LVM_GETITEMRECT) */
2416 /************************************************************/
2417 if (doLabel)
2419 /* calculate how far to the right can the label stretch */
2420 Label.right = Box.right;
2421 if (infoPtr->uView == LV_VIEW_DETAILS)
2423 if (lpLVItem->iSubItem == 0)
2425 /* we need a zero based rect here */
2426 Label = lpColumnInfo->rcHeader;
2427 OffsetRect(&Label, -Label.left, 0);
2431 if (lpLVItem->iSubItem || ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && infoPtr->uView == LV_VIEW_DETAILS))
2433 labelSize.cx = infoPtr->nItemWidth;
2434 labelSize.cy = infoPtr->nItemHeight;
2435 goto calc_label;
2438 /* we need the text in non owner draw mode */
2439 assert(lpLVItem->mask & LVIF_TEXT);
2440 if (is_text(lpLVItem->pszText))
2442 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
2443 HDC hdc = GetDC(infoPtr->hwndSelf);
2444 HFONT hOldFont = SelectObject(hdc, hFont);
2445 UINT uFormat;
2446 RECT rcText;
2448 /* compute rough rectangle where the label will go */
2449 SetRectEmpty(&rcText);
2450 rcText.right = infoPtr->nItemWidth - TRAILING_LABEL_PADDING;
2451 rcText.bottom = infoPtr->nItemHeight;
2452 if (infoPtr->uView == LV_VIEW_ICON)
2453 rcText.bottom -= ICON_TOP_PADDING + infoPtr->iconSize.cy + ICON_BOTTOM_PADDING;
2455 /* now figure out the flags */
2456 if (infoPtr->uView == LV_VIEW_ICON)
2457 uFormat = oversizedBox ? LV_FL_DT_FLAGS : LV_ML_DT_FLAGS;
2458 else
2459 uFormat = LV_SL_DT_FLAGS;
2461 DrawTextW (hdc, lpLVItem->pszText, -1, &rcText, uFormat | DT_CALCRECT);
2463 if (rcText.right != rcText.left)
2464 labelSize.cx = min(rcText.right - rcText.left + TRAILING_LABEL_PADDING, infoPtr->nItemWidth);
2466 labelSize.cy = rcText.bottom - rcText.top;
2468 SelectObject(hdc, hOldFont);
2469 ReleaseDC(infoPtr->hwndSelf, hdc);
2472 calc_label:
2473 if (infoPtr->uView == LV_VIEW_ICON)
2475 Label.left = Box.left + (infoPtr->nItemWidth - labelSize.cx) / 2;
2476 Label.top = Box.top + ICON_TOP_PADDING_HITABLE +
2477 infoPtr->iconSize.cy + ICON_BOTTOM_PADDING;
2478 Label.right = Label.left + labelSize.cx;
2479 Label.bottom = Label.top + infoPtr->nItemHeight;
2480 if (!oversizedBox && labelSize.cy > infoPtr->ntmHeight)
2482 labelSize.cy = min(Box.bottom - Label.top, labelSize.cy);
2483 labelSize.cy /= infoPtr->ntmHeight;
2484 labelSize.cy = max(labelSize.cy, 1);
2485 labelSize.cy *= infoPtr->ntmHeight;
2487 Label.bottom = Label.top + labelSize.cy + HEIGHT_PADDING;
2489 else if (infoPtr->uView == LV_VIEW_DETAILS)
2491 Label.left = Icon.right;
2492 Label.top = Box.top;
2493 Label.right = lpLVItem->iSubItem ? lpColumnInfo->rcHeader.right :
2494 lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left;
2495 Label.bottom = Label.top + infoPtr->nItemHeight;
2497 else /* LV_VIEW_SMALLICON or LV_VIEW_LIST */
2499 Label.left = Icon.right;
2500 Label.top = Box.top;
2501 Label.right = min(Label.left + labelSize.cx, Label.right);
2502 Label.bottom = Label.top + infoPtr->nItemHeight;
2505 if (lprcLabel) *lprcLabel = Label;
2506 TRACE(" - label=%s\n", wine_dbgstr_rect(&Label));
2509 /************************************************************/
2510 /* compute SELECT bounding box */
2511 /************************************************************/
2512 if (doSelectBox)
2514 if (infoPtr->uView == LV_VIEW_DETAILS)
2516 SelectBox.left = Icon.left;
2517 SelectBox.top = Box.top;
2518 SelectBox.bottom = Box.bottom;
2520 if (labelSize.cx)
2521 SelectBox.right = min(Label.left + labelSize.cx, Label.right);
2522 else
2523 SelectBox.right = min(Label.left + MAX_EMPTYTEXT_SELECT_WIDTH, Label.right);
2525 else
2527 UnionRect(&SelectBox, &Icon, &Label);
2529 if (lprcSelectBox) *lprcSelectBox = SelectBox;
2530 TRACE(" - select box=%s\n", wine_dbgstr_rect(&SelectBox));
2533 /* Fix the Box if necessary */
2534 if (lprcBox)
2536 if (oversizedBox) UnionRect(lprcBox, &Box, &Label);
2537 else *lprcBox = Box;
2539 TRACE(" - box=%s\n", wine_dbgstr_rect(&Box));
2542 /***
2543 * DESCRIPTION: [INTERNAL]
2545 * PARAMETER(S):
2546 * [I] infoPtr : valid pointer to the listview structure
2547 * [I] nItem : item number
2548 * [O] lprcBox : ptr to Box rectangle
2550 * RETURN:
2551 * None.
2553 static void LISTVIEW_GetItemBox(const LISTVIEW_INFO *infoPtr, INT nItem, LPRECT lprcBox)
2555 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
2556 POINT Position, Origin;
2557 LVITEMW lvItem;
2559 LISTVIEW_GetOrigin(infoPtr, &Origin);
2560 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
2562 /* Be smart and try to figure out the minimum we have to do */
2563 lvItem.mask = 0;
2564 if (infoPtr->uView == LV_VIEW_ICON && infoPtr->bFocus && LISTVIEW_GetItemState(infoPtr, nItem, LVIS_FOCUSED))
2565 lvItem.mask |= LVIF_TEXT;
2566 lvItem.iItem = nItem;
2567 lvItem.iSubItem = 0;
2568 lvItem.pszText = szDispText;
2569 lvItem.cchTextMax = DISP_TEXT_SIZE;
2570 if (lvItem.mask) LISTVIEW_GetItemW(infoPtr, &lvItem);
2571 if (infoPtr->uView == LV_VIEW_ICON)
2573 lvItem.mask |= LVIF_STATE;
2574 lvItem.stateMask = LVIS_FOCUSED;
2575 lvItem.state = (lvItem.mask & LVIF_TEXT ? LVIS_FOCUSED : 0);
2577 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, lprcBox, 0, 0, 0, 0);
2579 if (infoPtr->uView == LV_VIEW_DETAILS && infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT &&
2580 SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, 0, 0))
2582 OffsetRect(lprcBox, Origin.x, Position.y + Origin.y);
2584 else
2585 OffsetRect(lprcBox, Position.x + Origin.x, Position.y + Origin.y);
2588 /* LISTVIEW_MapIdToIndex helper */
2589 static INT CALLBACK MapIdSearchCompare(LPVOID p1, LPVOID p2, LPARAM lParam)
2591 ITEM_ID *id1 = (ITEM_ID*)p1;
2592 ITEM_ID *id2 = (ITEM_ID*)p2;
2594 if (id1->id == id2->id) return 0;
2596 return (id1->id < id2->id) ? -1 : 1;
2599 /***
2600 * DESCRIPTION:
2601 * Returns the item index for id specified.
2603 * PARAMETER(S):
2604 * [I] infoPtr : valid pointer to the listview structure
2605 * [I] iID : item id to get index for
2607 * RETURN:
2608 * Item index, or -1 on failure.
2610 static INT LISTVIEW_MapIdToIndex(const LISTVIEW_INFO *infoPtr, UINT iID)
2612 ITEM_ID ID;
2613 INT index;
2615 TRACE("iID=%d\n", iID);
2617 if (infoPtr->dwStyle & LVS_OWNERDATA) return -1;
2618 if (infoPtr->nItemCount == 0) return -1;
2620 ID.id = iID;
2621 index = DPA_Search(infoPtr->hdpaItemIds, &ID, -1, MapIdSearchCompare, 0, DPAS_SORTED);
2623 if (index != -1)
2625 ITEM_ID *lpID = DPA_GetPtr(infoPtr->hdpaItemIds, index);
2626 return DPA_GetPtrIndex(infoPtr->hdpaItems, lpID->item);
2629 return -1;
2632 /***
2633 * DESCRIPTION:
2634 * Returns the item id for index given.
2636 * PARAMETER(S):
2637 * [I] infoPtr : valid pointer to the listview structure
2638 * [I] iItem : item index to get id for
2640 * RETURN:
2641 * Item id.
2643 static DWORD LISTVIEW_MapIndexToId(const LISTVIEW_INFO *infoPtr, INT iItem)
2645 ITEM_INFO *lpItem;
2646 HDPA hdpaSubItems;
2648 TRACE("iItem=%d\n", iItem);
2650 if (infoPtr->dwStyle & LVS_OWNERDATA) return -1;
2651 if (iItem < 0 || iItem >= infoPtr->nItemCount) return -1;
2653 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, iItem);
2654 lpItem = DPA_GetPtr(hdpaSubItems, 0);
2656 return lpItem->id->id;
2659 /***
2660 * DESCRIPTION:
2661 * Returns the current icon position, and advances it along the top.
2662 * The returned position is not offset by Origin.
2664 * PARAMETER(S):
2665 * [I] infoPtr : valid pointer to the listview structure
2666 * [O] lpPos : will get the current icon position
2668 * RETURN:
2669 * None
2671 static void LISTVIEW_NextIconPosTop(LISTVIEW_INFO *infoPtr, LPPOINT lpPos)
2673 INT nListWidth = infoPtr->rcList.right - infoPtr->rcList.left;
2675 *lpPos = infoPtr->currIconPos;
2677 infoPtr->currIconPos.x += infoPtr->nItemWidth;
2678 if (infoPtr->currIconPos.x + infoPtr->nItemWidth <= nListWidth) return;
2680 infoPtr->currIconPos.x = 0;
2681 infoPtr->currIconPos.y += infoPtr->nItemHeight;
2685 /***
2686 * DESCRIPTION:
2687 * Returns the current icon position, and advances it down the left edge.
2688 * The returned position is not offset by Origin.
2690 * PARAMETER(S):
2691 * [I] infoPtr : valid pointer to the listview structure
2692 * [O] lpPos : will get the current icon position
2694 * RETURN:
2695 * None
2697 static void LISTVIEW_NextIconPosLeft(LISTVIEW_INFO *infoPtr, LPPOINT lpPos)
2699 INT nListHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
2701 *lpPos = infoPtr->currIconPos;
2703 infoPtr->currIconPos.y += infoPtr->nItemHeight;
2704 if (infoPtr->currIconPos.y + infoPtr->nItemHeight <= nListHeight) return;
2706 infoPtr->currIconPos.x += infoPtr->nItemWidth;
2707 infoPtr->currIconPos.y = 0;
2711 /***
2712 * DESCRIPTION:
2713 * Moves an icon to the specified position.
2714 * It takes care of invalidating the item, etc.
2716 * PARAMETER(S):
2717 * [I] infoPtr : valid pointer to the listview structure
2718 * [I] nItem : the item to move
2719 * [I] lpPos : the new icon position
2720 * [I] isNew : flags the item as being new
2722 * RETURN:
2723 * Success: TRUE
2724 * Failure: FALSE
2726 static BOOL LISTVIEW_MoveIconTo(const LISTVIEW_INFO *infoPtr, INT nItem, const POINT *lppt, BOOL isNew)
2728 POINT old;
2730 if (!isNew)
2732 old.x = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosX, nItem);
2733 old.y = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosY, nItem);
2735 if (lppt->x == old.x && lppt->y == old.y) return TRUE;
2736 LISTVIEW_InvalidateItem(infoPtr, nItem);
2739 /* Allocating a POINTER for every item is too resource intensive,
2740 * so we'll keep the (x,y) in different arrays */
2741 if (!DPA_SetPtr(infoPtr->hdpaPosX, nItem, (void *)(LONG_PTR)lppt->x)) return FALSE;
2742 if (!DPA_SetPtr(infoPtr->hdpaPosY, nItem, (void *)(LONG_PTR)lppt->y)) return FALSE;
2744 LISTVIEW_InvalidateItem(infoPtr, nItem);
2746 return TRUE;
2749 /***
2750 * DESCRIPTION:
2751 * Arranges listview items in icon display mode.
2753 * PARAMETER(S):
2754 * [I] infoPtr : valid pointer to the listview structure
2755 * [I] nAlignCode : alignment code
2757 * RETURN:
2758 * SUCCESS : TRUE
2759 * FAILURE : FALSE
2761 static BOOL LISTVIEW_Arrange(LISTVIEW_INFO *infoPtr, INT nAlignCode)
2763 void (*next_pos)(LISTVIEW_INFO *, LPPOINT);
2764 POINT pos;
2765 INT i;
2767 if (infoPtr->uView != LV_VIEW_ICON && infoPtr->uView != LV_VIEW_SMALLICON) return FALSE;
2769 TRACE("nAlignCode=%d\n", nAlignCode);
2771 if (nAlignCode == LVA_DEFAULT)
2773 if (infoPtr->dwStyle & LVS_ALIGNLEFT) nAlignCode = LVA_ALIGNLEFT;
2774 else nAlignCode = LVA_ALIGNTOP;
2777 switch (nAlignCode)
2779 case LVA_ALIGNLEFT: next_pos = LISTVIEW_NextIconPosLeft; break;
2780 case LVA_ALIGNTOP: next_pos = LISTVIEW_NextIconPosTop; break;
2781 case LVA_SNAPTOGRID: next_pos = LISTVIEW_NextIconPosTop; break; /* FIXME */
2782 default: return FALSE;
2785 infoPtr->bAutoarrange = TRUE;
2786 infoPtr->currIconPos.x = infoPtr->currIconPos.y = 0;
2787 for (i = 0; i < infoPtr->nItemCount; i++)
2789 next_pos(infoPtr, &pos);
2790 LISTVIEW_MoveIconTo(infoPtr, i, &pos, FALSE);
2793 return TRUE;
2796 /***
2797 * DESCRIPTION:
2798 * Retrieves the bounding rectangle of all the items, not offset by Origin.
2799 * For LVS_REPORT always returns empty rectangle.
2801 * PARAMETER(S):
2802 * [I] infoPtr : valid pointer to the listview structure
2803 * [O] lprcView : bounding rectangle
2805 * RETURN:
2806 * SUCCESS : TRUE
2807 * FAILURE : FALSE
2809 static void LISTVIEW_GetAreaRect(const LISTVIEW_INFO *infoPtr, LPRECT lprcView)
2811 INT i, x, y;
2813 SetRectEmpty(lprcView);
2815 switch (infoPtr->uView)
2817 case LV_VIEW_ICON:
2818 case LV_VIEW_SMALLICON:
2819 for (i = 0; i < infoPtr->nItemCount; i++)
2821 x = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosX, i);
2822 y = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosY, i);
2823 lprcView->right = max(lprcView->right, x);
2824 lprcView->bottom = max(lprcView->bottom, y);
2826 if (infoPtr->nItemCount > 0)
2828 lprcView->right += infoPtr->nItemWidth;
2829 lprcView->bottom += infoPtr->nItemHeight;
2831 break;
2833 case LV_VIEW_LIST:
2834 y = LISTVIEW_GetCountPerColumn(infoPtr);
2835 x = infoPtr->nItemCount / y;
2836 if (infoPtr->nItemCount % y) x++;
2837 lprcView->right = x * infoPtr->nItemWidth;
2838 lprcView->bottom = y * infoPtr->nItemHeight;
2839 break;
2843 /***
2844 * DESCRIPTION:
2845 * Retrieves the bounding rectangle of all the items.
2847 * PARAMETER(S):
2848 * [I] infoPtr : valid pointer to the listview structure
2849 * [O] lprcView : bounding rectangle
2851 * RETURN:
2852 * SUCCESS : TRUE
2853 * FAILURE : FALSE
2855 static BOOL LISTVIEW_GetViewRect(const LISTVIEW_INFO *infoPtr, LPRECT lprcView)
2857 POINT ptOrigin;
2859 TRACE("(lprcView=%p)\n", lprcView);
2861 if (!lprcView) return FALSE;
2863 LISTVIEW_GetAreaRect(infoPtr, lprcView);
2865 if (infoPtr->uView != LV_VIEW_DETAILS)
2867 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
2868 OffsetRect(lprcView, ptOrigin.x, ptOrigin.y);
2871 TRACE("lprcView=%s\n", wine_dbgstr_rect(lprcView));
2873 return TRUE;
2876 /***
2877 * DESCRIPTION:
2878 * Retrieves the subitem pointer associated with the subitem index.
2880 * PARAMETER(S):
2881 * [I] hdpaSubItems : DPA handle for a specific item
2882 * [I] nSubItem : index of subitem
2884 * RETURN:
2885 * SUCCESS : subitem pointer
2886 * FAILURE : NULL
2888 static SUBITEM_INFO* LISTVIEW_GetSubItemPtr(HDPA hdpaSubItems, INT nSubItem)
2890 SUBITEM_INFO *lpSubItem;
2891 INT i;
2893 /* we should binary search here if need be */
2894 for (i = 1; i < DPA_GetPtrCount(hdpaSubItems); i++)
2896 lpSubItem = DPA_GetPtr(hdpaSubItems, i);
2897 if (lpSubItem->iSubItem == nSubItem)
2898 return lpSubItem;
2901 return NULL;
2905 /***
2906 * DESCRIPTION:
2907 * Calculates the desired item width.
2909 * PARAMETER(S):
2910 * [I] infoPtr : valid pointer to the listview structure
2912 * RETURN:
2913 * The desired item width.
2915 static INT LISTVIEW_CalculateItemWidth(const LISTVIEW_INFO *infoPtr)
2917 INT nItemWidth = 0;
2919 TRACE("uView=%d\n", infoPtr->uView);
2921 if (infoPtr->uView == LV_VIEW_ICON)
2922 nItemWidth = infoPtr->iconSpacing.cx;
2923 else if (infoPtr->uView == LV_VIEW_DETAILS)
2925 if (DPA_GetPtrCount(infoPtr->hdpaColumns) > 0)
2927 RECT rcHeader;
2928 INT index;
2930 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX,
2931 DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, 0);
2933 LISTVIEW_GetHeaderRect(infoPtr, index, &rcHeader);
2934 nItemWidth = rcHeader.right;
2937 else /* LV_VIEW_SMALLICON, or LV_VIEW_LIST */
2939 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
2940 LVITEMW lvItem;
2941 INT i;
2943 lvItem.mask = LVIF_TEXT;
2944 lvItem.iSubItem = 0;
2946 for (i = 0; i < infoPtr->nItemCount; i++)
2948 lvItem.iItem = i;
2949 lvItem.pszText = szDispText;
2950 lvItem.cchTextMax = DISP_TEXT_SIZE;
2951 if (LISTVIEW_GetItemW(infoPtr, &lvItem))
2952 nItemWidth = max(LISTVIEW_GetStringWidthT(infoPtr, lvItem.pszText, TRUE),
2953 nItemWidth);
2956 if (infoPtr->himlSmall) nItemWidth += infoPtr->iconSize.cx;
2957 if (infoPtr->himlState) nItemWidth += infoPtr->iconStateSize.cx;
2959 nItemWidth = max(DEFAULT_COLUMN_WIDTH, nItemWidth + WIDTH_PADDING);
2962 return nItemWidth;
2965 /***
2966 * DESCRIPTION:
2967 * Calculates the desired item height.
2969 * PARAMETER(S):
2970 * [I] infoPtr : valid pointer to the listview structure
2972 * RETURN:
2973 * The desired item height.
2975 static INT LISTVIEW_CalculateItemHeight(const LISTVIEW_INFO *infoPtr)
2977 INT nItemHeight;
2979 TRACE("uView=%d\n", infoPtr->uView);
2981 if (infoPtr->uView == LV_VIEW_ICON)
2982 nItemHeight = infoPtr->iconSpacing.cy;
2983 else
2985 nItemHeight = infoPtr->ntmHeight;
2986 if (infoPtr->himlState)
2987 nItemHeight = max(nItemHeight, infoPtr->iconStateSize.cy);
2988 if (infoPtr->himlSmall)
2989 nItemHeight = max(nItemHeight, infoPtr->iconSize.cy);
2990 nItemHeight += HEIGHT_PADDING;
2991 if (infoPtr->nMeasureItemHeight > 0)
2992 nItemHeight = infoPtr->nMeasureItemHeight;
2995 return max(nItemHeight, 1);
2998 /***
2999 * DESCRIPTION:
3000 * Updates the width, and height of an item.
3002 * PARAMETER(S):
3003 * [I] infoPtr : valid pointer to the listview structure
3005 * RETURN:
3006 * None.
3008 static inline void LISTVIEW_UpdateItemSize(LISTVIEW_INFO *infoPtr)
3010 infoPtr->nItemWidth = LISTVIEW_CalculateItemWidth(infoPtr);
3011 infoPtr->nItemHeight = LISTVIEW_CalculateItemHeight(infoPtr);
3015 /***
3016 * DESCRIPTION:
3017 * Retrieves and saves important text metrics info for the current
3018 * Listview font.
3020 * PARAMETER(S):
3021 * [I] infoPtr : valid pointer to the listview structure
3024 static void LISTVIEW_SaveTextMetrics(LISTVIEW_INFO *infoPtr)
3026 HDC hdc = GetDC(infoPtr->hwndSelf);
3027 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
3028 HFONT hOldFont = SelectObject(hdc, hFont);
3029 TEXTMETRICW tm;
3030 SIZE sz;
3032 if (GetTextMetricsW(hdc, &tm))
3034 infoPtr->ntmHeight = tm.tmHeight;
3035 infoPtr->ntmMaxCharWidth = tm.tmMaxCharWidth;
3038 if (GetTextExtentPoint32A(hdc, "...", 3, &sz))
3039 infoPtr->nEllipsisWidth = sz.cx;
3041 SelectObject(hdc, hOldFont);
3042 ReleaseDC(infoPtr->hwndSelf, hdc);
3044 TRACE("tmHeight=%d\n", infoPtr->ntmHeight);
3047 /***
3048 * DESCRIPTION:
3049 * A compare function for ranges
3051 * PARAMETER(S)
3052 * [I] range1 : pointer to range 1;
3053 * [I] range2 : pointer to range 2;
3054 * [I] flags : flags
3056 * RETURNS:
3057 * > 0 : if range 1 > range 2
3058 * < 0 : if range 2 > range 1
3059 * = 0 : if range intersects range 2
3061 static INT CALLBACK ranges_cmp(LPVOID range1, LPVOID range2, LPARAM flags)
3063 INT cmp;
3065 if (((RANGE*)range1)->upper <= ((RANGE*)range2)->lower)
3066 cmp = -1;
3067 else if (((RANGE*)range2)->upper <= ((RANGE*)range1)->lower)
3068 cmp = 1;
3069 else
3070 cmp = 0;
3072 TRACE("range1=%s, range2=%s, cmp=%d\n", debugrange(range1), debugrange(range2), cmp);
3074 return cmp;
3077 #define ranges_check(ranges, desc) if (TRACE_ON(listview)) ranges_assert(ranges, desc, __FILE__, __LINE__)
3079 static void ranges_assert(RANGES ranges, LPCSTR desc, const char *file, int line)
3081 INT i;
3082 RANGE *prev, *curr;
3084 TRACE("*** Checking %s:%d:%s ***\n", file, line, desc);
3085 assert (ranges);
3086 assert (DPA_GetPtrCount(ranges->hdpa) >= 0);
3087 ranges_dump(ranges);
3088 if (DPA_GetPtrCount(ranges->hdpa) > 0)
3090 prev = DPA_GetPtr(ranges->hdpa, 0);
3091 assert (prev->lower >= 0 && prev->lower < prev->upper);
3092 for (i = 1; i < DPA_GetPtrCount(ranges->hdpa); i++)
3094 curr = DPA_GetPtr(ranges->hdpa, i);
3095 assert (prev->upper <= curr->lower);
3096 assert (curr->lower < curr->upper);
3097 prev = curr;
3100 TRACE("--- Done checking---\n");
3103 static RANGES ranges_create(int count)
3105 RANGES ranges = Alloc(sizeof(struct tagRANGES));
3106 if (!ranges) return NULL;
3107 ranges->hdpa = DPA_Create(count);
3108 if (ranges->hdpa) return ranges;
3109 Free(ranges);
3110 return NULL;
3113 static void ranges_clear(RANGES ranges)
3115 INT i;
3117 for(i = 0; i < DPA_GetPtrCount(ranges->hdpa); i++)
3118 Free(DPA_GetPtr(ranges->hdpa, i));
3119 DPA_DeleteAllPtrs(ranges->hdpa);
3123 static void ranges_destroy(RANGES ranges)
3125 if (!ranges) return;
3126 ranges_clear(ranges);
3127 DPA_Destroy(ranges->hdpa);
3128 Free(ranges);
3131 static RANGES ranges_clone(RANGES ranges)
3133 RANGES clone;
3134 INT i;
3136 if (!(clone = ranges_create(DPA_GetPtrCount(ranges->hdpa)))) goto fail;
3138 for (i = 0; i < DPA_GetPtrCount(ranges->hdpa); i++)
3140 RANGE *newrng = Alloc(sizeof(RANGE));
3141 if (!newrng) goto fail;
3142 *newrng = *((RANGE*)DPA_GetPtr(ranges->hdpa, i));
3143 if (!DPA_SetPtr(clone->hdpa, i, newrng))
3145 Free(newrng);
3146 goto fail;
3149 return clone;
3151 fail:
3152 TRACE ("clone failed\n");
3153 ranges_destroy(clone);
3154 return NULL;
3157 static RANGES ranges_diff(RANGES ranges, RANGES sub)
3159 INT i;
3161 for (i = 0; i < DPA_GetPtrCount(sub->hdpa); i++)
3162 ranges_del(ranges, *((RANGE *)DPA_GetPtr(sub->hdpa, i)));
3164 return ranges;
3167 static void ranges_dump(RANGES ranges)
3169 INT i;
3171 for (i = 0; i < DPA_GetPtrCount(ranges->hdpa); i++)
3172 TRACE(" %s\n", debugrange(DPA_GetPtr(ranges->hdpa, i)));
3175 static inline BOOL ranges_contain(RANGES ranges, INT nItem)
3177 RANGE srchrng = { nItem, nItem + 1 };
3179 TRACE("(nItem=%d)\n", nItem);
3180 ranges_check(ranges, "before contain");
3181 return DPA_Search(ranges->hdpa, &srchrng, 0, ranges_cmp, 0, DPAS_SORTED) != -1;
3184 static INT ranges_itemcount(RANGES ranges)
3186 INT i, count = 0;
3188 for (i = 0; i < DPA_GetPtrCount(ranges->hdpa); i++)
3190 RANGE *sel = DPA_GetPtr(ranges->hdpa, i);
3191 count += sel->upper - sel->lower;
3194 return count;
3197 static BOOL ranges_shift(RANGES ranges, INT nItem, INT delta, INT nUpper)
3199 RANGE srchrng = { nItem, nItem + 1 }, *chkrng;
3200 INT index;
3202 index = DPA_Search(ranges->hdpa, &srchrng, 0, ranges_cmp, 0, DPAS_SORTED | DPAS_INSERTAFTER);
3203 if (index == -1) return TRUE;
3205 for (; index < DPA_GetPtrCount(ranges->hdpa); index++)
3207 chkrng = DPA_GetPtr(ranges->hdpa, index);
3208 if (chkrng->lower >= nItem)
3209 chkrng->lower = max(min(chkrng->lower + delta, nUpper - 1), 0);
3210 if (chkrng->upper > nItem)
3211 chkrng->upper = max(min(chkrng->upper + delta, nUpper), 0);
3213 return TRUE;
3216 static BOOL ranges_add(RANGES ranges, RANGE range)
3218 RANGE srchrgn;
3219 INT index;
3221 TRACE("(%s)\n", debugrange(&range));
3222 ranges_check(ranges, "before add");
3224 /* try find overlapping regions first */
3225 srchrgn.lower = range.lower - 1;
3226 srchrgn.upper = range.upper + 1;
3227 index = DPA_Search(ranges->hdpa, &srchrgn, 0, ranges_cmp, 0, DPAS_SORTED);
3229 if (index == -1)
3231 RANGE *newrgn;
3233 TRACE("Adding new range\n");
3235 /* create the brand new range to insert */
3236 newrgn = Alloc(sizeof(RANGE));
3237 if(!newrgn) goto fail;
3238 *newrgn = range;
3240 /* figure out where to insert it */
3241 index = DPA_Search(ranges->hdpa, newrgn, 0, ranges_cmp, 0, DPAS_SORTED | DPAS_INSERTAFTER);
3242 TRACE("index=%d\n", index);
3243 if (index == -1) index = 0;
3245 /* and get it over with */
3246 if (DPA_InsertPtr(ranges->hdpa, index, newrgn) == -1)
3248 Free(newrgn);
3249 goto fail;
3252 else
3254 RANGE *chkrgn, *mrgrgn;
3255 INT fromindex, mergeindex;
3257 chkrgn = DPA_GetPtr(ranges->hdpa, index);
3258 TRACE("Merge with %s @%d\n", debugrange(chkrgn), index);
3260 chkrgn->lower = min(range.lower, chkrgn->lower);
3261 chkrgn->upper = max(range.upper, chkrgn->upper);
3263 TRACE("New range %s @%d\n", debugrange(chkrgn), index);
3265 /* merge now common ranges */
3266 fromindex = 0;
3267 srchrgn.lower = chkrgn->lower - 1;
3268 srchrgn.upper = chkrgn->upper + 1;
3272 mergeindex = DPA_Search(ranges->hdpa, &srchrgn, fromindex, ranges_cmp, 0, 0);
3273 if (mergeindex == -1) break;
3274 if (mergeindex == index)
3276 fromindex = index + 1;
3277 continue;
3280 TRACE("Merge with index %i\n", mergeindex);
3282 mrgrgn = DPA_GetPtr(ranges->hdpa, mergeindex);
3283 chkrgn->lower = min(chkrgn->lower, mrgrgn->lower);
3284 chkrgn->upper = max(chkrgn->upper, mrgrgn->upper);
3285 Free(mrgrgn);
3286 DPA_DeletePtr(ranges->hdpa, mergeindex);
3287 if (mergeindex < index) index --;
3288 } while(1);
3291 ranges_check(ranges, "after add");
3292 return TRUE;
3294 fail:
3295 ranges_check(ranges, "failed add");
3296 return FALSE;
3299 static BOOL ranges_del(RANGES ranges, RANGE range)
3301 RANGE *chkrgn;
3302 INT index;
3304 TRACE("(%s)\n", debugrange(&range));
3305 ranges_check(ranges, "before del");
3307 /* we don't use DPAS_SORTED here, since we need *
3308 * to find the first overlapping range */
3309 index = DPA_Search(ranges->hdpa, &range, 0, ranges_cmp, 0, 0);
3310 while(index != -1)
3312 chkrgn = DPA_GetPtr(ranges->hdpa, index);
3314 TRACE("Matches range %s @%d\n", debugrange(chkrgn), index);
3316 /* case 1: Same range */
3317 if ( (chkrgn->upper == range.upper) &&
3318 (chkrgn->lower == range.lower) )
3320 DPA_DeletePtr(ranges->hdpa, index);
3321 Free(chkrgn);
3322 break;
3324 /* case 2: engulf */
3325 else if ( (chkrgn->upper <= range.upper) &&
3326 (chkrgn->lower >= range.lower) )
3328 DPA_DeletePtr(ranges->hdpa, index);
3329 Free(chkrgn);
3331 /* case 3: overlap upper */
3332 else if ( (chkrgn->upper <= range.upper) &&
3333 (chkrgn->lower < range.lower) )
3335 chkrgn->upper = range.lower;
3337 /* case 4: overlap lower */
3338 else if ( (chkrgn->upper > range.upper) &&
3339 (chkrgn->lower >= range.lower) )
3341 chkrgn->lower = range.upper;
3342 break;
3344 /* case 5: fully internal */
3345 else
3347 RANGE *newrgn;
3349 if (!(newrgn = Alloc(sizeof(RANGE)))) goto fail;
3350 newrgn->lower = chkrgn->lower;
3351 newrgn->upper = range.lower;
3352 chkrgn->lower = range.upper;
3353 if (DPA_InsertPtr(ranges->hdpa, index, newrgn) == -1)
3355 Free(newrgn);
3356 goto fail;
3358 break;
3361 index = DPA_Search(ranges->hdpa, &range, index, ranges_cmp, 0, 0);
3364 ranges_check(ranges, "after del");
3365 return TRUE;
3367 fail:
3368 ranges_check(ranges, "failed del");
3369 return FALSE;
3372 /***
3373 * DESCRIPTION:
3374 * Removes all selection ranges
3376 * Parameters(s):
3377 * [I] infoPtr : valid pointer to the listview structure
3378 * [I] toSkip : item range to skip removing the selection
3380 * RETURNS:
3381 * SUCCESS : TRUE
3382 * FAILURE : FALSE
3384 static BOOL LISTVIEW_DeselectAllSkipItems(LISTVIEW_INFO *infoPtr, RANGES toSkip)
3386 LVITEMW lvItem;
3387 ITERATOR i;
3388 RANGES clone;
3390 TRACE("()\n");
3392 lvItem.state = 0;
3393 lvItem.stateMask = LVIS_SELECTED;
3395 /* need to clone the DPA because callbacks can change it */
3396 if (!(clone = ranges_clone(infoPtr->selectionRanges))) return FALSE;
3397 iterator_rangesitems(&i, ranges_diff(clone, toSkip));
3398 while(iterator_next(&i))
3399 LISTVIEW_SetItemState(infoPtr, i.nItem, &lvItem);
3400 /* note that the iterator destructor will free the cloned range */
3401 iterator_destroy(&i);
3403 return TRUE;
3406 static inline BOOL LISTVIEW_DeselectAllSkipItem(LISTVIEW_INFO *infoPtr, INT nItem)
3408 RANGES toSkip;
3410 if (!(toSkip = ranges_create(1))) return FALSE;
3411 if (nItem != -1) ranges_additem(toSkip, nItem);
3412 LISTVIEW_DeselectAllSkipItems(infoPtr, toSkip);
3413 ranges_destroy(toSkip);
3414 return TRUE;
3417 static inline BOOL LISTVIEW_DeselectAll(LISTVIEW_INFO *infoPtr)
3419 return LISTVIEW_DeselectAllSkipItem(infoPtr, -1);
3422 /***
3423 * DESCRIPTION:
3424 * Retrieves the number of items that are marked as selected.
3426 * PARAMETER(S):
3427 * [I] infoPtr : valid pointer to the listview structure
3429 * RETURN:
3430 * Number of items selected.
3432 static INT LISTVIEW_GetSelectedCount(const LISTVIEW_INFO *infoPtr)
3434 INT nSelectedCount = 0;
3436 if (infoPtr->uCallbackMask & LVIS_SELECTED)
3438 INT i;
3439 for (i = 0; i < infoPtr->nItemCount; i++)
3441 if (LISTVIEW_GetItemState(infoPtr, i, LVIS_SELECTED))
3442 nSelectedCount++;
3445 else
3446 nSelectedCount = ranges_itemcount(infoPtr->selectionRanges);
3448 TRACE("nSelectedCount=%d\n", nSelectedCount);
3449 return nSelectedCount;
3452 /***
3453 * DESCRIPTION:
3454 * Manages the item focus.
3456 * PARAMETER(S):
3457 * [I] infoPtr : valid pointer to the listview structure
3458 * [I] nItem : item index
3460 * RETURN:
3461 * TRUE : focused item changed
3462 * FALSE : focused item has NOT changed
3464 static inline BOOL LISTVIEW_SetItemFocus(LISTVIEW_INFO *infoPtr, INT nItem)
3466 INT oldFocus = infoPtr->nFocusedItem;
3467 LVITEMW lvItem;
3469 if (nItem == infoPtr->nFocusedItem) return FALSE;
3471 lvItem.state = nItem == -1 ? 0 : LVIS_FOCUSED;
3472 lvItem.stateMask = LVIS_FOCUSED;
3473 LISTVIEW_SetItemState(infoPtr, nItem == -1 ? infoPtr->nFocusedItem : nItem, &lvItem);
3475 return oldFocus != infoPtr->nFocusedItem;
3478 static INT shift_item(const LISTVIEW_INFO *infoPtr, INT nShiftItem, INT nItem, INT direction)
3480 if (nShiftItem < nItem) return nShiftItem;
3482 if (nShiftItem > nItem) return nShiftItem + direction;
3484 if (direction > 0) return nShiftItem + direction;
3486 return min(nShiftItem, infoPtr->nItemCount - 1);
3489 /* This function updates focus index.
3491 Parameters:
3492 focus : current focus index
3493 item : index of item to be added/removed
3494 direction : add/remove flag
3496 static void LISTVIEW_ShiftFocus(LISTVIEW_INFO *infoPtr, INT focus, INT item, INT direction)
3498 BOOL old_change = infoPtr->bDoChangeNotify;
3500 infoPtr->bDoChangeNotify = FALSE;
3501 focus = shift_item(infoPtr, focus, item, direction);
3502 if (focus != infoPtr->nFocusedItem)
3503 LISTVIEW_SetItemFocus(infoPtr, focus);
3504 infoPtr->bDoChangeNotify = old_change;
3508 * DESCRIPTION:
3509 * Updates the various indices after an item has been inserted or deleted.
3511 * PARAMETER(S):
3512 * [I] infoPtr : valid pointer to the listview structure
3513 * [I] nItem : item index
3514 * [I] direction : Direction of shift, +1 or -1.
3516 * RETURN:
3517 * None
3519 static void LISTVIEW_ShiftIndices(LISTVIEW_INFO *infoPtr, INT nItem, INT direction)
3521 TRACE("Shifting %i, %i steps\n", nItem, direction);
3523 ranges_shift(infoPtr->selectionRanges, nItem, direction, infoPtr->nItemCount);
3524 assert(abs(direction) == 1);
3525 infoPtr->nSelectionMark = shift_item(infoPtr, infoPtr->nSelectionMark, nItem, direction);
3527 /* But we are not supposed to modify nHotItem! */
3531 * DESCRIPTION:
3532 * Adds a block of selections.
3534 * PARAMETER(S):
3535 * [I] infoPtr : valid pointer to the listview structure
3536 * [I] nItem : item index
3538 * RETURN:
3539 * Whether the window is still valid.
3541 static BOOL LISTVIEW_AddGroupSelection(LISTVIEW_INFO *infoPtr, INT nItem)
3543 INT nFirst = min(infoPtr->nSelectionMark, nItem);
3544 INT nLast = max(infoPtr->nSelectionMark, nItem);
3545 HWND hwndSelf = infoPtr->hwndSelf;
3546 NMLVODSTATECHANGE nmlv;
3547 LVITEMW item;
3548 BOOL bOldChange;
3549 INT i;
3551 /* Temporarily disable change notification
3552 * If the control is LVS_OWNERDATA, we need to send
3553 * only one LVN_ODSTATECHANGED notification.
3554 * See MSDN documentation for LVN_ITEMCHANGED.
3556 bOldChange = infoPtr->bDoChangeNotify;
3557 if (infoPtr->dwStyle & LVS_OWNERDATA) infoPtr->bDoChangeNotify = FALSE;
3559 if (nFirst == -1) nFirst = nItem;
3561 item.state = LVIS_SELECTED;
3562 item.stateMask = LVIS_SELECTED;
3564 for (i = nFirst; i <= nLast; i++)
3565 LISTVIEW_SetItemState(infoPtr,i,&item);
3567 ZeroMemory(&nmlv, sizeof(nmlv));
3568 nmlv.iFrom = nFirst;
3569 nmlv.iTo = nLast;
3570 nmlv.uOldState = 0;
3571 nmlv.uNewState = item.state;
3573 notify_hdr(infoPtr, LVN_ODSTATECHANGED, (LPNMHDR)&nmlv);
3574 if (!IsWindow(hwndSelf))
3575 return FALSE;
3576 infoPtr->bDoChangeNotify = bOldChange;
3577 return TRUE;
3581 /***
3582 * DESCRIPTION:
3583 * Sets a single group selection.
3585 * PARAMETER(S):
3586 * [I] infoPtr : valid pointer to the listview structure
3587 * [I] nItem : item index
3589 * RETURN:
3590 * None
3592 static void LISTVIEW_SetGroupSelection(LISTVIEW_INFO *infoPtr, INT nItem)
3594 RANGES selection;
3595 LVITEMW item;
3596 ITERATOR i;
3597 BOOL bOldChange;
3599 if (!(selection = ranges_create(100))) return;
3601 item.state = LVIS_SELECTED;
3602 item.stateMask = LVIS_SELECTED;
3604 if ((infoPtr->uView == LV_VIEW_LIST) || (infoPtr->uView == LV_VIEW_DETAILS))
3606 if (infoPtr->nSelectionMark == -1)
3608 infoPtr->nSelectionMark = nItem;
3609 ranges_additem(selection, nItem);
3611 else
3613 RANGE sel;
3615 sel.lower = min(infoPtr->nSelectionMark, nItem);
3616 sel.upper = max(infoPtr->nSelectionMark, nItem) + 1;
3617 ranges_add(selection, sel);
3620 else
3622 RECT rcItem, rcSel, rcSelMark;
3623 POINT ptItem;
3625 rcItem.left = LVIR_BOUNDS;
3626 if (!LISTVIEW_GetItemRect(infoPtr, nItem, &rcItem)) {
3627 ranges_destroy (selection);
3628 return;
3630 rcSelMark.left = LVIR_BOUNDS;
3631 if (!LISTVIEW_GetItemRect(infoPtr, infoPtr->nSelectionMark, &rcSelMark)) {
3632 ranges_destroy (selection);
3633 return;
3635 UnionRect(&rcSel, &rcItem, &rcSelMark);
3636 iterator_frameditems(&i, infoPtr, &rcSel);
3637 while(iterator_next(&i))
3639 LISTVIEW_GetItemPosition(infoPtr, i.nItem, &ptItem);
3640 if (PtInRect(&rcSel, ptItem)) ranges_additem(selection, i.nItem);
3642 iterator_destroy(&i);
3645 /* disable per item notifications on LVS_OWNERDATA style
3646 FIXME: single LVN_ODSTATECHANGED should be used */
3647 bOldChange = infoPtr->bDoChangeNotify;
3648 if (infoPtr->dwStyle & LVS_OWNERDATA) infoPtr->bDoChangeNotify = FALSE;
3650 LISTVIEW_DeselectAllSkipItems(infoPtr, selection);
3653 iterator_rangesitems(&i, selection);
3654 while(iterator_next(&i))
3655 LISTVIEW_SetItemState(infoPtr, i.nItem, &item);
3656 /* this will also destroy the selection */
3657 iterator_destroy(&i);
3659 infoPtr->bDoChangeNotify = bOldChange;
3661 LISTVIEW_SetItemFocus(infoPtr, nItem);
3664 /***
3665 * DESCRIPTION:
3666 * Sets a single selection.
3668 * PARAMETER(S):
3669 * [I] infoPtr : valid pointer to the listview structure
3670 * [I] nItem : item index
3672 * RETURN:
3673 * None
3675 static void LISTVIEW_SetSelection(LISTVIEW_INFO *infoPtr, INT nItem)
3677 LVITEMW lvItem;
3679 TRACE("nItem=%d\n", nItem);
3681 LISTVIEW_DeselectAllSkipItem(infoPtr, nItem);
3683 lvItem.state = LVIS_FOCUSED | LVIS_SELECTED;
3684 lvItem.stateMask = LVIS_FOCUSED | LVIS_SELECTED;
3685 LISTVIEW_SetItemState(infoPtr, nItem, &lvItem);
3687 infoPtr->nSelectionMark = nItem;
3690 /***
3691 * DESCRIPTION:
3692 * Set selection(s) with keyboard.
3694 * PARAMETER(S):
3695 * [I] infoPtr : valid pointer to the listview structure
3696 * [I] nItem : item index
3697 * [I] space : VK_SPACE code sent
3699 * RETURN:
3700 * SUCCESS : TRUE (needs to be repainted)
3701 * FAILURE : FALSE (nothing has changed)
3703 static BOOL LISTVIEW_KeySelection(LISTVIEW_INFO *infoPtr, INT nItem, BOOL space)
3705 /* FIXME: pass in the state */
3706 WORD wShift = GetKeyState(VK_SHIFT) & 0x8000;
3707 WORD wCtrl = GetKeyState(VK_CONTROL) & 0x8000;
3708 BOOL bResult = FALSE;
3710 TRACE("nItem=%d, wShift=%d, wCtrl=%d\n", nItem, wShift, wCtrl);
3711 if ((nItem >= 0) && (nItem < infoPtr->nItemCount))
3713 bResult = TRUE;
3715 if (infoPtr->dwStyle & LVS_SINGLESEL || (wShift == 0 && wCtrl == 0))
3716 LISTVIEW_SetSelection(infoPtr, nItem);
3717 else
3719 if (wShift)
3720 LISTVIEW_SetGroupSelection(infoPtr, nItem);
3721 else if (wCtrl)
3723 LVITEMW lvItem;
3724 lvItem.state = ~LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED);
3725 lvItem.stateMask = LVIS_SELECTED;
3726 if (space)
3728 LISTVIEW_SetItemState(infoPtr, nItem, &lvItem);
3729 if (lvItem.state & LVIS_SELECTED)
3730 infoPtr->nSelectionMark = nItem;
3732 bResult = LISTVIEW_SetItemFocus(infoPtr, nItem);
3735 LISTVIEW_EnsureVisible(infoPtr, nItem, FALSE);
3738 UpdateWindow(infoPtr->hwndSelf); /* update client area */
3739 return bResult;
3742 static BOOL LISTVIEW_GetItemAtPt(const LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem, POINT pt)
3744 LVHITTESTINFO lvHitTestInfo;
3746 ZeroMemory(&lvHitTestInfo, sizeof(lvHitTestInfo));
3747 lvHitTestInfo.pt.x = pt.x;
3748 lvHitTestInfo.pt.y = pt.y;
3750 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
3752 lpLVItem->mask = LVIF_PARAM;
3753 lpLVItem->iItem = lvHitTestInfo.iItem;
3754 lpLVItem->iSubItem = 0;
3756 return LISTVIEW_GetItemT(infoPtr, lpLVItem, TRUE);
3759 static inline BOOL LISTVIEW_IsHotTracking(const LISTVIEW_INFO *infoPtr)
3761 return ((infoPtr->dwLvExStyle & LVS_EX_TRACKSELECT) ||
3762 (infoPtr->dwLvExStyle & LVS_EX_ONECLICKACTIVATE) ||
3763 (infoPtr->dwLvExStyle & LVS_EX_TWOCLICKACTIVATE));
3766 /***
3767 * DESCRIPTION:
3768 * Called when the mouse is being actively tracked and has hovered for a specified
3769 * amount of time
3771 * PARAMETER(S):
3772 * [I] infoPtr : valid pointer to the listview structure
3773 * [I] fwKeys : key indicator
3774 * [I] x,y : mouse position
3776 * RETURN:
3777 * 0 if the message was processed, non-zero if there was an error
3779 * INFO:
3780 * LVS_EX_TRACKSELECT: An item is automatically selected when the cursor remains
3781 * over the item for a certain period of time.
3784 static LRESULT LISTVIEW_MouseHover(LISTVIEW_INFO *infoPtr, INT x, INT y)
3786 NMHDR hdr;
3788 if (notify_hdr(infoPtr, NM_HOVER, &hdr)) return 0;
3790 if (LISTVIEW_IsHotTracking(infoPtr))
3792 LVITEMW item;
3793 POINT pt;
3795 pt.x = x;
3796 pt.y = y;
3798 if (LISTVIEW_GetItemAtPt(infoPtr, &item, pt))
3799 LISTVIEW_SetSelection(infoPtr, item.iItem);
3801 SetFocus(infoPtr->hwndSelf);
3804 return 0;
3807 #define SCROLL_LEFT 0x1
3808 #define SCROLL_RIGHT 0x2
3809 #define SCROLL_UP 0x4
3810 #define SCROLL_DOWN 0x8
3812 /***
3813 * DESCRIPTION:
3814 * Utility routine to draw and highlight items within a marquee selection rectangle.
3816 * PARAMETER(S):
3817 * [I] infoPtr : valid pointer to the listview structure
3818 * [I] coords_orig : original co-ordinates of the cursor
3819 * [I] coords_offs : offsetted coordinates of the cursor
3820 * [I] offset : offset amount
3821 * [I] scroll : Bitmask of which directions we should scroll, if at all
3823 * RETURN:
3824 * None.
3826 static void LISTVIEW_MarqueeHighlight(LISTVIEW_INFO *infoPtr, const POINT *coords_orig,
3827 const POINT *coords_offs, const POINT *offset,
3828 INT scroll)
3830 BOOL controlDown = FALSE;
3831 LVITEMW item;
3832 ITERATOR old_elems, new_elems;
3833 RECT rect;
3835 if (coords_offs->x > infoPtr->marqueeOrigin.x)
3837 rect.left = infoPtr->marqueeOrigin.x;
3838 rect.right = coords_offs->x;
3840 else
3842 rect.left = coords_offs->x;
3843 rect.right = infoPtr->marqueeOrigin.x;
3846 if (coords_offs->y > infoPtr->marqueeOrigin.y)
3848 rect.top = infoPtr->marqueeOrigin.y;
3849 rect.bottom = coords_offs->y;
3851 else
3853 rect.top = coords_offs->y;
3854 rect.bottom = infoPtr->marqueeOrigin.y;
3857 /* Cancel out the old marquee rectangle and draw the new one */
3858 LISTVIEW_InvalidateRect(infoPtr, &infoPtr->marqueeDrawRect);
3860 /* Scroll by the appropriate distance if applicable - speed up scrolling as
3861 the cursor is further away */
3863 if ((scroll & SCROLL_LEFT) && (coords_orig->x <= 0))
3864 LISTVIEW_Scroll(infoPtr, coords_orig->x, 0);
3866 if ((scroll & SCROLL_RIGHT) && (coords_orig->x >= infoPtr->rcList.right))
3867 LISTVIEW_Scroll(infoPtr, (coords_orig->x - infoPtr->rcList.right), 0);
3869 if ((scroll & SCROLL_UP) && (coords_orig->y <= 0))
3870 LISTVIEW_Scroll(infoPtr, 0, coords_orig->y);
3872 if ((scroll & SCROLL_DOWN) && (coords_orig->y >= infoPtr->rcList.bottom))
3873 LISTVIEW_Scroll(infoPtr, 0, (coords_orig->y - infoPtr->rcList.bottom));
3875 iterator_frameditems_absolute(&old_elems, infoPtr, &infoPtr->marqueeRect);
3877 infoPtr->marqueeRect = rect;
3878 infoPtr->marqueeDrawRect = rect;
3879 OffsetRect(&infoPtr->marqueeDrawRect, offset->x, offset->y);
3881 iterator_frameditems_absolute(&new_elems, infoPtr, &infoPtr->marqueeRect);
3882 iterator_remove_common_items(&old_elems, &new_elems);
3884 /* Iterate over no longer selected items */
3885 while (iterator_next(&old_elems))
3887 if (old_elems.nItem > -1)
3889 if (LISTVIEW_GetItemState(infoPtr, old_elems.nItem, LVIS_SELECTED) == LVIS_SELECTED)
3890 item.state = 0;
3891 else
3892 item.state = LVIS_SELECTED;
3894 item.stateMask = LVIS_SELECTED;
3896 LISTVIEW_SetItemState(infoPtr, old_elems.nItem, &item);
3899 iterator_destroy(&old_elems);
3902 /* Iterate over newly selected items */
3903 if (GetKeyState(VK_CONTROL) & 0x8000)
3904 controlDown = TRUE;
3906 while (iterator_next(&new_elems))
3908 if (new_elems.nItem > -1)
3910 /* If CTRL is pressed, invert. If not, always select the item. */
3911 if ((controlDown) && (LISTVIEW_GetItemState(infoPtr, new_elems.nItem, LVIS_SELECTED)))
3912 item.state = 0;
3913 else
3914 item.state = LVIS_SELECTED;
3916 item.stateMask = LVIS_SELECTED;
3918 LISTVIEW_SetItemState(infoPtr, new_elems.nItem, &item);
3921 iterator_destroy(&new_elems);
3923 LISTVIEW_InvalidateRect(infoPtr, &infoPtr->marqueeDrawRect);
3926 /***
3927 * DESCRIPTION:
3928 * Called when we are in a marquee selection that involves scrolling the listview (ie,
3929 * the cursor is outside the bounds of the client area). This is a TIMERPROC.
3931 * PARAMETER(S):
3932 * [I] hwnd : Handle to the listview
3933 * [I] uMsg : WM_TIMER (ignored)
3934 * [I] idEvent : The timer ID interpreted as a pointer to a LISTVIEW_INFO struct
3935 * [I] dwTimer : The elapsed time (ignored)
3937 * RETURN:
3938 * None.
3940 static VOID CALLBACK LISTVIEW_ScrollTimer(HWND hWnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
3942 LISTVIEW_INFO *infoPtr;
3943 SCROLLINFO scrollInfo;
3944 POINT coords_orig;
3945 POINT coords_offs;
3946 POINT offset;
3947 INT scroll = 0;
3949 infoPtr = (LISTVIEW_INFO *) idEvent;
3951 if (!infoPtr)
3952 return;
3954 /* Get the current cursor position and convert to client coordinates */
3955 GetCursorPos(&coords_orig);
3956 ScreenToClient(hWnd, &coords_orig);
3958 /* Ensure coordinates are within client bounds */
3959 coords_offs.x = max(min(coords_orig.x, infoPtr->rcList.right), 0);
3960 coords_offs.y = max(min(coords_orig.y, infoPtr->rcList.bottom), 0);
3962 /* Get offset */
3963 LISTVIEW_GetOrigin(infoPtr, &offset);
3965 /* Offset coordinates by the appropriate amount */
3966 coords_offs.x -= offset.x;
3967 coords_offs.y -= offset.y;
3969 scrollInfo.cbSize = sizeof(SCROLLINFO);
3970 scrollInfo.fMask = SIF_ALL;
3972 /* Work out in which directions we can scroll */
3973 if (GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
3975 if (scrollInfo.nPos != scrollInfo.nMin)
3976 scroll |= SCROLL_UP;
3978 if (((scrollInfo.nPage + scrollInfo.nPos) - 1) != scrollInfo.nMax)
3979 scroll |= SCROLL_DOWN;
3982 if (GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo))
3984 if (scrollInfo.nPos != scrollInfo.nMin)
3985 scroll |= SCROLL_LEFT;
3987 if (((scrollInfo.nPage + scrollInfo.nPos) - 1) != scrollInfo.nMax)
3988 scroll |= SCROLL_RIGHT;
3991 if (((coords_orig.x <= 0) && (scroll & SCROLL_LEFT)) ||
3992 ((coords_orig.y <= 0) && (scroll & SCROLL_UP)) ||
3993 ((coords_orig.x >= infoPtr->rcList.right) && (scroll & SCROLL_RIGHT)) ||
3994 ((coords_orig.y >= infoPtr->rcList.bottom) && (scroll & SCROLL_DOWN)))
3996 LISTVIEW_MarqueeHighlight(infoPtr, &coords_orig, &coords_offs, &offset, scroll);
4000 /***
4001 * DESCRIPTION:
4002 * Called whenever WM_MOUSEMOVE is received.
4004 * PARAMETER(S):
4005 * [I] infoPtr : valid pointer to the listview structure
4006 * [I] fwKeys : key indicator
4007 * [I] x,y : mouse position
4009 * RETURN:
4010 * 0 if the message is processed, non-zero if there was an error
4012 static LRESULT LISTVIEW_MouseMove(LISTVIEW_INFO *infoPtr, WORD fwKeys, INT x, INT y)
4014 LVHITTESTINFO ht;
4015 RECT rect;
4016 POINT pt;
4018 if (!(fwKeys & MK_LBUTTON))
4019 infoPtr->bLButtonDown = FALSE;
4021 if (infoPtr->bLButtonDown)
4023 rect.left = rect.right = infoPtr->ptClickPos.x;
4024 rect.top = rect.bottom = infoPtr->ptClickPos.y;
4026 InflateRect(&rect, GetSystemMetrics(SM_CXDRAG), GetSystemMetrics(SM_CYDRAG));
4028 if (infoPtr->bMarqueeSelect)
4030 POINT coords_orig;
4031 POINT coords_offs;
4032 POINT offset;
4034 coords_orig.x = x;
4035 coords_orig.y = y;
4037 /* Get offset */
4038 LISTVIEW_GetOrigin(infoPtr, &offset);
4040 /* Ensure coordinates are within client bounds */
4041 coords_offs.x = max(min(x, infoPtr->rcList.right), 0);
4042 coords_offs.y = max(min(y, infoPtr->rcList.bottom), 0);
4044 /* Offset coordinates by the appropriate amount */
4045 coords_offs.x -= offset.x;
4046 coords_offs.y -= offset.y;
4048 /* Enable the timer if we're going outside our bounds, in case the user doesn't
4049 move the mouse again */
4051 if ((x <= 0) || (y <= 0) || (x >= infoPtr->rcList.right) ||
4052 (y >= infoPtr->rcList.bottom))
4054 if (!infoPtr->bScrolling)
4056 infoPtr->bScrolling = TRUE;
4057 SetTimer(infoPtr->hwndSelf, (UINT_PTR) infoPtr, 1, LISTVIEW_ScrollTimer);
4060 else
4062 infoPtr->bScrolling = FALSE;
4063 KillTimer(infoPtr->hwndSelf, (UINT_PTR) infoPtr);
4066 LISTVIEW_MarqueeHighlight(infoPtr, &coords_orig, &coords_offs, &offset, 0);
4067 return 0;
4070 pt.x = x;
4071 pt.y = y;
4073 ht.pt = pt;
4074 LISTVIEW_HitTest(infoPtr, &ht, TRUE, TRUE);
4076 /* reset item marker */
4077 if (infoPtr->nLButtonDownItem != ht.iItem)
4078 infoPtr->nLButtonDownItem = -1;
4080 if (!PtInRect(&rect, pt))
4082 /* this path covers the following:
4083 1. WM_LBUTTONDOWN over selected item (sets focus on it)
4084 2. change focus with keys
4085 3. move mouse over item from step 1 selects it and moves focus on it */
4086 if (infoPtr->nLButtonDownItem != -1 &&
4087 !LISTVIEW_GetItemState(infoPtr, infoPtr->nLButtonDownItem, LVIS_SELECTED))
4089 LVITEMW lvItem;
4091 lvItem.state = LVIS_FOCUSED | LVIS_SELECTED;
4092 lvItem.stateMask = LVIS_FOCUSED | LVIS_SELECTED;
4094 LISTVIEW_SetItemState(infoPtr, infoPtr->nLButtonDownItem, &lvItem);
4095 infoPtr->nLButtonDownItem = -1;
4098 if (!infoPtr->bDragging)
4100 ht.pt = infoPtr->ptClickPos;
4101 LISTVIEW_HitTest(infoPtr, &ht, TRUE, TRUE);
4103 /* If the click is outside the range of an item, begin a
4104 highlight. If not, begin an item drag. */
4105 if (ht.iItem == -1)
4107 NMHDR hdr;
4109 /* If we're allowing multiple selections, send notification.
4110 If return value is non-zero, cancel. */
4111 if (!(infoPtr->dwStyle & LVS_SINGLESEL) && (notify_hdr(infoPtr, LVN_MARQUEEBEGIN, &hdr) == 0))
4113 /* Store the absolute coordinates of the click */
4114 POINT offset;
4115 LISTVIEW_GetOrigin(infoPtr, &offset);
4117 infoPtr->marqueeOrigin.x = infoPtr->ptClickPos.x - offset.x;
4118 infoPtr->marqueeOrigin.y = infoPtr->ptClickPos.y - offset.y;
4120 /* Begin selection and capture mouse */
4121 infoPtr->bMarqueeSelect = TRUE;
4122 SetCapture(infoPtr->hwndSelf);
4125 else
4127 NMLISTVIEW nmlv;
4129 ZeroMemory(&nmlv, sizeof(nmlv));
4130 nmlv.iItem = ht.iItem;
4131 nmlv.ptAction = infoPtr->ptClickPos;
4133 notify_listview(infoPtr, LVN_BEGINDRAG, &nmlv);
4134 infoPtr->bDragging = TRUE;
4138 return 0;
4142 /* see if we are supposed to be tracking mouse hovering */
4143 if (LISTVIEW_IsHotTracking(infoPtr)) {
4144 TRACKMOUSEEVENT trackinfo;
4145 DWORD flags;
4147 trackinfo.cbSize = sizeof(TRACKMOUSEEVENT);
4148 trackinfo.dwFlags = TME_QUERY;
4150 /* see if we are already tracking this hwnd */
4151 _TrackMouseEvent(&trackinfo);
4153 flags = TME_LEAVE;
4154 if(infoPtr->dwLvExStyle & LVS_EX_TRACKSELECT)
4155 flags |= TME_HOVER;
4157 if((trackinfo.dwFlags & flags) != flags || trackinfo.hwndTrack != infoPtr->hwndSelf) {
4158 trackinfo.dwFlags = flags;
4159 trackinfo.dwHoverTime = infoPtr->dwHoverTime;
4160 trackinfo.hwndTrack = infoPtr->hwndSelf;
4162 /* call TRACKMOUSEEVENT so we receive WM_MOUSEHOVER messages */
4163 _TrackMouseEvent(&trackinfo);
4167 return 0;
4171 /***
4172 * Tests whether the item is assignable to a list with style lStyle
4174 static inline BOOL is_assignable_item(const LVITEMW *lpLVItem, LONG lStyle)
4176 if ( (lpLVItem->mask & LVIF_TEXT) &&
4177 (lpLVItem->pszText == LPSTR_TEXTCALLBACKW) &&
4178 (lStyle & (LVS_SORTASCENDING | LVS_SORTDESCENDING)) ) return FALSE;
4180 return TRUE;
4184 /***
4185 * DESCRIPTION:
4186 * Helper for LISTVIEW_SetItemT and LISTVIEW_InsertItemT: sets item attributes.
4188 * PARAMETER(S):
4189 * [I] infoPtr : valid pointer to the listview structure
4190 * [I] lpLVItem : valid pointer to new item attributes
4191 * [I] isNew : the item being set is being inserted
4192 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
4193 * [O] bChanged : will be set to TRUE if the item really changed
4195 * RETURN:
4196 * SUCCESS : TRUE
4197 * FAILURE : FALSE
4199 static BOOL set_main_item(LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem, BOOL isNew, BOOL isW, BOOL *bChanged)
4201 ITEM_INFO *lpItem;
4202 NMLISTVIEW nmlv;
4203 UINT uChanged = 0;
4204 LVITEMW item;
4205 /* stateMask is ignored for LVM_INSERTITEM */
4206 UINT stateMask = isNew ? ~0 : lpLVItem->stateMask;
4208 TRACE("()\n");
4210 assert(lpLVItem->iItem >= 0 && lpLVItem->iItem < infoPtr->nItemCount);
4212 if (lpLVItem->mask == 0) return TRUE;
4214 if (infoPtr->dwStyle & LVS_OWNERDATA)
4216 /* a virtual listview only stores selection and focus */
4217 if (lpLVItem->mask & ~LVIF_STATE)
4218 return FALSE;
4219 lpItem = NULL;
4221 else
4223 HDPA hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, lpLVItem->iItem);
4224 lpItem = DPA_GetPtr(hdpaSubItems, 0);
4225 assert (lpItem);
4228 /* we need to get the lParam and state of the item */
4229 item.iItem = lpLVItem->iItem;
4230 item.iSubItem = lpLVItem->iSubItem;
4231 item.mask = LVIF_STATE | LVIF_PARAM;
4232 item.stateMask = (infoPtr->dwStyle & LVS_OWNERDATA) ? LVIS_FOCUSED | LVIS_SELECTED : ~0;
4234 item.state = 0;
4235 item.lParam = 0;
4236 if (!isNew && !LISTVIEW_GetItemW(infoPtr, &item)) return FALSE;
4238 TRACE("oldState=%x, newState=%x\n", item.state, lpLVItem->state);
4239 /* determine what fields will change */
4240 if ((lpLVItem->mask & LVIF_STATE) && ((item.state ^ lpLVItem->state) & stateMask & ~infoPtr->uCallbackMask))
4241 uChanged |= LVIF_STATE;
4243 if ((lpLVItem->mask & LVIF_IMAGE) && (lpItem->hdr.iImage != lpLVItem->iImage))
4244 uChanged |= LVIF_IMAGE;
4246 if ((lpLVItem->mask & LVIF_PARAM) && (lpItem->lParam != lpLVItem->lParam))
4247 uChanged |= LVIF_PARAM;
4249 if ((lpLVItem->mask & LVIF_INDENT) && (lpItem->iIndent != lpLVItem->iIndent))
4250 uChanged |= LVIF_INDENT;
4252 if ((lpLVItem->mask & LVIF_TEXT) && textcmpWT(lpItem->hdr.pszText, lpLVItem->pszText, isW))
4253 uChanged |= LVIF_TEXT;
4255 TRACE("change mask=0x%x\n", uChanged);
4257 memset(&nmlv, 0, sizeof(NMLISTVIEW));
4258 nmlv.iItem = lpLVItem->iItem;
4259 nmlv.uNewState = (item.state & ~stateMask) | (lpLVItem->state & stateMask);
4260 nmlv.uOldState = item.state;
4261 nmlv.uChanged = uChanged ? uChanged : lpLVItem->mask;
4262 nmlv.lParam = item.lParam;
4264 /* Send LVN_ITEMCHANGING notification, if the item is not being inserted
4265 and we are _NOT_ virtual (LVS_OWNERDATA), and change notifications
4266 are enabled. Even nothing really changed we still need to send this,
4267 in this case uChanged mask is just set to passed item mask. */
4268 if(lpItem && !isNew && infoPtr->bDoChangeNotify)
4270 HWND hwndSelf = infoPtr->hwndSelf;
4272 if (notify_listview(infoPtr, LVN_ITEMCHANGING, &nmlv))
4273 return FALSE;
4274 if (!IsWindow(hwndSelf))
4275 return FALSE;
4278 /* When item is inserted we need to shift existing focus index if new item has lower index. */
4279 if (isNew && (stateMask & ~infoPtr->uCallbackMask & LVIS_FOCUSED) &&
4280 /* this means we won't hit a focus change path later */
4281 ((uChanged & LVIF_STATE) == 0 || (!(lpLVItem->state & LVIS_FOCUSED) && (infoPtr->nFocusedItem != lpLVItem->iItem))))
4283 if (infoPtr->nFocusedItem != -1 && (lpLVItem->iItem <= infoPtr->nFocusedItem))
4284 infoPtr->nFocusedItem++;
4287 if (!uChanged) return TRUE;
4288 *bChanged = TRUE;
4290 /* copy information */
4291 if (lpLVItem->mask & LVIF_TEXT)
4292 textsetptrT(&lpItem->hdr.pszText, lpLVItem->pszText, isW);
4294 if (lpLVItem->mask & LVIF_IMAGE)
4295 lpItem->hdr.iImage = lpLVItem->iImage;
4297 if (lpLVItem->mask & LVIF_PARAM)
4298 lpItem->lParam = lpLVItem->lParam;
4300 if (lpLVItem->mask & LVIF_INDENT)
4301 lpItem->iIndent = lpLVItem->iIndent;
4303 if (uChanged & LVIF_STATE)
4305 if (lpItem && (stateMask & ~infoPtr->uCallbackMask))
4307 lpItem->state &= ~stateMask;
4308 lpItem->state |= (lpLVItem->state & stateMask);
4310 if (lpLVItem->state & stateMask & ~infoPtr->uCallbackMask & LVIS_SELECTED)
4312 if (infoPtr->dwStyle & LVS_SINGLESEL) LISTVIEW_DeselectAllSkipItem(infoPtr, lpLVItem->iItem);
4313 ranges_additem(infoPtr->selectionRanges, lpLVItem->iItem);
4315 else if (stateMask & LVIS_SELECTED)
4317 ranges_delitem(infoPtr->selectionRanges, lpLVItem->iItem);
4319 /* If we are asked to change focus, and we manage it, do it.
4320 It's important to have all new item data stored at this point,
4321 because changing existing focus could result in a redrawing operation,
4322 which in turn could ask for disp data, application should see all data
4323 for inserted item when processing LVN_GETDISPINFO.
4325 The way this works application will see nested item change notifications -
4326 changed item notifications interrupted by ones from item losing focus. */
4327 if (stateMask & ~infoPtr->uCallbackMask & LVIS_FOCUSED)
4329 if (lpLVItem->state & LVIS_FOCUSED)
4331 /* update selection mark */
4332 if (infoPtr->nFocusedItem == -1 && infoPtr->nSelectionMark == -1)
4333 infoPtr->nSelectionMark = lpLVItem->iItem;
4335 if (infoPtr->nFocusedItem != -1)
4337 /* remove current focus */
4338 item.mask = LVIF_STATE;
4339 item.state = 0;
4340 item.stateMask = LVIS_FOCUSED;
4342 /* recurse with redrawing an item */
4343 LISTVIEW_SetItemState(infoPtr, infoPtr->nFocusedItem, &item);
4346 infoPtr->nFocusedItem = lpLVItem->iItem;
4347 LISTVIEW_EnsureVisible(infoPtr, lpLVItem->iItem, infoPtr->uView == LV_VIEW_LIST);
4349 else if (infoPtr->nFocusedItem == lpLVItem->iItem)
4351 infoPtr->nFocusedItem = -1;
4356 /* if we're inserting the item, we're done */
4357 if (isNew) return TRUE;
4359 /* send LVN_ITEMCHANGED notification */
4360 if (lpLVItem->mask & LVIF_PARAM) nmlv.lParam = lpLVItem->lParam;
4361 if (infoPtr->bDoChangeNotify) notify_listview(infoPtr, LVN_ITEMCHANGED, &nmlv);
4363 return TRUE;
4366 /***
4367 * DESCRIPTION:
4368 * Helper for LISTVIEW_{Set,Insert}ItemT *only*: sets subitem attributes.
4370 * PARAMETER(S):
4371 * [I] infoPtr : valid pointer to the listview structure
4372 * [I] lpLVItem : valid pointer to new subitem attributes
4373 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
4374 * [O] bChanged : will be set to TRUE if the item really changed
4376 * RETURN:
4377 * SUCCESS : TRUE
4378 * FAILURE : FALSE
4380 static BOOL set_sub_item(const LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem, BOOL isW, BOOL *bChanged)
4382 HDPA hdpaSubItems;
4383 SUBITEM_INFO *lpSubItem;
4385 /* we do not support subitems for virtual listviews */
4386 if (infoPtr->dwStyle & LVS_OWNERDATA) return FALSE;
4388 /* set subitem only if column is present */
4389 if (lpLVItem->iSubItem >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
4391 /* First do some sanity checks */
4392 /* The LVIF_STATE flag is valid for subitems, but does not appear to be
4393 particularly useful. We currently do not actually do anything with
4394 the flag on subitems.
4396 if (lpLVItem->mask & ~(LVIF_TEXT | LVIF_IMAGE | LVIF_STATE | LVIF_DI_SETITEM)) return FALSE;
4397 if (!(lpLVItem->mask & (LVIF_TEXT | LVIF_IMAGE | LVIF_STATE))) return TRUE;
4399 /* get the subitem structure, and create it if not there */
4400 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, lpLVItem->iItem);
4401 assert (hdpaSubItems);
4403 lpSubItem = LISTVIEW_GetSubItemPtr(hdpaSubItems, lpLVItem->iSubItem);
4404 if (!lpSubItem)
4406 SUBITEM_INFO *tmpSubItem;
4407 INT i;
4409 lpSubItem = Alloc(sizeof(SUBITEM_INFO));
4410 if (!lpSubItem) return FALSE;
4411 /* we could binary search here, if need be...*/
4412 for (i = 1; i < DPA_GetPtrCount(hdpaSubItems); i++)
4414 tmpSubItem = DPA_GetPtr(hdpaSubItems, i);
4415 if (tmpSubItem->iSubItem > lpLVItem->iSubItem) break;
4417 if (DPA_InsertPtr(hdpaSubItems, i, lpSubItem) == -1)
4419 Free(lpSubItem);
4420 return FALSE;
4422 lpSubItem->iSubItem = lpLVItem->iSubItem;
4423 lpSubItem->hdr.iImage = I_IMAGECALLBACK;
4424 *bChanged = TRUE;
4427 if ((lpLVItem->mask & LVIF_IMAGE) && (lpSubItem->hdr.iImage != lpLVItem->iImage))
4429 lpSubItem->hdr.iImage = lpLVItem->iImage;
4430 *bChanged = TRUE;
4433 if ((lpLVItem->mask & LVIF_TEXT) && textcmpWT(lpSubItem->hdr.pszText, lpLVItem->pszText, isW))
4435 textsetptrT(&lpSubItem->hdr.pszText, lpLVItem->pszText, isW);
4436 *bChanged = TRUE;
4439 return TRUE;
4442 /***
4443 * DESCRIPTION:
4444 * Sets item attributes.
4446 * PARAMETER(S):
4447 * [I] infoPtr : valid pointer to the listview structure
4448 * [I] lpLVItem : new item attributes
4449 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
4451 * RETURN:
4452 * SUCCESS : TRUE
4453 * FAILURE : FALSE
4455 static BOOL LISTVIEW_SetItemT(LISTVIEW_INFO *infoPtr, LVITEMW *lpLVItem, BOOL isW)
4457 HWND hwndSelf = infoPtr->hwndSelf;
4458 LPWSTR pszText = NULL;
4459 BOOL bResult, bChanged = FALSE;
4460 RECT oldItemArea;
4462 TRACE("(lpLVItem=%s, isW=%d)\n", debuglvitem_t(lpLVItem, isW), isW);
4464 if (!lpLVItem || lpLVItem->iItem < 0 || lpLVItem->iItem >= infoPtr->nItemCount)
4465 return FALSE;
4467 /* Store old item area */
4468 LISTVIEW_GetItemBox(infoPtr, lpLVItem->iItem, &oldItemArea);
4470 /* For efficiency, we transform the lpLVItem->pszText to Unicode here */
4471 if ((lpLVItem->mask & LVIF_TEXT) && is_text(lpLVItem->pszText))
4473 pszText = lpLVItem->pszText;
4474 lpLVItem->pszText = textdupTtoW(lpLVItem->pszText, isW);
4477 /* actually set the fields */
4478 if (!is_assignable_item(lpLVItem, infoPtr->dwStyle)) return FALSE;
4480 if (lpLVItem->iSubItem)
4481 bResult = set_sub_item(infoPtr, lpLVItem, TRUE, &bChanged);
4482 else
4483 bResult = set_main_item(infoPtr, lpLVItem, FALSE, TRUE, &bChanged);
4484 if (!IsWindow(hwndSelf))
4485 return FALSE;
4487 /* redraw item, if necessary */
4488 if (bChanged && !infoPtr->bIsDrawing)
4490 /* this little optimization eliminates some nasty flicker */
4491 if ( infoPtr->uView == LV_VIEW_DETAILS && !(infoPtr->dwStyle & LVS_OWNERDRAWFIXED) &&
4492 !(infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) &&
4493 lpLVItem->iSubItem > 0 && lpLVItem->iSubItem <= DPA_GetPtrCount(infoPtr->hdpaColumns) )
4494 LISTVIEW_InvalidateSubItem(infoPtr, lpLVItem->iItem, lpLVItem->iSubItem);
4495 else
4497 LISTVIEW_InvalidateRect(infoPtr, &oldItemArea);
4498 LISTVIEW_InvalidateItem(infoPtr, lpLVItem->iItem);
4501 /* restore text */
4502 if (pszText)
4504 textfreeT(lpLVItem->pszText, isW);
4505 lpLVItem->pszText = pszText;
4508 return bResult;
4511 /***
4512 * DESCRIPTION:
4513 * Retrieves the index of the item at coordinate (0, 0) of the client area.
4515 * PARAMETER(S):
4516 * [I] infoPtr : valid pointer to the listview structure
4518 * RETURN:
4519 * item index
4521 static INT LISTVIEW_GetTopIndex(const LISTVIEW_INFO *infoPtr)
4523 INT nItem = 0;
4524 SCROLLINFO scrollInfo;
4526 scrollInfo.cbSize = sizeof(SCROLLINFO);
4527 scrollInfo.fMask = SIF_POS;
4529 if (infoPtr->uView == LV_VIEW_LIST)
4531 if (GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo))
4532 nItem = scrollInfo.nPos * LISTVIEW_GetCountPerColumn(infoPtr);
4534 else if (infoPtr->uView == LV_VIEW_DETAILS)
4536 if (GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
4537 nItem = scrollInfo.nPos;
4539 else
4541 if (GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
4542 nItem = LISTVIEW_GetCountPerRow(infoPtr) * (scrollInfo.nPos / infoPtr->nItemHeight);
4545 TRACE("nItem=%d\n", nItem);
4547 return nItem;
4551 /***
4552 * DESCRIPTION:
4553 * Erases the background of the given rectangle
4555 * PARAMETER(S):
4556 * [I] infoPtr : valid pointer to the listview structure
4557 * [I] hdc : device context handle
4558 * [I] lprcBox : clipping rectangle
4560 * RETURN:
4561 * Success: TRUE
4562 * Failure: FALSE
4564 static inline BOOL LISTVIEW_FillBkgnd(const LISTVIEW_INFO *infoPtr, HDC hdc, const RECT *lprcBox)
4566 if (!infoPtr->hBkBrush) return FALSE;
4568 TRACE("(hdc=%p, lprcBox=%s, hBkBrush=%p)\n", hdc, wine_dbgstr_rect(lprcBox), infoPtr->hBkBrush);
4570 return FillRect(hdc, lprcBox, infoPtr->hBkBrush);
4573 /* Draw main item or subitem */
4574 static void LISTVIEW_DrawItemPart(LISTVIEW_INFO *infoPtr, LVITEMW *item, const NMLVCUSTOMDRAW *nmlvcd, const POINT *pos)
4576 RECT rcSelect, rcLabel, rcBox, rcStateIcon, rcIcon;
4577 const RECT *background;
4578 HIMAGELIST himl;
4579 UINT format;
4580 RECT *focus;
4582 /* now check if we need to update the focus rectangle */
4583 focus = infoPtr->bFocus && (item->state & LVIS_FOCUSED) ? &infoPtr->rcFocus : 0;
4584 if (!focus) item->state &= ~LVIS_FOCUSED;
4586 LISTVIEW_GetItemMetrics(infoPtr, item, &rcBox, &rcSelect, &rcIcon, &rcStateIcon, &rcLabel);
4587 OffsetRect(&rcBox, pos->x, pos->y);
4588 OffsetRect(&rcSelect, pos->x, pos->y);
4589 OffsetRect(&rcIcon, pos->x, pos->y);
4590 OffsetRect(&rcStateIcon, pos->x, pos->y);
4591 OffsetRect(&rcLabel, pos->x, pos->y);
4592 TRACE("%d: box=%s, select=%s, icon=%s. label=%s\n", item->iSubItem,
4593 wine_dbgstr_rect(&rcBox), wine_dbgstr_rect(&rcSelect),
4594 wine_dbgstr_rect(&rcIcon), wine_dbgstr_rect(&rcLabel));
4596 /* FIXME: temporary hack */
4597 rcSelect.left = rcLabel.left;
4599 if (infoPtr->uView == LV_VIEW_DETAILS && item->iSubItem == 0)
4601 if (!(infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT))
4602 OffsetRect(&rcSelect, LISTVIEW_GetColumnInfo(infoPtr, 0)->rcHeader.left, 0);
4603 OffsetRect(&rcIcon, LISTVIEW_GetColumnInfo(infoPtr, 0)->rcHeader.left, 0);
4604 OffsetRect(&rcStateIcon, LISTVIEW_GetColumnInfo(infoPtr, 0)->rcHeader.left, 0);
4605 OffsetRect(&rcLabel, LISTVIEW_GetColumnInfo(infoPtr, 0)->rcHeader.left, 0);
4608 /* in icon mode, the label rect is really what we want to draw the
4609 * background for */
4610 /* in detail mode, we want to paint background for label rect when
4611 * item is not selected or listview has full row select; otherwise paint
4612 * background for text only */
4613 if ( infoPtr->uView == LV_VIEW_ICON ||
4614 (infoPtr->uView == LV_VIEW_DETAILS && (!(item->state & LVIS_SELECTED) ||
4615 (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT))))
4616 background = &rcLabel;
4617 else
4618 background = &rcSelect;
4620 if (nmlvcd->clrTextBk != CLR_NONE)
4621 ExtTextOutW(nmlvcd->nmcd.hdc, background->left, background->top, ETO_OPAQUE, background, NULL, 0, NULL);
4623 if (item->state & LVIS_FOCUSED)
4625 if (infoPtr->uView == LV_VIEW_DETAILS)
4627 if (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT)
4629 /* we have to update left focus bound too if item isn't in leftmost column
4630 and reduce right box bound */
4631 if (DPA_GetPtrCount(infoPtr->hdpaColumns) > 0)
4633 INT leftmost;
4635 if ((leftmost = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, 0, 0)))
4637 INT Originx = pos->x - LISTVIEW_GetColumnInfo(infoPtr, leftmost)->rcHeader.left;
4638 INT rightmost = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX,
4639 DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, 0);
4641 rcBox.right = LISTVIEW_GetColumnInfo(infoPtr, rightmost)->rcHeader.right + Originx;
4642 rcSelect.left = LISTVIEW_GetColumnInfo(infoPtr, leftmost)->rcHeader.left + Originx;
4645 rcSelect.right = rcBox.right;
4647 infoPtr->rcFocus = rcSelect;
4649 else
4650 infoPtr->rcFocus = rcLabel;
4653 /* state icons */
4654 if (infoPtr->himlState && STATEIMAGEINDEX(item->state) && (item->iSubItem == 0))
4656 UINT stateimage = STATEIMAGEINDEX(item->state);
4657 if (stateimage)
4659 TRACE("stateimage=%d\n", stateimage);
4660 ImageList_Draw(infoPtr->himlState, stateimage-1, nmlvcd->nmcd.hdc, rcStateIcon.left, rcStateIcon.top, ILD_NORMAL);
4664 /* item icons */
4665 himl = (infoPtr->uView == LV_VIEW_ICON ? infoPtr->himlNormal : infoPtr->himlSmall);
4666 if (himl && item->iImage >= 0 && !IsRectEmpty(&rcIcon))
4668 UINT style;
4670 TRACE("iImage=%d\n", item->iImage);
4672 if (item->state & (LVIS_SELECTED | LVIS_CUT) && infoPtr->bFocus)
4673 style = ILD_SELECTED;
4674 else
4675 style = ILD_NORMAL;
4677 ImageList_DrawEx(himl, item->iImage, nmlvcd->nmcd.hdc, rcIcon.left, rcIcon.top,
4678 rcIcon.right - rcIcon.left, rcIcon.bottom - rcIcon.top, infoPtr->clrBk,
4679 item->state & LVIS_CUT ? RGB(255, 255, 255) : CLR_DEFAULT,
4680 style | (item->state & LVIS_OVERLAYMASK));
4683 /* Don't bother painting item being edited */
4684 if (infoPtr->hwndEdit && item->iItem == infoPtr->nEditLabelItem && item->iSubItem == 0) return;
4686 /* figure out the text drawing flags */
4687 format = (infoPtr->uView == LV_VIEW_ICON ? (focus ? LV_FL_DT_FLAGS : LV_ML_DT_FLAGS) : LV_SL_DT_FLAGS);
4688 if (infoPtr->uView == LV_VIEW_ICON)
4689 format = (focus ? LV_FL_DT_FLAGS : LV_ML_DT_FLAGS);
4690 else if (item->iSubItem)
4692 switch (LISTVIEW_GetColumnInfo(infoPtr, item->iSubItem)->fmt & LVCFMT_JUSTIFYMASK)
4694 case LVCFMT_RIGHT: format |= DT_RIGHT; break;
4695 case LVCFMT_CENTER: format |= DT_CENTER; break;
4696 default: format |= DT_LEFT;
4699 if (!(format & (DT_RIGHT | DT_CENTER)))
4701 if (himl && item->iImage >= 0 && !IsRectEmpty(&rcIcon)) rcLabel.left += IMAGE_PADDING;
4702 else rcLabel.left += LABEL_HOR_PADDING;
4704 else if (format & DT_RIGHT) rcLabel.right -= LABEL_HOR_PADDING;
4706 /* for GRIDLINES reduce the bottom so the text formats correctly */
4707 if (infoPtr->uView == LV_VIEW_DETAILS && infoPtr->dwLvExStyle & LVS_EX_GRIDLINES)
4708 rcLabel.bottom--;
4710 DrawTextW(nmlvcd->nmcd.hdc, item->pszText, -1, &rcLabel, format);
4713 /***
4714 * DESCRIPTION:
4715 * Draws an item.
4717 * PARAMETER(S):
4718 * [I] infoPtr : valid pointer to the listview structure
4719 * [I] hdc : device context handle
4720 * [I] nItem : item index
4721 * [I] nSubItem : subitem index
4722 * [I] pos : item position in client coordinates
4723 * [I] cdmode : custom draw mode
4725 * RETURN:
4726 * Success: TRUE
4727 * Failure: FALSE
4729 static BOOL LISTVIEW_DrawItem(LISTVIEW_INFO *infoPtr, HDC hdc, INT nItem, ITERATOR *subitems, POINT pos, DWORD cdmode)
4731 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
4732 static WCHAR callbackW[] = { '(', 'c', 'a', 'l', 'l', 'b', 'a', 'c', 'k', ')', 0 };
4733 DWORD cdsubitemmode = CDRF_DODEFAULT;
4734 RECT *focus, rcBox;
4735 NMLVCUSTOMDRAW nmlvcd;
4736 LVITEMW lvItem;
4738 TRACE("(hdc=%p, nItem=%d, subitems=%p, pos=%s)\n", hdc, nItem, subitems, wine_dbgstr_point(&pos));
4740 /* get information needed for drawing the item */
4741 lvItem.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_PARAM | LVIF_STATE;
4742 if (infoPtr->uView == LV_VIEW_DETAILS) lvItem.mask |= LVIF_INDENT;
4743 lvItem.stateMask = LVIS_SELECTED | LVIS_FOCUSED | LVIS_STATEIMAGEMASK | LVIS_CUT | LVIS_OVERLAYMASK;
4744 lvItem.iItem = nItem;
4745 lvItem.iSubItem = 0;
4746 lvItem.state = 0;
4747 lvItem.lParam = 0;
4748 lvItem.cchTextMax = DISP_TEXT_SIZE;
4749 lvItem.pszText = szDispText;
4750 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return FALSE;
4751 if (lvItem.pszText == LPSTR_TEXTCALLBACKW) lvItem.pszText = callbackW;
4752 TRACE(" lvItem=%s\n", debuglvitem_t(&lvItem, TRUE));
4754 /* now check if we need to update the focus rectangle */
4755 focus = infoPtr->bFocus && (lvItem.state & LVIS_FOCUSED) ? &infoPtr->rcFocus : 0;
4756 if (!focus) lvItem.state &= ~LVIS_FOCUSED;
4758 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, &rcBox, NULL, NULL, NULL, NULL);
4759 OffsetRect(&rcBox, pos.x, pos.y);
4761 /* Full custom draw stage sequence looks like this:
4763 LV_VIEW_DETAILS:
4765 - CDDS_ITEMPREPAINT
4766 - CDDS_ITEMPREPAINT|CDDS_SUBITEM | => sent n times, where n is number of subitems,
4767 CDDS_ITEMPOSTPAINT|CDDS_SUBITEM | including item itself
4768 - CDDS_ITEMPOSTPAINT
4770 other styles:
4772 - CDDS_ITEMPREPAINT
4773 - CDDS_ITEMPOSTPAINT
4776 /* fill in the custom draw structure */
4777 customdraw_fill(&nmlvcd, infoPtr, hdc, &rcBox, &lvItem);
4778 if (cdmode & CDRF_NOTIFYITEMDRAW)
4779 cdsubitemmode = notify_customdraw(infoPtr, CDDS_ITEMPREPAINT, &nmlvcd);
4780 if (cdsubitemmode & CDRF_SKIPDEFAULT) goto postpaint;
4782 if (subitems)
4784 while (iterator_next(subitems))
4786 DWORD subitemstage = CDRF_DODEFAULT;
4788 /* We need to query for each subitem, item's data (subitem == 0) is already here at this point */
4789 if (subitems->nItem)
4791 lvItem.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_PARAM | LVIF_INDENT;
4792 lvItem.stateMask = LVIS_SELECTED | LVIS_FOCUSED | LVIS_STATEIMAGEMASK | LVIS_CUT | LVIS_OVERLAYMASK;
4793 lvItem.iItem = nItem;
4794 lvItem.iSubItem = subitems->nItem;
4795 lvItem.state = 0;
4796 lvItem.lParam = 0;
4797 lvItem.cchTextMax = DISP_TEXT_SIZE;
4798 lvItem.pszText = szDispText;
4799 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return FALSE;
4800 if (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT)
4801 lvItem.state = LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED);
4802 if (lvItem.pszText == LPSTR_TEXTCALLBACKW) lvItem.pszText = callbackW;
4803 TRACE(" lvItem=%s\n", debuglvitem_t(&lvItem, TRUE));
4805 /* update custom draw data */
4806 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, &nmlvcd.nmcd.rc, NULL, NULL, NULL, NULL);
4807 OffsetRect(&nmlvcd.nmcd.rc, pos.x, pos.y);
4808 nmlvcd.iSubItem = subitems->nItem;
4811 if (cdsubitemmode & CDRF_NOTIFYSUBITEMDRAW)
4812 subitemstage = notify_customdraw(infoPtr, CDDS_SUBITEM | CDDS_ITEMPREPAINT, &nmlvcd);
4813 else
4815 nmlvcd.clrTextBk = infoPtr->clrTextBk;
4816 nmlvcd.clrText = infoPtr->clrText;
4819 if (subitems->nItem == 0 || (cdmode & CDRF_NOTIFYITEMDRAW))
4820 prepaint_setup(infoPtr, hdc, &nmlvcd, FALSE);
4821 else if (!(infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT))
4822 prepaint_setup(infoPtr, hdc, &nmlvcd, TRUE);
4824 if (!(subitemstage & CDRF_SKIPDEFAULT))
4825 LISTVIEW_DrawItemPart(infoPtr, &lvItem, &nmlvcd, &pos);
4827 if (subitemstage & CDRF_NOTIFYPOSTPAINT)
4828 subitemstage = notify_customdraw(infoPtr, CDDS_SUBITEM | CDDS_ITEMPOSTPAINT, &nmlvcd);
4831 else
4833 prepaint_setup(infoPtr, hdc, &nmlvcd, FALSE);
4834 LISTVIEW_DrawItemPart(infoPtr, &lvItem, &nmlvcd, &pos);
4837 postpaint:
4838 if (cdsubitemmode & CDRF_NOTIFYPOSTPAINT)
4840 nmlvcd.iSubItem = 0;
4841 notify_customdraw(infoPtr, CDDS_ITEMPOSTPAINT, &nmlvcd);
4844 return TRUE;
4847 /***
4848 * DESCRIPTION:
4849 * Draws listview items when in owner draw mode.
4851 * PARAMETER(S):
4852 * [I] infoPtr : valid pointer to the listview structure
4853 * [I] hdc : device context handle
4855 * RETURN:
4856 * None
4858 static void LISTVIEW_RefreshOwnerDraw(const LISTVIEW_INFO *infoPtr, ITERATOR *i, HDC hdc, DWORD cdmode)
4860 UINT uID = (UINT)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
4861 DWORD cditemmode = CDRF_DODEFAULT;
4862 NMLVCUSTOMDRAW nmlvcd;
4863 POINT Origin, Position;
4864 DRAWITEMSTRUCT dis;
4865 LVITEMW item;
4867 TRACE("()\n");
4869 ZeroMemory(&dis, sizeof(dis));
4871 /* Get scroll info once before loop */
4872 LISTVIEW_GetOrigin(infoPtr, &Origin);
4874 /* iterate through the invalidated rows */
4875 while(iterator_next(i))
4877 item.iItem = i->nItem;
4878 item.iSubItem = 0;
4879 item.mask = LVIF_PARAM | LVIF_STATE;
4880 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
4881 if (!LISTVIEW_GetItemW(infoPtr, &item)) continue;
4883 dis.CtlType = ODT_LISTVIEW;
4884 dis.CtlID = uID;
4885 dis.itemID = item.iItem;
4886 dis.itemAction = ODA_DRAWENTIRE;
4887 dis.itemState = 0;
4888 if (item.state & LVIS_SELECTED) dis.itemState |= ODS_SELECTED;
4889 if (infoPtr->bFocus && (item.state & LVIS_FOCUSED)) dis.itemState |= ODS_FOCUS;
4890 dis.hwndItem = infoPtr->hwndSelf;
4891 dis.hDC = hdc;
4892 LISTVIEW_GetItemOrigin(infoPtr, dis.itemID, &Position);
4893 dis.rcItem.left = Position.x + Origin.x;
4894 dis.rcItem.right = dis.rcItem.left + infoPtr->nItemWidth;
4895 dis.rcItem.top = Position.y + Origin.y;
4896 dis.rcItem.bottom = dis.rcItem.top + infoPtr->nItemHeight;
4897 dis.itemData = item.lParam;
4899 TRACE("item=%s, rcItem=%s\n", debuglvitem_t(&item, TRUE), wine_dbgstr_rect(&dis.rcItem));
4902 * Even if we do not send the CDRF_NOTIFYITEMDRAW we need to fill the nmlvcd
4903 * structure for the rest. of the paint cycle
4905 customdraw_fill(&nmlvcd, infoPtr, hdc, &dis.rcItem, &item);
4906 if (cdmode & CDRF_NOTIFYITEMDRAW)
4907 cditemmode = notify_customdraw(infoPtr, CDDS_PREPAINT, &nmlvcd);
4909 if (!(cditemmode & CDRF_SKIPDEFAULT))
4911 prepaint_setup (infoPtr, hdc, &nmlvcd, FALSE);
4912 SendMessageW(infoPtr->hwndNotify, WM_DRAWITEM, dis.CtlID, (LPARAM)&dis);
4915 if (cditemmode & CDRF_NOTIFYPOSTPAINT)
4916 notify_postpaint(infoPtr, &nmlvcd);
4920 /***
4921 * DESCRIPTION:
4922 * Draws listview items when in report display mode.
4924 * PARAMETER(S):
4925 * [I] infoPtr : valid pointer to the listview structure
4926 * [I] hdc : device context handle
4927 * [I] cdmode : custom draw mode
4929 * RETURN:
4930 * None
4932 static void LISTVIEW_RefreshReport(LISTVIEW_INFO *infoPtr, ITERATOR *i, HDC hdc, DWORD cdmode)
4934 INT rgntype;
4935 RECT rcClip, rcItem;
4936 POINT Origin;
4937 RANGES colRanges;
4938 INT col;
4939 ITERATOR j;
4941 TRACE("()\n");
4943 /* figure out what to draw */
4944 rgntype = GetClipBox(hdc, &rcClip);
4945 if (rgntype == NULLREGION) return;
4947 /* Get scroll info once before loop */
4948 LISTVIEW_GetOrigin(infoPtr, &Origin);
4950 colRanges = ranges_create(DPA_GetPtrCount(infoPtr->hdpaColumns));
4952 /* narrow down the columns we need to paint */
4953 for(col = 0; col < DPA_GetPtrCount(infoPtr->hdpaColumns); col++)
4955 INT index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, col, 0);
4957 LISTVIEW_GetHeaderRect(infoPtr, index, &rcItem);
4958 if ((rcItem.right + Origin.x >= rcClip.left) && (rcItem.left + Origin.x < rcClip.right))
4959 ranges_additem(colRanges, index);
4961 iterator_rangesitems(&j, colRanges);
4963 /* in full row select, we _have_ to draw the main item */
4964 if (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT)
4965 j.nSpecial = 0;
4967 /* iterate through the invalidated rows */
4968 while(iterator_next(i))
4970 RANGES subitems;
4971 POINT Position;
4972 ITERATOR k;
4974 SelectObject(hdc, infoPtr->hFont);
4975 LISTVIEW_GetItemOrigin(infoPtr, i->nItem, &Position);
4976 Position.x = Origin.x;
4977 Position.y += Origin.y;
4979 subitems = ranges_create(DPA_GetPtrCount(infoPtr->hdpaColumns));
4981 /* iterate through the invalidated columns */
4982 while(iterator_next(&j))
4984 LISTVIEW_GetHeaderRect(infoPtr, j.nItem, &rcItem);
4986 if (rgntype == COMPLEXREGION && !((infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) && j.nItem == 0))
4988 rcItem.top = 0;
4989 rcItem.bottom = infoPtr->nItemHeight;
4990 OffsetRect(&rcItem, Origin.x, Position.y);
4991 if (!RectVisible(hdc, &rcItem)) continue;
4994 ranges_additem(subitems, j.nItem);
4997 iterator_rangesitems(&k, subitems);
4998 LISTVIEW_DrawItem(infoPtr, hdc, i->nItem, &k, Position, cdmode);
4999 iterator_destroy(&k);
5001 iterator_destroy(&j);
5004 /***
5005 * DESCRIPTION:
5006 * Draws the gridlines if necessary when in report display mode.
5008 * PARAMETER(S):
5009 * [I] infoPtr : valid pointer to the listview structure
5010 * [I] hdc : device context handle
5012 * RETURN:
5013 * None
5015 static void LISTVIEW_RefreshReportGrid(LISTVIEW_INFO *infoPtr, HDC hdc)
5017 INT rgntype;
5018 INT y, itemheight;
5019 INT col, index;
5020 HPEN hPen, hOldPen;
5021 RECT rcClip, rcItem = {0};
5022 POINT Origin;
5023 RANGES colRanges;
5024 ITERATOR j;
5025 BOOL rmost = FALSE;
5027 TRACE("()\n");
5029 /* figure out what to draw */
5030 rgntype = GetClipBox(hdc, &rcClip);
5031 if (rgntype == NULLREGION) return;
5033 /* Get scroll info once before loop */
5034 LISTVIEW_GetOrigin(infoPtr, &Origin);
5036 colRanges = ranges_create(DPA_GetPtrCount(infoPtr->hdpaColumns));
5038 /* narrow down the columns we need to paint */
5039 for(col = 0; col < DPA_GetPtrCount(infoPtr->hdpaColumns); col++)
5041 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, col, 0);
5043 LISTVIEW_GetHeaderRect(infoPtr, index, &rcItem);
5044 if ((rcItem.right + Origin.x >= rcClip.left) && (rcItem.left + Origin.x < rcClip.right))
5045 ranges_additem(colRanges, index);
5048 /* is right most vertical line visible? */
5049 if (DPA_GetPtrCount(infoPtr->hdpaColumns) > 0)
5051 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, 0);
5052 LISTVIEW_GetHeaderRect(infoPtr, index, &rcItem);
5053 rmost = (rcItem.right + Origin.x < rcClip.right);
5056 if ((hPen = CreatePen( PS_SOLID, 1, comctl32_color.clr3dFace )))
5058 hOldPen = SelectObject ( hdc, hPen );
5060 /* draw the vertical lines for the columns */
5061 iterator_rangesitems(&j, colRanges);
5062 while(iterator_next(&j))
5064 LISTVIEW_GetHeaderRect(infoPtr, j.nItem, &rcItem);
5065 if (rcItem.left == 0) continue; /* skip leftmost column */
5066 rcItem.left += Origin.x;
5067 rcItem.right += Origin.x;
5068 rcItem.top = infoPtr->rcList.top;
5069 rcItem.bottom = infoPtr->rcList.bottom;
5070 TRACE("vert col=%d, rcItem=%s\n", j.nItem, wine_dbgstr_rect(&rcItem));
5071 MoveToEx (hdc, rcItem.left, rcItem.top, NULL);
5072 LineTo (hdc, rcItem.left, rcItem.bottom);
5074 iterator_destroy(&j);
5075 /* draw rightmost grid line if visible */
5076 if (rmost)
5078 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX,
5079 DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, 0);
5080 LISTVIEW_GetHeaderRect(infoPtr, index, &rcItem);
5082 rcItem.right += Origin.x;
5084 MoveToEx (hdc, rcItem.right, infoPtr->rcList.top, NULL);
5085 LineTo (hdc, rcItem.right, infoPtr->rcList.bottom);
5088 /* draw the horizontal lines for the rows */
5089 itemheight = LISTVIEW_CalculateItemHeight(infoPtr);
5090 rcItem.left = infoPtr->rcList.left;
5091 rcItem.right = infoPtr->rcList.right;
5092 for(y = Origin.y > 1 ? Origin.y - 1 : itemheight - 1 + Origin.y % itemheight; y<=infoPtr->rcList.bottom; y+=itemheight)
5094 rcItem.bottom = rcItem.top = y;
5095 TRACE("horz rcItem=%s\n", wine_dbgstr_rect(&rcItem));
5096 MoveToEx (hdc, rcItem.left, rcItem.top, NULL);
5097 LineTo (hdc, rcItem.right, rcItem.top);
5100 SelectObject( hdc, hOldPen );
5101 DeleteObject( hPen );
5103 else
5104 ranges_destroy(colRanges);
5107 /***
5108 * DESCRIPTION:
5109 * Draws listview items when in list display mode.
5111 * PARAMETER(S):
5112 * [I] infoPtr : valid pointer to the listview structure
5113 * [I] hdc : device context handle
5114 * [I] cdmode : custom draw mode
5116 * RETURN:
5117 * None
5119 static void LISTVIEW_RefreshList(LISTVIEW_INFO *infoPtr, ITERATOR *i, HDC hdc, DWORD cdmode)
5121 POINT Origin, Position;
5123 /* Get scroll info once before loop */
5124 LISTVIEW_GetOrigin(infoPtr, &Origin);
5126 while(iterator_prev(i))
5128 SelectObject(hdc, infoPtr->hFont);
5129 LISTVIEW_GetItemOrigin(infoPtr, i->nItem, &Position);
5130 Position.x += Origin.x;
5131 Position.y += Origin.y;
5133 LISTVIEW_DrawItem(infoPtr, hdc, i->nItem, NULL, Position, cdmode);
5138 /***
5139 * DESCRIPTION:
5140 * Draws listview items.
5142 * PARAMETER(S):
5143 * [I] infoPtr : valid pointer to the listview structure
5144 * [I] hdc : device context handle
5145 * [I] prcErase : rect to be erased before refresh (may be NULL)
5147 * RETURN:
5148 * NoneX
5150 static void LISTVIEW_Refresh(LISTVIEW_INFO *infoPtr, HDC hdc, const RECT *prcErase)
5152 COLORREF oldTextColor = 0, oldBkColor = 0;
5153 NMLVCUSTOMDRAW nmlvcd;
5154 HFONT hOldFont = 0;
5155 DWORD cdmode;
5156 INT oldBkMode = 0;
5157 RECT rcClient;
5158 ITERATOR i;
5159 HDC hdcOrig = hdc;
5160 HBITMAP hbmp = NULL;
5161 RANGE range;
5163 LISTVIEW_DUMP(infoPtr);
5165 if (infoPtr->dwLvExStyle & LVS_EX_DOUBLEBUFFER) {
5166 TRACE("double buffering\n");
5168 hdc = CreateCompatibleDC(hdcOrig);
5169 if (!hdc) {
5170 ERR("Failed to create DC for backbuffer\n");
5171 return;
5173 hbmp = CreateCompatibleBitmap(hdcOrig, infoPtr->rcList.right,
5174 infoPtr->rcList.bottom);
5175 if (!hbmp) {
5176 ERR("Failed to create bitmap for backbuffer\n");
5177 DeleteDC(hdc);
5178 return;
5181 SelectObject(hdc, hbmp);
5182 SelectObject(hdc, infoPtr->hFont);
5184 if(GetClipBox(hdcOrig, &rcClient))
5185 IntersectClipRect(hdc, rcClient.left, rcClient.top, rcClient.right, rcClient.bottom);
5186 } else {
5187 /* Save dc values we're gonna trash while drawing
5188 * FIXME: Should be done in LISTVIEW_DrawItem() */
5189 hOldFont = SelectObject(hdc, infoPtr->hFont);
5190 oldBkMode = GetBkMode(hdc);
5191 oldBkColor = GetBkColor(hdc);
5192 oldTextColor = GetTextColor(hdc);
5195 infoPtr->bIsDrawing = TRUE;
5197 if (prcErase) {
5198 LISTVIEW_FillBkgnd(infoPtr, hdc, prcErase);
5199 } else if (infoPtr->dwLvExStyle & LVS_EX_DOUBLEBUFFER) {
5200 /* If no erasing was done (usually because RedrawWindow was called
5201 * with RDW_INVALIDATE only) we need to copy the old contents into
5202 * the backbuffer before continuing. */
5203 BitBlt(hdc, infoPtr->rcList.left, infoPtr->rcList.top,
5204 infoPtr->rcList.right - infoPtr->rcList.left,
5205 infoPtr->rcList.bottom - infoPtr->rcList.top,
5206 hdcOrig, infoPtr->rcList.left, infoPtr->rcList.top, SRCCOPY);
5209 GetClientRect(infoPtr->hwndSelf, &rcClient);
5210 customdraw_fill(&nmlvcd, infoPtr, hdc, &rcClient, 0);
5211 cdmode = notify_customdraw(infoPtr, CDDS_PREPAINT, &nmlvcd);
5212 if (cdmode & CDRF_SKIPDEFAULT) goto enddraw;
5214 /* nothing to draw */
5215 if(infoPtr->nItemCount == 0) goto enddraw;
5217 /* figure out what we need to draw */
5218 iterator_visibleitems(&i, infoPtr, hdc);
5219 range = iterator_range(&i);
5221 /* send cache hint notification */
5222 if (infoPtr->dwStyle & LVS_OWNERDATA)
5224 NMLVCACHEHINT nmlv;
5226 ZeroMemory(&nmlv, sizeof(NMLVCACHEHINT));
5227 nmlv.iFrom = range.lower;
5228 nmlv.iTo = range.upper - 1;
5229 notify_hdr(infoPtr, LVN_ODCACHEHINT, &nmlv.hdr);
5232 if ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (infoPtr->uView == LV_VIEW_DETAILS))
5233 LISTVIEW_RefreshOwnerDraw(infoPtr, &i, hdc, cdmode);
5234 else
5236 if (infoPtr->uView == LV_VIEW_DETAILS)
5237 LISTVIEW_RefreshReport(infoPtr, &i, hdc, cdmode);
5238 else /* LV_VIEW_LIST, LV_VIEW_ICON or LV_VIEW_SMALLICON */
5239 LISTVIEW_RefreshList(infoPtr, &i, hdc, cdmode);
5241 /* if we have a focus rect and it's visible, draw it */
5242 if (infoPtr->bFocus && range.lower <= infoPtr->nFocusedItem &&
5243 (range.upper - 1) >= infoPtr->nFocusedItem)
5244 LISTVIEW_DrawFocusRect(infoPtr, hdc);
5246 iterator_destroy(&i);
5248 enddraw:
5249 /* For LVS_EX_GRIDLINES go and draw lines */
5250 /* This includes the case where there were *no* items */
5251 if ((infoPtr->uView == LV_VIEW_DETAILS) && infoPtr->dwLvExStyle & LVS_EX_GRIDLINES)
5252 LISTVIEW_RefreshReportGrid(infoPtr, hdc);
5254 /* Draw marquee rectangle if appropriate */
5255 if (infoPtr->bMarqueeSelect)
5256 DrawFocusRect(hdc, &infoPtr->marqueeDrawRect);
5258 if (cdmode & CDRF_NOTIFYPOSTPAINT)
5259 notify_postpaint(infoPtr, &nmlvcd);
5261 if(hbmp) {
5262 BitBlt(hdcOrig, infoPtr->rcList.left, infoPtr->rcList.top,
5263 infoPtr->rcList.right - infoPtr->rcList.left,
5264 infoPtr->rcList.bottom - infoPtr->rcList.top,
5265 hdc, infoPtr->rcList.left, infoPtr->rcList.top, SRCCOPY);
5267 DeleteObject(hbmp);
5268 DeleteDC(hdc);
5269 } else {
5270 SelectObject(hdc, hOldFont);
5271 SetBkMode(hdc, oldBkMode);
5272 SetBkColor(hdc, oldBkColor);
5273 SetTextColor(hdc, oldTextColor);
5276 infoPtr->bIsDrawing = FALSE;
5280 /***
5281 * DESCRIPTION:
5282 * Calculates the approximate width and height of a given number of items.
5284 * PARAMETER(S):
5285 * [I] infoPtr : valid pointer to the listview structure
5286 * [I] nItemCount : number of items
5287 * [I] wWidth : width
5288 * [I] wHeight : height
5290 * RETURN:
5291 * Returns a DWORD. The width in the low word and the height in high word.
5293 static DWORD LISTVIEW_ApproximateViewRect(const LISTVIEW_INFO *infoPtr, INT nItemCount,
5294 WORD wWidth, WORD wHeight)
5296 DWORD dwViewRect = 0;
5298 if (nItemCount == -1)
5299 nItemCount = infoPtr->nItemCount;
5301 if (infoPtr->uView == LV_VIEW_LIST)
5303 INT nItemCountPerColumn = 1;
5304 INT nColumnCount = 0;
5306 if (wHeight == 0xFFFF)
5308 /* use current height */
5309 wHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
5312 if (wHeight < infoPtr->nItemHeight)
5313 wHeight = infoPtr->nItemHeight;
5315 if (nItemCount > 0)
5317 if (infoPtr->nItemHeight > 0)
5319 nItemCountPerColumn = wHeight / infoPtr->nItemHeight;
5320 if (nItemCountPerColumn == 0)
5321 nItemCountPerColumn = 1;
5323 if (nItemCount % nItemCountPerColumn != 0)
5324 nColumnCount = nItemCount / nItemCountPerColumn;
5325 else
5326 nColumnCount = nItemCount / nItemCountPerColumn + 1;
5330 /* Microsoft padding magic */
5331 wHeight = nItemCountPerColumn * infoPtr->nItemHeight + 2;
5332 wWidth = nColumnCount * infoPtr->nItemWidth + 2;
5334 dwViewRect = MAKELONG(wWidth, wHeight);
5336 else if (infoPtr->uView == LV_VIEW_DETAILS)
5338 RECT rcBox;
5340 if (infoPtr->nItemCount > 0)
5342 LISTVIEW_GetItemBox(infoPtr, 0, &rcBox);
5343 wWidth = rcBox.right - rcBox.left;
5344 wHeight = (rcBox.bottom - rcBox.top) * nItemCount;
5346 else
5348 /* use current height and width */
5349 if (wHeight == 0xffff)
5350 wHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
5351 if (wWidth == 0xffff)
5352 wWidth = infoPtr->rcList.right - infoPtr->rcList.left;
5355 dwViewRect = MAKELONG(wWidth, wHeight);
5357 else if (infoPtr->uView == LV_VIEW_ICON)
5359 UINT rows,cols;
5360 UINT nItemWidth;
5361 UINT nItemHeight;
5363 nItemWidth = infoPtr->iconSpacing.cx;
5364 nItemHeight = infoPtr->iconSpacing.cy;
5366 if (wWidth == 0xffff)
5367 wWidth = infoPtr->rcList.right - infoPtr->rcList.left;
5369 if (wWidth < nItemWidth)
5370 wWidth = nItemWidth;
5372 cols = wWidth / nItemWidth;
5373 if (cols > nItemCount)
5374 cols = nItemCount;
5375 if (cols < 1)
5376 cols = 1;
5378 if (nItemCount)
5380 rows = nItemCount / cols;
5381 if (nItemCount % cols)
5382 rows++;
5384 else
5385 rows = 0;
5387 wHeight = (nItemHeight * rows)+2;
5388 wWidth = (nItemWidth * cols)+2;
5390 dwViewRect = MAKELONG(wWidth, wHeight);
5392 else if (infoPtr->uView == LV_VIEW_SMALLICON)
5393 FIXME("uView == LV_VIEW_SMALLICON: not implemented\n");
5395 return dwViewRect;
5398 /***
5399 * DESCRIPTION:
5400 * Cancel edit label with saving item text.
5402 * PARAMETER(S):
5403 * [I] infoPtr : valid pointer to the listview structure
5405 * RETURN:
5406 * Always returns TRUE.
5408 static LRESULT LISTVIEW_CancelEditLabel(LISTVIEW_INFO *infoPtr)
5410 if (infoPtr->hwndEdit)
5412 /* handle value will be lost after LISTVIEW_EndEditLabelT */
5413 HWND edit = infoPtr->hwndEdit;
5415 LISTVIEW_EndEditLabelT(infoPtr, TRUE, IsWindowUnicode(infoPtr->hwndEdit));
5416 SendMessageW(edit, WM_CLOSE, 0, 0);
5419 return TRUE;
5422 /***
5423 * DESCRIPTION:
5424 * Create a drag image list for the specified item.
5426 * PARAMETER(S):
5427 * [I] infoPtr : valid pointer to the listview structure
5428 * [I] iItem : index of item
5429 * [O] lppt : Upper-left corner of the image
5431 * RETURN:
5432 * Returns a handle to the image list if successful, NULL otherwise.
5434 static HIMAGELIST LISTVIEW_CreateDragImage(LISTVIEW_INFO *infoPtr, INT iItem, LPPOINT lppt)
5436 RECT rcItem;
5437 SIZE size;
5438 POINT pos;
5439 HDC hdc, hdcOrig;
5440 HBITMAP hbmp, hOldbmp;
5441 HFONT hOldFont;
5442 HIMAGELIST dragList = 0;
5443 TRACE("iItem=%d Count=%d\n", iItem, infoPtr->nItemCount);
5445 if (iItem < 0 || iItem >= infoPtr->nItemCount || !lppt)
5446 return 0;
5448 rcItem.left = LVIR_BOUNDS;
5449 if (!LISTVIEW_GetItemRect(infoPtr, iItem, &rcItem))
5450 return 0;
5452 lppt->x = rcItem.left;
5453 lppt->y = rcItem.top;
5455 size.cx = rcItem.right - rcItem.left;
5456 size.cy = rcItem.bottom - rcItem.top;
5458 hdcOrig = GetDC(infoPtr->hwndSelf);
5459 hdc = CreateCompatibleDC(hdcOrig);
5460 hbmp = CreateCompatibleBitmap(hdcOrig, size.cx, size.cy);
5461 hOldbmp = SelectObject(hdc, hbmp);
5462 hOldFont = SelectObject(hdc, infoPtr->hFont);
5464 SetRect(&rcItem, 0, 0, size.cx, size.cy);
5465 FillRect(hdc, &rcItem, infoPtr->hBkBrush);
5467 pos.x = pos.y = 0;
5468 if (LISTVIEW_DrawItem(infoPtr, hdc, iItem, NULL, pos, CDRF_DODEFAULT))
5470 dragList = ImageList_Create(size.cx, size.cy, ILC_COLOR, 10, 10);
5471 SelectObject(hdc, hOldbmp);
5472 ImageList_Add(dragList, hbmp, 0);
5474 else
5475 SelectObject(hdc, hOldbmp);
5477 SelectObject(hdc, hOldFont);
5478 DeleteObject(hbmp);
5479 DeleteDC(hdc);
5480 ReleaseDC(infoPtr->hwndSelf, hdcOrig);
5482 TRACE("ret=%p\n", dragList);
5484 return dragList;
5488 /***
5489 * DESCRIPTION:
5490 * Removes all listview items and subitems.
5492 * PARAMETER(S):
5493 * [I] infoPtr : valid pointer to the listview structure
5495 * RETURN:
5496 * SUCCESS : TRUE
5497 * FAILURE : FALSE
5499 static BOOL LISTVIEW_DeleteAllItems(LISTVIEW_INFO *infoPtr, BOOL destroy)
5501 HDPA hdpaSubItems = NULL;
5502 BOOL suppress = FALSE;
5503 ITEMHDR *hdrItem;
5504 ITEM_INFO *lpItem;
5505 ITEM_ID *lpID;
5506 INT i, j;
5508 TRACE("()\n");
5510 /* we do it directly, to avoid notifications */
5511 ranges_clear(infoPtr->selectionRanges);
5512 infoPtr->nSelectionMark = -1;
5513 infoPtr->nFocusedItem = -1;
5514 SetRectEmpty(&infoPtr->rcFocus);
5515 /* But we are supposed to leave nHotItem as is! */
5517 /* send LVN_DELETEALLITEMS notification */
5518 if (!(infoPtr->dwStyle & LVS_OWNERDATA) || !destroy)
5520 NMLISTVIEW nmlv;
5522 memset(&nmlv, 0, sizeof(NMLISTVIEW));
5523 nmlv.iItem = -1;
5524 suppress = notify_listview(infoPtr, LVN_DELETEALLITEMS, &nmlv);
5527 for (i = infoPtr->nItemCount - 1; i >= 0; i--)
5529 if (!(infoPtr->dwStyle & LVS_OWNERDATA))
5531 /* send LVN_DELETEITEM notification, if not suppressed
5532 and if it is not a virtual listview */
5533 if (!suppress) notify_deleteitem(infoPtr, i);
5534 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, i);
5535 lpItem = DPA_GetPtr(hdpaSubItems, 0);
5536 /* free id struct */
5537 j = DPA_GetPtrIndex(infoPtr->hdpaItemIds, lpItem->id);
5538 lpID = DPA_GetPtr(infoPtr->hdpaItemIds, j);
5539 DPA_DeletePtr(infoPtr->hdpaItemIds, j);
5540 Free(lpID);
5541 /* both item and subitem start with ITEMHDR header */
5542 for (j = 0; j < DPA_GetPtrCount(hdpaSubItems); j++)
5544 hdrItem = DPA_GetPtr(hdpaSubItems, j);
5545 if (is_text(hdrItem->pszText)) Free(hdrItem->pszText);
5546 Free(hdrItem);
5548 DPA_Destroy(hdpaSubItems);
5549 DPA_DeletePtr(infoPtr->hdpaItems, i);
5551 DPA_DeletePtr(infoPtr->hdpaPosX, i);
5552 DPA_DeletePtr(infoPtr->hdpaPosY, i);
5553 infoPtr->nItemCount --;
5556 if (!destroy)
5558 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
5559 LISTVIEW_UpdateScroll(infoPtr);
5561 LISTVIEW_InvalidateList(infoPtr);
5563 return TRUE;
5566 /***
5567 * DESCRIPTION:
5568 * Scrolls, and updates the columns, when a column is changing width.
5570 * PARAMETER(S):
5571 * [I] infoPtr : valid pointer to the listview structure
5572 * [I] nColumn : column to scroll
5573 * [I] dx : amount of scroll, in pixels
5575 * RETURN:
5576 * None.
5578 static void LISTVIEW_ScrollColumns(LISTVIEW_INFO *infoPtr, INT nColumn, INT dx)
5580 COLUMN_INFO *lpColumnInfo;
5581 RECT rcOld, rcCol;
5582 POINT ptOrigin;
5583 INT nCol;
5584 HDITEMW hdi;
5586 if (nColumn < 0 || DPA_GetPtrCount(infoPtr->hdpaColumns) < 1) return;
5587 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, min(nColumn, DPA_GetPtrCount(infoPtr->hdpaColumns) - 1));
5588 rcCol = lpColumnInfo->rcHeader;
5589 if (nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns))
5590 rcCol.left = rcCol.right;
5592 /* adjust the other columns */
5593 hdi.mask = HDI_ORDER;
5594 if (SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nColumn, (LPARAM)&hdi))
5596 INT nOrder = hdi.iOrder;
5597 for (nCol = 0; nCol < DPA_GetPtrCount(infoPtr->hdpaColumns); nCol++)
5599 hdi.mask = HDI_ORDER;
5600 SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nCol, (LPARAM)&hdi);
5601 if (hdi.iOrder >= nOrder) {
5602 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, nCol);
5603 lpColumnInfo->rcHeader.left += dx;
5604 lpColumnInfo->rcHeader.right += dx;
5609 /* do not update screen if not in report mode */
5610 if (!is_redrawing(infoPtr) || infoPtr->uView != LV_VIEW_DETAILS) return;
5612 /* Need to reset the item width when inserting a new column */
5613 infoPtr->nItemWidth += dx;
5615 LISTVIEW_UpdateScroll(infoPtr);
5616 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
5618 /* scroll to cover the deleted column, and invalidate for redraw */
5619 rcOld = infoPtr->rcList;
5620 rcOld.left = ptOrigin.x + rcCol.left + dx;
5621 ScrollWindowEx(infoPtr->hwndSelf, dx, 0, &rcOld, &rcOld, 0, 0, SW_ERASE | SW_INVALIDATE);
5624 /***
5625 * DESCRIPTION:
5626 * Removes a column from the listview control.
5628 * PARAMETER(S):
5629 * [I] infoPtr : valid pointer to the listview structure
5630 * [I] nColumn : column index
5632 * RETURN:
5633 * SUCCESS : TRUE
5634 * FAILURE : FALSE
5636 static BOOL LISTVIEW_DeleteColumn(LISTVIEW_INFO *infoPtr, INT nColumn)
5638 RECT rcCol;
5640 TRACE("nColumn=%d\n", nColumn);
5642 if (nColumn < 0 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns))
5643 return FALSE;
5645 /* While the MSDN specifically says that column zero should not be deleted,
5646 what actually happens is that the column itself is deleted but no items or subitems
5647 are removed.
5650 LISTVIEW_GetHeaderRect(infoPtr, nColumn, &rcCol);
5652 if (!SendMessageW(infoPtr->hwndHeader, HDM_DELETEITEM, nColumn, 0))
5653 return FALSE;
5655 Free(DPA_GetPtr(infoPtr->hdpaColumns, nColumn));
5656 DPA_DeletePtr(infoPtr->hdpaColumns, nColumn);
5658 if (!(infoPtr->dwStyle & LVS_OWNERDATA) && nColumn)
5660 SUBITEM_INFO *lpSubItem, *lpDelItem;
5661 HDPA hdpaSubItems;
5662 INT nItem, nSubItem, i;
5664 for (nItem = 0; nItem < infoPtr->nItemCount; nItem++)
5666 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, nItem);
5667 nSubItem = 0;
5668 lpDelItem = 0;
5669 for (i = 1; i < DPA_GetPtrCount(hdpaSubItems); i++)
5671 lpSubItem = DPA_GetPtr(hdpaSubItems, i);
5672 if (lpSubItem->iSubItem == nColumn)
5674 nSubItem = i;
5675 lpDelItem = lpSubItem;
5677 else if (lpSubItem->iSubItem > nColumn)
5679 lpSubItem->iSubItem--;
5683 /* if we found our subitem, zap it */
5684 if (nSubItem > 0)
5686 /* free string */
5687 if (is_text(lpDelItem->hdr.pszText))
5688 Free(lpDelItem->hdr.pszText);
5690 /* free item */
5691 Free(lpDelItem);
5693 /* free dpa memory */
5694 DPA_DeletePtr(hdpaSubItems, nSubItem);
5699 /* update the other column info */
5700 if(DPA_GetPtrCount(infoPtr->hdpaColumns) == 0)
5701 LISTVIEW_InvalidateList(infoPtr);
5702 else
5703 LISTVIEW_ScrollColumns(infoPtr, nColumn, -(rcCol.right - rcCol.left));
5704 LISTVIEW_UpdateItemSize(infoPtr);
5706 return TRUE;
5709 /***
5710 * DESCRIPTION:
5711 * Invalidates the listview after an item's insertion or deletion.
5713 * PARAMETER(S):
5714 * [I] infoPtr : valid pointer to the listview structure
5715 * [I] nItem : item index
5716 * [I] dir : -1 if deleting, 1 if inserting
5718 * RETURN:
5719 * None
5721 static void LISTVIEW_ScrollOnInsert(LISTVIEW_INFO *infoPtr, INT nItem, INT dir)
5723 INT nPerCol, nItemCol, nItemRow;
5724 RECT rcScroll;
5725 POINT Origin;
5727 /* if we don't refresh, what's the point of scrolling? */
5728 if (!is_redrawing(infoPtr)) return;
5730 assert (abs(dir) == 1);
5732 /* arrange icons if autoarrange is on */
5733 if (is_autoarrange(infoPtr))
5735 BOOL arrange = TRUE;
5736 if (dir < 0 && nItem >= infoPtr->nItemCount) arrange = FALSE;
5737 if (dir > 0 && nItem == infoPtr->nItemCount - 1) arrange = FALSE;
5738 if (arrange) LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
5741 /* scrollbars need updating */
5742 LISTVIEW_UpdateScroll(infoPtr);
5744 /* figure out the item's position */
5745 if (infoPtr->uView == LV_VIEW_DETAILS)
5746 nPerCol = infoPtr->nItemCount + 1;
5747 else if (infoPtr->uView == LV_VIEW_LIST)
5748 nPerCol = LISTVIEW_GetCountPerColumn(infoPtr);
5749 else /* LV_VIEW_ICON, or LV_VIEW_SMALLICON */
5750 return;
5752 nItemCol = nItem / nPerCol;
5753 nItemRow = nItem % nPerCol;
5754 LISTVIEW_GetOrigin(infoPtr, &Origin);
5756 /* move the items below up a slot */
5757 rcScroll.left = nItemCol * infoPtr->nItemWidth;
5758 rcScroll.top = nItemRow * infoPtr->nItemHeight;
5759 rcScroll.right = rcScroll.left + infoPtr->nItemWidth;
5760 rcScroll.bottom = nPerCol * infoPtr->nItemHeight;
5761 OffsetRect(&rcScroll, Origin.x, Origin.y);
5762 TRACE("rcScroll=%s, dx=%d\n", wine_dbgstr_rect(&rcScroll), dir * infoPtr->nItemHeight);
5763 if (IntersectRect(&rcScroll, &rcScroll, &infoPtr->rcList))
5765 TRACE("Invalidating rcScroll=%s, rcList=%s\n", wine_dbgstr_rect(&rcScroll), wine_dbgstr_rect(&infoPtr->rcList));
5766 InvalidateRect(infoPtr->hwndSelf, &rcScroll, TRUE);
5769 /* report has only that column, so we're done */
5770 if (infoPtr->uView == LV_VIEW_DETAILS) return;
5772 /* now for LISTs, we have to deal with the columns to the right */
5773 SetRect(&rcScroll, (nItemCol + 1) * infoPtr->nItemWidth, 0,
5774 (infoPtr->nItemCount / nPerCol + 1) * infoPtr->nItemWidth,
5775 nPerCol * infoPtr->nItemHeight);
5776 OffsetRect(&rcScroll, Origin.x, Origin.y);
5777 if (IntersectRect(&rcScroll, &rcScroll, &infoPtr->rcList))
5778 InvalidateRect(infoPtr->hwndSelf, &rcScroll, TRUE);
5781 /***
5782 * DESCRIPTION:
5783 * Removes an item from the listview control.
5785 * PARAMETER(S):
5786 * [I] infoPtr : valid pointer to the listview structure
5787 * [I] nItem : item index
5789 * RETURN:
5790 * SUCCESS : TRUE
5791 * FAILURE : FALSE
5793 static BOOL LISTVIEW_DeleteItem(LISTVIEW_INFO *infoPtr, INT nItem)
5795 LVITEMW item;
5796 const BOOL is_icon = (infoPtr->uView == LV_VIEW_SMALLICON || infoPtr->uView == LV_VIEW_ICON);
5797 INT focus = infoPtr->nFocusedItem;
5799 TRACE("(nItem=%d)\n", nItem);
5801 if (nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
5803 /* remove selection, and focus */
5804 item.state = 0;
5805 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
5806 LISTVIEW_SetItemState(infoPtr, nItem, &item);
5808 /* send LVN_DELETEITEM notification. */
5809 if (!notify_deleteitem(infoPtr, nItem)) return FALSE;
5811 /* we need to do this here, because we'll be deleting stuff */
5812 if (is_icon)
5813 LISTVIEW_InvalidateItem(infoPtr, nItem);
5815 if (!(infoPtr->dwStyle & LVS_OWNERDATA))
5817 HDPA hdpaSubItems;
5818 ITEMHDR *hdrItem;
5819 ITEM_INFO *lpItem;
5820 ITEM_ID *lpID;
5821 INT i;
5823 hdpaSubItems = DPA_DeletePtr(infoPtr->hdpaItems, nItem);
5824 lpItem = DPA_GetPtr(hdpaSubItems, 0);
5826 /* free id struct */
5827 i = DPA_GetPtrIndex(infoPtr->hdpaItemIds, lpItem->id);
5828 lpID = DPA_GetPtr(infoPtr->hdpaItemIds, i);
5829 DPA_DeletePtr(infoPtr->hdpaItemIds, i);
5830 Free(lpID);
5831 for (i = 0; i < DPA_GetPtrCount(hdpaSubItems); i++)
5833 hdrItem = DPA_GetPtr(hdpaSubItems, i);
5834 if (is_text(hdrItem->pszText)) Free(hdrItem->pszText);
5835 Free(hdrItem);
5837 DPA_Destroy(hdpaSubItems);
5840 if (is_icon)
5842 DPA_DeletePtr(infoPtr->hdpaPosX, nItem);
5843 DPA_DeletePtr(infoPtr->hdpaPosY, nItem);
5846 infoPtr->nItemCount--;
5847 LISTVIEW_ShiftIndices(infoPtr, nItem, -1);
5848 LISTVIEW_ShiftFocus(infoPtr, focus, nItem, -1);
5850 /* now is the invalidation fun */
5851 if (!is_icon)
5852 LISTVIEW_ScrollOnInsert(infoPtr, nItem, -1);
5853 return TRUE;
5857 /***
5858 * DESCRIPTION:
5859 * Callback implementation for editlabel control
5861 * PARAMETER(S):
5862 * [I] infoPtr : valid pointer to the listview structure
5863 * [I] storeText : store edit box text as item text
5864 * [I] isW : TRUE if psxText is Unicode, FALSE if it's ANSI
5866 * RETURN:
5867 * SUCCESS : TRUE
5868 * FAILURE : FALSE
5870 static BOOL LISTVIEW_EndEditLabelT(LISTVIEW_INFO *infoPtr, BOOL storeText, BOOL isW)
5872 HWND hwndSelf = infoPtr->hwndSelf;
5873 WCHAR szDispText[DISP_TEXT_SIZE] = { 0 };
5874 NMLVDISPINFOW dispInfo;
5875 INT editedItem = infoPtr->nEditLabelItem;
5876 BOOL same;
5877 WCHAR *pszText = NULL;
5878 BOOL res;
5880 if (storeText)
5882 DWORD len = isW ? GetWindowTextLengthW(infoPtr->hwndEdit) : GetWindowTextLengthA(infoPtr->hwndEdit);
5884 if (len++)
5886 if (!(pszText = Alloc(len * (isW ? sizeof(WCHAR) : sizeof(CHAR)))))
5887 return FALSE;
5889 if (isW)
5890 GetWindowTextW(infoPtr->hwndEdit, pszText, len);
5891 else
5892 GetWindowTextA(infoPtr->hwndEdit, (CHAR*)pszText, len);
5896 TRACE("(pszText=%s, isW=%d)\n", debugtext_t(pszText, isW), isW);
5898 ZeroMemory(&dispInfo, sizeof(dispInfo));
5899 dispInfo.item.mask = LVIF_PARAM | LVIF_STATE | LVIF_TEXT;
5900 dispInfo.item.iItem = editedItem;
5901 dispInfo.item.iSubItem = 0;
5902 dispInfo.item.stateMask = ~0;
5903 dispInfo.item.pszText = szDispText;
5904 dispInfo.item.cchTextMax = DISP_TEXT_SIZE;
5905 if (!LISTVIEW_GetItemT(infoPtr, &dispInfo.item, isW))
5907 res = FALSE;
5908 goto cleanup;
5911 if (isW)
5912 same = (lstrcmpW(dispInfo.item.pszText, pszText) == 0);
5913 else
5915 LPWSTR tmp = textdupTtoW(pszText, FALSE);
5916 same = (lstrcmpW(dispInfo.item.pszText, tmp) == 0);
5917 textfreeT(tmp, FALSE);
5920 /* add the text from the edit in */
5921 dispInfo.item.mask |= LVIF_TEXT;
5922 dispInfo.item.pszText = same ? NULL : pszText;
5923 dispInfo.item.cchTextMax = textlenT(dispInfo.item.pszText, isW);
5925 /* Do we need to update the Item Text */
5926 res = notify_dispinfoT(infoPtr, LVN_ENDLABELEDITW, &dispInfo, isW);
5928 infoPtr->nEditLabelItem = -1;
5929 infoPtr->hwndEdit = 0;
5931 if (!res) goto cleanup;
5933 if (!IsWindow(hwndSelf))
5935 res = FALSE;
5936 goto cleanup;
5938 if (!pszText) return TRUE;
5939 if (same)
5941 res = TRUE;
5942 goto cleanup;
5945 if (!(infoPtr->dwStyle & LVS_OWNERDATA))
5947 HDPA hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, editedItem);
5948 ITEM_INFO* lpItem = DPA_GetPtr(hdpaSubItems, 0);
5949 if (lpItem && lpItem->hdr.pszText == LPSTR_TEXTCALLBACKW)
5951 LISTVIEW_InvalidateItem(infoPtr, editedItem);
5952 res = TRUE;
5953 goto cleanup;
5957 ZeroMemory(&dispInfo, sizeof(dispInfo));
5958 dispInfo.item.mask = LVIF_TEXT;
5959 dispInfo.item.iItem = editedItem;
5960 dispInfo.item.iSubItem = 0;
5961 dispInfo.item.pszText = pszText;
5962 dispInfo.item.cchTextMax = textlenT(pszText, isW);
5963 res = LISTVIEW_SetItemT(infoPtr, &dispInfo.item, isW);
5965 cleanup:
5966 Free(pszText);
5968 return res;
5971 /***
5972 * DESCRIPTION:
5973 * Subclassed edit control windproc function
5975 * PARAMETER(S):
5976 * [I] hwnd : the edit window handle
5977 * [I] uMsg : the message that is to be processed
5978 * [I] wParam : first message parameter
5979 * [I] lParam : second message parameter
5980 * [I] isW : TRUE if input is Unicode
5982 * RETURN:
5983 * Zero.
5985 static LRESULT EditLblWndProcT(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL isW)
5987 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongPtrW(GetParent(hwnd), 0);
5988 BOOL save = TRUE;
5990 TRACE("(hwnd=%p, uMsg=%x, wParam=%lx, lParam=%lx, isW=%d)\n",
5991 hwnd, uMsg, wParam, lParam, isW);
5993 switch (uMsg)
5995 case WM_GETDLGCODE:
5996 return DLGC_WANTARROWS | DLGC_WANTALLKEYS;
5998 case WM_DESTROY:
6000 WNDPROC editProc = infoPtr->EditWndProc;
6001 infoPtr->EditWndProc = 0;
6002 SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (DWORD_PTR)editProc);
6003 return CallWindowProcT(editProc, hwnd, uMsg, wParam, lParam, isW);
6006 case WM_KEYDOWN:
6007 if (VK_ESCAPE == (INT)wParam)
6009 save = FALSE;
6010 break;
6012 else if (VK_RETURN == (INT)wParam)
6013 break;
6015 default:
6016 return CallWindowProcT(infoPtr->EditWndProc, hwnd, uMsg, wParam, lParam, isW);
6019 /* kill the edit */
6020 if (infoPtr->hwndEdit)
6021 LISTVIEW_EndEditLabelT(infoPtr, save, isW);
6023 SendMessageW(hwnd, WM_CLOSE, 0, 0);
6024 return 0;
6027 /***
6028 * DESCRIPTION:
6029 * Subclassed edit control Unicode windproc function
6031 * PARAMETER(S):
6032 * [I] hwnd : the edit window handle
6033 * [I] uMsg : the message that is to be processed
6034 * [I] wParam : first message parameter
6035 * [I] lParam : second message parameter
6037 * RETURN:
6039 static LRESULT CALLBACK EditLblWndProcW(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
6041 return EditLblWndProcT(hwnd, uMsg, wParam, lParam, TRUE);
6044 /***
6045 * DESCRIPTION:
6046 * Subclassed edit control ANSI windproc function
6048 * PARAMETER(S):
6049 * [I] hwnd : the edit window handle
6050 * [I] uMsg : the message that is to be processed
6051 * [I] wParam : first message parameter
6052 * [I] lParam : second message parameter
6054 * RETURN:
6056 static LRESULT CALLBACK EditLblWndProcA(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
6058 return EditLblWndProcT(hwnd, uMsg, wParam, lParam, FALSE);
6061 /***
6062 * DESCRIPTION:
6063 * Creates a subclassed edit control
6065 * PARAMETER(S):
6066 * [I] infoPtr : valid pointer to the listview structure
6067 * [I] text : initial text for the edit
6068 * [I] style : the window style
6069 * [I] isW : TRUE if input is Unicode
6071 * RETURN:
6073 static HWND CreateEditLabelT(LISTVIEW_INFO *infoPtr, LPCWSTR text, BOOL isW)
6075 static const DWORD style = WS_CHILDWINDOW|WS_CLIPSIBLINGS|ES_LEFT|ES_AUTOHSCROLL|WS_BORDER|WS_VISIBLE;
6076 HINSTANCE hinst = (HINSTANCE)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_HINSTANCE);
6077 HWND hedit;
6079 TRACE("(%p, text=%s, isW=%d)\n", infoPtr, debugtext_t(text, isW), isW);
6081 /* window will be resized and positioned after LVN_BEGINLABELEDIT */
6082 if (isW)
6083 hedit = CreateWindowW(WC_EDITW, text, style, 0, 0, 0, 0, infoPtr->hwndSelf, 0, hinst, 0);
6084 else
6085 hedit = CreateWindowA(WC_EDITA, (LPCSTR)text, style, 0, 0, 0, 0, infoPtr->hwndSelf, 0, hinst, 0);
6087 if (!hedit) return 0;
6089 infoPtr->EditWndProc = (WNDPROC)
6090 (isW ? SetWindowLongPtrW(hedit, GWLP_WNDPROC, (DWORD_PTR)EditLblWndProcW) :
6091 SetWindowLongPtrA(hedit, GWLP_WNDPROC, (DWORD_PTR)EditLblWndProcA) );
6093 SendMessageW(hedit, WM_SETFONT, (WPARAM)infoPtr->hFont, FALSE);
6094 SendMessageW(hedit, EM_SETLIMITTEXT, DISP_TEXT_SIZE-1, 0);
6096 return hedit;
6099 /***
6100 * DESCRIPTION:
6101 * Begin in place editing of specified list view item
6103 * PARAMETER(S):
6104 * [I] infoPtr : valid pointer to the listview structure
6105 * [I] nItem : item index
6106 * [I] isW : TRUE if it's a Unicode req, FALSE if ASCII
6108 * RETURN:
6109 * SUCCESS : TRUE
6110 * FAILURE : FALSE
6112 static HWND LISTVIEW_EditLabelT(LISTVIEW_INFO *infoPtr, INT nItem, BOOL isW)
6114 WCHAR disptextW[DISP_TEXT_SIZE] = { 0 };
6115 HWND hwndSelf = infoPtr->hwndSelf;
6116 NMLVDISPINFOW dispInfo;
6117 HFONT hOldFont = NULL;
6118 TEXTMETRICW tm;
6119 RECT rect;
6120 SIZE sz;
6121 HDC hdc;
6123 TRACE("(nItem=%d, isW=%d)\n", nItem, isW);
6125 if (~infoPtr->dwStyle & LVS_EDITLABELS) return 0;
6127 /* remove existing edit box */
6128 if (infoPtr->hwndEdit)
6130 SetFocus(infoPtr->hwndSelf);
6131 infoPtr->hwndEdit = 0;
6134 if (nItem < 0 || nItem >= infoPtr->nItemCount) return 0;
6136 infoPtr->nEditLabelItem = nItem;
6138 LISTVIEW_SetSelection(infoPtr, nItem);
6139 LISTVIEW_SetItemFocus(infoPtr, nItem);
6140 LISTVIEW_InvalidateItem(infoPtr, nItem);
6142 rect.left = LVIR_LABEL;
6143 if (!LISTVIEW_GetItemRect(infoPtr, nItem, &rect)) return 0;
6145 ZeroMemory(&dispInfo, sizeof(dispInfo));
6146 dispInfo.item.mask = LVIF_PARAM | LVIF_STATE | LVIF_TEXT;
6147 dispInfo.item.iItem = nItem;
6148 dispInfo.item.iSubItem = 0;
6149 dispInfo.item.stateMask = ~0;
6150 dispInfo.item.pszText = disptextW;
6151 dispInfo.item.cchTextMax = DISP_TEXT_SIZE;
6152 if (!LISTVIEW_GetItemT(infoPtr, &dispInfo.item, isW)) return 0;
6154 infoPtr->hwndEdit = CreateEditLabelT(infoPtr, dispInfo.item.pszText, isW);
6155 if (!infoPtr->hwndEdit) return 0;
6157 if (notify_dispinfoT(infoPtr, LVN_BEGINLABELEDITW, &dispInfo, isW))
6159 if (!IsWindow(hwndSelf))
6160 return 0;
6161 SendMessageW(infoPtr->hwndEdit, WM_CLOSE, 0, 0);
6162 infoPtr->hwndEdit = 0;
6163 return 0;
6166 TRACE("disp text=%s\n", debugtext_t(dispInfo.item.pszText, isW));
6168 /* position and display edit box */
6169 hdc = GetDC(infoPtr->hwndSelf);
6171 /* select the font to get appropriate metric dimensions */
6172 if (infoPtr->hFont)
6173 hOldFont = SelectObject(hdc, infoPtr->hFont);
6175 /* use real edit box content, it could be altered during LVN_BEGINLABELEDIT notification */
6176 GetWindowTextW(infoPtr->hwndEdit, disptextW, DISP_TEXT_SIZE);
6177 TRACE("edit box text=%s\n", debugstr_w(disptextW));
6179 /* get string length in pixels */
6180 GetTextExtentPoint32W(hdc, disptextW, lstrlenW(disptextW), &sz);
6182 /* add extra spacing for the next character */
6183 GetTextMetricsW(hdc, &tm);
6184 sz.cx += tm.tmMaxCharWidth * 2;
6186 if (infoPtr->hFont)
6187 SelectObject(hdc, hOldFont);
6189 ReleaseDC(infoPtr->hwndSelf, hdc);
6191 sz.cy = rect.bottom - rect.top + 2;
6192 rect.left -= 2;
6193 rect.top -= 1;
6194 TRACE("moving edit=(%d,%d)-(%d,%d)\n", rect.left, rect.top, sz.cx, sz.cy);
6195 MoveWindow(infoPtr->hwndEdit, rect.left, rect.top, sz.cx, sz.cy, FALSE);
6196 ShowWindow(infoPtr->hwndEdit, SW_NORMAL);
6197 SetFocus(infoPtr->hwndEdit);
6198 SendMessageW(infoPtr->hwndEdit, EM_SETSEL, 0, -1);
6199 return infoPtr->hwndEdit;
6203 /***
6204 * DESCRIPTION:
6205 * Ensures the specified item is visible, scrolling into view if necessary.
6207 * PARAMETER(S):
6208 * [I] infoPtr : valid pointer to the listview structure
6209 * [I] nItem : item index
6210 * [I] bPartial : partially or entirely visible
6212 * RETURN:
6213 * SUCCESS : TRUE
6214 * FAILURE : FALSE
6216 static BOOL LISTVIEW_EnsureVisible(LISTVIEW_INFO *infoPtr, INT nItem, BOOL bPartial)
6218 INT nScrollPosHeight = 0;
6219 INT nScrollPosWidth = 0;
6220 INT nHorzAdjust = 0;
6221 INT nVertAdjust = 0;
6222 INT nHorzDiff = 0;
6223 INT nVertDiff = 0;
6224 RECT rcItem, rcTemp;
6226 rcItem.left = LVIR_BOUNDS;
6227 if (!LISTVIEW_GetItemRect(infoPtr, nItem, &rcItem)) return FALSE;
6229 if (bPartial && IntersectRect(&rcTemp, &infoPtr->rcList, &rcItem)) return TRUE;
6231 if (rcItem.left < infoPtr->rcList.left || rcItem.right > infoPtr->rcList.right)
6233 /* scroll left/right, but in LV_VIEW_DETAILS mode */
6234 if (infoPtr->uView == LV_VIEW_LIST)
6235 nScrollPosWidth = infoPtr->nItemWidth;
6236 else if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
6237 nScrollPosWidth = 1;
6239 if (rcItem.left < infoPtr->rcList.left)
6241 nHorzAdjust = -1;
6242 if (infoPtr->uView != LV_VIEW_DETAILS) nHorzDiff = rcItem.left - infoPtr->rcList.left;
6244 else
6246 nHorzAdjust = 1;
6247 if (infoPtr->uView != LV_VIEW_DETAILS) nHorzDiff = rcItem.right - infoPtr->rcList.right;
6251 if (rcItem.top < infoPtr->rcList.top || rcItem.bottom > infoPtr->rcList.bottom)
6253 /* scroll up/down, but not in LVS_LIST mode */
6254 if (infoPtr->uView == LV_VIEW_DETAILS)
6255 nScrollPosHeight = infoPtr->nItemHeight;
6256 else if ((infoPtr->uView == LV_VIEW_ICON) || (infoPtr->uView == LV_VIEW_SMALLICON))
6257 nScrollPosHeight = 1;
6259 if (rcItem.top < infoPtr->rcList.top)
6261 nVertAdjust = -1;
6262 if (infoPtr->uView != LV_VIEW_LIST) nVertDiff = rcItem.top - infoPtr->rcList.top;
6264 else
6266 nVertAdjust = 1;
6267 if (infoPtr->uView != LV_VIEW_LIST) nVertDiff = rcItem.bottom - infoPtr->rcList.bottom;
6271 if (!nScrollPosWidth && !nScrollPosHeight) return TRUE;
6273 if (nScrollPosWidth)
6275 INT diff = nHorzDiff / nScrollPosWidth;
6276 if (nHorzDiff % nScrollPosWidth) diff += nHorzAdjust;
6277 LISTVIEW_HScroll(infoPtr, SB_INTERNAL, diff);
6280 if (nScrollPosHeight)
6282 INT diff = nVertDiff / nScrollPosHeight;
6283 if (nVertDiff % nScrollPosHeight) diff += nVertAdjust;
6284 LISTVIEW_VScroll(infoPtr, SB_INTERNAL, diff);
6287 return TRUE;
6290 /***
6291 * DESCRIPTION:
6292 * Searches for an item with specific characteristics.
6294 * PARAMETER(S):
6295 * [I] hwnd : window handle
6296 * [I] nStart : base item index
6297 * [I] lpFindInfo : item information to look for
6299 * RETURN:
6300 * SUCCESS : index of item
6301 * FAILURE : -1
6303 static INT LISTVIEW_FindItemW(const LISTVIEW_INFO *infoPtr, INT nStart,
6304 const LVFINDINFOW *lpFindInfo)
6306 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
6307 BOOL bWrap = FALSE, bNearest = FALSE;
6308 INT nItem = nStart + 1, nLast = infoPtr->nItemCount, nNearestItem = -1;
6309 ULONG xdist, ydist, dist, mindist = 0x7fffffff;
6310 POINT Position, Destination;
6311 LVITEMW lvItem;
6313 /* Search in virtual listviews should be done by application, not by
6314 listview control, so we just send LVN_ODFINDITEMW and return the result */
6315 if (infoPtr->dwStyle & LVS_OWNERDATA)
6317 NMLVFINDITEMW nmlv;
6319 nmlv.iStart = nStart;
6320 nmlv.lvfi = *lpFindInfo;
6321 return notify_hdr(infoPtr, LVN_ODFINDITEMW, (LPNMHDR)&nmlv.hdr);
6324 if (!lpFindInfo || nItem < 0) return -1;
6326 lvItem.mask = 0;
6327 if (lpFindInfo->flags & (LVFI_STRING | LVFI_PARTIAL) ||
6328 lpFindInfo->flags & LVFI_SUBSTRING)
6330 lvItem.mask |= LVIF_TEXT;
6331 lvItem.pszText = szDispText;
6332 lvItem.cchTextMax = DISP_TEXT_SIZE;
6335 if (lpFindInfo->flags & LVFI_WRAP)
6336 bWrap = TRUE;
6338 if ((lpFindInfo->flags & LVFI_NEARESTXY) &&
6339 (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON))
6341 POINT Origin;
6342 RECT rcArea;
6344 LISTVIEW_GetOrigin(infoPtr, &Origin);
6345 Destination.x = lpFindInfo->pt.x - Origin.x;
6346 Destination.y = lpFindInfo->pt.y - Origin.y;
6347 switch(lpFindInfo->vkDirection)
6349 case VK_DOWN: Destination.y += infoPtr->nItemHeight; break;
6350 case VK_UP: Destination.y -= infoPtr->nItemHeight; break;
6351 case VK_RIGHT: Destination.x += infoPtr->nItemWidth; break;
6352 case VK_LEFT: Destination.x -= infoPtr->nItemWidth; break;
6353 case VK_HOME: Destination.x = Destination.y = 0; break;
6354 case VK_NEXT: Destination.y += infoPtr->rcList.bottom - infoPtr->rcList.top; break;
6355 case VK_PRIOR: Destination.y -= infoPtr->rcList.bottom - infoPtr->rcList.top; break;
6356 case VK_END:
6357 LISTVIEW_GetAreaRect(infoPtr, &rcArea);
6358 Destination.x = rcArea.right;
6359 Destination.y = rcArea.bottom;
6360 break;
6361 default: ERR("Unknown vkDirection=%d\n", lpFindInfo->vkDirection);
6363 bNearest = TRUE;
6365 else Destination.x = Destination.y = 0;
6367 /* if LVFI_PARAM is specified, all other flags are ignored */
6368 if (lpFindInfo->flags & LVFI_PARAM)
6370 lvItem.mask |= LVIF_PARAM;
6371 bNearest = FALSE;
6372 lvItem.mask &= ~LVIF_TEXT;
6375 again:
6376 for (; nItem < nLast; nItem++)
6378 lvItem.iItem = nItem;
6379 lvItem.iSubItem = 0;
6380 lvItem.pszText = szDispText;
6381 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) continue;
6383 if (lvItem.mask & LVIF_PARAM)
6385 if (lpFindInfo->lParam == lvItem.lParam)
6386 return nItem;
6387 else
6388 continue;
6391 if (lvItem.mask & LVIF_TEXT)
6393 if (lpFindInfo->flags & (LVFI_PARTIAL | LVFI_SUBSTRING))
6395 WCHAR *p = strstrW(lvItem.pszText, lpFindInfo->psz);
6396 if (!p || p != lvItem.pszText) continue;
6398 else
6400 if (lstrcmpW(lvItem.pszText, lpFindInfo->psz) != 0) continue;
6404 if (!bNearest) return nItem;
6406 /* This is very inefficient. To do a good job here,
6407 * we need a sorted array of (x,y) item positions */
6408 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
6410 /* compute the distance^2 to the destination */
6411 xdist = Destination.x - Position.x;
6412 ydist = Destination.y - Position.y;
6413 dist = xdist * xdist + ydist * ydist;
6415 /* remember the distance, and item if it's closer */
6416 if (dist < mindist)
6418 mindist = dist;
6419 nNearestItem = nItem;
6423 if (bWrap)
6425 nItem = 0;
6426 nLast = min(nStart + 1, infoPtr->nItemCount);
6427 bWrap = FALSE;
6428 goto again;
6431 return nNearestItem;
6434 /***
6435 * DESCRIPTION:
6436 * Searches for an item with specific characteristics.
6438 * PARAMETER(S):
6439 * [I] hwnd : window handle
6440 * [I] nStart : base item index
6441 * [I] lpFindInfo : item information to look for
6443 * RETURN:
6444 * SUCCESS : index of item
6445 * FAILURE : -1
6447 static INT LISTVIEW_FindItemA(const LISTVIEW_INFO *infoPtr, INT nStart,
6448 const LVFINDINFOA *lpFindInfo)
6450 BOOL hasText = lpFindInfo->flags & (LVFI_STRING | LVFI_PARTIAL) ||
6451 lpFindInfo->flags & LVFI_SUBSTRING;
6452 LVFINDINFOW fiw;
6453 INT res;
6454 LPWSTR strW = NULL;
6456 memcpy(&fiw, lpFindInfo, sizeof(fiw));
6457 if (hasText) fiw.psz = strW = textdupTtoW((LPCWSTR)lpFindInfo->psz, FALSE);
6458 res = LISTVIEW_FindItemW(infoPtr, nStart, &fiw);
6459 textfreeT(strW, FALSE);
6460 return res;
6463 /***
6464 * DESCRIPTION:
6465 * Retrieves column attributes.
6467 * PARAMETER(S):
6468 * [I] infoPtr : valid pointer to the listview structure
6469 * [I] nColumn : column index
6470 * [IO] lpColumn : column information
6471 * [I] isW : if TRUE, then lpColumn is a LPLVCOLUMNW
6472 * otherwise it is in fact a LPLVCOLUMNA
6474 * RETURN:
6475 * SUCCESS : TRUE
6476 * FAILURE : FALSE
6478 static BOOL LISTVIEW_GetColumnT(const LISTVIEW_INFO *infoPtr, INT nColumn, LPLVCOLUMNW lpColumn, BOOL isW)
6480 COLUMN_INFO *lpColumnInfo;
6481 HDITEMW hdi;
6483 if (!lpColumn || nColumn < 0 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
6484 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, nColumn);
6486 /* initialize memory */
6487 ZeroMemory(&hdi, sizeof(hdi));
6489 if (lpColumn->mask & LVCF_TEXT)
6491 hdi.mask |= HDI_TEXT;
6492 hdi.pszText = lpColumn->pszText;
6493 hdi.cchTextMax = lpColumn->cchTextMax;
6496 if (lpColumn->mask & LVCF_IMAGE)
6497 hdi.mask |= HDI_IMAGE;
6499 if (lpColumn->mask & LVCF_ORDER)
6500 hdi.mask |= HDI_ORDER;
6502 if (lpColumn->mask & LVCF_SUBITEM)
6503 hdi.mask |= HDI_LPARAM;
6505 if (!SendMessageW(infoPtr->hwndHeader, isW ? HDM_GETITEMW : HDM_GETITEMA, nColumn, (LPARAM)&hdi)) return FALSE;
6507 if (lpColumn->mask & LVCF_FMT)
6508 lpColumn->fmt = lpColumnInfo->fmt;
6510 if (lpColumn->mask & LVCF_WIDTH)
6511 lpColumn->cx = lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left;
6513 if (lpColumn->mask & LVCF_IMAGE)
6514 lpColumn->iImage = hdi.iImage;
6516 if (lpColumn->mask & LVCF_ORDER)
6517 lpColumn->iOrder = hdi.iOrder;
6519 if (lpColumn->mask & LVCF_SUBITEM)
6520 lpColumn->iSubItem = hdi.lParam;
6522 if (lpColumn->mask & LVCF_MINWIDTH)
6523 lpColumn->cxMin = lpColumnInfo->cxMin;
6525 return TRUE;
6528 static inline BOOL LISTVIEW_GetColumnOrderArray(const LISTVIEW_INFO *infoPtr, INT iCount, LPINT lpiArray)
6530 if (!infoPtr->hwndHeader) return FALSE;
6531 return SendMessageW(infoPtr->hwndHeader, HDM_GETORDERARRAY, iCount, (LPARAM)lpiArray);
6534 /***
6535 * DESCRIPTION:
6536 * Retrieves the column width.
6538 * PARAMETER(S):
6539 * [I] infoPtr : valid pointer to the listview structure
6540 * [I] int : column index
6542 * RETURN:
6543 * SUCCESS : column width
6544 * FAILURE : zero
6546 static INT LISTVIEW_GetColumnWidth(const LISTVIEW_INFO *infoPtr, INT nColumn)
6548 INT nColumnWidth = 0;
6549 HDITEMW hdItem;
6551 TRACE("nColumn=%d\n", nColumn);
6553 /* we have a 'column' in LIST and REPORT mode only */
6554 switch(infoPtr->uView)
6556 case LV_VIEW_LIST:
6557 nColumnWidth = infoPtr->nItemWidth;
6558 break;
6559 case LV_VIEW_DETAILS:
6560 /* We are not using LISTVIEW_GetHeaderRect as this data is updated only after a HDN_ITEMCHANGED.
6561 * There is an application that subclasses the listview, calls LVM_GETCOLUMNWIDTH in the
6562 * HDN_ITEMCHANGED handler and goes into infinite recursion if it receives old data.
6564 hdItem.mask = HDI_WIDTH;
6565 if (!SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nColumn, (LPARAM)&hdItem))
6567 WARN("(%p): HDM_GETITEMW failed for item %d\n", infoPtr->hwndSelf, nColumn);
6568 return 0;
6570 nColumnWidth = hdItem.cxy;
6571 break;
6574 TRACE("nColumnWidth=%d\n", nColumnWidth);
6575 return nColumnWidth;
6578 /***
6579 * DESCRIPTION:
6580 * In list or report display mode, retrieves the number of items that can fit
6581 * vertically in the visible area. In icon or small icon display mode,
6582 * retrieves the total number of visible items.
6584 * PARAMETER(S):
6585 * [I] infoPtr : valid pointer to the listview structure
6587 * RETURN:
6588 * Number of fully visible items.
6590 static INT LISTVIEW_GetCountPerPage(const LISTVIEW_INFO *infoPtr)
6592 switch (infoPtr->uView)
6594 case LV_VIEW_ICON:
6595 case LV_VIEW_SMALLICON:
6596 return infoPtr->nItemCount;
6597 case LV_VIEW_DETAILS:
6598 return LISTVIEW_GetCountPerColumn(infoPtr);
6599 case LV_VIEW_LIST:
6600 return LISTVIEW_GetCountPerRow(infoPtr) * LISTVIEW_GetCountPerColumn(infoPtr);
6602 assert(FALSE);
6603 return 0;
6606 /***
6607 * DESCRIPTION:
6608 * Retrieves an image list handle.
6610 * PARAMETER(S):
6611 * [I] infoPtr : valid pointer to the listview structure
6612 * [I] nImageList : image list identifier
6614 * RETURN:
6615 * SUCCESS : image list handle
6616 * FAILURE : NULL
6618 static HIMAGELIST LISTVIEW_GetImageList(const LISTVIEW_INFO *infoPtr, INT nImageList)
6620 switch (nImageList)
6622 case LVSIL_NORMAL: return infoPtr->himlNormal;
6623 case LVSIL_SMALL: return infoPtr->himlSmall;
6624 case LVSIL_STATE: return infoPtr->himlState;
6625 case LVSIL_GROUPHEADER:
6626 FIXME("LVSIL_GROUPHEADER not supported\n");
6627 break;
6628 default:
6629 WARN("got unknown imagelist index - %d\n", nImageList);
6631 return NULL;
6634 /* LISTVIEW_GetISearchString */
6636 /***
6637 * DESCRIPTION:
6638 * Retrieves item attributes.
6640 * PARAMETER(S):
6641 * [I] hwnd : window handle
6642 * [IO] lpLVItem : item info
6643 * [I] isW : if TRUE, then lpLVItem is a LPLVITEMW,
6644 * if FALSE, then lpLVItem is a LPLVITEMA.
6646 * NOTE:
6647 * This is the internal 'GetItem' interface -- it tries to
6648 * be smart and avoid text copies, if possible, by modifying
6649 * lpLVItem->pszText to point to the text string. Please note
6650 * that this is not always possible (e.g. OWNERDATA), so on
6651 * entry you *must* supply valid values for pszText, and cchTextMax.
6652 * The only difference to the documented interface is that upon
6653 * return, you should use *only* the lpLVItem->pszText, rather than
6654 * the buffer pointer you provided on input. Most code already does
6655 * that, so it's not a problem.
6656 * For the two cases when the text must be copied (that is,
6657 * for LVM_GETITEM, and LVM_GETITEMTEXT), use LISTVIEW_GetItemExtT.
6659 * RETURN:
6660 * SUCCESS : TRUE
6661 * FAILURE : FALSE
6663 static BOOL LISTVIEW_GetItemT(const LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem, BOOL isW)
6665 ITEMHDR callbackHdr = { LPSTR_TEXTCALLBACKW, I_IMAGECALLBACK };
6666 NMLVDISPINFOW dispInfo;
6667 ITEM_INFO *lpItem;
6668 ITEMHDR* pItemHdr;
6669 HDPA hdpaSubItems;
6670 INT isubitem;
6672 TRACE("(item=%s, isW=%d)\n", debuglvitem_t(lpLVItem, isW), isW);
6674 if (!lpLVItem || lpLVItem->iItem < 0 || lpLVItem->iItem >= infoPtr->nItemCount)
6675 return FALSE;
6677 if (lpLVItem->mask == 0) return TRUE;
6678 TRACE("mask=%x\n", lpLVItem->mask);
6680 /* make a local copy */
6681 isubitem = lpLVItem->iSubItem;
6683 /* a quick optimization if all we're asked is the focus state
6684 * these queries are worth optimising since they are common,
6685 * and can be answered in constant time, without the heavy accesses */
6686 if ( (lpLVItem->mask == LVIF_STATE) && (lpLVItem->stateMask == LVIS_FOCUSED) &&
6687 !(infoPtr->uCallbackMask & LVIS_FOCUSED) )
6689 lpLVItem->state = 0;
6690 if (infoPtr->nFocusedItem == lpLVItem->iItem)
6691 lpLVItem->state |= LVIS_FOCUSED;
6692 return TRUE;
6695 ZeroMemory(&dispInfo, sizeof(dispInfo));
6697 /* if the app stores all the data, handle it separately */
6698 if (infoPtr->dwStyle & LVS_OWNERDATA)
6700 dispInfo.item.state = 0;
6702 /* apparently, we should not callback for lParam in LVS_OWNERDATA */
6703 if ((lpLVItem->mask & ~(LVIF_STATE | LVIF_PARAM)) ||
6704 ((lpLVItem->mask & LVIF_STATE) && (infoPtr->uCallbackMask & lpLVItem->stateMask)))
6706 UINT mask = lpLVItem->mask;
6708 /* NOTE: copy only fields which we _know_ are initialized, some apps
6709 * depend on the uninitialized fields being 0 */
6710 dispInfo.item.mask = lpLVItem->mask & ~LVIF_PARAM;
6711 dispInfo.item.iItem = lpLVItem->iItem;
6712 dispInfo.item.iSubItem = isubitem;
6713 if (lpLVItem->mask & LVIF_TEXT)
6715 if (lpLVItem->mask & LVIF_NORECOMPUTE)
6716 /* reset mask */
6717 dispInfo.item.mask &= ~(LVIF_TEXT | LVIF_NORECOMPUTE);
6718 else
6720 dispInfo.item.pszText = lpLVItem->pszText;
6721 dispInfo.item.cchTextMax = lpLVItem->cchTextMax;
6724 if (lpLVItem->mask & LVIF_STATE)
6725 dispInfo.item.stateMask = lpLVItem->stateMask & infoPtr->uCallbackMask;
6726 /* could be zeroed on LVIF_NORECOMPUTE case */
6727 if (dispInfo.item.mask)
6729 notify_dispinfoT(infoPtr, LVN_GETDISPINFOW, &dispInfo, isW);
6730 dispInfo.item.stateMask = lpLVItem->stateMask;
6731 if (lpLVItem->mask & (LVIF_GROUPID|LVIF_COLUMNS))
6733 /* full size structure expected - _WIN32IE >= 0x560 */
6734 *lpLVItem = dispInfo.item;
6736 else if (lpLVItem->mask & LVIF_INDENT)
6738 /* indent member expected - _WIN32IE >= 0x300 */
6739 memcpy(lpLVItem, &dispInfo.item, offsetof( LVITEMW, iGroupId ));
6741 else
6743 /* minimal structure expected */
6744 memcpy(lpLVItem, &dispInfo.item, offsetof( LVITEMW, iIndent ));
6746 lpLVItem->mask = mask;
6747 TRACE(" getdispinfo(1):lpLVItem=%s\n", debuglvitem_t(lpLVItem, isW));
6751 /* make sure lParam is zeroed out */
6752 if (lpLVItem->mask & LVIF_PARAM) lpLVItem->lParam = 0;
6754 /* callback marked pointer required here */
6755 if ((lpLVItem->mask & LVIF_TEXT) && (lpLVItem->mask & LVIF_NORECOMPUTE))
6756 lpLVItem->pszText = LPSTR_TEXTCALLBACKW;
6758 /* we store only a little state, so if we're not asked, we're done */
6759 if (!(lpLVItem->mask & LVIF_STATE) || isubitem) return TRUE;
6761 /* if focus is handled by us, report it */
6762 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_FOCUSED )
6764 lpLVItem->state &= ~LVIS_FOCUSED;
6765 if (infoPtr->nFocusedItem == lpLVItem->iItem)
6766 lpLVItem->state |= LVIS_FOCUSED;
6769 /* and do the same for selection, if we handle it */
6770 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_SELECTED )
6772 lpLVItem->state &= ~LVIS_SELECTED;
6773 if (ranges_contain(infoPtr->selectionRanges, lpLVItem->iItem))
6774 lpLVItem->state |= LVIS_SELECTED;
6777 return TRUE;
6780 /* find the item and subitem structures before we proceed */
6781 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, lpLVItem->iItem);
6782 lpItem = DPA_GetPtr(hdpaSubItems, 0);
6783 assert (lpItem);
6785 if (isubitem)
6787 SUBITEM_INFO *lpSubItem = LISTVIEW_GetSubItemPtr(hdpaSubItems, isubitem);
6788 pItemHdr = lpSubItem ? &lpSubItem->hdr : &callbackHdr;
6789 if (!lpSubItem)
6791 WARN(" iSubItem invalid (%08x), ignored.\n", isubitem);
6792 isubitem = 0;
6795 else
6796 pItemHdr = &lpItem->hdr;
6798 /* Do we need to query the state from the app? */
6799 if ((lpLVItem->mask & LVIF_STATE) && infoPtr->uCallbackMask && isubitem == 0)
6801 dispInfo.item.mask |= LVIF_STATE;
6802 dispInfo.item.stateMask = infoPtr->uCallbackMask;
6805 /* Do we need to enquire about the image? */
6806 if ((lpLVItem->mask & LVIF_IMAGE) && pItemHdr->iImage == I_IMAGECALLBACK &&
6807 (isubitem == 0 || (infoPtr->dwLvExStyle & LVS_EX_SUBITEMIMAGES)))
6809 dispInfo.item.mask |= LVIF_IMAGE;
6810 dispInfo.item.iImage = I_IMAGECALLBACK;
6813 /* Only items support indentation */
6814 if ((lpLVItem->mask & LVIF_INDENT) && lpItem->iIndent == I_INDENTCALLBACK &&
6815 (isubitem == 0))
6817 dispInfo.item.mask |= LVIF_INDENT;
6818 dispInfo.item.iIndent = I_INDENTCALLBACK;
6821 /* Apps depend on calling back for text if it is NULL or LPSTR_TEXTCALLBACKW */
6822 if ((lpLVItem->mask & LVIF_TEXT) && !(lpLVItem->mask & LVIF_NORECOMPUTE) &&
6823 !is_text(pItemHdr->pszText))
6825 dispInfo.item.mask |= LVIF_TEXT;
6826 dispInfo.item.pszText = lpLVItem->pszText;
6827 dispInfo.item.cchTextMax = lpLVItem->cchTextMax;
6828 if (dispInfo.item.pszText && dispInfo.item.cchTextMax > 0)
6829 *dispInfo.item.pszText = '\0';
6832 /* If we don't have all the requested info, query the application */
6833 if (dispInfo.item.mask)
6835 dispInfo.item.iItem = lpLVItem->iItem;
6836 dispInfo.item.iSubItem = lpLVItem->iSubItem; /* yes: the original subitem */
6837 dispInfo.item.lParam = lpItem->lParam;
6838 notify_dispinfoT(infoPtr, LVN_GETDISPINFOW, &dispInfo, isW);
6839 TRACE(" getdispinfo(2):item=%s\n", debuglvitem_t(&dispInfo.item, isW));
6842 /* we should not store values for subitems */
6843 if (isubitem) dispInfo.item.mask &= ~LVIF_DI_SETITEM;
6845 /* Now, handle the iImage field */
6846 if (dispInfo.item.mask & LVIF_IMAGE)
6848 lpLVItem->iImage = dispInfo.item.iImage;
6849 if ((dispInfo.item.mask & LVIF_DI_SETITEM) && pItemHdr->iImage == I_IMAGECALLBACK)
6850 pItemHdr->iImage = dispInfo.item.iImage;
6852 else if (lpLVItem->mask & LVIF_IMAGE)
6854 if(isubitem == 0 || (infoPtr->dwLvExStyle & LVS_EX_SUBITEMIMAGES))
6855 lpLVItem->iImage = pItemHdr->iImage;
6856 else
6857 lpLVItem->iImage = 0;
6860 /* The pszText field */
6861 if (dispInfo.item.mask & LVIF_TEXT)
6863 if ((dispInfo.item.mask & LVIF_DI_SETITEM) && pItemHdr->pszText)
6864 textsetptrT(&pItemHdr->pszText, dispInfo.item.pszText, isW);
6866 lpLVItem->pszText = dispInfo.item.pszText;
6868 else if (lpLVItem->mask & LVIF_TEXT)
6870 /* if LVN_GETDISPINFO's disabled with LVIF_NORECOMPUTE return callback placeholder */
6871 if (isW || !is_text(pItemHdr->pszText)) lpLVItem->pszText = pItemHdr->pszText;
6872 else textcpynT(lpLVItem->pszText, isW, pItemHdr->pszText, TRUE, lpLVItem->cchTextMax);
6875 /* Next is the lParam field */
6876 if (dispInfo.item.mask & LVIF_PARAM)
6878 lpLVItem->lParam = dispInfo.item.lParam;
6879 if ((dispInfo.item.mask & LVIF_DI_SETITEM))
6880 lpItem->lParam = dispInfo.item.lParam;
6882 else if (lpLVItem->mask & LVIF_PARAM)
6883 lpLVItem->lParam = lpItem->lParam;
6885 /* if this is a subitem, we're done */
6886 if (isubitem) return TRUE;
6888 /* ... the state field (this one is different due to uCallbackmask) */
6889 if (lpLVItem->mask & LVIF_STATE)
6891 lpLVItem->state = lpItem->state & lpLVItem->stateMask;
6892 if (dispInfo.item.mask & LVIF_STATE)
6894 lpLVItem->state &= ~dispInfo.item.stateMask;
6895 lpLVItem->state |= (dispInfo.item.state & dispInfo.item.stateMask);
6897 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_FOCUSED )
6899 lpLVItem->state &= ~LVIS_FOCUSED;
6900 if (infoPtr->nFocusedItem == lpLVItem->iItem)
6901 lpLVItem->state |= LVIS_FOCUSED;
6903 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_SELECTED )
6905 lpLVItem->state &= ~LVIS_SELECTED;
6906 if (ranges_contain(infoPtr->selectionRanges, lpLVItem->iItem))
6907 lpLVItem->state |= LVIS_SELECTED;
6911 /* and last, but not least, the indent field */
6912 if (dispInfo.item.mask & LVIF_INDENT)
6914 lpLVItem->iIndent = dispInfo.item.iIndent;
6915 if ((dispInfo.item.mask & LVIF_DI_SETITEM) && lpItem->iIndent == I_INDENTCALLBACK)
6916 lpItem->iIndent = dispInfo.item.iIndent;
6918 else if (lpLVItem->mask & LVIF_INDENT)
6920 lpLVItem->iIndent = lpItem->iIndent;
6923 return TRUE;
6926 /***
6927 * DESCRIPTION:
6928 * Retrieves item attributes.
6930 * PARAMETER(S):
6931 * [I] hwnd : window handle
6932 * [IO] lpLVItem : item info
6933 * [I] isW : if TRUE, then lpLVItem is a LPLVITEMW,
6934 * if FALSE, then lpLVItem is a LPLVITEMA.
6936 * NOTE:
6937 * This is the external 'GetItem' interface -- it properly copies
6938 * the text in the provided buffer.
6940 * RETURN:
6941 * SUCCESS : TRUE
6942 * FAILURE : FALSE
6944 static BOOL LISTVIEW_GetItemExtT(const LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem, BOOL isW)
6946 LPWSTR pszText;
6947 BOOL bResult;
6949 if (!lpLVItem || lpLVItem->iItem < 0 || lpLVItem->iItem >= infoPtr->nItemCount)
6950 return FALSE;
6952 pszText = lpLVItem->pszText;
6953 bResult = LISTVIEW_GetItemT(infoPtr, lpLVItem, isW);
6954 if (bResult && (lpLVItem->mask & LVIF_TEXT) && lpLVItem->pszText != pszText)
6956 if (lpLVItem->pszText != LPSTR_TEXTCALLBACKW)
6957 textcpynT(pszText, isW, lpLVItem->pszText, isW, lpLVItem->cchTextMax);
6958 else
6959 pszText = LPSTR_TEXTCALLBACKW;
6961 lpLVItem->pszText = pszText;
6963 return bResult;
6967 /***
6968 * DESCRIPTION:
6969 * Retrieves the position (upper-left) of the listview control item.
6970 * Note that for LVS_ICON style, the upper-left is that of the icon
6971 * and not the bounding box.
6973 * PARAMETER(S):
6974 * [I] infoPtr : valid pointer to the listview structure
6975 * [I] nItem : item index
6976 * [O] lpptPosition : coordinate information
6978 * RETURN:
6979 * SUCCESS : TRUE
6980 * FAILURE : FALSE
6982 static BOOL LISTVIEW_GetItemPosition(const LISTVIEW_INFO *infoPtr, INT nItem, LPPOINT lpptPosition)
6984 POINT Origin;
6986 TRACE("(nItem=%d, lpptPosition=%p)\n", nItem, lpptPosition);
6988 if (!lpptPosition || nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
6990 LISTVIEW_GetOrigin(infoPtr, &Origin);
6991 LISTVIEW_GetItemOrigin(infoPtr, nItem, lpptPosition);
6993 if (infoPtr->uView == LV_VIEW_ICON)
6995 lpptPosition->x += (infoPtr->nItemWidth - infoPtr->iconSize.cx) / 2;
6996 lpptPosition->y += ICON_TOP_PADDING;
6998 lpptPosition->x += Origin.x;
6999 lpptPosition->y += Origin.y;
7001 TRACE (" lpptPosition=%s\n", wine_dbgstr_point(lpptPosition));
7002 return TRUE;
7006 /***
7007 * DESCRIPTION:
7008 * Retrieves the bounding rectangle for a listview control item.
7010 * PARAMETER(S):
7011 * [I] infoPtr : valid pointer to the listview structure
7012 * [I] nItem : item index
7013 * [IO] lprc : bounding rectangle coordinates
7014 * lprc->left specifies the portion of the item for which the bounding
7015 * rectangle will be retrieved.
7017 * LVIR_BOUNDS Returns the bounding rectangle of the entire item,
7018 * including the icon and label.
7020 * * For LVS_ICON
7021 * * Experiment shows that native control returns:
7022 * * width = min (48, length of text line)
7023 * * .left = position.x - (width - iconsize.cx)/2
7024 * * .right = .left + width
7025 * * height = #lines of text * ntmHeight + icon height + 8
7026 * * .top = position.y - 2
7027 * * .bottom = .top + height
7028 * * separation between items .y = itemSpacing.cy - height
7029 * * .x = itemSpacing.cx - width
7030 * LVIR_ICON Returns the bounding rectangle of the icon or small icon.
7032 * * For LVS_ICON
7033 * * Experiment shows that native control returns:
7034 * * width = iconSize.cx + 16
7035 * * .left = position.x - (width - iconsize.cx)/2
7036 * * .right = .left + width
7037 * * height = iconSize.cy + 4
7038 * * .top = position.y - 2
7039 * * .bottom = .top + height
7040 * * separation between items .y = itemSpacing.cy - height
7041 * * .x = itemSpacing.cx - width
7042 * LVIR_LABEL Returns the bounding rectangle of the item text.
7044 * * For LVS_ICON
7045 * * Experiment shows that native control returns:
7046 * * width = text length
7047 * * .left = position.x - width/2
7048 * * .right = .left + width
7049 * * height = ntmH * linecount + 2
7050 * * .top = position.y + iconSize.cy + 6
7051 * * .bottom = .top + height
7052 * * separation between items .y = itemSpacing.cy - height
7053 * * .x = itemSpacing.cx - width
7054 * LVIR_SELECTBOUNDS Returns the union of the LVIR_ICON and LVIR_LABEL
7055 * rectangles, but excludes columns in report view.
7057 * RETURN:
7058 * SUCCESS : TRUE
7059 * FAILURE : FALSE
7061 * NOTES
7062 * Note that the bounding rectangle of the label in the LVS_ICON view depends
7063 * upon whether the window has the focus currently and on whether the item
7064 * is the one with the focus. Ensure that the control's record of which
7065 * item has the focus agrees with the items' records.
7067 static BOOL LISTVIEW_GetItemRect(const LISTVIEW_INFO *infoPtr, INT nItem, LPRECT lprc)
7069 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
7070 BOOL doLabel = TRUE, oversizedBox = FALSE;
7071 POINT Position, Origin;
7072 LVITEMW lvItem;
7073 LONG mode;
7075 TRACE("(hwnd=%p, nItem=%d, lprc=%p)\n", infoPtr->hwndSelf, nItem, lprc);
7077 if (!lprc || nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
7079 LISTVIEW_GetOrigin(infoPtr, &Origin);
7080 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
7082 /* Be smart and try to figure out the minimum we have to do */
7083 if (lprc->left == LVIR_ICON) doLabel = FALSE;
7084 if (infoPtr->uView == LV_VIEW_DETAILS && lprc->left == LVIR_BOUNDS) doLabel = FALSE;
7085 if (infoPtr->uView == LV_VIEW_ICON && lprc->left != LVIR_ICON &&
7086 infoPtr->bFocus && LISTVIEW_GetItemState(infoPtr, nItem, LVIS_FOCUSED))
7087 oversizedBox = TRUE;
7089 /* get what we need from the item before hand, so we make
7090 * only one request. This can speed up things, if data
7091 * is stored on the app side */
7092 lvItem.mask = 0;
7093 if (infoPtr->uView == LV_VIEW_DETAILS) lvItem.mask |= LVIF_INDENT;
7094 if (doLabel) lvItem.mask |= LVIF_TEXT;
7095 lvItem.iItem = nItem;
7096 lvItem.iSubItem = 0;
7097 lvItem.pszText = szDispText;
7098 lvItem.cchTextMax = DISP_TEXT_SIZE;
7099 if (lvItem.mask && !LISTVIEW_GetItemW(infoPtr, &lvItem)) return FALSE;
7100 /* we got the state already up, simulate it here, to avoid a reget */
7101 if (infoPtr->uView == LV_VIEW_ICON && (lprc->left != LVIR_ICON))
7103 lvItem.mask |= LVIF_STATE;
7104 lvItem.stateMask = LVIS_FOCUSED;
7105 lvItem.state = (oversizedBox ? LVIS_FOCUSED : 0);
7108 if (infoPtr->uView == LV_VIEW_DETAILS && (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) && lprc->left == LVIR_SELECTBOUNDS)
7109 lprc->left = LVIR_BOUNDS;
7111 mode = lprc->left;
7112 switch(lprc->left)
7114 case LVIR_ICON:
7115 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, NULL, lprc, NULL, NULL);
7116 break;
7118 case LVIR_LABEL:
7119 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, NULL, NULL, NULL, lprc);
7120 break;
7122 case LVIR_BOUNDS:
7123 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, lprc, NULL, NULL, NULL, NULL);
7124 break;
7126 case LVIR_SELECTBOUNDS:
7127 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, lprc, NULL, NULL, NULL);
7128 break;
7130 default:
7131 WARN("Unknown value: %d\n", lprc->left);
7132 return FALSE;
7135 if (infoPtr->uView == LV_VIEW_DETAILS)
7137 if (mode != LVIR_BOUNDS)
7138 OffsetRect(lprc, Origin.x + LISTVIEW_GetColumnInfo(infoPtr, 0)->rcHeader.left,
7139 Position.y + Origin.y);
7140 else
7141 OffsetRect(lprc, Origin.x, Position.y + Origin.y);
7143 else
7144 OffsetRect(lprc, Position.x + Origin.x, Position.y + Origin.y);
7146 TRACE(" rect=%s\n", wine_dbgstr_rect(lprc));
7148 return TRUE;
7151 /***
7152 * DESCRIPTION:
7153 * Retrieves the spacing between listview control items.
7155 * PARAMETER(S):
7156 * [I] infoPtr : valid pointer to the listview structure
7157 * [IO] lprc : rectangle to receive the output
7158 * on input, lprc->top = nSubItem
7159 * lprc->left = LVIR_ICON | LVIR_BOUNDS | LVIR_LABEL
7161 * NOTE: for subItem = 0, we should return the bounds of the _entire_ item,
7162 * not only those of the first column.
7164 * RETURN:
7165 * TRUE: success
7166 * FALSE: failure
7168 static BOOL LISTVIEW_GetSubItemRect(const LISTVIEW_INFO *infoPtr, INT item, LPRECT lprc)
7170 RECT rect = { 0, 0, 0, 0 };
7171 POINT origin;
7172 INT y;
7174 if (!lprc) return FALSE;
7176 TRACE("(item=%d, subitem=%d, type=%d)\n", item, lprc->top, lprc->left);
7177 /* Subitem of '0' means item itself, and this works for all control view modes */
7178 if (lprc->top == 0)
7179 return LISTVIEW_GetItemRect(infoPtr, item, lprc);
7181 if (infoPtr->uView != LV_VIEW_DETAILS) return FALSE;
7183 LISTVIEW_GetOrigin(infoPtr, &origin);
7184 /* this works for any item index, no matter if it exists or not */
7185 y = item * infoPtr->nItemHeight + origin.y;
7187 if (infoPtr->hwndHeader && SendMessageW(infoPtr->hwndHeader, HDM_GETITEMRECT, lprc->top, (LPARAM)&rect))
7189 rect.top = 0;
7190 rect.bottom = infoPtr->nItemHeight;
7192 else
7194 /* Native implementation is broken for this case and garbage is left for left and right fields,
7195 we zero them to get predictable output */
7196 lprc->left = lprc->right = lprc->top = 0;
7197 lprc->bottom = infoPtr->nItemHeight;
7198 OffsetRect(lprc, origin.x, y);
7199 TRACE("return rect %s\n", wine_dbgstr_rect(lprc));
7200 return TRUE;
7203 switch (lprc->left)
7205 case LVIR_ICON:
7207 /* it doesn't matter if main item actually has an icon, if imagelist is set icon width is returned */
7208 if (infoPtr->himlSmall)
7209 rect.right = rect.left + infoPtr->iconSize.cx;
7210 else
7211 rect.right = rect.left;
7213 rect.bottom = rect.top + infoPtr->iconSize.cy;
7214 break;
7216 case LVIR_LABEL:
7217 case LVIR_BOUNDS:
7218 break;
7220 default:
7221 ERR("Unknown bounds=%d\n", lprc->left);
7222 return FALSE;
7225 OffsetRect(&rect, origin.x, y);
7226 *lprc = rect;
7227 TRACE("return rect %s\n", wine_dbgstr_rect(lprc));
7229 return TRUE;
7232 /***
7233 * DESCRIPTION:
7234 * Retrieves the spacing between listview control items.
7236 * PARAMETER(S):
7237 * [I] infoPtr : valid pointer to the listview structure
7238 * [I] bSmall : flag for small or large icon
7240 * RETURN:
7241 * Horizontal + vertical spacing
7243 static LONG LISTVIEW_GetItemSpacing(const LISTVIEW_INFO *infoPtr, BOOL bSmall)
7245 LONG lResult;
7247 if (!bSmall)
7249 lResult = MAKELONG(infoPtr->iconSpacing.cx, infoPtr->iconSpacing.cy);
7251 else
7253 if (infoPtr->uView == LV_VIEW_ICON)
7254 lResult = MAKELONG(DEFAULT_COLUMN_WIDTH, GetSystemMetrics(SM_CXSMICON)+HEIGHT_PADDING);
7255 else
7256 lResult = MAKELONG(infoPtr->nItemWidth, infoPtr->nItemHeight);
7258 return lResult;
7261 /***
7262 * DESCRIPTION:
7263 * Retrieves the state of a listview control item.
7265 * PARAMETER(S):
7266 * [I] infoPtr : valid pointer to the listview structure
7267 * [I] nItem : item index
7268 * [I] uMask : state mask
7270 * RETURN:
7271 * State specified by the mask.
7273 static UINT LISTVIEW_GetItemState(const LISTVIEW_INFO *infoPtr, INT nItem, UINT uMask)
7275 LVITEMW lvItem;
7277 if (nItem < 0 || nItem >= infoPtr->nItemCount) return 0;
7279 lvItem.iItem = nItem;
7280 lvItem.iSubItem = 0;
7281 lvItem.mask = LVIF_STATE;
7282 lvItem.stateMask = uMask;
7283 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return 0;
7285 return lvItem.state & uMask;
7288 /***
7289 * DESCRIPTION:
7290 * Retrieves the text of a listview control item or subitem.
7292 * PARAMETER(S):
7293 * [I] hwnd : window handle
7294 * [I] nItem : item index
7295 * [IO] lpLVItem : item information
7296 * [I] isW : TRUE if lpLVItem is Unicode
7298 * RETURN:
7299 * SUCCESS : string length
7300 * FAILURE : 0
7302 static INT LISTVIEW_GetItemTextT(const LISTVIEW_INFO *infoPtr, INT nItem, LPLVITEMW lpLVItem, BOOL isW)
7304 if (!lpLVItem || nItem < 0 || nItem >= infoPtr->nItemCount) return 0;
7306 lpLVItem->mask = LVIF_TEXT;
7307 lpLVItem->iItem = nItem;
7308 if (!LISTVIEW_GetItemExtT(infoPtr, lpLVItem, isW)) return 0;
7310 return textlenT(lpLVItem->pszText, isW);
7313 /***
7314 * DESCRIPTION:
7315 * Searches for an item based on properties + relationships.
7317 * PARAMETER(S):
7318 * [I] infoPtr : valid pointer to the listview structure
7319 * [I] nItem : item index
7320 * [I] uFlags : relationship flag
7322 * RETURN:
7323 * SUCCESS : item index
7324 * FAILURE : -1
7326 static INT LISTVIEW_GetNextItem(const LISTVIEW_INFO *infoPtr, INT nItem, UINT uFlags)
7328 UINT uMask = 0;
7329 LVFINDINFOW lvFindInfo;
7330 INT nCountPerColumn;
7331 INT nCountPerRow;
7332 INT i;
7334 TRACE("nItem=%d, uFlags=%x, nItemCount=%d\n", nItem, uFlags, infoPtr->nItemCount);
7335 if (nItem < -1 || nItem >= infoPtr->nItemCount) return -1;
7337 ZeroMemory(&lvFindInfo, sizeof(lvFindInfo));
7339 if (uFlags & LVNI_CUT)
7340 uMask |= LVIS_CUT;
7342 if (uFlags & LVNI_DROPHILITED)
7343 uMask |= LVIS_DROPHILITED;
7345 if (uFlags & LVNI_FOCUSED)
7346 uMask |= LVIS_FOCUSED;
7348 if (uFlags & LVNI_SELECTED)
7349 uMask |= LVIS_SELECTED;
7351 /* if we're asked for the focused item, that's only one,
7352 * so it's worth optimizing */
7353 if (uFlags & LVNI_FOCUSED)
7355 if ((LISTVIEW_GetItemState(infoPtr, infoPtr->nFocusedItem, uMask) & uMask) != uMask) return -1;
7356 return (infoPtr->nFocusedItem == nItem) ? -1 : infoPtr->nFocusedItem;
7359 if (uFlags & LVNI_ABOVE)
7361 if ((infoPtr->uView == LV_VIEW_LIST) || (infoPtr->uView == LV_VIEW_DETAILS))
7363 while (nItem >= 0)
7365 nItem--;
7366 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7367 return nItem;
7370 else
7372 /* Special case for autoarrange - move 'til the top of a list */
7373 if (is_autoarrange(infoPtr))
7375 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
7376 while (nItem - nCountPerRow >= 0)
7378 nItem -= nCountPerRow;
7379 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7380 return nItem;
7382 return -1;
7384 lvFindInfo.flags = LVFI_NEARESTXY;
7385 lvFindInfo.vkDirection = VK_UP;
7386 LISTVIEW_GetItemPosition(infoPtr, nItem, &lvFindInfo.pt);
7387 while ((nItem = LISTVIEW_FindItemW(infoPtr, nItem, &lvFindInfo)) != -1)
7389 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7390 return nItem;
7394 else if (uFlags & LVNI_BELOW)
7396 if ((infoPtr->uView == LV_VIEW_LIST) || (infoPtr->uView == LV_VIEW_DETAILS))
7398 while (nItem < infoPtr->nItemCount)
7400 nItem++;
7401 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7402 return nItem;
7405 else
7407 /* Special case for autoarrange - move 'til the bottom of a list */
7408 if (is_autoarrange(infoPtr))
7410 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
7411 while (nItem + nCountPerRow < infoPtr->nItemCount )
7413 nItem += nCountPerRow;
7414 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7415 return nItem;
7417 return -1;
7419 lvFindInfo.flags = LVFI_NEARESTXY;
7420 lvFindInfo.vkDirection = VK_DOWN;
7421 LISTVIEW_GetItemPosition(infoPtr, nItem, &lvFindInfo.pt);
7422 while ((nItem = LISTVIEW_FindItemW(infoPtr, nItem, &lvFindInfo)) != -1)
7424 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7425 return nItem;
7429 else if (uFlags & LVNI_TOLEFT)
7431 if (infoPtr->uView == LV_VIEW_LIST)
7433 nCountPerColumn = LISTVIEW_GetCountPerColumn(infoPtr);
7434 while (nItem - nCountPerColumn >= 0)
7436 nItem -= nCountPerColumn;
7437 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7438 return nItem;
7441 else if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
7443 /* Special case for autoarrange - move 'til the beginning of a row */
7444 if (is_autoarrange(infoPtr))
7446 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
7447 while (nItem % nCountPerRow > 0)
7449 nItem --;
7450 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7451 return nItem;
7453 return -1;
7455 lvFindInfo.flags = LVFI_NEARESTXY;
7456 lvFindInfo.vkDirection = VK_LEFT;
7457 LISTVIEW_GetItemPosition(infoPtr, nItem, &lvFindInfo.pt);
7458 while ((nItem = LISTVIEW_FindItemW(infoPtr, nItem, &lvFindInfo)) != -1)
7460 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7461 return nItem;
7465 else if (uFlags & LVNI_TORIGHT)
7467 if (infoPtr->uView == LV_VIEW_LIST)
7469 nCountPerColumn = LISTVIEW_GetCountPerColumn(infoPtr);
7470 while (nItem + nCountPerColumn < infoPtr->nItemCount)
7472 nItem += nCountPerColumn;
7473 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7474 return nItem;
7477 else if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
7479 /* Special case for autoarrange - move 'til the end of a row */
7480 if (is_autoarrange(infoPtr))
7482 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
7483 while (nItem % nCountPerRow < nCountPerRow - 1 )
7485 nItem ++;
7486 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7487 return nItem;
7489 return -1;
7491 lvFindInfo.flags = LVFI_NEARESTXY;
7492 lvFindInfo.vkDirection = VK_RIGHT;
7493 LISTVIEW_GetItemPosition(infoPtr, nItem, &lvFindInfo.pt);
7494 while ((nItem = LISTVIEW_FindItemW(infoPtr, nItem, &lvFindInfo)) != -1)
7496 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7497 return nItem;
7501 else
7503 nItem++;
7505 /* search by index */
7506 for (i = nItem; i < infoPtr->nItemCount; i++)
7508 if ((LISTVIEW_GetItemState(infoPtr, i, uMask) & uMask) == uMask)
7509 return i;
7513 return -1;
7516 /* LISTVIEW_GetNumberOfWorkAreas */
7518 /***
7519 * DESCRIPTION:
7520 * Retrieves the origin coordinates when in icon or small icon display mode.
7522 * PARAMETER(S):
7523 * [I] infoPtr : valid pointer to the listview structure
7524 * [O] lpptOrigin : coordinate information
7526 * RETURN:
7527 * None.
7529 static void LISTVIEW_GetOrigin(const LISTVIEW_INFO *infoPtr, LPPOINT lpptOrigin)
7531 INT nHorzPos = 0, nVertPos = 0;
7532 SCROLLINFO scrollInfo;
7534 scrollInfo.cbSize = sizeof(SCROLLINFO);
7535 scrollInfo.fMask = SIF_POS;
7537 if (GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo))
7538 nHorzPos = scrollInfo.nPos;
7539 if (GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
7540 nVertPos = scrollInfo.nPos;
7542 TRACE("nHorzPos=%d, nVertPos=%d\n", nHorzPos, nVertPos);
7544 lpptOrigin->x = infoPtr->rcList.left;
7545 lpptOrigin->y = infoPtr->rcList.top;
7546 if (infoPtr->uView == LV_VIEW_LIST)
7547 nHorzPos *= infoPtr->nItemWidth;
7548 else if (infoPtr->uView == LV_VIEW_DETAILS)
7549 nVertPos *= infoPtr->nItemHeight;
7551 lpptOrigin->x -= nHorzPos;
7552 lpptOrigin->y -= nVertPos;
7554 TRACE(" origin=%s\n", wine_dbgstr_point(lpptOrigin));
7557 /***
7558 * DESCRIPTION:
7559 * Retrieves the width of a string.
7561 * PARAMETER(S):
7562 * [I] hwnd : window handle
7563 * [I] lpszText : text string to process
7564 * [I] isW : TRUE if lpszText is Unicode, FALSE otherwise
7566 * RETURN:
7567 * SUCCESS : string width (in pixels)
7568 * FAILURE : zero
7570 static INT LISTVIEW_GetStringWidthT(const LISTVIEW_INFO *infoPtr, LPCWSTR lpszText, BOOL isW)
7572 SIZE stringSize;
7574 stringSize.cx = 0;
7575 if (is_text(lpszText))
7577 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
7578 HDC hdc = GetDC(infoPtr->hwndSelf);
7579 HFONT hOldFont = SelectObject(hdc, hFont);
7581 if (isW)
7582 GetTextExtentPointW(hdc, lpszText, lstrlenW(lpszText), &stringSize);
7583 else
7584 GetTextExtentPointA(hdc, (LPCSTR)lpszText, lstrlenA((LPCSTR)lpszText), &stringSize);
7585 SelectObject(hdc, hOldFont);
7586 ReleaseDC(infoPtr->hwndSelf, hdc);
7588 return stringSize.cx;
7591 /***
7592 * DESCRIPTION:
7593 * Determines which listview item is located at the specified position.
7595 * PARAMETER(S):
7596 * [I] infoPtr : valid pointer to the listview structure
7597 * [IO] lpht : hit test information
7598 * [I] subitem : fill out iSubItem.
7599 * [I] select : return the index only if the hit selects the item
7601 * NOTE:
7602 * (mm 20001022): We must not allow iSubItem to be touched, for
7603 * an app might pass only a structure with space up to iItem!
7604 * (MS Office 97 does that for instance in the file open dialog)
7606 * RETURN:
7607 * SUCCESS : item index
7608 * FAILURE : -1
7610 static INT LISTVIEW_HitTest(const LISTVIEW_INFO *infoPtr, LPLVHITTESTINFO lpht, BOOL subitem, BOOL select)
7612 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
7613 RECT rcBox, rcBounds, rcState, rcIcon, rcLabel, rcSearch;
7614 POINT Origin, Position, opt;
7615 BOOL is_fullrow;
7616 LVITEMW lvItem;
7617 ITERATOR i;
7618 INT iItem;
7620 TRACE("(pt=%s, subitem=%d, select=%d)\n", wine_dbgstr_point(&lpht->pt), subitem, select);
7622 lpht->flags = 0;
7623 lpht->iItem = -1;
7624 if (subitem) lpht->iSubItem = 0;
7626 LISTVIEW_GetOrigin(infoPtr, &Origin);
7628 /* set whole list relation flags */
7629 if (subitem && infoPtr->uView == LV_VIEW_DETAILS)
7631 /* LVM_SUBITEMHITTEST checks left bound of possible client area */
7632 if (infoPtr->rcList.left > lpht->pt.x && Origin.x < lpht->pt.x)
7633 lpht->flags |= LVHT_TOLEFT;
7635 if (lpht->pt.y < infoPtr->rcList.top && lpht->pt.y >= 0)
7636 opt.y = lpht->pt.y + infoPtr->rcList.top;
7637 else
7638 opt.y = lpht->pt.y;
7640 if (infoPtr->rcList.bottom < opt.y)
7641 lpht->flags |= LVHT_BELOW;
7643 else
7645 if (infoPtr->rcList.left > lpht->pt.x)
7646 lpht->flags |= LVHT_TOLEFT;
7647 else if (infoPtr->rcList.right < lpht->pt.x)
7648 lpht->flags |= LVHT_TORIGHT;
7650 if (infoPtr->rcList.top > lpht->pt.y)
7651 lpht->flags |= LVHT_ABOVE;
7652 else if (infoPtr->rcList.bottom < lpht->pt.y)
7653 lpht->flags |= LVHT_BELOW;
7656 /* even if item is invalid try to find subitem */
7657 if (infoPtr->uView == LV_VIEW_DETAILS && subitem)
7659 RECT *pRect;
7660 INT j;
7662 opt.x = lpht->pt.x - Origin.x;
7664 lpht->iSubItem = -1;
7665 for (j = 0; j < DPA_GetPtrCount(infoPtr->hdpaColumns); j++)
7667 pRect = &LISTVIEW_GetColumnInfo(infoPtr, j)->rcHeader;
7669 if ((opt.x >= pRect->left) && (opt.x < pRect->right))
7671 lpht->iSubItem = j;
7672 break;
7675 TRACE("lpht->iSubItem=%d\n", lpht->iSubItem);
7677 /* if we're outside horizontal columns bounds there's nothing to test further */
7678 if (lpht->iSubItem == -1)
7680 lpht->iItem = -1;
7681 lpht->flags = LVHT_NOWHERE;
7682 return -1;
7686 TRACE("lpht->flags=0x%x\n", lpht->flags);
7687 if (lpht->flags) return -1;
7689 lpht->flags |= LVHT_NOWHERE;
7691 /* first deal with the large items */
7692 rcSearch.left = lpht->pt.x;
7693 rcSearch.top = lpht->pt.y;
7694 rcSearch.right = rcSearch.left + 1;
7695 rcSearch.bottom = rcSearch.top + 1;
7697 iterator_frameditems(&i, infoPtr, &rcSearch);
7698 iterator_next(&i); /* go to first item in the sequence */
7699 iItem = i.nItem;
7700 iterator_destroy(&i);
7702 TRACE("lpht->iItem=%d\n", iItem);
7703 if (iItem == -1) return -1;
7705 lvItem.mask = LVIF_STATE | LVIF_TEXT;
7706 if (infoPtr->uView == LV_VIEW_DETAILS) lvItem.mask |= LVIF_INDENT;
7707 lvItem.stateMask = LVIS_STATEIMAGEMASK;
7708 if (infoPtr->uView == LV_VIEW_ICON) lvItem.stateMask |= LVIS_FOCUSED;
7709 lvItem.iItem = iItem;
7710 lvItem.iSubItem = subitem ? lpht->iSubItem : 0;
7711 lvItem.pszText = szDispText;
7712 lvItem.cchTextMax = DISP_TEXT_SIZE;
7713 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return -1;
7714 if (!infoPtr->bFocus) lvItem.state &= ~LVIS_FOCUSED;
7716 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, &rcBox, NULL, &rcIcon, &rcState, &rcLabel);
7717 LISTVIEW_GetItemOrigin(infoPtr, iItem, &Position);
7718 opt.x = lpht->pt.x - Position.x - Origin.x;
7720 if (lpht->pt.y < infoPtr->rcList.top && lpht->pt.y >= 0)
7721 opt.y = lpht->pt.y - Position.y - Origin.y + infoPtr->rcList.top;
7722 else
7723 opt.y = lpht->pt.y - Position.y - Origin.y;
7725 if (infoPtr->uView == LV_VIEW_DETAILS)
7727 rcBounds = rcBox;
7728 if (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT)
7729 opt.x = lpht->pt.x - Origin.x;
7731 else
7733 UnionRect(&rcBounds, &rcIcon, &rcLabel);
7734 UnionRect(&rcBounds, &rcBounds, &rcState);
7736 TRACE("rcBounds=%s\n", wine_dbgstr_rect(&rcBounds));
7737 if (!PtInRect(&rcBounds, opt)) return -1;
7739 /* That's a special case - row rectangle is used as item rectangle and
7740 returned flags contain all item parts. */
7741 is_fullrow = (infoPtr->uView == LV_VIEW_DETAILS) && ((infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) || (infoPtr->dwStyle & LVS_OWNERDRAWFIXED));
7743 if (PtInRect(&rcIcon, opt))
7744 lpht->flags |= LVHT_ONITEMICON;
7745 else if (PtInRect(&rcLabel, opt))
7746 lpht->flags |= LVHT_ONITEMLABEL;
7747 else if (infoPtr->himlState && PtInRect(&rcState, opt))
7748 lpht->flags |= LVHT_ONITEMSTATEICON;
7749 if (is_fullrow && !(lpht->flags & LVHT_ONITEM))
7751 lpht->flags = LVHT_ONITEM | LVHT_ABOVE;
7753 if (lpht->flags & LVHT_ONITEM)
7754 lpht->flags &= ~LVHT_NOWHERE;
7755 TRACE("lpht->flags=0x%x\n", lpht->flags);
7757 if (select && !is_fullrow)
7759 if (infoPtr->uView == LV_VIEW_DETAILS)
7761 /* get main item bounds */
7762 lvItem.iSubItem = 0;
7763 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, &rcBox, NULL, &rcIcon, &rcState, &rcLabel);
7764 UnionRect(&rcBounds, &rcIcon, &rcLabel);
7765 UnionRect(&rcBounds, &rcBounds, &rcState);
7767 if (!PtInRect(&rcBounds, opt)) iItem = -1;
7769 return lpht->iItem = iItem;
7772 /***
7773 * DESCRIPTION:
7774 * Inserts a new item in the listview control.
7776 * PARAMETER(S):
7777 * [I] infoPtr : valid pointer to the listview structure
7778 * [I] lpLVItem : item information
7779 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
7781 * RETURN:
7782 * SUCCESS : new item index
7783 * FAILURE : -1
7785 static INT LISTVIEW_InsertItemT(LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem, BOOL isW)
7787 INT nItem;
7788 HDPA hdpaSubItems;
7789 NMLISTVIEW nmlv;
7790 ITEM_INFO *lpItem;
7791 ITEM_ID *lpID;
7792 BOOL is_sorted, has_changed;
7793 LVITEMW item;
7794 HWND hwndSelf = infoPtr->hwndSelf;
7796 TRACE("(item=%s, isW=%d)\n", debuglvitem_t(lpLVItem, isW), isW);
7798 if (infoPtr->dwStyle & LVS_OWNERDATA) return infoPtr->nItemCount++;
7800 /* make sure it's an item, and not a subitem; cannot insert a subitem */
7801 if (!lpLVItem || lpLVItem->iSubItem) return -1;
7803 if (!is_assignable_item(lpLVItem, infoPtr->dwStyle)) return -1;
7805 if (!(lpItem = Alloc(sizeof(ITEM_INFO)))) return -1;
7807 /* insert item in listview control data structure */
7808 if ( !(hdpaSubItems = DPA_Create(8)) ) goto fail;
7809 if ( !DPA_SetPtr(hdpaSubItems, 0, lpItem) ) assert (FALSE);
7811 /* link with id struct */
7812 if (!(lpID = Alloc(sizeof(ITEM_ID)))) goto fail;
7813 lpItem->id = lpID;
7814 lpID->item = hdpaSubItems;
7815 lpID->id = get_next_itemid(infoPtr);
7816 if ( DPA_InsertPtr(infoPtr->hdpaItemIds, infoPtr->nItemCount, lpID) == -1) goto fail;
7818 is_sorted = (infoPtr->dwStyle & (LVS_SORTASCENDING | LVS_SORTDESCENDING)) &&
7819 !(infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (LPSTR_TEXTCALLBACKW != lpLVItem->pszText);
7821 if (lpLVItem->iItem < 0 && !is_sorted) return -1;
7823 /* calculate new item index */
7824 if (is_sorted)
7826 HDPA hItem;
7827 ITEM_INFO *item_s;
7828 INT i = 0, cmpv;
7830 while (i < infoPtr->nItemCount)
7832 hItem = DPA_GetPtr( infoPtr->hdpaItems, i);
7833 item_s = DPA_GetPtr(hItem, 0);
7835 cmpv = textcmpWT(item_s->hdr.pszText, lpLVItem->pszText, isW);
7836 if (infoPtr->dwStyle & LVS_SORTDESCENDING) cmpv *= -1;
7838 if (cmpv >= 0) break;
7839 i++;
7841 nItem = i;
7843 else
7844 nItem = min(lpLVItem->iItem, infoPtr->nItemCount);
7846 TRACE("inserting at %d, sorted=%d, count=%d, iItem=%d\n", nItem, is_sorted, infoPtr->nItemCount, lpLVItem->iItem);
7847 nItem = DPA_InsertPtr( infoPtr->hdpaItems, nItem, hdpaSubItems );
7848 if (nItem == -1) goto fail;
7849 infoPtr->nItemCount++;
7851 /* shift indices first so they don't get tangled */
7852 LISTVIEW_ShiftIndices(infoPtr, nItem, 1);
7854 /* set the item attributes */
7855 if (lpLVItem->mask & (LVIF_GROUPID|LVIF_COLUMNS))
7857 /* full size structure expected - _WIN32IE >= 0x560 */
7858 item = *lpLVItem;
7860 else if (lpLVItem->mask & LVIF_INDENT)
7862 /* indent member expected - _WIN32IE >= 0x300 */
7863 memcpy(&item, lpLVItem, offsetof( LVITEMW, iGroupId ));
7865 else
7867 /* minimal structure expected */
7868 memcpy(&item, lpLVItem, offsetof( LVITEMW, iIndent ));
7870 item.iItem = nItem;
7871 if (infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES)
7873 if (item.mask & LVIF_STATE)
7875 item.stateMask |= LVIS_STATEIMAGEMASK;
7876 item.state &= ~LVIS_STATEIMAGEMASK;
7877 item.state |= INDEXTOSTATEIMAGEMASK(1);
7879 else
7881 item.mask |= LVIF_STATE;
7882 item.stateMask = LVIS_STATEIMAGEMASK;
7883 item.state = INDEXTOSTATEIMAGEMASK(1);
7887 if (!set_main_item(infoPtr, &item, TRUE, isW, &has_changed)) goto undo;
7889 /* make room for the position, if we are in the right mode */
7890 if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
7892 if (DPA_InsertPtr(infoPtr->hdpaPosX, nItem, 0) == -1)
7893 goto undo;
7894 if (DPA_InsertPtr(infoPtr->hdpaPosY, nItem, 0) == -1)
7896 DPA_DeletePtr(infoPtr->hdpaPosX, nItem);
7897 goto undo;
7901 /* send LVN_INSERTITEM notification */
7902 memset(&nmlv, 0, sizeof(NMLISTVIEW));
7903 nmlv.iItem = nItem;
7904 nmlv.lParam = lpItem->lParam;
7905 notify_listview(infoPtr, LVN_INSERTITEM, &nmlv);
7906 if (!IsWindow(hwndSelf))
7907 return -1;
7909 /* align items (set position of each item) */
7910 if (infoPtr->uView == LV_VIEW_SMALLICON || infoPtr->uView == LV_VIEW_ICON)
7912 POINT pt;
7914 if (infoPtr->dwStyle & LVS_ALIGNLEFT)
7915 LISTVIEW_NextIconPosLeft(infoPtr, &pt);
7916 else
7917 LISTVIEW_NextIconPosTop(infoPtr, &pt);
7919 LISTVIEW_MoveIconTo(infoPtr, nItem, &pt, TRUE);
7922 /* now is the invalidation fun */
7923 LISTVIEW_ScrollOnInsert(infoPtr, nItem, 1);
7924 return nItem;
7926 undo:
7927 LISTVIEW_ShiftIndices(infoPtr, nItem, -1);
7928 LISTVIEW_ShiftFocus(infoPtr, infoPtr->nFocusedItem, nItem, -1);
7929 DPA_DeletePtr(infoPtr->hdpaItems, nItem);
7930 infoPtr->nItemCount--;
7931 fail:
7932 DPA_DeletePtr(hdpaSubItems, 0);
7933 DPA_Destroy (hdpaSubItems);
7934 Free (lpItem);
7935 return -1;
7938 /***
7939 * DESCRIPTION:
7940 * Checks item visibility.
7942 * PARAMETER(S):
7943 * [I] infoPtr : valid pointer to the listview structure
7944 * [I] nFirst : item index to check for
7946 * RETURN:
7947 * Item visible : TRUE
7948 * Item invisible or failure : FALSE
7950 static BOOL LISTVIEW_IsItemVisible(const LISTVIEW_INFO *infoPtr, INT nItem)
7952 POINT Origin, Position;
7953 RECT rcItem;
7954 HDC hdc;
7955 BOOL ret;
7957 TRACE("nItem=%d\n", nItem);
7959 if (nItem < 0 || nItem >= DPA_GetPtrCount(infoPtr->hdpaItems)) return FALSE;
7961 LISTVIEW_GetOrigin(infoPtr, &Origin);
7962 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
7963 rcItem.left = Position.x + Origin.x;
7964 rcItem.top = Position.y + Origin.y;
7965 rcItem.right = rcItem.left + infoPtr->nItemWidth;
7966 rcItem.bottom = rcItem.top + infoPtr->nItemHeight;
7968 hdc = GetDC(infoPtr->hwndSelf);
7969 if (!hdc) return FALSE;
7970 ret = RectVisible(hdc, &rcItem);
7971 ReleaseDC(infoPtr->hwndSelf, hdc);
7973 return ret;
7976 /***
7977 * DESCRIPTION:
7978 * Redraws a range of items.
7980 * PARAMETER(S):
7981 * [I] infoPtr : valid pointer to the listview structure
7982 * [I] nFirst : first item
7983 * [I] nLast : last item
7985 * RETURN:
7986 * SUCCESS : TRUE
7987 * FAILURE : FALSE
7989 static BOOL LISTVIEW_RedrawItems(const LISTVIEW_INFO *infoPtr, INT nFirst, INT nLast)
7991 INT i;
7993 for (i = max(nFirst, 0); i <= min(nLast, infoPtr->nItemCount - 1); i++)
7994 LISTVIEW_InvalidateItem(infoPtr, i);
7996 return TRUE;
7999 /***
8000 * DESCRIPTION:
8001 * Scroll the content of a listview.
8003 * PARAMETER(S):
8004 * [I] infoPtr : valid pointer to the listview structure
8005 * [I] dx : horizontal scroll amount in pixels
8006 * [I] dy : vertical scroll amount in pixels
8008 * RETURN:
8009 * SUCCESS : TRUE
8010 * FAILURE : FALSE
8012 * COMMENTS:
8013 * If the control is in report view (LV_VIEW_DETAILS) the control can
8014 * be scrolled only in line increments. "dy" will be rounded to the
8015 * nearest number of pixels that are a whole line. Ex: if line height
8016 * is 16 and an 8 is passed, the list will be scrolled by 16. If a 7
8017 * is passed, then the scroll will be 0. (per MSDN 7/2002)
8019 static BOOL LISTVIEW_Scroll(LISTVIEW_INFO *infoPtr, INT dx, INT dy)
8021 switch(infoPtr->uView) {
8022 case LV_VIEW_DETAILS:
8023 dy += (dy < 0 ? -1 : 1) * infoPtr->nItemHeight/2;
8024 dy /= infoPtr->nItemHeight;
8025 break;
8026 case LV_VIEW_LIST:
8027 if (dy != 0) return FALSE;
8028 break;
8029 default: /* icon */
8030 break;
8033 if (dx != 0) LISTVIEW_HScroll(infoPtr, SB_INTERNAL, dx);
8034 if (dy != 0) LISTVIEW_VScroll(infoPtr, SB_INTERNAL, dy);
8036 return TRUE;
8039 /***
8040 * DESCRIPTION:
8041 * Sets the background color.
8043 * PARAMETER(S):
8044 * [I] infoPtr : valid pointer to the listview structure
8045 * [I] color : background color
8047 * RETURN:
8048 * SUCCESS : TRUE
8049 * FAILURE : FALSE
8051 static BOOL LISTVIEW_SetBkColor(LISTVIEW_INFO *infoPtr, COLORREF color)
8053 TRACE("(color=%x)\n", color);
8055 if(infoPtr->clrBk != color) {
8056 if (infoPtr->clrBk != CLR_NONE) DeleteObject(infoPtr->hBkBrush);
8057 infoPtr->clrBk = color;
8058 if (color == CLR_NONE)
8059 infoPtr->hBkBrush = (HBRUSH)GetClassLongPtrW(infoPtr->hwndSelf, GCLP_HBRBACKGROUND);
8060 else
8062 infoPtr->hBkBrush = CreateSolidBrush(color);
8063 infoPtr->dwLvExStyle &= ~LVS_EX_TRANSPARENTBKGND;
8067 return TRUE;
8070 /* LISTVIEW_SetBkImage */
8072 /*** Helper for {Insert,Set}ColumnT *only* */
8073 static void column_fill_hditem(const LISTVIEW_INFO *infoPtr, HDITEMW *lphdi, INT nColumn,
8074 const LVCOLUMNW *lpColumn, BOOL isW)
8076 if (lpColumn->mask & LVCF_FMT)
8078 /* format member is valid */
8079 lphdi->mask |= HDI_FORMAT;
8081 /* set text alignment (leftmost column must be left-aligned) */
8082 if (nColumn == 0 || (lpColumn->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_LEFT)
8083 lphdi->fmt |= HDF_LEFT;
8084 else if ((lpColumn->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_RIGHT)
8085 lphdi->fmt |= HDF_RIGHT;
8086 else if ((lpColumn->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_CENTER)
8087 lphdi->fmt |= HDF_CENTER;
8089 if (lpColumn->fmt & LVCFMT_BITMAP_ON_RIGHT)
8090 lphdi->fmt |= HDF_BITMAP_ON_RIGHT;
8092 if (lpColumn->fmt & LVCFMT_COL_HAS_IMAGES)
8094 lphdi->fmt |= HDF_IMAGE;
8095 lphdi->iImage = I_IMAGECALLBACK;
8098 if (lpColumn->fmt & LVCFMT_FIXED_WIDTH)
8099 lphdi->fmt |= HDF_FIXEDWIDTH;
8102 if (lpColumn->mask & LVCF_WIDTH)
8104 lphdi->mask |= HDI_WIDTH;
8105 if(lpColumn->cx == LVSCW_AUTOSIZE_USEHEADER)
8107 /* make it fill the remainder of the controls width */
8108 RECT rcHeader;
8109 INT item_index;
8111 for(item_index = 0; item_index < (nColumn - 1); item_index++)
8113 LISTVIEW_GetHeaderRect(infoPtr, item_index, &rcHeader);
8114 lphdi->cxy += rcHeader.right - rcHeader.left;
8117 /* retrieve the layout of the header */
8118 GetClientRect(infoPtr->hwndSelf, &rcHeader);
8119 TRACE("start cxy=%d rcHeader=%s\n", lphdi->cxy, wine_dbgstr_rect(&rcHeader));
8121 lphdi->cxy = (rcHeader.right - rcHeader.left) - lphdi->cxy;
8123 else
8124 lphdi->cxy = lpColumn->cx;
8127 if (lpColumn->mask & LVCF_TEXT)
8129 lphdi->mask |= HDI_TEXT | HDI_FORMAT;
8130 lphdi->fmt |= HDF_STRING;
8131 lphdi->pszText = lpColumn->pszText;
8132 lphdi->cchTextMax = textlenT(lpColumn->pszText, isW);
8135 if (lpColumn->mask & LVCF_IMAGE)
8137 lphdi->mask |= HDI_IMAGE;
8138 lphdi->iImage = lpColumn->iImage;
8141 if (lpColumn->mask & LVCF_ORDER)
8143 lphdi->mask |= HDI_ORDER;
8144 lphdi->iOrder = lpColumn->iOrder;
8149 /***
8150 * DESCRIPTION:
8151 * Inserts a new column.
8153 * PARAMETER(S):
8154 * [I] infoPtr : valid pointer to the listview structure
8155 * [I] nColumn : column index
8156 * [I] lpColumn : column information
8157 * [I] isW : TRUE if lpColumn is Unicode, FALSE otherwise
8159 * RETURN:
8160 * SUCCESS : new column index
8161 * FAILURE : -1
8163 static INT LISTVIEW_InsertColumnT(LISTVIEW_INFO *infoPtr, INT nColumn,
8164 const LVCOLUMNW *lpColumn, BOOL isW)
8166 COLUMN_INFO *lpColumnInfo;
8167 INT nNewColumn;
8168 HDITEMW hdi;
8170 TRACE("(nColumn=%d, lpColumn=%s, isW=%d)\n", nColumn, debuglvcolumn_t(lpColumn, isW), isW);
8172 if (!lpColumn || nColumn < 0) return -1;
8173 nColumn = min(nColumn, DPA_GetPtrCount(infoPtr->hdpaColumns));
8175 ZeroMemory(&hdi, sizeof(HDITEMW));
8176 column_fill_hditem(infoPtr, &hdi, nColumn, lpColumn, isW);
8179 * A mask not including LVCF_WIDTH turns into a mask of width, width 10
8180 * (can be seen in SPY) otherwise column never gets added.
8182 if (!(lpColumn->mask & LVCF_WIDTH)) {
8183 hdi.mask |= HDI_WIDTH;
8184 hdi.cxy = 10;
8188 * when the iSubItem is available Windows copies it to the header lParam. It seems
8189 * to happen only in LVM_INSERTCOLUMN - not in LVM_SETCOLUMN
8191 if (lpColumn->mask & LVCF_SUBITEM)
8193 hdi.mask |= HDI_LPARAM;
8194 hdi.lParam = lpColumn->iSubItem;
8197 /* create header if not present */
8198 LISTVIEW_CreateHeader(infoPtr);
8199 if (!(LVS_NOCOLUMNHEADER & infoPtr->dwStyle) &&
8200 (infoPtr->uView == LV_VIEW_DETAILS) && (WS_VISIBLE & infoPtr->dwStyle))
8202 ShowWindow(infoPtr->hwndHeader, SW_SHOWNORMAL);
8205 /* insert item in header control */
8206 nNewColumn = SendMessageW(infoPtr->hwndHeader,
8207 isW ? HDM_INSERTITEMW : HDM_INSERTITEMA,
8208 nColumn, (LPARAM)&hdi);
8209 if (nNewColumn == -1) return -1;
8210 if (nNewColumn != nColumn) ERR("nColumn=%d, nNewColumn=%d\n", nColumn, nNewColumn);
8212 /* create our own column info */
8213 if (!(lpColumnInfo = Alloc(sizeof(COLUMN_INFO)))) goto fail;
8214 if (DPA_InsertPtr(infoPtr->hdpaColumns, nNewColumn, lpColumnInfo) == -1) goto fail;
8216 if (lpColumn->mask & LVCF_FMT) lpColumnInfo->fmt = lpColumn->fmt;
8217 if (lpColumn->mask & LVCF_MINWIDTH) lpColumnInfo->cxMin = lpColumn->cxMin;
8218 if (!SendMessageW(infoPtr->hwndHeader, HDM_GETITEMRECT, nNewColumn, (LPARAM)&lpColumnInfo->rcHeader))
8219 goto fail;
8221 /* now we have to actually adjust the data */
8222 if (!(infoPtr->dwStyle & LVS_OWNERDATA) && infoPtr->nItemCount > 0)
8224 SUBITEM_INFO *lpSubItem;
8225 HDPA hdpaSubItems;
8226 INT nItem, i;
8227 LVITEMW item;
8228 BOOL changed;
8230 item.iSubItem = nNewColumn;
8231 item.mask = LVIF_TEXT | LVIF_IMAGE;
8232 item.iImage = I_IMAGECALLBACK;
8233 item.pszText = LPSTR_TEXTCALLBACKW;
8235 for (nItem = 0; nItem < infoPtr->nItemCount; nItem++)
8237 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, nItem);
8238 for (i = 1; i < DPA_GetPtrCount(hdpaSubItems); i++)
8240 lpSubItem = DPA_GetPtr(hdpaSubItems, i);
8241 if (lpSubItem->iSubItem >= nNewColumn)
8242 lpSubItem->iSubItem++;
8245 /* add new subitem for each item */
8246 item.iItem = nItem;
8247 set_sub_item(infoPtr, &item, isW, &changed);
8251 /* make space for the new column */
8252 LISTVIEW_ScrollColumns(infoPtr, nNewColumn + 1, lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left);
8253 LISTVIEW_UpdateItemSize(infoPtr);
8255 return nNewColumn;
8257 fail:
8258 if (nNewColumn != -1) SendMessageW(infoPtr->hwndHeader, HDM_DELETEITEM, nNewColumn, 0);
8259 if (lpColumnInfo)
8261 DPA_DeletePtr(infoPtr->hdpaColumns, nNewColumn);
8262 Free(lpColumnInfo);
8264 return -1;
8267 /***
8268 * DESCRIPTION:
8269 * Sets the attributes of a header item.
8271 * PARAMETER(S):
8272 * [I] infoPtr : valid pointer to the listview structure
8273 * [I] nColumn : column index
8274 * [I] lpColumn : column attributes
8275 * [I] isW: if TRUE, then lpColumn is a LPLVCOLUMNW, else it is a LPLVCOLUMNA
8277 * RETURN:
8278 * SUCCESS : TRUE
8279 * FAILURE : FALSE
8281 static BOOL LISTVIEW_SetColumnT(const LISTVIEW_INFO *infoPtr, INT nColumn,
8282 const LVCOLUMNW *lpColumn, BOOL isW)
8284 HDITEMW hdi, hdiget;
8285 BOOL bResult;
8287 TRACE("(nColumn=%d, lpColumn=%s, isW=%d)\n", nColumn, debuglvcolumn_t(lpColumn, isW), isW);
8289 if (!lpColumn || nColumn < 0 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
8291 ZeroMemory(&hdi, sizeof(HDITEMW));
8292 if (lpColumn->mask & LVCF_FMT)
8294 hdi.mask |= HDI_FORMAT;
8295 hdiget.mask = HDI_FORMAT;
8296 if (SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nColumn, (LPARAM)&hdiget))
8297 hdi.fmt = hdiget.fmt & HDF_STRING;
8299 column_fill_hditem(infoPtr, &hdi, nColumn, lpColumn, isW);
8301 /* set header item attributes */
8302 bResult = SendMessageW(infoPtr->hwndHeader, isW ? HDM_SETITEMW : HDM_SETITEMA, nColumn, (LPARAM)&hdi);
8303 if (!bResult) return FALSE;
8305 if (lpColumn->mask & LVCF_FMT)
8307 COLUMN_INFO *lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, nColumn);
8308 INT oldFmt = lpColumnInfo->fmt;
8310 lpColumnInfo->fmt = lpColumn->fmt;
8311 if ((oldFmt ^ lpColumn->fmt) & (LVCFMT_JUSTIFYMASK | LVCFMT_IMAGE))
8313 if (infoPtr->uView == LV_VIEW_DETAILS) LISTVIEW_InvalidateColumn(infoPtr, nColumn);
8317 if (lpColumn->mask & LVCF_MINWIDTH)
8318 LISTVIEW_GetColumnInfo(infoPtr, nColumn)->cxMin = lpColumn->cxMin;
8320 return TRUE;
8323 /***
8324 * DESCRIPTION:
8325 * Sets the column order array
8327 * PARAMETERS:
8328 * [I] infoPtr : valid pointer to the listview structure
8329 * [I] iCount : number of elements in column order array
8330 * [I] lpiArray : pointer to column order array
8332 * RETURN:
8333 * SUCCESS : TRUE
8334 * FAILURE : FALSE
8336 static BOOL LISTVIEW_SetColumnOrderArray(LISTVIEW_INFO *infoPtr, INT iCount, const INT *lpiArray)
8338 if (!infoPtr->hwndHeader) return FALSE;
8339 infoPtr->colRectsDirty = TRUE;
8340 return SendMessageW(infoPtr->hwndHeader, HDM_SETORDERARRAY, iCount, (LPARAM)lpiArray);
8343 /***
8344 * DESCRIPTION:
8345 * Sets the width of a column
8347 * PARAMETERS:
8348 * [I] infoPtr : valid pointer to the listview structure
8349 * [I] nColumn : column index
8350 * [I] cx : column width
8352 * RETURN:
8353 * SUCCESS : TRUE
8354 * FAILURE : FALSE
8356 static BOOL LISTVIEW_SetColumnWidth(LISTVIEW_INFO *infoPtr, INT nColumn, INT cx)
8358 WCHAR szDispText[DISP_TEXT_SIZE] = { 0 };
8359 INT max_cx = 0;
8360 HDITEMW hdi;
8362 TRACE("(nColumn=%d, cx=%d)\n", nColumn, cx);
8364 /* set column width only if in report or list mode */
8365 if (infoPtr->uView != LV_VIEW_DETAILS && infoPtr->uView != LV_VIEW_LIST) return FALSE;
8367 /* take care of invalid cx values - LVSCW_AUTOSIZE_* values are negative,
8368 with _USEHEADER being the lowest */
8369 if (infoPtr->uView == LV_VIEW_DETAILS && cx < LVSCW_AUTOSIZE_USEHEADER) cx = LVSCW_AUTOSIZE;
8370 else if (infoPtr->uView == LV_VIEW_LIST && cx <= 0) return FALSE;
8372 /* resize all columns if in LV_VIEW_LIST mode */
8373 if(infoPtr->uView == LV_VIEW_LIST)
8375 infoPtr->nItemWidth = cx;
8376 LISTVIEW_InvalidateList(infoPtr);
8377 return TRUE;
8380 if (nColumn < 0 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
8382 if (cx == LVSCW_AUTOSIZE || (cx == LVSCW_AUTOSIZE_USEHEADER && nColumn < DPA_GetPtrCount(infoPtr->hdpaColumns) -1))
8384 INT nLabelWidth;
8385 LVITEMW lvItem;
8387 lvItem.mask = LVIF_TEXT;
8388 lvItem.iItem = 0;
8389 lvItem.iSubItem = nColumn;
8390 lvItem.cchTextMax = DISP_TEXT_SIZE;
8391 for (; lvItem.iItem < infoPtr->nItemCount; lvItem.iItem++)
8393 lvItem.pszText = szDispText;
8394 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) continue;
8395 nLabelWidth = LISTVIEW_GetStringWidthT(infoPtr, lvItem.pszText, TRUE);
8396 if (max_cx < nLabelWidth) max_cx = nLabelWidth;
8398 if (infoPtr->himlSmall && (nColumn == 0 || (LISTVIEW_GetColumnInfo(infoPtr, nColumn)->fmt & LVCFMT_IMAGE)))
8399 max_cx += infoPtr->iconSize.cx;
8400 max_cx += TRAILING_LABEL_PADDING;
8403 /* autosize based on listview items width */
8404 if(cx == LVSCW_AUTOSIZE)
8405 cx = max_cx;
8406 else if(cx == LVSCW_AUTOSIZE_USEHEADER)
8408 /* if iCol is the last column make it fill the remainder of the controls width */
8409 if(nColumn == DPA_GetPtrCount(infoPtr->hdpaColumns) - 1)
8411 RECT rcHeader;
8412 POINT Origin;
8414 LISTVIEW_GetOrigin(infoPtr, &Origin);
8415 LISTVIEW_GetHeaderRect(infoPtr, nColumn, &rcHeader);
8417 cx = infoPtr->rcList.right - Origin.x - rcHeader.left;
8419 else
8421 /* Despite what the MS docs say, if this is not the last
8422 column, then MS resizes the column to the width of the
8423 largest text string in the column, including headers
8424 and items. This is different from LVSCW_AUTOSIZE in that
8425 LVSCW_AUTOSIZE ignores the header string length. */
8426 cx = 0;
8428 /* retrieve header text */
8429 hdi.mask = HDI_TEXT|HDI_FORMAT|HDI_IMAGE|HDI_BITMAP;
8430 hdi.cchTextMax = DISP_TEXT_SIZE;
8431 hdi.pszText = szDispText;
8432 if (SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nColumn, (LPARAM)&hdi))
8434 HDC hdc = GetDC(infoPtr->hwndSelf);
8435 HFONT old_font = SelectObject(hdc, (HFONT)SendMessageW(infoPtr->hwndHeader, WM_GETFONT, 0, 0));
8436 HIMAGELIST himl = (HIMAGELIST)SendMessageW(infoPtr->hwndHeader, HDM_GETIMAGELIST, 0, 0);
8437 INT bitmap_margin = 0;
8438 SIZE size;
8440 if (GetTextExtentPoint32W(hdc, hdi.pszText, lstrlenW(hdi.pszText), &size))
8441 cx = size.cx + TRAILING_HEADER_PADDING;
8443 if (hdi.fmt & (HDF_IMAGE|HDF_BITMAP))
8444 bitmap_margin = SendMessageW(infoPtr->hwndHeader, HDM_GETBITMAPMARGIN, 0, 0);
8446 if ((hdi.fmt & HDF_IMAGE) && himl)
8448 INT icon_cx, icon_cy;
8450 if (!ImageList_GetIconSize(himl, &icon_cx, &icon_cy))
8451 cx += icon_cx + 2*bitmap_margin;
8453 else if (hdi.fmt & HDF_BITMAP)
8455 BITMAP bmp;
8457 GetObjectW(hdi.hbm, sizeof(BITMAP), &bmp);
8458 cx += bmp.bmWidth + 2*bitmap_margin;
8461 SelectObject(hdc, old_font);
8462 ReleaseDC(infoPtr->hwndSelf, hdc);
8464 cx = max (cx, max_cx);
8468 if (cx < 0) return FALSE;
8470 /* call header to update the column change */
8471 hdi.mask = HDI_WIDTH;
8472 hdi.cxy = max(cx, LISTVIEW_GetColumnInfo(infoPtr, nColumn)->cxMin);
8473 TRACE("hdi.cxy=%d\n", hdi.cxy);
8474 return SendMessageW(infoPtr->hwndHeader, HDM_SETITEMW, nColumn, (LPARAM)&hdi);
8477 /***
8478 * Creates the checkbox imagelist. Helper for LISTVIEW_SetExtendedListViewStyle
8481 static HIMAGELIST LISTVIEW_CreateCheckBoxIL(const LISTVIEW_INFO *infoPtr)
8483 HDC hdc_wnd, hdc;
8484 HBITMAP hbm_im, hbm_mask, hbm_orig;
8485 RECT rc;
8486 HBRUSH hbr_white = GetStockObject(WHITE_BRUSH);
8487 HBRUSH hbr_black = GetStockObject(BLACK_BRUSH);
8488 HIMAGELIST himl;
8490 himl = ImageList_Create(GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON),
8491 ILC_COLOR | ILC_MASK, 2, 2);
8492 hdc_wnd = GetDC(infoPtr->hwndSelf);
8493 hdc = CreateCompatibleDC(hdc_wnd);
8494 hbm_im = CreateCompatibleBitmap(hdc_wnd, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON));
8495 hbm_mask = CreateBitmap(GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), 1, 1, NULL);
8496 ReleaseDC(infoPtr->hwndSelf, hdc_wnd);
8498 SetRect(&rc, 0, 0, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON));
8499 hbm_orig = SelectObject(hdc, hbm_mask);
8500 FillRect(hdc, &rc, hbr_white);
8501 InflateRect(&rc, -2, -2);
8502 FillRect(hdc, &rc, hbr_black);
8504 SelectObject(hdc, hbm_im);
8505 DrawFrameControl(hdc, &rc, DFC_BUTTON, DFCS_BUTTONCHECK | DFCS_MONO);
8506 SelectObject(hdc, hbm_orig);
8507 ImageList_Add(himl, hbm_im, hbm_mask);
8509 SelectObject(hdc, hbm_im);
8510 DrawFrameControl(hdc, &rc, DFC_BUTTON, DFCS_BUTTONCHECK | DFCS_MONO | DFCS_CHECKED);
8511 SelectObject(hdc, hbm_orig);
8512 ImageList_Add(himl, hbm_im, hbm_mask);
8514 DeleteObject(hbm_mask);
8515 DeleteObject(hbm_im);
8516 DeleteDC(hdc);
8518 return himl;
8521 /***
8522 * DESCRIPTION:
8523 * Sets the extended listview style.
8525 * PARAMETERS:
8526 * [I] infoPtr : valid pointer to the listview structure
8527 * [I] dwMask : mask
8528 * [I] dwStyle : style
8530 * RETURN:
8531 * SUCCESS : previous style
8532 * FAILURE : 0
8534 static DWORD LISTVIEW_SetExtendedListViewStyle(LISTVIEW_INFO *infoPtr, DWORD mask, DWORD ex_style)
8536 DWORD old_ex_style = infoPtr->dwLvExStyle;
8538 TRACE("mask=0x%08x, ex_style=0x%08x\n", mask, ex_style);
8540 /* set new style */
8541 if (mask)
8542 infoPtr->dwLvExStyle = (old_ex_style & ~mask) | (ex_style & mask);
8543 else
8544 infoPtr->dwLvExStyle = ex_style;
8546 if((infoPtr->dwLvExStyle ^ old_ex_style) & LVS_EX_CHECKBOXES)
8548 HIMAGELIST himl = 0;
8549 if(infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES)
8551 LVITEMW item;
8552 item.mask = LVIF_STATE;
8553 item.stateMask = LVIS_STATEIMAGEMASK;
8554 item.state = INDEXTOSTATEIMAGEMASK(1);
8555 LISTVIEW_SetItemState(infoPtr, -1, &item);
8557 himl = LISTVIEW_CreateCheckBoxIL(infoPtr);
8558 if(!(infoPtr->dwStyle & LVS_SHAREIMAGELISTS))
8559 ImageList_Destroy(infoPtr->himlState);
8561 himl = LISTVIEW_SetImageList(infoPtr, LVSIL_STATE, himl);
8562 /* checkbox list replaces previous custom list or... */
8563 if(((infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES) &&
8564 !(infoPtr->dwStyle & LVS_SHAREIMAGELISTS)) ||
8565 /* ...previous was checkbox list */
8566 (old_ex_style & LVS_EX_CHECKBOXES))
8567 ImageList_Destroy(himl);
8570 if((infoPtr->dwLvExStyle ^ old_ex_style) & LVS_EX_HEADERDRAGDROP)
8572 DWORD style;
8574 /* if not already created */
8575 LISTVIEW_CreateHeader(infoPtr);
8577 style = GetWindowLongW(infoPtr->hwndHeader, GWL_STYLE);
8578 if (infoPtr->dwLvExStyle & LVS_EX_HEADERDRAGDROP)
8579 style |= HDS_DRAGDROP;
8580 else
8581 style &= ~HDS_DRAGDROP;
8582 SetWindowLongW(infoPtr->hwndHeader, GWL_STYLE, style);
8585 /* GRIDLINES adds decoration at top so changes sizes */
8586 if((infoPtr->dwLvExStyle ^ old_ex_style) & LVS_EX_GRIDLINES)
8588 LISTVIEW_CreateHeader(infoPtr);
8589 LISTVIEW_UpdateSize(infoPtr);
8592 if((infoPtr->dwLvExStyle ^ old_ex_style) & LVS_EX_FULLROWSELECT)
8594 LISTVIEW_CreateHeader(infoPtr);
8597 if((infoPtr->dwLvExStyle ^ old_ex_style) & LVS_EX_TRANSPARENTBKGND)
8599 if (infoPtr->dwLvExStyle & LVS_EX_TRANSPARENTBKGND)
8600 LISTVIEW_SetBkColor(infoPtr, CLR_NONE);
8603 if((infoPtr->dwLvExStyle ^ old_ex_style) & LVS_EX_HEADERINALLVIEWS)
8605 if (infoPtr->dwLvExStyle & LVS_EX_HEADERINALLVIEWS)
8606 LISTVIEW_CreateHeader(infoPtr);
8607 else
8608 ShowWindow(infoPtr->hwndHeader, SW_HIDE);
8609 LISTVIEW_UpdateSize(infoPtr);
8610 LISTVIEW_UpdateScroll(infoPtr);
8613 LISTVIEW_InvalidateList(infoPtr);
8614 return old_ex_style;
8617 /***
8618 * DESCRIPTION:
8619 * Sets the new hot cursor used during hot tracking and hover selection.
8621 * PARAMETER(S):
8622 * [I] infoPtr : valid pointer to the listview structure
8623 * [I] hCursor : the new hot cursor handle
8625 * RETURN:
8626 * Returns the previous hot cursor
8628 static HCURSOR LISTVIEW_SetHotCursor(LISTVIEW_INFO *infoPtr, HCURSOR hCursor)
8630 HCURSOR oldCursor = infoPtr->hHotCursor;
8632 infoPtr->hHotCursor = hCursor;
8634 return oldCursor;
8638 /***
8639 * DESCRIPTION:
8640 * Sets the hot item index.
8642 * PARAMETERS:
8643 * [I] infoPtr : valid pointer to the listview structure
8644 * [I] iIndex : index
8646 * RETURN:
8647 * SUCCESS : previous hot item index
8648 * FAILURE : -1 (no hot item)
8650 static INT LISTVIEW_SetHotItem(LISTVIEW_INFO *infoPtr, INT iIndex)
8652 INT iOldIndex = infoPtr->nHotItem;
8654 infoPtr->nHotItem = iIndex;
8656 return iOldIndex;
8660 /***
8661 * DESCRIPTION:
8662 * Sets the amount of time the cursor must hover over an item before it is selected.
8664 * PARAMETER(S):
8665 * [I] infoPtr : valid pointer to the listview structure
8666 * [I] dwHoverTime : hover time, if -1 the hover time is set to the default
8668 * RETURN:
8669 * Returns the previous hover time
8671 static DWORD LISTVIEW_SetHoverTime(LISTVIEW_INFO *infoPtr, DWORD dwHoverTime)
8673 DWORD oldHoverTime = infoPtr->dwHoverTime;
8675 infoPtr->dwHoverTime = dwHoverTime;
8677 return oldHoverTime;
8680 /***
8681 * DESCRIPTION:
8682 * Sets spacing for icons of LVS_ICON style.
8684 * PARAMETER(S):
8685 * [I] infoPtr : valid pointer to the listview structure
8686 * [I] cx : horizontal spacing (-1 = system spacing, 0 = autosize)
8687 * [I] cy : vertical spacing (-1 = system spacing, 0 = autosize)
8689 * RETURN:
8690 * MAKELONG(oldcx, oldcy)
8692 static DWORD LISTVIEW_SetIconSpacing(LISTVIEW_INFO *infoPtr, INT cx, INT cy)
8694 INT iconWidth = 0, iconHeight = 0;
8695 DWORD oldspacing = MAKELONG(infoPtr->iconSpacing.cx, infoPtr->iconSpacing.cy);
8697 TRACE("requested=(%d,%d)\n", cx, cy);
8699 /* set to defaults, if instructed to */
8700 if (cx == -1 && cy == -1)
8702 infoPtr->autoSpacing = TRUE;
8703 if (infoPtr->himlNormal)
8704 ImageList_GetIconSize(infoPtr->himlNormal, &iconWidth, &iconHeight);
8705 cx = GetSystemMetrics(SM_CXICONSPACING) - GetSystemMetrics(SM_CXICON) + iconWidth;
8706 cy = GetSystemMetrics(SM_CYICONSPACING) - GetSystemMetrics(SM_CYICON) + iconHeight;
8708 else
8709 infoPtr->autoSpacing = FALSE;
8711 /* if 0 then keep width */
8712 if (cx != 0)
8713 infoPtr->iconSpacing.cx = cx;
8715 /* if 0 then keep height */
8716 if (cy != 0)
8717 infoPtr->iconSpacing.cy = cy;
8719 TRACE("old=(%d,%d), new=(%d,%d), iconSize=(%d,%d), ntmH=%d\n",
8720 LOWORD(oldspacing), HIWORD(oldspacing), infoPtr->iconSpacing.cx, infoPtr->iconSpacing.cy,
8721 infoPtr->iconSize.cx, infoPtr->iconSize.cy,
8722 infoPtr->ntmHeight);
8724 /* these depend on the iconSpacing */
8725 LISTVIEW_UpdateItemSize(infoPtr);
8727 return oldspacing;
8730 static inline void set_icon_size(SIZE *size, HIMAGELIST himl, BOOL small)
8732 INT cx, cy;
8734 if (himl && ImageList_GetIconSize(himl, &cx, &cy))
8736 size->cx = cx;
8737 size->cy = cy;
8739 else
8741 size->cx = GetSystemMetrics(small ? SM_CXSMICON : SM_CXICON);
8742 size->cy = GetSystemMetrics(small ? SM_CYSMICON : SM_CYICON);
8746 /***
8747 * DESCRIPTION:
8748 * Sets image lists.
8750 * PARAMETER(S):
8751 * [I] infoPtr : valid pointer to the listview structure
8752 * [I] nType : image list type
8753 * [I] himl : image list handle
8755 * RETURN:
8756 * SUCCESS : old image list
8757 * FAILURE : NULL
8759 static HIMAGELIST LISTVIEW_SetImageList(LISTVIEW_INFO *infoPtr, INT nType, HIMAGELIST himl)
8761 INT oldHeight = infoPtr->nItemHeight;
8762 HIMAGELIST himlOld = 0;
8764 TRACE("(nType=%d, himl=%p)\n", nType, himl);
8766 switch (nType)
8768 case LVSIL_NORMAL:
8769 himlOld = infoPtr->himlNormal;
8770 infoPtr->himlNormal = himl;
8771 if (infoPtr->uView == LV_VIEW_ICON) set_icon_size(&infoPtr->iconSize, himl, FALSE);
8772 if (infoPtr->autoSpacing)
8773 LISTVIEW_SetIconSpacing(infoPtr, -1, -1);
8774 break;
8776 case LVSIL_SMALL:
8777 himlOld = infoPtr->himlSmall;
8778 infoPtr->himlSmall = himl;
8779 if (infoPtr->uView != LV_VIEW_ICON) set_icon_size(&infoPtr->iconSize, himl, TRUE);
8780 if (infoPtr->hwndHeader)
8781 SendMessageW(infoPtr->hwndHeader, HDM_SETIMAGELIST, 0, (LPARAM)himl);
8782 break;
8784 case LVSIL_STATE:
8785 himlOld = infoPtr->himlState;
8786 infoPtr->himlState = himl;
8787 set_icon_size(&infoPtr->iconStateSize, himl, TRUE);
8788 ImageList_SetBkColor(infoPtr->himlState, CLR_NONE);
8789 break;
8791 default:
8792 ERR("Unknown icon type=%d\n", nType);
8793 return NULL;
8796 infoPtr->nItemHeight = LISTVIEW_CalculateItemHeight(infoPtr);
8797 if (infoPtr->nItemHeight != oldHeight)
8798 LISTVIEW_UpdateScroll(infoPtr);
8800 return himlOld;
8803 /***
8804 * DESCRIPTION:
8805 * Preallocates memory (does *not* set the actual count of items !)
8807 * PARAMETER(S):
8808 * [I] infoPtr : valid pointer to the listview structure
8809 * [I] nItems : item count (projected number of items to allocate)
8810 * [I] dwFlags : update flags
8812 * RETURN:
8813 * SUCCESS : TRUE
8814 * FAILURE : FALSE
8816 static BOOL LISTVIEW_SetItemCount(LISTVIEW_INFO *infoPtr, INT nItems, DWORD dwFlags)
8818 TRACE("(nItems=%d, dwFlags=%x)\n", nItems, dwFlags);
8820 if (infoPtr->dwStyle & LVS_OWNERDATA)
8822 INT nOldCount = infoPtr->nItemCount;
8823 infoPtr->nItemCount = nItems;
8825 if (nItems < nOldCount)
8827 RANGE range = { nItems, nOldCount };
8828 ranges_del(infoPtr->selectionRanges, range);
8829 if (infoPtr->nFocusedItem >= nItems)
8831 LISTVIEW_SetItemFocus(infoPtr, -1);
8832 SetRectEmpty(&infoPtr->rcFocus);
8836 LISTVIEW_UpdateScroll(infoPtr);
8838 /* the flags are valid only in ownerdata report and list modes */
8839 if (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON) dwFlags = 0;
8841 if (!(dwFlags & LVSICF_NOSCROLL) && infoPtr->nFocusedItem != -1)
8842 LISTVIEW_EnsureVisible(infoPtr, infoPtr->nFocusedItem, FALSE);
8844 if (!(dwFlags & LVSICF_NOINVALIDATEALL))
8845 LISTVIEW_InvalidateList(infoPtr);
8846 else
8848 INT nFrom, nTo;
8849 POINT Origin;
8850 RECT rcErase;
8852 LISTVIEW_GetOrigin(infoPtr, &Origin);
8853 nFrom = min(nOldCount, nItems);
8854 nTo = max(nOldCount, nItems);
8856 if (infoPtr->uView == LV_VIEW_DETAILS)
8858 SetRect(&rcErase, 0, nFrom * infoPtr->nItemHeight, infoPtr->nItemWidth,
8859 nTo * infoPtr->nItemHeight);
8860 OffsetRect(&rcErase, Origin.x, Origin.y);
8861 if (IntersectRect(&rcErase, &rcErase, &infoPtr->rcList))
8862 LISTVIEW_InvalidateRect(infoPtr, &rcErase);
8864 else /* LV_VIEW_LIST */
8866 INT nPerCol = LISTVIEW_GetCountPerColumn(infoPtr);
8868 rcErase.left = (nFrom / nPerCol) * infoPtr->nItemWidth;
8869 rcErase.top = (nFrom % nPerCol) * infoPtr->nItemHeight;
8870 rcErase.right = rcErase.left + infoPtr->nItemWidth;
8871 rcErase.bottom = nPerCol * infoPtr->nItemHeight;
8872 OffsetRect(&rcErase, Origin.x, Origin.y);
8873 if (IntersectRect(&rcErase, &rcErase, &infoPtr->rcList))
8874 LISTVIEW_InvalidateRect(infoPtr, &rcErase);
8876 rcErase.left = (nFrom / nPerCol + 1) * infoPtr->nItemWidth;
8877 rcErase.top = 0;
8878 rcErase.right = (nTo / nPerCol + 1) * infoPtr->nItemWidth;
8879 rcErase.bottom = nPerCol * infoPtr->nItemHeight;
8880 OffsetRect(&rcErase, Origin.x, Origin.y);
8881 if (IntersectRect(&rcErase, &rcErase, &infoPtr->rcList))
8882 LISTVIEW_InvalidateRect(infoPtr, &rcErase);
8886 else
8888 /* According to MSDN for non-LVS_OWNERDATA this is just
8889 * a performance issue. The control allocates its internal
8890 * data structures for the number of items specified. It
8891 * cuts down on the number of memory allocations. Therefore
8892 * we will just issue a WARN here
8894 WARN("for non-ownerdata performance option not implemented.\n");
8897 return TRUE;
8900 /***
8901 * DESCRIPTION:
8902 * Sets the position of an item.
8904 * PARAMETER(S):
8905 * [I] infoPtr : valid pointer to the listview structure
8906 * [I] nItem : item index
8907 * [I] pt : coordinate
8909 * RETURN:
8910 * SUCCESS : TRUE
8911 * FAILURE : FALSE
8913 static BOOL LISTVIEW_SetItemPosition(LISTVIEW_INFO *infoPtr, INT nItem, const POINT *pt)
8915 POINT Origin, Pt;
8917 TRACE("(nItem=%d, pt=%s)\n", nItem, wine_dbgstr_point(pt));
8919 if (!pt || nItem < 0 || nItem >= infoPtr->nItemCount ||
8920 !(infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON)) return FALSE;
8922 Pt = *pt;
8923 LISTVIEW_GetOrigin(infoPtr, &Origin);
8925 /* This point value seems to be an undocumented feature.
8926 * The best guess is that it means either at the origin,
8927 * or at true beginning of the list. I will assume the origin. */
8928 if ((Pt.x == -1) && (Pt.y == -1))
8929 Pt = Origin;
8931 if (infoPtr->uView == LV_VIEW_ICON)
8933 Pt.x -= (infoPtr->nItemWidth - infoPtr->iconSize.cx) / 2;
8934 Pt.y -= ICON_TOP_PADDING;
8936 Pt.x -= Origin.x;
8937 Pt.y -= Origin.y;
8939 infoPtr->bAutoarrange = FALSE;
8941 return LISTVIEW_MoveIconTo(infoPtr, nItem, &Pt, FALSE);
8944 /***
8945 * DESCRIPTION:
8946 * Sets the state of one or many items.
8948 * PARAMETER(S):
8949 * [I] infoPtr : valid pointer to the listview structure
8950 * [I] nItem : item index
8951 * [I] item : item or subitem info
8953 * RETURN:
8954 * SUCCESS : TRUE
8955 * FAILURE : FALSE
8957 static BOOL LISTVIEW_SetItemState(LISTVIEW_INFO *infoPtr, INT nItem, const LVITEMW *item)
8959 BOOL ret = TRUE;
8960 LVITEMW lvItem;
8962 if (!item) return FALSE;
8964 lvItem.iItem = nItem;
8965 lvItem.iSubItem = 0;
8966 lvItem.mask = LVIF_STATE;
8967 lvItem.state = item->state;
8968 lvItem.stateMask = item->stateMask;
8969 TRACE("item=%s\n", debuglvitem_t(&lvItem, TRUE));
8971 if (nItem == -1)
8973 UINT oldstate = 0;
8974 BOOL notify;
8976 /* special case optimization for recurring attempt to deselect all */
8977 if (lvItem.state == 0 && lvItem.stateMask == LVIS_SELECTED && !LISTVIEW_GetSelectedCount(infoPtr))
8978 return TRUE;
8980 /* select all isn't allowed in LVS_SINGLESEL */
8981 if ((lvItem.state & lvItem.stateMask & LVIS_SELECTED) && (infoPtr->dwStyle & LVS_SINGLESEL))
8982 return FALSE;
8984 /* focus all isn't allowed */
8985 if (lvItem.state & lvItem.stateMask & LVIS_FOCUSED) return FALSE;
8987 notify = infoPtr->bDoChangeNotify;
8988 if (infoPtr->dwStyle & LVS_OWNERDATA)
8990 infoPtr->bDoChangeNotify = FALSE;
8991 if (!(lvItem.state & LVIS_SELECTED) && LISTVIEW_GetSelectedCount(infoPtr))
8992 oldstate |= LVIS_SELECTED;
8993 if (infoPtr->nFocusedItem != -1) oldstate |= LVIS_FOCUSED;
8996 /* apply to all items */
8997 for (lvItem.iItem = 0; lvItem.iItem < infoPtr->nItemCount; lvItem.iItem++)
8998 if (!LISTVIEW_SetItemT(infoPtr, &lvItem, TRUE)) ret = FALSE;
9000 if (infoPtr->dwStyle & LVS_OWNERDATA)
9002 NMLISTVIEW nmlv;
9004 infoPtr->bDoChangeNotify = notify;
9006 nmlv.iItem = -1;
9007 nmlv.iSubItem = 0;
9008 nmlv.uNewState = lvItem.state & lvItem.stateMask;
9009 nmlv.uOldState = oldstate & lvItem.stateMask;
9010 nmlv.uChanged = LVIF_STATE;
9011 nmlv.ptAction.x = nmlv.ptAction.y = 0;
9012 nmlv.lParam = 0;
9014 notify_listview(infoPtr, LVN_ITEMCHANGED, &nmlv);
9017 else
9018 ret = LISTVIEW_SetItemT(infoPtr, &lvItem, TRUE);
9020 return ret;
9023 /***
9024 * DESCRIPTION:
9025 * Sets the text of an item or subitem.
9027 * PARAMETER(S):
9028 * [I] hwnd : window handle
9029 * [I] nItem : item index
9030 * [I] lpLVItem : item or subitem info
9031 * [I] isW : TRUE if input is Unicode
9033 * RETURN:
9034 * SUCCESS : TRUE
9035 * FAILURE : FALSE
9037 static BOOL LISTVIEW_SetItemTextT(LISTVIEW_INFO *infoPtr, INT nItem, const LVITEMW *lpLVItem, BOOL isW)
9039 LVITEMW lvItem;
9041 if (!lpLVItem || nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
9042 if (infoPtr->dwStyle & LVS_OWNERDATA) return FALSE;
9044 lvItem.iItem = nItem;
9045 lvItem.iSubItem = lpLVItem->iSubItem;
9046 lvItem.mask = LVIF_TEXT;
9047 lvItem.pszText = lpLVItem->pszText;
9048 lvItem.cchTextMax = lpLVItem->cchTextMax;
9050 TRACE("(nItem=%d, lpLVItem=%s, isW=%d)\n", nItem, debuglvitem_t(&lvItem, isW), isW);
9052 return LISTVIEW_SetItemT(infoPtr, &lvItem, isW);
9055 /***
9056 * DESCRIPTION:
9057 * Set item index that marks the start of a multiple selection.
9059 * PARAMETER(S):
9060 * [I] infoPtr : valid pointer to the listview structure
9061 * [I] nIndex : index
9063 * RETURN:
9064 * Index number or -1 if there is no selection mark.
9066 static INT LISTVIEW_SetSelectionMark(LISTVIEW_INFO *infoPtr, INT nIndex)
9068 INT nOldIndex = infoPtr->nSelectionMark;
9070 TRACE("(nIndex=%d)\n", nIndex);
9072 if (nIndex >= -1 && nIndex < infoPtr->nItemCount)
9073 infoPtr->nSelectionMark = nIndex;
9075 return nOldIndex;
9078 /***
9079 * DESCRIPTION:
9080 * Sets the text background color.
9082 * PARAMETER(S):
9083 * [I] infoPtr : valid pointer to the listview structure
9084 * [I] color : text background color
9086 * RETURN:
9087 * SUCCESS : TRUE
9088 * FAILURE : FALSE
9090 static BOOL LISTVIEW_SetTextBkColor(LISTVIEW_INFO *infoPtr, COLORREF color)
9092 TRACE("(color=%x)\n", color);
9094 infoPtr->clrTextBk = color;
9095 return TRUE;
9098 /***
9099 * DESCRIPTION:
9100 * Sets the text foreground color.
9102 * PARAMETER(S):
9103 * [I] infoPtr : valid pointer to the listview structure
9104 * [I] color : text color
9106 * RETURN:
9107 * SUCCESS : TRUE
9108 * FAILURE : FALSE
9110 static BOOL LISTVIEW_SetTextColor (LISTVIEW_INFO *infoPtr, COLORREF color)
9112 TRACE("(color=%x)\n", color);
9114 infoPtr->clrText = color;
9115 return TRUE;
9118 /***
9119 * DESCRIPTION:
9120 * Sets new ToolTip window to ListView control.
9122 * PARAMETER(S):
9123 * [I] infoPtr : valid pointer to the listview structure
9124 * [I] hwndNewToolTip : handle to new ToolTip
9126 * RETURN:
9127 * old tool tip
9129 static HWND LISTVIEW_SetToolTips( LISTVIEW_INFO *infoPtr, HWND hwndNewToolTip)
9131 HWND hwndOldToolTip = infoPtr->hwndToolTip;
9132 infoPtr->hwndToolTip = hwndNewToolTip;
9133 return hwndOldToolTip;
9137 * DESCRIPTION:
9138 * sets the Unicode character format flag for the control
9139 * PARAMETER(S):
9140 * [I] infoPtr :valid pointer to the listview structure
9141 * [I] fUnicode :true to switch to UNICODE false to switch to ANSI
9143 * RETURN:
9144 * Old Unicode Format
9146 static BOOL LISTVIEW_SetUnicodeFormat( LISTVIEW_INFO *infoPtr, BOOL unicode)
9148 SHORT rc = infoPtr->notifyFormat;
9149 infoPtr->notifyFormat = (unicode) ? NFR_UNICODE : NFR_ANSI;
9150 return rc == NFR_UNICODE;
9154 * DESCRIPTION:
9155 * sets the control view mode
9156 * PARAMETER(S):
9157 * [I] infoPtr :valid pointer to the listview structure
9158 * [I] nView :new view mode value
9160 * RETURN:
9161 * SUCCESS: 1
9162 * FAILURE: -1
9164 static INT LISTVIEW_SetView(LISTVIEW_INFO *infoPtr, DWORD nView)
9166 HIMAGELIST himl;
9168 if (infoPtr->uView == nView) return 1;
9170 if ((INT)nView < 0 || nView > LV_VIEW_MAX) return -1;
9171 if (nView == LV_VIEW_TILE)
9173 FIXME("View LV_VIEW_TILE unimplemented\n");
9174 return -1;
9177 infoPtr->uView = nView;
9179 SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
9180 ShowWindow(infoPtr->hwndHeader, SW_HIDE);
9182 ShowScrollBar(infoPtr->hwndSelf, SB_BOTH, FALSE);
9183 SetRectEmpty(&infoPtr->rcFocus);
9185 himl = (nView == LV_VIEW_ICON ? infoPtr->himlNormal : infoPtr->himlSmall);
9186 set_icon_size(&infoPtr->iconSize, himl, nView != LV_VIEW_ICON);
9188 switch (nView)
9190 case LV_VIEW_ICON:
9191 case LV_VIEW_SMALLICON:
9192 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
9193 break;
9194 case LV_VIEW_DETAILS:
9196 HDLAYOUT hl;
9197 WINDOWPOS wp;
9199 LISTVIEW_CreateHeader( infoPtr );
9201 hl.prc = &infoPtr->rcList;
9202 hl.pwpos = &wp;
9203 SendMessageW(infoPtr->hwndHeader, HDM_LAYOUT, 0, (LPARAM)&hl);
9204 SetWindowPos(infoPtr->hwndHeader, infoPtr->hwndSelf, wp.x, wp.y, wp.cx, wp.cy,
9205 wp.flags | ((infoPtr->dwStyle & LVS_NOCOLUMNHEADER) ? SWP_HIDEWINDOW : SWP_SHOWWINDOW));
9206 break;
9208 case LV_VIEW_LIST:
9209 break;
9212 LISTVIEW_UpdateItemSize(infoPtr);
9213 LISTVIEW_UpdateSize(infoPtr);
9214 LISTVIEW_UpdateScroll(infoPtr);
9215 LISTVIEW_InvalidateList(infoPtr);
9217 TRACE("nView=%d\n", nView);
9219 return 1;
9222 /* LISTVIEW_SetWorkAreas */
9224 /***
9225 * DESCRIPTION:
9226 * Callback internally used by LISTVIEW_SortItems() in response of LVM_SORTITEMS
9228 * PARAMETER(S):
9229 * [I] first : pointer to first ITEM_INFO to compare
9230 * [I] second : pointer to second ITEM_INFO to compare
9231 * [I] lParam : HWND of control
9233 * RETURN:
9234 * if first comes before second : negative
9235 * if first comes after second : positive
9236 * if first and second are equivalent : zero
9238 static INT WINAPI LISTVIEW_CallBackCompare(LPVOID first, LPVOID second, LPARAM lParam)
9240 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)lParam;
9241 ITEM_INFO* lv_first = DPA_GetPtr( first, 0 );
9242 ITEM_INFO* lv_second = DPA_GetPtr( second, 0 );
9244 /* Forward the call to the client defined callback */
9245 return (infoPtr->pfnCompare)( lv_first->lParam , lv_second->lParam, infoPtr->lParamSort );
9248 /***
9249 * DESCRIPTION:
9250 * Callback internally used by LISTVIEW_SortItems() in response of LVM_SORTITEMSEX
9252 * PARAMETER(S):
9253 * [I] first : pointer to first ITEM_INFO to compare
9254 * [I] second : pointer to second ITEM_INFO to compare
9255 * [I] lParam : HWND of control
9257 * RETURN:
9258 * if first comes before second : negative
9259 * if first comes after second : positive
9260 * if first and second are equivalent : zero
9262 static INT WINAPI LISTVIEW_CallBackCompareEx(LPVOID first, LPVOID second, LPARAM lParam)
9264 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)lParam;
9265 INT first_idx = DPA_GetPtrIndex( infoPtr->hdpaItems, first );
9266 INT second_idx = DPA_GetPtrIndex( infoPtr->hdpaItems, second );
9268 /* Forward the call to the client defined callback */
9269 return (infoPtr->pfnCompare)( first_idx, second_idx, infoPtr->lParamSort );
9272 /***
9273 * DESCRIPTION:
9274 * Sorts the listview items.
9276 * PARAMETER(S):
9277 * [I] infoPtr : valid pointer to the listview structure
9278 * [I] pfnCompare : application-defined value
9279 * [I] lParamSort : pointer to comparison callback
9280 * [I] IsEx : TRUE when LVM_SORTITEMSEX used
9282 * RETURN:
9283 * SUCCESS : TRUE
9284 * FAILURE : FALSE
9286 static BOOL LISTVIEW_SortItems(LISTVIEW_INFO *infoPtr, PFNLVCOMPARE pfnCompare,
9287 LPARAM lParamSort, BOOL IsEx)
9289 HDPA hdpaSubItems;
9290 ITEM_INFO *lpItem;
9291 LPVOID selectionMarkItem = NULL;
9292 LPVOID focusedItem = NULL;
9293 int i;
9295 TRACE("(pfnCompare=%p, lParamSort=%lx)\n", pfnCompare, lParamSort);
9297 if (infoPtr->dwStyle & LVS_OWNERDATA) return FALSE;
9299 if (!pfnCompare) return FALSE;
9300 if (!infoPtr->hdpaItems) return FALSE;
9302 /* if there are 0 or 1 items, there is no need to sort */
9303 if (infoPtr->nItemCount < 2) return TRUE;
9305 /* clear selection */
9306 ranges_clear(infoPtr->selectionRanges);
9308 /* save selection mark and focused item */
9309 if (infoPtr->nSelectionMark >= 0)
9310 selectionMarkItem = DPA_GetPtr(infoPtr->hdpaItems, infoPtr->nSelectionMark);
9311 if (infoPtr->nFocusedItem >= 0)
9312 focusedItem = DPA_GetPtr(infoPtr->hdpaItems, infoPtr->nFocusedItem);
9314 infoPtr->pfnCompare = pfnCompare;
9315 infoPtr->lParamSort = lParamSort;
9316 if (IsEx)
9317 DPA_Sort(infoPtr->hdpaItems, LISTVIEW_CallBackCompareEx, (LPARAM)infoPtr);
9318 else
9319 DPA_Sort(infoPtr->hdpaItems, LISTVIEW_CallBackCompare, (LPARAM)infoPtr);
9321 /* restore selection ranges */
9322 for (i=0; i < infoPtr->nItemCount; i++)
9324 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, i);
9325 lpItem = DPA_GetPtr(hdpaSubItems, 0);
9327 if (lpItem->state & LVIS_SELECTED)
9328 ranges_additem(infoPtr->selectionRanges, i);
9330 /* restore selection mark and focused item */
9331 infoPtr->nSelectionMark = DPA_GetPtrIndex(infoPtr->hdpaItems, selectionMarkItem);
9332 infoPtr->nFocusedItem = DPA_GetPtrIndex(infoPtr->hdpaItems, focusedItem);
9334 /* I believe nHotItem should be left alone, see LISTVIEW_ShiftIndices */
9336 /* refresh the display */
9337 LISTVIEW_InvalidateList(infoPtr);
9338 return TRUE;
9341 /***
9342 * DESCRIPTION:
9343 * Update theme handle after a theme change.
9345 * PARAMETER(S):
9346 * [I] infoPtr : valid pointer to the listview structure
9348 * RETURN:
9349 * SUCCESS : 0
9350 * FAILURE : something else
9352 static LRESULT LISTVIEW_ThemeChanged(const LISTVIEW_INFO *infoPtr)
9354 HTHEME theme = GetWindowTheme(infoPtr->hwndSelf);
9355 CloseThemeData(theme);
9356 OpenThemeData(infoPtr->hwndSelf, themeClass);
9357 return 0;
9360 /***
9361 * DESCRIPTION:
9362 * Updates an items or rearranges the listview control.
9364 * PARAMETER(S):
9365 * [I] infoPtr : valid pointer to the listview structure
9366 * [I] nItem : item index
9368 * RETURN:
9369 * SUCCESS : TRUE
9370 * FAILURE : FALSE
9372 static BOOL LISTVIEW_Update(LISTVIEW_INFO *infoPtr, INT nItem)
9374 TRACE("(nItem=%d)\n", nItem);
9376 if (nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
9378 /* rearrange with default alignment style */
9379 if (is_autoarrange(infoPtr))
9380 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
9381 else
9382 LISTVIEW_InvalidateItem(infoPtr, nItem);
9384 return TRUE;
9387 /***
9388 * DESCRIPTION:
9389 * Draw the track line at the place defined in the infoPtr structure.
9390 * The line is drawn with a XOR pen so drawing the line for the second time
9391 * in the same place erases the line.
9393 * PARAMETER(S):
9394 * [I] infoPtr : valid pointer to the listview structure
9396 * RETURN:
9397 * SUCCESS : TRUE
9398 * FAILURE : FALSE
9400 static BOOL LISTVIEW_DrawTrackLine(const LISTVIEW_INFO *infoPtr)
9402 HDC hdc;
9404 if (infoPtr->xTrackLine == -1)
9405 return FALSE;
9407 if (!(hdc = GetDC(infoPtr->hwndSelf)))
9408 return FALSE;
9409 PatBlt( hdc, infoPtr->xTrackLine, infoPtr->rcList.top,
9410 1, infoPtr->rcList.bottom - infoPtr->rcList.top, DSTINVERT );
9411 ReleaseDC(infoPtr->hwndSelf, hdc);
9412 return TRUE;
9415 /***
9416 * DESCRIPTION:
9417 * Called when an edit control should be displayed. This function is called after
9418 * we are sure that there was a single click - not a double click (this is a TIMERPROC).
9420 * PARAMETER(S):
9421 * [I] hwnd : Handle to the listview
9422 * [I] uMsg : WM_TIMER (ignored)
9423 * [I] idEvent : The timer ID interpreted as a pointer to a DELAYED_EDIT_ITEM struct
9424 * [I] dwTimer : The elapsed time (ignored)
9426 * RETURN:
9427 * None.
9429 static VOID CALLBACK LISTVIEW_DelayedEditItem(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
9431 DELAYED_ITEM_EDIT *editItem = (DELAYED_ITEM_EDIT *)idEvent;
9432 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongPtrW(hwnd, 0);
9434 KillTimer(hwnd, idEvent);
9435 editItem->fEnabled = FALSE;
9436 /* check if the item is still selected */
9437 if (infoPtr->bFocus && LISTVIEW_GetItemState(infoPtr, editItem->iItem, LVIS_SELECTED))
9438 LISTVIEW_EditLabelT(infoPtr, editItem->iItem, TRUE);
9441 /***
9442 * DESCRIPTION:
9443 * Creates the listview control - the WM_NCCREATE phase.
9445 * PARAMETER(S):
9446 * [I] hwnd : window handle
9447 * [I] lpcs : the create parameters
9449 * RETURN:
9450 * Success: TRUE
9451 * Failure: FALSE
9453 static LRESULT LISTVIEW_NCCreate(HWND hwnd, const CREATESTRUCTW *lpcs)
9455 LISTVIEW_INFO *infoPtr;
9456 LOGFONTW logFont;
9458 TRACE("(lpcs=%p)\n", lpcs);
9460 /* initialize info pointer */
9461 infoPtr = Alloc(sizeof(LISTVIEW_INFO));
9462 if (!infoPtr) return FALSE;
9464 SetWindowLongPtrW(hwnd, 0, (DWORD_PTR)infoPtr);
9466 infoPtr->hwndSelf = hwnd;
9467 infoPtr->dwStyle = lpcs->style; /* Note: may be changed in WM_CREATE */
9468 map_style_view(infoPtr);
9469 /* determine the type of structures to use */
9470 infoPtr->hwndNotify = lpcs->hwndParent;
9471 /* infoPtr->notifyFormat will be filled in WM_CREATE */
9473 /* initialize color information */
9474 infoPtr->clrBk = CLR_NONE;
9475 infoPtr->clrText = CLR_DEFAULT;
9476 infoPtr->clrTextBk = CLR_DEFAULT;
9477 LISTVIEW_SetBkColor(infoPtr, comctl32_color.clrWindow);
9479 /* set default values */
9480 infoPtr->nFocusedItem = -1;
9481 infoPtr->nSelectionMark = -1;
9482 infoPtr->nHotItem = -1;
9483 infoPtr->redraw = TRUE;
9484 infoPtr->bNoItemMetrics = TRUE;
9485 infoPtr->bDoChangeNotify = TRUE;
9486 infoPtr->autoSpacing = TRUE;
9487 infoPtr->iconSpacing.cx = GetSystemMetrics(SM_CXICONSPACING) - GetSystemMetrics(SM_CXICON);
9488 infoPtr->iconSpacing.cy = GetSystemMetrics(SM_CYICONSPACING) - GetSystemMetrics(SM_CYICON);
9489 infoPtr->nEditLabelItem = -1;
9490 infoPtr->nLButtonDownItem = -1;
9491 infoPtr->dwHoverTime = HOVER_DEFAULT; /* default system hover time */
9492 infoPtr->cWheelRemainder = 0;
9493 infoPtr->nMeasureItemHeight = 0;
9494 infoPtr->xTrackLine = -1; /* no track line */
9495 infoPtr->itemEdit.fEnabled = FALSE;
9496 infoPtr->iVersion = COMCTL32_VERSION;
9497 infoPtr->colRectsDirty = FALSE;
9499 /* get default font (icon title) */
9500 SystemParametersInfoW(SPI_GETICONTITLELOGFONT, 0, &logFont, 0);
9501 infoPtr->hDefaultFont = CreateFontIndirectW(&logFont);
9502 infoPtr->hFont = infoPtr->hDefaultFont;
9503 LISTVIEW_SaveTextMetrics(infoPtr);
9505 /* allocate memory for the data structure */
9506 if (!(infoPtr->selectionRanges = ranges_create(10))) goto fail;
9507 if (!(infoPtr->hdpaItems = DPA_Create(10))) goto fail;
9508 if (!(infoPtr->hdpaItemIds = DPA_Create(10))) goto fail;
9509 if (!(infoPtr->hdpaPosX = DPA_Create(10))) goto fail;
9510 if (!(infoPtr->hdpaPosY = DPA_Create(10))) goto fail;
9511 if (!(infoPtr->hdpaColumns = DPA_Create(10))) goto fail;
9512 return TRUE;
9514 fail:
9515 DestroyWindow(infoPtr->hwndHeader);
9516 ranges_destroy(infoPtr->selectionRanges);
9517 DPA_Destroy(infoPtr->hdpaItems);
9518 DPA_Destroy(infoPtr->hdpaItemIds);
9519 DPA_Destroy(infoPtr->hdpaPosX);
9520 DPA_Destroy(infoPtr->hdpaPosY);
9521 DPA_Destroy(infoPtr->hdpaColumns);
9522 Free(infoPtr);
9523 return FALSE;
9526 /***
9527 * DESCRIPTION:
9528 * Creates the listview control - the WM_CREATE phase. Most of the data is
9529 * already set up in LISTVIEW_NCCreate
9531 * PARAMETER(S):
9532 * [I] hwnd : window handle
9533 * [I] lpcs : the create parameters
9535 * RETURN:
9536 * Success: 0
9537 * Failure: -1
9539 static LRESULT LISTVIEW_Create(HWND hwnd, const CREATESTRUCTW *lpcs)
9541 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongPtrW(hwnd, 0);
9543 TRACE("(lpcs=%p, style=0x%08x)\n", lpcs, lpcs->style);
9545 infoPtr->dwStyle = lpcs->style;
9546 map_style_view(infoPtr);
9548 infoPtr->notifyFormat = SendMessageW(infoPtr->hwndNotify, WM_NOTIFYFORMAT,
9549 (WPARAM)infoPtr->hwndSelf, NF_QUERY);
9550 /* on error defaulting to ANSI notifications */
9551 if (infoPtr->notifyFormat == 0) infoPtr->notifyFormat = NFR_ANSI;
9552 TRACE("notify format=%d\n", infoPtr->notifyFormat);
9554 if ((infoPtr->uView == LV_VIEW_DETAILS) && (lpcs->style & WS_VISIBLE))
9556 if (LISTVIEW_CreateHeader(infoPtr) < 0) return -1;
9558 else
9559 infoPtr->hwndHeader = 0;
9561 /* init item size to avoid division by 0 */
9562 LISTVIEW_UpdateItemSize (infoPtr);
9563 LISTVIEW_UpdateSize (infoPtr);
9565 if (infoPtr->uView == LV_VIEW_DETAILS)
9567 if (!(LVS_NOCOLUMNHEADER & lpcs->style) && (WS_VISIBLE & lpcs->style))
9569 ShowWindow(infoPtr->hwndHeader, SW_SHOWNORMAL);
9571 LISTVIEW_UpdateScroll(infoPtr);
9572 /* send WM_MEASUREITEM notification */
9573 if (infoPtr->dwStyle & LVS_OWNERDRAWFIXED) notify_measureitem(infoPtr);
9576 OpenThemeData(hwnd, themeClass);
9578 /* initialize the icon sizes */
9579 set_icon_size(&infoPtr->iconSize, infoPtr->himlNormal, infoPtr->uView != LV_VIEW_ICON);
9580 set_icon_size(&infoPtr->iconStateSize, infoPtr->himlState, TRUE);
9581 return 0;
9584 /***
9585 * DESCRIPTION:
9586 * Destroys the listview control.
9588 * PARAMETER(S):
9589 * [I] infoPtr : valid pointer to the listview structure
9591 * RETURN:
9592 * Success: 0
9593 * Failure: -1
9595 static LRESULT LISTVIEW_Destroy(LISTVIEW_INFO *infoPtr)
9597 HTHEME theme = GetWindowTheme(infoPtr->hwndSelf);
9598 CloseThemeData(theme);
9600 /* delete all items */
9601 LISTVIEW_DeleteAllItems(infoPtr, TRUE);
9603 return 0;
9606 /***
9607 * DESCRIPTION:
9608 * Enables the listview control.
9610 * PARAMETER(S):
9611 * [I] infoPtr : valid pointer to the listview structure
9612 * [I] bEnable : specifies whether to enable or disable the window
9614 * RETURN:
9615 * SUCCESS : TRUE
9616 * FAILURE : FALSE
9618 static BOOL LISTVIEW_Enable(const LISTVIEW_INFO *infoPtr)
9620 if (infoPtr->dwStyle & LVS_OWNERDRAWFIXED)
9621 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
9622 return TRUE;
9625 /***
9626 * DESCRIPTION:
9627 * Erases the background of the listview control.
9629 * PARAMETER(S):
9630 * [I] infoPtr : valid pointer to the listview structure
9631 * [I] hdc : device context handle
9633 * RETURN:
9634 * SUCCESS : TRUE
9635 * FAILURE : FALSE
9637 static inline BOOL LISTVIEW_EraseBkgnd(const LISTVIEW_INFO *infoPtr, HDC hdc)
9639 RECT rc;
9641 TRACE("(hdc=%p)\n", hdc);
9643 if (!GetClipBox(hdc, &rc)) return FALSE;
9645 if (infoPtr->clrBk == CLR_NONE)
9647 if (infoPtr->dwLvExStyle & LVS_EX_TRANSPARENTBKGND)
9648 return SendMessageW(infoPtr->hwndNotify, WM_PRINTCLIENT,
9649 (WPARAM)hdc, PRF_ERASEBKGND);
9650 else
9651 return SendMessageW(infoPtr->hwndNotify, WM_ERASEBKGND, (WPARAM)hdc, 0);
9654 /* for double buffered controls we need to do this during refresh */
9655 if (infoPtr->dwLvExStyle & LVS_EX_DOUBLEBUFFER) return FALSE;
9657 return LISTVIEW_FillBkgnd(infoPtr, hdc, &rc);
9661 /***
9662 * DESCRIPTION:
9663 * Helper function for LISTVIEW_[HV]Scroll *only*.
9664 * Performs vertical/horizontal scrolling by a give amount.
9666 * PARAMETER(S):
9667 * [I] infoPtr : valid pointer to the listview structure
9668 * [I] dx : amount of horizontal scroll
9669 * [I] dy : amount of vertical scroll
9671 static void scroll_list(LISTVIEW_INFO *infoPtr, INT dx, INT dy)
9673 /* now we can scroll the list */
9674 ScrollWindowEx(infoPtr->hwndSelf, dx, dy, &infoPtr->rcList,
9675 &infoPtr->rcList, 0, 0, SW_ERASE | SW_INVALIDATE);
9676 /* if we have focus, adjust rect */
9677 OffsetRect(&infoPtr->rcFocus, dx, dy);
9678 UpdateWindow(infoPtr->hwndSelf);
9681 /***
9682 * DESCRIPTION:
9683 * Performs vertical scrolling.
9685 * PARAMETER(S):
9686 * [I] infoPtr : valid pointer to the listview structure
9687 * [I] nScrollCode : scroll code
9688 * [I] nScrollDiff : units to scroll in SB_INTERNAL mode, 0 otherwise
9689 * [I] hScrollWnd : scrollbar control window handle
9691 * RETURN:
9692 * Zero
9694 * NOTES:
9695 * SB_LINEUP/SB_LINEDOWN:
9696 * for LVS_ICON, LVS_SMALLICON is 37 by experiment
9697 * for LVS_REPORT is 1 line
9698 * for LVS_LIST cannot occur
9701 static LRESULT LISTVIEW_VScroll(LISTVIEW_INFO *infoPtr, INT nScrollCode,
9702 INT nScrollDiff)
9704 INT nOldScrollPos, nNewScrollPos;
9705 SCROLLINFO scrollInfo;
9706 BOOL is_an_icon;
9708 TRACE("(nScrollCode=%d(%s), nScrollDiff=%d)\n", nScrollCode,
9709 debugscrollcode(nScrollCode), nScrollDiff);
9711 if (infoPtr->hwndEdit) SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
9713 scrollInfo.cbSize = sizeof(SCROLLINFO);
9714 scrollInfo.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_TRACKPOS;
9716 is_an_icon = ((infoPtr->uView == LV_VIEW_ICON) || (infoPtr->uView == LV_VIEW_SMALLICON));
9718 if (!GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo)) return 1;
9720 nOldScrollPos = scrollInfo.nPos;
9721 switch (nScrollCode)
9723 case SB_INTERNAL:
9724 break;
9726 case SB_LINEUP:
9727 nScrollDiff = (is_an_icon) ? -LISTVIEW_SCROLL_ICON_LINE_SIZE : -1;
9728 break;
9730 case SB_LINEDOWN:
9731 nScrollDiff = (is_an_icon) ? LISTVIEW_SCROLL_ICON_LINE_SIZE : 1;
9732 break;
9734 case SB_PAGEUP:
9735 nScrollDiff = -scrollInfo.nPage;
9736 break;
9738 case SB_PAGEDOWN:
9739 nScrollDiff = scrollInfo.nPage;
9740 break;
9742 case SB_THUMBPOSITION:
9743 case SB_THUMBTRACK:
9744 nScrollDiff = scrollInfo.nTrackPos - scrollInfo.nPos;
9745 break;
9747 default:
9748 nScrollDiff = 0;
9751 /* quit right away if pos isn't changing */
9752 if (nScrollDiff == 0) return 0;
9754 /* calculate new position, and handle overflows */
9755 nNewScrollPos = scrollInfo.nPos + nScrollDiff;
9756 if (nScrollDiff > 0) {
9757 if (nNewScrollPos < nOldScrollPos ||
9758 nNewScrollPos > scrollInfo.nMax)
9759 nNewScrollPos = scrollInfo.nMax;
9760 } else {
9761 if (nNewScrollPos > nOldScrollPos ||
9762 nNewScrollPos < scrollInfo.nMin)
9763 nNewScrollPos = scrollInfo.nMin;
9766 /* set the new position, and reread in case it changed */
9767 scrollInfo.fMask = SIF_POS;
9768 scrollInfo.nPos = nNewScrollPos;
9769 nNewScrollPos = SetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo, TRUE);
9771 /* carry on only if it really changed */
9772 if (nNewScrollPos == nOldScrollPos) return 0;
9774 /* now adjust to client coordinates */
9775 nScrollDiff = nOldScrollPos - nNewScrollPos;
9776 if (infoPtr->uView == LV_VIEW_DETAILS) nScrollDiff *= infoPtr->nItemHeight;
9778 /* and scroll the window */
9779 scroll_list(infoPtr, 0, nScrollDiff);
9781 return 0;
9784 /***
9785 * DESCRIPTION:
9786 * Performs horizontal scrolling.
9788 * PARAMETER(S):
9789 * [I] infoPtr : valid pointer to the listview structure
9790 * [I] nScrollCode : scroll code
9791 * [I] nScrollDiff : units to scroll in SB_INTERNAL mode, 0 otherwise
9792 * [I] hScrollWnd : scrollbar control window handle
9794 * RETURN:
9795 * Zero
9797 * NOTES:
9798 * SB_LINELEFT/SB_LINERIGHT:
9799 * for LVS_ICON, LVS_SMALLICON 1 pixel
9800 * for LVS_REPORT is 1 pixel
9801 * for LVS_LIST is 1 column --> which is a 1 because the
9802 * scroll is based on columns not pixels
9805 static LRESULT LISTVIEW_HScroll(LISTVIEW_INFO *infoPtr, INT nScrollCode,
9806 INT nScrollDiff)
9808 INT nOldScrollPos, nNewScrollPos;
9809 SCROLLINFO scrollInfo;
9810 BOOL is_an_icon;
9812 TRACE("(nScrollCode=%d(%s), nScrollDiff=%d)\n", nScrollCode,
9813 debugscrollcode(nScrollCode), nScrollDiff);
9815 if (infoPtr->hwndEdit) SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
9817 scrollInfo.cbSize = sizeof(SCROLLINFO);
9818 scrollInfo.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_TRACKPOS;
9820 is_an_icon = ((infoPtr->uView == LV_VIEW_ICON) || (infoPtr->uView == LV_VIEW_SMALLICON));
9822 if (!GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo)) return 1;
9824 nOldScrollPos = scrollInfo.nPos;
9826 switch (nScrollCode)
9828 case SB_INTERNAL:
9829 break;
9831 case SB_LINELEFT:
9832 nScrollDiff = (is_an_icon) ? -LISTVIEW_SCROLL_ICON_LINE_SIZE : -1;
9833 break;
9835 case SB_LINERIGHT:
9836 nScrollDiff = (is_an_icon) ? LISTVIEW_SCROLL_ICON_LINE_SIZE : 1;
9837 break;
9839 case SB_PAGELEFT:
9840 nScrollDiff = -scrollInfo.nPage;
9841 break;
9843 case SB_PAGERIGHT:
9844 nScrollDiff = scrollInfo.nPage;
9845 break;
9847 case SB_THUMBPOSITION:
9848 case SB_THUMBTRACK:
9849 nScrollDiff = scrollInfo.nTrackPos - scrollInfo.nPos;
9850 break;
9852 default:
9853 nScrollDiff = 0;
9856 /* quit right away if pos isn't changing */
9857 if (nScrollDiff == 0) return 0;
9859 /* calculate new position, and handle overflows */
9860 nNewScrollPos = scrollInfo.nPos + nScrollDiff;
9861 if (nScrollDiff > 0) {
9862 if (nNewScrollPos < nOldScrollPos ||
9863 nNewScrollPos > scrollInfo.nMax)
9864 nNewScrollPos = scrollInfo.nMax;
9865 } else {
9866 if (nNewScrollPos > nOldScrollPos ||
9867 nNewScrollPos < scrollInfo.nMin)
9868 nNewScrollPos = scrollInfo.nMin;
9871 /* set the new position, and reread in case it changed */
9872 scrollInfo.fMask = SIF_POS;
9873 scrollInfo.nPos = nNewScrollPos;
9874 nNewScrollPos = SetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo, TRUE);
9876 /* carry on only if it really changed */
9877 if (nNewScrollPos == nOldScrollPos) return 0;
9879 if (infoPtr->hwndHeader) LISTVIEW_UpdateHeaderSize(infoPtr, nNewScrollPos);
9881 /* now adjust to client coordinates */
9882 nScrollDiff = nOldScrollPos - nNewScrollPos;
9883 if (infoPtr->uView == LV_VIEW_LIST) nScrollDiff *= infoPtr->nItemWidth;
9885 /* and scroll the window */
9886 scroll_list(infoPtr, nScrollDiff, 0);
9888 return 0;
9891 static LRESULT LISTVIEW_MouseWheel(LISTVIEW_INFO *infoPtr, INT wheelDelta)
9893 UINT pulScrollLines = 3;
9895 TRACE("(wheelDelta=%d)\n", wheelDelta);
9897 switch(infoPtr->uView)
9899 case LV_VIEW_ICON:
9900 case LV_VIEW_SMALLICON:
9902 * listview should be scrolled by a multiple of 37 dependently on its dimension or its visible item number
9903 * should be fixed in the future.
9905 LISTVIEW_VScroll(infoPtr, SB_INTERNAL, (wheelDelta > 0) ?
9906 -LISTVIEW_SCROLL_ICON_LINE_SIZE : LISTVIEW_SCROLL_ICON_LINE_SIZE);
9907 break;
9909 case LV_VIEW_DETAILS:
9910 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
9912 /* if scrolling changes direction, ignore left overs */
9913 if ((wheelDelta < 0 && infoPtr->cWheelRemainder < 0) ||
9914 (wheelDelta > 0 && infoPtr->cWheelRemainder > 0))
9915 infoPtr->cWheelRemainder += wheelDelta;
9916 else
9917 infoPtr->cWheelRemainder = wheelDelta;
9918 if (infoPtr->cWheelRemainder && pulScrollLines)
9920 int cLineScroll;
9921 pulScrollLines = min((UINT)LISTVIEW_GetCountPerColumn(infoPtr), pulScrollLines);
9922 cLineScroll = pulScrollLines * (float)infoPtr->cWheelRemainder / WHEEL_DELTA;
9923 infoPtr->cWheelRemainder -= WHEEL_DELTA * cLineScroll / (int)pulScrollLines;
9924 LISTVIEW_VScroll(infoPtr, SB_INTERNAL, -cLineScroll);
9926 break;
9928 case LV_VIEW_LIST:
9929 LISTVIEW_HScroll(infoPtr, (wheelDelta > 0) ? SB_LINELEFT : SB_LINERIGHT, 0);
9930 break;
9932 return 0;
9935 /***
9936 * DESCRIPTION:
9937 * ???
9939 * PARAMETER(S):
9940 * [I] infoPtr : valid pointer to the listview structure
9941 * [I] nVirtualKey : virtual key
9942 * [I] lKeyData : key data
9944 * RETURN:
9945 * Zero
9947 static LRESULT LISTVIEW_KeyDown(LISTVIEW_INFO *infoPtr, INT nVirtualKey, LONG lKeyData)
9949 HWND hwndSelf = infoPtr->hwndSelf;
9950 INT nItem = -1;
9951 NMLVKEYDOWN nmKeyDown;
9953 TRACE("(nVirtualKey=%d, lKeyData=%d)\n", nVirtualKey, lKeyData);
9955 /* send LVN_KEYDOWN notification */
9956 nmKeyDown.wVKey = nVirtualKey;
9957 nmKeyDown.flags = 0;
9958 notify_hdr(infoPtr, LVN_KEYDOWN, &nmKeyDown.hdr);
9959 if (!IsWindow(hwndSelf))
9960 return 0;
9962 switch (nVirtualKey)
9964 case VK_SPACE:
9965 nItem = infoPtr->nFocusedItem;
9966 if (infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES)
9967 toggle_checkbox_state(infoPtr, infoPtr->nFocusedItem);
9968 break;
9970 case VK_RETURN:
9971 if ((infoPtr->nItemCount > 0) && (infoPtr->nFocusedItem != -1))
9973 if (!notify(infoPtr, NM_RETURN)) return 0;
9974 if (!notify(infoPtr, LVN_ITEMACTIVATE)) return 0;
9976 break;
9978 case VK_HOME:
9979 if (infoPtr->nItemCount > 0)
9980 nItem = 0;
9981 break;
9983 case VK_END:
9984 if (infoPtr->nItemCount > 0)
9985 nItem = infoPtr->nItemCount - 1;
9986 break;
9988 case VK_LEFT:
9989 nItem = LISTVIEW_GetNextItem(infoPtr, infoPtr->nFocusedItem, LVNI_TOLEFT);
9990 break;
9992 case VK_UP:
9993 nItem = LISTVIEW_GetNextItem(infoPtr, infoPtr->nFocusedItem, LVNI_ABOVE);
9994 break;
9996 case VK_RIGHT:
9997 nItem = LISTVIEW_GetNextItem(infoPtr, infoPtr->nFocusedItem, LVNI_TORIGHT);
9998 break;
10000 case VK_DOWN:
10001 nItem = LISTVIEW_GetNextItem(infoPtr, infoPtr->nFocusedItem, LVNI_BELOW);
10002 break;
10004 case VK_PRIOR:
10005 if (infoPtr->uView == LV_VIEW_DETAILS)
10007 INT topidx = LISTVIEW_GetTopIndex(infoPtr);
10008 if (infoPtr->nFocusedItem == topidx)
10009 nItem = topidx - LISTVIEW_GetCountPerColumn(infoPtr) + 1;
10010 else
10011 nItem = topidx;
10013 else
10014 nItem = infoPtr->nFocusedItem - LISTVIEW_GetCountPerColumn(infoPtr)
10015 * LISTVIEW_GetCountPerRow(infoPtr);
10016 if(nItem < 0) nItem = 0;
10017 break;
10019 case VK_NEXT:
10020 if (infoPtr->uView == LV_VIEW_DETAILS)
10022 INT topidx = LISTVIEW_GetTopIndex(infoPtr);
10023 INT cnt = LISTVIEW_GetCountPerColumn(infoPtr);
10024 if (infoPtr->nFocusedItem == topidx + cnt - 1)
10025 nItem = infoPtr->nFocusedItem + cnt - 1;
10026 else
10027 nItem = topidx + cnt - 1;
10029 else
10030 nItem = infoPtr->nFocusedItem + LISTVIEW_GetCountPerColumn(infoPtr)
10031 * LISTVIEW_GetCountPerRow(infoPtr);
10032 if(nItem >= infoPtr->nItemCount) nItem = infoPtr->nItemCount - 1;
10033 break;
10036 if ((nItem != -1) && (nItem != infoPtr->nFocusedItem || nVirtualKey == VK_SPACE))
10037 LISTVIEW_KeySelection(infoPtr, nItem, nVirtualKey == VK_SPACE);
10039 return 0;
10042 /***
10043 * DESCRIPTION:
10044 * Kills the focus.
10046 * PARAMETER(S):
10047 * [I] infoPtr : valid pointer to the listview structure
10049 * RETURN:
10050 * Zero
10052 static LRESULT LISTVIEW_KillFocus(LISTVIEW_INFO *infoPtr)
10054 TRACE("()\n");
10056 /* drop any left over scroll amount */
10057 infoPtr->cWheelRemainder = 0;
10059 /* if we did not have the focus, there's nothing more to do */
10060 if (!infoPtr->bFocus) return 0;
10062 /* send NM_KILLFOCUS notification */
10063 if (!notify(infoPtr, NM_KILLFOCUS)) return 0;
10065 /* if we have a focus rectangle, get rid of it */
10066 LISTVIEW_ShowFocusRect(infoPtr, FALSE);
10068 /* if have a marquee selection, stop it */
10069 if (infoPtr->bMarqueeSelect)
10071 /* Remove the marquee rectangle and release our mouse capture */
10072 LISTVIEW_InvalidateRect(infoPtr, &infoPtr->marqueeRect);
10073 ReleaseCapture();
10075 SetRectEmpty(&infoPtr->marqueeRect);
10077 infoPtr->bMarqueeSelect = FALSE;
10078 infoPtr->bScrolling = FALSE;
10079 KillTimer(infoPtr->hwndSelf, (UINT_PTR) infoPtr);
10082 /* set window focus flag */
10083 infoPtr->bFocus = FALSE;
10085 /* invalidate the selected items before resetting focus flag */
10086 LISTVIEW_InvalidateSelectedItems(infoPtr);
10088 return 0;
10091 /***
10092 * DESCRIPTION:
10093 * Processes double click messages (left mouse button).
10095 * PARAMETER(S):
10096 * [I] infoPtr : valid pointer to the listview structure
10097 * [I] wKey : key flag
10098 * [I] x,y : mouse coordinate
10100 * RETURN:
10101 * Zero
10103 static LRESULT LISTVIEW_LButtonDblClk(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
10105 LVHITTESTINFO htInfo;
10107 TRACE("(key=%hu, X=%u, Y=%u)\n", wKey, x, y);
10109 /* Cancel the item edition if any */
10110 if (infoPtr->itemEdit.fEnabled)
10112 KillTimer(infoPtr->hwndSelf, (UINT_PTR)&infoPtr->itemEdit);
10113 infoPtr->itemEdit.fEnabled = FALSE;
10116 /* send NM_RELEASEDCAPTURE notification */
10117 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
10119 htInfo.pt.x = x;
10120 htInfo.pt.y = y;
10122 /* send NM_DBLCLK notification */
10123 LISTVIEW_HitTest(infoPtr, &htInfo, TRUE, FALSE);
10124 if (!notify_click(infoPtr, NM_DBLCLK, &htInfo)) return 0;
10126 /* To send the LVN_ITEMACTIVATE, it must be on an Item */
10127 if(htInfo.iItem != -1) notify_itemactivate(infoPtr,&htInfo);
10129 return 0;
10132 static LRESULT LISTVIEW_TrackMouse(const LISTVIEW_INFO *infoPtr, POINT pt)
10134 MSG msg;
10135 RECT r;
10137 r.top = r.bottom = pt.y;
10138 r.left = r.right = pt.x;
10140 InflateRect(&r, GetSystemMetrics(SM_CXDRAG), GetSystemMetrics(SM_CYDRAG));
10142 SetCapture(infoPtr->hwndSelf);
10144 while (1)
10146 if (PeekMessageW(&msg, 0, 0, 0, PM_REMOVE | PM_NOYIELD))
10148 if (msg.message == WM_MOUSEMOVE)
10150 pt.x = (short)LOWORD(msg.lParam);
10151 pt.y = (short)HIWORD(msg.lParam);
10152 if (PtInRect(&r, pt))
10153 continue;
10154 else
10156 ReleaseCapture();
10157 return 1;
10160 else if (msg.message >= WM_LBUTTONDOWN &&
10161 msg.message <= WM_RBUTTONDBLCLK)
10163 break;
10166 DispatchMessageW(&msg);
10169 if (GetCapture() != infoPtr->hwndSelf)
10170 return 0;
10173 ReleaseCapture();
10174 return 0;
10178 /***
10179 * DESCRIPTION:
10180 * Processes mouse down messages (left mouse button).
10182 * PARAMETERS:
10183 * infoPtr [I ] valid pointer to the listview structure
10184 * wKey [I ] key flag
10185 * x,y [I ] mouse coordinate
10187 * RETURN:
10188 * Zero
10190 static LRESULT LISTVIEW_LButtonDown(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
10192 LVHITTESTINFO lvHitTestInfo;
10193 static BOOL bGroupSelect = TRUE;
10194 POINT pt = { x, y };
10195 INT nItem;
10197 TRACE("(key=%hu, X=%u, Y=%u)\n", wKey, x, y);
10199 /* send NM_RELEASEDCAPTURE notification */
10200 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
10202 /* set left button down flag and record the click position */
10203 infoPtr->bLButtonDown = TRUE;
10204 infoPtr->ptClickPos = pt;
10205 infoPtr->bDragging = FALSE;
10206 infoPtr->bMarqueeSelect = FALSE;
10207 infoPtr->bScrolling = FALSE;
10209 lvHitTestInfo.pt.x = x;
10210 lvHitTestInfo.pt.y = y;
10212 nItem = LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, TRUE);
10213 TRACE("at %s, nItem=%d\n", wine_dbgstr_point(&pt), nItem);
10214 if ((nItem >= 0) && (nItem < infoPtr->nItemCount))
10216 if ((infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES) && (lvHitTestInfo.flags & LVHT_ONITEMSTATEICON))
10218 toggle_checkbox_state(infoPtr, nItem);
10219 return 0;
10222 if (infoPtr->dwStyle & LVS_SINGLESEL)
10224 if (LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED))
10225 infoPtr->nEditLabelItem = nItem;
10226 else
10227 LISTVIEW_SetSelection(infoPtr, nItem);
10229 else
10231 if ((wKey & MK_CONTROL) && (wKey & MK_SHIFT))
10233 if (bGroupSelect)
10235 if (!LISTVIEW_AddGroupSelection(infoPtr, nItem)) return 0;
10236 LISTVIEW_SetItemFocus(infoPtr, nItem);
10237 infoPtr->nSelectionMark = nItem;
10239 else
10241 LVITEMW item;
10243 item.state = LVIS_SELECTED | LVIS_FOCUSED;
10244 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
10246 LISTVIEW_SetItemState(infoPtr,nItem,&item);
10247 infoPtr->nSelectionMark = nItem;
10250 else if (wKey & MK_CONTROL)
10252 LVITEMW item;
10254 bGroupSelect = (LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED) == 0);
10256 item.state = (bGroupSelect ? LVIS_SELECTED : 0) | LVIS_FOCUSED;
10257 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
10258 LISTVIEW_SetItemState(infoPtr, nItem, &item);
10259 infoPtr->nSelectionMark = nItem;
10261 else if (wKey & MK_SHIFT)
10263 LISTVIEW_SetGroupSelection(infoPtr, nItem);
10265 else
10267 if (LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED))
10269 infoPtr->nEditLabelItem = nItem;
10270 infoPtr->nLButtonDownItem = nItem;
10272 LISTVIEW_SetItemFocus(infoPtr, nItem);
10274 else
10275 /* set selection (clears other pre-existing selections) */
10276 LISTVIEW_SetSelection(infoPtr, nItem);
10280 if (!infoPtr->bFocus)
10281 SetFocus(infoPtr->hwndSelf);
10283 if (infoPtr->dwLvExStyle & LVS_EX_ONECLICKACTIVATE)
10284 if(lvHitTestInfo.iItem != -1) notify_itemactivate(infoPtr,&lvHitTestInfo);
10286 else
10288 if (!infoPtr->bFocus)
10289 SetFocus(infoPtr->hwndSelf);
10291 /* remove all selections */
10292 if (!(wKey & MK_CONTROL) && !(wKey & MK_SHIFT))
10293 LISTVIEW_DeselectAll(infoPtr);
10294 ReleaseCapture();
10297 return 0;
10300 /***
10301 * DESCRIPTION:
10302 * Processes mouse up messages (left mouse button).
10304 * PARAMETERS:
10305 * infoPtr [I ] valid pointer to the listview structure
10306 * wKey [I ] key flag
10307 * x,y [I ] mouse coordinate
10309 * RETURN:
10310 * Zero
10312 static LRESULT LISTVIEW_LButtonUp(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
10314 LVHITTESTINFO lvHitTestInfo;
10316 TRACE("(key=%hu, X=%u, Y=%u)\n", wKey, x, y);
10318 if (!infoPtr->bLButtonDown) return 0;
10320 lvHitTestInfo.pt.x = x;
10321 lvHitTestInfo.pt.y = y;
10323 /* send NM_CLICK notification */
10324 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
10325 if (!notify_click(infoPtr, NM_CLICK, &lvHitTestInfo)) return 0;
10327 /* set left button flag */
10328 infoPtr->bLButtonDown = FALSE;
10330 /* set a single selection, reset others */
10331 if(lvHitTestInfo.iItem == infoPtr->nLButtonDownItem && lvHitTestInfo.iItem != -1)
10332 LISTVIEW_SetSelection(infoPtr, infoPtr->nLButtonDownItem);
10333 infoPtr->nLButtonDownItem = -1;
10335 if (infoPtr->bDragging || infoPtr->bMarqueeSelect)
10337 /* Remove the marquee rectangle and release our mouse capture */
10338 if (infoPtr->bMarqueeSelect)
10340 LISTVIEW_InvalidateRect(infoPtr, &infoPtr->marqueeDrawRect);
10341 ReleaseCapture();
10344 SetRectEmpty(&infoPtr->marqueeRect);
10345 SetRectEmpty(&infoPtr->marqueeDrawRect);
10347 infoPtr->bDragging = FALSE;
10348 infoPtr->bMarqueeSelect = FALSE;
10349 infoPtr->bScrolling = FALSE;
10351 KillTimer(infoPtr->hwndSelf, (UINT_PTR) infoPtr);
10352 return 0;
10355 /* if we clicked on a selected item, edit the label */
10356 if(lvHitTestInfo.iItem == infoPtr->nEditLabelItem && (lvHitTestInfo.flags & LVHT_ONITEMLABEL))
10358 /* we want to make sure the user doesn't want to do a double click. So we will
10359 * delay the edit. WM_LBUTTONDBLCLICK will cancel the timer
10361 infoPtr->itemEdit.fEnabled = TRUE;
10362 infoPtr->itemEdit.iItem = lvHitTestInfo.iItem;
10363 SetTimer(infoPtr->hwndSelf,
10364 (UINT_PTR)&infoPtr->itemEdit,
10365 GetDoubleClickTime(),
10366 LISTVIEW_DelayedEditItem);
10369 return 0;
10372 /***
10373 * DESCRIPTION:
10374 * Destroys the listview control (called after WM_DESTROY).
10376 * PARAMETER(S):
10377 * [I] infoPtr : valid pointer to the listview structure
10379 * RETURN:
10380 * Zero
10382 static LRESULT LISTVIEW_NCDestroy(LISTVIEW_INFO *infoPtr)
10384 INT i;
10386 TRACE("()\n");
10388 /* destroy data structure */
10389 DPA_Destroy(infoPtr->hdpaItems);
10390 DPA_Destroy(infoPtr->hdpaItemIds);
10391 DPA_Destroy(infoPtr->hdpaPosX);
10392 DPA_Destroy(infoPtr->hdpaPosY);
10393 /* columns */
10394 for (i = 0; i < DPA_GetPtrCount(infoPtr->hdpaColumns); i++)
10395 Free(DPA_GetPtr(infoPtr->hdpaColumns, i));
10396 DPA_Destroy(infoPtr->hdpaColumns);
10397 ranges_destroy(infoPtr->selectionRanges);
10399 /* destroy image lists */
10400 if (!(infoPtr->dwStyle & LVS_SHAREIMAGELISTS))
10402 ImageList_Destroy(infoPtr->himlNormal);
10403 ImageList_Destroy(infoPtr->himlSmall);
10404 ImageList_Destroy(infoPtr->himlState);
10407 /* destroy font, bkgnd brush */
10408 infoPtr->hFont = 0;
10409 if (infoPtr->hDefaultFont) DeleteObject(infoPtr->hDefaultFont);
10410 if (infoPtr->clrBk != CLR_NONE) DeleteObject(infoPtr->hBkBrush);
10412 SetWindowLongPtrW(infoPtr->hwndSelf, 0, 0);
10414 /* free listview info pointer*/
10415 Free(infoPtr);
10417 return 0;
10420 /***
10421 * DESCRIPTION:
10422 * Handles notifications.
10424 * PARAMETER(S):
10425 * [I] infoPtr : valid pointer to the listview structure
10426 * [I] lpnmhdr : notification information
10428 * RETURN:
10429 * Zero
10431 static LRESULT LISTVIEW_Notify(LISTVIEW_INFO *infoPtr, NMHDR *lpnmhdr)
10433 NMHEADERW *lpnmh;
10435 TRACE("(lpnmhdr=%p)\n", lpnmhdr);
10437 if (!lpnmhdr || lpnmhdr->hwndFrom != infoPtr->hwndHeader) return 0;
10439 /* remember: HDN_LAST < HDN_FIRST */
10440 if (lpnmhdr->code > HDN_FIRST || lpnmhdr->code < HDN_LAST) return 0;
10441 lpnmh = (NMHEADERW *)lpnmhdr;
10443 if (lpnmh->iItem < 0 || lpnmh->iItem >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return 0;
10445 switch (lpnmhdr->code)
10447 case HDN_TRACKW:
10448 case HDN_TRACKA:
10450 COLUMN_INFO *lpColumnInfo;
10451 POINT ptOrigin;
10452 INT x;
10454 if (!lpnmh->pitem || !(lpnmh->pitem->mask & HDI_WIDTH))
10455 break;
10457 /* remove the old line (if any) */
10458 LISTVIEW_DrawTrackLine(infoPtr);
10460 /* compute & draw the new line */
10461 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, lpnmh->iItem);
10462 x = lpColumnInfo->rcHeader.left + lpnmh->pitem->cxy;
10463 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
10464 infoPtr->xTrackLine = x + ptOrigin.x;
10465 LISTVIEW_DrawTrackLine(infoPtr);
10466 return notify_forward_header(infoPtr, lpnmh);
10469 case HDN_ENDTRACKA:
10470 case HDN_ENDTRACKW:
10471 /* remove the track line (if any) */
10472 LISTVIEW_DrawTrackLine(infoPtr);
10473 infoPtr->xTrackLine = -1;
10474 return notify_forward_header(infoPtr, lpnmh);
10476 case HDN_BEGINDRAG:
10477 if ((infoPtr->dwLvExStyle & LVS_EX_HEADERDRAGDROP) == 0) return 1;
10478 return notify_forward_header(infoPtr, lpnmh);
10480 case HDN_ENDDRAG:
10481 infoPtr->colRectsDirty = TRUE;
10482 LISTVIEW_InvalidateList(infoPtr);
10483 return notify_forward_header(infoPtr, lpnmh);
10485 case HDN_ITEMCHANGEDW:
10486 case HDN_ITEMCHANGEDA:
10488 COLUMN_INFO *lpColumnInfo;
10489 HDITEMW hdi;
10490 INT dx, cxy;
10492 if (!lpnmh->pitem || !(lpnmh->pitem->mask & HDI_WIDTH))
10494 hdi.mask = HDI_WIDTH;
10495 if (!SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, lpnmh->iItem, (LPARAM)&hdi)) return 0;
10496 cxy = hdi.cxy;
10498 else
10499 cxy = lpnmh->pitem->cxy;
10501 /* determine how much we change since the last know position */
10502 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, lpnmh->iItem);
10503 dx = cxy - (lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left);
10504 if (dx != 0)
10506 lpColumnInfo->rcHeader.right += dx;
10508 hdi.mask = HDI_ORDER;
10509 SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, lpnmh->iItem, (LPARAM)&hdi);
10511 /* not the rightmost one */
10512 if (hdi.iOrder + 1 < DPA_GetPtrCount(infoPtr->hdpaColumns))
10514 INT nIndex = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX,
10515 hdi.iOrder + 1, 0);
10516 LISTVIEW_ScrollColumns(infoPtr, nIndex, dx);
10518 else
10520 /* only needs to update the scrolls */
10521 infoPtr->nItemWidth += dx;
10522 LISTVIEW_UpdateScroll(infoPtr);
10524 LISTVIEW_UpdateItemSize(infoPtr);
10525 if (infoPtr->uView == LV_VIEW_DETAILS && is_redrawing(infoPtr))
10527 POINT ptOrigin;
10528 RECT rcCol = lpColumnInfo->rcHeader;
10530 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
10531 OffsetRect(&rcCol, ptOrigin.x, 0);
10533 rcCol.top = infoPtr->rcList.top;
10534 rcCol.bottom = infoPtr->rcList.bottom;
10536 /* resizing left-aligned columns leaves most of the left side untouched */
10537 if ((lpColumnInfo->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_LEFT)
10539 INT nMaxDirty = infoPtr->nEllipsisWidth + infoPtr->ntmMaxCharWidth;
10540 if (dx > 0)
10541 nMaxDirty += dx;
10542 rcCol.left = max (rcCol.left, rcCol.right - nMaxDirty);
10545 /* when shrinking the last column clear the now unused field */
10546 if (hdi.iOrder == DPA_GetPtrCount(infoPtr->hdpaColumns) - 1)
10548 RECT right;
10550 rcCol.right -= dx;
10552 /* deal with right from rightmost column area */
10553 right.left = rcCol.right;
10554 right.top = rcCol.top;
10555 right.bottom = rcCol.bottom;
10556 right.right = infoPtr->rcList.right;
10558 LISTVIEW_InvalidateRect(infoPtr, &right);
10561 LISTVIEW_InvalidateRect(infoPtr, &rcCol);
10564 break;
10567 case HDN_ITEMCLICKW:
10568 case HDN_ITEMCLICKA:
10570 /* Handle sorting by Header Column */
10571 NMLISTVIEW nmlv;
10573 ZeroMemory(&nmlv, sizeof(NMLISTVIEW));
10574 nmlv.iItem = -1;
10575 nmlv.iSubItem = lpnmh->iItem;
10576 notify_listview(infoPtr, LVN_COLUMNCLICK, &nmlv);
10577 return notify_forward_header(infoPtr, lpnmh);
10580 case HDN_DIVIDERDBLCLICKW:
10581 case HDN_DIVIDERDBLCLICKA:
10582 /* FIXME: for LVS_EX_HEADERINALLVIEWS and not LV_VIEW_DETAILS
10583 we should use LVSCW_AUTOSIZE_USEHEADER, helper rework or
10584 split needed for that */
10585 LISTVIEW_SetColumnWidth(infoPtr, lpnmh->iItem, LVSCW_AUTOSIZE);
10586 return notify_forward_header(infoPtr, lpnmh);
10588 return 0;
10591 /***
10592 * DESCRIPTION:
10593 * Paint non-client area of control.
10595 * PARAMETER(S):
10596 * [I] infoPtr : valid pointer to the listview structureof the sender
10597 * [I] region : update region
10599 * RETURN:
10600 * TRUE - frame was painted
10601 * FALSE - call default window proc
10603 static BOOL LISTVIEW_NCPaint(const LISTVIEW_INFO *infoPtr, HRGN region)
10605 HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
10606 HDC dc;
10607 RECT r;
10608 HRGN cliprgn;
10609 int cxEdge = GetSystemMetrics (SM_CXEDGE),
10610 cyEdge = GetSystemMetrics (SM_CYEDGE);
10612 if (!theme)
10613 return DefWindowProcW (infoPtr->hwndSelf, WM_NCPAINT, (WPARAM)region, 0);
10615 GetWindowRect(infoPtr->hwndSelf, &r);
10617 cliprgn = CreateRectRgn (r.left + cxEdge, r.top + cyEdge,
10618 r.right - cxEdge, r.bottom - cyEdge);
10619 if (region != (HRGN)1)
10620 CombineRgn (cliprgn, cliprgn, region, RGN_AND);
10621 OffsetRect(&r, -r.left, -r.top);
10623 dc = GetDCEx(infoPtr->hwndSelf, region, DCX_WINDOW|DCX_INTERSECTRGN);
10624 OffsetRect(&r, -r.left, -r.top);
10626 if (IsThemeBackgroundPartiallyTransparent (theme, 0, 0))
10627 DrawThemeParentBackground(infoPtr->hwndSelf, dc, &r);
10628 DrawThemeBackground (theme, dc, 0, 0, &r, 0);
10629 ReleaseDC(infoPtr->hwndSelf, dc);
10631 /* Call default proc to get the scrollbars etc. painted */
10632 DefWindowProcW (infoPtr->hwndSelf, WM_NCPAINT, (WPARAM)cliprgn, 0);
10634 return FALSE;
10637 /***
10638 * DESCRIPTION:
10639 * Determines the type of structure to use.
10641 * PARAMETER(S):
10642 * [I] infoPtr : valid pointer to the listview structureof the sender
10643 * [I] hwndFrom : listview window handle
10644 * [I] nCommand : command specifying the nature of the WM_NOTIFYFORMAT
10646 * RETURN:
10647 * Zero
10649 static LRESULT LISTVIEW_NotifyFormat(LISTVIEW_INFO *infoPtr, HWND hwndFrom, INT nCommand)
10651 TRACE("(hwndFrom=%p, nCommand=%d)\n", hwndFrom, nCommand);
10653 if (nCommand == NF_REQUERY)
10654 infoPtr->notifyFormat = SendMessageW(infoPtr->hwndNotify, WM_NOTIFYFORMAT, (WPARAM)infoPtr->hwndSelf, NF_QUERY);
10656 return infoPtr->notifyFormat;
10659 /***
10660 * DESCRIPTION:
10661 * Paints/Repaints the listview control. Internal use.
10663 * PARAMETER(S):
10664 * [I] infoPtr : valid pointer to the listview structure
10665 * [I] hdc : device context handle
10667 * RETURN:
10668 * Zero
10670 static LRESULT LISTVIEW_Paint(LISTVIEW_INFO *infoPtr, HDC hdc)
10672 TRACE("(hdc=%p)\n", hdc);
10674 if (infoPtr->bNoItemMetrics && infoPtr->nItemCount)
10676 infoPtr->bNoItemMetrics = FALSE;
10677 LISTVIEW_UpdateItemSize(infoPtr);
10678 if (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON)
10679 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
10680 LISTVIEW_UpdateScroll(infoPtr);
10683 if (infoPtr->hwndHeader) UpdateWindow(infoPtr->hwndHeader);
10685 if (hdc)
10686 LISTVIEW_Refresh(infoPtr, hdc, NULL);
10687 else
10689 PAINTSTRUCT ps;
10691 hdc = BeginPaint(infoPtr->hwndSelf, &ps);
10692 if (!hdc) return 1;
10693 LISTVIEW_Refresh(infoPtr, hdc, ps.fErase ? &ps.rcPaint : NULL);
10694 EndPaint(infoPtr->hwndSelf, &ps);
10697 return 0;
10700 /***
10701 * DESCRIPTION:
10702 * Paints/Repaints the listview control, WM_PAINT handler.
10704 * PARAMETER(S):
10705 * [I] infoPtr : valid pointer to the listview structure
10706 * [I] hdc : device context handle
10708 * RETURN:
10709 * Zero
10711 static inline LRESULT LISTVIEW_WMPaint(LISTVIEW_INFO *infoPtr, HDC hdc)
10713 TRACE("(hdc=%p)\n", hdc);
10715 if (!is_redrawing(infoPtr))
10716 return DefWindowProcW (infoPtr->hwndSelf, WM_PAINT, (WPARAM)hdc, 0);
10718 return LISTVIEW_Paint(infoPtr, hdc);
10721 /***
10722 * DESCRIPTION:
10723 * Paints/Repaints the listview control.
10725 * PARAMETER(S):
10726 * [I] infoPtr : valid pointer to the listview structure
10727 * [I] hdc : device context handle
10728 * [I] options : drawing options
10730 * RETURN:
10731 * Zero
10733 static LRESULT LISTVIEW_PrintClient(LISTVIEW_INFO *infoPtr, HDC hdc, DWORD options)
10735 FIXME("Partial Stub: (hdc=%p options=0x%08x)\n", hdc, options);
10737 if ((options & PRF_CHECKVISIBLE) && !IsWindowVisible(infoPtr->hwndSelf))
10738 return 0;
10740 if (options & PRF_ERASEBKGND)
10741 LISTVIEW_EraseBkgnd(infoPtr, hdc);
10743 if (options & PRF_CLIENT)
10744 LISTVIEW_Paint(infoPtr, hdc);
10746 return 0;
10750 /***
10751 * DESCRIPTION:
10752 * Processes double click messages (right mouse button).
10754 * PARAMETER(S):
10755 * [I] infoPtr : valid pointer to the listview structure
10756 * [I] wKey : key flag
10757 * [I] x,y : mouse coordinate
10759 * RETURN:
10760 * Zero
10762 static LRESULT LISTVIEW_RButtonDblClk(const LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
10764 LVHITTESTINFO lvHitTestInfo;
10766 TRACE("(key=%hu,X=%u,Y=%u)\n", wKey, x, y);
10768 /* send NM_RELEASEDCAPTURE notification */
10769 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
10771 /* send NM_RDBLCLK notification */
10772 lvHitTestInfo.pt.x = x;
10773 lvHitTestInfo.pt.y = y;
10774 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
10775 notify_click(infoPtr, NM_RDBLCLK, &lvHitTestInfo);
10777 return 0;
10780 /***
10781 * DESCRIPTION:
10782 * Processes WM_RBUTTONDOWN message and corresponding drag operation.
10784 * PARAMETER(S):
10785 * [I] infoPtr : valid pointer to the listview structure
10786 * [I] wKey : key flag
10787 * [I] x, y : mouse coordinate
10789 * RETURN:
10790 * Zero
10792 static LRESULT LISTVIEW_RButtonDown(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
10794 LVHITTESTINFO ht;
10795 INT item;
10797 TRACE("(key=%hu, x=%d, y=%d)\n", wKey, x, y);
10799 /* send NM_RELEASEDCAPTURE notification */
10800 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
10802 /* determine the index of the selected item */
10803 ht.pt.x = x;
10804 ht.pt.y = y;
10805 item = LISTVIEW_HitTest(infoPtr, &ht, TRUE, TRUE);
10807 /* make sure the listview control window has the focus */
10808 if (!infoPtr->bFocus) SetFocus(infoPtr->hwndSelf);
10810 if ((item >= 0) && (item < infoPtr->nItemCount))
10812 LISTVIEW_SetItemFocus(infoPtr, item);
10813 if (!((wKey & MK_SHIFT) || (wKey & MK_CONTROL)) &&
10814 !LISTVIEW_GetItemState(infoPtr, item, LVIS_SELECTED))
10815 LISTVIEW_SetSelection(infoPtr, item);
10817 else
10818 LISTVIEW_DeselectAll(infoPtr);
10820 if (LISTVIEW_TrackMouse(infoPtr, ht.pt))
10822 if (ht.iItem != -1)
10824 NMLISTVIEW nmlv;
10826 memset(&nmlv, 0, sizeof(nmlv));
10827 nmlv.iItem = ht.iItem;
10828 nmlv.ptAction = ht.pt;
10830 notify_listview(infoPtr, LVN_BEGINRDRAG, &nmlv);
10833 else
10835 SetFocus(infoPtr->hwndSelf);
10837 ht.pt.x = x;
10838 ht.pt.y = y;
10839 LISTVIEW_HitTest(infoPtr, &ht, TRUE, FALSE);
10841 if (notify_click(infoPtr, NM_RCLICK, &ht))
10843 /* Send a WM_CONTEXTMENU message in response to the WM_RBUTTONUP */
10844 SendMessageW(infoPtr->hwndSelf, WM_CONTEXTMENU,
10845 (WPARAM)infoPtr->hwndSelf, (LPARAM)GetMessagePos());
10849 return 0;
10852 /***
10853 * DESCRIPTION:
10854 * Sets the cursor.
10856 * PARAMETER(S):
10857 * [I] infoPtr : valid pointer to the listview structure
10858 * [I] hwnd : window handle of window containing the cursor
10859 * [I] nHittest : hit-test code
10860 * [I] wMouseMsg : ideintifier of the mouse message
10862 * RETURN:
10863 * TRUE if cursor is set
10864 * FALSE otherwise
10866 static BOOL LISTVIEW_SetCursor(const LISTVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
10868 LVHITTESTINFO lvHitTestInfo;
10870 if (!LISTVIEW_IsHotTracking(infoPtr)) goto forward;
10872 if (!infoPtr->hHotCursor) goto forward;
10874 GetCursorPos(&lvHitTestInfo.pt);
10875 if (LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, FALSE, FALSE) < 0) goto forward;
10877 SetCursor(infoPtr->hHotCursor);
10879 return TRUE;
10881 forward:
10883 return DefWindowProcW(infoPtr->hwndSelf, WM_SETCURSOR, wParam, lParam);
10886 /***
10887 * DESCRIPTION:
10888 * Sets the focus.
10890 * PARAMETER(S):
10891 * [I] infoPtr : valid pointer to the listview structure
10892 * [I] hwndLoseFocus : handle of previously focused window
10894 * RETURN:
10895 * Zero
10897 static LRESULT LISTVIEW_SetFocus(LISTVIEW_INFO *infoPtr, HWND hwndLoseFocus)
10899 TRACE("(hwndLoseFocus=%p)\n", hwndLoseFocus);
10901 /* if we have the focus already, there's nothing to do */
10902 if (infoPtr->bFocus) return 0;
10904 /* send NM_SETFOCUS notification */
10905 if (!notify(infoPtr, NM_SETFOCUS)) return 0;
10907 /* set window focus flag */
10908 infoPtr->bFocus = TRUE;
10910 /* put the focus rect back on */
10911 LISTVIEW_ShowFocusRect(infoPtr, TRUE);
10913 /* redraw all visible selected items */
10914 LISTVIEW_InvalidateSelectedItems(infoPtr);
10916 return 0;
10919 /***
10920 * DESCRIPTION:
10921 * Sets the font.
10923 * PARAMETER(S):
10924 * [I] infoPtr : valid pointer to the listview structure
10925 * [I] fRedraw : font handle
10926 * [I] fRedraw : redraw flag
10928 * RETURN:
10929 * Zero
10931 static LRESULT LISTVIEW_SetFont(LISTVIEW_INFO *infoPtr, HFONT hFont, WORD fRedraw)
10933 HFONT oldFont = infoPtr->hFont;
10934 INT oldHeight = infoPtr->nItemHeight;
10936 TRACE("(hfont=%p,redraw=%hu)\n", hFont, fRedraw);
10938 infoPtr->hFont = hFont ? hFont : infoPtr->hDefaultFont;
10939 if (infoPtr->hFont == oldFont) return 0;
10941 LISTVIEW_SaveTextMetrics(infoPtr);
10943 infoPtr->nItemHeight = LISTVIEW_CalculateItemHeight(infoPtr);
10945 if (infoPtr->uView == LV_VIEW_DETAILS)
10947 SendMessageW(infoPtr->hwndHeader, WM_SETFONT, (WPARAM)hFont, MAKELPARAM(fRedraw, 0));
10948 LISTVIEW_UpdateSize(infoPtr);
10949 LISTVIEW_UpdateScroll(infoPtr);
10951 else if (infoPtr->nItemHeight != oldHeight)
10952 LISTVIEW_UpdateScroll(infoPtr);
10954 if (fRedraw) LISTVIEW_InvalidateList(infoPtr);
10956 return 0;
10959 /***
10960 * DESCRIPTION:
10961 * Message handling for WM_SETREDRAW.
10962 * For the Listview, it invalidates the entire window (the doc specifies otherwise)
10964 * PARAMETER(S):
10965 * [I] infoPtr : valid pointer to the listview structure
10966 * [I] redraw: state of redraw flag
10968 * RETURN:
10969 * Zero.
10971 static LRESULT LISTVIEW_SetRedraw(LISTVIEW_INFO *infoPtr, BOOL redraw)
10973 TRACE("old=%d, new=%d\n", infoPtr->redraw, redraw);
10975 if (infoPtr->redraw == !!redraw)
10976 return 0;
10978 if (!(infoPtr->redraw = !!redraw))
10979 return 0;
10981 if (is_autoarrange(infoPtr))
10982 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
10983 LISTVIEW_UpdateScroll(infoPtr);
10985 /* despite what the WM_SETREDRAW docs says, apps expect us
10986 * to invalidate the listview here... stupid! */
10987 LISTVIEW_InvalidateList(infoPtr);
10989 return 0;
10992 /***
10993 * DESCRIPTION:
10994 * Resizes the listview control. This function processes WM_SIZE
10995 * messages. At this time, the width and height are not used.
10997 * PARAMETER(S):
10998 * [I] infoPtr : valid pointer to the listview structure
10999 * [I] Width : new width
11000 * [I] Height : new height
11002 * RETURN:
11003 * Zero
11005 static LRESULT LISTVIEW_Size(LISTVIEW_INFO *infoPtr, int Width, int Height)
11007 RECT rcOld = infoPtr->rcList;
11009 TRACE("(width=%d, height=%d)\n", Width, Height);
11011 LISTVIEW_UpdateSize(infoPtr);
11012 if (EqualRect(&rcOld, &infoPtr->rcList)) return 0;
11014 /* do not bother with display related stuff if we're not redrawing */
11015 if (!is_redrawing(infoPtr)) return 0;
11017 if (is_autoarrange(infoPtr))
11018 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
11020 LISTVIEW_UpdateScroll(infoPtr);
11022 /* refresh all only for lists whose height changed significantly */
11023 if ((infoPtr->uView == LV_VIEW_LIST) &&
11024 (rcOld.bottom - rcOld.top) / infoPtr->nItemHeight !=
11025 (infoPtr->rcList.bottom - infoPtr->rcList.top) / infoPtr->nItemHeight)
11026 LISTVIEW_InvalidateList(infoPtr);
11028 return 0;
11031 /***
11032 * DESCRIPTION:
11033 * Sets the size information.
11035 * PARAMETER(S):
11036 * [I] infoPtr : valid pointer to the listview structure
11038 * RETURN:
11039 * None
11041 static void LISTVIEW_UpdateSize(LISTVIEW_INFO *infoPtr)
11043 TRACE("uView=%d, rcList(old)=%s\n", infoPtr->uView, wine_dbgstr_rect(&infoPtr->rcList));
11045 GetClientRect(infoPtr->hwndSelf, &infoPtr->rcList);
11047 if (infoPtr->uView == LV_VIEW_LIST)
11049 /* Apparently the "LIST" style is supposed to have the same
11050 * number of items in a column even if there is no scroll bar.
11051 * Since if a scroll bar already exists then the bottom is already
11052 * reduced, only reduce if the scroll bar does not currently exist.
11053 * The "2" is there to mimic the native control. I think it may be
11054 * related to either padding or edges. (GLA 7/2002)
11056 if (!(GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE) & WS_HSCROLL))
11057 infoPtr->rcList.bottom -= GetSystemMetrics(SM_CYHSCROLL);
11058 infoPtr->rcList.bottom = max (infoPtr->rcList.bottom - 2, 0);
11061 /* if control created invisible header isn't created */
11062 if (infoPtr->hwndHeader)
11064 HDLAYOUT hl;
11065 WINDOWPOS wp;
11067 hl.prc = &infoPtr->rcList;
11068 hl.pwpos = &wp;
11069 SendMessageW( infoPtr->hwndHeader, HDM_LAYOUT, 0, (LPARAM)&hl );
11070 TRACE(" wp.flags=0x%08x, wp=%d,%d (%dx%d)\n", wp.flags, wp.x, wp.y, wp.cx, wp.cy);
11072 if (LISTVIEW_IsHeaderEnabled(infoPtr))
11073 wp.flags |= SWP_SHOWWINDOW;
11074 else
11076 wp.flags |= SWP_HIDEWINDOW;
11077 wp.cy = 0;
11080 SetWindowPos(wp.hwnd, wp.hwndInsertAfter, wp.x, wp.y, wp.cx, wp.cy, wp.flags);
11081 TRACE(" after SWP wp=%d,%d (%dx%d)\n", wp.x, wp.y, wp.cx, wp.cy);
11083 infoPtr->rcList.top = max(wp.cy, 0);
11085 /* extra padding for grid */
11086 if (infoPtr->uView == LV_VIEW_DETAILS && infoPtr->dwLvExStyle & LVS_EX_GRIDLINES)
11087 infoPtr->rcList.top += 2;
11089 TRACE(" rcList=%s\n", wine_dbgstr_rect(&infoPtr->rcList));
11092 /***
11093 * DESCRIPTION:
11094 * Processes WM_STYLECHANGED messages.
11096 * PARAMETER(S):
11097 * [I] infoPtr : valid pointer to the listview structure
11098 * [I] wStyleType : window style type (normal or extended)
11099 * [I] lpss : window style information
11101 * RETURN:
11102 * Zero
11104 static INT LISTVIEW_StyleChanged(LISTVIEW_INFO *infoPtr, WPARAM wStyleType,
11105 const STYLESTRUCT *lpss)
11107 UINT uNewView = lpss->styleNew & LVS_TYPEMASK;
11108 UINT uOldView = lpss->styleOld & LVS_TYPEMASK;
11109 UINT style;
11111 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
11112 wStyleType, lpss->styleOld, lpss->styleNew);
11114 if (wStyleType != GWL_STYLE) return 0;
11116 infoPtr->dwStyle = lpss->styleNew;
11118 if (((lpss->styleOld & WS_HSCROLL) != 0)&&
11119 ((lpss->styleNew & WS_HSCROLL) == 0))
11120 ShowScrollBar(infoPtr->hwndSelf, SB_HORZ, FALSE);
11122 if (((lpss->styleOld & WS_VSCROLL) != 0)&&
11123 ((lpss->styleNew & WS_VSCROLL) == 0))
11124 ShowScrollBar(infoPtr->hwndSelf, SB_VERT, FALSE);
11126 if (uNewView != uOldView)
11128 HIMAGELIST himl;
11130 /* LVM_SETVIEW doesn't change window style bits within LVS_TYPEMASK,
11131 changing style updates current view only when view bits change. */
11132 map_style_view(infoPtr);
11133 SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
11134 ShowWindow(infoPtr->hwndHeader, SW_HIDE);
11136 ShowScrollBar(infoPtr->hwndSelf, SB_BOTH, FALSE);
11137 SetRectEmpty(&infoPtr->rcFocus);
11139 himl = (uNewView == LVS_ICON ? infoPtr->himlNormal : infoPtr->himlSmall);
11140 set_icon_size(&infoPtr->iconSize, himl, uNewView != LVS_ICON);
11142 if (uNewView == LVS_REPORT)
11144 HDLAYOUT hl;
11145 WINDOWPOS wp;
11147 LISTVIEW_CreateHeader( infoPtr );
11149 hl.prc = &infoPtr->rcList;
11150 hl.pwpos = &wp;
11151 SendMessageW( infoPtr->hwndHeader, HDM_LAYOUT, 0, (LPARAM)&hl );
11152 SetWindowPos(infoPtr->hwndHeader, infoPtr->hwndSelf, wp.x, wp.y, wp.cx, wp.cy,
11153 wp.flags | ((infoPtr->dwStyle & LVS_NOCOLUMNHEADER)
11154 ? SWP_HIDEWINDOW : SWP_SHOWWINDOW));
11157 LISTVIEW_UpdateItemSize(infoPtr);
11160 if (uNewView == LVS_REPORT || infoPtr->dwLvExStyle & LVS_EX_HEADERINALLVIEWS)
11162 if ((lpss->styleOld ^ lpss->styleNew) & LVS_NOCOLUMNHEADER)
11164 if (lpss->styleNew & LVS_NOCOLUMNHEADER)
11166 /* Turn off the header control */
11167 style = GetWindowLongW(infoPtr->hwndHeader, GWL_STYLE);
11168 TRACE("Hide header control, was 0x%08x\n", style);
11169 SetWindowLongW(infoPtr->hwndHeader, GWL_STYLE, style | HDS_HIDDEN);
11170 } else {
11171 /* Turn on the header control */
11172 if ((style = GetWindowLongW(infoPtr->hwndHeader, GWL_STYLE)) & HDS_HIDDEN)
11174 TRACE("Show header control, was 0x%08x\n", style);
11175 SetWindowLongW(infoPtr->hwndHeader, GWL_STYLE, (style & ~HDS_HIDDEN) | WS_VISIBLE);
11181 if ( (uNewView == LVS_ICON || uNewView == LVS_SMALLICON) &&
11182 (uNewView != uOldView || ((lpss->styleNew ^ lpss->styleOld) & LVS_ALIGNMASK)) )
11183 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
11185 /* update the size of the client area */
11186 LISTVIEW_UpdateSize(infoPtr);
11188 /* add scrollbars if needed */
11189 LISTVIEW_UpdateScroll(infoPtr);
11191 /* invalidate client area + erase background */
11192 LISTVIEW_InvalidateList(infoPtr);
11194 return 0;
11197 /***
11198 * DESCRIPTION:
11199 * Processes WM_STYLECHANGING messages.
11201 * PARAMETER(S):
11202 * [I] wStyleType : window style type (normal or extended)
11203 * [I0] lpss : window style information
11205 * RETURN:
11206 * Zero
11208 static INT LISTVIEW_StyleChanging(WPARAM wStyleType,
11209 STYLESTRUCT *lpss)
11211 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
11212 wStyleType, lpss->styleOld, lpss->styleNew);
11214 /* don't forward LVS_OWNERDATA only if not already set to */
11215 if ((lpss->styleNew ^ lpss->styleOld) & LVS_OWNERDATA)
11217 if (lpss->styleOld & LVS_OWNERDATA)
11218 lpss->styleNew |= LVS_OWNERDATA;
11219 else
11220 lpss->styleNew &= ~LVS_OWNERDATA;
11223 return 0;
11226 /***
11227 * DESCRIPTION:
11228 * Processes WM_SHOWWINDOW messages.
11230 * PARAMETER(S):
11231 * [I] infoPtr : valid pointer to the listview structure
11232 * [I] bShown : window is being shown (FALSE when hidden)
11233 * [I] iStatus : window show status
11235 * RETURN:
11236 * Zero
11238 static LRESULT LISTVIEW_ShowWindow(LISTVIEW_INFO *infoPtr, WPARAM bShown, LPARAM iStatus)
11240 /* header delayed creation */
11241 if ((infoPtr->uView == LV_VIEW_DETAILS) && bShown)
11243 LISTVIEW_CreateHeader(infoPtr);
11245 if (!(LVS_NOCOLUMNHEADER & infoPtr->dwStyle))
11246 ShowWindow(infoPtr->hwndHeader, SW_SHOWNORMAL);
11249 return DefWindowProcW(infoPtr->hwndSelf, WM_SHOWWINDOW, bShown, iStatus);
11252 /***
11253 * DESCRIPTION:
11254 * Processes CCM_GETVERSION messages.
11256 * PARAMETER(S):
11257 * [I] infoPtr : valid pointer to the listview structure
11259 * RETURN:
11260 * Current version
11262 static inline LRESULT LISTVIEW_GetVersion(const LISTVIEW_INFO *infoPtr)
11264 return infoPtr->iVersion;
11267 /***
11268 * DESCRIPTION:
11269 * Processes CCM_SETVERSION messages.
11271 * PARAMETER(S):
11272 * [I] infoPtr : valid pointer to the listview structure
11273 * [I] iVersion : version to be set
11275 * RETURN:
11276 * -1 when requested version is greater than DLL version;
11277 * previous version otherwise
11279 static LRESULT LISTVIEW_SetVersion(LISTVIEW_INFO *infoPtr, DWORD iVersion)
11281 INT iOldVersion = infoPtr->iVersion;
11283 if (iVersion > COMCTL32_VERSION)
11284 return -1;
11286 infoPtr->iVersion = iVersion;
11288 TRACE("new version %d\n", iVersion);
11290 return iOldVersion;
11293 /***
11294 * DESCRIPTION:
11295 * Window procedure of the listview control.
11298 static LRESULT WINAPI
11299 LISTVIEW_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
11301 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongPtrW(hwnd, 0);
11303 TRACE("(hwnd=%p uMsg=%x wParam=%lx lParam=%lx)\n", hwnd, uMsg, wParam, lParam);
11305 if (!infoPtr && (uMsg != WM_NCCREATE))
11306 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
11308 switch (uMsg)
11310 case LVM_APPROXIMATEVIEWRECT:
11311 return LISTVIEW_ApproximateViewRect(infoPtr, (INT)wParam,
11312 LOWORD(lParam), HIWORD(lParam));
11313 case LVM_ARRANGE:
11314 return LISTVIEW_Arrange(infoPtr, (INT)wParam);
11316 case LVM_CANCELEDITLABEL:
11317 return LISTVIEW_CancelEditLabel(infoPtr);
11319 case LVM_CREATEDRAGIMAGE:
11320 return (LRESULT)LISTVIEW_CreateDragImage(infoPtr, (INT)wParam, (LPPOINT)lParam);
11322 case LVM_DELETEALLITEMS:
11323 return LISTVIEW_DeleteAllItems(infoPtr, FALSE);
11325 case LVM_DELETECOLUMN:
11326 return LISTVIEW_DeleteColumn(infoPtr, (INT)wParam);
11328 case LVM_DELETEITEM:
11329 return LISTVIEW_DeleteItem(infoPtr, (INT)wParam);
11331 case LVM_EDITLABELA:
11332 case LVM_EDITLABELW:
11333 return (LRESULT)LISTVIEW_EditLabelT(infoPtr, (INT)wParam,
11334 uMsg == LVM_EDITLABELW);
11335 /* case LVM_ENABLEGROUPVIEW: */
11337 case LVM_ENSUREVISIBLE:
11338 return LISTVIEW_EnsureVisible(infoPtr, (INT)wParam, (BOOL)lParam);
11340 case LVM_FINDITEMW:
11341 return LISTVIEW_FindItemW(infoPtr, (INT)wParam, (LPLVFINDINFOW)lParam);
11343 case LVM_FINDITEMA:
11344 return LISTVIEW_FindItemA(infoPtr, (INT)wParam, (LPLVFINDINFOA)lParam);
11346 case LVM_GETBKCOLOR:
11347 return infoPtr->clrBk;
11349 /* case LVM_GETBKIMAGE: */
11351 case LVM_GETCALLBACKMASK:
11352 return infoPtr->uCallbackMask;
11354 case LVM_GETCOLUMNA:
11355 case LVM_GETCOLUMNW:
11356 return LISTVIEW_GetColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam,
11357 uMsg == LVM_GETCOLUMNW);
11359 case LVM_GETCOLUMNORDERARRAY:
11360 return LISTVIEW_GetColumnOrderArray(infoPtr, (INT)wParam, (LPINT)lParam);
11362 case LVM_GETCOLUMNWIDTH:
11363 return LISTVIEW_GetColumnWidth(infoPtr, (INT)wParam);
11365 case LVM_GETCOUNTPERPAGE:
11366 return LISTVIEW_GetCountPerPage(infoPtr);
11368 case LVM_GETEDITCONTROL:
11369 return (LRESULT)infoPtr->hwndEdit;
11371 case LVM_GETEXTENDEDLISTVIEWSTYLE:
11372 return infoPtr->dwLvExStyle;
11374 /* case LVM_GETGROUPINFO: */
11376 /* case LVM_GETGROUPMETRICS: */
11378 case LVM_GETHEADER:
11379 return (LRESULT)infoPtr->hwndHeader;
11381 case LVM_GETHOTCURSOR:
11382 return (LRESULT)infoPtr->hHotCursor;
11384 case LVM_GETHOTITEM:
11385 return infoPtr->nHotItem;
11387 case LVM_GETHOVERTIME:
11388 return infoPtr->dwHoverTime;
11390 case LVM_GETIMAGELIST:
11391 return (LRESULT)LISTVIEW_GetImageList(infoPtr, (INT)wParam);
11393 /* case LVM_GETINSERTMARK: */
11395 /* case LVM_GETINSERTMARKCOLOR: */
11397 /* case LVM_GETINSERTMARKRECT: */
11399 case LVM_GETISEARCHSTRINGA:
11400 case LVM_GETISEARCHSTRINGW:
11401 FIXME("LVM_GETISEARCHSTRING: unimplemented\n");
11402 return FALSE;
11404 case LVM_GETITEMA:
11405 case LVM_GETITEMW:
11406 return LISTVIEW_GetItemExtT(infoPtr, (LPLVITEMW)lParam, uMsg == LVM_GETITEMW);
11408 case LVM_GETITEMCOUNT:
11409 return infoPtr->nItemCount;
11411 case LVM_GETITEMPOSITION:
11412 return LISTVIEW_GetItemPosition(infoPtr, (INT)wParam, (LPPOINT)lParam);
11414 case LVM_GETITEMRECT:
11415 return LISTVIEW_GetItemRect(infoPtr, (INT)wParam, (LPRECT)lParam);
11417 case LVM_GETITEMSPACING:
11418 return LISTVIEW_GetItemSpacing(infoPtr, (BOOL)wParam);
11420 case LVM_GETITEMSTATE:
11421 return LISTVIEW_GetItemState(infoPtr, (INT)wParam, (UINT)lParam);
11423 case LVM_GETITEMTEXTA:
11424 case LVM_GETITEMTEXTW:
11425 return LISTVIEW_GetItemTextT(infoPtr, (INT)wParam, (LPLVITEMW)lParam,
11426 uMsg == LVM_GETITEMTEXTW);
11428 case LVM_GETNEXTITEM:
11429 return LISTVIEW_GetNextItem(infoPtr, (INT)wParam, LOWORD(lParam));
11431 case LVM_GETNUMBEROFWORKAREAS:
11432 FIXME("LVM_GETNUMBEROFWORKAREAS: unimplemented\n");
11433 return 1;
11435 case LVM_GETORIGIN:
11436 if (!lParam) return FALSE;
11437 if (infoPtr->uView == LV_VIEW_DETAILS ||
11438 infoPtr->uView == LV_VIEW_LIST) return FALSE;
11439 LISTVIEW_GetOrigin(infoPtr, (LPPOINT)lParam);
11440 return TRUE;
11442 /* case LVM_GETOUTLINECOLOR: */
11444 /* case LVM_GETSELECTEDCOLUMN: */
11446 case LVM_GETSELECTEDCOUNT:
11447 return LISTVIEW_GetSelectedCount(infoPtr);
11449 case LVM_GETSELECTIONMARK:
11450 return infoPtr->nSelectionMark;
11452 case LVM_GETSTRINGWIDTHA:
11453 case LVM_GETSTRINGWIDTHW:
11454 return LISTVIEW_GetStringWidthT(infoPtr, (LPCWSTR)lParam,
11455 uMsg == LVM_GETSTRINGWIDTHW);
11457 case LVM_GETSUBITEMRECT:
11458 return LISTVIEW_GetSubItemRect(infoPtr, (UINT)wParam, (LPRECT)lParam);
11460 case LVM_GETTEXTBKCOLOR:
11461 return infoPtr->clrTextBk;
11463 case LVM_GETTEXTCOLOR:
11464 return infoPtr->clrText;
11466 /* case LVM_GETTILEINFO: */
11468 /* case LVM_GETTILEVIEWINFO: */
11470 case LVM_GETTOOLTIPS:
11471 if( !infoPtr->hwndToolTip )
11472 infoPtr->hwndToolTip = COMCTL32_CreateToolTip( hwnd );
11473 return (LRESULT)infoPtr->hwndToolTip;
11475 case LVM_GETTOPINDEX:
11476 return LISTVIEW_GetTopIndex(infoPtr);
11478 case LVM_GETUNICODEFORMAT:
11479 return (infoPtr->notifyFormat == NFR_UNICODE);
11481 case LVM_GETVIEW:
11482 return infoPtr->uView;
11484 case LVM_GETVIEWRECT:
11485 return LISTVIEW_GetViewRect(infoPtr, (LPRECT)lParam);
11487 case LVM_GETWORKAREAS:
11488 FIXME("LVM_GETWORKAREAS: unimplemented\n");
11489 return FALSE;
11491 /* case LVM_HASGROUP: */
11493 case LVM_HITTEST:
11494 return LISTVIEW_HitTest(infoPtr, (LPLVHITTESTINFO)lParam, FALSE, TRUE);
11496 case LVM_INSERTCOLUMNA:
11497 case LVM_INSERTCOLUMNW:
11498 return LISTVIEW_InsertColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam,
11499 uMsg == LVM_INSERTCOLUMNW);
11501 /* case LVM_INSERTGROUP: */
11503 /* case LVM_INSERTGROUPSORTED: */
11505 case LVM_INSERTITEMA:
11506 case LVM_INSERTITEMW:
11507 return LISTVIEW_InsertItemT(infoPtr, (LPLVITEMW)lParam, uMsg == LVM_INSERTITEMW);
11509 /* case LVM_INSERTMARKHITTEST: */
11511 /* case LVM_ISGROUPVIEWENABLED: */
11513 case LVM_ISITEMVISIBLE:
11514 return LISTVIEW_IsItemVisible(infoPtr, (INT)wParam);
11516 case LVM_MAPIDTOINDEX:
11517 return LISTVIEW_MapIdToIndex(infoPtr, (UINT)wParam);
11519 case LVM_MAPINDEXTOID:
11520 return LISTVIEW_MapIndexToId(infoPtr, (INT)wParam);
11522 /* case LVM_MOVEGROUP: */
11524 /* case LVM_MOVEITEMTOGROUP: */
11526 case LVM_REDRAWITEMS:
11527 return LISTVIEW_RedrawItems(infoPtr, (INT)wParam, (INT)lParam);
11529 /* case LVM_REMOVEALLGROUPS: */
11531 /* case LVM_REMOVEGROUP: */
11533 case LVM_SCROLL:
11534 return LISTVIEW_Scroll(infoPtr, (INT)wParam, (INT)lParam);
11536 case LVM_SETBKCOLOR:
11537 return LISTVIEW_SetBkColor(infoPtr, (COLORREF)lParam);
11539 /* case LVM_SETBKIMAGE: */
11541 case LVM_SETCALLBACKMASK:
11542 infoPtr->uCallbackMask = (UINT)wParam;
11543 return TRUE;
11545 case LVM_SETCOLUMNA:
11546 case LVM_SETCOLUMNW:
11547 return LISTVIEW_SetColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam,
11548 uMsg == LVM_SETCOLUMNW);
11550 case LVM_SETCOLUMNORDERARRAY:
11551 return LISTVIEW_SetColumnOrderArray(infoPtr, (INT)wParam, (LPINT)lParam);
11553 case LVM_SETCOLUMNWIDTH:
11554 return LISTVIEW_SetColumnWidth(infoPtr, (INT)wParam, (short)LOWORD(lParam));
11556 case LVM_SETEXTENDEDLISTVIEWSTYLE:
11557 return LISTVIEW_SetExtendedListViewStyle(infoPtr, (DWORD)wParam, (DWORD)lParam);
11559 /* case LVM_SETGROUPINFO: */
11561 /* case LVM_SETGROUPMETRICS: */
11563 case LVM_SETHOTCURSOR:
11564 return (LRESULT)LISTVIEW_SetHotCursor(infoPtr, (HCURSOR)lParam);
11566 case LVM_SETHOTITEM:
11567 return LISTVIEW_SetHotItem(infoPtr, (INT)wParam);
11569 case LVM_SETHOVERTIME:
11570 return LISTVIEW_SetHoverTime(infoPtr, (DWORD)lParam);
11572 case LVM_SETICONSPACING:
11573 if(lParam == -1)
11574 return LISTVIEW_SetIconSpacing(infoPtr, -1, -1);
11575 return LISTVIEW_SetIconSpacing(infoPtr, LOWORD(lParam), HIWORD(lParam));
11577 case LVM_SETIMAGELIST:
11578 return (LRESULT)LISTVIEW_SetImageList(infoPtr, (INT)wParam, (HIMAGELIST)lParam);
11580 /* case LVM_SETINFOTIP: */
11582 /* case LVM_SETINSERTMARK: */
11584 /* case LVM_SETINSERTMARKCOLOR: */
11586 case LVM_SETITEMA:
11587 case LVM_SETITEMW:
11589 if (infoPtr->dwStyle & LVS_OWNERDATA) return FALSE;
11590 return LISTVIEW_SetItemT(infoPtr, (LPLVITEMW)lParam, (uMsg == LVM_SETITEMW));
11593 case LVM_SETITEMCOUNT:
11594 return LISTVIEW_SetItemCount(infoPtr, (INT)wParam, (DWORD)lParam);
11596 case LVM_SETITEMPOSITION:
11598 POINT pt;
11599 pt.x = (short)LOWORD(lParam);
11600 pt.y = (short)HIWORD(lParam);
11601 return LISTVIEW_SetItemPosition(infoPtr, (INT)wParam, &pt);
11604 case LVM_SETITEMPOSITION32:
11605 return LISTVIEW_SetItemPosition(infoPtr, (INT)wParam, (POINT*)lParam);
11607 case LVM_SETITEMSTATE:
11608 return LISTVIEW_SetItemState(infoPtr, (INT)wParam, (LPLVITEMW)lParam);
11610 case LVM_SETITEMTEXTA:
11611 case LVM_SETITEMTEXTW:
11612 return LISTVIEW_SetItemTextT(infoPtr, (INT)wParam, (LPLVITEMW)lParam,
11613 uMsg == LVM_SETITEMTEXTW);
11615 /* case LVM_SETOUTLINECOLOR: */
11617 /* case LVM_SETSELECTEDCOLUMN: */
11619 case LVM_SETSELECTIONMARK:
11620 return LISTVIEW_SetSelectionMark(infoPtr, (INT)lParam);
11622 case LVM_SETTEXTBKCOLOR:
11623 return LISTVIEW_SetTextBkColor(infoPtr, (COLORREF)lParam);
11625 case LVM_SETTEXTCOLOR:
11626 return LISTVIEW_SetTextColor(infoPtr, (COLORREF)lParam);
11628 /* case LVM_SETTILEINFO: */
11630 /* case LVM_SETTILEVIEWINFO: */
11632 /* case LVM_SETTILEWIDTH: */
11634 case LVM_SETTOOLTIPS:
11635 return (LRESULT)LISTVIEW_SetToolTips(infoPtr, (HWND)lParam);
11637 case LVM_SETUNICODEFORMAT:
11638 return LISTVIEW_SetUnicodeFormat(infoPtr, wParam);
11640 case LVM_SETVIEW:
11641 return LISTVIEW_SetView(infoPtr, wParam);
11643 /* case LVM_SETWORKAREAS: */
11645 /* case LVM_SORTGROUPS: */
11647 case LVM_SORTITEMS:
11648 case LVM_SORTITEMSEX:
11649 return LISTVIEW_SortItems(infoPtr, (PFNLVCOMPARE)lParam, wParam,
11650 uMsg == LVM_SORTITEMSEX);
11651 case LVM_SUBITEMHITTEST:
11652 return LISTVIEW_HitTest(infoPtr, (LPLVHITTESTINFO)lParam, TRUE, FALSE);
11654 case LVM_UPDATE:
11655 return LISTVIEW_Update(infoPtr, (INT)wParam);
11657 case CCM_GETVERSION:
11658 return LISTVIEW_GetVersion(infoPtr);
11660 case CCM_SETVERSION:
11661 return LISTVIEW_SetVersion(infoPtr, wParam);
11663 case WM_CHAR:
11664 return LISTVIEW_ProcessLetterKeys( infoPtr, wParam, lParam );
11666 case WM_COMMAND:
11667 return LISTVIEW_Command(infoPtr, wParam, lParam);
11669 case WM_NCCREATE:
11670 return LISTVIEW_NCCreate(hwnd, (LPCREATESTRUCTW)lParam);
11672 case WM_CREATE:
11673 return LISTVIEW_Create(hwnd, (LPCREATESTRUCTW)lParam);
11675 case WM_DESTROY:
11676 return LISTVIEW_Destroy(infoPtr);
11678 case WM_ENABLE:
11679 return LISTVIEW_Enable(infoPtr);
11681 case WM_ERASEBKGND:
11682 return LISTVIEW_EraseBkgnd(infoPtr, (HDC)wParam);
11684 case WM_GETDLGCODE:
11685 return DLGC_WANTCHARS | DLGC_WANTARROWS;
11687 case WM_GETFONT:
11688 return (LRESULT)infoPtr->hFont;
11690 case WM_HSCROLL:
11691 return LISTVIEW_HScroll(infoPtr, (INT)LOWORD(wParam), 0);
11693 case WM_KEYDOWN:
11694 return LISTVIEW_KeyDown(infoPtr, (INT)wParam, (LONG)lParam);
11696 case WM_KILLFOCUS:
11697 return LISTVIEW_KillFocus(infoPtr);
11699 case WM_LBUTTONDBLCLK:
11700 return LISTVIEW_LButtonDblClk(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11702 case WM_LBUTTONDOWN:
11703 return LISTVIEW_LButtonDown(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11705 case WM_LBUTTONUP:
11706 return LISTVIEW_LButtonUp(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11708 case WM_MOUSEMOVE:
11709 return LISTVIEW_MouseMove (infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11711 case WM_MOUSEHOVER:
11712 return LISTVIEW_MouseHover(infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11714 case WM_NCDESTROY:
11715 return LISTVIEW_NCDestroy(infoPtr);
11717 case WM_NCPAINT:
11718 return LISTVIEW_NCPaint(infoPtr, (HRGN)wParam);
11720 case WM_NOTIFY:
11721 return LISTVIEW_Notify(infoPtr, (LPNMHDR)lParam);
11723 case WM_NOTIFYFORMAT:
11724 return LISTVIEW_NotifyFormat(infoPtr, (HWND)wParam, (INT)lParam);
11726 case WM_PRINTCLIENT:
11727 return LISTVIEW_PrintClient(infoPtr, (HDC)wParam, (DWORD)lParam);
11729 case WM_PAINT:
11730 return LISTVIEW_WMPaint(infoPtr, (HDC)wParam);
11732 case WM_RBUTTONDBLCLK:
11733 return LISTVIEW_RButtonDblClk(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11735 case WM_RBUTTONDOWN:
11736 return LISTVIEW_RButtonDown(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11738 case WM_SETCURSOR:
11739 return LISTVIEW_SetCursor(infoPtr, wParam, lParam);
11741 case WM_SETFOCUS:
11742 return LISTVIEW_SetFocus(infoPtr, (HWND)wParam);
11744 case WM_SETFONT:
11745 return LISTVIEW_SetFont(infoPtr, (HFONT)wParam, (WORD)lParam);
11747 case WM_SETREDRAW:
11748 return LISTVIEW_SetRedraw(infoPtr, (BOOL)wParam);
11750 case WM_SHOWWINDOW:
11751 return LISTVIEW_ShowWindow(infoPtr, wParam, lParam);
11753 case WM_STYLECHANGED:
11754 return LISTVIEW_StyleChanged(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
11756 case WM_STYLECHANGING:
11757 return LISTVIEW_StyleChanging(wParam, (LPSTYLESTRUCT)lParam);
11759 case WM_SYSCOLORCHANGE:
11760 COMCTL32_RefreshSysColors();
11761 return 0;
11763 /* case WM_TIMER: */
11764 case WM_THEMECHANGED:
11765 return LISTVIEW_ThemeChanged(infoPtr);
11767 case WM_VSCROLL:
11768 return LISTVIEW_VScroll(infoPtr, (INT)LOWORD(wParam), 0);
11770 case WM_MOUSEWHEEL:
11771 if (wParam & (MK_SHIFT | MK_CONTROL))
11772 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
11773 return LISTVIEW_MouseWheel(infoPtr, (short int)HIWORD(wParam));
11775 case WM_WINDOWPOSCHANGED:
11776 if (!(((WINDOWPOS *)lParam)->flags & SWP_NOSIZE))
11778 SetWindowPos(infoPtr->hwndSelf, 0, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOACTIVATE |
11779 SWP_NOZORDER | SWP_NOMOVE | SWP_NOSIZE);
11781 if ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (infoPtr->uView == LV_VIEW_DETAILS))
11783 if (notify_measureitem(infoPtr)) LISTVIEW_InvalidateList(infoPtr);
11785 LISTVIEW_Size(infoPtr, ((WINDOWPOS *)lParam)->cx, ((WINDOWPOS *)lParam)->cy);
11787 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
11789 /* case WM_WININICHANGE: */
11791 default:
11792 if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
11793 ERR("unknown msg %04x wp=%08lx lp=%08lx\n", uMsg, wParam, lParam);
11795 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
11800 /***
11801 * DESCRIPTION:
11802 * Registers the window class.
11804 * PARAMETER(S):
11805 * None
11807 * RETURN:
11808 * None
11810 void LISTVIEW_Register(void)
11812 WNDCLASSW wndClass;
11814 ZeroMemory(&wndClass, sizeof(WNDCLASSW));
11815 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS;
11816 wndClass.lpfnWndProc = LISTVIEW_WindowProc;
11817 wndClass.cbClsExtra = 0;
11818 wndClass.cbWndExtra = sizeof(LISTVIEW_INFO *);
11819 wndClass.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
11820 wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
11821 wndClass.lpszClassName = WC_LISTVIEWW;
11822 RegisterClassW(&wndClass);
11825 /***
11826 * DESCRIPTION:
11827 * Unregisters the window class.
11829 * PARAMETER(S):
11830 * None
11832 * RETURN:
11833 * None
11835 void LISTVIEW_Unregister(void)
11837 UnregisterClassW(WC_LISTVIEWW, NULL);
11840 /***
11841 * DESCRIPTION:
11842 * Handle any WM_COMMAND messages
11844 * PARAMETER(S):
11845 * [I] infoPtr : valid pointer to the listview structure
11846 * [I] wParam : the first message parameter
11847 * [I] lParam : the second message parameter
11849 * RETURN:
11850 * Zero.
11852 static LRESULT LISTVIEW_Command(LISTVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
11855 TRACE("(%p %x %x %lx)\n", infoPtr, HIWORD(wParam), LOWORD(wParam), lParam);
11857 if (!infoPtr->hwndEdit) return 0;
11859 switch (HIWORD(wParam))
11861 case EN_UPDATE:
11864 * Adjust the edit window size
11866 WCHAR buffer[1024];
11867 HDC hdc = GetDC(infoPtr->hwndEdit);
11868 HFONT hFont, hOldFont = 0;
11869 RECT rect;
11870 SIZE sz;
11872 if (!infoPtr->hwndEdit || !hdc) return 0;
11873 GetWindowTextW(infoPtr->hwndEdit, buffer, sizeof(buffer)/sizeof(buffer[0]));
11874 GetWindowRect(infoPtr->hwndEdit, &rect);
11876 /* Select font to get the right dimension of the string */
11877 hFont = (HFONT)SendMessageW(infoPtr->hwndEdit, WM_GETFONT, 0, 0);
11878 if (hFont)
11880 hOldFont = SelectObject(hdc, hFont);
11883 if (GetTextExtentPoint32W(hdc, buffer, lstrlenW(buffer), &sz))
11885 TEXTMETRICW textMetric;
11887 /* Add Extra spacing for the next character */
11888 GetTextMetricsW(hdc, &textMetric);
11889 sz.cx += (textMetric.tmMaxCharWidth * 2);
11891 SetWindowPos(infoPtr->hwndEdit, NULL, 0, 0, sz.cx,
11892 rect.bottom - rect.top, SWP_DRAWFRAME | SWP_NOMOVE | SWP_NOZORDER);
11894 if (hFont)
11895 SelectObject(hdc, hOldFont);
11897 ReleaseDC(infoPtr->hwndEdit, hdc);
11899 break;
11901 case EN_KILLFOCUS:
11903 LISTVIEW_CancelEditLabel(infoPtr);
11904 break;
11907 default:
11908 return SendMessageW (infoPtr->hwndNotify, WM_COMMAND, wParam, lParam);
11911 return 0;